FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hq_hqa.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 160 191 83.8%
Branches: 64 90 71.1%

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/intreadwrite.h"
25
26 #include "avcodec.h"
27 #include "canopus.h"
28 #include "codec_internal.h"
29 #include "get_bits.h"
30 #include "internal.h"
31
32 #include "hq_hqa.h"
33 #include "hq_hqadsp.h"
34
35 /* HQ/HQA slices are a set of macroblocks belonging to a frame, and
36 * they usually form a pseudorandom pattern (probably because it is
37 * nicer to display on partial decode).
38 *
39 * For HQA it just happens that each slice is on every 8th macroblock,
40 * but they can be on any frame width like
41 * X.......X.
42 * ......X...
43 * ....X.....
44 * ..X.......
45 * etc.
46 *
47 * The original decoder has special handling for edge macroblocks,
48 * while lavc simply aligns coded_width and coded_height.
49 */
50
51 221520 static inline void put_blocks(HQContext *c, AVFrame *pic,
52 int plane, int x, int y, int ilace,
53 int16_t *block0, int16_t *block1)
54 {
55 221520 uint8_t *p = pic->data[plane] + x;
56
57 221520 c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
58 221520 pic->linesize[plane] << ilace, block0);
59 221520 c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
60
2/2
✓ Branch 0 taken 1916 times.
✓ Branch 1 taken 219604 times.
221520 pic->linesize[plane] << ilace, block1);
61 221520 }
62
63 365300 static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
64 int qsel, int is_chroma, int is_hqa)
65 {
66 const int32_t *q;
67 365300 int val, pos = 1;
68
69 365300 memset(block, 0, 64 * sizeof(*block));
70
71
2/2
✓ Branch 0 taken 247200 times.
✓ Branch 1 taken 118100 times.
365300 if (!is_hqa) {
72 247200 block[0] = get_sbits(gb, 9) * 64;
73 247200 q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
74 } else {
75 118100 q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
76 118100 block[0] = get_sbits(gb, 9) * 64;
77 }
78
79 for (;;) {
80 5841374 val = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5841374 times.
5841374 if (val < 0)
82 return AVERROR_INVALIDDATA;
83
84 5841374 pos += ff_hq_ac_skips[val];
85
2/2
✓ Branch 0 taken 365300 times.
✓ Branch 1 taken 5476074 times.
5841374 if (pos >= 64)
86 365300 break;
87 5476074 block[ff_zigzag_direct[pos]] = (int)(ff_hq_ac_syms[val] * (unsigned)q[pos]) >> 12;
88 5476074 pos++;
89 }
90
91 365300 return 0;
92 }
93
94 30900 static int hq_decode_mb(HQContext *c, AVFrame *pic,
95 GetBitContext *gb, int x, int y)
96 {
97 int qgroup, flag;
98 int i, ret;
99
100 30900 qgroup = get_bits(gb, 4);
101 30900 flag = get_bits1(gb);
102
103
2/2
✓ Branch 0 taken 247200 times.
✓ Branch 1 taken 30900 times.
278100 for (i = 0; i < 8; i++) {
104 247200 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 247200 times.
247200 if (ret < 0)
106 return ret;
107 }
108
109 30900 put_blocks(c, pic, 0, x, y, flag, c->block[0], c->block[2]);
110 30900 put_blocks(c, pic, 0, x + 8, y, flag, c->block[1], c->block[3]);
111 30900 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
112 30900 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
113
114 30900 return 0;
115 }
116
117 11 static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
118 int prof_num, size_t data_size)
119 {
120 const HQProfile *profile;
121 GetBitContext gb;
122 11 const uint8_t *perm, *src = ctx->gbc.buffer;
123 uint32_t slice_off[21];
124 int slice, start_off, next_off, i, ret;
125
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if ((unsigned)prof_num >= NUM_HQ_PROFILES) {
127 profile = &ff_hq_profile[0];
128 avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
129 } else {
130 11 profile = &ff_hq_profile[prof_num];
131 11 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
132 }
133
134 11 ctx->avctx->coded_width = FFALIGN(profile->width, 16);
135 11 ctx->avctx->coded_height = FFALIGN(profile->height, 16);
136 11 ctx->avctx->width = profile->width;
137 11 ctx->avctx->height = profile->height;
138 11 ctx->avctx->bits_per_raw_sample = 8;
139 11 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
140
141 11 ret = ff_get_buffer(ctx->avctx, pic, 0);
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
143 return ret;
144
145 /* Offsets are stored from CUV position, so adjust them accordingly. */
146
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 11 times.
134 for (i = 0; i < profile->num_slices + 1; i++)
147 123 slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
148
149 11 next_off = 0;
150
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 11 times.
123 for (slice = 0; slice < profile->num_slices; slice++) {
151 112 start_off = next_off;
152 112 next_off = profile->tab_h * (slice + 1) / profile->num_slices;
153 112 perm = profile->perm_tab + start_off * profile->tab_w * 2;
154
155
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
156
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 slice_off[slice] >= slice_off[slice + 1] ||
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 slice_off[slice + 1] > data_size) {
158 av_log(ctx->avctx, AV_LOG_ERROR,
159 "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
160 break;
161 }
162 112 init_get_bits(&gb, src + slice_off[slice],
163 112 (slice_off[slice + 1] - slice_off[slice]) * 8);
164
165
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++) {
166 30900 ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30900 times.
30900 if (ret < 0) {
168 av_log(ctx->avctx, AV_LOG_ERROR,
169 "Error decoding macroblock %d at slice %d.\n", i, slice);
170 return ret;
171 }
172 30900 perm += 2;
173 }
174 }
175
176 11 return 0;
177 }
178
179 16320 static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
180 GetBitContext *gb, int x, int y)
181 {
182 16320 int flag = 0;
183 int i, ret, cbp;
184
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16320 times.
16320 if (get_bits_left(gb) < 1)
186 return AVERROR_INVALIDDATA;
187
188 16320 cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
189
190
2/2
✓ Branch 0 taken 195840 times.
✓ Branch 1 taken 16320 times.
212160 for (i = 0; i < 12; i++)
191 195840 memset(c->block[i], 0, sizeof(*c->block));
192
2/2
✓ Branch 0 taken 195840 times.
✓ Branch 1 taken 16320 times.
212160 for (i = 0; i < 12; i++)
193 195840 c->block[i][0] = -128 * (1 << 6);
194
195
2/2
✓ Branch 0 taken 9978 times.
✓ Branch 1 taken 6342 times.
16320 if (cbp) {
196 9978 flag = get_bits1(gb);
197
198 9978 cbp |= cbp << 4;
199
2/2
✓ Branch 0 taken 9890 times.
✓ Branch 1 taken 88 times.
9978 if (cbp & 0x3)
200 9890 cbp |= 0x500;
201
2/2
✓ Branch 0 taken 9874 times.
✓ Branch 1 taken 104 times.
9978 if (cbp & 0xC)
202 9874 cbp |= 0xA00;
203
2/2
✓ Branch 0 taken 119736 times.
✓ Branch 1 taken 9978 times.
129714 for (i = 0; i < 12; i++) {
204
2/2
✓ Branch 0 taken 1636 times.
✓ Branch 1 taken 118100 times.
119736 if (!(cbp & (1 << i)))
205 1636 continue;
206 118100 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118100 times.
118100 if (ret < 0)
208 return ret;
209 }
210 }
211
212 16320 put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
213 16320 put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
214 16320 put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
215 16320 put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
216 16320 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
217 16320 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
218
219 16320 return 0;
220 }
221
222 16 static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
223 int quant, int slice_no, int w, int h)
224 {
225 int i, j, off;
226 int ret;
227
228
2/2
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 16 times.
1104 for (i = 0; i < h; i += 16) {
229 1088 off = (slice_no * 16 + i * 3) & 0x70;
230
2/2
✓ Branch 0 taken 16320 times.
✓ Branch 1 taken 1088 times.
17408 for (j = off; j < w; j += 128) {
231 16320 ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16320 times.
16320 if (ret < 0) {
233 av_log(ctx->avctx, AV_LOG_ERROR,
234 "Error decoding macroblock at %dx%d.\n", i, j);
235 return ret;
236 }
237 }
238 }
239
240 16 return 0;
241 }
242
243 2 static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
244 {
245 GetBitContext gb;
246 2 const int num_slices = 8;
247 uint32_t slice_off[9];
248 int i, slice, ret;
249 int width, height, quant;
250 2 const uint8_t *src = ctx->gbc.buffer;
251
252
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_bytes_left(&ctx->gbc) < 8 + 4*(num_slices + 1))
253 return AVERROR_INVALIDDATA;
254
255 2 width = bytestream2_get_be16(&ctx->gbc);
256 2 height = bytestream2_get_be16(&ctx->gbc);
257
258 2 ret = ff_set_dimensions(ctx->avctx, width, height);
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
260 return ret;
261
262 2 ctx->avctx->coded_width = FFALIGN(width, 16);
263 2 ctx->avctx->coded_height = FFALIGN(height, 16);
264 2 ctx->avctx->bits_per_raw_sample = 8;
265 2 ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
266
267 2 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
268
269 2 quant = bytestream2_get_byte(&ctx->gbc);
270 2 bytestream2_skip(&ctx->gbc, 3);
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (quant >= NUM_HQ_QUANTS) {
272 av_log(ctx->avctx, AV_LOG_ERROR,
273 "Invalid quantization matrix %d.\n", quant);
274 return AVERROR_INVALIDDATA;
275 }
276
277 2 ret = ff_get_buffer(ctx->avctx, pic, 0);
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
279 return ret;
280
281 /* Offsets are stored from HQA1 position, so adjust them accordingly. */
282
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (i = 0; i < num_slices + 1; i++)
283 18 slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
284
285
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (slice = 0; slice < num_slices; slice++) {
286
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (slice_off[slice] < (num_slices + 1) * 3 ||
287
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 slice_off[slice] >= slice_off[slice + 1] ||
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 slice_off[slice + 1] > data_size) {
289 av_log(ctx->avctx, AV_LOG_ERROR,
290 "Invalid slice size %"SIZE_SPECIFIER".\n", data_size);
291 break;
292 }
293 16 init_get_bits(&gb, src + slice_off[slice],
294 16 (slice_off[slice + 1] - slice_off[slice]) * 8);
295
296 16 ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
298 return ret;
299 }
300
301 2 return 0;
302 }
303
304 13 static int hq_hqa_decode_frame(AVCodecContext *avctx, AVFrame *pic,
305 int *got_frame, AVPacket *avpkt)
306 {
307 13 HQContext *ctx = avctx->priv_data;
308 uint32_t info_tag;
309 unsigned int data_size;
310 int ret;
311 unsigned tag;
312
313 13 bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
314
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
315 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
316 return AVERROR_INVALIDDATA;
317 }
318
319 13 info_tag = bytestream2_peek_le32(&ctx->gbc);
320
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
321 int info_size;
322 13 bytestream2_skip(&ctx->gbc, 4);
323 13 info_size = bytestream2_get_le32(&ctx->gbc);
324
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(&ctx->gbc) < info_size) {
325 av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
326 return AVERROR_INVALIDDATA;
327 }
328 13 ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
329
330 13 bytestream2_skip(&ctx->gbc, info_size);
331 }
332
333 13 data_size = bytestream2_get_bytes_left(&ctx->gbc);
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (data_size < 4) {
335 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
336 return AVERROR_INVALIDDATA;
337 }
338
339 /* HQ defines dimensions and number of slices, and thus slice traversal
340 * order. HQA has no size constraint and a fixed number of slices, so it
341 * needs a separate scheme for it. */
342 13 tag = bytestream2_get_le32(&ctx->gbc);
343
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
13 if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
344 11 ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
345
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
346 2 ret = hqa_decode_frame(ctx, pic, data_size);
347 } else {
348 av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
349 return AVERROR_INVALIDDATA;
350 }
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0) {
352 av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
353 return ret;
354 }
355
356 13 pic->key_frame = 1;
357 13 pic->pict_type = AV_PICTURE_TYPE_I;
358
359 13 *got_frame = 1;
360
361 13 return avpkt->size;
362 }
363
364 6 static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
365 {
366 6 HQContext *ctx = avctx->priv_data;
367 6 ctx->avctx = avctx;
368
369 6 ff_hqdsp_init(&ctx->hqhqadsp);
370
371 6 return ff_hq_init_vlcs(ctx);
372 }
373
374 6 static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
375 {
376 6 HQContext *ctx = avctx->priv_data;
377
378 6 ff_free_vlc(&ctx->hq_ac_vlc);
379 6 ff_free_vlc(&ctx->hqa_cbp_vlc);
380
381 6 return 0;
382 }
383
384 const FFCodec ff_hq_hqa_decoder = {
385 .p.name = "hq_hqa",
386 .p.long_name = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
387 .p.type = AVMEDIA_TYPE_VIDEO,
388 .p.id = AV_CODEC_ID_HQ_HQA,
389 .priv_data_size = sizeof(HQContext),
390 .init = hq_hqa_decode_init,
391 FF_CODEC_DECODE_CB(hq_hqa_decode_frame),
392 .close = hq_hqa_decode_close,
393 .p.capabilities = AV_CODEC_CAP_DR1,
394 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
395 FF_CODEC_CAP_INIT_CLEANUP,
396 };
397