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