3 * Copyright (C) 2008 Sascha Sommer (saschasommer@freenet.de)
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Sascha Sommer (saschasommer@freenet.de)
26 * For more information about the RL2 format, visit:
27 * http://wiki.multimedia.cx/index.php?title=RL2
34 #include "libavutil/intreadwrite.h"
38 #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr count, palette
40 typedef struct Rl2Context {
41 AVCodecContext *avctx;
44 unsigned short video_base; ///< initial drawing offset
45 unsigned int clr_count; ///< number of used colors (currently unused)
46 unsigned char* back_frame; ///< background frame
47 unsigned int palette[AVPALETTE_COUNT];
51 * Run Length Decode a single 320x200 frame
52 * @param s rl2 context
53 * @param in input buffer
54 * @param size input buffer size
55 * @param out ouput buffer
56 * @param stride stride of the output buffer
57 * @param video_base offset of the rle data inside the frame
59 static void rl2_rle_decode(Rl2Context *s,const unsigned char* in,int size,
60 unsigned char* out,int stride,int video_base){
61 int base_x = video_base % s->avctx->width;
62 int base_y = video_base / s->avctx->width;
63 int stride_adj = stride - s->avctx->width;
65 const unsigned char* back_frame = s->back_frame;
66 const unsigned char* in_end = in + size;
67 const unsigned char* out_end = out + stride * s->avctx->height;
68 unsigned char* line_end = out + s->avctx->width;
70 /** copy start of the background frame */
71 for(i=0;i<=base_y;i++){
73 memcpy(out,back_frame,s->avctx->width);
75 back_frame += s->avctx->width;
77 back_frame += base_x - s->avctx->width;
78 line_end = out - stride_adj;
79 out += base_x - stride;
81 /** decode the variable part of the frame */
83 unsigned char val = *in++;
93 if(len >= out_end - out)
102 *out++ = (val == 0x80)? *back_frame:val;
107 if(len >= out_end - out)
113 /** copy the rest from the background frame */
115 while(out < out_end){
116 memcpy(out, back_frame, line_end - out);
117 back_frame += line_end - out;
118 out = line_end + stride_adj;
126 * Initialize the decoder
127 * @param avctx decoder context
128 * @return 0 success, -1 on error
130 static av_cold int rl2_decode_init(AVCodecContext *avctx)
132 Rl2Context *s = avctx->priv_data;
136 avctx->pix_fmt = PIX_FMT_PAL8;
137 avcodec_get_frame_defaults(&s->frame);
139 /** parse extra data */
140 if(!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE){
141 av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
145 /** get frame_offset */
146 s->video_base = AV_RL16(&avctx->extradata[0]);
147 s->clr_count = AV_RL32(&avctx->extradata[2]);
149 if(s->video_base >= avctx->width * avctx->height){
150 av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
154 /** initialize palette */
155 for(i=0;i<AVPALETTE_COUNT;i++)
156 s->palette[i] = AV_RB24(&avctx->extradata[6 + i * 3]);
158 /** decode background frame if present */
159 back_size = avctx->extradata_size - EXTRADATA1_SIZE;
162 unsigned char* back_frame = av_mallocz(avctx->width*avctx->height);
165 rl2_rle_decode(s,avctx->extradata + EXTRADATA1_SIZE,back_size,
166 back_frame,avctx->width,0);
167 s->back_frame = back_frame;
173 static int rl2_decode_frame(AVCodecContext *avctx,
174 void *data, int *data_size,
177 const uint8_t *buf = avpkt->data;
178 int buf_size = avpkt->size;
179 Rl2Context *s = avctx->priv_data;
182 avctx->release_buffer(avctx, &s->frame);
185 s->frame.reference= 0;
186 if(avctx->get_buffer(avctx, &s->frame)) {
187 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
191 /** run length decode */
192 rl2_rle_decode(s,buf,buf_size,s->frame.data[0],s->frame.linesize[0],s->video_base);
194 /** make the palette available on the way out */
195 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
197 *data_size = sizeof(AVFrame);
198 *(AVFrame*)data = s->frame;
200 /** report that the buffer was completely consumed */
207 * @param avctx decoder context
208 * @return 0 success, -1 on error
210 static av_cold int rl2_decode_end(AVCodecContext *avctx)
212 Rl2Context *s = avctx->priv_data;
215 avctx->release_buffer(avctx, &s->frame);
217 av_free(s->back_frame);
223 AVCodec ff_rl2_decoder = {
233 .long_name = NULL_IF_CONFIG_SMALL("RL2 video"),