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