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