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