FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vmnc.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 253 356 71.1%
Functions: 9 10 90.0%
Branches: 125 197 63.5%

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