2008-03-21 13:45:55 +00:00
|
|
|
/*
|
|
|
|
* RL2 Video Decoder
|
|
|
|
* Copyright (C) 2008 Sascha Sommer (saschasommer@freenet.de)
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2008-03-21 13:45:55 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2008-03-21 13:45:55 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2008-03-21 13:45:55 +00: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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2008-03-21 13:45:55 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2011-07-14 01:46:19 +00:00
|
|
|
* RL2 Video Decoder
|
2008-03-21 13:45:55 +00:00
|
|
|
* @author Sascha Sommer (saschasommer@freenet.de)
|
2011-07-14 01:56:07 +00:00
|
|
|
* @see http://wiki.multimedia.cx/index.php?title=RL2
|
2008-03-21 13:45:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/internal.h"
|
2009-01-11 22:19:48 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/mem.h"
|
2008-03-21 13:45:55 +00:00
|
|
|
#include "avcodec.h"
|
2012-11-10 12:22:56 +00:00
|
|
|
#include "internal.h"
|
2008-03-21 13:45:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr count, palette
|
|
|
|
|
|
|
|
typedef struct Rl2Context {
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
|
2012-11-17 06:40:45 +00:00
|
|
|
uint16_t video_base; ///< initial drawing offset
|
|
|
|
uint32_t clr_count; ///< number of used colors (currently unused)
|
|
|
|
uint8_t *back_frame; ///< background frame
|
|
|
|
uint32_t palette[AVPALETTE_COUNT];
|
2008-03-21 13:45:55 +00:00
|
|
|
} Rl2Context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run Length Decode a single 320x200 frame
|
|
|
|
* @param s rl2 context
|
2010-07-02 10:49:29 +00:00
|
|
|
* @param in input buffer
|
2008-03-21 13:45:55 +00:00
|
|
|
* @param size input buffer size
|
|
|
|
* @param out ouput buffer
|
|
|
|
* @param stride stride of the output buffer
|
|
|
|
* @param video_base offset of the rle data inside the frame
|
|
|
|
*/
|
2012-11-17 06:40:45 +00:00
|
|
|
static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size,
|
|
|
|
uint8_t *out, int stride, int video_base)
|
|
|
|
{
|
2008-03-21 13:45:55 +00:00
|
|
|
int base_x = video_base % s->avctx->width;
|
|
|
|
int base_y = video_base / s->avctx->width;
|
|
|
|
int stride_adj = stride - s->avctx->width;
|
|
|
|
int i;
|
2012-11-17 06:40:45 +00:00
|
|
|
const uint8_t *back_frame = s->back_frame;
|
2012-11-17 06:45:05 +00:00
|
|
|
const uint8_t *in_end = in + size;
|
|
|
|
const uint8_t *out_end = out + stride * s->avctx->height;
|
2012-11-17 06:40:45 +00:00
|
|
|
uint8_t *line_end;
|
2008-03-21 13:45:55 +00:00
|
|
|
|
|
|
|
/** copy start of the background frame */
|
2012-11-17 06:45:05 +00:00
|
|
|
for (i = 0; i <= base_y; i++) {
|
|
|
|
if (s->back_frame)
|
|
|
|
memcpy(out, back_frame, s->avctx->width);
|
|
|
|
out += stride;
|
2008-03-21 13:45:55 +00:00
|
|
|
back_frame += s->avctx->width;
|
|
|
|
}
|
|
|
|
back_frame += base_x - s->avctx->width;
|
2012-11-17 06:45:05 +00:00
|
|
|
line_end = out - stride_adj;
|
|
|
|
out += base_x - stride;
|
2008-03-21 13:45:55 +00:00
|
|
|
|
|
|
|
/** decode the variable part of the frame */
|
2012-11-17 06:45:05 +00:00
|
|
|
while (in < in_end) {
|
2012-11-17 06:40:45 +00:00
|
|
|
uint8_t val = *in++;
|
2012-11-17 06:45:05 +00:00
|
|
|
int len = 1;
|
|
|
|
if (val >= 0x80) {
|
|
|
|
if (in >= in_end)
|
2008-03-21 13:45:55 +00:00
|
|
|
break;
|
|
|
|
len = *in++;
|
2012-11-17 06:45:05 +00:00
|
|
|
if (!len)
|
2008-03-21 13:45:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-17 06:45:05 +00:00
|
|
|
if (len >= out_end - out)
|
2008-03-21 13:45:55 +00:00
|
|
|
break;
|
|
|
|
|
2012-11-17 06:45:05 +00:00
|
|
|
if (s->back_frame)
|
2008-03-21 13:45:55 +00:00
|
|
|
val |= 0x80;
|
|
|
|
else
|
|
|
|
val &= ~0x80;
|
|
|
|
|
2012-11-17 06:45:05 +00:00
|
|
|
while (len--) {
|
|
|
|
*out++ = (val == 0x80) ? *back_frame : val;
|
2008-03-21 13:45:55 +00:00
|
|
|
back_frame++;
|
2012-11-17 06:45:05 +00:00
|
|
|
if (out == line_end) {
|
|
|
|
out += stride_adj;
|
2008-03-21 13:45:55 +00:00
|
|
|
line_end += stride;
|
2012-11-17 06:45:05 +00:00
|
|
|
if (len >= out_end - out)
|
2008-03-21 13:45:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** copy the rest from the background frame */
|
2012-11-17 06:45:05 +00:00
|
|
|
if (s->back_frame) {
|
|
|
|
while (out < out_end) {
|
2008-03-21 13:45:55 +00:00
|
|
|
memcpy(out, back_frame, line_end - out);
|
|
|
|
back_frame += line_end - out;
|
2012-11-17 06:45:05 +00:00
|
|
|
out = line_end + stride_adj;
|
|
|
|
line_end += stride;
|
2008-03-21 13:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the decoder
|
|
|
|
* @param avctx decoder context
|
|
|
|
* @return 0 success, -1 on error
|
|
|
|
*/
|
|
|
|
static av_cold int rl2_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
Rl2Context *s = avctx->priv_data;
|
|
|
|
int back_size;
|
|
|
|
int i;
|
2012-11-17 06:45:05 +00:00
|
|
|
|
|
|
|
s->avctx = avctx;
|
2012-10-06 10:10:34 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2008-03-21 13:45:55 +00:00
|
|
|
|
|
|
|
/** parse extra data */
|
2012-11-17 06:45:05 +00:00
|
|
|
if (!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE) {
|
2008-03-21 13:45:55 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
|
2012-11-17 06:37:08 +00:00
|
|
|
return AVERROR(EINVAL);
|
2008-03-21 13:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** get frame_offset */
|
|
|
|
s->video_base = AV_RL16(&avctx->extradata[0]);
|
2012-11-17 06:45:05 +00:00
|
|
|
s->clr_count = AV_RL32(&avctx->extradata[2]);
|
2008-03-21 13:45:55 +00:00
|
|
|
|
2012-11-17 06:45:05 +00:00
|
|
|
if (s->video_base >= avctx->width * avctx->height) {
|
2008-03-21 13:45:55 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
|
2012-11-17 06:37:08 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2008-03-21 13:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** initialize palette */
|
2012-11-17 06:45:05 +00:00
|
|
|
for (i = 0; i < AVPALETTE_COUNT; i++)
|
2008-03-21 13:45:55 +00:00
|
|
|
s->palette[i] = AV_RB24(&avctx->extradata[6 + i * 3]);
|
|
|
|
|
|
|
|
/** decode background frame if present */
|
|
|
|
back_size = avctx->extradata_size - EXTRADATA1_SIZE;
|
|
|
|
|
2012-11-17 06:45:05 +00:00
|
|
|
if (back_size > 0) {
|
2012-11-17 06:40:45 +00:00
|
|
|
uint8_t *back_frame = av_mallocz(avctx->width*avctx->height);
|
2012-11-17 06:45:05 +00:00
|
|
|
if (!back_frame)
|
2012-11-17 06:37:08 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2012-11-17 06:45:05 +00:00
|
|
|
rl2_rle_decode(s, avctx->extradata + EXTRADATA1_SIZE, back_size,
|
|
|
|
back_frame, avctx->width, 0);
|
2008-03-21 13:45:55 +00:00
|
|
|
s->back_frame = back_frame;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int rl2_decode_frame(AVCodecContext *avctx,
|
2012-11-17 06:45:05 +00:00
|
|
|
void *data, int *got_frame,
|
|
|
|
AVPacket *avpkt)
|
2008-03-21 13:45:55 +00:00
|
|
|
{
|
2012-11-21 20:34:46 +00:00
|
|
|
AVFrame *frame = data;
|
2009-04-07 15:59:50 +00:00
|
|
|
const uint8_t *buf = avpkt->data;
|
2012-11-17 06:45:05 +00:00
|
|
|
int ret, buf_size = avpkt->size;
|
2008-03-21 13:45:55 +00:00
|
|
|
Rl2Context *s = avctx->priv_data;
|
|
|
|
|
2012-11-21 20:34:46 +00:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
|
2008-03-21 13:45:55 +00:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
2012-11-17 06:37:08 +00:00
|
|
|
return ret;
|
2008-03-21 13:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** run length decode */
|
2012-11-21 20:34:46 +00:00
|
|
|
rl2_rle_decode(s, buf, buf_size, frame->data[0], frame->linesize[0],
|
2012-11-17 06:45:05 +00:00
|
|
|
s->video_base);
|
2008-03-21 13:45:55 +00:00
|
|
|
|
|
|
|
/** make the palette available on the way out */
|
2012-11-21 20:34:46 +00:00
|
|
|
memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
|
2008-03-21 13:45:55 +00:00
|
|
|
|
2012-11-13 18:35:22 +00:00
|
|
|
*got_frame = 1;
|
2008-03-21 13:45:55 +00:00
|
|
|
|
|
|
|
/** report that the buffer was completely consumed */
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninit decoder
|
|
|
|
* @param avctx decoder context
|
|
|
|
* @return 0 success, -1 on error
|
|
|
|
*/
|
|
|
|
static av_cold int rl2_decode_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
Rl2Context *s = avctx->priv_data;
|
|
|
|
|
|
|
|
av_free(s->back_frame);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-25 21:40:11 +00:00
|
|
|
AVCodec ff_rl2_decoder = {
|
2011-07-17 10:54:31 +00:00
|
|
|
.name = "rl2",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.id = AV_CODEC_ID_RL2,
|
2011-07-17 10:54:31 +00:00
|
|
|
.priv_data_size = sizeof(Rl2Context),
|
|
|
|
.init = rl2_decode_init,
|
|
|
|
.close = rl2_decode_end,
|
|
|
|
.decode = rl2_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2012-04-06 16:19:39 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("RL2 video"),
|
2008-03-21 13:45:55 +00:00
|
|
|
};
|