libavcodec/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 <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) ///< video base, clr count, palette
41 
42 typedef struct Rl2Context {
44 
45  uint16_t video_base; ///< initial drawing offset
46  uint32_t clr_count; ///< number of used colors (currently unused)
47  uint8_t *back_frame; ///< background frame
49 } Rl2Context;
50 
51 /**
52  * Run Length Decode a single 320x200 frame
53  * @param s rl2 context
54  * @param in input buffer
55  * @param size input buffer size
56  * @param out output buffer
57  * @param stride stride of the output buffer
58  * @param video_base offset of the rle data inside the frame
59  */
60 static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size,
61  uint8_t *out, int stride, int video_base)
62 {
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 uint8_t *back_frame = s->back_frame;
68  const uint8_t *in_end = in + size;
69  const uint8_t *out_end = out + stride * s->avctx->height;
70  uint8_t *line_end;
71 
72  /** copy start of the background frame */
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 
83  /** decode the variable part of the frame */
84  while (in < in_end) {
85  uint8_t 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 
115  /** copy the rest from the background frame */
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 
127 /**
128  * Initialize the decoder
129  * @param avctx decoder context
130  * @return 0 success, -1 on error
131  */
133 {
134  Rl2Context *s = avctx->priv_data;
135  int back_size;
136  int i;
137 
138  s->avctx = avctx;
139  avctx->pix_fmt = AV_PIX_FMT_PAL8;
140 
141  /** parse extra data */
142  if (!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE) {
143  av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
144  return AVERROR(EINVAL);
145  }
146 
147  /** get frame_offset */
148  s->video_base = AV_RL16(&avctx->extradata[0]);
149  s->clr_count = AV_RL32(&avctx->extradata[2]);
150 
151  if (s->video_base >= avctx->width * avctx->height) {
152  av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
153  return AVERROR_INVALIDDATA;
154  }
155 
156  /** initialize palette */
157  for (i = 0; i < AVPALETTE_COUNT; i++)
158  s->palette[i] = 0xFFU << 24 | AV_RB24(&avctx->extradata[6 + i * 3]);
159 
160  /** decode background frame if present */
161  back_size = avctx->extradata_size - EXTRADATA1_SIZE;
162 
163  if (back_size > 0) {
164  uint8_t *back_frame = av_mallocz(avctx->width*avctx->height);
165  if (!back_frame)
166  return AVERROR(ENOMEM);
167  rl2_rle_decode(s, avctx->extradata + EXTRADATA1_SIZE, back_size,
168  back_frame, avctx->width, 0);
169  s->back_frame = back_frame;
170  }
171  return 0;
172 }
173 
174 
176  void *data, int *got_frame,
177  AVPacket *avpkt)
178 {
179  AVFrame *frame = data;
180  const uint8_t *buf = avpkt->data;
181  int ret, buf_size = avpkt->size;
182  Rl2Context *s = avctx->priv_data;
183 
184  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
185  return ret;
186 
187  /** run length decode */
188  rl2_rle_decode(s, buf, buf_size, frame->data[0], frame->linesize[0],
189  s->video_base);
190 
191  /** make the palette available on the way out */
192  memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
193 
194  *got_frame = 1;
195 
196  /** report that the buffer was completely consumed */
197  return buf_size;
198 }
199 
200 
201 /**
202  * Uninit decoder
203  * @param avctx decoder context
204  * @return 0 success, -1 on error
205  */
207 {
208  Rl2Context *s = avctx->priv_data;
209 
210  av_free(s->back_frame);
211 
212  return 0;
213 }
214 
215 
217  .name = "rl2",
218  .type = AVMEDIA_TYPE_VIDEO,
219  .id = AV_CODEC_ID_RL2,
220  .priv_data_size = sizeof(Rl2Context),
224  .capabilities = CODEC_CAP_DR1,
225  .long_name = NULL_IF_CONFIG_SMALL("RL2 video"),
226 };
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
memory handling functions
#define EXTRADATA1_SIZE
video base, clr count, palette
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static av_cold int rl2_decode_end(AVCodecContext *avctx)
Uninit decoder.
#define AV_RB24
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define AV_RL16
int stride
Definition: mace.c:144
uint8_t
#define av_cold
Definition: attributes.h:78
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
#define AVPALETTE_SIZE
Definition: pixfmt.h:33
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVCodec ff_rl2_decoder
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
AVCodecContext * avctx
frame
Definition: stft.m:14
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
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.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
int size
common internal API header
ret
Definition: avfilter.c:821
int width
picture width / height.
static int rl2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define AV_RL32
uint8_t * back_frame
background frame
uint16_t video_base
initial drawing offset
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
synthesis window for stochastic i
uint32_t palette[AVPALETTE_COUNT]
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
#define AVPALETTE_COUNT
Definition: pixfmt.h:34
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
static av_cold int rl2_decode_init(AVCodecContext *avctx)
Initialize the decoder.
uint32_t clr_count
number of used colors (currently unused)
int len
struct Rl2Context Rl2Context
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
This structure stores compressed data.