libavcodec/kmvc.c
fd7b1991
 /*
  * KMVC decoder
  * Copyright (c) 2006 Konstantin Shishkov
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
fd7b1991
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
fd7b1991
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
fd7b1991
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
fd7b1991
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
fd7b1991
  * Karl Morton's Video Codec decoder
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "avcodec.h"
2c124cb6
 #include "bytestream.h"
594d4d5d
 #include "internal.h"
4e7f0b08
 #include "libavutil/common.h"
fd7b1991
 
 #define KMVC_KEYFRAME 0x80
 #define KMVC_PALETTE  0x40
 #define KMVC_METHOD   0x0F
386741f8
 #define MAX_PALSIZE   256
fd7b1991
 
 /*
  * Decoder context
  */
 typedef struct KmvcContext {
     AVCodecContext *avctx;
 
     int setpal;
     int palsize;
386741f8
     uint32_t pal[MAX_PALSIZE];
fd7b1991
     uint8_t *cur, *prev;
8f689770
     uint8_t frm0[320 * 200], frm1[320 * 200];
da2e774f
     GetByteContext g;
fd7b1991
 } KmvcContext;
 
 typedef struct BitBuf {
     int bits;
     int bitbuf;
 } BitBuf;
 
4e7f0b08
 #define BLK(data, x, y)  data[av_clip((x) + (y) * 320, 0, 320 * 200 -1)]
fd7b1991
 
da2e774f
 #define kmvc_init_getbits(bb, g)  bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g);
fd7b1991
 
da2e774f
 #define kmvc_getbit(bb, g, res) {\
fd7b1991
     res = 0; \
     if (bb.bitbuf & (1 << bb.bits)) res = 1; \
     bb.bits--; \
     if(bb.bits == -1) { \
da2e774f
         bb.bitbuf = bytestream2_get_byte(g); \
fd7b1991
         bb.bits = 7; \
     } \
 }
 
da2e774f
 static int kmvc_decode_intra_8x8(KmvcContext * ctx, int w, int h)
fd7b1991
 {
     BitBuf bb;
     int res, val;
     int i, j;
     int bx, by;
     int l0x, l1x, l0y, l1y;
     int mx, my;
 
da2e774f
     kmvc_init_getbits(bb, &ctx->g);
fd7b1991
 
     for (by = 0; by < h; by += 8)
         for (bx = 0; bx < w; bx += 8) {
da2e774f
             if (!bytestream2_get_bytes_left(&ctx->g)) {
                 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
                 return AVERROR_INVALIDDATA;
             }
             kmvc_getbit(bb, &ctx->g, res);
fd7b1991
             if (!res) {         // fill whole 8x8 block
da2e774f
                 val = bytestream2_get_byte(&ctx->g);
fd7b1991
                 for (i = 0; i < 64; i++)
                     BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
             } else {            // handle four 4x4 subblocks
                 for (i = 0; i < 4; i++) {
                     l0x = bx + (i & 1) * 4;
                     l0y = by + (i & 2) * 2;
da2e774f
                     kmvc_getbit(bb, &ctx->g, res);
fd7b1991
                     if (!res) {
da2e774f
                         kmvc_getbit(bb, &ctx->g, res);
fd7b1991
                         if (!res) {     // fill whole 4x4 block
da2e774f
                             val = bytestream2_get_byte(&ctx->g);
fd7b1991
                             for (j = 0; j < 16; j++)
                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
                         } else {        // copy block from already decoded place
da2e774f
                             val = bytestream2_get_byte(&ctx->g);
fd7b1991
                             mx = val & 0xF;
                             my = val >> 4;
3cd8aaa2
                             if ((l0x-mx) + 320*(l0y-my) < 0 || (l0x-mx) + 320*(l0y-my) > 320*197 - 4) {
70b5583b
                                 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
                                 return AVERROR_INVALIDDATA;
                             }
fd7b1991
                             for (j = 0; j < 16; j++)
                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
                                     BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
                         }
                     } else {    // descend to 2x2 sub-sub-blocks
                         for (j = 0; j < 4; j++) {
                             l1x = l0x + (j & 1) * 2;
                             l1y = l0y + (j & 2);
da2e774f
                             kmvc_getbit(bb, &ctx->g, res);
fd7b1991
                             if (!res) {
da2e774f
                                 kmvc_getbit(bb, &ctx->g, res);
fd7b1991
                                 if (!res) {     // fill whole 2x2 block
da2e774f
                                     val = bytestream2_get_byte(&ctx->g);
fd7b1991
                                     BLK(ctx->cur, l1x, l1y) = val;
                                     BLK(ctx->cur, l1x + 1, l1y) = val;
                                     BLK(ctx->cur, l1x, l1y + 1) = val;
                                     BLK(ctx->cur, l1x + 1, l1y + 1) = val;
                                 } else {        // copy block from already decoded place
da2e774f
                                     val = bytestream2_get_byte(&ctx->g);
fd7b1991
                                     mx = val & 0xF;
                                     my = val >> 4;
3cd8aaa2
                                     if ((l1x-mx) + 320*(l1y-my) < 0 || (l1x-mx) + 320*(l1y-my) > 320*199 - 2) {
70b5583b
                                         av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
                                         return AVERROR_INVALIDDATA;
                                     }
fd7b1991
                                     BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
                                     BLK(ctx->cur, l1x + 1, l1y) =
                                         BLK(ctx->cur, l1x + 1 - mx, l1y - my);
                                     BLK(ctx->cur, l1x, l1y + 1) =
                                         BLK(ctx->cur, l1x - mx, l1y + 1 - my);
                                     BLK(ctx->cur, l1x + 1, l1y + 1) =
                                         BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
                                 }
                             } else {    // read values for block
da2e774f
                                 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
                                 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
                                 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
                                 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
fd7b1991
                             }
                         }
                     }
                 }
             }
         }
ad3161ec
 
     return 0;
fd7b1991
 }
 
da2e774f
 static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h)
fd7b1991
 {
     BitBuf bb;
     int res, val;
     int i, j;
     int bx, by;
     int l0x, l1x, l0y, l1y;
     int mx, my;
 
da2e774f
     kmvc_init_getbits(bb, &ctx->g);
fd7b1991
 
     for (by = 0; by < h; by += 8)
         for (bx = 0; bx < w; bx += 8) {
da2e774f
             kmvc_getbit(bb, &ctx->g, res);
fd7b1991
             if (!res) {
da2e774f
                 kmvc_getbit(bb, &ctx->g, res);
fd7b1991
                 if (!res) {     // fill whole 8x8 block
da2e774f
                     if (!bytestream2_get_bytes_left(&ctx->g)) {
ad3161ec
                         av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
                         return AVERROR_INVALIDDATA;
                     }
da2e774f
                     val = bytestream2_get_byte(&ctx->g);
fd7b1991
                     for (i = 0; i < 64; i++)
                         BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
                 } else {        // copy block from previous frame
                     for (i = 0; i < 64; i++)
                         BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
                             BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
                 }
             } else {            // handle four 4x4 subblocks
da2e774f
                 if (!bytestream2_get_bytes_left(&ctx->g)) {
                     av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
                     return AVERROR_INVALIDDATA;
                 }
fd7b1991
                 for (i = 0; i < 4; i++) {
                     l0x = bx + (i & 1) * 4;
                     l0y = by + (i & 2) * 2;
da2e774f
                     kmvc_getbit(bb, &ctx->g, res);
fd7b1991
                     if (!res) {
da2e774f
                         kmvc_getbit(bb, &ctx->g, res);
fd7b1991
                         if (!res) {     // fill whole 4x4 block
da2e774f
                             val = bytestream2_get_byte(&ctx->g);
fd7b1991
                             for (j = 0; j < 16; j++)
                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
                         } else {        // copy block
da2e774f
                             val = bytestream2_get_byte(&ctx->g);
fd7b1991
                             mx = (val & 0xF) - 8;
                             my = (val >> 4) - 8;
3cd8aaa2
                             if ((l0x+mx) + 320*(l0y+my) < 0 || (l0x+mx) + 320*(l0y+my) > 320*197 - 4) {
70b5583b
                                 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
                                 return AVERROR_INVALIDDATA;
                             }
fd7b1991
                             for (j = 0; j < 16; j++)
                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
                                     BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
                         }
                     } else {    // descend to 2x2 sub-sub-blocks
                         for (j = 0; j < 4; j++) {
                             l1x = l0x + (j & 1) * 2;
                             l1y = l0y + (j & 2);
da2e774f
                             kmvc_getbit(bb, &ctx->g, res);
fd7b1991
                             if (!res) {
da2e774f
                                 kmvc_getbit(bb, &ctx->g, res);
fd7b1991
                                 if (!res) {     // fill whole 2x2 block
da2e774f
                                     val = bytestream2_get_byte(&ctx->g);
fd7b1991
                                     BLK(ctx->cur, l1x, l1y) = val;
                                     BLK(ctx->cur, l1x + 1, l1y) = val;
                                     BLK(ctx->cur, l1x, l1y + 1) = val;
                                     BLK(ctx->cur, l1x + 1, l1y + 1) = val;
                                 } else {        // copy block
da2e774f
                                     val = bytestream2_get_byte(&ctx->g);
fd7b1991
                                     mx = (val & 0xF) - 8;
                                     my = (val >> 4) - 8;
3cd8aaa2
                                     if ((l1x+mx) + 320*(l1y+my) < 0 || (l1x+mx) + 320*(l1y+my) > 320*199 - 2) {
70b5583b
                                         av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
                                         return AVERROR_INVALIDDATA;
                                     }
fd7b1991
                                     BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
                                     BLK(ctx->cur, l1x + 1, l1y) =
                                         BLK(ctx->prev, l1x + 1 + mx, l1y + my);
                                     BLK(ctx->cur, l1x, l1y + 1) =
                                         BLK(ctx->prev, l1x + mx, l1y + 1 + my);
                                     BLK(ctx->cur, l1x + 1, l1y + 1) =
                                         BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
                                 }
                             } else {    // read values for block
da2e774f
                                 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
                                 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
                                 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
                                 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
fd7b1991
                             }
                         }
                     }
                 }
             }
         }
ad3161ec
 
     return 0;
fd7b1991
 }
 
df9b9567
 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
                         AVPacket *avpkt)
fd7b1991
 {
e4141433
     KmvcContext *const ctx = avctx->priv_data;
759001c5
     AVFrame *frame = data;
fd7b1991
     uint8_t *out, *src;
e1a7061d
     int i, ret;
fd7b1991
     int header;
     int blocksize;
2d8591c2
     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
fd7b1991
 
da2e774f
     bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
fd7b1991
 
1ec94b0f
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
445f36d7
         return ret;
fd7b1991
 
da2e774f
     header = bytestream2_get_byte(&ctx->g);
2d2b86c2
 
     /* blocksize 127 is really palette change event */
da2e774f
     if (bytestream2_peek_byte(&ctx->g) == 127) {
         bytestream2_skip(&ctx->g, 3);
2d2b86c2
         for (i = 0; i < 127; i++) {
0e7fc3ca
             ctx->pal[i + (header & 0x81)] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
da2e774f
             bytestream2_skip(&ctx->g, 1);
2d2b86c2
         }
da2e774f
         bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
2d2b86c2
     }
fd7b1991
 
     if (header & KMVC_KEYFRAME) {
759001c5
         frame->key_frame = 1;
         frame->pict_type = AV_PICTURE_TYPE_I;
fd7b1991
     } else {
759001c5
         frame->key_frame = 0;
         frame->pict_type = AV_PICTURE_TYPE_P;
fd7b1991
     }
 
     if (header & KMVC_PALETTE) {
759001c5
         frame->palette_has_changed = 1;
fd7b1991
         // palette starts from index 1 and has 127 entries
         for (i = 1; i <= ctx->palsize; i++) {
0e7fc3ca
             ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
fd7b1991
         }
     }
 
2d8591c2
     if (pal) {
759001c5
         frame->palette_has_changed = 1;
2d8591c2
         memcpy(ctx->pal, pal, AVPALETTE_SIZE);
     }
 
fd7b1991
     if (ctx->setpal) {
         ctx->setpal = 0;
759001c5
         frame->palette_has_changed = 1;
fd7b1991
     }
 
     /* make the palette available on the way out */
759001c5
     memcpy(frame->data[1], ctx->pal, 1024);
fd7b1991
 
da2e774f
     blocksize = bytestream2_get_byte(&ctx->g);
fd7b1991
 
2d2b86c2
     if (blocksize != 8 && blocksize != 127) {
fd7b1991
         av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
445f36d7
         return AVERROR_INVALIDDATA;
fd7b1991
     }
     memset(ctx->cur, 0, 320 * 200);
     switch (header & KMVC_METHOD) {
2d2b86c2
     case 0:
     case 1: // used in palette changed event
         memcpy(ctx->cur, ctx->prev, 320 * 200);
         break;
fd7b1991
     case 3:
da2e774f
         kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
fd7b1991
         break;
     case 4:
da2e774f
         kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
fd7b1991
         break;
     default:
         av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
445f36d7
         return AVERROR_INVALIDDATA;
fd7b1991
     }
 
759001c5
     out = frame->data[0];
fd7b1991
     src = ctx->cur;
     for (i = 0; i < avctx->height; i++) {
         memcpy(out, src, avctx->width);
         src += 320;
759001c5
         out += frame->linesize[0];
fd7b1991
     }
 
     /* flip buffers */
     if (ctx->cur == ctx->frm0) {
         ctx->cur = ctx->frm1;
         ctx->prev = ctx->frm0;
     } else {
         ctx->cur = ctx->frm0;
         ctx->prev = ctx->frm1;
     }
 
df9b9567
     *got_frame = 1;
fd7b1991
 
     /* always report that the buffer was completely consumed */
da2e774f
     return avpkt->size;
fd7b1991
 }
 
 
 
 /*
  * Init kmvc decoder
  */
98a6fff9
 static av_cold int decode_init(AVCodecContext * avctx)
fd7b1991
 {
e4141433
     KmvcContext *const c = avctx->priv_data;
fd7b1991
     int i;
 
     c->avctx = avctx;
 
     if (avctx->width > 320 || avctx->height > 200) {
         av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
e1a7061d
         return AVERROR(EINVAL);
fd7b1991
     }
 
     c->cur = c->frm0;
     c->prev = c->frm1;
 
     for (i = 0; i < 256; i++) {
b12d92ef
         c->pal[i] = 0xFFU << 24 | i * 0x10101;
fd7b1991
     }
 
     if (avctx->extradata_size < 12) {
ae35210a
         av_log(avctx, AV_LOG_WARNING,
                "Extradata missing, decoding may not work properly...\n");
fd7b1991
         c->palsize = 127;
     } else {
fead30d4
         c->palsize = AV_RL16(avctx->extradata + 10);
151ecc2a
         if (c->palsize >= (unsigned)MAX_PALSIZE) {
70dba1e3
             c->palsize = 127;
386741f8
             av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
             return AVERROR_INVALIDDATA;
70dba1e3
         }
fd7b1991
     }
 
     if (avctx->extradata_size == 1036) {        // palette in extradata
         uint8_t *src = avctx->extradata + 12;
2d2b86c2
         for (i = 0; i < 256; i++) {
fead30d4
             c->pal[i] = AV_RL32(src);
fd7b1991
             src += 4;
         }
         c->setpal = 1;
     }
 
716d413c
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
fd7b1991
 
     return 0;
 }
 
e7e2df27
 AVCodec ff_kmvc_decoder = {
ec6402b7
     .name           = "kmvc",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_KMVC,
ec6402b7
     .priv_data_size = sizeof(KmvcContext),
     .init           = decode_init,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
fd7b1991
 };