FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/zmbv.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 243 289 84.1%
Branches: 118 161 73.3%

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