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