FFmpeg
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * RL2 Video Decoder
25  * @author Sascha Sommer (saschasommer@freenet.de)
26  * @see http://wiki.multimedia.cx/index.php?title=RL2
27  */
28 
29 #include <string.h>
30 
31 #include "libavutil/internal.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mem.h"
34 #include "avcodec.h"
35 #include "codec_internal.h"
36 #include "decode.h"
37 
38 
39 #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr count, palette
40 
41 typedef struct Rl2Context {
43 
44  uint16_t video_base; ///< initial drawing offset
45  uint32_t clr_count; ///< number of used colors (currently unused)
46  uint8_t *back_frame; ///< background frame
48 } Rl2Context;
49 
50 /**
51  * Run Length Decode a single 320x200 frame
52  * @param s rl2 context
53  * @param in input buffer
54  * @param size input buffer size
55  * @param out output buffer
56  * @param stride stride of the output buffer
57  * @param video_base offset of the rle data inside the frame
58  */
59 static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size,
60  uint8_t *out, int stride, int video_base)
61 {
62  int base_x = video_base % s->avctx->width;
63  int base_y = video_base / s->avctx->width;
64  int stride_adj = stride - s->avctx->width;
65  int i;
66  const uint8_t *back_frame = s->back_frame;
67  const uint8_t *in_end = in + size;
68  const uint8_t *out_end = out + stride * s->avctx->height;
69  uint8_t *line_end;
70 
71  /** copy start of the background frame */
72  for (i = 0; i <= base_y; i++) {
73  if (s->back_frame)
74  memcpy(out, back_frame, s->avctx->width);
75  out += stride;
76  back_frame += s->avctx->width;
77  }
78  back_frame += base_x - s->avctx->width;
79  line_end = out - stride_adj;
80  out += base_x - stride;
81 
82  /** decode the variable part of the frame */
83  while (in < in_end) {
84  uint8_t val = *in++;
85  int len = 1;
86  if (val >= 0x80) {
87  if (in >= in_end)
88  break;
89  len = *in++;
90  if (!len)
91  break;
92  }
93 
94  if (len >= out_end - out)
95  break;
96 
97  if (s->back_frame)
98  val |= 0x80;
99  else
100  val &= ~0x80;
101 
102  while (len--) {
103  *out++ = (val == 0x80) ? *back_frame : val;
104  back_frame++;
105  if (out == line_end) {
106  out += stride_adj;
107  line_end += stride;
108  if (len >= out_end - out)
109  break;
110  }
111  }
112  }
113 
114  /** copy the rest from the background frame */
115  if (s->back_frame) {
116  while (out < out_end) {
117  memcpy(out, back_frame, line_end - out);
118  back_frame += line_end - out;
119  out = line_end + stride_adj;
120  line_end += stride;
121  }
122  }
123 }
124 
125 
126 /**
127  * Initialize the decoder
128  * @param avctx decoder context
129  * @return 0 success, -1 on error
130  */
132 {
133  Rl2Context *s = avctx->priv_data;
134  int back_size;
135  int i;
136  int ret;
137 
138  s->avctx = avctx;
139  avctx->pix_fmt = AV_PIX_FMT_PAL8;
140 
141  ret = ff_set_dimensions(avctx, 320, 200);
142  if (ret < 0)
143  return ret;
144 
145  /** parse extra data */
146  if (!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE) {
147  av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
148  return AVERROR(EINVAL);
149  }
150 
151  /** get frame_offset */
152  s->video_base = AV_RL16(&avctx->extradata[0]);
153  s->clr_count = AV_RL32(&avctx->extradata[2]);
154 
155  if (s->video_base >= avctx->width * avctx->height) {
156  av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
157  return AVERROR_INVALIDDATA;
158  }
159 
160  /** initialize palette */
161  for (i = 0; i < AVPALETTE_COUNT; i++)
162  s->palette[i] = 0xFFU << 24 | AV_RB24(&avctx->extradata[6 + i * 3]);
163 
164  /** decode background frame if present */
165  back_size = avctx->extradata_size - EXTRADATA1_SIZE;
166 
167  if (back_size > 0) {
168  uint8_t *back_frame = av_mallocz(avctx->width*avctx->height);
169  if (!back_frame)
170  return AVERROR(ENOMEM);
171  rl2_rle_decode(s, avctx->extradata + EXTRADATA1_SIZE, back_size,
172  back_frame, avctx->width, 0);
173  s->back_frame = back_frame;
174  }
175  return 0;
176 }
177 
178 
180  int *got_frame, AVPacket *avpkt)
181 {
182  const uint8_t *buf = avpkt->data;
183  int ret, buf_size = avpkt->size;
184  Rl2Context *s = avctx->priv_data;
185 
186  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
187  return ret;
188 
189  /** run length decode */
190  rl2_rle_decode(s, buf, buf_size, frame->data[0], frame->linesize[0],
191  s->video_base);
192 
193  /** make the palette available on the way out */
194  memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
195 
196  *got_frame = 1;
197 
198  /** report that the buffer was completely consumed */
199  return buf_size;
200 }
201 
202 
203 /**
204  * Uninit decoder
205  * @param avctx decoder context
206  * @return 0 success, -1 on error
207  */
209 {
210  Rl2Context *s = avctx->priv_data;
211 
212  av_freep(&s->back_frame);
213 
214  return 0;
215 }
216 
217 
219  .p.name = "rl2",
220  CODEC_LONG_NAME("RL2 video"),
221  .p.type = AVMEDIA_TYPE_VIDEO,
222  .p.id = AV_CODEC_ID_RL2,
223  .priv_data_size = sizeof(Rl2Context),
225  .close = rl2_decode_end,
227  .p.capabilities = AV_CODEC_CAP_DR1,
228 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out
FILE * out
Definition: movenc.c:54
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:119
Rl2Context::clr_count
uint32_t clr_count
number of used colors (currently unused)
Definition: rl2.c:45
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:91
Rl2Context::back_frame
uint8_t * back_frame
background frame
Definition: rl2.c:46
init
static int init
Definition: av_tx.c:47
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
val
static double val(void *priv, double ch)
Definition: aeval.c:77
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:500
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:298
rl2_decode_frame
static int rl2_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: rl2.c:179
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_CODEC_ID_RL2
@ AV_CODEC_ID_RL2
Definition: codec_id.h:166
decode.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
Rl2Context::avctx
AVCodecContext * avctx
Definition: rl2.c:42
ff_rl2_decoder
const FFCodec ff_rl2_decoder
Definition: rl2.c:218
Rl2Context
Definition: rl2.c:41
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AVPALETTE_COUNT
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1450
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
codec_internal.h
size
int size
Definition: twinvq_data.h:10344
rl2_decode_init
static av_cold int rl2_decode_init(AVCodecContext *avctx)
Initialize the decoder.
Definition: rl2.c:131
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:499
internal.h
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:264
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:571
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:608
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
rl2_decode_end
static av_cold int rl2_decode_end(AVCodecContext *avctx)
Uninit decoder.
Definition: rl2.c:208
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecContext
main external API structure.
Definition: avcodec.h:398
rl2_rle_decode
static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size, uint8_t *out, int stride, int video_base)
Run Length Decode a single 320x200 frame.
Definition: rl2.c:59
Rl2Context::video_base
uint16_t video_base
initial drawing offset
Definition: rl2.c:44
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
EXTRADATA1_SIZE
#define EXTRADATA1_SIZE
video base, clr count, palette
Definition: rl2.c:39
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:571
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97
Rl2Context::palette
uint32_t palette[AVPALETTE_COUNT]
Definition: rl2.c:47