FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/gdv.c
Date: 2026-09-24 20:08:24
Exec Total Coverage
Lines: 0 329 0.0%
Functions: 0 14 0.0%
Branches: 0 178 0.0%

Line Branch Exec Source
1 /*
2 * Gremlin Digital Video (GDV) decoder
3 * Copyright (c) 2017 Konstantin Shishkov
4 * Copyright (c) 2017 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "libavutil/attributes.h"
24 #include "libavutil/common.h"
25 #include "libavutil/mem.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "codec_internal.h"
29 #include "decode.h"
30
31 typedef struct GDVContext {
32 AVCodecContext *avctx;
33
34 GetByteContext gb;
35 GetByteContext g2;
36 PutByteContext pb;
37
38 uint32_t pal[256];
39 uint8_t *frame;
40 unsigned frame_size;
41 unsigned scale_h, scale_v;
42 } GDVContext;
43
44 typedef struct Bits8 {
45 uint8_t queue;
46 uint8_t fill;
47 } Bits8;
48
49 typedef struct Bits32 {
50 uint32_t queue;
51 uint8_t fill;
52 } Bits32;
53
54 #define PREAMBLE_SIZE 4096
55
56 ✗ static av_cold int gdv_decode_init(AVCodecContext *avctx)
57 {
58 ✗ GDVContext *gdv = avctx->priv_data;
59 int i, j, k;
60
61 ✗ avctx->pix_fmt = AV_PIX_FMT_PAL8;
62 ✗ gdv->frame_size = avctx->width * avctx->height + PREAMBLE_SIZE;
63 ✗ gdv->frame = av_calloc(gdv->frame_size, 1);
64 ✗ if (!gdv->frame)
65 ✗ return AVERROR(ENOMEM);
66
67 ✗ for (i = 0; i < 2; i++) {
68 ✗ for (j = 0; j < 256; j++) {
69 ✗ for (k = 0; k < 8; k++) {
70 ✗ gdv->frame[i * 2048 + j * 8 + k] = j;
71 }
72 }
73 }
74
75 ✗ return 0;
76 }
77
78 ✗ static void scaleup(uint8_t *dst, const uint8_t *src, int w)
79 {
80 int x;
81 ✗ for (x = 0; x < w - 7; x+=8) {
82 ✗ dst[x + 0] =
83 ✗ dst[x + 1] = src[(x>>1) + 0];
84 ✗ dst[x + 2] =
85 ✗ dst[x + 3] = src[(x>>1) + 1];
86 ✗ dst[x + 4] =
87 ✗ dst[x + 5] = src[(x>>1) + 2];
88 ✗ dst[x + 6] =
89 ✗ dst[x + 7] = src[(x>>1) + 3];
90 }
91 ✗ for (; x < w; x++) {
92 ✗ dst[x] = src[(x>>1)];
93 }
94 ✗ }
95
96 ✗ static void scaleup_rev(uint8_t *dst, const uint8_t *src, int w)
97 {
98 int x;
99
100 ✗ for (x = w - 1; (x+1) & 7; x--) {
101 ✗ dst[x] = src[(x>>1)];
102 }
103 ✗ for (x -= 7; x >= 0; x -= 8) {
104 ✗ dst[x + 6] =
105 ✗ dst[x + 7] = src[(x>>1) + 3];
106 ✗ dst[x + 4] =
107 ✗ dst[x + 5] = src[(x>>1) + 2];
108 ✗ dst[x + 2] =
109 ✗ dst[x + 3] = src[(x>>1) + 1];
110 ✗ dst[x + 0] =
111 ✗ dst[x + 1] = src[(x>>1) + 0];
112 }
113 ✗ }
114
115 ✗ static void scaledown(uint8_t *dst, const uint8_t *src, int w)
116 {
117 int x;
118 ✗ for (x = 0; x < w - 7; x+=8) {
119 ✗ dst[x + 0] = src[2*x + 0];
120 ✗ dst[x + 1] = src[2*x + 2];
121 ✗ dst[x + 2] = src[2*x + 4];
122 ✗ dst[x + 3] = src[2*x + 6];
123 ✗ dst[x + 4] = src[2*x + 8];
124 ✗ dst[x + 5] = src[2*x +10];
125 ✗ dst[x + 6] = src[2*x +12];
126 ✗ dst[x + 7] = src[2*x +14];
127 }
128 ✗ for (; x < w; x++) {
129 ✗ dst[x] = src[2*x];
130 }
131 ✗ }
132
133 ✗ static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
134 {
135 int j, y;
136
137 ✗ if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
138 ✗ return;
139 }
140
141 ✗ if (gdv->scale_v) {
142 ✗ for (j = 0; j < h; j++) {
143 ✗ int y = h - j - 1;
144 ✗ uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
145 ✗ uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>!!gdv->scale_h) * (w>>1);
146
147 ✗ scaleup_rev(dst1, src1, w);
148 }
149 ✗ } else if (gdv->scale_h) {
150 ✗ for (j = 0; j < h; j++) {
151 ✗ int y = h - j - 1;
152 ✗ uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
153 ✗ uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * w;
154 ✗ memcpy(dst1, src1, w);
155 }
156 }
157
158 ✗ if (scale_h && scale_v) {
159 ✗ for (y = 0; y < (h>>1); y++) {
160 ✗ uint8_t *dst1 = dst + PREAMBLE_SIZE + y * (w>>1);
161 ✗ uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
162 ✗ scaledown(dst1, src1, w>>1);
163 }
164 ✗ } else if (scale_h) {
165 ✗ for (y = 0; y < (h>>1); y++) {
166 ✗ uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
167 ✗ uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
168 ✗ memcpy(dst1, src1, w);
169 }
170 ✗ } else if (scale_v) {
171 ✗ for (y = 0; y < h; y++) {
172 ✗ uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
173 ✗ scaledown(dst1, dst1, w>>1);
174 }
175 }
176
177 ✗ gdv->scale_v = scale_v;
178 ✗ gdv->scale_h = scale_h;
179 }
180
181 ✗ static int read_bits2(Bits8 *bits, GetByteContext *gb)
182 {
183 int res;
184
185 ✗ if (bits->fill == 0) {
186 ✗ bits->queue |= bytestream2_get_byte(gb);
187 ✗ bits->fill = 8;
188 }
189 ✗ res = bits->queue >> 6;
190 ✗ bits->queue <<= 2;
191 ✗ bits->fill -= 2;
192
193 ✗ return res;
194 }
195
196 ✗ static void fill_bits32(Bits32 *bits, GetByteContext *gb)
197 {
198 ✗ bits->queue = bytestream2_get_le32(gb);
199 ✗ bits->fill = 32;
200 ✗ }
201
202 ✗ static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
203 {
204 ✗ int res = bits->queue & ((1 << nbits) - 1);
205
206 ✗ bits->queue >>= nbits;
207 ✗ bits->fill -= nbits;
208 ✗ if (bits->fill <= 16) {
209 ✗ bits->queue |= bytestream2_get_le16(gb) << bits->fill;
210 ✗ bits->fill += 16;
211 }
212
213 ✗ return res;
214 }
215
216 ✗ static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
217 {
218 int i;
219
220 ✗ if (offset == -1) {
221 int c;
222
223 ✗ bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
224 ✗ c = bytestream2_get_byte(g2);
225 ✗ for (i = 0; i < len; i++) {
226 ✗ bytestream2_put_byte(pb, c);
227 }
228 ✗ } else if (offset < 0) {
229 ✗ int start = bytestream2_tell_p(pb) - (-offset);
230
231 ✗ bytestream2_seek(g2, start, SEEK_SET);
232 ✗ for (i = 0; i < len; i++) {
233 ✗ bytestream2_put_byte(pb, bytestream2_get_byte(g2));
234 }
235 } else {
236 ✗ int start = bytestream2_tell_p(pb) + offset;
237
238 ✗ bytestream2_seek(g2, start, SEEK_SET);
239 ✗ for (i = 0; i < len; i++) {
240 ✗ bytestream2_put_byte(pb, bytestream2_get_byte(g2));
241 }
242 }
243 ✗ }
244
245 ✗ static int decompress_2(AVCodecContext *avctx)
246 {
247 ✗ GDVContext *gdv = avctx->priv_data;
248 ✗ GetByteContext *gb = &gdv->gb;
249 ✗ GetByteContext *g2 = &gdv->g2;
250 ✗ PutByteContext *pb = &gdv->pb;
251 ✗ Bits8 bits = { 0 };
252 int c, i;
253
254 ✗ bytestream2_init(g2, gdv->frame, gdv->frame_size);
255 ✗ bytestream2_skip_p(pb, PREAMBLE_SIZE);
256
257 ✗ for (c = 0; c < 256; c++) {
258 ✗ for (i = 0; i < 16; i++) {
259 ✗ gdv->frame[c * 16 + i] = c;
260 }
261 }
262
263 ✗ while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
264 ✗ int tag = read_bits2(&bits, gb);
265 ✗ if (tag == 0) {
266 ✗ bytestream2_put_byte(pb, bytestream2_get_byte(gb));
267 ✗ } else if (tag == 1) {
268 ✗ int b = bytestream2_get_byte(gb);
269 ✗ int len = (b & 0xF) + 3;
270 ✗ int top = (b >> 4) & 0xF;
271 ✗ int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
272 ✗ lz_copy(pb, g2, off, len);
273 ✗ } else if (tag == 2) {
274 ✗ int len = (bytestream2_get_byte(gb)) + 2;
275 ✗ bytestream2_skip_p(pb, len);
276 } else {
277 ✗ break;
278 }
279 }
280
281 ✗ if (bytestream2_get_bytes_left_p(pb) > 0)
282 ✗ return AVERROR_INVALIDDATA;
283
284 ✗ return 0;
285 }
286
287 ✗ static int decompress_5(AVCodecContext *avctx, unsigned skip)
288 {
289 ✗ GDVContext *gdv = avctx->priv_data;
290 ✗ GetByteContext *gb = &gdv->gb;
291 ✗ GetByteContext *g2 = &gdv->g2;
292 ✗ PutByteContext *pb = &gdv->pb;
293 ✗ Bits8 bits = { 0 };
294
295 ✗ bytestream2_init(g2, gdv->frame, gdv->frame_size);
296 ✗ bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
297
298 ✗ while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
299 ✗ int tag = read_bits2(&bits, gb);
300 ✗ if (bytestream2_get_bytes_left(gb) < 1)
301 ✗ return AVERROR_INVALIDDATA;
302 ✗ if (tag == 0) {
303 ✗ bytestream2_put_byte(pb, bytestream2_get_byte(gb));
304 ✗ } else if (tag == 1) {
305 ✗ int b = bytestream2_get_byte(gb);
306 ✗ int len = (b & 0xF) + 3;
307 ✗ int top = b >> 4;
308 ✗ int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
309 ✗ lz_copy(pb, g2, off, len);
310 ✗ } else if (tag == 2) {
311 int len;
312 ✗ int b = bytestream2_get_byte(gb);
313 ✗ if (b == 0) {
314 ✗ return 0;
315 }
316 ✗ if (b != 0xFF) {
317 ✗ len = b;
318 } else {
319 ✗ len = bytestream2_get_le16(gb);
320 }
321 ✗ bytestream2_skip_p(pb, len + 1);
322 } else {
323 ✗ int b = bytestream2_get_byte(gb);
324 ✗ int len = (b & 0x3) + 2;
325 ✗ int off = -(b >> 2) - 1;
326 ✗ lz_copy(pb, g2, off, len);
327 }
328 }
329 ✗ if (bytestream2_get_bytes_left_p(pb) > 0)
330 ✗ return AVERROR_INVALIDDATA;
331 ✗ return 0;
332 }
333
334 ✗ static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
335 {
336 ✗ GDVContext *gdv = avctx->priv_data;
337 ✗ GetByteContext *gb = &gdv->gb;
338 ✗ GetByteContext *g2 = &gdv->g2;
339 ✗ PutByteContext *pb = &gdv->pb;
340 Bits32 bits;
341
342 ✗ bytestream2_init(g2, gdv->frame, gdv->frame_size);
343 ✗ bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
344 ✗ fill_bits32(&bits, gb);
345
346 ✗ while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
347 ✗ int tag = read_bits32(&bits, gb, 2);
348 ✗ if (tag == 0) {
349 ✗ int b = read_bits32(&bits, gb, 1);
350 ✗ if (b == 0) {
351 ✗ bytestream2_put_byte(pb, bytestream2_get_byte(gb));
352 } else {
353 ✗ int i, len = 2;
354 ✗ int lbits = 0;
355 ✗ while (1) {
356 int val;
357
358 ✗ lbits += 1;
359 ✗ val = read_bits32(&bits, gb, lbits);
360 ✗ len += val;
361 ✗ if (val != ((1 << lbits) - 1)) {
362 ✗ break;
363 }
364 ✗ if (lbits >= 16)
365 ✗ return AVERROR_INVALIDDATA;
366 }
367 ✗ for (i = 0; i < len; i++) {
368 ✗ bytestream2_put_byte(pb, bytestream2_get_byte(gb));
369 }
370 }
371 ✗ } else if (tag == 1) {
372 ✗ int b = read_bits32(&bits, gb, 1);
373 int len;
374
375 ✗ if (b == 0) {
376 ✗ len = (read_bits32(&bits, gb, 4)) + 2;
377 } else {
378 ✗ int bb = bytestream2_get_byte(gb);
379 ✗ if ((bb & 0x80) == 0) {
380 ✗ len = bb + 18;
381 } else {
382 ✗ int top = (bb & 0x7F) << 8;
383 ✗ len = top + bytestream2_get_byte(gb) + 146;
384 }
385 }
386 ✗ bytestream2_skip_p(pb, len);
387 ✗ } else if (tag == 2) {
388 ✗ int i, subtag = read_bits32(&bits, gb, 2);
389
390 ✗ if (subtag != 3) {
391 ✗ int top = (read_bits32(&bits, gb, 4)) << 8;
392 ✗ int offs = top + bytestream2_get_byte(gb);
393 ✗ if ((subtag != 0) || (offs <= 0xF80)) {
394 ✗ int len = (subtag) + 3;
395 ✗ lz_copy(pb, g2, (offs) - 4096, len);
396 } else {
397 int real_off, len, c1, c2;
398
399 ✗ if (offs == 0xFFF) {
400 ✗ return 0;
401 }
402
403 ✗ real_off = ((offs >> 4) & 0x7) + 1;
404 ✗ len = ((offs & 0xF) + 2) * 2;
405 ✗ c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
406 ✗ c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
407 ✗ for (i = 0; i < len/2; i++) {
408 ✗ bytestream2_put_byte(pb, c1);
409 ✗ bytestream2_put_byte(pb, c2);
410 }
411 }
412 } else {
413 ✗ int b = bytestream2_get_byte(gb);
414 ✗ int off = ((b & 0x7F)) + 1;
415 ✗ int len = ((b & 0x80) == 0) ? 2 : 3;
416
417 ✗ lz_copy(pb, g2, -off, len);
418 }
419 } else {
420 int len;
421 int off;
422 ✗ if (use8) {
423 ✗ int q, b = bytestream2_get_byte(gb);
424 ✗ if ((b & 0xC0) == 0xC0) {
425 ✗ len = ((b & 0x3F)) + 8;
426 ✗ q = read_bits32(&bits, gb, 4);
427 ✗ off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
428 } else {
429 int ofs1;
430 ✗ if ((b & 0x80) == 0) {
431 ✗ len = ((b >> 4)) + 6;
432 ✗ ofs1 = (b & 0xF);
433 } else {
434 ✗ len = ((b & 0x3F)) + 14;
435 ✗ ofs1 = read_bits32(&bits, gb, 4);
436 }
437 ✗ off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
438 }
439 } else {
440 ✗ int ofs1, b = bytestream2_get_byte(gb);
441
442 ✗ if ((b >> 4) == 0xF) {
443 ✗ len = bytestream2_get_byte(gb) + 21;
444 } else {
445 ✗ len = (b >> 4) + 6;
446 }
447 ✗ ofs1 = (b & 0xF);
448 ✗ off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
449 }
450 ✗ lz_copy(pb, g2, off, len);
451 }
452 }
453
454 ✗ if (bytestream2_get_bytes_left_p(pb) > 0)
455 ✗ return AVERROR_INVALIDDATA;
456
457 ✗ return 0;
458 }
459
460 ✗ static int gdv_decode_frame(AVCodecContext *avctx, AVFrame *frame,
461 int *got_frame, AVPacket *avpkt)
462 {
463 ✗ GDVContext *gdv = avctx->priv_data;
464 ✗ GetByteContext *gb = &gdv->gb;
465 ✗ PutByteContext *pb = &gdv->pb;
466 int ret, i;
467 int compression;
468 unsigned flags;
469 uint8_t *dst;
470
471 ✗ bytestream2_init(gb, avpkt->data, avpkt->size);
472 ✗ bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
473
474 ✗ flags = bytestream2_get_le32(gb);
475 ✗ compression = flags & 0xF;
476
477 ✗ if (compression == 4 || compression == 7 || compression > 8)
478 ✗ return AVERROR_INVALIDDATA;
479
480 ✗ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
481 ✗ return ret;
482 ✗ ff_copy_palette(gdv->pal, avpkt, avctx);
483
484 ✗ if (compression < 2 && bytestream2_get_bytes_left(gb) < 256*3)
485 ✗ return AVERROR_INVALIDDATA;
486 ✗ rescale(gdv, gdv->frame, avctx->width, avctx->height,
487 ✗ !!(flags & 0x10), !!(flags & 0x20));
488
489 ✗ switch (compression) {
490 ✗ case 1:
491 ✗ memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
492 av_fallthrough;
493 ✗ case 0:
494 ✗ for (i = 0; i < 256; i++) {
495 ✗ unsigned r = bytestream2_get_byte(gb);
496 ✗ unsigned g = bytestream2_get_byte(gb);
497 ✗ unsigned b = bytestream2_get_byte(gb);
498 ✗ gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
499 }
500 ✗ break;
501 ✗ case 2:
502 ✗ ret = decompress_2(avctx);
503 ✗ break;
504 ✗ case 3:
505 ✗ break;
506 ✗ case 5:
507 ✗ ret = decompress_5(avctx, flags >> 8);
508 ✗ break;
509 ✗ case 6:
510 ✗ ret = decompress_68(avctx, flags >> 8, 0);
511 ✗ break;
512 ✗ case 8:
513 ✗ ret = decompress_68(avctx, flags >> 8, 1);
514 ✗ break;
515 ✗ default:
516 ✗ av_assert0(0);
517 }
518 ✗ if (ret < 0)
519 ✗ return ret;
520
521 ✗ memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
522 ✗ dst = frame->data[0];
523
524 ✗ if (!gdv->scale_v && !gdv->scale_h) {
525 ✗ int sidx = PREAMBLE_SIZE, didx = 0;
526 int y;
527
528 ✗ for (y = 0; y < avctx->height; y++) {
529 ✗ memcpy(dst + didx, gdv->frame + sidx, avctx->width);
530 ✗ sidx += avctx->width;
531 ✗ didx += frame->linesize[0];
532 }
533 } else {
534 ✗ int sidx = PREAMBLE_SIZE, didx = 0;
535 int y;
536
537 ✗ for (y = 0; y < avctx->height; y++) {
538 ✗ if (!gdv->scale_v) {
539 ✗ memcpy(dst + didx, gdv->frame + sidx, avctx->width);
540 } else {
541 ✗ uint8_t *dst2 = dst + didx;
542 ✗ uint8_t *src2 = gdv->frame + sidx;
543
544 ✗ scaleup(dst2, src2, avctx->width);
545 }
546 ✗ if (!gdv->scale_h || ((y & 1) == 1)) {
547 ✗ sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
548 }
549 ✗ didx += frame->linesize[0];
550 }
551 }
552
553 ✗ *got_frame = 1;
554
555 ✗ return avpkt->size;
556 }
557
558 ✗ static av_cold int gdv_decode_close(AVCodecContext *avctx)
559 {
560 ✗ GDVContext *gdv = avctx->priv_data;
561 ✗ av_freep(&gdv->frame);
562 ✗ return 0;
563 }
564
565 const FFCodec ff_gdv_decoder = {
566 .p.name = "gdv",
567 CODEC_LONG_NAME("Gremlin Digital Video"),
568 .p.type = AVMEDIA_TYPE_VIDEO,
569 .p.id = AV_CODEC_ID_GDV,
570 .priv_data_size = sizeof(GDVContext),
571 .init = gdv_decode_init,
572 .close = gdv_decode_close,
573 FF_CODEC_DECODE_CB(gdv_decode_frame),
574 .p.capabilities = AV_CODEC_CAP_DR1,
575 };
576