FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hq_hqa.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 171 204 83.8%
Functions: 10 10 100.0%
Branches: 75 102 73.5%

Line Branch Exec Source
1 /*
2 * Canopus HQ/HQA decoder
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdint.h>
22
23 #include "libavutil/attributes.h"
24 #include "libavutil/mem_internal.h"
25 #include "libavutil/thread.h"
26
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "canopus.h"
30 #include "codec_internal.h"
31 #include "decode.h"
32 #include "get_bits.h"
33 #include "hq_common.h"
34 #include "hq_hqadata.h"
35 #include "hq_hqadsp.h"
36 #include "vlc.h"
37
38 /* HQ/HQA slices are a set of macroblocks belonging to a frame, and
39 * they usually form a pseudorandom pattern (probably because it is
40 * nicer to display on partial decode).
41 *
42 * For HQA it just happens that each slice is on every 8th macroblock,
43 * but they can be on any frame width like
44 * X.......X.
45 * ......X...
46 * ....X.....
47 * ..X.......
48 * etc.
49 *
50 * The original decoder has special handling for edge macroblocks,
51 * while lavc simply aligns coded_width and coded_height.
52 */
53
54 typedef struct HQContext {
55 AVCodecContext *avctx;
56 HQDSPContext hqhqadsp;
57
58 DECLARE_ALIGNED(16, int16_t, block)[12][64];
59 } HQContext;
60
61 static const int32_t *hq_quants[NUM_HQ_QUANTS][2][4];
62
63 static RL_VLC_ELEM hq_ac_rvlc[1184];
64
65 221520 static inline void put_blocks(HQContext *c, AVFrame *pic,
66 int plane, int x, int y, int ilace,
67 int16_t *block0, int16_t *block1)
68 {
69 221520 uint8_t *p = pic->data[plane] + x;
70
71 221520 c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
72 221520 pic->linesize[plane] << ilace, block0);
73 221520 c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
74
2/2
✓ Branch 0 taken 1916 times.
✓ Branch 1 taken 219604 times.
221520 pic->linesize[plane] << ilace, block1);
75 221520 }
76
77 365300 static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
78 int qsel, int is_chroma, int is_hqa)
79 {
80 const int32_t *q;
81
82
2/2
✓ Branch 0 taken 247200 times.
✓ Branch 1 taken 118100 times.
365300 if (!is_hqa) {
83 247200 block[0] = get_sbits(gb, 9) * 64;
84 247200 q = hq_quants[qsel][is_chroma][get_bits(gb, 2)];
85 } else {
86 118100 q = hq_quants[qsel][is_chroma][get_bits(gb, 2)];
87 118100 block[0] = get_sbits(gb, 9) * 64;
88 }
89
90 365300 OPEN_READER(re, gb);
91 365300 for (int pos = 0;;) {
92 int level, run;
93 5841374 UPDATE_CACHE(re, gb);
94
2/2
✓ Branch 1 taken 368917 times.
✓ Branch 2 taken 5472457 times.
5841374 GET_RL_VLC(level, run, re, gb, hq_ac_rvlc, 9, 2, 0);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5841374 times.
5841374 if (run == HQ_AC_INVALID_RUN) {
96 CLOSE_READER(re, gb);
97 return AVERROR_INVALIDDATA;
98 }
99
100 5841374 pos += run;
101
2/2
✓ Branch 0 taken 365300 times.
✓ Branch 1 taken 5476074 times.
5841374 if (pos >= 64)
102 365300 break;
103 5476074 block[ff_zigzag_direct[pos]] = (int)(level * (unsigned)q[pos]) >> 12;
104 }
105 365300 CLOSE_READER(re, gb);
106
107 365300 return 0;
108 }
109
110 30900 static int hq_decode_mb(HQContext *c, AVFrame *pic,
111 GetBitContext *gb, int x, int y)
112 {
113 int qgroup, flag;
114 int i, ret;
115
116 30900 memset(c->block, 0, 8 * sizeof(c->block[0]));
117
118 30900 qgroup = get_bits(gb, 4);
119 30900 flag = get_bits1(gb);
120
121
2/2
✓ Branch 0 taken 247200 times.
✓ Branch 1 taken 30900 times.
278100 for (i = 0; i < 8; i++) {
122 247200 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 247200 times.
247200 if (ret < 0)
124 return ret;
125 }
126
127 30900 put_blocks(c, pic, 0, x, y, flag, c->block[0], c->block[2]);
128 30900 put_blocks(c, pic, 0, x + 8, y, flag, c->block[1], c->block[3]);
129 30900 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
130 30900 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
131
132 30900 return 0;
133 }
134
135 11 static int hq_decode_frame(HQContext *ctx, AVFrame *pic, GetByteContext *gbc,
136 int prof_num, size_t data_size)
137 {
138 const HQProfile *profile;
139 GetBitContext gb;
140 11 const uint8_t *perm, *src = gbc->buffer;
141 uint32_t slice_off[21];
142 int slice, start_off, next_off, i, ret;
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if ((unsigned)prof_num >= NUM_HQ_PROFILES) {
145 profile = &hq_profile[0];
146 avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
147 } else {
148 11 profile = &hq_profile[prof_num];
149 11 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
150 }
151
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 if (bytestream2_get_bytes_left(gbc) < 3 * (profile->num_slices + 1))
153 return AVERROR_INVALIDDATA;
154
155 11 ctx->avctx->coded_width = FFALIGN(profile->width, 16);
156 11 ctx->avctx->coded_height = FFALIGN(profile->height, 16);
157 11 ctx->avctx->width = profile->width;
158 11 ctx->avctx->height = profile->height;
159 11 ctx->avctx->bits_per_raw_sample = 8;
160 11 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
161
162 11 ret = ff_get_buffer(ctx->avctx, pic, 0);
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
164 return ret;
165
166 /* Offsets are stored from CUV position, so adjust them accordingly. */
167
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 11 times.
134 for (i = 0; i < profile->num_slices + 1; i++)
168 123 slice_off[i] = bytestream2_get_be24u(gbc) - 4;
169
170 11 next_off = 0;
171
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 11 times.
123 for (slice = 0; slice < profile->num_slices; slice++) {
172 112 start_off = next_off;
173 112 next_off = profile->tab_h * (slice + 1) / profile->num_slices;
174 112 perm = profile->perm_tab + start_off * profile->tab_w * 2;
175
176
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
177
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 slice_off[slice] >= slice_off[slice + 1] ||
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 slice_off[slice + 1] > data_size) {
179 av_log(ctx->avctx, AV_LOG_ERROR,
180 "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
181 break;
182 }
183 112 init_get_bits(&gb, src + slice_off[slice],
184 112 (slice_off[slice + 1] - slice_off[slice]) * 8);
185
186
2/2
✓ Branch 0 taken 30900 times.
✓ Branch 1 taken 112 times.
31012 for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
187 30900 ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30900 times.
30900 if (ret < 0) {
189 av_log(ctx->avctx, AV_LOG_ERROR,
190 "Error decoding macroblock %d at slice %d.\n", i, slice);
191 return ret;
192 }
193 30900 perm += 2;
194 }
195 }
196
197 11 return 0;
198 }
199
200 16320 static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
201 GetBitContext *gb, int x, int y)
202 {
203 16320 int flag = 0;
204 int i, ret;
205
206
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16320 times.
16320 if (get_bits_left(gb) < 1)
207 return AVERROR_INVALIDDATA;
208
209 16320 memset(c->block, 0, 12 * sizeof(c->block[0]));
210
2/2
✓ Branch 0 taken 195840 times.
✓ Branch 1 taken 16320 times.
212160 for (i = 0; i < 12; i++)
211 195840 c->block[i][0] = -128 * (1 << 6);
212
213 16320 int cbp = get_vlc2(gb, ff_hq_cbp_vlc, HQ_CBP_VLC_BITS, 1);
214
2/2
✓ Branch 0 taken 9978 times.
✓ Branch 1 taken 6342 times.
16320 if (cbp) {
215 9978 flag = get_bits1(gb);
216
217
2/2
✓ Branch 0 taken 9890 times.
✓ Branch 1 taken 88 times.
9978 if (cbp & 0x3)
218 9890 cbp |= 0x500;
219
2/2
✓ Branch 0 taken 9874 times.
✓ Branch 1 taken 104 times.
9978 if (cbp & 0xC)
220 9874 cbp |= 0xA00;
221
2/2
✓ Branch 0 taken 119736 times.
✓ Branch 1 taken 9978 times.
129714 for (i = 0; i < 12; i++) {
222
2/2
✓ Branch 0 taken 1636 times.
✓ Branch 1 taken 118100 times.
119736 if (!(cbp & (1 << i)))
223 1636 continue;
224 118100 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118100 times.
118100 if (ret < 0)
226 return ret;
227 }
228 }
229
230 16320 put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
231 16320 put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
232 16320 put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
233 16320 put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
234 16320 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
235 16320 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
236
237 16320 return 0;
238 }
239
240 16 static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
241 int quant, int slice_no, int w, int h)
242 {
243 int i, j, off;
244 int ret;
245
246
2/2
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 16 times.
1104 for (i = 0; i < h; i += 16) {
247 1088 off = (slice_no * 16 + i * 3) & 0x70;
248
2/2
✓ Branch 0 taken 16320 times.
✓ Branch 1 taken 1088 times.
17408 for (j = off; j < w; j += 128) {
249 16320 ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16320 times.
16320 if (ret < 0) {
251 av_log(ctx->avctx, AV_LOG_ERROR,
252 "Error decoding macroblock at %dx%d.\n", i, j);
253 return ret;
254 }
255 }
256 }
257
258 16 return 0;
259 }
260
261 2 static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, GetByteContext *gbc, size_t data_size)
262 {
263 GetBitContext gb;
264 2 const int num_slices = 8;
265 uint32_t slice_off[9];
266 int i, slice, ret;
267 2 const uint8_t *src = gbc->buffer;
268
269
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_bytes_left(gbc) < 8 + 4*(num_slices + 1))
270 return AVERROR_INVALIDDATA;
271
272 2 int width = bytestream2_get_be16u(gbc);
273 2 int height = bytestream2_get_be16u(gbc);
274
275 2 ret = ff_set_dimensions(ctx->avctx, width, height);
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
277 return ret;
278
279 2 ctx->avctx->coded_width = FFALIGN(width, 16);
280 2 ctx->avctx->coded_height = FFALIGN(height, 16);
281 2 ctx->avctx->bits_per_raw_sample = 8;
282 2 ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
283
284 2 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
285
286 2 int quant = bytestream2_get_byteu(gbc);
287 2 bytestream2_skipu(gbc, 3);
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (quant >= NUM_HQ_QUANTS) {
289 av_log(ctx->avctx, AV_LOG_ERROR,
290 "Invalid quantization matrix %d.\n", quant);
291 return AVERROR_INVALIDDATA;
292 }
293
294 2 ret = ff_get_buffer(ctx->avctx, pic, 0);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
296 return ret;
297
298 /* Offsets are stored from HQA1 position, so adjust them accordingly. */
299
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (i = 0; i < num_slices + 1; i++)
300 18 slice_off[i] = bytestream2_get_be32u(gbc) - 4;
301
302
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (slice = 0; slice < num_slices; slice++) {
303
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (slice_off[slice] < (num_slices + 1) * 3 ||
304
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 slice_off[slice] >= slice_off[slice + 1] ||
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 slice_off[slice + 1] > data_size) {
306 av_log(ctx->avctx, AV_LOG_ERROR,
307 "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
308 break;
309 }
310 16 init_get_bits(&gb, src + slice_off[slice],
311 16 (slice_off[slice + 1] - slice_off[slice]) * 8);
312
313 16 ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
315 return ret;
316 }
317
318 2 return 0;
319 }
320
321 13 static int hq_hqa_decode_frame(AVCodecContext *avctx, AVFrame *pic,
322 int *got_frame, AVPacket *avpkt)
323 {
324 13 HQContext *ctx = avctx->priv_data;
325 13 GetByteContext gbc0, *const gbc = &gbc0;
326 unsigned int data_size;
327 int ret;
328
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (avpkt->size < 4 + 4) {
330 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
331 return AVERROR_INVALIDDATA;
332 }
333 13 bytestream2_init(gbc, avpkt->data, avpkt->size);
334
335 13 uint32_t info_tag = bytestream2_peek_le32u(gbc);
336
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
337 13 bytestream2_skipu(gbc, 4);
338 13 int info_size = bytestream2_get_le32u(gbc);
339
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
13 if (info_size < 0 || bytestream2_get_bytes_left(gbc) < info_size) {
340 av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
341 return AVERROR_INVALIDDATA;
342 }
343 13 ff_canopus_parse_info_tag(avctx, gbc->buffer, info_size);
344
345 13 bytestream2_skipu(gbc, info_size);
346 }
347
348 13 data_size = bytestream2_get_bytes_left(gbc);
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (data_size < 4) {
350 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
351 return AVERROR_INVALIDDATA;
352 }
353
354 /* HQ defines dimensions and number of slices, and thus slice traversal
355 * order. HQA has no size constraint and a fixed number of slices, so it
356 * needs a separate scheme for it. */
357 13 unsigned tag = bytestream2_get_le32u(gbc);
358
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
13 if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
359 11 ret = hq_decode_frame(ctx, pic, gbc, tag >> 24, data_size);
360
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
361 2 ret = hqa_decode_frame(ctx, pic, gbc, data_size);
362 } else {
363 av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
364 return AVERROR_INVALIDDATA;
365 }
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0) {
367 av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
368 return ret;
369 }
370
371 13 *got_frame = 1;
372
373 13 return avpkt->size;
374 }
375
376 3 static av_cold void hq_init_static(void)
377 {
378 3 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(hq_ac_rvlc, 9, NUM_HQ_AC_ENTRIES,
379 hq_ac_lens, 1, hq_ac_sym, 2, 2, 0, 0);
380
381
2/2
✓ Branch 0 taken 3552 times.
✓ Branch 1 taken 3 times.
3555 for (size_t i = 0; i < FF_ARRAY_ELEMS(hq_ac_rvlc); ++i) {
382 3552 int len = hq_ac_rvlc[i].len;
383 3552 int sym = (int16_t)hq_ac_rvlc[i].sym, level = sym;
384
385 // The invalid code has been remapped to HQ_AC_INVALID_RUN,
386 // so the VLC is complete.
387 av_assert1(len != 0);
388
389
2/2
✓ Branch 0 taken 3456 times.
✓ Branch 1 taken 96 times.
3552 if (len > 0) {
390 3456 level = sym >> 7;
391 3456 hq_ac_rvlc[i].run = sym & 0x7F;
392 }
393 3552 hq_ac_rvlc[i].len8 = len;
394 3552 hq_ac_rvlc[i].level = level;
395 }
396
397
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (size_t i = 0; i < FF_ARRAY_ELEMS(hq_quants); ++i) {
398
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
144 for (size_t j = 0; j < FF_ARRAY_ELEMS(hq_quants[0]); ++j) {
399
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 96 times.
480 for (size_t k = 0; k < FF_ARRAY_ELEMS(hq_quants[0][0]); ++k)
400 384 hq_quants[i][j][k] = qmats[hq_quant_map[i][j][k]];
401 }
402 }
403 3 }
404
405 6 static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
406 {
407 static AVOnce init_static_once = AV_ONCE_INIT;
408 6 HQContext *ctx = avctx->priv_data;
409 6 ctx->avctx = avctx;
410
411 6 ff_hqdsp_init(&ctx->hqhqadsp);
412
413 6 ff_thread_once(&init_static_once, hq_init_static);
414
415 6 return 0;
416 }
417
418 const FFCodec ff_hq_hqa_decoder = {
419 .p.name = "hq_hqa",
420 CODEC_LONG_NAME("Canopus HQ/HQA"),
421 .p.type = AVMEDIA_TYPE_VIDEO,
422 .p.id = AV_CODEC_ID_HQ_HQA,
423 .priv_data_size = sizeof(HQContext),
424 .init = hq_hqa_decode_init,
425 FF_CODEC_DECODE_CB(hq_hqa_decode_frame),
426 .p.capabilities = AV_CODEC_CAP_DR1,
427 };
428