blob: 3a6e0fc6e683cd51063fd1653ee4c4ca71b2d2ee [file] [log] [blame]
Marco Paniconi5b1e4732019-08-08 18:57:53 -07001/*
2 * Copyright (c) 2019, Alliance for Open Media. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_
12#define AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_
13
Marco Paniconi6dfcab22022-10-31 00:41:42 -070014#include "aom_scale/yv12config.h"
Marco Paniconi5b1e4732019-08-08 18:57:53 -070015#include "av1/encoder/aq_cyclicrefresh.h"
16#include "av1/encoder/encoder.h"
17#include "av1/encoder/ratectrl.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
Jerome Jiang66e76242020-07-09 11:38:19 -070023/*!
24 * \brief The stucture of quantities related to each spatial and temporal layer.
25 * \ingroup SVC
26 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070027typedef struct {
Jerome Jiang66e76242020-07-09 11:38:19 -070028 /*!\cond */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070029 RATE_CONTROL rc;
Mufaddal Chakera94ee9bf2021-04-12 01:02:22 +053030 PRIMARY_RATE_CONTROL p_rc;
Marco Paniconi5b1e4732019-08-08 18:57:53 -070031 int framerate_factor;
Wan-Teh Chang122be352023-02-15 14:08:19 -080032 int64_t layer_target_bitrate; // In bits per second.
Marco Paniconi5b1e4732019-08-08 18:57:53 -070033 int scaling_factor_num;
34 int scaling_factor_den;
Jerome Jiangf54337a2019-08-15 09:28:58 -070035 int64_t target_bandwidth;
36 int64_t spatial_layer_target_bandwidth;
Marco Paniconi5b1e4732019-08-08 18:57:53 -070037 double framerate;
38 int avg_frame_size;
39 int max_q;
40 int min_q;
41 int frames_from_key_frame;
Jerome Jiang66e76242020-07-09 11:38:19 -070042 /*!\endcond */
43
44 /*!
45 * Cyclic refresh parameters (aq-mode=3), that need to be updated per-frame.
46 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070047 int sb_index;
Jerome Jiang66e76242020-07-09 11:38:19 -070048 /*!
49 * Segmentation map
50 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070051 int8_t *map;
Jerome Jiang66e76242020-07-09 11:38:19 -070052 /*!
Jerome Jiang66e76242020-07-09 11:38:19 -070053 * Number of blocks on segment 1
54 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070055 int actual_num_seg1_blocks;
Jerome Jiang66e76242020-07-09 11:38:19 -070056
57 /*!
58 * Number of blocks on segment 2
59 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070060 int actual_num_seg2_blocks;
Jerome Jiang66e76242020-07-09 11:38:19 -070061 /*!
62 * Counter used to detect scene change.
63 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070064 int counter_encode_maxq_scene_change;
Jerome Jiang66e76242020-07-09 11:38:19 -070065
66 /*!
67 * Speed settings for each layer.
68 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070069 uint8_t speed;
Jerome Jiang66e76242020-07-09 11:38:19 -070070 /*!
71 * GF group index.
72 */
Marco Paniconiefac4492019-08-26 21:03:14 -070073 unsigned char group_index;
Jerome Jiang66e76242020-07-09 11:38:19 -070074 /*!
75 * If current layer is key frame.
76 */
Marco Paniconi2b22a1f2020-06-22 21:55:02 -070077 int is_key_frame;
Marco Paniconidbc29462020-07-29 09:13:25 -070078 /*!
79 * Maximum motion magnitude of previous encoded layer.
80 */
81 int max_mv_magnitude;
Marco Paniconi5b1e4732019-08-08 18:57:53 -070082} LAYER_CONTEXT;
83
Jerome Jiang66e76242020-07-09 11:38:19 -070084/*!
85 * \brief The stucture of SVC.
86 * \ingroup SVC
87 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070088typedef struct SVC {
Jerome Jiang66e76242020-07-09 11:38:19 -070089 /*!\cond */
Marco Paniconi5b1e4732019-08-08 18:57:53 -070090 int spatial_layer_id;
91 int temporal_layer_id;
92 int number_spatial_layers;
93 int number_temporal_layers;
Marco Paniconid8bcf2a2023-01-03 15:10:27 -080094 int prev_number_spatial_layers;
Marco Paniconie5de3322021-03-22 22:03:17 -070095 int use_flexible_mode;
96 int ksvc_fixed_mode;
Jerome Jiang66e76242020-07-09 11:38:19 -070097 /*!\endcond */
98
Jerome Jiang66e76242020-07-09 11:38:19 -070099 /*!\cond */
Marco Paniconi63971322019-08-15 21:32:05 -0700100 double base_framerate;
Marco Paniconiddd56662019-08-28 15:46:56 -0700101 unsigned int current_superframe;
Marco Paniconi84cb8b32021-01-19 14:02:19 -0800102 int skip_mvsearch_last;
103 int skip_mvsearch_gf;
chiyotsaifda9d9d2022-05-18 11:56:02 -0700104 int skip_mvsearch_altref;
Marco Paniconi42e1bdd2020-06-10 16:47:39 -0700105 int spatial_layer_fb[REF_FRAMES];
106 int temporal_layer_fb[REF_FRAMES];
Fyodor Kyslov34b591f2020-06-24 20:13:58 -0700107 int num_encoded_top_layer;
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -0800108 int first_layer_denoise;
Marco Paniconi6dfcab22022-10-31 00:41:42 -0700109 YV12_BUFFER_CONFIG source_last_TL0;
110 int mi_cols_full_resoln;
111 int mi_rows_full_resoln;
Jerome Jiang66e76242020-07-09 11:38:19 -0700112 /*!\endcond */
113
114 /*!
115 * Layer context used for rate control in CBR mode.
Wan-Teh Chang122be352023-02-15 14:08:19 -0800116 * An array. The index for spatial layer `sl` and temporal layer `tl` is
117 * sl * number_temporal_layers + tl.
Jerome Jiang66e76242020-07-09 11:38:19 -0700118 */
Deepa K G5c611492022-08-21 17:39:43 +0530119 LAYER_CONTEXT *layer_context;
120
121 /*!
Wan-Teh Chang122be352023-02-15 14:08:19 -0800122 * Number of layers allocated for layer_context. If nonzero, must be greater
123 * than or equal to number_spatial_layers * number_temporal_layers.
Deepa K G5c611492022-08-21 17:39:43 +0530124 */
125 int num_allocated_layers;
Jerome Jiang66e76242020-07-09 11:38:19 -0700126
127 /*!
128 * EIGHTTAP_SMOOTH or BILINEAR
129 */
Jerome Jiang790190e2020-06-12 16:47:45 -0700130 InterpFilter downsample_filter_type[AOM_MAX_SS_LAYERS];
Jerome Jiang66e76242020-07-09 11:38:19 -0700131
132 /*!
133 * Downsample_filter_phase: = 0 will do sub-sampling (no weighted average),
134 * = 8 will center the target pixel and get a symmetric averaging filter.
135 */
Jerome Jiang790190e2020-06-12 16:47:45 -0700136 int downsample_filter_phase[AOM_MAX_SS_LAYERS];
Jerome Jiang66e76242020-07-09 11:38:19 -0700137
138 /*!
139 * Force zero-mv in mode search for the spatial/inter-layer reference.
140 */
Marco Paniconi2b22a1f2020-06-22 21:55:02 -0700141 int force_zero_mode_spatial_ref;
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700142} SVC;
143
144struct AV1_COMP;
Marco Paniconi6dfcab22022-10-31 00:41:42 -0700145struct EncodeFrameInput;
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700146
Jerome Jiang66e76242020-07-09 11:38:19 -0700147/*!\brief Initialize layer context data from init_config().
148 *
149 * \ingroup SVC
150 * \callgraph
151 * \callergraph
152 *
153 * \param[in] cpi Top level encoder structure
154 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700155 * \remark Nothing returned. Set cpi->svc.
Jerome Jiang66e76242020-07-09 11:38:19 -0700156 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700157void av1_init_layer_context(struct AV1_COMP *const cpi);
158
Deepa K G5c611492022-08-21 17:39:43 +0530159/*!\brief Allocate layer context data.
160 *
161 * \ingroup SVC
162 * \callgraph
163 * \callergraph
164 *
165 * \param[in] cpi Top level encoder structure
166 * \param[in] num_layers Number of layers to be allocated
167 *
James Zernff135dc2022-12-06 15:23:51 -0800168 * \remark Allocates memory for cpi->svc.layer_context.
169 * \return True on success, false on allocation failure.
Deepa K G5c611492022-08-21 17:39:43 +0530170 */
James Zernff135dc2022-12-06 15:23:51 -0800171bool av1_alloc_layer_context(struct AV1_COMP *cpi, int num_layers);
Deepa K G5c611492022-08-21 17:39:43 +0530172
Jerome Jiang66e76242020-07-09 11:38:19 -0700173/*!\brief Update the layer context from a change_config() call.
174 *
175 * \ingroup SVC
176 * \callgraph
177 * \callergraph
178 *
179 * \param[in] cpi Top level encoder structure
180 * \param[in] target_bandwidth Total target bandwidth
181 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700182 * \remark Nothing returned. Buffer level for each layer is set.
Jerome Jiang66e76242020-07-09 11:38:19 -0700183 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700184void av1_update_layer_context_change_config(struct AV1_COMP *const cpi,
Jerome Jiangf54337a2019-08-15 09:28:58 -0700185 const int64_t target_bandwidth);
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700186
Jerome Jiang66e76242020-07-09 11:38:19 -0700187/*!\brief Prior to encoding the frame, update framerate-related quantities
188 for the current temporal layer.
189 *
190 * \ingroup SVC
191 * \callgraph
192 * \callergraph
193 *
194 * \param[in] cpi Top level encoder structure
195 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700196 * \remark Nothing returned. Frame related quantities for current temporal
Jerome Jiang66e76242020-07-09 11:38:19 -0700197 layer are updated.
198 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700199void av1_update_temporal_layer_framerate(struct AV1_COMP *const cpi);
200
Jerome Jiang66e76242020-07-09 11:38:19 -0700201/*!\brief Prior to encoding the frame, set the layer context, for the current
202 layer to be encoded, to the cpi struct.
203 *
204 * \ingroup SVC
205 * \callgraph
206 * \callergraph
207 *
208 * \param[in] cpi Top level encoder structure
209 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700210 * \remark Nothing returned. Layer context for current layer is set.
Jerome Jiang66e76242020-07-09 11:38:19 -0700211 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700212void av1_restore_layer_context(struct AV1_COMP *const cpi);
213
Jerome Jiang66e76242020-07-09 11:38:19 -0700214/*!\brief Save the layer context after encoding the frame.
215 *
216 * \ingroup SVC
217 * \callgraph
218 * \callergraph
219 *
220 * \param[in] cpi Top level encoder structure
Jerome Jiang66e76242020-07-09 11:38:19 -0700221 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700222void av1_save_layer_context(struct AV1_COMP *const cpi);
223
Jerome Jiang66e76242020-07-09 11:38:19 -0700224/*!\brief Free the memory used for cyclic refresh in layer context.
225 *
226 * \ingroup SVC
227 * \callgraph
228 * \callergraph
229 *
230 * \param[in] cpi Top level encoder structure
Jerome Jiang66e76242020-07-09 11:38:19 -0700231 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700232void av1_free_svc_cyclic_refresh(struct AV1_COMP *const cpi);
233
Jerome Jiang66e76242020-07-09 11:38:19 -0700234/*!\brief Reset on key frame: reset counters, references and buffer updates.
235 *
236 * \ingroup SVC
237 * \callgraph
238 * \callergraph
239 *
240 * \param[in] cpi Top level encoder structure
241 * \param[in] is_key Whether current layer is key frame
Jerome Jiang66e76242020-07-09 11:38:19 -0700242 */
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700243void av1_svc_reset_temporal_layers(struct AV1_COMP *const cpi, int is_key);
244
Jerome Jiang66e76242020-07-09 11:38:19 -0700245/*!\brief Before encoding, set resolutions and allocate compressor data.
246 *
247 * \ingroup SVC
248 * \callgraph
249 * \callergraph
250 *
251 * \param[in] cpi Top level encoder structure
Jerome Jiang66e76242020-07-09 11:38:19 -0700252 */
Marco Paniconi63971322019-08-15 21:32:05 -0700253void av1_one_pass_cbr_svc_start_layer(struct AV1_COMP *const cpi);
254
Jerome Jiang66e76242020-07-09 11:38:19 -0700255/*!\brief Get primary reference frame for current layer
256 *
257 * \ingroup SVC
258 * \callgraph
259 * \callergraph
260 *
261 * \param[in] cpi Top level encoder structure
262 *
263 * \return The primary reference frame for current layer.
264 */
Marco Paniconi42e1bdd2020-06-10 16:47:39 -0700265int av1_svc_primary_ref_frame(const struct AV1_COMP *const cpi);
266
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -0800267/*!\brief Get resolution for current layer.
268 *
269 * \ingroup SVC
270 * \param[in] width_org Original width, unscaled
271 * \param[in] height_org Original height, unscaled
272 * \param[in] num Numerator for the scale ratio
273 * \param[in] den Denominator for the scale ratio
274 * \param[in] width_out Output width, scaled for current layer
275 * \param[in] height_out Output height, scaled for current layer
276 *
Wan-Teh Chang0defff82022-08-23 14:17:01 -0700277 * \remark Nothing is returned. Instead the scaled width and height are set.
Fyodor Kyslovb790c9c2021-01-12 15:33:36 -0800278 */
279void av1_get_layer_resolution(const int width_org, const int height_org,
280 const int num, const int den, int *width_out,
281 int *height_out);
Marco Paniconie5de3322021-03-22 22:03:17 -0700282
283void av1_set_svc_fixed_mode(struct AV1_COMP *const cpi);
284
Marco Paniconi2b52ff12021-04-29 11:29:37 -0700285void av1_svc_check_reset_layer_rc_flag(struct AV1_COMP *const cpi);
286
Marco Paniconi6dfcab22022-10-31 00:41:42 -0700287void av1_svc_set_last_source(struct AV1_COMP *const cpi,
288 struct EncodeFrameInput *frame_input,
289 YV12_BUFFER_CONFIG *prev_source);
Marco Paniconib5b1be82022-10-22 23:37:42 -0700290
Marco Paniconi42b92732023-02-07 00:07:13 -0800291void av1_svc_update_buffer_slot_refreshed(struct AV1_COMP *const cpi);
292
293int av1_svc_get_min_ref_dist(const struct AV1_COMP *cpi);
294
295void av1_svc_set_reference_was_previous(struct AV1_COMP *cpi);
Marco Paniconi5b1e4732019-08-08 18:57:53 -0700296#ifdef __cplusplus
297} // extern "C"
298#endif
299
300#endif // AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_