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 (segments == 0 && offset == frame_end - frame)
71 return 0; // skip frame
72 if (frame_end - frame <= offset)
76 if (mask == 0x10000) {
79 bitbuf = bytestream_get_le16(&src);
82 if (src_end - src < 2 || frame_end - frame < 2)
85 v = bytestream_get_le16(&src);
86 offset = (v & 0x1FFF) << 1;
87 count = ((v >> 13) + 2) << 1;
88 if (frame - frame_start < offset || frame_end - frame < count)
90 av_memcpy_backptr(frame, offset, count);
102 static int decode_dsw1(uint8_t *frame, int width, int height,
103 const uint8_t *src, const uint8_t *src_end)
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 = bytestream_get_le16(&src);
112 if (mask == 0x10000) {
115 bitbuf = bytestream_get_le16(&src);
118 if (src_end - src < 2 || frame_end - frame < 2)
121 v = bytestream_get_le16(&src);
122 offset = (v & 0x1FFF) << 1;
123 count = ((v >> 13) + 2) << 1;
124 if (frame - frame_start < offset || frame_end - frame < count)
126 // can't use av_memcpy_backptr() since it can overwrite following pixels
127 for (v = 0; v < count; v++)
128 frame[v] = frame[v - offset];
130 } else if (bitbuf & (mask << 1)) {
131 frame += bytestream_get_le16(&src);
142 static int decode_dds1(uint8_t *frame, int width, int height,
143 const uint8_t *src, const uint8_t *src_end)
145 const uint8_t *frame_start = frame;
146 const uint8_t *frame_end = frame + width * height;
147 int mask = 0x10000, bitbuf = 0;
148 int i, v, offset, count, segments;
150 segments = bytestream_get_le16(&src);
152 if (mask == 0x10000) {
155 bitbuf = bytestream_get_le16(&src);
158 if (src_end - src < 2 || frame_end - frame < 2)
161 v = bytestream_get_le16(&src);
162 offset = (v & 0x1FFF) << 2;
163 count = ((v >> 13) + 2) << 1;
164 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
166 for (i = 0; i < count; i++) {
167 frame[0] = frame[1] =
168 frame[width] = frame[width + 1] = frame[-offset];
172 } else if (bitbuf & (mask << 1)) {
173 frame += bytestream_get_le16(&src) * 2;
175 frame[0] = frame[1] =
176 frame[width] = frame[width + 1] = *src++;
178 frame[0] = frame[1] =
179 frame[width] = frame[width + 1] = *src++;
188 static int decode_bdlt(uint8_t *frame, int width, int height,
189 const uint8_t *src, const uint8_t *src_end)
192 int count, lines, segments;
194 count = bytestream_get_le16(&src);
197 frame += width * count;
198 lines = bytestream_get_le16(&src);
199 if (count + lines > height || src >= src_end)
207 if (src_end - src < 3)
209 if (frame - line_ptr <= *src)
212 count = (int8_t)*src++;
214 if (frame - line_ptr < count || src_end - src < count)
216 bytestream_get_buffer(&src, line_ptr, count);
219 if (frame - line_ptr < count || src >= src_end)
221 memset(line_ptr, *src++, count);
230 static int decode_wdlt(uint8_t *frame, int width, int height,
231 const uint8_t *src, const uint8_t *src_end)
233 const uint8_t *frame_end = frame + width * height;
235 int count, i, v, lines, segments;
237 lines = bytestream_get_le16(&src);
238 if (lines > height || src >= src_end)
242 segments = bytestream_get_le16(&src);
243 while ((segments & 0xC000) == 0xC000) {
244 unsigned delta = -((int16_t)segments * width);
245 if (frame_end - frame <= delta)
248 segments = bytestream_get_le16(&src);
250 if (segments & 0x8000) {
251 frame[width - 1] = segments & 0xFF;
252 segments = bytestream_get_le16(&src);
257 if (src_end - src < 2)
259 if (frame - line_ptr <= *src)
262 count = (int8_t)*src++;
264 if (frame - line_ptr < count*2 || src_end - src < count*2)
266 bytestream_get_buffer(&src, line_ptr, count*2);
267 line_ptr += count * 2;
270 if (frame - line_ptr < count*2 || src_end - src < 2)
272 v = bytestream_get_le16(&src);
273 for (i = 0; i < count; i++)
274 bytestream_put_le16(&line_ptr, v);
282 static int decode_unk6(uint8_t *frame, int width, int height,
283 const uint8_t *src, const uint8_t *src_end)
288 static int decode_blck(uint8_t *frame, int width, int height,
289 const uint8_t *src, const uint8_t *src_end)
291 memset(frame, 0, width * height);
296 typedef int (*chunk_decoder)(uint8_t *frame, int width, int height,
297 const uint8_t *src, const uint8_t *src_end);
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 *data_size,
312 DfaContext *s = avctx->priv_data;
313 const uint8_t *buf = avpkt->data;
314 const uint8_t *buf_end = avpkt->data + avpkt->size;
315 const uint8_t *tmp_buf;
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 while (buf < buf_end) {
330 chunk_size = AV_RL32(buf + 4);
331 chunk_type = AV_RL32(buf + 8);
333 if (buf_end - buf < chunk_size) {
334 av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size);
339 if (chunk_type == 1) {
340 pal_elems = FFMIN(chunk_size / 3, 256);
342 for (i = 0; i < pal_elems; i++) {
343 s->pal[i] = bytestream_get_be24(&tmp_buf) << 2;
344 s->pal[i] |= 0xFF << 24 | (s->pal[i] >> 6) & 0x30303;
346 s->pic.palette_has_changed = 1;
347 } else if (chunk_type <= 9) {
348 if (decoder[chunk_type - 2](s->frame_buf, avctx->width, avctx->height,
349 buf, buf + chunk_size)) {
350 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
351 chunk_name[chunk_type - 2]);
355 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
362 dst = s->pic.data[0];
363 for (i = 0; i < avctx->height; i++) {
364 memcpy(dst, buf, avctx->width);
365 dst += s->pic.linesize[0];
368 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
370 *data_size = sizeof(AVFrame);
371 *(AVFrame*)data = s->pic;
376 static av_cold int dfa_decode_end(AVCodecContext *avctx)
378 DfaContext *s = avctx->priv_data;
381 avctx->release_buffer(avctx, &s->pic);
383 av_freep(&s->frame_buf);
388 AVCodec ff_dfa_decoder = {
390 .type = AVMEDIA_TYPE_VIDEO,
392 .priv_data_size = sizeof(DfaContext),
393 .init = dfa_decode_init,
394 .close = dfa_decode_end,
395 .decode = dfa_decode_frame,
396 .capabilities = CODEC_CAP_DR1,
397 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),