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