On 04/02/15 15:21, Vittorio Giovara wrote:
Based on work by Kostya Shishkov <[email protected]>.
---
  Changelog                      |    1 +
  libavcodec/Makefile            |    1 +
  libavcodec/allcodecs.c         |    1 +
  libavcodec/avcodec.h           |    1 +
  libavcodec/codec_desc.c        |    7 +
  libavcodec/hqx.c               |  639 ++++++++++++
  libavcodec/hqxvlc.c            | 2182 ++++++++++++++++++++++++++++++++++++++++
  libavcodec/hqxvlc.h            |   55 +
  libavcodec/version.h           |    2 +-
  libavformat/riff.c             |    1 +
  tests/fate/video.mak           |    9 +
  tests/ref/fate/canopus-hqx422  |    2 +
  tests/ref/fate/canopus-hqx422a |    2 +
  13 files changed, 2902 insertions(+), 1 deletion(-)
  create mode 100644 libavcodec/hqx.c
  create mode 100644 libavcodec/hqxvlc.c
  create mode 100644 libavcodec/hqxvlc.h
  create mode 100644 tests/ref/fate/canopus-hqx422
  create mode 100644 tests/ref/fate/canopus-hqx422a

diff --git a/Changelog b/Changelog
index f9be9ed..0234460 100644
--- a/Changelog
+++ b/Changelog
@@ -12,6 +12,7 @@ version <next>:
  - VP8 in Ogg demuxing
  - OpenH264 encoder wrapper
  - Support DNx100 (960x720@8)
+- Canopus HQX decoder


  version 11:
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index a38f840..5c9a75b 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -221,6 +221,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_HQX_DECODER)             += hqx.o hqxvlc.o
  OBJS-$(CONFIG_HUFFYUV_DECODER)         += huffyuv.o huffyuvdec.o
  OBJS-$(CONFIG_HUFFYUV_ENCODER)         += huffyuv.o huffyuvenc.o
  OBJS-$(CONFIG_IDCIN_DECODER)           += idcinvideo.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index bda8f53..6cc1fad 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -162,6 +162,7 @@ void avcodec_register_all(void)
      REGISTER_DECODER(H264,              h264);
      REGISTER_DECODER(HEVC,              hevc);
      REGISTER_DECODER(HNM4_VIDEO,        hnm4_video);
+    REGISTER_DECODER(HQX,               hqx);
      REGISTER_ENCDEC (HUFFYUV,           huffyuv);
      REGISTER_DECODER(IDCIN,             idcin);
      REGISTER_DECODER(IFF_BYTERUN1,      iff_byterun1);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 87f3a83..d327c71 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -293,6 +293,7 @@ enum AVCodecID {
      AV_CODEC_ID_SGIRLE,
      AV_CODEC_ID_MVC1,
      AV_CODEC_ID_MVC2,
+    AV_CODEC_ID_HQX,

      /* 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 c21c57e..cf64657 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1134,6 +1134,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
          .long_name = NULL_IF_CONFIG_SMALL("SGI RLE 8-bit"),
          .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
      },
+    {
+        .id        = AV_CODEC_ID_HQX,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "hqx",
+        .long_name = NULL_IF_CONFIG_SMALL("Canopus HQX"),
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
+    },

      /* image codecs */
      {
diff --git a/libavcodec/hqx.c b/libavcodec/hqx.c
new file mode 100644
index 0000000..5b6c7f9
--- /dev/null
+++ b/libavcodec/hqx.c
@@ -0,0 +1,639 @@
+/*
+ * Canopus HQX 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 <inttypes.h>
+
+#include "libavutil/imgutils.h"
+#include "libavutil/intreadwrite.h"
+
+#include "get_bits.h"
+#include "avcodec.h"
+#include "internal.h"
+
+#include "hqxvlc.h"
+
+/* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */
+enum HQXFormat {
+    HQX_422 = 0,
+    HQX_444,
+    HQX_422A,
+    HQX_444A,
+};
+
+#define HQX_HEADER_SIZE 59
+
+typedef struct HQXContext {
+    AVCodecContext *avctx;
+    int format, dcb, width, height;
+    int interlaced;
+
+    DECLARE_ALIGNED(16, int16_t, block)[16][64];
+} HQXContext;
+
+typedef int (*mb_decode_func)(HQXContext *ctx, AVFrame *pic,
+                              GetBitContext *gb, int x, int y);
+
+/* macroblock selects a group of 4 possible quants and
+ * a block can use any of those four quantisers
+ * one column is powers of 2, the other one is powers of 2 * 3,
+ * then there is the special one, powers of 2 * 5 */
+static const int hqx_quants[16][4] = {
+    {  0x1,   0x2,   0x4,   0x8 }, {  0x1,  0x3,   0x6,   0xC },
+    {  0x2,   0x4,   0x8,  0x10 }, {  0x3,  0x6,   0xC,  0x18 },
+    {  0x4,   0x8,  0x10,  0x20 }, {  0x6,  0xC,  0x18,  0x30 },
+    {  0x8,  0x10,  0x20,  0x40 },
+                      { 0xA, 0x14, 0x28, 0x50 },
+                                   {  0xC, 0x18,  0x30,  0x60 },
+    { 0x10,  0x20,  0x40,  0x80 }, { 0x18, 0x30,  0x60,  0xC0 },
+    { 0x20,  0x40,  0x80, 0x100 }, { 0x30, 0x60,  0xC0, 0x180 },
+    { 0x40,  0x80, 0x100, 0x200 }, { 0x60, 0xC0, 0x180, 0x300 },
+    { 0x80, 0x100, 0x200, 0x400 }
+};
+
+static const uint8_t hqx_quant_luma[64] = {
+    16,  16,  16,  19,  19,  19,  42,  44,
+    16,  16,  19,  19,  19,  38,  43,  45,
+    16,  19,  19,  19,  40,  41,  45,  48,
+    19,  19,  19,  40,  41,  42,  46,  49,
+    19,  19,  40,  41,  42,  43,  48, 101,
+    19,  38,  41,  42,  43,  44,  98, 104,
+    42,  43,  45,  46,  48,  98, 109, 116,
+    44,  45,  48,  49, 101, 104, 116, 123,
+};
+
+static const uint8_t hqx_quant_chroma[64] = {
+    16,  16,  19,  25,  26,  26,  42,  44,
+    16,  19,  25,  25,  26,  38,  43,  91,
+    19,  25,  26,  27,  40,  41,  91,  96,
+    25,  25,  27,  40,  41,  84,  93, 197,
+    26,  26,  40,  41,  84,  86, 191, 203,
+    26,  38,  41,  84,  86, 177, 197, 209,
+    42,  43,  91,  93, 191, 197, 219, 232,
+    44,  91,  96, 197, 203, 209, 232, 246,
+};
+
+static inline void idct_col(int16_t *blk, const uint8_t *quant)
+{
+    int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF;
+    int t10, t11, t12, t13;
+    int s0, s1, s2, s3, s4, s5, s6, s7;
+
+    s0 = (int) blk[0 * 8] * quant[0 * 8];
+    s1 = (int) blk[1 * 8] * quant[1 * 8];
+    s2 = (int) blk[2 * 8] * quant[2 * 8];
+    s3 = (int) blk[3 * 8] * quant[3 * 8];
+    s4 = (int) blk[4 * 8] * quant[4 * 8];
+    s5 = (int) blk[5 * 8] * quant[5 * 8];
+    s6 = (int) blk[6 * 8] * quant[6 * 8];
+    s7 = (int) blk[7 * 8] * quant[7 * 8];
+
+    t0  =  (s3 * 19266 + s5 * 12873) >> 15;
+    t1  =  (s5 * 19266 - s3 * 12873) >> 15;
+    t2  = ((s7 * 4520  + s1 * 22725) >> 15) - t0;
+    t3  = ((s1 * 4520  - s7 * 22725) >> 15) - t1;
+    t4  = t0 * 2 + t2;
+    t5  = t1 * 2 + t3;
+    t6  = t2 - t3;
+    t7  = t3 * 2 + t6;
+    t8  = (t6 * 11585) >> 14;
+    t9  = (t7 * 11585) >> 14;
+    tA  = (s2 * 8867 - s6 * 21407) >> 14;
+    tB  = (s6 * 8867 + s2 * 21407) >> 14;
+    tC  = (s0 >> 1) - (s4 >> 1);
+    tD  = (s4 >> 1) * 2 + tC;
+    tE  = tC - (tA >> 1);
+    tF  = tD - (tB >> 1);
+    t10 = tF - t5;
+    t11 = tE - t8;
+    t12 = tE + (tA >> 1) * 2 - t9;
+    t13 = tF + (tB >> 1) * 2 - t4;
+
+    blk[0 * 8] = t13 + t4 * 2;
+    blk[1 * 8] = t12 + t9 * 2;
+    blk[2 * 8] = t11 + t8 * 2;
+    blk[3 * 8] = t10 + t5 * 2;
+    blk[4 * 8] = t10;
+    blk[5 * 8] = t11;
+    blk[6 * 8] = t12;
+    blk[7 * 8] = t13;
+}
+
+static inline void idct_row(int16_t *blk)
+{
+    int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF;
+    int t10, t11, t12, t13;
+
+    t0  =  (blk[3] * 19266 + blk[5] * 12873) >> 14;
+    t1  =  (blk[5] * 19266 - blk[3] * 12873) >> 14;
+    t2  = ((blk[7] * 4520  + blk[1] * 22725) >> 14) - t0;
+    t3  = ((blk[1] * 4520  - blk[7] * 22725) >> 14) - t1;
+    t4  = t0 * 2 + t2;
+    t5  = t1 * 2 + t3;
+    t6  = t2 - t3;
+    t7  = t3 * 2 + t6;
+    t8  = (t6 * 11585) >> 14;
+    t9  = (t7 * 11585) >> 14;
+    tA  = (blk[2] * 8867 - blk[6] * 21407) >> 14;
+    tB  = (blk[6] * 8867 + blk[2] * 21407) >> 14;
+    tC  = blk[0] - blk[4];
+    tD  = blk[4] * 2 + tC;
+    tE  = tC - tA;
+    tF  = tD - tB;
+    t10 = tF - t5;
+    t11 = tE - t8;
+    t12 = tE + tA * 2 - t9;
+    t13 = tF + tB * 2 - t4;
+
+    blk[0] = (t13 + t4 * 2 + 4) >> 3;
+    blk[1] = (t12 + t9 * 2 + 4) >> 3;
+    blk[2] = (t11 + t8 * 2 + 4) >> 3;
+    blk[3] = (t10 + t5 * 2 + 4) >> 3;
+    blk[4] = (t10          + 4) >> 3;
+    blk[5] = (t11          + 4) >> 3;
+    blk[6] = (t12          + 4) >> 3;
+    blk[7] = (t13          + 4) >> 3;
+}
+
+static void hqx_idct(int16_t *block, const uint8_t *quant)
+{
+    int i;
+
+    for (i = 0; i < 8; i++)
+        idct_col(block + i, quant + i);
+    for (i = 0; i < 8; i++)
+        idct_row(block + i * 8);
+}
+
+static void hqx_idct_put(uint16_t *dst, int stride, int16_t *block,
+                         const uint8_t *quant)
+{
+    int i, j, v;
+
+    hqx_idct(block, quant);
+
+    for (i = 0; i < 8; i++) {
+        for (j = 0; j < 8; j++) {
+            v = av_clip(block[j + i * 8] + 0x800, 0, 0x1000);
+            dst[j] = (v << 4) | (v >> 8);
+        }
+        dst += stride >> 1;
+    }
+}
+
+static inline void put_blocks(AVFrame *pic, int plane,
+                              int x, int y, int ilace,
+                              int16_t *block0, int16_t *block1,
+                              const uint8_t *quant)
+{
+    if (!ilace) {
+        hqx_idct_put(
+            (uint16_t *)(pic->data[plane] + x * 2 + y * pic->linesize[plane]),
+             pic->linesize[plane], block0, quant);
+        hqx_idct_put(
+            (uint16_t *)(pic->data[plane] + x * 2 + (y + 8) * 
pic->linesize[plane]),
+            pic->linesize[plane], block1, quant);
+    } else {
+        hqx_idct_put(
+            (uint16_t *)(pic->data[plane] + x * 2 + y * pic->linesize[plane]),
+            pic->linesize[plane] * 2, block0, quant);
+        hqx_idct_put(
+            (uint16_t *)(pic->data[plane] + x * 2 + (y + 1) * 
pic->linesize[plane]),
+            pic->linesize[plane] * 2, block1, quant);
+    }
+}
+
+static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac,
+                              int *run, int *lev)
+{
+    int val;
+
+    val = show_bits(gb, ac->lut_bits);

The lut_bits/extra_bits can be larger than the lut?

+    if (ac->lut[val].bits == -1) {
+        GetBitContext gb2 = *gb;
+        skip_bits(&gb2, ac->lut_bits);
+        val = ac->lut[val].lev + show_bits(&gb2, ac->extra_bits);
+    }
+    *run = ac->lut[val].run;
+    *lev = ac->lut[val].lev;
+    skip_bits(gb, ac->lut[val].bits);
+}
+
+static int decode_block(GetBitContext *gb, const int *quants, int dcb,
+                        int16_t block[64], int *last_dc)
+{
+    int q, dc;
+    int ac_idx;
+    int run, lev, pos = 1;
+
+    memset(block, 0, 64 * sizeof(*block));
+    dc = get_vlc2(gb, ff_hqx_dc_vlc[dcb - 9].table, HQX_DC_VLC_BITS, 2);
+    if (dc < 0)
+        return AVERROR_INVALIDDATA;
+    *last_dc = *last_dc + dc;
+
+    block[0] = sign_extend(*last_dc << (12 - dcb), 12);
+
+    q = quants[get_bits(gb, 2)];
+    if (q >= 128)
+        ac_idx = HQX_AC_Q128;
+    else if (q >= 64)
+        ac_idx = HQX_AC_Q64;
+    else if (q >= 32)
+        ac_idx = HQX_AC_Q32;
+    else if (q >= 16)
+        ac_idx = HQX_AC_Q16;
+    else if (q >= 8)
+        ac_idx = HQX_AC_Q8;
+    else
+        ac_idx = HQX_AC_Q0;
+
+    do {
+        hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev);
+        pos += run;
+        if (pos >= 64)
+            break;
+        block[ff_zigzag_direct[pos++]] = lev * q;
+    } while (pos < 64);
+
+    return 0;
+}
+
+static int hqx_decode_422(HQXContext *ctx, AVFrame *pic,
+                          GetBitContext *gb, int x, int y)
+{
+    const int *quants;
+    int flag;
+    int last_dc;
+    int i, ret;
+
+    if (ctx->interlaced)
+        flag = get_bits1(gb);
+    else
+        flag = 0;
+
+    quants = hqx_quants[get_bits(gb, 4)];
+
+    for (i = 0; i < 8; i++) {
+        if (i == 0 || i == 4 || i == 6)
+            last_dc = 0;
+        ret = decode_block(gb, quants, ctx->dcb, ctx->block[i], &last_dc);
+        if (ret < 0)
+            return ret;
+    }
+
+    put_blocks(pic, 0, x,      y, flag, ctx->block[0], ctx->block[2], 
hqx_quant_luma);
+    put_blocks(pic, 0, x + 8,  y, flag, ctx->block[1], ctx->block[3], 
hqx_quant_luma);
+    put_blocks(pic, 2, x >> 1, y, flag, ctx->block[4], ctx->block[5], 
hqx_quant_chroma);
+    put_blocks(pic, 1, x >> 1, y, flag, ctx->block[6], ctx->block[7], 
hqx_quant_chroma);
+
+    return 0;
+}
+
+static int hqx_decode_422a(HQXContext *ctx, AVFrame *pic,
+                           GetBitContext *gb, int x, int y)
+{
+    const int *quants;
+    int flag = 0;
+    int last_dc;
+    int i, ret;
+    int cbp;
+
+    cbp = get_vlc2(gb, ff_hqx_cbp_vlc.table, ff_hqx_cbp_vlc.bits, 1);
+
+    for (i = 0; i < 12; i++)
+        memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);
+    for (i = 0; i < 12; i++)
+        ctx->block[i][0] = -0x800;
+    if (cbp) {
+        if (ctx->interlaced)
+            flag = get_bits1(gb);
+
+        quants = hqx_quants[get_bits(gb, 4)];
+
+        cbp |= cbp << 4; // alpha CBP
+        if (cbp & 0x3)   // chroma CBP - top
+            cbp |= 0x500;
+        if (cbp & 0xC)   // chroma CBP - bottom
+            cbp |= 0xA00;
+        for (i = 0; i < 12; i++) {
+            if (i == 0 || i == 4 || i == 8 || i == 10)
+                last_dc = 0;
+            if (cbp & (1 << i)) {
+                ret = decode_block(gb, quants, ctx->dcb,
+                                   ctx->block[i], &last_dc);
+                if (ret < 0)
+                    return ret;
+            }
+        }
+    }
+
+    put_blocks(pic, 3, x,      y, flag, ctx->block[ 0], ctx->block[ 2], 
hqx_quant_luma);
+    put_blocks(pic, 3, x + 8,  y, flag, ctx->block[ 1], ctx->block[ 3], 
hqx_quant_luma);
+    put_blocks(pic, 0, x,      y, flag, ctx->block[ 4], ctx->block[ 6], 
hqx_quant_luma);
+    put_blocks(pic, 0, x + 8,  y, flag, ctx->block[ 5], ctx->block[ 7], 
hqx_quant_luma);
+    put_blocks(pic, 2, x >> 1, y, flag, ctx->block[ 8], ctx->block[ 9], 
hqx_quant_chroma);
+    put_blocks(pic, 1, x >> 1, y, flag, ctx->block[10], ctx->block[11], 
hqx_quant_chroma);
+
+    return 0;
+}
+
+static int hqx_decode_444(HQXContext *ctx, AVFrame *pic,
+                          GetBitContext *gb, int x, int y)
+{
+    const int *quants;
+    int flag;
+    int last_dc;
+    int i, ret;
+
+    if (ctx->interlaced)
+        flag = get_bits1(gb);
+    else
+        flag = 0;
+
+    quants = hqx_quants[get_bits(gb, 4)];
+
+    for (i = 0; i < 12; i++) {
+        if (i == 0 || i == 4 || i == 8)
+            last_dc = 0;
+        ret = decode_block(gb, quants, ctx->dcb, ctx->block[i], &last_dc);
+        if (ret < 0)
+            return ret;
+    }
+
+    put_blocks(pic, 0, x,     y, flag, ctx->block[0], ctx->block[ 2], 
hqx_quant_luma);
+    put_blocks(pic, 0, x + 8, y, flag, ctx->block[1], ctx->block[ 3], 
hqx_quant_luma);
+    put_blocks(pic, 2, x,     y, flag, ctx->block[4], ctx->block[ 6], 
hqx_quant_chroma);
+    put_blocks(pic, 2, x + 8, y, flag, ctx->block[5], ctx->block[ 7], 
hqx_quant_chroma);
+    put_blocks(pic, 1, x,     y, flag, ctx->block[8], ctx->block[10], 
hqx_quant_chroma);
+    put_blocks(pic, 1, x + 8, y, flag, ctx->block[9], ctx->block[11], 
hqx_quant_chroma);
+
+    return 0;
+}
+
+static int hqx_decode_444a(HQXContext *ctx, AVFrame *pic,
+                           GetBitContext *gb, int x, int y)
+{
+    const int *quants;
+    int flag = 0;
+    int last_dc;
+    int i, ret;
+    int cbp;
+
+    cbp = get_vlc2(gb, ff_hqx_cbp_vlc.table, ff_hqx_cbp_vlc.bits, 1);
+
+    for (i = 0; i < 16; i++)
+        memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);
+    for (i = 0; i < 16; i++)
+        ctx->block[i][0] = -0x800;
+    if (cbp) {
+        if (ctx->interlaced)
+            flag = get_bits1(gb);
+
+        quants = hqx_quants[get_bits(gb, 4)];
+
+        cbp |= cbp << 4; // alpha CBP
+        cbp |= cbp << 8; // chroma CBP
+        for (i = 0; i < 16; i++) {
+            if (i == 0 || i == 4 || i == 8 || i == 12)
+                last_dc = 0;
+            if (cbp & (1 << i)) {
+                ret = decode_block(gb, quants, ctx->dcb,
+                                   ctx->block[i], &last_dc);
+                if (ret < 0)
+                    return ret;
+            }
+        }
+    }
+
+    put_blocks(pic, 3, x,     y, flag, ctx->block[ 0], ctx->block[ 2], 
hqx_quant_luma);
+    put_blocks(pic, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], 
hqx_quant_luma);
+    put_blocks(pic, 0, x,     y, flag, ctx->block[ 4], ctx->block[ 6], 
hqx_quant_luma);
+    put_blocks(pic, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], 
hqx_quant_luma);
+    put_blocks(pic, 2, x,     y, flag, ctx->block[ 8], ctx->block[10], 
hqx_quant_chroma);
+    put_blocks(pic, 2, x + 8, y, flag, ctx->block[ 9], ctx->block[11], 
hqx_quant_chroma);
+    put_blocks(pic, 1, x,     y, flag, ctx->block[12], ctx->block[14], 
hqx_quant_chroma);
+    put_blocks(pic, 1, x + 8, y, flag, ctx->block[13], ctx->block[15], 
hqx_quant_chroma);
+
+    return 0;
+}
+
+static const int shuffle_16[16] = {
+    0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12
+};
+
+static int decode_slice(HQXContext *ctx, AVFrame *pic, GetBitContext *gb,
+                        int slice_no, mb_decode_func decode_func)
+{
+    int mb_w = (ctx->width  + 15) >> 4;
+    int mb_h = (ctx->height + 15) >> 4;
+    int grp_w = (mb_w + 4) / 5;
+    int grp_h = (mb_h + 4) / 5;
+    int grp_h_edge = grp_w * (mb_w / grp_w);
+    int grp_v_edge = grp_h * (mb_h / grp_h);
+    int grp_v_rest = mb_w - grp_h_edge;
+    int grp_h_rest = mb_h - grp_v_edge;
+    int num_mbs = mb_w * mb_h;
+    int num_tiles = (num_mbs + 479) / 480;
+    int std_tile_blocks = num_mbs / (16 * num_tiles);
+    int g_tile = slice_no * num_tiles;
+    int blk_addr, loc_addr, mb_x, mb_y, pos, loc_row, i;
+    int tile_blocks, tile_limit, tile_no;
+
+    for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) {
+        tile_blocks = std_tile_blocks;
+        tile_limit = -1;
+        if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) {
+            tile_limit = num_mbs / (16 * num_tiles);
+            tile_blocks++;
+        }
+        for (i = 0; i < tile_blocks; i++) {
+            if (i == tile_limit)
+                blk_addr = g_tile + 16 * num_tiles * i;
+            else
+                blk_addr = tile_no + 16 * num_tiles * i +
+                           num_tiles * shuffle_16[(i + slice_no) & 0xF];
+            loc_row  = grp_h * (blk_addr / (grp_h * mb_w));
+            loc_addr =          blk_addr % (grp_h * mb_w);
+            if (loc_row >= grp_v_edge) {
+                mb_x = grp_w * (loc_addr / (grp_h_rest * grp_w));
+                pos  =          loc_addr % (grp_h_rest * grp_w);
+            } else {
+                mb_x = grp_w * (loc_addr / (grp_h * grp_w));
+                pos  =          loc_addr % (grp_h * grp_w);
+            }
+            if (mb_x >= grp_h_edge) {
+                mb_x +=            pos % grp_v_rest;
+                mb_y  = loc_row + (pos / grp_v_rest);
+            } else {
+                mb_x +=            pos % grp_w;
+                mb_y  = loc_row + (pos / grp_w);
+            }
+            decode_func(ctx, pic, gb, mb_x * 16, mb_y * 16);
+        }
+    }
+
+    return 0;
+}
+
+static int hqx_decode_frame(AVCodecContext *avctx, void *data,
+                            int *got_picture_ptr, AVPacket *avpkt)
+{
+    HQXContext *ctx = avctx->priv_data;
+    AVFrame *pic = 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;
+
+    /* Skip the INFO header if present */
+    info_offset = 0;


if (avpkt->size < 4 + 8)
    return ...
might be nice (the packet should have 16bytes of padding at the end so it would stay fine, but better be explicit)

lu
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to