]> git.sesse.net Git - ffmpeg/blob - libavcodec/vmnc.c
Merge commit '88bd7fdc821aaa0cbcf44cf075c62aaa42121e3f'
[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
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
66     /* cursor data */
67     int cur_w, cur_h;
68     int cur_x, cur_y;
69     int cur_hx, cur_hy;
70     uint8_t* curbits, *curmask;
71     uint8_t* screendta;
72 } VmncContext;
73
74 /* read pixel value from stream */
75 static av_always_inline int vmnc_get_pixel(const uint8_t* buf, int bpp, int be) {
76     switch(bpp * 2 + be) {
77     case 2:
78     case 3: return *buf;
79     case 4: return AV_RL16(buf);
80     case 5: return AV_RB16(buf);
81     case 8: return AV_RL32(buf);
82     case 9: return AV_RB32(buf);
83     default: return 0;
84     }
85 }
86
87 static void load_cursor(VmncContext *c, const uint8_t *src)
88 {
89     int i, j, p;
90     const int bpp = c->bpp2;
91     uint8_t  *dst8  = c->curbits;
92     uint16_t *dst16 = (uint16_t*)c->curbits;
93     uint32_t *dst32 = (uint32_t*)c->curbits;
94
95     for(j = 0; j < c->cur_h; j++) {
96         for(i = 0; i < c->cur_w; i++) {
97             p = vmnc_get_pixel(src, bpp, c->bigendian);
98             src += bpp;
99             if(bpp == 1) *dst8++ = p;
100             if(bpp == 2) *dst16++ = p;
101             if(bpp == 4) *dst32++ = p;
102         }
103     }
104     dst8 = c->curmask;
105     dst16 = (uint16_t*)c->curmask;
106     dst32 = (uint32_t*)c->curmask;
107     for(j = 0; j < c->cur_h; j++) {
108         for(i = 0; i < c->cur_w; i++) {
109             p = vmnc_get_pixel(src, bpp, c->bigendian);
110             src += bpp;
111             if(bpp == 1) *dst8++ = p;
112             if(bpp == 2) *dst16++ = p;
113             if(bpp == 4) *dst32++ = p;
114         }
115     }
116 }
117
118 static void put_cursor(uint8_t *dst, int stride, VmncContext *c, int dx, int dy)
119 {
120     int i, j;
121     int w, h, x, y;
122     w = c->cur_w;
123     if(c->width < c->cur_x + c->cur_w) w = c->width - c->cur_x;
124     h = c->cur_h;
125     if(c->height < c->cur_y + c->cur_h) h = c->height - c->cur_y;
126     x = c->cur_x;
127     y = c->cur_y;
128     if(x < 0) {
129         w += x;
130         x = 0;
131     }
132     if(y < 0) {
133         h += y;
134         y = 0;
135     }
136
137     if((w < 1) || (h < 1)) return;
138     dst += x * c->bpp2 + y * stride;
139
140     if(c->bpp2 == 1) {
141         uint8_t* cd = c->curbits, *msk = c->curmask;
142         for(j = 0; j < h; j++) {
143             for(i = 0; i < w; i++)
144                 dst[i] = (dst[i] & cd[i]) ^ msk[i];
145             msk += c->cur_w;
146             cd += c->cur_w;
147             dst += stride;
148         }
149     } else if(c->bpp2 == 2) {
150         uint16_t* cd = (uint16_t*)c->curbits, *msk = (uint16_t*)c->curmask;
151         uint16_t* dst2;
152         for(j = 0; j < h; j++) {
153             dst2 = (uint16_t*)dst;
154             for(i = 0; i < w; i++)
155                 dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
156             msk += c->cur_w;
157             cd += c->cur_w;
158             dst += stride;
159         }
160     } else if(c->bpp2 == 4) {
161         uint32_t* cd = (uint32_t*)c->curbits, *msk = (uint32_t*)c->curmask;
162         uint32_t* dst2;
163         for(j = 0; j < h; j++) {
164             dst2 = (uint32_t*)dst;
165             for(i = 0; i < w; i++)
166                 dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
167             msk += c->cur_w;
168             cd += c->cur_w;
169             dst += stride;
170         }
171     }
172 }
173
174 /* fill rectangle with given color */
175 static av_always_inline void paint_rect(uint8_t *dst, int dx, int dy, int w, int h, int color, int bpp, int stride)
176 {
177     int i, j;
178     dst += dx * bpp + dy * stride;
179     if(bpp == 1){
180         for(j = 0; j < h; j++) {
181             memset(dst, color, w);
182             dst += stride;
183         }
184     }else if(bpp == 2){
185         uint16_t* dst2;
186         for(j = 0; j < h; j++) {
187             dst2 = (uint16_t*)dst;
188             for(i = 0; i < w; i++) {
189                 *dst2++ = color;
190             }
191             dst += stride;
192         }
193     }else if(bpp == 4){
194         uint32_t* dst2;
195         for(j = 0; j < h; j++) {
196             dst2 = (uint32_t*)dst;
197             for(i = 0; i < w; i++) {
198                 dst2[i] = color;
199             }
200             dst += stride;
201         }
202     }
203 }
204
205 static av_always_inline void paint_raw(uint8_t *dst, int w, int h, const uint8_t* src, int bpp, int be, int stride)
206 {
207     int i, j, p;
208     for(j = 0; j < h; j++) {
209         for(i = 0; i < w; i++) {
210             p = vmnc_get_pixel(src, bpp, be);
211             src += bpp;
212             switch(bpp){
213             case 1:
214                 dst[i] = p;
215                 break;
216             case 2:
217                 ((uint16_t*)dst)[i] = p;
218                 break;
219             case 4:
220                 ((uint32_t*)dst)[i] = p;
221                 break;
222             }
223         }
224         dst += stride;
225     }
226 }
227
228 static int decode_hextile(VmncContext *c, uint8_t* dst, const uint8_t* src, int ssize, int w, int h, int stride)
229 {
230     int i, j, k;
231     int bg = 0, fg = 0, rects, color, flags, xy, wh;
232     const int bpp = c->bpp2;
233     uint8_t *dst2;
234     int bw = 16, bh = 16;
235     const uint8_t *ssrc=src;
236
237     for(j = 0; j < h; j += 16) {
238         dst2 = dst;
239         bw = 16;
240         if(j + 16 > h) bh = h - j;
241         for(i = 0; i < w; i += 16, dst2 += 16 * bpp) {
242             if(src - ssrc >= ssize) {
243                 av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
244                 return -1;
245             }
246             if(i + 16 > w) bw = w - i;
247             flags = *src++;
248             if(flags & HT_RAW) {
249                 if(src - ssrc > ssize - bw * bh * bpp) {
250                     av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
251                     return -1;
252                 }
253                 paint_raw(dst2, bw, bh, src, bpp, c->bigendian, stride);
254                 src += bw * bh * bpp;
255             } else {
256                 if(flags & HT_BKG) {
257                     bg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp;
258                 }
259                 if(flags & HT_FG) {
260                     fg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp;
261                 }
262                 rects = 0;
263                 if(flags & HT_SUB)
264                     rects = *src++;
265                 color = !!(flags & HT_CLR);
266
267                 paint_rect(dst2, 0, 0, bw, bh, bg, bpp, stride);
268
269                 if(src - ssrc > ssize - rects * (color * bpp + 2)) {
270                     av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
271                     return -1;
272                 }
273                 for(k = 0; k < rects; k++) {
274                     if(color) {
275                         fg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp;
276                     }
277                     xy = *src++;
278                     wh = *src++;
279                     paint_rect(dst2, xy >> 4, xy & 0xF, (wh>>4)+1, (wh & 0xF)+1, fg, bpp, stride);
280                 }
281             }
282         }
283         dst += stride * 16;
284     }
285     return src - ssrc;
286 }
287
288 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
289                         AVPacket *avpkt)
290 {
291     const uint8_t *buf = avpkt->data;
292     int buf_size = avpkt->size;
293     VmncContext * const c = avctx->priv_data;
294     uint8_t *outptr;
295     const uint8_t *src = buf;
296     int dx, dy, w, h, depth, enc, chunks, res, size_left;
297
298     c->pic.reference = 3;
299     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
300     if(avctx->reget_buffer(avctx, &c->pic) < 0){
301         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
302         return -1;
303     }
304
305     c->pic.key_frame = 0;
306     c->pic.pict_type = AV_PICTURE_TYPE_P;
307
308     //restore screen after cursor
309     if(c->screendta) {
310         int i;
311         w = c->cur_w;
312         if(c->width < c->cur_x + w) w = c->width - c->cur_x;
313         h = c->cur_h;
314         if(c->height < c->cur_y + h) h = c->height - c->cur_y;
315         dx = c->cur_x;
316         if(dx < 0) {
317             w += dx;
318             dx = 0;
319         }
320         dy = c->cur_y;
321         if(dy < 0) {
322             h += dy;
323             dy = 0;
324         }
325         if((w > 0) && (h > 0)) {
326             outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
327             for(i = 0; i < h; i++) {
328                 memcpy(outptr, c->screendta + i * c->cur_w * c->bpp2, w * c->bpp2);
329                 outptr += c->pic.linesize[0];
330             }
331         }
332     }
333     src += 2;
334     chunks = AV_RB16(src); src += 2;
335     while(chunks--) {
336         if(buf_size - (src - buf) < 12) {
337             av_log(avctx, AV_LOG_ERROR, "Premature end of data!\n");
338             return -1;
339         }
340         dx = AV_RB16(src); src += 2;
341         dy = AV_RB16(src); src += 2;
342         w  = AV_RB16(src); src += 2;
343         h  = AV_RB16(src); src += 2;
344         enc = AV_RB32(src); src += 4;
345         outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
346         size_left = buf_size - (src - buf);
347         switch(enc) {
348         case MAGIC_WMVd: // cursor
349             if (w*(int64_t)h*c->bpp2 > INT_MAX/2 - 2) {
350                 av_log(avctx, AV_LOG_ERROR, "dimensions too large\n");
351                 return AVERROR_INVALIDDATA;
352             }
353             if(size_left < 2 + w * h * c->bpp2 * 2) {
354                 av_log(avctx, AV_LOG_ERROR, "Premature end of data! (need %i got %i)\n", 2 + w * h * c->bpp2 * 2, size_left);
355                 return -1;
356             }
357             src += 2;
358             c->cur_w = w;
359             c->cur_h = h;
360             c->cur_hx = dx;
361             c->cur_hy = dy;
362             if((c->cur_hx > c->cur_w) || (c->cur_hy > c->cur_h)) {
363                 av_log(avctx, AV_LOG_ERROR, "Cursor hot spot is not in image: %ix%i of %ix%i cursor size\n", c->cur_hx, c->cur_hy, c->cur_w, c->cur_h);
364                 c->cur_hx = c->cur_hy = 0;
365             }
366             c->curbits = av_realloc(c->curbits, c->cur_w * c->cur_h * c->bpp2);
367             c->curmask = av_realloc(c->curmask, c->cur_w * c->cur_h * c->bpp2);
368             c->screendta = av_realloc(c->screendta, c->cur_w * c->cur_h * c->bpp2);
369             load_cursor(c, src);
370             src += w * h * c->bpp2 * 2;
371             break;
372         case MAGIC_WMVe: // unknown
373             src += 2;
374             break;
375         case MAGIC_WMVf: // update cursor position
376             c->cur_x = dx - c->cur_hx;
377             c->cur_y = dy - c->cur_hy;
378             break;
379         case MAGIC_WMVg: // unknown
380             src += 10;
381             break;
382         case MAGIC_WMVh: // unknown
383             src += 4;
384             break;
385         case MAGIC_WMVi: // ServerInitialization struct
386             c->pic.key_frame = 1;
387             c->pic.pict_type = AV_PICTURE_TYPE_I;
388             depth = *src++;
389             if(depth != c->bpp) {
390                 av_log(avctx, AV_LOG_INFO, "Depth mismatch. Container %i bpp, Frame data: %i bpp\n", c->bpp, depth);
391             }
392             src++;
393             c->bigendian = *src++;
394             if(c->bigendian & (~1)) {
395                 av_log(avctx, AV_LOG_INFO, "Invalid header: bigendian flag = %i\n", c->bigendian);
396                 return -1;
397             }
398             //skip the rest of pixel format data
399             src += 13;
400             break;
401         case MAGIC_WMVj: // unknown
402             src += 2;
403             break;
404         case 0x00000000: // raw rectangle data
405             if((dx + w > c->width) || (dy + h > c->height)) {
406                 av_log(avctx, AV_LOG_ERROR, "Incorrect frame size: %ix%i+%ix%i of %ix%i\n", w, h, dx, dy, c->width, c->height);
407                 return -1;
408             }
409             if(size_left < w * h * c->bpp2) {
410                 av_log(avctx, AV_LOG_ERROR, "Premature end of data! (need %i got %i)\n", w * h * c->bpp2, size_left);
411                 return -1;
412             }
413             paint_raw(outptr, w, h, src, c->bpp2, c->bigendian, c->pic.linesize[0]);
414             src += w * h * c->bpp2;
415             break;
416         case 0x00000005: // HexTile encoded rectangle
417             if((dx + w > c->width) || (dy + h > c->height)) {
418                 av_log(avctx, AV_LOG_ERROR, "Incorrect frame size: %ix%i+%ix%i of %ix%i\n", w, h, dx, dy, c->width, c->height);
419                 return -1;
420             }
421             res = decode_hextile(c, outptr, src, size_left, w, h, c->pic.linesize[0]);
422             if(res < 0)
423                 return -1;
424             src += res;
425             break;
426         default:
427             av_log(avctx, AV_LOG_ERROR, "Unsupported block type 0x%08X\n", enc);
428             chunks = 0; // leave chunks decoding loop
429         }
430     }
431     if(c->screendta){
432         int i;
433         //save screen data before painting cursor
434         w = c->cur_w;
435         if(c->width < c->cur_x + w) w = c->width - c->cur_x;
436         h = c->cur_h;
437         if(c->height < c->cur_y + h) h = c->height - c->cur_y;
438         dx = c->cur_x;
439         if(dx < 0) {
440             w += dx;
441             dx = 0;
442         }
443         dy = c->cur_y;
444         if(dy < 0) {
445             h += dy;
446             dy = 0;
447         }
448         if((w > 0) && (h > 0)) {
449             outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
450             for(i = 0; i < h; i++) {
451                 memcpy(c->screendta + i * c->cur_w * c->bpp2, outptr, w * c->bpp2);
452                 outptr += c->pic.linesize[0];
453             }
454             outptr = c->pic.data[0];
455             put_cursor(outptr, c->pic.linesize[0], c, c->cur_x, c->cur_y);
456         }
457     }
458     *got_frame      = 1;
459     *(AVFrame*)data = c->pic;
460
461     /* always report that the buffer was completely consumed */
462     return buf_size;
463 }
464
465
466
467 /*
468  *
469  * Init VMnc decoder
470  *
471  */
472 static av_cold int decode_init(AVCodecContext *avctx)
473 {
474     VmncContext * const c = avctx->priv_data;
475
476     c->avctx = avctx;
477
478     c->width = avctx->width;
479     c->height = avctx->height;
480
481     c->bpp = avctx->bits_per_coded_sample;
482     c->bpp2 = c->bpp/8;
483     avcodec_get_frame_defaults(&c->pic);
484
485     switch(c->bpp){
486     case 8:
487         avctx->pix_fmt = AV_PIX_FMT_PAL8;
488         break;
489     case 16:
490         avctx->pix_fmt = AV_PIX_FMT_RGB555;
491         break;
492     case 32:
493         avctx->pix_fmt = AV_PIX_FMT_RGB32;
494         break;
495     default:
496         av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", c->bpp);
497         return AVERROR_INVALIDDATA;
498     }
499
500     return 0;
501 }
502
503
504
505 /*
506  *
507  * Uninit VMnc decoder
508  *
509  */
510 static av_cold int decode_end(AVCodecContext *avctx)
511 {
512     VmncContext * const c = avctx->priv_data;
513
514     if (c->pic.data[0])
515         avctx->release_buffer(avctx, &c->pic);
516
517     av_free(c->curbits);
518     av_free(c->curmask);
519     av_free(c->screendta);
520     return 0;
521 }
522
523 AVCodec ff_vmnc_decoder = {
524     .name           = "vmnc",
525     .type           = AVMEDIA_TYPE_VIDEO,
526     .id             = AV_CODEC_ID_VMNC,
527     .priv_data_size = sizeof(VmncContext),
528     .init           = decode_init,
529     .close          = decode_end,
530     .decode         = decode_frame,
531     .capabilities   = CODEC_CAP_DR1,
532     .long_name      = NULL_IF_CONFIG_SMALL("VMware Screen Codec / VMware Video"),
533 };