blob: 53c74e9f35aa6c555134acb8e1fb566841059b95 [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
15#include <stdio.h>
16
Tom Finegan60e653d2018-05-22 11:34:58 -070017#include "config/aom_config.h"
18
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "aom/aomcx.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020
21#include "av1/common/alloccommon.h"
22#include "av1/common/entropymode.h"
23#include "av1/common/thread_common.h"
24#include "av1/common/onyxc_int.h"
Fergus Simpsond2bcbb52017-05-22 23:15:05 -070025#include "av1/common/resize.h"
Andrey Norkin795ba872018-03-06 13:24:14 -080026#include "av1/common/timing.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/encoder/aq_cyclicrefresh.h"
Tom Finegan17ce8b12017-02-08 12:46:31 -080028#include "av1/encoder/av1_quantize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029#include "av1/encoder/context_tree.h"
30#include "av1/encoder/encodemb.h"
31#include "av1/encoder/firstpass.h"
32#include "av1/encoder/lookahead.h"
33#include "av1/encoder/mbgraph.h"
34#include "av1/encoder/mcomp.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070035#include "av1/encoder/ratectrl.h"
36#include "av1/encoder/rd.h"
37#include "av1/encoder/speed_features.h"
38#include "av1/encoder/tokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070039
40#if CONFIG_INTERNAL_STATS
41#include "aom_dsp/ssim.h"
42#endif
43#include "aom_dsp/variance.h"
Neil Birkbecka2893ab2018-06-08 14:45:13 -070044#if CONFIG_DENOISE
45#include "aom_dsp/noise_model.h"
46#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070047#include "aom/internal/aom_codec_internal.h"
48#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070049
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54typedef struct {
Jingning Hanf050fc12018-03-09 14:53:33 -080055 int nmv_vec_cost[MV_JOINTS];
56 int nmv_costs[2][MV_VALS];
57 int nmv_costs_hp[2][MV_VALS];
James Zern01a9d702017-08-25 19:09:33 +000058
Yaowu Xuc27fc142016-08-22 16:08:15 -070059 FRAME_CONTEXT fc;
60} CODING_CONTEXT;
61
62typedef enum {
63 // regular inter frame
64 REGULAR_FRAME = 0,
65 // alternate reference frame
66 ARF_FRAME = 1,
67 // overlay frame
68 OVERLAY_FRAME = 2,
69 // golden frame
70 GLD_FRAME = 3,
Yaowu Xuc27fc142016-08-22 16:08:15 -070071 // backward reference frame
72 BRF_FRAME = 4,
73 // extra alternate reference frame
Thomas Daede51020e12017-12-14 20:12:44 -080074 EXT_ARF_FRAME = 5,
75 FRAME_CONTEXT_INDEXES
Yaowu Xuc27fc142016-08-22 16:08:15 -070076} FRAME_CONTEXT_INDEX;
77
78typedef enum {
Yaowu Xuc27fc142016-08-22 16:08:15 -070079 NORMAL = 0,
80 FOURFIVE = 1,
81 THREEFIVE = 2,
82 ONETWO = 3
Yaowu Xuf883b422016-08-30 14:01:10 -070083} AOM_SCALING;
Yaowu Xuc27fc142016-08-22 16:08:15 -070084
85typedef enum {
86 // Good Quality Fast Encoding. The encoder balances quality with the amount of
87 // time it takes to encode the output. Speed setting controls how fast.
Thomas Daede80826142017-03-20 15:44:24 -070088 GOOD
Yaowu Xuc27fc142016-08-22 16:08:15 -070089} MODE;
90
91typedef enum {
92 FRAMEFLAGS_KEY = 1 << 0,
93 FRAMEFLAGS_GOLDEN = 1 << 1,
Yaowu Xuc27fc142016-08-22 16:08:15 -070094 FRAMEFLAGS_BWDREF = 1 << 2,
Zoe Liu3ac20932017-08-30 16:35:55 -070095 // TODO(zoeliu): To determine whether a frame flag is needed for ALTREF2_FRAME
Yaowu Xuc27fc142016-08-22 16:08:15 -070096 FRAMEFLAGS_ALTREF = 1 << 3,
Debargha Mukherjee57378252018-09-21 18:29:37 -070097 FRAMEFLAGS_INTRAONLY = 1 << 4,
98 FRAMEFLAGS_SWITCH = 1 << 5,
99 FRAMEFLAGS_ERROR_RESILIENT = 1 << 6,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700100} FRAMETYPE_FLAGS;
101
102typedef enum {
103 NO_AQ = 0,
104 VARIANCE_AQ = 1,
105 COMPLEXITY_AQ = 2,
106 CYCLIC_REFRESH_AQ = 3,
107 AQ_MODE_COUNT // This should always be the last member of the enum
108} AQ_MODE;
Fangwen Fu6160df22017-04-24 09:45:51 -0700109typedef enum {
110 NO_DELTA_Q = 0,
111 DELTA_Q_ONLY = 1,
112 DELTA_Q_LF = 2,
113 DELTAQ_MODE_COUNT // This should always be the last member of the enum
114} DELTAQ_MODE;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800115
Yaowu Xuc27fc142016-08-22 16:08:15 -0700116typedef enum {
117 RESIZE_NONE = 0, // No frame resizing allowed.
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700118 RESIZE_FIXED = 1, // All frames are coded at the specified scale.
119 RESIZE_RANDOM = 2, // All frames are coded at a random scale.
120 RESIZE_MODES
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700121} RESIZE_MODE;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800122
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700123typedef enum {
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700124 SUPERRES_NONE = 0, // No frame superres allowed
125 SUPERRES_FIXED = 1, // All frames are coded at the specified scale,
126 // and super-resolved.
127 SUPERRES_RANDOM = 2, // All frames are coded at a random scale,
128 // and super-resolved.
129 SUPERRES_QTHRESH = 3, // Superres scale for a frame is determined based on
130 // q_index
131 SUPERRES_MODES
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700132} SUPERRES_MODE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700133
Yue Chen7cae98f2018-08-24 10:43:16 -0700134typedef struct TplDepStats {
135 int64_t intra_cost;
136 int64_t inter_cost;
137 int64_t mc_flow;
138 int64_t mc_dep_cost;
139 int64_t mc_ref_cost;
140
141 int ref_frame_index;
142 int_mv mv;
143} TplDepStats;
144
145typedef struct TplDepFrame {
146 uint8_t is_valid;
147 TplDepStats *tpl_stats_ptr;
148 int stride;
149 int width;
150 int height;
151 int mi_rows;
152 int mi_cols;
153 int base_qindex;
154} TplDepFrame;
155
156#define TPL_DEP_COST_SCALE_LOG2 4
157
Yaowu Xuf883b422016-08-30 14:01:10 -0700158typedef struct AV1EncoderConfig {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159 BITSTREAM_PROFILE profile;
Tom Finegan8ab2bba2018-02-28 07:36:28 -0800160 aom_bit_depth_t bit_depth; // Codec bit-depth.
161 int width; // width of data passed to the compressor
162 int height; // height of data passed to the compressor
163 int forced_max_frame_width; // forced maximum width of frame (if != 0)
164 int forced_max_frame_height; // forced maximum height of frame (if != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 unsigned int input_bit_depth; // Input bit depth.
166 double init_framerate; // set to passed in framerate
167 int64_t target_bandwidth; // bandwidth to be used in bits per second
168
169 int noise_sensitivity; // pre processing blur: recommendation 0
170 int sharpness; // sharpening output: recommendation 0:
171 int speed;
172 // maximum allowed bitrate for any intra frame in % of bitrate target.
173 unsigned int rc_max_intra_bitrate_pct;
174 // maximum allowed bitrate for any inter frame in % of bitrate target.
175 unsigned int rc_max_inter_bitrate_pct;
176 // percent of rate boost for golden frame in CBR mode.
177 unsigned int gf_cbr_boost_pct;
178
179 MODE mode;
180 int pass;
181
182 // Key Framing Operations
183 int auto_key; // autodetect cut scenes and set the keyframes
184 int key_freq; // maximum distance to key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -0500185 int sframe_dist;
186 int sframe_mode;
187 int sframe_enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188 int lag_in_frames; // how many frames lag before we start encoding
Sarah Parker93c03142018-05-22 13:35:45 -0700189 int fwd_kf_enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190
191 // ----------------------------------------------------------------
192 // DATARATE CONTROL OPTIONS
193
194 // vbr, cbr, constrained quality or constant quality
Yaowu Xuf883b422016-08-30 14:01:10 -0700195 enum aom_rc_mode rc_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700196
197 // buffer targeting aggressiveness
198 int under_shoot_pct;
199 int over_shoot_pct;
200
201 // buffering parameters
202 int64_t starting_buffer_level_ms;
203 int64_t optimal_buffer_level_ms;
204 int64_t maximum_buffer_size_ms;
205
206 // Frame drop threshold.
207 int drop_frames_water_mark;
208
209 // controlling quality
210 int fixed_q;
211 int worst_allowed_q;
212 int best_allowed_q;
213 int cq_level;
214 AQ_MODE aq_mode; // Adaptive Quantization mode
Fangwen Fu6160df22017-04-24 09:45:51 -0700215 DELTAQ_MODE deltaq_mode;
Debargha Mukherjee98a311c2018-03-25 16:33:11 -0700216 int enable_cdef;
217 int enable_restoration;
Yue Chen4835dc02018-05-11 15:57:43 -0700218 int disable_trellis_quant;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 int using_qm;
Yaowu Xuf7a12422018-01-31 15:29:20 -0800220 int qm_y;
221 int qm_u;
222 int qm_v;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223 int qm_minlevel;
224 int qm_maxlevel;
Yushin Chod808bfc2017-08-10 15:54:36 -0700225#if CONFIG_DIST_8X8
226 int using_dist_8x8;
227#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000228 unsigned int num_tile_groups;
229 unsigned int mtu;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700230
231 // Internal frame size scaling.
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700232 RESIZE_MODE resize_mode;
Urvang Joshide71d142017-10-05 12:12:15 -0700233 uint8_t resize_scale_denominator;
234 uint8_t resize_kf_scale_denominator;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700236 // Frame Super-Resolution size scaling.
237 SUPERRES_MODE superres_mode;
Urvang Joshide71d142017-10-05 12:12:15 -0700238 uint8_t superres_scale_denominator;
239 uint8_t superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700240 int superres_qthresh;
241 int superres_kf_qthresh;
Fergus Simpson3502d082017-04-10 12:25:07 -0700242
Yaowu Xuc27fc142016-08-22 16:08:15 -0700243 // Enable feature to reduce the frame quantization every x frames.
244 int frame_periodic_boost;
245
246 // two pass datarate control
247 int two_pass_vbrbias; // two pass datarate control tweaks
248 int two_pass_vbrmin_section;
249 int two_pass_vbrmax_section;
250 // END DATARATE CONTROL OPTIONS
251 // ----------------------------------------------------------------
252
253 int enable_auto_arf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254 int enable_auto_brf; // (b)ackward (r)ef (f)rame
Yaowu Xuc27fc142016-08-22 16:08:15 -0700255
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256 /* Bitfield defining the error resiliency features to enable.
257 * Can provide decodable frames after losses in previous
258 * frames and decodable partitions after losses in the same frame.
259 */
260 unsigned int error_resilient_mode;
261
Debargha Mukherjee52fb0472018-03-29 15:48:11 -0700262 unsigned int s_frame_mode;
263
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264 /* Bitfield defining the parallel decoding mode where the
265 * decoding in successive frames may be conducted in parallel
266 * just by decoding the frame headers.
267 */
268 unsigned int frame_parallel_decoding_mode;
269
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700270 unsigned int limit;
271
Yaowu Xuc27fc142016-08-22 16:08:15 -0700272 int arnr_max_frames;
273 int arnr_strength;
274
275 int min_gf_interval;
276 int max_gf_interval;
277
Ravi Chaudhary0ec03a42018-08-17 18:53:28 +0530278 int row_mt;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279 int tile_columns;
280 int tile_rows;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200281 int tile_width_count;
282 int tile_height_count;
283 int tile_widths[MAX_TILE_COLS];
284 int tile_heights[MAX_TILE_ROWS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700285
Yue Chen7cae98f2018-08-24 10:43:16 -0700286 int enable_tpl_model;
287
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288 int max_threads;
289
Yaowu Xuf883b422016-08-30 14:01:10 -0700290 aom_fixed_buf_t two_pass_stats_in;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700291
292#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -0700293 aom_fixed_buf_t firstpass_mb_stats_in;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294#endif
295
Yaowu Xuf883b422016-08-30 14:01:10 -0700296 aom_tune_metric tuning;
297 aom_tune_content content;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298 int use_highbitdepth;
Andrey Norkin9e694632017-12-21 18:50:57 -0800299 aom_color_primaries_t color_primaries;
300 aom_transfer_characteristics_t transfer_characteristics;
301 aom_matrix_coefficients_t matrix_coefficients;
anorkin76fb1262017-03-22 15:12:12 -0700302 aom_chroma_sample_position_t chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303 int color_range;
304 int render_width;
305 int render_height;
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800306 int timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -0800307 aom_timing_info_t timing_info;
308 int decoder_model_info_present_flag;
Andrey Norkin26495512018-06-20 17:13:11 -0700309 int display_model_info_present_flag;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -0700310 int buffer_removal_time_present;
Andrey Norkin795ba872018-03-06 13:24:14 -0800311 aom_dec_model_info_t buffer_model;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800312 int film_grain_test_vector;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700313 const char *film_grain_table_filename;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700314
Hui Su1cb1c002018-02-05 18:21:20 -0800315 uint8_t cdf_update_mode;
Yaowu Xuf883b422016-08-30 14:01:10 -0700316 aom_superblock_size_t superblock_size;
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700317 unsigned int large_scale_tile;
318 unsigned int single_tile_decoding;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800319 int monochrome;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -0700320 unsigned int full_still_picture_hdr;
Jingning Hana446f552018-02-22 15:42:12 -0800321 int enable_dual_filter;
Yunqing Wangff4fa062017-04-21 10:56:08 -0700322 unsigned int motion_vector_unit_test;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100323 const cfg_options_t *cfg;
Debargha Mukherjee0d8368a2018-03-25 12:23:02 -0700324 int enable_order_hint;
Cheng Chene0c918a2018-02-22 19:38:31 -0800325 int enable_jnt_comp;
Debargha Mukherjee0187d7c2018-03-25 11:37:56 -0700326 int enable_ref_frame_mvs;
Debargha Mukherjee0d8368a2018-03-25 12:23:02 -0700327 unsigned int allow_ref_frame_mvs;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -0800328 int enable_masked_comp;
329 int enable_interintra_comp;
Debargha Mukherjeed2e53ca2018-12-10 11:34:59 -0800330 int enable_global_motion;
Debargha Mukherjee37df9162018-03-25 12:48:24 -0700331 int enable_warped_motion;
332 int allow_warped_motion;
Urvang Joshi2c92b072018-03-19 17:23:31 -0700333 int enable_superres;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400334 unsigned int save_as_annexb;
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700335
336#if CONFIG_DENOISE
337 float noise_level;
338 int noise_block_size;
339#endif
Tom Finegan02b2a842018-08-24 13:50:00 -0700340
341 unsigned int chroma_subsampling_x;
342 unsigned int chroma_subsampling_y;
Yaowu Xuf883b422016-08-30 14:01:10 -0700343} AV1EncoderConfig;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344
Yaowu Xuf883b422016-08-30 14:01:10 -0700345static INLINE int is_lossless_requested(const AV1EncoderConfig *cfg) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700346 return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
347}
348
Yue Chencc6a6ef2018-05-21 16:21:05 -0700349typedef struct FRAME_COUNTS {
350// Note: This structure should only contain 'unsigned int' fields, or
351// aggregates built solely from 'unsigned int' fields/elements
352#if CONFIG_ENTROPY_STATS
353 unsigned int kf_y_mode[KF_MODE_CONTEXTS][KF_MODE_CONTEXTS][INTRA_MODES];
354 unsigned int angle_delta[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
355 unsigned int y_mode[BLOCK_SIZE_GROUPS][INTRA_MODES];
356 unsigned int uv_mode[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
357 unsigned int cfl_sign[CFL_JOINT_SIGNS];
358 unsigned int cfl_alpha[CFL_ALPHA_CONTEXTS][CFL_ALPHABET_SIZE];
359 unsigned int palette_y_mode[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
360 unsigned int palette_uv_mode[PALETTE_UV_MODE_CONTEXTS][2];
361 unsigned int palette_y_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
362 unsigned int palette_uv_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
363 unsigned int palette_y_color_index[PALETTE_SIZES]
364 [PALETTE_COLOR_INDEX_CONTEXTS]
365 [PALETTE_COLORS];
366 unsigned int palette_uv_color_index[PALETTE_SIZES]
367 [PALETTE_COLOR_INDEX_CONTEXTS]
368 [PALETTE_COLORS];
369 unsigned int partition[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
370 unsigned int txb_skip[TOKEN_CDF_Q_CTXS][TX_SIZES][TXB_SKIP_CONTEXTS][2];
371 unsigned int eob_extra[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
372 [EOB_COEF_CONTEXTS][2];
373 unsigned int dc_sign[PLANE_TYPES][DC_SIGN_CONTEXTS][2];
374 unsigned int coeff_lps[TX_SIZES][PLANE_TYPES][BR_CDF_SIZE - 1][LEVEL_CONTEXTS]
375 [2];
376 unsigned int eob_flag[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS][2];
377 unsigned int eob_multi16[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][5];
378 unsigned int eob_multi32[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][6];
379 unsigned int eob_multi64[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][7];
380 unsigned int eob_multi128[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][8];
381 unsigned int eob_multi256[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][9];
382 unsigned int eob_multi512[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][10];
383 unsigned int eob_multi1024[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][11];
384 unsigned int coeff_lps_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
385 [LEVEL_CONTEXTS][BR_CDF_SIZE];
386 unsigned int coeff_base_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
387 [SIG_COEF_CONTEXTS][NUM_BASE_LEVELS + 2];
388 unsigned int coeff_base_eob_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
389 [SIG_COEF_CONTEXTS_EOB][NUM_BASE_LEVELS + 1];
390 unsigned int newmv_mode[NEWMV_MODE_CONTEXTS][2];
391 unsigned int zeromv_mode[GLOBALMV_MODE_CONTEXTS][2];
392 unsigned int refmv_mode[REFMV_MODE_CONTEXTS][2];
393 unsigned int drl_mode[DRL_MODE_CONTEXTS][2];
394 unsigned int inter_compound_mode[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
395 unsigned int wedge_idx[BLOCK_SIZES_ALL][16];
396 unsigned int interintra[BLOCK_SIZE_GROUPS][2];
397 unsigned int interintra_mode[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
398 unsigned int wedge_interintra[BLOCK_SIZES_ALL][2];
399 unsigned int compound_type[BLOCK_SIZES_ALL][COMPOUND_TYPES - 1];
400 unsigned int motion_mode[BLOCK_SIZES_ALL][MOTION_MODES];
401 unsigned int obmc[BLOCK_SIZES_ALL][2];
402 unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
403 unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
404 unsigned int comp_ref_type[COMP_REF_TYPE_CONTEXTS][2];
405 unsigned int uni_comp_ref[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1][2];
406 unsigned int single_ref[REF_CONTEXTS][SINGLE_REFS - 1][2];
407 unsigned int comp_ref[REF_CONTEXTS][FWD_REFS - 1][2];
408 unsigned int comp_bwdref[REF_CONTEXTS][BWD_REFS - 1][2];
409 unsigned int intrabc[2];
410
411 unsigned int txfm_partition[TXFM_PARTITION_CONTEXTS][2];
412 unsigned int intra_tx_size[MAX_TX_CATS][TX_SIZE_CONTEXTS][MAX_TX_DEPTH + 1];
413 unsigned int skip_mode[SKIP_MODE_CONTEXTS][2];
414 unsigned int skip[SKIP_CONTEXTS][2];
415 unsigned int compound_index[COMP_INDEX_CONTEXTS][2];
416 unsigned int comp_group_idx[COMP_GROUP_IDX_CONTEXTS][2];
417 unsigned int delta_q[DELTA_Q_PROBS][2];
418 unsigned int delta_lf_multi[FRAME_LF_COUNT][DELTA_LF_PROBS][2];
419 unsigned int delta_lf[DELTA_LF_PROBS][2];
420
421 unsigned int inter_ext_tx[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
422 unsigned int intra_ext_tx[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
423 [TX_TYPES];
424 unsigned int filter_intra_mode[FILTER_INTRA_MODES];
425 unsigned int filter_intra[BLOCK_SIZES_ALL][2];
426 unsigned int switchable_restore[RESTORE_SWITCHABLE_TYPES];
427 unsigned int wiener_restore[2];
428 unsigned int sgrproj_restore[2];
429#endif // CONFIG_ENTROPY_STATS
430
431 unsigned int switchable_interp[SWITCHABLE_FILTER_CONTEXTS]
432 [SWITCHABLE_FILTERS];
433} FRAME_COUNTS;
434
Angie Chiang71572162018-08-06 16:20:36 -0700435#if CONFIG_COLLECT_INTER_MODE_RD_STATS
436#define INTER_MODE_RD_DATA_OVERALL_SIZE 6400
437
438typedef struct {
439 int ready;
440 double a;
441 double b;
442 double dist_mean;
Angie Chiang199f3d42018-08-14 18:07:02 -0700443 double ld_mean;
444 double sse_mean;
445 double sse_sse_mean;
446 double sse_ld_mean;
Angie Chiang106fa482018-08-07 18:28:40 -0700447 int num;
448 double dist_sum;
449 double ld_sum;
450 double sse_sum;
451 double sse_sse_sum;
452 double sse_ld_sum;
Angie Chiang71572162018-08-06 16:20:36 -0700453} InterModeRdModel;
454
455typedef struct {
456 int idx;
457 int64_t rd;
458} RdIdxPair;
459// TODO(angiebird): This is an estimated size. We still need to figure what is
460// the maximum number of modes.
461#define MAX_INTER_MODES 1024
462typedef struct inter_modes_info {
463 int num;
464 MB_MODE_INFO mbmi_arr[MAX_INTER_MODES];
465 int mode_rate_arr[MAX_INTER_MODES];
466 int64_t sse_arr[MAX_INTER_MODES];
467 int64_t est_rd_arr[MAX_INTER_MODES];
468 RdIdxPair rd_idx_pair_arr[MAX_INTER_MODES];
469} InterModesInfo;
470#endif
471
Ravi Chaudhary40cdf132018-10-08 11:04:16 +0530472// Encoder row synchronization
473typedef struct AV1RowMTSyncData {
474#if CONFIG_MULTITHREAD
475 pthread_mutex_t *mutex_;
476 pthread_cond_t *cond_;
477#endif
478 // Allocate memory to store the sb/mb block index in each row.
479 int *cur_col;
480 int sync_range;
481 int rows;
482} AV1RowMTSync;
483
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530484typedef struct AV1RowMTInfo {
485 int current_mi_row;
486 int num_threads_working;
487} AV1RowMTInfo;
488
Yaowu Xuc27fc142016-08-22 16:08:15 -0700489// TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
490typedef struct TileDataEnc {
491 TileInfo tile_info;
Rupert Swarbrick93c39e92017-07-12 11:11:02 +0100492 int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
493 int mode_map[BLOCK_SIZES_ALL][MAX_MODES];
Yunqing Wang8c1e57c2016-10-25 15:15:23 -0700494 int m_search_count;
495 int ex_search_count;
Luc Trudeauf8164152017-04-11 16:20:51 -0400496 CFL_CTX cfl;
Jingning Han9f07be12017-04-13 09:31:40 -0700497 DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
Ravi Chaudhary84a280a2018-09-24 16:09:48 +0530498 DECLARE_ALIGNED(16, FRAME_CONTEXT, backup_tctx);
Yunqing Wang0e141b52017-11-02 15:08:58 -0700499 uint8_t allow_update_cdf;
Angie Chiang71572162018-08-06 16:20:36 -0700500#if CONFIG_COLLECT_INTER_MODE_RD_STATS
501 InterModeRdModel inter_mode_rd_models[BLOCK_SIZES_ALL];
Angie Chiang71572162018-08-06 16:20:36 -0700502#endif
Ravi Chaudhary40cdf132018-10-08 11:04:16 +0530503 AV1RowMTSync row_mt_sync;
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530504 AV1RowMTInfo row_mt_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700505} TileDataEnc;
506
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530507typedef struct {
508 TOKENEXTRA *start;
509 TOKENEXTRA *stop;
510 unsigned int count;
511} TOKENLIST;
512
Ravi Chaudharyc5e74692018-10-08 16:05:38 +0530513typedef struct MultiThreadHandle {
514 int allocated_tile_rows;
515 int allocated_tile_cols;
516 int allocated_sb_rows;
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530517 int thread_id_to_tile_id[MAX_NUM_THREADS]; // Mapping of threads to tiles
Ravi Chaudharyc5e74692018-10-08 16:05:38 +0530518} MultiThreadHandle;
519
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520typedef struct RD_COUNTS {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521 int64_t comp_pred_diff[REFERENCE_MODES];
Debargha Mukherjeea575d232017-04-28 17:46:47 -0700522 // Stores number of 4x4 blocks using global motion per reference frame.
Zoe Liu27deb382018-03-27 15:13:56 -0700523 int global_motion_used[REF_FRAMES];
Arild Fuldseth (arilfuld)6c20c782017-06-15 09:45:02 +0200524 int compound_ref_used_flag;
Zoe Liu8a5d3432017-11-30 16:33:44 -0800525 int skip_mode_used_flag;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700526} RD_COUNTS;
527
528typedef struct ThreadData {
529 MACROBLOCK mb;
530 RD_COUNTS rd_counts;
531 FRAME_COUNTS *counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700532 PC_TREE *pc_tree;
533 PC_TREE *pc_root[MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2 + 1];
wenyao.liu0c6f5fd2018-10-23 18:41:16 +0800534 tran_low_t *tree_coeff_buf[MAX_MB_PLANE];
535 tran_low_t *tree_qcoeff_buf[MAX_MB_PLANE];
536 tran_low_t *tree_dqcoeff_buf[MAX_MB_PLANE];
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530537#if CONFIG_COLLECT_INTER_MODE_RD_STATS
538 InterModesInfo *inter_modes_info;
539#endif
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530540 uint32_t *hash_value_buffer[2][2];
Jingning Hand064cf02017-06-01 10:00:39 -0700541 int32_t *wsrc_buf;
542 int32_t *mask_buf;
543 uint8_t *above_pred_buf;
544 uint8_t *left_pred_buf;
hui su5d493142017-05-08 12:06:12 -0700545 PALETTE_BUFFER *palette_buffer;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700546 CONV_BUF_TYPE *tmp_conv_dst;
547 uint8_t *tmp_obmc_bufs[2];
Ravi Chaudhary00525ef2018-10-31 19:52:42 +0530548 int intrabc_used;
Ravi Chaudhary84a280a2018-09-24 16:09:48 +0530549 FRAME_CONTEXT *tctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700550} ThreadData;
551
552struct EncWorkerData;
553
554typedef struct ActiveMap {
555 int enabled;
556 int update;
557 unsigned char *map;
558} ActiveMap;
559
Wan-Teh Changc25c92a2018-04-23 15:04:14 -0700560#if CONFIG_INTERNAL_STATS
561// types of stats
562typedef enum {
563 STAT_Y,
564 STAT_U,
565 STAT_V,
566 STAT_ALL,
567 NUM_STAT_TYPES // This should always be the last member of the enum
568} StatType;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700569
570typedef struct IMAGE_STAT {
Urvang Joshib5ed3502016-10-17 16:38:05 -0700571 double stat[NUM_STAT_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700572 double worst;
573} ImageStat;
Wan-Teh Changc25c92a2018-04-23 15:04:14 -0700574#endif // CONFIG_INTERNAL_STATS
Urvang Joshib5ed3502016-10-17 16:38:05 -0700575
Yaowu Xuc27fc142016-08-22 16:08:15 -0700576typedef struct {
577 int ref_count;
578 YV12_BUFFER_CONFIG buf;
579} EncRefCntBuffer;
580
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581typedef struct TileBufferEnc {
582 uint8_t *data;
583 size_t size;
584} TileBufferEnc;
585
Yaowu Xuf883b422016-08-30 14:01:10 -0700586typedef struct AV1_COMP {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700587 QUANTS quants;
588 ThreadData td;
Yue Chencc6a6ef2018-05-21 16:21:05 -0700589 FRAME_COUNTS counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590 MB_MODE_INFO_EXT *mbmi_ext_base;
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700591 CB_COEFF_BUFFER *coeff_buffer_base;
Yi Luoc6210232017-05-25 15:09:25 -0700592 Dequants dequants;
Yaowu Xuf883b422016-08-30 14:01:10 -0700593 AV1_COMMON common;
594 AV1EncoderConfig oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700595 struct lookahead_ctx *lookahead;
596 struct lookahead_entry *alt_ref_source;
Sarah Parkeraf32a7b2018-06-29 14:59:05 -0700597 int no_show_kf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700598
Angie Chiange69a6822018-03-16 13:52:44 -0700599 int optimize_speed_feature;
600 int optimize_seg_arr[MAX_SEGMENTS];
601
Alex Conversef77fd0b2017-04-20 11:00:24 -0700602 YV12_BUFFER_CONFIG *source;
603 YV12_BUFFER_CONFIG *last_source; // NULL for first frame and alt_ref frames
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700604 YV12_BUFFER_CONFIG *unscaled_source;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700605 YV12_BUFFER_CONFIG scaled_source;
606 YV12_BUFFER_CONFIG *unscaled_last_source;
607 YV12_BUFFER_CONFIG scaled_last_source;
608
Yue Chen7cae98f2018-08-24 10:43:16 -0700609 TplDepFrame tpl_stats[MAX_LAG_BUFFERS];
610 YV12_BUFFER_CONFIG *tpl_recon_frames[INTER_REFS_PER_FRAME + 1];
611
Yaowu Xuc27fc142016-08-22 16:08:15 -0700612 // For a still frame, this flag is set to 1 to skip partition search.
613 int partition_search_skippable_frame;
RogerZhou3b635242017-09-19 10:06:46 -0700614 double csm_rate_array[32];
615 double m_rate_array[32];
616 int rate_size;
617 int rate_index;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -0800618 hash_table *previous_hash_table;
619 int previous_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700620
Ravi Chaudhary0ec03a42018-08-17 18:53:28 +0530621 unsigned int row_mt;
David Turnere7ebf902018-12-04 14:04:55 +0000622 RefCntBuffer *scaled_ref_buf[INTER_REFS_PER_FRAME];
Urvang Joshi4d9f15f2018-11-05 15:26:22 -0800623
David Turnere7ebf902018-12-04 14:04:55 +0000624 RefCntBuffer *last_show_frame_buf; // last show frame buffer
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625
Urvang Joshi06b37652018-11-06 11:17:56 -0800626 // refresh_*_frame are boolean flags. If 'refresh_xyz_frame' is true, then
627 // after the current frame is encoded, the XYZ reference frame gets refreshed
628 // (updated) to be the current frame.
629 //
630 // Special case: 'refresh_last_frame' specifies that:
631 // - LAST_FRAME reference should be updated to be the current frame (as usual)
632 // - Also, LAST2_FRAME and LAST3_FRAME references are implicitly updated to be
633 // the two past reference frames just before LAST_FRAME that are available.
634 //
635 // Note: Usually at most one of these refresh flags is true at a time.
636 // But a key-frame is special, for which all the flags are true at once.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 int refresh_last_frame;
638 int refresh_golden_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700639 int refresh_bwd_ref_frame;
Zoe Liue9b15e22017-07-19 15:53:01 -0700640 int refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700641 int refresh_alt_ref_frame;
Urvang Joshi06b37652018-11-06 11:17:56 -0800642
David Turner99e990e2018-12-10 12:54:26 +0000643 // For each type of reference frame, this contains the index of a reference
644 // frame buffer for a reference frame of the same type. We use this to
645 // choose our primary reference frame (which is the most recent reference
646 // frame of the same type as the current frame).
647 int fb_of_context_type[REF_FRAMES];
648
Wei-Ting Linbafa11c2018-07-10 13:20:59 -0700649#if USE_SYMM_MULTI_LAYER
Urvang Joshi06b37652018-11-06 11:17:56 -0800650 // When true, a new rule for backward (future) reference frames is in effect:
651 // - BWDREF_FRAME is always the closest future frame available
652 // - ALTREF2_FRAME is always the 2nd closest future frame available
653 // - 'refresh_bwd_ref_frame' flag is used for updating both the BWDREF_FRAME
654 // and ALTREF2_FRAME. ('refresh_alt2_ref_frame' flag is irrelevant).
Wei-Ting Linbafa11c2018-07-10 13:20:59 -0700655 int new_bwdref_update_rule;
656#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657
658 int ext_refresh_frame_flags_pending;
659 int ext_refresh_last_frame;
660 int ext_refresh_golden_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -0700661 int ext_refresh_bwd_ref_frame;
662 int ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700663 int ext_refresh_alt_ref_frame;
664
665 int ext_refresh_frame_context_pending;
666 int ext_refresh_frame_context;
sarahparker21dbca42018-03-30 17:43:44 -0700667 int ext_use_ref_frame_mvs;
sarahparker27d686a2018-03-30 17:43:44 -0700668 int ext_use_error_resilient;
sarahparker9806fed2018-03-30 17:43:44 -0700669 int ext_use_s_frame;
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700670 int ext_use_primary_ref_none;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700671
672 YV12_BUFFER_CONFIG last_frame_uf;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800673 YV12_BUFFER_CONFIG trial_frame_rst;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700674
675 // Ambient reconstruction err target for force key frames
676 int64_t ambient_err;
677
678 RD_OPT rd;
679
680 CODING_CONTEXT coding_context;
681
Yue Chenb23d00a2017-07-28 17:01:21 -0700682 int gmtype_cost[TRANS_TYPES];
Zoe Liu27deb382018-03-27 15:13:56 -0700683 int gmparams_cost[REF_FRAMES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700684
Jingning Hanf050fc12018-03-09 14:53:33 -0800685 int nmv_costs[2][MV_VALS];
686 int nmv_costs_hp[2][MV_VALS];
James Zern01a9d702017-08-25 19:09:33 +0000687
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688 int64_t last_time_stamp_seen;
689 int64_t last_end_time_stamp_seen;
690 int64_t first_time_stamp_ever;
691
692 RATE_CONTROL rc;
693 double framerate;
694
Urvang Joshia130dcc2018-11-06 10:27:35 -0800695 // Relevant for an inter frame.
696 // - Index '0' corresponds to the values for the currently coded frame.
Urvang Joshi03d8ebe2018-11-08 17:13:44 -0800697 // - Indices LAST_FRAME ... EXTREF_FRAMES are used to store values for all the
698 // possible inter reference frames.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700699 int interp_filter_selected[REF_FRAMES + 1][SWITCHABLE];
700
Yaowu Xuf883b422016-08-30 14:01:10 -0700701 struct aom_codec_pkt_list *output_pkt_list;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702
703 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
704 int mbgraph_n_frames; // number of frames filled in the above
705 int static_mb_pct; // % forced skip mbs by segmentation
706 int ref_frame_flags;
Yunqing Wangf2e7a392017-11-08 00:27:21 -0800707 int ext_ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700708
709 SPEED_FEATURES sf;
710
711 unsigned int max_mv_magnitude;
712 int mv_step_param;
713
Zoe Liu77fb5be2017-11-02 14:36:19 -0700714 int all_one_sided_refs;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700715
Yaowu Xuc27fc142016-08-22 16:08:15 -0700716 uint8_t *segmentation_map;
717
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718 CYCLIC_REFRESH *cyclic_refresh;
719 ActiveMap active_map;
720
721 fractional_mv_step_fp *find_fractional_mv_step;
Yaowu Xuf883b422016-08-30 14:01:10 -0700722 av1_diamond_search_fn_t diamond_search_sad;
Rupert Swarbrick93c39e92017-07-12 11:11:02 +0100723 aom_variance_fn_ptr_t fn_ptr[BLOCK_SIZES_ALL];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700724 uint64_t time_receive_data;
725 uint64_t time_compress_data;
726 uint64_t time_pick_lpf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727
728#if CONFIG_FP_MB_STATS
729 int use_fp_mb_stats;
730#endif
731
732 TWO_PASS twopass;
733
734 YV12_BUFFER_CONFIG alt_ref_buffer;
735
736#if CONFIG_INTERNAL_STATS
737 unsigned int mode_chosen_counts[MAX_MODES];
738
739 int count;
740 uint64_t total_sq_error;
741 uint64_t total_samples;
742 ImageStat psnr;
743
744 double total_blockiness;
745 double worst_blockiness;
746
747 int bytes;
748 double summed_quality;
749 double summed_weights;
750 unsigned int tot_recode_hits;
751 double worst_ssim;
752
753 ImageStat fastssim;
754 ImageStat psnrhvs;
755
756 int b_calculate_blockiness;
757 int b_calculate_consistency;
758
759 double total_inconsistency;
760 double worst_consistency;
761 Ssimv *ssim_vars;
762 Metrics metrics;
763#endif
764 int b_calculate_psnr;
765
766 int droppable;
767
768 int initial_width;
769 int initial_height;
770 int initial_mbs; // Number of MBs in the full-size frame; to be used to
771 // normalize the firstpass stats. This will differ from the
772 // number of MBs in the current frame when the frame is
773 // scaled.
774
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700775 // When resize is triggered through external control, the desired width/height
776 // are stored here until use in the next frame coded. They are effective only
777 // for
778 // one frame and are reset after use.
779 int resize_pending_width;
780 int resize_pending_height;
781
Yaowu Xuc27fc142016-08-22 16:08:15 -0700782 int frame_flags;
783
784 search_site_config ss_cfg;
785
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786 TileDataEnc *tile_data;
787 int allocated_tiles; // Keep track of memory allocated for tiles.
788
789 TOKENEXTRA *tile_tok[MAX_TILE_ROWS][MAX_TILE_COLS];
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530790 TOKENLIST *tplist[MAX_TILE_ROWS][MAX_TILE_COLS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791
792 TileBufferEnc tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
793
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794 int resize_state;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 int resize_avg_qp;
796 int resize_buffer_underflow;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -0700798 // Sequence parameters have been transmitted already and locked
799 // or not. Once locked av1_change_config cannot change the seq
800 // parameters.
801 int seq_params_locked;
802
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803 // VARIANCE_AQ segment map refresh
804 int vaq_refresh;
805
806 // Multi-threading
807 int num_workers;
Yaowu Xuf883b422016-08-30 14:01:10 -0700808 AVxWorker *workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700809 struct EncWorkerData *tile_thr_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810 int existing_fb_idx_to_show;
811 int is_arf_filter_off[MAX_EXT_ARFS + 1];
812 int num_extra_arfs;
Zoe Liuf271a952017-09-05 15:03:48 -0700813 int arf_pos_in_gf[MAX_EXT_ARFS + 1];
814 int arf_pos_for_ovrly[MAX_EXT_ARFS + 1];
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -0800815 int global_motion_search_done;
Di Chen53a04f62017-06-23 13:47:56 -0700816 int extra_arf_allowed;
Hui Su85878782017-11-07 14:56:31 -0800817 // A flag to indicate if intrabc is ever used in current frame.
818 int intrabc_used;
Hui Sudfcbfbd2017-11-13 12:05:30 -0800819 int dv_cost[2][MV_VALS];
820 // TODO(huisu@google.com): we can update dv_joint_cost per SB.
821 int dv_joint_cost[MV_JOINTS];
Hui Suad7551e2018-03-14 11:13:31 -0700822 int has_lossless_segment;
Zoe Liu1d90ceb2018-04-16 16:53:37 -0700823
Remya6924f4a2018-11-26 12:27:32 +0530824 // Factors to control gating of compound type selection based on best
825 // approximate rd so far
826 int max_comp_type_rd_threshold_mul;
827 int max_comp_type_rd_threshold_div;
828
Deepa K G964e72e2018-05-16 16:56:01 +0530829 AV1LfSync lf_row_sync;
Ravi Chaudharye2aa4012018-06-04 14:20:00 +0530830 AV1LrSync lr_row_sync;
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +0530831 AV1LrStruct lr_ctxt;
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700832
833 aom_film_grain_table_t *film_grain_table;
834#if CONFIG_DENOISE
835 struct aom_denoise_and_model_t *denoise_and_model;
836#endif
Ranjit Kumar Tulabandu8105bf42018-07-27 14:16:55 +0530837 // Stores the default value of skip flag depending on chroma format
838 // Set as 1 for monochrome and 3 for other color formats
839 int default_interp_skip_flags;
Wei-Ting Lina8c02452018-08-13 11:04:06 -0700840 int preserve_arf_as_gld;
Ravi Chaudharyc5e74692018-10-08 16:05:38 +0530841 MultiThreadHandle multi_thread_ctxt;
Ravi Chaudhary40cdf132018-10-08 11:04:16 +0530842 void (*row_mt_sync_read_ptr)(AV1RowMTSync *const, int, int);
843 void (*row_mt_sync_write_ptr)(AV1RowMTSync *const, int, int, const int);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530844#if CONFIG_MULTITHREAD
845 pthread_mutex_t *row_mt_mutex_;
846#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700847} AV1_COMP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700848
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700849// Must not be called more than once.
Yaowu Xuf883b422016-08-30 14:01:10 -0700850void av1_initialize_enc(void);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851
Yaowu Xuf883b422016-08-30 14:01:10 -0700852struct AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
853 BufferPool *const pool);
854void av1_remove_compressor(AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855
Yaowu Xuf883b422016-08-30 14:01:10 -0700856void av1_change_config(AV1_COMP *cpi, const AV1EncoderConfig *oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857
858// receive a frames worth of data. caller can assume that a copy of this
859// frame is made and not just a copy of the pointer..
James Zern3e2613b2017-03-30 23:14:40 -0700860int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -0700861 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
862 int64_t end_time_stamp);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863
Andrey Norkin795ba872018-03-06 13:24:14 -0800864int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
865 size_t *size, uint8_t *dest, int64_t *time_stamp,
866 int64_t *time_end, int flush,
867 const aom_rational_t *timebase);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868
Yaowu Xuf883b422016-08-30 14:01:10 -0700869int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870
Yaowu Xuf883b422016-08-30 14:01:10 -0700871int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872
Yunqing Wang93b18f32018-06-08 21:08:29 -0700873aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
874 YV12_BUFFER_CONFIG *new_frame,
875 YV12_BUFFER_CONFIG *sd);
Yunqing Wangff9bfca2018-06-06 11:46:08 -0700876
Yaowu Xuf883b422016-08-30 14:01:10 -0700877int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878
Yaowu Xuf883b422016-08-30 14:01:10 -0700879void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880
Thomas Daede497d1952017-08-08 17:33:06 -0700881int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882
Thomas Daede497d1952017-08-08 17:33:06 -0700883int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700884
Yaowu Xuf883b422016-08-30 14:01:10 -0700885int av1_update_entropy(AV1_COMP *cpi, int update);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700886
Yaowu Xuf883b422016-08-30 14:01:10 -0700887int av1_set_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700888
Yaowu Xuf883b422016-08-30 14:01:10 -0700889int av1_get_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700890
Yaowu Xuf883b422016-08-30 14:01:10 -0700891int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
892 AOM_SCALING vert_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893
Yaowu Xuf883b422016-08-30 14:01:10 -0700894int av1_get_quantizer(struct AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400896int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *input_size);
897
Andrey Norkin795ba872018-03-06 13:24:14 -0800898int64_t timebase_units_to_ticks(const aom_rational_t *timebase, int64_t n);
899int64_t ticks_to_timebase_units(const aom_rational_t *timebase, int64_t n);
Andrey Norkin795ba872018-03-06 13:24:14 -0800900
Yaowu Xuf883b422016-08-30 14:01:10 -0700901static INLINE int frame_is_kf_gf_arf(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902 return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
903 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
904}
905
Hui Su2d5fd742018-02-21 18:10:37 -0800906// TODO(huisu@google.com, youzhou@microsoft.com): enable hash-me for HBD.
907static INLINE int av1_use_hash_me(const AV1_COMMON *const cm) {
Debargha Mukherjee8112d2f2018-03-01 09:53:11 -0800908 return cm->allow_screen_content_tools;
Hui Su2d5fd742018-02-21 18:10:37 -0800909}
910
911static INLINE hash_table *av1_get_ref_frame_hash_map(
David Turnera21966b2018-12-05 14:48:49 +0000912 const AV1_COMMON *cm, MV_REFERENCE_FRAME ref_frame) {
913 const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
914 RefCntBuffer *buf =
915 (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
916 return buf ? &buf->hash_table : NULL;
RogerZhoucc5d35d2017-08-07 22:20:15 -0700917}
RogerZhoucc5d35d2017-08-07 22:20:15 -0700918
David Turnera21966b2018-12-05 14:48:49 +0000919static INLINE const YV12_BUFFER_CONFIG *get_ref_frame_yv12_buf(
920 const AV1_COMMON *const cm, MV_REFERENCE_FRAME ref_frame) {
921 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +0000922 return buf != NULL ? &buf->buf : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700923}
924
David Turnera21966b2018-12-05 14:48:49 +0000925static INLINE int enc_is_ref_frame_buf(const AV1_COMMON *const cm,
926 const RefCntBuffer *const frame_buf) {
Urvang Joshi40582172018-11-07 11:53:03 -0800927 MV_REFERENCE_FRAME ref_frame;
928 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +0000929 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +0000930 if (buf == NULL) continue;
931 if (frame_buf == buf) break;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700932 }
Urvang Joshi40582172018-11-07 11:53:03 -0800933 return (ref_frame <= ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935
Hui Su8ac0c982018-04-03 12:17:36 -0700936// Token buffer is only used for palette tokens.
Yaowu Xue39b3b82017-10-31 16:11:59 -0700937static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000938 int sb_size_log2,
939 const int num_planes) {
Rupert Swarbrickce63e832017-10-25 17:54:17 +0100940 // Calculate the maximum number of max superblocks in the image.
Yaowu Xue39b3b82017-10-31 16:11:59 -0700941 const int shift = sb_size_log2 - 4;
942 const int sb_size = 1 << sb_size_log2;
943 const int sb_size_square = sb_size * sb_size;
Rupert Swarbrickce63e832017-10-25 17:54:17 +0100944 const int sb_rows = ALIGN_POWER_OF_TWO(mb_rows, shift) >> shift;
945 const int sb_cols = ALIGN_POWER_OF_TWO(mb_cols, shift) >> shift;
946
Hui Su8ac0c982018-04-03 12:17:36 -0700947 // One palette token for each pixel. There can be palettes on two planes.
948 const int sb_palette_toks = AOMMIN(2, num_planes) * sb_size_square;
Rupert Swarbrickce63e832017-10-25 17:54:17 +0100949
Hui Su8ac0c982018-04-03 12:17:36 -0700950 return sb_rows * sb_cols * sb_palette_toks;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951}
952
953// Get the allocated token size for a tile. It does the same calculation as in
954// the frame token allocation.
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +0000955static INLINE unsigned int allocated_tokens(TileInfo tile, int sb_size_log2,
956 int num_planes) {
Jingning Haneafbd5f2017-03-07 11:18:17 -0800957 int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 2) >> 2;
958 int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 2) >> 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700959
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +0000960 return get_token_alloc(tile_mb_rows, tile_mb_cols, sb_size_log2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700961}
962
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530963static INLINE void get_start_tok(AV1_COMP *cpi, int tile_row, int tile_col,
964 int mi_row, TOKENEXTRA **tok, int sb_size_log2,
965 int num_planes) {
966 AV1_COMMON *const cm = &cpi->common;
967 const int tile_cols = cm->tile_cols;
968 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
969 const TileInfo *const tile_info = &this_tile->tile_info;
970
971 const int tile_mb_cols =
972 (tile_info->mi_col_end - tile_info->mi_col_start + 2) >> 2;
973 const int tile_mb_row = (mi_row - tile_info->mi_row_start + 2) >> 2;
974
975 *tok = cpi->tile_tok[tile_row][tile_col] +
976 get_token_alloc(tile_mb_row, tile_mb_cols, sb_size_log2, num_planes);
977}
978
Yaowu Xuf883b422016-08-30 14:01:10 -0700979void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700980
Yaowu Xu6da42302017-10-10 14:45:06 -0700981#define ALT_MIN_LAG 3
Yaowu Xuf883b422016-08-30 14:01:10 -0700982static INLINE int is_altref_enabled(const AV1_COMP *const cpi) {
Yaowu Xu6da42302017-10-10 14:45:06 -0700983 return cpi->oxcf.lag_in_frames >= ALT_MIN_LAG && cpi->oxcf.enable_auto_arf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700984}
985
986// TODO(zoeliu): To set up cpi->oxcf.enable_auto_brf
Yaowu Xuc27fc142016-08-22 16:08:15 -0700987
Urvang Joshi52648442016-10-13 17:27:51 -0700988static INLINE void set_ref_ptrs(const AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700989 MV_REFERENCE_FRAME ref0,
990 MV_REFERENCE_FRAME ref1) {
David Turnera21966b2018-12-05 14:48:49 +0000991 xd->block_ref_scale_factors[0] =
992 get_ref_scale_factors_const(cm, ref0 >= LAST_FRAME ? ref0 : 1);
993 xd->block_ref_scale_factors[1] =
994 get_ref_scale_factors_const(cm, ref1 >= LAST_FRAME ? ref1 : 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995}
996
Yaowu Xu4ff59b52017-04-24 12:41:56 -0700997static INLINE int get_chessboard_index(int frame_index) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700998 return frame_index & 0x1;
999}
1000
Yaowu Xuf883b422016-08-30 14:01:10 -07001001static INLINE int *cond_cost_list(const struct AV1_COMP *cpi, int *cost_list) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001002 return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
1003}
1004
Yaowu Xuf883b422016-08-30 14:01:10 -07001005void av1_new_framerate(AV1_COMP *cpi, double framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001006
1007#define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
1008
Cheng Chen09c83a52018-06-05 12:27:36 -07001009// Returns 1 if a frame is scaled and 0 otherwise.
1010static INLINE int av1_resize_scaled(const AV1_COMMON *cm) {
1011 return !(cm->superres_upscaled_width == cm->render_width &&
1012 cm->superres_upscaled_height == cm->render_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07001013}
1014
Cheng Chen09c83a52018-06-05 12:27:36 -07001015static INLINE int av1_frame_scaled(const AV1_COMMON *cm) {
1016 return !av1_superres_scaled(cm) && av1_resize_scaled(cm);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07001017}
1018
Sarah Parker33005522018-07-27 14:46:25 -07001019// Don't allow a show_existing_frame to coincide with an error resilient
1020// frame. An exception can be made for a forward keyframe since it has no
1021// previous dependencies.
1022static INLINE int encode_show_existing_frame(const AV1_COMMON *cm) {
David Turnerd2a592e2018-11-16 14:59:31 +00001023 return cm->show_existing_frame && (!cm->error_resilient_mode ||
1024 cm->current_frame.frame_type == KEY_FRAME);
Sarah Parker33005522018-07-27 14:46:25 -07001025}
1026
Tom Fineganf8d6a162018-08-21 10:47:55 -07001027// Returns a Sequence Header OBU stored in an aom_fixed_buf_t, or NULL upon
1028// failure. When a non-NULL aom_fixed_buf_t pointer is returned by this
1029// function, the memory must be freed by the caller. Both the buf member of the
Tom Fineganca1e28f2018-08-28 16:55:35 -07001030// aom_fixed_buf_t, and the aom_fixed_buf_t pointer itself must be freed. Memory
1031// returned must be freed via call to free().
1032//
1033// Note: The OBU returned is in Low Overhead Bitstream Format. Specifically,
1034// the obu_has_size_field bit is set, and the buffer contains the obu_size
1035// field.
Tom Fineganf8d6a162018-08-21 10:47:55 -07001036aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi);
1037
Yaowu Xuc27fc142016-08-22 16:08:15 -07001038#ifdef __cplusplus
1039} // extern "C"
1040#endif
1041
James Zerne1cbb132018-08-22 14:10:36 -07001042#endif // AOM_AV1_ENCODER_ENCODER_H_