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; |
27 // Superblock starting index for cycling through the frame. | 29 // Superblock starting index for cycling through the frame. |
28 int sb_index; | 30 int sb_index; |
29 // Controls how long block will need to wait to be refreshed again, in | 31 // Controls how long block will need to wait to be refreshed again, in |
30 // excess of the cycle time, i.e., in the case of all zero motion, block | 32 // excess of the cycle time, i.e., in the case of all zero motion, block |
31 // will be refreshed every (100/percent_refresh + time_for_refresh) frames. | 33 // will be refreshed every (100/percent_refresh + time_for_refresh) frames. |
32 int time_for_refresh; | 34 int time_for_refresh; |
33 // // Target number of (8x8) blocks that are set for delta-q (segment 1). | 35 // // Target number of (8x8) blocks that are set for delta-q (segment 1). |
34 int target_num_seg_blocks; | 36 int target_num_seg_blocks; |
35 // Actual number of (8x8) blocks that were applied delta-q (segment 1). | 37 // Actual number of (8x8) blocks that were applied delta-q (segment 1). |
36 int actual_num_seg_blocks; | 38 int actual_num_seg_blocks; |
37 // RD mult. parameters for segment 1. | 39 // RD mult. parameters for segment 1. |
38 int rdmult; | 40 int rdmult; |
39 // Cyclic refresh map. | 41 // Cyclic refresh map. |
40 signed char *map; | 42 signed char *map; |
41 // Thresholds applied to the projected rate/distortion of the coding block, | 43 // Thresholds applied to projected rate/distortion of the superblock. |
42 // when deciding whether block should be refreshed. | |
43 int64_t thresh_rate_sb; | 44 int64_t thresh_rate_sb; |
44 int64_t thresh_dist_sb; | 45 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; | |
48 // Rate target ratio to set q delta. | 46 // Rate target ratio to set q delta. |
49 double rate_ratio_qdelta; | 47 double rate_ratio_qdelta; |
50 }; | 48 }; |
51 | 49 |
52 CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) { | 50 CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) { |
53 CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr)); | 51 CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr)); |
54 if (cr == NULL) | 52 if (cr == NULL) |
55 return NULL; | 53 return NULL; |
56 | 54 |
57 cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map)); | 55 cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map)); |
(...skipping 30 matching lines...) Expand all Loading... |
88 else | 86 else |
89 return 1; | 87 return 1; |
90 } | 88 } |
91 | 89 |
92 // Check if this coding block, of size bsize, should be considered for refresh | 90 // Check if this coding block, of size bsize, should be considered for refresh |
93 // (lower-qp coding). Decision can be based on various factors, such as | 91 // (lower-qp coding). Decision can be based on various factors, such as |
94 // size of the coding block (i.e., below min_block size rejected), coding | 92 // size of the coding block (i.e., below min_block size rejected), coding |
95 // mode, and rate/distortion. | 93 // mode, and rate/distortion. |
96 static int candidate_refresh_aq(const CYCLIC_REFRESH *cr, | 94 static int candidate_refresh_aq(const CYCLIC_REFRESH *cr, |
97 const MB_MODE_INFO *mbmi, | 95 const MB_MODE_INFO *mbmi, |
98 int64_t rate, | 96 BLOCK_SIZE bsize, int use_rd, |
99 int64_t dist) { | 97 int64_t rate_sb) { |
100 MV mv = mbmi->mv[0].as_mv; | 98 if (use_rd) { |
101 // If projected rate is below the thresh_rate accept it for lower-qp coding. | 99 MV mv = mbmi->mv[0].as_mv; |
102 // Otherwise, reject the block for lower-qp coding if projected distortion | 100 // If projected rate is below the thresh_rate (well below target, |
103 // is above the threshold, and any of the following is true: | 101 // so undershoot expected), accept it for lower-qp coding. |
104 // 1) mode uses large mv | 102 if (rate_sb < cr->thresh_rate_sb) |
105 // 2) mode is an intra-mode | 103 return 1; |
106 if (rate < cr->thresh_rate_sb) | 104 // Otherwise, reject the block for lower-qp coding if any of the following: |
107 return 1; | 105 // 1) mode uses large mv |
108 else if (dist > cr->thresh_dist_sb && | 106 // 2) mode is an intra-mode (we may want to allow some of this under |
109 (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh || | 107 // another thresh_dist) |
110 mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh || | 108 else if (mv.row > 32 || mv.row < -32 || |
111 !is_inter_block(mbmi))) | 109 mv.col > 32 || mv.col < -32 || !is_inter_block(mbmi)) |
112 return 0; | 110 return 0; |
113 else | 111 else |
114 return 1; | 112 return 1; |
| 113 } else { |
| 114 // Rate/distortion not used for update. |
| 115 if (bsize < cr->min_block_size || |
| 116 mbmi->mv[0].as_int != 0 || |
| 117 !is_inter_block(mbmi)) |
| 118 return 0; |
| 119 else |
| 120 return 1; |
| 121 } |
115 } | 122 } |
116 | 123 |
117 // Compute delta-q for the segment. | 124 // Compute delta-q for the segment. |
118 static int compute_deltaq(const VP9_COMP *cpi, int q) { | 125 static int compute_deltaq(const VP9_COMP *cpi, int q) { |
119 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 126 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
120 const RATE_CONTROL *const rc = &cpi->rc; | 127 const RATE_CONTROL *const rc = &cpi->rc; |
121 int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type, | 128 int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type, |
122 q, cr->rate_ratio_qdelta, | 129 q, cr->rate_ratio_qdelta, |
123 cpi->common.bit_depth); | 130 cpi->common.bit_depth); |
124 if ((-deltaq) > cr->max_qdelta_perc * q / 100) { | 131 if ((-deltaq) > cr->max_qdelta_perc * q / 100) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 cm->bit_depth)); | 187 cm->bit_depth)); |
181 return bits_per_mb; | 188 return bits_per_mb; |
182 } | 189 } |
183 | 190 |
184 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col), | 191 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col), |
185 // check if we should reset the segment_id, and update the cyclic_refresh map | 192 // check if we should reset the segment_id, and update the cyclic_refresh map |
186 // and segmentation map. | 193 // and segmentation map. |
187 void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi, | 194 void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi, |
188 MB_MODE_INFO *const mbmi, | 195 MB_MODE_INFO *const mbmi, |
189 int mi_row, int mi_col, | 196 int mi_row, int mi_col, |
190 BLOCK_SIZE bsize, | 197 BLOCK_SIZE bsize, int use_rd, |
191 int64_t rate, | 198 int64_t rate_sb) { |
192 int64_t dist) { | |
193 const VP9_COMMON *const cm = &cpi->common; | 199 const VP9_COMMON *const cm = &cpi->common; |
194 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 200 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
195 const int bw = num_8x8_blocks_wide_lookup[bsize]; | 201 const int bw = num_8x8_blocks_wide_lookup[bsize]; |
196 const int bh = num_8x8_blocks_high_lookup[bsize]; | 202 const int bh = num_8x8_blocks_high_lookup[bsize]; |
197 const int xmis = MIN(cm->mi_cols - mi_col, bw); | 203 const int xmis = MIN(cm->mi_cols - mi_col, bw); |
198 const int ymis = MIN(cm->mi_rows - mi_row, bh); | 204 const int ymis = MIN(cm->mi_rows - mi_row, bh); |
199 const int block_index = mi_row * cm->mi_cols + mi_col; | 205 const int block_index = mi_row * cm->mi_cols + mi_col; |
200 const int refresh_this_block = candidate_refresh_aq(cr, mbmi, rate, dist); | 206 const int refresh_this_block = candidate_refresh_aq(cr, mbmi, bsize, use_rd, |
| 207 rate_sb); |
201 // Default is to not update the refresh map. | 208 // Default is to not update the refresh map. |
202 int new_map_value = cr->map[block_index]; | 209 int new_map_value = cr->map[block_index]; |
203 int x = 0; int y = 0; | 210 int x = 0; int y = 0; |
204 | 211 |
205 // Check if we should reset the segment_id for this block. | 212 // Check if we should reset the segment_id for this block. |
206 if (mbmi->segment_id > 0 && !refresh_this_block) | 213 if (mbmi->segment_id > 0 && !refresh_this_block) |
207 mbmi->segment_id = 0; | 214 mbmi->segment_id = 0; |
208 | 215 |
209 // Update the cyclic refresh map, to be used for setting segmentation map | 216 // Update the cyclic refresh map, to be used for setting segmentation map |
210 // for the next frame. If the block will be refreshed this frame, mark it | 217 // for the next frame. If the block will be refreshed this frame, mark it |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 vp9_disable_segmentation(&cm->seg); | 351 vp9_disable_segmentation(&cm->seg); |
345 if (cm->frame_type == KEY_FRAME) | 352 if (cm->frame_type == KEY_FRAME) |
346 cr->sb_index = 0; | 353 cr->sb_index = 0; |
347 return; | 354 return; |
348 } else { | 355 } else { |
349 int qindex_delta = 0; | 356 int qindex_delta = 0; |
350 int qindex2; | 357 int qindex2; |
351 const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth); | 358 const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth); |
352 vp9_clear_system_state(); | 359 vp9_clear_system_state(); |
353 cr->max_qdelta_perc = 50; | 360 cr->max_qdelta_perc = 50; |
| 361 cr->min_block_size = BLOCK_8X8; |
354 cr->time_for_refresh = 0; | 362 cr->time_for_refresh = 0; |
355 // Set rate threshold to some fraction (set to 1 for now) of the target | 363 // Set rate threshold to some fraction of target (and scaled by 256). |
356 // rate (target is given by sb64_target_rate and scaled by 256). | 364 cr->thresh_rate_sb = (rc->sb64_target_rate * 256) >> 2; |
357 cr->thresh_rate_sb = (rc->sb64_target_rate << 8); | |
358 // Distortion threshold, quadratic in Q, scale factor to be adjusted. | 365 // Distortion threshold, quadratic in Q, scale factor to be adjusted. |
359 cr->thresh_dist_sb = (int)(q * q) << 5; | 366 cr->thresh_dist_sb = 8 * (int)(q * q); |
360 cr->motion_thresh = 32; | 367 if (cpi->sf.use_nonrd_pick_mode) { |
| 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 |
361 // Set up segmentation. | 374 // Set up segmentation. |
362 // Clear down the segment map. | 375 // Clear down the segment map. |
363 vp9_enable_segmentation(&cm->seg); | 376 vp9_enable_segmentation(&cm->seg); |
364 vp9_clearall_segfeatures(seg); | 377 vp9_clearall_segfeatures(seg); |
365 // Select delta coding method. | 378 // Select delta coding method. |
366 seg->abs_delta = SEGMENT_DELTADATA; | 379 seg->abs_delta = SEGMENT_DELTADATA; |
367 | 380 |
368 // Note: setting temporal_update has no effect, as the seg-map coding method | 381 // Note: setting temporal_update has no effect, as the seg-map coding method |
369 // (temporal or spatial) is determined in vp9_choose_segmap_coding_method(), | 382 // (temporal or spatial) is determined in vp9_choose_segmap_coding_method(), |
370 // based on the coding cost of each method. For error_resilient mode on the | 383 // based on the coding cost of each method. For error_resilient mode on the |
(...skipping 16 matching lines...) Expand all Loading... |
387 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qindex_delta); | 400 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qindex_delta); |
388 | 401 |
389 // Update the segmentation and refresh map. | 402 // Update the segmentation and refresh map. |
390 vp9_cyclic_refresh_update_map(cpi); | 403 vp9_cyclic_refresh_update_map(cpi); |
391 } | 404 } |
392 } | 405 } |
393 | 406 |
394 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) { | 407 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) { |
395 return cr->rdmult; | 408 return cr->rdmult; |
396 } | 409 } |
OLD | NEW |