OSDN Git Service

doc: explain __STDC_CONSTANT_MACROS in C++
[coroid/libav_saccubus.git] / 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 "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     s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
94
95     if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
96         // audio is already interleaved for the RDFT format variant
97         sample_rate  *= avctx->channels;
98         s->channels = 1;
99         if (!s->version_b)
100             frame_len_bits += av_log2(avctx->channels);
101     } else {
102         s->channels = avctx->channels;
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     s->root          = 2.0 / sqrt(s->frame_len);
110
111     /* calculate number of bands */
112     for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
113         if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
114             break;
115
116     s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
117     if (!s->bands)
118         return AVERROR(ENOMEM);
119
120     /* populate bands data */
121     s->bands[0] = 2;
122     for (i = 1; i < s->num_bands; i++)
123         s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
124     s->bands[s->num_bands] = s->frame_len;
125
126     s->first = 1;
127     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
128
129     for (i = 0; i < s->channels; i++)
130         s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
131
132     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
133         ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
134     else if (CONFIG_BINKAUDIO_DCT_DECODER)
135         ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
136     else
137         return -1;
138
139     return 0;
140 }
141
142 static float get_float(GetBitContext *gb)
143 {
144     int power = get_bits(gb, 5);
145     float f = ldexpf(get_bits_long(gb, 23), power - 23);
146     if (get_bits1(gb))
147         f = -f;
148     return f;
149 }
150
151 static const uint8_t rle_length_tab[16] = {
152     2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
153 };
154
155 /**
156  * Decode Bink Audio block
157  * @param[out] out Output buffer (must contain s->block_size elements)
158  */
159 static void decode_block(BinkAudioContext *s, short *out, int use_dct)
160 {
161     int ch, i, j, k;
162     float q, quant[25];
163     int width, coeff;
164     GetBitContext *gb = &s->gb;
165
166     if (use_dct)
167         skip_bits(gb, 2);
168
169     for (ch = 0; ch < s->channels; ch++) {
170         FFTSample *coeffs = s->coeffs_ptr[ch];
171         if (s->version_b) {
172             coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
173             coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
174         } else {
175             coeffs[0] = get_float(gb) * s->root;
176             coeffs[1] = get_float(gb) * s->root;
177         }
178
179         for (i = 0; i < s->num_bands; i++) {
180             /* constant is result of 0.066399999/log10(M_E) */
181             int value = get_bits(gb, 8);
182             quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
183         }
184
185         k = 0;
186         q = quant[0];
187
188         // parse coefficients
189         i = 2;
190         while (i < s->frame_len) {
191             if (s->version_b) {
192                 j = i + 16;
193             } else if (get_bits1(gb)) {
194                 j = i + rle_length_tab[get_bits(gb, 4)] * 8;
195             } else {
196                 j = i + 8;
197             }
198
199             j = FFMIN(j, s->frame_len);
200
201             width = get_bits(gb, 4);
202             if (width == 0) {
203                 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
204                 i = j;
205                 while (s->bands[k] < i)
206                     q = quant[k++];
207             } else {
208                 while (i < j) {
209                     if (s->bands[k] == i)
210                         q = quant[k++];
211                     coeff = get_bits(gb, width);
212                     if (coeff) {
213                         if (get_bits1(gb))
214                             coeffs[i] = -q * coeff;
215                         else
216                             coeffs[i] =  q * coeff;
217                     } else {
218                         coeffs[i] = 0.0f;
219                     }
220                     i++;
221                 }
222             }
223         }
224
225         if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
226             coeffs[0] /= 0.5;
227             s->trans.dct.dct_calc(&s->trans.dct,  coeffs);
228             s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
229         }
230         else if (CONFIG_BINKAUDIO_RDFT_DECODER)
231             s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
232     }
233
234     s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
235                                           s->frame_len, s->channels);
236
237     if (!s->first) {
238         int count = s->overlap_len * s->channels;
239         int shift = av_log2(count);
240         for (i = 0; i < count; i++) {
241             out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
242         }
243     }
244
245     memcpy(s->previous, out + s->block_size,
246            s->overlap_len * s->channels * sizeof(*out));
247
248     s->first = 0;
249 }
250
251 static av_cold int decode_end(AVCodecContext *avctx)
252 {
253     BinkAudioContext * s = avctx->priv_data;
254     av_freep(&s->bands);
255     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
256         ff_rdft_end(&s->trans.rdft);
257     else if (CONFIG_BINKAUDIO_DCT_DECODER)
258         ff_dct_end(&s->trans.dct);
259     return 0;
260 }
261
262 static void get_bits_align32(GetBitContext *s)
263 {
264     int n = (-get_bits_count(s)) & 31;
265     if (n) skip_bits(s, n);
266 }
267
268 static int decode_frame(AVCodecContext *avctx,
269                         void *data, int *data_size,
270                         AVPacket *avpkt)
271 {
272     BinkAudioContext *s = avctx->priv_data;
273     const uint8_t *buf  = avpkt->data;
274     int buf_size        = avpkt->size;
275     short *samples      = data;
276     short *samples_end  = (short*)((uint8_t*)data + *data_size);
277     int reported_size;
278     GetBitContext *gb = &s->gb;
279
280     init_get_bits(gb, buf, buf_size * 8);
281
282     reported_size = get_bits_long(gb, 32);
283     while (get_bits_count(gb) / 8 < buf_size &&
284            samples + s->block_size <= samples_end) {
285         decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
286         samples += s->block_size;
287         get_bits_align32(gb);
288     }
289
290     *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
291     return buf_size;
292 }
293
294 AVCodec ff_binkaudio_rdft_decoder = {
295     .name           = "binkaudio_rdft",
296     .type           = AVMEDIA_TYPE_AUDIO,
297     .id             = CODEC_ID_BINKAUDIO_RDFT,
298     .priv_data_size = sizeof(BinkAudioContext),
299     .init           = decode_init,
300     .close          = decode_end,
301     .decode         = decode_frame,
302     .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
303 };
304
305 AVCodec ff_binkaudio_dct_decoder = {
306     .name           = "binkaudio_dct",
307     .type           = AVMEDIA_TYPE_AUDIO,
308     .id             = CODEC_ID_BINKAUDIO_DCT,
309     .priv_data_size = sizeof(BinkAudioContext),
310     .init           = decode_init,
311     .close          = decode_end,
312     .decode         = decode_frame,
313     .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
314 };