Commit cef28b56 authored by Paul B Mahol's avatar Paul B Mahol

tak: reduce difference with qatar

Mostly cosmetics changes, but also makes
decoding little faster here.
Signed-off-by: 's avatarPaul B Mahol <onemda@gmail.com>
parent 461b5bf4
...@@ -56,12 +56,13 @@ static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type) ...@@ -56,12 +56,13 @@ static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
if (type <= TAK_FST_250ms) { if (type <= TAK_FST_250ms) {
nb_samples = sample_rate * frame_duration_type_quants[type] >> nb_samples = sample_rate * frame_duration_type_quants[type] >>
TAK_FRAME_DURATION_QUANT_SHIFT; TAK_FRAME_DURATION_QUANT_SHIFT;
max_nb_samples = 16384; max_nb_samples = 16384;
} else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) { } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
nb_samples = frame_duration_type_quants[type]; nb_samples = frame_duration_type_quants[type];
max_nb_samples = sample_rate * frame_duration_type_quants[TAK_FST_250ms] >> max_nb_samples = sample_rate *
TAK_FRAME_DURATION_QUANT_SHIFT; frame_duration_type_quants[TAK_FST_250ms] >>
TAK_FRAME_DURATION_QUANT_SHIFT;
} else { } else {
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
...@@ -116,9 +117,12 @@ void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s) ...@@ -116,9 +117,12 @@ void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
s->samples = get_bits64(gb, TAK_SIZE_SAMPLES_NUM_BITS); s->samples = get_bits64(gb, TAK_SIZE_SAMPLES_NUM_BITS);
s->data_type = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS); s->data_type = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS);
s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) + TAK_SAMPLE_RATE_MIN; s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) +
s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) + TAK_BPS_MIN; TAK_SAMPLE_RATE_MIN;
s->channels = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) + TAK_CHANNELS_MIN; s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) +
TAK_BPS_MIN;
s->channels = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) +
TAK_CHANNELS_MIN;
if (get_bits1(gb)) { if (get_bits1(gb)) {
skip_bits(gb, TAK_FORMAT_VALID_BITS); skip_bits(gb, TAK_FORMAT_VALID_BITS);
...@@ -136,31 +140,25 @@ void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s) ...@@ -136,31 +140,25 @@ void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type); s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
} }
#define FRAME_IS_LAST 1
#define FRAME_HAVE_INFO 2
#define FRAME_HAVE_METADATA 4
int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
TAKStreamInfo *ti, int log_level_offset) TAKStreamInfo *ti, int log_level_offset)
{ {
int flags;
if (get_bits(gb, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) { if (get_bits(gb, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {
av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n"); av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
flags = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS); ti->flags = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS);
ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS); ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS);
if (flags & FRAME_IS_LAST) { if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
ti->last_frame_samples = get_bits(gb, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1; ti->last_frame_samples = get_bits(gb, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;
skip_bits(gb, 2); skip_bits(gb, 2);
} else { } else {
ti->last_frame_samples = 0; ti->last_frame_samples = 0;
} }
if (flags & FRAME_HAVE_INFO) { if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
avpriv_tak_parse_streaminfo(gb, ti); avpriv_tak_parse_streaminfo(gb, ti);
if (get_bits(gb, 6)) if (get_bits(gb, 6))
...@@ -168,7 +166,7 @@ int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, ...@@ -168,7 +166,7 @@ int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
align_get_bits(gb); align_get_bits(gb);
} }
if (flags & FRAME_HAVE_METADATA) if (ti->flags & TAK_FRAME_FLAG_HAS_METADATA)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
skip_bits(gb, 24); skip_bits(gb, 24);
......
...@@ -33,64 +33,74 @@ ...@@ -33,64 +33,74 @@
#include "get_bits.h" #include "get_bits.h"
#include "avcodec.h" #include "avcodec.h"
#define TAK_FORMAT_DATA_TYPE_BITS 3 #define TAK_FORMAT_DATA_TYPE_BITS 3
#define TAK_FORMAT_SAMPLE_RATE_BITS 18 #define TAK_FORMAT_SAMPLE_RATE_BITS 18
#define TAK_FORMAT_BPS_BITS 5 #define TAK_FORMAT_BPS_BITS 5
#define TAK_FORMAT_CHANNEL_BITS 4 #define TAK_FORMAT_CHANNEL_BITS 4
#define TAK_FORMAT_VALID_BITS 5 #define TAK_FORMAT_VALID_BITS 5
#define TAK_FORMAT_CH_LAYOUT_BITS 6 #define TAK_FORMAT_CH_LAYOUT_BITS 6
#define TAK_SIZE_FRAME_DURATION_BITS 4 #define TAK_SIZE_FRAME_DURATION_BITS 4
#define TAK_SIZE_SAMPLES_NUM_BITS 35 #define TAK_SIZE_SAMPLES_NUM_BITS 35
#define TAK_LAST_FRAME_POS_BITS 40 #define TAK_LAST_FRAME_POS_BITS 40
#define TAK_LAST_FRAME_SIZE_BITS 24 #define TAK_LAST_FRAME_SIZE_BITS 24
#define TAK_ENCODER_CODEC_BITS 6 #define TAK_ENCODER_CODEC_BITS 6
#define TAK_ENCODER_PROFILE_BITS 4 #define TAK_ENCODER_PROFILE_BITS 4
#define TAK_ENCODER_VERSION_BITS 24 #define TAK_ENCODER_VERSION_BITS 24
#define TAK_SAMPLE_RATE_MIN 6000 #define TAK_SAMPLE_RATE_MIN 6000
#define TAK_CHANNELS_MIN 1 #define TAK_CHANNELS_MIN 1
#define TAK_BPS_MIN 8 #define TAK_BPS_MIN 8
#define TAK_FRAME_HEADER_FLAGS_BITS 3 #define TAK_FRAME_HEADER_FLAGS_BITS 3
#define TAK_FRAME_HEADER_SYNC_ID 0xA0FF #define TAK_FRAME_HEADER_SYNC_ID 0xA0FF
#define TAK_FRAME_HEADER_SYNC_ID_BITS 16 #define TAK_FRAME_HEADER_SYNC_ID_BITS 16
#define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS 14 #define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS 14
#define TAK_FRAME_HEADER_NO_BITS 21 #define TAK_FRAME_HEADER_NO_BITS 21
#define TAK_FRAME_DURATION_QUANT_SHIFT 5 #define TAK_FRAME_DURATION_QUANT_SHIFT 5
#define TAK_CRC24_BITS 24 #define TAK_CRC24_BITS 24
#define TAK_MAX_CHANNELS ( 1 << TAK_FORMAT_CHANNEL_BITS )
#define TAK_MIN_FRAME_HEADER_BITS ( TAK_FRAME_HEADER_SYNC_ID_BITS + \ #define TAK_FRAME_FLAG_IS_LAST 0x1
TAK_FRAME_HEADER_FLAGS_BITS + \ #define TAK_FRAME_FLAG_HAS_INFO 0x2
TAK_FRAME_HEADER_NO_BITS + \ #define TAK_FRAME_FLAG_HAS_METADATA 0x4
TAK_CRC24_BITS )
#define TAK_MIN_FRAME_HEADER_LAST_BITS ( TAK_MIN_FRAME_HEADER_BITS + 2 + \ #define TAK_MAX_CHANNELS (1 << TAK_FORMAT_CHANNEL_BITS)
TAK_FRAME_HEADER_SAMPLE_COUNT_BITS )
#define TAK_ENCODER_BITS ( TAK_ENCODER_CODEC_BITS + \ #define TAK_MIN_FRAME_HEADER_BITS (TAK_FRAME_HEADER_SYNC_ID_BITS + \
TAK_ENCODER_PROFILE_BITS ) TAK_FRAME_HEADER_FLAGS_BITS + \
TAK_FRAME_HEADER_NO_BITS + \
TAK_CRC24_BITS)
#define TAK_SIZE_BITS ( TAK_SIZE_SAMPLES_NUM_BITS + \ #define TAK_MIN_FRAME_HEADER_LAST_BITS (TAK_MIN_FRAME_HEADER_BITS + 2 + \
TAK_SIZE_FRAME_DURATION_BITS ) TAK_FRAME_HEADER_SAMPLE_COUNT_BITS)
#define TAK_FORMAT_BITS ( TAK_FORMAT_DATA_TYPE_BITS + \ #define TAK_ENCODER_BITS (TAK_ENCODER_CODEC_BITS + \
TAK_FORMAT_SAMPLE_RATE_BITS + \ TAK_ENCODER_PROFILE_BITS)
TAK_FORMAT_BPS_BITS + \
TAK_FORMAT_CHANNEL_BITS + 1 + \
TAK_FORMAT_VALID_BITS + 1 + \
TAK_FORMAT_CH_LAYOUT_BITS * \
TAK_MAX_CHANNELS )
#define TAK_STREAMINFO_BITS ( TAK_ENCODER_BITS + \ #define TAK_SIZE_BITS (TAK_SIZE_SAMPLES_NUM_BITS + \
TAK_SIZE_BITS + \ TAK_SIZE_FRAME_DURATION_BITS)
TAK_FORMAT_BITS )
#define TAK_MAX_FRAME_HEADER_BITS ( TAK_MIN_FRAME_HEADER_LAST_BITS + \ #define TAK_FORMAT_BITS (TAK_FORMAT_DATA_TYPE_BITS + \
TAK_STREAMINFO_BITS + 31 ) TAK_FORMAT_SAMPLE_RATE_BITS + \
TAK_FORMAT_BPS_BITS + \
TAK_FORMAT_CHANNEL_BITS + 1 + \
TAK_FORMAT_VALID_BITS + 1 + \
TAK_FORMAT_CH_LAYOUT_BITS * \
TAK_MAX_CHANNELS)
#define TAK_STREAMINFO_BYTES (( TAK_STREAMINFO_BITS + 7 ) / 8) #define TAK_STREAMINFO_BITS (TAK_ENCODER_BITS + \
#define TAK_MAX_FRAME_HEADER_BYTES (( TAK_MAX_FRAME_HEADER_BITS + 7 ) / 8) TAK_SIZE_BITS + \
#define TAK_MIN_FRAME_HEADER_BYTES (( TAK_MIN_FRAME_HEADER_BITS + 7 ) / 8) TAK_FORMAT_BITS)
#define TAK_MAX_FRAME_HEADER_BITS (TAK_MIN_FRAME_HEADER_LAST_BITS + \
TAK_STREAMINFO_BITS + 31)
#define TAK_STREAMINFO_BYTES ((TAK_STREAMINFO_BITS + 7) / 8)
#define TAK_MAX_FRAME_HEADER_BYTES ((TAK_MAX_FRAME_HEADER_BITS + 7) / 8)
#define TAK_MIN_FRAME_HEADER_BYTES ((TAK_MIN_FRAME_HEADER_BITS + 7) / 8)
enum TAKCodecType {
TAK_CODEC_MONO_STEREO = 2,
TAK_CODEC_MULTICHANNEL = 4,
};
enum TAKMetaDataType { enum TAKMetaDataType {
TAK_METADATA_END = 0, TAK_METADATA_END = 0,
...@@ -117,16 +127,17 @@ enum TAKFrameSizeType { ...@@ -117,16 +127,17 @@ enum TAKFrameSizeType {
}; };
typedef struct TAKStreamInfo { typedef struct TAKStreamInfo {
int codec; int flags;
int data_type; enum TAKCodecType codec;
int sample_rate; int data_type;
int channels; int sample_rate;
int bps; int channels;
int frame_num; int bps;
int frame_samples; int frame_num;
int last_frame_samples; int frame_samples;
uint64_t ch_layout; int last_frame_samples;
int64_t samples; uint64_t ch_layout;
int64_t samples;
} TAKStreamInfo; } TAKStreamInfo;
void ff_tak_init_crc(void); void ff_tak_init_crc(void);
...@@ -145,8 +156,9 @@ void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s); ...@@ -145,8 +156,9 @@ void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s);
* @param avctx AVCodecContext to use as av_log() context * @param avctx AVCodecContext to use as av_log() context
* @param[in] gb GetBitContext from which to read frame header * @param[in] gb GetBitContext from which to read frame header
* @param[out] s frame information * @param[out] s frame information
* @param log_level_offset log level offset. can be used to silence error messages. * @param log_level_offset log level offset, can be used to silence
* @return non-zero on error, 0 if ok * error messages.
* @return non-zero on error, 0 if OK
*/ */
int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
TAKStreamInfo *s, int log_level_offset); TAKStreamInfo *s, int log_level_offset);
......
...@@ -24,8 +24,8 @@ ...@@ -24,8 +24,8 @@
* TAK parser * TAK parser
**/ **/
#include "parser.h"
#include "tak.h" #include "tak.h"
#include "parser.h"
typedef struct TAKParseContext { typedef struct TAKParseContext {
ParseContext pc; ParseContext pc;
...@@ -44,18 +44,18 @@ static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx, ...@@ -44,18 +44,18 @@ static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
const uint8_t *buf, int buf_size) const uint8_t *buf, int buf_size)
{ {
TAKParseContext *t = s->priv_data; TAKParseContext *t = s->priv_data;
ParseContext *pc = &t->pc; ParseContext *pc = &t->pc;
int next = END_NOT_FOUND; int next = END_NOT_FOUND;
GetBitContext gb; GetBitContext gb;
int consumed = 0; int consumed = 0;
int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8; int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
TAKStreamInfo ti; TAKStreamInfo ti;
init_get_bits(&gb, buf, buf_size); init_get_bits(&gb, buf, buf_size);
if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127)) if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples : s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
t->ti.frame_samples; : t->ti.frame_samples;
*poutbuf = buf; *poutbuf = buf;
*poutbuf_size = buf_size; *poutbuf_size = buf_size;
return buf_size; return buf_size;
...@@ -63,7 +63,8 @@ static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx, ...@@ -63,7 +63,8 @@ static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
while (buf_size || t->index + needed <= pc->index) { while (buf_size || t->index + needed <= pc->index) {
if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) { if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
int tmp_buf_size = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES, buf_size); int tmp_buf_size = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES,
buf_size);
const uint8_t *tmp_buf = buf; const uint8_t *tmp_buf = buf;
if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1) if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
...@@ -86,13 +87,13 @@ static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx, ...@@ -86,13 +87,13 @@ static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
get_bits_count(&gb) / 8)) { get_bits_count(&gb) / 8)) {
if (!pc->frame_start_found) { if (!pc->frame_start_found) {
pc->frame_start_found = 1; pc->frame_start_found = 1;
s->duration = t->ti.last_frame_samples ? s->duration = t->ti.last_frame_samples ?
t->ti.last_frame_samples : t->ti.last_frame_samples :
t->ti.frame_samples; t->ti.frame_samples;
} else { } else {
pc->frame_start_found = 0; pc->frame_start_found = 0;
next = t->index - pc->index; next = t->index - pc->index;
t->index = 0; t->index = 0;
goto found; goto found;
} }
} }
...@@ -109,7 +110,7 @@ found: ...@@ -109,7 +110,7 @@ found:
} }
if (next != END_NOT_FOUND) { if (next != END_NOT_FOUND) {
next += consumed; next += consumed;
pc->overread = FFMAX(0, -next); pc->overread = FFMAX(0, -next);
} }
......
...@@ -28,73 +28,61 @@ ...@@ -28,73 +28,61 @@
#include "libavutil/samplefmt.h" #include "libavutil/samplefmt.h"
#include "tak.h" #include "tak.h"
#include "avcodec.h" #include "avcodec.h"
#include "dsputil.h"
#include "internal.h" #include "internal.h"
#include "unary.h" #include "unary.h"
#include "dsputil.h"
#define MAX_SUBFRAMES 8 ///< max number of subframes per channel #define MAX_SUBFRAMES 8 ///< max number of subframes per channel
#define MAX_PREDICTORS 256 #define MAX_PREDICTORS 256
typedef struct MCDParam { typedef struct MCDParam {
int8_t present; ///< is decorrelation parameters available for this channel int8_t present; ///< decorrelation parameter availability for this channel
int8_t index; ///< index into array of decorrelation types int8_t index; ///< index into array of decorrelation types
int8_t chan1; int8_t chan1;
int8_t chan2; int8_t chan2;
} MCDParam; } MCDParam;
typedef struct TAKDecContext { typedef struct TAKDecContext {
AVCodecContext *avctx; ///< parent AVCodecContext AVCodecContext *avctx; ///< parent AVCodecContext
AVFrame frame; ///< AVFrame for decoded output AVFrame frame; ///< AVFrame for decoded output
DSPContext dsp; DSPContext dsp;
TAKStreamInfo ti; TAKStreamInfo ti;
GetBitContext gb; ///< bitstream reader initialized to start at the current frame GetBitContext gb; ///< bitstream reader initialized to start at the current frame
int nb_samples; ///< number of samples in the current frame int uval;
int nb_samples; ///< number of samples in the current frame
uint8_t *decode_buffer; uint8_t *decode_buffer;
unsigned int decode_buffer_size; unsigned int decode_buffer_size;
int32_t *decoded[TAK_MAX_CHANNELS]; ///< decoded samples for each channel int32_t *decoded[TAK_MAX_CHANNELS]; ///< decoded samples for each channel
int8_t lpc_mode[TAK_MAX_CHANNELS]; int8_t lpc_mode[TAK_MAX_CHANNELS];
int8_t sample_shift[TAK_MAX_CHANNELS]; ///< shift applied to every sample in the channel int8_t sample_shift[TAK_MAX_CHANNELS]; ///< shift applied to every sample in the channel
int32_t xred; int16_t predictors[MAX_PREDICTORS];
int size; int nb_subframes; ///< number of subframes in the current frame
int ared; int16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
int filter_order; int subframe_scale;
int16_t predictors[MAX_PREDICTORS];
int nb_subframes; ///< number of subframes in the current frame int8_t dmode; ///< channel decorrelation type in the current frame
int16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
int subframe_scale; MCDParam mcdparams[TAK_MAX_CHANNELS]; ///< multichannel decorrelation parameters
int8_t dmode; ///< channel decorrelation type in the current frame int8_t coding_mode[128];
int8_t dshift;
int16_t dfactor;
int8_t dval1;
int8_t dval2;
MCDParam mcdparams[TAK_MAX_CHANNELS]; ///< multichannel decorrelation parameters
int wlength;
int uval;
int rval;
int8_t coding_mode[128];
DECLARE_ALIGNED(16, int16_t, filter)[MAX_PREDICTORS]; DECLARE_ALIGNED(16, int16_t, filter)[MAX_PREDICTORS];
DECLARE_ALIGNED(16, int16_t, residues)[544]; DECLARE_ALIGNED(16, int16_t, residues)[544];
} TAKDecContext; } TAKDecContext;
static const int8_t mc_dmodes[] = { static const int8_t mc_dmodes[] = { 1, 3, 4, 6, };
1, 3, 4, 6,
};
static const uint16_t predictor_sizes[] = { static const uint16_t predictor_sizes[] = {
4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0, 4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0,
}; };
static const struct CParam { static const struct CParam {
int init; int init;
int escape; int escape;
int scale; int scale;
int aescape; int aescape;
int bias; int bias;
} xcodes[50] = { } xcodes[50] = {
{ 0x01, 0x0000001, 0x0000001, 0x0000003, 0x0000008 }, { 0x01, 0x0000001, 0x0000001, 0x0000003, 0x0000008 },
{ 0x02, 0x0000003, 0x0000001, 0x0000007, 0x0000006 }, { 0x02, 0x0000003, 0x0000001, 0x0000007, 0x0000006 },
...@@ -161,7 +149,8 @@ static int set_bps_params(AVCodecContext *avctx) ...@@ -161,7 +149,8 @@ static int set_bps_params(AVCodecContext *avctx)
avctx->sample_fmt = AV_SAMPLE_FMT_S32P; avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
break; break;
default: default:
av_log(avctx, AV_LOG_ERROR, "invalid/unsupported bits per sample\n"); av_log(avctx, AV_LOG_ERROR, "invalid/unsupported bits per sample: %d\n",
avctx->bits_per_raw_sample);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
...@@ -196,52 +185,52 @@ static av_cold int tak_decode_init(AVCodecContext *avctx) ...@@ -196,52 +185,52 @@ static av_cold int tak_decode_init(AVCodecContext *avctx)
static void decode_lpc(int32_t *coeffs, int mode, int length) static void decode_lpc(int32_t *coeffs, int mode, int length)
{ {
int i, a1, a2, a3, a4, a5; int i;
if (length < 2) if (length < 2)
return; return;
if (mode == 1) { if (mode == 1) {
a1 = *coeffs++; int a1 = *coeffs++;
for (i = 0; i < (length - 1 >> 1); i++) { for (i = 0; i < length - 1 >> 1; i++) {
*coeffs += a1; *coeffs += a1;
coeffs[1] += *coeffs; coeffs[1] += *coeffs;
a1 = coeffs[1]; a1 = coeffs[1];
coeffs += 2; coeffs += 2;
} }
if ((length - 1) & 1) if (length - 1 & 1)
*coeffs += a1; *coeffs += a1;
} else if (mode == 2) { } else if (mode == 2) {
a1 = coeffs[1]; int a1 = coeffs[1];
a2 = a1 + *coeffs; int a2 = a1 + *coeffs;
coeffs[1] = a2; coeffs[1] = a2;
if (length > 2) { if (length > 2) {
coeffs += 2; coeffs += 2;
for (i = 0; i < (length - 2 >> 1); i++) { for (i = 0; i < length - 2 >> 1; i++) {
a3 = *coeffs + a1; int a3 = *coeffs + a1;
a4 = a3 + a2; int a4 = a3 + a2;
*coeffs = a4; *coeffs = a4;
a1 = coeffs[1] + a3; a1 = coeffs[1] + a3;
a2 = a1 + a4; a2 = a1 + a4;
coeffs[1] = a2; coeffs[1] = a2;
coeffs += 2; coeffs += 2;
} }
if (length & 1) if (length & 1)
*coeffs += a1 + a2; *coeffs += a1 + a2;
} }
} else if (mode == 3) { } else if (mode == 3) {
a1 = coeffs[1]; int a1 = coeffs[1];
a2 = a1 + *coeffs; int a2 = a1 + *coeffs;
coeffs[1] = a2; coeffs[1] = a2;
if (length > 2) { if (length > 2) {
a3 = coeffs[2]; int a3 = coeffs[2];
a4 = a3 + a1; int a4 = a3 + a1;
a5 = a4 + a2; int a5 = a4 + a2;
coeffs += 3; coeffs += 3;
for (i = 0; i < length - 3; i++) { for (i = 0; i < length - 3; i++) {
a3 += *coeffs; a3 += *coeffs;
a4 += a3; a4 += a3;
a5 += a4; a5 += a4;
*coeffs = a5; *coeffs = a5;
coeffs++; coeffs++;
} }
...@@ -249,140 +238,123 @@ static void decode_lpc(int32_t *coeffs, int mode, int length) ...@@ -249,140 +238,123 @@ static void decode_lpc(int32_t *coeffs, int mode, int length)
} }
} }
static int decode_segment(TAKDecContext *s, int8_t value, int32_t *dst, int len) static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len)
{ {
struct CParam code;
GetBitContext *gb = &s->gb; GetBitContext *gb = &s->gb;
int i;
if (!value) { if (!mode) {
memset(dst, 0, len * 4); memset(decoded, 0, len * sizeof(*decoded));
} else { return 0;
int x, y, z, i = 0; }
value--;
do {
while (1) {
x = get_bits_long(gb, xcodes[value].init);
if (x >= xcodes[value].escape)
break;
dst[i++] = (x >> 1) ^ -(x & 1);
if (i >= len)
return 0;
}
y = get_bits1(gb);
x = (y << xcodes[value].init) | x;
if (x >= xcodes[value].aescape) {
int c = get_unary(gb, 1, 9);
if (c == 9) {
int d;
z = x + xcodes[value].bias; if (mode > FF_ARRAY_ELEMS(xcodes))
d = get_bits(gb, 3); return AVERROR_INVALIDDATA;
if (d == 7) { code = xcodes[mode - 1];
d = get_bits(gb, 5) + 7;
if (d > 29) for (i = 0; i < len; i++) {
return AVERROR_INVALIDDATA; 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;
} }
if (d) x += code.bias;
z += xcodes[value].scale * (get_bits_long(gb, d) + 1); } else
} else { x += code.scale * scale - code.escape;
z = xcodes[value].scale * c + x - xcodes[value].escape; } else
} x -= code.escape;
} else { }
z = x - (xcodes[value].escape & -y); decoded[i] = (x >> 1) ^ -(x & 1);
}
dst[i++] = (z >> 1) ^ -(z & 1);
} while (i < len);
} }
return 0; return 0;
} }
static int xget(TAKDecContext *s, int d, int q) static int decode_residues(TAKDecContext *s, int32_t *decoded, int length)
{
int x;
x = d / q;
s->rval = d - (x * q);
if (s->rval < q / 2) {
s->rval += q;
} else {
x++;
}
if (x <= 1 || x > 128)
return -1;
return x;
}
static int get_len(TAKDecContext *s, int b)
{
if (b >= s->wlength - 1)
return s->rval;
else
return s->uval;
}
static int decode_coeffs(TAKDecContext *s, int32_t *dst, int length)
{ {
GetBitContext *gb = &s->gb; GetBitContext *gb = &s->gb;
int i, v, ret; int i, mode, ret;
if (length > s->nb_samples) if (length > s->nb_samples)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
if (get_bits1(gb)) { if (get_bits1(gb)) {
if ((s->wlength = xget(s, length, s->uval)) < 0) int wlength, rval;
return AVERROR_INVALIDDATA;
s->coding_mode[0] = v = get_bits(gb, 6); wlength = length / s->uval;
if (s->coding_mode[0] > FF_ARRAY_ELEMS(xcodes))
rval = length - (wlength * s->uval);
if (rval < s->uval / 2)
rval += s->uval;
else
wlength++;
if (wlength <= 1 || wlength > 128)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
for (i = 1; i < s->wlength; i++) { s->coding_mode[0] = mode = get_bits(gb, 6);
int c = get_unary(gb, 1, 6);
if (c > 5) { for (i = 1; i < wlength; i++) {
v = get_bits(gb, 6); int c = get_unary(gb, 1, 6);
} else if (c > 2) {
int t = get_bits1(gb);
v += (-t ^ (c - 1)) + t; switch (c) {
} else { case 6:
v += (-(c & 1) ^ (((c & 1) + c) >> 1)) + (c & 1); 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;
} }
case 2:
if (v > FF_ARRAY_ELEMS(xcodes)) mode++;
return AVERROR_INVALIDDATA; break;
s->coding_mode[i] = v; case 1:
mode--;
break;
}
s->coding_mode[i] = mode;
} }
i = 0; i = 0;
while (i < s->wlength) { while (i < wlength) {
int len = 0; int len = 0;
v = s->coding_mode[i]; mode = s->coding_mode[i];
do { do {
len += get_len(s, i); if (i >= wlength - 1)
len += rval;
else
len += s->uval;
i++; i++;
if (i == s->wlength) if (i == wlength)
break; break;
} while (v == s->coding_mode[i]); } while (s->coding_mode[i] == mode);
if ((ret = decode_segment(s, v, dst, len)) < 0) if ((ret = decode_segment(s, mode, decoded, len)) < 0)
return ret; return ret;
dst += len; decoded += len;
} }
} else { } else {
v = get_bits(gb, 6); mode = get_bits(gb, 6);
if (v > FF_ARRAY_ELEMS(xcodes)) if ((ret = decode_segment(s, mode, decoded, length)) < 0)
return AVERROR_INVALIDDATA;
if ((ret = decode_segment(s, v, dst, length)) < 0)
return ret; return ret;
} }
...@@ -397,160 +369,157 @@ static int get_bits_esc4(GetBitContext *gb) ...@@ -397,160 +369,157 @@ static int get_bits_esc4(GetBitContext *gb)
return 0; return 0;
} }
static int decode_subframe(TAKDecContext *s, int32_t *ptr, int subframe_size, static int decode_subframe(TAKDecContext *s, int32_t *decoded,
int prev_subframe_size) int subframe_size, int prev_subframe_size)
{ {
GetBitContext *gb = &s->gb; GetBitContext *gb = &s->gb;
int tmp, x, y, i, j, ret = 0; int tmp, x, y, i, j, ret = 0;
int dshift, size, filter_quant, filter_order;
int tfilter[MAX_PREDICTORS]; int tfilter[MAX_PREDICTORS];
if (get_bits1(gb)) { if (!get_bits1(gb))
s->filter_order = predictor_sizes[get_bits(gb, 4)]; return decode_residues(s, decoded, subframe_size);
if (prev_subframe_size > 0 && get_bits1(gb)) {
if (s->filter_order > prev_subframe_size)
return AVERROR_INVALIDDATA;
ptr -= s->filter_order; filter_order = predictor_sizes[get_bits(gb, 4)];
subframe_size += s->filter_order;
if (s->filter_order > subframe_size) if (prev_subframe_size > 0 && get_bits1(gb)) {
return AVERROR_INVALIDDATA; if (filter_order > prev_subframe_size)
} else { return AVERROR_INVALIDDATA;
int lpc;
if (s->filter_order > subframe_size) decoded -= filter_order;
return AVERROR_INVALIDDATA; subframe_size += filter_order;
lpc = get_bits(gb, 2); if (filter_order > subframe_size)
if (lpc > 2) return AVERROR_INVALIDDATA;
return AVERROR_INVALIDDATA; } else {
int lpc_mode;
if ((ret = decode_coeffs(s, ptr, s->filter_order)) < 0) if (filter_order > subframe_size)
return ret; return AVERROR_INVALIDDATA;
decode_lpc(ptr, lpc, s->filter_order); lpc_mode = get_bits(gb, 2);
} if (lpc_mode > 2)
return AVERROR_INVALIDDATA;
s->xred = get_bits_esc4(gb); if ((ret = decode_residues(s, decoded, filter_order)) < 0)
s->size = get_bits1(gb) + 5; return ret;
if (get_bits1(gb)) { if (lpc_mode)
s->ared = get_bits(gb, 3) + 1; decode_lpc(decoded, lpc_mode, filter_order);
if (s->ared > 7) }
return AVERROR_INVALIDDATA;
} else {
s->ared = 0;
}
s->predictors[0] = get_sbits(gb, 10);
s->predictors[1] = get_sbits(gb, 10);
s->predictors[2] = get_sbits(gb, s->size + 1) << (9 - s->size);
s->predictors[3] = get_sbits(gb, s->size + 1) << (9 - s->size);
if (s->filter_order > 4) {
tmp = s->size + 1 - get_bits1(gb);
for (i = 4; i < s->filter_order; i++) {
if (!(i & 3))
x = tmp - get_bits(gb, 2);
s->predictors[i] = get_sbits(gb, x) << (9 - s->size);
}
}
tfilter[0] = s->predictors[0] << 6; dshift = get_bits_esc4(gb);
for (i = 1; i < s->filter_order; i++) { size = get_bits1(gb) + 6;
int32_t *p1 = &tfilter[0];
int32_t *p2 = &tfilter[i - 1];
for (j = 0; j < (i + 1) / 2; j++) { filter_quant = 10;
x = *p1 + (s->predictors[i] * *p2 + 256 >> 9); if (get_bits1(gb)) {
*p2 += s->predictors[i] * *p1 + 256 >> 9; filter_quant -= get_bits(gb, 3) + 1;
*p1++ = x; if (filter_quant < 3)
p2--; return AVERROR_INVALIDDATA;
} }
tfilter[i] = s->predictors[i] << 6; 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);
} }
}
x = -1 << (32 - (s->ared + 5)); tfilter[0] = s->predictors[0] << 6;
y = 1 << ((s->ared + 5) - 1); for (i = 1; i < filter_order; i++) {
for (i = 0, j = s->filter_order - 1; i < s->filter_order / 2; i++, j--) { int32_t *p1 = &tfilter[0];
tmp = y + tfilter[j]; int32_t *p2 = &tfilter[i - 1];
s->filter[j] = -(x & -(y + tfilter[i] >> 31) |
(y + tfilter[i]) >> (s->ared + 5));
s->filter[i] = -(x & -(tmp >> 31) | (tmp >> s->ared + 5));
}
if ((ret = decode_coeffs(s, &ptr[s->filter_order], for (j = 0; j < (i + 1) / 2; j++) {
subframe_size - s->filter_order)) < 0) x = *p1 + (s->predictors[i] * *p2 + 256 >> 9);
return ret; *p2 += s->predictors[i] * *p1 + 256 >> 9;
*p1++ = x;
p2--;
}
for (i = 0; i < s->filter_order; i++) tfilter[i] = s->predictors[i] << 6;
s->residues[i] = *ptr++ >> s->xred; }
y = FF_ARRAY_ELEMS(s->residues) - s->filter_order; x = 1 << (32 - (15 - filter_quant));
x = subframe_size - s->filter_order; y = 1 << ((15 - filter_quant) - 1);
while (x > 0) { for (i = 0, j = filter_order - 1; i < filter_order / 2; i++, j--) {
tmp = FFMIN(y, x); tmp = y + tfilter[j];
s->filter[j] = x - ((tfilter[i] + y) >> (15 - filter_quant));
s->filter[i] = x - ((tfilter[j] + y) >> (15 - filter_quant));
}
for (i = 0; i < tmp; i++) { if ((ret = decode_residues(s, &decoded[filter_order],
int v, w, m; subframe_size - filter_order)) < 0)
return ret;
v = 1 << (10 - s->ared - 1); for (i = 0; i < filter_order; i++)
if (!(s->filter_order & 15)) { s->residues[i] = *decoded++ >> dshift;
v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
s->filter_order); y = FF_ARRAY_ELEMS(s->residues) - filter_order;
} else if (s->filter_order & 4) { x = subframe_size - filter_order;
for (j = 0; j < s->filter_order; j += 4) { while (x > 0) {
v += s->residues[i + j + 3] * s->filter[j + 3] + tmp = FFMIN(y, x);
s->residues[i + j + 2] * s->filter[j + 2] +
s->residues[i + j + 1] * s->filter[j + 1] + for (i = 0; i < tmp; i++) {
s->residues[i + j ] * s->filter[j ]; int v = 1 << (filter_quant - 1);
}
} else { if (!(filter_order & 15)) {
for (j = 0; j < s->filter_order; j += 8) { v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
v += s->residues[i + j + 7] * s->filter[j + 7] + filter_order);
s->residues[i + j + 6] * s->filter[j + 6] + } else if (filter_order & 4) {
s->residues[i + j + 5] * s->filter[j + 5] + for (j = 0; j < filter_order; j += 4) {
s->residues[i + j + 4] * s->filter[j + 4] + v += s->residues[i + j + 3] * s->filter[j + 3] +
s->residues[i + j + 3] * s->filter[j + 3] + s->residues[i + j + 2] * s->filter[j + 2] +
s->residues[i + j + 2] * s->filter[j + 2] + s->residues[i + j + 1] * s->filter[j + 1] +
s->residues[i + j + 1] * s->filter[j + 1] + s->residues[i + j ] * s->filter[j ];
s->residues[i + j ] * s->filter[j ]; }
} } else {
for (j = 0; j < filter_order; j += 8) {
v += s->residues[i + j + 7] * s->filter[j + 7] +
s->residues[i + j + 6] * s->filter[j + 6] +
s->residues[i + j + 5] * s->filter[j + 5] +
s->residues[i + j + 4] * s->filter[j + 4] +
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 ];
} }
m = (-1 << (32 - (10 - s->ared))) & -(v >> 31) | (v >> 10 - s->ared);
m = av_clip(m, -8192, 8191);
w = (m << s->xred) - *ptr;
*ptr++ = w;
s->residues[s->filter_order + i] = w >> s->xred;
} }
v = (av_clip(v >> filter_quant, -8192, 8191) << dshift) - *decoded;
x -= tmp; *decoded++ = v;
if (x > 0) s->residues[filter_order + i] = v >> dshift;
memcpy(s->residues, &s->residues[y], 2 * s->filter_order);
} }
emms_c(); x -= tmp;
} else { if (x > 0)
ret = decode_coeffs(s, ptr, subframe_size); memcpy(s->residues, &s->residues[y], 2 * filter_order);
} }
return ret; emms_c();
return 0;
} }
static int decode_channel(TAKDecContext *s, int chan) static int decode_channel(TAKDecContext *s, int chan)
{ {
AVCodecContext *avctx = s->avctx; AVCodecContext *avctx = s->avctx;
GetBitContext *gb = &s->gb; GetBitContext *gb = &s->gb;
int32_t *dst = s->decoded[chan]; int32_t *decoded = s->decoded[chan];
int left = s->nb_samples - 1;
int i = 0, ret, prev = 0; int i = 0, ret, prev = 0;
int left = s->nb_samples - 1;
s->sample_shift[chan] = get_bits_esc4(gb); s->sample_shift[chan] = get_bits_esc4(gb);
if (s->sample_shift[chan] >= avctx->bits_per_raw_sample) if (s->sample_shift[chan] >= avctx->bits_per_raw_sample)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
*dst++ = get_sbits(gb, avctx->bits_per_raw_sample - s->sample_shift[chan]); *decoded++ = get_sbits(gb, avctx->bits_per_raw_sample - s->sample_shift[chan]);
s->lpc_mode[chan] = get_bits(gb, 2); s->lpc_mode[chan] = get_bits(gb, 2);
s->nb_subframes = get_bits(gb, 3) + 1; s->nb_subframes = get_bits(gb, 3) + 1;
...@@ -572,14 +541,14 @@ static int decode_channel(TAKDecContext *s, int chan) ...@@ -572,14 +541,14 @@ static int decode_channel(TAKDecContext *s, int chan)
if (left <= 0) if (left <= 0)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
s->subframe_len[i] = left; s->subframe_len[i] = left;
prev = 0; prev = 0;
for (i = 0; i < s->nb_subframes; i++) { for (i = 0; i < s->nb_subframes; i++) {
if ((ret = decode_subframe(s, dst, s->subframe_len[i], prev)) < 0) if ((ret = decode_subframe(s, decoded, s->subframe_len[i], prev)) < 0)
return ret; return ret;
dst += s->subframe_len[i]; decoded += s->subframe_len[i];
prev = s->subframe_len[i]; prev = s->subframe_len[i];
} }
return 0; return 0;
...@@ -587,102 +556,107 @@ static int decode_channel(TAKDecContext *s, int chan) ...@@ -587,102 +556,107 @@ static int decode_channel(TAKDecContext *s, int chan)
static int decorrelate(TAKDecContext *s, int c1, int c2, int length) static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
{ {
GetBitContext *gb = &s->gb; GetBitContext *gb = &s->gb;
uint32_t *p1 = s->decoded[c1] + 1; int32_t *p1 = s->decoded[c1] + 1;
uint32_t *p2 = s->decoded[c2] + 1; int32_t *p2 = s->decoded[c2] + 1;
int a, b, i, x, tmp; int i;
int dshift, dfactor;
if (s->dmode > 3) {
s->dshift = get_bits_esc4(gb);
if (s->dmode > 5) {
if (get_bits1(gb))
s->filter_order = 16;
else
s->filter_order = 8;
s->dval1 = get_bits1(gb);
s->dval2 = get_bits1(gb);
for (i = 0; i < s->filter_order; i++) {
if (!(i & 3))
x = 14 - get_bits(gb, 3);
s->filter[i] = get_sbits(gb, x);
}
} else {
s->dfactor = get_sbits(gb, 10);
}
}
switch (s->dmode) { switch (s->dmode) {
case 1: case 1: /* left/side */
for (i = 0; i < length; i++, p1++, p2++) for (i = 0; i < length; i++) {
*p2 += *p1; int32_t a = p1[i];
int32_t b = p2[i];
p2[i] = a + b;
}
break; break;
case 2: case 2: /* side/right */
for (i = 0; i < length; i++, p1++, p2++) for (i = 0; i < length; i++) {
*p1 = *p2 - *p1; int32_t a = p1[i];
int32_t b = p2[i];
p1[i] = b - a;
}
break; break;
case 3: case 3: /* side/mid */
for (i = 0; i < length; i++, p1++, p2++) { for (i = 0; i < length; i++) {
x = (*p2 & 1) + 2 * *p1; int32_t a = p1[i];
a = -*p2 + x; int32_t b = p2[i];
b = *p2 + x; a -= b >> 1;
*p1 = a & 0x80000000 | (a >> 1); p1[i] = a;
*p2 = b & 0x80000000 | (b >> 1); p2[i] = a + b;
} }
break; break;
case 4: case 4: /* side/left with scale factor */
FFSWAP(uint32_t *, p1, p2); FFSWAP(int32_t*, p1, p2);
case 5: case 5: /* side/right with scale factor */
if (s->dshift) dshift = get_bits_esc4(gb);
tmp = -1 << (32 - s->dshift); dfactor = get_sbits(gb, 10);
else for (i = 0; i < length; i++) {
tmp = 0; int32_t a = p1[i];
int32_t b = p2[i];
for (i = 0; i < length; i++, p1++, p2++) { b = dfactor * (b >> dshift) + 128 >> 8 << dshift;
x = s->dfactor * (tmp & -(*p2 >> 31) | (*p2 >> s->dshift)) + 128; p1[i] = b - a;
*p1 = ((-(x >> 31) & 0xFF000000 | (x >> 8)) << s->dshift) - *p1;
} }
break; break;
case 6: case 6:
FFSWAP(uint32_t *, p1, p2); FFSWAP(int32_t*, p1, p2);
case 7: case 7: {
int length2, order_half, filter_order, dval1, dval2;
int tmp, x, code_size;
if (length < 256) if (length < 256)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
a = s->filter_order / 2; dshift = get_bits_esc4(gb);
b = length - (s->filter_order - 1); 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);
if (s->dval1) { /* decorrelate beginning samples */
for (i = 0; i < a; i++) if (dval1) {
p1[i] += p2[i]; for (i = 0; i < order_half; i++) {
int32_t a = p1[i];
int32_t b = p2[i];
p1[i] = a + b;
}
} }
if (s->dval2) { /* decorrelate ending samples */
x = a + b; if (dval2) {
for (i = 0; i < length - x; i++) for (i = length2 + order_half; i < length; i++) {
p1[x + i] += p2[x + i]; int32_t a = p1[i];
int32_t b = p2[i];
p1[i] = a + b;
}
} }
for (i = 0; i < s->filter_order; i++)
s->residues[i] = *p2++ >> s->dshift;
p1 += a; for (i = 0; i < filter_order; i++)
x = FF_ARRAY_ELEMS(s->residues) - s->filter_order; s->residues[i] = *p2++ >> dshift;
for (; b > 0; b -= tmp) {
tmp = FFMIN(b, x); p1 += order_half;
x = FF_ARRAY_ELEMS(s->residues) - filter_order;
for (; length2 > 0; length2 -= tmp) {
tmp = FFMIN(length2, x);
for (i = 0; i < tmp; i++) for (i = 0; i < tmp; i++)
s->residues[s->filter_order + i] = *p2++ >> s->dshift; s->residues[filter_order + i] = *p2++ >> dshift;
for (i = 0; i < tmp; i++) { for (i = 0; i < tmp; i++) {
int v, w, m; int v = 1 << 9;
v = 1 << 9;
if (s->filter_order == 16) { if (filter_order == 16) {
v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter, v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
s->filter_order); filter_order);
} else { } else {
v += s->residues[i + 7] * s->filter[7] + v += s->residues[i + 7] * s->filter[7] +
s->residues[i + 6] * s->filter[6] + s->residues[i + 6] * s->filter[6] +
...@@ -694,18 +668,16 @@ static int decorrelate(TAKDecContext *s, int c1, int c2, int length) ...@@ -694,18 +668,16 @@ static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
s->residues[i ] * s->filter[0]; s->residues[i ] * s->filter[0];
} }
m = (-1 << 22) & -(v >> 31) | (v >> 10); *p1++ = (av_clip(v >> 10, -8192, 8191) << dshift) - *p1;
m = av_clip(m, -8192, 8191);
w = (m << s->dshift) - *p1;
*p1++ = w;
} }
memcpy(s->residues, &s->residues[tmp], 2 * s->filter_order); memcpy(s->residues, &s->residues[tmp], 2 * filter_order);
} }
emms_c(); emms_c();
break; break;
} }
}
return 0; return 0;
} }
...@@ -713,10 +685,9 @@ static int decorrelate(TAKDecContext *s, int c1, int c2, int length) ...@@ -713,10 +685,9 @@ static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
static int tak_decode_frame(AVCodecContext *avctx, void *data, static int tak_decode_frame(AVCodecContext *avctx, void *data,
int *got_frame_ptr, AVPacket *pkt) int *got_frame_ptr, AVPacket *pkt)
{ {
TAKDecContext *s = avctx->priv_data; TAKDecContext *s = avctx->priv_data;
GetBitContext *gb = &s->gb; GetBitContext *gb = &s->gb;
int chan, i, ret, hsize; int chan, i, ret, hsize;
int32_t *p;
if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES) if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
...@@ -734,20 +705,24 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data, ...@@ -734,20 +705,24 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
} }
} }
if (s->ti.codec != 2 && s->ti.codec != 4) { if (s->ti.codec != TAK_CODEC_MONO_STEREO &&
s->ti.codec != TAK_CODEC_MULTICHANNEL) {
av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec); av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec);
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
if (s->ti.data_type) { if (s->ti.data_type) {
av_log(avctx, AV_LOG_ERROR, "unsupported data type: %d\n", s->ti.data_type); av_log(avctx, AV_LOG_ERROR,
"unsupported data type: %d\n", s->ti.data_type);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
if (s->ti.codec == 2 && s->ti.channels > 2) { 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); av_log(avctx, AV_LOG_ERROR,
"invalid number of channels: %d\n", s->ti.channels);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
if (s->ti.channels > 6) { if (s->ti.channels > 6) {
av_log(avctx, AV_LOG_ERROR, "unsupported number of channels: %d\n", s->ti.channels); av_log(avctx, AV_LOG_ERROR,
"unsupported number of channels: %d\n", s->ti.channels);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
...@@ -769,8 +744,8 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data, ...@@ -769,8 +744,8 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
avctx->channel_layout = s->ti.ch_layout; avctx->channel_layout = s->ti.ch_layout;
avctx->channels = s->ti.channels; avctx->channels = s->ti.channels;
s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples : s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples
s->ti.frame_samples; : s->ti.frame_samples;
s->frame.nb_samples = s->nb_samples; s->frame.nb_samples = s->nb_samples;
if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) if ((ret = ff_get_buffer(avctx, &s->frame)) < 0)
...@@ -790,32 +765,32 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data, ...@@ -790,32 +765,32 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
return ret; return ret;
} else { } else {
for (chan = 0; chan < avctx->channels; chan++) for (chan = 0; chan < avctx->channels; chan++)
s->decoded[chan] = (int32_t *)s->frame.data[chan]; s->decoded[chan] = (int32_t *)s->frame.extended_data[chan];
} }
if (s->nb_samples < 16) { if (s->nb_samples < 16) {
for (chan = 0; chan < avctx->channels; chan++) { for (chan = 0; chan < avctx->channels; chan++) {
p = s->decoded[chan]; int32_t *decoded = s->decoded[chan];
for (i = 0; i < s->nb_samples; i++) for (i = 0; i < s->nb_samples; i++)
*p++ = get_sbits(gb, avctx->bits_per_raw_sample); decoded[i] = get_sbits(gb, avctx->bits_per_raw_sample);
} }
} else { } else {
if (s->ti.codec == 2) { if (s->ti.codec == TAK_CODEC_MONO_STEREO) {
for (chan = 0; chan < avctx->channels; chan++) { for (chan = 0; chan < avctx->channels; chan++)
if (ret = decode_channel(s, chan)) if (ret = decode_channel(s, chan))
return ret; return ret;
}
if (avctx->channels == 2) { if (avctx->channels == 2) {
s->nb_subframes = get_bits(gb, 1) + 1; s->nb_subframes = get_bits(gb, 1) + 1;
if (s->nb_subframes > 1) if (s->nb_subframes > 1) {
s->subframe_len[1] = get_bits(gb, 6); s->subframe_len[1] = get_bits(gb, 6);
}
s->dmode = get_bits(gb, 3); s->dmode = get_bits(gb, 3);
if (ret = decorrelate(s, 0, 1, s->nb_samples - 1)) if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))
return ret; return ret;
} }
} else if (s->ti.codec == 4) { } else if (s->ti.codec == TAK_CODEC_MULTICHANNEL) {
if (get_bits1(gb)) { if (get_bits1(gb)) {
int ch_mask = 0; int ch_mask = 0;
...@@ -859,32 +834,33 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data, ...@@ -859,32 +834,33 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
} }
for (i = 0; i < chan; i++) { for (i = 0; i < chan; i++) {
if (s->mcdparams[i].present && s->mcdparams[i].index == 1) { if (s->mcdparams[i].present && s->mcdparams[i].index == 1)
if (ret = decode_channel(s, s->mcdparams[i].chan2)) if (ret = decode_channel(s, s->mcdparams[i].chan2))
return ret; return ret;
}
if (ret = decode_channel(s, s->mcdparams[i].chan1)) if (ret = decode_channel(s, s->mcdparams[i].chan1))
return ret; return ret;
if (s->mcdparams[i].present) { if (s->mcdparams[i].present) {
s->dmode = mc_dmodes[s->mcdparams[i].index]; s->dmode = mc_dmodes[s->mcdparams[i].index];
if (ret = decorrelate(s, s->mcdparams[i].chan2, if (ret = decorrelate(s,
s->mcdparams[i].chan1, s->mcdparams[i].chan2,
s->nb_samples - 1)) s->mcdparams[i].chan1,
s->nb_samples - 1))
return ret; return ret;
} }
} }
} }
for (chan = 0; chan < avctx->channels; chan++) { for (chan = 0; chan < avctx->channels; chan++) {
p = s->decoded[chan]; int32_t *decoded = s->decoded[chan];
decode_lpc(p, s->lpc_mode[chan], s->nb_samples);
if (s->lpc_mode[chan])
decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples);
if (s->sample_shift[chan] > 0) { if (s->sample_shift[chan] > 0)
for (i = 0; i < s->nb_samples; i++) for (i = 0; i < s->nb_samples; i++)
*p++ <<= s->sample_shift[chan]; decoded[i] <<= s->sample_shift[chan];
}
} }
} }
...@@ -903,29 +879,29 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data, ...@@ -903,29 +879,29 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
} }
} }
// convert to output buffer /* convert to output buffer */
switch (avctx->bits_per_raw_sample) { switch (avctx->sample_fmt) {
case 8: case AV_SAMPLE_FMT_U8P:
for (chan = 0; chan < avctx->channels; chan++) { for (chan = 0; chan < avctx->channels; chan++) {
uint8_t *samples = (uint8_t *)s->frame.data[chan]; uint8_t *samples = (uint8_t *)s->frame.extended_data[chan];
p = s->decoded[chan]; int32_t *decoded = s->decoded[chan];
for (i = 0; i < s->nb_samples; i++, p++) for (i = 0; i < s->nb_samples; i++)
*samples++ = *p + 0x80; samples[i] = decoded[i] + 0x80;
} }
break; break;
case 16: case AV_SAMPLE_FMT_S16P:
for (chan = 0; chan < avctx->channels; chan++) { for (chan = 0; chan < avctx->channels; chan++) {
int16_t *samples = (int16_t *)s->frame.data[chan]; int16_t *samples = (int16_t *)s->frame.extended_data[chan];
p = s->decoded[chan]; int32_t *decoded = s->decoded[chan];
for (i = 0; i < s->nb_samples; i++, p++) for (i = 0; i < s->nb_samples; i++)
*samples++ = *p; samples[i] = decoded[i];
} }
break; break;
case 24: case AV_SAMPLE_FMT_S32P:
for (chan = 0; chan < avctx->channels; chan++) { for (chan = 0; chan < avctx->channels; chan++) {
int32_t *samples = (int32_t *)s->frame.data[chan]; int32_t *samples = (int32_t *)s->frame.extended_data[chan];
for (i = 0; i < s->nb_samples; i++) for (i = 0; i < s->nb_samples; i++)
*samples++ <<= 8; samples[i] <<= 8;
} }
break; break;
} }
...@@ -946,17 +922,17 @@ static av_cold int tak_decode_close(AVCodecContext *avctx) ...@@ -946,17 +922,17 @@ static av_cold int tak_decode_close(AVCodecContext *avctx)
} }
AVCodec ff_tak_decoder = { AVCodec ff_tak_decoder = {
.name = "tak", .name = "tak",
.type = AVMEDIA_TYPE_AUDIO, .type = AVMEDIA_TYPE_AUDIO,
.id = AV_CODEC_ID_TAK, .id = AV_CODEC_ID_TAK,
.priv_data_size = sizeof(TAKDecContext), .priv_data_size = sizeof(TAKDecContext),
.init = tak_decode_init, .init = tak_decode_init,
.close = tak_decode_close, .close = tak_decode_close,
.decode = tak_decode_frame, .decode = tak_decode_frame,
.capabilities = CODEC_CAP_DR1, .capabilities = CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"), .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P, .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S16P,
AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_S32P,
AV_SAMPLE_FMT_NONE }, AV_SAMPLE_FMT_NONE },
}; };
...@@ -26,8 +26,8 @@ ...@@ -26,8 +26,8 @@
#include "apetag.h" #include "apetag.h"
typedef struct TAKDemuxContext { typedef struct TAKDemuxContext {
int mlast_frame; int mlast_frame;
int64_t data_end; int64_t data_end;
} TAKDemuxContext; } TAKDemuxContext;
static int tak_probe(AVProbeData *p) static int tak_probe(AVProbeData *p)
...@@ -40,7 +40,7 @@ static int tak_probe(AVProbeData *p) ...@@ -40,7 +40,7 @@ static int tak_probe(AVProbeData *p)
static int tak_read_header(AVFormatContext *s) static int tak_read_header(AVFormatContext *s)
{ {
TAKDemuxContext *tc = s->priv_data; TAKDemuxContext *tc = s->priv_data;
AVIOContext *pb = s->pb; AVIOContext *pb = s->pb;
GetBitContext gb; GetBitContext gb;
AVStream *st; AVStream *st;
uint8_t *buffer = NULL; uint8_t *buffer = NULL;
...@@ -95,7 +95,7 @@ static int tak_read_header(AVFormatContext *s) ...@@ -95,7 +95,7 @@ static int tak_read_header(AVFormatContext *s)
av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]); av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
av_log(s, AV_LOG_VERBOSE, "\n"); av_log(s, AV_LOG_VERBOSE, "\n");
break; break;
} }
case TAK_METADATA_END: { case TAK_METADATA_END: {
int64_t curpos = avio_tell(pb); int64_t curpos = avio_tell(pb);
...@@ -106,8 +106,7 @@ static int tak_read_header(AVFormatContext *s) ...@@ -106,8 +106,7 @@ static int tak_read_header(AVFormatContext *s)
tc->data_end += curpos; tc->data_end += curpos;
return 0; return 0;
break; }
}
default: default:
ret = avio_skip(pb, size); ret = avio_skip(pb, size);
if (ret < 0) if (ret < 0)
...@@ -123,19 +122,19 @@ static int tak_read_header(AVFormatContext *s) ...@@ -123,19 +122,19 @@ static int tak_read_header(AVFormatContext *s)
st->codec->bits_per_coded_sample = ti.bps; st->codec->bits_per_coded_sample = ti.bps;
if (ti.ch_layout) if (ti.ch_layout)
st->codec->channel_layout = ti.ch_layout; st->codec->channel_layout = ti.ch_layout;
st->codec->sample_rate = ti.sample_rate; st->codec->sample_rate = ti.sample_rate;
st->codec->channels = ti.channels; st->codec->channels = ti.channels;
st->start_time = 0; st->start_time = 0;
avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
st->codec->extradata = buffer; st->codec->extradata = buffer;
st->codec->extradata_size = size; st->codec->extradata_size = size;
buffer = NULL; buffer = NULL;
} else if (type == TAK_METADATA_LAST_FRAME) { } else if (type == TAK_METADATA_LAST_FRAME) {
if (size != 11) if (size != 11)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
tc->mlast_frame = 1; tc->mlast_frame = 1;
tc->data_end = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) + tc->data_end = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) +
get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS); get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
av_freep(&buffer); av_freep(&buffer);
} else if (type == TAK_METADATA_ENCODER) { } else if (type == TAK_METADATA_ENCODER) {
av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n", av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment