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