2 * Zip Motion Blocks Video (ZMBV) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
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.
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.
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
23 * Zip Motion Blocks Video decoder
36 #define ZMBV_KEYFRAME 1
37 #define ZMBV_DELTAPAL 2
54 typedef struct ZmbvContext {
55 AVCodecContext *avctx;
59 unsigned int decomp_size;
72 int (*decode_intra)(struct ZmbvContext *c);
73 int (*decode_xor)(struct ZmbvContext *c);
77 * Decode XOR'ed frame - 8bpp version
80 static int zmbv_decode_xor_8(ZmbvContext *c)
82 uint8_t *src = c->decomp_buf;
83 uint8_t *output, *prev;
86 int d, dx, dy, bw2, bh2;
94 if(c->flags & ZMBV_DELTAPAL){
95 for(i = 0; i < 768; i++)
100 src += ((c->bx * c->by * 2 + 3) & ~3);
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;
109 dx = mvec[block] >> 1;
110 dy = mvec[block + 1] >> 1;
113 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
115 /* copy block - motion vectors out of bounds are used to zero blocks */
117 tprev = prev + x + dx + dy * c->width;
120 for(j = 0; j < bh2; j++){
121 if((my + j < 0) || (my + j >= c->height)) {
124 for(i = 0; i < bw2; i++){
125 if((mx + i < 0) || (mx + i >= c->width))
135 if(d) { /* apply XOR'ed difference */
137 for(j = 0; j < bh2; j++){
138 for(i = 0; i < bw2; i++)
144 output += c->width * c->bh;
145 prev += c->width * c->bh;
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);
153 * Decode XOR'ed frame - 24bpp version
156 static int zmbv_decode_xor_24(ZmbvContext *c)
158 uint8_t *src = c->decomp_buf;
159 uint8_t *output, *prev;
162 int d, dx, dy, bw2, bh2;
171 stride = c->width * 3;
173 src += ((c->bx * c->by * 2 + 3) & ~3);
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;
182 dx = mvec[block] >> 1;
183 dy = mvec[block + 1] >> 1;
186 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
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;
193 for(j = 0; j < bh2; j++){
194 if((my + j < 0) || (my + j >= c->height)) {
195 memset(out, 0, bw2 * 3);
197 for(i = 0; i < bw2; i++){
198 if((mx + i < 0) || (mx + i >= c->width)) {
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];
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++;
225 output += stride * c->bh;
226 prev += stride * c->bh;
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);
236 static int zmbv_decode_intra(ZmbvContext *c)
238 uint8_t *src = c->decomp_buf;
240 /* make the palette available on the way out */
241 if (c->fmt == ZMBV_FMT_8BPP) {
242 memcpy(c->pal, src, 768);
246 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
250 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
252 ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
255 int zret = Z_OK; // Zlib return code
261 avctx->release_buffer(avctx, &c->pic);
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");
270 outptr = c->pic.data[0]; // Output image pointer
275 if(c->flags & ZMBV_KEYFRAME) {
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);
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);
293 if(c->comp != 0 && c->comp != 1) {
294 av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
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);
302 zret = inflateReset(&c->zstream);
304 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
308 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
310 #endif /* CONFIG_ZLIB */
311 if(c->fmt == ZMBV_FMT_8BPP) {
313 c->decode_intra = zmbv_decode_intra;
314 c->decode_xor = zmbv_decode_xor_8;
317 c->decode_intra = zmbv_decode_intra;
318 c->decode_xor = zmbv_decode_xor_24;
320 c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
321 c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
322 c->bx = (c->width + c->bw - 1) / c->bw;
323 c->by = (c->height+ c->bh - 1) / c->bh;
327 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
331 if(c->comp == 0) { //Uncompressed data
332 memcpy(c->decomp_buf, buf, len);
334 } else { // ZLIB-compressed data
336 c->zstream.total_in = c->zstream.total_out = 0;
337 c->zstream.next_in = buf;
338 c->zstream.avail_in = len;
339 c->zstream.next_out = c->decomp_buf;
340 c->zstream.avail_out = c->decomp_size;
341 inflate(&c->zstream, Z_FINISH);
342 c->decomp_len = c->zstream.total_out;
344 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
348 if(c->flags & ZMBV_KEYFRAME) {
349 c->pic.key_frame = 1;
350 c->pic.pict_type = FF_I_TYPE;
353 c->pic.key_frame = 0;
354 c->pic.pict_type = FF_P_TYPE;
363 out = c->pic.data[0];
365 for(j = 0; j < c->height; j++) {
366 for(i = 0; i < c->width; i++) {
367 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
368 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
369 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
372 out += c->pic.linesize[0];
374 memcpy(c->prev, c->cur, c->width * c->height);
376 *data_size = sizeof(AVFrame);
377 *(AVFrame*)data = c->pic;
379 /* always report that the buffer was completely consumed */
390 static int decode_init(AVCodecContext *avctx)
392 ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
393 int zret; // Zlib return code
396 avctx->has_b_frames = 0;
398 c->pic.data[0] = NULL;
399 c->width = avctx->width;
400 c->height = avctx->height;
402 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
405 c->bpp = avctx->bits_per_sample;
408 // Needed if zlib unused or init aborted before inflateInit
409 memset(&(c->zstream), 0, sizeof(z_stream));
411 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
414 avctx->pix_fmt = PIX_FMT_RGB24;
415 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
417 /* Allocate decompression buffer */
418 if (c->decomp_size) {
419 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
420 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
426 c->zstream.zalloc = Z_NULL;
427 c->zstream.zfree = Z_NULL;
428 c->zstream.opaque = Z_NULL;
429 zret = inflateInit(&(c->zstream));
431 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
443 * Uninit zmbv decoder
446 static int decode_end(AVCodecContext *avctx)
448 ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
450 av_freep(&c->decomp_buf);
453 avctx->release_buffer(avctx, &c->pic);
455 inflateEnd(&(c->zstream));
465 AVCodec zmbv_decoder = {