]> git.sesse.net Git - ffmpeg/blob - libavcodec/hqx.c
hqx: Store shareable data in main decoder context
[ffmpeg] / libavcodec / hqx.c
1 /*
2  * Canopus HQX decoder
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <inttypes.h>
22
23 #include "libavutil/imgutils.h"
24 #include "libavutil/intreadwrite.h"
25
26 #include "avcodec.h"
27 #include "get_bits.h"
28 #include "internal.h"
29
30 #include "hqx.h"
31 #include "hqxdsp.h"
32
33 /* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */
34 enum HQXFormat {
35     HQX_422 = 0,
36     HQX_444,
37     HQX_422A,
38     HQX_444A,
39 };
40
41 #define HQX_HEADER_SIZE 59
42
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 }
58 };
59
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,
69 };
70
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,
80 };
81
82 static inline void put_blocks(HQXContext *ctx, int plane,
83                               int x, int y, int ilace,
84                               int16_t *block0, int16_t *block1,
85                               const uint8_t *quant)
86 {
87     int fields = ilace ? 2 : 1;
88     int lsize = ctx->pic->linesize[plane];
89     uint8_t *p = ctx->pic->data[plane] + x * 2;
90
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);
95 }
96
97 static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac,
98                               int *run, int *lev)
99 {
100     int val;
101
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);
107     }
108     *run = ac->lut[val].run;
109     *lev = ac->lut[val].lev;
110     skip_bits(gb, ac->lut[val].bits);
111 }
112
113 static int decode_block(GetBitContext *gb, VLC *vlc,
114                         const int *quants, int dcb,
115                         int16_t block[64], int *last_dc)
116 {
117     int q, dc;
118     int ac_idx;
119     int run, lev, pos = 1;
120
121     memset(block, 0, 64 * sizeof(*block));
122     dc = get_vlc2(gb, vlc->table, HQX_DC_VLC_BITS, 2);
123     if (dc < 0)
124         return AVERROR_INVALIDDATA;
125     *last_dc += dc;
126
127     block[0] = sign_extend(*last_dc << (12 - dcb), 12);
128
129     q = quants[get_bits(gb, 2)];
130     if (q >= 128)
131         ac_idx = HQX_AC_Q128;
132     else if (q >= 64)
133         ac_idx = HQX_AC_Q64;
134     else if (q >= 32)
135         ac_idx = HQX_AC_Q32;
136     else if (q >= 16)
137         ac_idx = HQX_AC_Q16;
138     else if (q >= 8)
139         ac_idx = HQX_AC_Q8;
140     else
141         ac_idx = HQX_AC_Q0;
142
143     do {
144         hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev);
145         pos += run;
146         if (pos >= 64)
147             break;
148         block[ff_zigzag_direct[pos++]] = lev * q;
149     } while (pos < 64);
150
151     return 0;
152 }
153
154 static int hqx_decode_422(HQXContext *ctx, GetBitContext *gb, int x, int y)
155 {
156     const int *quants;
157     int flag;
158     int last_dc;
159     int i, ret;
160
161     if (ctx->interlaced)
162         flag = get_bits1(gb);
163     else
164         flag = 0;
165
166     quants = hqx_quants[get_bits(gb, 4)];
167
168     for (i = 0; i < 8; i++) {
169         int vlc_index = ctx->dcb - 9;
170         if (i == 0 || i == 4 || i == 6)
171             last_dc = 0;
172         ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
173                            ctx->dcb, ctx->block[i], &last_dc);
174         if (ret < 0)
175             return ret;
176     }
177
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);
182
183     return 0;
184 }
185
186 static int hqx_decode_422a(HQXContext *ctx, GetBitContext *gb, int x, int y)
187 {
188     const int *quants;
189     int flag = 0;
190     int last_dc;
191     int i, ret;
192     int cbp;
193
194     cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
195
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;
200     if (cbp) {
201         if (ctx->interlaced)
202             flag = get_bits1(gb);
203
204         quants = hqx_quants[get_bits(gb, 4)];
205
206         cbp |= cbp << 4; // alpha CBP
207         if (cbp & 0x3)   // chroma CBP - top
208             cbp |= 0x500;
209         if (cbp & 0xC)   // chroma CBP - bottom
210             cbp |= 0xA00;
211         for (i = 0; i < 12; i++) {
212             if (i == 0 || i == 4 || i == 8 || i == 10)
213                 last_dc = 0;
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);
218                 if (ret < 0)
219                     return ret;
220             }
221         }
222     }
223
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);
230
231     return 0;
232 }
233
234 static int hqx_decode_444(HQXContext *ctx, GetBitContext *gb, int x, int y)
235 {
236     const int *quants;
237     int flag;
238     int last_dc;
239     int i, ret;
240
241     if (ctx->interlaced)
242         flag = get_bits1(gb);
243     else
244         flag = 0;
245
246     quants = hqx_quants[get_bits(gb, 4)];
247
248     for (i = 0; i < 12; i++) {
249         int vlc_index = ctx->dcb - 9;
250         if (i == 0 || i == 4 || i == 8)
251             last_dc = 0;
252         ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
253                            ctx->dcb, ctx->block[i], &last_dc);
254         if (ret < 0)
255             return ret;
256     }
257
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);
264
265     return 0;
266 }
267
268 static int hqx_decode_444a(HQXContext *ctx, GetBitContext *gb, int x, int y)
269 {
270     const int *quants;
271     int flag = 0;
272     int last_dc;
273     int i, ret;
274     int cbp;
275
276     cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
277
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;
282     if (cbp) {
283         if (ctx->interlaced)
284             flag = get_bits1(gb);
285
286         quants = hqx_quants[get_bits(gb, 4)];
287
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)
292                 last_dc = 0;
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);
297                 if (ret < 0)
298                     return ret;
299             }
300         }
301     }
302
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);
311
312     return 0;
313 }
314
315 static const int shuffle_16[16] = {
316     0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12
317 };
318
319 static int decode_slice(HQXContext *ctx, GetBitContext *gb, int slice_no)
320 {
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;
335
336     for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) {
337         tile_blocks = std_tile_blocks;
338         tile_limit = -1;
339         if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) {
340             tile_limit = num_mbs / (16 * num_tiles);
341             tile_blocks++;
342         }
343         for (i = 0; i < tile_blocks; i++) {
344             if (i == tile_limit)
345                 blk_addr = g_tile + 16 * num_tiles * i;
346             else
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);
354             } else {
355                 mb_x = grp_w * (loc_addr / (grp_h * grp_w));
356                 pos  =          loc_addr % (grp_h * grp_w);
357             }
358             if (mb_x >= grp_h_edge) {
359                 mb_x +=            pos % grp_v_rest;
360                 mb_y  = loc_row + (pos / grp_v_rest);
361             } else {
362                 mb_x +=            pos % grp_w;
363                 mb_y  = loc_row + (pos / grp_w);
364             }
365             ctx->decode_func(ctx, gb, mb_x * 16, mb_y * 16);
366         }
367     }
368
369     return 0;
370 }
371
372 static int hqx_decode_frame(AVCodecContext *avctx, void *data,
373                             int *got_picture_ptr, AVPacket *avpkt)
374 {
375     HQXContext *ctx = avctx->priv_data;
376     uint8_t *src = avpkt->data;
377     uint32_t info_tag, info_offset;
378     int data_start;
379     GetBitContext gb;
380     int i, ret;
381     int slice;
382
383     if (avpkt->size < 8)
384         return AVERROR_INVALIDDATA;
385
386     /* Skip the INFO header if present */
387     info_offset = 0;
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",
394                    info_offset);
395             return AVERROR_INVALIDDATA;
396         }
397
398         info_offset += 8;
399         src         += info_offset;
400
401         av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
402     }
403
404     data_start     = src - avpkt->data;
405     ctx->data_size = avpkt->size - data_start;
406     ctx->src       = src;
407     ctx->pic       = data;
408
409     if (ctx->data_size < HQX_HEADER_SIZE) {
410         av_log(avctx, AV_LOG_ERROR, "Frame too small.\n");
411         return AVERROR_INVALIDDATA;
412     }
413
414     if (src[0] != 'H' || src[1] != 'Q') {
415         av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n");
416         return AVERROR_INVALIDDATA;
417     }
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);
425
426     if (ctx->dcb == 8) {
427         av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb);
428         return AVERROR_INVALIDDATA;
429     }
430     ret = av_image_check_size(ctx->width, ctx->height, 0, avctx);
431     if (ret < 0) {
432         av_log(avctx, AV_LOG_ERROR, "Invalid stored dimenstions %dx%d.\n",
433                ctx->width, ctx->height);
434         return AVERROR_INVALIDDATA;
435     }
436
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;
442
443     switch (ctx->format) {
444     case HQX_422:
445         avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
446         ctx->decode_func = hqx_decode_422;
447         break;
448     case HQX_444:
449         avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
450         ctx->decode_func = hqx_decode_444;
451         break;
452     case HQX_422A:
453         avctx->pix_fmt = AV_PIX_FMT_YUVA422P16;
454         ctx->decode_func = hqx_decode_422a;
455         break;
456     case HQX_444A:
457         avctx->pix_fmt = AV_PIX_FMT_YUVA444P16;
458         ctx->decode_func = hqx_decode_444a;
459         break;
460     default:
461         av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format);
462         return AVERROR_INVALIDDATA;
463     }
464
465     ret = ff_get_buffer(avctx, ctx->pic, 0);
466     if (ret < 0) {
467         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
468         return ret;
469     }
470
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");
476             break;
477         }
478         ret = init_get_bits(&gb, src + ctx->slice_off[slice],
479                             (ctx->slice_off[slice + 1] - ctx->slice_off[slice]) * 8);
480         if (ret < 0)
481             return ret;
482         ret = decode_slice(ctx, &gb, slice);
483         if (ret < 0) {
484             av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d.\n", slice);
485         }
486     }
487
488     ctx->pic->key_frame = 1;
489     ctx->pic->pict_type = AV_PICTURE_TYPE_I;
490
491     *got_picture_ptr = 1;
492
493     return avpkt->size;
494 }
495
496 static av_cold int hqx_decode_close(AVCodecContext *avctx)
497 {
498     int i;
499     HQXContext *ctx = avctx->priv_data;
500
501     ff_free_vlc(&ctx->cbp_vlc);
502     for (i = 0; i < 3; i++) {
503         ff_free_vlc(&ctx->dc_vlc[i]);
504     }
505
506     return 0;
507 }
508
509 static av_cold int hqx_decode_init(AVCodecContext *avctx)
510 {
511     HQXContext *ctx = avctx->priv_data;
512     int ret = ff_hqx_init_vlcs(ctx);
513     if (ret < 0)
514         hqx_decode_close(avctx);
515
516     ff_hqxdsp_init(&ctx->hqxdsp);
517
518     return ret;
519 }
520
521 AVCodec ff_hqx_decoder = {
522     .name           = "hqx",
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,
531 };