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