1 /*
2 * Chronomaster DFA Video Decoder
3 * Copyright (c) 2011 Konstantin Shishkov
4 * based on work by Vladimir "VAG" Gneushev
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <inttypes.h>
24
28
32
37
39 {
41
43
46
48
52
53 return 0;
54 }
55
57 {
59
62 return 0;
63 }
64
66 {
69 int mask = 0x10000, bitbuf = 0;
72
73 segments = bytestream2_get_le32(gb);
74 offset = bytestream2_get_le32(gb);
75 if (segments == 0 && offset == frame_end - frame)
76 return 0; // skip frame
77 if (frame_end - frame <= offset)
80 while (segments--) {
83 if (mask == 0x10000) {
84 bitbuf = bytestream2_get_le16u(gb);
85 mask = 1;
86 }
87 if (frame_end - frame < 2)
89 if (bitbuf & mask) {
90 v = bytestream2_get_le16(gb);
91 offset = (v & 0x1FFF) << 1;
92 count = ((v >> 13) + 2) << 1;
93 if (frame - frame_start < offset || frame_end - frame < count)
97 } else {
98 *frame++ = bytestream2_get_byte(gb);
99 *frame++ = bytestream2_get_byte(gb);
100 }
101 mask <<= 1;
102 }
103
104 return 0;
105 }
106
108 {
111 int mask = 0x10000, bitbuf = 0;
113
114 segments = bytestream2_get_le16(gb);
115 while (segments--) {
118 if (mask == 0x10000) {
119 bitbuf = bytestream2_get_le16u(gb);
120 mask = 1;
121 }
122 if (frame_end - frame < 2)
124 if (bitbuf & mask) {
125 v = bytestream2_get_le16(gb);
126 offset = (v & 0x1FFF) << 1;
127 count = ((v >> 13) + 2) << 1;
128 if (frame - frame_start < offset || frame_end - frame < count)
132 } else if (bitbuf & (mask << 1)) {
133 frame += bytestream2_get_le16(gb);
134 } else {
135 *frame++ = bytestream2_get_byte(gb);
136 *frame++ = bytestream2_get_byte(gb);
137 }
138 mask <<= 2;
139 }
140
141 return 0;
142 }
143
145 {
148 int mask = 0x10000, bitbuf = 0;
150
151 segments = bytestream2_get_le16(gb);
152 while (segments--) {
155 if (mask == 0x10000) {
156 bitbuf = bytestream2_get_le16u(gb);
157 mask = 1;
158 }
159
160 if (bitbuf & mask) {
161 v = bytestream2_get_le16(gb);
162 offset = (v & 0x1FFF) << 2;
163 count = ((v >> 13) + 2) << 1;
164 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
166 for (i = 0; i <
count; i++) {
167 frame[0] = frame[1] =
169
170 frame += 2;
171 }
172 } else if (bitbuf & (mask << 1)) {
173 v = bytestream2_get_le16(gb)*2;
174 if (frame - frame_end < v)
177 } else {
178 if (frame_end - frame < width + 3)
180 frame[0] = frame[1] =
181 frame[
width] = frame[width + 1] = bytestream2_get_byte(gb);
182 frame += 2;
183 frame[0] = frame[1] =
184 frame[
width] = frame[width + 1] = bytestream2_get_byte(gb);
185 frame += 2;
186 }
187 mask <<= 2;
188 }
189
190 return 0;
191 }
192
194 {
196 int count, lines, segments;
197
198 count = bytestream2_get_le16(gb);
199 if (count >= height)
201 frame += width *
count;
202 lines = bytestream2_get_le16(gb);
203 if (count + lines > height)
205
206 while (lines--) {
211 segments = bytestream2_get_byteu(gb);
212 while (segments--) {
213 if (frame - line_ptr <= bytestream2_peek_byte(gb))
215 line_ptr += bytestream2_get_byte(gb);
216 count = (int8_t)bytestream2_get_byte(gb);
217 if (count >= 0) {
218 if (frame - line_ptr < count)
222 } else {
224 if (frame - line_ptr < count)
226 memset(line_ptr, bytestream2_get_byte(gb), count);
227 }
229 }
230 }
231
232 return 0;
233 }
234
236 {
239 int count, i,
v, lines, segments;
241
242 lines = bytestream2_get_le16(gb);
243 if (lines > height)
245
246 while (lines--) {
249 segments = bytestream2_get_le16u(gb);
250 while ((segments & 0xC000) == 0xC000) {
251 unsigned skip_lines = -(int16_t)segments;
252 unsigned delta = -((int16_t)segments * width);
253 if (frame_end - frame <= delta || y + lines + skip_lines > height)
256 y += skip_lines;
257 segments = bytestream2_get_le16(gb);
258 }
259
260 if (frame_end <= frame)
262 if (segments & 0x8000) {
263 frame[width - 1] = segments & 0xFF;
264 segments = bytestream2_get_le16(gb);
265 }
267 if (frame_end - frame < width)
270 y++;
271 while (segments--) {
272 if (frame - line_ptr <= bytestream2_peek_byte(gb))
274 line_ptr += bytestream2_get_byte(gb);
275 count = (int8_t)bytestream2_get_byte(gb);
276 if (count >= 0) {
277 if (frame - line_ptr < count * 2)
281 line_ptr += count * 2;
282 } else {
284 if (frame - line_ptr < count * 2)
286 v = bytestream2_get_le16(gb);
287 for (i = 0; i <
count; i++)
288 bytestream_put_le16(&line_ptr, v);
289 }
290 }
291 }
292
293 return 0;
294 }
295
297 {
299 uint32_t segments = bytestream2_get_le32(gb);
301
302 while (segments--) {
305 copy = bytestream2_get_byteu(gb) * 2;
306 skip = bytestream2_get_byteu(gb) * 2;
307 if (frame_end - frame < copy + skip ||
310 frame += skip;
313 }
314
315 return 0;
316 }
317
319 {
320 memset(frame, 0, width * height);
321 return 0;
322 }
323
324
326
330 };
331
333 "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
334 };
335
337 void *
data,
int *got_frame,
339 {
344 uint32_t chunk_type, chunk_size;
347 int i, pal_elems;
349
352
356 chunk_size = bytestream2_get_le32(&gb);
357 chunk_type = bytestream2_get_le32(&gb);
358 if (!chunk_type)
359 break;
360 if (chunk_type == 1) {
361 pal_elems =
FFMIN(chunk_size / 3, 256);
362 for (i = 0; i < pal_elems; i++) {
363 s->
pal[i] = bytestream2_get_be24(&gb) << 2;
364 s->
pal[i] |= 0xFF
U << 24 | (s->
pal[i] >> 6) & 0x30303;
365 }
367 } else if (chunk_type <= 9) {
372 }
373 } else {
375 "Ignoring unknown chunk type %"PRIu32"\n",
376 chunk_type);
377 }
378 buf += chunk_size;
379 }
380
382 dst = frame->
data[0];
383 for (i = 0; i < avctx->
height; i++) {
384 if(version == 0x100) {
385 int j;
386 for(j = 0; j < avctx->
width; j++) {
387 dst[j] = buf[ (i&3)*(avctx->
width /4) + (j/4) +
389 }
390 } else {
391 memcpy(dst, buf, avctx->
width);
393 }
395 }
396 memcpy(frame->
data[1], s->
pal,
sizeof(s->
pal));
397
398 *got_frame = 1;
399
401 }
402
404 {
406
408
409 return 0;
410 }
411
422 };