• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "av1/encoder/block.h"
17 #include "av1/encoder/tokenize.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 // The segment ids used in cyclic refresh: from base (no boost) to increasing
24 // boost (higher delta-qp).
25 #define CR_SEGMENT_ID_BASE 0
26 #define CR_SEGMENT_ID_BOOST1 1
27 #define CR_SEGMENT_ID_BOOST2 2
28 
29 // Maximum rate target ratio for setting segment delta-qp.
30 #define CR_MAX_RATE_TARGET_RATIO 4.0
31 
32 /*!
33  * \brief The stucture of CYCLIC_REFRESH.
34  * \ingroup cyclic_refresh
35  */
36 struct CYCLIC_REFRESH {
37   /*!
38    * Percentage of blocks per frame that are targeted as candidates
39    * for cyclic refresh.
40    */
41   int percent_refresh;
42 
43   /*!
44    * Active adjustment delta for cyclic refresh for rate control.
45    */
46   int percent_refresh_adjustment;
47 
48   /*!
49    * Maximum q-delta as percentage of base q.
50    */
51   int max_qdelta_perc;
52   /*!
53    *Superblock starting index for cycling through the frame.
54    */
55   int sb_index;
56   /*!
57    * Controls how long block will need to wait to be refreshed again, in
58    * excess of the cycle time, i.e., in the case of all zero motion, block
59    * will be refreshed every (100/percent_refresh + time_for_refresh) frames.
60    */
61   int time_for_refresh;
62   /*!
63    * Target number of (4x4) blocks that are set for delta-q.
64    */
65   int target_num_seg_blocks;
66   /*!
67    * Actual number of (4x4) blocks that were applied delta-q,
68    * for segment 1.
69    */
70   int actual_num_seg1_blocks;
71   /*!
72    * Actual number of (4x4) blocks that were applied delta-q,
73    * for segment 2.
74    */
75   int actual_num_seg2_blocks;
76   /*!
77    * RD mult. parameters for segment 1.
78    */
79   int rdmult;
80   /*!
81    * Cyclic refresh map.
82    */
83   int8_t *map;
84   /*!
85    * Threshold applied to the projected rate of the coding block,
86    * when deciding whether block should be refreshed.
87    */
88   int64_t thresh_rate_sb;
89   /*!
90    * Threshold applied to the projected distortion of the coding block,
91    * when deciding whether block should be refreshed.
92    */
93   int64_t thresh_dist_sb;
94   /*!
95    * Threshold applied to the motion vector (in units of 1/8 pel) of the
96    * coding block, when deciding whether block should be refreshed.
97    */
98   int16_t motion_thresh;
99   /*!
100    * Rate target ratio to set q delta.
101    */
102   double rate_ratio_qdelta;
103 
104   /*!
105    * Active adjustment of qdelta rate ratio for enhanced rate control
106    */
107   double rate_ratio_qdelta_adjustment;
108 
109   /*!
110    * Boost factor for rate target ratio, for segment CR_SEGMENT_ID_BOOST2.
111    */
112   int rate_boost_fac;
113 
114   /*!\cond */
115   int qindex_delta[3];
116   double weight_segment;
117   int apply_cyclic_refresh;
118   int skip_over4x4;
119   int counter_encode_maxq_scene_change;
120   int use_block_sad_scene_det;
121   /*!\endcond */
122 };
123 
124 struct AV1_COMP;
125 
126 typedef struct CYCLIC_REFRESH CYCLIC_REFRESH;
127 
128 CYCLIC_REFRESH *av1_cyclic_refresh_alloc(int mi_rows, int mi_cols);
129 
130 void av1_cyclic_refresh_free(CYCLIC_REFRESH *cr);
131 
132 /*!\brief Estimate the bits, incorporating the delta-q from the segments.
133  *
134  * For the just encoded frame, estimate the bits, incorporating the delta-q
135  * from non-base segment(s). Note this function is called in the postencode
136  * (called from rc_update_rate_correction_factors()).
137  *
138  * \ingroup cyclic_refresh
139  * \callgraph
140  * \callergraph
141  *
142  * \param[in]       cpi               Top level encoder structure
143  * \param[in]       correction_factor rate correction factor
144  *
145  * \return Return the estimated bits at given q.
146  */
147 int av1_cyclic_refresh_estimate_bits_at_q(const struct AV1_COMP *cpi,
148                                           double correction_factor);
149 
150 /*!\brief Estimate the bits per mb, for given q = i and delta-q.
151  *
152  * Prior to encoding the frame, estimate the bits per mb, for a given q = i and
153  * a corresponding delta-q (for segment 1). This function is called in the
154  * rc_regulate_q() to set the base qp index. Note: the segment map is set to
155  * either 0/CR_SEGMENT_ID_BASE (no refresh) or to 1/CR_SEGMENT_ID_BOOST1
156  * (refresh) for each superblock, prior to encoding.
157  *
158  * \ingroup cyclic_refresh
159  * \callgraph
160  * \callergraph
161  *
162  * \param[in]       cpi               Top level encoder structure
163  * \param[in]       i                 q index
164  * \param[in]       correction_factor rate correction factor
165  *
166  * \return Return the estimated bits for q = i and delta-q (segment 1).
167  */
168 int av1_cyclic_refresh_rc_bits_per_mb(const struct AV1_COMP *cpi, int i,
169                                       double correction_factor);
170 
171 /*!\brief Update segment_id for blocks are skipped.
172  *
173  * After encoding a given prediction block, of size bsize at (mi_row, mi_col),
174  * check if we should reset the segment_id based on skip_txfm,
175  * and update the cyclic_refresh map and segmentation counters.
176  *
177  * \ingroup cyclic_refresh
178  * \callgraph
179  * \callergraph
180  *
181  * \param[in]   cpi       Top level encoder structure
182  * \param[in]   x         Pointer to MACROBLOCK structure
183  * \param[in]   mi_row    Row coordinate of the block in a step size of MI_SIZE
184  * \param[in]   mi_col    Col coordinate of the block in a step size of MI_SIZE
185  * \param[in]   bsize     Block size
186  * \param[in]   dry_run   A code indicating whether it is part of the final
187  *                        pass for reconstructing the superblock
188  *
189  * \remark Update the \c mbmi->segment_id, the \c cpi->cyclic_refresh and
190  * the \c cm->cpi->enc_seg.map.
191  */
192 
193 void av1_cyclic_reset_segment_skip(const struct AV1_COMP *cpi,
194                                    MACROBLOCK *const x, int mi_row, int mi_col,
195                                    BLOCK_SIZE bsize, RUN_TYPE dry_run);
196 
197 /*!\brief Update segment_id for block based on mode selected.
198  *
199  * Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
200  * check if we should reset the segment_id (based on mode/motion/skip selected
201  * for that block) and update the cyclic_refresh map and segmentation map.
202  *
203  * \ingroup cyclic_refresh
204  * \callgraph
205  * \callergraph
206  *
207  * \param[in]   cpi       Top level encoder structure
208  * \param[in]   x         Pointer to MACROBLOCK structure
209  * \param[in]   mi_row    Row coordinate of the block in a step size of MI_SIZE
210  * \param[in]   mi_col    Col coordinate of the block in a step size of MI_SIZE
211  * \param[in]   bsize     Block size
212  * \param[in]   rate      Projected block rate from pickmode
213  * \param[in]   dist      Projected block dist from pickmode
214  * \param[in]   skip      Skip flag set from picmode
215  * \param[in]   dry_run   A code indicating whether it is part of the final
216  *                         pass for reconstructing the superblock
217  *
218  * \remark Update the \c mbmi->segment_id, the \c cpi->cyclic_refresh and
219  * the \c cm->cpi->enc_seg.map.
220  */
221 void av1_cyclic_refresh_update_segment(const struct AV1_COMP *cpi,
222                                        MACROBLOCK *const x, int mi_row,
223                                        int mi_col, BLOCK_SIZE bsize,
224                                        int64_t rate, int64_t dist, int skip,
225                                        RUN_TYPE dry_run);
226 
227 /*!\brief Initialize counters used for cyclic refresh.
228  *
229  * Initializes cyclic refresh counters cnt_zeromv, actual_num_seg1_blocks and
230  * actual_num_seg2_blocks.
231  *
232  * \ingroup cyclic_refresh
233  * \callgraph
234  * \callergraph
235  *
236  * \param[in]   x         Pointer to MACROBLOCK structure
237  *
238  * \remark Update the \c x->cnt_zeromv, the \c x->actual_num_seg1_blocks and
239  * the \c x->actual_num_seg1_blocks.
240  */
241 void av1_init_cyclic_refresh_counters(MACROBLOCK *const x);
242 
243 /*!\brief Accumulate cyclic refresh counters.
244  *
245  * Accumulates cyclic refresh counters cnt_zeromv, actual_num_seg1_blocks and
246  * actual_num_seg2_blocks from MACROBLOCK strcture to CYCLIC_REFRESH strcture.
247  *
248  * \ingroup cyclic_refresh
249  * \callgraph
250  * \callergraph
251  *
252  * \param[in]   cyclic_refresh Pointer to CYCLIC_REFRESH structure
253  * \param[in]   x              Pointer to MACROBLOCK structure
254  *
255  * \remark Update the \c cyclic_refresh->cnt_zeromv, the \c
256  * cyclic_refresh->actual_num_seg1_blocks and the \c
257  * cyclic_refresh->actual_num_seg1_blocks.
258  */
259 void av1_accumulate_cyclic_refresh_counters(
260     CYCLIC_REFRESH *const cyclic_refresh, const MACROBLOCK *const x);
261 
262 /*!\brief Set golden frame update interval nased on cyclic refresh.
263  *
264  * \ingroup cyclic_refresh
265  * \callgraph
266  * \callergraph
267  *
268  * \param[in]   cpi       Top level encoder structure
269  *
270  * \remark Returns the interval in \c cpi->rc.baseline_gf_interval.
271  */
272 void av1_cyclic_refresh_set_golden_update(struct AV1_COMP *const cpi);
273 
274 /*!\brief Set the global/frame level parameters for cyclic refresh.
275  *
276  * First call to the cyclic refresh, before encoding the frame.
277  * Sets the flag on whether cyclic refresh should be applied, sets
278  * the amount/percent of refresh, and the amount of boost applied to
279  * the two segments (set by rate_ratio_qdelta and rate_boost_fac).
280  *
281  * \ingroup cyclic_refresh
282  * \callgraph
283  * \callergraph
284  *
285  * \param[in]       cpi          Top level encoder structure
286  *
287  * \remark Updates the \c cpi->cyclic_refresh with the settings.
288  */
289 void av1_cyclic_refresh_update_parameters(struct AV1_COMP *const cpi);
290 
291 /*!\brief Setup the cyclic background refresh.
292  *
293  * Set the delta q for the segment(s), and set the segmentation map.
294  *
295  * \ingroup cyclic_refresh
296  * \callgraph
297  * \callergraph
298  *
299  * \param[in]       cpi          Top level encoder structure
300  *
301  * \remark Updates the \c cpi->cyclic_refresh with the cyclic refresh
302  * parameters and the \c cm->seg with the segmentation data.
303  */
304 void av1_cyclic_refresh_setup(struct AV1_COMP *const cpi);
305 
306 int av1_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr);
307 
308 void av1_cyclic_refresh_reset_resize(struct AV1_COMP *const cpi);
309 
310 int av1_cyclic_refresh_disable_lf_cdef(struct AV1_COMP *const cpi);
311 
cyclic_refresh_segment_id_boosted(int segment_id)312 static INLINE int cyclic_refresh_segment_id_boosted(int segment_id) {
313   return segment_id == CR_SEGMENT_ID_BOOST1 ||
314          segment_id == CR_SEGMENT_ID_BOOST2;
315 }
316 
cyclic_refresh_segment_id(int segment_id)317 static INLINE int cyclic_refresh_segment_id(int segment_id) {
318   if (segment_id == CR_SEGMENT_ID_BOOST1)
319     return CR_SEGMENT_ID_BOOST1;
320   else if (segment_id == CR_SEGMENT_ID_BOOST2)
321     return CR_SEGMENT_ID_BOOST2;
322   else
323     return CR_SEGMENT_ID_BASE;
324 }
325 
326 #ifdef __cplusplus
327 }  // extern "C"
328 #endif
329 
330 #endif  // AOM_AV1_ENCODER_AQ_CYCLICREFRESH_H_
331