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
28 #include "libavutil/internal.h"
29 #include "libavutil/samplefmt.h"
31 #define BITSTREAM_READER_LE
40 #define MAX_SUBFRAMES 8 ///< max number of subframes per channel
41 #define MAX_PREDICTORS 256
43 typedef struct MCDParam {
44 int8_t present; ///< decorrelation parameter availability for this channel
45 int8_t index; ///< index into array of decorrelation types
50 typedef struct TAKDecContext {
51 AVCodecContext *avctx; ///< parent AVCodecContext
55 GetBitContext gb; ///< bitstream reader initialized to start at the current frame
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
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
70 int8_t dmode; ///< channel decorrelation type in the current frame
72 MCDParam mcdparams[TAK_MAX_CHANNELS]; ///< multichannel decorrelation parameters
74 int8_t coding_mode[128];
75 DECLARE_ALIGNED(16, int16_t, filter)[MAX_PREDICTORS];
76 DECLARE_ALIGNED(16, int16_t, residues)[544];
79 static const int8_t mc_dmodes[] = { 1, 3, 4, 6, };
81 static const uint16_t predictor_sizes[] = {
82 4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0,
85 static const struct CParam {
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 },
144 static int set_bps_params(AVCodecContext *avctx)
146 switch (avctx->bits_per_raw_sample) {
148 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
151 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
154 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
157 av_log(avctx, AV_LOG_ERROR, "invalid/unsupported bits per sample: %d\n",
158 avctx->bits_per_raw_sample);
159 return AVERROR_INVALIDDATA;
165 static void set_sample_rate_params(AVCodecContext *avctx)
167 TAKDecContext *s = avctx->priv_data;
170 if (avctx->sample_rate < 11025) {
172 } else if (avctx->sample_rate < 22050) {
174 } else if (avctx->sample_rate < 44100) {
179 s->uval = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << shift;
180 s->subframe_scale = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << 1;
183 static av_cold int tak_decode_init(AVCodecContext *avctx)
185 TAKDecContext *s = avctx->priv_data;
187 ff_audiodsp_init(&s->adsp);
188 ff_takdsp_init(&s->tdsp);
191 avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
193 set_sample_rate_params(avctx);
195 return set_bps_params(avctx);
198 static void decode_lpc(int32_t *coeffs, int mode, int length)
206 unsigned a1 = *coeffs++;
207 for (i = 0; i < length - 1 >> 1; i++) {
209 coeffs[1] += (unsigned)*coeffs;
215 } else if (mode == 2) {
216 unsigned a1 = coeffs[1];
217 unsigned a2 = a1 + *coeffs;
221 for (i = 0; i < length - 2 >> 1; i++) {
222 unsigned a3 = *coeffs + a1;
223 unsigned a4 = a3 + a2;
233 } else if (mode == 3) {
234 unsigned a1 = coeffs[1];
235 unsigned a2 = a1 + *coeffs;
238 unsigned a3 = coeffs[2];
239 unsigned a4 = a3 + a1;
240 unsigned a5 = a4 + a2;
243 for (i = 0; i < length - 3; i++) {
254 static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len)
257 GetBitContext *gb = &s->gb;
261 memset(decoded, 0, len * sizeof(*decoded));
265 if (mode > FF_ARRAY_ELEMS(xcodes))
266 return AVERROR_INVALIDDATA;
267 code = xcodes[mode - 1];
269 for (i = 0; i < len; i++) {
270 unsigned x = get_bits_long(gb, code.init);
271 if (x >= code.escape && get_bits1(gb)) {
273 if (x >= code.aescape) {
274 unsigned scale = get_unary(gb, 1, 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);
281 return AVERROR_INVALIDDATA;
283 scale = get_bits_long(gb, scale_bits) + 1;
284 x += code.scale * scale;
288 x += code.scale * scale - code.escape;
292 decoded[i] = (x >> 1) ^ -(x & 1);
298 static int decode_residues(TAKDecContext *s, int32_t *decoded, int length)
300 GetBitContext *gb = &s->gb;
303 if (length > s->nb_samples)
304 return AVERROR_INVALIDDATA;
309 wlength = length / s->uval;
311 rval = length - (wlength * s->uval);
313 if (rval < s->uval / 2)
318 if (wlength <= 1 || wlength > 128)
319 return AVERROR_INVALIDDATA;
321 s->coding_mode[0] = mode = get_bits(gb, 6);
323 for (i = 1; i < wlength; i++) {
324 int c = get_unary(gb, 1, 6);
328 mode = get_bits(gb, 6);
333 /* mode += sign ? (1 - c) : (c - 1) */
334 int sign = get_bits1(gb);
335 mode += (-sign ^ (c - 1)) + sign;
345 s->coding_mode[i] = mode;
349 while (i < wlength) {
352 mode = s->coding_mode[i];
354 if (i >= wlength - 1)
362 } while (s->coding_mode[i] == mode);
364 if ((ret = decode_segment(s, mode, decoded, len)) < 0)
369 mode = get_bits(gb, 6);
370 if ((ret = decode_segment(s, mode, decoded, length)) < 0)
377 static int get_bits_esc4(GetBitContext *gb)
380 return get_bits(gb, 4) + 1;
385 static int decode_subframe(TAKDecContext *s, int32_t *decoded,
386 int subframe_size, int prev_subframe_size)
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];
394 return decode_residues(s, decoded, subframe_size);
396 filter_order = predictor_sizes[get_bits(gb, 4)];
398 if (prev_subframe_size > 0 && get_bits1(gb)) {
399 if (filter_order > prev_subframe_size)
400 return AVERROR_INVALIDDATA;
402 decoded -= filter_order;
403 subframe_size += filter_order;
405 if (filter_order > subframe_size)
406 return AVERROR_INVALIDDATA;
410 if (filter_order > subframe_size)
411 return AVERROR_INVALIDDATA;
413 lpc_mode = get_bits(gb, 2);
415 return AVERROR_INVALIDDATA;
417 if ((ret = decode_residues(s, decoded, filter_order)) < 0)
421 decode_lpc(decoded, lpc_mode, filter_order);
424 dshift = get_bits_esc4(gb);
425 size = get_bits1(gb) + 6;
429 filter_quant -= get_bits(gb, 3) + 1;
430 if (filter_quant < 3)
431 return AVERROR_INVALIDDATA;
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);
441 for (i = 4; i < filter_order; i++) {
443 x = tmp - get_bits(gb, 2);
444 s->predictors[i] = get_sbits(gb, x) * (1 << (10 - size));
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];
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;
460 tfilter[i] = s->predictors[i] * 64;
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));
470 if ((ret = decode_residues(s, &decoded[filter_order],
471 subframe_size - filter_order)) < 0)
474 for (i = 0; i < filter_order; i++)
475 s->residues[i] = *decoded++ >> dshift;
477 y = FF_ARRAY_ELEMS(s->residues) - filter_order;
478 x = subframe_size - filter_order;
480 int tmp = FFMIN(y, x);
482 for (i = 0; i < tmp; i++) {
483 int v = 1 << (filter_quant - 1);
485 if (filter_order & -16)
486 v += (unsigned)s->adsp.scalarproduct_int16(&s->residues[i], s->filter,
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 ];
494 v = (av_clip_intp2(v >> filter_quant, 13) * (1 << dshift)) - (unsigned)*decoded;
496 s->residues[filter_order + i] = v >> dshift;
501 memcpy(s->residues, &s->residues[y], 2 * filter_order);
509 static int decode_channel(TAKDecContext *s, int chan)
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;
517 s->sample_shift[chan] = get_bits_esc4(gb);
518 if (s->sample_shift[chan] >= avctx->bits_per_raw_sample)
519 return AVERROR_INVALIDDATA;
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;
525 if (s->nb_subframes > 1) {
526 if (get_bits_left(gb) < (s->nb_subframes - 1) * 6)
527 return AVERROR_INVALIDDATA;
529 for (; i < s->nb_subframes - 1; i++) {
530 int v = get_bits(gb, 6);
532 s->subframe_len[i] = (v - prev) * s->subframe_scale;
533 if (s->subframe_len[i] <= 0)
534 return AVERROR_INVALIDDATA;
536 left -= s->subframe_len[i];
541 return AVERROR_INVALIDDATA;
543 s->subframe_len[i] = left;
546 for (i = 0; i < s->nb_subframes; i++) {
547 if ((ret = decode_subframe(s, decoded, s->subframe_len[i], prev)) < 0)
549 decoded += s->subframe_len[i];
550 prev = s->subframe_len[i];
556 static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
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);
566 length += s->dmode < 6;
569 case 1: /* left/side */
570 s->tdsp.decorrelate_ls(p1, p2, length);
572 case 2: /* side/right */
573 s->tdsp.decorrelate_sr(p1, p2, length);
575 case 3: /* side/mid */
576 s->tdsp.decorrelate_sm(p1, p2, length);
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);
587 FFSWAP(int32_t*, p1, p2);
589 int length2, order_half, filter_order, dval1, dval2;
590 int tmp, x, code_size;
593 return AVERROR_INVALIDDATA;
595 dshift = get_bits_esc4(gb);
596 filter_order = 8 << get_bits1(gb);
597 dval1 = get_bits1(gb);
598 dval2 = get_bits1(gb);
600 for (i = 0; i < filter_order; i++) {
602 code_size = 14 - get_bits(gb, 3);
603 s->filter[i] = get_sbits(gb, code_size);
606 order_half = filter_order / 2;
607 length2 = length - (filter_order - 1);
609 /* decorrelate beginning samples */
611 for (i = 0; i < order_half; i++) {
618 /* decorrelate ending samples */
620 for (i = length2 + order_half; i < length; i++) {
628 for (i = 0; i < filter_order; i++)
629 s->residues[i] = *p2++ >> dshift;
632 x = FF_ARRAY_ELEMS(s->residues) - filter_order;
633 for (; length2 > 0; length2 -= tmp) {
634 tmp = FFMIN(length2, x);
636 for (i = 0; i < tmp - (tmp == length2); i++)
637 s->residues[filter_order + i] = *p2++ >> dshift;
639 for (i = 0; i < tmp; i++) {
642 if (filter_order == 16) {
643 v += s->adsp.scalarproduct_int16(&s->residues[i], s->filter,
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];
656 v = av_clip_intp2(v >> 10, 13) * (1 << dshift) - *p1;
660 memmove(s->residues, &s->residues[tmp], 2 * filter_order);
668 if (s->dmode > 0 && s->dmode < 6) {
676 static int tak_decode_frame(AVCodecContext *avctx, void *data,
677 int *got_frame_ptr, AVPacket *pkt)
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;
685 if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
686 return AVERROR_INVALIDDATA;
688 if ((ret = init_get_bits8(gb, pkt->data, pkt->size)) < 0)
691 if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
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;
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;
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;
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;
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;
724 if (s->ti.frame_samples <= 0) {
725 av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\n");
726 return AVERROR_INVALIDDATA;
729 avctx->bits_per_raw_sample = s->ti.bps;
730 if ((ret = set_bps_params(avctx)) < 0)
732 if (s->ti.sample_rate != avctx->sample_rate) {
733 avctx->sample_rate = s->ti.sample_rate;
734 set_sample_rate_params(avctx);
737 avctx->channel_layout = s->ti.ch_layout;
738 avctx->channels = s->ti.channels;
740 s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples
741 : s->ti.frame_samples;
743 frame->nb_samples = s->nb_samples;
744 if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
746 ff_thread_finish_setup(avctx);
748 if (avctx->bits_per_raw_sample <= 16) {
749 int buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
751 AV_SAMPLE_FMT_S32P, 0);
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);
763 for (chan = 0; chan < avctx->channels; chan++)
764 s->decoded[chan] = (int32_t *)frame->extended_data[chan];
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);
774 if (s->ti.codec == TAK_CODEC_MONO_STEREO) {
775 for (chan = 0; chan < avctx->channels; chan++)
776 if (ret = decode_channel(s, chan))
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);
785 s->dmode = get_bits(gb, 3);
786 if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))
789 } else if (s->ti.codec == TAK_CODEC_MULTICHANNEL) {
793 chan = get_bits(gb, 4) + 1;
794 if (chan > avctx->channels)
795 return AVERROR_INVALIDDATA;
797 for (i = 0; i < chan; i++) {
798 int nbit = get_bits(gb, 4);
800 if (nbit >= avctx->channels)
801 return AVERROR_INVALIDDATA;
803 if (ch_mask & 1 << nbit)
804 return AVERROR_INVALIDDATA;
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;
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;
821 ch_mask |= 1 << s->mcdparams[i].chan2;
822 } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {
823 return AVERROR_INVALIDDATA;
826 s->mcdparams[i].chan1 = nbit;
828 ch_mask |= 1 << nbit;
831 chan = avctx->channels;
832 for (i = 0; i < chan; i++) {
833 s->mcdparams[i].present = 0;
834 s->mcdparams[i].chan1 = i;
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))
843 if (ret = decode_channel(s, s->mcdparams[i].chan1))
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,
857 for (chan = 0; chan < avctx->channels; chan++) {
858 int32_t *decoded = s->decoded[chan];
860 if (s->lpc_mode[chan])
861 decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples);
863 if (s->sample_shift[chan] > 0)
864 for (i = 0; i < s->nb_samples; i++)
865 decoded[i] *= 1U << s->sample_shift[chan];
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");
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;
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;
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];
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;
918 static int init_thread_copy(AVCodecContext *avctx)
920 TAKDecContext *s = avctx->priv_data;
925 static int update_thread_context(AVCodecContext *dst,
926 const AVCodecContext *src)
928 TAKDecContext *tsrc = src->priv_data;
929 TAKDecContext *tdst = dst->priv_data;
933 memcpy(&tdst->ti, &tsrc->ti, sizeof(TAKStreamInfo));
938 static av_cold int tak_decode_close(AVCodecContext *avctx)
940 TAKDecContext *s = avctx->priv_data;
942 av_freep(&s->decode_buffer);
947 AVCodec ff_tak_decoder = {
949 .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
950 .type = AVMEDIA_TYPE_AUDIO,
951 .id = AV_CODEC_ID_TAK,
952 .priv_data_size = sizeof(TAKDecContext),
953 .init = tak_decode_init,
954 .close = tak_decode_close,
955 .decode = tak_decode_frame,
956 .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
957 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
958 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
959 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
962 AV_SAMPLE_FMT_NONE },