/src/ffmpeg/libavcodec/wmadec.c
Line | Count | Source |
1 | | /* |
2 | | * WMA compatible decoder |
3 | | * Copyright (c) 2002 The FFmpeg Project |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | /** |
23 | | * @file |
24 | | * WMA compatible decoder. |
25 | | * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2. |
26 | | * WMA v1 is identified by audio format 0x160 in Microsoft media files |
27 | | * (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161. |
28 | | * |
29 | | * To use this decoder, a calling application must supply the extra data |
30 | | * bytes provided with the WMA data. These are the extra, codec-specific |
31 | | * bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes |
32 | | * to the decoder using the extradata[_size] fields in AVCodecContext. There |
33 | | * should be 4 extra bytes for v1 data and 6 extra bytes for v2 data. |
34 | | */ |
35 | | |
36 | | #include "config_components.h" |
37 | | |
38 | | #include "libavutil/attributes.h" |
39 | | #include "libavutil/ffmath.h" |
40 | | |
41 | | #include "avcodec.h" |
42 | | #include "codec_internal.h" |
43 | | #include "decode.h" |
44 | | #include "internal.h" |
45 | | #include "wma.h" |
46 | | |
47 | 648k | #define EXPVLCBITS 8 |
48 | 216k | #define EXPMAX ((19 + EXPVLCBITS - 1) / EXPVLCBITS) |
49 | | |
50 | 162k | #define HGAINVLCBITS 9 |
51 | 53.5k | #define HGAINMAX ((13 + HGAINVLCBITS - 1) / HGAINVLCBITS) |
52 | | |
53 | | static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len); |
54 | | |
55 | | #ifdef TRACE |
56 | | static void dump_floats(WMACodecContext *s, const char *name, |
57 | | int prec, const float *tab, int n) |
58 | | { |
59 | | int i; |
60 | | |
61 | | ff_tlog(s->avctx, "%s[%d]:\n", name, n); |
62 | | for (i = 0; i < n; i++) { |
63 | | if ((i & 7) == 0) |
64 | | ff_tlog(s->avctx, "%4d: ", i); |
65 | | ff_tlog(s->avctx, " %8.*f", prec, tab[i]); |
66 | | if ((i & 7) == 7) |
67 | | ff_tlog(s->avctx, "\n"); |
68 | | } |
69 | | if ((i & 7) != 0) |
70 | | ff_tlog(s->avctx, "\n"); |
71 | | } |
72 | | #endif /* TRACE */ |
73 | | |
74 | | static av_cold int wma_decode_init(AVCodecContext *avctx) |
75 | 2.88k | { |
76 | 2.88k | WMACodecContext *s = avctx->priv_data; |
77 | 2.88k | int i, flags2, ret; |
78 | 2.88k | uint8_t *extradata; |
79 | | |
80 | 2.88k | if (!avctx->block_align) { |
81 | 19 | av_log(avctx, AV_LOG_ERROR, "block_align is not set\n"); |
82 | 19 | return AVERROR(EINVAL); |
83 | 19 | } |
84 | | |
85 | 2.86k | s->avctx = avctx; |
86 | | |
87 | | /* extract flag info */ |
88 | 2.86k | flags2 = 0; |
89 | 2.86k | extradata = avctx->extradata; |
90 | 2.86k | if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 4) |
91 | 768 | flags2 = AV_RL16(extradata + 2); |
92 | 2.09k | else if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 6) |
93 | 701 | flags2 = AV_RL16(extradata + 4); |
94 | | |
95 | 2.86k | s->use_exp_vlc = flags2 & 0x0001; |
96 | 2.86k | s->use_bit_reservoir = flags2 & 0x0002; |
97 | 2.86k | s->use_variable_block_len = flags2 & 0x0004; |
98 | | |
99 | 2.86k | if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 8){ |
100 | 627 | if (AV_RL16(extradata+4)==0xd && s->use_variable_block_len){ |
101 | 2 | av_log(avctx, AV_LOG_WARNING, "Disabling use_variable_block_len, if this fails contact the ffmpeg developers and send us the file\n"); |
102 | 2 | s->use_variable_block_len= 0; // this fixes issue1503 |
103 | 2 | } |
104 | 627 | } |
105 | | |
106 | 8.59k | for (i=0; i<MAX_CHANNELS; i++) |
107 | 5.73k | s->max_exponent[i] = 1.0; |
108 | | |
109 | 2.86k | if ((ret = ff_wma_init(avctx, flags2)) < 0) |
110 | 544 | return ret; |
111 | | |
112 | | /* init MDCT */ |
113 | 6.60k | for (i = 0; i < s->nb_block_sizes; i++) { |
114 | 4.28k | float scale = 1.0 / 32768.0; |
115 | 4.28k | ret = av_tx_init(&s->mdct_ctx[i], &s->mdct_fn[i], AV_TX_FLOAT_MDCT, |
116 | 4.28k | 1, 1 << (s->frame_len_bits - i), &scale, AV_TX_FULL_IMDCT); |
117 | 4.28k | if (ret < 0) |
118 | 0 | return ret; |
119 | 4.28k | } |
120 | | |
121 | 2.32k | if (s->use_noise_coding) { |
122 | 2.09k | ret = ff_vlc_init_from_lengths(&s->hgain_vlc, HGAINVLCBITS, |
123 | 2.09k | FF_ARRAY_ELEMS(ff_wma_hgain_hufftab), |
124 | 2.09k | &ff_wma_hgain_hufftab[0][1], 2, |
125 | 2.09k | &ff_wma_hgain_hufftab[0][0], 2, 1, |
126 | 2.09k | -18, 0, avctx); |
127 | 2.09k | if (ret < 0) |
128 | 0 | return ret; |
129 | 2.09k | } |
130 | | |
131 | 2.32k | if (s->use_exp_vlc) { |
132 | | // FIXME move out of context |
133 | 811 | ret = vlc_init(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), |
134 | 811 | ff_aac_scalefactor_bits, 1, 1, |
135 | 811 | ff_aac_scalefactor_code, 4, 4, 0); |
136 | 811 | if (ret < 0) |
137 | 0 | return ret; |
138 | 811 | } else |
139 | 1.51k | wma_lsp_to_curve_init(s, s->frame_len); |
140 | | |
141 | 2.32k | avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; |
142 | | |
143 | 2.32k | avctx->internal->skip_samples = s->frame_len * 2; |
144 | | |
145 | 2.32k | return 0; |
146 | 2.32k | } |
147 | | |
148 | | /** |
149 | | * compute x^-0.25 with an exponent and mantissa table. We use linear |
150 | | * interpolation to reduce the mantissa table size at a small speed |
151 | | * expense (linear interpolation approximately doubles the number of |
152 | | * bits of precision). |
153 | | */ |
154 | | static inline float pow_m1_4(WMACodecContext *s, float x) |
155 | 317M | { |
156 | 317M | union { |
157 | 317M | float f; |
158 | 317M | unsigned int v; |
159 | 317M | } u, t; |
160 | 317M | unsigned int e, m; |
161 | 317M | float a, b; |
162 | | |
163 | 317M | u.f = x; |
164 | 317M | e = u.v >> 23; |
165 | 317M | m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1); |
166 | | /* build interpolation scale: 1 <= t < 2. */ |
167 | 317M | t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23); |
168 | 317M | a = s->lsp_pow_m_table1[m]; |
169 | 317M | b = s->lsp_pow_m_table2[m]; |
170 | 317M | return s->lsp_pow_e_table[e] * (a + b * t.f); |
171 | 317M | } |
172 | | |
173 | | static av_cold void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len) |
174 | 1.51k | { |
175 | 1.51k | float wdel, a, b; |
176 | 1.51k | int i, e, m; |
177 | | |
178 | 1.51k | wdel = M_PI / frame_len; |
179 | 1.50M | for (i = 0; i < frame_len; i++) |
180 | 1.50M | s->lsp_cos_table[i] = 2.0f * cos(wdel * i); |
181 | | |
182 | | /* tables for x^-0.25 computation */ |
183 | 388k | for (i = 0; i < 256; i++) { |
184 | 386k | e = i - 126; |
185 | 386k | s->lsp_pow_e_table[i] = exp2f(e * -0.25); |
186 | 386k | } |
187 | | |
188 | | /* NOTE: these two tables are needed to avoid two operations in |
189 | | * pow_m1_4 */ |
190 | 1.51k | b = 1.0; |
191 | 194k | for (i = (1 << LSP_POW_BITS) - 1; i >= 0; i--) { |
192 | 193k | m = (1 << LSP_POW_BITS) + i; |
193 | 193k | a = (float) m * (0.5 / (1 << LSP_POW_BITS)); |
194 | 193k | a = 1/sqrt(sqrt(a)); |
195 | 193k | s->lsp_pow_m_table1[i] = 2 * a - b; |
196 | 193k | s->lsp_pow_m_table2[i] = b - a; |
197 | 193k | b = a; |
198 | 193k | } |
199 | 1.51k | } |
200 | | |
201 | | /** |
202 | | * NOTE: We use the same code as Vorbis here |
203 | | * @todo optimize it further with SSE/3Dnow |
204 | | */ |
205 | | static void wma_lsp_to_curve(WMACodecContext *s, float *out, float *val_max_ptr, |
206 | | int n, float *lsp) |
207 | 275k | { |
208 | 275k | int i, j; |
209 | 275k | float p, q, w, v, val_max; |
210 | | |
211 | 275k | val_max = 0; |
212 | 317M | for (i = 0; i < n; i++) { |
213 | 317M | p = 0.5f; |
214 | 317M | q = 0.5f; |
215 | 317M | w = s->lsp_cos_table[i]; |
216 | 1.90G | for (j = 1; j < NB_LSP_COEFS; j += 2) { |
217 | 1.58G | q *= w - lsp[j - 1]; |
218 | 1.58G | p *= w - lsp[j]; |
219 | 1.58G | } |
220 | 317M | p *= p * (2.0f - w); |
221 | 317M | q *= q * (2.0f + w); |
222 | 317M | v = p + q; |
223 | 317M | v = pow_m1_4(s, v); |
224 | 317M | if (v > val_max) |
225 | 10.7M | val_max = v; |
226 | 317M | out[i] = v; |
227 | 317M | } |
228 | 275k | *val_max_ptr = val_max; |
229 | 275k | } |
230 | | |
231 | | /** |
232 | | * decode exponents coded with LSP coefficients (same idea as Vorbis) |
233 | | */ |
234 | | static void decode_exp_lsp(WMACodecContext *s, int ch) |
235 | 275k | { |
236 | 275k | float lsp_coefs[NB_LSP_COEFS]; |
237 | 275k | int val, i; |
238 | | |
239 | 3.03M | for (i = 0; i < NB_LSP_COEFS; i++) { |
240 | 2.75M | if (i == 0 || i >= 8) |
241 | 826k | val = get_bits(&s->gb, 3); |
242 | 1.92M | else |
243 | 1.92M | val = get_bits(&s->gb, 4); |
244 | 2.75M | lsp_coefs[i] = ff_wma_lsp_codebook[i][val]; |
245 | 2.75M | } |
246 | | |
247 | 275k | wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch], |
248 | 275k | s->block_len, lsp_coefs); |
249 | 275k | } |
250 | | |
251 | | /** pow(10, i / 16.0) for i in -60..95 */ |
252 | | static const float pow_tab[] = { |
253 | | 1.7782794100389e-04, 2.0535250264571e-04, |
254 | | 2.3713737056617e-04, 2.7384196342644e-04, |
255 | | 3.1622776601684e-04, 3.6517412725484e-04, |
256 | | 4.2169650342858e-04, 4.8696752516586e-04, |
257 | | 5.6234132519035e-04, 6.4938163157621e-04, |
258 | | 7.4989420933246e-04, 8.6596432336006e-04, |
259 | | 1.0000000000000e-03, 1.1547819846895e-03, |
260 | | 1.3335214321633e-03, 1.5399265260595e-03, |
261 | | 1.7782794100389e-03, 2.0535250264571e-03, |
262 | | 2.3713737056617e-03, 2.7384196342644e-03, |
263 | | 3.1622776601684e-03, 3.6517412725484e-03, |
264 | | 4.2169650342858e-03, 4.8696752516586e-03, |
265 | | 5.6234132519035e-03, 6.4938163157621e-03, |
266 | | 7.4989420933246e-03, 8.6596432336006e-03, |
267 | | 1.0000000000000e-02, 1.1547819846895e-02, |
268 | | 1.3335214321633e-02, 1.5399265260595e-02, |
269 | | 1.7782794100389e-02, 2.0535250264571e-02, |
270 | | 2.3713737056617e-02, 2.7384196342644e-02, |
271 | | 3.1622776601684e-02, 3.6517412725484e-02, |
272 | | 4.2169650342858e-02, 4.8696752516586e-02, |
273 | | 5.6234132519035e-02, 6.4938163157621e-02, |
274 | | 7.4989420933246e-02, 8.6596432336007e-02, |
275 | | 1.0000000000000e-01, 1.1547819846895e-01, |
276 | | 1.3335214321633e-01, 1.5399265260595e-01, |
277 | | 1.7782794100389e-01, 2.0535250264571e-01, |
278 | | 2.3713737056617e-01, 2.7384196342644e-01, |
279 | | 3.1622776601684e-01, 3.6517412725484e-01, |
280 | | 4.2169650342858e-01, 4.8696752516586e-01, |
281 | | 5.6234132519035e-01, 6.4938163157621e-01, |
282 | | 7.4989420933246e-01, 8.6596432336007e-01, |
283 | | 1.0000000000000e+00, 1.1547819846895e+00, |
284 | | 1.3335214321633e+00, 1.5399265260595e+00, |
285 | | 1.7782794100389e+00, 2.0535250264571e+00, |
286 | | 2.3713737056617e+00, 2.7384196342644e+00, |
287 | | 3.1622776601684e+00, 3.6517412725484e+00, |
288 | | 4.2169650342858e+00, 4.8696752516586e+00, |
289 | | 5.6234132519035e+00, 6.4938163157621e+00, |
290 | | 7.4989420933246e+00, 8.6596432336007e+00, |
291 | | 1.0000000000000e+01, 1.1547819846895e+01, |
292 | | 1.3335214321633e+01, 1.5399265260595e+01, |
293 | | 1.7782794100389e+01, 2.0535250264571e+01, |
294 | | 2.3713737056617e+01, 2.7384196342644e+01, |
295 | | 3.1622776601684e+01, 3.6517412725484e+01, |
296 | | 4.2169650342858e+01, 4.8696752516586e+01, |
297 | | 5.6234132519035e+01, 6.4938163157621e+01, |
298 | | 7.4989420933246e+01, 8.6596432336007e+01, |
299 | | 1.0000000000000e+02, 1.1547819846895e+02, |
300 | | 1.3335214321633e+02, 1.5399265260595e+02, |
301 | | 1.7782794100389e+02, 2.0535250264571e+02, |
302 | | 2.3713737056617e+02, 2.7384196342644e+02, |
303 | | 3.1622776601684e+02, 3.6517412725484e+02, |
304 | | 4.2169650342858e+02, 4.8696752516586e+02, |
305 | | 5.6234132519035e+02, 6.4938163157621e+02, |
306 | | 7.4989420933246e+02, 8.6596432336007e+02, |
307 | | 1.0000000000000e+03, 1.1547819846895e+03, |
308 | | 1.3335214321633e+03, 1.5399265260595e+03, |
309 | | 1.7782794100389e+03, 2.0535250264571e+03, |
310 | | 2.3713737056617e+03, 2.7384196342644e+03, |
311 | | 3.1622776601684e+03, 3.6517412725484e+03, |
312 | | 4.2169650342858e+03, 4.8696752516586e+03, |
313 | | 5.6234132519035e+03, 6.4938163157621e+03, |
314 | | 7.4989420933246e+03, 8.6596432336007e+03, |
315 | | 1.0000000000000e+04, 1.1547819846895e+04, |
316 | | 1.3335214321633e+04, 1.5399265260595e+04, |
317 | | 1.7782794100389e+04, 2.0535250264571e+04, |
318 | | 2.3713737056617e+04, 2.7384196342644e+04, |
319 | | 3.1622776601684e+04, 3.6517412725484e+04, |
320 | | 4.2169650342858e+04, 4.8696752516586e+04, |
321 | | 5.6234132519035e+04, 6.4938163157621e+04, |
322 | | 7.4989420933246e+04, 8.6596432336007e+04, |
323 | | 1.0000000000000e+05, 1.1547819846895e+05, |
324 | | 1.3335214321633e+05, 1.5399265260595e+05, |
325 | | 1.7782794100389e+05, 2.0535250264571e+05, |
326 | | 2.3713737056617e+05, 2.7384196342644e+05, |
327 | | 3.1622776601684e+05, 3.6517412725484e+05, |
328 | | 4.2169650342858e+05, 4.8696752516586e+05, |
329 | | 5.6234132519035e+05, 6.4938163157621e+05, |
330 | | 7.4989420933246e+05, 8.6596432336007e+05, |
331 | | }; |
332 | | |
333 | | /** |
334 | | * decode exponents coded with VLC codes |
335 | | */ |
336 | | static int decode_exp_vlc(WMACodecContext *s, int ch) |
337 | 34.5k | { |
338 | 34.5k | int last_exp, n, code; |
339 | 34.5k | const uint16_t *ptr; |
340 | 34.5k | float v, max_scale; |
341 | 34.5k | uint32_t *q, *q_end, iv; |
342 | 34.5k | const float *ptab = pow_tab + 60; |
343 | 34.5k | const uint32_t *iptab = (const uint32_t *) ptab; |
344 | | |
345 | 34.5k | ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits]; |
346 | 34.5k | q = (uint32_t *) s->exponents[ch]; |
347 | 34.5k | q_end = q + s->block_len; |
348 | 34.5k | max_scale = 0; |
349 | 34.5k | if (s->version == 1) { |
350 | 5.65k | last_exp = get_bits(&s->gb, 5) + 10; |
351 | 5.65k | v = ptab[last_exp]; |
352 | 5.65k | iv = iptab[last_exp]; |
353 | 5.65k | max_scale = v; |
354 | 5.65k | n = *ptr++; |
355 | 154k | switch (n & 3) do { |
356 | 157k | case 0: *q++ = iv; |
357 | 157k | case 3: *q++ = iv; |
358 | 158k | case 2: *q++ = iv; |
359 | 160k | case 1: *q++ = iv; |
360 | 160k | } while ((n -= 4) > 0); |
361 | 5.65k | } else |
362 | 28.9k | last_exp = 36; |
363 | | |
364 | 249k | while (q < q_end) { |
365 | 216k | code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX); |
366 | | /* NOTE: this offset is the same as MPEG-4 AAC! */ |
367 | 216k | last_exp += code - 60; |
368 | 216k | if ((unsigned) last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) { |
369 | 1.15k | av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n", |
370 | 1.15k | last_exp); |
371 | 1.15k | return AVERROR_INVALIDDATA; |
372 | 1.15k | } |
373 | 215k | v = ptab[last_exp]; |
374 | 215k | iv = iptab[last_exp]; |
375 | 215k | if (v > max_scale) |
376 | 44.8k | max_scale = v; |
377 | 215k | n = *ptr++; |
378 | 4.50M | switch (n & 3) do { |
379 | 4.67M | case 0: *q++ = iv; |
380 | 4.69M | case 3: *q++ = iv; |
381 | 4.70M | case 2: *q++ = iv; |
382 | 4.72M | case 1: *q++ = iv; |
383 | 4.72M | } while ((n -= 4) > 0); |
384 | 215k | } |
385 | 33.4k | s->max_exponent[ch] = max_scale; |
386 | 33.4k | return 0; |
387 | 34.5k | } |
388 | | |
389 | | /** |
390 | | * Apply MDCT window and add into output. |
391 | | * |
392 | | * We ensure that when the windows overlap their squared sum |
393 | | * is always 1 (MDCT reconstruction rule). |
394 | | */ |
395 | | static void wma_window(WMACodecContext *s, float *out) |
396 | 1.92M | { |
397 | 1.92M | float *in = s->output; |
398 | 1.92M | int block_len, bsize, n; |
399 | | |
400 | | /* left part */ |
401 | 1.92M | if (s->block_len_bits <= s->prev_block_len_bits) { |
402 | 1.92M | block_len = s->block_len; |
403 | 1.92M | bsize = s->frame_len_bits - s->block_len_bits; |
404 | | |
405 | 1.92M | s->fdsp->vector_fmul_add(out, in, s->windows[bsize], |
406 | 1.92M | out, block_len); |
407 | 1.92M | } else { |
408 | 2.73k | block_len = 1 << s->prev_block_len_bits; |
409 | 2.73k | n = (s->block_len - block_len) / 2; |
410 | 2.73k | bsize = s->frame_len_bits - s->prev_block_len_bits; |
411 | | |
412 | 2.73k | s->fdsp->vector_fmul_add(out + n, in + n, s->windows[bsize], |
413 | 2.73k | out + n, block_len); |
414 | | |
415 | 2.73k | memcpy(out + n + block_len, in + n + block_len, n * sizeof(float)); |
416 | 2.73k | } |
417 | | |
418 | 1.92M | out += s->block_len; |
419 | 1.92M | in += s->block_len; |
420 | | |
421 | | /* right part */ |
422 | 1.92M | if (s->block_len_bits <= s->next_block_len_bits) { |
423 | 1.92M | block_len = s->block_len; |
424 | 1.92M | bsize = s->frame_len_bits - s->block_len_bits; |
425 | | |
426 | 1.92M | s->fdsp->vector_fmul_reverse(out, in, s->windows[bsize], block_len); |
427 | 1.92M | } else { |
428 | 3.55k | block_len = 1 << s->next_block_len_bits; |
429 | 3.55k | n = (s->block_len - block_len) / 2; |
430 | 3.55k | bsize = s->frame_len_bits - s->next_block_len_bits; |
431 | | |
432 | 3.55k | memcpy(out, in, n * sizeof(float)); |
433 | | |
434 | 3.55k | s->fdsp->vector_fmul_reverse(out + n, in + n, s->windows[bsize], |
435 | 3.55k | block_len); |
436 | | |
437 | 3.55k | memset(out + n + block_len, 0, n * sizeof(float)); |
438 | 3.55k | } |
439 | 1.92M | } |
440 | | |
441 | | /** |
442 | | * @return |
443 | | * 0 if OK. |
444 | | * 1 if last block of frame. |
445 | | * AVERROR if unrecoverable error. |
446 | | */ |
447 | | static int wma_decode_block(WMACodecContext *s) |
448 | 1.86M | { |
449 | 1.86M | int channels = s->avctx->ch_layout.nb_channels; |
450 | 1.86M | int n, v, a, ch, bsize; |
451 | 1.86M | int coef_nb_bits, total_gain; |
452 | 1.86M | int nb_coefs[MAX_CHANNELS]; |
453 | 1.86M | float mdct_norm; |
454 | 1.86M | AVTXContext *mdct; |
455 | 1.86M | av_tx_fn mdct_fn; |
456 | | |
457 | | #ifdef TRACE |
458 | | ff_tlog(s->avctx, "***decode_block: %d:%d\n", |
459 | | s->frame_count - 1, s->block_num); |
460 | | #endif /* TRACE */ |
461 | | |
462 | | /* compute current block length */ |
463 | 1.86M | if (s->use_variable_block_len) { |
464 | 24.5k | n = av_log2(s->nb_block_sizes - 1) + 1; |
465 | | |
466 | 24.5k | if (s->reset_block_lengths) { |
467 | 5.23k | s->reset_block_lengths = 0; |
468 | 5.23k | v = get_bits(&s->gb, n); |
469 | 5.23k | if (v >= s->nb_block_sizes) { |
470 | 674 | av_log(s->avctx, AV_LOG_ERROR, |
471 | 674 | "prev_block_len_bits %d out of range\n", |
472 | 674 | s->frame_len_bits - v); |
473 | 674 | return AVERROR_INVALIDDATA; |
474 | 674 | } |
475 | 4.56k | s->prev_block_len_bits = s->frame_len_bits - v; |
476 | 4.56k | v = get_bits(&s->gb, n); |
477 | 4.56k | if (v >= s->nb_block_sizes) { |
478 | 477 | av_log(s->avctx, AV_LOG_ERROR, |
479 | 477 | "block_len_bits %d out of range\n", |
480 | 477 | s->frame_len_bits - v); |
481 | 477 | return AVERROR_INVALIDDATA; |
482 | 477 | } |
483 | 4.08k | s->block_len_bits = s->frame_len_bits - v; |
484 | 19.3k | } else { |
485 | | /* update block lengths */ |
486 | 19.3k | s->prev_block_len_bits = s->block_len_bits; |
487 | 19.3k | s->block_len_bits = s->next_block_len_bits; |
488 | 19.3k | } |
489 | 23.4k | v = get_bits(&s->gb, n); |
490 | 23.4k | if (v >= s->nb_block_sizes) { |
491 | 1.39k | av_log(s->avctx, AV_LOG_ERROR, |
492 | 1.39k | "next_block_len_bits %d out of range\n", |
493 | 1.39k | s->frame_len_bits - v); |
494 | 1.39k | return AVERROR_INVALIDDATA; |
495 | 1.39k | } |
496 | 22.0k | s->next_block_len_bits = s->frame_len_bits - v; |
497 | 1.84M | } else { |
498 | | /* fixed block len */ |
499 | 1.84M | s->next_block_len_bits = s->frame_len_bits; |
500 | 1.84M | s->prev_block_len_bits = s->frame_len_bits; |
501 | 1.84M | s->block_len_bits = s->frame_len_bits; |
502 | 1.84M | } |
503 | | |
504 | 1.86M | if (s->frame_len_bits - s->block_len_bits >= s->nb_block_sizes){ |
505 | 0 | av_log(s->avctx, AV_LOG_ERROR, "block_len_bits not initialized to a valid value\n"); |
506 | 0 | return AVERROR_INVALIDDATA; |
507 | 0 | } |
508 | | |
509 | | /* now check if the block length is coherent with the frame length */ |
510 | 1.86M | s->block_len = 1 << s->block_len_bits; |
511 | 1.86M | if ((s->block_pos + s->block_len) > s->frame_len) { |
512 | 1.70k | av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n"); |
513 | 1.70k | return AVERROR_INVALIDDATA; |
514 | 1.70k | } |
515 | | |
516 | 1.86M | if (channels == 2) |
517 | 83.7k | s->ms_stereo = get_bits1(&s->gb); |
518 | 1.86M | v = 0; |
519 | 3.81M | for (ch = 0; ch < channels; ch++) { |
520 | 1.94M | a = get_bits1(&s->gb); |
521 | 1.94M | s->channel_coded[ch] = a; |
522 | 1.94M | v |= a; |
523 | 1.94M | } |
524 | | |
525 | 1.86M | bsize = s->frame_len_bits - s->block_len_bits; |
526 | | |
527 | | /* if no channel coded, no need to go further */ |
528 | | /* XXX: fix potential framing problems */ |
529 | 1.86M | if (!v) |
530 | 1.55M | goto next; |
531 | | |
532 | | /* read total gain and extract corresponding number of bits for |
533 | | * coef escape coding */ |
534 | 313k | total_gain = 1; |
535 | 330k | for (;;) { |
536 | 330k | if (get_bits_left(&s->gb) < 7) { |
537 | 3.52k | av_log(s->avctx, AV_LOG_ERROR, "total_gain overread\n"); |
538 | 3.52k | return AVERROR_INVALIDDATA; |
539 | 3.52k | } |
540 | 326k | a = get_bits(&s->gb, 7); |
541 | 326k | total_gain += a; |
542 | 326k | if (a != 127) |
543 | 309k | break; |
544 | 326k | } |
545 | | |
546 | 309k | coef_nb_bits = ff_wma_total_gain_to_bits(total_gain); |
547 | | |
548 | | /* compute number of coefficients */ |
549 | 309k | n = s->coefs_end[bsize] - s->coefs_start; |
550 | 635k | for (ch = 0; ch < channels; ch++) |
551 | 325k | nb_coefs[ch] = n; |
552 | | |
553 | | /* complex coding */ |
554 | 309k | if (s->use_noise_coding) { |
555 | 516k | for (ch = 0; ch < channels; ch++) { |
556 | 265k | if (s->channel_coded[ch]) { |
557 | 253k | int i, n, a; |
558 | 253k | n = s->exponent_high_sizes[bsize]; |
559 | 829k | for (i = 0; i < n; i++) { |
560 | 576k | a = get_bits1(&s->gb); |
561 | 576k | s->high_band_coded[ch][i] = a; |
562 | | /* if noise coding, the coefficients are not transmitted */ |
563 | 576k | if (a) |
564 | 195k | nb_coefs[ch] -= s->exponent_high_bands[bsize][i]; |
565 | 576k | } |
566 | 253k | } |
567 | 265k | } |
568 | 516k | for (ch = 0; ch < channels; ch++) { |
569 | 265k | if (s->channel_coded[ch]) { |
570 | 253k | int i, n, val; |
571 | | |
572 | 253k | n = s->exponent_high_sizes[bsize]; |
573 | 253k | val = (int) 0x80000000; |
574 | 829k | for (i = 0; i < n; i++) { |
575 | 576k | if (s->high_band_coded[ch][i]) { |
576 | 195k | if (val == (int) 0x80000000) { |
577 | 141k | val = get_bits(&s->gb, 7) - 19; |
578 | 141k | } else { |
579 | 53.5k | val += get_vlc2(&s->gb, s->hgain_vlc.table, |
580 | 53.5k | HGAINVLCBITS, HGAINMAX); |
581 | 53.5k | } |
582 | 195k | s->high_band_values[ch][i] = val; |
583 | 195k | } |
584 | 576k | } |
585 | 253k | } |
586 | 265k | } |
587 | 250k | } |
588 | | |
589 | | /* exponents can be reused in short blocks. */ |
590 | 309k | if ((s->block_len_bits == s->frame_len_bits) || get_bits1(&s->gb)) { |
591 | 628k | for (ch = 0; ch < channels; ch++) { |
592 | 322k | if (s->channel_coded[ch]) { |
593 | 310k | if (s->use_exp_vlc) { |
594 | 34.5k | if (decode_exp_vlc(s, ch) < 0) |
595 | 1.15k | return AVERROR_INVALIDDATA; |
596 | 275k | } else { |
597 | 275k | decode_exp_lsp(s, ch); |
598 | 275k | } |
599 | 308k | s->exponents_bsize[ch] = bsize; |
600 | 308k | s->exponents_initialized[ch] = 1; |
601 | 308k | } |
602 | 322k | } |
603 | 307k | } |
604 | | |
605 | 631k | for (ch = 0; ch < channels; ch++) { |
606 | 323k | if (s->channel_coded[ch] && !s->exponents_initialized[ch]) |
607 | 716 | return AVERROR_INVALIDDATA; |
608 | 323k | } |
609 | | |
610 | | /* parse spectral coefficients : just RLE encoding */ |
611 | 616k | for (ch = 0; ch < channels; ch++) { |
612 | 322k | if (s->channel_coded[ch]) { |
613 | 310k | int tindex; |
614 | 310k | WMACoef *ptr = &s->coefs1[ch][0]; |
615 | 310k | int ret; |
616 | | |
617 | | /* special VLC tables are used for ms stereo because |
618 | | * there is potentially less energy there */ |
619 | 310k | tindex = (ch == 1 && s->ms_stereo); |
620 | 310k | memset(ptr, 0, s->block_len * sizeof(WMACoef)); |
621 | 310k | ret = ff_wma_run_level_decode(s->avctx, &s->gb, s->coef_vlc[tindex].table, |
622 | 310k | s->level_table[tindex], s->run_table[tindex], |
623 | 310k | 0, ptr, 0, nb_coefs[ch], |
624 | 310k | s->block_len, s->frame_len_bits, coef_nb_bits); |
625 | 310k | if (ret < 0) |
626 | 13.3k | return ret; |
627 | 310k | } |
628 | 308k | if (s->version == 1 && channels >= 2) |
629 | 12.5k | align_get_bits(&s->gb); |
630 | 308k | } |
631 | | |
632 | | /* normalize */ |
633 | 294k | { |
634 | 294k | int n4 = s->block_len / 2; |
635 | 294k | mdct_norm = 1.0 / (float) n4; |
636 | 294k | if (s->version == 1) |
637 | 133k | mdct_norm *= sqrt(n4); |
638 | 294k | } |
639 | | |
640 | | /* finally compute the MDCT coefficients */ |
641 | 601k | for (ch = 0; ch < channels; ch++) { |
642 | 307k | if (s->channel_coded[ch]) { |
643 | 296k | WMACoef *coefs1; |
644 | 296k | float *coefs, *exponents, mult, mult1, noise; |
645 | 296k | int i, j, n, n1, last_high_band, esize; |
646 | 296k | float exp_power[HIGH_BAND_MAX_SIZE]; |
647 | | |
648 | 296k | coefs1 = s->coefs1[ch]; |
649 | 296k | exponents = s->exponents[ch]; |
650 | 296k | esize = s->exponents_bsize[ch]; |
651 | 296k | mult = ff_exp10(total_gain * 0.05) / s->max_exponent[ch]; |
652 | 296k | mult *= mdct_norm; |
653 | 296k | coefs = s->coefs[ch]; |
654 | 296k | if (s->use_noise_coding) { |
655 | 237k | mult1 = mult; |
656 | | /* very low freqs : noise */ |
657 | 640k | for (i = 0; i < s->coefs_start; i++) { |
658 | 402k | *coefs++ = s->noise_table[s->noise_index] * |
659 | 402k | exponents[i << bsize >> esize] * mult1; |
660 | 402k | s->noise_index = (s->noise_index + 1) & |
661 | 402k | (NOISE_TAB_SIZE - 1); |
662 | 402k | } |
663 | | |
664 | 237k | n1 = s->exponent_high_sizes[bsize]; |
665 | | |
666 | | /* compute power of high bands */ |
667 | 237k | exponents = s->exponents[ch] + |
668 | 237k | (s->high_band_start[bsize] << bsize >> esize); |
669 | 237k | last_high_band = 0; /* avoid warning */ |
670 | 767k | for (j = 0; j < n1; j++) { |
671 | 530k | n = s->exponent_high_bands[s->frame_len_bits - |
672 | 530k | s->block_len_bits][j]; |
673 | 530k | if (s->high_band_coded[ch][j]) { |
674 | 178k | float e2, v; |
675 | 178k | e2 = 0; |
676 | 27.1M | for (i = 0; i < n; i++) { |
677 | 27.0M | v = exponents[i << bsize >> esize]; |
678 | 27.0M | e2 += v * v; |
679 | 27.0M | } |
680 | 178k | exp_power[j] = e2 / n; |
681 | 178k | last_high_band = j; |
682 | 178k | ff_tlog(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n); |
683 | 178k | } |
684 | 530k | exponents += n << bsize >> esize; |
685 | 530k | } |
686 | | |
687 | | /* main freqs and high freqs */ |
688 | 237k | exponents = s->exponents[ch] + (s->coefs_start << bsize >> esize); |
689 | 1.00M | for (j = -1; j < n1; j++) { |
690 | 767k | if (j < 0) |
691 | 237k | n = s->high_band_start[bsize] - s->coefs_start; |
692 | 530k | else |
693 | 530k | n = s->exponent_high_bands[s->frame_len_bits - |
694 | 530k | s->block_len_bits][j]; |
695 | 767k | if (j >= 0 && s->high_band_coded[ch][j]) { |
696 | | /* use noise with specified power */ |
697 | 178k | mult1 = sqrt(exp_power[j] / exp_power[last_high_band]); |
698 | | /* XXX: use a table */ |
699 | 178k | mult1 = mult1 * ff_exp10(s->high_band_values[ch][j] * 0.05); |
700 | 178k | mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult); |
701 | 178k | mult1 *= mdct_norm; |
702 | 27.1M | for (i = 0; i < n; i++) { |
703 | 27.0M | noise = s->noise_table[s->noise_index]; |
704 | 27.0M | s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); |
705 | 27.0M | *coefs++ = noise * exponents[i << bsize >> esize] * mult1; |
706 | 27.0M | } |
707 | 178k | exponents += n << bsize >> esize; |
708 | 589k | } else { |
709 | | /* coded values + small noise */ |
710 | 164M | for (i = 0; i < n; i++) { |
711 | 163M | noise = s->noise_table[s->noise_index]; |
712 | 163M | s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); |
713 | 163M | *coefs++ = ((*coefs1++) + noise) * |
714 | 163M | exponents[i << bsize >> esize] * mult; |
715 | 163M | } |
716 | 589k | exponents += n << bsize >> esize; |
717 | 589k | } |
718 | 767k | } |
719 | | |
720 | | /* very high freqs : noise */ |
721 | 237k | n = s->block_len - s->coefs_end[bsize]; |
722 | 237k | mult1 = mult * exponents[(-(1 << bsize)) >> esize]; |
723 | 19.1M | for (i = 0; i < n; i++) { |
724 | 18.8M | *coefs++ = s->noise_table[s->noise_index] * mult1; |
725 | 18.8M | s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); |
726 | 18.8M | } |
727 | 237k | } else { |
728 | | /* XXX: optimize more */ |
729 | 59.9k | for (i = 0; i < s->coefs_start; i++) |
730 | 1.07k | *coefs++ = 0.0; |
731 | 58.8k | n = nb_coefs[ch]; |
732 | 107M | for (i = 0; i < n; i++) |
733 | 107M | *coefs++ = coefs1[i] * exponents[i << bsize >> esize] * mult; |
734 | 58.8k | n = s->block_len - s->coefs_end[bsize]; |
735 | 10.6M | for (i = 0; i < n; i++) |
736 | 10.6M | *coefs++ = 0.0; |
737 | 58.8k | } |
738 | 296k | } |
739 | 307k | } |
740 | | |
741 | | #ifdef TRACE |
742 | | for (ch = 0; ch < channels; ch++) { |
743 | | if (s->channel_coded[ch]) { |
744 | | dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len); |
745 | | dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len); |
746 | | } |
747 | | } |
748 | | #endif /* TRACE */ |
749 | | |
750 | 294k | if (s->ms_stereo && s->channel_coded[1]) { |
751 | | /* nominal case for ms stereo: we do it before mdct */ |
752 | | /* no need to optimize this case because it should almost |
753 | | * never happen */ |
754 | 5.97k | if (!s->channel_coded[0]) { |
755 | 4.63k | ff_tlog(s->avctx, "rare ms-stereo case happened\n"); |
756 | 4.63k | memset(s->coefs[0], 0, sizeof(float) * s->block_len); |
757 | 4.63k | s->channel_coded[0] = 1; |
758 | 4.63k | } |
759 | | |
760 | 5.97k | s->fdsp->butterflies_float(s->coefs[0], s->coefs[1], s->block_len); |
761 | 5.97k | } |
762 | | |
763 | 1.84M | next: |
764 | 1.84M | mdct = s->mdct_ctx[bsize]; |
765 | 1.84M | mdct_fn = s->mdct_fn[bsize]; |
766 | | |
767 | 3.76M | for (ch = 0; ch < channels; ch++) { |
768 | 1.92M | int n4, index; |
769 | | |
770 | 1.92M | n4 = s->block_len / 2; |
771 | 1.92M | if (s->channel_coded[ch]) |
772 | 300k | mdct_fn(mdct, s->output, s->coefs[ch], sizeof(float)); |
773 | 1.62M | else if (!(s->ms_stereo && ch == 1)) |
774 | 1.61M | memset(s->output, 0, sizeof(s->output)); |
775 | | |
776 | | /* multiply by the window and add in the frame */ |
777 | 1.92M | index = (s->frame_len / 2) + s->block_pos - n4; |
778 | 1.92M | wma_window(s, &s->frame_out[ch][index]); |
779 | 1.92M | } |
780 | | |
781 | | /* update block number */ |
782 | 1.84M | s->block_num++; |
783 | 1.84M | s->block_pos += s->block_len; |
784 | 1.84M | if (s->block_pos >= s->frame_len) |
785 | 1.83M | return 1; |
786 | 7.33k | else |
787 | 7.33k | return 0; |
788 | 1.84M | } |
789 | | |
790 | | /* decode a frame of frame_len samples */ |
791 | | static int wma_decode_frame(WMACodecContext *s, float **samples, |
792 | | int samples_offset) |
793 | 1.86M | { |
794 | 1.86M | int ret, ch; |
795 | | |
796 | | #ifdef TRACE |
797 | | ff_tlog(s->avctx, "***decode_frame: %d size=%d\n", |
798 | | s->frame_count++, s->frame_len); |
799 | | #endif /* TRACE */ |
800 | | |
801 | | /* read each block */ |
802 | 1.86M | s->block_num = 0; |
803 | 1.86M | s->block_pos = 0; |
804 | 1.86M | for (;;) { |
805 | 1.86M | ret = wma_decode_block(s); |
806 | 1.86M | if (ret < 0) |
807 | 23.0k | return ret; |
808 | 1.84M | if (ret) |
809 | 1.83M | break; |
810 | 1.84M | } |
811 | | |
812 | 3.75M | for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) { |
813 | | /* copy current block to output */ |
814 | 1.91M | memcpy(samples[ch] + samples_offset, s->frame_out[ch], |
815 | 1.91M | s->frame_len * sizeof(*s->frame_out[ch])); |
816 | | /* prepare for next block */ |
817 | 1.91M | memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len], |
818 | 1.91M | s->frame_len * sizeof(*s->frame_out[ch])); |
819 | | |
820 | | #ifdef TRACE |
821 | | dump_floats(s, "samples", 6, samples[ch] + samples_offset, |
822 | | s->frame_len); |
823 | | #endif /* TRACE */ |
824 | 1.91M | } |
825 | | |
826 | 1.83M | return 0; |
827 | 1.86M | } |
828 | | |
829 | | static int wma_decode_superframe(AVCodecContext *avctx, AVFrame *frame, |
830 | | int *got_frame_ptr, AVPacket *avpkt) |
831 | 2.02M | { |
832 | 2.02M | const uint8_t *buf = avpkt->data; |
833 | 2.02M | int buf_size = avpkt->size; |
834 | 2.02M | WMACodecContext *s = avctx->priv_data; |
835 | 2.02M | int nb_frames, bit_offset, i, pos, len, ret; |
836 | 2.02M | uint8_t *q; |
837 | 2.02M | float **samples; |
838 | 2.02M | int samples_offset; |
839 | | |
840 | 2.02M | ff_tlog(avctx, "***decode_superframe:\n"); |
841 | | |
842 | 2.02M | if (buf_size == 0) { |
843 | 3.22k | if (s->eof_done) |
844 | 1.61k | return 0; |
845 | | |
846 | 1.61k | frame->nb_samples = s->frame_len; |
847 | 1.61k | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
848 | 0 | return ret; |
849 | | |
850 | 1.61k | frame->pts = AV_NOPTS_VALUE; |
851 | 3.87k | for (i = 0; i < s->avctx->ch_layout.nb_channels; i++) |
852 | 2.26k | memcpy(frame->extended_data[i], &s->frame_out[i][0], |
853 | 2.26k | frame->nb_samples * sizeof(s->frame_out[i][0])); |
854 | | |
855 | 1.61k | s->last_superframe_len = 0; |
856 | 1.61k | s->eof_done = 1; |
857 | 1.61k | *got_frame_ptr = 1; |
858 | 1.61k | return 0; |
859 | 1.61k | } |
860 | 2.01M | if (buf_size < avctx->block_align) { |
861 | 162k | av_log(avctx, AV_LOG_ERROR, |
862 | 162k | "Input packet size too small (%d < %d)\n", |
863 | 162k | buf_size, avctx->block_align); |
864 | 162k | return AVERROR_INVALIDDATA; |
865 | 162k | } |
866 | 1.85M | if (avctx->block_align) |
867 | 1.85M | buf_size = avctx->block_align; |
868 | | |
869 | 1.85M | init_get_bits(&s->gb, buf, buf_size * 8); |
870 | | |
871 | 1.85M | if (s->use_bit_reservoir) { |
872 | | /* read super frame header */ |
873 | 33.0k | skip_bits(&s->gb, 4); /* super frame index */ |
874 | 33.0k | nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0); |
875 | 33.0k | if (nb_frames <= 0) { |
876 | 12.3k | int is_error = nb_frames < 0 || get_bits_left(&s->gb) <= 8; |
877 | 12.3k | av_log(avctx, is_error ? AV_LOG_ERROR : AV_LOG_WARNING, |
878 | 12.3k | "nb_frames is %d bits left %d\n", |
879 | 12.3k | nb_frames, get_bits_left(&s->gb)); |
880 | 12.3k | if (is_error) |
881 | 3.10k | return AVERROR_INVALIDDATA; |
882 | | |
883 | 9.28k | if ((s->last_superframe_len + buf_size - 1) > |
884 | 9.28k | MAX_CODED_SUPERFRAME_SIZE) { |
885 | 136 | ret = AVERROR_INVALIDDATA; |
886 | 136 | goto fail; |
887 | 136 | } |
888 | | |
889 | 9.15k | q = s->last_superframe + s->last_superframe_len; |
890 | 9.15k | len = buf_size - 1; |
891 | 2.20M | while (len > 0) { |
892 | 2.19M | *q++ = get_bits (&s->gb, 8); |
893 | 2.19M | len --; |
894 | 2.19M | } |
895 | 9.15k | memset(q, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
896 | | |
897 | 9.15k | s->last_superframe_len += 8*buf_size - 8; |
898 | | // s->reset_block_lengths = 1; //XXX is this needed ? |
899 | 9.15k | *got_frame_ptr = 0; |
900 | 9.15k | return buf_size; |
901 | 9.28k | } |
902 | 33.0k | } else |
903 | 1.82M | nb_frames = 1; |
904 | | |
905 | | /* get output buffer */ |
906 | 1.84M | frame->nb_samples = nb_frames * s->frame_len; |
907 | 1.84M | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
908 | 0 | return ret; |
909 | 1.84M | samples = (float **) frame->extended_data; |
910 | 1.84M | samples_offset = 0; |
911 | | |
912 | 1.84M | if (s->use_bit_reservoir) { |
913 | 20.6k | bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3); |
914 | 20.6k | if (bit_offset > get_bits_left(&s->gb)) { |
915 | 8.35k | av_log(avctx, AV_LOG_ERROR, |
916 | 8.35k | "Invalid last frame bit offset %d > buf size %d (%d)\n", |
917 | 8.35k | bit_offset, get_bits_left(&s->gb), buf_size); |
918 | 8.35k | ret = AVERROR_INVALIDDATA; |
919 | 8.35k | goto fail; |
920 | 8.35k | } |
921 | | |
922 | 12.3k | if (s->last_superframe_len > 0) { |
923 | | /* add bit_offset bits to last frame */ |
924 | 5.99k | if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) > |
925 | 5.99k | MAX_CODED_SUPERFRAME_SIZE) { |
926 | 135 | ret = AVERROR_INVALIDDATA; |
927 | 135 | goto fail; |
928 | 135 | } |
929 | 5.86k | q = s->last_superframe + s->last_superframe_len; |
930 | 5.86k | len = bit_offset; |
931 | 114k | while (len > 7) { |
932 | 108k | *q++ = get_bits(&s->gb, 8); |
933 | 108k | len -= 8; |
934 | 108k | } |
935 | 5.86k | if (len > 0) |
936 | 3.35k | *q++ = get_bits(&s->gb, len) << (8 - len); |
937 | 5.86k | memset(q, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
938 | | |
939 | | /* XXX: bit_offset bits into last frame */ |
940 | 5.86k | init_get_bits(&s->gb, s->last_superframe, |
941 | 5.86k | s->last_superframe_len * 8 + bit_offset); |
942 | | /* skip unused bits */ |
943 | 5.86k | if (s->last_bitoffset > 0) |
944 | 4.02k | skip_bits(&s->gb, s->last_bitoffset); |
945 | | /* this frame is stored in the last superframe and in the |
946 | | * current one */ |
947 | 5.86k | if ((ret = wma_decode_frame(s, samples, samples_offset)) < 0) |
948 | 844 | goto fail; |
949 | 5.01k | samples_offset += s->frame_len; |
950 | 5.01k | nb_frames--; |
951 | 5.01k | } |
952 | | |
953 | | /* read each frame starting from bit_offset */ |
954 | 11.3k | pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3; |
955 | 11.3k | if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8) |
956 | 11 | return AVERROR_INVALIDDATA; |
957 | 11.3k | init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3)) * 8); |
958 | 11.3k | len = pos & 7; |
959 | 11.3k | if (len > 0) |
960 | 10.3k | skip_bits(&s->gb, len); |
961 | | |
962 | 11.3k | s->reset_block_lengths = 1; |
963 | 40.0k | for (i = 0; i < nb_frames; i++) { |
964 | 32.4k | if ((ret = wma_decode_frame(s, samples, samples_offset)) < 0) |
965 | 3.66k | goto fail; |
966 | 28.7k | samples_offset += s->frame_len; |
967 | 28.7k | } |
968 | | |
969 | | /* we copy the end of the frame in the last frame buffer */ |
970 | 7.66k | pos = get_bits_count(&s->gb) + |
971 | 7.66k | ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7); |
972 | 7.66k | s->last_bitoffset = pos & 7; |
973 | 7.66k | pos >>= 3; |
974 | 7.66k | len = buf_size - pos; |
975 | 7.66k | if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) { |
976 | 1.18k | av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len); |
977 | 1.18k | ret = AVERROR_INVALIDDATA; |
978 | 1.18k | goto fail; |
979 | 1.18k | } |
980 | 6.48k | s->last_superframe_len = len; |
981 | 6.48k | memcpy(s->last_superframe, buf + pos, len); |
982 | 1.82M | } else { |
983 | | /* single frame decode */ |
984 | 1.82M | if ((ret = wma_decode_frame(s, samples, samples_offset)) < 0) |
985 | 18.5k | goto fail; |
986 | 1.80M | samples_offset += s->frame_len; |
987 | 1.80M | } |
988 | | |
989 | 1.80M | ff_dlog(s->avctx, "%d %d %d %d eaten:%d\n", |
990 | 1.80M | s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len, |
991 | 1.80M | avctx->block_align); |
992 | | |
993 | 1.80M | *got_frame_ptr = 1; |
994 | | |
995 | 1.80M | return buf_size; |
996 | | |
997 | 32.8k | fail: |
998 | | /* when error, we reset the bit reservoir */ |
999 | 32.8k | s->last_superframe_len = 0; |
1000 | 32.8k | return ret; |
1001 | 1.84M | } |
1002 | | |
1003 | | static av_cold void flush(AVCodecContext *avctx) |
1004 | 176k | { |
1005 | 176k | WMACodecContext *s = avctx->priv_data; |
1006 | | |
1007 | 176k | s->last_bitoffset = |
1008 | 176k | s->last_superframe_len = 0; |
1009 | | |
1010 | 176k | s->eof_done = 0; |
1011 | 176k | avctx->internal->skip_samples = s->frame_len * 2; |
1012 | 176k | } |
1013 | | |
1014 | | #if CONFIG_WMAV1_DECODER |
1015 | | const FFCodec ff_wmav1_decoder = { |
1016 | | .p.name = "wmav1", |
1017 | | CODEC_LONG_NAME("Windows Media Audio 1"), |
1018 | | .p.type = AVMEDIA_TYPE_AUDIO, |
1019 | | .p.id = AV_CODEC_ID_WMAV1, |
1020 | | .priv_data_size = sizeof(WMACodecContext), |
1021 | | .init = wma_decode_init, |
1022 | | .close = ff_wma_end, |
1023 | | FF_CODEC_DECODE_CB(wma_decode_superframe), |
1024 | | .flush = flush, |
1025 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, |
1026 | | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP), |
1027 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
1028 | | }; |
1029 | | #endif |
1030 | | #if CONFIG_WMAV2_DECODER |
1031 | | const FFCodec ff_wmav2_decoder = { |
1032 | | .p.name = "wmav2", |
1033 | | CODEC_LONG_NAME("Windows Media Audio 2"), |
1034 | | .p.type = AVMEDIA_TYPE_AUDIO, |
1035 | | .p.id = AV_CODEC_ID_WMAV2, |
1036 | | .priv_data_size = sizeof(WMACodecContext), |
1037 | | .init = wma_decode_init, |
1038 | | .close = ff_wma_end, |
1039 | | FF_CODEC_DECODE_CB(wma_decode_superframe), |
1040 | | .flush = flush, |
1041 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, |
1042 | | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP), |
1043 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
1044 | | }; |
1045 | | #endif |