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