GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/takdec.c Lines: 353 525 67.2 %
Date: 2020-09-25 23:16:12 Branches: 180 327 55.0 %

Line Branch Exec Source
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
40
static int set_bps_params(AVCodecContext *avctx)
145
{
146

40
    switch (avctx->bits_per_raw_sample) {
147
    case 8:
148
        avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
149
        break;
150
40
    case 16:
151
40
        avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
152
40
        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
40
    return 0;
163
}
164
165
2
static void set_sample_rate_params(AVCodecContext *avctx)
166
{
167
2
    TAKDecContext *s  = avctx->priv_data;
168
    int shift;
169
170
2
    if (avctx->sample_rate < 11025) {
171
        shift = 3;
172
2
    } else if (avctx->sample_rate < 22050) {
173
        shift = 2;
174
2
    } else if (avctx->sample_rate < 44100) {
175
        shift = 1;
176
    } else {
177
2
        shift = 0;
178
    }
179
2
    s->uval           = FFALIGN(avctx->sample_rate + 511LL >> 9, 4) << shift;
180
2
    s->subframe_scale = FFALIGN(avctx->sample_rate + 511LL >> 9, 4) << 1;
181
2
}
182
183
2
static av_cold int tak_decode_init(AVCodecContext *avctx)
184
{
185
2
    TAKDecContext *s = avctx->priv_data;
186
187
2
    ff_audiodsp_init(&s->adsp);
188
2
    ff_takdsp_init(&s->tdsp);
189
190
2
    s->avctx = avctx;
191
2
    avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
192
193
2
    set_sample_rate_params(avctx);
194
195
2
    return set_bps_params(avctx);
196
}
197
198
122
static void decode_lpc(int32_t *coeffs, int mode, int length)
199
{
200
    int i;
201
202
122
    if (length < 2)
203
        return;
204
205
122
    if (mode == 1) {
206
115
        unsigned a1 = *coeffs++;
207
420150
        for (i = 0; i < length - 1 >> 1; i++) {
208
420035
            *coeffs   += a1;
209
420035
            coeffs[1] += (unsigned)*coeffs;
210
420035
            a1         = coeffs[1];
211
420035
            coeffs    += 2;
212
        }
213
115
        if (length - 1 & 1)
214
39
            *coeffs += a1;
215
7
    } else if (mode == 2) {
216
7
        unsigned a1    = coeffs[1];
217
7
        unsigned a2    = a1 + *coeffs;
218
7
        coeffs[1] = a2;
219
7
        if (length > 2) {
220
7
            coeffs += 2;
221
258
            for (i = 0; i < length - 2 >> 1; i++) {
222
251
                unsigned a3    = *coeffs + a1;
223
251
                unsigned a4    = a3 + a2;
224
251
                *coeffs   = a4;
225
251
                a1        = coeffs[1] + a3;
226
251
                a2        = a1 + a4;
227
251
                coeffs[1] = a2;
228
251
                coeffs   += 2;
229
            }
230
7
            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
3930
static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len)
255
{
256
    struct CParam code;
257
3930
    GetBitContext *gb = &s->gb;
258
    int i;
259
260
3930
    if (!mode) {
261
        memset(decoded, 0, len * sizeof(*decoded));
262
        return 0;
263
    }
264
265
3930
    if (mode > FF_ARRAY_ELEMS(xcodes))
266
        return AVERROR_INVALIDDATA;
267
3930
    code = xcodes[mode - 1];
268
269
841754
    for (i = 0; i < len; i++) {
270
837824
        unsigned x = get_bits_long(gb, code.init);
271

837824
        if (x >= code.escape && get_bits1(gb)) {
272
182416
            x |= 1 << code.init;
273
182416
            if (x >= code.aescape) {
274
135064
                unsigned scale = get_unary(gb, 1, 9);
275
135064
                if (scale == 9) {
276
164
                    int scale_bits = get_bits(gb, 3);
277
164
                    if (scale_bits > 0) {
278
109
                        if (scale_bits == 7) {
279
                            scale_bits += get_bits(gb, 5);
280
                            if (scale_bits > 29)
281
                                return AVERROR_INVALIDDATA;
282
                        }
283
109
                        scale = get_bits_long(gb, scale_bits) + 1;
284
109
                        x    += code.scale * scale;
285
                    }
286
164
                    x += code.bias;
287
                } else
288
134900
                    x += code.scale * scale - code.escape;
289
            } else
290
47352
                x -= code.escape;
291
        }
292
837824
        decoded[i] = (x >> 1) ^ -(x & 1);
293
    }
294
295
3930
    return 0;
296
}
297
298
341
static int decode_residues(TAKDecContext *s, int32_t *decoded, int length)
299
{
300
341
    GetBitContext *gb = &s->gb;
301
    int i, mode, ret;
302
303
341
    if (length > s->nb_samples)
304
        return AVERROR_INVALIDDATA;
305
306
341
    if (get_bits1(gb)) {
307
        int wlength, rval;
308
309
250
        wlength = length / s->uval;
310
311
250
        rval = length - (wlength * s->uval);
312
313
250
        if (rval < s->uval / 2)
314
209
            rval += s->uval;
315
        else
316
41
            wlength++;
317
318

250
        if (wlength <= 1 || wlength > 128)
319
            return AVERROR_INVALIDDATA;
320
321
250
        s->coding_mode[0] = mode = get_bits(gb, 6);
322
323
8926
        for (i = 1; i < wlength; i++) {
324
8676
            int c = get_unary(gb, 1, 6);
325
326

8676
            switch (c) {
327
            case 6:
328
                mode = get_bits(gb, 6);
329
                break;
330
238
            case 5:
331
            case 4:
332
            case 3: {
333
                /* mode += sign ? (1 - c) : (c - 1) */
334
238
                int sign = get_bits1(gb);
335
238
                mode    += (-sign ^ (c - 1)) + sign;
336
238
                break;
337
            }
338
1607
            case 2:
339
1607
                mode++;
340
1607
                break;
341
1744
            case 1:
342
1744
                mode--;
343
1744
                break;
344
            }
345
8676
            s->coding_mode[i] = mode;
346
        }
347
348
250
        i = 0;
349
4089
        while (i < wlength) {
350
3839
            int len = 0;
351
352
3839
            mode = s->coding_mode[i];
353
            do {
354
8926
                if (i >= wlength - 1)
355
250
                    len += rval;
356
                else
357
8676
                    len += s->uval;
358
8926
                i++;
359
360
8926
                if (i == wlength)
361
250
                    break;
362
8676
            } while (s->coding_mode[i] == mode);
363
364
3839
            if ((ret = decode_segment(s, mode, decoded, len)) < 0)
365
                return ret;
366
3839
            decoded += len;
367
        }
368
    } else {
369
91
        mode = get_bits(gb, 6);
370
91
        if ((ret = decode_segment(s, mode, decoded, length)) < 0)
371
            return ret;
372
    }
373
374
341
    return 0;
375
}
376
377
376
static int get_bits_esc4(GetBitContext *gb)
378
{
379
376
    if (get_bits1(gb))
380
25
        return get_bits(gb, 4) + 1;
381
    else
382
351
        return 0;
383
}
384
385
265
static int decode_subframe(TAKDecContext *s, int32_t *decoded,
386
                           int subframe_size, int prev_subframe_size)
387
{
388
265
    GetBitContext *gb = &s->gb;
389
265
    int x, y, i, j, ret = 0;
390
    int dshift, size, filter_quant, filter_order;
391
    int tfilter[MAX_PREDICTORS];
392
393
265
    if (!get_bits1(gb))
394
        return decode_residues(s, decoded, subframe_size);
395
396
265
    filter_order = predictor_sizes[get_bits(gb, 4)];
397
398

265
    if (prev_subframe_size > 0 && get_bits1(gb)) {
399
189
        if (filter_order > prev_subframe_size)
400
            return AVERROR_INVALIDDATA;
401
402
189
        decoded       -= filter_order;
403
189
        subframe_size += filter_order;
404
405
189
        if (filter_order > subframe_size)
406
            return AVERROR_INVALIDDATA;
407
    } else {
408
        int lpc_mode;
409
410
76
        if (filter_order > subframe_size)
411
            return AVERROR_INVALIDDATA;
412
413
76
        lpc_mode = get_bits(gb, 2);
414
76
        if (lpc_mode > 2)
415
            return AVERROR_INVALIDDATA;
416
417
76
        if ((ret = decode_residues(s, decoded, filter_order)) < 0)
418
            return ret;
419
420
76
        if (lpc_mode)
421
46
            decode_lpc(decoded, lpc_mode, filter_order);
422
    }
423
424
265
    dshift = get_bits_esc4(gb);
425
265
    size   = get_bits1(gb) + 6;
426
427
265
    filter_quant = 10;
428
265
    if (get_bits1(gb)) {
429
        filter_quant -= get_bits(gb, 3) + 1;
430
        if (filter_quant < 3)
431
            return AVERROR_INVALIDDATA;
432
    }
433
434
265
    s->predictors[0] = get_sbits(gb, 10);
435
265
    s->predictors[1] = get_sbits(gb, 10);
436
265
    s->predictors[2] = get_sbits(gb, size) * (1 << (10 - size));
437
265
    s->predictors[3] = get_sbits(gb, size) * (1 << (10 - size));
438
265
    if (filter_order > 4) {
439
242
        int tmp = size - get_bits1(gb);
440
441
9054
        for (i = 4; i < filter_order; i++) {
442
8812
            if (!(i & 3))
443
2203
                x = tmp - get_bits(gb, 2);
444
8812
            s->predictors[i] = get_sbits(gb, x) * (1 << (10 - size));
445
        }
446
    }
447
448
265
    tfilter[0] = s->predictors[0] * 64;
449
9872
    for (i = 1; i < filter_order; i++) {
450
9607
        uint32_t *p1 = &tfilter[0];
451
9607
        uint32_t *p2 = &tfilter[i - 1];
452
453
215679
        for (j = 0; j < (i + 1) / 2; j++) {
454
206072
            x     = *p1 + ((int32_t)(s->predictors[i] * *p2 + 256) >> 9);
455
206072
            *p2  += (int32_t)(s->predictors[i] * *p1 + 256) >> 9;
456
206072
            *p1++ = x;
457
206072
            p2--;
458
        }
459
460
9607
        tfilter[i] = s->predictors[i] * 64;
461
    }
462
463
265
    x = 1 << (32 - (15 - filter_quant));
464
265
    y = 1 << ((15 - filter_quant) - 1);
465
5201
    for (i = 0, j = filter_order - 1; i < filter_order / 2; i++, j--) {
466
4936
        s->filter[j] = x - ((tfilter[i] + y) >> (15 - filter_quant));
467
4936
        s->filter[i] = x - ((tfilter[j] + y) >> (15 - filter_quant));
468
    }
469
470
265
    if ((ret = decode_residues(s, &decoded[filter_order],
471
                               subframe_size - filter_order)) < 0)
472
        return ret;
473
474
10137
    for (i = 0; i < filter_order; i++)
475
9872
        s->residues[i] = *decoded++ >> dshift;
476
477
265
    y    = FF_ARRAY_ELEMS(s->residues) - filter_order;
478
265
    x    = subframe_size - filter_order;
479
2113
    while (x > 0) {
480
1848
        int tmp = FFMIN(y, x);
481
482
835784
        for (i = 0; i < tmp; i++) {
483
833936
            int v = 1 << (filter_quant - 1);
484
485
833936
            if (filter_order & -16)
486
685736
                v += (unsigned)s->adsp.scalarproduct_int16(&s->residues[i], s->filter,
487
                                                 filter_order & -16);
488
1303584
            for (j = filter_order & -16; j < filter_order; j += 4) {
489
469648
                v += s->residues[i + j + 3] * (unsigned)s->filter[j + 3] +
490
469648
                     s->residues[i + j + 2] * (unsigned)s->filter[j + 2] +
491
469648
                     s->residues[i + j + 1] * (unsigned)s->filter[j + 1] +
492
469648
                     s->residues[i + j    ] * (unsigned)s->filter[j    ];
493
            }
494
833936
            v = (av_clip_intp2(v >> filter_quant, 13) * (1 << dshift)) - (unsigned)*decoded;
495
833936
            *decoded++ = v;
496
833936
            s->residues[filter_order + i] = v >> dshift;
497
        }
498
499
1848
        x -= tmp;
500
1848
        if (x > 0)
501
1583
            memcpy(s->residues, &s->residues[y], 2 * filter_order);
502
    }
503
504
265
    emms_c();
505
506
265
    return 0;
507
}
508
509
76
static int decode_channel(TAKDecContext *s, int chan)
510
{
511
76
    AVCodecContext *avctx = s->avctx;
512
76
    GetBitContext *gb     = &s->gb;
513
76
    int32_t *decoded      = s->decoded[chan];
514
76
    int left              = s->nb_samples - 1;
515
76
    int i = 0, ret, prev = 0;
516
517
76
    s->sample_shift[chan] = get_bits_esc4(gb);
518
76
    if (s->sample_shift[chan] >= avctx->bits_per_raw_sample)
519
        return AVERROR_INVALIDDATA;
520
521
76
    *decoded++ = get_sbits(gb, avctx->bits_per_raw_sample - s->sample_shift[chan]);
522
76
    s->lpc_mode[chan] = get_bits(gb, 2);
523
76
    s->nb_subframes   = get_bits(gb, 3) + 1;
524
525
76
    if (s->nb_subframes > 1) {
526
68
        if (get_bits_left(gb) < (s->nb_subframes - 1) * 6)
527
            return AVERROR_INVALIDDATA;
528
529
257
        for (; i < s->nb_subframes - 1; i++) {
530
189
            int v = get_bits(gb, 6);
531
532
189
            s->subframe_len[i] = (v - prev) * s->subframe_scale;
533
189
            if (s->subframe_len[i] <= 0)
534
                return AVERROR_INVALIDDATA;
535
536
189
            left -= s->subframe_len[i];
537
189
            prev  = v;
538
        }
539
540
68
        if (left <= 0)
541
            return AVERROR_INVALIDDATA;
542
    }
543
76
    s->subframe_len[i] = left;
544
545
76
    prev = 0;
546
341
    for (i = 0; i < s->nb_subframes; i++) {
547
265
        if ((ret = decode_subframe(s, decoded, s->subframe_len[i], prev)) < 0)
548
            return ret;
549
265
        decoded += s->subframe_len[i];
550
265
        prev     = s->subframe_len[i];
551
    }
552
553
76
    return 0;
554
}
555
556
38
static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
557
{
558
38
    GetBitContext *gb = &s->gb;
559
38
    int32_t *p1       = s->decoded[c1] + (s->dmode > 5);
560
38
    int32_t *p2       = s->decoded[c2] + (s->dmode > 5);
561
38
    int32_t bp1       = p1[0];
562
38
    int32_t bp2       = p2[0];
563
    int i;
564
    int dshift, dfactor;
565
566
38
    length += s->dmode < 6;
567
568


38
    switch (s->dmode) {
569
1
    case 1: /* left/side */
570
1
        s->tdsp.decorrelate_ls(p1, p2, length);
571
1
        break;
572
    case 2: /* side/right */
573
        s->tdsp.decorrelate_sr(p1, p2, length);
574
        break;
575
2
    case 3: /* side/mid */
576
2
        s->tdsp.decorrelate_sm(p1, p2, length);
577
2
        break;
578
3
    case 4: /* side/left with scale factor */
579
3
        FFSWAP(int32_t*, p1, p2);
580
3
        FFSWAP(int32_t, bp1, bp2);
581
5
    case 5: /* side/right with scale factor */
582
5
        dshift  = get_bits_esc4(gb);
583
5
        dfactor = get_sbits(gb, 10);
584
5
        s->tdsp.decorrelate_sf(p1, p2, length, dshift, dfactor);
585
5
        break;
586
13
    case 6:
587
13
        FFSWAP(int32_t*, p1, p2);
588
30
    case 7: {
589
        int length2, order_half, filter_order, dval1, dval2;
590
        int tmp, x, code_size;
591
592
30
        if (length < 256)
593
            return AVERROR_INVALIDDATA;
594
595
30
        dshift       = get_bits_esc4(gb);
596
30
        filter_order = 8 << get_bits1(gb);
597
30
        dval1        = get_bits1(gb);
598
30
        dval2        = get_bits1(gb);
599
600
366
        for (i = 0; i < filter_order; i++) {
601
336
            if (!(i & 3))
602
84
                code_size = 14 - get_bits(gb, 3);
603
336
            s->filter[i] = get_sbits(gb, code_size);
604
        }
605
606
30
        order_half = filter_order / 2;
607
30
        length2    = length - (filter_order - 1);
608
609
        /* decorrelate beginning samples */
610
30
        if (dval1) {
611
141
            for (i = 0; i < order_half; i++) {
612
120
                int32_t a = p1[i];
613
120
                int32_t b = p2[i];
614
120
                p1[i]     = a + b;
615
            }
616
        }
617
618
        /* decorrelate ending samples */
619
30
        if (dval2) {
620
96
            for (i = length2 + order_half; i < length; i++) {
621
78
                int32_t a = p1[i];
622
78
                int32_t b = p2[i];
623
78
                p1[i]     = a + b;
624
            }
625
        }
626
627
628
366
        for (i = 0; i < filter_order; i++)
629
336
            s->residues[i] = *p2++ >> dshift;
630
631
30
        p1 += order_half;
632
30
        x = FF_ARRAY_ELEMS(s->residues) - filter_order;
633
660
        for (; length2 > 0; length2 -= tmp) {
634
630
            tmp = FFMIN(length2, x);
635
636
331014
            for (i = 0; i < tmp - (tmp == length2); i++)
637
330384
                s->residues[filter_order + i] = *p2++ >> dshift;
638
639
331044
            for (i = 0; i < tmp; i++) {
640
330414
                int v = 1 << 9;
641
642
330414
                if (filter_order == 16) {
643
132108
                    v += s->adsp.scalarproduct_int16(&s->residues[i], s->filter,
644
                                                     filter_order);
645
                } else {
646
198306
                    v += s->residues[i + 7] * s->filter[7] +
647
198306
                         s->residues[i + 6] * s->filter[6] +
648
198306
                         s->residues[i + 5] * s->filter[5] +
649
198306
                         s->residues[i + 4] * s->filter[4] +
650
198306
                         s->residues[i + 3] * s->filter[3] +
651
198306
                         s->residues[i + 2] * s->filter[2] +
652
198306
                         s->residues[i + 1] * s->filter[1] +
653
198306
                         s->residues[i    ] * s->filter[0];
654
                }
655
656
330414
                v = av_clip_intp2(v >> 10, 13) * (1U << dshift) - *p1;
657
330414
                *p1++ = v;
658
            }
659
660
630
            memmove(s->residues, &s->residues[tmp], 2 * filter_order);
661
        }
662
663
30
        emms_c();
664
30
        break;
665
    }
666
    }
667
668

38
    if (s->dmode > 0 && s->dmode < 6) {
669
8
        p1[0] = bp1;
670
8
        p2[0] = bp2;
671
    }
672
673
38
    return 0;
674
}
675
676
38
static int tak_decode_frame(AVCodecContext *avctx, void *data,
677
                            int *got_frame_ptr, AVPacket *pkt)
678
{
679
38
    TAKDecContext *s  = avctx->priv_data;
680
38
    AVFrame *frame    = data;
681
38
    ThreadFrame tframe = { .f = data };
682
38
    GetBitContext *gb = &s->gb;
683
    int chan, i, ret, hsize;
684
685
38
    if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
686
        return AVERROR_INVALIDDATA;
687
688
38
    if ((ret = init_get_bits8(gb, pkt->data, pkt->size)) < 0)
689
        return ret;
690
691
38
    if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
692
        return ret;
693
694
38
    hsize = get_bits_count(gb) / 8;
695
38
    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
38
    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
38
    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

38
    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
38
    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
38
    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
38
    avctx->bits_per_raw_sample = s->ti.bps;
730
38
    if ((ret = set_bps_params(avctx)) < 0)
731
        return ret;
732
38
    if (s->ti.sample_rate != avctx->sample_rate) {
733
        avctx->sample_rate = s->ti.sample_rate;
734
        set_sample_rate_params(avctx);
735
    }
736
38
    if (s->ti.ch_layout)
737
        avctx->channel_layout = s->ti.ch_layout;
738
38
    avctx->channels = s->ti.channels;
739
740
76
    s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples
741
38
                                             : s->ti.frame_samples;
742
743
38
    frame->nb_samples = s->nb_samples;
744
38
    if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
745
        return ret;
746
38
    ff_thread_finish_setup(avctx);
747
748
38
    if (avctx->bits_per_raw_sample <= 16) {
749
38
        int buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
750
                                                  s->nb_samples,
751
                                                  AV_SAMPLE_FMT_S32P, 0);
752
38
        if (buf_size < 0)
753
            return buf_size;
754
38
        av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size, buf_size);
755
38
        if (!s->decode_buffer)
756
            return AVERROR(ENOMEM);
757
38
        ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
758
38
                                     s->decode_buffer, avctx->channels,
759
                                     s->nb_samples, AV_SAMPLE_FMT_S32P, 0);
760
38
        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
38
    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
38
        if (s->ti.codec == TAK_CODEC_MONO_STEREO) {
775
114
            for (chan = 0; chan < avctx->channels; chan++)
776
76
                if (ret = decode_channel(s, chan))
777
                    return ret;
778
779
38
            if (avctx->channels == 2) {
780
38
                s->nb_subframes = get_bits(gb, 1) + 1;
781
38
                if (s->nb_subframes > 1) {
782
                    s->subframe_len[1] = get_bits(gb, 6);
783
                }
784
785
38
                s->dmode = get_bits(gb, 3);
786
38
                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
114
        for (chan = 0; chan < avctx->channels; chan++) {
858
76
            int32_t *decoded = s->decoded[chan];
859
860
76
            if (s->lpc_mode[chan])
861
76
                decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples);
862
863
76
            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
38
    align_get_bits(gb);
870
38
    skip_bits(gb, 24);
871
38
    if (get_bits_left(gb) < 0)
872
        av_log(avctx, AV_LOG_DEBUG, "overread\n");
873
38
    else if (get_bits_left(gb) > 0)
874
        av_log(avctx, AV_LOG_DEBUG, "underread\n");
875
876
38
    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

38
    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
38
    case AV_SAMPLE_FMT_S16P:
896
114
        for (chan = 0; chan < avctx->channels; chan++) {
897
76
            int16_t *samples = (int16_t *)frame->extended_data[chan];
898
76
            int32_t *decoded = s->decoded[chan];
899
837976
            for (i = 0; i < s->nb_samples; i++)
900
837900
                samples[i] = decoded[i];
901
        }
902
38
        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
38
    *got_frame_ptr = 1;
913
914
38
    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
2
static av_cold int tak_decode_close(AVCodecContext *avctx)
932
{
933
2
    TAKDecContext *s = avctx->priv_data;
934
935
2
    av_freep(&s->decode_buffer);
936
937
2
    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
};