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 "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, offset, count, segments;
67 segments = bytestream_get_le32(&src);
68 frame += bytestream_get_le32(&src);
69 if (frame < frame_start || frame > frame_end)
72 if (mask == 0x10000) {
75 bitbuf = bytestream_get_le16(&src);
78 if (src_end - src < 2 || frame_end - frame < 2)
81 v = bytestream_get_le16(&src);
82 offset = (v & 0x1FFF) << 1;
83 count = ((v >> 13) + 2) << 1;
84 if (frame - frame_start < offset || frame_end - frame < count)
86 av_memcpy_backptr(frame, offset, count);
98 static int decode_dsw1(uint8_t *frame, int width, int height,
99 const uint8_t *src, const uint8_t *src_end)
101 const uint8_t *frame_start = frame;
102 const uint8_t *frame_end = frame + width * height;
103 int mask = 0x10000, bitbuf = 0;
104 int v, offset, count, segments;
106 segments = bytestream_get_le16(&src);
108 if (mask == 0x10000) {
111 bitbuf = bytestream_get_le16(&src);
114 if (src_end - src < 2 || frame_end - frame < 2)
117 v = bytestream_get_le16(&src);
118 offset = (v & 0x1FFF) << 1;
119 count = ((v >> 13) + 2) << 1;
120 if (frame - frame_start < offset || frame_end - frame < count)
122 // can't use av_memcpy_backptr() since it can overwrite following pixels
123 for (v = 0; v < count; v++)
124 frame[v] = frame[v - offset];
126 } else if (bitbuf & (mask << 1)) {
127 frame += bytestream_get_le16(&src);
138 static int decode_dds1(uint8_t *frame, int width, int height,
139 const uint8_t *src, const uint8_t *src_end)
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 = bytestream_get_le16(&src);
148 if (mask == 0x10000) {
151 bitbuf = bytestream_get_le16(&src);
154 if (src_end - src < 2 || frame_end - frame < 2)
157 v = bytestream_get_le16(&src);
158 offset = (v & 0x1FFF) << 2;
159 count = ((v >> 13) + 2) << 1;
160 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
162 for (i = 0; i < count; i++) {
163 frame[0] = frame[1] =
164 frame[width] = frame[width + 1] = frame[-offset];
168 } else if (bitbuf & (mask << 1)) {
169 frame += bytestream_get_le16(&src) * 2;
171 frame[0] = frame[1] =
172 frame[width] = frame[width + 1] = *src++;
174 frame[0] = frame[1] =
175 frame[width] = frame[width + 1] = *src++;
184 static int decode_bdlt(uint8_t *frame, int width, int height,
185 const uint8_t *src, const uint8_t *src_end)
187 const uint8_t *frame_end = frame + width * height;
189 int count, lines, segments;
191 count = bytestream_get_le16(&src);
192 if (count >= height || width * count < 0)
194 frame += width * count;
195 lines = bytestream_get_le16(&src);
196 if (frame + lines * width > frame_end || src >= src_end)
204 if (src_end - src < 3)
207 if (line_ptr >= frame)
209 count = (int8_t)*src++;
211 if (line_ptr + count > frame || src_end - src < count)
213 bytestream_get_buffer(&src, line_ptr, count);
216 if (line_ptr + count > frame || src >= src_end)
218 memset(line_ptr, *src++, count);
227 static int decode_wdlt(uint8_t *frame, int width, int height,
228 const uint8_t *src, const uint8_t *src_end)
230 const uint8_t *frame_end = frame + width * height;
232 int count, i, v, lines, segments;
234 lines = bytestream_get_le16(&src);
235 if (frame + lines * width > frame_end || src >= src_end)
239 segments = bytestream_get_le16(&src);
240 while ((segments & 0xC000) == 0xC000) {
241 frame -= (int16_t)segments * width;
242 if (frame >= frame_end)
244 segments = bytestream_get_le16(&src);
246 if (segments & 0x8000) {
247 frame[width - 1] = segments & 0xFF;
248 segments = bytestream_get_le16(&src);
253 if (src_end - src < 2)
256 if (line_ptr >= frame)
258 count = (int8_t)*src++;
260 if (line_ptr + count*2 > frame || src_end - src < count*2)
262 bytestream_get_buffer(&src, line_ptr, count*2);
263 line_ptr += count * 2;
266 if (line_ptr + count*2 > frame || src_end - src < 2)
268 v = bytestream_get_le16(&src);
269 for (i = 0; i < count; i++)
270 bytestream_put_le16(&line_ptr, v);
278 static int decode_unk6(uint8_t *frame, int width, int height,
279 const uint8_t *src, const uint8_t *src_end)
284 static int decode_blck(uint8_t *frame, int width, int height,
285 const uint8_t *src, const uint8_t *src_end)
287 memset(frame, 0, width * height);
292 typedef int (*chunk_decoder)(uint8_t *frame, int width, int height,
293 const uint8_t *src, const uint8_t *src_end);
295 static const chunk_decoder decoder[8] = {
296 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
297 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
300 static const char* chunk_name[8] = {
301 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
304 static int dfa_decode_frame(AVCodecContext *avctx,
305 void *data, int *data_size,
308 DfaContext *s = avctx->priv_data;
309 const uint8_t *buf = avpkt->data;
310 const uint8_t *buf_end = avpkt->data + avpkt->size;
311 const uint8_t *tmp_buf;
312 uint32_t chunk_type, chunk_size;
318 avctx->release_buffer(avctx, &s->pic);
320 if ((ret = avctx->get_buffer(avctx, &s->pic))) {
321 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
325 while (buf < buf_end) {
326 chunk_size = AV_RL32(buf + 4);
327 chunk_type = AV_RL32(buf + 8);
329 if (buf_end - buf < chunk_size) {
330 av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size);
335 if (chunk_type == 1) {
336 pal_elems = FFMIN(chunk_size / 3, 256);
338 for (i = 0; i < pal_elems; i++) {
339 s->pal[i] = bytestream_get_be24(&tmp_buf) << 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](s->frame_buf, avctx->width, avctx->height,
345 buf, buf + chunk_size)) {
346 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
347 chunk_name[chunk_type - 2]);
351 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
358 dst = s->pic.data[0];
359 for (i = 0; i < avctx->height; i++) {
360 memcpy(dst, buf, avctx->width);
361 dst += s->pic.linesize[0];
364 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
366 *data_size = sizeof(AVFrame);
367 *(AVFrame*)data = s->pic;
372 static av_cold int dfa_decode_end(AVCodecContext *avctx)
374 DfaContext *s = avctx->priv_data;
377 avctx->release_buffer(avctx, &s->pic);
379 av_freep(&s->frame_buf);
384 AVCodec ff_dfa_decoder = {
394 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),