FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/speedhq.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 172 330 52.1%
Branches: 76 199 38.2%

Line Branch Exec Source
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 {
51 AVCodecContext *avctx;
52 BlockDSPContext bdsp;
53 IDCTDSPContext idsp;
54 ScanTable intra_scantable;
55 int quant_matrix[64];
56 enum { SHQ_SUBSAMPLING_420, SHQ_SUBSAMPLING_422, SHQ_SUBSAMPLING_444 }
57 subsampling;
58 enum { SHQ_NO_ALPHA, SHQ_RLE_ALPHA, SHQ_DCT_ALPHA } alpha_type;
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
137 RLTable ff_rl_speedhq = {
138 121,
139 121,
140 speedhq_vlc,
141 speedhq_run,
142 speedhq_level,
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 336 static inline int decode_dc_le(GetBitContext *gb, int component)
164 {
165 int code, diff;
166
167
3/4
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 168 times.
336 if (component == 0 || component == 3) {
168 168 code = get_vlc2(gb, dc_lum_vlc_le.table, DC_VLC_BITS, 2);
169 } else {
170 168 code = get_vlc2(gb, dc_chroma_vlc_le.table, DC_VLC_BITS, 2);
171 }
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
336 if (!code) {
173 diff = 0;
174 } else {
175 336 diff = get_xbits_le(gb, code);
176 }
177 336 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 336 static inline int decode_dct_block(const SHQContext *s, GetBitContext *gb, int last_dc[4], int component, uint8_t *dest, int linesize)
221 {
222 336 const int *quant_matrix = s->quant_matrix;
223 336 const uint8_t *scantable = s->intra_scantable.permutated;
224 336 LOCAL_ALIGNED_32(int16_t, block, [64]);
225 int dc_offset;
226
227 336 s->bdsp.clear_block(block);
228
229 336 dc_offset = decode_dc_le(gb, component);
230 336 last_dc[component] -= dc_offset; /* Note: Opposite of most codecs. */
231 336 block[scantable[0]] = last_dc[component]; /* quant_matrix[0] is always 16. */
232
233 /* Read AC coefficients. */
234 {
235 336 int i = 0;
236 336 OPEN_READER(re, gb);
237 14663 for ( ;; ) {
238 int level, run;
239 14999 UPDATE_CACHE_LE(re, gb);
240
2/2
✓ Branch 0 taken 1131 times.
✓ Branch 1 taken 13868 times.
14999 GET_RL_VLC(level, run, re, gb, ff_rl_speedhq.rl_vlc[0],
241 TEX_VLC_BITS, 2, 0);
242
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 14663 times.
14999 if (level == 127) {
243 336 break;
244
2/2
✓ Branch 0 taken 14187 times.
✓ Branch 1 taken 476 times.
14663 } else if (level) {
245 14187 i += run;
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14187 times.
14187 if (i > MAX_INDEX)
247 return AVERROR_INVALIDDATA;
248 /* If next bit is 1, level = -level */
249 14187 level = (level ^ SHOW_SBITS(re, gb, 1)) -
250 14187 SHOW_SBITS(re, gb, 1);
251 14187 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 476 run = SHOW_UBITS(re, gb, 6) + 1;
258 476 SKIP_BITS(re, gb, 6);
259 476 level = SHOW_UBITS(re, gb, 12) - 2048;
260 476 LAST_SKIP_BITS(re, gb, 12);
261
262 476 i += run;
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 476 times.
476 if (i > MAX_INDEX)
264 return AVERROR_INVALIDDATA;
265 }
266
267 14663 block[scantable[i]] = (level * quant_matrix[i]) >> 4;
268 }
269 336 CLOSE_READER(re, gb);
270 }
271
272 336 s->idsp.idct_put(dest, linesize, block);
273
274 336 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 if (s->subsampling == SHQ_SUBSAMPLING_422) {
299 dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number) + x / 2;
300 dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number) + x / 2;
301 }
302 if (s->alpha_type != SHQ_NO_ALPHA) {
303 memset(last_alpha, 255, sizeof(last_alpha));
304 dest_a = frame->data[3] + frame->linesize[3] * (y + field_number) + x;
305 }
306
307 if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y, linesize_y)) < 0)
308 return ret;
309 if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
310 return ret;
311 if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
312 return ret;
313 if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
314 return ret;
315 if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
316 return ret;
317 if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
318 return ret;
319
320 if (s->subsampling != SHQ_SUBSAMPLING_420) {
321 if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
322 return ret;
323 if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
324 return ret;
325 }
326
327 if (s->alpha_type == SHQ_RLE_ALPHA) {
328 /* Alpha coded using 16x8 RLE blocks. */
329 if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a, linesize_a)) < 0)
330 return ret;
331 if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
332 return ret;
333 } else if (s->alpha_type == SHQ_DCT_ALPHA) {
334 /* Alpha encoded exactly like luma. */
335 if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a, linesize_a)) < 0)
336 return ret;
337 if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
338 return ret;
339 if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
340 return ret;
341 if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
342 return ret;
343 }
344 }
345
346 return 0;
347 }
348
349 3 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)
350 {
351 int ret, slice_number, slice_offsets[5];
352 3 int linesize_y = frame->linesize[0] * line_stride;
353 3 int linesize_cb = frame->linesize[1] * line_stride;
354 3 int linesize_cr = frame->linesize[2] * line_stride;
355 int linesize_a;
356 GetBitContext gb;
357
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->alpha_type != SHQ_NO_ALPHA)
359 linesize_a = frame->linesize[3] * line_stride;
360
361
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
3 if (end < start || end - start < 3 || end > buf_size)
362 return AVERROR_INVALIDDATA;
363
364 3 slice_offsets[0] = start;
365 3 slice_offsets[4] = end;
366
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (slice_number = 1; slice_number < 4; slice_number++) {
367 uint32_t last_offset, slice_len;
368
369 9 last_offset = slice_offsets[slice_number - 1];
370 9 slice_len = AV_RL24(buf + last_offset);
371 9 slice_offsets[slice_number] = last_offset + slice_len;
372
373
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 if (slice_len < 3 || slice_offsets[slice_number] > end - 3)
374 return AVERROR_INVALIDDATA;
375 }
376
377
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (slice_number = 0; slice_number < 4; slice_number++) {
378 uint32_t slice_begin, slice_end;
379 int x, y;
380
381 12 slice_begin = slice_offsets[slice_number];
382 12 slice_end = slice_offsets[slice_number + 1];
383
384
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if ((ret = init_get_bits8(&gb, buf + slice_begin + 3, slice_end - slice_begin - 3)) < 0)
385 return ret;
386
387
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
18 for (y = slice_number * 16 * line_stride; y < frame->height; y += line_stride * 64) {
388 uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
389 6 int last_dc[4] = { 1024, 1024, 1024, 1024 };
390 uint8_t last_alpha[16];
391
392 6 memset(last_alpha, 255, sizeof(last_alpha));
393
394 6 dest_y = frame->data[0] + frame->linesize[0] * (y + field_number);
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (s->subsampling == SHQ_SUBSAMPLING_420) {
396 dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number);
397 dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number);
398 } else {
399 6 dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number);
400 6 dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number);
401 }
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (s->alpha_type != SHQ_NO_ALPHA) {
403 dest_a = frame->data[3] + frame->linesize[3] * (y + field_number);
404 }
405
406
3/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 6 times.
48 for (x = 0; x < frame->width - 8 * (s->subsampling != SHQ_SUBSAMPLING_444); x += 16) {
407 /* Decode the four luma blocks. */
408
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y, linesize_y)) < 0)
409 return ret;
410
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8, linesize_y)) < 0)
411 return ret;
412
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0)
413 return ret;
414
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0)
415 return ret;
416
417 /*
418 * Decode the first chroma block. For 4:2:0, this is the only one;
419 * for 4:2:2, it's the top block; for 4:4:4, it's the top-left block.
420 */
421
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb, linesize_cb)) < 0)
422 return ret;
423
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr, linesize_cr)) < 0)
424 return ret;
425
426
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (s->subsampling != SHQ_SUBSAMPLING_420) {
427 /* For 4:2:2, this is the bottom block; for 4:4:4, it's the bottom-left block. */
428
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0)
429 return ret;
430
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0)
431 return ret;
432
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (s->subsampling == SHQ_SUBSAMPLING_444) {
434 /* Top-right and bottom-right blocks. */
435 if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8, linesize_cb)) < 0)
436 return ret;
437 if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8, linesize_cr)) < 0)
438 return ret;
439 if ((ret = decode_dct_block(s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb + 8, linesize_cb)) < 0)
440 return ret;
441 if ((ret = decode_dct_block(s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr + 8, linesize_cr)) < 0)
442 return ret;
443
444 dest_cb += 8;
445 dest_cr += 8;
446 }
447 }
448 42 dest_y += 16;
449 42 dest_cb += 8;
450 42 dest_cr += 8;
451
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (s->alpha_type == SHQ_RLE_ALPHA) {
453 /* Alpha coded using 16x8 RLE blocks. */
454 if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a, linesize_a)) < 0)
455 return ret;
456 if ((ret = decode_alpha_block(s, &gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
457 return ret;
458 dest_a += 16;
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 } else if (s->alpha_type == SHQ_DCT_ALPHA) {
460 /* Alpha encoded exactly like luma. */
461 if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a, linesize_a)) < 0)
462 return ret;
463 if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8, linesize_a)) < 0)
464 return ret;
465 if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0)
466 return ret;
467 if ((ret = decode_dct_block(s, &gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0)
468 return ret;
469 dest_a += 16;
470 }
471 }
472 }
473 }
474
475
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (s->subsampling != SHQ_SUBSAMPLING_444 && (frame->width & 15))
476 return decode_speedhq_border(s, &gb, frame, field_number, line_stride);
477
478 3 return 0;
479 }
480
481 2 static void compute_quant_matrix(int *output, int qscale)
482 {
483 int i;
484
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 for (i = 0; i < 64; i++) output[i] = unscaled_quant_matrix[ff_zigzag_direct[i]] * qscale;
485 2 }
486
487 2 static int speedhq_decode_frame(AVCodecContext *avctx,
488 void *data, int *got_frame,
489 AVPacket *avpkt)
490 {
491 2 SHQContext * const s = avctx->priv_data;
492 2 const uint8_t *buf = avpkt->data;
493 2 int buf_size = avpkt->size;
494 2 AVFrame *frame = data;
495 uint8_t quality;
496 uint32_t second_field_offset;
497 int ret;
498
499
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (buf_size < 4 || avctx->width < 8)
500 return AVERROR_INVALIDDATA;
501
502 2 quality = buf[0];
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (quality >= 100) {
504 return AVERROR_INVALIDDATA;
505 }
506
507 2 compute_quant_matrix(s->quant_matrix, 100 - quality);
508
509 2 second_field_offset = AV_RL24(buf + 1);
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (second_field_offset >= buf_size - 3) {
511 return AVERROR_INVALIDDATA;
512 }
513
514 2 avctx->coded_width = FFALIGN(avctx->width, 16);
515 2 avctx->coded_height = FFALIGN(avctx->height, 16);
516
517
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
518 return ret;
519 }
520 2 frame->key_frame = 1;
521
522
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 if (second_field_offset == 4 || second_field_offset == (buf_size-4)) {
523 /*
524 * Overlapping first and second fields is used to signal
525 * encoding only a single field. In this case, "height"
526 * is ambiguous; it could mean either the height of the
527 * frame as a whole, or of the field. The former would make
528 * more sense for compatibility with legacy decoders,
529 * but this matches the convention used in NDI, which is
530 * the primary user of this trick.
531 */
532
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, buf_size, 1)) < 0)
533 return ret;
534 } else {
535
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, second_field_offset, 2)) < 0)
536 return ret;
537
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 1, second_field_offset, buf_size, 2)) < 0)
538 return ret;
539 }
540
541 2 *got_frame = 1;
542 2 return buf_size;
543 }
544
545 /*
546 * Alpha VLC. Run and level are independently coded, and would be
547 * outside the default limits for MAX_RUN/MAX_LEVEL, so we don't
548 * bother with combining them into one table.
549 */
550 2 static av_cold void compute_alpha_vlcs(void)
551 {
552 uint16_t run_code[134], level_code[266];
553 uint8_t run_bits[134], level_bits[266];
554 int16_t run_symbols[134], level_symbols[266];
555 int entry, i, sign;
556
557 /* Initialize VLC for alpha run. */
558 2 entry = 0;
559
560 /* 0 -> 0. */
561 2 run_code[entry] = 0;
562 2 run_bits[entry] = 1;
563 2 run_symbols[entry] = 0;
564 2 ++entry;
565
566 /* 10xx -> xx plus 1. */
567
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < 4; ++i) {
568 8 run_code[entry] = (i << 2) | 1;
569 8 run_bits[entry] = 4;
570 8 run_symbols[entry] = i + 1;
571 8 ++entry;
572 }
573
574 /* 111xxxxxxx -> xxxxxxx. */
575
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 2 times.
258 for (i = 0; i < 128; ++i) {
576 256 run_code[entry] = (i << 3) | 7;
577 256 run_bits[entry] = 10;
578 256 run_symbols[entry] = i;
579 256 ++entry;
580 }
581
582 /* 110 -> EOB. */
583 2 run_code[entry] = 3;
584 2 run_bits[entry] = 3;
585 2 run_symbols[entry] = -1;
586 2 ++entry;
587
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(entry == FF_ARRAY_ELEMS(run_code));
589
590 2 INIT_LE_VLC_SPARSE_STATIC(&dc_alpha_run_vlc_le, ALPHA_VLC_BITS,
591 FF_ARRAY_ELEMS(run_code),
592 run_bits, 1, 1,
593 run_code, 2, 2,
594 run_symbols, 2, 2, 160);
595
596 /* Initialize VLC for alpha level. */
597 2 entry = 0;
598
599
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (sign = 0; sign <= 1; ++sign) {
600 /* 1s -> -1 or +1 (depending on sign bit). */
601 4 level_code[entry] = (sign << 1) | 1;
602 4 level_bits[entry] = 2;
603
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 level_symbols[entry] = sign ? -1 : 1;
604 4 ++entry;
605
606 /* 01sxx -> xx plus 2 (2..5 or -2..-5, depending on sign bit). */
607
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (i = 0; i < 4; ++i) {
608 16 level_code[entry] = (i << 3) | (sign << 2) | 2;
609 16 level_bits[entry] = 5;
610
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 level_symbols[entry] = sign ? -(i + 2) : (i + 2);
611 16 ++entry;
612 }
613 }
614
615 /*
616 * 00xxxxxxxx -> xxxxxxxx, in two's complement. There are many codes
617 * here that would better be encoded in other ways (e.g. 0 would be
618 * encoded by increasing run, and +/- 1 would be encoded with a
619 * shorter code), but it doesn't hurt to allow everything.
620 */
621
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (i = 0; i < 256; ++i) {
622 512 level_code[entry] = i << 2;
623 512 level_bits[entry] = 10;
624 512 level_symbols[entry] = i;
625 512 ++entry;
626 }
627
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(entry == FF_ARRAY_ELEMS(level_code));
629
630 2 INIT_LE_VLC_SPARSE_STATIC(&dc_alpha_level_vlc_le, ALPHA_VLC_BITS,
631 FF_ARRAY_ELEMS(level_code),
632 level_bits, 1, 1,
633 level_code, 2, 2,
634 level_symbols, 2, 2, 288);
635 2 }
636
637 2 static av_cold void speedhq_static_init(void)
638 {
639 /* Exactly the same as MPEG-2, except for a little-endian reader. */
640 2 INIT_CUSTOM_VLC_STATIC(&dc_lum_vlc_le, DC_VLC_BITS, 12,
641 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
642 ff_mpeg12_vlc_dc_lum_code, 2, 2,
643 INIT_VLC_OUTPUT_LE, 512);
644 2 INIT_CUSTOM_VLC_STATIC(&dc_chroma_vlc_le, DC_VLC_BITS, 12,
645 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
646 ff_mpeg12_vlc_dc_chroma_code, 2, 2,
647 INIT_VLC_OUTPUT_LE, 514);
648
649 2 INIT_2D_VLC_RL(ff_rl_speedhq, 674, INIT_VLC_LE);
650
651 2 compute_alpha_vlcs();
652 2 }
653
654 4 static av_cold int speedhq_decode_init(AVCodecContext *avctx)
655 {
656 int ret;
657 static AVOnce init_once = AV_ONCE_INIT;
658 4 SHQContext * const s = avctx->priv_data;
659
660 4 s->avctx = avctx;
661
662 4 ret = ff_thread_once(&init_once, speedhq_static_init);
663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret)
664 return AVERROR_UNKNOWN;
665
666 4 ff_blockdsp_init(&s->bdsp, avctx);
667 4 ff_idctdsp_init(&s->idsp, avctx);
668 4 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
669
670
2/9
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
4 switch (avctx->codec_tag) {
671 case MKTAG('S', 'H', 'Q', '0'):
672 s->subsampling = SHQ_SUBSAMPLING_420;
673 s->alpha_type = SHQ_NO_ALPHA;
674 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
675 break;
676 case MKTAG('S', 'H', 'Q', '1'):
677 s->subsampling = SHQ_SUBSAMPLING_420;
678 s->alpha_type = SHQ_RLE_ALPHA;
679 avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
680 break;
681 2 case MKTAG('S', 'H', 'Q', '2'):
682 2 s->subsampling = SHQ_SUBSAMPLING_422;
683 2 s->alpha_type = SHQ_NO_ALPHA;
684 2 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
685 2 break;
686 case MKTAG('S', 'H', 'Q', '3'):
687 s->subsampling = SHQ_SUBSAMPLING_422;
688 s->alpha_type = SHQ_RLE_ALPHA;
689 avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
690 break;
691 case MKTAG('S', 'H', 'Q', '4'):
692 s->subsampling = SHQ_SUBSAMPLING_444;
693 s->alpha_type = SHQ_NO_ALPHA;
694 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
695 break;
696 case MKTAG('S', 'H', 'Q', '5'):
697 s->subsampling = SHQ_SUBSAMPLING_444;
698 s->alpha_type = SHQ_RLE_ALPHA;
699 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
700 break;
701 case MKTAG('S', 'H', 'Q', '7'):
702 s->subsampling = SHQ_SUBSAMPLING_422;
703 s->alpha_type = SHQ_DCT_ALPHA;
704 avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
705 break;
706 case MKTAG('S', 'H', 'Q', '9'):
707 s->subsampling = SHQ_SUBSAMPLING_444;
708 s->alpha_type = SHQ_DCT_ALPHA;
709 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
710 break;
711 2 default:
712 2 av_log(avctx, AV_LOG_ERROR, "Unknown NewTek SpeedHQ FOURCC provided (%08X)\n",
713 avctx->codec_tag);
714 2 return AVERROR_INVALIDDATA;
715 }
716
717 /* This matches what NDI's RGB -> Y'CbCr 4:2:2 converter uses. */
718 2 avctx->colorspace = AVCOL_SPC_BT470BG;
719 2 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
720
721 2 return 0;
722 }
723
724 const AVCodec ff_speedhq_decoder = {
725 .name = "speedhq",
726 .long_name = NULL_IF_CONFIG_SMALL("NewTek SpeedHQ"),
727 .type = AVMEDIA_TYPE_VIDEO,
728 .id = AV_CODEC_ID_SPEEDHQ,
729 .priv_data_size = sizeof(SHQContext),
730 .init = speedhq_decode_init,
731 .decode = speedhq_decode_frame,
732 .capabilities = AV_CODEC_CAP_DR1,
733 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
734 };
735 #endif /* CONFIG_SPEEDHQ_DECODER */
736