11067
|
1 /*
|
|
2 * Bink Audio decoder
|
|
3 * Copyright (c) 2007-2010 Peter Ross (pross@xvid.org)
|
|
4 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
|
|
5 *
|
|
6 * This file is part of FFmpeg.
|
|
7 *
|
|
8 * FFmpeg is free software; you can redistribute it and/or
|
|
9 * modify it under the terms of the GNU Lesser General Public
|
|
10 * License as published by the Free Software Foundation; either
|
|
11 * version 2.1 of the License, or (at your option) any later version.
|
|
12 *
|
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 * Lesser General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU Lesser General Public
|
|
19 * License along with FFmpeg; if not, write to the Free Software
|
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
21 */
|
|
22
|
|
23 /**
|
|
24 * @file libavcodec/binkaudio.c
|
|
25 * Bink Audio decoder
|
|
26 *
|
|
27 * Technical details here:
|
|
28 * http://wiki.multimedia.cx/index.php?title=Bink_Audio
|
|
29 */
|
|
30
|
|
31 #include "avcodec.h"
|
|
32 #define ALT_BITSTREAM_READER_LE
|
|
33 #include "get_bits.h"
|
|
34 #include "dsputil.h"
|
|
35 extern const uint16_t ff_wma_critical_freqs[25];
|
|
36
|
|
37 #define MAX_CHANNELS 2
|
|
38 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
|
|
39
|
|
40 typedef struct {
|
|
41 AVCodecContext *avctx;
|
|
42 GetBitContext gb;
|
|
43 DSPContext dsp;
|
|
44 int first;
|
|
45 int channels;
|
|
46 int frame_len; ///< transform size (samples)
|
|
47 int overlap_len; ///< overlap size (samples)
|
|
48 int block_size;
|
|
49 int num_bands;
|
|
50 unsigned int *bands;
|
|
51 float root;
|
11069
|
52 DECLARE_ALIGNED_16(FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
|
|
53 DECLARE_ALIGNED_16(short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
|
11067
|
54 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
|
|
55 union {
|
|
56 RDFTContext rdft;
|
|
57 DCTContext dct;
|
|
58 } trans;
|
|
59 } BinkAudioContext;
|
|
60
|
|
61
|
|
62 static av_cold int decode_init(AVCodecContext *avctx)
|
|
63 {
|
|
64 BinkAudioContext *s = avctx->priv_data;
|
|
65 int sample_rate = avctx->sample_rate;
|
|
66 int sample_rate_half;
|
|
67 int i;
|
|
68 int frame_len_bits;
|
|
69
|
|
70 s->avctx = avctx;
|
|
71 dsputil_init(&s->dsp, avctx);
|
|
72
|
|
73 /* determine frame length */
|
|
74 if (avctx->sample_rate < 22050) {
|
|
75 frame_len_bits = 9;
|
|
76 } else if (avctx->sample_rate < 44100) {
|
|
77 frame_len_bits = 10;
|
|
78 } else {
|
|
79 frame_len_bits = 11;
|
|
80 }
|
|
81 s->frame_len = 1 << frame_len_bits;
|
|
82
|
|
83 if (s->channels > MAX_CHANNELS) {
|
|
84 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
|
|
85 return -1;
|
|
86 }
|
|
87
|
|
88 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
|
|
89 // audio is already interleaved for the RDFT format variant
|
|
90 sample_rate *= avctx->channels;
|
|
91 s->frame_len *= avctx->channels;
|
|
92 s->channels = 1;
|
|
93 if (avctx->channels == 2)
|
|
94 frame_len_bits++;
|
|
95 } else {
|
|
96 s->channels = avctx->channels;
|
|
97 }
|
|
98
|
|
99 s->overlap_len = s->frame_len / 16;
|
|
100 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
|
|
101 sample_rate_half = (sample_rate + 1) / 2;
|
|
102 s->root = 2.0 / sqrt(s->frame_len);
|
|
103
|
|
104 /* calculate number of bands */
|
|
105 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
|
|
106 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
|
|
107 break;
|
|
108
|
|
109 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
|
|
110 if (!s->bands)
|
|
111 return AVERROR(ENOMEM);
|
|
112
|
|
113 /* populate bands data */
|
|
114 s->bands[0] = 1;
|
|
115 for (i = 1; i < s->num_bands; i++)
|
|
116 s->bands[i] = ff_wma_critical_freqs[i - 1] * (s->frame_len / 2) / sample_rate_half;
|
|
117 s->bands[s->num_bands] = s->frame_len / 2;
|
|
118
|
|
119 s->first = 1;
|
|
120 avctx->sample_fmt = SAMPLE_FMT_S16;
|
|
121
|
|
122 for (i = 0; i < s->channels; i++)
|
|
123 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
|
|
124
|
|
125 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
|
|
126 ff_rdft_init(&s->trans.rdft, frame_len_bits, IRIDFT);
|
|
127 else
|
|
128 ff_dct_init(&s->trans.dct, frame_len_bits, 0);
|
|
129
|
|
130 return 0;
|
|
131 }
|
|
132
|
|
133 static float get_float(GetBitContext *gb)
|
|
134 {
|
|
135 int power = get_bits(gb, 5);
|
|
136 float f = ldexpf(get_bits_long(gb, 23), power - 23);
|
|
137 if (get_bits1(gb))
|
|
138 f = -f;
|
|
139 return f;
|
|
140 }
|
|
141
|
|
142 static const uint8_t rle_length_tab[16] = {
|
|
143 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
|
|
144 };
|
|
145
|
|
146 /**
|
|
147 * Decode Bink Audio block
|
|
148 * @param[out] out Output buffer (must contain s->block_size elements)
|
|
149 */
|
|
150 static void decode_block(BinkAudioContext *s, short *out, int use_dct)
|
|
151 {
|
|
152 int ch, i, j, k;
|
|
153 float q, quant[25];
|
|
154 int width, coeff;
|
|
155 GetBitContext *gb = &s->gb;
|
|
156
|
|
157 if (use_dct)
|
|
158 skip_bits(gb, 2);
|
|
159
|
|
160 for (ch = 0; ch < s->channels; ch++) {
|
|
161 FFTSample *coeffs = s->coeffs_ptr[ch];
|
|
162 q = 0.0f;
|
|
163 coeffs[0] = get_float(gb) * s->root;
|
|
164 coeffs[1] = get_float(gb) * s->root;
|
|
165
|
|
166 for (i = 0; i < s->num_bands; i++) {
|
|
167 /* constant is result of 0.066399999/log10(M_E) */
|
|
168 int value = get_bits(gb, 8);
|
|
169 quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
|
|
170 }
|
|
171
|
|
172 // find band (k)
|
|
173 for (k = 0; s->bands[k] < 1; k++) {
|
|
174 q = quant[k];
|
|
175 }
|
|
176
|
|
177 // parse coefficients
|
|
178 i = 2;
|
|
179 while (i < s->frame_len) {
|
|
180 if (get_bits1(gb)) {
|
|
181 j = i + rle_length_tab[get_bits(gb, 4)] * 8;
|
|
182 } else {
|
|
183 j = i + 8;
|
|
184 }
|
|
185
|
|
186 j = FFMIN(j, s->frame_len);
|
|
187
|
|
188 width = get_bits(gb, 4);
|
|
189 if (width == 0) {
|
|
190 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
|
|
191 i = j;
|
|
192 while (s->bands[k] * 2 < i)
|
|
193 q = quant[k++];
|
|
194 } else {
|
|
195 while (i < j) {
|
|
196 if (s->bands[k] * 2 == i)
|
|
197 q = quant[k++];
|
|
198 coeff = get_bits(gb, width);
|
|
199 if (coeff) {
|
|
200 if (get_bits1(gb))
|
|
201 coeffs[i] = -q * coeff;
|
|
202 else
|
|
203 coeffs[i] = q * coeff;
|
|
204 } else {
|
|
205 coeffs[i] = 0.0f;
|
|
206 }
|
|
207 i++;
|
|
208 }
|
|
209 }
|
|
210 }
|
|
211
|
|
212 if (use_dct)
|
|
213 ff_dct_calc (&s->trans.dct, coeffs);
|
|
214 else
|
|
215 ff_rdft_calc(&s->trans.rdft, coeffs);
|
|
216 }
|
|
217
|
|
218 s->dsp.float_to_int16_interleave(out, (const float **)s->coeffs_ptr, s->frame_len, s->channels);
|
|
219
|
|
220 if (!s->first) {
|
|
221 int count = s->overlap_len * s->channels;
|
|
222 int shift = av_log2(count);
|
|
223 for (i = 0; i < count; i++) {
|
|
224 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
|
|
225 }
|
|
226 }
|
|
227
|
|
228 memcpy(s->previous, out + s->block_size,
|
|
229 s->overlap_len * s->channels * sizeof(*out));
|
|
230
|
|
231 s->first = 0;
|
|
232 }
|
|
233
|
|
234 static av_cold int decode_end(AVCodecContext *avctx)
|
|
235 {
|
|
236 BinkAudioContext * s = avctx->priv_data;
|
|
237 av_freep(&s->bands);
|
|
238 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
|
|
239 ff_rdft_end(&s->trans.rdft);
|
|
240 else
|
|
241 ff_dct_end(&s->trans.dct);
|
|
242 return 0;
|
|
243 }
|
|
244
|
|
245 static void get_bits_align32(GetBitContext *s)
|
|
246 {
|
|
247 int n = (-get_bits_count(s)) & 31;
|
|
248 if (n) skip_bits(s, n);
|
|
249 }
|
|
250
|
|
251 static int decode_frame(AVCodecContext *avctx,
|
|
252 void *data, int *data_size,
|
|
253 AVPacket *avpkt)
|
|
254 {
|
|
255 BinkAudioContext *s = avctx->priv_data;
|
|
256 const uint8_t *buf = avpkt->data;
|
|
257 int buf_size = avpkt->size;
|
|
258 short *samples = data;
|
|
259 short *samples_end = (short*)((uint8_t*)data + *data_size);
|
|
260 int reported_size;
|
|
261 GetBitContext *gb = &s->gb;
|
|
262
|
|
263 init_get_bits(gb, buf, buf_size * 8);
|
|
264
|
|
265 reported_size = get_bits_long(gb, 32);
|
|
266 while (get_bits_count(gb) / 8 < buf_size &&
|
|
267 samples + s->block_size <= samples_end) {
|
|
268 decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
|
|
269 samples += s->block_size;
|
|
270 get_bits_align32(gb);
|
|
271 }
|
|
272
|
|
273 *data_size = (uint8_t*)samples - (uint8_t*)data;
|
|
274 if (reported_size != *data_size) {
|
|
275 av_log(avctx, AV_LOG_WARNING, "reported data size (%d) does not match output data size (%d)\n",
|
|
276 reported_size, *data_size);
|
|
277 }
|
|
278 return buf_size;
|
|
279 }
|
|
280
|
|
281 AVCodec binkaudio_rdft_decoder = {
|
|
282 "binkaudio_rdft",
|
|
283 CODEC_TYPE_AUDIO,
|
|
284 CODEC_ID_BINKAUDIO_RDFT,
|
|
285 sizeof(BinkAudioContext),
|
|
286 decode_init,
|
|
287 NULL,
|
|
288 decode_end,
|
|
289 decode_frame,
|
|
290 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
|
|
291 };
|
|
292
|
|
293 AVCodec binkaudio_dct_decoder = {
|
|
294 "binkaudio_dct",
|
|
295 CODEC_TYPE_AUDIO,
|
|
296 CODEC_ID_BINKAUDIO_DCT,
|
|
297 sizeof(BinkAudioContext),
|
|
298 decode_init,
|
|
299 NULL,
|
|
300 decode_end,
|
|
301 decode_frame,
|
|
302 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
|
|
303 };
|