FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/fmvc.c
Date: 2024-04-20 14:10:07
Exec Total Coverage
Lines: 339 396 85.6%
Functions: 5 5 100.0%
Branches: 174 230 75.7%

Line Branch Exec Source
1 /*
2 * FM Screen Capture Codec decoder
3 *
4 * Copyright (c) 2017 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "libavutil/mem.h"
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "codec_internal.h"
30 #include "decode.h"
31
32 #define BLOCK_HEIGHT 112u
33 #define BLOCK_WIDTH 84u
34
35 typedef struct InterBlock {
36 int w, h;
37 int size;
38 int xor;
39 } InterBlock;
40
41 typedef struct FMVCContext {
42 GetByteContext gb;
43 PutByteContext pb;
44 uint8_t *buffer;
45 size_t buffer_size;
46 uint8_t *pbuffer;
47 size_t pbuffer_size;
48 ptrdiff_t stride;
49 int bpp;
50 int yb, xb;
51 InterBlock *blocks;
52 unsigned nb_blocks;
53 } FMVCContext;
54
55 72 static int decode_type2(GetByteContext *gb, PutByteContext *pb)
56 {
57 72 unsigned repeat = 0, first = 1, opcode = 0;
58 int i, len, pos;
59
60
1/2
✓ Branch 1 taken 4593 times.
✗ Branch 2 not taken.
4593 while (bytestream2_get_bytes_left(gb) > 0) {
61 GetByteContext gbc;
62
63
1/2
✓ Branch 1 taken 33101 times.
✗ Branch 2 not taken.
33101 while (bytestream2_get_bytes_left(gb) > 0) {
64
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 33029 times.
33101 if (first) {
65 72 first = 0;
66
1/2
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
72 if (bytestream2_peek_byte(gb) > 17) {
67 72 len = bytestream2_get_byte(gb) - 17;
68
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 7 times.
72 if (len < 4) {
69 do {
70 65 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
71 65 --len;
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 } while (len);
73 65 opcode = bytestream2_peek_byte(gb);
74 65 continue;
75 } else {
76 do {
77 103 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
78 103 --len;
79
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 7 times.
103 } while (len);
80 7 opcode = bytestream2_peek_byte(gb);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (opcode < 0x10) {
82 bytestream2_skip(gb, 1);
83 pos = - (opcode >> 2) - 4 * bytestream2_get_byte(gb) - 2049;
84
85 bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);
86 bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);
87
88 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
89 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
90 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
91 len = opcode & 3;
92 if (!len) {
93 repeat = 1;
94 } else {
95 do {
96 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
97 --len;
98 } while (len);
99 opcode = bytestream2_peek_byte(gb);
100 }
101 continue;
102 }
103 }
104 7 repeat = 0;
105 }
106 7 repeat = 1;
107 }
108
2/2
✓ Branch 0 taken 26608 times.
✓ Branch 1 taken 6428 times.
33036 if (repeat) {
109 26608 repeat = 0;
110 26608 opcode = bytestream2_peek_byte(gb);
111
2/2
✓ Branch 0 taken 586 times.
✓ Branch 1 taken 26022 times.
26608 if (opcode < 0x10) {
112 586 bytestream2_skip(gb, 1);
113
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 504 times.
586 if (!opcode) {
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
82 if (!bytestream2_peek_byte(gb)) {
115 do {
116 bytestream2_skip(gb, 1);
117 opcode += 255;
118 } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0);
119 }
120 82 opcode += bytestream2_get_byte(gb) + 15;
121 }
122 586 bytestream2_put_le32(pb, bytestream2_get_le32(gb));
123
2/2
✓ Branch 0 taken 3626 times.
✓ Branch 1 taken 586 times.
4212 for (i = opcode - 1; i > 0; --i)
124 3626 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
125 586 opcode = bytestream2_peek_byte(gb);
126
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 558 times.
586 if (opcode < 0x10) {
127 28 bytestream2_skip(gb, 1);
128 28 pos = - (opcode >> 2) - 4 * bytestream2_get_byte(gb) - 2049;
129
130 28 bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);
131 28 bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);
132
133 28 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
134 28 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
135 28 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
136 28 len = opcode & 3;
137
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 6 times.
28 if (!len) {
138 22 repeat = 1;
139 } else {
140 do {
141 17 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
142 17 --len;
143
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6 times.
17 } while (len);
144 6 opcode = bytestream2_peek_byte(gb);
145 }
146 28 continue;
147 }
148 }
149 }
150
151
2/2
✓ Branch 0 taken 6293 times.
✓ Branch 1 taken 26715 times.
33008 if (opcode >= 0x40) {
152 6293 bytestream2_skip(gb, 1);
153 6293 pos = - ((opcode >> 2) & 7) - 1 - 8 * bytestream2_get_byte(gb);
154 6293 len = (opcode >> 5) - 1;
155
156 6293 bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);
157 6293 bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);
158
159 6293 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
160 6293 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
161 do {
162 15169 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
163 15169 --len;
164
2/2
✓ Branch 0 taken 8876 times.
✓ Branch 1 taken 6293 times.
15169 } while (len);
165
166 6293 len = opcode & 3;
167
168
2/2
✓ Branch 0 taken 3620 times.
✓ Branch 1 taken 2673 times.
6293 if (!len) {
169 3620 repeat = 1;
170 } else {
171 do {
172 4856 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
173 4856 --len;
174
2/2
✓ Branch 0 taken 2183 times.
✓ Branch 1 taken 2673 times.
4856 } while (len);
175 2673 opcode = bytestream2_peek_byte(gb);
176 }
177 6293 continue;
178
2/2
✓ Branch 0 taken 4593 times.
✓ Branch 1 taken 22122 times.
26715 } else if (opcode < 0x20) {
179 4593 break;
180 }
181 22122 len = opcode & 0x1F;
182 22122 bytestream2_skip(gb, 1);
183
2/2
✓ Branch 0 taken 7443 times.
✓ Branch 1 taken 14679 times.
22122 if (!len) {
184
2/2
✓ Branch 1 taken 2416 times.
✓ Branch 2 taken 5027 times.
7443 if (!bytestream2_peek_byte(gb)) {
185 do {
186 9973 bytestream2_skip(gb, 1);
187 9973 len += 255;
188
3/4
✓ Branch 1 taken 7557 times.
✓ Branch 2 taken 2416 times.
✓ Branch 4 taken 7557 times.
✗ Branch 5 not taken.
9973 } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0);
189 }
190 7443 len += bytestream2_get_byte(gb) + 31;
191 }
192 22122 i = bytestream2_get_le16(gb);
193 22122 pos = - (i >> 2) - 1;
194
195 22122 bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);
196 22122 bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);
197
198
4/4
✓ Branch 0 taken 20962 times.
✓ Branch 1 taken 1160 times.
✓ Branch 4 taken 1890 times.
✓ Branch 5 taken 19072 times.
22122 if (len < 6 || bytestream2_tell_p(pb) - bytestream2_tell(&gbc) < 4) {
199 3050 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
200 3050 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
201 do {
202 1990991 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
203 1990991 --len;
204
2/2
✓ Branch 0 taken 1987941 times.
✓ Branch 1 taken 3050 times.
1990991 } while (len);
205 } else {
206 19072 bytestream2_put_le32(pb, bytestream2_get_le32(&gbc));
207
2/2
✓ Branch 0 taken 1681528 times.
✓ Branch 1 taken 19072 times.
1700600 for (len = len - 2; len; --len)
208 1681528 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
209 }
210 22122 len = i & 3;
211
2/2
✓ Branch 0 taken 19401 times.
✓ Branch 1 taken 2721 times.
22122 if (!len) {
212 19401 repeat = 1;
213 } else {
214 do {
215 5082 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
216 5082 --len;
217
2/2
✓ Branch 0 taken 2361 times.
✓ Branch 1 taken 2721 times.
5082 } while (len);
218 2721 opcode = bytestream2_peek_byte(gb);
219 }
220 }
221 4593 bytestream2_skip(gb, 1);
222
2/2
✓ Branch 0 taken 998 times.
✓ Branch 1 taken 3595 times.
4593 if (opcode < 0x10) {
223 998 pos = -(opcode >> 2) - 1 - 4 * bytestream2_get_byte(gb);
224
225 998 bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);
226 998 bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);
227
228 998 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
229 998 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
230 998 len = opcode & 3;
231
2/2
✓ Branch 0 taken 371 times.
✓ Branch 1 taken 627 times.
998 if (!len) {
232 371 repeat = 1;
233 } else {
234 do {
235 1101 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
236 1101 --len;
237
2/2
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 627 times.
1101 } while (len);
238 627 opcode = bytestream2_peek_byte(gb);
239 }
240 998 continue;
241 }
242 3595 len = opcode & 7;
243
2/2
✓ Branch 0 taken 3085 times.
✓ Branch 1 taken 510 times.
3595 if (!len) {
244
2/2
✓ Branch 1 taken 154 times.
✓ Branch 2 taken 2931 times.
3085 if (!bytestream2_peek_byte(gb)) {
245 do {
246 411 bytestream2_skip(gb, 1);
247 411 len += 255;
248
3/4
✓ Branch 1 taken 257 times.
✓ Branch 2 taken 154 times.
✓ Branch 4 taken 257 times.
✗ Branch 5 not taken.
411 } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0);
249 }
250 3085 len += bytestream2_get_byte(gb) + 7;
251 }
252 3595 i = bytestream2_get_le16(gb);
253 3595 pos = bytestream2_tell_p(pb) - 2048 * (opcode & 8);
254 3595 pos = pos - (i >> 2);
255
2/2
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 3523 times.
3595 if (pos == bytestream2_tell_p(pb))
256 72 break;
257
258 3523 pos = pos - 0x4000;
259 3523 bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);
260 3523 bytestream2_seek(&gbc, pos, SEEK_SET);
261
262
3/4
✓ Branch 0 taken 3307 times.
✓ Branch 1 taken 216 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3307 times.
3523 if (len < 6 || bytestream2_tell_p(pb) - bytestream2_tell(&gbc) < 4) {
263 216 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
264 216 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
265 do {
266 667 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
267 667 --len;
268
2/2
✓ Branch 0 taken 451 times.
✓ Branch 1 taken 216 times.
667 } while (len);
269 } else {
270 3307 bytestream2_put_le32(pb, bytestream2_get_le32(&gbc));
271
2/2
✓ Branch 0 taken 215954 times.
✓ Branch 1 taken 3307 times.
219261 for (len = len - 2; len; --len)
272 215954 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
273 }
274
275 3523 len = i & 3;
276
2/2
✓ Branch 0 taken 3187 times.
✓ Branch 1 taken 336 times.
3523 if (!len) {
277 3187 repeat = 1;
278 } else {
279 do {
280 551 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
281 551 --len;
282
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 336 times.
551 } while (len);
283 336 opcode = bytestream2_peek_byte(gb);
284 }
285 }
286
287 72 return 0;
288 }
289
290 42 static int decode_type1(GetByteContext *gb, PutByteContext *pb)
291 {
292 42 unsigned opcode = 0, len;
293 42 int high = 0;
294 int i, pos;
295
296
1/2
✓ Branch 1 taken 10429 times.
✗ Branch 2 not taken.
10429 while (bytestream2_get_bytes_left(gb) > 0) {
297 GetByteContext gbc;
298
299
1/2
✓ Branch 1 taken 21442 times.
✗ Branch 2 not taken.
21442 while (bytestream2_get_bytes_left(gb) > 0) {
300
1/2
✓ Branch 1 taken 21442 times.
✗ Branch 2 not taken.
21442 while (bytestream2_get_bytes_left(gb) > 0) {
301 21442 opcode = bytestream2_get_byte(gb);
302 21442 high = opcode >= 0x20;
303
2/2
✓ Branch 0 taken 18221 times.
✓ Branch 1 taken 3221 times.
21442 if (high)
304 18221 break;
305
2/2
✓ Branch 0 taken 3220 times.
✓ Branch 1 taken 1 times.
3221 if (opcode)
306 3220 break;
307 1 opcode = bytestream2_get_byte(gb);
308
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (opcode < 0xF8) {
309 1 opcode += 32;
310 1 break;
311 }
312 i = opcode - 0xF8;
313 if (i) {
314 len = 256;
315 do {
316 len *= 2;
317 --i;
318 } while (i);
319 } else {
320 len = 280;
321 }
322 do {
323 bytestream2_put_le32(pb, bytestream2_get_le32(gb));
324 bytestream2_put_le32(pb, bytestream2_get_le32(gb));
325 len -= 8;
326 } while (len && bytestream2_get_bytes_left(gb) > 0);
327 }
328
329
2/2
✓ Branch 0 taken 3221 times.
✓ Branch 1 taken 18221 times.
21442 if (!high) {
330 do {
331 11005 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
332 11005 --opcode;
333
3/4
✓ Branch 0 taken 7784 times.
✓ Branch 1 taken 3221 times.
✓ Branch 3 taken 7784 times.
✗ Branch 4 not taken.
11005 } while (opcode && bytestream2_get_bytes_left(gb) > 0);
334
335
1/2
✓ Branch 1 taken 3223 times.
✗ Branch 2 not taken.
3223 while (bytestream2_get_bytes_left(gb) > 0) {
336 GetByteContext gbc;
337
338 3223 opcode = bytestream2_get_byte(gb);
339
2/2
✓ Branch 0 taken 3221 times.
✓ Branch 1 taken 2 times.
3223 if (opcode >= 0x20)
340 3221 break;
341 2 bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);
342
343 2 pos = -(opcode | 32 * bytestream2_get_byte(gb)) - 1;
344 2 bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);
345 2 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
346 2 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
347 2 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
348 2 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
349 }
350 }
351 21442 high = 0;
352
2/2
✓ Branch 0 taken 10429 times.
✓ Branch 1 taken 11013 times.
21442 if (opcode < 0x40)
353 10429 break;
354 11013 bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);
355 11013 pos = (-((opcode & 0x1F) | 32 * bytestream2_get_byte(gb)) - 1);
356 11013 bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);
357 11013 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
358 11013 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
359 11013 len = (opcode >> 5) - 1;
360 do {
361 35434 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
362 35434 --len;
363
3/4
✓ Branch 0 taken 24421 times.
✓ Branch 1 taken 11013 times.
✓ Branch 3 taken 24421 times.
✗ Branch 4 not taken.
35434 } while (len && bytestream2_get_bytes_left(&gbc) > 0);
364 }
365 10429 len = opcode & 0x1F;
366
2/2
✓ Branch 0 taken 3296 times.
✓ Branch 1 taken 7133 times.
10429 if (!len) {
367
2/2
✓ Branch 1 taken 1486 times.
✓ Branch 2 taken 1810 times.
3296 if (!bytestream2_peek_byte(gb)) {
368 do {
369 7405 bytestream2_skip(gb, 1);
370 7405 len += 255;
371
3/4
✓ Branch 1 taken 5919 times.
✓ Branch 2 taken 1486 times.
✓ Branch 4 taken 5919 times.
✗ Branch 5 not taken.
7405 } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0);
372 }
373 3296 len += bytestream2_get_byte(gb) + 31;
374 }
375 10429 pos = -bytestream2_get_byte(gb);
376 10429 bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);
377 10429 bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos - (bytestream2_get_byte(gb) << 8), SEEK_SET);
378
2/2
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 10387 times.
10429 if (bytestream2_tell_p(pb) == bytestream2_tell(&gbc))
379 42 break;
380
4/4
✓ Branch 0 taken 9497 times.
✓ Branch 1 taken 890 times.
✓ Branch 4 taken 178 times.
✓ Branch 5 taken 9319 times.
10387 if (len < 5 || bytestream2_tell_p(pb) - bytestream2_tell(&gbc) < 4) {
381 1068 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
382 1068 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
383 1068 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
384 } else {
385 9319 bytestream2_put_le32(pb, bytestream2_get_le32(&gbc));
386 9319 len--;
387 }
388 do {
389 2411287 bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));
390 2411287 len--;
391
3/4
✓ Branch 0 taken 2400900 times.
✓ Branch 1 taken 10387 times.
✓ Branch 3 taken 2400900 times.
✗ Branch 4 not taken.
2411287 } while (len && bytestream2_get_bytes_left(&gbc) > 0);
392 }
393
394 42 return 0;
395 }
396
397 34 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
398 int *got_frame, AVPacket *avpkt)
399 {
400 34 FMVCContext *s = avctx->priv_data;
401 34 GetByteContext *gb = &s->gb;
402 34 PutByteContext *pb = &s->pb;
403 int ret, y, x;
404 int key_frame;
405
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (avpkt->size < 8)
407 return AVERROR_INVALIDDATA;
408
409 34 bytestream2_init(gb, avpkt->data, avpkt->size);
410 34 bytestream2_skip(gb, 2);
411
412 34 key_frame = !!bytestream2_get_le16(gb);
413
414
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 32 times.
34 if (key_frame) {
415 const uint8_t *src;
416 unsigned type, size;
417 uint8_t *dst;
418
419 2 type = bytestream2_get_le16(gb);
420 2 size = bytestream2_get_le16(gb);
421
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (size > bytestream2_get_bytes_left(gb))
422 return AVERROR_INVALIDDATA;
423
424 2 bytestream2_init_writer(pb, s->buffer, s->buffer_size);
425
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (type == 1) {
426 1 decode_type1(gb, pb);
427
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (type == 2){
428 1 decode_type2(gb, pb);
429 } else {
430 avpriv_report_missing_feature(avctx, "Compression type %d", type);
431 return AVERROR_PATCHWELCOME;
432 }
433
434
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
435 return ret;
436
437 2 frame->flags |= AV_FRAME_FLAG_KEY;
438 2 frame->pict_type = AV_PICTURE_TYPE_I;
439
440 2 src = s->buffer;
441 2 dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
442
2/2
✓ Branch 0 taken 958 times.
✓ Branch 1 taken 2 times.
960 for (y = 0; y < avctx->height; y++) {
443 958 memcpy(dst, src, avctx->width * s->bpp);
444 958 dst -= frame->linesize[0];
445 958 src += s->stride * 4;
446
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 958 times.
958 if (bytestream2_tell_p(pb) < y*s->stride * 4)
447 break;
448 }
449 } else {
450 unsigned block, nb_blocks;
451 int type, k, l;
452 uint8_t *ssrc, *ddst;
453 const uint32_t *src;
454 uint32_t *dst;
455
456
2/2
✓ Branch 0 taken 1035 times.
✓ Branch 1 taken 32 times.
1067 for (block = 0; block < s->nb_blocks; block++)
457 1035 s->blocks[block].xor = 0;
458
459 32 nb_blocks = bytestream2_get_le16(gb);
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (nb_blocks > s->nb_blocks)
461 return AVERROR_INVALIDDATA;
462
463 32 bytestream2_init_writer(pb, s->pbuffer, s->pbuffer_size);
464
465 32 type = bytestream2_get_le16(gb);
466
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 32 times.
144 for (block = 0; block < nb_blocks; block++) {
467 unsigned size, offset;
468 112 int start = 0;
469
470 112 offset = bytestream2_get_le16(gb);
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (offset >= s->nb_blocks)
472 return AVERROR_INVALIDDATA;
473
474 112 size = bytestream2_get_le16(gb);
475
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
112 if (size > bytestream2_get_bytes_left(gb))
476 return AVERROR_INVALIDDATA;
477
478 112 start = bytestream2_tell_p(pb);
479
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 71 times.
112 if (type == 1) {
480 41 decode_type1(gb, pb);
481
1/2
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
71 } else if (type == 2){
482 71 decode_type2(gb, pb);
483 } else {
484 avpriv_report_missing_feature(avctx, "Compression type %d", type);
485 return AVERROR_PATCHWELCOME;
486 }
487
488
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
112 if (s->blocks[offset].size * 4 != bytestream2_tell_p(pb) - start)
489 return AVERROR_INVALIDDATA;
490
491 112 s->blocks[offset].xor = 1;
492 }
493
494 32 src = (const uint32_t *)s->pbuffer;
495 32 dst = (uint32_t *)s->buffer;
496
497
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 32 times.
182 for (block = 0, y = 0; y < s->yb; y++) {
498 150 int block_h = s->blocks[block].h;
499 150 uint32_t *rect = dst;
500
501
2/2
✓ Branch 0 taken 1035 times.
✓ Branch 1 taken 150 times.
1185 for (x = 0; x < s->xb; x++) {
502 1035 int block_w = s->blocks[block].w;
503 1035 uint32_t *row = dst;
504
505 1035 block_h = s->blocks[block].h;
506
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 923 times.
1035 if (s->blocks[block].xor) {
507
2/2
✓ Branch 0 taken 13556 times.
✓ Branch 1 taken 112 times.
13668 for (k = 0; k < block_h; k++) {
508 13556 uint32_t *column = dst;
509
2/2
✓ Branch 0 taken 1116240 times.
✓ Branch 1 taken 13556 times.
1129796 for (l = 0; l < block_w; l++)
510 1116240 *dst++ ^= *src++;
511 13556 dst = &column[s->stride];
512 }
513 }
514 1035 dst = &row[block_w];
515 1035 ++block;
516 }
517 150 dst = &rect[block_h * s->stride];
518 }
519
520
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
521 return ret;
522
523 32 frame->flags &= ~AV_FRAME_FLAG_KEY;
524 32 frame->pict_type = AV_PICTURE_TYPE_P;
525
526 32 ssrc = s->buffer;
527 32 ddst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
528
2/2
✓ Branch 0 taken 17462 times.
✓ Branch 1 taken 32 times.
17494 for (y = 0; y < avctx->height; y++) {
529 17462 memcpy(ddst, ssrc, avctx->width * s->bpp);
530 17462 ddst -= frame->linesize[0];
531 17462 ssrc += s->stride * 4;
532 }
533 }
534
535 34 *got_frame = 1;
536
537 34 return avpkt->size;
538 }
539
540 4 static av_cold int decode_init(AVCodecContext *avctx)
541 {
542 4 FMVCContext *s = avctx->priv_data;
543 4 int i, j, m, block = 0, h = BLOCK_HEIGHT, w = BLOCK_WIDTH;
544
545
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 switch (avctx->bits_per_coded_sample) {
546 case 16:
547 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
548 break;
549 4 case 24:
550 4 avctx->pix_fmt = AV_PIX_FMT_BGR24;
551 4 break;
552 case 32:
553 avctx->pix_fmt = AV_PIX_FMT_BGRA;
554 break;
555 default:
556 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n",
557 avctx->bits_per_coded_sample);
558 return AVERROR_INVALIDDATA;
559 }
560
561 4 s->stride = (avctx->width * avctx->bits_per_coded_sample + 31) / 32;
562 4 s->xb = s->stride / BLOCK_WIDTH;
563 4 m = s->stride % BLOCK_WIDTH;
564
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (m) {
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (m < 37) {
566 w = m + BLOCK_WIDTH;
567 } else {
568 4 w = m;
569 4 s->xb++;
570 }
571 }
572
573 4 s->yb = avctx->height / BLOCK_HEIGHT;
574 4 m = avctx->height % BLOCK_HEIGHT;
575
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (m) {
576
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (m < 49) {
577 4 h = m + BLOCK_HEIGHT;
578 } else {
579 h = m;
580 s->yb++;
581 }
582 }
583
584 4 s->nb_blocks = s->xb * s->yb;
585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!s->nb_blocks)
586 return AVERROR_INVALIDDATA;
587 4 s->blocks = av_calloc(s->nb_blocks, sizeof(*s->blocks));
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!s->blocks)
589 return AVERROR(ENOMEM);
590
591
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (i = 0; i < s->yb; i++) {
592
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 16 times.
122 for (j = 0; j < s->xb; j++) {
593
4/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 4 times.
106 if (i != (s->yb - 1) || j != (s->xb - 1)) {
594
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 80 times.
102 if (i == s->yb - 1) {
595 22 s->blocks[block].w = BLOCK_WIDTH;
596 22 s->blocks[block].h = h;
597 22 s->blocks[block].size = BLOCK_WIDTH * h;
598
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 68 times.
80 } else if (j == s->xb - 1) {
599 12 s->blocks[block].w = w;
600 12 s->blocks[block].h = BLOCK_HEIGHT;
601 12 s->blocks[block].size = BLOCK_HEIGHT * w;
602 } else {
603 68 s->blocks[block].w = BLOCK_WIDTH;
604 68 s->blocks[block].h = BLOCK_HEIGHT;
605 68 s->blocks[block].size = BLOCK_WIDTH * BLOCK_HEIGHT;
606 }
607 } else {
608 4 s->blocks[block].w = w;
609 4 s->blocks[block].h = h;
610 4 s->blocks[block].size = w * h;
611 }
612 106 block++;
613 }
614 }
615
616 4 s->bpp = avctx->bits_per_coded_sample >> 3;
617 4 s->buffer_size = avctx->width * avctx->height * 4;
618 4 s->pbuffer_size = avctx->width * avctx->height * 4;
619 4 s->buffer = av_mallocz(s->buffer_size);
620 4 s->pbuffer = av_mallocz(s->pbuffer_size);
621
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (!s->buffer || !s->pbuffer)
622 return AVERROR(ENOMEM);
623
624 4 return 0;
625 }
626
627 4 static av_cold int decode_close(AVCodecContext *avctx)
628 {
629 4 FMVCContext *s = avctx->priv_data;
630
631 4 av_freep(&s->buffer);
632 4 av_freep(&s->pbuffer);
633 4 av_freep(&s->blocks);
634
635 4 return 0;
636 }
637
638 const FFCodec ff_fmvc_decoder = {
639 .p.name = "fmvc",
640 CODEC_LONG_NAME("FM Screen Capture Codec"),
641 .p.type = AVMEDIA_TYPE_VIDEO,
642 .p.id = AV_CODEC_ID_FMVC,
643 .priv_data_size = sizeof(FMVCContext),
644 .init = decode_init,
645 .close = decode_close,
646 FF_CODEC_DECODE_CB(decode_frame),
647 .p.capabilities = AV_CODEC_CAP_DR1,
648 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
649 };
650