Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / ad_faad.c
blob039cdc15e1570f499c11e229d0abb0a5abeec7d5
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.
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
11 #include "config.h"
12 #include "ad_internal.h"
13 #include "libaf/reorder_ch.h"
15 static ad_info_t info =
17 "AAC (MPEG2/4 Advanced Audio Coding)",
18 "faad",
19 "Felix Buenemann",
20 "faad2",
21 "uses libfaad2"
24 LIBAD_EXTERN(faad)
26 #ifndef USE_FAAD_INTERNAL
27 #include <faad.h>
28 #else
29 #include "libfaad2/faad.h"
30 #endif
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;
46 return 1;
49 static int aac_probe(unsigned char *buffer, int len)
51 int i = 0, pos = 0;
52 mp_msg(MSGT_DECAUDIO,MSGL_V, "\nAAC_PROBE: %d bytes\n", len);
53 while(i <= len-4) {
54 if(
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')
57 ) {
58 pos = i;
59 break;
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]);
62 i++;
64 mp_msg(MSGT_DECAUDIO,MSGL_V, "\nAAC_PROBE: ret %d\n", pos);
65 return 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) {
82 #if 1
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);
87 if(sh->samplerate)
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){
94 case 1: // 8Bit
95 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"FAAD: 8Bit samplesize not supported by FAAD, assuming 16Bit!\n");
96 default:
97 sh->samplesize=2;
98 case 2: // 16Bit
99 faac_conf->outputFormat = FAAD_FMT_16BIT;
100 break;
101 case 3: // 24Bit
102 faac_conf->outputFormat = FAAD_FMT_24BIT;
103 break;
104 case 4: // 32Bit
105 faac_conf->outputFormat = FAAD_FMT_32BIT;
106 break;
108 //faac_conf->defObjectType = LTP; // => MAIN, LC, SSR, LTP available.
110 faacDecSetConfiguration(faac_hdec, faac_conf);
111 #endif
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);
115 if(pos) {
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);
121 pos = 0;
124 /* init the codec */
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);
138 /*int i;
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);
145 if(faac_init < 0) {
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?
149 return 0;
150 } else {
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;
156 sh->samplesize=2;
157 //sh->o_bps = sh->samplesize*faac_channels*faac_samplerate;
158 if(!sh->i_bps) {
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
161 } else
162 mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: got %dkbit/s bitrate from MP4 header!\n",sh->i_bps*8/1000);
164 return 1;
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)
175 int pos = 0;
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);
183 if(pos) {
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);
189 return pos;
192 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
194 switch(cmd)
196 case ADCTRL_RESYNC_STREAM:
197 aac_sync(sh);
198 return CONTROL_TRUE;
199 #if 0
200 case ADCTRL_SKIP_FRAME:
201 return CONTROL_TRUE;
202 #endif
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;
211 // int j = 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
225 {int i;
226 for (i = 0; i < 16; i++)
227 printf ("%02X ", sh->a_in_buffer[i]);
228 printf ("\n");}
229 #endif
231 if(!sh->codecdata_len){
232 // raw aac stream:
233 do {
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;
239 } else {
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;
249 break;
251 sh->a_in_buffer_len--;
252 memmove(sh->a_in_buffer,&sh->a_in_buffer[1],sh->a_in_buffer_len);
253 aac_sync(sh);
254 errors++;
255 } else
256 break;
257 } while(errors < MAX_FAAD_ERRORS);
258 } else {
259 // packetized (.mp4) aac stream:
260 unsigned char* bufptr=NULL;
261 double pts;
262 int buflen=ds_get_packet_pts(sh->ds, &bufptr, &pts);
263 if(buflen<=0) break;
264 if (pts != MP_NOPTS_VALUE) {
265 sh->pts = pts;
266 sh->pts_bytes = 0;
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");
277 } else {
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,
286 sh->channels,
287 faac_finfo.samples, sh->samplesize);
288 else
289 memcpy(buf+len,faac_sample_buffer, sh->samplesize*faac_finfo.samples);
290 last_dec_len = sh->samplesize*faac_finfo.samples;
291 len += last_dec_len;
292 sh->pts_bytes += last_dec_len;
293 //printf("FAAD: buffer: %d bytes consumed: %d \n", k, faac_finfo.bytesconsumed);
296 return len;