aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/midivid.c
blob: 38465c5fbcaade666e7e26e9dca4fd8d24e29648 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * MidiVid decoder
 * Copyright (c) 2019 Paul B Mahol
 *
 * 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
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "libavutil/imgutils.h"
#include "libavutil/internal.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"

#define BITSTREAM_READER_LE
#include "avcodec.h"
#include "get_bits.h"
#include "bytestream.h"
#include "internal.h"

typedef struct MidiVidContext {
    GetByteContext gb;

    uint8_t *uncompressed;
    unsigned int uncompressed_size;
    uint8_t *skip;

    AVFrame *frame;
} MidiVidContext;

static int decode_mvdv(MidiVidContext *s, AVCodecContext *avctx, AVFrame *frame)
{
    GetByteContext *gb = &s->gb;
    GetBitContext mask;
    GetByteContext idx9;
    uint16_t nb_vectors, intra_flag;
    const uint8_t *vec;
    const uint8_t *mask_start;
    uint8_t *skip;
    uint32_t mask_size;
    int idx9bits = 0;
    int idx9val = 0;
    uint32_t nb_blocks;

    nb_vectors = bytestream2_get_le16(gb);
    intra_flag = bytestream2_get_le16(gb);
    if (intra_flag) {
        nb_blocks = (avctx->width / 2) * (avctx->height / 2);
    } else {
        int skip_linesize;

        nb_blocks = bytestream2_get_le32(gb);
        skip_linesize = avctx->width >> 1;
        mask_start = gb->buffer_start + bytestream2_tell(gb);
        mask_size = (avctx->width >> 5) * (avctx->height >> 2);

        if (bytestream2_get_bytes_left(gb) < mask_size)
            return AVERROR_INVALIDDATA;

        init_get_bits8(&mask, mask_start, mask_size);
        bytestream2_skip(gb, mask_size);
        skip = s->skip;

        for (int y = 0; y < avctx->height >> 2; y++) {
            for (int x = 0; x < avctx->width >> 2; x++) {
                int flag = !get_bits1(&mask);

                skip[(y*2)  *skip_linesize + x*2  ] = flag;
                skip[(y*2)  *skip_linesize + x*2+1] = flag;
                skip[(y*2+1)*skip_linesize + x*2  ] = flag;
                skip[(y*2+1)*skip_linesize + x*2+1] = flag;
            }
        }
    }

    vec = gb->buffer_start + bytestream2_tell(gb);
    if (bytestream2_get_bytes_left(gb) < nb_vectors * 12)
        return AVERROR_INVALIDDATA;
    bytestream2_skip(gb, nb_vectors * 12);
    if (nb_vectors > 256) {
        if (bytestream2_get_bytes_left(gb) < (nb_blocks + 7) / 8)
            return AVERROR_INVALIDDATA;
        bytestream2_init(&idx9, gb->buffer_start + bytestream2_tell(gb), (nb_blocks + 7) / 8);
        bytestream2_skip(gb, (nb_blocks + 7) / 8);
    }

    skip = s->skip;

    for (int y = avctx->height - 2; y >= 0; y -= 2) {
        uint8_t *dsty = frame->data[0] + y * frame->linesize[0];
        uint8_t *dstu = frame->data[1] + y * frame->linesize[1];
        uint8_t *dstv = frame->data[2] + y * frame->linesize[2];

        for (int x = 0; x < avctx->width; x += 2) {
            int idx;

            if (!intra_flag && *skip++)
                continue;
            if (bytestream2_get_bytes_left(gb) <= 0)
                return AVERROR_INVALIDDATA;
            if (nb_vectors <= 256) {
                idx = bytestream2_get_byte(gb);
            } else {
                if (idx9bits == 0) {
                    idx9val = bytestream2_get_byte(&idx9);
                    idx9bits = 8;
                }
                idx9bits--;
                idx = bytestream2_get_byte(gb) | (((idx9val >> (7 - idx9bits)) & 1) << 8);
            }

            dsty[x  +frame->linesize[0]] = vec[idx * 12 + 0];
            dsty[x+1+frame->linesize[0]] = vec[idx * 12 + 3];
            dsty[x]                      = vec[idx * 12 + 6];
            dsty[x+1]                    = vec[idx * 12 + 9];

            dstu[x  +frame->linesize[1]] = vec[idx * 12 + 1];
            dstu[x+1+frame->linesize[1]] = vec[idx * 12 + 4];
            dstu[x]                      = vec[idx * 12 + 7];
            dstu[x+1]                    = vec[idx * 12 +10];

            dstv[x  +frame->linesize[2]] = vec[idx * 12 + 2];
            dstv[x+1+frame->linesize[2]] = vec[idx * 12 + 5];
            dstv[x]                      = vec[idx * 12 + 8];
            dstv[x+1]                    = vec[idx * 12 +11];
        }
    }

    return intra_flag;
}

static ptrdiff_t lzss_uncompress(MidiVidContext *s, GetByteContext *gb, uint8_t *dst, unsigned int size)
{
    uint8_t *dst_start = dst;
    uint8_t *dst_end = dst + size;

    for (;bytestream2_get_bytes_left(gb) >= 3;) {
        int op = bytestream2_get_le16(gb);

        for (int i = 0; i < 16; i++) {
            if (op & 1) {
                int s0 = bytestream2_get_byte(gb);
                int s1 = bytestream2_get_byte(gb);
                int offset = ((s0 & 0xF0) << 4) | s1;
                int length = (s0 & 0xF) + 3;

                if (dst + length > dst_end ||
                    dst - offset < dst_start)
                    return AVERROR_INVALIDDATA;
                if (offset > 0) {
                    for (int j = 0; j < length; j++) {
                        dst[j] = dst[j - offset];
                    }
                }
                dst += length;
            } else {
                if (dst >= dst_end)
                    return AVERROR_INVALIDDATA;
                *dst++ = bytestream2_get_byte(gb);
            }
            op >>= 1;
        }
    }

    return dst - dst_start;
}

static int decode_frame(AVCodecContext *avctx, void *data,
                        int *got_frame, AVPacket *avpkt)
{
    MidiVidContext *s = avctx->priv_data;
    GetByteContext *gb = &s->gb;
    AVFrame *frame = s->frame;
    int ret, key, uncompressed;

    if (avpkt->size <= 13)
        return AVERROR_INVALIDDATA;

    bytestream2_init(gb, avpkt->data, avpkt->size);
    bytestream2_skip(gb, 8);
    uncompressed = bytestream2_get_le32(gb);

    if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
        return ret;

    if (uncompressed) {
        ret = decode_mvdv(s, avctx, frame);
    } else {
        av_fast_padded_malloc(&s->uncompressed, &s->uncompressed_size, 16LL * (avpkt->size - 12));
        if (!s->uncompressed)
            return AVERROR(ENOMEM);

        ret = lzss_uncompress(s, gb, s->uncompressed, s->uncompressed_size);
        if (ret < 0)
            return ret;
        bytestream2_init(gb, s->uncompressed, ret);
        ret = decode_mvdv(s, avctx, frame);
    }

    if (ret < 0)
        return ret;
    key = ret;

    if ((ret = av_frame_ref(data, s->frame)) < 0)
        return ret;

    frame->pict_type = key ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
    frame->key_frame = key;
    *got_frame = 1;

    return avpkt->size;
}

static av_cold int decode_init(AVCodecContext *avctx)
{
    MidiVidContext *s = avctx->priv_data;
    int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);

    if (ret < 0) {
        av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
               avctx->width, avctx->height);
        return ret;
    }

    avctx->pix_fmt = AV_PIX_FMT_YUV444P;

    s->frame = av_frame_alloc();
    if (!s->frame)
        return AVERROR(ENOMEM);
    s->skip = av_calloc(avctx->width >> 1, avctx->height >> 1);
    if (!s->skip)
        return AVERROR(ENOMEM);

    return 0;
}

static void decode_flush(AVCodecContext *avctx)
{
    MidiVidContext *s = avctx->priv_data;

    av_frame_unref(s->frame);
}

static av_cold int decode_close(AVCodecContext *avctx)
{
    MidiVidContext *s = avctx->priv_data;

    av_frame_free(&s->frame);
    av_freep(&s->uncompressed);
    av_freep(&s->skip);

    return 0;
}

AVCodec ff_mvdv_decoder = {
    .name           = "mvdv",
    .long_name      = NULL_IF_CONFIG_SMALL("MidiVid VQ"),
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = AV_CODEC_ID_MVDV,
    .priv_data_size = sizeof(MidiVidContext),
    .init           = decode_init,
    .decode         = decode_frame,
    .flush          = decode_flush,
    .close          = decode_close,
    .capabilities   = AV_CODEC_CAP_DR1,
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
};