4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/imgutils.h"
24 #include "libavutil/intreadwrite.h"
33 /* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */
41 #define HQX_HEADER_SIZE 59
43 /* macroblock selects a group of 4 possible quants and
44 * a block can use any of those four quantisers
45 * one column is powers of 2, the other one is powers of 2 * 3,
46 * then there is the special one, powers of 2 * 5 */
47 static const int hqx_quants[16][4] = {
48 { 0x1, 0x2, 0x4, 0x8 }, { 0x1, 0x3, 0x6, 0xC },
49 { 0x2, 0x4, 0x8, 0x10 }, { 0x3, 0x6, 0xC, 0x18 },
50 { 0x4, 0x8, 0x10, 0x20 }, { 0x6, 0xC, 0x18, 0x30 },
51 { 0x8, 0x10, 0x20, 0x40 },
52 { 0xA, 0x14, 0x28, 0x50 },
53 { 0xC, 0x18, 0x30, 0x60 },
54 { 0x10, 0x20, 0x40, 0x80 }, { 0x18, 0x30, 0x60, 0xC0 },
55 { 0x20, 0x40, 0x80, 0x100 }, { 0x30, 0x60, 0xC0, 0x180 },
56 { 0x40, 0x80, 0x100, 0x200 }, { 0x60, 0xC0, 0x180, 0x300 },
57 { 0x80, 0x100, 0x200, 0x400 }
60 static const uint8_t hqx_quant_luma[64] = {
61 16, 16, 16, 19, 19, 19, 42, 44,
62 16, 16, 19, 19, 19, 38, 43, 45,
63 16, 19, 19, 19, 40, 41, 45, 48,
64 19, 19, 19, 40, 41, 42, 46, 49,
65 19, 19, 40, 41, 42, 43, 48, 101,
66 19, 38, 41, 42, 43, 44, 98, 104,
67 42, 43, 45, 46, 48, 98, 109, 116,
68 44, 45, 48, 49, 101, 104, 116, 123,
71 static const uint8_t hqx_quant_chroma[64] = {
72 16, 16, 19, 25, 26, 26, 42, 44,
73 16, 19, 25, 25, 26, 38, 43, 91,
74 19, 25, 26, 27, 40, 41, 91, 96,
75 25, 25, 27, 40, 41, 84, 93, 197,
76 26, 26, 40, 41, 84, 86, 191, 203,
77 26, 38, 41, 84, 86, 177, 197, 209,
78 42, 43, 91, 93, 191, 197, 219, 232,
79 44, 91, 96, 197, 203, 209, 232, 246,
82 static inline void put_blocks(HQXContext *ctx, int plane,
83 int x, int y, int ilace,
84 int16_t *block0, int16_t *block1,
87 int fields = ilace ? 2 : 1;
88 int lsize = ctx->pic->linesize[plane];
89 uint8_t *p = ctx->pic->data[plane] + x * 2;
91 ctx->hqxdsp.idct_put((uint16_t *)(p + y * lsize),
92 lsize * fields, block0, quant);
93 ctx->hqxdsp.idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),
94 lsize * fields, block1, quant);
97 static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac,
102 val = show_bits(gb, ac->lut_bits);
103 if (ac->lut[val].bits == -1) {
104 GetBitContext gb2 = *gb;
105 skip_bits(&gb2, ac->lut_bits);
106 val = ac->lut[val].lev + show_bits(&gb2, ac->extra_bits);
108 *run = ac->lut[val].run;
109 *lev = ac->lut[val].lev;
110 skip_bits(gb, ac->lut[val].bits);
113 static int decode_block(GetBitContext *gb, VLC *vlc,
114 const int *quants, int dcb,
115 int16_t block[64], int *last_dc)
119 int run, lev, pos = 1;
121 memset(block, 0, 64 * sizeof(*block));
122 dc = get_vlc2(gb, vlc->table, HQX_DC_VLC_BITS, 2);
124 return AVERROR_INVALIDDATA;
127 block[0] = sign_extend(*last_dc << (12 - dcb), 12);
129 q = quants[get_bits(gb, 2)];
131 ac_idx = HQX_AC_Q128;
144 hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev);
148 block[ff_zigzag_direct[pos++]] = lev * q;
154 static int hqx_decode_422(HQXContext *ctx, GetBitContext *gb, int x, int y)
162 flag = get_bits1(gb);
166 quants = hqx_quants[get_bits(gb, 4)];
168 for (i = 0; i < 8; i++) {
169 int vlc_index = ctx->dcb - 9;
170 if (i == 0 || i == 4 || i == 6)
172 ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
173 ctx->dcb, ctx->block[i], &last_dc);
178 put_blocks(ctx, 0, x, y, flag, ctx->block[0], ctx->block[2], hqx_quant_luma);
179 put_blocks(ctx, 0, x + 8, y, flag, ctx->block[1], ctx->block[3], hqx_quant_luma);
180 put_blocks(ctx, 2, x >> 1, y, flag, ctx->block[4], ctx->block[5], hqx_quant_chroma);
181 put_blocks(ctx, 1, x >> 1, y, flag, ctx->block[6], ctx->block[7], hqx_quant_chroma);
186 static int hqx_decode_422a(HQXContext *ctx, GetBitContext *gb, int x, int y)
194 cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
196 for (i = 0; i < 12; i++)
197 memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);
198 for (i = 0; i < 12; i++)
199 ctx->block[i][0] = -0x800;
202 flag = get_bits1(gb);
204 quants = hqx_quants[get_bits(gb, 4)];
206 cbp |= cbp << 4; // alpha CBP
207 if (cbp & 0x3) // chroma CBP - top
209 if (cbp & 0xC) // chroma CBP - bottom
211 for (i = 0; i < 12; i++) {
212 if (i == 0 || i == 4 || i == 8 || i == 10)
214 if (cbp & (1 << i)) {
215 int vlc_index = ctx->dcb - 9;
216 ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
217 ctx->dcb, ctx->block[i], &last_dc);
224 put_blocks(ctx, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma);
225 put_blocks(ctx, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma);
226 put_blocks(ctx, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma);
227 put_blocks(ctx, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma);
228 put_blocks(ctx, 2, x >> 1, y, flag, ctx->block[ 8], ctx->block[ 9], hqx_quant_chroma);
229 put_blocks(ctx, 1, x >> 1, y, flag, ctx->block[10], ctx->block[11], hqx_quant_chroma);
234 static int hqx_decode_444(HQXContext *ctx, GetBitContext *gb, int x, int y)
242 flag = get_bits1(gb);
246 quants = hqx_quants[get_bits(gb, 4)];
248 for (i = 0; i < 12; i++) {
249 int vlc_index = ctx->dcb - 9;
250 if (i == 0 || i == 4 || i == 8)
252 ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
253 ctx->dcb, ctx->block[i], &last_dc);
258 put_blocks(ctx, 0, x, y, flag, ctx->block[0], ctx->block[ 2], hqx_quant_luma);
259 put_blocks(ctx, 0, x + 8, y, flag, ctx->block[1], ctx->block[ 3], hqx_quant_luma);
260 put_blocks(ctx, 2, x, y, flag, ctx->block[4], ctx->block[ 6], hqx_quant_chroma);
261 put_blocks(ctx, 2, x + 8, y, flag, ctx->block[5], ctx->block[ 7], hqx_quant_chroma);
262 put_blocks(ctx, 1, x, y, flag, ctx->block[8], ctx->block[10], hqx_quant_chroma);
263 put_blocks(ctx, 1, x + 8, y, flag, ctx->block[9], ctx->block[11], hqx_quant_chroma);
268 static int hqx_decode_444a(HQXContext *ctx, GetBitContext *gb, int x, int y)
276 cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
278 for (i = 0; i < 16; i++)
279 memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);
280 for (i = 0; i < 16; i++)
281 ctx->block[i][0] = -0x800;
284 flag = get_bits1(gb);
286 quants = hqx_quants[get_bits(gb, 4)];
288 cbp |= cbp << 4; // alpha CBP
289 cbp |= cbp << 8; // chroma CBP
290 for (i = 0; i < 16; i++) {
291 if (i == 0 || i == 4 || i == 8 || i == 12)
293 if (cbp & (1 << i)) {
294 int vlc_index = ctx->dcb - 9;
295 ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
296 ctx->dcb, ctx->block[i], &last_dc);
303 put_blocks(ctx, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma);
304 put_blocks(ctx, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma);
305 put_blocks(ctx, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma);
306 put_blocks(ctx, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma);
307 put_blocks(ctx, 2, x, y, flag, ctx->block[ 8], ctx->block[10], hqx_quant_chroma);
308 put_blocks(ctx, 2, x + 8, y, flag, ctx->block[ 9], ctx->block[11], hqx_quant_chroma);
309 put_blocks(ctx, 1, x, y, flag, ctx->block[12], ctx->block[14], hqx_quant_chroma);
310 put_blocks(ctx, 1, x + 8, y, flag, ctx->block[13], ctx->block[15], hqx_quant_chroma);
315 static const int shuffle_16[16] = {
316 0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12
319 static int decode_slice(HQXContext *ctx, GetBitContext *gb, int slice_no)
321 int mb_w = (ctx->width + 15) >> 4;
322 int mb_h = (ctx->height + 15) >> 4;
323 int grp_w = (mb_w + 4) / 5;
324 int grp_h = (mb_h + 4) / 5;
325 int grp_h_edge = grp_w * (mb_w / grp_w);
326 int grp_v_edge = grp_h * (mb_h / grp_h);
327 int grp_v_rest = mb_w - grp_h_edge;
328 int grp_h_rest = mb_h - grp_v_edge;
329 int num_mbs = mb_w * mb_h;
330 int num_tiles = (num_mbs + 479) / 480;
331 int std_tile_blocks = num_mbs / (16 * num_tiles);
332 int g_tile = slice_no * num_tiles;
333 int blk_addr, loc_addr, mb_x, mb_y, pos, loc_row, i;
334 int tile_blocks, tile_limit, tile_no;
336 for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) {
337 tile_blocks = std_tile_blocks;
339 if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) {
340 tile_limit = num_mbs / (16 * num_tiles);
343 for (i = 0; i < tile_blocks; i++) {
345 blk_addr = g_tile + 16 * num_tiles * i;
347 blk_addr = tile_no + 16 * num_tiles * i +
348 num_tiles * shuffle_16[(i + slice_no) & 0xF];
349 loc_row = grp_h * (blk_addr / (grp_h * mb_w));
350 loc_addr = blk_addr % (grp_h * mb_w);
351 if (loc_row >= grp_v_edge) {
352 mb_x = grp_w * (loc_addr / (grp_h_rest * grp_w));
353 pos = loc_addr % (grp_h_rest * grp_w);
355 mb_x = grp_w * (loc_addr / (grp_h * grp_w));
356 pos = loc_addr % (grp_h * grp_w);
358 if (mb_x >= grp_h_edge) {
359 mb_x += pos % grp_v_rest;
360 mb_y = loc_row + (pos / grp_v_rest);
363 mb_y = loc_row + (pos / grp_w);
365 ctx->decode_func(ctx, gb, mb_x * 16, mb_y * 16);
372 static int hqx_decode_frame(AVCodecContext *avctx, void *data,
373 int *got_picture_ptr, AVPacket *avpkt)
375 HQXContext *ctx = avctx->priv_data;
376 uint8_t *src = avpkt->data;
377 uint32_t info_tag, info_offset;
384 return AVERROR_INVALIDDATA;
386 /* Skip the INFO header if present */
388 info_tag = AV_RL32(src);
389 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
390 info_offset = AV_RL32(src + 4);
391 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
392 av_log(avctx, AV_LOG_ERROR,
393 "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
395 return AVERROR_INVALIDDATA;
401 av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
404 data_start = src - avpkt->data;
405 ctx->data_size = avpkt->size - data_start;
409 if (ctx->data_size < HQX_HEADER_SIZE) {
410 av_log(avctx, AV_LOG_ERROR, "Frame too small.\n");
411 return AVERROR_INVALIDDATA;
414 if (src[0] != 'H' || src[1] != 'Q') {
415 av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n");
416 return AVERROR_INVALIDDATA;
418 ctx->interlaced = !(src[2] & 0x80);
419 ctx->format = src[2] & 7;
420 ctx->dcb = (src[3] & 3) + 8;
421 ctx->width = AV_RB16(src + 4);
422 ctx->height = AV_RB16(src + 6);
423 for (i = 0; i < 17; i++)
424 ctx->slice_off[i] = AV_RB24(src + 8 + i * 3);
427 av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb);
428 return AVERROR_INVALIDDATA;
430 ret = av_image_check_size(ctx->width, ctx->height, 0, avctx);
432 av_log(avctx, AV_LOG_ERROR, "Invalid stored dimenstions %dx%d.\n",
433 ctx->width, ctx->height);
434 return AVERROR_INVALIDDATA;
437 avctx->coded_width = FFALIGN(ctx->width, 16);
438 avctx->coded_height = FFALIGN(ctx->height, 16);
439 avctx->width = ctx->width;
440 avctx->height = ctx->height;
441 avctx->bits_per_raw_sample = 10;
443 switch (ctx->format) {
445 avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
446 ctx->decode_func = hqx_decode_422;
449 avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
450 ctx->decode_func = hqx_decode_444;
453 avctx->pix_fmt = AV_PIX_FMT_YUVA422P16;
454 ctx->decode_func = hqx_decode_422a;
457 avctx->pix_fmt = AV_PIX_FMT_YUVA444P16;
458 ctx->decode_func = hqx_decode_444a;
461 av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format);
462 return AVERROR_INVALIDDATA;
465 ret = ff_get_buffer(avctx, ctx->pic, 0);
467 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
471 for (slice = 0; slice < 16; slice++) {
472 if (ctx->slice_off[slice] < HQX_HEADER_SIZE ||
473 ctx->slice_off[slice] >= ctx->slice_off[slice + 1] ||
474 ctx->slice_off[slice + 1] > ctx->data_size) {
475 av_log(avctx, AV_LOG_ERROR, "Invalid slice size.\n");
478 ret = init_get_bits(&gb, src + ctx->slice_off[slice],
479 (ctx->slice_off[slice + 1] - ctx->slice_off[slice]) * 8);
482 ret = decode_slice(ctx, &gb, slice);
484 av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d.\n", slice);
488 ctx->pic->key_frame = 1;
489 ctx->pic->pict_type = AV_PICTURE_TYPE_I;
491 *got_picture_ptr = 1;
496 static av_cold int hqx_decode_close(AVCodecContext *avctx)
499 HQXContext *ctx = avctx->priv_data;
501 ff_free_vlc(&ctx->cbp_vlc);
502 for (i = 0; i < 3; i++) {
503 ff_free_vlc(&ctx->dc_vlc[i]);
509 static av_cold int hqx_decode_init(AVCodecContext *avctx)
511 HQXContext *ctx = avctx->priv_data;
512 int ret = ff_hqx_init_vlcs(ctx);
514 hqx_decode_close(avctx);
516 ff_hqxdsp_init(&ctx->hqxdsp);
521 AVCodec ff_hqx_decoder = {
523 .long_name = NULL_IF_CONFIG_SMALL("Canopus HQX"),
524 .type = AVMEDIA_TYPE_VIDEO,
525 .id = AV_CODEC_ID_HQX,
526 .priv_data_size = sizeof(HQXContext),
527 .init = hqx_decode_init,
528 .decode = hqx_decode_frame,
529 .close = hqx_decode_close,
530 .capabilities = CODEC_CAP_DR1,