vc1.c 152 KB
Newer Older
1
/*
2
 * VC-1 and WMV3 decoder
3
 * Copyright (c) 2006-2007 Konstantin Shishkov
4
 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14 15 16 17 18
 * 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
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 22 23
 */

/**
24 25
 * @file vc1.c
 * VC-1 and WMV3 decoder
26 27 28 29 30
 *
 */
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
31
#include "vc1.h"
32
#include "vc1data.h"
33
#include "vc1acdata.h"
34
#include "msmpeg4data.h"
35
#include "unary.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
36
#include "simple_idct.h"
anonymous's avatar
anonymous committed
37

38 39 40
#undef NDEBUG
#include <assert.h>

anonymous's avatar
anonymous committed
41 42
#define MB_INTRA_VLC_BITS 9
#define DC_VLC_BITS 9
43
#define AC_VLC_BITS 9
anonymous's avatar
anonymous committed
44
static const uint16_t table_mb_intra[64][2];
45 46


anonymous's avatar
anonymous committed
47
/**
48 49
 * Init VC-1 specific tables and VC1Context members
 * @param v The VC1Context to initialize
anonymous's avatar
anonymous committed
50 51
 * @return Status
 */
52
static int vc1_init_common(VC1Context *v)
53 54
{
    static int done = 0;
55
    int i = 0;
56 57

    v->hrd_rate = v->hrd_buffer = NULL;
anonymous's avatar
Fixes:  
anonymous committed
58 59

    /* VLC tables */
60 61 62
    if(!done)
    {
        done = 1;
63 64 65 66 67 68 69 70 71 72 73 74
        init_vlc(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
                 ff_vc1_bfraction_bits, 1, 1,
                 ff_vc1_bfraction_codes, 1, 1, 1);
        init_vlc(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
                 ff_vc1_norm2_bits, 1, 1,
                 ff_vc1_norm2_codes, 1, 1, 1);
        init_vlc(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
                 ff_vc1_norm6_bits, 1, 1,
                 ff_vc1_norm6_codes, 2, 2, 1);
        init_vlc(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
                 ff_vc1_imode_bits, 1, 1,
                 ff_vc1_imode_codes, 1, 1, 1);
anonymous's avatar
Fixes:  
anonymous committed
75 76
        for (i=0; i<3; i++)
        {
77 78 79 80 81 82 83 84 85
            init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
                     ff_vc1_ttmb_bits[i], 1, 1,
                     ff_vc1_ttmb_codes[i], 2, 2, 1);
            init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
                     ff_vc1_ttblk_bits[i], 1, 1,
                     ff_vc1_ttblk_codes[i], 1, 1, 1);
            init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
                     ff_vc1_subblkpat_bits[i], 1, 1,
                     ff_vc1_subblkpat_codes[i], 1, 1, 1);
anonymous's avatar
Fixes:  
anonymous committed
86 87
        }
        for(i=0; i<4; i++)
88
        {
89 90 91 92 93 94 95 96 97
            init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
                     ff_vc1_4mv_block_pattern_bits[i], 1, 1,
                     ff_vc1_4mv_block_pattern_codes[i], 1, 1, 1);
            init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
                     ff_vc1_cbpcy_p_bits[i], 1, 1,
                     ff_vc1_cbpcy_p_codes[i], 2, 2, 1);
            init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
                     ff_vc1_mv_diff_bits[i], 1, 1,
                     ff_vc1_mv_diff_codes[i], 2, 2, 1);
98
        }
99
        for(i=0; i<8; i++)
100
            init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
101 102 103 104 105
                     &vc1_ac_tables[i][0][1], 8, 4,
                     &vc1_ac_tables[i][0][0], 8, 4, 1);
        init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
                 &ff_msmp4_mb_i_table[0][1], 4, 2,
                 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
106 107
    }

anonymous's avatar
Fixes:  
anonymous committed
108 109 110 111
    /* Other defaults */
    v->pq = -1;
    v->mvrange = 0; /* 7.1.1.18, p80 */

112 113 114
    return 0;
}

115
/***********************************************************************/
anonymous's avatar
anonymous committed
116
/**
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
 * @defgroup bitplane VC9 Bitplane decoding
 * @see 8.7, p56
 * @{
 */

/** @addtogroup bitplane
 * Imode types
 * @{
 */
enum Imode {
    IMODE_RAW,
    IMODE_NORM2,
    IMODE_DIFF2,
    IMODE_NORM6,
    IMODE_DIFF6,
    IMODE_ROWSKIP,
    IMODE_COLSKIP
};
/** @} */ //imode defines

/** Decode rows by checking if they are skipped
 * @param plane Buffer to store decoded bits
 * @param[in] width Width of this buffer
 * @param[in] height Height of this buffer
 * @param[in] stride of this buffer
 */
static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
    int x, y;
145

146
    for (y=0; y<height; y++){
147
        if (!get_bits1(gb)) //rowskip
148 149 150
            memset(plane, 0, width);
        else
            for (x=0; x<width; x++)
151
                plane[x] = get_bits1(gb);
152
        plane += stride;
153
    }
154
}
155

156 157 158 159 160
/** Decode columns by checking if they are skipped
 * @param plane Buffer to store decoded bits
 * @param[in] width Width of this buffer
 * @param[in] height Height of this buffer
 * @param[in] stride of this buffer
161
 * @todo FIXME: Optimize
162 163 164
 */
static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
    int x, y;
165

166
    for (x=0; x<width; x++){
167
        if (!get_bits1(gb)) //colskip
168 169 170 171
            for (y=0; y<height; y++)
                plane[y*stride] = 0;
        else
            for (y=0; y<height; y++)
172
                plane[y*stride] = get_bits1(gb);
173
        plane ++;
174 175 176
    }
}

177 178 179
/** Decode a bitplane's bits
 * @param bp Bitplane where to store the decode bits
 * @param v VC-1 context for bit reading and logging
anonymous's avatar
anonymous committed
180
 * @return Status
181
 * @todo FIXME: Optimize
anonymous's avatar
anonymous committed
182
 */
183
static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
184
{
185
    GetBitContext *gb = &v->s.gb;
186

187
    int imode, x, y, code, offset;
188 189
    uint8_t invert, *planep = data;
    int width, height, stride;
190

191 192 193
    width = v->s.mb_width;
    height = v->s.mb_height;
    stride = v->s.mb_stride;
194
    invert = get_bits1(gb);
195
    imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
196

197
    *raw_flag = 0;
198
    switch (imode)
199
    {
200 201
    case IMODE_RAW:
        //Data is actually read in the MB layer (same for all tests == "raw")
202
        *raw_flag = 1; //invert ignored
203 204 205
        return invert;
    case IMODE_DIFF2:
    case IMODE_NORM2:
206
        if ((height * width) & 1)
207
        {
208
            *planep++ = get_bits1(gb);
209
            offset = 1;
210
        }
211 212
        else offset = 0;
        // decode bitplane as one long line
213
        for (y = offset; y < height * width; y += 2) {
214
            code = get_vlc2(gb, ff_vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
215 216
            *planep++ = code & 1;
            offset++;
217
            if(offset == width) {
218
                offset = 0;
219
                planep += stride - width;
220
            }
221 222
            *planep++ = code >> 1;
            offset++;
223
            if(offset == width) {
224
                offset = 0;
225
                planep += stride - width;
226
            }
227 228 229 230
        }
        break;
    case IMODE_DIFF6:
    case IMODE_NORM6:
231 232 233
        if(!(height % 3) && (width % 3)) { // use 2x3 decoding
            for(y = 0; y < height; y+= 3) {
                for(x = width & 1; x < width; x += 2) {
234
                    code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
235 236 237 238 239 240
                    if(code < 0){
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
                        return -1;
                    }
                    planep[x + 0] = (code >> 0) & 1;
                    planep[x + 1] = (code >> 1) & 1;
241 242 243 244
                    planep[x + 0 + stride] = (code >> 2) & 1;
                    planep[x + 1 + stride] = (code >> 3) & 1;
                    planep[x + 0 + stride * 2] = (code >> 4) & 1;
                    planep[x + 1 + stride * 2] = (code >> 5) & 1;
245
                }
246
                planep += stride * 3;
247
            }
248
            if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
249
        } else { // 3x2
250
            planep += (height & 1) * stride;
251 252
            for(y = height & 1; y < height; y += 2) {
                for(x = width % 3; x < width; x += 3) {
253
                    code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
254 255 256 257 258 259 260
                    if(code < 0){
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
                        return -1;
                    }
                    planep[x + 0] = (code >> 0) & 1;
                    planep[x + 1] = (code >> 1) & 1;
                    planep[x + 2] = (code >> 2) & 1;
261 262 263
                    planep[x + 0 + stride] = (code >> 3) & 1;
                    planep[x + 1 + stride] = (code >> 4) & 1;
                    planep[x + 2 + stride] = (code >> 5) & 1;
264
                }
265
                planep += stride * 2;
266
            }
267 268 269
            x = width % 3;
            if(x) decode_colskip(data  ,             x, height    , stride, &v->s.gb);
            if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
270 271 272
        }
        break;
    case IMODE_ROWSKIP:
273
        decode_rowskip(data, width, height, stride, &v->s.gb);
274 275
        break;
    case IMODE_COLSKIP:
276
        decode_colskip(data, width, height, stride, &v->s.gb);
277 278 279 280 281 282 283
        break;
    default: break;
    }

    /* Applying diff operator */
    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
    {
284
        planep = data;
285
        planep[0] ^= invert;
286
        for (x=1; x<width; x++)
287
            planep[x] ^= planep[x-1];
288
        for (y=1; y<height; y++)
289
        {
290 291 292
            planep += stride;
            planep[0] ^= planep[-stride];
            for (x=1; x<width; x++)
293
            {
294 295
                if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
                else                                 planep[x] ^= planep[x-1];
296 297 298
            }
        }
    }
299
    else if (invert)
300
    {
301 302
        planep = data;
        for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
303
    }
304 305
    return (imode<<1) + invert;
}
306

307
/** @} */ //Bitplane group
308

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 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
#define FILTSIGN(a) ((a) >= 0 ? 1 : -1)
/**
 * VC-1 in-loop deblocking filter for one line
 * @param src source block type
 * @param pq block quantizer
 * @return whether other 3 pairs should be filtered or not
 * @see 8.6
 */
static int vc1_filter_line(uint8_t* src, int stride, int pq){
    int a0, a1, a2, a3, d, clip, filt3 = 0;
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;

    a0     = (2*(src[-2*stride] - src[ 1*stride]) - 5*(src[-1*stride] - src[ 0*stride]) + 4) >> 3;
    if(FFABS(a0) < pq){
        a1 = (2*(src[-4*stride] - src[-1*stride]) - 5*(src[-3*stride] - src[-2*stride]) + 4) >> 3;
        a2 = (2*(src[ 0*stride] - src[ 3*stride]) - 5*(src[ 1*stride] - src[ 2*stride]) + 4) >> 3;
        a3 = FFMIN(FFABS(a1), FFABS(a2));
        if(a3 < FFABS(a0)){
            d = 5 * ((a0 >=0 ? a3 : -a3) - a0) / 8;
            clip = (src[-1*stride] - src[ 0*stride])/2;
            if(clip){
                filt3 = 1;
                if(clip > 0)
                    d = av_clip(d, 0, clip);
                else
                    d = av_clip(d, clip, 0);
                src[-1*stride] = cm[src[-1*stride] - d];
                src[ 0*stride] = cm[src[ 0*stride] + d];
            }
        }
    }
    return filt3;
}

/**
 * VC-1 in-loop deblocking filter
 * @param src source block type
 * @param len edge length to filter (4 or 8 pixels)
 * @param pq block quantizer
 * @see 8.6
 */
static void vc1_loop_filter(uint8_t* src, int step, int stride, int len, int pq)
{
    int i;
    int filt3;

    for(i = 0; i < len; i += 4){
        filt3 = vc1_filter_line(src + 2*step, stride, pq);
        if(filt3){
            vc1_filter_line(src + 0*step, stride, pq);
            vc1_filter_line(src + 1*step, stride, pq);
            vc1_filter_line(src + 3*step, stride, pq);
        }
        src += step * 4;
    }
}

static void vc1_loop_filter_iblk(MpegEncContext *s, int pq)
{
    int i, j;
    if(!s->first_slice_line)
        vc1_loop_filter(s->dest[0], 1, s->linesize, 16, pq);
    vc1_loop_filter(s->dest[0] + 8*s->linesize, 1, s->linesize, 16, pq);
    for(i = !s->mb_x*8; i < 16; i += 8)
        vc1_loop_filter(s->dest[0] + i, s->linesize, 1, 16, pq);
    for(j = 0; j < 2; j++){
        if(!s->first_slice_line)
            vc1_loop_filter(s->dest[j+1], 1, s->uvlinesize, 8, pq);
        if(s->mb_x)
            vc1_loop_filter(s->dest[j+1], s->uvlinesize, 1, 8, pq);
    }
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
/***********************************************************************/
/** VOP Dquant decoding
 * @param v VC-1 Context
 */
static int vop_dquant_decoding(VC1Context *v)
{
    GetBitContext *gb = &v->s.gb;
    int pqdiff;

    //variable size
    if (v->dquant == 2)
    {
        pqdiff = get_bits(gb, 3);
        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
        else v->altpq = v->pq + pqdiff + 1;
    }
    else
399
    {
400
        v->dquantfrm = get_bits1(gb);
401
        if ( v->dquantfrm )
402
        {
403 404
            v->dqprofile = get_bits(gb, 2);
            switch (v->dqprofile)
405
            {
406 407 408 409 410
            case DQPROFILE_SINGLE_EDGE:
            case DQPROFILE_DOUBLE_EDGES:
                v->dqsbedge = get_bits(gb, 2);
                break;
            case DQPROFILE_ALL_MBS:
411
                v->dqbilevel = get_bits1(gb);
412 413
                if(!v->dqbilevel)
                    v->halfpq = 0;
414
            default: break; //Forbidden ?
Ivan Kalvachev's avatar
Ivan Kalvachev committed
415
            }
416
            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
417
            {
418 419 420
                pqdiff = get_bits(gb, 3);
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
                else v->altpq = v->pq + pqdiff + 1;
421 422 423
            }
        }
    }
424 425
    return 0;
}
426

427 428 429 430 431 432 433 434
/** Put block onto picture
 */
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
{
    uint8_t *Y;
    int ys, us, vs;
    DSPContext *dsp = &v->s.dsp;

Kostya Shishkov's avatar
Kostya Shishkov committed
435 436 437 438 439 440 441 442
    if(v->rangeredfrm) {
        int i, j, k;
        for(k = 0; k < 6; k++)
            for(j = 0; j < 8; j++)
                for(i = 0; i < 8; i++)
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;

    }
443 444 445 446 447 448 449 450 451 452 453
    ys = v->s.current_picture.linesize[0];
    us = v->s.current_picture.linesize[1];
    vs = v->s.current_picture.linesize[2];
    Y = v->s.dest[0];

    dsp->put_pixels_clamped(block[0], Y, ys);
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
    Y += ys * 8;
    dsp->put_pixels_clamped(block[2], Y, ys);
    dsp->put_pixels_clamped(block[3], Y + 8, ys);

Kostya Shishkov's avatar
Kostya Shishkov committed
454 455 456 457
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
    }
458 459 460 461 462
}

/** Do motion compensation over 1 macroblock
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
 */
463
static void vc1_mc_1mv(VC1Context *v, int dir)
464 465 466 467
{
    MpegEncContext *s = &v->s;
    DSPContext *dsp = &v->s.dsp;
    uint8_t *srcY, *srcU, *srcV;
468
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
469 470 471

    if(!v->s.last_picture.data[0])return;

472 473 474 475
    mx = s->mv[dir][0][0];
    my = s->mv[dir][0][1];

    // store motion vectors for further use in B frames
476
    if(s->pict_type == FF_P_TYPE) {
477 478 479
        s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
        s->current_picture.motion_val[1][s->block_index[0]][1] = my;
    }
480 481
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
    uvmy = (my + ((my & 3) == 3)) >> 1;
482 483 484 485
    if(v->fastuvmc) {
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
    }
486 487 488 489 490 491 492 493 494
    if(!dir) {
        srcY = s->last_picture.data[0];
        srcU = s->last_picture.data[1];
        srcV = s->last_picture.data[2];
    } else {
        srcY = s->next_picture.data[0];
        srcU = s->next_picture.data[1];
        srcV = s->next_picture.data[2];
    }
495

496 497 498 499 500
    src_x = s->mb_x * 16 + (mx >> 2);
    src_y = s->mb_y * 16 + (my >> 2);
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);

Kostya Shishkov's avatar
Kostya Shishkov committed
501 502 503 504 505 506 507 508 509 510 511
    if(v->profile != PROFILE_ADVANCED){
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
    }else{
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
    }
512 513 514 515 516

    srcY += src_y * s->linesize + src_x;
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;

Kostya Shishkov's avatar
Kostya Shishkov committed
517 518 519 520 521 522
    /* for grayscale we should not try to read from unknown area */
    if(s->flags & CODEC_FLAG_GRAY) {
        srcU = s->edge_emu_buffer + 18 * s->linesize;
        srcV = s->edge_emu_buffer + 18 * s->linesize;
    }

Kostya Shishkov's avatar
Kostya Shishkov committed
523
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
524 525 526
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
527

528 529 530
        srcY -= s->mspel * (1 + s->linesize);
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
531 532 533 534 535 536 537
        srcY = s->edge_emu_buffer;
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
        srcU = uvbuf;
        srcV = uvbuf + 16;
Kostya Shishkov's avatar
Kostya Shishkov committed
538 539 540 541 542 543
        /* if we deal with range reduction we need to scale source blocks */
        if(v->rangeredfrm) {
            int i, j;
            uint8_t *src, *src2;

            src = srcY;
544 545
            for(j = 0; j < 17 + s->mspel*2; j++) {
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
Kostya Shishkov's avatar
Kostya Shishkov committed
546 547 548 549 550 551 552 553 554 555 556 557
                src += s->linesize;
            }
            src = srcU; src2 = srcV;
            for(j = 0; j < 9; j++) {
                for(i = 0; i < 9; i++) {
                    src[i] = ((src[i] - 128) >> 1) + 128;
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
                }
                src += s->uvlinesize;
                src2 += s->uvlinesize;
            }
        }
558 559 560 561 562 563
        /* if we deal with intensity compensation we need to scale source blocks */
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
            int i, j;
            uint8_t *src, *src2;

            src = srcY;
564 565
            for(j = 0; j < 17 + s->mspel*2; j++) {
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
566 567 568 569 570 571 572 573 574 575 576 577
                src += s->linesize;
            }
            src = srcU; src2 = srcV;
            for(j = 0; j < 9; j++) {
                for(i = 0; i < 9; i++) {
                    src[i] = v->lutuv[src[i]];
                    src2[i] = v->lutuv[src2[i]];
                }
                src += s->uvlinesize;
                src2 += s->uvlinesize;
            }
        }
578
        srcY += s->mspel * (1 + s->linesize);
579 580
    }

581 582 583 584 585 586 587
    if(s->mspel) {
        dxy = ((my & 3) << 2) | (mx & 3);
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
        srcY += s->linesize * 8;
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
588 589
    } else { // hpel mc - always used for luma
        dxy = (my & 2) | ((mx & 2) >> 1);
590

591 592 593 594 595
        if(!v->rnd)
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
        else
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
    }
Kostya Shishkov's avatar
Kostya Shishkov committed
596 597

    if(s->flags & CODEC_FLAG_GRAY) return;
Kostya Shishkov's avatar
Kostya Shishkov committed
598
    /* Chroma MC always uses qpel bilinear */
599
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
600 601 602 603 604 605 606 607 608
    uvmx = (uvmx&3)<<1;
    uvmy = (uvmy&3)<<1;
    if(!v->rnd){
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
    }else{
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
    }
609 610
}

Kostya Shishkov's avatar
Kostya Shishkov committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
/** Do motion compensation for 4-MV macroblock - luminance block
 */
static void vc1_mc_4mv_luma(VC1Context *v, int n)
{
    MpegEncContext *s = &v->s;
    DSPContext *dsp = &v->s.dsp;
    uint8_t *srcY;
    int dxy, mx, my, src_x, src_y;
    int off;

    if(!v->s.last_picture.data[0])return;
    mx = s->mv[0][n][0];
    my = s->mv[0][n][1];
    srcY = s->last_picture.data[0];

    off = s->linesize * 4 * (n&2) + (n&1) * 8;

    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);

Kostya Shishkov's avatar
Kostya Shishkov committed
631 632 633 634 635 636 637
    if(v->profile != PROFILE_ADVANCED){
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
    }else{
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
    }
Kostya Shishkov's avatar
Kostya Shishkov committed
638 639 640

    srcY += src_y * s->linesize + src_x;

641
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
642 643
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
644 645 646
        srcY -= s->mspel * (1 + s->linesize);
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
Kostya Shishkov's avatar
Kostya Shishkov committed
647
        srcY = s->edge_emu_buffer;
Kostya Shishkov's avatar
Kostya Shishkov committed
648 649 650 651 652 653
        /* if we deal with range reduction we need to scale source blocks */
        if(v->rangeredfrm) {
            int i, j;
            uint8_t *src;

            src = srcY;
654 655
            for(j = 0; j < 9 + s->mspel*2; j++) {
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
Kostya Shishkov's avatar
Kostya Shishkov committed
656 657 658
                src += s->linesize;
            }
        }
659 660 661 662 663 664 665 666 667 668 669
        /* if we deal with intensity compensation we need to scale source blocks */
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
            int i, j;
            uint8_t *src;

            src = srcY;
            for(j = 0; j < 9 + s->mspel*2; j++) {
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
                src += s->linesize;
            }
        }
670
        srcY += s->mspel * (1 + s->linesize);
Kostya Shishkov's avatar
Kostya Shishkov committed
671 672
    }

673 674 675
    if(s->mspel) {
        dxy = ((my & 3) << 2) | (mx & 3);
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
676 677
    } else { // hpel mc - always used for luma
        dxy = (my & 2) | ((mx & 2) >> 1);
678 679 680 681
        if(!v->rnd)
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
        else
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
Kostya Shishkov's avatar
Kostya Shishkov committed
682 683 684 685 686
    }
}

static inline int median4(int a, int b, int c, int d)
{
687
    if(a < b) {
688 689
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
690
    } else {
691 692
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
693
    }
Kostya Shishkov's avatar
Kostya Shishkov committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
}


/** Do motion compensation for 4-MV macroblock - both chroma blocks
 */
static void vc1_mc_4mv_chroma(VC1Context *v)
{
    MpegEncContext *s = &v->s;
    DSPContext *dsp = &v->s.dsp;
    uint8_t *srcU, *srcV;
    int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
    int i, idx, tx = 0, ty = 0;
    int mvx[4], mvy[4], intra[4];
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};

    if(!v->s.last_picture.data[0])return;
Kostya Shishkov's avatar
Kostya Shishkov committed
710
    if(s->flags & CODEC_FLAG_GRAY) return;
Kostya Shishkov's avatar
Kostya Shishkov committed
711 712 713 714 715 716 717 718

    for(i = 0; i < 4; i++) {
        mvx[i] = s->mv[0][i][0];
        mvy[i] = s->mv[0][i][1];
        intra[i] = v->mb_type[0][s->block_index[i]];
    }

    /* calculate chroma MV vector from four luma MVs */
719
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
Kostya Shishkov's avatar
Kostya Shishkov committed
720 721 722 723 724 725 726
    if(!idx) { // all blocks are inter
        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
    } else if(count[idx] == 1) { // 3 inter blocks
        switch(idx) {
        case 0x1:
            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
727
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
Kostya Shishkov's avatar
Kostya Shishkov committed
728 729 730
            break;
        case 0x2:
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
731
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
Kostya Shishkov's avatar
Kostya Shishkov committed
732 733 734
            break;
        case 0x4:
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
735
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
Kostya Shishkov's avatar
Kostya Shishkov committed
736 737 738
            break;
        case 0x8:
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
739
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
Kostya Shishkov's avatar
Kostya Shishkov committed
740 741 742 743 744 745
            break;
        }
    } else if(count[idx] == 2) {
        int t1 = 0, t2 = 0;
        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
746 747
        tx = (mvx[t1] + mvx[t2]) / 2;
        ty = (mvy[t1] + mvy[t2]) / 2;
748 749 750
    } else {
        s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
        s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
Kostya Shishkov's avatar
Kostya Shishkov committed
751
        return; //no need to do MC for inter blocks
752
    }
Kostya Shishkov's avatar
Kostya Shishkov committed
753

754 755
    s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
    s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
Kostya Shishkov's avatar
Kostya Shishkov committed
756 757
    uvmx = (tx + ((tx&3) == 3)) >> 1;
    uvmy = (ty + ((ty&3) == 3)) >> 1;
758 759 760 761
    if(v->fastuvmc) {
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
    }
Kostya Shishkov's avatar
Kostya Shishkov committed
762 763 764 765

    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);

Kostya Shishkov's avatar
Kostya Shishkov committed
766 767 768 769 770 771 772
    if(v->profile != PROFILE_ADVANCED){
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
    }else{
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
    }
773

Kostya Shishkov's avatar
Kostya Shishkov committed
774 775
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
776 777
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
       || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
Kostya Shishkov's avatar
Kostya Shishkov committed
778
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
Kostya Shishkov's avatar
Kostya Shishkov committed
779 780 781 782 783 784
        ff_emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
        ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
        srcU = s->edge_emu_buffer;
        srcV = s->edge_emu_buffer + 16;
Kostya Shishkov's avatar
Kostya Shishkov committed
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800

        /* if we deal with range reduction we need to scale source blocks */
        if(v->rangeredfrm) {
            int i, j;
            uint8_t *src, *src2;

            src = srcU; src2 = srcV;
            for(j = 0; j < 9; j++) {
                for(i = 0; i < 9; i++) {
                    src[i] = ((src[i] - 128) >> 1) + 128;
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
                }
                src += s->uvlinesize;
                src2 += s->uvlinesize;
            }
        }
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
        /* if we deal with intensity compensation we need to scale source blocks */
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
            int i, j;
            uint8_t *src, *src2;

            src = srcU; src2 = srcV;
            for(j = 0; j < 9; j++) {
                for(i = 0; i < 9; i++) {
                    src[i] = v->lutuv[src[i]];
                    src2[i] = v->lutuv[src2[i]];
                }
                src += s->uvlinesize;
                src2 += s->uvlinesize;
            }
        }
Kostya Shishkov's avatar
Kostya Shishkov committed
816 817
    }

Kostya Shishkov's avatar
Kostya Shishkov committed
818
    /* Chroma MC always uses qpel bilinear */
819
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
820 821 822 823 824 825 826 827 828
    uvmx = (uvmx&3)<<1;
    uvmy = (uvmy&3)<<1;
    if(!v->rnd){
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
    }else{
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
    }
Kostya Shishkov's avatar
Kostya Shishkov committed
829 830
}

831 832
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);

833
/**
anonymous's avatar
anonymous committed
834 835 836 837 838 839
 * Decode Simple/Main Profiles sequence header
 * @see Figure 7-8, p16-17
 * @param avctx Codec context
 * @param gb GetBit context initialized from Codec context extra_data
 * @return Status
 */
840 841
static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
{
842
    VC1Context *v = avctx->priv_data;
843

844
    av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
845
    v->profile = get_bits(gb, 2);
846
    if (v->profile == PROFILE_COMPLEX)
847
    {
848
        av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
849
    }
850

851
    if (v->profile == PROFILE_ADVANCED)
852
    {
853 854
        v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
        v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
855
        return decode_sequence_header_adv(v, gb);
856 857 858
    }
    else
    {
Stefan Gehrer's avatar
Stefan Gehrer committed
859 860
        v->zz_8x4 = wmv2_scantableA;
        v->zz_4x8 = wmv2_scantableB;
861 862 863 864 865
        v->res_sm = get_bits(gb, 2); //reserved
        if (v->res_sm)
        {
            av_log(avctx, AV_LOG_ERROR,
                   "Reserved RES_SM=%i is forbidden\n", v->res_sm);
866
            return -1;
867 868 869 870 871 872 873
        }
    }

    // (fps-2)/4 (->30)
    v->frmrtq_postproc = get_bits(gb, 3); //common
    // (bitrate-32kbps)/64kbps
    v->bitrtq_postproc = get_bits(gb, 5); //common
874
    v->s.loop_filter = get_bits1(gb); //common
875 876 877 878 879
    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
    {
        av_log(avctx, AV_LOG_ERROR,
               "LOOPFILTER shell not be enabled in simple profile\n");
    }
880 881
    if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
        v->s.loop_filter = 0;
882

883 884 885
    v->res_x8 = get_bits1(gb); //reserved
    v->multires = get_bits1(gb);
    v->res_fasttx = get_bits1(gb);
886 887
    if (!v->res_fasttx)
    {
888 889 890
        v->s.dsp.vc1_inv_trans_8x8 = ff_simple_idct;
        v->s.dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
        v->s.dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
891
        v->s.dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
892 893
    }

894
    v->fastuvmc =  get_bits1(gb); //common
895 896 897 898 899 900
    if (!v->profile && !v->fastuvmc)
    {
        av_log(avctx, AV_LOG_ERROR,
               "FASTUVMC unavailable in Simple Profile\n");
        return -1;
    }
901
    v->extended_mv =  get_bits1(gb); //common
902 903 904 905 906 907 908
    if (!v->profile && v->extended_mv)
    {
        av_log(avctx, AV_LOG_ERROR,
               "Extended MVs unavailable in Simple Profile\n");
        return -1;
    }
    v->