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