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