mss2.c 26.3 KB
Newer Older
Alberto Delmás's avatar
Alberto Delmás committed
1 2 3
/*
 * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
 *
4
 * This file is part of FFmpeg.
Alberto Delmás's avatar
Alberto Delmás committed
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
Alberto Delmás's avatar
Alberto Delmás committed
7 8 9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
Alberto Delmás's avatar
Alberto Delmás committed
12 13 14 15 16
 * 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
17
 * License along with FFmpeg; if not, write to the Free Software
Alberto Delmás's avatar
Alberto Delmás committed
18 19 20 21 22 23 24 25 26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
 */

#include "libavutil/avassert.h"
27
#include "error_resilience.h"
28
#include "internal.h"
29
#include "mpeg_er.h"
30
#include "msmpeg4.h"
31
#include "qpeldsp.h"
Alberto Delmás's avatar
Alberto Delmás committed
32
#include "vc1.h"
33
#include "wmv2data.h"
Alberto Delmás's avatar
Alberto Delmás committed
34 35 36 37 38 39
#include "mss12.h"
#include "mss2dsp.h"

typedef struct MSS2Context {
    VC1Context     v;
    int            split_position;
40
    AVFrame       *last_pic;
Alberto Delmás's avatar
Alberto Delmás committed
41 42
    MSS12Context   c;
    MSS2DSPContext dsp;
43
    QpelDSPContext qdsp;
Alberto Delmás's avatar
Alberto Delmás committed
44 45 46 47 48 49 50 51 52 53 54
    SliceContext   sc[2];
} MSS2Context;

static void arith2_normalise(ArithCoder *c)
{
    while ((c->high >> 15) - (c->low >> 15) < 2) {
        if ((c->low ^ c->high) & 0x10000) {
            c->high  ^= 0x8000;
            c->value ^= 0x8000;
            c->low   ^= 0x8000;
        }
55 56 57
        c->high  = (uint16_t)c->high  << 8  | 0xFF;
        c->value = (uint16_t)c->value << 8  | bytestream2_get_byte(c->gbc.gB);
        c->low   = (uint16_t)c->low   << 8;
Alberto Delmás's avatar
Alberto Delmás committed
58 59 60
    }
}

61
ARITH_GET_BIT(arith2)
Alberto Delmás's avatar
Alberto Delmás committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

/* L. Stuiver and A. Moffat: "Piecewise Integer Mapping for Arithmetic Coding."
 * In Proc. 8th Data Compression Conference (DCC '98), pp. 3-12, Mar. 1998 */

static int arith2_get_scaled_value(int value, int n, int range)
{
    int split = (n << 1) - range;

    if (value > split)
        return split + (value - split >> 1);
    else
        return value;
}

static void arith2_rescale_interval(ArithCoder *c, int range,
                                    int low, int high, int n)
{
    int split = (n << 1) - range;

    if (high > split)
        c->high = split + (high - split << 1);
    else
        c->high = high;

    c->high += c->low - 1;

    if (low > split)
        c->low += split + (low - split << 1);
    else
        c->low += low;
}

static int arith2_get_number(ArithCoder *c, int n)
{
    int range = c->high - c->low + 1;
    int scale = av_log2(range) - av_log2(n);
    int val;

    if (n << scale > range)
        scale--;

    n <<= scale;

    val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;

    arith2_rescale_interval(c, range, val << scale, (val + 1) << scale, n);

    arith2_normalise(c);

    return val;
}

114
static int arith2_get_prob(ArithCoder *c, int16_t *probs)
Alberto Delmás's avatar
Alberto Delmás committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
{
    int range = c->high - c->low + 1, n = *probs;
    int scale = av_log2(range) - av_log2(n);
    int i     = 0, val;

    if (n << scale > range)
        scale--;

    n <<= scale;

    val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
    while (probs[++i] > val) ;

    arith2_rescale_interval(c, range,
                            probs[i] << scale, probs[i - 1] << scale, n);

    return i;
}

134
ARITH_GET_MODEL_SYM(arith2)
Alberto Delmás's avatar
Alberto Delmás committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

static int arith2_get_consumed_bytes(ArithCoder *c)
{
    int diff = (c->high >> 16) - (c->low >> 16);
    int bp   = bytestream2_tell(c->gbc.gB) - 3 << 3;
    int bits = 1;

    while (!(diff & 0x80)) {
        bits++;
        diff <<= 1;
    }

    return (bits + bp + 7 >> 3) + ((c->low >> 16) + 1 == c->high >> 16);
}

static void arith2_init(ArithCoder *c, GetByteContext *gB)
{
    c->low           = 0;
    c->high          = 0xFFFFFF;
    c->value         = bytestream2_get_be24(gB);
    c->gbc.gB        = gB;
    c->get_model_sym = arith2_get_model_sym;
    c->get_number    = arith2_get_number;
}

static int decode_pal_v2(MSS12Context *ctx, const uint8_t *buf, int buf_size)
{
    int i, ncol;
    uint32_t *pal = ctx->pal + 256 - ctx->free_colours;

    if (!ctx->free_colours)
        return 0;

    ncol = *buf++;
169
    if (ncol > ctx->free_colours || buf_size < 2 + ncol * 3)
170
        return AVERROR_INVALIDDATA;
Alberto Delmás's avatar
Alberto Delmás committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    for (i = 0; i < ncol; i++)
        *pal++ = AV_RB24(buf + 3 * i);

    return 1 + ncol * 3;
}

static int decode_555(GetByteContext *gB, uint16_t *dst, int stride,
                      int keyframe, int w, int h)
{
    int last_symbol = 0, repeat = 0, prev_avail = 0;

    if (!keyframe) {
        int x, y, endx, endy, t;

#define READ_PAIR(a, b)                 \
    a  = bytestream2_get_byte(gB) << 4; \
    t  = bytestream2_get_byte(gB);      \
    a |= t >> 4;                        \
    b  = (t & 0xF) << 8;                \
    b |= bytestream2_get_byte(gB);      \

        READ_PAIR(x, endx)
        READ_PAIR(y, endy)

        if (endx >= w || endy >= h || x > endx || y > endy)
196
            return AVERROR_INVALIDDATA;
Alberto Delmás's avatar
Alberto Delmás committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        dst += x + stride * y;
        w    = endx - x + 1;
        h    = endy - y + 1;
        if (y)
            prev_avail = 1;
    }

    do {
        uint16_t *p = dst;
        do {
            if (repeat-- < 1) {
                int b = bytestream2_get_byte(gB);
                if (b < 128)
                    last_symbol = b << 8 | bytestream2_get_byte(gB);
                else if (b > 129) {
                    repeat = 0;
213 214 215 216 217
                    while (b-- > 130) {
                        if (repeat >= (INT_MAX >> 8) - 1) {
                            av_log(NULL, AV_LOG_ERROR, "repeat overflow\n");
                            return AVERROR_INVALIDDATA;
                        }
Alberto Delmás's avatar
Alberto Delmás committed
218
                        repeat = (repeat << 8) + bytestream2_get_byte(gB) + 1;
219
                    }
Alberto Delmás's avatar
Alberto Delmás committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
                    if (last_symbol == -2) {
                        int skip = FFMIN((unsigned)repeat, dst + w - p);
                        repeat -= skip;
                        p      += skip;
                    }
                } else
                    last_symbol = 127 - b;
            }
            if (last_symbol >= 0)
                *p = last_symbol;
            else if (last_symbol == -1 && prev_avail)
                *p = *(p - stride);
        } while (++p < dst + w);
        dst       += stride;
        prev_avail = 1;
    } while (--h);

    return 0;
}

static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, int pal_stride,
                      uint8_t *rgb_dst, int rgb_stride, uint32_t *pal,
                      int keyframe, int kf_slipt, int slice, int w, int h)
{
    uint8_t bits[270] = { 0 };
    uint32_t codes[270];
    VLC vlc;

    int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;
    int remaining_codes, surplus_codes, i;

    const int alphabet_size = 270 - keyframe;

    int last_symbol = 0, repeat = 0, prev_avail = 0;

    if (!keyframe) {
        int x, y, clipw, cliph;

        x     = get_bits(gb, 12);
        y     = get_bits(gb, 12);
        clipw = get_bits(gb, 12) + 1;
        cliph = get_bits(gb, 12) + 1;

        if (x + clipw > w || y + cliph > h)
            return AVERROR_INVALIDDATA;
        pal_dst += pal_stride * y + x;
        rgb_dst += rgb_stride * y + x * 3;
        w        = clipw;
        h        = cliph;
        if (y)
            prev_avail = 1;
    } else {
        if (slice > 0) {
            pal_dst   += pal_stride * kf_slipt;
            rgb_dst   += rgb_stride * kf_slipt;
            prev_avail = 1;
            h         -= kf_slipt;
        } else
            h = kf_slipt;
    }

    /* read explicit codes */
    do {
        while (current_codes--) {
            int symbol = get_bits(gb, 8);
            if (symbol >= 204 - keyframe)
                symbol += 14 - keyframe;
            else if (symbol > 189)
                symbol = get_bits1(gb) + (symbol << 1) - 190;
            if (bits[symbol])
                return AVERROR_INVALIDDATA;
            bits[symbol]  = current_length;
            codes[symbol] = next_code++;
            read_codes++;
        }
        current_length++;
        next_code     <<= 1;
        remaining_codes = (1 << current_length) - next_code;
        current_codes   = get_bits(gb, av_ceil_log2(remaining_codes + 1));
        if (current_length > 22 || current_codes > remaining_codes)
            return AVERROR_INVALIDDATA;
    } while (current_codes != remaining_codes);

    remaining_codes = alphabet_size - read_codes;

    /* determine the minimum length to fit the rest of the alphabet */
    while ((surplus_codes = (2 << current_length) -
                            (next_code << 1) - remaining_codes) < 0) {
        current_length++;
        next_code <<= 1;
    }

    /* add the rest of the symbols lexicographically */
    for (i = 0; i < alphabet_size; i++)
        if (!bits[i]) {
            if (surplus_codes-- == 0) {
                current_length++;
                next_code <<= 1;
            }
            bits[i]  = current_length;
            codes[i] = next_code++;
        }

    if (next_code != 1 << current_length)
        return AVERROR_INVALIDDATA;

326
    if ((i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0)) < 0)
Alberto Delmás's avatar
Alberto Delmás committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
        return i;

    /* frame decode */
    do {
        uint8_t *pp = pal_dst;
        uint8_t *rp = rgb_dst;
        do {
            if (repeat-- < 1) {
                int b = get_vlc2(gb, vlc.table, 9, 3);
                if (b < 256)
                    last_symbol = b;
                else if (b < 268) {
                    b -= 256;
                    if (b == 11)
                        b = get_bits(gb, 4) + 10;

                    if (!b)
                        repeat = 0;
                    else
                        repeat = get_bits(gb, b);

348
                    repeat += (1 << b) - 1;
Alberto Delmás's avatar
Alberto Delmás committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

                    if (last_symbol == -2) {
                        int skip = FFMIN(repeat, pal_dst + w - pp);
                        repeat -= skip;
                        pp     += skip;
                        rp     += skip * 3;
                    }
                } else
                    last_symbol = 267 - b;
            }
            if (last_symbol >= 0) {
                *pp = last_symbol;
                AV_WB24(rp, pal[last_symbol]);
            } else if (last_symbol == -1 && prev_avail) {
                *pp = *(pp - pal_stride);
                memcpy(rp, rp - rgb_stride, 3);
            }
            rp += 3;
        } while (++pp < pal_dst + w);
        pal_dst   += pal_stride;
        rgb_dst   += rgb_stride;
        prev_avail = 1;
    } while (--h);

    ff_free_vlc(&vlc);
    return 0;
}

static int decode_wmv9(AVCodecContext *avctx, const uint8_t *buf, int buf_size,
                       int x, int y, int w, int h, int wmv9_mask)
{
    MSS2Context *ctx  = avctx->priv_data;
    MSS12Context *c   = &ctx->c;
    VC1Context *v     = avctx->priv_data;
    MpegEncContext *s = &v->s;
    AVFrame *f;
385
    int ret;
Alberto Delmás's avatar
Alberto Delmás committed
386 387 388

    ff_mpeg_flush(avctx);

389 390
    if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
        return ret;
Alberto Delmás's avatar
Alberto Delmás committed
391 392 393

    s->loop_filter = avctx->skip_loop_filter < AVDISCARD_ALL;

394
    if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
Alberto Delmás's avatar
Alberto Delmás committed
395 396 397 398 399 400 401 402 403
        av_log(v->s.avctx, AV_LOG_ERROR, "header error\n");
        return AVERROR_INVALIDDATA;
    }

    if (s->pict_type != AV_PICTURE_TYPE_I) {
        av_log(v->s.avctx, AV_LOG_ERROR, "expected I-frame\n");
        return AVERROR_INVALIDDATA;
    }

404
    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
Alberto Delmás's avatar
Alberto Delmás committed
405

406 407
    if ((ret = ff_mpv_frame_start(s, avctx)) < 0) {
        av_log(v->s.avctx, AV_LOG_ERROR, "ff_mpv_frame_start error\n");
408
        avctx->pix_fmt = AV_PIX_FMT_RGB24;
409
        return ret;
Alberto Delmás's avatar
Alberto Delmás committed
410 411
    }

412
    ff_mpeg_er_frame_start(s);
Alberto Delmás's avatar
Alberto Delmás committed
413 414 415 416 417 418 419 420 421 422 423 424

    v->bits = buf_size * 8;

    v->end_mb_x = (w + 15) >> 4;
    s->end_mb_y = (h + 15) >> 4;
    if (v->respic & 1)
        v->end_mb_x = v->end_mb_x + 1 >> 1;
    if (v->respic & 2)
        s->end_mb_y = s->end_mb_y + 1 >> 1;

    ff_vc1_decode_blocks(v);

425 426 427 428 429 430 431
    if (v->end_mb_x == s->mb_width && s->end_mb_y == s->mb_height) {
        ff_er_frame_end(&s->er);
    } else {
        av_log(v->s.avctx, AV_LOG_WARNING,
               "disabling error correction due to block count mismatch %dx%d != %dx%d\n",
               v->end_mb_x, s->end_mb_y, s->mb_width, s->mb_height);
    }
Alberto Delmás's avatar
Alberto Delmás committed
432

433
    ff_mpv_frame_end(s);
Alberto Delmás's avatar
Alberto Delmás committed
434

435
    f = s->current_picture.f;
Alberto Delmás's avatar
Alberto Delmás committed
436 437 438

    if (v->respic == 3) {
        ctx->dsp.upsample_plane(f->data[0], f->linesize[0], w,      h);
439 440
        ctx->dsp.upsample_plane(f->data[1], f->linesize[1], w+1 >> 1, h+1 >> 1);
        ctx->dsp.upsample_plane(f->data[2], f->linesize[2], w+1 >> 1, h+1 >> 1);
Alberto Delmás's avatar
Alberto Delmás committed
441
    } else if (v->respic)
442 443
        avpriv_request_sample(v->s.avctx,
                              "Asymmetric WMV9 rectangle subsampling");
Alberto Delmás's avatar
Alberto Delmás committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

    av_assert0(f->linesize[1] == f->linesize[2]);

    if (wmv9_mask != -1)
        ctx->dsp.mss2_blit_wmv9_masked(c->rgb_pic + y * c->rgb_stride + x * 3,
                                       c->rgb_stride, wmv9_mask,
                                       c->pal_pic + y * c->pal_stride + x,
                                       c->pal_stride,
                                       f->data[0], f->linesize[0],
                                       f->data[1], f->data[2], f->linesize[1],
                                       w, h);
    else
        ctx->dsp.mss2_blit_wmv9(c->rgb_pic + y * c->rgb_stride + x * 3,
                                c->rgb_stride,
                                f->data[0], f->linesize[0],
                                f->data[1], f->data[2], f->linesize[1],
                                w, h);

462
    avctx->pix_fmt = AV_PIX_FMT_RGB24;
Alberto Delmás's avatar
Alberto Delmás committed
463 464 465 466 467 468 469 470 471 472 473

    return 0;
}

typedef struct Rectangle {
    int coded, x, y, w, h;
} Rectangle;

#define MAX_WMV9_RECTANGLES 20
#define ARITH2_PADDING 2

474
static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
Alberto Delmás's avatar
Alberto Delmás committed
475 476 477 478 479 480
                             AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size       = avpkt->size;
    MSS2Context *ctx = avctx->priv_data;
    MSS12Context *c  = &ctx->c;
481
    AVFrame *frame   = data;
Alberto Delmás's avatar
Alberto Delmás committed
482 483 484 485 486 487 488
    GetBitContext gb;
    GetByteContext gB;
    ArithCoder acoder;

    int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;

    Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
489
    int used_rects = 0, i, implicit_rect = 0, av_uninit(wmv9_mask);
Alberto Delmás's avatar
Alberto Delmás committed
490

491 492
    if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
        return ret;
Alberto Delmás's avatar
Alberto Delmás committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

    if (keyframe = get_bits1(&gb))
        skip_bits(&gb, 7);
    has_wmv9 = get_bits1(&gb);
    has_mv   = keyframe ? 0 : get_bits1(&gb);
    is_rle   = get_bits1(&gb);
    is_555   = is_rle && get_bits1(&gb);
    if (c->slice_split > 0)
        ctx->split_position = c->slice_split;
    else if (c->slice_split < 0) {
        if (get_bits1(&gb)) {
            if (get_bits1(&gb)) {
                if (get_bits1(&gb))
                    ctx->split_position = get_bits(&gb, 16);
                else
                    ctx->split_position = get_bits(&gb, 12);
            } else
                ctx->split_position = get_bits(&gb, 8) << 4;
        } else {
            if (keyframe)
                ctx->split_position = avctx->height / 2;
        }
    } else
        ctx->split_position = avctx->height;

    if (c->slice_split && (ctx->split_position < 1 - is_555 ||
                           ctx->split_position > avctx->height - 1))
        return AVERROR_INVALIDDATA;

    align_get_bits(&gb);
    buf      += get_bits_count(&gb) >> 3;
    buf_size -= get_bits_count(&gb) >> 3;

    if (buf_size < 1)
        return AVERROR_INVALIDDATA;

    if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
        return AVERROR_INVALIDDATA;

532
    avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;
533 534
    if (ctx->last_pic->format != avctx->pix_fmt)
        av_frame_unref(ctx->last_pic);
Alberto Delmás's avatar
Alberto Delmás committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

    if (has_wmv9) {
        bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
        arith2_init(&acoder, &gB);

        implicit_rect = !arith2_get_bit(&acoder);

        while (arith2_get_bit(&acoder)) {
            if (used_rects == MAX_WMV9_RECTANGLES)
                return AVERROR_INVALIDDATA;
            r = &wmv9rects[used_rects];
            if (!used_rects)
                r->x = arith2_get_number(&acoder, avctx->width);
            else
                r->x = arith2_get_number(&acoder, avctx->width -
                                         wmv9rects[used_rects - 1].x) +
                       wmv9rects[used_rects - 1].x;
            r->y = arith2_get_number(&acoder, avctx->height);
            r->w = arith2_get_number(&acoder, avctx->width  - r->x) + 1;
            r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
            used_rects++;
        }

        if (implicit_rect && used_rects) {
            av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
            return AVERROR_INVALIDDATA;
        }

        if (implicit_rect) {
            wmv9rects[0].x = 0;
            wmv9rects[0].y = 0;
            wmv9rects[0].w = avctx->width;
            wmv9rects[0].h = avctx->height;

            used_rects = 1;
        }
        for (i = 0; i < used_rects; i++) {
            if (!implicit_rect && arith2_get_bit(&acoder)) {
                av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
                return AVERROR_INVALIDDATA;
            }
            if (!i) {
                wmv9_mask = arith2_get_bit(&acoder) - 1;
                if (!wmv9_mask)
                    wmv9_mask = arith2_get_number(&acoder, 256);
            }
            wmv9rects[i].coded = arith2_get_number(&acoder, 2);
        }

        buf      += arith2_get_consumed_bytes(&acoder);
        buf_size -= arith2_get_consumed_bytes(&acoder);
        if (buf_size < 1)
            return AVERROR_INVALIDDATA;
    }

    c->mvX = c->mvY = 0;
    if (keyframe && !is_555) {
        if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
            return AVERROR_INVALIDDATA;
        buf      += i;
        buf_size -= i;
    } else if (has_mv) {
        buf      += 4;
        buf_size -= 4;
        if (buf_size < 1)
            return AVERROR_INVALIDDATA;
        c->mvX = AV_RB16(buf - 4) - avctx->width;
        c->mvY = AV_RB16(buf - 2) - avctx->height;
    }

    if (c->mvX < 0 || c->mvY < 0) {
        FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);

608
        if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
Alberto Delmás's avatar
Alberto Delmás committed
609 610
            return ret;

611 612 613 614
        if (ctx->last_pic->data[0]) {
            av_assert0(frame->linesize[0] == ctx->last_pic->linesize[0]);
            c->last_rgb_pic = ctx->last_pic->data[0] +
                              ctx->last_pic->linesize[0] * (avctx->height - 1);
Alberto Delmás's avatar
Alberto Delmás committed
615 616
        } else {
            av_log(avctx, AV_LOG_ERROR, "Missing keyframe\n");
617
            return AVERROR_INVALIDDATA;
Alberto Delmás's avatar
Alberto Delmás committed
618 619
        }
    } else {
620
        if ((ret = ff_reget_buffer(avctx, ctx->last_pic)) < 0)
Alberto Delmás's avatar
Alberto Delmás committed
621
            return ret;
622
        if ((ret = av_frame_ref(frame, ctx->last_pic)) < 0)
623
            return ret;
Alberto Delmás's avatar
Alberto Delmás committed
624 625 626

        c->last_rgb_pic = NULL;
    }
627 628 629
    c->rgb_pic    = frame->data[0] +
                    frame->linesize[0] * (avctx->height - 1);
    c->rgb_stride = -frame->linesize[0];
Alberto Delmás's avatar
Alberto Delmás committed
630

631 632
    frame->key_frame = keyframe;
    frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
Alberto Delmás's avatar
Alberto Delmás committed
633 634 635 636 637 638 639 640 641 642

    if (is_555) {
        bytestream2_init(&gB, buf, buf_size);

        if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
                       keyframe, avctx->width, avctx->height))
            return AVERROR_INVALIDDATA;

        buf_size -= bytestream2_tell(&gB);
    } else {
643 644 645 646 647 648
        if (keyframe) {
            c->corrupted = 0;
            ff_mss12_slicecontext_reset(&ctx->sc[0]);
            if (c->slice_split)
                ff_mss12_slicecontext_reset(&ctx->sc[1]);
        }
649
        if (is_rle) {
650 651
            if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
                return ret;
Alberto Delmás's avatar
Alberto Delmás committed
652 653
            if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
                                 c->rgb_pic, c->rgb_stride, c->pal, keyframe,
654
                                 ctx->split_position, 0,
Alberto Delmás's avatar
Alberto Delmás committed
655 656
                                 avctx->width, avctx->height))
                return ret;
657
            align_get_bits(&gb);
Alberto Delmás's avatar
Alberto Delmás committed
658

659 660 661 662 663 664 665 666 667 668 669 670
            if (c->slice_split)
                if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
                                     c->rgb_pic, c->rgb_stride, c->pal, keyframe,
                                     ctx->split_position, 1,
                                     avctx->width, avctx->height))
                    return ret;

            align_get_bits(&gb);
            buf      += get_bits_count(&gb) >> 3;
            buf_size -= get_bits_count(&gb) >> 3;
        } else if (!implicit_rect || wmv9_mask != -1) {
            if (c->corrupted)
Alberto Delmás's avatar
Alberto Delmás committed
671 672 673
                return AVERROR_INVALIDDATA;
            bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
            arith2_init(&acoder, &gB);
674 675
            c->keyframe = keyframe;
            if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[0], &acoder, 0, 0,
Alberto Delmás's avatar
Alberto Delmás committed
676
                                                    avctx->width,
677
                                                    ctx->split_position))
Alberto Delmás's avatar
Alberto Delmás committed
678 679 680 681
                return AVERROR_INVALIDDATA;

            buf      += arith2_get_consumed_bytes(&acoder);
            buf_size -= arith2_get_consumed_bytes(&acoder);
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
            if (c->slice_split) {
                if (buf_size < 1)
                    return AVERROR_INVALIDDATA;
                bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
                arith2_init(&acoder, &gB);
                if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[1], &acoder, 0,
                                                        ctx->split_position,
                                                        avctx->width,
                                                        avctx->height - ctx->split_position))
                    return AVERROR_INVALIDDATA;

                buf      += arith2_get_consumed_bytes(&acoder);
                buf_size -= arith2_get_consumed_bytes(&acoder);
            }
        } else
            memset(c->pal_pic, 0, c->pal_stride * avctx->height);
Alberto Delmás's avatar
Alberto Delmás committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
    }

    if (has_wmv9) {
        for (i = 0; i < used_rects; i++) {
            int x = wmv9rects[i].x;
            int y = wmv9rects[i].y;
            int w = wmv9rects[i].w;
            int h = wmv9rects[i].h;
            if (wmv9rects[i].coded) {
                int WMV9codedFrameSize;
                if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
                    return AVERROR_INVALIDDATA;
                if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
                                      x, y, w, h, wmv9_mask))
                    return ret;
                buf      += WMV9codedFrameSize + 3;
                buf_size -= WMV9codedFrameSize + 3;
            } else {
                uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
                if (wmv9_mask != -1) {
                    ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
                                                   wmv9_mask,
                                                   c->pal_pic + y * c->pal_stride + x,
                                                   c->pal_stride,
                                                   w, h);
                } else {
                    do {
                        memset(dst, 0x80, w * 3);
                        dst += c->rgb_stride;
                    } while (--h);
                }
            }
        }
    }

    if (buf_size)
        av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");

736
    if (c->mvX < 0 || c->mvY < 0) {
737 738
        av_frame_unref(ctx->last_pic);
        ret = av_frame_ref(ctx->last_pic, frame);
739 740 741 742
        if (ret < 0)
            return ret;
    }

743
    *got_frame       = 1;
Alberto Delmás's avatar
Alberto Delmás committed
744 745 746 747 748 749 750

    return avpkt->size;
}

static av_cold int wmv9_init(AVCodecContext *avctx)
{
    VC1Context *v = avctx->priv_data;
751
    int ret;
Alberto Delmás's avatar
Alberto Delmás committed
752 753 754

    v->s.avctx    = avctx;

755 756
    if ((ret = ff_vc1_init_common(v)) < 0)
        return ret;
Alberto Delmás's avatar
Alberto Delmás committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
    ff_vc1dsp_init(&v->vc1dsp);

    v->profile = PROFILE_MAIN;

    v->zz_8x4     = ff_wmv2_scantableA;
    v->zz_4x8     = ff_wmv2_scantableB;
    v->res_y411   = 0;
    v->res_sprite = 0;

    v->frmrtq_postproc = 7;
    v->bitrtq_postproc = 31;

    v->res_x8          = 0;
    v->multires        = 0;
    v->res_fasttx      = 1;

    v->fastuvmc        = 0;

    v->extended_mv     = 0;

    v->dquant          = 1;
    v->vstransform     = 1;

    v->res_transtab    = 0;

    v->overlap         = 0;

784
    v->resync_marker   = 0;
Alberto Delmás's avatar
Alberto Delmás committed
785 786 787 788 789 790 791 792 793 794 795
    v->rangered        = 0;

    v->s.max_b_frames = avctx->max_b_frames = 0;
    v->quantizer_mode = 0;

    v->finterpflag = 0;

    v->res_rtm_flag = 1;

    ff_vc1_init_transposed_scantables(v);

796 797 798
    if ((ret = ff_msmpeg4_decode_init(avctx)) < 0 ||
        (ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
        return ret;
Alberto Delmás's avatar
Alberto Delmás committed
799 800

    /* error concealment */
801 802
    v->s.me.qpel_put = v->s.qdsp.put_qpel_pixels_tab;
    v->s.me.qpel_avg = v->s.qdsp.avg_qpel_pixels_tab;
Alberto Delmás's avatar
Alberto Delmás committed
803 804 805 806 807 808 809 810

    return 0;
}

static av_cold int mss2_decode_end(AVCodecContext *avctx)
{
    MSS2Context *const ctx = avctx->priv_data;

811
    av_frame_free(&ctx->last_pic);
Alberto Delmás's avatar
Alberto Delmás committed
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826

    ff_mss12_decode_end(&ctx->c);
    av_freep(&ctx->c.pal_pic);
    av_freep(&ctx->c.last_pal_pic);
    ff_vc1_decode_end(avctx);

    return 0;
}

static av_cold int mss2_decode_init(AVCodecContext *avctx)
{
    MSS2Context * const ctx = avctx->priv_data;
    MSS12Context *c = &ctx->c;
    int ret;
    c->avctx = avctx;
827
    if (ret = ff_mss12_decode_init(c, 1, &ctx->sc[0], &ctx->sc[1]))
Alberto Delmás's avatar
Alberto Delmás committed
828
        return ret;
829
    ctx->last_pic   = av_frame_alloc();
Alberto Delmás's avatar
Alberto Delmás committed
830
    c->pal_stride   = c->mask_stride;
831 832
    c->pal_pic      = av_mallocz(c->pal_stride * avctx->height);
    c->last_pal_pic = av_mallocz(c->pal_stride * avctx->height);
833
    if (!c->pal_pic || !c->last_pal_pic || !ctx->last_pic) {
Alberto Delmás's avatar
Alberto Delmás committed
834 835 836 837 838 839 840 841
        mss2_decode_end(avctx);
        return AVERROR(ENOMEM);
    }
    if (ret = wmv9_init(avctx)) {
        mss2_decode_end(avctx);
        return ret;
    }
    ff_mss2dsp_init(&ctx->dsp);
842
    ff_qpeldsp_init(&ctx->qdsp);
843

844 845
    avctx->pix_fmt = c->free_colours == 127 ? AV_PIX_FMT_RGB555
                                            : AV_PIX_FMT_RGB24;
846

847

Alberto Delmás's avatar
Alberto Delmás committed
848 849 850 851 852
    return 0;
}

AVCodec ff_mss2_decoder = {
    .name           = "mss2",
853
    .long_name      = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
Alberto Delmás's avatar
Alberto Delmás committed
854 855 856 857 858 859
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = AV_CODEC_ID_MSS2,
    .priv_data_size = sizeof(MSS2Context),
    .init           = mss2_decode_init,
    .close          = mss2_decode_end,
    .decode         = mss2_decode_frame,
860
    .capabilities   = AV_CODEC_CAP_DR1,
Alberto Delmás's avatar
Alberto Delmás committed
861
};