tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

svc_layercontext.h (9445B)


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