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