2007-04-07 16:25:25 +02:00
|
|
|
/*
|
|
|
|
* Interplay C93 video decoder
|
|
|
|
* Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2007-07-05 12:40:25 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-04-07 16:25:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2012-11-21 21:34:46 +01:00
|
|
|
#include "internal.h"
|
2007-04-07 16:25:25 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2013-11-09 10:14:46 +01:00
|
|
|
AVFrame *pictures[2];
|
2007-04-07 16:25:25 +02:00
|
|
|
int currentpic;
|
|
|
|
} C93DecoderContext;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
C93_8X8_FROM_PREV = 0x02,
|
|
|
|
C93_4X4_FROM_PREV = 0x06,
|
|
|
|
C93_4X4_FROM_CURR = 0x07,
|
|
|
|
C93_8X8_2COLOR = 0x08,
|
|
|
|
C93_4X4_2COLOR = 0x0A,
|
|
|
|
C93_4X4_4COLOR_GRP = 0x0B,
|
|
|
|
C93_4X4_4COLOR = 0x0D,
|
|
|
|
C93_NOOP = 0x0E,
|
|
|
|
C93_8X8_INTRA = 0x0F,
|
|
|
|
} C93BlockType;
|
|
|
|
|
|
|
|
#define WIDTH 320
|
|
|
|
#define HEIGHT 192
|
|
|
|
|
|
|
|
#define C93_HAS_PALETTE 0x01
|
|
|
|
#define C93_FIRST_FRAME 0x02
|
|
|
|
|
2013-11-09 10:14:46 +01:00
|
|
|
static av_cold int decode_end(AVCodecContext *avctx)
|
2007-04-07 16:25:25 +02:00
|
|
|
{
|
2013-11-09 10:14:46 +01:00
|
|
|
C93DecoderContext * const c93 = avctx->priv_data;
|
|
|
|
|
|
|
|
av_frame_free(&c93->pictures[0]);
|
|
|
|
av_frame_free(&c93->pictures[1]);
|
|
|
|
|
2007-04-07 16:25:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-09 10:14:46 +01:00
|
|
|
static av_cold int decode_init(AVCodecContext *avctx)
|
2007-04-07 16:25:25 +02:00
|
|
|
{
|
2013-11-09 10:14:46 +01:00
|
|
|
C93DecoderContext *s = avctx->priv_data;
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2007-04-07 16:25:25 +02:00
|
|
|
|
2013-11-09 10:14:46 +01:00
|
|
|
s->pictures[0] = av_frame_alloc();
|
|
|
|
s->pictures[1] = av_frame_alloc();
|
|
|
|
if (!s->pictures[0] || !s->pictures[1]) {
|
|
|
|
decode_end(avctx);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2012-11-21 21:34:46 +01:00
|
|
|
|
2007-04-07 16:25:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-08 11:31:54 +02:00
|
|
|
static inline int copy_block(AVCodecContext *avctx, uint8_t *to,
|
2007-04-07 16:25:25 +02:00
|
|
|
uint8_t *from, int offset, int height, int stride)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int width = height;
|
|
|
|
int from_x = offset % WIDTH;
|
|
|
|
int from_y = offset / WIDTH;
|
|
|
|
int overflow = from_x + width - WIDTH;
|
|
|
|
|
|
|
|
if (!from) {
|
|
|
|
/* silently ignoring predictive blocks in first frame */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (from_y + height > HEIGHT) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "invalid offset %d during C93 decoding\n",
|
|
|
|
offset);
|
2012-10-17 14:41:17 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-07 16:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (overflow > 0) {
|
|
|
|
width -= overflow;
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
memcpy(&to[i*stride+width], &from[(from_y+i)*stride], overflow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
memcpy(&to[i*stride], &from[(from_y+i)*stride+from_x], width);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-08 11:31:54 +02:00
|
|
|
static inline void draw_n_color(uint8_t *out, int stride, int width,
|
2007-04-07 16:25:25 +02:00
|
|
|
int height, int bpp, uint8_t cols[4], uint8_t grps[4], uint32_t col)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
for (y = 0; y < height; y++) {
|
|
|
|
if (grps)
|
|
|
|
cols[0] = grps[3 * (y >> 1)];
|
|
|
|
for (x = 0; x < width; x++) {
|
|
|
|
if (grps)
|
|
|
|
cols[1]= grps[(x >> 1) + 1];
|
|
|
|
out[x + y*stride] = cols[col & ((1 << bpp) - 1)];
|
|
|
|
col >>= bpp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-08 11:31:54 +02:00
|
|
|
static int decode_frame(AVCodecContext *avctx, void *data,
|
2012-11-13 19:35:22 +01:00
|
|
|
int *got_frame, AVPacket *avpkt)
|
2007-04-07 16:25:25 +02:00
|
|
|
{
|
2009-04-07 17:59:50 +02:00
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2007-04-07 16:25:25 +02:00
|
|
|
C93DecoderContext * const c93 = avctx->priv_data;
|
2013-11-09 10:14:46 +01:00
|
|
|
AVFrame * const newpic = c93->pictures[c93->currentpic];
|
|
|
|
AVFrame * const oldpic = c93->pictures[c93->currentpic^1];
|
2012-03-12 15:27:04 +01:00
|
|
|
GetByteContext gb;
|
2007-04-07 16:25:25 +02:00
|
|
|
uint8_t *out;
|
2012-10-17 14:41:17 +02:00
|
|
|
int stride, ret, i, x, y, b, bt = 0;
|
2007-04-07 16:25:25 +02:00
|
|
|
|
2013-11-05 13:24:39 +01:00
|
|
|
if ((ret = ff_set_dimensions(avctx, WIDTH, HEIGHT)) < 0)
|
|
|
|
return ret;
|
2013-09-15 20:04:24 +02:00
|
|
|
|
2007-04-07 16:25:25 +02:00
|
|
|
c93->currentpic ^= 1;
|
|
|
|
|
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_reget_buffer(avctx, newpic)) < 0)
|
2012-10-17 14:41:17 +02:00
|
|
|
return ret;
|
2007-04-07 16:25:25 +02:00
|
|
|
|
|
|
|
stride = newpic->linesize[0];
|
|
|
|
|
2012-03-12 15:27:04 +01:00
|
|
|
bytestream2_init(&gb, buf, buf_size);
|
|
|
|
b = bytestream2_get_byte(&gb);
|
|
|
|
if (b & C93_FIRST_FRAME) {
|
2011-04-28 01:40:44 +02:00
|
|
|
newpic->pict_type = AV_PICTURE_TYPE_I;
|
2007-04-07 16:25:25 +02:00
|
|
|
newpic->key_frame = 1;
|
|
|
|
} else {
|
2011-04-28 01:40:44 +02:00
|
|
|
newpic->pict_type = AV_PICTURE_TYPE_P;
|
2007-04-07 16:25:25 +02:00
|
|
|
newpic->key_frame = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (y = 0; y < HEIGHT; y += 8) {
|
|
|
|
out = newpic->data[0] + y * stride;
|
|
|
|
for (x = 0; x < WIDTH; x += 8) {
|
|
|
|
uint8_t *copy_from = oldpic->data[0];
|
|
|
|
unsigned int offset, j;
|
|
|
|
uint8_t cols[4], grps[4];
|
2007-04-08 11:44:34 +02:00
|
|
|
C93BlockType block_type;
|
2007-04-07 16:25:25 +02:00
|
|
|
|
|
|
|
if (!bt)
|
2012-03-12 15:27:04 +01:00
|
|
|
bt = bytestream2_get_byte(&gb);
|
2007-04-07 16:25:25 +02:00
|
|
|
|
2007-04-08 11:44:34 +02:00
|
|
|
block_type= bt & 0x0F;
|
|
|
|
switch (block_type) {
|
2007-04-07 16:25:25 +02:00
|
|
|
case C93_8X8_FROM_PREV:
|
2012-03-12 15:27:04 +01:00
|
|
|
offset = bytestream2_get_le16(&gb);
|
2012-11-15 10:29:28 +01:00
|
|
|
if ((ret = copy_block(avctx, out, copy_from, offset, 8, stride)) < 0)
|
|
|
|
return ret;
|
2007-04-07 16:25:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case C93_4X4_FROM_CURR:
|
|
|
|
copy_from = newpic->data[0];
|
|
|
|
case C93_4X4_FROM_PREV:
|
|
|
|
for (j = 0; j < 8; j += 4) {
|
|
|
|
for (i = 0; i < 8; i += 4) {
|
2013-04-16 16:41:21 +02:00
|
|
|
int offset = bytestream2_get_le16(&gb);
|
|
|
|
int from_x = offset % WIDTH;
|
|
|
|
int from_y = offset / WIDTH;
|
|
|
|
if (block_type == C93_4X4_FROM_CURR && from_y == y+j &&
|
|
|
|
(FFABS(from_x - x-i) < 4 || FFABS(from_x - x-i) > WIDTH-4)) {
|
|
|
|
avpriv_request_sample(avctx, "block overlap %d %d %d %d\n", from_x, x+i, from_y, y+j);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2012-11-15 10:29:28 +01:00
|
|
|
if ((ret = copy_block(avctx, &out[j*stride+i],
|
|
|
|
copy_from, offset, 4, stride)) < 0)
|
|
|
|
return ret;
|
2007-04-07 16:25:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case C93_8X8_2COLOR:
|
2012-03-12 15:27:04 +01:00
|
|
|
bytestream2_get_buffer(&gb, cols, 2);
|
2007-04-07 16:25:25 +02:00
|
|
|
for (i = 0; i < 8; i++) {
|
2007-04-08 11:31:54 +02:00
|
|
|
draw_n_color(out + i*stride, stride, 8, 1, 1, cols,
|
2012-03-12 15:27:04 +01:00
|
|
|
NULL, bytestream2_get_byte(&gb));
|
2007-04-07 16:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case C93_4X4_2COLOR:
|
|
|
|
case C93_4X4_4COLOR:
|
|
|
|
case C93_4X4_4COLOR_GRP:
|
|
|
|
for (j = 0; j < 8; j += 4) {
|
|
|
|
for (i = 0; i < 8; i += 4) {
|
2007-04-08 11:44:34 +02:00
|
|
|
if (block_type == C93_4X4_2COLOR) {
|
2012-03-12 15:27:04 +01:00
|
|
|
bytestream2_get_buffer(&gb, cols, 2);
|
2007-04-08 11:31:54 +02:00
|
|
|
draw_n_color(out + i + j*stride, stride, 4, 4,
|
2012-03-12 15:27:04 +01:00
|
|
|
1, cols, NULL, bytestream2_get_le16(&gb));
|
2007-04-08 11:44:34 +02:00
|
|
|
} else if (block_type == C93_4X4_4COLOR) {
|
2012-03-12 15:27:04 +01:00
|
|
|
bytestream2_get_buffer(&gb, cols, 4);
|
2007-04-08 11:31:54 +02:00
|
|
|
draw_n_color(out + i + j*stride, stride, 4, 4,
|
2012-03-12 15:27:04 +01:00
|
|
|
2, cols, NULL, bytestream2_get_le32(&gb));
|
2007-04-07 16:25:25 +02:00
|
|
|
} else {
|
2012-03-12 15:27:04 +01:00
|
|
|
bytestream2_get_buffer(&gb, grps, 4);
|
2007-04-08 11:31:54 +02:00
|
|
|
draw_n_color(out + i + j*stride, stride, 4, 4,
|
2012-03-12 15:27:04 +01:00
|
|
|
1, cols, grps, bytestream2_get_le16(&gb));
|
2007-04-07 16:25:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case C93_NOOP:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case C93_8X8_INTRA:
|
|
|
|
for (j = 0; j < 8; j++)
|
2012-03-12 15:27:04 +01:00
|
|
|
bytestream2_get_buffer(&gb, out + j*stride, 8);
|
2007-04-07 16:25:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "unexpected type %x at %dx%d\n",
|
2007-04-08 11:44:34 +02:00
|
|
|
block_type, x, y);
|
2012-10-17 14:41:17 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-07 16:25:25 +02:00
|
|
|
}
|
|
|
|
bt >>= 4;
|
|
|
|
out += 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-12 15:27:04 +01:00
|
|
|
if (b & C93_HAS_PALETTE) {
|
|
|
|
uint32_t *palette = (uint32_t *) newpic->data[1];
|
|
|
|
for (i = 0; i < 256; i++) {
|
2012-03-13 01:56:33 +01:00
|
|
|
palette[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
|
2012-03-12 15:27:04 +01:00
|
|
|
}
|
2012-11-15 10:38:38 +01:00
|
|
|
newpic->palette_has_changed = 1;
|
2012-03-12 15:27:04 +01:00
|
|
|
} else {
|
|
|
|
if (oldpic->data[1])
|
|
|
|
memcpy(newpic->data[1], oldpic->data[1], 256 * 4);
|
|
|
|
}
|
|
|
|
|
2012-11-21 21:34:46 +01:00
|
|
|
if ((ret = av_frame_ref(data, newpic)) < 0)
|
|
|
|
return ret;
|
2012-11-13 19:35:22 +01:00
|
|
|
*got_frame = 1;
|
2007-04-07 16:25:25 +02:00
|
|
|
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_c93_decoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "c93",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Interplay C93"),
|
2011-07-17 12:54:31 +02:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_C93,
|
2011-07-17 12:54:31 +02:00
|
|
|
.priv_data_size = sizeof(C93DecoderContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.close = decode_end,
|
|
|
|
.decode = decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2007-04-07 16:25:25 +02:00
|
|
|
};
|