FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vqcdec.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 228 244 93.4%
Functions: 15 15 100.0%
Branches: 60 81 74.1%

Line Branch Exec Source
1 /*
2 * ViewQuest VQC decoder
3 * Copyright (C) 2022 Peter Ross
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 #include "avcodec.h"
23 #include "get_bits.h"
24 #include "codec_internal.h"
25 #include "decode.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/thread.h"
28
29 #define VECTOR_VLC_BITS 6
30
31 static const uint8_t vector_nbits[] = {
32 2, 4, 4, 4, 4, 2, 4, 4,
33 6, 6, 6, 6, 6, 6, 6, 6
34 };
35
36 enum {
37 SKIP_3 = 0x10,
38 SKIP_4,
39 SKIP_5,
40 SKIP_6,
41 STOP_RUN,
42 SIGNED_8BIT,
43 SIGNED_6BIT
44 };
45
46 /* vector symbols are signed, but returned unsigned by get_vlc2()
47 codebook indexes are cast as uint8_t in seed_codebook() to compensate */
48 static const int8_t vector_symbols[] = {
49 0, SKIP_3, SKIP_4, SKIP_5, SKIP_6, STOP_RUN, 1, -1,
50 2, 3, 4, SIGNED_8BIT, -2, -3, -4, SIGNED_6BIT
51 };
52
53 static VLCElem vector_vlc[1 << VECTOR_VLC_BITS];
54
55 1 static av_cold void vqc_init_static_data(void)
56 {
57 1 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vector_vlc, VECTOR_VLC_BITS,
58 FF_ARRAY_ELEMS(vector_nbits),
59 vector_nbits, 1,
60 vector_symbols, 1, 1,
61 0, 0);
62 1 }
63
64 typedef struct VqcContext {
65 AVFrame *frame;
66 uint8_t * vectors;
67 int16_t * coeff, *tmp1, *tmp2;
68 int16_t codebook[4][256];
69 } VqcContext;
70
71 2 static av_cold int vqc_decode_init(AVCodecContext * avctx)
72 {
73 static AVOnce init_static_once = AV_ONCE_INIT;
74 2 VqcContext *s = avctx->priv_data;
75
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->width & 15)
77 return AVERROR_PATCHWELCOME;
78
79 2 s->vectors = av_malloc((avctx->width * avctx->height * 3) / 2);
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->vectors)
81 return AVERROR(ENOMEM);
82
83 2 s->coeff = av_malloc_array(2 * avctx->width, sizeof(s->coeff[0]));
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->coeff)
85 return AVERROR(ENOMEM);
86
87 2 s->tmp1 = av_malloc_array(avctx->width / 2, sizeof(s->tmp1[0]));
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->tmp1)
89 return AVERROR(ENOMEM);
90
91 2 s->tmp2 = av_malloc_array(avctx->width / 2, sizeof(s->tmp2[0]));
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->tmp2)
93 return AVERROR(ENOMEM);
94
95 2 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
96 2 s->frame = av_frame_alloc();
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->frame)
98 return AVERROR(ENOMEM);
99
100 2 ff_thread_once(&init_static_once, vqc_init_static_data);
101
102 2 return 0;
103 }
104
105 3 static int seed_pow1(int x)
106 {
107
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 return x >= 1 && x <= 5 ? 1 << x : 0;
108 }
109
110 4 static int seed_pow2(int x)
111 {
112
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
4 return x >= 1 && x <= 4 ? 1 << x : 1;
113 }
114
115 768 static int bias(int x, int c)
116 {
117
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 384 times.
768 if (x < 0)
118 384 return x - c;
119
2/2
✓ Branch 0 taken 381 times.
✓ Branch 1 taken 3 times.
384 else if (x > 0)
120 381 return x + c;
121 else
122 3 return 0;
123 }
124
125 1 static void seed_codebooks(VqcContext * s, const int * seed)
126 {
127 1 int book1 = -256 * seed[3];
128 1 int book2 = -128 * seed[4];
129 1 int book3 = -128 * seed[5];
130 1 int book4 = -128 * seed[6];
131
132
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 1 times.
257 for (int i = -128; i < 128; i++) {
133 256 s->codebook[0][(uint8_t)i] = book1;
134 256 s->codebook[1][(uint8_t)i] = bias(book2, seed[0]);
135 256 s->codebook[2][(uint8_t)i] = bias(book3, seed[1]);
136 256 s->codebook[3][(uint8_t)i] = bias(book4, seed[2]);
137
138 256 book1 += 2 * seed[3];
139 256 book2 += seed[4];
140 256 book3 += seed[5];
141 256 book4 += seed[6];
142 }
143 1 }
144
145 1 static int decode_vectors(VqcContext * s, const uint8_t * buf, int size, int width, int height)
146 {
147 GetBitContext gb;
148 1 uint8_t * vectors = s->vectors;
149 1 uint8_t * vectors_end = s->vectors + (width * height * 3) / 2;
150 int ret;
151
152 1 memset(vectors, 0, 3 * width * height / 2);
153
154 1 ret = init_get_bits8(&gb, buf, size);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
156 return ret;
157
158
1/2
✓ Branch 0 taken 3600 times.
✗ Branch 1 not taken.
3600 for (int i = 0; i < 3 * width * height / 2 / 32; i++) {
159 3600 uint8_t * dst = vectors;
160 int symbol;
161
162 3600 *dst++ = get_bits(&gb, 8);
163 3600 *dst++ = get_bits(&gb, 8);
164
165
2/2
✓ Branch 1 taken 26098 times.
✓ Branch 2 taken 3599 times.
29697 while (show_bits(&gb, 2) != 2) {
166
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26097 times.
26098 if (dst >= vectors_end - 1)
167 1 return 0;
168
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26097 times.
26097 if (get_bits_left(&gb) < 4)
170 return AVERROR_INVALIDDATA;
171
172
2/2
✓ Branch 1 taken 1668 times.
✓ Branch 2 taken 24429 times.
26097 if (!show_bits(&gb, 4)) {
173 1668 *dst++ = 0;
174 1668 *dst++ = 0;
175 1668 skip_bits(&gb, 4);
176 1668 continue;
177 }
178
179 24429 symbol = get_vlc2(&gb, vector_vlc, VECTOR_VLC_BITS, 1);
180
7/7
✓ Branch 0 taken 1151 times.
✓ Branch 1 taken 704 times.
✓ Branch 2 taken 502 times.
✓ Branch 3 taken 1293 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2141 times.
✓ Branch 6 taken 18636 times.
24429 switch(symbol) {
181 1151 case SKIP_3: dst += 3; break;
182 704 case SKIP_4: dst += 4; break;
183 502 case SKIP_5: dst += 5; break;
184 1293 case SKIP_6: dst += 6; break;
185 2 case SIGNED_8BIT: *dst++ = get_sbits(&gb, 8); break;
186 2141 case SIGNED_6BIT: *dst++ = get_sbits(&gb, 6); break;
187 18636 default:
188 18636 *dst++ = symbol;
189 }
190 }
191
192 3599 skip_bits(&gb, 2);
193 3599 vectors += 32;
194 }
195
196 return 0;
197 }
198
199 240 static void load_coeffs(VqcContext * s, const uint8_t * v, int width, int coeff_width)
200 {
201 240 int16_t * c0 = s->coeff;
202 240 int16_t * c1 = s->coeff + coeff_width;
203 240 int16_t * c0_125 = s->coeff + (coeff_width >> 3);
204 240 int16_t * c1_125 = s->coeff + coeff_width + (coeff_width >> 3);
205 240 int16_t * c0_25 = s->coeff + (coeff_width >> 2);
206 240 int16_t * c1_25 = s->coeff + coeff_width + (coeff_width >> 2);
207 240 int16_t * c0_5 = s->coeff + (coeff_width >> 1);
208 240 int16_t * c1_5 = s->coeff + coeff_width + (coeff_width >> 1);
209
210
2/2
✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 240 times.
3840 for (int i = 0; i < width; i++) {
211 3600 c0[0] = s->codebook[0][v[0]];
212 3600 c0[1] = s->codebook[0][v[1]];
213 3600 c0 += 2;
214
215 3600 c1[0] = s->codebook[0][v[2]];
216 3600 c1[1] = s->codebook[0][v[3]];
217 3600 c1 += 2;
218
219 3600 c0_125[0] = s->codebook[1][v[4]];
220 3600 c0_125[1] = s->codebook[1][v[5]];
221 3600 c0_125 += 2;
222
223 3600 c1_125[0] = s->codebook[1][v[6]];
224 3600 c1_125[1] = s->codebook[1][v[7]];
225 3600 c1_125 += 2;
226
227 3600 c0_25[0] = s->codebook[2][v[8]];
228 3600 c0_25[1] = s->codebook[2][v[9]];
229 3600 c0_25[2] = s->codebook[2][v[10]];
230 3600 c0_25[3] = s->codebook[2][v[11]];
231 3600 c0_25 += 4;
232
233 3600 c1_25[0] = s->codebook[2][v[12]];
234 3600 c1_25[1] = s->codebook[2][v[13]];
235 3600 c1_25[2] = s->codebook[2][v[14]];
236 3600 c1_25[3] = s->codebook[2][v[15]];
237 3600 c1_25 += 4;
238
239
2/2
✓ Branch 0 taken 589 times.
✓ Branch 1 taken 3011 times.
3600 if (v[16] | v[17] | v[18] | v[19]) {
240 589 c0_5[0] = s->codebook[3][v[16]];
241 589 c0_5[1] = s->codebook[3][v[17]];
242 589 c0_5[2] = s->codebook[3][v[18]];
243 589 c0_5[3] = s->codebook[3][v[19]];
244 } else {
245 3011 c0_5[0] = c0_5[1] = c0_5[2] = c0_5[3] = 0;
246 }
247
248
2/2
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 2934 times.
3600 if (v[20] | v[21] | v[22] | v[23]) {
249 666 c0_5[4] = s->codebook[3][v[20]];
250 666 c0_5[5] = s->codebook[3][v[21]];
251 666 c0_5[6] = s->codebook[3][v[22]];
252 666 c0_5[7] = s->codebook[3][v[23]];
253 } else {
254 2934 c0_5[4] = c0_5[5] = c0_5[6] = c0_5[7] = 0;
255 }
256 3600 c0_5 += 8;
257
258
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 3407 times.
3600 if (v[24] | v[25] | v[26] | v[27]) {
259 193 c1_5[0] = s->codebook[3][v[24]];
260 193 c1_5[1] = s->codebook[3][v[25]];
261 193 c1_5[2] = s->codebook[3][v[26]];
262 193 c1_5[3] = s->codebook[3][v[27]];
263 } else {
264 3407 c1_5[0] = c1_5[1] = c1_5[2] = c1_5[3] = 0;
265 }
266
267
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 3394 times.
3600 if (v[28] | v[29] | v[30] | v[31]) {
268 206 c1_5[4] = s->codebook[3][v[28]];
269 206 c1_5[5] = s->codebook[3][v[29]];
270 206 c1_5[6] = s->codebook[3][v[30]];
271 206 c1_5[7] = s->codebook[3][v[31]];
272 } else {
273 3394 c1_5[4] = c1_5[5] = c1_5[6] = c1_5[7] = 0;
274 }
275 3600 c1_5 += 8;
276
277 3600 v += 32;
278 }
279 240 }
280
281 960 static void transform1(const int16_t * a, const int16_t * b, int16_t * dst, int width)
282 {
283 960 int s0 = a[0] + (b[0] >> 1);
284
285
2/2
✓ Branch 0 taken 42240 times.
✓ Branch 1 taken 960 times.
43200 for (int i = 0; i < width / 2 - 1; i++) {
286 42240 dst[i * 2] = s0;
287 42240 s0 = a[i + 1] + ((b[i] + b[i + 1]) >> 1);
288 42240 dst[i * 2 + 1] = ((dst[i * 2] + s0) >> 1) - 2 * b[i];
289 }
290
291 960 dst[width - 2] = s0;
292 960 dst[width - 1] = a[width / 2 - 1] + ((b[width / 2 - 2] - 2 * b[width / 2 - 1]) >> 2) - b[width / 2 - 1];
293 960 }
294
295 960 static uint8_t clip(int x)
296 {
297
2/4
✓ Branch 0 taken 960 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 960 times.
✗ Branch 3 not taken.
960 return x >= -128 ? x <= 127 ? x + 0x80 : 0x00 : 0xFF;
298 }
299
300 480 static void transform2(const int16_t * a, const int16_t * b, uint8_t * dst, int width)
301 {
302 480 int s0 = a[0] + (b[0] >> 1);
303 int tmp;
304
305
2/2
✓ Branch 0 taken 57120 times.
✓ Branch 1 taken 480 times.
57600 for (int i = 0; i < width / 2 - 1; i++) {
306 57120 dst[i * 2] = av_clip_uint8(s0 + 0x80);
307 57120 tmp = a[i + 1] + ((b[i] + b[i + 1]) >> 1);
308 57120 dst[i * 2 + 1] = av_clip_uint8(((tmp + s0) >> 1) - 2 * b[i] + 0x80);
309 57120 s0 = tmp;
310 }
311
312 480 dst[width - 2] = clip(s0);
313 480 dst[width - 1] = clip(a[width / 2 - 1] + ((b[width / 2 - 2] - 2 * b[width / 2 - 1]) >> 2) - b[width / 2 - 1]);
314 480 }
315
316 240 static void decode_strip(VqcContext * s, uint8_t * dst, int stride, int width)
317 {
318 const int16_t * coeff;
319
320
2/2
✓ Branch 0 taken 57600 times.
✓ Branch 1 taken 240 times.
57840 for (int i = 0; i < width; i++) {
321 57600 int v0 = s->coeff[i];
322 57600 int v1 = s->coeff[width + i];
323 57600 s->coeff[i] = v0 - v1;
324 57600 s->coeff[width + i] = v0 + v1;
325 }
326
327 240 coeff = s->coeff;
328
329 240 transform1(coeff, coeff + width / 8, s->tmp1, width / 4);
330 240 transform1(s->tmp1, coeff + width / 4, s->tmp2, width / 2);
331 240 transform2(s->tmp2, coeff + width / 2, dst, width);
332
333 240 coeff += width;
334 240 dst += stride;
335
336 240 transform1(coeff, coeff + width / 8, s->tmp1, width / 4);
337 240 transform1(s->tmp1, coeff + width / 4, s->tmp2, width / 2);
338 240 transform2(s->tmp2, coeff + width / 2, dst, width);
339 240 }
340
341 1 static void decode_frame(VqcContext * s, int width, int height)
342 {
343 1 uint8_t * vectors = s->vectors;
344 1 uint8_t * y = s->frame->data[0];
345 1 uint8_t * u = s->frame->data[1];
346 1 uint8_t * v = s->frame->data[2];
347
348
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1 times.
61 for (int j = 0; j < height / 4; j++) {
349 60 load_coeffs(s, vectors, width / 16, width);
350 60 decode_strip(s, y, s->frame->linesize[0], width);
351 60 vectors += 2 * width;
352 60 y += 2 * s->frame->linesize[0];
353
354 60 load_coeffs(s, vectors, width / 32, width / 2);
355 60 decode_strip(s, u, s->frame->linesize[1], width / 2);
356 60 vectors += width;
357 60 u += 2 * s->frame->linesize[1];
358
359 60 load_coeffs(s, vectors, width / 16, width);
360 60 decode_strip(s, y, s->frame->linesize[0], width);
361 60 vectors += 2 * width;
362 60 y += 2 * s->frame->linesize[0];
363
364 60 load_coeffs(s, vectors, width / 32, width / 2);
365 60 decode_strip(s, v, s->frame->linesize[2], width / 2);
366 60 vectors += width;
367 60 v += 2 * s->frame->linesize[2];
368 }
369 1 }
370
371 1 static int vqc_decode_frame(AVCodecContext *avctx, AVFrame * rframe,
372 int * got_frame, AVPacket * avpkt)
373 {
374 1 VqcContext *s = avctx->priv_data;
375 int ret;
376 1 const uint8_t * buf = avpkt->data;
377 int cache, seed[7], gamma, contrast;
378
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avpkt->size < 7)
380 return AVERROR_INVALIDDATA;
381
382
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
383 return ret;
384
385 1 av_log(avctx, AV_LOG_DEBUG, "VQC%d format\n", (buf[2] & 1) + 1);
386
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (((buf[0] >> 1) & 7) != 5) {
388 avpriv_request_sample(avctx, "subversion != 5\n");
389 return AVERROR_PATCHWELCOME;
390 }
391
392 1 cache = AV_RL24(buf + 4);
393 1 seed[2] = seed_pow1((cache >> 1) & 7);
394 1 seed[1] = seed_pow1((cache >> 4) & 7);
395 1 seed[0] = seed_pow1((cache >> 7) & 7);
396 1 seed[6] = seed_pow2((cache >> 10) & 7);
397 1 seed[5] = seed_pow2((cache >> 13) & 7);
398 1 seed[4] = seed_pow2((cache >> 16) & 7);
399 1 seed[3] = seed_pow2((cache >> 19) & 7);
400
401 1 gamma = buf[0] >> 4;
402 1 contrast = AV_RL16(buf + 2) >> 1;
403
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (gamma || contrast)
404 avpriv_request_sample(avctx, "gamma=0x%x, contrast=0x%x\n", gamma, contrast);
405
406 1 seed_codebooks(s, seed);
407 1 ret = decode_vectors(s, buf + 7, avpkt->size - 7, avctx->width, avctx->height);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
409 return ret;
410 1 decode_frame(s, avctx->width, avctx->height);
411
412
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
413 return ret;
414
415 1 *got_frame = 1;
416
417 1 return avpkt->size;
418 }
419
420 2 static av_cold int vqc_decode_end(AVCodecContext * avctx)
421 {
422 2 VqcContext *s = avctx->priv_data;
423
424 2 av_freep(&s->vectors);
425 2 av_freep(&s->coeff);
426 2 av_freep(&s->tmp1);
427 2 av_freep(&s->tmp2);
428 2 av_frame_free(&s->frame);
429
430 2 return 0;
431 }
432
433 const FFCodec ff_vqc_decoder = {
434 .p.name = "vqc",
435 CODEC_LONG_NAME("ViewQuest VQC"),
436 .p.type = AVMEDIA_TYPE_VIDEO,
437 .p.id = AV_CODEC_ID_VQC,
438 .priv_data_size = sizeof(VqcContext),
439 .init = vqc_decode_init,
440 .close = vqc_decode_end,
441 FF_CODEC_DECODE_CB(vqc_decode_frame),
442 .p.capabilities = AV_CODEC_CAP_DR1,
443 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
444 };
445