vc1dec.c 223 KB
Newer Older
1 2
/*
 * VC-1 and WMV3 decoder
3
 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 5 6
 * Copyright (c) 2006-2007 Konstantin Shishkov
 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
 *
7
 * This file is part of Libav.
8
 *
9
 * Libav is free software; you can redistribute it and/or
10 11 12 13
 * 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.
 *
14
 * Libav is distributed in the hope that it will be useful,
15 16 17 18 19
 * 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
20
 * License along with Libav; if not, write to the Free Software
21 22 23 24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
25
 * @file
26 27
 * VC-1 and WMV3 decoder
 */
28

29 30 31 32
#include "internal.h"
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
33
#include "h263.h"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "vc1.h"
#include "vc1data.h"
#include "vc1acdata.h"
#include "msmpeg4data.h"
#include "unary.h"
#include "mathops.h"
#include "vdpau_internal.h"

#undef NDEBUG
#include <assert.h>

#define MB_INTRA_VLC_BITS 9
#define DC_VLC_BITS 9


49
// offset tables for interlaced picture MVDATA decoding
50 51
static const int offset_table1[9] = {  0,  1,  2,  4,  8, 16, 32,  64, 128 };
static const int offset_table2[9] = {  0,  1,  3,  7, 15, 31, 63, 127, 255 };
52

53 54
/***********************************************************************/
/**
55
 * @name VC-1 Bitplane decoding
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 * @see 8.7, p56
 * @{
 */

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


/** @} */ //Bitplane group

78 79 80
static void vc1_put_signed_blocks_clamped(VC1Context *v)
{
    MpegEncContext *s = &v->s;
81 82 83
    int topleft_mb_pos, top_mb_pos;
    int stride_y, fieldtx;
    int v_dist;
84 85 86 87 88 89 90 91 92 93

    /* The put pixels loop is always one MB row behind the decoding loop,
     * because we can only put pixels when overlap filtering is done, and
     * for filtering of the bottom edge of a MB, we need the next MB row
     * present as well.
     * Within the row, the put pixels loop is also one MB col behind the
     * decoding loop. The reason for this is again, because for filtering
     * of the right MB edge, we need the next MB present. */
    if (!s->first_slice_line) {
        if (s->mb_x) {
94
            topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
95
            fieldtx        = v->fieldtx_plane[topleft_mb_pos];
96
            stride_y       = s->linesize << fieldtx;
97
            v_dist         = (16 - fieldtx) >> (fieldtx == 0);
98 99
            s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
                                             s->dest[0] - 16 * s->linesize - 16,
100
                                             stride_y);
101 102
            s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
                                             s->dest[0] - 16 * s->linesize - 8,
103
                                             stride_y);
104
            s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
105 106
                                             s->dest[0] - v_dist * s->linesize - 16,
                                             stride_y);
107
            s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
108 109
                                             s->dest[0] - v_dist * s->linesize - 8,
                                             stride_y);
110 111 112 113 114 115 116 117
            s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
                                             s->dest[1] - 8 * s->uvlinesize - 8,
                                             s->uvlinesize);
            s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
                                             s->dest[2] - 8 * s->uvlinesize - 8,
                                             s->uvlinesize);
        }
        if (s->mb_x == s->mb_width - 1) {
118
            top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
119 120 121
            fieldtx    = v->fieldtx_plane[top_mb_pos];
            stride_y   = s->linesize << fieldtx;
            v_dist     = fieldtx ? 15 : 8;
122 123
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
                                             s->dest[0] - 16 * s->linesize,
124
                                             stride_y);
125 126
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
                                             s->dest[0] - 16 * s->linesize + 8,
127
                                             stride_y);
128
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
129 130
                                             s->dest[0] - v_dist * s->linesize,
                                             stride_y);
131
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
132 133
                                             s->dest[0] - v_dist * s->linesize + 8,
                                             stride_y);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
                                             s->dest[1] - 8 * s->uvlinesize,
                                             s->uvlinesize);
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
                                             s->dest[2] - 8 * s->uvlinesize,
                                             s->uvlinesize);
        }
    }

#define inc_blk_idx(idx) do { \
        idx++; \
        if (idx >= v->n_allocated_blks) \
            idx = 0; \
    } while (0)

    inc_blk_idx(v->topleft_blk_idx);
    inc_blk_idx(v->top_blk_idx);
    inc_blk_idx(v->left_blk_idx);
    inc_blk_idx(v->cur_blk_idx);
}

155
static void vc1_loop_filter_iblk(VC1Context *v, int pq)
156
{
157
    MpegEncContext *s = &v->s;
158 159
    int j;
    if (!s->first_slice_line) {
160
        v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
161
        if (s->mb_x)
162 163 164 165
            v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
        v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
        for (j = 0; j < 2; j++) {
            v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
166
            if (s->mb_x)
167
                v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
168 169
        }
    }
170
    v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
171

172
    if (s->mb_y == s->end_mb_y - 1) {
173
        if (s->mb_x) {
174 175 176
            v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
            v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
            v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
177
        }
178
        v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
179 180 181
    }
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
{
    MpegEncContext *s = &v->s;
    int j;

    /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
     * means it runs two rows/cols behind the decoding loop. */
    if (!s->first_slice_line) {
        if (s->mb_x) {
            if (s->mb_y >= s->start_mb_y + 2) {
                v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);

                if (s->mb_x >= 2)
                    v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
                v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
197 198
                for (j = 0; j < 2; j++) {
                    v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
199
                    if (s->mb_x >= 2) {
200
                        v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
201 202 203 204 205 206 207 208 209 210 211 212 213
                    }
                }
            }
            v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
        }

        if (s->mb_x == s->mb_width - 1) {
            if (s->mb_y >= s->start_mb_y + 2) {
                v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);

                if (s->mb_x)
                    v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
                v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
214 215
                for (j = 0; j < 2; j++) {
                    v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
216
                    if (s->mb_x >= 2) {
217
                        v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
218 219 220 221 222 223
                    }
                }
            }
            v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
        }

224
        if (s->mb_y == s->end_mb_y) {
225 226 227 228 229
            if (s->mb_x) {
                if (s->mb_x >= 2)
                    v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
                v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
                if (s->mb_x >= 2) {
230 231
                    for (j = 0; j < 2; j++) {
                        v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
232 233 234 235 236 237 238 239 240
                    }
                }
            }

            if (s->mb_x == s->mb_width - 1) {
                if (s->mb_x)
                    v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
                v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
                if (s->mb_x) {
241 242
                    for (j = 0; j < 2; j++) {
                        v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
                    }
                }
            }
        }
    }
}

static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
{
    MpegEncContext *s = &v->s;
    int mb_pos;

    if (v->condover == CONDOVER_NONE)
        return;

    mb_pos = s->mb_x + s->mb_y * s->mb_stride;

    /* Within a MB, the horizontal overlap always runs before the vertical.
     * To accomplish that, we run the H on left and internal borders of the
     * currently decoded MB. Then, we wait for the next overlap iteration
     * to do H overlap on the right edge of this MB, before moving over and
     * running the V overlap. Therefore, the V overlap makes us trail by one
     * MB col and the H overlap filter makes us trail by one MB row. This
     * is reflected in the time at which we run the put_pixels loop. */
267 268 269
    if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
        if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
                        v->over_flags_plane[mb_pos - 1])) {
270 271 272 273
            v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
                                      v->block[v->cur_blk_idx][0]);
            v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
                                      v->block[v->cur_blk_idx][2]);
274
            if (!(s->flags & CODEC_FLAG_GRAY)) {
275 276 277 278 279 280 281 282 283 284 285 286
                v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
                                          v->block[v->cur_blk_idx][4]);
                v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
                                          v->block[v->cur_blk_idx][5]);
            }
        }
        v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
                                  v->block[v->cur_blk_idx][1]);
        v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
                                  v->block[v->cur_blk_idx][3]);

        if (s->mb_x == s->mb_width - 1) {
287 288
            if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
                                         v->over_flags_plane[mb_pos - s->mb_stride])) {
289 290 291 292
                v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
                                          v->block[v->cur_blk_idx][0]);
                v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
                                          v->block[v->cur_blk_idx][1]);
293
                if (!(s->flags & CODEC_FLAG_GRAY)) {
294 295 296 297 298 299 300 301 302 303 304 305 306
                    v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
                                              v->block[v->cur_blk_idx][4]);
                    v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
                                              v->block[v->cur_blk_idx][5]);
                }
            }
            v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
                                      v->block[v->cur_blk_idx][2]);
            v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
                                      v->block[v->cur_blk_idx][3]);
        }
    }
    if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
307 308
        if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
                                     v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
309 310 311 312
            v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
                                      v->block[v->left_blk_idx][0]);
            v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
                                      v->block[v->left_blk_idx][1]);
313
            if (!(s->flags & CODEC_FLAG_GRAY)) {
314 315 316 317 318 319 320 321 322 323 324 325 326
                v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
                                          v->block[v->left_blk_idx][4]);
                v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
                                          v->block[v->left_blk_idx][5]);
            }
        }
        v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
                                  v->block[v->left_blk_idx][2]);
        v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
                                  v->block[v->left_blk_idx][3]);
    }
}

327 328 329 330 331 332
/** Do motion compensation over 1 macroblock
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
 */
static void vc1_mc_1mv(VC1Context *v, int dir)
{
    MpegEncContext *s = &v->s;
333
    DSPContext *dsp   = &v->s.dsp;
334 335
    uint8_t *srcY, *srcU, *srcV;
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
336 337
    int off, off_uv;
    int v_edge_pos = s->v_edge_pos >> v->field_mode;
338 339 340 341

    if ((!v->field_mode ||
         (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
        !v->s.last_picture.f.data[0])
342
        return;
343 344 345 346 347

    mx = s->mv[dir][0][0];
    my = s->mv[dir][0][1];

    // store motion vectors for further use in B frames
348
    if (s->pict_type == AV_PICTURE_TYPE_P) {
349 350
        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
351
    }
352

353 354
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
    uvmy = (my + ((my & 3) == 3)) >> 1;
355 356
    v->luma_mv[s->mb_x][0] = uvmx;
    v->luma_mv[s->mb_x][1] = uvmy;
357 358 359

    if (v->field_mode &&
        v->cur_field_type != v->ref_field_type[dir]) {
360
        my   = my   - 2 + 4 * v->cur_field_type;
361 362 363
        uvmy = uvmy - 2 + 4 * v->cur_field_type;
    }

364 365
    // fastuvmc shall be ignored for interlaced frame picture
    if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
366 367
        uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
        uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
368
    }
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    if (v->field_mode) { // interlaced field picture
        if (!dir) {
            if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type) {
                srcY = s->current_picture.f.data[0];
                srcU = s->current_picture.f.data[1];
                srcV = s->current_picture.f.data[2];
            } else {
                srcY = s->last_picture.f.data[0];
                srcU = s->last_picture.f.data[1];
                srcV = s->last_picture.f.data[2];
            }
        } else {
            srcY = s->next_picture.f.data[0];
            srcU = s->next_picture.f.data[1];
            srcV = s->next_picture.f.data[2];
        }
385
    } else {
386
        if (!dir) {
387 388 389 390 391 392 393 394
            srcY = s->last_picture.f.data[0];
            srcU = s->last_picture.f.data[1];
            srcV = s->last_picture.f.data[2];
        } else {
            srcY = s->next_picture.f.data[0];
            srcU = s->next_picture.f.data[1];
            srcV = s->next_picture.f.data[2];
        }
395 396
    }

397 398 399 400
    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);
401

402
    if (v->profile != PROFILE_ADVANCED) {
403 404 405 406
        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);
407
    } else {
408 409 410 411 412 413
        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);
    }

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

418 419 420 421 422 423
    if (v->field_mode && v->ref_field_type[dir]) {
        srcY += s->current_picture_ptr->f.linesize[0];
        srcU += s->current_picture_ptr->f.linesize[1];
        srcV += s->current_picture_ptr->f.linesize[2];
    }

424
    /* for grayscale we should not try to read from unknown area */
425
    if (s->flags & CODEC_FLAG_GRAY) {
426 427 428 429
        srcU = s->edge_emu_buffer + 18 * s->linesize;
        srcV = s->edge_emu_buffer + 18 * s->linesize;
    }

430
    if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
431
        || s->h_edge_pos < 22 || v_edge_pos < 22
432
        || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
433
        || (unsigned)(src_y - 1)        > v_edge_pos    - (my&3) - 16 - 3) {
434
        uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
435 436

        srcY -= s->mspel * (1 + s->linesize);
437 438 439 440
        s->dsp.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, v_edge_pos);
441
        srcY = s->edge_emu_buffer;
442 443 444 445
        s->dsp.emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8 + 1, 8 + 1,
                                uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
        s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
                                uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
446 447 448
        srcU = uvbuf;
        srcV = uvbuf + 16;
        /* if we deal with range reduction we need to scale source blocks */
449
        if (v->rangeredfrm) {
450 451 452 453
            int i, j;
            uint8_t *src, *src2;

            src = srcY;
454 455 456
            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;
457 458
                src += s->linesize;
            }
459 460 461 462 463
            src  = srcU;
            src2 = srcV;
            for (j = 0; j < 9; j++) {
                for (i = 0; i < 9; i++) {
                    src[i]  = ((src[i]  - 128) >> 1) + 128;
464 465
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
                }
466
                src  += s->uvlinesize;
467 468 469 470
                src2 += s->uvlinesize;
            }
        }
        /* if we deal with intensity compensation we need to scale source blocks */
471
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
472 473 474 475
            int i, j;
            uint8_t *src, *src2;

            src = srcY;
476 477 478
            for (j = 0; j < 17 + s->mspel * 2; j++) {
                for (i = 0; i < 17 + s->mspel * 2; i++)
                    src[i] = v->luty[src[i]];
479 480
                src += s->linesize;
            }
481 482 483 484 485
            src  = srcU;
            src2 = srcV;
            for (j = 0; j < 9; j++) {
                for (i = 0; i < 9; i++) {
                    src[i]  = v->lutuv[src[i]];
486 487
                    src2[i] = v->lutuv[src2[i]];
                }
488
                src  += s->uvlinesize;
489 490 491 492 493 494
                src2 += s->uvlinesize;
            }
        }
        srcY += s->mspel * (1 + s->linesize);
    }

495 496 497 498 499 500 501
    if (v->field_mode && v->cur_field_type) {
        off    = s->current_picture_ptr->f.linesize[0];
        off_uv = s->current_picture_ptr->f.linesize[1];
    } else {
        off    = 0;
        off_uv = 0;
    }
502
    if (s->mspel) {
503
        dxy = ((my & 3) << 2) | (mx & 3);
504 505
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off    , srcY    , s->linesize, v->rnd);
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
506
        srcY += s->linesize * 8;
507 508
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
509 510
    } else { // hpel mc - always used for luma
        dxy = (my & 2) | ((mx & 2) >> 1);
511
        if (!v->rnd)
512
            dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
513
        else
514
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
515 516
    }

517
    if (s->flags & CODEC_FLAG_GRAY) return;
518
    /* Chroma MC always uses qpel bilinear */
519 520 521
    uvmx = (uvmx & 3) << 1;
    uvmy = (uvmy & 3) << 1;
    if (!v->rnd) {
522 523
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
524
    } else {
525 526 527 528 529 530 531
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
    }
}

static inline int median4(int a, int b, int c, int d)
{
532 533 534
    if (a < b) {
        if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
        else       return (FFMIN(b, c) + FFMAX(a, d)) / 2;
535
    } else {
536 537
        if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
        else       return (FFMIN(a, c) + FFMAX(b, d)) / 2;
538 539 540 541 542
    }
}

/** Do motion compensation for 4-MV macroblock - luminance block
 */
543
static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
544 545 546 547 548 549
{
    MpegEncContext *s = &v->s;
    DSPContext *dsp = &v->s.dsp;
    uint8_t *srcY;
    int dxy, mx, my, src_x, src_y;
    int off;
550
    int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
551
    int v_edge_pos = s->v_edge_pos >> v->field_mode;
552

553 554 555
    if ((!v->field_mode ||
         (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
        !v->s.last_picture.f.data[0])
556 557
        return;

558 559 560 561 562 563 564 565 566 567 568 569 570
    mx = s->mv[dir][n][0];
    my = s->mv[dir][n][1];

    if (!dir) {
        if (v->field_mode) {
            if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type)
                srcY = s->current_picture.f.data[0];
            else
                srcY = s->last_picture.f.data[0];
        } else
            srcY = s->last_picture.f.data[0];
    } else
        srcY = s->next_picture.f.data[0];
571

572 573 574 575 576 577 578 579 580 581 582 583 584
    if (v->field_mode) {
        if (v->cur_field_type != v->ref_field_type[dir])
            my = my - 2 + 4 * v->cur_field_type;
    }

    if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
        int same_count = 0, opp_count = 0, k;
        int chosen_mv[2][4][2], f;
        int tx, ty;
        for (k = 0; k < 4; k++) {
            f = v->mv_f[0][s->block_index[k] + v->blocks_off];
            chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
            chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
585
            opp_count  += f;
586 587 588 589 590
            same_count += 1 - f;
        }
        f = opp_count > same_count;
        switch (f ? opp_count : same_count) {
        case 4:
591 592 593 594
            tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
                         chosen_mv[f][2][0], chosen_mv[f][3][0]);
            ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
                         chosen_mv[f][2][1], chosen_mv[f][3][1]);
595 596 597 598 599 600 601 602 603 604 605 606
            break;
        case 3:
            tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
            ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
            break;
        case 2:
            tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
            ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
            break;
        }
        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
607 608
        for (k = 0; k < 4; k++)
            v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
609 610
    }

611
    if (v->fcm == ILACE_FRAME) {  // not sure if needed for other types of picture
612
        int qx, qy;
613
        int width  = s->avctx->coded_width;
614 615 616 617 618 619 620 621 622 623 624 625 626 627
        int height = s->avctx->coded_height >> 1;
        qx = (s->mb_x * 16) + (mx >> 2);
        qy = (s->mb_y *  8) + (my >> 3);

        if (qx < -17)
            mx -= 4 * (qx + 17);
        else if (qx > width)
            mx -= 4 * (qx - width);
        if (qy < -18)
            my -= 8 * (qy + 18);
        else if (qy > height + 1)
            my -= 8 * (qy - height - 1);
    }

628
    if ((v->fcm == ILACE_FRAME) && fieldmv)
629
        off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
630
    else
631
        off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
632 633
    if (v->field_mode && v->cur_field_type)
        off += s->current_picture_ptr->f.linesize[0];
634

635
    src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
636
    if (!fieldmv)
637
        src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
638 639
    else
        src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
640

641 642 643 644 645
    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);
646
        if (v->fcm == ILACE_FRAME) {
647
            if (src_y & 1)
648
                src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
649
            else
650
                src_y = av_clip(src_y, -18, s->avctx->coded_height);
651
        } else {
652
            src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
653
        }
654 655 656
    }

    srcY += src_y * s->linesize + src_x;
657 658
    if (v->field_mode && v->ref_field_type[dir])
        srcY += s->current_picture_ptr->f.linesize[0];
659

660 661 662 663
    if (fieldmv && !(src_y & 1))
        v_edge_pos--;
    if (fieldmv && (src_y & 1) && src_y < 4)
        src_y--;
664
    if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
665
        || s->h_edge_pos < 13 || v_edge_pos < 23
666 667
        || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
        || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
668 669
        srcY -= s->mspel * (1 + (s->linesize << fieldmv));
        /* check emulate edge stride and offset */
670 671 672 673
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
                                9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
                                src_x - s->mspel, src_y - (s->mspel << fieldmv),
                                s->h_edge_pos, v_edge_pos);
674 675
        srcY = s->edge_emu_buffer;
        /* if we deal with range reduction we need to scale source blocks */
676
        if (v->rangeredfrm) {
677 678 679 680
            int i, j;
            uint8_t *src;

            src = srcY;
681 682 683
            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;
684
                src += s->linesize << fieldmv;
685 686 687
            }
        }
        /* if we deal with intensity compensation we need to scale source blocks */
688
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
689 690 691 692
            int i, j;
            uint8_t *src;

            src = srcY;
693 694 695
            for (j = 0; j < 9 + s->mspel * 2; j++) {
                for (i = 0; i < 9 + s->mspel * 2; i++)
                    src[i] = v->luty[src[i]];
696
                src += s->linesize << fieldmv;
697 698
            }
        }
699
        srcY += s->mspel * (1 + (s->linesize << fieldmv));
700 701
    }

702
    if (s->mspel) {
703
        dxy = ((my & 3) << 2) | (mx & 3);
704
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
705 706
    } else { // hpel mc - always used for luma
        dxy = (my & 2) | ((mx & 2) >> 1);
707
        if (!v->rnd)
708 709 710 711 712 713
            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);
    }
}

714
static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
715
{
716 717
    int idx, i;
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
718 719 720 721 722 723

    idx =  ((a[3] != flag) << 3)
         | ((a[2] != flag) << 2)
         | ((a[1] != flag) << 1)
         |  (a[0] != flag);
    if (!idx) {
724 725 726
        *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
        *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
        return 4;
727 728
    } else if (count[idx] == 1) {
        switch (idx) {
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
        case 0x1:
            *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
            *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
            return 3;
        case 0x2:
            *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
            *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
            return 3;
        case 0x4:
            *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
            *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
            return 3;
        case 0x8:
            *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
            *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
            return 3;
        }
746
    } else if (count[idx] == 2) {
747 748 749 750 751 752 753 754 755 756 757 758 759 760
        int t1 = 0, t2 = 0;
        for (i = 0; i < 3; i++)
            if (!a[i]) {
                t1 = i;
                break;
            }
        for (i = t1 + 1; i < 4; i++)
            if (!a[i]) {
                t2 = i;
                break;
            }
        *tx = (mvx[t1] + mvx[t2]) / 2;
        *ty = (mvy[t1] + mvy[t2]) / 2;
        return 2;
761
    } else {
762
        return 0;
763
    }
764
    return -1;
765 766 767 768
}

/** Do motion compensation for 4-MV macroblock - both chroma blocks
 */
769
static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
770 771
{
    MpegEncContext *s = &v->s;
772
    DSPContext *dsp   = &v->s.dsp;
773 774
    uint8_t *srcU, *srcV;
    int uvmx, uvmy, uvsrc_x, uvsrc_y;
775 776 777 778 779
    int k, tx = 0, ty = 0;
    int mvx[4], mvy[4], intra[4], mv_f[4];
    int valid_count;
    int chroma_ref_type = v->cur_field_type, off = 0;
    int v_edge_pos = s->v_edge_pos >> v->field_mode;
780

781 782 783 784
    if (!v->field_mode && !v->s.last_picture.f.data[0])
        return;
    if (s->flags & CODEC_FLAG_GRAY)
        return;
785

786
    for (k = 0; k < 4; k++) {
787 788 789 790 791
        mvx[k] = s->mv[dir][k][0];
        mvy[k] = s->mv[dir][k][1];
        intra[k] = v->mb_type[0][s->block_index[k]];
        if (v->field_mode)
            mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
792 793 794
    }

    /* calculate chroma MV vector from four luma MVs */
795 796
    if (!v->field_mode || (v->field_mode && !v->numref)) {
        valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
797
        chroma_ref_type = v->reffield;
798
        if (!valid_count) {
799 800
            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
801 802
            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
            return; //no need to do MC for intra blocks
803 804
        }
    } else {
805 806 807 808 809 810
        int dominant = 0;
        if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
            dominant = 1;
        valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
        if (dominant)
            chroma_ref_type = !v->cur_field_type;
811
    }
812 813
    if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
        return;
814 815
    s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
    s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
816 817
    uvmx = (tx + ((tx & 3) == 3)) >> 1;
    uvmy = (ty + ((ty & 3) == 3)) >> 1;
818

819 820
    v->luma_mv[s->mb_x][0] = uvmx;
    v->luma_mv[s->mb_x][1] = uvmy;
821

822 823 824
    if (v->fastuvmc) {
        uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
        uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
825
    }
826 827 828
    // Field conversion bias
    if (v->cur_field_type != chroma_ref_type)
        uvmy += 2 - 4 * chroma_ref_type;
829 830 831 832

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

833 834 835 836 837 838
    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);
839 840
    }

841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
    if (!dir) {
        if (v->field_mode) {
            if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
                srcU = s->current_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
                srcV = s->current_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
            } else {
                srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
                srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
            }
        } else {
            srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
            srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
        }
    } else {
        srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
        srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
    }

    if (v->field_mode) {
        if (chroma_ref_type) {
            srcU += s->current_picture_ptr->f.linesize[1];
            srcV += s->current_picture_ptr->f.linesize[2];
        }
        off = v->cur_field_type ? s->current_picture_ptr->f.linesize[1] : 0;
    }

867
    if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
868
        || s->h_edge_pos < 18 || v_edge_pos < 18
869 870 871 872 873 874 875 876
        || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
        || (unsigned)uvsrc_y > (v_edge_pos    >> 1) - 9) {
        s->dsp.emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize,
                                8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
                                s->h_edge_pos >> 1, v_edge_pos >> 1);
        s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
                                8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
                                s->h_edge_pos >> 1, v_edge_pos >> 1);
877 878 879 880
        srcU = s->edge_emu_buffer;
        srcV = s->edge_emu_buffer + 16;

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

885 886 887 888 889
            src  = srcU;
            src2 = srcV;
            for (j = 0; j < 9; j++) {
                for (i = 0; i < 9; i++) {
                    src[i]  = ((src[i]  - 128) >> 1) + 128;
890 891
                    src2[i] = ((src2[i] - 128</