• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
get_aq_c_strength(int q_index,aom_bit_depth_t bit_depth)42 static int get_aq_c_strength(int q_index, aom_bit_depth_t bit_depth) {
43   // Approximate base quatizer (truncated to int)
44   const int base_quant = av1_ac_quant_Q3(q_index, 0, bit_depth) / 4;
45   return (base_quant > 10) + (base_quant > 25);
46 }
47 
av1_setup_in_frame_q_adj(AV1_COMP * cpi)48 void av1_setup_in_frame_q_adj(AV1_COMP *cpi) {
49   AV1_COMMON *const cm = &cpi->common;
50   struct segmentation *const seg = &cm->seg;
51   int resolution_change =
52       cm->prev_frame && (cm->width != cm->prev_frame->width ||
53                          cm->height != cm->prev_frame->height);
54 
55   // Make SURE use of floating point in this function is safe.
56   aom_clear_system_state();
57 
58   if (resolution_change) {
59     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
60     av1_clearall_segfeatures(seg);
61     av1_disable_segmentation(seg);
62     return;
63   }
64 
65   if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
66       cpi->refresh_alt_ref_frame ||
67       (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
68     int segment;
69     const int aq_strength =
70         get_aq_c_strength(cm->base_qindex, cm->seq_params.bit_depth);
71 
72     // Clear down the segment map.
73     memset(cpi->segmentation_map, DEFAULT_AQ2_SEG, cm->mi_rows * cm->mi_cols);
74 
75     av1_clearall_segfeatures(seg);
76 
77     // Segmentation only makes sense if the target bits per SB is above a
78     // threshold. Below this the overheads will usually outweigh any benefit.
79     if (cpi->rc.sb64_target_rate < 256) {
80       av1_disable_segmentation(seg);
81       return;
82     }
83 
84     av1_enable_segmentation(seg);
85 
86     // Default segment "Q" feature is disabled so it defaults to the baseline Q.
87     av1_disable_segfeature(seg, DEFAULT_AQ2_SEG, SEG_LVL_ALT_Q);
88 
89     // Use some of the segments for in frame Q adjustment.
90     for (segment = 0; segment < AQ_C_SEGMENTS; ++segment) {
91       int qindex_delta;
92 
93       if (segment == DEFAULT_AQ2_SEG) continue;
94 
95       qindex_delta = av1_compute_qdelta_by_rate(
96           &cpi->rc, cm->current_frame.frame_type, cm->base_qindex,
97           aq_c_q_adj_factor[aq_strength][segment], cm->seq_params.bit_depth);
98 
99       // For AQ complexity mode, we dont allow Q0 in a segment if the base
100       // Q is not 0. Q0 (lossless) implies 4x4 only and in AQ mode 2 a segment
101       // Q delta is sometimes applied without going back around the rd loop.
102       // This could lead to an illegal combination of partition size and q.
103       if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
104         qindex_delta = -cm->base_qindex + 1;
105       }
106       if ((cm->base_qindex + qindex_delta) > 0) {
107         av1_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
108         av1_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
109       }
110     }
111   }
112 }
113 
114 #define DEFAULT_LV_THRESH 10.0
115 #define MIN_DEFAULT_LV_THRESH 8.0
116 // Select a segment for the current block.
117 // The choice of segment for a block depends on the ratio of the projected
118 // 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)119 void av1_caq_select_segment(const AV1_COMP *cpi, MACROBLOCK *mb, BLOCK_SIZE bs,
120                             int mi_row, int mi_col, int projected_rate) {
121   const AV1_COMMON *const cm = &cpi->common;
122   const int num_planes = av1_num_planes(cm);
123 
124   const int mi_offset = mi_row * cm->mi_cols + mi_col;
125   const int xmis = AOMMIN(cm->mi_cols - mi_col, mi_size_wide[bs]);
126   const int ymis = AOMMIN(cm->mi_rows - mi_row, mi_size_high[bs]);
127   int x, y;
128   int i;
129   unsigned char segment;
130 
131   if (0) {
132     segment = DEFAULT_AQ2_SEG;
133   } else {
134     // Rate depends on fraction of a SB64 in frame (xmis * ymis / bw * bh).
135     // It is converted to bits << AV1_PROB_COST_SHIFT units.
136     const int64_t num = (int64_t)(cpi->rc.sb64_target_rate * xmis * ymis)
137                         << AV1_PROB_COST_SHIFT;
138     const int denom = cm->seq_params.mib_size * cm->seq_params.mib_size;
139     const int target_rate = (int)(num / denom);
140     double logvar;
141     double low_var_thresh;
142     const int aq_strength =
143         get_aq_c_strength(cm->base_qindex, cm->seq_params.bit_depth);
144 
145     aom_clear_system_state();
146     low_var_thresh =
147         (cpi->oxcf.pass == 2)
148             ? AOMMAX(exp(cpi->twopass.mb_av_energy), MIN_DEFAULT_LV_THRESH)
149             : DEFAULT_LV_THRESH;
150 
151     av1_setup_src_planes(mb, cpi->source, mi_row, mi_col, num_planes, bs);
152     logvar = av1_log_block_var(cpi, mb, bs);
153 
154     segment = AQ_C_SEGMENTS - 1;  // Just in case no break out below.
155     for (i = 0; i < AQ_C_SEGMENTS; ++i) {
156       // Test rate against a threshold value and variance against a threshold.
157       // Increasing segment number (higher variance and complexity) = higher Q.
158       if ((projected_rate < target_rate * aq_c_transitions[aq_strength][i]) &&
159           (logvar < (low_var_thresh + aq_c_var_thresholds[aq_strength][i]))) {
160         segment = i;
161         break;
162       }
163     }
164   }
165 
166   // Fill in the entires in the segment map corresponding to this SB64.
167   for (y = 0; y < ymis; y++) {
168     for (x = 0; x < xmis; x++) {
169       cpi->segmentation_map[mi_offset + y * cm->mi_cols + x] = segment;
170     }
171   }
172 }
173