2006-04-07 19:59:57 +03:00
|
|
|
/*
|
|
|
|
* KMVC decoder
|
|
|
|
* Copyright (c) 2006 Konstantin Shishkov
|
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-04-07 19:59:57 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-04-07 19:59:57 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-04-07 19:59:57 +03:00
|
|
|
* 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
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-04-07 19:59:57 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2006-04-07 19:59:57 +03:00
|
|
|
* Karl Morton's Video Codec decoder
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2007-06-02 04:41:07 +03:00
|
|
|
#include "bytestream.h"
|
2012-11-10 15:22:56 +03:00
|
|
|
#include "internal.h"
|
2013-07-01 04:05:41 +03:00
|
|
|
#include "libavutil/common.h"
|
2006-04-07 19:59:57 +03:00
|
|
|
|
|
|
|
#define KMVC_KEYFRAME 0x80
|
|
|
|
#define KMVC_PALETTE 0x40
|
|
|
|
#define KMVC_METHOD 0x0F
|
2012-01-26 19:30:49 +03:00
|
|
|
#define MAX_PALSIZE 256
|
2006-04-07 19:59:57 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Decoder context
|
|
|
|
*/
|
|
|
|
typedef struct KmvcContext {
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
|
|
|
|
int setpal;
|
|
|
|
int palsize;
|
2012-01-26 19:30:49 +03:00
|
|
|
uint32_t pal[MAX_PALSIZE];
|
2006-04-07 19:59:57 +03:00
|
|
|
uint8_t *cur, *prev;
|
2013-07-01 04:04:15 +03:00
|
|
|
uint8_t frm0[320 * 200], frm1[320 * 200];
|
2012-01-10 04:21:17 +03:00
|
|
|
GetByteContext g;
|
2006-04-07 19:59:57 +03:00
|
|
|
} KmvcContext;
|
|
|
|
|
|
|
|
typedef struct BitBuf {
|
|
|
|
int bits;
|
|
|
|
int bitbuf;
|
|
|
|
} BitBuf;
|
|
|
|
|
2013-07-01 04:05:41 +03:00
|
|
|
#define BLK(data, x, y) data[av_clip((x) + (y) * 320, 0, 320 * 200 -1)]
|
2006-04-07 19:59:57 +03:00
|
|
|
|
2012-01-10 04:21:17 +03:00
|
|
|
#define kmvc_init_getbits(bb, g) bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g);
|
2006-04-07 19:59:57 +03:00
|
|
|
|
2012-01-10 04:21:17 +03:00
|
|
|
#define kmvc_getbit(bb, g, res) {\
|
2006-04-07 19:59:57 +03:00
|
|
|
res = 0; \
|
|
|
|
if (bb.bitbuf & (1 << bb.bits)) res = 1; \
|
|
|
|
bb.bits--; \
|
|
|
|
if(bb.bits == -1) { \
|
2012-01-10 04:21:17 +03:00
|
|
|
bb.bitbuf = bytestream2_get_byte(g); \
|
2006-04-07 19:59:57 +03:00
|
|
|
bb.bits = 7; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2012-01-10 04:21:17 +03:00
|
|
|
static int kmvc_decode_intra_8x8(KmvcContext * ctx, int w, int h)
|
2006-04-07 19:59:57 +03:00
|
|
|
{
|
|
|
|
BitBuf bb;
|
|
|
|
int res, val;
|
|
|
|
int i, j;
|
|
|
|
int bx, by;
|
|
|
|
int l0x, l1x, l0y, l1y;
|
|
|
|
int mx, my;
|
|
|
|
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_init_getbits(bb, &ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
|
|
|
|
for (by = 0; by < h; by += 8)
|
|
|
|
for (bx = 0; bx < w; bx += 8) {
|
2012-01-10 04:21:17 +03:00
|
|
|
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);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) { // fill whole 8x8 block
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
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;
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) {
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) { // fill whole 4x4 block
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
for (j = 0; j < 16; j++)
|
|
|
|
BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
|
|
|
|
} else { // copy block from already decoded place
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
mx = val & 0xF;
|
|
|
|
my = val >> 4;
|
2013-07-29 21:43:45 +03:00
|
|
|
if ((l0x-mx) + 320*(l0y-my) < 0 || (l0x-mx) + 320*(l0y-my) > 320*197 - 4) {
|
2012-03-03 06:14:37 +03:00
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2006-04-07 19:59:57 +03:00
|
|
|
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);
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) {
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) { // fill whole 2x2 block
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
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
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
mx = val & 0xF;
|
|
|
|
my = val >> 4;
|
2013-07-29 21:43:45 +03:00
|
|
|
if ((l1x-mx) + 320*(l1y-my) < 0 || (l1x-mx) + 320*(l1y-my) > 320*199 - 2) {
|
2012-03-03 06:14:37 +03:00
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2006-04-07 19:59:57 +03:00
|
|
|
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
|
2012-01-10 04:21:17 +03:00
|
|
|
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);
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-12 17:54:54 +03:00
|
|
|
|
|
|
|
return 0;
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
2012-01-10 04:21:17 +03:00
|
|
|
static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h)
|
2006-04-07 19:59:57 +03:00
|
|
|
{
|
|
|
|
BitBuf bb;
|
|
|
|
int res, val;
|
|
|
|
int i, j;
|
|
|
|
int bx, by;
|
|
|
|
int l0x, l1x, l0y, l1y;
|
|
|
|
int mx, my;
|
|
|
|
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_init_getbits(bb, &ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
|
|
|
|
for (by = 0; by < h; by += 8)
|
|
|
|
for (bx = 0; bx < w; bx += 8) {
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) {
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) { // fill whole 8x8 block
|
2012-01-10 04:21:17 +03:00
|
|
|
if (!bytestream2_get_bytes_left(&ctx->g)) {
|
2011-12-12 17:54:54 +03:00
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
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
|
2012-01-10 04:21:17 +03:00
|
|
|
if (!bytestream2_get_bytes_left(&ctx->g)) {
|
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2006-04-07 19:59:57 +03:00
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
l0x = bx + (i & 1) * 4;
|
|
|
|
l0y = by + (i & 2) * 2;
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) {
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) { // fill whole 4x4 block
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
for (j = 0; j < 16; j++)
|
|
|
|
BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
|
|
|
|
} else { // copy block
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
mx = (val & 0xF) - 8;
|
|
|
|
my = (val >> 4) - 8;
|
2013-07-29 21:43:45 +03:00
|
|
|
if ((l0x+mx) + 320*(l0y+my) < 0 || (l0x+mx) + 320*(l0y+my) > 320*197 - 4) {
|
2012-03-03 06:14:37 +03:00
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2006-04-07 19:59:57 +03:00
|
|
|
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);
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) {
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_getbit(bb, &ctx->g, res);
|
2006-04-07 19:59:57 +03:00
|
|
|
if (!res) { // fill whole 2x2 block
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
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
|
2012-01-10 04:21:17 +03:00
|
|
|
val = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
mx = (val & 0xF) - 8;
|
|
|
|
my = (val >> 4) - 8;
|
2013-07-29 21:43:45 +03:00
|
|
|
if ((l1x+mx) + 320*(l1y+my) < 0 || (l1x+mx) + 320*(l1y+my) > 320*199 - 2) {
|
2012-03-03 06:14:37 +03:00
|
|
|
av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2006-04-07 19:59:57 +03:00
|
|
|
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
|
2012-01-10 04:21:17 +03:00
|
|
|
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);
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-12 17:54:54 +03:00
|
|
|
|
|
|
|
return 0;
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
|
|
|
|
AVPacket *avpkt)
|
2006-04-07 19:59:57 +03:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
KmvcContext *const ctx = avctx->priv_data;
|
2012-11-21 23:34:46 +03:00
|
|
|
AVFrame *frame = data;
|
2006-04-07 19:59:57 +03:00
|
|
|
uint8_t *out, *src;
|
2012-11-17 00:36:10 +03:00
|
|
|
int i, ret;
|
2006-04-07 19:59:57 +03:00
|
|
|
int header;
|
|
|
|
int blocksize;
|
2011-04-09 16:49:51 +03:00
|
|
|
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
|
2006-04-07 19:59:57 +03:00
|
|
|
|
2012-01-10 04:21:17 +03:00
|
|
|
bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
|
2006-04-07 19:59:57 +03:00
|
|
|
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 10:41:53 +03:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
2012-10-17 18:42:24 +03:00
|
|
|
return ret;
|
2006-04-07 19:59:57 +03:00
|
|
|
|
2012-01-10 04:21:17 +03:00
|
|
|
header = bytestream2_get_byte(&ctx->g);
|
2006-05-31 07:52:02 +03:00
|
|
|
|
|
|
|
/* blocksize 127 is really palette change event */
|
2012-01-10 04:21:17 +03:00
|
|
|
if (bytestream2_peek_byte(&ctx->g) == 127) {
|
|
|
|
bytestream2_skip(&ctx->g, 3);
|
2006-05-31 07:52:02 +03:00
|
|
|
for (i = 0; i < 127; i++) {
|
2012-01-11 04:23:55 +03:00
|
|
|
ctx->pal[i + (header & 0x81)] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
|
2012-01-10 04:21:17 +03:00
|
|
|
bytestream2_skip(&ctx->g, 1);
|
2006-05-31 07:52:02 +03:00
|
|
|
}
|
2012-01-10 04:21:17 +03:00
|
|
|
bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
|
2006-05-31 07:52:02 +03:00
|
|
|
}
|
2006-04-07 19:59:57 +03:00
|
|
|
|
|
|
|
if (header & KMVC_KEYFRAME) {
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->key_frame = 1;
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
2006-04-07 19:59:57 +03:00
|
|
|
} else {
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->key_frame = 0;
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_P;
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (header & KMVC_PALETTE) {
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->palette_has_changed = 1;
|
2006-04-07 19:59:57 +03:00
|
|
|
// palette starts from index 1 and has 127 entries
|
|
|
|
for (i = 1; i <= ctx->palsize; i++) {
|
2012-01-11 04:23:55 +03:00
|
|
|
ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-09 16:49:51 +03:00
|
|
|
if (pal) {
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->palette_has_changed = 1;
|
2011-04-09 16:49:51 +03:00
|
|
|
memcpy(ctx->pal, pal, AVPALETTE_SIZE);
|
|
|
|
}
|
|
|
|
|
2006-04-07 19:59:57 +03:00
|
|
|
if (ctx->setpal) {
|
|
|
|
ctx->setpal = 0;
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->palette_has_changed = 1;
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make the palette available on the way out */
|
2012-11-21 23:34:46 +03:00
|
|
|
memcpy(frame->data[1], ctx->pal, 1024);
|
2006-04-07 19:59:57 +03:00
|
|
|
|
2012-01-10 04:21:17 +03:00
|
|
|
blocksize = bytestream2_get_byte(&ctx->g);
|
2006-04-07 19:59:57 +03:00
|
|
|
|
2006-05-31 07:52:02 +03:00
|
|
|
if (blocksize != 8 && blocksize != 127) {
|
2006-04-07 19:59:57 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
|
2012-10-17 18:42:24 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
memset(ctx->cur, 0, 320 * 200);
|
|
|
|
switch (header & KMVC_METHOD) {
|
2006-05-31 07:52:02 +03:00
|
|
|
case 0:
|
|
|
|
case 1: // used in palette changed event
|
|
|
|
memcpy(ctx->cur, ctx->prev, 320 * 200);
|
|
|
|
break;
|
2006-04-07 19:59:57 +03:00
|
|
|
case 3:
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
|
2006-04-07 19:59:57 +03:00
|
|
|
break;
|
|
|
|
case 4:
|
2012-01-10 04:21:17 +03:00
|
|
|
kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
|
2006-04-07 19:59:57 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
|
2012-10-17 18:42:24 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
out = frame->data[0];
|
2006-04-07 19:59:57 +03:00
|
|
|
src = ctx->cur;
|
|
|
|
for (i = 0; i < avctx->height; i++) {
|
|
|
|
memcpy(out, src, avctx->width);
|
|
|
|
src += 320;
|
2012-11-21 23:34:46 +03:00
|
|
|
out += frame->linesize[0];
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* flip buffers */
|
|
|
|
if (ctx->cur == ctx->frm0) {
|
|
|
|
ctx->cur = ctx->frm1;
|
|
|
|
ctx->prev = ctx->frm0;
|
|
|
|
} else {
|
|
|
|
ctx->cur = ctx->frm0;
|
|
|
|
ctx->prev = ctx->frm1;
|
|
|
|
}
|
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2006-04-07 19:59:57 +03:00
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
2012-01-10 04:21:17 +03:00
|
|
|
return avpkt->size;
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init kmvc decoder
|
|
|
|
*/
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int decode_init(AVCodecContext * avctx)
|
2006-04-07 19:59:57 +03:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
KmvcContext *const c = avctx->priv_data;
|
2006-04-07 19:59:57 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
c->avctx = avctx;
|
|
|
|
|
|
|
|
if (avctx->width > 320 || avctx->height > 200) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
|
2012-11-17 00:36:10 +03:00
|
|
|
return AVERROR(EINVAL);
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
c->cur = c->frm0;
|
|
|
|
c->prev = c->frm1;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
2012-10-14 04:17:13 +03:00
|
|
|
c->pal[i] = 0xFFU << 24 | i * 0x10101;
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (avctx->extradata_size < 12) {
|
2012-01-30 21:27:50 +03:00
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"Extradata missing, decoding may not work properly...\n");
|
2006-04-07 19:59:57 +03:00
|
|
|
c->palsize = 127;
|
|
|
|
} else {
|
2007-01-20 00:12:59 +02:00
|
|
|
c->palsize = AV_RL16(avctx->extradata + 10);
|
2012-01-31 03:14:58 +03:00
|
|
|
if (c->palsize >= (unsigned)MAX_PALSIZE) {
|
2012-01-26 19:30:49 +03:00
|
|
|
c->palsize = 127;
|
2012-01-26 19:30:49 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-01-26 19:30:49 +03:00
|
|
|
}
|
2006-04-07 19:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (avctx->extradata_size == 1036) { // palette in extradata
|
|
|
|
uint8_t *src = avctx->extradata + 12;
|
2006-05-31 07:52:02 +03:00
|
|
|
for (i = 0; i < 256; i++) {
|
2007-01-20 00:12:59 +02:00
|
|
|
c->pal[i] = AV_RL32(src);
|
2006-04-07 19:59:57 +03:00
|
|
|
src += 4;
|
|
|
|
}
|
|
|
|
c->setpal = 1;
|
|
|
|
}
|
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2006-04-07 19:59:57 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_kmvc_decoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "kmvc",
|
2013-10-03 23:57:53 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
|
2011-07-17 13:54:31 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_KMVC,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(KmvcContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.decode = decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2006-04-07 19:59:57 +03:00
|
|
|
};
|