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 FFmpeg.
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.
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.
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
24 #include "libavutil/intreadwrite.h"
25 #include "bytestream.h"
26 #include "libavutil/lzo.h" // for av_memcpy_backptr
28 typedef struct DfaContext {
35 static av_cold int dfa_decode_init(AVCodecContext *avctx)
37 DfaContext *s = avctx->priv_data;
39 avctx->pix_fmt = PIX_FMT_PAL8;
41 s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING);
43 return AVERROR(ENOMEM);
48 static int decode_copy(uint8_t *frame, int width, int height,
49 const uint8_t *src, const uint8_t *src_end)
51 const int size = width * height;
53 if (src_end - src < size)
55 bytestream_get_buffer(&src, frame, size);
59 static int decode_tsw1(uint8_t *frame, int width, int height,
60 const uint8_t *src, const uint8_t *src_end)
62 const uint8_t *frame_start = frame;
63 const uint8_t *frame_end = frame + width * height;
64 int mask = 0x10000, bitbuf = 0;
65 int v, count, segments;
68 segments = bytestream_get_le32(&src);
69 offset = bytestream_get_le32(&src);
70 if (frame_end - frame <= offset)
74 if (mask == 0x10000) {
77 bitbuf = bytestream_get_le16(&src);
80 if (src_end - src < 2 || frame_end - frame < 2)
83 v = bytestream_get_le16(&src);
84 offset = (v & 0x1FFF) << 1;
85 count = ((v >> 13) + 2) << 1;
86 if (frame - frame_start < offset || frame_end - frame < count)
88 av_memcpy_backptr(frame, offset, count);
100 static int decode_dsw1(uint8_t *frame, int width, int height,
101 const uint8_t *src, const uint8_t *src_end)
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 = bytestream_get_le16(&src);
110 if (mask == 0x10000) {
113 bitbuf = bytestream_get_le16(&src);
116 if (src_end - src < 2 || frame_end - frame < 2)
119 v = bytestream_get_le16(&src);
120 offset = (v & 0x1FFF) << 1;
121 count = ((v >> 13) + 2) << 1;
122 if (frame - frame_start < offset || frame_end - frame < count)
124 // can't use av_memcpy_backptr() since it can overwrite following pixels
125 for (v = 0; v < count; v++)
126 frame[v] = frame[v - offset];
128 } else if (bitbuf & (mask << 1)) {
129 frame += bytestream_get_le16(&src);
140 static int decode_dds1(uint8_t *frame, int width, int height,
141 const uint8_t *src, const uint8_t *src_end)
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 = bytestream_get_le16(&src);
150 if (mask == 0x10000) {
153 bitbuf = bytestream_get_le16(&src);
156 if (src_end - src < 2 || frame_end - frame < 2)
159 v = bytestream_get_le16(&src);
160 offset = (v & 0x1FFF) << 2;
161 count = ((v >> 13) + 2) << 1;
162 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
164 for (i = 0; i < count; i++) {
165 frame[0] = frame[1] =
166 frame[width] = frame[width + 1] = frame[-offset];
170 } else if (bitbuf & (mask << 1)) {
171 frame += bytestream_get_le16(&src) * 2;
173 frame[0] = frame[1] =
174 frame[width] = frame[width + 1] = *src++;
176 frame[0] = frame[1] =
177 frame[width] = frame[width + 1] = *src++;
186 static int decode_bdlt(uint8_t *frame, int width, int height,
187 const uint8_t *src, const uint8_t *src_end)
189 const uint8_t *frame_end = frame + width * height;
191 int count, lines, segments;
193 count = bytestream_get_le16(&src);
196 frame += width * count;
197 lines = bytestream_get_le16(&src);
198 if (count + lines > height || src >= src_end)
206 if (src_end - src < 3)
208 if (frame - line_ptr <= *src)
211 count = (int8_t)*src++;
213 if (frame - line_ptr < count || src_end - src < count)
215 bytestream_get_buffer(&src, line_ptr, count);
218 if (frame - line_ptr < count || src >= src_end)
220 memset(line_ptr, *src++, count);
229 static int decode_wdlt(uint8_t *frame, int width, int height,
230 const uint8_t *src, const uint8_t *src_end)
232 const uint8_t *frame_end = frame + width * height;
234 int count, i, v, lines, segments;
236 lines = bytestream_get_le16(&src);
237 if (lines > height || src >= src_end)
241 segments = bytestream_get_le16(&src);
242 while ((segments & 0xC000) == 0xC000) {
243 unsigned delta = -((int16_t)segments * width);
244 if (frame_end - frame <= delta)
247 segments = bytestream_get_le16(&src);
249 if (segments & 0x8000) {
250 frame[width - 1] = segments & 0xFF;
251 segments = bytestream_get_le16(&src);
256 if (src_end - src < 2)
258 if (frame - line_ptr <= *src)
261 count = (int8_t)*src++;
263 if (frame - line_ptr < count*2 || src_end - src < count*2)
265 bytestream_get_buffer(&src, line_ptr, count*2);
266 line_ptr += count * 2;
269 if (frame - line_ptr < count*2 || src_end - src < 2)
271 v = bytestream_get_le16(&src);
272 for (i = 0; i < count; i++)
273 bytestream_put_le16(&line_ptr, v);
281 static int decode_unk6(uint8_t *frame, int width, int height,
282 const uint8_t *src, const uint8_t *src_end)
287 static int decode_blck(uint8_t *frame, int width, int height,
288 const uint8_t *src, const uint8_t *src_end)
290 memset(frame, 0, width * height);
295 typedef int (*chunk_decoder)(uint8_t *frame, int width, int height,
296 const uint8_t *src, const uint8_t *src_end);
298 static const chunk_decoder decoder[8] = {
299 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
300 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
303 static const char* chunk_name[8] = {
304 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
307 static int dfa_decode_frame(AVCodecContext *avctx,
308 void *data, int *data_size,
311 DfaContext *s = avctx->priv_data;
312 const uint8_t *buf = avpkt->data;
313 const uint8_t *buf_end = avpkt->data + avpkt->size;
314 const uint8_t *tmp_buf;
315 uint32_t chunk_type, chunk_size;
321 avctx->release_buffer(avctx, &s->pic);
323 if ((ret = avctx->get_buffer(avctx, &s->pic))) {
324 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
328 while (buf < buf_end) {
329 chunk_size = AV_RL32(buf + 4);
330 chunk_type = AV_RL32(buf + 8);
332 if (buf_end - buf < chunk_size) {
333 av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size);
338 if (chunk_type == 1) {
339 pal_elems = FFMIN(chunk_size / 3, 256);
341 for (i = 0; i < pal_elems; i++) {
342 s->pal[i] = bytestream_get_be24(&tmp_buf) << 2;
343 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
345 s->pic.palette_has_changed = 1;
346 } else if (chunk_type <= 9) {
347 if (decoder[chunk_type - 2](s->frame_buf, avctx->width, avctx->height,
348 buf, buf + chunk_size)) {
349 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
350 chunk_name[chunk_type - 2]);
354 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
361 dst = s->pic.data[0];
362 for (i = 0; i < avctx->height; i++) {
363 memcpy(dst, buf, avctx->width);
364 dst += s->pic.linesize[0];
367 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
369 *data_size = sizeof(AVFrame);
370 *(AVFrame*)data = s->pic;
375 static av_cold int dfa_decode_end(AVCodecContext *avctx)
377 DfaContext *s = avctx->priv_data;
380 avctx->release_buffer(avctx, &s->pic);
382 av_freep(&s->frame_buf);
387 AVCodec ff_dfa_decoder = {
397 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),