FFmpeg
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 "canopus.h"
28 #include "get_bits.h"
29 #include "internal.h"
30 #include "thread.h"
31 
32 #include "hqx.h"
33 #include "hqxdsp.h"
34 
35 /* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */
36 enum HQXFormat {
37  HQX_422 = 0,
41 };
42 
43 #define HQX_HEADER_SIZE 59
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 put_blocks(HQXContext *ctx, int plane,
85  int x, int y, int ilace,
86  int16_t *block0, int16_t *block1,
87  const uint8_t *quant)
88 {
89  int fields = ilace ? 2 : 1;
90  int lsize = ctx->pic->linesize[plane];
91  uint8_t *p = ctx->pic->data[plane] + x * 2;
92 
93  ctx->hqxdsp.idct_put((uint16_t *)(p + y * lsize),
94  lsize * fields, block0, quant);
95  ctx->hqxdsp.idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),
96  lsize * fields, block1, quant);
97 }
98 
99 static inline void hqx_get_ac(GetBitContext *gb, const HQXAC *ac,
100  int *run, int *lev)
101 {
102  int val;
103 
104  val = show_bits(gb, ac->lut_bits);
105  if (ac->lut[val].bits == -1) {
106  GetBitContext gb2 = *gb;
107  skip_bits(&gb2, ac->lut_bits);
108  val = ac->lut[val].lev + show_bits(&gb2, ac->extra_bits);
109  }
110  *run = ac->lut[val].run;
111  *lev = ac->lut[val].lev;
112  skip_bits(gb, ac->lut[val].bits);
113 }
114 
115 static int decode_block(GetBitContext *gb, VLC *vlc,
116  const int *quants, int dcb,
117  int16_t block[64], int *last_dc)
118 {
119  int q, dc;
120  int ac_idx;
121  int run, lev, pos = 1;
122 
123  memset(block, 0, 64 * sizeof(*block));
124  dc = get_vlc2(gb, vlc->table, HQX_DC_VLC_BITS, 2);
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, int slice_no, int x, int y)
155 {
156  HQXSlice *slice = &ctx->slice[slice_no];
157  GetBitContext *gb = &slice->gb;
158  const int *quants;
159  int flag;
160  int last_dc;
161  int i, ret;
162 
163  if (ctx->interlaced)
164  flag = get_bits1(gb);
165  else
166  flag = 0;
167 
168  quants = hqx_quants[get_bits(gb, 4)];
169 
170  for (i = 0; i < 8; i++) {
171  int vlc_index = ctx->dcb - 9;
172  if (i == 0 || i == 4 || i == 6)
173  last_dc = 0;
174  ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
175  ctx->dcb, slice->block[i], &last_dc);
176  if (ret < 0)
177  return ret;
178  }
179 
180  put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[2], hqx_quant_luma);
181  put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[3], hqx_quant_luma);
182  put_blocks(ctx, 2, x >> 1, y, flag, slice->block[4], slice->block[5], hqx_quant_chroma);
183  put_blocks(ctx, 1, x >> 1, y, flag, slice->block[6], slice->block[7], hqx_quant_chroma);
184 
185  return 0;
186 }
187 
188 static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y)
189 {
190  HQXSlice *slice = &ctx->slice[slice_no];
191  GetBitContext *gb = &slice->gb;
192  const int *quants;
193  int flag = 0;
194  int last_dc;
195  int i, ret;
196  int cbp;
197 
198  cbp = get_vlc2(gb, ctx->cbp_vlc.table, HQX_CBP_VLC_BITS, 1);
199 
200  for (i = 0; i < 12; i++)
201  memset(slice->block[i], 0, sizeof(**slice->block) * 64);
202  for (i = 0; i < 12; i++)
203  slice->block[i][0] = -0x800;
204  if (cbp) {
205  if (ctx->interlaced)
206  flag = get_bits1(gb);
207 
208  quants = hqx_quants[get_bits(gb, 4)];
209 
210  cbp |= cbp << 4; // alpha CBP
211  if (cbp & 0x3) // chroma CBP - top
212  cbp |= 0x500;
213  if (cbp & 0xC) // chroma CBP - bottom
214  cbp |= 0xA00;
215  for (i = 0; i < 12; i++) {
216  if (i == 0 || i == 4 || i == 8 || i == 10)
217  last_dc = 0;
218  if (cbp & (1 << i)) {
219  int vlc_index = ctx->dcb - 9;
220  ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
221  ctx->dcb, slice->block[i], &last_dc);
222  if (ret < 0)
223  return ret;
224  }
225  }
226  }
227 
228  put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma);
229  put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma);
230  put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma);
231  put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma);
232  put_blocks(ctx, 2, x >> 1, y, flag, slice->block[ 8], slice->block[ 9], hqx_quant_chroma);
233  put_blocks(ctx, 1, x >> 1, y, flag, slice->block[10], slice->block[11], hqx_quant_chroma);
234 
235  return 0;
236 }
237 
238 static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y)
239 {
240  HQXSlice *slice = &ctx->slice[slice_no];
241  GetBitContext *gb = &slice->gb;
242  const int *quants;
243  int flag;
244  int last_dc;
245  int i, ret;
246 
247  if (ctx->interlaced)
248  flag = get_bits1(gb);
249  else
250  flag = 0;
251 
252  quants = hqx_quants[get_bits(gb, 4)];
253 
254  for (i = 0; i < 12; i++) {
255  int vlc_index = ctx->dcb - 9;
256  if (i == 0 || i == 4 || i == 8)
257  last_dc = 0;
258  ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
259  ctx->dcb, slice->block[i], &last_dc);
260  if (ret < 0)
261  return ret;
262  }
263 
264  put_blocks(ctx, 0, x, y, flag, slice->block[0], slice->block[ 2], hqx_quant_luma);
265  put_blocks(ctx, 0, x + 8, y, flag, slice->block[1], slice->block[ 3], hqx_quant_luma);
266  put_blocks(ctx, 2, x, y, flag, slice->block[4], slice->block[ 6], hqx_quant_chroma);
267  put_blocks(ctx, 2, x + 8, y, flag, slice->block[5], slice->block[ 7], hqx_quant_chroma);
268  put_blocks(ctx, 1, x, y, flag, slice->block[8], slice->block[10], hqx_quant_chroma);
269  put_blocks(ctx, 1, x + 8, y, flag, slice->block[9], slice->block[11], hqx_quant_chroma);
270 
271  return 0;
272 }
273 
274 static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y)
275 {
276  HQXSlice *slice = &ctx->slice[slice_no];
277  GetBitContext *gb = &slice->gb;
278  const int *quants;
279  int flag = 0;
280  int last_dc;
281  int i, ret;
282  int cbp;
283 
284  cbp = get_vlc2(gb, ctx->cbp_vlc.table, HQX_CBP_VLC_BITS, 1);
285 
286  for (i = 0; i < 16; i++)
287  memset(slice->block[i], 0, sizeof(**slice->block) * 64);
288  for (i = 0; i < 16; i++)
289  slice->block[i][0] = -0x800;
290  if (cbp) {
291  if (ctx->interlaced)
292  flag = get_bits1(gb);
293 
294  quants = hqx_quants[get_bits(gb, 4)];
295 
296  cbp |= cbp << 4; // alpha CBP
297  cbp |= cbp << 8; // chroma CBP
298  for (i = 0; i < 16; i++) {
299  if (i == 0 || i == 4 || i == 8 || i == 12)
300  last_dc = 0;
301  if (cbp & (1 << i)) {
302  int vlc_index = ctx->dcb - 9;
303  ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,
304  ctx->dcb, slice->block[i], &last_dc);
305  if (ret < 0)
306  return ret;
307  }
308  }
309  }
310 
311  put_blocks(ctx, 3, x, y, flag, slice->block[ 0], slice->block[ 2], hqx_quant_luma);
312  put_blocks(ctx, 3, x + 8, y, flag, slice->block[ 1], slice->block[ 3], hqx_quant_luma);
313  put_blocks(ctx, 0, x, y, flag, slice->block[ 4], slice->block[ 6], hqx_quant_luma);
314  put_blocks(ctx, 0, x + 8, y, flag, slice->block[ 5], slice->block[ 7], hqx_quant_luma);
315  put_blocks(ctx, 2, x, y, flag, slice->block[ 8], slice->block[10], hqx_quant_chroma);
316  put_blocks(ctx, 2, x + 8, y, flag, slice->block[ 9], slice->block[11], hqx_quant_chroma);
317  put_blocks(ctx, 1, x, y, flag, slice->block[12], slice->block[14], hqx_quant_chroma);
318  put_blocks(ctx, 1, x + 8, y, flag, slice->block[13], slice->block[15], hqx_quant_chroma);
319 
320  return 0;
321 }
322 
323 static const int shuffle_16[16] = {
324  0, 5, 11, 14, 2, 7, 9, 13, 1, 4, 10, 15, 3, 6, 8, 12
325 };
326 
327 static int decode_slice(HQXContext *ctx, int slice_no)
328 {
329  int mb_w = (ctx->width + 15) >> 4;
330  int mb_h = (ctx->height + 15) >> 4;
331  int grp_w = (mb_w + 4) / 5;
332  int grp_h = (mb_h + 4) / 5;
333  int grp_h_edge = grp_w * (mb_w / grp_w);
334  int grp_v_edge = grp_h * (mb_h / grp_h);
335  int grp_v_rest = mb_w - grp_h_edge;
336  int grp_h_rest = mb_h - grp_v_edge;
337  int num_mbs = mb_w * mb_h;
338  int num_tiles = (num_mbs + 479) / 480;
339  int std_tile_blocks = num_mbs / (16 * num_tiles);
340  int g_tile = slice_no * num_tiles;
341  int blk_addr, loc_addr, mb_x, mb_y, pos, loc_row, i;
342  int tile_blocks, tile_limit, tile_no;
343 
344  for (tile_no = 0; tile_no < num_tiles; tile_no++, g_tile++) {
345  tile_blocks = std_tile_blocks;
346  tile_limit = -1;
347  if (g_tile < num_mbs - std_tile_blocks * 16 * num_tiles) {
348  tile_limit = num_mbs / (16 * num_tiles);
349  tile_blocks++;
350  }
351  for (i = 0; i < tile_blocks; i++) {
352  if (i == tile_limit)
353  blk_addr = g_tile + 16 * num_tiles * i;
354  else
355  blk_addr = tile_no + 16 * num_tiles * i +
356  num_tiles * shuffle_16[(i + slice_no) & 0xF];
357  loc_row = grp_h * (blk_addr / (grp_h * mb_w));
358  loc_addr = blk_addr % (grp_h * mb_w);
359  if (loc_row >= grp_v_edge) {
360  mb_x = grp_w * (loc_addr / (grp_h_rest * grp_w));
361  pos = loc_addr % (grp_h_rest * grp_w);
362  } else {
363  mb_x = grp_w * (loc_addr / (grp_h * grp_w));
364  pos = loc_addr % (grp_h * grp_w);
365  }
366  if (mb_x >= grp_h_edge) {
367  mb_x += pos % grp_v_rest;
368  mb_y = loc_row + (pos / grp_v_rest);
369  } else {
370  mb_x += pos % grp_w;
371  mb_y = loc_row + (pos / grp_w);
372  }
373  ctx->decode_func(ctx, slice_no, mb_x * 16, mb_y * 16);
374  }
375  }
376 
377  return 0;
378 }
379 
380 static int decode_slice_thread(AVCodecContext *avctx, void *arg,
381  int slice_no, int threadnr)
382 {
383  HQXContext *ctx = avctx->priv_data;
384  uint32_t *slice_off = ctx->slice_off;
385  int ret;
386 
387  if (slice_off[slice_no] < HQX_HEADER_SIZE ||
388  slice_off[slice_no] >= slice_off[slice_no + 1] ||
389  slice_off[slice_no + 1] > ctx->data_size) {
390  av_log(avctx, AV_LOG_ERROR, "Invalid slice size %d.\n", ctx->data_size);
391  return AVERROR_INVALIDDATA;
392  }
393 
394  ret = init_get_bits8(&ctx->slice[slice_no].gb,
395  ctx->src + slice_off[slice_no],
396  slice_off[slice_no + 1] - slice_off[slice_no]);
397  if (ret < 0)
398  return ret;
399 
400  return decode_slice(ctx, slice_no);
401 }
402 
403 static int hqx_decode_frame(AVCodecContext *avctx, void *data,
404  int *got_picture_ptr, AVPacket *avpkt)
405 {
406  HQXContext *ctx = avctx->priv_data;
407  ThreadFrame frame = { .f = data };
408  uint8_t *src = avpkt->data;
409  uint32_t info_tag;
410  int data_start;
411  int i, ret;
412 
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;
416  }
417 
418  info_tag = AV_RL32(src);
419  if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
420  uint32_t info_offset = AV_RL32(src + 4);
421  if (info_offset > INT_MAX || info_offset + 8 > avpkt->size) {
422  av_log(avctx, AV_LOG_ERROR,
423  "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
424  info_offset);
425  return AVERROR_INVALIDDATA;
426  }
427  ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
428 
429  info_offset += 8;
430  src += info_offset;
431  }
432 
433  data_start = src - avpkt->data;
434  ctx->data_size = avpkt->size - data_start;
435  ctx->src = src;
436  ctx->pic = data;
437 
438  if (ctx->data_size < HQX_HEADER_SIZE) {
439  av_log(avctx, AV_LOG_ERROR, "Frame too small.\n");
440  return AVERROR_INVALIDDATA;
441  }
442 
443  if (src[0] != 'H' || src[1] != 'Q') {
444  av_log(avctx, AV_LOG_ERROR, "Not an HQX frame.\n");
445  return AVERROR_INVALIDDATA;
446  }
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);
454 
455  if (ctx->dcb == 8) {
456  av_log(avctx, AV_LOG_ERROR, "Invalid DC precision %d.\n", ctx->dcb);
457  return AVERROR_INVALIDDATA;
458  }
459  ret = av_image_check_size(ctx->width, ctx->height, 0, avctx);
460  if (ret < 0) {
461  av_log(avctx, AV_LOG_ERROR, "Invalid stored dimensions %dx%d.\n",
462  ctx->width, ctx->height);
463  return AVERROR_INVALIDDATA;
464  }
465 
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;
471 
472  //The minimum size is 2bit per macroblock
473  // hqx_decode_422 & hqx_decode_444 have a unconditionally stored 4bits hqx_quants index
474  // hqx_decode_422a & hqx_decode_444a use cbp_vlc which has a minimum length of 2 bits for its VLCs
475  // The code rejects slices overlapping in their input data
476  if (avctx->coded_width / 16 * (avctx->coded_height / 16) *
477  (100 - avctx->discard_damaged_percentage) / 100 > 4LL * avpkt->size)
478  return AVERROR_INVALIDDATA;
479 
480  switch (ctx->format) {
481  case HQX_422:
482  avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
483  ctx->decode_func = hqx_decode_422;
484  break;
485  case HQX_444:
486  avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
487  ctx->decode_func = hqx_decode_444;
488  break;
489  case HQX_422A:
491  ctx->decode_func = hqx_decode_422a;
492  break;
493  case HQX_444A:
495  ctx->decode_func = hqx_decode_444a;
496  break;
497  default:
498  av_log(avctx, AV_LOG_ERROR, "Invalid format: %d.\n", ctx->format);
499  return AVERROR_INVALIDDATA;
500  }
501 
502  ret = ff_thread_get_buffer(avctx, &frame, 0);
503  if (ret < 0)
504  return ret;
505 
506  avctx->execute2(avctx, decode_slice_thread, NULL, NULL, 16);
507 
508  ctx->pic->key_frame = 1;
509  ctx->pic->pict_type = AV_PICTURE_TYPE_I;
510 
511  *got_picture_ptr = 1;
512 
513  return avpkt->size;
514 }
515 
517 {
518  int i;
519  HQXContext *ctx = avctx->priv_data;
520 
521  ff_free_vlc(&ctx->cbp_vlc);
522  for (i = 0; i < 3; i++) {
523  ff_free_vlc(&ctx->dc_vlc[i]);
524  }
525 
526  return 0;
527 }
528 
530 {
531  HQXContext *ctx = avctx->priv_data;
532 
533  ff_hqxdsp_init(&ctx->hqxdsp);
534 
535  return ff_hqx_init_vlcs(ctx);
536 }
537 
539  .name = "hqx",
540  .long_name = NULL_IF_CONFIG_SMALL("Canopus HQX"),
541  .type = AVMEDIA_TYPE_VIDEO,
542  .id = AV_CODEC_ID_HQX,
543  .priv_data_size = sizeof(HQXContext),
546  .close = hqx_decode_close,
547  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
549  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
551 };
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:447
AVCodec
AVCodec.
Definition: codec.h:202
ff_hqx_init_vlcs
int ff_hqx_init_vlcs(HQXContext *ctx)
Definition: hqxvlc.c:2151
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
hqx_decode_init
static av_cold int hqx_decode_init(AVCodecContext *avctx)
Definition: hqx.c:529
HQXLUT::bits
int8_t bits
Definition: hqx.h:45
HQX_AC_Q8
@ HQX_AC_Q8
Definition: hqx.h:34
hqx_decode_422a
static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y)
Definition: hqx.c:188
HQX_422
@ HQX_422
Definition: hqx.c:37
HQXSlice::gb
GetBitContext gb
Definition: hqx.h:59
HQXAC::lut
const HQXLUT * lut
Definition: hqx.h:50
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
hqx_decode_422
static int hqx_decode_422(HQXContext *ctx, int slice_no, int x, int y)
Definition: hqx.c:154
hqx_decode_close
static av_cold int hqx_decode_close(AVCodecContext *avctx)
Definition: hqx.c:516
data
const char data[16]
Definition: mxf.c:143
get_vlc2
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:798
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:380
init
static int init
Definition: av_tx.c:47
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:468
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
HQXLUT::lev
int16_t lev
Definition: hqx.h:43
HQX_DC_VLC_BITS
#define HQX_DC_VLC_BITS
Definition: hqx.h:82
HQX_CBP_VLC_BITS
#define HQX_CBP_VLC_BITS
Definition: hqx.h:81
hqx_quant_luma
static const uint8_t hqx_quant_luma[64]
Definition: hqx.c:62
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:448
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:62
ff_thread_get_buffer
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in AVCodec caps_internal and use ff_thread_get_buffer() to allocate frames. The frames must then be freed with ff_thread_release_buffer(). Otherwise decode directly into the user-supplied frames. Call ff_thread_report_progress() after some part of the current picture has decoded. A good place to put this is where draw_horiz_band() is called - add this if it isn 't called anywhere
hqx_quant_chroma
static const uint8_t hqx_quant_chroma[64]
Definition: hqx.c:73
val
static double val(void *priv, double ch)
Definition: aeval.c:76
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:571
quant
static int quant(float coef, const float Q, const float rounding)
Quantize one coefficient.
Definition: aacenc_utils.h:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:416
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
HQXAC::lut_bits
int lut_bits
Definition: hqx.h:49
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
intreadwrite.h
HQX_444
@ HQX_444
Definition: hqx.c:38
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:417
hqx_decode_444
static int hqx_decode_444(HQXContext *ctx, int slice_no, int x, int y)
Definition: hqx.c:238
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1425
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
ff_free_vlc
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:431
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
HQX_AC_Q32
@ HQX_AC_Q32
Definition: hqx.h:36
if
if(ret)
Definition: filter_design.txt:179
HQXAC::extra_bits
int extra_bits
Definition: hqx.h:49
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:113
HQX_HEADER_SIZE
#define HQX_HEADER_SIZE
Definition: hqx.c:43
NULL
#define NULL
Definition: coverity.c:32
run
uint8_t run
Definition: svq3.c:203
ff_hqx_ac
const HQXAC ff_hqx_ac[NUM_HQX_AC]
Definition: hqxvlc.c:2132
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:499
src
#define src
Definition: vp8dsp.c:255
ff_hqx_decoder
const AVCodec ff_hqx_decoder
Definition: hqx.c:538
hqx.h
HQXLUT::run
uint8_t run
Definition: hqx.h:44
canopus.h
HQXSlice
Definition: hqx.h:58
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:374
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
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
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:84
decode_block
static int decode_block(GetBitContext *gb, VLC *vlc, const int *quants, int dcb, int16_t block[64], int *last_dc)
Definition: hqx.c:115
hqxdsp.h
HQX_AC_Q64
@ HQX_AC_Q64
Definition: hqx.h:37
AV_CODEC_CAP_SLICE_THREADS
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:117
HQXContext
Definition: hqx.h:63
shuffle_16
static const int shuffle_16[16]
Definition: hqx.c:323
HQXSlice::block
int16_t block[16][64]
Definition: hqx.h:60
flag
#define flag(name)
Definition: cbs_av1.c:553
hqx_decode_444a
static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y)
Definition: hqx.c:274
hqx_quants
static const int hqx_quants[16][4]
Definition: hqx.c:49
AV_CODEC_ID_HQX
@ AV_CODEC_ID_HQX
Definition: codec_id.h:236
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:447
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: internal.h:50
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
lev
static LevelCodes lev[4+3+3]
Definition: clearvideo.c:85
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
HQX_AC_Q128
@ HQX_AC_Q128
Definition: hqx.h:38
avcodec.h
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
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
pos
unsigned int pos
Definition: spdifenc.c:412
decode_slice
static int decode_slice(HQXContext *ctx, int slice_no)
Definition: hqx.c:327
hqx_decode_frame
static int hqx_decode_frame(AVCodecContext *avctx, void *data, int *got_picture_ptr, AVPacket *avpkt)
Definition: hqx.c:403
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
HQX_444A
@ HQX_444A
Definition: hqx.c:40
AVCodecContext
main external API structure.
Definition: avcodec.h:383
ThreadFrame
Definition: thread.h:34
HQX_AC_Q0
@ HQX_AC_Q0
Definition: hqx.h:33
VLC
Definition: vlc.h:26
HQXAC
Definition: hqx.h:48
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
AVCodecContext::discard_damaged_percentage
int discard_damaged_percentage
The percentage of damaged samples to discard a frame.
Definition: avcodec.h:1966
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:571
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
imgutils.h
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:28
HQXFormat
HQXFormat
Definition: hqx.c:36
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
VLC::table
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
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:1511
block1
static int16_t block1[64]
Definition: dct.c:117
hqx_get_ac
static void hqx_get_ac(GetBitContext *gb, const HQXAC *ac, int *run, int *lev)
Definition: hqx.c:99
HQX_AC_Q16
@ HQX_AC_Q16
Definition: hqx.h:35
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:39