kmvc.c 14.6 KB
Newer Older
1 2 3 4
/*
 * KMVC decoder
 * Copyright (c) 2006 Konstantin Shishkov
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with Libav; if not, write to the Free Software
19 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25 26 27 28 29 30
 * Karl Morton's Video Codec decoder
 */

#include <stdio.h>
#include <stdlib.h>

#include "avcodec.h"
31
#include "bytestream.h"
32
#include "internal.h"
33
#include "libavutil/common.h"
34 35 36 37

#define KMVC_KEYFRAME 0x80
#define KMVC_PALETTE  0x40
#define KMVC_METHOD   0x0F
Alex Converse's avatar
Alex Converse committed
38
#define MAX_PALSIZE   256
39 40 41 42 43 44 45 46 47

/*
 * Decoder context
 */
typedef struct KmvcContext {
    AVCodecContext *avctx;

    int setpal;
    int palsize;
Alex Converse's avatar
Alex Converse committed
48
    uint32_t pal[MAX_PALSIZE];
49
    uint8_t *cur, *prev;
50
    uint8_t frm0[320 * 200], frm1[320 * 200];
51
    GetByteContext g;
52 53 54 55 56 57 58
} KmvcContext;

typedef struct BitBuf {
    int bits;
    int bitbuf;
} BitBuf;

59
#define BLK(data, x, y)  data[av_clip((x) + (y) * 320, 0, 320 * 200 -1)]
60

61
#define kmvc_init_getbits(bb, g)  bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g);
62

63
#define kmvc_getbit(bb, g, res) {\
64 65 66 67
    res = 0; \
    if (bb.bitbuf & (1 << bb.bits)) res = 1; \
    bb.bits--; \
    if(bb.bits == -1) { \
68
        bb.bitbuf = bytestream2_get_byte(g); \
69 70 71 72
        bb.bits = 7; \
    } \
}

73
static int kmvc_decode_intra_8x8(KmvcContext * ctx, int w, int h)
74 75 76 77 78 79 80 81
{
    BitBuf bb;
    int res, val;
    int i, j;
    int bx, by;
    int l0x, l1x, l0y, l1y;
    int mx, my;

82
    kmvc_init_getbits(bb, &ctx->g);
83 84 85

    for (by = 0; by < h; by += 8)
        for (bx = 0; bx < w; bx += 8) {
86 87 88 89 90
            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);
91
            if (!res) {         // fill whole 8x8 block
92
                val = bytestream2_get_byte(&ctx->g);
93 94 95 96 97 98
                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;
99
                    kmvc_getbit(bb, &ctx->g, res);
100
                    if (!res) {
101
                        kmvc_getbit(bb, &ctx->g, res);
102
                        if (!res) {     // fill whole 4x4 block
103
                            val = bytestream2_get_byte(&ctx->g);
104 105 106
                            for (j = 0; j < 16; j++)
                                BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
                        } else {        // copy block from already decoded place
107
                            val = bytestream2_get_byte(&ctx->g);
108 109 110 111 112 113 114 115 116 117
                            mx = val & 0xF;
                            my = val >> 4;
                            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);
118
                            kmvc_getbit(bb, &ctx->g, res);
119
                            if (!res) {
120
                                kmvc_getbit(bb, &ctx->g, res);
121
                                if (!res) {     // fill whole 2x2 block
122
                                    val = bytestream2_get_byte(&ctx->g);
123 124 125 126 127
                                    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
128
                                    val = bytestream2_get_byte(&ctx->g);
129 130 131 132 133 134 135 136 137 138 139
                                    mx = val & 0xF;
                                    my = val >> 4;
                                    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
140 141 142 143
                                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);
144 145 146 147 148 149
                            }
                        }
                    }
                }
            }
        }
Gaurav Narula's avatar
Gaurav Narula committed
150 151

    return 0;
152 153
}

154
static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h)
155 156 157 158 159 160 161 162
{
    BitBuf bb;
    int res, val;
    int i, j;
    int bx, by;
    int l0x, l1x, l0y, l1y;
    int mx, my;

163
    kmvc_init_getbits(bb, &ctx->g);
164 165 166

    for (by = 0; by < h; by += 8)
        for (bx = 0; bx < w; bx += 8) {
167
            kmvc_getbit(bb, &ctx->g, res);
168
            if (!res) {
169
                kmvc_getbit(bb, &ctx->g, res);
170
                if (!res) {     // fill whole 8x8 block
171
                    if (!bytestream2_get_bytes_left(&ctx->g)) {
Gaurav Narula's avatar
Gaurav Narula committed
172 173 174
                        av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
                        return AVERROR_INVALIDDATA;
                    }
175
                    val = bytestream2_get_byte(&ctx->g);
176 177 178 179 180 181 182 183
                    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
184 185 186 187
                if (!bytestream2_get_bytes_left(&ctx->g)) {
                    av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
                    return AVERROR_INVALIDDATA;
                }
188 189 190
                for (i = 0; i < 4; i++) {
                    l0x = bx + (i & 1) * 4;
                    l0y = by + (i & 2) * 2;
191
                    kmvc_getbit(bb, &ctx->g, res);
192
                    if (!res) {
193
                        kmvc_getbit(bb, &ctx->g, res);
194
                        if (!res) {     // fill whole 4x4 block
195
                            val = bytestream2_get_byte(&ctx->g);
196 197 198
                            for (j = 0; j < 16; j++)
                                BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
                        } else {        // copy block
199
                            val = bytestream2_get_byte(&ctx->g);
200 201 202 203 204 205 206 207 208 209
                            mx = (val & 0xF) - 8;
                            my = (val >> 4) - 8;
                            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);
210
                            kmvc_getbit(bb, &ctx->g, res);
211
                            if (!res) {
212
                                kmvc_getbit(bb, &ctx->g, res);
213
                                if (!res) {     // fill whole 2x2 block
214
                                    val = bytestream2_get_byte(&ctx->g);
215 216 217 218 219
                                    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
220
                                    val = bytestream2_get_byte(&ctx->g);
221 222 223 224 225 226 227 228 229 230 231
                                    mx = (val & 0xF) - 8;
                                    my = (val >> 4) - 8;
                                    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
232 233 234 235
                                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);
236 237 238 239 240 241
                            }
                        }
                    }
                }
            }
        }
Gaurav Narula's avatar
Gaurav Narula committed
242 243

    return 0;
244 245
}

246 247
static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
                        AVPacket *avpkt)
248
{
249
    KmvcContext *const ctx = avctx->priv_data;
250
    AVFrame *frame = data;
251
    uint8_t *out, *src;
252
    int i, ret;
253 254
    int header;
    int blocksize;
255
    const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
256

257
    bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
258

259
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
260
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
261
        return ret;
262 263
    }

264
    header = bytestream2_get_byte(&ctx->g);
265 266

    /* blocksize 127 is really palette change event */
267 268
    if (bytestream2_peek_byte(&ctx->g) == 127) {
        bytestream2_skip(&ctx->g, 3);
269
        for (i = 0; i < 127; i++) {
270 271
            ctx->pal[i + (header & 0x81)] = bytestream2_get_be24(&ctx->g);
            bytestream2_skip(&ctx->g, 1);
272
        }
273
        bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
274
    }
275 276

    if (header & KMVC_KEYFRAME) {
277 278
        frame->key_frame = 1;
        frame->pict_type = AV_PICTURE_TYPE_I;
279
    } else {
280 281
        frame->key_frame = 0;
        frame->pict_type = AV_PICTURE_TYPE_P;
282 283 284
    }

    if (header & KMVC_PALETTE) {
285
        frame->palette_has_changed = 1;
286 287
        // palette starts from index 1 and has 127 entries
        for (i = 1; i <= ctx->palsize; i++) {
288
            ctx->pal[i] = bytestream2_get_be24(&ctx->g);
289 290 291
        }
    }

292
    if (pal) {
293
        frame->palette_has_changed = 1;
294 295 296
        memcpy(ctx->pal, pal, AVPALETTE_SIZE);
    }

297 298
    if (ctx->setpal) {
        ctx->setpal = 0;
299
        frame->palette_has_changed = 1;
300 301 302
    }

    /* make the palette available on the way out */
303
    memcpy(frame->data[1], ctx->pal, 1024);
304

305
    blocksize = bytestream2_get_byte(&ctx->g);
306

307
    if (blocksize != 8 && blocksize != 127) {
308
        av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
309
        return AVERROR_INVALIDDATA;
310 311 312
    }
    memset(ctx->cur, 0, 320 * 200);
    switch (header & KMVC_METHOD) {
313 314 315 316
    case 0:
    case 1: // used in palette changed event
        memcpy(ctx->cur, ctx->prev, 320 * 200);
        break;
317
    case 3:
318
        kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
319 320
        break;
    case 4:
321
        kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
322 323 324
        break;
    default:
        av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
325
        return AVERROR_INVALIDDATA;
326 327
    }

328
    out = frame->data[0];
329 330 331 332
    src = ctx->cur;
    for (i = 0; i < avctx->height; i++) {
        memcpy(out, src, avctx->width);
        src += 320;
333
        out += frame->linesize[0];
334 335 336 337 338 339 340 341 342 343 344
    }

    /* flip buffers */
    if (ctx->cur == ctx->frm0) {
        ctx->cur = ctx->frm1;
        ctx->prev = ctx->frm0;
    } else {
        ctx->cur = ctx->frm0;
        ctx->prev = ctx->frm1;
    }

345
    *got_frame = 1;
346 347

    /* always report that the buffer was completely consumed */
348
    return avpkt->size;
349 350 351 352 353 354 355
}



/*
 * Init kmvc decoder
 */
356
static av_cold int decode_init(AVCodecContext * avctx)
357
{
358
    KmvcContext *const c = avctx->priv_data;
359 360 361 362 363 364
    int i;

    c->avctx = avctx;

    if (avctx->width > 320 || avctx->height > 200) {
        av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
365
        return AVERROR(EINVAL);
366 367 368 369 370 371 372 373 374 375
    }

    c->cur = c->frm0;
    c->prev = c->frm1;

    for (i = 0; i < 256; i++) {
        c->pal[i] = i * 0x10101;
    }

    if (avctx->extradata_size < 12) {
376 377
        av_log(avctx, AV_LOG_WARNING,
               "Extradata missing, decoding may not work properly...\n");
378 379
        c->palsize = 127;
    } else {
380
        c->palsize = AV_RL16(avctx->extradata + 10);
Alex Converse's avatar
Alex Converse committed
381 382 383 384
        if (c->palsize >= MAX_PALSIZE) {
            av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
            return AVERROR_INVALIDDATA;
        }
385 386 387 388
    }

    if (avctx->extradata_size == 1036) {        // palette in extradata
        uint8_t *src = avctx->extradata + 12;
389
        for (i = 0; i < 256; i++) {
390
            c->pal[i] = AV_RL32(src);
391 392 393 394 395
            src += 4;
        }
        c->setpal = 1;
    }

396
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
397 398 399 400

    return 0;
}

401
AVCodec ff_kmvc_decoder = {
402
    .name           = "kmvc",
403
    .long_name      = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
404
    .type           = AVMEDIA_TYPE_VIDEO,
405
    .id             = AV_CODEC_ID_KMVC,
406 407 408
    .priv_data_size = sizeof(KmvcContext),
    .init           = decode_init,
    .decode         = decode_frame,
409
    .capabilities   = AV_CODEC_CAP_DR1,
410
};