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