kgv1dec.c 5.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Kega Game Video (KGV1) decoder
 * Copyright (c) 2010 Daniel Verkamp
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25 26
 * 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"
32

33
typedef struct KgvContext {
34 35
    uint16_t *frame_buffer;
    uint16_t *last_frame_buffer;
36 37
} KgvContext;

38 39 40 41
static void decode_flush(AVCodecContext *avctx)
{
    KgvContext * const c = avctx->priv_data;

42 43
    av_freep(&c->frame_buffer);
    av_freep(&c->last_frame_buffer);
44 45
}

46 47
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
                        AVPacket *avpkt)
48
{
49
    AVFrame *frame = data;
50 51 52
    const uint8_t *buf = avpkt->data;
    const uint8_t *buf_end = buf + avpkt->size;
    KgvContext * const c = avctx->priv_data;
53
    int offsets[8];
54
    uint8_t *out, *prev;
55
    int outcnt = 0, maxcnt;
56
    int w, h, i, res;
57 58

    if (avpkt->size < 2)
59
        return AVERROR_INVALIDDATA;
60 61 62 63 64

    w = (buf[0] + 1) * 8;
    h = (buf[1] + 1) * 8;
    buf += 2;

65
    if (w != avctx->width || h != avctx->height) {
66 67
        av_freep(&c->frame_buffer);
        av_freep(&c->last_frame_buffer);
68 69
        if ((res = ff_set_dimensions(avctx, w, h)) < 0)
            return res;
70
    }
71

72 73 74 75 76 77 78 79 80
    if (!c->frame_buffer) {
        c->frame_buffer      = av_mallocz(avctx->width * avctx->height * 2);
        c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
        if (!c->frame_buffer || !c->last_frame_buffer) {
            decode_flush(avctx);
            return AVERROR(ENOMEM);
        }
    }

81 82
    maxcnt = w * h;

83
    if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
84
        return res;
85 86
    out  = (uint8_t*)c->frame_buffer;
    prev = (uint8_t*)c->last_frame_buffer;
87

88
    for (i = 0; i < 8; i++)
89 90
        offsets[i] = -1;

91
    while (outcnt < maxcnt && buf_end - 2 >= buf) {
92 93 94 95
        int code = AV_RL16(buf);
        buf += 2;

        if (!(code & 0x8000)) {
96 97
            AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
            outcnt++;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        } else {
            int count;

            if ((code & 0x6000) == 0x6000) {
                // copy from previous frame
                int oidx = (code >> 10) & 7;
                int start;

                count = (code & 0x3FF) + 3;

                if (offsets[oidx] < 0) {
                    if (buf_end - 3 < buf)
                        break;
                    offsets[oidx] = AV_RL24(buf);
                    buf += 3;
                }

                start = (outcnt + offsets[oidx]) % maxcnt;

117
                if (maxcnt - start < count || maxcnt - outcnt < count)
118 119
                    break;

120 121 122 123 124 125
                if (!prev) {
                    av_log(avctx, AV_LOG_ERROR,
                           "Frame reference does not exist\n");
                    break;
                }

126
                memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
127 128 129 130 131 132 133 134 135 136 137 138 139 140
            } else {
                // copy from earlier in this frame
                int offset = (code & 0x1FFF) + 1;

                if (!(code & 0x6000)) {
                    count = 2;
                } else if ((code & 0x6000) == 0x2000) {
                    count = 3;
                } else {
                    if (buf_end - 1 < buf)
                        break;
                    count = 4 + *buf++;
                }

141
                if (outcnt < offset || maxcnt - outcnt < count)
142 143
                    break;

144
                av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
145
            }
146
            outcnt += count;
147 148 149 150 151 152
        }
    }

    if (outcnt - maxcnt)
        av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);

153 154 155 156
    av_image_copy_plane(frame->data[0], frame->linesize[0],
                        (const uint8_t*)c->frame_buffer,  avctx->width * 2,
                        avctx->width * 2, avctx->height);
    FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer);
157

158
    *got_frame = 1;
159 160 161 162 163 164

    return avpkt->size;
}

static av_cold int decode_init(AVCodecContext *avctx)
{
165
    avctx->pix_fmt = AV_PIX_FMT_RGB555;
166 167 168 169

    return 0;
}

170 171 172 173 174 175
static av_cold int decode_end(AVCodecContext *avctx)
{
    decode_flush(avctx);
    return 0;
}

176
AVCodec ff_kgv1_decoder = {
177
    .name           = "kgv1",
178
    .long_name      = NULL_IF_CONFIG_SMALL("Kega Game Video"),
179
    .type           = AVMEDIA_TYPE_VIDEO,
180
    .id             = AV_CODEC_ID_KGV1,
181 182
    .priv_data_size = sizeof(KgvContext),
    .init           = decode_init,
183
    .close          = decode_end,
184
    .decode         = decode_frame,
185
    .flush          = decode_flush,
186
    .capabilities   = AV_CODEC_CAP_DR1,
187
};