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/encoder/vp9_aq_cyclicrefresh.h" | 14 #include "vp9/encoder/vp9_aq_cyclicrefresh.h" |
15 | 15 |
16 #include "vp9/common/vp9_seg_common.h" | 16 #include "vp9/common/vp9_seg_common.h" |
17 | 17 |
18 #include "vp9/encoder/vp9_ratectrl.h" | 18 #include "vp9/encoder/vp9_ratectrl.h" |
19 #include "vp9/encoder/vp9_segmentation.h" | 19 #include "vp9/encoder/vp9_segmentation.h" |
20 | 20 |
21 struct CYCLIC_REFRESH { | 21 struct CYCLIC_REFRESH { |
22 // Percentage of blocks per frame that are targeted as candidates | 22 // Percentage of blocks per frame that are targeted as candidates |
23 // for cyclic refresh. | 23 // for cyclic refresh. |
24 int percent_refresh; | 24 int percent_refresh; |
25 // Maximum q-delta as percentage of base q. | 25 // Maximum q-delta as percentage of base q. |
26 int max_qdelta_perc; | 26 int max_qdelta_perc; |
27 // Block size below which we don't apply cyclic refresh. | |
28 BLOCK_SIZE min_block_size; | |
29 // Superblock starting index for cycling through the frame. | 27 // Superblock starting index for cycling through the frame. |
30 int sb_index; | 28 int sb_index; |
31 // Controls how long block will need to wait to be refreshed again, in | 29 // Controls how long block will need to wait to be refreshed again, in |
32 // excess of the cycle time, i.e., in the case of all zero motion, block | 30 // excess of the cycle time, i.e., in the case of all zero motion, block |
33 // will be refreshed every (100/percent_refresh + time_for_refresh) frames. | 31 // will be refreshed every (100/percent_refresh + time_for_refresh) frames. |
34 int time_for_refresh; | 32 int time_for_refresh; |
35 // // Target number of (8x8) blocks that are set for delta-q (segment 1). | 33 // // Target number of (8x8) blocks that are set for delta-q (segment 1). |
36 int target_num_seg_blocks; | 34 int target_num_seg_blocks; |
37 // Actual number of (8x8) blocks that were applied delta-q (segment 1). | 35 // Actual number of (8x8) blocks that were applied delta-q (segment 1). |
38 int actual_num_seg_blocks; | 36 int actual_num_seg_blocks; |
39 // RD mult. parameters for segment 1. | 37 // RD mult. parameters for segment 1. |
40 int rdmult; | 38 int rdmult; |
41 // Cyclic refresh map. | 39 // Cyclic refresh map. |
42 signed char *map; | 40 signed char *map; |
43 // Thresholds applied to projected rate/distortion of the superblock. | 41 // Thresholds applied to the projected rate/distortion of the coding block, |
| 42 // when deciding whether block should be refreshed. |
44 int64_t thresh_rate_sb; | 43 int64_t thresh_rate_sb; |
45 int64_t thresh_dist_sb; | 44 int64_t thresh_dist_sb; |
| 45 // Threshold applied to the motion vector (in units of 1/8 pel) of the |
| 46 // coding block, when deciding whether block should be refreshed. |
| 47 int16_t motion_thresh; |
46 // Rate target ratio to set q delta. | 48 // Rate target ratio to set q delta. |
47 double rate_ratio_qdelta; | 49 double rate_ratio_qdelta; |
48 }; | 50 }; |
49 | 51 |
50 CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) { | 52 CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) { |
51 CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr)); | 53 CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr)); |
52 if (cr == NULL) | 54 if (cr == NULL) |
53 return NULL; | 55 return NULL; |
54 | 56 |
55 cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map)); | 57 cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map)); |
(...skipping 30 matching lines...) Expand all Loading... |
86 else | 88 else |
87 return 1; | 89 return 1; |
88 } | 90 } |
89 | 91 |
90 // Check if this coding block, of size bsize, should be considered for refresh | 92 // Check if this coding block, of size bsize, should be considered for refresh |
91 // (lower-qp coding). Decision can be based on various factors, such as | 93 // (lower-qp coding). Decision can be based on various factors, such as |
92 // size of the coding block (i.e., below min_block size rejected), coding | 94 // size of the coding block (i.e., below min_block size rejected), coding |
93 // mode, and rate/distortion. | 95 // mode, and rate/distortion. |
94 static int candidate_refresh_aq(const CYCLIC_REFRESH *cr, | 96 static int candidate_refresh_aq(const CYCLIC_REFRESH *cr, |
95 const MB_MODE_INFO *mbmi, | 97 const MB_MODE_INFO *mbmi, |
96 BLOCK_SIZE bsize, int use_rd, | 98 int64_t rate, |
97 int64_t rate_sb) { | 99 int64_t dist, |
98 if (use_rd) { | 100 int bsize) { |
99 MV mv = mbmi->mv[0].as_mv; | 101 MV mv = mbmi->mv[0].as_mv; |
100 // If projected rate is below the thresh_rate (well below target, | 102 // Reject the block for lower-qp coding if projected distortion |
101 // so undershoot expected), accept it for lower-qp coding. | 103 // is above the threshold, and any of the following is true: |
102 if (rate_sb < cr->thresh_rate_sb) | 104 // 1) mode uses large mv |
103 return 1; | 105 // 2) mode is an intra-mode |
104 // Otherwise, reject the block for lower-qp coding if any of the following: | 106 // Otherwise accept for refresh. |
105 // 1) mode uses large mv | 107 if (dist > cr->thresh_dist_sb && |
106 // 2) mode is an intra-mode (we may want to allow some of this under | 108 (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh || |
107 // another thresh_dist) | 109 mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh || |
108 else if (mv.row > 32 || mv.row < -32 || | 110 !is_inter_block(mbmi))) |
109 mv.col > 32 || mv.col < -32 || !is_inter_block(mbmi)) | 111 return CR_SEGMENT_ID_BASE; |
110 return 0; | 112 else if (bsize >= BLOCK_32X32 && |
111 else | 113 rate < cr->thresh_rate_sb && |
112 return 1; | 114 is_inter_block(mbmi) && |
113 } else { | 115 mbmi->mv[0].as_int == 0) |
114 // Rate/distortion not used for update. | 116 // More aggressive delta-q for bigger blocks with zero motion. |
115 if (bsize < cr->min_block_size || | 117 return CR_SEGMENT_ID_BOOST2; |
116 mbmi->mv[0].as_int != 0 || | 118 else |
117 !is_inter_block(mbmi)) | 119 return CR_SEGMENT_ID_BOOST1; |
118 return 0; | |
119 else | |
120 return 1; | |
121 } | |
122 } | 120 } |
123 | 121 |
124 // Compute delta-q for the segment. | 122 // Compute delta-q for the segment. |
125 static int compute_deltaq(const VP9_COMP *cpi, int q) { | 123 static int compute_deltaq(const VP9_COMP *cpi, int q, double rate_factor) { |
126 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 124 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
127 const RATE_CONTROL *const rc = &cpi->rc; | 125 const RATE_CONTROL *const rc = &cpi->rc; |
128 int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type, | 126 int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type, |
129 q, cr->rate_ratio_qdelta, | 127 q, rate_factor, |
130 cpi->common.bit_depth); | 128 cpi->common.bit_depth); |
131 if ((-deltaq) > cr->max_qdelta_perc * q / 100) { | 129 if ((-deltaq) > cr->max_qdelta_perc * q / 100) { |
132 deltaq = -cr->max_qdelta_perc * q / 100; | 130 deltaq = -cr->max_qdelta_perc * q / 100; |
133 } | 131 } |
134 return deltaq; | 132 return deltaq; |
135 } | 133 } |
136 | 134 |
137 // For the just encoded frame, estimate the bits, incorporating the delta-q | 135 // For the just encoded frame, estimate the bits, incorporating the delta-q |
138 // from segment 1. This function is called in the postencode (called from | 136 // from non-base segment. For now ignore effect of multiple segments |
139 // rc_update_rate_correction_factors()). | 137 // (with different delta-q). Note this function is called in the postencode |
| 138 // (called from rc_update_rate_correction_factors()). |
140 int vp9_cyclic_refresh_estimate_bits_at_q(const VP9_COMP *cpi, | 139 int vp9_cyclic_refresh_estimate_bits_at_q(const VP9_COMP *cpi, |
141 double correction_factor) { | 140 double correction_factor) { |
142 const VP9_COMMON *const cm = &cpi->common; | 141 const VP9_COMMON *const cm = &cpi->common; |
143 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 142 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
144 int estimated_bits; | 143 int estimated_bits; |
145 int mbs = cm->MBs; | 144 int mbs = cm->MBs; |
146 int num8x8bl = mbs << 2; | 145 int num8x8bl = mbs << 2; |
147 // Weight for segment 1: use actual number of blocks refreshed in | 146 // Weight for non-base segments: use actual number of blocks refreshed in |
148 // previous/just encoded frame. Note number of blocks here is in 8x8 units. | 147 // previous/just encoded frame. Note number of blocks here is in 8x8 units. |
149 double weight_segment = (double)cr->actual_num_seg_blocks / num8x8bl; | 148 double weight_segment = (double)cr->actual_num_seg_blocks / num8x8bl; |
150 // Compute delta-q that was used in the just encoded frame. | 149 // Compute delta-q that was used in the just encoded frame. |
151 int deltaq = compute_deltaq(cpi, cm->base_qindex); | 150 int deltaq = compute_deltaq(cpi, cm->base_qindex, cr->rate_ratio_qdelta); |
152 // Take segment weighted average for estimated bits. | 151 // Take segment weighted average for estimated bits. |
153 estimated_bits = (int)((1.0 - weight_segment) * | 152 estimated_bits = (int)((1.0 - weight_segment) * |
154 vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex, mbs, | 153 vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex, mbs, |
155 correction_factor, cm->bit_depth) + | 154 correction_factor, cm->bit_depth) + |
156 weight_segment * | 155 weight_segment * |
157 vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex + deltaq, mbs, | 156 vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex + deltaq, mbs, |
158 correction_factor, cm->bit_depth)); | 157 correction_factor, cm->bit_depth)); |
159 return estimated_bits; | 158 return estimated_bits; |
160 } | 159 } |
161 | 160 |
162 // Prior to encoding the frame, estimate the bits per mb, for a given q = i and | 161 // Prior to encoding the frame, estimate the bits per mb, for a given q = i and |
163 // a corresponding delta-q (for segment 1). This function is called in the | 162 // a corresponding delta-q (for segment 1). This function is called in the |
164 // rc_regulate_q() to set the base qp index. | 163 // rc_regulate_q() to set the base qp index. |
| 164 // Note: the segment map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or |
| 165 // to 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock, prior to encoding. |
165 int vp9_cyclic_refresh_rc_bits_per_mb(const VP9_COMP *cpi, int i, | 166 int vp9_cyclic_refresh_rc_bits_per_mb(const VP9_COMP *cpi, int i, |
166 double correction_factor) { | 167 double correction_factor) { |
167 const VP9_COMMON *const cm = &cpi->common; | 168 const VP9_COMMON *const cm = &cpi->common; |
168 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 169 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
169 int bits_per_mb; | 170 int bits_per_mb; |
170 int num8x8bl = cm->MBs << 2; | 171 int num8x8bl = cm->MBs << 2; |
171 // Weight for segment 1 prior to encoding: take the target number for the | 172 // Weight for segment 1 prior to encoding: take the target number for the |
172 // frame to be encoded. Number of blocks here is in 8x8 units. | 173 // frame to be encoded. Number of blocks here is in 8x8 units. |
173 // Note that this is called in rc_regulate_q, which is called before the | 174 // Note that this is called in rc_regulate_q, which is called before the |
174 // cyclic_refresh_setup (which sets cr->target_num_seg_blocks). So a mismatch | 175 // cyclic_refresh_setup (which sets cr->target_num_seg_blocks). So a mismatch |
175 // may occur between the cr->target_num_seg_blocks value here and the | 176 // may occur between the cr->target_num_seg_blocks value here and the |
176 // cr->target_num_seg_block set for encoding the frame. For the current use | 177 // cr->target_num_seg_block set for encoding the frame. For the current use |
177 // case of fixed cr->percent_refresh and cr->time_for_refresh = 0, mismatch | 178 // case of fixed cr->percent_refresh and cr->time_for_refresh = 0, mismatch |
178 // does not occur/is very small. | 179 // does not occur/is very small. |
179 double weight_segment = (double)cr->target_num_seg_blocks / num8x8bl; | 180 double weight_segment = (double)cr->target_num_seg_blocks / num8x8bl; |
180 // Compute delta-q corresponding to qindex i. | 181 // Compute delta-q corresponding to qindex i. |
181 int deltaq = compute_deltaq(cpi, i); | 182 int deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta); |
182 // Take segment weighted average for bits per mb. | 183 // Take segment weighted average for bits per mb. |
183 bits_per_mb = (int)((1.0 - weight_segment) * | 184 bits_per_mb = (int)((1.0 - weight_segment) * |
184 vp9_rc_bits_per_mb(cm->frame_type, i, correction_factor, cm->bit_depth) + | 185 vp9_rc_bits_per_mb(cm->frame_type, i, correction_factor, cm->bit_depth) + |
185 weight_segment * | 186 weight_segment * |
186 vp9_rc_bits_per_mb(cm->frame_type, i + deltaq, correction_factor, | 187 vp9_rc_bits_per_mb(cm->frame_type, i + deltaq, correction_factor, |
187 cm->bit_depth)); | 188 cm->bit_depth)); |
188 return bits_per_mb; | 189 return bits_per_mb; |
189 } | 190 } |
190 | 191 |
191 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col), | 192 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col), |
192 // check if we should reset the segment_id, and update the cyclic_refresh map | 193 // check if we should reset the segment_id, and update the cyclic_refresh map |
193 // and segmentation map. | 194 // and segmentation map. |
194 void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi, | 195 void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi, |
195 MB_MODE_INFO *const mbmi, | 196 MB_MODE_INFO *const mbmi, |
196 int mi_row, int mi_col, | 197 int mi_row, int mi_col, |
197 BLOCK_SIZE bsize, int use_rd, | 198 BLOCK_SIZE bsize, |
198 int64_t rate_sb) { | 199 int64_t rate, |
| 200 int64_t dist) { |
199 const VP9_COMMON *const cm = &cpi->common; | 201 const VP9_COMMON *const cm = &cpi->common; |
200 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 202 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
201 const int bw = num_8x8_blocks_wide_lookup[bsize]; | 203 const int bw = num_8x8_blocks_wide_lookup[bsize]; |
202 const int bh = num_8x8_blocks_high_lookup[bsize]; | 204 const int bh = num_8x8_blocks_high_lookup[bsize]; |
203 const int xmis = MIN(cm->mi_cols - mi_col, bw); | 205 const int xmis = MIN(cm->mi_cols - mi_col, bw); |
204 const int ymis = MIN(cm->mi_rows - mi_row, bh); | 206 const int ymis = MIN(cm->mi_rows - mi_row, bh); |
205 const int block_index = mi_row * cm->mi_cols + mi_col; | 207 const int block_index = mi_row * cm->mi_cols + mi_col; |
206 const int refresh_this_block = candidate_refresh_aq(cr, mbmi, bsize, use_rd, | 208 const int refresh_this_block = candidate_refresh_aq(cr, mbmi, rate, dist, |
207 rate_sb); | 209 bsize); |
208 // Default is to not update the refresh map. | 210 // Default is to not update the refresh map. |
209 int new_map_value = cr->map[block_index]; | 211 int new_map_value = cr->map[block_index]; |
210 int x = 0; int y = 0; | 212 int x = 0; int y = 0; |
211 | 213 |
212 // Check if we should reset the segment_id for this block. | 214 // If this block is labeled for refresh, check if we should reset the |
213 if (mbmi->segment_id > 0 && !refresh_this_block) | 215 // segment_id. |
214 mbmi->segment_id = 0; | 216 if (mbmi->segment_id != CR_SEGMENT_ID_BASE) |
| 217 mbmi->segment_id = refresh_this_block; |
215 | 218 |
216 // Update the cyclic refresh map, to be used for setting segmentation map | 219 // Update the cyclic refresh map, to be used for setting segmentation map |
217 // for the next frame. If the block will be refreshed this frame, mark it | 220 // for the next frame. If the block will be refreshed this frame, mark it |
218 // as clean. The magnitude of the -ve influences how long before we consider | 221 // as clean. The magnitude of the -ve influences how long before we consider |
219 // it for refresh again. | 222 // it for refresh again. |
220 if (mbmi->segment_id == 1) { | 223 if (mbmi->segment_id != CR_SEGMENT_ID_BASE) { |
221 new_map_value = -cr->time_for_refresh; | 224 new_map_value = -cr->time_for_refresh; |
222 } else if (refresh_this_block) { | 225 } else if (refresh_this_block) { |
223 // Else if it is accepted as candidate for refresh, and has not already | 226 // Else if it is accepted as candidate for refresh, and has not already |
224 // been refreshed (marked as 1) then mark it as a candidate for cleanup | 227 // been refreshed (marked as 1) then mark it as a candidate for cleanup |
225 // for future time (marked as 0), otherwise don't update it. | 228 // for future time (marked as 0), otherwise don't update it. |
226 if (cr->map[block_index] == 1) | 229 if (cr->map[block_index] == 1) |
227 new_map_value = 0; | 230 new_map_value = 0; |
228 } else { | 231 } else { |
229 // Leave it marked as block that is not candidate for refresh. | 232 // Leave it marked as block that is not candidate for refresh. |
230 new_map_value = 1; | 233 new_map_value = 1; |
(...skipping 11 matching lines...) Expand all Loading... |
242 | 245 |
243 // Update the actual number of blocks that were applied the segment delta q. | 246 // Update the actual number of blocks that were applied the segment delta q. |
244 void vp9_cyclic_refresh_update_actual_count(struct VP9_COMP *const cpi) { | 247 void vp9_cyclic_refresh_update_actual_count(struct VP9_COMP *const cpi) { |
245 VP9_COMMON *const cm = &cpi->common; | 248 VP9_COMMON *const cm = &cpi->common; |
246 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 249 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
247 unsigned char *const seg_map = cpi->segmentation_map; | 250 unsigned char *const seg_map = cpi->segmentation_map; |
248 int mi_row, mi_col; | 251 int mi_row, mi_col; |
249 cr->actual_num_seg_blocks = 0; | 252 cr->actual_num_seg_blocks = 0; |
250 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) | 253 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) |
251 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) { | 254 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) { |
252 if (seg_map[mi_row * cm->mi_cols + mi_col] == 1) | 255 if (seg_map[mi_row * cm->mi_cols + mi_col] != CR_SEGMENT_ID_BASE) |
253 cr->actual_num_seg_blocks++; | 256 cr->actual_num_seg_blocks++; |
254 } | 257 } |
255 } | 258 } |
256 | 259 |
257 // Update the segmentation map, and related quantities: cyclic refresh map, | 260 // Update the segmentation map, and related quantities: cyclic refresh map, |
258 // refresh sb_index, and target number of blocks to be refreshed. | 261 // refresh sb_index, and target number of blocks to be refreshed. |
| 262 // The map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or to |
| 263 // 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock. |
| 264 // Blocks labeled as BOOST1 may later get set to BOOST2 (during the |
| 265 // encoding of the superblock). |
259 void vp9_cyclic_refresh_update_map(VP9_COMP *const cpi) { | 266 void vp9_cyclic_refresh_update_map(VP9_COMP *const cpi) { |
260 VP9_COMMON *const cm = &cpi->common; | 267 VP9_COMMON *const cm = &cpi->common; |
261 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 268 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
262 unsigned char *const seg_map = cpi->segmentation_map; | 269 unsigned char *const seg_map = cpi->segmentation_map; |
263 int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame; | 270 int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame; |
264 int xmis, ymis, x, y; | 271 int xmis, ymis, x, y; |
265 vpx_memset(seg_map, 0, cm->mi_rows * cm->mi_cols); | 272 vpx_memset(seg_map, CR_SEGMENT_ID_BASE, cm->mi_rows * cm->mi_cols); |
266 sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE; | 273 sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE; |
267 sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE; | 274 sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE; |
268 sbs_in_frame = sb_cols * sb_rows; | 275 sbs_in_frame = sb_cols * sb_rows; |
269 // Number of target blocks to get the q delta (segment 1). | 276 // Number of target blocks to get the q delta (segment 1). |
270 block_count = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100; | 277 block_count = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100; |
271 // Set the segmentation map: cycle through the superblocks, starting at | 278 // Set the segmentation map: cycle through the superblocks, starting at |
272 // cr->mb_index, and stopping when either block_count blocks have been found | 279 // cr->mb_index, and stopping when either block_count blocks have been found |
273 // to be refreshed, or we have passed through whole frame. | 280 // to be refreshed, or we have passed through whole frame. |
274 assert(cr->sb_index < sbs_in_frame); | 281 assert(cr->sb_index < sbs_in_frame); |
275 i = cr->sb_index; | 282 i = cr->sb_index; |
(...skipping 24 matching lines...) Expand all Loading... |
300 } else if (cr->map[bl_index2] < 0) { | 307 } else if (cr->map[bl_index2] < 0) { |
301 cr->map[bl_index2]++; | 308 cr->map[bl_index2]++; |
302 } | 309 } |
303 } | 310 } |
304 } | 311 } |
305 // Enforce constant segment over superblock. | 312 // Enforce constant segment over superblock. |
306 // If segment is at least half of superblock, set to 1. | 313 // If segment is at least half of superblock, set to 1. |
307 if (sum_map >= xmis * ymis / 2) { | 314 if (sum_map >= xmis * ymis / 2) { |
308 for (y = 0; y < ymis; y++) | 315 for (y = 0; y < ymis; y++) |
309 for (x = 0; x < xmis; x++) { | 316 for (x = 0; x < xmis; x++) { |
310 seg_map[bl_index + y * cm->mi_cols + x] = 1; | 317 seg_map[bl_index + y * cm->mi_cols + x] = CR_SEGMENT_ID_BOOST1; |
311 } | 318 } |
312 cr->target_num_seg_blocks += xmis * ymis; | 319 cr->target_num_seg_blocks += xmis * ymis; |
313 } | 320 } |
314 i++; | 321 i++; |
315 if (i == sbs_in_frame) { | 322 if (i == sbs_in_frame) { |
316 i = 0; | 323 i = 0; |
317 } | 324 } |
318 } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index); | 325 } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index); |
319 cr->sb_index = i; | 326 cr->sb_index = i; |
320 } | 327 } |
(...skipping 30 matching lines...) Expand all Loading... |
351 vp9_disable_segmentation(&cm->seg); | 358 vp9_disable_segmentation(&cm->seg); |
352 if (cm->frame_type == KEY_FRAME) | 359 if (cm->frame_type == KEY_FRAME) |
353 cr->sb_index = 0; | 360 cr->sb_index = 0; |
354 return; | 361 return; |
355 } else { | 362 } else { |
356 int qindex_delta = 0; | 363 int qindex_delta = 0; |
357 int qindex2; | 364 int qindex2; |
358 const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth); | 365 const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth); |
359 vp9_clear_system_state(); | 366 vp9_clear_system_state(); |
360 cr->max_qdelta_perc = 50; | 367 cr->max_qdelta_perc = 50; |
361 cr->min_block_size = BLOCK_8X8; | |
362 cr->time_for_refresh = 0; | 368 cr->time_for_refresh = 0; |
363 // Set rate threshold to some fraction of target (and scaled by 256). | 369 // Set rate threshold to some multiple (set to 2 for now) of the target |
364 cr->thresh_rate_sb = (rc->sb64_target_rate * 256) >> 2; | 370 // rate (target is given by sb64_target_rate and scaled by 256). |
| 371 cr->thresh_rate_sb = (rc->sb64_target_rate << 8) << 1; |
365 // Distortion threshold, quadratic in Q, scale factor to be adjusted. | 372 // Distortion threshold, quadratic in Q, scale factor to be adjusted. |
366 cr->thresh_dist_sb = 8 * (int)(q * q); | 373 cr->thresh_dist_sb = (int)(q * q) << 2; |
367 if (cpi->sf.use_nonrd_pick_mode) { | 374 cr->motion_thresh = 32; |
368 // May want to be more conservative with thresholds in non-rd mode for now | |
369 // as rate/distortion are derived from model based on prediction residual. | |
370 cr->thresh_rate_sb = (rc->sb64_target_rate * 256); | |
371 cr->thresh_dist_sb = 16 * (int)(q * q); | |
372 } | |
373 | |
374 // Set up segmentation. | 375 // Set up segmentation. |
375 // Clear down the segment map. | 376 // Clear down the segment map. |
376 vp9_enable_segmentation(&cm->seg); | 377 vp9_enable_segmentation(&cm->seg); |
377 vp9_clearall_segfeatures(seg); | 378 vp9_clearall_segfeatures(seg); |
378 // Select delta coding method. | 379 // Select delta coding method. |
379 seg->abs_delta = SEGMENT_DELTADATA; | 380 seg->abs_delta = SEGMENT_DELTADATA; |
380 | 381 |
381 // Note: setting temporal_update has no effect, as the seg-map coding method | 382 // Note: setting temporal_update has no effect, as the seg-map coding method |
382 // (temporal or spatial) is determined in vp9_choose_segmap_coding_method(), | 383 // (temporal or spatial) is determined in vp9_choose_segmap_coding_method(), |
383 // based on the coding cost of each method. For error_resilient mode on the | 384 // based on the coding cost of each method. For error_resilient mode on the |
384 // last_frame_seg_map is set to 0, so if temporal coding is used, it is | 385 // last_frame_seg_map is set to 0, so if temporal coding is used, it is |
385 // relative to 0 previous map. | 386 // relative to 0 previous map. |
386 // seg->temporal_update = 0; | 387 // seg->temporal_update = 0; |
387 | 388 |
388 // Segment 0 "Q" feature is disabled so it defaults to the baseline Q. | 389 // Segment BASE "Q" feature is disabled so it defaults to the baseline Q. |
389 vp9_disable_segfeature(seg, 0, SEG_LVL_ALT_Q); | 390 vp9_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q); |
390 // Use segment 1 for in-frame Q adjustment. | 391 // Use segment BOOST1 for in-frame Q adjustment. |
391 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); | 392 vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q); |
| 393 // Use segment BOOST2 for more aggressive in-frame Q adjustment. |
| 394 vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q); |
392 | 395 |
393 // Set the q delta for segment 1. | 396 // Set the q delta for segment BOOST1. |
394 qindex_delta = compute_deltaq(cpi, cm->base_qindex); | 397 qindex_delta = compute_deltaq(cpi, cm->base_qindex, cr->rate_ratio_qdelta); |
395 | 398 |
396 // Compute rd-mult for segment 1. | 399 // Compute rd-mult for segment BOOST1. |
397 qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ); | 400 qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ); |
398 cr->rdmult = vp9_compute_rd_mult(cpi, qindex2); | 401 cr->rdmult = vp9_compute_rd_mult(cpi, qindex2); |
399 | 402 |
400 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qindex_delta); | 403 vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q, qindex_delta); |
| 404 |
| 405 // Set a more aggressive (higher) q delta for segment BOOST2. |
| 406 qindex_delta = compute_deltaq(cpi, cm->base_qindex, |
| 407 MIN(CR_MAX_RATE_TARGET_RATIO, |
| 408 CR_BOOST2_FAC * cr->rate_ratio_qdelta)); |
| 409 vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta); |
401 | 410 |
402 // Update the segmentation and refresh map. | 411 // Update the segmentation and refresh map. |
403 vp9_cyclic_refresh_update_map(cpi); | 412 vp9_cyclic_refresh_update_map(cpi); |
404 } | 413 } |
405 } | 414 } |
406 | 415 |
407 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) { | 416 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) { |
408 return cr->rdmult; | 417 return cr->rdmult; |
409 } | 418 } |
OLD | NEW |