blob: cb738ef282dc7e90cd891df034c40acdc4fd044e [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
James Zerne1cbb132018-08-22 14:10:36 -070012#ifndef AOM_AV1_ENCODER_ENCODER_H_
13#define AOM_AV1_ENCODER_ENCODER_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
elliottkb8becb22019-03-04 01:01:30 -080015#include <stdbool.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070016#include <stdio.h>
17
Tom Finegan60e653d2018-05-22 11:34:58 -070018#include "config/aom_config.h"
19
Yaowu Xuf883b422016-08-30 14:01:10 -070020#include "aom/aomcx.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021
22#include "av1/common/alloccommon.h"
23#include "av1/common/entropymode.h"
24#include "av1/common/thread_common.h"
25#include "av1/common/onyxc_int.h"
Fergus Simpsond2bcbb52017-05-22 23:15:05 -070026#include "av1/common/resize.h"
Andrey Norkin795ba872018-03-06 13:24:14 -080027#include "av1/common/timing.h"
kyslov7b9d0d62018-12-21 11:12:26 -080028#include "av1/common/blockd.h"
29#include "av1/common/enums.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070030#include "av1/encoder/aq_cyclicrefresh.h"
Tom Finegan17ce8b12017-02-08 12:46:31 -080031#include "av1/encoder/av1_quantize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070032#include "av1/encoder/context_tree.h"
33#include "av1/encoder/encodemb.h"
34#include "av1/encoder/firstpass.h"
chiyotsaid16a2572019-03-05 17:48:10 -080035#include "av1/encoder/level.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070036#include "av1/encoder/lookahead.h"
37#include "av1/encoder/mbgraph.h"
38#include "av1/encoder/mcomp.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070039#include "av1/encoder/ratectrl.h"
40#include "av1/encoder/rd.h"
41#include "av1/encoder/speed_features.h"
Marco Paniconi5b1e4732019-08-08 18:57:53 -070042#include "av1/encoder/svc_layercontext.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/encoder/tokenize.h"
kyslov7b9d0d62018-12-21 11:12:26 -080044#include "av1/encoder/block.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045
46#if CONFIG_INTERNAL_STATS
47#include "aom_dsp/ssim.h"
48#endif
49#include "aom_dsp/variance.h"
Neil Birkbecka2893ab2018-06-08 14:45:13 -070050#if CONFIG_DENOISE
51#include "aom_dsp/noise_model.h"
52#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070053#include "aom/internal/aom_codec_internal.h"
54#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055
56#ifdef __cplusplus
57extern "C" {
58#endif
59
Yue Chenc79fd242019-04-05 14:49:29 -070060// Rational number with an int64 numerator
61// This structure holds a fractional value
Yue Chen1bc5be62018-08-24 13:57:32 -070062typedef struct aom_rational64 {
Yue Chenc79fd242019-04-05 14:49:29 -070063 int64_t num; // fraction numerator
64 int den; // fraction denominator
65} aom_rational64_t; // alias for struct aom_rational
Yue Chen1bc5be62018-08-24 13:57:32 -070066
Yaowu Xuc27fc142016-08-22 16:08:15 -070067typedef struct {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -070068#if CONFIG_SUPERRES_IN_RECODE
69 struct loopfilter lf;
70 CdefInfo cdef_info;
71 YV12_BUFFER_CONFIG copy_buffer;
72 RATE_CONTROL rc;
73#endif // CONFIG_SUPERRES_IN_RECODE
Yaowu Xuc27fc142016-08-22 16:08:15 -070074} CODING_CONTEXT;
75
Satish Kumar Suman4667aa12018-12-14 18:28:19 +053076enum {
Urvang Joshif70375a2019-03-22 23:30:19 -070077 REGULAR_FRAME, // regular inter frame
78 ARF_FRAME, // alternate reference frame
79 OVERLAY_FRAME, // overlay frame
80 GLD_FRAME, // golden frame
81 BRF_FRAME, // backward reference frame
82 INTERNAL_ARF_FRAME, // internal alternate reference frame
Thomas Daede51020e12017-12-14 20:12:44 -080083 FRAME_CONTEXT_INDEXES
Satish Kumar Suman4667aa12018-12-14 18:28:19 +053084} UENUM1BYTE(FRAME_CONTEXT_INDEX);
Yaowu Xuc27fc142016-08-22 16:08:15 -070085
Satish Kumar Suman4667aa12018-12-14 18:28:19 +053086enum {
Yaowu Xuc27fc142016-08-22 16:08:15 -070087 NORMAL = 0,
88 FOURFIVE = 1,
89 THREEFIVE = 2,
90 ONETWO = 3
Satish Kumar Suman4667aa12018-12-14 18:28:19 +053091} UENUM1BYTE(AOM_SCALING);
Yaowu Xuc27fc142016-08-22 16:08:15 -070092
Satish Kumar Suman4667aa12018-12-14 18:28:19 +053093enum {
Yaowu Xuc27fc142016-08-22 16:08:15 -070094 // Good Quality Fast Encoding. The encoder balances quality with the amount of
95 // time it takes to encode the output. Speed setting controls how fast.
kyslov7b9d0d62018-12-21 11:12:26 -080096 GOOD,
97 // Realtime Fast Encoding. Will force some restrictions on bitrate
98 // constraints.
99 REALTIME
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530100} UENUM1BYTE(MODE);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530102enum {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700103 FRAMEFLAGS_KEY = 1 << 0,
104 FRAMEFLAGS_GOLDEN = 1 << 1,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700105 FRAMEFLAGS_BWDREF = 1 << 2,
Zoe Liu3ac20932017-08-30 16:35:55 -0700106 // TODO(zoeliu): To determine whether a frame flag is needed for ALTREF2_FRAME
Yaowu Xuc27fc142016-08-22 16:08:15 -0700107 FRAMEFLAGS_ALTREF = 1 << 3,
Debargha Mukherjee57378252018-09-21 18:29:37 -0700108 FRAMEFLAGS_INTRAONLY = 1 << 4,
109 FRAMEFLAGS_SWITCH = 1 << 5,
110 FRAMEFLAGS_ERROR_RESILIENT = 1 << 6,
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530111} UENUM1BYTE(FRAMETYPE_FLAGS);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700112
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530113enum {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114 NO_AQ = 0,
115 VARIANCE_AQ = 1,
116 COMPLEXITY_AQ = 2,
117 CYCLIC_REFRESH_AQ = 3,
118 AQ_MODE_COUNT // This should always be the last member of the enum
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530119} UENUM1BYTE(AQ_MODE);
120enum {
Fangwen Fu6160df22017-04-24 09:45:51 -0700121 NO_DELTA_Q = 0,
Debargha Mukherjee4fef7a52019-04-04 01:02:06 -0700122 DELTA_Q_OBJECTIVE = 1, // Modulation to improve objective quality
123 DELTA_Q_PERCEPTUAL = 2, // Modulation to improve perceptual quality
124 DELTA_Q_MODE_COUNT // This should always be the last member of the enum
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530125} UENUM1BYTE(DELTAQ_MODE);
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800126
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530127enum {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 RESIZE_NONE = 0, // No frame resizing allowed.
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700129 RESIZE_FIXED = 1, // All frames are coded at the specified scale.
130 RESIZE_RANDOM = 2, // All frames are coded at a random scale.
131 RESIZE_MODES
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530132} UENUM1BYTE(RESIZE_MODE);
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800133
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530134enum {
Urvang Joshi36a83732019-01-31 15:31:57 -0800135 SUPERRES_NONE, // No frame superres allowed.
136 SUPERRES_FIXED, // All frames are coded at the specified scale,
137 // and super-resolved.
138 SUPERRES_RANDOM, // All frames are coded at a random scale,
139 // and super-resolved.
140 SUPERRES_QTHRESH, // Superres scale for a frame is determined based on
141 // q_index.
142 SUPERRES_AUTO, // Automatically select superres for appropriate frames.
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700143 SUPERRES_MODES
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530144} UENUM1BYTE(SUPERRES_MODE);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145
kyslov7b9d0d62018-12-21 11:12:26 -0800146typedef enum {
147 kInvalid = 0,
148 kLowSadLowSumdiff = 1,
149 kLowSadHighSumdiff = 2,
150 kHighSadLowSumdiff = 3,
151 kHighSadHighSumdiff = 4,
152 kLowVarHighSumdiff = 5,
153 kVeryHighSad = 6,
154} CONTENT_STATE_SB;
155
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +0530156enum {
157 SS_CFG_SRC = 0,
158 SS_CFG_LOOKAHEAD = 1,
159 SS_CFG_TOTAL = 2
160} UENUM1BYTE(SS_CFG_OFFSET);
161
Jingning Han31a0ee92019-07-15 13:56:55 -0700162#define MAX_LENGTH_TPL_FRAME_STATS (27 + 9)
Yue Chenc9b23e02019-04-10 16:54:03 -0700163
Yue Chen7cae98f2018-08-24 10:43:16 -0700164typedef struct TplDepStats {
165 int64_t intra_cost;
166 int64_t inter_cost;
Jingning Hanfdd0ab42019-09-27 19:47:28 -0700167 int64_t srcrf_dist;
168 int64_t recrf_dist;
Jingning Han40e870f2019-10-02 16:05:39 -0700169 int64_t srcrf_rate;
170 int64_t recrf_rate;
Jingning Han40e870f2019-10-02 16:05:39 -0700171 int64_t mc_dep_rate;
172 int64_t mc_dep_dist;
Jingning Hane8baf832019-09-23 15:43:47 -0700173 int_mv mv;
174 int ref_frame_index;
Yue Chenbd934232019-08-05 14:23:39 -0700175#if !USE_TPL_CLASSIC_MODEL
Debargha Mukherjee88ff7382019-05-01 12:36:10 -0700176 int64_t mc_count;
177 int64_t mc_saved;
Yue Chenbd934232019-08-05 14:23:39 -0700178#endif // !USE_TPL_CLASSIC_MODEL
Yue Chen7cae98f2018-08-24 10:43:16 -0700179} TplDepStats;
180
181typedef struct TplDepFrame {
182 uint8_t is_valid;
183 TplDepStats *tpl_stats_ptr;
Jingning Han81d6fbb2019-07-15 10:14:13 -0700184 const YV12_BUFFER_CONFIG *gf_picture;
Jingning Han5e98d3b2019-09-23 21:59:36 -0700185 YV12_BUFFER_CONFIG rec_picture_buf;
186 YV12_BUFFER_CONFIG *rec_picture;
Jingning Han31a0ee92019-07-15 13:56:55 -0700187 int ref_map_index[REF_FRAMES];
Yue Chen7cae98f2018-08-24 10:43:16 -0700188 int stride;
189 int width;
190 int height;
191 int mi_rows;
192 int mi_cols;
Deepa K G33f1ab72019-09-23 17:59:06 +0530193 unsigned int frame_display_index;
Jingning Han40e870f2019-10-02 16:05:39 -0700194 int base_rdmult;
Yue Chen7cae98f2018-08-24 10:43:16 -0700195} TplDepFrame;
196
Debargha Mukherjee03ad12d2019-02-07 11:17:59 -0800197typedef enum {
198 COST_UPD_SB,
199 COST_UPD_SBROW,
200 COST_UPD_TILE,
Fyodor Kyslov25daf5d2019-10-23 10:33:26 -0700201 COST_UPD_OFF,
Debargha Mukherjee03ad12d2019-02-07 11:17:59 -0800202} COST_UPDATE_TYPE;
203
Yue Chen7cae98f2018-08-24 10:43:16 -0700204#define TPL_DEP_COST_SCALE_LOG2 4
205
Yaowu Xuf883b422016-08-30 14:01:10 -0700206typedef struct AV1EncoderConfig {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700207 BITSTREAM_PROFILE profile;
Tom Finegan8ab2bba2018-02-28 07:36:28 -0800208 aom_bit_depth_t bit_depth; // Codec bit-depth.
209 int width; // width of data passed to the compressor
210 int height; // height of data passed to the compressor
211 int forced_max_frame_width; // forced maximum width of frame (if != 0)
212 int forced_max_frame_height; // forced maximum height of frame (if != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700213 unsigned int input_bit_depth; // Input bit depth.
214 double init_framerate; // set to passed in framerate
215 int64_t target_bandwidth; // bandwidth to be used in bits per second
216
217 int noise_sensitivity; // pre processing blur: recommendation 0
218 int sharpness; // sharpening output: recommendation 0:
219 int speed;
220 // maximum allowed bitrate for any intra frame in % of bitrate target.
221 unsigned int rc_max_intra_bitrate_pct;
222 // maximum allowed bitrate for any inter frame in % of bitrate target.
223 unsigned int rc_max_inter_bitrate_pct;
224 // percent of rate boost for golden frame in CBR mode.
225 unsigned int gf_cbr_boost_pct;
226
227 MODE mode;
228 int pass;
229
230 // Key Framing Operations
231 int auto_key; // autodetect cut scenes and set the keyframes
232 int key_freq; // maximum distance to key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -0500233 int sframe_dist;
234 int sframe_mode;
235 int sframe_enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700236 int lag_in_frames; // how many frames lag before we start encoding
Sarah Parker93c03142018-05-22 13:35:45 -0700237 int fwd_kf_enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238
239 // ----------------------------------------------------------------
240 // DATARATE CONTROL OPTIONS
241
242 // vbr, cbr, constrained quality or constant quality
Yaowu Xuf883b422016-08-30 14:01:10 -0700243 enum aom_rc_mode rc_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700244
245 // buffer targeting aggressiveness
246 int under_shoot_pct;
247 int over_shoot_pct;
248
249 // buffering parameters
250 int64_t starting_buffer_level_ms;
251 int64_t optimal_buffer_level_ms;
252 int64_t maximum_buffer_size_ms;
253
254 // Frame drop threshold.
255 int drop_frames_water_mark;
256
257 // controlling quality
258 int fixed_q;
259 int worst_allowed_q;
260 int best_allowed_q;
261 int cq_level;
Yue Chenc5806c22019-07-19 17:32:57 -0700262 int enable_chroma_deltaq;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 AQ_MODE aq_mode; // Adaptive Quantization mode
Fangwen Fu6160df22017-04-24 09:45:51 -0700264 DELTAQ_MODE deltaq_mode;
Debargha Mukherjee4fef7a52019-04-04 01:02:06 -0700265 int deltalf_mode;
Debargha Mukherjee98a311c2018-03-25 16:33:11 -0700266 int enable_cdef;
267 int enable_restoration;
Yaowu Xu2a9ac432019-08-06 14:21:17 -0700268 int force_video_mode;
Yue Chen5e129512018-12-10 10:48:54 -0800269 int enable_obmc;
Yue Chen4835dc02018-05-11 15:57:43 -0700270 int disable_trellis_quant;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700271 int using_qm;
Yaowu Xuf7a12422018-01-31 15:29:20 -0800272 int qm_y;
273 int qm_u;
274 int qm_v;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700275 int qm_minlevel;
276 int qm_maxlevel;
Yushin Chod808bfc2017-08-10 15:54:36 -0700277#if CONFIG_DIST_8X8
278 int using_dist_8x8;
279#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000280 unsigned int num_tile_groups;
281 unsigned int mtu;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700282
283 // Internal frame size scaling.
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700284 RESIZE_MODE resize_mode;
Urvang Joshide71d142017-10-05 12:12:15 -0700285 uint8_t resize_scale_denominator;
286 uint8_t resize_kf_scale_denominator;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700288 // Frame Super-Resolution size scaling.
289 SUPERRES_MODE superres_mode;
Urvang Joshide71d142017-10-05 12:12:15 -0700290 uint8_t superres_scale_denominator;
291 uint8_t superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700292 int superres_qthresh;
293 int superres_kf_qthresh;
Fergus Simpson3502d082017-04-10 12:25:07 -0700294
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295 // Enable feature to reduce the frame quantization every x frames.
296 int frame_periodic_boost;
297
298 // two pass datarate control
299 int two_pass_vbrbias; // two pass datarate control tweaks
300 int two_pass_vbrmin_section;
301 int two_pass_vbrmax_section;
302 // END DATARATE CONTROL OPTIONS
303 // ----------------------------------------------------------------
304
305 int enable_auto_arf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306 int enable_auto_brf; // (b)ackward (r)ef (f)rame
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307
Yaowu Xuc27fc142016-08-22 16:08:15 -0700308 /* Bitfield defining the error resiliency features to enable.
309 * Can provide decodable frames after losses in previous
310 * frames and decodable partitions after losses in the same frame.
311 */
312 unsigned int error_resilient_mode;
313
Debargha Mukherjee52fb0472018-03-29 15:48:11 -0700314 unsigned int s_frame_mode;
315
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316 /* Bitfield defining the parallel decoding mode where the
317 * decoding in successive frames may be conducted in parallel
318 * just by decoding the frame headers.
319 */
320 unsigned int frame_parallel_decoding_mode;
321
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700322 unsigned int limit;
323
Yaowu Xuc27fc142016-08-22 16:08:15 -0700324 int arnr_max_frames;
325 int arnr_strength;
326
327 int min_gf_interval;
328 int max_gf_interval;
Urvang Joshif4e775c2018-12-14 11:33:17 -0800329 int gf_max_pyr_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700330
Ravi Chaudhary0ec03a42018-08-17 18:53:28 +0530331 int row_mt;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700332 int tile_columns;
333 int tile_rows;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200334 int tile_width_count;
335 int tile_height_count;
336 int tile_widths[MAX_TILE_COLS];
337 int tile_heights[MAX_TILE_ROWS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700338
Yue Chen7cae98f2018-08-24 10:43:16 -0700339 int enable_tpl_model;
Cheng Chen0fcf6f82019-10-11 11:41:19 -0700340 int enable_keyframe_filtering;
Yue Chen7cae98f2018-08-24 10:43:16 -0700341
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342 int max_threads;
343
Yaowu Xuf883b422016-08-30 14:01:10 -0700344 aom_fixed_buf_t two_pass_stats_in;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345
Yaowu Xuf883b422016-08-30 14:01:10 -0700346 aom_tune_metric tuning;
347 aom_tune_content content;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700348 int use_highbitdepth;
Andrey Norkin9e694632017-12-21 18:50:57 -0800349 aom_color_primaries_t color_primaries;
350 aom_transfer_characteristics_t transfer_characteristics;
351 aom_matrix_coefficients_t matrix_coefficients;
anorkin76fb1262017-03-22 15:12:12 -0700352 aom_chroma_sample_position_t chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700353 int color_range;
354 int render_width;
355 int render_height;
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800356 int timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -0800357 aom_timing_info_t timing_info;
358 int decoder_model_info_present_flag;
Andrey Norkin26495512018-06-20 17:13:11 -0700359 int display_model_info_present_flag;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -0700360 int buffer_removal_time_present;
Andrey Norkin795ba872018-03-06 13:24:14 -0800361 aom_dec_model_info_t buffer_model;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800362 int film_grain_test_vector;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700363 const char *film_grain_table_filename;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364
Hui Su1cb1c002018-02-05 18:21:20 -0800365 uint8_t cdf_update_mode;
Yaowu Xuf883b422016-08-30 14:01:10 -0700366 aom_superblock_size_t superblock_size;
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700367 unsigned int large_scale_tile;
368 unsigned int single_tile_decoding;
Yaowu Xu76537ca2019-01-03 12:41:32 -0800369 uint8_t monochrome;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -0700370 unsigned int full_still_picture_hdr;
Jingning Hana446f552018-02-22 15:42:12 -0800371 int enable_dual_filter;
Yunqing Wangff4fa062017-04-21 10:56:08 -0700372 unsigned int motion_vector_unit_test;
James Zernbf3ca362019-10-21 11:33:44 -0700373 const cfg_options_t *cfg;
Debargha Mukherjee9d9cf192018-12-17 11:24:21 -0800374 int enable_rect_partitions;
Ryan Lei9543c702019-03-11 11:06:14 -0700375 int enable_ab_partitions;
376 int enable_1to4_partitions;
377 int min_partition_size;
378 int max_partition_size;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -0800379 int enable_intra_edge_filter;
Debargha Mukherjee8c917b02018-12-17 13:45:50 -0800380 int enable_tx64;
Ryan Lei82d0a042019-03-18 15:02:26 -0700381 int tx_size_search_method;
Ryan Lei9543c702019-03-11 11:06:14 -0700382 int enable_flip_idtx;
Debargha Mukherjee0d8368a2018-03-25 12:23:02 -0700383 int enable_order_hint;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -0800384 int enable_dist_wtd_comp;
Debargha Mukherjee0187d7c2018-03-25 11:37:56 -0700385 int enable_ref_frame_mvs;
Urvang Joshi9ad9f082019-01-22 12:31:33 -0800386 unsigned int max_reference_frames;
Urvang Joshi1a9e27e2019-02-19 11:17:23 -0800387 int enable_reduced_reference_set;
Debargha Mukherjee0d8368a2018-03-25 12:23:02 -0700388 unsigned int allow_ref_frame_mvs;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -0800389 int enable_masked_comp;
Ryan Lei9543c702019-03-11 11:06:14 -0700390 int enable_onesided_comp;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -0800391 int enable_interintra_comp;
Yue Chen8cbee6c2018-12-14 11:54:39 -0800392 int enable_smooth_interintra;
Debargha Mukherjee78e6b2c2018-12-11 07:50:57 -0800393 int enable_diff_wtd_comp;
394 int enable_interinter_wedge;
395 int enable_interintra_wedge;
Debargha Mukherjeed2e53ca2018-12-10 11:34:59 -0800396 int enable_global_motion;
Debargha Mukherjee37df9162018-03-25 12:48:24 -0700397 int enable_warped_motion;
398 int allow_warped_motion;
Yue Chen8f9ca582018-12-12 15:11:47 -0800399 int enable_filter_intra;
Debargha Mukherjee078ae862018-12-18 11:05:43 -0800400 int enable_smooth_intra;
401 int enable_paeth_intra;
Debargha Mukherjee8e256782018-12-18 13:44:13 -0800402 int enable_cfl_intra;
Urvang Joshi2c92b072018-03-19 17:23:31 -0700403 int enable_superres;
Debargha Mukherjeea2074dd2019-09-04 10:03:44 -0700404 int enable_overlay;
Hui Sua9e3d142018-12-14 15:21:21 -0800405 int enable_palette;
Hui Su440e6d42018-12-17 11:47:07 -0800406 int enable_intrabc;
Hui Su94b38172018-12-19 10:40:29 -0800407 int enable_angle_delta;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400408 unsigned int save_as_annexb;
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700409
410#if CONFIG_DENOISE
411 float noise_level;
412 int noise_block_size;
413#endif
Tom Finegan02b2a842018-08-24 13:50:00 -0700414
415 unsigned int chroma_subsampling_x;
416 unsigned int chroma_subsampling_y;
Sarah Parker0b7e5902018-12-10 13:35:25 -0800417 int reduced_tx_type_set;
Debargha Mukherjeedb458152019-01-25 11:04:09 -0800418 int use_intra_dct_only;
419 int use_inter_dct_only;
Debargha Mukherjee042af922019-02-07 05:58:26 -0800420 int use_intra_default_tx_only;
Sarah Parker740e8392019-01-23 15:47:53 -0800421 int quant_b_adapt;
Debargha Mukherjee03ad12d2019-02-07 11:17:59 -0800422 COST_UPDATE_TYPE coeff_cost_upd_freq;
423 COST_UPDATE_TYPE mode_cost_upd_freq;
Debargha Mukherjee8d273412019-09-11 10:36:50 -0700424 COST_UPDATE_TYPE mv_cost_upd_freq;
Satish Kumar Suman29909962019-01-09 10:31:21 +0530425 int border_in_pixels;
Hui Su2aa492c2019-03-12 15:18:18 -0700426 AV1_LEVEL target_seq_level_idx[MAX_NUM_OPERATING_POINTS];
Hui Sud909c2c2019-03-08 11:51:14 -0800427 // Bit mask to specify which tier each of the 32 possible operating points
428 // conforms to.
429 unsigned int tier_mask;
Hui Suef139e12019-05-20 15:51:22 -0700430 // min_cr / 100 is the target minimum compression ratio for each frame.
431 unsigned int min_cr;
Yaowu Xuf883b422016-08-30 14:01:10 -0700432} AV1EncoderConfig;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700433
Yaowu Xuf883b422016-08-30 14:01:10 -0700434static INLINE int is_lossless_requested(const AV1EncoderConfig *cfg) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700435 return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
436}
437
Yue Chencc6a6ef2018-05-21 16:21:05 -0700438typedef struct FRAME_COUNTS {
439// Note: This structure should only contain 'unsigned int' fields, or
440// aggregates built solely from 'unsigned int' fields/elements
441#if CONFIG_ENTROPY_STATS
442 unsigned int kf_y_mode[KF_MODE_CONTEXTS][KF_MODE_CONTEXTS][INTRA_MODES];
443 unsigned int angle_delta[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
444 unsigned int y_mode[BLOCK_SIZE_GROUPS][INTRA_MODES];
445 unsigned int uv_mode[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
446 unsigned int cfl_sign[CFL_JOINT_SIGNS];
447 unsigned int cfl_alpha[CFL_ALPHA_CONTEXTS][CFL_ALPHABET_SIZE];
448 unsigned int palette_y_mode[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
449 unsigned int palette_uv_mode[PALETTE_UV_MODE_CONTEXTS][2];
450 unsigned int palette_y_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
451 unsigned int palette_uv_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
452 unsigned int palette_y_color_index[PALETTE_SIZES]
453 [PALETTE_COLOR_INDEX_CONTEXTS]
454 [PALETTE_COLORS];
455 unsigned int palette_uv_color_index[PALETTE_SIZES]
456 [PALETTE_COLOR_INDEX_CONTEXTS]
457 [PALETTE_COLORS];
458 unsigned int partition[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
459 unsigned int txb_skip[TOKEN_CDF_Q_CTXS][TX_SIZES][TXB_SKIP_CONTEXTS][2];
460 unsigned int eob_extra[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
461 [EOB_COEF_CONTEXTS][2];
462 unsigned int dc_sign[PLANE_TYPES][DC_SIGN_CONTEXTS][2];
463 unsigned int coeff_lps[TX_SIZES][PLANE_TYPES][BR_CDF_SIZE - 1][LEVEL_CONTEXTS]
464 [2];
465 unsigned int eob_flag[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS][2];
466 unsigned int eob_multi16[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][5];
467 unsigned int eob_multi32[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][6];
468 unsigned int eob_multi64[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][7];
469 unsigned int eob_multi128[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][8];
470 unsigned int eob_multi256[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][9];
471 unsigned int eob_multi512[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][10];
472 unsigned int eob_multi1024[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][11];
473 unsigned int coeff_lps_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
474 [LEVEL_CONTEXTS][BR_CDF_SIZE];
475 unsigned int coeff_base_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
476 [SIG_COEF_CONTEXTS][NUM_BASE_LEVELS + 2];
477 unsigned int coeff_base_eob_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
478 [SIG_COEF_CONTEXTS_EOB][NUM_BASE_LEVELS + 1];
479 unsigned int newmv_mode[NEWMV_MODE_CONTEXTS][2];
480 unsigned int zeromv_mode[GLOBALMV_MODE_CONTEXTS][2];
481 unsigned int refmv_mode[REFMV_MODE_CONTEXTS][2];
482 unsigned int drl_mode[DRL_MODE_CONTEXTS][2];
483 unsigned int inter_compound_mode[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
484 unsigned int wedge_idx[BLOCK_SIZES_ALL][16];
485 unsigned int interintra[BLOCK_SIZE_GROUPS][2];
486 unsigned int interintra_mode[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
487 unsigned int wedge_interintra[BLOCK_SIZES_ALL][2];
Debargha Mukherjee54eabb52019-02-01 16:54:33 -0800488 unsigned int compound_type[BLOCK_SIZES_ALL][MASKED_COMPOUND_TYPES];
Yue Chencc6a6ef2018-05-21 16:21:05 -0700489 unsigned int motion_mode[BLOCK_SIZES_ALL][MOTION_MODES];
490 unsigned int obmc[BLOCK_SIZES_ALL][2];
491 unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
492 unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
493 unsigned int comp_ref_type[COMP_REF_TYPE_CONTEXTS][2];
494 unsigned int uni_comp_ref[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1][2];
495 unsigned int single_ref[REF_CONTEXTS][SINGLE_REFS - 1][2];
496 unsigned int comp_ref[REF_CONTEXTS][FWD_REFS - 1][2];
497 unsigned int comp_bwdref[REF_CONTEXTS][BWD_REFS - 1][2];
498 unsigned int intrabc[2];
499
500 unsigned int txfm_partition[TXFM_PARTITION_CONTEXTS][2];
501 unsigned int intra_tx_size[MAX_TX_CATS][TX_SIZE_CONTEXTS][MAX_TX_DEPTH + 1];
502 unsigned int skip_mode[SKIP_MODE_CONTEXTS][2];
503 unsigned int skip[SKIP_CONTEXTS][2];
504 unsigned int compound_index[COMP_INDEX_CONTEXTS][2];
505 unsigned int comp_group_idx[COMP_GROUP_IDX_CONTEXTS][2];
506 unsigned int delta_q[DELTA_Q_PROBS][2];
507 unsigned int delta_lf_multi[FRAME_LF_COUNT][DELTA_LF_PROBS][2];
508 unsigned int delta_lf[DELTA_LF_PROBS][2];
509
510 unsigned int inter_ext_tx[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
511 unsigned int intra_ext_tx[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
512 [TX_TYPES];
513 unsigned int filter_intra_mode[FILTER_INTRA_MODES];
514 unsigned int filter_intra[BLOCK_SIZES_ALL][2];
515 unsigned int switchable_restore[RESTORE_SWITCHABLE_TYPES];
516 unsigned int wiener_restore[2];
517 unsigned int sgrproj_restore[2];
518#endif // CONFIG_ENTROPY_STATS
519
520 unsigned int switchable_interp[SWITCHABLE_FILTER_CONTEXTS]
521 [SWITCHABLE_FILTERS];
522} FRAME_COUNTS;
523
Angie Chiang71572162018-08-06 16:20:36 -0700524#define INTER_MODE_RD_DATA_OVERALL_SIZE 6400
525
526typedef struct {
527 int ready;
528 double a;
529 double b;
530 double dist_mean;
Angie Chiang199f3d42018-08-14 18:07:02 -0700531 double ld_mean;
532 double sse_mean;
533 double sse_sse_mean;
534 double sse_ld_mean;
Angie Chiang106fa482018-08-07 18:28:40 -0700535 int num;
536 double dist_sum;
537 double ld_sum;
538 double sse_sum;
539 double sse_sse_sum;
540 double sse_ld_sum;
Angie Chiang71572162018-08-06 16:20:36 -0700541} InterModeRdModel;
542
543typedef struct {
544 int idx;
545 int64_t rd;
546} RdIdxPair;
547// TODO(angiebird): This is an estimated size. We still need to figure what is
548// the maximum number of modes.
549#define MAX_INTER_MODES 1024
550typedef struct inter_modes_info {
551 int num;
552 MB_MODE_INFO mbmi_arr[MAX_INTER_MODES];
553 int mode_rate_arr[MAX_INTER_MODES];
554 int64_t sse_arr[MAX_INTER_MODES];
555 int64_t est_rd_arr[MAX_INTER_MODES];
556 RdIdxPair rd_idx_pair_arr[MAX_INTER_MODES];
elliottkb8becb22019-03-04 01:01:30 -0800557 RD_STATS rd_cost_arr[MAX_INTER_MODES];
558 RD_STATS rd_cost_y_arr[MAX_INTER_MODES];
559 RD_STATS rd_cost_uv_arr[MAX_INTER_MODES];
Angie Chiang71572162018-08-06 16:20:36 -0700560} InterModesInfo;
Angie Chiang71572162018-08-06 16:20:36 -0700561
Ravi Chaudhary40cdf132018-10-08 11:04:16 +0530562// Encoder row synchronization
563typedef struct AV1RowMTSyncData {
564#if CONFIG_MULTITHREAD
565 pthread_mutex_t *mutex_;
566 pthread_cond_t *cond_;
567#endif
568 // Allocate memory to store the sb/mb block index in each row.
569 int *cur_col;
570 int sync_range;
571 int rows;
572} AV1RowMTSync;
573
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530574typedef struct AV1RowMTInfo {
575 int current_mi_row;
576 int num_threads_working;
577} AV1RowMTInfo;
578
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579// TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
580typedef struct TileDataEnc {
581 TileInfo tile_info;
Yunqing Wang8c1e57c2016-10-25 15:15:23 -0700582 int m_search_count;
583 int ex_search_count;
Luc Trudeauf8164152017-04-11 16:20:51 -0400584 CFL_CTX cfl;
Jingning Han9f07be12017-04-13 09:31:40 -0700585 DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
Ravi Chaudharye5215172018-12-21 18:47:25 +0530586 FRAME_CONTEXT *row_ctx;
Yunqing Wang0e141b52017-11-02 15:08:58 -0700587 uint8_t allow_update_cdf;
Angie Chiang71572162018-08-06 16:20:36 -0700588 InterModeRdModel inter_mode_rd_models[BLOCK_SIZES_ALL];
Ravi Chaudhary40cdf132018-10-08 11:04:16 +0530589 AV1RowMTSync row_mt_sync;
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530590 AV1RowMTInfo row_mt_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700591} TileDataEnc;
592
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530593typedef struct {
594 TOKENEXTRA *start;
595 TOKENEXTRA *stop;
596 unsigned int count;
597} TOKENLIST;
598
Ravi Chaudharyc5e74692018-10-08 16:05:38 +0530599typedef struct MultiThreadHandle {
600 int allocated_tile_rows;
601 int allocated_tile_cols;
602 int allocated_sb_rows;
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530603 int thread_id_to_tile_id[MAX_NUM_THREADS]; // Mapping of threads to tiles
Ravi Chaudharyc5e74692018-10-08 16:05:38 +0530604} MultiThreadHandle;
605
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606typedef struct RD_COUNTS {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607 int64_t comp_pred_diff[REFERENCE_MODES];
Debargha Mukherjeea575d232017-04-28 17:46:47 -0700608 // Stores number of 4x4 blocks using global motion per reference frame.
Zoe Liu27deb382018-03-27 15:13:56 -0700609 int global_motion_used[REF_FRAMES];
Arild Fuldseth (arilfuld)6c20c782017-06-15 09:45:02 +0200610 int compound_ref_used_flag;
Zoe Liu8a5d3432017-11-30 16:33:44 -0800611 int skip_mode_used_flag;
Yunqing Wangdb70bf42019-08-19 09:28:11 -0700612 int tx_type_used[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700613} RD_COUNTS;
614
615typedef struct ThreadData {
616 MACROBLOCK mb;
617 RD_COUNTS rd_counts;
618 FRAME_COUNTS *counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619 PC_TREE *pc_tree;
620 PC_TREE *pc_root[MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2 + 1];
wenyao.liu0c6f5fd2018-10-23 18:41:16 +0800621 tran_low_t *tree_coeff_buf[MAX_MB_PLANE];
622 tran_low_t *tree_qcoeff_buf[MAX_MB_PLANE];
623 tran_low_t *tree_dqcoeff_buf[MAX_MB_PLANE];
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530624 InterModesInfo *inter_modes_info;
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530625 uint32_t *hash_value_buffer[2][2];
Jingning Hand064cf02017-06-01 10:00:39 -0700626 int32_t *wsrc_buf;
627 int32_t *mask_buf;
628 uint8_t *above_pred_buf;
629 uint8_t *left_pred_buf;
hui su5d493142017-05-08 12:06:12 -0700630 PALETTE_BUFFER *palette_buffer;
Hui Su38711e72019-06-11 10:49:47 -0700631 CompoundTypeRdBuffers comp_rd_buffer;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700632 CONV_BUF_TYPE *tmp_conv_dst;
633 uint8_t *tmp_obmc_bufs[2];
Ravi Chaudhary00525ef2018-10-31 19:52:42 +0530634 int intrabc_used;
Yue Chenc87d7492019-05-30 17:22:49 -0700635 int deltaq_used;
Ravi Chaudhary84a280a2018-09-24 16:09:48 +0530636 FRAME_CONTEXT *tctx;
Remya0cce44c2019-08-16 11:57:24 +0530637 MB_MODE_INFO_EXT *mbmi_ext;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638} ThreadData;
639
640struct EncWorkerData;
641
642typedef struct ActiveMap {
643 int enabled;
644 int update;
645 unsigned char *map;
646} ActiveMap;
647
Wan-Teh Changc25c92a2018-04-23 15:04:14 -0700648#if CONFIG_INTERNAL_STATS
649// types of stats
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530650enum {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -0700651 STAT_Y,
652 STAT_U,
653 STAT_V,
654 STAT_ALL,
655 NUM_STAT_TYPES // This should always be the last member of the enum
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530656} UENUM1BYTE(StatType);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657
658typedef struct IMAGE_STAT {
Urvang Joshib5ed3502016-10-17 16:38:05 -0700659 double stat[NUM_STAT_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660 double worst;
661} ImageStat;
Wan-Teh Changc25c92a2018-04-23 15:04:14 -0700662#endif // CONFIG_INTERNAL_STATS
Urvang Joshib5ed3502016-10-17 16:38:05 -0700663
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664typedef struct {
665 int ref_count;
666 YV12_BUFFER_CONFIG buf;
667} EncRefCntBuffer;
668
chiyotsai9c484b32019-03-07 16:01:50 -0800669#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -0800670typedef struct PartitionStats {
671 int partition_decisions[6][EXT_PARTITION_TYPES];
672 int partition_attempts[6][EXT_PARTITION_TYPES];
chiyotsai9c484b32019-03-07 16:01:50 -0800673 int64_t partition_times[6][EXT_PARTITION_TYPES];
chiyotsai92ed0dd2019-01-25 14:50:14 -0800674
675 int partition_redo;
676} PartitionStats;
677#endif
678
Yunqing Wangba255582019-02-11 16:21:12 -0800679#if CONFIG_COLLECT_COMPONENT_TIMING
680#include "aom_ports/aom_timer.h"
681// Adjust the following to add new components.
682enum {
Yunqing Wangd1f32e62019-02-20 10:37:51 -0800683 encode_frame_to_data_rate_time,
684 encode_with_recode_loop_time,
685 loop_filter_time,
686 cdef_time,
687 loop_restoration_time,
688 av1_pack_bitstream_final_time,
689 av1_encode_frame_time,
690 av1_compute_global_motion_time,
691 av1_setup_motion_field_time,
Yunqing Wangba255582019-02-11 16:21:12 -0800692 encode_sb_time,
693 rd_pick_partition_time,
694 rd_pick_sb_modes_time,
695 av1_rd_pick_intra_mode_sb_time,
696 av1_rd_pick_inter_mode_sb_time,
Yunqing Wangd1f32e62019-02-20 10:37:51 -0800697 handle_intra_mode_time,
Yunqing Wangd1f32e62019-02-20 10:37:51 -0800698 do_tx_search_time,
699 handle_newmv_time,
700 compound_type_rd_time,
701 interpolation_filter_search_time,
Yunqing Wangba255582019-02-11 16:21:12 -0800702 motion_mode_rd_time,
703 kTimingComponents,
704} UENUM1BYTE(TIMING_COMPONENT);
705
706static INLINE char const *get_component_name(int index) {
707 switch (index) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -0800708 case encode_frame_to_data_rate_time:
709 return "encode_frame_to_data_rate_time";
710 case encode_with_recode_loop_time: return "encode_with_recode_loop_time";
711 case loop_filter_time: return "loop_filter_time";
712 case cdef_time: return "cdef_time";
713 case loop_restoration_time: return "loop_restoration_time";
714 case av1_pack_bitstream_final_time: return "av1_pack_bitstream_final_time";
715 case av1_encode_frame_time: return "av1_encode_frame_time";
716 case av1_compute_global_motion_time:
717 return "av1_compute_global_motion_time";
718 case av1_setup_motion_field_time: return "av1_setup_motion_field_time";
719 case encode_sb_time: return "encode_sb_time";
Yunqing Wangd1f32e62019-02-20 10:37:51 -0800720 case rd_pick_partition_time: return "rd_pick_partition_time";
721 case rd_pick_sb_modes_time: return "rd_pick_sb_modes_time";
722 case av1_rd_pick_intra_mode_sb_time:
723 return "av1_rd_pick_intra_mode_sb_time";
724 case av1_rd_pick_inter_mode_sb_time:
725 return "av1_rd_pick_inter_mode_sb_time";
726 case handle_intra_mode_time: return "handle_intra_mode_time";
Yunqing Wangd1f32e62019-02-20 10:37:51 -0800727 case do_tx_search_time: return "do_tx_search_time";
728 case handle_newmv_time: return "handle_newmv_time";
729 case compound_type_rd_time: return "compound_type_rd_time";
730 case interpolation_filter_search_time:
731 return "interpolation_filter_search_time";
732 case motion_mode_rd_time: return "motion_mode_rd_time";
Yunqing Wangba255582019-02-11 16:21:12 -0800733 default: assert(0);
734 }
735 return "error";
736}
737#endif
738
Urvang Joshif70375a2019-03-22 23:30:19 -0700739// The maximum number of internal ARFs except ALTREF_FRAME
740#define MAX_INTERNAL_ARFS (REF_FRAMES - BWDREF_FRAME - 1)
741
Jingning Han42266ca2019-07-12 14:37:16 -0700742typedef struct {
743 int arf_stack[FRAME_BUFFERS];
744 int arf_stack_size;
745 int lst_stack[FRAME_BUFFERS];
746 int lst_stack_size;
747 int gld_stack[FRAME_BUFFERS];
748 int gld_stack_size;
749} RefBufferStack;
750
Yaowu Xuf883b422016-08-30 14:01:10 -0700751typedef struct AV1_COMP {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 QUANTS quants;
753 ThreadData td;
Yue Chencc6a6ef2018-05-21 16:21:05 -0700754 FRAME_COUNTS counts;
Remya0cce44c2019-08-16 11:57:24 +0530755 MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame_base;
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700756 CB_COEFF_BUFFER *coeff_buffer_base;
Yi Luoc6210232017-05-25 15:09:25 -0700757 Dequants dequants;
Yaowu Xuf883b422016-08-30 14:01:10 -0700758 AV1_COMMON common;
759 AV1EncoderConfig oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760 struct lookahead_ctx *lookahead;
761 struct lookahead_entry *alt_ref_source;
Sarah Parkeraf32a7b2018-06-29 14:59:05 -0700762 int no_show_kf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763
Angie Chiange69a6822018-03-16 13:52:44 -0700764 int optimize_seg_arr[MAX_SEGMENTS];
chiyotsaia7091f12019-08-09 16:48:27 -0700765 int mi_ext_alloc_size;
Angie Chiange69a6822018-03-16 13:52:44 -0700766
Alex Conversef77fd0b2017-04-20 11:00:24 -0700767 YV12_BUFFER_CONFIG *source;
768 YV12_BUFFER_CONFIG *last_source; // NULL for first frame and alt_ref frames
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700769 YV12_BUFFER_CONFIG *unscaled_source;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770 YV12_BUFFER_CONFIG scaled_source;
771 YV12_BUFFER_CONFIG *unscaled_last_source;
772 YV12_BUFFER_CONFIG scaled_last_source;
773
Yue Chenb4c93f02019-08-05 13:47:31 -0700774 uint8_t tpl_stats_block_mis_log2; // block granularity of tpl score storage
Jingning Han81d6fbb2019-07-15 10:14:13 -0700775 TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS];
776 TplDepFrame *tpl_frame;
Yue Chen7cae98f2018-08-24 10:43:16 -0700777
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 // For a still frame, this flag is set to 1 to skip partition search.
779 int partition_search_skippable_frame;
Yunqing Wangc3e21ec2019-02-28 16:29:27 -0800780
RogerZhou3b635242017-09-19 10:06:46 -0700781 double csm_rate_array[32];
782 double m_rate_array[32];
783 int rate_size;
784 int rate_index;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -0800785 hash_table *previous_hash_table;
chiyotsaifc1404d2019-08-08 12:09:12 -0700786 int need_to_clear_prev_hash_table;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -0800787 int previous_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788
Ravi Chaudhary0ec03a42018-08-17 18:53:28 +0530789 unsigned int row_mt;
David Turnere7ebf902018-12-04 14:04:55 +0000790 RefCntBuffer *scaled_ref_buf[INTER_REFS_PER_FRAME];
Urvang Joshi4d9f15f2018-11-05 15:26:22 -0800791
David Turnere7ebf902018-12-04 14:04:55 +0000792 RefCntBuffer *last_show_frame_buf; // last show frame buffer
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793
Urvang Joshi06b37652018-11-06 11:17:56 -0800794 // refresh_*_frame are boolean flags. If 'refresh_xyz_frame' is true, then
795 // after the current frame is encoded, the XYZ reference frame gets refreshed
796 // (updated) to be the current frame.
797 //
798 // Special case: 'refresh_last_frame' specifies that:
799 // - LAST_FRAME reference should be updated to be the current frame (as usual)
800 // - Also, LAST2_FRAME and LAST3_FRAME references are implicitly updated to be
801 // the two past reference frames just before LAST_FRAME that are available.
802 //
803 // Note: Usually at most one of these refresh flags is true at a time.
804 // But a key-frame is special, for which all the flags are true at once.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 int refresh_last_frame;
806 int refresh_golden_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700807 int refresh_bwd_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808 int refresh_alt_ref_frame;
Urvang Joshi06b37652018-11-06 11:17:56 -0800809
David Turner99e990e2018-12-10 12:54:26 +0000810 // For each type of reference frame, this contains the index of a reference
811 // frame buffer for a reference frame of the same type. We use this to
812 // choose our primary reference frame (which is the most recent reference
813 // frame of the same type as the current frame).
814 int fb_of_context_type[REF_FRAMES];
815
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816 int ext_refresh_frame_flags_pending;
817 int ext_refresh_last_frame;
818 int ext_refresh_golden_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -0700819 int ext_refresh_bwd_ref_frame;
820 int ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700821 int ext_refresh_alt_ref_frame;
822
823 int ext_refresh_frame_context_pending;
824 int ext_refresh_frame_context;
sarahparker21dbca42018-03-30 17:43:44 -0700825 int ext_use_ref_frame_mvs;
sarahparker27d686a2018-03-30 17:43:44 -0700826 int ext_use_error_resilient;
sarahparker9806fed2018-03-30 17:43:44 -0700827 int ext_use_s_frame;
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700828 int ext_use_primary_ref_none;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829
830 YV12_BUFFER_CONFIG last_frame_uf;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800831 YV12_BUFFER_CONFIG trial_frame_rst;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832
833 // Ambient reconstruction err target for force key frames
834 int64_t ambient_err;
835
836 RD_OPT rd;
837
838 CODING_CONTEXT coding_context;
839
Yue Chenb23d00a2017-07-28 17:01:21 -0700840 int gmtype_cost[TRANS_TYPES];
Zoe Liu27deb382018-03-27 15:13:56 -0700841 int gmparams_cost[REF_FRAMES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700842
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843 int64_t last_time_stamp_seen;
844 int64_t last_end_time_stamp_seen;
845 int64_t first_time_stamp_ever;
846
847 RATE_CONTROL rc;
848 double framerate;
849
Yaowu Xuf883b422016-08-30 14:01:10 -0700850 struct aom_codec_pkt_list *output_pkt_list;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851
852 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
853 int mbgraph_n_frames; // number of frames filled in the above
854 int static_mb_pct; // % forced skip mbs by segmentation
855 int ref_frame_flags;
Yunqing Wangf2e7a392017-11-08 00:27:21 -0800856 int ext_ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857
David Turner04b70d82019-01-24 15:39:19 +0000858 // speed is passed as a per-frame parameter into the encoder
859 int speed;
860 // sf contains fine-grained config set internally based on speed
Yaowu Xuc27fc142016-08-22 16:08:15 -0700861 SPEED_FEATURES sf;
862
863 unsigned int max_mv_magnitude;
864 int mv_step_param;
865
Zoe Liu77fb5be2017-11-02 14:36:19 -0700866 int all_one_sided_refs;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700867
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 uint8_t *segmentation_map;
869
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870 CYCLIC_REFRESH *cyclic_refresh;
871 ActiveMap active_map;
872
873 fractional_mv_step_fp *find_fractional_mv_step;
Yaowu Xuf883b422016-08-30 14:01:10 -0700874 av1_diamond_search_fn_t diamond_search_sad;
Rupert Swarbrick93c39e92017-07-12 11:11:02 +0100875 aom_variance_fn_ptr_t fn_ptr[BLOCK_SIZES_ALL];
Yunqing Wangd1f32e62019-02-20 10:37:51 -0800876
877#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878 uint64_t time_receive_data;
879 uint64_t time_compress_data;
Yunqing Wangd1f32e62019-02-20 10:37:51 -0800880#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700881
Debargha Mukherjeee3778392019-05-06 11:44:23 -0700882 // number of show frames encoded in current gf_group
883 int num_gf_group_show_frames;
884
885 // when two pass tpl model is used, set to 1 for the
886 // first pass, then 0 for the final pass.
887 int tpl_model_pass;
Debargha Mukherjee347c64d2019-05-08 13:53:46 -0700888 // Number of gf_group frames for tpl stats
889 int tpl_gf_group_frames;
Debargha Mukherjeee3778392019-05-06 11:44:23 -0700890
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891 TWO_PASS twopass;
892
Sarah Parkere1b22012019-06-06 16:35:25 -0700893 GF_GROUP gf_group;
894
Jingning Hancf6d3252019-07-03 14:26:45 -0700895 // To control the reference frame buffer and selection.
Jingning Han42266ca2019-07-12 14:37:16 -0700896 RefBufferStack ref_buffer_stack;
Jingning Hancf6d3252019-07-03 14:26:45 -0700897
Yaowu Xuc27fc142016-08-22 16:08:15 -0700898 YV12_BUFFER_CONFIG alt_ref_buffer;
899
Yunqing Wang1973f112019-10-18 15:50:04 -0700900 // Tell if OVERLAY frame shows existing alt_ref frame.
901 int show_existing_alt_ref;
902
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903#if CONFIG_INTERNAL_STATS
904 unsigned int mode_chosen_counts[MAX_MODES];
905
906 int count;
907 uint64_t total_sq_error;
908 uint64_t total_samples;
909 ImageStat psnr;
910
911 double total_blockiness;
912 double worst_blockiness;
913
914 int bytes;
915 double summed_quality;
916 double summed_weights;
917 unsigned int tot_recode_hits;
918 double worst_ssim;
919
920 ImageStat fastssim;
921 ImageStat psnrhvs;
922
923 int b_calculate_blockiness;
924 int b_calculate_consistency;
925
926 double total_inconsistency;
927 double worst_consistency;
928 Ssimv *ssim_vars;
929 Metrics metrics;
930#endif
931 int b_calculate_psnr;
Debargha Mukherjee0857e662019-01-04 16:22:09 -0800932#if CONFIG_SPEED_STATS
933 unsigned int tx_search_count;
934#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935
936 int droppable;
937
Jingning Hanc7a52172019-09-17 15:28:46 -0700938 FRAME_INFO frame_info;
939
Yaowu Xuc27fc142016-08-22 16:08:15 -0700940 int initial_width;
941 int initial_height;
942 int initial_mbs; // Number of MBs in the full-size frame; to be used to
943 // normalize the firstpass stats. This will differ from the
944 // number of MBs in the current frame when the frame is
945 // scaled.
946
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700947 // When resize is triggered through external control, the desired width/height
948 // are stored here until use in the next frame coded. They are effective only
949 // for
950 // one frame and are reset after use.
951 int resize_pending_width;
952 int resize_pending_height;
953
Satish Kumar Suman1de46882019-02-27 17:24:18 +0530954 // ss_cfg[SS_CFG_LOOKAHEAD] : used in following cases
955 // -> temporal filtering
956 // -> intrabc
957 // ss_cfg[SS_CFG_SRC] : used everywhere except above mentioned cases
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +0530958 search_site_config ss_cfg[SS_CFG_TOTAL];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700959
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960 TileDataEnc *tile_data;
961 int allocated_tiles; // Keep track of memory allocated for tiles.
962
963 TOKENEXTRA *tile_tok[MAX_TILE_ROWS][MAX_TILE_COLS];
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530964 TOKENLIST *tplist[MAX_TILE_ROWS][MAX_TILE_COLS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700965
Yaowu Xuc27fc142016-08-22 16:08:15 -0700966 int resize_state;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700967 int resize_avg_qp;
968 int resize_buffer_underflow;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700969
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -0700970 // Sequence parameters have been transmitted already and locked
971 // or not. Once locked av1_change_config cannot change the seq
972 // parameters.
973 int seq_params_locked;
974
Yaowu Xuc27fc142016-08-22 16:08:15 -0700975 // VARIANCE_AQ segment map refresh
976 int vaq_refresh;
977
kyslov7b9d0d62018-12-21 11:12:26 -0800978 // VAR_BASED_PARTITION thresholds
979 // 0 - threshold_128x128; 1 - threshold_64x64;
980 // 2 - threshold_32x32; 3 - threshold_16x16;
981 // 4 - vbp_threshold_8x8;
982 int64_t vbp_thresholds[5];
983 int64_t vbp_threshold_minmax;
984 int64_t vbp_threshold_sad;
985 int64_t vbp_threshold_copy;
986 BLOCK_SIZE vbp_bsize_min;
987
Yunqing Wangdb70bf42019-08-19 09:28:11 -0700988 int tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES];
989 int tx_type_probs_thresh[FRAME_UPDATE_TYPES];
990
Yaowu Xuc27fc142016-08-22 16:08:15 -0700991 // Multi-threading
992 int num_workers;
Yaowu Xuf883b422016-08-30 14:01:10 -0700993 AVxWorker *workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700994 struct EncWorkerData *tile_thr_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995 int existing_fb_idx_to_show;
Urvang Joshif70375a2019-03-22 23:30:19 -0700996 int is_arf_filter_off[MAX_INTERNAL_ARFS + 1];
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -0800997 int global_motion_search_done;
Urvang Joshif70375a2019-03-22 23:30:19 -0700998 int internal_altref_allowed;
Hui Su85878782017-11-07 14:56:31 -0800999 // A flag to indicate if intrabc is ever used in current frame.
1000 int intrabc_used;
Hui Sudfcbfbd2017-11-13 12:05:30 -08001001 int dv_cost[2][MV_VALS];
1002 // TODO(huisu@google.com): we can update dv_joint_cost per SB.
1003 int dv_joint_cost[MV_JOINTS];
Hui Suad7551e2018-03-14 11:13:31 -07001004 int has_lossless_segment;
Zoe Liu1d90ceb2018-04-16 16:53:37 -07001005
Remya6924f4a2018-11-26 12:27:32 +05301006 // Factors to control gating of compound type selection based on best
1007 // approximate rd so far
1008 int max_comp_type_rd_threshold_mul;
1009 int max_comp_type_rd_threshold_div;
1010
Cherma Rajan A628efce2019-09-18 18:55:12 +05301011 // Threshold of transform domain distortion
1012 // Index 0: Default mode evaluation, Winner mode processing is not applicable
1013 // (Eg : IntraBc).
1014 // Index 1: Mode evaluation.
1015 // Index 2: Winner mode evaluation.
1016 // Index 1 and 2 are applicable when enable_winner_mode_for_use_tx_domain_dist
1017 // speed feature is ON
1018 unsigned int tx_domain_dist_threshold[MODE_EVAL_TYPES];
Venkatb62a64b2019-01-03 17:50:40 +05301019
Venkat48067652019-01-09 17:59:53 +05301020 // Factor to control R-D optimization of coeffs based on block
1021 // mse.
Cherma Rajan A628efce2019-09-18 18:55:12 +05301022 // Index 0: Default mode evaluation, Winner mode processing is not applicable
1023 // (Eg : IntraBc). Index 1: Mode evaluation.
1024 // Index 2: Winner mode evaluation
1025 // Index 1 and 2 are applicable when enable_winner_mode_for_coeff_opt speed
1026 // feature is ON
1027 unsigned int coeff_opt_dist_threshold[MODE_EVAL_TYPES];
1028
1029 // Transform size to be used in transform search
1030 // Index 0: Default mode evaluation, Winner mode processing is not applicable
1031 // (Eg : IntraBc).
1032 // Index 1: Mode evaluation. Index 2: Winner mode evaluation
1033 // Index 1 and 2 are applicable when enable_winner_mode_for_tx_size_srch speed
1034 // feature is ON
1035 TX_SIZE_SEARCH_METHOD tx_size_search_methods[MODE_EVAL_TYPES];
1036
1037 // Transform domain distortion levels
1038 // Index 0: Default mode evaluation, Winner mode processing is not applicable
1039 // (Eg : IntraBc).
1040 // Index 1: Mode evaluation. Index 2: Winner mode evaluation
1041 // Index 1 and 2 are applicable when enable_winner_mode_for_use_tx_domain_dist
1042 // speed feature is ON
1043 unsigned int use_transform_domain_distortion[MODE_EVAL_TYPES];
Venkat48067652019-01-09 17:59:53 +05301044
Deepa K G964e72e2018-05-16 16:56:01 +05301045 AV1LfSync lf_row_sync;
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05301046 AV1LrSync lr_row_sync;
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +05301047 AV1LrStruct lr_ctxt;
Neil Birkbecka2893ab2018-06-08 14:45:13 -07001048
1049 aom_film_grain_table_t *film_grain_table;
1050#if CONFIG_DENOISE
1051 struct aom_denoise_and_model_t *denoise_and_model;
1052#endif
Ranjit Kumar Tulabandu8105bf42018-07-27 14:16:55 +05301053 // Stores the default value of skip flag depending on chroma format
1054 // Set as 1 for monochrome and 3 for other color formats
1055 int default_interp_skip_flags;
Marco Paniconi67142112019-07-24 15:00:31 -07001056 int preserve_arf_as_gld;
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05301057 MultiThreadHandle multi_thread_ctxt;
Ravi Chaudhary40cdf132018-10-08 11:04:16 +05301058 void (*row_mt_sync_read_ptr)(AV1RowMTSync *const, int, int);
1059 void (*row_mt_sync_write_ptr)(AV1RowMTSync *const, int, int, const int);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05301060#if CONFIG_MULTITHREAD
1061 pthread_mutex_t *row_mt_mutex_;
1062#endif
Aniket Dhokf6d7ed82019-01-04 14:05:57 +05301063 // Set if screen content is set or relevant tools are enabled
1064 int is_screen_content_type;
chiyotsai9c484b32019-03-07 16:01:50 -08001065#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08001066 PartitionStats partition_stats;
1067#endif
Yunqing Wangba255582019-02-11 16:21:12 -08001068
1069#if CONFIG_COLLECT_COMPONENT_TIMING
1070 // component_time[] are initialized to zero while encoder starts.
1071 uint64_t component_time[kTimingComponents];
1072 struct aom_usec_timer component_timer[kTimingComponents];
1073 // frame_component_time[] are initialized to zero at beginning of each frame.
1074 uint64_t frame_component_time[kTimingComponents];
1075#endif
Hui Su81a59f12019-03-26 16:47:52 -07001076
1077 // The following data are for AV1 bitstream levels.
Hui Su2aa492c2019-03-12 15:18:18 -07001078 AV1_LEVEL target_seq_level_idx[MAX_NUM_OPERATING_POINTS];
Hui Su72ff0482019-05-28 14:07:37 -07001079 // Bit mask to indicate whether to keep level stats for corresponding
1080 // operating points.
1081 uint32_t keep_level_stats;
Hui Suc3a8d372019-05-28 15:52:45 -07001082 AV1LevelInfo *level_info[MAX_NUM_OPERATING_POINTS];
Hui Su4fd11762019-03-26 16:05:07 -07001083 // Count the number of OBU_FRAME and OBU_FRAME_HEADER for level calculation.
1084 int frame_header_count;
Debargha Mukherjee472df412019-04-29 15:00:02 -07001085
1086 // whether any no-zero delta_q was actually used
Yue Chenc87d7492019-05-30 17:22:49 -07001087 int deltaq_used;
sdengc23c7f12019-06-11 16:56:50 -07001088
Lokeshwar Reddy Bce8b7592019-07-09 18:19:55 +05301089 // Indicates the true relative distance of ref frame w.r.t. current frame
Lokeshwar Reddy B6c2c0f92019-07-31 10:18:58 +05301090 int ref_relative_dist[INTER_REFS_PER_FRAME];
Lokeshwar Reddy Bce8b7592019-07-09 18:19:55 +05301091
Lokeshwar Reddy B895efa72019-07-29 13:44:36 +05301092 // Indicate nearest references w.r.t. current frame in past and future
1093 int8_t nearest_past_ref;
1094 int8_t nearest_future_ref;
1095
sdengf46a1062019-08-04 18:43:50 -07001096 // TODO(sdeng): consider merge the following arrays.
1097 double *tpl_rdmult_scaling_factors;
1098 double *tpl_sb_rdmult_scaling_factors;
sdengc23c7f12019-06-11 16:56:50 -07001099 double *ssim_rdmult_scaling_factors;
Marco Paniconi67142112019-07-24 15:00:31 -07001100
1101 int use_svc;
1102 SVC svc;
Yaowu Xuf883b422016-08-30 14:01:10 -07001103} AV1_COMP;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001104
David Turnercb5e36f2019-01-17 17:15:25 +00001105typedef struct {
1106 YV12_BUFFER_CONFIG *source;
1107 YV12_BUFFER_CONFIG *last_source;
1108 int64_t ts_duration;
1109} EncodeFrameInput;
1110
David Turner056f7cd2019-01-07 17:48:13 +00001111// EncodeFrameParams contains per-frame encoding parameters decided upon by
1112// av1_encode_strategy() and passed down to av1_encode()
David Turner475a3132019-01-18 15:17:17 +00001113struct EncodeFrameParams {
David Turner07dbd8e2019-01-08 17:16:25 +00001114 int error_resilient_mode;
David Turner475a3132019-01-18 15:17:17 +00001115 FRAME_TYPE frame_type;
David Turnera7f133c2019-01-22 14:47:16 +00001116 int primary_ref_frame;
1117 int order_offset;
David Turnerdedd8ff2019-01-23 13:59:46 +00001118 int show_frame;
David Turner6e8b4d92019-02-18 15:01:33 +00001119 int refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00001120
David Turnere86ee0d2019-02-18 17:16:28 +00001121 int show_existing_frame;
1122 int existing_fb_idx_to_show;
1123
David Turnerfe3aecb2019-02-06 14:42:42 +00001124 // Bitmask of which reference buffers may be referenced by this frame
David Turner07dbd8e2019-01-08 17:16:25 +00001125 int ref_frame_flags;
1126
David Turner73245762019-02-11 16:42:34 +00001127 // Reference buffer assignment for this frame.
1128 int remapped_ref_idx[REF_FRAMES];
1129
David Turnerfe3aecb2019-02-06 14:42:42 +00001130 // Flags which determine which reference buffers are refreshed by this frame
1131 int refresh_last_frame;
1132 int refresh_golden_frame;
1133 int refresh_bwd_ref_frame;
David Turnerfe3aecb2019-02-06 14:42:42 +00001134 int refresh_alt_ref_frame;
1135
David Turner04b70d82019-01-24 15:39:19 +00001136 // Speed level to use for this frame: Bigger number means faster.
1137 int speed;
David Turner475a3132019-01-18 15:17:17 +00001138};
1139typedef struct EncodeFrameParams EncodeFrameParams;
David Turner056f7cd2019-01-07 17:48:13 +00001140
1141// EncodeFrameResults contains information about the result of encoding a
1142// single frame
1143typedef struct {
1144 size_t size; // Size of resulting bitstream
1145} EncodeFrameResults;
1146
Wan-Teh Chang3cac4542018-06-29 10:21:39 -07001147// Must not be called more than once.
Yaowu Xuf883b422016-08-30 14:01:10 -07001148void av1_initialize_enc(void);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001149
Yaowu Xuf883b422016-08-30 14:01:10 -07001150struct AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
1151 BufferPool *const pool);
1152void av1_remove_compressor(AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001153
Yaowu Xuf883b422016-08-30 14:01:10 -07001154void av1_change_config(AV1_COMP *cpi, const AV1EncoderConfig *oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001155
1156// receive a frames worth of data. caller can assume that a copy of this
1157// frame is made and not just a copy of the pointer..
James Zern3e2613b2017-03-30 23:14:40 -07001158int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07001159 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
1160 int64_t end_time_stamp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001161
Andrey Norkin795ba872018-03-06 13:24:14 -08001162int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
1163 size_t *size, uint8_t *dest, int64_t *time_stamp,
1164 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07001165 const aom_rational64_t *timebase);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001166
David Turner056f7cd2019-01-07 17:48:13 +00001167int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00001168 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00001169 const EncodeFrameParams *const frame_params,
1170 EncodeFrameResults *const frame_results);
1171
Yaowu Xuf883b422016-08-30 14:01:10 -07001172int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001173
Yaowu Xuf883b422016-08-30 14:01:10 -07001174int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001175
Yunqing Wang93b18f32018-06-08 21:08:29 -07001176aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
1177 YV12_BUFFER_CONFIG *new_frame,
1178 YV12_BUFFER_CONFIG *sd);
Yunqing Wangff9bfca2018-06-06 11:46:08 -07001179
Yaowu Xuf883b422016-08-30 14:01:10 -07001180int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001181
Thomas Daede497d1952017-08-08 17:33:06 -07001182int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001183
Thomas Daede497d1952017-08-08 17:33:06 -07001184int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001185
Marco Paniconi63971322019-08-15 21:32:05 -07001186int av1_set_size_literal(AV1_COMP *cpi, int width, int height);
1187
David Turner475a3132019-01-18 15:17:17 +00001188void av1_set_frame_size(AV1_COMP *cpi, int width, int height);
1189
Yaowu Xuf883b422016-08-30 14:01:10 -07001190int av1_update_entropy(AV1_COMP *cpi, int update);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001191
Yaowu Xuf883b422016-08-30 14:01:10 -07001192int av1_set_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001193
Yaowu Xuf883b422016-08-30 14:01:10 -07001194int av1_get_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001195
Yaowu Xuf883b422016-08-30 14:01:10 -07001196int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
1197 AOM_SCALING vert_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001198
Yaowu Xuf883b422016-08-30 14:01:10 -07001199int av1_get_quantizer(struct AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001200
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04001201int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *input_size);
1202
Hui Su38711e72019-06-11 10:49:47 -07001203void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
1204 CompoundTypeRdBuffers *const bufs);
1205void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs);
1206
Jingning Hancf6d3252019-07-03 14:26:45 -07001207// TODO(jingning): Move these functions as primitive members for the new cpi
1208// class.
1209static INLINE void stack_push(int *stack, int *stack_size, int item) {
Jingning Han0a2af4e2019-07-08 19:30:03 -07001210 for (int i = *stack_size - 1; i >= 0; --i) stack[i + 1] = stack[i];
Jingning Hancf6d3252019-07-03 14:26:45 -07001211 stack[0] = item;
1212 ++*stack_size;
1213}
1214
1215static INLINE int stack_pop(int *stack, int *stack_size) {
Jingning Hanf58175c2019-07-07 15:02:00 -07001216 if (*stack_size <= 0) return -1;
1217
Jingning Hancf6d3252019-07-03 14:26:45 -07001218 int item = stack[0];
1219 for (int i = 0; i < *stack_size; ++i) stack[i] = stack[i + 1];
1220 --*stack_size;
1221
1222 return item;
1223}
1224
1225static INLINE int stack_pop_end(int *stack, int *stack_size) {
1226 int item = stack[*stack_size - 1];
1227 stack[*stack_size - 1] = -1;
1228 --*stack_size;
1229
1230 return item;
1231}
1232
1233static INLINE void stack_reset(int *stack, int *stack_size) {
1234 for (int i = 0; i < *stack_size; ++i) stack[i] = INVALID_IDX;
1235 *stack_size = 0;
1236}
1237
David Turnerdedd8ff2019-01-23 13:59:46 +00001238// av1 uses 10,000,000 ticks/second as time stamp
1239#define TICKS_PER_SEC 10000000LL
1240
Yue Chen1bc5be62018-08-24 13:57:32 -07001241static INLINE int64_t
1242timebase_units_to_ticks(const aom_rational64_t *timestamp_ratio, int64_t n) {
1243 return n * timestamp_ratio->num / timestamp_ratio->den;
David Turnerdedd8ff2019-01-23 13:59:46 +00001244}
1245
Yue Chen1bc5be62018-08-24 13:57:32 -07001246static INLINE int64_t
1247ticks_to_timebase_units(const aom_rational64_t *timestamp_ratio, int64_t n) {
1248 int64_t round = timestamp_ratio->num / 2;
1249 if (round > 0) --round;
1250 return (n * timestamp_ratio->den + round) / timestamp_ratio->num;
David Turnerdedd8ff2019-01-23 13:59:46 +00001251}
Andrey Norkin795ba872018-03-06 13:24:14 -08001252
Yaowu Xuf883b422016-08-30 14:01:10 -07001253static INLINE int frame_is_kf_gf_arf(const AV1_COMP *cpi) {
Jingning Han52af4392019-08-13 11:44:40 -07001254 const GF_GROUP *const gf_group = &cpi->gf_group;
1255 const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_group->index];
1256
1257 return frame_is_intra_only(&cpi->common) || update_type == ARF_UPDATE ||
1258 update_type == GF_UPDATE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001259}
1260
Hui Su2d5fd742018-02-21 18:10:37 -08001261// TODO(huisu@google.com, youzhou@microsoft.com): enable hash-me for HBD.
1262static INLINE int av1_use_hash_me(const AV1_COMMON *const cm) {
Debargha Mukherjee8112d2f2018-03-01 09:53:11 -08001263 return cm->allow_screen_content_tools;
Hui Su2d5fd742018-02-21 18:10:37 -08001264}
1265
1266static INLINE hash_table *av1_get_ref_frame_hash_map(
David Turnera21966b2018-12-05 14:48:49 +00001267 const AV1_COMMON *cm, MV_REFERENCE_FRAME ref_frame) {
1268 const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1269 RefCntBuffer *buf =
1270 (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
1271 return buf ? &buf->hash_table : NULL;
RogerZhoucc5d35d2017-08-07 22:20:15 -07001272}
RogerZhoucc5d35d2017-08-07 22:20:15 -07001273
David Turnera21966b2018-12-05 14:48:49 +00001274static INLINE const YV12_BUFFER_CONFIG *get_ref_frame_yv12_buf(
1275 const AV1_COMMON *const cm, MV_REFERENCE_FRAME ref_frame) {
1276 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00001277 return buf != NULL ? &buf->buf : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001278}
1279
David Turnera21966b2018-12-05 14:48:49 +00001280static INLINE int enc_is_ref_frame_buf(const AV1_COMMON *const cm,
1281 const RefCntBuffer *const frame_buf) {
Urvang Joshi40582172018-11-07 11:53:03 -08001282 MV_REFERENCE_FRAME ref_frame;
1283 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00001284 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00001285 if (buf == NULL) continue;
1286 if (frame_buf == buf) break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001287 }
Urvang Joshi40582172018-11-07 11:53:03 -08001288 return (ref_frame <= ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001289}
Yaowu Xuc27fc142016-08-22 16:08:15 -07001290
David Turnere3e520d2019-01-18 14:38:08 +00001291static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, RefCntBuffer *buf) {
1292 assert(buf != NULL);
1293 ensure_mv_buffer(buf, cm);
1294 buf->width = cm->width;
1295 buf->height = cm->height;
1296}
1297
Hui Su8ac0c982018-04-03 12:17:36 -07001298// Token buffer is only used for palette tokens.
Yaowu Xue39b3b82017-10-31 16:11:59 -07001299static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001300 int sb_size_log2,
1301 const int num_planes) {
Rupert Swarbrickce63e832017-10-25 17:54:17 +01001302 // Calculate the maximum number of max superblocks in the image.
Yaowu Xue39b3b82017-10-31 16:11:59 -07001303 const int shift = sb_size_log2 - 4;
1304 const int sb_size = 1 << sb_size_log2;
1305 const int sb_size_square = sb_size * sb_size;
Rupert Swarbrickce63e832017-10-25 17:54:17 +01001306 const int sb_rows = ALIGN_POWER_OF_TWO(mb_rows, shift) >> shift;
1307 const int sb_cols = ALIGN_POWER_OF_TWO(mb_cols, shift) >> shift;
1308
Hui Su8ac0c982018-04-03 12:17:36 -07001309 // One palette token for each pixel. There can be palettes on two planes.
1310 const int sb_palette_toks = AOMMIN(2, num_planes) * sb_size_square;
Rupert Swarbrickce63e832017-10-25 17:54:17 +01001311
Hui Su8ac0c982018-04-03 12:17:36 -07001312 return sb_rows * sb_cols * sb_palette_toks;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001313}
1314
1315// Get the allocated token size for a tile. It does the same calculation as in
1316// the frame token allocation.
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001317static INLINE unsigned int allocated_tokens(TileInfo tile, int sb_size_log2,
1318 int num_planes) {
Jingning Haneafbd5f2017-03-07 11:18:17 -08001319 int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 2) >> 2;
1320 int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 2) >> 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001321
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001322 return get_token_alloc(tile_mb_rows, tile_mb_cols, sb_size_log2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001323}
1324
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301325static INLINE void get_start_tok(AV1_COMP *cpi, int tile_row, int tile_col,
1326 int mi_row, TOKENEXTRA **tok, int sb_size_log2,
1327 int num_planes) {
1328 AV1_COMMON *const cm = &cpi->common;
1329 const int tile_cols = cm->tile_cols;
1330 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
1331 const TileInfo *const tile_info = &this_tile->tile_info;
1332
1333 const int tile_mb_cols =
1334 (tile_info->mi_col_end - tile_info->mi_col_start + 2) >> 2;
1335 const int tile_mb_row = (mi_row - tile_info->mi_row_start + 2) >> 2;
1336
1337 *tok = cpi->tile_tok[tile_row][tile_col] +
1338 get_token_alloc(tile_mb_row, tile_mb_cols, sb_size_log2, num_planes);
1339}
1340
Yaowu Xuf883b422016-08-30 14:01:10 -07001341void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001342
Yaowu Xu6da42302017-10-10 14:45:06 -07001343#define ALT_MIN_LAG 3
Yaowu Xuf883b422016-08-30 14:01:10 -07001344static INLINE int is_altref_enabled(const AV1_COMP *const cpi) {
Yaowu Xu6da42302017-10-10 14:45:06 -07001345 return cpi->oxcf.lag_in_frames >= ALT_MIN_LAG && cpi->oxcf.enable_auto_arf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001346}
1347
1348// TODO(zoeliu): To set up cpi->oxcf.enable_auto_brf
Yaowu Xuc27fc142016-08-22 16:08:15 -07001349
Urvang Joshi52648442016-10-13 17:27:51 -07001350static INLINE void set_ref_ptrs(const AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001351 MV_REFERENCE_FRAME ref0,
1352 MV_REFERENCE_FRAME ref1) {
David Turnera21966b2018-12-05 14:48:49 +00001353 xd->block_ref_scale_factors[0] =
1354 get_ref_scale_factors_const(cm, ref0 >= LAST_FRAME ? ref0 : 1);
1355 xd->block_ref_scale_factors[1] =
1356 get_ref_scale_factors_const(cm, ref1 >= LAST_FRAME ? ref1 : 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001357}
1358
Yaowu Xu4ff59b52017-04-24 12:41:56 -07001359static INLINE int get_chessboard_index(int frame_index) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001360 return frame_index & 0x1;
1361}
1362
Yaowu Xuf883b422016-08-30 14:01:10 -07001363static INLINE int *cond_cost_list(const struct AV1_COMP *cpi, int *cost_list) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001364 return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
1365}
1366
Hui Suef139e12019-05-20 15:51:22 -07001367// Compression ratio of current frame.
1368double av1_get_compression_ratio(const AV1_COMMON *const cm,
1369 size_t encoded_frame_size);
1370
Yaowu Xuf883b422016-08-30 14:01:10 -07001371void av1_new_framerate(AV1_COMP *cpi, double framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001372
David Turnerdedd8ff2019-01-23 13:59:46 +00001373void av1_setup_frame_size(AV1_COMP *cpi);
1374
Yaowu Xuc27fc142016-08-22 16:08:15 -07001375#define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
1376
Cheng Chen09c83a52018-06-05 12:27:36 -07001377// Returns 1 if a frame is scaled and 0 otherwise.
1378static INLINE int av1_resize_scaled(const AV1_COMMON *cm) {
1379 return !(cm->superres_upscaled_width == cm->render_width &&
1380 cm->superres_upscaled_height == cm->render_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07001381}
1382
Cheng Chen09c83a52018-06-05 12:27:36 -07001383static INLINE int av1_frame_scaled(const AV1_COMMON *cm) {
1384 return !av1_superres_scaled(cm) && av1_resize_scaled(cm);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07001385}
1386
Sarah Parker33005522018-07-27 14:46:25 -07001387// Don't allow a show_existing_frame to coincide with an error resilient
1388// frame. An exception can be made for a forward keyframe since it has no
1389// previous dependencies.
1390static INLINE int encode_show_existing_frame(const AV1_COMMON *cm) {
David Turnerd2a592e2018-11-16 14:59:31 +00001391 return cm->show_existing_frame && (!cm->error_resilient_mode ||
1392 cm->current_frame.frame_type == KEY_FRAME);
Sarah Parker33005522018-07-27 14:46:25 -07001393}
1394
kyslov7b9d0d62018-12-21 11:12:26 -08001395// Lighter version of set_offsets that only sets the mode info
1396// pointers.
1397static INLINE void set_mode_info_offsets(const AV1_COMP *const cpi,
1398 MACROBLOCK *const x,
1399 MACROBLOCKD *const xd, int mi_row,
1400 int mi_col) {
1401 const AV1_COMMON *const cm = &cpi->common;
chiyotsaia7091f12019-08-09 16:48:27 -07001402 const int grid_idx = get_mi_grid_idx(cm, mi_row, mi_col);
1403 const int mi_idx = get_alloc_mi_idx(cm, mi_row, mi_col);
1404 const int ext_idx = get_mi_ext_idx(cm, mi_row, mi_col);
chiyotsai426c0662019-08-05 16:15:11 -07001405
chiyotsaia7091f12019-08-09 16:48:27 -07001406 xd->mi = cm->mi_grid_base + grid_idx;
1407 xd->mi[0] = cm->mi + mi_idx;
Hui Su52b7ddc2019-10-10 16:27:16 -07001408 xd->tx_type_map = cm->tx_type_map + grid_idx;
1409 xd->tx_type_map_stride = cm->mi_stride;
Remya0cce44c2019-08-16 11:57:24 +05301410 x->mbmi_ext_frame = cpi->mbmi_ext_frame_base + ext_idx;
kyslov7b9d0d62018-12-21 11:12:26 -08001411}
1412
1413// Check to see if the given partition size is allowed for a specified number
1414// of mi block rows and columns remaining in the image.
1415// If not then return the largest allowed partition size
1416static INLINE BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize, int rows_left,
1417 int cols_left, int *bh, int *bw) {
1418 int int_size = (int)bsize;
1419 if (rows_left <= 0 || cols_left <= 0) {
1420 return AOMMIN(bsize, BLOCK_8X8);
1421 } else {
1422 for (; int_size > 0; int_size -= 3) {
1423 *bh = mi_size_high[int_size];
1424 *bw = mi_size_wide[int_size];
1425 if ((*bh <= rows_left) && (*bw <= cols_left)) {
1426 break;
1427 }
1428 }
1429 }
1430 return (BLOCK_SIZE)int_size;
1431}
1432
chiyotsaidd132d22019-03-11 14:22:49 -07001433static const uint8_t av1_ref_frame_flag_list[REF_FRAMES] = { 0,
1434 AOM_LAST_FLAG,
1435 AOM_LAST2_FLAG,
1436 AOM_LAST3_FLAG,
1437 AOM_GOLD_FLAG,
1438 AOM_BWD_FLAG,
1439 AOM_ALT2_FLAG,
1440 AOM_ALT_FLAG };
1441
Deepa K G33f1ab72019-09-23 17:59:06 +05301442// When more than 'max_allowed_refs' are available, we reduce the number of
1443// reference frames one at a time based on this order.
1444static const MV_REFERENCE_FRAME disable_order[] = {
1445 LAST3_FRAME,
1446 LAST2_FRAME,
1447 ALTREF2_FRAME,
1448 GOLDEN_FRAME,
1449};
1450
1451static INLINE int get_max_allowed_ref_frames(const AV1_COMP *cpi) {
1452 const unsigned int max_allowed_refs_for_given_speed =
1453 (cpi->sf.selective_ref_frame >= 3) ? INTER_REFS_PER_FRAME - 1
1454 : INTER_REFS_PER_FRAME;
1455 return AOMMIN(max_allowed_refs_for_given_speed,
1456 cpi->oxcf.max_reference_frames);
1457}
1458
Tom Fineganf8d6a162018-08-21 10:47:55 -07001459// Returns a Sequence Header OBU stored in an aom_fixed_buf_t, or NULL upon
1460// failure. When a non-NULL aom_fixed_buf_t pointer is returned by this
1461// function, the memory must be freed by the caller. Both the buf member of the
Tom Fineganca1e28f2018-08-28 16:55:35 -07001462// aom_fixed_buf_t, and the aom_fixed_buf_t pointer itself must be freed. Memory
1463// returned must be freed via call to free().
1464//
1465// Note: The OBU returned is in Low Overhead Bitstream Format. Specifically,
1466// the obu_has_size_field bit is set, and the buffer contains the obu_size
1467// field.
Tom Fineganf8d6a162018-08-21 10:47:55 -07001468aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi);
1469
Yue Chen4e585cc2019-06-03 14:47:16 -07001470#define ENABLE_KF_TPL 1
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07001471#define MAX_PYR_LEVEL_FROMTOP_DELTAQ 0
Yue Chen4e585cc2019-06-03 14:47:16 -07001472
1473static INLINE int is_frame_kf_and_tpl_eligible(AV1_COMP *const cpi) {
1474 AV1_COMMON *cm = &cpi->common;
Urvang Joshi05fc9fa2019-10-10 15:49:31 -07001475 return (cm->current_frame.frame_type == KEY_FRAME) && cm->show_frame &&
1476 (cpi->rc.frames_to_key > 1);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07001477}
1478
Jingning Han44a573b2019-09-04 14:01:22 -07001479static INLINE int is_frame_arf_and_tpl_eligible(const GF_GROUP *gf_group) {
1480 const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_group->index];
1481 return update_type == ARF_UPDATE || update_type == GF_UPDATE;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07001482}
1483
Yue Chen4e585cc2019-06-03 14:47:16 -07001484static INLINE int is_frame_tpl_eligible(AV1_COMP *const cpi) {
1485#if ENABLE_KF_TPL
1486 return is_frame_kf_and_tpl_eligible(cpi) ||
Jingning Han44a573b2019-09-04 14:01:22 -07001487 is_frame_arf_and_tpl_eligible(&cpi->gf_group);
Yue Chen4e585cc2019-06-03 14:47:16 -07001488#else
Jingning Han44a573b2019-09-04 14:01:22 -07001489 return is_frame_arf_and_tpl_eligible(&cpi->gf_group);
Yue Chen4e585cc2019-06-03 14:47:16 -07001490#endif // ENABLE_KF_TPL
1491}
1492
Yunqing Wangdb70bf42019-08-19 09:28:11 -07001493// Get update type of the current frame.
Jingning Hancc914b42019-09-04 14:49:46 -07001494static INLINE FRAME_UPDATE_TYPE
1495get_frame_update_type(const GF_GROUP *gf_group) {
Yunqing Wangdb70bf42019-08-19 09:28:11 -07001496 return gf_group->update_type[gf_group->index];
1497}
1498
Hui Su30573182019-10-08 16:24:34 -07001499static INLINE int av1_pixels_to_mi(int pixels) {
1500 return ALIGN_POWER_OF_TWO(pixels, 3) >> MI_SIZE_LOG2;
1501}
1502
chiyotsai9c484b32019-03-07 16:01:50 -08001503#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08001504static INLINE void av1_print_partition_stats(PartitionStats *part_stats) {
1505 FILE *f = fopen("partition_stats.csv", "w");
1506 if (!f) {
1507 return;
1508 }
1509
1510 fprintf(f, "bsize,redo,");
1511 for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1512 fprintf(f, "decision_%d,", part);
1513 }
1514 for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1515 fprintf(f, "attempt_%d,", part);
1516 }
chiyotsai9c484b32019-03-07 16:01:50 -08001517 for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1518 fprintf(f, "time_%d,", part);
1519 }
chiyotsai92ed0dd2019-01-25 14:50:14 -08001520 fprintf(f, "\n");
1521
1522 const int bsizes[6] = { 128, 64, 32, 16, 8, 4 };
1523
1524 for (int bsize_idx = 0; bsize_idx < 6; bsize_idx++) {
1525 fprintf(f, "%d,%d,", bsizes[bsize_idx], part_stats->partition_redo);
1526 for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1527 fprintf(f, "%d,", part_stats->partition_decisions[bsize_idx][part]);
1528 }
1529 for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1530 fprintf(f, "%d,", part_stats->partition_attempts[bsize_idx][part]);
1531 }
chiyotsai9c484b32019-03-07 16:01:50 -08001532 for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1533 fprintf(f, "%ld,", part_stats->partition_times[bsize_idx][part]);
1534 }
chiyotsai92ed0dd2019-01-25 14:50:14 -08001535 fprintf(f, "\n");
1536 }
1537 fclose(f);
1538}
1539
1540static INLINE int av1_get_bsize_idx_for_part_stats(BLOCK_SIZE bsize) {
1541 assert(bsize == BLOCK_128X128 || bsize == BLOCK_64X64 ||
chiyotsai9c484b32019-03-07 16:01:50 -08001542 bsize == BLOCK_32X32 || bsize == BLOCK_16X16 || bsize == BLOCK_8X8 ||
1543 bsize == BLOCK_4X4);
chiyotsai92ed0dd2019-01-25 14:50:14 -08001544 switch (bsize) {
1545 case BLOCK_128X128: return 0;
1546 case BLOCK_64X64: return 1;
1547 case BLOCK_32X32: return 2;
1548 case BLOCK_16X16: return 3;
1549 case BLOCK_8X8: return 4;
1550 case BLOCK_4X4: return 5;
1551 default: assert(0 && "Invalid bsize for partition_stats."); return -1;
1552 }
1553}
1554#endif
1555
Yunqing Wangba255582019-02-11 16:21:12 -08001556#if CONFIG_COLLECT_COMPONENT_TIMING
1557static INLINE void start_timing(AV1_COMP *cpi, int component) {
1558 aom_usec_timer_start(&cpi->component_timer[component]);
1559}
1560static INLINE void end_timing(AV1_COMP *cpi, int component) {
1561 aom_usec_timer_mark(&cpi->component_timer[component]);
1562 cpi->frame_component_time[component] +=
1563 aom_usec_timer_elapsed(&cpi->component_timer[component]);
1564}
1565static INLINE char const *get_frame_type_enum(int type) {
1566 switch (type) {
1567 case 0: return "KEY_FRAME";
1568 case 1: return "INTER_FRAME";
1569 case 2: return "INTRA_ONLY_FRAME";
1570 case 3: return "S_FRAME";
1571 default: assert(0);
1572 }
1573 return "error";
1574}
1575#endif
1576
Yaowu Xuc27fc142016-08-22 16:08:15 -07001577#ifdef __cplusplus
1578} // extern "C"
1579#endif
1580
James Zerne1cbb132018-08-22 14:10:36 -07001581#endif // AOM_AV1_ENCODER_ENCODER_H_