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