3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
24 * TAK (Tom's lossless Audio Kompressor) decoder
25 * @author Paul B Mahol
33 #define MAX_SUBFRAMES 8 ///< max number of subframes per channel
34 #define MAX_PREDICTORS 256
36 typedef struct MCDParam {
37 int8_t present; ///< is decorrelation parameters available for this channel
38 int8_t index; ///< index into array of decorrelation types
43 typedef struct TAKDecContext {
44 AVCodecContext *avctx; ///< parent AVCodecContext
45 AVFrame frame; ///< AVFrame for decoded output
48 GetBitContext gb; ///< bitstream reader initialized to start at the current frame
50 int nb_samples; ///< number of samples in the current frame
51 int32_t *decode_buffer;
52 int decode_buffer_size;
53 int32_t *decoded[TAK_MAX_CHANNELS]; ///< decoded samples for each channel
55 int8_t lpc_mode[TAK_MAX_CHANNELS];
56 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
66 int8_t dmode; ///< channel decorrelation type in the current frame
72 MCDParam mcdparams[TAK_MAX_CHANNELS]; ///< multichannel decorrelation parameters
77 int8_t coding_mode[128];
78 DECLARE_ALIGNED(16, int16_t, filter)[MAX_PREDICTORS];
79 DECLARE_ALIGNED(16, int16_t, residues)[544];
82 static const int8_t mc_dmodes[] = {
86 static const uint16_t predictor_sizes[] = {
87 4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0,
90 static const struct CParam {
97 { 0x01, 0x0000001, 0x0000001, 0x0000003, 0x0000008 },
98 { 0x02, 0x0000003, 0x0000001, 0x0000007, 0x0000006 },
99 { 0x03, 0x0000005, 0x0000002, 0x000000E, 0x000000D },
100 { 0x03, 0x0000003, 0x0000003, 0x000000D, 0x0000018 },
101 { 0x04, 0x000000B, 0x0000004, 0x000001C, 0x0000019 },
102 { 0x04, 0x0000006, 0x0000006, 0x000001A, 0x0000030 },
103 { 0x05, 0x0000016, 0x0000008, 0x0000038, 0x0000032 },
104 { 0x05, 0x000000C, 0x000000C, 0x0000034, 0x0000060 },
105 { 0x06, 0x000002C, 0x0000010, 0x0000070, 0x0000064 },
106 { 0x06, 0x0000018, 0x0000018, 0x0000068, 0x00000C0 },
107 { 0x07, 0x0000058, 0x0000020, 0x00000E0, 0x00000C8 },
108 { 0x07, 0x0000030, 0x0000030, 0x00000D0, 0x0000180 },
109 { 0x08, 0x00000B0, 0x0000040, 0x00001C0, 0x0000190 },
110 { 0x08, 0x0000060, 0x0000060, 0x00001A0, 0x0000300 },
111 { 0x09, 0x0000160, 0x0000080, 0x0000380, 0x0000320 },
112 { 0x09, 0x00000C0, 0x00000C0, 0x0000340, 0x0000600 },
113 { 0x0A, 0x00002C0, 0x0000100, 0x0000700, 0x0000640 },
114 { 0x0A, 0x0000180, 0x0000180, 0x0000680, 0x0000C00 },
115 { 0x0B, 0x0000580, 0x0000200, 0x0000E00, 0x0000C80 },
116 { 0x0B, 0x0000300, 0x0000300, 0x0000D00, 0x0001800 },
117 { 0x0C, 0x0000B00, 0x0000400, 0x0001C00, 0x0001900 },
118 { 0x0C, 0x0000600, 0x0000600, 0x0001A00, 0x0003000 },
119 { 0x0D, 0x0001600, 0x0000800, 0x0003800, 0x0003200 },
120 { 0x0D, 0x0000C00, 0x0000C00, 0x0003400, 0x0006000 },
121 { 0x0E, 0x0002C00, 0x0001000, 0x0007000, 0x0006400 },
122 { 0x0E, 0x0001800, 0x0001800, 0x0006800, 0x000C000 },
123 { 0x0F, 0x0005800, 0x0002000, 0x000E000, 0x000C800 },
124 { 0x0F, 0x0003000, 0x0003000, 0x000D000, 0x0018000 },
125 { 0x10, 0x000B000, 0x0004000, 0x001C000, 0x0019000 },
126 { 0x10, 0x0006000, 0x0006000, 0x001A000, 0x0030000 },
127 { 0x11, 0x0016000, 0x0008000, 0x0038000, 0x0032000 },
128 { 0x11, 0x000C000, 0x000C000, 0x0034000, 0x0060000 },
129 { 0x12, 0x002C000, 0x0010000, 0x0070000, 0x0064000 },
130 { 0x12, 0x0018000, 0x0018000, 0x0068000, 0x00C0000 },
131 { 0x13, 0x0058000, 0x0020000, 0x00E0000, 0x00C8000 },
132 { 0x13, 0x0030000, 0x0030000, 0x00D0000, 0x0180000 },
133 { 0x14, 0x00B0000, 0x0040000, 0x01C0000, 0x0190000 },
134 { 0x14, 0x0060000, 0x0060000, 0x01A0000, 0x0300000 },
135 { 0x15, 0x0160000, 0x0080000, 0x0380000, 0x0320000 },
136 { 0x15, 0x00C0000, 0x00C0000, 0x0340000, 0x0600000 },
137 { 0x16, 0x02C0000, 0x0100000, 0x0700000, 0x0640000 },
138 { 0x16, 0x0180000, 0x0180000, 0x0680000, 0x0C00000 },
139 { 0x17, 0x0580000, 0x0200000, 0x0E00000, 0x0C80000 },
140 { 0x17, 0x0300000, 0x0300000, 0x0D00000, 0x1800000 },
141 { 0x18, 0x0B00000, 0x0400000, 0x1C00000, 0x1900000 },
142 { 0x18, 0x0600000, 0x0600000, 0x1A00000, 0x3000000 },
143 { 0x19, 0x1600000, 0x0800000, 0x3800000, 0x3200000 },
144 { 0x19, 0x0C00000, 0x0C00000, 0x3400000, 0x6000000 },
145 { 0x1A, 0x2C00000, 0x1000000, 0x7000000, 0x6400000 },
146 { 0x1A, 0x1800000, 0x1800000, 0x6800000, 0xC000000 },
149 static void tak_set_bps(AVCodecContext *avctx)
151 switch (avctx->bits_per_coded_sample) {
153 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
156 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
159 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
164 static int get_shift(int sample_rate)
168 if (sample_rate < 11025)
170 else if (sample_rate < 22050)
172 else if (sample_rate < 44100)
180 static int get_scale(int sample_rate, int shift)
182 return FFALIGN(sample_rate + 511 >> 9, 4) << shift;
185 static av_cold int tak_decode_init(AVCodecContext *avctx)
187 TAKDecContext *s = avctx->priv_data;
190 ff_dsputil_init(&s->dsp, avctx);
193 avcodec_get_frame_defaults(&s->frame);
194 avctx->coded_frame = &s->frame;
196 s->uval = get_scale(avctx->sample_rate, get_shift(avctx->sample_rate));
197 s->subframe_scale = get_scale(avctx->sample_rate, 1);
204 static int get_code(GetBitContext *gb, int nbits)
210 return get_sbits(gb, nbits);
214 static void decode_lpc(int32_t *coeffs, int mode, int length)
216 int i, a1, a2, a3, a4, a5;
223 for (i = 0; i < (length - 1 >> 1); i++) {
225 coeffs[1] += *coeffs;
229 if ((length - 1) & 1)
231 } else if (mode == 2) {
237 for (i = 0; i < (length - 2 >> 1); i++) {
249 } else if (mode == 3) {
258 for (i = 0; i < length - 3; i++) {
269 static int decode_segment(TAKDecContext *s, int8_t value, int32_t *dst, int len)
271 GetBitContext *gb = &s->gb;
274 memset(dst, 0, len * 4);
281 x = get_bits_long(gb, xcodes[value].init);
282 if (x >= xcodes[value].escape)
284 dst[i++] = (x >> 1) ^ -(x & 1);
290 x = (y << xcodes[value].init) | x;
291 if (x >= xcodes[value].aescape) {
292 int c = get_unary(gb, 1, 9);
297 z = x + xcodes[value].bias;
300 d = get_bits(gb, 5) + 7;
302 return AVERROR_INVALIDDATA;
305 z += xcodes[value].scale * (get_bits_long(gb, d) + 1);
307 z = xcodes[value].scale * c + x - xcodes[value].escape;
310 z = x - (xcodes[value].escape & -y);
312 dst[i++] = (z >> 1) ^ -(z & 1);
319 static int xget(TAKDecContext *s, int d, int q)
325 s->rval = d - (x * q);
327 if (s->rval < q / 2) {
333 if (x <= 1 || x > 128)
339 static int get_len(TAKDecContext *s, int b)
341 if (b >= s->wlength - 1)
347 static int decode_coeffs(TAKDecContext *s, int32_t *dst, int length)
349 GetBitContext *gb = &s->gb;
352 if (length > s->nb_samples)
353 return AVERROR_INVALIDDATA;
356 if ((s->wlength = xget(s, length, s->uval)) < 0)
357 return AVERROR_INVALIDDATA;
359 s->coding_mode[0] = v = get_bits(gb, 6);
360 if (s->coding_mode[0] > FF_ARRAY_ELEMS(xcodes))
361 return AVERROR_INVALIDDATA;
363 for (i = 1; i < s->wlength; i++) {
364 int c = get_unary(gb, 1, 6);
369 int t = get_bits1(gb);
371 v += (-t ^ (c - 1)) + t;
373 v += (-(c & 1) ^ (((c & 1) + c) >> 1)) + (c & 1);
376 if (v > FF_ARRAY_ELEMS(xcodes))
377 return AVERROR_INVALIDDATA;
378 s->coding_mode[i] = v;
382 while (i < s->wlength) {
385 v = s->coding_mode[i];
387 len += get_len(s, i);
392 } while (v == s->coding_mode[i]);
394 if ((ret = decode_segment(s, v, dst, len)) < 0)
400 if (v > FF_ARRAY_ELEMS(xcodes))
401 return AVERROR_INVALIDDATA;
402 if ((ret = decode_segment(s, v, dst, length)) < 0)
409 static int get_b(GetBitContext *gb)
412 return get_bits(gb, 4) + 1;
417 static int decode_subframe(TAKDecContext *s, int32_t *ptr, int subframe_size,
418 int prev_subframe_size)
420 GetBitContext *gb = &s->gb;
421 int tmp, x, y, i, j, ret = 0;
422 int tfilter[MAX_PREDICTORS];
425 s->filter_order = predictor_sizes[get_bits(gb, 4)];
427 if (prev_subframe_size > 0 && get_bits1(gb)) {
428 if (s->filter_order > prev_subframe_size)
429 return AVERROR_INVALIDDATA;
431 ptr -= s->filter_order;
432 subframe_size += s->filter_order;
434 if (s->filter_order > subframe_size)
435 return AVERROR_INVALIDDATA;
439 if (s->filter_order > subframe_size)
440 return AVERROR_INVALIDDATA;
442 lpc = get_bits(gb, 2);
444 return AVERROR_INVALIDDATA;
446 if ((ret = decode_coeffs(s, ptr, s->filter_order)) < 0)
449 decode_lpc(ptr, lpc, s->filter_order);
453 s->size = get_bits1(gb) + 5;
456 s->ared = get_bits(gb, 3) + 1;
458 return AVERROR_INVALIDDATA;
462 s->predictors[0] = get_code(gb, 10);
463 s->predictors[1] = get_code(gb, 10);
464 s->predictors[2] = get_code(gb, s->size + 1) << (9 - s->size);
465 s->predictors[3] = get_code(gb, s->size + 1) << (9 - s->size);
466 if (s->filter_order > 4) {
467 tmp = s->size + 1 - get_bits1(gb);
469 for (i = 4; i < s->filter_order; i++) {
471 x = tmp - get_bits(gb, 2);
472 s->predictors[i] = get_code(gb, x) << (9 - s->size);
476 tfilter[0] = s->predictors[0] << 6;
477 for (i = 1; i < s->filter_order; i++) {
478 int32_t *p1 = &tfilter[0];
479 int32_t *p2 = &tfilter[i - 1];
481 for (j = 0; j < (i + 1) / 2; j++) {
482 x = *p1 + (s->predictors[i] * *p2 + 256 >> 9);
483 *p2 += s->predictors[i] * *p1 + 256 >> 9;
488 tfilter[i] = s->predictors[i] << 6;
491 x = -1 << (32 - (s->ared + 5));
492 y = 1 << ((s->ared + 5) - 1);
493 for (i = 0, j = s->filter_order - 1; i < s->filter_order / 2; i++, j--) {
494 tmp = y + tfilter[j];
495 s->filter[j] = -(x & -(y + tfilter[i] >> 31) |
496 (y + tfilter[i]) >> (s->ared + 5));
497 s->filter[i] = -(x & -(tmp >> 31) | (tmp >> s->ared + 5));
500 if ((ret = decode_coeffs(s, &ptr[s->filter_order],
501 subframe_size - s->filter_order)) < 0)
504 for (i = 0; i < s->filter_order; i++)
505 s->residues[i] = *ptr++ >> s->xred;
507 y = FF_ARRAY_ELEMS(s->residues) - s->filter_order;
508 x = subframe_size - s->filter_order;
512 for (i = 0; i < tmp; i++) {
515 v = 1 << (10 - s->ared - 1);
516 if (!(s->filter_order & 15)) {
517 v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
519 } else if (s->filter_order & 4) {
520 for (j = 0; j < s->filter_order; j += 4) {
521 v += s->residues[i + j + 3] * s->filter[j + 3] +
522 s->residues[i + j + 2] * s->filter[j + 2] +
523 s->residues[i + j + 1] * s->filter[j + 1] +
524 s->residues[i + j ] * s->filter[j ];
527 for (j = 0; j < s->filter_order; j += 8) {
528 v += s->residues[i + j + 7] * s->filter[j + 7] +
529 s->residues[i + j + 6] * s->filter[j + 6] +
530 s->residues[i + j + 5] * s->filter[j + 5] +
531 s->residues[i + j + 4] * s->filter[j + 4] +
532 s->residues[i + j + 3] * s->filter[j + 3] +
533 s->residues[i + j + 2] * s->filter[j + 2] +
534 s->residues[i + j + 1] * s->filter[j + 1] +
535 s->residues[i + j ] * s->filter[j ];
538 m = (-1 << (32 - (10 - s->ared))) & -(v >> 31) | (v >> 10 - s->ared);
539 m = av_clip(m, -8192, 8191);
540 w = (m << s->xred) - *ptr;
542 s->residues[s->filter_order + i] = w >> s->xred;
547 memcpy(s->residues, &s->residues[y], 2 * s->filter_order);
552 ret = decode_coeffs(s, ptr, subframe_size);
558 static int decode_channel(TAKDecContext *s, int chan)
560 AVCodecContext *avctx = s->avctx;
561 GetBitContext *gb = &s->gb;
562 int32_t *dst = s->decoded[chan];
563 int i = 0, ret, prev = 0;
564 int left = s->nb_samples - 1;
566 s->sample_shift[chan] = get_b(gb);
567 if (s->sample_shift[chan] >= avctx->bits_per_coded_sample)
568 return AVERROR_INVALIDDATA;
570 *dst++ = get_code(gb, avctx->bits_per_coded_sample - s->sample_shift[chan]);
571 s->lpc_mode[chan] = get_bits(gb, 2);
572 s->nb_subframes = get_bits(gb, 3) + 1;
574 if (s->nb_subframes > 1) {
575 if (get_bits_left(gb) < (s->nb_subframes - 1) * 6)
576 return AVERROR_INVALIDDATA;
578 for (; i < s->nb_subframes - 1; i++) {
579 int v = get_bits(gb, 6);
581 s->subframe_len[i] = (v - prev) * s->subframe_scale;
582 if (s->subframe_len[i] <= 0)
583 return AVERROR_INVALIDDATA;
585 left -= s->subframe_len[i];
590 return AVERROR_INVALIDDATA;
593 s->subframe_len[i] = left;
595 for (i = 0; i < s->nb_subframes; i++) {
596 if ((ret = decode_subframe(s, dst, s->subframe_len[i], prev)) < 0)
598 dst += s->subframe_len[i];
599 prev = s->subframe_len[i];
605 static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
607 GetBitContext *gb = &s->gb;
608 uint32_t *p1 = s->decoded[c1] + 1;
609 uint32_t *p2 = s->decoded[c2] + 1;
613 s->dshift = get_b(gb);
616 s->filter_order = 16;
620 s->dval1 = get_bits1(gb);
621 s->dval2 = get_bits1(gb);
623 for (i = 0; i < s->filter_order; i++) {
625 x = 14 - get_bits(gb, 3);
626 s->filter[i] = get_code(gb, x);
629 s->dfactor = get_code(gb, 10);
635 for (i = 0; i < length; i++, p1++, p2++)
639 for (i = 0; i < length; i++, p1++, p2++)
643 for (i = 0; i < length; i++, p1++, p2++) {
644 x = (*p2 & 1) + 2 * *p1;
647 *p1 = a & 0x80000000 | (a >> 1);
648 *p2 = b & 0x80000000 | (b >> 1);
652 FFSWAP(uint32_t *, p1, p2);
655 tmp = -1 << (32 - s->dshift);
659 for (i = 0; i < length; i++, p1++, p2++) {
660 x = s->dfactor * (tmp & -(*p2 >> 31) | (*p2 >> s->dshift)) + 128;
661 *p1 = ((-(x >> 31) & 0xFF000000 | (x >> 8)) << s->dshift) - *p1;
665 FFSWAP(uint32_t *, p1, p2);
668 return AVERROR_INVALIDDATA;
670 a = s->filter_order / 2;
671 b = length - (s->filter_order - 1);
674 for (i = 0; i < a; i++)
680 for (i = 0; i < length - x; i++)
681 p1[x + i] += p2[x + i];
684 for (i = 0; i < s->filter_order; i++)
685 s->residues[i] = *p2++ >> s->dshift;
688 x = FF_ARRAY_ELEMS(s->residues) - s->filter_order;
689 for (; b > 0; b -= tmp) {
692 for (i = 0; i < tmp; i++)
693 s->residues[s->filter_order + i] = *p2++ >> s->dshift;
695 for (i = 0; i < tmp; i++) {
700 if (s->filter_order == 16) {
701 v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
704 v += s->residues[i + 7] * s->filter[7] +
705 s->residues[i + 6] * s->filter[6] +
706 s->residues[i + 5] * s->filter[5] +
707 s->residues[i + 4] * s->filter[4] +
708 s->residues[i + 3] * s->filter[3] +
709 s->residues[i + 2] * s->filter[2] +
710 s->residues[i + 1] * s->filter[1] +
711 s->residues[i ] * s->filter[0];
714 m = (-1 << 22) & -(v >> 31) | (v >> 10);
715 m = av_clip(m, -8192, 8191);
716 w = (m << s->dshift) - *p1;
720 memcpy(s->residues, &s->residues[tmp], 2 * s->filter_order);
730 static int tak_decode_frame(AVCodecContext *avctx, void *data,
731 int *got_frame_ptr, AVPacket *pkt)
733 TAKDecContext *s = avctx->priv_data;
734 GetBitContext *gb = &s->gb;
735 int chan, i, ret, hsize;
738 if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
739 return AVERROR_INVALIDDATA;
741 init_get_bits(gb, pkt->data, pkt->size * 8);
743 if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
746 if (avctx->err_recognition & AV_EF_CRCCHECK) {
747 hsize = get_bits_count(gb) / 8;
748 if (ff_tak_check_crc(pkt->data, hsize)) {
749 av_log(avctx, AV_LOG_ERROR, "CRC error\n");
750 return AVERROR_INVALIDDATA;
754 if (s->ti.codec != 2 && s->ti.codec != 4) {
755 av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec);
756 return AVERROR_PATCHWELCOME;
758 if (s->ti.data_type) {
759 av_log(avctx, AV_LOG_ERROR, "unsupported data type: %d\n", s->ti.data_type);
760 return AVERROR_INVALIDDATA;
762 if (s->ti.codec == 2 && s->ti.channels > 2) {
763 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", s->ti.channels);
764 return AVERROR_INVALIDDATA;
766 if (s->ti.channels > 6) {
767 av_log(avctx, AV_LOG_ERROR, "unsupported number of channels: %d\n", s->ti.channels);
768 return AVERROR_INVALIDDATA;
771 if (s->ti.frame_samples <= 0) {
772 av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\n");
773 return AVERROR_INVALIDDATA;
776 if (s->ti.bps != avctx->bits_per_coded_sample) {
777 avctx->bits_per_coded_sample = s->ti.bps;
780 if (s->ti.sample_rate != avctx->sample_rate) {
781 avctx->sample_rate = s->ti.sample_rate;
782 s->uval = get_scale(avctx->sample_rate, get_shift(avctx->sample_rate));
783 s->subframe_scale = get_scale(avctx->sample_rate, 1);
786 avctx->channel_layout = s->ti.ch_layout;
787 avctx->channels = s->ti.channels;
789 s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples :
792 s->frame.nb_samples = s->nb_samples;
793 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0)
796 if (avctx->bits_per_coded_sample <= 16) {
797 av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size,
798 sizeof(*s->decode_buffer) * FFALIGN(s->nb_samples, 8) *
799 avctx->channels + FF_INPUT_BUFFER_PADDING_SIZE);
800 if (!s->decode_buffer)
801 return AVERROR(ENOMEM);
802 for (chan = 0; chan < avctx->channels; chan++)
803 s->decoded[chan] = s->decode_buffer +
804 chan * FFALIGN(s->nb_samples, 8);
806 for (chan = 0; chan < avctx->channels; chan++)
807 s->decoded[chan] = (int32_t *)s->frame.data[chan];
810 if (s->nb_samples < 16) {
811 for (chan = 0; chan < avctx->channels; chan++) {
812 p = s->decoded[chan];
813 for (i = 0; i < s->nb_samples; i++)
814 *p++ = get_code(gb, avctx->bits_per_coded_sample);
817 if (s->ti.codec == 2) {
818 for (chan = 0; chan < avctx->channels; chan++) {
819 if (ret = decode_channel(s, chan))
823 if (avctx->channels == 2) {
824 s->nb_subframes = get_bits(gb, 1) + 1;
825 if (s->nb_subframes > 1)
826 s->subframe_len[1] = get_bits(gb, 6);
828 s->dmode = get_bits(gb, 3);
829 if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))
832 } else if (s->ti.codec == 4) {
836 chan = get_bits(gb, 4) + 1;
837 if (chan > avctx->channels)
838 return AVERROR_INVALIDDATA;
840 for (i = 0; i < chan; i++) {
841 int nbit = get_bits(gb, 4);
843 if (nbit >= avctx->channels)
844 return AVERROR_INVALIDDATA;
846 if (ch_mask & 1 << nbit)
847 return AVERROR_INVALIDDATA;
849 s->mcdparams[i].present = get_bits1(gb);
850 if (s->mcdparams[i].present) {
851 s->mcdparams[i].index = get_bits(gb, 2);
852 s->mcdparams[i].chan2 = get_bits(gb, 4);
853 if (s->mcdparams[i].index == 1) {
854 if ((nbit == s->mcdparams[i].chan2) ||
855 (ch_mask & 1 << s->mcdparams[i].chan2))
856 return AVERROR_INVALIDDATA;
858 ch_mask |= 1 << s->mcdparams[i].chan2;
859 } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {
860 return AVERROR_INVALIDDATA;
863 s->mcdparams[i].chan1 = nbit;
865 ch_mask |= 1 << nbit;
868 chan = avctx->channels;
869 for (i = 0; i < chan; i++) {
870 s->mcdparams[i].present = 0;
871 s->mcdparams[i].chan1 = i;
875 for (i = 0; i < chan; i++) {
876 if (s->mcdparams[i].present && s->mcdparams[i].index == 1) {
877 if (ret = decode_channel(s, s->mcdparams[i].chan2))
881 if (ret = decode_channel(s, s->mcdparams[i].chan1))
884 if (s->mcdparams[i].present) {
885 s->dmode = mc_dmodes[s->mcdparams[i].index];
886 if (ret = decorrelate(s, s->mcdparams[i].chan2,
887 s->mcdparams[i].chan1,
894 for (chan = 0; chan < avctx->channels; chan++) {
895 p = s->decoded[chan];
896 decode_lpc(p, s->lpc_mode[chan], s->nb_samples);
898 if (s->sample_shift[chan] > 0) {
899 for (i = 0; i < s->nb_samples; i++)
900 *p++ <<= s->sample_shift[chan];
907 if (get_bits_left(gb) < 0)
908 av_log(avctx, AV_LOG_DEBUG, "overread\n");
909 else if (get_bits_left(gb) > 0)
910 av_log(avctx, AV_LOG_DEBUG, "underread\n");
912 if (avctx->err_recognition & AV_EF_CRCCHECK) {
913 if (ff_tak_check_crc(pkt->data + hsize,
914 get_bits_count(gb) / 8 - hsize)) {
915 av_log(avctx, AV_LOG_ERROR, "CRC error\n");
916 return AVERROR_INVALIDDATA;
920 // convert to output buffer
921 switch (avctx->bits_per_coded_sample) {
923 for (chan = 0; chan < avctx->channels; chan++) {
924 uint8_t *samples = (uint8_t *)s->frame.data[chan];
925 p = s->decoded[chan];
926 for (i = 0; i < s->nb_samples; i++, p++)
927 *samples++ = *p + 0x80;
931 for (chan = 0; chan < avctx->channels; chan++) {
932 int16_t *samples = (int16_t *)s->frame.data[chan];
933 p = s->decoded[chan];
934 for (i = 0; i < s->nb_samples; i++, p++)
939 for (chan = 0; chan < avctx->channels; chan++) {
940 int32_t *samples = (int32_t *)s->frame.data[chan];
941 for (i = 0; i < s->nb_samples; i++)
948 *(AVFrame *)data = s->frame;
953 static av_cold int tak_decode_close(AVCodecContext *avctx)
955 TAKDecContext *s = avctx->priv_data;
957 av_freep(&s->decode_buffer);
962 AVCodec ff_tak_decoder = {
964 .type = AVMEDIA_TYPE_AUDIO,
965 .id = AV_CODEC_ID_TAK,
966 .priv_data_size = sizeof(TAKDecContext),
967 .init = tak_decode_init,
968 .close = tak_decode_close,
969 .decode = tak_decode_frame,
970 .capabilities = CODEC_CAP_DR1,
971 .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),