vc1.c 155 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
 * @file libavcodec/vc1.c
25
 * VC-1 and WMV3 decoder
26 27
 *
 */
28
#include "internal.h"
29 30 31
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
32
#include "vc1.h"
33
#include "vc1data.h"
34
#include "vc1acdata.h"
35
#include "msmpeg4data.h"
36
#include "unary.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
37
#include "simple_idct.h"
38
#include "mathops.h"
39
#include "vdpau_internal.h"
anonymous's avatar
anonymous committed
40

41 42 43
#undef NDEBUG
#include <assert.h>

anonymous's avatar
anonymous committed
44 45
#define MB_INTRA_VLC_BITS 9
#define DC_VLC_BITS 9
46
#define AC_VLC_BITS 9
anonymous's avatar
anonymous committed
47
static const uint16_t table_mb_intra[64][2];
48 49


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

    v->hrd_rate = v->hrd_buffer = NULL;
anonymous's avatar
Fixes:  
anonymous committed
61 62

    /* VLC tables */
63 64 65
    if(!done)
    {
        done = 1;
66 67 68 69 70 71 72 73 74 75 76 77
        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
78 79
        for (i=0; i<3; i++)
        {
80 81 82 83 84 85 86 87 88
            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
89 90
        }
        for(i=0; i<4; i++)
91
        {
92 93 94 95 96 97 98 99 100
            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);
101
        }
102
        for(i=0; i<8; i++)
103
            init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
104 105 106 107 108
                     &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);
109 110
    }

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

115 116 117
    return 0;
}

118
/***********************************************************************/
anonymous's avatar
anonymous committed
119
/**
120
 * @defgroup vc1bitplane VC-1 Bitplane decoding
121 122 123 124
 * @see 8.7, p56
 * @{
 */

125
/**
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
 * 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;
148

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

159 160 161 162 163
/** 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
164
 * @todo FIXME: Optimize
165 166 167
 */
static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
    int x, y;
168

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

180
/** Decode a bitplane's bits
181 182
 * @param data bitplane where to store the decode bits
 * @param[out] raw_flag pointer to the flag indicating that this bitplane is not coded explicitly
183
 * @param v VC-1 context for bit reading and logging
anonymous's avatar
anonymous committed
184
 * @return Status
185
 * @todo FIXME: Optimize
anonymous's avatar
anonymous committed
186
 */
187
static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
188
{
189
    GetBitContext *gb = &v->s.gb;
190

191
    int imode, x, y, code, offset;
192 193
    uint8_t invert, *planep = data;
    int width, height, stride;
194

195 196 197
    width = v->s.mb_width;
    height = v->s.mb_height;
    stride = v->s.mb_stride;
198
    invert = get_bits1(gb);
199
    imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
200

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

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

311
/** @} */ //Bitplane group
312

313 314 315
/**
 * VC-1 in-loop deblocking filter for one line
 * @param src source block type
316
 * @param stride block stride
317 318 319 320
 * @param pq block quantizer
 * @return whether other 3 pairs should be filtered or not
 * @see 8.6
 */
321
static av_always_inline int vc1_filter_line(uint8_t* src, int stride, int pq){
322 323
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;

324 325 326 327 328 329 330 331 332 333
    int a0 = (2*(src[-2*stride] - src[ 1*stride]) - 5*(src[-1*stride] - src[ 0*stride]) + 4) >> 3;
    int a0_sign = a0 >> 31;        /* Store sign */
    a0 = (a0 ^ a0_sign) - a0_sign; /* a0 = FFABS(a0); */
    if(a0 < pq){
        int a1 = FFABS((2*(src[-4*stride] - src[-1*stride]) - 5*(src[-3*stride] - src[-2*stride]) + 4) >> 3);
        int a2 = FFABS((2*(src[ 0*stride] - src[ 3*stride]) - 5*(src[ 1*stride] - src[ 2*stride]) + 4) >> 3);
        if(a1 < a0 || a2 < a0){
            int clip = src[-1*stride] - src[ 0*stride];
            int clip_sign = clip >> 31;
            clip = ((clip ^ clip_sign) - clip_sign)>>1;
334
            if(clip){
335 336 337 338 339 340
                int a3 = FFMIN(a1, a2);
                int d = 5 * (a3 - a0);
                int d_sign = (d >> 31);
                d = ((d ^ d_sign) - d_sign) >> 3;
                d_sign ^= a0_sign;

341
                if( d_sign ^ clip_sign )
342 343 344 345 346 347 348 349
                    d = 0;
                else{
                    d = FFMIN(d, clip);
                    d = (d ^ d_sign) - d_sign;          /* Restore sign */
                    src[-1*stride] = cm[src[-1*stride] - d];
                    src[ 0*stride] = cm[src[ 0*stride] + d];
                }
                return 1;
350 351 352
            }
        }
    }
353
    return 0;
354 355 356 357 358
}

/**
 * VC-1 in-loop deblocking filter
 * @param src source block type
359 360
 * @param step distance between horizontally adjacent elements
 * @param stride distance between vertically adjacent elements
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
 * @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);
    }
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/***********************************************************************/
/** 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
414
    {
415
        v->dquantfrm = get_bits1(gb);
416
        if ( v->dquantfrm )
417
        {
418 419
            v->dqprofile = get_bits(gb, 2);
            switch (v->dqprofile)
420
            {
421 422 423 424 425
            case DQPROFILE_SINGLE_EDGE:
            case DQPROFILE_DOUBLE_EDGES:
                v->dqsbedge = get_bits(gb, 2);
                break;
            case DQPROFILE_ALL_MBS:
426
                v->dqbilevel = get_bits1(gb);
427 428
                if(!v->dqbilevel)
                    v->halfpq = 0;
429
            default: break; //Forbidden ?
Ivan Kalvachev's avatar
Ivan Kalvachev committed
430
            }
431
            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
432
            {
433 434 435
                pqdiff = get_bits(gb, 3);
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
                else v->altpq = v->pq + pqdiff + 1;
436 437 438
            }
        }
    }
439 440
    return 0;
}
441

442 443 444 445 446 447 448 449
/** 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
450 451 452 453 454 455 456 457
    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;

    }
458 459 460 461 462 463 464 465 466 467 468
    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
469 470 471 472
    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);
    }
473 474 475 476 477
}

/** Do motion compensation over 1 macroblock
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
 */
478
static void vc1_mc_1mv(VC1Context *v, int dir)
479 480 481 482
{
    MpegEncContext *s = &v->s;
    DSPContext *dsp = &v->s.dsp;
    uint8_t *srcY, *srcU, *srcV;
483
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
484 485 486

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

487 488 489 490
    mx = s->mv[dir][0][0];
    my = s->mv[dir][0][1];

    // store motion vectors for further use in B frames
491
    if(s->pict_type == FF_P_TYPE) {
492 493 494
        s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
        s->current_picture.motion_val[1][s->block_index[0]][1] = my;
    }
495 496
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
    uvmy = (my + ((my & 3) == 3)) >> 1;
497 498 499 500
    if(v->fastuvmc) {
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
    }
501 502 503 504 505 506 507 508 509
    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];
    }
510

511 512 513 514 515
    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
516 517 518 519 520 521 522 523 524 525 526
    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);
    }
527 528 529 530 531

    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
532 533 534 535 536 537
    /* 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
538
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
539 540 541
       || (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;
542

543 544 545
        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);
546 547 548 549 550 551 552
        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
553 554 555 556 557 558
        /* if we deal with range reduction we need to scale source blocks */
        if(v->rangeredfrm) {
            int i, j;
            uint8_t *src, *src2;

            src = srcY;
559 560
            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
561 562 563 564 565 566 567 568 569 570 571 572
                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;
            }
        }
573 574 575 576 577 578
        /* 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;
579 580
            for(j = 0; j < 17 + s->mspel*2; j++) {
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
581 582 583 584 585 586 587 588 589 590 591 592
                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;
            }
        }
593
        srcY += s->mspel * (1 + s->linesize);
594 595
    }

596 597 598 599 600 601 602
    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);
603 604
    } else { // hpel mc - always used for luma
        dxy = (my & 2) | ((mx & 2) >> 1);
605

606 607 608 609 610
        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
611 612

    if(s->flags & CODEC_FLAG_GRAY) return;
Kostya Shishkov's avatar
Kostya Shishkov committed
613
    /* Chroma MC always uses qpel bilinear */
614
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
615 616 617 618 619 620 621 622 623
    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);
    }
624 625
}

Kostya Shishkov's avatar
Kostya Shishkov committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
/** 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
646 647 648 649 650 651 652
    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
653 654 655

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

656
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
657 658
       || (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){
659 660 661
        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
662
        srcY = s->edge_emu_buffer;
Kostya Shishkov's avatar
Kostya Shishkov committed
663 664 665 666 667 668
        /* if we deal with range reduction we need to scale source blocks */
        if(v->rangeredfrm) {
            int i, j;
            uint8_t *src;

            src = srcY;
669 670
            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
671 672 673
                src += s->linesize;
            }
        }
674 675 676 677 678 679 680 681 682 683 684
        /* 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;
            }
        }
685
        srcY += s->mspel * (1 + s->linesize);
Kostya Shishkov's avatar
Kostya Shishkov committed
686 687
    }

688 689 690
    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);
691 692
    } else { // hpel mc - always used for luma
        dxy = (my & 2) | ((mx & 2) >> 1);
693 694 695 696
        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
697 698 699 700 701
    }
}

static inline int median4(int a, int b, int c, int d)
{
702
    if(a < b) {
703 704
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
705
    } else {
706 707
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
708
    }
Kostya Shishkov's avatar
Kostya Shishkov committed
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
}


/** 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
725
    if(s->flags & CODEC_FLAG_GRAY) return;
Kostya Shishkov's avatar
Kostya Shishkov committed
726 727 728 729 730 731 732 733

    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 */
734
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
Kostya Shishkov's avatar
Kostya Shishkov committed
735 736 737 738 739 740 741
    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]);
742
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
Kostya Shishkov's avatar
Kostya Shishkov committed
743 744 745
            break;
        case 0x2:
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
746
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
Kostya Shishkov's avatar
Kostya Shishkov committed
747 748 749
            break;
        case 0x4:
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
750
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
Kostya Shishkov's avatar
Kostya Shishkov committed
751 752 753
            break;
        case 0x8:
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
754
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
Kostya Shishkov's avatar
Kostya Shishkov committed
755 756 757 758 759 760
            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;}
761 762
        tx = (mvx[t1] + mvx[t2]) / 2;
        ty = (mvy[t1] + mvy[t2]) / 2;
763 764 765
    } 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
766
        return; //no need to do MC for inter blocks
767
    }
Kostya Shishkov's avatar
Kostya Shishkov committed
768

769 770
    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
771 772
    uvmx = (tx + ((tx&3) == 3)) >> 1;
    uvmy = (ty + ((ty&3) == 3)) >> 1;
773 774 775 776
    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
777 778 779 780

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

Kostya Shishkov's avatar
Kostya Shishkov committed
781 782 783 784 785 786 787
    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);
    }
788

Kostya Shishkov's avatar
Kostya Shishkov committed
789 790
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
791 792
    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
793
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
Kostya Shishkov's avatar
Kostya Shishkov committed
794 795 796 797 798 799
        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
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815

        /* 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;
            }
        }
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
        /* 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
831 832
    }

Kostya Shishkov's avatar
Kostya Shishkov committed
833
    /* Chroma MC always uses qpel bilinear */
834
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
835 836 837 838 839 840 841 842 843
    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
844 845
}

846 847
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);

848
/**
anonymous's avatar
anonymous committed
849 850 851 852 853 854
 * 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
 */
855 856
static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
{
857
    VC1Context *v = avctx->priv_data;
858

859
    av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
860
    v->profile = get_bits(gb, 2);
861
    if (v->profile == PROFILE_COMPLEX)
862
    {
863
        av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
864
    }
865

866
    if (v->profile == PROFILE_ADVANCED)
867
    {
868 869
        v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
        v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
870
        return decode_sequence_header_adv(v, gb);
871 872 873
    }
    else
    {
Stefan Gehrer's avatar
Stefan Gehrer committed
874 875
        v->zz_8x4 = wmv2_scantableA;
        v->zz_4x8 = wmv2_scantableB;
876 877 878 879 880
        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);
anonymous's avatar