FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
hqx.c
Go to the documentation of this file.
1 /*
2  * Canopus HQX decoder
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
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 
32 /* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */
33 enum HQXFormat {
34  HQX_422 = 0,
38 };
39 
40 #define HQX_HEADER_SIZE 59
41 
42 typedef int (*mb_decode_func)(HQXContext *ctx, AVFrame *pic,
43  GetBitContext *gb, int x, int y);
44 
45 /* macroblock selects a group of 4 possible quants and
46  * a block can use any of those four quantisers
47  * one column is powers of 2, the other one is powers of 2 * 3,
48  * then there is the special one, powers of 2 * 5 */
49 static const int hqx_quants[16][4] = {
50  { 0x1, 0x2, 0x4, 0x8 }, { 0x1, 0x3, 0x6, 0xC },
51  { 0x2, 0x4, 0x8, 0x10 }, { 0x3, 0x6, 0xC, 0x18 },
52  { 0x4, 0x8, 0x10, 0x20 }, { 0x6, 0xC, 0x18, 0x30 },
53  { 0x8, 0x10, 0x20, 0x40 },
54  { 0xA, 0x14, 0x28, 0x50 },
55  { 0xC, 0x18, 0x30, 0x60 },
56  { 0x10, 0x20, 0x40, 0x80 }, { 0x18, 0x30, 0x60, 0xC0 },
57  { 0x20, 0x40, 0x80, 0x100 }, { 0x30, 0x60, 0xC0, 0x180 },
58  { 0x40, 0x80, 0x100, 0x200 }, { 0x60, 0xC0, 0x180, 0x300 },
59  { 0x80, 0x100, 0x200, 0x400 }
60 };
61 
62 static const uint8_t hqx_quant_luma[64] = {
63  16, 16, 16, 19, 19, 19, 42, 44,
64  16, 16, 19, 19, 19, 38, 43, 45,
65  16, 19, 19, 19, 40, 41, 45, 48,
66  19, 19, 19, 40, 41, 42, 46, 49,
67  19, 19, 40, 41, 42, 43, 48, 101,
68  19, 38, 41, 42, 43, 44, 98, 104,
69  42, 43, 45, 46, 48, 98, 109, 116,
70  44, 45, 48, 49, 101, 104, 116, 123,
71 };
72 
73 static const uint8_t hqx_quant_chroma[64] = {
74  16, 16, 19, 25, 26, 26, 42, 44,
75  16, 19, 25, 25, 26, 38, 43, 91,
76  19, 25, 26, 27, 40, 41, 91, 96,
77  25, 25, 27, 40, 41, 84, 93, 197,
78  26, 26, 40, 41, 84, 86, 191, 203,
79  26, 38, 41, 84, 86, 177, 197, 209,
80  42, 43, 91, 93, 191, 197, 219, 232,
81  44, 91, 96, 197, 203, 209, 232, 246,
82 };
83 
84 static inline void idct_col(int16_t *blk, const uint8_t *quant)
85 {
86  int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF;
87  int t10, t11, t12, t13;
88  int s0, s1, s2, s3, s4, s5, s6, s7;
89 
90  s0 = (int) blk[0 * 8] * quant[0 * 8];
91  s1 = (int) blk[1 * 8] * quant[1 * 8];
92  s2 = (int) blk[2 * 8] * quant[2 * 8];
93  s3 = (int) blk[3 * 8] * quant[3 * 8];
94  s4 = (int) blk[4 * 8] * quant[4 * 8];
95  s5 = (int) blk[5 * 8] * quant[5 * 8];
96  s6 = (int) blk[6 * 8] * quant[6 * 8];
97  s7 = (int) blk[7 * 8] * quant[7 * 8];
98 
99  t0 = (s3 * 19266 + s5 * 12873) >> 15;
100  t1 = (s5 * 19266 - s3 * 12873) >> 15;
101  t2 = ((s7 * 4520 + s1 * 22725) >> 15) - t0;
102  t3 = ((s1 * 4520 - s7 * 22725) >> 15) - t1;
103  t4 = t0 * 2 + t2;
104  t5 = t1 * 2 + t3;
105  t6 = t2 - t3;
106  t7 = t3 * 2 + t6;
107  t8 = (t6 * 11585) >> 14;
108  t9 = (t7 * 11585) >> 14;
109  tA = (s2 * 8867 - s6 * 21407) >> 14;
110  tB = (s6 * 8867 + s2 * 21407) >> 14;
111  tC = (s0 >> 1) - (s4 >> 1);
112  tD = (s4 >> 1) * 2 + tC;
113  tE = tC - (tA >> 1);
114  tF = tD - (tB >> 1);
115  t10 = tF - t5;
116  t11 = tE - t8;
117  t12 = tE + (tA >> 1) * 2 - t9;
118  t13 = tF + (tB >> 1) * 2 - t4;
119 
120  blk[0 * 8] = t13 + t4 * 2;
121  blk[1 * 8] = t12 + t9 * 2;
122  blk[2 * 8] = t11 + t8 * 2;
123  blk[3 * 8] = t10 + t5 * 2;
124  blk[4 * 8] = t10;
125  blk[5 * 8] = t11;
126  blk[6 * 8] = t12;
127  blk[7 * 8] = t13;
128 }
129 
130 static inline void idct_row(int16_t *blk)
131 {
132  int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF;
133  int t10, t11, t12, t13;
134 
135  t0 = (blk[3] * 19266 + blk[5] * 12873) >> 14;
136  t1 = (blk[5] * 19266 - blk[3] * 12873) >> 14;
137  t2 = ((blk[7] * 4520 + blk[1] * 22725) >> 14) - t0;
138  t3 = ((blk[1] * 4520 - blk[7] * 22725) >> 14) - t1;
139  t4 = t0 * 2 + t2;
140  t5 = t1 * 2 + t3;
141  t6 = t2 - t3;
142  t7 = t3 * 2 + t6;
143  t8 = (t6 * 11585) >> 14;
144  t9 = (t7 * 11585) >> 14;
145  tA = (blk[2] * 8867 - blk[6] * 21407) >> 14;
146  tB = (blk[6] * 8867 + blk[2] * 21407) >> 14;
147  tC = blk[0] - blk[4];
148  tD = blk[4] * 2 + tC;
149  tE = tC - tA;
150  tF = tD - tB;
151  t10 = tF - t5;
152  t11 = tE - t8;
153  t12 = tE + tA * 2 - t9;
154  t13 = tF + tB * 2 - t4;
155 
156  blk[0] = (t13 + t4 * 2 + 4) >> 3;
157  blk[1] = (t12 + t9 * 2 + 4) >> 3;
158  blk[2] = (t11 + t8 * 2 + 4) >> 3;
159  blk[3] = (t10 + t5 * 2 + 4) >> 3;
160  blk[4] = (t10 + 4) >> 3;
161  blk[5] = (t11 + 4) >> 3;
162  blk[6] = (t12 + 4) >> 3;
163  blk[7] = (t13 + 4) >> 3;
164 }
165 
166 static void hqx_idct(int16_t *block, const uint8_t *quant)
167 {
168  int i;
169 
170  for (i = 0; i < 8; i++)
171  idct_col(block + i, quant + i);
172  for (i = 0; i < 8; i++)
173  idct_row(block + i * 8);
174 }
175 
176 static void hqx_idct_put(uint16_t *dst, ptrdiff_t stride,
177  int16_t *block, const uint8_t *quant)
178 {
179  int i, j;
180 
181  hqx_idct(block, quant);
182 
183  for (i = 0; i < 8; i++) {
184  for (j = 0; j < 8; j++) {
185  int v = av_clip(block[j + i * 8] + 0x800, 0, 0x1000);
186  dst[j] = (v << 4) | (v >> 8);
187  }
188  dst += stride >> 1;
189  }
190 }
191 
192 static inline void put_blocks(AVFrame *pic, int plane,
193  int x, int y, int ilace,
194  int16_t *block0, int16_t *block1,
195  const uint8_t *quant)
196 {
197  int fields = ilace ? 2 : 1;
198  int lsize = pic->linesize[plane];
199  uint8_t *p = pic->data[plane] + x * 2;
200 
201  hqx_idct_put((uint16_t *)(p + y * lsize), lsize * fields, block0, quant);
202  hqx_idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),
203  lsize * fields, block1, quant);
204 }
205 
206 static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac,
207  int *run, int *lev)
208 {
209  int val;
210 
211  val = show_bits(gb, ac->lut_bits);
212  if (ac->lut[val].bits == -1) {
213  GetBitContext gb2 = *gb;
214  skip_bits(&gb2, ac->lut_bits);
215  val = ac->lut[val].lev + show_bits(&gb2, ac->extra_bits);
216  }
217  *run = ac->lut[val].run;
218  *lev = ac->lut[val].lev;
219  skip_bits(gb, ac->lut[val].bits);
220 }
221 
222 static int decode_block(GetBitContext *gb, VLC *vlc,
223  const int *quants, int dcb,
224  int16_t block[64], int *last_dc)
225 {
226  int q, dc;
227  int ac_idx;
228  int run, lev, pos = 1;
229 
230  memset(block, 0, 64 * sizeof(*block));
231  dc = get_vlc2(gb, vlc->table, HQX_DC_VLC_BITS, 2);
232  if (dc < 0)
233  return AVERROR_INVALIDDATA;
234  *last_dc += dc;
235 
236  block[0] = sign_extend(*last_dc << (12 - dcb), 12);
237 
238  q = quants[get_bits(gb, 2)];
239  if (q >= 128)
240  ac_idx = HQX_AC_Q128;
241  else if (q >= 64)
242  ac_idx = HQX_AC_Q64;
243  else if (q >= 32)
244  ac_idx = HQX_AC_Q32;
245  else if (q >= 16)
246  ac_idx = HQX_AC_Q16;
247  else if (q >= 8)
248  ac_idx = HQX_AC_Q8;
249  else
250  ac_idx = HQX_AC_Q0;
251 
252  do {
253  hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev);
254  pos += run;
255  if (pos >= 64)
256  break;
257  block[ff_zigzag_direct[pos++]] = lev * q;
258  } while (pos < 64);
259 
260  return 0;
261 }
262 
263 static int hqx_decode_422(HQXContext *ctx, AVFrame *pic,
264  GetBitContext *gb, int x, int y)
265 {
266  const int *quants;
267  int flag;
268  int last_dc;
269  int i, ret;
270 
271  if (ctx->interlaced)
272  flag = get_bits1(gb);
273  else
274  flag = 0;
275 
276  quants = hqx_quants[get_bits(gb, 4)];
277 
278  for (i = 0; i < 8; i++) {
279  int vlc_index = ctx->dcb - 9;
280  if (i == 0 || i == 4 || i == 6)
281  last_dc = 0;
282  ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
283  ctx->dcb, ctx->block[i], &last_dc);
284  if (ret < 0)
285  return ret;
286  }
287 
288  put_blocks(pic, 0, x, y, flag, ctx->block[0], ctx->block[2], hqx_quant_luma);
289  put_blocks(pic, 0, x + 8, y, flag, ctx->block[1], ctx->block[3], hqx_quant_luma);
290  put_blocks(pic, 2, x >> 1, y, flag, ctx->block[4], ctx->block[5], hqx_quant_chroma);
291  put_blocks(pic, 1, x >> 1, y, flag, ctx->block[6], ctx->block[7], hqx_quant_chroma);
292 
293  return 0;
294 }
295 
296 static int hqx_decode_422a(HQXContext *ctx, AVFrame *pic,
297  GetBitContext *gb, int x, int y)
298 {
299  const int *quants;
300  int flag = 0;
301  int last_dc;
302  int i, ret;
303  int cbp;
304 
305  cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
306 
307  for (i = 0; i < 12; i++)
308  memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);
309  for (i = 0; i < 12; i++)
310  ctx->block[i][0] = -0x800;
311  if (cbp) {
312  if (ctx->interlaced)
313  flag = get_bits1(gb);
314 
315  quants = hqx_quants[get_bits(gb, 4)];
316 
317  cbp |= cbp << 4; // alpha CBP
318  if (cbp & 0x3) // chroma CBP - top
319  cbp |= 0x500;
320  if (cbp & 0xC) // chroma CBP - bottom
321  cbp |= 0xA00;
322  for (i = 0; i < 12; i++) {
323  if (i == 0 || i == 4 || i == 8 || i == 10)
324  last_dc = 0;
325  if (cbp & (1 << i)) {
326  int vlc_index = ctx->dcb - 9;
327  ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
328  ctx->dcb, ctx->block[i], &last_dc);
329  if (ret < 0)
330  return ret;
331  }
332  }
333  }
334 
335  put_blocks(pic, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma);
336  put_blocks(pic, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma);
337  put_blocks(pic, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma);
338  put_blocks(pic, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma);
339  put_blocks(pic, 2, x >> 1, y, flag, ctx->block[ 8], ctx->block[ 9], hqx_quant_chroma);
340  put_blocks(pic, 1, x >> 1, y, flag, ctx->block[10], ctx->block[11], hqx_quant_chroma);
341 
342  return 0;
343 }
344 
345 static int hqx_decode_444(HQXContext *ctx, AVFrame *pic,
346  GetBitContext *gb, int x, int y)
347 {
348  const int *quants;
349  int flag;
350  int last_dc;
351  int i, ret;
352 
353  if (ctx->interlaced)
354  flag = get_bits1(gb);
355  else
356  flag = 0;
357 
358  quants = hqx_quants[get_bits(gb, 4)];
359 
360  for (i = 0; i < 12; i++) {
361  int vlc_index = ctx->dcb - 9;
362  if (i == 0 || i == 4 || i == 8)
363  last_dc = 0;
364  ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
365  ctx->dcb, ctx->block[i], &last_dc);
366  if (ret < 0)
367  return ret;
368  }
369 
370  put_blocks(pic, 0, x, y, flag, ctx->block[0], ctx->block[ 2], hqx_quant_luma);
371  put_blocks(pic, 0, x + 8, y, flag, ctx->block[1], ctx->block[ 3], hqx_quant_luma);
372  put_blocks(pic, 2, x, y, flag, ctx->block[4], ctx->block[ 6], hqx_quant_chroma);
373  put_blocks(pic, 2, x + 8, y, flag, ctx->block[5], ctx->block[ 7], hqx_quant_chroma);
374  put_blocks(pic, 1, x, y, flag, ctx->block[8], ctx->block[10], hqx_quant_chroma);
375  put_blocks(pic, 1, x + 8, y, flag, ctx->block[9], ctx->block[11], hqx_quant_chroma);
376 
377  return 0;
378 }
379 
380 static int hqx_decode_444a(HQXContext *ctx, AVFrame *pic,
381  GetBitContext *gb, int x, int y)
382 {
383  const int *quants;
384  int flag = 0;
385  int last_dc;
386  int i, ret;
387  int cbp;
388 
389  cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);
390 
391  for (i = 0; i < 16; i++)
392  memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);
393  for (i = 0; i < 16; i++)
394  ctx->block[i][0] = -0x800;
395  if (cbp) {
396  if (ctx->interlaced)
397  flag = get_bits1(gb);
398 
399  quants = hqx_quants[get_bits(gb, 4)];
400 
401  cbp |= cbp << 4; // alpha CBP
402  cbp |= cbp << 8; // chroma CBP
403  for (i = 0; i < 16; i++) {
404  if (i == 0 || i == 4 || i == 8 || i == 12)
405  last_dc = 0;
406  if (cbp & (1 << i)) {
407  int vlc_index = ctx->dcb - 9;
408  ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
409  ctx->dcb, ctx->block[i], &last_dc);
410  if (ret < 0)
411  return ret;
412  }
413  }
414  }
415 
416  put_blocks(pic, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma);
417  put_blocks(pic, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma);
418  put_blocks(pic, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma);
419  put_blocks(pic, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma);
420  put_blocks(pic, 2, x, y, flag, ctx->block[ 8], ctx->block[10], hqx_quant_chroma);
421  put_blocks(pic, 2, x + 8, y, flag, ctx->block[ 9], ctx->block[11], hqx_quant_chroma);
422  put_blocks(pic, 1, x, y, flag, ctx->block[12], ctx->block[14], hqx_quant_chroma);
423  put_blocks(pic, 1, x + 8, y, flag, ctx->block[13], ctx->block[15], hqx_quant_chroma);
424 
425  return 0;
426 }
427 
428 static const int shuffle_16[16] = {
429  0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12
430 };
431 
432 static int decode_slice(HQXContext *ctx, AVFrame *pic, GetBitContext *gb,
433  int slice_no, mb_decode_func decode_func)
434 {
435  int mb_w = (ctx->width + 15) >> 4;
436  int mb_h = (ctx->height + 15) >> 4;
437  int grp_w = (mb_w + 4) / 5;
438  int grp_h = (mb_h + 4) / 5;
439  int grp_h_edge = grp_w * (mb_w / grp_w);
440  int grp_v_edge = grp_h * (mb_h / grp_h);
441  int grp_v_rest = mb_w - grp_h_edge;
442  int grp_h_rest = mb_h - grp_v_edge;
443  int num_mbs = mb_w * mb_h;
444  int num_tiles = (num_mbs + 479) / 480;
445  int std_tile_blocks = num_mbs / (16 * num_tiles);
446  int g_tile = slice_no * num_tiles;
447  int blk_addr, loc_addr, mb_x, mb_y, pos, loc_row, i;
448  int tile_blocks, tile_limit, tile_no;
449 
450  for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) {
451  tile_blocks = std_tile_blocks;
452  tile_limit = -1;
453  if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) {
454  tile_limit = num_mbs / (16 * num_tiles);
455  tile_blocks++;
456  }
457  for (i = 0; i < tile_blocks; i++) {
458  if (i == tile_limit)
459  blk_addr = g_tile + 16 * num_tiles * i;
460  else
461  blk_addr = tile_no + 16 * num_tiles * i +
462  num_tiles * shuffle_16[(i + slice_no) & 0xF];
463  loc_row = grp_h * (blk_addr / (grp_h * mb_w));
464  loc_addr = blk_addr % (grp_h * mb_w);
465  if (loc_row >= grp_v_edge) {
466  mb_x = grp_w * (loc_addr / (grp_h_rest * grp_w));
467  pos = loc_addr % (grp_h_rest * grp_w);
468  } else {
469  mb_x = grp_w * (loc_addr / (grp_h * grp_w));
470  pos = loc_addr % (grp_h * grp_w);
471  }
472  if (mb_x >= grp_h_edge) {
473  mb_x += pos % grp_v_rest;
474  mb_y = loc_row + (pos / grp_v_rest);
475  } else {
476  mb_x += pos % grp_w;
477  mb_y = loc_row + (pos / grp_w);
478  }
479  decode_func(ctx, pic, gb, mb_x * 16, mb_y * 16);
480  }
481  }
482 
483  return 0;
484 }
485 
486 static int hqx_decode_frame(AVCodecContext *avctx, void *data,
487  int *got_picture_ptr, AVPacket *avpkt)
488 {
489  HQXContext *ctx = avctx->priv_data;
490  AVFrame *pic = data;
491  uint8_t *src = avpkt->data;
492  uint32_t info_tag, info_offset;
493  int data_start;
494  unsigned data_size;
495  GetBitContext gb;
496  int i, ret;
497  int slice;
498  uint32_t slice_off[17];
499  mb_decode_func decode_func = 0;
500 
501  if (avpkt->size < 8)
502  return AVERROR_INVALIDDATA;
503 
504  /* Skip the INFO header if present */
505  info_offset = 0;
506  info_tag = AV_RL32(src);
507  if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
508  info_offset = AV_RL32(src + 4);
509  if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
510  av_log(avctx, AV_LOG_ERROR,
511  "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
512  info_offset);
513  return AVERROR_INVALIDDATA;
514  }
515 
516  info_offset += 8;
517  src += info_offset;
518 
519  av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
520  }
521 
522  data_start = src - avpkt->data;
523  data_size = avpkt->size - data_start;
524 
525  if (data_size < HQX_HEADER_SIZE) {
526  av_log(avctx, AV_LOG_ERROR, "Frame too small.\n");
527  return AVERROR_INVALIDDATA;
528  }
529 
530  if (src[0] != 'H' || src[1] != 'Q') {
531  av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n");
532  return AVERROR_INVALIDDATA;
533  }
534  ctx->interlaced = !(src[2] & 0x80);
535  ctx->format = src[2] & 7;
536  ctx->dcb = (src[3] & 3) + 8;
537  ctx->width = AV_RB16(src + 4);
538  ctx->height = AV_RB16(src + 6);
539  for (i = 0; i < 17; i++)
540  slice_off[i] = AV_RB24(src + 8 + i * 3);
541 
542  if (ctx->dcb == 8) {
543  av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb);
544  return AVERROR_INVALIDDATA;
545  }
546  ret = av_image_check_size(ctx->width, ctx->height, 0, avctx);
547  if (ret < 0) {
548  av_log(avctx, AV_LOG_ERROR, "Invalid stored dimensions %dx%d.\n",
549  ctx->width, ctx->height);
550  return AVERROR_INVALIDDATA;
551  }
552 
553  avctx->coded_width = FFALIGN(ctx->width, 16);
554  avctx->coded_height = FFALIGN(ctx->height, 16);
555  avctx->width = ctx->width;
556  avctx->height = ctx->height;
557  avctx->bits_per_raw_sample = 10;
558 
559  switch (ctx->format) {
560  case HQX_422:
561  avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
562  decode_func = hqx_decode_422;
563  break;
564  case HQX_444:
565  avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
566  decode_func = hqx_decode_444;
567  break;
568  case HQX_422A:
570  decode_func = hqx_decode_422a;
571  break;
572  case HQX_444A:
574  decode_func = hqx_decode_444a;
575  break;
576  }
577  if (!decode_func) {
578  av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format);
579  return AVERROR_INVALIDDATA;
580  }
581 
582  ret = ff_get_buffer(avctx, pic, 0);
583  if (ret < 0)
584  return ret;
585 
586  for (slice = 0; slice < 16; slice++) {
587  if (slice_off[slice] < HQX_HEADER_SIZE ||
588  slice_off[slice] >= slice_off[slice + 1] ||
589  slice_off[slice + 1] > data_size) {
590  av_log(avctx, AV_LOG_ERROR, "Invalid slice size.\n");
591  break;
592  }
593  ret = init_get_bits8(&gb, src + slice_off[slice],
594  slice_off[slice + 1] - slice_off[slice]);
595  if (ret < 0)
596  return ret;
597  ret = decode_slice(ctx, pic, &gb, slice, decode_func);
598  if (ret < 0) {
599  av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d.\n", slice);
600  }
601  }
602 
603  pic->key_frame = 1;
605 
606  *got_picture_ptr = 1;
607 
608  return avpkt->size;
609 }
610 
612 {
613  int i;
614  HQXContext *ctx = avctx->priv_data;
615 
616  ff_free_vlc(&ctx->cbp_vlc);
617  for (i = 0; i < 3; i++) {
618  ff_free_vlc(&ctx->dc_vlc[i]);
619  }
620 
621  return 0;
622 }
623 
625 {
626  HQXContext *ctx = avctx->priv_data;
627  int ret = ff_hqx_init_vlcs(ctx);
628  if (ret < 0)
629  hqx_decode_close(avctx);
630  return ret;
631 }
632 
634  .name = "hqx",
635  .long_name = NULL_IF_CONFIG_SMALL("Canopus HQX"),
636  .type = AVMEDIA_TYPE_VIDEO,
637  .id = AV_CODEC_ID_HQX,
638  .priv_data_size = sizeof(HQXContext),
641  .close = hqx_decode_close,
642  .capabilities = CODEC_CAP_DR1,
643 };
Definition: hqx.h:44
const char const char void * val
Definition: avisynth_c.h:672
static void idct_col(int16_t *blk, const uint8_t *quant)
Definition: hqx.c:84
float v
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:391
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int dcb
Definition: hqx.h:50
int height
Definition: hqx.h:50
#define t9
Definition: regdef.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:163
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int interlaced
Definition: hqx.h:51
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1422
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
static void hqx_get_ac(GetBitContext *gb, const HQXAC *ac, int *run, int *lev)
Definition: hqx.c:206
#define HQX_DC_VLC_BITS
Definition: hqx.h:59
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
Definition: hqx.c:36
int size
Definition: avcodec.h:1161
Definition: hqx.h:32
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1442
int ff_hqx_init_vlcs(HQXContext *ctx)
Definition: hqxvlc.c:2155
#define t8
Definition: regdef.h:53
uint8_t run
Definition: svq3.c:149
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2725
static int hqx_decode_444(HQXContext *ctx, AVFrame *pic, GetBitContext *gb, int x, int y)
Definition: hqx.c:345
#define blk(i)
Definition: sha.c:185
AVCodec.
Definition: avcodec.h:3173
Definition: hqx.h:31
static av_cold int hqx_decode_init(AVCodecContext *avctx)
Definition: hqx.c:624
#define FFALIGN(x, a)
Definition: common.h:86
static int hqx_decode_frame(AVCodecContext *avctx, void *data, int *got_picture_ptr, AVPacket *avpkt)
Definition: hqx.c:486
#define t7
Definition: regdef.h:35
const HQXAC ff_hqx_ac[NUM_HQX_AC]
Definition: hqxvlc.c:2132
if()
Definition: avfilter.c:975
#define t12
Definition: regdef.h:58
uint8_t
#define av_cold
Definition: attributes.h:74
uint8_t run
Definition: hqx.h:40
#define t11
Definition: regdef.h:56
#define t0
Definition: regdef.h:28
static const uint8_t hqx_quant_luma[64]
Definition: hqx.c:62
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:787
int16_t block[16][64]
Definition: hqx.h:53
static const int hqx_quants[16][4]
Definition: hqx.c:49
uint8_t * data
Definition: avcodec.h:1160
bitstream reader API header.
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:369
static const uint8_t hqx_quant_chroma[64]
Definition: hqx.c:73
static int hqx_decode_422a(HQXContext *ctx, AVFrame *pic, GetBitContext *gb, int x, int y)
Definition: hqx.c:296
#define av_log(a,...)
AVCodec ff_hqx_decoder
Definition: hqx.c:633
const HQXLUT * lut
Definition: hqx.h:46
static void hqx_idct(int16_t *block, const uint8_t *quant)
Definition: hqx.c:166
VLC dc_vlc[3]
Definition: hqx.h:56
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
static av_cold int hqx_decode_close(AVCodecContext *avctx)
Definition: hqx.c:611
#define s2
Definition: regdef.h:39
#define t10
Definition: regdef.h:55
static void idct_row(int16_t *blk)
Definition: hqx.c:130
#define AV_RB16
Definition: intreadwrite.h:53
int lut_bits
Definition: hqx.h:45
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:180
static int decode_block(GetBitContext *gb, VLC *vlc, const int *quants, int dcb, int16_t block[64], int *last_dc)
Definition: hqx.c:222
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:196
static int hqx_decode_444a(HQXContext *ctx, AVFrame *pic, GetBitContext *gb, int x, int y)
Definition: hqx.c:380
#define s0
Definition: regdef.h:37
#define t1
Definition: regdef.h:29
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:392
Definition: hqx.c:35
static void put_blocks(AVFrame *pic, int plane, int x, int y, int ilace, int16_t *block0, int16_t *block1, const uint8_t *quant)
Definition: hqx.c:192
const char * name
Name of the codec implementation.
Definition: avcodec.h:3180
#define t3
Definition: regdef.h:31
Libavcodec external API header.
Definition: hqx.h:49
Definition: get_bits.h:63
#define s5
Definition: regdef.h:42
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:241
Definition: hqx.h:33
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:234
int(* mb_decode_func)(HQXContext *ctx, AVFrame *pic, GetBitContext *gb, int x, int y)
Definition: hqx.c:42
float y
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1412
static int decode_slice(HQXContext *ctx, AVFrame *pic, GetBitContext *gb, int slice_no, mb_decode_func decode_func)
Definition: hqx.c:432
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:287
int extra_bits
Definition: hqx.h:45
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:555
#define AV_RL32
Definition: intreadwrite.h:146
Definition: hqx.c:37
#define HQX_HEADER_SIZE
Definition: hqx.c:40
#define s4
Definition: regdef.h:41
#define s3
Definition: regdef.h:40
int bits
Definition: get_bits.h:64
int width
Definition: hqx.h:50
Definition: hqx.h:29
AVS_Value src
Definition: avisynth_c.h:524
int8_t bits
Definition: hqx.h:41
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:191
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:441
main external API structure.
Definition: avcodec.h:1239
HQXFormat
Definition: hqx.c:33
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1032
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
int coded_height
Definition: avcodec.h:1422
static const int shuffle_16[16]
Definition: hqx.c:428
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:297
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:117
#define t5
Definition: regdef.h:33
#define s1
Definition: regdef.h:38
const uint8_t * quant
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:139
static int16_t block1[64]
Definition: dct-test.c:111
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:174
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
int16_t lev
Definition: hqx.h:39
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:520
Definition: hqx.c:34
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
common internal api header.
#define t6
Definition: regdef.h:34
static int hqx_decode_422(HQXContext *ctx, AVFrame *pic, GetBitContext *gb, int x, int y)
Definition: hqx.c:263
void * priv_data
Definition: avcodec.h:1281
#define t4
Definition: regdef.h:32
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int format
Definition: hqx.h:50
VLC cbp_vlc
Definition: hqx.h:55
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:229
Definition: hqx.h:30
#define s6
Definition: regdef.h:43
static void hqx_idct_put(uint16_t *dst, ptrdiff_t stride, int16_t *block, const uint8_t *quant)
Definition: hqx.c:176
#define MKTAG(a, b, c, d)
Definition: common.h:319
This structure stores compressed data.
Definition: avcodec.h:1137
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:358
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:368
#define t2
Definition: regdef.h:30
static int16_t block[64]
Definition: dct-test.c:110