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