vp9_mbgraph.c 14.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 *  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>
12

13 14 15 16 17 18 19 20 21 22
#include "vpx_mem/vpx_mem.h"
#include "vp9/encoder/vp9_rdopt.h"
#include "vp9/encoder/vp9_segmentation.h"
#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/common/vp9_blockd.h"
#include "vp9/common/vp9_reconinter.h"
#include "vp9/common/vp9_reconintra.h"
#include "vp9/common/vp9_systemdependent.h"


23

24 25
static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi,
                                              int_mv *ref_mv,
26 27 28
                                              int_mv *dst_mv,
                                              int mb_row,
                                              int mb_col) {
John Koleszar's avatar
John Koleszar committed
29 30
  MACROBLOCK   *const x  = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
31
  vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
John Koleszar's avatar
John Koleszar committed
32
  unsigned int best_err;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
33

34 35 36 37
  const int tmp_col_min = x->mv_col_min;
  const int tmp_col_max = x->mv_col_max;
  const int tmp_row_min = x->mv_row_min;
  const int tmp_row_max = x->mv_row_max;
John Koleszar's avatar
John Koleszar committed
38 39 40
  int_mv ref_full;

  // Further step/diamond searches as necessary
41
  int step_param = cpi->sf.reduce_first_step_size +
42
      (cpi->speed < 8 ? (cpi->speed > 5 ? 1 : 0) : 2);
43
  step_param = MIN(step_param, (cpi->sf.max_step_search_steps - 2));
John Koleszar's avatar
John Koleszar committed
44

45
  vp9_set_mv_search_range(x, &ref_mv->as_mv);
John Koleszar's avatar
John Koleszar committed
46 47 48 49 50

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

  /*cpi->sf.search_method == HEX*/
51
  best_err = vp9_hex_search(x, &ref_full.as_mv, step_param, x->errorperbit,
52
                            0, &v_fn_ptr,
53
                            0, &ref_mv->as_mv, &dst_mv->as_mv);
John Koleszar's avatar
John Koleszar committed
54 55 56 57 58 59

  // Try sub-pixel MC
  // if (bestsme > error_thresh && bestsme < INT_MAX)
  {
    int distortion;
    unsigned int sse;
60
    best_err = cpi->find_fractional_mv_step(
John Koleszar's avatar
John Koleszar committed
61
        x,
62
        &dst_mv->as_mv, &ref_mv->as_mv,
63
        cpi->common.allow_high_precision_mv,
64
        x->errorperbit, &v_fn_ptr,
65
        0, cpi->sf.subpel_iters_per_step, NULL, NULL,
66
        & distortion, &sse);
John Koleszar's avatar
John Koleszar committed
67
  }
68

69
  vp9_set_mbmode_and_mvs(x, NEWMV, dst_mv);
70
  vp9_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
John Koleszar's avatar
John Koleszar committed
71
  best_err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
72 73
                          xd->plane[0].dst.buf, xd->plane[0].dst.stride,
                          INT_MAX);
74

John Koleszar's avatar
John Koleszar committed
75 76 77 78 79
  /* 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;
80

John Koleszar's avatar
John Koleszar committed
81
  return best_err;
82 83
}

84
static int do_16x16_motion_search(VP9_COMP *cpi, int_mv *ref_mv, int_mv *dst_mv,
85 86
                                  int mb_row, int mb_col) {
  MACROBLOCK *const x = &cpi->mb;
John Koleszar's avatar
John Koleszar committed
87 88 89 90 91 92
  MACROBLOCKD *const xd = &x->e_mbd;
  unsigned int err, tmp_err;
  int_mv tmp_mv;

  // Try zero MV first
  // FIXME should really use something like near/nearest MV and/or MV prediction
John Koleszar's avatar
John Koleszar committed
93
  err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
94 95
                     xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
                     INT_MAX);
John Koleszar's avatar
John Koleszar committed
96 97 98 99
  dst_mv->as_int = 0;

  // Test last reference frame using the previous best mv as the
  // starting point (best reference) for the search
100
  tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv, mb_row, mb_col);
John Koleszar's avatar
John Koleszar committed
101
  if (tmp_err < err) {
102
    err = tmp_err;
John Koleszar's avatar
John Koleszar committed
103 104 105
    dst_mv->as_int = tmp_mv.as_int;
  }

Jim Bankoski's avatar
Jim Bankoski committed
106 107
  // If the current best reference mv is not centered on 0,0 then do a 0,0
  // based search as well.
John Koleszar's avatar
John Koleszar committed
108
  if (ref_mv->as_int) {
109
    unsigned int tmp_err;
John Koleszar's avatar
John Koleszar committed
110 111 112
    int_mv zero_ref_mv, tmp_mv;

    zero_ref_mv.as_int = 0;
113 114
    tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv,
                                        mb_row, mb_col);
John Koleszar's avatar
John Koleszar committed
115 116 117
    if (tmp_err < err) {
      dst_mv->as_int = tmp_mv.as_int;
      err = tmp_err;
118
    }
John Koleszar's avatar
John Koleszar committed
119
  }
120

John Koleszar's avatar
John Koleszar committed
121
  return err;
122 123
}

124
static int do_16x16_zerozero_search(VP9_COMP *cpi, int_mv *dst_mv) {
125
  MACROBLOCK *const x = &cpi->mb;
John Koleszar's avatar
John Koleszar committed
126 127 128 129 130
  MACROBLOCKD *const xd = &x->e_mbd;
  unsigned int err;

  // Try zero MV first
  // FIXME should really use something like near/nearest MV and/or MV prediction
John Koleszar's avatar
John Koleszar committed
131
  err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
132 133
                     xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
                     INT_MAX);
John Koleszar's avatar
John Koleszar committed
134 135 136 137

  dst_mv->as_int = 0;

  return err;
Paul Wilkins's avatar
Paul Wilkins committed
138
}
139 140 141
static int find_best_16x16_intra(VP9_COMP *cpi,
                                 int mb_y_offset,
                                 MB_PREDICTION_MODE *pbest_mode) {
John Koleszar's avatar
John Koleszar committed
142 143 144
  MACROBLOCK   *const x  = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_PREDICTION_MODE best_mode = -1, mode;
145
  unsigned int best_err = INT_MAX;
John Koleszar's avatar
John Koleszar committed
146 147 148 149 150 151

  // 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;

152
    xd->mi_8x8[0]->mbmi.mode = mode;
153 154 155
    vp9_predict_intra_block(xd, 0, 2, TX_16X16, mode,
                            x->plane[0].src.buf, x->plane[0].src.stride,
                            xd->plane[0].dst.buf, xd->plane[0].dst.stride);
John Koleszar's avatar
John Koleszar committed
156
    err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
157
                       xd->plane[0].dst.buf, xd->plane[0].dst.stride, best_err);
158

John Koleszar's avatar
John Koleszar committed
159 160 161 162
    // find best
    if (err < best_err) {
      best_err  = err;
      best_mode = mode;
163
    }
John Koleszar's avatar
John Koleszar committed
164
  }
165

John Koleszar's avatar
John Koleszar committed
166 167
  if (pbest_mode)
    *pbest_mode = best_mode;
168

John Koleszar's avatar
John Koleszar committed
169
  return best_err;
170 171 172 173
}

static void update_mbgraph_mb_stats
(
174
  VP9_COMP *cpi,
John Koleszar's avatar
John Koleszar committed
175 176 177 178 179 180 181 182
  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,
183 184 185
  int arf_y_offset,
  int mb_row,
  int mb_col
John Koleszar's avatar
John Koleszar committed
186
) {
187
  MACROBLOCK *const x = &cpi->mb;
John Koleszar's avatar
John Koleszar committed
188 189
  MACROBLOCKD *const xd = &x->e_mbd;
  int intra_error;
190
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
191 192

  // FIXME in practice we're completely ignoring chroma here
John Koleszar's avatar
John Koleszar committed
193 194
  x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
  x->plane[0].src.stride = buf->y_stride;
195

196 197
  xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
  xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
John Koleszar's avatar
John Koleszar committed
198 199

  // do intra 16x16 prediction
200
  intra_error = find_best_16x16_intra(cpi, mb_y_offset,
201
                                      &stats->ref[INTRA_FRAME].m.mode);
John Koleszar's avatar
John Koleszar committed
202 203 204 205 206 207
  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) {
208
    int g_motion_error;
209 210
    xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
    xd->plane[0].pre[0].stride = golden_ref->y_stride;
211 212
    g_motion_error = do_16x16_motion_search(cpi,
                                            prev_golden_ref_mv,
213 214
                                            &stats->ref[GOLDEN_FRAME].m.mv,
                                            mb_row, mb_col);
John Koleszar's avatar
John Koleszar committed
215 216 217 218 219 220
    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;
  }

Jim Bankoski's avatar
Jim Bankoski committed
221 222
  // Do an Alt-ref frame MV search, if it exists and is different than
  // last/golden frame.
John Koleszar's avatar
John Koleszar committed
223
  if (alt_ref) {
224
    int a_motion_error;
225 226
    xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
    xd->plane[0].pre[0].stride = alt_ref->y_stride;
227
    a_motion_error = do_16x16_zerozero_search(cpi,
228
                                              &stats->ref[ALTREF_FRAME].m.mv);
John Koleszar's avatar
John Koleszar committed
229 230 231 232 233 234

    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;
  }
235 236
}

237 238 239 240 241 242
static void update_mbgraph_frame_stats(VP9_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;
John Koleszar's avatar
John Koleszar committed
243
  MACROBLOCKD *const xd = &x->e_mbd;
244 245
  VP9_COMMON *const cm = &cpi->common;

John Koleszar's avatar
John Koleszar committed
246 247 248
  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;
249
  MODE_INFO mi_local = { { 0 } };
250

Jim Bankoski's avatar
Jim Bankoski committed
251 252
  // Set up limit values for motion vectors to prevent them extending outside
  // the UMV borders.
John Koleszar's avatar
John Koleszar committed
253 254
  arf_top_mv.as_int = 0;
  gld_top_mv.as_int = 0;
255 256
  x->mv_row_min     = -BORDER_MV_PIXELS_B16;
  x->mv_row_max     = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
John Koleszar's avatar
John Koleszar committed
257
  xd->up_available  = 0;
258
  xd->plane[0].dst.stride  = buf->y_stride;
259
  xd->plane[0].pre[0].stride  = buf->y_stride;
260
  xd->plane[1].dst.stride = buf->uv_stride;
261
  xd->mi_8x8[0] = &mi_local;
262
  mi_local.mbmi.sb_type = BLOCK_16X16;
263 264
  mi_local.mbmi.ref_frame[0] = LAST_FRAME;
  mi_local.mbmi.ref_frame[1] = NONE;
John Koleszar's avatar
John Koleszar committed
265 266 267 268 269 270

  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;
271

Jim Bankoski's avatar
Jim Bankoski committed
272 273
    // Set up limit values for motion vectors to prevent them extending outside
    // the UMV borders.
John Koleszar's avatar
John Koleszar committed
274 275
    arf_left_mv.as_int = arf_top_mv.as_int;
    gld_left_mv.as_int = gld_top_mv.as_int;
276 277
    x->mv_col_min      = -BORDER_MV_PIXELS_B16;
    x->mv_col_max      = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
John Koleszar's avatar
John Koleszar committed
278 279 280 281 282 283 284
    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,
285 286
                              alt_ref,    &arf_left_mv, arf_y_in_offset,
                              mb_row, mb_col);
John Koleszar's avatar
John Koleszar committed
287 288 289 290 291 292 293 294 295 296 297 298
      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;
299
    }
John Koleszar's avatar
John Koleszar committed
300 301 302 303 304 305 306 307 308
    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;
  }
309 310
}

John Koleszar's avatar
John Koleszar committed
311
// void separate_arf_mbs_byzz
312 313
static void separate_arf_mbs(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
314
  int mb_col, mb_row, offset, i;
315
  int mi_row, mi_col;
316
  int ncnt[4] = { 0 };
John Koleszar's avatar
John Koleszar committed
317 318 319 320
  int n_frames = cpi->mbgraph_n_frames;

  int *arf_not_zz;

321 322 323
  CHECK_MEM_ERROR(cm, arf_not_zz,
                  vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz),
                             1));
John Koleszar's avatar
John Koleszar committed
324 325

  // We are not interested in results beyond the alt ref itself.
326 327
  if (n_frames > cpi->rc.frames_till_gf_update_due)
    n_frames = cpi->rc.frames_till_gf_update_due;
John Koleszar's avatar
John Koleszar committed
328 329 330 331

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

    for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
John Koleszar's avatar
John Koleszar committed
334 335
         offset += cm->mb_cols, mb_row++) {
      for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
336
        MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
John Koleszar's avatar
John Koleszar committed
337 338 339 340 341 342

        int altref_err = mb_stats->ref[ALTREF_FRAME].err;
        int intra_err  = mb_stats->ref[INTRA_FRAME ].err;
        int golden_err = mb_stats->ref[GOLDEN_FRAME].err;

        // Test for altref vs intra and gf and that its mv was 0,0.
343 344 345
        if (altref_err > 1000 ||
            altref_err > intra_err ||
            altref_err > golden_err) {
John Koleszar's avatar
John Koleszar committed
346
          arf_not_zz[offset + mb_col]++;
347
        }
John Koleszar's avatar
John Koleszar committed
348
      }
349
    }
John Koleszar's avatar
John Koleszar committed
350 351
  }

352 353 354 355
  // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
  // of bound access in segmentation_map
  for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
    for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
John Koleszar's avatar
John Koleszar committed
356 357
      // If any of the blocks in the sequence failed then the MB
      // goes in segment 0
358
      if (arf_not_zz[mi_row/2*cm->mb_cols + mi_col/2]) {
John Koleszar's avatar
John Koleszar committed
359
        ncnt[0]++;
360
        cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
361
      } else {
362
        cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
363
        ncnt[1]++;
John Koleszar's avatar
John Koleszar committed
364
      }
365
    }
John Koleszar's avatar
John Koleszar committed
366 367 368 369 370 371 372
  }

  // Only bother with segmentation if over 10% of the MBs in static segment
  // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
  if (1) {
    // Note % of blocks that are marked as static
    if (cm->MBs)
373
      cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
John Koleszar's avatar
John Koleszar committed
374 375

    // This error case should not be reachable as this function should
376
    // never be called with the common data structure uninitialized.
377
    else
John Koleszar's avatar
John Koleszar committed
378 379 380
      cpi->static_mb_pct = 0;

    cpi->seg0_cnt = ncnt[0];
381
    vp9_enable_segmentation((VP9_PTR)cpi);
John Koleszar's avatar
John Koleszar committed
382 383
  } else {
    cpi->static_mb_pct = 0;
384
    vp9_disable_segmentation((VP9_PTR)cpi);
John Koleszar's avatar
John Koleszar committed
385
  }
386

John Koleszar's avatar
John Koleszar committed
387 388
  // Free localy allocated storage
  vpx_free(arf_not_zz);
389 390
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
391
void vp9_update_mbgraph_stats(VP9_COMP *cpi) {
392
  VP9_COMMON *const cm = &cpi->common;
393
  int i, n_frames = vp9_lookahead_depth(cpi->lookahead);
394
  YV12_BUFFER_CONFIG *golden_ref =
395
      &cm->yv12_fb[cm->ref_frame_map[cpi->gld_fb_idx]];
John Koleszar's avatar
John Koleszar committed
396 397 398

  // we need to look ahead beyond where the ARF transitions into
  // being a GF - so exit if we don't look ahead beyond that
399
  if (n_frames <= cpi->rc.frames_till_gf_update_due)
John Koleszar's avatar
John Koleszar committed
400
    return;
401 402
  if (n_frames > (int)cpi->frames_till_alt_ref_frame)
    n_frames = cpi->frames_till_alt_ref_frame;
John Koleszar's avatar
John Koleszar committed
403 404 405 406 407 408 409
  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,
Jim Bankoski's avatar
Jim Bankoski committed
410 411
               cm->mb_rows * cm->mb_cols *
               sizeof(*cpi->mbgraph_stats[i].mb_stats));
John Koleszar's avatar
John Koleszar committed
412 413 414 415 416 417 418 419
  }

  // 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];
420
    struct lookahead_entry *q_cur = vp9_lookahead_peek(cpi->lookahead, i);
John Koleszar's avatar
John Koleszar committed
421 422 423 424 425 426 427

    assert(q_cur != NULL);

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

428
  vp9_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
429 430

  separate_arf_mbs(cpi);
431
}