FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 {
51 };
52 
53 /*
54  * Decoder context
55  */
56 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 stride;
69  int bw, bh, bx, by;
71  z_stream zstream;
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 
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 %"PTRDIFF_SPECIFIER" of %i bytes\n",
149  src-c->decomp_buf, c->decomp_len);
150  return 0;
151 }
152 
153 /**
154  * Decode XOR'ed frame - 15bpp and 16bpp version
155  */
156 
158 {
159  uint8_t *src = c->decomp_buf;
160  uint16_t *output, *prev;
161  int8_t *mvec;
162  int x, y;
163  int d, dx, dy, bw2, bh2;
164  int block;
165  int i, j;
166  int mx, my;
167 
168  output = (uint16_t*)c->cur;
169  prev = (uint16_t*)c->prev;
170 
171  mvec = (int8_t*)src;
172  src += ((c->bx * c->by * 2 + 3) & ~3);
173 
174  block = 0;
175  for (y = 0; y < c->height; y += c->bh) {
176  bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
177  for (x = 0; x < c->width; x += c->bw) {
178  uint16_t *out, *tprev;
179 
180  d = mvec[block] & 1;
181  dx = mvec[block] >> 1;
182  dy = mvec[block + 1] >> 1;
183  block += 2;
184 
185  bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
186 
187  /* copy block - motion vectors out of bounds are used to zero blocks */
188  out = output + x;
189  tprev = prev + x + dx + dy * c->width;
190  mx = x + dx;
191  my = y + dy;
192  for (j = 0; j < bh2; j++) {
193  if (my + j < 0 || my + j >= c->height) {
194  memset(out, 0, bw2 * 2);
195  } else {
196  for (i = 0; i < bw2; i++) {
197  if (mx + i < 0 || mx + i >= c->width)
198  out[i] = 0;
199  else
200  out[i] = tprev[i];
201  }
202  }
203  out += c->width;
204  tprev += c->width;
205  }
206 
207  if (d) { /* apply XOR'ed difference */
208  out = output + x;
209  for (j = 0; j < bh2; j++){
210  for (i = 0; i < bw2; i++) {
211  out[i] ^= *((uint16_t*)src);
212  src += 2;
213  }
214  out += c->width;
215  }
216  }
217  }
218  output += c->width * c->bh;
219  prev += c->width * c->bh;
220  }
221  if (src - c->decomp_buf != c->decomp_len)
222  av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
223  src-c->decomp_buf, c->decomp_len);
224  return 0;
225 }
226 
227 #ifdef ZMBV_ENABLE_24BPP
228 /**
229  * Decode XOR'ed frame - 24bpp version
230  */
231 
232 static int zmbv_decode_xor_24(ZmbvContext *c)
233 {
234  uint8_t *src = c->decomp_buf;
235  uint8_t *output, *prev;
236  int8_t *mvec;
237  int x, y;
238  int d, dx, dy, bw2, bh2;
239  int block;
240  int i, j;
241  int mx, my;
242  int stride;
243 
244  output = c->cur;
245  prev = c->prev;
246 
247  stride = c->width * 3;
248  mvec = (int8_t*)src;
249  src += ((c->bx * c->by * 2 + 3) & ~3);
250 
251  block = 0;
252  for (y = 0; y < c->height; y += c->bh) {
253  bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
254  for (x = 0; x < c->width; x += c->bw) {
255  uint8_t *out, *tprev;
256 
257  d = mvec[block] & 1;
258  dx = mvec[block] >> 1;
259  dy = mvec[block + 1] >> 1;
260  block += 2;
261 
262  bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
263 
264  /* copy block - motion vectors out of bounds are used to zero blocks */
265  out = output + x * 3;
266  tprev = prev + (x + dx) * 3 + dy * stride;
267  mx = x + dx;
268  my = y + dy;
269  for (j = 0; j < bh2; j++) {
270  if (my + j < 0 || my + j >= c->height) {
271  memset(out, 0, bw2 * 3);
272  } else {
273  for (i = 0; i < bw2; i++){
274  if (mx + i < 0 || mx + i >= c->width) {
275  out[i * 3 + 0] = 0;
276  out[i * 3 + 1] = 0;
277  out[i * 3 + 2] = 0;
278  } else {
279  out[i * 3 + 0] = tprev[i * 3 + 0];
280  out[i * 3 + 1] = tprev[i * 3 + 1];
281  out[i * 3 + 2] = tprev[i * 3 + 2];
282  }
283  }
284  }
285  out += stride;
286  tprev += stride;
287  }
288 
289  if (d) { /* apply XOR'ed difference */
290  out = output + x * 3;
291  for (j = 0; j < bh2; j++) {
292  for (i = 0; i < bw2; i++) {
293  out[i * 3 + 0] ^= *src++;
294  out[i * 3 + 1] ^= *src++;
295  out[i * 3 + 2] ^= *src++;
296  }
297  out += stride;
298  }
299  }
300  }
301  output += stride * c->bh;
302  prev += stride * c->bh;
303  }
304  if (src - c->decomp_buf != c->decomp_len)
305  av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n",
306  src-c->decomp_buf, c->decomp_len);
307  return 0;
308 }
309 #endif //ZMBV_ENABLE_24BPP
310 
311 /**
312  * Decode XOR'ed frame - 32bpp version
313  */
314 
316 {
317  uint8_t *src = c->decomp_buf;
318  uint32_t *output, *prev;
319  int8_t *mvec;
320  int x, y;
321  int d, dx, dy, bw2, bh2;
322  int block;
323  int i, j;
324  int mx, my;
325 
326  output = (uint32_t*)c->cur;
327  prev = (uint32_t*)c->prev;
328 
329  mvec = (int8_t*)src;
330  src += ((c->bx * c->by * 2 + 3) & ~3);
331 
332  block = 0;
333  for (y = 0; y < c->height; y += c->bh) {
334  bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
335  for (x = 0; x < c->width; x += c->bw) {
336  uint32_t *out, *tprev;
337 
338  d = mvec[block] & 1;
339  dx = mvec[block] >> 1;
340  dy = mvec[block + 1] >> 1;
341  block += 2;
342 
343  bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
344 
345  /* copy block - motion vectors out of bounds are used to zero blocks */
346  out = output + x;
347  tprev = prev + x + dx + dy * c->width;
348  mx = x + dx;
349  my = y + dy;
350  for (j = 0; j < bh2; j++) {
351  if (my + j < 0 || my + j >= c->height) {
352  memset(out, 0, bw2 * 4);
353  } else {
354  for (i = 0; i < bw2; i++){
355  if (mx + i < 0 || mx + i >= c->width)
356  out[i] = 0;
357  else
358  out[i] = tprev[i];
359  }
360  }
361  out += c->width;
362  tprev += c->width;
363  }
364 
365  if (d) { /* apply XOR'ed difference */
366  out = output + x;
367  for (j = 0; j < bh2; j++){
368  for (i = 0; i < bw2; i++) {
369  out[i] ^= *((uint32_t *) src);
370  src += 4;
371  }
372  out += c->width;
373  }
374  }
375  }
376  output += c->width * c->bh;
377  prev += c->width * c->bh;
378  }
379  if (src - c->decomp_buf != c->decomp_len)
380  av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
381  src-c->decomp_buf, c->decomp_len);
382  return 0;
383 }
384 
385 /**
386  * Decode intraframe
387  */
389 {
390  uint8_t *src = c->decomp_buf;
391 
392  /* make the palette available on the way out */
393  if (c->fmt == ZMBV_FMT_8BPP) {
394  memcpy(c->pal, src, 768);
395  src += 768;
396  }
397 
398  memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
399  return 0;
400 }
401 
402 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
403 {
404  AVFrame *frame = data;
405  const uint8_t *buf = avpkt->data;
406  int buf_size = avpkt->size;
407  ZmbvContext * const c = avctx->priv_data;
408  int zret = Z_OK; // Zlib return code
409  int len = buf_size;
410  int hi_ver, lo_ver, ret;
411 
412  /* parse header */
413  if (len < 1)
414  return AVERROR_INVALIDDATA;
415  c->flags = buf[0];
416  buf++; len--;
417  if (c->flags & ZMBV_KEYFRAME) {
418  void *decode_intra = NULL;
419  c->decode_intra= NULL;
420 
421  if (len < 6)
422  return AVERROR_INVALIDDATA;
423  hi_ver = buf[0];
424  lo_ver = buf[1];
425  c->comp = buf[2];
426  c->fmt = buf[3];
427  c->bw = buf[4];
428  c->bh = buf[5];
429  c->decode_intra = NULL;
430  c->decode_xor = NULL;
431 
432  buf += 6;
433  len -= 6;
434  av_log(avctx, AV_LOG_DEBUG,
435  "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
436  c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
437  if (hi_ver != 0 || lo_ver != 1) {
438  avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
439  return AVERROR_PATCHWELCOME;
440  }
441  if (c->bw == 0 || c->bh == 0) {
442  avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
443  return AVERROR_PATCHWELCOME;
444  }
445  if (c->comp != 0 && c->comp != 1) {
446  avpriv_request_sample(avctx, "Compression type %i", c->comp);
447  return AVERROR_PATCHWELCOME;
448  }
449 
450  switch (c->fmt) {
451  case ZMBV_FMT_8BPP:
452  c->bpp = 8;
453  decode_intra = zmbv_decode_intra;
455  avctx->pix_fmt = AV_PIX_FMT_PAL8;
456  c->stride = c->width;
457  break;
458  case ZMBV_FMT_15BPP:
459  case ZMBV_FMT_16BPP:
460  c->bpp = 16;
461  decode_intra = zmbv_decode_intra;
463  if (c->fmt == ZMBV_FMT_15BPP)
464  avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
465  else
466  avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
467  c->stride = c->width * 2;
468  break;
469 #ifdef ZMBV_ENABLE_24BPP
470  case ZMBV_FMT_24BPP:
471  c->bpp = 24;
472  decode_intra = zmbv_decode_intra;
473  c->decode_xor = zmbv_decode_xor_24;
474  avctx->pix_fmt = AV_PIX_FMT_RGB24;
475  c->stride = c->width * 3;
476  break;
477 #endif //ZMBV_ENABLE_24BPP
478  case ZMBV_FMT_32BPP:
479  c->bpp = 32;
480  decode_intra = zmbv_decode_intra;
482  avctx->pix_fmt = AV_PIX_FMT_BGR0;
483  c->stride = c->width * 4;
484  break;
485  default:
486  c->decode_xor = NULL;
487  avpriv_request_sample(avctx, "Format %i", c->fmt);
488  return AVERROR_PATCHWELCOME;
489  }
490 
491  zret = inflateReset(&c->zstream);
492  if (zret != Z_OK) {
493  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
494  return AVERROR_UNKNOWN;
495  }
496 
497  c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
498  c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
499  c->bx = (c->width + c->bw - 1) / c->bw;
500  c->by = (c->height+ c->bh - 1) / c->bh;
501  if (!c->cur || !c->prev)
502  return AVERROR(ENOMEM);
503  memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
504  memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
506  }
507 
508  if (!c->decode_intra) {
509  av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
510  return AVERROR_INVALIDDATA;
511  }
512 
513  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
514  return ret;
515 
516  if (c->comp == 0) { //Uncompressed data
517  if (c->decomp_size < len) {
518  av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
519  return AVERROR_INVALIDDATA;
520  }
521  memcpy(c->decomp_buf, buf, len);
522  } else { // ZLIB-compressed data
523  c->zstream.total_in = c->zstream.total_out = 0;
524  c->zstream.next_in = (uint8_t*)buf;
525  c->zstream.avail_in = len;
526  c->zstream.next_out = c->decomp_buf;
527  c->zstream.avail_out = c->decomp_size;
528  zret = inflate(&c->zstream, Z_SYNC_FLUSH);
529  if (zret != Z_OK && zret != Z_STREAM_END) {
530  av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
531  return AVERROR_INVALIDDATA;
532  }
533  c->decomp_len = c->zstream.total_out;
534  }
535  if (c->flags & ZMBV_KEYFRAME) {
536  frame->key_frame = 1;
537  frame->pict_type = AV_PICTURE_TYPE_I;
538  c->decode_intra(c);
539  } else {
540  frame->key_frame = 0;
541  frame->pict_type = AV_PICTURE_TYPE_P;
542  if (c->decomp_len)
543  c->decode_xor(c);
544  }
545 
546  /* update frames */
547  {
548  uint8_t *out, *src;
549  int j;
550 
551  out = frame->data[0];
552  src = c->cur;
553  switch (c->fmt) {
554  case ZMBV_FMT_8BPP:
555  for (j = 0; j < 256; j++)
556  AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 3]));
557  case ZMBV_FMT_15BPP:
558  case ZMBV_FMT_16BPP:
559 #ifdef ZMBV_ENABLE_24BPP
560  case ZMBV_FMT_24BPP:
561 #endif
562  case ZMBV_FMT_32BPP:
563  av_image_copy_plane(out, frame->linesize[0], src, c->stride,
564  c->stride, c->height);
565  break;
566  default:
567  av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
568  }
569  FFSWAP(uint8_t *, c->cur, c->prev);
570  }
571  *got_frame = 1;
572 
573  /* always report that the buffer was completely consumed */
574  return buf_size;
575 }
576 
578 {
579  ZmbvContext * const c = avctx->priv_data;
580  int zret; // Zlib return code
581 
582  c->avctx = avctx;
583 
584  c->width = avctx->width;
585  c->height = avctx->height;
586 
587  c->bpp = avctx->bits_per_coded_sample;
588 
589  // Needed if zlib unused or init aborted before inflateInit
590  memset(&c->zstream, 0, sizeof(z_stream));
591 
592  c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
593 
594  /* Allocate decompression buffer */
595  if (c->decomp_size) {
596  if (!(c->decomp_buf = av_mallocz(c->decomp_size))) {
597  av_log(avctx, AV_LOG_ERROR,
598  "Can't allocate decompression buffer.\n");
599  return AVERROR(ENOMEM);
600  }
601  }
602 
603  c->zstream.zalloc = Z_NULL;
604  c->zstream.zfree = Z_NULL;
605  c->zstream.opaque = Z_NULL;
606  zret = inflateInit(&c->zstream);
607  if (zret != Z_OK) {
608  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
609  return AVERROR_UNKNOWN;
610  }
611 
612  return 0;
613 }
614 
616 {
617  ZmbvContext * const c = avctx->priv_data;
618 
619  av_freep(&c->decomp_buf);
620 
621  inflateEnd(&c->zstream);
622  av_freep(&c->cur);
623  av_freep(&c->prev);
624 
625  return 0;
626 }
627 
629  .name = "zmbv",
630  .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
631  .type = AVMEDIA_TYPE_VIDEO,
632  .id = AV_CODEC_ID_ZMBV,
633  .priv_data_size = sizeof(ZmbvContext),
634  .init = decode_init,
635  .close = decode_end,
636  .decode = decode_frame,
637  .capabilities = CODEC_CAP_DR1,
638 };