annotate binkaudio.c @ 11303:b0cb1f7efcbc libavcodec

Do not read beyond end of input in EA-TGV. This should avoid FATE test #362 result depending on uninitialized data. FATE result may change for this test.
author vitor
date Fri, 26 Feb 2010 18:12:27 +0000
parents bfffb50b80bc
children 15dd07e86519
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
1 /*
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
2 * Bink Audio decoder
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
3 * Copyright (c) 2007-2010 Peter Ross (pross@xvid.org)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
4 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
5 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
6 * This file is part of FFmpeg.
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
7 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
12 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
16 * Lesser General Public License for more details.
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
17 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
21 */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
22
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
23 /**
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
24 * @file libavcodec/binkaudio.c
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
25 * Bink Audio decoder
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
26 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
27 * Technical details here:
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
28 * http://wiki.multimedia.cx/index.php?title=Bink_Audio
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
29 */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
30
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
31 #include "avcodec.h"
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
32 #define ALT_BITSTREAM_READER_LE
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
33 #include "get_bits.h"
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
34 #include "dsputil.h"
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
35 extern const uint16_t ff_wma_critical_freqs[25];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
36
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
37 #define MAX_CHANNELS 2
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
38 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
39
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
40 typedef struct {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
41 AVCodecContext *avctx;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
42 GetBitContext gb;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
43 DSPContext dsp;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
44 int first;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
45 int channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
46 int frame_len; ///< transform size (samples)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
47 int overlap_len; ///< overlap size (samples)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
48 int block_size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
49 int num_bands;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
50 unsigned int *bands;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
51 float root;
11069
28f00789adcd Update binkaudio to new DECLARE_ALIGNED syntax
daniel
parents: 11067
diff changeset
52 DECLARE_ALIGNED_16(FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
28f00789adcd Update binkaudio to new DECLARE_ALIGNED syntax
daniel
parents: 11067
diff changeset
53 DECLARE_ALIGNED_16(short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
54 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
55 union {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
56 RDFTContext rdft;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
57 DCTContext dct;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
58 } trans;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
59 } BinkAudioContext;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
60
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
61
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
62 static av_cold int decode_init(AVCodecContext *avctx)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
63 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
64 BinkAudioContext *s = avctx->priv_data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
65 int sample_rate = avctx->sample_rate;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
66 int sample_rate_half;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
67 int i;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
68 int frame_len_bits;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
69
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
70 s->avctx = avctx;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
71 dsputil_init(&s->dsp, avctx);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
72
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
73 /* determine frame length */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
74 if (avctx->sample_rate < 22050) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
75 frame_len_bits = 9;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
76 } else if (avctx->sample_rate < 44100) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
77 frame_len_bits = 10;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
78 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
79 frame_len_bits = 11;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
80 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
81 s->frame_len = 1 << frame_len_bits;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
82
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
83 if (s->channels > MAX_CHANNELS) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
84 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
85 return -1;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
86 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
87
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
88 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
89 // audio is already interleaved for the RDFT format variant
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
90 sample_rate *= avctx->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
91 s->frame_len *= avctx->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
92 s->channels = 1;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
93 if (avctx->channels == 2)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
94 frame_len_bits++;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
95 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
96 s->channels = avctx->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
97 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
98
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
99 s->overlap_len = s->frame_len / 16;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
100 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
101 sample_rate_half = (sample_rate + 1) / 2;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
102 s->root = 2.0 / sqrt(s->frame_len);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
103
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
104 /* calculate number of bands */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
105 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
106 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
107 break;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
108
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
109 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
110 if (!s->bands)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
111 return AVERROR(ENOMEM);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
112
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
113 /* populate bands data */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
114 s->bands[0] = 1;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
115 for (i = 1; i < s->num_bands; i++)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
116 s->bands[i] = ff_wma_critical_freqs[i - 1] * (s->frame_len / 2) / sample_rate_half;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
117 s->bands[s->num_bands] = s->frame_len / 2;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
118
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
119 s->first = 1;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
120 avctx->sample_fmt = SAMPLE_FMT_S16;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
121
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
122 for (i = 0; i < s->channels; i++)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
123 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
124
11216
359c8ba0698e Fix compilation of binkaudio_rdft when dct is disabled
daniel
parents: 11069
diff changeset
125 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
126 ff_rdft_init(&s->trans.rdft, frame_len_bits, IRIDFT);
11216
359c8ba0698e Fix compilation of binkaudio_rdft when dct is disabled
daniel
parents: 11069
diff changeset
127 else if (CONFIG_BINKAUDIO_DCT_DECODER)
359c8ba0698e Fix compilation of binkaudio_rdft when dct is disabled
daniel
parents: 11069
diff changeset
128 ff_dct_init(&s->trans.dct, frame_len_bits, 0);
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
129 else
11216
359c8ba0698e Fix compilation of binkaudio_rdft when dct is disabled
daniel
parents: 11069
diff changeset
130 return -1;
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
131
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
132 return 0;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
133 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
134
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
135 static float get_float(GetBitContext *gb)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
136 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
137 int power = get_bits(gb, 5);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
138 float f = ldexpf(get_bits_long(gb, 23), power - 23);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
139 if (get_bits1(gb))
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
140 f = -f;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
141 return f;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
142 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
143
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
144 static const uint8_t rle_length_tab[16] = {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
145 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
146 };
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
147
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
148 /**
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
149 * Decode Bink Audio block
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
150 * @param[out] out Output buffer (must contain s->block_size elements)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
151 */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
152 static void decode_block(BinkAudioContext *s, short *out, int use_dct)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
153 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
154 int ch, i, j, k;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
155 float q, quant[25];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
156 int width, coeff;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
157 GetBitContext *gb = &s->gb;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
158
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
159 if (use_dct)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
160 skip_bits(gb, 2);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
161
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
162 for (ch = 0; ch < s->channels; ch++) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
163 FFTSample *coeffs = s->coeffs_ptr[ch];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
164 q = 0.0f;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
165 coeffs[0] = get_float(gb) * s->root;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
166 coeffs[1] = get_float(gb) * s->root;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
167
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
168 for (i = 0; i < s->num_bands; i++) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
169 /* constant is result of 0.066399999/log10(M_E) */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
170 int value = get_bits(gb, 8);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
171 quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
172 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
173
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
174 // find band (k)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
175 for (k = 0; s->bands[k] < 1; k++) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
176 q = quant[k];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
177 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
178
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
179 // parse coefficients
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
180 i = 2;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
181 while (i < s->frame_len) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
182 if (get_bits1(gb)) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
183 j = i + rle_length_tab[get_bits(gb, 4)] * 8;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
184 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
185 j = i + 8;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
186 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
187
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
188 j = FFMIN(j, s->frame_len);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
189
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
190 width = get_bits(gb, 4);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
191 if (width == 0) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
192 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
193 i = j;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
194 while (s->bands[k] * 2 < i)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
195 q = quant[k++];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
196 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
197 while (i < j) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
198 if (s->bands[k] * 2 == i)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
199 q = quant[k++];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
200 coeff = get_bits(gb, width);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
201 if (coeff) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
202 if (get_bits1(gb))
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
203 coeffs[i] = -q * coeff;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
204 else
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
205 coeffs[i] = q * coeff;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
206 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
207 coeffs[i] = 0.0f;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
208 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
209 i++;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
210 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
211 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
212 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
213
11216
359c8ba0698e Fix compilation of binkaudio_rdft when dct is disabled
daniel
parents: 11069
diff changeset
214 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct)
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
215 ff_dct_calc (&s->trans.dct, coeffs);
11216
359c8ba0698e Fix compilation of binkaudio_rdft when dct is disabled
daniel
parents: 11069
diff changeset
216 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
217 ff_rdft_calc(&s->trans.rdft, coeffs);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
218 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
219
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
220 s->dsp.float_to_int16_interleave(out, (const float **)s->coeffs_ptr, s->frame_len, s->channels);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
221
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
222 if (!s->first) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
223 int count = s->overlap_len * s->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
224 int shift = av_log2(count);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
225 for (i = 0; i < count; i++) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
226 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
227 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
228 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
229
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
230 memcpy(s->previous, out + s->block_size,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
231 s->overlap_len * s->channels * sizeof(*out));
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
232
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
233 s->first = 0;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
234 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
235
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
236 static av_cold int decode_end(AVCodecContext *avctx)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
237 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
238 BinkAudioContext * s = avctx->priv_data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
239 av_freep(&s->bands);
11216
359c8ba0698e Fix compilation of binkaudio_rdft when dct is disabled
daniel
parents: 11069
diff changeset
240 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
241 ff_rdft_end(&s->trans.rdft);
11216
359c8ba0698e Fix compilation of binkaudio_rdft when dct is disabled
daniel
parents: 11069
diff changeset
242 else if (CONFIG_BINKAUDIO_DCT_DECODER)
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
243 ff_dct_end(&s->trans.dct);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
244 return 0;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
245 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
246
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
247 static void get_bits_align32(GetBitContext *s)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
248 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
249 int n = (-get_bits_count(s)) & 31;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
250 if (n) skip_bits(s, n);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
251 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
252
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
253 static int decode_frame(AVCodecContext *avctx,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
254 void *data, int *data_size,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
255 AVPacket *avpkt)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
256 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
257 BinkAudioContext *s = avctx->priv_data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
258 const uint8_t *buf = avpkt->data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
259 int buf_size = avpkt->size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
260 short *samples = data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
261 short *samples_end = (short*)((uint8_t*)data + *data_size);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
262 int reported_size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
263 GetBitContext *gb = &s->gb;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
264
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
265 init_get_bits(gb, buf, buf_size * 8);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
266
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
267 reported_size = get_bits_long(gb, 32);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
268 while (get_bits_count(gb) / 8 < buf_size &&
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
269 samples + s->block_size <= samples_end) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
270 decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
271 samples += s->block_size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
272 get_bits_align32(gb);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
273 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
274
11264
bfffb50b80bc Use reported_size to truncate final Bink Audio frame
pross
parents: 11216
diff changeset
275 *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
276 return buf_size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
277 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
278
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
279 AVCodec binkaudio_rdft_decoder = {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
280 "binkaudio_rdft",
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
281 CODEC_TYPE_AUDIO,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
282 CODEC_ID_BINKAUDIO_RDFT,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
283 sizeof(BinkAudioContext),
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
284 decode_init,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
285 NULL,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
286 decode_end,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
287 decode_frame,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
288 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
289 };
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
290
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
291 AVCodec binkaudio_dct_decoder = {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
292 "binkaudio_dct",
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
293 CODEC_TYPE_AUDIO,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
294 CODEC_ID_BINKAUDIO_DCT,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
295 sizeof(BinkAudioContext),
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
296 decode_init,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
297 NULL,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
298 decode_end,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
299 decode_frame,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
300 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
301 };