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"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/mem.h"
30 typedef struct DfaContext {
37 static av_cold int dfa_decode_init(AVCodecContext *avctx)
39 DfaContext *s = avctx->priv_data;
41 avctx->pix_fmt = AV_PIX_FMT_PAL8;
43 if (!avctx->width || !avctx->height)
44 return AVERROR_INVALIDDATA;
46 av_assert0(av_image_check_size(avctx->width, avctx->height, 0, avctx) >= 0);
48 s->frame_buf = av_mallocz(avctx->width * avctx->height);
50 return AVERROR(ENOMEM);
55 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
57 const int size = width * height;
59 if (bytestream2_get_buffer(gb, frame, size) != size)
60 return AVERROR_INVALIDDATA;
64 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
66 const uint8_t *frame_start = frame;
67 const uint8_t *frame_end = frame + width * height;
68 int mask = 0x10000, bitbuf = 0;
69 int v, count, segments;
72 segments = bytestream2_get_le32(gb);
73 offset = bytestream2_get_le32(gb);
74 if (segments == 0 && offset == frame_end - frame)
75 return 0; // skip frame
76 if (frame_end - frame <= offset)
77 return AVERROR_INVALIDDATA;
80 if (bytestream2_get_bytes_left(gb) < 2)
81 return AVERROR_INVALIDDATA;
82 if (mask == 0x10000) {
83 bitbuf = bytestream2_get_le16u(gb);
86 if (frame_end - frame < 2)
87 return AVERROR_INVALIDDATA;
89 v = bytestream2_get_le16(gb);
90 offset = (v & 0x1FFF) << 1;
91 count = ((v >> 13) + 2) << 1;
92 if (frame - frame_start < offset || frame_end - frame < count)
93 return AVERROR_INVALIDDATA;
94 av_memcpy_backptr(frame, offset, count);
97 *frame++ = bytestream2_get_byte(gb);
98 *frame++ = bytestream2_get_byte(gb);
106 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
108 const uint8_t *frame_start = frame;
109 const uint8_t *frame_end = frame + width * height;
110 int mask = 0x10000, bitbuf = 0;
111 int v, offset, count, segments;
113 segments = bytestream2_get_le16(gb);
115 if (bytestream2_get_bytes_left(gb) < 2)
116 return AVERROR_INVALIDDATA;
117 if (mask == 0x10000) {
118 bitbuf = bytestream2_get_le16u(gb);
121 if (frame_end - frame < 2)
122 return AVERROR_INVALIDDATA;
124 v = bytestream2_get_le16(gb);
125 offset = (v & 0x1FFF) << 1;
126 count = ((v >> 13) + 2) << 1;
127 if (frame - frame_start < offset || frame_end - frame < count)
128 return AVERROR_INVALIDDATA;
129 av_memcpy_backptr(frame, offset, count);
131 } else if (bitbuf & (mask << 1)) {
132 frame += bytestream2_get_le16(gb);
134 *frame++ = bytestream2_get_byte(gb);
135 *frame++ = bytestream2_get_byte(gb);
143 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
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 = bytestream2_get_le16(gb);
152 if (bytestream2_get_bytes_left(gb) < 2)
153 return AVERROR_INVALIDDATA;
154 if (mask == 0x10000) {
155 bitbuf = bytestream2_get_le16u(gb);
160 v = bytestream2_get_le16(gb);
161 offset = (v & 0x1FFF) << 2;
162 count = ((v >> 13) + 2) << 1;
163 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
164 return AVERROR_INVALIDDATA;
165 for (i = 0; i < count; i++) {
166 frame[0] = frame[1] =
167 frame[width] = frame[width + 1] = frame[-offset];
171 } else if (bitbuf & (mask << 1)) {
172 v = bytestream2_get_le16(gb)*2;
173 if (frame - frame_end < v)
174 return AVERROR_INVALIDDATA;
177 if (frame_end - frame < width + 3)
178 return AVERROR_INVALIDDATA;
179 frame[0] = frame[1] =
180 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
182 frame[0] = frame[1] =
183 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
192 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
195 int count, lines, segments;
197 count = bytestream2_get_le16(gb);
199 return AVERROR_INVALIDDATA;
200 frame += width * count;
201 lines = bytestream2_get_le16(gb);
202 if (count + lines > height)
203 return AVERROR_INVALIDDATA;
206 if (bytestream2_get_bytes_left(gb) < 1)
207 return AVERROR_INVALIDDATA;
210 segments = bytestream2_get_byteu(gb);
212 if (frame - line_ptr <= bytestream2_peek_byte(gb))
213 return AVERROR_INVALIDDATA;
214 line_ptr += bytestream2_get_byte(gb);
215 count = (int8_t)bytestream2_get_byte(gb);
217 if (frame - line_ptr < count)
218 return AVERROR_INVALIDDATA;
219 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
220 return AVERROR_INVALIDDATA;
223 if (frame - line_ptr < count)
224 return AVERROR_INVALIDDATA;
225 memset(line_ptr, bytestream2_get_byte(gb), count);
234 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
236 const uint8_t *frame_end = frame + width * height;
238 int count, i, v, lines, segments;
241 lines = bytestream2_get_le16(gb);
243 return AVERROR_INVALIDDATA;
246 if (bytestream2_get_bytes_left(gb) < 2)
247 return AVERROR_INVALIDDATA;
248 segments = bytestream2_get_le16u(gb);
249 while ((segments & 0xC000) == 0xC000) {
250 unsigned skip_lines = -(int16_t)segments;
251 unsigned delta = -((int16_t)segments * width);
252 if (frame_end - frame <= delta || y + lines + skip_lines > height)
253 return AVERROR_INVALIDDATA;
256 segments = bytestream2_get_le16(gb);
258 if (frame_end <= frame)
260 if (segments & 0x8000) {
261 frame[width - 1] = segments & 0xFF;
262 segments = bytestream2_get_le16(gb);
268 if (frame - line_ptr <= bytestream2_peek_byte(gb))
269 return AVERROR_INVALIDDATA;
270 line_ptr += bytestream2_get_byte(gb);
271 count = (int8_t)bytestream2_get_byte(gb);
273 if (frame - line_ptr < count * 2)
274 return AVERROR_INVALIDDATA;
275 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
276 return AVERROR_INVALIDDATA;
277 line_ptr += count * 2;
280 if (frame - line_ptr < count * 2)
281 return AVERROR_INVALIDDATA;
282 v = bytestream2_get_le16(gb);
283 for (i = 0; i < count; i++)
284 bytestream_put_le16(&line_ptr, v);
292 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
294 return AVERROR_PATCHWELCOME;
297 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
299 memset(frame, 0, width * height);
304 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
306 static const chunk_decoder decoder[8] = {
307 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
308 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
311 static const char* chunk_name[8] = {
312 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
315 static int dfa_decode_frame(AVCodecContext *avctx,
316 void *data, int *data_size,
319 DfaContext *s = avctx->priv_data;
321 const uint8_t *buf = avpkt->data;
322 uint32_t chunk_type, chunk_size;
328 avctx->release_buffer(avctx, &s->pic);
330 if ((ret = avctx->get_buffer(avctx, &s->pic))) {
331 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
335 bytestream2_init(&gb, avpkt->data, avpkt->size);
336 while (bytestream2_get_bytes_left(&gb) > 0) {
337 bytestream2_skip(&gb, 4);
338 chunk_size = bytestream2_get_le32(&gb);
339 chunk_type = bytestream2_get_le32(&gb);
342 if (chunk_type == 1) {
343 pal_elems = FFMIN(chunk_size / 3, 256);
344 for (i = 0; i < pal_elems; i++) {
345 s->pal[i] = bytestream2_get_be24(&gb) << 2;
346 s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
348 s->pic.palette_has_changed = 1;
349 } else if (chunk_type <= 9) {
350 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
351 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
352 chunk_name[chunk_type - 2]);
353 return AVERROR_INVALIDDATA;
356 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
363 dst = s->pic.data[0];
364 for (i = 0; i < avctx->height; i++) {
365 memcpy(dst, buf, avctx->width);
366 dst += s->pic.linesize[0];
369 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
371 *data_size = sizeof(AVFrame);
372 *(AVFrame*)data = s->pic;
377 static av_cold int dfa_decode_end(AVCodecContext *avctx)
379 DfaContext *s = avctx->priv_data;
382 avctx->release_buffer(avctx, &s->pic);
384 av_freep(&s->frame_buf);
389 AVCodec ff_dfa_decoder = {
391 .type = AVMEDIA_TYPE_VIDEO,
392 .id = AV_CODEC_ID_DFA,
393 .priv_data_size = sizeof(DfaContext),
394 .init = dfa_decode_init,
395 .close = dfa_decode_end,
396 .decode = dfa_decode_frame,
397 .capabilities = CODEC_CAP_DR1,
398 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),