FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/zmbv.c
Date: 2026-07-16 17:05:34
Exec Total Coverage
Lines: 246 296 83.1%
Functions: 7 7 100.0%
Branches: 123 169 72.8%

Line Branch Exec Source
1 /*
2 * Zip Motion Blocks Video (ZMBV) decoder
3 * Copyright (c) 2006 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 * Zip Motion Blocks Video decoder
25 */
26
27 #include <stddef.h>
28
29 #include "libavutil/attributes.h"
30 #include "libavutil/common.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mem.h"
34 #include "avcodec.h"
35 #include "codec_internal.h"
36 #include "decode.h"
37 #include "zlib_wrapper.h"
38
39 #include <zlib.h>
40
41 #define ZMBV_KEYFRAME 1
42 #define ZMBV_DELTAPAL 2
43
44 enum ZmbvFormat {
45 ZMBV_FMT_NONE = 0,
46 ZMBV_FMT_1BPP = 1,
47 ZMBV_FMT_2BPP = 2,
48 ZMBV_FMT_4BPP = 3,
49 ZMBV_FMT_8BPP = 4,
50 ZMBV_FMT_15BPP = 5,
51 ZMBV_FMT_16BPP = 6,
52 ZMBV_FMT_24BPP = 7,
53 ZMBV_FMT_32BPP = 8
54 };
55
56 /*
57 * Decoder context
58 */
59 typedef struct ZmbvContext {
60 AVCodecContext *avctx;
61
62 int bpp;
63 int alloc_bpp;
64 unsigned int decomp_size;
65 uint8_t* decomp_buf;
66 uint8_t pal[768];
67 uint8_t *prev, *cur;
68 int width, height;
69 int fmt;
70 int comp;
71 int flags;
72 int stride;
73 int bw, bh, bx, by;
74 int decomp_len;
75 int got_keyframe;
76 FFZStream zstream;
77 int (*decode_xor)(struct ZmbvContext *c);
78 } ZmbvContext;
79
80 /**
81 * Decode XOR'ed frame - 8bpp version
82 */
83
84 275 static int zmbv_decode_xor_8(ZmbvContext *c)
85 {
86 275 uint8_t *src = c->decomp_buf;
87 uint8_t *output, *prev;
88 int8_t *mvec;
89 int x, y;
90 int d, dx, dy, bw2, bh2;
91 int block;
92 int i, j;
93 int mx, my;
94
95 275 output = c->cur;
96 275 prev = c->prev;
97
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275 times.
275 if (c->flags & ZMBV_DELTAPAL) {
99 for (i = 0; i < 768; i++)
100 c->pal[i] ^= *src++;
101 }
102
103 275 mvec = (int8_t*)src;
104 275 src += ((c->bx * c->by * 2 + 3) & ~3);
105
106 275 block = 0;
107
2/2
✓ Branch 0 taken 3570 times.
✓ Branch 1 taken 274 times.
3844 for (y = 0; y < c->height; y += c->bh) {
108 3570 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
109
2/2
✓ Branch 0 taken 71382 times.
✓ Branch 1 taken 3569 times.
74951 for (x = 0; x < c->width; x += c->bw) {
110 uint8_t *out, *tprev;
111
112 71382 d = mvec[block] & 1;
113 71382 dx = mvec[block] >> 1;
114 71382 dy = mvec[block + 1] >> 1;
115 71382 block += 2;
116
117 71382 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
118
119 /* copy block - motion vectors out of bounds are used to zero blocks */
120 71382 out = output + x;
121 71382 tprev = prev + x + dx + dy * c->width;
122 71382 mx = x + dx;
123 71382 my = y + dy;
124
2/2
✓ Branch 0 taken 1098272 times.
✓ Branch 1 taken 71382 times.
1169654 for (j = 0; j < bh2; j++) {
125
2/4
✓ Branch 0 taken 1098272 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1098272 times.
1098272 if (my + j < 0 || my + j >= c->height) {
126 memset(out, 0, bw2);
127
2/4
✓ Branch 0 taken 1098272 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1098272 times.
✗ Branch 3 not taken.
1098272 } else if (mx >= 0 && mx + bw2 <= c->width){
128 1098272 memcpy(out, tprev, sizeof(*out) * bw2);
129 } else {
130 for (i = 0; i < bw2; i++) {
131 if (mx + i < 0 || mx + i >= c->width)
132 out[i] = 0;
133 else
134 out[i] = tprev[i];
135 }
136 }
137 1098272 out += c->width;
138 1098272 tprev += c->width;
139 }
140
141
2/2
✓ Branch 0 taken 1477 times.
✓ Branch 1 taken 69905 times.
71382 if (d) { /* apply XOR'ed difference */
142
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1476 times.
1477 if (c->decomp_len - (src - c->decomp_buf) < bw2 * bh2)
143 1 return AVERROR_INVALIDDATA;
144 1476 out = output + x;
145
2/2
✓ Branch 0 taken 23616 times.
✓ Branch 1 taken 1476 times.
25092 for (j = 0; j < bh2; j++) {
146
2/2
✓ Branch 0 taken 377856 times.
✓ Branch 1 taken 23616 times.
401472 for (i = 0; i < bw2; i++)
147 377856 out[i] ^= *src++;
148 23616 out += c->width;
149 }
150 }
151 }
152 3569 output += c->width * c->bh;
153 3569 prev += c->width * c->bh;
154 }
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 274 times.
274 if (src - c->decomp_buf != c->decomp_len)
156 av_log(c->avctx, AV_LOG_ERROR, "Used %td of %i bytes\n",
157 src-c->decomp_buf, c->decomp_len);
158 274 return 0;
159 }
160
161 /**
162 * Decode XOR'ed frame - 15bpp and 16bpp version
163 */
164
165 324 static int zmbv_decode_xor_16(ZmbvContext *c)
166 {
167 324 uint8_t *src = c->decomp_buf;
168 uint16_t *output, *prev;
169 int8_t *mvec;
170 int x, y;
171 int d, dx, dy, bw2, bh2;
172 int block;
173 int i, j;
174 int mx, my;
175
176 324 output = (uint16_t*)c->cur;
177 324 prev = (uint16_t*)c->prev;
178
179 324 mvec = (int8_t*)src;
180 324 src += ((c->bx * c->by * 2 + 3) & ~3);
181
182 324 block = 0;
183
2/2
✓ Branch 0 taken 4212 times.
✓ Branch 1 taken 324 times.
4536 for (y = 0; y < c->height; y += c->bh) {
184 4212 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
185
2/2
✓ Branch 0 taken 84240 times.
✓ Branch 1 taken 4212 times.
88452 for (x = 0; x < c->width; x += c->bw) {
186 uint16_t *out, *tprev;
187
188 84240 d = mvec[block] & 1;
189 84240 dx = mvec[block] >> 1;
190 84240 dy = mvec[block + 1] >> 1;
191 84240 block += 2;
192
193 84240 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
194
195 /* copy block - motion vectors out of bounds are used to zero blocks */
196 84240 out = output + x;
197 84240 tprev = prev + x + dx + dy * c->width;
198 84240 mx = x + dx;
199 84240 my = y + dy;
200
2/2
✓ Branch 0 taken 1296000 times.
✓ Branch 1 taken 84240 times.
1380240 for (j = 0; j < bh2; j++) {
201
4/4
✓ Branch 0 taken 1295985 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 320 times.
✓ Branch 3 taken 1295665 times.
1296000 if (my + j < 0 || my + j >= c->height) {
202 335 memset(out, 0, bw2 * 2);
203
4/4
✓ Branch 0 taken 1295633 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 1295577 times.
✓ Branch 3 taken 56 times.
1295665 } else if (mx >= 0 && mx + bw2 <= c->width){
204 1295577 memcpy(out, tprev, sizeof(*out) * bw2);
205 } else {
206
2/2
✓ Branch 0 taken 1408 times.
✓ Branch 1 taken 88 times.
1496 for (i = 0; i < bw2; i++) {
207
4/4
✓ Branch 0 taken 1264 times.
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 160 times.
✓ Branch 3 taken 1104 times.
1408 if (mx + i < 0 || mx + i >= c->width)
208 304 out[i] = 0;
209 else
210 1104 out[i] = tprev[i];
211 }
212 }
213 1296000 out += c->width;
214 1296000 tprev += c->width;
215 }
216
217
2/2
✓ Branch 0 taken 7935 times.
✓ Branch 1 taken 76305 times.
84240 if (d) { /* apply XOR'ed difference */
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7935 times.
7935 if (c->decomp_len - (src - c->decomp_buf) < bw2 * bh2 * 2)
219 return AVERROR_INVALIDDATA;
220 7935 out = output + x;
221
2/2
✓ Branch 0 taken 123312 times.
✓ Branch 1 taken 7935 times.
131247 for (j = 0; j < bh2; j++){
222
2/2
✓ Branch 0 taken 1972992 times.
✓ Branch 1 taken 123312 times.
2096304 for (i = 0; i < bw2; i++) {
223 1972992 out[i] ^= *((uint16_t*)src);
224 1972992 src += 2;
225 }
226 123312 out += c->width;
227 }
228 }
229 }
230 4212 output += c->width * c->bh;
231 4212 prev += c->width * c->bh;
232 }
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 324 times.
324 if (src - c->decomp_buf != c->decomp_len)
234 av_log(c->avctx, AV_LOG_ERROR, "Used %td of %i bytes\n",
235 src-c->decomp_buf, c->decomp_len);
236 324 return 0;
237 }
238
239 #ifdef ZMBV_ENABLE_24BPP
240 /**
241 * Decode XOR'ed frame - 24bpp version
242 */
243
244 static int zmbv_decode_xor_24(ZmbvContext *c)
245 {
246 uint8_t *src = c->decomp_buf;
247 uint8_t *output, *prev;
248 int8_t *mvec;
249 int x, y;
250 int d, dx, dy, bw2, bh2;
251 int block;
252 int i, j;
253 int mx, my;
254 int stride;
255
256 output = c->cur;
257 prev = c->prev;
258
259 stride = c->width * 3;
260 mvec = (int8_t*)src;
261 src += ((c->bx * c->by * 2 + 3) & ~3);
262
263 block = 0;
264 for (y = 0; y < c->height; y += c->bh) {
265 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
266 for (x = 0; x < c->width; x += c->bw) {
267 uint8_t *out, *tprev;
268
269 d = mvec[block] & 1;
270 dx = mvec[block] >> 1;
271 dy = mvec[block + 1] >> 1;
272 block += 2;
273
274 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
275
276 /* copy block - motion vectors out of bounds are used to zero blocks */
277 out = output + x * 3;
278 tprev = prev + (x + dx) * 3 + dy * stride;
279 mx = x + dx;
280 my = y + dy;
281 for (j = 0; j < bh2; j++) {
282 if (my + j < 0 || my + j >= c->height) {
283 memset(out, 0, bw2 * 3);
284 } else if (mx >= 0 && mx + bw2 <= c->width){
285 memcpy(out, tprev, 3 * bw2);
286 } else {
287 for (i = 0; i < bw2; i++){
288 if (mx + i < 0 || mx + i >= c->width) {
289 out[i * 3 + 0] = 0;
290 out[i * 3 + 1] = 0;
291 out[i * 3 + 2] = 0;
292 } else {
293 out[i * 3 + 0] = tprev[i * 3 + 0];
294 out[i * 3 + 1] = tprev[i * 3 + 1];
295 out[i * 3 + 2] = tprev[i * 3 + 2];
296 }
297 }
298 }
299 out += stride;
300 tprev += stride;
301 }
302
303 if (d) { /* apply XOR'ed difference */
304 if (c->decomp_len - (src - c->decomp_buf) < bw2 * bh2 * 3)
305 return AVERROR_INVALIDDATA;
306 out = output + x * 3;
307 for (j = 0; j < bh2; j++) {
308 for (i = 0; i < bw2; i++) {
309 out[i * 3 + 0] ^= *src++;
310 out[i * 3 + 1] ^= *src++;
311 out[i * 3 + 2] ^= *src++;
312 }
313 out += stride;
314 }
315 }
316 }
317 output += stride * c->bh;
318 prev += stride * c->bh;
319 }
320 if (src - c->decomp_buf != c->decomp_len)
321 av_log(c->avctx, AV_LOG_ERROR, "Used %td of %i bytes\n",
322 src-c->decomp_buf, c->decomp_len);
323 return 0;
324 }
325 #endif //ZMBV_ENABLE_24BPP
326
327 /**
328 * Decode XOR'ed frame - 32bpp version
329 */
330
331 162 static int zmbv_decode_xor_32(ZmbvContext *c)
332 {
333 162 uint8_t *src = c->decomp_buf;
334 uint32_t *output, *prev;
335 int8_t *mvec;
336 int x, y;
337 int d, dx, dy, bw2, bh2;
338 int block;
339 int i, j;
340 int mx, my;
341
342 162 output = (uint32_t*)c->cur;
343 162 prev = (uint32_t*)c->prev;
344
345 162 mvec = (int8_t*)src;
346 162 src += ((c->bx * c->by * 2 + 3) & ~3);
347
348 162 block = 0;
349
2/2
✓ Branch 0 taken 2106 times.
✓ Branch 1 taken 162 times.
2268 for (y = 0; y < c->height; y += c->bh) {
350 2106 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
351
2/2
✓ Branch 0 taken 42120 times.
✓ Branch 1 taken 2106 times.
44226 for (x = 0; x < c->width; x += c->bw) {
352 uint32_t *out, *tprev;
353
354 42120 d = mvec[block] & 1;
355 42120 dx = mvec[block] >> 1;
356 42120 dy = mvec[block + 1] >> 1;
357 42120 block += 2;
358
359 42120 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
360
361 /* copy block - motion vectors out of bounds are used to zero blocks */
362 42120 out = output + x;
363 42120 tprev = prev + x + dx + dy * c->width;
364 42120 mx = x + dx;
365 42120 my = y + dy;
366
2/2
✓ Branch 0 taken 648000 times.
✓ Branch 1 taken 42120 times.
690120 for (j = 0; j < bh2; j++) {
367
4/4
✓ Branch 0 taken 647984 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 160 times.
✓ Branch 3 taken 647824 times.
648000 if (my + j < 0 || my + j >= c->height) {
368 176 memset(out, 0, bw2 * 4);
369
4/4
✓ Branch 0 taken 647792 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 647760 times.
✓ Branch 3 taken 32 times.
647824 } else if (mx >= 0 && mx + bw2 <= c->width){
370 647760 memcpy(out, tprev, sizeof(*out) * bw2);
371 } else {
372
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 64 times.
1088 for (i = 0; i < bw2; i++){
373
4/4
✓ Branch 0 taken 880 times.
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 768 times.
1024 if (mx + i < 0 || mx + i >= c->width)
374 256 out[i] = 0;
375 else
376 768 out[i] = tprev[i];
377 }
378 }
379 648000 out += c->width;
380 648000 tprev += c->width;
381 }
382
383
2/2
✓ Branch 0 taken 4451 times.
✓ Branch 1 taken 37669 times.
42120 if (d) { /* apply XOR'ed difference */
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4451 times.
4451 if (c->decomp_len - (src - c->decomp_buf) < bw2 * bh2 * 4)
385 return AVERROR_INVALIDDATA;
386 4451 out = output + x;
387
2/2
✓ Branch 0 taken 69136 times.
✓ Branch 1 taken 4451 times.
73587 for (j = 0; j < bh2; j++){
388
2/2
✓ Branch 0 taken 1106176 times.
✓ Branch 1 taken 69136 times.
1175312 for (i = 0; i < bw2; i++) {
389 1106176 out[i] ^= *((uint32_t *) src);
390 1106176 src += 4;
391 }
392 69136 out += c->width;
393 }
394 }
395 }
396 2106 output += c->width * c->bh;
397 2106 prev += c->width * c->bh;
398 }
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162 times.
162 if (src - c->decomp_buf != c->decomp_len)
400 av_log(c->avctx, AV_LOG_ERROR, "Used %td of %i bytes\n",
401 src-c->decomp_buf, c->decomp_len);
402 162 return 0;
403 }
404
405 /**
406 * Decode intraframe
407 */
408 8 static int zmbv_decode_intra(ZmbvContext *c)
409 {
410 8 uint8_t *src = c->decomp_buf;
411
412 /* make the palette available on the way out */
413
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (c->fmt == ZMBV_FMT_8BPP) {
414 2 memcpy(c->pal, src, 768);
415 2 src += 768;
416 }
417
418 8 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
419 8 return 0;
420 }
421
422 769 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
423 int *got_frame, AVPacket *avpkt)
424 {
425 769 const uint8_t *buf = avpkt->data;
426 769 int buf_size = avpkt->size;
427 769 ZmbvContext * const c = avctx->priv_data;
428 769 int zret = Z_OK; // Zlib return code
429 769 int len = buf_size;
430 int hi_ver, lo_ver, ret;
431 int expected_size;
432
433 /* parse header */
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 769 times.
769 if (len < 1)
435 return AVERROR_INVALIDDATA;
436 769 c->flags = buf[0];
437 769 buf++; len--;
438
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 761 times.
769 if (c->flags & ZMBV_KEYFRAME) {
439 8 c->got_keyframe = 0;
440
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (len < 6)
442 return AVERROR_INVALIDDATA;
443 8 hi_ver = buf[0];
444 8 lo_ver = buf[1];
445 8 c->comp = buf[2];
446 8 c->fmt = buf[3];
447 8 c->bw = buf[4];
448 8 c->bh = buf[5];
449 8 c->decode_xor = NULL;
450
451 8 buf += 6;
452 8 len -= 6;
453 8 av_log(avctx, AV_LOG_DEBUG,
454 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
455 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
456
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (hi_ver != 0 || lo_ver != 1) {
457 avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
458 return AVERROR_PATCHWELCOME;
459 }
460
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (c->bw == 0 || c->bh == 0) {
461 avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
462 return AVERROR_PATCHWELCOME;
463 }
464
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (c->comp != 0 && c->comp != 1) {
465 avpriv_request_sample(avctx, "Compression type %i", c->comp);
466 return AVERROR_PATCHWELCOME;
467 }
468
469
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
8 switch (c->fmt) {
470 2 case ZMBV_FMT_8BPP:
471 2 c->bpp = 8;
472 2 c->decode_xor = zmbv_decode_xor_8;
473 2 avctx->pix_fmt = AV_PIX_FMT_PAL8;
474 2 c->stride = c->width;
475 2 break;
476 4 case ZMBV_FMT_15BPP:
477 case ZMBV_FMT_16BPP:
478 4 c->bpp = 16;
479 4 c->decode_xor = zmbv_decode_xor_16;
480
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (c->fmt == ZMBV_FMT_15BPP)
481 2 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
482 else
483 2 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
484 4 c->stride = c->width * 2;
485 4 break;
486 #ifdef ZMBV_ENABLE_24BPP
487 case ZMBV_FMT_24BPP:
488 c->bpp = 24;
489 c->decode_xor = zmbv_decode_xor_24;
490 avctx->pix_fmt = AV_PIX_FMT_BGR24;
491 c->stride = c->width * 3;
492 break;
493 #endif //ZMBV_ENABLE_24BPP
494 2 case ZMBV_FMT_32BPP:
495 2 c->bpp = 32;
496 2 c->decode_xor = zmbv_decode_xor_32;
497 2 avctx->pix_fmt = AV_PIX_FMT_BGR0;
498 2 c->stride = c->width * 4;
499 2 break;
500 default:
501 c->decode_xor = NULL;
502 avpriv_request_sample(avctx, "Format %i", c->fmt);
503 return AVERROR_PATCHWELCOME;
504 }
505
506 8 zret = inflateReset(&c->zstream.zstream);
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (zret != Z_OK) {
508 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
509 return AVERROR_UNKNOWN;
510 }
511
512
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (c->alloc_bpp < c->bpp) {
513 8 c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
514 8 c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
515 8 c->alloc_bpp = c->bpp;
516 }
517 8 c->bx = (c->width + c->bw - 1) / c->bw;
518 8 c->by = (c->height+ c->bh - 1) / c->bh;
519
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (!c->cur || !c->prev) {
520 c->alloc_bpp = 0;
521 return AVERROR(ENOMEM);
522 }
523 8 memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
524 8 memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
525 8 c->got_keyframe = 1;
526 }
527
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 761 times.
769 if (c->flags & ZMBV_KEYFRAME) {
528 8 expected_size = avctx->width * avctx->height * (c->bpp / 8);
529 } else {
530 761 expected_size = (c->bx * c->by * 2 + 3) & ~3;
531 }
532
2/2
✓ Branch 0 taken 277 times.
✓ Branch 1 taken 492 times.
769 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
533
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 275 times.
277 (c->flags & (ZMBV_DELTAPAL | ZMBV_KEYFRAME)))
534 2 expected_size += 768;
535
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 769 times.
769 if (!c->got_keyframe) {
537 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
538 return AVERROR_INVALIDDATA;
539 }
540
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 769 times.
769 if (c->comp == 0) { // uncompressed data
542 if (c->decomp_size < len) {
543 av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
544 return AVERROR_INVALIDDATA;
545 }
546 memcpy(c->decomp_buf, buf, len);
547 c->decomp_len = len;
548 } else { // ZLIB-compressed data
549 769 z_stream *const zstream = &c->zstream.zstream;
550
551 769 zstream->total_in = zstream->total_out = 0;
552 769 zstream->next_in = buf;
553 769 zstream->avail_in = len;
554 769 zstream->next_out = c->decomp_buf;
555 769 zstream->avail_out = c->decomp_size;
556 769 zret = inflate(zstream, Z_SYNC_FLUSH);
557
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 769 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
769 if (zret != Z_OK && zret != Z_STREAM_END) {
558 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
559 return AVERROR_INVALIDDATA;
560 }
561 769 c->decomp_len = zstream->total_out;
562 }
563
1/2
✓ Branch 0 taken 769 times.
✗ Branch 1 not taken.
769 if (expected_size > c->decomp_len ||
564
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 761 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
769 (c->flags & ZMBV_KEYFRAME) && expected_size < c->decomp_len) {
565 av_log(avctx, AV_LOG_ERROR, "decompressed size %d is incorrect, expected %d\n", c->decomp_len, expected_size);
566 return AVERROR_INVALIDDATA;
567 }
568
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 769 times.
769 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
569 return ret;
570
571
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 761 times.
769 if (c->flags & ZMBV_KEYFRAME) {
572 8 frame->flags |= AV_FRAME_FLAG_KEY;
573 8 frame->pict_type = AV_PICTURE_TYPE_I;
574 8 zmbv_decode_intra(c);
575 } else {
576 761 frame->flags &= ~AV_FRAME_FLAG_KEY;
577 761 frame->pict_type = AV_PICTURE_TYPE_P;
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 761 times.
761 if (c->decomp_len < 2LL * ((c->width + c->bw - 1) / c->bw) * ((c->height + c->bh - 1) / c->bh))
579 return AVERROR_INVALIDDATA;
580
1/2
✓ Branch 0 taken 761 times.
✗ Branch 1 not taken.
761 if (c->decomp_len) {
581
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 760 times.
761 if ((ret = c->decode_xor(c)) < 0)
582 1 return ret;
583 }
584 }
585
586 /* update frames */
587 {
588 uint8_t *out, *src;
589 int j;
590
591 768 out = frame->data[0];
592 768 src = c->cur;
593
2/3
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 492 times.
✗ Branch 2 not taken.
768 switch (c->fmt) {
594 276 case ZMBV_FMT_8BPP:
595
2/2
✓ Branch 0 taken 70656 times.
✓ Branch 1 taken 276 times.
70932 for (j = 0; j < 256; j++)
596 70656 AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 3]));
597 av_fallthrough;
598 case ZMBV_FMT_15BPP:
599 case ZMBV_FMT_16BPP:
600 #ifdef ZMBV_ENABLE_24BPP
601 case ZMBV_FMT_24BPP:
602 #endif
603 case ZMBV_FMT_32BPP:
604 768 av_image_copy_plane(out, frame->linesize[0], src, c->stride,
605 c->stride, c->height);
606 768 break;
607 default:
608 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
609 }
610 768 FFSWAP(uint8_t *, c->cur, c->prev);
611 }
612 768 *got_frame = 1;
613
614 /* always report that the buffer was completely consumed */
615 768 return buf_size;
616 }
617
618 8 static av_cold int decode_init(AVCodecContext *avctx)
619 {
620 8 ZmbvContext * const c = avctx->priv_data;
621
622 8 c->avctx = avctx;
623
624 8 c->width = avctx->width;
625 8 c->height = avctx->height;
626
627 8 c->bpp = avctx->bits_per_coded_sample;
628
629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if ((avctx->width + 255ULL) * (avctx->height + 64ULL) > FFMIN(avctx->max_pixels, INT_MAX / 4) ) {
630 av_log(avctx, AV_LOG_ERROR, "Internal buffer (decomp_size) larger than max_pixels or too large\n");
631 return AVERROR_INVALIDDATA;
632 }
633
634 8 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
635
636 /* Allocate decompression buffer */
637 8 c->decomp_buf = av_mallocz(c->decomp_size);
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!c->decomp_buf) {
639 av_log(avctx, AV_LOG_ERROR,
640 "Can't allocate decompression buffer.\n");
641 return AVERROR(ENOMEM);
642 }
643
644 8 return ff_inflate_init(&c->zstream, avctx);
645 }
646
647 8 static av_cold int decode_end(AVCodecContext *avctx)
648 {
649 8 ZmbvContext * const c = avctx->priv_data;
650
651 8 av_freep(&c->decomp_buf);
652
653 8 av_freep(&c->cur);
654 8 av_freep(&c->prev);
655 8 ff_inflate_end(&c->zstream);
656
657 8 return 0;
658 }
659
660 const FFCodec ff_zmbv_decoder = {
661 .p.name = "zmbv",
662 CODEC_LONG_NAME("Zip Motion Blocks Video"),
663 .p.type = AVMEDIA_TYPE_VIDEO,
664 .p.id = AV_CODEC_ID_ZMBV,
665 .priv_data_size = sizeof(ZmbvContext),
666 .init = decode_init,
667 .close = decode_end,
668 FF_CODEC_DECODE_CB(decode_frame),
669 .p.capabilities = AV_CODEC_CAP_DR1,
670 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
671 };
672