FFmpeg
speedhq.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 
31 #include "avcodec.h"
32 #include "get_bits.h"
33 #include "internal.h"
34 #include "libavutil/thread.h"
35 #include "mathops.h"
36 #include "mpeg12.h"
37 #include "mpeg12data.h"
38 #include "mpeg12vlc.h"
39 
40 #define MAX_INDEX (64 - 1)
41 
42 /*
43  * 5 bits makes for very small tables, with no more than two lookups needed
44  * for the longest (10-bit) codes.
45  */
46 #define ALPHA_VLC_BITS 5
47 
48 typedef struct SHQContext {
53  int quant_matrix[64];
57 } SHQContext;
58 
59 
60 /* AC codes: Very similar but not identical to MPEG-2. */
61 static const uint16_t speedhq_vlc[123][2] = {
62  {0x0001, 2}, {0x0003, 3}, {0x000E, 4}, {0x0007, 5},
63  {0x0017, 5}, {0x0028, 6}, {0x0008, 6}, {0x006F, 7},
64  {0x001F, 7}, {0x00C4, 8}, {0x0044, 8}, {0x005F, 8},
65  {0x00DF, 8}, {0x007F, 8}, {0x00FF, 8}, {0x3E00, 14},
66  {0x1E00, 14}, {0x2E00, 14}, {0x0E00, 14}, {0x3600, 14},
67  {0x1600, 14}, {0x2600, 14}, {0x0600, 14}, {0x3A00, 14},
68  {0x1A00, 14}, {0x2A00, 14}, {0x0A00, 14}, {0x3200, 14},
69  {0x1200, 14}, {0x2200, 14}, {0x0200, 14}, {0x0C00, 15},
70  {0x7400, 15}, {0x3400, 15}, {0x5400, 15}, {0x1400, 15},
71  {0x6400, 15}, {0x2400, 15}, {0x4400, 15}, {0x0400, 15},
72  {0x0002, 3}, {0x000C, 5}, {0x004F, 7}, {0x00E4, 8},
73  {0x0004, 8}, {0x0D00, 13}, {0x1500, 13}, {0x7C00, 15},
74  {0x3C00, 15}, {0x5C00, 15}, {0x1C00, 15}, {0x6C00, 15},
75  {0x2C00, 15}, {0x4C00, 15}, {0xC800, 16}, {0x4800, 16},
76  {0x8800, 16}, {0x0800, 16}, {0x0300, 13}, {0x1D00, 13},
77  {0x0014, 5}, {0x0070, 7}, {0x003F, 8}, {0x00C0, 10},
78  {0x0500, 13}, {0x0180, 12}, {0x0280, 12}, {0x0C80, 12},
79  {0x0080, 12}, {0x0B00, 13}, {0x1300, 13}, {0x001C, 5},
80  {0x0064, 8}, {0x0380, 12}, {0x1900, 13}, {0x0D80, 12},
81  {0x0018, 6}, {0x00BF, 8}, {0x0480, 12}, {0x0B80, 12},
82  {0x0038, 6}, {0x0040, 9}, {0x0900, 13}, {0x0030, 7},
83  {0x0780, 12}, {0x2800, 16}, {0x0010, 7}, {0x0A80, 12},
84  {0x0050, 7}, {0x0880, 12}, {0x000F, 7}, {0x1100, 13},
85  {0x002F, 7}, {0x0100, 13}, {0x0084, 8}, {0x5800, 16},
86  {0x00A4, 8}, {0x9800, 16}, {0x0024, 8}, {0x1800, 16},
87  {0x0140, 9}, {0xE800, 16}, {0x01C0, 9}, {0x6800, 16},
88  {0x02C0, 10}, {0xA800, 16}, {0x0F80, 12}, {0x0580, 12},
89  {0x0980, 12}, {0x0E80, 12}, {0x0680, 12}, {0x1F00, 13},
90  {0x0F00, 13}, {0x1700, 13}, {0x0700, 13}, {0x1B00, 13},
91  {0xF800, 16}, {0x7800, 16}, {0xB800, 16}, {0x3800, 16},
92  {0xD800, 16},
93  {0x0020, 6}, /* escape */
94  {0x0006, 4} /* EOB */
95 };
96 
97 static const uint8_t speedhq_level[121] = {
98  1, 2, 3, 4, 5, 6, 7, 8,
99  9, 10, 11, 12, 13, 14, 15, 16,
100  17, 18, 19, 20, 21, 22, 23, 24,
101  25, 26, 27, 28, 29, 30, 31, 32,
102  33, 34, 35, 36, 37, 38, 39, 40,
103  1, 2, 3, 4, 5, 6, 7, 8,
104  9, 10, 11, 12, 13, 14, 15, 16,
105  17, 18, 19, 20, 1, 2, 3, 4,
106  5, 6, 7, 8, 9, 10, 11, 1,
107  2, 3, 4, 5, 1, 2, 3, 4,
108  1, 2, 3, 1, 2, 3, 1, 2,
109  1, 2, 1, 2, 1, 2, 1, 2,
110  1, 2, 1, 2, 1, 2, 1, 2,
111  1, 2, 1, 1, 1, 1, 1, 1,
112  1, 1, 1, 1, 1, 1, 1, 1,
113  1,
114 };
115 
116 static const uint8_t speedhq_run[121] = {
117  0, 0, 0, 0, 0, 0, 0, 0,
118  0, 0, 0, 0, 0, 0, 0, 0,
119  0, 0, 0, 0, 0, 0, 0, 0,
120  0, 0, 0, 0, 0, 0, 0, 0,
121  0, 0, 0, 0, 0, 0, 0, 0,
122  1, 1, 1, 1, 1, 1, 1, 1,
123  1, 1, 1, 1, 1, 1, 1, 1,
124  1, 1, 1, 1, 2, 2, 2, 2,
125  2, 2, 2, 2, 2, 2, 2, 3,
126  3, 3, 3, 3, 4, 4, 4, 4,
127  5, 5, 5, 6, 6, 6, 7, 7,
128  8, 8, 9, 9, 10, 10, 11, 11,
129  12, 12, 13, 13, 14, 14, 15, 15,
130  16, 16, 17, 18, 19, 20, 21, 22,
131  23, 24, 25, 26, 27, 28, 29, 30,
132  31,
133 };
134 
136  121,
137  121,
138  (const uint16_t (*)[])speedhq_vlc,
139  speedhq_run,
141 };
142 
143 /* NOTE: The first element is always 16, unscaled. */
144 static const uint8_t unscaled_quant_matrix[64] = {
145  16, 16, 19, 22, 26, 27, 29, 34,
146  16, 16, 22, 24, 27, 29, 34, 37,
147  19, 22, 26, 27, 29, 34, 34, 38,
148  22, 22, 26, 27, 29, 34, 37, 40,
149  22, 26, 27, 29, 32, 35, 40, 48,
150  26, 27, 29, 32, 35, 40, 48, 58,
151  26, 27, 29, 34, 38, 46, 56, 69,
152  27, 29, 35, 38, 46, 56, 69, 83
153 };
154 
156 
161 
162 static inline int decode_dc_le(GetBitContext *gb, int component)
163 {
164  int code, diff;
165 
166  if (component == 0 || component == 3) {
167  code = get_vlc2(gb, dc_lum_vlc_le.table, DC_VLC_BITS, 2);
168  } else {
169  code = get_vlc2(gb, dc_chroma_vlc_le.table, DC_VLC_BITS, 2);
170  }
171  if (!code) {
172  diff = 0;
173  } else {
174  diff = get_xbits_le(gb, code);
175  }
176  return diff;
177 }
178 
179 static inline int decode_alpha_block(const SHQContext *s, GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest, int linesize)
180 {
181  uint8_t block[128];
182  int i = 0, x, y;
183 
184  memset(block, 0, sizeof(block));
185 
186  {
187  OPEN_READER(re, gb);
188 
189  for ( ;; ) {
190  int run, level;
191 
192  UPDATE_CACHE_LE(re, gb);
193  GET_VLC(run, re, gb, dc_alpha_run_vlc_le.table, ALPHA_VLC_BITS, 2);
194 
195  if (run < 0) break;
196  i += run;
197  if (i >= 128)
198  return AVERROR_INVALIDDATA;
199 
200  UPDATE_CACHE_LE(re, gb);
201  GET_VLC(level, re, gb, dc_alpha_level_vlc_le.table, ALPHA_VLC_BITS, 2);
202  block[i++] = level;
203  }
204 
205  CLOSE_READER(re, gb);
206  }
207 
208  for (y = 0; y < 8; y++) {
209  for (x = 0; x < 16; x++) {
210  last_alpha[x] -= block[y * 16 + x];
211  }
212  memcpy(dest, last_alpha, 16);
213  dest += linesize;
214  }
215 
216  return 0;
217 }
218 
219 static inline int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
220 {
221  const int *quant_matrix = s->quant_matrix;
222  const uint8_t *scantable = s->intra_scantable.permutated;
223  LOCAL_ALIGNED_32(int16_t, block, [64]);
224  int dc_offset;
225 
226  s->bdsp.clear_block(block);
227 
228  dc_offset = decode_dc_le(gb, component);
229  last_dc[component] -= dc_offset; /* Note: Opposite of most codecs. */
230  block[scantable[0]] = last_dc[component]; /* quant_matrix[0] is always 16. */
231 
232  /* Read AC coefficients. */
233  {
234  int i = 0;
235  OPEN_READER(re, gb);
236  for ( ;; ) {
237  int level, run;
238  UPDATE_CACHE_LE(re, gb);
239  GET_RL_VLC(level, run, re, gb, ff_rl_speedhq.rl_vlc[0],
240  TEX_VLC_BITS, 2, 0);
241  if (level == 127) {
242  break;
243  } else if (level) {
244  i += run;
245  if (i > MAX_INDEX)
246  return AVERROR_INVALIDDATA;
247  /* If next bit is 1, level = -level */
248  level = (level ^ SHOW_SBITS(re, gb, 1)) -
249  SHOW_SBITS(re, gb, 1);
250  LAST_SKIP_BITS(re, gb, 1);
251  } else {
252  /* Escape. */
253 #if MIN_CACHE_BITS < 6 + 6 + 12
254 #error MIN_CACHE_BITS is too small for the escape code, add UPDATE_CACHE
255 #endif
256  run = SHOW_UBITS(re, gb, 6) + 1;
257  SKIP_BITS(re, gb, 6);
258  level = SHOW_UBITS(re, gb, 12) - 2048;
259  LAST_SKIP_BITS(re, gb, 12);
260 
261  i += run;
262  if (i > MAX_INDEX)
263  return AVERROR_INVALIDDATA;
264  }
265 
266  block[scantable[i]] = (level * quant_matrix[i]) >> 4;
267  }
268  CLOSE_READER(re, gb);
269  }
270 
271  s->idsp.idct_put(dest, linesize, block);
272 
273  return 0;
274 }
275 
276 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)
277 {
278  int ret, slice_number, slice_offsets[5];
279  int linesize_y = frame->linesize[0] * line_stride;
280  int linesize_cb = frame->linesize[1] * line_stride;
281  int linesize_cr = frame->linesize[2] * line_stride;
282  int linesize_a;
283 
284  if (s->alpha_type != SHQ_NO_ALPHA)
285  linesize_a = frame->linesize[3] * line_stride;
286 
287  if (end < start || end - start < 3 || end > buf_size)
288  return AVERROR_INVALIDDATA;
289 
290  slice_offsets[0] = start;
291  slice_offsets[4] = end;
292  for (slice_number = 1; slice_number < 4; slice_number++) {
293  uint32_t last_offset, slice_len;
294 
295  last_offset = slice_offsets[slice_number - 1];
296  slice_len = AV_RL24(buf + last_offset);
297  slice_offsets[slice_number] = last_offset + slice_len;
298 
299  if (slice_len < 3 || slice_offsets[slice_number] > end - 3)
300  return AVERROR_INVALIDDATA;
301  }
302 
303  for (slice_number = 0; slice_number < 4; slice_number++) {
304  GetBitContext gb;
305  uint32_t slice_begin, slice_end;
306  int x, y;
307 
308  slice_begin = slice_offsets[slice_number];
309  slice_end = slice_offsets[slice_number + 1];
310 
311  if ((ret = init_get_bits8(&gb, buf + slice_begin + 3, slice_end - slice_begin - 3)) < 0)
312  return ret;
313 
314  for (y = slice_number * 16 * line_stride; y < frame->height; y += line_stride * 64) {
315  uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
316  int last_dc[4] = { 1024, 1024, 1024, 1024 };
317  uint8_t last_alpha[16];
318 
319  memset(last_alpha, 255, sizeof(last_alpha));
320 
321  dest_y = frame->data[0] + frame->linesize[0] * (y + field_number);
322  if (s->subsampling == SHQ_SUBSAMPLING_420) {
323  dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number);
324  dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number);
325  } else {
326  dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number);
327  dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number);
328  }
329  if (s->alpha_type != SHQ_NO_ALPHA) {
330  dest_a = frame->data[3] + frame->linesize[3] * (y + field_number);
331  }
332 
333  for (x = 0; x < frame->width; x += 16) {
334  /* Decode the four luma blocks. */
335  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y, linesize_y)) < 0)
336  return ret;
337  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
338  return ret;
339  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
340  return ret;
341  if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
342  return ret;
343 
344  /*
345  * Decode the first chroma block. For 4:2:0, this is the only one;
346  * for 4:2:2, it's the top block; for 4:4:4, it's the top-left block.
347  */
348  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
349  return ret;
350  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
351  return ret;
352 
353  if (s->subsampling != SHQ_SUBSAMPLING_420) {
354  /* For 4:2:2, this is the bottom block; for 4:4:4, it's the bottom-left block. */
355  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
356  return ret;
357  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
358  return ret;
359 
360  if (s->subsampling == SHQ_SUBSAMPLING_444) {
361  /* Top-right and bottom-right blocks. */
362  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8, linesize_cb)) < 0)
363  return ret;
364  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8, linesize_cr)) < 0)
365  return ret;
366  if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb + 8, linesize_cb)) < 0)
367  return ret;
368  if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr + 8, linesize_cr)) < 0)
369  return ret;
370 
371  dest_cb += 8;
372  dest_cr += 8;
373  }
374  }
375  dest_y += 16;
376  dest_cb += 8;
377  dest_cr += 8;
378 
379  if (s->alpha_type == SHQ_RLE_ALPHA) {
380  /* Alpha coded using 16x8 RLE blocks. */
381  if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a, linesize_a)) < 0)
382  return ret;
383  if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
384  return ret;
385  dest_a += 16;
386  } else if (s->alpha_type == SHQ_DCT_ALPHA) {
387  /* Alpha encoded exactly like luma. */
388  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a, linesize_a)) < 0)
389  return ret;
390  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
391  return ret;
392  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
393  return ret;
394  if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
395  return ret;
396  dest_a += 16;
397  }
398  }
399  }
400  }
401 
402  return 0;
403 }
404 
405 static void compute_quant_matrix(int *output, int qscale)
406 {
407  int i;
408  for (i = 0; i < 64; i++) output[i] = unscaled_quant_matrix[ff_zigzag_direct[i]] * qscale;
409 }
410 
412  void *data, int *got_frame,
413  AVPacket *avpkt)
414 {
415  SHQContext * const s = avctx->priv_data;
416  const uint8_t *buf = avpkt->data;
417  int buf_size = avpkt->size;
418  AVFrame *frame = data;
420  uint32_t second_field_offset;
421  int ret;
422 
423  if (buf_size < 4)
424  return AVERROR_INVALIDDATA;
425 
426  quality = buf[0];
427  if (quality >= 100) {
428  return AVERROR_INVALIDDATA;
429  }
430 
431  compute_quant_matrix(s->quant_matrix, 100 - quality);
432 
433  second_field_offset = AV_RL24(buf + 1);
434  if (second_field_offset >= buf_size - 3) {
435  return AVERROR_INVALIDDATA;
436  }
437 
438  avctx->coded_width = FFALIGN(avctx->width, 16);
439  avctx->coded_height = FFALIGN(avctx->height, 16);
440 
441  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
442  return ret;
443  }
444  frame->key_frame = 1;
445 
446  if (second_field_offset == 4 || second_field_offset == (buf_size-4)) {
447  /*
448  * Overlapping first and second fields is used to signal
449  * encoding only a single field. In this case, "height"
450  * is ambiguous; it could mean either the height of the
451  * frame as a whole, or of the field. The former would make
452  * more sense for compatibility with legacy decoders,
453  * but this matches the convention used in NDI, which is
454  * the primary user of this trick.
455  */
456  if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, buf_size, 1)) < 0)
457  return ret;
458  } else {
459  if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, second_field_offset, 2)) < 0)
460  return ret;
461  if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 1, second_field_offset, buf_size, 2)) < 0)
462  return ret;
463  }
464 
465  *got_frame = 1;
466  return buf_size;
467 }
468 
469 /*
470  * Alpha VLC. Run and level are independently coded, and would be
471  * outside the default limits for MAX_RUN/MAX_LEVEL, so we don't
472  * bother with combining them into one table.
473  */
474 static av_cold void compute_alpha_vlcs(void)
475 {
476  uint16_t run_code[134], level_code[266];
477  uint8_t run_bits[134], level_bits[266];
478  int16_t run_symbols[134], level_symbols[266];
479  int entry, i, sign;
480 
481  /* Initialize VLC for alpha run. */
482  entry = 0;
483 
484  /* 0 -> 0. */
485  run_code[entry] = 0;
486  run_bits[entry] = 1;
487  run_symbols[entry] = 0;
488  ++entry;
489 
490  /* 10xx -> xx plus 1. */
491  for (i = 0; i < 4; ++i) {
492  run_code[entry] = (i << 2) | 1;
493  run_bits[entry] = 4;
494  run_symbols[entry] = i + 1;
495  ++entry;
496  }
497 
498  /* 111xxxxxxx -> xxxxxxx. */
499  for (i = 0; i < 128; ++i) {
500  run_code[entry] = (i << 3) | 7;
501  run_bits[entry] = 10;
502  run_symbols[entry] = i;
503  ++entry;
504  }
505 
506  /* 110 -> EOB. */
507  run_code[entry] = 3;
508  run_bits[entry] = 3;
509  run_symbols[entry] = -1;
510  ++entry;
511 
512  av_assert0(entry == FF_ARRAY_ELEMS(run_code));
513 
514  INIT_LE_VLC_SPARSE_STATIC(&dc_alpha_run_vlc_le, ALPHA_VLC_BITS,
515  FF_ARRAY_ELEMS(run_code),
516  run_bits, 1, 1,
517  run_code, 2, 2,
518  run_symbols, 2, 2, 160);
519 
520  /* Initialize VLC for alpha level. */
521  entry = 0;
522 
523  for (sign = 0; sign <= 1; ++sign) {
524  /* 1s -> -1 or +1 (depending on sign bit). */
525  level_code[entry] = (sign << 1) | 1;
526  level_bits[entry] = 2;
527  level_symbols[entry] = sign ? -1 : 1;
528  ++entry;
529 
530  /* 01sxx -> xx plus 2 (2..5 or -2..-5, depending on sign bit). */
531  for (i = 0; i < 4; ++i) {
532  level_code[entry] = (i << 3) | (sign << 2) | 2;
533  level_bits[entry] = 5;
534  level_symbols[entry] = sign ? -(i + 2) : (i + 2);
535  ++entry;
536  }
537  }
538 
539  /*
540  * 00xxxxxxxx -> xxxxxxxx, in two's complement. There are many codes
541  * here that would better be encoded in other ways (e.g. 0 would be
542  * encoded by increasing run, and +/- 1 would be encoded with a
543  * shorter code), but it doesn't hurt to allow everything.
544  */
545  for (i = 0; i < 256; ++i) {
546  level_code[entry] = i << 2;
547  level_bits[entry] = 10;
548  level_symbols[entry] = i;
549  ++entry;
550  }
551 
552  av_assert0(entry == FF_ARRAY_ELEMS(level_code));
553 
554  INIT_LE_VLC_SPARSE_STATIC(&dc_alpha_level_vlc_le, ALPHA_VLC_BITS,
555  FF_ARRAY_ELEMS(level_code),
556  level_bits, 1, 1,
557  level_code, 2, 2,
558  level_symbols, 2, 2, 288);
559 }
560 
561 static av_cold void speedhq_static_init(void)
562 {
563  /* Exactly the same as MPEG-2, except for a little-endian reader. */
564  INIT_CUSTOM_VLC_STATIC(&dc_lum_vlc_le, DC_VLC_BITS, 12,
567  INIT_VLC_OUTPUT_LE, 512);
568  INIT_CUSTOM_VLC_STATIC(&dc_chroma_vlc_le, DC_VLC_BITS, 12,
571  INIT_VLC_OUTPUT_LE, 514);
572 
573  ff_rl_init(&ff_rl_speedhq, speedhq_static_rl_table_store);
574  INIT_2D_VLC_RL(ff_rl_speedhq, 674, INIT_VLC_LE);
575 
577 }
578 
580 {
581  int ret;
582  static AVOnce init_once = AV_ONCE_INIT;
583  SHQContext * const s = avctx->priv_data;
584 
585  s->avctx = avctx;
586 
587  ret = ff_thread_once(&init_once, speedhq_static_init);
588  if (ret)
589  return AVERROR_UNKNOWN;
590 
591  ff_blockdsp_init(&s->bdsp, avctx);
592  ff_idctdsp_init(&s->idsp, avctx);
594 
595  switch (avctx->codec_tag) {
596  case MKTAG('S', 'H', 'Q', '0'):
599  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
600  break;
601  case MKTAG('S', 'H', 'Q', '1'):
604  avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
605  break;
606  case MKTAG('S', 'H', 'Q', '2'):
609  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
610  break;
611  case MKTAG('S', 'H', 'Q', '3'):
614  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
615  break;
616  case MKTAG('S', 'H', 'Q', '4'):
619  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
620  break;
621  case MKTAG('S', 'H', 'Q', '5'):
624  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
625  break;
626  case MKTAG('S', 'H', 'Q', '7'):
629  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
630  break;
631  case MKTAG('S', 'H', 'Q', '9'):
634  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
635  break;
636  default:
637  av_log(avctx, AV_LOG_ERROR, "Unknown NewTek SpeedHQ FOURCC provided (%08X)\n",
638  avctx->codec_tag);
639  return AVERROR_INVALIDDATA;
640  }
641 
642  /* This matches what NDI's RGB -> Y'CbCr 4:2:2 converter uses. */
643  avctx->colorspace = AVCOL_SPC_BT470BG;
645 
646  return 0;
647 }
648 
650  .name = "speedhq",
651  .long_name = NULL_IF_CONFIG_SMALL("NewTek SpeedHQ"),
652  .type = AVMEDIA_TYPE_VIDEO,
653  .id = AV_CODEC_ID_SPEEDHQ,
654  .priv_data_size = sizeof(SHQContext),
657  .capabilities = AV_CODEC_CAP_DR1,
658 };
static int get_xbits_le(GetBitContext *s, int n)
Definition: get_bits.h:344
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
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: speedhq.c:276
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:719
float re
Definition: fft.c:82
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
#define MAX_INDEX
Definition: speedhq.c:40
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
Definition: speedhq.c:219
static const uint8_t unscaled_quant_matrix[64]
Definition: speedhq.c:144
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:518
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:36
static VLC dc_chroma_vlc_le
Definition: speedhq.c:158
static int speedhq_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: speedhq.c:411
Scantable.
Definition: idctdsp.h:31
MPEG-1/2 VLC.
const unsigned char ff_mpeg12_vlc_dc_lum_bits[12]
Definition: mpeg12data.c:55
int size
Definition: packet.h:364
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:60
IDCTDSPContext idsp
Definition: speedhq.c:51
int quant_matrix[64]
Definition: speedhq.c:53
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:741
#define INIT_LE_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size)
Definition: vlc.h:76
uint8_t permutated[64]
Definition: idctdsp.h:33
const uint16_t ff_mpeg12_vlc_dc_lum_code[12]
Definition: mpeg12data.c:52
uint8_t run
Definition: svq3.c:204
AVCodec.
Definition: codec.h:190
RLTable.
Definition: rl.h:39
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static int decode_alpha_block(const SHQContext *s, GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest, int linesize)
Definition: speedhq.c:179
Macro definitions for various function/variable attributes.
static int decode_dc_le(GetBitContext *gb, int component)
Definition: speedhq.c:162
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
The exact code depends on how similar the blocks are and how related they are to the block
uint8_t
#define av_cold
Definition: attributes.h:88
#define INIT_2D_VLC_RL(rl, static_size, flags)
Definition: mpeg12.h:32
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
enum SHQContext::@138 subsampling
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
static const uint8_t run_bits[7][16]
Definition: h264_cavlc.c:229
uint8_t * data
Definition: packet.h:363
bitstream reader API header.
#define AVOnce
Definition: thread.h:172
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1173
#define FFALIGN(x, a)
Definition: macros.h:48
#define MAX_LEVEL
Definition: rl.h:36
#define av_log(a,...)
static const uint16_t speedhq_vlc[123][2]
Definition: speedhq.c:61
static VLC dc_alpha_run_vlc_le
Definition: speedhq.c:159
static av_cold void compute_alpha_vlcs(void)
Definition: speedhq.c:474
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
int width
Definition: frame.h:372
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
const uint16_t ff_mpeg12_vlc_dc_chroma_code[12]
Definition: mpeg12data.c:59
static const uint8_t speedhq_run[121]
Definition: speedhq.c:116
const char * name
Name of the codec implementation.
Definition: codec.h:197
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:91
static VLC dc_lum_vlc_le
Definition: speedhq.c:157
#define CLOSE_READER(name, gb)
Definition: get_bits.h:149
#define GET_RL_VLC(level, run, name, gb, table, bits,max_depth, need_update)
Definition: get_bits.h:738
Definition: vlc.h:26
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:193
static void compute_quant_matrix(int *output, int qscale)
Definition: speedhq.c:405
int width
picture width / height.
Definition: avcodec.h:704
const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12]
Definition: mpeg12data.c:62
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
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
#define s(width, name)
Definition: cbs_vp9.c:257
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:199
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:797
#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:706
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:96
void(* idct_put)(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
block -> idct -> clip to unsigned 8 bit -> dest.
Definition: idctdsp.h:72
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:211
#define FF_ARRAY_ELEMS(a)
#define INIT_VLC_LE
Definition: vlc.h:59
#define AV_ONCE_INIT
Definition: thread.h:173
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:345
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
AVCodec ff_speedhq_decoder
Definition: speedhq.c:649
main external API structure.
Definition: avcodec.h:531
MPEG-1/2 tables.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:556
static const uint8_t speedhq_level[121]
Definition: speedhq.c:97
#define OPEN_READER(name, gb)
Definition: get_bits.h:138
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1879
av_cold int ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: rl.c:39
static av_cold int speedhq_decode_init(AVCodecContext *avctx)
Definition: speedhq.c:579
BlockDSPContext bdsp
Definition: speedhq.c:50
static uint8_t speedhq_static_rl_table_store[2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: speedhq.c:155
int coded_height
Definition: avcodec.h:719
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1159
#define UPDATE_CACHE_LE(name, gb)
Definition: get_bits.h:161
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
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
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
uint8_t level
Definition: svq3.c:205
#define INIT_CUSTOM_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, flags, static_size)
Definition: vlc.h:80
#define DC_VLC_BITS
Definition: intrax8.c:38
#define LOCAL_ALIGNED_32(t, v,...)
Definition: internal.h:137
#define ALPHA_VLC_BITS
Definition: speedhq.c:46
#define MAX_RUN
Definition: rl.h:35
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:212
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
common internal api header.
ScanTable intra_scantable
Definition: speedhq.c:52
#define TEX_VLC_BITS
Definition: dv.h:99
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2035
void * priv_data
Definition: avcodec.h:558
static av_always_inline int diff(const uint32_t a, const uint32_t b)
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:29
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:238
enum SHQContext::@139 alpha_type
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:392
AVCodecContext * avctx
Definition: speedhq.c:49
int height
Definition: frame.h:372
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:608
static av_cold void speedhq_static_init(void)
Definition: speedhq.c:561
static VLC dc_alpha_level_vlc_le
Definition: speedhq.c:160
#define MKTAG(a, b, c, d)
Definition: common.h:405
This structure stores compressed data.
Definition: packet.h:340
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
int i
Definition: input.c:407
#define INIT_VLC_OUTPUT_LE
Definition: vlc.h:58
RLTable ff_rl_speedhq
Definition: speedhq.c:135