2 * Kega Game Video (KGV1) decoder
3 * Copyright (c) 2010 Daniel Verkamp
5 * This file is part of Libav.
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.
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.
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
24 * Kega Game Video decoder
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/imgutils.h"
32 AVCodecContext *avctx;
36 static void decode_flush(AVCodecContext *avctx)
38 KgvContext * const c = avctx->priv_data;
41 avctx->release_buffer(avctx, &c->prev);
44 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
46 const uint8_t *buf = avpkt->data;
47 const uint8_t *buf_end = buf + avpkt->size;
48 KgvContext * const c = avctx->priv_data;
51 int outcnt = 0, maxcnt;
61 if (av_image_check_size(w, h, 0, avctx))
64 if (w != avctx->width || h != avctx->height) {
66 avctx->release_buffer(avctx, &c->prev);
67 avcodec_set_dimensions(avctx, w, h);
73 if ((res = avctx->get_buffer(avctx, &c->cur)) < 0)
75 out = (uint16_t *) c->cur.data[0];
76 if (c->prev.data[0]) {
77 prev = (uint16_t *) c->prev.data[0];
82 for (i = 0; i < 8; i++)
85 while (outcnt < maxcnt && buf_end - 2 > buf) {
86 int code = AV_RL16(buf);
89 if (!(code & 0x8000)) {
90 out[outcnt++] = code; // rgb555 pixel coded directly
96 if ((code & 0x6000) == 0x6000) {
97 // copy from previous frame
98 int oidx = (code >> 10) & 7;
101 count = (code & 0x3FF) + 3;
103 if (offsets[oidx] < 0) {
104 if (buf_end - 3 < buf)
106 offsets[oidx] = AV_RL24(buf);
110 start = (outcnt + offsets[oidx]) % maxcnt;
112 if (maxcnt - start < count)
116 av_log(avctx, AV_LOG_ERROR,
117 "Frame reference does not exist\n");
124 // copy from earlier in this frame
125 int offset = (code & 0x1FFF) + 1;
127 if (!(code & 0x6000)) {
129 } else if ((code & 0x6000) == 0x2000) {
132 if (buf_end - 1 < buf)
141 inp_off = outcnt - offset;
144 if (maxcnt - outcnt < count)
147 for (i = inp_off; i < count + inp_off; i++) {
148 out[outcnt++] = inp[i];
154 av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
156 *data_size = sizeof(AVFrame);
157 *(AVFrame*)data = c->cur;
160 avctx->release_buffer(avctx, &c->prev);
161 FFSWAP(AVFrame, c->cur, c->prev);
166 static av_cold int decode_init(AVCodecContext *avctx)
168 KgvContext * const c = avctx->priv_data;
171 avctx->pix_fmt = PIX_FMT_RGB555;
172 avctx->flags |= CODEC_FLAG_EMU_EDGE;
177 static av_cold int decode_end(AVCodecContext *avctx)
183 AVCodec ff_kgv1_decoder = {
185 .type = AVMEDIA_TYPE_VIDEO,
187 .priv_data_size = sizeof(KgvContext),
190 .decode = decode_frame,
191 .flush = decode_flush,
192 .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),