ffmpeg / libavcodec / binkaudio.c @ 9806fbd5
History | View | Annotate | Download (8.99 KB)
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
|
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 |
#include "fft.h" |
36 |
#include "fmtconvert.h" |
37 |
|
38 |
extern const uint16_t ff_wma_critical_freqs[25]; |
39 |
|
40 |
#define MAX_CHANNELS 2 |
41 |
#define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11) |
42 |
|
43 |
typedef struct { |
44 |
AVCodecContext *avctx; |
45 |
GetBitContext gb; |
46 |
DSPContext dsp; |
47 |
FmtConvertContext fmt_conv; |
48 |
int first;
|
49 |
int channels;
|
50 |
int frame_len; ///< transform size (samples) |
51 |
int overlap_len; ///< overlap size (samples) |
52 |
int block_size;
|
53 |
int num_bands;
|
54 |
unsigned int *bands; |
55 |
float root;
|
56 |
DECLARE_ALIGNED(16, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
|
57 |
DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block |
58 |
float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave |
59 |
union {
|
60 |
RDFTContext rdft; |
61 |
DCTContext dct; |
62 |
} trans; |
63 |
} BinkAudioContext; |
64 |
|
65 |
|
66 |
static av_cold int decode_init(AVCodecContext *avctx) |
67 |
{ |
68 |
BinkAudioContext *s = avctx->priv_data; |
69 |
int sample_rate = avctx->sample_rate;
|
70 |
int sample_rate_half;
|
71 |
int i;
|
72 |
int frame_len_bits;
|
73 |
|
74 |
s->avctx = avctx; |
75 |
dsputil_init(&s->dsp, avctx); |
76 |
ff_fmt_convert_init(&s->fmt_conv, avctx); |
77 |
|
78 |
/* determine frame length */
|
79 |
if (avctx->sample_rate < 22050) { |
80 |
frame_len_bits = 9;
|
81 |
} else if (avctx->sample_rate < 44100) { |
82 |
frame_len_bits = 10;
|
83 |
} else {
|
84 |
frame_len_bits = 11;
|
85 |
} |
86 |
s->frame_len = 1 << frame_len_bits;
|
87 |
|
88 |
if (avctx->channels > MAX_CHANNELS) {
|
89 |
av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
|
90 |
return -1; |
91 |
} |
92 |
|
93 |
if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
|
94 |
// audio is already interleaved for the RDFT format variant
|
95 |
sample_rate *= avctx->channels; |
96 |
s->frame_len *= avctx->channels; |
97 |
s->channels = 1;
|
98 |
if (avctx->channels == 2) |
99 |
frame_len_bits++; |
100 |
} else {
|
101 |
s->channels = avctx->channels; |
102 |
} |
103 |
|
104 |
s->overlap_len = s->frame_len / 16;
|
105 |
s->block_size = (s->frame_len - s->overlap_len) * s->channels; |
106 |
sample_rate_half = (sample_rate + 1) / 2; |
107 |
s->root = 2.0 / sqrt(s->frame_len); |
108 |
|
109 |
/* calculate number of bands */
|
110 |
for (s->num_bands = 1; s->num_bands < 25; s->num_bands++) |
111 |
if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1]) |
112 |
break;
|
113 |
|
114 |
s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands)); |
115 |
if (!s->bands)
|
116 |
return AVERROR(ENOMEM);
|
117 |
|
118 |
/* populate bands data */
|
119 |
s->bands[0] = 1; |
120 |
for (i = 1; i < s->num_bands; i++) |
121 |
s->bands[i] = ff_wma_critical_freqs[i - 1] * (s->frame_len / 2) / sample_rate_half; |
122 |
s->bands[s->num_bands] = s->frame_len / 2;
|
123 |
|
124 |
s->first = 1;
|
125 |
avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
126 |
|
127 |
for (i = 0; i < s->channels; i++) |
128 |
s->coeffs_ptr[i] = s->coeffs + i * s->frame_len; |
129 |
|
130 |
if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
|
131 |
ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R); |
132 |
else if (CONFIG_BINKAUDIO_DCT_DECODER) |
133 |
ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III); |
134 |
else
|
135 |
return -1; |
136 |
|
137 |
return 0; |
138 |
} |
139 |
|
140 |
static float get_float(GetBitContext *gb) |
141 |
{ |
142 |
int power = get_bits(gb, 5); |
143 |
float f = ldexpf(get_bits_long(gb, 23), power - 23); |
144 |
if (get_bits1(gb))
|
145 |
f = -f; |
146 |
return f;
|
147 |
} |
148 |
|
149 |
static const uint8_t rle_length_tab[16] = { |
150 |
2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64 |
151 |
}; |
152 |
|
153 |
/**
|
154 |
* Decode Bink Audio block
|
155 |
* @param[out] out Output buffer (must contain s->block_size elements)
|
156 |
*/
|
157 |
static void decode_block(BinkAudioContext *s, short *out, int use_dct) |
158 |
{ |
159 |
int ch, i, j, k;
|
160 |
float q, quant[25]; |
161 |
int width, coeff;
|
162 |
GetBitContext *gb = &s->gb; |
163 |
|
164 |
if (use_dct)
|
165 |
skip_bits(gb, 2);
|
166 |
|
167 |
for (ch = 0; ch < s->channels; ch++) { |
168 |
FFTSample *coeffs = s->coeffs_ptr[ch]; |
169 |
q = 0.0f; |
170 |
coeffs[0] = get_float(gb) * s->root;
|
171 |
coeffs[1] = get_float(gb) * s->root;
|
172 |
|
173 |
for (i = 0; i < s->num_bands; i++) { |
174 |
/* constant is result of 0.066399999/log10(M_E) */
|
175 |
int value = get_bits(gb, 8); |
176 |
quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root; |
177 |
} |
178 |
|
179 |
// find band (k)
|
180 |
for (k = 0; s->bands[k] < 1; k++) { |
181 |
q = quant[k]; |
182 |
} |
183 |
|
184 |
// parse coefficients
|
185 |
i = 2;
|
186 |
while (i < s->frame_len) {
|
187 |
if (get_bits1(gb)) {
|
188 |
j = i + rle_length_tab[get_bits(gb, 4)] * 8; |
189 |
} else {
|
190 |
j = i + 8;
|
191 |
} |
192 |
|
193 |
j = FFMIN(j, s->frame_len); |
194 |
|
195 |
width = get_bits(gb, 4);
|
196 |
if (width == 0) { |
197 |
memset(coeffs + i, 0, (j - i) * sizeof(*coeffs)); |
198 |
i = j; |
199 |
while (s->bands[k] * 2 < i) |
200 |
q = quant[k++]; |
201 |
} else {
|
202 |
while (i < j) {
|
203 |
if (s->bands[k] * 2 == i) |
204 |
q = quant[k++]; |
205 |
coeff = get_bits(gb, width); |
206 |
if (coeff) {
|
207 |
if (get_bits1(gb))
|
208 |
coeffs[i] = -q * coeff; |
209 |
else
|
210 |
coeffs[i] = q * coeff; |
211 |
} else {
|
212 |
coeffs[i] = 0.0f; |
213 |
} |
214 |
i++; |
215 |
} |
216 |
} |
217 |
} |
218 |
|
219 |
if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
|
220 |
coeffs[0] /= 0.5; |
221 |
ff_dct_calc (&s->trans.dct, coeffs); |
222 |
s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
|
223 |
} |
224 |
else if (CONFIG_BINKAUDIO_RDFT_DECODER) |
225 |
ff_rdft_calc(&s->trans.rdft, coeffs); |
226 |
} |
227 |
|
228 |
s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr, |
229 |
s->frame_len, s->channels); |
230 |
|
231 |
if (!s->first) {
|
232 |
int count = s->overlap_len * s->channels;
|
233 |
int shift = av_log2(count);
|
234 |
for (i = 0; i < count; i++) { |
235 |
out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift; |
236 |
} |
237 |
} |
238 |
|
239 |
memcpy(s->previous, out + s->block_size, |
240 |
s->overlap_len * s->channels * sizeof(*out));
|
241 |
|
242 |
s->first = 0;
|
243 |
} |
244 |
|
245 |
static av_cold int decode_end(AVCodecContext *avctx) |
246 |
{ |
247 |
BinkAudioContext * s = avctx->priv_data; |
248 |
av_freep(&s->bands); |
249 |
if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
|
250 |
ff_rdft_end(&s->trans.rdft); |
251 |
else if (CONFIG_BINKAUDIO_DCT_DECODER) |
252 |
ff_dct_end(&s->trans.dct); |
253 |
return 0; |
254 |
} |
255 |
|
256 |
static void get_bits_align32(GetBitContext *s) |
257 |
{ |
258 |
int n = (-get_bits_count(s)) & 31; |
259 |
if (n) skip_bits(s, n);
|
260 |
} |
261 |
|
262 |
static int decode_frame(AVCodecContext *avctx, |
263 |
void *data, int *data_size, |
264 |
AVPacket *avpkt) |
265 |
{ |
266 |
BinkAudioContext *s = avctx->priv_data; |
267 |
const uint8_t *buf = avpkt->data;
|
268 |
int buf_size = avpkt->size;
|
269 |
short *samples = data;
|
270 |
short *samples_end = (short*)((uint8_t*)data + *data_size); |
271 |
int reported_size;
|
272 |
GetBitContext *gb = &s->gb; |
273 |
|
274 |
init_get_bits(gb, buf, buf_size * 8);
|
275 |
|
276 |
reported_size = get_bits_long(gb, 32);
|
277 |
while (get_bits_count(gb) / 8 < buf_size && |
278 |
samples + s->block_size <= samples_end) { |
279 |
decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT); |
280 |
samples += s->block_size; |
281 |
get_bits_align32(gb); |
282 |
} |
283 |
|
284 |
*data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data); |
285 |
return buf_size;
|
286 |
} |
287 |
|
288 |
AVCodec ff_binkaudio_rdft_decoder = { |
289 |
"binkaudio_rdft",
|
290 |
AVMEDIA_TYPE_AUDIO, |
291 |
CODEC_ID_BINKAUDIO_RDFT, |
292 |
sizeof(BinkAudioContext),
|
293 |
decode_init, |
294 |
NULL,
|
295 |
decode_end, |
296 |
decode_frame, |
297 |
.long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
|
298 |
}; |
299 |
|
300 |
AVCodec ff_binkaudio_dct_decoder = { |
301 |
"binkaudio_dct",
|
302 |
AVMEDIA_TYPE_AUDIO, |
303 |
CODEC_ID_BINKAUDIO_DCT, |
304 |
sizeof(BinkAudioContext),
|
305 |
decode_init, |
306 |
NULL,
|
307 |
decode_end, |
308 |
decode_frame, |
309 |
.long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
|
310 |
}; |