Mercurial > libavcodec.hg
annotate a52dec.c @ 4560:a798e8cc5e67 libavcodec
gcc 2.95 fix
author | michael |
---|---|
date | Thu, 22 Feb 2007 00:04:36 +0000 |
parents | 790d1cb93686 |
children | a0b9e07527c4 |
rev | line source |
---|---|
332 | 1 /* |
2 * A52 decoder | |
429 | 3 * Copyright (c) 2001 Fabrice Bellard. |
332 | 4 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3059
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3059
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3059
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
429 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3059
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
332 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3059
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
332 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Lesser General Public License for more details. | |
332 | 16 * |
429 | 17 * You should have received a copy of the GNU Lesser General Public |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3059
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
332 | 20 */ |
1106 | 21 |
22 /** | |
23 * @file a52dec.c | |
24 * A52 decoder. | |
25 */ | |
26 | |
332 | 27 #include "avcodec.h" |
4511
91872ae737b2
Remove internal liba52; external lib still works, native decoder coming up.
diego
parents:
4356
diff
changeset
|
28 #include <a52dec/a52.h> |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
29 |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
30 #ifdef CONFIG_LIBA52BIN |
332 | 31 #include <dlfcn.h> |
32 static const char* liba52name = "liba52.so.0"; | |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
33 #endif |
332 | 34 |
35 /** | |
36 * liba52 - Copyright (C) Aaron Holtzman | |
37 * released under the GPL license. | |
38 */ | |
39 typedef struct AC3DecodeState { | |
1064 | 40 uint8_t inbuf[4096]; /* input buffer */ |
41 uint8_t *inbuf_ptr; | |
332 | 42 int frame_size; |
43 int flags; | |
44 int channels; | |
45 a52_state_t* state; | |
46 sample_t* samples; | |
47 | |
48 /* | |
49 * virtual method table | |
50 * | |
51 * using this function table so the liba52 doesn't | |
52 * have to be really linked together with ffmpeg | |
53 * and might be linked in runtime - this allows binary | |
54 * distribution of ffmpeg library which doens't depend | |
55 * on liba52 library - but if user has it installed | |
56 * it will be used - user might install such library | |
57 * separately | |
58 */ | |
59 void* handle; | |
60 a52_state_t* (*a52_init)(uint32_t mm_accel); | |
61 sample_t* (*a52_samples)(a52_state_t * state); | |
62 int (*a52_syncinfo)(uint8_t * buf, int * flags, | |
2979 | 63 int * sample_rate, int * bit_rate); |
332 | 64 int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags, |
2979 | 65 sample_t * level, sample_t bias); |
332 | 66 void (*a52_dynrng)(a52_state_t * state, |
2979 | 67 sample_t (* call) (sample_t, void *), void * data); |
332 | 68 int (*a52_block)(a52_state_t * state); |
69 void (*a52_free)(a52_state_t * state); | |
70 | |
71 } AC3DecodeState; | |
72 | |
73 static void* dlsymm(void* handle, const char* symbol) | |
74 { | |
75 void* f = dlsym(handle, symbol); | |
76 if (!f) | |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2028
diff
changeset
|
77 av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol); |
332 | 78 return f; |
79 } | |
80 | |
81 static int a52_decode_init(AVCodecContext *avctx) | |
82 { | |
83 AC3DecodeState *s = avctx->priv_data; | |
84 | |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
85 #ifdef CONFIG_LIBA52BIN |
332 | 86 s->handle = dlopen(liba52name, RTLD_LAZY); |
87 if (!s->handle) | |
88 { | |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2028
diff
changeset
|
89 av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror()); |
332 | 90 return -1; |
91 } | |
92 s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init"); | |
93 s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples"); | |
94 s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo"); | |
95 s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame"); | |
96 s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block"); | |
97 s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free"); | |
98 if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo | |
99 || !s->a52_frame || !s->a52_block || !s->a52_free) | |
100 { | |
2979 | 101 dlclose(s->handle); |
332 | 102 return -1; |
103 } | |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
104 #else |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
105 s->handle = 0; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
106 s->a52_init = a52_init; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
107 s->a52_samples = a52_samples; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
108 s->a52_syncinfo = a52_syncinfo; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
109 s->a52_frame = a52_frame; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
110 s->a52_block = a52_block; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
111 s->a52_free = a52_free; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
112 #endif |
332 | 113 s->state = s->a52_init(0); /* later use CPU flags */ |
114 s->samples = s->a52_samples(s->state); | |
115 s->inbuf_ptr = s->inbuf; | |
116 s->frame_size = 0; | |
117 | |
118 return 0; | |
119 } | |
120 | |
121 /**** the following two functions comes from a52dec */ | |
122 static inline int blah (int32_t i) | |
123 { | |
124 if (i > 0x43c07fff) | |
2979 | 125 return 32767; |
332 | 126 else if (i < 0x43bf8000) |
2979 | 127 return -32768; |
332 | 128 return i - 0x43c00000; |
129 } | |
130 | |
1064 | 131 static inline void float_to_int (float * _f, int16_t * s16, int nchannels) |
332 | 132 { |
133 int i, j, c; | |
2979 | 134 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format |
332 | 135 |
136 j = 0; | |
137 nchannels *= 256; | |
138 for (i = 0; i < 256; i++) { | |
2979 | 139 for (c = 0; c < nchannels; c += 256) |
140 s16[j++] = blah (f[i + c]); | |
332 | 141 } |
142 } | |
143 | |
144 /**** end */ | |
145 | |
146 #define HEADER_SIZE 7 | |
147 | |
148 static int a52_decode_frame(AVCodecContext *avctx, | |
149 void *data, int *data_size, | |
1064 | 150 uint8_t *buf, int buf_size) |
332 | 151 { |
152 AC3DecodeState *s = avctx->priv_data; | |
1064 | 153 uint8_t *buf_ptr; |
332 | 154 int flags, i, len; |
155 int sample_rate, bit_rate; | |
156 short *out_samples = data; | |
157 float level; | |
158 static const int ac3_channels[8] = { | |
2979 | 159 2, 1, 2, 3, 3, 4, 4, 5 |
332 | 160 }; |
161 | |
4356
c8bc01fee409
set data_size to 0 so that in case we return without setting it nothing funny can happen
michael
parents:
4343
diff
changeset
|
162 *data_size= 0; |
c8bc01fee409
set data_size to 0 so that in case we return without setting it nothing funny can happen
michael
parents:
4343
diff
changeset
|
163 |
332 | 164 buf_ptr = buf; |
165 while (buf_size > 0) { | |
166 len = s->inbuf_ptr - s->inbuf; | |
167 if (s->frame_size == 0) { | |
168 /* no header seen : find one. We need at least 7 bytes to parse it */ | |
169 len = HEADER_SIZE - len; | |
170 if (len > buf_size) | |
171 len = buf_size; | |
172 memcpy(s->inbuf_ptr, buf_ptr, len); | |
173 buf_ptr += len; | |
174 s->inbuf_ptr += len; | |
175 buf_size -= len; | |
176 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) { | |
177 len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
178 if (len == 0) { | |
179 /* no sync found : move by one byte (inefficient, but simple!) */ | |
180 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1); | |
181 s->inbuf_ptr--; | |
182 } else { | |
2979 | 183 s->frame_size = len; |
332 | 184 /* update codec info */ |
185 avctx->sample_rate = sample_rate; | |
186 s->channels = ac3_channels[s->flags & 7]; | |
187 if (s->flags & A52_LFE) | |
2979 | 188 s->channels++; |
189 if (avctx->channels == 0) | |
190 /* No specific number of channel requested */ | |
191 avctx->channels = s->channels; | |
192 else if (s->channels < avctx->channels) { | |
193 av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len); | |
194 avctx->channels = s->channels; | |
195 } | |
196 avctx->bit_rate = bit_rate; | |
332 | 197 } |
198 } | |
199 } else if (len < s->frame_size) { | |
200 len = s->frame_size - len; | |
201 if (len > buf_size) | |
202 len = buf_size; | |
203 | |
204 memcpy(s->inbuf_ptr, buf_ptr, len); | |
205 buf_ptr += len; | |
206 s->inbuf_ptr += len; | |
207 buf_size -= len; | |
208 } else { | |
209 flags = s->flags; | |
210 if (avctx->channels == 1) | |
211 flags = A52_MONO; | |
212 else if (avctx->channels == 2) | |
213 flags = A52_STEREO; | |
214 else | |
215 flags |= A52_ADJUST_LEVEL; | |
216 level = 1; | |
217 if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) { | |
218 fail: | |
4343 | 219 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n"); |
332 | 220 s->inbuf_ptr = s->inbuf; |
221 s->frame_size = 0; | |
222 continue; | |
223 } | |
224 for (i = 0; i < 6; i++) { | |
225 if (s->a52_block(s->state)) | |
226 goto fail; | |
227 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels); | |
228 } | |
229 s->inbuf_ptr = s->inbuf; | |
230 s->frame_size = 0; | |
1064 | 231 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t); |
332 | 232 break; |
233 } | |
234 } | |
235 return buf_ptr - buf; | |
236 } | |
237 | |
238 static int a52_decode_end(AVCodecContext *avctx) | |
239 { | |
240 AC3DecodeState *s = avctx->priv_data; | |
241 s->a52_free(s->state); | |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
242 #ifdef CONFIG_LIBA52BIN |
332 | 243 dlclose(s->handle); |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
244 #endif |
332 | 245 return 0; |
246 } | |
247 | |
4513
3367310f8460
Rename ac3 decoder to liba52 to prepare for native decoder.
diego
parents:
4511
diff
changeset
|
248 AVCodec liba52_decoder = { |
332 | 249 "ac3", |
250 CODEC_TYPE_AUDIO, | |
251 CODEC_ID_AC3, | |
252 sizeof(AC3DecodeState), | |
253 a52_decode_init, | |
254 NULL, | |
255 a52_decode_end, | |
256 a52_decode_frame, | |
257 }; |