2 * Kega Game Video (KGV1) decoder
3 * Copyright (c) 2010 Daniel Verkamp
5 * This file is part of FFmpeg.
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.
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.
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
24 * Kega Game Video decoder
27 #include "libavutil/intreadwrite.h"
31 AVCodecContext *avctx;
36 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
38 const uint8_t *buf = avpkt->data;
39 const uint8_t *buf_end = buf + avpkt->size;
40 KgvContext * const c = avctx->priv_data;
43 int outcnt = 0, maxcnt;
53 if (avcodec_check_dimensions(avctx, w, h))
56 if (w != avctx->width || h != avctx->height)
57 avcodec_set_dimensions(avctx, w, h);
61 out = av_realloc(c->cur, w * h * 2);
66 prev = av_realloc(c->prev, w * h * 2);
71 for (i = 0; i < 7; i++)
74 while (outcnt < maxcnt && buf_end - 2 > buf) {
75 int code = AV_RL16(buf);
78 if (!(code & 0x8000)) {
79 out[outcnt++] = code; // rgb555 pixel coded directly
84 if ((code & 0x6000) == 0x6000) {
85 // copy from previous frame
86 int oidx = (code >> 10) & 7;
89 count = (code & 0x3FF) + 3;
91 if (offsets[oidx] < 0) {
92 if (buf_end - 3 < buf)
94 offsets[oidx] = AV_RL24(buf);
98 start = (outcnt + offsets[oidx]) % maxcnt;
100 if (maxcnt - start < count)
105 // copy from earlier in this frame
106 int offset = (code & 0x1FFF) + 1;
108 if (!(code & 0x6000)) {
110 } else if ((code & 0x6000) == 0x2000) {
113 if (buf_end - 1 < buf)
121 inp = out + outcnt - offset;
124 if (maxcnt - outcnt < count)
127 for (i = 0; i < count; i++)
128 out[outcnt++] = inp[i];
133 av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
135 c->pic.data[0] = (uint8_t *)c->cur;
136 c->pic.linesize[0] = w * 2;
138 *data_size = sizeof(AVFrame);
139 *(AVFrame*)data = c->pic;
141 FFSWAP(uint16_t *, c->cur, c->prev);
146 static av_cold int decode_init(AVCodecContext *avctx)
148 KgvContext * const c = avctx->priv_data;
151 avctx->pix_fmt = PIX_FMT_RGB555;
156 static av_cold int decode_end(AVCodecContext *avctx)
158 KgvContext * const c = avctx->priv_data;
166 AVCodec kgv1_decoder = {
176 .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),