2 * MPlayer AAC decoder using libfaad2
4 * Copyright (C) 2002 Felix Buenemann <atmosfear at users.sourceforge.net>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "ad_internal.h"
31 #include "libaf/reorder_ch.h"
33 static const ad_info_t info
=
35 "AAC (MPEG2/4 Advanced Audio Coding)",
44 /* configure maximum supported channels, *
45 * this is theoretically max. 64 chans */
46 #define FAAD_MAX_CHANNELS 8
47 #define FAAD_BUFFLEN (FAAD_MIN_STREAMSIZE*FAAD_MAX_CHANNELS)
49 //#define AAC_DUMP_COMPRESSED
51 static faacDecHandle faac_hdec
;
52 static faacDecFrameInfo faac_finfo
;
54 static int preinit(sh_audio_t
*sh
)
56 sh
->audio_out_minsize
=8192*FAAD_MAX_CHANNELS
;
57 sh
->audio_in_minsize
=FAAD_BUFFLEN
;
61 static int aac_probe(unsigned char *buffer
, int len
)
64 mp_msg(MSGT_DECAUDIO
,MSGL_V
, "\nAAC_PROBE: %d bytes\n", len
);
67 ((buffer
[i
] == 0xff) && ((buffer
[i
+1] & 0xf6) == 0xf0)) ||
68 (buffer
[i
] == 'A' && buffer
[i
+1] == 'D' && buffer
[i
+2] == 'I' && buffer
[i
+3] == 'F')
73 mp_msg(MSGT_DECAUDIO
,MSGL_V
, "AUDIO PAYLOAD: %x %x %x %x\n", buffer
[i
], buffer
[i
+1], buffer
[i
+2], buffer
[i
+3]);
76 mp_msg(MSGT_DECAUDIO
,MSGL_V
, "\nAAC_PROBE: ret %d\n", pos
);
80 static int init(sh_audio_t
*sh
)
82 struct MPOpts
*opts
= sh
->opts
;
83 unsigned long faac_samplerate
;
84 unsigned char faac_channels
;
85 int faac_init
, pos
= 0;
86 faac_hdec
= faacDecOpen();
88 // If we don't get the ES descriptor, try manual config
89 if(!sh
->codecdata_len
&& sh
->wf
) {
90 sh
->codecdata_len
= sh
->wf
->cbSize
;
91 sh
->codecdata
= malloc(sh
->codecdata_len
);
92 memcpy(sh
->codecdata
, sh
->wf
+1, sh
->codecdata_len
);
93 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"FAAD: codecdata extracted from WAVEFORMATEX\n");
95 if(!sh
->codecdata_len
|| sh
->format
== mmioFOURCC('M', 'P', '4', 'L')) {
96 faacDecConfigurationPtr faac_conf
;
97 /* Set the default object type and samplerate */
98 /* This is useful for RAW AAC files */
99 faac_conf
= faacDecGetCurrentConfiguration(faac_hdec
);
101 faac_conf
->defSampleRate
= sh
->samplerate
;
102 /* XXX: FAAD support FLOAT output, how do we handle
103 * that (FAAD_FMT_FLOAT)? ::atmos
105 if (opts
->audio_output_channels
<= 2)
106 faac_conf
->downMatrix
= 1;
107 switch(sh
->samplesize
){
109 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"FAAD: 8Bit samplesize not supported by FAAD, assuming 16Bit!\n");
113 faac_conf
->outputFormat
= FAAD_FMT_16BIT
;
116 faac_conf
->outputFormat
= FAAD_FMT_24BIT
;
119 faac_conf
->outputFormat
= FAAD_FMT_32BIT
;
122 //faac_conf->defObjectType = LTP; // => MAIN, LC, SSR, LTP available.
124 faacDecSetConfiguration(faac_hdec
, faac_conf
);
126 sh
->a_in_buffer_len
= demux_read_data(sh
->ds
, sh
->a_in_buffer
, sh
->a_in_buffer_size
);
127 if (!sh
->a_in_buffer_len
) {
128 // faad init will crash with 0 buffer length
129 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Could not get audio data!\n");
132 /* external faad does not have latm lookup support */
133 faac_init
= faacDecInit(faac_hdec
, sh
->a_in_buffer
,
134 sh
->a_in_buffer_len
, &faac_samplerate
, &faac_channels
);
137 pos
= aac_probe(sh
->a_in_buffer
, sh
->a_in_buffer_len
);
139 sh
->a_in_buffer_len
-= pos
;
140 memmove(sh
->a_in_buffer
, &(sh
->a_in_buffer
[pos
]), sh
->a_in_buffer_len
);
141 sh
->a_in_buffer_len
+=
142 demux_read_data(sh
->ds
,&(sh
->a_in_buffer
[sh
->a_in_buffer_len
]),
143 sh
->a_in_buffer_size
- sh
->a_in_buffer_len
);
148 faac_init
= faacDecInit(faac_hdec
, sh
->a_in_buffer
,
149 sh
->a_in_buffer_len
, &faac_samplerate
, &faac_channels
);
152 sh
->a_in_buffer_len
-= (faac_init
> 0)?faac_init
:0; // how many bytes init consumed
153 // XXX FIXME: shouldn't we memcpy() here in a_in_buffer ?? --A'rpi
155 } else { // We have ES DS in codecdata
156 faacDecConfigurationPtr faac_conf
= faacDecGetCurrentConfiguration(faac_hdec
);
157 if (opts
->audio_output_channels
<= 2) {
158 faac_conf
->downMatrix
= 1;
159 faacDecSetConfiguration(faac_hdec
, faac_conf
);
163 for(i = 0; i < sh_audio->codecdata_len; i++)
164 printf("codecdata_dump %d: 0x%02X\n", i, sh_audio->codecdata[i]);*/
166 faac_init
= faacDecInit2(faac_hdec
, sh
->codecdata
,
167 sh
->codecdata_len
, &faac_samplerate
, &faac_channels
);
170 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"FAAD: Failed to initialize the decoder!\n"); // XXX: deal with cleanup!
171 faacDecClose(faac_hdec
);
172 // XXX: free a_in_buffer here or in uninit?
175 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FAAD: Decoder init done (%dBytes)!\n", sh
->a_in_buffer_len
); // XXX: remove or move to debug!
176 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FAAD: Negotiated samplerate: %ldHz channels: %d\n", faac_samplerate
, faac_channels
);
177 // 8 channels is aac channel order #7.
178 sh
->channels
= faac_channels
== 7 ? 8 : faac_channels
;
179 if (opts
->audio_output_channels
<= 2)
180 sh
->channels
= faac_channels
> 1 ? 2 : 1;
181 sh
->samplerate
= faac_samplerate
;
183 //sh->o_bps = sh->samplesize*faac_channels*faac_samplerate;
185 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "FAAD: compressed input bitrate missing, assuming 128kbit/s!\n");
186 sh
->i_bps
= 128*1000/8; // XXX: HACK!!! ::atmos
188 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FAAD: got %dkbit/s bitrate from MP4 header!\n",sh
->i_bps
*8/1000);
193 static void uninit(sh_audio_t
*sh
)
195 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"FAAD: Closing decoder!\n");
196 faacDecClose(faac_hdec
);
199 static int aac_sync(sh_audio_t
*sh
)
202 // do not probe LATM, faad does that
203 if(!sh
->codecdata_len
&& sh
->format
!= mmioFOURCC('M', 'P', '4', 'L')) {
204 if(sh
->a_in_buffer_len
< sh
->a_in_buffer_size
){
205 sh
->a_in_buffer_len
+=
206 demux_read_data(sh
->ds
,&sh
->a_in_buffer
[sh
->a_in_buffer_len
],
207 sh
->a_in_buffer_size
- sh
->a_in_buffer_len
);
209 pos
= aac_probe(sh
->a_in_buffer
, sh
->a_in_buffer_len
);
211 sh
->a_in_buffer_len
-= pos
;
212 memmove(sh
->a_in_buffer
, &(sh
->a_in_buffer
[pos
]), sh
->a_in_buffer_len
);
213 mp_msg(MSGT_DECAUDIO
,MSGL_V
, "\nAAC SYNC AFTER %d bytes\n", pos
);
219 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
223 case ADCTRL_RESYNC_STREAM
:
227 case ADCTRL_SKIP_FRAME
:
231 return CONTROL_UNKNOWN
;
234 #define MAX_FAAD_ERRORS 10
235 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
)
237 int len
= 0, last_dec_len
= 1, errors
= 0;
239 void *faac_sample_buffer
;
241 while(len
< minlen
&& last_dec_len
> 0 && errors
< MAX_FAAD_ERRORS
) {
243 /* update buffer for raw aac streams: */
244 if(!sh
->codecdata_len
)
245 if(sh
->a_in_buffer_len
< sh
->a_in_buffer_size
){
246 sh
->a_in_buffer_len
+=
247 demux_read_data(sh
->ds
,&sh
->a_in_buffer
[sh
->a_in_buffer_len
],
248 sh
->a_in_buffer_size
- sh
->a_in_buffer_len
);
251 #ifdef DUMP_AAC_COMPRESSED
253 for (i
= 0; i
< 16; i
++)
254 printf ("%02X ", sh
->a_in_buffer
[i
]);
258 if(!sh
->codecdata_len
){
261 faac_sample_buffer
= faacDecDecode(faac_hdec
, &faac_finfo
, sh
->a_in_buffer
, sh
->a_in_buffer_len
);
263 /* update buffer index after faacDecDecode */
264 if(faac_finfo
.bytesconsumed
>= sh
->a_in_buffer_len
) {
265 sh
->a_in_buffer_len
=0;
267 sh
->a_in_buffer_len
-=faac_finfo
.bytesconsumed
;
268 memmove(sh
->a_in_buffer
,&sh
->a_in_buffer
[faac_finfo
.bytesconsumed
],sh
->a_in_buffer_len
);
271 if(faac_finfo
.error
> 0) {
272 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"FAAD: error: %s, trying to resync!\n",
273 faacDecGetErrorMessage(faac_finfo
.error
));
274 if (sh
->a_in_buffer_len
<= 0) {
275 errors
= MAX_FAAD_ERRORS
;
278 sh
->a_in_buffer_len
--;
279 memmove(sh
->a_in_buffer
,&sh
->a_in_buffer
[1],sh
->a_in_buffer_len
);
284 } while(errors
< MAX_FAAD_ERRORS
);
286 // packetized (.mp4) aac stream:
287 unsigned char* bufptr
=NULL
;
289 int buflen
=ds_get_packet_pts(sh
->ds
, &bufptr
, &pts
);
291 if (pts
!= MP_NOPTS_VALUE
) {
295 faac_sample_buffer
= faacDecDecode(faac_hdec
, &faac_finfo
, bufptr
, buflen
);
297 //for (j=0;j<faac_finfo.channels;j++) printf("%d:%d\n", j, faac_finfo.channel_position[j]);
299 if(faac_finfo
.error
> 0) {
300 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"FAAD: Failed to decode frame: %s \n",
301 faacDecGetErrorMessage(faac_finfo
.error
));
302 } else if (faac_finfo
.samples
== 0) {
303 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"FAAD: Decoded zero samples!\n");
305 /* XXX: samples already multiplied by channels! */
306 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"FAAD: Successfully decoded frame (%ld Bytes)!\n",
307 sh
->samplesize
*faac_finfo
.samples
);
309 if (sh
->channels
>= 5)
310 reorder_channel_copy_nch(faac_sample_buffer
,
311 AF_CHANNEL_LAYOUT_AAC_DEFAULT
,
312 buf
+len
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
314 faac_finfo
.samples
, sh
->samplesize
);
316 memcpy(buf
+len
,faac_sample_buffer
, sh
->samplesize
*faac_finfo
.samples
);
317 last_dec_len
= sh
->samplesize
*faac_finfo
.samples
;
319 sh
->pts_bytes
+= last_dec_len
;
320 //printf("FAAD: buffer: %d bytes consumed: %d \n", k, faac_finfo.bytesconsumed);