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 = 1;
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 hi_ver = buf[0];
00423 lo_ver = buf[1];
00424 c->comp = buf[2];
00425 c->fmt = buf[3];
00426 c->bw = buf[4];
00427 c->bh = buf[5];
00428
00429 buf += 6;
00430 len -= 6;
00431 av_log(avctx, AV_LOG_DEBUG,
00432 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
00433 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
00434 if (hi_ver != 0 || lo_ver != 1) {
00435 av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n",
00436 hi_ver, lo_ver);
00437 return -1;
00438 }
00439 if (c->bw == 0 || c->bh == 0) {
00440 av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n",
00441 c->bw, c->bh);
00442 return -1;
00443 }
00444 if (c->comp != 0 && c->comp != 1) {
00445 av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n",
00446 c->comp);
00447 return -1;
00448 }
00449
00450 switch (c->fmt) {
00451 case ZMBV_FMT_8BPP:
00452 c->bpp = 8;
00453 c->decode_intra = zmbv_decode_intra;
00454 c->decode_xor = zmbv_decode_xor_8;
00455 break;
00456 case ZMBV_FMT_15BPP:
00457 case ZMBV_FMT_16BPP:
00458 c->bpp = 16;
00459 c->decode_intra = zmbv_decode_intra;
00460 c->decode_xor = zmbv_decode_xor_16;
00461 break;
00462 #ifdef ZMBV_ENABLE_24BPP
00463 case ZMBV_FMT_24BPP:
00464 c->bpp = 24;
00465 c->decode_intra = zmbv_decode_intra;
00466 c->decode_xor = zmbv_decode_xor_24;
00467 break;
00468 #endif //ZMBV_ENABLE_24BPP
00469 case ZMBV_FMT_32BPP:
00470 c->bpp = 32;
00471 c->decode_intra = zmbv_decode_intra;
00472 c->decode_xor = zmbv_decode_xor_32;
00473 break;
00474 default:
00475 c->decode_intra = NULL;
00476 c->decode_xor = NULL;
00477 av_log(avctx, AV_LOG_ERROR,
00478 "Unsupported (for now) format %i\n", c->fmt);
00479 return -1;
00480 }
00481
00482 zret = inflateReset(&c->zstream);
00483 if (zret != Z_OK) {
00484 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
00485 return -1;
00486 }
00487
00488 c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
00489 c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
00490 c->bx = (c->width + c->bw - 1) / c->bw;
00491 c->by = (c->height+ c->bh - 1) / c->bh;
00492 }
00493
00494 if (c->decode_intra == NULL) {
00495 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
00496 return -1;
00497 }
00498
00499 if (c->comp == 0) {
00500 if (c->decomp_size < len) {
00501 av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
00502 return AVERROR_INVALIDDATA;
00503 }
00504 memcpy(c->decomp_buf, buf, len);
00505 } else {
00506 c->zstream.total_in = c->zstream.total_out = 0;
00507 c->zstream.next_in = buf;
00508 c->zstream.avail_in = len;
00509 c->zstream.next_out = c->decomp_buf;
00510 c->zstream.avail_out = c->decomp_size;
00511 inflate(&c->zstream, Z_FINISH);
00512 c->decomp_len = c->zstream.total_out;
00513 }
00514 if (c->flags & ZMBV_KEYFRAME) {
00515 c->pic.key_frame = 1;
00516 c->pic.pict_type = AV_PICTURE_TYPE_I;
00517 c->decode_intra(c);
00518 } else {
00519 c->pic.key_frame = 0;
00520 c->pic.pict_type = AV_PICTURE_TYPE_P;
00521 if (c->decomp_len)
00522 c->decode_xor(c);
00523 }
00524
00525
00526 {
00527 uint8_t *out, *src;
00528 int i, j;
00529
00530 out = c->pic.data[0];
00531 src = c->cur;
00532 switch (c->fmt) {
00533 case ZMBV_FMT_8BPP:
00534 for (j = 0; j < c->height; j++) {
00535 for (i = 0; i < c->width; i++) {
00536 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
00537 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
00538 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
00539 src++;
00540 }
00541 out += c->pic.linesize[0];
00542 }
00543 break;
00544 case ZMBV_FMT_15BPP:
00545 for (j = 0; j < c->height; j++) {
00546 for (i = 0; i < c->width; i++) {
00547 uint16_t tmp = AV_RL16(src);
00548 src += 2;
00549 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
00550 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
00551 out[i * 3 + 2] = (tmp & 0x001F) << 3;
00552 }
00553 out += c->pic.linesize[0];
00554 }
00555 break;
00556 case ZMBV_FMT_16BPP:
00557 for (j = 0; j < c->height; j++) {
00558 for (i = 0; i < c->width; i++) {
00559 uint16_t tmp = AV_RL16(src);
00560 src += 2;
00561 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
00562 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
00563 out[i * 3 + 2] = (tmp & 0x001F) << 3;
00564 }
00565 out += c->pic.linesize[0];
00566 }
00567 break;
00568 #ifdef ZMBV_ENABLE_24BPP
00569 case ZMBV_FMT_24BPP:
00570 for (j = 0; j < c->height; j++) {
00571 memcpy(out, src, c->width * 3);
00572 src += c->width * 3;
00573 out += c->pic.linesize[0];
00574 }
00575 break;
00576 #endif //ZMBV_ENABLE_24BPP
00577 case ZMBV_FMT_32BPP:
00578 for (j = 0; j < c->height; j++) {
00579 for (i = 0; i < c->width; i++) {
00580 uint32_t tmp = AV_RL32(src);
00581 src += 4;
00582 AV_WB24(out+(i*3), tmp);
00583 }
00584 out += c->pic.linesize[0];
00585 }
00586 break;
00587 default:
00588 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
00589 }
00590 FFSWAP(uint8_t *, c->cur, c->prev);
00591 }
00592 *data_size = sizeof(AVFrame);
00593 *(AVFrame*)data = c->pic;
00594
00595
00596 return buf_size;
00597 }
00598
00599
00600
00601
00602
00603
00604
00605
00606 static av_cold int decode_init(AVCodecContext *avctx)
00607 {
00608 ZmbvContext * const c = avctx->priv_data;
00609 int zret;
00610
00611 c->avctx = avctx;
00612
00613 c->width = avctx->width;
00614 c->height = avctx->height;
00615
00616 c->bpp = avctx->bits_per_coded_sample;
00617
00618
00619 memset(&c->zstream, 0, sizeof(z_stream));
00620
00621 avctx->pix_fmt = PIX_FMT_RGB24;
00622 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
00623
00624
00625 if (c->decomp_size) {
00626 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
00627 av_log(avctx, AV_LOG_ERROR,
00628 "Can't allocate decompression buffer.\n");
00629 return 1;
00630 }
00631 }
00632
00633 c->zstream.zalloc = Z_NULL;
00634 c->zstream.zfree = Z_NULL;
00635 c->zstream.opaque = Z_NULL;
00636 zret = inflateInit(&c->zstream);
00637 if (zret != Z_OK) {
00638 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00639 return 1;
00640 }
00641
00642 return 0;
00643 }
00644
00645
00646
00647
00648
00649
00650
00651
00652 static av_cold int decode_end(AVCodecContext *avctx)
00653 {
00654 ZmbvContext * const c = avctx->priv_data;
00655
00656 av_freep(&c->decomp_buf);
00657
00658 if (c->pic.data[0])
00659 avctx->release_buffer(avctx, &c->pic);
00660 inflateEnd(&c->zstream);
00661 av_freep(&c->cur);
00662 av_freep(&c->prev);
00663
00664 return 0;
00665 }
00666
00667 AVCodec ff_zmbv_decoder = {
00668 .name = "zmbv",
00669 .type = AVMEDIA_TYPE_VIDEO,
00670 .id = CODEC_ID_ZMBV,
00671 .priv_data_size = sizeof(ZmbvContext),
00672 .init = decode_init,
00673 .close = decode_end,
00674 .decode = decode_frame,
00675 .capabilities = CODEC_CAP_DR1,
00676 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
00677 };
00678