00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029
00030 #include "libavutil/common.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "avcodec.h"
00033
00034 #include <zlib.h>
00035
00036 #define ZMBV_KEYFRAME 1
00037 #define ZMBV_DELTAPAL 2
00038
00039 enum ZmbvFormat {
00040 ZMBV_FMT_NONE = 0,
00041 ZMBV_FMT_1BPP = 1,
00042 ZMBV_FMT_2BPP = 2,
00043 ZMBV_FMT_4BPP = 3,
00044 ZMBV_FMT_8BPP = 4,
00045 ZMBV_FMT_15BPP = 5,
00046 ZMBV_FMT_16BPP = 6,
00047 ZMBV_FMT_24BPP = 7,
00048 ZMBV_FMT_32BPP = 8
00049 };
00050
00051
00052
00053
00054 typedef struct ZmbvContext {
00055 AVCodecContext *avctx;
00056 AVFrame pic;
00057
00058 int bpp;
00059 unsigned int decomp_size;
00060 uint8_t* decomp_buf;
00061 uint8_t pal[768];
00062 uint8_t *prev, *cur;
00063 int width, height;
00064 int fmt;
00065 int comp;
00066 int flags;
00067 int bw, bh, bx, by;
00068 int decomp_len;
00069 z_stream zstream;
00070 int (*decode_intra)(struct ZmbvContext *c);
00071 int (*decode_xor)(struct ZmbvContext *c);
00072 } ZmbvContext;
00073
00078 static int zmbv_decode_xor_8(ZmbvContext *c)
00079 {
00080 uint8_t *src = c->decomp_buf;
00081 uint8_t *output, *prev;
00082 int8_t *mvec;
00083 int x, y;
00084 int d, dx, dy, bw2, bh2;
00085 int block;
00086 int i, j;
00087 int mx, my;
00088
00089 output = c->cur;
00090 prev = c->prev;
00091
00092 if (c->flags & ZMBV_DELTAPAL) {
00093 for (i = 0; i < 768; i++)
00094 c->pal[i] ^= *src++;
00095 }
00096
00097 mvec = (int8_t*)src;
00098 src += ((c->bx * c->by * 2 + 3) & ~3);
00099
00100 block = 0;
00101 for (y = 0; y < c->height; y += c->bh) {
00102 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
00103 for (x = 0; x < c->width; x += c->bw) {
00104 uint8_t *out, *tprev;
00105
00106 d = mvec[block] & 1;
00107 dx = mvec[block] >> 1;
00108 dy = mvec[block + 1] >> 1;
00109 block += 2;
00110
00111 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
00112
00113
00114 out = output + x;
00115 tprev = prev + x + dx + dy * c->width;
00116 mx = x + dx;
00117 my = y + dy;
00118 for (j = 0; j < bh2; j++) {
00119 if (my + j < 0 || my + j >= c->height) {
00120 memset(out, 0, bw2);
00121 } else {
00122 for (i = 0; i < bw2; i++) {
00123 if (mx + i < 0 || mx + i >= c->width)
00124 out[i] = 0;
00125 else
00126 out[i] = tprev[i];
00127 }
00128 }
00129 out += c->width;
00130 tprev += c->width;
00131 }
00132
00133 if (d) {
00134 out = output + x;
00135 for (j = 0; j < bh2; j++) {
00136 for (i = 0; i < bw2; i++)
00137 out[i] ^= *src++;
00138 out += c->width;
00139 }
00140 }
00141 }
00142 output += c->width * c->bh;
00143 prev += c->width * c->bh;
00144 }
00145 if (src - c->decomp_buf != c->decomp_len)
00146 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
00147 src-c->decomp_buf, c->decomp_len);
00148 return 0;
00149 }
00150
00155 static int zmbv_decode_xor_16(ZmbvContext *c)
00156 {
00157 uint8_t *src = c->decomp_buf;
00158 uint16_t *output, *prev;
00159 int8_t *mvec;
00160 int x, y;
00161 int d, dx, dy, bw2, bh2;
00162 int block;
00163 int i, j;
00164 int mx, my;
00165
00166 output = (uint16_t*)c->cur;
00167 prev = (uint16_t*)c->prev;
00168
00169 mvec = (int8_t*)src;
00170 src += ((c->bx * c->by * 2 + 3) & ~3);
00171
00172 block = 0;
00173 for (y = 0; y < c->height; y += c->bh) {
00174 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
00175 for (x = 0; x < c->width; x += c->bw) {
00176 uint16_t *out, *tprev;
00177
00178 d = mvec[block] & 1;
00179 dx = mvec[block] >> 1;
00180 dy = mvec[block + 1] >> 1;
00181 block += 2;
00182
00183 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
00184
00185
00186 out = output + x;
00187 tprev = prev + x + dx + dy * c->width;
00188 mx = x + dx;
00189 my = y + dy;
00190 for (j = 0; j < bh2; j++) {
00191 if (my + j < 0 || my + j >= c->height) {
00192 memset(out, 0, bw2 * 2);
00193 } else {
00194 for (i = 0; i < bw2; i++) {
00195 if (mx + i < 0 || mx + i >= c->width)
00196 out[i] = 0;
00197 else
00198 out[i] = tprev[i];
00199 }
00200 }
00201 out += c->width;
00202 tprev += c->width;
00203 }
00204
00205 if (d) {
00206 out = output + x;
00207 for (j = 0; j < bh2; j++){
00208 for (i = 0; i < bw2; i++) {
00209 out[i] ^= *((uint16_t*)src);
00210 src += 2;
00211 }
00212 out += c->width;
00213 }
00214 }
00215 }
00216 output += c->width * c->bh;
00217 prev += c->width * c->bh;
00218 }
00219 if (src - c->decomp_buf != c->decomp_len)
00220 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
00221 src-c->decomp_buf, c->decomp_len);
00222 return 0;
00223 }
00224
00225 #ifdef ZMBV_ENABLE_24BPP
00226
00230 static int zmbv_decode_xor_24(ZmbvContext *c)
00231 {
00232 uint8_t *src = c->decomp_buf;
00233 uint8_t *output, *prev;
00234 int8_t *mvec;
00235 int x, y;
00236 int d, dx, dy, bw2, bh2;
00237 int block;
00238 int i, j;
00239 int mx, my;
00240 int stride;
00241
00242 output = c->cur;
00243 prev = c->prev;
00244
00245 stride = c->width * 3;
00246 mvec = (int8_t*)src;
00247 src += ((c->bx * c->by * 2 + 3) & ~3);
00248
00249 block = 0;
00250 for (y = 0; y < c->height; y += c->bh) {
00251 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
00252 for (x = 0; x < c->width; x += c->bw) {
00253 uint8_t *out, *tprev;
00254
00255 d = mvec[block] & 1;
00256 dx = mvec[block] >> 1;
00257 dy = mvec[block + 1] >> 1;
00258 block += 2;
00259
00260 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
00261
00262
00263 out = output + x * 3;
00264 tprev = prev + (x + dx) * 3 + dy * stride;
00265 mx = x + dx;
00266 my = y + dy;
00267 for (j = 0; j < bh2; j++) {
00268 if (my + j < 0 || my + j >= c->height) {
00269 memset(out, 0, bw2 * 3);
00270 } else {
00271 for (i = 0; i < bw2; i++){
00272 if (mx + i < 0 || mx + i >= c->width) {
00273 out[i * 3 + 0] = 0;
00274 out[i * 3 + 1] = 0;
00275 out[i * 3 + 2] = 0;
00276 } else {
00277 out[i * 3 + 0] = tprev[i * 3 + 0];
00278 out[i * 3 + 1] = tprev[i * 3 + 1];
00279 out[i * 3 + 2] = tprev[i * 3 + 2];
00280 }
00281 }
00282 }
00283 out += stride;
00284 tprev += stride;
00285 }
00286
00287 if (d) {
00288 out = output + x * 3;
00289 for (j = 0; j < bh2; j++) {
00290 for (i = 0; i < bw2; i++) {
00291 out[i * 3 + 0] ^= *src++;
00292 out[i * 3 + 1] ^= *src++;
00293 out[i * 3 + 2] ^= *src++;
00294 }
00295 out += stride;
00296 }
00297 }
00298 }
00299 output += stride * c->bh;
00300 prev += stride * c->bh;
00301 }
00302 if (src - c->decomp_buf != c->decomp_len)
00303 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n",
00304 src-c->decomp_buf, c->decomp_len);
00305 return 0;
00306 }
00307 #endif //ZMBV_ENABLE_24BPP
00308
00313 static int zmbv_decode_xor_32(ZmbvContext *c)
00314 {
00315 uint8_t *src = c->decomp_buf;
00316 uint32_t *output, *prev;
00317 int8_t *mvec;
00318 int x, y;
00319 int d, dx, dy, bw2, bh2;
00320 int block;
00321 int i, j;
00322 int mx, my;
00323
00324 output = (uint32_t*)c->cur;
00325 prev = (uint32_t*)c->prev;
00326
00327 mvec = (int8_t*)src;
00328 src += ((c->bx * c->by * 2 + 3) & ~3);
00329
00330 block = 0;
00331 for (y = 0; y < c->height; y += c->bh) {
00332 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
00333 for (x = 0; x < c->width; x += c->bw) {
00334 uint32_t *out, *tprev;
00335
00336 d = mvec[block] & 1;
00337 dx = mvec[block] >> 1;
00338 dy = mvec[block + 1] >> 1;
00339 block += 2;
00340
00341 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
00342
00343
00344 out = output + x;
00345 tprev = prev + x + dx + dy * c->width;
00346 mx = x + dx;
00347 my = y + dy;
00348 for (j = 0; j < bh2; j++) {
00349 if (my + j < 0 || my + j >= c->height) {
00350 memset(out, 0, bw2 * 4);
00351 } else {
00352 for (i = 0; i < bw2; i++){
00353 if (mx + i < 0 || mx + i >= c->width)
00354 out[i] = 0;
00355 else
00356 out[i] = tprev[i];
00357 }
00358 }
00359 out += c->width;
00360 tprev += c->width;
00361 }
00362
00363 if (d) {
00364 out = output + x;
00365 for (j = 0; j < bh2; j++){
00366 for (i = 0; i < bw2; i++) {
00367 out[i] ^= *((uint32_t *) src);
00368 src += 4;
00369 }
00370 out += c->width;
00371 }
00372 }
00373 }
00374 output += c->width * c->bh;
00375 prev += c->width * c->bh;
00376 }
00377 if (src - c->decomp_buf != c->decomp_len)
00378 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
00379 src-c->decomp_buf, c->decomp_len);
00380 return 0;
00381 }
00382
00386 static int zmbv_decode_intra(ZmbvContext *c)
00387 {
00388 uint8_t *src = c->decomp_buf;
00389
00390
00391 if (c->fmt == ZMBV_FMT_8BPP) {
00392 memcpy(c->pal, src, 768);
00393 src += 768;
00394 }
00395
00396 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
00397 return 0;
00398 }
00399
00400 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00401 {
00402 const uint8_t *buf = avpkt->data;
00403 int buf_size = avpkt->size;
00404 ZmbvContext * const c = avctx->priv_data;
00405 int zret = Z_OK;
00406 int len = buf_size;
00407 int hi_ver, lo_ver, ret;
00408
00409 if (c->pic.data[0])
00410 avctx->release_buffer(avctx, &c->pic);
00411
00412 c->pic.reference = 3;
00413 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00414 if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
00415 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00416 return ret;
00417 }
00418
00419
00420 c->flags = buf[0];
00421 buf++; len--;
00422 if (c->flags & ZMBV_KEYFRAME) {
00423 void *decode_intra = NULL;
00424 c->decode_intra= NULL;
00425 hi_ver = buf[0];
00426 lo_ver = buf[1];
00427 c->comp = buf[2];
00428 c->fmt = buf[3];
00429 c->bw = buf[4];
00430 c->bh = buf[5];
00431
00432 buf += 6;
00433 len -= 6;
00434 av_log(avctx, AV_LOG_DEBUG,
00435 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
00436 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
00437 if (hi_ver != 0 || lo_ver != 1) {
00438 av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
00439 hi_ver, lo_ver);
00440 return AVERROR_PATCHWELCOME;
00441 }
00442 if (c->bw == 0 || c->bh == 0) {
00443 av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
00444 c->bw, c->bh);
00445 return AVERROR_PATCHWELCOME;
00446 }
00447 if (c->comp != 0 && c->comp != 1) {
00448 av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
00449 c->comp);
00450 return AVERROR_PATCHWELCOME;
00451 }
00452
00453 switch (c->fmt) {
00454 case ZMBV_FMT_8BPP:
00455 c->bpp = 8;
00456 decode_intra = zmbv_decode_intra;
00457 c->decode_xor = zmbv_decode_xor_8;
00458 break;
00459 case ZMBV_FMT_15BPP:
00460 case ZMBV_FMT_16BPP:
00461 c->bpp = 16;
00462 decode_intra = zmbv_decode_intra;
00463 c->decode_xor = zmbv_decode_xor_16;
00464 break;
00465 #ifdef ZMBV_ENABLE_24BPP
00466 case ZMBV_FMT_24BPP:
00467 c->bpp = 24;
00468 decode_intra = zmbv_decode_intra;
00469 c->decode_xor = zmbv_decode_xor_24;
00470 break;
00471 #endif //ZMBV_ENABLE_24BPP
00472 case ZMBV_FMT_32BPP:
00473 c->bpp = 32;
00474 decode_intra = zmbv_decode_intra;
00475 c->decode_xor = zmbv_decode_xor_32;
00476 break;
00477 default:
00478 c->decode_xor = NULL;
00479 av_log_ask_for_sample(avctx, "Unsupported (for now) format %i\n",
00480 c->fmt);
00481 return AVERROR_PATCHWELCOME;
00482 }
00483
00484 zret = inflateReset(&c->zstream);
00485 if (zret != Z_OK) {
00486 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
00487 return -1;
00488 }
00489
00490 c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
00491 c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
00492 c->bx = (c->width + c->bw - 1) / c->bw;
00493 c->by = (c->height+ c->bh - 1) / c->bh;
00494 if (!c->cur || !c->prev)
00495 return -1;
00496 c->decode_intra= decode_intra;
00497 }
00498
00499 if (c->decode_intra == NULL) {
00500 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
00501 return AVERROR_INVALIDDATA;
00502 }
00503
00504 if (c->comp == 0) {
00505 memcpy(c->decomp_buf, buf, len);
00506 c->decomp_size = 1;
00507 } else {
00508 c->zstream.total_in = c->zstream.total_out = 0;
00509 c->zstream.next_in = (uint8_t*)buf;
00510 c->zstream.avail_in = len;
00511 c->zstream.next_out = c->decomp_buf;
00512 c->zstream.avail_out = c->decomp_size;
00513 zret = inflate(&c->zstream, Z_SYNC_FLUSH);
00514 if (zret != Z_OK && zret != Z_STREAM_END) {
00515 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
00516 return AVERROR_INVALIDDATA;
00517 }
00518 c->decomp_len = c->zstream.total_out;
00519 }
00520 if (c->flags & ZMBV_KEYFRAME) {
00521 c->pic.key_frame = 1;
00522 c->pic.pict_type = AV_PICTURE_TYPE_I;
00523 c->decode_intra(c);
00524 } else {
00525 c->pic.key_frame = 0;
00526 c->pic.pict_type = AV_PICTURE_TYPE_P;
00527 if (c->decomp_len)
00528 c->decode_xor(c);
00529 }
00530
00531
00532 {
00533 uint8_t *out, *src;
00534 int i, j;
00535
00536 out = c->pic.data[0];
00537 src = c->cur;
00538 switch (c->fmt) {
00539 case ZMBV_FMT_8BPP:
00540 for (j = 0; j < c->height; j++) {
00541 for (i = 0; i < c->width; i++) {
00542 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
00543 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
00544 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
00545 src++;
00546 }
00547 out += c->pic.linesize[0];
00548 }
00549 break;
00550 case ZMBV_FMT_15BPP:
00551 for (j = 0; j < c->height; j++) {
00552 for (i = 0; i < c->width; i++) {
00553 uint16_t tmp = AV_RL16(src);
00554 src += 2;
00555 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
00556 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
00557 out[i * 3 + 2] = (tmp & 0x001F) << 3;
00558 }
00559 out += c->pic.linesize[0];
00560 }
00561 break;
00562 case ZMBV_FMT_16BPP:
00563 for (j = 0; j < c->height; j++) {
00564 for (i = 0; i < c->width; i++) {
00565 uint16_t tmp = AV_RL16(src);
00566 src += 2;
00567 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
00568 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
00569 out[i * 3 + 2] = (tmp & 0x001F) << 3;
00570 }
00571 out += c->pic.linesize[0];
00572 }
00573 break;
00574 #ifdef ZMBV_ENABLE_24BPP
00575 case ZMBV_FMT_24BPP:
00576 for (j = 0; j < c->height; j++) {
00577 memcpy(out, src, c->width * 3);
00578 src += c->width * 3;
00579 out += c->pic.linesize[0];
00580 }
00581 break;
00582 #endif //ZMBV_ENABLE_24BPP
00583 case ZMBV_FMT_32BPP:
00584 for (j = 0; j < c->height; j++) {
00585 for (i = 0; i < c->width; i++) {
00586 uint32_t tmp = AV_RL32(src);
00587 src += 4;
00588 AV_WB24(out+(i*3), tmp);
00589 }
00590 out += c->pic.linesize[0];
00591 }
00592 break;
00593 default:
00594 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
00595 }
00596 FFSWAP(uint8_t *, c->cur, c->prev);
00597 }
00598 *data_size = sizeof(AVFrame);
00599 *(AVFrame*)data = c->pic;
00600
00601
00602 return buf_size;
00603 }
00604
00605
00606
00607
00608
00609
00610
00611
00612 static av_cold int decode_init(AVCodecContext *avctx)
00613 {
00614 ZmbvContext * const c = avctx->priv_data;
00615 int zret;
00616
00617 c->avctx = avctx;
00618
00619 c->width = avctx->width;
00620 c->height = avctx->height;
00621 avcodec_get_frame_defaults(&c->pic);
00622
00623 c->bpp = avctx->bits_per_coded_sample;
00624
00625
00626 memset(&c->zstream, 0, sizeof(z_stream));
00627
00628 avctx->pix_fmt = PIX_FMT_RGB24;
00629 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
00630
00631
00632 if (c->decomp_size) {
00633 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
00634 av_log(avctx, AV_LOG_ERROR,
00635 "Can't allocate decompression buffer.\n");
00636 return AVERROR(ENOMEM);
00637 }
00638 }
00639
00640 c->zstream.zalloc = Z_NULL;
00641 c->zstream.zfree = Z_NULL;
00642 c->zstream.opaque = Z_NULL;
00643 zret = inflateInit(&c->zstream);
00644 if (zret != Z_OK) {
00645 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00646 return -1;
00647 }
00648
00649 return 0;
00650 }
00651
00652
00653
00654
00655
00656
00657
00658
00659 static av_cold int decode_end(AVCodecContext *avctx)
00660 {
00661 ZmbvContext * const c = avctx->priv_data;
00662
00663 av_freep(&c->decomp_buf);
00664
00665 if (c->pic.data[0])
00666 avctx->release_buffer(avctx, &c->pic);
00667 inflateEnd(&c->zstream);
00668 av_freep(&c->cur);
00669 av_freep(&c->prev);
00670
00671 return 0;
00672 }
00673
00674 AVCodec ff_zmbv_decoder = {
00675 .name = "zmbv",
00676 .type = AVMEDIA_TYPE_VIDEO,
00677 .id = AV_CODEC_ID_ZMBV,
00678 .priv_data_size = sizeof(ZmbvContext),
00679 .init = decode_init,
00680 .close = decode_end,
00681 .decode = decode_frame,
00682 .capabilities = CODEC_CAP_DR1,
00683 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
00684 };