00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033
00034 #include "libavutil/intreadwrite.h"
00035 #include "avcodec.h"
00036
00037
00038 #define EXTRADATA1_SIZE (6 + 256 * 3)
00039
00040 typedef struct Rl2Context {
00041 AVCodecContext *avctx;
00042 AVFrame frame;
00043
00044 unsigned short video_base;
00045 unsigned int clr_count;
00046 unsigned char* back_frame;
00047 unsigned int palette[AVPALETTE_COUNT];
00048 } Rl2Context;
00049
00059 static void rl2_rle_decode(Rl2Context *s,const unsigned char* in,int size,
00060 unsigned char* out,int stride,int video_base){
00061 int base_x = video_base % s->avctx->width;
00062 int base_y = video_base / s->avctx->width;
00063 int stride_adj = stride - s->avctx->width;
00064 int i;
00065 const unsigned char* back_frame = s->back_frame;
00066 const unsigned char* in_end = in + size;
00067 const unsigned char* out_end = out + stride * s->avctx->height;
00068 unsigned char* line_end = out + s->avctx->width;
00069
00071 for(i=0;i<=base_y;i++){
00072 if(s->back_frame)
00073 memcpy(out,back_frame,s->avctx->width);
00074 out += stride;
00075 back_frame += s->avctx->width;
00076 }
00077 back_frame += base_x - s->avctx->width;
00078 line_end = out - stride_adj;
00079 out += base_x - stride;
00080
00082 while(in < in_end){
00083 unsigned char val = *in++;
00084 int len = 1;
00085 if(val >= 0x80){
00086 if(in >= in_end)
00087 break;
00088 len = *in++;
00089 if(!len)
00090 break;
00091 }
00092
00093 if(len >= out_end - out)
00094 break;
00095
00096 if(s->back_frame)
00097 val |= 0x80;
00098 else
00099 val &= ~0x80;
00100
00101 while(len--){
00102 *out++ = (val == 0x80)? *back_frame:val;
00103 back_frame++;
00104 if(out == line_end){
00105 out += stride_adj;
00106 line_end += stride;
00107 if(len >= out_end - out)
00108 break;
00109 }
00110 }
00111 }
00112
00114 if(s->back_frame){
00115 while(out < out_end){
00116 memcpy(out, back_frame, line_end - out);
00117 back_frame += line_end - out;
00118 out = line_end + stride_adj;
00119 line_end += stride;
00120 }
00121 }
00122 }
00123
00124
00130 static av_cold int rl2_decode_init(AVCodecContext *avctx)
00131 {
00132 Rl2Context *s = avctx->priv_data;
00133 int back_size;
00134 int i;
00135 s->avctx = avctx;
00136 avctx->pix_fmt = PIX_FMT_PAL8;
00137 avcodec_get_frame_defaults(&s->frame);
00138
00140 if(!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE){
00141 av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
00142 return -1;
00143 }
00144
00146 s->video_base = AV_RL16(&avctx->extradata[0]);
00147 s->clr_count = AV_RL32(&avctx->extradata[2]);
00148
00149 if(s->video_base >= avctx->width * avctx->height){
00150 av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
00151 return -1;
00152 }
00153
00155 for(i=0;i<AVPALETTE_COUNT;i++)
00156 s->palette[i] = AV_RB24(&avctx->extradata[6 + i * 3]);
00157
00159 back_size = avctx->extradata_size - EXTRADATA1_SIZE;
00160
00161 if(back_size > 0){
00162 unsigned char* back_frame = av_mallocz(avctx->width*avctx->height);
00163 if(!back_frame)
00164 return -1;
00165 rl2_rle_decode(s,avctx->extradata + EXTRADATA1_SIZE,back_size,
00166 back_frame,avctx->width,0);
00167 s->back_frame = back_frame;
00168 }
00169 return 0;
00170 }
00171
00172
00173 static int rl2_decode_frame(AVCodecContext *avctx,
00174 void *data, int *data_size,
00175 AVPacket *avpkt)
00176 {
00177 const uint8_t *buf = avpkt->data;
00178 int buf_size = avpkt->size;
00179 Rl2Context *s = avctx->priv_data;
00180
00181 if(s->frame.data[0])
00182 avctx->release_buffer(avctx, &s->frame);
00183
00185 s->frame.reference= 0;
00186 if(avctx->get_buffer(avctx, &s->frame)) {
00187 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00188 return -1;
00189 }
00190
00192 rl2_rle_decode(s,buf,buf_size,s->frame.data[0],s->frame.linesize[0],s->video_base);
00193
00195 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00196
00197 *data_size = sizeof(AVFrame);
00198 *(AVFrame*)data = s->frame;
00199
00201 return buf_size;
00202 }
00203
00204
00210 static av_cold int rl2_decode_end(AVCodecContext *avctx)
00211 {
00212 Rl2Context *s = avctx->priv_data;
00213
00214 if(s->frame.data[0])
00215 avctx->release_buffer(avctx, &s->frame);
00216
00217 av_free(s->back_frame);
00218
00219 return 0;
00220 }
00221
00222
00223 AVCodec ff_rl2_decoder = {
00224 "rl2",
00225 AVMEDIA_TYPE_VIDEO,
00226 CODEC_ID_RL2,
00227 sizeof(Rl2Context),
00228 rl2_decode_init,
00229 NULL,
00230 rl2_decode_end,
00231 rl2_decode_frame,
00232 CODEC_CAP_DR1,
00233 .long_name = NULL_IF_CONFIG_SMALL("RL2 video"),
00234 };
00235