GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/kgv1dec.c Lines: 67 81 82.7 %
Date: 2019-11-22 03:34:36 Branches: 32 50 64.0 %

Line Branch Exec 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 "avcodec.h"
31
#include "internal.h"
32
33
typedef struct KgvContext {
34
    uint16_t *frame_buffer;
35
    uint16_t *last_frame_buffer;
36
} KgvContext;
37
38
2
static void decode_flush(AVCodecContext *avctx)
39
{
40
2
    KgvContext * const c = avctx->priv_data;
41
42
2
    av_freep(&c->frame_buffer);
43
2
    av_freep(&c->last_frame_buffer);
44
2
}
45
46
313
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
47
                        AVPacket *avpkt)
48
{
49
313
    AVFrame *frame = data;
50
313
    const uint8_t *buf = avpkt->data;
51
313
    const uint8_t *buf_end = buf + avpkt->size;
52
313
    KgvContext * const c = avctx->priv_data;
53
    int offsets[8];
54
    uint8_t *out, *prev;
55
313
    int outcnt = 0, maxcnt;
56
    int w, h, i, res;
57
58
313
    if (avpkt->size < 2)
59
        return AVERROR_INVALIDDATA;
60
61
313
    w = (buf[0] + 1) * 8;
62
313
    h = (buf[1] + 1) * 8;
63
313
    buf += 2;
64
65
313
    if (avpkt->size < 2 + w*h / 513)
66
        return AVERROR_INVALIDDATA;
67
68

313
    if (w != avctx->width || h != avctx->height) {
69
        av_freep(&c->frame_buffer);
70
        av_freep(&c->last_frame_buffer);
71
        if ((res = ff_set_dimensions(avctx, w, h)) < 0)
72
            return res;
73
    }
74
75
313
    if (!c->frame_buffer) {
76
1
        c->frame_buffer      = av_mallocz(avctx->width * avctx->height * 2);
77
1
        c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
78

1
        if (!c->frame_buffer || !c->last_frame_buffer) {
79
            decode_flush(avctx);
80
            return AVERROR(ENOMEM);
81
        }
82
    }
83
84
313
    maxcnt = w * h;
85
86
313
    if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
87
        return res;
88
313
    out  = (uint8_t*)c->frame_buffer;
89
313
    prev = (uint8_t*)c->last_frame_buffer;
90
91
2817
    for (i = 0; i < 8; i++)
92
2504
        offsets[i] = -1;
93
94

51104
    while (outcnt < maxcnt && buf_end - 2 >= buf) {
95
50791
        int code = AV_RL16(buf);
96
50791
        buf += 2;
97
98
50791
        if (!(code & 0x8000)) {
99
756
            AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
100
756
            outcnt++;
101
        } else {
102
            int count;
103
104
50035
            if ((code & 0x6000) == 0x6000) {
105
                // copy from previous frame
106
22112
                int oidx = (code >> 10) & 7;
107
                int start;
108
109
22112
                count = (code & 0x3FF) + 3;
110
111
22112
                if (offsets[oidx] < 0) {
112
304
                    if (buf_end - 3 < buf)
113
                        break;
114
304
                    offsets[oidx] = AV_RL24(buf);
115
304
                    buf += 3;
116
                }
117
118
22112
                start = (outcnt + offsets[oidx]) % maxcnt;
119
120

22112
                if (maxcnt - start < count || maxcnt - outcnt < count)
121
                    break;
122
123
22112
                if (!prev) {
124
                    av_log(avctx, AV_LOG_ERROR,
125
                           "Frame reference does not exist\n");
126
                    break;
127
                }
128
129
22112
                memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
130
            } else {
131
                // copy from earlier in this frame
132
27923
                int offset = (code & 0x1FFF) + 1;
133
134
27923
                if (!(code & 0x6000)) {
135
1091
                    count = 2;
136
26832
                } else if ((code & 0x6000) == 0x2000) {
137
776
                    count = 3;
138
                } else {
139
26056
                    if (buf_end - 1 < buf)
140
                        break;
141
26056
                    count = 4 + *buf++;
142
                }
143
144

27923
                if (outcnt < offset || maxcnt - outcnt < count)
145
                    break;
146
147
27923
                av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
148
            }
149
50035
            outcnt += count;
150
        }
151
    }
152
153
313
    if (outcnt - maxcnt)
154
        av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
155
156
313
    av_image_copy_plane(frame->data[0], frame->linesize[0],
157
313
                        (const uint8_t*)c->frame_buffer,  avctx->width * 2,
158
313
                        avctx->width * 2, avctx->height);
159
313
    FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer);
160
161
313
    *got_frame = 1;
162
163
313
    return avpkt->size;
164
}
165
166
2
static av_cold int decode_init(AVCodecContext *avctx)
167
{
168
2
    avctx->pix_fmt = AV_PIX_FMT_RGB555;
169
170
2
    return 0;
171
}
172
173
2
static av_cold int decode_end(AVCodecContext *avctx)
174
{
175
2
    decode_flush(avctx);
176
2
    return 0;
177
}
178
179
AVCodec ff_kgv1_decoder = {
180
    .name           = "kgv1",
181
    .long_name      = NULL_IF_CONFIG_SMALL("Kega Game Video"),
182
    .type           = AVMEDIA_TYPE_VIDEO,
183
    .id             = AV_CODEC_ID_KGV1,
184
    .priv_data_size = sizeof(KgvContext),
185
    .init           = decode_init,
186
    .close          = decode_end,
187
    .decode         = decode_frame,
188
    .flush          = decode_flush,
189
    .capabilities   = AV_CODEC_CAP_DR1,
190
};