Mercurial > libavcodec.hg
annotate a52dec.c @ 4513:3367310f8460 libavcodec
Rename ac3 decoder to liba52 to prepare for native decoder.
author | diego |
---|---|
date | Sun, 11 Feb 2007 19:02:59 +0000 |
parents | 91872ae737b2 |
children | 790d1cb93686 |
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> |
332 | 29 #include <dlfcn.h> |
30 static const char* liba52name = "liba52.so.0"; | |
31 | |
32 /** | |
33 * liba52 - Copyright (C) Aaron Holtzman | |
34 * released under the GPL license. | |
35 */ | |
36 typedef struct AC3DecodeState { | |
1064 | 37 uint8_t inbuf[4096]; /* input buffer */ |
38 uint8_t *inbuf_ptr; | |
332 | 39 int frame_size; |
40 int flags; | |
41 int channels; | |
42 a52_state_t* state; | |
43 sample_t* samples; | |
44 | |
45 /* | |
46 * virtual method table | |
47 * | |
48 * using this function table so the liba52 doesn't | |
49 * have to be really linked together with ffmpeg | |
50 * and might be linked in runtime - this allows binary | |
51 * distribution of ffmpeg library which doens't depend | |
52 * on liba52 library - but if user has it installed | |
53 * it will be used - user might install such library | |
54 * separately | |
55 */ | |
56 void* handle; | |
57 a52_state_t* (*a52_init)(uint32_t mm_accel); | |
58 sample_t* (*a52_samples)(a52_state_t * state); | |
59 int (*a52_syncinfo)(uint8_t * buf, int * flags, | |
2979 | 60 int * sample_rate, int * bit_rate); |
332 | 61 int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags, |
2979 | 62 sample_t * level, sample_t bias); |
332 | 63 void (*a52_dynrng)(a52_state_t * state, |
2979 | 64 sample_t (* call) (sample_t, void *), void * data); |
332 | 65 int (*a52_block)(a52_state_t * state); |
66 void (*a52_free)(a52_state_t * state); | |
67 | |
68 } AC3DecodeState; | |
69 | |
70 static void* dlsymm(void* handle, const char* symbol) | |
71 { | |
72 void* f = dlsym(handle, symbol); | |
73 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
|
74 av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol); |
332 | 75 return f; |
76 } | |
77 | |
78 static int a52_decode_init(AVCodecContext *avctx) | |
79 { | |
80 AC3DecodeState *s = avctx->priv_data; | |
81 | |
82 s->handle = dlopen(liba52name, RTLD_LAZY); | |
83 if (!s->handle) | |
84 { | |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2028
diff
changeset
|
85 av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror()); |
332 | 86 return -1; |
87 } | |
88 s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init"); | |
89 s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples"); | |
90 s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo"); | |
91 s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame"); | |
92 s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block"); | |
93 s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free"); | |
94 if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo | |
95 || !s->a52_frame || !s->a52_block || !s->a52_free) | |
96 { | |
2979 | 97 dlclose(s->handle); |
332 | 98 return -1; |
99 } | |
100 s->state = s->a52_init(0); /* later use CPU flags */ | |
101 s->samples = s->a52_samples(s->state); | |
102 s->inbuf_ptr = s->inbuf; | |
103 s->frame_size = 0; | |
104 | |
105 return 0; | |
106 } | |
107 | |
108 /**** the following two functions comes from a52dec */ | |
109 static inline int blah (int32_t i) | |
110 { | |
111 if (i > 0x43c07fff) | |
2979 | 112 return 32767; |
332 | 113 else if (i < 0x43bf8000) |
2979 | 114 return -32768; |
332 | 115 return i - 0x43c00000; |
116 } | |
117 | |
1064 | 118 static inline void float_to_int (float * _f, int16_t * s16, int nchannels) |
332 | 119 { |
120 int i, j, c; | |
2979 | 121 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format |
332 | 122 |
123 j = 0; | |
124 nchannels *= 256; | |
125 for (i = 0; i < 256; i++) { | |
2979 | 126 for (c = 0; c < nchannels; c += 256) |
127 s16[j++] = blah (f[i + c]); | |
332 | 128 } |
129 } | |
130 | |
131 /**** end */ | |
132 | |
133 #define HEADER_SIZE 7 | |
134 | |
135 static int a52_decode_frame(AVCodecContext *avctx, | |
136 void *data, int *data_size, | |
1064 | 137 uint8_t *buf, int buf_size) |
332 | 138 { |
139 AC3DecodeState *s = avctx->priv_data; | |
1064 | 140 uint8_t *buf_ptr; |
332 | 141 int flags, i, len; |
142 int sample_rate, bit_rate; | |
143 short *out_samples = data; | |
144 float level; | |
145 static const int ac3_channels[8] = { | |
2979 | 146 2, 1, 2, 3, 3, 4, 4, 5 |
332 | 147 }; |
148 | |
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
|
149 *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
|
150 |
332 | 151 buf_ptr = buf; |
152 while (buf_size > 0) { | |
153 len = s->inbuf_ptr - s->inbuf; | |
154 if (s->frame_size == 0) { | |
155 /* no header seen : find one. We need at least 7 bytes to parse it */ | |
156 len = HEADER_SIZE - len; | |
157 if (len > buf_size) | |
158 len = buf_size; | |
159 memcpy(s->inbuf_ptr, buf_ptr, len); | |
160 buf_ptr += len; | |
161 s->inbuf_ptr += len; | |
162 buf_size -= len; | |
163 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) { | |
164 len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
165 if (len == 0) { | |
166 /* no sync found : move by one byte (inefficient, but simple!) */ | |
167 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1); | |
168 s->inbuf_ptr--; | |
169 } else { | |
2979 | 170 s->frame_size = len; |
332 | 171 /* update codec info */ |
172 avctx->sample_rate = sample_rate; | |
173 s->channels = ac3_channels[s->flags & 7]; | |
174 if (s->flags & A52_LFE) | |
2979 | 175 s->channels++; |
176 if (avctx->channels == 0) | |
177 /* No specific number of channel requested */ | |
178 avctx->channels = s->channels; | |
179 else if (s->channels < avctx->channels) { | |
180 av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len); | |
181 avctx->channels = s->channels; | |
182 } | |
183 avctx->bit_rate = bit_rate; | |
332 | 184 } |
185 } | |
186 } else if (len < s->frame_size) { | |
187 len = s->frame_size - len; | |
188 if (len > buf_size) | |
189 len = buf_size; | |
190 | |
191 memcpy(s->inbuf_ptr, buf_ptr, len); | |
192 buf_ptr += len; | |
193 s->inbuf_ptr += len; | |
194 buf_size -= len; | |
195 } else { | |
196 flags = s->flags; | |
197 if (avctx->channels == 1) | |
198 flags = A52_MONO; | |
199 else if (avctx->channels == 2) | |
200 flags = A52_STEREO; | |
201 else | |
202 flags |= A52_ADJUST_LEVEL; | |
203 level = 1; | |
204 if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) { | |
205 fail: | |
4343 | 206 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n"); |
332 | 207 s->inbuf_ptr = s->inbuf; |
208 s->frame_size = 0; | |
209 continue; | |
210 } | |
211 for (i = 0; i < 6; i++) { | |
212 if (s->a52_block(s->state)) | |
213 goto fail; | |
214 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels); | |
215 } | |
216 s->inbuf_ptr = s->inbuf; | |
217 s->frame_size = 0; | |
1064 | 218 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t); |
332 | 219 break; |
220 } | |
221 } | |
222 return buf_ptr - buf; | |
223 } | |
224 | |
225 static int a52_decode_end(AVCodecContext *avctx) | |
226 { | |
227 AC3DecodeState *s = avctx->priv_data; | |
228 s->a52_free(s->state); | |
229 dlclose(s->handle); | |
230 return 0; | |
231 } | |
232 | |
4513
3367310f8460
Rename ac3 decoder to liba52 to prepare for native decoder.
diego
parents:
4511
diff
changeset
|
233 AVCodec liba52_decoder = { |
332 | 234 "ac3", |
235 CODEC_TYPE_AUDIO, | |
236 CODEC_ID_AC3, | |
237 sizeof(AC3DecodeState), | |
238 a52_decode_init, | |
239 NULL, | |
240 a52_decode_end, | |
241 a52_decode_frame, | |
242 }; |