]> git.sesse.net Git - ffmpeg/blob - libavcodec/binkaudio.c
ra288dec: set channel layout
[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 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.h"
39
40 extern const uint16_t ff_wma_critical_freqs[25];
41
42 static float quant_table[96];
43
44 #define MAX_CHANNELS 2
45 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
46
47 typedef struct {
48     AVFrame frame;
49     GetBitContext gb;
50     int version_b;          ///< Bink version 'b'
51     int first;
52     int channels;
53     int frame_len;          ///< transform size (samples)
54     int overlap_len;        ///< overlap size (samples)
55     int block_size;
56     int num_bands;
57     unsigned int *bands;
58     float root;
59     DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
60     float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16];  ///< coeffs from previous audio block
61     uint8_t *packet_buffer;
62     union {
63         RDFTContext rdft;
64         DCTContext dct;
65     } trans;
66 } BinkAudioContext;
67
68
69 static av_cold int decode_init(AVCodecContext *avctx)
70 {
71     BinkAudioContext *s = avctx->priv_data;
72     int sample_rate = avctx->sample_rate;
73     int sample_rate_half;
74     int i;
75     int frame_len_bits;
76
77     /* determine frame length */
78     if (avctx->sample_rate < 22050) {
79         frame_len_bits = 9;
80     } else if (avctx->sample_rate < 44100) {
81         frame_len_bits = 10;
82     } else {
83         frame_len_bits = 11;
84     }
85
86     if (avctx->channels > MAX_CHANNELS) {
87         av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
88         return -1;
89     }
90
91     s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
92
93     if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
94         // audio is already interleaved for the RDFT format variant
95         avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
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         avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
103     }
104
105     s->frame_len     = 1 << frame_len_bits;
106     s->overlap_len   = s->frame_len / 16;
107     s->block_size    = (s->frame_len - s->overlap_len) * s->channels;
108     sample_rate_half = (sample_rate + 1) / 2;
109     if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
110         s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
111     else
112         s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
113     for (i = 0; i < 96; i++) {
114         /* constant is result of 0.066399999/log10(M_E) */
115         quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
116     }
117
118     /* calculate number of bands */
119     for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
120         if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
121             break;
122
123     s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
124     if (!s->bands)
125         return AVERROR(ENOMEM);
126
127     /* populate bands data */
128     s->bands[0] = 2;
129     for (i = 1; i < s->num_bands; i++)
130         s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
131     s->bands[s->num_bands] = s->frame_len;
132
133     s->first = 1;
134
135     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
136         ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
137     else if (CONFIG_BINKAUDIO_DCT_DECODER)
138         ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
139     else
140         return -1;
141
142     avcodec_get_frame_defaults(&s->frame);
143     avctx->coded_frame = &s->frame;
144
145     return 0;
146 }
147
148 static float get_float(GetBitContext *gb)
149 {
150     int power = get_bits(gb, 5);
151     float f = ldexpf(get_bits_long(gb, 23), power - 23);
152     if (get_bits1(gb))
153         f = -f;
154     return f;
155 }
156
157 static const uint8_t rle_length_tab[16] = {
158     2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
159 };
160
161 /**
162  * Decode Bink Audio block
163  * @param[out] out Output buffer (must contain s->block_size elements)
164  * @return 0 on success, negative error code on failure
165  */
166 static int decode_block(BinkAudioContext *s, float **out, int use_dct)
167 {
168     int ch, i, j, k;
169     float q, quant[25];
170     int width, coeff;
171     GetBitContext *gb = &s->gb;
172
173     if (use_dct)
174         skip_bits(gb, 2);
175
176     for (ch = 0; ch < s->channels; ch++) {
177         FFTSample *coeffs = out[ch];
178
179         if (s->version_b) {
180             if (get_bits_left(gb) < 64)
181                 return AVERROR_INVALIDDATA;
182             coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
183             coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
184         } else {
185             if (get_bits_left(gb) < 58)
186                 return AVERROR_INVALIDDATA;
187             coeffs[0] = get_float(gb) * s->root;
188             coeffs[1] = get_float(gb) * s->root;
189         }
190
191         if (get_bits_left(gb) < s->num_bands * 8)
192             return AVERROR_INVALIDDATA;
193         for (i = 0; i < s->num_bands; i++) {
194             int value = get_bits(gb, 8);
195             quant[i]  = quant_table[FFMIN(value, 95)];
196         }
197
198         k = 0;
199         q = quant[0];
200
201         // parse coefficients
202         i = 2;
203         while (i < s->frame_len) {
204             if (s->version_b) {
205                 j = i + 16;
206             } else {
207                 int v = get_bits1(gb);
208                 if (v) {
209                     v = get_bits(gb, 4);
210                     j = i + rle_length_tab[v] * 8;
211                 } else {
212                     j = i + 8;
213                 }
214             }
215
216             j = FFMIN(j, s->frame_len);
217
218             width = get_bits(gb, 4);
219             if (width == 0) {
220                 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
221                 i = j;
222                 while (s->bands[k] < i)
223                     q = quant[k++];
224             } else {
225                 while (i < j) {
226                     if (s->bands[k] == i)
227                         q = quant[k++];
228                     coeff = get_bits(gb, width);
229                     if (coeff) {
230                         int v;
231                         v = get_bits1(gb);
232                         if (v)
233                             coeffs[i] = -q * coeff;
234                         else
235                             coeffs[i] =  q * coeff;
236                     } else {
237                         coeffs[i] = 0.0f;
238                     }
239                     i++;
240                 }
241             }
242         }
243
244         if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
245             coeffs[0] /= 0.5;
246             s->trans.dct.dct_calc(&s->trans.dct,  coeffs);
247         }
248         else if (CONFIG_BINKAUDIO_RDFT_DECODER)
249             s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
250     }
251
252     for (ch = 0; ch < s->channels; ch++) {
253         int j;
254         int count = s->overlap_len * s->channels;
255         if (!s->first) {
256             j = ch;
257             for (i = 0; i < s->overlap_len; i++, j += s->channels)
258                 out[ch][i] = (s->previous[ch][i] * (count - j) +
259                                       out[ch][i] *          j) / count;
260         }
261         memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
262                s->overlap_len * sizeof(*s->previous[ch]));
263     }
264
265     s->first = 0;
266
267     return 0;
268 }
269
270 static av_cold int decode_end(AVCodecContext *avctx)
271 {
272     BinkAudioContext * s = avctx->priv_data;
273     av_freep(&s->bands);
274     av_freep(&s->packet_buffer);
275     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
276         ff_rdft_end(&s->trans.rdft);
277     else if (CONFIG_BINKAUDIO_DCT_DECODER)
278         ff_dct_end(&s->trans.dct);
279
280     return 0;
281 }
282
283 static void get_bits_align32(GetBitContext *s)
284 {
285     int n = (-get_bits_count(s)) & 31;
286     if (n) skip_bits(s, n);
287 }
288
289 static int decode_frame(AVCodecContext *avctx, void *data,
290                         int *got_frame_ptr, AVPacket *avpkt)
291 {
292     BinkAudioContext *s = avctx->priv_data;
293     GetBitContext *gb = &s->gb;
294     int ret, consumed = 0;
295
296     if (!get_bits_left(gb)) {
297         uint8_t *buf;
298         /* handle end-of-stream */
299         if (!avpkt->size) {
300             *got_frame_ptr = 0;
301             return 0;
302         }
303         if (avpkt->size < 4) {
304             av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
305             return AVERROR_INVALIDDATA;
306         }
307         buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
308         if (!buf)
309             return AVERROR(ENOMEM);
310         s->packet_buffer = buf;
311         memcpy(s->packet_buffer, avpkt->data, avpkt->size);
312         init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
313         consumed = avpkt->size;
314
315         /* skip reported size */
316         skip_bits_long(gb, 32);
317     }
318
319     /* get output buffer */
320     s->frame.nb_samples = s->frame_len;
321     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
322         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
323         return ret;
324     }
325
326     if (decode_block(s, (float **)s->frame.extended_data,
327                      avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
328         av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
329         return AVERROR_INVALIDDATA;
330     }
331     get_bits_align32(gb);
332
333     s->frame.nb_samples = s->block_size / avctx->channels;
334     *got_frame_ptr   = 1;
335     *(AVFrame *)data = s->frame;
336
337     return consumed;
338 }
339
340 AVCodec ff_binkaudio_rdft_decoder = {
341     .name           = "binkaudio_rdft",
342     .type           = AVMEDIA_TYPE_AUDIO,
343     .id             = AV_CODEC_ID_BINKAUDIO_RDFT,
344     .priv_data_size = sizeof(BinkAudioContext),
345     .init           = decode_init,
346     .close          = decode_end,
347     .decode         = decode_frame,
348     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
349     .long_name      = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
350 };
351
352 AVCodec ff_binkaudio_dct_decoder = {
353     .name           = "binkaudio_dct",
354     .type           = AVMEDIA_TYPE_AUDIO,
355     .id             = AV_CODEC_ID_BINKAUDIO_DCT,
356     .priv_data_size = sizeof(BinkAudioContext),
357     .init           = decode_init,
358     .close          = decode_end,
359     .decode         = decode_frame,
360     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
361     .long_name      = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
362 };