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 "bytestream.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/mem.h"
30 typedef struct DfaContext {
35 static av_cold int dfa_decode_init(AVCodecContext *avctx)
37 DfaContext *s = avctx->priv_data;
40 avctx->pix_fmt = AV_PIX_FMT_PAL8;
42 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
45 s->frame_buf = av_mallocz(avctx->width * avctx->height);
47 return AVERROR(ENOMEM);
52 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
54 const int size = width * height;
56 if (bytestream2_get_buffer(gb, frame, size) != size)
57 return AVERROR_INVALIDDATA;
61 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
63 const uint8_t *frame_start = frame;
64 const uint8_t *frame_end = frame + width * height;
65 int mask = 0x10000, bitbuf = 0;
66 int v, count, segments;
69 segments = bytestream2_get_le32(gb);
70 offset = bytestream2_get_le32(gb);
71 if (frame_end - frame <= offset)
72 return AVERROR_INVALIDDATA;
75 if (bytestream2_get_bytes_left(gb) < 2)
76 return AVERROR_INVALIDDATA;
77 if (mask == 0x10000) {
78 bitbuf = bytestream2_get_le16u(gb);
81 if (frame_end - frame < 2)
82 return AVERROR_INVALIDDATA;
84 v = bytestream2_get_le16(gb);
85 offset = (v & 0x1FFF) << 1;
86 count = ((v >> 13) + 2) << 1;
87 if (frame - frame_start < offset || frame_end - frame < count)
88 return AVERROR_INVALIDDATA;
89 av_memcpy_backptr(frame, offset, count);
92 *frame++ = bytestream2_get_byte(gb);
93 *frame++ = bytestream2_get_byte(gb);
101 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
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 = bytestream2_get_le16(gb);
110 if (bytestream2_get_bytes_left(gb) < 2)
111 return AVERROR_INVALIDDATA;
112 if (mask == 0x10000) {
113 bitbuf = bytestream2_get_le16u(gb);
116 if (frame_end - frame < 2)
117 return AVERROR_INVALIDDATA;
119 v = bytestream2_get_le16(gb);
120 offset = (v & 0x1FFF) << 1;
121 count = ((v >> 13) + 2) << 1;
122 if (frame - frame_start < offset || frame_end - frame < count)
123 return AVERROR_INVALIDDATA;
124 av_memcpy_backptr(frame, offset, count);
126 } else if (bitbuf & (mask << 1)) {
127 frame += bytestream2_get_le16(gb);
129 *frame++ = bytestream2_get_byte(gb);
130 *frame++ = bytestream2_get_byte(gb);
138 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
140 const uint8_t *frame_start = frame;
141 const uint8_t *frame_end = frame + width * height;
142 int mask = 0x10000, bitbuf = 0;
143 int i, v, offset, count, segments;
145 segments = bytestream2_get_le16(gb);
147 if (bytestream2_get_bytes_left(gb) < 2)
148 return AVERROR_INVALIDDATA;
149 if (mask == 0x10000) {
150 bitbuf = bytestream2_get_le16u(gb);
155 v = bytestream2_get_le16(gb);
156 offset = (v & 0x1FFF) << 2;
157 count = ((v >> 13) + 2) << 1;
158 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
159 return AVERROR_INVALIDDATA;
160 for (i = 0; i < count; i++) {
161 frame[0] = frame[1] =
162 frame[width] = frame[width + 1] = frame[-offset];
166 } else if (bitbuf & (mask << 1)) {
167 v = bytestream2_get_le16(gb)*2;
168 if (frame - frame_end < v)
169 return AVERROR_INVALIDDATA;
172 if (frame_end - frame < width + 3)
173 return AVERROR_INVALIDDATA;
174 frame[0] = frame[1] =
175 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
177 frame[0] = frame[1] =
178 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
187 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
190 int count, lines, segments;
192 count = bytestream2_get_le16(gb);
194 return AVERROR_INVALIDDATA;
195 frame += width * count;
196 lines = bytestream2_get_le16(gb);
197 if (count + lines > height)
198 return AVERROR_INVALIDDATA;
201 if (bytestream2_get_bytes_left(gb) < 1)
202 return AVERROR_INVALIDDATA;
205 segments = bytestream2_get_byteu(gb);
207 if (frame - line_ptr <= bytestream2_peek_byte(gb))
208 return AVERROR_INVALIDDATA;
209 line_ptr += bytestream2_get_byte(gb);
210 count = (int8_t)bytestream2_get_byte(gb);
212 if (frame - line_ptr < count)
213 return AVERROR_INVALIDDATA;
214 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
215 return AVERROR_INVALIDDATA;
218 if (frame - line_ptr < count)
219 return AVERROR_INVALIDDATA;
220 memset(line_ptr, bytestream2_get_byte(gb), count);
229 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
231 const uint8_t *frame_end = frame + width * height;
233 int count, i, v, lines, segments;
236 lines = bytestream2_get_le16(gb);
238 return AVERROR_INVALIDDATA;
241 if (bytestream2_get_bytes_left(gb) < 2)
242 return AVERROR_INVALIDDATA;
243 segments = bytestream2_get_le16u(gb);
244 while ((segments & 0xC000) == 0xC000) {
245 unsigned skip_lines = -(int16_t)segments;
246 unsigned delta = -((int16_t)segments * width);
247 if (frame_end - frame <= delta || y + lines + skip_lines > height)
248 return AVERROR_INVALIDDATA;
251 segments = bytestream2_get_le16(gb);
253 if (segments & 0x8000) {
254 frame[width - 1] = segments & 0xFF;
255 segments = bytestream2_get_le16(gb);
258 if (frame_end - frame < width)
259 return AVERROR_INVALIDDATA;
263 if (frame - line_ptr <= bytestream2_peek_byte(gb))
264 return AVERROR_INVALIDDATA;
265 line_ptr += bytestream2_get_byte(gb);
266 count = (int8_t)bytestream2_get_byte(gb);
268 if (frame - line_ptr < count * 2)
269 return AVERROR_INVALIDDATA;
270 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
271 return AVERROR_INVALIDDATA;
272 line_ptr += count * 2;
275 if (frame - line_ptr < count * 2)
276 return AVERROR_INVALIDDATA;
277 v = bytestream2_get_le16(gb);
278 for (i = 0; i < count; i++)
279 bytestream_put_le16(&line_ptr, v);
287 static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
289 const uint8_t *frame_end = frame + width * height;
290 int segments = bytestream2_get_le32(gb);
294 if (bytestream2_get_bytes_left(gb) < 2)
295 return AVERROR_INVALIDDATA;
296 copy = bytestream2_get_byteu(gb) * 2;
297 skip = bytestream2_get_byteu(gb) * 2;
298 if (frame_end - frame < copy + skip ||
299 bytestream2_get_bytes_left(gb) < copy)
300 return AVERROR_INVALIDDATA;
302 bytestream2_get_buffer(gb, frame, copy);
309 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
311 memset(frame, 0, width * height);
316 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
318 static const chunk_decoder decoder[8] = {
319 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
320 decode_tdlt, decode_dsw1, decode_blck, decode_dds1,
323 static const char* chunk_name[8] = {
324 "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
327 static int dfa_decode_frame(AVCodecContext *avctx,
328 void *data, int *got_frame,
331 AVFrame *frame = data;
332 DfaContext *s = avctx->priv_data;
334 const uint8_t *buf = avpkt->data;
335 uint32_t chunk_type, chunk_size;
340 if ((ret = ff_get_buffer(avctx, frame, 0))) {
341 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
345 bytestream2_init(&gb, avpkt->data, avpkt->size);
346 while (bytestream2_get_bytes_left(&gb) > 0) {
347 bytestream2_skip(&gb, 4);
348 chunk_size = bytestream2_get_le32(&gb);
349 chunk_type = bytestream2_get_le32(&gb);
352 if (chunk_type == 1) {
353 pal_elems = FFMIN(chunk_size / 3, 256);
354 for (i = 0; i < pal_elems; i++) {
355 s->pal[i] = bytestream2_get_be24(&gb) << 2;
356 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
358 frame->palette_has_changed = 1;
359 } else if (chunk_type <= 9) {
360 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
361 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
362 chunk_name[chunk_type - 2]);
363 return AVERROR_INVALIDDATA;
366 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
373 dst = frame->data[0];
374 for (i = 0; i < avctx->height; i++) {
375 memcpy(dst, buf, avctx->width);
376 dst += frame->linesize[0];
379 memcpy(frame->data[1], s->pal, sizeof(s->pal));
386 static av_cold int dfa_decode_end(AVCodecContext *avctx)
388 DfaContext *s = avctx->priv_data;
390 av_freep(&s->frame_buf);
395 AVCodec ff_dfa_decoder = {
397 .type = AVMEDIA_TYPE_VIDEO,
398 .id = AV_CODEC_ID_DFA,
399 .priv_data_size = sizeof(DfaContext),
400 .init = dfa_decode_init,
401 .close = dfa_decode_end,
402 .decode = dfa_decode_frame,
403 .capabilities = CODEC_CAP_DR1,
404 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),