hqx.c 18.1 KB
Newer Older
Vittorio Giovara's avatar
Vittorio Giovara committed
1 2 3
/*
 * Canopus HQX decoder
 *
4
 * This file is part of FFmpeg.
Vittorio Giovara's avatar
Vittorio Giovara committed
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
Vittorio Giovara's avatar
Vittorio Giovara committed
7 8 9 10
 * 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.
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
Vittorio Giovara's avatar
Vittorio Giovara committed
12 13 14 15 16
 * 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
17
 * License along with FFmpeg; if not, write to the Free Software
Vittorio Giovara's avatar
Vittorio Giovara committed
18 19 20 21 22 23 24 25 26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <inttypes.h>

#include "libavutil/imgutils.h"
#include "libavutil/intreadwrite.h"

#include "avcodec.h"
27
#include "canopus.h"
Vittorio Giovara's avatar
Vittorio Giovara committed
28 29
#include "get_bits.h"
#include "internal.h"
30
#include "thread.h"
Vittorio Giovara's avatar
Vittorio Giovara committed
31 32

#include "hqx.h"
33
#include "hqxdsp.h"
Vittorio Giovara's avatar
Vittorio Giovara committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

/* 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

/* 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,
};

84
static inline void put_blocks(HQXContext *ctx, int plane,
Vittorio Giovara's avatar
Vittorio Giovara committed
85 86 87 88 89
                              int x, int y, int ilace,
                              int16_t *block0, int16_t *block1,
                              const uint8_t *quant)
{
    int fields = ilace ? 2 : 1;
90 91
    int lsize = ctx->pic->linesize[plane];
    uint8_t *p = ctx->pic->data[plane] + x * 2;
Vittorio Giovara's avatar
Vittorio Giovara committed
92

93 94 95 96
    ctx->hqxdsp.idct_put((uint16_t *)(p + y * lsize),
                         lsize * fields, block0, quant);
    ctx->hqxdsp.idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),
                         lsize * fields, block1, quant);
Vittorio Giovara's avatar
Vittorio Giovara committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
}

static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac,
                              int *run, int *lev)
{
    int val;

    val = show_bits(gb, ac->lut_bits);
    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, VLC *vlc,
                        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, vlc->table, HQX_DC_VLC_BITS, 2);
    if (dc < 0)
        return AVERROR_INVALIDDATA;
    *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;
}

156
static int hqx_decode_422(HQXContext *ctx, int slice_no, int x, int y)
Vittorio Giovara's avatar
Vittorio Giovara committed
157
{
158 159
    HQXSlice *slice = &ctx->slice[slice_no];
    GetBitContext *gb = &slice->gb;
Vittorio Giovara's avatar
Vittorio Giovara committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    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++) {
        int vlc_index = ctx->dcb - 9;
        if (i == 0 || i == 4 || i == 6)
            last_dc = 0;
        ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
177
                           ctx->dcb, slice->block[i], &last_dc);
Vittorio Giovara's avatar
Vittorio Giovara committed
178 179 180 181
        if (ret < 0)
            return ret;
    }

182 183 184 185
    put_blocks(ctx, 0, x,      y, flag, slice->block[0], slice->block[2], hqx_quant_luma);
    put_blocks(ctx, 0, x + 8,  y, flag, slice->block[1], slice->block[3], hqx_quant_luma);
    put_blocks(ctx, 2, x >> 1, y, flag, slice->block[4], slice->block[5], hqx_quant_chroma);
    put_blocks(ctx, 1, x >> 1, y, flag, slice->block[6], slice->block[7], hqx_quant_chroma);
Vittorio Giovara's avatar
Vittorio Giovara committed
186 187 188 189

    return 0;
}

190
static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y)
Vittorio Giovara's avatar
Vittorio Giovara committed
191
{
192 193
    HQXSlice *slice = &ctx->slice[slice_no];
    GetBitContext *gb = &slice->gb;
Vittorio Giovara's avatar
Vittorio Giovara committed
194 195 196 197 198 199 200 201 202
    const int *quants;
    int flag = 0;
    int last_dc;
    int i, ret;
    int cbp;

    cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);

    for (i = 0; i < 12; i++)
203
        memset(slice->block[i], 0, sizeof(**slice->block) * 64);
Vittorio Giovara's avatar
Vittorio Giovara committed
204
    for (i = 0; i < 12; i++)
205
        slice->block[i][0] = -0x800;
Vittorio Giovara's avatar
Vittorio Giovara committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    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)) {
                int vlc_index = ctx->dcb - 9;
                ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
223
                                   ctx->dcb, slice->block[i], &last_dc);
Vittorio Giovara's avatar
Vittorio Giovara committed
224 225 226 227 228 229
                if (ret < 0)
                    return ret;
            }
        }
    }

230 231 232 233 234 235
    put_blocks(ctx, 3, x,      y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma);
    put_blocks(ctx, 3, x + 8,  y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma);
    put_blocks(ctx, 0, x,      y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma);
    put_blocks(ctx, 0, x + 8,  y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma);
    put_blocks(ctx, 2, x >> 1, y, flag, slice->block[ 8], slice->block[ 9], hqx_quant_chroma);
    put_blocks(ctx, 1, x >> 1, y, flag, slice->block[10], slice->block[11], hqx_quant_chroma);
Vittorio Giovara's avatar
Vittorio Giovara committed
236 237 238 239

    return 0;
}

240
static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y)
Vittorio Giovara's avatar
Vittorio Giovara committed
241
{
242 243
    HQXSlice *slice = &ctx->slice[slice_no];
    GetBitContext *gb = &slice->gb;
Vittorio Giovara's avatar
Vittorio Giovara committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    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++) {
        int vlc_index = ctx->dcb - 9;
        if (i == 0 || i == 4 || i == 8)
            last_dc = 0;
        ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
261
                           ctx->dcb, slice->block[i], &last_dc);
Vittorio Giovara's avatar
Vittorio Giovara committed
262 263 264 265
        if (ret < 0)
            return ret;
    }

266 267 268 269 270 271
    put_blocks(ctx, 0, x,     y, flag, slice->block[0], slice->block[ 2], hqx_quant_luma);
    put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[ 3], hqx_quant_luma);
    put_blocks(ctx, 2, x,     y, flag, slice->block[4], slice->block[ 6], hqx_quant_chroma);
    put_blocks(ctx, 2, x + 8, y, flag, slice->block[5], slice->block[ 7], hqx_quant_chroma);
    put_blocks(ctx, 1, x,     y, flag, slice->block[8], slice->block[10], hqx_quant_chroma);
    put_blocks(ctx, 1, x + 8, y, flag, slice->block[9], slice->block[11], hqx_quant_chroma);
Vittorio Giovara's avatar
Vittorio Giovara committed
272 273 274 275

    return 0;
}

276
static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y)
Vittorio Giovara's avatar
Vittorio Giovara committed
277
{
278 279
    HQXSlice *slice = &ctx->slice[slice_no];
    GetBitContext *gb = &slice->gb;
Vittorio Giovara's avatar
Vittorio Giovara committed
280 281 282 283 284 285 286 287 288
    const int *quants;
    int flag = 0;
    int last_dc;
    int i, ret;
    int cbp;

    cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);

    for (i = 0; i < 16; i++)
289
        memset(slice->block[i], 0, sizeof(**slice->block) * 64);
Vittorio Giovara's avatar
Vittorio Giovara committed
290
    for (i = 0; i < 16; i++)
291
        slice->block[i][0] = -0x800;
Vittorio Giovara's avatar
Vittorio Giovara committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305
    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)) {
                int vlc_index = ctx->dcb - 9;
                ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
306
                                   ctx->dcb, slice->block[i], &last_dc);
Vittorio Giovara's avatar
Vittorio Giovara committed
307 308 309 310 311 312
                if (ret < 0)
                    return ret;
            }
        }
    }

313 314 315 316 317 318 319 320
    put_blocks(ctx, 3, x,     y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma);
    put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma);
    put_blocks(ctx, 0, x,     y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma);
    put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma);
    put_blocks(ctx, 2, x,     y, flag, slice->block[ 8], slice->block[10], hqx_quant_chroma);
    put_blocks(ctx, 2, x + 8, y, flag, slice->block[ 9], slice->block[11], hqx_quant_chroma);
    put_blocks(ctx, 1, x,     y, flag, slice->block[12], slice->block[14], hqx_quant_chroma);
    put_blocks(ctx, 1, x + 8, y, flag, slice->block[13], slice->block[15], hqx_quant_chroma);
Vittorio Giovara's avatar
Vittorio Giovara committed
321 322 323 324 325 326 327 328

    return 0;
}

static const int shuffle_16[16] = {
    0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12
};

329
static int decode_slice(HQXContext *ctx, int slice_no)
Vittorio Giovara's avatar
Vittorio Giovara committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
{
    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);
            }
375
            ctx->decode_func(ctx, slice_no, mb_x * 16, mb_y * 16);
Vittorio Giovara's avatar
Vittorio Giovara committed
376 377 378 379 380 381
        }
    }

    return 0;
}

382 383
static int decode_slice_thread(AVCodecContext *avctx, void *arg,
                               int slice_no, int threadnr)
384 385
{
    HQXContext *ctx = avctx->priv_data;
386
    uint32_t *slice_off = ctx->slice_off;
387 388
    int ret;

389 390 391 392
    if (slice_off[slice_no] < HQX_HEADER_SIZE ||
        slice_off[slice_no] >= slice_off[slice_no + 1] ||
        slice_off[slice_no + 1] > ctx->data_size) {
        av_log(avctx, AV_LOG_ERROR, "Invalid slice size %d.\n", ctx->data_size);
393 394 395
        return AVERROR_INVALIDDATA;
    }

396 397 398
    ret = init_get_bits8(&ctx->slice[slice_no].gb,
                         ctx->src + slice_off[slice_no],
                         slice_off[slice_no + 1] - slice_off[slice_no]);
399 400 401
    if (ret < 0)
        return ret;

402
    return decode_slice(ctx, slice_no);
403 404
}

Vittorio Giovara's avatar
Vittorio Giovara committed
405 406 407 408
static int hqx_decode_frame(AVCodecContext *avctx, void *data,
                            int *got_picture_ptr, AVPacket *avpkt)
{
    HQXContext *ctx = avctx->priv_data;
409
    ThreadFrame frame = { .f = data };
Vittorio Giovara's avatar
Vittorio Giovara committed
410
    uint8_t *src = avpkt->data;
411
    uint32_t info_tag;
Vittorio Giovara's avatar
Vittorio Giovara committed
412 413 414
    int data_start;
    int i, ret;

415 416
    if (avpkt->size < 4 + 4) {
        av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
Vittorio Giovara's avatar
Vittorio Giovara committed
417
        return AVERROR_INVALIDDATA;
418
    }
Vittorio Giovara's avatar
Vittorio Giovara committed
419 420 421

    info_tag    = AV_RL32(src);
    if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
422
        uint32_t info_offset = AV_RL32(src + 4);
423
        if (info_offset > INT_MAX || info_offset + 8 > avpkt->size) {
Vittorio Giovara's avatar
Vittorio Giovara committed
424 425 426 427 428
            av_log(avctx, AV_LOG_ERROR,
                   "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
                   info_offset);
            return AVERROR_INVALIDDATA;
        }
429
        ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
Vittorio Giovara's avatar
Vittorio Giovara committed
430 431 432 433 434

        info_offset += 8;
        src         += info_offset;
    }

435 436 437 438
    data_start     = src - avpkt->data;
    ctx->data_size = avpkt->size - data_start;
    ctx->src       = src;
    ctx->pic       = data;
Vittorio Giovara's avatar
Vittorio Giovara committed
439

440
    if (ctx->data_size < HQX_HEADER_SIZE) {
Vittorio Giovara's avatar
Vittorio Giovara committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454
        av_log(avctx, AV_LOG_ERROR, "Frame too small.\n");
        return AVERROR_INVALIDDATA;
    }

    if (src[0] != 'H' || src[1] != 'Q') {
        av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n");
        return AVERROR_INVALIDDATA;
    }
    ctx->interlaced = !(src[2] & 0x80);
    ctx->format     = src[2] & 7;
    ctx->dcb        = (src[3] & 3) + 8;
    ctx->width      = AV_RB16(src + 4);
    ctx->height     = AV_RB16(src + 6);
    for (i = 0; i < 17; i++)
455
        ctx->slice_off[i] = AV_RB24(src + 8 + i * 3);
Vittorio Giovara's avatar
Vittorio Giovara committed
456 457 458 459 460 461 462

    if (ctx->dcb == 8) {
        av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb);
        return AVERROR_INVALIDDATA;
    }
    ret = av_image_check_size(ctx->width, ctx->height, 0, avctx);
    if (ret < 0) {
Paul B Mahol's avatar
Paul B Mahol committed
463
        av_log(avctx, AV_LOG_ERROR, "Invalid stored dimensions %dx%d.\n",
Vittorio Giovara's avatar
Vittorio Giovara committed
464 465 466 467 468 469 470 471 472 473
               ctx->width, ctx->height);
        return AVERROR_INVALIDDATA;
    }

    avctx->coded_width         = FFALIGN(ctx->width,  16);
    avctx->coded_height        = FFALIGN(ctx->height, 16);
    avctx->width               = ctx->width;
    avctx->height              = ctx->height;
    avctx->bits_per_raw_sample = 10;

474 475 476 477 478 479 480 481
    //The minimum size is 2bit per macroblock
    // hqx_decode_422 & hqx_decode_444 have a unconditionally stored 4bits hqx_quants index
    // hqx_decode_422a & hqx_decode_444a use cbp_vlc which has a minimum length of 2 bits for its VLCs
    // The code rejects slices overlapping in their input data
    if (avctx->coded_width / 16 * (avctx->coded_height / 16) *
        (100 - avctx->discard_damaged_percentage) / 100 > 4LL * avpkt->size)
        return AVERROR_INVALIDDATA;

Vittorio Giovara's avatar
Vittorio Giovara committed
482 483 484
    switch (ctx->format) {
    case HQX_422:
        avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
485
        ctx->decode_func = hqx_decode_422;
Vittorio Giovara's avatar
Vittorio Giovara committed
486 487 488
        break;
    case HQX_444:
        avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
489
        ctx->decode_func = hqx_decode_444;
Vittorio Giovara's avatar
Vittorio Giovara committed
490 491 492
        break;
    case HQX_422A:
        avctx->pix_fmt = AV_PIX_FMT_YUVA422P16;
493
        ctx->decode_func = hqx_decode_422a;
Vittorio Giovara's avatar
Vittorio Giovara committed
494 495 496
        break;
    case HQX_444A:
        avctx->pix_fmt = AV_PIX_FMT_YUVA444P16;
497
        ctx->decode_func = hqx_decode_444a;
Vittorio Giovara's avatar
Vittorio Giovara committed
498
        break;
499
    default:
Vittorio Giovara's avatar
Vittorio Giovara committed
500 501 502 503
        av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format);
        return AVERROR_INVALIDDATA;
    }

504
    ret = ff_thread_get_buffer(avctx, &frame, 0);
505
    if (ret < 0)
Vittorio Giovara's avatar
Vittorio Giovara committed
506 507
        return ret;

508
    avctx->execute2(avctx, decode_slice_thread, NULL, NULL, 16);
509

510 511
    ctx->pic->key_frame = 1;
    ctx->pic->pict_type = AV_PICTURE_TYPE_I;
Vittorio Giovara's avatar
Vittorio Giovara committed
512 513 514 515 516 517 518 519 520 521 522

    *got_picture_ptr = 1;

    return avpkt->size;
}

static av_cold int hqx_decode_close(AVCodecContext *avctx)
{
    int i;
    HQXContext *ctx = avctx->priv_data;

523 524 525
    if (avctx->internal->is_copy)
        return 0;

Vittorio Giovara's avatar
Vittorio Giovara committed
526 527 528 529 530 531 532 533 534 535 536
    ff_free_vlc(&ctx->cbp_vlc);
    for (i = 0; i < 3; i++) {
        ff_free_vlc(&ctx->dc_vlc[i]);
    }

    return 0;
}

static av_cold int hqx_decode_init(AVCodecContext *avctx)
{
    HQXContext *ctx = avctx->priv_data;
537 538 539

    ff_hqxdsp_init(&ctx->hqxdsp);

540
    return ff_hqx_init_vlcs(ctx);
Vittorio Giovara's avatar
Vittorio Giovara committed
541 542 543 544 545 546 547 548 549 550 551
}

AVCodec ff_hqx_decoder = {
    .name           = "hqx",
    .long_name      = NULL_IF_CONFIG_SMALL("Canopus HQX"),
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = AV_CODEC_ID_HQX,
    .priv_data_size = sizeof(HQXContext),
    .init           = hqx_decode_init,
    .decode         = hqx_decode_frame,
    .close          = hqx_decode_close,
552 553
    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
                      AV_CODEC_CAP_FRAME_THREADS,
554 555
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
                      FF_CODEC_CAP_INIT_CLEANUP,
Vittorio Giovara's avatar
Vittorio Giovara committed
556
};