Mercurial > libavcodec.hg
annotate liba52.c @ 7639:b9a51c307ea7 libavcodec
first and second stage LSP codebooks for G.729
author | voroshil |
---|---|
date | Thu, 21 Aug 2008 13:35:23 +0000 |
parents | 85ab7655ad4d |
children | dc1a7a6ec58d |
rev | line source |
---|---|
332 | 1 /* |
4617 | 2 * A52 decoder using liba52 |
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 | |
4617 | 24 * A52 decoder using liba52 |
1106 | 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 { | |
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 | |
4618
bb06df70f857
Add proper #idef to dlsymm call, also fixes an unresolved symbol on OS X 10.2.
diego
parents:
4617
diff
changeset
|
70 #ifdef CONFIG_LIBA52BIN |
332 | 71 static void* dlsymm(void* handle, const char* symbol) |
72 { | |
73 void* f = dlsym(handle, symbol); | |
74 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
|
75 av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol); |
332 | 76 return f; |
77 } | |
4618
bb06df70f857
Add proper #idef to dlsymm call, also fixes an unresolved symbol on OS X 10.2.
diego
parents:
4617
diff
changeset
|
78 #endif |
332 | 79 |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6352
diff
changeset
|
80 static av_cold int a52_decode_init(AVCodecContext *avctx) |
332 | 81 { |
82 AC3DecodeState *s = avctx->priv_data; | |
83 | |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
84 #ifdef CONFIG_LIBA52BIN |
332 | 85 s->handle = dlopen(liba52name, RTLD_LAZY); |
86 if (!s->handle) | |
87 { | |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2028
diff
changeset
|
88 av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror()); |
332 | 89 return -1; |
90 } | |
91 s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init"); | |
92 s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples"); | |
93 s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo"); | |
94 s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame"); | |
95 s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block"); | |
96 s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free"); | |
97 if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo | |
98 || !s->a52_frame || !s->a52_block || !s->a52_free) | |
99 { | |
2979 | 100 dlclose(s->handle); |
332 | 101 return -1; |
102 } | |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
103 #else |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
104 s->handle = 0; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
105 s->a52_init = a52_init; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
106 s->a52_samples = a52_samples; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
107 s->a52_syncinfo = a52_syncinfo; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
108 s->a52_frame = a52_frame; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
109 s->a52_block = a52_block; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
110 s->a52_free = a52_free; |
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
111 #endif |
332 | 112 s->state = s->a52_init(0); /* later use CPU flags */ |
113 s->samples = s->a52_samples(s->state); | |
114 | |
6114 | 115 /* allow downmixing to stereo or mono */ |
116 if (avctx->channels > 0 && avctx->request_channels > 0 && | |
117 avctx->request_channels < avctx->channels && | |
118 avctx->request_channels <= 2) { | |
119 avctx->channels = avctx->request_channels; | |
120 } | |
121 | |
7451
85ab7655ad4d
Modify all codecs to report their supported input and output sample format(s).
pross
parents:
7040
diff
changeset
|
122 avctx->sample_fmt = SAMPLE_FMT_S16; |
332 | 123 return 0; |
124 } | |
125 | |
5525
bc4791868c52
various simplifications around recent av_clip_int16() usage
aurel
parents:
5523
diff
changeset
|
126 /**** the following function comes from a52dec */ |
1064 | 127 static inline void float_to_int (float * _f, int16_t * s16, int nchannels) |
332 | 128 { |
129 int i, j, c; | |
2979 | 130 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format |
332 | 131 |
132 j = 0; | |
133 nchannels *= 256; | |
134 for (i = 0; i < 256; i++) { | |
2979 | 135 for (c = 0; c < nchannels; c += 256) |
5525
bc4791868c52
various simplifications around recent av_clip_int16() usage
aurel
parents:
5523
diff
changeset
|
136 s16[j++] = av_clip_int16(f[i + c] - 0x43c00000); |
332 | 137 } |
138 } | |
139 | |
140 /**** end */ | |
141 | |
142 #define HEADER_SIZE 7 | |
143 | |
144 static int a52_decode_frame(AVCodecContext *avctx, | |
145 void *data, int *data_size, | |
1064 | 146 uint8_t *buf, int buf_size) |
332 | 147 { |
148 AC3DecodeState *s = avctx->priv_data; | |
149 int flags, i, len; | |
150 int sample_rate, bit_rate; | |
151 short *out_samples = data; | |
152 float level; | |
153 static const int ac3_channels[8] = { | |
2979 | 154 2, 1, 2, 3, 3, 4, 4, 5 |
332 | 155 }; |
156 | |
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
|
157 *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
|
158 |
6351
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
159 if (buf_size < HEADER_SIZE) { |
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
160 av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes for header\n"); |
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
161 return -1; |
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
162 } |
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
163 len = s->a52_syncinfo(buf, &s->flags, &sample_rate, &bit_rate); |
6352 | 164 if (len == 0) { |
165 av_log(avctx, AV_LOG_ERROR, "Error decoding frame, no sync byte at begin\n"); | |
166 return -1; | |
167 } | |
6351
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
168 if (buf_size < len) { |
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
169 av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes\n"); |
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
170 return -1; |
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
171 } |
6352 | 172 /* update codec info */ |
173 avctx->sample_rate = sample_rate; | |
174 s->channels = ac3_channels[s->flags & 7]; | |
175 if (s->flags & A52_LFE) | |
176 s->channels++; | |
177 if (avctx->request_channels > 0 && | |
178 avctx->request_channels <= 2 && | |
179 avctx->request_channels < s->channels) { | |
180 avctx->channels = avctx->request_channels; | |
181 } else { | |
182 avctx->channels = s->channels; | |
183 } | |
184 avctx->bit_rate = bit_rate; | |
185 flags = s->flags; | |
186 if (avctx->channels == 1) | |
187 flags = A52_MONO; | |
188 else if (avctx->channels == 2) | |
189 flags = A52_STEREO; | |
190 else | |
191 flags |= A52_ADJUST_LEVEL; | |
192 level = 1; | |
193 if (s->a52_frame(s->state, buf, &flags, &level, 384)) { | |
194 fail: | |
195 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n"); | |
196 return -1; | |
197 } | |
198 for (i = 0; i < 6; i++) { | |
199 if (s->a52_block(s->state)) | |
200 goto fail; | |
201 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels); | |
202 } | |
203 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t); | |
6351
b568fe642948
Remove useless buffering of input data, so that avcodec_decode_audio never
superdump
parents:
6115
diff
changeset
|
204 return len; |
332 | 205 } |
206 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6352
diff
changeset
|
207 static av_cold int a52_decode_end(AVCodecContext *avctx) |
332 | 208 { |
209 AC3DecodeState *s = avctx->priv_data; | |
210 s->a52_free(s->state); | |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
211 #ifdef CONFIG_LIBA52BIN |
332 | 212 dlclose(s->handle); |
4514
790d1cb93686
Restore the possibility to link liba52 instead of dlopening.
diego
parents:
4513
diff
changeset
|
213 #endif |
332 | 214 return 0; |
215 } | |
216 | |
4513
3367310f8460
Rename ac3 decoder to liba52 to prepare for native decoder.
diego
parents:
4511
diff
changeset
|
217 AVCodec liba52_decoder = { |
5102
4323e587708d
Give names of external library decoders/encoders a lib prefix
diego
parents:
5101
diff
changeset
|
218 "liba52", |
332 | 219 CODEC_TYPE_AUDIO, |
220 CODEC_ID_AC3, | |
221 sizeof(AC3DecodeState), | |
222 a52_decode_init, | |
223 NULL, | |
224 a52_decode_end, | |
225 a52_decode_frame, | |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6712
diff
changeset
|
226 .long_name = NULL_IF_CONFIG_SMALL("liba52 ATSC A/52 / AC-3"), |
332 | 227 }; |