• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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

Generated on Wed Apr 11 2012 07:31:33 for FFmpeg by  doxygen 1.7.1