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