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
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 */
57 faacDecHandle
FAADAPI (*faacDecOpen
)(void);
58 faacDecConfigurationPtr
FAADAPI (*faacDecGetCurrentConfiguration
)(faacDecHandle hDecoder
);
60 int FAADAPI (*faacDecSetConfiguration
)(faacDecHandle hDecoder
,
61 faacDecConfigurationPtr config
);
62 int FAADAPI (*faacDecInit
)(faacDecHandle hDecoder
,
63 unsigned char *buffer
,
64 unsigned long *samplerate
,
65 unsigned long *channels
);
66 int FAADAPI (*faacDecInit2
)(faacDecHandle hDecoder
, unsigned char *pBuffer
,
67 unsigned long SizeOfDecoderSpecificInfo
,
68 unsigned long *samplerate
, unsigned long *channels
);
69 int FAADAPI (*faacDecDecode
)(faacDecHandle hDecoder
,
70 unsigned char *buffer
,
71 unsigned long *bytesconsumed
,
73 unsigned long *samples
);
75 unsigned char FAADAPI (*faacDecSetConfiguration
)(faacDecHandle hDecoder
,
76 faacDecConfigurationPtr config
);
77 long FAADAPI (*faacDecInit
)(faacDecHandle hDecoder
,
78 unsigned char *buffer
,
79 unsigned long buffer_size
,
80 unsigned long *samplerate
,
81 unsigned char *channels
);
82 char FAADAPI (*faacDecInit2
)(faacDecHandle hDecoder
, unsigned char *pBuffer
,
83 unsigned long SizeOfDecoderSpecificInfo
,
84 unsigned long *samplerate
, unsigned char *channels
);
85 void *FAADAPI (*faacDecDecode
)(faacDecHandle hDecoder
,
86 faacDecFrameInfo
*hInfo
,
87 unsigned char *buffer
,
88 unsigned long buffer_size
);
89 char* FAADAPI (*faacDecGetErrorMessage
)(unsigned char errcode
);
92 void FAADAPI (*faacDecClose
)(faacDecHandle hDecoder
);
97 static const unsigned long faac_srates
[] =
99 96000, 88200, 64000, 48000, 44100, 32000,
100 24000, 22050, 16000, 12000, 11025, 8000
103 static int faac_init_mp4(AVCodecContext
*avctx
)
105 FAACContext
*s
= (FAACContext
*) avctx
->priv_data
;
106 unsigned long samplerate
;
107 #ifndef FAAD2_VERSION
108 unsigned long channels
;
110 unsigned char channels
;
114 if (avctx
->extradata
)
115 r
= s
->faacDecInit2(s
->faac_handle
, (uint8_t*) avctx
->extradata
,
116 avctx
->extradata_size
,
117 &samplerate
, &channels
);
118 // else r = s->faacDecInit(s->faac_handle ... );
121 av_log(avctx
, AV_LOG_ERROR
, "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n",
122 r
, samplerate
, (long)channels
, avctx
->extradata_size
);
123 avctx
->sample_rate
= samplerate
;
124 avctx
->channels
= channels
;
129 static int faac_decode_frame(AVCodecContext
*avctx
,
130 void *data
, int *data_size
,
131 uint8_t *buf
, int buf_size
)
133 FAACContext
*s
= (FAACContext
*) avctx
->priv_data
;
134 #ifndef FAAD2_VERSION
135 unsigned long bytesconsumed
;
136 short *sample_buffer
= NULL
;
137 unsigned long samples
;
140 faacDecFrameInfo frame_info
;
145 #ifndef FAAD2_VERSION
146 out
= s
->faacDecDecode(s
->faac_handle
,
151 samples
*= s
->sample_size
;
153 *data_size
= samples
;
154 return (buf_size
< (int)bytesconsumed
)
155 ? buf_size
: (int)bytesconsumed
;
158 out
= s
->faacDecDecode(s
->faac_handle
, &frame_info
, (unsigned char*)buf
, (unsigned long)buf_size
);
160 if (frame_info
.error
> 0) {
161 av_log(avctx
, AV_LOG_ERROR
, "faac: frame decoding failed: %s\n",
162 s
->faacDecGetErrorMessage(frame_info
.error
));
166 frame_info
.samples
*= s
->sample_size
;
167 memcpy(data
, out
, frame_info
.samples
); // CHECKME - can we cheat this one
170 *data_size
= frame_info
.samples
;
172 return (buf_size
< (int)frame_info
.bytesconsumed
)
173 ? buf_size
: (int)frame_info
.bytesconsumed
;
177 static int faac_decode_end(AVCodecContext
*avctx
)
179 FAACContext
*s
= (FAACContext
*) avctx
->priv_data
;
182 s
->faacDecClose(s
->faac_handle
);
188 static int faac_decode_init(AVCodecContext
*avctx
)
190 FAACContext
*s
= (FAACContext
*) avctx
->priv_data
;
191 faacDecConfigurationPtr faac_cfg
;
193 #ifdef CONFIG_FAADBIN
196 s
->handle
= dlopen(libfaadname
, RTLD_LAZY
);
199 av_log(avctx
, AV_LOG_ERROR
, "FAAD library: %s could not be opened! \n%s\n",
200 libfaadname
, dlerror());
203 #define dfaac(a, b) \
204 do { static const char* n = "faacDec" #a; \
205 if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
207 #else /* !CONFIG_FAADBIN */
208 #define dfaac(a, b) s->faacDec ## a = faacDec ## a
209 #endif /* CONFIG_FAADBIN */
211 // resolve all needed function calls
212 dfaac(Open
, (faacDecHandle
FAADAPI (*)(void)));
213 dfaac(GetCurrentConfiguration
, (faacDecConfigurationPtr
214 FAADAPI (*)(faacDecHandle
)));
215 #ifndef FAAD2_VERSION
216 dfaac(SetConfiguration
, (int FAADAPI (*)(faacDecHandle
,
217 faacDecConfigurationPtr
)));
219 dfaac(Init
, (int FAADAPI (*)(faacDecHandle
, unsigned char*,
220 unsigned long*, unsigned long*)));
221 dfaac(Init2
, (int FAADAPI (*)(faacDecHandle
, unsigned char*,
222 unsigned long, unsigned long*,
224 dfaac(Close
, (void FAADAPI (*)(faacDecHandle hDecoder
)));
225 dfaac(Decode
, (int FAADAPI (*)(faacDecHandle
, unsigned char*,
226 unsigned long*, short*, unsigned long*)));
228 dfaac(SetConfiguration
, (unsigned char FAADAPI (*)(faacDecHandle
,
229 faacDecConfigurationPtr
)));
230 dfaac(Init
, (long FAADAPI (*)(faacDecHandle
, unsigned char*,
231 unsigned long, unsigned long*, unsigned char*)));
232 dfaac(Init2
, (char FAADAPI (*)(faacDecHandle
, unsigned char*,
233 unsigned long, unsigned long*,
235 dfaac(Decode
, (void *FAADAPI (*)(faacDecHandle
, faacDecFrameInfo
*,
236 unsigned char*, unsigned long)));
237 dfaac(GetErrorMessage
, (char* FAADAPI (*)(unsigned char)));
241 #ifdef CONFIG_FAADBIN
246 av_log(avctx
, AV_LOG_ERROR
, "FAAD library: cannot resolve %s in %s!\n",
252 s
->faac_handle
= s
->faacDecOpen();
253 if (!s
->faac_handle
) {
254 av_log(avctx
, AV_LOG_ERROR
, "FAAD library: cannot create handler!\n");
255 faac_decode_end(avctx
);
260 faac_cfg
= s
->faacDecGetCurrentConfiguration(s
->faac_handle
);
263 switch (avctx
->bits_per_sample
) {
264 case 8: av_log(avctx
, AV_LOG_ERROR
, "FAADlib unsupported bps %d\n", avctx
->bits_per_sample
); break;
268 faac_cfg
->outputFormat
= FAAD_FMT_16BIT
;
274 faac_cfg
->outputFormat
= FAAD_FMT_24BIT
;
280 faac_cfg
->outputFormat
= FAAD_FMT_32BIT
;
286 faac_cfg
->defSampleRate
= (!avctx
->sample_rate
) ? 44100 : avctx
->sample_rate
;
287 faac_cfg
->defObjectType
= LC
;
290 s
->faacDecSetConfiguration(s
->faac_handle
, faac_cfg
);
292 faac_init_mp4(avctx
);
297 #define AAC_CODEC(id, name) \
298 AVCodec name ## _decoder = { \
302 sizeof(FAACContext), \
309 // FIXME - raw AAC files - maybe just one entry will be enough
310 AAC_CODEC(CODEC_ID_AAC
, aac
);
311 // If it's mp4 file - usually embeded into Qt Mov
312 AAC_CODEC(CODEC_ID_MPEG4AAC
, mpeg4aac
);