Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / kgv1dec.c
blob74ae6b7ffe1a1f35aec0091c7513b873c06e6c50
1 /*
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
22 /**
23 * @file
24 * Kega Game Video decoder
27 #include "libavutil/common.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/imgutils.h"
30 #include "avcodec.h"
31 #include "internal.h"
33 typedef struct {
34 AVCodecContext *avctx;
35 AVFrame prev;
36 } KgvContext;
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,
46 AVPacket *avpkt)
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;
52 int offsets[8];
53 uint16_t *out, *prev;
54 int outcnt = 0, maxcnt;
55 int w, h, i, res;
57 if (avpkt->size < 2)
58 return AVERROR_INVALIDDATA;
60 w = (buf[0] + 1) * 8;
61 h = (buf[1] + 1) * 8;
62 buf += 2;
64 if ((res = av_image_check_size(w, h, 0, avctx)) < 0)
65 return res;
67 if (w != avctx->width || h != avctx->height) {
68 av_frame_unref(&c->prev);
69 avcodec_set_dimensions(avctx, w, h);
72 maxcnt = w * h;
74 if ((res = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
75 return res;
76 out = (uint16_t *) frame->data[0];
77 if (c->prev.data[0]) {
78 prev = (uint16_t *) c->prev.data[0];
79 } else {
80 prev = NULL;
83 for (i = 0; i < 8; i++)
84 offsets[i] = -1;
86 while (outcnt < maxcnt && buf_end - 2 > buf) {
87 int code = AV_RL16(buf);
88 buf += 2;
90 if (!(code & 0x8000)) {
91 out[outcnt++] = code; // rgb555 pixel coded directly
92 } else {
93 int count;
94 int inp_off;
95 uint16_t *inp;
97 if ((code & 0x6000) == 0x6000) {
98 // copy from previous frame
99 int oidx = (code >> 10) & 7;
100 int start;
102 count = (code & 0x3FF) + 3;
104 if (offsets[oidx] < 0) {
105 if (buf_end - 3 < buf)
106 break;
107 offsets[oidx] = AV_RL24(buf);
108 buf += 3;
111 start = (outcnt + offsets[oidx]) % maxcnt;
113 if (maxcnt - start < count)
114 break;
116 if (!prev) {
117 av_log(avctx, AV_LOG_ERROR,
118 "Frame reference does not exist\n");
119 break;
122 inp = prev;
123 inp_off = start;
124 } else {
125 // copy from earlier in this frame
126 int offset = (code & 0x1FFF) + 1;
128 if (!(code & 0x6000)) {
129 count = 2;
130 } else if ((code & 0x6000) == 0x2000) {
131 count = 3;
132 } else {
133 if (buf_end - 1 < buf)
134 break;
135 count = 4 + *buf++;
138 if (outcnt < offset)
139 break;
141 inp = out;
142 inp_off = outcnt - offset;
145 if (maxcnt - outcnt < count)
146 break;
148 for (i = inp_off; i < count + inp_off; i++) {
149 out[outcnt++] = inp[i];
154 if (outcnt - maxcnt)
155 av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
157 av_frame_unref(&c->prev);
158 if ((res = av_frame_ref(&c->prev, frame)) < 0)
159 return res;
161 *got_frame = 1;
163 return avpkt->size;
166 static av_cold int decode_init(AVCodecContext *avctx)
168 KgvContext * const c = avctx->priv_data;
170 c->avctx = avctx;
171 avctx->pix_fmt = AV_PIX_FMT_RGB555;
172 avctx->flags |= CODEC_FLAG_EMU_EDGE;
174 return 0;
177 static av_cold int decode_end(AVCodecContext *avctx)
179 decode_flush(avctx);
180 return 0;
183 AVCodec ff_kgv1_decoder = {
184 .name = "kgv1",
185 .type = AVMEDIA_TYPE_VIDEO,
186 .id = AV_CODEC_ID_KGV1,
187 .priv_data_size = sizeof(KgvContext),
188 .init = decode_init,
189 .close = decode_end,
190 .decode = decode_frame,
191 .flush = decode_flush,
192 .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
193 .capabilities = CODEC_CAP_DR1,