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"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/lzo.h" // for av_memcpy_backptr
29 typedef struct DfaContext {
36 static av_cold int dfa_decode_init(AVCodecContext *avctx)
38 DfaContext *s = avctx->priv_data;
41 avctx->pix_fmt = AV_PIX_FMT_PAL8;
43 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
46 s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING);
48 return AVERROR(ENOMEM);
53 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
55 const int size = width * height;
57 if (bytestream2_get_buffer(gb, frame, size) != size)
58 return AVERROR_INVALIDDATA;
62 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
64 const uint8_t *frame_start = frame;
65 const uint8_t *frame_end = frame + width * height;
66 int mask = 0x10000, bitbuf = 0;
67 int v, count, segments;
70 segments = bytestream2_get_le32(gb);
71 offset = bytestream2_get_le32(gb);
72 if (frame_end - frame <= offset)
73 return AVERROR_INVALIDDATA;
76 if (bytestream2_get_bytes_left(gb) < 2)
77 return AVERROR_INVALIDDATA;
78 if (mask == 0x10000) {
79 bitbuf = bytestream2_get_le16u(gb);
82 if (frame_end - frame < 2)
83 return AVERROR_INVALIDDATA;
85 v = bytestream2_get_le16(gb);
86 offset = (v & 0x1FFF) << 1;
87 count = ((v >> 13) + 2) << 1;
88 if (frame - frame_start < offset || frame_end - frame < count)
89 return AVERROR_INVALIDDATA;
90 av_memcpy_backptr(frame, offset, count);
93 *frame++ = bytestream2_get_byte(gb);
94 *frame++ = bytestream2_get_byte(gb);
102 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
104 const uint8_t *frame_start = frame;
105 const uint8_t *frame_end = frame + width * height;
106 int mask = 0x10000, bitbuf = 0;
107 int v, offset, count, segments;
109 segments = bytestream2_get_le16(gb);
111 if (bytestream2_get_bytes_left(gb) < 2)
112 return AVERROR_INVALIDDATA;
113 if (mask == 0x10000) {
114 bitbuf = bytestream2_get_le16u(gb);
117 if (frame_end - frame < 2)
118 return AVERROR_INVALIDDATA;
120 v = bytestream2_get_le16(gb);
121 offset = (v & 0x1FFF) << 1;
122 count = ((v >> 13) + 2) << 1;
123 if (frame - frame_start < offset || frame_end - frame < count)
124 return AVERROR_INVALIDDATA;
125 // can't use av_memcpy_backptr() since it can overwrite following pixels
126 for (v = 0; v < count; v++)
127 frame[v] = frame[v - offset];
129 } else if (bitbuf & (mask << 1)) {
130 frame += bytestream2_get_le16(gb);
132 *frame++ = bytestream2_get_byte(gb);
133 *frame++ = bytestream2_get_byte(gb);
141 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
143 const uint8_t *frame_start = frame;
144 const uint8_t *frame_end = frame + width * height;
145 int mask = 0x10000, bitbuf = 0;
146 int i, v, offset, count, segments;
148 segments = bytestream2_get_le16(gb);
150 if (bytestream2_get_bytes_left(gb) < 2)
151 return AVERROR_INVALIDDATA;
152 if (mask == 0x10000) {
153 bitbuf = bytestream2_get_le16u(gb);
158 v = bytestream2_get_le16(gb);
159 offset = (v & 0x1FFF) << 2;
160 count = ((v >> 13) + 2) << 1;
161 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
162 return AVERROR_INVALIDDATA;
163 for (i = 0; i < count; i++) {
164 frame[0] = frame[1] =
165 frame[width] = frame[width + 1] = frame[-offset];
169 } else if (bitbuf & (mask << 1)) {
170 v = bytestream2_get_le16(gb)*2;
171 if (frame - frame_end < v)
172 return AVERROR_INVALIDDATA;
175 if (frame_end - frame < width + 3)
176 return AVERROR_INVALIDDATA;
177 frame[0] = frame[1] =
178 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
180 frame[0] = frame[1] =
181 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
190 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
193 int count, lines, segments;
195 count = bytestream2_get_le16(gb);
197 return AVERROR_INVALIDDATA;
198 frame += width * count;
199 lines = bytestream2_get_le16(gb);
200 if (count + lines > height)
201 return AVERROR_INVALIDDATA;
204 if (bytestream2_get_bytes_left(gb) < 1)
205 return AVERROR_INVALIDDATA;
208 segments = bytestream2_get_byteu(gb);
210 if (frame - line_ptr <= bytestream2_peek_byte(gb))
211 return AVERROR_INVALIDDATA;
212 line_ptr += bytestream2_get_byte(gb);
213 count = (int8_t)bytestream2_get_byte(gb);
215 if (frame - line_ptr < count)
216 return AVERROR_INVALIDDATA;
217 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
218 return AVERROR_INVALIDDATA;
221 if (frame - line_ptr < count)
222 return AVERROR_INVALIDDATA;
223 memset(line_ptr, bytestream2_get_byte(gb), count);
232 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
234 const uint8_t *frame_end = frame + width * height;
236 int count, i, v, lines, segments;
239 lines = bytestream2_get_le16(gb);
241 return AVERROR_INVALIDDATA;
244 if (bytestream2_get_bytes_left(gb) < 2)
245 return AVERROR_INVALIDDATA;
246 segments = bytestream2_get_le16u(gb);
247 while ((segments & 0xC000) == 0xC000) {
248 unsigned skip_lines = -(int16_t)segments;
249 unsigned delta = -((int16_t)segments * width);
250 if (frame_end - frame <= delta || y + lines + skip_lines > height)
251 return AVERROR_INVALIDDATA;
254 segments = bytestream2_get_le16(gb);
256 if (segments & 0x8000) {
257 frame[width - 1] = segments & 0xFF;
258 segments = bytestream2_get_le16(gb);
264 if (frame - line_ptr <= bytestream2_peek_byte(gb))
265 return AVERROR_INVALIDDATA;
266 line_ptr += bytestream2_get_byte(gb);
267 count = (int8_t)bytestream2_get_byte(gb);
269 if (frame - line_ptr < count * 2)
270 return AVERROR_INVALIDDATA;
271 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
272 return AVERROR_INVALIDDATA;
273 line_ptr += count * 2;
276 if (frame - line_ptr < count * 2)
277 return AVERROR_INVALIDDATA;
278 v = bytestream2_get_le16(gb);
279 for (i = 0; i < count; i++)
280 bytestream_put_le16(&line_ptr, v);
288 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
290 return AVERROR_PATCHWELCOME;
293 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
295 memset(frame, 0, width * height);
300 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
302 static const chunk_decoder decoder[8] = {
303 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
304 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
307 static const char* chunk_name[8] = {
308 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
311 static int dfa_decode_frame(AVCodecContext *avctx,
312 void *data, int *data_size,
315 DfaContext *s = avctx->priv_data;
317 const uint8_t *buf = avpkt->data;
318 uint32_t chunk_type, chunk_size;
324 avctx->release_buffer(avctx, &s->pic);
326 if ((ret = avctx->get_buffer(avctx, &s->pic))) {
327 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
331 bytestream2_init(&gb, avpkt->data, avpkt->size);
332 while (bytestream2_get_bytes_left(&gb) > 0) {
333 bytestream2_skip(&gb, 4);
334 chunk_size = bytestream2_get_le32(&gb);
335 chunk_type = bytestream2_get_le32(&gb);
338 if (chunk_type == 1) {
339 pal_elems = FFMIN(chunk_size / 3, 256);
340 for (i = 0; i < pal_elems; i++) {
341 s->pal[i] = bytestream2_get_be24(&gb) << 2;
342 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
344 s->pic.palette_has_changed = 1;
345 } else if (chunk_type <= 9) {
346 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
347 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
348 chunk_name[chunk_type - 2]);
349 return AVERROR_INVALIDDATA;
352 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
359 dst = s->pic.data[0];
360 for (i = 0; i < avctx->height; i++) {
361 memcpy(dst, buf, avctx->width);
362 dst += s->pic.linesize[0];
365 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
367 *data_size = sizeof(AVFrame);
368 *(AVFrame*)data = s->pic;
373 static av_cold int dfa_decode_end(AVCodecContext *avctx)
375 DfaContext *s = avctx->priv_data;
378 avctx->release_buffer(avctx, &s->pic);
380 av_freep(&s->frame_buf);
385 AVCodec ff_dfa_decoder = {
387 .type = AVMEDIA_TYPE_VIDEO,
388 .id = AV_CODEC_ID_DFA,
389 .priv_data_size = sizeof(DfaContext),
390 .init = dfa_decode_init,
391 .close = dfa_decode_end,
392 .decode = dfa_decode_frame,
393 .capabilities = CODEC_CAP_DR1,
394 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),