AOMedia AV1 Codec
encode_strategy.h
Go to the documentation of this file.
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 
15 #ifndef AOM_AV1_ENCODER_ENCODE_STRATEGY_H_
16 #define AOM_AV1_ENCODER_ENCODE_STRATEGY_H_
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <stdint.h>
23 
24 #include "aom/aom_encoder.h"
25 
26 #include "av1/encoder/encoder.h"
27 #include "av1/encoder/firstpass.h"
28 
55 int av1_encode_strategy(AV1_COMP *const cpi, size_t *const size,
56  uint8_t *const dest, unsigned int *frame_flags,
57  int64_t *const time_stamp, int64_t *const time_end,
58  const aom_rational64_t *const timestamp_ratio,
59  int *const pop_lookahead, int flush);
60 
62 // Set individual buffer update flags based on frame reference type.
63 // force_refresh_all is used when we have a KEY_FRAME or S_FRAME. It forces all
64 // refresh_*_frame flags to be set, because we refresh all buffers in this case.
65 void av1_configure_buffer_updates(AV1_COMP *const cpi,
66  RefreshFrameInfo *const refresh_frame,
67  const FRAME_UPDATE_TYPE type,
68  const REFBUF_STATE refbuf_state,
69  int force_refresh_all);
70 
71 int av1_get_refresh_frame_flags(const AV1_COMP *const cpi,
72  const EncodeFrameParams *const frame_params,
73  FRAME_UPDATE_TYPE frame_update_type,
74  int gf_index,
75 #if CONFIG_FRAME_PARALLEL_ENCODE
76  int cur_disp_order,
77  RefFrameMapPair ref_frame_map_pairs[REF_FRAMES],
78 #endif // CONFIG_FRAME_PARALLEL_ENCODE
79  const RefBufferStack *const ref_buffer_stack);
80 
81 int av1_get_refresh_ref_frame_map(int refresh_frame_flags);
82 
83 void av1_update_ref_frame_map(const AV1_COMP *cpi,
84  FRAME_UPDATE_TYPE frame_update_type,
85  REFBUF_STATE refbuf_state, int ref_map_index,
86  RefBufferStack *ref_buffer_stack);
87 
100 void av1_get_ref_frames(const RefBufferStack *ref_buffer_stack,
101 #if CONFIG_FRAME_PARALLEL_ENCODE
102  RefFrameMapPair ref_frame_map_pairs[REF_FRAMES],
103  int cur_frame_disp,
104 #if CONFIG_FRAME_PARALLEL_ENCODE_2
105  const AV1_COMP *cpi, int gf_index,
106  int is_parallel_encode,
107 #endif // CONFIG_FRAME_PARALLEL_ENCODE_2
108 #endif // CONFIG_FRAME_PARALLEL_ENCODE
109  int remapped_ref_idx[REF_FRAMES]);
110 
111 int is_forced_keyframe_pending(struct lookahead_ctx *lookahead,
112  const int up_to_index,
113  const COMPRESSOR_STAGE compressor_stage);
114 
115 static AOM_INLINE int is_frame_droppable(
116  const SVC *const svc,
117  const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags) {
118  // Droppable frame is only used by external refresh flags. VoD setting won't
119  // trigger its use case.
120  if (svc->set_ref_frame_config)
121  return svc->non_reference_frame;
122  else if (ext_refresh_frame_flags->update_pending)
123  return !(ext_refresh_frame_flags->alt_ref_frame ||
124  ext_refresh_frame_flags->alt2_ref_frame ||
125  ext_refresh_frame_flags->bwd_ref_frame ||
126  ext_refresh_frame_flags->golden_frame ||
127  ext_refresh_frame_flags->last_frame);
128  else
129  return 0;
130 }
131 
132 static AOM_INLINE int get_current_frame_ref_type(const AV1_COMP *const cpi) {
133  // We choose the reference "type" of this frame from the flags which indicate
134  // which reference frames will be refreshed by it. More than one of these
135  // flags may be set, so the order here implies an order of precedence. This is
136  // just used to choose the primary_ref_frame (as the most recent reference
137  // buffer of the same reference-type as the current frame).
138 
139  switch (cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index]) {
140  case 0: return 0;
141  case 1: return 1;
142  case MAX_ARF_LAYERS:
143  case MAX_ARF_LAYERS + 1: return 4;
144  default: return 7;
145  }
146 }
147 
148 #if CONFIG_FRAME_PARALLEL_ENCODE
149 #if CONFIG_FRAME_PARALLEL_ENCODE_2
150 int av1_calc_refresh_idx_for_intnl_arf(
151  AV1_COMP *cpi, RefFrameMapPair ref_frame_map_pairs[REF_FRAMES],
152  int gf_index);
153 #endif // CONFIG_FRAME_PARALLEL_ENCODE_2
154 #endif // CONFIG_FRAME_PARALLEL_ENCODE
155 
156 #ifdef __cplusplus
157 } // extern "C"
158 #endif
159 
160 #endif // AOM_AV1_ENCODER_ENCODE_STRATEGY_H_
SPEED_FEATURES::rt_sf
REAL_TIME_SPEED_FEATURES rt_sf
Definition: speed_features.h:1457
encoder.h
Declares top-level encoder structures and functions.
FeatureFlags::interp_filter
InterpFilter interp_filter
Definition: av1_common_int.h:411
av1_rc_pick_q_and_bounds
int av1_rc_pick_q_and_bounds(const struct AV1_COMP *cpi, int width, int height, int gf_index, int *bottom_index, int *top_index)
Picks q and q bounds given the rate control parameters in cpi->rc.
av1_cyclic_refresh_update_parameters
void av1_cyclic_refresh_update_parameters(struct AV1_COMP *const cpi)
Set the global/frame level parameters for cyclic refresh.
ExtRefreshFrameFlagsInfo::alt_ref_frame
bool alt_ref_frame
Definition: encoder.h:2084
AV1_COMP::ext_flags
ExternalFlags ext_flags
Definition: encoder.h:2765
EncodeFrameParams::ref_frame_flags
int ref_frame_flags
Definition: encoder.h:3332
GF_GROUP
Data related to the current GF/ARF group and the individual frames within the group.
Definition: firstpass.h:344
av1_check_show_filtered_frame
int av1_check_show_filtered_frame(const YV12_BUFFER_CONFIG *frame, const FRAME_DIFF *frame_diff, int q_index, aom_bit_depth_t bit_depth)
Check whether a filtered frame can be show directly.
av1_get_second_pass_params
void av1_get_second_pass_params(struct AV1_COMP *cpi, struct EncodeFrameParams *const frame_params, unsigned int frame_flags)
Main per frame entry point for second pass of two pass encode.
Definition: pass2_strategy.c:3607
AOM_EFLAG_SET_S_FRAME
#define AOM_EFLAG_SET_S_FRAME
Enable s frame mode.
Definition: aomcx.h:159
ExternalFlags::use_error_resilient
bool use_error_resilient
Definition: encoder.h:2124
ExternalFlags::refresh_frame
ExtRefreshFrameFlagsInfo refresh_frame
Definition: encoder.h:2103
RefreshFrameInfo::alt_ref_frame
bool alt_ref_frame
Definition: encoder.h:1978
av1_set_reference_structure_one_pass_rt
void av1_set_reference_structure_one_pass_rt(AV1_COMP *cpi, int gf_update)
Setup the reference prediction structure for 1 pass real-time.
Definition: ratectrl.c:2556
FeatureFlags::error_resilient_mode
bool error_resilient_mode
Definition: av1_common_int.h:404
AlgoCfg::enable_tpl_model
bool enable_tpl_model
Definition: encoder.h:833
av1_gop_is_second_arf
int av1_gop_is_second_arf(const GF_GROUP *gf_group, int gf_frame_index)
Check whether a frame in the GOP is the second arf.
AV1Common::tiles
CommonTileParams tiles
Definition: av1_common_int.h:997
AV1_PRIMARY::twopass
TWO_PASS twopass
Definition: encoder.h:2483
AV1_COMP::sf
SPEED_FEATURES sf
Definition: encoder.h:2833
AV1_PRIMARY::number_spatial_layers
unsigned int number_spatial_layers
Definition: encoder.h:2518
ExternalFlags::use_s_frame
bool use_s_frame
Definition: encoder.h:2129
AV1_COMP::td
ThreadData td
Definition: encoder.h:2647
AV1_COMP::gf_frame_index
unsigned char gf_frame_index
Definition: encoder.h:2864
AV1_COMP::compressor_stage
COMPRESSOR_STAGE compressor_stage
Definition: encoder.h:3125
ExternalFlags::use_primary_ref_none
bool use_primary_ref_none
Definition: encoder.h:2135
ExtRefreshFrameFlagsInfo::golden_frame
bool golden_frame
Definition: encoder.h:2081
ExternalFlags::ref_frame_flags
int ref_frame_flags
Definition: encoder.h:2098
ExtRefreshFrameFlagsInfo::alt2_ref_frame
bool alt2_ref_frame
Definition: encoder.h:2083
AV1_PRIMARY::gf_group
GF_GROUP gf_group
Definition: encoder.h:2453
AOM_CODEC_OK
@ AOM_CODEC_OK
Operation completed without error.
Definition: aom_codec.h:157
AV1EncoderConfig::kf_cfg
KeyFrameCfg kf_cfg
Definition: encoder.h:911
FeatureFlags::allow_ref_frame_mvs
bool allow_ref_frame_mvs
Definition: av1_common_int.h:385
av1_encode_strategy
int av1_encode_strategy(AV1_COMP *const cpi, size_t *const size, uint8_t *const dest, unsigned int *frame_flags, int64_t *const time_stamp, int64_t *const time_end, const aom_rational64_t *const timestamp_ratio, int *const pop_lookahead, int flush)
Implement high-level encode strategy.
Definition: encode_strategy.c:1512
av1_temporal_filter
void av1_temporal_filter(struct AV1_COMP *cpi, const int filter_frame_lookahead_idx, int gf_frame_index, FRAME_DIFF *frame_diff, YV12_BUFFER_CONFIG *output_frame)
Performs temporal filtering if needed on a source frame. For example to create a filtered alternate r...
AOM_EFLAG_FORCE_KF
#define AOM_EFLAG_FORCE_KF
Force this frame to be a keyframe.
Definition: aom_encoder.h:378
aom_codec.h
Describes the codec algorithm interface to applications.
AV1_COMP::twopass_frame
TWO_PASS_FRAME twopass_frame
Definition: encoder.h:3274
AV1EncoderConfig
Main encoder configuration data structure.
Definition: encoder.h:894
AOM_RC_FIRST_PASS
@ AOM_RC_FIRST_PASS
Definition: aom_encoder.h:177
AV1Common::cur_frame
RefCntBuffer * cur_frame
Definition: av1_common_int.h:837
AV1_PRIMARY::number_temporal_layers
unsigned int number_temporal_layers
Definition: encoder.h:2513
ExternalFlags::refresh_frame_context
bool refresh_frame_context
Definition: encoder.h:2108
AOM_CODEC_ERROR
@ AOM_CODEC_ERROR
Unspecified error.
Definition: aom_codec.h:160
AV1_COMP
Top level encoder structure.
Definition: encoder.h:2632
EncodeFrameParams::remapped_ref_idx
int remapped_ref_idx[REF_FRAMES]
Definition: encoder.h:3337
TimeStamps
Frame time stamps.
Definition: encoder.h:2239
AV1Common::height
int height
Definition: av1_common_int.h:781
AV1_COMP::droppable
int droppable
Definition: encoder.h:2892
AV1_PRIMARY::tpl_data
TplParams tpl_data
Definition: encoder.h:2551
SVC
The stucture of SVC.
Definition: svc_layercontext.h:87
AV1_COMP::unscaled_source
YV12_BUFFER_CONFIG * unscaled_source
Definition: encoder.h:2708
av1_encode
int av1_encode(AV1_COMP *const cpi, uint8_t *const dest, const EncodeFrameInput *const frame_input, const EncodeFrameParams *const frame_params, EncodeFrameResults *const frame_results)
Run 1-pass/2-pass encoding.
Definition: encoder.c:3617
TimeStamps::prev_ts_end
int64_t prev_ts_end
Definition: encoder.h:2247
AOM_EFLAG_ERROR_RESILIENT
#define AOM_EFLAG_ERROR_RESILIENT
Enable error resilient frame.
Definition: aomcx.h:153
RateControlCfg::mode
enum aom_rc_mode mode
Definition: encoder.h:587
EncodeFrameParams::frame_type
FRAME_TYPE frame_type
Definition: encoder.h:3310
RefreshFrameInfo
Refresh frame flags for different type of frames.
Definition: encoder.h:1975
AV1_PRIMARY::tf_info
TEMPORAL_FILTER_INFO tf_info
Definition: encoder.h:2493
AV1Common::seq_params
SequenceHeader * seq_params
Definition: av1_common_int.h:981
AV1_PRIMARY::lookahead
struct lookahead_ctx * lookahead
Definition: encoder.h:2425
AV1_PRIMARY::mv_stats
MV_STATS mv_stats
Definition: encoder.h:2556
AV1_COMP::common
AV1_COMMON common
Definition: encoder.h:2675
EncodeFrameParams::error_resilient_mode
int error_resilient_mode
Definition: encoder.h:3306
FeatureFlags::refresh_frame_context
REFRESH_FRAME_CONTEXT_MODE refresh_frame_context
Definition: av1_common_int.h:425
AV1Common::superres_upscaled_width
int superres_upscaled_width
Definition: av1_common_int.h:804
ExtRefreshFrameFlagsInfo::last_frame
bool last_frame
Definition: encoder.h:2080
RefreshFrameInfo::golden_frame
bool golden_frame
Definition: encoder.h:1976
CommonQuantParams::using_qmatrix
bool using_qmatrix
Definition: av1_common_int.h:701
encode_strategy.h
Declares frame encoding functions.
av1_tpl_setup_stats
int av1_tpl_setup_stats(struct AV1_COMP *cpi, int gop_eval, const struct EncodeFrameParams *const frame_params)
Implements temporal dependency modelling for a GOP (GF/ARF group) and selects between 16 and 32 frame...
AV1Common::film_grain_params
aom_film_grain_t film_grain_params
Definition: av1_common_int.h:965
AV1Common::frame_presentation_time
uint32_t frame_presentation_time
Definition: av1_common_int.h:826
av1_get_one_pass_rt_params
void av1_get_one_pass_rt_params(AV1_COMP *cpi, EncodeFrameParams *const frame_params, unsigned int frame_flags)
Setup the rate control parameters for 1 pass real-time mode.
Definition: ratectrl.c:2938
AV1_COMP::film_grain_table
aom_film_grain_table_t * film_grain_table
Definition: encoder.h:3026
av1_lookahead_pop_sz
int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage)
Get pop_sz value.
has_no_stats_stage
static int has_no_stats_stage(const AV1_COMP *const cpi)
Check if the current stage has statistics.
Definition: encoder.h:3732
AV1_COMP::ref_buffer_stack
RefBufferStack ref_buffer_stack
Definition: encoder.h:2869
AOM_CODEC_MEM_ERROR
@ AOM_CODEC_MEM_ERROR
Memory operation failed.
Definition: aom_codec.h:163
AV1EncoderConfig::pass
enum aom_enc_pass pass
Definition: encoder.h:1019
AV1_PRIMARY::p_rc
PRIMARY_RATE_CONTROL p_rc
Definition: encoder.h:2488
EncodeFrameParams::speed
int speed
Definition: encoder.h:3348
SPEED_FEATURES::hl_sf
HIGH_LEVEL_SPEED_FEATURES hl_sf
Definition: speed_features.h:1392
CommonTileParams::large_scale
unsigned int large_scale
Definition: av1_common_int.h:492
AV1_PRIMARY::show_existing_alt_ref
int show_existing_alt_ref
Definition: encoder.h:2448
TimeStamps::prev_ts_start
int64_t prev_ts_start
Definition: encoder.h:2243
SPEED_FEATURES::tpl_sf
TPL_SPEED_FEATURES tpl_sf
Definition: speed_features.h:1402
AV1Common::quant_params
CommonQuantParams quant_params
Definition: av1_common_int.h:927
AV1_PRIMARY::use_svc
int use_svc
Definition: encoder.h:2503
yv12_buffer_config
YV12 frame buffer data structure.
Definition: yv12config.h:39
AV1_COMP::time_stamps
TimeStamps time_stamps
Definition: encoder.h:2808
aom_encoder.h
Describes the encoder algorithm interface to applications.
ExternalFlags
Flags signalled by the external interface at frame level.
Definition: encoder.h:2094
AV1_COMP::rc
RATE_CONTROL rc
Definition: encoder.h:2813
AV1Common
Top level common structure used by both encoder and decoder.
Definition: av1_common_int.h:755
ExternalFlags::use_ref_frame_mvs
bool use_ref_frame_mvs
Definition: encoder.h:2119
AV1_COMP::unfiltered_source
YV12_BUFFER_CONFIG * unfiltered_source
Definition: encoder.h:2729
av1_svc_primary_ref_frame
int av1_svc_primary_ref_frame(const struct AV1_COMP *const cpi)
Get primary reference frame for current layer.
AOM_Q
@ AOM_Q
Definition: aom_encoder.h:188
AV1EncoderConfig::algo_cfg
AlgoCfg algo_cfg
Definition: encoder.h:906
AV1_COMP::scaled_source
YV12_BUFFER_CONFIG scaled_source
Definition: encoder.h:2713
RefreshFrameInfo::bwd_ref_frame
bool bwd_ref_frame
Definition: encoder.h:1977
AV1_COMP::refresh_frame
RefreshFrameInfo refresh_frame
Definition: encoder.h:2760
EncodeFrameInput
Input frames and last input frame.
Definition: encoder.h:3290
AV1_COMP::skip_tpl_setup_stats
int skip_tpl_setup_stats
Definition: encoder.h:2734
AV1_COMP::framerate
double framerate
Definition: encoder.h:2818
av1_lookahead_peek
struct lookahead_entry * av1_lookahead_peek(struct lookahead_ctx *ctx, int index, COMPRESSOR_STAGE stage)
Get a future source buffer to encode.
EncodeFrameParams
contains per-frame encoding parameters decided upon by av1_encode_strategy() and passed down to av1_e...
Definition: encoder.h:3302
HIGH_LEVEL_SPEED_FEATURES::second_alt_ref_filtering
int second_alt_ref_filtering
Definition: speed_features.h:393
AV1Common::ref_frame_map
RefCntBuffer * ref_frame_map[REF_FRAMES]
Definition: av1_common_int.h:883
AV1_COMP::ppi
AV1_PRIMARY * ppi
Definition: encoder.h:2636
TEMPORAL_FILTER_INFO::tf_buf_second_arf
YV12_BUFFER_CONFIG tf_buf_second_arf
Definition: temporal_filter.h:173
macroblockd
Variables related to current coding block.
Definition: blockd.h:577
KeyFrameCfg::enable_keyframe_filtering
int enable_keyframe_filtering
Definition: encoder.h:459
AV1Common::superres_upscaled_height
int superres_upscaled_height
Definition: av1_common_int.h:805
ExternalFlags::refresh_frame_context_pending
bool refresh_frame_context_pending
Definition: encoder.h:2114
macroblock::e_mbd
MACROBLOCKD e_mbd
Decoder's view of current coding block.
Definition: block.h:796
ExtRefreshFrameFlagsInfo::bwd_ref_frame
bool bwd_ref_frame
Definition: encoder.h:2082
av1_lookahead_depth
unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage)
Get the number of frames currently in the lookahead queue.
KeyFrameCfg::enable_sframe
bool enable_sframe
Definition: encoder.h:492
AV1_PRIMARY::fb_of_context_type
int fb_of_context_type[REF_FRAMES]
Definition: encoder.h:2608
AV1Common::width
int width
Definition: av1_common_int.h:780
AV1_COMP::oxcf
AV1EncoderConfig oxcf
Definition: encoder.h:2680
EncodeFrameParams::refresh_frame
RefreshFrameInfo refresh_frame
Definition: encoder.h:3343
TimeStamps::first_ts_start
int64_t first_ts_start
Definition: encoder.h:2251
ExtRefreshFrameFlagsInfo
Frame refresh flags set by the external interface.
Definition: encoder.h:2079
AV1EncoderConfig::rc_cfg
RateControlCfg rc_cfg
Definition: encoder.h:916
PRIMARY_RATE_CONTROL::rate_correction_factors
double rate_correction_factors[RATE_FACTOR_LEVELS]
Definition: ratectrl.h:467
ExtRefreshFrameFlagsInfo::update_pending
bool update_pending
Definition: encoder.h:2088
AV1Common::showable_frame
int showable_frame
Definition: av1_common_int.h:898
AV1Common::current_frame
CurrentFrame current_frame
Definition: av1_common_int.h:759
AV1Common::features
FeatureFlags features
Definition: av1_common_int.h:910
AV1_COMP::source
YV12_BUFFER_CONFIG * source
Definition: encoder.h:2693
AV1Common::mi_params
CommonModeInfoParams mi_params
Definition: av1_common_int.h:915
EncodeFrameParams::show_frame
int show_frame
Definition: encoder.h:3320
macroblock
Encoder's parameters related to the current coding block.
Definition: block.h:778
AV1Common::remapped_ref_idx
int remapped_ref_idx[REF_FRAMES]
Definition: av1_common_int.h:859
RATE_CONTROL::frames_to_key
int frames_to_key
Definition: ratectrl.h:172
KeyFrameCfg::key_freq_max
int key_freq_max
Definition: encoder.h:454
AV1_COMP::svc
SVC svc
Definition: encoder.h:3120