/* * Copyright (c) 2014 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.
*/
staticint mv_refs_rt(VP9_COMP *cpi, const VP9_COMMON *cm, const MACROBLOCK *x, const MACROBLOCKD *xd, const TileInfo *const tile,
MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
int_mv *mv_ref_list, int_mv *base_mv, int mi_row, int mi_col, int use_base_mv) { constint *ref_sign_bias = cm->ref_frame_sign_bias; int i, refmv_count = 0;
const POSITION *const mv_ref_search = mv_ref_blocks[mi->sb_type];
int different_ref_found = 0; int context_counter = 0; int const_motion = 0;
// Blank the reference vector list
memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
// The nearest 2 blocks are treated differently // if the size < 8x8 we get the mv from the bmi substructure, // and we also need to keep a mode count. for (i = 0; i < 2; ++i) { const POSITION *const mv_ref = &mv_ref_search[i]; if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) { const MODE_INFO *const candidate_mi =
xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride]; // Keep counts for entropy encoding.
context_counter += mode_2_counter[candidate_mi->mode];
different_ref_found = 1;
// Check the rest of the neighbors in much the same way // as before except we don't need to keep track of sub blocks or // mode counts. for (; i < MVREF_NEIGHBOURS && !refmv_count; ++i) { const POSITION *const mv_ref = &mv_ref_search[i]; if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) { const MODE_INFO *const candidate_mi =
xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
different_ref_found = 1;
if (candidate_mi->ref_frame[0] == ref_frame)
ADD_MV_REF_LIST(candidate_mi->mv[0], refmv_count, mv_ref_list, Done);
}
}
// Since we couldn't find 2 mvs from the same reference frame // go back through the neighbors and find motion vectors from // different reference frames. if (different_ref_found && !refmv_count) { for (i = 0; i < MVREF_NEIGHBOURS; ++i) { const POSITION *mv_ref = &mv_ref_search[i]; if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) { const MODE_INFO *const candidate_mi =
xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
// If the candidate is INTRA we don't want to consider its mv.
IF_DIFF_REF_FRAME_ADD_MV(candidate_mi, ref_frame, ref_sign_bias,
refmv_count, mv_ref_list, Done);
}
}
} if (use_base_mv &&
!cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame &&
ref_frame == LAST_FRAME) { // Get base layer mv. constint prev_layer = cpi->svc.spatial_layer_id - 1; constint index =
(mi_col >> 1) + (mi_row >> 1) * cpi->svc.mi_cols[prev_layer]; // prev_frame->mvs[] is allocated to size mi_cols * mi_rows corresponding // to the previous spatial layer, so the index check is against // svc.mi_col/rows[prev_layer]. if (index < cpi->svc.mi_cols[prev_layer] * cpi->svc.mi_rows[prev_layer]) {
MV_REF *candidate = &cm->prev_frame->mvs[index]; // Avoid using base_mv if scaled mv is out of range, for either component. if (candidate->mv[0].as_int != INVALID_MV &&
abs(candidate->mv[0].as_mv.row) <= INT16_MAX >> 1 &&
abs(candidate->mv[0].as_mv.col) <= INT16_MAX >> 1) {
base_mv->as_mv.row = candidate->mv[0].as_mv.row * 2;
base_mv->as_mv.col = candidate->mv[0].as_mv.col * 2;
clamp_mv_ref(&base_mv->as_mv, xd);
} else {
base_mv->as_int = INVALID_MV;
}
}
}
// Clamp vectors for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
return const_motion;
}
staticint combined_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
BLOCK_SIZE bsize, int mi_row, int mi_col,
int_mv *tmp_mv, int *rate_mv,
int64_t best_rd_sofar, int use_base_mv) {
MACROBLOCKD *xd = &x->e_mbd;
MODE_INFO *mi = xd->mi[0]; struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0 } }; constint step_param = cpi->sf.mv.fullpel_search_step_param; constint sadpb = x->sadperbit16;
MV mvp_full; constint ref = mi->ref_frame[0]; const MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv;
MV center_mv;
uint32_t dis; int rate_mode; const MvLimits tmp_mv_limits = x->mv_limits; int rv = 0; int cost_list[5]; int search_subpel = 1; const YV12_BUFFER_CONFIG *scaled_ref_frame =
vp9_get_scaled_ref_frame(cpi, ref); if (scaled_ref_frame) { int i; // Swap out the reference frame for a version that's been scaled to // match the resolution of the current frame, allowing the existing // motion search code to be used without additional modifications. for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
}
vp9_set_mv_search_range(&x->mv_limits, &ref_mv);
if (scaled_ref_frame) { int i; for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
} return rv;
}
staticvoid block_variance(const uint8_t *src, int src_stride, const uint8_t *ref, int ref_stride, int w, int h, unsignedint *sse, int *sum, int block_size, #if CONFIG_VP9_HIGHBITDEPTH int use_highbitdepth, vpx_bit_depth_t bd, #endif
uint32_t *sse8x8, int *sum8x8, uint32_t *var8x8) { int i, j, k = 0;
uint32_t k_sqr = 0;
*sse = 0;
*sum = 0;
for (i = 0; i < h; i += block_size) { for (j = 0; j < w; j += block_size) { #if CONFIG_VP9_HIGHBITDEPTH if (use_highbitdepth) { switch (bd) { case VPX_BITS_8:
vpx_highbd_8_get8x8var(src + src_stride * i + j, src_stride,
ref + ref_stride * i + j, ref_stride,
&sse8x8[k], &sum8x8[k]); break; case VPX_BITS_10:
vpx_highbd_10_get8x8var(src + src_stride * i + j, src_stride,
ref + ref_stride * i + j, ref_stride,
&sse8x8[k], &sum8x8[k]); break; case VPX_BITS_12:
vpx_highbd_12_get8x8var(src + src_stride * i + j, src_stride,
ref + ref_stride * i + j, ref_stride,
&sse8x8[k], &sum8x8[k]); break;
}
} else {
vpx_get8x8var(src + src_stride * i + j, src_stride,
ref + ref_stride * i + j, ref_stride, &sse8x8[k],
&sum8x8[k]);
} #else
vpx_get8x8var(src + src_stride * i + j, src_stride,
ref + ref_stride * i + j, ref_stride, &sse8x8[k],
&sum8x8[k]); #endif
*sse += sse8x8[k];
*sum += sum8x8[k];
k_sqr = (uint32_t)(((int64_t)sum8x8[k] * sum8x8[k]) >> 6);
var8x8[k] = sse8x8[k] > k_sqr ? sse8x8[k] - k_sqr : k_sqr - sse8x8[k];
k++;
}
}
}
staticvoid calculate_variance(int bw, int bh, TX_SIZE tx_size, unsignedint *sse_i, int *sum_i, unsignedint *var_o, unsignedint *sse_o, int *sum_o) { const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size]; constint nw = 1 << (bw - b_width_log2_lookup[unit_size]); constint nh = 1 << (bh - b_height_log2_lookup[unit_size]); int i, j, k = 0;
uint32_t k_sqr = 0;
// Adjust the ac_thr according to speed, width, height and normalized sum staticint ac_thr_factor(constint speed, constint width, constint height, constint norm_sum) { if (speed >= 8 && norm_sum < 5) { if (width <= 640 && height <= 480) return 4; else return 2;
} return 1;
}
static TX_SIZE calculate_tx_size(VP9_COMP *const cpi, BLOCK_SIZE bsize,
MACROBLOCKD *const xd, unsignedint var, unsignedint sse, int64_t ac_thr, unsignedint source_variance, int is_intra) { // TODO(marpan): Tune selection for intra-modes, screen content, etc.
TX_SIZE tx_size; unsignedint var_thresh = is_intra ? (unsignedint)ac_thr : 1; int limit_tx = 1; if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
(source_variance == 0 || var < var_thresh))
limit_tx = 0; if (cpi->common.tx_mode == TX_MODE_SELECT) { if (sse > (var << 2))
tx_size = VPXMIN(max_txsize_lookup[bsize],
tx_mode_to_biggest_tx_size[cpi->common.tx_mode]); else
tx_size = TX_8X8; if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && limit_tx &&
cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id))
tx_size = TX_8X8; elseif (tx_size > TX_16X16 && limit_tx)
tx_size = TX_16X16; // For screen-content force 4X4 tx_size over 8X8, for large variance. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && tx_size == TX_8X8 &&
bsize <= BLOCK_16X16 && ((var >> 5) > (unsignedint)ac_thr))
tx_size = TX_4X4;
} else {
tx_size = VPXMIN(max_txsize_lookup[bsize],
tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
} return tx_size;
}
staticvoid compute_intra_yprediction(PREDICTION_MODE mode, BLOCK_SIZE bsize,
MACROBLOCK *x, MACROBLOCKD *xd) { struct macroblockd_plane *const pd = &xd->plane[0]; struct macroblock_plane *const p = &x->plane[0];
uint8_t *const src_buf_base = p->src.buf;
uint8_t *const dst_buf_base = pd->dst.buf; constint src_stride = p->src.stride; constint dst_stride = pd->dst.stride; // block and transform sizes, in number of 4x4 blocks log 2 ("*_b") // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8 const TX_SIZE tx_size = max_txsize_lookup[bsize]; constint num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; constint num_4x4_h = num_4x4_blocks_high_lookup[bsize]; int row, col; // If mb_to_right_edge is < 0 we are in a situation in which // the current block size extends into the UMV and we won't // visit the sub blocks that are wholly within the UMV. constint max_blocks_wide =
num_4x4_w + (xd->mb_to_right_edge >= 0
? 0
: xd->mb_to_right_edge >> (5 + pd->subsampling_x)); constint max_blocks_high =
num_4x4_h + (xd->mb_to_bottom_edge >= 0
? 0
: xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
// Keep track of the row and column of the blocks we use so that we know // if we are in the unrestricted motion border. for (row = 0; row < max_blocks_high; row += (1 << tx_size)) { // Skip visiting the sub blocks that are wholly within the UMV. for (col = 0; col < max_blocks_wide; col += (1 << tx_size)) {
p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)];
pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)];
vp9_predict_intra_block(xd, b_width_log2_lookup[bsize], tx_size, mode,
x->skip_encode ? p->src.buf : pd->dst.buf,
x->skip_encode ? src_stride : dst_stride,
pd->dst.buf, dst_stride, col, row, 0);
}
}
p->src.buf = src_buf_base;
pd->dst.buf = dst_buf_base;
}
staticvoid model_rd_for_sb_y_large(VP9_COMP *cpi, BLOCK_SIZE bsize,
MACROBLOCK *x, MACROBLOCKD *xd, int *out_rate_sum, int64_t *out_dist_sum, unsignedint *var_y, unsignedint *sse_y, int mi_row, int mi_col, int *early_term, int *flag_preduv_computed) { // Note our transform coeffs are 8 times an orthogonal transform. // Hence quantizer step is also 8 times. To get effective quantizer // we need to divide by 8 before sending to modeling function. unsignedint sse; int rate;
int64_t dist; struct macroblock_plane *const p = &x->plane[0]; struct macroblockd_plane *const pd = &xd->plane[0]; const uint32_t dc_quant = pd->dequant[0]; const uint32_t ac_quant = pd->dequant[1];
int64_t dc_thr = dc_quant * dc_quant >> 6;
int64_t ac_thr = ac_quant * ac_quant >> 6; unsignedint var; int sum; int skip_dc = 0;
constint bw = b_width_log2_lookup[bsize]; constint bh = b_height_log2_lookup[bsize]; constint num8x8 = 1 << (bw + bh - 2); unsignedint sse8x8[64] = { 0 }; int sum8x8[64] = { 0 }; unsignedint var8x8[64] = { 0 };
TX_SIZE tx_size; int i, k;
uint32_t sum_sqr; #if CONFIG_VP9_HIGHBITDEPTH const vpx_bit_depth_t bd = cpi->common.bit_depth; #endif // Calculate variance for whole partition, and also save 8x8 blocks' variance // to be used in following transform skipping test.
block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
4 << bw, 4 << bh, &sse, &sum, 8, #if CONFIG_VP9_HIGHBITDEPTH
cpi->common.use_highbitdepth, bd, #endif
sse8x8, sum8x8, var8x8);
sum_sqr = (uint32_t)((int64_t)sum * sum) >> (bw + bh + 4);
var = sse > sum_sqr ? sse - sum_sqr : sum_sqr - sse;
tx_size = calculate_tx_size(cpi, bsize, xd, var, sse, ac_thr,
x->source_variance, 0); // The code below for setting skip flag assumes tranform size of at least 8x8, // so force this lower limit on transform. if (tx_size < TX_8X8) tx_size = TX_8X8;
xd->mi[0]->tx_size = tx_size;
// Skipping test
x->skip_txfm[0] = SKIP_TXFM_NONE; for (k = 0; k < num; k++) // Check if all ac coefficients can be quantized to zero. if (!(var_tx[k] < ac_thr || var == 0)) {
ac_test = 0; break;
}
for (k = 0; k < num; k++) // Check if dc coefficient can be quantized to zero. if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) {
dc_test = 0; break;
}
if (ac_test) {
x->skip_txfm[0] = SKIP_TXFM_AC_ONLY;
// If the transform in YUV planes are skippable, the mode search checks // fewer inter modes and doesn't check intra modes. if (skip_uv[0] & skip_uv[1]) {
*early_term = 1;
} return;
}
if (!skip_dc) { #if CONFIG_VP9_HIGHBITDEPTH
vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
dc_quant >> (xd->bd - 5), &rate, &dist); #else
vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
dc_quant >> 3, &rate, &dist); #endif// CONFIG_VP9_HIGHBITDEPTH
}
staticvoid model_rd_for_sb_y(VP9_COMP *cpi, BLOCK_SIZE bsize, MACROBLOCK *x,
MACROBLOCKD *xd, int *out_rate_sum,
int64_t *out_dist_sum, unsignedint *var_y, unsignedint *sse_y, int is_intra) { // Note our transform coeffs are 8 times an orthogonal transform. // Hence quantizer step is also 8 times. To get effective quantizer // we need to divide by 8 before sending to modeling function. unsignedint sse; int rate;
int64_t dist; struct macroblock_plane *const p = &x->plane[0]; struct macroblockd_plane *const pd = &xd->plane[0]; const int64_t dc_thr = p->quant_thred[0] >> 6; const int64_t ac_thr = p->quant_thred[1] >> 6; const uint32_t dc_quant = pd->dequant[0]; const uint32_t ac_quant = pd->dequant[1]; unsignedint var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride,
pd->dst.buf, pd->dst.stride, &sse); int skip_dc = 0;
*var_y = var;
*sse_y = sse;
xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, xd, var, sse, ac_thr,
x->source_variance, is_intra);
// Evaluate if the partition block is a skippable block in Y plane.
{ const BLOCK_SIZE unit_size = txsize_to_bsize[xd->mi[0]->tx_size]; constunsignedint num_blk_log2 =
(b_width_log2_lookup[bsize] - b_width_log2_lookup[unit_size]) +
(b_height_log2_lookup[bsize] - b_height_log2_lookup[unit_size]); constunsignedint sse_tx = sse >> num_blk_log2; constunsignedint var_tx = var >> num_blk_log2;
x->skip_txfm[0] = SKIP_TXFM_NONE; // Check if all ac coefficients can be quantized to zero. if (var_tx < ac_thr || var == 0) {
x->skip_txfm[0] = SKIP_TXFM_AC_ONLY; // Check if dc coefficient can be quantized to zero. if (sse_tx - var_tx < dc_thr || sse == var)
x->skip_txfm[0] = SKIP_TXFM_AC_DC;
} else { if (sse_tx - var_tx < dc_thr || sse == var) skip_dc = 1;
}
}
// If skippable is set, rate gets clobbered later.
this_rdc->rate <<= (2 + VP9_PROB_COST_SHIFT);
this_rdc->rate += (eob_cost << VP9_PROB_COST_SHIFT);
}
staticvoid model_rd_for_sb_uv(VP9_COMP *cpi, BLOCK_SIZE plane_bsize,
MACROBLOCK *x, MACROBLOCKD *xd,
RD_COST *this_rdc, unsignedint *var_y, unsignedint *sse_y, int start_plane, int stop_plane) { // Note our transform coeffs are 8 times an orthogonal transform. // Hence quantizer step is also 8 times. To get effective quantizer // we need to divide by 8 before sending to modeling function. unsignedint sse; int rate;
int64_t dist; int i; #if CONFIG_VP9_HIGHBITDEPTH
uint64_t tot_var = *var_y;
uint64_t tot_sse = *sse_y; #else
uint32_t tot_var = *var_y;
uint32_t tot_sse = *sse_y; #endif
this_rdc->rate = 0;
this_rdc->dist = 0;
for (i = start_plane; i <= stop_plane; ++i) { struct macroblock_plane *const p = &x->plane[i]; struct macroblockd_plane *const pd = &xd->plane[i]; const uint32_t dc_quant = pd->dequant[0]; const uint32_t ac_quant = pd->dequant[1]; const BLOCK_SIZE bs = plane_bsize; unsignedint var; if (!x->color_sensitivity[i - 1]) continue;
// The cost of skip bit needs to be added.
*rate = cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
[INTER_OFFSET(this_mode)];
// More on this part of rate // rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
// Scaling factor for SSE from spatial domain to frequency // domain is 16. Adjust distortion accordingly. // TODO(yunqingwang): In this function, only y-plane dist is // calculated.
*dist = (sse << 4); // + ((sse_u + sse_v) << 4);
mi->ref_frame[0] = INTRA_FRAME; // Initialize interp_filter here so we do not have to check for inter block // modes in get_pred_context_switchable_interp()
mi->interp_filter = SWITCHABLE_FILTERS;
staticINLINEvoid find_predictors(
VP9_COMP *cpi, MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame,
int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES], int const_motion[MAX_REF_FRAMES], int *ref_frame_skip_mask,
TileDataEnc *tile_data, int mi_row, int mi_col, struct buf_2d yv12_mb[4][MAX_MB_PLANE], BLOCK_SIZE bsize, int force_skip_low_temp_var, int comp_pred_allowed) {
VP9_COMMON *const cm = &cpi->common;
MACROBLOCKD *const xd = &x->e_mbd; const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
TileInfo *const tile_info = &tile_data->tile_info; // TODO(jingning) placeholder for inter-frame non-RD mode decision.
x->pred_mv_sad[ref_frame] = INT_MAX;
frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
frame_mv[ZEROMV][ref_frame].as_int = 0; // this needs various further optimizations. to be continued.. if ((cpi->ref_frame_flags & ref_frame_to_flag(ref_frame)) && (yv12 != NULL)) {
int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; conststruct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf); if (cm->use_prev_frame_mvs || comp_pred_allowed) {
vp9_find_mv_refs(cm, xd, xd->mi[0], ref_frame, candidates, mi_row, mi_col,
x->mbmi_ext->mode_context);
} else {
const_motion[ref_frame] =
mv_refs_rt(cpi, cm, x, xd, tile_info, xd->mi[0], ref_frame,
candidates, &frame_mv[NEWMV][ref_frame], mi_row, mi_col,
(int)(cpi->svc.use_base_mv && cpi->svc.spatial_layer_id));
}
vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
&frame_mv[NEARESTMV][ref_frame],
&frame_mv[NEARMV][ref_frame]); // Early exit for golden frame if force_skip_low_temp_var is set. if (!vp9_is_scaled(sf) && bsize >= BLOCK_8X8 &&
!(force_skip_low_temp_var && ref_frame == GOLDEN_FRAME)) {
vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
bsize);
}
} else {
*ref_frame_skip_mask |= (1 << ref_frame);
}
}
staticvoid vp9_NEWMV_diff_bias(const NOISE_ESTIMATE *ne, MACROBLOCKD *xd,
PREDICTION_MODE this_mode, RD_COST *this_rdc,
BLOCK_SIZE bsize, int mv_row, int mv_col, int is_last_frame, int lowvar_highsumdiff, int is_skin) { // Bias against MVs associated with NEWMV mode that are very different from // top/left neighbors. if (this_mode == NEWMV) { int al_mv_average_row; int al_mv_average_col; int left_row, left_col; int row_diff, col_diff; int above_mv_valid = 0; int left_mv_valid = 0; int above_row = 0; int above_col = 0;
if (cpi->sf.cb_pred_filter_search) { constint bsl = mi_width_log2_lookup[bsize];
pred_filter_search = cm->interp_filter == SWITCHABLE
? (((mi_row + mi_col) >> bsl) +
get_chessboard_index(cm->current_video_frame)) &
0x1
: 0;
} // Instead of using vp9_get_pred_context_switchable_interp(xd) to assign // filter_ref, we use a less strict condition on assigning filter_ref. // This is to reduce the probabily of entering the flow of not assigning // filter_ref and then skip filter search.
filter_ref = cm->interp_filter; if (cpi->sf.default_interp_filter != BILINEAR) { if (xd->above_mi && is_inter_block(xd->above_mi))
filter_ref = xd->above_mi->interp_filter; elseif (xd->left_mi && is_inter_block(xd->left_mi))
filter_ref = xd->left_mi->interp_filter;
}
if (cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR) { if (cpi->rc.alt_ref_gf_group || cpi->rc.is_src_frame_alt_ref)
usable_ref_frame = ALTREF_FRAME;
if (cpi->rc.is_src_frame_alt_ref) {
skip_ref_find_pred[LAST_FRAME] = 1;
skip_ref_find_pred[GOLDEN_FRAME] = 1;
} if (!cm->show_frame) { if (cpi->rc.frames_since_key == 1) {
usable_ref_frame = LAST_FRAME;
skip_ref_find_pred[GOLDEN_FRAME] = 1;
skip_ref_find_pred[ALTREF_FRAME] = 1;
}
}
}
// For svc mode, on spatial_layer_id > 0: if the reference has different scale // constrain the inter mode to only test zero motion. if (cpi->use_svc && svc->force_zero_mode_spatial_ref &&
svc->spatial_layer_id > 0 && !gf_temporal_ref) { if (cpi->ref_frame_flags & VP9_LAST_FLAG) { struct scale_factors *const ref_sf = &cm->frame_refs[LAST_FRAME - 1].sf; if (vp9_is_scaled(ref_sf)) {
svc_force_zero_mode[LAST_FRAME - 1] = 1;
inter_layer_ref = LAST_FRAME;
}
} if (cpi->ref_frame_flags & VP9_GOLD_FLAG) { struct scale_factors *const ref_sf = &cm->frame_refs[GOLDEN_FRAME - 1].sf; if (vp9_is_scaled(ref_sf)) {
svc_force_zero_mode[GOLDEN_FRAME - 1] = 1;
inter_layer_ref = GOLDEN_FRAME;
}
}
}
if (cpi->sf.short_circuit_low_temp_var) {
force_skip_low_temp_var =
get_force_skip_low_temp_var(&x->variance_low[0], mi_row, mi_col, bsize); // If force_skip_low_temp_var is set, and for short circuit mode = 1 and 3, // skip golden reference. if ((cpi->sf.short_circuit_low_temp_var == 1 ||
cpi->sf.short_circuit_low_temp_var == 3) &&
force_skip_low_temp_var) {
usable_ref_frame = LAST_FRAME;
}
}
// Compound prediction modes: (0,0) on LAST/GOLDEN and ARF. if (cm->reference_mode == REFERENCE_MODE_SELECT &&
cpi->sf.use_compound_nonrd_pickmode && usable_ref_frame == ALTREF_FRAME)
comp_modes = 2;
// If the segment reference frame feature is enabled and it's set to GOLDEN // reference, then make sure we don't skip checking GOLDEN, this is to // prevent possibility of not picking any mode. if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) {
usable_ref_frame = GOLDEN_FRAME;
skip_ref_find_pred[GOLDEN_FRAME] = 0;
thresh_svc_skip_golden = 0;
}
for (ref_frame = LAST_FRAME; ref_frame <= usable_ref_frame; ++ref_frame) { // Skip find_predictor if the reference frame is not in the // ref_frame_flags (i.e., not used as a reference for this frame).
skip_ref_find_pred[ref_frame] =
!(cpi->ref_frame_flags & ref_frame_to_flag(ref_frame)); if (!skip_ref_find_pred[ref_frame]) {
find_predictors(cpi, x, ref_frame, frame_mv, const_motion,
&ref_frame_skip_mask, tile_data, mi_row, mi_col, yv12_mb,
bsize, force_skip_low_temp_var, comp_modes > 0);
}
}
// Set the flag_svc_subpel to 1 for SVC if the lower spatial layer used // an averaging filter for downsampling (phase = 8). If so, we will test // a nonzero motion mode on the spatial reference. // The nonzero motion is half pixel shifted to left and top (-4, -4). if (cpi->use_svc && svc->spatial_layer_id > 0 &&
svc_force_zero_mode[inter_layer_ref - 1] &&
svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 &&
!gf_temporal_ref) {
svc_mv_col = -4;
svc_mv_row = -4;
flag_svc_subpel = 1;
}
// For SVC with quality layers, when QP of lower layer is lower // than current layer: force check of GF-ZEROMV before early exit // due to skip flag. if (svc->spatial_layer_id > 0 && no_scaling &&
(cpi->ref_frame_flags & VP9_GOLD_FLAG) &&
cm->base_qindex > svc->lower_layer_qindex + 10)
force_test_gf_zeromv = 1;
// For low motion content use x->sb_is_skin in addition to VeryHighSad // for setting large_block.
large_block = (x->content_state_sb == kVeryHighSad ||
(x->sb_is_skin && cpi->rc.avg_frame_low_motion > 70) ||
cpi->oxcf.speed < 7)
? bsize > BLOCK_32X32
: bsize >= BLOCK_32X32;
use_model_yrd_large =
cpi->oxcf.rc_mode == VPX_CBR && large_block &&
!cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) &&
cm->base_qindex;
for (idx = 0; idx < num_inter_modes + comp_modes; ++idx) { int rate_mv = 0; int mode_rd_thresh; int mode_index; int i;
int64_t this_sse; int is_skippable; int this_early_term = 0; int rd_computed = 0; int flag_preduv_computed[2] = { 0 }; int inter_mv_mode = 0; int skip_this_mv = 0; int comp_pred = 0; int force_mv_inter_layer = 0;
PREDICTION_MODE this_mode;
second_ref_frame = NO_REF_FRAME;
if (ref_frame > usable_ref_frame) continue; if (skip_ref_find_pred[ref_frame]) continue;
if (svc->previous_frame_is_intra_only) { if (ref_frame != LAST_FRAME || frame_mv[this_mode][ref_frame].as_int != 0) continue;
}
// If the segment reference frame feature is enabled then do nothing if the // current ref frame is not allowed. if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) continue;
if (flag_svc_subpel && ref_frame == inter_layer_ref) {
force_mv_inter_layer = 1; // Only test mode if NEARESTMV/NEARMV is (svc_mv_col, svc_mv_row), // otherwise set NEWMV to (svc_mv_col, svc_mv_row). if (this_mode == NEWMV) {
frame_mv[this_mode][ref_frame].as_mv.col = svc_mv_col;
frame_mv[this_mode][ref_frame].as_mv.row = svc_mv_row;
} elseif (frame_mv[this_mode][ref_frame].as_mv.col != svc_mv_col ||
frame_mv[this_mode][ref_frame].as_mv.row != svc_mv_row) { continue;
}
}
if (comp_pred) { if (!cpi->allow_comp_inter_inter) continue; // Skip compound inter modes if ARF is not available. if (!(cpi->ref_frame_flags & ref_frame_to_flag(second_ref_frame))) continue; // Do not allow compound prediction if the segment level reference frame // feature is in use as in this case there can only be one reference. if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME)) continue;
}
// For CBR mode: skip the golden reference search if sse of zeromv_last is // below threshold. if (ref_frame == GOLDEN_FRAME && cpi->oxcf.rc_mode == VPX_CBR &&
((cpi->use_svc && sse_zeromv_normalized < thresh_svc_skip_golden) ||
(!cpi->use_svc && sse_zeromv_normalized < thresh_skip_golden))) continue;
if (!(cpi->ref_frame_flags & ref_frame_to_flag(ref_frame))) continue;
// For screen content. If zero_temp_sad source is computed: skip // non-zero motion check for stationary blocks. If the superblock is // non-stationary then for flat blocks skip the zero last check (keep golden // as it may be inter-layer reference). Otherwise (if zero_temp_sad_source // is not computed) skip non-zero motion check for flat blocks. // TODO(marpan): Compute zero_temp_sad_source per coding block. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) { if (cpi->compute_source_sad_onepass && cpi->sf.use_source_sad) { if ((frame_mv[this_mode][ref_frame].as_int != 0 &&
x->zero_temp_sad_source) ||
(frame_mv[this_mode][ref_frame].as_int == 0 &&
x->source_variance == 0 && ref_frame == LAST_FRAME &&
!x->zero_temp_sad_source)) continue;
} elseif (frame_mv[this_mode][ref_frame].as_int != 0 &&
x->source_variance == 0) { continue;
}
}
if (!(cpi->sf.inter_mode_mask[bsize] & (1 << this_mode))) continue;
if (cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR) { if (cpi->rc.is_src_frame_alt_ref &&
(ref_frame != ALTREF_FRAME ||
frame_mv[this_mode][ref_frame].as_int != 0)) continue;
if (const_motion[ref_frame] && this_mode == NEARMV) continue;
// Skip non-zeromv mode search for golden frame if force_skip_low_temp_var // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped // later. if (!force_mv_inter_layer && force_skip_low_temp_var &&
ref_frame == GOLDEN_FRAME &&
frame_mv[this_mode][ref_frame].as_int != 0) { continue;
}
// Search for the best prediction filter type, when the resulting // motion vector is at sub-pixel accuracy level for luma component, i.e., // the last three bits are all zeros. if (reuse_inter_pred) { if (!this_mode_pred) {
this_mode_pred = &tmp[3];
} else {
this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
pd->dst.buf = this_mode_pred->data;
pd->dst.stride = bw;
}
}
// Bias against NEWMV that is very different from its neighbors, and bias // to small motion-lastref for noisy input. if (cpi->oxcf.rc_mode == VPX_CBR && cpi->oxcf.speed >= 5 &&
cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
vp9_NEWMV_diff_bias(&cpi->noise_estimate, xd, this_mode, &this_rdc, bsize,
frame_mv[this_mode][ref_frame].as_mv.row,
frame_mv[this_mode][ref_frame].as_mv.col,
ref_frame == LAST_FRAME, x->lowvar_highsumdiff,
x->sb_is_skin);
}
// Skipping checking: test to see if this block can be reconstructed by // prediction only. if (cpi->allow_encode_breakout && !xd->lossless && !scene_change_detected &&
!svc->high_num_blocks_with_motion) {
encode_breakout_test(cpi, x, bsize, mi_row, mi_col, ref_frame, this_mode,
var_y, sse_y, yv12_mb, &this_rdc.rate,
&this_rdc.dist, flag_preduv_computed); if (x->skip) {
this_rdc.rate += rate_mv;
this_rdc.rdcost =
RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
}
}
// On spatially flat blocks for screne content: bias against zero-last // if the sse_y is non-zero. Only on scene change or high motion frames. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
(scene_change_detected || svc->high_num_blocks_with_motion) &&
ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0 &&
svc->spatial_layer_id == 0 && x->source_variance == 0 && sse_y > 0) {
this_rdc.rdcost = this_rdc.rdcost << 2;
}
if (reuse_inter_pred) {
free_pred_buffer(best_pickmode.best_pred);
best_pickmode.best_pred = this_mode_pred;
}
} else { if (reuse_inter_pred) free_pred_buffer(this_mode_pred);
}
if (x->skip &&
(!force_test_gf_zeromv || mode_checked[ZEROMV][GOLDEN_FRAME])) break;
// If early termination flag is 1 and at least 2 modes are checked, // the mode search is terminated. if (best_early_term && idx > 0 && !scene_change_detected &&
(!force_test_gf_zeromv || mode_checked[ZEROMV][GOLDEN_FRAME])) {
x->skip = 1; break;
}
}
// For spatial enhancemanent layer: perform intra prediction only if base // layer is chosen as the reference. Always perform intra prediction if // LAST is the only reference, or is_key_frame is set, or on base // temporal layer. if (svc->spatial_layer_id && !gf_temporal_ref) {
perform_intra_pred =
svc->temporal_layer_id == 0 ||
svc->layer_context[svc->temporal_layer_id].is_key_frame ||
!(cpi->ref_frame_flags & VP9_GOLD_FLAG) ||
(!svc->layer_context[svc->temporal_layer_id].is_key_frame &&
svc_force_zero_mode[best_pickmode.best_ref_frame - 1]);
inter_mode_thresh = (inter_mode_thresh << 1) + inter_mode_thresh;
} if ((cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR &&
cpi->rc.is_src_frame_alt_ref) ||
svc->previous_frame_is_intra_only)
perform_intra_pred = 0;
// If the segment reference frame feature is enabled and set then // skip the intra prediction. if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) > 0)
perform_intra_pred = 0;
// Perform intra prediction search, if the best SAD is above a certain // threshold. if (best_rdc.rdcost == INT64_MAX ||
(cpi->oxcf.content == VP9E_CONTENT_SCREEN && x->source_variance == 0) ||
(scene_change_detected && perform_intra_pred) ||
((!force_skip_low_temp_var || bsize < BLOCK_32X32 ||
x->content_state_sb == kVeryHighSad) &&
perform_intra_pred && !x->skip && best_rdc.rdcost > inter_mode_thresh &&
bsize <= cpi->sf.max_intra_bsize && !x->skip_low_source_sad &&
!x->lowvar_highsumdiff)) { struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
int64_t this_sse = INT64_MAX; int i;
PRED_BUFFER *const best_pred = best_pickmode.best_pred;
TX_SIZE intra_tx_size =
VPXMIN(max_txsize_lookup[bsize],
tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
for (i = 0; i < 4; ++i) { const PREDICTION_MODE this_mode = intra_mode_list[i];
THR_MODES mode_index = mode_idx[INTRA_FRAME][mode_offset(this_mode)]; int mode_rd_thresh = rd_threshes[mode_index]; // For spatially flat blocks, under short_circuit_flat_blocks flag: // only check DC mode for stationary blocks, otherwise also check // H and V mode. if (sf->short_circuit_flat_blocks && x->source_variance == 0 &&
((x->zero_temp_sad_source && this_mode != DC_PRED) || i > 2)) { continue;
}
if (!((1 << this_mode) & cpi->sf.intra_y_mode_bsize_mask[bsize])) continue;
if ((cpi->sf.adaptive_rd_thresh_row_mt &&
rd_less_than_thresh_row_mt(best_rdc.rdcost, mode_rd_thresh,
&rd_thresh_freq_fact[mode_index])) ||
(!cpi->sf.adaptive_rd_thresh_row_mt &&
rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
&rd_thresh_freq_fact[mode_index]))) { // Avoid this early exit for screen on base layer, for scene // changes or high motion frames. if (cpi->oxcf.content != VP9E_CONTENT_SCREEN ||
svc->spatial_layer_id > 0 ||
(!scene_change_detected && !svc->high_num_blocks_with_motion)) continue;
}
// Check skip cost here since skippable is not set for for uv, this // mirrors the behavior used by inter if (args.skippable) {
x->skip_txfm[0] = SKIP_TXFM_AC_DC;
this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 1);
} else {
x->skip_txfm[0] = SKIP_TXFM_NONE;
this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 0);
} // Inter and intra RD will mismatch in scale for non-screen content. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) { if (x->color_sensitivity[0])
vp9_foreach_transformed_block_in_plane(xd, bsize, 1,
estimate_block_intra, &args); if (x->color_sensitivity[1])
vp9_foreach_transformed_block_in_plane(xd, bsize, 2,
estimate_block_intra, &args);
}
this_rdc.rate += cpi->mbmode_cost[this_mode];
this_rdc.rate += ref_frame_cost[INTRA_FRAME];
this_rdc.rate += intra_cost_penalty;
this_rdc.rdcost =
RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
// Reset mb_mode_info to the best inter mode. if (best_pickmode.best_ref_frame != INTRA_FRAME) {
mi->tx_size = best_pickmode.best_tx_size;
} else {
mi->tx_size = best_pickmode.best_intra_tx_size;
}
}
if (cpi->sf.adaptive_rd_thresh) {
THR_MODES best_mode_idx =
mode_idx[best_pickmode.best_ref_frame][mode_offset(mi->mode)];
if (best_pickmode.best_ref_frame == INTRA_FRAME) { // Only consider the modes that are included in the intra_mode_list. int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE); int i;
// TODO(yunqingwang): Check intra mode mask and only update freq_fact // for those valid modes. for (i = 0; i < intra_modes; i++) { if (cpi->sf.adaptive_rd_thresh_row_mt)
update_thresh_freq_fact_row_mt(cpi, tile_data, x->source_variance,
thresh_freq_fact_idx, INTRA_FRAME,
best_mode_idx, intra_mode_list[i]); else
update_thresh_freq_fact(cpi, tile_data, x->source_variance, bsize,
INTRA_FRAME, best_mode_idx,
intra_mode_list[i]);
}
} else { for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
PREDICTION_MODE this_mode; if (best_pickmode.best_ref_frame != ref_frame) continue; for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) { if (cpi->sf.adaptive_rd_thresh_row_mt)
update_thresh_freq_fact_row_mt(cpi, tile_data, x->source_variance,
thresh_freq_fact_idx, ref_frame,
best_mode_idx, this_mode); else
update_thresh_freq_fact(cpi, tile_data, x->source_variance, bsize,
ref_frame, best_mode_idx, this_mode);
}
}
}
}
// TODO(jingning, agrange): Scaling reference frame not supported for // sub8x8 blocks. Is this supported now? if (ref_frame > INTRA_FRAME &&
vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf)) continue;
// If the segment reference frame feature is enabled.... // then do nothing if the current ref frame is not allowed.. if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) continue;
// set buffer pointers for sub8x8 motion search.
p->src.buf =
&p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
pd->dst.buf =
&pd->dst.buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->dst.stride)];
pd->pre[0].buf =
&pd->pre[0]
.buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->pre[0].stride)];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.