3 * Copyright (c) 2003 Zdenek Kabelac.
4 * Copyright (c) 2004 Thomas Raivio.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * still a bit unfinished - but it plays something
36 * when CONFIG_FAADBIN is defined the libfaad will be opened at runtime
38 //#undef CONFIG_FAADBIN
39 //#define CONFIG_FAADBIN
43 static const char* libfaadname = "libfaad.so.0";
50 void* handle; /* dlopen handle */
51 void* faac_handle; /* FAAD library handle */
56 faacDecHandle FAADAPI (*faacDecOpen)(void);
57 faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
59 int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
60 faacDecConfigurationPtr config);
61 int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
62 unsigned char *buffer,
63 unsigned long *samplerate,
64 unsigned long *channels);
65 int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
66 unsigned long SizeOfDecoderSpecificInfo,
67 unsigned long *samplerate, unsigned long *channels);
68 int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
69 unsigned char *buffer,
70 unsigned long *bytesconsumed,
72 unsigned long *samples);
74 unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
75 faacDecConfigurationPtr config);
76 long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
77 unsigned char *buffer,
78 unsigned long buffer_size,
79 unsigned long *samplerate,
80 unsigned char *channels);
81 char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
82 unsigned long SizeOfDecoderSpecificInfo,
83 unsigned long *samplerate, unsigned char *channels);
84 void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
85 faacDecFrameInfo *hInfo,
86 unsigned char *buffer,
87 unsigned long buffer_size);
88 char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
91 void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
96 static const unsigned long faac_srates[] =
98 96000, 88200, 64000, 48000, 44100, 32000,
99 24000, 22050, 16000, 12000, 11025, 8000
102 static int faac_init_mp4(AVCodecContext *avctx)
104 FAACContext *s = (FAACContext *) avctx->priv_data;
105 unsigned long samplerate;
106 #ifndef FAAD2_VERSION
107 unsigned long channels;
109 unsigned char channels;
113 if (avctx->extradata){
114 r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
115 avctx->extradata_size,
116 &samplerate, &channels);
118 av_log(avctx, AV_LOG_ERROR,
119 "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n",
120 r, samplerate, (long)channels, avctx->extradata_size);
122 avctx->sample_rate = samplerate;
123 avctx->channels = channels;
131 static int faac_decode_frame(AVCodecContext *avctx,
132 void *data, int *data_size,
133 uint8_t *buf, int buf_size)
135 FAACContext *s = (FAACContext *) avctx->priv_data;
136 #ifndef FAAD2_VERSION
137 unsigned long bytesconsumed;
138 short *sample_buffer = NULL;
139 unsigned long samples;
142 faacDecFrameInfo frame_info;
147 #ifndef FAAD2_VERSION
148 out = s->faacDecDecode(s->faac_handle,
153 samples *= s->sample_size;
155 *data_size = samples;
156 return (buf_size < (int)bytesconsumed)
157 ? buf_size : (int)bytesconsumed;
162 unsigned char channels;
163 int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
165 av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n",
166 s->faacDecGetErrorMessage(frame_info.error));
169 avctx->sample_rate = srate;
170 avctx->channels = channels;
174 out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
176 if (frame_info.error > 0) {
177 av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
178 s->faacDecGetErrorMessage(frame_info.error));
182 frame_info.samples *= s->sample_size;
183 memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
186 *data_size = frame_info.samples;
188 return (buf_size < (int)frame_info.bytesconsumed)
189 ? buf_size : (int)frame_info.bytesconsumed;
193 static int faac_decode_end(AVCodecContext *avctx)
195 FAACContext *s = (FAACContext *) avctx->priv_data;
198 s->faacDecClose(s->faac_handle);
204 static int faac_decode_init(AVCodecContext *avctx)
206 FAACContext *s = (FAACContext *) avctx->priv_data;
207 faacDecConfigurationPtr faac_cfg;
209 #ifdef CONFIG_FAADBIN
212 s->handle = dlopen(libfaadname, RTLD_LAZY);
215 av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
216 libfaadname, dlerror());
219 #define dfaac(a, b) \
220 do { static const char* n = "faacDec" #a; \
221 if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
223 #else /* !CONFIG_FAADBIN */
224 #define dfaac(a, b) s->faacDec ## a = faacDec ## a
225 #endif /* CONFIG_FAADBIN */
227 // resolve all needed function calls
228 dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
229 dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
230 FAADAPI (*)(faacDecHandle)));
231 #ifndef FAAD2_VERSION
232 dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
233 faacDecConfigurationPtr)));
235 dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
236 unsigned long*, unsigned long*)));
237 dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
238 unsigned long, unsigned long*,
240 dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
241 dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
242 unsigned long*, short*, unsigned long*)));
244 dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
245 faacDecConfigurationPtr)));
246 dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
247 unsigned long, unsigned long*, unsigned char*)));
248 dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
249 unsigned long, unsigned long*,
251 dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
252 unsigned char*, unsigned long)));
253 dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char)));
257 #ifdef CONFIG_FAADBIN
262 av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
268 s->faac_handle = s->faacDecOpen();
269 if (!s->faac_handle) {
270 av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
271 faac_decode_end(avctx);
276 faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
279 switch (avctx->bits_per_sample) {
280 case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
284 faac_cfg->outputFormat = FAAD_FMT_16BIT;
290 faac_cfg->outputFormat = FAAD_FMT_24BIT;
296 faac_cfg->outputFormat = FAAD_FMT_32BIT;
302 faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
303 faac_cfg->defObjectType = LC;
306 s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
308 faac_init_mp4(avctx);
313 #define AAC_CODEC(id, name) \
314 AVCodec name ## _decoder = { \
318 sizeof(FAACContext), \
325 // FIXME - raw AAC files - maybe just one entry will be enough
326 AAC_CODEC(CODEC_ID_AAC, aac);
327 // If it's mp4 file - usually embeded into Qt Mov
328 AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);