libavcodec/zmbv.c
Go to the documentation of this file.
00001 /*
00002  * Zip Motion Blocks Video (ZMBV) decoder
00003  * Copyright (c) 2006 Konstantin Shishkov
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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  * Decoder context
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             /* copy block - motion vectors out of bounds are used to zero blocks */
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) { /* apply XOR'ed difference */
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             /* copy block - motion vectors out of bounds are used to zero blocks */
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) { /* apply XOR'ed difference */
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             /* copy block - motion vectors out of bounds are used to zero blocks */
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) { /* apply XOR'ed difference */
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             /* copy block - motion vectors out of bounds are used to zero blocks */
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) { /* apply XOR'ed difference */
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     /* make the palette available on the way out */
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; // Zlib return code
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     /* parse header */
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) { //Uncompressed data
00500          memcpy(c->decomp_buf, buf, len);
00501          c->decomp_size = 1;
00502      } else { // ZLIB-compressed data
00503         c->zstream.total_in = c->zstream.total_out = 0;
00504         c->zstream.next_in = buf;
00505         c->zstream.avail_in = len;
00506         c->zstream.next_out = c->decomp_buf;
00507         c->zstream.avail_out = c->decomp_size;
00508         inflate(&c->zstream, Z_FINISH);
00509         c->decomp_len = c->zstream.total_out;
00510     }
00511     if (c->flags & ZMBV_KEYFRAME) {
00512         c->pic.key_frame = 1;
00513         c->pic.pict_type = AV_PICTURE_TYPE_I;
00514         c->decode_intra(c);
00515     } else {
00516         c->pic.key_frame = 0;
00517         c->pic.pict_type = AV_PICTURE_TYPE_P;
00518         if (c->decomp_len)
00519             c->decode_xor(c);
00520     }
00521 
00522     /* update frames */
00523     {
00524         uint8_t *out, *src;
00525         int i, j;
00526 
00527         out = c->pic.data[0];
00528         src = c->cur;
00529         switch (c->fmt) {
00530         case ZMBV_FMT_8BPP:
00531             for (j = 0; j < c->height; j++) {
00532                 for (i = 0; i < c->width; i++) {
00533                     out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
00534                     out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
00535                     out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
00536                     src++;
00537                 }
00538                 out += c->pic.linesize[0];
00539             }
00540             break;
00541         case ZMBV_FMT_15BPP:
00542             for (j = 0; j < c->height; j++) {
00543                 for (i = 0; i < c->width; i++) {
00544                     uint16_t tmp = AV_RL16(src);
00545                     src += 2;
00546                     out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
00547                     out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
00548                     out[i * 3 + 2] = (tmp & 0x001F) << 3;
00549                 }
00550                 out += c->pic.linesize[0];
00551             }
00552             break;
00553         case ZMBV_FMT_16BPP:
00554             for (j = 0; j < c->height; j++) {
00555                 for (i = 0; i < c->width; i++) {
00556                     uint16_t tmp = AV_RL16(src);
00557                     src += 2;
00558                     out[i * 3 + 0] = (tmp & 0xF800) >> 8;
00559                     out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
00560                     out[i * 3 + 2] = (tmp & 0x001F) << 3;
00561                 }
00562                 out += c->pic.linesize[0];
00563             }
00564             break;
00565 #ifdef ZMBV_ENABLE_24BPP
00566         case ZMBV_FMT_24BPP:
00567             for (j = 0; j < c->height; j++) {
00568                 memcpy(out, src, c->width * 3);
00569                 src += c->width * 3;
00570                 out += c->pic.linesize[0];
00571             }
00572             break;
00573 #endif //ZMBV_ENABLE_24BPP
00574         case ZMBV_FMT_32BPP:
00575             for (j = 0; j < c->height; j++) {
00576                 for (i = 0; i < c->width; i++) {
00577                     uint32_t tmp = AV_RL32(src);
00578                     src += 4;
00579                     AV_WB24(out+(i*3), tmp);
00580                 }
00581                 out += c->pic.linesize[0];
00582             }
00583             break;
00584         default:
00585             av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
00586         }
00587         FFSWAP(uint8_t *, c->cur, c->prev);
00588     }
00589     *data_size = sizeof(AVFrame);
00590     *(AVFrame*)data = c->pic;
00591 
00592     /* always report that the buffer was completely consumed */
00593     return buf_size;
00594 }
00595 
00596 
00597 
00598 /*
00599  *
00600  * Init zmbv decoder
00601  *
00602  */
00603 static av_cold int decode_init(AVCodecContext *avctx)
00604 {
00605     ZmbvContext * const c = avctx->priv_data;
00606     int zret; // Zlib return code
00607 
00608     c->avctx = avctx;
00609 
00610     c->width = avctx->width;
00611     c->height = avctx->height;
00612 
00613     c->bpp = avctx->bits_per_coded_sample;
00614 
00615     // Needed if zlib unused or init aborted before inflateInit
00616     memset(&c->zstream, 0, sizeof(z_stream));
00617 
00618     avctx->pix_fmt = PIX_FMT_RGB24;
00619     c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
00620 
00621     /* Allocate decompression buffer */
00622     if (c->decomp_size) {
00623         if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
00624             av_log(avctx, AV_LOG_ERROR,
00625                    "Can't allocate decompression buffer.\n");
00626             return 1;
00627         }
00628     }
00629 
00630     c->zstream.zalloc = Z_NULL;
00631     c->zstream.zfree = Z_NULL;
00632     c->zstream.opaque = Z_NULL;
00633     zret = inflateInit(&c->zstream);
00634     if (zret != Z_OK) {
00635         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00636         return 1;
00637     }
00638 
00639     return 0;
00640 }
00641 
00642 
00643 
00644 /*
00645  *
00646  * Uninit zmbv decoder
00647  *
00648  */
00649 static av_cold int decode_end(AVCodecContext *avctx)
00650 {
00651     ZmbvContext * const c = avctx->priv_data;
00652 
00653     av_freep(&c->decomp_buf);
00654 
00655     if (c->pic.data[0])
00656         avctx->release_buffer(avctx, &c->pic);
00657     inflateEnd(&c->zstream);
00658     av_freep(&c->cur);
00659     av_freep(&c->prev);
00660 
00661     return 0;
00662 }
00663 
00664 AVCodec ff_zmbv_decoder = {
00665     .name           = "zmbv",
00666     .type           = AVMEDIA_TYPE_VIDEO,
00667     .id             = CODEC_ID_ZMBV,
00668     .priv_data_size = sizeof(ZmbvContext),
00669     .init           = decode_init,
00670     .close          = decode_end,
00671     .decode         = decode_frame,
00672     .capabilities   = CODEC_CAP_DR1,
00673     .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
00674 };
00675