]> git.sesse.net Git - ffmpeg/blob - libavcodec/kgv1dec.c
ca639b7c5df5859992d1d6638b4ca44140b59f59
[ffmpeg] / libavcodec / kgv1dec.c
1 /*
2  * Kega Game Video (KGV1) decoder
3  * Copyright (c) 2010 Daniel Verkamp
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "libavutil/imgutils.h"
29 #include "avcodec.h"
30
31 typedef struct {
32     AVCodecContext *avctx;
33     AVFrame prev, cur;
34 } KgvContext;
35
36 static void decode_flush(AVCodecContext *avctx)
37 {
38     KgvContext * const c = avctx->priv_data;
39
40     if (c->prev.data[0])
41         avctx->release_buffer(avctx, &c->prev);
42 }
43
44 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
45 {
46     const uint8_t *buf = avpkt->data;
47     const uint8_t *buf_end = buf + avpkt->size;
48     KgvContext * const c = avctx->priv_data;
49     int offsets[8];
50     uint16_t *out, *prev;
51     int outcnt = 0, maxcnt;
52     int w, h, i, res;
53
54     if (avpkt->size < 2)
55         return -1;
56
57     w = (buf[0] + 1) * 8;
58     h = (buf[1] + 1) * 8;
59     buf += 2;
60
61     if (av_image_check_size(w, h, 0, avctx))
62         return -1;
63
64     if (w != avctx->width || h != avctx->height)
65         avcodec_set_dimensions(avctx, w, h);
66
67     maxcnt = w * h;
68
69     c->cur.reference = 3;
70     if ((res = avctx->get_buffer(avctx, &c->cur)) < 0)
71         return res;
72     out  = (uint16_t *) c->cur.data[0];
73     if (c->prev.data[0]) {
74         prev = (uint16_t *) c->prev.data[0];
75     } else {
76         prev = NULL;
77     }
78
79     for (i = 0; i < 8; i++)
80         offsets[i] = -1;
81
82     while (outcnt < maxcnt && buf_end - 2 > buf) {
83         int code = AV_RL16(buf);
84         buf += 2;
85
86         if (!(code & 0x8000)) {
87             out[outcnt++] = code; // rgb555 pixel coded directly
88         } else {
89             int count;
90             int inp_off;
91             uint16_t *inp;
92
93             if ((code & 0x6000) == 0x6000) {
94                 // copy from previous frame
95                 int oidx = (code >> 10) & 7;
96                 int start;
97
98                 count = (code & 0x3FF) + 3;
99
100                 if (offsets[oidx] < 0) {
101                     if (buf_end - 3 < buf)
102                         break;
103                     offsets[oidx] = AV_RL24(buf);
104                     buf += 3;
105                 }
106
107                 start = (outcnt + offsets[oidx]) % maxcnt;
108
109                 if (maxcnt - start < count)
110                     break;
111
112                 if (!prev) {
113                     av_log(avctx, AV_LOG_ERROR,
114                            "Frame reference does not exist\n");
115                     break;
116                 }
117
118                 inp = prev;
119                 inp_off = start;
120             } else {
121                 // copy from earlier in this frame
122                 int offset = (code & 0x1FFF) + 1;
123
124                 if (!(code & 0x6000)) {
125                     count = 2;
126                 } else if ((code & 0x6000) == 0x2000) {
127                     count = 3;
128                 } else {
129                     if (buf_end - 1 < buf)
130                         break;
131                     count = 4 + *buf++;
132                 }
133
134                 if (outcnt < offset)
135                     break;
136
137                 inp = out;
138                 inp_off = outcnt - offset;
139             }
140
141             if (maxcnt - outcnt < count)
142                 break;
143
144             for (i = inp_off; i < count + inp_off; i++) {
145                 out[outcnt++] = inp[i];
146             }
147         }
148     }
149
150     if (outcnt - maxcnt)
151         av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
152
153     *data_size = sizeof(AVFrame);
154     *(AVFrame*)data = c->cur;
155
156     if (c->prev.data[0])
157         avctx->release_buffer(avctx, &c->prev);
158     FFSWAP(AVFrame, c->cur, c->prev);
159
160     return avpkt->size;
161 }
162
163 static av_cold int decode_init(AVCodecContext *avctx)
164 {
165     KgvContext * const c = avctx->priv_data;
166
167     c->avctx = avctx;
168     avctx->pix_fmt = PIX_FMT_RGB555;
169     avctx->flags  |= CODEC_FLAG_EMU_EDGE;
170
171     return 0;
172 }
173
174 static av_cold int decode_end(AVCodecContext *avctx)
175 {
176     decode_flush(avctx);
177     return 0;
178 }
179
180 AVCodec ff_kgv1_decoder = {
181     .name           = "kgv1",
182     .type           = AVMEDIA_TYPE_VIDEO,
183     .id             = CODEC_ID_KGV1,
184     .priv_data_size = sizeof(KgvContext),
185     .init           = decode_init,
186     .close          = decode_end,
187     .decode         = decode_frame,
188     .flush          = decode_flush,
189     .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
190 };