1 /*
2  * TAK decoder
3  * Copyright (c) 2012 Paul B Mahol
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  * TAK (Tom's lossless Audio Kompressor) decoder
25  * @author Paul B Mahol
26  */
27 
28 #include "libavutil/internal.h"
29 #include "libavutil/samplefmt.h"
30 #include "tak.h"
31 #include "audiodsp.h"
32 #include "thread.h"
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "unary.h"
36 
37 #define MAX_SUBFRAMES     8                         ///< max number of subframes per channel
38 #define MAX_PREDICTORS  256
39 
40 typedef struct MCDParam {
41     int8_t present;                                 ///< decorrelation parameter availability for this channel
42     int8_t index;                                   ///< index into array of decorrelation types
43     int8_t chan1;
44     int8_t chan2;
45 } MCDParam;
46 
47 typedef struct TAKDecContext {
48     AVCodecContext *avctx;                          ///< parent AVCodecContext
49     AudioDSPContext adsp;
50     TAKStreamInfo   ti;
51     GetBitContext   gb;                             ///< bitstream reader initialized to start at the current frame
52 
53     int             uval;
54     int             nb_samples;                     ///< number of samples in the current frame
55     uint8_t        *decode_buffer;
56     unsigned int    decode_buffer_size;
57     int32_t        *decoded[TAK_MAX_CHANNELS];      ///< decoded samples for each channel
58 
59     int8_t          lpc_mode[TAK_MAX_CHANNELS];
60     int8_t          sample_shift[TAK_MAX_CHANNELS]; ///< shift applied to every sample in the channel
61     int16_t         predictors[MAX_PREDICTORS];
62     int             nb_subframes;                   ///< number of subframes in the current frame
63     int16_t         subframe_len[MAX_SUBFRAMES];    ///< subframe length in samples
64     int             subframe_scale;
65 
66     int8_t          dmode;                          ///< channel decorrelation type in the current frame
67 
68     MCDParam        mcdparams[TAK_MAX_CHANNELS];    ///< multichannel decorrelation parameters
69 
70     int8_t          coding_mode[128];
71     DECLARE_ALIGNED(16, int16_t, filter)[MAX_PREDICTORS];
72     DECLARE_ALIGNED(16, int16_t, residues)[544];
73 } TAKDecContext;
74 
75 static const int8_t mc_dmodes[] = { 1, 3, 4, 6, };
76 
77 static const uint16_t predictor_sizes[] = {
78     4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0,
79 };
80 
81 static const struct CParam {
82     int init;
83     int escape;
84     int scale;
85     int aescape;
86     int bias;
87 } xcodes[50] = {
88     { 0x01, 0x0000001, 0x0000001, 0x0000003, 0x0000008 },
89     { 0x02, 0x0000003, 0x0000001, 0x0000007, 0x0000006 },
90     { 0x03, 0x0000005, 0x0000002, 0x000000E, 0x000000D },
91     { 0x03, 0x0000003, 0x0000003, 0x000000D, 0x0000018 },
92     { 0x04, 0x000000B, 0x0000004, 0x000001C, 0x0000019 },
93     { 0x04, 0x0000006, 0x0000006, 0x000001A, 0x0000030 },
94     { 0x05, 0x0000016, 0x0000008, 0x0000038, 0x0000032 },
95     { 0x05, 0x000000C, 0x000000C, 0x0000034, 0x0000060 },
96     { 0x06, 0x000002C, 0x0000010, 0x0000070, 0x0000064 },
97     { 0x06, 0x0000018, 0x0000018, 0x0000068, 0x00000C0 },
98     { 0x07, 0x0000058, 0x0000020, 0x00000E0, 0x00000C8 },
99     { 0x07, 0x0000030, 0x0000030, 0x00000D0, 0x0000180 },
100     { 0x08, 0x00000B0, 0x0000040, 0x00001C0, 0x0000190 },
101     { 0x08, 0x0000060, 0x0000060, 0x00001A0, 0x0000300 },
102     { 0x09, 0x0000160, 0x0000080, 0x0000380, 0x0000320 },
103     { 0x09, 0x00000C0, 0x00000C0, 0x0000340, 0x0000600 },
104     { 0x0A, 0x00002C0, 0x0000100, 0x0000700, 0x0000640 },
105     { 0x0A, 0x0000180, 0x0000180, 0x0000680, 0x0000C00 },
106     { 0x0B, 0x0000580, 0x0000200, 0x0000E00, 0x0000C80 },
107     { 0x0B, 0x0000300, 0x0000300, 0x0000D00, 0x0001800 },
108     { 0x0C, 0x0000B00, 0x0000400, 0x0001C00, 0x0001900 },
109     { 0x0C, 0x0000600, 0x0000600, 0x0001A00, 0x0003000 },
110     { 0x0D, 0x0001600, 0x0000800, 0x0003800, 0x0003200 },
111     { 0x0D, 0x0000C00, 0x0000C00, 0x0003400, 0x0006000 },
112     { 0x0E, 0x0002C00, 0x0001000, 0x0007000, 0x0006400 },
113     { 0x0E, 0x0001800, 0x0001800, 0x0006800, 0x000C000 },
114     { 0x0F, 0x0005800, 0x0002000, 0x000E000, 0x000C800 },
115     { 0x0F, 0x0003000, 0x0003000, 0x000D000, 0x0018000 },
116     { 0x10, 0x000B000, 0x0004000, 0x001C000, 0x0019000 },
117     { 0x10, 0x0006000, 0x0006000, 0x001A000, 0x0030000 },
118     { 0x11, 0x0016000, 0x0008000, 0x0038000, 0x0032000 },
119     { 0x11, 0x000C000, 0x000C000, 0x0034000, 0x0060000 },
120     { 0x12, 0x002C000, 0x0010000, 0x0070000, 0x0064000 },
121     { 0x12, 0x0018000, 0x0018000, 0x0068000, 0x00C0000 },
122     { 0x13, 0x0058000, 0x0020000, 0x00E0000, 0x00C8000 },
123     { 0x13, 0x0030000, 0x0030000, 0x00D0000, 0x0180000 },
124     { 0x14, 0x00B0000, 0x0040000, 0x01C0000, 0x0190000 },
125     { 0x14, 0x0060000, 0x0060000, 0x01A0000, 0x0300000 },
126     { 0x15, 0x0160000, 0x0080000, 0x0380000, 0x0320000 },
127     { 0x15, 0x00C0000, 0x00C0000, 0x0340000, 0x0600000 },
128     { 0x16, 0x02C0000, 0x0100000, 0x0700000, 0x0640000 },
129     { 0x16, 0x0180000, 0x0180000, 0x0680000, 0x0C00000 },
130     { 0x17, 0x0580000, 0x0200000, 0x0E00000, 0x0C80000 },
131     { 0x17, 0x0300000, 0x0300000, 0x0D00000, 0x1800000 },
132     { 0x18, 0x0B00000, 0x0400000, 0x1C00000, 0x1900000 },
133     { 0x18, 0x0600000, 0x0600000, 0x1A00000, 0x3000000 },
134     { 0x19, 0x1600000, 0x0800000, 0x3800000, 0x3200000 },
135     { 0x19, 0x0C00000, 0x0C00000, 0x3400000, 0x6000000 },
136     { 0x1A, 0x2C00000, 0x1000000, 0x7000000, 0x6400000 },
137     { 0x1A, 0x1800000, 0x1800000, 0x6800000, 0xC000000 },
138 };
139 
set_bps_params(AVCodecContext * avctx)140 static int set_bps_params(AVCodecContext *avctx)
141 {
142     switch (avctx->bits_per_raw_sample) {
143     case 8:
144         avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
145         break;
146     case 16:
147         avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
148         break;
149     case 24:
150         avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
151         break;
152     default:
153         av_log(avctx, AV_LOG_ERROR, "invalid/unsupported bits per sample: %d\n",
154                avctx->bits_per_raw_sample);
155         return AVERROR_INVALIDDATA;
156     }
157 
158     return 0;
159 }
160 
set_sample_rate_params(AVCodecContext * avctx)161 static void set_sample_rate_params(AVCodecContext *avctx)
162 {
163     TAKDecContext *s  = avctx->priv_data;
164     int shift         = 3 - (avctx->sample_rate / 11025);
165     shift             = FFMAX(0, shift);
166     s->uval           = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << shift;
167     s->subframe_scale = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << 1;
168 }
169 
tak_decode_init(AVCodecContext * avctx)170 static av_cold int tak_decode_init(AVCodecContext *avctx)
171 {
172     TAKDecContext *s = avctx->priv_data;
173 
174     ff_audiodsp_init(&s->adsp);
175 
176     s->avctx = avctx;
177     avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
178 
179     set_sample_rate_params(avctx);
180 
181     return set_bps_params(avctx);
182 }
183 
decode_lpc(int32_t * coeffs,int mode,int length)184 static void decode_lpc(int32_t *coeffs, int mode, int length)
185 {
186     int i;
187 
188     if (length < 2)
189         return;
190 
191     if (mode == 1) {
192         int a1 = *coeffs++;
193         for (i = 0; i < length - 1 >> 1; i++) {
194             *coeffs   += a1;
195             coeffs[1] += *coeffs;
196             a1         = coeffs[1];
197             coeffs    += 2;
198         }
199         if (length - 1 & 1)
200             *coeffs += a1;
201     } else if (mode == 2) {
202         int a1    = coeffs[1];
203         int a2    = a1 + *coeffs;
204         coeffs[1] = a2;
205         if (length > 2) {
206             coeffs += 2;
207             for (i = 0; i < length - 2 >> 1; i++) {
208                 int a3    = *coeffs + a1;
209                 int a4    = a3 + a2;
210                 *coeffs   = a4;
211                 a1        = coeffs[1] + a3;
212                 a2        = a1 + a4;
213                 coeffs[1] = a2;
214                 coeffs   += 2;
215             }
216             if (length & 1)
217                 *coeffs += a1 + a2;
218         }
219     } else if (mode == 3) {
220         int a1    = coeffs[1];
221         int a2    = a1 + *coeffs;
222         coeffs[1] = a2;
223         if (length > 2) {
224             int a3  = coeffs[2];
225             int a4  = a3 + a1;
226             int a5  = a4 + a2;
227             coeffs += 3;
228             for (i = 0; i < length - 3; i++) {
229                 a3     += *coeffs;
230                 a4     += a3;
231                 a5     += a4;
232                 *coeffs = a5;
233                 coeffs++;
234             }
235         }
236     }
237 }
238 
decode_segment(TAKDecContext * s,int8_t mode,int32_t * decoded,int len)239 static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len)
240 {
241     struct CParam code;
242     GetBitContext *gb = &s->gb;
243     int i;
244 
245     if (!mode) {
246         memset(decoded, 0, len * sizeof(*decoded));
247         return 0;
248     }
249 
250     if (mode > FF_ARRAY_ELEMS(xcodes))
251         return AVERROR_INVALIDDATA;
252     code = xcodes[mode - 1];
253 
254     for (i = 0; i < len; i++) {
255         int x = get_bits_long(gb, code.init);
256         if (x >= code.escape && get_bits1(gb)) {
257             x |= 1 << code.init;
258             if (x >= code.aescape) {
259                 int scale = get_unary(gb, 1, 9);
260                 if (scale == 9) {
261                     int scale_bits = get_bits(gb, 3);
262                     if (scale_bits > 0) {
263                         if (scale_bits == 7) {
264                             scale_bits += get_bits(gb, 5);
265                             if (scale_bits > 29)
266                                 return AVERROR_INVALIDDATA;
267                         }
268                         scale = get_bits_long(gb, scale_bits) + 1;
269                         x    += code.scale * scale;
270                     }
271                     x += code.bias;
272                 } else
273                     x += code.scale * scale - code.escape;
274             } else
275                 x -= code.escape;
276         }
277         decoded[i] = (x >> 1) ^ -(x & 1);
278     }
279 
280     return 0;
281 }
282 
decode_residues(TAKDecContext * s,int32_t * decoded,int length)283 static int decode_residues(TAKDecContext *s, int32_t *decoded, int length)
284 {
285     GetBitContext *gb = &s->gb;
286     int i, mode, ret;
287 
288     if (length > s->nb_samples)
289         return AVERROR_INVALIDDATA;
290 
291     if (get_bits1(gb)) {
292         int wlength, rval;
293 
294         wlength = length / s->uval;
295 
296         rval = length - (wlength * s->uval);
297 
298         if (rval < s->uval / 2)
299             rval += s->uval;
300         else
301             wlength++;
302 
303         if (wlength <= 1 || wlength > 128)
304             return AVERROR_INVALIDDATA;
305 
306         s->coding_mode[0] = mode = get_bits(gb, 6);
307 
308         for (i = 1; i < wlength; i++) {
309             int c = get_unary(gb, 1, 6);
310 
311             switch (c) {
312             case 6:
313                 mode = get_bits(gb, 6);
314                 break;
315             case 5:
316             case 4:
317             case 3: {
318                 /* mode += sign ? (1 - c) : (c - 1) */
319                 int sign = get_bits1(gb);
320                 mode    += (-sign ^ (c - 1)) + sign;
321                 break;
322             }
323             case 2:
324                 mode++;
325                 break;
326             case 1:
327                 mode--;
328                 break;
329             }
330             s->coding_mode[i] = mode;
331         }
332 
333         i = 0;
334         while (i < wlength) {
335             int len = 0;
336 
337             mode = s->coding_mode[i];
338             do {
339                 if (i >= wlength - 1)
340                     len += rval;
341                 else
342                     len += s->uval;
343                 i++;
344 
345                 if (i == wlength)
346                     break;
347             } while (s->coding_mode[i] == mode);
348 
349             if ((ret = decode_segment(s, mode, decoded, len)) < 0)
350                 return ret;
351             decoded += len;
352         }
353     } else {
354         mode = get_bits(gb, 6);
355         if ((ret = decode_segment(s, mode, decoded, length)) < 0)
356             return ret;
357     }
358 
359     return 0;
360 }
361 
get_bits_esc4(GetBitContext * gb)362 static int get_bits_esc4(GetBitContext *gb)
363 {
364     if (get_bits1(gb))
365         return get_bits(gb, 4) + 1;
366     else
367         return 0;
368 }
369 
decode_subframe(TAKDecContext * s,int32_t * decoded,int subframe_size,int prev_subframe_size)370 static int decode_subframe(TAKDecContext *s, int32_t *decoded,
371                            int subframe_size, int prev_subframe_size)
372 {
373     GetBitContext *gb = &s->gb;
374     int x= 0;
375     int y, i, j, ret = 0;
376     int dshift, size, filter_quant, filter_order;
377     int tfilter[MAX_PREDICTORS];
378 
379     if (!get_bits1(gb))
380         return decode_residues(s, decoded, subframe_size);
381 
382     filter_order = predictor_sizes[get_bits(gb, 4)];
383 
384     if (prev_subframe_size > 0 && get_bits1(gb)) {
385         if (filter_order > prev_subframe_size)
386             return AVERROR_INVALIDDATA;
387 
388         decoded       -= filter_order;
389         subframe_size += filter_order;
390 
391         if (filter_order > subframe_size)
392             return AVERROR_INVALIDDATA;
393     } else {
394         int lpc_mode;
395 
396         if (filter_order > subframe_size)
397             return AVERROR_INVALIDDATA;
398 
399         lpc_mode = get_bits(gb, 2);
400         if (lpc_mode > 2)
401             return AVERROR_INVALIDDATA;
402 
403         if ((ret = decode_residues(s, decoded, filter_order)) < 0)
404             return ret;
405 
406         if (lpc_mode)
407             decode_lpc(decoded, lpc_mode, filter_order);
408     }
409 
410     dshift = get_bits_esc4(gb);
411     size   = get_bits1(gb) + 6;
412 
413     filter_quant = 10;
414     if (get_bits1(gb)) {
415         filter_quant -= get_bits(gb, 3) + 1;
416         if (filter_quant < 3)
417             return AVERROR_INVALIDDATA;
418     }
419 
420     s->predictors[0] = get_sbits(gb, 10);
421     s->predictors[1] = get_sbits(gb, 10);
422     s->predictors[2] = get_sbits(gb, size) << (10 - size);
423     s->predictors[3] = get_sbits(gb, size) << (10 - size);
424     if (filter_order > 4) {
425         int tmp = size - get_bits1(gb);
426 
427         for (i = 4; i < filter_order; i++) {
428             if (!(i & 3))
429                 x = tmp - get_bits(gb, 2);
430             s->predictors[i] = get_sbits(gb, x) << (10 - size);
431         }
432     }
433 
434     tfilter[0] = s->predictors[0] << 6;
435     for (i = 1; i < filter_order; i++) {
436         int32_t *p1 = &tfilter[0];
437         int32_t *p2 = &tfilter[i - 1];
438 
439         for (j = 0; j < (i + 1) / 2; j++) {
440             x     = *p1 + (s->predictors[i] * *p2 + 256 >> 9);
441             *p2  += s->predictors[i] * *p1 + 256 >> 9;
442             *p1++ = x;
443             p2--;
444         }
445 
446         tfilter[i] = s->predictors[i] << 6;
447     }
448 
449     x = 1 << (32 - (15 - filter_quant));
450     y = 1 << ((15 - filter_quant) - 1);
451     for (i = 0, j = filter_order - 1; i < filter_order / 2; i++, j--) {
452         s->filter[j] = x - ((tfilter[i] + y) >> (15 - filter_quant));
453         s->filter[i] = x - ((tfilter[j] + y) >> (15 - filter_quant));
454     }
455 
456     if ((ret = decode_residues(s, &decoded[filter_order],
457                                subframe_size - filter_order)) < 0)
458         return ret;
459 
460     for (i = 0; i < filter_order; i++)
461         s->residues[i] = *decoded++ >> dshift;
462 
463     y    = FF_ARRAY_ELEMS(s->residues) - filter_order;
464     x    = subframe_size - filter_order;
465     while (x > 0) {
466         int tmp = FFMIN(y, x);
467 
468         for (i = 0; i < tmp; i++) {
469             int v = 1 << (filter_quant - 1);
470 
471             if (filter_order & -16)
472                 v += s->adsp.scalarproduct_int16(&s->residues[i], s->filter,
473                                                  filter_order & -16);
474             for (j = filter_order & -16; j < filter_order; j += 4) {
475                 v += s->residues[i + j + 3] * s->filter[j + 3] +
476                      s->residues[i + j + 2] * s->filter[j + 2] +
477                      s->residues[i + j + 1] * s->filter[j + 1] +
478                      s->residues[i + j    ] * s->filter[j    ];
479             }
480             v = (av_clip(v >> filter_quant, -8192, 8191) << dshift) - *decoded;
481             *decoded++ = v;
482             s->residues[filter_order + i] = v >> dshift;
483         }
484 
485         x -= tmp;
486         if (x > 0)
487             memcpy(s->residues, &s->residues[y], 2 * filter_order);
488     }
489 
490     emms_c();
491 
492     return 0;
493 }
494 
decode_channel(TAKDecContext * s,int chan)495 static int decode_channel(TAKDecContext *s, int chan)
496 {
497     AVCodecContext *avctx = s->avctx;
498     GetBitContext *gb     = &s->gb;
499     int32_t *decoded      = s->decoded[chan];
500     int left              = s->nb_samples - 1;
501     int i = 0, ret, prev = 0;
502 
503     s->sample_shift[chan] = get_bits_esc4(gb);
504     if (s->sample_shift[chan] >= avctx->bits_per_raw_sample)
505         return AVERROR_INVALIDDATA;
506 
507     *decoded++ = get_sbits(gb, avctx->bits_per_raw_sample - s->sample_shift[chan]);
508     s->lpc_mode[chan] = get_bits(gb, 2);
509     s->nb_subframes   = get_bits(gb, 3) + 1;
510 
511     if (s->nb_subframes > 1) {
512         if (get_bits_left(gb) < (s->nb_subframes - 1) * 6)
513             return AVERROR_INVALIDDATA;
514 
515         for (; i < s->nb_subframes - 1; i++) {
516             int v = get_bits(gb, 6);
517 
518             s->subframe_len[i] = (v - prev) * s->subframe_scale;
519             if (s->subframe_len[i] <= 0)
520                 return AVERROR_INVALIDDATA;
521 
522             left -= s->subframe_len[i];
523             prev  = v;
524         }
525 
526         if (left <= 0)
527             return AVERROR_INVALIDDATA;
528     }
529     s->subframe_len[i] = left;
530 
531     prev = 0;
532     for (i = 0; i < s->nb_subframes; i++) {
533         if ((ret = decode_subframe(s, decoded, s->subframe_len[i], prev)) < 0)
534             return ret;
535         decoded += s->subframe_len[i];
536         prev     = s->subframe_len[i];
537     }
538 
539     return 0;
540 }
541 
decorrelate(TAKDecContext * s,int c1,int c2,int length)542 static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
543 {
544     GetBitContext *gb = &s->gb;
545     int32_t *p1       = s->decoded[c1] + 1;
546     int32_t *p2       = s->decoded[c2] + 1;
547     int i;
548     int dshift, dfactor;
549 
550     switch (s->dmode) {
551     case 1: /* left/side */
552         for (i = 0; i < length; i++) {
553             int32_t a = p1[i];
554             int32_t b = p2[i];
555             p2[i]     = a + b;
556         }
557         break;
558     case 2: /* side/right */
559         for (i = 0; i < length; i++) {
560             int32_t a = p1[i];
561             int32_t b = p2[i];
562             p1[i]     = b - a;
563         }
564         break;
565     case 3: /* side/mid */
566         for (i = 0; i < length; i++) {
567             int32_t a = p1[i];
568             int32_t b = p2[i];
569             a        -= b >> 1;
570             p1[i]     = a;
571             p2[i]     = a + b;
572         }
573         break;
574     case 4: /* side/left with scale factor */
575         FFSWAP(int32_t*, p1, p2);
576     case 5: /* side/right with scale factor */
577         dshift  = get_bits_esc4(gb);
578         dfactor = get_sbits(gb, 10);
579         for (i = 0; i < length; i++) {
580             int32_t a = p1[i];
581             int32_t b = p2[i];
582             b         = dfactor * (b >> dshift) + 128 >> 8 << dshift;
583             p1[i]     = b - a;
584         }
585         break;
586     case 6:
587         FFSWAP(int32_t*, p1, p2);
588     case 7: {
589         int length2, order_half, filter_order, dval1, dval2;
590         int tmp, x;
591         int code_size = 0;
592 
593         if (length < 256)
594             return AVERROR_INVALIDDATA;
595 
596         dshift       = get_bits_esc4(gb);
597         filter_order = 8 << get_bits1(gb);
598         dval1        = get_bits1(gb);
599         dval2        = get_bits1(gb);
600 
601         for (i = 0; i < filter_order; i++) {
602             if (!(i & 3))
603                 code_size = 14 - get_bits(gb, 3);
604             s->filter[i] = get_sbits(gb, code_size);
605         }
606 
607         order_half = filter_order / 2;
608         length2    = length - (filter_order - 1);
609 
610         /* decorrelate beginning samples */
611         if (dval1) {
612             for (i = 0; i < order_half; i++) {
613                 int32_t a = p1[i];
614                 int32_t b = p2[i];
615                 p1[i]     = a + b;
616             }
617         }
618 
619         /* decorrelate ending samples */
620         if (dval2) {
621             for (i = length2 + order_half; i < length; i++) {
622                 int32_t a = p1[i];
623                 int32_t b = p2[i];
624                 p1[i]     = a + b;
625             }
626         }
627 
628 
629         for (i = 0; i < filter_order; i++)
630             s->residues[i] = *p2++ >> dshift;
631 
632         p1 += order_half;
633         x = FF_ARRAY_ELEMS(s->residues) - filter_order;
634         for (; length2 > 0; length2 -= tmp) {
635             tmp = FFMIN(length2, x);
636 
637             for (i = 0; i < tmp; i++)
638                 s->residues[filter_order + i] = *p2++ >> dshift;
639 
640             for (i = 0; i < tmp; i++) {
641                 int v = 1 << 9;
642 
643                 if (filter_order == 16) {
644                     v += s->adsp.scalarproduct_int16(&s->residues[i], s->filter,
645                                                      filter_order);
646                 } else {
647                     v += s->residues[i + 7] * s->filter[7] +
648                          s->residues[i + 6] * s->filter[6] +
649                          s->residues[i + 5] * s->filter[5] +
650                          s->residues[i + 4] * s->filter[4] +
651                          s->residues[i + 3] * s->filter[3] +
652                          s->residues[i + 2] * s->filter[2] +
653                          s->residues[i + 1] * s->filter[1] +
654                          s->residues[i    ] * s->filter[0];
655                 }
656 
657                 v = (av_clip(v >> 10, -8192, 8191) << dshift) - *p1;
658                 *p1++ = v;
659             }
660 
661             memcpy(s->residues, &s->residues[tmp], 2 * filter_order);
662         }
663 
664         emms_c();
665         break;
666     }
667     }
668 
669     return 0;
670 }
671 
tak_decode_frame(AVCodecContext * avctx,void * data,int * got_frame_ptr,AVPacket * pkt)672 static int tak_decode_frame(AVCodecContext *avctx, void *data,
673                             int *got_frame_ptr, AVPacket *pkt)
674 {
675     TAKDecContext *s  = avctx->priv_data;
676     AVFrame *frame    = data;
677 	ThreadFrame tframe = { .f = data };
678 	GetBitContext *gb = &s->gb;
679     int chan, i, ret, hsize;
680 
681     if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
682         return AVERROR_INVALIDDATA;
683 
684     if ((ret = init_get_bits8(gb, pkt->data, pkt->size)) < 0)
685         return ret;
686 
687     if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
688         return ret;
689 
690     hsize = get_bits_count(gb) / 8;
691     if (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) {
692         if (ff_tak_check_crc(pkt->data, hsize)) {
693             av_log(avctx, AV_LOG_ERROR, "CRC error\n");
694             if (avctx->err_recognition & AV_EF_EXPLODE)
695                 return AVERROR_INVALIDDATA;
696         }
697     }
698 
699     if (s->ti.codec != TAK_CODEC_MONO_STEREO &&
700         s->ti.codec != TAK_CODEC_MULTICHANNEL) {
701         av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec);
702         return AVERROR_PATCHWELCOME;
703     }
704     if (s->ti.data_type) {
705         av_log(avctx, AV_LOG_ERROR,
706                "unsupported data type: %d\n", s->ti.data_type);
707         return AVERROR_INVALIDDATA;
708     }
709     if (s->ti.codec == TAK_CODEC_MONO_STEREO && s->ti.channels > 2) {
710         av_log(avctx, AV_LOG_ERROR,
711                "invalid number of channels: %d\n", s->ti.channels);
712         return AVERROR_INVALIDDATA;
713     }
714     if (s->ti.channels > 6) {
715         av_log(avctx, AV_LOG_ERROR,
716                "unsupported number of channels: %d\n", s->ti.channels);
717         return AVERROR_INVALIDDATA;
718     }
719 
720     if (s->ti.frame_samples <= 0) {
721         av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\n");
722         return AVERROR_INVALIDDATA;
723     }
724 
725     avctx->bits_per_raw_sample = s->ti.bps;
726     if ((ret = set_bps_params(avctx)) < 0)
727         return ret;
728     if (s->ti.sample_rate != avctx->sample_rate) {
729         avctx->sample_rate = s->ti.sample_rate;
730         set_sample_rate_params(avctx);
731     }
732     if (s->ti.ch_layout)
733         avctx->channel_layout = s->ti.ch_layout;
734     avctx->channels = s->ti.channels;
735 
736     s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples
737                                              : s->ti.frame_samples;
738 
739     frame->nb_samples = s->nb_samples;
740     if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
741         return ret;
742     ff_thread_finish_setup(avctx);
743 
744     if (avctx->bits_per_raw_sample <= 16) {
745         int buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
746                                                   s->nb_samples,
747                                                   AV_SAMPLE_FMT_S32P, 0);
748         av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size, buf_size);
749         if (!s->decode_buffer)
750             return AVERROR(ENOMEM);
751         ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
752                                      s->decode_buffer, avctx->channels,
753                                      s->nb_samples, AV_SAMPLE_FMT_S32P, 0);
754         if (ret < 0)
755             return ret;
756     } else {
757         for (chan = 0; chan < avctx->channels; chan++)
758             s->decoded[chan] = (int32_t *)frame->extended_data[chan];
759     }
760 
761     if (s->nb_samples < 16) {
762         for (chan = 0; chan < avctx->channels; chan++) {
763             int32_t *decoded = s->decoded[chan];
764             for (i = 0; i < s->nb_samples; i++)
765                 decoded[i] = get_sbits(gb, avctx->bits_per_raw_sample);
766         }
767     } else {
768         if (s->ti.codec == TAK_CODEC_MONO_STEREO) {
769             for (chan = 0; chan < avctx->channels; chan++)
770                 if (ret = decode_channel(s, chan))
771                     return ret;
772 
773             if (avctx->channels == 2) {
774                 s->nb_subframes = get_bits(gb, 1) + 1;
775                 if (s->nb_subframes > 1) {
776                     s->subframe_len[1] = get_bits(gb, 6);
777                 }
778 
779                 s->dmode = get_bits(gb, 3);
780                 if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))
781                     return ret;
782             }
783         } else if (s->ti.codec == TAK_CODEC_MULTICHANNEL) {
784             if (get_bits1(gb)) {
785                 int ch_mask = 0;
786 
787                 chan = get_bits(gb, 4) + 1;
788                 if (chan > avctx->channels)
789                     return AVERROR_INVALIDDATA;
790 
791                 for (i = 0; i < chan; i++) {
792                     int nbit = get_bits(gb, 4);
793 
794                     if (nbit >= avctx->channels)
795                         return AVERROR_INVALIDDATA;
796 
797                     if (ch_mask & 1 << nbit)
798                         return AVERROR_INVALIDDATA;
799 
800                     s->mcdparams[i].present = get_bits1(gb);
801                     if (s->mcdparams[i].present) {
802                         s->mcdparams[i].index = get_bits(gb, 2);
803                         s->mcdparams[i].chan2 = get_bits(gb, 4);
804                         if (s->mcdparams[i].index == 1) {
805                             if ((nbit == s->mcdparams[i].chan2) ||
806                                 (ch_mask & 1 << s->mcdparams[i].chan2))
807                                 return AVERROR_INVALIDDATA;
808 
809                             ch_mask |= 1 << s->mcdparams[i].chan2;
810                         } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {
811                             return AVERROR_INVALIDDATA;
812                         }
813                     }
814                     s->mcdparams[i].chan1 = nbit;
815 
816                     ch_mask |= 1 << nbit;
817                 }
818             } else {
819                 chan = avctx->channels;
820                 for (i = 0; i < chan; i++) {
821                     s->mcdparams[i].present = 0;
822                     s->mcdparams[i].chan1   = i;
823                 }
824             }
825 
826             for (i = 0; i < chan; i++) {
827                 if (s->mcdparams[i].present && s->mcdparams[i].index == 1)
828                     if (ret = decode_channel(s, s->mcdparams[i].chan2))
829                         return ret;
830 
831                 if (ret = decode_channel(s, s->mcdparams[i].chan1))
832                     return ret;
833 
834                 if (s->mcdparams[i].present) {
835                     s->dmode = mc_dmodes[s->mcdparams[i].index];
836                     if (ret = decorrelate(s,
837                                           s->mcdparams[i].chan2,
838                                           s->mcdparams[i].chan1,
839                                           s->nb_samples - 1))
840                         return ret;
841                 }
842             }
843         }
844 
845         for (chan = 0; chan < avctx->channels; chan++) {
846             int32_t *decoded = s->decoded[chan];
847 
848             if (s->lpc_mode[chan])
849                 decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples);
850 
851             if (s->sample_shift[chan] > 0)
852                 for (i = 0; i < s->nb_samples; i++)
853                     decoded[i] <<= s->sample_shift[chan];
854         }
855     }
856 
857     align_get_bits(gb);
858     skip_bits(gb, 24);
859     if (get_bits_left(gb) < 0)
860         av_log(avctx, AV_LOG_DEBUG, "overread\n");
861     else if (get_bits_left(gb) > 0)
862         av_log(avctx, AV_LOG_DEBUG, "underread\n");
863 
864     if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_COMPLIANT)) {
865         if (ff_tak_check_crc(pkt->data + hsize,
866                              get_bits_count(gb) / 8 - hsize)) {
867             av_log(avctx, AV_LOG_ERROR, "CRC error\n");
868             if (avctx->err_recognition & AV_EF_EXPLODE)
869                 return AVERROR_INVALIDDATA;
870         }
871     }
872 
873     /* convert to output buffer */
874     switch (avctx->sample_fmt) {
875     case AV_SAMPLE_FMT_U8P:
876         for (chan = 0; chan < avctx->channels; chan++) {
877             uint8_t *samples = (uint8_t *)frame->extended_data[chan];
878             int32_t *decoded = s->decoded[chan];
879             for (i = 0; i < s->nb_samples; i++)
880                 samples[i] = decoded[i] + 0x80;
881         }
882         break;
883     case AV_SAMPLE_FMT_S16P:
884         for (chan = 0; chan < avctx->channels; chan++) {
885             int16_t *samples = (int16_t *)frame->extended_data[chan];
886             int32_t *decoded = s->decoded[chan];
887             for (i = 0; i < s->nb_samples; i++)
888                 samples[i] = decoded[i];
889         }
890         break;
891     case AV_SAMPLE_FMT_S32P:
892         for (chan = 0; chan < avctx->channels; chan++) {
893             int32_t *samples = (int32_t *)frame->extended_data[chan];
894             for (i = 0; i < s->nb_samples; i++)
895                 samples[i] <<= 8;
896         }
897         break;
898     }
899 
900     *got_frame_ptr = 1;
901 
902     return pkt->size;
903 }
904 
init_thread_copy(AVCodecContext * avctx)905 static int init_thread_copy(AVCodecContext *avctx)
906 {
907     TAKDecContext *s = avctx->priv_data;
908     s->avctx = avctx;
909     return 0;
910 }
911 
update_thread_context(AVCodecContext * dst,const AVCodecContext * src)912 static int update_thread_context(AVCodecContext *dst,
913                                  const AVCodecContext *src)
914 {
915     TAKDecContext *tsrc = src->priv_data;
916     TAKDecContext *tdst = dst->priv_data;
917 
918     if (dst == src)
919         return 0;
920     memcpy(&tdst->ti, &tsrc->ti, sizeof(TAKStreamInfo));
921     return 0;
922 }
923 
tak_decode_close(AVCodecContext * avctx)924 static av_cold int tak_decode_close(AVCodecContext *avctx)
925 {
926     TAKDecContext *s = avctx->priv_data;
927 
928     av_freep(&s->decode_buffer);
929 
930     return 0;
931 }
932 
933 AVCodec ff_tak_decoder = {
934 	.name             = "tak",
935     .long_name        = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
936     .type             = AVMEDIA_TYPE_AUDIO,
937     .id               = AV_CODEC_ID_TAK,
938     .priv_data_size   = sizeof(TAKDecContext),
939     .init             = tak_decode_init,
940     .close            = tak_decode_close,
941     .decode           = tak_decode_frame,
942     .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
943     .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
944     .capabilities     = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
945     .sample_fmts      = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
946                                                         AV_SAMPLE_FMT_S16P,
947                                                         AV_SAMPLE_FMT_S32P,
948                                                         AV_SAMPLE_FMT_NONE },
949 };
950