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
23 #include "libavutil/avassert.h"
25 #include "bytestream.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/mem.h"
31 typedef struct DfaContext {
38 static av_cold int dfa_decode_init(AVCodecContext *avctx)
40 DfaContext *s = avctx->priv_data;
42 avctx->pix_fmt = AV_PIX_FMT_PAL8;
44 if (!avctx->width || !avctx->height)
45 return AVERROR_INVALIDDATA;
47 av_assert0(av_image_check_size(avctx->width, avctx->height, 0, avctx) >= 0);
49 s->frame_buf = av_mallocz(avctx->width * avctx->height);
51 return AVERROR(ENOMEM);
56 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
58 const int size = width * height;
60 if (bytestream2_get_buffer(gb, frame, size) != size)
61 return AVERROR_INVALIDDATA;
65 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
67 const uint8_t *frame_start = frame;
68 const uint8_t *frame_end = frame + width * height;
69 int mask = 0x10000, bitbuf = 0;
70 int v, count, segments;
73 segments = bytestream2_get_le32(gb);
74 offset = bytestream2_get_le32(gb);
75 if (segments == 0 && offset == frame_end - frame)
76 return 0; // skip frame
77 if (frame_end - frame <= offset)
78 return AVERROR_INVALIDDATA;
81 if (bytestream2_get_bytes_left(gb) < 2)
82 return AVERROR_INVALIDDATA;
83 if (mask == 0x10000) {
84 bitbuf = bytestream2_get_le16u(gb);
87 if (frame_end - frame < 2)
88 return AVERROR_INVALIDDATA;
90 v = bytestream2_get_le16(gb);
91 offset = (v & 0x1FFF) << 1;
92 count = ((v >> 13) + 2) << 1;
93 if (frame - frame_start < offset || frame_end - frame < count)
94 return AVERROR_INVALIDDATA;
95 av_memcpy_backptr(frame, offset, count);
98 *frame++ = bytestream2_get_byte(gb);
99 *frame++ = bytestream2_get_byte(gb);
107 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
109 const uint8_t *frame_start = frame;
110 const uint8_t *frame_end = frame + width * height;
111 int mask = 0x10000, bitbuf = 0;
112 int v, offset, count, segments;
114 segments = bytestream2_get_le16(gb);
116 if (bytestream2_get_bytes_left(gb) < 2)
117 return AVERROR_INVALIDDATA;
118 if (mask == 0x10000) {
119 bitbuf = bytestream2_get_le16u(gb);
122 if (frame_end - frame < 2)
123 return AVERROR_INVALIDDATA;
125 v = bytestream2_get_le16(gb);
126 offset = (v & 0x1FFF) << 1;
127 count = ((v >> 13) + 2) << 1;
128 if (frame - frame_start < offset || frame_end - frame < count)
129 return AVERROR_INVALIDDATA;
130 av_memcpy_backptr(frame, offset, count);
132 } else if (bitbuf & (mask << 1)) {
133 frame += bytestream2_get_le16(gb);
135 *frame++ = bytestream2_get_byte(gb);
136 *frame++ = bytestream2_get_byte(gb);
144 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
146 const uint8_t *frame_start = frame;
147 const uint8_t *frame_end = frame + width * height;
148 int mask = 0x10000, bitbuf = 0;
149 int i, v, offset, count, segments;
151 segments = bytestream2_get_le16(gb);
153 if (bytestream2_get_bytes_left(gb) < 2)
154 return AVERROR_INVALIDDATA;
155 if (mask == 0x10000) {
156 bitbuf = bytestream2_get_le16u(gb);
161 v = bytestream2_get_le16(gb);
162 offset = (v & 0x1FFF) << 2;
163 count = ((v >> 13) + 2) << 1;
164 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
165 return AVERROR_INVALIDDATA;
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 v = bytestream2_get_le16(gb)*2;
174 if (frame - frame_end < v)
175 return AVERROR_INVALIDDATA;
178 if (frame_end - frame < width + 3)
179 return AVERROR_INVALIDDATA;
180 frame[0] = frame[1] =
181 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
183 frame[0] = frame[1] =
184 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
193 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
196 int count, lines, segments;
198 count = bytestream2_get_le16(gb);
200 return AVERROR_INVALIDDATA;
201 frame += width * count;
202 lines = bytestream2_get_le16(gb);
203 if (count + lines > height)
204 return AVERROR_INVALIDDATA;
207 if (bytestream2_get_bytes_left(gb) < 1)
208 return AVERROR_INVALIDDATA;
211 segments = bytestream2_get_byteu(gb);
213 if (frame - line_ptr <= bytestream2_peek_byte(gb))
214 return AVERROR_INVALIDDATA;
215 line_ptr += bytestream2_get_byte(gb);
216 count = (int8_t)bytestream2_get_byte(gb);
218 if (frame - line_ptr < count)
219 return AVERROR_INVALIDDATA;
220 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
221 return AVERROR_INVALIDDATA;
224 if (frame - line_ptr < count)
225 return AVERROR_INVALIDDATA;
226 memset(line_ptr, bytestream2_get_byte(gb), count);
235 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
237 const uint8_t *frame_end = frame + width * height;
239 int count, i, v, lines, segments;
242 lines = bytestream2_get_le16(gb);
244 return AVERROR_INVALIDDATA;
247 if (bytestream2_get_bytes_left(gb) < 2)
248 return AVERROR_INVALIDDATA;
249 segments = bytestream2_get_le16u(gb);
250 while ((segments & 0xC000) == 0xC000) {
251 unsigned skip_lines = -(int16_t)segments;
252 unsigned delta = -((int16_t)segments * width);
253 if (frame_end - frame <= delta || y + lines + skip_lines > height)
254 return AVERROR_INVALIDDATA;
257 segments = bytestream2_get_le16(gb);
259 if (frame_end <= frame)
261 if (segments & 0x8000) {
262 frame[width - 1] = segments & 0xFF;
263 segments = bytestream2_get_le16(gb);
269 if (frame - line_ptr <= bytestream2_peek_byte(gb))
270 return AVERROR_INVALIDDATA;
271 line_ptr += bytestream2_get_byte(gb);
272 count = (int8_t)bytestream2_get_byte(gb);
274 if (frame - line_ptr < count * 2)
275 return AVERROR_INVALIDDATA;
276 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
277 return AVERROR_INVALIDDATA;
278 line_ptr += count * 2;
281 if (frame - line_ptr < count * 2)
282 return AVERROR_INVALIDDATA;
283 v = bytestream2_get_le16(gb);
284 for (i = 0; i < count; i++)
285 bytestream_put_le16(&line_ptr, v);
293 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
295 return AVERROR_PATCHWELCOME;
298 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
300 memset(frame, 0, width * height);
305 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
307 static const chunk_decoder decoder[8] = {
308 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
309 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
312 static const char* chunk_name[8] = {
313 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
316 static int dfa_decode_frame(AVCodecContext *avctx,
317 void *data, int *got_frame,
320 DfaContext *s = avctx->priv_data;
322 const uint8_t *buf = avpkt->data;
323 uint32_t chunk_type, chunk_size;
329 avctx->release_buffer(avctx, &s->pic);
331 if ((ret = ff_get_buffer(avctx, &s->pic))) {
332 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
336 bytestream2_init(&gb, avpkt->data, avpkt->size);
337 while (bytestream2_get_bytes_left(&gb) > 0) {
338 bytestream2_skip(&gb, 4);
339 chunk_size = bytestream2_get_le32(&gb);
340 chunk_type = bytestream2_get_le32(&gb);
343 if (chunk_type == 1) {
344 pal_elems = FFMIN(chunk_size / 3, 256);
345 for (i = 0; i < pal_elems; i++) {
346 s->pal[i] = bytestream2_get_be24(&gb) << 2;
347 s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
349 s->pic.palette_has_changed = 1;
350 } else if (chunk_type <= 9) {
351 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
352 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
353 chunk_name[chunk_type - 2]);
354 return AVERROR_INVALIDDATA;
357 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
364 dst = s->pic.data[0];
365 for (i = 0; i < avctx->height; i++) {
366 memcpy(dst, buf, avctx->width);
367 dst += s->pic.linesize[0];
370 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
373 *(AVFrame*)data = s->pic;
378 static av_cold int dfa_decode_end(AVCodecContext *avctx)
380 DfaContext *s = avctx->priv_data;
383 avctx->release_buffer(avctx, &s->pic);
385 av_freep(&s->frame_buf);
390 AVCodec ff_dfa_decoder = {
392 .type = AVMEDIA_TYPE_VIDEO,
393 .id = AV_CODEC_ID_DFA,
394 .priv_data_size = sizeof(DfaContext),
395 .init = dfa_decode_init,
396 .close = dfa_decode_end,
397 .decode = dfa_decode_frame,
398 .capabilities = CODEC_CAP_DR1,
399 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),