FFmpeg
speedhqdec.c
Go to the documentation of this file.
1 /*
2  * NewTek SpeedHQ codec
3  * Copyright 2017 Steinar H. Gunderson
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * NewTek SpeedHQ decoder.
25  */
26 
27 #define BITSTREAM_READER_LE
28 
29 #include "libavutil/attributes.h"
30 #include "libavutil/mem_internal.h"
31 
32 #include "avcodec.h"
33 #include "blockdsp.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "get_bits.h"
37 #include "idctdsp.h"
38 #include "libavutil/thread.h"
39 #include "mathops.h"
40 #include "mpeg12data.h"
41 #include "mpeg12vlc.h"
42 #include "speedhq.h"
43 #include "thread.h"
44 
45 #define MAX_INDEX (64 - 1)
46 
47 /*
48  * 5 bits makes for very small tables, with no more than two lookups needed
49  * for the longest (10-bit) codes.
50  */
51 #define ALPHA_VLC_BITS 5
52 
53 typedef struct SHQContext {
57  int quant_matrix[64];
61 } SHQContext;
62 
63 /* NOTE: The first element is always 16, unscaled. */
64 static const uint8_t unscaled_quant_matrix[64] = {
65  16, 16, 19, 22, 26, 27, 29, 34,
66  16, 16, 22, 24, 27, 29, 34, 37,
67  19, 22, 26, 27, 29, 34, 34, 38,
68  22, 22, 26, 27, 29, 34, 37, 40,
69  22, 26, 27, 29, 32, 35, 40, 48,
70  26, 27, 29, 32, 35, 40, 48, 58,
71  26, 27, 29, 34, 38, 46, 56, 69,
72  27, 29, 35, 38, 46, 56, 69, 83
73 };
74 
75 static VLCElem dc_lum_vlc_le[512];
79 
81 
82 static inline int decode_dc_le(GetBitContext *gb, int component)
83 {
84  int code, diff;
85 
86  if (component == 0 || component == 3) {
88  } else {
90  }
91  if (!code) {
92  diff = 0;
93  } else {
94  diff = get_xbits_le(gb, code);
95  }
96  return diff;
97 }
98 
99 static inline int decode_alpha_block(const SHQContext *s, GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest, int linesize)
100 {
101  uint8_t block[128];
102  int i = 0, x, y;
103 
104  memset(block, 0, sizeof(block));
105 
106  {
107  OPEN_READER(re, gb);
108 
109  for ( ;; ) {
110  int run, level;
111 
112  UPDATE_CACHE_LE(re, gb);
114 
115  if (run < 0) break;
116  i += run;
117  if (i >= 128)
118  return AVERROR_INVALIDDATA;
119 
120  UPDATE_CACHE_LE(re, gb);
122  block[i++] = level;
123  }
124 
125  CLOSE_READER(re, gb);
126  }
127 
128  for (y = 0; y < 8; y++) {
129  for (x = 0; x < 16; x++) {
130  last_alpha[x] -= block[y * 16 + x];
131  }
132  memcpy(dest, last_alpha, 16);
133  dest += linesize;
134  }
135 
136  return 0;
137 }
138 
139 static inline int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
140 {
141  const int *quant_matrix = s->quant_matrix;
142  const uint8_t *scantable = s->permutated_intra_scantable;
143  LOCAL_ALIGNED_32(int16_t, block, [64]);
144  int dc_offset;
145 
146  s->bdsp.clear_block(block);
147 
148  dc_offset = decode_dc_le(gb, component);
149  last_dc[component] -= dc_offset; /* Note: Opposite of most codecs. */
150  block[scantable[0]] = last_dc[component]; /* quant_matrix[0] is always 16. */
151 
152  /* Read AC coefficients. */
153  {
154  int i = 0;
155  OPEN_READER(re, gb);
156  for ( ;; ) {
157  int level, run;
158  UPDATE_CACHE_LE(re, gb);
160  TEX_VLC_BITS, 2, 0);
161  if (level == 127) {
162  break;
163  } else if (level) {
164  i += run;
165  if (i > MAX_INDEX)
166  return AVERROR_INVALIDDATA;
167  /* If next bit is 1, level = -level */
168  level = (level ^ SHOW_SBITS(re, gb, 1)) -
169  SHOW_SBITS(re, gb, 1);
170  LAST_SKIP_BITS(re, gb, 1);
171  } else {
172  /* Escape. */
173 #if MIN_CACHE_BITS < 6 + 6 + 12
174 #error MIN_CACHE_BITS is too small for the escape code, add UPDATE_CACHE
175 #endif
176  run = SHOW_UBITS(re, gb, 6) + 1;
177  SKIP_BITS(re, gb, 6);
178  level = SHOW_UBITS(re, gb, 12) - 2048;
179  LAST_SKIP_BITS(re, gb, 12);
180 
181  i += run;
182  if (i > MAX_INDEX)
183  return AVERROR_INVALIDDATA;
184  }
185 
186  block[scantable[i]] = (level * quant_matrix[i]) >> 4;
187  }
188  CLOSE_READER(re, gb);
189  }
190 
191  s->idsp.idct_put(dest, linesize, block);
192 
193  return 0;
194 }
195 
196 static int decode_speedhq_border(const SHQContext *s, GetBitContext *gb, AVFrame *frame, int field_number, int line_stride)
197 {
198  int linesize_y = frame->linesize[0] * line_stride;
199  int linesize_cb = frame->linesize[1] * line_stride;
200  int linesize_cr = frame->linesize[2] * line_stride;
201  int linesize_a;
202  int ret;
203 
204  if (s->alpha_type != SHQ_NO_ALPHA)
205  linesize_a = frame->linesize[3] * line_stride;
206 
207  for (int y = 0; y < frame->height; y += 16 * line_stride) {
208  int last_dc[4] = { 1024, 1024, 1024, 1024 };
209  uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
210  uint8_t last_alpha[16];
211  int x = frame->width - 8;
212 
213  dest_y = frame->data[0] + frame->linesize[0] * (y + field_number) + x;
214  if (s->subsampling == SHQ_SUBSAMPLING_420) {
215  dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number) + x / 2;
216  dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number) + x / 2;
217  } else {
218  av_assert2(s->subsampling == SHQ_SUBSAMPLING_422);
219  dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number) + x / 2;
220  dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number) + x / 2;
221  }
222  if (s->alpha_type != SHQ_NO_ALPHA) {
223  memset(last_alpha, 255, sizeof(last_alpha));
224  dest_a = frame->data[3] + frame->linesize[3] * (y + field_number) + x;
225  }
226 
227  if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y, linesize_y)) < 0)
228  return ret;
229  if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
230  return ret;
231  if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
232  return ret;
233  if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
234  return ret;
235  if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
236  return ret;
237  if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
238  return ret;
239 
240  if (s->subsampling != SHQ_SUBSAMPLING_420) {
241  if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
242  return ret;
243  if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
244  return ret;
245  }
246 
247  if (s->alpha_type == SHQ_RLE_ALPHA) {
248  /* Alpha coded using 16x8 RLE blocks. */
249  if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a, linesize_a)) < 0)
250  return ret;
251  if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
252  return ret;
253  } else if (s->alpha_type == SHQ_DCT_ALPHA) {
254  /* Alpha encoded exactly like luma. */
255  if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a, linesize_a)) < 0)
256  return ret;
257  if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
258  return ret;
259  if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
260  return ret;
261  if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
262  return ret;
263  }
264  }
265 
266  return 0;
267 }
268 
269 static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf_size, AVFrame *frame, int field_number, int start, int end, int line_stride)
270 {
271  int ret, slice_number, slice_offsets[5];
272  int linesize_y = frame->linesize[0] * line_stride;
273  int linesize_cb = frame->linesize[1] * line_stride;
274  int linesize_cr = frame->linesize[2] * line_stride;
275  int linesize_a;
276  GetBitContext gb;
277 
278  if (s->alpha_type != SHQ_NO_ALPHA)
279  linesize_a = frame->linesize[3] * line_stride;
280 
281  if (end < start || end - start < 3 || end > buf_size)
282  return AVERROR_INVALIDDATA;
283 
284  slice_offsets[0] = start;
285  slice_offsets[4] = end;
286  for (slice_number = 1; slice_number < 4; slice_number++) {
287  uint32_t last_offset, slice_len;
288 
289  last_offset = slice_offsets[slice_number - 1];
290  slice_len = AV_RL24(buf + last_offset);
291  slice_offsets[slice_number] = last_offset + slice_len;
292 
293  if (slice_len < 3 || slice_offsets[slice_number] > end - 3)
294  return AVERROR_INVALIDDATA;
295  }
296 
297  for (slice_number = 0; slice_number < 4; slice_number++) {
298  uint32_t slice_begin, slice_end;
299  int x, y;
300 
301  slice_begin = slice_offsets[slice_number];
302  slice_end = slice_offsets[slice_number + 1];
303 
304  if ((ret = init_get_bits8(&gb, buf + slice_begin + 3, slice_end - slice_begin - 3)) < 0)
305  return ret;
306 
307  for (y = slice_number * 16 * line_stride; y < frame->height; y += line_stride * 64) {
308  uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
309  int last_dc[4] = { 1024, 1024, 1024, 1024 };
310  uint8_t last_alpha[16];
311 
312  memset(last_alpha, 255, sizeof(last_alpha));
313 
314  dest_y = frame->data[0] + frame->linesize[0] * (y + field_number);
315  if (s->subsampling == SHQ_SUBSAMPLING_420) {
316  dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number);
317  dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number);
318  } else {
319  dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number);
320  dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number);
321  }
322  if (s->alpha_type != SHQ_NO_ALPHA) {
323  dest_a = frame->data[3] + frame->linesize[3] * (y + field_number);
324  }
325 
326  for (x = 0; x < frame->width - 8 * (s->subsampling != SHQ_SUBSAMPLING_444); x += 16) {
327  /* Decode the four luma blocks. */
328  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y, linesize_y)) < 0)
329  return ret;
330  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
331  return ret;
332  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
333  return ret;
334  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
335  return ret;
336 
337  /*
338  * Decode the first chroma block. For 4:2:0, this is the only one;
339  * for 4:2:2, it's the top block; for 4:4:4, it's the top-left block.
340  */
341  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
342  return ret;
343  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
344  return ret;
345 
346  if (s->subsampling != SHQ_SUBSAMPLING_420) {
347  /* For 4:2:2, this is the bottom block; for 4:4:4, it's the bottom-left block. */
348  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
349  return ret;
350  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
351  return ret;
352 
353  if (s->subsampling == SHQ_SUBSAMPLING_444) {
354  /* Top-right and bottom-right blocks. */
355  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8, linesize_cb)) < 0)
356  return ret;
357  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8, linesize_cr)) < 0)
358  return ret;
359  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb + 8, linesize_cb)) < 0)
360  return ret;
361  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr + 8, linesize_cr)) < 0)
362  return ret;
363 
364  dest_cb += 8;
365  dest_cr += 8;
366  }
367  }
368  dest_y += 16;
369  dest_cb += 8;
370  dest_cr += 8;
371 
372  if (s->alpha_type == SHQ_RLE_ALPHA) {
373  /* Alpha coded using 16x8 RLE blocks. */
374  if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a, linesize_a)) < 0)
375  return ret;
376  if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
377  return ret;
378  dest_a += 16;
379  } else if (s->alpha_type == SHQ_DCT_ALPHA) {
380  /* Alpha encoded exactly like luma. */
381  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a, linesize_a)) < 0)
382  return ret;
383  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
384  return ret;
385  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
386  return ret;
387  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
388  return ret;
389  dest_a += 16;
390  }
391  }
392  }
393  }
394 
395  if (s->subsampling != SHQ_SUBSAMPLING_444 && (frame->width & 15))
396  return decode_speedhq_border(s, &gb, frame, field_number, line_stride);
397 
398  return 0;
399 }
400 
401 static void compute_quant_matrix(int *output, int qscale)
402 {
403  int i;
404  for (i = 0; i < 64; i++) output[i] = unscaled_quant_matrix[ff_zigzag_direct[i]] * qscale;
405 }
406 
408  int *got_frame, AVPacket *avpkt)
409 {
410  SHQContext * const s = avctx->priv_data;
411  const uint8_t *buf = avpkt->data;
412  int buf_size = avpkt->size;
413  uint8_t quality;
414  uint32_t second_field_offset;
415  int ret;
416 
417  if (buf_size < 4 || avctx->width < 8 || avctx->width % 8 != 0)
418  return AVERROR_INVALIDDATA;
419  if (buf_size < avctx->width*avctx->height / 64 / 4)
420  return AVERROR_INVALIDDATA;
421 
422  quality = buf[0];
423  if (quality >= 100) {
424  return AVERROR_INVALIDDATA;
425  }
426 
427  if (avctx->skip_frame >= AVDISCARD_ALL)
428  return avpkt->size;
429 
430  compute_quant_matrix(s->quant_matrix, 100 - quality);
431 
432  second_field_offset = AV_RL24(buf + 1);
433  if (second_field_offset >= buf_size - 3) {
434  return AVERROR_INVALIDDATA;
435  }
436 
437  avctx->coded_width = FFALIGN(avctx->width, 16);
438  avctx->coded_height = FFALIGN(avctx->height, 16);
439 
440  if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0) {
441  return ret;
442  }
443  frame->flags |= AV_FRAME_FLAG_KEY;
444 
445  if (second_field_offset == 4 || second_field_offset == (buf_size-4)) {
446  /*
447  * Overlapping first and second fields is used to signal
448  * encoding only a single field. In this case, "height"
449  * is ambiguous; it could mean either the height of the
450  * frame as a whole, or of the field. The former would make
451  * more sense for compatibility with legacy decoders,
452  * but this matches the convention used in NDI, which is
453  * the primary user of this trick.
454  */
455  if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, buf_size, 1)) < 0)
456  return ret;
457  } else {
458  if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, second_field_offset, 2)) < 0)
459  return ret;
460  if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 1, second_field_offset, buf_size, 2)) < 0)
461  return ret;
462  }
463 
464  *got_frame = 1;
465  return buf_size;
466 }
467 
468 /*
469  * Alpha VLC. Run and level are independently coded, and would be
470  * outside the default limits for MAX_RUN/MAX_LEVEL, so we don't
471  * bother with combining them into one table.
472  */
473 static av_cold void compute_alpha_vlcs(void)
474 {
475  uint16_t run_code[134], level_code[266];
476  uint8_t run_bits[134], level_bits[266];
477  int16_t run_symbols[134], level_symbols[266];
478  int entry, i, sign;
479 
480  /* Initialize VLC for alpha run. */
481  entry = 0;
482 
483  /* 0 -> 0. */
484  run_code[entry] = 0;
485  run_bits[entry] = 1;
486  run_symbols[entry] = 0;
487  ++entry;
488 
489  /* 10xx -> xx plus 1. */
490  for (i = 0; i < 4; ++i) {
491  run_code[entry] = (i << 2) | 1;
492  run_bits[entry] = 4;
493  run_symbols[entry] = i + 1;
494  ++entry;
495  }
496 
497  /* 111xxxxxxx -> xxxxxxx. */
498  for (i = 0; i < 128; ++i) {
499  run_code[entry] = (i << 3) | 7;
500  run_bits[entry] = 10;
501  run_symbols[entry] = i;
502  ++entry;
503  }
504 
505  /* 110 -> EOB. */
506  run_code[entry] = 3;
507  run_bits[entry] = 3;
508  run_symbols[entry] = -1;
509  ++entry;
510 
511  av_assert0(entry == FF_ARRAY_ELEMS(run_code));
512 
514  FF_ARRAY_ELEMS(run_code),
515  run_bits, 1, 1,
516  run_code, 2, 2,
517  run_symbols, 2, 2, VLC_INIT_LE);
518 
519  /* Initialize VLC for alpha level. */
520  entry = 0;
521 
522  for (sign = 0; sign <= 1; ++sign) {
523  /* 1s -> -1 or +1 (depending on sign bit). */
524  level_code[entry] = (sign << 1) | 1;
525  level_bits[entry] = 2;
526  level_symbols[entry] = sign ? -1 : 1;
527  ++entry;
528 
529  /* 01sxx -> xx plus 2 (2..5 or -2..-5, depending on sign bit). */
530  for (i = 0; i < 4; ++i) {
531  level_code[entry] = (i << 3) | (sign << 2) | 2;
532  level_bits[entry] = 5;
533  level_symbols[entry] = sign ? -(i + 2) : (i + 2);
534  ++entry;
535  }
536  }
537 
538  /*
539  * 00xxxxxxxx -> xxxxxxxx, in two's complement. There are many codes
540  * here that would better be encoded in other ways (e.g. 0 would be
541  * encoded by increasing run, and +/- 1 would be encoded with a
542  * shorter code), but it doesn't hurt to allow everything.
543  */
544  for (i = 0; i < 256; ++i) {
545  level_code[entry] = i << 2;
546  level_bits[entry] = 10;
547  level_symbols[entry] = i;
548  ++entry;
549  }
550 
551  av_assert0(entry == FF_ARRAY_ELEMS(level_code));
552 
554  FF_ARRAY_ELEMS(level_code),
555  level_bits, 1, 1,
556  level_code, 2, 2,
557  level_symbols, 2, 2, VLC_INIT_LE);
558 }
559 
560 static av_cold void speedhq_static_init(void)
561 {
562  /* Exactly the same as MPEG-2, except for a little-endian reader. */
571 
575 
577 }
578 
580 {
581  int ret;
582  static AVOnce init_once = AV_ONCE_INIT;
583  SHQContext * const s = avctx->priv_data;
584 
585  ret = ff_thread_once(&init_once, speedhq_static_init);
586  if (ret)
587  return AVERROR_UNKNOWN;
588 
589  ff_blockdsp_init(&s->bdsp);
590  ff_idctdsp_init(&s->idsp, avctx);
591  ff_permute_scantable(s->permutated_intra_scantable, ff_zigzag_direct,
592  s->idsp.idct_permutation);
593 
594  switch (avctx->codec_tag) {
595  case MKTAG('S', 'H', 'Q', '0'):
596  s->subsampling = SHQ_SUBSAMPLING_420;
597  s->alpha_type = SHQ_NO_ALPHA;
598  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
599  break;
600  case MKTAG('S', 'H', 'Q', '1'):
601  s->subsampling = SHQ_SUBSAMPLING_420;
602  s->alpha_type = SHQ_RLE_ALPHA;
603  avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
604  break;
605  case MKTAG('S', 'H', 'Q', '2'):
606  s->subsampling = SHQ_SUBSAMPLING_422;
607  s->alpha_type = SHQ_NO_ALPHA;
608  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
609  break;
610  case MKTAG('S', 'H', 'Q', '3'):
611  s->subsampling = SHQ_SUBSAMPLING_422;
612  s->alpha_type = SHQ_RLE_ALPHA;
613  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
614  break;
615  case MKTAG('S', 'H', 'Q', '4'):
616  s->subsampling = SHQ_SUBSAMPLING_444;
617  s->alpha_type = SHQ_NO_ALPHA;
618  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
619  break;
620  case MKTAG('S', 'H', 'Q', '5'):
621  s->subsampling = SHQ_SUBSAMPLING_444;
622  s->alpha_type = SHQ_RLE_ALPHA;
623  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
624  break;
625  case MKTAG('S', 'H', 'Q', '7'):
626  s->subsampling = SHQ_SUBSAMPLING_422;
627  s->alpha_type = SHQ_DCT_ALPHA;
628  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
629  break;
630  case MKTAG('S', 'H', 'Q', '9'):
631  s->subsampling = SHQ_SUBSAMPLING_444;
632  s->alpha_type = SHQ_DCT_ALPHA;
633  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
634  break;
635  default:
636  av_log(avctx, AV_LOG_ERROR, "Unknown NewTek SpeedHQ FOURCC provided (%08X)\n",
637  avctx->codec_tag);
638  return AVERROR_INVALIDDATA;
639  }
640 
641  /* This matches what NDI's RGB -> Y'CbCr 4:2:2 converter uses. */
642  avctx->colorspace = AVCOL_SPC_BT470BG;
644 
645  return 0;
646 }
647 
649  .p.name = "speedhq",
650  CODEC_LONG_NAME("NewTek SpeedHQ"),
651  .p.type = AVMEDIA_TYPE_VIDEO,
652  .p.id = AV_CODEC_ID_SPEEDHQ,
653  .priv_data_size = sizeof(SHQContext),
656  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
657 };
entry
#define entry
Definition: aom_film_grain_template.c:66
level
uint8_t level
Definition: svq3.c:205
blockdsp.h
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:685
mem_internal.h
SHQContext::SHQ_SUBSAMPLING_420
@ SHQ_SUBSAMPLING_420
Definition: speedhqdec.c:58
thread.h
GET_VLC
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
Definition: get_bits.h:574
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVPacket::data
uint8_t * data
Definition: packet.h:524
FFCodec
Definition: codec_internal.h:126
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
BlockDSPContext
Definition: blockdsp.h:32
ff_mpeg12_vlc_dc_chroma_bits
const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12]
Definition: mpeg12data.c:63
thread.h
ff_idctdsp_init
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:228
quality
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
Definition: rate_distortion.txt:12
MAX_INDEX
#define MAX_INDEX
Definition: speedhqdec.c:45
ff_mpeg12_vlc_dc_lum_bits
const unsigned char ff_mpeg12_vlc_dc_lum_bits[12]
Definition: mpeg12data.c:56
SHQContext::quant_matrix
int quant_matrix[64]
Definition: speedhqdec.c:57
ff_permute_scantable
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Definition: idctdsp.c:30
AVCOL_SPC_BT470BG
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:615
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
speedhq_rl_vlc
static RL_VLC_ELEM speedhq_rl_vlc[674]
Definition: speedhqdec.c:80
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1820
ALPHA_VLC_BITS
#define ALPHA_VLC_BITS
Definition: speedhqdec.c:51
GetBitContext
Definition: get_bits.h:108
mpeg12vlc.h
dc_lum_vlc_le
static VLCElem dc_lum_vlc_le[512]
Definition: speedhqdec.c:75
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:633
SHQContext::SHQ_SUBSAMPLING_422
@ SHQ_SUBSAMPLING_422
Definition: speedhqdec.c:58
decode_alpha_block
static int decode_alpha_block(const SHQContext *s, GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest, int linesize)
Definition: speedhqdec.c:99
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:180
SHQContext::SHQ_RLE_ALPHA
@ SHQ_RLE_ALPHA
Definition: speedhqdec.c:60
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
dc_alpha_level_vlc_le
static VLCElem dc_alpha_level_vlc_le[288]
Definition: speedhqdec.c:78
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
CLOSE_READER
#define CLOSE_READER(name, gb)
Definition: get_bits.h:188
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
ff_blockdsp_init
av_cold void ff_blockdsp_init(BlockDSPContext *c)
Definition: blockdsp.c:58
s
#define s(width, name)
Definition: cbs_vp9.c:198
VLC_INIT_LE
#define VLC_INIT_LE
Definition: vlc.h:189
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:108
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:975
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:1730
SHOW_SBITS
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:260
speedhq_static_init
static av_cold void speedhq_static_init(void)
Definition: speedhqdec.c:560
ff_speedhq_decoder
const FFCodec ff_speedhq_decoder
Definition: speedhqdec.c:648
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
SHQContext
Definition: speedhqdec.c:53
decode.h
get_bits.h
SKIP_BITS
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:241
dc_alpha_run_vlc_le
static VLCElem dc_alpha_run_vlc_le[160]
Definition: speedhqdec.c:77
speedhq_decode_frame
static int speedhq_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: speedhqdec.c:407
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:219
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
LOCAL_ALIGNED_32
#define LOCAL_ALIGNED_32(t, v,...)
Definition: mem_internal.h:156
run
uint8_t run
Definition: svq3.c:204
SHQContext::SHQ_NO_ALPHA
@ SHQ_NO_ALPHA
Definition: speedhqdec.c:60
AV_CODEC_ID_SPEEDHQ
@ AV_CODEC_ID_SPEEDHQ
Definition: codec_id.h:275
mathops.h
LAST_SKIP_BITS
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:247
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:652
AVOnce
#define AVOnce
Definition: thread.h:202
ff_init_2d_vlc_rl
av_cold void ff_init_2d_vlc_rl(const uint16_t table_vlc[][2], RL_VLC_ELEM rl_vlc[], const int8_t table_run[], const uint8_t table_level[], int n, unsigned static_size, int flags)
Definition: mpeg12.c:67
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
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:525
SHQContext::permutated_intra_scantable
uint8_t permutated_intra_scantable[64]
Definition: speedhqdec.c:56
RL_VLC_ELEM
Definition: vlc.h:56
codec_internal.h
VLCElem
Definition: vlc.h:32
run_bits
static const uint8_t run_bits[7][16]
Definition: h264_cavlc.c:227
SHQContext::idsp
IDCTDSPContext idsp
Definition: speedhqdec.c:55
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:165
OPEN_READER
#define OPEN_READER(name, gb)
Definition: get_bits.h:177
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:174
attributes.h
SHQContext::bdsp
BlockDSPContext bdsp
Definition: speedhqdec.c:54
speedhq.h
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
unscaled_quant_matrix
static const uint8_t unscaled_quant_matrix[64]
Definition: speedhqdec.c:64
ff_speedhq_vlc_table
const uint16_t ff_speedhq_vlc_table[SPEEDHQ_RL_NB_ELEMS+2][2]
Definition: speedhq.c:26
SHQContext::subsampling
enum SHQContext::@182 subsampling
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
compute_alpha_vlcs
static av_cold void compute_alpha_vlcs(void)
Definition: speedhqdec.c:473
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:702
decode_dct_block
static int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
Definition: speedhqdec.c:139
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
VLC_INIT_STATIC_SPARSE_TABLE
#define VLC_INIT_STATIC_SPARSE_TABLE(vlc_table, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, symbols, symbols_wrap, symbols_size, flags)
Definition: vlc.h:258
idctdsp.h
avcodec.h
GET_RL_VLC
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition: get_bits.h:606
ff_mpeg12_vlc_dc_chroma_code
const uint16_t ff_mpeg12_vlc_dc_chroma_code[12]
Definition: mpeg12data.c:60
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
get_xbits_le
static int get_xbits_le(GetBitContext *s, int n)
Definition: get_bits.h:306
ret
ret
Definition: filter_design.txt:187
decode_speedhq_border
static int decode_speedhq_border(const SHQContext *s, GetBitContext *gb, AVFrame *frame, int field_number, int line_stride)
Definition: speedhqdec.c:196
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
TEX_VLC_BITS
#define TEX_VLC_BITS
Definition: dvdec.c:147
decode_speedhq_field
static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf_size, AVFrame *frame, int field_number, int start, int end, int line_stride)
Definition: speedhqdec.c:269
IDCTDSPContext
Definition: idctdsp.h:43
mpeg12data.h
SHQContext::SHQ_SUBSAMPLING_444
@ SHQ_SUBSAMPLING_444
Definition: speedhqdec.c:58
AVCodecContext
main external API structure.
Definition: avcodec.h:445
compute_quant_matrix
static void compute_quant_matrix(int *output, int qscale)
Definition: speedhqdec.c:401
SHOW_UBITS
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:259
UPDATE_CACHE_LE
#define UPDATE_CACHE_LE(name, gb)
Definition: get_bits.h:209
AVCHROMA_LOC_CENTER
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:708
VLC_INIT_STATIC_TABLE
#define VLC_INIT_STATIC_TABLE(vlc_table, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:270
decode_dc_le
static int decode_dc_le(GetBitContext *gb, int component)
Definition: speedhqdec.c:82
ff_speedhq_level
const uint8_t ff_speedhq_level[121]
Definition: speedhq.c:62
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
SHQContext::alpha_type
enum SHQContext::@183 alpha_type
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:633
ff_speedhq_run
const uint8_t ff_speedhq_run[121]
Definition: speedhq.c:81
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
VLC_INIT_OUTPUT_LE
#define VLC_INIT_OUTPUT_LE
Definition: vlc.h:188
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:470
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:501
dc_chroma_vlc_le
static VLCElem dc_chroma_vlc_le[514]
Definition: speedhqdec.c:76
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
ff_mpeg12_vlc_dc_lum_code
const uint16_t ff_mpeg12_vlc_dc_lum_code[12]
Definition: mpeg12data.c:53
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
speedhq_decode_init
static av_cold int speedhq_decode_init(AVCodecContext *avctx)
Definition: speedhqdec.c:579
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
DC_VLC_BITS
#define DC_VLC_BITS
Definition: intrax8.c:41
SPEEDHQ_RL_NB_ELEMS
#define SPEEDHQ_RL_NB_ELEMS
Definition: speedhq.h:27
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:173
SHQContext::SHQ_DCT_ALPHA
@ SHQ_DCT_ALPHA
Definition: speedhqdec.c:60