Line | Branch | Exec | 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 | #define EXPVLCBITS 8 | ||
48 | #define EXPMAX ((19 + EXPVLCBITS - 1) / EXPVLCBITS) | ||
49 | |||
50 | #define HGAINVLCBITS 9 | ||
51 | #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 | 14 | static av_cold int wma_decode_init(AVCodecContext *avctx) | |
75 | { | ||
76 | 14 | WMACodecContext *s = avctx->priv_data; | |
77 | int i, flags2, ret; | ||
78 | uint8_t *extradata; | ||
79 | |||
80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (!avctx->block_align) { |
81 | ✗ | av_log(avctx, AV_LOG_ERROR, "block_align is not set\n"); | |
82 | ✗ | return AVERROR(EINVAL); | |
83 | } | ||
84 | |||
85 | 14 | s->avctx = avctx; | |
86 | |||
87 | /* extract flag info */ | ||
88 | 14 | flags2 = 0; | |
89 | 14 | extradata = avctx->extradata; | |
90 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
14 | if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 4) |
91 | 2 | flags2 = AV_RL16(extradata + 2); | |
92 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | else if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 6) |
93 | 12 | flags2 = AV_RL16(extradata + 4); | |
94 | |||
95 | 14 | s->use_exp_vlc = flags2 & 0x0001; | |
96 | 14 | s->use_bit_reservoir = flags2 & 0x0002; | |
97 | 14 | s->use_variable_block_len = flags2 & 0x0004; | |
98 | |||
99 |
4/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1 times.
|
14 | if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 8){ |
100 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
11 | if (AV_RL16(extradata+4)==0xd && s->use_variable_block_len){ |
101 | ✗ | 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 | ✗ | s->use_variable_block_len= 0; // this fixes issue1503 | |
103 | } | ||
104 | } | ||
105 | |||
106 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
|
42 | for (i=0; i<MAX_CHANNELS; i++) |
107 | 28 | s->max_exponent[i] = 1.0; | |
108 | |||
109 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
|
14 | if ((ret = ff_wma_init(avctx, flags2)) < 0) |
110 | ✗ | return ret; | |
111 | |||
112 | /* init MDCT */ | ||
113 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 14 times.
|
51 | for (i = 0; i < s->nb_block_sizes; i++) { |
114 | 37 | float scale = 1.0 / 32768.0; | |
115 | 37 | ret = av_tx_init(&s->mdct_ctx[i], &s->mdct_fn[i], AV_TX_FLOAT_MDCT, | |
116 | 37 | 1, 1 << (s->frame_len_bits - i), &scale, AV_TX_FULL_IMDCT); | |
117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
|
37 | if (ret < 0) |
118 | ✗ | return ret; | |
119 | } | ||
120 | |||
121 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 | if (s->use_noise_coding) { |
122 | 2 | ret = ff_vlc_init_from_lengths(&s->hgain_vlc, HGAINVLCBITS, | |
123 | FF_ARRAY_ELEMS(ff_wma_hgain_hufftab), | ||
124 | &ff_wma_hgain_hufftab[0][1], 2, | ||
125 | &ff_wma_hgain_hufftab[0][0], 2, 1, | ||
126 | -18, 0, avctx); | ||
127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
128 | ✗ | return ret; | |
129 | } | ||
130 | |||
131 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3 times.
|
14 | if (s->use_exp_vlc) { |
132 | // FIXME move out of context | ||
133 | 11 | ret = vlc_init(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), | |
134 | ff_aac_scalefactor_bits, 1, 1, | ||
135 | ff_aac_scalefactor_code, 4, 4, 0); | ||
136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (ret < 0) |
137 | ✗ | return ret; | |
138 | } else | ||
139 | 3 | wma_lsp_to_curve_init(s, s->frame_len); | |
140 | |||
141 | 14 | avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; | |
142 | |||
143 | 14 | avctx->internal->skip_samples = s->frame_len * 2; | |
144 | |||
145 | 14 | return 0; | |
146 | } | ||
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 | { | ||
156 | union { | ||
157 | float f; | ||
158 | unsigned int v; | ||
159 | } u, t; | ||
160 | unsigned int e, m; | ||
161 | float a, b; | ||
162 | |||
163 | ✗ | u.f = x; | |
164 | ✗ | e = u.v >> 23; | |
165 | ✗ | m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1); | |
166 | /* build interpolation scale: 1 <= t < 2. */ | ||
167 | ✗ | t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23); | |
168 | ✗ | a = s->lsp_pow_m_table1[m]; | |
169 | ✗ | b = s->lsp_pow_m_table2[m]; | |
170 | ✗ | return s->lsp_pow_e_table[e] * (a + b * t.f); | |
171 | } | ||
172 | |||
173 | 3 | static av_cold void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len) | |
174 | { | ||
175 | float wdel, a, b; | ||
176 | int i, e, m; | ||
177 | |||
178 | 3 | wdel = M_PI / frame_len; | |
179 |
2/2✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 3 times.
|
1539 | for (i = 0; i < frame_len; i++) |
180 | 1536 | s->lsp_cos_table[i] = 2.0f * cos(wdel * i); | |
181 | |||
182 | /* tables for x^-0.25 computation */ | ||
183 |
2/2✓ Branch 0 taken 768 times.
✓ Branch 1 taken 3 times.
|
771 | for (i = 0; i < 256; i++) { |
184 | 768 | e = i - 126; | |
185 | 768 | s->lsp_pow_e_table[i] = exp2f(e * -0.25); | |
186 | } | ||
187 | |||
188 | /* NOTE: these two tables are needed to avoid two operations in | ||
189 | * pow_m1_4 */ | ||
190 | 3 | b = 1.0; | |
191 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 3 times.
|
387 | for (i = (1 << LSP_POW_BITS) - 1; i >= 0; i--) { |
192 | 384 | m = (1 << LSP_POW_BITS) + i; | |
193 | 384 | a = (float) m * (0.5 / (1 << LSP_POW_BITS)); | |
194 | 384 | a = 1/sqrt(sqrt(a)); | |
195 | 384 | s->lsp_pow_m_table1[i] = 2 * a - b; | |
196 | 384 | s->lsp_pow_m_table2[i] = b - a; | |
197 | 384 | b = a; | |
198 | } | ||
199 | 3 | } | |
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 | { | ||
208 | int i, j; | ||
209 | float p, q, w, v, val_max; | ||
210 | |||
211 | ✗ | val_max = 0; | |
212 | ✗ | for (i = 0; i < n; i++) { | |
213 | ✗ | p = 0.5f; | |
214 | ✗ | q = 0.5f; | |
215 | ✗ | w = s->lsp_cos_table[i]; | |
216 | ✗ | for (j = 1; j < NB_LSP_COEFS; j += 2) { | |
217 | ✗ | q *= w - lsp[j - 1]; | |
218 | ✗ | p *= w - lsp[j]; | |
219 | } | ||
220 | ✗ | p *= p * (2.0f - w); | |
221 | ✗ | q *= q * (2.0f + w); | |
222 | ✗ | v = p + q; | |
223 | ✗ | v = pow_m1_4(s, v); | |
224 | ✗ | if (v > val_max) | |
225 | ✗ | val_max = v; | |
226 | ✗ | out[i] = v; | |
227 | } | ||
228 | ✗ | *val_max_ptr = val_max; | |
229 | ✗ | } | |
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 | { | ||
236 | float lsp_coefs[NB_LSP_COEFS]; | ||
237 | int val, i; | ||
238 | |||
239 | ✗ | for (i = 0; i < NB_LSP_COEFS; i++) { | |
240 | ✗ | if (i == 0 || i >= 8) | |
241 | ✗ | val = get_bits(&s->gb, 3); | |
242 | else | ||
243 | ✗ | val = get_bits(&s->gb, 4); | |
244 | ✗ | lsp_coefs[i] = ff_wma_lsp_codebook[i][val]; | |
245 | } | ||
246 | |||
247 | ✗ | wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch], | |
248 | s->block_len, lsp_coefs); | ||
249 | ✗ | } | |
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 | 1136 | static int decode_exp_vlc(WMACodecContext *s, int ch) | |
337 | { | ||
338 | int last_exp, n, code; | ||
339 | const uint16_t *ptr; | ||
340 | float v, max_scale; | ||
341 | uint32_t *q, *q_end, iv; | ||
342 | 1136 | const float *ptab = pow_tab + 60; | |
343 | 1136 | const uint32_t *iptab = (const uint32_t *) ptab; | |
344 | |||
345 | 1136 | ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits]; | |
346 | 1136 | q = (uint32_t *) s->exponents[ch]; | |
347 | 1136 | q_end = q + s->block_len; | |
348 | 1136 | max_scale = 0; | |
349 |
2/2✓ Branch 0 taken 410 times.
✓ Branch 1 taken 726 times.
|
1136 | if (s->version == 1) { |
350 | 410 | last_exp = get_bits(&s->gb, 5) + 10; | |
351 | 410 | v = ptab[last_exp]; | |
352 | 410 | iv = iptab[last_exp]; | |
353 | 410 | max_scale = v; | |
354 | 410 | n = *ptr++; | |
355 |
1/5✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 410 times.
✗ Branch 4 not taken.
|
410 | switch (n & 3) do { |
356 | 820 | case 0: *q++ = iv; | |
357 | 820 | case 3: *q++ = iv; | |
358 | 820 | case 2: *q++ = iv; | |
359 | 1230 | case 1: *q++ = iv; | |
360 |
2/2✓ Branch 0 taken 820 times.
✓ Branch 1 taken 410 times.
|
1230 | } while ((n -= 4) > 0); |
361 | } else | ||
362 | 726 | last_exp = 36; | |
363 | |||
364 |
2/2✓ Branch 0 taken 27990 times.
✓ Branch 1 taken 1136 times.
|
29126 | while (q < q_end) { |
365 | 27990 | code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX); | |
366 | /* NOTE: this offset is the same as MPEG-4 AAC! */ | ||
367 | 27990 | last_exp += code - 60; | |
368 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27990 times.
|
27990 | if ((unsigned) last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) { |
369 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n", | |
370 | last_exp); | ||
371 | ✗ | return -1; | |
372 | } | ||
373 | 27990 | v = ptab[last_exp]; | |
374 | 27990 | iv = iptab[last_exp]; | |
375 |
2/2✓ Branch 0 taken 1141 times.
✓ Branch 1 taken 26849 times.
|
27990 | if (v > max_scale) |
376 | 1141 | max_scale = v; | |
377 | 27990 | n = *ptr++; | |
378 |
4/5✓ Branch 0 taken 19380 times.
✓ Branch 1 taken 2460 times.
✓ Branch 2 taken 2460 times.
✓ Branch 3 taken 3690 times.
✗ Branch 4 not taken.
|
27990 | switch (n & 3) do { |
379 | 576712 | case 0: *q++ = iv; | |
380 | 579172 | case 3: *q++ = iv; | |
381 | 581632 | case 2: *q++ = iv; | |
382 | 585322 | case 1: *q++ = iv; | |
383 |
2/2✓ Branch 0 taken 557332 times.
✓ Branch 1 taken 27990 times.
|
585322 | } while ((n -= 4) > 0); |
384 | } | ||
385 | 1136 | s->max_exponent[ch] = max_scale; | |
386 | 1136 | return 0; | |
387 | } | ||
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 | 1172 | static void wma_window(WMACodecContext *s, float *out) | |
396 | { | ||
397 | 1172 | float *in = s->output; | |
398 | int block_len, bsize, n; | ||
399 | |||
400 | /* left part */ | ||
401 |
1/2✓ Branch 0 taken 1172 times.
✗ Branch 1 not taken.
|
1172 | if (s->block_len_bits <= s->prev_block_len_bits) { |
402 | 1172 | block_len = s->block_len; | |
403 | 1172 | bsize = s->frame_len_bits - s->block_len_bits; | |
404 | |||
405 | 1172 | s->fdsp->vector_fmul_add(out, in, s->windows[bsize], | |
406 | out, block_len); | ||
407 | } else { | ||
408 | ✗ | block_len = 1 << s->prev_block_len_bits; | |
409 | ✗ | n = (s->block_len - block_len) / 2; | |
410 | ✗ | bsize = s->frame_len_bits - s->prev_block_len_bits; | |
411 | |||
412 | ✗ | s->fdsp->vector_fmul_add(out + n, in + n, s->windows[bsize], | |
413 | ✗ | out + n, block_len); | |
414 | |||
415 | ✗ | memcpy(out + n + block_len, in + n + block_len, n * sizeof(float)); | |
416 | } | ||
417 | |||
418 | 1172 | out += s->block_len; | |
419 | 1172 | in += s->block_len; | |
420 | |||
421 | /* right part */ | ||
422 |
1/2✓ Branch 0 taken 1172 times.
✗ Branch 1 not taken.
|
1172 | if (s->block_len_bits <= s->next_block_len_bits) { |
423 | 1172 | block_len = s->block_len; | |
424 | 1172 | bsize = s->frame_len_bits - s->block_len_bits; | |
425 | |||
426 | 1172 | s->fdsp->vector_fmul_reverse(out, in, s->windows[bsize], block_len); | |
427 | } else { | ||
428 | ✗ | block_len = 1 << s->next_block_len_bits; | |
429 | ✗ | n = (s->block_len - block_len) / 2; | |
430 | ✗ | bsize = s->frame_len_bits - s->next_block_len_bits; | |
431 | |||
432 | ✗ | memcpy(out, in, n * sizeof(float)); | |
433 | |||
434 | ✗ | s->fdsp->vector_fmul_reverse(out + n, in + n, s->windows[bsize], | |
435 | block_len); | ||
436 | |||
437 | ✗ | memset(out + n + block_len, 0, n * sizeof(float)); | |
438 | } | ||
439 | 1172 | } | |
440 | |||
441 | /** | ||
442 | * @return 0 if OK. 1 if last block of frame. return -1 if | ||
443 | * unrecoverable error. | ||
444 | */ | ||
445 | 586 | static int wma_decode_block(WMACodecContext *s) | |
446 | { | ||
447 | 586 | int channels = s->avctx->ch_layout.nb_channels; | |
448 | int n, v, a, ch, bsize; | ||
449 | int coef_nb_bits, total_gain; | ||
450 | int nb_coefs[MAX_CHANNELS]; | ||
451 | float mdct_norm; | ||
452 | AVTXContext *mdct; | ||
453 | av_tx_fn mdct_fn; | ||
454 | |||
455 | #ifdef TRACE | ||
456 | ff_tlog(s->avctx, "***decode_block: %d:%d\n", | ||
457 | s->frame_count - 1, s->block_num); | ||
458 | #endif /* TRACE */ | ||
459 | |||
460 | /* compute current block length */ | ||
461 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 586 times.
|
586 | if (s->use_variable_block_len) { |
462 | ✗ | n = av_log2(s->nb_block_sizes - 1) + 1; | |
463 | |||
464 | ✗ | if (s->reset_block_lengths) { | |
465 | ✗ | s->reset_block_lengths = 0; | |
466 | ✗ | v = get_bits(&s->gb, n); | |
467 | ✗ | if (v >= s->nb_block_sizes) { | |
468 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
469 | "prev_block_len_bits %d out of range\n", | ||
470 | ✗ | s->frame_len_bits - v); | |
471 | ✗ | return -1; | |
472 | } | ||
473 | ✗ | s->prev_block_len_bits = s->frame_len_bits - v; | |
474 | ✗ | v = get_bits(&s->gb, n); | |
475 | ✗ | if (v >= s->nb_block_sizes) { | |
476 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
477 | "block_len_bits %d out of range\n", | ||
478 | ✗ | s->frame_len_bits - v); | |
479 | ✗ | return -1; | |
480 | } | ||
481 | ✗ | s->block_len_bits = s->frame_len_bits - v; | |
482 | } else { | ||
483 | /* update block lengths */ | ||
484 | ✗ | s->prev_block_len_bits = s->block_len_bits; | |
485 | ✗ | s->block_len_bits = s->next_block_len_bits; | |
486 | } | ||
487 | ✗ | v = get_bits(&s->gb, n); | |
488 | ✗ | if (v >= s->nb_block_sizes) { | |
489 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
490 | "next_block_len_bits %d out of range\n", | ||
491 | ✗ | s->frame_len_bits - v); | |
492 | ✗ | return -1; | |
493 | } | ||
494 | ✗ | s->next_block_len_bits = s->frame_len_bits - v; | |
495 | } else { | ||
496 | /* fixed block len */ | ||
497 | 586 | s->next_block_len_bits = s->frame_len_bits; | |
498 | 586 | s->prev_block_len_bits = s->frame_len_bits; | |
499 | 586 | s->block_len_bits = s->frame_len_bits; | |
500 | } | ||
501 | |||
502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 586 times.
|
586 | if (s->frame_len_bits - s->block_len_bits >= s->nb_block_sizes){ |
503 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "block_len_bits not initialized to a valid value\n"); | |
504 | ✗ | return -1; | |
505 | } | ||
506 | |||
507 | /* now check if the block length is coherent with the frame length */ | ||
508 | 586 | s->block_len = 1 << s->block_len_bits; | |
509 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 586 times.
|
586 | if ((s->block_pos + s->block_len) > s->frame_len) { |
510 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n"); | |
511 | ✗ | return -1; | |
512 | } | ||
513 | |||
514 |
1/2✓ Branch 0 taken 586 times.
✗ Branch 1 not taken.
|
586 | if (channels == 2) |
515 | 586 | s->ms_stereo = get_bits1(&s->gb); | |
516 | 586 | v = 0; | |
517 |
2/2✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 586 times.
|
1758 | for (ch = 0; ch < channels; ch++) { |
518 | 1172 | a = get_bits1(&s->gb); | |
519 | 1172 | s->channel_coded[ch] = a; | |
520 | 1172 | v |= a; | |
521 | } | ||
522 | |||
523 | 586 | bsize = s->frame_len_bits - s->block_len_bits; | |
524 | |||
525 | /* if no channel coded, no need to go further */ | ||
526 | /* XXX: fix potential framing problems */ | ||
527 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 586 times.
|
586 | if (!v) |
528 | ✗ | goto next; | |
529 | |||
530 | /* read total gain and extract corresponding number of bits for | ||
531 | * coef escape coding */ | ||
532 | 586 | total_gain = 1; | |
533 | for (;;) { | ||
534 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 586 times.
|
586 | if (get_bits_left(&s->gb) < 7) { |
535 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "total_gain overread\n"); | |
536 | ✗ | return AVERROR_INVALIDDATA; | |
537 | } | ||
538 | 586 | a = get_bits(&s->gb, 7); | |
539 | 586 | total_gain += a; | |
540 |
1/2✓ Branch 0 taken 586 times.
✗ Branch 1 not taken.
|
586 | if (a != 127) |
541 | 586 | break; | |
542 | } | ||
543 | |||
544 | 586 | coef_nb_bits = ff_wma_total_gain_to_bits(total_gain); | |
545 | |||
546 | /* compute number of coefficients */ | ||
547 | 586 | n = s->coefs_end[bsize] - s->coefs_start; | |
548 |
2/2✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 586 times.
|
1758 | for (ch = 0; ch < channels; ch++) |
549 | 1172 | nb_coefs[ch] = n; | |
550 | |||
551 | /* complex coding */ | ||
552 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 586 times.
|
586 | if (s->use_noise_coding) { |
553 | ✗ | for (ch = 0; ch < channels; ch++) { | |
554 | ✗ | if (s->channel_coded[ch]) { | |
555 | int i, n, a; | ||
556 | ✗ | n = s->exponent_high_sizes[bsize]; | |
557 | ✗ | for (i = 0; i < n; i++) { | |
558 | ✗ | a = get_bits1(&s->gb); | |
559 | ✗ | s->high_band_coded[ch][i] = a; | |
560 | /* if noise coding, the coefficients are not transmitted */ | ||
561 | ✗ | if (a) | |
562 | ✗ | nb_coefs[ch] -= s->exponent_high_bands[bsize][i]; | |
563 | } | ||
564 | } | ||
565 | } | ||
566 | ✗ | for (ch = 0; ch < channels; ch++) { | |
567 | ✗ | if (s->channel_coded[ch]) { | |
568 | int i, n, val; | ||
569 | |||
570 | ✗ | n = s->exponent_high_sizes[bsize]; | |
571 | ✗ | val = (int) 0x80000000; | |
572 | ✗ | for (i = 0; i < n; i++) { | |
573 | ✗ | if (s->high_band_coded[ch][i]) { | |
574 | ✗ | if (val == (int) 0x80000000) { | |
575 | ✗ | val = get_bits(&s->gb, 7) - 19; | |
576 | } else { | ||
577 | ✗ | val += get_vlc2(&s->gb, s->hgain_vlc.table, | |
578 | HGAINVLCBITS, HGAINMAX); | ||
579 | } | ||
580 | ✗ | s->high_band_values[ch][i] = val; | |
581 | } | ||
582 | } | ||
583 | } | ||
584 | } | ||
585 | } | ||
586 | |||
587 | /* exponents can be reused in short blocks. */ | ||
588 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 586 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
586 | if ((s->block_len_bits == s->frame_len_bits) || get_bits1(&s->gb)) { |
589 |
2/2✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 586 times.
|
1758 | for (ch = 0; ch < channels; ch++) { |
590 |
2/2✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 36 times.
|
1172 | if (s->channel_coded[ch]) { |
591 |
1/2✓ Branch 0 taken 1136 times.
✗ Branch 1 not taken.
|
1136 | if (s->use_exp_vlc) { |
592 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1136 times.
|
1136 | if (decode_exp_vlc(s, ch) < 0) |
593 | ✗ | return -1; | |
594 | } else { | ||
595 | ✗ | decode_exp_lsp(s, ch); | |
596 | } | ||
597 | 1136 | s->exponents_bsize[ch] = bsize; | |
598 | 1136 | s->exponents_initialized[ch] = 1; | |
599 | } | ||
600 | } | ||
601 | } | ||
602 | |||
603 |
2/2✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 586 times.
|
1758 | for (ch = 0; ch < channels; ch++) { |
604 |
3/4✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1136 times.
|
1172 | if (s->channel_coded[ch] && !s->exponents_initialized[ch]) |
605 | ✗ | return AVERROR_INVALIDDATA; | |
606 | } | ||
607 | |||
608 | /* parse spectral coefficients : just RLE encoding */ | ||
609 |
2/2✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 586 times.
|
1758 | for (ch = 0; ch < channels; ch++) { |
610 |
2/2✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 36 times.
|
1172 | if (s->channel_coded[ch]) { |
611 | int tindex; | ||
612 | 1136 | WMACoef *ptr = &s->coefs1[ch][0]; | |
613 | int ret; | ||
614 | |||
615 | /* special VLC tables are used for ms stereo because | ||
616 | * there is potentially less energy there */ | ||
617 |
4/4✓ Branch 0 taken 550 times.
✓ Branch 1 taken 586 times.
✓ Branch 2 taken 480 times.
✓ Branch 3 taken 70 times.
|
1136 | tindex = (ch == 1 && s->ms_stereo); |
618 | 1136 | memset(ptr, 0, s->block_len * sizeof(WMACoef)); | |
619 | 1136 | ret = ff_wma_run_level_decode(s->avctx, &s->gb, s->coef_vlc[tindex].table, | |
620 | 1136 | s->level_table[tindex], s->run_table[tindex], | |
621 | 0, ptr, 0, nb_coefs[ch], | ||
622 | s->block_len, s->frame_len_bits, coef_nb_bits); | ||
623 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1136 times.
|
1136 | if (ret < 0) |
624 | ✗ | return ret; | |
625 | } | ||
626 |
3/4✓ Branch 0 taken 410 times.
✓ Branch 1 taken 762 times.
✓ Branch 2 taken 410 times.
✗ Branch 3 not taken.
|
1172 | if (s->version == 1 && channels >= 2) |
627 | 410 | align_get_bits(&s->gb); | |
628 | } | ||
629 | |||
630 | /* normalize */ | ||
631 | { | ||
632 | 586 | int n4 = s->block_len / 2; | |
633 | 586 | mdct_norm = 1.0 / (float) n4; | |
634 |
2/2✓ Branch 0 taken 205 times.
✓ Branch 1 taken 381 times.
|
586 | if (s->version == 1) |
635 | 205 | mdct_norm *= sqrt(n4); | |
636 | } | ||
637 | |||
638 | /* finally compute the MDCT coefficients */ | ||
639 |
2/2✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 586 times.
|
1758 | for (ch = 0; ch < channels; ch++) { |
640 |
2/2✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 36 times.
|
1172 | if (s->channel_coded[ch]) { |
641 | WMACoef *coefs1; | ||
642 | float *coefs, *exponents, mult, mult1, noise; | ||
643 | int i, j, n, n1, last_high_band, esize; | ||
644 | float exp_power[HIGH_BAND_MAX_SIZE]; | ||
645 | |||
646 | 1136 | coefs1 = s->coefs1[ch]; | |
647 | 1136 | exponents = s->exponents[ch]; | |
648 | 1136 | esize = s->exponents_bsize[ch]; | |
649 | 1136 | mult = ff_exp10(total_gain * 0.05) / s->max_exponent[ch]; | |
650 | 1136 | mult *= mdct_norm; | |
651 | 1136 | coefs = s->coefs[ch]; | |
652 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1136 times.
|
1136 | if (s->use_noise_coding) { |
653 | ✗ | mult1 = mult; | |
654 | /* very low freqs : noise */ | ||
655 | ✗ | for (i = 0; i < s->coefs_start; i++) { | |
656 | ✗ | *coefs++ = s->noise_table[s->noise_index] * | |
657 | ✗ | exponents[i << bsize >> esize] * mult1; | |
658 | ✗ | s->noise_index = (s->noise_index + 1) & | |
659 | (NOISE_TAB_SIZE - 1); | ||
660 | } | ||
661 | |||
662 | ✗ | n1 = s->exponent_high_sizes[bsize]; | |
663 | |||
664 | /* compute power of high bands */ | ||
665 | ✗ | exponents = s->exponents[ch] + | |
666 | ✗ | (s->high_band_start[bsize] << bsize >> esize); | |
667 | ✗ | last_high_band = 0; /* avoid warning */ | |
668 | ✗ | for (j = 0; j < n1; j++) { | |
669 | ✗ | n = s->exponent_high_bands[s->frame_len_bits - | |
670 | ✗ | s->block_len_bits][j]; | |
671 | ✗ | if (s->high_band_coded[ch][j]) { | |
672 | float e2, v; | ||
673 | ✗ | e2 = 0; | |
674 | ✗ | for (i = 0; i < n; i++) { | |
675 | ✗ | v = exponents[i << bsize >> esize]; | |
676 | ✗ | e2 += v * v; | |
677 | } | ||
678 | ✗ | exp_power[j] = e2 / n; | |
679 | ✗ | last_high_band = j; | |
680 | ff_tlog(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n); | ||
681 | } | ||
682 | ✗ | exponents += n << bsize >> esize; | |
683 | } | ||
684 | |||
685 | /* main freqs and high freqs */ | ||
686 | ✗ | exponents = s->exponents[ch] + (s->coefs_start << bsize >> esize); | |
687 | ✗ | for (j = -1; j < n1; j++) { | |
688 | ✗ | if (j < 0) | |
689 | ✗ | n = s->high_band_start[bsize] - s->coefs_start; | |
690 | else | ||
691 | ✗ | n = s->exponent_high_bands[s->frame_len_bits - | |
692 | ✗ | s->block_len_bits][j]; | |
693 | ✗ | if (j >= 0 && s->high_band_coded[ch][j]) { | |
694 | /* use noise with specified power */ | ||
695 | ✗ | mult1 = sqrt(exp_power[j] / exp_power[last_high_band]); | |
696 | /* XXX: use a table */ | ||
697 | ✗ | mult1 = mult1 * ff_exp10(s->high_band_values[ch][j] * 0.05); | |
698 | ✗ | mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult); | |
699 | ✗ | mult1 *= mdct_norm; | |
700 | ✗ | for (i = 0; i < n; i++) { | |
701 | ✗ | noise = s->noise_table[s->noise_index]; | |
702 | ✗ | s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
703 | ✗ | *coefs++ = noise * exponents[i << bsize >> esize] * mult1; | |
704 | } | ||
705 | ✗ | exponents += n << bsize >> esize; | |
706 | } else { | ||
707 | /* coded values + small noise */ | ||
708 | ✗ | for (i = 0; i < n; i++) { | |
709 | ✗ | noise = s->noise_table[s->noise_index]; | |
710 | ✗ | s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
711 | ✗ | *coefs++ = ((*coefs1++) + noise) * | |
712 | ✗ | exponents[i << bsize >> esize] * mult; | |
713 | } | ||
714 | ✗ | exponents += n << bsize >> esize; | |
715 | } | ||
716 | } | ||
717 | |||
718 | /* very high freqs : noise */ | ||
719 | ✗ | n = s->block_len - s->coefs_end[bsize]; | |
720 | ✗ | mult1 = mult * exponents[(-(1 << bsize)) >> esize]; | |
721 | ✗ | for (i = 0; i < n; i++) { | |
722 | ✗ | *coefs++ = s->noise_table[s->noise_index] * mult1; | |
723 | ✗ | s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
724 | } | ||
725 | } else { | ||
726 | /* XXX: optimize more */ | ||
727 |
2/2✓ Branch 0 taken 1230 times.
✓ Branch 1 taken 1136 times.
|
2366 | for (i = 0; i < s->coefs_start; i++) |
728 | 1230 | *coefs++ = 0.0; | |
729 | 1136 | n = nb_coefs[ch]; | |
730 |
2/2✓ Branch 0 taken 2116274 times.
✓ Branch 1 taken 1136 times.
|
2117410 | for (i = 0; i < n; i++) |
731 | 2116274 | *coefs++ = coefs1[i] * exponents[i << bsize >> esize] * mult; | |
732 | 1136 | n = s->block_len - s->coefs_end[bsize]; | |
733 |
2/2✓ Branch 0 taken 209024 times.
✓ Branch 1 taken 1136 times.
|
210160 | for (i = 0; i < n; i++) |
734 | 209024 | *coefs++ = 0.0; | |
735 | } | ||
736 | } | ||
737 | } | ||
738 | |||
739 | #ifdef TRACE | ||
740 | for (ch = 0; ch < channels; ch++) { | ||
741 | if (s->channel_coded[ch]) { | ||
742 | dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len); | ||
743 | dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len); | ||
744 | } | ||
745 | } | ||
746 | #endif /* TRACE */ | ||
747 | |||
748 |
4/4✓ Branch 0 taken 105 times.
✓ Branch 1 taken 481 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 480 times.
|
586 | if (s->ms_stereo && s->channel_coded[1]) { |
749 | /* nominal case for ms stereo: we do it before mdct */ | ||
750 | /* no need to optimize this case because it should almost | ||
751 | * never happen */ | ||
752 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 480 times.
|
480 | if (!s->channel_coded[0]) { |
753 | ff_tlog(s->avctx, "rare ms-stereo case happened\n"); | ||
754 | ✗ | memset(s->coefs[0], 0, sizeof(float) * s->block_len); | |
755 | ✗ | s->channel_coded[0] = 1; | |
756 | } | ||
757 | |||
758 | 480 | s->fdsp->butterflies_float(s->coefs[0], s->coefs[1], s->block_len); | |
759 | } | ||
760 | |||
761 | 106 | next: | |
762 | 586 | mdct = s->mdct_ctx[bsize]; | |
763 | 586 | mdct_fn = s->mdct_fn[bsize]; | |
764 | |||
765 |
2/2✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 586 times.
|
1758 | for (ch = 0; ch < channels; ch++) { |
766 | int n4, index; | ||
767 | |||
768 | 1172 | n4 = s->block_len / 2; | |
769 |
2/2✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 36 times.
|
1172 | if (s->channel_coded[ch]) |
770 | 1136 | mdct_fn(mdct, s->output, s->coefs[ch], sizeof(float)); | |
771 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
36 | else if (!(s->ms_stereo && ch == 1)) |
772 | 35 | memset(s->output, 0, sizeof(s->output)); | |
773 | |||
774 | /* multiply by the window and add in the frame */ | ||
775 | 1172 | index = (s->frame_len / 2) + s->block_pos - n4; | |
776 | 1172 | wma_window(s, &s->frame_out[ch][index]); | |
777 | } | ||
778 | |||
779 | /* update block number */ | ||
780 | 586 | s->block_num++; | |
781 | 586 | s->block_pos += s->block_len; | |
782 |
1/2✓ Branch 0 taken 586 times.
✗ Branch 1 not taken.
|
586 | if (s->block_pos >= s->frame_len) |
783 | 586 | return 1; | |
784 | else | ||
785 | ✗ | return 0; | |
786 | } | ||
787 | |||
788 | /* decode a frame of frame_len samples */ | ||
789 | 586 | static int wma_decode_frame(WMACodecContext *s, float **samples, | |
790 | int samples_offset) | ||
791 | { | ||
792 | int ret, ch; | ||
793 | |||
794 | #ifdef TRACE | ||
795 | ff_tlog(s->avctx, "***decode_frame: %d size=%d\n", | ||
796 | s->frame_count++, s->frame_len); | ||
797 | #endif /* TRACE */ | ||
798 | |||
799 | /* read each block */ | ||
800 | 586 | s->block_num = 0; | |
801 | 586 | s->block_pos = 0; | |
802 | for (;;) { | ||
803 | 586 | ret = wma_decode_block(s); | |
804 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 586 times.
|
586 | if (ret < 0) |
805 | ✗ | return -1; | |
806 |
1/2✓ Branch 0 taken 586 times.
✗ Branch 1 not taken.
|
586 | if (ret) |
807 | 586 | break; | |
808 | } | ||
809 | |||
810 |
2/2✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 586 times.
|
1758 | for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) { |
811 | /* copy current block to output */ | ||
812 | 1172 | memcpy(samples[ch] + samples_offset, s->frame_out[ch], | |
813 | 1172 | s->frame_len * sizeof(*s->frame_out[ch])); | |
814 | /* prepare for next block */ | ||
815 | 1172 | memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len], | |
816 | 1172 | s->frame_len * sizeof(*s->frame_out[ch])); | |
817 | |||
818 | #ifdef TRACE | ||
819 | dump_floats(s, "samples", 6, samples[ch] + samples_offset, | ||
820 | s->frame_len); | ||
821 | #endif /* TRACE */ | ||
822 | } | ||
823 | |||
824 | 586 | return 0; | |
825 | } | ||
826 | |||
827 | 593 | static int wma_decode_superframe(AVCodecContext *avctx, AVFrame *frame, | |
828 | int *got_frame_ptr, AVPacket *avpkt) | ||
829 | { | ||
830 | 593 | const uint8_t *buf = avpkt->data; | |
831 | 593 | int buf_size = avpkt->size; | |
832 | 593 | WMACodecContext *s = avctx->priv_data; | |
833 | int nb_frames, bit_offset, i, pos, len, ret; | ||
834 | uint8_t *q; | ||
835 | float **samples; | ||
836 | int samples_offset; | ||
837 | |||
838 | ff_tlog(avctx, "***decode_superframe:\n"); | ||
839 | |||
840 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 587 times.
|
593 | if (buf_size == 0) { |
841 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (s->eof_done) |
842 | 3 | return 0; | |
843 | |||
844 | 3 | frame->nb_samples = s->frame_len; | |
845 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
846 | ✗ | return ret; | |
847 | |||
848 | 3 | frame->pts = AV_NOPTS_VALUE; | |
849 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (i = 0; i < s->avctx->ch_layout.nb_channels; i++) |
850 | 6 | memcpy(frame->extended_data[i], &s->frame_out[i][0], | |
851 | 6 | frame->nb_samples * sizeof(s->frame_out[i][0])); | |
852 | |||
853 | 3 | s->last_superframe_len = 0; | |
854 | 3 | s->eof_done = 1; | |
855 | 3 | *got_frame_ptr = 1; | |
856 | 3 | return 0; | |
857 | } | ||
858 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 586 times.
|
587 | if (buf_size < avctx->block_align) { |
859 | 1 | av_log(avctx, AV_LOG_ERROR, | |
860 | "Input packet size too small (%d < %d)\n", | ||
861 | buf_size, avctx->block_align); | ||
862 | 1 | return AVERROR_INVALIDDATA; | |
863 | } | ||
864 |
1/2✓ Branch 0 taken 586 times.
✗ Branch 1 not taken.
|
586 | if (avctx->block_align) |
865 | 586 | buf_size = avctx->block_align; | |
866 | |||
867 | 586 | init_get_bits(&s->gb, buf, buf_size * 8); | |
868 | |||
869 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 586 times.
|
586 | if (s->use_bit_reservoir) { |
870 | /* read super frame header */ | ||
871 | ✗ | skip_bits(&s->gb, 4); /* super frame index */ | |
872 | ✗ | nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0); | |
873 | ✗ | if (nb_frames <= 0) { | |
874 | ✗ | int is_error = nb_frames < 0 || get_bits_left(&s->gb) <= 8; | |
875 | ✗ | av_log(avctx, is_error ? AV_LOG_ERROR : AV_LOG_WARNING, | |
876 | "nb_frames is %d bits left %d\n", | ||
877 | nb_frames, get_bits_left(&s->gb)); | ||
878 | ✗ | if (is_error) | |
879 | ✗ | return AVERROR_INVALIDDATA; | |
880 | |||
881 | ✗ | if ((s->last_superframe_len + buf_size - 1) > | |
882 | MAX_CODED_SUPERFRAME_SIZE) | ||
883 | ✗ | goto fail; | |
884 | |||
885 | ✗ | q = s->last_superframe + s->last_superframe_len; | |
886 | ✗ | len = buf_size - 1; | |
887 | ✗ | while (len > 0) { | |
888 | ✗ | *q++ = get_bits (&s->gb, 8); | |
889 | ✗ | len --; | |
890 | } | ||
891 | ✗ | memset(q, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
892 | |||
893 | ✗ | s->last_superframe_len += 8*buf_size - 8; | |
894 | // s->reset_block_lengths = 1; //XXX is this needed ? | ||
895 | ✗ | *got_frame_ptr = 0; | |
896 | ✗ | return buf_size; | |
897 | } | ||
898 | } else | ||
899 | 586 | nb_frames = 1; | |
900 | |||
901 | /* get output buffer */ | ||
902 | 586 | frame->nb_samples = nb_frames * s->frame_len; | |
903 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 586 times.
|
586 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
904 | ✗ | return ret; | |
905 | 586 | samples = (float **) frame->extended_data; | |
906 | 586 | samples_offset = 0; | |
907 | |||
908 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 586 times.
|
586 | if (s->use_bit_reservoir) { |
909 | ✗ | bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3); | |
910 | ✗ | if (bit_offset > get_bits_left(&s->gb)) { | |
911 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
912 | "Invalid last frame bit offset %d > buf size %d (%d)\n", | ||
913 | bit_offset, get_bits_left(&s->gb), buf_size); | ||
914 | ✗ | goto fail; | |
915 | } | ||
916 | |||
917 | ✗ | if (s->last_superframe_len > 0) { | |
918 | /* add bit_offset bits to last frame */ | ||
919 | ✗ | if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) > | |
920 | MAX_CODED_SUPERFRAME_SIZE) | ||
921 | ✗ | goto fail; | |
922 | ✗ | q = s->last_superframe + s->last_superframe_len; | |
923 | ✗ | len = bit_offset; | |
924 | ✗ | while (len > 7) { | |
925 | ✗ | *q++ = get_bits(&s->gb, 8); | |
926 | ✗ | len -= 8; | |
927 | } | ||
928 | ✗ | if (len > 0) | |
929 | ✗ | *q++ = get_bits(&s->gb, len) << (8 - len); | |
930 | ✗ | memset(q, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
931 | |||
932 | /* XXX: bit_offset bits into last frame */ | ||
933 | ✗ | init_get_bits(&s->gb, s->last_superframe, | |
934 | ✗ | s->last_superframe_len * 8 + bit_offset); | |
935 | /* skip unused bits */ | ||
936 | ✗ | if (s->last_bitoffset > 0) | |
937 | ✗ | skip_bits(&s->gb, s->last_bitoffset); | |
938 | /* this frame is stored in the last superframe and in the | ||
939 | * current one */ | ||
940 | ✗ | if (wma_decode_frame(s, samples, samples_offset) < 0) | |
941 | ✗ | goto fail; | |
942 | ✗ | samples_offset += s->frame_len; | |
943 | ✗ | nb_frames--; | |
944 | } | ||
945 | |||
946 | /* read each frame starting from bit_offset */ | ||
947 | ✗ | pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3; | |
948 | ✗ | if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8) | |
949 | ✗ | return AVERROR_INVALIDDATA; | |
950 | ✗ | init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3)) * 8); | |
951 | ✗ | len = pos & 7; | |
952 | ✗ | if (len > 0) | |
953 | ✗ | skip_bits(&s->gb, len); | |
954 | |||
955 | ✗ | s->reset_block_lengths = 1; | |
956 | ✗ | for (i = 0; i < nb_frames; i++) { | |
957 | ✗ | if (wma_decode_frame(s, samples, samples_offset) < 0) | |
958 | ✗ | goto fail; | |
959 | ✗ | samples_offset += s->frame_len; | |
960 | } | ||
961 | |||
962 | /* we copy the end of the frame in the last frame buffer */ | ||
963 | ✗ | pos = get_bits_count(&s->gb) + | |
964 | ✗ | ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7); | |
965 | ✗ | s->last_bitoffset = pos & 7; | |
966 | ✗ | pos >>= 3; | |
967 | ✗ | len = buf_size - pos; | |
968 | ✗ | if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) { | |
969 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len); | |
970 | ✗ | goto fail; | |
971 | } | ||
972 | ✗ | s->last_superframe_len = len; | |
973 | ✗ | memcpy(s->last_superframe, buf + pos, len); | |
974 | } else { | ||
975 | /* single frame decode */ | ||
976 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 586 times.
|
586 | if (wma_decode_frame(s, samples, samples_offset) < 0) |
977 | ✗ | goto fail; | |
978 | 586 | samples_offset += s->frame_len; | |
979 | } | ||
980 | |||
981 | ff_dlog(s->avctx, "%d %d %d %d eaten:%d\n", | ||
982 | s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len, | ||
983 | avctx->block_align); | ||
984 | |||
985 | 586 | *got_frame_ptr = 1; | |
986 | |||
987 | 586 | return buf_size; | |
988 | |||
989 | ✗ | fail: | |
990 | /* when error, we reset the bit reservoir */ | ||
991 | ✗ | s->last_superframe_len = 0; | |
992 | ✗ | return -1; | |
993 | } | ||
994 | |||
995 | 1 | static av_cold void flush(AVCodecContext *avctx) | |
996 | { | ||
997 | 1 | WMACodecContext *s = avctx->priv_data; | |
998 | |||
999 | 1 | s->last_bitoffset = | |
1000 | 1 | s->last_superframe_len = 0; | |
1001 | |||
1002 | 1 | s->eof_done = 0; | |
1003 | 1 | avctx->internal->skip_samples = s->frame_len * 2; | |
1004 | 1 | } | |
1005 | |||
1006 | #if CONFIG_WMAV1_DECODER | ||
1007 | const FFCodec ff_wmav1_decoder = { | ||
1008 | .p.name = "wmav1", | ||
1009 | CODEC_LONG_NAME("Windows Media Audio 1"), | ||
1010 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
1011 | .p.id = AV_CODEC_ID_WMAV1, | ||
1012 | .priv_data_size = sizeof(WMACodecContext), | ||
1013 | .init = wma_decode_init, | ||
1014 | .close = ff_wma_end, | ||
1015 | FF_CODEC_DECODE_CB(wma_decode_superframe), | ||
1016 | .flush = flush, | ||
1017 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, | ||
1018 | .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, | ||
1019 | AV_SAMPLE_FMT_NONE }, | ||
1020 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1021 | }; | ||
1022 | #endif | ||
1023 | #if CONFIG_WMAV2_DECODER | ||
1024 | const FFCodec ff_wmav2_decoder = { | ||
1025 | .p.name = "wmav2", | ||
1026 | CODEC_LONG_NAME("Windows Media Audio 2"), | ||
1027 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
1028 | .p.id = AV_CODEC_ID_WMAV2, | ||
1029 | .priv_data_size = sizeof(WMACodecContext), | ||
1030 | .init = wma_decode_init, | ||
1031 | .close = ff_wma_end, | ||
1032 | FF_CODEC_DECODE_CB(wma_decode_superframe), | ||
1033 | .flush = flush, | ||
1034 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, | ||
1035 | .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, | ||
1036 | AV_SAMPLE_FMT_NONE }, | ||
1037 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1038 | }; | ||
1039 | #endif | ||
1040 |