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"
34 AVCodecContext *avctx;
38 static void decode_flush(AVCodecContext *avctx)
40 KgvContext * const c = avctx->priv_data;
42 av_frame_unref(&c->prev);
45 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
48 AVFrame *frame = data;
49 const uint8_t *buf = avpkt->data;
50 const uint8_t *buf_end = buf + avpkt->size;
51 KgvContext * const c = avctx->priv_data;
54 int outcnt = 0, maxcnt;
58 return AVERROR_INVALIDDATA;
64 if ((res = av_image_check_size(w, h, 0, avctx)) < 0)
67 if (w != avctx->width || h != avctx->height) {
68 av_frame_unref(&c->prev);
69 avcodec_set_dimensions(avctx, w, h);
74 if ((res = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
77 if (c->prev.data[0]) {
78 prev = c->prev.data[0];
83 for (i = 0; i < 8; i++)
86 while (outcnt < maxcnt && buf_end - 2 >= buf) {
87 int code = AV_RL16(buf);
90 if (!(code & 0x8000)) {
91 AV_WN16A(&out[2 * 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 || maxcnt - outcnt < count)
116 av_log(avctx, AV_LOG_ERROR,
117 "Frame reference does not exist\n");
121 memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
123 // copy from earlier in this frame
124 int offset = (code & 0x1FFF) + 1;
126 if (!(code & 0x6000)) {
128 } else if ((code & 0x6000) == 0x2000) {
131 if (buf_end - 1 < buf)
136 if (outcnt < offset || maxcnt - outcnt < count)
139 av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
146 av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
148 av_frame_unref(&c->prev);
149 if ((res = av_frame_ref(&c->prev, frame)) < 0)
157 static av_cold int decode_init(AVCodecContext *avctx)
159 KgvContext * const c = avctx->priv_data;
162 avctx->pix_fmt = AV_PIX_FMT_RGB555;
163 avctx->flags |= CODEC_FLAG_EMU_EDGE;
168 static av_cold int decode_end(AVCodecContext *avctx)
174 AVCodec ff_kgv1_decoder = {
176 .type = AVMEDIA_TYPE_VIDEO,
177 .id = AV_CODEC_ID_KGV1,
178 .priv_data_size = sizeof(KgvContext),
181 .decode = decode_frame,
182 .flush = decode_flush,
183 .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
184 .capabilities = CODEC_CAP_DR1,