Merge "Fix high bit depth in vp10 codebase"
[platform/upstream/libvpx.git] / vp10 / encoder / aq_complexity.c
1 /*
2  *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10
11 #include <limits.h>
12 #include <math.h>
13
14 #include "vp10/encoder/aq_complexity.h"
15 #include "vp10/encoder/aq_variance.h"
16 #include "vp10/encoder/encodeframe.h"
17 #include "vp10/common/seg_common.h"
18 #include "vp10/encoder/segmentation.h"
19
20 #define AQ_C_SEGMENTS  5
21 #define DEFAULT_AQ2_SEG 3   // Neutral Q segment
22 #define AQ_C_STRENGTHS 3
23 static const double aq_c_q_adj_factor[AQ_C_STRENGTHS][AQ_C_SEGMENTS] =
24   { {1.75, 1.25, 1.05, 1.00, 0.90},
25     {2.00, 1.50, 1.15, 1.00, 0.85},
26     {2.50, 1.75, 1.25, 1.00, 0.80} };
27 static const double aq_c_transitions[AQ_C_STRENGTHS][AQ_C_SEGMENTS] =
28   { {0.15, 0.30, 0.55, 2.00, 100.0},
29     {0.20, 0.40, 0.65, 2.00, 100.0},
30     {0.25, 0.50, 0.75, 2.00, 100.0} };
31 static const double aq_c_var_thresholds[AQ_C_STRENGTHS][AQ_C_SEGMENTS] =
32   { {-4.0, -3.0, -2.0, 100.00, 100.0},
33     {-3.5, -2.5, -1.5, 100.00, 100.0},
34     {-3.0, -2.0, -1.0, 100.00, 100.0} };
35
36 #define DEFAULT_COMPLEXITY 64
37
38
39 static int get_aq_c_strength(int q_index, vpx_bit_depth_t bit_depth) {
40   // Approximate base quatizer (truncated to int)
41   const int base_quant = vp10_ac_quant(q_index, 0, bit_depth) / 4;
42   return (base_quant > 10) + (base_quant > 25);
43 }
44
45 void vp10_setup_in_frame_q_adj(VP9_COMP *cpi) {
46   VP9_COMMON *const cm = &cpi->common;
47   struct segmentation *const seg = &cm->seg;
48
49   // Make SURE use of floating point in this function is safe.
50   vpx_clear_system_state();
51
52   if (cm->frame_type == KEY_FRAME ||
53       cpi->refresh_alt_ref_frame ||
54       (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
55     int segment;
56     const int aq_strength = get_aq_c_strength(cm->base_qindex, cm->bit_depth);
57
58     // Clear down the segment map.
59     memset(cpi->segmentation_map, DEFAULT_AQ2_SEG, cm->mi_rows * cm->mi_cols);
60
61     vp10_clearall_segfeatures(seg);
62
63     // Segmentation only makes sense if the target bits per SB is above a
64     // threshold. Below this the overheads will usually outweigh any benefit.
65     if (cpi->rc.sb64_target_rate < 256) {
66       vp10_disable_segmentation(seg);
67       return;
68     }
69
70     vp10_enable_segmentation(seg);
71
72     // Select delta coding method.
73     seg->abs_delta = SEGMENT_DELTADATA;
74
75     // Default segment "Q" feature is disabled so it defaults to the baseline Q.
76     vp10_disable_segfeature(seg, DEFAULT_AQ2_SEG, SEG_LVL_ALT_Q);
77
78     // Use some of the segments for in frame Q adjustment.
79     for (segment = 0; segment < AQ_C_SEGMENTS; ++segment) {
80       int qindex_delta;
81
82       if (segment == DEFAULT_AQ2_SEG)
83         continue;
84
85       qindex_delta =
86         vp10_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, cm->base_qindex,
87                                    aq_c_q_adj_factor[aq_strength][segment],
88                                    cm->bit_depth);
89
90
91       // For AQ complexity mode, we dont allow Q0 in a segment if the base
92       // Q is not 0. Q0 (lossless) implies 4x4 only and in AQ mode 2 a segment
93       // Q delta is sometimes applied without going back around the rd loop.
94       // This could lead to an illegal combination of partition size and q.
95       if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
96         qindex_delta = -cm->base_qindex + 1;
97       }
98       if ((cm->base_qindex + qindex_delta) > 0) {
99         vp10_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
100         vp10_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
101       }
102     }
103   }
104 }
105
106 #define DEFAULT_LV_THRESH 10.0
107 #define MIN_DEFAULT_LV_THRESH 8.0
108 #define VAR_STRENGTH_STEP 0.25
109 // Select a segment for the current block.
110 // The choice of segment for a block depends on the ratio of the projected
111 // bits for the block vs a target average and its spatial complexity.
112 void vp10_caq_select_segment(VP9_COMP *cpi, MACROBLOCK *mb, BLOCK_SIZE bs,
113                             int mi_row, int mi_col, int projected_rate) {
114   VP9_COMMON *const cm = &cpi->common;
115
116   const int mi_offset = mi_row * cm->mi_cols + mi_col;
117   const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
118   const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
119   const int xmis = MIN(cm->mi_cols - mi_col, num_8x8_blocks_wide_lookup[bs]);
120   const int ymis = MIN(cm->mi_rows - mi_row, num_8x8_blocks_high_lookup[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     const int target_rate = (cpi->rc.sb64_target_rate * xmis * ymis * 256) /
131                             (bw * bh);
132     double logvar;
133     double low_var_thresh;
134     const int aq_strength = get_aq_c_strength(cm->base_qindex, cm->bit_depth);
135
136     vpx_clear_system_state();
137     low_var_thresh = (cpi->oxcf.pass == 2)
138       ? MAX(cpi->twopass.mb_av_energy, MIN_DEFAULT_LV_THRESH)
139       : DEFAULT_LV_THRESH;
140
141     vp10_setup_src_planes(mb, cpi->Source, mi_row, mi_col);
142     logvar = vp10_log_block_var(cpi, mb, bs);
143
144     segment = AQ_C_SEGMENTS - 1;    // Just in case no break out below.
145     for (i = 0; i < AQ_C_SEGMENTS; ++i) {
146       // Test rate against a threshold value and variance against a threshold.
147       // Increasing segment number (higher variance and complexity) = higher Q.
148       if ((projected_rate <
149            target_rate * aq_c_transitions[aq_strength][i]) &&
150           (logvar < (low_var_thresh + aq_c_var_thresholds[aq_strength][i]))) {
151         segment = i;
152         break;
153       }
154     }
155   }
156
157   // Fill in the entires in the segment map corresponding to this SB64.
158   for (y = 0; y < ymis; y++) {
159     for (x = 0; x < xmis; x++) {
160       cpi->segmentation_map[mi_offset + y * cm->mi_cols + x] = segment;
161     }
162   }
163 }