diff --git a/libavcodec/bink.c b/libavcodec/bink.c index bd1326c473..554f61d364 100644 --- a/libavcodec/bink.c +++ b/libavcodec/bink.c @@ -672,12 +672,11 @@ static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count) return 0; } -static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt) +static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx, + int is_chroma) { - BinkContext * const c = avctx->priv_data; - GetBitContext gb; int blk; - int i, j, plane, plane_idx, bx, by; + int i, j, bx, by; uint8_t *dst, *prev, *ref, *ref_start, *ref_end; int v, col[2]; const uint8_t *scan; @@ -685,39 +684,16 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac DECLARE_ALIGNED_16(DCTELEM, block[64]); DECLARE_ALIGNED_16(uint8_t, ublock[64]); int coordmap[64]; - int bits_count = pkt->size << 3; - if(c->pic.data[0]) - avctx->release_buffer(avctx, &c->pic); - - if(avctx->get_buffer(avctx, &c->pic) < 0){ - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); - return -1; - } - - init_get_bits(&gb, pkt->data, bits_count); - if (c->has_alpha) { - int aplane_bits = get_bits_long(&gb, 32) << 3; - if (aplane_bits <= 32 || (aplane_bits & 0x1F)) { - av_log(avctx, AV_LOG_ERROR, "Incorrect alpha plane size %d\n", aplane_bits); - return -1; - } - skip_bits_long(&gb, aplane_bits - 32); - } - if (c->version >= 'i') - skip_bits_long(&gb, 32); - - for (plane = 0; plane < 3; plane++) { - const int stride = c->pic.linesize[plane]; - int bw = plane ? (avctx->width + 15) >> 4 : (avctx->width + 7) >> 3; - int bh = plane ? (avctx->height + 15) >> 4 : (avctx->height + 7) >> 3; - int width = avctx->width >> !!plane; + const int stride = c->pic.linesize[plane_idx]; + int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3; + int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3; + int width = c->avctx->width >> is_chroma; init_lengths(c, FFMAX(width, 8), bw); for (i = 0; i < BINK_NB_SRC; i++) - read_bundle(&gb, c, i); + read_bundle(gb, c, i); - plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3); ref_start = c->last.data[plane_idx]; ref_end = c->last.data[plane_idx] + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8; @@ -726,23 +702,23 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac coordmap[i] = (i & 7) + (i >> 3) * stride; for (by = 0; by < bh; by++) { - if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0) + if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0) return -1; - if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0) + if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0) return -1; - if (read_colors(&gb, &c->bundle[BINK_SRC_COLORS], c) < 0) + if (read_colors(gb, &c->bundle[BINK_SRC_COLORS], c) < 0) return -1; - if (read_patterns(avctx, &gb, &c->bundle[BINK_SRC_PATTERN]) < 0) + if (read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN]) < 0) return -1; - if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_X_OFF]) < 0) + if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF]) < 0) return -1; - if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_Y_OFF]) < 0) + if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF]) < 0) return -1; - if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0) + if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0) return -1; - if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0) + if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0) return -1; - if (read_runs(avctx, &gb, &c->bundle[BINK_SRC_RUN]) < 0) + if (read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN]) < 0) return -1; if (by == bh) @@ -766,17 +742,17 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES); switch (blk) { case RUN_BLOCK: - scan = bink_patterns[get_bits(&gb, 4)]; + scan = bink_patterns[get_bits(gb, 4)]; i = 0; do { int run = get_value(c, BINK_SRC_RUN) + 1; i += run; if (i > 64) { - av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\n"); + av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); return -1; } - if (get_bits1(&gb)) { + if (get_bits1(gb)) { v = get_value(c, BINK_SRC_COLORS); for (j = 0; j < run; j++) ublock[*scan++] = v; @@ -791,7 +767,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac case INTRA_BLOCK: c->dsp.clear_block(block); block[0] = get_value(c, BINK_SRC_INTRA_DC); - read_dct_coeffs(&gb, block, c->scantable.permutated, 1); + read_dct_coeffs(gb, block, c->scantable.permutated, 1); c->dsp.idct(block); c->dsp.put_pixels_nonclamped(block, ublock, 8); break; @@ -814,7 +790,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac ublock[i + j*8] = get_value(c, BINK_SRC_COLORS); break; default: - av_log(avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk); + av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk); return -1; } if (blk != FILL_BLOCK) @@ -828,24 +804,24 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac yoff = get_value(c, BINK_SRC_Y_OFF); ref = prev + xoff + yoff * stride; if (ref < ref_start || ref > ref_end) { - av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n", + av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n", bx*8 + xoff, by*8 + yoff); return -1; } c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8); break; case RUN_BLOCK: - scan = bink_patterns[get_bits(&gb, 4)]; + scan = bink_patterns[get_bits(gb, 4)]; i = 0; do { int run = get_value(c, BINK_SRC_RUN) + 1; i += run; if (i > 64) { - av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\n"); + av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); return -1; } - if (get_bits1(&gb)) { + if (get_bits1(gb)) { v = get_value(c, BINK_SRC_COLORS); for (j = 0; j < run; j++) dst[coordmap[*scan++]] = v; @@ -862,20 +838,20 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac yoff = get_value(c, BINK_SRC_Y_OFF); ref = prev + xoff + yoff * stride; if (ref < ref_start || ref > ref_end) { - av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n", + av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n", bx*8 + xoff, by*8 + yoff); return -1; } c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8); c->dsp.clear_block(block); - v = get_bits(&gb, 7); - read_residue(&gb, block, v); + v = get_bits(gb, 7); + read_residue(gb, block, v); c->dsp.add_pixels8(dst, block, stride); break; case INTRA_BLOCK: c->dsp.clear_block(block); block[0] = get_value(c, BINK_SRC_INTRA_DC); - read_dct_coeffs(&gb, block, c->scantable.permutated, 1); + read_dct_coeffs(gb, block, c->scantable.permutated, 1); c->dsp.idct_put(dst, stride, block); break; case FILL_BLOCK: @@ -889,7 +865,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8); c->dsp.clear_block(block); block[0] = get_value(c, BINK_SRC_INTER_DC); - read_dct_coeffs(&gb, block, c->scantable.permutated, 0); + read_dct_coeffs(gb, block, c->scantable.permutated, 0); c->dsp.idct_add(dst, stride, block); break; case PATTERN_BLOCK: @@ -907,13 +883,49 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac c->bundle[BINK_SRC_COLORS].cur_ptr += 64; break; default: - av_log(avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk); + av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk); return -1; } } } - if (get_bits_count(&gb) & 0x1F) //next plane data starts at 32-bit boundary - skip_bits_long(&gb, 32 - (get_bits_count(&gb) & 0x1F)); + if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary + skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F)); + + return 0; +} + +static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt) +{ + BinkContext * const c = avctx->priv_data; + GetBitContext gb; + int plane, plane_idx; + int bits_count = pkt->size << 3; + + if(c->pic.data[0]) + avctx->release_buffer(avctx, &c->pic); + + if(avctx->get_buffer(avctx, &c->pic) < 0){ + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + init_get_bits(&gb, pkt->data, bits_count); + if (c->has_alpha) { + int aplane_bits = get_bits_long(&gb, 32) << 3; + if (aplane_bits <= 32 || (aplane_bits & 0x1F)) { + av_log(avctx, AV_LOG_ERROR, "Incorrect alpha plane size %d\n", aplane_bits); + return -1; + } + skip_bits_long(&gb, aplane_bits - 32); + } + if (c->version >= 'i') + skip_bits_long(&gb, 32); + + for (plane = 0; plane < 3; plane++) { + plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3); + + if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0) + return -1; if (get_bits_count(&gb) >= bits_count) break; }