3 * Copyright (c) 2007-2011 Peter Ross (pross@xvid.org)
4 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
6 * This file is part of FFmpeg.
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.
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.
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
27 * Technical details here:
28 * http://wiki.multimedia.cx/index.php?title=Bink_Audio
31 #include "libavutil/channel_layout.h"
33 #define BITSTREAM_READER_LE
37 #include "fmtconvert.h"
39 #include "wma_freqs.h"
40 #include "libavutil/intfloat.h"
42 static float quant_table
[96];
44 #define MAX_CHANNELS 2
45 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
49 int version_b
; ///< Bink version 'b'
52 int frame_len
; ///< transform size (samples)
53 int overlap_len
; ///< overlap size (samples)
58 DECLARE_ALIGNED(32, FFTSample
, coeffs
)[BINK_BLOCK_MAX_SIZE
];
59 float previous
[MAX_CHANNELS
][BINK_BLOCK_MAX_SIZE
/ 16]; ///< coeffs from previous audio block
60 uint8_t *packet_buffer
;
68 static av_cold
int decode_init(AVCodecContext
*avctx
)
70 BinkAudioContext
*s
= avctx
->priv_data
;
71 int sample_rate
= avctx
->sample_rate
;
76 /* determine frame length */
77 if (avctx
->sample_rate
< 22050) {
79 } else if (avctx
->sample_rate
< 44100) {
85 if (avctx
->channels
< 1 || avctx
->channels
> MAX_CHANNELS
) {
86 av_log(avctx
, AV_LOG_ERROR
, "invalid number of channels: %d\n", avctx
->channels
);
87 return AVERROR_INVALIDDATA
;
89 avctx
->channel_layout
= avctx
->channels
== 1 ? AV_CH_LAYOUT_MONO
:
92 s
->version_b
= avctx
->extradata_size
>= 4 && avctx
->extradata
[3] == 'b';
94 if (avctx
->codec
->id
== AV_CODEC_ID_BINKAUDIO_RDFT
) {
95 // audio is already interleaved for the RDFT format variant
96 avctx
->sample_fmt
= AV_SAMPLE_FMT_FLT
;
97 sample_rate
*= avctx
->channels
;
100 frame_len_bits
+= av_log2(avctx
->channels
);
102 s
->channels
= avctx
->channels
;
103 avctx
->sample_fmt
= AV_SAMPLE_FMT_FLTP
;
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 if (avctx
->codec
->id
== AV_CODEC_ID_BINKAUDIO_RDFT
)
111 s
->root
= 2.0 / (sqrt(s
->frame_len
) * 32768.0);
113 s
->root
= s
->frame_len
/ (sqrt(s
->frame_len
) * 32768.0);
114 for (i
= 0; i
< 96; i
++) {
115 /* constant is result of 0.066399999/log10(M_E) */
116 quant_table
[i
] = expf(i
* 0.15289164787221953823f
) * s
->root
;
119 /* calculate number of bands */
120 for (s
->num_bands
= 1; s
->num_bands
< 25; s
->num_bands
++)
121 if (sample_rate_half
<= ff_wma_critical_freqs
[s
->num_bands
- 1])
124 s
->bands
= av_malloc((s
->num_bands
+ 1) * sizeof(*s
->bands
));
126 return AVERROR(ENOMEM
);
128 /* populate bands data */
130 for (i
= 1; i
< s
->num_bands
; i
++)
131 s
->bands
[i
] = (ff_wma_critical_freqs
[i
- 1] * s
->frame_len
/ sample_rate_half
) & ~1;
132 s
->bands
[s
->num_bands
] = s
->frame_len
;
136 if (CONFIG_BINKAUDIO_RDFT_DECODER
&& avctx
->codec
->id
== AV_CODEC_ID_BINKAUDIO_RDFT
)
137 ff_rdft_init(&s
->trans
.rdft
, frame_len_bits
, DFT_C2R
);
138 else if (CONFIG_BINKAUDIO_DCT_DECODER
)
139 ff_dct_init(&s
->trans
.dct
, frame_len_bits
, DCT_III
);
146 static float get_float(GetBitContext
*gb
)
148 int power
= get_bits(gb
, 5);
149 float f
= ldexpf(get_bits_long(gb
, 23), power
- 23);
155 static const uint8_t rle_length_tab
[16] = {
156 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
160 * Decode Bink Audio block
161 * @param[out] out Output buffer (must contain s->block_size elements)
162 * @return 0 on success, negative error code on failure
164 static int decode_block(BinkAudioContext
*s
, float **out
, int use_dct
)
169 GetBitContext
*gb
= &s
->gb
;
174 for (ch
= 0; ch
< s
->channels
; ch
++) {
175 FFTSample
*coeffs
= out
[ch
];
178 if (get_bits_left(gb
) < 64)
179 return AVERROR_INVALIDDATA
;
180 coeffs
[0] = av_int2float(get_bits_long(gb
, 32)) * s
->root
;
181 coeffs
[1] = av_int2float(get_bits_long(gb
, 32)) * s
->root
;
183 if (get_bits_left(gb
) < 58)
184 return AVERROR_INVALIDDATA
;
185 coeffs
[0] = get_float(gb
) * s
->root
;
186 coeffs
[1] = get_float(gb
) * s
->root
;
189 if (get_bits_left(gb
) < s
->num_bands
* 8)
190 return AVERROR_INVALIDDATA
;
191 for (i
= 0; i
< s
->num_bands
; i
++) {
192 int value
= get_bits(gb
, 8);
193 quant
[i
] = quant_table
[FFMIN(value
, 95)];
199 // parse coefficients
201 while (i
< s
->frame_len
) {
205 int v
= get_bits1(gb
);
208 j
= i
+ rle_length_tab
[v
] * 8;
214 j
= FFMIN(j
, s
->frame_len
);
216 width
= get_bits(gb
, 4);
218 memset(coeffs
+ i
, 0, (j
- i
) * sizeof(*coeffs
));
220 while (s
->bands
[k
] < i
)
224 if (s
->bands
[k
] == i
)
226 coeff
= get_bits(gb
, width
);
231 coeffs
[i
] = -q
* coeff
;
233 coeffs
[i
] = q
* coeff
;
242 if (CONFIG_BINKAUDIO_DCT_DECODER
&& use_dct
) {
244 s
->trans
.dct
.dct_calc(&s
->trans
.dct
, coeffs
);
246 else if (CONFIG_BINKAUDIO_RDFT_DECODER
)
247 s
->trans
.rdft
.rdft_calc(&s
->trans
.rdft
, coeffs
);
250 for (ch
= 0; ch
< s
->channels
; ch
++) {
252 int count
= s
->overlap_len
* s
->channels
;
255 for (i
= 0; i
< s
->overlap_len
; i
++, j
+= s
->channels
)
256 out
[ch
][i
] = (s
->previous
[ch
][i
] * (count
- j
) +
257 out
[ch
][i
] * j
) / count
;
259 memcpy(s
->previous
[ch
], &out
[ch
][s
->frame_len
- s
->overlap_len
],
260 s
->overlap_len
* sizeof(*s
->previous
[ch
]));
268 static av_cold
int decode_end(AVCodecContext
*avctx
)
270 BinkAudioContext
* s
= avctx
->priv_data
;
272 av_freep(&s
->packet_buffer
);
273 if (CONFIG_BINKAUDIO_RDFT_DECODER
&& avctx
->codec
->id
== AV_CODEC_ID_BINKAUDIO_RDFT
)
274 ff_rdft_end(&s
->trans
.rdft
);
275 else if (CONFIG_BINKAUDIO_DCT_DECODER
)
276 ff_dct_end(&s
->trans
.dct
);
281 static void get_bits_align32(GetBitContext
*s
)
283 int n
= (-get_bits_count(s
)) & 31;
284 if (n
) skip_bits(s
, n
);
287 static int decode_frame(AVCodecContext
*avctx
, void *data
,
288 int *got_frame_ptr
, AVPacket
*avpkt
)
290 BinkAudioContext
*s
= avctx
->priv_data
;
291 AVFrame
*frame
= data
;
292 GetBitContext
*gb
= &s
->gb
;
293 int ret
, consumed
= 0;
295 if (!get_bits_left(gb
)) {
297 /* handle end-of-stream */
302 if (avpkt
->size
< 4) {
303 av_log(avctx
, AV_LOG_ERROR
, "Packet is too small\n");
304 return AVERROR_INVALIDDATA
;
306 buf
= av_realloc(s
->packet_buffer
, avpkt
->size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
308 return AVERROR(ENOMEM
);
309 memset(buf
+ avpkt
->size
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
310 s
->packet_buffer
= buf
;
311 memcpy(s
->packet_buffer
, avpkt
->data
, avpkt
->size
);
312 if ((ret
= init_get_bits8(gb
, s
->packet_buffer
, avpkt
->size
)) < 0)
314 consumed
= avpkt
->size
;
316 /* skip reported size */
317 skip_bits_long(gb
, 32);
320 /* get output buffer */
321 frame
->nb_samples
= s
->frame_len
;
322 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
325 if (decode_block(s
, (float **)frame
->extended_data
,
326 avctx
->codec
->id
== AV_CODEC_ID_BINKAUDIO_DCT
)) {
327 av_log(avctx
, AV_LOG_ERROR
, "Incomplete packet\n");
328 return AVERROR_INVALIDDATA
;
330 get_bits_align32(gb
);
332 frame
->nb_samples
= s
->block_size
/ avctx
->channels
;
338 AVCodec ff_binkaudio_rdft_decoder
= {
339 .name
= "binkaudio_rdft",
340 .long_name
= NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
341 .type
= AVMEDIA_TYPE_AUDIO
,
342 .id
= AV_CODEC_ID_BINKAUDIO_RDFT
,
343 .priv_data_size
= sizeof(BinkAudioContext
),
346 .decode
= decode_frame
,
347 .capabilities
= CODEC_CAP_DELAY
| CODEC_CAP_DR1
,
350 AVCodec ff_binkaudio_dct_decoder
= {
351 .name
= "binkaudio_dct",
352 .long_name
= NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
353 .type
= AVMEDIA_TYPE_AUDIO
,
354 .id
= AV_CODEC_ID_BINKAUDIO_DCT
,
355 .priv_data_size
= sizeof(BinkAudioContext
),
358 .decode
= decode_frame
,
359 .capabilities
= CODEC_CAP_DELAY
| CODEC_CAP_DR1
,