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 MV mv = mbmi->mv[0].as_mv; |
99 MV mv = mbmi->mv[0].as_mv; | 101 // If projected rate is below the thresh_rate accept it for lower-qp coding. |
100 // If projected rate is below the thresh_rate (well below target, | 102 // Otherwise, 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 if (rate < cr->thresh_rate_sb) |
105 // 1) mode uses large mv | 107 return 1; |
106 // 2) mode is an intra-mode (we may want to allow some of this under | 108 else if (dist > cr->thresh_dist_sb && |
107 // another thresh_dist) | 109 (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh || |
108 else if (mv.row > 32 || mv.row < -32 || | 110 mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh || |
109 mv.col > 32 || mv.col < -32 || !is_inter_block(mbmi)) | 111 !is_inter_block(mbmi))) |
110 return 0; | 112 return 0; |
111 else | 113 else |
112 return 1; | 114 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 } | |
122 } | 115 } |
123 | 116 |
124 // Compute delta-q for the segment. | 117 // Compute delta-q for the segment. |
125 static int compute_deltaq(const VP9_COMP *cpi, int q) { | 118 static int compute_deltaq(const VP9_COMP *cpi, int q) { |
126 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 119 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
127 const RATE_CONTROL *const rc = &cpi->rc; | 120 const RATE_CONTROL *const rc = &cpi->rc; |
128 int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type, | 121 int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type, |
129 q, cr->rate_ratio_qdelta, | 122 q, cr->rate_ratio_qdelta, |
130 cpi->common.bit_depth); | 123 cpi->common.bit_depth); |
131 if ((-deltaq) > cr->max_qdelta_perc * q / 100) { | 124 if ((-deltaq) > cr->max_qdelta_perc * q / 100) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 cm->bit_depth)); | 180 cm->bit_depth)); |
188 return bits_per_mb; | 181 return bits_per_mb; |
189 } | 182 } |
190 | 183 |
191 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col), | 184 // 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 | 185 // check if we should reset the segment_id, and update the cyclic_refresh map |
193 // and segmentation map. | 186 // and segmentation map. |
194 void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi, | 187 void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi, |
195 MB_MODE_INFO *const mbmi, | 188 MB_MODE_INFO *const mbmi, |
196 int mi_row, int mi_col, | 189 int mi_row, int mi_col, |
197 BLOCK_SIZE bsize, int use_rd, | 190 BLOCK_SIZE bsize, |
198 int64_t rate_sb) { | 191 int64_t rate, |
| 192 int64_t dist) { |
199 const VP9_COMMON *const cm = &cpi->common; | 193 const VP9_COMMON *const cm = &cpi->common; |
200 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; | 194 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
201 const int bw = num_8x8_blocks_wide_lookup[bsize]; | 195 const int bw = num_8x8_blocks_wide_lookup[bsize]; |
202 const int bh = num_8x8_blocks_high_lookup[bsize]; | 196 const int bh = num_8x8_blocks_high_lookup[bsize]; |
203 const int xmis = MIN(cm->mi_cols - mi_col, bw); | 197 const int xmis = MIN(cm->mi_cols - mi_col, bw); |
204 const int ymis = MIN(cm->mi_rows - mi_row, bh); | 198 const int ymis = MIN(cm->mi_rows - mi_row, bh); |
205 const int block_index = mi_row * cm->mi_cols + mi_col; | 199 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, | 200 const int refresh_this_block = candidate_refresh_aq(cr, mbmi, rate, dist); |
207 rate_sb); | |
208 // Default is to not update the refresh map. | 201 // Default is to not update the refresh map. |
209 int new_map_value = cr->map[block_index]; | 202 int new_map_value = cr->map[block_index]; |
210 int x = 0; int y = 0; | 203 int x = 0; int y = 0; |
211 | 204 |
212 // Check if we should reset the segment_id for this block. | 205 // Check if we should reset the segment_id for this block. |
213 if (mbmi->segment_id > 0 && !refresh_this_block) | 206 if (mbmi->segment_id > 0 && !refresh_this_block) |
214 mbmi->segment_id = 0; | 207 mbmi->segment_id = 0; |
215 | 208 |
216 // Update the cyclic refresh map, to be used for setting segmentation map | 209 // 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 | 210 // 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... |
351 vp9_disable_segmentation(&cm->seg); | 344 vp9_disable_segmentation(&cm->seg); |
352 if (cm->frame_type == KEY_FRAME) | 345 if (cm->frame_type == KEY_FRAME) |
353 cr->sb_index = 0; | 346 cr->sb_index = 0; |
354 return; | 347 return; |
355 } else { | 348 } else { |
356 int qindex_delta = 0; | 349 int qindex_delta = 0; |
357 int qindex2; | 350 int qindex2; |
358 const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth); | 351 const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth); |
359 vp9_clear_system_state(); | 352 vp9_clear_system_state(); |
360 cr->max_qdelta_perc = 50; | 353 cr->max_qdelta_perc = 50; |
361 cr->min_block_size = BLOCK_8X8; | |
362 cr->time_for_refresh = 0; | 354 cr->time_for_refresh = 0; |
363 // Set rate threshold to some fraction of target (and scaled by 256). | 355 // Set rate threshold to some fraction (set to 1 for now) of the target |
364 cr->thresh_rate_sb = (rc->sb64_target_rate * 256) >> 2; | 356 // rate (target is given by sb64_target_rate and scaled by 256). |
| 357 cr->thresh_rate_sb = (rc->sb64_target_rate << 8); |
365 // Distortion threshold, quadratic in Q, scale factor to be adjusted. | 358 // Distortion threshold, quadratic in Q, scale factor to be adjusted. |
366 cr->thresh_dist_sb = 8 * (int)(q * q); | 359 cr->thresh_dist_sb = (int)(q * q) << 5; |
367 if (cpi->sf.use_nonrd_pick_mode) { | 360 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. | 361 // Set up segmentation. |
375 // Clear down the segment map. | 362 // Clear down the segment map. |
376 vp9_enable_segmentation(&cm->seg); | 363 vp9_enable_segmentation(&cm->seg); |
377 vp9_clearall_segfeatures(seg); | 364 vp9_clearall_segfeatures(seg); |
378 // Select delta coding method. | 365 // Select delta coding method. |
379 seg->abs_delta = SEGMENT_DELTADATA; | 366 seg->abs_delta = SEGMENT_DELTADATA; |
380 | 367 |
381 // Note: setting temporal_update has no effect, as the seg-map coding method | 368 // 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(), | 369 // (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 | 370 // based on the coding cost of each method. For error_resilient mode on the |
(...skipping 16 matching lines...) Expand all Loading... |
400 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qindex_delta); | 387 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qindex_delta); |
401 | 388 |
402 // Update the segmentation and refresh map. | 389 // Update the segmentation and refresh map. |
403 vp9_cyclic_refresh_update_map(cpi); | 390 vp9_cyclic_refresh_update_map(cpi); |
404 } | 391 } |
405 } | 392 } |
406 | 393 |
407 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) { | 394 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) { |
408 return cr->rdmult; | 395 return cr->rdmult; |
409 } | 396 } |
OLD | NEW |