Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / dfa.c
blobbbe4ce2888b3a4fe02680e7aa74edd28d0499628
1 /*
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
23 #include "avcodec.h"
24 #include "bytestream.h"
25 #include "internal.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/mem.h"
30 typedef struct DfaContext {
31 uint32_t pal[256];
32 uint8_t *frame_buf;
33 } DfaContext;
35 static av_cold int dfa_decode_init(AVCodecContext *avctx)
37 DfaContext *s = avctx->priv_data;
38 int ret;
40 avctx->pix_fmt = AV_PIX_FMT_PAL8;
42 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
43 return ret;
45 s->frame_buf = av_mallocz(avctx->width * avctx->height);
46 if (!s->frame_buf)
47 return AVERROR(ENOMEM);
49 return 0;
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;
58 return 0;
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;
67 unsigned offset;
69 segments = bytestream2_get_le32(gb);
70 offset = bytestream2_get_le32(gb);
71 if (frame_end - frame <= offset)
72 return AVERROR_INVALIDDATA;
73 frame += offset;
74 while (segments--) {
75 if (bytestream2_get_bytes_left(gb) < 2)
76 return AVERROR_INVALIDDATA;
77 if (mask == 0x10000) {
78 bitbuf = bytestream2_get_le16u(gb);
79 mask = 1;
81 if (frame_end - frame < 2)
82 return AVERROR_INVALIDDATA;
83 if (bitbuf & mask) {
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);
90 frame += count;
91 } else {
92 *frame++ = bytestream2_get_byte(gb);
93 *frame++ = bytestream2_get_byte(gb);
95 mask <<= 1;
98 return 0;
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);
109 while (segments--) {
110 if (bytestream2_get_bytes_left(gb) < 2)
111 return AVERROR_INVALIDDATA;
112 if (mask == 0x10000) {
113 bitbuf = bytestream2_get_le16u(gb);
114 mask = 1;
116 if (frame_end - frame < 2)
117 return AVERROR_INVALIDDATA;
118 if (bitbuf & mask) {
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);
125 frame += count;
126 } else if (bitbuf & (mask << 1)) {
127 frame += bytestream2_get_le16(gb);
128 } else {
129 *frame++ = bytestream2_get_byte(gb);
130 *frame++ = bytestream2_get_byte(gb);
132 mask <<= 2;
135 return 0;
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);
146 while (segments--) {
147 if (bytestream2_get_bytes_left(gb) < 2)
148 return AVERROR_INVALIDDATA;
149 if (mask == 0x10000) {
150 bitbuf = bytestream2_get_le16u(gb);
151 mask = 1;
154 if (bitbuf & mask) {
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];
164 frame += 2;
166 } else if (bitbuf & (mask << 1)) {
167 v = bytestream2_get_le16(gb)*2;
168 if (frame - frame_end < v)
169 return AVERROR_INVALIDDATA;
170 frame += v;
171 } else {
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);
176 frame += 2;
177 frame[0] = frame[1] =
178 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
179 frame += 2;
181 mask <<= 2;
184 return 0;
187 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
189 uint8_t *line_ptr;
190 int count, lines, segments;
192 count = bytestream2_get_le16(gb);
193 if (count >= height)
194 return AVERROR_INVALIDDATA;
195 frame += width * count;
196 lines = bytestream2_get_le16(gb);
197 if (count + lines > height)
198 return AVERROR_INVALIDDATA;
200 while (lines--) {
201 if (bytestream2_get_bytes_left(gb) < 1)
202 return AVERROR_INVALIDDATA;
203 line_ptr = frame;
204 frame += width;
205 segments = bytestream2_get_byteu(gb);
206 while (segments--) {
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);
211 if (count >= 0) {
212 if (frame - line_ptr < count)
213 return AVERROR_INVALIDDATA;
214 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
215 return AVERROR_INVALIDDATA;
216 } else {
217 count = -count;
218 if (frame - line_ptr < count)
219 return AVERROR_INVALIDDATA;
220 memset(line_ptr, bytestream2_get_byte(gb), count);
222 line_ptr += count;
226 return 0;
229 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
231 const uint8_t *frame_end = frame + width * height;
232 uint8_t *line_ptr;
233 int count, i, v, lines, segments;
234 int y = 0;
236 lines = bytestream2_get_le16(gb);
237 if (lines > height)
238 return AVERROR_INVALIDDATA;
240 while (lines--) {
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;
249 frame += delta;
250 y += skip_lines;
251 segments = bytestream2_get_le16(gb);
253 if (segments & 0x8000) {
254 frame[width - 1] = segments & 0xFF;
255 segments = bytestream2_get_le16(gb);
257 line_ptr = frame;
258 frame += width;
259 y++;
260 while (segments--) {
261 if (frame - line_ptr <= bytestream2_peek_byte(gb))
262 return AVERROR_INVALIDDATA;
263 line_ptr += bytestream2_get_byte(gb);
264 count = (int8_t)bytestream2_get_byte(gb);
265 if (count >= 0) {
266 if (frame - line_ptr < count * 2)
267 return AVERROR_INVALIDDATA;
268 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
269 return AVERROR_INVALIDDATA;
270 line_ptr += count * 2;
271 } else {
272 count = -count;
273 if (frame - line_ptr < count * 2)
274 return AVERROR_INVALIDDATA;
275 v = bytestream2_get_le16(gb);
276 for (i = 0; i < count; i++)
277 bytestream_put_le16(&line_ptr, v);
282 return 0;
285 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
287 return AVERROR_PATCHWELCOME;
290 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
292 memset(frame, 0, width * height);
293 return 0;
297 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
299 static const chunk_decoder decoder[8] = {
300 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
301 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
304 static const char* chunk_name[8] = {
305 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
308 static int dfa_decode_frame(AVCodecContext *avctx,
309 void *data, int *got_frame,
310 AVPacket *avpkt)
312 AVFrame *frame = data;
313 DfaContext *s = avctx->priv_data;
314 GetByteContext gb;
315 const uint8_t *buf = avpkt->data;
316 uint32_t chunk_type, chunk_size;
317 uint8_t *dst;
318 int ret;
319 int i, pal_elems;
321 if ((ret = ff_get_buffer(avctx, frame, 0))) {
322 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
323 return ret;
326 bytestream2_init(&gb, avpkt->data, avpkt->size);
327 while (bytestream2_get_bytes_left(&gb) > 0) {
328 bytestream2_skip(&gb, 4);
329 chunk_size = bytestream2_get_le32(&gb);
330 chunk_type = bytestream2_get_le32(&gb);
331 if (!chunk_type)
332 break;
333 if (chunk_type == 1) {
334 pal_elems = FFMIN(chunk_size / 3, 256);
335 for (i = 0; i < pal_elems; i++) {
336 s->pal[i] = bytestream2_get_be24(&gb) << 2;
337 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
339 frame->palette_has_changed = 1;
340 } else if (chunk_type <= 9) {
341 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
342 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
343 chunk_name[chunk_type - 2]);
344 return AVERROR_INVALIDDATA;
346 } else {
347 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
348 chunk_type);
350 buf += chunk_size;
353 buf = s->frame_buf;
354 dst = frame->data[0];
355 for (i = 0; i < avctx->height; i++) {
356 memcpy(dst, buf, avctx->width);
357 dst += frame->linesize[0];
358 buf += avctx->width;
360 memcpy(frame->data[1], s->pal, sizeof(s->pal));
362 *got_frame = 1;
364 return avpkt->size;
367 static av_cold int dfa_decode_end(AVCodecContext *avctx)
369 DfaContext *s = avctx->priv_data;
371 av_freep(&s->frame_buf);
373 return 0;
376 AVCodec ff_dfa_decoder = {
377 .name = "dfa",
378 .type = AVMEDIA_TYPE_VIDEO,
379 .id = AV_CODEC_ID_DFA,
380 .priv_data_size = sizeof(DfaContext),
381 .init = dfa_decode_init,
382 .close = dfa_decode_end,
383 .decode = dfa_decode_frame,
384 .capabilities = CODEC_CAP_DR1,
385 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),