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

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

29 30
#include "internal.h"
#include "avcodec.h"
31
#include "error_resilience.h"
32
#include "mpegutils.h"
33
#include "mpegvideo.h"
34
#include "h263.h"
35
#include "h264chroma.h"
36 37 38 39 40 41
#include "vc1.h"
#include "vc1data.h"
#include "vc1acdata.h"
#include "msmpeg4data.h"
#include "unary.h"
#include "mathops.h"
42
#include "vdpau_internal.h"
43
#include "libavutil/avassert.h"
44 45 46 47 48 49 50 51

#undef NDEBUG
#include <assert.h>

#define MB_INTRA_VLC_BITS 9
#define DC_VLC_BITS 9


52
// offset tables for interlaced picture MVDATA decoding
53 54
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 };
55

56 57
/***********************************************************************/
/**
58
 * @name VC-1 Bitplane decoding
59 60 61 62 63
 * @see 8.7, p56
 * @{
 */


64 65 66 67
static void init_block_index(VC1Context *v)
{
    MpegEncContext *s = &v->s;
    ff_init_block_index(s);
68
    if (v->field_mode && !(v->second_field ^ v->tff)) {
69 70 71
        s->dest[0] += s->current_picture_ptr->f->linesize[0];
        s->dest[1] += s->current_picture_ptr->f->linesize[1];
        s->dest[2] += s->current_picture_ptr->f->linesize[2];
72 73 74
    }
}

75 76
/** @} */ //Bitplane group

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

    /* 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) {
93
            topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
94 95
            if (v->fcm == ILACE_FRAME)
                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
            if (v->fcm == ILACE_FRAME)
                fieldtx = v->fieldtx_plane[top_mb_pos];
121 122
            stride_y   = s->linesize << fieldtx;
            v_dist     = fieldtx ? 15 : 8;
123 124
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
                                             s->dest[0] - 16 * s->linesize,
125
                                             stride_y);
126 127
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
                                             s->dest[0] - 16 * s->linesize + 8,
128
                                             stride_y);
129
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
130 131
                                             s->dest[0] - v_dist * s->linesize,
                                             stride_y);
132
            s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
133 134
                                             s->dest[0] - v_dist * s->linesize + 8,
                                             stride_y);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
            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);
}

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

173
    if (s->mb_y == s->end_mb_y - 1) {
174
        if (s->mb_x) {
175 176 177
            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);
178
        }
179
        v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
180 181 182
    }
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
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);
198 199
                for (j = 0; j < 2; j++) {
                    v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
200
                    if (s->mb_x >= 2) {
201
                        v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
202 203 204 205 206 207 208 209 210 211 212 213 214
                    }
                }
            }
            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);
215 216
                for (j = 0; j < 2; j++) {
                    v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
217
                    if (s->mb_x >= 2) {
218
                        v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
219 220 221 222 223 224
                    }
                }
            }
            v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
        }

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

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

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. */
268 269 270
    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])) {
271 272 273 274
            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]);
275
            if (!(s->flags & CODEC_FLAG_GRAY)) {
276 277 278 279 280 281 282 283 284 285 286 287
                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) {
288 289
            if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
                                         v->over_flags_plane[mb_pos - s->mb_stride])) {
290 291 292 293
                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]);
294
                if (!(s->flags & CODEC_FLAG_GRAY)) {
295 296 297 298 299 300 301 302 303 304 305 306 307
                    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])) {
308 309
        if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
                                     v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
310 311 312 313
            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]);
314
            if (!(s->flags & CODEC_FLAG_GRAY)) {
315 316 317 318 319 320 321 322 323 324 325 326 327
                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]);
    }
}

328 329 330 331 332 333
/** 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;
334
    H264ChromaContext *h264chroma = &v->h264chroma;
335 336
    uint8_t *srcY, *srcU, *srcV;
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
337
    int v_edge_pos = s->v_edge_pos >> v->field_mode;
338
    int i;
339
    uint8_t (*luty)[256], (*lutuv)[256];
340
    int use_ic;
341 342 343

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

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

    // store motion vectors for further use in B frames
351
    if (s->pict_type == AV_PICTURE_TYPE_P) {
352 353 354 355
        for (i = 0; i < 4; i++) {
            s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
            s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
        }
356
    }
357

358 359
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
    uvmy = (my + ((my & 3) == 3)) >> 1;
360 361
    v->luma_mv[s->mb_x][0] = uvmx;
    v->luma_mv[s->mb_x][1] = uvmy;
362 363 364

    if (v->field_mode &&
        v->cur_field_type != v->ref_field_type[dir]) {
365
        my   = my   - 2 + 4 * v->cur_field_type;
366 367 368
        uvmy = uvmy - 2 + 4 * v->cur_field_type;
    }

369 370
    // fastuvmc shall be ignored for interlaced frame picture
    if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
371 372
        uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
        uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
373
    }
374 375
    if (!dir) {
        if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
376 377 378
            srcY = s->current_picture.f->data[0];
            srcU = s->current_picture.f->data[1];
            srcV = s->current_picture.f->data[2];
379 380
            luty  = v->curr_luty;
            lutuv = v->curr_lutuv;
381
            use_ic = *v->curr_use_ic;
382
        } else {
383 384 385
            srcY = s->last_picture.f->data[0];
            srcU = s->last_picture.f->data[1];
            srcV = s->last_picture.f->data[2];
386 387
            luty  = v->last_luty;
            lutuv = v->last_lutuv;
388
            use_ic = v->last_use_ic;
389
        }
390
    } else {
391 392 393
        srcY = s->next_picture.f->data[0];
        srcU = s->next_picture.f->data[1];
        srcV = s->next_picture.f->data[2];
394 395
        luty  = v->next_luty;
        lutuv = v->next_lutuv;
396
        use_ic = v->next_use_ic;
397 398
    }

399 400
    if (!srcY || !srcU) {
        av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
401
        return;
402
    }
403

404 405 406 407
    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);
408

409
    if (v->profile != PROFILE_ADVANCED) {
410 411 412 413
        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);
414
    } else {
415 416 417 418 419 420
        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);
    }

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

425
    if (v->field_mode && v->ref_field_type[dir]) {
426 427 428
        srcY += s->current_picture_ptr->f->linesize[0];
        srcU += s->current_picture_ptr->f->linesize[1];
        srcV += s->current_picture_ptr->f->linesize[2];
429 430
    }

431
    /* for grayscale we should not try to read from unknown area */
432
    if (s->flags & CODEC_FLAG_GRAY) {
433 434 435 436
        srcU = s->edge_emu_buffer + 18 * s->linesize;
        srcV = s->edge_emu_buffer + 18 * s->linesize;
    }

437
    if (v->rangeredfrm || use_ic
438
        || s->h_edge_pos < 22 || v_edge_pos < 22
439
        || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
440
        || (unsigned)(src_y - 1)        > v_edge_pos    - (my&3) - 16 - 3) {
441
        uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
442 443

        srcY -= s->mspel * (1 + s->linesize);
444 445
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
                                 s->linesize, s->linesize,
446 447 448
                                 17 + s->mspel * 2, 17 + s->mspel * 2,
                                 src_x - s->mspel, src_y - s->mspel,
                                 s->h_edge_pos, v_edge_pos);
449
        srcY = s->edge_emu_buffer;
450 451 452
        s->vdsp.emulated_edge_mc(uvbuf, srcU,
                                 s->uvlinesize, s->uvlinesize,
                                 8 + 1, 8 + 1,
453
                                 uvsrc_x, uvsrc_y,
454
                                 s->h_edge_pos >> 1, v_edge_pos >> 1);
455 456 457
        s->vdsp.emulated_edge_mc(uvbuf + 16, srcV,
                                 s->uvlinesize, s->uvlinesize,
                                 8 + 1, 8 + 1,
458
                                 uvsrc_x, uvsrc_y,
459
                                 s->h_edge_pos >> 1, v_edge_pos >> 1);
460 461 462
        srcU = uvbuf;
        srcV = uvbuf + 16;
        /* if we deal with range reduction we need to scale source blocks */
463
        if (v->rangeredfrm) {
464 465 466 467
            int i, j;
            uint8_t *src, *src2;

            src = srcY;
468 469 470
            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;
471 472
                src += s->linesize;
            }
473 474 475 476 477
            src  = srcU;
            src2 = srcV;
            for (j = 0; j < 9; j++) {
                for (i = 0; i < 9; i++) {
                    src[i]  = ((src[i]  - 128) >> 1) + 128;
478 479
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
                }
480
                src  += s->uvlinesize;
481 482 483 484
                src2 += s->uvlinesize;
            }
        }
        /* if we deal with intensity compensation we need to scale source blocks */
485
        if (use_ic) {
486 487 488 489
            int i, j;
            uint8_t *src, *src2;

            src = srcY;
490
            for (j = 0; j < 17 + s->mspel * 2; j++) {
491
                int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
492
                for (i = 0; i < 17 + s->mspel * 2; i++)
493
                    src[i] = luty[f][src[i]];
494 495
                src += s->linesize;
            }
496 497 498
            src  = srcU;
            src2 = srcV;
            for (j = 0; j < 9; j++) {
499
                int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
500
                for (i = 0; i < 9; i++) {
501 502
                    src[i]  = lutuv[f][src[i]];
                    src2[i] = lutuv[f][src2[i]];
503
                }
504
                src  += s->uvlinesize;
505 506 507 508 509 510
                src2 += s->uvlinesize;
            }
        }
        srcY += s->mspel * (1 + s->linesize);
    }

511
    if (s->mspel) {
512
        dxy = ((my & 3) << 2) | (mx & 3);
513
        v->vc1dsp.put_vc1_mspel_pixels_tab[0][dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
514 515
    } else { // hpel mc - always used for luma
        dxy = (my & 2) | ((mx & 2) >> 1);
516
        if (!v->rnd)
517
            s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
518
        else
519
            s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
520 521
    }

522
    if (s->flags & CODEC_FLAG_GRAY) return;
523
    /* Chroma MC always uses qpel bilinear */
524 525 526
    uvmx = (uvmx & 3) << 1;
    uvmy = (uvmy & 3) << 1;
    if (!v->rnd) {
527 528
        h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
        h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
529
    } else {
530 531
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
532 533 534 535 536
    }
}

static inline int median4(int a, int b, int c, int d)
{
537 538 539
    if (a < b) {
        if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
        else       return (FFMIN(b, c) + FFMAX(a, d)) / 2;
540
    } else {
541 542
        if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
        else       return (FFMIN(a, c) + FFMAX(b, d)) / 2;
543 544 545 546 547
    }
}

/** Do motion compensation for 4-MV macroblock - luminance block
 */
548
static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
549 550 551 552 553
{
    MpegEncContext *s = &v->s;
    uint8_t *srcY;
    int dxy, mx, my, src_x, src_y;
    int off;
554
    int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
555
    int v_edge_pos = s->v_edge_pos >> v->field_mode;
556
    uint8_t (*luty)[256];
557
    int use_ic;
558

559 560
    if ((!v->field_mode ||
         (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
561
        !v->s.last_picture.f->data[0])
562 563
        return;

564 565 566 567
    mx = s->mv[dir][n][0];
    my = s->mv[dir][n][1];

    if (!dir) {
568
        if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
569
            srcY = s->current_picture.f->data[0];
570
            luty = v->curr_luty;
571
            use_ic = *v->curr_use_ic;
572
        } else {
573
            srcY = s->last_picture.f->data[0];
574
            luty = v->last_luty;
575
            use_ic = v->last_use_ic;
576 577
        }
    } else {
578
        srcY = s->next_picture.f->data[0];
579
        luty = v->next_luty;
580
        use_ic = v->next_use_ic;
581
    }
582

583 584
    if (!srcY) {
        av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
585
        return;
586
    }
587

588 589 590 591 592 593 594 595 596 597 598 599 600
    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];
601
            opp_count  += f;
602 603 604 605 606
            same_count += 1 - f;
        }
        f = opp_count > same_count;
        switch (f ? opp_count : same_count) {
        case 4:
607 608 609 610
            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]);
611 612 613 614 615 616 617 618 619
            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;
620
        default:
621
            av_assert0(0);
622
        }
623 624
        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
625 626
        for (k = 0; k < 4; k++)
            v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
627 628
    }

629
    if (v->fcm == ILACE_FRAME) {  // not sure if needed for other types of picture
630
        int qx, qy;
631
        int width  = s->avctx->coded_width;
632
        int height = s->avctx->coded_height >> 1;
633 634 635 636
        if (s->pict_type == AV_PICTURE_TYPE_P) {
            s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
            s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
        }
637 638 639 640 641 642 643 644 645 646 647 648 649
        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);
    }

650
    if ((v->fcm == ILACE_FRAME) && fieldmv)
651
        off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
652
    else
653
        off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
654

655
    src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
656
    if (!fieldmv)
657
        src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
658 659
    else
        src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
660

661 662 663 664 665
    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);
666
        if (v->fcm == ILACE_FRAME) {
667
            if (src_y & 1)
668
                src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
669
            else
670
                src_y = av_clip(src_y, -18, s->avctx->coded_height);
671
        } else {
672
            src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
673
        }
674 675 676
    }

    srcY += src_y * s->linesize + src_x;
677
    if (v->field_mode && v->ref_field_type[dir])
678
        srcY += s->current_picture_ptr->f->linesize[0];
679

680 681 682 683
    if (fieldmv && !(src_y & 1))
        v_edge_pos--;
    if (fieldmv && (src_y & 1) && src_y < 4)
        src_y--;
684
    if (v->rangeredfrm || use_ic
685
        || s->h_edge_pos < 13 || v_edge_pos < 23
686 687
        || (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)) {
688 689
        srcY -= s->mspel * (1 + (s->linesize << fieldmv));
        /* check emulate edge stride and offset */
690 691
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
                                 s->linesize, s->linesize,
692 693 694
                                 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);
695 696
        srcY = s->edge_emu_buffer;
        /* if we deal with range reduction we need to scale source blocks */
697
        if (v->rangeredfrm) {
698 699 700 701
            int i, j;
            uint8_t *src;

            src = srcY;
702 703 704
            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;
705
                src += s->linesize << fieldmv;
706 707 708
            }
        }
        /* if we deal with intensity compensation we need to scale source blocks */
709
        if (use_ic) {
710 711 712 713
            int i, j;
            uint8_t *src;

            src = srcY;
714
            for (j = 0; j < 9 + s->mspel * 2; j++) {
715
                int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
716
                for (i = 0; i < 9 + s->mspel * 2; i++)
717
                    src[i] = luty[f][src[i]];
718
                src += s->linesize << fieldmv;
719 720
            }
        }
721
        srcY += s->mspel * (1 + (s->linesize << fieldmv));
722 723
    }

724
    if (s->mspel) {
725
        dxy = ((my & 3) << 2) | (mx & 3);
726
        if (avg)
727
            v->vc1dsp.avg_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
728
        else
729
            v->vc1dsp.put_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
730 731
    } else { // hpel mc - always used for luma
        dxy = (my & 2) | ((mx & 2) >> 1);
732
        if (!v->rnd)
733
            s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
734
        else
735
            s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
736 737 738
    }
}

739
static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
740
{
741 742
    int idx, i;
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
743 744 745 746 747 748

    idx =  ((a[3] != flag) << 3)
         | ((a[2] != flag) << 2)
         | ((a[1] != flag) << 1)
         |  (a[0] != flag);
    if (!idx) {
749 750 751
        *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
        *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
        return 4;
752 753
    } else if (count[idx] == 1) {
        switch (idx) {
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
        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;
        }
771
    } else if (count[idx] == 2) {
772 773 774 775 776 777 778 779 780 781 782 783 784 785
        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;
786
    } else {
787
        return 0;
788
    }
789
    return -1;
790 791 792 793
}

/** Do motion compensation for 4-MV macroblock - both chroma blocks
 */
794
static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
795 796
{
    MpegEncContext *s = &v->s;
797
    H264ChromaContext *h264chroma = &v->h264chroma;
798 799
    uint8_t *srcU, *srcV;
    int uvmx, uvmy, uvsrc_x, uvsrc_y;
800 801 802
    int k, tx = 0, ty = 0;
    int mvx[4], mvy[4], intra[4], mv_f[4];
    int valid_count;
803
    int chroma_ref_type = v->cur_field_type;
804
    int v_edge_pos = s->v_edge_pos >> v->field_mode;
805
    uint8_t (*lutuv)[256];
806
    int use_ic;
807

808
    if (!v->field_mode && !v->s.last_picture.f->data[0])
809 810 811
        return;
    if (s->flags & CODEC_FLAG_GRAY)
        return;
812

813
    for (k = 0; k < 4; k++) {
814 815 816 817 818
        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];
819 820 821
    }

    /* calculate chroma MV vector from four luma MVs */
822 823
    if (!v->field_mode || (v->field_mode && !v->numref)) {
        valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
824
        chroma_ref_type = v->reffield;
825
        if (!valid_count) {
826 827
            s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
            s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
828 829
            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
            return; //no need to do MC for intra blocks
830 831
        }
    } else {
832 833 834 835 836 837
        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;
838
    }
839
    if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
840
        return;
841 842
    s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
    s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
843 844
    uvmx = (tx + ((tx & 3) == 3)) >> 1;
    uvmy = (ty + ((ty & 3) == 3)) >> 1;
845

846 847
    v->luma_mv[s->mb_x][0] = uvmx;
    v->luma_mv[s->mb_x][1] = uvmy;
848

849 850 851
    if (v->fastuvmc) {
        uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
        uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
852
    }
853 854 855
    // Field conversion bias
    if (v->cur_field_type != chroma_ref_type)
        uvmy += 2 - 4 * chroma_ref_type;
856 857 858 859

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

860 861 862 863 864 865
    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);
866 867
    }

868
    if (!dir) {
869
        if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
870 871
            srcU = s->current_picture.f->data[1];
            srcV = s->current_picture.f->data[2];
872
            lutuv = v->curr_lutuv;
873
            use_ic = *v->curr_use_ic;
874
        } else {
875 876
            srcU = s->last_picture.f->data[1];
            srcV = s->last_picture.f->data[2];
877
            lutuv = v->last_lutuv;
878
            use_ic = v->last_use_ic;
879 880
        }
    } else {
881 882
        srcU = s->next_picture.f->data[1];
        srcV = s->next_picture.f->data[2];
883
        lutuv = v->next_lutuv;
884
        use_ic = v->next_use_ic;
885 886
    }

887 888
    if (!srcU) {
        av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
889
        return;
890
    }
891

892 893 894
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;

895 896
    if (v->field_mode) {
        if (chroma_ref_type) {
897 898
            srcU += s->current_picture_ptr->f->linesize[1];
            srcV += s->current_picture_ptr->f->linesize[2];
899 900 901
        }
    }

902
    if (v->rangeredfrm || use_ic
903
        || s->h_edge_pos < 18 || v_edge_pos < 18
904 905
        || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
        || (unsigned)uvsrc_y > (v_edge_pos    >> 1) - 9) {
906 907
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
                                 s->uvlinesize, s->uvlinesize,
908 909
                                 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
                                 s->h_edge_pos >> 1, v_edge_pos >> 1);
910 911
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
                                 s->uvlinesize, s->uvlinesize,
912 913
                                 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
                                 s->h_edge_pos >> 1, v_edge_pos >> 1);
914 915 916 917
        srcU = s->edge_emu_buffer;
        srcV = s->edge_emu_buffer + 16;

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

922 923 924 925 926
            src  = srcU;
            src2 = srcV;
            for (j = 0; j < 9; j++) {
                for (i = 0; i < 9; i++) {
                    src[i]  = ((src[i]  - 128) >> 1) + 128;
927 928
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
                }
929
                src  += s->uvlinesize;
930 931 932 933
                src2 += s->uvlinesize;
            }
        }
        /* if we deal with intensity compensation we need to scale source blocks */
934
        if (use_ic) {
935 936 937
            int i, j;
            uint8_t *src, *src2;

938 939 940
            src  = srcU;
            src2 = srcV;
            for (j = 0; j < 9; j++) {
941
                int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
942
                for (i = 0; i < 9; i++) {
943 944
                    src[i]  = lutuv[f][src[i]];
                    src2[i] = lutuv[f][src2[i]];
945
                }
946
                src  += s->uvlinesize;
947 948 949 950 951 952
                src2 += s->uvlinesize;
            }
        }
    }

    /* Chroma MC always uses qpel bilinear */
953 954 955
    uvmx = (uvmx & 3) << 1;
    uvmy = (uvmy & 3) << 1;
    if (!v->rnd) {
956 957
        h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
        h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
958
    } else {
959 960
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
961 962 963
    }
}

964
/** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
965
 */
966
static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
967 968
{
    MpegEncContext *s = &v->s;
969
    H264ChromaContext *h264chroma = &v->h264chroma;
970 971 972 973 974
    uint8_t *srcU, *srcV;
    int uvsrc_x, uvsrc_y;
    int uvmx_field[4], uvmy_field[4];
    int i, off, tx, ty;
    int fieldmv = v->blk_mv_type[s->block_index[0]];
975
    static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
976 977
    int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
    int v_edge_pos = s->v_edge_pos >> 1;
978 979
    int use_ic;
    uint8_t (*lutuv)[256];
980

981 982
    if (s->flags & CODEC_FLAG_GRAY)
        return;
983 984

    for (i = 0; i < 4; i++) {
985
        int d = i < 2 ? dir: dir2;
986
        tx = s->mv[d][i][0];
987
        uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
988
        ty = s->mv[d][i][1];
989 990 991 992 993 994 995 996
        if (fieldmv)
            uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
        else
            uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
    }

    for (i = 0; i < 4; i++) {
        off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
997
        uvsrc_x = s->mb_x * 8 +  (i & 1) * 4           + (uvmx_field[i] >> 2);
998 999
        uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
        // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1000 1001
        uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width  >> 1);
        uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1002
        if (i < 2 ? dir : dir2) {
1003 1004
            srcU = s->next_picture.f->data[1];
            srcV = s->next_picture.f->data[2];
1005 1006 1007
            lutuv  = v->next_lutuv;
            use_ic = v->next_use_ic;
        } else {
1008 1009
            srcU = s->last_picture.f->data[1];
            srcV = s->last_picture.f->data[2];
1010 1011 1012
            lutuv  = v->last_lutuv;
            use_ic = v->last_use_ic;
        }
1013 1014 1015 1016
        if (!srcU)
            return;
        srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
        srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1017 1018 1019 1020
        uvmx_field[i] = (uvmx_field[i] & 3) << 1;
        uvmy_field[i] = (uvmy_field[i] & 3) << 1;

        if (fieldmv && !(uvsrc_y & 1))
1021 1022
            v_edge_pos = (s->v_edge_pos >> 1) - 1;

1023 1024
        if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
            uvsrc_y--;
1025
        if (use_ic
1026
            || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1027
            || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1028
            || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1029 1030
            s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
                                     s->uvlinesize, s->uvlinesize,
1031 1032
                                     5, (5 << fieldmv), uvsrc_x, uvsrc_y,
                                     s->h_edge_pos >> 1, v_edge_pos);
1033 1034
            s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
                                     s->uvlinesize, s->uvlinesize,
1035 1036
                                     5, (5 << fieldmv), uvsrc_x, uvsrc_y,
                                     s->h_edge_pos >> 1, v_edge_pos);
1037 1038 1039 1040
            srcU = s->edge_emu_buffer;
            srcV = s->edge_emu_buffer + 16;

            /* if we deal with intensity compensation we need to scale source blocks */
1041
            if (use_ic) {
1042 1043 1044
                int i, j;
                uint8_t *src, *src2;

1045 1046 1047
                src  = srcU;
                src2 = srcV;
                for (j = 0; j < 5; j++) {
1048
                    int f = (uvsrc_y + (j << fieldmv)) & 1;
1049
                    for (i = 0; i < 5; i++) {
1050 1051
                        src[i]  = lutuv[f][src[i]];
                        src2[i] = lutuv[f][src2[i]];
1052
                    }
1053 1054
                    src  += s->uvlinesize << fieldmv;
                    src2 += s->uvlinesize << fieldmv;
1055 1056 1057
                }
            }
        }
1058 1059 1060 1061 1062 1063 1064 1065 1066
        if (avg) {
            if (!v->rnd) {
                h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
                h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
            } else {
                v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
                v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
            }
        } else {
1067 1068 1069 1070 1071 1072 1073
            if (!v->rnd) {
                h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
                h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
            } else {
                v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
                v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
            }
1074
        }
1075 1076 1077 1078 1079
    }
}

/***********************************************************************/
/**
1080
 * @name VC-1 Block-level functions
1081 1082 1083 1084 1085 1086 1087 1088 1089
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
 * @{
 */

/**
 * @def GET_MQUANT
 * @brief Get macroblock-level quantizer scale
 */
#define GET_MQUANT()                                           \
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    if (v->dquantfrm) {                                        \
        int edges = 0;                                         \
        if (v->dqprofile == DQPROFILE_ALL_MBS) {               \
            if (v->dqbilevel) {                                \
                mquant = (get_bits1(gb)) ? v->altpq : v->pq;   \
            } else {                                           \
                mqdiff = get_bits(gb, 3);                      \
                if (mqdiff != 7)                               \
                    mquant = v->pq + mqdiff;                   \
                else                                           \
                    mquant = get_bits(gb, 5);                  \
            }                                                  \
        }                                                      \
        if (v->dqprofile == DQPROFILE_SINGLE_EDGE)             \
            edges = 1 << v->dqsbedge;                          \
        else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES)       \
            edges = (3 << v->dqsbedge) % 15;                   \
        else if (v->dqprofile == DQPROFILE_FOUR_EDGES)         \
            edges = 15;                                        \
        if ((edges&1) && !s->mb_x)                             \
            mquant = v->altpq;                                 \
        if ((edges&2) && s->first_slice_line)                  \
            mquant = v->altpq;                                 \
        if ((edges&4) && s->mb_x == (s->mb_width - 1))         \
            mquant = v->altpq;                                 \
        if ((edges&8) && s->mb_y == (s->mb_height - 1))        \
            mquant = v->altpq;                                 \
1117
        if (!mquant || mquant > 31) {                          \
1118 1119 1120 1121
            av_log(v->s.avctx, AV_LOG_ERROR,                   \
                   "Overriding invalid mquant %d\n", mquant);  \
            mquant = 1;                                        \
        }                                                      \
1122
    }
1123 1124 1125 1126 1127 1128 1129 1130

/**
 * @def GET_MVDATA(_dmv_x, _dmv_y)
 * @brief Get MV differentials
 * @see MVDATA decoding from 8.3.5.2, p(1)20
 * @param _dmv_x Horizontal differential for decoded MV
 * @param _dmv_y Vertical differential for decoded MV
 */
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
#define GET_MVDATA(_dmv_x, _dmv_y)                                      \
    index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
                         VC1_MV_DIFF_VLC_BITS, 2);                      \
    if (index > 36) {                                                   \
        mb_has_coeffs = 1;                                              \
        index -= 37;                                                    \
    } else                                                              \
        mb_has_coeffs = 0;                                              \
    s->mb_intra = 0;                                                    \
    if (!index) {                                                       \
        _dmv_x = _dmv_y = 0;                                            \
    } else if (index == 35) {                                           \
        _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
        _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
    } else if (index == 36) {                                           \
        _dmv_x = 0;                                                     \
        _dmv_y = 0;                                                     \
        s->mb_intra = 1;                                                \
    } else {                                                            \
        index1 = index % 6;                                             \
        if (!s->quarter_sample && index1 == 5) val = 1;                 \
        else                                   val = 0;                 \
        if (size_table[index1] - val > 0)                               \
            val = get_bits(gb, size_table[index1] - val);               \
        else                                   val = 0;                 \
        sign = 0 - (val&1);                                             \
        _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
                                                                        \
        index1 = index / 6;                                             \
        if (!s->quarter_sample && index1 == 5) val = 1;                 \
        else                                   val = 0;                 \
        if (size_table[index1] - val > 0)                               \
            val = get_bits(gb, size_table[index1] - val);               \
        else                                   val = 0;                 \
        sign = 0 - (val & 1);                                           \
        _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign;   \
    }

static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
                                                   int *dmv_y, int *pred_flag)
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
{
    int index, index1;
    int extend_x = 0, extend_y = 0;
    GetBitContext *gb = &v->s.gb;
    int bits, esc;
    int val, sign;
    const int* offs_tab;

    if (v->numref) {
        bits = VC1_2REF_MVDATA_VLC_BITS;
1181
        esc  = 125;
1182 1183
    } else {
        bits = VC1_1REF_MVDATA_VLC_BITS;
1184
        esc  = 71;
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    }
    switch (v->dmvrange) {
    case 1:
        extend_x = 1;
        break;
    case 2:
        extend_y = 1;
        break;
    case 3:
        extend_x = extend_y = 1;
        break;
    }
    index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
    if (index == esc) {
        *dmv_x = get_bits(gb, v->k_x);
        *dmv_y = get_bits(gb, v->k_y);
        if (v->numref) {
1202 1203 1204 1205 1206 1207
            if (pred_flag) {
                *pred_flag = *dmv_y & 1;
                *dmv_y     = (*dmv_y + *pred_flag) >> 1;
            } else {
                *dmv_y     = (*dmv_y + (*dmv_y & 1)) >> 1;
            }
1208 1209 1210
        }
    }
    else {
1211
        av_assert0(index < esc);
1212 1213 1214 1215 1216 1217
        if (extend_x)
            offs_tab = offset_table2;
        else
            offs_tab = offset_table1;
        index1 = (index + 1) % 9;
        if (index1 != 0) {
1218 1219
            val    = get_bits(gb, index1 + extend_x);
            sign   = 0 -(val & 1);
1220 1221 1222 1223 1224 1225 1226 1227 1228
            *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
        } else
            *dmv_x = 0;
        if (extend_y)
            offs_tab = offset_table2;
        else
            offs_tab = offset_table1;
        index1 = (index + 1) / 9;
        if (index1 > v->numref) {
1229 1230
            val    = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
            sign   = 0 - (val & 1);
1231 1232 1233
            *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
        } else
            *dmv_y = 0;
1234
        if (v->numref && pred_flag)
1235 1236 1237 1238 1239 1240 1241 1242 1243
            *pred_flag = index1 & 1;
    }
}

static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
{
    int scaledvalue, refdist;
    int scalesame1, scalesame2;
    int scalezone1_x, zone1offset_x;
1244
    int table_index = dir ^ v->second_field;
1245 1246 1247 1248 1249 1250 1251

    if (v->s.pict_type != AV_PICTURE_TYPE_B)
        refdist = v->refdist;
    else
        refdist = dir ? v->brfd : v->frfd;
    if (refdist > 3)
        refdist = 3;
1252 1253 1254 1255
    scalesame1    = ff_vc1_field_mvpred_scales[table_index][1][refdist];
    scalesame2    = ff_vc1_field_mvpred_scales[table_index][2][refdist];
    scalezone1_x  = ff_vc1_field_mvpred_scales[table_index][3][refdist];
    zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

    if (FFABS(n) > 255)
        scaledvalue = n;
    else {
        if (FFABS(n) < scalezone1_x)
            scaledvalue = (n * scalesame1) >> 8;
        else {
            if (n < 0)
                scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
            else
                scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
        }
    }
    return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
}

static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
{
    int scaledvalue, refdist;
    int scalesame1, scalesame2;
    int scalezone1_y, zone1offset_y;
1277
    int table_index = dir ^ v->second_field;
1278 1279 1280 1281 1282 1283 1284

    if (v->s.pict_type != AV_PICTURE_TYPE_B)
        refdist = v->refdist;
    else
        refdist = dir ? v->brfd : v->frfd;
    if (refdist > 3)
        refdist = 3;
1285 1286 1287 1288
    scalesame1    = ff_vc1_field_mvpred_scales[table_index][1][refdist];
    scalesame2    = ff_vc1_field_mvpred_scales[table_index][2][refdist];
    scalezone1_y  = ff_vc1_field_mvpred_scales[table_index][4][refdist];
    zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

    if (FFABS(n) > 63)
        scaledvalue = n;
    else {
        if (FFABS(n) < scalezone1_y)
            scaledvalue = (n * scalesame1) >> 8;
        else {
            if (n < 0)
                scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
            else
                scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
        }
    }

    if (v->cur_field_type && !v->ref_field_type[dir])
        return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
    else
        return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
}

static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
{
    int scalezone1_x, zone1offset_x;
    int scaleopp1, scaleopp2, brfd;
    int scaledvalue;

    brfd = FFMIN(v->brfd, 3);
1316 1317 1318 1319
    scalezone1_x  = ff_vc1_b_field_mvpred_scales[3][brfd];
    zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
    scaleopp1     = ff_vc1_b_field_mvpred_scales[1][brfd];
    scaleopp2     = ff_vc1_b_field_mvpred_scales[2][brfd];
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

    if (FFABS(n) > 255)
        scaledvalue = n;
    else {
        if (FFABS(n) < scalezone1_x)
            scaledvalue = (n * scaleopp1) >> 8;
        else {
            if (n < 0)
                scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
            else
                scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
        }
    }
    return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
}

static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
{
    int scalezone1_y, zone1offset_y;
    int scaleopp1, scaleopp2, brfd;
    int scaledvalue;

    brfd = FFMIN(v->brfd, 3);
1343 1344 1345 1346
    scalezone1_y  = ff_vc1_b_field_mvpred_scales[4][brfd];
    zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
    scaleopp1     = ff_vc1_b_field_mvpred_scales[1][brfd];
    scaleopp2     = ff_vc1_b_field_mvpred_scales[2][brfd];
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

    if (FFABS(n) > 63)
        scaledvalue = n;
    else {
        if (FFABS(n) < scalezone1_y)
            scaledvalue = (n * scaleopp1) >> 8;
        else {
            if (n < 0)
                scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
            else
                scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
        }
    }
    if (v->cur_field_type && !v->ref_field_type[dir]) {
        return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
    } else {
        return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
    }
}

1367 1368
static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
                                         int dim, int dir)
1369 1370
{
    int brfd, scalesame;
1371
    int hpel = 1 - v->s.quarter_sample;
1372

1373
    n >>= hpel;
1374 1375
    if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
        if (dim)
1376
            n = scaleforsame_y(v, i, n, dir) << hpel;
1377
        else
1378 1379
            n = scaleforsame_x(v, n, dir) << hpel;
        return n;
1380
    }
1381
    brfd      = FFMIN(v->brfd, 3);
1382
    scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1383

1384 1385
    n = (n * scalesame >> 8) << hpel;
    return n;
1386 1387
}

1388 1389
static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
                                        int dim, int dir)
1390 1391
{
    int refdist, scaleopp;
1392
    int hpel = 1 - v->s.quarter_sample;
1393

1394
    n >>= hpel;
1395 1396
    if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
        if (dim)
1397
            n = scaleforopp_y(v, n, dir) << hpel;
1398
        else
1399 1400
            n = scaleforopp_x(v, n) << hpel;
        return n;
1401 1402 1403 1404 1405
    }
    if (v->s.pict_type != AV_PICTURE_TYPE_B)
        refdist = FFMIN(v->refdist, 3);
    else
        refdist = dir ? v->brfd : v->frfd;
1406
    scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1407

1408 1409
    n = (n * scaleopp >> 8) << hpel;
    return n;
1410 1411
}

1412 1413
/** Predict and set motion vector
 */
1414 1415 1416
static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
                               int mv1, int r_x, int r_y, uint8_t* is_intra,
                               int pred_flag, int dir)
1417
{
1418
    MpegEncContext *s = &v->s;
1419 1420 1421 1422
    int xy, wrap, off = 0;
    int16_t *A, *B, *C;
    int px, py;
    int sum;
1423
    int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1424
    int opposite, a_f, b_f, c_f;
1425 1426 1427
    int16_t field_predA[2];
    int16_t field_predB[2];
    int16_t field_predC[2];
1428 1429 1430 1431
    int a_valid, b_valid, c_valid;
    int hybridmv_thresh, y_bias = 0;

    if (v->mv_mode == MV_PMODE_MIXED_MV ||
1432 1433 1434 1435
        ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
        mixedmv_pic = 1;
    else
        mixedmv_pic = 0;
1436 1437 1438 1439 1440
    /* scale MV difference to be quad-pel */
    dmv_x <<= 1 - s->quarter_sample;
    dmv_y <<= 1 - s->quarter_sample;

    wrap = s->b8_stride;
1441
    xy   = s->block_index[n];
1442

1443
    if (s->mb_intra) {
1444 1445 1446 1447
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
        s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
        s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1448
        if (mv1) { /* duplicate motion data for 1-MV block */
1449 1450 1451 1452 1453 1454
            s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0]        = 0;
            s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1]        = 0;
            s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0]     = 0;
            s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1]     = 0;
            s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
            s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1455
            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1456 1457 1458 1459 1460 1461
            s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0]        = 0;
            s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1]        = 0;
            s->current_picture.motion_val[1][xy + wrap][0]                     = 0;
            s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1]     = 0;
            s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
            s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1462 1463 1464 1465
        }
        return;
    }

1466 1467
    C = s->current_picture.motion_val[dir][xy -    1 + v->blocks_off];
    A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1468
    if (mv1) {
1469 1470 1471 1472 1473
        if (v->field_mode && mixedmv_pic)
            off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
        else
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
    } else {
1474
        //in 4-MV mode different blocks have different B predictor position
1475
        switch (n) {
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
        case 0:
            off = (s->mb_x > 0) ? -1 : 1;
            break;
        case 1:
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
            break;
        case 2:
            off = 1;
            break;
        case 3:
            off = -1;
        }
    }
1489
    B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1490

1491
    a_valid = !s->first_slice_line || (n == 2 || n == 3);
1492
    b_valid = a_valid && (s->mb_width > 1);
1493
    c_valid = s->mb_x || (n == 1 || n == 3);
1494 1495 1496 1497 1498 1499 1500
    if (v->field_mode) {
        a_valid = a_valid && !is_intra[xy - wrap];
        b_valid = b_valid && !is_intra[xy - wrap + off];
        c_valid = c_valid && !is_intra[xy - 1];
    }

    if (a_valid) {
1501 1502 1503 1504 1505
        a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
        num_oppfield  += a_f;
        num_samefield += 1 - a_f;
        field_predA[0] = A[0];
        field_predA[1] = A[1];
1506
    } else {
1507 1508
        field_predA[0] = field_predA[1] = 0;
        a_f = 0;
1509 1510
    }
    if (b_valid) {
1511 1512 1513 1514 1515
        b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
        num_oppfield  += b_f;
        num_samefield += 1 - b_f;
        field_predB[0] = B[0];
        field_predB[1] = B[1];
1516
    } else {
1517 1518
        field_predB[0] = field_predB[1] = 0;
        b_f = 0;
1519
    }
1520 1521 1522 1523 1524 1525
    if (c_valid) {
        c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
        num_oppfield  += c_f;
        num_samefield += 1 - c_f;
        field_predC[0] = C[0];
        field_predC[1] = C[1];
1526
    } else {
1527 1528
        field_predC[0] = field_predC[1] = 0;
        c_f = 0;
1529 1530 1531
    }

    if (v->field_mode) {
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
        if (!v->numref)
            // REFFIELD determines if the last field or the second-last field is
            // to be used as reference
            opposite = 1 - v->reffield;
        else {
            if (num_samefield <= num_oppfield)
                opposite = 1 - pred_flag;
            else
                opposite = pred_flag;
        }
1542
    } else
1543 1544
        opposite = 0;
    if (opposite) {
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
        if (a_valid && !a_f) {
            field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
            field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
        }
        if (b_valid && !b_f) {
            field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
            field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
        }
        if (c_valid && !c_f) {
            field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
            field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
        }
        v->mv_f[dir][xy + v->blocks_off] = 1;
1558 1559
        v->ref_field_type[dir] = !v->cur_field_type;
    } else {
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
        if (a_valid && a_f) {
            field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
            field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
        }
        if (b_valid && b_f) {
            field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
            field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
        }
        if (c_valid && c_f) {
            field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
            field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
        }
        v->mv_f[dir][xy + v->blocks_off] = 0;
1573
        v->ref_field_type[dir] = v->cur_field_type;
1574
    }
1575

1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
    if (a_valid) {
        px = field_predA[0];
        py = field_predA[1];
    } else if (c_valid) {
        px = field_predC[0];
        py = field_predC[1];
    } else if (b_valid) {
        px = field_predB[0];
        py = field_predB[1];
    } else {
        px = 0;
        py = 0;
    }

    if (num_samefield + num_oppfield > 1) {
        px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
        py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
    }

1595
    /* Pullback MV as specified in 8.3.5.3.4 */
1596
    if (!v->field_mode) {
1597
        int qx, qy, X, Y;
1598 1599 1600 1601 1602 1603 1604
        qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
        qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
        X  = (s->mb_width  << 6) - 4;
        Y  = (s->mb_height << 6) - 4;
        if (mv1) {
            if (qx + px < -60) px = -60 - qx;
            if (qy + py < -60) py = -60 - qy;
1605
        } else {
1606 1607
            if (qx + px < -28) px = -28 - qx;
            if (qy + py < -28) py = -28 - qy;
1608
        }
1609 1610
        if (qx + px > X) px = X - qx;
        if (qy + py > Y) py = Y - qy;
1611
    }
1612 1613 1614

    if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1615
        hybridmv_thresh = 32;
1616 1617 1618 1619
        if (a_valid && c_valid) {
            if (is_intra[xy - wrap])
                sum = FFABS(px) + FFABS(py);
            else
1620
                sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1621 1622
            if (sum > hybridmv_thresh) {
                if (get_bits1(&s->gb)) {     // read HYBRIDPRED bit
1623 1624
                    px = field_predA[0];
                    py = field_predA[1];
1625
                } else {
1626 1627
                    px = field_predC[0];
                    py = field_predC[1];
1628
                }
1629
            } else {
1630 1631 1632
                if (is_intra[xy - 1])
                    sum = FFABS(px) + FFABS(py);
                else
1633
                    sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1634
                if (sum > hybridmv_thresh) {
1635
                    if (get_bits1(&s->gb)) {
1636 1637
                        px = field_predA[0];
                        py = field_predA[1];
1638
                    } else {
1639 1640
                        px = field_predC[0];
                        py = field_predC[1];
1641 1642
                    }
                }
1643
            }
1644 1645 1646 1647 1648 1649 1650 1651
        }
    }

    if (v->field_mode && v->numref)
        r_y >>= 1;
    if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
        y_bias = 1;
    /* store MV using signed modulus of MV range defined in 4.11 */
1652 1653
    s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
    s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1654
    if (mv1) { /* duplicate motion data for 1-MV block */
1655 1656 1657 1658 1659 1660
        s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
        s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
        s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
        s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
        s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
        s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1661
        v->mv_f[dir][xy +    1 + v->blocks_off] = v->mv_f[dir][xy +            v->blocks_off];
1662 1663 1664 1665 1666 1667
        v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
    }
}

/** Predict and set motion vector for interlaced frame picture MBs
 */
1668
static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1669
                                     int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1670 1671 1672 1673
{
    MpegEncContext *s = &v->s;
    int xy, wrap, off = 0;
    int A[2], B[2], C[2];
1674
    int px = 0, py = 0;
1675 1676 1677 1678 1679 1680 1681 1682
    int a_valid = 0, b_valid = 0, c_valid = 0;
    int field_a, field_b, field_c; // 0: same, 1: opposit
    int total_valid, num_samefield, num_oppfield;
    int pos_c, pos_b, n_adj;

    wrap = s->b8_stride;
    xy = s->block_index[n];

1683
    if (s->mb_intra) {
1684 1685 1686 1687
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
        s->current_picture.motion_val[1][xy][0] = 0;
        s->current_picture.motion_val[1][xy][1] = 0;
1688
        if (mvn == 1) { /* duplicate motion data for 1-MV block */
1689 1690 1691 1692 1693 1694
            s->current_picture.motion_val[0][xy + 1][0]        = 0;
            s->current_picture.motion_val[0][xy + 1][1]        = 0;
            s->current_picture.motion_val[0][xy + wrap][0]     = 0;
            s->current_picture.motion_val[0][xy + wrap][1]     = 0;
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1695
            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1696 1697 1698 1699 1700 1701
            s->current_picture.motion_val[1][xy + 1][0]        = 0;
            s->current_picture.motion_val[1][xy + 1][1]        = 0;
            s->current_picture.motion_val[1][xy + wrap][0]     = 0;
            s->current_picture.motion_val[1][xy + wrap][1]     = 0;
            s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
            s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1702 1703 1704 1705 1706 1707 1708 1709
        }
        return;
    }

    off = ((n == 0) || (n == 1)) ? 1 : -1;
    /* predict A */
    if (s->mb_x || (n == 1) || (n == 3)) {
        if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1710
            || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1711 1712
            A[0] = s->current_picture.motion_val[dir][xy - 1][0];
            A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1713 1714
            a_valid = 1;
        } else { // current block has frame mv and cand. has field MV (so average)
1715 1716 1717 1718
            A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
                    + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
            A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
                    + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1719 1720 1721 1722 1723 1724
            a_valid = 1;
        }
        if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
            a_valid = 0;
            A[0] = A[1] = 0;
        }
1725 1726
    } else
        A[0] = A[1] = 0;
1727 1728 1729 1730 1731 1732
    /* Predict B and C */
    B[0] = B[1] = C[0] = C[1] = 0;
    if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
        if (!s->first_slice_line) {
            if (!v->is_intra[s->mb_x - s->mb_stride]) {
                b_valid = 1;
1733 1734
                n_adj   = n | 2;
                pos_b   = s->block_index[n_adj] - 2 * wrap;
1735 1736 1737
                if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
                    n_adj = (n & 2) | (n & 1);
                }
1738 1739
                B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
                B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1740
                if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1741 1742
                    B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
                    B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1743 1744 1745 1746 1747
                }
            }
            if (s->mb_width > 1) {
                if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
                    c_valid = 1;
1748 1749
                    n_adj   = 2;
                    pos_c   = s->block_index[2] - 2 * wrap + 2;
1750 1751 1752
                    if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
                        n_adj = n & 2;
                    }
1753 1754
                    C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
                    C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1755
                    if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1756 1757
                        C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
                        C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1758 1759 1760 1761
                    }
                    if (s->mb_x == s->mb_width - 1) {
                        if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
                            c_valid = 1;
1762 1763
                            n_adj   = 3;
                            pos_c   = s->block_index[3] - 2 * wrap - 2;
1764 1765 1766
                            if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
                                n_adj = n | 1;
                            }
1767 1768
                            C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
                            C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1769
                            if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1770 1771
                                C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
                                C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1772
                            }
1773 1774
                        } else
                            c_valid = 0;
1775 1776 1777 1778 1779
                    }
                }
            }
        }
    } else {
1780
        pos_b   = s->block_index[1];
1781
        b_valid = 1;
1782 1783
        B[0]    = s->current_picture.motion_val[dir][pos_b][0];
        B[1]    = s->current_picture.motion_val[dir][pos_b][1];
1784
        pos_c   = s->block_index[0];
1785
        c_valid = 1;
1786 1787
        C[0]    = s->current_picture.motion_val[dir][pos_c][0];
        C[1]    = s->current_picture.motion_val[dir][pos_c][1];
1788 1789 1790 1791
    }

    total_valid = a_valid + b_valid + c_valid;
    // check if predictor A is out of bounds
1792
    if (!s->mb_x && !(n == 1 || n == 3)) {
1793 1794 1795 1796 1797 1798 1799
        A[0] = A[1] = 0;
    }
    // check if predictor B is out of bounds
    if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
        B[0] = B[1] = C[0] = C[1] = 0;
    }
    if (!v->blk_mv_type[xy]) {
1800
        if (s->mb_width == 1) {
1801 1802
            px = B[0];
            py = B[1];
1803
        } else {
1804 1805 1806 1807
            if (total_valid >= 2) {
                px = mid_pred(A[0], B[0], C[0]);
                py = mid_pred(A[1], B[1], C[1]);
            } else if (total_valid) {
1808 1809
                if      (a_valid) { px = A[0]; py = A[1]; }
                else if (b_valid) { px = B[0]; py = B[1]; }
1810
                else              { px = C[0]; py = C[1]; }
1811
            }
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
        }
    } else {
        if (a_valid)
            field_a = (A[1] & 4) ? 1 : 0;
        else
            field_a = 0;
        if (b_valid)
            field_b = (B[1] & 4) ? 1 : 0;
        else
            field_b = 0;
        if (c_valid)
            field_c = (C[1] & 4) ? 1 : 0;
        else
            field_c = 0;

1827
        num_oppfield  = field_a + field_b + field_c;
1828 1829 1830 1831 1832 1833 1834 1835
        num_samefield = total_valid - num_oppfield;
        if (total_valid == 3) {
            if ((num_samefield == 3) || (num_oppfield == 3)) {
                px = mid_pred(A[0], B[0], C[0]);
                py = mid_pred(A[1], B[1], C[1]);
            } else if (num_samefield >= num_oppfield) {
                /* take one MV from same field set depending on priority
                the check for B may not be necessary */
1836 1837
                px = !field_a ? A[0] : B[0];
                py = !field_a ? A[1] : B[1];
1838
            } else {
1839 1840
                px =  field_a ? A[0] : B[0];
                py =  field_a ? A[1] : B[1];
1841 1842 1843 1844
            }
        } else if (total_valid == 2) {
            if (num_samefield >= num_oppfield) {
                if (!field_a && a_valid) {
1845 1846
                    px = A[0];
                    py = A[1];
1847 1848 1849
                } else if (!field_b && b_valid) {
                    px = B[0];
                    py = B[1];
1850 1851
                } else /*if (c_valid)*/ {
                    av_assert1(c_valid);
1852 1853
                    px = C[0];
                    py = C[1];
1854
                } /*else px = py = 0;*/
1855 1856 1857 1858
            } else {
                if (field_a && a_valid) {
                    px = A[0];
                    py = A[1];
1859 1860
                } else /*if (field_b && b_valid)*/ {
                    av_assert1(field_b && b_valid);
1861 1862
                    px = B[0];
                    py = B[1];
1863
                } /*else if (c_valid) {
1864 1865
                    px = C[0];
                    py = C[1];
1866
                }*/
1867
            }
1868 1869 1870
        } else if (total_valid == 1) {
            px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
            py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1871
        }
1872
    }
1873

1874
    /* store MV using signed modulus of MV range defined in 4.11 */
1875 1876
    s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
    s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1877
    if (mvn == 1) { /* duplicate motion data for 1-MV block */
1878 1879 1880 1881 1882 1883
        s->current_picture.motion_val[dir][xy +    1    ][0] = s->current_picture.motion_val[dir][xy][0];
        s->current_picture.motion_val[dir][xy +    1    ][1] = s->current_picture.motion_val[dir][xy][1];
        s->current_picture.motion_val[dir][xy + wrap    ][0] = s->current_picture.motion_val[dir][xy][0];
        s->current_picture.motion_val[dir][xy + wrap    ][1] = s->current_picture.motion_val[dir][xy][1];
        s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
        s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1884
    } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1885 1886 1887 1888
        s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
        s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
        s->mv[dir][n + 1][0] = s->mv[dir][n][0];
        s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1889 1890 1891 1892 1893 1894 1895 1896
    }
}

/** Motion compensation for direct or interpolated blocks in B-frames
 */
static void vc1_interp_mc(VC1Context *v)
{
    MpegEncContext *s = &v->s;
1897
    H264ChromaContext *h264chroma = &v->h264chroma;
1898 1899
    uint8_t *srcY, *srcU, *srcV;
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1900 1901
    int off, off_uv;
    int v_edge_pos = s->v_edge_pos >> v->field_mode;
1902
    int use_ic = v->next_use_ic;
1903

1904
    if (!v->field_mode && !v->s.next_picture.f->data[0])
1905
        return;
1906

1907 1908
    mx   = s->mv[1][0][0];
    my   = s->mv[1][0][1];
1909 1910
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
    uvmy = (my + ((my & 3) == 3)) >> 1;
1911
    if (v->field_mode) {
1912
        if (v->cur_field_type != v->ref_field_type[1]) {
1913
            my   = my   - 2 + 4 * v->cur_field_type;
1914
            uvmy = uvmy - 2 + 4 * v->cur_field_type;
1915
        }
1916
    }
1917 1918 1919
    if (v->fastuvmc) {
        uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
        uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1920
    }
1921 1922 1923
    srcY = s->next_picture.f->data[0];
    srcU = s->next_picture.f->data[1];
    srcV = s->next_picture.f->data[2];
1924

1925 1926 1927 1928
    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);
1929

1930
    if (v->profile != PROFILE_ADVANCED) {
1931 1932 1933 1934
        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);
1935
    } else {
1936 1937 1938 1939 1940 1941
        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);
    }

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

1946
    if (v->field_mode && v->ref_field_type[1]) {
1947 1948 1949
        srcY += s->current_picture_ptr->f->linesize[0];
        srcU += s->current_picture_ptr->f->linesize[1];
        srcV += s->current_picture_ptr->f->linesize[2];
1950 1951
    }

1952
    /* for grayscale we should not try to read from unknown area */
1953
    if (s->flags & CODEC_FLAG_GRAY) {
1954 1955 1956 1957
        srcU = s->edge_emu_buffer + 18 * s->linesize;
        srcV = s->edge_emu_buffer + 18 * s->linesize;
    }

1958
    if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1959 1960
        || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
        || (unsigned)(src_y - 1) > v_edge_pos    - (my & 3) - 16 - 3) {
1961
        uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1962 1963

        srcY -= s->mspel * (1 + s->linesize);
1964 1965
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
                                 s->linesize, s->linesize,
1966 1967 1968
                                 17 + s->mspel * 2, 17 + s->mspel * 2,
                                 src_x - s->mspel, src_y - s->mspel,
                                 s->h_edge_pos, v_edge_pos);
1969
        srcY = s->edge_emu_buffer;
1970 1971 1972
        s->vdsp.emulated_edge_mc(uvbuf, srcU,
                                 s->uvlinesize, s->uvlinesize,
                                 8 + 1, 8 + 1,
1973
                                 uvsrc_x, uvsrc_y,
1974
                                 s->h_edge_pos >> 1, v_edge_pos >> 1);
1975 1976 1977
        s->vdsp.emulated_edge_mc(uvbuf + 16, srcV,
                                 s->uvlinesize, s->uvlinesize,
                                 8 + 1, 8 + 1,
1978
                                 uvsrc_x, uvsrc_y,
1979
                                 s->h_edge_pos >> 1, v_edge_pos >> 1);
1980 1981 1982
        srcU = uvbuf;
        srcV = uvbuf + 16;
        /* if we deal with range reduction we need to scale source blocks */
1983
        if (v->rangeredfrm) {
1984 1985 1986 1987
            int i, j;
            uint8_t *src, *src2;

            src = srcY;
1988 1989 1990
            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;
1991 1992
                src += s->linesize;
            }
1993 1994 1995 1996 1997
            src = srcU;
            src2 = srcV;
            for (j = 0; j < 9; j++) {
                for (i = 0; i < 9; i++) {
                    src[i]  = ((src[i]  - 128) >> 1) + 128;
1998 1999
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
                }
2000
                src  += s->uvlinesize;
2001 2002 2003
                src2 += s->uvlinesize;
            }
        }
2004

2005
        if (use_ic) {
2006 2007
            uint8_t (*luty )[256] = v->next_luty;
            uint8_t (*lutuv)[256] = v->next_lutuv;
2008 2009 2010 2011 2012
            int i, j;
            uint8_t *src, *src2;

            src = srcY;
            for (j = 0; j < 17 + s->mspel * 2; j++) {
2013
                int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
2014
                for (i = 0; i < 17 + s->mspel * 2; i++)
2015
                    src[i] = luty[f][src[i]];
2016 2017 2018 2019 2020
                src += s->linesize;
            }
            src  = srcU;
            src2 = srcV;
            for (j = 0; j < 9; j++) {
2021
                int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
2022
                for (i = 0; i < 9; i++) {
2023 2024
                    src[i]  = lutuv[f][src[i]];
                    src2[i] = lutuv[f][src2[i]];
2025 2026 2027 2028 2029
                }
                src  += s->uvlinesize;
                src2 += s->uvlinesize;
            }
        }
2030 2031 2032
        srcY += s->mspel * (1 + s->linesize);
    }

2033 2034
    off    = 0;
    off_uv = 0;
2035

2036
    if (s->mspel) {
2037
        dxy = ((my & 3) << 2) | (mx & 3);
2038
        v->vc1dsp.avg_vc1_mspel_pixels_tab[0][dxy](s->dest[0] + off    , srcY    , s->linesize, v->rnd);
2039 2040 2041
    } else { // hpel mc
        dxy = (my & 2) | ((mx & 2) >> 1);

2042
        if (!v->rnd)
2043
            s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2044
        else
2045
            s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2046 2047
    }

2048
    if (s->flags & CODEC_FLAG_GRAY) return;
2049
    /* Chroma MC always uses qpel blilinear */
2050 2051 2052
    uvmx = (uvmx & 3) << 1;
    uvmy = (uvmy & 3) << 1;
    if (!v->rnd) {
2053 2054
        h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
        h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2055
    } else {
2056 2057
        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2058 2059 2060 2061 2062 2063 2064 2065
    }
}

static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
{
    int n = bfrac;

#if B_FRACTION_DEN==256
2066
    if (inv)
2067
        n -= 256;
2068
    if (!qs)
2069 2070 2071
        return 2 * ((value * n + 255) >> 9);
    return (value * n + 128) >> 8;
#else
2072
    if (inv)
2073
        n -= B_FRACTION_DEN;
2074
    if (!qs)
2075 2076 2077 2078 2079 2080 2081
        return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
    return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
#endif
}

/** Reconstruct motion vector for B-frame and do motion compensation
 */
2082 2083
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
                            int direct, int mode)
2084
{
2085
    if (direct) {
2086 2087 2088 2089
        vc1_mc_1mv(v, 0);
        vc1_interp_mc(v);
        return;
    }
2090
    if (mode == BMV_TYPE_INTERPOLATED) {
2091 2092 2093 2094 2095 2096 2097 2098
        vc1_mc_1mv(v, 0);
        vc1_interp_mc(v);
        return;
    }

    vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
}

2099 2100
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
                                 int direct, int mvtype)
2101 2102 2103 2104 2105 2106 2107 2108 2109
{
    MpegEncContext *s = &v->s;
    int xy, wrap, off = 0;
    int16_t *A, *B, *C;
    int px, py;
    int sum;
    int r_x, r_y;
    const uint8_t *is_intra = v->mb_type[0];

2110 2111
    av_assert0(!v->field_mode);

2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
    r_x = v->range_x;
    r_y = v->range_y;
    /* scale MV difference to be quad-pel */
    dmv_x[0] <<= 1 - s->quarter_sample;
    dmv_y[0] <<= 1 - s->quarter_sample;
    dmv_x[1] <<= 1 - s->quarter_sample;
    dmv_y[1] <<= 1 - s->quarter_sample;

    wrap = s->b8_stride;
    xy = s->block_index[0];

2123
    if (s->mb_intra) {
2124 2125 2126 2127
        s->current_picture.motion_val[0][xy][0] =
        s->current_picture.motion_val[0][xy][1] =
        s->current_picture.motion_val[1][xy][0] =
        s->current_picture.motion_val[1][xy][1] = 0;
2128 2129
        return;
    }
2130 2131 2132
        if (direct && s->next_picture_ptr->field_picture)
            av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");

2133 2134 2135 2136
        s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
        s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
        s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
        s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2137

2138 2139 2140 2141 2142
        /* Pullback predicted motion vectors as specified in 8.4.5.4 */
        s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width  << 6) - 4 - (s->mb_x << 6));
        s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
        s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width  << 6) - 4 - (s->mb_x << 6));
        s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
2143
    if (direct) {
2144 2145 2146 2147
        s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
        s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
        s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
        s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2148 2149 2150
        return;
    }

2151
    if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2152 2153
        C   = s->current_picture.motion_val[0][xy - 2];
        A   = s->current_picture.motion_val[0][xy - wrap * 2];
2154
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2155
        B   = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2156

2157 2158 2159
        if (!s->mb_x) C[0] = C[1] = 0;
        if (!s->first_slice_line) { // predictor A is not out of bounds
            if (s->mb_width == 1) {
2160 2161 2162 2163 2164 2165
                px = A[0];
                py = A[1];
            } else {
                px = mid_pred(A[0], B[0], C[0]);
                py = mid_pred(A[1], B[1], C[1]);
            }
2166
        } else if (s->mb_x) { // predictor C is not out of bounds
2167 2168 2169 2170 2171 2172 2173 2174
            px = C[0];
            py = C[1];
        } else {
            px = py = 0;
        }
        /* Pullback MV as specified in 8.3.5.3.4 */
        {
            int qx, qy, X, Y;
2175
            if (v->profile < PROFILE_ADVANCED) {
2176 2177
                qx = (s->mb_x << 5);
                qy = (s->mb_y << 5);
2178 2179 2180 2181 2182 2183
                X  = (s->mb_width  << 5) - 4;
                Y  = (s->mb_height << 5) - 4;
                if (qx + px < -28) px = -28 - qx;
                if (qy + py < -28) py = -28 - qy;
                if (qx + px > X) px = X - qx;
                if (qy + py > Y) py = Y - qy;
2184 2185 2186
            } else {
                qx = (s->mb_x << 6);
                qy = (s->mb_y << 6);
2187 2188 2189 2190 2191 2192
                X  = (s->mb_width  << 6) - 4;
                Y  = (s->mb_height << 6) - 4;
                if (qx + px < -60) px = -60 - qx;
                if (qy + py < -60) py = -60 - qy;
                if (qx + px > X) px = X - qx;
                if (qy + py > Y) py = Y - qy;
2193 2194 2195
            }
        }
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2196 2197
        if (0 && !s->first_slice_line && s->mb_x) {
            if (is_intra[xy - wrap])
2198 2199 2200
                sum = FFABS(px) + FFABS(py);
            else
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2201 2202
            if (sum > 32) {
                if (get_bits1(&s->gb)) {
2203 2204 2205 2206 2207 2208 2209
                    px = A[0];
                    py = A[1];
                } else {
                    px = C[0];
                    py = C[1];
                }
            } else {
2210
                if (is_intra[xy - 2])
2211 2212 2213
                    sum = FFABS(px) + FFABS(py);
                else
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2214 2215
                if (sum > 32) {
                    if (get_bits1(&s->gb)) {
2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
                        px = A[0];
                        py = A[1];
                    } else {
                        px = C[0];
                        py = C[1];
                    }
                }
            }
        }
        /* store MV using signed modulus of MV range defined in 4.11 */
        s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
        s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
    }
2229
    if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2230 2231
        C   = s->current_picture.motion_val[1][xy - 2];
        A   = s->current_picture.motion_val[1][xy - wrap * 2];
2232
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2233
        B   = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2234

2235 2236 2237 2238
        if (!s->mb_x)
            C[0] = C[1] = 0;
        if (!s->first_slice_line) { // predictor A is not out of bounds
            if (s->mb_width == 1) {
2239 2240 2241 2242 2243 2244
                px = A[0];
                py = A[1];
            } else {
                px = mid_pred(A[0], B[0], C[0]);
                py = mid_pred(A[1], B[1], C[1]);
            }
2245
        } else if (s->mb_x) { // predictor C is not out of bounds
2246 2247 2248 2249 2250 2251 2252 2253
            px = C[0];
            py = C[1];
        } else {
            px = py = 0;
        }
        /* Pullback MV as specified in 8.3.5.3.4 */
        {
            int qx, qy, X, Y;
2254
            if (v->profile < PROFILE_ADVANCED) {
2255 2256
                qx = (s->mb_x << 5);
                qy = (s->mb_y << 5);
2257 2258 2259 2260 2261 2262
                X  = (s->mb_width  << 5) - 4;
                Y  = (s->mb_height << 5) - 4;
                if (qx + px < -28) px = -28 - qx;
                if (qy + py < -28) py = -28 - qy;
                if (qx + px > X) px = X - qx;
                if (qy + py > Y) py = Y - qy;
2263 2264 2265
            } else {
                qx = (s->mb_x << 6);
                qy = (s->mb_y << 6);
2266 2267 2268 2269 2270 2271
                X  = (s->mb_width  << 6) - 4;
                Y  = (s->mb_height << 6) - 4;
                if (qx + px < -60) px = -60 - qx;
                if (qy + py < -60) py = -60 - qy;
                if (qx + px > X) px = X - qx;
                if (qy + py > Y) py = Y - qy;
2272 2273 2274
            }
        }
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2275 2276
        if (0 && !s->first_slice_line && s->mb_x) {
            if (is_intra[xy - wrap])
2277 2278 2279
                sum = FFABS(px) + FFABS(py);
            else
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2280 2281
            if (sum > 32) {
                if (get_bits1(&s->gb)) {
2282 2283 2284 2285 2286 2287 2288
                    px = A[0];
                    py = A[1];
                } else {
                    px = C[0];
                    py = C[1];
                }
            } else {
2289
                if (is_intra[xy - 2])
2290 2291 2292
                    sum = FFABS(px) + FFABS(py);
                else
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2293 2294
                if (sum > 32) {
                    if (get_bits1(&s->gb)) {
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
                        px = A[0];
                        py = A[1];
                    } else {
                        px = C[0];
                        py = C[1];
                    }
                }
            }
        }
        /* store MV using signed modulus of MV range defined in 4.11 */

        s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
        s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
    }
2309 2310 2311 2312
    s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
    s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
    s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
    s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2313 2314
}

2315 2316 2317 2318 2319 2320 2321 2322
static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
{
    int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
    MpegEncContext *s = &v->s;
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;

    if (v->bmvtype == BMV_TYPE_DIRECT) {
        int total_opp, k, f;
2323 2324
        if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
            s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2325
                                      v->bfraction, 0, s->quarter_sample);
2326
            s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2327
                                      v->bfraction, 0, s->quarter_sample);
2328
            s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2329
                                      v->bfraction, 1, s->quarter_sample);
2330
            s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2331
                                      v->bfraction, 1, s->quarter_sample);
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344

            total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
                      + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
                      + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
                      + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
            f = (total_opp > 2) ? 1 : 0;
        } else {
            s->mv[0][0][0] = s->mv[0][0][1] = 0;
            s->mv[1][0][0] = s->mv[1][0][1] = 0;
            f = 0;
        }
        v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
        for (k = 0; k < 4; k++) {
2345 2346 2347 2348
            s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
            s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
            s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
            s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
            v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
            v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
        }
        return;
    }
    if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
        vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0],   1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
        vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1],   1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
        return;
    }
    if (dir) { // backward
        vc1_pred_mv(v, n, dmv_x[1], dmv_y[1], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
        if (n == 3 || mv1) {
2362
            vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0],   1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2363 2364 2365 2366
        }
    } else { // forward
        vc1_pred_mv(v, n, dmv_x[0], dmv_y[0], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
        if (n == 3 || mv1) {
2367
            vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1],   1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2368 2369 2370 2371
        }
    }
}

2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
/** Get predicted DC value for I-frames only
 * prediction dir: left=0, top=1
 * @param s MpegEncContext
 * @param overlap flag indicating that overlap filtering is used
 * @param pq integer part of picture quantizer
 * @param[in] n block index in the current MB
 * @param dc_val_ptr Pointer to DC predictor
 * @param dir_ptr Prediction direction for use in AC prediction
 */
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2382
                                int16_t **dc_val_ptr, int *dir_ptr)
2383 2384 2385 2386
{
    int a, b, c, wrap, pred, scale;
    int16_t *dc_val;
    static const uint16_t dcpred[32] = {
2387 2388 2389 2390
        -1, 1024,  512,  341,  256,  205,  171,  146,  128,
             114,  102,   93,   85,   79,   73,   68,   64,
              60,   57,   54,   51,   49,   47,   45,   43,
              41,   39,   38,   37,   35,   34,   33
2391 2392 2393
    };

    /* find prediction - wmv3_dc_scale always used here in fact */
2394 2395
    if (n < 4) scale = s->y_dc_scale;
    else       scale = s->c_dc_scale;
2396

2397 2398
    wrap   = s->block_wrap[n];
    dc_val = s->dc_val[0] + s->block_index[n];
2399 2400 2401 2402 2403 2404 2405 2406

    /* B A
     * C X
     */
    c = dc_val[ - 1];
    b = dc_val[ - 1 - wrap];
    a = dc_val[ - wrap];

2407
    if (pq < 9 || !overlap) {
2408
        /* Set outer values */
2409 2410 2411 2412 2413
        if (s->first_slice_line && (n != 2 && n != 3))
            b = a = dcpred[scale];
        if (s->mb_x == 0 && (n != 1 && n != 3))
            b = c = dcpred[scale];
    } else {
2414
        /* Set outer values */
2415 2416 2417 2418
        if (s->first_slice_line && (n != 2 && n != 3))
            b = a = 0;
        if (s->mb_x == 0 && (n != 1 && n != 3))
            b = c = 0;
2419 2420 2421
    }

    if (abs(a - b) <= abs(b - c)) {
2422 2423
        pred     = c;
        *dir_ptr = 1; // left
2424
    } else {
2425 2426
        pred     = a;
        *dir_ptr = 0; // top
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
    }

    /* update predictor */
    *dc_val_ptr = &dc_val[0];
    return pred;
}


/** Get predicted DC value
 * prediction dir: left=0, top=1
 * @param s MpegEncContext
 * @param overlap flag indicating that overlap filtering is used
 * @param pq integer part of picture quantizer
 * @param[in] n block index in the current MB
 * @param a_avail flag indicating top block availability
 * @param c_avail flag indicating left block availability
 * @param dc_val_ptr Pointer to DC predictor
 * @param dir_ptr Prediction direction for use in AC prediction
 */
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
                              int a_avail, int c_avail,
                              int16_t **dc_val_ptr, int *dir_ptr)
{
    int a, b, c, wrap, pred;
    int16_t *dc_val;
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    int q1, q2 = 0;
2454
    int dqscale_index;
2455 2456

    wrap = s->block_wrap[n];
2457
    dc_val = s->dc_val[0] + s->block_index[n];
2458 2459 2460 2461 2462 2463 2464 2465

    /* B A
     * C X
     */
    c = dc_val[ - 1];
    b = dc_val[ - 1 - wrap];
    a = dc_val[ - wrap];
    /* scale predictors if needed */
2466
    q1 = s->current_picture.qscale_table[mb_pos];
2467 2468 2469
    dqscale_index = s->y_dc_scale_table[q1] - 1;
    if (dqscale_index < 0)
        return 0;
2470
    if (c_avail && (n != 1 && n != 3)) {
2471
        q2 = s->current_picture.qscale_table[mb_pos - 1];
2472
        if (q2 && q2 != q1)
2473
            c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2474
    }
2475
    if (a_avail && (n != 2 && n != 3)) {
2476
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2477
        if (q2 && q2 != q1)
2478
            a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2479
    }
2480
    if (a_avail && c_avail && (n != 3)) {
2481
        int off = mb_pos;
2482 2483 2484 2485
        if (n != 1)
            off--;
        if (n != 2)
            off -= s->mb_stride;
2486
        q2 = s->current_picture.qscale_table[off];
2487
        if (q2 && q2 != q1)
2488
            b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2489 2490
    }

2491 2492 2493 2494
    if (a_avail && c_avail) {
        if (abs(a - b) <= abs(b - c)) {
            pred     = c;
            *dir_ptr = 1; // left
2495
        } else {
2496 2497 2498 2499 2500 2501 2502 2503 2504
            pred     = a;
            *dir_ptr = 0; // top
        }
    } else if (a_avail) {
        pred     = a;
        *dir_ptr = 0; // top
    } else if (c_avail) {
        pred     = c;
        *dir_ptr = 1; // left
2505
    } else {
2506 2507
        pred     = 0;
        *dir_ptr = 1; // left
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
    }

    /* update predictor */
    *dc_val_ptr = &dc_val[0];
    return pred;
}

/** @} */ // Block group

/**
2518
 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2519 2520 2521 2522
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
 * @{
 */

2523 2524
static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
                                       uint8_t **coded_block_ptr)
2525 2526 2527
{
    int xy, wrap, pred, a, b, c;

2528
    xy   = s->block_index[n];
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558
    wrap = s->b8_stride;

    /* B C
     * A X
     */
    a = s->coded_block[xy - 1       ];
    b = s->coded_block[xy - 1 - wrap];
    c = s->coded_block[xy     - wrap];

    if (b == c) {
        pred = a;
    } else {
        pred = c;
    }

    /* store value */
    *coded_block_ptr = &s->coded_block[xy];

    return pred;
}

/**
 * Decode one AC coefficient
 * @param v The VC1 context
 * @param last Last coefficient
 * @param skip How much zero coefficients to skip
 * @param value Decoded AC coefficient value
 * @param codingset set of VLC to decode data
 * @see 8.1.3.4
 */
2559 2560
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
                                int *value, int codingset)
2561 2562 2563 2564 2565
{
    GetBitContext *gb = &v->s.gb;
    int index, escape, run = 0, level = 0, lst = 0;

    index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2566
    if (index != ff_vc1_ac_sizes[codingset] - 1) {
2567
        run   = vc1_index_decode_table[codingset][index][0];
2568
        level = vc1_index_decode_table[codingset][index][1];
2569 2570
        lst   = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
        if (get_bits1(gb))
2571 2572 2573 2574 2575
            level = -level;
    } else {
        escape = decode210(gb);
        if (escape != 2) {
            index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2576
            run   = vc1_index_decode_table[codingset][index][0];
2577
            level = vc1_index_decode_table[codingset][index][1];
2578 2579 2580
            lst   = index >= vc1_last_decode_table[codingset];
            if (escape == 0) {
                if (lst)
2581 2582 2583 2584
                    level += vc1_last_delta_level_table[codingset][run];
                else
                    level += vc1_delta_level_table[codingset][run];
            } else {
2585
                if (lst)
2586 2587 2588 2589
                    run += vc1_last_delta_run_table[codingset][level] + 1;
                else
                    run += vc1_delta_run_table[codingset][level] + 1;
            }
2590
            if (get_bits1(gb))
2591 2592 2593 2594
                level = -level;
        } else {
            int sign;
            lst = get_bits1(gb);
2595 2596
            if (v->s.esc3_level_length == 0) {
                if (v->pq < 8 || v->dquantfrm) { // table 59
2597
                    v->s.esc3_level_length = get_bits(gb, 3);
2598
                    if (!v->s.esc3_level_length)
2599
                        v->s.esc3_level_length = get_bits(gb, 2) + 8;
2600
                } else { // table 60
2601 2602 2603 2604
                    v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
                }
                v->s.esc3_run_length = 3 + get_bits(gb, 2);
            }
2605 2606
            run   = get_bits(gb, v->s.esc3_run_length);
            sign  = get_bits1(gb);
2607
            level = get_bits(gb, v->s.esc3_level_length);
2608
            if (sign)
2609 2610 2611 2612
                level = -level;
        }
    }

2613 2614
    *last  = lst;
    *skip  = run;
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
    *value = level;
}

/** Decode intra block in intra frames - should be faster than decode_intra_block
 * @param v VC1Context
 * @param block block to decode
 * @param[in] n subblock index
 * @param coded are AC coeffs present or not
 * @param codingset set of VLC to decode data
 */
Diego Biurrun's avatar
Diego Biurrun committed
2625
static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2626
                              int coded, int codingset)
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641
{
    GetBitContext *gb = &v->s.gb;
    MpegEncContext *s = &v->s;
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
    int i;
    int16_t *dc_val;
    int16_t *ac_val, *ac_val2;
    int dcdiff;

    /* Get DC differential */
    if (n < 4) {
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
    } else {
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
    }
2642
    if (dcdiff < 0) {
2643 2644 2645
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
        return -1;
    }
2646 2647
    if (dcdiff) {
        if (dcdiff == 119 /* ESC index value */) {
2648
            /* TODO: Optimize */
2649
            if (v->pq == 1)      dcdiff = get_bits(gb, 10);
2650
            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2651 2652
            else                 dcdiff = get_bits(gb, 8);
        } else {
2653
            if (v->pq == 1)
2654
                dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2655
            else if (v->pq == 2)
2656
                dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
        }
        if (get_bits1(gb))
            dcdiff = -dcdiff;
    }

    /* Prediction */
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
    *dc_val = dcdiff;

    /* Store the quantized DC coeff, used for prediction */
    if (n < 4) {
        block[0] = dcdiff * s->y_dc_scale;
    } else {
        block[0] = dcdiff * s->c_dc_scale;
    }
    /* Skip ? */
    if (!coded) {
        goto not_coded;
    }

2677
    // AC Decoding
2678 2679 2680 2681
    i = 1;

    {
        int last = 0, skip, value;
2682
        const uint8_t *zz_table;
2683 2684 2685 2686 2687
        int scale;
        int k;

        scale = v->pq * 2 + v->halfpq;

2688 2689
        if (v->s.ac_pred) {
            if (!dc_pred_dir)
2690
                zz_table = v->zz_8x8[2];
2691
            else
2692
                zz_table = v->zz_8x8[3];
2693
        } else
2694
            zz_table = v->zz_8x8[1];
2695

2696
        ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
2697
        ac_val2 = ac_val;
2698
        if (dc_pred_dir) // left
2699
            ac_val -= 16;
2700
        else // top
2701 2702 2703 2704 2705
            ac_val -= 16 * s->block_wrap[n];

        while (!last) {
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
            i += skip;
2706
            if (i > 63)
2707 2708 2709 2710 2711
                break;
            block[zz_table[i++]] = value;
        }

        /* apply AC prediction if needed */
2712 2713 2714
        if (s->ac_pred) {
            if (dc_pred_dir) { // left
                for (k = 1; k < 8; k++)
2715
                    block[k << v->left_blk_sh] += ac_val[k];
2716 2717
            } else { // top
                for (k = 1; k < 8; k++)
2718
                    block[k << v->top_blk_sh] += ac_val[k + 8];
2719 2720 2721
            }
        }
        /* save AC coeffs for further prediction */
2722
        for (k = 1; k < 8; k++) {
2723 2724
            ac_val2[k]     = block[k << v->left_blk_sh];
            ac_val2[k + 8] = block[k << v->top_blk_sh];
2725 2726 2727
        }

        /* scale AC coeffs */
2728 2729
        for (k = 1; k < 64; k++)
            if (block[k]) {
2730
                block[k] *= scale;
2731
                if (!v->pquantizer)
2732 2733 2734
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
            }

2735
        if (s->ac_pred) i = 63;
2736 2737 2738
    }

not_coded:
2739
    if (!coded) {
2740
        int k, scale;
2741
        ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
2742 2743 2744 2745 2746
        ac_val2 = ac_val;

        i = 0;
        scale = v->pq * 2 + v->halfpq;
        memset(ac_val2, 0, 16 * 2);
2747
        if (dc_pred_dir) { // left
2748
            ac_val -= 16;
2749
            if (s->ac_pred)
2750
                memcpy(ac_val2, ac_val, 8 * 2);
2751
        } else { // top
2752
            ac_val -= 16 * s->block_wrap[n];
2753
            if (s->ac_pred)
2754 2755 2756 2757
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
        }

        /* apply AC prediction if needed */
2758 2759 2760
        if (s->ac_pred) {
            if (dc_pred_dir) { //left
                for (k = 1; k < 8; k++) {
2761
                    block[k << v->left_blk_sh] = ac_val[k] * scale;
2762
                    if (!v->pquantizer && block[k << v->left_blk_sh])
2763
                        block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2764
                }
2765 2766
            } else { // top
                for (k = 1; k < 8; k++) {
2767
                    block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2768
                    if (!v->pquantizer && block[k << v->top_blk_sh])
2769
                        block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787
                }
            }
            i = 63;
        }
    }
    s->block_last_index[n] = i;

    return 0;
}

/** Decode intra block in intra frames - should be faster than decode_intra_block
 * @param v VC1Context
 * @param block block to decode
 * @param[in] n subblock number
 * @param coded are AC coeffs present or not
 * @param codingset set of VLC to decode data
 * @param mquant quantizer value for this macroblock
 */
Diego Biurrun's avatar
Diego Biurrun committed
2788
static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2789
                                  int coded, int codingset, int mquant)
2790 2791 2792 2793 2794
{
    GetBitContext *gb = &v->s.gb;
    MpegEncContext *s = &v->s;
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
    int i;
2795
    int16_t *dc_val = NULL;
2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
    int16_t *ac_val, *ac_val2;
    int dcdiff;
    int a_avail = v->a_avail, c_avail = v->c_avail;
    int use_pred = s->ac_pred;
    int scale;
    int q1, q2 = 0;
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;

    /* Get DC differential */
    if (n < 4) {
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
    } else {
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
    }
2810
    if (dcdiff < 0) {
2811 2812 2813
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
        return -1;
    }
2814 2815
    if (dcdiff) {
        if (dcdiff == 119 /* ESC index value */) {
2816
            /* TODO: Optimize */
2817
            if (mquant == 1)      dcdiff = get_bits(gb, 10);
2818
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2819 2820
            else                  dcdiff = get_bits(gb, 8);
        } else {
2821
            if (mquant == 1)
2822
                dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2823
            else if (mquant == 2)
2824
                dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844
        }
        if (get_bits1(gb))
            dcdiff = -dcdiff;
    }

    /* Prediction */
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
    *dc_val = dcdiff;

    /* Store the quantized DC coeff, used for prediction */
    if (n < 4) {
        block[0] = dcdiff * s->y_dc_scale;
    } else {
        block[0] = dcdiff * s->c_dc_scale;
    }

    //AC Decoding
    i = 1;

    /* check if AC is needed at all */
2845 2846 2847
    if (!a_avail && !c_avail)
        use_pred = 0;
    ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
2848 2849 2850 2851
    ac_val2 = ac_val;

    scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);

2852
    if (dc_pred_dir) // left
2853
        ac_val -= 16;
2854
    else // top
2855 2856
        ac_val -= 16 * s->block_wrap[n];

2857
    q1 = s->current_picture.qscale_table[mb_pos];
2858
    if ( dc_pred_dir && c_avail && mb_pos)
2859
        q2 = s->current_picture.qscale_table[mb_pos - 1];
2860
    if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2861
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2862 2863 2864 2865 2866 2867 2868 2869
    if ( dc_pred_dir && n == 1)
        q2 = q1;
    if (!dc_pred_dir && n == 2)
        q2 = q1;
    if (n == 3)
        q2 = q1;

    if (coded) {
2870
        int last = 0, skip, value;
2871
        const uint8_t *zz_table;
2872 2873
        int k;

2874
        if (v->s.ac_pred) {
2875
            if (!use_pred && v->fcm == ILACE_FRAME) {
2876 2877
                zz_table = v->zzi_8x8;
            } else {
2878
                if (!dc_pred_dir) // top
2879
                    zz_table = v->zz_8x8[2];
2880
                else // left
2881 2882 2883
                    zz_table = v->zz_8x8[3];
            }
        } else {
2884
            if (v->fcm != ILACE_FRAME)
2885
                zz_table = v->zz_8x8[1];
2886
            else
2887 2888
                zz_table = v->zzi_8x8;
        }
2889 2890 2891 2892

        while (!last) {
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
            i += skip;
2893
            if (i > 63)
2894 2895 2896 2897 2898
                break;
            block[zz_table[i++]] = value;
        }

        /* apply AC prediction if needed */
2899
        if (use_pred) {
2900
            /* scale predictors if needed*/
2901
            if (q2 && q1 != q2) {
2902 2903 2904
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;

2905 2906
                if (q1 < 1)
                    return AVERROR_INVALIDDATA;
2907 2908
                if (dc_pred_dir) { // left
                    for (k = 1; k < 8; k++)
2909
                        block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2910 2911
                } else { // top
                    for (k = 1; k < 8; k++)
2912
                        block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2913 2914
                }
            } else {
2915 2916
                if (dc_pred_dir) { //left
                    for (k = 1; k < 8; k++)
2917
                        block[k << v->left_blk_sh] += ac_val[k];
2918
                } else { //top
2919
                    for (k = 1; k < 8; k++)
2920
                        block[k << v->top_blk_sh] += ac_val[k + 8];
2921 2922 2923 2924
                }
            }
        }
        /* save AC coeffs for further prediction */
2925
        for (k = 1; k < 8; k++) {
2926 2927
            ac_val2[k    ] = block[k << v->left_blk_sh];
            ac_val2[k + 8] = block[k << v->top_blk_sh];
2928 2929 2930
        }

        /* scale AC coeffs */
2931 2932
        for (k = 1; k < 64; k++)
            if (block[k]) {
2933
                block[k] *= scale;
2934
                if (!v->pquantizer)
2935 2936 2937
                    block[k] += (block[k] < 0) ? -mquant : mquant;
            }

2938
        if (use_pred) i = 63;
2939 2940 2941 2942
    } else { // no AC coeffs
        int k;

        memset(ac_val2, 0, 16 * 2);
2943 2944
        if (dc_pred_dir) { // left
            if (use_pred) {
2945
                memcpy(ac_val2, ac_val, 8 * 2);
2946
                if (q2 && q1 != q2) {
2947 2948
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2949 2950
                    if (q1 < 1)
                        return AVERROR_INVALIDDATA;
2951
                    for (k = 1; k < 8; k++)
2952 2953 2954
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
                }
            }
2955 2956
        } else { // top
            if (use_pred) {
2957
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2958
                if (q2 && q1 != q2) {
2959 2960
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2961 2962
                    if (q1 < 1)
                        return AVERROR_INVALIDDATA;
2963
                    for (k = 1; k < 8; k++)
2964 2965 2966 2967 2968 2969
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
                }
            }
        }

        /* apply AC prediction if needed */
2970 2971 2972
        if (use_pred) {
            if (dc_pred_dir) { // left
                for (k = 1; k < 8; k++) {
2973
                    block[k << v->left_blk_sh] = ac_val2[k] * scale;
2974
                    if (!v->pquantizer && block[k << v->left_blk_sh])
2975
                        block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2976
                }
2977 2978
            } else { // top
                for (k = 1; k < 8; k++) {
2979
                    block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2980
                    if (!v->pquantizer && block[k << v->top_blk_sh])
2981
                        block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
                }
            }
            i = 63;
        }
    }
    s->block_last_index[n] = i;

    return 0;
}

/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
 * @param v VC1Context
 * @param block block to decode
 * @param[in] n subblock index
 * @param coded are AC coeffs present or not
 * @param mquant block quantizer
 * @param codingset set of VLC to decode data
 */
Diego Biurrun's avatar
Diego Biurrun committed
3000
static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
3001
                                  int coded, int mquant, int codingset)
3002 3003 3004 3005 3006
{
    GetBitContext *gb = &v->s.gb;
    MpegEncContext *s = &v->s;
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
    int i;
3007
    int16_t *dc_val = NULL;
3008 3009 3010 3011 3012 3013 3014 3015
    int16_t *ac_val, *ac_val2;
    int dcdiff;
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    int a_avail = v->a_avail, c_avail = v->c_avail;
    int use_pred = s->ac_pred;
    int scale;
    int q1, q2 = 0;

3016 3017
    s->dsp.clear_block(block);

3018
    /* XXX: Guard against dumb values of mquant */
3019
    mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030

    /* Set DC scale - y and c use the same */
    s->y_dc_scale = s->y_dc_scale_table[mquant];
    s->c_dc_scale = s->c_dc_scale_table[mquant];

    /* Get DC differential */
    if (n < 4) {
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
    } else {
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
    }
3031
    if (dcdiff < 0) {
3032 3033 3034
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
        return -1;
    }
3035 3036
    if (dcdiff) {
        if (dcdiff == 119 /* ESC index value */) {
3037
            /* TODO: Optimize */
3038
            if (mquant == 1)      dcdiff = get_bits(gb, 10);
3039
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
3040 3041
            else                  dcdiff = get_bits(gb, 8);
        } else {
3042
            if (mquant == 1)
3043
                dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3044
            else if (mquant == 2)
3045
                dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
        }
        if (get_bits1(gb))
            dcdiff = -dcdiff;
    }

    /* Prediction */
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
    *dc_val = dcdiff;

    /* Store the quantized DC coeff, used for prediction */

    if (n < 4) {
        block[0] = dcdiff * s->y_dc_scale;
    } else {
        block[0] = dcdiff * s->c_dc_scale;
    }

    //AC Decoding
    i = 1;

    /* check if AC is needed at all and adjust direction if needed */
3067 3068 3069
    if (!a_avail) dc_pred_dir = 1;
    if (!c_avail) dc_pred_dir = 0;
    if (!a_avail && !c_avail) use_pred = 0;
3070 3071 3072 3073 3074
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
    ac_val2 = ac_val;

    scale = mquant * 2 + v->halfpq;

3075
    if (dc_pred_dir) //left
3076 3077 3078 3079
        ac_val -= 16;
    else //top
        ac_val -= 16 * s->block_wrap[n];

3080
    q1 = s->current_picture.qscale_table[mb_pos];
3081
    if (dc_pred_dir && c_avail && mb_pos)
3082
        q2 = s->current_picture.qscale_table[mb_pos - 1];
3083
    if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3084
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3085 3086 3087 3088 3089
    if ( dc_pred_dir && n == 1)
        q2 = q1;
    if (!dc_pred_dir && n == 2)
        q2 = q1;
    if (n == 3) q2 = q1;
3090

3091
    if (coded) {
3092 3093 3094 3095 3096 3097
        int last = 0, skip, value;
        int k;

        while (!last) {
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
            i += skip;
3098
            if (i > 63)
3099
                break;
3100
            if (v->fcm == PROGRESSIVE)
3101 3102
                block[v->zz_8x8[0][i++]] = value;
            else {
3103
                if (use_pred && (v->fcm == ILACE_FRAME)) {
3104
                    if (!dc_pred_dir) // top
3105
                        block[v->zz_8x8[2][i++]] = value;
3106
                    else // left
3107 3108 3109 3110 3111
                        block[v->zz_8x8[3][i++]] = value;
                } else {
                    block[v->zzi_8x8[i++]] = value;
                }
            }
3112 3113 3114
        }

        /* apply AC prediction if needed */
3115
        if (use_pred) {
3116
            /* scale predictors if needed*/
3117
            if (q2 && q1 != q2) {
3118 3119 3120
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;

3121 3122
                if (q1 < 1)
                    return AVERROR_INVALIDDATA;
3123 3124
                if (dc_pred_dir) { // left
                    for (k = 1; k < 8; k++)
3125
                        block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3126
                } else { //top
3127
                    for (k = 1; k < 8; k++)
3128
                        block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3129 3130
                }
            } else {
3131 3132
                if (dc_pred_dir) { // left
                    for (k = 1; k < 8; k++)
3133
                        block[k << v->left_blk_sh] += ac_val[k];
3134 3135
                } else { // top
                    for (k = 1; k < 8; k++)
3136
                        block[k << v->top_blk_sh] += ac_val[k + 8];
3137 3138 3139 3140
                }
            }
        }
        /* save AC coeffs for further prediction */
3141
        for (k = 1; k < 8; k++) {
3142 3143
            ac_val2[k    ] = block[k << v->left_blk_sh];
            ac_val2[k + 8] = block[k << v->top_blk_sh];
3144 3145 3146
        }

        /* scale AC coeffs */
3147 3148
        for (k = 1; k < 64; k++)
            if (block[k]) {
3149
                block[k] *= scale;
3150
                if (!v->pquantizer)
3151 3152 3153
                    block[k] += (block[k] < 0) ? -mquant : mquant;
            }

3154
        if (use_pred) i = 63;
3155 3156 3157 3158
    } else { // no AC coeffs
        int k;

        memset(ac_val2, 0, 16 * 2);
3159 3160
        if (dc_pred_dir) { // left
            if (use_pred) {
3161
                memcpy(ac_val2, ac_val, 8 * 2);
3162
                if (q2 && q1 != q2) {
3163 3164
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3165 3166
                    if (q1 < 1)
                        return AVERROR_INVALIDDATA;
3167
                    for (k = 1; k < 8; k++)
3168 3169 3170
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
                }
            }
3171 3172
        } else { // top
            if (use_pred) {
3173
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3174
                if (q2 && q1 != q2) {
3175 3176
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3177 3178
                    if (q1 < 1)
                        return AVERROR_INVALIDDATA;
3179
                    for (k = 1; k < 8; k++)
3180 3181 3182 3183 3184 3185
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
                }
            }
        }

        /* apply AC prediction if needed */
3186 3187 3188
        if (use_pred) {
            if (dc_pred_dir) { // left
                for (k = 1; k < 8; k++) {
3189
                    block[k << v->left_blk_sh] = ac_val2[k] * scale;
3190
                    if (!v->pquantizer && block[k << v->left_blk_sh])
3191
                        block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3192
                }
3193 3194
            } else { // top
                for (k = 1; k < 8; k++) {
3195
                    block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3196
                    if (!v->pquantizer && block[k << v->top_blk_sh])
3197
                        block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
                }
            }
            i = 63;
        }
    }
    s->block_last_index[n] = i;

    return 0;
}

/** Decode P block
 */
Diego Biurrun's avatar
Diego Biurrun committed
3210
static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3211 3212 3213
                              int mquant, int ttmb, int first_block,
                              uint8_t *dst, int linesize, int skip_block,
                              int *ttmb_out)
3214 3215 3216 3217 3218 3219 3220 3221 3222
{
    MpegEncContext *s = &v->s;
    GetBitContext *gb = &s->gb;
    int i, j;
    int subblkpat = 0;
    int scale, off, idx, last, skip, value;
    int ttblk = ttmb & 7;
    int pat = 0;

3223 3224
    s->dsp.clear_block(block);

3225
    if (ttmb == -1) {
3226 3227
        ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
    }
3228
    if (ttblk == TT_4X4) {
3229 3230
        subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
    }
3231
    if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3232 3233
        && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
            || (!v->res_rtm_flag && !first_block))) {
3234
        subblkpat = decode012(gb);
3235 3236 3237 3238 3239 3240
        if (subblkpat)
            subblkpat ^= 3; // swap decoded pattern bits
        if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
            ttblk = TT_8X4;
        if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
            ttblk = TT_4X8;
3241 3242 3243 3244
    }
    scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);

    // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3245
    if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3246
        subblkpat = 2 - (ttblk == TT_8X4_TOP);
3247
        ttblk     = TT_8X4;
3248
    }
3249
    if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3250
        subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3251
        ttblk     = TT_4X8;
3252
    }
3253
    switch (ttblk) {
3254
    case TT_8X8:
3255 3256
        pat  = 0xF;
        i    = 0;
3257 3258 3259 3260
        last = 0;
        while (!last) {
            vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
            i += skip;
3261
            if (i > 63)
3262
                break;
3263
            if (!v->fcm)
3264 3265 3266
                idx = v->zz_8x8[0][i++];
            else
                idx = v->zzi_8x8[i++];
3267
            block[idx] = value * scale;
3268
            if (!v->pquantizer)
3269 3270
                block[idx] += (block[idx] < 0) ? -mquant : mquant;
        }
3271 3272
        if (!skip_block) {
            if (i == 1)
3273
                v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3274
            else {
3275 3276
                v->vc1dsp.vc1_inv_trans_8x8(block);
                s->dsp.add_pixels_clamped(block, dst, linesize);
3277
            }
3278 3279 3280 3281
        }
        break;
    case TT_4X4:
        pat = ~subblkpat & 0xF;
3282
        for (j = 0; j < 4; j++) {
3283
            last = subblkpat & (1 << (3 - j));
3284 3285
            i    = 0;
            off  = (j & 1) * 4 + (j & 2) * 16;
3286 3287 3288
            while (!last) {
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
                i += skip;
3289
                if (i > 15)
3290
                    break;
3291
                if (!v->fcm)
3292 3293 3294
                    idx = ff_vc1_simple_progressive_4x4_zz[i++];
                else
                    idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3295
                block[idx + off] = value * scale;
3296
                if (!v->pquantizer)
3297 3298
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
            }
3299 3300 3301
            if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
                if (i == 1)
                    v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3302
                else
3303
                    v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) *  2 * linesize, linesize, block + off);
3304 3305 3306 3307
            }
        }
        break;
    case TT_8X4:
3308 3309
        pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
        for (j = 0; j < 2; j++) {
3310
            last = subblkpat & (1 << (1 - j));
3311 3312
            i    = 0;
            off  = j * 32;
3313 3314 3315
            while (!last) {
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
                i += skip;
3316
                if (i > 31)
3317
                    break;
3318
                if (!v->fcm)
3319 3320 3321
                    idx = v->zz_8x4[i++] + off;
                else
                    idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3322
                block[idx] = value * scale;
3323
                if (!v->pquantizer)
3324 3325
                    block[idx] += (block[idx] < 0) ? -mquant : mquant;
            }
3326 3327 3328
            if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
                if (i == 1)
                    v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3329
                else
3330
                    v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3331 3332 3333 3334
            }
        }
        break;
    case TT_4X8:
3335 3336
        pat = ~(subblkpat * 5) & 0xF;
        for (j = 0; j < 2; j++) {
3337
            last = subblkpat & (1 << (1 - j));
3338 3339
            i    = 0;
            off  = j * 4;
3340 3341 3342
            while (!last) {
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
                i += skip;
3343
                if (i > 31)
3344
                    break;
3345
                if (!v->fcm)
3346 3347 3348
                    idx = v->zz_4x8[i++] + off;
                else
                    idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3349
                block[idx] = value * scale;
3350
                if (!v->pquantizer)
3351 3352
                    block[idx] += (block[idx] < 0) ? -mquant : mquant;
            }
3353 3354 3355
            if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
                if (i == 1)
                    v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3356
                else
3357
                    v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3358 3359 3360 3361
            }
        }
        break;
    }
3362 3363
    if (ttmb_out)
        *ttmb_out |= ttblk << (n * 4);
3364 3365 3366 3367 3368 3369 3370 3371
    return pat;
}

/** @} */ // Macroblock group

static const int size_table  [6] = { 0, 2, 3, 4,  5,  8 };
static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };

3372 3373
static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
{
3374
    MpegEncContext *s  = &v->s;
3375 3376 3377 3378
    int mb_cbp         = v->cbp[s->mb_x - s->mb_stride],
        block_cbp      = mb_cbp      >> (block_num * 4), bottom_cbp,
        mb_is_intra    = v->is_intra[s->mb_x - s->mb_stride],
        block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3379
    int idx, linesize  = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3380 3381
    uint8_t *dst;

3382
    if (block_num > 3) {
3383 3384 3385 3386
        dst      = s->dest[block_num - 3];
    } else {
        dst      = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
    }
3387
    if (s->mb_y != s->end_mb_y || block_num < 2) {
3388 3389 3390
        int16_t (*mv)[2];
        int mv_stride;

3391
        if (block_num > 3) {
3392 3393 3394 3395 3396
            bottom_cbp      = v->cbp[s->mb_x]      >> (block_num * 4);
            bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
            mv              = &v->luma_mv[s->mb_x - s->mb_stride];
            mv_stride       = s->mb_stride;
        } else {
3397 3398 3399 3400
            bottom_cbp      = (block_num < 2) ? (mb_cbp               >> ((block_num + 2) * 4))
                                              : (v->cbp[s->mb_x]      >> ((block_num - 2) * 4));
            bottom_is_intra = (block_num < 2) ? (mb_is_intra          >> ((block_num + 2) * 4))
                                              : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3401
            mv_stride       = s->b8_stride;
3402
            mv              = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3403 3404 3405 3406 3407 3408 3409
        }

        if (bottom_is_intra & 1 || block_is_intra & 1 ||
            mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
            v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
        } else {
            idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3410
            if (idx == 3) {
3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421
                v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
            } else if (idx) {
                if (idx == 1)
                    v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
                else
                    v->vc1dsp.vc1_v_loop_filter4(dst,     linesize, v->pq);
            }
        }
    }

    dst -= 4 * linesize;
3422
    ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
    if (ttblk == TT_4X4 || ttblk == TT_8X4) {
        idx = (block_cbp | (block_cbp >> 2)) & 3;
        if (idx == 3) {
            v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
        } else if (idx) {
            if (idx == 1)
                v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
            else
                v->vc1dsp.vc1_v_loop_filter4(dst,     linesize, v->pq);
        }
    }
}

static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
{
3438
    MpegEncContext *s  = &v->s;
3439 3440 3441 3442
    int mb_cbp         = v->cbp[s->mb_x - 1 - s->mb_stride],
        block_cbp      = mb_cbp      >> (block_num * 4), right_cbp,
        mb_is_intra    = v->is_intra[s->mb_x - 1 - s->mb_stride],
        block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3443
    int idx, linesize  = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454
    uint8_t *dst;

    if (block_num > 3) {
        dst = s->dest[block_num - 3] - 8 * linesize;
    } else {
        dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
    }

    if (s->mb_x != s->mb_width || !(block_num & 5)) {
        int16_t (*mv)[2];

3455
        if (block_num > 3) {
3456 3457 3458
            right_cbp      = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
            right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
            mv             = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3459 3460 3461 3462 3463
        } else {
            right_cbp      = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride]      >> ((block_num - 1) * 4))
                                             : (mb_cbp                              >> ((block_num + 1) * 4));
            right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
                                             : (mb_is_intra                         >> ((block_num + 1) * 4));
3464
            mv             = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3465 3466 3467 3468 3469 3470 3471 3472 3473
        }
        if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
            v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
        } else {
            idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
            if (idx == 5) {
                v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
            } else if (idx) {
                if (idx == 1)
3474
                    v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3475
                else
3476
                    v->vc1dsp.vc1_h_loop_filter4(dst,                linesize, v->pq);
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488
            }
        }
    }

    dst -= 4;
    ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
    if (ttblk == TT_4X4 || ttblk == TT_4X8) {
        idx = (block_cbp | (block_cbp >> 1)) & 5;
        if (idx == 5) {
            v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
        } else if (idx) {
            if (idx == 1)
3489
                v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3490
            else
3491
                v->vc1dsp.vc1_h_loop_filter4(dst,                linesize, v->pq);
3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
        }
    }
}

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

    for (i = 0; i < 6; i++) {
        vc1_apply_p_v_loop_filter(v, i);
    }

3505
    /* V always precedes H, therefore we run H one MB before V;
3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520
     * at the end of a row, we catch up to complete the row */
    if (s->mb_x) {
        for (i = 0; i < 6; i++) {
            vc1_apply_p_h_loop_filter(v, i);
        }
        if (s->mb_x == s->mb_width - 1) {
            s->mb_x++;
            ff_update_block_index(s);
            for (i = 0; i < 6; i++) {
                vc1_apply_p_h_loop_filter(v, i);
            }
        }
    }
}

3521
/** Decode one P-frame MB
3522 3523 3524 3525 3526
 */
static int vc1_decode_p_mb(VC1Context *v)
{
    MpegEncContext *s = &v->s;
    GetBitContext *gb = &s->gb;
3527
    int i, j;
3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    int cbp; /* cbp decoding stuff */
    int mqdiff, mquant; /* MB quantization */
    int ttmb = v->ttfrm; /* MB Transform type */

    int mb_has_coeffs = 1; /* last_flag */
    int dmv_x, dmv_y; /* Differential MV components */
    int index, index1; /* LUT indexes */
    int val, sign; /* temp values */
    int first_block = 1;
    int dst_idx, off;
    int skipped, fourmv;
3540
    int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3541

3542
    mquant = v->pq; /* lossy initialization */
3543 3544 3545 3546 3547 3548 3549 3550 3551 3552

    if (v->mv_type_is_raw)
        fourmv = get_bits1(gb);
    else
        fourmv = v->mv_type_mb_plane[mb_pos];
    if (v->skip_is_raw)
        skipped = get_bits1(gb);
    else
        skipped = v->s.mbskip_table[mb_pos];

3553 3554
    if (!fourmv) { /* 1MV mode */
        if (!skipped) {
3555 3556 3557
            GET_MVDATA(dmv_x, dmv_y);

            if (s->mb_intra) {
3558 3559
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3560
            }
3561
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3562
            vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3563 3564

            /* FIXME Set DC val for inter block ? */
3565
            if (s->mb_intra && !mb_has_coeffs) {
3566 3567
                GET_MQUANT();
                s->ac_pred = get_bits1(gb);
3568 3569
                cbp        = 0;
            } else if (mb_has_coeffs) {
3570 3571
                if (s->mb_intra)
                    s->ac_pred = get_bits1(gb);
3572 3573
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
                GET_MQUANT();
3574
            } else {
3575
                mquant = v->pq;
3576
                cbp    = 0;
3577
            }
3578
            s->current_picture.qscale_table[mb_pos] = mquant;
3579 3580 3581 3582

            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
                                VC1_TTMB_VLC_BITS, 2);
3583
            if (!s->mb_intra) vc1_mc_1mv(v, 0);
3584
            dst_idx = 0;
3585
            for (i = 0; i < 6; i++) {
3586 3587 3588 3589 3590
                s->dc_val[0][s->block_index[i]] = 0;
                dst_idx += i >> 2;
                val = ((cbp >> (5 - i)) & 1);
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
3591
                if (s->mb_intra) {
3592 3593
                    /* check if prediction blocks A and C are available */
                    v->a_avail = v->c_avail = 0;
3594
                    if (i == 2 || i == 3 || !s->first_slice_line)
3595
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3596
                    if (i == 1 || i == 3 || s->mb_x)
3597 3598
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];

3599 3600 3601 3602
                    vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                           (i & 4) ? v->codingset2 : v->codingset);
                    if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
                        continue;
3603
                    v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3604 3605 3606
                    if (v->rangeredfrm)
                        for (j = 0; j < 64; j++)
                            s->block[i][j] <<= 1;
3607
                    s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3608 3609
                    if (v->pq >= 9 && v->overlap) {
                        if (v->c_avail)
3610
                            v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3611
                        if (v->a_avail)
3612
                            v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3613
                    }
3614
                    block_cbp   |= 0xF << (i << 2);
3615
                    block_intra |= 1 << i;
3616 3617 3618 3619
                } else if (val) {
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
                                             s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
                                             (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3620
                    block_cbp |= pat << (i << 2);
3621 3622
                    if (!v->ttmbf && ttmb < 8)
                        ttmb = -1;
3623 3624 3625
                    first_block = 0;
                }
            }
3626
        } else { // skipped
3627
            s->mb_intra = 0;
3628
            for (i = 0; i < 6; i++) {
3629
                v->mb_type[0][s->block_index[i]] = 0;
3630
                s->dc_val[0][s->block_index[i]]  = 0;
3631
            }
3632 3633
            s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
            s->current_picture.qscale_table[mb_pos] = 0;
3634
            vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3635 3636
            vc1_mc_1mv(v, 0);
        }
3637 3638
    } else { // 4MV mode
        if (!skipped /* unskipped MB */) {
3639 3640 3641 3642
            int intra_count = 0, coded_inter = 0;
            int is_intra[6], is_coded[6];
            /* Get CBPCY */
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3643
            for (i = 0; i < 6; i++) {
3644 3645
                val = ((cbp >> (5 - i)) & 1);
                s->dc_val[0][s->block_index[i]] = 0;
3646 3647
                s->mb_intra                     = 0;
                if (i < 4) {
3648
                    dmv_x = dmv_y = 0;
3649
                    s->mb_intra   = 0;
3650
                    mb_has_coeffs = 0;
3651
                    if (val) {
3652 3653
                        GET_MVDATA(dmv_x, dmv_y);
                    }
3654
                    vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3655
                    if (!s->mb_intra)
3656
                        vc1_mc_4mv_luma(v, i, 0, 0);
3657
                    intra_count += s->mb_intra;
3658 3659
                    is_intra[i]  = s->mb_intra;
                    is_coded[i]  = mb_has_coeffs;
3660
                }
3661
                if (i & 4) {
3662 3663 3664
                    is_intra[i] = (intra_count >= 3);
                    is_coded[i] = val;
                }
3665 3666
                if (i == 4)
                    vc1_mc_4mv_chroma(v, 0);
3667
                v->mb_type[0][s->block_index[i]] = is_intra[i];
3668 3669
                if (!coded_inter)
                    coded_inter = !is_intra[i] & is_coded[i];
3670 3671 3672
            }
            // if there are no coded blocks then don't do anything more
            dst_idx = 0;
3673
            if (!intra_count && !coded_inter)
3674
                goto end;
3675
            GET_MQUANT();
3676
            s->current_picture.qscale_table[mb_pos] = mquant;
3677 3678 3679
            /* test if block is intra and has pred */
            {
                int intrapred = 0;
3680 3681 3682 3683
                for (i = 0; i < 6; i++)
                    if (is_intra[i]) {
                        if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
                            || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3684 3685 3686 3687
                            intrapred = 1;
                            break;
                        }
                    }
3688 3689 3690 3691
                if (intrapred)
                    s->ac_pred = get_bits1(gb);
                else
                    s->ac_pred = 0;
3692 3693 3694
            }
            if (!v->ttmbf && coded_inter)
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3695 3696 3697
            for (i = 0; i < 6; i++) {
                dst_idx    += i >> 2;
                off         = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3698 3699 3700 3701
                s->mb_intra = is_intra[i];
                if (is_intra[i]) {
                    /* check if prediction blocks A and C are available */
                    v->a_avail = v->c_avail = 0;
3702
                    if (i == 2 || i == 3 || !s->first_slice_line)
3703
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3704
                    if (i == 1 || i == 3 || s->mb_x)
3705 3706
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];

3707 3708 3709 3710
                    vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
                                           (i & 4) ? v->codingset2 : v->codingset);
                    if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
                        continue;
3711
                    v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3712 3713 3714 3715 3716 3717 3718
                    if (v->rangeredfrm)
                        for (j = 0; j < 64; j++)
                            s->block[i][j] <<= 1;
                    s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
                                                     (i & 4) ? s->uvlinesize : s->linesize);
                    if (v->pq >= 9 && v->overlap) {
                        if (v->c_avail)
3719
                            v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3720
                        if (v->a_avail)
3721
                            v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3722
                    }
3723
                    block_cbp   |= 0xF << (i << 2);
3724
                    block_intra |= 1 << i;
3725 3726 3727 3728 3729 3730
                } else if (is_coded[i]) {
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
                                             first_block, s->dest[dst_idx] + off,
                                             (i & 4) ? s->uvlinesize : s->linesize,
                                             (i & 4) && (s->flags & CODEC_FLAG_GRAY),
                                             &block_tt);
3731
                    block_cbp |= pat << (i << 2);
3732 3733
                    if (!v->ttmbf && ttmb < 8)
                        ttmb = -1;
3734 3735 3736
                    first_block = 0;
                }
            }
3737 3738
        } else { // skipped MB
            s->mb_intra                               = 0;
3739
            s->current_picture.qscale_table[mb_pos] = 0;
3740
            for (i = 0; i < 6; i++) {
3741
                v->mb_type[0][s->block_index[i]] = 0;
3742
                s->dc_val[0][s->block_index[i]]  = 0;
3743
            }
3744
            for (i = 0; i < 4; i++) {
3745
                vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3746
                vc1_mc_4mv_luma(v, i, 0, 0);
3747
            }
3748
            vc1_mc_4mv_chroma(v, 0);
3749
            s->current_picture.qscale_table[mb_pos] = 0;
3750 3751
        }
    }
3752
end:
3753 3754
    v->cbp[s->mb_x]      = block_cbp;
    v->ttblk[s->mb_x]    = block_tt;
3755
    v->is_intra[s->mb_x] = block_intra;
3756

3757
    return 0;
3758 3759
}

3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781
/* Decode one macroblock in an interlaced frame p picture */

static int vc1_decode_p_mb_intfr(VC1Context *v)
{
    MpegEncContext *s = &v->s;
    GetBitContext *gb = &s->gb;
    int i;
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    int cbp = 0; /* cbp decoding stuff */
    int mqdiff, mquant; /* MB quantization */
    int ttmb = v->ttfrm; /* MB Transform type */

    int mb_has_coeffs = 1; /* last_flag */
    int dmv_x, dmv_y; /* Differential MV components */
    int val; /* temp value */
    int first_block = 1;
    int dst_idx, off;
    int skipped, fourmv = 0, twomv = 0;
    int block_cbp = 0, pat, block_tt = 0;
    int idx_mbmode = 0, mvbp;
    int stride_y, fieldtx;

Lou Logan's avatar
Lou Logan committed
3782
    mquant = v->pq; /* Lossy initialization */
3783 3784 3785 3786 3787 3788 3789 3790 3791

    if (v->skip_is_raw)
        skipped = get_bits1(gb);
    else
        skipped = v->s.mbskip_table[mb_pos];
    if (!skipped) {
        if (v->fourmvswitch)
            idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
        else
3792
            idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823
        switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
        /* store the motion vector type in a flag (useful later) */
        case MV_PMODE_INTFR_4MV:
            fourmv = 1;
            v->blk_mv_type[s->block_index[0]] = 0;
            v->blk_mv_type[s->block_index[1]] = 0;
            v->blk_mv_type[s->block_index[2]] = 0;
            v->blk_mv_type[s->block_index[3]] = 0;
            break;
        case MV_PMODE_INTFR_4MV_FIELD:
            fourmv = 1;
            v->blk_mv_type[s->block_index[0]] = 1;
            v->blk_mv_type[s->block_index[1]] = 1;
            v->blk_mv_type[s->block_index[2]] = 1;
            v->blk_mv_type[s->block_index[3]] = 1;
            break;
        case MV_PMODE_INTFR_2MV_FIELD:
            twomv = 1;
            v->blk_mv_type[s->block_index[0]] = 1;
            v->blk_mv_type[s->block_index[1]] = 1;
            v->blk_mv_type[s->block_index[2]] = 1;
            v->blk_mv_type[s->block_index[3]] = 1;
            break;
        case MV_PMODE_INTFR_1MV:
            v->blk_mv_type[s->block_index[0]] = 0;
            v->blk_mv_type[s->block_index[1]] = 0;
            v->blk_mv_type[s->block_index[2]] = 0;
            v->blk_mv_type[s->block_index[3]] = 0;
            break;
        }
        if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3824 3825 3826 3827
            for (i = 0; i < 4; i++) {
                s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
                s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
            }
3828
            s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
3829
            s->mb_intra = v->is_intra[s->mb_x] = 1;
3830 3831
            for (i = 0; i < 6; i++)
                v->mb_type[0][s->block_index[i]] = 1;
3832 3833 3834 3835 3836 3837
            fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
            mb_has_coeffs = get_bits1(gb);
            if (mb_has_coeffs)
                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
            v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
            GET_MQUANT();
3838
            s->current_picture.qscale_table[mb_pos] = mquant;
3839 3840 3841 3842
            /* Set DC scale - y and c use the same (not sure if necessary here) */
            s->y_dc_scale = s->y_dc_scale_table[mquant];
            s->c_dc_scale = s->c_dc_scale_table[mquant];
            dst_idx = 0;
3843
            for (i = 0; i < 6; i++) {
3844 3845 3846 3847 3848
                s->dc_val[0][s->block_index[i]] = 0;
                dst_idx += i >> 2;
                val = ((cbp >> (5 - i)) & 1);
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
                v->a_avail = v->c_avail = 0;
3849
                if (i == 2 || i == 3 || !s->first_slice_line)
3850
                    v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3851
                if (i == 1 || i == 3 || s->mb_x)
3852 3853
                    v->c_avail = v->mb_type[0][s->block_index[i] - 1];

3854 3855 3856
                vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                       (i & 4) ? v->codingset2 : v->codingset);
                if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868
                v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
                if (i < 4) {
                    stride_y = s->linesize << fieldtx;
                    off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
                } else {
                    stride_y = s->uvlinesize;
                    off = 0;
                }
                s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
                //TODO: loop filter
            }

3869
        } else { // inter MB
3870 3871 3872 3873 3874 3875 3876
            mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
            if (mb_has_coeffs)
                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
            if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
                v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
            } else {
                if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3877
                    || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3878 3879 3880 3881
                    v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
                }
            }
            s->mb_intra = v->is_intra[s->mb_x] = 0;
3882 3883
            for (i = 0; i < 6; i++)
                v->mb_type[0][s->block_index[i]] = 0;
3884 3885 3886 3887 3888
            fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
            /* for all motion vector read MVDATA and motion compensate each block */
            dst_idx = 0;
            if (fourmv) {
                mvbp = v->fourmvbp;
3889
                for (i = 0; i < 6; i++) {
3890 3891
                    if (i < 4) {
                        dmv_x = dmv_y = 0;
3892 3893
                        val   = ((mvbp >> (3 - i)) & 1);
                        if (val) {
3894 3895
                            get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                        }
3896 3897
                        vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
                        vc1_mc_4mv_luma(v, i, 0, 0);
3898
                    } else if (i == 4) {
3899
                        vc1_mc_4mv_chroma4(v, 0, 0, 0);
3900 3901 3902
                    }
                }
            } else if (twomv) {
3903
                mvbp  = v->twomvbp;
3904 3905 3906 3907
                dmv_x = dmv_y = 0;
                if (mvbp & 2) {
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                }
3908 3909 3910
                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
                vc1_mc_4mv_luma(v, 0, 0, 0);
                vc1_mc_4mv_luma(v, 1, 0, 0);
3911 3912 3913 3914
                dmv_x = dmv_y = 0;
                if (mvbp & 1) {
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                }
3915 3916 3917
                vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
                vc1_mc_4mv_luma(v, 2, 0, 0);
                vc1_mc_4mv_luma(v, 3, 0, 0);
3918
                vc1_mc_4mv_chroma4(v, 0, 0, 0);
3919 3920
            } else {
                mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3921
                dmv_x = dmv_y = 0;
3922 3923 3924
                if (mvbp) {
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                }
3925
                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3926 3927 3928 3929
                vc1_mc_1mv(v, 0);
            }
            if (cbp)
                GET_MQUANT();  // p. 227
3930
            s->current_picture.qscale_table[mb_pos] = mquant;
3931 3932
            if (!v->ttmbf && cbp)
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3933
            for (i = 0; i < 6; i++) {
3934 3935 3936 3937 3938 3939 3940 3941
                s->dc_val[0][s->block_index[i]] = 0;
                dst_idx += i >> 2;
                val = ((cbp >> (5 - i)) & 1);
                if (!fieldtx)
                    off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
                else
                    off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
                if (val) {
3942 3943 3944 3945
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
                                             first_block, s->dest[dst_idx] + off,
                                             (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
                                             (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3946 3947 3948 3949 3950 3951 3952 3953 3954
                    block_cbp |= pat << (i << 2);
                    if (!v->ttmbf && ttmb < 8)
                        ttmb = -1;
                    first_block = 0;
                }
            }
        }
    } else { // skipped
        s->mb_intra = v->is_intra[s->mb_x] = 0;
3955
        for (i = 0; i < 6; i++) {
3956 3957 3958
            v->mb_type[0][s->block_index[i]] = 0;
            s->dc_val[0][s->block_index[i]] = 0;
        }
3959 3960
        s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
        s->current_picture.qscale_table[mb_pos] = 0;
3961 3962 3963 3964
        v->blk_mv_type[s->block_index[0]] = 0;
        v->blk_mv_type[s->block_index[1]] = 0;
        v->blk_mv_type[s->block_index[2]] = 0;
        v->blk_mv_type[s->block_index[3]] = 0;
3965
        vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987
        vc1_mc_1mv(v, 0);
    }
    if (s->mb_x == s->mb_width - 1)
        memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
    return 0;
}

static int vc1_decode_p_mb_intfi(VC1Context *v)
{
    MpegEncContext *s = &v->s;
    GetBitContext *gb = &s->gb;
    int i;
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    int cbp = 0; /* cbp decoding stuff */
    int mqdiff, mquant; /* MB quantization */
    int ttmb = v->ttfrm; /* MB Transform type */

    int mb_has_coeffs = 1; /* last_flag */
    int dmv_x, dmv_y; /* Differential MV components */
    int val; /* temp values */
    int first_block = 1;
    int dst_idx, off;
3988
    int pred_flag = 0;
3989 3990 3991
    int block_cbp = 0, pat, block_tt = 0;
    int idx_mbmode = 0;

Lou Logan's avatar
Lou Logan committed
3992
    mquant = v->pq; /* Lossy initialization */
3993 3994 3995 3996

    idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
    if (idx_mbmode <= 1) { // intra MB
        s->mb_intra = v->is_intra[s->mb_x] = 1;
3997 3998 3999
        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4000
        GET_MQUANT();
4001
        s->current_picture.qscale_table[mb_pos] = mquant;
4002 4003 4004
        /* Set DC scale - y and c use the same (not sure if necessary here) */
        s->y_dc_scale = s->y_dc_scale_table[mquant];
        s->c_dc_scale = s->c_dc_scale_table[mquant];
4005
        v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
4006 4007 4008 4009
        mb_has_coeffs = idx_mbmode & 1;
        if (mb_has_coeffs)
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
        dst_idx = 0;
4010 4011
        for (i = 0; i < 6; i++) {
            s->dc_val[0][s->block_index[i]]  = 0;
4012 4013 4014 4015
            v->mb_type[0][s->block_index[i]] = 1;
            dst_idx += i >> 2;
            val = ((cbp >> (5 - i)) & 1);
            v->a_avail = v->c_avail = 0;
4016
            if (i == 2 || i == 3 || !s->first_slice_line)
4017
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4018
            if (i == 1 || i == 3 || s->mb_x)
4019 4020
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];

4021 4022 4023 4024
            vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                   (i & 4) ? v->codingset2 : v->codingset);
            if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
                continue;
4025
            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4026
            off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4027
            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4028
            // TODO: loop filter
4029 4030 4031
        }
    } else {
        s->mb_intra = v->is_intra[s->mb_x] = 0;
4032
        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4033 4034
        for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
        if (idx_mbmode <= 5) { // 1-MV
4035
            dmv_x = dmv_y = pred_flag = 0;
4036 4037 4038 4039 4040 4041 4042 4043
            if (idx_mbmode & 1) {
                get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
            }
            vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
            vc1_mc_1mv(v, 0);
            mb_has_coeffs = !(idx_mbmode & 2);
        } else { // 4-MV
            v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4044
            for (i = 0; i < 6; i++) {
4045 4046
                if (i < 4) {
                    dmv_x = dmv_y = pred_flag = 0;
4047 4048
                    val   = ((v->fourmvbp >> (3 - i)) & 1);
                    if (val) {
4049 4050 4051
                        get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
                    }
                    vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4052
                    vc1_mc_4mv_luma(v, i, 0, 0);
4053 4054 4055 4056 4057 4058 4059 4060 4061 4062
                } else if (i == 4)
                    vc1_mc_4mv_chroma(v, 0);
            }
            mb_has_coeffs = idx_mbmode & 1;
        }
        if (mb_has_coeffs)
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
        if (cbp) {
            GET_MQUANT();
        }
4063
        s->current_picture.qscale_table[mb_pos] = mquant;
4064 4065 4066 4067
        if (!v->ttmbf && cbp) {
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
        }
        dst_idx = 0;
4068
        for (i = 0; i < 6; i++) {
4069 4070 4071 4072
            s->dc_val[0][s->block_index[i]] = 0;
            dst_idx += i >> 2;
            val = ((cbp >> (5 - i)) & 1);
            off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4073 4074 4075 4076 4077 4078
            if (val) {
                pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
                                         first_block, s->dest[dst_idx] + off,
                                         (i & 4) ? s->uvlinesize : s->linesize,
                                         (i & 4) && (s->flags & CODEC_FLAG_GRAY),
                                         &block_tt);
4079
                block_cbp |= pat << (i << 2);
4080
                if (!v->ttmbf && ttmb < 8) ttmb = -1;
4081 4082 4083 4084 4085
                first_block = 0;
            }
        }
    }
    if (s->mb_x == s->mb_width - 1)
4086
        memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4087 4088 4089
    return 0;
}

4090 4091 4092 4093 4094 4095
/** Decode one B-frame MB (in Main profile)
 */
static void vc1_decode_b_mb(VC1Context *v)
{
    MpegEncContext *s = &v->s;
    GetBitContext *gb = &s->gb;
4096
    int i, j;
4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    int cbp = 0; /* cbp decoding stuff */
    int mqdiff, mquant; /* MB quantization */
    int ttmb = v->ttfrm; /* MB Transform type */
    int mb_has_coeffs = 0; /* last_flag */
    int index, index1; /* LUT indexes */
    int val, sign; /* temp values */
    int first_block = 1;
    int dst_idx, off;
    int skipped, direct;
    int dmv_x[2], dmv_y[2];
    int bmvtype = BMV_TYPE_BACKWARD;

4110
    mquant      = v->pq; /* lossy initialization */
4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122
    s->mb_intra = 0;

    if (v->dmb_is_raw)
        direct = get_bits1(gb);
    else
        direct = v->direct_mb_plane[mb_pos];
    if (v->skip_is_raw)
        skipped = get_bits1(gb);
    else
        skipped = v->s.mbskip_table[mb_pos];

    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4123
    for (i = 0; i < 6; i++) {
4124
        v->mb_type[0][s->block_index[i]] = 0;
4125
        s->dc_val[0][s->block_index[i]]  = 0;
4126
    }
4127
    s->current_picture.qscale_table[mb_pos] = 0;
4128 4129 4130 4131 4132 4133 4134

    if (!direct) {
        if (!skipped) {
            GET_MVDATA(dmv_x[0], dmv_y[0]);
            dmv_x[1] = dmv_x[0];
            dmv_y[1] = dmv_y[0];
        }
4135
        if (skipped || !s->mb_intra) {
4136
            bmvtype = decode012(gb);
4137
            switch (bmvtype) {
4138 4139 4140 4141 4142 4143 4144
            case 0:
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
                break;
            case 1:
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
                break;
            case 2:
4145
                bmvtype  = BMV_TYPE_INTERPOLATED;
4146 4147 4148 4149
                dmv_x[0] = dmv_y[0] = 0;
            }
        }
    }
4150
    for (i = 0; i < 6; i++)
4151 4152 4153
        v->mb_type[0][s->block_index[i]] = s->mb_intra;

    if (skipped) {
4154 4155
        if (direct)
            bmvtype = BMV_TYPE_INTERPOLATED;
4156 4157 4158 4159 4160 4161 4162 4163
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
        return;
    }
    if (direct) {
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
        GET_MQUANT();
        s->mb_intra = 0;
4164
        s->current_picture.qscale_table[mb_pos] = mquant;
4165
        if (!v->ttmbf)
4166 4167 4168 4169 4170
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
        dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
    } else {
4171
        if (!mb_has_coeffs && !s->mb_intra) {
4172 4173 4174 4175 4176
            /* no coded blocks - effectively skipped */
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
            return;
        }
4177
        if (s->mb_intra && !mb_has_coeffs) {
4178
            GET_MQUANT();
4179
            s->current_picture.qscale_table[mb_pos] = mquant;
4180 4181 4182 4183
            s->ac_pred = get_bits1(gb);
            cbp = 0;
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
        } else {
4184
            if (bmvtype == BMV_TYPE_INTERPOLATED) {
4185
                GET_MVDATA(dmv_x[0], dmv_y[0]);
4186
                if (!mb_has_coeffs) {
4187 4188 4189 4190 4191 4192 4193
                    /* interpolated skipped block */
                    vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
                    return;
                }
            }
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4194
            if (!s->mb_intra) {
4195 4196
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
            }
4197
            if (s->mb_intra)
4198 4199 4200
                s->ac_pred = get_bits1(gb);
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
            GET_MQUANT();
4201
            s->current_picture.qscale_table[mb_pos] = mquant;
4202
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4203 4204 4205 4206
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
        }
    }
    dst_idx = 0;
4207
    for (i = 0; i < 6; i++) {
4208 4209 4210 4211 4212
        s->dc_val[0][s->block_index[i]] = 0;
        dst_idx += i >> 2;
        val = ((cbp >> (5 - i)) & 1);
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
4213
        if (s->mb_intra) {
4214 4215
            /* check if prediction blocks A and C are available */
            v->a_avail = v->c_avail = 0;
4216
            if (i == 2 || i == 3 || !s->first_slice_line)
4217
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4218
            if (i == 1 || i == 3 || s->mb_x)
4219 4220
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];

4221 4222 4223 4224
            vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                   (i & 4) ? v->codingset2 : v->codingset);
            if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
                continue;
4225
            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4226 4227 4228
            if (v->rangeredfrm)
                for (j = 0; j < 64; j++)
                    s->block[i][j] <<= 1;
4229
            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4230 4231 4232 4233 4234 4235 4236
        } else if (val) {
            vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
                               first_block, s->dest[dst_idx] + off,
                               (i & 4) ? s->uvlinesize : s->linesize,
                               (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
            if (!v->ttmbf && ttmb < 8)
                ttmb = -1;
4237 4238 4239 4240 4241
            first_block = 0;
        }
    }
}

4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259
/** Decode one B-frame MB (in interlaced field B picture)
 */
static void vc1_decode_b_mb_intfi(VC1Context *v)
{
    MpegEncContext *s = &v->s;
    GetBitContext *gb = &s->gb;
    int i, j;
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    int cbp = 0; /* cbp decoding stuff */
    int mqdiff, mquant; /* MB quantization */
    int ttmb = v->ttfrm; /* MB Transform type */
    int mb_has_coeffs = 0; /* last_flag */
    int val; /* temp value */
    int first_block = 1;
    int dst_idx, off;
    int fwd;
    int dmv_x[2], dmv_y[2], pred_flag[2];
    int bmvtype = BMV_TYPE_BACKWARD;
4260
    int idx_mbmode;
4261

Lou Logan's avatar
Lou Logan committed
4262
    mquant      = v->pq; /* Lossy initialization */
4263 4264 4265 4266 4267
    s->mb_intra = 0;

    idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
    if (idx_mbmode <= 1) { // intra MB
        s->mb_intra = v->is_intra[s->mb_x] = 1;
4268 4269 4270
        s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
        s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
        s->current_picture.mb_type[mb_pos + v->mb_off]         = MB_TYPE_INTRA;
4271
        GET_MQUANT();
4272
        s->current_picture.qscale_table[mb_pos] = mquant;
4273 4274 4275
        /* Set DC scale - y and c use the same (not sure if necessary here) */
        s->y_dc_scale = s->y_dc_scale_table[mquant];
        s->c_dc_scale = s->c_dc_scale_table[mquant];
4276
        v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
4277 4278 4279 4280
        mb_has_coeffs = idx_mbmode & 1;
        if (mb_has_coeffs)
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
        dst_idx = 0;
4281
        for (i = 0; i < 6; i++) {
4282 4283 4284 4285
            s->dc_val[0][s->block_index[i]] = 0;
            dst_idx += i >> 2;
            val = ((cbp >> (5 - i)) & 1);
            v->mb_type[0][s->block_index[i]] = s->mb_intra;
4286 4287
            v->a_avail                       = v->c_avail = 0;
            if (i == 2 || i == 3 || !s->first_slice_line)
4288
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4289
            if (i == 1 || i == 3 || s->mb_x)
4290 4291
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];

4292 4293 4294 4295
            vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                   (i & 4) ? v->codingset2 : v->codingset);
            if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
                continue;
4296
            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4297 4298 4299 4300
            if (v->rangeredfrm)
                for (j = 0; j < 64; j++)
                    s->block[i][j] <<= 1;
            off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4301
            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4302
            // TODO: yet to perform loop filter
4303 4304 4305
        }
    } else {
        s->mb_intra = v->is_intra[s->mb_x] = 0;
4306
        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4307 4308 4309 4310 4311 4312
        for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
        if (v->fmb_is_raw)
            fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
        else
            fwd = v->forward_mb_plane[mb_pos];
        if (idx_mbmode <= 5) { // 1-MV
4313
            int interpmvp = 0;
4314
            dmv_x[0]     = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4315 4316 4317 4318 4319
            pred_flag[0] = pred_flag[1] = 0;
            if (fwd)
                bmvtype = BMV_TYPE_FORWARD;
            else {
                bmvtype = decode012(gb);
4320
                switch (bmvtype) {
4321 4322 4323 4324 4325 4326 4327
                case 0:
                    bmvtype = BMV_TYPE_BACKWARD;
                    break;
                case 1:
                    bmvtype = BMV_TYPE_DIRECT;
                    break;
                case 2:
4328
                    bmvtype   = BMV_TYPE_INTERPOLATED;
4329 4330 4331 4332 4333 4334 4335
                    interpmvp = get_bits1(gb);
                }
            }
            v->bmvtype = bmvtype;
            if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
                get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
            }
4336
            if (interpmvp) {
4337 4338 4339 4340 4341
                get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
            }
            if (bmvtype == BMV_TYPE_DIRECT) {
                dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
                dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4342 4343 4344 4345
                if (!s->next_picture_ptr->field_picture) {
                    av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
                    return;
                }
4346 4347 4348 4349 4350 4351 4352
            }
            vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
            vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
            mb_has_coeffs = !(idx_mbmode & 2);
        } else { // 4-MV
            if (fwd)
                bmvtype = BMV_TYPE_FORWARD;
4353
            v->bmvtype  = bmvtype;
4354
            v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4355
            for (i = 0; i < 6; i++) {
4356 4357 4358 4359
                if (i < 4) {
                    dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
                    dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
                    val = ((v->fourmvbp >> (3 - i)) & 1);
4360 4361 4362 4363
                    if (val) {
                        get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
                                                 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
                                             &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4364 4365
                    }
                    vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4366
                    vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4367 4368 4369 4370 4371 4372 4373 4374 4375 4376
                } else if (i == 4)
                    vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
            }
            mb_has_coeffs = idx_mbmode & 1;
        }
        if (mb_has_coeffs)
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
        if (cbp) {
            GET_MQUANT();
        }
4377
        s->current_picture.qscale_table[mb_pos] = mquant;
4378 4379 4380 4381
        if (!v->ttmbf && cbp) {
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
        }
        dst_idx = 0;
4382
        for (i = 0; i < 6; i++) {
4383 4384 4385 4386
            s->dc_val[0][s->block_index[i]] = 0;
            dst_idx += i >> 2;
            val = ((cbp >> (5 - i)) & 1);
            off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4387 4388 4389 4390 4391 4392
            if (val) {
                vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
                                   first_block, s->dest[dst_idx] + off,
                                   (i & 4) ? s->uvlinesize : s->linesize,
                                   (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
                if (!v->ttmbf && ttmb < 8)
4393 4394 4395 4396 4397 4398 4399
                    ttmb = -1;
                first_block = 0;
            }
        }
    }
}

4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432
/** Decode one B-frame MB (in interlaced frame B picture)
 */
static int vc1_decode_b_mb_intfr(VC1Context *v)
{
    MpegEncContext *s = &v->s;
    GetBitContext *gb = &s->gb;
    int i, j;
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    int cbp = 0; /* cbp decoding stuff */
    int mqdiff, mquant; /* MB quantization */
    int ttmb = v->ttfrm; /* MB Transform type */
    int mvsw = 0; /* motion vector switch */
    int mb_has_coeffs = 1; /* last_flag */
    int dmv_x, dmv_y; /* Differential MV components */
    int val; /* temp value */
    int first_block = 1;
    int dst_idx, off;
    int skipped, direct, twomv = 0;
    int block_cbp = 0, pat, block_tt = 0;
    int idx_mbmode = 0, mvbp;
    int stride_y, fieldtx;
    int bmvtype = BMV_TYPE_BACKWARD;
    int dir, dir2;

    mquant = v->pq; /* Lossy initialization */
    s->mb_intra = 0;
    if (v->skip_is_raw)
        skipped = get_bits1(gb);
    else
        skipped = v->s.mbskip_table[mb_pos];

    if (!skipped) {
        idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4433
        if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452
            twomv = 1;
            v->blk_mv_type[s->block_index[0]] = 1;
            v->blk_mv_type[s->block_index[1]] = 1;
            v->blk_mv_type[s->block_index[2]] = 1;
            v->blk_mv_type[s->block_index[3]] = 1;
        } else {
            v->blk_mv_type[s->block_index[0]] = 0;
            v->blk_mv_type[s->block_index[1]] = 0;
            v->blk_mv_type[s->block_index[2]] = 0;
            v->blk_mv_type[s->block_index[3]] = 0;
        }
    }

    if (v->dmb_is_raw)
        direct = get_bits1(gb);
    else
        direct = v->direct_mb_plane[mb_pos];

    if (direct) {
4453 4454
        if (s->next_picture_ptr->field_picture)
            av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465
        s->mv[0][0][0] = s->current_picture.motion_val[0][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 0, s->quarter_sample);
        s->mv[0][0][1] = s->current_picture.motion_val[0][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 0, s->quarter_sample);
        s->mv[1][0][0] = s->current_picture.motion_val[1][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 1, s->quarter_sample);
        s->mv[1][0][1] = s->current_picture.motion_val[1][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 1, s->quarter_sample);

        if (twomv) {
            s->mv[0][2][0] = s->current_picture.motion_val[0][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 0, s->quarter_sample);
            s->mv[0][2][1] = s->current_picture.motion_val[0][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 0, s->quarter_sample);
            s->mv[1][2][0] = s->current_picture.motion_val[1][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 1, s->quarter_sample);
            s->mv[1][2][1] = s->current_picture.motion_val[1][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 1, s->quarter_sample);

4466
            for (i = 1; i < 4; i += 2) {
4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488
                s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
                s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
                s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
                s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
            }
        } else {
            for (i = 1; i < 4; i++) {
                s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
                s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
                s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
                s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
            }
        }
    }

    if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
        for (i = 0; i < 4; i++) {
            s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
            s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
            s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
            s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
        }
4489
        s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516
        s->mb_intra = v->is_intra[s->mb_x] = 1;
        for (i = 0; i < 6; i++)
            v->mb_type[0][s->block_index[i]] = 1;
        fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
        mb_has_coeffs = get_bits1(gb);
        if (mb_has_coeffs)
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
        v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
        GET_MQUANT();
        s->current_picture.qscale_table[mb_pos] = mquant;
        /* Set DC scale - y and c use the same (not sure if necessary here) */
        s->y_dc_scale = s->y_dc_scale_table[mquant];
        s->c_dc_scale = s->c_dc_scale_table[mquant];
        dst_idx = 0;
        for (i = 0; i < 6; i++) {
            s->dc_val[0][s->block_index[i]] = 0;
            dst_idx += i >> 2;
            val = ((cbp >> (5 - i)) & 1);
            v->mb_type[0][s->block_index[i]] = s->mb_intra;
            v->a_avail = v->c_avail = 0;
            if (i == 2 || i == 3 || !s->first_slice_line)
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
            if (i == 1 || i == 3 || s->mb_x)
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];

            vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                   (i & 4) ? v->codingset2 : v->codingset);
4517 4518
            if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
                continue;
4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554
            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
            if (i < 4) {
                stride_y = s->linesize << fieldtx;
                off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
            } else {
                stride_y = s->uvlinesize;
                off = 0;
            }
            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
        }
    } else {
        s->mb_intra = v->is_intra[s->mb_x] = 0;
        if (!direct) {
            if (skipped || !s->mb_intra) {
                bmvtype = decode012(gb);
                switch (bmvtype) {
                case 0:
                    bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
                    break;
                case 1:
                    bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
                    break;
                case 2:
                    bmvtype  = BMV_TYPE_INTERPOLATED;
                }
            }

            if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
                mvsw = get_bits1(gb);
        }

        if (!skipped) { // inter MB
            mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
            if (mb_has_coeffs)
                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
            if (!direct) {
4555
                if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
4556
                    v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4557
                } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572
                    v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
                }
            }

            for (i = 0; i < 6; i++)
                v->mb_type[0][s->block_index[i]] = 0;
            fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
            /* for all motion vector read MVDATA and motion compensate each block */
            dst_idx = 0;
            if (direct) {
                if (twomv) {
                    for (i = 0; i < 4; i++) {
                        vc1_mc_4mv_luma(v, i, 0, 0);
                        vc1_mc_4mv_luma(v, i, 1, 1);
                    }
4573
                    vc1_mc_4mv_chroma4(v, 0, 0, 0);
4574
                    vc1_mc_4mv_chroma4(v, 1, 1, 1);
4575 4576 4577 4578 4579 4580 4581 4582 4583 4584
                } else {
                    vc1_mc_1mv(v, 0);
                    vc1_interp_mc(v);
                }
            } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
                mvbp = v->fourmvbp;
                for (i = 0; i < 4; i++) {
                    dir = i==1 || i==3;
                    dmv_x = dmv_y = 0;
                    val = ((mvbp >> (3 - i)) & 1);
4585
                    if (val)
4586 4587 4588 4589 4590 4591 4592
                        get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                    j = i > 1 ? 2 : 0;
                    vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
                    vc1_mc_4mv_luma(v, j, dir, dir);
                    vc1_mc_4mv_luma(v, j+1, dir, dir);
                }

4593
                vc1_mc_4mv_chroma4(v, 0, 0, 0);
4594
                vc1_mc_4mv_chroma4(v, 1, 1, 1);
4595 4596 4597
            } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
                mvbp = v->twomvbp;
                dmv_x = dmv_y = 0;
4598
                if (mvbp & 2)
4599 4600 4601 4602 4603 4604
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);

                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
                vc1_mc_1mv(v, 0);

                dmv_x = dmv_y = 0;
4605
                if (mvbp & 1)
4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);

                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
                vc1_interp_mc(v);
            } else if (twomv) {
                dir = bmvtype == BMV_TYPE_BACKWARD;
                dir2 = dir;
                if (mvsw)
                    dir2 = !dir;
                mvbp = v->twomvbp;
                dmv_x = dmv_y = 0;
4617
                if (mvbp & 2)
4618 4619 4620 4621
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);

                dmv_x = dmv_y = 0;
4622
                if (mvbp & 1)
4623 4624 4625 4626
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);

                if (mvsw) {
4627
                    for (i = 0; i < 2; i++) {
4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641
                        s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
                        s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
                        s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
                        s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
                    }
                } else {
                    vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
                    vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
                }

                vc1_mc_4mv_luma(v, 0, dir, 0);
                vc1_mc_4mv_luma(v, 1, dir, 0);
                vc1_mc_4mv_luma(v, 2, dir2, 0);
                vc1_mc_4mv_luma(v, 3, dir2, 0);
4642
                vc1_mc_4mv_chroma4(v, dir, dir2, 0);
4643 4644 4645 4646 4647
            } else {
                dir = bmvtype == BMV_TYPE_BACKWARD;

                mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
                dmv_x = dmv_y = 0;
4648
                if (mvbp)
4649 4650 4651 4652 4653 4654 4655 4656
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);

                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
                v->blk_mv_type[s->block_index[0]] = 1;
                v->blk_mv_type[s->block_index[1]] = 1;
                v->blk_mv_type[s->block_index[2]] = 1;
                v->blk_mv_type[s->block_index[3]] = 1;
                vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4657
                for (i = 0; i < 2; i++) {
4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712
                    s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
                    s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
                }
                vc1_mc_1mv(v, dir);
            }

            if (cbp)
                GET_MQUANT();  // p. 227
            s->current_picture.qscale_table[mb_pos] = mquant;
            if (!v->ttmbf && cbp)
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
            for (i = 0; i < 6; i++) {
                s->dc_val[0][s->block_index[i]] = 0;
                dst_idx += i >> 2;
                val = ((cbp >> (5 - i)) & 1);
                if (!fieldtx)
                    off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
                else
                    off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
                if (val) {
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
                                             first_block, s->dest[dst_idx] + off,
                                             (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
                                             (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
                    block_cbp |= pat << (i << 2);
                    if (!v->ttmbf && ttmb < 8)
                        ttmb = -1;
                    first_block = 0;
                }
            }

        } else { // skipped
            dir = 0;
            for (i = 0; i < 6; i++) {
                v->mb_type[0][s->block_index[i]] = 0;
                s->dc_val[0][s->block_index[i]] = 0;
            }
            s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
            s->current_picture.qscale_table[mb_pos] = 0;
            v->blk_mv_type[s->block_index[0]] = 0;
            v->blk_mv_type[s->block_index[1]] = 0;
            v->blk_mv_type[s->block_index[2]] = 0;
            v->blk_mv_type[s->block_index[3]] = 0;

            if (!direct) {
                if (bmvtype == BMV_TYPE_INTERPOLATED) {
                    vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
                    vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
                } else {
                    dir = bmvtype == BMV_TYPE_BACKWARD;
                    vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
                    if (mvsw) {
                        int dir2 = dir;
                        if (mvsw)
                            dir2 = !dir;
4713
                        for (i = 0; i < 2; i++) {
4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724
                            s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
                            s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
                            s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
                            s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
                        }
                    } else {
                        v->blk_mv_type[s->block_index[0]] = 1;
                        v->blk_mv_type[s->block_index[1]] = 1;
                        v->blk_mv_type[s->block_index[2]] = 1;
                        v->blk_mv_type[s->block_index[3]] = 1;
                        vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4725
                        for (i = 0; i < 2; i++) {
4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739
                            s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
                            s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
                        }
                    }
                }
            }

            vc1_mc_1mv(v, dir);
            if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
                vc1_interp_mc(v);
            }
        }
    }
    if (s->mb_x == s->mb_width - 1)
4740
        memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4741 4742 4743 4744 4745
    v->cbp[s->mb_x]      = block_cbp;
    v->ttblk[s->mb_x]    = block_tt;
    return 0;
}

4746 4747 4748 4749
/** Decode blocks of I-frame
 */
static void vc1_decode_i_blocks(VC1Context *v)
{
4750
    int k, j;
4751 4752 4753 4754 4755 4756
    MpegEncContext *s = &v->s;
    int cbp, val;
    uint8_t *coded_val;
    int mb_pos;

    /* select codingmode used for VLC tables selection */
4757
    switch (v->y_ac_table_index) {
4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768
    case 0:
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
        break;
    case 1:
        v->codingset = CS_HIGH_MOT_INTRA;
        break;
    case 2:
        v->codingset = CS_MID_RATE_INTRA;
        break;
    }

4769
    switch (v->c_ac_table_index) {
4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786
    case 0:
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
        break;
    case 1:
        v->codingset2 = CS_HIGH_MOT_INTER;
        break;
    case 2:
        v->codingset2 = CS_MID_RATE_INTER;
        break;
    }

    /* Set DC scale - y and c use the same */
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
    s->c_dc_scale = s->c_dc_scale_table[v->pq];

    //do frame decode
    s->mb_x = s->mb_y = 0;
4787
    s->mb_intra         = 1;
4788
    s->first_slice_line = 1;
Alberto Delmás's avatar
Alberto Delmás committed
4789
    for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4790
        s->mb_x = 0;
4791
        init_block_index(v);
Alberto Delmás's avatar
Alberto Delmás committed
4792
        for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4793
            uint8_t *dst[6];
4794
            ff_update_block_index(s);
4795 4796 4797 4798 4799 4800
            dst[0] = s->dest[0];
            dst[1] = dst[0] + 8;
            dst[2] = s->dest[0] + s->linesize * 8;
            dst[3] = dst[2] + 8;
            dst[4] = s->dest[1];
            dst[5] = s->dest[2];
4801 4802
            s->dsp.clear_blocks(s->block[0]);
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
4803 4804 4805 4806
            s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
            s->current_picture.qscale_table[mb_pos]                = v->pq;
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4807 4808 4809 4810 4811

            // do actual MB decoding and displaying
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
            v->s.ac_pred = get_bits1(&v->s.gb);

4812
            for (k = 0; k < 6; k++) {
4813 4814 4815
                val = ((cbp >> (5 - k)) & 1);

                if (k < 4) {
4816 4817
                    int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
                    val        = val ^ pred;
4818 4819 4820 4821
                    *coded_val = val;
                }
                cbp |= val << (5 - k);

4822
                vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4823

4824 4825
                if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
                    continue;
4826
                v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4827 4828 4829 4830
                if (v->pq >= 9 && v->overlap) {
                    if (v->rangeredfrm)
                        for (j = 0; j < 64; j++)
                            s->block[k][j] <<= 1;
4831 4832
                    s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
                } else {
4833 4834 4835
                    if (v->rangeredfrm)
                        for (j = 0; j < 64; j++)
                            s->block[k][j] = (s->block[k][j] - 64) << 1;
4836 4837
                    s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
                }
4838 4839
            }

4840 4841
            if (v->pq >= 9 && v->overlap) {
                if (s->mb_x) {
4842 4843
                    v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
                    v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4844
                    if (!(s->flags & CODEC_FLAG_GRAY)) {
4845 4846
                        v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
                        v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4847 4848
                    }
                }
4849 4850
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4851
                if (!s->first_slice_line) {
4852 4853
                    v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
                    v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4854
                    if (!(s->flags & CODEC_FLAG_GRAY)) {
4855 4856
                        v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
                        v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4857 4858
                    }
                }
4859 4860
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4861
            }
4862
            if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4863

4864
            if (get_bits_count(&s->gb) > v->bits) {
4865
                ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4866 4867
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
                       get_bits_count(&s->gb), v->bits);
4868 4869 4870
                return;
            }
        }
4871
        if (!v->s.loop_filter)
4872
            ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4873
        else if (s->mb_y)
4874
            ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4875

4876 4877
        s->first_slice_line = 0;
    }
4878
    if (v->s.loop_filter)
4879
        ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
Alberto Delmás's avatar
Alberto Delmás committed
4880 4881 4882

    /* This is intentionally mb_height and not end_mb_y - unlike in advanced
     * profile, these only differ are when decoding MSS2 rectangles. */
4883
    ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4884 4885 4886 4887
}

/** Decode blocks of I-frame for advanced profile
 */
4888
static void vc1_decode_i_blocks_adv(VC1Context *v)
4889
{
4890
    int k;
4891 4892 4893 4894 4895 4896 4897 4898 4899
    MpegEncContext *s = &v->s;
    int cbp, val;
    uint8_t *coded_val;
    int mb_pos;
    int mquant = v->pq;
    int mqdiff;
    GetBitContext *gb = &s->gb;

    /* select codingmode used for VLC tables selection */
4900
    switch (v->y_ac_table_index) {
4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911
    case 0:
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
        break;
    case 1:
        v->codingset = CS_HIGH_MOT_INTRA;
        break;
    case 2:
        v->codingset = CS_MID_RATE_INTRA;
        break;
    }

4912
    switch (v->c_ac_table_index) {
4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923
    case 0:
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
        break;
    case 1:
        v->codingset2 = CS_HIGH_MOT_INTER;
        break;
    case 2:
        v->codingset2 = CS_MID_RATE_INTER;
        break;
    }

4924 4925 4926
    // do frame decode
    s->mb_x             = s->mb_y = 0;
    s->mb_intra         = 1;
4927
    s->first_slice_line = 1;
4928
    s->mb_y             = s->start_mb_y;
4929
    if (s->start_mb_y) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
4930
        s->mb_x = 0;
4931
        init_block_index(v);
4932
        memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4933
               (1 + s->b8_stride) * sizeof(*s->coded_block));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
4934
    }
4935
    for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4936
        s->mb_x = 0;
4937
        init_block_index(v);
4938
        for (;s->mb_x < s->mb_width; s->mb_x++) {
Diego Biurrun's avatar
Diego Biurrun committed
4939
            int16_t (*block)[64] = v->block[v->cur_blk_idx];
4940
            ff_update_block_index(s);
4941
            s->dsp.clear_blocks(block[0]);
4942
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4943 4944 4945
            s->current_picture.mb_type[mb_pos + v->mb_off]                         = MB_TYPE_INTRA;
            s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
            s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4946 4947

            // do actual MB decoding and displaying
4948 4949
            if (v->fieldtx_is_raw)
                v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4950
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4951
            if ( v->acpred_is_raw)
4952 4953 4954 4955
                v->s.ac_pred = get_bits1(&v->s.gb);
            else
                v->s.ac_pred = v->acpred_plane[mb_pos];

4956 4957
            if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
                v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4958 4959 4960

            GET_MQUANT();

4961
            s->current_picture.qscale_table[mb_pos] = mquant;
4962 4963 4964 4965
            /* Set DC scale - y and c use the same */
            s->y_dc_scale = s->y_dc_scale_table[mquant];
            s->c_dc_scale = s->c_dc_scale_table[mquant];

4966
            for (k = 0; k < 6; k++) {
4967 4968 4969
                val = ((cbp >> (5 - k)) & 1);

                if (k < 4) {
4970 4971
                    int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
                    val        = val ^ pred;
4972 4973 4974 4975
                    *coded_val = val;
                }
                cbp |= val << (5 - k);

4976 4977
                v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
                v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4978

4979 4980
                vc1_decode_i_block_adv(v, block[k], k, val,
                                       (k < 4) ? v->codingset : v->codingset2, mquant);
4981

4982 4983
                if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
                    continue;
4984
                v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4985 4986
            }

4987 4988
            vc1_smooth_overlap_filter_iblk(v);
            vc1_put_signed_blocks_clamped(v);
4989
            if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4990

4991
            if (get_bits_count(&s->gb) > v->bits) {
4992
                // TODO: may need modification to handle slice coding
4993
                ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4994 4995
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
                       get_bits_count(&s->gb), v->bits);
4996 4997 4998
                return;
            }
        }
4999
        if (!v->s.loop_filter)
5000
            ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5001
        else if (s->mb_y)
5002
            ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
5003 5004
        s->first_slice_line = 0;
    }
5005 5006 5007

    /* raw bottom MB row */
    s->mb_x = 0;
5008 5009
    init_block_index(v);

5010
    for (;s->mb_x < s->mb_width; s->mb_x++) {
5011 5012
        ff_update_block_index(s);
        vc1_put_signed_blocks_clamped(v);
5013 5014
        if (v->s.loop_filter)
            vc1_loop_filter_iblk_delayed(v, v->pq);
5015
    }
5016
    if (v->s.loop_filter)
5017
        ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
5018
    ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5019
                    (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5020 5021
}

5022
static void vc1_decode_p_blocks(VC1Context *v)
5023 5024
{
    MpegEncContext *s = &v->s;
5025
    int apply_loop_filter;
5026 5027

    /* select codingmode used for VLC tables selection */
5028
    switch (v->c_ac_table_index) {
5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039
    case 0:
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
        break;
    case 1:
        v->codingset = CS_HIGH_MOT_INTRA;
        break;
    case 2:
        v->codingset = CS_MID_RATE_INTRA;
        break;
    }

5040
    switch (v->c_ac_table_index) {
5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051
    case 0:
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
        break;
    case 1:
        v->codingset2 = CS_HIGH_MOT_INTER;
        break;
    case 2:
        v->codingset2 = CS_MID_RATE_INTER;
        break;
    }

5052 5053
    apply_loop_filter   = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
                          v->fcm == PROGRESSIVE;
5054 5055
    s->first_slice_line = 1;
    memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5056
    for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5057
        s->mb_x = 0;
5058
        init_block_index(v);
5059
        for (; s->mb_x < s->mb_width; s->mb_x++) {
5060 5061
            ff_update_block_index(s);

5062
            if (v->fcm == ILACE_FIELD)
5063
                vc1_decode_p_mb_intfi(v);
5064
            else if (v->fcm == ILACE_FRAME)
5065 5066
                vc1_decode_p_mb_intfr(v);
            else vc1_decode_p_mb(v);
5067
            if (s->mb_y != s->start_mb_y && apply_loop_filter)
5068
                vc1_apply_p_loop_filter(v);
5069
            if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5070
                // TODO: may need modification to handle slice coding
5071
                ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5072 5073
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
                       get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5074 5075 5076
                return;
            }
        }
5077 5078 5079 5080
        memmove(v->cbp_base,      v->cbp,      sizeof(v->cbp_base[0])      * s->mb_stride);
        memmove(v->ttblk_base,    v->ttblk,    sizeof(v->ttblk_base[0])    * s->mb_stride);
        memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
        memmove(v->luma_mv_base,  v->luma_mv,  sizeof(v->luma_mv_base[0])  * s->mb_stride);
5081
        if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5082 5083
        s->first_slice_line = 0;
    }
5084 5085
    if (apply_loop_filter) {
        s->mb_x = 0;
5086
        init_block_index(v);
5087 5088 5089 5090 5091
        for (; s->mb_x < s->mb_width; s->mb_x++) {
            ff_update_block_index(s);
            vc1_apply_p_loop_filter(v);
        }
    }
5092
    if (s->end_mb_y >= s->start_mb_y)
5093
        ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5094
    ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5095
                    (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5096 5097
}

5098
static void vc1_decode_b_blocks(VC1Context *v)
5099 5100 5101 5102
{
    MpegEncContext *s = &v->s;

    /* select codingmode used for VLC tables selection */
5103
    switch (v->c_ac_table_index) {
5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114
    case 0:
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
        break;
    case 1:
        v->codingset = CS_HIGH_MOT_INTRA;
        break;
    case 2:
        v->codingset = CS_MID_RATE_INTRA;
        break;
    }

5115
    switch (v->c_ac_table_index) {
5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127
    case 0:
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
        break;
    case 1:
        v->codingset2 = CS_HIGH_MOT_INTER;
        break;
    case 2:
        v->codingset2 = CS_MID_RATE_INTER;
        break;
    }

    s->first_slice_line = 1;
5128
    for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5129
        s->mb_x = 0;
5130
        init_block_index(v);
5131
        for (; s->mb_x < s->mb_width; s->mb_x++) {
5132 5133
            ff_update_block_index(s);

5134
            if (v->fcm == ILACE_FIELD)
5135
                vc1_decode_b_mb_intfi(v);
5136 5137
            else if (v->fcm == ILACE_FRAME)
                vc1_decode_b_mb_intfr(v);
5138 5139
            else
                vc1_decode_b_mb(v);
5140
            if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5141
                // TODO: may need modification to handle slice coding
5142
                ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5143 5144
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
                       get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5145 5146
                return;
            }
5147
            if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5148
        }
5149
        if (!v->s.loop_filter)
5150
            ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5151
        else if (s->mb_y)
5152
            ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5153 5154
        s->first_slice_line = 0;
    }
5155
    if (v->s.loop_filter)
5156
        ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5157
    ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5158
                    (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5159 5160 5161 5162 5163 5164
}

static void vc1_decode_skip_blocks(VC1Context *v)
{
    MpegEncContext *s = &v->s;

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

5168
    ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5169
    s->first_slice_line = 1;
5170
    for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5171
        s->mb_x = 0;
5172
        init_block_index(v);
5173
        ff_update_block_index(s);
5174 5175 5176
        memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize,   s->linesize   * 16);
        memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
        memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
5177
        ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5178 5179
        s->first_slice_line = 0;
    }
5180
    s->pict_type = AV_PICTURE_TYPE_P;
5181 5182
}

5183
void ff_vc1_decode_blocks(VC1Context *v)
5184 5185 5186
{

    v->s.esc3_level_length = 0;
5187 5188 5189 5190 5191 5192 5193 5194
    if (v->x8_type) {
        ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
    } else {
        v->cur_blk_idx     =  0;
        v->left_blk_idx    = -1;
        v->topleft_blk_idx =  1;
        v->top_blk_idx     =  2;
        switch (v->s.pict_type) {
5195
        case AV_PICTURE_TYPE_I:
5196
            if (v->profile == PROFILE_ADVANCED)
5197
                vc1_decode_i_blocks_adv(v);
5198 5199 5200
            else
                vc1_decode_i_blocks(v);
            break;
5201
        case AV_PICTURE_TYPE_P:
5202
            if (v->p_frame_skipped)
5203 5204
                vc1_decode_skip_blocks(v);
            else
5205
                vc1_decode_p_blocks(v);
5206
            break;
5207
        case AV_PICTURE_TYPE_B:
5208 5209
            if (v->bi_type) {
                if (v->profile == PROFILE_ADVANCED)
5210
                    vc1_decode_i_blocks_adv(v);
5211 5212
                else
                    vc1_decode_i_blocks(v);
5213
            } else
5214
                vc1_decode_b_blocks(v);
5215 5216 5217 5218 5219
            break;
        }
    }
}

5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241
#if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER

typedef struct {
    /**
     * Transform coefficients for both sprites in 16.16 fixed point format,
     * in the order they appear in the bitstream:
     *  x scale
     *  rotation 1 (unused)
     *  x offset
     *  rotation 2 (unused)
     *  y scale
     *  y offset
     *  alpha
     */
    int coefs[2][7];

    int effect_type, effect_flag;
    int effect_pcount1, effect_pcount2;   ///< amount of effect parameters stored in effect_params
    int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
} SpriteData;

static inline int get_fp_val(GetBitContext* gb)
5242
{
5243
    return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5244 5245
}

5246
static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5247
{
5248
    c[1] = c[3] = 0;
5249 5250 5251

    switch (get_bits(gb, 2)) {
    case 0:
5252
        c[0] = 1 << 16;
5253
        c[2] = get_fp_val(gb);
5254
        c[4] = 1 << 16;
5255 5256
        break;
    case 1:
5257 5258
        c[0] = c[4] = get_fp_val(gb);
        c[2] = get_fp_val(gb);
5259 5260
        break;
    case 2:
5261 5262 5263
        c[0] = get_fp_val(gb);
        c[2] = get_fp_val(gb);
        c[4] = get_fp_val(gb);
5264 5265
        break;
    case 3:
5266 5267 5268 5269 5270
        c[0] = get_fp_val(gb);
        c[1] = get_fp_val(gb);
        c[2] = get_fp_val(gb);
        c[3] = get_fp_val(gb);
        c[4] = get_fp_val(gb);
5271 5272
        break;
    }
5273
    c[5] = get_fp_val(gb);
5274
    if (get_bits1(gb))
5275
        c[6] = get_fp_val(gb);
5276
    else
5277
        c[6] = 1 << 16;
5278 5279
}

5280
static int vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5281
{
5282 5283 5284 5285 5286 5287
    AVCodecContext *avctx = v->s.avctx;
    int sprite, i;

    for (sprite = 0; sprite <= v->two_sprites; sprite++) {
        vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
        if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5288
            avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5289
        av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5290
        for (i = 0; i < 7; i++)
5291 5292
            av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
                   sd->coefs[sprite][i] / (1<<16),
5293
                   (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5294
        av_log(avctx, AV_LOG_DEBUG, "\n");
5295
    }
5296

5297
    skip_bits(gb, 2);
5298 5299
    if (sd->effect_type = get_bits_long(gb, 30)) {
        switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5300
        case 7:
5301
            vc1_sprite_parse_transform(gb, sd->effect_params1);
5302 5303
            break;
        case 14:
5304 5305
            vc1_sprite_parse_transform(gb, sd->effect_params1);
            vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5306 5307
            break;
        default:
5308 5309
            for (i = 0; i < sd->effect_pcount1; i++)
                sd->effect_params1[i] = get_fp_val(gb);
5310
        }
5311
        if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5312
            // effect 13 is simple alpha blending and matches the opacity above
5313 5314 5315
            av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
            for (i = 0; i < sd->effect_pcount1; i++)
                av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5316 5317
                       sd->effect_params1[i] / (1 << 16),
                       (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5318
            av_log(avctx, AV_LOG_DEBUG, "\n");
5319 5320
        }

5321 5322 5323
        sd->effect_pcount2 = get_bits(gb, 16);
        if (sd->effect_pcount2 > 10) {
            av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5324
            return AVERROR_INVALIDDATA;
5325 5326 5327
        } else if (sd->effect_pcount2) {
            i = -1;
            av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5328
            while (++i < sd->effect_pcount2) {
5329 5330
                sd->effect_params2[i] = get_fp_val(gb);
                av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5331 5332
                       sd->effect_params2[i] / (1 << 16),
                       (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5333
            }
5334
            av_log(avctx, AV_LOG_DEBUG, "\n");
5335 5336
        }
    }
5337 5338
    if (sd->effect_flag = get_bits1(gb))
        av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5339 5340

    if (get_bits_count(gb) >= gb->size_in_bits +
5341
       (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0)) {
5342
        av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5343 5344
        return AVERROR_INVALIDDATA;
    }
5345
    if (get_bits_count(gb) < gb->size_in_bits - 8)
5346
        av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5347 5348

    return 0;
5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359
}

static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
{
    int i, plane, row, sprite;
    int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
    uint8_t* src_h[2][2];
    int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
    int ysub[2];
    MpegEncContext *s = &v->s;

5360
    for (i = 0; i <= v->two_sprites; i++) {
5361 5362
        xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
        xadv[i] = sd->coefs[i][0];
5363
        if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5364 5365 5366
            xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);

        yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5367
        yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5368 5369 5370 5371 5372 5373 5374
    }
    alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);

    for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
        int width = v->output_width>>!!plane;

        for (row = 0; row < v->output_height>>!!plane; row++) {
5375 5376
            uint8_t *dst = v->sprite_output_frame->data[plane] +
                           v->sprite_output_frame->linesize[plane] * row;
5377 5378

            for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5379 5380
                uint8_t *iplane = s->current_picture.f->data[plane];
                int      iline  = s->current_picture.f->linesize[plane];
5381 5382
                int      ycoord = yoff[sprite] + yadv[sprite] * row;
                int      yline  = ycoord >> 16;
5383
                int      next_line;
5384
                ysub[sprite] = ycoord & 0xFFFF;
5385
                if (sprite) {
5386 5387
                    iplane = s->last_picture.f->data[plane];
                    iline  = s->last_picture.f->linesize[plane];
5388
                }
5389
                next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5390 5391
                if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
                        src_h[sprite][0] = iplane + (xoff[sprite] >> 16) +  yline      * iline;
5392
                    if (ysub[sprite])
5393
                        src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5394 5395 5396 5397 5398 5399
                } else {
                    if (sr_cache[sprite][0] != yline) {
                        if (sr_cache[sprite][1] == yline) {
                            FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
                            FFSWAP(int,        sr_cache[sprite][0],   sr_cache[sprite][1]);
                        } else {
5400
                            v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5401 5402 5403 5404
                            sr_cache[sprite][0] = yline;
                        }
                    }
                    if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5405 5406 5407
                        v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
                                           iplane + next_line, xoff[sprite],
                                           xadv[sprite], width);
5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437
                        sr_cache[sprite][1] = yline + 1;
                    }
                    src_h[sprite][0] = v->sr_rows[sprite][0];
                    src_h[sprite][1] = v->sr_rows[sprite][1];
                }
            }

            if (!v->two_sprites) {
                if (ysub[0]) {
                    v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
                } else {
                    memcpy(dst, src_h[0][0], width);
                }
            } else {
                if (ysub[0] && ysub[1]) {
                    v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
                                                       src_h[1][0], src_h[1][1], ysub[1], alpha, width);
                } else if (ysub[0]) {
                    v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
                                                       src_h[1][0], alpha, width);
                } else if (ysub[1]) {
                    v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
                                                       src_h[0][0], (1<<16)-1-alpha, width);
                } else {
                    v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
                }
            }
        }

        if (!plane) {
5438
            for (i = 0; i <= v->two_sprites; i++) {
5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449
                xoff[i] >>= 1;
                yoff[i] >>= 1;
            }
        }

    }
}


static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
{
5450
    int ret;
5451
    MpegEncContext *s     = &v->s;
5452 5453 5454
    AVCodecContext *avctx = s->avctx;
    SpriteData sd;

5455 5456
    memset(&sd, 0, sizeof(sd));

5457 5458 5459
    ret = vc1_parse_sprites(v, gb, &sd);
    if (ret < 0)
        return ret;
5460

5461
    if (!s->current_picture.f->data[0]) {
5462 5463 5464 5465
        av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
        return -1;
    }

5466
    if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
5467 5468 5469 5470
        av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
        v->two_sprites = 0;
    }

5471
    av_frame_unref(v->sprite_output_frame);
5472
    if ((ret = ff_get_buffer(avctx, v->sprite_output_frame, 0)) < 0)
5473
        return ret;
5474 5475 5476 5477 5478 5479 5480 5481

    vc1_draw_sprites(v, &sd);

    return 0;
}

static void vc1_sprite_flush(AVCodecContext *avctx)
{
5482
    VC1Context *v     = avctx->priv_data;
5483
    MpegEncContext *s = &v->s;
5484
    AVFrame *f = s->current_picture.f;
5485 5486 5487 5488 5489 5490 5491 5492 5493
    int plane, i;

    /* Windows Media Image codecs have a convergence interval of two keyframes.
       Since we can't enforce it, clear to black the missing sprite. This is
       wrong but it looks better than doing nothing. */

    if (f->data[0])
        for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
            for (i = 0; i < v->sprite_height>>!!plane; i++)
5494
                memset(f->data[plane] + i * f->linesize[plane],
5495
                       plane ? 128 : 0, f->linesize[plane]);
5496 5497
}

5498 5499
#endif

5500
av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5501 5502 5503
{
    MpegEncContext *s = &v->s;
    int i;
5504
    int mb_height = FFALIGN(s->mb_height, 2);
5505 5506

    /* Allocate mb bitplanes */
5507 5508 5509 5510 5511 5512
    v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
    v->direct_mb_plane  = av_malloc (s->mb_stride * mb_height);
    v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
    v->fieldtx_plane    = av_mallocz(s->mb_stride * mb_height);
    v->acpred_plane     = av_malloc (s->mb_stride * mb_height);
    v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
5513 5514

    v->n_allocated_blks = s->mb_width + 2;
5515 5516 5517 5518 5519 5520 5521
    v->block            = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
    v->cbp_base         = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
    v->cbp              = v->cbp_base + s->mb_stride;
    v->ttblk_base       = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
    v->ttblk            = v->ttblk_base + s->mb_stride;
    v->is_intra_base    = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
    v->is_intra         = v->is_intra_base + s->mb_stride;
5522
    v->luma_mv_base     = av_mallocz(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5523
    v->luma_mv          = v->luma_mv_base + s->mb_stride;
5524 5525

    /* allocate block type info in that way so it could be used with s->block_index[] */
5526
    v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5527
    v->mb_type[0]   = v->mb_type_base + s->b8_stride + 1;
5528 5529
    v->mb_type[1]   = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
    v->mb_type[2]   = v->mb_type[1] + s->mb_stride * (mb_height + 1);
5530

5531
    /* allocate memory to store block level MV info */
5532
    v->blk_mv_type_base = av_mallocz(     s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5533
    v->blk_mv_type      = v->blk_mv_type_base + s->b8_stride + 1;
5534
    v->mv_f_base        = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5535
    v->mv_f[0]          = v->mv_f_base + s->b8_stride + 1;
5536 5537
    v->mv_f[1]          = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
    v->mv_f_next_base   = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5538
    v->mv_f_next[0]     = v->mv_f_next_base + s->b8_stride + 1;
5539
    v->mv_f_next[1]     = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5540

5541
    /* Init coded blocks info */
5542
    if (v->profile == PROFILE_ADVANCED) {
5543 5544 5545 5546 5547 5548 5549 5550
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
//            return -1;
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
//            return -1;
    }

    ff_intrax8_common_init(&v->x8,s);

5551
    if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5552
        for (i = 0; i < 4; i++)
5553 5554
            if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width)))
                return AVERROR(ENOMEM);
5555 5556 5557 5558
    }

    if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
        !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571
        !v->mb_type_base) {
        av_freep(&v->mv_type_mb_plane);
        av_freep(&v->direct_mb_plane);
        av_freep(&v->acpred_plane);
        av_freep(&v->over_flags_plane);
        av_freep(&v->block);
        av_freep(&v->cbp_base);
        av_freep(&v->ttblk_base);
        av_freep(&v->is_intra_base);
        av_freep(&v->luma_mv_base);
        av_freep(&v->mb_type_base);
        return AVERROR(ENOMEM);
    }
5572 5573 5574 5575

    return 0;
}

5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590
av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
{
    int i;
    for (i = 0; i < 64; i++) {
#define transpose(x) ((x >> 3) | ((x & 7) << 3))
        v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
        v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
        v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
        v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
        v->zzi_8x8[i]   = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
    }
    v->left_blk_sh = 0;
    v->top_blk_sh  = 3;
}

5591 5592 5593 5594 5595 5596 5597 5598 5599
/** Initialize a VC1/WMV3 decoder
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
 * @todo TODO: Decypher remaining bits in extra_data
 */
static av_cold int vc1_decode_init(AVCodecContext *avctx)
{
    VC1Context *v = avctx->priv_data;
    MpegEncContext *s = &v->s;
    GetBitContext gb;
5600
    int ret;
5601

5602 5603 5604 5605
    /* save the container output size for WMImage */
    v->output_width  = avctx->width;
    v->output_height = avctx->height;

5606 5607
    if (!avctx->extradata_size || !avctx->extradata)
        return -1;
5608 5609 5610
    if (!(avctx->flags & CODEC_FLAG_GRAY))
        avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
    else
5611
        avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5612
    avctx->hwaccel = ff_find_hwaccel(avctx);
5613 5614
    v->s.avctx = avctx;

5615 5616
    if ((ret = ff_vc1_init_common(v)) < 0)
        return ret;
5617
    // ensure static VLC tables are initialized
5618 5619 5620 5621
    if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
        return ret;
    if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
        return ret;
5622 5623 5624 5625
    // Hack to ensure the above functions will be called
    // again once we know all necessary settings.
    // That this is necessary might indicate a bug.
    ff_vc1_decode_end(avctx);
5626

5627
    ff_h264chroma_init(&v->h264chroma, 8);
5628

5629
    if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5630 5631 5632 5633 5634 5635 5636 5637 5638
        int count = 0;

        // looks like WMV3 has a sequence header stored in the extradata
        // advanced sequence header may be before the first frame
        // the last byte of the extradata is a version number, 1 for the
        // samples we can decode

        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);

5639 5640
        if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0)
          return ret;
5641 5642

        count = avctx->extradata_size*8 - get_bits_count(&gb);
5643
        if (count > 0) {
5644 5645
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
                   count, get_bits(&gb, count));
5646
        } else if (count < 0) {
5647 5648
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
        }
5649
    } else { // VC1/WVC1/WVP2
5650 5651 5652 5653 5654 5655 5656
        const uint8_t *start = avctx->extradata;
        uint8_t *end = avctx->extradata + avctx->extradata_size;
        const uint8_t *next;
        int size, buf2_size;
        uint8_t *buf2 = NULL;
        int seq_initialized = 0, ep_initialized = 0;

5657
        if (avctx->extradata_size < 16) {
5658 5659 5660 5661
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
            return -1;
        }

5662
        buf2  = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5663
        start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5664 5665
        next  = start;
        for (; next < end; start = next) {
5666 5667
            next = find_next_marker(start + 4, end);
            size = next - start - 4;
5668 5669
            if (size <= 0)
                continue;
5670 5671
            buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
            init_get_bits(&gb, buf2, buf2_size * 8);
5672
            switch (AV_RB32(start)) {
5673
            case VC1_CODE_SEQHDR:
5674
                if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) {
5675
                    av_free(buf2);
5676
                    return ret;
5677 5678 5679 5680
                }
                seq_initialized = 1;
                break;
            case VC1_CODE_ENTRYPOINT:
5681
                if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) {
5682
                    av_free(buf2);
5683
                    return ret;
5684 5685 5686 5687 5688 5689
                }
                ep_initialized = 1;
                break;
            }
        }
        av_free(buf2);
5690
        if (!seq_initialized || !ep_initialized) {
5691 5692 5693
            av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
            return -1;
        }
5694
        v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5695
    }
5696

5697 5698 5699 5700
    v->sprite_output_frame = av_frame_alloc();
    if (!v->sprite_output_frame)
        return AVERROR(ENOMEM);

5701 5702 5703 5704
    avctx->profile = v->profile;
    if (v->profile == PROFILE_ADVANCED)
        avctx->level = v->level;

5705
    avctx->has_b_frames = !!avctx->max_b_frames;
5706

5707 5708
    s->mb_width  = (avctx->coded_width  + 15) >> 4;
    s->mb_height = (avctx->coded_height + 15) >> 4;
5709

5710
    if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5711
        ff_vc1_init_transposed_scantables(v);
5712
    } else {
5713
        memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5714 5715 5716 5717
        v->left_blk_sh = 3;
        v->top_blk_sh  = 0;
    }

5718
    if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5719 5720 5721 5722 5723 5724 5725
        v->sprite_width  = avctx->coded_width;
        v->sprite_height = avctx->coded_height;

        avctx->coded_width  = avctx->width  = v->output_width;
        avctx->coded_height = avctx->height = v->output_height;

        // prevent 16.16 overflows
5726 5727 5728 5729
        if (v->sprite_width  > 1 << 14 ||
            v->sprite_height > 1 << 14 ||
            v->output_width  > 1 << 14 ||
            v->output_height > 1 << 14) return -1;
5730 5731

        if ((v->sprite_width&1) || (v->sprite_height&1)) {
5732
            avpriv_request_sample(avctx, "odd sprites support");
5733 5734
            return AVERROR_PATCHWELCOME;
        }
5735
    }
5736 5737 5738
    return 0;
}

5739 5740 5741
/** Close a VC1/WMV3 decoder
 * @warning Initial try at using MpegEncContext stuff
 */
5742
av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5743 5744 5745 5746
{
    VC1Context *v = avctx->priv_data;
    int i;

5747
    av_frame_free(&v->sprite_output_frame);
5748

5749
    for (i = 0; i < 4; i++)
5750
        av_freep(&v->sr_rows[i >> 1][i & 1]);
5751 5752
    av_freep(&v->hrd_rate);
    av_freep(&v->hrd_buffer);
5753
    ff_MPV_common_end(&v->s);
5754 5755
    av_freep(&v->mv_type_mb_plane);
    av_freep(&v->direct_mb_plane);
5756 5757
    av_freep(&v->forward_mb_plane);
    av_freep(&v->fieldtx_plane);
5758 5759 5760
    av_freep(&v->acpred_plane);
    av_freep(&v->over_flags_plane);
    av_freep(&v->mb_type_base);
5761 5762 5763
    av_freep(&v->blk_mv_type_base);
    av_freep(&v->mv_f_base);
    av_freep(&v->mv_f_next_base);
5764 5765 5766 5767 5768 5769 5770 5771 5772
    av_freep(&v->block);
    av_freep(&v->cbp_base);
    av_freep(&v->ttblk_base);
    av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
    av_freep(&v->luma_mv_base);
    ff_intrax8_common_end(&v->x8);
    return 0;
}

5773 5774 5775 5776

/** Decode a VC1/WMV3 frame
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
 */
5777
static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5778
                            int *got_frame, AVPacket *avpkt)
5779 5780
{
    const uint8_t *buf = avpkt->data;
5781
    int buf_size = avpkt->size, n_slices = 0, i, ret;
5782 5783 5784 5785
    VC1Context *v = avctx->priv_data;
    MpegEncContext *s = &v->s;
    AVFrame *pict = data;
    uint8_t *buf2 = NULL;
5786
    const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5787
    int mb_height, n_slices1=-1;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
5788 5789 5790 5791
    struct {
        uint8_t *buf;
        GetBitContext gb;
        int mby_start;
5792
    } *slices = NULL, *tmp;
5793

5794 5795
    v->second_field = 0;

Mean's avatar
Mean committed
5796 5797 5798
    if(s->flags & CODEC_FLAG_LOW_DELAY)
        s->low_delay = 1;

5799
    /* no supplementary picture */
5800
    if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5801
        /* special case for last picture */
5802
        if (s->low_delay == 0 && s->next_picture_ptr) {
5803
            if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
5804
                return ret;
5805
            s->next_picture_ptr = NULL;
5806

5807
            *got_frame = 1;
5808 5809
        }

5810
        return buf_size;
5811 5812
    }

5813 5814 5815 5816 5817 5818 5819
    if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
        if (v->profile < PROFILE_ADVANCED)
            avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
        else
            avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
    }

5820
    //for advanced profile we may need to parse and unescape data
5821
    if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5822 5823
        int buf_size2 = 0;
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5824 5825
        if (!buf2)
            return AVERROR(ENOMEM);
5826

5827
        if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5828 5829 5830 5831
            const uint8_t *start, *end, *next;
            int size;

            next = buf;
5832
            for (start = buf, end = buf + buf_size; next < end; start = next) {
5833 5834
                next = find_next_marker(start + 4, end);
                size = next - start - 4;
5835 5836
                if (size <= 0) continue;
                switch (AV_RB32(start)) {
5837
                case VC1_CODE_FRAME:
5838 5839
                    if (avctx->hwaccel ||
                        s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5840 5841 5842
                        buf_start = start;
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
                    break;
5843 5844
                case VC1_CODE_FIELD: {
                    int buf_size3;
5845 5846 5847
                    if (avctx->hwaccel ||
                        s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
                        buf_start_second_field = start;
5848
                    tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5849
                    if (!tmp)
5850
                        goto err;
5851
                    slices = tmp;
5852
                    slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5853 5854
                    if (!slices[n_slices].buf)
                        goto err;
5855 5856 5857 5858 5859 5860
                    buf_size3 = vc1_unescape_buffer(start + 4, size,
                                                    slices[n_slices].buf);
                    init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
                                  buf_size3 << 3);
                    /* assuming that the field marker is at the exact middle,
                       hope it's correct */
5861
                    slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5862 5863 5864 5865
                    n_slices1 = n_slices - 1; // index of the last slice of the first field
                    n_slices++;
                    break;
                }
5866 5867
                case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5868
                    init_get_bits(&s->gb, buf2, buf_size2 * 8);
5869
                    ff_vc1_decode_entry_point(avctx, v, &s->gb);
5870
                    break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
5871 5872
                case VC1_CODE_SLICE: {
                    int buf_size3;
5873
                    tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5874
                    if (!tmp)
5875
                        goto err;
5876
                    slices = tmp;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
5877
                    slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5878 5879
                    if (!slices[n_slices].buf)
                        goto err;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
5880 5881 5882 5883 5884 5885 5886 5887
                    buf_size3 = vc1_unescape_buffer(start + 4, size,
                                                    slices[n_slices].buf);
                    init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
                                  buf_size3 << 3);
                    slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
                    n_slices++;
                    break;
                }
5888 5889
                }
            }
5890
        } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5891
            const uint8_t *divider;
5892
            int buf_size3;
5893 5894

            divider = find_next_marker(buf, buf + buf_size);
5895
            if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5896
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5897
                goto err;
5898
            } else { // found field marker, unescape second field
5899 5900 5901
                if (avctx->hwaccel ||
                    s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
                    buf_start_second_field = divider;
5902
                tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5903 5904 5905 5906 5907 5908 5909 5910 5911
                if (!tmp)
                    goto err;
                slices = tmp;
                slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
                if (!slices[n_slices].buf)
                    goto err;
                buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
                init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
                              buf_size3 << 3);
5912
                slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5913 5914
                n_slices1 = n_slices - 1;
                n_slices++;
5915 5916
            }
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5917
        } else {
5918 5919 5920 5921 5922
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
        }
        init_get_bits(&s->gb, buf2, buf_size2*8);
    } else
        init_get_bits(&s->gb, buf, buf_size*8);
5923 5924

    if (v->res_sprite) {
5925 5926
        v->new_sprite  = !get_bits1(&s->gb);
        v->two_sprites =  get_bits1(&s->gb);
5927
        /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5928 5929 5930
           we're using the sprite compositor. These are intentionally kept separate
           so you can get the raw sprites by using the wmv3 decoder for WMVP or
           the vc1 one for WVP2 */
5931
        if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5932 5933 5934 5935 5936 5937 5938 5939
            if (v->new_sprite) {
                // switch AVCodecContext parameters to those of the sprites
                avctx->width  = avctx->coded_width  = v->sprite_width;
                avctx->height = avctx->coded_height = v->sprite_height;
            } else {
                goto image;
            }
        }
5940 5941
    }

5942 5943 5944
    if (s->context_initialized &&
        (s->width  != avctx->coded_width ||
         s->height != avctx->coded_height)) {
5945
        ff_vc1_decode_end(avctx);
5946 5947 5948
    }

    if (!s->context_initialized) {
5949
        if (ff_msmpeg4_decode_init(avctx) < 0)
5950
            goto err;
5951 5952 5953 5954
        if (ff_vc1_decode_init_alloc_tables(v) < 0) {
            ff_MPV_common_end(s);
            goto err;
        }
5955 5956 5957 5958

        s->low_delay = !avctx->has_b_frames || v->res_sprite;

        if (v->profile == PROFILE_ADVANCED) {
5959 5960
            if(avctx->coded_width<=1 || avctx->coded_height<=1)
                goto err;
5961 5962 5963 5964 5965
            s->h_edge_pos = avctx->coded_width;
            s->v_edge_pos = avctx->coded_height;
        }
    }

5966
    // do parse frame header
5967
    v->pic_header_flag = 0;
5968
    v->first_pic_header_flag = 1;
5969
    if (v->profile < PROFILE_ADVANCED) {
5970
        if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5971
            goto err;
5972 5973
        }
    } else {
5974
        if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5975
            goto err;
5976 5977
        }
    }
5978
    v->first_pic_header_flag = 0;
5979

5980 5981 5982
    if (avctx->debug & FF_DEBUG_PICT_INFO)
        av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));

5983
    if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5984
        && s->pict_type != AV_PICTURE_TYPE_I) {
5985 5986
        av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
        goto err;
5987 5988
    }

5989 5990 5991 5992 5993
    if ((s->mb_height >> v->field_mode) == 0) {
        av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
        goto err;
    }

5994
    // for skipping the frame
5995 5996
    s->current_picture.f->pict_type = s->pict_type;
    s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5997 5998

    /* skip B-frames if we don't have reference frames */
5999
    if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
Michael Niedermayer's avatar
Michael Niedermayer committed
6000
        av_log(v->s.avctx, AV_LOG_DEBUG, "Skipping B frame without reference frames\n");
6001
        goto end;
6002
    }
6003 6004 6005
    if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
        (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
         avctx->skip_frame >= AVDISCARD_ALL) {
6006
        goto end;
6007 6008
    }

6009 6010
    if (s->next_p_frame_damaged) {
        if (s->pict_type == AV_PICTURE_TYPE_B)
6011
            goto end;
6012
        else
6013
            s->next_p_frame_damaged = 0;
6014 6015
    }

6016
    if (ff_MPV_frame_start(s, avctx) < 0) {
6017
        goto err;
6018 6019
    }

6020
    v->s.current_picture_ptr->field_picture = v->field_mode;
6021 6022
    v->s.current_picture_ptr->f->interlaced_frame = (v->fcm != PROGRESSIVE);
    v->s.current_picture_ptr->f->top_field_first  = v->tff;
6023

6024
    // process pulldown flags
6025
    s->current_picture_ptr->f->repeat_pict = 0;
6026 6027 6028 6029
    // Pulldown flags are only valid when 'broadcast' has been set.
    // So ticks_per_frame will be 2
    if (v->rff) {
        // repeat field
6030
        s->current_picture_ptr->f->repeat_pict = 1;
6031 6032
    } else if (v->rptfrm) {
        // repeat frames
6033
        s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
6034 6035
    }

6036 6037
    s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
    s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
6038

6039 6040 6041 6042 6043 6044 6045 6046 6047
    if ((CONFIG_VC1_VDPAU_DECODER)
        &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
        if (v->field_mode && buf_start_second_field) {
            ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
            ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
        } else {
            ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
        }
    } else if (avctx->hwaccel) {
6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066
        if (v->field_mode && buf_start_second_field) {
            // decode first field
            s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
            if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
                goto err;
            if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
                goto err;
            if (avctx->hwaccel->end_frame(avctx) < 0)
                goto err;

            // decode second field
            s->gb = slices[n_slices1 + 1].gb;
            s->picture_structure = PICT_TOP_FIELD + v->tff;
            v->second_field = 1;
            v->pic_header_flag = 0;
            if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
                av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
                goto err;
            }
6067
            v->s.current_picture_ptr->f->pict_type = v->s.pict_type;
6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083

            if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
                goto err;
            if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
                goto err;
            if (avctx->hwaccel->end_frame(avctx) < 0)
                goto err;
        } else {
            s->picture_structure = PICT_FRAME;
            if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
                goto err;
            if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
                goto err;
            if (avctx->hwaccel->end_frame(avctx) < 0)
                goto err;
        }
6084
    } else {
6085 6086
        int header_ret = 0;

6087
        ff_mpeg_er_frame_start(s);
6088 6089

        v->bits = buf_size * 8;
Alberto Delmás's avatar
Alberto Delmás committed
6090
        v->end_mb_x = s->mb_width;
6091
        if (v->field_mode) {
6092 6093 6094
            s->current_picture.f->linesize[0] <<= 1;
            s->current_picture.f->linesize[1] <<= 1;
            s->current_picture.f->linesize[2] <<= 1;
6095 6096
            s->linesize                      <<= 1;
            s->uvlinesize                    <<= 1;
6097 6098
        }
        mb_height = s->mb_height >> v->field_mode;
6099

6100
        av_assert0 (mb_height > 0);
6101

Ronald S. Bultje's avatar
Ronald S. Bultje committed
6102
        for (i = 0; i <= n_slices; i++) {
6103
            if (i > 0 &&  slices[i - 1].mby_start >= mb_height) {
6104 6105 6106 6107
                if (v->field_mode <= 0) {
                    av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
                           "picture boundary (%d >= %d)\n", i,
                           slices[i - 1].mby_start, mb_height);
6108 6109
                    continue;
                }
6110
                v->second_field = 1;
6111
                av_assert0((s->mb_height & 1) == 0);
6112
                v->blocks_off   = s->b8_stride * (s->mb_height&~1);
6113 6114 6115 6116 6117 6118 6119 6120
                v->mb_off       = s->mb_stride * s->mb_height >> 1;
            } else {
                v->second_field = 0;
                v->blocks_off   = 0;
                v->mb_off       = 0;
            }
            if (i) {
                v->pic_header_flag = 0;
6121
                if (v->field_mode && i == n_slices1 + 2) {
6122
                    if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6123
                        av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
6124 6125
                        if (avctx->err_recognition & AV_EF_EXPLODE)
                            goto err;
6126 6127 6128
                        continue;
                    }
                } else if (get_bits1(&s->gb)) {
6129
                    v->pic_header_flag = 1;
6130
                    if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6131
                        av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
6132 6133
                        if (avctx->err_recognition & AV_EF_EXPLODE)
                            goto err;
6134 6135
                        continue;
                    }
6136 6137
                }
            }
6138 6139
            if (header_ret < 0)
                continue;
6140 6141 6142
            s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
            if (!v->field_mode || v->second_field)
                s->end_mb_y = (i == n_slices     ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6143 6144 6145 6146 6147
            else {
                if (i >= n_slices) {
                    av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
                    continue;
                }
6148
                s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6149
            }
6150 6151 6152 6153
            if (s->end_mb_y <= s->start_mb_y) {
                av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
                continue;
            }
6154 6155 6156 6157
            if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
                av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
                continue;
            }
6158
            ff_vc1_decode_blocks(v);
6159 6160 6161 6162 6163
            if (i != n_slices)
                s->gb = slices[i].gb;
        }
        if (v->field_mode) {
            v->second_field = 0;
6164 6165 6166
            s->current_picture.f->linesize[0] >>= 1;
            s->current_picture.f->linesize[1] >>= 1;
            s->current_picture.f->linesize[2] >>= 1;
6167 6168
            s->linesize                      >>= 1;
            s->uvlinesize                    >>= 1;
6169
            if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
6170 6171
                FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
                FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
6172
            }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
6173
        }
6174 6175
        av_dlog(s->avctx, "Consumed %i/%i bits\n",
                get_bits_count(&s->gb), s->gb.size_in_bits);
6176
//  if (get_bits_count(&s->gb) > buf_size * 8)
6177
//      return -1;
6178
        if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
6179
            goto err;
6180
        if (!v->field_mode)
6181
            ff_er_frame_end(&s->er);
6182 6183
    }

6184
    ff_MPV_frame_end(s);
6185

6186
    if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
6187 6188 6189
image:
        avctx->width  = avctx->coded_width  = v->output_width;
        avctx->height = avctx->coded_height = v->output_height;
6190 6191
        if (avctx->skip_frame >= AVDISCARD_NONREF)
            goto end;
6192
#if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
6193 6194
        if (vc1_decode_sprites(v, &s->gb))
            goto err;
6195
#endif
6196
        if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
6197
            goto err;
6198
        *got_frame = 1;
6199
    } else {
6200
        if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
6201
            if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
6202
                goto err;
6203
            ff_print_debug_info(s, s->current_picture_ptr, pict);
Anton Khirnov's avatar
Anton Khirnov committed
6204
            *got_frame = 1;
6205
        } else if (s->last_picture_ptr != NULL) {
6206
            if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
6207
                goto err;
6208
            ff_print_debug_info(s, s->last_picture_ptr, pict);
6209
            *got_frame = 1;
6210
        }
6211 6212
    }

6213
end:
6214
    av_free(buf2);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
6215 6216 6217
    for (i = 0; i < n_slices; i++)
        av_free(slices[i].buf);
    av_free(slices);
6218
    return buf_size;
6219 6220 6221

err:
    av_free(buf2);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
6222 6223 6224
    for (i = 0; i < n_slices; i++)
        av_free(slices[i].buf);
    av_free(slices);
6225
    return -1;
6226 6227 6228
}


6229 6230 6231 6232 6233 6234 6235
static const AVProfile profiles[] = {
    { FF_PROFILE_VC1_SIMPLE,   "Simple"   },
    { FF_PROFILE_VC1_MAIN,     "Main"     },
    { FF_PROFILE_VC1_COMPLEX,  "Complex"  },
    { FF_PROFILE_VC1_ADVANCED, "Advanced" },
    { FF_PROFILE_UNKNOWN },
};
6236

6237
static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
6238
#if CONFIG_VC1_DXVA2_HWACCEL
6239 6240
    AV_PIX_FMT_DXVA2_VLD,
#endif
6241
#if CONFIG_VC1_VAAPI_HWACCEL
6242 6243
    AV_PIX_FMT_VAAPI_VLD,
#endif
6244
#if CONFIG_VC1_VDPAU_HWACCEL
6245 6246 6247 6248 6249 6250
    AV_PIX_FMT_VDPAU,
#endif
    AV_PIX_FMT_YUV420P,
    AV_PIX_FMT_NONE
};

6251
AVCodec ff_vc1_decoder = {
6252
    .name           = "vc1",
6253
    .long_name      = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
6254
    .type           = AVMEDIA_TYPE_VIDEO,
6255
    .id             = AV_CODEC_ID_VC1,
6256 6257
    .priv_data_size = sizeof(VC1Context),
    .init           = vc1_decode_init,
6258
    .close          = ff_vc1_decode_end,
6259
    .decode         = vc1_decode_frame,
6260
    .flush          = ff_mpeg_flush,
6261
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6262
    .pix_fmts       = vc1_hwaccel_pixfmt_list_420,
6263
    .profiles       = NULL_IF_CONFIG_SMALL(profiles)
6264 6265
};

6266
#if CONFIG_WMV3_DECODER
6267
AVCodec ff_wmv3_decoder = {
6268
    .name           = "wmv3",
6269
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
6270
    .type           = AVMEDIA_TYPE_VIDEO,
6271
    .id             = AV_CODEC_ID_WMV3,
6272 6273
    .priv_data_size = sizeof(VC1Context),
    .init           = vc1_decode_init,
6274
    .close          = ff_vc1_decode_end,
6275
    .decode         = vc1_decode_frame,
6276
    .flush          = ff_mpeg_flush,
6277
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6278
    .pix_fmts       = vc1_hwaccel_pixfmt_list_420,
6279
    .profiles       = NULL_IF_CONFIG_SMALL(profiles)
6280
};
6281
#endif
6282

6283 6284 6285
#if CONFIG_WMV3_VDPAU_DECODER
AVCodec ff_wmv3_vdpau_decoder = {
    .name           = "wmv3_vdpau",
6286
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = AV_CODEC_ID_WMV3,
    .priv_data_size = sizeof(VC1Context),
    .init           = vc1_decode_init,
    .close          = ff_vc1_decode_end,
    .decode         = vc1_decode_frame,
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
    .profiles       = NULL_IF_CONFIG_SMALL(profiles)
};
#endif

#if CONFIG_VC1_VDPAU_DECODER
AVCodec ff_vc1_vdpau_decoder = {
    .name           = "vc1_vdpau",
6302
    .long_name      = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = AV_CODEC_ID_VC1,
    .priv_data_size = sizeof(VC1Context),
    .init           = vc1_decode_init,
    .close          = ff_vc1_decode_end,
    .decode         = vc1_decode_frame,
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
    .profiles       = NULL_IF_CONFIG_SMALL(profiles)
};
#endif

6315 6316 6317
#if CONFIG_WMV3IMAGE_DECODER
AVCodec ff_wmv3image_decoder = {
    .name           = "wmv3image",
6318
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6319
    .type           = AVMEDIA_TYPE_VIDEO,
6320
    .id             = AV_CODEC_ID_WMV3IMAGE,
6321 6322
    .priv_data_size = sizeof(VC1Context),
    .init           = vc1_decode_init,
6323
    .close          = ff_vc1_decode_end,
6324 6325 6326
    .decode         = vc1_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
    .flush          = vc1_sprite_flush,
6327 6328 6329 6330
    .pix_fmts       = (const enum AVPixelFormat[]) {
        AV_PIX_FMT_YUV420P,
        AV_PIX_FMT_NONE
    },
6331 6332 6333 6334 6335 6336
};
#endif

#if CONFIG_VC1IMAGE_DECODER
AVCodec ff_vc1image_decoder = {
    .name           = "vc1image",
6337
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6338
    .type           = AVMEDIA_TYPE_VIDEO,
6339
    .id             = AV_CODEC_ID_VC1IMAGE,
6340 6341
    .priv_data_size = sizeof(VC1Context),
    .init           = vc1_decode_init,
6342
    .close          = ff_vc1_decode_end,
6343 6344 6345
    .decode         = vc1_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
    .flush          = vc1_sprite_flush,
6346 6347 6348 6349
    .pix_fmts       = (const enum AVPixelFormat[]) {
        AV_PIX_FMT_YUV420P,
        AV_PIX_FMT_NONE
    },
6350 6351
};
#endif