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)
190 int count, lines, segments;
192 count = bytestream_get_le16(&src);
195 frame += width * count;
196 lines = bytestream_get_le16(&src);
197 if (count + lines > height || src >= src_end)
205 if (src_end - src < 3)
207 if (frame - line_ptr <= *src)
210 count = (int8_t)*src++;
212 if (frame - line_ptr < count || src_end - src < count)
214 bytestream_get_buffer(&src, line_ptr, count);
217 if (frame - line_ptr < count || src >= src_end)
219 memset(line_ptr, *src++, count);
228 static int decode_wdlt(uint8_t *frame, int width, int height,
229 const uint8_t *src, const uint8_t *src_end)
231 const uint8_t *frame_end = frame + width * height;
233 int count, i, v, lines, segments;
235 lines = bytestream_get_le16(&src);
236 if (lines > height || src >= src_end)
240 segments = bytestream_get_le16(&src);
241 while ((segments & 0xC000) == 0xC000) {
242 unsigned delta = -((int16_t)segments * width);
243 if (frame_end - frame <= delta)
246 segments = bytestream_get_le16(&src);
248 if (segments & 0x8000) {
249 frame[width - 1] = segments & 0xFF;
250 segments = bytestream_get_le16(&src);
255 if (src_end - src < 2)
257 if (frame - line_ptr <= *src)
260 count = (int8_t)*src++;
262 if (frame - line_ptr < count*2 || src_end - src < count*2)
264 bytestream_get_buffer(&src, line_ptr, count*2);
265 line_ptr += count * 2;
268 if (frame - line_ptr < count*2 || src_end - src < 2)
270 v = bytestream_get_le16(&src);
271 for (i = 0; i < count; i++)
272 bytestream_put_le16(&line_ptr, v);
280 static int decode_unk6(uint8_t *frame, int width, int height,
281 const uint8_t *src, const uint8_t *src_end)
286 static int decode_blck(uint8_t *frame, int width, int height,
287 const uint8_t *src, const uint8_t *src_end)
289 memset(frame, 0, width * height);
294 typedef int (*chunk_decoder)(uint8_t *frame, int width, int height,
295 const uint8_t *src, const uint8_t *src_end);
297 static const chunk_decoder decoder[8] = {
298 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
299 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
302 static const char* chunk_name[8] = {
303 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
306 static int dfa_decode_frame(AVCodecContext *avctx,
307 void *data, int *data_size,
310 DfaContext *s = avctx->priv_data;
311 const uint8_t *buf = avpkt->data;
312 const uint8_t *buf_end = avpkt->data + avpkt->size;
313 const uint8_t *tmp_buf;
314 uint32_t chunk_type, chunk_size;
320 avctx->release_buffer(avctx, &s->pic);
322 if ((ret = avctx->get_buffer(avctx, &s->pic))) {
323 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
327 while (buf < buf_end) {
328 chunk_size = AV_RL32(buf + 4);
329 chunk_type = AV_RL32(buf + 8);
331 if (buf_end - buf < chunk_size) {
332 av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size);
337 if (chunk_type == 1) {
338 pal_elems = FFMIN(chunk_size / 3, 256);
340 for (i = 0; i < pal_elems; i++) {
341 s->pal[i] = bytestream_get_be24(&tmp_buf) << 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](s->frame_buf, avctx->width, avctx->height,
347 buf, buf + chunk_size)) {
348 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
349 chunk_name[chunk_type - 2]);
353 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
360 dst = s->pic.data[0];
361 for (i = 0; i < avctx->height; i++) {
362 memcpy(dst, buf, avctx->width);
363 dst += s->pic.linesize[0];
366 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
368 *data_size = sizeof(AVFrame);
369 *(AVFrame*)data = s->pic;
374 static av_cold int dfa_decode_end(AVCodecContext *avctx)
376 DfaContext *s = avctx->priv_data;
379 avctx->release_buffer(avctx, &s->pic);
381 av_freep(&s->frame_buf);
386 AVCodec ff_dfa_decoder = {
396 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),