diff options
Diffstat (limited to 'libavcodec/truemotion2.c')
-rw-r--r-- | libavcodec/truemotion2.c | 118 |
1 files changed, 76 insertions, 42 deletions
diff --git a/libavcodec/truemotion2.c b/libavcodec/truemotion2.c index 5d9408d166..42badcbf8e 100644 --- a/libavcodec/truemotion2.c +++ b/libavcodec/truemotion2.c @@ -2,20 +2,20 @@ * Duck/ON2 TrueMotion 2 Decoder * Copyright (c) 2005 Konstantin Shishkov * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -58,11 +58,14 @@ enum TM2_BLOCKS { typedef struct TM2Context { AVCodecContext *avctx; - AVFrame pic; + AVFrame *pic; GetBitContext gb; DSPContext dsp; + uint8_t *buffer; + int buffer_size; + /* TM2 streams */ int *tokens[TM2_NUM_STREAMS]; int tok_lens[TM2_NUM_STREAMS]; @@ -85,7 +88,7 @@ typedef struct TM2Context { * Huffman codes for each of streams */ typedef struct TM2Codes { - VLC vlc; ///< table for Libav bitstream reader + VLC vlc; ///< table for FFmpeg bitstream reader int bits; int *recode; ///< table for converting from code indexes to values int length; @@ -166,9 +169,14 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) /* allocate space for codes - it is exactly ceil(nodes / 2) entries */ huff.max_num = (huff.nodes + 1) >> 1; - huff.nums = av_mallocz(huff.max_num * sizeof(int)); - huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t)); - huff.lens = av_mallocz(huff.max_num * sizeof(int)); + huff.nums = av_calloc(huff.max_num, sizeof(int)); + huff.bits = av_calloc(huff.max_num, sizeof(uint32_t)); + huff.lens = av_calloc(huff.max_num, sizeof(int)); + + if (!huff.nums || !huff.bits || !huff.lens) { + res = AVERROR(ENOMEM); + goto fail; + } res = tm2_read_tree(ctx, 0, 0, &huff); @@ -190,11 +198,16 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) else { code->bits = huff.max_bits; code->length = huff.max_num; - code->recode = av_malloc(code->length * sizeof(int)); + code->recode = av_malloc_array(code->length, sizeof(int)); + if (!code->recode) { + res = AVERROR(ENOMEM); + goto fail; + } for (i = 0; i < code->length; i++) code->recode[i] = huff.nums[i]; } } +fail: /* free allocated memory */ av_free(huff.nums); av_free(huff.bits); @@ -214,6 +227,8 @@ static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code) { int val; val = get_vlc2(gb, code->vlc.table, code->bits, 1); + if(val<0) + return -1; return code->recode[val]; } @@ -270,6 +285,11 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i TM2Codes codes; GetByteContext gb; + if (buf_size < 4) { + av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n"); + return AVERROR_INVALIDDATA; + } + /* get stream length in dwords */ bytestream2_init(&gb, buf, buf_size); len = bytestream2_get_be32(&gb); @@ -278,8 +298,8 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i if (len == 0) return 4; - if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) { - av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n"); + if (len >= INT_MAX/4-1 || len < 0 || skip > buf_size) { + av_log(ctx->avctx, AV_LOG_ERROR, "invalid stream size\n"); return AVERROR_INVALIDDATA; } @@ -322,7 +342,11 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i tm2_free_codes(&codes); return AVERROR_INVALIDDATA; } - ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int)); + ret = av_reallocp_array(&ctx->tokens[stream_id], toks, sizeof(int)); + if (ret < 0) { + ctx->tok_lens[stream_id] = 0; + return ret; + } ctx->tok_lens[stream_id] = toks; len = bytestream2_get_be32(&gb); if (len > 0) { @@ -336,7 +360,7 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i return AVERROR_INVALIDDATA; } 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 || ctx->tokens[stream_id][i]<0) { av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n", ctx->tokens[stream_id][i], stream_id, i); return AVERROR_INVALIDDATA; @@ -363,8 +387,13 @@ static inline int GET_TOK(TM2Context *ctx,int type) av_log(ctx->avctx, AV_LOG_ERROR, "Read token from stream %i out of bounds (%i>=%i)\n", type, ctx->tok_ptrs[type], ctx->tok_lens[type]); return 0; } - if (type <= TM2_MOT) + if (type <= TM2_MOT) { + if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) { + av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]); + return 0; + } return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]]; + } return ctx->tokens[type][ctx->tok_ptrs[type]++]; } @@ -671,6 +700,11 @@ static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int b mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4); my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4); + if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) { + av_log(ctx->avctx, AV_LOG_ERROR, "MV out of picture\n"); + return; + } + Yo += my * oYstride + mx; Uo += (my >> 1) * oUstride + (mx >> 1); Vo += (my >> 1) * oVstride + (mx >> 1); @@ -838,39 +872,36 @@ static int decode_frame(AVCodecContext *avctx, TM2Context * const l = avctx->priv_data; const uint8_t *buf = avpkt->data; int buf_size = avpkt->size & ~3; - AVFrame * const p = &l->pic; + AVFrame * const p = l->pic; int offset = TM2_HEADER_SIZE; int i, t, ret; - uint8_t *swbuf; - swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE); - if (!swbuf) { + av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size); + if (!l->buffer) { av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n"); return AVERROR(ENOMEM); } - if ((ret = ff_reget_buffer(avctx, p)) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); - av_free(swbuf); + if ((ret = ff_reget_buffer(avctx, p)) < 0) return ret; - } - l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2); + l->dsp.bswap_buf((uint32_t*)l->buffer, (const uint32_t*)buf, buf_size >> 2); - if ((ret = tm2_read_header(l, swbuf)) < 0) { - av_free(swbuf); + if ((ret = tm2_read_header(l, l->buffer)) < 0) { return ret; } for (i = 0; i < TM2_NUM_STREAMS; i++) { if (offset >= buf_size) { - av_free(swbuf); + av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n"); return AVERROR_INVALIDDATA; } - t = tm2_read_stream(l, swbuf + offset, tm2_stream_order[i], + + t = tm2_read_stream(l, l->buffer + offset, tm2_stream_order[i], buf_size - offset); if (t < 0) { - av_free(swbuf); + int j = tm2_stream_order[i]; + memset(l->tokens[j], 0, sizeof(**l->tokens) * l->tok_lens[j]); return t; } offset += t; @@ -883,8 +914,7 @@ static int decode_frame(AVCodecContext *avctx, l->cur = !l->cur; *got_frame = 1; - ret = av_frame_ref(data, &l->pic); - av_free(swbuf); + ret = av_frame_ref(data, l->pic); return (ret < 0) ? ret : buf_size; } @@ -900,13 +930,15 @@ static av_cold int decode_init(AVCodecContext *avctx) } l->avctx = avctx; - avcodec_get_frame_defaults(&l->pic); avctx->pix_fmt = AV_PIX_FMT_BGR24; + l->pic = av_frame_alloc(); + if (!l->pic) + return AVERROR(ENOMEM); ff_dsputil_init(&l->dsp, avctx); - l->last = av_malloc(4 * sizeof(*l->last) * (w >> 2)); - l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2)); + l->last = av_malloc_array(w >> 2, 4 * sizeof(*l->last) ); + l->clast = av_malloc_array(w >> 2, 4 * sizeof(*l->clast)); for (i = 0; i < TM2_NUM_STREAMS; i++) { l->tokens[i] = NULL; @@ -915,15 +947,15 @@ static av_cold int decode_init(AVCodecContext *avctx) w += 8; h += 8; - l->Y1_base = av_malloc(sizeof(*l->Y1_base) * w * h); - l->Y2_base = av_malloc(sizeof(*l->Y2_base) * w * h); + l->Y1_base = av_calloc(w * h, sizeof(*l->Y1_base)); + l->Y2_base = av_calloc(w * h, sizeof(*l->Y2_base)); l->y_stride = w; w = (w + 1) >> 1; h = (h + 1) >> 1; - l->U1_base = av_malloc(sizeof(*l->U1_base) * w * h); - l->V1_base = av_malloc(sizeof(*l->V1_base) * w * h); - l->U2_base = av_malloc(sizeof(*l->U2_base) * w * h); - l->V2_base = av_malloc(sizeof(*l->V1_base) * w * h); + l->U1_base = av_calloc(w * h, sizeof(*l->U1_base)); + l->V1_base = av_calloc(w * h, sizeof(*l->V1_base)); + l->U2_base = av_calloc(w * h, sizeof(*l->U2_base)); + l->V2_base = av_calloc(w * h, sizeof(*l->V1_base)); l->uv_stride = w; l->cur = 0; if (!l->Y1_base || !l->Y2_base || !l->U1_base || @@ -937,6 +969,7 @@ static av_cold int decode_init(AVCodecContext *avctx) av_freep(&l->V2_base); av_freep(&l->last); av_freep(&l->clast); + av_frame_free(&l->pic); return AVERROR(ENOMEM); } l->Y1 = l->Y1_base + l->y_stride * 4 + 4; @@ -952,7 +985,6 @@ static av_cold int decode_init(AVCodecContext *avctx) static av_cold int decode_end(AVCodecContext *avctx) { TM2Context * const l = avctx->priv_data; - AVFrame *pic = &l->pic; int i; av_free(l->last); @@ -967,8 +999,10 @@ static av_cold int decode_end(AVCodecContext *avctx) av_free(l->U2_base); av_free(l->V2_base); } + av_freep(&l->buffer); + l->buffer_size = 0; - av_frame_unref(pic); + av_frame_free(&l->pic); return 0; } |