FFmpeg
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/frame.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/mem_internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/thread.h"
28 
29 #include "avcodec.h"
30 #include "canopus.h"
31 #include "codec_internal.h"
32 #include "get_bits.h"
33 #include "thread.h"
34 #include "vlc.h"
35 
36 #include "hqxdsp.h"
37 #include "hqxvlc.h"
38 
39 /* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */
40 enum HQXFormat {
41  HQX_422 = 0,
45 };
46 
47 struct HQXContext;
48 
49 typedef int (*mb_decode_func)(struct HQXContext *ctx,
50  int slice_no, int x, int y);
51 
52 typedef struct HQXSlice {
54  DECLARE_ALIGNED(16, int16_t, block)[16][64];
55 } HQXSlice;
56 
57 typedef struct HQXContext {
60 
63 
66 
67  const uint8_t *src;
68  unsigned int data_size;
69  uint32_t slice_off[17];
70 
71  const VLCElem *dc_vlc;
72 
74 } HQXContext;
75 
76 #define HQX_HEADER_SIZE 59
77 
78 #define AC_IDX(q) ((q) >= 128 ? HQX_AC_Q128 : (q) >= 64 ? HQX_AC_Q64 : \
79  (q) >= 32 ? HQX_AC_Q32 : (q) >= 16 ? HQX_AC_Q16 : \
80  (q) >= 8 ? HQX_AC_Q8 : HQX_AC_Q0)
81 
82 /* macroblock selects a group of 4 possible quants and
83  * a block can use any of those four quantisers
84  * one column is powers of 2, the other one is powers of 2 * 3,
85  * then there is the special one, powers of 2 * 5.
86  * We also encode the corresponding AC index in these tables in bits 29-31. */
87 static const unsigned hqx_quants[16][4] = {
88 #define Q(q) ((unsigned)AC_IDX(q) << 29 | (q))
89  { Q( 0x1), Q( 0x2), Q( 0x4), Q( 0x8) }, { Q( 0x1), Q( 0x3), Q( 0x6), Q( 0xC) },
90  { Q( 0x2), Q( 0x4), Q( 0x8), Q( 0x10) }, { Q( 0x3), Q( 0x6), Q( 0xC), Q( 0x18) },
91  { Q( 0x4), Q( 0x8), Q( 0x10), Q( 0x20) }, { Q( 0x6), Q( 0xC), Q( 0x18), Q( 0x30) },
92  { Q( 0x8), Q( 0x10), Q( 0x20), Q( 0x40) },
93  { Q(0xA), Q(0x14), Q(0x28), Q(0x50) },
94  { Q( 0xC), Q(0x18), Q( 0x30), Q( 0x60) },
95  { Q(0x10), Q( 0x20), Q( 0x40), Q( 0x80) }, { Q(0x18), Q(0x30), Q( 0x60), Q( 0xC0) },
96  { Q(0x20), Q( 0x40), Q( 0x80), Q(0x100) }, { Q(0x30), Q(0x60), Q( 0xC0), Q(0x180) },
97  { Q(0x40), Q( 0x80), Q(0x100), Q(0x200) }, { Q(0x60), Q(0xC0), Q(0x180), Q(0x300) },
98  { Q(0x80), Q(0x100), Q(0x200), Q(0x400) }
99 };
100 
101 static const uint8_t hqx_quant_luma[64] = {
102  16, 16, 16, 19, 19, 19, 42, 44,
103  16, 16, 19, 19, 19, 38, 43, 45,
104  16, 19, 19, 19, 40, 41, 45, 48,
105  19, 19, 19, 40, 41, 42, 46, 49,
106  19, 19, 40, 41, 42, 43, 48, 101,
107  19, 38, 41, 42, 43, 44, 98, 104,
108  42, 43, 45, 46, 48, 98, 109, 116,
109  44, 45, 48, 49, 101, 104, 116, 123,
110 };
111 
112 static const uint8_t hqx_quant_chroma[64] = {
113  16, 16, 19, 25, 26, 26, 42, 44,
114  16, 19, 25, 25, 26, 38, 43, 91,
115  19, 25, 26, 27, 40, 41, 91, 96,
116  25, 25, 27, 40, 41, 84, 93, 197,
117  26, 26, 40, 41, 84, 86, 191, 203,
118  26, 38, 41, 84, 86, 177, 197, 209,
119  42, 43, 91, 93, 191, 197, 219, 232,
120  44, 91, 96, 197, 203, 209, 232, 246,
121 };
122 
123 static inline void put_blocks(HQXContext *ctx, int plane,
124  int x, int y, int ilace,
125  int16_t *block0, int16_t *block1,
126  const uint8_t *quant)
127 {
128  int fields = ilace ? 2 : 1;
129  int lsize = ctx->pic->linesize[plane];
130  uint8_t *p = ctx->pic->data[plane] + x * 2;
131 
132  ctx->hqxdsp.idct_put((uint16_t *)(p + y * lsize),
133  lsize * fields, block0, quant);
134  ctx->hqxdsp.idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),
135  lsize * fields, block1, quant);
136 }
137 
138 static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac,
139  int *runp, int *lev)
140 {
141  int level, run;
142  OPEN_READER(re, gb);
143 
144  UPDATE_CACHE(re, gb);
145  GET_RL_VLC(level, run, re, gb, ac->lut, ac->bits, 2, 0);
146  CLOSE_READER(re, gb);
147  *runp = run;
148  *lev = level;
149 }
150 
151 static int decode_block(GetBitContext *gb, const VLCElem vlc[],
152  const unsigned *quants, int dcb,
153  int16_t block[64], int *last_dc)
154 {
155  int run, lev, pos = 0;
156  unsigned ac_idx, q;
157  int dc;
158 
159  dc = get_vlc2(gb, vlc, HQX_DC_VLC_BITS, 2);
160  *last_dc += dc;
161 
162  block[0] = sign_extend(*last_dc << (12 - dcb), 12);
163 
164  q = quants[get_bits(gb, 2)];
165  // ac_idx is encoded in the high bits of quants;
166  // because block is 16 bit, we do not even need to clear said bits.
167  ac_idx = q >> 29;
168 
169  do {
170  hqx_get_ac(gb, &hqx_ac[ac_idx], &run, &lev);
171  pos += run;
172  if (pos > 63)
173  break;
174  block[ff_zigzag_direct[pos]] = lev * q;
175  } while (pos < 63);
176 
177  return 0;
178 }
179 
180 static int hqx_decode_422(HQXContext *ctx, int slice_no, int x, int y)
181 {
182  HQXSlice *slice = &ctx->slice[slice_no];
183  GetBitContext *gb = &slice->gb;
184  const unsigned *quants;
185  int flag;
186  int last_dc;
187  int i, ret;
188 
189  memset(slice->block, 0, sizeof(*slice->block) * 8);
190 
191  if (ctx->interlaced)
192  flag = get_bits1(gb);
193  else
194  flag = 0;
195 
196  quants = hqx_quants[get_bits(gb, 4)];
197 
198  for (i = 0; i < 8; i++) {
199  if (i == 0 || i == 4 || i == 6)
200  last_dc = 0;
201  ret = decode_block(gb, ctx->dc_vlc, quants,
202  ctx->dcb, slice->block[i], &last_dc);
203  if (ret < 0)
204  return ret;
205  }
206 
207  put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[2], hqx_quant_luma);
208  put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[3], hqx_quant_luma);
209  put_blocks(ctx, 2, x >> 1, y, flag, slice->block[4], slice->block[5], hqx_quant_chroma);
210  put_blocks(ctx, 1, x >> 1, y, flag, slice->block[6], slice->block[7], hqx_quant_chroma);
211 
212  return 0;
213 }
214 
215 static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y)
216 {
217  HQXSlice *slice = &ctx->slice[slice_no];
218  GetBitContext *gb = &slice->gb;
219  int flag = 0;
220  int last_dc;
221  int i, ret;
222  int cbp;
223 
224  memset(slice->block, 0, sizeof(*slice->block) * 12);
225  for (i = 0; i < 12; i++)
226  slice->block[i][0] = -0x800;
227 
228  cbp = get_vlc2(gb, cbp_vlc, HQX_CBP_VLC_BITS, 1);
229  if (cbp) {
230  const unsigned *quants;
231 
232  if (ctx->interlaced)
233  flag = get_bits1(gb);
234 
235  quants = hqx_quants[get_bits(gb, 4)];
236 
237  cbp |= cbp << 4; // alpha CBP
238  if (cbp & 0x3) // chroma CBP - top
239  cbp |= 0x500;
240  if (cbp & 0xC) // chroma CBP - bottom
241  cbp |= 0xA00;
242  for (i = 0; i < 12; i++) {
243  if (i == 0 || i == 4 || i == 8 || i == 10)
244  last_dc = 0;
245  if (cbp & (1 << i)) {
246  ret = decode_block(gb, ctx->dc_vlc, quants,
247  ctx->dcb, slice->block[i], &last_dc);
248  if (ret < 0)
249  return ret;
250  }
251  }
252  }
253 
254  put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma);
255  put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma);
256  put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma);
257  put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma);
258  put_blocks(ctx, 2, x >> 1, y, flag, slice->block[ 8], slice->block[ 9], hqx_quant_chroma);
259  put_blocks(ctx, 1, x >> 1, y, flag, slice->block[10], slice->block[11], hqx_quant_chroma);
260 
261  return 0;
262 }
263 
264 static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y)
265 {
266  HQXSlice *slice = &ctx->slice[slice_no];
267  GetBitContext *gb = &slice->gb;
268  const unsigned *quants;
269  int flag;
270  int last_dc;
271  int i, ret;
272 
273  memset(slice->block, 0, sizeof(*slice->block) * 12);
274 
275  if (ctx->interlaced)
276  flag = get_bits1(gb);
277  else
278  flag = 0;
279 
280  quants = hqx_quants[get_bits(gb, 4)];
281 
282  for (i = 0; i < 12; i++) {
283  if (!(i & 3))
284  last_dc = 0;
285  ret = decode_block(gb, ctx->dc_vlc, quants,
286  ctx->dcb, slice->block[i], &last_dc);
287  if (ret < 0)
288  return ret;
289  }
290 
291  put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[ 2], hqx_quant_luma);
292  put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[ 3], hqx_quant_luma);
293  put_blocks(ctx, 2, x, y, flag, slice->block[4], slice->block[ 6], hqx_quant_chroma);
294  put_blocks(ctx, 2, x + 8, y, flag, slice->block[5], slice->block[ 7], hqx_quant_chroma);
295  put_blocks(ctx, 1, x, y, flag, slice->block[8], slice->block[10], hqx_quant_chroma);
296  put_blocks(ctx, 1, x + 8, y, flag, slice->block[9], slice->block[11], hqx_quant_chroma);
297 
298  return 0;
299 }
300 
301 static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y)
302 {
303  HQXSlice *slice = &ctx->slice[slice_no];
304  GetBitContext *gb = &slice->gb;
305  int flag = 0;
306  int last_dc;
307  int i, ret;
308  int cbp;
309 
310  memset(slice->block, 0, sizeof(*slice->block) * 16);
311  for (i = 0; i < 16; i++)
312  slice->block[i][0] = -0x800;
313 
314  cbp = get_vlc2(gb, cbp_vlc, HQX_CBP_VLC_BITS, 1);
315  if (cbp) {
316  const unsigned *quants;
317 
318  if (ctx->interlaced)
319  flag = get_bits1(gb);
320 
321  quants = hqx_quants[get_bits(gb, 4)];
322 
323  cbp |= cbp << 4; // alpha CBP
324  cbp |= cbp << 8; // chroma CBP
325  for (i = 0; i < 16; i++) {
326  if (!(i & 3))
327  last_dc = 0;
328  if (cbp & (1 << i)) {
329  ret = decode_block(gb, ctx->dc_vlc, quants,
330  ctx->dcb, slice->block[i], &last_dc);
331  if (ret < 0)
332  return ret;
333  }
334  }
335  }
336 
337  put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma);
338  put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma);
339  put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma);
340  put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma);
341  put_blocks(ctx, 2, x, y, flag, slice->block[ 8], slice->block[10], hqx_quant_chroma);
342  put_blocks(ctx, 2, x + 8, y, flag, slice->block[ 9], slice->block[11], hqx_quant_chroma);
343  put_blocks(ctx, 1, x, y, flag, slice->block[12], slice->block[14], hqx_quant_chroma);
344  put_blocks(ctx, 1, x + 8, y, flag, slice->block[13], slice->block[15], hqx_quant_chroma);
345 
346  return 0;
347 }
348 
349 static const int shuffle_16[16] = {
350  0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12
351 };
352 
353 static int decode_slice(HQXContext *ctx, int slice_no)
354 {
355  int mb_w = (ctx->width + 15) >> 4;
356  int mb_h = (ctx->height + 15) >> 4;
357  int grp_w = (mb_w + 4) / 5;
358  int grp_h = (mb_h + 4) / 5;
359  int grp_h_edge = grp_w * (mb_w / grp_w);
360  int grp_v_edge = grp_h * (mb_h / grp_h);
361  int grp_v_rest = mb_w - grp_h_edge;
362  int grp_h_rest = mb_h - grp_v_edge;
363  int num_mbs = mb_w * mb_h;
364  int num_tiles = (num_mbs + 479) / 480;
365  int std_tile_blocks = num_mbs / (16 * num_tiles);
366  int g_tile = slice_no * num_tiles;
367  int blk_addr, loc_addr, mb_x, mb_y, pos, loc_row, i;
368  int tile_blocks, tile_limit, tile_no;
369 
370  for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) {
371  tile_blocks = std_tile_blocks;
372  tile_limit = -1;
373  if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) {
374  tile_limit = num_mbs / (16 * num_tiles);
375  tile_blocks++;
376  }
377  for (i = 0; i < tile_blocks; i++) {
378  if (i == tile_limit)
379  blk_addr = g_tile + 16 * num_tiles * i;
380  else
381  blk_addr = tile_no + 16 * num_tiles * i +
382  num_tiles * shuffle_16[(i + slice_no) & 0xF];
383  loc_row = grp_h * (blk_addr / (grp_h * mb_w));
384  loc_addr = blk_addr % (grp_h * mb_w);
385  if (loc_row >= grp_v_edge) {
386  mb_x = grp_w * (loc_addr / (grp_h_rest * grp_w));
387  pos = loc_addr % (grp_h_rest * grp_w);
388  } else {
389  mb_x = grp_w * (loc_addr / (grp_h * grp_w));
390  pos = loc_addr % (grp_h * grp_w);
391  }
392  if (mb_x >= grp_h_edge) {
393  mb_x += pos % grp_v_rest;
394  mb_y = loc_row + (pos / grp_v_rest);
395  } else {
396  mb_x += pos % grp_w;
397  mb_y = loc_row + (pos / grp_w);
398  }
399  ctx->decode_func(ctx, slice_no, mb_x * 16, mb_y * 16);
400  }
401  }
402 
403  return 0;
404 }
405 
406 static int decode_slice_thread(AVCodecContext *avctx, void *arg,
407  int slice_no, int threadnr)
408 {
409  HQXContext *ctx = avctx->priv_data;
410  uint32_t *slice_off = ctx->slice_off;
411  int ret;
412 
413  if (slice_off[slice_no] < HQX_HEADER_SIZE ||
414  slice_off[slice_no] >= slice_off[slice_no + 1] ||
415  slice_off[slice_no + 1] > ctx->data_size) {
416  av_log(avctx, AV_LOG_ERROR, "Invalid slice size %d.\n", ctx->data_size);
417  return AVERROR_INVALIDDATA;
418  }
419 
420  ret = init_get_bits8(&ctx->slice[slice_no].gb,
421  ctx->src + slice_off[slice_no],
422  slice_off[slice_no + 1] - slice_off[slice_no]);
423  if (ret < 0)
424  return ret;
425 
426  return decode_slice(ctx, slice_no);
427 }
428 
430  int *got_picture_ptr, AVPacket *avpkt)
431 {
432  HQXContext *ctx = avctx->priv_data;
433  const uint8_t *src = avpkt->data;
434  uint32_t info_tag;
435  int data_start, dcb_code;
436  int i, ret;
437 
438  if (avpkt->size < 4 + 4) {
439  av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
440  return AVERROR_INVALIDDATA;
441  }
442 
443  info_tag = AV_RL32(src);
444  if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
445  uint32_t info_offset = AV_RL32(src + 4);
446  if (info_offset > INT_MAX || info_offset + 8 > avpkt->size) {
447  av_log(avctx, AV_LOG_ERROR,
448  "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
449  info_offset);
450  return AVERROR_INVALIDDATA;
451  }
452  ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
453 
454  info_offset += 8;
455  src += info_offset;
456  }
457 
458  data_start = src - avpkt->data;
459  ctx->data_size = avpkt->size - data_start;
460  ctx->src = src;
461  ctx->pic = frame;
462 
463  if (ctx->data_size < HQX_HEADER_SIZE) {
464  av_log(avctx, AV_LOG_ERROR, "Frame too small.\n");
465  return AVERROR_INVALIDDATA;
466  }
467 
468  if (src[0] != 'H' || src[1] != 'Q') {
469  av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n");
470  return AVERROR_INVALIDDATA;
471  }
472  ctx->interlaced = !(src[2] & 0x80);
473  ctx->format = src[2] & 7;
474  dcb_code = src[3] & 3;
475  ctx->width = AV_RB16(src + 4);
476  ctx->height = AV_RB16(src + 6);
477  for (i = 0; i < 17; i++)
478  ctx->slice_off[i] = AV_RB24(src + 8 + i * 3);
479 
480  if (dcb_code == 0) {
481  av_log(avctx, AV_LOG_ERROR, "Invalid DC precision 8.\n");
482  return AVERROR_INVALIDDATA;
483  }
484  ctx->dc_vlc = dcb_code == 3 ? ctx->dc11_vlc.table : dc_vlc[dcb_code - 1];
485  ctx->dcb = dcb_code + 8;
486  ret = av_image_check_size(ctx->width, ctx->height, 0, avctx);
487  if (ret < 0) {
488  av_log(avctx, AV_LOG_ERROR, "Invalid stored dimensions %dx%d.\n",
489  ctx->width, ctx->height);
490  return AVERROR_INVALIDDATA;
491  }
492 
493  avctx->coded_width = FFALIGN(ctx->width, 16);
494  avctx->coded_height = FFALIGN(ctx->height, 16);
495  avctx->width = ctx->width;
496  avctx->height = ctx->height;
497  avctx->bits_per_raw_sample = 10;
498 
499  //The minimum size is 2bit per macroblock
500  // hqx_decode_422 & hqx_decode_444 have a unconditionally stored 4bits hqx_quants index
501  // hqx_decode_422a & hqx_decode_444a use cbp_vlc which has a minimum length of 2 bits for its VLCs
502  // The code rejects slices overlapping in their input data
503  if (avctx->coded_width / 16 * (avctx->coded_height / 16) *
504  (100 - avctx->discard_damaged_percentage) / 100 > 4LL * avpkt->size)
505  return AVERROR_INVALIDDATA;
506 
507  switch (ctx->format) {
508  case HQX_422:
509  avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
510  ctx->decode_func = hqx_decode_422;
511  break;
512  case HQX_444:
513  avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
514  ctx->decode_func = hqx_decode_444;
515  break;
516  case HQX_422A:
518  ctx->decode_func = hqx_decode_422a;
519  break;
520  case HQX_444A:
522  ctx->decode_func = hqx_decode_444a;
523  break;
524  default:
525  av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format);
526  return AVERROR_INVALIDDATA;
527  }
528 
529  ret = ff_thread_get_buffer(avctx, frame, 0);
530  if (ret < 0)
531  return ret;
532 
533  avctx->execute2(avctx, decode_slice_thread, NULL, NULL, 16);
534 
535  *got_picture_ptr = 1;
536 
537  return avpkt->size;
538 }
539 
541 {
542  HQXContext *ctx = avctx->priv_data;
543 
544  ff_vlc_free(&ctx->dc11_vlc);
545 
546  return 0;
547 }
548 
550 {
551  static AVOnce init_static_once = AV_ONCE_INIT;
552  HQXContext *ctx = avctx->priv_data;
554  dc11_vlc_lens, 1, 1, dc11_vlc_bits, 2, 2, 0);
555 
556  if (ret < 0)
557  return ret;
558 
559  ff_hqxdsp_init(&ctx->hqxdsp);
560 
561  ff_thread_once(&init_static_once, hqx_init_static);
562 
563  return 0;
564 }
565 
567  .p.name = "hqx",
568  CODEC_LONG_NAME("Canopus HQX"),
569  .p.type = AVMEDIA_TYPE_VIDEO,
570  .p.id = AV_CODEC_ID_HQX,
571  .priv_data_size = sizeof(HQXContext),
574  .close = hqx_decode_close,
575  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
577  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
578 };
hqx_get_ac
static void hqx_get_ac(GetBitContext *gb, const HQXAC *ac, int *runp, int *lev)
Definition: hqx.c:138
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:579
hqx_decode_init
static av_cold int hqx_decode_init(AVCodecContext *avctx)
Definition: hqx.c:549
level
uint8_t level
Definition: svq3.c:205
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
HQXContext::data_size
unsigned int data_size
Definition: hqx.c:68
HQXContext::hqxdsp
HQXDSPContext hqxdsp
Definition: hqx.c:58
hqx_decode_422a
static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y)
Definition: hqx.c:215
mem_internal.h
ff_hqx_decoder
const FFCodec ff_hqx_decoder
Definition: hqx.c:566
HQX_422
@ HQX_422
Definition: hqx.c:41
HQX_CBP_VLC_BITS
#define HQX_CBP_VLC_BITS
Definition: hqxvlc.h:31
HQXSlice::gb
GetBitContext gb
Definition: hqx.c:53
thread.h
HQXAC::bits
int bits
Definition: hqxvlc.h:45
HQXContext::width
int width
Definition: hqx.c:64
HQXContext::pic
AVFrame * pic
Definition: hqx.c:61
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:410
AVPacket::data
uint8_t * data
Definition: packet.h:539
hqx_decode_422
static int hqx_decode_422(HQXContext *ctx, int slice_no, int x, int y)
Definition: hqx.c:180
hqx_decode_close
static av_cold int hqx_decode_close(AVCodecContext *avctx)
Definition: hqx.c:540
FFCodec
Definition: codec_internal.h:127
HQXContext::decode_func
mb_decode_func decode_func
Definition: hqx.c:62
HQX_DC_VLC_BITS
#define HQX_DC_VLC_BITS
Definition: hqxvlc.h:32
UPDATE_CACHE
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:208
thread.h
ff_hqxdsp_init
av_cold void ff_hqxdsp_init(HQXDSPContext *c)
Definition: hqxdsp.c:128
decode_slice_thread
static int decode_slice_thread(AVCodecContext *avctx, void *arg, int slice_no, int threadnr)
Definition: hqx.c:406
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:318
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
hqx_quant_luma
static const uint8_t hqx_quant_luma[64]
Definition: hqx.c:101
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:580
ff_canopus_parse_info_tag
int ff_canopus_parse_info_tag(AVCodecContext *avctx, const uint8_t *src, size_t size)
Definition: canopus.c:30
GetBitContext
Definition: get_bits.h:108
hqx_quant_chroma
static const uint8_t hqx_quant_chroma[64]
Definition: hqx.c:112
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:647
hqx_ac
static HQXAC hqx_ac[NUM_HQX_AC]
Definition: hqxvlc.h:749
Q
#define Q(q)
quant
static const uint8_t quant[64]
Definition: vmixdec.c:71
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:540
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:528
CLOSE_READER
#define CLOSE_READER(name, gb)
Definition: get_bits.h:184
HQXContext::slice
HQXSlice slice[16]
Definition: hqx.c:59
HQXDSPContext
Definition: hqxdsp.h:32
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:341
intreadwrite.h
HQX_444
@ HQX_444
Definition: hqx.c:42
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:541
hqx_decode_444
static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y)
Definition: hqx.c:264
ff_thread_get_buffer
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:1053
vlc_init
#define vlc_init(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:70
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1593
ctx
AVFormatContext * ctx
Definition: movenc.c:49
get_bits.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:326
arg
const char * arg
Definition: jacosubdec.c:67
fields
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the then the processing requires a frame on this link and the filter is expected to make efforts in that direction The status of input links is stored by the fifo and status_out fields
Definition: filter_design.txt:155
dc_vlc
static VLCElem dc_vlc[1104]
Definition: clearvideo.c:79
hqxvlc.h
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
HQX_HEADER_SIZE
#define HQX_HEADER_SIZE
Definition: hqx.c:76
NULL
#define NULL
Definition: coverity.c:32
run
uint8_t run
Definition: svq3.c:204
HQXContext::dc11_vlc
VLC dc11_vlc
Definition: hqx.c:73
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:371
HQXContext::dcb
int dcb
Definition: hqx.c:64
hqx_init_static
static av_cold av_unused void hqx_init_static(void)
Definition: hqxvlc.h:1545
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:635
canopus.h
hqx_quants
static const unsigned hqx_quants[16][4]
Definition: hqx.c:87
AVOnce
#define AVOnce
Definition: thread.h:202
HQXAC::lut
const RL_VLC_ELEM * lut
Definition: hqxvlc.h:46
HQXSlice
Definition: hqx.c:52
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
dc
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
VLCElem
Definition: vlc.h:32
dc11_vlc_lens
static const uint8_t dc11_vlc_lens[2048]
Definition: hqxvlc.h:617
put_blocks
static void put_blocks(HQXContext *ctx, int plane, int x, int y, int ilace, int16_t *block0, int16_t *block1, const uint8_t *quant)
Definition: hqx.c:123
frame.h
hqxdsp.h
OPEN_READER
#define OPEN_READER(name, gb)
Definition: get_bits.h:173
block1
static int16_t block1[64]
Definition: dct.c:121
mb_decode_func
int(* mb_decode_func)(struct HQXContext *ctx, int slice_no, int x, int y)
Definition: hqx.c:49
AV_CODEC_CAP_SLICE_THREADS
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:114
HQXContext
Definition: hqx.c:57
shuffle_16
static const int shuffle_16[16]
Definition: hqx.c:349
HQXSlice::block
int16_t block[16][64]
Definition: hqx.c:54
HQXContext::format
int format
Definition: hqx.c:64
flag
#define flag(name)
Definition: cbs_av1.c:474
hqx_decode_444a
static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y)
Definition: hqx.c:301
AV_CODEC_ID_HQX
@ AV_CODEC_ID_HQX
Definition: codec_id.h:240
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
HQXContext::slice_off
uint32_t slice_off[17]
Definition: hqx.c:69
lev
static LevelCodes lev[4+3+3]
Definition: clearvideo.c:80
AVCodecContext::height
int height
Definition: avcodec.h:632
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:671
avcodec.h
GET_RL_VLC
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition: get_bits.h:589
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
ff_vlc_free
void ff_vlc_free(VLC *vlc)
Definition: vlc.c:580
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
HQXContext::interlaced
int interlaced
Definition: hqx.c:65
pos
unsigned int pos
Definition: spdifenc.c:414
decode_slice
static int decode_slice(HQXContext *ctx, int slice_no)
Definition: hqx.c:353
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
dc11_vlc_bits
static const uint16_t dc11_vlc_bits[2048]
Definition: hqxvlc.h:358
HQX_444A
@ HQX_444A
Definition: hqx.c:44
AVCodecContext
main external API structure.
Definition: avcodec.h:451
VLC
Definition: vlc.h:50
HQXAC
Definition: hqxvlc.h:44
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:131
decode_block
static int decode_block(GetBitContext *gb, const VLCElem vlc[], const unsigned *quants, int dcb, int16_t block[64], int *last_dc)
Definition: hqx.c:151
AVCodecContext::discard_damaged_percentage
int discard_damaged_percentage
The percentage of damaged samples to discard a frame.
Definition: avcodec.h:1999
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:647
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
hqx_decode_frame
static int hqx_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_picture_ptr, AVPacket *avpkt)
Definition: hqx.c:429
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
vlc.h
cbp_vlc
static VLCElem cbp_vlc[(1<< HQX_CBP_VLC_BITS)+896+1344+15630]
Definition: hqxvlc.h:1533
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:632
imgutils.h
HQXContext::src
const uint8_t * src
Definition: hqx.c:67
HQXContext::dc_vlc
const VLCElem * dc_vlc
Definition: hqx.c:71
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
HQXFormat
HQXFormat
Definition: hqx.c:40
HQXContext::height
int height
Definition: hqx.c:64
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
av_image_check_size
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:318
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97
AVCodecContext::execute2
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:1650
src
#define src
Definition: vp8dsp.c:248
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
HQX_422A
@ HQX_422A
Definition: hqx.c:43