blob: ea7d03a479d2dcba2501fab8e1d93e0a1068aa29 [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;
291 struct aom_codec_pkt_list *output_pkt_list;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292
293#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -0700294 aom_fixed_buf_t firstpass_mb_stats_in;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295#endif
296
Yaowu Xuf883b422016-08-30 14:01:10 -0700297 aom_tune_metric tuning;
298 aom_tune_content content;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700299 int use_highbitdepth;
Andrey Norkin9e694632017-12-21 18:50:57 -0800300 aom_color_primaries_t color_primaries;
301 aom_transfer_characteristics_t transfer_characteristics;
302 aom_matrix_coefficients_t matrix_coefficients;
anorkin76fb1262017-03-22 15:12:12 -0700303 aom_chroma_sample_position_t chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304 int color_range;
305 int render_width;
306 int render_height;
Andrey Norkin795ba872018-03-06 13:24:14 -0800307 aom_timing_info_type_t timing_info_type;
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800308 int timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -0800309 aom_timing_info_t timing_info;
310 int decoder_model_info_present_flag;
Andrey Norkin26495512018-06-20 17:13:11 -0700311 int display_model_info_present_flag;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -0700312 int buffer_removal_time_present;
Andrey Norkin795ba872018-03-06 13:24:14 -0800313 aom_dec_model_info_t buffer_model;
314 aom_dec_model_op_parameters_t op_params[MAX_NUM_OPERATING_POINTS + 1];
315 aom_op_timing_info_t op_frame_timing[MAX_NUM_OPERATING_POINTS + 1];
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800316 int film_grain_test_vector;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700317 const char *film_grain_table_filename;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318
Hui Su1cb1c002018-02-05 18:21:20 -0800319 uint8_t cdf_update_mode;
Yaowu Xuf883b422016-08-30 14:01:10 -0700320 aom_superblock_size_t superblock_size;
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700321 unsigned int large_scale_tile;
322 unsigned int single_tile_decoding;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800323 int monochrome;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -0700324 unsigned int full_still_picture_hdr;
Jingning Hana446f552018-02-22 15:42:12 -0800325 int enable_dual_filter;
Yunqing Wangff4fa062017-04-21 10:56:08 -0700326 unsigned int motion_vector_unit_test;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100327 const cfg_options_t *cfg;
Debargha Mukherjee0d8368a2018-03-25 12:23:02 -0700328 int enable_order_hint;
Cheng Chene0c918a2018-02-22 19:38:31 -0800329 int enable_jnt_comp;
Debargha Mukherjee0187d7c2018-03-25 11:37:56 -0700330 int enable_ref_frame_mvs;
Debargha Mukherjee0d8368a2018-03-25 12:23:02 -0700331 unsigned int allow_ref_frame_mvs;
Debargha Mukherjee37df9162018-03-25 12:48:24 -0700332 int enable_warped_motion;
333 int allow_warped_motion;
Urvang Joshi2c92b072018-03-19 17:23:31 -0700334 int enable_superres;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400335 unsigned int save_as_annexb;
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700336
337#if CONFIG_DENOISE
338 float noise_level;
339 int noise_block_size;
340#endif
Tom Finegan02b2a842018-08-24 13:50:00 -0700341
342 unsigned int chroma_subsampling_x;
343 unsigned int chroma_subsampling_y;
Yaowu Xuf883b422016-08-30 14:01:10 -0700344} AV1EncoderConfig;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345
Yaowu Xuf883b422016-08-30 14:01:10 -0700346static INLINE int is_lossless_requested(const AV1EncoderConfig *cfg) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347 return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
348}
349
Yue Chencc6a6ef2018-05-21 16:21:05 -0700350typedef struct FRAME_COUNTS {
351// Note: This structure should only contain 'unsigned int' fields, or
352// aggregates built solely from 'unsigned int' fields/elements
353#if CONFIG_ENTROPY_STATS
354 unsigned int kf_y_mode[KF_MODE_CONTEXTS][KF_MODE_CONTEXTS][INTRA_MODES];
355 unsigned int angle_delta[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
356 unsigned int y_mode[BLOCK_SIZE_GROUPS][INTRA_MODES];
357 unsigned int uv_mode[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
358 unsigned int cfl_sign[CFL_JOINT_SIGNS];
359 unsigned int cfl_alpha[CFL_ALPHA_CONTEXTS][CFL_ALPHABET_SIZE];
360 unsigned int palette_y_mode[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
361 unsigned int palette_uv_mode[PALETTE_UV_MODE_CONTEXTS][2];
362 unsigned int palette_y_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
363 unsigned int palette_uv_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
364 unsigned int palette_y_color_index[PALETTE_SIZES]
365 [PALETTE_COLOR_INDEX_CONTEXTS]
366 [PALETTE_COLORS];
367 unsigned int palette_uv_color_index[PALETTE_SIZES]
368 [PALETTE_COLOR_INDEX_CONTEXTS]
369 [PALETTE_COLORS];
370 unsigned int partition[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
371 unsigned int txb_skip[TOKEN_CDF_Q_CTXS][TX_SIZES][TXB_SKIP_CONTEXTS][2];
372 unsigned int eob_extra[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
373 [EOB_COEF_CONTEXTS][2];
374 unsigned int dc_sign[PLANE_TYPES][DC_SIGN_CONTEXTS][2];
375 unsigned int coeff_lps[TX_SIZES][PLANE_TYPES][BR_CDF_SIZE - 1][LEVEL_CONTEXTS]
376 [2];
377 unsigned int eob_flag[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS][2];
378 unsigned int eob_multi16[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][5];
379 unsigned int eob_multi32[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][6];
380 unsigned int eob_multi64[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][7];
381 unsigned int eob_multi128[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][8];
382 unsigned int eob_multi256[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][9];
383 unsigned int eob_multi512[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][10];
384 unsigned int eob_multi1024[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][11];
385 unsigned int coeff_lps_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
386 [LEVEL_CONTEXTS][BR_CDF_SIZE];
387 unsigned int coeff_base_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
388 [SIG_COEF_CONTEXTS][NUM_BASE_LEVELS + 2];
389 unsigned int coeff_base_eob_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
390 [SIG_COEF_CONTEXTS_EOB][NUM_BASE_LEVELS + 1];
391 unsigned int newmv_mode[NEWMV_MODE_CONTEXTS][2];
392 unsigned int zeromv_mode[GLOBALMV_MODE_CONTEXTS][2];
393 unsigned int refmv_mode[REFMV_MODE_CONTEXTS][2];
394 unsigned int drl_mode[DRL_MODE_CONTEXTS][2];
395 unsigned int inter_compound_mode[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
396 unsigned int wedge_idx[BLOCK_SIZES_ALL][16];
397 unsigned int interintra[BLOCK_SIZE_GROUPS][2];
398 unsigned int interintra_mode[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
399 unsigned int wedge_interintra[BLOCK_SIZES_ALL][2];
400 unsigned int compound_type[BLOCK_SIZES_ALL][COMPOUND_TYPES - 1];
401 unsigned int motion_mode[BLOCK_SIZES_ALL][MOTION_MODES];
402 unsigned int obmc[BLOCK_SIZES_ALL][2];
403 unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
404 unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
405 unsigned int comp_ref_type[COMP_REF_TYPE_CONTEXTS][2];
406 unsigned int uni_comp_ref[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1][2];
407 unsigned int single_ref[REF_CONTEXTS][SINGLE_REFS - 1][2];
408 unsigned int comp_ref[REF_CONTEXTS][FWD_REFS - 1][2];
409 unsigned int comp_bwdref[REF_CONTEXTS][BWD_REFS - 1][2];
410 unsigned int intrabc[2];
411
412 unsigned int txfm_partition[TXFM_PARTITION_CONTEXTS][2];
413 unsigned int intra_tx_size[MAX_TX_CATS][TX_SIZE_CONTEXTS][MAX_TX_DEPTH + 1];
414 unsigned int skip_mode[SKIP_MODE_CONTEXTS][2];
415 unsigned int skip[SKIP_CONTEXTS][2];
416 unsigned int compound_index[COMP_INDEX_CONTEXTS][2];
417 unsigned int comp_group_idx[COMP_GROUP_IDX_CONTEXTS][2];
418 unsigned int delta_q[DELTA_Q_PROBS][2];
419 unsigned int delta_lf_multi[FRAME_LF_COUNT][DELTA_LF_PROBS][2];
420 unsigned int delta_lf[DELTA_LF_PROBS][2];
421
422 unsigned int inter_ext_tx[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
423 unsigned int intra_ext_tx[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
424 [TX_TYPES];
425 unsigned int filter_intra_mode[FILTER_INTRA_MODES];
426 unsigned int filter_intra[BLOCK_SIZES_ALL][2];
427 unsigned int switchable_restore[RESTORE_SWITCHABLE_TYPES];
428 unsigned int wiener_restore[2];
429 unsigned int sgrproj_restore[2];
430#endif // CONFIG_ENTROPY_STATS
431
432 unsigned int switchable_interp[SWITCHABLE_FILTER_CONTEXTS]
433 [SWITCHABLE_FILTERS];
434} FRAME_COUNTS;
435
Angie Chiang71572162018-08-06 16:20:36 -0700436#if CONFIG_COLLECT_INTER_MODE_RD_STATS
437#define INTER_MODE_RD_DATA_OVERALL_SIZE 6400
438
439typedef struct {
440 int ready;
441 double a;
442 double b;
443 double dist_mean;
Angie Chiang199f3d42018-08-14 18:07:02 -0700444 double ld_mean;
445 double sse_mean;
446 double sse_sse_mean;
447 double sse_ld_mean;
Angie Chiang106fa482018-08-07 18:28:40 -0700448 int num;
449 double dist_sum;
450 double ld_sum;
451 double sse_sum;
452 double sse_sse_sum;
453 double sse_ld_sum;
Angie Chiang71572162018-08-06 16:20:36 -0700454} InterModeRdModel;
455
456typedef struct {
457 int idx;
458 int64_t rd;
459} RdIdxPair;
460// TODO(angiebird): This is an estimated size. We still need to figure what is
461// the maximum number of modes.
462#define MAX_INTER_MODES 1024
463typedef struct inter_modes_info {
464 int num;
465 MB_MODE_INFO mbmi_arr[MAX_INTER_MODES];
466 int mode_rate_arr[MAX_INTER_MODES];
467 int64_t sse_arr[MAX_INTER_MODES];
468 int64_t est_rd_arr[MAX_INTER_MODES];
469 RdIdxPair rd_idx_pair_arr[MAX_INTER_MODES];
470} InterModesInfo;
471#endif
472
Ravi Chaudhary40cdf132018-10-08 11:04:16 +0530473// Encoder row synchronization
474typedef struct AV1RowMTSyncData {
475#if CONFIG_MULTITHREAD
476 pthread_mutex_t *mutex_;
477 pthread_cond_t *cond_;
478#endif
479 // Allocate memory to store the sb/mb block index in each row.
480 int *cur_col;
481 int sync_range;
482 int rows;
483} AV1RowMTSync;
484
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530485typedef struct AV1RowMTInfo {
486 int current_mi_row;
487 int num_threads_working;
488} AV1RowMTInfo;
489
Yaowu Xuc27fc142016-08-22 16:08:15 -0700490// TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
491typedef struct TileDataEnc {
492 TileInfo tile_info;
Rupert Swarbrick93c39e92017-07-12 11:11:02 +0100493 int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
494 int mode_map[BLOCK_SIZES_ALL][MAX_MODES];
Yunqing Wang8c1e57c2016-10-25 15:15:23 -0700495 int m_search_count;
496 int ex_search_count;
Luc Trudeauf8164152017-04-11 16:20:51 -0400497 CFL_CTX cfl;
Jingning Han9f07be12017-04-13 09:31:40 -0700498 DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
Ravi Chaudhary84a280a2018-09-24 16:09:48 +0530499 DECLARE_ALIGNED(16, FRAME_CONTEXT, backup_tctx);
Yunqing Wang0e141b52017-11-02 15:08:58 -0700500 uint8_t allow_update_cdf;
Angie Chiang71572162018-08-06 16:20:36 -0700501#if CONFIG_COLLECT_INTER_MODE_RD_STATS
502 InterModeRdModel inter_mode_rd_models[BLOCK_SIZES_ALL];
Angie Chiang71572162018-08-06 16:20:36 -0700503#endif
Ravi Chaudhary40cdf132018-10-08 11:04:16 +0530504 AV1RowMTSync row_mt_sync;
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530505 AV1RowMTInfo row_mt_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506} TileDataEnc;
507
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530508typedef struct {
509 TOKENEXTRA *start;
510 TOKENEXTRA *stop;
511 unsigned int count;
512} TOKENLIST;
513
Ravi Chaudharyc5e74692018-10-08 16:05:38 +0530514typedef struct MultiThreadHandle {
515 int allocated_tile_rows;
516 int allocated_tile_cols;
517 int allocated_sb_rows;
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530518 int thread_id_to_tile_id[MAX_NUM_THREADS]; // Mapping of threads to tiles
Ravi Chaudharyc5e74692018-10-08 16:05:38 +0530519} MultiThreadHandle;
520
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521typedef struct RD_COUNTS {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522 int64_t comp_pred_diff[REFERENCE_MODES];
Debargha Mukherjeea575d232017-04-28 17:46:47 -0700523 // Stores number of 4x4 blocks using global motion per reference frame.
Zoe Liu27deb382018-03-27 15:13:56 -0700524 int global_motion_used[REF_FRAMES];
Arild Fuldseth (arilfuld)6c20c782017-06-15 09:45:02 +0200525 int compound_ref_used_flag;
Zoe Liu8a5d3432017-11-30 16:33:44 -0800526 int skip_mode_used_flag;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700527} RD_COUNTS;
528
529typedef struct ThreadData {
530 MACROBLOCK mb;
531 RD_COUNTS rd_counts;
532 FRAME_COUNTS *counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 PC_TREE *pc_tree;
534 PC_TREE *pc_root[MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2 + 1];
wenyao.liu0c6f5fd2018-10-23 18:41:16 +0800535 tran_low_t *tree_coeff_buf[MAX_MB_PLANE];
536 tran_low_t *tree_qcoeff_buf[MAX_MB_PLANE];
537 tran_low_t *tree_dqcoeff_buf[MAX_MB_PLANE];
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530538#if CONFIG_COLLECT_INTER_MODE_RD_STATS
539 InterModesInfo *inter_modes_info;
540#endif
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530541 uint32_t *hash_value_buffer[2][2];
Jingning Hand064cf02017-06-01 10:00:39 -0700542 int32_t *wsrc_buf;
543 int32_t *mask_buf;
544 uint8_t *above_pred_buf;
545 uint8_t *left_pred_buf;
hui su5d493142017-05-08 12:06:12 -0700546 PALETTE_BUFFER *palette_buffer;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700547 CONV_BUF_TYPE *tmp_conv_dst;
548 uint8_t *tmp_obmc_bufs[2];
Ravi Chaudhary00525ef2018-10-31 19:52:42 +0530549 int intrabc_used;
Ravi Chaudhary84a280a2018-09-24 16:09:48 +0530550 FRAME_CONTEXT *tctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551} ThreadData;
552
553struct EncWorkerData;
554
555typedef struct ActiveMap {
556 int enabled;
557 int update;
558 unsigned char *map;
559} ActiveMap;
560
Wan-Teh Changc25c92a2018-04-23 15:04:14 -0700561#if CONFIG_INTERNAL_STATS
562// types of stats
563typedef enum {
564 STAT_Y,
565 STAT_U,
566 STAT_V,
567 STAT_ALL,
568 NUM_STAT_TYPES // This should always be the last member of the enum
569} StatType;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700570
571typedef struct IMAGE_STAT {
Urvang Joshib5ed3502016-10-17 16:38:05 -0700572 double stat[NUM_STAT_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700573 double worst;
574} ImageStat;
Wan-Teh Changc25c92a2018-04-23 15:04:14 -0700575#endif // CONFIG_INTERNAL_STATS
Urvang Joshib5ed3502016-10-17 16:38:05 -0700576
Yaowu Xuc27fc142016-08-22 16:08:15 -0700577typedef struct {
578 int ref_count;
579 YV12_BUFFER_CONFIG buf;
580} EncRefCntBuffer;
581
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582typedef struct TileBufferEnc {
583 uint8_t *data;
584 size_t size;
585} TileBufferEnc;
586
Yaowu Xuf883b422016-08-30 14:01:10 -0700587typedef struct AV1_COMP {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700588 QUANTS quants;
589 ThreadData td;
Yue Chencc6a6ef2018-05-21 16:21:05 -0700590 FRAME_COUNTS counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700591 MB_MODE_INFO_EXT *mbmi_ext_base;
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700592 CB_COEFF_BUFFER *coeff_buffer_base;
Yi Luoc6210232017-05-25 15:09:25 -0700593 Dequants dequants;
Yaowu Xuf883b422016-08-30 14:01:10 -0700594 AV1_COMMON common;
595 AV1EncoderConfig oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596 struct lookahead_ctx *lookahead;
597 struct lookahead_entry *alt_ref_source;
Sarah Parkeraf32a7b2018-06-29 14:59:05 -0700598 int no_show_kf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700599
Angie Chiange69a6822018-03-16 13:52:44 -0700600 int optimize_speed_feature;
601 int optimize_seg_arr[MAX_SEGMENTS];
602
Alex Conversef77fd0b2017-04-20 11:00:24 -0700603 YV12_BUFFER_CONFIG *source;
604 YV12_BUFFER_CONFIG *last_source; // NULL for first frame and alt_ref frames
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700605 YV12_BUFFER_CONFIG *unscaled_source;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 YV12_BUFFER_CONFIG scaled_source;
607 YV12_BUFFER_CONFIG *unscaled_last_source;
608 YV12_BUFFER_CONFIG scaled_last_source;
609
Yue Chen7cae98f2018-08-24 10:43:16 -0700610 TplDepFrame tpl_stats[MAX_LAG_BUFFERS];
611 YV12_BUFFER_CONFIG *tpl_recon_frames[INTER_REFS_PER_FRAME + 1];
612
Yaowu Xuc27fc142016-08-22 16:08:15 -0700613 // For a still frame, this flag is set to 1 to skip partition search.
614 int partition_search_skippable_frame;
RogerZhou3b635242017-09-19 10:06:46 -0700615 double csm_rate_array[32];
616 double m_rate_array[32];
617 int rate_size;
618 int rate_index;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -0800619 hash_table *previous_hash_table;
620 int previous_index;
RogerZhou3b635242017-09-19 10:06:46 -0700621 int cur_poc; // DebugInfo
Yaowu Xuc27fc142016-08-22 16:08:15 -0700622
Ravi Chaudhary0ec03a42018-08-17 18:53:28 +0530623 unsigned int row_mt;
Wan-Teh Changbb6a3d22018-10-05 11:22:48 -0700624 int scaled_ref_idx[INTER_REFS_PER_FRAME];
Urvang Joshi4d9f15f2018-11-05 15:26:22 -0800625
626 // For encoder, we have a two-level mapping from reference frame type to the
627 // corresponding buffer in the buffer pool:
628 // * 'remapped_ref_idx[i - 1]' maps reference type ‘i’ (range: LAST_FRAME ...
629 // EXTREF_FRAME) to a remapped index ‘j’ (in range: 0 ... REF_FRAMES - 1)
630 // * Later, 'cm->ref_frame_map[j]' maps the remapped index ‘j’ to actual index
631 // of the buffer in the buffer pool ‘cm->buffer_pool.frame_bufs’.
632 //
633 // LAST_FRAME, ..., EXTREF_FRAME
634 // | |
635 // v v
636 // remapped_ref_idx[LAST_FRAME - 1], ..., remapped_ref_idx[EXTREF_FRAME - 1]
637 // | |
638 // v v
639 // ref_frame_map[], ..., ref_frame_map[]
640 //
641 // Note: INTRA_FRAME always refers to the current frame, so there's no need to
642 // have a remapped index for the same.
643 int remapped_ref_idx[REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700644
645 int last_show_frame_buf_idx; // last show frame buffer index
646
Urvang Joshi06b37652018-11-06 11:17:56 -0800647 // refresh_*_frame are boolean flags. If 'refresh_xyz_frame' is true, then
648 // after the current frame is encoded, the XYZ reference frame gets refreshed
649 // (updated) to be the current frame.
650 //
651 // Special case: 'refresh_last_frame' specifies that:
652 // - LAST_FRAME reference should be updated to be the current frame (as usual)
653 // - Also, LAST2_FRAME and LAST3_FRAME references are implicitly updated to be
654 // the two past reference frames just before LAST_FRAME that are available.
655 //
656 // Note: Usually at most one of these refresh flags is true at a time.
657 // But a key-frame is special, for which all the flags are true at once.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658 int refresh_last_frame;
659 int refresh_golden_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660 int refresh_bwd_ref_frame;
Zoe Liue9b15e22017-07-19 15:53:01 -0700661 int refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700662 int refresh_alt_ref_frame;
Urvang Joshi06b37652018-11-06 11:17:56 -0800663
Wei-Ting Linbafa11c2018-07-10 13:20:59 -0700664#if USE_SYMM_MULTI_LAYER
Urvang Joshi06b37652018-11-06 11:17:56 -0800665 // When true, a new rule for backward (future) reference frames is in effect:
666 // - BWDREF_FRAME is always the closest future frame available
667 // - ALTREF2_FRAME is always the 2nd closest future frame available
668 // - 'refresh_bwd_ref_frame' flag is used for updating both the BWDREF_FRAME
669 // and ALTREF2_FRAME. ('refresh_alt2_ref_frame' flag is irrelevant).
Wei-Ting Linbafa11c2018-07-10 13:20:59 -0700670 int new_bwdref_update_rule;
671#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700672
673 int ext_refresh_frame_flags_pending;
674 int ext_refresh_last_frame;
675 int ext_refresh_golden_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -0700676 int ext_refresh_bwd_ref_frame;
677 int ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700678 int ext_refresh_alt_ref_frame;
679
680 int ext_refresh_frame_context_pending;
681 int ext_refresh_frame_context;
sarahparker21dbca42018-03-30 17:43:44 -0700682 int ext_use_ref_frame_mvs;
sarahparker27d686a2018-03-30 17:43:44 -0700683 int ext_use_error_resilient;
sarahparker9806fed2018-03-30 17:43:44 -0700684 int ext_use_s_frame;
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700685 int ext_use_primary_ref_none;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686
687 YV12_BUFFER_CONFIG last_frame_uf;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800688 YV12_BUFFER_CONFIG trial_frame_rst;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689
690 // Ambient reconstruction err target for force key frames
691 int64_t ambient_err;
692
693 RD_OPT rd;
694
695 CODING_CONTEXT coding_context;
696
Yue Chenb23d00a2017-07-28 17:01:21 -0700697 int gmtype_cost[TRANS_TYPES];
Zoe Liu27deb382018-03-27 15:13:56 -0700698 int gmparams_cost[REF_FRAMES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700699
Jingning Hanf050fc12018-03-09 14:53:33 -0800700 int nmv_costs[2][MV_VALS];
701 int nmv_costs_hp[2][MV_VALS];
James Zern01a9d702017-08-25 19:09:33 +0000702
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703 int64_t last_time_stamp_seen;
704 int64_t last_end_time_stamp_seen;
705 int64_t first_time_stamp_ever;
706
707 RATE_CONTROL rc;
708 double framerate;
709
Urvang Joshia130dcc2018-11-06 10:27:35 -0800710 // Relevant for an inter frame.
711 // - Index '0' corresponds to the values for the currently coded frame.
712 // - Indices 1 ... REF_FRAMES are used to store values for all the possible
713 // reference frames.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700714 int interp_filter_selected[REF_FRAMES + 1][SWITCHABLE];
715
Yaowu Xuf883b422016-08-30 14:01:10 -0700716 struct aom_codec_pkt_list *output_pkt_list;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700717
718 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
719 int mbgraph_n_frames; // number of frames filled in the above
720 int static_mb_pct; // % forced skip mbs by segmentation
721 int ref_frame_flags;
Yunqing Wangf2e7a392017-11-08 00:27:21 -0800722 int ext_ref_frame_flags;
Zoe Liuf452fdf2017-11-02 23:08:12 -0700723 RATE_FACTOR_LEVEL frame_rf_level[FRAME_BUFFERS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700724
725 SPEED_FEATURES sf;
726
727 unsigned int max_mv_magnitude;
728 int mv_step_param;
729
730 int allow_comp_inter_inter;
Zoe Liu77fb5be2017-11-02 14:36:19 -0700731 int all_one_sided_refs;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732
Yaowu Xuc27fc142016-08-22 16:08:15 -0700733 uint8_t *segmentation_map;
734
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 CYCLIC_REFRESH *cyclic_refresh;
736 ActiveMap active_map;
737
738 fractional_mv_step_fp *find_fractional_mv_step;
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 av1_diamond_search_fn_t diamond_search_sad;
Rupert Swarbrick93c39e92017-07-12 11:11:02 +0100740 aom_variance_fn_ptr_t fn_ptr[BLOCK_SIZES_ALL];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 uint64_t time_receive_data;
742 uint64_t time_compress_data;
743 uint64_t time_pick_lpf;
744 uint64_t time_encode_sb_row;
745
746#if CONFIG_FP_MB_STATS
747 int use_fp_mb_stats;
748#endif
749
750 TWO_PASS twopass;
751
752 YV12_BUFFER_CONFIG alt_ref_buffer;
753
754#if CONFIG_INTERNAL_STATS
755 unsigned int mode_chosen_counts[MAX_MODES];
756
757 int count;
758 uint64_t total_sq_error;
759 uint64_t total_samples;
760 ImageStat psnr;
761
762 double total_blockiness;
763 double worst_blockiness;
764
765 int bytes;
766 double summed_quality;
767 double summed_weights;
768 unsigned int tot_recode_hits;
769 double worst_ssim;
770
771 ImageStat fastssim;
772 ImageStat psnrhvs;
773
774 int b_calculate_blockiness;
775 int b_calculate_consistency;
776
777 double total_inconsistency;
778 double worst_consistency;
779 Ssimv *ssim_vars;
780 Metrics metrics;
781#endif
782 int b_calculate_psnr;
783
784 int droppable;
785
786 int initial_width;
787 int initial_height;
788 int initial_mbs; // Number of MBs in the full-size frame; to be used to
789 // normalize the firstpass stats. This will differ from the
790 // number of MBs in the current frame when the frame is
791 // scaled.
792
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700793 // When resize is triggered through external control, the desired width/height
794 // are stored here until use in the next frame coded. They are effective only
795 // for
796 // one frame and are reset after use.
797 int resize_pending_width;
798 int resize_pending_height;
799
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 int frame_flags;
801
802 search_site_config ss_cfg;
803
Yaowu Xuc27fc142016-08-22 16:08:15 -0700804 TileDataEnc *tile_data;
805 int allocated_tiles; // Keep track of memory allocated for tiles.
806
807 TOKENEXTRA *tile_tok[MAX_TILE_ROWS][MAX_TILE_COLS];
808 unsigned int tok_count[MAX_TILE_ROWS][MAX_TILE_COLS];
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530809 TOKENLIST *tplist[MAX_TILE_ROWS][MAX_TILE_COLS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810
811 TileBufferEnc tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
812
Yaowu Xuc27fc142016-08-22 16:08:15 -0700813 int resize_state;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700814 int resize_avg_qp;
815 int resize_buffer_underflow;
816 int resize_count;
817
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -0700818 // Sequence parameters have been transmitted already and locked
819 // or not. Once locked av1_change_config cannot change the seq
820 // parameters.
821 int seq_params_locked;
822
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823 // VARIANCE_AQ segment map refresh
824 int vaq_refresh;
825
826 // Multi-threading
827 int num_workers;
Yaowu Xuf883b422016-08-30 14:01:10 -0700828 AVxWorker *workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829 struct EncWorkerData *tile_thr_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700830 int refresh_frame_mask;
831 int existing_fb_idx_to_show;
832 int is_arf_filter_off[MAX_EXT_ARFS + 1];
833 int num_extra_arfs;
Zoe Liuf271a952017-09-05 15:03:48 -0700834 int arf_pos_in_gf[MAX_EXT_ARFS + 1];
835 int arf_pos_for_ovrly[MAX_EXT_ARFS + 1];
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -0800836 int global_motion_search_done;
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700837 tran_low_t *tcoeff_buf[MAX_MB_PLANE];
Di Chen53a04f62017-06-23 13:47:56 -0700838 int extra_arf_allowed;
Hui Su85878782017-11-07 14:56:31 -0800839 // A flag to indicate if intrabc is ever used in current frame.
840 int intrabc_used;
Hui Sudfcbfbd2017-11-13 12:05:30 -0800841 int dv_cost[2][MV_VALS];
842 // TODO(huisu@google.com): we can update dv_joint_cost per SB.
843 int dv_joint_cost[MV_JOINTS];
Hui Suad7551e2018-03-14 11:13:31 -0700844 int has_lossless_segment;
Zoe Liu1d90ceb2018-04-16 16:53:37 -0700845
846 // For frame refs short signaling:
847 // A mapping of each reference frame from its encoder side value to the
848 // decoder side value obtained following the short signaling procedure.
849 int ref_conv[REF_FRAMES];
Deepa K G964e72e2018-05-16 16:56:01 +0530850
851 AV1LfSync lf_row_sync;
Ravi Chaudharye2aa4012018-06-04 14:20:00 +0530852 AV1LrSync lr_row_sync;
Ravi Chaudharyce0f5fc2018-05-30 16:19:32 +0530853 AV1LrStruct lr_ctxt;
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700854
855 aom_film_grain_table_t *film_grain_table;
856#if CONFIG_DENOISE
857 struct aom_denoise_and_model_t *denoise_and_model;
858#endif
Ranjit Kumar Tulabandu8105bf42018-07-27 14:16:55 +0530859 // Stores the default value of skip flag depending on chroma format
860 // Set as 1 for monochrome and 3 for other color formats
861 int default_interp_skip_flags;
Wei-Ting Lina8c02452018-08-13 11:04:06 -0700862 int preserve_arf_as_gld;
Ravi Chaudharyc5e74692018-10-08 16:05:38 +0530863 MultiThreadHandle multi_thread_ctxt;
Ravi Chaudhary40cdf132018-10-08 11:04:16 +0530864 void (*row_mt_sync_read_ptr)(AV1RowMTSync *const, int, int);
865 void (*row_mt_sync_write_ptr)(AV1RowMTSync *const, int, int, const int);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +0530866#if CONFIG_MULTITHREAD
867 pthread_mutex_t *row_mt_mutex_;
868#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700869} AV1_COMP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700871// Must not be called more than once.
Yaowu Xuf883b422016-08-30 14:01:10 -0700872void av1_initialize_enc(void);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873
Yaowu Xuf883b422016-08-30 14:01:10 -0700874struct AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
875 BufferPool *const pool);
876void av1_remove_compressor(AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700877
Yaowu Xuf883b422016-08-30 14:01:10 -0700878void av1_change_config(AV1_COMP *cpi, const AV1EncoderConfig *oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700879
880// receive a frames worth of data. caller can assume that a copy of this
881// frame is made and not just a copy of the pointer..
James Zern3e2613b2017-03-30 23:14:40 -0700882int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -0700883 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
884 int64_t end_time_stamp);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700885
Andrey Norkin795ba872018-03-06 13:24:14 -0800886int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
887 size_t *size, uint8_t *dest, int64_t *time_stamp,
888 int64_t *time_end, int flush,
889 const aom_rational_t *timebase);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700890
Yaowu Xuf883b422016-08-30 14:01:10 -0700891int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700892
Yaowu Xuf883b422016-08-30 14:01:10 -0700893int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700894
Yunqing Wang93b18f32018-06-08 21:08:29 -0700895aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
896 YV12_BUFFER_CONFIG *new_frame,
897 YV12_BUFFER_CONFIG *sd);
Yunqing Wangff9bfca2018-06-06 11:46:08 -0700898
Yaowu Xuf883b422016-08-30 14:01:10 -0700899int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900
Yaowu Xuf883b422016-08-30 14:01:10 -0700901void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902
Thomas Daede497d1952017-08-08 17:33:06 -0700903int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700904
Thomas Daede497d1952017-08-08 17:33:06 -0700905int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700906
Yaowu Xuf883b422016-08-30 14:01:10 -0700907int av1_update_entropy(AV1_COMP *cpi, int update);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908
Yaowu Xuf883b422016-08-30 14:01:10 -0700909int av1_set_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700910
Yaowu Xuf883b422016-08-30 14:01:10 -0700911int av1_get_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700912
Yaowu Xuf883b422016-08-30 14:01:10 -0700913int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
914 AOM_SCALING vert_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700915
Yaowu Xuf883b422016-08-30 14:01:10 -0700916int av1_get_quantizer(struct AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700917
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400918int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *input_size);
919
Andrey Norkin795ba872018-03-06 13:24:14 -0800920int64_t timebase_units_to_ticks(const aom_rational_t *timebase, int64_t n);
921int64_t ticks_to_timebase_units(const aom_rational_t *timebase, int64_t n);
Andrey Norkin795ba872018-03-06 13:24:14 -0800922
Yaowu Xuf883b422016-08-30 14:01:10 -0700923static INLINE int frame_is_kf_gf_arf(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924 return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
925 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
926}
927
Yaowu Xuf883b422016-08-30 14:01:10 -0700928static INLINE int get_ref_frame_map_idx(const AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929 MV_REFERENCE_FRAME ref_frame) {
Urvang Joshi4d9f15f2018-11-05 15:26:22 -0800930 return (ref_frame >= 1) ? cpi->remapped_ref_idx[ref_frame - 1] : INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700931}
932
Urvang Joshi52648442016-10-13 17:27:51 -0700933static INLINE int get_ref_frame_buf_idx(const AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934 MV_REFERENCE_FRAME ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700935 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700936 const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
937 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX;
938}
939
Hui Su2d5fd742018-02-21 18:10:37 -0800940// TODO(huisu@google.com, youzhou@microsoft.com): enable hash-me for HBD.
941static INLINE int av1_use_hash_me(const AV1_COMMON *const cm) {
Debargha Mukherjee8112d2f2018-03-01 09:53:11 -0800942 return cm->allow_screen_content_tools;
Hui Su2d5fd742018-02-21 18:10:37 -0800943}
944
945static INLINE hash_table *av1_get_ref_frame_hash_map(
946 const AV1_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
RogerZhoucc5d35d2017-08-07 22:20:15 -0700947 const AV1_COMMON *const cm = &cpi->common;
948 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
949 return buf_idx != INVALID_IDX
950 ? &cm->buffer_pool->frame_bufs[buf_idx].hash_table
951 : NULL;
952}
RogerZhoucc5d35d2017-08-07 22:20:15 -0700953
Yaowu Xuc27fc142016-08-22 16:08:15 -0700954static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
Urvang Joshi52648442016-10-13 17:27:51 -0700955 const AV1_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
956 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
958 return buf_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[buf_idx].buf
959 : NULL;
960}
961
Yaowu Xuf883b422016-08-30 14:01:10 -0700962static INLINE int enc_is_ref_frame_buf(AV1_COMP *cpi, RefCntBuffer *frame_buf) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700963 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi30058542018-11-05 15:08:31 -0800964 int ref_num;
965 for (ref_num = LAST_FRAME; ref_num <= ALTREF_FRAME; ++ref_num) {
966 const MV_REFERENCE_FRAME ref_frame = (MV_REFERENCE_FRAME)ref_num;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700967 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
968 if (buf_idx == INVALID_IDX) continue;
969 if (frame_buf == &cm->buffer_pool->frame_bufs[buf_idx]) break;
970 }
Urvang Joshi30058542018-11-05 15:08:31 -0800971 return (ref_num <= ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700973
Hui Su8ac0c982018-04-03 12:17:36 -0700974// Token buffer is only used for palette tokens.
Yaowu Xue39b3b82017-10-31 16:11:59 -0700975static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000976 int sb_size_log2,
977 const int num_planes) {
Rupert Swarbrickce63e832017-10-25 17:54:17 +0100978 // Calculate the maximum number of max superblocks in the image.
Yaowu Xue39b3b82017-10-31 16:11:59 -0700979 const int shift = sb_size_log2 - 4;
980 const int sb_size = 1 << sb_size_log2;
981 const int sb_size_square = sb_size * sb_size;
Rupert Swarbrickce63e832017-10-25 17:54:17 +0100982 const int sb_rows = ALIGN_POWER_OF_TWO(mb_rows, shift) >> shift;
983 const int sb_cols = ALIGN_POWER_OF_TWO(mb_cols, shift) >> shift;
984
Hui Su8ac0c982018-04-03 12:17:36 -0700985 // One palette token for each pixel. There can be palettes on two planes.
986 const int sb_palette_toks = AOMMIN(2, num_planes) * sb_size_square;
Rupert Swarbrickce63e832017-10-25 17:54:17 +0100987
Hui Su8ac0c982018-04-03 12:17:36 -0700988 return sb_rows * sb_cols * sb_palette_toks;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700989}
990
991// Get the allocated token size for a tile. It does the same calculation as in
992// the frame token allocation.
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +0000993static INLINE unsigned int allocated_tokens(TileInfo tile, int sb_size_log2,
994 int num_planes) {
Jingning Haneafbd5f2017-03-07 11:18:17 -0800995 int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 2) >> 2;
996 int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 2) >> 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700997
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +0000998 return get_token_alloc(tile_mb_rows, tile_mb_cols, sb_size_log2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700999}
1000
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301001static INLINE void get_start_tok(AV1_COMP *cpi, int tile_row, int tile_col,
1002 int mi_row, TOKENEXTRA **tok, int sb_size_log2,
1003 int num_planes) {
1004 AV1_COMMON *const cm = &cpi->common;
1005 const int tile_cols = cm->tile_cols;
1006 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
1007 const TileInfo *const tile_info = &this_tile->tile_info;
1008
1009 const int tile_mb_cols =
1010 (tile_info->mi_col_end - tile_info->mi_col_start + 2) >> 2;
1011 const int tile_mb_row = (mi_row - tile_info->mi_row_start + 2) >> 2;
1012
1013 *tok = cpi->tile_tok[tile_row][tile_col] +
1014 get_token_alloc(tile_mb_row, tile_mb_cols, sb_size_log2, num_planes);
1015}
1016
Yaowu Xuf883b422016-08-30 14:01:10 -07001017void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001018
Yaowu Xu6da42302017-10-10 14:45:06 -07001019#define ALT_MIN_LAG 3
Yaowu Xuf883b422016-08-30 14:01:10 -07001020static INLINE int is_altref_enabled(const AV1_COMP *const cpi) {
Yaowu Xu6da42302017-10-10 14:45:06 -07001021 return cpi->oxcf.lag_in_frames >= ALT_MIN_LAG && cpi->oxcf.enable_auto_arf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001022}
1023
1024// TODO(zoeliu): To set up cpi->oxcf.enable_auto_brf
Yaowu Xuc27fc142016-08-22 16:08:15 -07001025
Urvang Joshi52648442016-10-13 17:27:51 -07001026static INLINE void set_ref_ptrs(const AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001027 MV_REFERENCE_FRAME ref0,
1028 MV_REFERENCE_FRAME ref1) {
1029 xd->block_refs[0] =
1030 &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME : 0];
1031 xd->block_refs[1] =
1032 &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME : 0];
1033}
1034
Yaowu Xu4ff59b52017-04-24 12:41:56 -07001035static INLINE int get_chessboard_index(int frame_index) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001036 return frame_index & 0x1;
1037}
1038
Yaowu Xuf883b422016-08-30 14:01:10 -07001039static INLINE int *cond_cost_list(const struct AV1_COMP *cpi, int *cost_list) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001040 return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
1041}
1042
Yaowu Xuf883b422016-08-30 14:01:10 -07001043void av1_new_framerate(AV1_COMP *cpi, double framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001044
1045#define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
1046
Cheng Chen09c83a52018-06-05 12:27:36 -07001047// Returns 1 if a frame is scaled and 0 otherwise.
1048static INLINE int av1_resize_scaled(const AV1_COMMON *cm) {
1049 return !(cm->superres_upscaled_width == cm->render_width &&
1050 cm->superres_upscaled_height == cm->render_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07001051}
1052
Cheng Chen09c83a52018-06-05 12:27:36 -07001053static INLINE int av1_frame_scaled(const AV1_COMMON *cm) {
1054 return !av1_superres_scaled(cm) && av1_resize_scaled(cm);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07001055}
1056
Sarah Parker33005522018-07-27 14:46:25 -07001057// Don't allow a show_existing_frame to coincide with an error resilient
1058// frame. An exception can be made for a forward keyframe since it has no
1059// previous dependencies.
1060static INLINE int encode_show_existing_frame(const AV1_COMMON *cm) {
1061 return cm->show_existing_frame &&
1062 (!cm->error_resilient_mode || cm->frame_type == KEY_FRAME);
1063}
1064
Tom Fineganf8d6a162018-08-21 10:47:55 -07001065// Returns a Sequence Header OBU stored in an aom_fixed_buf_t, or NULL upon
1066// failure. When a non-NULL aom_fixed_buf_t pointer is returned by this
1067// function, the memory must be freed by the caller. Both the buf member of the
Tom Fineganca1e28f2018-08-28 16:55:35 -07001068// aom_fixed_buf_t, and the aom_fixed_buf_t pointer itself must be freed. Memory
1069// returned must be freed via call to free().
1070//
1071// Note: The OBU returned is in Low Overhead Bitstream Format. Specifically,
1072// the obu_has_size_field bit is set, and the buffer contains the obu_size
1073// field.
Tom Fineganf8d6a162018-08-21 10:47:55 -07001074aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi);
1075
Yaowu Xuc27fc142016-08-22 16:08:15 -07001076#ifdef __cplusplus
1077} // extern "C"
1078#endif
1079
James Zerne1cbb132018-08-22 14:10:36 -07001080#endif // AOM_AV1_ENCODER_ENCODER_H_