Mercurial > libavcodec.hg
comparison liba52.c @ 5101:c3f2379b80db libavcodec
Give all wrappers for external libraries names starting with lib.
author | diego |
---|---|
date | Wed, 06 Jun 2007 00:14:18 +0000 |
parents | a52dec.c@bb06df70f857 |
children | 4323e587708d |
comparison
equal
deleted
inserted
replaced
5100:daff9ebd2e0b | 5101:c3f2379b80db |
---|---|
1 /* | |
2 * A52 decoder using liba52 | |
3 * Copyright (c) 2001 Fabrice Bellard. | |
4 * | |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 /** | |
23 * @file a52dec.c | |
24 * A52 decoder using liba52 | |
25 */ | |
26 | |
27 #include "avcodec.h" | |
28 #include <a52dec/a52.h> | |
29 | |
30 #ifdef CONFIG_LIBA52BIN | |
31 #include <dlfcn.h> | |
32 static const char* liba52name = "liba52.so.0"; | |
33 #endif | |
34 | |
35 /** | |
36 * liba52 - Copyright (C) Aaron Holtzman | |
37 * released under the GPL license. | |
38 */ | |
39 typedef struct AC3DecodeState { | |
40 uint8_t inbuf[4096]; /* input buffer */ | |
41 uint8_t *inbuf_ptr; | |
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, | |
63 int * sample_rate, int * bit_rate); | |
64 int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags, | |
65 sample_t * level, sample_t bias); | |
66 void (*a52_dynrng)(a52_state_t * state, | |
67 sample_t (* call) (sample_t, void *), void * data); | |
68 int (*a52_block)(a52_state_t * state); | |
69 void (*a52_free)(a52_state_t * state); | |
70 | |
71 } AC3DecodeState; | |
72 | |
73 #ifdef CONFIG_LIBA52BIN | |
74 static void* dlsymm(void* handle, const char* symbol) | |
75 { | |
76 void* f = dlsym(handle, symbol); | |
77 if (!f) | |
78 av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol); | |
79 return f; | |
80 } | |
81 #endif | |
82 | |
83 static int a52_decode_init(AVCodecContext *avctx) | |
84 { | |
85 AC3DecodeState *s = avctx->priv_data; | |
86 | |
87 #ifdef CONFIG_LIBA52BIN | |
88 s->handle = dlopen(liba52name, RTLD_LAZY); | |
89 if (!s->handle) | |
90 { | |
91 av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror()); | |
92 return -1; | |
93 } | |
94 s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init"); | |
95 s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples"); | |
96 s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo"); | |
97 s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame"); | |
98 s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block"); | |
99 s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free"); | |
100 if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo | |
101 || !s->a52_frame || !s->a52_block || !s->a52_free) | |
102 { | |
103 dlclose(s->handle); | |
104 return -1; | |
105 } | |
106 #else | |
107 s->handle = 0; | |
108 s->a52_init = a52_init; | |
109 s->a52_samples = a52_samples; | |
110 s->a52_syncinfo = a52_syncinfo; | |
111 s->a52_frame = a52_frame; | |
112 s->a52_block = a52_block; | |
113 s->a52_free = a52_free; | |
114 #endif | |
115 s->state = s->a52_init(0); /* later use CPU flags */ | |
116 s->samples = s->a52_samples(s->state); | |
117 s->inbuf_ptr = s->inbuf; | |
118 s->frame_size = 0; | |
119 | |
120 return 0; | |
121 } | |
122 | |
123 /**** the following two functions comes from a52dec */ | |
124 static inline int blah (int32_t i) | |
125 { | |
126 if (i > 0x43c07fff) | |
127 return 32767; | |
128 else if (i < 0x43bf8000) | |
129 return -32768; | |
130 return i - 0x43c00000; | |
131 } | |
132 | |
133 static inline void float_to_int (float * _f, int16_t * s16, int nchannels) | |
134 { | |
135 int i, j, c; | |
136 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format | |
137 | |
138 j = 0; | |
139 nchannels *= 256; | |
140 for (i = 0; i < 256; i++) { | |
141 for (c = 0; c < nchannels; c += 256) | |
142 s16[j++] = blah (f[i + c]); | |
143 } | |
144 } | |
145 | |
146 /**** end */ | |
147 | |
148 #define HEADER_SIZE 7 | |
149 | |
150 static int a52_decode_frame(AVCodecContext *avctx, | |
151 void *data, int *data_size, | |
152 uint8_t *buf, int buf_size) | |
153 { | |
154 AC3DecodeState *s = avctx->priv_data; | |
155 uint8_t *buf_ptr; | |
156 int flags, i, len; | |
157 int sample_rate, bit_rate; | |
158 short *out_samples = data; | |
159 float level; | |
160 static const int ac3_channels[8] = { | |
161 2, 1, 2, 3, 3, 4, 4, 5 | |
162 }; | |
163 | |
164 *data_size= 0; | |
165 | |
166 buf_ptr = buf; | |
167 while (buf_size > 0) { | |
168 len = s->inbuf_ptr - s->inbuf; | |
169 if (s->frame_size == 0) { | |
170 /* no header seen : find one. We need at least 7 bytes to parse it */ | |
171 len = HEADER_SIZE - len; | |
172 if (len > buf_size) | |
173 len = buf_size; | |
174 memcpy(s->inbuf_ptr, buf_ptr, len); | |
175 buf_ptr += len; | |
176 s->inbuf_ptr += len; | |
177 buf_size -= len; | |
178 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) { | |
179 len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
180 if (len == 0) { | |
181 /* no sync found : move by one byte (inefficient, but simple!) */ | |
182 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1); | |
183 s->inbuf_ptr--; | |
184 } else { | |
185 s->frame_size = len; | |
186 /* update codec info */ | |
187 avctx->sample_rate = sample_rate; | |
188 s->channels = ac3_channels[s->flags & 7]; | |
189 if (s->flags & A52_LFE) | |
190 s->channels++; | |
191 if (avctx->channels == 0) | |
192 /* No specific number of channel requested */ | |
193 avctx->channels = s->channels; | |
194 else if (s->channels < avctx->channels) { | |
195 av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len); | |
196 avctx->channels = s->channels; | |
197 } | |
198 avctx->bit_rate = bit_rate; | |
199 } | |
200 } | |
201 } else if (len < s->frame_size) { | |
202 len = s->frame_size - len; | |
203 if (len > buf_size) | |
204 len = buf_size; | |
205 | |
206 memcpy(s->inbuf_ptr, buf_ptr, len); | |
207 buf_ptr += len; | |
208 s->inbuf_ptr += len; | |
209 buf_size -= len; | |
210 } else { | |
211 flags = s->flags; | |
212 if (avctx->channels == 1) | |
213 flags = A52_MONO; | |
214 else if (avctx->channels == 2) | |
215 flags = A52_STEREO; | |
216 else | |
217 flags |= A52_ADJUST_LEVEL; | |
218 level = 1; | |
219 if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) { | |
220 fail: | |
221 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n"); | |
222 s->inbuf_ptr = s->inbuf; | |
223 s->frame_size = 0; | |
224 continue; | |
225 } | |
226 for (i = 0; i < 6; i++) { | |
227 if (s->a52_block(s->state)) | |
228 goto fail; | |
229 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels); | |
230 } | |
231 s->inbuf_ptr = s->inbuf; | |
232 s->frame_size = 0; | |
233 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t); | |
234 break; | |
235 } | |
236 } | |
237 return buf_ptr - buf; | |
238 } | |
239 | |
240 static int a52_decode_end(AVCodecContext *avctx) | |
241 { | |
242 AC3DecodeState *s = avctx->priv_data; | |
243 s->a52_free(s->state); | |
244 #ifdef CONFIG_LIBA52BIN | |
245 dlclose(s->handle); | |
246 #endif | |
247 return 0; | |
248 } | |
249 | |
250 AVCodec liba52_decoder = { | |
251 "ac3", | |
252 CODEC_TYPE_AUDIO, | |
253 CODEC_ID_AC3, | |
254 sizeof(AC3DecodeState), | |
255 a52_decode_init, | |
256 NULL, | |
257 a52_decode_end, | |
258 a52_decode_frame, | |
259 }; |