Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/rl2.c |
Date: | 2022-07-07 01:21:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 74 | 84 | 88.1% |
Branches: | 33 | 44 | 75.0% |
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 <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 "codec_internal.h" | ||
38 | #include "internal.h" | ||
39 | |||
40 | |||
41 | #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr count, palette | ||
42 | |||
43 | typedef struct Rl2Context { | ||
44 | AVCodecContext *avctx; | ||
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 | 110 | static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size, | |
62 | uint8_t *out, int stride, int video_base) | ||
63 | { | ||
64 | 110 | int base_x = video_base % s->avctx->width; | |
65 | 110 | int base_y = video_base / s->avctx->width; | |
66 | 110 | int stride_adj = stride - s->avctx->width; | |
67 | int i; | ||
68 | 110 | const uint8_t *back_frame = s->back_frame; | |
69 | 110 | const uint8_t *in_end = in + size; | |
70 | 110 | const uint8_t *out_end = out + stride * s->avctx->height; | |
71 | uint8_t *line_end; | ||
72 | |||
73 | /** copy start of the background frame */ | ||
74 |
2/2✓ Branch 0 taken 3890 times.
✓ Branch 1 taken 110 times.
|
4000 | for (i = 0; i <= base_y; i++) { |
75 |
2/2✓ Branch 0 taken 3888 times.
✓ Branch 1 taken 2 times.
|
3890 | if (s->back_frame) |
76 | 3888 | memcpy(out, back_frame, s->avctx->width); | |
77 | 3890 | out += stride; | |
78 | 3890 | back_frame += s->avctx->width; | |
79 | } | ||
80 | 110 | back_frame += base_x - s->avctx->width; | |
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 | } | ||
95 | |||
96 |
2/2✓ Branch 0 taken 110 times.
✓ Branch 1 taken 1296520 times.
|
1296630 | if (len >= out_end - out) |
97 | 110 | break; | |
98 | |||
99 |
2/2✓ Branch 0 taken 1244528 times.
✓ Branch 1 taken 51992 times.
|
1296520 | if (s->back_frame) |
100 | 1244528 | val |= 0x80; | |
101 | else | ||
102 | 51992 | val &= ~0x80; | |
103 | |||
104 |
2/2✓ Branch 0 taken 5802447 times.
✓ Branch 1 taken 1296520 times.
|
7098967 | while (len--) { |
105 |
2/2✓ Branch 0 taken 4136803 times.
✓ Branch 1 taken 1665644 times.
|
5802447 | *out++ = (val == 0x80) ? *back_frame : val; |
106 | 5802447 | back_frame++; | |
107 |
2/2✓ Branch 0 taken 18110 times.
✓ Branch 1 taken 5784337 times.
|
5802447 | if (out == line_end) { |
108 | 18110 | out += stride_adj; | |
109 | 18110 | line_end += stride; | |
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18110 times.
|
18110 | if (len >= out_end - out) |
111 | ✗ | break; | |
112 | } | ||
113 | } | ||
114 | } | ||
115 | |||
116 | /** copy the rest from the background frame */ | ||
117 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 2 times.
|
110 | if (s->back_frame) { |
118 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 108 times.
|
216 | while (out < out_end) { |
119 | 108 | memcpy(out, back_frame, line_end - out); | |
120 | 108 | back_frame += line_end - out; | |
121 | 108 | out = line_end + stride_adj; | |
122 | 108 | line_end += stride; | |
123 | } | ||
124 | } | ||
125 | 110 | } | |
126 | |||
127 | |||
128 | /** | ||
129 | * Initialize the decoder | ||
130 | * @param avctx decoder context | ||
131 | * @return 0 success, -1 on error | ||
132 | */ | ||
133 | 2 | static av_cold int rl2_decode_init(AVCodecContext *avctx) | |
134 | { | ||
135 | 2 | Rl2Context *s = avctx->priv_data; | |
136 | int back_size; | ||
137 | int i; | ||
138 | int ret; | ||
139 | |||
140 | 2 | s->avctx = avctx; | |
141 | 2 | avctx->pix_fmt = AV_PIX_FMT_PAL8; | |
142 | |||
143 | 2 | ret = ff_set_dimensions(avctx, 320, 200); | |
144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
145 | ✗ | return ret; | |
146 | |||
147 | /** parse extra data */ | ||
148 |
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) { |
149 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n"); | |
150 | ✗ | return AVERROR(EINVAL); | |
151 | } | ||
152 | |||
153 | /** get frame_offset */ | ||
154 | 2 | s->video_base = AV_RL16(&avctx->extradata[0]); | |
155 | 2 | s->clr_count = AV_RL32(&avctx->extradata[2]); | |
156 | |||
157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->video_base >= avctx->width * avctx->height) { |
158 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid video_base\n"); | |
159 | ✗ | return AVERROR_INVALIDDATA; | |
160 | } | ||
161 | |||
162 | /** initialize palette */ | ||
163 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
|
514 | for (i = 0; i < AVPALETTE_COUNT; i++) |
164 | 512 | s->palette[i] = 0xFFU << 24 | AV_RB24(&avctx->extradata[6 + i * 3]); | |
165 | |||
166 | /** decode background frame if present */ | ||
167 | 2 | back_size = avctx->extradata_size - EXTRADATA1_SIZE; | |
168 | |||
169 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (back_size > 0) { |
170 | 2 | uint8_t *back_frame = av_mallocz(avctx->width*avctx->height); | |
171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!back_frame) |
172 | ✗ | return AVERROR(ENOMEM); | |
173 | 2 | rl2_rle_decode(s, avctx->extradata + EXTRADATA1_SIZE, back_size, | |
174 | back_frame, avctx->width, 0); | ||
175 | 2 | s->back_frame = back_frame; | |
176 | } | ||
177 | 2 | return 0; | |
178 | } | ||
179 | |||
180 | |||
181 | 108 | static int rl2_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
182 | int *got_frame, AVPacket *avpkt) | ||
183 | { | ||
184 | 108 | const uint8_t *buf = avpkt->data; | |
185 | 108 | int ret, buf_size = avpkt->size; | |
186 | 108 | Rl2Context *s = avctx->priv_data; | |
187 | |||
188 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
|
108 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
189 | ✗ | return ret; | |
190 | |||
191 | /** run length decode */ | ||
192 | 108 | rl2_rle_decode(s, buf, buf_size, frame->data[0], frame->linesize[0], | |
193 | 108 | s->video_base); | |
194 | |||
195 | /** make the palette available on the way out */ | ||
196 | 108 | memcpy(frame->data[1], s->palette, AVPALETTE_SIZE); | |
197 | |||
198 | 108 | *got_frame = 1; | |
199 | |||
200 | /** report that the buffer was completely consumed */ | ||
201 | 108 | return buf_size; | |
202 | } | ||
203 | |||
204 | |||
205 | /** | ||
206 | * Uninit decoder | ||
207 | * @param avctx decoder context | ||
208 | * @return 0 success, -1 on error | ||
209 | */ | ||
210 | 2 | static av_cold int rl2_decode_end(AVCodecContext *avctx) | |
211 | { | ||
212 | 2 | Rl2Context *s = avctx->priv_data; | |
213 | |||
214 | 2 | av_freep(&s->back_frame); | |
215 | |||
216 | 2 | return 0; | |
217 | } | ||
218 | |||
219 | |||
220 | const FFCodec ff_rl2_decoder = { | ||
221 | .p.name = "rl2", | ||
222 | .p.long_name = NULL_IF_CONFIG_SMALL("RL2 video"), | ||
223 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
224 | .p.id = AV_CODEC_ID_RL2, | ||
225 | .priv_data_size = sizeof(Rl2Context), | ||
226 | .init = rl2_decode_init, | ||
227 | .close = rl2_decode_end, | ||
228 | FF_CODEC_DECODE_CB(rl2_decode_frame), | ||
229 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
230 | .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, | ||
231 | }; | ||
232 |