FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dxa.c
Date: 2026-05-01 13:04:54
Exec Total Coverage
Lines: 182 216 84.3%
Functions: 4 4 100.0%
Branches: 90 121 74.4%

Line Branch Exec Source
1 /*
2 * Feeble Files/ScummVM DXA decoder
3 * Copyright (c) 2007 Konstantin Shishkov
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 /**
23 * @file
24 * DXA Video decoder
25 */
26
27 #include "libavutil/attributes.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mem.h"
30 #include "bytestream.h"
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34
35 #include <zlib.h>
36
37 /*
38 * Decoder context
39 */
40 typedef struct DxaDecContext {
41 AVFrame *prev;
42
43 int dsize;
44 #define DECOMP_BUF_PADDING 16
45 uint8_t *decomp_buf;
46 uint32_t pal[256];
47 } DxaDecContext;
48
49 static const uint8_t shift1[6] = { 0, 8, 8, 8, 4, 4 };
50 static const uint8_t shift2[6] = { 0, 0, 8, 4, 0, 4 };
51
52 5 static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst,
53 int stride, uint8_t *src, int srcsize, uint8_t *ref)
54 {
55 uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
56 5 uint8_t *src_end = src + srcsize;
57 int i, j, k;
58 int type, x, y, d, d2;
59 uint32_t mask;
60
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (12ULL + ((avctx->width * avctx->height) >> 4) + AV_RB32(src + 0) + AV_RB32(src + 4) > srcsize)
62 return AVERROR_INVALIDDATA;
63
64 5 code = src + 12;
65 5 data = code + ((avctx->width * avctx->height) >> 4);
66 5 mv = data + AV_RB32(src + 0);
67 5 msk = mv + AV_RB32(src + 4);
68
69
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 5 times.
255 for(j = 0; j < avctx->height; j += 4){
70
2/2
✓ Branch 0 taken 40000 times.
✓ Branch 1 taken 250 times.
40250 for(i = 0; i < avctx->width; i += 4){
71
3/6
✓ Branch 0 taken 40000 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40000 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 40000 times.
40000 if (data > src_end || mv > src_end || msk > src_end)
72 return AVERROR_INVALIDDATA;
73 40000 tmp = dst + i;
74 40000 tmp2 = ref + i;
75 40000 type = *code++;
76
8/9
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 26388 times.
✓ Branch 2 taken 326 times.
✓ Branch 3 taken 2428 times.
✓ Branch 4 taken 705 times.
✓ Branch 5 taken 681 times.
✓ Branch 6 taken 9229 times.
✓ Branch 7 taken 16 times.
✗ Branch 8 not taken.
40000 switch(type){
77 227 case 4: // motion compensation
78
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 113 times.
227 x = (*mv) >> 4; if(x & 8) x = 8 - x;
79
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 150 times.
227 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
80
2/4
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
227 if (i < -x || avctx->width - i - 4 < x ||
81
2/4
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 227 times.
227 j < -y || avctx->height - j - 4 < y) {
82 av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
83 return AVERROR_INVALIDDATA;
84 }
85 227 tmp2 += x + y*stride;
86 av_fallthrough;
87 26615 case 0: // skip
88 case 5: // skip in method 12
89
2/2
✓ Branch 0 taken 106460 times.
✓ Branch 1 taken 26615 times.
133075 for(y = 0; y < 4; y++){
90 106460 memcpy(tmp, tmp2, 4);
91 106460 tmp += stride;
92 106460 tmp2 += stride;
93 }
94 26615 break;
95 326 case 1: // masked change
96 case 10: // masked change with only half of pixels changed
97 case 11: // cases 10-15 are for method 12 only
98 case 12:
99 case 13:
100 case 14:
101 case 15:
102
1/2
✓ Branch 0 taken 326 times.
✗ Branch 1 not taken.
326 if(type == 1){
103 326 mask = AV_RB16(msk);
104 326 msk += 2;
105 }else{
106 type -= 10;
107 mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
108 msk++;
109 }
110
2/2
✓ Branch 0 taken 1304 times.
✓ Branch 1 taken 326 times.
1630 for(y = 0; y < 4; y++){
111
2/2
✓ Branch 0 taken 5216 times.
✓ Branch 1 taken 1304 times.
6520 for(x = 0; x < 4; x++){
112
2/2
✓ Branch 0 taken 2189 times.
✓ Branch 1 taken 3027 times.
5216 tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
113 5216 mask <<= 1;
114 }
115 1304 tmp += stride;
116 1304 tmp2 += stride;
117 }
118 326 break;
119 2428 case 2: // fill block
120
2/2
✓ Branch 0 taken 9712 times.
✓ Branch 1 taken 2428 times.
12140 for(y = 0; y < 4; y++){
121 9712 memset(tmp, data[0], 4);
122 9712 tmp += stride;
123 }
124 2428 data++;
125 2428 break;
126 705 case 3: // raw block
127
2/2
✓ Branch 0 taken 2820 times.
✓ Branch 1 taken 705 times.
3525 for(y = 0; y < 4; y++){
128 2820 memcpy(tmp, data, 4);
129 2820 data += 4;
130 2820 tmp += stride;
131 }
132 705 break;
133 681 case 8: // subblocks - method 13 only
134 681 mask = *msk++;
135
2/2
✓ Branch 0 taken 2724 times.
✓ Branch 1 taken 681 times.
3405 for(k = 0; k < 4; k++){
136 2724 d = ((k & 1) << 1) + ((k & 2) * stride);
137 2724 d2 = ((k & 1) << 1) + ((k & 2) * stride);
138 2724 tmp2 = ref + i + d2;
139
4/5
✓ Branch 0 taken 361 times.
✓ Branch 1 taken 1085 times.
✓ Branch 2 taken 696 times.
✓ Branch 3 taken 582 times.
✗ Branch 4 not taken.
2724 switch(mask & 0xC0){
140 361 case 0x80: // motion compensation
141
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 195 times.
361 x = (*mv) >> 4; if(x & 8) x = 8 - x;
142
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 193 times.
361 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
143
2/4
✓ Branch 0 taken 361 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 361 times.
✗ Branch 3 not taken.
361 if (i + 2*(k & 1) < -x || avctx->width - i - 2*(k & 1) - 2 < x ||
144
2/4
✓ Branch 0 taken 361 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 361 times.
361 j + (k & 2) < -y || avctx->height - j - (k & 2) - 2 < y) {
145 av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
146 return AVERROR_INVALIDDATA;
147 }
148 361 tmp2 += x + y*stride;
149 av_fallthrough;
150 1446 case 0x00: // skip
151 1446 tmp[d + 0 ] = tmp2[0];
152 1446 tmp[d + 1 ] = tmp2[1];
153 1446 tmp[d + 0 + stride] = tmp2[0 + stride];
154 1446 tmp[d + 1 + stride] = tmp2[1 + stride];
155 1446 break;
156 696 case 0x40: // fill
157 696 tmp[d + 0 ] = data[0];
158 696 tmp[d + 1 ] = data[0];
159 696 tmp[d + 0 + stride] = data[0];
160 696 tmp[d + 1 + stride] = data[0];
161 696 data++;
162 696 break;
163 582 case 0xC0: // raw
164 582 tmp[d + 0 ] = *data++;
165 582 tmp[d + 1 ] = *data++;
166 582 tmp[d + 0 + stride] = *data++;
167 582 tmp[d + 1 + stride] = *data++;
168 582 break;
169 }
170 2724 mask <<= 2;
171 }
172 681 break;
173 9229 case 32: // vector quantization - 2 colors
174 9229 mask = AV_RB16(msk);
175 9229 msk += 2;
176
2/2
✓ Branch 0 taken 36916 times.
✓ Branch 1 taken 9229 times.
46145 for(y = 0; y < 4; y++){
177
2/2
✓ Branch 0 taken 147664 times.
✓ Branch 1 taken 36916 times.
184580 for(x = 0; x < 4; x++){
178 147664 tmp[x] = data[mask & 1];
179 147664 mask >>= 1;
180 }
181 36916 tmp += stride;
182 36916 tmp2 += stride;
183 }
184 9229 data += 2;
185 9229 break;
186 16 case 33: // vector quantization - 3 or 4 colors
187 case 34:
188 16 mask = AV_RB32(msk);
189 16 msk += 4;
190
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 for(y = 0; y < 4; y++){
191
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 64 times.
320 for(x = 0; x < 4; x++){
192 256 tmp[x] = data[mask & 3];
193 256 mask >>= 2;
194 }
195 64 tmp += stride;
196 64 tmp2 += stride;
197 }
198 16 data += type - 30;
199 16 break;
200 default:
201 av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
202 return AVERROR_INVALIDDATA;
203 }
204 }
205 250 dst += stride * 4;
206 250 ref += stride * 4;
207 }
208 5 return 0;
209 }
210
211 34 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
212 int *got_frame, AVPacket *avpkt)
213 {
214 34 DxaDecContext * const c = avctx->priv_data;
215 uint8_t *outptr, *srcptr, *tmpptr;
216 unsigned long dsize;
217 int i, j, compr, ret;
218 int stride;
219 GetByteContext gb;
220
221 34 bytestream2_init(&gb, avpkt->data, avpkt->size);
222
223 /* make the palette available on the way out */
224
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 32 times.
34 if (bytestream2_peek_le32(&gb) == MKTAG('C','M','A','P')) {
225 2 bytestream2_skip(&gb, 4);
226
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for(i = 0; i < 256; i++){
227 512 c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
228 }
229 }
230
231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
34 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
232 return ret;
233 34 memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
234
235 34 outptr = frame->data[0];
236 34 srcptr = c->decomp_buf;
237 34 tmpptr = c->prev->data[0];
238 34 stride = frame->linesize[0];
239
240
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 17 times.
34 if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
241 17 compr = -1;
242 else
243 17 compr = bytestream2_get_byte(&gb);
244
245 34 dsize = c->dsize;
246
3/4
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 17 times.
34 if (compr != 4 && compr != -1) {
247 17 bytestream2_skip(&gb, 4);
248
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
17 if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
249 17 bytestream2_get_bytes_left(&gb)) != Z_OK) {
250 av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
251 return AVERROR_UNKNOWN;
252 }
253 17 memset(c->decomp_buf + dsize, 0, DECOMP_BUF_PADDING);
254 }
255
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (avctx->debug & FF_DEBUG_PICT_INFO)
257 av_log(avctx, AV_LOG_DEBUG, "compr:%2d, dsize:%d\n", compr, (int)dsize);
258
259
4/5
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
34 switch(compr){
260 17 case -1:
261 17 frame->flags &= ~AV_FRAME_FLAG_KEY;
262 17 frame->pict_type = AV_PICTURE_TYPE_P;
263
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (c->prev->data[0])
264 17 memcpy(frame->data[0], c->prev->data[0], frame->linesize[0] * avctx->height);
265 else{ // Should happen only when first frame is 'NULL'
266 memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
267 frame->flags |= AV_FRAME_FLAG_KEY;
268 frame->pict_type = AV_PICTURE_TYPE_I;
269 }
270 17 break;
271 2 case 2:
272 case 4:
273 2 frame->flags |= AV_FRAME_FLAG_KEY;
274 2 frame->pict_type = AV_PICTURE_TYPE_I;
275
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 2 times.
682 for (j = 0; j < avctx->height; j++) {
276 680 memcpy(outptr, srcptr, avctx->width);
277 680 outptr += stride;
278 680 srcptr += avctx->width;
279 }
280 2 break;
281 10 case 3:
282 case 5:
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!tmpptr) {
284 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
285 if (!(avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
286 return AVERROR_INVALIDDATA;
287 }
288 10 frame->flags &= ~AV_FRAME_FLAG_KEY;
289 10 frame->pict_type = AV_PICTURE_TYPE_P;
290
2/2
✓ Branch 0 taken 4800 times.
✓ Branch 1 taken 10 times.
4810 for (j = 0; j < avctx->height; j++) {
291
1/2
✓ Branch 0 taken 4800 times.
✗ Branch 1 not taken.
4800 if(tmpptr){
292
2/2
✓ Branch 0 taken 3072000 times.
✓ Branch 1 taken 4800 times.
3076800 for(i = 0; i < avctx->width; i++)
293 3072000 outptr[i] = srcptr[i] ^ tmpptr[i];
294 4800 tmpptr += stride;
295 }else
296 memcpy(outptr, srcptr, avctx->width);
297 4800 outptr += stride;
298 4800 srcptr += avctx->width;
299 }
300 10 break;
301 5 case 12: // ScummVM coding
302 case 13:
303 5 frame->flags &= ~AV_FRAME_FLAG_KEY;
304 5 frame->pict_type = AV_PICTURE_TYPE_P;
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!c->prev->data[0]) {
306 av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
307 return AVERROR_INVALIDDATA;
308 }
309 5 decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, dsize, c->prev->data[0]);
310 5 break;
311 default:
312 av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
313 return AVERROR_INVALIDDATA;
314 }
315
316
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
34 if ((ret = av_frame_replace(c->prev, frame)) < 0)
317 return ret;
318
319 34 *got_frame = 1;
320
321 /* always report that the buffer was completely consumed */
322 34 return avpkt->size;
323 }
324
325 5 static av_cold int decode_init(AVCodecContext *avctx)
326 {
327 5 DxaDecContext * const c = avctx->priv_data;
328
329
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (avctx->width%4 || avctx->height%4) {
330 avpriv_request_sample(avctx, "dimensions are not a multiple of 4");
331 return AVERROR_INVALIDDATA;
332 }
333
334 5 c->prev = av_frame_alloc();
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!c->prev)
336 return AVERROR(ENOMEM);
337
338 5 avctx->pix_fmt = AV_PIX_FMT_PAL8;
339
340 5 c->dsize = avctx->width * avctx->height * 2;
341 5 c->decomp_buf = av_malloc(c->dsize + DECOMP_BUF_PADDING);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!c->decomp_buf) {
343 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
344 return AVERROR(ENOMEM);
345 }
346
347 5 return 0;
348 }
349
350 5 static av_cold int decode_end(AVCodecContext *avctx)
351 {
352 5 DxaDecContext * const c = avctx->priv_data;
353
354 5 av_freep(&c->decomp_buf);
355 5 av_frame_free(&c->prev);
356
357 5 return 0;
358 }
359
360 const FFCodec ff_dxa_decoder = {
361 .p.name = "dxa",
362 CODEC_LONG_NAME("Feeble Files/ScummVM DXA"),
363 .p.type = AVMEDIA_TYPE_VIDEO,
364 .p.id = AV_CODEC_ID_DXA,
365 .priv_data_size = sizeof(DxaDecContext),
366 .init = decode_init,
367 .close = decode_end,
368 FF_CODEC_DECODE_CB(decode_frame),
369 .p.capabilities = AV_CODEC_CAP_DR1,
370 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
371 };
372