• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #ifndef AOM_AV1_ENCODER_ENCODER_H_
13 #define AOM_AV1_ENCODER_ENCODER_H_
14 
15 #include <stdbool.h>
16 #include <stdio.h>
17 
18 #include "config/aom_config.h"
19 
20 #include "aom/aomcx.h"
21 
22 #include "av1/common/alloccommon.h"
23 #include "av1/common/av1_common_int.h"
24 #include "av1/common/blockd.h"
25 #include "av1/common/entropymode.h"
26 #include "av1/common/enums.h"
27 #include "av1/common/resize.h"
28 #include "av1/common/thread_common.h"
29 #include "av1/common/timing.h"
30 #include "av1/encoder/aq_cyclicrefresh.h"
31 #include "av1/encoder/av1_quantize.h"
32 #include "av1/encoder/block.h"
33 #include "av1/encoder/context_tree.h"
34 #include "av1/encoder/encodemb.h"
35 #include "av1/encoder/firstpass.h"
36 #include "av1/encoder/level.h"
37 #include "av1/encoder/lookahead.h"
38 #include "av1/encoder/mcomp.h"
39 #include "av1/encoder/ratectrl.h"
40 #include "av1/encoder/rd.h"
41 #include "av1/encoder/speed_features.h"
42 #include "av1/encoder/svc_layercontext.h"
43 #include "av1/encoder/tokenize.h"
44 
45 #if CONFIG_INTERNAL_STATS
46 #include "aom_dsp/ssim.h"
47 #endif
48 #include "aom_dsp/variance.h"
49 #if CONFIG_DENOISE
50 #include "aom_dsp/noise_model.h"
51 #endif
52 #include "aom/internal/aom_codec_internal.h"
53 #include "aom_util/aom_thread.h"
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 // Number of frames required to test for scene cut detection
60 #define SCENE_CUT_KEY_TEST_INTERVAL 16
61 
62 // Rational number with an int64 numerator
63 // This structure holds a fractional value
64 typedef struct aom_rational64 {
65   int64_t num;       // fraction numerator
66   int den;           // fraction denominator
67 } aom_rational64_t;  // alias for struct aom_rational
68 
69 typedef struct {
70 #if CONFIG_SUPERRES_IN_RECODE
71   struct loopfilter lf;
72   CdefInfo cdef_info;
73   YV12_BUFFER_CONFIG copy_buffer;
74   RATE_CONTROL rc;
75 #endif  // CONFIG_SUPERRES_IN_RECODE
76 } CODING_CONTEXT;
77 
78 enum {
79   NORMAL = 0,
80   FOURFIVE = 1,
81   THREEFIVE = 2,
82   ONETWO = 3
83 } UENUM1BYTE(AOM_SCALING);
84 
85 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.
88   GOOD,
89   // Realtime Fast Encoding. Will force some restrictions on bitrate
90   // constraints.
91   REALTIME
92 } UENUM1BYTE(MODE);
93 
94 enum {
95   FRAMEFLAGS_KEY = 1 << 0,
96   FRAMEFLAGS_GOLDEN = 1 << 1,
97   FRAMEFLAGS_BWDREF = 1 << 2,
98   // TODO(zoeliu): To determine whether a frame flag is needed for ALTREF2_FRAME
99   FRAMEFLAGS_ALTREF = 1 << 3,
100   FRAMEFLAGS_INTRAONLY = 1 << 4,
101   FRAMEFLAGS_SWITCH = 1 << 5,
102   FRAMEFLAGS_ERROR_RESILIENT = 1 << 6,
103 } UENUM1BYTE(FRAMETYPE_FLAGS);
104 
105 enum {
106   NO_AQ = 0,
107   VARIANCE_AQ = 1,
108   COMPLEXITY_AQ = 2,
109   CYCLIC_REFRESH_AQ = 3,
110   AQ_MODE_COUNT  // This should always be the last member of the enum
111 } UENUM1BYTE(AQ_MODE);
112 enum {
113   NO_DELTA_Q = 0,
114   DELTA_Q_OBJECTIVE = 1,   // Modulation to improve objective quality
115   DELTA_Q_PERCEPTUAL = 2,  // Modulation to improve perceptual quality
116   DELTA_Q_MODE_COUNT       // This should always be the last member of the enum
117 } UENUM1BYTE(DELTAQ_MODE);
118 
119 enum {
120   RESIZE_NONE = 0,    // No frame resizing allowed.
121   RESIZE_FIXED = 1,   // All frames are coded at the specified scale.
122   RESIZE_RANDOM = 2,  // All frames are coded at a random scale.
123   RESIZE_MODES
124 } UENUM1BYTE(RESIZE_MODE);
125 
126 enum {
127   SUPERRES_NONE,     // No frame superres allowed.
128   SUPERRES_FIXED,    // All frames are coded at the specified scale,
129                      // and super-resolved.
130   SUPERRES_RANDOM,   // All frames are coded at a random scale,
131                      // and super-resolved.
132   SUPERRES_QTHRESH,  // Superres scale for a frame is determined based on
133                      // q_index.
134   SUPERRES_AUTO,     // Automatically select superres for appropriate frames.
135   SUPERRES_MODES
136 } UENUM1BYTE(SUPERRES_MODE);
137 
138 typedef enum {
139   kInvalid = 0,
140   kLowSad = 1,
141   kHighSad = 2,
142   kLowVarHighSumdiff = 3,
143 } CONTENT_STATE_SB;
144 
145 enum {
146   SS_CFG_SRC = 0,
147   SS_CFG_LOOKAHEAD = 1,
148   SS_CFG_FPF = 2,
149   SS_CFG_TOTAL = 3
150 } UENUM1BYTE(SS_CFG_OFFSET);
151 
152 // TODO(jingning): This needs to be cleaned up next.
153 #define MAX_LENGTH_TPL_FRAME_STATS (MAX_TOTAL_BUFFERS + REF_FRAMES + 1)
154 
155 typedef struct TplDepStats {
156   int64_t intra_cost;
157   int64_t inter_cost;
158   int64_t srcrf_dist;
159   int64_t recrf_dist;
160   int64_t srcrf_rate;
161   int64_t recrf_rate;
162   int64_t mc_dep_rate;
163   int64_t mc_dep_dist;
164   int_mv mv[INTER_REFS_PER_FRAME];
165   int ref_frame_index;
166   int64_t pred_error[INTER_REFS_PER_FRAME];
167   int64_t mc_count;
168   int64_t mc_saved;
169 } TplDepStats;
170 
171 typedef struct TplDepFrame {
172   uint8_t is_valid;
173   TplDepStats *tpl_stats_ptr;
174   const YV12_BUFFER_CONFIG *gf_picture;
175   YV12_BUFFER_CONFIG *rec_picture;
176   int ref_map_index[REF_FRAMES];
177   int stride;
178   int width;
179   int height;
180   int mi_rows;
181   int mi_cols;
182   unsigned int frame_display_index;
183   int base_rdmult;
184 } TplDepFrame;
185 
186 typedef struct TplParams {
187   // Block granularity of tpl score storage.
188   uint8_t tpl_stats_block_mis_log2;
189 
190   // Buffer to store the frame level tpl information for each frame in a gf
191   // group. tpl_stats_buffer[i] stores the tpl information of ith frame in a gf
192   // group
193   TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS];
194 
195   // Buffer to store tpl stats at block granularity.
196   // tpl_stats_pool[i][j] stores the tpl stats of jth block of ith frame in a gf
197   // group.
198   TplDepStats *tpl_stats_pool[MAX_LAG_BUFFERS];
199 
200   // Buffer to store tpl reconstructed frame.
201   // tpl_rec_pool[i] stores the reconstructed frame of ith frame in a gf group.
202   YV12_BUFFER_CONFIG tpl_rec_pool[MAX_LAG_BUFFERS];
203 
204   // Pointer to tpl_stats_buffer.
205   TplDepFrame *tpl_frame;
206 } TplParams;
207 
208 typedef enum {
209   COST_UPD_SB,
210   COST_UPD_SBROW,
211   COST_UPD_TILE,
212   COST_UPD_OFF,
213 } COST_UPDATE_TYPE;
214 
215 #define TPL_DEP_COST_SCALE_LOG2 4
216 
217 typedef struct AV1EncoderConfig {
218   BITSTREAM_PROFILE profile;
219   aom_bit_depth_t bit_depth;     // Codec bit-depth.
220   int width;                     // width of data passed to the compressor
221   int height;                    // height of data passed to the compressor
222   int forced_max_frame_width;    // forced maximum width of frame (if != 0)
223   int forced_max_frame_height;   // forced maximum height of frame (if != 0)
224   unsigned int input_bit_depth;  // Input bit depth.
225   double init_framerate;         // set to passed in framerate
226   int64_t target_bandwidth;      // bandwidth to be used in bits per second
227 
228   int noise_sensitivity;  // pre processing blur: recommendation 0
229   int sharpness;          // sharpening output: recommendation 0:
230   int speed;
231   // maximum allowed bitrate for any intra frame in % of bitrate target.
232   unsigned int rc_max_intra_bitrate_pct;
233   // maximum allowed bitrate for any inter frame in % of bitrate target.
234   unsigned int rc_max_inter_bitrate_pct;
235   // percent of rate boost for golden frame in CBR mode.
236   unsigned int gf_cbr_boost_pct;
237 
238   MODE mode;
239   int pass;
240 
241   // Key Framing Operations
242   int auto_key;  // autodetect cut scenes and set the keyframes
243   int key_freq;  // maximum distance to key frame.
244   int sframe_dist;
245   int sframe_mode;
246   int sframe_enabled;
247   int lag_in_frames;  // how many frames lag before we start encoding
248   int fwd_kf_enabled;
249 
250   // ----------------------------------------------------------------
251   // DATARATE CONTROL OPTIONS
252 
253   // vbr, cbr, constrained quality or constant quality
254   enum aom_rc_mode rc_mode;
255 
256   // buffer targeting aggressiveness
257   int under_shoot_pct;
258   int over_shoot_pct;
259 
260   // buffering parameters
261   int64_t starting_buffer_level_ms;
262   int64_t optimal_buffer_level_ms;
263   int64_t maximum_buffer_size_ms;
264 
265   // Frame drop threshold.
266   int drop_frames_water_mark;
267 
268   // controlling quality
269   int fixed_q;
270   int worst_allowed_q;
271   int best_allowed_q;
272   int cq_level;
273   int enable_chroma_deltaq;
274   AQ_MODE aq_mode;  // Adaptive Quantization mode
275   DELTAQ_MODE deltaq_mode;
276   int deltalf_mode;
277   int enable_cdef;
278   int enable_restoration;
279   int force_video_mode;
280   int enable_obmc;
281   int disable_trellis_quant;
282   int using_qm;
283   int qm_y;
284   int qm_u;
285   int qm_v;
286   int qm_minlevel;
287   int qm_maxlevel;
288   unsigned int num_tile_groups;
289   unsigned int mtu;
290 
291   // Internal frame size scaling.
292   RESIZE_MODE resize_mode;
293   uint8_t resize_scale_denominator;
294   uint8_t resize_kf_scale_denominator;
295 
296   // Frame Super-Resolution size scaling.
297   SUPERRES_MODE superres_mode;
298   uint8_t superres_scale_denominator;
299   uint8_t superres_kf_scale_denominator;
300   int superres_qthresh;
301   int superres_kf_qthresh;
302 
303   // Enable feature to reduce the frame quantization every x frames.
304   int frame_periodic_boost;
305 
306   // two pass datarate control
307   int two_pass_vbrbias;  // two pass datarate control tweaks
308   int two_pass_vbrmin_section;
309   int two_pass_vbrmax_section;
310   // END DATARATE CONTROL OPTIONS
311   // ----------------------------------------------------------------
312 
313   int enable_auto_arf;
314   int enable_auto_brf;  // (b)ackward (r)ef (f)rame
315 
316   /* Bitfield defining the error resiliency features to enable.
317    * Can provide decodable frames after losses in previous
318    * frames and decodable partitions after losses in the same frame.
319    */
320   unsigned int error_resilient_mode;
321 
322   unsigned int s_frame_mode;
323 
324   /* Bitfield defining the parallel decoding mode where the
325    * decoding in successive frames may be conducted in parallel
326    * just by decoding the frame headers.
327    */
328   unsigned int frame_parallel_decoding_mode;
329 
330   unsigned int limit;
331 
332   int arnr_max_frames;
333   int arnr_strength;
334 
335   int min_gf_interval;
336   int max_gf_interval;
337   int gf_min_pyr_height;
338   int gf_max_pyr_height;
339 
340   int row_mt;
341   int tile_columns;
342   int tile_rows;
343   int tile_width_count;
344   int tile_height_count;
345   int tile_widths[MAX_TILE_COLS];
346   int tile_heights[MAX_TILE_ROWS];
347 
348   int enable_tpl_model;
349   int enable_keyframe_filtering;
350 
351   int max_threads;
352 
353   aom_fixed_buf_t two_pass_stats_in;
354 
355   aom_tune_metric tuning;
356   const char *vmaf_model_path;
357   aom_tune_content content;
358   int use_highbitdepth;
359   aom_color_primaries_t color_primaries;
360   aom_transfer_characteristics_t transfer_characteristics;
361   aom_matrix_coefficients_t matrix_coefficients;
362   aom_chroma_sample_position_t chroma_sample_position;
363   int color_range;
364   int render_width;
365   int render_height;
366   int timing_info_present;
367   aom_timing_info_t timing_info;
368   int decoder_model_info_present_flag;
369   int display_model_info_present_flag;
370   int buffer_removal_time_present;
371   aom_dec_model_info_t buffer_model;
372   int film_grain_test_vector;
373   const char *film_grain_table_filename;
374 
375   uint8_t cdf_update_mode;
376   aom_superblock_size_t superblock_size;
377   unsigned int large_scale_tile;
378   unsigned int single_tile_decoding;
379   uint8_t monochrome;
380   unsigned int full_still_picture_hdr;
381   int enable_dual_filter;
382   unsigned int motion_vector_unit_test;
383   unsigned int sb_multipass_unit_test;
384   unsigned int ext_tile_debug;
385   int enable_rect_partitions;
386   int enable_ab_partitions;
387   int enable_1to4_partitions;
388   int min_partition_size;
389   int max_partition_size;
390   int enable_intra_edge_filter;
391   int enable_tx64;
392   int enable_flip_idtx;
393   int enable_order_hint;
394   int enable_dist_wtd_comp;
395   int enable_ref_frame_mvs;
396   unsigned int max_reference_frames;
397   int enable_reduced_reference_set;
398   unsigned int allow_ref_frame_mvs;
399   int enable_masked_comp;
400   int enable_onesided_comp;
401   int enable_interintra_comp;
402   int enable_smooth_interintra;
403   int enable_diff_wtd_comp;
404   int enable_interinter_wedge;
405   int enable_interintra_wedge;
406   int enable_global_motion;
407   int enable_warped_motion;
408   int allow_warped_motion;
409   int enable_filter_intra;
410   int enable_smooth_intra;
411   int enable_paeth_intra;
412   int enable_cfl_intra;
413   int enable_superres;
414   int enable_overlay;
415   int enable_palette;
416   int enable_intrabc;
417   int enable_angle_delta;
418   unsigned int save_as_annexb;
419 
420 #if CONFIG_DENOISE
421   float noise_level;
422   int noise_block_size;
423 #endif
424 
425   unsigned int chroma_subsampling_x;
426   unsigned int chroma_subsampling_y;
427   int reduced_tx_type_set;
428   int use_intra_dct_only;
429   int use_inter_dct_only;
430   int use_intra_default_tx_only;
431   int quant_b_adapt;
432   COST_UPDATE_TYPE coeff_cost_upd_freq;
433   COST_UPDATE_TYPE mode_cost_upd_freq;
434   COST_UPDATE_TYPE mv_cost_upd_freq;
435   int border_in_pixels;
436   AV1_LEVEL target_seq_level_idx[MAX_NUM_OPERATING_POINTS];
437   // Bit mask to specify which tier each of the 32 possible operating points
438   // conforms to.
439   unsigned int tier_mask;
440   // If true, encoder will use fixed QP offsets, that are either:
441   // - Given by the user, and stored in 'fixed_qp_offsets' array, OR
442   // - Picked automatically from cq_level.
443   int use_fixed_qp_offsets;
444   // List of QP offsets for: keyframe, ALTREF, and 3 levels of internal ARFs.
445   // If any of these values are negative, fixed offsets are disabled.
446   // Uses internal q range.
447   double fixed_qp_offsets[FIXED_QP_OFFSET_COUNT];
448   // min_cr / 100 is the target minimum compression ratio for each frame.
449   unsigned int min_cr;
450   const cfg_options_t *encoder_cfg;
451 } AV1EncoderConfig;
452 
is_lossless_requested(const AV1EncoderConfig * cfg)453 static INLINE int is_lossless_requested(const AV1EncoderConfig *cfg) {
454   return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
455 }
456 
457 typedef struct {
458   // obmc_probs[i][j] is the probability of OBMC being the best motion mode for
459   // jth block size and ith frame update type, averaged over past frames. If
460   // obmc_probs[i][j] < thresh, then OBMC search is pruned.
461   int obmc_probs[FRAME_UPDATE_TYPES][BLOCK_SIZES_ALL];
462 
463   // warped_probs[i] is the probability of warped motion being the best motion
464   // mode for ith frame update type, averaged over past frames. If
465   // warped_probs[i] < thresh, then warped motion search is pruned.
466   int warped_probs[FRAME_UPDATE_TYPES];
467 
468   // tx_type_probs[i][j][k] is the probability of kth tx_type being the best
469   // for jth transform size and ith frame update type, averaged over past
470   // frames. If tx_type_probs[i][j][k] < thresh, then transform search for that
471   // type is pruned.
472   int tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES];
473 
474   // switchable_interp_probs[i][j][k] is the probability of kth interpolation
475   // filter being the best for jth filter context and ith frame update type,
476   // averaged over past frames. If switchable_interp_probs[i][j][k] < thresh,
477   // then interpolation filter search is pruned for that case.
478   int switchable_interp_probs[FRAME_UPDATE_TYPES][SWITCHABLE_FILTER_CONTEXTS]
479                              [SWITCHABLE_FILTERS];
480 } FrameProbInfo;
481 
482 typedef struct FRAME_COUNTS {
483 // Note: This structure should only contain 'unsigned int' fields, or
484 // aggregates built solely from 'unsigned int' fields/elements
485 #if CONFIG_ENTROPY_STATS
486   unsigned int kf_y_mode[KF_MODE_CONTEXTS][KF_MODE_CONTEXTS][INTRA_MODES];
487   unsigned int angle_delta[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
488   unsigned int y_mode[BLOCK_SIZE_GROUPS][INTRA_MODES];
489   unsigned int uv_mode[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
490   unsigned int cfl_sign[CFL_JOINT_SIGNS];
491   unsigned int cfl_alpha[CFL_ALPHA_CONTEXTS][CFL_ALPHABET_SIZE];
492   unsigned int palette_y_mode[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
493   unsigned int palette_uv_mode[PALETTE_UV_MODE_CONTEXTS][2];
494   unsigned int palette_y_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
495   unsigned int palette_uv_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
496   unsigned int palette_y_color_index[PALETTE_SIZES]
497                                     [PALETTE_COLOR_INDEX_CONTEXTS]
498                                     [PALETTE_COLORS];
499   unsigned int palette_uv_color_index[PALETTE_SIZES]
500                                      [PALETTE_COLOR_INDEX_CONTEXTS]
501                                      [PALETTE_COLORS];
502   unsigned int partition[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
503   unsigned int txb_skip[TOKEN_CDF_Q_CTXS][TX_SIZES][TXB_SKIP_CONTEXTS][2];
504   unsigned int eob_extra[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
505                         [EOB_COEF_CONTEXTS][2];
506   unsigned int dc_sign[PLANE_TYPES][DC_SIGN_CONTEXTS][2];
507   unsigned int coeff_lps[TX_SIZES][PLANE_TYPES][BR_CDF_SIZE - 1][LEVEL_CONTEXTS]
508                         [2];
509   unsigned int eob_flag[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS][2];
510   unsigned int eob_multi16[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][5];
511   unsigned int eob_multi32[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][6];
512   unsigned int eob_multi64[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][7];
513   unsigned int eob_multi128[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][8];
514   unsigned int eob_multi256[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][9];
515   unsigned int eob_multi512[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][10];
516   unsigned int eob_multi1024[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][11];
517   unsigned int coeff_lps_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
518                               [LEVEL_CONTEXTS][BR_CDF_SIZE];
519   unsigned int coeff_base_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
520                                [SIG_COEF_CONTEXTS][NUM_BASE_LEVELS + 2];
521   unsigned int coeff_base_eob_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
522                                    [SIG_COEF_CONTEXTS_EOB][NUM_BASE_LEVELS + 1];
523   unsigned int newmv_mode[NEWMV_MODE_CONTEXTS][2];
524   unsigned int zeromv_mode[GLOBALMV_MODE_CONTEXTS][2];
525   unsigned int refmv_mode[REFMV_MODE_CONTEXTS][2];
526   unsigned int drl_mode[DRL_MODE_CONTEXTS][2];
527   unsigned int inter_compound_mode[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
528   unsigned int wedge_idx[BLOCK_SIZES_ALL][16];
529   unsigned int interintra[BLOCK_SIZE_GROUPS][2];
530   unsigned int interintra_mode[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
531   unsigned int wedge_interintra[BLOCK_SIZES_ALL][2];
532   unsigned int compound_type[BLOCK_SIZES_ALL][MASKED_COMPOUND_TYPES];
533   unsigned int motion_mode[BLOCK_SIZES_ALL][MOTION_MODES];
534   unsigned int obmc[BLOCK_SIZES_ALL][2];
535   unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
536   unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
537   unsigned int comp_ref_type[COMP_REF_TYPE_CONTEXTS][2];
538   unsigned int uni_comp_ref[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1][2];
539   unsigned int single_ref[REF_CONTEXTS][SINGLE_REFS - 1][2];
540   unsigned int comp_ref[REF_CONTEXTS][FWD_REFS - 1][2];
541   unsigned int comp_bwdref[REF_CONTEXTS][BWD_REFS - 1][2];
542   unsigned int intrabc[2];
543 
544   unsigned int txfm_partition[TXFM_PARTITION_CONTEXTS][2];
545   unsigned int intra_tx_size[MAX_TX_CATS][TX_SIZE_CONTEXTS][MAX_TX_DEPTH + 1];
546   unsigned int skip_mode[SKIP_MODE_CONTEXTS][2];
547   unsigned int skip[SKIP_CONTEXTS][2];
548   unsigned int compound_index[COMP_INDEX_CONTEXTS][2];
549   unsigned int comp_group_idx[COMP_GROUP_IDX_CONTEXTS][2];
550   unsigned int delta_q[DELTA_Q_PROBS][2];
551   unsigned int delta_lf_multi[FRAME_LF_COUNT][DELTA_LF_PROBS][2];
552   unsigned int delta_lf[DELTA_LF_PROBS][2];
553 
554   unsigned int inter_ext_tx[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
555   unsigned int intra_ext_tx[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
556                            [TX_TYPES];
557   unsigned int filter_intra_mode[FILTER_INTRA_MODES];
558   unsigned int filter_intra[BLOCK_SIZES_ALL][2];
559   unsigned int switchable_restore[RESTORE_SWITCHABLE_TYPES];
560   unsigned int wiener_restore[2];
561   unsigned int sgrproj_restore[2];
562 #endif  // CONFIG_ENTROPY_STATS
563 
564   unsigned int switchable_interp[SWITCHABLE_FILTER_CONTEXTS]
565                                 [SWITCHABLE_FILTERS];
566 } FRAME_COUNTS;
567 
568 #define INTER_MODE_RD_DATA_OVERALL_SIZE 6400
569 
570 typedef struct {
571   int ready;
572   double a;
573   double b;
574   double dist_mean;
575   double ld_mean;
576   double sse_mean;
577   double sse_sse_mean;
578   double sse_ld_mean;
579   int num;
580   double dist_sum;
581   double ld_sum;
582   double sse_sum;
583   double sse_sse_sum;
584   double sse_ld_sum;
585 } InterModeRdModel;
586 
587 typedef struct {
588   int idx;
589   int64_t rd;
590 } RdIdxPair;
591 // TODO(angiebird): This is an estimated size. We still need to figure what is
592 // the maximum number of modes.
593 #define MAX_INTER_MODES 1024
594 typedef struct inter_modes_info {
595   int num;
596   MB_MODE_INFO mbmi_arr[MAX_INTER_MODES];
597   int mode_rate_arr[MAX_INTER_MODES];
598   int64_t sse_arr[MAX_INTER_MODES];
599   int64_t est_rd_arr[MAX_INTER_MODES];
600   RdIdxPair rd_idx_pair_arr[MAX_INTER_MODES];
601   RD_STATS rd_cost_arr[MAX_INTER_MODES];
602   RD_STATS rd_cost_y_arr[MAX_INTER_MODES];
603   RD_STATS rd_cost_uv_arr[MAX_INTER_MODES];
604 } InterModesInfo;
605 
606 // Encoder row synchronization
607 typedef struct AV1RowMTSyncData {
608 #if CONFIG_MULTITHREAD
609   pthread_mutex_t *mutex_;
610   pthread_cond_t *cond_;
611 #endif
612   // Allocate memory to store the sb/mb block index in each row.
613   int *cur_col;
614   int sync_range;
615   int rows;
616 } AV1RowMTSync;
617 
618 typedef struct AV1RowMTInfo {
619   int current_mi_row;
620   int num_threads_working;
621 } AV1RowMTInfo;
622 
623 typedef struct {
624   // TODO(kyslov): consider changing to 64bit
625 
626   // This struct is used for computing variance in choose_partitioning(), where
627   // the max number of samples within a superblock is 32x32 (with 4x4 avg).
628   // With 8bit bitdepth, uint32_t is enough for sum_square_error (2^8 * 2^8 * 32
629   // * 32 = 2^26). For high bitdepth we need to consider changing this to 64 bit
630   uint32_t sum_square_error;
631   int32_t sum_error;
632   int log2_count;
633   int variance;
634 } VPartVar;
635 
636 typedef struct {
637   VPartVar none;
638   VPartVar horz[2];
639   VPartVar vert[2];
640 } VPVariance;
641 
642 typedef struct {
643   VPVariance part_variances;
644   VPartVar split[4];
645 } VP4x4;
646 
647 typedef struct {
648   VPVariance part_variances;
649   VP4x4 split[4];
650 } VP8x8;
651 
652 typedef struct {
653   VPVariance part_variances;
654   VP8x8 split[4];
655 } VP16x16;
656 
657 typedef struct {
658   VPVariance part_variances;
659   VP16x16 split[4];
660 } VP32x32;
661 
662 typedef struct {
663   VPVariance part_variances;
664   VP32x32 split[4];
665 } VP64x64;
666 
667 typedef struct {
668   VPVariance part_variances;
669   VP64x64 *split;
670 } VP128x128;
671 
672 typedef struct {
673   // Thresholds for variance based partitioning. If block variance > threshold,
674   // then that block is forced to split.
675   // thresholds[0] - threshold for 128x128;
676   // thresholds[1] - threshold for 64x64;
677   // thresholds[2] - threshold for 32x32;
678   // thresholds[3] - threshold for 16x16;
679   // thresholds[4] - threshold for 8x8;
680   int64_t thresholds[5];
681 
682   // MinMax variance threshold for 8x8 sub blocks of a 16x16 block. If actual
683   // minmax > threshold_minmax, the 16x16 is forced to split.
684   int64_t threshold_minmax;
685 } VarBasedPartitionInfo;
686 
687 // TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
688 typedef struct TileDataEnc {
689   TileInfo tile_info;
690   CFL_CTX cfl;
691   DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
692   FRAME_CONTEXT *row_ctx;
693   uint8_t allow_update_cdf;
694   InterModeRdModel inter_mode_rd_models[BLOCK_SIZES_ALL];
695   AV1RowMTSync row_mt_sync;
696   AV1RowMTInfo row_mt_info;
697 } TileDataEnc;
698 
699 typedef struct {
700   TOKENEXTRA *start;
701   TOKENEXTRA *stop;
702   unsigned int count;
703 } TOKENLIST;
704 
705 typedef struct MultiThreadHandle {
706   int allocated_tile_rows;
707   int allocated_tile_cols;
708   int allocated_sb_rows;
709   int thread_id_to_tile_id[MAX_NUM_THREADS];  // Mapping of threads to tiles
710 } MultiThreadHandle;
711 
712 typedef struct RD_COUNTS {
713   int64_t comp_pred_diff[REFERENCE_MODES];
714   // Stores number of 4x4 blocks using global motion per reference frame.
715   int global_motion_used[REF_FRAMES];
716   int compound_ref_used_flag;
717   int skip_mode_used_flag;
718   int tx_type_used[TX_SIZES_ALL][TX_TYPES];
719   int obmc_used[BLOCK_SIZES_ALL][2];
720   int warped_used[2];
721 } RD_COUNTS;
722 
723 typedef struct ThreadData {
724   MACROBLOCK mb;
725   RD_COUNTS rd_counts;
726   FRAME_COUNTS *counts;
727   PC_TREE *pc_tree;
728   PC_TREE *pc_root;
729   tran_low_t *tree_coeff_buf[MAX_MB_PLANE];
730   tran_low_t *tree_qcoeff_buf[MAX_MB_PLANE];
731   tran_low_t *tree_dqcoeff_buf[MAX_MB_PLANE];
732   InterModesInfo *inter_modes_info;
733   uint32_t *hash_value_buffer[2][2];
734   int32_t *wsrc_buf;
735   int32_t *mask_buf;
736   uint8_t *above_pred_buf;
737   uint8_t *left_pred_buf;
738   PALETTE_BUFFER *palette_buffer;
739   CompoundTypeRdBuffers comp_rd_buffer;
740   CONV_BUF_TYPE *tmp_conv_dst;
741   uint8_t *tmp_obmc_bufs[2];
742   int intrabc_used;
743   int deltaq_used;
744   FRAME_CONTEXT *tctx;
745   MB_MODE_INFO_EXT *mbmi_ext;
746   VP64x64 *vt64x64;
747   int32_t num_64x64_blocks;
748 } ThreadData;
749 
750 struct EncWorkerData;
751 
752 typedef struct ActiveMap {
753   int enabled;
754   int update;
755   unsigned char *map;
756 } ActiveMap;
757 
758 typedef struct {
759   // cs_rate_array[i] is the fraction of blocks in a frame which either match
760   // with the collocated block or are smooth, where i is the rate_index.
761   double cs_rate_array[32];
762   // rate_index is used to index cs_rate_array.
763   int rate_index;
764   // rate_size is the total number of entries populated in cs_rate_array.
765   int rate_size;
766 } ForceIntegerMVInfo;
767 
768 #if CONFIG_INTERNAL_STATS
769 // types of stats
770 enum {
771   STAT_Y,
772   STAT_U,
773   STAT_V,
774   STAT_ALL,
775   NUM_STAT_TYPES  // This should always be the last member of the enum
776 } UENUM1BYTE(StatType);
777 
778 typedef struct IMAGE_STAT {
779   double stat[NUM_STAT_TYPES];
780   double worst;
781 } ImageStat;
782 #endif  // CONFIG_INTERNAL_STATS
783 
784 typedef struct {
785   int ref_count;
786   YV12_BUFFER_CONFIG buf;
787 } EncRefCntBuffer;
788 
789 typedef struct {
790   // Buffer to store mode information at mi_alloc_bsize (4x4 or 8x8) level for
791   // use in bitstream preparation. frame_base[mi_row * stride + mi_col] stores
792   // the mode information of block (mi_row,mi_col).
793   MB_MODE_INFO_EXT_FRAME *frame_base;
794   // Size of frame_base buffer.
795   int alloc_size;
796   // Stride of frame_base buffer.
797   int stride;
798 } MBMIExtFrameBufferInfo;
799 
800 #if CONFIG_COLLECT_PARTITION_STATS == 2
801 typedef struct PartitionStats {
802   int partition_decisions[6][EXT_PARTITION_TYPES];
803   int partition_attempts[6][EXT_PARTITION_TYPES];
804   int64_t partition_times[6][EXT_PARTITION_TYPES];
805 
806   int partition_redo;
807 } PartitionStats;
808 #endif
809 
810 #if CONFIG_COLLECT_COMPONENT_TIMING
811 #include "aom_ports/aom_timer.h"
812 // Adjust the following to add new components.
813 enum {
814   encode_frame_to_data_rate_time,
815   encode_with_recode_loop_time,
816   loop_filter_time,
817   cdef_time,
818   loop_restoration_time,
819   av1_pack_bitstream_final_time,
820   av1_encode_frame_time,
821   av1_compute_global_motion_time,
822   av1_setup_motion_field_time,
823   encode_sb_time,
824   rd_pick_partition_time,
825   rd_pick_sb_modes_time,
826   av1_rd_pick_intra_mode_sb_time,
827   av1_rd_pick_inter_mode_sb_time,
828   handle_intra_mode_time,
829   do_tx_search_time,
830   handle_newmv_time,
831   compound_type_rd_time,
832   interpolation_filter_search_time,
833   motion_mode_rd_time,
834   kTimingComponents,
835 } UENUM1BYTE(TIMING_COMPONENT);
836 
get_component_name(int index)837 static INLINE char const *get_component_name(int index) {
838   switch (index) {
839     case encode_frame_to_data_rate_time:
840       return "encode_frame_to_data_rate_time";
841     case encode_with_recode_loop_time: return "encode_with_recode_loop_time";
842     case loop_filter_time: return "loop_filter_time";
843     case cdef_time: return "cdef_time";
844     case loop_restoration_time: return "loop_restoration_time";
845     case av1_pack_bitstream_final_time: return "av1_pack_bitstream_final_time";
846     case av1_encode_frame_time: return "av1_encode_frame_time";
847     case av1_compute_global_motion_time:
848       return "av1_compute_global_motion_time";
849     case av1_setup_motion_field_time: return "av1_setup_motion_field_time";
850     case encode_sb_time: return "encode_sb_time";
851     case rd_pick_partition_time: return "rd_pick_partition_time";
852     case rd_pick_sb_modes_time: return "rd_pick_sb_modes_time";
853     case av1_rd_pick_intra_mode_sb_time:
854       return "av1_rd_pick_intra_mode_sb_time";
855     case av1_rd_pick_inter_mode_sb_time:
856       return "av1_rd_pick_inter_mode_sb_time";
857     case handle_intra_mode_time: return "handle_intra_mode_time";
858     case do_tx_search_time: return "do_tx_search_time";
859     case handle_newmv_time: return "handle_newmv_time";
860     case compound_type_rd_time: return "compound_type_rd_time";
861     case interpolation_filter_search_time:
862       return "interpolation_filter_search_time";
863     case motion_mode_rd_time: return "motion_mode_rd_time";
864     default: assert(0);
865   }
866   return "error";
867 }
868 #endif
869 
870 // The maximum number of internal ARFs except ALTREF_FRAME
871 #define MAX_INTERNAL_ARFS (REF_FRAMES - BWDREF_FRAME - 1)
872 
873 typedef struct {
874   // Array to store the cost for signalling each global motion model.
875   // gmtype_cost[i] stores the cost of signalling the ith Global Motion model.
876   int type_cost[TRANS_TYPES];
877 
878   // Array to store the cost for signalling a particular global motion model for
879   // each reference frame. gmparams_cost[i] stores the cost of signalling global
880   // motion for the ith reference frame.
881   int params_cost[REF_FRAMES];
882 
883   // Flag to indicate if global motion search needs to be rerun.
884   bool search_done;
885 } GlobalMotionInfo;
886 
887 typedef struct {
888   // Stores the default value of skip flag depending on chroma format
889   // Set as 1 for monochrome and 3 for other color formats
890   int default_interp_skip_flags;
891   // Filter mask to allow certain interp_filter type.
892   uint16_t interp_filter_search_mask;
893 } InterpSearchFlags;
894 
895 typedef struct {
896   // Largest MV component used in a frame.
897   // The value from the previous frame is used to set the full pixel search
898   // range for the current frame.
899   int max_mv_magnitude;
900   // Parameter indicating initial search window to be used in full-pixel search.
901   // Range [0, MAX_MVSEARCH_STEPS-2]. Lower value indicates larger window.
902   int mv_step_param;
903   // Pointer to sub-pixel search function.
904   // In encoder: av1_find_best_sub_pixel_tree
905   //             av1_find_best_sub_pixel_tree_pruned
906   //             av1_find_best_sub_pixel_tree_pruned_more
907   //             av1_find_best_sub_pixel_tree_pruned_evenmore
908   // In MV unit test: av1_return_max_sub_pixel_mv
909   //                  av1_return_min_sub_pixel_mv
910   fractional_mv_step_fp *find_fractional_mv_step;
911   // Search site configuration for full-pel MV search.
912   // ss_cfg[SS_CFG_SRC]: Used in tpl, rd/non-rd inter mode loop, simple motion
913   // search.
914   // ss_cfg[SS_CFG_LOOKAHEAD]: Used in intraBC, temporal filter
915   // ss_cfg[SS_CFG_FPF]: Used during first pass and lookahead
916   search_site_config ss_cfg[SS_CFG_TOTAL];
917 } MotionVectorSearchParams;
918 
919 typedef struct {
920   // When resize is triggered externally, the desired dimensions are stored in
921   // this struct until used in the next frame to be coded. These values are
922   // effective only for one frame and are reset after they are used.
923   int width;
924   int height;
925 } ResizePendingParams;
926 
927 typedef struct {
928   // Threshold of transform domain distortion
929   // Index 0: Default mode evaluation, Winner mode processing is not applicable
930   // (Eg : IntraBc).
931   // Index 1: Mode evaluation.
932   // Index 2: Winner mode evaluation.
933   // Index 1 and 2 are applicable when enable_winner_mode_for_use_tx_domain_dist
934   // speed feature is ON
935   unsigned int tx_domain_dist_threshold[MODE_EVAL_TYPES];
936 
937   // Factor to control R-D optimization of coeffs based on block
938   // mse.
939   // Index 0: Default mode evaluation, Winner mode processing is not applicable
940   // (Eg : IntraBc). Index 1: Mode evaluation.
941   // Index 2: Winner mode evaluation
942   // Index 1 and 2 are applicable when enable_winner_mode_for_coeff_opt speed
943   // feature is ON
944   unsigned int coeff_opt_dist_threshold[MODE_EVAL_TYPES];
945 
946   // Transform size to be used in transform search
947   // Index 0: Default mode evaluation, Winner mode processing is not applicable
948   // (Eg : IntraBc).
949   // Index 1: Mode evaluation. Index 2: Winner mode evaluation
950   // Index 1 and 2 are applicable when enable_winner_mode_for_tx_size_srch speed
951   // feature is ON
952   TX_SIZE_SEARCH_METHOD tx_size_search_methods[MODE_EVAL_TYPES];
953 
954   // Transform domain distortion levels
955   // Index 0: Default mode evaluation, Winner mode processing is not applicable
956   // (Eg : IntraBc).
957   // Index 1: Mode evaluation. Index 2: Winner mode evaluation
958   // Index 1 and 2 are applicable when enable_winner_mode_for_use_tx_domain_dist
959   // speed feature is ON
960   unsigned int use_transform_domain_distortion[MODE_EVAL_TYPES];
961 
962   // Predict transform skip levels to be used for default, mode and winner mode
963   // evaluation. Index 0: Default mode evaluation, Winner mode processing is not
964   // applicable. Index 1: Mode evaluation, Index 2: Winner mode evaluation
965   unsigned int predict_skip_level[MODE_EVAL_TYPES];
966 } WinnerModeParams;
967 
968 typedef struct {
969   // Bit mask to disable certain reference frame types.
970   int ref_frame_flags;
971 
972   // Flags to determine which reference buffers are refreshed by this frame.
973   // When set, the encoder will update the particular reference frame buffer
974   // with the contents of the current frame.
975   bool refresh_last_frame;
976   bool refresh_golden_frame;
977   bool refresh_bwd_ref_frame;
978   bool refresh_alt2_ref_frame;
979   bool refresh_alt_ref_frame;
980 
981   // Flag to indicate that updation of refresh frame flags from external
982   // interface is pending.
983   bool refresh_frame_flags_pending;
984 
985   // Flag to enable the updation of frame contexts at the end of a frame decode.
986   bool refresh_frame_context;
987 
988   // Flag to indicate that updation of refresh_frame_context from external
989   // interface is pending.
990   bool refresh_frame_context_pending;
991 
992   // Flag to enable temporal MV prediction.
993   bool use_ref_frame_mvs;
994 
995   // Flag to code the frame as error-resilient.
996   bool use_error_resilient;
997 
998   // Flag to code the frame as s-frame.
999   bool use_s_frame;
1000 
1001   // Flag to set the frame's primary_ref_frame to PRIMARY_REF_NONE.
1002   bool use_primary_ref_none;
1003 } ExternalFlags;
1004 
1005 typedef struct {
1006   int arf_stack[FRAME_BUFFERS];
1007   int arf_stack_size;
1008   int lst_stack[FRAME_BUFFERS];
1009   int lst_stack_size;
1010   int gld_stack[FRAME_BUFFERS];
1011   int gld_stack_size;
1012 } RefBufferStack;
1013 
1014 typedef struct {
1015   // Some misc info
1016   int high_prec;
1017   int q;
1018   int order;
1019 
1020   // MV counters
1021   int inter_count;
1022   int intra_count;
1023   int default_mvs;
1024   int mv_joint_count[4];
1025   int last_bit_zero;
1026   int last_bit_nonzero;
1027 
1028   // Keep track of the rates
1029   int total_mv_rate;
1030   int hp_total_mv_rate;
1031   int lp_total_mv_rate;
1032 
1033   // Texture info
1034   int horz_text;
1035   int vert_text;
1036   int diag_text;
1037 
1038   // Whether the current struct contains valid data
1039   int valid;
1040 } MV_STATS;
1041 
1042 typedef struct {
1043   int frame_width;
1044   int frame_height;
1045   int mi_rows;
1046   int mi_cols;
1047   int mb_rows;
1048   int mb_cols;
1049   int num_mbs;
1050   aom_bit_depth_t bit_depth;
1051   int subsampling_x;
1052   int subsampling_y;
1053 } FRAME_INFO;
1054 
1055 typedef struct {
1056   // 3-bit number containing the segment affiliation for each 4x4 block in the
1057   // frame. map[y * stride + x] contains the segment id of the 4x4 block at
1058   // (x,y) position.
1059   uint8_t *map;
1060   // Flag to indicate if current frame has lossless segments or not.
1061   // 1: frame has at least one lossless segment.
1062   // 0: frame has no lossless segments.
1063   bool has_lossless_segment;
1064 } EncSegmentationInfo;
1065 
1066 typedef struct {
1067   // Start time stamp of the previous frame
1068   int64_t prev_start_seen;
1069   // End time stamp of the previous frame
1070   int64_t prev_end_seen;
1071   // Start time stamp of the first frame
1072   int64_t first_ever;
1073 } TimeStamps;
1074 
1075 typedef struct AV1_COMP {
1076   // Quantization and dequantization parameters for internal quantizer setup
1077   // in the encoder.
1078   EncQuantDequantParams enc_quant_dequant_params;
1079   ThreadData td;
1080   FRAME_COUNTS counts;
1081 
1082   // Holds buffer storing mode information at 4x4/8x8 level.
1083   MBMIExtFrameBufferInfo mbmi_ext_info;
1084 
1085   CB_COEFF_BUFFER *coeff_buffer_base;
1086   AV1_COMMON common;
1087   AV1EncoderConfig oxcf;
1088   struct lookahead_ctx *lookahead;
1089   int no_show_kf;
1090 
1091   TRELLIS_OPT_TYPE optimize_seg_arr[MAX_SEGMENTS];
1092 
1093   YV12_BUFFER_CONFIG *source;
1094   YV12_BUFFER_CONFIG *last_source;  // NULL for first frame and alt_ref frames
1095   YV12_BUFFER_CONFIG *unscaled_source;
1096   YV12_BUFFER_CONFIG scaled_source;
1097   YV12_BUFFER_CONFIG *unscaled_last_source;
1098   YV12_BUFFER_CONFIG scaled_last_source;
1099   YV12_BUFFER_CONFIG *unfiltered_source;
1100 
1101   TplParams tpl_data;
1102 
1103   // For a still frame, this flag is set to 1 to skip partition search.
1104   int partition_search_skippable_frame;
1105 
1106   // Variables related to forcing integer mv decisions for the current frame.
1107   ForceIntegerMVInfo force_intpel_info;
1108 
1109   unsigned int row_mt;
1110   RefCntBuffer *scaled_ref_buf[INTER_REFS_PER_FRAME];
1111 
1112   RefCntBuffer *last_show_frame_buf;  // last show frame buffer
1113 
1114   // refresh_*_frame are boolean flags. If 'refresh_xyz_frame' is true, then
1115   // after the current frame is encoded, the XYZ reference frame gets refreshed
1116   // (updated) to be the current frame.
1117   //
1118   // Note: Usually at most one of these refresh flags is true at a time.
1119   // But a key-frame is special, for which all the flags are true at once.
1120   int refresh_golden_frame;
1121   int refresh_bwd_ref_frame;
1122   int refresh_alt_ref_frame;
1123 
1124   // For each type of reference frame, this contains the index of a reference
1125   // frame buffer for a reference frame of the same type.  We use this to
1126   // choose our primary reference frame (which is the most recent reference
1127   // frame of the same type as the current frame).
1128   int fb_of_context_type[REF_FRAMES];
1129 
1130   // Flags signalled by the external interface at frame level.
1131   ExternalFlags ext_flags;
1132 
1133   YV12_BUFFER_CONFIG last_frame_uf;
1134   YV12_BUFFER_CONFIG trial_frame_rst;
1135 
1136   // Ambient reconstruction err target for force key frames
1137   int64_t ambient_err;
1138 
1139   RD_OPT rd;
1140 
1141   CODING_CONTEXT coding_context;
1142 
1143   // Parameters related to global motion search.
1144   GlobalMotionInfo gm_info;
1145 
1146   // Parameters related to winner mode processing.
1147   WinnerModeParams winner_mode_params;
1148 
1149   // Frame time stamps
1150   TimeStamps time_stamps;
1151 
1152   RATE_CONTROL rc;
1153   double framerate;
1154 
1155   struct aom_codec_pkt_list *output_pkt_list;
1156 
1157   int ref_frame_flags;
1158 
1159   // speed is passed as a per-frame parameter into the encoder
1160   int speed;
1161   // sf contains fine-grained config set internally based on speed
1162   SPEED_FEATURES sf;
1163 
1164   // Parameters for motion vector search process.
1165   MotionVectorSearchParams mv_search_params;
1166 
1167   int all_one_sided_refs;
1168 
1169   // Segmentation related information for current frame.
1170   EncSegmentationInfo enc_seg;
1171 
1172   CYCLIC_REFRESH *cyclic_refresh;
1173   ActiveMap active_map;
1174 
1175   aom_variance_fn_ptr_t fn_ptr[BLOCK_SIZES_ALL];
1176 
1177 #if CONFIG_INTERNAL_STATS
1178   uint64_t time_receive_data;
1179   uint64_t time_compress_data;
1180 #endif
1181 
1182   // number of show frames encoded in current gf_group
1183   int num_gf_group_show_frames;
1184 
1185   TWO_PASS twopass;
1186 
1187   GF_GROUP gf_group;
1188 
1189   // To control the reference frame buffer and selection.
1190   RefBufferStack ref_buffer_stack;
1191 
1192   YV12_BUFFER_CONFIG alt_ref_buffer;
1193 
1194   // Tell if OVERLAY frame shows existing alt_ref frame.
1195   int show_existing_alt_ref;
1196 
1197 #if CONFIG_INTERNAL_STATS
1198   unsigned int mode_chosen_counts[MAX_MODES];
1199 
1200   int count;
1201   uint64_t total_sq_error;
1202   uint64_t total_samples;
1203   ImageStat psnr;
1204 
1205   double total_blockiness;
1206   double worst_blockiness;
1207 
1208   int bytes;
1209   double summed_quality;
1210   double summed_weights;
1211   unsigned int tot_recode_hits;
1212   double worst_ssim;
1213 
1214   ImageStat fastssim;
1215   ImageStat psnrhvs;
1216 
1217   int b_calculate_blockiness;
1218   int b_calculate_consistency;
1219 
1220   double total_inconsistency;
1221   double worst_consistency;
1222   Ssimv *ssim_vars;
1223   Metrics metrics;
1224 #endif
1225   int b_calculate_psnr;
1226 #if CONFIG_SPEED_STATS
1227   unsigned int tx_search_count;
1228 #endif  // CONFIG_SPEED_STATS
1229 
1230   int droppable;
1231 
1232   FRAME_INFO frame_info;
1233 
1234   int initial_width;
1235   int initial_height;
1236   int initial_mbs;  // Number of MBs in the full-size frame; to be used to
1237                     // normalize the firstpass stats. This will differ from the
1238                     // number of MBs in the current frame when the frame is
1239                     // scaled.
1240   // Resize related parameters
1241   ResizePendingParams resize_pending_params;
1242 
1243   TileDataEnc *tile_data;
1244   int allocated_tiles;  // Keep track of memory allocated for tiles.
1245 
1246   TOKENEXTRA *tile_tok[MAX_TILE_ROWS][MAX_TILE_COLS];
1247   TOKENLIST *tplist[MAX_TILE_ROWS][MAX_TILE_COLS];
1248 
1249   // Sequence parameters have been transmitted already and locked
1250   // or not. Once locked av1_change_config cannot change the seq
1251   // parameters.
1252   int seq_params_locked;
1253 
1254   // VARIANCE_AQ segment map refresh
1255   int vaq_refresh;
1256 
1257   // Thresholds for variance based partitioning.
1258   VarBasedPartitionInfo vbp_info;
1259 
1260   // Probabilities for pruning of various AV1 tools.
1261   FrameProbInfo frame_probs;
1262 
1263   // Multi-threading
1264   int num_workers;
1265   AVxWorker *workers;
1266   struct EncWorkerData *tile_thr_data;
1267   int existing_fb_idx_to_show;
1268   int internal_altref_allowed;
1269   // A flag to indicate if intrabc is ever used in current frame.
1270   int intrabc_used;
1271 
1272   // Tables to calculate IntraBC MV cost.
1273   IntraBCMVCosts dv_costs;
1274 
1275   // Mark which ref frames can be skipped for encoding current frame druing RDO.
1276   int prune_ref_frame_mask;
1277 
1278   AV1LfSync lf_row_sync;
1279   AV1LrSync lr_row_sync;
1280   AV1LrStruct lr_ctxt;
1281 
1282   aom_film_grain_table_t *film_grain_table;
1283 #if CONFIG_DENOISE
1284   struct aom_denoise_and_model_t *denoise_and_model;
1285 #endif
1286 
1287   // Flags related to interpolation filter search.
1288   InterpSearchFlags interp_search_flags;
1289 
1290   MultiThreadHandle multi_thread_ctxt;
1291   void (*row_mt_sync_read_ptr)(AV1RowMTSync *const, int, int);
1292   void (*row_mt_sync_write_ptr)(AV1RowMTSync *const, int, int, const int);
1293 #if CONFIG_MULTITHREAD
1294   pthread_mutex_t *row_mt_mutex_;
1295 #endif
1296   // Set if screen content is set or relevant tools are enabled
1297   int is_screen_content_type;
1298 #if CONFIG_COLLECT_PARTITION_STATS == 2
1299   PartitionStats partition_stats;
1300 #endif
1301 
1302 #if CONFIG_COLLECT_COMPONENT_TIMING
1303   // component_time[] are initialized to zero while encoder starts.
1304   uint64_t component_time[kTimingComponents];
1305   struct aom_usec_timer component_timer[kTimingComponents];
1306   // frame_component_time[] are initialized to zero at beginning of each frame.
1307   uint64_t frame_component_time[kTimingComponents];
1308 #endif
1309 
1310   // Parameters for AV1 bitstream levels.
1311   AV1LevelParams level_params;
1312 
1313   // whether any no-zero delta_q was actually used
1314   int deltaq_used;
1315 
1316   // Indicates the true relative distance of ref frame w.r.t. current frame
1317   int ref_relative_dist[INTER_REFS_PER_FRAME];
1318 
1319   // Indicate nearest references w.r.t. current frame in past and future
1320   int8_t nearest_past_ref;
1321   int8_t nearest_future_ref;
1322 
1323   // TODO(sdeng): consider merge the following arrays.
1324   double *tpl_rdmult_scaling_factors;
1325   double *tpl_sb_rdmult_scaling_factors;
1326   double *ssim_rdmult_scaling_factors;
1327 
1328 #if CONFIG_TUNE_VMAF
1329   double *vmaf_rdmult_scaling_factors;
1330   double last_frame_ysse;
1331   double last_frame_vmaf;
1332   double last_frame_unsharp_amount;
1333 #endif
1334 
1335   int use_svc;
1336   SVC svc;
1337 
1338   int lap_enabled;
1339   COMPRESSOR_STAGE compressor_stage;
1340 
1341   // Some motion vector stats from the last encoded frame to help us decide what
1342   // precision to use to encode the current frame.
1343   MV_STATS mv_stats;
1344 
1345   // Frame type of the last frame. May be used in some heuristics for speeding
1346   // up the encoding.
1347   FRAME_TYPE last_frame_type;
1348   int num_tg;
1349 
1350   // Super-resolution mode currently being used by the encoder.
1351   // This may / may not be same as user-supplied mode in oxcf->superres_mode
1352   // (when we are recoding to try multiple options for example).
1353   SUPERRES_MODE superres_mode;
1354 } AV1_COMP;
1355 
1356 typedef struct {
1357   YV12_BUFFER_CONFIG *source;
1358   YV12_BUFFER_CONFIG *last_source;
1359   int64_t ts_duration;
1360 } EncodeFrameInput;
1361 
1362 // EncodeFrameParams contains per-frame encoding parameters decided upon by
1363 // av1_encode_strategy() and passed down to av1_encode()
1364 struct EncodeFrameParams {
1365   int error_resilient_mode;
1366   FRAME_TYPE frame_type;
1367   int primary_ref_frame;
1368   int order_offset;
1369   int show_frame;
1370   int refresh_frame_flags;
1371 
1372   int show_existing_frame;
1373   int existing_fb_idx_to_show;
1374 
1375   // Bitmask of which reference buffers may be referenced by this frame
1376   int ref_frame_flags;
1377 
1378   // Reference buffer assignment for this frame.
1379   int remapped_ref_idx[REF_FRAMES];
1380 
1381   // Flags which determine which reference buffers are refreshed by this frame
1382   int refresh_golden_frame;
1383   int refresh_bwd_ref_frame;
1384   int refresh_alt_ref_frame;
1385 
1386   // Speed level to use for this frame: Bigger number means faster.
1387   int speed;
1388 };
1389 typedef struct EncodeFrameParams EncodeFrameParams;
1390 
1391 // EncodeFrameResults contains information about the result of encoding a
1392 // single frame
1393 typedef struct {
1394   size_t size;  // Size of resulting bitstream
1395 } EncodeFrameResults;
1396 
1397 // Must not be called more than once.
1398 void av1_initialize_enc(void);
1399 
1400 struct AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
1401                                        BufferPool *const pool,
1402                                        FIRSTPASS_STATS *frame_stats_buf,
1403                                        COMPRESSOR_STAGE stage,
1404                                        int num_lap_buffers,
1405                                        int lap_lag_in_frames,
1406                                        STATS_BUFFER_CTX *stats_buf_context);
1407 void av1_remove_compressor(AV1_COMP *cpi);
1408 
1409 void av1_change_config(AV1_COMP *cpi, const AV1EncoderConfig *oxcf);
1410 
1411 void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
1412                              int subsampling_x, int subsampling_y);
1413 
1414 // receive a frames worth of data. caller can assume that a copy of this
1415 // frame is made and not just a copy of the pointer..
1416 int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
1417                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
1418                           int64_t end_time_stamp);
1419 
1420 int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
1421                             size_t *size, uint8_t *dest, int64_t *time_stamp,
1422                             int64_t *time_end, int flush,
1423                             const aom_rational64_t *timebase);
1424 
1425 int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
1426                const EncodeFrameInput *const frame_input,
1427                const EncodeFrameParams *const frame_params,
1428                EncodeFrameResults *const frame_results);
1429 
1430 int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest);
1431 
1432 int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame);
1433 
1434 aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
1435                                        YV12_BUFFER_CONFIG *new_frame,
1436                                        YV12_BUFFER_CONFIG *sd);
1437 
1438 int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags);
1439 
1440 int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
1441 
1442 int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
1443 
1444 int av1_set_size_literal(AV1_COMP *cpi, int width, int height);
1445 
1446 void av1_set_frame_size(AV1_COMP *cpi, int width, int height);
1447 
1448 int av1_update_entropy(bool *ext_refresh_frame_context,
1449                        bool *ext_refresh_frame_context_pending, bool update);
1450 
1451 int av1_set_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
1452 
1453 int av1_get_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
1454 
1455 int av1_set_internal_size(AV1EncoderConfig *const oxcf,
1456                           ResizePendingParams *resize_pending_params,
1457                           AOM_SCALING horiz_mode, AOM_SCALING vert_mode);
1458 
1459 int av1_get_quantizer(struct AV1_COMP *cpi);
1460 
1461 int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *input_size);
1462 
1463 void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
1464                                         CompoundTypeRdBuffers *const bufs);
1465 void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs);
1466 
1467 // Set screen content options.
1468 // This function estimates whether to use screen content tools, by counting
1469 // the portion of blocks that have few luma colors.
1470 // Modifies:
1471 //   cpi->commom.allow_screen_content_tools
1472 //   cpi->common.allow_intrabc
1473 // However, the estimation is not accurate and may misclassify videos.
1474 // A slower but more accurate approach that determines whether to use screen
1475 // content tools is employed later. See determine_sc_tools_with_encoding().
1476 void av1_set_screen_content_options(const struct AV1_COMP *cpi,
1477                                     FeatureFlags *features);
1478 
1479 // TODO(jingning): Move these functions as primitive members for the new cpi
1480 // class.
stack_push(int * stack,int * stack_size,int item)1481 static INLINE void stack_push(int *stack, int *stack_size, int item) {
1482   for (int i = *stack_size - 1; i >= 0; --i) stack[i + 1] = stack[i];
1483   stack[0] = item;
1484   ++*stack_size;
1485 }
1486 
stack_pop(int * stack,int * stack_size)1487 static INLINE int stack_pop(int *stack, int *stack_size) {
1488   if (*stack_size <= 0) return -1;
1489 
1490   int item = stack[0];
1491   for (int i = 0; i < *stack_size; ++i) stack[i] = stack[i + 1];
1492   --*stack_size;
1493 
1494   return item;
1495 }
1496 
stack_pop_end(int * stack,int * stack_size)1497 static INLINE int stack_pop_end(int *stack, int *stack_size) {
1498   int item = stack[*stack_size - 1];
1499   stack[*stack_size - 1] = -1;
1500   --*stack_size;
1501 
1502   return item;
1503 }
1504 
stack_reset(int * stack,int * stack_size)1505 static INLINE void stack_reset(int *stack, int *stack_size) {
1506   for (int i = 0; i < *stack_size; ++i) stack[i] = INVALID_IDX;
1507   *stack_size = 0;
1508 }
1509 
1510 // av1 uses 10,000,000 ticks/second as time stamp
1511 #define TICKS_PER_SEC 10000000LL
1512 
1513 static INLINE int64_t
timebase_units_to_ticks(const aom_rational64_t * timestamp_ratio,int64_t n)1514 timebase_units_to_ticks(const aom_rational64_t *timestamp_ratio, int64_t n) {
1515   return n * timestamp_ratio->num / timestamp_ratio->den;
1516 }
1517 
1518 static INLINE int64_t
ticks_to_timebase_units(const aom_rational64_t * timestamp_ratio,int64_t n)1519 ticks_to_timebase_units(const aom_rational64_t *timestamp_ratio, int64_t n) {
1520   int64_t round = timestamp_ratio->num / 2;
1521   if (round > 0) --round;
1522   return (n * timestamp_ratio->den + round) / timestamp_ratio->num;
1523 }
1524 
frame_is_kf_gf_arf(const AV1_COMP * cpi)1525 static INLINE int frame_is_kf_gf_arf(const AV1_COMP *cpi) {
1526   const GF_GROUP *const gf_group = &cpi->gf_group;
1527   const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_group->index];
1528 
1529   return frame_is_intra_only(&cpi->common) || update_type == ARF_UPDATE ||
1530          update_type == GF_UPDATE;
1531 }
1532 
1533 // TODO(huisu@google.com, youzhou@microsoft.com): enable hash-me for HBD.
av1_use_hash_me(const AV1_COMP * const cpi)1534 static INLINE int av1_use_hash_me(const AV1_COMP *const cpi) {
1535   return (cpi->common.features.allow_screen_content_tools &&
1536           cpi->common.features.allow_intrabc &&
1537           frame_is_intra_only(&cpi->common));
1538 }
1539 
get_ref_frame_yv12_buf(const AV1_COMMON * const cm,MV_REFERENCE_FRAME ref_frame)1540 static INLINE const YV12_BUFFER_CONFIG *get_ref_frame_yv12_buf(
1541     const AV1_COMMON *const cm, MV_REFERENCE_FRAME ref_frame) {
1542   const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
1543   return buf != NULL ? &buf->buf : NULL;
1544 }
1545 
enc_is_ref_frame_buf(const AV1_COMMON * const cm,const RefCntBuffer * const frame_buf)1546 static INLINE int enc_is_ref_frame_buf(const AV1_COMMON *const cm,
1547                                        const RefCntBuffer *const frame_buf) {
1548   MV_REFERENCE_FRAME ref_frame;
1549   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1550     const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
1551     if (buf == NULL) continue;
1552     if (frame_buf == buf) break;
1553   }
1554   return (ref_frame <= ALTREF_FRAME);
1555 }
1556 
alloc_frame_mvs(AV1_COMMON * const cm,RefCntBuffer * buf)1557 static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, RefCntBuffer *buf) {
1558   assert(buf != NULL);
1559   ensure_mv_buffer(buf, cm);
1560   buf->width = cm->width;
1561   buf->height = cm->height;
1562 }
1563 
1564 // Token buffer is only used for palette tokens.
get_token_alloc(int mb_rows,int mb_cols,int sb_size_log2,const int num_planes)1565 static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols,
1566                                            int sb_size_log2,
1567                                            const int num_planes) {
1568   // Calculate the maximum number of max superblocks in the image.
1569   const int shift = sb_size_log2 - 4;
1570   const int sb_size = 1 << sb_size_log2;
1571   const int sb_size_square = sb_size * sb_size;
1572   const int sb_rows = ALIGN_POWER_OF_TWO(mb_rows, shift) >> shift;
1573   const int sb_cols = ALIGN_POWER_OF_TWO(mb_cols, shift) >> shift;
1574 
1575   // One palette token for each pixel. There can be palettes on two planes.
1576   const int sb_palette_toks = AOMMIN(2, num_planes) * sb_size_square;
1577 
1578   return sb_rows * sb_cols * sb_palette_toks;
1579 }
1580 
1581 // Get the allocated token size for a tile. It does the same calculation as in
1582 // the frame token allocation.
allocated_tokens(TileInfo tile,int sb_size_log2,int num_planes)1583 static INLINE unsigned int allocated_tokens(TileInfo tile, int sb_size_log2,
1584                                             int num_planes) {
1585   int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 2) >> 2;
1586   int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 2) >> 2;
1587 
1588   return get_token_alloc(tile_mb_rows, tile_mb_cols, sb_size_log2, num_planes);
1589 }
1590 
get_start_tok(AV1_COMP * cpi,int tile_row,int tile_col,int mi_row,TOKENEXTRA ** tok,int sb_size_log2,int num_planes)1591 static INLINE void get_start_tok(AV1_COMP *cpi, int tile_row, int tile_col,
1592                                  int mi_row, TOKENEXTRA **tok, int sb_size_log2,
1593                                  int num_planes) {
1594   AV1_COMMON *const cm = &cpi->common;
1595   const int tile_cols = cm->tiles.cols;
1596   TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
1597   const TileInfo *const tile_info = &this_tile->tile_info;
1598 
1599   const int tile_mb_cols =
1600       (tile_info->mi_col_end - tile_info->mi_col_start + 2) >> 2;
1601   const int tile_mb_row = (mi_row - tile_info->mi_row_start + 2) >> 2;
1602 
1603   *tok = cpi->tile_tok[tile_row][tile_col] +
1604          get_token_alloc(tile_mb_row, tile_mb_cols, sb_size_log2, num_planes);
1605 }
1606 
1607 void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags);
1608 
1609 #define ALT_MIN_LAG 3
is_altref_enabled(const AV1_COMP * const cpi)1610 static INLINE int is_altref_enabled(const AV1_COMP *const cpi) {
1611   return cpi->oxcf.lag_in_frames >= ALT_MIN_LAG && cpi->oxcf.enable_auto_arf;
1612 }
1613 
1614 // Check if statistics generation stage
is_stat_generation_stage(const AV1_COMP * const cpi)1615 static INLINE int is_stat_generation_stage(const AV1_COMP *const cpi) {
1616   assert(IMPLIES(cpi->compressor_stage == LAP_STAGE,
1617                  cpi->oxcf.pass == 0 && cpi->lap_enabled));
1618   return (cpi->oxcf.pass == 1 || (cpi->compressor_stage == LAP_STAGE));
1619 }
1620 // Check if statistics consumption stage
is_stat_consumption_stage_twopass(const AV1_COMP * const cpi)1621 static INLINE int is_stat_consumption_stage_twopass(const AV1_COMP *const cpi) {
1622   return (cpi->oxcf.pass == 2);
1623 }
1624 
1625 // Check if statistics consumption stage
is_stat_consumption_stage(const AV1_COMP * const cpi)1626 static INLINE int is_stat_consumption_stage(const AV1_COMP *const cpi) {
1627   return (is_stat_consumption_stage_twopass(cpi) ||
1628           (cpi->oxcf.pass == 0 && (cpi->compressor_stage == ENCODE_STAGE) &&
1629            cpi->lap_enabled));
1630 }
1631 
1632 // Check if the current stage has statistics
has_no_stats_stage(const AV1_COMP * const cpi)1633 static INLINE int has_no_stats_stage(const AV1_COMP *const cpi) {
1634   assert(IMPLIES(!cpi->lap_enabled, cpi->compressor_stage == ENCODE_STAGE));
1635   return (cpi->oxcf.pass == 0 && !cpi->lap_enabled);
1636 }
1637 
1638 // Function return size of frame stats buffer
get_stats_buf_size(int num_lap_buffer,int num_lag_buffer)1639 static INLINE int get_stats_buf_size(int num_lap_buffer, int num_lag_buffer) {
1640   /* if lookahead is enabled return num_lap_buffers else num_lag_buffers */
1641   return (num_lap_buffer > 0 ? num_lap_buffer + 1 : num_lag_buffer);
1642 }
1643 
1644 // TODO(zoeliu): To set up cpi->oxcf.enable_auto_brf
1645 
set_ref_ptrs(const AV1_COMMON * cm,MACROBLOCKD * xd,MV_REFERENCE_FRAME ref0,MV_REFERENCE_FRAME ref1)1646 static INLINE void set_ref_ptrs(const AV1_COMMON *cm, MACROBLOCKD *xd,
1647                                 MV_REFERENCE_FRAME ref0,
1648                                 MV_REFERENCE_FRAME ref1) {
1649   xd->block_ref_scale_factors[0] =
1650       get_ref_scale_factors_const(cm, ref0 >= LAST_FRAME ? ref0 : 1);
1651   xd->block_ref_scale_factors[1] =
1652       get_ref_scale_factors_const(cm, ref1 >= LAST_FRAME ? ref1 : 1);
1653 }
1654 
get_chessboard_index(int frame_index)1655 static INLINE int get_chessboard_index(int frame_index) {
1656   return frame_index & 0x1;
1657 }
1658 
cond_cost_list_const(const struct AV1_COMP * cpi,const int * cost_list)1659 static INLINE const int *cond_cost_list_const(const struct AV1_COMP *cpi,
1660                                               const int *cost_list) {
1661   const int use_cost_list = cpi->sf.mv_sf.subpel_search_method != SUBPEL_TREE &&
1662                             cpi->sf.mv_sf.use_fullpel_costlist;
1663   return use_cost_list ? cost_list : NULL;
1664 }
1665 
cond_cost_list(const struct AV1_COMP * cpi,int * cost_list)1666 static INLINE int *cond_cost_list(const struct AV1_COMP *cpi, int *cost_list) {
1667   const int use_cost_list = cpi->sf.mv_sf.subpel_search_method != SUBPEL_TREE &&
1668                             cpi->sf.mv_sf.use_fullpel_costlist;
1669   return use_cost_list ? cost_list : NULL;
1670 }
1671 
1672 // Compression ratio of current frame.
1673 double av1_get_compression_ratio(const AV1_COMMON *const cm,
1674                                  size_t encoded_frame_size);
1675 
1676 void av1_new_framerate(AV1_COMP *cpi, double framerate);
1677 
1678 void av1_setup_frame_size(AV1_COMP *cpi);
1679 
1680 #define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
1681 
1682 // Returns 1 if a frame is scaled and 0 otherwise.
av1_resize_scaled(const AV1_COMMON * cm)1683 static INLINE int av1_resize_scaled(const AV1_COMMON *cm) {
1684   return !(cm->superres_upscaled_width == cm->render_width &&
1685            cm->superres_upscaled_height == cm->render_height);
1686 }
1687 
av1_frame_scaled(const AV1_COMMON * cm)1688 static INLINE int av1_frame_scaled(const AV1_COMMON *cm) {
1689   return !av1_superres_scaled(cm) && av1_resize_scaled(cm);
1690 }
1691 
1692 // Don't allow a show_existing_frame to coincide with an error resilient
1693 // frame. An exception can be made for a forward keyframe since it has no
1694 // previous dependencies.
encode_show_existing_frame(const AV1_COMMON * cm)1695 static INLINE int encode_show_existing_frame(const AV1_COMMON *cm) {
1696   return cm->show_existing_frame && (!cm->features.error_resilient_mode ||
1697                                      cm->current_frame.frame_type == KEY_FRAME);
1698 }
1699 
1700 // Get index into the 'cpi->mbmi_ext_info.frame_base' array for the given
1701 // 'mi_row' and 'mi_col'.
get_mi_ext_idx(const int mi_row,const int mi_col,const BLOCK_SIZE mi_alloc_bsize,const int mbmi_ext_stride)1702 static INLINE int get_mi_ext_idx(const int mi_row, const int mi_col,
1703                                  const BLOCK_SIZE mi_alloc_bsize,
1704                                  const int mbmi_ext_stride) {
1705   const int mi_ext_size_1d = mi_size_wide[mi_alloc_bsize];
1706   const int mi_ext_row = mi_row / mi_ext_size_1d;
1707   const int mi_ext_col = mi_col / mi_ext_size_1d;
1708   return mi_ext_row * mbmi_ext_stride + mi_ext_col;
1709 }
1710 
1711 // Lighter version of set_offsets that only sets the mode info
1712 // pointers.
set_mode_info_offsets(const CommonModeInfoParams * const mi_params,const MBMIExtFrameBufferInfo * const mbmi_ext_info,MACROBLOCK * const x,MACROBLOCKD * const xd,int mi_row,int mi_col)1713 static INLINE void set_mode_info_offsets(
1714     const CommonModeInfoParams *const mi_params,
1715     const MBMIExtFrameBufferInfo *const mbmi_ext_info, MACROBLOCK *const x,
1716     MACROBLOCKD *const xd, int mi_row, int mi_col) {
1717   set_mi_offsets(mi_params, xd, mi_row, mi_col);
1718   const int ext_idx = get_mi_ext_idx(mi_row, mi_col, mi_params->mi_alloc_bsize,
1719                                      mbmi_ext_info->stride);
1720   x->mbmi_ext_frame = mbmi_ext_info->frame_base + ext_idx;
1721 }
1722 
1723 // Check to see if the given partition size is allowed for a specified number
1724 // of mi block rows and columns remaining in the image.
1725 // If not then return the largest allowed partition size
find_partition_size(BLOCK_SIZE bsize,int rows_left,int cols_left,int * bh,int * bw)1726 static INLINE BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize, int rows_left,
1727                                              int cols_left, int *bh, int *bw) {
1728   int int_size = (int)bsize;
1729   if (rows_left <= 0 || cols_left <= 0) {
1730     return AOMMIN(bsize, BLOCK_8X8);
1731   } else {
1732     for (; int_size > 0; int_size -= 3) {
1733       *bh = mi_size_high[int_size];
1734       *bw = mi_size_wide[int_size];
1735       if ((*bh <= rows_left) && (*bw <= cols_left)) {
1736         break;
1737       }
1738     }
1739   }
1740   return (BLOCK_SIZE)int_size;
1741 }
1742 
1743 static const uint8_t av1_ref_frame_flag_list[REF_FRAMES] = { 0,
1744                                                              AOM_LAST_FLAG,
1745                                                              AOM_LAST2_FLAG,
1746                                                              AOM_LAST3_FLAG,
1747                                                              AOM_GOLD_FLAG,
1748                                                              AOM_BWD_FLAG,
1749                                                              AOM_ALT2_FLAG,
1750                                                              AOM_ALT_FLAG };
1751 
1752 // When more than 'max_allowed_refs' are available, we reduce the number of
1753 // reference frames one at a time based on this order.
1754 static const MV_REFERENCE_FRAME disable_order[] = {
1755   LAST3_FRAME,
1756   LAST2_FRAME,
1757   ALTREF2_FRAME,
1758   GOLDEN_FRAME,
1759 };
1760 
get_max_allowed_ref_frames(const AV1_COMP * cpi)1761 static INLINE int get_max_allowed_ref_frames(const AV1_COMP *cpi) {
1762   const unsigned int max_allowed_refs_for_given_speed =
1763       (cpi->sf.inter_sf.selective_ref_frame >= 3) ? INTER_REFS_PER_FRAME - 1
1764                                                   : INTER_REFS_PER_FRAME;
1765   return AOMMIN(max_allowed_refs_for_given_speed,
1766                 cpi->oxcf.max_reference_frames);
1767 }
1768 
1769 static const MV_REFERENCE_FRAME
1770     ref_frame_priority_order[INTER_REFS_PER_FRAME] = {
1771       LAST_FRAME,    ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME,
1772       ALTREF2_FRAME, LAST2_FRAME,  LAST3_FRAME,
1773     };
1774 
get_ref_frame_flags(const SPEED_FEATURES * const sf,const YV12_BUFFER_CONFIG ** ref_frames,const int ext_ref_frame_flags)1775 static INLINE int get_ref_frame_flags(const SPEED_FEATURES *const sf,
1776                                       const YV12_BUFFER_CONFIG **ref_frames,
1777                                       const int ext_ref_frame_flags) {
1778   // cpi->ext_flags.ref_frame_flags allows certain reference types to be
1779   // disabled by the external interface.  These are set by
1780   // av1_apply_encoding_flags(). Start with what the external interface allows,
1781   // then suppress any reference types which we have found to be duplicates.
1782   int flags = ext_ref_frame_flags;
1783 
1784   for (int i = 1; i < INTER_REFS_PER_FRAME; ++i) {
1785     const YV12_BUFFER_CONFIG *const this_ref = ref_frames[i];
1786     // If this_ref has appeared before, mark the corresponding ref frame as
1787     // invalid. For nonrd mode, only disable GOLDEN_FRAME if it's the same
1788     // as LAST_FRAME or ALTREF_FRAME (if ALTREF is being used in nonrd).
1789     int index = (sf->rt_sf.use_nonrd_pick_mode &&
1790                  ref_frame_priority_order[i] == GOLDEN_FRAME)
1791                     ? (1 + sf->rt_sf.use_nonrd_altref_frame)
1792                     : i;
1793     for (int j = 0; j < index; ++j) {
1794       if (this_ref == ref_frames[j]) {
1795         flags &= ~(1 << (ref_frame_priority_order[i] - 1));
1796         break;
1797       }
1798     }
1799   }
1800   return flags;
1801 }
1802 
1803 // Enforce the number of references for each arbitrary frame based on user
1804 // options and speed.
enforce_max_ref_frames(AV1_COMP * cpi,int * ref_frame_flags)1805 static AOM_INLINE void enforce_max_ref_frames(AV1_COMP *cpi,
1806                                               int *ref_frame_flags) {
1807   MV_REFERENCE_FRAME ref_frame;
1808   int total_valid_refs = 0;
1809 
1810   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1811     if (*ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
1812       total_valid_refs++;
1813     }
1814   }
1815 
1816   const int max_allowed_refs = get_max_allowed_ref_frames(cpi);
1817 
1818   for (int i = 0; i < 4 && total_valid_refs > max_allowed_refs; ++i) {
1819     const MV_REFERENCE_FRAME ref_frame_to_disable = disable_order[i];
1820 
1821     if (!(*ref_frame_flags & av1_ref_frame_flag_list[ref_frame_to_disable])) {
1822       continue;
1823     }
1824 
1825     switch (ref_frame_to_disable) {
1826       case LAST3_FRAME: *ref_frame_flags &= ~AOM_LAST3_FLAG; break;
1827       case LAST2_FRAME: *ref_frame_flags &= ~AOM_LAST2_FLAG; break;
1828       case ALTREF2_FRAME: *ref_frame_flags &= ~AOM_ALT2_FLAG; break;
1829       case GOLDEN_FRAME: *ref_frame_flags &= ~AOM_GOLD_FLAG; break;
1830       default: assert(0);
1831     }
1832     --total_valid_refs;
1833   }
1834   assert(total_valid_refs <= max_allowed_refs);
1835 }
1836 
1837 // Returns a Sequence Header OBU stored in an aom_fixed_buf_t, or NULL upon
1838 // failure. When a non-NULL aom_fixed_buf_t pointer is returned by this
1839 // function, the memory must be freed by the caller. Both the buf member of the
1840 // aom_fixed_buf_t, and the aom_fixed_buf_t pointer itself must be freed. Memory
1841 // returned must be freed via call to free().
1842 //
1843 // Note: The OBU returned is in Low Overhead Bitstream Format. Specifically,
1844 // the obu_has_size_field bit is set, and the buffer contains the obu_size
1845 // field.
1846 aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi);
1847 
1848 #define MAX_GFUBOOST_FACTOR 10.0
1849 #define MIN_GFUBOOST_FACTOR 4.0
1850 double av1_get_gfu_boost_projection_factor(double min_factor, double max_factor,
1851                                            int frame_count);
1852 double av1_get_kf_boost_projection_factor(int frame_count);
1853 
1854 #define ENABLE_KF_TPL 1
1855 #define MAX_PYR_LEVEL_FROMTOP_DELTAQ 0
1856 
is_frame_kf_and_tpl_eligible(AV1_COMP * const cpi)1857 static INLINE int is_frame_kf_and_tpl_eligible(AV1_COMP *const cpi) {
1858   AV1_COMMON *cm = &cpi->common;
1859   return (cm->current_frame.frame_type == KEY_FRAME) && cm->show_frame &&
1860          (cpi->rc.frames_to_key > 1);
1861 }
1862 
is_frame_arf_and_tpl_eligible(const GF_GROUP * gf_group)1863 static INLINE int is_frame_arf_and_tpl_eligible(const GF_GROUP *gf_group) {
1864   const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_group->index];
1865   return update_type == ARF_UPDATE || update_type == GF_UPDATE;
1866 }
1867 
is_frame_tpl_eligible(AV1_COMP * const cpi)1868 static INLINE int is_frame_tpl_eligible(AV1_COMP *const cpi) {
1869 #if ENABLE_KF_TPL
1870   return is_frame_kf_and_tpl_eligible(cpi) ||
1871          is_frame_arf_and_tpl_eligible(&cpi->gf_group);
1872 #else
1873   return is_frame_arf_and_tpl_eligible(&cpi->gf_group);
1874 #endif  // ENABLE_KF_TPL
1875 }
1876 
1877 // Get update type of the current frame.
1878 static INLINE FRAME_UPDATE_TYPE
get_frame_update_type(const GF_GROUP * gf_group)1879 get_frame_update_type(const GF_GROUP *gf_group) {
1880   return gf_group->update_type[gf_group->index];
1881 }
1882 
av1_pixels_to_mi(int pixels)1883 static INLINE int av1_pixels_to_mi(int pixels) {
1884   return ALIGN_POWER_OF_TWO(pixels, 3) >> MI_SIZE_LOG2;
1885 }
1886 
1887 #if CONFIG_COLLECT_PARTITION_STATS == 2
av1_print_partition_stats(PartitionStats * part_stats)1888 static INLINE void av1_print_partition_stats(PartitionStats *part_stats) {
1889   FILE *f = fopen("partition_stats.csv", "w");
1890   if (!f) {
1891     return;
1892   }
1893 
1894   fprintf(f, "bsize,redo,");
1895   for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1896     fprintf(f, "decision_%d,", part);
1897   }
1898   for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1899     fprintf(f, "attempt_%d,", part);
1900   }
1901   for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1902     fprintf(f, "time_%d,", part);
1903   }
1904   fprintf(f, "\n");
1905 
1906   const int bsizes[6] = { 128, 64, 32, 16, 8, 4 };
1907 
1908   for (int bsize_idx = 0; bsize_idx < 6; bsize_idx++) {
1909     fprintf(f, "%d,%d,", bsizes[bsize_idx], part_stats->partition_redo);
1910     for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1911       fprintf(f, "%d,", part_stats->partition_decisions[bsize_idx][part]);
1912     }
1913     for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1914       fprintf(f, "%d,", part_stats->partition_attempts[bsize_idx][part]);
1915     }
1916     for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
1917       fprintf(f, "%ld,", part_stats->partition_times[bsize_idx][part]);
1918     }
1919     fprintf(f, "\n");
1920   }
1921   fclose(f);
1922 }
1923 
av1_get_bsize_idx_for_part_stats(BLOCK_SIZE bsize)1924 static INLINE int av1_get_bsize_idx_for_part_stats(BLOCK_SIZE bsize) {
1925   assert(bsize == BLOCK_128X128 || bsize == BLOCK_64X64 ||
1926          bsize == BLOCK_32X32 || bsize == BLOCK_16X16 || bsize == BLOCK_8X8 ||
1927          bsize == BLOCK_4X4);
1928   switch (bsize) {
1929     case BLOCK_128X128: return 0;
1930     case BLOCK_64X64: return 1;
1931     case BLOCK_32X32: return 2;
1932     case BLOCK_16X16: return 3;
1933     case BLOCK_8X8: return 4;
1934     case BLOCK_4X4: return 5;
1935     default: assert(0 && "Invalid bsize for partition_stats."); return -1;
1936   }
1937 }
1938 #endif
1939 
1940 #if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(AV1_COMP * cpi,int component)1941 static INLINE void start_timing(AV1_COMP *cpi, int component) {
1942   aom_usec_timer_start(&cpi->component_timer[component]);
1943 }
end_timing(AV1_COMP * cpi,int component)1944 static INLINE void end_timing(AV1_COMP *cpi, int component) {
1945   aom_usec_timer_mark(&cpi->component_timer[component]);
1946   cpi->frame_component_time[component] +=
1947       aom_usec_timer_elapsed(&cpi->component_timer[component]);
1948 }
get_frame_type_enum(int type)1949 static INLINE char const *get_frame_type_enum(int type) {
1950   switch (type) {
1951     case 0: return "KEY_FRAME";
1952     case 1: return "INTER_FRAME";
1953     case 2: return "INTRA_ONLY_FRAME";
1954     case 3: return "S_FRAME";
1955     default: assert(0);
1956   }
1957   return "error";
1958 }
1959 #endif
1960 
1961 #ifdef __cplusplus
1962 }  // extern "C"
1963 #endif
1964 
1965 #endif  // AOM_AV1_ENCODER_ENCODER_H_
1966