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
26 #include "bytestream.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/mem.h"
32 typedef struct DfaContext {
37 static av_cold int dfa_decode_init(AVCodecContext *avctx)
39 DfaContext *s = avctx->priv_data;
42 avctx->pix_fmt = AV_PIX_FMT_PAL8;
44 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
47 s->frame_buf = av_mallocz(avctx->width * avctx->height);
49 return AVERROR(ENOMEM);
54 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
56 const int size = width * height;
58 if (bytestream2_get_buffer(gb, frame, size) != size)
59 return AVERROR_INVALIDDATA;
63 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
65 const uint8_t *frame_start = frame;
66 const uint8_t *frame_end = frame + width * height;
67 int mask = 0x10000, bitbuf = 0;
68 int v, count, segments;
71 segments = bytestream2_get_le32(gb);
72 offset = bytestream2_get_le32(gb);
73 if (frame_end - frame <= offset)
74 return AVERROR_INVALIDDATA;
77 if (bytestream2_get_bytes_left(gb) < 2)
78 return AVERROR_INVALIDDATA;
79 if (mask == 0x10000) {
80 bitbuf = bytestream2_get_le16u(gb);
83 if (frame_end - frame < 2)
84 return AVERROR_INVALIDDATA;
86 v = bytestream2_get_le16(gb);
87 offset = (v & 0x1FFF) << 1;
88 count = ((v >> 13) + 2) << 1;
89 if (frame - frame_start < offset || frame_end - frame < count)
90 return AVERROR_INVALIDDATA;
91 av_memcpy_backptr(frame, offset, count);
94 *frame++ = bytestream2_get_byte(gb);
95 *frame++ = bytestream2_get_byte(gb);
103 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
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 = bytestream2_get_le16(gb);
112 if (bytestream2_get_bytes_left(gb) < 2)
113 return AVERROR_INVALIDDATA;
114 if (mask == 0x10000) {
115 bitbuf = bytestream2_get_le16u(gb);
118 if (frame_end - frame < 2)
119 return AVERROR_INVALIDDATA;
121 v = bytestream2_get_le16(gb);
122 offset = (v & 0x1FFF) << 1;
123 count = ((v >> 13) + 2) << 1;
124 if (frame - frame_start < offset || frame_end - frame < count)
125 return AVERROR_INVALIDDATA;
126 av_memcpy_backptr(frame, offset, count);
128 } else if (bitbuf & (mask << 1)) {
129 frame += bytestream2_get_le16(gb);
131 *frame++ = bytestream2_get_byte(gb);
132 *frame++ = bytestream2_get_byte(gb);
140 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
142 const uint8_t *frame_start = frame;
143 const uint8_t *frame_end = frame + width * height;
144 int mask = 0x10000, bitbuf = 0;
145 int i, v, offset, count, segments;
147 segments = bytestream2_get_le16(gb);
149 if (bytestream2_get_bytes_left(gb) < 2)
150 return AVERROR_INVALIDDATA;
151 if (mask == 0x10000) {
152 bitbuf = bytestream2_get_le16u(gb);
157 v = bytestream2_get_le16(gb);
158 offset = (v & 0x1FFF) << 2;
159 count = ((v >> 13) + 2) << 1;
160 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
161 return AVERROR_INVALIDDATA;
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 v = bytestream2_get_le16(gb)*2;
170 if (frame - frame_end < v)
171 return AVERROR_INVALIDDATA;
174 if (frame_end - frame < width + 3)
175 return AVERROR_INVALIDDATA;
176 frame[0] = frame[1] =
177 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
179 frame[0] = frame[1] =
180 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
189 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
192 int count, lines, segments;
194 count = bytestream2_get_le16(gb);
196 return AVERROR_INVALIDDATA;
197 frame += width * count;
198 lines = bytestream2_get_le16(gb);
199 if (count + lines > height)
200 return AVERROR_INVALIDDATA;
203 if (bytestream2_get_bytes_left(gb) < 1)
204 return AVERROR_INVALIDDATA;
207 segments = bytestream2_get_byteu(gb);
209 if (frame - line_ptr <= bytestream2_peek_byte(gb))
210 return AVERROR_INVALIDDATA;
211 line_ptr += bytestream2_get_byte(gb);
212 count = (int8_t)bytestream2_get_byte(gb);
214 if (frame - line_ptr < count)
215 return AVERROR_INVALIDDATA;
216 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
217 return AVERROR_INVALIDDATA;
220 if (frame - line_ptr < count)
221 return AVERROR_INVALIDDATA;
222 memset(line_ptr, bytestream2_get_byte(gb), count);
231 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
233 const uint8_t *frame_end = frame + width * height;
235 int count, i, v, lines, segments;
238 lines = bytestream2_get_le16(gb);
240 return AVERROR_INVALIDDATA;
243 if (bytestream2_get_bytes_left(gb) < 2)
244 return AVERROR_INVALIDDATA;
245 segments = bytestream2_get_le16u(gb);
246 while ((segments & 0xC000) == 0xC000) {
247 unsigned skip_lines = -(int16_t)segments;
248 unsigned delta = -((int16_t)segments * width);
249 if (frame_end - frame <= delta || y + lines + skip_lines > height)
250 return AVERROR_INVALIDDATA;
253 segments = bytestream2_get_le16(gb);
255 if (segments & 0x8000) {
256 frame[width - 1] = segments & 0xFF;
257 segments = bytestream2_get_le16(gb);
260 if (frame_end - frame < width)
261 return AVERROR_INVALIDDATA;
265 if (frame - line_ptr <= bytestream2_peek_byte(gb))
266 return AVERROR_INVALIDDATA;
267 line_ptr += bytestream2_get_byte(gb);
268 count = (int8_t)bytestream2_get_byte(gb);
270 if (frame - line_ptr < count * 2)
271 return AVERROR_INVALIDDATA;
272 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
273 return AVERROR_INVALIDDATA;
274 line_ptr += count * 2;
277 if (frame - line_ptr < count * 2)
278 return AVERROR_INVALIDDATA;
279 v = bytestream2_get_le16(gb);
280 for (i = 0; i < count; i++)
281 bytestream_put_le16(&line_ptr, v);
289 static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
291 const uint8_t *frame_end = frame + width * height;
292 int segments = bytestream2_get_le32(gb);
296 if (bytestream2_get_bytes_left(gb) < 2)
297 return AVERROR_INVALIDDATA;
298 copy = bytestream2_get_byteu(gb) * 2;
299 skip = bytestream2_get_byteu(gb) * 2;
300 if (frame_end - frame < copy + skip ||
301 bytestream2_get_bytes_left(gb) < copy)
302 return AVERROR_INVALIDDATA;
304 bytestream2_get_buffer(gb, frame, copy);
311 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
313 memset(frame, 0, width * height);
318 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
320 static const chunk_decoder decoder[8] = {
321 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
322 decode_tdlt, decode_dsw1, decode_blck, decode_dds1,
325 static const char* chunk_name[8] = {
326 "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
329 static int dfa_decode_frame(AVCodecContext *avctx,
330 void *data, int *got_frame,
333 AVFrame *frame = data;
334 DfaContext *s = avctx->priv_data;
336 const uint8_t *buf = avpkt->data;
337 uint32_t chunk_type, chunk_size;
342 if ((ret = ff_get_buffer(avctx, frame, 0))) {
343 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
347 bytestream2_init(&gb, avpkt->data, avpkt->size);
348 while (bytestream2_get_bytes_left(&gb) > 0) {
349 bytestream2_skip(&gb, 4);
350 chunk_size = bytestream2_get_le32(&gb);
351 chunk_type = bytestream2_get_le32(&gb);
354 if (chunk_type == 1) {
355 pal_elems = FFMIN(chunk_size / 3, 256);
356 for (i = 0; i < pal_elems; i++) {
357 s->pal[i] = bytestream2_get_be24(&gb) << 2;
358 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
360 frame->palette_has_changed = 1;
361 } else if (chunk_type <= 9) {
362 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
363 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
364 chunk_name[chunk_type - 2]);
365 return AVERROR_INVALIDDATA;
368 av_log(avctx, AV_LOG_WARNING,
369 "Ignoring unknown chunk type %"PRIu32"\n",
376 dst = frame->data[0];
377 for (i = 0; i < avctx->height; i++) {
378 memcpy(dst, buf, avctx->width);
379 dst += frame->linesize[0];
382 memcpy(frame->data[1], s->pal, sizeof(s->pal));
389 static av_cold int dfa_decode_end(AVCodecContext *avctx)
391 DfaContext *s = avctx->priv_data;
393 av_freep(&s->frame_buf);
398 AVCodec ff_dfa_decoder = {
400 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
401 .type = AVMEDIA_TYPE_VIDEO,
402 .id = AV_CODEC_ID_DFA,
403 .priv_data_size = sizeof(DfaContext),
404 .init = dfa_decode_init,
405 .close = dfa_decode_end,
406 .decode = dfa_decode_frame,
407 .capabilities = AV_CODEC_CAP_DR1,