X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Ftta.c;h=b463946724ada35b97702132d1355d53cdbf3f5d;hb=a8cedbebf163ad376abc4703b3156c44d0858404;hp=3f5ab23b1ece38ba7f089cd04b1d742c184be9d6;hpb=cd1c12b5c5b79195140a93d59cbf990d034f61d8;p=ffmpeg diff --git a/libavcodec/tta.c b/libavcodec/tta.c index 3f5ab23b1ec..b463946724a 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -32,13 +32,14 @@ #include #include "avcodec.h" #include "get_bits.h" +#include "libavutil/crc.h" #define FORMAT_SIMPLE 1 #define FORMAT_ENCRYPTED 2 #define MAX_ORDER 16 typedef struct TTAFilter { - int32_t shift, round, error, mode; + int32_t shift, round, error; int32_t qm[MAX_ORDER]; int32_t dx[MAX_ORDER]; int32_t dl[MAX_ORDER]; @@ -58,6 +59,7 @@ typedef struct TTAContext { AVCodecContext *avctx; AVFrame frame; GetBitContext gb; + const AVCRC *crc_table; int format, channels, bps, data_length; int frame_length, last_frame_length, total_frames; @@ -82,19 +84,18 @@ static const uint32_t shift_1[] = { static const uint32_t * const shift_16 = shift_1 + 4; -static const int32_t ttafilter_configs[4][2] = { - {10, 1}, - {9, 1}, - {10, 1}, - {12, 0} +static const int32_t ttafilter_configs[4] = { + 10, + 9, + 10, + 12 }; -static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) { +static void ttafilter_init(TTAFilter *c, int32_t shift) { memset(c, 0, sizeof(TTAFilter)); c->shift = shift; c->round = shift_1[shift-1]; // c->round = 1 << (shift - 1); - c->mode = mode; } // FIXME: copy paste from original @@ -109,9 +110,8 @@ static inline void memshl(register int32_t *a, register int32_t *b) { *a = *b; } -// FIXME: copy paste from original -// mode=1 encoder, mode=0 decoder -static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) { +static inline void ttafilter_process(TTAFilter *c, int32_t *in) +{ register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round; if (!c->error) { @@ -149,22 +149,13 @@ static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) { *(dx-2) = ((*(dl-3) >> 30) | 1) << 1; *(dx-3) = ((*(dl-4) >> 30) | 1); - // compress - if (mode) { - *dl = *in; - *in -= (sum >> c->shift); - c->error = *in; - } else { - c->error = *in; - *in += (sum >> c->shift); - *dl = *in; - } + c->error = *in; + *in += (sum >> c->shift); + *dl = *in; - if (c->mode) { - *(dl-1) = *dl - *(dl-1); - *(dl-2) = *(dl-1) - *(dl-2); - *(dl-3) = *(dl-2) - *(dl-3); - } + *(dl-1) = *dl - *(dl-1); + *(dl-2) = *(dl-1) - *(dl-2); + *(dl-3) = *(dl-2) - *(dl-3); memshl(c->dl, c->dl + 1); memshl(c->dx, c->dx + 1); @@ -198,6 +189,20 @@ static const int64_t tta_channel_layouts[7] = { AV_CH_LAYOUT_7POINT1_WIDE }; +static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size) +{ + uint32_t crc, CRC; + + CRC = AV_RL32(buf + buf_size); + crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size); + if (CRC != (crc ^ 0xFFFFFFFFU)) { + av_log(s->avctx, AV_LOG_ERROR, "CRC error\n"); + return AVERROR_INVALIDDATA; + } + + return 0; +} + static av_cold int tta_decode_init(AVCodecContext * avctx) { TTAContext *s = avctx->priv_data; @@ -211,6 +216,11 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8); if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1")) { + if (avctx->err_recognition & AV_EF_CRCCHECK) { + s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE); + tta_check_crc(s, avctx->extradata, 18); + } + /* signature */ skip_bits_long(&s->gb, 32); @@ -274,6 +284,12 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) s->data_length, s->frame_length, s->last_frame_length, s->total_frames); // FIXME: seek table + if (get_bits_left(&s->gb) < 32 * s->total_frames + 32) + av_log(avctx, AV_LOG_WARNING, "Seek table missing or too small\n"); + else if (avctx->err_recognition & AV_EF_CRCCHECK) { + if (tta_check_crc(s, avctx->extradata + 22, s->total_frames * 4)) + return AVERROR_INVALIDDATA; + } skip_bits_long(&s->gb, 32 * s->total_frames); skip_bits_long(&s->gb, 32); // CRC32 of seektable @@ -314,6 +330,11 @@ static int tta_decode_frame(AVCodecContext *avctx, void *data, int cur_chan = 0, framelen = s->frame_length; int32_t *p; + if (avctx->err_recognition & AV_EF_CRCCHECK) { + if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4)) + return AVERROR_INVALIDDATA; + } + init_get_bits(&s->gb, buf, buf_size*8); // FIXME: seeking @@ -335,7 +356,7 @@ static int tta_decode_frame(AVCodecContext *avctx, void *data, // init per channel states for (i = 0; i < s->channels; i++) { s->ch_ctx[i].predictor = 0; - ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]); + ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1]); rice_init(&s->ch_ctx[i].rice, 10, 10); } @@ -389,19 +410,18 @@ static int tta_decode_frame(AVCodecContext *avctx, void *data, } // extract coded value -#define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1)) - *p = UNFOLD(value); + *p = 1 + ((value >> 1) ^ ((value & 1) - 1)); // run hybrid filter - ttafilter_process(filter, p, 0); + ttafilter_process(filter, p); // fixed order prediction #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k) switch (s->bps) { - case 1: *p += PRED(*predictor, 4); break; - case 2: - case 3: *p += PRED(*predictor, 5); break; - case 4: *p += *predictor; break; + case 1: *p += PRED(*predictor, 4); break; + case 2: + case 3: *p += PRED(*predictor, 5); break; + case 4: *p += *predictor; break; } *predictor = *p;