diff options
Diffstat (limited to 'libavcodec/vble.c')
-rw-r--r-- | libavcodec/vble.c | 109 |
1 files changed, 56 insertions, 53 deletions
diff --git a/libavcodec/vble.c b/libavcodec/vble.c index a349d8f..ed08b83 100644 --- a/libavcodec/vble.c +++ b/libavcodec/vble.c @@ -2,20 +2,20 @@ * VBLE Decoder * Copyright (c) 2011 Derek Buitenhuis * - * 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 */ @@ -37,56 +37,52 @@ typedef struct { DSPContext dsp; int size; - uint8_t *val; /* First holds the lengths of vlc symbols and then their values */ + uint8_t *val; ///< This array first holds the lengths of vlc symbols and then their value. } VBLEContext; -static uint8_t vble_read_reverse_unary(GetBitContext *gb) -{ - /* At most we need to read 9 bits total to get indices up to 8 */ - uint8_t val = show_bits(gb, 8); - - if (val) { - val = 7 - av_log2_16bit(ff_reverse[val]); - skip_bits(gb, val + 1); - return val; - } else { - skip_bits(gb, 8); - if (get_bits1(gb)) - return 8; - } - - /* Return something larger than 8 on error */ - return UINT8_MAX; -} - static int vble_unpack(VBLEContext *ctx, GetBitContext *gb) { int i; + int allbits = 0; + static const uint8_t LUT[256] = { + 8,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, + 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, + 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, + 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, + 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, + 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, + 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, + 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, + }; /* Read all the lengths in first */ for (i = 0; i < ctx->size; i++) { - ctx->val[i] = vble_read_reverse_unary(gb); - - if (ctx->val[i] == UINT8_MAX) - return -1; - } - - for (i = 0; i < ctx->size; i++) { - /* Check we have enough bits left */ - if (get_bits_left(gb) < ctx->val[i]) - return -1; - - /* get_bits can't take a length of 0 */ - if (ctx->val[i]) - ctx->val[i] = (1 << ctx->val[i]) + get_bits(gb, ctx->val[i]) - 1; + /* At most we need to read 9 bits total to get indices up to 8 */ + int val = show_bits(gb, 8); + + // read reverse unary + if (val) { + val = LUT[val]; + skip_bits(gb, val + 1); + ctx->val[i] = val; + } else { + skip_bits(gb, 8); + if (!get_bits1(gb)) + return -1; + ctx->val[i] = 8; + } + allbits += ctx->val[i]; } + /* Check we have enough bits left */ + if (get_bits_left(gb) < allbits) + return -1; return 0; } static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic, - int plane, int offset, - int width, int height) + GetBitContext *gb, int plane, + int offset, int width, int height) { uint8_t *dst = pic->data[plane]; uint8_t *val = ctx->val + offset; @@ -94,14 +90,17 @@ static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic, int i, j, left, left_top; for (i = 0; i < height; i++) { - for (j = 0; j < width; j++) - val[j] = (val[j] >> 1) ^ -(val[j] & 1); - + for (j = 0; j < width; j++) { + /* get_bits can't take a length of 0 */ + if (val[j]) { + int v = (1 << val[j]) + get_bits(gb, val[j]) - 1; + val[j] = (v >> 1) ^ -(v & 1); + } + } if (i) { left = 0; left_top = dst[-stride]; - ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val, - width, &left, &left_top); + ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val, width, &left, &left_top); } else { dst[0] = val[0]; for (j = 1; j < width; j++) @@ -122,13 +121,17 @@ static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, int version; int offset = 0; int width_uv = avctx->width / 2, height_uv = avctx->height / 2; + int ret; - /* Allocate buffer */ - if (ff_get_buffer(avctx, pic, 0) < 0) { - av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n"); - return AVERROR(ENOMEM); + if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) { + av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n"); + return AVERROR_INVALIDDATA; } + /* Allocate buffer */ + if ((ret = ff_get_buffer(avctx, pic, 0)) < 0) + return ret; + /* Set flags */ pic->key_frame = 1; pic->pict_type = AV_PICTURE_TYPE_I; @@ -148,15 +151,15 @@ static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, } /* Restore planes. Should be almost identical to Huffyuv's. */ - vble_restore_plane(ctx, pic, 0, offset, avctx->width, avctx->height); + vble_restore_plane(ctx, pic, &gb, 0, offset, avctx->width, avctx->height); /* Chroma */ if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) { offset += avctx->width * avctx->height; - vble_restore_plane(ctx, pic, 1, offset, width_uv, height_uv); + vble_restore_plane(ctx, pic, &gb, 1, offset, width_uv, height_uv); offset += width_uv * height_uv; - vble_restore_plane(ctx, pic, 2, offset, width_uv, height_uv); + vble_restore_plane(ctx, pic, &gb, 2, offset, width_uv, height_uv); } *got_frame = 1; @@ -186,7 +189,7 @@ static av_cold int vble_decode_init(AVCodecContext *avctx) ctx->size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height); - ctx->val = av_malloc(ctx->size * sizeof(*ctx->val)); + ctx->val = av_malloc_array(ctx->size, sizeof(*ctx->val)); if (!ctx->val) { av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n"); |