blob: 10974f018b00985ad0a0ca3914d7b0fe9a7f9978 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
James Zerne1cbb132018-08-22 14:10:36 -070012#ifndef AOM_AV1_ENCODER_AQ_CYCLICREFRESH_H_
13#define AOM_AV1_ENCODER_AQ_CYCLICREFRESH_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include "av1/common/blockd.h"
Cherma Rajan Ad27a1472021-02-19 11:35:40 +053016#include "av1/encoder/block.h"
17#include "av1/encoder/tokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018
19#ifdef __cplusplus
20extern "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
Marco Paniconi9cd08812020-07-13 11:01:24 -070032/*!
33 * \brief The stucture of CYCLIC_REFRESH.
34 * \ingroup cyclic_refresh
35 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070036struct CYCLIC_REFRESH {
Marco Paniconi9cd08812020-07-13 11:01:24 -070037 /*!
38 * Percentage of blocks per frame that are targeted as candidates
39 * for cyclic refresh.
40 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070041 int percent_refresh;
Paul Wilkinsff3923f2022-08-22 17:16:03 +010042
43 /*!
44 * Active adjustment delta for cyclic refresh for rate control.
45 */
46 int percent_refresh_adjustment;
47
Marco Paniconi9cd08812020-07-13 11:01:24 -070048 /*!
49 * Maximum q-delta as percentage of base q.
50 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070051 int max_qdelta_perc;
Marco Paniconi9cd08812020-07-13 11:01:24 -070052 /*!
53 *Superblock starting index for cycling through the frame.
54 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070055 int sb_index;
Marco Paniconi9cd08812020-07-13 11:01:24 -070056 /*!
Paul Wilkinsece2c482023-01-10 16:18:32 +000057 *Superblock index cyclic refresh index last frame
58 */
59 int last_sb_index;
60 /*!
Marco Paniconi9cd08812020-07-13 11:01:24 -070061 * Controls how long block will need to wait to be refreshed again, in
62 * excess of the cycle time, i.e., in the case of all zero motion, block
63 * will be refreshed every (100/percent_refresh + time_for_refresh) frames.
64 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070065 int time_for_refresh;
Marco Paniconi9cd08812020-07-13 11:01:24 -070066 /*!
67 * Target number of (4x4) blocks that are set for delta-q.
68 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070069 int target_num_seg_blocks;
Marco Paniconi9cd08812020-07-13 11:01:24 -070070 /*!
71 * Actual number of (4x4) blocks that were applied delta-q,
72 * for segment 1.
73 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070074 int actual_num_seg1_blocks;
Marco Paniconi9cd08812020-07-13 11:01:24 -070075 /*!
76 * Actual number of (4x4) blocks that were applied delta-q,
77 * for segment 2.
78 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070079 int actual_num_seg2_blocks;
Marco Paniconi9cd08812020-07-13 11:01:24 -070080 /*!
81 * RD mult. parameters for segment 1.
82 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070083 int rdmult;
Marco Paniconi9cd08812020-07-13 11:01:24 -070084 /*!
85 * Cyclic refresh map.
86 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070087 int8_t *map;
Marco Paniconi9cd08812020-07-13 11:01:24 -070088 /*!
Marco Paniconi9cd08812020-07-13 11:01:24 -070089 * Threshold applied to the projected rate of the coding block,
90 * when deciding whether block should be refreshed.
91 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070092 int64_t thresh_rate_sb;
Marco Paniconi9cd08812020-07-13 11:01:24 -070093 /*!
94 * Threshold applied to the projected distortion of the coding block,
95 * when deciding whether block should be refreshed.
96 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070097 int64_t thresh_dist_sb;
Marco Paniconi9cd08812020-07-13 11:01:24 -070098 /*!
99 * Threshold applied to the motion vector (in units of 1/8 pel) of the
100 * coding block, when deciding whether block should be refreshed.
101 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700102 int16_t motion_thresh;
Marco Paniconi9cd08812020-07-13 11:01:24 -0700103 /*!
104 * Rate target ratio to set q delta.
105 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700106 double rate_ratio_qdelta;
Paul Wilkinsff3923f2022-08-22 17:16:03 +0100107
108 /*!
109 * Active adjustment of qdelta rate ratio for enhanced rate control
110 */
111 double rate_ratio_qdelta_adjustment;
112
Marco Paniconi9cd08812020-07-13 11:01:24 -0700113 /*!
114 * Boost factor for rate target ratio, for segment CR_SEGMENT_ID_BOOST2.
115 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700116 int rate_boost_fac;
Marco Paniconi9cd08812020-07-13 11:01:24 -0700117
118 /*!\cond */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700119 int qindex_delta[3];
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700120 int apply_cyclic_refresh;
Marco Paniconi03b6f692021-09-30 10:22:36 -0700121 int skip_over4x4;
Marco Paniconi033ae472022-01-13 00:48:35 -0800122 int counter_encode_maxq_scene_change;
Marco Paniconiec42d4d2022-05-22 22:18:15 -0700123 int use_block_sad_scene_det;
Marco Paniconi9cd08812020-07-13 11:01:24 -0700124 /*!\endcond */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700125};
126
Yaowu Xuf883b422016-08-30 14:01:10 -0700127struct AV1_COMP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128
Yaowu Xuc27fc142016-08-22 16:08:15 -0700129typedef struct CYCLIC_REFRESH CYCLIC_REFRESH;
130
Yaowu Xuf883b422016-08-30 14:01:10 -0700131CYCLIC_REFRESH *av1_cyclic_refresh_alloc(int mi_rows, int mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132
Yaowu Xuf883b422016-08-30 14:01:10 -0700133void av1_cyclic_refresh_free(CYCLIC_REFRESH *cr);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134
Marco Paniconi9cd08812020-07-13 11:01:24 -0700135/*!\brief Estimate the bits, incorporating the delta-q from the segments.
136 *
137 * For the just encoded frame, estimate the bits, incorporating the delta-q
138 * from non-base segment(s). Note this function is called in the postencode
139 * (called from rc_update_rate_correction_factors()).
140 *
141 * \ingroup cyclic_refresh
142 * \callgraph
143 * \callergraph
144 *
145 * \param[in] cpi Top level encoder structure
146 * \param[in] correction_factor rate correction factor
147 *
148 * \return Return the estimated bits at given q.
149 */
Yaowu Xuf883b422016-08-30 14:01:10 -0700150int av1_cyclic_refresh_estimate_bits_at_q(const struct AV1_COMP *cpi,
151 double correction_factor);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152
Marco Paniconi9cd08812020-07-13 11:01:24 -0700153/*!\brief Estimate the bits per mb, for given q = i and delta-q.
154 *
155 * Prior to encoding the frame, estimate the bits per mb, for a given q = i and
156 * a corresponding delta-q (for segment 1). This function is called in the
157 * rc_regulate_q() to set the base qp index. Note: the segment map is set to
158 * either 0/CR_SEGMENT_ID_BASE (no refresh) or to 1/CR_SEGMENT_ID_BOOST1
159 * (refresh) for each superblock, prior to encoding.
160 *
161 * \ingroup cyclic_refresh
162 * \callgraph
163 * \callergraph
164 *
165 * \param[in] cpi Top level encoder structure
166 * \param[in] i q index
167 * \param[in] correction_factor rate correction factor
168 *
169 * \return Return the estimated bits for q = i and delta-q (segment 1).
170 */
Yaowu Xuf883b422016-08-30 14:01:10 -0700171int av1_cyclic_refresh_rc_bits_per_mb(const struct AV1_COMP *cpi, int i,
172 double correction_factor);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700173
Marco Paniconi14f0c642021-04-27 16:08:11 -0700174/*!\brief Update segment_id for blocks are skipped.
175 *
176 * After encoding a given prediction block, of size bsize at (mi_row, mi_col),
177 * check if we should reset the segment_id based on skip_txfm,
178 * and update the cyclic_refresh map and segmentation counters.
179 *
180 * \ingroup cyclic_refresh
181 * \callgraph
182 * \callergraph
183 *
184 * \param[in] cpi Top level encoder structure
185 * \param[in] x Pointer to MACROBLOCK structure
186 * \param[in] mi_row Row coordinate of the block in a step size of MI_SIZE
187 * \param[in] mi_col Col coordinate of the block in a step size of MI_SIZE
188 * \param[in] bsize Block size
Ranjit Kumar Tulabandu0066e7a2022-10-10 11:57:06 +0530189 * \param[in] dry_run A code indicating whether it is part of the final
190 * pass for reconstructing the superblock
Marco Paniconi14f0c642021-04-27 16:08:11 -0700191 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700192 * \remark Update the \c mbmi->segment_id, the \c cpi->cyclic_refresh and
Marco Paniconi14f0c642021-04-27 16:08:11 -0700193 * the \c cm->cpi->enc_seg.map.
194 */
195
196void av1_cyclic_reset_segment_skip(const struct AV1_COMP *cpi,
197 MACROBLOCK *const x, int mi_row, int mi_col,
Ranjit Kumar Tulabandu0066e7a2022-10-10 11:57:06 +0530198 BLOCK_SIZE bsize, RUN_TYPE dry_run);
Marco Paniconi14f0c642021-04-27 16:08:11 -0700199
Marco Paniconi9cd08812020-07-13 11:01:24 -0700200/*!\brief Update segment_id for block based on mode selected.
201 *
202 * Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
203 * check if we should reset the segment_id (based on mode/motion/skip selected
204 * for that block) and update the cyclic_refresh map and segmentation map.
205 *
206 * \ingroup cyclic_refresh
207 * \callgraph
208 * \callergraph
209 *
210 * \param[in] cpi Top level encoder structure
Cherma Rajan Ad27a1472021-02-19 11:35:40 +0530211 * \param[in] x Pointer to MACROBLOCK structure
Marco Paniconi9cd08812020-07-13 11:01:24 -0700212 * \param[in] mi_row Row coordinate of the block in a step size of MI_SIZE
213 * \param[in] mi_col Col coordinate of the block in a step size of MI_SIZE
214 * \param[in] bsize Block size
215 * \param[in] rate Projected block rate from pickmode
216 * \param[in] dist Projected block dist from pickmode
Cherma Rajan Ad27a1472021-02-19 11:35:40 +0530217 * \param[in] skip Skip flag set from picmode
218 * \param[in] dry_run A code indicating whether it is part of the final
219 * pass for reconstructing the superblock
Marco Paniconi9cd08812020-07-13 11:01:24 -0700220 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700221 * \remark Update the \c mbmi->segment_id, the \c cpi->cyclic_refresh and
Marco Paniconi9cd08812020-07-13 11:01:24 -0700222 * the \c cm->cpi->enc_seg.map.
223 */
Urvang Joshi52648442016-10-13 17:27:51 -0700224void av1_cyclic_refresh_update_segment(const struct AV1_COMP *cpi,
Cherma Rajan Ad27a1472021-02-19 11:35:40 +0530225 MACROBLOCK *const x, int mi_row,
Yaowu Xuf883b422016-08-30 14:01:10 -0700226 int mi_col, BLOCK_SIZE bsize,
Cherma Rajan Ad27a1472021-02-19 11:35:40 +0530227 int64_t rate, int64_t dist, int skip,
228 RUN_TYPE dry_run);
229
230/*!\brief Initialize counters used for cyclic refresh.
231 *
Marco Paniconi92a24f92023-04-18 14:42:26 -0700232 * Initializes cyclic refresh counters actual_num_seg1_blocks and
Cherma Rajan Ad27a1472021-02-19 11:35:40 +0530233 * actual_num_seg2_blocks.
234 *
235 * \ingroup cyclic_refresh
236 * \callgraph
237 * \callergraph
238 *
239 * \param[in] x Pointer to MACROBLOCK structure
240 *
Marco Paniconi92a24f92023-04-18 14:42:26 -0700241 * \remark Update the \c x->actual_num_seg1_blocks and the
242 * \c x->actual_num_seg2_blocks.
Cherma Rajan Ad27a1472021-02-19 11:35:40 +0530243 */
244void av1_init_cyclic_refresh_counters(MACROBLOCK *const x);
245
246/*!\brief Accumulate cyclic refresh counters.
247 *
Marco Paniconi92a24f92023-04-18 14:42:26 -0700248 * Accumulates cyclic refresh counters actual_num_seg1_blocks and
Cherma Rajan Ad27a1472021-02-19 11:35:40 +0530249 * actual_num_seg2_blocks from MACROBLOCK strcture to CYCLIC_REFRESH strcture.
250 *
251 * \ingroup cyclic_refresh
252 * \callgraph
253 * \callergraph
254 *
255 * \param[in] cyclic_refresh Pointer to CYCLIC_REFRESH structure
256 * \param[in] x Pointer to MACROBLOCK structure
257 *
Marco Paniconi92a24f92023-04-18 14:42:26 -0700258 * \remark Update the \c cyclic_refresh->actual_num_seg1_blocks and the
259 * \c cyclic_refresh->actual_num_seg2_blocks.
Cherma Rajan Ad27a1472021-02-19 11:35:40 +0530260 */
261void av1_accumulate_cyclic_refresh_counters(
262 CYCLIC_REFRESH *const cyclic_refresh, const MACROBLOCK *const x);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263
Marco Paniconi9cd08812020-07-13 11:01:24 -0700264/*!\brief Set golden frame update interval nased on cyclic refresh.
265 *
266 * \ingroup cyclic_refresh
267 * \callgraph
268 * \callergraph
269 *
270 * \param[in] cpi Top level encoder structure
271 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700272 * \remark Returns the interval in \c cpi->rc.baseline_gf_interval.
Marco Paniconi9cd08812020-07-13 11:01:24 -0700273 */
Yaowu Xuf883b422016-08-30 14:01:10 -0700274void av1_cyclic_refresh_set_golden_update(struct AV1_COMP *const cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700275
Marco Paniconi5b2faba2020-07-09 11:39:22 -0700276/*!\brief Set the global/frame level parameters for cyclic refresh.
277 *
278 * First call to the cyclic refresh, before encoding the frame.
279 * Sets the flag on whether cyclic refresh should be applied, sets
280 * the amount/percent of refresh, and the amount of boost applied to
281 * the two segments (set by rate_ratio_qdelta and rate_boost_fac).
282 *
283 * \ingroup cyclic_refresh
284 * \callgraph
285 * \callergraph
286 *
287 * \param[in] cpi Top level encoder structure
288 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700289 * \remark Updates the \c cpi->cyclic_refresh with the settings.
Marco Paniconi5b2faba2020-07-09 11:39:22 -0700290 */
Yaowu Xuf883b422016-08-30 14:01:10 -0700291void av1_cyclic_refresh_update_parameters(struct AV1_COMP *const cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292
Marco Paniconi5b2faba2020-07-09 11:39:22 -0700293/*!\brief Setup the cyclic background refresh.
294 *
295 * Set the delta q for the segment(s), and set the segmentation map.
296 *
297 * \ingroup cyclic_refresh
298 * \callgraph
299 * \callergraph
300 *
301 * \param[in] cpi Top level encoder structure
302 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700303 * \remark Updates the \c cpi->cyclic_refresh with the cyclic refresh
Marco Paniconi5b2faba2020-07-09 11:39:22 -0700304 * parameters and the \c cm->seg with the segmentation data.
305 */
Yaowu Xuf883b422016-08-30 14:01:10 -0700306void av1_cyclic_refresh_setup(struct AV1_COMP *const cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307
Yaowu Xuf883b422016-08-30 14:01:10 -0700308int av1_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309
Yaowu Xuf883b422016-08-30 14:01:10 -0700310void av1_cyclic_refresh_reset_resize(struct AV1_COMP *const cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700311
Marco Paniconi85b9ecb2022-02-27 23:04:58 -0800312int av1_cyclic_refresh_disable_lf_cdef(struct AV1_COMP *const cpi);
313
Yaowu Xuc27fc142016-08-22 16:08:15 -0700314static INLINE int cyclic_refresh_segment_id_boosted(int segment_id) {
315 return segment_id == CR_SEGMENT_ID_BOOST1 ||
316 segment_id == CR_SEGMENT_ID_BOOST2;
317}
318
319static INLINE int cyclic_refresh_segment_id(int segment_id) {
320 if (segment_id == CR_SEGMENT_ID_BOOST1)
321 return CR_SEGMENT_ID_BOOST1;
322 else if (segment_id == CR_SEGMENT_ID_BOOST2)
323 return CR_SEGMENT_ID_BOOST2;
324 else
325 return CR_SEGMENT_ID_BASE;
326}
327
328#ifdef __cplusplus
329} // extern "C"
330#endif
331
James Zerne1cbb132018-08-22 14:10:36 -0700332#endif // AOM_AV1_ENCODER_AQ_CYCLICREFRESH_H_