/* * Copyright (c) 2016, Alliance for Open Media. All rights reserved. * * This source code is subject to the terms of the BSD 2 Clause License and * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License * was not distributed with this source code in the LICENSE file, you can * obtain it at www.aomedia.org/license/software. If the Alliance for Open * Media Patent License 1.0 was not distributed with this source code in the * PATENTS file, you can obtain it at www.aomedia.org/license/patent. */ #ifndef AOM_AV1_ENCODER_RATECTRL_H_ #define AOM_AV1_ENCODER_RATECTRL_H_ #include "aom/aom_codec.h" #include "aom/aom_integer.h" #include "aom_ports/mem.h" #include "av1/common/av1_common_int.h" #include "av1/common/blockd.h" #ifdef __cplusplus extern "C" { #endif /*!\cond */ // Bits Per MB at different Q (Multiplied by 512) #define BPER_MB_NORMBITS … // Use this macro to turn on/off use of alt-refs in one-pass mode. #define USE_ALTREF_FOR_ONE_PASS … // Threshold used to define if a KF group is static (e.g. a slide show). // Essentially, this means that no frame in the group has more than 1% of MBs // that are not marked as coded with 0,0 motion in the first pass. #define STATIC_KF_GROUP_THRESH … #define STATIC_KF_GROUP_FLOAT_THRESH … // The maximum duration of a GF group that is static (e.g. a slide show). #define MAX_STATIC_GF_GROUP_LENGTH … #define MIN_GF_INTERVAL … #define MAX_GF_INTERVAL … #define FIXED_GF_INTERVAL … #define MAX_GF_LENGTH_LAP … #define FIXED_GF_INTERVAL_RT … #define MAX_GF_INTERVAL_RT … #define MAX_NUM_GF_INTERVALS … #define MAX_ARF_LAYERS … // #define STRICT_RC #define DEFAULT_KF_BOOST_RT … #define DEFAULT_GF_BOOST_RT … // A passive rate control strategy for screen content type in real-time mode. // When it is turned on, the compression performance is improved by // 7.8% (overall_psnr), 5.0% (VMAF) on average. Some clips see gains // over 20% on metric. // The downside is that it does not guarantee frame size. // Since RT mode has a tight restriction on buffer overflow control, we // turn it off by default. #define RT_PASSIVE_STRATEGY … #define MAX_Q_HISTORY … size_params_type; enum { … } UENUM1BYTE(…) …; enum { … } UENUM1BYTE(…) …; enum { … } UENUM1BYTE(…) …; RESIZE_ACTION; RESIZE_STATE; #define MAX_FIRSTPASS_ANALYSIS_FRAMES … REGION_TYPES; REGIONS; /*!\endcond */ /*! * \brief Rate Control parameters and status */ RATE_CONTROL; /*! * \brief Primary Rate Control parameters and status */ PRIMARY_RATE_CONTROL; /*!\cond */ struct AV1_COMP; struct AV1EncoderConfig; struct GF_GROUP; void av1_primary_rc_init(const struct AV1EncoderConfig *oxcf, PRIMARY_RATE_CONTROL *p_rc); void av1_rc_init(const struct AV1EncoderConfig *oxcf, RATE_CONTROL *rc); int av1_estimate_bits_at_q(const struct AV1_COMP *cpi, int q, double correction_factor); double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth); void av1_rc_init_minq_luts(void); int av1_rc_get_default_min_gf_interval(int width, int height, double framerate); // Generally at the high level, the following flow is expected // to be enforced for rate control: // First call per frame, one of: // av1_get_one_pass_rt_params() // av1_get_second_pass_params() // depending on the usage to set the rate control encode parameters desired. // // Then, call encode_frame_to_data_rate() to perform the // actual encode. This function will in turn call encode_frame() // one or more times, followed by: // av1_rc_postencode_update_drop_frame() // // The majority of rate control parameters are only expected // to be set in the av1_get_..._params() functions and // updated during the av1_rc_postencode_update...() functions. // The only exceptions are av1_rc_drop_frame() and // av1_rc_update_rate_correction_factors() functions. // Functions to set parameters for encoding before the actual // encode_frame_to_data_rate() function. struct EncodeFrameInput; // Post encode update of the rate control parameters based // on bytes used void av1_rc_postencode_update(struct AV1_COMP *cpi, uint64_t bytes_used); // Post encode update of the rate control parameters for dropped frames void av1_rc_postencode_update_drop_frame(struct AV1_COMP *cpi); /*!\endcond */ /*!\brief Updates the rate correction factor linking Q to output bits * * This function updates the Q rate correction factor after an encode * cycle depending on whether we overshot or undershot the target rate. * * \ingroup rate_control * \param[in] cpi Top level encoder instance structure * \param[in] is_encode_stage Indicates if recode loop or post-encode * \param[in] width Frame width * \param[in] height Frame height * * \remark Updates the relevant rate correction factor in cpi->rc */ void av1_rc_update_rate_correction_factors(struct AV1_COMP *cpi, int is_encode_stage, int width, int height); /*!\cond */ // Decide if we should drop this frame: For 1-pass CBR. // Changes only the decimation count in the rate control structure int av1_rc_drop_frame(struct AV1_COMP *cpi); // Computes frame size bounds. void av1_rc_compute_frame_size_bounds(const struct AV1_COMP *cpi, int this_frame_target, int *frame_under_shoot_limit, int *frame_over_shoot_limit); /*!\endcond */ /*!\brief Picks q and q bounds given the rate control parameters in \c cpi->rc. * * \ingroup rate_control * \param[in] cpi Top level encoder structure * \param[in] width Coded frame width * \param[in] height Coded frame height * \param[in] gf_index Index of this frame in the golden frame group * \param[out] bottom_index Bottom bound for q index (best quality) * \param[out] top_index Top bound for q index (worst quality) * \return Returns selected q index to be used for encoding this frame. * Also, updates \c rc->arf_q. */ int av1_rc_pick_q_and_bounds(struct AV1_COMP *cpi, int width, int height, int gf_index, int *bottom_index, int *top_index); /*!\brief Estimates q to achieve a target bits per frame * * \ingroup rate_control * \param[in] cpi Top level encoder instance structure * \param[in] target_bits_per_frame Frame rate target * \param[in] active_worst_quality Max Q allowed * \param[in] active_best_quality Min Q allowed * \param[in] width Frame width * \param[in] height Frame height * * \return Returns a q index value */ int av1_rc_regulate_q(const struct AV1_COMP *cpi, int target_bits_per_frame, int active_best_quality, int active_worst_quality, int width, int height); /*!\cond */ // Estimates bits per mb for a given qindex and correction factor. int av1_rc_bits_per_mb(const struct AV1_COMP *cpi, FRAME_TYPE frame_type, int qindex, double correction_factor, int accurate_estimate); // Find q_index corresponding to desired_q, within [best_qindex, worst_qindex]. // To be precise, 'q_index' is the smallest integer, for which the corresponding // q >= desired_q. // If no such q index is found, returns 'worst_qindex'. int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth, int best_qindex, int worst_qindex); // Computes a q delta (in "q index" terms) to get from a starting q value // to a target q value int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, aom_bit_depth_t bit_depth); // Computes a q delta (in "q index" terms) to get from a starting q value // to a value that should equate to the given rate ratio. int av1_compute_qdelta_by_rate(const struct AV1_COMP *cpi, FRAME_TYPE frame_type, int qindex, double rate_target_ratio); void av1_rc_update_framerate(struct AV1_COMP *cpi, int width, int height); void av1_set_target_rate(struct AV1_COMP *cpi, int width, int height); int av1_resize_one_pass_cbr(struct AV1_COMP *cpi); void av1_rc_set_frame_target(struct AV1_COMP *cpi, int target, int width, int height); void av1_adjust_gf_refresh_qp_one_pass_rt(struct AV1_COMP *cpi); void av1_set_rtc_reference_structure_one_layer(struct AV1_COMP *cpi, int gf_update); /*!\endcond */ /*!\brief Calculates how many bits to use for a P frame in one pass vbr * * \ingroup rate_control * \callgraph * \callergraph * * \param[in] cpi Top level encoder structure * \param[in] frame_update_type Type of frame * * \return Returns the target number of bits for this frame. */ int av1_calc_pframe_target_size_one_pass_vbr( const struct AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type); /*!\brief Calculates how many bits to use for an i frame in one pass vbr * * \ingroup rate_control * \callgraph * \callergraph * * \param[in] cpi Top level encoder structure * * \return Returns the target number of bits for this frame. */ int av1_calc_iframe_target_size_one_pass_vbr(const struct AV1_COMP *const cpi); /*!\brief Calculates how many bits to use for a P frame in one pass cbr * * \ingroup rate_control * \callgraph * \callergraph * * \param[in] cpi Top level encoder structure * \param[in] frame_update_type Type of frame * * \return Returns the target number of bits for this frame. */ int av1_calc_pframe_target_size_one_pass_cbr( const struct AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type); /*!\brief Calculates how many bits to use for an i frame in one pass cbr * * \ingroup rate_control * \callgraph * \callergraph * * \param[in] cpi Top level encoder structure * * \return Returns the target number of bits for this frame. */ int av1_calc_iframe_target_size_one_pass_cbr(const struct AV1_COMP *cpi); /*!\brief Setup the rate control parameters for 1 pass real-time mode. * * - Sets the frame type and target frame size. * - Sets the GF update. * - Checks for scene change. * - Sets the reference prediction structure for 1 layers (non-SVC). * - Resets and updates are done for SVC. * * \ingroup rate_control * \param[in] cpi Top level encoder structure * \param[in] frame_type Encoder frame type * \param[in] frame_input Current and last input source frames * \param[in] frame_flags Encoder frame flags * * \remark Nothing is returned. Instead the settings computed in this * function are set in: \c frame_params, \c cpi->common, \c cpi->rc, * \c cpi->svc. */ void av1_get_one_pass_rt_params(struct AV1_COMP *cpi, FRAME_TYPE *const frame_type, const struct EncodeFrameInput *frame_input, unsigned int frame_flags); /*!\brief Increase q on expected encoder overshoot, for CBR mode. * * Handles the case when encoder is expected to create a large frame: * - q is increased to value closer to \c cpi->rc.worst_quality * - avg_frame_qindex is reset * - buffer levels are reset * - rate correction factor is adjusted * * \ingroup rate_control * \param[in] cpi Top level encoder structure * \param[in] q Current q index * * \return q is returned, and updates are done to \c cpi->rc. */ int av1_encodedframe_overshoot_cbr(struct AV1_COMP *cpi, int *q); /*!\brief Check if frame should be dropped, for RTC mode. * * \ingroup rate_control * \param[in] cpi Top level encoder structure * \param[in,out] size Size of encoded frame * * \return 1 if frame is to be dropped, 0 otherwise (no drop). * Set cpi->rc.force_max_q if frame is to be dropped, and updates are * made to rate control parameters. *size is set to 0 when this * function returns 1 (frame is dropped). */ int av1_postencode_drop_cbr(struct AV1_COMP *cpi, size_t *size); /*!\brief Compute the q_indices for a single frame. * * Intended to be used with AOM_Q mode. * * \param[in] base_q_index Base q index * \param[in] gf_update_type GOP update type * \param[in] gf_pyramid_level GOP level of the current frame * \param[in] arf_q ARF q_index * * \return Returns the q_index for the current frame. */ int av1_q_mode_get_q_index(int base_q_index, int gf_update_type, int gf_pyramid_level, int arf_q); #ifdef __cplusplus } // extern "C" #endif #endif // AOM_AV1_ENCODER_RATECTRL_H_