|
|
@ -90,9 +90,10 @@ typedef struct TM2Huff{ |
|
|
|
|
|
|
|
|
|
|
|
static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff) |
|
|
|
static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
int ret; |
|
|
|
if(length > huff->max_bits) { |
|
|
|
if(length > huff->max_bits) { |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n", huff->max_bits); |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n", huff->max_bits); |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(!get_bits1(&ctx->gb)) { /* literal */ |
|
|
|
if(!get_bits1(&ctx->gb)) { /* literal */ |
|
|
@ -101,7 +102,7 @@ static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff * |
|
|
|
} |
|
|
|
} |
|
|
|
if(huff->num >= huff->max_num) { |
|
|
|
if(huff->num >= huff->max_num) { |
|
|
|
av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n"); |
|
|
|
av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n"); |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits); |
|
|
|
huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits); |
|
|
|
huff->bits[huff->num] = prefix; |
|
|
|
huff->bits[huff->num] = prefix; |
|
|
@ -109,10 +110,10 @@ static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff * |
|
|
|
huff->num++; |
|
|
|
huff->num++; |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} else { /* non-terminal node */ |
|
|
|
} else { /* non-terminal node */ |
|
|
|
if(tm2_read_tree(ctx, prefix << 1, length + 1, huff) == -1) |
|
|
|
if ((ret = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0) |
|
|
|
return -1; |
|
|
|
return ret; |
|
|
|
if(tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff) == -1) |
|
|
|
if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0) |
|
|
|
return -1; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
@ -133,11 +134,11 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) |
|
|
|
(huff.max_bits < 0) || (huff.max_bits > 25)) { |
|
|
|
(huff.max_bits < 0) || (huff.max_bits > 25)) { |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n", |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n", |
|
|
|
huff.val_bits, huff.max_bits); |
|
|
|
huff.val_bits, huff.max_bits); |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
if((huff.nodes <= 0) || (huff.nodes > 0x10000)) { |
|
|
|
if((huff.nodes <= 0) || (huff.nodes > 0x10000)) { |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes); |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes); |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
/* one-node tree */ |
|
|
|
/* one-node tree */ |
|
|
|
if(huff.max_bits == 0) |
|
|
|
if(huff.max_bits == 0) |
|
|
@ -149,28 +150,24 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) |
|
|
|
huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t)); |
|
|
|
huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t)); |
|
|
|
huff.lens = av_mallocz(huff.max_num * sizeof(int)); |
|
|
|
huff.lens = av_mallocz(huff.max_num * sizeof(int)); |
|
|
|
|
|
|
|
|
|
|
|
if(tm2_read_tree(ctx, 0, 0, &huff) == -1) |
|
|
|
res = tm2_read_tree(ctx, 0, 0, &huff); |
|
|
|
res = -1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (huff.num != huff.max_num) { |
|
|
|
if (huff.num != huff.max_num) { |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n", |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n", |
|
|
|
huff.num, huff.max_num); |
|
|
|
huff.num, huff.max_num); |
|
|
|
res = -1; |
|
|
|
res = AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* convert codes to vlc_table */ |
|
|
|
/* convert codes to vlc_table */ |
|
|
|
if(res != -1) { |
|
|
|
if(res >= 0) { |
|
|
|
int i; |
|
|
|
int i; |
|
|
|
|
|
|
|
|
|
|
|
res = init_vlc(&code->vlc, huff.max_bits, huff.max_num, |
|
|
|
res = init_vlc(&code->vlc, huff.max_bits, huff.max_num, |
|
|
|
huff.lens, sizeof(int), sizeof(int), |
|
|
|
huff.lens, sizeof(int), sizeof(int), |
|
|
|
huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0); |
|
|
|
huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0); |
|
|
|
if(res < 0) { |
|
|
|
if(res < 0) |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); |
|
|
|
res = -1; |
|
|
|
else { |
|
|
|
} else |
|
|
|
|
|
|
|
res = 0; |
|
|
|
|
|
|
|
if(res != -1) { |
|
|
|
|
|
|
|
code->bits = huff.max_bits; |
|
|
|
code->bits = huff.max_bits; |
|
|
|
code->length = huff.max_num; |
|
|
|
code->length = huff.max_num; |
|
|
|
code->recode = av_malloc(code->length * sizeof(int)); |
|
|
|
code->recode = av_malloc(code->length * sizeof(int)); |
|
|
@ -228,7 +225,7 @@ static int tm2_read_deltas(TM2Context *ctx, int stream_id) { |
|
|
|
|
|
|
|
|
|
|
|
if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) { |
|
|
|
if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) { |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb); |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb); |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for(i = 0; i < d; i++) { |
|
|
|
for(i = 0; i < d; i++) { |
|
|
@ -246,7 +243,7 @@ static int tm2_read_deltas(TM2Context *ctx, int stream_id) { |
|
|
|
|
|
|
|
|
|
|
|
static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size) |
|
|
|
static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int i; |
|
|
|
int i, ret; |
|
|
|
int skip = 0; |
|
|
|
int skip = 0; |
|
|
|
int len, toks, pos; |
|
|
|
int len, toks, pos; |
|
|
|
TM2Codes codes; |
|
|
|
TM2Codes codes; |
|
|
@ -262,7 +259,7 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i |
|
|
|
|
|
|
|
|
|
|
|
if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) { |
|
|
|
if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) { |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n"); |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n"); |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
toks = bytestream2_get_be32(&gb); |
|
|
|
toks = bytestream2_get_be32(&gb); |
|
|
@ -274,10 +271,10 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i |
|
|
|
if(len > 0) { |
|
|
|
if(len > 0) { |
|
|
|
pos = bytestream2_tell(&gb); |
|
|
|
pos = bytestream2_tell(&gb); |
|
|
|
if (skip <= pos) |
|
|
|
if (skip <= pos) |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); |
|
|
|
init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); |
|
|
|
if(tm2_read_deltas(ctx, stream_id) == -1) |
|
|
|
if ((ret = tm2_read_deltas(ctx, stream_id)) < 0) |
|
|
|
return -1; |
|
|
|
return ret; |
|
|
|
bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2); |
|
|
|
bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -291,10 +288,10 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i |
|
|
|
|
|
|
|
|
|
|
|
pos = bytestream2_tell(&gb); |
|
|
|
pos = bytestream2_tell(&gb); |
|
|
|
if (skip <= pos) |
|
|
|
if (skip <= pos) |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); |
|
|
|
init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); |
|
|
|
if(tm2_build_huff_table(ctx, &codes) == -1) |
|
|
|
if ((ret = tm2_build_huff_table(ctx, &codes)) < 0) |
|
|
|
return -1; |
|
|
|
return ret; |
|
|
|
bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2); |
|
|
|
bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2); |
|
|
|
|
|
|
|
|
|
|
|
toks >>= 1; |
|
|
|
toks >>= 1; |
|
|
@ -302,7 +299,7 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i |
|
|
|
if((toks < 0) || (toks > 0xFFFFFF)){ |
|
|
|
if((toks < 0) || (toks > 0xFFFFFF)){ |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks); |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks); |
|
|
|
tm2_free_codes(&codes); |
|
|
|
tm2_free_codes(&codes); |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int)); |
|
|
|
ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int)); |
|
|
|
ctx->tok_lens[stream_id] = toks; |
|
|
|
ctx->tok_lens[stream_id] = toks; |
|
|
@ -310,12 +307,12 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i |
|
|
|
if(len > 0) { |
|
|
|
if(len > 0) { |
|
|
|
pos = bytestream2_tell(&gb); |
|
|
|
pos = bytestream2_tell(&gb); |
|
|
|
if (skip <= pos) |
|
|
|
if (skip <= pos) |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); |
|
|
|
init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8); |
|
|
|
for(i = 0; i < toks; i++) { |
|
|
|
for(i = 0; i < toks; i++) { |
|
|
|
if (get_bits_left(&ctx->gb) <= 0) { |
|
|
|
if (get_bits_left(&ctx->gb) <= 0) { |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks); |
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks); |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes); |
|
|
|
ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes); |
|
|
|
if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) { |
|
|
|
if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) { |
|
|
@ -700,7 +697,7 @@ static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p) |
|
|
|
|
|
|
|
|
|
|
|
if (ctx->tok_lens[TM2_TYPE]<bw*bh){ |
|
|
|
if (ctx->tok_lens[TM2_TYPE]<bw*bh){ |
|
|
|
av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh); |
|
|
|
av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh); |
|
|
|
return -1; |
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
memset(ctx->last, 0, 4 * bw * sizeof(int)); |
|
|
|
memset(ctx->last, 0, 4 * bw * sizeof(int)); |
|
|
@ -822,14 +819,14 @@ static int decode_frame(AVCodecContext *avctx, |
|
|
|
swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE); |
|
|
|
swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE); |
|
|
|
if(!swbuf){ |
|
|
|
if(!swbuf){ |
|
|
|
av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n"); |
|
|
|
av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n"); |
|
|
|
return -1; |
|
|
|
return AVERROR(ENOMEM); |
|
|
|
} |
|
|
|
} |
|
|
|
p->reference = 1; |
|
|
|
p->reference = 1; |
|
|
|
p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; |
|
|
|
p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; |
|
|
|
if(avctx->reget_buffer(avctx, p) < 0){ |
|
|
|
if ((ret = avctx->reget_buffer(avctx, p)) < 0) { |
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); |
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); |
|
|
|
av_free(swbuf); |
|
|
|
av_free(swbuf); |
|
|
|
return -1; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2); |
|
|
|
l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2); |
|
|
@ -872,7 +869,7 @@ static av_cold int decode_init(AVCodecContext *avctx){ |
|
|
|
|
|
|
|
|
|
|
|
if((avctx->width & 3) || (avctx->height & 3)){ |
|
|
|
if((avctx->width & 3) || (avctx->height & 3)){ |
|
|
|
av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n"); |
|
|
|
av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n"); |
|
|
|
return -1; |
|
|
|
return AVERROR(EINVAL); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
l->avctx = avctx; |
|
|
|
l->avctx = avctx; |
|
|
|