diff options
Diffstat (limited to 'libavcodec/hqx.c')
-rw-r--r-- | libavcodec/hqx.c | 168 |
1 files changed, 93 insertions, 75 deletions
diff --git a/libavcodec/hqx.c b/libavcodec/hqx.c index c323a70fb7..8c568f2439 100644 --- a/libavcodec/hqx.c +++ b/libavcodec/hqx.c @@ -1,20 +1,20 @@ /* * Canopus HQX decoder * - * 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 */ @@ -40,7 +40,7 @@ enum HQXFormat { #define HQX_HEADER_SIZE 59 -typedef int (*mb_decode_func)(HQXContext *ctx, AVFrame *pic, +typedef int (*mb_decode_func)(HQXContext *ctx, HQXSliceData * slice_data, AVFrame *pic, GetBitContext *gb, int x, int y); /* macroblock selects a group of 4 possible quants and @@ -154,7 +154,7 @@ static int decode_block(GetBitContext *gb, VLC *vlc, return 0; } -static int hqx_decode_422(HQXContext *ctx, AVFrame *pic, +static int hqx_decode_422(HQXContext *ctx, HQXSliceData * slice_data, AVFrame *pic, GetBitContext *gb, int x, int y) { const int *quants; @@ -174,20 +174,20 @@ static int hqx_decode_422(HQXContext *ctx, AVFrame *pic, if (i == 0 || i == 4 || i == 6) last_dc = 0; ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, - ctx->dcb, ctx->block[i], &last_dc); + ctx->dcb, slice_data->block[i], &last_dc); if (ret < 0) return ret; } - put_blocks(ctx, pic, 0, x, y, flag, ctx->block[0], ctx->block[2], hqx_quant_luma); - put_blocks(ctx, pic, 0, x + 8, y, flag, ctx->block[1], ctx->block[3], hqx_quant_luma); - put_blocks(ctx, pic, 2, x >> 1, y, flag, ctx->block[4], ctx->block[5], hqx_quant_chroma); - put_blocks(ctx, pic, 1, x >> 1, y, flag, ctx->block[6], ctx->block[7], hqx_quant_chroma); + put_blocks(ctx, pic, 0, x , y, flag, slice_data->block[0], slice_data->block[2], hqx_quant_luma); + put_blocks(ctx, pic, 0, x + 8 , y, flag, slice_data->block[1], slice_data->block[3], hqx_quant_luma); + put_blocks(ctx, pic, 2, x >> 1, y, flag, slice_data->block[4], slice_data->block[5], hqx_quant_chroma); + put_blocks(ctx, pic, 1, x >> 1, y, flag, slice_data->block[6], slice_data->block[7], hqx_quant_chroma); return 0; } -static int hqx_decode_422a(HQXContext *ctx, AVFrame *pic, +static int hqx_decode_422a(HQXContext *ctx, HQXSliceData * slice_data, AVFrame *pic, GetBitContext *gb, int x, int y) { const int *quants; @@ -199,9 +199,9 @@ static int hqx_decode_422a(HQXContext *ctx, AVFrame *pic, cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1); for (i = 0; i < 12; i++) - memset(ctx->block[i], 0, sizeof(**ctx->block) * 64); + memset(slice_data->block[i], 0, sizeof(**slice_data->block) * 64); for (i = 0; i < 12; i++) - ctx->block[i][0] = -0x800; + slice_data->block[i][0] = -0x800; if (cbp) { if (ctx->interlaced) flag = get_bits1(gb); @@ -219,24 +219,24 @@ static int hqx_decode_422a(HQXContext *ctx, AVFrame *pic, if (cbp & (1 << i)) { int vlc_index = ctx->dcb - 9; ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, - ctx->dcb, ctx->block[i], &last_dc); + ctx->dcb, slice_data->block[i], &last_dc); if (ret < 0) return ret; } } } - put_blocks(ctx, pic, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma); - put_blocks(ctx, pic, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma); - put_blocks(ctx, pic, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma); - put_blocks(ctx, pic, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma); - put_blocks(ctx, pic, 2, x >> 1, y, flag, ctx->block[ 8], ctx->block[ 9], hqx_quant_chroma); - put_blocks(ctx, pic, 1, x >> 1, y, flag, ctx->block[10], ctx->block[11], hqx_quant_chroma); + put_blocks(ctx, pic, 3, x, y, flag, slice_data->block[ 0], slice_data->block[ 2], hqx_quant_luma); + put_blocks(ctx, pic, 3, x + 8, y, flag, slice_data->block[ 1], slice_data->block[ 3], hqx_quant_luma); + put_blocks(ctx, pic, 0, x, y, flag, slice_data->block[ 4], slice_data->block[ 6], hqx_quant_luma); + put_blocks(ctx, pic, 0, x + 8, y, flag, slice_data->block[ 5], slice_data->block[ 7], hqx_quant_luma); + put_blocks(ctx, pic, 2, x >> 1, y, flag, slice_data->block[ 8], slice_data->block[ 9], hqx_quant_chroma); + put_blocks(ctx, pic, 1, x >> 1, y, flag, slice_data->block[10], slice_data->block[11], hqx_quant_chroma); return 0; } -static int hqx_decode_444(HQXContext *ctx, AVFrame *pic, +static int hqx_decode_444(HQXContext *ctx, HQXSliceData * slice_data, AVFrame *pic, GetBitContext *gb, int x, int y) { const int *quants; @@ -256,22 +256,22 @@ static int hqx_decode_444(HQXContext *ctx, AVFrame *pic, if (i == 0 || i == 4 || i == 8) last_dc = 0; ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, - ctx->dcb, ctx->block[i], &last_dc); + ctx->dcb, slice_data->block[i], &last_dc); if (ret < 0) return ret; } - put_blocks(ctx, pic, 0, x, y, flag, ctx->block[0], ctx->block[ 2], hqx_quant_luma); - put_blocks(ctx, pic, 0, x + 8, y, flag, ctx->block[1], ctx->block[ 3], hqx_quant_luma); - put_blocks(ctx, pic, 2, x, y, flag, ctx->block[4], ctx->block[ 6], hqx_quant_chroma); - put_blocks(ctx, pic, 2, x + 8, y, flag, ctx->block[5], ctx->block[ 7], hqx_quant_chroma); - put_blocks(ctx, pic, 1, x, y, flag, ctx->block[8], ctx->block[10], hqx_quant_chroma); - put_blocks(ctx, pic, 1, x + 8, y, flag, ctx->block[9], ctx->block[11], hqx_quant_chroma); + put_blocks(ctx, pic, 0, x, y, flag, slice_data->block[0], slice_data->block[ 2], hqx_quant_luma); + put_blocks(ctx, pic, 0, x + 8, y, flag, slice_data->block[1], slice_data->block[ 3], hqx_quant_luma); + put_blocks(ctx, pic, 2, x, y, flag, slice_data->block[4], slice_data->block[ 6], hqx_quant_chroma); + put_blocks(ctx, pic, 2, x + 8, y, flag, slice_data->block[5], slice_data->block[ 7], hqx_quant_chroma); + put_blocks(ctx, pic, 1, x, y, flag, slice_data->block[8], slice_data->block[10], hqx_quant_chroma); + put_blocks(ctx, pic, 1, x + 8, y, flag, slice_data->block[9], slice_data->block[11], hqx_quant_chroma); return 0; } -static int hqx_decode_444a(HQXContext *ctx, AVFrame *pic, +static int hqx_decode_444a(HQXContext *ctx, HQXSliceData * slice_data, AVFrame *pic, GetBitContext *gb, int x, int y) { const int *quants; @@ -283,9 +283,9 @@ static int hqx_decode_444a(HQXContext *ctx, AVFrame *pic, cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1); for (i = 0; i < 16; i++) - memset(ctx->block[i], 0, sizeof(**ctx->block) * 64); + memset(slice_data->block[i], 0, sizeof(**slice_data->block) * 64); for (i = 0; i < 16; i++) - ctx->block[i][0] = -0x800; + slice_data->block[i][0] = -0x800; if (cbp) { if (ctx->interlaced) flag = get_bits1(gb); @@ -300,21 +300,21 @@ static int hqx_decode_444a(HQXContext *ctx, AVFrame *pic, if (cbp & (1 << i)) { int vlc_index = ctx->dcb - 9; ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants, - ctx->dcb, ctx->block[i], &last_dc); + ctx->dcb, slice_data->block[i], &last_dc); if (ret < 0) return ret; } } } - put_blocks(ctx, pic, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma); - put_blocks(ctx, pic, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma); - put_blocks(ctx, pic, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma); - put_blocks(ctx, pic, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma); - put_blocks(ctx, pic, 2, x, y, flag, ctx->block[ 8], ctx->block[10], hqx_quant_chroma); - put_blocks(ctx, pic, 2, x + 8, y, flag, ctx->block[ 9], ctx->block[11], hqx_quant_chroma); - put_blocks(ctx, pic, 1, x, y, flag, ctx->block[12], ctx->block[14], hqx_quant_chroma); - put_blocks(ctx, pic, 1, x + 8, y, flag, ctx->block[13], ctx->block[15], hqx_quant_chroma); + put_blocks(ctx, pic, 3, x, y, flag, slice_data->block[ 0], slice_data->block[ 2], hqx_quant_luma); + put_blocks(ctx, pic, 3, x + 8, y, flag, slice_data->block[ 1], slice_data->block[ 3], hqx_quant_luma); + put_blocks(ctx, pic, 0, x, y, flag, slice_data->block[ 4], slice_data->block[ 6], hqx_quant_luma); + put_blocks(ctx, pic, 0, x + 8, y, flag, slice_data->block[ 5], slice_data->block[ 7], hqx_quant_luma); + put_blocks(ctx, pic, 2, x, y, flag, slice_data->block[ 8], slice_data->block[10], hqx_quant_chroma); + put_blocks(ctx, pic, 2, x + 8, y, flag, slice_data->block[ 9], slice_data->block[11], hqx_quant_chroma); + put_blocks(ctx, pic, 1, x, y, flag, slice_data->block[12], slice_data->block[14], hqx_quant_chroma); + put_blocks(ctx, pic, 1, x + 8, y, flag, slice_data->block[13], slice_data->block[15], hqx_quant_chroma); return 0; } @@ -370,13 +370,48 @@ static int decode_slice(HQXContext *ctx, AVFrame *pic, GetBitContext *gb, mb_x += pos % grp_w; mb_y = loc_row + (pos / grp_w); } - decode_func(ctx, pic, gb, mb_x * 16, mb_y * 16); + decode_func(ctx, &ctx->slice[slice_no], pic, gb, mb_x * 16, mb_y * 16); } } return 0; } +typedef struct { + AVFrame *pic; + uint8_t *src; + GetBitContext gb[17]; + unsigned data_size; + mb_decode_func decode_func; + uint32_t slice_off[17]; +} Data; + +static int decode_slice_thread(AVCodecContext *avctx, void *arg, int slice, int threadnr) +{ + Data * data = (Data*) arg; + uint32_t * slice_off = data->slice_off; + unsigned data_size = data->data_size; + HQXContext *ctx = avctx->priv_data; + int ret; + + if (slice_off[slice] < HQX_HEADER_SIZE || + slice_off[slice] >= slice_off[slice + 1] || + slice_off[slice + 1] > data_size) { + av_log(avctx, AV_LOG_ERROR, "Invalid slice size.\n"); + return AVERROR_INVALIDDATA; + } + + ret = init_get_bits8(&data->gb[slice], data->src + slice_off[slice], slice_off[slice + 1] - slice_off[slice]); + if (ret < 0) + return ret; + + ret = decode_slice(ctx, data->pic, &data->gb[slice], slice, data->decode_func); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d.\n", slice); + } + return ret; +} + static int hqx_decode_frame(AVCodecContext *avctx, void *data, int *got_picture_ptr, AVPacket *avpkt) { @@ -385,12 +420,9 @@ static int hqx_decode_frame(AVCodecContext *avctx, void *data, uint8_t *src = avpkt->data; uint32_t info_tag, info_offset; int data_start; - unsigned data_size; - GetBitContext gb; int i, ret; - int slice; - uint32_t slice_off[17]; - mb_decode_func decode_func = 0; + Data arg_data; + arg_data.decode_func = 0; if (avpkt->size < 8) return AVERROR_INVALIDDATA; @@ -414,9 +446,11 @@ static int hqx_decode_frame(AVCodecContext *avctx, void *data, } data_start = src - avpkt->data; - data_size = avpkt->size - data_start; + arg_data.src = src; + arg_data.pic = data; + arg_data.data_size = avpkt->size - data_start; - if (data_size < HQX_HEADER_SIZE) { + if (arg_data.data_size < HQX_HEADER_SIZE) { av_log(avctx, AV_LOG_ERROR, "Frame too small.\n"); return AVERROR_INVALIDDATA; } @@ -431,7 +465,7 @@ static int hqx_decode_frame(AVCodecContext *avctx, void *data, ctx->width = AV_RB16(src + 4); ctx->height = AV_RB16(src + 6); for (i = 0; i < 17; i++) - slice_off[i] = AV_RB24(src + 8 + i * 3); + arg_data.slice_off[i] = AV_RB24(src + 8 + i * 3); if (ctx->dcb == 8) { av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb); @@ -439,7 +473,7 @@ static int hqx_decode_frame(AVCodecContext *avctx, void *data, } ret = av_image_check_size(ctx->width, ctx->height, 0, avctx); if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "Invalid stored dimenstions %dx%d.\n", + av_log(avctx, AV_LOG_ERROR, "Invalid stored dimensions %dx%d.\n", ctx->width, ctx->height); return AVERROR_INVALIDDATA; } @@ -453,19 +487,19 @@ static int hqx_decode_frame(AVCodecContext *avctx, void *data, switch (ctx->format) { case HQX_422: avctx->pix_fmt = AV_PIX_FMT_YUV422P16; - decode_func = hqx_decode_422; + arg_data.decode_func = hqx_decode_422; break; case HQX_444: avctx->pix_fmt = AV_PIX_FMT_YUV444P16; - decode_func = hqx_decode_444; + arg_data.decode_func = hqx_decode_444; break; case HQX_422A: avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; - decode_func = hqx_decode_422a; + arg_data.decode_func = hqx_decode_422a; break; case HQX_444A: avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; - decode_func = hqx_decode_444a; + arg_data.decode_func = hqx_decode_444a; break; default: av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format); @@ -473,27 +507,11 @@ static int hqx_decode_frame(AVCodecContext *avctx, void *data, } ret = ff_get_buffer(avctx, pic, 0); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n"); + if (ret < 0) return ret; - } - for (slice = 0; slice < 16; slice++) { - if (slice_off[slice] < HQX_HEADER_SIZE || - slice_off[slice] >= slice_off[slice + 1] || - slice_off[slice + 1] > data_size) { - av_log(avctx, AV_LOG_ERROR, "Invalid slice size.\n"); - break; - } - ret = init_get_bits(&gb, src + slice_off[slice], - (slice_off[slice + 1] - slice_off[slice]) * 8); - if (ret < 0) - return ret; - ret = decode_slice(ctx, pic, &gb, slice, decode_func); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d.\n", slice); - } - } + + avctx->execute2(avctx, decode_slice_thread, &arg_data, NULL, 16); pic->key_frame = 1; pic->pict_type = AV_PICTURE_TYPE_I; @@ -537,5 +555,5 @@ AVCodec ff_hqx_decoder = { .init = hqx_decode_init, .decode = hqx_decode_frame, .close = hqx_decode_close, - .capabilities = CODEC_CAP_DR1, + .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS, }; |