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