[libav-devel] [PATCHv3] Canopus HQ/HQA decoder

Anton Khirnov anton at khirnov.net
Wed Apr 8 10:24:19 CEST 2015


Quoting Vittorio Giovara (2015-03-31 19:03:33)
> Based on work by Kostya Shishkov <kostya.shishkov at gmail.com>.
> 
> Signed-off-by: Vittorio Giovara <vittorio.giovara at gmail.com>
> ---
> This version adds support for parsing aspect ratio and field order, as well
> as additional bound checkings.
> Vittorio
> 
>  Changelog                           |    1 +
>  doc/general.texi                    |    2 +
>  libavcodec/Makefile                 |    1 +
>  libavcodec/allcodecs.c              |    1 +
>  libavcodec/avcodec.h                |    1 +
>  libavcodec/codec_desc.c             |    7 +
>  libavcodec/hq_hqa.c                 |  378 ++
>  libavcodec/hq_hqa.h                 |   57 +
>  libavcodec/hq_hqadata.c             | 8376 +++++++++++++++++++++++++++++++++++
>  libavcodec/hq_hqadsp.c              |  130 +
>  libavcodec/hq_hqadsp.h              |   38 +
>  libavcodec/version.h                |    2 +-
>  libavformat/riff.c                  |    1 +
>  tests/fate/video.mak                |   12 +
>  tests/ref/fate/canopus-hq_hqa-hq    |    9 +
>  tests/ref/fate/canopus-hq_hqa-hqa   |    2 +
>  tests/ref/fate/canopus-hq_hqa-inter |    2 +
>  17 files changed, 9019 insertions(+), 1 deletion(-)
>  create mode 100644 libavcodec/hq_hqa.c
>  create mode 100644 libavcodec/hq_hqa.h
>  create mode 100644 libavcodec/hq_hqadata.c
>  create mode 100644 libavcodec/hq_hqadsp.c
>  create mode 100644 libavcodec/hq_hqadsp.h
>  create mode 100644 tests/ref/fate/canopus-hq_hqa-hq
>  create mode 100644 tests/ref/fate/canopus-hq_hqa-hqa
>  create mode 100644 tests/ref/fate/canopus-hq_hqa-inter
> 
> diff --git a/Changelog b/Changelog
> index 4542e32..c3ea67c 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -27,6 +27,7 @@ version <next>:
>  - Intel QSV-accelerated H.264 encoding
>  - MMAL-accelerated H.264 decoding
>  - DTS decoding through libdcadec
> +- Canopus HQ/HQA decoder
>  
>  
>  version 11:
> diff --git a/doc/general.texi b/doc/general.texi
> index 3fa4377..472ee55 100644
> --- a/doc/general.texi
> +++ b/doc/general.texi
> @@ -227,6 +227,8 @@ library:
>  @item Delphine Software International CIN @tab   @tab X
>      @tab Multimedia format used by Delphine Software games.
>  @item Digital Speech Standard (DSS) @tab   @tab X
> + at item Canopus HQ                @tab   @tab X
> + at item Canopus HQA               @tab   @tab X
>  @item Canopus HQX               @tab   @tab X
>  @item CD+G                      @tab   @tab X
>      @tab Video format used by CD+G karaoke disks
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 8dd10c3..134a808 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -232,6 +232,7 @@ OBJS-$(CONFIG_HEVC_DECODER)            += hevc.o hevc_mvs.o hevc_ps.o hevc_sei.o
>                                            hevc_cabac.o hevc_refs.o hevcpred.o    \
>                                            hevcdsp.o hevc_filter.o
>  OBJS-$(CONFIG_HNM4_VIDEO_DECODER)      += hnm4video.o
> +OBJS-$(CONFIG_HQ_HQA_DECODER)          += hq_hqa.o hq_hqadata.o hq_hqadsp.o
>  OBJS-$(CONFIG_HQX_DECODER)             += hqx.o hqxvlc.o
>  OBJS-$(CONFIG_HUFFYUV_DECODER)         += huffyuv.o huffyuvdec.o
>  OBJS-$(CONFIG_HUFFYUV_ENCODER)         += huffyuv.o huffyuvenc.o
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index 329bf17..d2679ad 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -166,6 +166,7 @@ void avcodec_register_all(void)
>      REGISTER_DECODER(H264_QSV,          h264_qsv);
>      REGISTER_DECODER(HEVC,              hevc);
>      REGISTER_DECODER(HNM4_VIDEO,        hnm4_video);
> +    REGISTER_DECODER(HQ_HQA,            hq_hqa);
>      REGISTER_DECODER(HQX,               hqx);
>      REGISTER_ENCDEC (HUFFYUV,           huffyuv);
>      REGISTER_DECODER(IDCIN,             idcin);
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index b3a1a31..3440126 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -295,6 +295,7 @@ enum AVCodecID {
>      AV_CODEC_ID_MVC2,
>      AV_CODEC_ID_HQX,
>      AV_CODEC_ID_TDSC,
> +    AV_CODEC_ID_HQ_HQA,
>  
>      /* various PCM "codecs" */
>      AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
> diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
> index de46704..4f2a63a 100644
> --- a/libavcodec/codec_desc.c
> +++ b/libavcodec/codec_desc.c
> @@ -1141,6 +1141,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
>          .long_name = NULL_IF_CONFIG_SMALL("Canopus HQX"),
>          .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
>      },
> +    {
> +        .id        = AV_CODEC_ID_HQ_HQA,
> +        .type      = AVMEDIA_TYPE_VIDEO,
> +        .name      = "hq_hqa",
> +        .long_name = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
> +        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
> +    },
>  
>      /* image codecs */
>      {
> diff --git a/libavcodec/hq_hqa.c b/libavcodec/hq_hqa.c
> new file mode 100644
> index 0000000..935125b
> --- /dev/null
> +++ b/libavcodec/hq_hqa.c
> @@ -0,0 +1,378 @@
> +/*
> + * Canopus HQ/HQA decoder
> + *
> + * This file is part of Libav.
> + *
> + * Libav 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,
> + * 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
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include <stdint.h>
> +
> +#include "libavutil/attributes.h"
> +#include "libavutil/intreadwrite.h"
> +
> +#include "avcodec.h"
> +#include "internal.h"
> +
> +#include "hq_hqa.h"
> +#include "hq_hqadsp.h"
> +
> +#define HQ_HQA_INFO_SIZE 0x38
> +
> +static inline void put_blocks(HQContext *c, AVFrame *pic,
> +                              int plane, int x, int y, int ilace,
> +                              int16_t *block0, int16_t *block1)
> +{
> +    uint8_t *p = pic->data[plane] + x;
> +
> +    c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
> +                         pic->linesize[plane] << ilace, block0);
> +    c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
> +                         pic->linesize[plane] << ilace, block1);
> +}
> +
> +static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
> +                           int qsel, int is_chroma, int is_hqa)
> +{
> +    const int32_t *q;
> +    int val, pos = 1;
> +
> +    memset(block, 0, 64 * sizeof(*block));
> +
> +    if (!is_hqa) {
> +        block[0] = get_sbits(gb, 9) << 6;
> +        q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
> +    } else {
> +        q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
> +        block[0] = get_sbits(gb, 9) << 6;
> +    }
> +
> +    for (;;) {
> +        val  = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
> +        pos += ff_hq_ac_skips[val];
> +        if (pos >= 64)
> +            break;
> +        block[ff_zigzag_direct[pos]] = (ff_hq_ac_syms[val] * q[pos]) >> 12;
> +        pos++;
> +    }
> +
> +    return 0;
> +}
> +
> +static int hq_decode_mb(HQContext *c, AVFrame *pic,
> +                        GetBitContext *gb, int x, int y)
> +{
> +    int qgroup, flag;
> +    int i, ret;
> +
> +    qgroup = get_bits(gb, 4);
> +    flag = get_bits1(gb);
> +
> +    for (i = 0; i < 8; i++) {
> +        ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
> +        if (ret < 0)
> +            return ret;
> +    }
> +
> +    put_blocks(c, pic, 0, x,      y, flag, c->block[0], c->block[2]);
> +    put_blocks(c, pic, 0, x + 8,  y, flag, c->block[1], c->block[3]);
> +    put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
> +    put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
> +
> +    return 0;
> +}
> +
> +static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
> +                           uint8_t *src, int data_size)
> +{
> +    const HQProfile *profile;
> +    GetBitContext gb;
> +    uint32_t slice_off[21];
> +    int start_off, next_off;
> +    const uint8_t *perm;
> +    int i, slice, ret;
> +
> +    if (src[3] >= NUM_HQ_PROFILES) {
> +        profile = &ff_hq_profile[0];
> +        avpriv_request_sample(ctx->avctx, "Profile %d", src[3]);
> +    } else
> +        profile = &ff_hq_profile[src[3]];

Unchecked reads.

> +
> +    ctx->avctx->coded_width         = FFALIGN(profile->width,  16);
> +    ctx->avctx->coded_height        = FFALIGN(profile->height, 16);
> +    ctx->avctx->width               = profile->width;
> +    ctx->avctx->height              = profile->height;
> +    ctx->avctx->bits_per_raw_sample = 8;
> +    ctx->avctx->pix_fmt             = AV_PIX_FMT_YUV422P;
> +
> +    ret = ff_get_buffer(ctx->avctx, pic, 0);
> +    if (ret < 0) {
> +        av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
> +        return ret;
> +    }
> +
> +    for (i = 0; i < profile->num_slices + 1; i++)
> +        slice_off[i] = AV_RB24(src + 4 + i * 3);

Yet more unchecked reads.

> +
> +    next_off = 0;
> +    for (slice = 0; slice < profile->num_slices; slice++) {
> +        start_off = next_off;
> +        next_off  = profile->tab_h * (slice + 1) / profile->num_slices;
> +        perm = profile->perm_tab + start_off * profile->tab_w * 2;
> +
> +        if (slice_off[slice] < (profile->num_slices + 1) * 3 + 4 ||
> +            slice_off[slice] >= slice_off[slice + 1] ||
> +            slice_off[slice + 1] > data_size) {
> +            av_log(ctx->avctx, AV_LOG_ERROR,
> +                   "Invalid slice size %d.\n", data_size);
> +            break;
> +        }
> +        init_get_bits(&gb, src + slice_off[slice],
> +                      (slice_off[slice + 1] - slice_off[slice]) * 8);
> +
> +        for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
> +            ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
> +            if (ret < 0) {
> +                av_log(ctx->avctx, AV_LOG_ERROR,
> +                       "Error decoding macroblock %d at slice %d.\n", i, slice);
> +                return ret;
> +            }
> +            perm += 2;
> +        }
> +    }
> +
> +    return 0;
> +}
> +
> +static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
> +                         GetBitContext *gb, int x, int y)
> +{
> +    int flag = 0;
> +    int i, ret, cbp;
> +
> +    cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
> +
> +    for (i = 0; i < 12; i++)
> +        memset(c->block[i], 0, sizeof(*c->block));
> +    for (i = 0; i < 12; i++)
> +        c->block[i][0] = -128 * (1 << 6);
> +
> +    if (cbp) {
> +        flag = get_bits1(gb);
> +
> +        cbp |= cbp << 4;
> +        if (cbp & 0x3)
> +            cbp |= 0x500;
> +        if (cbp & 0xC)
> +            cbp |= 0xA00;
> +        for (i = 0; i < 12; i++) {
> +            if (!(cbp & (1 << i)))
> +                continue;
> +            ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
> +            if (ret < 0)
> +                return ret;
> +        }
> +    }
> +
> +    put_blocks(c, pic, 3, x,      y, flag, c->block[ 0], c->block[ 2]);
> +    put_blocks(c, pic, 3, x + 8,  y, flag, c->block[ 1], c->block[ 3]);
> +    put_blocks(c, pic, 0, x,      y, flag, c->block[ 4], c->block[ 6]);
> +    put_blocks(c, pic, 0, x + 8,  y, flag, c->block[ 5], c->block[ 7]);
> +    put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
> +    put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
> +
> +    return 0;
> +}
> +
> +static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
> +                            int quant, int slice_no, int w, int h)
> +{
> +    int i, j, off;
> +    int ret;
> +
> +    for (i = 0; i < h; i += 16) {
> +        off = (slice_no * 16 + i * 3) & 0x70;
> +        for (j = off; j < w; j += 128) {
> +            ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
> +            if (ret < 0) {
> +                av_log(ctx->avctx, AV_LOG_ERROR,
> +                       "Error decoding macroblock at %dx%d.\n", i, j);
> +                return ret;
> +            }
> +        }
> +    }
> +
> +    return 0;
> +}
> +
> +static int hqa_decode_frame(HQContext *ctx, AVFrame *pic,
> +                            uint8_t *src, int data_size)
> +{
> +    GetBitContext gb;
> +    uint32_t slice_off[9];
> +    int i, slice, ret;
> +    int width, height, num_slices = 8;
> +
> +    width  = AV_RB16(src + 4);
> +    height = AV_RB16(src + 6);

What does the scouter say about those unchecked reads?

> +
> +    ctx->avctx->coded_width         = FFALIGN(width,  16);
> +    ctx->avctx->coded_height        = FFALIGN(height, 16);
> +    ctx->avctx->width               = width;
> +    ctx->avctx->height              = height;
> +    ctx->avctx->bits_per_raw_sample = 8;
> +    ctx->avctx->pix_fmt             = AV_PIX_FMT_YUVA422P;
> +
> +    ret = ff_get_buffer(ctx->avctx, pic, 0);
> +    if (ret < 0) {
> +        av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
> +        return ret;
> +    }
> +
> +    for (i = 0; i < num_slices + 1; i++)
> +        slice_off[i] = AV_RB32(src + 12 + i * 4);

At least 0x2329 unchecked reads.

-- 
Anton Khirnov


More information about the libav-devel mailing list