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
29typedef struct DfaContext {
30    AVFrame pic;
31
32    uint32_t pal[256];
33    uint8_t *frame_buf;
34} DfaContext;
35
36static av_cold int dfa_decode_init(AVCodecContext *avctx)
37{
38    DfaContext *s = avctx->priv_data;
39    int ret;
40
41    avctx->pix_fmt = 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
53static 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
62static 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
102static 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
141static 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
190static 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
232static 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        if (frame_end - frame < width)
262            return AVERROR_INVALIDDATA;
263        frame += width;
264        y++;
265        while (segments--) {
266            if (frame - line_ptr <= bytestream2_peek_byte(gb))
267                return AVERROR_INVALIDDATA;
268            line_ptr += bytestream2_get_byte(gb);
269            count = (int8_t)bytestream2_get_byte(gb);
270            if (count >= 0) {
271                if (frame - line_ptr < count * 2)
272                    return AVERROR_INVALIDDATA;
273                if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
274                    return AVERROR_INVALIDDATA;
275                line_ptr += count * 2;
276            } else {
277                count = -count;
278                if (frame - line_ptr < count * 2)
279                    return AVERROR_INVALIDDATA;
280                v = bytestream2_get_le16(gb);
281                for (i = 0; i < count; i++)
282                    bytestream_put_le16(&line_ptr, v);
283            }
284        }
285    }
286
287    return 0;
288}
289
290static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
291{
292    return AVERROR_PATCHWELCOME;
293}
294
295static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
296{
297    memset(frame, 0, width * height);
298    return 0;
299}
300
301
302typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
303
304static const chunk_decoder decoder[8] = {
305    decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
306    decode_unk6, decode_dsw1, decode_blck, decode_dds1,
307};
308
309static const char* chunk_name[8] = {
310    "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
311};
312
313static int dfa_decode_frame(AVCodecContext *avctx,
314                            void *data, int *data_size,
315                            AVPacket *avpkt)
316{
317    DfaContext *s = avctx->priv_data;
318    GetByteContext gb;
319    const uint8_t *buf = avpkt->data;
320    uint32_t chunk_type, chunk_size;
321    uint8_t *dst;
322    int ret;
323    int i, pal_elems;
324
325    if (s->pic.data[0])
326        avctx->release_buffer(avctx, &s->pic);
327
328    if ((ret = avctx->get_buffer(avctx, &s->pic))) {
329        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
330        return ret;
331    }
332
333    bytestream2_init(&gb, avpkt->data, avpkt->size);
334    while (bytestream2_get_bytes_left(&gb) > 0) {
335        bytestream2_skip(&gb, 4);
336        chunk_size = bytestream2_get_le32(&gb);
337        chunk_type = bytestream2_get_le32(&gb);
338        if (!chunk_type)
339            break;
340        if (chunk_type == 1) {
341            pal_elems = FFMIN(chunk_size / 3, 256);
342            for (i = 0; i < pal_elems; i++) {
343                s->pal[i] = bytestream2_get_be24(&gb) << 2;
344                s->pal[i] |= (s->pal[i] >> 6) & 0x333;
345            }
346            s->pic.palette_has_changed = 1;
347        } else if (chunk_type <= 9) {
348            if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
349                av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
350                       chunk_name[chunk_type - 2]);
351                return AVERROR_INVALIDDATA;
352            }
353        } else {
354            av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
355                   chunk_type);
356        }
357        buf += chunk_size;
358    }
359
360    buf = s->frame_buf;
361    dst = s->pic.data[0];
362    for (i = 0; i < avctx->height; i++) {
363        memcpy(dst, buf, avctx->width);
364        dst += s->pic.linesize[0];
365        buf += avctx->width;
366    }
367    memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
368
369    *data_size = sizeof(AVFrame);
370    *(AVFrame*)data = s->pic;
371
372    return avpkt->size;
373}
374
375static av_cold int dfa_decode_end(AVCodecContext *avctx)
376{
377    DfaContext *s = avctx->priv_data;
378
379    if (s->pic.data[0])
380        avctx->release_buffer(avctx, &s->pic);
381
382    av_freep(&s->frame_buf);
383
384    return 0;
385}
386
387AVCodec ff_dfa_decoder = {
388    .name           = "dfa",
389    .type           = AVMEDIA_TYPE_VIDEO,
390    .id             = CODEC_ID_DFA,
391    .priv_data_size = sizeof(DfaContext),
392    .init           = dfa_decode_init,
393    .close          = dfa_decode_end,
394    .decode         = dfa_decode_frame,
395    .capabilities   = CODEC_CAP_DR1,
396    .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
397};
398