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
{
37 static av_cold
int dfa_decode_init(AVCodecContext
*avctx
)
39 DfaContext
*s
= avctx
->priv_data
;
42 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
44 if ((ret
= av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
)) < 0)
47 s
->frame_buf
= av_mallocz(avctx
->width
* avctx
->height
);
49 return AVERROR(ENOMEM
);
54 static int decode_copy(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
56 const int size
= width
* height
;
58 if (bytestream2_get_buffer(gb
, frame
, size
) != size
)
59 return AVERROR_INVALIDDATA
;
63 static int decode_tsw1(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
65 const uint8_t *frame_start
= frame
;
66 const uint8_t *frame_end
= frame
+ width
* height
;
67 int mask
= 0x10000, bitbuf
= 0;
68 int v
, count
, segments
;
71 segments
= bytestream2_get_le32(gb
);
72 offset
= bytestream2_get_le32(gb
);
73 if (frame_end
- frame
<= offset
)
74 return AVERROR_INVALIDDATA
;
77 if (bytestream2_get_bytes_left(gb
) < 2)
78 return AVERROR_INVALIDDATA
;
79 if (mask
== 0x10000) {
80 bitbuf
= bytestream2_get_le16u(gb
);
83 if (frame_end
- frame
< 2)
84 return AVERROR_INVALIDDATA
;
86 v
= bytestream2_get_le16(gb
);
87 offset
= (v
& 0x1FFF) << 1;
88 count
= ((v
>> 13) + 2) << 1;
89 if (frame
- frame_start
< offset
|| frame_end
- frame
< count
)
90 return AVERROR_INVALIDDATA
;
91 av_memcpy_backptr(frame
, offset
, count
);
94 *frame
++ = bytestream2_get_byte(gb
);
95 *frame
++ = bytestream2_get_byte(gb
);
103 static int decode_dsw1(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
105 const uint8_t *frame_start
= frame
;
106 const uint8_t *frame_end
= frame
+ width
* height
;
107 int mask
= 0x10000, bitbuf
= 0;
108 int v
, offset
, count
, segments
;
110 segments
= bytestream2_get_le16(gb
);
112 if (bytestream2_get_bytes_left(gb
) < 2)
113 return AVERROR_INVALIDDATA
;
114 if (mask
== 0x10000) {
115 bitbuf
= bytestream2_get_le16u(gb
);
118 if (frame_end
- frame
< 2)
119 return AVERROR_INVALIDDATA
;
121 v
= bytestream2_get_le16(gb
);
122 offset
= (v
& 0x1FFF) << 1;
123 count
= ((v
>> 13) + 2) << 1;
124 if (frame
- frame_start
< offset
|| frame_end
- frame
< count
)
125 return AVERROR_INVALIDDATA
;
126 av_memcpy_backptr(frame
, offset
, count
);
128 } else if (bitbuf
& (mask
<< 1)) {
129 frame
+= bytestream2_get_le16(gb
);
131 *frame
++ = bytestream2_get_byte(gb
);
132 *frame
++ = bytestream2_get_byte(gb
);
140 static int decode_dds1(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
142 const uint8_t *frame_start
= frame
;
143 const uint8_t *frame_end
= frame
+ width
* height
;
144 int mask
= 0x10000, bitbuf
= 0;
145 int i
, v
, offset
, count
, segments
;
147 segments
= bytestream2_get_le16(gb
);
149 if (bytestream2_get_bytes_left(gb
) < 2)
150 return AVERROR_INVALIDDATA
;
151 if (mask
== 0x10000) {
152 bitbuf
= bytestream2_get_le16u(gb
);
157 v
= bytestream2_get_le16(gb
);
158 offset
= (v
& 0x1FFF) << 2;
159 count
= ((v
>> 13) + 2) << 1;
160 if (frame
- frame_start
< offset
|| frame_end
- frame
< count
*2 + width
)
161 return AVERROR_INVALIDDATA
;
162 for (i
= 0; i
< count
; i
++) {
163 frame
[0] = frame
[1] =
164 frame
[width
] = frame
[width
+ 1] = frame
[-offset
];
168 } else if (bitbuf
& (mask
<< 1)) {
169 v
= bytestream2_get_le16(gb
)*2;
170 if (frame
- frame_end
< v
)
171 return AVERROR_INVALIDDATA
;
174 if (frame_end
- frame
< width
+ 3)
175 return AVERROR_INVALIDDATA
;
176 frame
[0] = frame
[1] =
177 frame
[width
] = frame
[width
+ 1] = bytestream2_get_byte(gb
);
179 frame
[0] = frame
[1] =
180 frame
[width
] = frame
[width
+ 1] = bytestream2_get_byte(gb
);
189 static int decode_bdlt(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
192 int count
, lines
, segments
;
194 count
= bytestream2_get_le16(gb
);
196 return AVERROR_INVALIDDATA
;
197 frame
+= width
* count
;
198 lines
= bytestream2_get_le16(gb
);
199 if (count
+ lines
> height
)
200 return AVERROR_INVALIDDATA
;
203 if (bytestream2_get_bytes_left(gb
) < 1)
204 return AVERROR_INVALIDDATA
;
207 segments
= bytestream2_get_byteu(gb
);
209 if (frame
- line_ptr
<= bytestream2_peek_byte(gb
))
210 return AVERROR_INVALIDDATA
;
211 line_ptr
+= bytestream2_get_byte(gb
);
212 count
= (int8_t)bytestream2_get_byte(gb
);
214 if (frame
- line_ptr
< count
)
215 return AVERROR_INVALIDDATA
;
216 if (bytestream2_get_buffer(gb
, line_ptr
, count
) != count
)
217 return AVERROR_INVALIDDATA
;
220 if (frame
- line_ptr
< count
)
221 return AVERROR_INVALIDDATA
;
222 memset(line_ptr
, bytestream2_get_byte(gb
), count
);
231 static int decode_wdlt(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
233 const uint8_t *frame_end
= frame
+ width
* height
;
235 int count
, i
, v
, lines
, segments
;
238 lines
= bytestream2_get_le16(gb
);
240 return AVERROR_INVALIDDATA
;
243 if (bytestream2_get_bytes_left(gb
) < 2)
244 return AVERROR_INVALIDDATA
;
245 segments
= bytestream2_get_le16u(gb
);
246 while ((segments
& 0xC000) == 0xC000) {
247 unsigned skip_lines
= -(int16_t)segments
;
248 unsigned delta
= -((int16_t)segments
* width
);
249 if (frame_end
- frame
<= delta
|| y
+ lines
+ skip_lines
> height
)
250 return AVERROR_INVALIDDATA
;
253 segments
= bytestream2_get_le16(gb
);
255 if (segments
& 0x8000) {
256 frame
[width
- 1] = segments
& 0xFF;
257 segments
= bytestream2_get_le16(gb
);
263 if (frame
- line_ptr
<= bytestream2_peek_byte(gb
))
264 return AVERROR_INVALIDDATA
;
265 line_ptr
+= bytestream2_get_byte(gb
);
266 count
= (int8_t)bytestream2_get_byte(gb
);
268 if (frame
- line_ptr
< count
* 2)
269 return AVERROR_INVALIDDATA
;
270 if (bytestream2_get_buffer(gb
, line_ptr
, count
* 2) != count
* 2)
271 return AVERROR_INVALIDDATA
;
272 line_ptr
+= count
* 2;
275 if (frame
- line_ptr
< count
* 2)
276 return AVERROR_INVALIDDATA
;
277 v
= bytestream2_get_le16(gb
);
278 for (i
= 0; i
< count
; i
++)
279 bytestream_put_le16(&line_ptr
, v
);
287 static int decode_unk6(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
289 return AVERROR_PATCHWELCOME
;
292 static int decode_blck(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
)
294 memset(frame
, 0, width
* height
);
299 typedef int (*chunk_decoder
)(GetByteContext
*gb
, uint8_t *frame
, int width
, int height
);
301 static const chunk_decoder decoder
[8] = {
302 decode_copy
, decode_tsw1
, decode_bdlt
, decode_wdlt
,
303 decode_unk6
, decode_dsw1
, decode_blck
, decode_dds1
,
306 static const char* chunk_name
[8] = {
307 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
310 static int dfa_decode_frame(AVCodecContext
*avctx
,
311 void *data
, int *got_frame
,
314 DfaContext
*s
= avctx
->priv_data
;
316 const uint8_t *buf
= avpkt
->data
;
317 uint32_t chunk_type
, chunk_size
;
323 avctx
->release_buffer(avctx
, &s
->pic
);
325 if ((ret
= ff_get_buffer(avctx
, &s
->pic
))) {
326 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
330 bytestream2_init(&gb
, avpkt
->data
, avpkt
->size
);
331 while (bytestream2_get_bytes_left(&gb
) > 0) {
332 bytestream2_skip(&gb
, 4);
333 chunk_size
= bytestream2_get_le32(&gb
);
334 chunk_type
= bytestream2_get_le32(&gb
);
337 if (chunk_type
== 1) {
338 pal_elems
= FFMIN(chunk_size
/ 3, 256);
339 for (i
= 0; i
< pal_elems
; i
++) {
340 s
->pal
[i
] = bytestream2_get_be24(&gb
) << 2;
341 s
->pal
[i
] |= (s
->pal
[i
] >> 6) & 0x333;
343 s
->pic
.palette_has_changed
= 1;
344 } else if (chunk_type
<= 9) {
345 if (decoder
[chunk_type
- 2](&gb
, s
->frame_buf
, avctx
->width
, avctx
->height
)) {
346 av_log(avctx
, AV_LOG_ERROR
, "Error decoding %s chunk\n",
347 chunk_name
[chunk_type
- 2]);
348 return AVERROR_INVALIDDATA
;
351 av_log(avctx
, AV_LOG_WARNING
, "Ignoring unknown chunk type %d\n",
358 dst
= s
->pic
.data
[0];
359 for (i
= 0; i
< avctx
->height
; i
++) {
360 memcpy(dst
, buf
, avctx
->width
);
361 dst
+= s
->pic
.linesize
[0];
364 memcpy(s
->pic
.data
[1], s
->pal
, sizeof(s
->pal
));
367 *(AVFrame
*)data
= s
->pic
;
372 static av_cold
int dfa_decode_end(AVCodecContext
*avctx
)
374 DfaContext
*s
= avctx
->priv_data
;
377 avctx
->release_buffer(avctx
, &s
->pic
);
379 av_freep(&s
->frame_buf
);
384 AVCodec ff_dfa_decoder
= {
386 .type
= AVMEDIA_TYPE_VIDEO
,
387 .id
= AV_CODEC_ID_DFA
,
388 .priv_data_size
= sizeof(DfaContext
),
389 .init
= dfa_decode_init
,
390 .close
= dfa_decode_end
,
391 .decode
= dfa_decode_frame
,
392 .capabilities
= CODEC_CAP_DR1
,
393 .long_name
= NULL_IF_CONFIG_SMALL("Chronomaster DFA"),