]> git.sesse.net Git - ffmpeg/blob - libavcodec/dfa.c
Merge commit 'a65bdceb060628881578afb29df4eb222421381f'
[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/mem.h"
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);
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             av_memcpy_backptr(frame, offset, count);
130             frame += count;
131         } else if (bitbuf & (mask << 1)) {
132             frame += bytestream2_get_le16(gb);
133         } else {
134             *frame++ = bytestream2_get_byte(gb);
135             *frame++ = bytestream2_get_byte(gb);
136         }
137         mask <<= 2;
138     }
139
140     return 0;
141 }
142
143 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
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 = bytestream2_get_le16(gb);
151     while (segments--) {
152         if (bytestream2_get_bytes_left(gb) < 2)
153             return AVERROR_INVALIDDATA;
154         if (mask == 0x10000) {
155             bitbuf = bytestream2_get_le16u(gb);
156             mask = 1;
157         }
158
159         if (bitbuf & mask) {
160             v = bytestream2_get_le16(gb);
161             offset = (v & 0x1FFF) << 2;
162             count = ((v >> 13) + 2) << 1;
163             if (frame - frame_start < offset || frame_end - frame < count*2 + width)
164                 return AVERROR_INVALIDDATA;
165             for (i = 0; i < count; i++) {
166                 frame[0] = frame[1] =
167                 frame[width] = frame[width + 1] = frame[-offset];
168
169                 frame += 2;
170             }
171         } else if (bitbuf & (mask << 1)) {
172             v = bytestream2_get_le16(gb)*2;
173             if (frame - frame_end < v)
174                 return AVERROR_INVALIDDATA;
175             frame += v;
176         } else {
177             if (frame_end - frame < width + 3)
178                 return AVERROR_INVALIDDATA;
179             frame[0] = frame[1] =
180             frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
181             frame += 2;
182             frame[0] = frame[1] =
183             frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
184             frame += 2;
185         }
186         mask <<= 2;
187     }
188
189     return 0;
190 }
191
192 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
193 {
194     uint8_t *line_ptr;
195     int count, lines, segments;
196
197     count = bytestream2_get_le16(gb);
198     if (count >= height)
199         return AVERROR_INVALIDDATA;
200     frame += width * count;
201     lines = bytestream2_get_le16(gb);
202     if (count + lines > height)
203         return AVERROR_INVALIDDATA;
204
205     while (lines--) {
206         if (bytestream2_get_bytes_left(gb) < 1)
207             return AVERROR_INVALIDDATA;
208         line_ptr = frame;
209         frame += width;
210         segments = bytestream2_get_byteu(gb);
211         while (segments--) {
212             if (frame - line_ptr <= bytestream2_peek_byte(gb))
213                 return AVERROR_INVALIDDATA;
214             line_ptr += bytestream2_get_byte(gb);
215             count = (int8_t)bytestream2_get_byte(gb);
216             if (count >= 0) {
217                 if (frame - line_ptr < count)
218                     return AVERROR_INVALIDDATA;
219                 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
220                     return AVERROR_INVALIDDATA;
221             } else {
222                 count = -count;
223                 if (frame - line_ptr < count)
224                     return AVERROR_INVALIDDATA;
225                 memset(line_ptr, bytestream2_get_byte(gb), count);
226             }
227             line_ptr += count;
228         }
229     }
230
231     return 0;
232 }
233
234 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
235 {
236     const uint8_t *frame_end   = frame + width * height;
237     uint8_t *line_ptr;
238     int count, i, v, lines, segments;
239     int y = 0;
240
241     lines = bytestream2_get_le16(gb);
242     if (lines > height)
243         return AVERROR_INVALIDDATA;
244
245     while (lines--) {
246         if (bytestream2_get_bytes_left(gb) < 2)
247             return AVERROR_INVALIDDATA;
248         segments = bytestream2_get_le16u(gb);
249         while ((segments & 0xC000) == 0xC000) {
250             unsigned skip_lines = -(int16_t)segments;
251             unsigned delta = -((int16_t)segments * width);
252             if (frame_end - frame <= delta || y + lines + skip_lines > height)
253                 return AVERROR_INVALIDDATA;
254             frame    += delta;
255             y        += skip_lines;
256             segments = bytestream2_get_le16(gb);
257         }
258         if (frame_end <= frame)
259             return -1;
260         if (segments & 0x8000) {
261             frame[width - 1] = segments & 0xFF;
262             segments = bytestream2_get_le16(gb);
263         }
264         line_ptr = frame;
265         frame += width;
266         y++;
267         while (segments--) {
268             if (frame - line_ptr <= bytestream2_peek_byte(gb))
269                 return AVERROR_INVALIDDATA;
270             line_ptr += bytestream2_get_byte(gb);
271             count = (int8_t)bytestream2_get_byte(gb);
272             if (count >= 0) {
273                 if (frame - line_ptr < count * 2)
274                     return AVERROR_INVALIDDATA;
275                 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
276                     return AVERROR_INVALIDDATA;
277                 line_ptr += count * 2;
278             } else {
279                 count = -count;
280                 if (frame - line_ptr < count * 2)
281                     return AVERROR_INVALIDDATA;
282                 v = bytestream2_get_le16(gb);
283                 for (i = 0; i < count; i++)
284                     bytestream_put_le16(&line_ptr, v);
285             }
286         }
287     }
288
289     return 0;
290 }
291
292 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
293 {
294     return AVERROR_PATCHWELCOME;
295 }
296
297 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
298 {
299     memset(frame, 0, width * height);
300     return 0;
301 }
302
303
304 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
305
306 static const chunk_decoder decoder[8] = {
307     decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
308     decode_unk6, decode_dsw1, decode_blck, decode_dds1,
309 };
310
311 static const char* chunk_name[8] = {
312     "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
313 };
314
315 static int dfa_decode_frame(AVCodecContext *avctx,
316                             void *data, int *data_size,
317                             AVPacket *avpkt)
318 {
319     DfaContext *s = avctx->priv_data;
320     GetByteContext gb;
321     const uint8_t *buf = avpkt->data;
322     uint32_t chunk_type, chunk_size;
323     uint8_t *dst;
324     int ret;
325     int i, pal_elems;
326
327     if (s->pic.data[0])
328         avctx->release_buffer(avctx, &s->pic);
329
330     if ((ret = avctx->get_buffer(avctx, &s->pic))) {
331         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
332         return ret;
333     }
334
335     bytestream2_init(&gb, avpkt->data, avpkt->size);
336     while (bytestream2_get_bytes_left(&gb) > 0) {
337         bytestream2_skip(&gb, 4);
338         chunk_size = bytestream2_get_le32(&gb);
339         chunk_type = bytestream2_get_le32(&gb);
340         if (!chunk_type)
341             break;
342         if (chunk_type == 1) {
343             pal_elems = FFMIN(chunk_size / 3, 256);
344             for (i = 0; i < pal_elems; i++) {
345                 s->pal[i] = bytestream2_get_be24(&gb) << 2;
346                 s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
347             }
348             s->pic.palette_has_changed = 1;
349         } else if (chunk_type <= 9) {
350             if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
351                 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
352                        chunk_name[chunk_type - 2]);
353                 return AVERROR_INVALIDDATA;
354             }
355         } else {
356             av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
357                    chunk_type);
358         }
359         buf += chunk_size;
360     }
361
362     buf = s->frame_buf;
363     dst = s->pic.data[0];
364     for (i = 0; i < avctx->height; i++) {
365         memcpy(dst, buf, avctx->width);
366         dst += s->pic.linesize[0];
367         buf += avctx->width;
368     }
369     memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
370
371     *data_size = sizeof(AVFrame);
372     *(AVFrame*)data = s->pic;
373
374     return avpkt->size;
375 }
376
377 static av_cold int dfa_decode_end(AVCodecContext *avctx)
378 {
379     DfaContext *s = avctx->priv_data;
380
381     if (s->pic.data[0])
382         avctx->release_buffer(avctx, &s->pic);
383
384     av_freep(&s->frame_buf);
385
386     return 0;
387 }
388
389 AVCodec ff_dfa_decoder = {
390     .name           = "dfa",
391     .type           = AVMEDIA_TYPE_VIDEO,
392     .id             = AV_CODEC_ID_DFA,
393     .priv_data_size = sizeof(DfaContext),
394     .init           = dfa_decode_init,
395     .close          = dfa_decode_end,
396     .decode         = dfa_decode_frame,
397     .capabilities   = CODEC_CAP_DR1,
398     .long_name      = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
399 };