Merge "Cleaning up vp9_aq_cyclicrefresh.{c, h}."

This commit is contained in:
Dmitry Kovalev
2014-03-24 15:44:50 -07:00
committed by Gerrit Code Review
3 changed files with 53 additions and 64 deletions

View File

@@ -21,31 +21,27 @@
// Check if we should turn off cyclic refresh based on bitrate condition. // Check if we should turn off cyclic refresh based on bitrate condition.
static int apply_cyclic_refresh_bitrate(VP9_COMP *const cpi) { static int apply_cyclic_refresh_bitrate(const VP9_COMMON *cm,
const RATE_CONTROL *rc) {
// Turn off cyclic refresh if bits available per frame is not sufficiently // Turn off cyclic refresh if bits available per frame is not sufficiently
// larger than bit cost of segmentation. Segment map bit cost should scale // larger than bit cost of segmentation. Segment map bit cost should scale
// with number of seg blocks, so compare available bits to number of blocks. // with number of seg blocks, so compare available bits to number of blocks.
// Average bits available per frame = av_per_frame_bandwidth // Average bits available per frame = av_per_frame_bandwidth
// Number of (8x8) blocks in frame = mi_rows * mi_cols; // Number of (8x8) blocks in frame = mi_rows * mi_cols;
float factor = 0.5; const float factor = 0.5;
int number_blocks = cpi->common.mi_rows * cpi->common.mi_cols; const int number_blocks = cm->mi_rows * cm->mi_cols;
// The condition below corresponds to turning off at target bitrates: // The condition below corresponds to turning off at target bitrates:
// ~24kbps for CIF, 72kbps for VGA (at 30fps). // ~24kbps for CIF, 72kbps for VGA (at 30fps).
if (cpi->rc.av_per_frame_bandwidth < factor * number_blocks) return rc->av_per_frame_bandwidth >= factor * number_blocks;
return 0;
else
return 1;
} }
// Check if this coding block, of size bsize, should be considered for refresh // Check if this coding block, of size bsize, should be considered for refresh
// (lower-qp coding). Decision can be based on various factors, such as // (lower-qp coding). Decision can be based on various factors, such as
// size of the coding block (i.e., below min_block size rejected), coding // size of the coding block (i.e., below min_block size rejected), coding
// mode, and rate/distortion. // mode, and rate/distortion.
static int candidate_refresh_aq(VP9_COMP *const cpi, static int candidate_refresh_aq(const CYCLIC_REFRESH *cr,
MODE_INFO *const mi, const MB_MODE_INFO *mbmi,
int bsize, BLOCK_SIZE bsize, int use_rd) {
int use_rd) {
CYCLIC_REFRESH *const cr = &cpi->cyclic_refresh;
if (use_rd) { if (use_rd) {
// If projected rate is below the thresh_rate (well below target, // If projected rate is below the thresh_rate (well below target,
// so undershoot expected), accept it for lower-qp coding. // so undershoot expected), accept it for lower-qp coding.
@@ -56,18 +52,18 @@ static int candidate_refresh_aq(VP9_COMP *const cpi,
// 2) mode is non-zero mv and projected distortion is above thresh_dist // 2) mode is non-zero mv and projected distortion is above thresh_dist
// 3) mode is an intra-mode (we may want to allow some of this under // 3) mode is an intra-mode (we may want to allow some of this under
// another thresh_dist) // another thresh_dist)
else if ((bsize < cr->min_block_size) || else if (bsize < cr->min_block_size ||
(mi->mbmi.mv[0].as_int != 0 && (mbmi->mv[0].as_int != 0 &&
cr->projected_dist_sb > cr->thresh_dist_sb) || cr->projected_dist_sb > cr->thresh_dist_sb) ||
!is_inter_block(&mi->mbmi)) !is_inter_block(mbmi))
return 0; return 0;
else else
return 1; return 1;
} else { } else {
// Rate/distortion not used for update. // Rate/distortion not used for update.
if ((bsize < cr->min_block_size) || if (bsize < cr->min_block_size ||
(mi->mbmi.mv[0].as_int != 0) || mbmi->mv[0].as_int != 0 ||
!is_inter_block(&mi->mbmi)) !is_inter_block(mbmi))
return 0; return 0;
else else
return 1; return 1;
@@ -78,32 +74,32 @@ static int candidate_refresh_aq(VP9_COMP *const cpi,
// check if we should reset the segment_id, and update the cyclic_refresh map // check if we should reset the segment_id, and update the cyclic_refresh map
// and segmentation map. // and segmentation map.
void vp9_update_segment_aq(VP9_COMP *const cpi, void vp9_update_segment_aq(VP9_COMP *const cpi,
MODE_INFO *const mi, MB_MODE_INFO *const mbmi,
int mi_row, int mi_row,
int mi_col, int mi_col,
int bsize, BLOCK_SIZE bsize,
int use_rd) { int use_rd) {
const VP9_COMMON *const cm = &cpi->common;
CYCLIC_REFRESH *const cr = &cpi->cyclic_refresh; CYCLIC_REFRESH *const cr = &cpi->cyclic_refresh;
VP9_COMMON *const cm = &cpi->common;
const int bw = num_8x8_blocks_wide_lookup[bsize]; const int bw = num_8x8_blocks_wide_lookup[bsize];
const int bh = num_8x8_blocks_high_lookup[bsize]; const int bh = num_8x8_blocks_high_lookup[bsize];
const int xmis = MIN(cm->mi_cols - mi_col, bw); const int xmis = MIN(cm->mi_cols - mi_col, bw);
const int ymis = MIN(cm->mi_rows - mi_row, bh); const int ymis = MIN(cm->mi_rows - mi_row, bh);
const int block_index = mi_row * cm->mi_cols + mi_col; const int block_index = mi_row * cm->mi_cols + mi_col;
const int refresh_this_block = candidate_refresh_aq(cr, mbmi, bsize, use_rd);
// Default is to not update the refresh map. // Default is to not update the refresh map.
int new_map_value = cr->map[block_index]; int new_map_value = cr->map[block_index];
int x = 0; int y = 0; int x = 0; int y = 0;
int current_segment = mi->mbmi.segment_id;
int refresh_this_block = candidate_refresh_aq(cpi, mi, bsize, use_rd);
// Check if we should reset the segment_id for this block. // Check if we should reset the segment_id for this block.
if (current_segment && !refresh_this_block) if (mbmi->segment_id > 0 && !refresh_this_block)
mi->mbmi.segment_id = 0; mbmi->segment_id = 0;
// Update the cyclic refresh map, to be used for setting segmentation map // Update the cyclic refresh map, to be used for setting segmentation map
// for the next frame. If the block will be refreshed this frame, mark it // for the next frame. If the block will be refreshed this frame, mark it
// as clean. The magnitude of the -ve influences how long before we consider // as clean. The magnitude of the -ve influences how long before we consider
// it for refresh again. // it for refresh again.
if (mi->mbmi.segment_id == 1) { if (mbmi->segment_id == 1) {
new_map_value = -cr->time_for_refresh; new_map_value = -cr->time_for_refresh;
} else if (refresh_this_block) { } else if (refresh_this_block) {
// Else if it is accepted as candidate for refresh, and has not already // Else if it is accepted as candidate for refresh, and has not already
@@ -121,39 +117,40 @@ void vp9_update_segment_aq(VP9_COMP *const cpi,
for (x = 0; x < xmis; x++) { for (x = 0; x < xmis; x++) {
cr->map[block_index + y * cm->mi_cols + x] = new_map_value; cr->map[block_index + y * cm->mi_cols + x] = new_map_value;
cpi->segmentation_map[block_index + y * cm->mi_cols + x] = cpi->segmentation_map[block_index + y * cm->mi_cols + x] =
mi->mbmi.segment_id; mbmi->segment_id;
} }
// Keep track of actual number (in units of 8x8) of blocks in segment 1 used // Keep track of actual number (in units of 8x8) of blocks in segment 1 used
// for encoding this frame. // for encoding this frame.
if (mi->mbmi.segment_id) if (mbmi->segment_id)
cr->num_seg_blocks += xmis * ymis; cr->num_seg_blocks += xmis * ymis;
} }
// Setup cyclic background refresh: set delta q and segmentation map. // Setup cyclic background refresh: set delta q and segmentation map.
void vp9_setup_cyclic_refresh_aq(VP9_COMP *const cpi) { void vp9_setup_cyclic_refresh_aq(VP9_COMP *const cpi) {
VP9_COMMON *const cm = &cpi->common; VP9_COMMON *const cm = &cpi->common;
const RATE_CONTROL *const rc = &cpi->rc;
CYCLIC_REFRESH *const cr = &cpi->cyclic_refresh; CYCLIC_REFRESH *const cr = &cpi->cyclic_refresh;
struct segmentation *const seg = &cm->seg; struct segmentation *const seg = &cm->seg;
unsigned char *seg_map = cpi->segmentation_map; unsigned char *const seg_map = cpi->segmentation_map;
int apply_cyclic_refresh = apply_cyclic_refresh_bitrate(cpi); const int apply_cyclic_refresh = apply_cyclic_refresh_bitrate(cm, rc);
// Don't apply refresh on key frame or enhancement layer frames. // Don't apply refresh on key frame or enhancement layer frames.
if (!apply_cyclic_refresh || if (!apply_cyclic_refresh ||
(cpi->common.frame_type == KEY_FRAME) || (cm->frame_type == KEY_FRAME) ||
(cpi->svc.temporal_layer_id > 0)) { (cpi->svc.temporal_layer_id > 0)) {
// Set segmentation map to 0 and disable. // Set segmentation map to 0 and disable.
vpx_memset(seg_map, 0, cm->mi_rows * cm->mi_cols); vpx_memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
vp9_disable_segmentation(&cm->seg); vp9_disable_segmentation(&cm->seg);
if (cpi->common.frame_type == KEY_FRAME) if (cm->frame_type == KEY_FRAME)
cr->mb_index = 0; cr->mb_index = 0;
return; return;
} else { } else {
const int mbs_in_frame = cm->mi_rows * cm->mi_cols;
int qindex_delta = 0; int qindex_delta = 0;
int mbs_in_frame = cm->mi_rows * cm->mi_cols; int i, x, y, block_count;
int i, x, y, block_count, bl_index, bl_index2; int mi_row, mi_col, qindex2;
int sum_map, mi_row, mi_col, xmis, ymis, qindex2;
// Rate target ratio to set q delta. // Rate target ratio to set q delta.
float rate_ratio_qdelta = 2.0; const float rate_ratio_qdelta = 2.0;
vp9_clear_system_state(); vp9_clear_system_state();
// Some of these parameters may be set via codec-control function later. // Some of these parameters may be set via codec-control function later.
cr->max_mbs_perframe = 10; cr->max_mbs_perframe = 10;
@@ -161,14 +158,14 @@ void vp9_setup_cyclic_refresh_aq(VP9_COMP *const cpi) {
cr->min_block_size = BLOCK_16X16; cr->min_block_size = BLOCK_16X16;
cr->time_for_refresh = 1; cr->time_for_refresh = 1;
// Set rate threshold to some fraction of target (and scaled by 256). // Set rate threshold to some fraction of target (and scaled by 256).
cr->thresh_rate_sb = (cpi->rc.sb64_target_rate * 256) >> 2; cr->thresh_rate_sb = (rc->sb64_target_rate * 256) >> 2;
// Distortion threshold, quadratic in Q, scale factor to be adjusted. // Distortion threshold, quadratic in Q, scale factor to be adjusted.
cr->thresh_dist_sb = 8 * (int)(vp9_convert_qindex_to_q(cm->base_qindex) * cr->thresh_dist_sb = 8 * (int)(vp9_convert_qindex_to_q(cm->base_qindex) *
vp9_convert_qindex_to_q(cm->base_qindex)); vp9_convert_qindex_to_q(cm->base_qindex));
if (cpi->sf.use_nonrd_pick_mode) { if (cpi->sf.use_nonrd_pick_mode) {
// May want to be more conservative with thresholds in non-rd mode for now // May want to be more conservative with thresholds in non-rd mode for now
// as rate/distortion are derived from model based on prediction residual. // as rate/distortion are derived from model based on prediction residual.
cr->thresh_rate_sb = (cpi->rc.sb64_target_rate * 256) >> 3; cr->thresh_rate_sb = (rc->sb64_target_rate * 256) >> 3;
cr->thresh_dist_sb = 4 * (int)(vp9_convert_qindex_to_q(cm->base_qindex) * cr->thresh_dist_sb = 4 * (int)(vp9_convert_qindex_to_q(cm->base_qindex) *
vp9_convert_qindex_to_q(cm->base_qindex)); vp9_convert_qindex_to_q(cm->base_qindex));
} }
@@ -200,9 +197,8 @@ void vp9_setup_cyclic_refresh_aq(VP9_COMP *const cpi) {
rate_ratio_qdelta); rate_ratio_qdelta);
// TODO(marpan): Incorporate the actual-vs-target rate over/undershoot from // TODO(marpan): Incorporate the actual-vs-target rate over/undershoot from
// previous encoded frame. // previous encoded frame.
if ((-qindex_delta) > cr->max_qdelta_perc * cm->base_qindex / 100) { if (-qindex_delta > cr->max_qdelta_perc * cm->base_qindex / 100)
qindex_delta = -cr->max_qdelta_perc * cm->base_qindex / 100; qindex_delta = -cr->max_qdelta_perc * cm->base_qindex / 100;
}
// Compute rd-mult for segment 1. // Compute rd-mult for segment 1.
qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ); qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ);
@@ -238,29 +234,21 @@ void vp9_setup_cyclic_refresh_aq(VP9_COMP *const cpi) {
// Enforce constant segment map over superblock. // Enforce constant segment map over superblock.
for (mi_row = 0; mi_row < cm->mi_rows; mi_row += MI_BLOCK_SIZE) for (mi_row = 0; mi_row < cm->mi_rows; mi_row += MI_BLOCK_SIZE)
for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) { for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) {
bl_index = mi_row * cm->mi_cols + mi_col; const int bl_index = mi_row * cm->mi_cols + mi_col;
xmis = num_8x8_blocks_wide_lookup[BLOCK_64X64]; const int xmis = MIN(cm->mi_cols - mi_col,
ymis = num_8x8_blocks_high_lookup[BLOCK_64X64]; num_8x8_blocks_wide_lookup[BLOCK_64X64]);
xmis = MIN(cm->mi_cols - mi_col, xmis); const int ymis = MIN(cm->mi_rows - mi_row,
ymis = MIN(cm->mi_rows - mi_row, ymis); num_8x8_blocks_high_lookup[BLOCK_64X64]);
sum_map = 0; int sum_map = 0;
for (y = 0; y < ymis; y++) for (y = 0; y < ymis; y++)
for (x = 0; x < xmis; x++) { for (x = 0; x < xmis; x++)
bl_index2 = bl_index + y * cm->mi_cols + x; sum_map += seg_map[bl_index + y * cm->mi_cols + x];
sum_map += seg_map[bl_index2];
}
// If segment is partial over superblock, reset. // If segment is partial over superblock, reset.
if (sum_map > 0 && sum_map < xmis * ymis) { if (sum_map > 0 && sum_map < xmis * ymis) {
int new_value; const int new_value = (sum_map >= xmis * ymis / 2);
if (sum_map < xmis * ymis / 2)
new_value = 0;
else
new_value = 1;
for (y = 0; y < ymis; y++) for (y = 0; y < ymis; y++)
for (x = 0; x < xmis; x++) { for (x = 0; x < xmis; x++)
bl_index2 = bl_index + y * cm->mi_cols + x; seg_map[bl_index + y * cm->mi_cols + x] = new_value;
seg_map[bl_index2] = new_value;
}
} }
} }
} }

View File

@@ -51,10 +51,10 @@ struct VP9_COMP;
// check if we should reset the segment_id, and update the cyclic_refresh map // check if we should reset the segment_id, and update the cyclic_refresh map
// and segmentation map. // and segmentation map.
void vp9_update_segment_aq(struct VP9_COMP *const cpi, void vp9_update_segment_aq(struct VP9_COMP *const cpi,
MODE_INFO *const mi, MB_MODE_INFO *const mbmi,
int mi_row, int mi_row,
int mi_col, int mi_col,
int bsize, BLOCK_SIZE bsize,
int use_rd); int use_rd);
// Setup cyclic background refresh: set delta q and segmentation map. // Setup cyclic background refresh: set delta q and segmentation map.

View File

@@ -903,7 +903,8 @@ static void update_state(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx,
output_enabled) { output_enabled) {
// Check for reseting segment_id and update cyclic map. // Check for reseting segment_id and update cyclic map.
if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && seg->enabled) { if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && seg->enabled) {
vp9_update_segment_aq(cpi, xd->mi_8x8[0], mi_row, mi_col, bsize, 1); vp9_update_segment_aq(cpi, &xd->mi_8x8[0]->mbmi,
mi_row, mi_col, bsize, 1);
vp9_init_plane_quantizers(cpi, x); vp9_init_plane_quantizers(cpi, x);
} }
mi->mbmi.segment_id = xd->mi_8x8[0]->mbmi.segment_id; mi->mbmi.segment_id = xd->mi_8x8[0]->mbmi.segment_id;
@@ -1470,7 +1471,7 @@ static void update_state_rt(VP9_COMP *cpi, const PICK_MODE_CONTEXT *ctx,
// Check for reseting segment_id and update cyclic map. // Check for reseting segment_id and update cyclic map.
if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && seg->enabled) { if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && seg->enabled) {
vp9_update_segment_aq(cpi, xd->mi_8x8[0], mi_row, mi_col, bsize, 1); vp9_update_segment_aq(cpi, &xd->mi_8x8[0]->mbmi, mi_row, mi_col, bsize, 1);
vp9_init_plane_quantizers(cpi, x); vp9_init_plane_quantizers(cpi, x);
} }