zmbv.c
Go to the documentation of this file.
1 /*
2  * Zip Motion Blocks Video (ZMBV) decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avcodec.h"
33 #include "internal.h"
34 
35 #include <zlib.h>
36 
37 #define ZMBV_KEYFRAME 1
38 #define ZMBV_DELTAPAL 2
39 
40 enum ZmbvFormat {
50 };
51 
52 /*
53  * Decoder context
54  */
55 typedef struct ZmbvContext {
58 
59  int bpp;
60  unsigned int decomp_size;
62  uint8_t pal[768];
64  int width, height;
65  int fmt;
66  int comp;
67  int flags;
68  int bw, bh, bx, by;
70  z_stream zstream;
71  int (*decode_intra)(struct ZmbvContext *c);
72  int (*decode_xor)(struct ZmbvContext *c);
73 } ZmbvContext;
74 
80 {
81  uint8_t *src = c->decomp_buf;
82  uint8_t *output, *prev;
83  int8_t *mvec;
84  int x, y;
85  int d, dx, dy, bw2, bh2;
86  int block;
87  int i, j;
88  int mx, my;
89 
90  output = c->cur;
91  prev = c->prev;
92 
93  if (c->flags & ZMBV_DELTAPAL) {
94  for (i = 0; i < 768; i++)
95  c->pal[i] ^= *src++;
96  }
97 
98  mvec = (int8_t*)src;
99  src += ((c->bx * c->by * 2 + 3) & ~3);
100 
101  block = 0;
102  for (y = 0; y < c->height; y += c->bh) {
103  bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
104  for (x = 0; x < c->width; x += c->bw) {
105  uint8_t *out, *tprev;
106 
107  d = mvec[block] & 1;
108  dx = mvec[block] >> 1;
109  dy = mvec[block + 1] >> 1;
110  block += 2;
111 
112  bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
113 
114  /* copy block - motion vectors out of bounds are used to zero blocks */
115  out = output + x;
116  tprev = prev + x + dx + dy * c->width;
117  mx = x + dx;
118  my = y + dy;
119  for (j = 0; j < bh2; j++) {
120  if (my + j < 0 || my + j >= c->height) {
121  memset(out, 0, bw2);
122  } else {
123  for (i = 0; i < bw2; i++) {
124  if (mx + i < 0 || mx + i >= c->width)
125  out[i] = 0;
126  else
127  out[i] = tprev[i];
128  }
129  }
130  out += c->width;
131  tprev += c->width;
132  }
133 
134  if (d) { /* apply XOR'ed difference */
135  out = output + x;
136  for (j = 0; j < bh2; j++) {
137  for (i = 0; i < bw2; i++)
138  out[i] ^= *src++;
139  out += c->width;
140  }
141  }
142  }
143  output += c->width * c->bh;
144  prev += c->width * c->bh;
145  }
146  if (src - c->decomp_buf != c->decomp_len)
147  av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
148  src-c->decomp_buf, c->decomp_len);
149  return 0;
150 }
151 
157 {
158  uint8_t *src = c->decomp_buf;
159  uint16_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 
167  output = (uint16_t*)c->cur;
168  prev = (uint16_t*)c->prev;
169 
170  mvec = (int8_t*)src;
171  src += ((c->bx * c->by * 2 + 3) & ~3);
172 
173  block = 0;
174  for (y = 0; y < c->height; y += c->bh) {
175  bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
176  for (x = 0; x < c->width; x += c->bw) {
177  uint16_t *out, *tprev;
178 
179  d = mvec[block] & 1;
180  dx = mvec[block] >> 1;
181  dy = mvec[block + 1] >> 1;
182  block += 2;
183 
184  bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
185 
186  /* copy block - motion vectors out of bounds are used to zero blocks */
187  out = output + x;
188  tprev = prev + x + dx + dy * c->width;
189  mx = x + dx;
190  my = y + dy;
191  for (j = 0; j < bh2; j++) {
192  if (my + j < 0 || my + j >= c->height) {
193  memset(out, 0, bw2 * 2);
194  } else {
195  for (i = 0; i < bw2; i++) {
196  if (mx + i < 0 || mx + i >= c->width)
197  out[i] = 0;
198  else
199  out[i] = tprev[i];
200  }
201  }
202  out += c->width;
203  tprev += c->width;
204  }
205 
206  if (d) { /* apply XOR'ed difference */
207  out = output + x;
208  for (j = 0; j < bh2; j++){
209  for (i = 0; i < bw2; i++) {
210  out[i] ^= *((uint16_t*)src);
211  src += 2;
212  }
213  out += c->width;
214  }
215  }
216  }
217  output += c->width * c->bh;
218  prev += c->width * c->bh;
219  }
220  if (src - c->decomp_buf != c->decomp_len)
221  av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
222  src-c->decomp_buf, c->decomp_len);
223  return 0;
224 }
225 
226 #ifdef ZMBV_ENABLE_24BPP
227 
231 static int zmbv_decode_xor_24(ZmbvContext *c)
232 {
233  uint8_t *src = c->decomp_buf;
234  uint8_t *output, *prev;
235  int8_t *mvec;
236  int x, y;
237  int d, dx, dy, bw2, bh2;
238  int block;
239  int i, j;
240  int mx, my;
241  int stride;
242 
243  output = c->cur;
244  prev = c->prev;
245 
246  stride = c->width * 3;
247  mvec = (int8_t*)src;
248  src += ((c->bx * c->by * 2 + 3) & ~3);
249 
250  block = 0;
251  for (y = 0; y < c->height; y += c->bh) {
252  bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
253  for (x = 0; x < c->width; x += c->bw) {
254  uint8_t *out, *tprev;
255 
256  d = mvec[block] & 1;
257  dx = mvec[block] >> 1;
258  dy = mvec[block + 1] >> 1;
259  block += 2;
260 
261  bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
262 
263  /* copy block - motion vectors out of bounds are used to zero blocks */
264  out = output + x * 3;
265  tprev = prev + (x + dx) * 3 + dy * stride;
266  mx = x + dx;
267  my = y + dy;
268  for (j = 0; j < bh2; j++) {
269  if (my + j < 0 || my + j >= c->height) {
270  memset(out, 0, bw2 * 3);
271  } else {
272  for (i = 0; i < bw2; i++){
273  if (mx + i < 0 || mx + i >= c->width) {
274  out[i * 3 + 0] = 0;
275  out[i * 3 + 1] = 0;
276  out[i * 3 + 2] = 0;
277  } else {
278  out[i * 3 + 0] = tprev[i * 3 + 0];
279  out[i * 3 + 1] = tprev[i * 3 + 1];
280  out[i * 3 + 2] = tprev[i * 3 + 2];
281  }
282  }
283  }
284  out += stride;
285  tprev += stride;
286  }
287 
288  if (d) { /* apply XOR'ed difference */
289  out = output + x * 3;
290  for (j = 0; j < bh2; j++) {
291  for (i = 0; i < bw2; i++) {
292  out[i * 3 + 0] ^= *src++;
293  out[i * 3 + 1] ^= *src++;
294  out[i * 3 + 2] ^= *src++;
295  }
296  out += stride;
297  }
298  }
299  }
300  output += stride * c->bh;
301  prev += stride * c->bh;
302  }
303  if (src - c->decomp_buf != c->decomp_len)
304  av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n",
305  src-c->decomp_buf, c->decomp_len);
306  return 0;
307 }
308 #endif //ZMBV_ENABLE_24BPP
309 
315 {
316  uint8_t *src = c->decomp_buf;
317  uint32_t *output, *prev;
318  int8_t *mvec;
319  int x, y;
320  int d, dx, dy, bw2, bh2;
321  int block;
322  int i, j;
323  int mx, my;
324 
325  output = (uint32_t*)c->cur;
326  prev = (uint32_t*)c->prev;
327 
328  mvec = (int8_t*)src;
329  src += ((c->bx * c->by * 2 + 3) & ~3);
330 
331  block = 0;
332  for (y = 0; y < c->height; y += c->bh) {
333  bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
334  for (x = 0; x < c->width; x += c->bw) {
335  uint32_t *out, *tprev;
336 
337  d = mvec[block] & 1;
338  dx = mvec[block] >> 1;
339  dy = mvec[block + 1] >> 1;
340  block += 2;
341 
342  bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
343 
344  /* copy block - motion vectors out of bounds are used to zero blocks */
345  out = output + x;
346  tprev = prev + x + dx + dy * c->width;
347  mx = x + dx;
348  my = y + dy;
349  for (j = 0; j < bh2; j++) {
350  if (my + j < 0 || my + j >= c->height) {
351  memset(out, 0, bw2 * 4);
352  } else {
353  for (i = 0; i < bw2; i++){
354  if (mx + i < 0 || mx + i >= c->width)
355  out[i] = 0;
356  else
357  out[i] = tprev[i];
358  }
359  }
360  out += c->width;
361  tprev += c->width;
362  }
363 
364  if (d) { /* apply XOR'ed difference */
365  out = output + x;
366  for (j = 0; j < bh2; j++){
367  for (i = 0; i < bw2; i++) {
368  out[i] ^= *((uint32_t *) src);
369  src += 4;
370  }
371  out += c->width;
372  }
373  }
374  }
375  output += c->width * c->bh;
376  prev += c->width * c->bh;
377  }
378  if (src - c->decomp_buf != c->decomp_len)
379  av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
380  src-c->decomp_buf, c->decomp_len);
381  return 0;
382 }
383 
388 {
389  uint8_t *src = c->decomp_buf;
390 
391  /* make the palette available on the way out */
392  if (c->fmt == ZMBV_FMT_8BPP) {
393  memcpy(c->pal, src, 768);
394  src += 768;
395  }
396 
397  memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
398  return 0;
399 }
400 
401 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
402 {
403  const uint8_t *buf = avpkt->data;
404  int buf_size = avpkt->size;
405  ZmbvContext * const c = avctx->priv_data;
406  int zret = Z_OK; // Zlib return code
407  int len = buf_size;
408  int hi_ver, lo_ver, ret;
409  uint8_t *tmp;
410 
411  if (c->pic.data[0])
412  avctx->release_buffer(avctx, &c->pic);
413 
414  c->pic.reference = 1;
416  if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
417  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
418  return ret;
419  }
420 
421  /* parse header */
422  c->flags = buf[0];
423  buf++; len--;
424  if (c->flags & ZMBV_KEYFRAME) {
425  hi_ver = buf[0];
426  lo_ver = buf[1];
427  c->comp = buf[2];
428  c->fmt = buf[3];
429  c->bw = buf[4];
430  c->bh = buf[5];
431  c->decode_intra = NULL;
432  c->decode_xor = NULL;
433 
434  buf += 6;
435  len -= 6;
436  av_log(avctx, AV_LOG_DEBUG,
437  "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
438  c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
439  if (hi_ver != 0 || lo_ver != 1) {
440  av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
441  hi_ver, lo_ver);
442  return AVERROR_PATCHWELCOME;
443  }
444  if (c->bw == 0 || c->bh == 0) {
445  av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
446  c->bw, c->bh);
447  return AVERROR_PATCHWELCOME;
448  }
449  if (c->comp != 0 && c->comp != 1) {
450  av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
451  c->comp);
452  return AVERROR_PATCHWELCOME;
453  }
454 
455  switch (c->fmt) {
456  case ZMBV_FMT_8BPP:
457  c->bpp = 8;
460  break;
461  case ZMBV_FMT_15BPP:
462  case ZMBV_FMT_16BPP:
463  c->bpp = 16;
466  break;
467 #ifdef ZMBV_ENABLE_24BPP
468  case ZMBV_FMT_24BPP:
469  c->bpp = 24;
471  c->decode_xor = zmbv_decode_xor_24;
472  break;
473 #endif //ZMBV_ENABLE_24BPP
474  case ZMBV_FMT_32BPP:
475  c->bpp = 32;
478  break;
479  default:
480  c->decode_intra = NULL;
481  c->decode_xor = NULL;
482  av_log_ask_for_sample(avctx, "Unsupported (for now) format %i\n",
483  c->fmt);
484  return AVERROR_PATCHWELCOME;
485  }
486 
487  zret = inflateReset(&c->zstream);
488  if (zret != Z_OK) {
489  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
490  return -1;
491  }
492 
493  tmp = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
494  if (!tmp)
495  return AVERROR(ENOMEM);
496  c->cur = tmp;
497  tmp = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
498  if (!tmp)
499  return AVERROR(ENOMEM);
500  c->prev = tmp;
501  c->bx = (c->width + c->bw - 1) / c->bw;
502  c->by = (c->height + c->bh - 1) / c->bh;
503  }
504 
505  if (c->decode_intra == NULL) {
506  av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
507  return AVERROR_INVALIDDATA;
508  }
509 
510  if (c->comp == 0) { //Uncompressed data
511  if (c->decomp_size < len) {
512  av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
513  return AVERROR_INVALIDDATA;
514  }
515  memcpy(c->decomp_buf, buf, len);
516  } else { // ZLIB-compressed data
517  c->zstream.total_in = c->zstream.total_out = 0;
518  c->zstream.next_in = buf;
519  c->zstream.avail_in = len;
520  c->zstream.next_out = c->decomp_buf;
521  c->zstream.avail_out = c->decomp_size;
522  zret = inflate(&c->zstream, Z_SYNC_FLUSH);
523  if (zret != Z_OK && zret != Z_STREAM_END) {
524  av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
525  return AVERROR_INVALIDDATA;
526  }
527  c->decomp_len = c->zstream.total_out;
528  }
529  if (c->flags & ZMBV_KEYFRAME) {
530  c->pic.key_frame = 1;
532  c->decode_intra(c);
533  } else {
534  c->pic.key_frame = 0;
536  if (c->decomp_len)
537  c->decode_xor(c);
538  }
539 
540  /* update frames */
541  {
542  uint8_t *out, *src;
543  int i, j;
544 
545  out = c->pic.data[0];
546  src = c->cur;
547  switch (c->fmt) {
548  case ZMBV_FMT_8BPP:
549  for (j = 0; j < c->height; j++) {
550  for (i = 0; i < c->width; i++) {
551  out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
552  out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
553  out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
554  src++;
555  }
556  out += c->pic.linesize[0];
557  }
558  break;
559  case ZMBV_FMT_15BPP:
560  for (j = 0; j < c->height; j++) {
561  for (i = 0; i < c->width; i++) {
562  uint16_t tmp = AV_RL16(src);
563  src += 2;
564  out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
565  out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
566  out[i * 3 + 2] = (tmp & 0x001F) << 3;
567  }
568  out += c->pic.linesize[0];
569  }
570  break;
571  case ZMBV_FMT_16BPP:
572  for (j = 0; j < c->height; j++) {
573  for (i = 0; i < c->width; i++) {
574  uint16_t tmp = AV_RL16(src);
575  src += 2;
576  out[i * 3 + 0] = (tmp & 0xF800) >> 8;
577  out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
578  out[i * 3 + 2] = (tmp & 0x001F) << 3;
579  }
580  out += c->pic.linesize[0];
581  }
582  break;
583 #ifdef ZMBV_ENABLE_24BPP
584  case ZMBV_FMT_24BPP:
585  for (j = 0; j < c->height; j++) {
586  memcpy(out, src, c->width * 3);
587  src += c->width * 3;
588  out += c->pic.linesize[0];
589  }
590  break;
591 #endif //ZMBV_ENABLE_24BPP
592  case ZMBV_FMT_32BPP:
593  for (j = 0; j < c->height; j++) {
594  for (i = 0; i < c->width; i++) {
595  uint32_t tmp = AV_RL32(src);
596  src += 4;
597  AV_WB24(out+(i*3), tmp);
598  }
599  out += c->pic.linesize[0];
600  }
601  break;
602  default:
603  av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
604  }
605  FFSWAP(uint8_t *, c->cur, c->prev);
606  }
607  *got_frame = 1;
608  *(AVFrame*)data = c->pic;
609 
610  /* always report that the buffer was completely consumed */
611  return buf_size;
612 }
613 
615 {
616  ZmbvContext * const c = avctx->priv_data;
617  int zret; // Zlib return code
618 
619  c->avctx = avctx;
620 
621  c->width = avctx->width;
622  c->height = avctx->height;
623 
624  c->bpp = avctx->bits_per_coded_sample;
625 
626  // Needed if zlib unused or init aborted before inflateInit
627  memset(&c->zstream, 0, sizeof(z_stream));
628 
629  avctx->pix_fmt = AV_PIX_FMT_RGB24;
630  c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
631 
632  /* Allocate decompression buffer */
633  if (c->decomp_size) {
634  if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
635  av_log(avctx, AV_LOG_ERROR,
636  "Can't allocate decompression buffer.\n");
637  return AVERROR(ENOMEM);
638  }
639  }
640 
641  c->zstream.zalloc = Z_NULL;
642  c->zstream.zfree = Z_NULL;
643  c->zstream.opaque = Z_NULL;
644  zret = inflateInit(&c->zstream);
645  if (zret != Z_OK) {
646  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
647  return -1;
648  }
649 
650  return 0;
651 }
652 
654 {
655  ZmbvContext * const c = avctx->priv_data;
656 
657  av_freep(&c->decomp_buf);
658 
659  if (c->pic.data[0])
660  avctx->release_buffer(avctx, &c->pic);
661  inflateEnd(&c->zstream);
662  av_freep(&c->cur);
663  av_freep(&c->prev);
664 
665  return 0;
666 }
667 
669  .name = "zmbv",
670  .type = AVMEDIA_TYPE_VIDEO,
671  .id = AV_CODEC_ID_ZMBV,
672  .priv_data_size = sizeof(ZmbvContext),
673  .init = decode_init,
674  .close = decode_end,
675  .decode = decode_frame,
676  .capabilities = CODEC_CAP_DR1,
677  .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
678 };