FFmpeg coverage


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