Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/kgv1dec.c
Line
Count
Source
1
/*
2
 * Kega Game Video (KGV1) decoder
3
 * Copyright (c) 2010 Daniel Verkamp
4
 *
5
 * This file is part of FFmpeg.
6
 *
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.
11
 *
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.
16
 *
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
20
 */
21
22
/**
23
 * @file
24
 * Kega Game Video decoder
25
 */
26
27
#include "libavutil/common.h"
28
#include "libavutil/intreadwrite.h"
29
#include "libavutil/imgutils.h"
30
#include "libavutil/mem.h"
31
#include "avcodec.h"
32
#include "codec_internal.h"
33
#include "decode.h"
34
35
typedef struct KgvContext {
36
    uint16_t *frame_buffer;
37
    uint16_t *last_frame_buffer;
38
} KgvContext;
39
40
static void decode_flush(AVCodecContext *avctx)
41
78.9k
{
42
78.9k
    KgvContext * const c = avctx->priv_data;
43
44
78.9k
    av_freep(&c->frame_buffer);
45
78.9k
    av_freep(&c->last_frame_buffer);
46
78.9k
}
47
48
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
49
                        int *got_frame, AVPacket *avpkt)
50
307k
{
51
307k
    const uint8_t *buf = avpkt->data;
52
307k
    const uint8_t *buf_end = buf + avpkt->size;
53
307k
    KgvContext * const c = avctx->priv_data;
54
307k
    int offsets[8];
55
307k
    uint8_t *out, *prev;
56
307k
    int outcnt = 0, maxcnt;
57
307k
    int w, h, i, res;
58
59
307k
    if (avpkt->size < 2)
60
165k
        return AVERROR_INVALIDDATA;
61
62
141k
    w = (buf[0] + 1) * 8;
63
141k
    h = (buf[1] + 1) * 8;
64
141k
    buf += 2;
65
66
141k
    if (avpkt->size < 2 + w*h / 513)
67
14.6k
        return AVERROR_INVALIDDATA;
68
69
127k
    if (w != avctx->width || h != avctx->height) {
70
9.74k
        av_freep(&c->frame_buffer);
71
9.74k
        av_freep(&c->last_frame_buffer);
72
9.74k
        if ((res = ff_set_dimensions(avctx, w, h)) < 0)
73
0
            return res;
74
9.74k
    }
75
76
127k
    if (!c->frame_buffer) {
77
50.2k
        c->frame_buffer      = av_mallocz(avctx->width * avctx->height * 2);
78
50.2k
        c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
79
50.2k
        if (!c->frame_buffer || !c->last_frame_buffer) {
80
0
            decode_flush(avctx);
81
0
            return AVERROR(ENOMEM);
82
0
        }
83
50.2k
    }
84
85
127k
    maxcnt = w * h;
86
87
127k
    if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
88
0
        return res;
89
127k
    out  = (uint8_t*)c->frame_buffer;
90
127k
    prev = (uint8_t*)c->last_frame_buffer;
91
92
1.14M
    for (i = 0; i < 8; i++)
93
1.01M
        offsets[i] = -1;
94
95
328k
    while (outcnt < maxcnt && buf_end - 2 >= buf) {
96
204k
        int code = AV_RL16(buf);
97
204k
        buf += 2;
98
99
204k
        if (!(code & 0x8000)) {
100
111k
            AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
101
111k
            outcnt++;
102
111k
        } else {
103
92.2k
            int count;
104
105
92.2k
            if ((code & 0x6000) == 0x6000) {
106
                // copy from previous frame
107
67.6k
                int oidx = (code >> 10) & 7;
108
67.6k
                int start;
109
110
67.6k
                count = (code & 0x3FF) + 3;
111
112
67.6k
                if (offsets[oidx] < 0) {
113
2.87k
                    if (buf_end - 3 < buf)
114
534
                        break;
115
2.34k
                    offsets[oidx] = AV_RL24(buf);
116
2.34k
                    buf += 3;
117
2.34k
                }
118
119
67.0k
                start = (outcnt + offsets[oidx]) % maxcnt;
120
121
67.0k
                if (maxcnt - start < count || maxcnt - outcnt < count)
122
1.27k
                    break;
123
124
65.8k
                if (!prev) {
125
0
                    av_log(avctx, AV_LOG_ERROR,
126
0
                           "Frame reference does not exist\n");
127
0
                    break;
128
0
                }
129
130
65.8k
                memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
131
65.8k
            } else {
132
                // copy from earlier in this frame
133
24.5k
                int offset = (code & 0x1FFF) + 1;
134
135
24.5k
                if (!(code & 0x6000)) {
136
6.95k
                    count = 2;
137
17.6k
                } else if ((code & 0x6000) == 0x2000) {
138
8.51k
                    count = 3;
139
9.12k
                } else {
140
9.12k
                    if (buf_end - 1 < buf)
141
215
                        break;
142
8.90k
                    count = 4 + *buf++;
143
8.90k
                }
144
145
24.3k
                if (outcnt < offset || maxcnt - outcnt < count)
146
1.06k
                    break;
147
148
23.3k
                av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
149
23.3k
            }
150
89.1k
            outcnt += count;
151
89.1k
        }
152
204k
    }
153
154
127k
    if (outcnt - maxcnt)
155
126k
        av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
156
157
127k
    av_image_copy_plane(frame->data[0], frame->linesize[0],
158
127k
                        (const uint8_t*)c->frame_buffer,  avctx->width * 2,
159
127k
                        avctx->width * 2, avctx->height);
160
127k
    FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer);
161
162
127k
    *got_frame = 1;
163
164
127k
    return avpkt->size;
165
127k
}
166
167
static av_cold int decode_init(AVCodecContext *avctx)
168
1.13k
{
169
1.13k
    avctx->pix_fmt = AV_PIX_FMT_RGB555;
170
171
1.13k
    return 0;
172
1.13k
}
173
174
static av_cold int decode_end(AVCodecContext *avctx)
175
1.13k
{
176
1.13k
    decode_flush(avctx);
177
1.13k
    return 0;
178
1.13k
}
179
180
const FFCodec ff_kgv1_decoder = {
181
    .p.name         = "kgv1",
182
    CODEC_LONG_NAME("Kega Game Video"),
183
    .p.type         = AVMEDIA_TYPE_VIDEO,
184
    .p.id           = AV_CODEC_ID_KGV1,
185
    .priv_data_size = sizeof(KgvContext),
186
    .init           = decode_init,
187
    .close          = decode_end,
188
    FF_CODEC_DECODE_CB(decode_frame),
189
    .flush          = decode_flush,
190
    .p.capabilities = AV_CODEC_CAP_DR1,
191
};