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