rl2.c
Go to the documentation of this file.
1 /*
2  * RL2 Video Decoder
3  * Copyright (C) 2008 Sascha Sommer (saschasommer@freenet.de)
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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.
11  *
12  * Libav 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "libavutil/internal.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/mem.h"
36 #include "avcodec.h"
37 #include "internal.h"
38 
39 
40 #define EXTRADATA1_SIZE (6 + 256 * 3)
41 
42 typedef struct Rl2Context {
45 
46  unsigned short video_base;
47  unsigned int clr_count;
48  unsigned char* back_frame;
49  unsigned int palette[AVPALETTE_COUNT];
50 } Rl2Context;
51 
61 static void rl2_rle_decode(Rl2Context *s,const unsigned char* in,int size,
62  unsigned char* out,int stride,int video_base){
63  int base_x = video_base % s->avctx->width;
64  int base_y = video_base / s->avctx->width;
65  int stride_adj = stride - s->avctx->width;
66  int i;
67  const unsigned char* back_frame = s->back_frame;
68  const unsigned char* in_end = in + size;
69  const unsigned char* out_end = out + stride * s->avctx->height;
70  unsigned char* line_end;
71 
73  for(i=0;i<=base_y;i++){
74  if(s->back_frame)
75  memcpy(out,back_frame,s->avctx->width);
76  out += stride;
77  back_frame += s->avctx->width;
78  }
79  back_frame += base_x - s->avctx->width;
80  line_end = out - stride_adj;
81  out += base_x - stride;
82 
84  while(in < in_end){
85  unsigned char val = *in++;
86  int len = 1;
87  if(val >= 0x80){
88  if(in >= in_end)
89  break;
90  len = *in++;
91  if(!len)
92  break;
93  }
94 
95  if(len >= out_end - out)
96  break;
97 
98  if(s->back_frame)
99  val |= 0x80;
100  else
101  val &= ~0x80;
102 
103  while(len--){
104  *out++ = (val == 0x80)? *back_frame:val;
105  back_frame++;
106  if(out == line_end){
107  out += stride_adj;
108  line_end += stride;
109  if(len >= out_end - out)
110  break;
111  }
112  }
113  }
114 
116  if(s->back_frame){
117  while(out < out_end){
118  memcpy(out, back_frame, line_end - out);
119  back_frame += line_end - out;
120  out = line_end + stride_adj;
121  line_end += stride;
122  }
123  }
124 }
125 
126 
133 {
134  Rl2Context *s = avctx->priv_data;
135  int back_size;
136  int i;
137  s->avctx = avctx;
138  avctx->pix_fmt = AV_PIX_FMT_PAL8;
139 
141  if(!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE){
142  av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
143  return -1;
144  }
145 
147  s->video_base = AV_RL16(&avctx->extradata[0]);
148  s->clr_count = AV_RL32(&avctx->extradata[2]);
149 
150  if(s->video_base >= avctx->width * avctx->height){
151  av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
152  return -1;
153  }
154 
156  for(i=0;i<AVPALETTE_COUNT;i++)
157  s->palette[i] = AV_RB24(&avctx->extradata[6 + i * 3]);
158 
160  back_size = avctx->extradata_size - EXTRADATA1_SIZE;
161 
162  if(back_size > 0){
163  unsigned char* back_frame = av_mallocz(avctx->width*avctx->height);
164  if(!back_frame)
165  return -1;
166  rl2_rle_decode(s,avctx->extradata + EXTRADATA1_SIZE,back_size,
167  back_frame,avctx->width,0);
168  s->back_frame = back_frame;
169  }
170  return 0;
171 }
172 
173 
175  void *data, int *got_frame,
176  AVPacket *avpkt)
177 {
178  const uint8_t *buf = avpkt->data;
179  int buf_size = avpkt->size;
180  Rl2Context *s = avctx->priv_data;
181 
182  if(s->frame.data[0])
183  avctx->release_buffer(avctx, &s->frame);
184 
186  s->frame.reference= 0;
187  if(ff_get_buffer(avctx, &s->frame)) {
188  av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
189  return -1;
190  }
191 
193  rl2_rle_decode(s,buf,buf_size,s->frame.data[0],s->frame.linesize[0],s->video_base);
194 
196  memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
197 
198  *got_frame = 1;
199  *(AVFrame*)data = s->frame;
200 
202  return buf_size;
203 }
204 
205 
212 {
213  Rl2Context *s = avctx->priv_data;
214 
215  if(s->frame.data[0])
216  avctx->release_buffer(avctx, &s->frame);
217 
218  av_free(s->back_frame);
219 
220  return 0;
221 }
222 
223 
225  .name = "rl2",
226  .type = AVMEDIA_TYPE_VIDEO,
227  .id = AV_CODEC_ID_RL2,
228  .priv_data_size = sizeof(Rl2Context),
232  .capabilities = CODEC_CAP_DR1,
233  .long_name = NULL_IF_CONFIG_SMALL("RL2 video"),
234 };