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