2012-10-02 15:43:19 +02:00
|
|
|
/*
|
|
|
|
* TAK decoder
|
|
|
|
* Copyright (c) 2012 Paul B Mahol
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* TAK (Tom's lossless Audio Kompressor) decoder
|
|
|
|
* @author Paul B Mahol
|
|
|
|
*/
|
|
|
|
|
2013-02-08 00:31:13 +01:00
|
|
|
#include "libavutil/internal.h"
|
2012-12-07 02:11:55 +01:00
|
|
|
#include "libavutil/samplefmt.h"
|
2012-10-02 15:43:19 +02:00
|
|
|
#include "tak.h"
|
2013-06-03 17:42:39 +02:00
|
|
|
#include "thread.h"
|
2012-10-02 15:43:19 +02:00
|
|
|
#include "avcodec.h"
|
2012-12-08 13:18:23 +01:00
|
|
|
#include "dsputil.h"
|
2012-12-05 15:18:12 +01:00
|
|
|
#include "internal.h"
|
2012-10-02 15:43:19 +02:00
|
|
|
#include "unary.h"
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
#define MAX_SUBFRAMES 8 ///< max number of subframes per channel
|
2012-10-02 15:43:19 +02:00
|
|
|
#define MAX_PREDICTORS 256
|
|
|
|
|
|
|
|
typedef struct MCDParam {
|
2012-12-08 13:18:23 +01:00
|
|
|
int8_t present; ///< decorrelation parameter availability for this channel
|
|
|
|
int8_t index; ///< index into array of decorrelation types
|
|
|
|
int8_t chan1;
|
|
|
|
int8_t chan2;
|
2012-10-02 15:43:19 +02:00
|
|
|
} MCDParam;
|
|
|
|
|
|
|
|
typedef struct TAKDecContext {
|
2012-12-08 13:18:23 +01:00
|
|
|
AVCodecContext *avctx; ///< parent AVCodecContext
|
|
|
|
DSPContext dsp;
|
|
|
|
TAKStreamInfo ti;
|
|
|
|
GetBitContext gb; ///< bitstream reader initialized to start at the current frame
|
|
|
|
|
|
|
|
int uval;
|
|
|
|
int nb_samples; ///< number of samples in the current frame
|
2012-12-07 02:11:55 +01:00
|
|
|
uint8_t *decode_buffer;
|
2012-12-08 13:18:23 +01:00
|
|
|
unsigned int decode_buffer_size;
|
|
|
|
int32_t *decoded[TAK_MAX_CHANNELS]; ///< decoded samples for each channel
|
|
|
|
|
|
|
|
int8_t lpc_mode[TAK_MAX_CHANNELS];
|
|
|
|
int8_t sample_shift[TAK_MAX_CHANNELS]; ///< shift applied to every sample in the channel
|
|
|
|
int16_t predictors[MAX_PREDICTORS];
|
|
|
|
int nb_subframes; ///< number of subframes in the current frame
|
|
|
|
int16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
|
|
|
|
int subframe_scale;
|
|
|
|
|
|
|
|
int8_t dmode; ///< channel decorrelation type in the current frame
|
|
|
|
|
|
|
|
MCDParam mcdparams[TAK_MAX_CHANNELS]; ///< multichannel decorrelation parameters
|
|
|
|
|
|
|
|
int8_t coding_mode[128];
|
2012-10-02 15:43:19 +02:00
|
|
|
DECLARE_ALIGNED(16, int16_t, filter)[MAX_PREDICTORS];
|
|
|
|
DECLARE_ALIGNED(16, int16_t, residues)[544];
|
|
|
|
} TAKDecContext;
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
static const int8_t mc_dmodes[] = { 1, 3, 4, 6, };
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
static const uint16_t predictor_sizes[] = {
|
|
|
|
4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct CParam {
|
2012-12-08 13:18:23 +01:00
|
|
|
int init;
|
|
|
|
int escape;
|
|
|
|
int scale;
|
|
|
|
int aescape;
|
|
|
|
int bias;
|
2012-10-02 15:43:19 +02:00
|
|
|
} xcodes[50] = {
|
|
|
|
{ 0x01, 0x0000001, 0x0000001, 0x0000003, 0x0000008 },
|
|
|
|
{ 0x02, 0x0000003, 0x0000001, 0x0000007, 0x0000006 },
|
|
|
|
{ 0x03, 0x0000005, 0x0000002, 0x000000E, 0x000000D },
|
|
|
|
{ 0x03, 0x0000003, 0x0000003, 0x000000D, 0x0000018 },
|
|
|
|
{ 0x04, 0x000000B, 0x0000004, 0x000001C, 0x0000019 },
|
|
|
|
{ 0x04, 0x0000006, 0x0000006, 0x000001A, 0x0000030 },
|
|
|
|
{ 0x05, 0x0000016, 0x0000008, 0x0000038, 0x0000032 },
|
|
|
|
{ 0x05, 0x000000C, 0x000000C, 0x0000034, 0x0000060 },
|
|
|
|
{ 0x06, 0x000002C, 0x0000010, 0x0000070, 0x0000064 },
|
|
|
|
{ 0x06, 0x0000018, 0x0000018, 0x0000068, 0x00000C0 },
|
|
|
|
{ 0x07, 0x0000058, 0x0000020, 0x00000E0, 0x00000C8 },
|
|
|
|
{ 0x07, 0x0000030, 0x0000030, 0x00000D0, 0x0000180 },
|
|
|
|
{ 0x08, 0x00000B0, 0x0000040, 0x00001C0, 0x0000190 },
|
|
|
|
{ 0x08, 0x0000060, 0x0000060, 0x00001A0, 0x0000300 },
|
|
|
|
{ 0x09, 0x0000160, 0x0000080, 0x0000380, 0x0000320 },
|
|
|
|
{ 0x09, 0x00000C0, 0x00000C0, 0x0000340, 0x0000600 },
|
|
|
|
{ 0x0A, 0x00002C0, 0x0000100, 0x0000700, 0x0000640 },
|
|
|
|
{ 0x0A, 0x0000180, 0x0000180, 0x0000680, 0x0000C00 },
|
|
|
|
{ 0x0B, 0x0000580, 0x0000200, 0x0000E00, 0x0000C80 },
|
|
|
|
{ 0x0B, 0x0000300, 0x0000300, 0x0000D00, 0x0001800 },
|
|
|
|
{ 0x0C, 0x0000B00, 0x0000400, 0x0001C00, 0x0001900 },
|
|
|
|
{ 0x0C, 0x0000600, 0x0000600, 0x0001A00, 0x0003000 },
|
|
|
|
{ 0x0D, 0x0001600, 0x0000800, 0x0003800, 0x0003200 },
|
|
|
|
{ 0x0D, 0x0000C00, 0x0000C00, 0x0003400, 0x0006000 },
|
|
|
|
{ 0x0E, 0x0002C00, 0x0001000, 0x0007000, 0x0006400 },
|
|
|
|
{ 0x0E, 0x0001800, 0x0001800, 0x0006800, 0x000C000 },
|
|
|
|
{ 0x0F, 0x0005800, 0x0002000, 0x000E000, 0x000C800 },
|
|
|
|
{ 0x0F, 0x0003000, 0x0003000, 0x000D000, 0x0018000 },
|
|
|
|
{ 0x10, 0x000B000, 0x0004000, 0x001C000, 0x0019000 },
|
|
|
|
{ 0x10, 0x0006000, 0x0006000, 0x001A000, 0x0030000 },
|
|
|
|
{ 0x11, 0x0016000, 0x0008000, 0x0038000, 0x0032000 },
|
|
|
|
{ 0x11, 0x000C000, 0x000C000, 0x0034000, 0x0060000 },
|
|
|
|
{ 0x12, 0x002C000, 0x0010000, 0x0070000, 0x0064000 },
|
|
|
|
{ 0x12, 0x0018000, 0x0018000, 0x0068000, 0x00C0000 },
|
|
|
|
{ 0x13, 0x0058000, 0x0020000, 0x00E0000, 0x00C8000 },
|
|
|
|
{ 0x13, 0x0030000, 0x0030000, 0x00D0000, 0x0180000 },
|
|
|
|
{ 0x14, 0x00B0000, 0x0040000, 0x01C0000, 0x0190000 },
|
|
|
|
{ 0x14, 0x0060000, 0x0060000, 0x01A0000, 0x0300000 },
|
|
|
|
{ 0x15, 0x0160000, 0x0080000, 0x0380000, 0x0320000 },
|
|
|
|
{ 0x15, 0x00C0000, 0x00C0000, 0x0340000, 0x0600000 },
|
|
|
|
{ 0x16, 0x02C0000, 0x0100000, 0x0700000, 0x0640000 },
|
|
|
|
{ 0x16, 0x0180000, 0x0180000, 0x0680000, 0x0C00000 },
|
|
|
|
{ 0x17, 0x0580000, 0x0200000, 0x0E00000, 0x0C80000 },
|
|
|
|
{ 0x17, 0x0300000, 0x0300000, 0x0D00000, 0x1800000 },
|
|
|
|
{ 0x18, 0x0B00000, 0x0400000, 0x1C00000, 0x1900000 },
|
|
|
|
{ 0x18, 0x0600000, 0x0600000, 0x1A00000, 0x3000000 },
|
|
|
|
{ 0x19, 0x1600000, 0x0800000, 0x3800000, 0x3200000 },
|
|
|
|
{ 0x19, 0x0C00000, 0x0C00000, 0x3400000, 0x6000000 },
|
|
|
|
{ 0x1A, 0x2C00000, 0x1000000, 0x7000000, 0x6400000 },
|
|
|
|
{ 0x1A, 0x1800000, 0x1800000, 0x6800000, 0xC000000 },
|
|
|
|
};
|
|
|
|
|
2012-12-07 15:56:22 +01:00
|
|
|
static int set_bps_params(AVCodecContext *avctx)
|
2012-10-02 15:43:19 +02:00
|
|
|
{
|
2012-12-07 15:56:22 +01:00
|
|
|
switch (avctx->bits_per_raw_sample) {
|
2012-10-02 15:43:19 +02:00
|
|
|
case 8:
|
|
|
|
avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
|
|
|
|
break;
|
2012-10-12 11:18:58 +02:00
|
|
|
default:
|
2012-12-08 13:18:23 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "invalid/unsupported bits per sample: %d\n",
|
|
|
|
avctx->bits_per_raw_sample);
|
2012-10-12 11:18:58 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
2012-10-12 11:18:58 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
2012-12-07 15:56:22 +01:00
|
|
|
static void set_sample_rate_params(AVCodecContext *avctx)
|
2012-10-02 15:43:19 +02:00
|
|
|
{
|
2012-12-07 15:56:22 +01:00
|
|
|
TAKDecContext *s = avctx->priv_data;
|
|
|
|
int shift = 3 - (avctx->sample_rate / 11025);
|
|
|
|
shift = FFMAX(0, shift);
|
|
|
|
s->uval = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << shift;
|
|
|
|
s->subframe_scale = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << 1;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int tak_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
TAKDecContext *s = avctx->priv_data;
|
|
|
|
|
|
|
|
ff_dsputil_init(&s->dsp, avctx);
|
|
|
|
|
|
|
|
s->avctx = avctx;
|
2012-12-07 15:56:22 +01:00
|
|
|
avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-07 15:56:22 +01:00
|
|
|
set_sample_rate_params(avctx);
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-07 15:56:22 +01:00
|
|
|
return set_bps_params(avctx);
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void decode_lpc(int32_t *coeffs, int mode, int length)
|
|
|
|
{
|
2012-12-08 13:18:23 +01:00
|
|
|
int i;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
if (length < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mode == 1) {
|
2012-12-08 13:18:23 +01:00
|
|
|
int a1 = *coeffs++;
|
|
|
|
for (i = 0; i < length - 1 >> 1; i++) {
|
2012-10-02 15:43:19 +02:00
|
|
|
*coeffs += a1;
|
|
|
|
coeffs[1] += *coeffs;
|
2012-12-08 13:18:23 +01:00
|
|
|
a1 = coeffs[1];
|
2012-10-02 15:43:19 +02:00
|
|
|
coeffs += 2;
|
|
|
|
}
|
2012-12-08 13:18:23 +01:00
|
|
|
if (length - 1 & 1)
|
2012-10-02 15:43:19 +02:00
|
|
|
*coeffs += a1;
|
|
|
|
} else if (mode == 2) {
|
2012-12-08 13:18:23 +01:00
|
|
|
int a1 = coeffs[1];
|
|
|
|
int a2 = a1 + *coeffs;
|
2012-10-02 15:43:19 +02:00
|
|
|
coeffs[1] = a2;
|
|
|
|
if (length > 2) {
|
|
|
|
coeffs += 2;
|
2012-12-08 13:18:23 +01:00
|
|
|
for (i = 0; i < length - 2 >> 1; i++) {
|
|
|
|
int a3 = *coeffs + a1;
|
|
|
|
int a4 = a3 + a2;
|
2012-10-02 15:43:19 +02:00
|
|
|
*coeffs = a4;
|
2012-12-08 13:18:23 +01:00
|
|
|
a1 = coeffs[1] + a3;
|
|
|
|
a2 = a1 + a4;
|
2012-10-02 15:43:19 +02:00
|
|
|
coeffs[1] = a2;
|
|
|
|
coeffs += 2;
|
|
|
|
}
|
|
|
|
if (length & 1)
|
2012-12-08 13:18:23 +01:00
|
|
|
*coeffs += a1 + a2;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
} else if (mode == 3) {
|
2012-12-08 13:18:23 +01:00
|
|
|
int a1 = coeffs[1];
|
|
|
|
int a2 = a1 + *coeffs;
|
2012-10-02 15:43:19 +02:00
|
|
|
coeffs[1] = a2;
|
|
|
|
if (length > 2) {
|
2012-12-08 13:18:23 +01:00
|
|
|
int a3 = coeffs[2];
|
|
|
|
int a4 = a3 + a1;
|
|
|
|
int a5 = a4 + a2;
|
2012-10-02 15:43:19 +02:00
|
|
|
coeffs += 3;
|
|
|
|
for (i = 0; i < length - 3; i++) {
|
2012-12-08 13:18:23 +01:00
|
|
|
a3 += *coeffs;
|
|
|
|
a4 += a3;
|
|
|
|
a5 += a4;
|
2012-10-02 15:43:19 +02:00
|
|
|
*coeffs = a5;
|
|
|
|
coeffs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len)
|
2012-10-02 15:43:19 +02:00
|
|
|
{
|
2012-12-08 13:18:23 +01:00
|
|
|
struct CParam code;
|
2012-10-02 15:43:19 +02:00
|
|
|
GetBitContext *gb = &s->gb;
|
2012-12-08 13:18:23 +01:00
|
|
|
int i;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (!mode) {
|
|
|
|
memset(decoded, 0, len * sizeof(*decoded));
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (mode > FF_ARRAY_ELEMS(xcodes))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
code = xcodes[mode - 1];
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
int x = get_bits_long(gb, code.init);
|
|
|
|
if (x >= code.escape && get_bits1(gb)) {
|
|
|
|
x |= 1 << code.init;
|
|
|
|
if (x >= code.aescape) {
|
|
|
|
int scale = get_unary(gb, 1, 9);
|
|
|
|
if (scale == 9) {
|
|
|
|
int scale_bits = get_bits(gb, 3);
|
|
|
|
if (scale_bits > 0) {
|
|
|
|
if (scale_bits == 7) {
|
|
|
|
scale_bits += get_bits(gb, 5);
|
|
|
|
if (scale_bits > 29)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
scale = get_bits_long(gb, scale_bits) + 1;
|
|
|
|
x += code.scale * scale;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
2012-12-08 13:18:23 +01:00
|
|
|
x += code.bias;
|
|
|
|
} else
|
|
|
|
x += code.scale * scale - code.escape;
|
|
|
|
} else
|
|
|
|
x -= code.escape;
|
|
|
|
}
|
|
|
|
decoded[i] = (x >> 1) ^ -(x & 1);
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
static int decode_residues(TAKDecContext *s, int32_t *decoded, int length)
|
2012-10-02 15:43:19 +02:00
|
|
|
{
|
|
|
|
GetBitContext *gb = &s->gb;
|
2012-12-08 13:18:23 +01:00
|
|
|
int i, mode, ret;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
if (length > s->nb_samples)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
if (get_bits1(gb)) {
|
2012-12-08 13:18:23 +01:00
|
|
|
int wlength, rval;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
wlength = length / s->uval;
|
|
|
|
|
|
|
|
rval = length - (wlength * s->uval);
|
|
|
|
|
|
|
|
if (rval < s->uval / 2)
|
|
|
|
rval += s->uval;
|
|
|
|
else
|
|
|
|
wlength++;
|
|
|
|
|
|
|
|
if (wlength <= 1 || wlength > 128)
|
2012-10-02 15:43:19 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
s->coding_mode[0] = mode = get_bits(gb, 6);
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
for (i = 1; i < wlength; i++) {
|
|
|
|
int c = get_unary(gb, 1, 6);
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
switch (c) {
|
|
|
|
case 6:
|
|
|
|
mode = get_bits(gb, 6);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
case 4:
|
|
|
|
case 3: {
|
|
|
|
/* mode += sign ? (1 - c) : (c - 1) */
|
|
|
|
int sign = get_bits1(gb);
|
|
|
|
mode += (-sign ^ (c - 1)) + sign;
|
|
|
|
break;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
2012-12-08 13:18:23 +01:00
|
|
|
case 2:
|
|
|
|
mode++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
mode--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->coding_mode[i] = mode;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
2012-12-08 13:18:23 +01:00
|
|
|
while (i < wlength) {
|
2012-10-02 15:43:19 +02:00
|
|
|
int len = 0;
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
mode = s->coding_mode[i];
|
2012-10-02 15:43:19 +02:00
|
|
|
do {
|
2012-12-08 13:18:23 +01:00
|
|
|
if (i >= wlength - 1)
|
|
|
|
len += rval;
|
|
|
|
else
|
|
|
|
len += s->uval;
|
2012-10-02 15:43:19 +02:00
|
|
|
i++;
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (i == wlength)
|
2012-10-02 15:43:19 +02:00
|
|
|
break;
|
2012-12-08 13:18:23 +01:00
|
|
|
} while (s->coding_mode[i] == mode);
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if ((ret = decode_segment(s, mode, decoded, len)) < 0)
|
2012-10-02 15:43:19 +02:00
|
|
|
return ret;
|
2012-12-08 13:18:23 +01:00
|
|
|
decoded += len;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-12-08 13:18:23 +01:00
|
|
|
mode = get_bits(gb, 6);
|
|
|
|
if ((ret = decode_segment(s, mode, decoded, length)) < 0)
|
2012-10-02 15:43:19 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-07 16:17:15 +01:00
|
|
|
static int get_bits_esc4(GetBitContext *gb)
|
2012-10-02 15:43:19 +02:00
|
|
|
{
|
|
|
|
if (get_bits1(gb))
|
|
|
|
return get_bits(gb, 4) + 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
static int decode_subframe(TAKDecContext *s, int32_t *decoded,
|
|
|
|
int subframe_size, int prev_subframe_size)
|
2012-10-02 15:43:19 +02:00
|
|
|
{
|
2012-12-08 13:18:23 +01:00
|
|
|
GetBitContext *gb = &s->gb;
|
2012-10-02 15:43:19 +02:00
|
|
|
int tmp, x, y, i, j, ret = 0;
|
2012-12-08 13:18:23 +01:00
|
|
|
int dshift, size, filter_quant, filter_order;
|
2012-10-02 15:43:19 +02:00
|
|
|
int tfilter[MAX_PREDICTORS];
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (!get_bits1(gb))
|
|
|
|
return decode_residues(s, decoded, subframe_size);
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
filter_order = predictor_sizes[get_bits(gb, 4)];
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (prev_subframe_size > 0 && get_bits1(gb)) {
|
|
|
|
if (filter_order > prev_subframe_size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
decoded -= filter_order;
|
|
|
|
subframe_size += filter_order;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (filter_order > subframe_size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
} else {
|
|
|
|
int lpc_mode;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (filter_order > subframe_size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
lpc_mode = get_bits(gb, 2);
|
|
|
|
if (lpc_mode > 2)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if ((ret = decode_residues(s, decoded, filter_order)) < 0)
|
|
|
|
return ret;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (lpc_mode)
|
|
|
|
decode_lpc(decoded, lpc_mode, filter_order);
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
dshift = get_bits_esc4(gb);
|
|
|
|
size = get_bits1(gb) + 6;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
filter_quant = 10;
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
filter_quant -= get_bits(gb, 3) + 1;
|
|
|
|
if (filter_quant < 3)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
s->predictors[0] = get_sbits(gb, 10);
|
|
|
|
s->predictors[1] = get_sbits(gb, 10);
|
|
|
|
s->predictors[2] = get_sbits(gb, size) << (10 - size);
|
|
|
|
s->predictors[3] = get_sbits(gb, size) << (10 - size);
|
|
|
|
if (filter_order > 4) {
|
|
|
|
tmp = size - get_bits1(gb);
|
|
|
|
|
|
|
|
for (i = 4; i < filter_order; i++) {
|
|
|
|
if (!(i & 3))
|
|
|
|
x = tmp - get_bits(gb, 2);
|
|
|
|
s->predictors[i] = get_sbits(gb, x) << (10 - size);
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
2012-12-08 13:18:23 +01:00
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
tfilter[0] = s->predictors[0] << 6;
|
|
|
|
for (i = 1; i < filter_order; i++) {
|
|
|
|
int32_t *p1 = &tfilter[0];
|
|
|
|
int32_t *p2 = &tfilter[i - 1];
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
for (j = 0; j < (i + 1) / 2; j++) {
|
|
|
|
x = *p1 + (s->predictors[i] * *p2 + 256 >> 9);
|
|
|
|
*p2 += s->predictors[i] * *p1 + 256 >> 9;
|
|
|
|
*p1++ = x;
|
|
|
|
p2--;
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
tfilter[i] = s->predictors[i] << 6;
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
x = 1 << (32 - (15 - filter_quant));
|
|
|
|
y = 1 << ((15 - filter_quant) - 1);
|
|
|
|
for (i = 0, j = filter_order - 1; i < filter_order / 2; i++, j--) {
|
|
|
|
tmp = y + tfilter[j];
|
|
|
|
s->filter[j] = x - ((tfilter[i] + y) >> (15 - filter_quant));
|
|
|
|
s->filter[i] = x - ((tfilter[j] + y) >> (15 - filter_quant));
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if ((ret = decode_residues(s, &decoded[filter_order],
|
|
|
|
subframe_size - filter_order)) < 0)
|
|
|
|
return ret;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
for (i = 0; i < filter_order; i++)
|
|
|
|
s->residues[i] = *decoded++ >> dshift;
|
|
|
|
|
|
|
|
y = FF_ARRAY_ELEMS(s->residues) - filter_order;
|
|
|
|
x = subframe_size - filter_order;
|
|
|
|
while (x > 0) {
|
|
|
|
tmp = FFMIN(y, x);
|
|
|
|
|
|
|
|
for (i = 0; i < tmp; i++) {
|
|
|
|
int v = 1 << (filter_quant - 1);
|
|
|
|
|
2013-05-09 11:59:38 +02:00
|
|
|
if (filter_order & -16)
|
2012-12-08 13:18:23 +01:00
|
|
|
v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
|
2013-05-09 11:59:38 +02:00
|
|
|
filter_order & -16);
|
|
|
|
for (j = filter_order & -16; j < filter_order; j += 4) {
|
|
|
|
v += s->residues[i + j + 3] * s->filter[j + 3] +
|
|
|
|
s->residues[i + j + 2] * s->filter[j + 2] +
|
|
|
|
s->residues[i + j + 1] * s->filter[j + 1] +
|
|
|
|
s->residues[i + j ] * s->filter[j ];
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
2012-12-08 13:18:23 +01:00
|
|
|
v = (av_clip(v >> filter_quant, -8192, 8191) << dshift) - *decoded;
|
|
|
|
*decoded++ = v;
|
|
|
|
s->residues[filter_order + i] = v >> dshift;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
x -= tmp;
|
|
|
|
if (x > 0)
|
|
|
|
memcpy(s->residues, &s->residues[y], 2 * filter_order);
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
emms_c();
|
|
|
|
|
|
|
|
return 0;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_channel(TAKDecContext *s, int chan)
|
|
|
|
{
|
|
|
|
AVCodecContext *avctx = s->avctx;
|
2012-12-08 13:18:23 +01:00
|
|
|
GetBitContext *gb = &s->gb;
|
|
|
|
int32_t *decoded = s->decoded[chan];
|
|
|
|
int left = s->nb_samples - 1;
|
2012-10-02 15:43:19 +02:00
|
|
|
int i = 0, ret, prev = 0;
|
|
|
|
|
2012-12-07 16:17:15 +01:00
|
|
|
s->sample_shift[chan] = get_bits_esc4(gb);
|
2012-10-12 10:51:47 +02:00
|
|
|
if (s->sample_shift[chan] >= avctx->bits_per_raw_sample)
|
2012-10-02 15:43:19 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
*decoded++ = get_sbits(gb, avctx->bits_per_raw_sample - s->sample_shift[chan]);
|
2012-10-02 15:43:19 +02:00
|
|
|
s->lpc_mode[chan] = get_bits(gb, 2);
|
|
|
|
s->nb_subframes = get_bits(gb, 3) + 1;
|
|
|
|
|
|
|
|
if (s->nb_subframes > 1) {
|
|
|
|
if (get_bits_left(gb) < (s->nb_subframes - 1) * 6)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
for (; i < s->nb_subframes - 1; i++) {
|
|
|
|
int v = get_bits(gb, 6);
|
|
|
|
|
|
|
|
s->subframe_len[i] = (v - prev) * s->subframe_scale;
|
|
|
|
if (s->subframe_len[i] <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
left -= s->subframe_len[i];
|
|
|
|
prev = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
s->subframe_len[i] = left;
|
2012-12-08 13:18:23 +01:00
|
|
|
|
2012-10-02 15:43:19 +02:00
|
|
|
prev = 0;
|
|
|
|
for (i = 0; i < s->nb_subframes; i++) {
|
2012-12-08 13:18:23 +01:00
|
|
|
if ((ret = decode_subframe(s, decoded, s->subframe_len[i], prev)) < 0)
|
2012-10-02 15:43:19 +02:00
|
|
|
return ret;
|
2012-12-08 13:18:23 +01:00
|
|
|
decoded += s->subframe_len[i];
|
|
|
|
prev = s->subframe_len[i];
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
|
|
|
|
{
|
2012-12-08 13:18:23 +01:00
|
|
|
GetBitContext *gb = &s->gb;
|
|
|
|
int32_t *p1 = s->decoded[c1] + 1;
|
|
|
|
int32_t *p2 = s->decoded[c2] + 1;
|
|
|
|
int i;
|
|
|
|
int dshift, dfactor;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
switch (s->dmode) {
|
2012-12-08 13:18:23 +01:00
|
|
|
case 1: /* left/side */
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
int32_t a = p1[i];
|
|
|
|
int32_t b = p2[i];
|
|
|
|
p2[i] = a + b;
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
break;
|
2012-12-08 13:18:23 +01:00
|
|
|
case 2: /* side/right */
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
int32_t a = p1[i];
|
|
|
|
int32_t b = p2[i];
|
|
|
|
p1[i] = b - a;
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
break;
|
2012-12-08 13:18:23 +01:00
|
|
|
case 3: /* side/mid */
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
int32_t a = p1[i];
|
|
|
|
int32_t b = p2[i];
|
|
|
|
a -= b >> 1;
|
|
|
|
p1[i] = a;
|
|
|
|
p2[i] = a + b;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-12-08 13:18:23 +01:00
|
|
|
case 4: /* side/left with scale factor */
|
|
|
|
FFSWAP(int32_t*, p1, p2);
|
|
|
|
case 5: /* side/right with scale factor */
|
|
|
|
dshift = get_bits_esc4(gb);
|
|
|
|
dfactor = get_sbits(gb, 10);
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
int32_t a = p1[i];
|
|
|
|
int32_t b = p2[i];
|
|
|
|
b = dfactor * (b >> dshift) + 128 >> 8 << dshift;
|
|
|
|
p1[i] = b - a;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6:
|
2012-12-08 13:18:23 +01:00
|
|
|
FFSWAP(int32_t*, p1, p2);
|
|
|
|
case 7: {
|
|
|
|
int length2, order_half, filter_order, dval1, dval2;
|
|
|
|
int tmp, x, code_size;
|
|
|
|
|
2012-10-02 15:43:19 +02:00
|
|
|
if (length < 256)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
dshift = get_bits_esc4(gb);
|
|
|
|
filter_order = 8 << get_bits1(gb);
|
|
|
|
dval1 = get_bits1(gb);
|
|
|
|
dval2 = get_bits1(gb);
|
|
|
|
|
|
|
|
for (i = 0; i < filter_order; i++) {
|
|
|
|
if (!(i & 3))
|
|
|
|
code_size = 14 - get_bits(gb, 3);
|
|
|
|
s->filter[i] = get_sbits(gb, code_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
order_half = filter_order / 2;
|
|
|
|
length2 = length - (filter_order - 1);
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
/* decorrelate beginning samples */
|
|
|
|
if (dval1) {
|
|
|
|
for (i = 0; i < order_half; i++) {
|
|
|
|
int32_t a = p1[i];
|
|
|
|
int32_t b = p2[i];
|
|
|
|
p1[i] = a + b;
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
/* decorrelate ending samples */
|
|
|
|
if (dval2) {
|
|
|
|
for (i = length2 + order_half; i < length; i++) {
|
|
|
|
int32_t a = p1[i];
|
|
|
|
int32_t b = p2[i];
|
|
|
|
p1[i] = a + b;
|
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
for (i = 0; i < filter_order; i++)
|
|
|
|
s->residues[i] = *p2++ >> dshift;
|
|
|
|
|
|
|
|
p1 += order_half;
|
|
|
|
x = FF_ARRAY_ELEMS(s->residues) - filter_order;
|
|
|
|
for (; length2 > 0; length2 -= tmp) {
|
|
|
|
tmp = FFMIN(length2, x);
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
for (i = 0; i < tmp; i++)
|
2012-12-08 13:18:23 +01:00
|
|
|
s->residues[filter_order + i] = *p2++ >> dshift;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
for (i = 0; i < tmp; i++) {
|
2012-12-08 13:18:23 +01:00
|
|
|
int v = 1 << 9;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (filter_order == 16) {
|
2012-10-02 15:43:19 +02:00
|
|
|
v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
|
2012-12-08 13:18:23 +01:00
|
|
|
filter_order);
|
2012-10-02 15:43:19 +02:00
|
|
|
} else {
|
|
|
|
v += s->residues[i + 7] * s->filter[7] +
|
|
|
|
s->residues[i + 6] * s->filter[6] +
|
|
|
|
s->residues[i + 5] * s->filter[5] +
|
|
|
|
s->residues[i + 4] * s->filter[4] +
|
|
|
|
s->residues[i + 3] * s->filter[3] +
|
|
|
|
s->residues[i + 2] * s->filter[2] +
|
|
|
|
s->residues[i + 1] * s->filter[1] +
|
|
|
|
s->residues[i ] * s->filter[0];
|
|
|
|
}
|
|
|
|
|
2012-12-10 13:04:02 +01:00
|
|
|
v = (av_clip(v >> 10, -8192, 8191) << dshift) - *p1;
|
|
|
|
*p1++ = v;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
memcpy(s->residues, &s->residues[tmp], 2 * filter_order);
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emms_c();
|
|
|
|
break;
|
|
|
|
}
|
2012-12-08 13:18:23 +01:00
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tak_decode_frame(AVCodecContext *avctx, void *data,
|
|
|
|
int *got_frame_ptr, AVPacket *pkt)
|
|
|
|
{
|
2012-12-08 13:18:23 +01:00
|
|
|
TAKDecContext *s = avctx->priv_data;
|
2012-12-24 02:02:46 +01:00
|
|
|
AVFrame *frame = data;
|
2013-06-03 17:42:39 +02:00
|
|
|
ThreadFrame tframe = { .f = data };
|
2012-10-02 15:43:19 +02:00
|
|
|
GetBitContext *gb = &s->gb;
|
|
|
|
int chan, i, ret, hsize;
|
|
|
|
|
|
|
|
if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2013-01-28 23:47:30 +01:00
|
|
|
if ((ret = init_get_bits8(gb, pkt->data, pkt->size)) < 0)
|
|
|
|
return ret;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2014-05-08 17:33:28 +02:00
|
|
|
hsize = get_bits_count(gb) / 8;
|
2013-10-27 09:28:30 +01:00
|
|
|
if (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) {
|
2012-10-02 15:43:19 +02:00
|
|
|
if (ff_tak_check_crc(pkt->data, hsize)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CRC error\n");
|
2013-10-26 22:46:48 +02:00
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (s->ti.codec != TAK_CODEC_MONO_STEREO &&
|
|
|
|
s->ti.codec != TAK_CODEC_MULTICHANNEL) {
|
2012-10-02 15:43:19 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
if (s->ti.data_type) {
|
2012-12-08 13:18:23 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"unsupported data type: %d\n", s->ti.data_type);
|
2012-10-02 15:43:19 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2012-12-08 13:18:23 +01:00
|
|
|
if (s->ti.codec == TAK_CODEC_MONO_STEREO && s->ti.channels > 2) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"invalid number of channels: %d\n", s->ti.channels);
|
2012-10-02 15:43:19 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
if (s->ti.channels > 6) {
|
2012-12-08 13:18:23 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"unsupported number of channels: %d\n", s->ti.channels);
|
2012-10-02 15:43:19 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->ti.frame_samples <= 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2014-01-31 19:16:02 +01:00
|
|
|
avctx->bits_per_raw_sample = s->ti.bps;
|
|
|
|
if ((ret = set_bps_params(avctx)) < 0)
|
|
|
|
return ret;
|
2012-10-02 15:43:19 +02:00
|
|
|
if (s->ti.sample_rate != avctx->sample_rate) {
|
|
|
|
avctx->sample_rate = s->ti.sample_rate;
|
2012-12-07 15:56:22 +01:00
|
|
|
set_sample_rate_params(avctx);
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
if (s->ti.ch_layout)
|
|
|
|
avctx->channel_layout = s->ti.ch_layout;
|
|
|
|
avctx->channels = s->ti.channels;
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples
|
|
|
|
: s->ti.frame_samples;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-24 02:02:46 +01:00
|
|
|
frame->nb_samples = s->nb_samples;
|
2013-06-03 17:42:39 +02:00
|
|
|
if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
|
2012-10-02 15:43:19 +02:00
|
|
|
return ret;
|
2013-06-03 17:42:39 +02:00
|
|
|
ff_thread_finish_setup(avctx);
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-10-12 10:51:47 +02:00
|
|
|
if (avctx->bits_per_raw_sample <= 16) {
|
2012-12-07 02:11:55 +01:00
|
|
|
int buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
|
|
|
|
s->nb_samples,
|
|
|
|
AV_SAMPLE_FMT_S32P, 0);
|
|
|
|
av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size, buf_size);
|
2012-10-02 15:43:19 +02:00
|
|
|
if (!s->decode_buffer)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-12-07 02:11:55 +01:00
|
|
|
ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
|
|
|
|
s->decode_buffer, avctx->channels,
|
|
|
|
s->nb_samples, AV_SAMPLE_FMT_S32P, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-10-02 15:43:19 +02:00
|
|
|
} else {
|
|
|
|
for (chan = 0; chan < avctx->channels; chan++)
|
2012-12-24 02:02:46 +01:00
|
|
|
s->decoded[chan] = (int32_t *)frame->extended_data[chan];
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->nb_samples < 16) {
|
|
|
|
for (chan = 0; chan < avctx->channels; chan++) {
|
2012-12-08 13:18:23 +01:00
|
|
|
int32_t *decoded = s->decoded[chan];
|
2012-10-02 15:43:19 +02:00
|
|
|
for (i = 0; i < s->nb_samples; i++)
|
2012-12-08 13:18:23 +01:00
|
|
|
decoded[i] = get_sbits(gb, avctx->bits_per_raw_sample);
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-12-08 13:18:23 +01:00
|
|
|
if (s->ti.codec == TAK_CODEC_MONO_STEREO) {
|
|
|
|
for (chan = 0; chan < avctx->channels; chan++)
|
2012-10-02 15:43:19 +02:00
|
|
|
if (ret = decode_channel(s, chan))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (avctx->channels == 2) {
|
|
|
|
s->nb_subframes = get_bits(gb, 1) + 1;
|
2012-12-08 13:18:23 +01:00
|
|
|
if (s->nb_subframes > 1) {
|
2012-10-02 15:43:19 +02:00
|
|
|
s->subframe_len[1] = get_bits(gb, 6);
|
2012-12-08 13:18:23 +01:00
|
|
|
}
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
s->dmode = get_bits(gb, 3);
|
|
|
|
if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))
|
|
|
|
return ret;
|
|
|
|
}
|
2012-12-08 13:18:23 +01:00
|
|
|
} else if (s->ti.codec == TAK_CODEC_MULTICHANNEL) {
|
2012-10-02 15:43:19 +02:00
|
|
|
if (get_bits1(gb)) {
|
|
|
|
int ch_mask = 0;
|
|
|
|
|
|
|
|
chan = get_bits(gb, 4) + 1;
|
|
|
|
if (chan > avctx->channels)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
for (i = 0; i < chan; i++) {
|
|
|
|
int nbit = get_bits(gb, 4);
|
|
|
|
|
|
|
|
if (nbit >= avctx->channels)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
if (ch_mask & 1 << nbit)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
s->mcdparams[i].present = get_bits1(gb);
|
|
|
|
if (s->mcdparams[i].present) {
|
|
|
|
s->mcdparams[i].index = get_bits(gb, 2);
|
|
|
|
s->mcdparams[i].chan2 = get_bits(gb, 4);
|
|
|
|
if (s->mcdparams[i].index == 1) {
|
|
|
|
if ((nbit == s->mcdparams[i].chan2) ||
|
|
|
|
(ch_mask & 1 << s->mcdparams[i].chan2))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
ch_mask |= 1 << s->mcdparams[i].chan2;
|
|
|
|
} else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->mcdparams[i].chan1 = nbit;
|
|
|
|
|
|
|
|
ch_mask |= 1 << nbit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chan = avctx->channels;
|
|
|
|
for (i = 0; i < chan; i++) {
|
|
|
|
s->mcdparams[i].present = 0;
|
|
|
|
s->mcdparams[i].chan1 = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < chan; i++) {
|
2012-12-08 13:18:23 +01:00
|
|
|
if (s->mcdparams[i].present && s->mcdparams[i].index == 1)
|
2012-10-02 15:43:19 +02:00
|
|
|
if (ret = decode_channel(s, s->mcdparams[i].chan2))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (ret = decode_channel(s, s->mcdparams[i].chan1))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (s->mcdparams[i].present) {
|
|
|
|
s->dmode = mc_dmodes[s->mcdparams[i].index];
|
2012-12-08 13:18:23 +01:00
|
|
|
if (ret = decorrelate(s,
|
|
|
|
s->mcdparams[i].chan2,
|
|
|
|
s->mcdparams[i].chan1,
|
|
|
|
s->nb_samples - 1))
|
2012-10-02 15:43:19 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (chan = 0; chan < avctx->channels; chan++) {
|
2012-12-08 13:18:23 +01:00
|
|
|
int32_t *decoded = s->decoded[chan];
|
|
|
|
|
|
|
|
if (s->lpc_mode[chan])
|
|
|
|
decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples);
|
2012-10-02 15:43:19 +02:00
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
if (s->sample_shift[chan] > 0)
|
2012-10-02 15:43:19 +02:00
|
|
|
for (i = 0; i < s->nb_samples; i++)
|
2012-12-08 13:18:23 +01:00
|
|
|
decoded[i] <<= s->sample_shift[chan];
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
align_get_bits(gb);
|
|
|
|
skip_bits(gb, 24);
|
|
|
|
if (get_bits_left(gb) < 0)
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "overread\n");
|
|
|
|
else if (get_bits_left(gb) > 0)
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "underread\n");
|
|
|
|
|
2013-10-27 09:28:30 +01:00
|
|
|
if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_COMPLIANT)) {
|
2012-10-02 15:43:19 +02:00
|
|
|
if (ff_tak_check_crc(pkt->data + hsize,
|
|
|
|
get_bits_count(gb) / 8 - hsize)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CRC error\n");
|
2013-10-26 22:46:48 +02:00
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-08 13:18:23 +01:00
|
|
|
/* convert to output buffer */
|
|
|
|
switch (avctx->sample_fmt) {
|
|
|
|
case AV_SAMPLE_FMT_U8P:
|
2012-10-02 15:43:19 +02:00
|
|
|
for (chan = 0; chan < avctx->channels; chan++) {
|
2012-12-24 02:02:46 +01:00
|
|
|
uint8_t *samples = (uint8_t *)frame->extended_data[chan];
|
2012-12-08 13:18:23 +01:00
|
|
|
int32_t *decoded = s->decoded[chan];
|
|
|
|
for (i = 0; i < s->nb_samples; i++)
|
|
|
|
samples[i] = decoded[i] + 0x80;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-12-08 13:18:23 +01:00
|
|
|
case AV_SAMPLE_FMT_S16P:
|
2012-10-02 15:43:19 +02:00
|
|
|
for (chan = 0; chan < avctx->channels; chan++) {
|
2012-12-24 02:02:46 +01:00
|
|
|
int16_t *samples = (int16_t *)frame->extended_data[chan];
|
2012-12-08 13:18:23 +01:00
|
|
|
int32_t *decoded = s->decoded[chan];
|
|
|
|
for (i = 0; i < s->nb_samples; i++)
|
|
|
|
samples[i] = decoded[i];
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-12-08 13:18:23 +01:00
|
|
|
case AV_SAMPLE_FMT_S32P:
|
2012-10-02 15:43:19 +02:00
|
|
|
for (chan = 0; chan < avctx->channels; chan++) {
|
2012-12-24 02:02:46 +01:00
|
|
|
int32_t *samples = (int32_t *)frame->extended_data[chan];
|
2012-10-02 15:43:19 +02:00
|
|
|
for (i = 0; i < s->nb_samples; i++)
|
2012-12-08 13:18:23 +01:00
|
|
|
samples[i] <<= 8;
|
2012-10-02 15:43:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-24 02:02:46 +01:00
|
|
|
*got_frame_ptr = 1;
|
2012-10-02 15:43:19 +02:00
|
|
|
|
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
2013-06-13 23:49:18 +02:00
|
|
|
static int init_thread_copy(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
TAKDecContext *s = avctx->priv_data;
|
|
|
|
s->avctx = avctx;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-03 17:42:39 +02:00
|
|
|
static int update_thread_context(AVCodecContext *dst,
|
|
|
|
const AVCodecContext *src)
|
|
|
|
{
|
|
|
|
TAKDecContext *tsrc = src->priv_data;
|
|
|
|
TAKDecContext *tdst = dst->priv_data;
|
|
|
|
|
|
|
|
if (dst == src)
|
|
|
|
return 0;
|
|
|
|
memcpy(&tdst->ti, &tsrc->ti, sizeof(TAKStreamInfo));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-02 15:43:19 +02:00
|
|
|
static av_cold int tak_decode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
TAKDecContext *s = avctx->priv_data;
|
|
|
|
|
|
|
|
av_freep(&s->decode_buffer);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec ff_tak_decoder = {
|
2012-12-08 13:18:23 +01:00
|
|
|
.name = "tak",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
|
2012-12-08 13:18:23 +01:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.id = AV_CODEC_ID_TAK,
|
|
|
|
.priv_data_size = sizeof(TAKDecContext),
|
|
|
|
.init = tak_decode_init,
|
|
|
|
.close = tak_decode_close,
|
|
|
|
.decode = tak_decode_frame,
|
2013-06-13 23:49:18 +02:00
|
|
|
.init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
|
2013-06-06 22:17:36 +02:00
|
|
|
.update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
|
2013-06-03 17:42:39 +02:00
|
|
|
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
|
2012-12-08 13:18:23 +01:00
|
|
|
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
|
|
|
|
AV_SAMPLE_FMT_S16P,
|
|
|
|
AV_SAMPLE_FMT_S32P,
|
|
|
|
AV_SAMPLE_FMT_NONE },
|
2012-10-02 15:43:19 +02:00
|
|
|
};
|