3 * Copyright (c) 2003 Zdenek Kabelac.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * still a bit unfinished - but it plays something
31 * when CONFIG_FAADBIN is defined the libfaad will be opened at runtime
33 //#undef CONFIG_FAADBIN
34 //#define CONFIG_FAADBIN
38 static const char* libfaadname = "libfaad.so.0";
45 void* handle; /* dlopen handle */
46 void* faac_handle; /* FAAD library handle */
52 faacDecHandle FAADAPI (*faacDecOpen)(void);
53 faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
54 unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
55 faacDecConfigurationPtr config);
56 long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
57 unsigned char *buffer,
58 unsigned long *samplerate,
59 unsigned char *channels);
60 char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
61 unsigned long SizeOfDecoderSpecificInfo,
62 unsigned long *samplerate, unsigned char *channels);
63 void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
64 void* FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
65 faacDecFrameInfo *hInfo,
66 unsigned char *buffer);
67 unsigned char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
70 static const unsigned long faac_srates[] =
72 96000, 88200, 64000, 48000, 44100, 32000,
73 24000, 22050, 16000, 12000, 11025, 8000
76 static int faac_init_mp4(AVCodecContext *avctx)
78 FAACContext *s = (FAACContext *) avctx->priv_data;
79 unsigned long samplerate;
80 unsigned char channels;
84 r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
85 avctx->extradata_size,
86 &samplerate, &channels);
87 // else r = s->faacDecInit(s->faac_handle ... );
90 fprintf(stderr, "faacDecInit2 failed r:%d sr:%ld ch:%d s:%d\n",
91 r, samplerate, channels, avctx->extradata_size);
96 static int faac_init_aac(AVCodecContext *avctx)
98 FAACContext *s = (FAACContext *) avctx->priv_data;
102 static int faac_decode_frame(AVCodecContext *avctx,
103 void *data, int *data_size,
104 uint8_t *buf, int buf_size)
106 FAACContext *s = (FAACContext *) avctx->priv_data;
107 faacDecFrameInfo frame_info;
108 void* out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf);
109 //printf("DECODE FRAME %d, %d, %d - %p\n", buf_size, frame_info.samples, frame_info.bytesconsumed, out);
111 if (frame_info.error > 0) {
112 fprintf(stderr, "faac: frame decodinf failed: %s\n",
113 s->faacDecGetErrorMessage(frame_info.error));
117 frame_info.samples *= s->sample_size;
118 memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
121 *data_size = frame_info.samples;
123 return (buf_size < (int)frame_info.bytesconsumed)
124 ? buf_size : (int)frame_info.bytesconsumed;
127 static int faac_decode_end(AVCodecContext *avctx)
129 FAACContext *s = (FAACContext *) avctx->priv_data;
132 s->faacDecClose(s->faac_handle);
138 static int faac_decode_init(AVCodecContext *avctx)
140 FAACContext *s = (FAACContext *) avctx->priv_data;
141 faacDecConfigurationPtr faac_cfg;
143 #ifdef CONFIG_FAADBIN
146 s->handle = dlopen(libfaadname, RTLD_LAZY);
149 fprintf(stderr, "FAAD library: %s could not be opened! \n%s\n",
150 libfaadname, dlerror());
153 #define dfaac(a, b) \
154 do { static const char* n = "faacDec" #a; \
155 if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
157 #else /* !CONFIG_FAADBIN */
158 #define dfaac(a, b) s->faacDec ## a = faacDec ## a
159 #endif /* CONFIG_FAADBIN */
161 // resolve all needed function calls
162 dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
163 dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
164 FAADAPI (*)(faacDecHandle)));
165 dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
166 faacDecConfigurationPtr)));
168 dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
169 unsigned long*, unsigned char*)));
170 dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
171 unsigned long, unsigned long*,
173 dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
174 dfaac(Decode, (void* FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
177 dfaac(GetErrorMessage, (unsigned char* FAADAPI (*)(unsigned char)));
180 #ifdef CONFIG_FAADBIN
185 fprintf(stderr, "FAAD library: cannot resolve %s in %s!\n",
191 s->faac_handle = s->faacDecOpen();
192 if (!s->faac_handle) {
193 fprintf(stderr, "FAAD library: cannot create handler!\n");
194 faac_decode_end(avctx);
199 faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
202 switch (avctx->bits_per_sample) {
203 case 8: fprintf(stderr, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
206 faac_cfg->outputFormat = FAAD_FMT_16BIT;
210 faac_cfg->outputFormat = FAAD_FMT_24BIT;
214 faac_cfg->outputFormat = FAAD_FMT_32BIT;
219 faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
220 faac_cfg->defObjectType = LC;
223 s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
225 faac_init_mp4(avctx);
230 #define AAC_CODEC(id, name) \
231 AVCodec name ## _decoder = { \
235 sizeof(FAACContext), \
242 // FIXME - raw AAC files - maybe just one entry will be enough
243 AAC_CODEC(CODEC_ID_AAC, aac);
244 // If it's mp4 file - usually embeded into Qt Mov
245 AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);