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
26 #include "bytestream.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/mem.h"
33 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;
74 segments = bytestream2_get_le32(gb);
75 offset = bytestream2_get_le32(gb);
76 if (segments == 0 && offset == frame_end - frame)
77 return 0; // skip frame
78 if (frame_end - frame <= offset)
79 return AVERROR_INVALIDDATA;
82 if (bytestream2_get_bytes_left(gb) < 2)
83 return AVERROR_INVALIDDATA;
84 if (mask == 0x10000) {
85 bitbuf = bytestream2_get_le16u(gb);
88 if (frame_end - frame < 2)
89 return AVERROR_INVALIDDATA;
91 v = bytestream2_get_le16(gb);
92 offset = (v & 0x1FFF) << 1;
93 count = ((v >> 13) + 2) << 1;
94 if (frame - frame_start < offset || frame_end - frame < count)
95 return AVERROR_INVALIDDATA;
96 av_memcpy_backptr(frame, offset, count);
99 *frame++ = bytestream2_get_byte(gb);
100 *frame++ = bytestream2_get_byte(gb);
108 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
110 const uint8_t *frame_start = frame;
111 const uint8_t *frame_end = frame + width * height;
112 int mask = 0x10000, bitbuf = 0;
113 int v, offset, count, segments;
115 segments = bytestream2_get_le16(gb);
117 if (bytestream2_get_bytes_left(gb) < 2)
118 return AVERROR_INVALIDDATA;
119 if (mask == 0x10000) {
120 bitbuf = bytestream2_get_le16u(gb);
123 if (frame_end - frame < 2)
124 return AVERROR_INVALIDDATA;
126 v = bytestream2_get_le16(gb);
127 offset = (v & 0x1FFF) << 1;
128 count = ((v >> 13) + 2) << 1;
129 if (frame - frame_start < offset || frame_end - frame < count)
130 return AVERROR_INVALIDDATA;
131 av_memcpy_backptr(frame, offset, count);
133 } else if (bitbuf & (mask << 1)) {
134 frame += bytestream2_get_le16(gb);
136 *frame++ = bytestream2_get_byte(gb);
137 *frame++ = bytestream2_get_byte(gb);
145 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
147 const uint8_t *frame_start = frame;
148 const uint8_t *frame_end = frame + width * height;
149 int mask = 0x10000, bitbuf = 0;
150 int i, v, offset, count, segments;
152 if ((width | height) & 1)
153 return AVERROR_INVALIDDATA;
154 segments = bytestream2_get_le16(gb);
156 if (bytestream2_get_bytes_left(gb) < 2)
157 return AVERROR_INVALIDDATA;
158 if (mask == 0x10000) {
159 bitbuf = bytestream2_get_le16u(gb);
164 v = bytestream2_get_le16(gb);
165 offset = (v & 0x1FFF) << 2;
166 count = ((v >> 13) + 2) << 1;
167 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
168 return AVERROR_INVALIDDATA;
169 for (i = 0; i < count; i++) {
170 frame[0] = frame[1] =
171 frame[width] = frame[width + 1] = frame[-offset];
175 } else if (bitbuf & (mask << 1)) {
176 v = bytestream2_get_le16(gb)*2;
177 if (frame - frame_end < v)
178 return AVERROR_INVALIDDATA;
181 if (width < 4 || frame_end - frame < width + 4)
182 return AVERROR_INVALIDDATA;
183 frame[0] = frame[1] =
184 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
186 frame[0] = frame[1] =
187 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
196 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
199 int count, lines, segments;
201 count = bytestream2_get_le16(gb);
203 return AVERROR_INVALIDDATA;
204 frame += width * count;
205 lines = bytestream2_get_le16(gb);
206 if (count + lines > height)
207 return AVERROR_INVALIDDATA;
210 if (bytestream2_get_bytes_left(gb) < 1)
211 return AVERROR_INVALIDDATA;
214 segments = bytestream2_get_byteu(gb);
216 if (frame - line_ptr <= bytestream2_peek_byte(gb))
217 return AVERROR_INVALIDDATA;
218 line_ptr += bytestream2_get_byte(gb);
219 count = (int8_t)bytestream2_get_byte(gb);
221 if (frame - line_ptr < count)
222 return AVERROR_INVALIDDATA;
223 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
224 return AVERROR_INVALIDDATA;
227 if (frame - line_ptr < count)
228 return AVERROR_INVALIDDATA;
229 memset(line_ptr, bytestream2_get_byte(gb), count);
238 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
240 const uint8_t *frame_end = frame + width * height;
242 int count, i, v, lines, segments;
245 lines = bytestream2_get_le16(gb);
247 return AVERROR_INVALIDDATA;
250 if (bytestream2_get_bytes_left(gb) < 2)
251 return AVERROR_INVALIDDATA;
252 segments = bytestream2_get_le16u(gb);
253 while ((segments & 0xC000) == 0xC000) {
254 unsigned skip_lines = -(int16_t)segments;
255 int64_t delta = -((int16_t)segments * (int64_t)width);
256 if (frame_end - frame <= delta || y + lines + skip_lines > height)
257 return AVERROR_INVALIDDATA;
260 segments = bytestream2_get_le16(gb);
263 if (frame_end <= frame)
264 return AVERROR_INVALIDDATA;
265 if (segments & 0x8000) {
266 frame[width - 1] = segments & 0xFF;
267 segments = bytestream2_get_le16(gb);
270 if (frame_end - frame < width)
271 return AVERROR_INVALIDDATA;
275 if (frame - line_ptr <= bytestream2_peek_byte(gb))
276 return AVERROR_INVALIDDATA;
277 line_ptr += bytestream2_get_byte(gb);
278 count = (int8_t)bytestream2_get_byte(gb);
280 if (frame - line_ptr < count * 2)
281 return AVERROR_INVALIDDATA;
282 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
283 return AVERROR_INVALIDDATA;
284 line_ptr += count * 2;
287 if (frame - line_ptr < count * 2)
288 return AVERROR_INVALIDDATA;
289 v = bytestream2_get_le16(gb);
290 for (i = 0; i < count; i++)
291 bytestream_put_le16(&line_ptr, v);
299 static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
301 const uint8_t *frame_end = frame + width * height;
302 uint32_t segments = bytestream2_get_le32(gb);
306 if (bytestream2_get_bytes_left(gb) < 2)
307 return AVERROR_INVALIDDATA;
308 copy = bytestream2_get_byteu(gb) * 2;
309 skip = bytestream2_get_byteu(gb) * 2;
310 if (frame_end - frame < copy + skip ||
311 bytestream2_get_bytes_left(gb) < copy)
312 return AVERROR_INVALIDDATA;
314 bytestream2_get_buffer(gb, frame, copy);
321 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
323 memset(frame, 0, width * height);
328 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
330 static const chunk_decoder decoder[8] = {
331 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
332 decode_tdlt, decode_dsw1, decode_blck, decode_dds1,
335 static const char * const chunk_name[8] = {
336 "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
339 static int dfa_decode_frame(AVCodecContext *avctx,
340 void *data, int *got_frame,
343 AVFrame *frame = data;
344 DfaContext *s = avctx->priv_data;
346 const uint8_t *buf = avpkt->data;
347 uint32_t chunk_type, chunk_size;
351 int version = avctx->extradata_size==2 ? AV_RL16(avctx->extradata) : 0;
353 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
356 bytestream2_init(&gb, avpkt->data, avpkt->size);
357 while (bytestream2_get_bytes_left(&gb) > 0) {
358 bytestream2_skip(&gb, 4);
359 chunk_size = bytestream2_get_le32(&gb);
360 chunk_type = bytestream2_get_le32(&gb);
363 if (chunk_type == 1) {
364 pal_elems = FFMIN(chunk_size / 3, 256);
365 for (i = 0; i < pal_elems; i++) {
366 s->pal[i] = bytestream2_get_be24(&gb) << 2;
367 s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
369 frame->palette_has_changed = 1;
370 } else if (chunk_type <= 9) {
371 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
372 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
373 chunk_name[chunk_type - 2]);
374 return AVERROR_INVALIDDATA;
377 av_log(avctx, AV_LOG_WARNING,
378 "Ignoring unknown chunk type %"PRIu32"\n",
385 dst = frame->data[0];
386 for (i = 0; i < avctx->height; i++) {
387 if(version == 0x100) {
389 for(j = 0; j < avctx->width; j++) {
390 dst[j] = buf[ (i&3)*(avctx->width /4) + (j/4) +
391 ((j&3)*(avctx->height/4) + (i/4))*avctx->width];
394 memcpy(dst, buf, avctx->width);
397 dst += frame->linesize[0];
399 memcpy(frame->data[1], s->pal, sizeof(s->pal));
406 static av_cold int dfa_decode_end(AVCodecContext *avctx)
408 DfaContext *s = avctx->priv_data;
410 av_freep(&s->frame_buf);
415 AVCodec ff_dfa_decoder = {
417 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
418 .type = AVMEDIA_TYPE_VIDEO,
419 .id = AV_CODEC_ID_DFA,
420 .priv_data_size = sizeof(DfaContext),
421 .init = dfa_decode_init,
422 .close = dfa_decode_end,
423 .decode = dfa_decode_frame,
424 .capabilities = AV_CODEC_CAP_DR1,