]> git.sesse.net Git - ffmpeg/blob - libavcodec/rl2.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / rl2.c
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 {
43     AVCodecContext *avctx;
44     AVFrame frame;
45
46     uint16_t video_base;  ///< initial drawing offset
47     uint32_t clr_count;   ///< number of used colors (currently unused)
48     uint8_t *back_frame;  ///< background frame
49     uint32_t palette[AVPALETTE_COUNT];
50 } Rl2Context;
51
52 /**
53  * Run Length Decode a single 320x200 frame
54  * @param s rl2 context
55  * @param in input buffer
56  * @param size input buffer size
57  * @param out output buffer
58  * @param stride stride of the output buffer
59  * @param video_base offset of the rle data inside the frame
60  */
61 static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size,
62                                uint8_t *out, int stride, int video_base)
63 {
64     int base_x = video_base % s->avctx->width;
65     int base_y = video_base / s->avctx->width;
66     int stride_adj = stride - s->avctx->width;
67     int i;
68     const uint8_t *back_frame = s->back_frame;
69     const uint8_t *in_end     = in + size;
70     const uint8_t *out_end    = out + stride * s->avctx->height;
71     uint8_t *line_end;
72
73     /** copy start of the background frame */
74     for (i = 0; i <= base_y; i++) {
75         if (s->back_frame)
76             memcpy(out, back_frame, s->avctx->width);
77         out        += stride;
78         back_frame += s->avctx->width;
79     }
80     back_frame += base_x - s->avctx->width;
81     line_end    = out - stride_adj;
82     out        += base_x - stride;
83
84     /** decode the variable part of the frame */
85     while (in < in_end) {
86         uint8_t val = *in++;
87         int len     = 1;
88         if (val >= 0x80) {
89             if (in >= in_end)
90                 break;
91             len = *in++;
92             if (!len)
93                 break;
94         }
95
96         if (len >= out_end - out)
97             break;
98
99         if (s->back_frame)
100             val |= 0x80;
101         else
102             val &= ~0x80;
103
104         while (len--) {
105             *out++ = (val == 0x80) ? *back_frame : val;
106             back_frame++;
107             if (out == line_end) {
108                  out      += stride_adj;
109                  line_end += stride;
110                  if (len >= out_end - out)
111                      break;
112             }
113         }
114     }
115
116     /** copy the rest from the background frame */
117     if (s->back_frame) {
118         while (out < out_end) {
119             memcpy(out, back_frame, line_end - out);
120             back_frame += line_end - out;
121             out         = line_end + stride_adj;
122             line_end   += stride;
123         }
124     }
125 }
126
127
128 /**
129  * Initialize the decoder
130  * @param avctx decoder context
131  * @return 0 success, -1 on error
132  */
133 static av_cold int rl2_decode_init(AVCodecContext *avctx)
134 {
135     Rl2Context *s = avctx->priv_data;
136     int back_size;
137     int i;
138
139     s->avctx       = avctx;
140     avctx->pix_fmt = AV_PIX_FMT_PAL8;
141     avcodec_get_frame_defaults(&s->frame);
142
143     /** parse extra data */
144     if (!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE) {
145         av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
146         return AVERROR(EINVAL);
147     }
148
149     /** get frame_offset */
150     s->video_base = AV_RL16(&avctx->extradata[0]);
151     s->clr_count  = AV_RL32(&avctx->extradata[2]);
152
153     if (s->video_base >= avctx->width * avctx->height) {
154         av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
155         return AVERROR_INVALIDDATA;
156     }
157
158     /** initialize palette */
159     for (i = 0; i < AVPALETTE_COUNT; i++)
160         s->palette[i] = 0xFFU << 24 | AV_RB24(&avctx->extradata[6 + i * 3]);
161
162     /** decode background frame if present */
163     back_size = avctx->extradata_size - EXTRADATA1_SIZE;
164
165     if (back_size > 0) {
166         uint8_t *back_frame = av_mallocz(avctx->width*avctx->height);
167         if (!back_frame)
168             return AVERROR(ENOMEM);
169         rl2_rle_decode(s, avctx->extradata + EXTRADATA1_SIZE, back_size,
170                        back_frame, avctx->width, 0);
171         s->back_frame = back_frame;
172     }
173     return 0;
174 }
175
176
177 static int rl2_decode_frame(AVCodecContext *avctx,
178                             void *data, int *got_frame,
179                             AVPacket *avpkt)
180 {
181     const uint8_t *buf = avpkt->data;
182     int ret, buf_size  = avpkt->size;
183     Rl2Context *s = avctx->priv_data;
184
185     if (s->frame.data[0])
186         avctx->release_buffer(avctx, &s->frame);
187
188     /** get buffer */
189     s->frame.reference = 0;
190     if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
191         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
192         return ret;
193     }
194
195     /** run length decode */
196     rl2_rle_decode(s, buf, buf_size, s->frame.data[0], s->frame.linesize[0],
197                    s->video_base);
198
199     /** make the palette available on the way out */
200     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
201
202     *got_frame = 1;
203     *(AVFrame*)data = s->frame;
204
205     /** report that the buffer was completely consumed */
206     return buf_size;
207 }
208
209
210 /**
211  * Uninit decoder
212  * @param avctx decoder context
213  * @return 0 success, -1 on error
214  */
215 static av_cold int rl2_decode_end(AVCodecContext *avctx)
216 {
217     Rl2Context *s = avctx->priv_data;
218
219     if (s->frame.data[0])
220         avctx->release_buffer(avctx, &s->frame);
221
222     av_free(s->back_frame);
223
224     return 0;
225 }
226
227
228 AVCodec ff_rl2_decoder = {
229     .name           = "rl2",
230     .type           = AVMEDIA_TYPE_VIDEO,
231     .id             = AV_CODEC_ID_RL2,
232     .priv_data_size = sizeof(Rl2Context),
233     .init           = rl2_decode_init,
234     .close          = rl2_decode_end,
235     .decode         = rl2_decode_frame,
236     .capabilities   = CODEC_CAP_DR1,
237     .long_name      = NULL_IF_CONFIG_SMALL("RL2 video"),
238 };