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);
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 av_memcpy_backptr(frame, offset, count);
127 } else if (bitbuf & (mask << 1)) {
128 frame += bytestream2_get_le16(gb);
130 *frame++ = bytestream2_get_byte(gb);
131 *frame++ = bytestream2_get_byte(gb);
139 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
141 const uint8_t *frame_start = frame;
142 const uint8_t *frame_end = frame + width * height;
143 int mask = 0x10000, bitbuf = 0;
144 int i, v, offset, count, segments;
146 segments = bytestream2_get_le16(gb);
148 if (bytestream2_get_bytes_left(gb) < 2)
149 return AVERROR_INVALIDDATA;
150 if (mask == 0x10000) {
151 bitbuf = bytestream2_get_le16u(gb);
156 v = bytestream2_get_le16(gb);
157 offset = (v & 0x1FFF) << 2;
158 count = ((v >> 13) + 2) << 1;
159 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
160 return AVERROR_INVALIDDATA;
161 for (i = 0; i < count; i++) {
162 frame[0] = frame[1] =
163 frame[width] = frame[width + 1] = frame[-offset];
167 } else if (bitbuf & (mask << 1)) {
168 v = bytestream2_get_le16(gb)*2;
169 if (frame - frame_end < v)
170 return AVERROR_INVALIDDATA;
173 if (frame_end - frame < width + 3)
174 return AVERROR_INVALIDDATA;
175 frame[0] = frame[1] =
176 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
178 frame[0] = frame[1] =
179 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
188 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
191 int count, lines, segments;
193 count = bytestream2_get_le16(gb);
195 return AVERROR_INVALIDDATA;
196 frame += width * count;
197 lines = bytestream2_get_le16(gb);
198 if (count + lines > height)
199 return AVERROR_INVALIDDATA;
202 if (bytestream2_get_bytes_left(gb) < 1)
203 return AVERROR_INVALIDDATA;
206 segments = bytestream2_get_byteu(gb);
208 if (frame - line_ptr <= bytestream2_peek_byte(gb))
209 return AVERROR_INVALIDDATA;
210 line_ptr += bytestream2_get_byte(gb);
211 count = (int8_t)bytestream2_get_byte(gb);
213 if (frame - line_ptr < count)
214 return AVERROR_INVALIDDATA;
215 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
216 return AVERROR_INVALIDDATA;
219 if (frame - line_ptr < count)
220 return AVERROR_INVALIDDATA;
221 memset(line_ptr, bytestream2_get_byte(gb), count);
230 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
232 const uint8_t *frame_end = frame + width * height;
234 int count, i, v, lines, segments;
237 lines = bytestream2_get_le16(gb);
239 return AVERROR_INVALIDDATA;
242 if (bytestream2_get_bytes_left(gb) < 2)
243 return AVERROR_INVALIDDATA;
244 segments = bytestream2_get_le16u(gb);
245 while ((segments & 0xC000) == 0xC000) {
246 unsigned skip_lines = -(int16_t)segments;
247 unsigned delta = -((int16_t)segments * width);
248 if (frame_end - frame <= delta || y + lines + skip_lines > height)
249 return AVERROR_INVALIDDATA;
252 segments = bytestream2_get_le16(gb);
254 if (segments & 0x8000) {
255 frame[width - 1] = segments & 0xFF;
256 segments = bytestream2_get_le16(gb);
262 if (frame - line_ptr <= bytestream2_peek_byte(gb))
263 return AVERROR_INVALIDDATA;
264 line_ptr += bytestream2_get_byte(gb);
265 count = (int8_t)bytestream2_get_byte(gb);
267 if (frame - line_ptr < count * 2)
268 return AVERROR_INVALIDDATA;
269 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
270 return AVERROR_INVALIDDATA;
271 line_ptr += count * 2;
274 if (frame - line_ptr < count * 2)
275 return AVERROR_INVALIDDATA;
276 v = bytestream2_get_le16(gb);
277 for (i = 0; i < count; i++)
278 bytestream_put_le16(&line_ptr, v);
286 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
288 return AVERROR_PATCHWELCOME;
291 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
293 memset(frame, 0, width * height);
298 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
300 static const chunk_decoder decoder[8] = {
301 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
302 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
305 static const char* chunk_name[8] = {
306 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
309 static int dfa_decode_frame(AVCodecContext *avctx,
310 void *data, int *data_size,
313 DfaContext *s = avctx->priv_data;
315 const uint8_t *buf = avpkt->data;
316 uint32_t chunk_type, chunk_size;
322 avctx->release_buffer(avctx, &s->pic);
324 if ((ret = avctx->get_buffer(avctx, &s->pic))) {
325 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
329 bytestream2_init(&gb, avpkt->data, avpkt->size);
330 while (bytestream2_get_bytes_left(&gb) > 0) {
331 bytestream2_skip(&gb, 4);
332 chunk_size = bytestream2_get_le32(&gb);
333 chunk_type = bytestream2_get_le32(&gb);
336 if (chunk_type == 1) {
337 pal_elems = FFMIN(chunk_size / 3, 256);
338 for (i = 0; i < pal_elems; i++) {
339 s->pal[i] = bytestream2_get_be24(&gb) << 2;
340 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
342 s->pic.palette_has_changed = 1;
343 } else if (chunk_type <= 9) {
344 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
345 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
346 chunk_name[chunk_type - 2]);
347 return AVERROR_INVALIDDATA;
350 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
357 dst = s->pic.data[0];
358 for (i = 0; i < avctx->height; i++) {
359 memcpy(dst, buf, avctx->width);
360 dst += s->pic.linesize[0];
363 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
365 *data_size = sizeof(AVFrame);
366 *(AVFrame*)data = s->pic;
371 static av_cold int dfa_decode_end(AVCodecContext *avctx)
373 DfaContext *s = avctx->priv_data;
376 avctx->release_buffer(avctx, &s->pic);
378 av_freep(&s->frame_buf);
383 AVCodec ff_dfa_decoder = {
385 .type = AVMEDIA_TYPE_VIDEO,
386 .id = AV_CODEC_ID_DFA,
387 .priv_data_size = sizeof(DfaContext),
388 .init = dfa_decode_init,
389 .close = dfa_decode_end,
390 .decode = dfa_decode_frame,
391 .capabilities = CODEC_CAP_DR1,
392 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),