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 #include "internal.h"
00038
00039
00040 #define EXTRADATA1_SIZE (6 + 256 * 3)
00041
00042 typedef struct Rl2Context {
00043 AVCodecContext *avctx;
00044 AVFrame frame;
00045
00046 unsigned short video_base;
00047 unsigned int clr_count;
00048 unsigned char* back_frame;
00049 unsigned int palette[AVPALETTE_COUNT];
00050 } Rl2Context;
00051
00061 static void rl2_rle_decode(Rl2Context *s,const unsigned char* in,int size,
00062 unsigned char* out,int stride,int video_base){
00063 int base_x = video_base % s->avctx->width;
00064 int base_y = video_base / s->avctx->width;
00065 int stride_adj = stride - s->avctx->width;
00066 int i;
00067 const unsigned char* back_frame = s->back_frame;
00068 const unsigned char* in_end = in + size;
00069 const unsigned char* out_end = out + stride * s->avctx->height;
00070 unsigned char* line_end;
00071
00073 for(i=0;i<=base_y;i++){
00074 if(s->back_frame)
00075 memcpy(out,back_frame,s->avctx->width);
00076 out += stride;
00077 back_frame += s->avctx->width;
00078 }
00079 back_frame += base_x - s->avctx->width;
00080 line_end = out - stride_adj;
00081 out += base_x - stride;
00082
00084 while(in < in_end){
00085 unsigned char val = *in++;
00086 int len = 1;
00087 if(val >= 0x80){
00088 if(in >= in_end)
00089 break;
00090 len = *in++;
00091 if(!len)
00092 break;
00093 }
00094
00095 if(len >= out_end - out)
00096 break;
00097
00098 if(s->back_frame)
00099 val |= 0x80;
00100 else
00101 val &= ~0x80;
00102
00103 while(len--){
00104 *out++ = (val == 0x80)? *back_frame:val;
00105 back_frame++;
00106 if(out == line_end){
00107 out += stride_adj;
00108 line_end += stride;
00109 if(len >= out_end - out)
00110 break;
00111 }
00112 }
00113 }
00114
00116 if(s->back_frame){
00117 while(out < out_end){
00118 memcpy(out, back_frame, line_end - out);
00119 back_frame += line_end - out;
00120 out = line_end + stride_adj;
00121 line_end += stride;
00122 }
00123 }
00124 }
00125
00126
00132 static av_cold int rl2_decode_init(AVCodecContext *avctx)
00133 {
00134 Rl2Context *s = avctx->priv_data;
00135 int back_size;
00136 int i;
00137 s->avctx = avctx;
00138 avctx->pix_fmt = AV_PIX_FMT_PAL8;
00139 avcodec_get_frame_defaults(&s->frame);
00140
00142 if(!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE){
00143 av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
00144 return AVERROR_INVALIDDATA;
00145 }
00146
00148 s->video_base = AV_RL16(&avctx->extradata[0]);
00149 s->clr_count = AV_RL32(&avctx->extradata[2]);
00150
00151 if(s->video_base >= avctx->width * avctx->height){
00152 av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
00153 return AVERROR_INVALIDDATA;
00154 }
00155
00157 for(i=0;i<AVPALETTE_COUNT;i++)
00158 s->palette[i] = 0xFFU << 24 | AV_RB24(&avctx->extradata[6 + i * 3]);
00159
00161 back_size = avctx->extradata_size - EXTRADATA1_SIZE;
00162
00163 if(back_size > 0){
00164 unsigned char* back_frame = av_mallocz(avctx->width*avctx->height);
00165 if(!back_frame)
00166 return AVERROR(ENOMEM);
00167 rl2_rle_decode(s,avctx->extradata + EXTRADATA1_SIZE,back_size,
00168 back_frame,avctx->width,0);
00169 s->back_frame = back_frame;
00170 }
00171 return 0;
00172 }
00173
00174
00175 static int rl2_decode_frame(AVCodecContext *avctx,
00176 void *data, int *got_frame,
00177 AVPacket *avpkt)
00178 {
00179 const uint8_t *buf = avpkt->data;
00180 int buf_size = avpkt->size;
00181 Rl2Context *s = avctx->priv_data;
00182 int ret;
00183
00184 if(s->frame.data[0])
00185 avctx->release_buffer(avctx, &s->frame);
00186
00188 s->frame.reference= 0;
00189 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
00190 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00191 return ret;
00192 }
00193
00195 rl2_rle_decode(s,buf,buf_size,s->frame.data[0],s->frame.linesize[0],s->video_base);
00196
00198 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00199
00200 *got_frame = 1;
00201 *(AVFrame*)data = s->frame;
00202
00204 return buf_size;
00205 }
00206
00207
00213 static av_cold int rl2_decode_end(AVCodecContext *avctx)
00214 {
00215 Rl2Context *s = avctx->priv_data;
00216
00217 if(s->frame.data[0])
00218 avctx->release_buffer(avctx, &s->frame);
00219
00220 av_free(s->back_frame);
00221
00222 return 0;
00223 }
00224
00225
00226 AVCodec ff_rl2_decoder = {
00227 .name = "rl2",
00228 .type = AVMEDIA_TYPE_VIDEO,
00229 .id = AV_CODEC_ID_RL2,
00230 .priv_data_size = sizeof(Rl2Context),
00231 .init = rl2_decode_init,
00232 .close = rl2_decode_end,
00233 .decode = rl2_decode_frame,
00234 .capabilities = CODEC_CAP_DR1,
00235 .long_name = NULL_IF_CONFIG_SMALL("RL2 video"),
00236 };