comparison zmbv.c @ 3120:1d184d61e714 libavcodec

dosbox native ZMBV decoder, courtesy of Kostya
author melanson
date Sun, 12 Feb 2006 08:53:29 +0000
parents
children d57bf6a15b04
comparison
equal deleted inserted replaced
3119:09ae2e981d64 3120:1d184d61e714
1 /*
2 * Zip Motion Blocks Video (ZMBV) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 /**
22 * @file zmbv.c
23 * Zip Motion Blocks Video decoder
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "common.h"
30 #include "avcodec.h"
31
32 #ifdef CONFIG_ZLIB
33 #include <zlib.h>
34 #endif
35
36 #define ZMBV_KEYFRAME 1
37 #define ZMBV_DELTAPAL 2
38
39 enum ZmbvFormat {
40 ZMBV_FMT_NONE = 0,
41 ZMBV_FMT_1BPP = 1,
42 ZMBV_FMT_2BPP = 2,
43 ZMBV_FMT_4BPP = 3,
44 ZMBV_FMT_8BPP = 4,
45 ZMBV_FMT_15BPP = 5,
46 ZMBV_FMT_16BPP = 6,
47 ZMBV_FMT_24BPP = 7,
48 ZMBV_FMT_32BPP = 8
49 };
50
51 /*
52 * Decoder context
53 */
54 typedef struct ZmbvContext {
55 AVCodecContext *avctx;
56 AVFrame pic;
57
58 int bpp;
59 unsigned int decomp_size;
60 uint8_t* decomp_buf;
61 uint8_t pal[768];
62 uint8_t *prev, *cur;
63 int width, height;
64 int fmt;
65 int comp;
66 int flags;
67 int bw, bh, bx, by;
68 int decomp_len;
69 #ifdef CONFIG_ZLIB
70 z_stream zstream;
71 #endif
72 int (*decode_intra)(struct ZmbvContext *c);
73 int (*decode_xor)(struct ZmbvContext *c);
74 } ZmbvContext;
75
76 /**
77 * Decode XOR'ed frame - 8bpp version
78 */
79
80 static int zmbv_decode_xor_8(ZmbvContext *c)
81 {
82 uint8_t *src = c->decomp_buf;
83 uint8_t *output, *prev;
84 int8_t *mvec;
85 int x, y;
86 int d, dx, dy, bw2, bh2;
87 int block;
88 int i, j;
89 int mx, my;
90
91 output = c->cur;
92 prev = c->prev;
93
94 if(c->flags & ZMBV_DELTAPAL){
95 for(i = 0; i < 768; i++)
96 c->pal[i] ^= *src++;
97 }
98
99 mvec = (int8_t*)src;
100 src += ((c->bx * c->by * 2 + 3) & ~3);
101
102 block = 0;
103 for(y = 0; y < c->height; y += c->bh) {
104 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
105 for(x = 0; x < c->width; x += c->bw) {
106 uint8_t *out, *tprev;
107
108 d = mvec[block] & 1;
109 dx = mvec[block] >> 1;
110 dy = mvec[block + 1] >> 1;
111 block += 2;
112
113 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
114
115 /* copy block - motion vectors out of bounds are used to zero blocks */
116 out = output + x;
117 tprev = prev + x + dx + dy * c->width;
118 mx = x + dx;
119 my = y + dy;
120 for(j = 0; j < bh2; j++){
121 if((my + j < 0) || (my + j >= c->height)) {
122 memset(out, 0, bw2);
123 } else {
124 for(i = 0; i < bw2; i++){
125 if((mx + i < 0) || (mx + i >= c->width))
126 out[i] = 0;
127 else
128 out[i] = tprev[i];
129 }
130 }
131 out += c->width;
132 tprev += c->width;
133 }
134
135 if(d) { /* apply XOR'ed difference */
136 out = output + x;
137 for(j = 0; j < bh2; j++){
138 for(i = 0; i < bw2; i++)
139 out[i] ^= *src++;
140 out += c->width;
141 }
142 }
143 }
144 output += c->width * c->bh;
145 prev += c->width * c->bh;
146 }
147 if(src - c->decomp_buf != c->decomp_len)
148 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
149 return 0;
150 }
151
152 /**
153 * Decode XOR'ed frame - 24bpp version
154 */
155
156 static int zmbv_decode_xor_24(ZmbvContext *c)
157 {
158 uint8_t *src = c->decomp_buf;
159 uint8_t *output, *prev;
160 int8_t *mvec;
161 int x, y;
162 int d, dx, dy, bw2, bh2;
163 int block;
164 int i, j;
165 int mx, my;
166 int stride;
167
168 output = c->cur;
169 prev = c->prev;
170
171 stride = c->width * 3;
172 mvec = (int8_t*)src;
173 src += ((c->bx * c->by * 2 + 3) & ~3);
174
175 block = 0;
176 for(y = 0; y < c->height; y += c->bh) {
177 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
178 for(x = 0; x < c->width; x += c->bw) {
179 uint8_t *out, *tprev;
180
181 d = mvec[block] & 1;
182 dx = mvec[block] >> 1;
183 dy = mvec[block + 1] >> 1;
184 block += 2;
185
186 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
187
188 /* copy block - motion vectors out of bounds are used to zero blocks */
189 out = output + x * 3;
190 tprev = prev + (x + dx) * 3 + dy * stride;
191 mx = x + dx;
192 my = y + dy;
193 for(j = 0; j < bh2; j++){
194 if((my + j < 0) || (my + j >= c->height)) {
195 memset(out, 0, bw2 * 3);
196 } else {
197 for(i = 0; i < bw2; i++){
198 if((mx + i < 0) || (mx + i >= c->width)) {
199 out[i * 3 + 0] = 0;
200 out[i * 3 + 1] = 0;
201 out[i * 3 + 2] = 0;
202 } else {
203 out[i * 3 + 0] = tprev[i * 3 + 0];
204 out[i * 3 + 1] = tprev[i * 3 + 1];
205 out[i * 3 + 2] = tprev[i * 3 + 2];
206 }
207 }
208 }
209 out += stride;
210 tprev += stride;
211 }
212
213 if(d) { /* apply XOR'ed difference */
214 out = output + x * 3;
215 for(j = 0; j < bh2; j++){
216 for(i = 0; i < bw2; i++) {
217 out[i * 3 + 0] ^= *src++;
218 out[i * 3 + 1] ^= *src++;
219 out[i * 3 + 2] ^= *src++;
220 }
221 out += stride;
222 }
223 }
224 }
225 output += stride * c->bh;
226 prev += stride * c->bh;
227 }
228 if(src - c->decomp_buf != c->decomp_len)
229 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
230 return 0;
231 }
232
233 /**
234 * Decode intraframe
235 */
236 static int zmbv_decode_intra(ZmbvContext *c)
237 {
238 uint8_t *src = c->decomp_buf;
239
240 /* make the palette available on the way out */
241 if (c->fmt == ZMBV_FMT_8BPP) {
242 memcpy(c->pal, src, 768);
243 src += 768;
244 }
245
246 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
247 return 0;
248 }
249
250 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
251 {
252 ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
253 uint8_t *outptr;
254 #ifdef CONFIG_ZLIB
255 int zret = Z_OK; // Zlib return code
256 #endif
257 int len = buf_size;
258 int hi_ver, lo_ver;
259
260 if(c->pic.data[0])
261 avctx->release_buffer(avctx, &c->pic);
262
263 c->pic.reference = 1;
264 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
265 if(avctx->get_buffer(avctx, &c->pic) < 0){
266 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
267 return -1;
268 }
269
270 outptr = c->pic.data[0]; // Output image pointer
271
272 /* parse header */
273 c->flags = buf[0];
274 buf++; len--;
275 if(c->flags & ZMBV_KEYFRAME) {
276 hi_ver = buf[0];
277 lo_ver = buf[1];
278 c->comp = buf[2];
279 c->fmt = buf[3];
280 c->bw = buf[4];
281 c->bh = buf[5];
282
283 buf += 6;
284 len -= 6;
285 av_log(avctx, AV_LOG_DEBUG, "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
286 if(hi_ver != 0 || lo_ver != 1) {
287 av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver);
288 return -1;
289 }
290 if(c->bw == 0 || c->bh == 0) {
291 av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh);
292 }
293 if(c->comp != 0 && c->comp != 1) {
294 av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
295 return -1;
296 }
297 if(c->fmt != ZMBV_FMT_8BPP && c->fmt != ZMBV_FMT_24BPP) {
298 av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt);
299 return -1;
300 }
301 zret = inflateReset(&c->zstream);
302 if (zret != Z_OK) {
303 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
304 return -1;
305 }
306 if(c->fmt == ZMBV_FMT_8BPP) {
307 c->bpp = 8;
308 c->decode_intra = zmbv_decode_intra;
309 c->decode_xor = zmbv_decode_xor_8;
310 } else {
311 c->bpp = 24;
312 c->decode_intra = zmbv_decode_intra;
313 c->decode_xor = zmbv_decode_xor_24;
314 }
315 c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
316 c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
317 c->bx = (c->width + c->bw - 1) / c->bw;
318 c->by = (c->height+ c->bh - 1) / c->bh;
319 }
320
321 if(c->fmt == 0) {
322 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
323 return -1;
324 }
325
326 if(c->comp == 0) { //Uncompressed data
327 memcpy(c->decomp_buf, buf, len);
328 c->decomp_size = 1;
329 } else { // ZLIB-compressed data
330 #ifdef CONFIG_ZLIB
331 c->zstream.total_in = c->zstream.total_out = 0;
332 c->zstream.next_in = buf;
333 c->zstream.avail_in = len;
334 c->zstream.next_out = c->decomp_buf;
335 c->zstream.avail_out = c->decomp_size;
336 inflate(&c->zstream, Z_FINISH);
337 c->decomp_len = c->zstream.total_out;
338 #else
339 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
340 return -1;
341 #endif
342 }
343 if(c->flags & ZMBV_KEYFRAME) {
344 c->pic.key_frame = 1;
345 c->pic.pict_type = FF_I_TYPE;
346 c->decode_intra(c);
347 } else {
348 c->pic.key_frame = 0;
349 c->pic.pict_type = FF_P_TYPE;
350 c->decode_xor(c);
351 }
352
353 /* update frames */
354 {
355 uint8_t *out, *src;
356 int i, j;
357
358 out = c->pic.data[0];
359 src = c->cur;
360 for(j = 0; j < c->height; j++) {
361 for(i = 0; i < c->width; i++) {
362 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
363 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
364 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
365 *src++;
366 }
367 out += c->pic.linesize[0];
368 }
369 memcpy(c->prev, c->cur, c->width * c->height);
370 }
371 *data_size = sizeof(AVFrame);
372 *(AVFrame*)data = c->pic;
373
374 /* always report that the buffer was completely consumed */
375 return buf_size;
376 }
377
378
379
380 /*
381 *
382 * Init zmbv decoder
383 *
384 */
385 static int decode_init(AVCodecContext *avctx)
386 {
387 ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
388 int zret; // Zlib return code
389
390 c->avctx = avctx;
391 avctx->has_b_frames = 0;
392
393 c->pic.data[0] = NULL;
394 c->width = avctx->width;
395 c->height = avctx->height;
396
397 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
398 return 1;
399 }
400 c->bpp = avctx->bits_per_sample;
401
402 #ifdef CONFIG_ZLIB
403 // Needed if zlib unused or init aborted before inflateInit
404 memset(&(c->zstream), 0, sizeof(z_stream));
405 #else
406 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
407 return 1;
408 #endif
409 avctx->pix_fmt = PIX_FMT_RGB24;
410 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
411
412 /* Allocate decompression buffer */
413 if (c->decomp_size) {
414 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
415 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
416 return 1;
417 }
418 }
419
420 #ifdef CONFIG_ZLIB
421 c->zstream.zalloc = Z_NULL;
422 c->zstream.zfree = Z_NULL;
423 c->zstream.opaque = Z_NULL;
424 zret = inflateInit(&(c->zstream));
425 if (zret != Z_OK) {
426 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
427 return 1;
428 }
429 #endif
430
431 return 0;
432 }
433
434
435
436 /*
437 *
438 * Uninit zmbv decoder
439 *
440 */
441 static int decode_end(AVCodecContext *avctx)
442 {
443 ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
444
445 av_freep(&c->decomp_buf);
446
447 if (c->pic.data[0])
448 avctx->release_buffer(avctx, &c->pic);
449 #ifdef CONFIG_ZLIB
450 inflateEnd(&(c->zstream));
451 #endif
452 if(c->cur)
453 av_freep(&c->cur);
454 if(c->prev)
455 av_freep(&c->prev);
456
457 return 0;
458 }
459
460 AVCodec zmbv_decoder = {
461 "zmbv",
462 CODEC_TYPE_VIDEO,
463 CODEC_ID_ZMBV,
464 sizeof(ZmbvContext),
465 decode_init,
466 NULL,
467 decode_end,
468 decode_frame
469 };
470