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 
44 #define MAX_INDEX (64 - 1)
45 
46 /*
47  * 5 bits makes for very small tables, with no more than two lookups needed
48  * for the longest (10-bit) codes.
49  */
50 #define ALPHA_VLC_BITS 5
51 
52 typedef struct SHQContext {
56  int quant_matrix[64];
60 } SHQContext;
61 
62 /* NOTE: The first element is always 16, unscaled. */
63 static const uint8_t unscaled_quant_matrix[64] = {
64  16, 16, 19, 22, 26, 27, 29, 34,
65  16, 16, 22, 24, 27, 29, 34, 37,
66  19, 22, 26, 27, 29, 34, 34, 38,
67  22, 22, 26, 27, 29, 34, 37, 40,
68  22, 26, 27, 29, 32, 35, 40, 48,
69  26, 27, 29, 32, 35, 40, 48, 58,
70  26, 27, 29, 34, 38, 46, 56, 69,
71  27, 29, 35, 38, 46, 56, 69, 83
72 };
73 
78 
80 
81 static inline int decode_dc_le(GetBitContext *gb, int component)
82 {
83  int code, diff;
84 
85  if (component == 0 || component == 3) {
87  } else {
89  }
90  if (!code) {
91  diff = 0;
92  } else {
93  diff = get_xbits_le(gb, code);
94  }
95  return diff;
96 }
97 
98 static inline int decode_alpha_block(const SHQContext *s, GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest, int linesize)
99 {
100  uint8_t block[128];
101  int i = 0, x, y;
102 
103  memset(block, 0, sizeof(block));
104 
105  {
106  OPEN_READER(re, gb);
107 
108  for ( ;; ) {
109  int run, level;
110 
111  UPDATE_CACHE_LE(re, gb);
113 
114  if (run < 0) break;
115  i += run;
116  if (i >= 128)
117  return AVERROR_INVALIDDATA;
118 
119  UPDATE_CACHE_LE(re, gb);
121  block[i++] = level;
122  }
123 
124  CLOSE_READER(re, gb);
125  }
126 
127  for (y = 0; y < 8; y++) {
128  for (x = 0; x < 16; x++) {
129  last_alpha[x] -= block[y * 16 + x];
130  }
131  memcpy(dest, last_alpha, 16);
132  dest += linesize;
133  }
134 
135  return 0;
136 }
137 
138 static inline int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
139 {
140  const int *quant_matrix = s->quant_matrix;
141  const uint8_t *scantable = s->permutated_intra_scantable;
142  LOCAL_ALIGNED_32(int16_t, block, [64]);
143  int dc_offset;
144 
145  s->bdsp.clear_block(block);
146 
147  dc_offset = decode_dc_le(gb, component);
148  last_dc[component] -= dc_offset; /* Note: Opposite of most codecs. */
149  block[scantable[0]] = last_dc[component]; /* quant_matrix[0] is always 16. */
150 
151  /* Read AC coefficients. */
152  {
153  int i = 0;
154  OPEN_READER(re, gb);
155  for ( ;; ) {
156  int level, run;
157  UPDATE_CACHE_LE(re, gb);
159  TEX_VLC_BITS, 2, 0);
160  if (level == 127) {
161  break;
162  } else if (level) {
163  i += run;
164  if (i > MAX_INDEX)
165  return AVERROR_INVALIDDATA;
166  /* If next bit is 1, level = -level */
167  level = (level ^ SHOW_SBITS(re, gb, 1)) -
168  SHOW_SBITS(re, gb, 1);
169  LAST_SKIP_BITS(re, gb, 1);
170  } else {
171  /* Escape. */
172 #if MIN_CACHE_BITS < 6 + 6 + 12
173 #error MIN_CACHE_BITS is too small for the escape code, add UPDATE_CACHE
174 #endif
175  run = SHOW_UBITS(re, gb, 6) + 1;
176  SKIP_BITS(re, gb, 6);
177  level = SHOW_UBITS(re, gb, 12) - 2048;
178  LAST_SKIP_BITS(re, gb, 12);
179 
180  i += run;
181  if (i > MAX_INDEX)
182  return AVERROR_INVALIDDATA;
183  }
184 
185  block[scantable[i]] = (level * quant_matrix[i]) >> 4;
186  }
187  CLOSE_READER(re, gb);
188  }
189 
190  s->idsp.idct_put(dest, linesize, block);
191 
192  return 0;
193 }
194 
195 static int decode_speedhq_border(const SHQContext *s, GetBitContext *gb, AVFrame *frame, int field_number, int line_stride)
196 {
197  int linesize_y = frame->linesize[0] * line_stride;
198  int linesize_cb = frame->linesize[1] * line_stride;
199  int linesize_cr = frame->linesize[2] * line_stride;
200  int linesize_a;
201  int ret;
202 
203  if (s->alpha_type != SHQ_NO_ALPHA)
204  linesize_a = frame->linesize[3] * line_stride;
205 
206  for (int y = 0; y < frame->height; y += 16 * line_stride) {
207  int last_dc[4] = { 1024, 1024, 1024, 1024 };
208  uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
209  uint8_t last_alpha[16];
210  int x = frame->width - 8;
211 
212  dest_y = frame->data[0] + frame->linesize[0] * (y + field_number) + x;
213  if (s->subsampling == SHQ_SUBSAMPLING_420) {
214  dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number) + x / 2;
215  dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number) + x / 2;
216  } else {
217  av_assert2(s->subsampling == SHQ_SUBSAMPLING_422);
218  dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number) + x / 2;
219  dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number) + x / 2;
220  }
221  if (s->alpha_type != SHQ_NO_ALPHA) {
222  memset(last_alpha, 255, sizeof(last_alpha));
223  dest_a = frame->data[3] + frame->linesize[3] * (y + field_number) + x;
224  }
225 
226  if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y, linesize_y)) < 0)
227  return ret;
228  if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
229  return ret;
230  if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
231  return ret;
232  if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
233  return ret;
234  if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
235  return ret;
236  if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
237  return ret;
238 
239  if (s->subsampling != SHQ_SUBSAMPLING_420) {
240  if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
241  return ret;
242  if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
243  return ret;
244  }
245 
246  if (s->alpha_type == SHQ_RLE_ALPHA) {
247  /* Alpha coded using 16x8 RLE blocks. */
248  if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a, linesize_a)) < 0)
249  return ret;
250  if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
251  return ret;
252  } else if (s->alpha_type == SHQ_DCT_ALPHA) {
253  /* Alpha encoded exactly like luma. */
254  if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a, linesize_a)) < 0)
255  return ret;
256  if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
257  return ret;
258  if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
259  return ret;
260  if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
261  return ret;
262  }
263  }
264 
265  return 0;
266 }
267 
268 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)
269 {
270  int ret, slice_number, slice_offsets[5];
271  int linesize_y = frame->linesize[0] * line_stride;
272  int linesize_cb = frame->linesize[1] * line_stride;
273  int linesize_cr = frame->linesize[2] * line_stride;
274  int linesize_a;
275  GetBitContext gb;
276 
277  if (s->alpha_type != SHQ_NO_ALPHA)
278  linesize_a = frame->linesize[3] * line_stride;
279 
280  if (end < start || end - start < 3 || end > buf_size)
281  return AVERROR_INVALIDDATA;
282 
283  slice_offsets[0] = start;
284  slice_offsets[4] = end;
285  for (slice_number = 1; slice_number < 4; slice_number++) {
286  uint32_t last_offset, slice_len;
287 
288  last_offset = slice_offsets[slice_number - 1];
289  slice_len = AV_RL24(buf + last_offset);
290  slice_offsets[slice_number] = last_offset + slice_len;
291 
292  if (slice_len < 3 || slice_offsets[slice_number] > end - 3)
293  return AVERROR_INVALIDDATA;
294  }
295 
296  for (slice_number = 0; slice_number < 4; slice_number++) {
297  uint32_t slice_begin, slice_end;
298  int x, y;
299 
300  slice_begin = slice_offsets[slice_number];
301  slice_end = slice_offsets[slice_number + 1];
302 
303  if ((ret = init_get_bits8(&gb, buf + slice_begin + 3, slice_end - slice_begin - 3)) < 0)
304  return ret;
305 
306  for (y = slice_number * 16 * line_stride; y < frame->height; y += line_stride * 64) {
307  uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
308  int last_dc[4] = { 1024, 1024, 1024, 1024 };
309  uint8_t last_alpha[16];
310 
311  memset(last_alpha, 255, sizeof(last_alpha));
312 
313  dest_y = frame->data[0] + frame->linesize[0] * (y + field_number);
314  if (s->subsampling == SHQ_SUBSAMPLING_420) {
315  dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number);
316  dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number);
317  } else {
318  dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number);
319  dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number);
320  }
321  if (s->alpha_type != SHQ_NO_ALPHA) {
322  dest_a = frame->data[3] + frame->linesize[3] * (y + field_number);
323  }
324 
325  for (x = 0; x < frame->width - 8 * (s->subsampling != SHQ_SUBSAMPLING_444); x += 16) {
326  /* Decode the four luma blocks. */
327  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y, linesize_y)) < 0)
328  return ret;
329  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
330  return ret;
331  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
332  return ret;
333  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
334  return ret;
335 
336  /*
337  * Decode the first chroma block. For 4:2:0, this is the only one;
338  * for 4:2:2, it's the top block; for 4:4:4, it's the top-left block.
339  */
340  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
341  return ret;
342  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
343  return ret;
344 
345  if (s->subsampling != SHQ_SUBSAMPLING_420) {
346  /* For 4:2:2, this is the bottom block; for 4:4:4, it's the bottom-left block. */
347  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
348  return ret;
349  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
350  return ret;
351 
352  if (s->subsampling == SHQ_SUBSAMPLING_444) {
353  /* Top-right and bottom-right blocks. */
354  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8, linesize_cb)) < 0)
355  return ret;
356  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8, linesize_cr)) < 0)
357  return ret;
358  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb + 8, linesize_cb)) < 0)
359  return ret;
360  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr + 8, linesize_cr)) < 0)
361  return ret;
362 
363  dest_cb += 8;
364  dest_cr += 8;
365  }
366  }
367  dest_y += 16;
368  dest_cb += 8;
369  dest_cr += 8;
370 
371  if (s->alpha_type == SHQ_RLE_ALPHA) {
372  /* Alpha coded using 16x8 RLE blocks. */
373  if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a, linesize_a)) < 0)
374  return ret;
375  if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
376  return ret;
377  dest_a += 16;
378  } else if (s->alpha_type == SHQ_DCT_ALPHA) {
379  /* Alpha encoded exactly like luma. */
380  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a, linesize_a)) < 0)
381  return ret;
382  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
383  return ret;
384  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
385  return ret;
386  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
387  return ret;
388  dest_a += 16;
389  }
390  }
391  }
392  }
393 
394  if (s->subsampling != SHQ_SUBSAMPLING_444 && (frame->width & 15))
395  return decode_speedhq_border(s, &gb, frame, field_number, line_stride);
396 
397  return 0;
398 }
399 
400 static void compute_quant_matrix(int *output, int qscale)
401 {
402  int i;
403  for (i = 0; i < 64; i++) output[i] = unscaled_quant_matrix[ff_zigzag_direct[i]] * qscale;
404 }
405 
407  int *got_frame, AVPacket *avpkt)
408 {
409  SHQContext * const s = avctx->priv_data;
410  const uint8_t *buf = avpkt->data;
411  int buf_size = avpkt->size;
412  uint8_t quality;
413  uint32_t second_field_offset;
414  int ret;
415 
416  if (buf_size < 4 || avctx->width < 8 || avctx->width % 8 != 0)
417  return AVERROR_INVALIDDATA;
418  if (buf_size < avctx->width*avctx->height / 64 / 4)
419  return AVERROR_INVALIDDATA;
420 
421  quality = buf[0];
422  if (quality >= 100) {
423  return AVERROR_INVALIDDATA;
424  }
425 
426  compute_quant_matrix(s->quant_matrix, 100 - quality);
427 
428  second_field_offset = AV_RL24(buf + 1);
429  if (second_field_offset >= buf_size - 3) {
430  return AVERROR_INVALIDDATA;
431  }
432 
433  avctx->coded_width = FFALIGN(avctx->width, 16);
434  avctx->coded_height = FFALIGN(avctx->height, 16);
435 
436  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
437  return ret;
438  }
440 
441  if (second_field_offset == 4 || second_field_offset == (buf_size-4)) {
442  /*
443  * Overlapping first and second fields is used to signal
444  * encoding only a single field. In this case, "height"
445  * is ambiguous; it could mean either the height of the
446  * frame as a whole, or of the field. The former would make
447  * more sense for compatibility with legacy decoders,
448  * but this matches the convention used in NDI, which is
449  * the primary user of this trick.
450  */
451  if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, buf_size, 1)) < 0)
452  return ret;
453  } else {
454  if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, second_field_offset, 2)) < 0)
455  return ret;
456  if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 1, second_field_offset, buf_size, 2)) < 0)
457  return ret;
458  }
459 
460  *got_frame = 1;
461  return buf_size;
462 }
463 
464 /*
465  * Alpha VLC. Run and level are independently coded, and would be
466  * outside the default limits for MAX_RUN/MAX_LEVEL, so we don't
467  * bother with combining them into one table.
468  */
469 static av_cold void compute_alpha_vlcs(void)
470 {
471  uint16_t run_code[134], level_code[266];
472  uint8_t run_bits[134], level_bits[266];
473  int16_t run_symbols[134], level_symbols[266];
474  int entry, i, sign;
475 
476  /* Initialize VLC for alpha run. */
477  entry = 0;
478 
479  /* 0 -> 0. */
480  run_code[entry] = 0;
481  run_bits[entry] = 1;
482  run_symbols[entry] = 0;
483  ++entry;
484 
485  /* 10xx -> xx plus 1. */
486  for (i = 0; i < 4; ++i) {
487  run_code[entry] = (i << 2) | 1;
488  run_bits[entry] = 4;
489  run_symbols[entry] = i + 1;
490  ++entry;
491  }
492 
493  /* 111xxxxxxx -> xxxxxxx. */
494  for (i = 0; i < 128; ++i) {
495  run_code[entry] = (i << 3) | 7;
496  run_bits[entry] = 10;
497  run_symbols[entry] = i;
498  ++entry;
499  }
500 
501  /* 110 -> EOB. */
502  run_code[entry] = 3;
503  run_bits[entry] = 3;
504  run_symbols[entry] = -1;
505  ++entry;
506 
507  av_assert0(entry == FF_ARRAY_ELEMS(run_code));
508 
510  FF_ARRAY_ELEMS(run_code),
511  run_bits, 1, 1,
512  run_code, 2, 2,
513  run_symbols, 2, 2, 160);
514 
515  /* Initialize VLC for alpha level. */
516  entry = 0;
517 
518  for (sign = 0; sign <= 1; ++sign) {
519  /* 1s -> -1 or +1 (depending on sign bit). */
520  level_code[entry] = (sign << 1) | 1;
521  level_bits[entry] = 2;
522  level_symbols[entry] = sign ? -1 : 1;
523  ++entry;
524 
525  /* 01sxx -> xx plus 2 (2..5 or -2..-5, depending on sign bit). */
526  for (i = 0; i < 4; ++i) {
527  level_code[entry] = (i << 3) | (sign << 2) | 2;
528  level_bits[entry] = 5;
529  level_symbols[entry] = sign ? -(i + 2) : (i + 2);
530  ++entry;
531  }
532  }
533 
534  /*
535  * 00xxxxxxxx -> xxxxxxxx, in two's complement. There are many codes
536  * here that would better be encoded in other ways (e.g. 0 would be
537  * encoded by increasing run, and +/- 1 would be encoded with a
538  * shorter code), but it doesn't hurt to allow everything.
539  */
540  for (i = 0; i < 256; ++i) {
541  level_code[entry] = i << 2;
542  level_bits[entry] = 10;
543  level_symbols[entry] = i;
544  ++entry;
545  }
546 
547  av_assert0(entry == FF_ARRAY_ELEMS(level_code));
548 
550  FF_ARRAY_ELEMS(level_code),
551  level_bits, 1, 1,
552  level_code, 2, 2,
553  level_symbols, 2, 2, 288);
554 }
555 
556 static av_cold void speedhq_static_init(void)
557 {
558  /* Exactly the same as MPEG-2, except for a little-endian reader. */
562  VLC_INIT_OUTPUT_LE, 512);
566  VLC_INIT_OUTPUT_LE, 514);
567 
571 
573 }
574 
576 {
577  int ret;
578  static AVOnce init_once = AV_ONCE_INIT;
579  SHQContext * const s = avctx->priv_data;
580 
581  ret = ff_thread_once(&init_once, speedhq_static_init);
582  if (ret)
583  return AVERROR_UNKNOWN;
584 
585  ff_blockdsp_init(&s->bdsp);
586  ff_idctdsp_init(&s->idsp, avctx);
587  ff_permute_scantable(s->permutated_intra_scantable, ff_zigzag_direct,
588  s->idsp.idct_permutation);
589 
590  switch (avctx->codec_tag) {
591  case MKTAG('S', 'H', 'Q', '0'):
592  s->subsampling = SHQ_SUBSAMPLING_420;
593  s->alpha_type = SHQ_NO_ALPHA;
594  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
595  break;
596  case MKTAG('S', 'H', 'Q', '1'):
597  s->subsampling = SHQ_SUBSAMPLING_420;
598  s->alpha_type = SHQ_RLE_ALPHA;
599  avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
600  break;
601  case MKTAG('S', 'H', 'Q', '2'):
602  s->subsampling = SHQ_SUBSAMPLING_422;
603  s->alpha_type = SHQ_NO_ALPHA;
604  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
605  break;
606  case MKTAG('S', 'H', 'Q', '3'):
607  s->subsampling = SHQ_SUBSAMPLING_422;
608  s->alpha_type = SHQ_RLE_ALPHA;
609  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
610  break;
611  case MKTAG('S', 'H', 'Q', '4'):
612  s->subsampling = SHQ_SUBSAMPLING_444;
613  s->alpha_type = SHQ_NO_ALPHA;
614  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
615  break;
616  case MKTAG('S', 'H', 'Q', '5'):
617  s->subsampling = SHQ_SUBSAMPLING_444;
618  s->alpha_type = SHQ_RLE_ALPHA;
619  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
620  break;
621  case MKTAG('S', 'H', 'Q', '7'):
622  s->subsampling = SHQ_SUBSAMPLING_422;
623  s->alpha_type = SHQ_DCT_ALPHA;
624  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
625  break;
626  case MKTAG('S', 'H', 'Q', '9'):
627  s->subsampling = SHQ_SUBSAMPLING_444;
628  s->alpha_type = SHQ_DCT_ALPHA;
629  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
630  break;
631  default:
632  av_log(avctx, AV_LOG_ERROR, "Unknown NewTek SpeedHQ FOURCC provided (%08X)\n",
633  avctx->codec_tag);
634  return AVERROR_INVALIDDATA;
635  }
636 
637  /* This matches what NDI's RGB -> Y'CbCr 4:2:2 converter uses. */
638  avctx->colorspace = AVCOL_SPC_BT470BG;
640 
641  return 0;
642 }
643 
645  .p.name = "speedhq",
646  CODEC_LONG_NAME("NewTek SpeedHQ"),
647  .p.type = AVMEDIA_TYPE_VIDEO,
648  .p.id = AV_CODEC_ID_SPEEDHQ,
649  .priv_data_size = sizeof(SHQContext),
652  .p.capabilities = AV_CODEC_CAP_DR1,
653 };
SHQContext::SHQ_SUBSAMPLING_420
@ SHQ_SUBSAMPLING_420
Definition: speedhqdec.c:57
level
uint8_t level
Definition: svq3.c:204
blockdsp.h
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1029
mem_internal.h
SHQContext::SHQ_SUBSAMPLING_422
@ SHQ_SUBSAMPLING_422
Definition: speedhqdec.c:57
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:340
AVFrame::width
int width
Definition: frame.h:412
AVPacket::data
uint8_t * data
Definition: packet.h:491
FFCodec
Definition: codec_internal.h:127
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:649
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
BlockDSPContext
Definition: blockdsp.h:32
SHQContext::SHQ_RLE_ALPHA
@ SHQ_RLE_ALPHA
Definition: speedhqdec.c:59
ff_mpeg12_vlc_dc_chroma_bits
const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12]
Definition: mpeg12data.c:63
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
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
MAX_INDEX
#define MAX_INDEX
Definition: speedhqdec.c:44
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:56
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:605
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
speedhq_rl_vlc
static RL_VLC_ELEM speedhq_rl_vlc[674]
Definition: speedhqdec.c:79
ALPHA_VLC_BITS
#define ALPHA_VLC_BITS
Definition: speedhqdec.c:50
GetBitContext
Definition: get_bits.h:108
mpeg12vlc.h
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:636
VLC_INIT_LE_SPARSE_STATIC
#define VLC_INIT_LE_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size)
Definition: vlc.h:199
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:98
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:203
dc_alpha_level_vlc_le
static VLC dc_alpha_level_vlc_le
Definition: speedhqdec.c:77
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
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:628
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:306
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:183
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:101
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:1979
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:556
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
ff_speedhq_decoder
const FFCodec ff_speedhq_decoder
Definition: speedhqdec.c:644
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
SHQContext
Definition: speedhqdec.c:52
decode.h
get_bits.h
SKIP_BITS
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:241
speedhq_decode_frame
static int speedhq_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: speedhqdec.c:406
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:66
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:201
LOCAL_ALIGNED_32
#define LOCAL_ALIGNED_32(t, v,...)
Definition: mem_internal.h:135
run
uint8_t run
Definition: svq3.c:203
dc_chroma_vlc_le
static VLC dc_chroma_vlc_le
Definition: speedhqdec.c:75
AV_CODEC_ID_SPEEDHQ
@ AV_CODEC_ID_SPEEDHQ
Definition: codec_id.h:278
mathops.h
LAST_SKIP_BITS
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:247
dc_lum_vlc_le
static VLC dc_lum_vlc_le
Definition: speedhqdec.c:74
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:200
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
SHQContext::SHQ_DCT_ALPHA
@ SHQ_DCT_ALPHA
Definition: speedhqdec.c:59
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1617
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:492
SHQContext::permutated_intra_scantable
uint8_t permutated_intra_scantable[64]
Definition: speedhqdec.c:55
RL_VLC_ELEM
Definition: vlc.h:50
codec_internal.h
run_bits
static const uint8_t run_bits[7][16]
Definition: h264_cavlc.c:227
SHQContext::idsp
IDCTDSPContext idsp
Definition: speedhqdec.c:54
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:164
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:167
attributes.h
SHQContext::bdsp
BlockDSPContext bdsp
Definition: speedhqdec.c:53
speedhq.h
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
SHQContext::SHQ_NO_ALPHA
@ SHQ_NO_ALPHA
Definition: speedhqdec.c:59
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
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:63
ff_speedhq_vlc_table
const uint16_t ff_speedhq_vlc_table[SPEEDHQ_RL_NB_ELEMS+2][2]
Definition: speedhq.c:26
dc_alpha_run_vlc_le
static VLC dc_alpha_run_vlc_le
Definition: speedhqdec.c:76
SHQContext::SHQ_SUBSAMPLING_444
@ SHQ_SUBSAMPLING_444
Definition: speedhqdec.c:57
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:469
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1046
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:138
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
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:195
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:268
IDCTDSPContext
Definition: idctdsp.h:45
mpeg12data.h
VLC_INIT_CUSTOM_STATIC
#define VLC_INIT_CUSTOM_STATIC(vlc, bits, a, b, c, d, e, f, g, flags, static_size)
Definition: vlc.h:203
AVCodecContext
main external API structure.
Definition: avcodec.h:441
AVFrame::height
int height
Definition: frame.h:412
compute_quant_matrix
static void compute_quant_matrix(int *output, int qscale)
Definition: speedhqdec.c:400
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:695
VLC
Definition: vlc.h:33
VLC::table
VLCElem * table
Definition: vlc.h:35
decode_dc_le
static int decode_dc_le(GetBitContext *gb, int component)
Definition: speedhqdec.c:81
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:71
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:636
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:70
VLC_INIT_OUTPUT_LE
#define VLC_INIT_OUTPUT_LE
Definition: vlc.h:182
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:466
SHQContext::alpha_type
enum SHQContext::@153 alpha_type
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
SHQContext::subsampling
enum SHQContext::@152 subsampling
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
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:575
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:40
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:166