2002-03-26 15:53:18 +00:00
|
|
|
/* ad_faad.c - MPlayer AAC decoder using libfaad2
|
|
|
|
* This file is part of MPlayer, see http://mplayerhq.hu/ for info.
|
|
|
|
* (c)2002 by Felix Buenemann <atmosfear at users.sourceforge.net>
|
|
|
|
* File licensed under the GPL, see http://www.fsf.org/ for more info.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "ad_internal.h"
|
|
|
|
|
|
|
|
static ad_info_t info =
|
|
|
|
{
|
|
|
|
"AAC (MPEG2/4 Advanced Audio Coding)",
|
2002-08-30 20:06:22 +00:00
|
|
|
"faad",
|
2002-03-26 15:53:18 +00:00
|
|
|
"Felix Buenemann",
|
|
|
|
"faad2",
|
2002-08-31 13:09:23 +00:00
|
|
|
"uses libfaad2"
|
2002-03-26 15:53:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LIBAD_EXTERN(faad)
|
|
|
|
|
2006-08-07 08:57:43 +00:00
|
|
|
#ifndef USE_FAAD_INTERNAL
|
2003-09-21 17:05:51 +00:00
|
|
|
#include <faad.h>
|
|
|
|
#else
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "libfaad2/faad.h"
|
2003-09-21 17:05:51 +00:00
|
|
|
#endif
|
2002-03-26 15:53:18 +00:00
|
|
|
|
|
|
|
/* configure maximum supported channels, *
|
|
|
|
* this is theoretically max. 64 chans */
|
|
|
|
#define FAAD_MAX_CHANNELS 6
|
|
|
|
#define FAAD_BUFFLEN (FAAD_MIN_STREAMSIZE*FAAD_MAX_CHANNELS)
|
|
|
|
|
|
|
|
//#define AAC_DUMP_COMPRESSED
|
|
|
|
|
|
|
|
static faacDecHandle faac_hdec;
|
|
|
|
static faacDecFrameInfo faac_finfo;
|
|
|
|
|
|
|
|
static int preinit(sh_audio_t *sh)
|
|
|
|
{
|
2004-04-01 20:14:59 +00:00
|
|
|
sh->audio_out_minsize=8192*FAAD_MAX_CHANNELS;
|
2002-04-01 17:58:04 +00:00
|
|
|
sh->audio_in_minsize=FAAD_BUFFLEN;
|
2002-03-26 15:53:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-01-21 20:50:09 +00:00
|
|
|
static int aac_probe(unsigned char *buffer, int len)
|
|
|
|
{
|
|
|
|
int i = 0, pos = 0;
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V, "\nAAC_PROBE: %d bytes\n", len);
|
|
|
|
while(i <= len-4) {
|
|
|
|
if(
|
2005-06-12 13:30:40 +00:00
|
|
|
((buffer[i] == 0xff) && ((buffer[i+1] & 0xf6) == 0xf0)) ||
|
2005-01-21 20:50:09 +00:00
|
|
|
(buffer[i] == 'A' && buffer[i+1] == 'D' && buffer[i+2] == 'I' && buffer[i+3] == 'F')
|
|
|
|
) {
|
|
|
|
pos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V, "AUDIO PAYLOAD: %x %x %x %x\n", buffer[i], buffer[i+1], buffer[i+2], buffer[i+3]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V, "\nAAC_PROBE: ret %d\n", pos);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2002-03-26 15:53:18 +00:00
|
|
|
static int init(sh_audio_t *sh)
|
|
|
|
{
|
2002-10-07 10:18:46 +00:00
|
|
|
unsigned long faac_samplerate;
|
|
|
|
unsigned char faac_channels;
|
2005-01-21 20:50:09 +00:00
|
|
|
int faac_init, pos = 0;
|
2002-03-26 15:53:18 +00:00
|
|
|
faac_hdec = faacDecOpen();
|
|
|
|
|
|
|
|
// If we don't get the ES descriptor, try manual config
|
2004-05-14 18:47:17 +00:00
|
|
|
if(!sh->codecdata_len && sh->wf) {
|
|
|
|
sh->codecdata_len = sh->wf->cbSize;
|
|
|
|
sh->codecdata = (char*)(sh->wf+1);
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FAAD: codecdata extracted from WAVEFORMATEX\n");
|
|
|
|
}
|
2002-03-26 15:53:18 +00:00
|
|
|
if(!sh->codecdata_len) {
|
|
|
|
#if 1
|
|
|
|
faacDecConfigurationPtr faac_conf;
|
|
|
|
/* Set the default object type and samplerate */
|
|
|
|
/* This is useful for RAW AAC files */
|
|
|
|
faac_conf = faacDecGetCurrentConfiguration(faac_hdec);
|
|
|
|
if(sh->samplerate)
|
|
|
|
faac_conf->defSampleRate = sh->samplerate;
|
|
|
|
/* XXX: FAAD support FLOAT output, how do we handle
|
|
|
|
* that (FAAD_FMT_FLOAT)? ::atmos
|
|
|
|
*/
|
2005-03-29 17:59:08 +00:00
|
|
|
if (audio_output_channels <= 2) faac_conf->downMatrix = 1;
|
2002-03-26 15:53:18 +00:00
|
|
|
switch(sh->samplesize){
|
|
|
|
case 1: // 8Bit
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: 8Bit samplesize not supported by FAAD, assuming 16Bit!\n");
|
|
|
|
default:
|
2004-09-21 20:34:47 +00:00
|
|
|
sh->samplesize=2;
|
2002-03-26 15:53:18 +00:00
|
|
|
case 2: // 16Bit
|
|
|
|
faac_conf->outputFormat = FAAD_FMT_16BIT;
|
|
|
|
break;
|
|
|
|
case 3: // 24Bit
|
|
|
|
faac_conf->outputFormat = FAAD_FMT_24BIT;
|
|
|
|
break;
|
|
|
|
case 4: // 32Bit
|
|
|
|
faac_conf->outputFormat = FAAD_FMT_32BIT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//faac_conf->defObjectType = LTP; // => MAIN, LC, SSR, LTP available.
|
|
|
|
|
|
|
|
faacDecSetConfiguration(faac_hdec, faac_conf);
|
|
|
|
#endif
|
|
|
|
|
2002-08-04 15:33:41 +00:00
|
|
|
sh->a_in_buffer_len = demux_read_data(sh->ds, sh->a_in_buffer, sh->a_in_buffer_size);
|
2005-01-21 20:50:09 +00:00
|
|
|
pos = aac_probe(sh->a_in_buffer, sh->a_in_buffer_len);
|
|
|
|
if(pos) {
|
|
|
|
sh->a_in_buffer_len -= pos;
|
|
|
|
memmove(sh->a_in_buffer, &(sh->a_in_buffer[pos]), sh->a_in_buffer_len);
|
|
|
|
sh->a_in_buffer_len +=
|
|
|
|
demux_read_data(sh->ds,&(sh->a_in_buffer[sh->a_in_buffer_len]),
|
|
|
|
sh->a_in_buffer_size - sh->a_in_buffer_len);
|
|
|
|
pos = 0;
|
|
|
|
}
|
2002-08-04 15:33:41 +00:00
|
|
|
|
2002-03-26 15:53:18 +00:00
|
|
|
/* init the codec */
|
2003-02-07 21:04:35 +00:00
|
|
|
faac_init = faacDecInit(faac_hdec, sh->a_in_buffer,
|
|
|
|
sh->a_in_buffer_len, &faac_samplerate, &faac_channels);
|
2002-08-04 15:33:41 +00:00
|
|
|
|
2002-03-26 15:53:18 +00:00
|
|
|
sh->a_in_buffer_len -= (faac_init > 0)?faac_init:0; // how many bytes init consumed
|
2002-08-04 15:33:41 +00:00
|
|
|
// XXX FIXME: shouldn't we memcpy() here in a_in_buffer ?? --A'rpi
|
2002-03-26 15:53:18 +00:00
|
|
|
|
|
|
|
} else { // We have ES DS in codecdata
|
2005-03-29 17:59:08 +00:00
|
|
|
faacDecConfigurationPtr faac_conf = faacDecGetCurrentConfiguration(faac_hdec);
|
|
|
|
if (audio_output_channels <= 2) {
|
|
|
|
faac_conf->downMatrix = 1;
|
|
|
|
faacDecSetConfiguration(faac_hdec, faac_conf);
|
|
|
|
}
|
|
|
|
|
2002-03-26 15:53:18 +00:00
|
|
|
/*int i;
|
|
|
|
for(i = 0; i < sh_audio->codecdata_len; i++)
|
|
|
|
printf("codecdata_dump %d: 0x%02X\n", i, sh_audio->codecdata[i]);*/
|
|
|
|
|
|
|
|
faac_init = faacDecInit2(faac_hdec, sh->codecdata,
|
|
|
|
sh->codecdata_len, &faac_samplerate, &faac_channels);
|
|
|
|
}
|
|
|
|
if(faac_init < 0) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: Failed to initialize the decoder!\n"); // XXX: deal with cleanup!
|
|
|
|
faacDecClose(faac_hdec);
|
|
|
|
// XXX: free a_in_buffer here or in uninit?
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: Decoder init done (%dBytes)!\n", sh->a_in_buffer_len); // XXX: remove or move to debug!
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: Negotiated samplerate: %ldHz channels: %d\n", faac_samplerate, faac_channels);
|
2002-03-26 15:53:18 +00:00
|
|
|
sh->channels = faac_channels;
|
2005-03-29 17:59:08 +00:00
|
|
|
if (audio_output_channels <= 2) sh->channels = faac_channels > 1 ? 2 : 1;
|
2002-03-26 15:53:18 +00:00
|
|
|
sh->samplerate = faac_samplerate;
|
2004-09-21 20:34:47 +00:00
|
|
|
sh->samplesize=2;
|
2002-03-26 15:53:18 +00:00
|
|
|
//sh->o_bps = sh->samplesize*faac_channels*faac_samplerate;
|
|
|
|
if(!sh->i_bps) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: compressed input bitrate missing, assuming 128kbit/s!\n");
|
|
|
|
sh->i_bps = 128*1000/8; // XXX: HACK!!! ::atmos
|
|
|
|
} else
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: got %dkbit/s bitrate from MP4 header!\n",sh->i_bps*8/1000);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(sh_audio_t *sh)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: Closing decoder!\n");
|
|
|
|
faacDecClose(faac_hdec);
|
|
|
|
}
|
|
|
|
|
2005-01-21 20:50:09 +00:00
|
|
|
static int aac_sync(sh_audio_t *sh)
|
|
|
|
{
|
|
|
|
int pos = 0;
|
|
|
|
if(!sh->codecdata_len) {
|
|
|
|
if(sh->a_in_buffer_len < sh->a_in_buffer_size){
|
|
|
|
sh->a_in_buffer_len +=
|
|
|
|
demux_read_data(sh->ds,&sh->a_in_buffer[sh->a_in_buffer_len],
|
|
|
|
sh->a_in_buffer_size - sh->a_in_buffer_len);
|
|
|
|
}
|
|
|
|
pos = aac_probe(sh->a_in_buffer, sh->a_in_buffer_len);
|
|
|
|
if(pos) {
|
|
|
|
sh->a_in_buffer_len -= pos;
|
|
|
|
memmove(sh->a_in_buffer, &(sh->a_in_buffer[pos]), sh->a_in_buffer_len);
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V, "\nAAC SYNC AFTER %d bytes\n", pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2002-03-26 15:53:18 +00:00
|
|
|
static int control(sh_audio_t *sh,int cmd,void* arg, ...)
|
|
|
|
{
|
|
|
|
switch(cmd)
|
|
|
|
{
|
|
|
|
case ADCTRL_RESYNC_STREAM:
|
2005-01-21 20:50:09 +00:00
|
|
|
aac_sync(sh);
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
#if 0
|
2002-03-26 15:53:18 +00:00
|
|
|
case ADCTRL_SKIP_FRAME:
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2005-09-01 15:59:55 +00:00
|
|
|
#define MAX_FAAD_ERRORS 10
|
2002-03-26 15:53:18 +00:00
|
|
|
static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen)
|
|
|
|
{
|
2005-09-01 15:59:55 +00:00
|
|
|
int j = 0, len = 0, last_dec_len = 1, errors = 0;
|
2002-03-26 16:37:42 +00:00
|
|
|
void *faac_sample_buffer;
|
|
|
|
|
2005-09-01 15:59:55 +00:00
|
|
|
while(len < minlen && last_dec_len > 0 && errors < MAX_FAAD_ERRORS) {
|
2002-08-04 15:33:41 +00:00
|
|
|
|
|
|
|
/* update buffer for raw aac streams: */
|
|
|
|
if(!sh->codecdata_len)
|
2002-03-26 16:37:42 +00:00
|
|
|
if(sh->a_in_buffer_len < sh->a_in_buffer_size){
|
|
|
|
sh->a_in_buffer_len +=
|
|
|
|
demux_read_data(sh->ds,&sh->a_in_buffer[sh->a_in_buffer_len],
|
|
|
|
sh->a_in_buffer_size - sh->a_in_buffer_len);
|
|
|
|
}
|
2002-03-26 15:53:18 +00:00
|
|
|
|
|
|
|
#ifdef DUMP_AAC_COMPRESSED
|
2002-03-26 16:37:42 +00:00
|
|
|
{int i;
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
printf ("%02X ", sh->a_in_buffer[i]);
|
|
|
|
printf ("\n");}
|
2002-03-26 15:53:18 +00:00
|
|
|
#endif
|
2002-08-04 15:33:41 +00:00
|
|
|
|
|
|
|
if(!sh->codecdata_len){
|
|
|
|
// raw aac stream:
|
|
|
|
do {
|
2006-06-08 06:56:13 +00:00
|
|
|
faac_sample_buffer = faacDecDecode(faac_hdec, &faac_finfo, sh->a_in_buffer, sh->a_in_buffer_len);
|
2003-02-07 21:04:35 +00:00
|
|
|
|
2002-03-26 16:37:42 +00:00
|
|
|
/* update buffer index after faacDecDecode */
|
|
|
|
if(faac_finfo.bytesconsumed >= sh->a_in_buffer_len) {
|
|
|
|
sh->a_in_buffer_len=0;
|
|
|
|
} else {
|
|
|
|
sh->a_in_buffer_len-=faac_finfo.bytesconsumed;
|
2005-07-15 22:09:30 +00:00
|
|
|
memmove(sh->a_in_buffer,&sh->a_in_buffer[faac_finfo.bytesconsumed],sh->a_in_buffer_len);
|
2002-03-26 16:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(faac_finfo.error > 0) {
|
2004-09-11 09:07:07 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: error: %s, trying to resync!\n",
|
|
|
|
faacDecGetErrorMessage(faac_finfo.error));
|
2006-09-27 21:45:15 +00:00
|
|
|
if (sh->a_in_buffer_len <= 0) {
|
|
|
|
errors = MAX_FAAD_ERRORS;
|
|
|
|
break;
|
|
|
|
}
|
2006-06-08 06:56:13 +00:00
|
|
|
sh->a_in_buffer_len--;
|
|
|
|
memmove(sh->a_in_buffer,&sh->a_in_buffer[1],sh->a_in_buffer_len);
|
|
|
|
aac_sync(sh);
|
2005-09-01 15:59:55 +00:00
|
|
|
errors++;
|
2002-03-26 16:37:42 +00:00
|
|
|
} else
|
|
|
|
break;
|
2006-06-08 06:56:13 +00:00
|
|
|
} while(errors < MAX_FAAD_ERRORS);
|
2002-08-04 15:33:41 +00:00
|
|
|
} else {
|
|
|
|
// packetized (.mp4) aac stream:
|
|
|
|
unsigned char* bufptr=NULL;
|
2006-04-24 10:58:40 +00:00
|
|
|
double pts;
|
|
|
|
int buflen=ds_get_packet_pts(sh->ds, &bufptr, &pts);
|
2002-08-04 15:33:41 +00:00
|
|
|
if(buflen<=0) break;
|
2006-04-24 10:58:40 +00:00
|
|
|
if (pts != MP_NOPTS_VALUE) {
|
|
|
|
sh->pts = pts;
|
|
|
|
sh->pts_bytes = 0;
|
|
|
|
}
|
2003-02-07 21:04:35 +00:00
|
|
|
faac_sample_buffer = faacDecDecode(faac_hdec, &faac_finfo, bufptr, buflen);
|
2002-08-04 15:33:41 +00:00
|
|
|
}
|
2005-03-29 17:59:08 +00:00
|
|
|
//for (j=0;j<faac_finfo.channels;j++) printf("%d:%d\n", j, faac_finfo.channel_position[j]);
|
2002-08-04 15:33:41 +00:00
|
|
|
|
2002-03-26 16:37:42 +00:00
|
|
|
if(faac_finfo.error > 0) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: Failed to decode frame: %s \n",
|
|
|
|
faacDecGetErrorMessage(faac_finfo.error));
|
|
|
|
} else if (faac_finfo.samples == 0) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FAAD: Decoded zero samples!\n");
|
|
|
|
} else {
|
|
|
|
/* XXX: samples already multiplied by channels! */
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FAAD: Successfully decoded frame (%ld Bytes)!\n",
|
2002-03-26 16:37:42 +00:00
|
|
|
sh->samplesize*faac_finfo.samples);
|
|
|
|
memcpy(buf+len,faac_sample_buffer, sh->samplesize*faac_finfo.samples);
|
2005-09-01 15:59:55 +00:00
|
|
|
last_dec_len = sh->samplesize*faac_finfo.samples;
|
|
|
|
len += last_dec_len;
|
2006-04-24 10:58:40 +00:00
|
|
|
sh->pts_bytes += last_dec_len;
|
2002-03-26 16:37:42 +00:00
|
|
|
//printf("FAAD: buffer: %d bytes consumed: %d \n", k, faac_finfo.bytesconsumed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
2002-03-26 15:53:18 +00:00
|
|
|
}
|