Line | Branch | Exec | Source |
---|---|---|---|
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 { | ||
42 | AVCodecContext *avctx; | ||
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 | ||
47 | uint32_t palette[AVPALETTE_COUNT]; | ||
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 | 110 | static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size, | |
60 | uint8_t *out, ptrdiff_t stride, int video_base) | ||
61 | { | ||
62 | 110 | int base_x = video_base % s->avctx->width; | |
63 | 110 | int base_y = video_base / s->avctx->width; | |
64 | 110 | ptrdiff_t stride_adj = stride - s->avctx->width; | |
65 | 110 | const uint8_t *back_frame = s->back_frame; | |
66 | 110 | const uint8_t *in_end = in + size; | |
67 | 110 | const uint8_t *out_end = out + stride * s->avctx->height - stride_adj; | |
68 | uint8_t *line_end; | ||
69 | |||
70 | /** copy start of the background frame */ | ||
71 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 2 times.
|
110 | if (s->back_frame) { |
72 |
2/2✓ Branch 0 taken 3888 times.
✓ Branch 1 taken 108 times.
|
3996 | for (int i = 0; i <= base_y; i++) { |
73 | 3888 | memcpy(out, back_frame, s->avctx->width); | |
74 | 3888 | out += stride; | |
75 | 3888 | back_frame += s->avctx->width; | |
76 | } | ||
77 | 108 | back_frame += base_x - s->avctx->width; | |
78 | } else { | ||
79 | 2 | out += stride * (base_y + 1); | |
80 | } | ||
81 | 110 | line_end = out - stride_adj; | |
82 | 110 | out += base_x - stride; | |
83 | |||
84 | /** decode the variable part of the frame */ | ||
85 |
1/2✓ Branch 0 taken 1296630 times.
✗ Branch 1 not taken.
|
1296630 | while (in < in_end) { |
86 | 1296630 | uint8_t val = *in++; | |
87 | 1296630 | int len = 1; | |
88 |
2/2✓ Branch 0 taken 130220 times.
✓ Branch 1 taken 1166410 times.
|
1296630 | if (val >= 0x80) { |
89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 130220 times.
|
130220 | if (in >= in_end) |
90 | ✗ | break; | |
91 | 130220 | len = *in++; | |
92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 130220 times.
|
130220 | if (!len) |
93 | ✗ | break; | |
94 | 130220 | val &= 0x7F; | |
95 | } | ||
96 | |||
97 |
2/2✓ Branch 0 taken 1244636 times.
✓ Branch 1 taken 51994 times.
|
1296630 | if (back_frame) { |
98 |
2/2✓ Branch 0 taken 24596 times.
✓ Branch 1 taken 1220040 times.
|
1244636 | if (!val) { |
99 | do { | ||
100 | 42308 | size_t copy = FFMIN(line_end - out, len); | |
101 | 42308 | memcpy(out, back_frame, copy); | |
102 | 42308 | out += copy; | |
103 | 42308 | back_frame += copy; | |
104 | 42308 | len -= copy; | |
105 |
2/2✓ Branch 0 taken 17820 times.
✓ Branch 1 taken 24488 times.
|
42308 | if (out == line_end) { |
106 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 17712 times.
|
17820 | if (out == out_end) |
107 | 108 | return; | |
108 | 17712 | out += stride_adj; | |
109 | 17712 | line_end += stride; | |
110 | } | ||
111 |
2/2✓ Branch 0 taken 17712 times.
✓ Branch 1 taken 24488 times.
|
42200 | } while (len > 0); |
112 | 24488 | continue; | |
113 | } | ||
114 | 1220040 | back_frame += len; | |
115 | 1220040 | val |= 0x80; | |
116 | } | ||
117 | |||
118 |
2/2✓ Branch 0 taken 1665646 times.
✓ Branch 1 taken 1272032 times.
|
2937678 | while (len--) { |
119 | 1665646 | *out++ = val; | |
120 |
2/2✓ Branch 0 taken 400 times.
✓ Branch 1 taken 1665246 times.
|
1665646 | if (out == line_end) { |
121 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 398 times.
|
400 | if (out == out_end) |
122 | 2 | return; | |
123 | 398 | out += stride_adj; | |
124 | 398 | line_end += stride; | |
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | /** copy the rest from the background frame */ | ||
130 | ✗ | if (s->back_frame) { | |
131 | while (1) { | ||
132 | ✗ | memcpy(out, back_frame, line_end - out); | |
133 | ✗ | if (line_end == out_end) | |
134 | ✗ | break; | |
135 | ✗ | back_frame += line_end - out; | |
136 | ✗ | out = line_end + stride_adj; | |
137 | ✗ | line_end += stride; | |
138 | } | ||
139 | } | ||
140 | } | ||
141 | |||
142 | |||
143 | /** | ||
144 | * Initialize the decoder | ||
145 | * @param avctx decoder context | ||
146 | * @return 0 success, -1 on error | ||
147 | */ | ||
148 | 2 | static av_cold int rl2_decode_init(AVCodecContext *avctx) | |
149 | { | ||
150 | 2 | Rl2Context *s = avctx->priv_data; | |
151 | int back_size; | ||
152 | int i; | ||
153 | int ret; | ||
154 | |||
155 | 2 | s->avctx = avctx; | |
156 | 2 | avctx->pix_fmt = AV_PIX_FMT_PAL8; | |
157 | |||
158 | 2 | ret = ff_set_dimensions(avctx, 320, 200); | |
159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
160 | ✗ | return ret; | |
161 | |||
162 | /** parse extra data */ | ||
163 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE) { |
164 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n"); | |
165 | ✗ | return AVERROR(EINVAL); | |
166 | } | ||
167 | |||
168 | /** get frame_offset */ | ||
169 | 2 | s->video_base = AV_RL16(&avctx->extradata[0]); | |
170 | 2 | s->clr_count = AV_RL32(&avctx->extradata[2]); | |
171 | |||
172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->video_base >= avctx->width * avctx->height) { |
173 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid video_base\n"); | |
174 | ✗ | return AVERROR_INVALIDDATA; | |
175 | } | ||
176 | |||
177 | /** initialize palette */ | ||
178 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
|
514 | for (i = 0; i < AVPALETTE_COUNT; i++) |
179 | 512 | s->palette[i] = 0xFFU << 24 | AV_RB24(&avctx->extradata[6 + i * 3]); | |
180 | |||
181 | /** decode background frame if present */ | ||
182 | 2 | back_size = avctx->extradata_size - EXTRADATA1_SIZE; | |
183 | |||
184 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (back_size > 0) { |
185 | /* The 254 are padding to ensure that pointer arithmetic stays within | ||
186 | * the buffer. */ | ||
187 | 2 | uint8_t *back_frame = av_mallocz(avctx->width * avctx->height + 254); | |
188 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!back_frame) |
189 | ✗ | return AVERROR(ENOMEM); | |
190 | 2 | rl2_rle_decode(s, avctx->extradata + EXTRADATA1_SIZE, back_size, | |
191 | 2 | back_frame, avctx->width, 0); | |
192 | 2 | s->back_frame = back_frame; | |
193 | } | ||
194 | 2 | return 0; | |
195 | } | ||
196 | |||
197 | |||
198 | 108 | static int rl2_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
199 | int *got_frame, AVPacket *avpkt) | ||
200 | { | ||
201 | 108 | const uint8_t *buf = avpkt->data; | |
202 | 108 | int ret, buf_size = avpkt->size; | |
203 | 108 | Rl2Context *s = avctx->priv_data; | |
204 | |||
205 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
|
108 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
206 | ✗ | return ret; | |
207 | |||
208 | /** run length decode */ | ||
209 | 108 | rl2_rle_decode(s, buf, buf_size, frame->data[0], frame->linesize[0], | |
210 | 108 | s->video_base); | |
211 | |||
212 | /** make the palette available on the way out */ | ||
213 | 108 | memcpy(frame->data[1], s->palette, AVPALETTE_SIZE); | |
214 | |||
215 | 108 | *got_frame = 1; | |
216 | |||
217 | /** report that the buffer was completely consumed */ | ||
218 | 108 | return buf_size; | |
219 | } | ||
220 | |||
221 | |||
222 | /** | ||
223 | * Uninit decoder | ||
224 | * @param avctx decoder context | ||
225 | * @return 0 success, -1 on error | ||
226 | */ | ||
227 | 2 | static av_cold int rl2_decode_end(AVCodecContext *avctx) | |
228 | { | ||
229 | 2 | Rl2Context *s = avctx->priv_data; | |
230 | |||
231 | 2 | av_freep(&s->back_frame); | |
232 | |||
233 | 2 | return 0; | |
234 | } | ||
235 | |||
236 | |||
237 | const FFCodec ff_rl2_decoder = { | ||
238 | .p.name = "rl2", | ||
239 | CODEC_LONG_NAME("RL2 video"), | ||
240 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
241 | .p.id = AV_CODEC_ID_RL2, | ||
242 | .priv_data_size = sizeof(Rl2Context), | ||
243 | .init = rl2_decode_init, | ||
244 | .close = rl2_decode_end, | ||
245 | FF_CODEC_DECODE_CB(rl2_decode_frame), | ||
246 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
247 | }; | ||
248 |