OSDN Git Service

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