2014-03-15 01:35:47 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2014-03-22 01:31:53 +04:00
|
|
|
#include "vp9/encoder/vp9_aq_cyclicrefresh.h"
|
2014-03-15 01:35:47 +04:00
|
|
|
|
|
|
|
#include "vp9/common/vp9_seg_common.h"
|
|
|
|
|
|
|
|
#include "vp9/encoder/vp9_ratectrl.h"
|
|
|
|
#include "vp9/encoder/vp9_segmentation.h"
|
|
|
|
|
2014-03-26 22:00:35 +04:00
|
|
|
struct CYCLIC_REFRESH {
|
2014-04-03 05:37:04 +04:00
|
|
|
// Percentage of blocks per frame that are targeted as candidates
|
2014-03-27 05:09:20 +04:00
|
|
|
// for cyclic refresh.
|
2014-04-03 05:37:04 +04:00
|
|
|
int percent_refresh;
|
2014-03-26 22:00:35 +04:00
|
|
|
// Maximum q-delta as percentage of base q.
|
|
|
|
int max_qdelta_perc;
|
2014-03-27 05:09:20 +04:00
|
|
|
// Superblock starting index for cycling through the frame.
|
|
|
|
int sb_index;
|
2014-04-03 05:37:04 +04:00
|
|
|
// Controls how long block will need to wait to be refreshed again, in
|
|
|
|
// excess of the cycle time, i.e., in the case of all zero motion, block
|
|
|
|
// will be refreshed every (100/percent_refresh + time_for_refresh) frames.
|
2014-03-26 22:00:35 +04:00
|
|
|
int time_for_refresh;
|
2014-04-03 05:37:04 +04:00
|
|
|
// // Target number of (8x8) blocks that are set for delta-q (segment 1).
|
|
|
|
int target_num_seg_blocks;
|
2014-03-27 05:09:20 +04:00
|
|
|
// Actual number of (8x8) blocks that were applied delta-q (segment 1).
|
2014-04-03 05:37:04 +04:00
|
|
|
int actual_num_seg_blocks;
|
2014-03-26 22:00:35 +04:00
|
|
|
// RD mult. parameters for segment 1.
|
|
|
|
int rdmult;
|
|
|
|
// Cyclic refresh map.
|
|
|
|
signed char *map;
|
2015-01-22 03:09:13 +03:00
|
|
|
// Thresholds applied to the projected rate/distortion of the coding block,
|
|
|
|
// when deciding whether block should be refreshed.
|
2014-03-26 22:00:35 +04:00
|
|
|
int64_t thresh_rate_sb;
|
|
|
|
int64_t thresh_dist_sb;
|
2015-01-22 03:09:13 +03:00
|
|
|
// Threshold applied to the motion vector (in units of 1/8 pel) of the
|
|
|
|
// coding block, when deciding whether block should be refreshed.
|
|
|
|
int16_t motion_thresh;
|
2014-04-03 05:37:04 +04:00
|
|
|
// Rate target ratio to set q delta.
|
|
|
|
double rate_ratio_qdelta;
|
2015-03-06 04:26:52 +03:00
|
|
|
double low_content_avg;
|
2014-03-26 22:00:35 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) {
|
|
|
|
CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr));
|
|
|
|
if (cr == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map));
|
|
|
|
if (cr->map == NULL) {
|
|
|
|
vpx_free(cr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vp9_cyclic_refresh_free(CYCLIC_REFRESH *cr) {
|
|
|
|
vpx_free(cr->map);
|
|
|
|
vpx_free(cr);
|
|
|
|
}
|
2014-03-15 01:35:47 +04:00
|
|
|
|
|
|
|
// Check if we should turn off cyclic refresh based on bitrate condition.
|
2014-03-24 21:48:14 +04:00
|
|
|
static int apply_cyclic_refresh_bitrate(const VP9_COMMON *cm,
|
|
|
|
const RATE_CONTROL *rc) {
|
2014-03-15 01:35:47 +04:00
|
|
|
// Turn off cyclic refresh if bits available per frame is not sufficiently
|
|
|
|
// 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.
|
2014-04-10 22:28:29 +04:00
|
|
|
// Average bits available per frame = avg_frame_bandwidth
|
2014-03-15 01:35:47 +04:00
|
|
|
// Number of (8x8) blocks in frame = mi_rows * mi_cols;
|
2014-03-24 21:48:14 +04:00
|
|
|
const float factor = 0.5;
|
|
|
|
const int number_blocks = cm->mi_rows * cm->mi_cols;
|
2014-03-15 01:35:47 +04:00
|
|
|
// The condition below corresponds to turning off at target bitrates:
|
|
|
|
// ~24kbps for CIF, 72kbps for VGA (at 30fps).
|
2014-03-27 05:09:20 +04:00
|
|
|
// Also turn off at very small frame sizes, to avoid too large fraction of
|
|
|
|
// superblocks to be refreshed per frame. Threshold below is less than QCIF.
|
2014-04-10 22:28:29 +04:00
|
|
|
if (rc->avg_frame_bandwidth < factor * number_blocks ||
|
2014-03-27 05:09:20 +04:00
|
|
|
number_blocks / 64 < 5)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
2014-03-15 01:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// size of the coding block (i.e., below min_block size rejected), coding
|
|
|
|
// mode, and rate/distortion.
|
2014-03-24 21:48:14 +04:00
|
|
|
static int candidate_refresh_aq(const CYCLIC_REFRESH *cr,
|
|
|
|
const MB_MODE_INFO *mbmi,
|
2015-01-22 03:09:13 +03:00
|
|
|
int64_t rate,
|
2015-01-28 22:34:24 +03:00
|
|
|
int64_t dist,
|
|
|
|
int bsize) {
|
2015-01-22 03:09:13 +03:00
|
|
|
MV mv = mbmi->mv[0].as_mv;
|
2015-01-28 22:34:24 +03:00
|
|
|
// Reject the block for lower-qp coding if projected distortion
|
2015-01-22 03:09:13 +03:00
|
|
|
// is above the threshold, and any of the following is true:
|
|
|
|
// 1) mode uses large mv
|
|
|
|
// 2) mode is an intra-mode
|
2015-01-28 22:34:24 +03:00
|
|
|
// Otherwise accept for refresh.
|
|
|
|
if (dist > cr->thresh_dist_sb &&
|
|
|
|
(mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh ||
|
|
|
|
mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh ||
|
|
|
|
!is_inter_block(mbmi)))
|
|
|
|
return CR_SEGMENT_ID_BASE;
|
|
|
|
else if (bsize >= BLOCK_32X32 &&
|
|
|
|
rate < cr->thresh_rate_sb &&
|
|
|
|
is_inter_block(mbmi) &&
|
|
|
|
mbmi->mv[0].as_int == 0)
|
|
|
|
// More aggressive delta-q for bigger blocks with zero motion.
|
|
|
|
return CR_SEGMENT_ID_BOOST2;
|
2015-01-22 03:09:13 +03:00
|
|
|
else
|
2015-01-28 22:34:24 +03:00
|
|
|
return CR_SEGMENT_ID_BOOST1;
|
2014-03-15 01:35:47 +04:00
|
|
|
}
|
|
|
|
|
2014-04-03 05:37:04 +04:00
|
|
|
// Compute delta-q for the segment.
|
2015-01-28 22:34:24 +03:00
|
|
|
static int compute_deltaq(const VP9_COMP *cpi, int q, double rate_factor) {
|
2014-04-03 05:37:04 +04:00
|
|
|
const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
|
|
|
const RATE_CONTROL *const rc = &cpi->rc;
|
|
|
|
int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type,
|
2015-01-28 22:34:24 +03:00
|
|
|
q, rate_factor,
|
2014-04-03 05:37:04 +04:00
|
|
|
cpi->common.bit_depth);
|
|
|
|
if ((-deltaq) > cr->max_qdelta_perc * q / 100) {
|
|
|
|
deltaq = -cr->max_qdelta_perc * q / 100;
|
|
|
|
}
|
|
|
|
return deltaq;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the just encoded frame, estimate the bits, incorporating the delta-q
|
2015-01-28 22:34:24 +03:00
|
|
|
// from non-base segment. For now ignore effect of multiple segments
|
|
|
|
// (with different delta-q). Note this function is called in the postencode
|
|
|
|
// (called from rc_update_rate_correction_factors()).
|
2014-04-03 05:37:04 +04:00
|
|
|
int vp9_cyclic_refresh_estimate_bits_at_q(const VP9_COMP *cpi,
|
|
|
|
double correction_factor) {
|
|
|
|
const VP9_COMMON *const cm = &cpi->common;
|
|
|
|
const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
|
|
|
int estimated_bits;
|
|
|
|
int mbs = cm->MBs;
|
|
|
|
int num8x8bl = mbs << 2;
|
2015-01-28 22:34:24 +03:00
|
|
|
// Weight for non-base segments: use actual number of blocks refreshed in
|
2014-04-03 05:37:04 +04:00
|
|
|
// previous/just encoded frame. Note number of blocks here is in 8x8 units.
|
|
|
|
double weight_segment = (double)cr->actual_num_seg_blocks / num8x8bl;
|
|
|
|
// Compute delta-q that was used in the just encoded frame.
|
2015-01-28 22:34:24 +03:00
|
|
|
int deltaq = compute_deltaq(cpi, cm->base_qindex, cr->rate_ratio_qdelta);
|
2014-04-03 05:37:04 +04:00
|
|
|
// Take segment weighted average for estimated bits.
|
|
|
|
estimated_bits = (int)((1.0 - weight_segment) *
|
|
|
|
vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex, mbs,
|
|
|
|
correction_factor, cm->bit_depth) +
|
|
|
|
weight_segment *
|
|
|
|
vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex + deltaq, mbs,
|
|
|
|
correction_factor, cm->bit_depth));
|
|
|
|
return estimated_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prior to encoding the frame, estimate the bits per mb, for a given q = i and
|
|
|
|
// a corresponding delta-q (for segment 1). This function is called in the
|
|
|
|
// rc_regulate_q() to set the base qp index.
|
2015-01-28 22:34:24 +03:00
|
|
|
// Note: the segment map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or
|
|
|
|
// to 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock, prior to encoding.
|
2014-04-03 05:37:04 +04:00
|
|
|
int vp9_cyclic_refresh_rc_bits_per_mb(const VP9_COMP *cpi, int i,
|
|
|
|
double correction_factor) {
|
|
|
|
const VP9_COMMON *const cm = &cpi->common;
|
|
|
|
CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
|
|
|
int bits_per_mb;
|
|
|
|
int num8x8bl = cm->MBs << 2;
|
|
|
|
// Weight for segment 1 prior to encoding: take the target number for the
|
|
|
|
// frame to be encoded. Number of blocks here is in 8x8 units.
|
|
|
|
// Note that this is called in rc_regulate_q, which is called before the
|
|
|
|
// cyclic_refresh_setup (which sets cr->target_num_seg_blocks). So a mismatch
|
|
|
|
// may occur between the cr->target_num_seg_blocks value here and the
|
|
|
|
// cr->target_num_seg_block set for encoding the frame. For the current use
|
|
|
|
// case of fixed cr->percent_refresh and cr->time_for_refresh = 0, mismatch
|
|
|
|
// does not occur/is very small.
|
|
|
|
double weight_segment = (double)cr->target_num_seg_blocks / num8x8bl;
|
|
|
|
// Compute delta-q corresponding to qindex i.
|
2015-01-28 22:34:24 +03:00
|
|
|
int deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta);
|
2014-04-03 05:37:04 +04:00
|
|
|
// Take segment weighted average for bits per mb.
|
|
|
|
bits_per_mb = (int)((1.0 - weight_segment) *
|
|
|
|
vp9_rc_bits_per_mb(cm->frame_type, i, correction_factor, cm->bit_depth) +
|
|
|
|
weight_segment *
|
|
|
|
vp9_rc_bits_per_mb(cm->frame_type, i + deltaq, correction_factor,
|
|
|
|
cm->bit_depth));
|
|
|
|
return bits_per_mb;
|
|
|
|
}
|
|
|
|
|
2014-03-15 01:35:47 +04:00
|
|
|
// Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
|
|
|
|
// check if we should reset the segment_id, and update the cyclic_refresh map
|
|
|
|
// and segmentation map.
|
2014-03-26 22:00:35 +04:00
|
|
|
void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi,
|
|
|
|
MB_MODE_INFO *const mbmi,
|
|
|
|
int mi_row, int mi_col,
|
2015-01-22 03:09:13 +03:00
|
|
|
BLOCK_SIZE bsize,
|
|
|
|
int64_t rate,
|
|
|
|
int64_t dist) {
|
2014-03-24 21:48:14 +04:00
|
|
|
const VP9_COMMON *const cm = &cpi->common;
|
2014-03-26 22:00:35 +04:00
|
|
|
CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
2014-03-15 01:35:47 +04:00
|
|
|
const int bw = num_8x8_blocks_wide_lookup[bsize];
|
|
|
|
const int bh = num_8x8_blocks_high_lookup[bsize];
|
|
|
|
const int xmis = MIN(cm->mi_cols - mi_col, bw);
|
|
|
|
const int ymis = MIN(cm->mi_rows - mi_row, bh);
|
|
|
|
const int block_index = mi_row * cm->mi_cols + mi_col;
|
2015-01-28 22:34:24 +03:00
|
|
|
const int refresh_this_block = candidate_refresh_aq(cr, mbmi, rate, dist,
|
|
|
|
bsize);
|
2014-03-15 01:35:47 +04:00
|
|
|
// Default is to not update the refresh map.
|
|
|
|
int new_map_value = cr->map[block_index];
|
|
|
|
int x = 0; int y = 0;
|
2014-03-24 21:48:14 +04:00
|
|
|
|
2015-01-28 22:34:24 +03:00
|
|
|
// If this block is labeled for refresh, check if we should reset the
|
|
|
|
// segment_id.
|
|
|
|
if (mbmi->segment_id != CR_SEGMENT_ID_BASE)
|
|
|
|
mbmi->segment_id = refresh_this_block;
|
2014-03-15 01:35:47 +04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// as clean. The magnitude of the -ve influences how long before we consider
|
|
|
|
// it for refresh again.
|
2015-01-28 22:34:24 +03:00
|
|
|
if (mbmi->segment_id != CR_SEGMENT_ID_BASE) {
|
2014-03-15 01:35:47 +04:00
|
|
|
new_map_value = -cr->time_for_refresh;
|
|
|
|
} else if (refresh_this_block) {
|
|
|
|
// Else if it is accepted as candidate for refresh, and has not already
|
|
|
|
// been refreshed (marked as 1) then mark it as a candidate for cleanup
|
|
|
|
// for future time (marked as 0), otherwise don't update it.
|
|
|
|
if (cr->map[block_index] == 1)
|
|
|
|
new_map_value = 0;
|
|
|
|
} else {
|
|
|
|
// Leave it marked as block that is not candidate for refresh.
|
|
|
|
new_map_value = 1;
|
|
|
|
}
|
2014-11-05 01:32:04 +03:00
|
|
|
|
2014-03-15 01:35:47 +04:00
|
|
|
// Update entries in the cyclic refresh map with new_map_value, and
|
|
|
|
// copy mbmi->segment_id into global segmentation map.
|
|
|
|
for (y = 0; y < ymis; y++)
|
|
|
|
for (x = 0; x < xmis; x++) {
|
|
|
|
cr->map[block_index + y * cm->mi_cols + x] = new_map_value;
|
|
|
|
cpi->segmentation_map[block_index + y * cm->mi_cols + x] =
|
2014-03-24 21:48:14 +04:00
|
|
|
mbmi->segment_id;
|
2014-03-15 01:35:47 +04:00
|
|
|
}
|
2014-04-03 05:37:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the actual number of blocks that were applied the segment delta q.
|
2015-03-06 04:26:52 +03:00
|
|
|
void vp9_cyclic_refresh_postencode(VP9_COMP *const cpi) {
|
2014-04-03 05:37:04 +04:00
|
|
|
VP9_COMMON *const cm = &cpi->common;
|
|
|
|
CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
|
|
|
unsigned char *const seg_map = cpi->segmentation_map;
|
|
|
|
int mi_row, mi_col;
|
|
|
|
cr->actual_num_seg_blocks = 0;
|
|
|
|
for (mi_row = 0; mi_row < cm->mi_rows; mi_row++)
|
|
|
|
for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
|
2015-01-28 22:34:24 +03:00
|
|
|
if (seg_map[mi_row * cm->mi_cols + mi_col] != CR_SEGMENT_ID_BASE)
|
2014-04-03 05:37:04 +04:00
|
|
|
cr->actual_num_seg_blocks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 04:26:52 +03:00
|
|
|
// Set golden frame update interval, for non-svc 1 pass CBR mode.
|
|
|
|
void vp9_cyclic_refresh_set_golden_update(VP9_COMP *const cpi) {
|
|
|
|
RATE_CONTROL *const rc = &cpi->rc;
|
|
|
|
CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
|
|
|
// Set minimum gf_interval for GF update to a multiple (== 2) of refresh
|
|
|
|
// period. Depending on past encoding stats, GF flag may be reset and update
|
|
|
|
// may not occur until next baseline_gf_interval.
|
|
|
|
if (cr->percent_refresh > 0)
|
|
|
|
rc->baseline_gf_interval = 2 * (100 / cr->percent_refresh);
|
|
|
|
else
|
|
|
|
rc->baseline_gf_interval = 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update some encoding stats (from the just encoded frame), and if the golden
|
|
|
|
// reference is to be updated check if we should NOT update the golden ref.
|
|
|
|
void vp9_cyclic_refresh_check_golden_update(VP9_COMP *const cpi) {
|
|
|
|
VP9_COMMON *const cm = &cpi->common;
|
|
|
|
CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
|
|
|
int mi_row, mi_col;
|
|
|
|
double fraction_low = 0.0;
|
|
|
|
int low_content_frame = 0;
|
|
|
|
for (mi_row = 0; mi_row < cm->mi_rows; mi_row++)
|
|
|
|
for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
|
|
|
|
if (cr->map[mi_row * cm->mi_cols + mi_col] < 1)
|
|
|
|
low_content_frame++;
|
|
|
|
}
|
|
|
|
fraction_low =
|
|
|
|
(double)low_content_frame / (cm->mi_rows * cm->mi_cols);
|
|
|
|
// Update average.
|
|
|
|
cr->low_content_avg = (fraction_low + 3 * cr->low_content_avg) / 4;
|
|
|
|
if (cpi->refresh_golden_frame == 1) {
|
|
|
|
// Don't update golden reference if the amount of low_content for the
|
|
|
|
// current encoded frame is small, or if the recursive average of the
|
|
|
|
// low_content over the update interval window falls below threshold.
|
|
|
|
if (fraction_low < 0.8 || cr->low_content_avg < 0.7)
|
|
|
|
cpi->refresh_golden_frame = 0;
|
|
|
|
// Reset for next internal.
|
|
|
|
cr->low_content_avg = fraction_low;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 05:37:04 +04:00
|
|
|
// Update the segmentation map, and related quantities: cyclic refresh map,
|
|
|
|
// refresh sb_index, and target number of blocks to be refreshed.
|
2015-01-28 22:34:24 +03:00
|
|
|
// The map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or to
|
|
|
|
// 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock.
|
|
|
|
// Blocks labeled as BOOST1 may later get set to BOOST2 (during the
|
|
|
|
// encoding of the superblock).
|
2014-04-03 05:37:04 +04:00
|
|
|
void vp9_cyclic_refresh_update_map(VP9_COMP *const cpi) {
|
|
|
|
VP9_COMMON *const cm = &cpi->common;
|
|
|
|
CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
|
|
|
unsigned char *const seg_map = cpi->segmentation_map;
|
|
|
|
int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame;
|
|
|
|
int xmis, ymis, x, y;
|
2015-01-28 22:34:24 +03:00
|
|
|
vpx_memset(seg_map, CR_SEGMENT_ID_BASE, cm->mi_rows * cm->mi_cols);
|
2014-04-03 05:37:04 +04:00
|
|
|
sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
|
|
|
|
sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
|
|
|
|
sbs_in_frame = sb_cols * sb_rows;
|
|
|
|
// Number of target blocks to get the q delta (segment 1).
|
|
|
|
block_count = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100;
|
|
|
|
// Set the segmentation map: cycle through the superblocks, starting at
|
|
|
|
// cr->mb_index, and stopping when either block_count blocks have been found
|
|
|
|
// to be refreshed, or we have passed through whole frame.
|
|
|
|
assert(cr->sb_index < sbs_in_frame);
|
|
|
|
i = cr->sb_index;
|
|
|
|
cr->target_num_seg_blocks = 0;
|
|
|
|
do {
|
|
|
|
int sum_map = 0;
|
|
|
|
// Get the mi_row/mi_col corresponding to superblock index i.
|
|
|
|
int sb_row_index = (i / sb_cols);
|
|
|
|
int sb_col_index = i - sb_row_index * sb_cols;
|
|
|
|
int mi_row = sb_row_index * MI_BLOCK_SIZE;
|
|
|
|
int mi_col = sb_col_index * MI_BLOCK_SIZE;
|
|
|
|
assert(mi_row >= 0 && mi_row < cm->mi_rows);
|
|
|
|
assert(mi_col >= 0 && mi_col < cm->mi_cols);
|
|
|
|
bl_index = mi_row * cm->mi_cols + mi_col;
|
|
|
|
// Loop through all 8x8 blocks in superblock and update map.
|
|
|
|
xmis = MIN(cm->mi_cols - mi_col,
|
|
|
|
num_8x8_blocks_wide_lookup[BLOCK_64X64]);
|
|
|
|
ymis = MIN(cm->mi_rows - mi_row,
|
|
|
|
num_8x8_blocks_high_lookup[BLOCK_64X64]);
|
|
|
|
for (y = 0; y < ymis; y++) {
|
|
|
|
for (x = 0; x < xmis; x++) {
|
|
|
|
const int bl_index2 = bl_index + y * cm->mi_cols + x;
|
|
|
|
// If the block is as a candidate for clean up then mark it
|
|
|
|
// for possible boost/refresh (segment 1). The segment id may get
|
|
|
|
// reset to 0 later if block gets coded anything other than ZEROMV.
|
|
|
|
if (cr->map[bl_index2] == 0) {
|
|
|
|
sum_map++;
|
|
|
|
} else if (cr->map[bl_index2] < 0) {
|
|
|
|
cr->map[bl_index2]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Enforce constant segment over superblock.
|
|
|
|
// If segment is at least half of superblock, set to 1.
|
|
|
|
if (sum_map >= xmis * ymis / 2) {
|
|
|
|
for (y = 0; y < ymis; y++)
|
|
|
|
for (x = 0; x < xmis; x++) {
|
2015-01-28 22:34:24 +03:00
|
|
|
seg_map[bl_index + y * cm->mi_cols + x] = CR_SEGMENT_ID_BOOST1;
|
2014-04-03 05:37:04 +04:00
|
|
|
}
|
|
|
|
cr->target_num_seg_blocks += xmis * ymis;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
if (i == sbs_in_frame) {
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
} while (cr->target_num_seg_blocks < block_count && i != cr->sb_index);
|
|
|
|
cr->sb_index = i;
|
2014-03-15 01:35:47 +04:00
|
|
|
}
|
|
|
|
|
2014-12-03 21:19:54 +03:00
|
|
|
// Set/update global/frame level cyclic refresh parameters.
|
|
|
|
void vp9_cyclic_refresh_update_parameters(VP9_COMP *const cpi) {
|
|
|
|
const RATE_CONTROL *const rc = &cpi->rc;
|
|
|
|
CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
|
|
|
cr->percent_refresh = 10;
|
|
|
|
// Use larger delta-qp (increase rate_ratio_qdelta) for first few (~4)
|
|
|
|
// periods of the refresh cycle, after a key frame. This corresponds to ~40
|
|
|
|
// frames with cr->percent_refresh = 10.
|
|
|
|
if (rc->frames_since_key < 40)
|
|
|
|
cr->rate_ratio_qdelta = 3.0;
|
|
|
|
else
|
|
|
|
cr->rate_ratio_qdelta = 2.0;
|
|
|
|
}
|
|
|
|
|
2014-03-15 01:35:47 +04:00
|
|
|
// Setup cyclic background refresh: set delta q and segmentation map.
|
2014-03-26 22:00:35 +04:00
|
|
|
void vp9_cyclic_refresh_setup(VP9_COMP *const cpi) {
|
2014-03-15 01:35:47 +04:00
|
|
|
VP9_COMMON *const cm = &cpi->common;
|
2014-03-24 21:48:14 +04:00
|
|
|
const RATE_CONTROL *const rc = &cpi->rc;
|
2014-03-26 22:00:35 +04:00
|
|
|
CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
|
2014-03-15 01:35:47 +04:00
|
|
|
struct segmentation *const seg = &cm->seg;
|
2014-03-24 21:48:14 +04:00
|
|
|
const int apply_cyclic_refresh = apply_cyclic_refresh_bitrate(cm, rc);
|
2015-03-06 04:26:52 +03:00
|
|
|
if (cm->current_video_frame == 0)
|
|
|
|
cr->low_content_avg = 0.0;
|
2014-03-15 01:35:47 +04:00
|
|
|
// Don't apply refresh on key frame or enhancement layer frames.
|
|
|
|
if (!apply_cyclic_refresh ||
|
2014-03-24 21:48:14 +04:00
|
|
|
(cm->frame_type == KEY_FRAME) ||
|
2014-11-20 02:31:29 +03:00
|
|
|
(cpi->svc.temporal_layer_id > 0) ||
|
|
|
|
(cpi->svc.spatial_layer_id > 0)) {
|
2014-03-15 01:35:47 +04:00
|
|
|
// Set segmentation map to 0 and disable.
|
2014-04-03 05:37:04 +04:00
|
|
|
unsigned char *const seg_map = cpi->segmentation_map;
|
2014-03-15 01:35:47 +04:00
|
|
|
vpx_memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
|
|
|
|
vp9_disable_segmentation(&cm->seg);
|
2014-03-24 21:48:14 +04:00
|
|
|
if (cm->frame_type == KEY_FRAME)
|
2014-03-27 05:09:20 +04:00
|
|
|
cr->sb_index = 0;
|
2014-03-15 01:35:47 +04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
int qindex_delta = 0;
|
2014-04-03 05:37:04 +04:00
|
|
|
int qindex2;
|
2014-09-03 03:34:09 +04:00
|
|
|
const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth);
|
2014-03-15 01:35:47 +04:00
|
|
|
vp9_clear_system_state();
|
|
|
|
cr->max_qdelta_perc = 50;
|
2014-04-03 05:37:04 +04:00
|
|
|
cr->time_for_refresh = 0;
|
2015-01-28 22:34:24 +03:00
|
|
|
// Set rate threshold to some multiple (set to 2 for now) of the target
|
2015-01-22 03:09:13 +03:00
|
|
|
// rate (target is given by sb64_target_rate and scaled by 256).
|
2015-02-27 01:27:16 +03:00
|
|
|
cr->thresh_rate_sb = ((int64_t)(rc->sb64_target_rate) << 8) << 1;
|
2014-03-15 01:35:47 +04:00
|
|
|
// Distortion threshold, quadratic in Q, scale factor to be adjusted.
|
2015-02-27 01:27:16 +03:00
|
|
|
// q will not exceed 457, so (q * q) is within 32bit; see:
|
|
|
|
// vp9_convert_qindex_to_q(), vp9_ac_quant(), ac_qlookup*[].
|
|
|
|
cr->thresh_dist_sb = ((int64_t)(q * q)) << 2;
|
2015-01-22 03:09:13 +03:00
|
|
|
cr->motion_thresh = 32;
|
2014-03-15 01:35:47 +04:00
|
|
|
// Set up segmentation.
|
|
|
|
// Clear down the segment map.
|
|
|
|
vp9_enable_segmentation(&cm->seg);
|
|
|
|
vp9_clearall_segfeatures(seg);
|
|
|
|
// Select delta coding method.
|
|
|
|
seg->abs_delta = SEGMENT_DELTADATA;
|
|
|
|
|
|
|
|
// Note: setting temporal_update has no effect, as the seg-map coding method
|
|
|
|
// (temporal or spatial) is determined in vp9_choose_segmap_coding_method(),
|
|
|
|
// based on the coding cost of each method. For error_resilient mode on the
|
|
|
|
// last_frame_seg_map is set to 0, so if temporal coding is used, it is
|
|
|
|
// relative to 0 previous map.
|
|
|
|
// seg->temporal_update = 0;
|
|
|
|
|
2015-01-28 22:34:24 +03:00
|
|
|
// Segment BASE "Q" feature is disabled so it defaults to the baseline Q.
|
|
|
|
vp9_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q);
|
|
|
|
// Use segment BOOST1 for in-frame Q adjustment.
|
|
|
|
vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q);
|
|
|
|
// Use segment BOOST2 for more aggressive in-frame Q adjustment.
|
|
|
|
vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q);
|
2014-03-15 01:35:47 +04:00
|
|
|
|
2015-01-28 22:34:24 +03:00
|
|
|
// Set the q delta for segment BOOST1.
|
|
|
|
qindex_delta = compute_deltaq(cpi, cm->base_qindex, cr->rate_ratio_qdelta);
|
2014-03-15 01:35:47 +04:00
|
|
|
|
2015-01-28 22:34:24 +03:00
|
|
|
// Compute rd-mult for segment BOOST1.
|
2014-03-15 01:35:47 +04:00
|
|
|
qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ);
|
|
|
|
cr->rdmult = vp9_compute_rd_mult(cpi, qindex2);
|
|
|
|
|
2015-01-28 22:34:24 +03:00
|
|
|
vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q, qindex_delta);
|
|
|
|
|
|
|
|
// Set a more aggressive (higher) q delta for segment BOOST2.
|
|
|
|
qindex_delta = compute_deltaq(cpi, cm->base_qindex,
|
|
|
|
MIN(CR_MAX_RATE_TARGET_RATIO,
|
|
|
|
CR_BOOST2_FAC * cr->rate_ratio_qdelta));
|
|
|
|
vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta);
|
2014-03-27 05:09:20 +04:00
|
|
|
|
2014-04-03 05:37:04 +04:00
|
|
|
// Update the segmentation and refresh map.
|
|
|
|
vp9_cyclic_refresh_update_map(cpi);
|
2014-03-15 01:35:47 +04:00
|
|
|
}
|
|
|
|
}
|
2014-03-26 22:00:35 +04:00
|
|
|
|
|
|
|
int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) {
|
|
|
|
return cr->rdmult;
|
|
|
|
}
|