/* * Copyright (c) 2016, Alliance for Open Media. All rights reserved. * * This source code is subject to the terms of the BSD 2 Clause License and * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License * was not distributed with this source code in the LICENSE file, you can * obtain it at www.aomedia.org/license/software. If the Alliance for Open * Media Patent License 1.0 was not distributed with this source code in the * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
// Hyper-parameters for dropout optimization, based on following logics. // TODO(yjshen): These settings are tuned by experiments. They may still be // optimized for better performance. // (1) Coefficients which are large enough will ALWAYS be kept. staticconst tran_low_t DROPOUT_COEFF_MAX = 2; // Max dropout-able coefficient. // (2) Continuous coefficients will ALWAYS be kept. Here rigorous continuity is // NOT required. For example, `5 0 0 0 7` is treated as two continuous // coefficients if three zeros do not fulfill the dropout condition. staticconstint DROPOUT_CONTINUITY_MAX =
2; // Max dropout-able continuous coeff. // (3) Dropout operation is NOT applicable to blocks with large or small // quantization index. staticconstint DROPOUT_Q_MAX = 128; staticconstint DROPOUT_Q_MIN = 16; // (4) Recall that dropout optimization will forcibly set some quantized // coefficients to zero. The key logic on determining whether a coefficient // should be dropped is to check the number of continuous zeros before AND // after this coefficient. The exact number of zeros for judgement depends // on block size and quantization index. More concretely, block size // determines the base number of zeros, while quantization index determines // the multiplier. Intuitively, larger block requires more zeros and larger // quantization index also requires more zeros (more information is lost // when using larger quantization index). staticconstint DROPOUT_BEFORE_BASE_MAX =
32; // Max base number for leading zeros. staticconstint DROPOUT_BEFORE_BASE_MIN =
16; // Min base number for leading zeros. staticconstint DROPOUT_AFTER_BASE_MAX =
32; // Max base number for trailing zeros. staticconstint DROPOUT_AFTER_BASE_MIN =
16; // Min base number for trailing zeros. staticconstint DROPOUT_MULTIPLIER_MAX =
8; // Max multiplier on number of zeros. staticconstint DROPOUT_MULTIPLIER_MIN =
2; // Min multiplier on number of zeros. staticconstint DROPOUT_MULTIPLIER_Q_BASE =
32; // Base Q to compute multiplier.
void av1_dropout_qcoeff(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
TX_TYPE tx_type, int qindex) { constint tx_width = tx_size_wide[tx_size]; constint tx_height = tx_size_high[tx_size];
// Early return if `qindex` is out of range. if (qindex > DROPOUT_Q_MAX || qindex < DROPOUT_Q_MIN) { return;
}
// Compute number of zeros used for dropout judgement. constint base_size = AOMMAX(tx_width, tx_height); constint multiplier = CLIP(qindex / DROPOUT_MULTIPLIER_Q_BASE,
DROPOUT_MULTIPLIER_MIN, DROPOUT_MULTIPLIER_MAX); constint dropout_num_before =
multiplier *
CLIP(base_size, DROPOUT_BEFORE_BASE_MIN, DROPOUT_BEFORE_BASE_MAX); constint dropout_num_after =
multiplier *
CLIP(base_size, DROPOUT_AFTER_BASE_MIN, DROPOUT_AFTER_BASE_MAX);
void av1_dropout_qcoeff_num(MACROBLOCK *mb, int plane, int block,
TX_SIZE tx_size, TX_TYPE tx_type, int dropout_num_before, int dropout_num_after) { conststruct macroblock_plane *const p = &mb->plane[plane];
tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block);
tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block); constint max_eob = av1_get_max_eob(tx_size); const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
// Early return if there are not enough non-zero coefficients. if (p->eobs[block] == 0 || p->eobs[block] <= dropout_num_before ||
max_eob <= dropout_num_before + dropout_num_after) { return;
}
int count_zeros_before = 0; int count_zeros_after = 0; int count_nonzeros = 0; // Index of the first non-zero coefficient after sufficient number of // continuous zeros. If equals to `-1`, it means number of leading zeros // hasn't reach `dropout_num_before`. int idx = -1; int eob = 0; // New end of block.
for (int i = 0; i < p->eobs[block]; ++i) { constint scan_idx = scan_order->scan[i]; if (abs(qcoeff[scan_idx]) > DROPOUT_COEFF_MAX) { // Keep large coefficients.
count_zeros_before = 0;
count_zeros_after = 0;
idx = -1;
eob = i + 1;
} elseif (qcoeff[scan_idx] == 0) { // Count zeros. if (idx == -1) {
++count_zeros_before;
} else {
++count_zeros_after;
}
} else { // Count non-zeros. if (count_zeros_before >= dropout_num_before) {
idx = (idx == -1) ? i : idx;
++count_nonzeros;
} else {
count_zeros_before = 0;
eob = i + 1;
}
}
// Handle the trailing zeros after original end of block. if (idx != -1 && i == p->eobs[block] - 1) {
count_zeros_after += (max_eob - p->eobs[block]);
}
// Set redundant coefficients to zeros if needed. if (count_zeros_after >= dropout_num_after) { for (int j = idx; j <= i; ++j) {
qcoeff[scan_order->scan[j]] = 0;
dqcoeff[scan_order->scan[j]] = 0;
}
count_zeros_before += (i - idx + 1);
count_zeros_after = 0;
count_nonzeros = 0;
} elseif (i == p->eobs[block] - 1) {
eob = i + 1;
}
}
// Settings for optimization type. NOTE: To set optimization type for all intra // frames, both `KEY_BLOCK_OPT_TYPE` and `INTRA_BLOCK_OPT_TYPE` should be set. // TODO(yjshen): These settings are hard-coded and look okay for now. They // should be made configurable later. // Blocks of key frames ONLY. staticconst OPT_TYPE KEY_BLOCK_OPT_TYPE = TRELLIS_DROPOUT_OPT; // Blocks of intra frames (key frames EXCLUSIVE). staticconst OPT_TYPE INTRA_BLOCK_OPT_TYPE = TRELLIS_DROPOUT_OPT; // Blocks of inter frames. (NOTE: Dropout optimization is DISABLED by default // if trellis optimization is on for inter frames.) staticconst OPT_TYPE INTER_BLOCK_OPT_TYPE = TRELLIS_DROPOUT_OPT;
txfm_param->bd = xd->bd;
txfm_param->is_hbd = is_cur_buf_hbd(xd);
} void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx, int use_quant_b_adapt, QUANT_PARAM *qparam) {
qparam->log_scale = av1_get_tx_scale(tx_size);
qparam->tx_size = tx_size;
qparam->use_quant_b_adapt = use_quant_b_adapt;
// TODO(bohanli): optimize_b and quantization idx has relationship, // but is kind of buried and complicated in different encoding stages. // Should have a unified function to derive quant_idx, rather than // determine and pass in the quant_idx
qparam->use_optimize_b = use_optimize_b;
qparam->xform_quant_idx = xform_quant_idx;
av1_set_txb_context(x, plane, block, tx_size, a, l);
if (p->eobs[block]) { // As long as any YUV plane has non-zero quantized transform coefficients, // mbmi->skip_txfm flag is set to 0.
mbmi->skip_txfm = 0;
av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
pd->dst.stride, p->eobs[block],
cm->features.reduced_tx_set_used);
} else { // Only when YUV planes all have zero quantized transform coefficients, // mbmi->skip_txfm flag is set to 1.
mbmi->skip_txfm &= 1;
}
// TODO(debargha, jingning): Temporarily disable txk_type check for eob=0 // case. It is possible that certain collision in hash index would cause // the assertion failure. To further optimize the rate-distortion // performance, we need to re-visit this part and enable this assert // again. if (p->eobs[block] == 0 && plane == 0) { #if 0 if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ &&
args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) { // TODO(jingning,angiebird,huisu@google.com): enable txk_check when // enable_optimize_b is true to detect potential RD bug. const uint8_t disable_txk_check = args->enable_optimize_b; if (!disable_txk_check) {
assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
DCT_DCT);
}
} #endif
update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
}
void av1_foreach_transformed_block_in_plane( const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
foreach_transformed_block_visitor visit, void *arg) { conststruct macroblockd_plane *const pd = &xd->plane[plane]; // block and transform sizes, in number of 4x4 blocks log 2 ("*_b") // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8 // transform size varies per plane, look it up in a common way. const TX_SIZE tx_size = av1_get_tx_size(plane, xd); const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size]; // Call visit() directly with zero offsets if the current block size is the // same as the transform block size. if (plane_bsize == tx_bsize) {
visit(plane, 0, 0, 0, plane_bsize, tx_size, arg); return;
} const uint8_t txw_unit = tx_size_wide_unit[tx_size]; const uint8_t txh_unit = tx_size_high_unit[tx_size]; constint step = txw_unit * txh_unit;
// 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 = max_block_wide(xd, plane_bsize, plane); constint max_blocks_high = max_block_high(xd, plane_bsize, plane); const BLOCK_SIZE max_unit_bsize =
get_plane_block_size(BLOCK_64X64, pd->subsampling_x, pd->subsampling_y); constint mu_blocks_wide =
AOMMIN(mi_size_wide[max_unit_bsize], max_blocks_wide); constint mu_blocks_high =
AOMMIN(mi_size_high[max_unit_bsize], max_blocks_high);
// Keep track of the row and column of the blocks we use so that we know // if we are in the unrestricted motion border. int i = 0; for (int r = 0; r < max_blocks_high; r += mu_blocks_high) { constint unit_height = AOMMIN(mu_blocks_high + r, max_blocks_high); // Skip visiting the sub blocks that are wholly within the UMV. for (int c = 0; c < max_blocks_wide; c += mu_blocks_wide) { constint unit_width = AOMMIN(mu_blocks_wide + c, max_blocks_wide); for (int blk_row = r; blk_row < unit_height; blk_row += txh_unit) { for (int blk_col = c; blk_col < unit_width; blk_col += txw_unit) {
visit(plane, i, blk_row, blk_col, plane_bsize, tx_size, arg);
i += step;
}
}
}
} // Check if visit() is invoked at least once.
assert(i >= 1);
}
void av1_encode_sb(conststruct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
RUN_TYPE dry_run) {
assert(bsize < BLOCK_SIZES_ALL);
MACROBLOCKD *const xd = &x->e_mbd;
MB_MODE_INFO *mbmi = xd->mi[0]; // In the current encoder implementation, for inter blocks, // only when YUV planes all have zero quantized transform coefficients, // mbmi->skip_txfm flag is set to 1. // For intra blocks, this flag is set to 0 since skipped blocks are so rare // that transmitting skip_txfm = 1 is very expensive. // mbmi->skip_txfm is init to 1, and will be modified in encode_block() based // on transform, quantization, and (if exists) trellis optimization.
mbmi->skip_txfm = 1; if (x->txfm_search_info.skip_txfm) return;
// TODO(jingning): Temporarily disable txk_type check for eob=0 case. // It is possible that certain collision in hash index would cause // the assertion failure. To further optimize the rate-distortion // performance, we need to re-visit this part and enable this assert // again. if (*eob == 0 && plane == 0) { #if 0 if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ
&& args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) {
assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
DCT_DCT);
} #endif
update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
}
// For intra mode, skipped blocks are so rare that transmitting // skip_txfm = 1 is very expensive.
mbmi->skip_txfm = 0;
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.