2 * On2 Audio for Video Codec decoder
4 * Copyright (c) 2013 Konstantin Shishkov
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/float_dsp.h"
26 #include "bytestream.h"
33 #include "on2avcdata.h"
35 #define ON2AVC_SUBFRAME_SIZE 1024
39 WINDOW_TYPE_LONG_STOP,
40 WINDOW_TYPE_LONG_START,
41 WINDOW_TYPE_8SHORT = 3,
48 typedef struct On2AVCContext {
49 AVCodecContext *avctx;
50 AVFloatDSPContext fdsp;
51 FFTContext mdct, mdct_half, mdct_small;
52 FFTContext fft128, fft256, fft512, fft1024;
53 void (*wtf)(struct On2AVCContext *ctx, float *out, float *in, int size);
57 const On2AVCMode *modes;
58 int window_type, prev_window_type;
59 int num_windows, num_bands;
61 const int *band_start;
65 int ms_info[ON2AVC_MAX_BANDS];
69 uint8_t band_type[ON2AVC_MAX_BANDS];
70 uint8_t band_run_end[ON2AVC_MAX_BANDS];
73 float band_scales[ON2AVC_MAX_BANDS];
80 DECLARE_ALIGNED(32, float, coeffs)[2][ON2AVC_SUBFRAME_SIZE];
81 DECLARE_ALIGNED(32, float, delay) [2][ON2AVC_SUBFRAME_SIZE];
83 DECLARE_ALIGNED(32, float, temp) [ON2AVC_SUBFRAME_SIZE * 2];
84 DECLARE_ALIGNED(32, float, mdct_buf) [ON2AVC_SUBFRAME_SIZE];
85 DECLARE_ALIGNED(32, float, long_win) [ON2AVC_SUBFRAME_SIZE];
86 DECLARE_ALIGNED(32, float, short_win)[ON2AVC_SUBFRAME_SIZE / 8];
89 static void on2avc_read_ms_info(On2AVCContext *c, GetBitContext *gb)
91 int w, b, band_off = 0;
93 c->ms_present = get_bits1(gb);
96 for (w = 0; w < c->num_windows; w++) {
97 if (!c->grouping[w]) {
98 memcpy(c->ms_info + band_off,
99 c->ms_info + band_off - c->num_bands,
100 c->num_bands * sizeof(*c->ms_info));
101 band_off += c->num_bands;
104 for (b = 0; b < c->num_bands; b++)
105 c->ms_info[band_off++] = get_bits1(gb);
109 // do not see Table 17 in ISO/IEC 13818-7
110 static int on2avc_decode_band_types(On2AVCContext *c, GetBitContext *gb)
112 int bits_per_sect = c->is_long ? 5 : 3;
113 int esc_val = (1 << bits_per_sect) - 1;
114 int num_bands = c->num_bands * c->num_windows;
115 int band = 0, i, band_type, run_len, run;
117 while (band < num_bands) {
118 band_type = get_bits(gb, 4);
121 run = get_bits(gb, bits_per_sect);
123 } while (run == esc_val);
124 if (band + run_len > num_bands) {
125 av_log(c->avctx, AV_LOG_ERROR, "Invalid band type run\n");
126 return AVERROR_INVALIDDATA;
128 for (i = band; i < band + run_len; i++) {
129 c->band_type[i] = band_type;
130 c->band_run_end[i] = band + run_len;
138 // completely not like Table 18 in ISO/IEC 13818-7
139 // (no intensity stereo, different coding for the first coefficient)
140 static int on2avc_decode_band_scales(On2AVCContext *c, GetBitContext *gb)
142 int w, w2, b, scale, first = 1;
145 for (w = 0; w < c->num_windows; w++) {
146 if (!c->grouping[w]) {
147 memcpy(c->band_scales + band_off,
148 c->band_scales + band_off - c->num_bands,
149 c->num_bands * sizeof(*c->band_scales));
150 band_off += c->num_bands;
153 for (b = 0; b < c->num_bands; b++) {
154 if (!c->band_type[band_off]) {
156 for (w2 = w + 1; w2 < c->num_windows; w2++) {
159 if (c->band_type[w2 * c->num_bands + b]) {
165 c->band_scales[band_off++] = 0;
170 scale = get_bits(gb, 7);
173 scale += get_vlc2(gb, c->scale_diff.table, 9, 3) - 60;
175 if (scale < 0 || scale > 127) {
176 av_log(c->avctx, AV_LOG_ERROR, "Invalid scale value %d\n",
178 return AVERROR_INVALIDDATA;
180 c->band_scales[band_off++] = c->scale_tab[scale];
187 static inline float on2avc_scale(int v, float scale)
189 return v * sqrtf(fabsf(v)) * scale;
192 // spectral data is coded completely differently - there are no unsigned codebooks
193 static int on2avc_decode_quads(On2AVCContext *c, GetBitContext *gb, float *dst,
194 int dst_size, int type, float band_scale)
198 for (i = 0; i < dst_size; i += 4) {
199 val = get_vlc2(gb, c->cb_vlc[type].table, 9, 3);
201 for (j = 0; j < 4; j++) {
202 val1 = sign_extend((val >> (12 - j * 4)) & 0xF, 4);
203 *dst++ = on2avc_scale(val1, band_scale);
210 static inline int get_egolomb(GetBitContext *gb)
214 while (get_bits1(gb)) v++;
216 return (1 << v) + get_bits(gb, v);
219 static int on2avc_decode_pairs(On2AVCContext *c, GetBitContext *gb, float *dst,
220 int dst_size, int type, float band_scale)
222 int i, val, val1, val2, sign;
224 for (i = 0; i < dst_size; i += 2) {
225 val = get_vlc2(gb, c->cb_vlc[type].table, 9, 3);
227 val1 = sign_extend(val >> 8, 8);
228 val2 = sign_extend(val & 0xFF, 8);
229 if (type == ON2AVC_ESC_CB) {
230 if (val1 <= -16 || val1 >= 16) {
231 sign = 1 - (val1 < 0) * 2;
232 val1 = sign * get_egolomb(gb);
234 if (val2 <= -16 || val2 >= 16) {
235 sign = 1 - (val2 < 0) * 2;
236 val2 = sign * get_egolomb(gb);
240 *dst++ = on2avc_scale(val1, band_scale);
241 *dst++ = on2avc_scale(val2, band_scale);
247 static int on2avc_read_channel_data(On2AVCContext *c, GetBitContext *gb, int ch)
253 if ((ret = on2avc_decode_band_types(c, gb)) < 0)
255 if ((ret = on2avc_decode_band_scales(c, gb)) < 0)
258 coeff_ptr = c->coeffs[ch];
260 memset(coeff_ptr, 0, ON2AVC_SUBFRAME_SIZE * sizeof(*coeff_ptr));
261 for (w = 0; w < c->num_windows; w++) {
262 for (b = 0; b < c->num_bands; b++) {
263 int band_size = c->band_start[b + 1] - c->band_start[b];
264 int band_type = c->band_type[band_idx + b];
267 coeff_ptr += band_size;
271 on2avc_decode_quads(c, gb, coeff_ptr, band_size, band_type,
272 c->band_scales[band_idx + b]);
274 on2avc_decode_pairs(c, gb, coeff_ptr, band_size, band_type,
275 c->band_scales[band_idx + b]);
276 coeff_ptr += band_size;
278 band_idx += c->num_bands;
284 static int on2avc_apply_ms(On2AVCContext *c)
288 float *ch0 = c->coeffs[0];
289 float *ch1 = c->coeffs[1];
291 for (w = 0; w < c->num_windows; w++) {
292 for (b = 0; b < c->num_bands; b++) {
293 if (c->ms_info[band_off + b]) {
294 for (i = c->band_start[b]; i < c->band_start[b + 1]; i++) {
295 float l = *ch0, r = *ch1;
300 ch0 += c->band_start[b + 1] - c->band_start[b];
301 ch1 += c->band_start[b + 1] - c->band_start[b];
304 band_off += c->num_bands;
309 static void zero_head_and_tail(float *src, int len, int order0, int order1)
311 memset(src, 0, sizeof(*src) * order0);
312 memset(src + len - order1, 0, sizeof(*src) * order1);
315 static void pretwiddle(float *src, float *dst, int dst_len, int tab_step,
316 int step, int order0, int order1, const double **tabs)
324 for (i = 0; i < tab_step; i++) {
326 for (j = 0; j < order0; j++)
327 sum += src[j] * tab[j * tab_step + i];
331 out = dst + dst_len - tab_step;
333 src2 = src + (dst_len - tab_step) / step + 1 + order0;
334 for (i = 0; i < tab_step; i++) {
336 for (j = 0; j < order1; j++)
337 sum += src2[j] * tab[j * tab_step + i];
342 static void twiddle(float *src1, float *src2, int src2_len,
343 const double *tab, int tab_len, int step,
344 int order0, int order1, const double **tabs)
350 steps = (src2_len - tab_len) / step + 1;
351 pretwiddle(src1, src2, src2_len, tab_len, step, order0, order1, tabs);
354 for (i = 0; i < steps; i++) {
355 float in0 = src1[order0 + i];
356 int pos = (src2_len - 1) & mask;
359 const double *t = tab;
360 for (j = pos; j >= 0; j--)
361 src2[j] += in0 * *t++;
362 for (j = 0; j < tab_len - pos - 1; j++)
363 src2[src2_len - j - 1] += in0 * tab[pos + 1 + j];
365 for (j = 0; j < tab_len; j++)
366 src2[pos - j] += in0 * tab[j];
372 #define CMUL1_R(s, t, is, it) \
373 s[is + 0] * t[it + 0] - s[is + 1] * t[it + 1]
374 #define CMUL1_I(s, t, is, it) \
375 s[is + 0] * t[it + 1] + s[is + 1] * t[it + 0]
376 #define CMUL2_R(s, t, is, it) \
377 s[is + 0] * t[it + 0] + s[is + 1] * t[it + 1]
378 #define CMUL2_I(s, t, is, it) \
379 s[is + 0] * t[it + 1] - s[is + 1] * t[it + 0]
381 #define CMUL0(dst, id, s0, s1, s2, s3, t0, t1, t2, t3, is, it) \
382 dst[id] = s0[is] * t0[it] + s1[is] * t1[it] \
383 + s2[is] * t2[it] + s3[is] * t3[it]; \
384 dst[id + 1] = s0[is] * t0[it + 1] + s1[is] * t1[it + 1] \
385 + s2[is] * t2[it + 1] + s3[is] * t3[it + 1];
387 #define CMUL1(dst, s0, s1, s2, s3, t0, t1, t2, t3, is, it) \
388 *dst++ = CMUL1_R(s0, t0, is, it) \
389 + CMUL1_R(s1, t1, is, it) \
390 + CMUL1_R(s2, t2, is, it) \
391 + CMUL1_R(s3, t3, is, it); \
392 *dst++ = CMUL1_I(s0, t0, is, it) \
393 + CMUL1_I(s1, t1, is, it) \
394 + CMUL1_I(s2, t2, is, it) \
395 + CMUL1_I(s3, t3, is, it);
397 #define CMUL2(dst, s0, s1, s2, s3, t0, t1, t2, t3, is, it) \
398 *dst++ = CMUL2_R(s0, t0, is, it) \
399 + CMUL2_R(s1, t1, is, it) \
400 + CMUL2_R(s2, t2, is, it) \
401 + CMUL2_R(s3, t3, is, it); \
402 *dst++ = CMUL2_I(s0, t0, is, it) \
403 + CMUL2_I(s1, t1, is, it) \
404 + CMUL2_I(s2, t2, is, it) \
405 + CMUL2_I(s3, t3, is, it);
407 static void combine_fft(float *s0, float *s1, float *s2, float *s3, float *dst,
408 const float *t0, const float *t1,
409 const float *t2, const float *t3, int len, int step)
411 const float *h0, *h1, *h2, *h3;
414 int len2 = len >> 1, len4 = len >> 2;
419 for (half = len2; tmp > 1; half <<= 1, tmp >>= 1);
426 CMUL0(dst, 0, s0, s1, s2, s3, t0, t1, t2, t3, 0, 0);
428 hoff = 2 * step * (len4 >> 1);
433 d2 = dst + 2 + (len >> 1);
434 for (i = 0; i < (len4 - 1) >> 1; i++) {
435 CMUL1(d1, s0, s1, s2, s3, t0, t1, t2, t3, j, k);
436 CMUL1(d2, s0, s1, s2, s3, h0, h1, h2, h3, j, k);
440 CMUL0(dst, len4, s0, s1, s2, s3, t0, t1, t2, t3, 1, hoff);
441 CMUL0(dst, len4 + len2, s0, s1, s2, s3, h0, h1, h2, h3, 1, hoff);
444 k = hoff + 2 * step * len4;
446 d2 = dst + len4 + 2 + len2;
447 for (i = 0; i < (len4 - 2) >> 1; i++) {
448 CMUL2(d1, s0, s1, s2, s3, t0, t1, t2, t3, j, k);
449 CMUL2(d2, s0, s1, s2, s3, h0, h1, h2, h3, j, k);
453 CMUL0(dst, len2 + 4, s0, s1, s2, s3, t0, t1, t2, t3, 0, k);
456 static void wtf_end_512(On2AVCContext *c, float *out, float *src,
457 float *tmp0, float *tmp1)
459 memcpy(src, tmp0, 384 * sizeof(*tmp0));
460 memcpy(tmp0 + 384, src + 384, 128 * sizeof(*tmp0));
462 zero_head_and_tail(src, 128, 16, 4);
463 zero_head_and_tail(src + 128, 128, 16, 4);
464 zero_head_and_tail(src + 256, 128, 13, 7);
465 zero_head_and_tail(src + 384, 128, 15, 5);
467 c->fft128.fft_permute(&c->fft128, (FFTComplex*)src);
468 c->fft128.fft_permute(&c->fft128, (FFTComplex*)(src + 128));
469 c->fft128.fft_permute(&c->fft128, (FFTComplex*)(src + 256));
470 c->fft128.fft_permute(&c->fft128, (FFTComplex*)(src + 384));
471 c->fft128.fft_calc(&c->fft128, (FFTComplex*)src);
472 c->fft128.fft_calc(&c->fft128, (FFTComplex*)(src + 128));
473 c->fft128.fft_calc(&c->fft128, (FFTComplex*)(src + 256));
474 c->fft128.fft_calc(&c->fft128, (FFTComplex*)(src + 384));
475 combine_fft(src, src + 128, src + 256, src + 384, tmp1,
476 ff_on2avc_ctab_1, ff_on2avc_ctab_2,
477 ff_on2avc_ctab_3, ff_on2avc_ctab_4, 512, 2);
478 c->fft512.fft_permute(&c->fft512, (FFTComplex*)tmp1);
479 c->fft512.fft_calc(&c->fft512, (FFTComplex*)tmp1);
481 pretwiddle(&tmp0[ 0], tmp1, 512, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
482 pretwiddle(&tmp0[128], tmp1, 512, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
483 pretwiddle(&tmp0[256], tmp1, 512, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
484 pretwiddle(&tmp0[384], tmp1, 512, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
486 memcpy(src, tmp1, 512 * sizeof(float));
489 static void wtf_end_1024(On2AVCContext *c, float *out, float *src,
490 float *tmp0, float *tmp1)
492 memcpy(src, tmp0, 768 * sizeof(*tmp0));
493 memcpy(tmp0 + 768, src + 768, 256 * sizeof(*tmp0));
495 zero_head_and_tail(src, 256, 16, 4);
496 zero_head_and_tail(src + 256, 256, 16, 4);
497 zero_head_and_tail(src + 512, 256, 13, 7);
498 zero_head_and_tail(src + 768, 256, 15, 5);
500 c->fft256.fft_permute(&c->fft256, (FFTComplex*)src);
501 c->fft256.fft_permute(&c->fft256, (FFTComplex*)(src + 256));
502 c->fft256.fft_permute(&c->fft256, (FFTComplex*)(src + 512));
503 c->fft256.fft_permute(&c->fft256, (FFTComplex*)(src + 768));
504 c->fft256.fft_calc(&c->fft256, (FFTComplex*)src);
505 c->fft256.fft_calc(&c->fft256, (FFTComplex*)(src + 256));
506 c->fft256.fft_calc(&c->fft256, (FFTComplex*)(src + 512));
507 c->fft256.fft_calc(&c->fft256, (FFTComplex*)(src + 768));
508 combine_fft(src, src + 256, src + 512, src + 768, tmp1,
509 ff_on2avc_ctab_1, ff_on2avc_ctab_2,
510 ff_on2avc_ctab_3, ff_on2avc_ctab_4, 1024, 1);
511 c->fft1024.fft_permute(&c->fft1024, (FFTComplex*)tmp1);
512 c->fft1024.fft_calc(&c->fft1024, (FFTComplex*)tmp1);
514 pretwiddle(&tmp0[ 0], tmp1, 1024, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
515 pretwiddle(&tmp0[256], tmp1, 1024, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
516 pretwiddle(&tmp0[512], tmp1, 1024, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
517 pretwiddle(&tmp0[768], tmp1, 1024, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
519 memcpy(src, tmp1, 1024 * sizeof(float));
522 static void wtf_40(On2AVCContext *c, float *out, float *src, int size)
524 float *tmp0 = c->temp, *tmp1 = c->temp + 1024;
526 memset(tmp0, 0, sizeof(*tmp0) * 1024);
527 memset(tmp1, 0, sizeof(*tmp1) * 1024);
530 twiddle(src, &tmp0[ 0], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
531 twiddle(src + 8, &tmp0[ 0], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
532 twiddle(src + 16, &tmp0[ 16], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
533 twiddle(src + 24, &tmp0[ 16], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
534 twiddle(src + 32, &tmp0[ 32], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
535 twiddle(src + 40, &tmp0[ 32], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
536 twiddle(src + 48, &tmp0[ 48], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
537 twiddle(src + 56, &tmp0[ 48], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
538 twiddle(&tmp0[ 0], &tmp1[ 0], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
539 twiddle(&tmp0[16], &tmp1[ 0], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
540 twiddle(&tmp0[32], &tmp1[ 32], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
541 twiddle(&tmp0[48], &tmp1[ 32], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
542 twiddle(src + 64, &tmp1[ 64], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
543 twiddle(src + 80, &tmp1[ 64], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
544 twiddle(src + 96, &tmp1[ 96], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
545 twiddle(src + 112, &tmp1[ 96], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
546 twiddle(src + 128, &tmp1[128], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
547 twiddle(src + 144, &tmp1[128], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
548 twiddle(src + 160, &tmp1[160], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
549 twiddle(src + 176, &tmp1[160], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
551 memset(tmp0, 0, 64 * sizeof(*tmp0));
553 twiddle(&tmp1[ 0], &tmp0[ 0], 128, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
554 twiddle(&tmp1[ 32], &tmp0[ 0], 128, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
555 twiddle(&tmp1[ 64], &tmp0[ 0], 128, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
556 twiddle(&tmp1[ 96], &tmp0[ 0], 128, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
557 twiddle(&tmp1[128], &tmp0[128], 128, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
558 twiddle(&tmp1[160], &tmp0[128], 128, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
559 twiddle(src + 192, &tmp0[128], 128, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
560 twiddle(src + 224, &tmp0[128], 128, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
561 twiddle(src + 256, &tmp0[256], 128, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
562 twiddle(src + 288, &tmp0[256], 128, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
563 twiddle(src + 320, &tmp0[256], 128, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
564 twiddle(src + 352, &tmp0[256], 128, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
566 wtf_end_512(c, out, src, tmp0, tmp1);
568 twiddle(src, &tmp0[ 0], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
569 twiddle(src + 16, &tmp0[ 0], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
570 twiddle(src + 32, &tmp0[ 32], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
571 twiddle(src + 48, &tmp0[ 32], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
572 twiddle(src + 64, &tmp0[ 64], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
573 twiddle(src + 80, &tmp0[ 64], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
574 twiddle(src + 96, &tmp0[ 96], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
575 twiddle(src + 112, &tmp0[ 96], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
576 twiddle(&tmp0[ 0], &tmp1[ 0], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
577 twiddle(&tmp0[32], &tmp1[ 0], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
578 twiddle(&tmp0[64], &tmp1[ 64], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
579 twiddle(&tmp0[96], &tmp1[ 64], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
580 twiddle(src + 128, &tmp1[128], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
581 twiddle(src + 160, &tmp1[128], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
582 twiddle(src + 192, &tmp1[192], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
583 twiddle(src + 224, &tmp1[192], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
584 twiddle(src + 256, &tmp1[256], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
585 twiddle(src + 288, &tmp1[256], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
586 twiddle(src + 320, &tmp1[320], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
587 twiddle(src + 352, &tmp1[320], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
589 memset(tmp0, 0, 128 * sizeof(*tmp0));
591 twiddle(&tmp1[ 0], &tmp0[ 0], 256, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
592 twiddle(&tmp1[ 64], &tmp0[ 0], 256, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
593 twiddle(&tmp1[128], &tmp0[ 0], 256, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
594 twiddle(&tmp1[192], &tmp0[ 0], 256, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
595 twiddle(&tmp1[256], &tmp0[256], 256, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
596 twiddle(&tmp1[320], &tmp0[256], 256, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
597 twiddle(src + 384, &tmp0[256], 256, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
598 twiddle(src + 448, &tmp0[256], 256, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
599 twiddle(src + 512, &tmp0[512], 256, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
600 twiddle(src + 576, &tmp0[512], 256, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
601 twiddle(src + 640, &tmp0[512], 256, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
602 twiddle(src + 704, &tmp0[512], 256, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
604 wtf_end_1024(c, out, src, tmp0, tmp1);
608 static void wtf_44(On2AVCContext *c, float *out, float *src, int size)
610 float *tmp0 = c->temp, *tmp1 = c->temp + 1024;
612 memset(tmp0, 0, sizeof(*tmp0) * 1024);
613 memset(tmp1, 0, sizeof(*tmp1) * 1024);
616 twiddle(src, &tmp0[ 0], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
617 twiddle(src + 8, &tmp0[ 0], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
618 twiddle(src + 16, &tmp0[16], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
619 twiddle(src + 24, &tmp0[16], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
620 twiddle(src + 32, &tmp0[32], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
621 twiddle(src + 40, &tmp0[32], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
622 twiddle(src + 48, &tmp0[48], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
623 twiddle(src + 56, &tmp0[48], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
624 twiddle(&tmp0[ 0], &tmp1[ 0], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
625 twiddle(&tmp0[16], &tmp1[ 0], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
626 twiddle(&tmp0[32], &tmp1[32], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
627 twiddle(&tmp0[48], &tmp1[32], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
628 twiddle(src + 64, &tmp1[64], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
629 twiddle(src + 80, &tmp1[64], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
630 twiddle(src + 96, &tmp1[96], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
631 twiddle(src + 112, &tmp1[96], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
633 memset(tmp0, 0, 64 * sizeof(*tmp0));
635 twiddle(&tmp1[ 0], &tmp0[ 0], 128, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
636 twiddle(&tmp1[32], &tmp0[ 0], 128, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
637 twiddle(&tmp1[64], &tmp0[ 0], 128, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
638 twiddle(&tmp1[96], &tmp0[ 0], 128, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
639 twiddle(src + 128, &tmp0[128], 128, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
640 twiddle(src + 160, &tmp0[128], 128, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
641 twiddle(src + 192, &tmp0[128], 128, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
642 twiddle(src + 224, &tmp0[128], 128, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
643 twiddle(src + 256, &tmp0[256], 128, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
644 twiddle(src + 320, &tmp0[256], 128, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
646 wtf_end_512(c, out, src, tmp0, tmp1);
648 twiddle(src, &tmp0[ 0], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
649 twiddle(src + 16, &tmp0[ 0], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
650 twiddle(src + 32, &tmp0[ 32], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
651 twiddle(src + 48, &tmp0[ 32], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
652 twiddle(src + 64, &tmp0[ 64], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
653 twiddle(src + 80, &tmp0[ 64], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
654 twiddle(src + 96, &tmp0[ 96], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
655 twiddle(src + 112, &tmp0[ 96], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
656 twiddle(&tmp0[ 0], &tmp1[ 0], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
657 twiddle(&tmp0[32], &tmp1[ 0], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
658 twiddle(&tmp0[64], &tmp1[ 64], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
659 twiddle(&tmp0[96], &tmp1[ 64], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
660 twiddle(src + 128, &tmp1[128], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
661 twiddle(src + 160, &tmp1[128], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
662 twiddle(src + 192, &tmp1[192], 64, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
663 twiddle(src + 224, &tmp1[192], 64, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
665 memset(tmp0, 0, 128 * sizeof(*tmp0));
667 twiddle(&tmp1[ 0], &tmp0[ 0], 256, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
668 twiddle(&tmp1[ 64], &tmp0[ 0], 256, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
669 twiddle(&tmp1[128], &tmp0[ 0], 256, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
670 twiddle(&tmp1[192], &tmp0[ 0], 256, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
671 twiddle(src + 256, &tmp0[256], 256, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
672 twiddle(src + 320, &tmp0[256], 256, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
673 twiddle(src + 384, &tmp0[256], 256, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
674 twiddle(src + 448, &tmp0[256], 256, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
675 twiddle(src + 512, &tmp0[512], 256, ff_on2avc_tab_40_1, 40, 2, 11, 8, ff_on2avc_tabs_19_40_1);
676 twiddle(src + 640, &tmp0[512], 256, ff_on2avc_tab_40_2, 40, 2, 8, 11, ff_on2avc_tabs_19_40_2);
678 wtf_end_1024(c, out, src, tmp0, tmp1);
682 static int on2avc_reconstruct_stereo(On2AVCContext *c, AVFrame *dst, int offset)
686 for (ch = 0; ch < 2; ch++) {
687 float *out = (float*)dst->extended_data[ch] + offset;
688 float *in = c->coeffs[ch];
689 float *saved = c->delay[ch];
690 float *buf = c->mdct_buf;
691 float *wout = out + 448;
693 switch (c->window_type) {
694 case WINDOW_TYPE_EXT7:
695 c->mdct.imdct_half(&c->mdct, buf, in);
697 case WINDOW_TYPE_EXT4:
698 c->wtf(c, buf, in, 1024);
700 case WINDOW_TYPE_EXT5:
701 c->wtf(c, buf, in, 512);
702 c->mdct.imdct_half(&c->mdct_half, buf + 512, in + 512);
703 for (i = 0; i < 256; i++) {
704 FFSWAP(float, buf[i + 512], buf[1023 - i]);
707 case WINDOW_TYPE_EXT6:
708 c->mdct.imdct_half(&c->mdct_half, buf, in);
709 for (i = 0; i < 256; i++) {
710 FFSWAP(float, buf[i], buf[511 - i]);
712 c->wtf(c, buf + 512, in + 512, 512);
716 memcpy(out, saved, 448 * sizeof(float));
717 c->fdsp.vector_fmul_window(wout, saved + 448, buf, c->short_win, 64);
718 memcpy(wout + 128, buf + 64, 448 * sizeof(float));
719 memcpy(saved, buf + 512, 448 * sizeof(float));
720 memcpy(saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
726 // not borrowed from aacdec.c - the codec has original design after all
727 static int on2avc_reconstruct_channel(On2AVCContext *c, int channel,
728 AVFrame *dst, int offset)
731 float *out = (float*)dst->extended_data[channel] + offset;
732 float *in = c->coeffs[channel];
733 float *saved = c->delay[channel];
734 float *buf = c->mdct_buf;
735 float *temp = c->temp;
737 switch (c->window_type) {
738 case WINDOW_TYPE_LONG_START:
739 case WINDOW_TYPE_LONG_STOP:
740 case WINDOW_TYPE_LONG:
741 c->mdct.imdct_half(&c->mdct, buf, in);
743 case WINDOW_TYPE_8SHORT:
744 for (i = 0; i < ON2AVC_SUBFRAME_SIZE; i += ON2AVC_SUBFRAME_SIZE / 8)
745 c->mdct_small.imdct_half(&c->mdct_small, buf + i, in + i);
749 if ((c->prev_window_type == WINDOW_TYPE_LONG ||
750 c->prev_window_type == WINDOW_TYPE_LONG_STOP) &&
751 (c->window_type == WINDOW_TYPE_LONG ||
752 c->window_type == WINDOW_TYPE_LONG_START)) {
753 c->fdsp.vector_fmul_window(out, saved, buf, c->long_win, 512);
755 float *wout = out + 448;
756 memcpy(out, saved, 448 * sizeof(float));
758 if (c->window_type == WINDOW_TYPE_8SHORT) {
759 c->fdsp.vector_fmul_window(wout + 0*128, saved + 448, buf + 0*128, c->short_win, 64);
760 c->fdsp.vector_fmul_window(wout + 1*128, buf + 0*128 + 64, buf + 1*128, c->short_win, 64);
761 c->fdsp.vector_fmul_window(wout + 2*128, buf + 1*128 + 64, buf + 2*128, c->short_win, 64);
762 c->fdsp.vector_fmul_window(wout + 3*128, buf + 2*128 + 64, buf + 3*128, c->short_win, 64);
763 c->fdsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, c->short_win, 64);
764 memcpy(wout + 4*128, temp, 64 * sizeof(float));
766 c->fdsp.vector_fmul_window(wout, saved + 448, buf, c->short_win, 64);
767 memcpy(wout + 128, buf + 64, 448 * sizeof(float));
772 switch (c->window_type) {
773 case WINDOW_TYPE_8SHORT:
774 memcpy(saved, temp + 64, 64 * sizeof(float));
775 c->fdsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, c->short_win, 64);
776 c->fdsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, c->short_win, 64);
777 c->fdsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, c->short_win, 64);
778 memcpy(saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
780 case WINDOW_TYPE_LONG_START:
781 memcpy(saved, buf + 512, 448 * sizeof(float));
782 memcpy(saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
784 case WINDOW_TYPE_LONG_STOP:
785 case WINDOW_TYPE_LONG:
786 memcpy(saved, buf + 512, 512 * sizeof(float));
792 static int on2avc_decode_subframe(On2AVCContext *c, const uint8_t *buf,
793 int buf_size, AVFrame *dst, int offset)
798 init_get_bits(&gb, buf, buf_size * 8);
799 if (get_bits1(&gb)) {
800 av_log(c->avctx, AV_LOG_ERROR, "enh bit set\n");
801 return AVERROR_INVALIDDATA;
803 c->prev_window_type = c->window_type;
804 c->window_type = get_bits(&gb, 3);
805 if (c->window_type >= WINDOW_TYPE_EXT4 && c->avctx->channels == 1) {
806 av_log(c->avctx, AV_LOG_ERROR, "stereo mode window for mono audio\n");
807 return AVERROR_INVALIDDATA;
810 c->band_start = c->modes[c->window_type].band_start;
811 c->num_windows = c->modes[c->window_type].num_windows;
812 c->num_bands = c->modes[c->window_type].num_bands;
813 c->is_long = (c->window_type != WINDOW_TYPE_8SHORT);
816 for (i = 1; i < c->num_windows; i++)
817 c->grouping[i] = !get_bits1(&gb);
819 on2avc_read_ms_info(c, &gb);
820 for (i = 0; i < c->avctx->channels; i++)
821 if ((ret = on2avc_read_channel_data(c, &gb, i)) < 0)
822 return AVERROR_INVALIDDATA;
823 if (c->avctx->channels == 2 && c->ms_present)
825 if (c->window_type < WINDOW_TYPE_EXT4) {
826 for (i = 0; i < c->avctx->channels; i++)
827 on2avc_reconstruct_channel(c, i, dst, offset);
829 on2avc_reconstruct_stereo(c, dst, offset);
835 static int on2avc_decode_frame(AVCodecContext * avctx, void *data,
836 int *got_frame_ptr, AVPacket *avpkt)
838 AVFrame *frame = data;
839 const uint8_t *buf = avpkt->data;
840 int buf_size = avpkt->size;
841 On2AVCContext *c = avctx->priv_data;
843 int num_frames = 0, frame_size, audio_off;
847 /* get output buffer */
848 frame->nb_samples = ON2AVC_SUBFRAME_SIZE;
849 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
850 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
854 if ((ret = on2avc_decode_subframe(c, buf, buf_size, frame, 0)) < 0)
857 bytestream2_init(&gb, buf, buf_size);
858 while (bytestream2_get_bytes_left(&gb) > 2) {
859 frame_size = bytestream2_get_le16(&gb);
860 if (!frame_size || frame_size > bytestream2_get_bytes_left(&gb)) {
861 av_log(avctx, AV_LOG_ERROR, "Invalid subframe size %d\n",
863 return AVERROR_INVALIDDATA;
866 bytestream2_skip(&gb, frame_size);
869 av_log(avctx, AV_LOG_ERROR, "No subframes present\n");
870 return AVERROR_INVALIDDATA;
873 /* get output buffer */
874 frame->nb_samples = ON2AVC_SUBFRAME_SIZE * num_frames;
875 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
876 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
881 bytestream2_init(&gb, buf, buf_size);
882 while (bytestream2_get_bytes_left(&gb) > 2) {
883 frame_size = bytestream2_get_le16(&gb);
884 if ((ret = on2avc_decode_subframe(c, gb.buffer, frame_size,
885 frame, audio_off)) < 0)
887 audio_off += ON2AVC_SUBFRAME_SIZE;
888 bytestream2_skip(&gb, frame_size);
897 static av_cold void on2avc_free_vlcs(On2AVCContext *c)
901 ff_free_vlc(&c->scale_diff);
902 for (i = 1; i < 16; i++)
903 ff_free_vlc(&c->cb_vlc[i]);
906 static av_cold int on2avc_decode_init(AVCodecContext *avctx)
908 On2AVCContext *c = avctx->priv_data;
912 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
913 avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
916 c->is_av500 = (avctx->codec_tag == 0x500);
917 if (c->is_av500 && avctx->channels == 2) {
918 av_log(avctx, AV_LOG_ERROR, "0x500 version should be mono\n");
919 return AVERROR_INVALIDDATA;
921 if (avctx->channels > 2) {
922 av_log(avctx, AV_LOG_ERROR, "Only 1 or 2 channels are supported.\n");
923 return AVERROR(EINVAL);
925 if (avctx->channels == 2)
926 av_log(avctx, AV_LOG_WARNING,
927 "Stereo mode support is not good, patch is welcome\n");
929 for (i = 0; i < 20; i++)
930 c->scale_tab[i] = ceil(pow(10.0, i * 0.1) * 16) / 32;
932 c->scale_tab[i] = ceil(pow(10.0, i * 0.1) * 0.5);
934 if (avctx->sample_rate < 32000 || avctx->channels == 1)
935 memcpy(c->long_win, ff_on2avc_window_long_24000,
936 1024 * sizeof(*c->long_win));
938 memcpy(c->long_win, ff_on2avc_window_long_32000,
939 1024 * sizeof(*c->long_win));
940 memcpy(c->short_win, ff_on2avc_window_short, 128 * sizeof(*c->short_win));
942 c->modes = (avctx->sample_rate <= 40000) ? ff_on2avc_modes_40
943 : ff_on2avc_modes_44;
944 c->wtf = (avctx->sample_rate <= 40000) ? wtf_40
947 ff_mdct_init(&c->mdct, 11, 1, 1.0 / (32768.0 * 1024.0));
948 ff_mdct_init(&c->mdct_half, 10, 1, 1.0 / (32768.0 * 512.0));
949 ff_mdct_init(&c->mdct_small, 8, 1, 1.0 / (32768.0 * 128.0));
950 ff_fft_init(&c->fft128, 6, 0);
951 ff_fft_init(&c->fft256, 7, 0);
952 ff_fft_init(&c->fft512, 8, 1);
953 ff_fft_init(&c->fft1024, 9, 1);
954 avpriv_float_dsp_init(&c->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
956 if (init_vlc(&c->scale_diff, 9, ON2AVC_SCALE_DIFFS,
957 ff_on2avc_scale_diff_bits, 1, 1,
958 ff_on2avc_scale_diff_codes, 4, 4, 0)) {
959 av_log(avctx, AV_LOG_ERROR, "Cannot init VLC\n");
960 return AVERROR(ENOMEM);
962 for (i = 1; i < 9; i++) {
964 if (ff_init_vlc_sparse(&c->cb_vlc[i], 9, ff_on2avc_quad_cb_elems[idx],
965 ff_on2avc_quad_cb_bits[idx], 1, 1,
966 ff_on2avc_quad_cb_codes[idx], 4, 4,
967 ff_on2avc_quad_cb_syms[idx], 2, 2, 0)) {
968 av_log(avctx, AV_LOG_ERROR, "Cannot init VLC\n");
970 return AVERROR(ENOMEM);
973 for (i = 9; i < 16; i++) {
975 if (ff_init_vlc_sparse(&c->cb_vlc[i], 9, ff_on2avc_pair_cb_elems[idx],
976 ff_on2avc_pair_cb_bits[idx], 1, 1,
977 ff_on2avc_pair_cb_codes[idx], 2, 2,
978 ff_on2avc_pair_cb_syms[idx], 2, 2, 0)) {
979 av_log(avctx, AV_LOG_ERROR, "Cannot init VLC\n");
981 return AVERROR(ENOMEM);
988 static av_cold int on2avc_decode_close(AVCodecContext *avctx)
990 On2AVCContext *c = avctx->priv_data;
992 ff_mdct_end(&c->mdct);
993 ff_mdct_end(&c->mdct_half);
994 ff_mdct_end(&c->mdct_small);
995 ff_fft_end(&c->fft128);
996 ff_fft_end(&c->fft256);
997 ff_fft_end(&c->fft512);
998 ff_fft_end(&c->fft1024);
1000 on2avc_free_vlcs(c);
1006 AVCodec ff_on2avc_decoder = {
1008 .long_name = NULL_IF_CONFIG_SMALL("On2 Audio for Video Codec"),
1009 .type = AVMEDIA_TYPE_AUDIO,
1010 .id = AV_CODEC_ID_ON2AVC,
1011 .priv_data_size = sizeof(On2AVCContext),
1012 .init = on2avc_decode_init,
1013 .decode = on2avc_decode_frame,
1014 .close = on2avc_decode_close,
1015 .capabilities = CODEC_CAP_DR1,
1016 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1017 AV_SAMPLE_FMT_NONE },