FFmpeg coverage


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