demux_pva.c: Remove completely nonsensical le2me usages
[mplayer/glamo.git] / libmpcodecs / ad_faad.c
blob2dc6c1f5804fcf928c574018ce5b18a912ada08c
1 /*
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.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
27 #include "config.h"
28 #include "ad_internal.h"
29 #include "libaf/reorder_ch.h"
31 static const ad_info_t info =
33 "AAC (MPEG2/4 Advanced Audio Coding)",
34 "faad",
35 "Felix Buenemann",
36 "faad2",
37 "uses libfaad2"
40 LIBAD_EXTERN(faad)
42 #ifndef CONFIG_FAAD_INTERNAL
43 #include <faad.h>
44 #else
45 #include "libfaad2/faad.h"
46 #endif
48 /* configure maximum supported channels, *
49 * this is theoretically max. 64 chans */
50 #define FAAD_MAX_CHANNELS 8
51 #define FAAD_BUFFLEN (FAAD_MIN_STREAMSIZE*FAAD_MAX_CHANNELS)
53 //#define AAC_DUMP_COMPRESSED
55 static faacDecHandle faac_hdec;
56 static faacDecFrameInfo faac_finfo;
58 static int preinit(sh_audio_t *sh)
60 sh->audio_out_minsize=8192*FAAD_MAX_CHANNELS;
61 sh->audio_in_minsize=FAAD_BUFFLEN;
62 return 1;
65 static int aac_probe(unsigned char *buffer, int len)
67 int i = 0, pos = 0;
68 mp_msg(MSGT_DECAUDIO,MSGL_V, "\nAAC_PROBE: %d bytes\n", len);
69 while(i <= len-4) {
70 if(
71 ((buffer[i] == 0xff) && ((buffer[i+1] & 0xf6) == 0xf0)) ||
72 (buffer[i] == 'A' && buffer[i+1] == 'D' && buffer[i+2] == 'I' && buffer[i+3] == 'F')
73 ) {
74 pos = i;
75 break;
77 mp_msg(MSGT_DECAUDIO,MSGL_V, "AUDIO PAYLOAD: %x %x %x %x\n", buffer[i], buffer[i+1], buffer[i+2], buffer[i+3]);
78 i++;
80 mp_msg(MSGT_DECAUDIO,MSGL_V, "\nAAC_PROBE: ret %d\n", pos);
81 return pos;
84 static int init(sh_audio_t *sh)
86 unsigned long faac_samplerate;
87 unsigned char faac_channels;
88 int faac_init, pos = 0;
89 faac_hdec = faacDecOpen();
91 // If we don't get the ES descriptor, try manual config
92 if(!sh->codecdata_len && sh->wf) {
93 sh->codecdata_len = sh->wf->cbSize;
94 sh->codecdata = malloc(sh->codecdata_len);
95 memcpy(sh->codecdata, sh->wf+1, sh->codecdata_len);
96 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FAAD: codecdata extracted from WAVEFORMATEX\n");
98 if(!sh->codecdata_len) {
99 faacDecConfigurationPtr faac_conf;
100 /* Set the default object type and samplerate */
101 /* This is useful for RAW AAC files */
102 faac_conf = faacDecGetCurrentConfiguration(faac_hdec);
103 if(sh->samplerate)
104 faac_conf->defSampleRate = sh->samplerate;
105 /* XXX: FAAD support FLOAT output, how do we handle
106 * that (FAAD_FMT_FLOAT)? ::atmos
108 if (audio_output_channels <= 2) faac_conf->downMatrix = 1;
109 switch(sh->samplesize){
110 case 1: // 8Bit
111 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: 8Bit samplesize not supported by FAAD, assuming 16Bit!\n");
112 default:
113 sh->samplesize=2;
114 case 2: // 16Bit
115 faac_conf->outputFormat = FAAD_FMT_16BIT;
116 break;
117 case 3: // 24Bit
118 faac_conf->outputFormat = FAAD_FMT_24BIT;
119 break;
120 case 4: // 32Bit
121 faac_conf->outputFormat = FAAD_FMT_32BIT;
122 break;
124 //faac_conf->defObjectType = LTP; // => MAIN, LC, SSR, LTP available.
126 faacDecSetConfiguration(faac_hdec, faac_conf);
128 sh->a_in_buffer_len = demux_read_data(sh->ds, sh->a_in_buffer, sh->a_in_buffer_size);
129 #if CONFIG_FAAD_INTERNAL
130 /* init the codec, look for LATM */
131 faac_init = faacDecInit(faac_hdec, sh->a_in_buffer,
132 sh->a_in_buffer_len, &faac_samplerate, &faac_channels,1);
133 if (faac_init < 0 && sh->a_in_buffer_len >= 3 && sh->format == mmioFOURCC('M', 'P', '4', 'L')) {
134 // working LATM not found at first try, look further on in stream
135 int i;
137 for (i = 0; i < 5; i++) {
138 pos = sh->a_in_buffer_len-3;
139 memmove(sh->a_in_buffer, &(sh->a_in_buffer[pos]), 3);
140 sh->a_in_buffer_len = 3;
141 sh->a_in_buffer_len += demux_read_data(sh->ds,&sh->a_in_buffer[sh->a_in_buffer_len],
142 sh->a_in_buffer_size - sh->a_in_buffer_len);
143 faac_init = faacDecInit(faac_hdec, sh->a_in_buffer,
144 sh->a_in_buffer_len, &faac_samplerate, &faac_channels,1);
145 if (faac_init >= 0) break;
148 #else
149 /* external faad does not have latm lookup support */
150 faac_init = faacDecInit(faac_hdec, sh->a_in_buffer,
151 sh->a_in_buffer_len, &faac_samplerate, &faac_channels);
152 #endif
154 if (faac_init < 0) {
155 pos = aac_probe(sh->a_in_buffer, sh->a_in_buffer_len);
156 if(pos) {
157 sh->a_in_buffer_len -= pos;
158 memmove(sh->a_in_buffer, &(sh->a_in_buffer[pos]), sh->a_in_buffer_len);
159 sh->a_in_buffer_len +=
160 demux_read_data(sh->ds,&(sh->a_in_buffer[sh->a_in_buffer_len]),
161 sh->a_in_buffer_size - sh->a_in_buffer_len);
162 pos = 0;
165 /* init the codec */
166 #if CONFIG_FAAD_INTERNAL
167 faac_init = faacDecInit(faac_hdec, sh->a_in_buffer,
168 sh->a_in_buffer_len, &faac_samplerate, &faac_channels,0);
169 #else
170 faac_init = faacDecInit(faac_hdec, sh->a_in_buffer,
171 sh->a_in_buffer_len, &faac_samplerate, &faac_channels);
172 #endif
175 sh->a_in_buffer_len -= (faac_init > 0)?faac_init:0; // how many bytes init consumed
176 // XXX FIXME: shouldn't we memcpy() here in a_in_buffer ?? --A'rpi
178 } else { // We have ES DS in codecdata
179 faacDecConfigurationPtr faac_conf = faacDecGetCurrentConfiguration(faac_hdec);
180 if (audio_output_channels <= 2) {
181 faac_conf->downMatrix = 1;
182 faacDecSetConfiguration(faac_hdec, faac_conf);
185 /*int i;
186 for(i = 0; i < sh_audio->codecdata_len; i++)
187 printf("codecdata_dump %d: 0x%02X\n", i, sh_audio->codecdata[i]);*/
189 faac_init = faacDecInit2(faac_hdec, sh->codecdata,
190 sh->codecdata_len, &faac_samplerate, &faac_channels);
192 if(faac_init < 0) {
193 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: Failed to initialize the decoder!\n"); // XXX: deal with cleanup!
194 faacDecClose(faac_hdec);
195 // XXX: free a_in_buffer here or in uninit?
196 return 0;
197 } else {
198 mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: Decoder init done (%dBytes)!\n", sh->a_in_buffer_len); // XXX: remove or move to debug!
199 mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: Negotiated samplerate: %ldHz channels: %d\n", faac_samplerate, faac_channels);
200 // 8 channels is aac channel order #7.
201 sh->channels = faac_channels == 7 ? 8 : faac_channels;
202 if (audio_output_channels <= 2) sh->channels = faac_channels > 1 ? 2 : 1;
203 sh->samplerate = faac_samplerate;
204 sh->samplesize=2;
205 //sh->o_bps = sh->samplesize*faac_channels*faac_samplerate;
206 if(!sh->i_bps) {
207 mp_msg(MSGT_DECAUDIO, MSGL_V, "FAAD: compressed input bitrate missing, assuming 128kbit/s!\n");
208 sh->i_bps = 128*1000/8; // XXX: HACK!!! ::atmos
209 } else
210 mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: got %dkbit/s bitrate from MP4 header!\n",sh->i_bps*8/1000);
212 return 1;
215 static void uninit(sh_audio_t *sh)
217 mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: Closing decoder!\n");
218 faacDecClose(faac_hdec);
221 static int aac_sync(sh_audio_t *sh)
223 int pos = 0;
224 // do not probe LATM, faad does that
225 if(!sh->codecdata_len && sh->format != mmioFOURCC('M', 'P', '4', 'L')) {
226 if(sh->a_in_buffer_len < sh->a_in_buffer_size){
227 sh->a_in_buffer_len +=
228 demux_read_data(sh->ds,&sh->a_in_buffer[sh->a_in_buffer_len],
229 sh->a_in_buffer_size - sh->a_in_buffer_len);
231 pos = aac_probe(sh->a_in_buffer, sh->a_in_buffer_len);
232 if(pos) {
233 sh->a_in_buffer_len -= pos;
234 memmove(sh->a_in_buffer, &(sh->a_in_buffer[pos]), sh->a_in_buffer_len);
235 mp_msg(MSGT_DECAUDIO,MSGL_V, "\nAAC SYNC AFTER %d bytes\n", pos);
238 return pos;
241 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
243 switch(cmd)
245 case ADCTRL_RESYNC_STREAM:
246 aac_sync(sh);
247 return CONTROL_TRUE;
248 #if 0
249 case ADCTRL_SKIP_FRAME:
250 return CONTROL_TRUE;
251 #endif
253 return CONTROL_UNKNOWN;
256 #define MAX_FAAD_ERRORS 10
257 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen)
259 int len = 0, last_dec_len = 1, errors = 0;
260 // int j = 0;
261 void *faac_sample_buffer;
263 while(len < minlen && last_dec_len > 0 && errors < MAX_FAAD_ERRORS) {
265 /* update buffer for raw aac streams: */
266 if(!sh->codecdata_len)
267 if(sh->a_in_buffer_len < sh->a_in_buffer_size){
268 sh->a_in_buffer_len +=
269 demux_read_data(sh->ds,&sh->a_in_buffer[sh->a_in_buffer_len],
270 sh->a_in_buffer_size - sh->a_in_buffer_len);
273 #ifdef DUMP_AAC_COMPRESSED
274 {int i;
275 for (i = 0; i < 16; i++)
276 printf ("%02X ", sh->a_in_buffer[i]);
277 printf ("\n");}
278 #endif
280 if(!sh->codecdata_len){
281 // raw aac stream:
282 do {
283 faac_sample_buffer = faacDecDecode(faac_hdec, &faac_finfo, sh->a_in_buffer, sh->a_in_buffer_len);
285 /* update buffer index after faacDecDecode */
286 if(faac_finfo.bytesconsumed >= sh->a_in_buffer_len) {
287 sh->a_in_buffer_len=0;
288 } else {
289 sh->a_in_buffer_len-=faac_finfo.bytesconsumed;
290 memmove(sh->a_in_buffer,&sh->a_in_buffer[faac_finfo.bytesconsumed],sh->a_in_buffer_len);
293 if(faac_finfo.error > 0) {
294 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: error: %s, trying to resync!\n",
295 faacDecGetErrorMessage(faac_finfo.error));
296 if (sh->a_in_buffer_len <= 0) {
297 errors = MAX_FAAD_ERRORS;
298 break;
300 sh->a_in_buffer_len--;
301 memmove(sh->a_in_buffer,&sh->a_in_buffer[1],sh->a_in_buffer_len);
302 aac_sync(sh);
303 errors++;
304 } else
305 break;
306 } while(errors < MAX_FAAD_ERRORS);
307 } else {
308 // packetized (.mp4) aac stream:
309 unsigned char* bufptr=NULL;
310 double pts;
311 int buflen=ds_get_packet_pts(sh->ds, &bufptr, &pts);
312 if(buflen<=0) break;
313 if (pts != MP_NOPTS_VALUE) {
314 sh->pts = pts;
315 sh->pts_bytes = 0;
317 faac_sample_buffer = faacDecDecode(faac_hdec, &faac_finfo, bufptr, buflen);
319 //for (j=0;j<faac_finfo.channels;j++) printf("%d:%d\n", j, faac_finfo.channel_position[j]);
321 if(faac_finfo.error > 0) {
322 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: Failed to decode frame: %s \n",
323 faacDecGetErrorMessage(faac_finfo.error));
324 } else if (faac_finfo.samples == 0) {
325 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FAAD: Decoded zero samples!\n");
326 } else {
327 /* XXX: samples already multiplied by channels! */
328 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FAAD: Successfully decoded frame (%ld Bytes)!\n",
329 sh->samplesize*faac_finfo.samples);
331 if (sh->channels >= 5)
332 reorder_channel_copy_nch(faac_sample_buffer,
333 AF_CHANNEL_LAYOUT_AAC_DEFAULT,
334 buf+len, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
335 sh->channels,
336 faac_finfo.samples, sh->samplesize);
337 else
338 memcpy(buf+len,faac_sample_buffer, sh->samplesize*faac_finfo.samples);
339 last_dec_len = sh->samplesize*faac_finfo.samples;
340 len += last_dec_len;
341 sh->pts_bytes += last_dec_len;
342 //printf("FAAD: buffer: %d bytes consumed: %d \n", k, faac_finfo.bytesconsumed);
345 return len;