975cdc8f |
/*
* RL2 Video Decoder
* Copyright (C) 2008 Sascha Sommer (saschasommer@freenet.de)
*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** |
ba87f080 |
* @file |
f6f95d4e |
* RL2 Video Decoder |
975cdc8f |
* @author Sascha Sommer (saschasommer@freenet.de) |
ad4cd0c2 |
* @see http://wiki.multimedia.cx/index.php?title=RL2 |
975cdc8f |
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
|
1d9c2dc8 |
#include "libavutil/internal.h" |
6a5d31ac |
#include "libavutil/intreadwrite.h" |
1d9c2dc8 |
#include "libavutil/mem.h" |
975cdc8f |
#include "avcodec.h" |
594d4d5d |
#include "internal.h" |
975cdc8f |
#define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr count, palette
typedef struct Rl2Context {
AVCodecContext *avctx;
|
3c6e5a84 |
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]; |
975cdc8f |
} Rl2Context;
/**
* Run Length Decode a single 320x200 frame
* @param s rl2 context |
9a58234f |
* @param in input buffer |
975cdc8f |
* @param size input buffer size |
23b639c4 |
* @param out output buffer |
975cdc8f |
* @param stride stride of the output buffer
* @param video_base offset of the rle data inside the frame
*/ |
3c6e5a84 |
static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size,
uint8_t *out, int stride, int video_base)
{ |
975cdc8f |
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; |
3c6e5a84 |
const uint8_t *back_frame = s->back_frame; |
6781b531 |
const uint8_t *in_end = in + size;
const uint8_t *out_end = out + stride * s->avctx->height; |
3c6e5a84 |
uint8_t *line_end; |
975cdc8f |
/** copy start of the background frame */ |
6781b531 |
for (i = 0; i <= base_y; i++) {
if (s->back_frame)
memcpy(out, back_frame, s->avctx->width);
out += stride; |
975cdc8f |
back_frame += s->avctx->width;
}
back_frame += base_x - s->avctx->width; |
6781b531 |
line_end = out - stride_adj;
out += base_x - stride; |
975cdc8f |
/** decode the variable part of the frame */ |
6781b531 |
while (in < in_end) { |
3c6e5a84 |
uint8_t val = *in++; |
6781b531 |
int len = 1;
if (val >= 0x80) {
if (in >= in_end) |
975cdc8f |
break;
len = *in++; |
6781b531 |
if (!len) |
975cdc8f |
break;
}
|
6781b531 |
if (len >= out_end - out) |
975cdc8f |
break;
|
6781b531 |
if (s->back_frame) |
975cdc8f |
val |= 0x80;
else
val &= ~0x80;
|
6781b531 |
while (len--) {
*out++ = (val == 0x80) ? *back_frame : val; |
975cdc8f |
back_frame++; |
6781b531 |
if (out == line_end) {
out += stride_adj; |
975cdc8f |
line_end += stride; |
6781b531 |
if (len >= out_end - out) |
975cdc8f |
break;
}
}
}
/** copy the rest from the background frame */ |
6781b531 |
if (s->back_frame) {
while (out < out_end) { |
975cdc8f |
memcpy(out, back_frame, line_end - out);
back_frame += line_end - out; |
6781b531 |
out = line_end + stride_adj;
line_end += stride; |
975cdc8f |
}
}
}
/**
* 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; |
6781b531 |
s->avctx = avctx; |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_PAL8; |
975cdc8f |
/** parse extra data */ |
6781b531 |
if (!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE) { |
975cdc8f |
av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n"); |
126abaaa |
return AVERROR(EINVAL); |
975cdc8f |
}
/** get frame_offset */
s->video_base = AV_RL16(&avctx->extradata[0]); |
6781b531 |
s->clr_count = AV_RL32(&avctx->extradata[2]); |
975cdc8f |
|
6781b531 |
if (s->video_base >= avctx->width * avctx->height) { |
975cdc8f |
av_log(avctx, AV_LOG_ERROR, "invalid video_base\n"); |
ed591ed8 |
return AVERROR_INVALIDDATA; |
975cdc8f |
}
/** initialize palette */ |
6781b531 |
for (i = 0; i < AVPALETTE_COUNT; i++) |
b12d92ef |
s->palette[i] = 0xFFU << 24 | AV_RB24(&avctx->extradata[6 + i * 3]); |
975cdc8f |
/** decode background frame if present */
back_size = avctx->extradata_size - EXTRADATA1_SIZE;
|
6781b531 |
if (back_size > 0) { |
3c6e5a84 |
uint8_t *back_frame = av_mallocz(avctx->width*avctx->height); |
6781b531 |
if (!back_frame) |
ed591ed8 |
return AVERROR(ENOMEM); |
6781b531 |
rl2_rle_decode(s, avctx->extradata + EXTRADATA1_SIZE, back_size,
back_frame, avctx->width, 0); |
975cdc8f |
s->back_frame = back_frame;
}
return 0;
}
static int rl2_decode_frame(AVCodecContext *avctx, |
6781b531 |
void *data, int *got_frame,
AVPacket *avpkt) |
975cdc8f |
{ |
759001c5 |
AVFrame *frame = data; |
7a00bbad |
const uint8_t *buf = avpkt->data; |
6781b531 |
int ret, buf_size = avpkt->size; |
975cdc8f |
Rl2Context *s = avctx->priv_data;
|
1ec94b0f |
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
ed591ed8 |
return ret; |
975cdc8f |
/** run length decode */ |
759001c5 |
rl2_rle_decode(s, buf, buf_size, frame->data[0], frame->linesize[0], |
6781b531 |
s->video_base); |
975cdc8f |
/** make the palette available on the way out */ |
759001c5 |
memcpy(frame->data[1], s->palette, AVPALETTE_SIZE); |
975cdc8f |
|
df9b9567 |
*got_frame = 1; |
975cdc8f |
/** 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;
}
|
e7e2df27 |
AVCodec ff_rl2_decoder = { |
ec6402b7 |
.name = "rl2", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("RL2 video"), |
ec6402b7 |
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_RL2, |
ec6402b7 |
.priv_data_size = sizeof(Rl2Context),
.init = rl2_decode_init,
.close = rl2_decode_end,
.decode = rl2_decode_frame,
.capabilities = CODEC_CAP_DR1, |
975cdc8f |
}; |