FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vqcdec.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 227 242 93.8%
Functions: 15 15 100.0%
Branches: 59 79 74.7%

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
151 1 memset(vectors, 0, 3 * width * height / 2);
152
153 1 init_get_bits8(&gb, buf, size);
154
155
1/2
✓ Branch 0 taken 3600 times.
✗ Branch 1 not taken.
3600 for (int i = 0; i < 3 * width * height / 2 / 32; i++) {
156 3600 uint8_t * dst = vectors;
157 int symbol;
158
159 3600 *dst++ = get_bits(&gb, 8);
160 3600 *dst++ = get_bits(&gb, 8);
161
162
2/2
✓ Branch 1 taken 26098 times.
✓ Branch 2 taken 3599 times.
29697 while (show_bits(&gb, 2) != 2) {
163
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26097 times.
26098 if (dst >= vectors_end - 1)
164 1 return 0;
165
166
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26097 times.
26097 if (get_bits_left(&gb) < 4)
167 return AVERROR_INVALIDDATA;
168
169
2/2
✓ Branch 1 taken 1668 times.
✓ Branch 2 taken 24429 times.
26097 if (!show_bits(&gb, 4)) {
170 1668 *dst++ = 0;
171 1668 *dst++ = 0;
172 1668 skip_bits(&gb, 4);
173 1668 continue;
174 }
175
176 24429 symbol = get_vlc2(&gb, vector_vlc, VECTOR_VLC_BITS, 1);
177
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) {
178 1151 case SKIP_3: dst += 3; break;
179 704 case SKIP_4: dst += 4; break;
180 502 case SKIP_5: dst += 5; break;
181 1293 case SKIP_6: dst += 6; break;
182 2 case SIGNED_8BIT: *dst++ = get_sbits(&gb, 8); break;
183 2141 case SIGNED_6BIT: *dst++ = get_sbits(&gb, 6); break;
184 18636 default:
185 18636 *dst++ = symbol;
186 }
187 }
188
189 3599 skip_bits(&gb, 2);
190 3599 vectors += 32;
191 }
192
193 return 0;
194 }
195
196 240 static void load_coeffs(VqcContext * s, const uint8_t * v, int width, int coeff_width)
197 {
198 240 int16_t * c0 = s->coeff;
199 240 int16_t * c1 = s->coeff + coeff_width;
200 240 int16_t * c0_125 = s->coeff + (coeff_width >> 3);
201 240 int16_t * c1_125 = s->coeff + coeff_width + (coeff_width >> 3);
202 240 int16_t * c0_25 = s->coeff + (coeff_width >> 2);
203 240 int16_t * c1_25 = s->coeff + coeff_width + (coeff_width >> 2);
204 240 int16_t * c0_5 = s->coeff + (coeff_width >> 1);
205 240 int16_t * c1_5 = s->coeff + coeff_width + (coeff_width >> 1);
206
207
2/2
✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 240 times.
3840 for (int i = 0; i < width; i++) {
208 3600 c0[0] = s->codebook[0][v[0]];
209 3600 c0[1] = s->codebook[0][v[1]];
210 3600 c0 += 2;
211
212 3600 c1[0] = s->codebook[0][v[2]];
213 3600 c1[1] = s->codebook[0][v[3]];
214 3600 c1 += 2;
215
216 3600 c0_125[0] = s->codebook[1][v[4]];
217 3600 c0_125[1] = s->codebook[1][v[5]];
218 3600 c0_125 += 2;
219
220 3600 c1_125[0] = s->codebook[1][v[6]];
221 3600 c1_125[1] = s->codebook[1][v[7]];
222 3600 c1_125 += 2;
223
224 3600 c0_25[0] = s->codebook[2][v[8]];
225 3600 c0_25[1] = s->codebook[2][v[9]];
226 3600 c0_25[2] = s->codebook[2][v[10]];
227 3600 c0_25[3] = s->codebook[2][v[11]];
228 3600 c0_25 += 4;
229
230 3600 c1_25[0] = s->codebook[2][v[12]];
231 3600 c1_25[1] = s->codebook[2][v[13]];
232 3600 c1_25[2] = s->codebook[2][v[14]];
233 3600 c1_25[3] = s->codebook[2][v[15]];
234 3600 c1_25 += 4;
235
236
2/2
✓ Branch 0 taken 589 times.
✓ Branch 1 taken 3011 times.
3600 if (v[16] | v[17] | v[18] | v[19]) {
237 589 c0_5[0] = s->codebook[3][v[16]];
238 589 c0_5[1] = s->codebook[3][v[17]];
239 589 c0_5[2] = s->codebook[3][v[18]];
240 589 c0_5[3] = s->codebook[3][v[19]];
241 } else {
242 3011 c0_5[0] = c0_5[1] = c0_5[2] = c0_5[3] = 0;
243 }
244
245
2/2
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 2934 times.
3600 if (v[20] | v[21] | v[22] | v[23]) {
246 666 c0_5[4] = s->codebook[3][v[20]];
247 666 c0_5[5] = s->codebook[3][v[21]];
248 666 c0_5[6] = s->codebook[3][v[22]];
249 666 c0_5[7] = s->codebook[3][v[23]];
250 } else {
251 2934 c0_5[4] = c0_5[5] = c0_5[6] = c0_5[7] = 0;
252 }
253 3600 c0_5 += 8;
254
255
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 3407 times.
3600 if (v[24] | v[25] | v[26] | v[27]) {
256 193 c1_5[0] = s->codebook[3][v[24]];
257 193 c1_5[1] = s->codebook[3][v[25]];
258 193 c1_5[2] = s->codebook[3][v[26]];
259 193 c1_5[3] = s->codebook[3][v[27]];
260 } else {
261 3407 c1_5[0] = c1_5[1] = c1_5[2] = c1_5[3] = 0;
262 }
263
264
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 3394 times.
3600 if (v[28] | v[29] | v[30] | v[31]) {
265 206 c1_5[4] = s->codebook[3][v[28]];
266 206 c1_5[5] = s->codebook[3][v[29]];
267 206 c1_5[6] = s->codebook[3][v[30]];
268 206 c1_5[7] = s->codebook[3][v[31]];
269 } else {
270 3394 c1_5[4] = c1_5[5] = c1_5[6] = c1_5[7] = 0;
271 }
272 3600 c1_5 += 8;
273
274 3600 v += 32;
275 }
276 240 }
277
278 960 static void transform1(const int16_t * a, const int16_t * b, int16_t * dst, int width)
279 {
280 960 int s0 = a[0] + (b[0] >> 1);
281
282
2/2
✓ Branch 0 taken 42240 times.
✓ Branch 1 taken 960 times.
43200 for (int i = 0; i < width / 2 - 1; i++) {
283 42240 dst[i * 2] = s0;
284 42240 s0 = a[i + 1] + ((b[i] + b[i + 1]) >> 1);
285 42240 dst[i * 2 + 1] = ((dst[i * 2] + s0) >> 1) - 2 * b[i];
286 }
287
288 960 dst[width - 2] = s0;
289 960 dst[width - 1] = a[width / 2 - 1] + ((b[width / 2 - 2] - 2 * b[width / 2 - 1]) >> 2) - b[width / 2 - 1];
290 960 }
291
292 960 static uint8_t clip(int x)
293 {
294
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;
295 }
296
297 480 static void transform2(const int16_t * a, const int16_t * b, uint8_t * dst, int width)
298 {
299 480 int s0 = a[0] + (b[0] >> 1);
300 int tmp;
301
302
2/2
✓ Branch 0 taken 57120 times.
✓ Branch 1 taken 480 times.
57600 for (int i = 0; i < width / 2 - 1; i++) {
303 57120 dst[i * 2] = av_clip_uint8(s0 + 0x80);
304 57120 tmp = a[i + 1] + ((b[i] + b[i + 1]) >> 1);
305 57120 dst[i * 2 + 1] = av_clip_uint8(((tmp + s0) >> 1) - 2 * b[i] + 0x80);
306 57120 s0 = tmp;
307 }
308
309 480 dst[width - 2] = clip(s0);
310 480 dst[width - 1] = clip(a[width / 2 - 1] + ((b[width / 2 - 2] - 2 * b[width / 2 - 1]) >> 2) - b[width / 2 - 1]);
311 480 }
312
313 240 static void decode_strip(VqcContext * s, uint8_t * dst, int stride, int width)
314 {
315 const int16_t * coeff;
316
317
2/2
✓ Branch 0 taken 57600 times.
✓ Branch 1 taken 240 times.
57840 for (int i = 0; i < width; i++) {
318 57600 int v0 = s->coeff[i];
319 57600 int v1 = s->coeff[width + i];
320 57600 s->coeff[i] = v0 - v1;
321 57600 s->coeff[width + i] = v0 + v1;
322 }
323
324 240 coeff = s->coeff;
325
326 240 transform1(coeff, coeff + width / 8, s->tmp1, width / 4);
327 240 transform1(s->tmp1, coeff + width / 4, s->tmp2, width / 2);
328 240 transform2(s->tmp2, coeff + width / 2, dst, width);
329
330 240 coeff += width;
331 240 dst += stride;
332
333 240 transform1(coeff, coeff + width / 8, s->tmp1, width / 4);
334 240 transform1(s->tmp1, coeff + width / 4, s->tmp2, width / 2);
335 240 transform2(s->tmp2, coeff + width / 2, dst, width);
336 240 }
337
338 1 static void decode_frame(VqcContext * s, int width, int height)
339 {
340 1 uint8_t * vectors = s->vectors;
341 1 uint8_t * y = s->frame->data[0];
342 1 uint8_t * u = s->frame->data[1];
343 1 uint8_t * v = s->frame->data[2];
344
345
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1 times.
61 for (int j = 0; j < height / 4; j++) {
346 60 load_coeffs(s, vectors, width / 16, width);
347 60 decode_strip(s, y, s->frame->linesize[0], width);
348 60 vectors += 2 * width;
349 60 y += 2 * s->frame->linesize[0];
350
351 60 load_coeffs(s, vectors, width / 32, width / 2);
352 60 decode_strip(s, u, s->frame->linesize[1], width / 2);
353 60 vectors += width;
354 60 u += 2 * s->frame->linesize[1];
355
356 60 load_coeffs(s, vectors, width / 16, width);
357 60 decode_strip(s, y, s->frame->linesize[0], width);
358 60 vectors += 2 * width;
359 60 y += 2 * s->frame->linesize[0];
360
361 60 load_coeffs(s, vectors, width / 32, width / 2);
362 60 decode_strip(s, v, s->frame->linesize[2], width / 2);
363 60 vectors += width;
364 60 v += 2 * s->frame->linesize[2];
365 }
366 1 }
367
368 1 static int vqc_decode_frame(AVCodecContext *avctx, AVFrame * rframe,
369 int * got_frame, AVPacket * avpkt)
370 {
371 1 VqcContext *s = avctx->priv_data;
372 int ret;
373 1 const uint8_t * buf = avpkt->data;
374 int cache, seed[7], gamma, contrast;
375
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avpkt->size < 7)
377 return AVERROR_INVALIDDATA;
378
379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
380 return ret;
381
382 1 av_log(avctx, AV_LOG_DEBUG, "VQC%d format\n", (buf[2] & 1) + 1);
383
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (((buf[0] >> 1) & 7) != 5) {
385 avpriv_request_sample(avctx, "subversion != 5\n");
386 return AVERROR_PATCHWELCOME;
387 }
388
389 1 cache = AV_RL24(buf + 4);
390 1 seed[2] = seed_pow1((cache >> 1) & 7);
391 1 seed[1] = seed_pow1((cache >> 4) & 7);
392 1 seed[0] = seed_pow1((cache >> 7) & 7);
393 1 seed[6] = seed_pow2((cache >> 10) & 7);
394 1 seed[5] = seed_pow2((cache >> 13) & 7);
395 1 seed[4] = seed_pow2((cache >> 16) & 7);
396 1 seed[3] = seed_pow2((cache >> 19) & 7);
397
398 1 gamma = buf[0] >> 4;
399 1 contrast = AV_RL16(buf + 2) >> 1;
400
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (gamma || contrast)
401 avpriv_request_sample(avctx, "gamma=0x%x, contrast=0x%x\n", gamma, contrast);
402
403 1 seed_codebooks(s, seed);
404 1 ret = decode_vectors(s, buf + 7, avpkt->size - 7, avctx->width, avctx->height);
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
406 return ret;
407 1 decode_frame(s, avctx->width, avctx->height);
408
409
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
410 return ret;
411
412 1 *got_frame = 1;
413
414 1 return avpkt->size;
415 }
416
417 2 static av_cold int vqc_decode_end(AVCodecContext * avctx)
418 {
419 2 VqcContext *s = avctx->priv_data;
420
421 2 av_freep(&s->vectors);
422 2 av_freep(&s->coeff);
423 2 av_freep(&s->tmp1);
424 2 av_freep(&s->tmp2);
425 2 av_frame_free(&s->frame);
426
427 2 return 0;
428 }
429
430 const FFCodec ff_vqc_decoder = {
431 .p.name = "vqc",
432 CODEC_LONG_NAME("ViewQuest VQC"),
433 .p.type = AVMEDIA_TYPE_VIDEO,
434 .p.id = AV_CODEC_ID_VQC,
435 .priv_data_size = sizeof(VqcContext),
436 .init = vqc_decode_init,
437 .close = vqc_decode_end,
438 FF_CODEC_DECODE_CB(vqc_decode_frame),
439 .p.capabilities = AV_CODEC_CAP_DR1,
440 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
441 };
442