FFmpeg coverage


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