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;
71 unsigned segments;
73
74 segments = bytestream2_get_le32(gb);
75 offset = bytestream2_get_le32(gb);
76 if (segments == 0 && offset == frame_end - frame)
77 return 0; // skip frame
78 if (frame_end - frame <= offset)
81 while (segments--) {
84 if (mask == 0x10000) {
85 bitbuf = bytestream2_get_le16u(gb);
86 mask = 1;
87 }
88 if (frame_end - frame < 2)
90 if (bitbuf & mask) {
91 v = bytestream2_get_le16(gb);
92 offset = (v & 0x1FFF) << 1;
93 count = ((v >> 13) + 2) << 1;
94 if (frame - frame_start < offset || frame_end - frame < count)
98 } else {
99 *frame++ = bytestream2_get_byte(gb);
100 *frame++ = bytestream2_get_byte(gb);
101 }
102 mask <<= 1;
103 }
104
105 return 0;
106 }
107
109 {
112 int mask = 0x10000, bitbuf = 0;
114
115 segments = bytestream2_get_le16(gb);
116 while (segments--) {
119 if (mask == 0x10000) {
120 bitbuf = bytestream2_get_le16u(gb);
121 mask = 1;
122 }
123 if (frame_end - frame < 2)
125 if (bitbuf & mask) {
126 v = bytestream2_get_le16(gb);
127 offset = (v & 0x1FFF) << 1;
128 count = ((v >> 13) + 2) << 1;
129 if (frame - frame_start < offset || frame_end - frame < count)
133 } else if (bitbuf & (mask << 1)) {
134 frame += bytestream2_get_le16(gb);
135 } else {
136 *frame++ = bytestream2_get_byte(gb);
137 *frame++ = bytestream2_get_byte(gb);
138 }
139 mask <<= 2;
140 }
141
142 return 0;
143 }
144
146 {
149 int mask = 0x10000, bitbuf = 0;
151
152 if ((width | height) & 1)
154 segments = bytestream2_get_le16(gb);
155 while (segments--) {
158 if (mask == 0x10000) {
159 bitbuf = bytestream2_get_le16u(gb);
160 mask = 1;
161 }
162
163 if (bitbuf & mask) {
164 v = bytestream2_get_le16(gb);
165 offset = (v & 0x1FFF) << 2;
166 count = ((v >> 13) + 2) << 1;
167 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
169 for (i = 0; i <
count; i++) {
170 frame[0] = frame[1] =
172
173 frame += 2;
174 }
175 } else if (bitbuf & (mask << 1)) {
176 v = bytestream2_get_le16(gb)*2;
177 if (frame - frame_end < v)
179 frame += v;
180 } else {
181 if (width < 4 || frame_end - frame < width + 4)
183 frame[0] = frame[1] =
184 frame[
width] = frame[width + 1] = bytestream2_get_byte(gb);
185 frame += 2;
186 frame[0] = frame[1] =
187 frame[
width] = frame[width + 1] = bytestream2_get_byte(gb);
188 frame += 2;
189 }
190 mask <<= 2;
191 }
192
193 return 0;
194 }
195
197 {
199 int count, lines, segments;
200
201 count = bytestream2_get_le16(gb);
202 if (count >= height)
204 frame += width *
count;
205 lines = bytestream2_get_le16(gb);
206 if (count + lines > height)
208
209 while (lines--) {
214 segments = bytestream2_get_byteu(gb);
215 while (segments--) {
216 if (frame - line_ptr <= bytestream2_peek_byte(gb))
218 line_ptr += bytestream2_get_byte(gb);
219 count = (int8_t)bytestream2_get_byte(gb);
220 if (count >= 0) {
221 if (frame - line_ptr < count)
225 } else {
227 if (frame - line_ptr < count)
229 memset(line_ptr, bytestream2_get_byte(gb), count);
230 }
232 }
233 }
234
235 return 0;
236 }
237
239 {
242 int count, i, v, lines, segments;
243 int y = 0;
244
245 lines = bytestream2_get_le16(gb);
246 if (lines > height)
248
249 while (lines--) {
252 segments = bytestream2_get_le16u(gb);
253 while ((segments & 0xC000) == 0xC000) {
254 unsigned skip_lines = -(int16_t)segments;
255 int64_t
delta = -((int16_t)segments * (int64_t)
width);
256 if (frame_end - frame <= delta || y + lines + skip_lines > height)
259 y += skip_lines;
260 segments = bytestream2_get_le16(gb);
261 }
262
263 if (frame_end <= frame)
265 if (segments & 0x8000) {
266 frame[width - 1] = segments & 0xFF;
267 segments = bytestream2_get_le16(gb);
268 }
270 if (frame_end - frame < width)
273 y++;
274 while (segments--) {
275 if (frame - line_ptr <= bytestream2_peek_byte(gb))
277 line_ptr += bytestream2_get_byte(gb);
278 count = (int8_t)bytestream2_get_byte(gb);
279 if (count >= 0) {
280 if (frame - line_ptr < count * 2)
284 line_ptr += count * 2;
285 } else {
287 if (frame - line_ptr < count * 2)
289 v = bytestream2_get_le16(gb);
290 for (i = 0; i <
count; i++)
291 bytestream_put_le16(&line_ptr, v);
292 }
293 }
294 }
295
296 return 0;
297 }
298
300 {
302 uint32_t segments = bytestream2_get_le32(gb);
304
305 while (segments--) {
308 copy = bytestream2_get_byteu(gb) * 2;
309 skip = bytestream2_get_byteu(gb) * 2;
310 if (frame_end - frame < copy + skip ||
313 frame += skip;
316 }
317
318 return 0;
319 }
320
322 {
323 memset(frame, 0, width * height);
324 return 0;
325 }
326
327
329
333 };
334
336 "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
337 };
338
340 void *
data,
int *got_frame,
342 {
347 uint32_t chunk_type, chunk_size;
349 int ret;
350 int i, pal_elems;
352
354 return ret;
355
359 chunk_size = bytestream2_get_le32(&gb);
360 chunk_type = bytestream2_get_le32(&gb);
361 if (!chunk_type)
362 break;
363 if (chunk_type == 1) {
364 pal_elems =
FFMIN(chunk_size / 3, 256);
365 for (i = 0; i < pal_elems; i++) {
366 s->
pal[i] = bytestream2_get_be24(&gb) << 2;
367 s->
pal[i] |= 0xFF
U << 24 | (s->
pal[i] >> 6) & 0x30303;
368 }
370 } else if (chunk_type <= 9) {
375 }
376 } else {
378 "Ignoring unknown chunk type %"PRIu32"\n",
379 chunk_type);
380 }
381 buf += chunk_size;
382 }
383
385 dst = frame->
data[0];
386 for (i = 0; i < avctx->
height; i++) {
387 if(version == 0x100) {
388 int j;
389 for(j = 0; j < avctx->
width; j++) {
390 dst[j] = buf[ (i&3)*(avctx->
width /4) + (j/4) +
392 }
393 } else {
394 memcpy(dst, buf, avctx->
width);
396 }
398 }
399 memcpy(frame->
data[1], s->
pal,
sizeof(s->
pal));
400
401 *got_frame = 1;
402
404 }
405
407 {
409
411
412 return 0;
413 }
414
425 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static void copy(const float *p1, float *p2, const int length)
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
#define AV_LOG_WARNING
Something somehow does not look correct.
Memory handling functions.
static av_cold int init(AVCodecContext *avctx)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static av_cold int dfa_decode_end(AVCodecContext *avctx)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
8 bits with AV_PIX_FMT_RGB32 palette
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static const char *const chunk_name[8]
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
static int dfa_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static const uint16_t mask[17]
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
static const uint8_t offset[127][2]
static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
static void frame_end(MpegEncContext *s)
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
static const chunk_decoder decoder[8]
int width
picture width / height.
static av_cold int dfa_decode_init(AVCodecContext *avctx)
static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static int frame_start(MpegEncContext *s)
int palette_has_changed
Tell user application that palette has changed from previous frame.
static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
common internal api header.
static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
int(* chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height)
This structure stores compressed data.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.