mbgraph.c 18.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <limits.h>
#include <vp8/encoder/encodeintra.h>
#include <vp8/encoder/rdopt.h>
#include <vp8/common/setupintrarecon.h>
#include <vp8/common/blockd.h>
#include <vp8/common/reconinter.h>
#include <vp8/common/systemdependent.h>
#include <vpx_mem/vpx_mem.h>
#include <vp8/encoder/segmentation.h>

static unsigned int do_16x16_motion_iteration
(
    VP8_COMP *cpi,
    int_mv *ref_mv,
    int_mv *dst_mv
)
{
    MACROBLOCK  * const x  = &cpi->mb;
    MACROBLOCKD * const xd = &x->e_mbd;
    BLOCK *b  = &x->block[0];
    BLOCKD *d = &xd->block[0];
    vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
    unsigned int best_err;
    int step_param, further_steps;
    static int dummy_cost[2*mv_max+1];
    int *mvcost[2]    = { &dummy_cost[mv_max+1], &dummy_cost[mv_max+1] };
    int *mvsadcost[2] = { &dummy_cost[mv_max+1], &dummy_cost[mv_max+1] };
38 39
#if CONFIG_HIGH_PRECISION_MV
    static int dummy_cost_hp[2*mv_max_hp+1];
40 41
    int *mvcost_hp[2]    = { &dummy_cost_hp[mv_max_hp+1], &dummy_cost_hp[mv_max_hp+1] };
    int *mvsadcost_hp[2] = { &dummy_cost_hp[mv_max_hp+1], &dummy_cost_hp[mv_max_hp+1] };
42
#endif
43 44 45 46 47 48 49 50 51
    int col_min = (ref_mv->as_mv.col>>3) - MAX_FULL_PEL_VAL + ((ref_mv->as_mv.col & 7)?1:0);
    int row_min = (ref_mv->as_mv.row>>3) - MAX_FULL_PEL_VAL + ((ref_mv->as_mv.row & 7)?1:0);
    int col_max = (ref_mv->as_mv.col>>3) + MAX_FULL_PEL_VAL;
    int row_max = (ref_mv->as_mv.row>>3) + MAX_FULL_PEL_VAL;
    int tmp_col_min = x->mv_col_min;
    int tmp_col_max = x->mv_col_max;
    int tmp_row_min = x->mv_row_min;
    int tmp_row_max = x->mv_row_max;
    int_mv ref_full;
52 53 54 55 56 57 58 59 60 61 62 63 64

    // Further step/diamond searches as necessary
    if (cpi->Speed < 8)
    {
        step_param = cpi->sf.first_step + ((cpi->Speed > 5) ? 1 : 0);
        further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
    }
    else
    {
        step_param = cpi->sf.first_step + 2;
        further_steps = 0;
    }

65 66 67 68 69 70 71 72 73 74 75 76 77
    /* Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. */
    if (x->mv_col_min < col_min )
        x->mv_col_min = col_min;
    if (x->mv_col_max > col_max )
        x->mv_col_max = col_max;
    if (x->mv_row_min < row_min )
        x->mv_row_min = row_min;
    if (x->mv_row_max > row_max )
        x->mv_row_max = row_max;

    ref_full.as_mv.col = ref_mv->as_mv.col >> 3;
    ref_full.as_mv.row = ref_mv->as_mv.row >> 3;

78 79
    /*cpi->sf.search_method == HEX*/
    best_err = vp8_hex_search(x, b, d,
80
                             &ref_full, dst_mv,
81 82 83
                             step_param,
                             x->errorperbit,
                             &v_fn_ptr,
84 85 86 87 88 89
#if CONFIG_HIGH_PRECISION_MV
                             xd->allow_high_precision_mv?mvsadcost_hp:mvsadcost, xd->allow_high_precision_mv?mvcost_hp:mvcost,
#else
                             mvsadcost, mvcost,
#endif
                             ref_mv);
90 91 92 93 94 95 96 97 98

    // Try sub-pixel MC
    //if (bestsme > error_thresh && bestsme < INT_MAX)
    {
        int distortion;
        unsigned int sse;
        best_err = cpi->find_fractional_mv_step(x, b, d,
                                               dst_mv, ref_mv,
                                               x->errorperbit, &v_fn_ptr,
99 100 101 102 103 104
#if CONFIG_HIGH_PRECISION_MV
                                               xd->allow_high_precision_mv?mvcost_hp:mvcost,
#else
                                               mvcost,
#endif
                                               &distortion, &sse);
105 106
    }

107 108 109 110 111
#if CONFIG_PRED_FILTER
    // Disable the prediction filter
    xd->mode_info_context->mbmi.pred_filter_enabled = 0;
#endif

112 113
    vp8_set_mbmode_and_mvs(x, NEWMV, dst_mv);
    vp8_build_inter16x16_predictors_mby(xd);
Paul Wilkins's avatar
Paul Wilkins committed
114 115
    //VARIANCE_INVOKE(&cpi->rtcd.variance, satd16x16)
    best_err = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16)
116
                    (xd->dst.y_buffer, xd->dst.y_stride,
Yaowu Xu's avatar
Yaowu Xu committed
117
                     xd->predictor, 16, INT_MAX);
118

119 120 121 122 123 124
    /* restore UMV window */
    x->mv_col_min = tmp_col_min;
    x->mv_col_max = tmp_col_max;
    x->mv_row_min = tmp_row_min;
    x->mv_row_max = tmp_row_max;

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    return best_err;
}

static int do_16x16_motion_search
(
    VP8_COMP *cpi,
    int_mv *ref_mv,
    int_mv *dst_mv,
    YV12_BUFFER_CONFIG *buf,
    int buf_mb_y_offset,
    YV12_BUFFER_CONFIG *ref,
    int mb_y_offset
)
{
    MACROBLOCK  * const x  = &cpi->mb;
    MACROBLOCKD * const xd = &x->e_mbd;
    unsigned int err, tmp_err;
    int_mv tmp_mv;
    int n;

    for (n = 0; n < 16; n++) {
        BLOCKD *d = &xd->block[n];
        BLOCK *b  = &x->block[n];

        b->base_src   = &buf->y_buffer;
        b->src_stride = buf->y_stride;
        b->src        = buf->y_stride * (n & 12) + (n & 3) * 4 + buf_mb_y_offset;

        d->base_pre   = &ref->y_buffer;
        d->pre_stride = ref->y_stride;
        d->pre        = ref->y_stride * (n & 12) + (n & 3) * 4 + mb_y_offset;
    }

    // Try zero MV first
    // FIXME should really use something like near/nearest MV and/or MV prediction
    xd->pre.y_buffer = ref->y_buffer + mb_y_offset;
    xd->pre.y_stride = ref->y_stride;
Paul Wilkins's avatar
Paul Wilkins committed
162 163
    //VARIANCE_INVOKE(&cpi->rtcd.variance, satd16x16)
    err = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16)
164 165
                    (ref->y_buffer + mb_y_offset,
                     ref->y_stride, xd->dst.y_buffer,
Adrian Grange's avatar
Adrian Grange committed
166
                     xd->dst.y_stride, INT_MAX);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    dst_mv->as_int = 0;

    // Test last reference frame using the previous best mv as the
    // starting point (best reference) for the search
    tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv);
    if (tmp_err < err)
    {
        err            = tmp_err;
        dst_mv->as_int = tmp_mv.as_int;
    }

    // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
    if (ref_mv->as_int)
    {
        int tmp_err;
        int_mv zero_ref_mv, tmp_mv;

        zero_ref_mv.as_int = 0;
        tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv);
        if (tmp_err < err)
        {
            dst_mv->as_int = tmp_mv.as_int;
            err = tmp_err;
        }
    }

    return err;
}

Paul Wilkins's avatar
Paul Wilkins committed
196 197 198 199 200 201 202 203 204 205 206 207
static int do_16x16_zerozero_search
(
    VP8_COMP *cpi,
    int_mv *dst_mv,
    YV12_BUFFER_CONFIG *buf,
    int buf_mb_y_offset,
    YV12_BUFFER_CONFIG *ref,
    int mb_y_offset
)
{
    MACROBLOCK  * const x  = &cpi->mb;
    MACROBLOCKD * const xd = &x->e_mbd;
Christian Duvivier's avatar
Christian Duvivier committed
208
    unsigned int err;
Paul Wilkins's avatar
Paul Wilkins committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    int n;

    for (n = 0; n < 16; n++) {
        BLOCKD *d = &xd->block[n];
        BLOCK *b  = &x->block[n];

        b->base_src   = &buf->y_buffer;
        b->src_stride = buf->y_stride;
        b->src        = buf->y_stride * (n & 12) + (n & 3) * 4 + buf_mb_y_offset;

        d->base_pre   = &ref->y_buffer;
        d->pre_stride = ref->y_stride;
        d->pre        = ref->y_stride * (n & 12) + (n & 3) * 4 + mb_y_offset;
    }

    // Try zero MV first
    // FIXME should really use something like near/nearest MV and/or MV prediction
    xd->pre.y_buffer = ref->y_buffer + mb_y_offset;
    xd->pre.y_stride = ref->y_stride;
Paul Wilkins's avatar
Paul Wilkins committed
228 229
    //VARIANCE_INVOKE(&cpi->rtcd.variance, satd16x16)
    err = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16)
Paul Wilkins's avatar
Paul Wilkins committed
230 231
                    (ref->y_buffer + mb_y_offset,
                     ref->y_stride, xd->dst.y_buffer,
Adrian Grange's avatar
Adrian Grange committed
232
                     xd->dst.y_stride, INT_MAX);
Paul Wilkins's avatar
Paul Wilkins committed
233

Paul Wilkins's avatar
Paul Wilkins committed
234 235 236 237
    dst_mv->as_int = 0;

    return err;
}
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
static int find_best_16x16_intra
(
    VP8_COMP *cpi,
    YV12_BUFFER_CONFIG *buf,
    int mb_y_offset,
    MB_PREDICTION_MODE *pbest_mode
)
{
    MACROBLOCK  * const x  = &cpi->mb;
    MACROBLOCKD * const xd = &x->e_mbd;
    MB_PREDICTION_MODE best_mode = -1, mode;
    int best_err = INT_MAX;

    // calculate SATD for each intra prediction mode;
    // we're intentionally not doing 4x4, we just want a rough estimate
    for (mode = DC_PRED; mode <= TM_PRED; mode++)
    {
        unsigned int err;

        xd->mode_info_context->mbmi.mode = mode;
        RECON_INVOKE(&cpi->rtcd.common->recon, build_intra_predictors_mby)(xd);
Paul Wilkins's avatar
Paul Wilkins committed
259 260
        //VARIANCE_INVOKE(&cpi->rtcd.variance, satd16x16)
        err = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16)
261 262
                        (xd->predictor, 16,
                         buf->y_buffer + mb_y_offset,
Yaowu Xu's avatar
Yaowu Xu committed
263
                         buf->y_stride, best_err);
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
        // find best
        if (err < best_err)
        {
            best_err  = err;
            best_mode = mode;
        }
    }

    if (pbest_mode)
        *pbest_mode = best_mode;

    return best_err;
}

static void update_mbgraph_mb_stats
(
    VP8_COMP *cpi,
    MBGRAPH_MB_STATS *stats,
    YV12_BUFFER_CONFIG *buf,
    int mb_y_offset,
    YV12_BUFFER_CONFIG *golden_ref,
    int_mv *prev_golden_ref_mv,
    int gld_y_offset,
    YV12_BUFFER_CONFIG *alt_ref,
    int_mv *prev_alt_ref_mv,
    int arf_y_offset
)
{
    MACROBLOCK  * const x  = &cpi->mb;
    MACROBLOCKD * const xd = &x->e_mbd;
    int intra_error;

    // FIXME in practice we're completely ignoring chroma here
    xd->dst.y_buffer = buf->y_buffer + mb_y_offset;

    // do intra 16x16 prediction
    intra_error = find_best_16x16_intra(cpi, buf, mb_y_offset, &stats->ref[INTRA_FRAME].m.mode);
    if (intra_error <= 0)
        intra_error = 1;
    stats->ref[INTRA_FRAME].err = intra_error;

    // Golden frame MV search, if it exists and is different than last frame
    if (golden_ref)
    {
        int g_motion_error = do_16x16_motion_search(cpi, prev_golden_ref_mv,
                                                    &stats->ref[GOLDEN_FRAME].m.mv,
                                                    buf, mb_y_offset,
                                                    golden_ref, gld_y_offset);
        stats->ref[GOLDEN_FRAME].err = g_motion_error;
    }
    else
    {
        stats->ref[GOLDEN_FRAME].err = INT_MAX;
        stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
    }

    // Alt-ref frame MV search, if it exists and is different than last/golden frame
    if (alt_ref)
    {
Paul Wilkins's avatar
Paul Wilkins committed
323 324 325 326 327 328 329 330 331 332 333
        //int a_motion_error = do_16x16_motion_search(cpi, prev_alt_ref_mv,
        //                                            &stats->ref[ALTREF_FRAME].m.mv,
        //                                            buf, mb_y_offset,
        //                                            alt_ref, arf_y_offset);

        int a_motion_error =
                do_16x16_zerozero_search( cpi,
                                          &stats->ref[ALTREF_FRAME].m.mv,
                                          buf, mb_y_offset,
                                          alt_ref, arf_y_offset);

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
        stats->ref[ALTREF_FRAME].err = a_motion_error;
    }
    else
    {
        stats->ref[ALTREF_FRAME].err = INT_MAX;
        stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
    }
}

static void update_mbgraph_frame_stats
(
    VP8_COMP *cpi,
    MBGRAPH_FRAME_STATS *stats,
    YV12_BUFFER_CONFIG *buf,
    YV12_BUFFER_CONFIG *golden_ref,
    YV12_BUFFER_CONFIG *alt_ref
)
{
    MACROBLOCK  * const x  = &cpi->mb;
    VP8_COMMON  * const cm = &cpi->common;
    MACROBLOCKD * const xd = &x->e_mbd;
    int mb_col, mb_row, offset = 0;
    int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
    int_mv arf_top_mv, gld_top_mv;
358
    MODE_INFO mi_local;
359 360 361 362

    // Set up limit values for motion vectors to prevent them extending outside the UMV borders
    arf_top_mv.as_int = 0;
    gld_top_mv.as_int = 0;
363 364
    x->mv_row_min     = -(VP8BORDERINPIXELS - 16 - INTERP_EXTEND);
    x->mv_row_max     = (cm->mb_rows - 1) * 16 + VP8BORDERINPIXELS - 16 - INTERP_EXTEND;
365 366 367 368
    xd->up_available  = 0;
    xd->dst.y_stride  = buf->y_stride;
    xd->pre.y_stride  = buf->y_stride;
    xd->dst.uv_stride = buf->uv_stride;
369
    xd->mode_info_context = &mi_local;
370 371 372 373 374 375 376 377 378 379 380

    for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
    {
        int_mv arf_left_mv, gld_left_mv;
        int mb_y_in_offset  = mb_y_offset;
        int arf_y_in_offset = arf_y_offset;
        int gld_y_in_offset = gld_y_offset;

        // Set up limit values for motion vectors to prevent them extending outside the UMV borders
        arf_left_mv.as_int = arf_top_mv.as_int;
        gld_left_mv.as_int = gld_top_mv.as_int;
381 382
        x->mv_col_min      = -(VP8BORDERINPIXELS - 16 - INTERP_EXTEND);
        x->mv_col_max      = (cm->mb_cols - 1) * 16 + VP8BORDERINPIXELS - 16 - INTERP_EXTEND;
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        xd->left_available = 0;

        for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
        {
            MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];

            update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset,
                                    golden_ref, &gld_left_mv, gld_y_in_offset,
                                    alt_ref,    &arf_left_mv, arf_y_in_offset);
            arf_left_mv.as_int = mb_stats->ref[ALTREF_FRAME].m.mv.as_int;
            gld_left_mv.as_int = mb_stats->ref[GOLDEN_FRAME].m.mv.as_int;
            if (mb_col == 0)
            {
                arf_top_mv.as_int = arf_left_mv.as_int;
                gld_top_mv.as_int = gld_left_mv.as_int;
            }
            xd->left_available = 1;
            mb_y_in_offset    += 16;
            gld_y_in_offset   += 16;
            arf_y_in_offset   += 16;
            x->mv_col_min     -= 16;
            x->mv_col_max     -= 16;
        }
        xd->up_available = 1;
        mb_y_offset     += buf->y_stride * 16;
        gld_y_offset    += golden_ref->y_stride * 16;
        if (alt_ref)
            arf_y_offset    += alt_ref->y_stride * 16;
        x->mv_row_min   -= 16;
        x->mv_row_max   -= 16;
        offset          += cm->mb_cols;
    }
}

Paul Wilkins's avatar
Paul Wilkins committed
417 418 419 420 421 422 423 424 425
// Test for small magnitude (<= 1 pel mvs)
int small_mv( MV mv )
{
    if ( (abs( (int)mv.col ) > 2) || (abs( (int)mv.row ) > 2) )
        return FALSE;
    else
        return TRUE;
}

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
//void separate_arf_mbs_byzz
void separate_arf_mbs
(
    VP8_COMP *cpi
)
{
    VP8_COMMON * const cm = &cpi->common;
    int mb_col, mb_row, offset, i;
    int ncnt[4];
    int n_frames = cpi->mbgraph_n_frames;

    int * arf_not_zz;

    CHECK_MEM_ERROR(arf_not_zz,
        vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz), 1));

    vpx_memset(arf_not_zz, 0, sizeof(arf_not_zz));

    // We are not interested in results beyond the alt ref itself.
    if ( n_frames > cpi->frames_till_gf_update_due )
        n_frames = cpi->frames_till_gf_update_due;

    // defer cost to reference frames
    for (i = n_frames - 1; i >= 0; i--)
    {
        MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];

        for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
             offset += cm->mb_cols, mb_row++)
        {
            for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
            {
                MBGRAPH_MB_STATS *mb_stats =
                    &frame_stats->mb_stats[offset + mb_col];

                int altref_err = mb_stats->ref[ALTREF_FRAME].err;
Yaowu Xu's avatar
Yaowu Xu committed
462 463
                int intra_err  = mb_stats->ref[INTRA_FRAME ].err;
                int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
464 465

                // Test for altref vs intra and gf and that its mv was 0,0.
Paul Wilkins's avatar
Paul Wilkins committed
466 467 468
                if ( (altref_err > 1000) ||
                     (altref_err > intra_err) ||
                     (altref_err > golden_err) )
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
                {
                    arf_not_zz[offset + mb_col]++;
                }
            }
        }
    }

    vpx_memset(ncnt, 0, sizeof(ncnt));
    for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
         offset += cm->mb_cols, mb_row++)
    {
        for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
        {
            // If any of the blocks in the sequence failed then the MB
            // goes in segment 0
            if ( arf_not_zz[offset + mb_col] )
            {
                ncnt[0]++;
                cpi->segmentation_map[offset + mb_col] = 0;
            }
            else
            {
                ncnt[1]++;
                cpi->segmentation_map[offset + mb_col] = 1;
            }
        }
    }

    // Only bother with segmentation if over 10% of the MBs in static segment
Paul Wilkins's avatar
Paul Wilkins committed
498 499
    //if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
    if ( 1 )
500
    {
501
        // Note % of blocks that are marked as static
Paul Wilkins's avatar
Paul Wilkins committed
502 503 504 505 506 507 508
        if ( cm->MBs )
            cpi->static_mb_pct = (ncnt[1] * 100) / cm->MBs;

        // This error case should not be reachable as this function should
        // never be called with the common data structure unititialized.
        else
            cpi->static_mb_pct = 0;
509

510
        cpi->seg0_cnt = ncnt[0];
511 512 513 514
        vp8_enable_segmentation((VP8_PTR) cpi);
    }
    else
    {
515
        cpi->static_mb_pct = 0;
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
        vp8_disable_segmentation((VP8_PTR) cpi);
    }

    // Free localy allocated storage
    vpx_free(arf_not_zz);
}

void vp8_update_mbgraph_stats
(
    VP8_COMP *cpi
)
{
    VP8_COMMON * const cm = &cpi->common;
    int i, n_frames = vp8_lookahead_depth(cpi->lookahead);
    YV12_BUFFER_CONFIG *golden_ref = &cm->yv12_fb[cm->gld_fb_idx];

    // we need to look ahead beyond where the ARF transitions into
    // being a GF - so exit if we don't look ahead beyond that
    if (n_frames <= cpi->frames_till_gf_update_due)
        return;
Yaowu Xu's avatar
Yaowu Xu committed
536 537
    if( n_frames > cpi->common.frames_till_alt_ref_frame)
        n_frames = cpi->common.frames_till_alt_ref_frame;
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    if (n_frames > MAX_LAG_BUFFERS)
        n_frames = MAX_LAG_BUFFERS;

    cpi->mbgraph_n_frames = n_frames;
    for (i = 0; i < n_frames; i++)
    {
        MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
        vpx_memset(frame_stats->mb_stats, 0,
                   cm->mb_rows * cm->mb_cols * sizeof(*cpi->mbgraph_stats[i].mb_stats));
    }

    // do motion search to find contribution of each reference to data
    // later on in this GF group
    // FIXME really, the GF/last MC search should be done forward, and
    // the ARF MC search backwards, to get optimal results for MV caching
    for (i = 0; i < n_frames; i++)
    {
        MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
        struct lookahead_entry *q_cur =
                vp8_lookahead_peek(cpi->lookahead, i);

        assert(q_cur != NULL);

        update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img,
                                   golden_ref, cpi->Source);
    }

    vp8_clear_system_state();  //__asm emms;

    separate_arf_mbs(cpi);
}