2 * Chronomaster DFA Video Decoder
3 * Copyright (c) 2011 Konstantin Shishkov
4 * based on work by Vladimir "VAG" Gneushev
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "bytestream.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/mem.h"
30 typedef struct DfaContext
{
35 static av_cold
int dfa_decode_init(AVCodecContext
*avctx
)
37 DfaContext
*s
= avctx
->priv_data
;
40 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
42 if ((ret
= av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
)) < 0)
45 s
->frame_buf
= av_mallocz(avctx
->width
* avctx
->height
);
47 return AVERROR(ENOMEM
);
52 static int decode_copy(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
54 const int size
= width
* height
;
56 if (bytestream2_get_buffer(gb
, frame
, size
) != size
)
57 return AVERROR_INVALIDDATA
;
61 static int decode_tsw1(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
63 const uint8_t *frame_start
= frame
;
64 const uint8_t *frame_end
= frame
+ width
* height
;
65 int mask
= 0x10000, bitbuf
= 0;
66 int v
, count
, segments
;
69 segments
= bytestream2_get_le32(gb
);
70 offset
= bytestream2_get_le32(gb
);
71 if (frame_end
- frame
<= offset
)
72 return AVERROR_INVALIDDATA
;
75 if (bytestream2_get_bytes_left(gb
) < 2)
76 return AVERROR_INVALIDDATA
;
77 if (mask
== 0x10000) {
78 bitbuf
= bytestream2_get_le16u(gb
);
81 if (frame_end
- frame
< 2)
82 return AVERROR_INVALIDDATA
;
84 v
= bytestream2_get_le16(gb
);
85 offset
= (v
& 0x1FFF) << 1;
86 count
= ((v
>> 13) + 2) << 1;
87 if (frame
- frame_start
< offset
|| frame_end
- frame
< count
)
88 return AVERROR_INVALIDDATA
;
89 av_memcpy_backptr(frame
, offset
, count
);
92 *frame
++ = bytestream2_get_byte(gb
);
93 *frame
++ = bytestream2_get_byte(gb
);
101 static int decode_dsw1(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
103 const uint8_t *frame_start
= frame
;
104 const uint8_t *frame_end
= frame
+ width
* height
;
105 int mask
= 0x10000, bitbuf
= 0;
106 int v
, offset
, count
, segments
;
108 segments
= bytestream2_get_le16(gb
);
110 if (bytestream2_get_bytes_left(gb
) < 2)
111 return AVERROR_INVALIDDATA
;
112 if (mask
== 0x10000) {
113 bitbuf
= bytestream2_get_le16u(gb
);
116 if (frame_end
- frame
< 2)
117 return AVERROR_INVALIDDATA
;
119 v
= bytestream2_get_le16(gb
);
120 offset
= (v
& 0x1FFF) << 1;
121 count
= ((v
>> 13) + 2) << 1;
122 if (frame
- frame_start
< offset
|| frame_end
- frame
< count
)
123 return AVERROR_INVALIDDATA
;
124 av_memcpy_backptr(frame
, offset
, count
);
126 } else if (bitbuf
& (mask
<< 1)) {
127 frame
+= bytestream2_get_le16(gb
);
129 *frame
++ = bytestream2_get_byte(gb
);
130 *frame
++ = bytestream2_get_byte(gb
);
138 static int decode_dds1(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
140 const uint8_t *frame_start
= frame
;
141 const uint8_t *frame_end
= frame
+ width
* height
;
142 int mask
= 0x10000, bitbuf
= 0;
143 int i
, v
, offset
, count
, segments
;
145 segments
= bytestream2_get_le16(gb
);
147 if (bytestream2_get_bytes_left(gb
) < 2)
148 return AVERROR_INVALIDDATA
;
149 if (mask
== 0x10000) {
150 bitbuf
= bytestream2_get_le16u(gb
);
155 v
= bytestream2_get_le16(gb
);
156 offset
= (v
& 0x1FFF) << 2;
157 count
= ((v
>> 13) + 2) << 1;
158 if (frame
- frame_start
< offset
|| frame_end
- frame
< count
*2 + width
)
159 return AVERROR_INVALIDDATA
;
160 for (i
= 0; i
< count
; i
++) {
161 frame
[0] = frame
[1] =
162 frame
[width
] = frame
[width
+ 1] = frame
[-offset
];
166 } else if (bitbuf
& (mask
<< 1)) {
167 v
= bytestream2_get_le16(gb
)*2;
168 if (frame
- frame_end
< v
)
169 return AVERROR_INVALIDDATA
;
172 if (frame_end
- frame
< width
+ 3)
173 return AVERROR_INVALIDDATA
;
174 frame
[0] = frame
[1] =
175 frame
[width
] = frame
[width
+ 1] = bytestream2_get_byte(gb
);
177 frame
[0] = frame
[1] =
178 frame
[width
] = frame
[width
+ 1] = bytestream2_get_byte(gb
);
187 static int decode_bdlt(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
190 int count
, lines
, segments
;
192 count
= bytestream2_get_le16(gb
);
194 return AVERROR_INVALIDDATA
;
195 frame
+= width
* count
;
196 lines
= bytestream2_get_le16(gb
);
197 if (count
+ lines
> height
)
198 return AVERROR_INVALIDDATA
;
201 if (bytestream2_get_bytes_left(gb
) < 1)
202 return AVERROR_INVALIDDATA
;
205 segments
= bytestream2_get_byteu(gb
);
207 if (frame
- line_ptr
<= bytestream2_peek_byte(gb
))
208 return AVERROR_INVALIDDATA
;
209 line_ptr
+= bytestream2_get_byte(gb
);
210 count
= (int8_t)bytestream2_get_byte(gb
);
212 if (frame
- line_ptr
< count
)
213 return AVERROR_INVALIDDATA
;
214 if (bytestream2_get_buffer(gb
, line_ptr
, count
) != count
)
215 return AVERROR_INVALIDDATA
;
218 if (frame
- line_ptr
< count
)
219 return AVERROR_INVALIDDATA
;
220 memset(line_ptr
, bytestream2_get_byte(gb
), count
);
229 static int decode_wdlt(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
231 const uint8_t *frame_end
= frame
+ width
* height
;
233 int count
, i
, v
, lines
, segments
;
236 lines
= bytestream2_get_le16(gb
);
238 return AVERROR_INVALIDDATA
;
241 if (bytestream2_get_bytes_left(gb
) < 2)
242 return AVERROR_INVALIDDATA
;
243 segments
= bytestream2_get_le16u(gb
);
244 while ((segments
& 0xC000) == 0xC000) {
245 unsigned skip_lines
= -(int16_t)segments
;
246 unsigned delta
= -((int16_t)segments
* width
);
247 if (frame_end
- frame
<= delta
|| y
+ lines
+ skip_lines
> height
)
248 return AVERROR_INVALIDDATA
;
251 segments
= bytestream2_get_le16(gb
);
253 if (segments
& 0x8000) {
254 frame
[width
- 1] = segments
& 0xFF;
255 segments
= bytestream2_get_le16(gb
);
261 if (frame
- line_ptr
<= bytestream2_peek_byte(gb
))
262 return AVERROR_INVALIDDATA
;
263 line_ptr
+= bytestream2_get_byte(gb
);
264 count
= (int8_t)bytestream2_get_byte(gb
);
266 if (frame
- line_ptr
< count
* 2)
267 return AVERROR_INVALIDDATA
;
268 if (bytestream2_get_buffer(gb
, line_ptr
, count
* 2) != count
* 2)
269 return AVERROR_INVALIDDATA
;
270 line_ptr
+= count
* 2;
273 if (frame
- line_ptr
< count
* 2)
274 return AVERROR_INVALIDDATA
;
275 v
= bytestream2_get_le16(gb
);
276 for (i
= 0; i
< count
; i
++)
277 bytestream_put_le16(&line_ptr
, v
);
285 static int decode_unk6(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
287 return AVERROR_PATCHWELCOME
;
290 static int decode_blck(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
292 memset(frame
, 0, width
* height
);
297 typedef int (*chunk_decoder
)(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
);
299 static const chunk_decoder decoder
[8] = {
300 decode_copy
, decode_tsw1
, decode_bdlt
, decode_wdlt
,
301 decode_unk6
, decode_dsw1
, decode_blck
, decode_dds1
,
304 static const char* chunk_name
[8] = {
305 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
308 static int dfa_decode_frame(AVCodecContext
*avctx
,
309 void *data
, int *got_frame
,
312 AVFrame
*frame
= data
;
313 DfaContext
*s
= avctx
->priv_data
;
315 const uint8_t *buf
= avpkt
->data
;
316 uint32_t chunk_type
, chunk_size
;
321 if ((ret
= ff_get_buffer(avctx
, frame
, 0))) {
322 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
326 bytestream2_init(&gb
, avpkt
->data
, avpkt
->size
);
327 while (bytestream2_get_bytes_left(&gb
) > 0) {
328 bytestream2_skip(&gb
, 4);
329 chunk_size
= bytestream2_get_le32(&gb
);
330 chunk_type
= bytestream2_get_le32(&gb
);
333 if (chunk_type
== 1) {
334 pal_elems
= FFMIN(chunk_size
/ 3, 256);
335 for (i
= 0; i
< pal_elems
; i
++) {
336 s
->pal
[i
] = bytestream2_get_be24(&gb
) << 2;
337 s
->pal
[i
] |= (s
->pal
[i
] >> 6) & 0x333;
339 frame
->palette_has_changed
= 1;
340 } else if (chunk_type
<= 9) {
341 if (decoder
[chunk_type
- 2](&gb
, s
->frame_buf
, avctx
->width
, avctx
->height
)) {
342 av_log(avctx
, AV_LOG_ERROR
, "Error decoding %s chunk\n",
343 chunk_name
[chunk_type
- 2]);
344 return AVERROR_INVALIDDATA
;
347 av_log(avctx
, AV_LOG_WARNING
, "Ignoring unknown chunk type %d\n",
354 dst
= frame
->data
[0];
355 for (i
= 0; i
< avctx
->height
; i
++) {
356 memcpy(dst
, buf
, avctx
->width
);
357 dst
+= frame
->linesize
[0];
360 memcpy(frame
->data
[1], s
->pal
, sizeof(s
->pal
));
367 static av_cold
int dfa_decode_end(AVCodecContext
*avctx
)
369 DfaContext
*s
= avctx
->priv_data
;
371 av_freep(&s
->frame_buf
);
376 AVCodec ff_dfa_decoder
= {
378 .type
= AVMEDIA_TYPE_VIDEO
,
379 .id
= AV_CODEC_ID_DFA
,
380 .priv_data_size
= sizeof(DfaContext
),
381 .init
= dfa_decode_init
,
382 .close
= dfa_decode_end
,
383 .decode
= dfa_decode_frame
,
384 .capabilities
= CODEC_CAP_DR1
,
385 .long_name
= NULL_IF_CONFIG_SMALL("Chronomaster DFA"),