Statistics
| Branch: | Revision:

ffmpeg / libavcodec / binkaudio.c @ 8d09fc19

History | View | Annotate | Download (8.8 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
    GetBitContext gb;
45
    DSPContext dsp;
46
    FmtConvertContext fmt_conv;
47
    int first;
48
    int channels;
49
    int frame_len;          ///< transform size (samples)
50
    int overlap_len;        ///< overlap size (samples)
51
    int block_size;
52
    int num_bands;
53
    unsigned int *bands;
54
    float root;
55
    DECLARE_ALIGNED(16, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
56
    DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16];  ///< coeffs from previous audio block
57
    float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
58
    union {
59
        RDFTContext rdft;
60
        DCTContext dct;
61
    } trans;
62
} BinkAudioContext;
63

    
64

    
65
static av_cold int decode_init(AVCodecContext *avctx)
66
{
67
    BinkAudioContext *s = avctx->priv_data;
68
    int sample_rate = avctx->sample_rate;
69
    int sample_rate_half;
70
    int i;
71
    int frame_len_bits;
72

    
73
    dsputil_init(&s->dsp, avctx);
74
    ff_fmt_convert_init(&s->fmt_conv, avctx);
75

    
76
    /* determine frame length */
77
    if (avctx->sample_rate < 22050) {
78
        frame_len_bits = 9;
79
    } else if (avctx->sample_rate < 44100) {
80
        frame_len_bits = 10;
81
    } else {
82
        frame_len_bits = 11;
83
    }
84

    
85
    if (avctx->channels > MAX_CHANNELS) {
86
        av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
87
        return -1;
88
    }
89

    
90
    if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
91
        // audio is already interleaved for the RDFT format variant
92
        sample_rate  *= avctx->channels;
93
        s->channels = 1;
94
        frame_len_bits += av_log2(avctx->channels);
95
    } else {
96
        s->channels = avctx->channels;
97
    }
98

    
99
    s->frame_len     = 1 << frame_len_bits;
100
    s->overlap_len   = s->frame_len / 16;
101
    s->block_size    = (s->frame_len - s->overlap_len) * s->channels;
102
    sample_rate_half = (sample_rate + 1) / 2;
103
    s->root          = 2.0 / sqrt(s->frame_len);
104

    
105
    /* calculate number of bands */
106
    for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
107
        if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
108
            break;
109

    
110
    s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
111
    if (!s->bands)
112
        return AVERROR(ENOMEM);
113

    
114
    /* populate bands data */
115
    s->bands[0] = 2;
116
    for (i = 1; i < s->num_bands; i++)
117
        s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
118
    s->bands[s->num_bands] = s->frame_len;
119

    
120
    s->first = 1;
121
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
122

    
123
    for (i = 0; i < s->channels; i++)
124
        s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
125

    
126
    if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
127
        ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
128
    else if (CONFIG_BINKAUDIO_DCT_DECODER)
129
        ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
130
    else
131
        return -1;
132

    
133
    return 0;
134
}
135

    
136
static float get_float(GetBitContext *gb)
137
{
138
    int power = get_bits(gb, 5);
139
    float f = ldexpf(get_bits_long(gb, 23), power - 23);
140
    if (get_bits1(gb))
141
        f = -f;
142
    return f;
143
}
144

    
145
static const uint8_t rle_length_tab[16] = {
146
    2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
147
};
148

    
149
/**
150
 * Decode Bink Audio block
151
 * @param[out] out Output buffer (must contain s->block_size elements)
152
 */
153
static void decode_block(BinkAudioContext *s, short *out, int use_dct)
154
{
155
    int ch, i, j, k;
156
    float q, quant[25];
157
    int width, coeff;
158
    GetBitContext *gb = &s->gb;
159

    
160
    if (use_dct)
161
        skip_bits(gb, 2);
162

    
163
    for (ch = 0; ch < s->channels; ch++) {
164
        FFTSample *coeffs = s->coeffs_ptr[ch];
165
        coeffs[0] = get_float(gb) * s->root;
166
        coeffs[1] = get_float(gb) * s->root;
167

    
168
        for (i = 0; i < s->num_bands; i++) {
169
            /* constant is result of 0.066399999/log10(M_E) */
170
            int value = get_bits(gb, 8);
171
            quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
172
        }
173

    
174
        k = 0;
175
        q = quant[0];
176

    
177
        // parse coefficients
178
        i = 2;
179
        while (i < s->frame_len) {
180
            if (get_bits1(gb)) {
181
                j = i + rle_length_tab[get_bits(gb, 4)] * 8;
182
            } else {
183
                j = i + 8;
184
            }
185

    
186
            j = FFMIN(j, s->frame_len);
187

    
188
            width = get_bits(gb, 4);
189
            if (width == 0) {
190
                memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
191
                i = j;
192
                while (s->bands[k] < i)
193
                    q = quant[k++];
194
            } else {
195
                while (i < j) {
196
                    if (s->bands[k] == i)
197
                        q = quant[k++];
198
                    coeff = get_bits(gb, width);
199
                    if (coeff) {
200
                        if (get_bits1(gb))
201
                            coeffs[i] = -q * coeff;
202
                        else
203
                            coeffs[i] =  q * coeff;
204
                    } else {
205
                        coeffs[i] = 0.0f;
206
                    }
207
                    i++;
208
                }
209
            }
210
        }
211

    
212
        if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
213
            coeffs[0] /= 0.5;
214
            ff_dct_calc (&s->trans.dct,  coeffs);
215
            s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
216
        }
217
        else if (CONFIG_BINKAUDIO_RDFT_DECODER)
218
            ff_rdft_calc(&s->trans.rdft, coeffs);
219
    }
220

    
221
    s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
222
                                          s->frame_len, s->channels);
223

    
224
    if (!s->first) {
225
        int count = s->overlap_len * s->channels;
226
        int shift = av_log2(count);
227
        for (i = 0; i < count; i++) {
228
            out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
229
        }
230
    }
231

    
232
    memcpy(s->previous, out + s->block_size,
233
           s->overlap_len * s->channels * sizeof(*out));
234

    
235
    s->first = 0;
236
}
237

    
238
static av_cold int decode_end(AVCodecContext *avctx)
239
{
240
    BinkAudioContext * s = avctx->priv_data;
241
    av_freep(&s->bands);
242
    if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
243
        ff_rdft_end(&s->trans.rdft);
244
    else if (CONFIG_BINKAUDIO_DCT_DECODER)
245
        ff_dct_end(&s->trans.dct);
246
    return 0;
247
}
248

    
249
static void get_bits_align32(GetBitContext *s)
250
{
251
    int n = (-get_bits_count(s)) & 31;
252
    if (n) skip_bits(s, n);
253
}
254

    
255
static int decode_frame(AVCodecContext *avctx,
256
                        void *data, int *data_size,
257
                        AVPacket *avpkt)
258
{
259
    BinkAudioContext *s = avctx->priv_data;
260
    const uint8_t *buf  = avpkt->data;
261
    int buf_size        = avpkt->size;
262
    short *samples      = data;
263
    short *samples_end  = (short*)((uint8_t*)data + *data_size);
264
    int reported_size;
265
    GetBitContext *gb = &s->gb;
266

    
267
    init_get_bits(gb, buf, buf_size * 8);
268

    
269
    reported_size = get_bits_long(gb, 32);
270
    while (get_bits_count(gb) / 8 < buf_size &&
271
           samples + s->block_size <= samples_end) {
272
        decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
273
        samples += s->block_size;
274
        get_bits_align32(gb);
275
    }
276

    
277
    *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
278
    return buf_size;
279
}
280

    
281
AVCodec ff_binkaudio_rdft_decoder = {
282
    "binkaudio_rdft",
283
    AVMEDIA_TYPE_AUDIO,
284
    CODEC_ID_BINKAUDIO_RDFT,
285
    sizeof(BinkAudioContext),
286
    decode_init,
287
    NULL,
288
    decode_end,
289
    decode_frame,
290
    .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
291
};
292

    
293
AVCodec ff_binkaudio_dct_decoder = {
294
    "binkaudio_dct",
295
    AVMEDIA_TYPE_AUDIO,
296
    CODEC_ID_BINKAUDIO_DCT,
297
    sizeof(BinkAudioContext),
298
    decode_init,
299
    NULL,
300
    decode_end,
301
    decode_frame,
302
    .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
303
};