../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
255 / 358
71.2%
Branches:
125 / 197
63.5%
Functions:
9 / 10
90.0%

libavcodec/vmnc.c
Line Branch Exec Source
1 /*
2 * VMware Screen Codec (VMnc) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
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 * VMware Screen Codec (VMnc) decoder
25 * As Alex Beregszaszi discovered, this is effectively RFB data dump
26 */
27
28 #include "libavutil/attributes.h"
29 #include "libavutil/common.h"
30 #include "libavutil/mem.h"
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 #include "bytestream.h"
35
36 enum EncTypes {
37 MAGIC_WMVd = 0x574D5664,
38 MAGIC_WMVe,
39 MAGIC_WMVf,
40 MAGIC_WMVg,
41 MAGIC_WMVh,
42 MAGIC_WMVi,
43 MAGIC_WMVj
44 };
45
46 enum HexTile_Flags {
47 HT_RAW = 1, // tile is raw
48 HT_BKG = 2, // background color is present
49 HT_FG = 4, // foreground color is present
50 HT_SUB = 8, // subrects are present
51 HT_CLR = 16 // each subrect has own color
52 };
53
54 /*
55 * Decoder context
56 */
57 typedef struct VmncContext {
58 AVCodecContext *avctx;
59 AVFrame *pic;
60
61 int bpp;
62 int bpp2;
63 int bigendian;
64 uint8_t pal[768];
65 int width, height;
66 GetByteContext gb;
67
68 /* cursor data */
69 int cur_w, cur_h;
70 int cur_x, cur_y;
71 int cur_hx, cur_hy;
72 uint8_t *curbits, *curmask;
73 uint8_t *screendta;
74 int cursor_painted;
75 } VmncContext;
76
77 /* read pixel value from stream */
78 401741 static av_always_inline int vmnc_get_pixel(GetByteContext *gb, int bpp, int be)
79 {
80
2/6
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 58268 times.
✗ Branch 2 → 7 not taken.
✓ Branch 2 → 9 taken 343473 times.
✗ Branch 2 → 11 not taken.
✗ Branch 2 → 13 not taken.
401741 switch (bpp * 2 + be) {
81 ✗ case 2:
82 case 3:
83 ✗ return bytestream2_get_byte(gb);
84 58268 case 4:
85 58268 return bytestream2_get_le16(gb);
86 ✗ case 5:
87 ✗ return bytestream2_get_be16(gb);
88 343473 case 8:
89 343473 return bytestream2_get_le32(gb);
90 ✗ case 9:
91 ✗ return bytestream2_get_be32(gb);
92 ✗ default: return 0;
93 }
94 }
95
96 16 static void load_cursor(VmncContext *c)
97 {
98 int i, j, p;
99 16 const int bpp = c->bpp2;
100 16 uint8_t *dst8 = c->curbits;
101 16 uint16_t *dst16 = (uint16_t *)c->curbits;
102 16 uint32_t *dst32 = (uint32_t *)c->curbits;
103
104
2/2
✓ Branch 14 → 3 taken 416 times.
✓ Branch 14 → 15 taken 16 times.
432 for (j = 0; j < c->cur_h; j++) {
105
2/2
✓ Branch 12 → 4 taken 13312 times.
✓ Branch 12 → 13 taken 416 times.
13728 for (i = 0; i < c->cur_w; i++) {
106 13312 p = vmnc_get_pixel(&c->gb, bpp, c->bigendian);
107
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 13312 times.
13312 if (bpp == 1)
108 ✗ *dst8++ = p;
109
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 13312 times.
13312 if (bpp == 2)
110 ✗ *dst16++ = p;
111
1/2
✓ Branch 9 → 10 taken 13312 times.
✗ Branch 9 → 11 not taken.
13312 if (bpp == 4)
112 13312 *dst32++ = p;
113 }
114 }
115 16 dst8 = c->curmask;
116 16 dst16 = (uint16_t*)c->curmask;
117 16 dst32 = (uint32_t*)c->curmask;
118
2/2
✓ Branch 27 → 16 taken 416 times.
✓ Branch 27 → 28 taken 16 times.
432 for (j = 0; j < c->cur_h; j++) {
119
2/2
✓ Branch 25 → 17 taken 13312 times.
✓ Branch 25 → 26 taken 416 times.
13728 for (i = 0; i < c->cur_w; i++) {
120 13312 p = vmnc_get_pixel(&c->gb, bpp, c->bigendian);
121
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 13312 times.
13312 if (bpp == 1)
122 ✗ *dst8++ = p;
123
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 13312 times.
13312 if (bpp == 2)
124 ✗ *dst16++ = p;
125
1/2
✓ Branch 22 → 23 taken 13312 times.
✗ Branch 22 → 24 not taken.
13312 if (bpp == 4)
126 13312 *dst32++ = p;
127 }
128 }
129 16 }
130
131 49 static void put_cursor(uint8_t *dst, int stride, VmncContext *c, int dx, int dy)
132 {
133 int i, j;
134 int w, h, x, y;
135 49 w = c->cur_w;
136
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 49 times.
49 if (c->width < c->cur_x + c->cur_w)
137 ✗ w = c->width - c->cur_x;
138 49 h = c->cur_h;
139
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 49 times.
49 if (c->height < c->cur_y + c->cur_h)
140 ✗ h = c->height - c->cur_y;
141 49 x = c->cur_x;
142 49 y = c->cur_y;
143
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 49 times.
49 if (x < 0) {
144 ✗ w += x;
145 ✗ x = 0;
146 }
147
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 49 times.
49 if (y < 0) {
148 ✗ h += y;
149 ✗ y = 0;
150 }
151
152
2/4
✓ Branch 10 → 11 taken 49 times.
✗ Branch 10 → 12 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 49 times.
49 if ((w < 1) || (h < 1))
153 ✗ return;
154 49 dst += x * c->bpp2 + y * stride;
155
156
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 20 taken 49 times.
49 if (c->bpp2 == 1) {
157 ✗ uint8_t *cd = c->curbits, *msk = c->curmask;
158
0/2
✗ Branch 19 → 15 not taken.
✗ Branch 19 → 34 not taken.
✗ for (j = 0; j < h; j++) {
159
0/2
✗ Branch 17 → 16 not taken.
✗ Branch 17 → 18 not taken.
✗ for (i = 0; i < w; i++)
160 ✗ dst[i] = (dst[i] & cd[i]) ^ msk[i];
161 ✗ msk += c->cur_w;
162 ✗ cd += c->cur_w;
163 ✗ dst += stride;
164 }
165
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 27 taken 49 times.
49 } else if (c->bpp2 == 2) {
166 ✗ uint16_t *cd = (uint16_t*)c->curbits, *msk = (uint16_t*)c->curmask;
167 uint16_t *dst2;
168
0/2
✗ Branch 26 → 22 not taken.
✗ Branch 26 → 34 not taken.
✗ for (j = 0; j < h; j++) {
169 ✗ dst2 = (uint16_t*)dst;
170
0/2
✗ Branch 24 → 23 not taken.
✗ Branch 24 → 25 not taken.
✗ for (i = 0; i < w; i++)
171 ✗ dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
172 ✗ msk += c->cur_w;
173 ✗ cd += c->cur_w;
174 ✗ dst += stride;
175 }
176
1/2
✓ Branch 27 → 28 taken 49 times.
✗ Branch 27 → 34 not taken.
49 } else if (c->bpp2 == 4) {
177 49 uint32_t *cd = (uint32_t*)c->curbits, *msk = (uint32_t*)c->curmask;
178 uint32_t *dst2;
179
2/2
✓ Branch 33 → 29 taken 1568 times.
✓ Branch 33 → 34 taken 49 times.
1617 for (j = 0; j < h; j++) {
180 1568 dst2 = (uint32_t*)dst;
181
2/2
✓ Branch 31 → 30 taken 50176 times.
✓ Branch 31 → 32 taken 1568 times.
51744 for (i = 0; i < w; i++)
182 50176 dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
183 1568 msk += c->cur_w;
184 1568 cd += c->cur_w;
185 1568 dst += stride;
186 }
187 }
188 }
189
190 /* fill rectangle with given color */
191 443805 static av_always_inline void paint_rect(uint8_t *dst, int dx, int dy,
192 int w, int h, int color,
193 int bpp, int stride)
194 {
195 int i, j;
196 443805 dst += dx * bpp + dy * stride;
197
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 443805 times.
443805 if (bpp == 1) {
198
0/2
✗ Branch 5 → 4 not taken.
✗ Branch 5 → 20 not taken.
✗ for (j = 0; j < h; j++) {
199 ✗ memset(dst, color, w);
200 ✗ dst += stride;
201 }
202
2/2
✓ Branch 6 → 7 taken 91696 times.
✓ Branch 6 → 13 taken 352109 times.
443805 } else if (bpp == 2) {
203 uint16_t *dst2;
204
2/2
✓ Branch 12 → 8 taken 687588 times.
✓ Branch 12 → 20 taken 91696 times.
779284 for (j = 0; j < h; j++) {
205 687588 dst2 = (uint16_t*)dst;
206
2/2
✓ Branch 10 → 9 taken 8908537 times.
✓ Branch 10 → 11 taken 687588 times.
9596125 for (i = 0; i < w; i++)
207 8908537 *dst2++ = color;
208 687588 dst += stride;
209 }
210
1/2
✓ Branch 13 → 14 taken 352109 times.
✗ Branch 13 → 20 not taken.
352109 } else if (bpp == 4) {
211 uint32_t *dst2;
212
2/2
✓ Branch 19 → 15 taken 1320455 times.
✓ Branch 19 → 20 taken 352109 times.
1672564 for (j = 0; j < h; j++) {
213 1320455 dst2 = (uint32_t*)dst;
214
2/2
✓ Branch 17 → 16 taken 12708159 times.
✓ Branch 17 → 18 taken 1320455 times.
14028614 for (i = 0; i < w; i++)
215 12708159 dst2[i] = color;
216 1320455 dst += stride;
217 }
218 }
219 443805 }
220
221 50 static av_always_inline void paint_raw(uint8_t *dst, int w, int h,
222 GetByteContext *gb, int bpp,
223 int be, int stride)
224 {
225 int i, j, p;
226
2/2
✓ Branch 12 → 3 taken 417 times.
✓ Branch 12 → 13 taken 50 times.
467 for (j = 0; j < h; j++) {
227
2/2
✓ Branch 10 → 4 taken 6204 times.
✓ Branch 10 → 11 taken 417 times.
6621 for (i = 0; i < w; i++) {
228 6204 p = vmnc_get_pixel(gb, bpp, be);
229
1/4
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✓ Branch 5 → 8 taken 6204 times.
✗ Branch 5 → 9 not taken.
6204 switch (bpp) {
230 ✗ case 1:
231 ✗ dst[i] = p;
232 ✗ break;
233 ✗ case 2:
234 ✗ ((uint16_t*)dst)[i] = p;
235 ✗ break;
236 6204 case 4:
237 6204 ((uint32_t*)dst)[i] = p;
238 6204 break;
239 }
240 }
241 417 dst += stride;
242 }
243 50 }
244
245 463 static int decode_hextile(VmncContext *c, uint8_t* dst, GetByteContext *gb,
246 int w, int h, int stride)
247 {
248 int i, j, k;
249 463 int bg = 0, fg = 0, rects, color, flags, xy, wh;
250 463 const int bpp = c->bpp2;
251 uint8_t *dst2;
252 463 int bw = 16, bh = 16;
253
254
2/2
✓ Branch 46 → 3 taken 2247 times.
✓ Branch 46 → 47 taken 462 times.
2709 for (j = 0; j < h; j += 16) {
255 2247 dst2 = dst;
256 2247 bw = 16;
257
2/2
✓ Branch 3 → 4 taken 420 times.
✓ Branch 3 → 5 taken 1827 times.
2247 if (j + 16 > h)
258 420 bh = h - j;
259
2/2
✓ Branch 44 → 6 taken 76192 times.
✓ Branch 44 → 45 taken 2246 times.
78438 for (i = 0; i < w; i += 16, dst2 += 16 * bpp) {
260
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 76192 times.
76192 if (bytestream2_get_bytes_left(gb) <= 0) {
261 ✗ av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
262 ✗ return AVERROR_INVALIDDATA;
263 }
264
2/2
✓ Branch 10 → 11 taken 2103 times.
✓ Branch 10 → 12 taken 74089 times.
76192 if (i + 16 > w)
265 2103 bw = w - i;
266 76192 flags = bytestream2_get_byte(gb);
267
2/2
✓ Branch 13 → 14 taken 42 times.
✓ Branch 13 → 19 taken 76150 times.
76192 if (flags & HT_RAW) {
268
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 42 times.
42 if (bytestream2_get_bytes_left(gb) < bw * bh * bpp) {
269 ✗ av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
270 ✗ return AVERROR_INVALIDDATA;
271 }
272 42 paint_raw(dst2, bw, bh, gb, bpp, c->bigendian, stride);
273 } else {
274
2/2
✓ Branch 19 → 20 taken 1377 times.
✓ Branch 19 → 21 taken 74773 times.
76150 if (flags & HT_BKG)
275 1377 bg = vmnc_get_pixel(gb, bpp, c->bigendian);
276
2/2
✓ Branch 21 → 22 taken 1494 times.
✓ Branch 21 → 23 taken 74656 times.
76150 if (flags & HT_FG)
277 1494 fg = vmnc_get_pixel(gb, bpp, c->bigendian);
278 76150 rects = 0;
279
2/2
✓ Branch 23 → 24 taken 30064 times.
✓ Branch 23 → 26 taken 46086 times.
76150 if (flags & HT_SUB)
280 30064 rects = bytestream2_get_byte(gb);
281 76150 color = !!(flags & HT_CLR);
282
283 76150 paint_rect(dst2, 0, 0, bw, bh, bg, bpp, stride);
284
285
2/2
✓ Branch 28 → 29 taken 1 time.
✓ Branch 28 → 31 taken 76149 times.
76150 if (bytestream2_get_bytes_left(gb) < rects * (color * bpp + 2)) {
286 1 av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
287 1 return AVERROR_INVALIDDATA;
288 }
289
2/2
✓ Branch 42 → 32 taken 367655 times.
✓ Branch 42 → 43 taken 76149 times.
443804 for (k = 0; k < rects; k++) {
290 int rect_x, rect_y, rect_w, rect_h;
291
2/2
✓ Branch 32 → 33 taken 366042 times.
✓ Branch 32 → 34 taken 1613 times.
367655 if (color)
292 366042 fg = vmnc_get_pixel(gb, bpp, c->bigendian);
293 367655 xy = bytestream2_get_byte(gb);
294 367655 wh = bytestream2_get_byte(gb);
295
296 367655 rect_x = xy >> 4;
297 367655 rect_y = xy & 0xF;
298 367655 rect_w = (wh >> 4) + 1;
299 367655 rect_h = (wh & 0xF) + 1;
300
301
2/4
✓ Branch 36 → 37 taken 367655 times.
✗ Branch 36 → 38 not taken.
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 40 taken 367655 times.
367655 if (rect_x + rect_w > w - i || rect_y + rect_h > h - j) {
302 ✗ av_log(c->avctx, AV_LOG_ERROR, "Rectangle outside picture\n");
303 ✗ return AVERROR_INVALIDDATA;
304 }
305
306 367655 paint_rect(dst2, rect_x, rect_y,
307 rect_w, rect_h, fg, bpp, stride);
308 }
309 }
310 }
311 2246 dst += stride * 16;
312 }
313 462 return 0;
314 }
315
316 ✗ static void reset_buffers(VmncContext *c)
317 {
318 ✗ av_freep(&c->curbits);
319 ✗ av_freep(&c->curmask);
320 ✗ av_freep(&c->screendta);
321 ✗ c->cur_w = c->cur_h = 0;
322 ✗ c->cur_hx = c->cur_hy = 0;
323
324 ✗ }
325
326 242 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
327 int *got_frame, AVPacket *avpkt)
328 {
329 242 const uint8_t *buf = avpkt->data;
330 242 int buf_size = avpkt->size;
331 242 VmncContext * const c = avctx->priv_data;
332 242 GetByteContext *gb = &c->gb;
333 uint8_t *outptr;
334 int dx, dy, w, h, depth, enc, chunks, res, size_left, ret;
335
336 242 bytestream2_init(gb, buf, buf_size);
337 242 bytestream2_skip(gb, 2);
338 242 chunks = bytestream2_get_be16(gb);
339
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 242 times.
242 if (12LL * chunks > bytestream2_get_bytes_left(gb))
340 ✗ return AVERROR_INVALIDDATA;
341
342
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 242 times.
242 if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
343 ✗ return ret;
344
345 242 c->pic->flags &= ~AV_FRAME_FLAG_KEY;
346 242 c->pic->pict_type = AV_PICTURE_TYPE_P;
347
348 // restore screen after cursor
349
2/2
✓ Branch 11 → 12 taken 49 times.
✓ Branch 11 → 26 taken 193 times.
242 if (c->cursor_painted) {
350 int i;
351 49 w = c->cur_w;
352
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 49 times.
49 if (c->width < c->cur_x + w)
353 ✗ w = c->width - c->cur_x;
354 49 h = c->cur_h;
355
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 49 times.
49 if (c->height < c->cur_y + h)
356 ✗ h = c->height - c->cur_y;
357 49 dx = c->cur_x;
358
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 49 times.
49 if (dx < 0) {
359 ✗ w += dx;
360 ✗ dx = 0;
361 }
362 49 dy = c->cur_y;
363
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 49 times.
49 if (dy < 0) {
364 ✗ h += dy;
365 ✗ dy = 0;
366 }
367
2/4
✓ Branch 20 → 21 taken 49 times.
✗ Branch 20 → 25 not taken.
✓ Branch 21 → 22 taken 49 times.
✗ Branch 21 → 25 not taken.
49 if ((w > 0) && (h > 0)) {
368 49 outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
369
2/2
✓ Branch 24 → 23 taken 1568 times.
✓ Branch 24 → 25 taken 49 times.
1617 for (i = 0; i < h; i++) {
370 1568 memcpy(outptr, c->screendta + i * c->cur_w * c->bpp2,
371 1568 w * c->bpp2);
372 1568 outptr += c->pic->linesize[0];
373 }
374 }
375 49 c->cursor_painted = 0;
376 }
377
378
2/2
✓ Branch 97 → 27 taken 567 times.
✓ Branch 97 → 98 taken 241 times.
808 while (chunks--) {
379
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 31 taken 567 times.
567 if (bytestream2_get_bytes_left(gb) < 12) {
380 ✗ av_log(avctx, AV_LOG_ERROR, "Premature end of data!\n");
381 ✗ return -1;
382 }
383 567 dx = bytestream2_get_be16(gb);
384 567 dy = bytestream2_get_be16(gb);
385 567 w = bytestream2_get_be16(gb);
386 567 h = bytestream2_get_be16(gb);
387 567 enc = bytestream2_get_be32(gb);
388
2/4
✓ Branch 36 → 37 taken 567 times.
✗ Branch 36 → 38 not taken.
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 40 taken 567 times.
567 if ((dx + w > c->width) || (dy + h > c->height)) {
389 ✗ av_log(avctx, AV_LOG_ERROR,
390 "Incorrect frame size: %ix%i+%ix%i of %ix%i\n",
391 w, h, dx, dy, c->width, c->height);
392 ✗ return AVERROR_INVALIDDATA;
393 }
394 567 outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
395 567 size_left = bytestream2_get_bytes_left(gb);
396
9/10
✓ Branch 41 → 42 taken 16 times.
✓ Branch 41 → 66 taken 35 times.
✓ Branch 41 → 68 taken 29 times.
✓ Branch 41 → 69 taken 4 times.
✓ Branch 41 → 71 taken 4 times.
✓ Branch 41 → 73 taken 4 times.
✓ Branch 41 → 83 taken 4 times.
✓ Branch 41 → 85 taken 8 times.
✓ Branch 41 → 90 taken 463 times.
✗ Branch 41 → 94 not taken.
567 switch (enc) {
397 16 case MAGIC_WMVd: // cursor
398
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 45 taken 16 times.
16 if (w*(int64_t)h*c->bpp2 > INT_MAX/2 - 2) {
399 ✗ av_log(avctx, AV_LOG_ERROR, "dimensions too large\n");
400 ✗ return AVERROR_INVALIDDATA;
401 }
402
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 48 taken 16 times.
16 if (size_left < 2 + w * h * c->bpp2 * 2) {
403 ✗ av_log(avctx, AV_LOG_ERROR,
404 "Premature end of data! (need %i got %i)\n",
405 ✗ 2 + w * h * c->bpp2 * 2, size_left);
406 ✗ return AVERROR_INVALIDDATA;
407 }
408 16 bytestream2_skip(gb, 2);
409 16 c->cur_w = w;
410 16 c->cur_h = h;
411 16 c->cur_hx = dx;
412 16 c->cur_hy = dy;
413
2/4
✓ Branch 49 → 50 taken 16 times.
✗ Branch 49 → 51 not taken.
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 53 taken 16 times.
16 if ((c->cur_hx > c->cur_w) || (c->cur_hy > c->cur_h)) {
414 ✗ av_log(avctx, AV_LOG_ERROR,
415 "Cursor hot spot is not in image: "
416 "%ix%i of %ix%i cursor size\n",
417 c->cur_hx, c->cur_hy, c->cur_w, c->cur_h);
418 ✗ c->cur_hx = c->cur_hy = 0;
419 }
420
1/2
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 56 taken 16 times.
16 if (c->cur_w * c->cur_h >= INT_MAX / c->bpp2) {
421 ✗ reset_buffers(c);
422 ✗ return AVERROR(EINVAL);
423 } else {
424 16 int screen_size = c->cur_w * c->cur_h * c->bpp2;
425
2/4
✓ Branch 57 → 58 taken 16 times.
✗ Branch 57 → 62 not taken.
✓ Branch 59 → 60 taken 16 times.
✗ Branch 59 → 62 not taken.
32 if ((ret = av_reallocp(&c->curbits, screen_size)) < 0 ||
426
1/2
✗ Branch 61 → 62 not taken.
✓ Branch 61 → 64 taken 16 times.
32 (ret = av_reallocp(&c->curmask, screen_size)) < 0 ||
427 16 (ret = av_reallocp(&c->screendta, screen_size)) < 0) {
428 ✗ reset_buffers(c);
429 ✗ return ret;
430 }
431 }
432 16 load_cursor(c);
433 16 break;
434 35 case MAGIC_WMVe: // unknown
435 35 bytestream2_skip(gb, 2);
436 35 break;
437 29 case MAGIC_WMVf: // update cursor position
438 29 c->cur_x = dx - c->cur_hx;
439 29 c->cur_y = dy - c->cur_hy;
440 29 break;
441 4 case MAGIC_WMVg: // unknown
442 4 bytestream2_skip(gb, 10);
443 4 break;
444 4 case MAGIC_WMVh: // unknown
445 4 bytestream2_skip(gb, 4);
446 4 break;
447 4 case MAGIC_WMVi: // ServerInitialization struct
448 4 c->pic->flags |= AV_FRAME_FLAG_KEY;
449 4 c->pic->pict_type = AV_PICTURE_TYPE_I;
450 4 depth = bytestream2_get_byte(gb);
451
1/2
✗ Branch 74 → 75 not taken.
✓ Branch 74 → 76 taken 4 times.
4 if (depth != c->bpp) {
452 ✗ av_log(avctx, AV_LOG_INFO,
453 "Depth mismatch. Container %i bpp, "
454 "Frame data: %i bpp\n",
455 c->bpp, depth);
456 }
457 4 bytestream2_skip(gb, 1);
458 4 c->bigendian = bytestream2_get_byte(gb);
459
1/2
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 81 taken 4 times.
4 if (c->bigendian & (~1)) {
460 ✗ av_log(avctx, AV_LOG_INFO,
461 "Invalid header: bigendian flag = %i\n", c->bigendian);
462 ✗ return AVERROR_INVALIDDATA;
463 }
464 //skip the rest of pixel format data
465 4 bytestream2_skip(gb, 13);
466 4 break;
467 4 case MAGIC_WMVj: // unknown
468 4 bytestream2_skip(gb, 2);
469 4 break;
470 8 case 0x00000000: // raw rectangle data
471
1/2
✗ Branch 85 → 86 not taken.
✓ Branch 85 → 88 taken 8 times.
8 if (size_left < w * h * c->bpp2) {
472 ✗ av_log(avctx, AV_LOG_ERROR,
473 "Premature end of data! (need %i got %i)\n",
474 ✗ w * h * c->bpp2, size_left);
475 ✗ return AVERROR_INVALIDDATA;
476 }
477 8 paint_raw(outptr, w, h, gb, c->bpp2, c->bigendian,
478 8 c->pic->linesize[0]);
479 8 break;
480 463 case 0x00000005: // HexTile encoded rectangle
481 463 res = decode_hextile(c, outptr, gb, w, h, c->pic->linesize[0]);
482
2/2
✓ Branch 91 → 92 taken 1 time.
✓ Branch 91 → 93 taken 462 times.
463 if (res < 0)
483 1 return res;
484 462 break;
485 ✗ default:
486 ✗ av_log(avctx, AV_LOG_ERROR, "Unsupported block type 0x%08X\n", enc);
487 ✗ chunks = 0; // leave chunks decoding loop
488 }
489 }
490
2/2
✓ Branch 98 → 99 taken 49 times.
✓ Branch 98 → 114 taken 192 times.
241 if (c->screendta) {
491 int i;
492 // save screen data before painting cursor
493 49 w = c->cur_w;
494
1/2
✗ Branch 99 → 100 not taken.
✓ Branch 99 → 101 taken 49 times.
49 if (c->width < c->cur_x + w)
495 ✗ w = c->width - c->cur_x;
496 49 h = c->cur_h;
497
1/2
✗ Branch 101 → 102 not taken.
✓ Branch 101 → 103 taken 49 times.
49 if (c->height < c->cur_y + h)
498 ✗ h = c->height - c->cur_y;
499 49 dx = c->cur_x;
500
1/2
✗ Branch 103 → 104 not taken.
✓ Branch 103 → 105 taken 49 times.
49 if (dx < 0) {
501 ✗ w += dx;
502 ✗ dx = 0;
503 }
504 49 dy = c->cur_y;
505
1/2
✗ Branch 105 → 106 not taken.
✓ Branch 105 → 107 taken 49 times.
49 if (dy < 0) {
506 ✗ h += dy;
507 ✗ dy = 0;
508 }
509
2/4
✓ Branch 107 → 108 taken 49 times.
✗ Branch 107 → 114 not taken.
✓ Branch 108 → 109 taken 49 times.
✗ Branch 108 → 114 not taken.
49 if ((w > 0) && (h > 0)) {
510 49 outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
511
2/2
✓ Branch 111 → 110 taken 1568 times.
✓ Branch 111 → 112 taken 49 times.
1617 for (i = 0; i < h; i++) {
512 1568 memcpy(c->screendta + i * c->cur_w * c->bpp2, outptr,
513 1568 w * c->bpp2);
514 1568 outptr += c->pic->linesize[0];
515 }
516 49 outptr = c->pic->data[0];
517 49 put_cursor(outptr, c->pic->linesize[0], c, c->cur_x, c->cur_y);
518 49 c->cursor_painted = 1;
519 }
520 }
521 241 *got_frame = 1;
522
1/2
✗ Branch 115 → 116 not taken.
✓ Branch 115 → 117 taken 241 times.
241 if ((ret = av_frame_ref(rframe, c->pic)) < 0)
523 ✗ return ret;
524
525 /* always report that the buffer was completely consumed */
526 241 return buf_size;
527 }
528
529 4 static av_cold int decode_init(AVCodecContext *avctx)
530 {
531 4 VmncContext * const c = avctx->priv_data;
532
533 4 c->avctx = avctx;
534 4 c->width = avctx->width;
535 4 c->height = avctx->height;
536 4 c->bpp = avctx->bits_per_coded_sample;
537
538
2/5
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
✗ Branch 2 → 5 not taken.
✓ Branch 2 → 6 taken 2 times.
✗ Branch 2 → 7 not taken.
4 switch (c->bpp) {
539 ✗ case 8:
540 ✗ avctx->pix_fmt = AV_PIX_FMT_PAL8;
541 ✗ break;
542 2 case 16:
543 2 avctx->pix_fmt = AV_PIX_FMT_RGB555;
544 2 break;
545 ✗ case 24:
546 /* 24 bits is not technically supported, but some clients might
547 * mistakenly set it, so let's assume they actually meant 32 bits */
548 ✗ c->bpp = 32;
549 av_fallthrough;
550 2 case 32:
551 2 avctx->pix_fmt = AV_PIX_FMT_0RGB32;
552 2 break;
553 ✗ default:
554 ✗ av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", c->bpp);
555 ✗ return AVERROR_INVALIDDATA;
556 }
557 4 c->bpp2 = c->bpp / 8;
558
559 4 c->pic = av_frame_alloc();
560
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 4 times.
4 if (!c->pic)
561 ✗ return AVERROR(ENOMEM);
562
563 4 return 0;
564 }
565
566 4 static av_cold int decode_end(AVCodecContext *avctx)
567 {
568 4 VmncContext * const c = avctx->priv_data;
569
570 4 av_frame_free(&c->pic);
571
572 4 av_freep(&c->curbits);
573 4 av_freep(&c->curmask);
574 4 av_freep(&c->screendta);
575 4 return 0;
576 }
577
578 const FFCodec ff_vmnc_decoder = {
579 .p.name = "vmnc",
580 CODEC_LONG_NAME("VMware Screen Codec / VMware Video"),
581 .p.type = AVMEDIA_TYPE_VIDEO,
582 .p.id = AV_CODEC_ID_VMNC,
583 .priv_data_size = sizeof(VmncContext),
584 .init = decode_init,
585 .close = decode_end,
586 FF_CODEC_DECODE_CB(decode_frame),
587 .p.capabilities = AV_CODEC_CAP_DR1,
588 };
589