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