1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
14*77c1e3ccSAndroid Build Coastguard Worker
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_complexity.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/aq_variance.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/seg_common.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/segmentation.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
21*77c1e3ccSAndroid Build Coastguard Worker
22*77c1e3ccSAndroid Build Coastguard Worker #define AQ_C_SEGMENTS 5
23*77c1e3ccSAndroid Build Coastguard Worker #define DEFAULT_AQ2_SEG 3 // Neutral Q segment
24*77c1e3ccSAndroid Build Coastguard Worker #define AQ_C_STRENGTHS 3
25*77c1e3ccSAndroid Build Coastguard Worker static const double aq_c_q_adj_factor[AQ_C_STRENGTHS][AQ_C_SEGMENTS] = {
26*77c1e3ccSAndroid Build Coastguard Worker { 1.75, 1.25, 1.05, 1.00, 0.90 },
27*77c1e3ccSAndroid Build Coastguard Worker { 2.00, 1.50, 1.15, 1.00, 0.85 },
28*77c1e3ccSAndroid Build Coastguard Worker { 2.50, 1.75, 1.25, 1.00, 0.80 }
29*77c1e3ccSAndroid Build Coastguard Worker };
30*77c1e3ccSAndroid Build Coastguard Worker static const double aq_c_transitions[AQ_C_STRENGTHS][AQ_C_SEGMENTS] = {
31*77c1e3ccSAndroid Build Coastguard Worker { 0.15, 0.30, 0.55, 2.00, 100.0 },
32*77c1e3ccSAndroid Build Coastguard Worker { 0.20, 0.40, 0.65, 2.00, 100.0 },
33*77c1e3ccSAndroid Build Coastguard Worker { 0.25, 0.50, 0.75, 2.00, 100.0 }
34*77c1e3ccSAndroid Build Coastguard Worker };
35*77c1e3ccSAndroid Build Coastguard Worker static const double aq_c_var_thresholds[AQ_C_STRENGTHS][AQ_C_SEGMENTS] = {
36*77c1e3ccSAndroid Build Coastguard Worker { -4.0, -3.0, -2.0, 100.00, 100.0 },
37*77c1e3ccSAndroid Build Coastguard Worker { -3.5, -2.5, -1.5, 100.00, 100.0 },
38*77c1e3ccSAndroid Build Coastguard Worker { -3.0, -2.0, -1.0, 100.00, 100.0 }
39*77c1e3ccSAndroid Build Coastguard Worker };
40*77c1e3ccSAndroid Build Coastguard Worker
get_aq_c_strength(int q_index,aom_bit_depth_t bit_depth)41*77c1e3ccSAndroid Build Coastguard Worker static int get_aq_c_strength(int q_index, aom_bit_depth_t bit_depth) {
42*77c1e3ccSAndroid Build Coastguard Worker // Approximate base quatizer (truncated to int)
43*77c1e3ccSAndroid Build Coastguard Worker const int base_quant = av1_ac_quant_QTX(q_index, 0, bit_depth) / 4;
44*77c1e3ccSAndroid Build Coastguard Worker return (base_quant > 10) + (base_quant > 25);
45*77c1e3ccSAndroid Build Coastguard Worker }
46*77c1e3ccSAndroid Build Coastguard Worker
is_frame_aq_enabled(const AV1_COMP * const cpi)47*77c1e3ccSAndroid Build Coastguard Worker static bool is_frame_aq_enabled(const AV1_COMP *const cpi) {
48*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
49*77c1e3ccSAndroid Build Coastguard Worker const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
50*77c1e3ccSAndroid Build Coastguard Worker
51*77c1e3ccSAndroid Build Coastguard Worker return frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
52*77c1e3ccSAndroid Build Coastguard Worker refresh_frame->alt_ref_frame ||
53*77c1e3ccSAndroid Build Coastguard Worker (refresh_frame->golden_frame && !cpi->rc.is_src_frame_alt_ref);
54*77c1e3ccSAndroid Build Coastguard Worker }
55*77c1e3ccSAndroid Build Coastguard Worker
56*77c1e3ccSAndroid Build Coastguard Worker // Segmentation only makes sense if the target bits per SB is above a threshold.
57*77c1e3ccSAndroid Build Coastguard Worker // Below this the overheads will usually outweigh any benefit.
is_sb_aq_enabled(const AV1_COMP * const cpi)58*77c1e3ccSAndroid Build Coastguard Worker static bool is_sb_aq_enabled(const AV1_COMP *const cpi) {
59*77c1e3ccSAndroid Build Coastguard Worker return cpi->rc.sb64_target_rate >= 256;
60*77c1e3ccSAndroid Build Coastguard Worker }
61*77c1e3ccSAndroid Build Coastguard Worker
av1_setup_in_frame_q_adj(AV1_COMP * cpi)62*77c1e3ccSAndroid Build Coastguard Worker void av1_setup_in_frame_q_adj(AV1_COMP *cpi) {
63*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
64*77c1e3ccSAndroid Build Coastguard Worker const int base_qindex = cm->quant_params.base_qindex;
65*77c1e3ccSAndroid Build Coastguard Worker struct segmentation *const seg = &cm->seg;
66*77c1e3ccSAndroid Build Coastguard Worker const int resolution_change =
67*77c1e3ccSAndroid Build Coastguard Worker cm->prev_frame && (cm->width != cm->prev_frame->width ||
68*77c1e3ccSAndroid Build Coastguard Worker cm->height != cm->prev_frame->height);
69*77c1e3ccSAndroid Build Coastguard Worker
70*77c1e3ccSAndroid Build Coastguard Worker // Make SURE use of floating point in this function is safe.
71*77c1e3ccSAndroid Build Coastguard Worker
72*77c1e3ccSAndroid Build Coastguard Worker if (resolution_change) {
73*77c1e3ccSAndroid Build Coastguard Worker memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
74*77c1e3ccSAndroid Build Coastguard Worker av1_clearall_segfeatures(seg);
75*77c1e3ccSAndroid Build Coastguard Worker av1_disable_segmentation(seg);
76*77c1e3ccSAndroid Build Coastguard Worker return;
77*77c1e3ccSAndroid Build Coastguard Worker }
78*77c1e3ccSAndroid Build Coastguard Worker
79*77c1e3ccSAndroid Build Coastguard Worker if (is_frame_aq_enabled(cpi)) {
80*77c1e3ccSAndroid Build Coastguard Worker int segment;
81*77c1e3ccSAndroid Build Coastguard Worker const int aq_strength =
82*77c1e3ccSAndroid Build Coastguard Worker get_aq_c_strength(base_qindex, cm->seq_params->bit_depth);
83*77c1e3ccSAndroid Build Coastguard Worker
84*77c1e3ccSAndroid Build Coastguard Worker // Clear down the segment map.
85*77c1e3ccSAndroid Build Coastguard Worker memset(cpi->enc_seg.map, DEFAULT_AQ2_SEG,
86*77c1e3ccSAndroid Build Coastguard Worker cm->mi_params.mi_rows * cm->mi_params.mi_cols);
87*77c1e3ccSAndroid Build Coastguard Worker
88*77c1e3ccSAndroid Build Coastguard Worker av1_clearall_segfeatures(seg);
89*77c1e3ccSAndroid Build Coastguard Worker
90*77c1e3ccSAndroid Build Coastguard Worker if (!is_sb_aq_enabled(cpi)) {
91*77c1e3ccSAndroid Build Coastguard Worker av1_disable_segmentation(seg);
92*77c1e3ccSAndroid Build Coastguard Worker return;
93*77c1e3ccSAndroid Build Coastguard Worker }
94*77c1e3ccSAndroid Build Coastguard Worker
95*77c1e3ccSAndroid Build Coastguard Worker av1_enable_segmentation(seg);
96*77c1e3ccSAndroid Build Coastguard Worker
97*77c1e3ccSAndroid Build Coastguard Worker // Default segment "Q" feature is disabled so it defaults to the baseline Q.
98*77c1e3ccSAndroid Build Coastguard Worker av1_disable_segfeature(seg, DEFAULT_AQ2_SEG, SEG_LVL_ALT_Q);
99*77c1e3ccSAndroid Build Coastguard Worker
100*77c1e3ccSAndroid Build Coastguard Worker // Use some of the segments for in frame Q adjustment.
101*77c1e3ccSAndroid Build Coastguard Worker for (segment = 0; segment < AQ_C_SEGMENTS; ++segment) {
102*77c1e3ccSAndroid Build Coastguard Worker int qindex_delta;
103*77c1e3ccSAndroid Build Coastguard Worker
104*77c1e3ccSAndroid Build Coastguard Worker if (segment == DEFAULT_AQ2_SEG) continue;
105*77c1e3ccSAndroid Build Coastguard Worker
106*77c1e3ccSAndroid Build Coastguard Worker qindex_delta = av1_compute_qdelta_by_rate(
107*77c1e3ccSAndroid Build Coastguard Worker cpi, cm->current_frame.frame_type, base_qindex,
108*77c1e3ccSAndroid Build Coastguard Worker aq_c_q_adj_factor[aq_strength][segment]);
109*77c1e3ccSAndroid Build Coastguard Worker
110*77c1e3ccSAndroid Build Coastguard Worker // For AQ complexity mode, we dont allow Q0 in a segment if the base
111*77c1e3ccSAndroid Build Coastguard Worker // Q is not 0. Q0 (lossless) implies 4x4 only and in AQ mode 2 a segment
112*77c1e3ccSAndroid Build Coastguard Worker // Q delta is sometimes applied without going back around the rd loop.
113*77c1e3ccSAndroid Build Coastguard Worker // This could lead to an illegal combination of partition size and q.
114*77c1e3ccSAndroid Build Coastguard Worker if ((base_qindex != 0) && ((base_qindex + qindex_delta) == 0)) {
115*77c1e3ccSAndroid Build Coastguard Worker qindex_delta = -base_qindex + 1;
116*77c1e3ccSAndroid Build Coastguard Worker }
117*77c1e3ccSAndroid Build Coastguard Worker if ((base_qindex + qindex_delta) > 0) {
118*77c1e3ccSAndroid Build Coastguard Worker av1_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
119*77c1e3ccSAndroid Build Coastguard Worker av1_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
120*77c1e3ccSAndroid Build Coastguard Worker }
121*77c1e3ccSAndroid Build Coastguard Worker }
122*77c1e3ccSAndroid Build Coastguard Worker }
123*77c1e3ccSAndroid Build Coastguard Worker }
124*77c1e3ccSAndroid Build Coastguard Worker
125*77c1e3ccSAndroid Build Coastguard Worker #define DEFAULT_LV_THRESH 10.0
126*77c1e3ccSAndroid Build Coastguard Worker #define MIN_DEFAULT_LV_THRESH 8.0
127*77c1e3ccSAndroid Build Coastguard Worker // Select a segment for the current block.
128*77c1e3ccSAndroid Build Coastguard Worker // The choice of segment for a block depends on the ratio of the projected
129*77c1e3ccSAndroid Build Coastguard Worker // bits for the block vs a target average and its spatial complexity.
av1_caq_select_segment(const AV1_COMP * cpi,MACROBLOCK * mb,BLOCK_SIZE bs,int mi_row,int mi_col,int projected_rate)130*77c1e3ccSAndroid Build Coastguard Worker void av1_caq_select_segment(const AV1_COMP *cpi, MACROBLOCK *mb, BLOCK_SIZE bs,
131*77c1e3ccSAndroid Build Coastguard Worker int mi_row, int mi_col, int projected_rate) {
132*77c1e3ccSAndroid Build Coastguard Worker if ((!is_frame_aq_enabled(cpi)) || (!is_sb_aq_enabled(cpi))) return;
133*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
134*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
135*77c1e3ccSAndroid Build Coastguard Worker
136*77c1e3ccSAndroid Build Coastguard Worker const int mi_offset = mi_row * cm->mi_params.mi_cols + mi_col;
137*77c1e3ccSAndroid Build Coastguard Worker const int xmis = AOMMIN(cm->mi_params.mi_cols - mi_col, mi_size_wide[bs]);
138*77c1e3ccSAndroid Build Coastguard Worker const int ymis = AOMMIN(cm->mi_params.mi_rows - mi_row, mi_size_high[bs]);
139*77c1e3ccSAndroid Build Coastguard Worker int i;
140*77c1e3ccSAndroid Build Coastguard Worker unsigned char segment;
141*77c1e3ccSAndroid Build Coastguard Worker
142*77c1e3ccSAndroid Build Coastguard Worker // Rate depends on fraction of a SB64 in frame (xmis * ymis / bw * bh).
143*77c1e3ccSAndroid Build Coastguard Worker // It is converted to bits << AV1_PROB_COST_SHIFT units.
144*77c1e3ccSAndroid Build Coastguard Worker const int64_t num = (int64_t)(cpi->rc.sb64_target_rate * xmis * ymis)
145*77c1e3ccSAndroid Build Coastguard Worker << AV1_PROB_COST_SHIFT;
146*77c1e3ccSAndroid Build Coastguard Worker const int denom = cm->seq_params->mib_size * cm->seq_params->mib_size;
147*77c1e3ccSAndroid Build Coastguard Worker const int target_rate = (int)(num / denom);
148*77c1e3ccSAndroid Build Coastguard Worker double logvar;
149*77c1e3ccSAndroid Build Coastguard Worker double low_var_thresh;
150*77c1e3ccSAndroid Build Coastguard Worker const int aq_strength = get_aq_c_strength(cm->quant_params.base_qindex,
151*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->bit_depth);
152*77c1e3ccSAndroid Build Coastguard Worker
153*77c1e3ccSAndroid Build Coastguard Worker low_var_thresh =
154*77c1e3ccSAndroid Build Coastguard Worker (is_stat_consumption_stage_twopass(cpi))
155*77c1e3ccSAndroid Build Coastguard Worker ? AOMMAX(exp(cpi->twopass_frame.mb_av_energy), MIN_DEFAULT_LV_THRESH)
156*77c1e3ccSAndroid Build Coastguard Worker : DEFAULT_LV_THRESH;
157*77c1e3ccSAndroid Build Coastguard Worker
158*77c1e3ccSAndroid Build Coastguard Worker av1_setup_src_planes(mb, cpi->source, mi_row, mi_col, num_planes, bs);
159*77c1e3ccSAndroid Build Coastguard Worker logvar = av1_log_block_var(cpi, mb, bs);
160*77c1e3ccSAndroid Build Coastguard Worker
161*77c1e3ccSAndroid Build Coastguard Worker segment = AQ_C_SEGMENTS - 1; // Just in case no break out below.
162*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < AQ_C_SEGMENTS; ++i) {
163*77c1e3ccSAndroid Build Coastguard Worker // Test rate against a threshold value and variance against a threshold.
164*77c1e3ccSAndroid Build Coastguard Worker // Increasing segment number (higher variance and complexity) = higher Q.
165*77c1e3ccSAndroid Build Coastguard Worker if ((projected_rate < target_rate * aq_c_transitions[aq_strength][i]) &&
166*77c1e3ccSAndroid Build Coastguard Worker (logvar < (low_var_thresh + aq_c_var_thresholds[aq_strength][i]))) {
167*77c1e3ccSAndroid Build Coastguard Worker segment = i;
168*77c1e3ccSAndroid Build Coastguard Worker break;
169*77c1e3ccSAndroid Build Coastguard Worker }
170*77c1e3ccSAndroid Build Coastguard Worker }
171*77c1e3ccSAndroid Build Coastguard Worker
172*77c1e3ccSAndroid Build Coastguard Worker // Fill in the entires in the segment map corresponding to this SB64.
173*77c1e3ccSAndroid Build Coastguard Worker const int mi_stride = cm->mi_params.mi_cols;
174*77c1e3ccSAndroid Build Coastguard Worker set_segment_id(cpi->enc_seg.map, mi_offset, xmis, ymis, mi_stride, segment);
175*77c1e3ccSAndroid Build Coastguard Worker }
176