Libav

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