]> git.sesse.net Git - ffmpeg/blob - libavcodec/dfa.c
ae184d7b5f6ebdec1b36704fba7fabd054ecffa1
[ffmpeg] / libavcodec / dfa.c
1 /*
2  * Chronomaster DFA Video Decoder
3  * Copyright (c) 2011 Konstantin Shishkov
4  * based on work by Vladimir "VAG" Gneushev
5  *
6  * This file is part of Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "avcodec.h"
24 #include "bytestream.h"
25
26 #include "libavutil/imgutils.h"
27 #include "libavutil/lzo.h" // for av_memcpy_backptr
28
29 typedef struct DfaContext {
30     AVFrame pic;
31
32     uint32_t pal[256];
33     uint8_t *frame_buf;
34 } DfaContext;
35
36 static av_cold int dfa_decode_init(AVCodecContext *avctx)
37 {
38     DfaContext *s = avctx->priv_data;
39     int ret;
40
41     avctx->pix_fmt = AV_PIX_FMT_PAL8;
42
43     if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
44         return ret;
45
46     s->frame_buf = av_mallocz(avctx->width * avctx->height);
47     if (!s->frame_buf)
48         return AVERROR(ENOMEM);
49
50     return 0;
51 }
52
53 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
54 {
55     const int size = width * height;
56
57     if (bytestream2_get_buffer(gb, frame, size) != size)
58         return AVERROR_INVALIDDATA;
59     return 0;
60 }
61
62 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
63 {
64     const uint8_t *frame_start = frame;
65     const uint8_t *frame_end   = frame + width * height;
66     int mask = 0x10000, bitbuf = 0;
67     int v, count, segments;
68     unsigned offset;
69
70     segments = bytestream2_get_le32(gb);
71     offset   = bytestream2_get_le32(gb);
72     if (frame_end - frame <= offset)
73         return AVERROR_INVALIDDATA;
74     frame += offset;
75     while (segments--) {
76         if (bytestream2_get_bytes_left(gb) < 2)
77             return AVERROR_INVALIDDATA;
78         if (mask == 0x10000) {
79             bitbuf = bytestream2_get_le16u(gb);
80             mask = 1;
81         }
82         if (frame_end - frame < 2)
83             return AVERROR_INVALIDDATA;
84         if (bitbuf & mask) {
85             v = bytestream2_get_le16(gb);
86             offset = (v & 0x1FFF) << 1;
87             count = ((v >> 13) + 2) << 1;
88             if (frame - frame_start < offset || frame_end - frame < count)
89                 return AVERROR_INVALIDDATA;
90             av_memcpy_backptr(frame, offset, count);
91             frame += count;
92         } else {
93             *frame++ = bytestream2_get_byte(gb);
94             *frame++ = bytestream2_get_byte(gb);
95         }
96         mask <<= 1;
97     }
98
99     return 0;
100 }
101
102 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
103 {
104     const uint8_t *frame_start = frame;
105     const uint8_t *frame_end   = frame + width * height;
106     int mask = 0x10000, bitbuf = 0;
107     int v, offset, count, segments;
108
109     segments = bytestream2_get_le16(gb);
110     while (segments--) {
111         if (bytestream2_get_bytes_left(gb) < 2)
112             return AVERROR_INVALIDDATA;
113         if (mask == 0x10000) {
114             bitbuf = bytestream2_get_le16u(gb);
115             mask = 1;
116         }
117         if (frame_end - frame < 2)
118             return AVERROR_INVALIDDATA;
119         if (bitbuf & mask) {
120             v = bytestream2_get_le16(gb);
121             offset = (v & 0x1FFF) << 1;
122             count = ((v >> 13) + 2) << 1;
123             if (frame - frame_start < offset || frame_end - frame < count)
124                 return AVERROR_INVALIDDATA;
125             av_memcpy_backptr(frame, offset, count);
126             frame += count;
127         } else if (bitbuf & (mask << 1)) {
128             frame += bytestream2_get_le16(gb);
129         } else {
130             *frame++ = bytestream2_get_byte(gb);
131             *frame++ = bytestream2_get_byte(gb);
132         }
133         mask <<= 2;
134     }
135
136     return 0;
137 }
138
139 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
140 {
141     const uint8_t *frame_start = frame;
142     const uint8_t *frame_end   = frame + width * height;
143     int mask = 0x10000, bitbuf = 0;
144     int i, v, offset, count, segments;
145
146     segments = bytestream2_get_le16(gb);
147     while (segments--) {
148         if (bytestream2_get_bytes_left(gb) < 2)
149             return AVERROR_INVALIDDATA;
150         if (mask == 0x10000) {
151             bitbuf = bytestream2_get_le16u(gb);
152             mask = 1;
153         }
154
155         if (bitbuf & mask) {
156             v = bytestream2_get_le16(gb);
157             offset = (v & 0x1FFF) << 2;
158             count = ((v >> 13) + 2) << 1;
159             if (frame - frame_start < offset || frame_end - frame < count*2 + width)
160                 return AVERROR_INVALIDDATA;
161             for (i = 0; i < count; i++) {
162                 frame[0] = frame[1] =
163                 frame[width] = frame[width + 1] = frame[-offset];
164
165                 frame += 2;
166             }
167         } else if (bitbuf & (mask << 1)) {
168             v = bytestream2_get_le16(gb)*2;
169             if (frame - frame_end < v)
170                 return AVERROR_INVALIDDATA;
171             frame += v;
172         } else {
173             if (frame_end - frame < width + 3)
174                 return AVERROR_INVALIDDATA;
175             frame[0] = frame[1] =
176             frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
177             frame += 2;
178             frame[0] = frame[1] =
179             frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
180             frame += 2;
181         }
182         mask <<= 2;
183     }
184
185     return 0;
186 }
187
188 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
189 {
190     uint8_t *line_ptr;
191     int count, lines, segments;
192
193     count = bytestream2_get_le16(gb);
194     if (count >= height)
195         return AVERROR_INVALIDDATA;
196     frame += width * count;
197     lines = bytestream2_get_le16(gb);
198     if (count + lines > height)
199         return AVERROR_INVALIDDATA;
200
201     while (lines--) {
202         if (bytestream2_get_bytes_left(gb) < 1)
203             return AVERROR_INVALIDDATA;
204         line_ptr = frame;
205         frame += width;
206         segments = bytestream2_get_byteu(gb);
207         while (segments--) {
208             if (frame - line_ptr <= bytestream2_peek_byte(gb))
209                 return AVERROR_INVALIDDATA;
210             line_ptr += bytestream2_get_byte(gb);
211             count = (int8_t)bytestream2_get_byte(gb);
212             if (count >= 0) {
213                 if (frame - line_ptr < count)
214                     return AVERROR_INVALIDDATA;
215                 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
216                     return AVERROR_INVALIDDATA;
217             } else {
218                 count = -count;
219                 if (frame - line_ptr < count)
220                     return AVERROR_INVALIDDATA;
221                 memset(line_ptr, bytestream2_get_byte(gb), count);
222             }
223             line_ptr += count;
224         }
225     }
226
227     return 0;
228 }
229
230 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
231 {
232     const uint8_t *frame_end   = frame + width * height;
233     uint8_t *line_ptr;
234     int count, i, v, lines, segments;
235     int y = 0;
236
237     lines = bytestream2_get_le16(gb);
238     if (lines > height)
239         return AVERROR_INVALIDDATA;
240
241     while (lines--) {
242         if (bytestream2_get_bytes_left(gb) < 2)
243             return AVERROR_INVALIDDATA;
244         segments = bytestream2_get_le16u(gb);
245         while ((segments & 0xC000) == 0xC000) {
246             unsigned skip_lines = -(int16_t)segments;
247             unsigned delta = -((int16_t)segments * width);
248             if (frame_end - frame <= delta || y + lines + skip_lines > height)
249                 return AVERROR_INVALIDDATA;
250             frame    += delta;
251             y        += skip_lines;
252             segments = bytestream2_get_le16(gb);
253         }
254         if (segments & 0x8000) {
255             frame[width - 1] = segments & 0xFF;
256             segments = bytestream2_get_le16(gb);
257         }
258         line_ptr = frame;
259         frame += width;
260         y++;
261         while (segments--) {
262             if (frame - line_ptr <= bytestream2_peek_byte(gb))
263                 return AVERROR_INVALIDDATA;
264             line_ptr += bytestream2_get_byte(gb);
265             count = (int8_t)bytestream2_get_byte(gb);
266             if (count >= 0) {
267                 if (frame - line_ptr < count * 2)
268                     return AVERROR_INVALIDDATA;
269                 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
270                     return AVERROR_INVALIDDATA;
271                 line_ptr += count * 2;
272             } else {
273                 count = -count;
274                 if (frame - line_ptr < count * 2)
275                     return AVERROR_INVALIDDATA;
276                 v = bytestream2_get_le16(gb);
277                 for (i = 0; i < count; i++)
278                     bytestream_put_le16(&line_ptr, v);
279             }
280         }
281     }
282
283     return 0;
284 }
285
286 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
287 {
288     return AVERROR_PATCHWELCOME;
289 }
290
291 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
292 {
293     memset(frame, 0, width * height);
294     return 0;
295 }
296
297
298 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
299
300 static const chunk_decoder decoder[8] = {
301     decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
302     decode_unk6, decode_dsw1, decode_blck, decode_dds1,
303 };
304
305 static const char* chunk_name[8] = {
306     "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
307 };
308
309 static int dfa_decode_frame(AVCodecContext *avctx,
310                             void *data, int *data_size,
311                             AVPacket *avpkt)
312 {
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;
320
321     if (s->pic.data[0])
322         avctx->release_buffer(avctx, &s->pic);
323
324     if ((ret = avctx->get_buffer(avctx, &s->pic))) {
325         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
326         return ret;
327     }
328
329     bytestream2_init(&gb, avpkt->data, avpkt->size);
330     while (bytestream2_get_bytes_left(&gb) > 0) {
331         bytestream2_skip(&gb, 4);
332         chunk_size = bytestream2_get_le32(&gb);
333         chunk_type = bytestream2_get_le32(&gb);
334         if (!chunk_type)
335             break;
336         if (chunk_type == 1) {
337             pal_elems = FFMIN(chunk_size / 3, 256);
338             for (i = 0; i < pal_elems; i++) {
339                 s->pal[i] = bytestream2_get_be24(&gb) << 2;
340                 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
341             }
342             s->pic.palette_has_changed = 1;
343         } else if (chunk_type <= 9) {
344             if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
345                 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
346                        chunk_name[chunk_type - 2]);
347                 return AVERROR_INVALIDDATA;
348             }
349         } else {
350             av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
351                    chunk_type);
352         }
353         buf += chunk_size;
354     }
355
356     buf = s->frame_buf;
357     dst = s->pic.data[0];
358     for (i = 0; i < avctx->height; i++) {
359         memcpy(dst, buf, avctx->width);
360         dst += s->pic.linesize[0];
361         buf += avctx->width;
362     }
363     memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
364
365     *data_size = sizeof(AVFrame);
366     *(AVFrame*)data = s->pic;
367
368     return avpkt->size;
369 }
370
371 static av_cold int dfa_decode_end(AVCodecContext *avctx)
372 {
373     DfaContext *s = avctx->priv_data;
374
375     if (s->pic.data[0])
376         avctx->release_buffer(avctx, &s->pic);
377
378     av_freep(&s->frame_buf);
379
380     return 0;
381 }
382
383 AVCodec ff_dfa_decoder = {
384     .name           = "dfa",
385     .type           = AVMEDIA_TYPE_VIDEO,
386     .id             = AV_CODEC_ID_DFA,
387     .priv_data_size = sizeof(DfaContext),
388     .init           = dfa_decode_init,
389     .close          = dfa_decode_end,
390     .decode         = dfa_decode_frame,
391     .capabilities   = CODEC_CAP_DR1,
392     .long_name      = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
393 };