Mercurial > libavcodec.hg
annotate faad.c @ 3420:54814e15aa3d libavcodec
Mark some read-only datastructures as const.
patch by Stefan Huehner, stefan & at & huehner & dot & org
author | diego |
---|---|
date | Thu, 06 Jul 2006 13:53:07 +0000 |
parents | d771383fbcb8 |
children | c8c591fe26f8 |
rev | line source |
---|---|
1245 | 1 /* |
2 * Faad decoder | |
3 * Copyright (c) 2003 Zdenek Kabelac. | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
4 * Copyright (c) 2004 Thomas Raivio. |
1245 | 5 * |
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. | |
10 * | |
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. | |
15 * | |
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 | |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1245 | 19 */ |
20 | |
21 /** | |
22 * @file faad.c | |
23 * AAC decoder. | |
24 * | |
25 * still a bit unfinished - but it plays something | |
26 */ | |
27 | |
28 #include "avcodec.h" | |
29 #include "faad.h" | |
30 | |
2061
c28b03fea50f
Building with faad2-cvs fix by ("Steven M. Schultz" <sms at 2bsd dot com>)
michael
parents:
2059
diff
changeset
|
31 #ifndef FAADAPI |
c28b03fea50f
Building with faad2-cvs fix by ("Steven M. Schultz" <sms at 2bsd dot com>)
michael
parents:
2059
diff
changeset
|
32 #define FAADAPI |
c28b03fea50f
Building with faad2-cvs fix by ("Steven M. Schultz" <sms at 2bsd dot com>)
michael
parents:
2059
diff
changeset
|
33 #endif |
c28b03fea50f
Building with faad2-cvs fix by ("Steven M. Schultz" <sms at 2bsd dot com>)
michael
parents:
2059
diff
changeset
|
34 |
1245 | 35 /* |
36 * when CONFIG_FAADBIN is defined the libfaad will be opened at runtime | |
37 */ | |
38 //#undef CONFIG_FAADBIN | |
39 //#define CONFIG_FAADBIN | |
40 | |
41 #ifdef CONFIG_FAADBIN | |
42 #include <dlfcn.h> | |
43 static const char* libfaadname = "libfaad.so.0"; | |
44 #else | |
45 #define dlopen(a) | |
46 #define dlclose(a) | |
47 #endif | |
48 | |
49 typedef struct { | |
2979 | 50 void* handle; /* dlopen handle */ |
51 void* faac_handle; /* FAAD library handle */ | |
1245 | 52 int sample_size; |
3097 | 53 int init; |
1245 | 54 |
55 /* faad calls */ | |
56 faacDecHandle FAADAPI (*faacDecOpen)(void); | |
57 faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder); | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
58 #ifndef FAAD2_VERSION |
2979 | 59 int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder, |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
60 faacDecConfigurationPtr config); |
2979 | 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, | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
66 unsigned long SizeOfDecoderSpecificInfo, |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
67 unsigned long *samplerate, unsigned long *channels); |
2979 | 68 int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder, |
69 unsigned char *buffer, | |
70 unsigned long *bytesconsumed, | |
71 short *sample_buffer, | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
72 unsigned long *samples); |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
73 #else |
2979 | 74 unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder, |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
75 faacDecConfigurationPtr config); |
2979 | 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, | |
1245 | 82 unsigned long SizeOfDecoderSpecificInfo, |
83 unsigned long *samplerate, unsigned char *channels); | |
2979 | 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); | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
89 #endif |
2967 | 90 |
1245 | 91 void FAADAPI (*faacDecClose)(faacDecHandle hDecoder); |
2967 | 92 |
93 | |
1245 | 94 } FAACContext; |
95 | |
96 static const unsigned long faac_srates[] = | |
97 { | |
98 96000, 88200, 64000, 48000, 44100, 32000, | |
99 24000, 22050, 16000, 12000, 11025, 8000 | |
100 }; | |
101 | |
102 static int faac_init_mp4(AVCodecContext *avctx) | |
103 { | |
104 FAACContext *s = (FAACContext *) avctx->priv_data; | |
105 unsigned long samplerate; | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
106 #ifndef FAAD2_VERSION |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
107 unsigned long channels; |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
108 #else |
1245 | 109 unsigned char channels; |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
110 #endif |
1245 | 111 int r = 0; |
112 | |
3097 | 113 if (avctx->extradata){ |
2979 | 114 r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata, |
115 avctx->extradata_size, | |
116 &samplerate, &channels); | |
3097 | 117 if (r < 0){ |
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); | |
121 } else { | |
122 avctx->sample_rate = samplerate; | |
123 avctx->channels = channels; | |
124 s->init = 1; | |
125 } | |
126 } | |
2059
ad972ab280bc
sample_rate and channels in aac audio patch by (Luca Abeni <lucabe72 at email dot it>)
michael
parents:
1929
diff
changeset
|
127 |
1245 | 128 return r; |
129 } | |
130 | |
131 static int faac_decode_frame(AVCodecContext *avctx, | |
132 void *data, int *data_size, | |
133 uint8_t *buf, int buf_size) | |
134 { | |
135 FAACContext *s = (FAACContext *) avctx->priv_data; | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
136 #ifndef FAAD2_VERSION |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
137 unsigned long bytesconsumed; |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
138 short *sample_buffer = NULL; |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
139 unsigned long samples; |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
140 int out; |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
141 #else |
1245 | 142 faacDecFrameInfo frame_info; |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
143 void *out; |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
144 #endif |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
145 if(buf_size == 0) |
2979 | 146 return 0; |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
147 #ifndef FAAD2_VERSION |
2967 | 148 out = s->faacDecDecode(s->faac_handle, |
149 (unsigned char*)buf, | |
150 &bytesconsumed, | |
151 data, | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
152 &samples); |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
153 samples *= s->sample_size; |
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
154 if (data_size) |
2979 | 155 *data_size = samples; |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
156 return (buf_size < (int)bytesconsumed) |
2979 | 157 ? buf_size : (int)bytesconsumed; |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
158 #else |
2967 | 159 |
3097 | 160 if(!s->init){ |
161 unsigned long srate; | |
162 unsigned char channels; | |
3155 | 163 int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels); |
3097 | 164 if(r < 0){ |
165 av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n", | |
166 s->faacDecGetErrorMessage(frame_info.error)); | |
3164
d771383fbcb8
Make faac_decode_frame return -1 on error and not 0 which avoids
mru
parents:
3155
diff
changeset
|
167 return -1; |
3097 | 168 } |
169 avctx->sample_rate = srate; | |
170 avctx->channels = channels; | |
171 s->init = 1; | |
172 } | |
173 | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
174 out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size); |
1245 | 175 |
176 if (frame_info.error > 0) { | |
2979 | 177 av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n", |
178 s->faacDecGetErrorMessage(frame_info.error)); | |
3164
d771383fbcb8
Make faac_decode_frame return -1 on error and not 0 which avoids
mru
parents:
3155
diff
changeset
|
179 return -1; |
1245 | 180 } |
181 | |
182 frame_info.samples *= s->sample_size; | |
183 memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one | |
184 | |
185 if (data_size) | |
2979 | 186 *data_size = frame_info.samples; |
1245 | 187 |
188 return (buf_size < (int)frame_info.bytesconsumed) | |
2979 | 189 ? buf_size : (int)frame_info.bytesconsumed; |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
190 #endif |
1245 | 191 } |
192 | |
193 static int faac_decode_end(AVCodecContext *avctx) | |
194 { | |
195 FAACContext *s = (FAACContext *) avctx->priv_data; | |
196 | |
197 if (s->faacDecClose) | |
198 s->faacDecClose(s->faac_handle); | |
199 | |
200 dlclose(s->handle); | |
201 return 0; | |
202 } | |
203 | |
204 static int faac_decode_init(AVCodecContext *avctx) | |
205 { | |
206 FAACContext *s = (FAACContext *) avctx->priv_data; | |
207 faacDecConfigurationPtr faac_cfg; | |
208 | |
209 #ifdef CONFIG_FAADBIN | |
210 const char* err = 0; | |
211 | |
212 s->handle = dlopen(libfaadname, RTLD_LAZY); | |
213 if (!s->handle) | |
214 { | |
2979 | 215 av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n", |
216 libfaadname, dlerror()); | |
1245 | 217 return -1; |
218 } | |
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) | |
222 for(;;) { | |
223 #else /* !CONFIG_FAADBIN */ | |
224 #define dfaac(a, b) s->faacDec ## a = faacDec ## a | |
225 #endif /* CONFIG_FAADBIN */ | |
226 | |
227 // resolve all needed function calls | |
2979 | 228 dfaac(Open, (faacDecHandle FAADAPI (*)(void))); |
229 dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr | |
230 FAADAPI (*)(faacDecHandle))); | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
231 #ifndef FAAD2_VERSION |
2979 | 232 dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle, |
233 faacDecConfigurationPtr))); | |
1245 | 234 |
2979 | 235 dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*, |
236 unsigned long*, unsigned long*))); | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
237 dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*, |
2979 | 238 unsigned long, unsigned long*, |
239 unsigned long*))); | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
240 dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder))); |
2979 | 241 dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*, |
242 unsigned long*, short*, unsigned long*))); | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
243 #else |
2979 | 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*, | |
250 unsigned char*))); | |
251 dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*, | |
252 unsigned char*, unsigned long))); | |
253 dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char))); | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
254 #endif |
1245 | 255 #undef dfacc |
256 | |
257 #ifdef CONFIG_FAADBIN | |
258 break; | |
259 } | |
260 if (err) { | |
261 dlclose(s->handle); | |
2979 | 262 av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n", |
263 err, libfaadname); | |
1245 | 264 return -1; |
265 } | |
266 #endif | |
267 | |
268 s->faac_handle = s->faacDecOpen(); | |
269 if (!s->faac_handle) { | |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1245
diff
changeset
|
270 av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n"); |
1245 | 271 faac_decode_end(avctx); |
272 return -1; | |
273 } | |
274 | |
275 | |
276 faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle); | |
277 | |
278 if (faac_cfg) { | |
2979 | 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; | |
281 default: | |
282 case 16: | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
283 #ifdef FAAD2_VERSION |
2979 | 284 faac_cfg->outputFormat = FAAD_FMT_16BIT; |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
285 #endif |
2979 | 286 s->sample_size = 2; |
287 break; | |
288 case 24: | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
289 #ifdef FAAD2_VERSION |
2979 | 290 faac_cfg->outputFormat = FAAD_FMT_24BIT; |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
291 #endif |
2979 | 292 s->sample_size = 3; |
293 break; | |
294 case 32: | |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
295 #ifdef FAAD2_VERSION |
2979 | 296 faac_cfg->outputFormat = FAAD_FMT_32BIT; |
1929
d4525c3065d0
aac decoding patch by ("Thomas Raivio" <tjraivio at cc dot hut dot fi>)
michael
parents:
1602
diff
changeset
|
297 #endif |
2979 | 298 s->sample_size = 4; |
299 break; | |
300 } | |
1245 | 301 |
2979 | 302 faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate; |
303 faac_cfg->defObjectType = LC; | |
1245 | 304 } |
305 | |
306 s->faacDecSetConfiguration(s->faac_handle, faac_cfg); | |
307 | |
308 faac_init_mp4(avctx); | |
309 | |
310 return 0; | |
311 } | |
312 | |
313 #define AAC_CODEC(id, name) \ | |
314 AVCodec name ## _decoder = { \ | |
315 #name, \ | |
316 CODEC_TYPE_AUDIO, \ | |
317 id, \ | |
318 sizeof(FAACContext), \ | |
319 faac_decode_init, \ | |
320 NULL, \ | |
321 faac_decode_end, \ | |
322 faac_decode_frame, \ | |
323 } | |
324 | |
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); | |
329 | |
330 #undef AAC_CODEC |