blob: f7b3b4fb924f1d5098aa1f3a34e51e3b00e490eb [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
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AV1_ENCODER_ENCODER_H_
13#define AV1_ENCODER_ENCODER_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include <stdio.h>
16
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "./aom_config.h"
18#include "aom/aomcx.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
20#include "av1/common/alloccommon.h"
21#include "av1/common/entropymode.h"
22#include "av1/common/thread_common.h"
23#include "av1/common/onyxc_int.h"
Fergus Simpsond2bcbb52017-05-22 23:15:05 -070024#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "av1/encoder/aq_cyclicrefresh.h"
Tom Finegan17ce8b12017-02-08 12:46:31 -080026#include "av1/encoder/av1_quantize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/encoder/context_tree.h"
28#include "av1/encoder/encodemb.h"
29#include "av1/encoder/firstpass.h"
30#include "av1/encoder/lookahead.h"
31#include "av1/encoder/mbgraph.h"
32#include "av1/encoder/mcomp.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033#include "av1/encoder/ratectrl.h"
34#include "av1/encoder/rd.h"
35#include "av1/encoder/speed_features.h"
36#include "av1/encoder/tokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070037
38#if CONFIG_INTERNAL_STATS
39#include "aom_dsp/ssim.h"
40#endif
41#include "aom_dsp/variance.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070042#include "aom/internal/aom_codec_internal.h"
43#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070044
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49typedef struct {
Jingning Hanf050fc12018-03-09 14:53:33 -080050 int nmv_vec_cost[MV_JOINTS];
51 int nmv_costs[2][MV_VALS];
52 int nmv_costs_hp[2][MV_VALS];
James Zern01a9d702017-08-25 19:09:33 +000053
Yaowu Xuc27fc142016-08-22 16:08:15 -070054 FRAME_CONTEXT fc;
55} CODING_CONTEXT;
56
57typedef enum {
58 // regular inter frame
59 REGULAR_FRAME = 0,
60 // alternate reference frame
61 ARF_FRAME = 1,
62 // overlay frame
63 OVERLAY_FRAME = 2,
64 // golden frame
65 GLD_FRAME = 3,
Yaowu Xuc27fc142016-08-22 16:08:15 -070066 // backward reference frame
67 BRF_FRAME = 4,
68 // extra alternate reference frame
Thomas Daede51020e12017-12-14 20:12:44 -080069 EXT_ARF_FRAME = 5,
70 FRAME_CONTEXT_INDEXES
Yaowu Xuc27fc142016-08-22 16:08:15 -070071} FRAME_CONTEXT_INDEX;
72
73typedef enum {
Yaowu Xuc27fc142016-08-22 16:08:15 -070074 NORMAL = 0,
75 FOURFIVE = 1,
76 THREEFIVE = 2,
77 ONETWO = 3
Yaowu Xuf883b422016-08-30 14:01:10 -070078} AOM_SCALING;
Yaowu Xuc27fc142016-08-22 16:08:15 -070079
80typedef enum {
81 // Good Quality Fast Encoding. The encoder balances quality with the amount of
82 // time it takes to encode the output. Speed setting controls how fast.
Thomas Daede80826142017-03-20 15:44:24 -070083 GOOD
Yaowu Xuc27fc142016-08-22 16:08:15 -070084} MODE;
85
86typedef enum {
87 FRAMEFLAGS_KEY = 1 << 0,
88 FRAMEFLAGS_GOLDEN = 1 << 1,
Yaowu Xuc27fc142016-08-22 16:08:15 -070089 FRAMEFLAGS_BWDREF = 1 << 2,
Zoe Liu3ac20932017-08-30 16:35:55 -070090 // TODO(zoeliu): To determine whether a frame flag is needed for ALTREF2_FRAME
Yaowu Xuc27fc142016-08-22 16:08:15 -070091 FRAMEFLAGS_ALTREF = 1 << 3,
Yaowu Xuc27fc142016-08-22 16:08:15 -070092} FRAMETYPE_FLAGS;
93
94typedef enum {
95 NO_AQ = 0,
96 VARIANCE_AQ = 1,
97 COMPLEXITY_AQ = 2,
98 CYCLIC_REFRESH_AQ = 3,
99 AQ_MODE_COUNT // This should always be the last member of the enum
100} AQ_MODE;
Fangwen Fu6160df22017-04-24 09:45:51 -0700101typedef enum {
102 NO_DELTA_Q = 0,
103 DELTA_Q_ONLY = 1,
104 DELTA_Q_LF = 2,
105 DELTAQ_MODE_COUNT // This should always be the last member of the enum
106} DELTAQ_MODE;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800107
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108typedef enum {
109 RESIZE_NONE = 0, // No frame resizing allowed.
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700110 RESIZE_FIXED = 1, // All frames are coded at the specified scale.
111 RESIZE_RANDOM = 2, // All frames are coded at a random scale.
112 RESIZE_MODES
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700113} RESIZE_MODE;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800114
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700115typedef enum {
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700116 SUPERRES_NONE = 0, // No frame superres allowed
117 SUPERRES_FIXED = 1, // All frames are coded at the specified scale,
118 // and super-resolved.
119 SUPERRES_RANDOM = 2, // All frames are coded at a random scale,
120 // and super-resolved.
121 SUPERRES_QTHRESH = 3, // Superres scale for a frame is determined based on
122 // q_index
123 SUPERRES_MODES
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700124} SUPERRES_MODE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125
Yaowu Xuf883b422016-08-30 14:01:10 -0700126typedef struct AV1EncoderConfig {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700127 BITSTREAM_PROFILE profile;
Tom Finegan8ab2bba2018-02-28 07:36:28 -0800128 aom_bit_depth_t bit_depth; // Codec bit-depth.
129 int width; // width of data passed to the compressor
130 int height; // height of data passed to the compressor
131 int forced_max_frame_width; // forced maximum width of frame (if != 0)
132 int forced_max_frame_height; // forced maximum height of frame (if != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700133 unsigned int input_bit_depth; // Input bit depth.
134 double init_framerate; // set to passed in framerate
135 int64_t target_bandwidth; // bandwidth to be used in bits per second
136
137 int noise_sensitivity; // pre processing blur: recommendation 0
138 int sharpness; // sharpening output: recommendation 0:
139 int speed;
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800140 int dev_sf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700141 // maximum allowed bitrate for any intra frame in % of bitrate target.
142 unsigned int rc_max_intra_bitrate_pct;
143 // maximum allowed bitrate for any inter frame in % of bitrate target.
144 unsigned int rc_max_inter_bitrate_pct;
145 // percent of rate boost for golden frame in CBR mode.
146 unsigned int gf_cbr_boost_pct;
147
148 MODE mode;
149 int pass;
150
151 // Key Framing Operations
152 int auto_key; // autodetect cut scenes and set the keyframes
153 int key_freq; // maximum distance to key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -0500154 int sframe_dist;
155 int sframe_mode;
156 int sframe_enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700157 int lag_in_frames; // how many frames lag before we start encoding
158
159 // ----------------------------------------------------------------
160 // DATARATE CONTROL OPTIONS
161
162 // vbr, cbr, constrained quality or constant quality
Yaowu Xuf883b422016-08-30 14:01:10 -0700163 enum aom_rc_mode rc_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164
165 // buffer targeting aggressiveness
166 int under_shoot_pct;
167 int over_shoot_pct;
168
169 // buffering parameters
170 int64_t starting_buffer_level_ms;
171 int64_t optimal_buffer_level_ms;
172 int64_t maximum_buffer_size_ms;
173
174 // Frame drop threshold.
175 int drop_frames_water_mark;
176
177 // controlling quality
178 int fixed_q;
179 int worst_allowed_q;
180 int best_allowed_q;
181 int cq_level;
182 AQ_MODE aq_mode; // Adaptive Quantization mode
Fangwen Fu6160df22017-04-24 09:45:51 -0700183 DELTAQ_MODE deltaq_mode;
Debargha Mukherjee98a311c2018-03-25 16:33:11 -0700184 int enable_cdef;
185 int enable_restoration;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 int using_qm;
Yaowu Xuf7a12422018-01-31 15:29:20 -0800187 int qm_y;
188 int qm_u;
189 int qm_v;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190 int qm_minlevel;
191 int qm_maxlevel;
Yushin Chod808bfc2017-08-10 15:54:36 -0700192#if CONFIG_DIST_8X8
193 int using_dist_8x8;
194#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000195 unsigned int num_tile_groups;
196 unsigned int mtu;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197
198 // Internal frame size scaling.
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700199 RESIZE_MODE resize_mode;
Urvang Joshide71d142017-10-05 12:12:15 -0700200 uint8_t resize_scale_denominator;
201 uint8_t resize_kf_scale_denominator;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700203 // Frame Super-Resolution size scaling.
204 SUPERRES_MODE superres_mode;
Urvang Joshide71d142017-10-05 12:12:15 -0700205 uint8_t superres_scale_denominator;
206 uint8_t superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700207 int superres_qthresh;
208 int superres_kf_qthresh;
Fergus Simpson3502d082017-04-10 12:25:07 -0700209
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210 // Enable feature to reduce the frame quantization every x frames.
211 int frame_periodic_boost;
212
213 // two pass datarate control
214 int two_pass_vbrbias; // two pass datarate control tweaks
215 int two_pass_vbrmin_section;
216 int two_pass_vbrmax_section;
217 // END DATARATE CONTROL OPTIONS
218 // ----------------------------------------------------------------
219
220 int enable_auto_arf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700221 int enable_auto_brf; // (b)ackward (r)ef (f)rame
Yaowu Xuc27fc142016-08-22 16:08:15 -0700222
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223 /* Bitfield defining the error resiliency features to enable.
224 * Can provide decodable frames after losses in previous
225 * frames and decodable partitions after losses in the same frame.
226 */
227 unsigned int error_resilient_mode;
228
Debargha Mukherjee52fb0472018-03-29 15:48:11 -0700229 unsigned int s_frame_mode;
230
Yaowu Xuc27fc142016-08-22 16:08:15 -0700231 /* Bitfield defining the parallel decoding mode where the
232 * decoding in successive frames may be conducted in parallel
233 * just by decoding the frame headers.
234 */
235 unsigned int frame_parallel_decoding_mode;
236
237 int arnr_max_frames;
238 int arnr_strength;
239
240 int min_gf_interval;
241 int max_gf_interval;
242
243 int tile_columns;
244 int tile_rows;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200245 int tile_width_count;
246 int tile_height_count;
247 int tile_widths[MAX_TILE_COLS];
248 int tile_heights[MAX_TILE_ROWS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249
250 int max_threads;
251
Yaowu Xuf883b422016-08-30 14:01:10 -0700252 aom_fixed_buf_t two_pass_stats_in;
253 struct aom_codec_pkt_list *output_pkt_list;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254
255#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -0700256 aom_fixed_buf_t firstpass_mb_stats_in;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257#endif
258
Yaowu Xuf883b422016-08-30 14:01:10 -0700259 aom_tune_metric tuning;
260 aom_tune_content content;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261 int use_highbitdepth;
Andrey Norkin9e694632017-12-21 18:50:57 -0800262 aom_color_primaries_t color_primaries;
263 aom_transfer_characteristics_t transfer_characteristics;
264 aom_matrix_coefficients_t matrix_coefficients;
anorkin76fb1262017-03-22 15:12:12 -0700265 aom_chroma_sample_position_t chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700266 int color_range;
267 int render_width;
268 int render_height;
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800269 aom_timing_info_t timing_info;
270 int timing_info_present;
271 uint32_t num_units_in_tick;
272 uint32_t time_scale;
273 int equal_picture_interval;
274 uint32_t num_ticks_per_picture;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800275 int film_grain_test_vector;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700276 const char *film_grain_table_filename;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700277
Hui Su1cb1c002018-02-05 18:21:20 -0800278 uint8_t cdf_update_mode;
Yaowu Xuf883b422016-08-30 14:01:10 -0700279 aom_superblock_size_t superblock_size;
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700280 unsigned int large_scale_tile;
281 unsigned int single_tile_decoding;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800282 int monochrome;
Jingning Hana446f552018-02-22 15:42:12 -0800283 int enable_dual_filter;
Yunqing Wangff4fa062017-04-21 10:56:08 -0700284 unsigned int motion_vector_unit_test;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100285 const cfg_options_t *cfg;
Debargha Mukherjee0d8368a2018-03-25 12:23:02 -0700286 int enable_order_hint;
Cheng Chene0c918a2018-02-22 19:38:31 -0800287 int enable_jnt_comp;
Debargha Mukherjee0187d7c2018-03-25 11:37:56 -0700288 int enable_ref_frame_mvs;
Debargha Mukherjee0d8368a2018-03-25 12:23:02 -0700289 unsigned int allow_ref_frame_mvs;
Debargha Mukherjee37df9162018-03-25 12:48:24 -0700290 int enable_warped_motion;
291 int allow_warped_motion;
Urvang Joshi2c92b072018-03-19 17:23:31 -0700292 int enable_superres;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400293 unsigned int save_as_annexb;
Yaowu Xuf883b422016-08-30 14:01:10 -0700294} AV1EncoderConfig;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295
Yaowu Xuf883b422016-08-30 14:01:10 -0700296static INLINE int is_lossless_requested(const AV1EncoderConfig *cfg) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700297 return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
298}
299
300// TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
301typedef struct TileDataEnc {
302 TileInfo tile_info;
Rupert Swarbrick93c39e92017-07-12 11:11:02 +0100303 int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
304 int mode_map[BLOCK_SIZES_ALL][MAX_MODES];
Yunqing Wang8c1e57c2016-10-25 15:15:23 -0700305 int m_search_count;
306 int ex_search_count;
Luc Trudeauf8164152017-04-11 16:20:51 -0400307 CFL_CTX cfl;
Jingning Han9f07be12017-04-13 09:31:40 -0700308 DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
Yunqing Wang0e141b52017-11-02 15:08:58 -0700309 uint8_t allow_update_cdf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310} TileDataEnc;
311
312typedef struct RD_COUNTS {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700313 int64_t comp_pred_diff[REFERENCE_MODES];
Debargha Mukherjeea575d232017-04-28 17:46:47 -0700314 // Stores number of 4x4 blocks using global motion per reference frame.
Zoe Liu27deb382018-03-27 15:13:56 -0700315 int global_motion_used[REF_FRAMES];
Arild Fuldseth (arilfuld)6c20c782017-06-15 09:45:02 +0200316 int compound_ref_used_flag;
Zoe Liu8a5d3432017-11-30 16:33:44 -0800317 int skip_mode_used_flag;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318} RD_COUNTS;
319
320typedef struct ThreadData {
321 MACROBLOCK mb;
322 RD_COUNTS rd_counts;
323 FRAME_COUNTS *counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700324 PC_TREE *pc_tree;
325 PC_TREE *pc_root[MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2 + 1];
Jingning Hand064cf02017-06-01 10:00:39 -0700326 int32_t *wsrc_buf;
327 int32_t *mask_buf;
328 uint8_t *above_pred_buf;
329 uint8_t *left_pred_buf;
hui su5d493142017-05-08 12:06:12 -0700330 PALETTE_BUFFER *palette_buffer;
Hui Su85878782017-11-07 14:56:31 -0800331 int intrabc_used_this_tile;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700332} ThreadData;
333
334struct EncWorkerData;
335
336typedef struct ActiveMap {
337 int enabled;
338 int update;
339 unsigned char *map;
340} ActiveMap;
341
Urvang Joshib5ed3502016-10-17 16:38:05 -0700342#define NUM_STAT_TYPES 4 // types of stats: Y, U, V and ALL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343
344typedef struct IMAGE_STAT {
Urvang Joshib5ed3502016-10-17 16:38:05 -0700345 double stat[NUM_STAT_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700346 double worst;
347} ImageStat;
348
Urvang Joshib5ed3502016-10-17 16:38:05 -0700349#undef NUM_STAT_TYPES
350
Yaowu Xuc27fc142016-08-22 16:08:15 -0700351typedef struct {
352 int ref_count;
353 YV12_BUFFER_CONFIG buf;
354} EncRefCntBuffer;
355
Yaowu Xuc27fc142016-08-22 16:08:15 -0700356typedef struct TileBufferEnc {
357 uint8_t *data;
358 size_t size;
359} TileBufferEnc;
360
Yaowu Xuf883b422016-08-30 14:01:10 -0700361typedef struct AV1_COMP {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 QUANTS quants;
363 ThreadData td;
364 MB_MODE_INFO_EXT *mbmi_ext_base;
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700365 CB_COEFF_BUFFER *coeff_buffer_base;
Yi Luoc6210232017-05-25 15:09:25 -0700366 Dequants dequants;
Yaowu Xuf883b422016-08-30 14:01:10 -0700367 AV1_COMMON common;
368 AV1EncoderConfig oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700369 struct lookahead_ctx *lookahead;
370 struct lookahead_entry *alt_ref_source;
371
Angie Chiange69a6822018-03-16 13:52:44 -0700372 int optimize_speed_feature;
373 int optimize_seg_arr[MAX_SEGMENTS];
374
Alex Conversef77fd0b2017-04-20 11:00:24 -0700375 YV12_BUFFER_CONFIG *source;
376 YV12_BUFFER_CONFIG *last_source; // NULL for first frame and alt_ref frames
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700377 YV12_BUFFER_CONFIG *unscaled_source;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700378 YV12_BUFFER_CONFIG scaled_source;
379 YV12_BUFFER_CONFIG *unscaled_last_source;
380 YV12_BUFFER_CONFIG scaled_last_source;
381
Yaowu Xuc27fc142016-08-22 16:08:15 -0700382 // For a still frame, this flag is set to 1 to skip partition search.
383 int partition_search_skippable_frame;
RogerZhou3b635242017-09-19 10:06:46 -0700384 double csm_rate_array[32];
385 double m_rate_array[32];
386 int rate_size;
387 int rate_index;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -0800388 hash_table *previous_hash_table;
389 int previous_index;
RogerZhou3b635242017-09-19 10:06:46 -0700390 int cur_poc; // DebugInfo
Yaowu Xuc27fc142016-08-22 16:08:15 -0700391
Zoe Liu27deb382018-03-27 15:13:56 -0700392 int scaled_ref_idx[REF_FRAMES];
Zoe Liu5989a722018-03-29 13:37:36 -0700393 int ref_fb_idx[REF_FRAMES];
Zoe Liu8dd1c982017-09-11 10:14:35 -0700394 int refresh_fb_idx; // ref frame buffer index to refresh
Yaowu Xuc27fc142016-08-22 16:08:15 -0700395
396 int last_show_frame_buf_idx; // last show frame buffer index
397
398 int refresh_last_frame;
399 int refresh_golden_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400 int refresh_bwd_ref_frame;
Zoe Liue9b15e22017-07-19 15:53:01 -0700401 int refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402 int refresh_alt_ref_frame;
403
404 int ext_refresh_frame_flags_pending;
405 int ext_refresh_last_frame;
406 int ext_refresh_golden_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -0700407 int ext_refresh_bwd_ref_frame;
408 int ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700409 int ext_refresh_alt_ref_frame;
410
411 int ext_refresh_frame_context_pending;
412 int ext_refresh_frame_context;
sarahparker21dbca42018-03-30 17:43:44 -0700413 int ext_use_ref_frame_mvs;
sarahparker27d686a2018-03-30 17:43:44 -0700414 int ext_use_error_resilient;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700415
416 YV12_BUFFER_CONFIG last_frame_uf;
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800417 YV12_BUFFER_CONFIG trial_frame_rst;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700418
419 // Ambient reconstruction err target for force key frames
420 int64_t ambient_err;
421
422 RD_OPT rd;
423
424 CODING_CONTEXT coding_context;
425
Yue Chenb23d00a2017-07-28 17:01:21 -0700426 int gmtype_cost[TRANS_TYPES];
Zoe Liu27deb382018-03-27 15:13:56 -0700427 int gmparams_cost[REF_FRAMES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700428
Jingning Hanf050fc12018-03-09 14:53:33 -0800429 int nmv_costs[2][MV_VALS];
430 int nmv_costs_hp[2][MV_VALS];
James Zern01a9d702017-08-25 19:09:33 +0000431
Yaowu Xuc27fc142016-08-22 16:08:15 -0700432 int64_t last_time_stamp_seen;
433 int64_t last_end_time_stamp_seen;
434 int64_t first_time_stamp_ever;
435
436 RATE_CONTROL rc;
437 double framerate;
438
439 // NOTE(zoeliu): Any inter frame allows maximum of REF_FRAMES inter
440 // references; Plus the currently coded frame itself, it is needed to allocate
441 // sufficient space to the size of the maximum possible number of frames.
442 int interp_filter_selected[REF_FRAMES + 1][SWITCHABLE];
443
Yaowu Xuf883b422016-08-30 14:01:10 -0700444 struct aom_codec_pkt_list *output_pkt_list;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700445
446 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
447 int mbgraph_n_frames; // number of frames filled in the above
448 int static_mb_pct; // % forced skip mbs by segmentation
449 int ref_frame_flags;
Yunqing Wangf2e7a392017-11-08 00:27:21 -0800450 int ext_ref_frame_flags;
Zoe Liuf452fdf2017-11-02 23:08:12 -0700451 RATE_FACTOR_LEVEL frame_rf_level[FRAME_BUFFERS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700452
453 SPEED_FEATURES sf;
454
455 unsigned int max_mv_magnitude;
456 int mv_step_param;
457
458 int allow_comp_inter_inter;
Zoe Liu77fb5be2017-11-02 14:36:19 -0700459 int all_one_sided_refs;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700460
Yaowu Xuc27fc142016-08-22 16:08:15 -0700461 uint8_t *segmentation_map;
462
Yaowu Xuc27fc142016-08-22 16:08:15 -0700463 CYCLIC_REFRESH *cyclic_refresh;
464 ActiveMap active_map;
465
466 fractional_mv_step_fp *find_fractional_mv_step;
Yaowu Xuf883b422016-08-30 14:01:10 -0700467 av1_full_search_fn_t full_search_sad; // It is currently unused.
468 av1_diamond_search_fn_t diamond_search_sad;
Rupert Swarbrick93c39e92017-07-12 11:11:02 +0100469 aom_variance_fn_ptr_t fn_ptr[BLOCK_SIZES_ALL];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700470 uint64_t time_receive_data;
471 uint64_t time_compress_data;
472 uint64_t time_pick_lpf;
473 uint64_t time_encode_sb_row;
474
475#if CONFIG_FP_MB_STATS
476 int use_fp_mb_stats;
477#endif
478
479 TWO_PASS twopass;
480
481 YV12_BUFFER_CONFIG alt_ref_buffer;
482
483#if CONFIG_INTERNAL_STATS
484 unsigned int mode_chosen_counts[MAX_MODES];
485
486 int count;
487 uint64_t total_sq_error;
488 uint64_t total_samples;
489 ImageStat psnr;
490
491 double total_blockiness;
492 double worst_blockiness;
493
494 int bytes;
495 double summed_quality;
496 double summed_weights;
497 unsigned int tot_recode_hits;
498 double worst_ssim;
499
500 ImageStat fastssim;
501 ImageStat psnrhvs;
502
503 int b_calculate_blockiness;
504 int b_calculate_consistency;
505
506 double total_inconsistency;
507 double worst_consistency;
508 Ssimv *ssim_vars;
509 Metrics metrics;
510#endif
511 int b_calculate_psnr;
512
513 int droppable;
514
515 int initial_width;
516 int initial_height;
517 int initial_mbs; // Number of MBs in the full-size frame; to be used to
518 // normalize the firstpass stats. This will differ from the
519 // number of MBs in the current frame when the frame is
520 // scaled.
521
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700522 // When resize is triggered through external control, the desired width/height
523 // are stored here until use in the next frame coded. They are effective only
524 // for
525 // one frame and are reset after use.
526 int resize_pending_width;
527 int resize_pending_height;
528
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529 int frame_flags;
530
531 search_site_config ss_cfg;
532
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 int multi_arf_allowed;
534 int multi_arf_enabled;
535 int multi_arf_last_grp_enabled;
536
537 TileDataEnc *tile_data;
538 int allocated_tiles; // Keep track of memory allocated for tiles.
539
540 TOKENEXTRA *tile_tok[MAX_TILE_ROWS][MAX_TILE_COLS];
541 unsigned int tok_count[MAX_TILE_ROWS][MAX_TILE_COLS];
542
543 TileBufferEnc tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
544
Yaowu Xuc27fc142016-08-22 16:08:15 -0700545 int resize_state;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700546 int resize_avg_qp;
547 int resize_buffer_underflow;
548 int resize_count;
549
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -0700550 // Sequence parameters have been transmitted already and locked
551 // or not. Once locked av1_change_config cannot change the seq
552 // parameters.
553 int seq_params_locked;
554
Yaowu Xuc27fc142016-08-22 16:08:15 -0700555 // VARIANCE_AQ segment map refresh
556 int vaq_refresh;
557
558 // Multi-threading
559 int num_workers;
Yaowu Xuf883b422016-08-30 14:01:10 -0700560 AVxWorker *workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700561 struct EncWorkerData *tile_thr_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 int refresh_frame_mask;
563 int existing_fb_idx_to_show;
564 int is_arf_filter_off[MAX_EXT_ARFS + 1];
565 int num_extra_arfs;
566 int arf_map[MAX_EXT_ARFS + 1];
Zoe Liuf271a952017-09-05 15:03:48 -0700567 int arf_pos_in_gf[MAX_EXT_ARFS + 1];
568 int arf_pos_for_ovrly[MAX_EXT_ARFS + 1];
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -0800569 int global_motion_search_done;
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700570 tran_low_t *tcoeff_buf[MAX_MB_PLANE];
Di Chen53a04f62017-06-23 13:47:56 -0700571 int extra_arf_allowed;
572 int bwd_ref_allowed;
Hui Su85878782017-11-07 14:56:31 -0800573 // A flag to indicate if intrabc is ever used in current frame.
574 int intrabc_used;
Hui Sudfcbfbd2017-11-13 12:05:30 -0800575 int dv_cost[2][MV_VALS];
576 // TODO(huisu@google.com): we can update dv_joint_cost per SB.
577 int dv_joint_cost[MV_JOINTS];
Hui Suad7551e2018-03-14 11:13:31 -0700578 int has_lossless_segment;
Yaowu Xuf883b422016-08-30 14:01:10 -0700579} AV1_COMP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700580
Yaowu Xuf883b422016-08-30 14:01:10 -0700581void av1_initialize_enc(void);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582
Yaowu Xuf883b422016-08-30 14:01:10 -0700583struct AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
584 BufferPool *const pool);
585void av1_remove_compressor(AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700586
Yaowu Xuf883b422016-08-30 14:01:10 -0700587void av1_change_config(AV1_COMP *cpi, const AV1EncoderConfig *oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700588
589// receive a frames worth of data. caller can assume that a copy of this
590// frame is made and not just a copy of the pointer..
James Zern3e2613b2017-03-30 23:14:40 -0700591int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -0700592 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
593 int64_t end_time_stamp);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594
Yaowu Xuf883b422016-08-30 14:01:10 -0700595int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
596 size_t *size, uint8_t *dest, int64_t *time_stamp,
597 int64_t *time_end, int flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700598
Yaowu Xuf883b422016-08-30 14:01:10 -0700599int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700600
Yaowu Xuf883b422016-08-30 14:01:10 -0700601int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700602
Yaowu Xuf883b422016-08-30 14:01:10 -0700603int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604
Yaowu Xuf883b422016-08-30 14:01:10 -0700605void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606
Thomas Daede497d1952017-08-08 17:33:06 -0700607int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608
Thomas Daede497d1952017-08-08 17:33:06 -0700609int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610
Yaowu Xuf883b422016-08-30 14:01:10 -0700611int av1_update_entropy(AV1_COMP *cpi, int update);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700612
Yaowu Xuf883b422016-08-30 14:01:10 -0700613int av1_set_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700614
Yaowu Xuf883b422016-08-30 14:01:10 -0700615int av1_get_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700616
Yaowu Xuf883b422016-08-30 14:01:10 -0700617int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
618 AOM_SCALING vert_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619
Yaowu Xuf883b422016-08-30 14:01:10 -0700620int av1_get_quantizer(struct AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400622int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *input_size);
623
Yaowu Xuf883b422016-08-30 14:01:10 -0700624static INLINE int frame_is_kf_gf_arf(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625 return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
626 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
627}
628
Yaowu Xuf883b422016-08-30 14:01:10 -0700629static INLINE int get_ref_frame_map_idx(const AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630 MV_REFERENCE_FRAME ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -0700631 return cpi->ref_fb_idx[ref_frame - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632}
633
Urvang Joshi52648442016-10-13 17:27:51 -0700634static INLINE int get_ref_frame_buf_idx(const AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700635 MV_REFERENCE_FRAME ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700636 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
638 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX;
639}
640
Hui Su2d5fd742018-02-21 18:10:37 -0800641// TODO(huisu@google.com, youzhou@microsoft.com): enable hash-me for HBD.
642static INLINE int av1_use_hash_me(const AV1_COMMON *const cm) {
Debargha Mukherjee8112d2f2018-03-01 09:53:11 -0800643 return cm->allow_screen_content_tools;
Hui Su2d5fd742018-02-21 18:10:37 -0800644}
645
646static INLINE hash_table *av1_get_ref_frame_hash_map(
647 const AV1_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
RogerZhoucc5d35d2017-08-07 22:20:15 -0700648 const AV1_COMMON *const cm = &cpi->common;
649 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
650 return buf_idx != INVALID_IDX
651 ? &cm->buffer_pool->frame_bufs[buf_idx].hash_table
652 : NULL;
653}
RogerZhoucc5d35d2017-08-07 22:20:15 -0700654
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
Urvang Joshi52648442016-10-13 17:27:51 -0700656 const AV1_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
657 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
659 return buf_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[buf_idx].buf
660 : NULL;
661}
662
Yaowu Xuf883b422016-08-30 14:01:10 -0700663static INLINE int enc_is_ref_frame_buf(AV1_COMP *cpi, RefCntBuffer *frame_buf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -0700665 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700666 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
667 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
668 if (buf_idx == INVALID_IDX) continue;
669 if (frame_buf == &cm->buffer_pool->frame_bufs[buf_idx]) break;
670 }
671 return (ref_frame <= ALTREF_FRAME);
672}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673
Hui Su8ac0c982018-04-03 12:17:36 -0700674// Token buffer is only used for palette tokens.
Yaowu Xue39b3b82017-10-31 16:11:59 -0700675static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000676 int sb_size_log2,
677 const int num_planes) {
Rupert Swarbrickce63e832017-10-25 17:54:17 +0100678 // Calculate the maximum number of max superblocks in the image.
Yaowu Xue39b3b82017-10-31 16:11:59 -0700679 const int shift = sb_size_log2 - 4;
680 const int sb_size = 1 << sb_size_log2;
681 const int sb_size_square = sb_size * sb_size;
Rupert Swarbrickce63e832017-10-25 17:54:17 +0100682 const int sb_rows = ALIGN_POWER_OF_TWO(mb_rows, shift) >> shift;
683 const int sb_cols = ALIGN_POWER_OF_TWO(mb_cols, shift) >> shift;
684
Hui Su8ac0c982018-04-03 12:17:36 -0700685 // One palette token for each pixel. There can be palettes on two planes.
686 const int sb_palette_toks = AOMMIN(2, num_planes) * sb_size_square;
Rupert Swarbrickce63e832017-10-25 17:54:17 +0100687
Hui Su8ac0c982018-04-03 12:17:36 -0700688 return sb_rows * sb_cols * sb_palette_toks;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689}
690
691// Get the allocated token size for a tile. It does the same calculation as in
692// the frame token allocation.
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +0000693static INLINE unsigned int allocated_tokens(TileInfo tile, int sb_size_log2,
694 int num_planes) {
Jingning Haneafbd5f2017-03-07 11:18:17 -0800695 int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 2) >> 2;
696 int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 2) >> 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +0000698 return get_token_alloc(tile_mb_rows, tile_mb_cols, sb_size_log2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700699}
700
Yaowu Xuf883b422016-08-30 14:01:10 -0700701void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702
Yaowu Xu6da42302017-10-10 14:45:06 -0700703#define ALT_MIN_LAG 3
Yaowu Xuf883b422016-08-30 14:01:10 -0700704static INLINE int is_altref_enabled(const AV1_COMP *const cpi) {
Yaowu Xu6da42302017-10-10 14:45:06 -0700705 return cpi->oxcf.lag_in_frames >= ALT_MIN_LAG && cpi->oxcf.enable_auto_arf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706}
707
708// TODO(zoeliu): To set up cpi->oxcf.enable_auto_brf
Yaowu Xuc27fc142016-08-22 16:08:15 -0700709
Urvang Joshi52648442016-10-13 17:27:51 -0700710static INLINE void set_ref_ptrs(const AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700711 MV_REFERENCE_FRAME ref0,
712 MV_REFERENCE_FRAME ref1) {
713 xd->block_refs[0] =
714 &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME : 0];
715 xd->block_refs[1] =
716 &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME : 0];
717}
718
Yaowu Xu4ff59b52017-04-24 12:41:56 -0700719static INLINE int get_chessboard_index(int frame_index) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700720 return frame_index & 0x1;
721}
722
Yaowu Xuf883b422016-08-30 14:01:10 -0700723static INLINE int *cond_cost_list(const struct AV1_COMP *cpi, int *cost_list) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700724 return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
725}
726
Yaowu Xuf883b422016-08-30 14:01:10 -0700727void av1_new_framerate(AV1_COMP *cpi, double framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728
729#define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
730
731// Update up-sampled reference frame index.
732static INLINE void uref_cnt_fb(EncRefCntBuffer *ubufs, int *uidx,
733 int new_uidx) {
734 const int ref_index = *uidx;
735
736 if (ref_index >= 0 && ubufs[ref_index].ref_count > 0)
737 ubufs[ref_index].ref_count--;
738
739 *uidx = new_uidx;
740 ubufs[new_uidx].ref_count++;
741}
742
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -0700743// Returns 1 if a frame is unscaled and 0 otherwise.
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700744static INLINE int av1_resize_unscaled(const AV1_COMMON *cm) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700745 return cm->superres_upscaled_width == cm->render_width &&
746 cm->superres_upscaled_height == cm->render_height;
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -0700747}
748
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700749static INLINE int av1_frame_unscaled(const AV1_COMMON *cm) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700750 return av1_superres_unscaled(cm) && av1_resize_unscaled(cm);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -0700751}
752
Yaowu Xuc27fc142016-08-22 16:08:15 -0700753#ifdef __cplusplus
754} // extern "C"
755#endif
756
Yaowu Xuf883b422016-08-30 14:01:10 -0700757#endif // AV1_ENCODER_ENCODER_H_