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