vp9_mbgraph.c 13.7 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

Johann's avatar
Johann committed
13 14 15
#include "./vp9_rtcd.h"
#include "./vpx_dsp_rtcd.h"

16
#include "vpx_dsp/vpx_dsp_common.h"
17
#include "vpx_mem/vpx_mem.h"
18
#include "vpx_ports/system_state.h"
19 20 21 22 23 24
#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"

25 26
static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi, const MV *ref_mv,
                                              MV *dst_mv, int mb_row,
27
                                              int mb_col) {
28
  MACROBLOCK *const x = &cpi->td.mb;
John Koleszar's avatar
John Koleszar committed
29
  MACROBLOCKD *const xd = &x->e_mbd;
30 31
  MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
  const SEARCH_METHODS old_search_method = mv_sf->search_method;
32
  const vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
Alex Converse's avatar
Alex Converse committed
33
  const MvLimits tmp_mv_limits = x->mv_limits;
34
  MV ref_full;
35
  int cost_list[5];
John Koleszar's avatar
John Koleszar committed
36 37

  // Further step/diamond searches as necessary
38
  int step_param = mv_sf->reduce_first_step_size;
39
  step_param = VPXMIN(step_param, MAX_MVSEARCH_STEPS - 2);
John Koleszar's avatar
John Koleszar committed
40

Alex Converse's avatar
Alex Converse committed
41
  vp9_set_mv_search_range(&x->mv_limits, ref_mv);
John Koleszar's avatar
John Koleszar committed
42

43 44
  ref_full.col = ref_mv->col >> 3;
  ref_full.row = ref_mv->row >> 3;
John Koleszar's avatar
John Koleszar committed
45

46 47
  mv_sf->search_method = HEX;
  vp9_full_pixel_search(cpi, x, BLOCK_16X16, &ref_full, step_param,
48 49
                        cpi->sf.mv.search_method, x->errorperbit,
                        cond_cost_list(cpi, cost_list), ref_mv, dst_mv, 0, 0);
50
  mv_sf->search_method = old_search_method;
John Koleszar's avatar
John Koleszar committed
51

52 53 54
  /* restore UMV window */
  x->mv_limits = tmp_mv_limits;

John Koleszar's avatar
John Koleszar committed
55 56 57
  // Try sub-pixel MC
  // if (bestsme > error_thresh && bestsme < INT_MAX)
  {
58 59
    uint32_t distortion;
    uint32_t sse;
60 61
    cpi->find_fractional_mv_step(
        x, dst_mv, ref_mv, cpi->common.allow_high_precision_mv, x->errorperbit,
62
        &v_fn_ptr, 0, mv_sf->subpel_iters_per_step,
63 64
        cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL, 0,
        0);
John Koleszar's avatar
John Koleszar committed
65
  }
66

67 68
  xd->mi[0]->mode = NEWMV;
  xd->mi[0]->mv[0].as_mv = *dst_mv;
69

70
  vp9_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
71

Johann's avatar
Johann committed
72 73
  return vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
                      xd->plane[0].dst.buf, xd->plane[0].dst.stride);
74 75
}

76
static int do_16x16_motion_search(VP9_COMP *cpi, const MV *ref_mv,
77
                                  int_mv *dst_mv, int mb_row, int mb_col) {
78
  MACROBLOCK *const x = &cpi->td.mb;
John Koleszar's avatar
John Koleszar committed
79 80
  MACROBLOCKD *const xd = &x->e_mbd;
  unsigned int err, tmp_err;
81
  MV tmp_mv;
John Koleszar's avatar
John Koleszar committed
82 83 84

  // Try zero MV first
  // FIXME should really use something like near/nearest MV and/or MV prediction
Johann's avatar
Johann committed
85
  err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
86
                     xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
John Koleszar's avatar
John Koleszar committed
87 88 89 90
  dst_mv->as_int = 0;

  // Test last reference frame using the previous best mv as the
  // starting point (best reference) for the search
91
  tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv, mb_row, mb_col);
John Koleszar's avatar
John Koleszar committed
92
  if (tmp_err < err) {
93
    err = tmp_err;
94
    dst_mv->as_mv = tmp_mv;
John Koleszar's avatar
John Koleszar committed
95 96
  }

97 98
  // If the current best reference mv is not centered on 0,0 then do a 0,0
  // based search as well.
99
  if (ref_mv->row != 0 || ref_mv->col != 0) {
100
    unsigned int tmp_err;
101
    MV zero_ref_mv = { 0, 0 }, tmp_mv;
John Koleszar's avatar
John Koleszar committed
102

103 104
    tmp_err =
        do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv, mb_row, mb_col);
John Koleszar's avatar
John Koleszar committed
105
    if (tmp_err < err) {
106
      dst_mv->as_mv = tmp_mv;
John Koleszar's avatar
John Koleszar committed
107
      err = tmp_err;
108
    }
John Koleszar's avatar
John Koleszar committed
109
  }
110

John Koleszar's avatar
John Koleszar committed
111
  return err;
112 113
}

114
static int do_16x16_zerozero_search(VP9_COMP *cpi, int_mv *dst_mv) {
115
  MACROBLOCK *const x = &cpi->td.mb;
John Koleszar's avatar
John Koleszar committed
116 117 118 119 120
  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
Johann's avatar
Johann committed
121
  err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
122
                     xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
John Koleszar's avatar
John Koleszar committed
123 124 125 126

  dst_mv->as_int = 0;

  return err;
Paul Wilkins's avatar
Paul Wilkins committed
127
}
128
static int find_best_16x16_intra(VP9_COMP *cpi, PREDICTION_MODE *pbest_mode) {
129
  MACROBLOCK *const x = &cpi->td.mb;
John Koleszar's avatar
John Koleszar committed
130
  MACROBLOCKD *const xd = &x->e_mbd;
131
  PREDICTION_MODE best_mode = -1, mode;
132
  unsigned int best_err = INT_MAX;
John Koleszar's avatar
John Koleszar committed
133 134 135 136 137 138

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

139
    xd->mi[0]->mode = mode;
140 141 142
    vp9_predict_intra_block(xd, 2, TX_16X16, mode, x->plane[0].src.buf,
                            x->plane[0].src.stride, xd->plane[0].dst.buf,
                            xd->plane[0].dst.stride, 0, 0, 0);
Johann's avatar
Johann committed
143
    err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
144
                       xd->plane[0].dst.buf, xd->plane[0].dst.stride);
145

John Koleszar's avatar
John Koleszar committed
146 147
    // find best
    if (err < best_err) {
148
      best_err = err;
John Koleszar's avatar
John Koleszar committed
149
      best_mode = mode;
150
    }
John Koleszar's avatar
John Koleszar committed
151
  }
152

153
  if (pbest_mode) *pbest_mode = best_mode;
154

John Koleszar's avatar
John Koleszar committed
155
  return best_err;
156 157
}

158 159 160 161 162 163
static void update_mbgraph_mb_stats(VP9_COMP *cpi, MBGRAPH_MB_STATS *stats,
                                    YV12_BUFFER_CONFIG *buf, int mb_y_offset,
                                    YV12_BUFFER_CONFIG *golden_ref,
                                    const MV *prev_golden_ref_mv,
                                    YV12_BUFFER_CONFIG *alt_ref, int mb_row,
                                    int mb_col) {
164
  MACROBLOCK *const x = &cpi->td.mb;
John Koleszar's avatar
John Koleszar committed
165 166
  MACROBLOCKD *const xd = &x->e_mbd;
  int intra_error;
167
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
168 169

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

173 174
  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
175 176

  // do intra 16x16 prediction
177 178
  intra_error = find_best_16x16_intra(cpi, &stats->ref[INTRA_FRAME].m.mode);
  if (intra_error <= 0) intra_error = 1;
John Koleszar's avatar
John Koleszar committed
179 180 181 182
  stats->ref[INTRA_FRAME].err = intra_error;

  // Golden frame MV search, if it exists and is different than last frame
  if (golden_ref) {
183
    int g_motion_error;
184 185
    xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
    xd->plane[0].pre[0].stride = golden_ref->y_stride;
186 187 188
    g_motion_error =
        do_16x16_motion_search(cpi, prev_golden_ref_mv,
                               &stats->ref[GOLDEN_FRAME].m.mv, mb_row, mb_col);
John Koleszar's avatar
John Koleszar committed
189 190 191 192 193 194
    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;
  }

195 196
  // Do an Alt-ref frame MV search, if it exists and is different than
  // last/golden frame.
John Koleszar's avatar
John Koleszar committed
197
  if (alt_ref) {
198
    int a_motion_error;
199 200
    xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
    xd->plane[0].pre[0].stride = alt_ref->y_stride;
201 202
    a_motion_error =
        do_16x16_zerozero_search(cpi, &stats->ref[ALTREF_FRAME].m.mv);
John Koleszar's avatar
John Koleszar committed
203 204 205 206 207 208

    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;
  }
209 210
}

211 212 213 214 215
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) {
216
  MACROBLOCK *const x = &cpi->td.mb;
John Koleszar's avatar
John Koleszar committed
217
  MACROBLOCKD *const xd = &x->e_mbd;
218 219
  VP9_COMMON *const cm = &cpi->common;

John Koleszar's avatar
John Koleszar committed
220 221
  int mb_col, mb_row, offset = 0;
  int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
222
  MV gld_top_mv = { 0, 0 };
223
  MODE_INFO mi_local;
224
  MODE_INFO mi_above, mi_left;
225

226
  vp9_zero(mi_local);
227 228
  // Set up limit values for motion vectors to prevent them extending outside
  // the UMV borders.
Alex Converse's avatar
Alex Converse committed
229 230
  x->mv_limits.row_min = -BORDER_MV_PIXELS_B16;
  x->mv_limits.row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
231 232 233
  // Signal to vp9_predict_intra_block() that above is not available
  xd->above_mi = NULL;

234 235
  xd->plane[0].dst.stride = buf->y_stride;
  xd->plane[0].pre[0].stride = buf->y_stride;
236
  xd->plane[1].dst.stride = buf->uv_stride;
237
  xd->mi[0] = &mi_local;
238 239 240
  mi_local.sb_type = BLOCK_16X16;
  mi_local.ref_frame[0] = LAST_FRAME;
  mi_local.ref_frame[1] = NONE;
John Koleszar's avatar
John Koleszar committed
241 242

  for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
Yaowu Xu's avatar
Yaowu Xu committed
243
    MV gld_left_mv = gld_top_mv;
244
    int mb_y_in_offset = mb_y_offset;
John Koleszar's avatar
John Koleszar committed
245 246
    int arf_y_in_offset = arf_y_offset;
    int gld_y_in_offset = gld_y_offset;
247

248 249
    // Set up limit values for motion vectors to prevent them extending outside
    // the UMV borders.
Alex Converse's avatar
Alex Converse committed
250 251
    x->mv_limits.col_min = -BORDER_MV_PIXELS_B16;
    x->mv_limits.col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
252 253
    // Signal to vp9_predict_intra_block() that left is not available
    xd->left_mi = NULL;
John Koleszar's avatar
John Koleszar committed
254 255 256 257

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

258 259
      update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset, golden_ref,
                              &gld_left_mv, alt_ref, mb_row, mb_col);
260
      gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
John Koleszar's avatar
John Koleszar committed
261
      if (mb_col == 0) {
262
        gld_top_mv = gld_left_mv;
John Koleszar's avatar
John Koleszar committed
263
      }
264 265 266
      // Signal to vp9_predict_intra_block() that left is available
      xd->left_mi = &mi_left;

267 268 269
      mb_y_in_offset += 16;
      gld_y_in_offset += 16;
      arf_y_in_offset += 16;
Alex Converse's avatar
Alex Converse committed
270 271
      x->mv_limits.col_min -= 16;
      x->mv_limits.col_max -= 16;
272
    }
273 274 275 276

    // Signal to vp9_predict_intra_block() that above is available
    xd->above_mi = &mi_above;

277 278 279
    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;
Alex Converse's avatar
Alex Converse committed
280 281
    x->mv_limits.row_min -= 16;
    x->mv_limits.row_max -= 16;
282
    offset += cm->mb_cols;
John Koleszar's avatar
John Koleszar committed
283
  }
284 285
}

John Koleszar's avatar
John Koleszar committed
286
// void separate_arf_mbs_byzz
287 288
static void separate_arf_mbs(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
289
  int mb_col, mb_row, offset, i;
290
  int mi_row, mi_col;
291
  int ncnt[4] = { 0 };
John Koleszar's avatar
John Koleszar committed
292 293 294 295
  int n_frames = cpi->mbgraph_n_frames;

  int *arf_not_zz;

296 297 298
  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
299 300

  // We are not interested in results beyond the alt ref itself.
301 302
  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
303 304 305 306

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

    for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
John Koleszar's avatar
John Koleszar committed
309 310
         offset += cm->mb_cols, mb_row++) {
      for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
311
        MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
John Koleszar's avatar
John Koleszar committed
312 313

        int altref_err = mb_stats->ref[ALTREF_FRAME].err;
314
        int intra_err = mb_stats->ref[INTRA_FRAME].err;
John Koleszar's avatar
John Koleszar committed
315 316 317
        int golden_err = mb_stats->ref[GOLDEN_FRAME].err;

        // Test for altref vs intra and gf and that its mv was 0,0.
318
        if (altref_err > 1000 || altref_err > intra_err ||
319
            altref_err > golden_err) {
John Koleszar's avatar
John Koleszar committed
320
          arf_not_zz[offset + mb_col]++;
321
        }
John Koleszar's avatar
John Koleszar committed
322
      }
323
    }
John Koleszar's avatar
John Koleszar committed
324 325
  }

326 327 328 329
  // 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
330 331
      // If any of the blocks in the sequence failed then the MB
      // goes in segment 0
332
      if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
John Koleszar's avatar
John Koleszar committed
333
        ncnt[0]++;
334
        cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
335
      } else {
336
        cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
337
        ncnt[1]++;
John Koleszar's avatar
John Koleszar committed
338
      }
339
    }
John Koleszar's avatar
John Koleszar committed
340 341 342 343 344 345 346
  }

  // 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)
347
      cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
John Koleszar's avatar
John Koleszar committed
348 349

    // This error case should not be reachable as this function should
350
    // never be called with the common data structure uninitialized.
351
    else
John Koleszar's avatar
John Koleszar committed
352 353
      cpi->static_mb_pct = 0;

354
    vp9_enable_segmentation(&cm->seg);
John Koleszar's avatar
John Koleszar committed
355 356
  } else {
    cpi->static_mb_pct = 0;
357
    vp9_disable_segmentation(&cm->seg);
John Koleszar's avatar
John Koleszar committed
358
  }
359

John Koleszar's avatar
John Koleszar committed
360 361
  // Free localy allocated storage
  vpx_free(arf_not_zz);
362 363
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
364
void vp9_update_mbgraph_stats(VP9_COMP *cpi) {
365
  VP9_COMMON *const cm = &cpi->common;
366
  int i, n_frames = vp9_lookahead_depth(cpi->lookahead);
367
  YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
John Koleszar's avatar
John Koleszar committed
368

369 370
  assert(golden_ref != NULL);

John Koleszar's avatar
John Koleszar committed
371 372
  // we need to look ahead beyond where the ARF transitions into
  // being a GF - so exit if we don't look ahead beyond that
373
  if (n_frames <= cpi->rc.frames_till_gf_update_due) return;
374

375
  if (n_frames > MAX_LAG_BUFFERS) n_frames = MAX_LAG_BUFFERS;
John Koleszar's avatar
John Koleszar committed
376 377 378 379

  cpi->mbgraph_n_frames = n_frames;
  for (i = 0; i < n_frames; i++) {
    MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
James Zern's avatar
James Zern committed
380 381
    memset(frame_stats->mb_stats, 0,
           cm->mb_rows * cm->mb_cols * sizeof(*cpi->mbgraph_stats[i].mb_stats));
John Koleszar's avatar
John Koleszar committed
382 383 384 385 386 387 388 389
  }

  // 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];
390
    struct lookahead_entry *q_cur = vp9_lookahead_peek(cpi->lookahead, i);
John Koleszar's avatar
John Koleszar committed
391 392 393

    assert(q_cur != NULL);

394 395
    update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img, golden_ref,
                               cpi->Source);
John Koleszar's avatar
John Koleszar committed
396 397
  }

398
  vpx_clear_system_state();
John Koleszar's avatar
John Koleszar committed
399 400

  separate_arf_mbs(cpi);
401
}