2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
14 #include "vp9/encoder/vp9_aq_variance.h"
15 #include "vp9/encoder/vp9_encodeframe.h"
16 #include "vp9/common/vp9_seg_common.h"
17 #include "vp9/encoder/vp9_segmentation.h"
19 #define AQ_C_SEGMENTS 3
20 #define AQ_C_STRENGTHS 3
21 static const int aq_c_active_segments[AQ_C_STRENGTHS] = {1, 2, 3};
22 static const double aq_c_q_adj_factor[AQ_C_STRENGTHS][AQ_C_SEGMENTS] =
23 {{1.0, 1.0, 1.0}, {1.0, 2.0, 1.0}, {1.0, 1.5, 2.5}};
24 static const double aq_c_transitions[AQ_C_STRENGTHS][AQ_C_SEGMENTS] =
25 {{1.0, 1.0, 1.0}, {1.0, 0.25, 0.0}, {1.0, 0.5, 0.25}};
26 static const double aq_c_var_thresholds[AQ_C_SEGMENTS] = {100.0, -1.0, -2.0};
28 static int get_aq_c_strength(int q_index, vpx_bit_depth_t bit_depth) {
29 // Approximate base quatizer (truncated to int)
30 const int base_quant = vp9_ac_quant(q_index, 0, bit_depth) / 4;
31 return (base_quant > 20) + (base_quant > 45);
34 void vp9_setup_in_frame_q_adj(VP9_COMP *cpi) {
35 VP9_COMMON *const cm = &cpi->common;
36 struct segmentation *const seg = &cm->seg;
38 // Make SURE use of floating point in this function is safe.
39 vp9_clear_system_state();
41 if (cm->frame_type == KEY_FRAME ||
42 cpi->refresh_alt_ref_frame ||
43 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
45 const int aq_strength = get_aq_c_strength(cm->base_qindex, cm->bit_depth);
46 const int active_segments = aq_c_active_segments[aq_strength];
48 // Clear down the segment map.
49 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
51 // Clear down the complexity map used for rd.
52 vpx_memset(cpi->complexity_map, 0, cm->mi_rows * cm->mi_cols);
54 vp9_clearall_segfeatures(seg);
56 // Segmentation only makes sense if the target bits per SB is above a
57 // threshold. Below this the overheads will usually outweigh any benefit.
58 if (cpi->rc.sb64_target_rate < 256) {
59 vp9_disable_segmentation(seg);
63 vp9_enable_segmentation(seg);
65 // Select delta coding method.
66 seg->abs_delta = SEGMENT_DELTADATA;
68 // Segment 0 "Q" feature is disabled so it defaults to the baseline Q.
69 vp9_disable_segfeature(seg, 0, SEG_LVL_ALT_Q);
71 // Use some of the segments for in frame Q adjustment.
72 for (segment = 1; segment < active_segments; ++segment) {
74 vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, cm->base_qindex,
75 aq_c_q_adj_factor[aq_strength][segment],
78 // For AQ complexity mode, we dont allow Q0 in a segment if the base
79 // Q is not 0. Q0 (lossless) implies 4x4 only and in AQ mode 2 a segment
80 // Q delta is sometimes applied without going back around the rd loop.
81 // This could lead to an illegal combination of partition size and q.
82 if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
83 qindex_delta = -cm->base_qindex + 1;
85 if ((cm->base_qindex + qindex_delta) > 0) {
86 vp9_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
87 vp9_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
93 #define DEFAULT_LV_THRESH 10.0
95 // Select a segment for the current SB64 block.
96 // The choice of segment for a block depends on the ratio of the projected
97 // bits for the block vs a target average.
98 // An "aq_strength" value determines how many segments are supported,
99 // the set of transition points to use and the extent of the quantizer
100 // adjustment for each segment (configured in vp9_setup_in_frame_q_adj()).
101 void vp9_select_in_frame_q_segment(VP9_COMP *cpi, MACROBLOCK *mb,
103 int mi_row, int mi_col,
104 int output_enabled, int projected_rate) {
105 VP9_COMMON *const cm = &cpi->common;
107 const int mi_offset = mi_row * cm->mi_cols + mi_col;
108 const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
109 const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
110 const int xmis = MIN(cm->mi_cols - mi_col, bw);
111 const int ymis = MIN(cm->mi_rows - mi_row, bh);
112 int complexity_metric = 64;
115 unsigned char segment;
117 if (!output_enabled) {
120 // Rate depends on fraction of a SB64 in frame (xmis * ymis / bw * bh).
121 // It is converted to bits * 256 units.
122 const int target_rate = (cpi->rc.sb64_target_rate * xmis * ymis * 256) /
124 const int aq_strength = get_aq_c_strength(cm->base_qindex, cm->bit_depth);
125 const int active_segments = aq_c_active_segments[aq_strength];
127 double low_var_thresh;
129 vp9_clear_system_state();
131 (cpi->oxcf.pass == 2) ? cpi->twopass.mb_av_energy : DEFAULT_LV_THRESH;
133 vp9_setup_src_planes(mb, cpi->Source, mi_row, mi_col);
134 logvar = vp9_log_block_var(cpi, mb, bs);
136 // The number of segments considered and the transition points used to
137 // select them is determined by the "aq_strength" value.
138 // Currently this loop only supports segments that reduce Q (i.e. where
139 // there is undershoot.
140 // The loop counts down towards segment 0 which is the default segment
141 // with no Q adjustment.
142 segment = active_segments - 1;
143 while (segment > 0) {
144 if ((projected_rate <
145 target_rate * aq_c_transitions[aq_strength][segment]) &&
146 (logvar < (low_var_thresh + aq_c_var_thresholds[segment]))) {
152 if (target_rate > 0) {
154 clamp((int)((projected_rate * 64) / target_rate), 16, 255);
158 // Fill in the entires in the segment map corresponding to this SB64.
159 for (y = 0; y < ymis; y++) {
160 for (x = 0; x < xmis; x++) {
161 cpi->segmentation_map[mi_offset + y * cm->mi_cols + x] = segment;
162 cpi->complexity_map[mi_offset + y * cm->mi_cols + x] =
163 (unsigned char)complexity_metric;