1 /* ad_faad.c - MPlayer AAC decoder using libfaad2
2 * This file is part of MPlayer, see http://mplayerhq.hu/ for info.
3 * (c)2002 by Felix Buenemann <atmosfear at users.sourceforge.net>
4 * File licensed under the GPL, see http://www.fsf.org/ for more info.
12 #include "ad_internal.h"
13 #include "libaf/reorder_ch.h"
15 static ad_info_t info
=
17 "AAC (MPEG2/4 Advanced Audio Coding)",
26 #ifndef USE_FAAD_INTERNAL
29 #include "libfaad2/faad.h"
32 /* configure maximum supported channels, *
33 * this is theoretically max. 64 chans */
34 #define FAAD_MAX_CHANNELS 6
35 #define FAAD_BUFFLEN (FAAD_MIN_STREAMSIZE*FAAD_MAX_CHANNELS)
37 //#define AAC_DUMP_COMPRESSED
39 static faacDecHandle faac_hdec
;
40 static faacDecFrameInfo faac_finfo
;
42 static int preinit(sh_audio_t
*sh
)
44 sh
->audio_out_minsize
=8192*FAAD_MAX_CHANNELS
;
45 sh
->audio_in_minsize
=FAAD_BUFFLEN
;
49 static int aac_probe(unsigned char *buffer
, int len
)
52 mp_msg(MSGT_DECAUDIO
,MSGL_V
, "\nAAC_PROBE: %d bytes\n", len
);
55 ((buffer
[i
] == 0xff) && ((buffer
[i
+1] & 0xf6) == 0xf0)) ||
56 (buffer
[i
] == 'A' && buffer
[i
+1] == 'D' && buffer
[i
+2] == 'I' && buffer
[i
+3] == 'F')
61 mp_msg(MSGT_DECAUDIO
,MSGL_V
, "AUDIO PAYLOAD: %x %x %x %x\n", buffer
[i
], buffer
[i
+1], buffer
[i
+2], buffer
[i
+3]);
64 mp_msg(MSGT_DECAUDIO
,MSGL_V
, "\nAAC_PROBE: ret %d\n", pos
);
68 static int init(sh_audio_t
*sh
)
70 unsigned long faac_samplerate
;
71 unsigned char faac_channels
;
72 int faac_init
, pos
= 0;
73 faac_hdec
= faacDecOpen();
75 // If we don't get the ES descriptor, try manual config
76 if(!sh
->codecdata_len
&& sh
->wf
) {
77 sh
->codecdata_len
= sh
->wf
->cbSize
;
78 sh
->codecdata
= (char*)(sh
->wf
+1);
79 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"FAAD: codecdata extracted from WAVEFORMATEX\n");
81 if(!sh
->codecdata_len
) {
83 faacDecConfigurationPtr faac_conf
;
84 /* Set the default object type and samplerate */
85 /* This is useful for RAW AAC files */
86 faac_conf
= faacDecGetCurrentConfiguration(faac_hdec
);
88 faac_conf
->defSampleRate
= sh
->samplerate
;
89 /* XXX: FAAD support FLOAT output, how do we handle
90 * that (FAAD_FMT_FLOAT)? ::atmos
92 if (audio_output_channels
<= 2) faac_conf
->downMatrix
= 1;
93 switch(sh
->samplesize
){
95 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"FAAD: 8Bit samplesize not supported by FAAD, assuming 16Bit!\n");
99 faac_conf
->outputFormat
= FAAD_FMT_16BIT
;
102 faac_conf
->outputFormat
= FAAD_FMT_24BIT
;
105 faac_conf
->outputFormat
= FAAD_FMT_32BIT
;
108 //faac_conf->defObjectType = LTP; // => MAIN, LC, SSR, LTP available.
110 faacDecSetConfiguration(faac_hdec
, faac_conf
);
113 sh
->a_in_buffer_len
= demux_read_data(sh
->ds
, sh
->a_in_buffer
, sh
->a_in_buffer_size
);
114 pos
= aac_probe(sh
->a_in_buffer
, sh
->a_in_buffer_len
);
116 sh
->a_in_buffer_len
-= pos
;
117 memmove(sh
->a_in_buffer
, &(sh
->a_in_buffer
[pos
]), sh
->a_in_buffer_len
);
118 sh
->a_in_buffer_len
+=
119 demux_read_data(sh
->ds
,&(sh
->a_in_buffer
[sh
->a_in_buffer_len
]),
120 sh
->a_in_buffer_size
- sh
->a_in_buffer_len
);
125 faac_init
= faacDecInit(faac_hdec
, sh
->a_in_buffer
,
126 sh
->a_in_buffer_len
, &faac_samplerate
, &faac_channels
);
128 sh
->a_in_buffer_len
-= (faac_init
> 0)?faac_init
:0; // how many bytes init consumed
129 // XXX FIXME: shouldn't we memcpy() here in a_in_buffer ?? --A'rpi
131 } else { // We have ES DS in codecdata
132 faacDecConfigurationPtr faac_conf
= faacDecGetCurrentConfiguration(faac_hdec
);
133 if (audio_output_channels
<= 2) {
134 faac_conf
->downMatrix
= 1;
135 faacDecSetConfiguration(faac_hdec
, faac_conf
);
139 for(i = 0; i < sh_audio->codecdata_len; i++)
140 printf("codecdata_dump %d: 0x%02X\n", i, sh_audio->codecdata[i]);*/
142 faac_init
= faacDecInit2(faac_hdec
, sh
->codecdata
,
143 sh
->codecdata_len
, &faac_samplerate
, &faac_channels
);
146 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"FAAD: Failed to initialize the decoder!\n"); // XXX: deal with cleanup!
147 faacDecClose(faac_hdec
);
148 // XXX: free a_in_buffer here or in uninit?
151 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FAAD: Decoder init done (%dBytes)!\n", sh
->a_in_buffer_len
); // XXX: remove or move to debug!
152 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FAAD: Negotiated samplerate: %ldHz channels: %d\n", faac_samplerate
, faac_channels
);
153 sh
->channels
= faac_channels
;
154 if (audio_output_channels
<= 2) sh
->channels
= faac_channels
> 1 ? 2 : 1;
155 sh
->samplerate
= faac_samplerate
;
157 //sh->o_bps = sh->samplesize*faac_channels*faac_samplerate;
159 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"FAAD: compressed input bitrate missing, assuming 128kbit/s!\n");
160 sh
->i_bps
= 128*1000/8; // XXX: HACK!!! ::atmos
162 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FAAD: got %dkbit/s bitrate from MP4 header!\n",sh
->i_bps
*8/1000);
167 static void uninit(sh_audio_t
*sh
)
169 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FAAD: Closing decoder!\n");
170 faacDecClose(faac_hdec
);
173 static int aac_sync(sh_audio_t
*sh
)
176 if(!sh
->codecdata_len
) {
177 if(sh
->a_in_buffer_len
< sh
->a_in_buffer_size
){
178 sh
->a_in_buffer_len
+=
179 demux_read_data(sh
->ds
,&sh
->a_in_buffer
[sh
->a_in_buffer_len
],
180 sh
->a_in_buffer_size
- sh
->a_in_buffer_len
);
182 pos
= aac_probe(sh
->a_in_buffer
, sh
->a_in_buffer_len
);
184 sh
->a_in_buffer_len
-= pos
;
185 memmove(sh
->a_in_buffer
, &(sh
->a_in_buffer
[pos
]), sh
->a_in_buffer_len
);
186 mp_msg(MSGT_DECAUDIO
,MSGL_V
, "\nAAC SYNC AFTER %d bytes\n", pos
);
192 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
196 case ADCTRL_RESYNC_STREAM
:
200 case ADCTRL_SKIP_FRAME
:
204 return CONTROL_UNKNOWN
;
207 #define MAX_FAAD_ERRORS 10
208 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
)
210 int len
= 0, last_dec_len
= 1, errors
= 0;
212 void *faac_sample_buffer
;
214 while(len
< minlen
&& last_dec_len
> 0 && errors
< MAX_FAAD_ERRORS
) {
216 /* update buffer for raw aac streams: */
217 if(!sh
->codecdata_len
)
218 if(sh
->a_in_buffer_len
< sh
->a_in_buffer_size
){
219 sh
->a_in_buffer_len
+=
220 demux_read_data(sh
->ds
,&sh
->a_in_buffer
[sh
->a_in_buffer_len
],
221 sh
->a_in_buffer_size
- sh
->a_in_buffer_len
);
224 #ifdef DUMP_AAC_COMPRESSED
226 for (i
= 0; i
< 16; i
++)
227 printf ("%02X ", sh
->a_in_buffer
[i
]);
231 if(!sh
->codecdata_len
){
234 faac_sample_buffer
= faacDecDecode(faac_hdec
, &faac_finfo
, sh
->a_in_buffer
, sh
->a_in_buffer_len
);
236 /* update buffer index after faacDecDecode */
237 if(faac_finfo
.bytesconsumed
>= sh
->a_in_buffer_len
) {
238 sh
->a_in_buffer_len
=0;
240 sh
->a_in_buffer_len
-=faac_finfo
.bytesconsumed
;
241 memmove(sh
->a_in_buffer
,&sh
->a_in_buffer
[faac_finfo
.bytesconsumed
],sh
->a_in_buffer_len
);
244 if(faac_finfo
.error
> 0) {
245 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"FAAD: error: %s, trying to resync!\n",
246 faacDecGetErrorMessage(faac_finfo
.error
));
247 if (sh
->a_in_buffer_len
<= 0) {
248 errors
= MAX_FAAD_ERRORS
;
251 sh
->a_in_buffer_len
--;
252 memmove(sh
->a_in_buffer
,&sh
->a_in_buffer
[1],sh
->a_in_buffer_len
);
257 } while(errors
< MAX_FAAD_ERRORS
);
259 // packetized (.mp4) aac stream:
260 unsigned char* bufptr
=NULL
;
262 int buflen
=ds_get_packet_pts(sh
->ds
, &bufptr
, &pts
);
264 if (pts
!= MP_NOPTS_VALUE
) {
268 faac_sample_buffer
= faacDecDecode(faac_hdec
, &faac_finfo
, bufptr
, buflen
);
270 //for (j=0;j<faac_finfo.channels;j++) printf("%d:%d\n", j, faac_finfo.channel_position[j]);
272 if(faac_finfo
.error
> 0) {
273 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"FAAD: Failed to decode frame: %s \n",
274 faacDecGetErrorMessage(faac_finfo
.error
));
275 } else if (faac_finfo
.samples
== 0) {
276 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"FAAD: Decoded zero samples!\n");
278 /* XXX: samples already multiplied by channels! */
279 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"FAAD: Successfully decoded frame (%ld Bytes)!\n",
280 sh
->samplesize
*faac_finfo
.samples
);
282 if (sh
->channels
>= 5)
283 reorder_channel_copy_nch(faac_sample_buffer
,
284 AF_CHANNEL_LAYOUT_AAC_DEFAULT
,
285 buf
+len
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
287 faac_finfo
.samples
, sh
->samplesize
);
289 memcpy(buf
+len
,faac_sample_buffer
, sh
->samplesize
*faac_finfo
.samples
);
290 last_dec_len
= sh
->samplesize
*faac_finfo
.samples
;
292 sh
->pts_bytes
+= last_dec_len
;
293 //printf("FAAD: buffer: %d bytes consumed: %d \n", k, faac_finfo.bytesconsumed);