4 * This file is part of FFmpeg.
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
34 /* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */
42 #define HQX_HEADER_SIZE 59
44 /* macroblock selects a group of 4 possible quants and
45 * a block can use any of those four quantisers
46 * one column is powers of 2, the other one is powers of 2 * 3,
47 * then there is the special one, powers of 2 * 5 */
48 static const int hqx_quants[16][4] = {
49 { 0x1, 0x2, 0x4, 0x8 }, { 0x1, 0x3, 0x6, 0xC },
50 { 0x2, 0x4, 0x8, 0x10 }, { 0x3, 0x6, 0xC, 0x18 },
51 { 0x4, 0x8, 0x10, 0x20 }, { 0x6, 0xC, 0x18, 0x30 },
52 { 0x8, 0x10, 0x20, 0x40 },
53 { 0xA, 0x14, 0x28, 0x50 },
54 { 0xC, 0x18, 0x30, 0x60 },
55 { 0x10, 0x20, 0x40, 0x80 }, { 0x18, 0x30, 0x60, 0xC0 },
56 { 0x20, 0x40, 0x80, 0x100 }, { 0x30, 0x60, 0xC0, 0x180 },
57 { 0x40, 0x80, 0x100, 0x200 }, { 0x60, 0xC0, 0x180, 0x300 },
58 { 0x80, 0x100, 0x200, 0x400 }
61 static const uint8_t hqx_quant_luma[64] = {
62 16, 16, 16, 19, 19, 19, 42, 44,
63 16, 16, 19, 19, 19, 38, 43, 45,
64 16, 19, 19, 19, 40, 41, 45, 48,
65 19, 19, 19, 40, 41, 42, 46, 49,
66 19, 19, 40, 41, 42, 43, 48, 101,
67 19, 38, 41, 42, 43, 44, 98, 104,
68 42, 43, 45, 46, 48, 98, 109, 116,
69 44, 45, 48, 49, 101, 104, 116, 123,
72 static const uint8_t hqx_quant_chroma[64] = {
73 16, 16, 19, 25, 26, 26, 42, 44,
74 16, 19, 25, 25, 26, 38, 43, 91,
75 19, 25, 26, 27, 40, 41, 91, 96,
76 25, 25, 27, 40, 41, 84, 93, 197,
77 26, 26, 40, 41, 84, 86, 191, 203,
78 26, 38, 41, 84, 86, 177, 197, 209,
79 42, 43, 91, 93, 191, 197, 219, 232,
80 44, 91, 96, 197, 203, 209, 232, 246,
83 static inline void put_blocks(HQXContext *ctx, int plane,
84 int x, int y, int ilace,
85 int16_t *block0, int16_t *block1,
88 int fields = ilace ? 2 : 1;
89 int lsize = ctx->pic->linesize[plane];
90 uint8_t *p = ctx->pic->data[plane] + x * 2;
92 ctx->hqxdsp.idct_put((uint16_t *)(p + y * lsize),
93 lsize * fields, block0, quant);
94 ctx->hqxdsp.idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),
95 lsize * fields, block1, quant);
98 static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac,
103 val = show_bits(gb, ac->lut_bits);
104 if (ac->lut[val].bits == -1) {
105 GetBitContext gb2 = *gb;
106 skip_bits(&gb2, ac->lut_bits);
107 val = ac->lut[val].lev + show_bits(&gb2, ac->extra_bits);
109 *run = ac->lut[val].run;
110 *lev = ac->lut[val].lev;
111 skip_bits(gb, ac->lut[val].bits);
114 static int decode_block(GetBitContext *gb, VLC *vlc,
115 const int *quants, int dcb,
116 int16_t block[64], int *last_dc)
120 int run, lev, pos = 1;
122 memset(block, 0, 64 * sizeof(*block));
123 dc = get_vlc2(gb, vlc->table, HQX_DC_VLC_BITS, 2);
125 return AVERROR_INVALIDDATA;
128 block[0] = sign_extend(*last_dc << (12 - dcb), 12);
130 q = quants[get_bits(gb, 2)];
132 ac_idx = HQX_AC_Q128;
145 hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev);
149 block[ff_zigzag_direct[pos++]] = lev * q;
155 static int hqx_decode_422(HQXContext *ctx, int slice_no, int x, int y)
157 HQXSlice *slice = &ctx->slice[slice_no];
158 GetBitContext *gb = &slice->gb;
165 flag = get_bits1(gb);
169 quants = hqx_quants[get_bits(gb, 4)];
171 for (i = 0; i < 8; i++) {
172 int vlc_index = ctx->dcb - 9;
173 if (i == 0 || i == 4 || i == 6)
175 ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
176 ctx->dcb, slice->block[i], &last_dc);
181 put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[2], hqx_quant_luma);
182 put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[3], hqx_quant_luma);
183 put_blocks(ctx, 2, x >> 1, y, flag, slice->block[4], slice->block[5], hqx_quant_chroma);
184 put_blocks(ctx, 1, x >> 1, y, flag, slice->block[6], slice->block[7], hqx_quant_chroma);
189 static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y)
191 HQXSlice *slice = &ctx->slice[slice_no];
192 GetBitContext *gb = &slice->gb;
199 cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
201 for (i = 0; i < 12; i++)
202 memset(slice->block[i], 0, sizeof(**slice->block) * 64);
203 for (i = 0; i < 12; i++)
204 slice->block[i][0] = -0x800;
207 flag = get_bits1(gb);
209 quants = hqx_quants[get_bits(gb, 4)];
211 cbp |= cbp << 4; // alpha CBP
212 if (cbp & 0x3) // chroma CBP - top
214 if (cbp & 0xC) // chroma CBP - bottom
216 for (i = 0; i < 12; i++) {
217 if (i == 0 || i == 4 || i == 8 || i == 10)
219 if (cbp & (1 << i)) {
220 int vlc_index = ctx->dcb - 9;
221 ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
222 ctx->dcb, slice->block[i], &last_dc);
229 put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma);
230 put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma);
231 put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma);
232 put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma);
233 put_blocks(ctx, 2, x >> 1, y, flag, slice->block[ 8], slice->block[ 9], hqx_quant_chroma);
234 put_blocks(ctx, 1, x >> 1, y, flag, slice->block[10], slice->block[11], hqx_quant_chroma);
239 static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y)
241 HQXSlice *slice = &ctx->slice[slice_no];
242 GetBitContext *gb = &slice->gb;
249 flag = get_bits1(gb);
253 quants = hqx_quants[get_bits(gb, 4)];
255 for (i = 0; i < 12; i++) {
256 int vlc_index = ctx->dcb - 9;
257 if (i == 0 || i == 4 || i == 8)
259 ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
260 ctx->dcb, slice->block[i], &last_dc);
265 put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[ 2], hqx_quant_luma);
266 put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[ 3], hqx_quant_luma);
267 put_blocks(ctx, 2, x, y, flag, slice->block[4], slice->block[ 6], hqx_quant_chroma);
268 put_blocks(ctx, 2, x + 8, y, flag, slice->block[5], slice->block[ 7], hqx_quant_chroma);
269 put_blocks(ctx, 1, x, y, flag, slice->block[8], slice->block[10], hqx_quant_chroma);
270 put_blocks(ctx, 1, x + 8, y, flag, slice->block[9], slice->block[11], hqx_quant_chroma);
275 static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y)
277 HQXSlice *slice = &ctx->slice[slice_no];
278 GetBitContext *gb = &slice->gb;
285 cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
287 for (i = 0; i < 16; i++)
288 memset(slice->block[i], 0, sizeof(**slice->block) * 64);
289 for (i = 0; i < 16; i++)
290 slice->block[i][0] = -0x800;
293 flag = get_bits1(gb);
295 quants = hqx_quants[get_bits(gb, 4)];
297 cbp |= cbp << 4; // alpha CBP
298 cbp |= cbp << 8; // chroma CBP
299 for (i = 0; i < 16; i++) {
300 if (i == 0 || i == 4 || i == 8 || i == 12)
302 if (cbp & (1 << i)) {
303 int vlc_index = ctx->dcb - 9;
304 ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
305 ctx->dcb, slice->block[i], &last_dc);
312 put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma);
313 put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma);
314 put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma);
315 put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma);
316 put_blocks(ctx, 2, x, y, flag, slice->block[ 8], slice->block[10], hqx_quant_chroma);
317 put_blocks(ctx, 2, x + 8, y, flag, slice->block[ 9], slice->block[11], hqx_quant_chroma);
318 put_blocks(ctx, 1, x, y, flag, slice->block[12], slice->block[14], hqx_quant_chroma);
319 put_blocks(ctx, 1, x + 8, y, flag, slice->block[13], slice->block[15], hqx_quant_chroma);
324 static const int shuffle_16[16] = {
325 0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12
328 static int decode_slice(HQXContext *ctx, int slice_no)
330 int mb_w = (ctx->width + 15) >> 4;
331 int mb_h = (ctx->height + 15) >> 4;
332 int grp_w = (mb_w + 4) / 5;
333 int grp_h = (mb_h + 4) / 5;
334 int grp_h_edge = grp_w * (mb_w / grp_w);
335 int grp_v_edge = grp_h * (mb_h / grp_h);
336 int grp_v_rest = mb_w - grp_h_edge;
337 int grp_h_rest = mb_h - grp_v_edge;
338 int num_mbs = mb_w * mb_h;
339 int num_tiles = (num_mbs + 479) / 480;
340 int std_tile_blocks = num_mbs / (16 * num_tiles);
341 int g_tile = slice_no * num_tiles;
342 int blk_addr, loc_addr, mb_x, mb_y, pos, loc_row, i;
343 int tile_blocks, tile_limit, tile_no;
345 for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) {
346 tile_blocks = std_tile_blocks;
348 if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) {
349 tile_limit = num_mbs / (16 * num_tiles);
352 for (i = 0; i < tile_blocks; i++) {
354 blk_addr = g_tile + 16 * num_tiles * i;
356 blk_addr = tile_no + 16 * num_tiles * i +
357 num_tiles * shuffle_16[(i + slice_no) & 0xF];
358 loc_row = grp_h * (blk_addr / (grp_h * mb_w));
359 loc_addr = blk_addr % (grp_h * mb_w);
360 if (loc_row >= grp_v_edge) {
361 mb_x = grp_w * (loc_addr / (grp_h_rest * grp_w));
362 pos = loc_addr % (grp_h_rest * grp_w);
364 mb_x = grp_w * (loc_addr / (grp_h * grp_w));
365 pos = loc_addr % (grp_h * grp_w);
367 if (mb_x >= grp_h_edge) {
368 mb_x += pos % grp_v_rest;
369 mb_y = loc_row + (pos / grp_v_rest);
372 mb_y = loc_row + (pos / grp_w);
374 ctx->decode_func(ctx, slice_no, mb_x * 16, mb_y * 16);
381 static int decode_slice_thread(AVCodecContext *avctx, void *arg,
382 int slice_no, int threadnr)
384 HQXContext *ctx = avctx->priv_data;
385 uint32_t *slice_off = ctx->slice_off;
388 if (slice_off[slice_no] < HQX_HEADER_SIZE ||
389 slice_off[slice_no] >= slice_off[slice_no + 1] ||
390 slice_off[slice_no + 1] > ctx->data_size) {
391 av_log(avctx, AV_LOG_ERROR, "Invalid slice size %d.\n", ctx->data_size);
392 return AVERROR_INVALIDDATA;
395 ret = init_get_bits8(&ctx->slice[slice_no].gb,
396 ctx->src + slice_off[slice_no],
397 slice_off[slice_no + 1] - slice_off[slice_no]);
401 return decode_slice(ctx, slice_no);
404 static int hqx_decode_frame(AVCodecContext *avctx, void *data,
405 int *got_picture_ptr, AVPacket *avpkt)
407 HQXContext *ctx = avctx->priv_data;
408 uint8_t *src = avpkt->data;
413 if (avpkt->size < 4 + 4) {
414 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
415 return AVERROR_INVALIDDATA;
418 info_tag = AV_RL32(src);
419 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
420 int info_offset = AV_RL32(src + 4);
421 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
422 av_log(avctx, AV_LOG_ERROR,
423 "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
425 return AVERROR_INVALIDDATA;
427 ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
433 data_start = src - avpkt->data;
434 ctx->data_size = avpkt->size - data_start;
438 if (ctx->data_size < HQX_HEADER_SIZE) {
439 av_log(avctx, AV_LOG_ERROR, "Frame too small.\n");
440 return AVERROR_INVALIDDATA;
443 if (src[0] != 'H' || src[1] != 'Q') {
444 av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n");
445 return AVERROR_INVALIDDATA;
447 ctx->interlaced = !(src[2] & 0x80);
448 ctx->format = src[2] & 7;
449 ctx->dcb = (src[3] & 3) + 8;
450 ctx->width = AV_RB16(src + 4);
451 ctx->height = AV_RB16(src + 6);
452 for (i = 0; i < 17; i++)
453 ctx->slice_off[i] = AV_RB24(src + 8 + i * 3);
456 av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb);
457 return AVERROR_INVALIDDATA;
459 ret = av_image_check_size(ctx->width, ctx->height, 0, avctx);
461 av_log(avctx, AV_LOG_ERROR, "Invalid stored dimensions %dx%d.\n",
462 ctx->width, ctx->height);
463 return AVERROR_INVALIDDATA;
466 avctx->coded_width = FFALIGN(ctx->width, 16);
467 avctx->coded_height = FFALIGN(ctx->height, 16);
468 avctx->width = ctx->width;
469 avctx->height = ctx->height;
470 avctx->bits_per_raw_sample = 10;
472 switch (ctx->format) {
474 avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
475 ctx->decode_func = hqx_decode_422;
478 avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
479 ctx->decode_func = hqx_decode_444;
482 avctx->pix_fmt = AV_PIX_FMT_YUVA422P16;
483 ctx->decode_func = hqx_decode_422a;
486 avctx->pix_fmt = AV_PIX_FMT_YUVA444P16;
487 ctx->decode_func = hqx_decode_444a;
490 av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format);
491 return AVERROR_INVALIDDATA;
494 ret = ff_get_buffer(avctx, ctx->pic, 0);
498 avctx->execute2(avctx, decode_slice_thread, NULL, NULL, 16);
500 ctx->pic->key_frame = 1;
501 ctx->pic->pict_type = AV_PICTURE_TYPE_I;
503 *got_picture_ptr = 1;
508 static av_cold int hqx_decode_close(AVCodecContext *avctx)
511 HQXContext *ctx = avctx->priv_data;
513 ff_free_vlc(&ctx->cbp_vlc);
514 for (i = 0; i < 3; i++) {
515 ff_free_vlc(&ctx->dc_vlc[i]);
521 static av_cold int hqx_decode_init(AVCodecContext *avctx)
523 HQXContext *ctx = avctx->priv_data;
525 ff_hqxdsp_init(&ctx->hqxdsp);
527 return ff_hqx_init_vlcs(ctx);
530 AVCodec ff_hqx_decoder = {
532 .long_name = NULL_IF_CONFIG_SMALL("Canopus HQX"),
533 .type = AVMEDIA_TYPE_VIDEO,
534 .id = AV_CODEC_ID_HQX,
535 .priv_data_size = sizeof(HQXContext),
536 .init = hqx_decode_init,
537 .decode = hqx_decode_frame,
538 .close = hqx_decode_close,
539 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
540 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
541 FF_CODEC_CAP_INIT_CLEANUP,