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/common.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/imgutils.h"
37 static void decode_flush(AVCodecContext *avctx)
39 KgvContext * const c = avctx->priv_data;
41 av_frame_unref(c->prev);
44 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
47 AVFrame *frame = data;
48 const uint8_t *buf = avpkt->data;
49 const uint8_t *buf_end = buf + avpkt->size;
50 KgvContext * const c = avctx->priv_data;
53 int outcnt = 0, maxcnt;
57 return AVERROR_INVALIDDATA;
63 if ((res = av_image_check_size(w, h, 0, avctx)) < 0)
66 if (w != avctx->width || h != avctx->height) {
67 av_frame_unref(c->prev);
68 avcodec_set_dimensions(avctx, w, h);
73 if ((res = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
76 if (c->prev->data[0]) {
77 prev = 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 AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
95 if ((code & 0x6000) == 0x6000) {
96 // copy from previous frame
97 int oidx = (code >> 10) & 7;
100 count = (code & 0x3FF) + 3;
102 if (offsets[oidx] < 0) {
103 if (buf_end - 3 < buf)
105 offsets[oidx] = AV_RL24(buf);
109 start = (outcnt + offsets[oidx]) % maxcnt;
111 if (maxcnt - start < count || maxcnt - outcnt < count)
115 av_log(avctx, AV_LOG_ERROR,
116 "Frame reference does not exist\n");
120 memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
122 // copy from earlier in this frame
123 int offset = (code & 0x1FFF) + 1;
125 if (!(code & 0x6000)) {
127 } else if ((code & 0x6000) == 0x2000) {
130 if (buf_end - 1 < buf)
135 if (outcnt < offset || maxcnt - outcnt < count)
138 av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
145 av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
147 av_frame_unref(c->prev);
148 if ((res = av_frame_ref(c->prev, frame)) < 0)
156 static av_cold int decode_init(AVCodecContext *avctx)
158 KgvContext * const c = avctx->priv_data;
160 avctx->pix_fmt = AV_PIX_FMT_RGB555;
161 avctx->flags |= CODEC_FLAG_EMU_EDGE;
163 c->prev = av_frame_alloc();
165 return AVERROR(ENOMEM);
170 static av_cold int decode_end(AVCodecContext *avctx)
172 KgvContext * const c = avctx->priv_data;
173 av_frame_free(&c->prev);
177 AVCodec ff_kgv1_decoder = {
179 .type = AVMEDIA_TYPE_VIDEO,
180 .id = AV_CODEC_ID_KGV1,
181 .priv_data_size = sizeof(KgvContext),
184 .decode = decode_frame,
185 .flush = decode_flush,
186 .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
187 .capabilities = CODEC_CAP_DR1,