Libav 0.7.1
libavcodec/kmvc.c
Go to the documentation of this file.
00001 /*
00002  * KMVC 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 "avcodec.h"
00031 #include "bytestream.h"
00032 
00033 #define KMVC_KEYFRAME 0x80
00034 #define KMVC_PALETTE  0x40
00035 #define KMVC_METHOD   0x0F
00036 
00037 /*
00038  * Decoder context
00039  */
00040 typedef struct KmvcContext {
00041     AVCodecContext *avctx;
00042     AVFrame pic;
00043 
00044     int setpal;
00045     int palsize;
00046     uint32_t pal[256];
00047     uint8_t *cur, *prev;
00048     uint8_t *frm0, *frm1;
00049 } KmvcContext;
00050 
00051 typedef struct BitBuf {
00052     int bits;
00053     int bitbuf;
00054 } BitBuf;
00055 
00056 #define BLK(data, x, y)  data[(x) + (y) * 320]
00057 
00058 #define kmvc_init_getbits(bb, src)  bb.bits = 7; bb.bitbuf = *src++;
00059 
00060 #define kmvc_getbit(bb, src, res) {\
00061     res = 0; \
00062     if (bb.bitbuf & (1 << bb.bits)) res = 1; \
00063     bb.bits--; \
00064     if(bb.bits == -1) { \
00065         bb.bitbuf = *src++; \
00066         bb.bits = 7; \
00067     } \
00068 }
00069 
00070 static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int w, int h)
00071 {
00072     BitBuf bb;
00073     int res, val;
00074     int i, j;
00075     int bx, by;
00076     int l0x, l1x, l0y, l1y;
00077     int mx, my;
00078 
00079     kmvc_init_getbits(bb, src);
00080 
00081     for (by = 0; by < h; by += 8)
00082         for (bx = 0; bx < w; bx += 8) {
00083             kmvc_getbit(bb, src, res);
00084             if (!res) {         // fill whole 8x8 block
00085                 val = *src++;
00086                 for (i = 0; i < 64; i++)
00087                     BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
00088             } else {            // handle four 4x4 subblocks
00089                 for (i = 0; i < 4; i++) {
00090                     l0x = bx + (i & 1) * 4;
00091                     l0y = by + (i & 2) * 2;
00092                     kmvc_getbit(bb, src, res);
00093                     if (!res) {
00094                         kmvc_getbit(bb, src, res);
00095                         if (!res) {     // fill whole 4x4 block
00096                             val = *src++;
00097                             for (j = 0; j < 16; j++)
00098                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
00099                         } else {        // copy block from already decoded place
00100                             val = *src++;
00101                             mx = val & 0xF;
00102                             my = val >> 4;
00103                             for (j = 0; j < 16; j++)
00104                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
00105                                     BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
00106                         }
00107                     } else {    // descend to 2x2 sub-sub-blocks
00108                         for (j = 0; j < 4; j++) {
00109                             l1x = l0x + (j & 1) * 2;
00110                             l1y = l0y + (j & 2);
00111                             kmvc_getbit(bb, src, res);
00112                             if (!res) {
00113                                 kmvc_getbit(bb, src, res);
00114                                 if (!res) {     // fill whole 2x2 block
00115                                     val = *src++;
00116                                     BLK(ctx->cur, l1x, l1y) = val;
00117                                     BLK(ctx->cur, l1x + 1, l1y) = val;
00118                                     BLK(ctx->cur, l1x, l1y + 1) = val;
00119                                     BLK(ctx->cur, l1x + 1, l1y + 1) = val;
00120                                 } else {        // copy block from already decoded place
00121                                     val = *src++;
00122                                     mx = val & 0xF;
00123                                     my = val >> 4;
00124                                     BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
00125                                     BLK(ctx->cur, l1x + 1, l1y) =
00126                                         BLK(ctx->cur, l1x + 1 - mx, l1y - my);
00127                                     BLK(ctx->cur, l1x, l1y + 1) =
00128                                         BLK(ctx->cur, l1x - mx, l1y + 1 - my);
00129                                     BLK(ctx->cur, l1x + 1, l1y + 1) =
00130                                         BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
00131                                 }
00132                             } else {    // read values for block
00133                                 BLK(ctx->cur, l1x, l1y) = *src++;
00134                                 BLK(ctx->cur, l1x + 1, l1y) = *src++;
00135                                 BLK(ctx->cur, l1x, l1y + 1) = *src++;
00136                                 BLK(ctx->cur, l1x + 1, l1y + 1) = *src++;
00137                             }
00138                         }
00139                     }
00140                 }
00141             }
00142         }
00143 }
00144 
00145 static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int w, int h)
00146 {
00147     BitBuf bb;
00148     int res, val;
00149     int i, j;
00150     int bx, by;
00151     int l0x, l1x, l0y, l1y;
00152     int mx, my;
00153 
00154     kmvc_init_getbits(bb, src);
00155 
00156     for (by = 0; by < h; by += 8)
00157         for (bx = 0; bx < w; bx += 8) {
00158             kmvc_getbit(bb, src, res);
00159             if (!res) {
00160                 kmvc_getbit(bb, src, res);
00161                 if (!res) {     // fill whole 8x8 block
00162                     val = *src++;
00163                     for (i = 0; i < 64; i++)
00164                         BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
00165                 } else {        // copy block from previous frame
00166                     for (i = 0; i < 64; i++)
00167                         BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
00168                             BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
00169                 }
00170             } else {            // handle four 4x4 subblocks
00171                 for (i = 0; i < 4; i++) {
00172                     l0x = bx + (i & 1) * 4;
00173                     l0y = by + (i & 2) * 2;
00174                     kmvc_getbit(bb, src, res);
00175                     if (!res) {
00176                         kmvc_getbit(bb, src, res);
00177                         if (!res) {     // fill whole 4x4 block
00178                             val = *src++;
00179                             for (j = 0; j < 16; j++)
00180                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
00181                         } else {        // copy block
00182                             val = *src++;
00183                             mx = (val & 0xF) - 8;
00184                             my = (val >> 4) - 8;
00185                             for (j = 0; j < 16; j++)
00186                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
00187                                     BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
00188                         }
00189                     } else {    // descend to 2x2 sub-sub-blocks
00190                         for (j = 0; j < 4; j++) {
00191                             l1x = l0x + (j & 1) * 2;
00192                             l1y = l0y + (j & 2);
00193                             kmvc_getbit(bb, src, res);
00194                             if (!res) {
00195                                 kmvc_getbit(bb, src, res);
00196                                 if (!res) {     // fill whole 2x2 block
00197                                     val = *src++;
00198                                     BLK(ctx->cur, l1x, l1y) = val;
00199                                     BLK(ctx->cur, l1x + 1, l1y) = val;
00200                                     BLK(ctx->cur, l1x, l1y + 1) = val;
00201                                     BLK(ctx->cur, l1x + 1, l1y + 1) = val;
00202                                 } else {        // copy block
00203                                     val = *src++;
00204                                     mx = (val & 0xF) - 8;
00205                                     my = (val >> 4) - 8;
00206                                     BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
00207                                     BLK(ctx->cur, l1x + 1, l1y) =
00208                                         BLK(ctx->prev, l1x + 1 + mx, l1y + my);
00209                                     BLK(ctx->cur, l1x, l1y + 1) =
00210                                         BLK(ctx->prev, l1x + mx, l1y + 1 + my);
00211                                     BLK(ctx->cur, l1x + 1, l1y + 1) =
00212                                         BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
00213                                 }
00214                             } else {    // read values for block
00215                                 BLK(ctx->cur, l1x, l1y) = *src++;
00216                                 BLK(ctx->cur, l1x + 1, l1y) = *src++;
00217                                 BLK(ctx->cur, l1x, l1y + 1) = *src++;
00218                                 BLK(ctx->cur, l1x + 1, l1y + 1) = *src++;
00219                             }
00220                         }
00221                     }
00222                 }
00223             }
00224         }
00225 }
00226 
00227 static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPacket *avpkt)
00228 {
00229     const uint8_t *buf = avpkt->data;
00230     int buf_size = avpkt->size;
00231     KmvcContext *const ctx = avctx->priv_data;
00232     uint8_t *out, *src;
00233     int i;
00234     int header;
00235     int blocksize;
00236     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00237 
00238     if (ctx->pic.data[0])
00239         avctx->release_buffer(avctx, &ctx->pic);
00240 
00241     ctx->pic.reference = 1;
00242     ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00243     if (avctx->get_buffer(avctx, &ctx->pic) < 0) {
00244         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00245         return -1;
00246     }
00247 
00248     header = *buf++;
00249 
00250     /* blocksize 127 is really palette change event */
00251     if (buf[0] == 127) {
00252         buf += 3;
00253         for (i = 0; i < 127; i++) {
00254             ctx->pal[i + (header & 0x81)] = AV_RB24(buf);
00255             buf += 4;
00256         }
00257         buf -= 127 * 4 + 3;
00258     }
00259 
00260     if (header & KMVC_KEYFRAME) {
00261         ctx->pic.key_frame = 1;
00262         ctx->pic.pict_type = AV_PICTURE_TYPE_I;
00263     } else {
00264         ctx->pic.key_frame = 0;
00265         ctx->pic.pict_type = AV_PICTURE_TYPE_P;
00266     }
00267 
00268     if (header & KMVC_PALETTE) {
00269         ctx->pic.palette_has_changed = 1;
00270         // palette starts from index 1 and has 127 entries
00271         for (i = 1; i <= ctx->palsize; i++) {
00272             ctx->pal[i] = bytestream_get_be24(&buf);
00273         }
00274     }
00275 
00276     if (pal) {
00277         ctx->pic.palette_has_changed = 1;
00278         memcpy(ctx->pal, pal, AVPALETTE_SIZE);
00279     }
00280 
00281     if (ctx->setpal) {
00282         ctx->setpal = 0;
00283         ctx->pic.palette_has_changed = 1;
00284     }
00285 
00286     /* make the palette available on the way out */
00287     memcpy(ctx->pic.data[1], ctx->pal, 1024);
00288 
00289     blocksize = *buf++;
00290 
00291     if (blocksize != 8 && blocksize != 127) {
00292         av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
00293         return -1;
00294     }
00295     memset(ctx->cur, 0, 320 * 200);
00296     switch (header & KMVC_METHOD) {
00297     case 0:
00298     case 1: // used in palette changed event
00299         memcpy(ctx->cur, ctx->prev, 320 * 200);
00300         break;
00301     case 3:
00302         kmvc_decode_intra_8x8(ctx, buf, avctx->width, avctx->height);
00303         break;
00304     case 4:
00305         kmvc_decode_inter_8x8(ctx, buf, avctx->width, avctx->height);
00306         break;
00307     default:
00308         av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
00309         return -1;
00310     }
00311 
00312     out = ctx->pic.data[0];
00313     src = ctx->cur;
00314     for (i = 0; i < avctx->height; i++) {
00315         memcpy(out, src, avctx->width);
00316         src += 320;
00317         out += ctx->pic.linesize[0];
00318     }
00319 
00320     /* flip buffers */
00321     if (ctx->cur == ctx->frm0) {
00322         ctx->cur = ctx->frm1;
00323         ctx->prev = ctx->frm0;
00324     } else {
00325         ctx->cur = ctx->frm0;
00326         ctx->prev = ctx->frm1;
00327     }
00328 
00329     *data_size = sizeof(AVFrame);
00330     *(AVFrame *) data = ctx->pic;
00331 
00332     /* always report that the buffer was completely consumed */
00333     return buf_size;
00334 }
00335 
00336 
00337 
00338 /*
00339  * Init kmvc decoder
00340  */
00341 static av_cold int decode_init(AVCodecContext * avctx)
00342 {
00343     KmvcContext *const c = avctx->priv_data;
00344     int i;
00345 
00346     c->avctx = avctx;
00347 
00348     if (avctx->width > 320 || avctx->height > 200) {
00349         av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
00350         return -1;
00351     }
00352 
00353     c->frm0 = av_mallocz(320 * 200);
00354     c->frm1 = av_mallocz(320 * 200);
00355     c->cur = c->frm0;
00356     c->prev = c->frm1;
00357 
00358     for (i = 0; i < 256; i++) {
00359         c->pal[i] = i * 0x10101;
00360     }
00361 
00362     if (avctx->extradata_size < 12) {
00363         av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n");
00364         c->palsize = 127;
00365     } else {
00366         c->palsize = AV_RL16(avctx->extradata + 10);
00367     }
00368 
00369     if (avctx->extradata_size == 1036) {        // palette in extradata
00370         uint8_t *src = avctx->extradata + 12;
00371         for (i = 0; i < 256; i++) {
00372             c->pal[i] = AV_RL32(src);
00373             src += 4;
00374         }
00375         c->setpal = 1;
00376     }
00377 
00378     avctx->pix_fmt = PIX_FMT_PAL8;
00379 
00380     return 0;
00381 }
00382 
00383 
00384 
00385 /*
00386  * Uninit kmvc decoder
00387  */
00388 static av_cold int decode_end(AVCodecContext * avctx)
00389 {
00390     KmvcContext *const c = avctx->priv_data;
00391 
00392     av_freep(&c->frm0);
00393     av_freep(&c->frm1);
00394     if (c->pic.data[0])
00395         avctx->release_buffer(avctx, &c->pic);
00396 
00397     return 0;
00398 }
00399 
00400 AVCodec ff_kmvc_decoder = {
00401     "kmvc",
00402     AVMEDIA_TYPE_VIDEO,
00403     CODEC_ID_KMVC,
00404     sizeof(KmvcContext),
00405     decode_init,
00406     NULL,
00407     decode_end,
00408     decode_frame,
00409     CODEC_CAP_DR1,
00410     .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
00411 };