vmnc.c 17.2 KB
Newer Older
1 2 3 4
/*
 * VMware Screen Codec (VMnc) decoder
 * Copyright (c) 2006 Konstantin Shishkov
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25 26 27 28 29 30
 * VMware Screen Codec (VMnc) decoder
 * As Alex Beregszaszi discovered, this is effectively RFB data dump
 */

#include <stdio.h>
#include <stdlib.h>

31
#include "libavutil/common.h"
32
#include "libavutil/intreadwrite.h"
33
#include "avcodec.h"
34
#include "internal.h"
Luca Barbato's avatar
Luca Barbato committed
35
#include "bytestream.h"
36

37 38 39 40 41 42 43 44 45
enum EncTypes {
    MAGIC_WMVd = 0x574D5664,
    MAGIC_WMVe,
    MAGIC_WMVf,
    MAGIC_WMVg,
    MAGIC_WMVh,
    MAGIC_WMVi,
    MAGIC_WMVj
};
46 47 48 49 50 51 52 53 54 55 56 57 58 59

enum HexTile_Flags {
    HT_RAW =  1, // tile is raw
    HT_BKG =  2, // background color is present
    HT_FG  =  4, // foreground color is present
    HT_SUB =  8, // subrects are present
    HT_CLR = 16  // each subrect has own color
};

/*
 * Decoder context
 */
typedef struct VmncContext {
    AVCodecContext *avctx;
60
    AVFrame *pic;
61 62 63 64 65 66

    int bpp;
    int bpp2;
    int bigendian;
    uint8_t pal[768];
    int width, height;
Luca Barbato's avatar
Luca Barbato committed
67
    GetByteContext gb;
Kostya Shishkov's avatar
Kostya Shishkov committed
68 69 70 71 72

    /* cursor data */
    int cur_w, cur_h;
    int cur_x, cur_y;
    int cur_hx, cur_hy;
73 74
    uint8_t *curbits, *curmask;
    uint8_t *screendta;
75 76 77
} VmncContext;

/* read pixel value from stream */
Luca Barbato's avatar
Luca Barbato committed
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:
Luca Barbato's avatar
Luca Barbato committed
83
        return bytestream2_get_byte(gb);
84
    case 4:
Luca Barbato's avatar
Luca Barbato committed
85
        return bytestream2_get_le16(gb);
86
    case 5:
Luca Barbato's avatar
Luca Barbato committed
87
        return bytestream2_get_be16(gb);
88
    case 8:
Luca Barbato's avatar
Luca Barbato committed
89
        return bytestream2_get_le32(gb);
90
    case 9:
Luca Barbato's avatar
Luca Barbato committed
91 92
        return bytestream2_get_be32(gb);
    default: return 0;
93 94 95
    }
}

Luca Barbato's avatar
Luca Barbato committed
96
static void load_cursor(VmncContext *c)
Kostya Shishkov's avatar
Kostya Shishkov committed
97 98
{
    int i, j, p;
99 100 101 102
    const int bpp   = c->bpp2;
    uint8_t *dst8   =             c->curbits;
    uint16_t *dst16 = (uint16_t *)c->curbits;
    uint32_t *dst32 = (uint32_t *)c->curbits;
Kostya Shishkov's avatar
Kostya Shishkov committed
103

104 105
    for (j = 0; j < c->cur_h; j++) {
        for (i = 0; i < c->cur_w; i++) {
Luca Barbato's avatar
Luca Barbato committed
106
            p = vmnc_get_pixel(&c->gb, bpp, c->bigendian);
107 108 109 110 111 112
            if (bpp == 1)
                *dst8++ = p;
            if (bpp == 2)
                *dst16++ = p;
            if (bpp == 4)
                *dst32++ = p;
Kostya Shishkov's avatar
Kostya Shishkov committed
113 114
        }
    }
115
    dst8  =            c->curmask;
Kostya Shishkov's avatar
Kostya Shishkov committed
116 117
    dst16 = (uint16_t*)c->curmask;
    dst32 = (uint32_t*)c->curmask;
118 119
    for (j = 0; j < c->cur_h; j++) {
        for (i = 0; i < c->cur_w; i++) {
Luca Barbato's avatar
Luca Barbato committed
120
            p = vmnc_get_pixel(&c->gb, bpp, c->bigendian);
121 122 123 124 125 126
            if (bpp == 1)
                *dst8++ = p;
            if (bpp == 2)
                *dst16++ = p;
            if (bpp == 4)
                *dst32++ = p;
Kostya Shishkov's avatar
Kostya Shishkov committed
127 128 129 130 131 132
        }
    }
}

static void put_cursor(uint8_t *dst, int stride, VmncContext *c, int dx, int dy)
{
133
    int i, j;
Kostya Shishkov's avatar
Kostya Shishkov committed
134 135
    int w, h, x, y;
    w = c->cur_w;
136 137
    if (c->width < c->cur_x + c->cur_w)
        w = c->width - c->cur_x;
Kostya Shishkov's avatar
Kostya Shishkov committed
138
    h = c->cur_h;
139 140
    if (c->height < c->cur_y + c->cur_h)
        h = c->height - c->cur_y;
Kostya Shishkov's avatar
Kostya Shishkov committed
141 142
    x = c->cur_x;
    y = c->cur_y;
143
    if (x < 0) {
Kostya Shishkov's avatar
Kostya Shishkov committed
144
        w += x;
145
        x  = 0;
Kostya Shishkov's avatar
Kostya Shishkov committed
146
    }
147
    if (y < 0) {
Kostya Shishkov's avatar
Kostya Shishkov committed
148
        h += y;
149
        y  = 0;
Kostya Shishkov's avatar
Kostya Shishkov committed
150 151
    }

152 153
    if ((w < 1) || (h < 1))
        return;
Kostya Shishkov's avatar
Kostya Shishkov committed
154 155
    dst += x * c->bpp2 + y * stride;

156 157 158 159
    if (c->bpp2 == 1) {
        uint8_t *cd = c->curbits, *msk = c->curmask;
        for (j = 0; j < h; j++) {
            for (i = 0; i < w; i++)
Kostya Shishkov's avatar
Kostya Shishkov committed
160 161
                dst[i] = (dst[i] & cd[i]) ^ msk[i];
            msk += c->cur_w;
162
            cd  += c->cur_w;
Kostya Shishkov's avatar
Kostya Shishkov committed
163 164
            dst += stride;
        }
165 166 167 168
    } else if (c->bpp2 == 2) {
        uint16_t *cd = (uint16_t*)c->curbits, *msk = (uint16_t*)c->curmask;
        uint16_t *dst2;
        for (j = 0; j < h; j++) {
Kostya Shishkov's avatar
Kostya Shishkov committed
169
            dst2 = (uint16_t*)dst;
170
            for (i = 0; i < w; i++)
Kostya Shishkov's avatar
Kostya Shishkov committed
171 172
                dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
            msk += c->cur_w;
173
            cd  += c->cur_w;
Kostya Shishkov's avatar
Kostya Shishkov committed
174 175
            dst += stride;
        }
176 177 178 179
    } else if (c->bpp2 == 4) {
        uint32_t *cd = (uint32_t*)c->curbits, *msk = (uint32_t*)c->curmask;
        uint32_t *dst2;
        for (j = 0; j < h; j++) {
Kostya Shishkov's avatar
Kostya Shishkov committed
180
            dst2 = (uint32_t*)dst;
181
            for (i = 0; i < w; i++)
Kostya Shishkov's avatar
Kostya Shishkov committed
182 183
                dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
            msk += c->cur_w;
184
            cd  += c->cur_w;
Kostya Shishkov's avatar
Kostya Shishkov committed
185 186 187 188 189
            dst += stride;
        }
    }
}

190
/* fill rectangle with given color */
191 192 193
static av_always_inline void paint_rect(uint8_t *dst, int dx, int dy,
                                        int w, int h, int color,
                                        int bpp, int stride)
194 195 196
{
    int i, j;
    dst += dx * bpp + dy * stride;
197 198
    if (bpp == 1) {
        for (j = 0; j < h; j++) {
199 200 201
            memset(dst, color, w);
            dst += stride;
        }
202 203 204
    } else if (bpp == 2) {
        uint16_t *dst2;
        for (j = 0; j < h; j++) {
205
            dst2 = (uint16_t*)dst;
206
            for (i = 0; i < w; i++)
207 208 209
                *dst2++ = color;
            dst += stride;
        }
210 211 212
    } else if (bpp == 4) {
        uint32_t *dst2;
        for (j = 0; j < h; j++) {
213
            dst2 = (uint32_t*)dst;
214
            for (i = 0; i < w; i++)
215 216 217 218 219 220
                dst2[i] = color;
            dst += stride;
        }
    }
}

221
static av_always_inline void paint_raw(uint8_t *dst, int w, int h,
Luca Barbato's avatar
Luca Barbato committed
222
                                       GetByteContext *gb, int bpp,
223
                                       int be, int stride)
224 225
{
    int i, j, p;
226 227
    for (j = 0; j < h; j++) {
        for (i = 0; i < w; i++) {
Luca Barbato's avatar
Luca Barbato committed
228
            p = vmnc_get_pixel(gb, bpp, be);
229
            switch (bpp) {
230 231 232 233 234 235 236 237 238 239
            case 1:
                dst[i] = p;
                break;
            case 2:
                ((uint16_t*)dst)[i] = p;
                break;
            case 4:
                ((uint32_t*)dst)[i] = p;
                break;
            }
240 241 242 243 244
        }
        dst += stride;
    }
}

Luca Barbato's avatar
Luca Barbato committed
245 246
static int decode_hextile(VmncContext *c, uint8_t* dst, GetByteContext *gb,
                          int w, int h, int stride)
247 248 249 250 251 252 253
{
    int i, j, k;
    int bg = 0, fg = 0, rects, color, flags, xy, wh;
    const int bpp = c->bpp2;
    uint8_t *dst2;
    int bw = 16, bh = 16;

254
    for (j = 0; j < h; j += 16) {
255
        dst2 = dst;
256 257 258 259
        bw   = 16;
        if (j + 16 > h)
            bh = h - j;
        for (i = 0; i < w; i += 16, dst2 += 16 * bpp) {
Luca Barbato's avatar
Luca Barbato committed
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 265
            if (i + 16 > w)
                bw = w - i;
Luca Barbato's avatar
Luca Barbato committed
266
            flags = bytestream2_get_byte(gb);
267
            if (flags & HT_RAW) {
Luca Barbato's avatar
Luca Barbato committed
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
                }
Luca Barbato's avatar
Luca Barbato committed
272
                paint_raw(dst2, bw, bh, gb, bpp, c->bigendian, stride);
273
            } else {
Luca Barbato's avatar
Luca Barbato committed
274 275 276 277
                if (flags & HT_BKG)
                    bg = vmnc_get_pixel(gb, bpp, c->bigendian);
                if (flags & HT_FG)
                    fg = vmnc_get_pixel(gb, bpp, c->bigendian);
278
                rects = 0;
279
                if (flags & HT_SUB)
Luca Barbato's avatar
Luca Barbato committed
280
                    rects = bytestream2_get_byte(gb);
281
                color = !!(flags & HT_CLR);
282 283 284

                paint_rect(dst2, 0, 0, bw, bh, bg, bpp, stride);

Luca Barbato's avatar
Luca Barbato committed
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++) {
Luca Barbato's avatar
Luca Barbato committed
290 291 292 293 294 295
                    if (color)
                        fg = vmnc_get_pixel(gb, bpp, c->bigendian);
                    xy = bytestream2_get_byte(gb);
                    wh = bytestream2_get_byte(gb);
                    paint_rect(dst2, xy >> 4, xy & 0xF,
                               (wh>>4)+1, (wh & 0xF)+1, fg, bpp, stride);
296 297 298 299 300
                }
            }
        }
        dst += stride * 16;
    }
Luca Barbato's avatar
Luca Barbato committed
301
    return 0;
302 303
}

304 305 306 307 308 309
static void reset_buffers(VmncContext *c)
{
    av_freep(&c->curbits);
    av_freep(&c->curmask);
    av_freep(&c->screendta);
    c->cur_w = c->cur_h = 0;
310 311
    c->cur_hx = c->cur_hy = 0;

312 313
}

314 315
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
                        AVPacket *avpkt)
316
{
317
    const uint8_t *buf = avpkt->data;
318
    int buf_size       = avpkt->size;
319
    VmncContext * const c = avctx->priv_data;
Luca Barbato's avatar
Luca Barbato committed
320
    GetByteContext *gb = &c->gb;
321
    uint8_t *outptr;
322
    int dx, dy, w, h, depth, enc, chunks, res, size_left, ret;
323

324
    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
325
        return ret;
326

Luca Barbato's avatar
Luca Barbato committed
327 328
    bytestream2_init(gb, buf, buf_size);

329 330
    c->pic->key_frame = 0;
    c->pic->pict_type = AV_PICTURE_TYPE_P;
331

332 333
    // restore screen after cursor
    if (c->screendta) {
Kostya Shishkov's avatar
Kostya Shishkov committed
334 335
        int i;
        w = c->cur_w;
336 337
        if (c->width < c->cur_x + w)
            w = c->width - c->cur_x;
Kostya Shishkov's avatar
Kostya Shishkov committed
338
        h = c->cur_h;
339 340
        if (c->height < c->cur_y + h)
            h = c->height - c->cur_y;
Kostya Shishkov's avatar
Kostya Shishkov committed
341
        dx = c->cur_x;
342
        if (dx < 0) {
Kostya Shishkov's avatar
Kostya Shishkov committed
343 344 345 346
            w += dx;
            dx = 0;
        }
        dy = c->cur_y;
347
        if (dy < 0) {
Kostya Shishkov's avatar
Kostya Shishkov committed
348 349 350
            h += dy;
            dy = 0;
        }
351
        if ((w > 0) && (h > 0)) {
352
            outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
353 354 355
            for (i = 0; i < h; i++) {
                memcpy(outptr, c->screendta + i * c->cur_w * c->bpp2,
                       w * c->bpp2);
356
                outptr += c->pic->linesize[0];
Kostya Shishkov's avatar
Kostya Shishkov committed
357 358 359
            }
        }
    }
Luca Barbato's avatar
Luca Barbato committed
360 361
    bytestream2_skip(gb, 2);
    chunks = bytestream2_get_be16(gb);
362
    while (chunks--) {
363
        if (bytestream2_get_bytes_left(gb) < 12) {
364 365 366
            av_log(avctx, AV_LOG_ERROR, "Premature end of data!\n");
            return -1;
        }
Luca Barbato's avatar
Luca Barbato committed
367 368 369 370 371
        dx  = bytestream2_get_be16(gb);
        dy  = bytestream2_get_be16(gb);
        w   = bytestream2_get_be16(gb);
        h   = bytestream2_get_be16(gb);
        enc = bytestream2_get_be32(gb);
372
        outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
Luca Barbato's avatar
Luca Barbato committed
373
        size_left = bytestream2_get_bytes_left(gb);
374
        switch (enc) {
Kostya Shishkov's avatar
Kostya Shishkov committed
375
        case MAGIC_WMVd: // cursor
376 377 378 379
            if (w*(int64_t)h*c->bpp2 > INT_MAX/2 - 2) {
                av_log(avctx, AV_LOG_ERROR, "dimensions too large\n");
                return AVERROR_INVALIDDATA;
            }
380 381 382 383
            if (size_left < 2 + w * h * c->bpp2 * 2) {
                av_log(avctx, AV_LOG_ERROR,
                       "Premature end of data! (need %i got %i)\n",
                       2 + w * h * c->bpp2 * 2, size_left);
384
                return AVERROR_INVALIDDATA;
385
            }
Luca Barbato's avatar
Luca Barbato committed
386
            bytestream2_skip(gb, 2);
387 388
            c->cur_w  = w;
            c->cur_h  = h;
Kostya Shishkov's avatar
Kostya Shishkov committed
389 390
            c->cur_hx = dx;
            c->cur_hy = dy;
391 392 393 394 395
            if ((c->cur_hx > c->cur_w) || (c->cur_hy > c->cur_h)) {
                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);
Kostya Shishkov's avatar
Kostya Shishkov committed
396 397
                c->cur_hx = c->cur_hy = 0;
            }
398 399 400 401 402 403 404 405 406 407 408 409
            if (c->cur_w * c->cur_h >= INT_MAX / c->bpp2) {
                reset_buffers(c);
                return AVERROR(EINVAL);
            } else {
                int screen_size = c->cur_w * c->cur_h * c->bpp2;
                if ((ret = av_reallocp(&c->curbits, screen_size)) < 0 ||
                    (ret = av_reallocp(&c->curmask, screen_size)) < 0 ||
                    (ret = av_reallocp(&c->screendta, screen_size)) < 0) {
                    reset_buffers(c);
                    return ret;
                }
            }
Luca Barbato's avatar
Luca Barbato committed
410
            load_cursor(c);
411 412
            break;
        case MAGIC_WMVe: // unknown
Luca Barbato's avatar
Luca Barbato committed
413
            bytestream2_skip(gb, 2);
414
            break;
Kostya Shishkov's avatar
Kostya Shishkov committed
415 416 417
        case MAGIC_WMVf: // update cursor position
            c->cur_x = dx - c->cur_hx;
            c->cur_y = dy - c->cur_hy;
418
            break;
419
        case MAGIC_WMVg: // unknown
Luca Barbato's avatar
Luca Barbato committed
420
            bytestream2_skip(gb, 10);
421 422
            break;
        case MAGIC_WMVh: // unknown
Luca Barbato's avatar
Luca Barbato committed
423
            bytestream2_skip(gb, 4);
424
            break;
425
        case MAGIC_WMVi: // ServerInitialization struct
426 427
            c->pic->key_frame = 1;
            c->pic->pict_type = AV_PICTURE_TYPE_I;
Luca Barbato's avatar
Luca Barbato committed
428
            depth = bytestream2_get_byte(gb);
429 430 431 432 433
            if (depth != c->bpp) {
                av_log(avctx, AV_LOG_INFO,
                       "Depth mismatch. Container %i bpp, "
                       "Frame data: %i bpp\n",
                       c->bpp, depth);
434
            }
Luca Barbato's avatar
Luca Barbato committed
435 436
            bytestream2_skip(gb, 1);
            c->bigendian = bytestream2_get_byte(gb);
437 438 439
            if (c->bigendian & (~1)) {
                av_log(avctx, AV_LOG_INFO,
                       "Invalid header: bigendian flag = %i\n", c->bigendian);
440
                return AVERROR_INVALIDDATA;
441
            }
Luca Barbato's avatar
Luca Barbato committed
442 443
            //skip the rest of pixel format data
            bytestream2_skip(gb, 13);
444
            break;
445
        case MAGIC_WMVj: // unknown
Luca Barbato's avatar
Luca Barbato committed
446
            bytestream2_skip(gb, 2);
447
            break;
448
        case 0x00000000: // raw rectangle data
449 450 451 452
            if ((dx + w > c->width) || (dy + h > c->height)) {
                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);
453
                return AVERROR_INVALIDDATA;
454
            }
455 456 457 458
            if (size_left < w * h * c->bpp2) {
                av_log(avctx, AV_LOG_ERROR,
                       "Premature end of data! (need %i got %i)\n",
                       w * h * c->bpp2, size_left);
459
                return AVERROR_INVALIDDATA;
460
            }
Luca Barbato's avatar
Luca Barbato committed
461
            paint_raw(outptr, w, h, gb, c->bpp2, c->bigendian,
462
                      c->pic->linesize[0]);
463 464
            break;
        case 0x00000005: // HexTile encoded rectangle
465 466 467 468
            if ((dx + w > c->width) || (dy + h > c->height)) {
                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);
469
                return AVERROR_INVALIDDATA;
470
            }
471
            res = decode_hextile(c, outptr, gb, w, h, c->pic->linesize[0]);
472
            if (res < 0)
473
                return res;
474 475 476 477
            break;
        default:
            av_log(avctx, AV_LOG_ERROR, "Unsupported block type 0x%08X\n", enc);
            chunks = 0; // leave chunks decoding loop
478 479
        }
    }
480
    if (c->screendta) {
Kostya Shishkov's avatar
Kostya Shishkov committed
481
        int i;
482
        // save screen data before painting cursor
Kostya Shishkov's avatar
Kostya Shishkov committed
483
        w = c->cur_w;
484 485
        if (c->width < c->cur_x + w)
            w = c->width - c->cur_x;
Kostya Shishkov's avatar
Kostya Shishkov committed
486
        h = c->cur_h;
487 488
        if (c->height < c->cur_y + h)
            h = c->height - c->cur_y;
Kostya Shishkov's avatar
Kostya Shishkov committed
489
        dx = c->cur_x;
490
        if (dx < 0) {
Kostya Shishkov's avatar
Kostya Shishkov committed
491 492 493 494
            w += dx;
            dx = 0;
        }
        dy = c->cur_y;
495
        if (dy < 0) {
Kostya Shishkov's avatar
Kostya Shishkov committed
496 497 498
            h += dy;
            dy = 0;
        }
499
        if ((w > 0) && (h > 0)) {
500
            outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
501 502 503
            for (i = 0; i < h; i++) {
                memcpy(c->screendta + i * c->cur_w * c->bpp2, outptr,
                       w * c->bpp2);
504
                outptr += c->pic->linesize[0];
Kostya Shishkov's avatar
Kostya Shishkov committed
505
            }
506 507
            outptr = c->pic->data[0];
            put_cursor(outptr, c->pic->linesize[0], c, c->cur_x, c->cur_y);
Kostya Shishkov's avatar
Kostya Shishkov committed
508 509
        }
    }
510
    *got_frame = 1;
511
    if ((ret = av_frame_ref(data, c->pic)) < 0)
512
        return ret;
513 514 515 516 517

    /* always report that the buffer was completely consumed */
    return buf_size;
}

518
static av_cold int decode_init(AVCodecContext *avctx)
519
{
520
    VmncContext * const c = avctx->priv_data;
521

522 523
    c->avctx  = avctx;
    c->width  = avctx->width;
524
    c->height = avctx->height;
525 526
    c->bpp    = avctx->bits_per_coded_sample;
    c->bpp2   = c->bpp / 8;
527

528
    switch (c->bpp) {
529
    case 8:
530
        avctx->pix_fmt = AV_PIX_FMT_PAL8;
531 532
        break;
    case 16:
533
        avctx->pix_fmt = AV_PIX_FMT_RGB555;
534 535
        break;
    case 32:
536
        avctx->pix_fmt = AV_PIX_FMT_RGB32;
537 538 539
        break;
    default:
        av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", c->bpp);
540
        return AVERROR_INVALIDDATA;
541 542
    }

543 544
    c->pic = av_frame_alloc();
    if (!c->pic)
545
        return AVERROR(ENOMEM);
546

547 548 549
    return 0;
}

550
static av_cold int decode_end(AVCodecContext *avctx)
551
{
552
    VmncContext * const c = avctx->priv_data;
553

554
    av_frame_free(&c->pic);
555

556 557 558
    av_freep(&c->curbits);
    av_freep(&c->curmask);
    av_freep(&c->screendta);
559 560 561
    return 0;
}

562
AVCodec ff_vmnc_decoder = {
563
    .name           = "vmnc",
564
    .long_name      = NULL_IF_CONFIG_SMALL("VMware Screen Codec / VMware Video"),
565
    .type           = AVMEDIA_TYPE_VIDEO,
566
    .id             = AV_CODEC_ID_VMNC,
567 568 569 570 571
    .priv_data_size = sizeof(VmncContext),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
    .capabilities   = CODEC_CAP_DR1,
572
};