FFmpeg
Loading...
Searching...
No Matches
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
31
32#include "avcodec.h"
33#include "blockdsp.h"
34#include "codec_internal.h"
35#include "decode.h"
36#include "get_bits.h"
37#include "idctdsp.h"
38#include "libavutil/thread.h"
39#include "mathops.h"
40#include "mpeg12data.h"
41#include "mpeg12vlc.h"
42#include "speedhq.h"
43#include "thread.h"
44
45#define MAX_INDEX (64 - 1)
46
47/*
48 * 5 bits makes for very small tables, with no more than two lookups needed
49 * for the longest (10-bit) codes.
50 */
51#define ALPHA_VLC_BITS 5
52
64
65/* NOTE: The first element is always 16, unscaled. */
66static const uint8_t unscaled_quant_matrix[64] = {
67 16, 16, 19, 22, 26, 27, 29, 34,
68 16, 16, 22, 24, 27, 29, 34, 37,
69 19, 22, 26, 27, 29, 34, 34, 38,
70 22, 22, 26, 27, 29, 34, 37, 40,
71 22, 26, 27, 29, 32, 35, 40, 48,
72 26, 27, 29, 32, 35, 40, 48, 58,
73 26, 27, 29, 34, 38, 46, 56, 69,
74 27, 29, 35, 38, 46, 56, 69, 83
75};
76
81
83
84static inline int decode_dc_le(GetBitContext *gb, int component)
85{
86 int code, diff;
87
88 if (component == 0 || component == 3) {
90 } else {
92 }
93 if (!code) {
94 diff = 0;
95 } else {
96 diff = get_xbits_le(gb, code);
97 }
98 return diff;
99}
100
101static inline int decode_alpha_block(const SHQContext *s, GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest, int linesize)
102{
103 uint8_t block[128];
104 int i = 0, x, y;
105
106 memset(block, 0, sizeof(block));
107
108 {
109 OPEN_READER(re, gb);
110
111 for ( ;; ) {
112 int run, level;
113
114 UPDATE_CACHE_LE(re, gb);
116
117 if (run < 0) break;
118 i += run;
119 if (i >= 128)
120 return AVERROR_INVALIDDATA;
121
122 UPDATE_CACHE_LE(re, gb);
124 block[i++] = level;
125 }
126
127 CLOSE_READER(re, gb);
128 }
129
130 for (y = 0; y < 8; y++) {
131 for (x = 0; x < 16; x++) {
132 last_alpha[x] -= block[y * 16 + x];
133 }
134 memcpy(dest, last_alpha, 16);
135 dest += linesize;
136 }
137
138 return 0;
139}
140
141static inline int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
142{
143 const int *quant_matrix = s->quant_matrix;
144 const uint8_t *scantable = s->permutated_intra_scantable;
145 LOCAL_ALIGNED_32(int16_t, block, [64]);
146 int dc_offset;
147
148 s->bdsp.clear_block(block);
149
150 dc_offset = decode_dc_le(gb, component);
151 last_dc[component] -= dc_offset; /* Note: Opposite of most codecs. */
152 block[scantable[0]] = last_dc[component]; /* quant_matrix[0] is always 16. */
153
154 /* Read AC coefficients. */
155 {
156 int i = 0;
157 OPEN_READER(re, gb);
158 for ( ;; ) {
159 int level, run;
160 UPDATE_CACHE_LE(re, gb);
162 TEX_VLC_BITS, 2, 0);
163 if (level == 127) {
164 break;
165 } else if (level) {
166 i += run;
167 if (i > MAX_INDEX)
168 return AVERROR_INVALIDDATA;
169 /* If next bit is 1, level = -level */
170 level = (level ^ SHOW_SBITS(re, gb, 1)) -
171 SHOW_SBITS(re, gb, 1);
172 LAST_SKIP_BITS(re, gb, 1);
173 } else {
174 /* Escape. */
175#if MIN_CACHE_BITS < 6 + 6 + 12
176#error MIN_CACHE_BITS is too small for the escape code, add UPDATE_CACHE
177#endif
178 run = SHOW_UBITS(re, gb, 6) + 1;
179 SKIP_BITS(re, gb, 6);
180 level = SHOW_UBITS(re, gb, 12) - 2048;
181 LAST_SKIP_BITS(re, gb, 12);
182
183 i += run;
184 if (i > MAX_INDEX)
185 return AVERROR_INVALIDDATA;
186 }
187
188 block[scantable[i]] = (level * quant_matrix[i]) >> 4;
189 }
190 CLOSE_READER(re, gb);
191 }
192
193 s->idsp.idct_put(dest, linesize, block);
194
195 return 0;
196}
197
198static int decode_speedhq_border(const SHQContext *s, GetBitContext *gb, AVFrame *frame, int field_number, int line_stride)
199{
200 int linesize_y = frame->linesize[0] * line_stride;
201 int linesize_cb = frame->linesize[1] * line_stride;
202 int linesize_cr = frame->linesize[2] * line_stride;
203 int linesize_a;
204 int ret;
205
206 if (s->alpha_type != SHQ_NO_ALPHA)
207 linesize_a = frame->linesize[3] * line_stride;
208
209 for (int y = 0; y < frame->height; y += 16 * line_stride) {
210 int last_dc[4] = { 1024, 1024, 1024, 1024 };
211 uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
212 uint8_t last_alpha[16];
213 int x = frame->width - 8;
214
215 dest_y = frame->data[0] + frame->linesize[0] * (y + field_number) + x;
216 if (s->subsampling == SHQ_SUBSAMPLING_420) {
217 dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number) + x / 2;
218 dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number) + x / 2;
219 } else {
220 av_assert2(s->subsampling == SHQ_SUBSAMPLING_422);
221 dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number) + x / 2;
222 dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number) + x / 2;
223 }
224 if (s->alpha_type != SHQ_NO_ALPHA) {
225 memset(last_alpha, 255, sizeof(last_alpha));
226 dest_a = frame->data[3] + frame->linesize[3] * (y + field_number) + x;
227 }
228
229 if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y, linesize_y)) < 0)
230 return ret;
231 if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
232 return ret;
233 if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
234 return ret;
235 if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
236 return ret;
237 if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
238 return ret;
239 if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
240 return ret;
241
242 if (s->subsampling != SHQ_SUBSAMPLING_420) {
243 if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
244 return ret;
245 if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
246 return ret;
247 }
248
249 if (s->alpha_type == SHQ_RLE_ALPHA) {
250 /* Alpha coded using 16x8 RLE blocks. */
251 if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a, linesize_a)) < 0)
252 return ret;
253 if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
254 return ret;
255 } else if (s->alpha_type == SHQ_DCT_ALPHA) {
256 /* Alpha encoded exactly like luma. */
257 if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a, linesize_a)) < 0)
258 return ret;
259 if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
260 return ret;
261 if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
262 return ret;
263 if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
264 return ret;
265 }
266 }
267
268 return 0;
269}
270
271static 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, int slice_number)
272{
273 int ret, x, y, slice_offsets[5];
274 uint32_t slice_begin, slice_end;
275 int linesize_y = frame->linesize[0] * line_stride;
276 int linesize_cb = frame->linesize[1] * line_stride;
277 int linesize_cr = frame->linesize[2] * line_stride;
278 int linesize_a;
279 GetBitContext gb;
280
281 if (s->alpha_type != SHQ_NO_ALPHA)
282 linesize_a = frame->linesize[3] * line_stride;
283
284 if (end < start || end - start < 3 || end > buf_size)
285 return AVERROR_INVALIDDATA;
286
287 slice_offsets[0] = start;
288 slice_offsets[4] = end;
289 for (x = 1; x < 4; x++) {
290 uint32_t last_offset, slice_len;
291
292 last_offset = slice_offsets[x - 1];
293 slice_len = AV_RL24(buf + last_offset);
294 slice_offsets[x] = last_offset + slice_len;
295
296 if (slice_len < 3 || slice_offsets[x] > end - 3)
297 return AVERROR_INVALIDDATA;
298 }
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 if (s->subsampling != SHQ_SUBSAMPLING_444 && (frame->width & 15) && slice_number == 3)
394 return decode_speedhq_border(s, &gb, frame, field_number, line_stride);
395
396 return 0;
397}
398
399static int decode_slice_progressive(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
400{
401 SHQContext *s = avctx->priv_data;
402 (void)threadnr;
403
404 return decode_speedhq_field(avctx->priv_data, s->avpkt->data, s->avpkt->size, arg, 0, 4, s->avpkt->size, 1, jobnr);
405}
406
407static int decode_slice_interlaced(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
408{
409 SHQContext *s = avctx->priv_data;
410 int field_number = jobnr / 4;
411 int slice_number = jobnr % 4;
412 (void)threadnr;
413
414 if (field_number == 0)
415 return decode_speedhq_field(avctx->priv_data, s->avpkt->data, s->avpkt->size, arg, 0, 4, s->second_field_offset, 2, slice_number);
416 else
417 return decode_speedhq_field(avctx->priv_data, s->avpkt->data, s->avpkt->size, arg, 1, s->second_field_offset, s->avpkt->size, 2, slice_number);
418}
419
420static void compute_quant_matrix(int *output, int qscale)
421{
422 int i;
423 for (i = 0; i < 64; i++) output[i] = unscaled_quant_matrix[ff_zigzag_direct[i]] * qscale;
424}
425
427 int *got_frame, AVPacket *avpkt)
428{
429 SHQContext * const s = avctx->priv_data;
430 const uint8_t *buf = avpkt->data;
431 int buf_size = avpkt->size;
432 uint8_t quality;
433 int ret;
434
435 if (buf_size < 4 || avctx->width < 8 || avctx->width % 8 != 0)
436 return AVERROR_INVALIDDATA;
437 if (buf_size < avctx->width*avctx->height / 64 / 4)
438 return AVERROR_INVALIDDATA;
439
440 quality = buf[0];
441 if (quality >= 100) {
442 return AVERROR_INVALIDDATA;
443 }
444
445 if (avctx->skip_frame >= AVDISCARD_ALL)
446 return avpkt->size;
447
448 compute_quant_matrix(s->quant_matrix, 100 - quality);
449
450 s->second_field_offset = AV_RL24(buf + 1);
451 if (s->second_field_offset >= buf_size - 3) {
452 return AVERROR_INVALIDDATA;
453 }
454
455 avctx->coded_width = FFALIGN(avctx->width, 16);
456 avctx->coded_height = FFALIGN(avctx->height, 16);
457
458 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0) {
459 return ret;
460 }
461
462 s->avpkt = avpkt;
463
464 if (s->second_field_offset == 4 || s->second_field_offset == (buf_size-4)) {
465 /*
466 * Overlapping first and second fields is used to signal
467 * encoding only a single field. In this case, "height"
468 * is ambiguous; it could mean either the height of the
469 * frame as a whole, or of the field. The former would make
470 * more sense for compatibility with legacy decoders,
471 * but this matches the convention used in NDI, which is
472 * the primary user of this trick.
473 */
474 if ((ret = avctx->execute2(avctx, decode_slice_progressive, frame, NULL, 4)) < 0)
475 return ret;
476 } else {
477 if ((ret = avctx->execute2(avctx, decode_slice_interlaced, frame, NULL, 8)) < 0)
478 return ret;
479 }
480
481 *got_frame = 1;
482 return buf_size;
483}
484
485/*
486 * Alpha VLC. Run and level are independently coded, and would be
487 * outside the default limits for MAX_RUN/MAX_LEVEL, so we don't
488 * bother with combining them into one table.
489 */
491{
492 uint16_t run_code[134], level_code[266];
493 uint8_t run_bits[134], level_bits[266];
494 int16_t run_symbols[134], level_symbols[266];
495 int entry, i, sign;
496
497 /* Initialize VLC for alpha run. */
498 entry = 0;
499
500 /* 0 -> 0. */
501 run_code[entry] = 0;
502 run_bits[entry] = 1;
503 run_symbols[entry] = 0;
504 ++entry;
505
506 /* 10xx -> xx plus 1. */
507 for (i = 0; i < 4; ++i) {
508 run_code[entry] = (i << 2) | 1;
509 run_bits[entry] = 4;
510 run_symbols[entry] = i + 1;
511 ++entry;
512 }
513
514 /* 111xxxxxxx -> xxxxxxx. */
515 for (i = 0; i < 128; ++i) {
516 run_code[entry] = (i << 3) | 7;
517 run_bits[entry] = 10;
518 run_symbols[entry] = i;
519 ++entry;
520 }
521
522 /* 110 -> EOB. */
523 run_code[entry] = 3;
524 run_bits[entry] = 3;
525 run_symbols[entry] = -1;
526 ++entry;
527
528 av_assert0(entry == FF_ARRAY_ELEMS(run_code));
529
531 FF_ARRAY_ELEMS(run_code),
532 run_bits, 1, 1,
533 run_code, 2, 2,
534 run_symbols, 2, 2, VLC_INIT_LE);
535
536 /* Initialize VLC for alpha level. */
537 entry = 0;
538
539 for (sign = 0; sign <= 1; ++sign) {
540 /* 1s -> -1 or +1 (depending on sign bit). */
541 level_code[entry] = (sign << 1) | 1;
542 level_bits[entry] = 2;
543 level_symbols[entry] = sign ? -1 : 1;
544 ++entry;
545
546 /* 01sxx -> xx plus 2 (2..5 or -2..-5, depending on sign bit). */
547 for (i = 0; i < 4; ++i) {
548 level_code[entry] = (i << 3) | (sign << 2) | 2;
549 level_bits[entry] = 5;
550 level_symbols[entry] = sign ? -(i + 2) : (i + 2);
551 ++entry;
552 }
553 }
554
555 /*
556 * 00xxxxxxxx -> xxxxxxxx, in two's complement. There are many codes
557 * here that would better be encoded in other ways (e.g. 0 would be
558 * encoded by increasing run, and +/- 1 would be encoded with a
559 * shorter code), but it doesn't hurt to allow everything.
560 */
561 for (i = 0; i < 256; ++i) {
562 level_code[entry] = i << 2;
563 level_bits[entry] = 10;
564 level_symbols[entry] = i;
565 ++entry;
566 }
567
568 av_assert0(entry == FF_ARRAY_ELEMS(level_code));
569
571 FF_ARRAY_ELEMS(level_code),
572 level_bits, 1, 1,
573 level_code, 2, 2,
574 level_symbols, 2, 2, VLC_INIT_LE);
575}
576
595
597{
598 int ret;
599 static AVOnce init_once = AV_ONCE_INIT;
600 SHQContext * const s = avctx->priv_data;
601
602 ret = ff_thread_once(&init_once, speedhq_static_init);
603 if (ret)
604 return AVERROR_UNKNOWN;
605
606 ff_blockdsp_init(&s->bdsp);
607 ff_idctdsp_init(&s->idsp, avctx);
608 ff_permute_scantable(s->permutated_intra_scantable, ff_zigzag_direct,
609 s->idsp.idct_permutation);
610
611 switch (avctx->codec_tag) {
612 case MKTAG('S', 'H', 'Q', '0'):
613 s->subsampling = SHQ_SUBSAMPLING_420;
614 s->alpha_type = SHQ_NO_ALPHA;
616 break;
617 case MKTAG('S', 'H', 'Q', '1'):
618 s->subsampling = SHQ_SUBSAMPLING_420;
619 s->alpha_type = SHQ_RLE_ALPHA;
621 break;
622 case MKTAG('S', 'H', 'Q', '2'):
623 s->subsampling = SHQ_SUBSAMPLING_422;
624 s->alpha_type = SHQ_NO_ALPHA;
626 break;
627 case MKTAG('S', 'H', 'Q', '3'):
628 s->subsampling = SHQ_SUBSAMPLING_422;
629 s->alpha_type = SHQ_RLE_ALPHA;
631 break;
632 case MKTAG('S', 'H', 'Q', '4'):
633 s->subsampling = SHQ_SUBSAMPLING_444;
634 s->alpha_type = SHQ_NO_ALPHA;
636 break;
637 case MKTAG('S', 'H', 'Q', '5'):
638 s->subsampling = SHQ_SUBSAMPLING_444;
639 s->alpha_type = SHQ_RLE_ALPHA;
641 break;
642 case MKTAG('S', 'H', 'Q', '7'):
643 s->subsampling = SHQ_SUBSAMPLING_422;
644 s->alpha_type = SHQ_DCT_ALPHA;
646 break;
647 case MKTAG('S', 'H', 'Q', '9'):
648 s->subsampling = SHQ_SUBSAMPLING_444;
649 s->alpha_type = SHQ_DCT_ALPHA;
651 break;
652 default:
653 av_log(avctx, AV_LOG_ERROR, "Unknown NewTek SpeedHQ FOURCC provided (%08X)\n",
654 avctx->codec_tag);
655 return AVERROR_INVALIDDATA;
656 }
657
658 /* This matches what NDI's RGB -> Y'CbCr 4:2:2 converter uses. */
661
662 return 0;
663}
664
666 .p.name = "speedhq",
667 CODEC_LONG_NAME("NewTek SpeedHQ"),
668 .p.type = AVMEDIA_TYPE_VIDEO,
669 .p.id = AV_CODEC_ID_SPEEDHQ,
670 .priv_data_size = sizeof(SHQContext),
674};
const FFCodec ff_speedhq_decoder
Definition speedhqdec.c:665
#define entry
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define NULL
Definition coverity.c:32
static int16_t block[64]
Definition dct.c:125
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define TEX_VLC_BITS
Definition dvdec.c:146
bitstream reader API header.
#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:573
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:645
#define CLOSE_READER(name, gb)
Definition get_bits.h:189
static int get_xbits_le(GetBitContext *s, int n)
Definition get_bits.h:308
#define SHOW_UBITS(name, gb, num)
Definition get_bits.h:247
#define SHOW_SBITS(name, gb, num)
Definition get_bits.h:248
#define OPEN_READER(name, gb)
Definition get_bits.h:182
#define SKIP_BITS(name, gb, num)
Definition get_bits.h:229
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition get_bits.h:602
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
#define LAST_SKIP_BITS(name, gb, num)
Definition get_bits.h:235
#define UPDATE_CACHE_LE(name, gb)
Definition get_bits.h:201
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_SPEEDHQ
Definition codec_id.h:270
@ AVDISCARD_ALL
discard all
Definition defs.h:232
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static const uint8_t run_bits[7][16]
Definition h264_cavlc.c:227
#define DC_VLC_BITS
Definition intrax8.c:41
#define AV_RL24(x)
av_cold void ff_blockdsp_init(BlockDSPContext *c)
Definition blockdsp.c:58
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition idctdsp.c:228
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Definition idctdsp.c:30
const char * arg
Definition jacosubdec.c:65
Multithreading API for decoders.
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFALIGN(x, a)
Definition macros.h:78
const uint8_t ff_zigzag_direct[64]
Definition mathtables.c:137
#define LOCAL_ALIGNED_32(t, v,...)
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:86
#define MAX_INDEX
Definition mpeg12.c:181
const uint16_t ff_mpeg12_vlc_dc_chroma_code[12]
Definition mpeg12data.c:60
const uint16_t ff_mpeg12_vlc_dc_lum_code[12]
Definition mpeg12data.c:53
const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12]
Definition mpeg12data.c:63
const unsigned char ff_mpeg12_vlc_dc_lum_bits[12]
Definition mpeg12data.c:56
MPEG-1/2 tables.
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
MPEG-1/2 VLC.
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition pixfmt.h:805
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ 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:712
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
#define FF_ARRAY_ELEMS(a)
const uint8_t * code
Definition spdifenc.c:433
const uint8_t ff_speedhq_level[121]
Definition speedhq.c:62
const uint16_t ff_speedhq_vlc_table[SPEEDHQ_RL_NB_ELEMS+2][2]
Definition speedhq.c:26
const uint8_t ff_speedhq_run[121]
Definition speedhq.c:81
#define SPEEDHQ_RL_NB_ELEMS
Definition speedhq.h:27
static const uint8_t unscaled_quant_matrix[64]
Definition speedhqdec.c:66
static av_cold int speedhq_decode_init(AVCodecContext *avctx)
Definition speedhqdec.c:596
static int speedhq_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition speedhqdec.c:426
static RL_VLC_ELEM speedhq_rl_vlc[674]
Definition speedhqdec.c:82
static VLCElem dc_alpha_run_vlc_le[160]
Definition speedhqdec.c:79
static av_cold void compute_alpha_vlcs(void)
Definition speedhqdec.c:490
static int decode_speedhq_border(const SHQContext *s, GetBitContext *gb, AVFrame *frame, int field_number, int line_stride)
Definition speedhqdec.c:198
static av_cold void speedhq_static_init(void)
Definition speedhqdec.c:577
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, int slice_number)
Definition speedhqdec.c:271
static int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
Definition speedhqdec.c:141
static VLCElem dc_alpha_level_vlc_le[288]
Definition speedhqdec.c:80
static VLCElem dc_lum_vlc_le[512]
Definition speedhqdec.c:77
#define ALPHA_VLC_BITS
Definition speedhqdec.c:51
static int decode_slice_interlaced(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition speedhqdec.c:407
static void compute_quant_matrix(int *output, int qscale)
Definition speedhqdec.c:420
static int decode_slice_progressive(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition speedhqdec.c:399
static int decode_alpha_block(const SHQContext *s, GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest, int linesize)
Definition speedhqdec.c:101
static VLCElem dc_chroma_vlc_le[514]
Definition speedhqdec.c:78
static int decode_dc_le(GetBitContext *gb, int component)
Definition speedhqdec.c:84
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition avcodec.h:688
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition avcodec.h:1628
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:619
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1667
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
AVPacket * avpkt
Definition speedhqdec.c:61
BlockDSPContext bdsp
Definition speedhqdec.c:54
@ SHQ_SUBSAMPLING_420
Definition speedhqdec.c:58
@ SHQ_SUBSAMPLING_444
Definition speedhqdec.c:58
@ SHQ_SUBSAMPLING_422
Definition speedhqdec.c:58
uint32_t second_field_offset
Definition speedhqdec.c:62
IDCTDSPContext idsp
Definition speedhqdec.c:55
enum SHQContext::@125152013121145276305063330322113210325213330302 subsampling
enum SHQContext::@357215202121312356337062337000000340245345367061 alpha_type
uint8_t permutated_intra_scantable[64]
Definition speedhqdec.c:56
int quant_matrix[64]
Definition speedhqdec.c:57
Definition vlc.h:32
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
#define av_log(a,...)
#define width
Definition dsp.h:89
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
#define VLC_INIT_OUTPUT_LE
Definition vlc.h:196
#define VLC_INIT_LE
Definition vlc.h:197
#define VLC_INIT_STATIC_TABLE(vlc_table, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition vlc.h:278
#define VLC_INIT_STATIC_SPARSE_TABLE(vlc_table, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, symbols, symbols_wrap, symbols_size, flags)
Definition vlc.h:266
VLCElem RL_VLC_ELEM
Definition vlc.h:48
static const uint8_t quality[]
Definition vmixdec.c:58