1/*
2 * Kega Game Video (KGV1) decoder
3 * Copyright (c) 2010 Daniel Verkamp
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * Kega Game Video decoder
25 */
26
27#include "libavutil/intreadwrite.h"
28#include "avcodec.h"
29
30typedef struct {
31    AVCodecContext *avctx;
32    AVFrame pic;
33    uint16_t *prev, *cur;
34} KgvContext;
35
36static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
37{
38    const uint8_t *buf = avpkt->data;
39    const uint8_t *buf_end = buf + avpkt->size;
40    KgvContext * const c = avctx->priv_data;
41    int offsets[7];
42    uint16_t *out, *prev;
43    int outcnt = 0, maxcnt;
44    int w, h, i;
45
46    if (avpkt->size < 2)
47        return -1;
48
49    w = (buf[0] + 1) * 8;
50    h = (buf[1] + 1) * 8;
51    buf += 2;
52
53    if (avcodec_check_dimensions(avctx, w, h))
54        return -1;
55
56    if (w != avctx->width || h != avctx->height)
57        avcodec_set_dimensions(avctx, w, h);
58
59    maxcnt = w * h;
60
61    out = av_realloc(c->cur, w * h * 2);
62    if (!out)
63        return -1;
64    c->cur = out;
65
66    prev = av_realloc(c->prev, w * h * 2);
67    if (!prev)
68        return -1;
69    c->prev = prev;
70
71    for (i = 0; i < 7; i++)
72        offsets[i] = -1;
73
74    while (outcnt < maxcnt && buf_end - 2 > buf) {
75        int code = AV_RL16(buf);
76        buf += 2;
77
78        if (!(code & 0x8000)) {
79            out[outcnt++] = code; // rgb555 pixel coded directly
80        } else {
81            int count;
82            uint16_t *inp;
83
84            if ((code & 0x6000) == 0x6000) {
85                // copy from previous frame
86                int oidx = (code >> 10) & 7;
87                int start;
88
89                count = (code & 0x3FF) + 3;
90
91                if (offsets[oidx] < 0) {
92                    if (buf_end - 3 < buf)
93                        break;
94                    offsets[oidx] = AV_RL24(buf);
95                    buf += 3;
96                }
97
98                start = (outcnt + offsets[oidx]) % maxcnt;
99
100                if (maxcnt - start < count)
101                    break;
102
103                inp = prev + start;
104            } else {
105                // copy from earlier in this frame
106                int offset = (code & 0x1FFF) + 1;
107
108                if (!(code & 0x6000)) {
109                    count = 2;
110                } else if ((code & 0x6000) == 0x2000) {
111                    count = 3;
112                } else {
113                    if (buf_end - 1 < buf)
114                        break;
115                    count = 4 + *buf++;
116                }
117
118                if (outcnt < offset)
119                    break;
120
121                inp = out + outcnt - offset;
122            }
123
124            if (maxcnt - outcnt < count)
125                break;
126
127            for (i = 0; i < count; i++)
128                out[outcnt++] = inp[i];
129        }
130    }
131
132    if (outcnt - maxcnt)
133        av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
134
135    c->pic.data[0]     = (uint8_t *)c->cur;
136    c->pic.linesize[0] = w * 2;
137
138    *data_size = sizeof(AVFrame);
139    *(AVFrame*)data = c->pic;
140
141    FFSWAP(uint16_t *, c->cur, c->prev);
142
143    return avpkt->size;
144}
145
146static av_cold int decode_init(AVCodecContext *avctx)
147{
148    KgvContext * const c = avctx->priv_data;
149
150    c->avctx = avctx;
151    avctx->pix_fmt = PIX_FMT_RGB555;
152
153    return 0;
154}
155
156static av_cold int decode_end(AVCodecContext *avctx)
157{
158    KgvContext * const c = avctx->priv_data;
159
160    av_freep(&c->cur);
161    av_freep(&c->prev);
162
163    return 0;
164}
165
166AVCodec kgv1_decoder = {
167    "kgv1",
168    AVMEDIA_TYPE_VIDEO,
169    CODEC_ID_KGV1,
170    sizeof(KgvContext),
171    decode_init,
172    NULL,
173    decode_end,
174    decode_frame,
175    .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
176};
177