• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * On2 Audio for Video Codec decoder
3  *
4  * Copyright (c) 2013 Konstantin Shishkov
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/ffmath.h"
25 #include "libavutil/float_dsp.h"
26 #include "libavutil/mem_internal.h"
27 
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "fft.h"
31 #include "get_bits.h"
32 #include "internal.h"
33 
34 #include "on2avcdata.h"
35 
36 #define ON2AVC_SUBFRAME_SIZE   1024
37 
38 enum WindowTypes {
39     WINDOW_TYPE_LONG       = 0,
40     WINDOW_TYPE_LONG_STOP,
41     WINDOW_TYPE_LONG_START,
42     WINDOW_TYPE_8SHORT     = 3,
43     WINDOW_TYPE_EXT4,
44     WINDOW_TYPE_EXT5,
45     WINDOW_TYPE_EXT6,
46     WINDOW_TYPE_EXT7,
47 };
48 
49 typedef struct On2AVCContext {
50     AVCodecContext *avctx;
51     AVFloatDSPContext *fdsp;
52     FFTContext mdct, mdct_half, mdct_small;
53     FFTContext fft128, fft256, fft512, fft1024;
54     void (*wtf)(struct On2AVCContext *ctx, float *out, float *in, int size);
55 
56     int is_av500;
57 
58     const On2AVCMode *modes;
59     int window_type, prev_window_type;
60     int num_windows, num_bands;
61     int bits_per_section;
62     const int *band_start;
63 
64     int grouping[8];
65     int ms_present;
66     int ms_info[ON2AVC_MAX_BANDS];
67 
68     int is_long;
69 
70     uint8_t band_type[ON2AVC_MAX_BANDS];
71     uint8_t band_run_end[ON2AVC_MAX_BANDS];
72     int     num_sections;
73 
74     float band_scales[ON2AVC_MAX_BANDS];
75 
76     VLC scale_diff;
77     VLC cb_vlc[16];
78 
79     float scale_tab[128];
80 
81     DECLARE_ALIGNED(32, float, coeffs)[2][ON2AVC_SUBFRAME_SIZE];
82     DECLARE_ALIGNED(32, float, delay) [2][ON2AVC_SUBFRAME_SIZE];
83 
84     DECLARE_ALIGNED(32, float, temp)     [ON2AVC_SUBFRAME_SIZE * 2];
85     DECLARE_ALIGNED(32, float, mdct_buf) [ON2AVC_SUBFRAME_SIZE];
86     DECLARE_ALIGNED(32, float, long_win) [ON2AVC_SUBFRAME_SIZE];
87     DECLARE_ALIGNED(32, float, short_win)[ON2AVC_SUBFRAME_SIZE / 8];
88 } On2AVCContext;
89 
on2avc_read_ms_info(On2AVCContext * c,GetBitContext * gb)90 static void on2avc_read_ms_info(On2AVCContext *c, GetBitContext *gb)
91 {
92     int w, b, band_off = 0;
93 
94     c->ms_present = get_bits1(gb);
95     if (!c->ms_present)
96         return;
97     for (w = 0; w < c->num_windows; w++) {
98         if (!c->grouping[w]) {
99             memcpy(c->ms_info + band_off,
100                    c->ms_info + band_off - c->num_bands,
101                    c->num_bands * sizeof(*c->ms_info));
102             band_off += c->num_bands;
103             continue;
104         }
105         for (b = 0; b < c->num_bands; b++)
106             c->ms_info[band_off++] = get_bits1(gb);
107     }
108 }
109 
110 // do not see Table 17 in ISO/IEC 13818-7
on2avc_decode_band_types(On2AVCContext * c,GetBitContext * gb)111 static int on2avc_decode_band_types(On2AVCContext *c, GetBitContext *gb)
112 {
113     int bits_per_sect = c->is_long ? 5 : 3;
114     int esc_val = (1 << bits_per_sect) - 1;
115     int num_bands = c->num_bands * c->num_windows;
116     int band = 0, i, band_type, run_len, run;
117 
118     while (band < num_bands) {
119         band_type = get_bits(gb, 4);
120         run_len   = 1;
121         do {
122             run = get_bits(gb, bits_per_sect);
123             if (run > num_bands - band - run_len) {
124                 av_log(c->avctx, AV_LOG_ERROR, "Invalid band type run\n");
125                 return AVERROR_INVALIDDATA;
126             }
127             run_len += run;
128         } while (run == esc_val);
129         for (i = band; i < band + run_len; i++) {
130             c->band_type[i]    = band_type;
131             c->band_run_end[i] = band + run_len;
132         }
133         band += run_len;
134     }
135 
136     return 0;
137 }
138 
139 // completely not like Table 18 in ISO/IEC 13818-7
140 // (no intensity stereo, different coding for the first coefficient)
on2avc_decode_band_scales(On2AVCContext * c,GetBitContext * gb)141 static int on2avc_decode_band_scales(On2AVCContext *c, GetBitContext *gb)
142 {
143     int w, w2, b, scale, first = 1;
144     int band_off = 0;
145 
146     for (w = 0; w < c->num_windows; w++) {
147         if (!c->grouping[w]) {
148             memcpy(c->band_scales + band_off,
149                    c->band_scales + band_off - c->num_bands,
150                    c->num_bands * sizeof(*c->band_scales));
151             band_off += c->num_bands;
152             continue;
153         }
154         for (b = 0; b < c->num_bands; b++) {
155             if (!c->band_type[band_off]) {
156                 int all_zero = 1;
157                 for (w2 = w + 1; w2 < c->num_windows; w2++) {
158                     if (c->grouping[w2])
159                         break;
160                     if (c->band_type[w2 * c->num_bands + b]) {
161                         all_zero = 0;
162                         break;
163                     }
164                 }
165                 if (all_zero) {
166                     c->band_scales[band_off++] = 0;
167                     continue;
168                 }
169             }
170             if (first) {
171                 scale = get_bits(gb, 7);
172                 first = 0;
173             } else {
174                 scale += get_vlc2(gb, c->scale_diff.table, 9, 3);
175             }
176             if (scale < 0 || scale > 127) {
177                 av_log(c->avctx, AV_LOG_ERROR, "Invalid scale value %d\n",
178                        scale);
179                 return AVERROR_INVALIDDATA;
180             }
181             c->band_scales[band_off++] = c->scale_tab[scale];
182         }
183     }
184 
185     return 0;
186 }
187 
on2avc_scale(int v,float scale)188 static inline float on2avc_scale(int v, float scale)
189 {
190     return v * sqrtf(abs(v)) * scale;
191 }
192 
193 // spectral data is coded completely differently - there are no unsigned codebooks
on2avc_decode_quads(On2AVCContext * c,GetBitContext * gb,float * dst,int dst_size,int type,float band_scale)194 static int on2avc_decode_quads(On2AVCContext *c, GetBitContext *gb, float *dst,
195                                int dst_size, int type, float band_scale)
196 {
197     int i, j, val, val1;
198 
199     for (i = 0; i < dst_size; i += 4) {
200         val = get_vlc2(gb, c->cb_vlc[type].table, 9, 2);
201 
202         for (j = 0; j < 4; j++) {
203             val1 = sign_extend((val >> (12 - j * 4)) & 0xF, 4);
204             *dst++ = on2avc_scale(val1, band_scale);
205         }
206     }
207 
208     return 0;
209 }
210 
get_egolomb(GetBitContext * gb)211 static inline int get_egolomb(GetBitContext *gb)
212 {
213     int v = 4;
214 
215     while (get_bits1(gb)) {
216         v++;
217         if (v > 30) {
218             av_log(NULL, AV_LOG_WARNING, "Too large golomb code in get_egolomb.\n");
219             v = 30;
220             break;
221         }
222     }
223 
224     return (1 << v) + get_bits_long(gb, v);
225 }
226 
on2avc_decode_pairs(On2AVCContext * c,GetBitContext * gb,float * dst,int dst_size,int type,float band_scale)227 static int on2avc_decode_pairs(On2AVCContext *c, GetBitContext *gb, float *dst,
228                                int dst_size, int type, float band_scale)
229 {
230     int i, val, val1, val2, sign;
231 
232     for (i = 0; i < dst_size; i += 2) {
233         val = get_vlc2(gb, c->cb_vlc[type].table, 9, 2);
234 
235         val1 = sign_extend(val >> 8,   8);
236         val2 = sign_extend(val & 0xFF, 8);
237         if (type == ON2AVC_ESC_CB) {
238             if (val1 <= -16 || val1 >= 16) {
239                 sign = 1 - (val1 < 0) * 2;
240                 val1 = sign * get_egolomb(gb);
241             }
242             if (val2 <= -16 || val2 >= 16) {
243                 sign = 1 - (val2 < 0) * 2;
244                 val2 = sign * get_egolomb(gb);
245             }
246         }
247 
248         *dst++ = on2avc_scale(val1, band_scale);
249         *dst++ = on2avc_scale(val2, band_scale);
250     }
251 
252     return 0;
253 }
254 
on2avc_read_channel_data(On2AVCContext * c,GetBitContext * gb,int ch)255 static int on2avc_read_channel_data(On2AVCContext *c, GetBitContext *gb, int ch)
256 {
257     int ret;
258     int w, b, band_idx;
259     float *coeff_ptr;
260 
261     if ((ret = on2avc_decode_band_types(c, gb)) < 0)
262         return ret;
263     if ((ret = on2avc_decode_band_scales(c, gb)) < 0)
264         return ret;
265 
266     coeff_ptr = c->coeffs[ch];
267     band_idx  = 0;
268     memset(coeff_ptr, 0, ON2AVC_SUBFRAME_SIZE * sizeof(*coeff_ptr));
269     for (w = 0; w < c->num_windows; w++) {
270         for (b = 0; b < c->num_bands; b++) {
271             int band_size = c->band_start[b + 1] - c->band_start[b];
272             int band_type = c->band_type[band_idx + b];
273 
274             if (!band_type) {
275                 coeff_ptr += band_size;
276                 continue;
277             }
278             if (band_type < 9)
279                 on2avc_decode_quads(c, gb, coeff_ptr, band_size, band_type,
280                                     c->band_scales[band_idx + b]);
281             else
282                 on2avc_decode_pairs(c, gb, coeff_ptr, band_size, band_type,
283                                     c->band_scales[band_idx + b]);
284             coeff_ptr += band_size;
285         }
286         band_idx += c->num_bands;
287     }
288 
289     return 0;
290 }
291 
on2avc_apply_ms(On2AVCContext * c)292 static int on2avc_apply_ms(On2AVCContext *c)
293 {
294     int w, b, i;
295     int band_off = 0;
296     float *ch0 = c->coeffs[0];
297     float *ch1 = c->coeffs[1];
298 
299     for (w = 0; w < c->num_windows; w++) {
300         for (b = 0; b < c->num_bands; b++) {
301             if (c->ms_info[band_off + b]) {
302                 for (i = c->band_start[b]; i < c->band_start[b + 1]; i++) {
303                     float l = *ch0, r = *ch1;
304                     *ch0++ = l + r;
305                     *ch1++ = l - r;
306                 }
307             } else {
308                 ch0 += c->band_start[b + 1] - c->band_start[b];
309                 ch1 += c->band_start[b + 1] - c->band_start[b];
310             }
311         }
312         band_off += c->num_bands;
313     }
314     return 0;
315 }
316 
zero_head_and_tail(float * src,int len,int order0,int order1)317 static void zero_head_and_tail(float *src, int len, int order0, int order1)
318 {
319     memset(src,                0, sizeof(*src) * order0);
320     memset(src + len - order1, 0, sizeof(*src) * order1);
321 }
322 
pretwiddle(float * src,float * dst,int dst_len,int tab_step,int step,int order0,int order1,const double * const * tabs)323 static void pretwiddle(float *src, float *dst, int dst_len, int tab_step,
324                        int step, int order0, int order1, const double * const *tabs)
325 {
326     float *src2, *out;
327     const double *tab;
328     int i, j;
329 
330     out = dst;
331     tab = tabs[0];
332     for (i = 0; i < tab_step; i++) {
333         double sum = 0;
334         for (j = 0; j < order0; j++)
335             sum += src[j] * tab[j * tab_step + i];
336         out[i] += sum;
337     }
338 
339     out = dst + dst_len - tab_step;
340     tab = tabs[order0];
341     src2 = src + (dst_len - tab_step) / step + 1 + order0;
342     for (i = 0; i < tab_step; i++) {
343         double sum = 0;
344         for (j = 0; j < order1; j++)
345             sum += src2[j] * tab[j * tab_step + i];
346         out[i] += sum;
347     }
348 }
349 
twiddle(float * src1,float * src2,int src2_len,const double * tab,int tab_len,int step,int order0,int order1,const double * const * tabs)350 static void twiddle(float *src1, float *src2, int src2_len,
351                     const double *tab, int tab_len, int step,
352                     int order0, int order1, const double * const *tabs)
353 {
354     int steps;
355     int mask;
356     int i, j;
357 
358     steps = (src2_len - tab_len) / step + 1;
359     pretwiddle(src1, src2, src2_len, tab_len, step, order0, order1, tabs);
360     mask = tab_len - 1;
361 
362     for (i = 0; i < steps; i++) {
363         float in0 = src1[order0 + i];
364         int   pos = (src2_len - 1) & mask;
365 
366         if (pos < tab_len) {
367             const double *t = tab;
368             for (j = pos; j >= 0; j--)
369                 src2[j] += in0 * *t++;
370             for (j = 0; j < tab_len - pos - 1; j++)
371                 src2[src2_len - j - 1] += in0 * tab[pos + 1 + j];
372         } else {
373             for (j = 0; j < tab_len; j++)
374                 src2[pos - j] += in0 * tab[j];
375         }
376         mask = pos + step;
377     }
378 }
379 
380 #define CMUL1_R(s, t, is, it) \
381     s[is + 0] * t[it + 0] - s[is + 1] * t[it + 1]
382 #define CMUL1_I(s, t, is, it) \
383     s[is + 0] * t[it + 1] + s[is + 1] * t[it + 0]
384 #define CMUL2_R(s, t, is, it) \
385     s[is + 0] * t[it + 0] + s[is + 1] * t[it + 1]
386 #define CMUL2_I(s, t, is, it) \
387     s[is + 0] * t[it + 1] - s[is + 1] * t[it + 0]
388 
389 #define CMUL0(dst, id, s0, s1, s2, s3, t0, t1, t2, t3, is, it)         \
390     dst[id]     = s0[is] * t0[it]     + s1[is] * t1[it]                \
391                 + s2[is] * t2[it]     + s3[is] * t3[it];               \
392     dst[id + 1] = s0[is] * t0[it + 1] + s1[is] * t1[it + 1]            \
393                 + s2[is] * t2[it + 1] + s3[is] * t3[it + 1];
394 
395 #define CMUL1(dst, s0, s1, s2, s3, t0, t1, t2, t3, is, it)             \
396     *dst++ = CMUL1_R(s0, t0, is, it)                                   \
397            + CMUL1_R(s1, t1, is, it)                                   \
398            + CMUL1_R(s2, t2, is, it)                                   \
399            + CMUL1_R(s3, t3, is, it);                                  \
400     *dst++ = CMUL1_I(s0, t0, is, it)                                   \
401            + CMUL1_I(s1, t1, is, it)                                   \
402            + CMUL1_I(s2, t2, is, it)                                   \
403            + CMUL1_I(s3, t3, is, it);
404 
405 #define CMUL2(dst, s0, s1, s2, s3, t0, t1, t2, t3, is, it)             \
406     *dst++ = CMUL2_R(s0, t0, is, it)                                   \
407            + CMUL2_R(s1, t1, is, it)                                   \
408            + CMUL2_R(s2, t2, is, it)                                   \
409            + CMUL2_R(s3, t3, is, it);                                  \
410     *dst++ = CMUL2_I(s0, t0, is, it)                                   \
411            + CMUL2_I(s1, t1, is, it)                                   \
412            + CMUL2_I(s2, t2, is, it)                                   \
413            + CMUL2_I(s3, t3, is, it);
414 
combine_fft(float * s0,float * s1,float * s2,float * s3,float * dst,const float * t0,const float * t1,const float * t2,const float * t3,int len,int step)415 static void combine_fft(float *s0, float *s1, float *s2, float *s3, float *dst,
416                         const float *t0, const float *t1,
417                         const float *t2, const float *t3, int len, int step)
418 {
419     const float *h0, *h1, *h2, *h3;
420     float *d1, *d2;
421     int tmp, half;
422     int len2 = len >> 1, len4 = len >> 2;
423     int hoff;
424     int i, j, k;
425 
426     tmp = step;
427     for (half = len2; tmp > 1; half <<= 1, tmp >>= 1);
428 
429     h0 = t0 + half;
430     h1 = t1 + half;
431     h2 = t2 + half;
432     h3 = t3 + half;
433 
434     CMUL0(dst, 0, s0, s1, s2, s3, t0, t1, t2, t3, 0, 0);
435 
436     hoff = 2 * step * (len4 >> 1);
437 
438     j = 2;
439     k = 2 * step;
440     d1 = dst + 2;
441     d2 = dst + 2 + (len >> 1);
442     for (i = 0; i < (len4 - 1) >> 1; i++) {
443         CMUL1(d1, s0, s1, s2, s3, t0, t1, t2, t3, j, k);
444         CMUL1(d2, s0, s1, s2, s3, h0, h1, h2, h3, j, k);
445         j += 2;
446         k += 2 * step;
447     }
448     CMUL0(dst, len4,        s0, s1, s2, s3, t0, t1, t2, t3, 1, hoff);
449     CMUL0(dst, len4 + len2, s0, s1, s2, s3, h0, h1, h2, h3, 1, hoff);
450 
451     j = len4;
452     k = hoff + 2 * step * len4;
453     d1 = dst + len4 + 2;
454     d2 = dst + len4 + 2 + len2;
455     for (i = 0; i < (len4 - 2) >> 1; i++) {
456         CMUL2(d1, s0, s1, s2, s3, t0, t1, t2, t3, j, k);
457         CMUL2(d2, s0, s1, s2, s3, h0, h1, h2, h3, j, k);
458         j -= 2;
459         k += 2 * step;
460     }
461     CMUL0(dst, len2 + 4, s0, s1, s2, s3, t0, t1, t2, t3, 0, k);
462 }
463 
wtf_end_512(On2AVCContext * c,float * out,float * src,float * tmp0,float * tmp1)464 static void wtf_end_512(On2AVCContext *c, float *out, float *src,
465                         float *tmp0, float *tmp1)
466 {
467     memcpy(src,        tmp0,      384 * sizeof(*tmp0));
468     memcpy(tmp0 + 384, src + 384, 128 * sizeof(*tmp0));
469 
470     zero_head_and_tail(src,       128, 16, 4);
471     zero_head_and_tail(src + 128, 128, 16, 4);
472     zero_head_and_tail(src + 256, 128, 13, 7);
473     zero_head_and_tail(src + 384, 128, 15, 5);
474 
475     c->fft128.fft_permute(&c->fft128, (FFTComplex*)src);
476     c->fft128.fft_permute(&c->fft128, (FFTComplex*)(src + 128));
477     c->fft128.fft_permute(&c->fft128, (FFTComplex*)(src + 256));
478     c->fft128.fft_permute(&c->fft128, (FFTComplex*)(src + 384));
479     c->fft128.fft_calc(&c->fft128, (FFTComplex*)src);
480     c->fft128.fft_calc(&c->fft128, (FFTComplex*)(src + 128));
481     c->fft128.fft_calc(&c->fft128, (FFTComplex*)(src + 256));
482     c->fft128.fft_calc(&c->fft128, (FFTComplex*)(src + 384));
483     combine_fft(src, src + 128, src + 256, src + 384, tmp1,
484                 ff_on2avc_ctab_1, ff_on2avc_ctab_2,
485                 ff_on2avc_ctab_3, ff_on2avc_ctab_4, 512, 2);
486     c->fft512.fft_permute(&c->fft512, (FFTComplex*)tmp1);
487     c->fft512.fft_calc(&c->fft512, (FFTComplex*)tmp1);
488 
489     pretwiddle(&tmp0[  0], tmp1, 512, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
490     pretwiddle(&tmp0[128], tmp1, 512, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
491     pretwiddle(&tmp0[256], tmp1, 512, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
492     pretwiddle(&tmp0[384], tmp1, 512, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
493 
494     memcpy(src, tmp1, 512 * sizeof(float));
495 }
496 
wtf_end_1024(On2AVCContext * c,float * out,float * src,float * tmp0,float * tmp1)497 static void wtf_end_1024(On2AVCContext *c, float *out, float *src,
498                          float *tmp0, float *tmp1)
499 {
500     memcpy(src,        tmp0,      768 * sizeof(*tmp0));
501     memcpy(tmp0 + 768, src + 768, 256 * sizeof(*tmp0));
502 
503     zero_head_and_tail(src,       256, 16, 4);
504     zero_head_and_tail(src + 256, 256, 16, 4);
505     zero_head_and_tail(src + 512, 256, 13, 7);
506     zero_head_and_tail(src + 768, 256, 15, 5);
507 
508     c->fft256.fft_permute(&c->fft256, (FFTComplex*)src);
509     c->fft256.fft_permute(&c->fft256, (FFTComplex*)(src + 256));
510     c->fft256.fft_permute(&c->fft256, (FFTComplex*)(src + 512));
511     c->fft256.fft_permute(&c->fft256, (FFTComplex*)(src + 768));
512     c->fft256.fft_calc(&c->fft256, (FFTComplex*)src);
513     c->fft256.fft_calc(&c->fft256, (FFTComplex*)(src + 256));
514     c->fft256.fft_calc(&c->fft256, (FFTComplex*)(src + 512));
515     c->fft256.fft_calc(&c->fft256, (FFTComplex*)(src + 768));
516     combine_fft(src, src + 256, src + 512, src + 768, tmp1,
517                 ff_on2avc_ctab_1, ff_on2avc_ctab_2,
518                 ff_on2avc_ctab_3, ff_on2avc_ctab_4, 1024, 1);
519     c->fft1024.fft_permute(&c->fft1024, (FFTComplex*)tmp1);
520     c->fft1024.fft_calc(&c->fft1024, (FFTComplex*)tmp1);
521 
522     pretwiddle(&tmp0[  0], tmp1, 1024, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
523     pretwiddle(&tmp0[256], tmp1, 1024, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
524     pretwiddle(&tmp0[512], tmp1, 1024, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
525     pretwiddle(&tmp0[768], tmp1, 1024, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
526 
527     memcpy(src, tmp1, 1024 * sizeof(float));
528 }
529 
wtf_40(On2AVCContext * c,float * out,float * src,int size)530 static void wtf_40(On2AVCContext *c, float *out, float *src, int size)
531 {
532     float *tmp0 = c->temp, *tmp1 = c->temp + 1024;
533 
534     memset(tmp0, 0, sizeof(*tmp0) * 1024);
535     memset(tmp1, 0, sizeof(*tmp1) * 1024);
536 
537     if (size == 512) {
538         twiddle(src,       &tmp0[  0], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
539         twiddle(src +   8, &tmp0[  0], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
540         twiddle(src +  16, &tmp0[ 16], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
541         twiddle(src +  24, &tmp0[ 16], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
542         twiddle(src +  32, &tmp0[ 32], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
543         twiddle(src +  40, &tmp0[ 32], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
544         twiddle(src +  48, &tmp0[ 48], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
545         twiddle(src +  56, &tmp0[ 48], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
546         twiddle(&tmp0[ 0], &tmp1[  0], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
547         twiddle(&tmp0[16], &tmp1[  0], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
548         twiddle(&tmp0[32], &tmp1[ 32], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
549         twiddle(&tmp0[48], &tmp1[ 32], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
550         twiddle(src +  64, &tmp1[ 64], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
551         twiddle(src +  80, &tmp1[ 64], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
552         twiddle(src +  96, &tmp1[ 96], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
553         twiddle(src + 112, &tmp1[ 96], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
554         twiddle(src + 128, &tmp1[128], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
555         twiddle(src + 144, &tmp1[128], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
556         twiddle(src + 160, &tmp1[160], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
557         twiddle(src + 176, &tmp1[160], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
558 
559         memset(tmp0, 0, 64 * sizeof(*tmp0));
560 
561         twiddle(&tmp1[  0], &tmp0[  0], 128, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
562         twiddle(&tmp1[ 32], &tmp0[  0], 128, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
563         twiddle(&tmp1[ 64], &tmp0[  0], 128, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
564         twiddle(&tmp1[ 96], &tmp0[  0], 128, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
565         twiddle(&tmp1[128], &tmp0[128], 128, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
566         twiddle(&tmp1[160], &tmp0[128], 128, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
567         twiddle(src + 192,  &tmp0[128], 128, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
568         twiddle(src + 224,  &tmp0[128], 128, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
569         twiddle(src + 256,  &tmp0[256], 128, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
570         twiddle(src + 288,  &tmp0[256], 128, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
571         twiddle(src + 320,  &tmp0[256], 128, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
572         twiddle(src + 352,  &tmp0[256], 128, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
573 
574         wtf_end_512(c, out, src, tmp0, tmp1);
575     } else {
576         twiddle(src,       &tmp0[  0], 32, ff_on2avc_tab_20_1, 20, 2,  5,  4, ff_on2avc_tabs_9_20_1);
577         twiddle(src +  16, &tmp0[  0], 32, ff_on2avc_tab_20_2, 20, 2,  4,  5, ff_on2avc_tabs_9_20_2);
578         twiddle(src +  32, &tmp0[ 32], 32, ff_on2avc_tab_20_2, 20, 2,  4,  5, ff_on2avc_tabs_9_20_2);
579         twiddle(src +  48, &tmp0[ 32], 32, ff_on2avc_tab_20_1, 20, 2,  5,  4, ff_on2avc_tabs_9_20_1);
580         twiddle(src +  64, &tmp0[ 64], 32, ff_on2avc_tab_20_1, 20, 2,  5,  4, ff_on2avc_tabs_9_20_1);
581         twiddle(src +  80, &tmp0[ 64], 32, ff_on2avc_tab_20_2, 20, 2,  4,  5, ff_on2avc_tabs_9_20_2);
582         twiddle(src +  96, &tmp0[ 96], 32, ff_on2avc_tab_20_2, 20, 2,  4,  5, ff_on2avc_tabs_9_20_2);
583         twiddle(src + 112, &tmp0[ 96], 32, ff_on2avc_tab_20_1, 20, 2,  5,  4, ff_on2avc_tabs_9_20_1);
584         twiddle(&tmp0[ 0], &tmp1[  0], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
585         twiddle(&tmp0[32], &tmp1[  0], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
586         twiddle(&tmp0[64], &tmp1[ 64], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
587         twiddle(&tmp0[96], &tmp1[ 64], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
588         twiddle(src + 128, &tmp1[128], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
589         twiddle(src + 160, &tmp1[128], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
590         twiddle(src + 192, &tmp1[192], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
591         twiddle(src + 224, &tmp1[192], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
592         twiddle(src + 256, &tmp1[256], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
593         twiddle(src + 288, &tmp1[256], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
594         twiddle(src + 320, &tmp1[320], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
595         twiddle(src + 352, &tmp1[320], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
596 
597         memset(tmp0, 0, 128 * sizeof(*tmp0));
598 
599         twiddle(&tmp1[  0], &tmp0[  0], 256, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
600         twiddle(&tmp1[ 64], &tmp0[  0], 256, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
601         twiddle(&tmp1[128], &tmp0[  0], 256, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
602         twiddle(&tmp1[192], &tmp0[  0], 256, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
603         twiddle(&tmp1[256], &tmp0[256], 256, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
604         twiddle(&tmp1[320], &tmp0[256], 256, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
605         twiddle(src + 384,  &tmp0[256], 256, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
606         twiddle(src + 448,  &tmp0[256], 256, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
607         twiddle(src + 512,  &tmp0[512], 256, ff_on2avc_tab_84_1, 84, 4, 16, 4, ff_on2avc_tabs_20_84_1);
608         twiddle(src + 576,  &tmp0[512], 256, ff_on2avc_tab_84_2, 84, 4, 16, 4, ff_on2avc_tabs_20_84_2);
609         twiddle(src + 640,  &tmp0[512], 256, ff_on2avc_tab_84_3, 84, 4, 13, 7, ff_on2avc_tabs_20_84_3);
610         twiddle(src + 704,  &tmp0[512], 256, ff_on2avc_tab_84_4, 84, 4, 15, 5, ff_on2avc_tabs_20_84_4);
611 
612         wtf_end_1024(c, out, src, tmp0, tmp1);
613     }
614 }
615 
wtf_44(On2AVCContext * c,float * out,float * src,int size)616 static void wtf_44(On2AVCContext *c, float *out, float *src, int size)
617 {
618     float *tmp0 = c->temp, *tmp1 = c->temp + 1024;
619 
620     memset(tmp0, 0, sizeof(*tmp0) * 1024);
621     memset(tmp1, 0, sizeof(*tmp1) * 1024);
622 
623     if (size == 512) {
624         twiddle(src,       &tmp0[ 0], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
625         twiddle(src +   8, &tmp0[ 0], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
626         twiddle(src +  16, &tmp0[16], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
627         twiddle(src +  24, &tmp0[16], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
628         twiddle(src +  32, &tmp0[32], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
629         twiddle(src +  40, &tmp0[32], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
630         twiddle(src +  48, &tmp0[48], 16, ff_on2avc_tab_10_2, 10, 2, 3, 1, ff_on2avc_tabs_4_10_2);
631         twiddle(src +  56, &tmp0[48], 16, ff_on2avc_tab_10_1, 10, 2, 1, 3, ff_on2avc_tabs_4_10_1);
632         twiddle(&tmp0[ 0], &tmp1[ 0], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
633         twiddle(&tmp0[16], &tmp1[ 0], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
634         twiddle(&tmp0[32], &tmp1[32], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
635         twiddle(&tmp0[48], &tmp1[32], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
636         twiddle(src +  64, &tmp1[64], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
637         twiddle(src +  80, &tmp1[64], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
638         twiddle(src +  96, &tmp1[96], 32, ff_on2avc_tab_20_2, 20, 2, 4, 5, ff_on2avc_tabs_9_20_2);
639         twiddle(src + 112, &tmp1[96], 32, ff_on2avc_tab_20_1, 20, 2, 5, 4, ff_on2avc_tabs_9_20_1);
640 
641         memset(tmp0, 0, 64 * sizeof(*tmp0));
642 
643         twiddle(&tmp1[ 0], &tmp0[  0], 128, ff_on2avc_tab_84_1, 84, 4, 16,  4, ff_on2avc_tabs_20_84_1);
644         twiddle(&tmp1[32], &tmp0[  0], 128, ff_on2avc_tab_84_2, 84, 4, 16,  4, ff_on2avc_tabs_20_84_2);
645         twiddle(&tmp1[64], &tmp0[  0], 128, ff_on2avc_tab_84_3, 84, 4, 13,  7, ff_on2avc_tabs_20_84_3);
646         twiddle(&tmp1[96], &tmp0[  0], 128, ff_on2avc_tab_84_4, 84, 4, 15,  5, ff_on2avc_tabs_20_84_4);
647         twiddle(src + 128, &tmp0[128], 128, ff_on2avc_tab_84_4, 84, 4, 15,  5, ff_on2avc_tabs_20_84_4);
648         twiddle(src + 160, &tmp0[128], 128, ff_on2avc_tab_84_3, 84, 4, 13,  7, ff_on2avc_tabs_20_84_3);
649         twiddle(src + 192, &tmp0[128], 128, ff_on2avc_tab_84_2, 84, 4, 16,  4, ff_on2avc_tabs_20_84_2);
650         twiddle(src + 224, &tmp0[128], 128, ff_on2avc_tab_84_1, 84, 4, 16,  4, ff_on2avc_tabs_20_84_1);
651         twiddle(src + 256, &tmp0[256], 128, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
652         twiddle(src + 320, &tmp0[256], 128, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
653 
654         wtf_end_512(c, out, src, tmp0, tmp1);
655     } else {
656         twiddle(src,       &tmp0[  0], 32, ff_on2avc_tab_20_1, 20, 2,  5,  4, ff_on2avc_tabs_9_20_1);
657         twiddle(src +  16, &tmp0[  0], 32, ff_on2avc_tab_20_2, 20, 2,  4,  5, ff_on2avc_tabs_9_20_2);
658         twiddle(src +  32, &tmp0[ 32], 32, ff_on2avc_tab_20_2, 20, 2,  4,  5, ff_on2avc_tabs_9_20_2);
659         twiddle(src +  48, &tmp0[ 32], 32, ff_on2avc_tab_20_1, 20, 2,  5,  4, ff_on2avc_tabs_9_20_1);
660         twiddle(src +  64, &tmp0[ 64], 32, ff_on2avc_tab_20_1, 20, 2,  5,  4, ff_on2avc_tabs_9_20_1);
661         twiddle(src +  80, &tmp0[ 64], 32, ff_on2avc_tab_20_2, 20, 2,  4,  5, ff_on2avc_tabs_9_20_2);
662         twiddle(src +  96, &tmp0[ 96], 32, ff_on2avc_tab_20_2, 20, 2,  4,  5, ff_on2avc_tabs_9_20_2);
663         twiddle(src + 112, &tmp0[ 96], 32, ff_on2avc_tab_20_1, 20, 2,  5,  4, ff_on2avc_tabs_9_20_1);
664         twiddle(&tmp0[ 0], &tmp1[  0], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
665         twiddle(&tmp0[32], &tmp1[  0], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
666         twiddle(&tmp0[64], &tmp1[ 64], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
667         twiddle(&tmp0[96], &tmp1[ 64], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
668         twiddle(src + 128, &tmp1[128], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
669         twiddle(src + 160, &tmp1[128], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
670         twiddle(src + 192, &tmp1[192], 64, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
671         twiddle(src + 224, &tmp1[192], 64, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
672 
673         memset(tmp0, 0, 128 * sizeof(*tmp0));
674 
675         twiddle(&tmp1[  0], &tmp0[  0], 256, ff_on2avc_tab_84_1, 84, 4, 16,  4, ff_on2avc_tabs_20_84_1);
676         twiddle(&tmp1[ 64], &tmp0[  0], 256, ff_on2avc_tab_84_2, 84, 4, 16,  4, ff_on2avc_tabs_20_84_2);
677         twiddle(&tmp1[128], &tmp0[  0], 256, ff_on2avc_tab_84_3, 84, 4, 13,  7, ff_on2avc_tabs_20_84_3);
678         twiddle(&tmp1[192], &tmp0[  0], 256, ff_on2avc_tab_84_4, 84, 4, 15,  5, ff_on2avc_tabs_20_84_4);
679         twiddle(src + 256,  &tmp0[256], 256, ff_on2avc_tab_84_4, 84, 4, 15,  5, ff_on2avc_tabs_20_84_4);
680         twiddle(src + 320,  &tmp0[256], 256, ff_on2avc_tab_84_3, 84, 4, 13,  7, ff_on2avc_tabs_20_84_3);
681         twiddle(src + 384,  &tmp0[256], 256, ff_on2avc_tab_84_2, 84, 4, 16,  4, ff_on2avc_tabs_20_84_2);
682         twiddle(src + 448,  &tmp0[256], 256, ff_on2avc_tab_84_1, 84, 4, 16,  4, ff_on2avc_tabs_20_84_1);
683         twiddle(src + 512,  &tmp0[512], 256, ff_on2avc_tab_40_1, 40, 2, 11,  8, ff_on2avc_tabs_19_40_1);
684         twiddle(src + 640,  &tmp0[512], 256, ff_on2avc_tab_40_2, 40, 2,  8, 11, ff_on2avc_tabs_19_40_2);
685 
686         wtf_end_1024(c, out, src, tmp0, tmp1);
687     }
688 }
689 
on2avc_reconstruct_channel_ext(On2AVCContext * c,AVFrame * dst,int offset)690 static int on2avc_reconstruct_channel_ext(On2AVCContext *c, AVFrame *dst, int offset)
691 {
692     int ch, i;
693 
694     for (ch = 0; ch < c->avctx->channels; ch++) {
695         float *out   = (float*)dst->extended_data[ch] + offset;
696         float *in    = c->coeffs[ch];
697         float *saved = c->delay[ch];
698         float *buf   = c->mdct_buf;
699         float *wout  = out + 448;
700 
701         switch (c->window_type) {
702         case WINDOW_TYPE_EXT7:
703             c->mdct.imdct_half(&c->mdct, buf, in);
704             break;
705         case WINDOW_TYPE_EXT4:
706             c->wtf(c, buf, in, 1024);
707             break;
708         case WINDOW_TYPE_EXT5:
709             c->wtf(c, buf, in, 512);
710             c->mdct.imdct_half(&c->mdct_half, buf + 512, in + 512);
711             for (i = 0; i < 256; i++) {
712                 FFSWAP(float, buf[i + 512], buf[1023 - i]);
713             }
714             break;
715         case WINDOW_TYPE_EXT6:
716             c->mdct.imdct_half(&c->mdct_half, buf, in);
717             for (i = 0; i < 256; i++) {
718                 FFSWAP(float, buf[i], buf[511 - i]);
719             }
720             c->wtf(c, buf + 512, in + 512, 512);
721             break;
722         }
723 
724         memcpy(out, saved, 448 * sizeof(float));
725         c->fdsp->vector_fmul_window(wout, saved + 448, buf, c->short_win, 64);
726         memcpy(wout + 128,  buf + 64,         448 * sizeof(float));
727         memcpy(saved,       buf + 512,        448 * sizeof(float));
728         memcpy(saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
729     }
730 
731     return 0;
732 }
733 
734 // not borrowed from aacdec.c - the codec has original design after all
on2avc_reconstruct_channel(On2AVCContext * c,int channel,AVFrame * dst,int offset)735 static int on2avc_reconstruct_channel(On2AVCContext *c, int channel,
736                                       AVFrame *dst, int offset)
737 {
738     int i;
739     float *out   = (float*)dst->extended_data[channel] + offset;
740     float *in    = c->coeffs[channel];
741     float *saved = c->delay[channel];
742     float *buf   = c->mdct_buf;
743     float *temp  = c->temp;
744 
745     switch (c->window_type) {
746     case WINDOW_TYPE_LONG_START:
747     case WINDOW_TYPE_LONG_STOP:
748     case WINDOW_TYPE_LONG:
749         c->mdct.imdct_half(&c->mdct, buf, in);
750         break;
751     case WINDOW_TYPE_8SHORT:
752         for (i = 0; i < ON2AVC_SUBFRAME_SIZE; i += ON2AVC_SUBFRAME_SIZE / 8)
753             c->mdct_small.imdct_half(&c->mdct_small, buf + i, in + i);
754         break;
755     }
756 
757     if ((c->prev_window_type == WINDOW_TYPE_LONG ||
758          c->prev_window_type == WINDOW_TYPE_LONG_STOP) &&
759         (c->window_type == WINDOW_TYPE_LONG ||
760          c->window_type == WINDOW_TYPE_LONG_START)) {
761         c->fdsp->vector_fmul_window(out, saved, buf, c->long_win, 512);
762     } else {
763         float *wout = out + 448;
764         memcpy(out, saved, 448 * sizeof(float));
765 
766         if (c->window_type == WINDOW_TYPE_8SHORT) {
767             c->fdsp->vector_fmul_window(wout + 0*128, saved + 448,      buf + 0*128, c->short_win, 64);
768             c->fdsp->vector_fmul_window(wout + 1*128, buf + 0*128 + 64, buf + 1*128, c->short_win, 64);
769             c->fdsp->vector_fmul_window(wout + 2*128, buf + 1*128 + 64, buf + 2*128, c->short_win, 64);
770             c->fdsp->vector_fmul_window(wout + 3*128, buf + 2*128 + 64, buf + 3*128, c->short_win, 64);
771             c->fdsp->vector_fmul_window(temp,         buf + 3*128 + 64, buf + 4*128, c->short_win, 64);
772             memcpy(wout + 4*128, temp, 64 * sizeof(float));
773         } else {
774             c->fdsp->vector_fmul_window(wout, saved + 448, buf, c->short_win, 64);
775             memcpy(wout + 128, buf + 64, 448 * sizeof(float));
776         }
777     }
778 
779     // buffer update
780     switch (c->window_type) {
781     case WINDOW_TYPE_8SHORT:
782         memcpy(saved,       temp + 64,         64 * sizeof(float));
783         c->fdsp->vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, c->short_win, 64);
784         c->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, c->short_win, 64);
785         c->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, c->short_win, 64);
786         memcpy(saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
787         break;
788     case WINDOW_TYPE_LONG_START:
789         memcpy(saved,       buf + 512,        448 * sizeof(float));
790         memcpy(saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
791         break;
792     case WINDOW_TYPE_LONG_STOP:
793     case WINDOW_TYPE_LONG:
794         memcpy(saved,       buf + 512,        512 * sizeof(float));
795         break;
796     }
797     return 0;
798 }
799 
on2avc_decode_subframe(On2AVCContext * c,const uint8_t * buf,int buf_size,AVFrame * dst,int offset)800 static int on2avc_decode_subframe(On2AVCContext *c, const uint8_t *buf,
801                                   int buf_size, AVFrame *dst, int offset)
802 {
803     GetBitContext gb;
804     int i, ret;
805 
806     if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
807         return ret;
808 
809     if (get_bits1(&gb)) {
810         av_log(c->avctx, AV_LOG_ERROR, "enh bit set\n");
811         return AVERROR_INVALIDDATA;
812     }
813     c->prev_window_type = c->window_type;
814     c->window_type      = get_bits(&gb, 3);
815 
816     c->band_start  = c->modes[c->window_type].band_start;
817     c->num_windows = c->modes[c->window_type].num_windows;
818     c->num_bands   = c->modes[c->window_type].num_bands;
819     c->is_long     = (c->window_type != WINDOW_TYPE_8SHORT);
820 
821     c->grouping[0] = 1;
822     for (i = 1; i < c->num_windows; i++)
823         c->grouping[i] = !get_bits1(&gb);
824 
825     on2avc_read_ms_info(c, &gb);
826     for (i = 0; i < c->avctx->channels; i++)
827         if ((ret = on2avc_read_channel_data(c, &gb, i)) < 0)
828             return AVERROR_INVALIDDATA;
829     if (c->avctx->channels == 2 && c->ms_present)
830         on2avc_apply_ms(c);
831     if (c->window_type < WINDOW_TYPE_EXT4) {
832         for (i = 0; i < c->avctx->channels; i++)
833             on2avc_reconstruct_channel(c, i, dst, offset);
834     } else {
835         on2avc_reconstruct_channel_ext(c, dst, offset);
836     }
837 
838     return 0;
839 }
840 
on2avc_decode_frame(AVCodecContext * avctx,void * data,int * got_frame_ptr,AVPacket * avpkt)841 static int on2avc_decode_frame(AVCodecContext * avctx, void *data,
842                                int *got_frame_ptr, AVPacket *avpkt)
843 {
844     AVFrame *frame     = data;
845     const uint8_t *buf = avpkt->data;
846     int buf_size       = avpkt->size;
847     On2AVCContext *c   = avctx->priv_data;
848     GetByteContext gb;
849     int num_frames = 0, frame_size, audio_off;
850     int ret;
851 
852     if (c->is_av500) {
853         /* get output buffer */
854         frame->nb_samples = ON2AVC_SUBFRAME_SIZE;
855         if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
856             return ret;
857 
858         if ((ret = on2avc_decode_subframe(c, buf, buf_size, frame, 0)) < 0)
859             return ret;
860     } else {
861         bytestream2_init(&gb, buf, buf_size);
862         while (bytestream2_get_bytes_left(&gb) > 2) {
863             frame_size = bytestream2_get_le16(&gb);
864             if (!frame_size || frame_size > bytestream2_get_bytes_left(&gb)) {
865                 av_log(avctx, AV_LOG_ERROR, "Invalid subframe size %d\n",
866                        frame_size);
867                 return AVERROR_INVALIDDATA;
868             }
869             num_frames++;
870             bytestream2_skip(&gb, frame_size);
871         }
872         if (!num_frames) {
873             av_log(avctx, AV_LOG_ERROR, "No subframes present\n");
874             return AVERROR_INVALIDDATA;
875         }
876 
877         /* get output buffer */
878         frame->nb_samples = ON2AVC_SUBFRAME_SIZE * num_frames;
879         if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
880             return ret;
881 
882         audio_off = 0;
883         bytestream2_init(&gb, buf, buf_size);
884         while (bytestream2_get_bytes_left(&gb) > 2) {
885             frame_size = bytestream2_get_le16(&gb);
886             if ((ret = on2avc_decode_subframe(c, gb.buffer, frame_size,
887                                               frame, audio_off)) < 0)
888                 return ret;
889             audio_off += ON2AVC_SUBFRAME_SIZE;
890             bytestream2_skip(&gb, frame_size);
891         }
892     }
893 
894     *got_frame_ptr = 1;
895 
896     return buf_size;
897 }
898 
on2avc_free_vlcs(On2AVCContext * c)899 static av_cold void on2avc_free_vlcs(On2AVCContext *c)
900 {
901     int i;
902 
903     ff_free_vlc(&c->scale_diff);
904     for (i = 1; i < 16; i++)
905         ff_free_vlc(&c->cb_vlc[i]);
906 }
907 
on2avc_decode_init(AVCodecContext * avctx)908 static av_cold int on2avc_decode_init(AVCodecContext *avctx)
909 {
910     On2AVCContext *c = avctx->priv_data;
911     const uint8_t  *lens = ff_on2avc_cb_lens;
912     const uint16_t *syms = ff_on2avc_cb_syms;
913     int i, ret;
914 
915     if (avctx->channels > 2U) {
916         avpriv_request_sample(avctx, "Decoding more than 2 channels");
917         return AVERROR_PATCHWELCOME;
918     }
919 
920     c->avctx = avctx;
921     avctx->sample_fmt     = AV_SAMPLE_FMT_FLTP;
922     avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
923                                                    : AV_CH_LAYOUT_MONO;
924 
925     c->is_av500 = (avctx->codec_tag == 0x500);
926 
927     if (avctx->channels == 2)
928         av_log(avctx, AV_LOG_WARNING,
929                "Stereo mode support is not good, patch is welcome\n");
930 
931     // We add -0.01 before ceil() to avoid any values to fall at exactly the
932     // midpoint between different ceil values. The results are identical to
933     // using pow(10, i / 10.0) without such bias
934     for (i = 0; i < 20; i++)
935         c->scale_tab[i] = ceil(ff_exp10(i * 0.1) * 16 - 0.01) / 32;
936     for (; i < 128; i++)
937         c->scale_tab[i] = ceil(ff_exp10(i * 0.1) * 0.5 - 0.01);
938 
939     if (avctx->sample_rate < 32000 || avctx->channels == 1)
940         memcpy(c->long_win, ff_on2avc_window_long_24000,
941                1024 * sizeof(*c->long_win));
942     else
943         memcpy(c->long_win, ff_on2avc_window_long_32000,
944                1024 * sizeof(*c->long_win));
945     memcpy(c->short_win, ff_on2avc_window_short, 128 * sizeof(*c->short_win));
946 
947     c->modes = (avctx->sample_rate <= 40000) ? ff_on2avc_modes_40
948                                              : ff_on2avc_modes_44;
949     c->wtf   = (avctx->sample_rate <= 40000) ? wtf_40
950                                              : wtf_44;
951 
952     ff_mdct_init(&c->mdct,       11, 1, 1.0 / (32768.0 * 1024.0));
953     ff_mdct_init(&c->mdct_half,  10, 1, 1.0 / (32768.0 * 512.0));
954     ff_mdct_init(&c->mdct_small,  8, 1, 1.0 / (32768.0 * 128.0));
955     ff_fft_init(&c->fft128,  6, 0);
956     ff_fft_init(&c->fft256,  7, 0);
957     ff_fft_init(&c->fft512,  8, 1);
958     ff_fft_init(&c->fft1024, 9, 1);
959     c->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
960     if (!c->fdsp)
961         return AVERROR(ENOMEM);
962 
963     ret = ff_init_vlc_from_lengths(&c->scale_diff, 9, ON2AVC_SCALE_DIFFS,
964                                    ff_on2avc_scale_diff_bits, 1,
965                                    ff_on2avc_scale_diff_syms, 1, 1, -60, 0, avctx);
966     if (ret < 0)
967         goto vlc_fail;
968     for (i = 1; i < 16; i++) {
969         int idx = i - 1;
970         ret = ff_init_vlc_from_lengths(&c->cb_vlc[i], 9, ff_on2avc_cb_elems[idx],
971                                        lens, 1,
972                                        syms, 2, 2, 0, 0, avctx);
973         if (ret < 0)
974             goto vlc_fail;
975         lens += ff_on2avc_cb_elems[idx];
976         syms += ff_on2avc_cb_elems[idx];
977     }
978 
979     return 0;
980 vlc_fail:
981     av_log(avctx, AV_LOG_ERROR, "Cannot init VLC\n");
982     return ret;
983 }
984 
on2avc_decode_close(AVCodecContext * avctx)985 static av_cold int on2avc_decode_close(AVCodecContext *avctx)
986 {
987     On2AVCContext *c = avctx->priv_data;
988 
989     ff_mdct_end(&c->mdct);
990     ff_mdct_end(&c->mdct_half);
991     ff_mdct_end(&c->mdct_small);
992     ff_fft_end(&c->fft128);
993     ff_fft_end(&c->fft256);
994     ff_fft_end(&c->fft512);
995     ff_fft_end(&c->fft1024);
996 
997     av_freep(&c->fdsp);
998 
999     on2avc_free_vlcs(c);
1000 
1001     return 0;
1002 }
1003 
1004 
1005 AVCodec ff_on2avc_decoder = {
1006     .name           = "on2avc",
1007     .long_name      = NULL_IF_CONFIG_SMALL("On2 Audio for Video Codec"),
1008     .type           = AVMEDIA_TYPE_AUDIO,
1009     .id             = AV_CODEC_ID_ON2AVC,
1010     .priv_data_size = sizeof(On2AVCContext),
1011     .init           = on2avc_decode_init,
1012     .decode         = on2avc_decode_frame,
1013     .close          = on2avc_decode_close,
1014     .capabilities   = AV_CODEC_CAP_DR1,
1015     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
1016     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1017                                                       AV_SAMPLE_FMT_NONE },
1018 };
1019