1 /* 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 14 #include "aom_scale/yv12config.h" 15 #include "av1/encoder/aq_cyclicrefresh.h" 16 #include "av1/encoder/encoder.h" 17 #include "av1/encoder/ratectrl.h" 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 /*! 24 * \brief The stucture of quantities related to each spatial and temporal layer. 25 * \ingroup SVC 26 */ 27 typedef struct { 28 /*!\cond */ 29 RATE_CONTROL rc; 30 PRIMARY_RATE_CONTROL p_rc; 31 int framerate_factor; 32 int64_t layer_target_bitrate; // In bits per second. 33 int scaling_factor_num; 34 int scaling_factor_den; 35 int64_t target_bandwidth; 36 int64_t spatial_layer_target_bandwidth; 37 double framerate; 38 int avg_frame_size; 39 int max_q; 40 int min_q; 41 int frames_from_key_frame; 42 /*!\endcond */ 43 44 /*! 45 * Cyclic refresh parameters (aq-mode=3), that need to be updated per-frame. 46 */ 47 int sb_index; 48 /*! 49 * Segmentation map 50 */ 51 int8_t *map; 52 /*! 53 * Number of blocks on segment 1 54 */ 55 int actual_num_seg1_blocks; 56 57 /*! 58 * Number of blocks on segment 2 59 */ 60 int actual_num_seg2_blocks; 61 /*! 62 * Counter used to detect scene change. 63 */ 64 int counter_encode_maxq_scene_change; 65 66 /*! 67 * Speed settings for each layer. 68 */ 69 uint8_t speed; 70 /*! 71 * GF group index. 72 */ 73 unsigned char group_index; 74 /*! 75 * If current layer is key frame. 76 */ 77 int is_key_frame; 78 /*! 79 * Maximum motion magnitude of previous encoded layer. 80 */ 81 int max_mv_magnitude; 82 } LAYER_CONTEXT; 83 84 /*! 85 * \brief The stucture of SVC. 86 * \ingroup SVC 87 */ 88 typedef struct SVC { 89 /*!\cond */ 90 int spatial_layer_id; 91 int temporal_layer_id; 92 int number_spatial_layers; 93 int number_temporal_layers; 94 int prev_number_spatial_layers; 95 int use_flexible_mode; 96 int ksvc_fixed_mode; 97 /*!\endcond */ 98 99 /*!\cond */ 100 double base_framerate; 101 unsigned int current_superframe; 102 int skip_mvsearch_last; 103 int skip_mvsearch_gf; 104 int skip_mvsearch_altref; 105 int spatial_layer_fb[REF_FRAMES]; 106 int temporal_layer_fb[REF_FRAMES]; 107 int num_encoded_top_layer; 108 int first_layer_denoise; 109 YV12_BUFFER_CONFIG source_last_TL0; 110 int mi_cols_full_resoln; 111 int mi_rows_full_resoln; 112 /*!\endcond */ 113 114 /*! 115 * Layer context used for rate control in CBR mode. 116 * An array. The index for spatial layer `sl` and temporal layer `tl` is 117 * sl * number_temporal_layers + tl. 118 */ 119 LAYER_CONTEXT *layer_context; 120 121 /*! 122 * Number of layers allocated for layer_context. If nonzero, must be greater 123 * than or equal to number_spatial_layers * number_temporal_layers. 124 */ 125 int num_allocated_layers; 126 127 /*! 128 * EIGHTTAP_SMOOTH or BILINEAR 129 */ 130 InterpFilter downsample_filter_type[AOM_MAX_SS_LAYERS]; 131 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 */ 136 int downsample_filter_phase[AOM_MAX_SS_LAYERS]; 137 138 /*! 139 * Force zero-mv in mode search for the spatial/inter-layer reference. 140 */ 141 int force_zero_mode_spatial_ref; 142 143 /*! 144 * Flag to indicate that current spatial layer has a lower quality layer 145 * (at the same timestamp) that can be used as a reference. 146 * Lower quality layer refers to the same resolution but encoded at 147 * different/lower bitrate. 148 */ 149 int has_lower_quality_layer; 150 151 /*! 152 * Flag to indicate the frame drop mode for SVC: one of the two settings: 153 * AOM_LAYER_DROP (default) or AOM_FULL_SUPERFRAME_DROP. 154 */ 155 AOM_SVC_FRAME_DROP_MODE framedrop_mode; 156 157 /*! 158 * Flag to indicate if frame was dropped for a given spatial_layer_id on 159 * previous superframe. 160 */ 161 bool last_layer_dropped[AOM_MAX_SS_LAYERS]; 162 163 /*! 164 * Flag to indicate if a previous spatial was dropped for the same superframe. 165 */ 166 bool drop_spatial_layer[AOM_MAX_SS_LAYERS]; 167 } SVC; 168 169 struct AV1_COMP; 170 struct EncodeFrameInput; 171 172 /*!\brief Initialize layer context data from init_config(). 173 * 174 * \ingroup SVC 175 * \callgraph 176 * \callergraph 177 * 178 * \param[in] cpi Top level encoder structure 179 * 180 * \remark Nothing returned. Set cpi->svc. 181 */ 182 void av1_init_layer_context(struct AV1_COMP *const cpi); 183 184 /*!\brief Allocate layer context data. 185 * 186 * \ingroup SVC 187 * \callgraph 188 * \callergraph 189 * 190 * \param[in] cpi Top level encoder structure 191 * \param[in] num_layers Number of layers to be allocated 192 * 193 * \remark Allocates memory for cpi->svc.layer_context. 194 * \return True on success, false on allocation failure. 195 */ 196 bool av1_alloc_layer_context(struct AV1_COMP *cpi, int num_layers); 197 198 /*!\brief Update the layer context from a change_config() call. 199 * 200 * \ingroup SVC 201 * \callgraph 202 * \callergraph 203 * 204 * \param[in] cpi Top level encoder structure 205 * \param[in] target_bandwidth Total target bandwidth 206 * 207 * \remark Nothing returned. Buffer level for each layer is set. 208 */ 209 void av1_update_layer_context_change_config(struct AV1_COMP *const cpi, 210 const int64_t target_bandwidth); 211 212 /*!\brief Prior to encoding the frame, update framerate-related quantities 213 for the current temporal layer. 214 * 215 * \ingroup SVC 216 * \callgraph 217 * \callergraph 218 * 219 * \param[in] cpi Top level encoder structure 220 * 221 * \remark Nothing returned. Frame related quantities for current temporal 222 layer are updated. 223 */ 224 void av1_update_temporal_layer_framerate(struct AV1_COMP *const cpi); 225 226 /*!\brief Prior to check if reference is lower spatial layer at the same 227 * timestamp/superframe. 228 * 229 * \ingroup SVC 230 * \callgraph 231 * \callergraph 232 * 233 * \param[in] cpi Top level encoder structure 234 * \param[in] ref_frame Reference frame 235 * 236 * \return True if the ref_frame if lower spatial layer, otherwise false. 237 */ 238 bool av1_check_ref_is_low_spatial_res_super_frame(struct AV1_COMP *const cpi, 239 int ref_frame); 240 241 /*!\brief Prior to encoding the frame, set the layer context, for the current 242 layer to be encoded, to the cpi struct. 243 * 244 * \ingroup SVC 245 * \callgraph 246 * \callergraph 247 * 248 * \param[in] cpi Top level encoder structure 249 * 250 * \remark Nothing returned. Layer context for current layer is set. 251 */ 252 void av1_restore_layer_context(struct AV1_COMP *const cpi); 253 254 /*!\brief Save the layer context after encoding the frame. 255 * 256 * \ingroup SVC 257 * \callgraph 258 * \callergraph 259 * 260 * \param[in] cpi Top level encoder structure 261 */ 262 void av1_save_layer_context(struct AV1_COMP *const cpi); 263 264 /*!\brief Free the memory used for cyclic refresh in layer context. 265 * 266 * \ingroup SVC 267 * \callgraph 268 * \callergraph 269 * 270 * \param[in] cpi Top level encoder structure 271 */ 272 void av1_free_svc_cyclic_refresh(struct AV1_COMP *const cpi); 273 274 /*!\brief Reset on key frame: reset counters, references and buffer updates. 275 * 276 * \ingroup SVC 277 * \callgraph 278 * \callergraph 279 * 280 * \param[in] cpi Top level encoder structure 281 * \param[in] is_key Whether current layer is key frame 282 */ 283 void av1_svc_reset_temporal_layers(struct AV1_COMP *const cpi, int is_key); 284 285 /*!\brief Before encoding, set resolutions and allocate compressor data. 286 * 287 * \ingroup SVC 288 * \callgraph 289 * \callergraph 290 * 291 * \param[in] cpi Top level encoder structure 292 */ 293 void av1_one_pass_cbr_svc_start_layer(struct AV1_COMP *const cpi); 294 295 /*!\brief Get primary reference frame for current layer 296 * 297 * \ingroup SVC 298 * \callgraph 299 * \callergraph 300 * 301 * \param[in] cpi Top level encoder structure 302 * 303 * \return The primary reference frame for current layer. 304 */ 305 int av1_svc_primary_ref_frame(const struct AV1_COMP *const cpi); 306 307 /*!\brief Get resolution for current layer. 308 * 309 * \ingroup SVC 310 * \param[in] width_org Original width, unscaled 311 * \param[in] height_org Original height, unscaled 312 * \param[in] num Numerator for the scale ratio 313 * \param[in] den Denominator for the scale ratio 314 * \param[in] width_out Output width, scaled for current layer 315 * \param[in] height_out Output height, scaled for current layer 316 * 317 * \remark Nothing is returned. Instead the scaled width and height are set. 318 */ 319 void av1_get_layer_resolution(const int width_org, const int height_org, 320 const int num, const int den, int *width_out, 321 int *height_out); 322 323 void av1_set_svc_fixed_mode(struct AV1_COMP *const cpi); 324 325 void av1_svc_check_reset_layer_rc_flag(struct AV1_COMP *const cpi); 326 327 void av1_svc_set_last_source(struct AV1_COMP *const cpi, 328 struct EncodeFrameInput *frame_input, 329 YV12_BUFFER_CONFIG *prev_source); 330 331 void av1_svc_update_buffer_slot_refreshed(struct AV1_COMP *const cpi); 332 333 int av1_svc_get_min_ref_dist(const struct AV1_COMP *cpi); 334 335 void av1_svc_set_reference_was_previous(struct AV1_COMP *cpi); 336 #ifdef __cplusplus 337 } // extern "C" 338 #endif 339 340 #endif // AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_ 341