GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/midivid.c Lines: 0 139 0.0 %
Date: 2019-12-07 16:49:47 Branches: 0 66 0.0 %

Line Branch Exec Source
1
/*
2
 * MidiVid decoder
3
 * Copyright (c) 2019 Paul B Mahol
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
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "libavutil/imgutils.h"
27
#include "libavutil/internal.h"
28
#include "libavutil/intreadwrite.h"
29
#include "libavutil/mem.h"
30
31
#define BITSTREAM_READER_LE
32
#include "avcodec.h"
33
#include "get_bits.h"
34
#include "bytestream.h"
35
#include "internal.h"
36
37
typedef struct MidiVidContext {
38
    GetByteContext gb;
39
40
    uint8_t *uncompressed;
41
    unsigned int uncompressed_size;
42
    uint8_t *skip;
43
44
    AVFrame *frame;
45
} MidiVidContext;
46
47
static int decode_mvdv(MidiVidContext *s, AVCodecContext *avctx, AVFrame *frame)
48
{
49
    GetByteContext *gb = &s->gb;
50
    GetBitContext mask;
51
    GetByteContext idx9;
52
    uint16_t nb_vectors, intra_flag;
53
    const uint8_t *vec;
54
    const uint8_t *mask_start;
55
    uint8_t *skip;
56
    uint32_t mask_size;
57
    int idx9bits = 0;
58
    int idx9val = 0;
59
    uint32_t nb_blocks;
60
61
    nb_vectors = bytestream2_get_le16(gb);
62
    intra_flag = bytestream2_get_le16(gb);
63
    if (intra_flag) {
64
        nb_blocks = (avctx->width / 2) * (avctx->height / 2);
65
    } else {
66
        int skip_linesize;
67
68
        nb_blocks = bytestream2_get_le32(gb);
69
        skip_linesize = avctx->width >> 1;
70
        mask_start = gb->buffer_start + bytestream2_tell(gb);
71
        mask_size = (avctx->width >> 5) * (avctx->height >> 2);
72
73
        if (bytestream2_get_bytes_left(gb) < mask_size)
74
            return AVERROR_INVALIDDATA;
75
76
        init_get_bits8(&mask, mask_start, mask_size);
77
        bytestream2_skip(gb, mask_size);
78
        skip = s->skip;
79
80
        for (int y = 0; y < avctx->height >> 2; y++) {
81
            for (int x = 0; x < avctx->width >> 2; x++) {
82
                int flag = !get_bits1(&mask);
83
84
                skip[(y*2)  *skip_linesize + x*2  ] = flag;
85
                skip[(y*2)  *skip_linesize + x*2+1] = flag;
86
                skip[(y*2+1)*skip_linesize + x*2  ] = flag;
87
                skip[(y*2+1)*skip_linesize + x*2+1] = flag;
88
            }
89
        }
90
    }
91
92
    vec = gb->buffer_start + bytestream2_tell(gb);
93
    if (bytestream2_get_bytes_left(gb) < nb_vectors * 12)
94
        return AVERROR_INVALIDDATA;
95
    bytestream2_skip(gb, nb_vectors * 12);
96
    if (nb_vectors > 256) {
97
        if (bytestream2_get_bytes_left(gb) < (nb_blocks + 7) / 8)
98
            return AVERROR_INVALIDDATA;
99
        bytestream2_init(&idx9, gb->buffer_start + bytestream2_tell(gb), (nb_blocks + 7) / 8);
100
        bytestream2_skip(gb, (nb_blocks + 7) / 8);
101
    }
102
103
    skip = s->skip;
104
105
    for (int y = avctx->height - 2; y >= 0; y -= 2) {
106
        uint8_t *dsty = frame->data[0] + y * frame->linesize[0];
107
        uint8_t *dstu = frame->data[1] + y * frame->linesize[1];
108
        uint8_t *dstv = frame->data[2] + y * frame->linesize[2];
109
110
        for (int x = 0; x < avctx->width; x += 2) {
111
            int idx;
112
113
            if (!intra_flag && *skip++)
114
                continue;
115
            if (bytestream2_get_bytes_left(gb) <= 0)
116
                return AVERROR_INVALIDDATA;
117
            if (nb_vectors <= 256) {
118
                idx = bytestream2_get_byte(gb);
119
            } else {
120
                if (idx9bits == 0) {
121
                    idx9val = bytestream2_get_byte(&idx9);
122
                    idx9bits = 8;
123
                }
124
                idx9bits--;
125
                idx = bytestream2_get_byte(gb) | (((idx9val >> (7 - idx9bits)) & 1) << 8);
126
            }
127
128
            dsty[x  +frame->linesize[0]] = vec[idx * 12 + 0];
129
            dsty[x+1+frame->linesize[0]] = vec[idx * 12 + 3];
130
            dsty[x]                      = vec[idx * 12 + 6];
131
            dsty[x+1]                    = vec[idx * 12 + 9];
132
133
            dstu[x  +frame->linesize[1]] = vec[idx * 12 + 1];
134
            dstu[x+1+frame->linesize[1]] = vec[idx * 12 + 4];
135
            dstu[x]                      = vec[idx * 12 + 7];
136
            dstu[x+1]                    = vec[idx * 12 +10];
137
138
            dstv[x  +frame->linesize[2]] = vec[idx * 12 + 2];
139
            dstv[x+1+frame->linesize[2]] = vec[idx * 12 + 5];
140
            dstv[x]                      = vec[idx * 12 + 8];
141
            dstv[x+1]                    = vec[idx * 12 +11];
142
        }
143
    }
144
145
    return intra_flag;
146
}
147
148
static ptrdiff_t lzss_uncompress(MidiVidContext *s, GetByteContext *gb, uint8_t *dst, unsigned int size)
149
{
150
    uint8_t *dst_start = dst;
151
    uint8_t *dst_end = dst + size;
152
153
    for (;bytestream2_get_bytes_left(gb) >= 3;) {
154
        int op = bytestream2_get_le16(gb);
155
156
        for (int i = 0; i < 16; i++) {
157
            if (op & 1) {
158
                int s0 = bytestream2_get_byte(gb);
159
                int s1 = bytestream2_get_byte(gb);
160
                int offset = ((s0 & 0xF0) << 4) | s1;
161
                int length = (s0 & 0xF) + 3;
162
163
                if (dst + length > dst_end ||
164
                    dst - offset < dst_start)
165
                    return AVERROR_INVALIDDATA;
166
                if (offset > 0) {
167
                    for (int j = 0; j < length; j++) {
168
                        dst[j] = dst[j - offset];
169
                    }
170
                }
171
                dst += length;
172
            } else {
173
                if (dst >= dst_end)
174
                    return AVERROR_INVALIDDATA;
175
                *dst++ = bytestream2_get_byte(gb);
176
            }
177
            op >>= 1;
178
        }
179
    }
180
181
    return dst - dst_start;
182
}
183
184
static int decode_frame(AVCodecContext *avctx, void *data,
185
                        int *got_frame, AVPacket *avpkt)
186
{
187
    MidiVidContext *s = avctx->priv_data;
188
    GetByteContext *gb = &s->gb;
189
    AVFrame *frame = s->frame;
190
    int ret, key, uncompressed;
191
192
    if (avpkt->size <= 13)
193
        return AVERROR_INVALIDDATA;
194
195
    bytestream2_init(gb, avpkt->data, avpkt->size);
196
    bytestream2_skip(gb, 8);
197
    uncompressed = bytestream2_get_le32(gb);
198
199
    if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
200
        return ret;
201
202
    if (uncompressed) {
203
        ret = decode_mvdv(s, avctx, frame);
204
    } else {
205
        av_fast_padded_malloc(&s->uncompressed, &s->uncompressed_size, 16LL * (avpkt->size - 12));
206
        if (!s->uncompressed)
207
            return AVERROR(ENOMEM);
208
209
        ret = lzss_uncompress(s, gb, s->uncompressed, s->uncompressed_size);
210
        if (ret < 0)
211
            return ret;
212
        bytestream2_init(gb, s->uncompressed, ret);
213
        ret = decode_mvdv(s, avctx, frame);
214
    }
215
216
    if (ret < 0)
217
        return ret;
218
    key = ret;
219
220
    if ((ret = av_frame_ref(data, s->frame)) < 0)
221
        return ret;
222
223
    frame->pict_type = key ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
224
    frame->key_frame = key;
225
    *got_frame = 1;
226
227
    return avpkt->size;
228
}
229
230
static av_cold int decode_init(AVCodecContext *avctx)
231
{
232
    MidiVidContext *s = avctx->priv_data;
233
    int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
234
235
    if (ret < 0) {
236
        av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
237
               avctx->width, avctx->height);
238
        return ret;
239
    }
240
241
    avctx->pix_fmt = AV_PIX_FMT_YUV444P;
242
243
    s->frame = av_frame_alloc();
244
    if (!s->frame)
245
        return AVERROR(ENOMEM);
246
    s->skip = av_calloc(avctx->width >> 1, avctx->height >> 1);
247
    if (!s->skip)
248
        return AVERROR(ENOMEM);
249
250
    return 0;
251
}
252
253
static void decode_flush(AVCodecContext *avctx)
254
{
255
    MidiVidContext *s = avctx->priv_data;
256
257
    av_frame_unref(s->frame);
258
}
259
260
static av_cold int decode_close(AVCodecContext *avctx)
261
{
262
    MidiVidContext *s = avctx->priv_data;
263
264
    av_frame_free(&s->frame);
265
    av_freep(&s->uncompressed);
266
    av_freep(&s->skip);
267
268
    return 0;
269
}
270
271
AVCodec ff_mvdv_decoder = {
272
    .name           = "mvdv",
273
    .long_name      = NULL_IF_CONFIG_SMALL("MidiVid VQ"),
274
    .type           = AVMEDIA_TYPE_VIDEO,
275
    .id             = AV_CODEC_ID_MVDV,
276
    .priv_data_size = sizeof(MidiVidContext),
277
    .init           = decode_init,
278
    .decode         = decode_frame,
279
    .flush          = decode_flush,
280
    .close          = decode_close,
281
    .capabilities   = AV_CODEC_CAP_DR1,
282
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
283
};