1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #ifndef AOM_AV1_ENCODER_AQ_CYCLICREFRESH_H_
13 #define AOM_AV1_ENCODER_AQ_CYCLICREFRESH_H_
14
15 #include "av1/common/blockd.h"
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 // The segment ids used in cyclic refresh: from base (no boost) to increasing
22 // boost (higher delta-qp).
23 #define CR_SEGMENT_ID_BASE 0
24 #define CR_SEGMENT_ID_BOOST1 1
25 #define CR_SEGMENT_ID_BOOST2 2
26
27 // Maximum rate target ratio for setting segment delta-qp.
28 #define CR_MAX_RATE_TARGET_RATIO 4.0
29
30 struct CYCLIC_REFRESH {
31 // Percentage of blocks per frame that are targeted as candidates
32 // for cyclic refresh.
33 int percent_refresh;
34 // Maximum q-delta as percentage of base q.
35 int max_qdelta_perc;
36 // Superblock starting index for cycling through the frame.
37 int sb_index;
38 // Controls how long block will need to wait to be refreshed again, in
39 // excess of the cycle time, i.e., in the case of all zero motion, block
40 // will be refreshed every (100/percent_refresh + time_for_refresh) frames.
41 int time_for_refresh;
42 // Target number of (4x4) blocks that are set for delta-q.
43 int target_num_seg_blocks;
44 // Actual number of (4x4) blocks that were applied delta-q.
45 int actual_num_seg1_blocks;
46 int actual_num_seg2_blocks;
47 // RD mult. parameters for segment 1.
48 int rdmult;
49 // Cyclic refresh map.
50 int8_t *map;
51 // Map of the last q a block was coded at.
52 uint8_t *last_coded_q_map;
53 // Thresholds applied to the projected rate/distortion of the coding block,
54 // when deciding whether block should be refreshed.
55 int64_t thresh_rate_sb;
56 int64_t thresh_dist_sb;
57 // Threshold applied to the motion vector (in units of 1/8 pel) of the
58 // coding block, when deciding whether block should be refreshed.
59 int16_t motion_thresh;
60 // Rate target ratio to set q delta.
61 double rate_ratio_qdelta;
62 // Boost factor for rate target ratio, for segment CR_SEGMENT_ID_BOOST2.
63 int rate_boost_fac;
64 double low_content_avg;
65 int qindex_delta[3];
66 double weight_segment;
67 int apply_cyclic_refresh;
68 int cnt_zeromv;
69 double avg_frame_low_motion;
70 };
71
72 struct AV1_COMP;
73
74 typedef struct CYCLIC_REFRESH CYCLIC_REFRESH;
75
76 CYCLIC_REFRESH *av1_cyclic_refresh_alloc(int mi_rows, int mi_cols);
77
78 void av1_cyclic_refresh_free(CYCLIC_REFRESH *cr);
79
80 // Estimate the bits, incorporating the delta-q from segment 1, after encoding
81 // the frame.
82 int av1_cyclic_refresh_estimate_bits_at_q(const struct AV1_COMP *cpi,
83 double correction_factor);
84
85 // Estimate the bits per mb, for a given q = i and a corresponding delta-q
86 // (for segment 1), prior to encoding the frame.
87 int av1_cyclic_refresh_rc_bits_per_mb(const struct AV1_COMP *cpi, int i,
88 double correction_factor);
89
90 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
91 // check if we should reset the segment_id, and update the cyclic_refresh map
92 // and segmentation map.
93 void av1_cyclic_refresh_update_segment(const struct AV1_COMP *cpi,
94 MB_MODE_INFO *const mbmi, int mi_row,
95 int mi_col, BLOCK_SIZE bsize,
96 int64_t rate, int64_t dist, int skip);
97
98 // Update the some stats after encode frame is done.
99 void av1_cyclic_refresh_postencode(struct AV1_COMP *const cpi);
100
101 // Set golden frame update interval, for 1 pass CBR mode.
102 void av1_cyclic_refresh_set_golden_update(struct AV1_COMP *const cpi);
103
104 // Set/update global/frame level refresh parameters.
105 void av1_cyclic_refresh_update_parameters(struct AV1_COMP *const cpi);
106
107 // Setup cyclic background refresh: set delta q and segmentation map.
108 void av1_cyclic_refresh_setup(struct AV1_COMP *const cpi);
109
110 int av1_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr);
111
112 void av1_cyclic_refresh_reset_resize(struct AV1_COMP *const cpi);
113
cyclic_refresh_segment_id_boosted(int segment_id)114 static INLINE int cyclic_refresh_segment_id_boosted(int segment_id) {
115 return segment_id == CR_SEGMENT_ID_BOOST1 ||
116 segment_id == CR_SEGMENT_ID_BOOST2;
117 }
118
cyclic_refresh_segment_id(int segment_id)119 static INLINE int cyclic_refresh_segment_id(int segment_id) {
120 if (segment_id == CR_SEGMENT_ID_BOOST1)
121 return CR_SEGMENT_ID_BOOST1;
122 else if (segment_id == CR_SEGMENT_ID_BOOST2)
123 return CR_SEGMENT_ID_BOOST2;
124 else
125 return CR_SEGMENT_ID_BASE;
126 }
127
128 #ifdef __cplusplus
129 } // extern "C"
130 #endif
131
132 #endif // AOM_AV1_ENCODER_AQ_CYCLICREFRESH_H_
133