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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
* still a bit unfinished - but it plays something
32
* when CONFIG_FAADBIN is defined the libfaad will be opened at runtime
34
//#undef CONFIG_FAADBIN
35
//#define CONFIG_FAADBIN
39
static const char* libfaadname = "libfaad.so.0";
46
void* handle; /* dlopen handle */
47
void* faac_handle; /* FAAD library handle */
53
faacDecHandle FAADAPI (*faacDecOpen)(void);
54
faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
56
int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
57
faacDecConfigurationPtr config);
58
int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
59
unsigned char *buffer,
60
unsigned long *samplerate,
61
unsigned long *channels);
62
int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
63
unsigned long SizeOfDecoderSpecificInfo,
64
unsigned long *samplerate, unsigned long *channels);
65
int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
66
unsigned char *buffer,
67
unsigned long *bytesconsumed,
69
unsigned long *samples);
71
unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
72
faacDecConfigurationPtr config);
73
long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
74
unsigned char *buffer,
75
unsigned long buffer_size,
76
unsigned long *samplerate,
77
unsigned char *channels);
78
char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
79
unsigned long SizeOfDecoderSpecificInfo,
80
unsigned long *samplerate, unsigned char *channels);
81
void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
82
faacDecFrameInfo *hInfo,
83
unsigned char *buffer,
84
unsigned long buffer_size);
85
unsigned char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
88
void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
93
static const unsigned long faac_srates[] =
95
96000, 88200, 64000, 48000, 44100, 32000,
96
24000, 22050, 16000, 12000, 11025, 8000
99
static int faac_init_mp4(AVCodecContext *avctx)
101
FAACContext *s = (FAACContext *) avctx->priv_data;
102
unsigned long samplerate;
103
#ifndef FAAD2_VERSION
104
unsigned long channels;
106
unsigned char channels;
110
if (avctx->extradata)
111
r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
112
avctx->extradata_size,
113
&samplerate, &channels);
114
// else r = s->faacDecInit(s->faac_handle ... );
117
av_log(avctx, AV_LOG_ERROR, "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n",
118
r, samplerate, (long)channels, avctx->extradata_size);
122
static int faac_init_aac(AVCodecContext *avctx)
127
static int faac_decode_frame(AVCodecContext *avctx,
128
void *data, int *data_size,
129
uint8_t *buf, int buf_size)
131
FAACContext *s = (FAACContext *) avctx->priv_data;
132
#ifndef FAAD2_VERSION
133
unsigned long bytesconsumed;
134
short *sample_buffer = NULL;
135
unsigned long samples;
138
faacDecFrameInfo frame_info;
143
#ifndef FAAD2_VERSION
144
out = s->faacDecDecode(s->faac_handle,
149
samples *= s->sample_size;
151
*data_size = samples;
152
return (buf_size < (int)bytesconsumed)
153
? buf_size : (int)bytesconsumed;
156
out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
158
if (frame_info.error > 0) {
159
av_log(avctx, AV_LOG_ERROR, "faac: frame decodinf failed: %s\n",
160
s->faacDecGetErrorMessage(frame_info.error));
164
frame_info.samples *= s->sample_size;
165
memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
168
*data_size = frame_info.samples;
170
return (buf_size < (int)frame_info.bytesconsumed)
171
? buf_size : (int)frame_info.bytesconsumed;
175
static int faac_decode_end(AVCodecContext *avctx)
177
FAACContext *s = (FAACContext *) avctx->priv_data;
180
s->faacDecClose(s->faac_handle);
186
static int faac_decode_init(AVCodecContext *avctx)
188
FAACContext *s = (FAACContext *) avctx->priv_data;
189
faacDecConfigurationPtr faac_cfg;
191
#ifdef CONFIG_FAADBIN
194
s->handle = dlopen(libfaadname, RTLD_LAZY);
197
av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
198
libfaadname, dlerror());
201
#define dfaac(a, b) \
202
do { static const char* n = "faacDec" #a; \
203
if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
205
#else /* !CONFIG_FAADBIN */
206
#define dfaac(a, b) s->faacDec ## a = faacDec ## a
207
#endif /* CONFIG_FAADBIN */
209
// resolve all needed function calls
210
dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
211
dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
212
FAADAPI (*)(faacDecHandle)));
213
#ifndef FAAD2_VERSION
214
dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
215
faacDecConfigurationPtr)));
217
dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
218
unsigned long*, unsigned long*)));
219
dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
220
unsigned long, unsigned long*,
222
dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
223
dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
224
unsigned long*, short*, unsigned long*)));
226
dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
227
faacDecConfigurationPtr)));
228
dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
229
unsigned long, unsigned long*, unsigned char*)));
230
dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
231
unsigned long, unsigned long*,
233
dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
234
unsigned char*, unsigned long)));
235
dfaac(GetErrorMessage, (unsigned char* FAADAPI (*)(unsigned char)));
239
#ifdef CONFIG_FAADBIN
244
av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
250
s->faac_handle = s->faacDecOpen();
251
if (!s->faac_handle) {
252
av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
253
faac_decode_end(avctx);
258
faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
261
switch (avctx->bits_per_sample) {
262
case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
266
faac_cfg->outputFormat = FAAD_FMT_16BIT;
272
faac_cfg->outputFormat = FAAD_FMT_24BIT;
278
faac_cfg->outputFormat = FAAD_FMT_32BIT;
284
faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
285
faac_cfg->defObjectType = LC;
288
s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
290
faac_init_mp4(avctx);
295
#define AAC_CODEC(id, name) \
296
AVCodec name ## _decoder = { \
300
sizeof(FAACContext), \
307
// FIXME - raw AAC files - maybe just one entry will be enough
308
AAC_CODEC(CODEC_ID_AAC, aac);
309
// If it's mp4 file - usually embeded into Qt Mov
310
AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);