• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/rl2.c

Go to the documentation of this file.
00001 /*
00002  * RL2 Video Decoder
00003  * Copyright (C) 2008 Sascha Sommer (saschasommer@freenet.de)
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 
00033 #include "libavutil/intreadwrite.h"
00034 #include "avcodec.h"
00035 
00036 
00037 #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr count, palette
00038 
00039 typedef struct Rl2Context {
00040     AVCodecContext *avctx;
00041     AVFrame frame;
00042 
00043     unsigned short video_base; 
00044     unsigned int clr_count;    
00045     unsigned char* back_frame; 
00046     unsigned int palette[AVPALETTE_COUNT];
00047 } Rl2Context;
00048 
00058 static void rl2_rle_decode(Rl2Context *s,const unsigned char* in,int size,
00059                                unsigned char* out,int stride,int video_base){
00060     int base_x = video_base % s->avctx->width;
00061     int base_y = video_base / s->avctx->width;
00062     int stride_adj = stride - s->avctx->width;
00063     int i;
00064     const unsigned char* back_frame = s->back_frame;
00065     const unsigned char* in_end = in + size;
00066     const unsigned char* out_end = out + stride * s->avctx->height;
00067     unsigned char* line_end = out + s->avctx->width;
00068 
00070     for(i=0;i<=base_y;i++){
00071         if(s->back_frame)
00072             memcpy(out,back_frame,s->avctx->width);
00073         out += stride;
00074         back_frame += s->avctx->width;
00075     }
00076     back_frame += base_x - s->avctx->width;
00077     line_end = out - stride_adj;
00078     out += base_x - stride;
00079 
00081     while(in < in_end){
00082         unsigned char val = *in++;
00083         int len = 1;
00084         if(val >= 0x80){
00085             if(in >= in_end)
00086                 break;
00087             len = *in++;
00088             if(!len)
00089                 break;
00090         }
00091 
00092         if(len >= out_end - out)
00093             break;
00094 
00095         if(s->back_frame)
00096             val |= 0x80;
00097         else
00098             val &= ~0x80;
00099 
00100         while(len--){
00101             *out++ = (val == 0x80)? *back_frame:val;
00102             back_frame++;
00103             if(out == line_end){
00104                  out += stride_adj;
00105                  line_end += stride;
00106                  if(len >= out_end - out)
00107                      break;
00108             }
00109         }
00110     }
00111 
00113     if(s->back_frame){
00114         while(out < out_end){
00115             memcpy(out, back_frame, line_end - out);
00116             back_frame += line_end - out;
00117             out = line_end + stride_adj;
00118             line_end += stride;
00119         }
00120     }
00121 }
00122 
00123 
00129 static av_cold int rl2_decode_init(AVCodecContext *avctx)
00130 {
00131     Rl2Context *s = avctx->priv_data;
00132     int back_size;
00133     int i;
00134     s->avctx = avctx;
00135     avctx->pix_fmt = PIX_FMT_PAL8;
00136 
00138     if(!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE){
00139         av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
00140         return -1;
00141     }
00142 
00144     s->video_base = AV_RL16(&avctx->extradata[0]);
00145     s->clr_count = AV_RL32(&avctx->extradata[2]);
00146 
00147     if(s->video_base >= avctx->width * avctx->height){
00148         av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
00149         return -1;
00150     }
00151 
00153     for(i=0;i<AVPALETTE_COUNT;i++)
00154         s->palette[i] = AV_RB24(&avctx->extradata[6 + i * 3]);
00155 
00157     back_size = avctx->extradata_size - EXTRADATA1_SIZE;
00158 
00159     if(back_size > 0){
00160         unsigned char* back_frame = av_mallocz(avctx->width*avctx->height);
00161         if(!back_frame)
00162             return -1;
00163         rl2_rle_decode(s,avctx->extradata + EXTRADATA1_SIZE,back_size,
00164                            back_frame,avctx->width,0);
00165         s->back_frame = back_frame;
00166     }
00167     return 0;
00168 }
00169 
00170 
00171 static int rl2_decode_frame(AVCodecContext *avctx,
00172                               void *data, int *data_size,
00173                               AVPacket *avpkt)
00174 {
00175     const uint8_t *buf = avpkt->data;
00176     int buf_size = avpkt->size;
00177     Rl2Context *s = avctx->priv_data;
00178 
00179     if(s->frame.data[0])
00180         avctx->release_buffer(avctx, &s->frame);
00181 
00183     s->frame.reference= 0;
00184     if(avctx->get_buffer(avctx, &s->frame)) {
00185         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00186         return -1;
00187     }
00188 
00190     rl2_rle_decode(s,buf,buf_size,s->frame.data[0],s->frame.linesize[0],s->video_base);
00191 
00193     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00194 
00195     *data_size = sizeof(AVFrame);
00196     *(AVFrame*)data = s->frame;
00197 
00199     return buf_size;
00200 }
00201 
00202 
00208 static av_cold int rl2_decode_end(AVCodecContext *avctx)
00209 {
00210     Rl2Context *s = avctx->priv_data;
00211 
00212     if(s->frame.data[0])
00213         avctx->release_buffer(avctx, &s->frame);
00214 
00215     av_free(s->back_frame);
00216 
00217     return 0;
00218 }
00219 
00220 
00221 AVCodec ff_rl2_decoder = {
00222     .name           = "rl2",
00223     .type           = AVMEDIA_TYPE_VIDEO,
00224     .id             = CODEC_ID_RL2,
00225     .priv_data_size = sizeof(Rl2Context),
00226     .init           = rl2_decode_init,
00227     .close          = rl2_decode_end,
00228     .decode         = rl2_decode_frame,
00229     .capabilities   = CODEC_CAP_DR1,
00230     .long_name = NULL_IF_CONFIG_SMALL("RL2 video"),
00231 };
00232 
Generated on Thu Jul 11 2013 15:38:21 for Libav by doxygen 1.7.1