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 <stdio.h>
16 
17 #include "config/aom_config.h"
18 
19 #include "aom/aomcx.h"
20 
21 #include "av1/common/alloccommon.h"
22 #include "av1/common/entropymode.h"
23 #include "av1/common/thread_common.h"
24 #include "av1/common/onyxc_int.h"
25 #include "av1/common/resize.h"
26 #include "av1/common/timing.h"
27 #include "av1/encoder/aq_cyclicrefresh.h"
28 #include "av1/encoder/av1_quantize.h"
29 #include "av1/encoder/context_tree.h"
30 #include "av1/encoder/encodemb.h"
31 #include "av1/encoder/firstpass.h"
32 #include "av1/encoder/lookahead.h"
33 #include "av1/encoder/mbgraph.h"
34 #include "av1/encoder/mcomp.h"
35 #include "av1/encoder/ratectrl.h"
36 #include "av1/encoder/rd.h"
37 #include "av1/encoder/speed_features.h"
38 #include "av1/encoder/tokenize.h"
39 
40 #if CONFIG_INTERNAL_STATS
41 #include "aom_dsp/ssim.h"
42 #endif
43 #include "aom_dsp/variance.h"
44 #if CONFIG_DENOISE
45 #include "aom_dsp/noise_model.h"
46 #endif
47 #include "aom/internal/aom_codec_internal.h"
48 #include "aom_util/aom_thread.h"
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 typedef struct {
55   int nmv_vec_cost[MV_JOINTS];
56   int nmv_costs[2][MV_VALS];
57   int nmv_costs_hp[2][MV_VALS];
58 
59   FRAME_CONTEXT fc;
60 } CODING_CONTEXT;
61 
62 typedef enum {
63   // regular inter frame
64   REGULAR_FRAME = 0,
65   // alternate reference frame
66   ARF_FRAME = 1,
67   // overlay frame
68   OVERLAY_FRAME = 2,
69   // golden frame
70   GLD_FRAME = 3,
71   // backward reference frame
72   BRF_FRAME = 4,
73   // extra alternate reference frame
74   EXT_ARF_FRAME = 5,
75   FRAME_CONTEXT_INDEXES
76 } FRAME_CONTEXT_INDEX;
77 
78 typedef enum {
79   NORMAL = 0,
80   FOURFIVE = 1,
81   THREEFIVE = 2,
82   ONETWO = 3
83 } AOM_SCALING;
84 
85 typedef 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 } MODE;
90 
91 typedef enum {
92   FRAMEFLAGS_KEY = 1 << 0,
93   FRAMEFLAGS_GOLDEN = 1 << 1,
94   FRAMEFLAGS_BWDREF = 1 << 2,
95   // TODO(zoeliu): To determine whether a frame flag is needed for ALTREF2_FRAME
96   FRAMEFLAGS_ALTREF = 1 << 3,
97 } FRAMETYPE_FLAGS;
98 
99 typedef enum {
100   NO_AQ = 0,
101   VARIANCE_AQ = 1,
102   COMPLEXITY_AQ = 2,
103   CYCLIC_REFRESH_AQ = 3,
104   AQ_MODE_COUNT  // This should always be the last member of the enum
105 } AQ_MODE;
106 typedef enum {
107   NO_DELTA_Q = 0,
108   DELTA_Q_ONLY = 1,
109   DELTA_Q_LF = 2,
110   DELTAQ_MODE_COUNT  // This should always be the last member of the enum
111 } DELTAQ_MODE;
112 
113 typedef enum {
114   RESIZE_NONE = 0,    // No frame resizing allowed.
115   RESIZE_FIXED = 1,   // All frames are coded at the specified scale.
116   RESIZE_RANDOM = 2,  // All frames are coded at a random scale.
117   RESIZE_MODES
118 } RESIZE_MODE;
119 
120 typedef enum {
121   SUPERRES_NONE = 0,     // No frame superres allowed
122   SUPERRES_FIXED = 1,    // All frames are coded at the specified scale,
123                          // and super-resolved.
124   SUPERRES_RANDOM = 2,   // All frames are coded at a random scale,
125                          // and super-resolved.
126   SUPERRES_QTHRESH = 3,  // Superres scale for a frame is determined based on
127                          // q_index
128   SUPERRES_MODES
129 } SUPERRES_MODE;
130 
131 typedef struct AV1EncoderConfig {
132   BITSTREAM_PROFILE profile;
133   aom_bit_depth_t bit_depth;     // Codec bit-depth.
134   int width;                     // width of data passed to the compressor
135   int height;                    // height of data passed to the compressor
136   int forced_max_frame_width;    // forced maximum width of frame (if != 0)
137   int forced_max_frame_height;   // forced maximum height of frame (if != 0)
138   unsigned int input_bit_depth;  // Input bit depth.
139   double init_framerate;         // set to passed in framerate
140   int64_t target_bandwidth;      // bandwidth to be used in bits per second
141 
142   int noise_sensitivity;  // pre processing blur: recommendation 0
143   int sharpness;          // sharpening output: recommendation 0:
144   int speed;
145   // maximum allowed bitrate for any intra frame in % of bitrate target.
146   unsigned int rc_max_intra_bitrate_pct;
147   // maximum allowed bitrate for any inter frame in % of bitrate target.
148   unsigned int rc_max_inter_bitrate_pct;
149   // percent of rate boost for golden frame in CBR mode.
150   unsigned int gf_cbr_boost_pct;
151 
152   MODE mode;
153   int pass;
154 
155   // Key Framing Operations
156   int auto_key;  // autodetect cut scenes and set the keyframes
157   int key_freq;  // maximum distance to key frame.
158   int sframe_dist;
159   int sframe_mode;
160   int sframe_enabled;
161   int lag_in_frames;  // how many frames lag before we start encoding
162   int fwd_kf_enabled;
163 
164   // ----------------------------------------------------------------
165   // DATARATE CONTROL OPTIONS
166 
167   // vbr, cbr, constrained quality or constant quality
168   enum aom_rc_mode rc_mode;
169 
170   // buffer targeting aggressiveness
171   int under_shoot_pct;
172   int over_shoot_pct;
173 
174   // buffering parameters
175   int64_t starting_buffer_level_ms;
176   int64_t optimal_buffer_level_ms;
177   int64_t maximum_buffer_size_ms;
178 
179   // Frame drop threshold.
180   int drop_frames_water_mark;
181 
182   // controlling quality
183   int fixed_q;
184   int worst_allowed_q;
185   int best_allowed_q;
186   int cq_level;
187   AQ_MODE aq_mode;  // Adaptive Quantization mode
188   DELTAQ_MODE deltaq_mode;
189   int enable_cdef;
190   int enable_restoration;
191   int disable_trellis_quant;
192   int using_qm;
193   int qm_y;
194   int qm_u;
195   int qm_v;
196   int qm_minlevel;
197   int qm_maxlevel;
198 #if CONFIG_DIST_8X8
199   int using_dist_8x8;
200 #endif
201   unsigned int num_tile_groups;
202   unsigned int mtu;
203 
204   // Internal frame size scaling.
205   RESIZE_MODE resize_mode;
206   uint8_t resize_scale_denominator;
207   uint8_t resize_kf_scale_denominator;
208 
209   // Frame Super-Resolution size scaling.
210   SUPERRES_MODE superres_mode;
211   uint8_t superres_scale_denominator;
212   uint8_t superres_kf_scale_denominator;
213   int superres_qthresh;
214   int superres_kf_qthresh;
215 
216   // Enable feature to reduce the frame quantization every x frames.
217   int frame_periodic_boost;
218 
219   // two pass datarate control
220   int two_pass_vbrbias;  // two pass datarate control tweaks
221   int two_pass_vbrmin_section;
222   int two_pass_vbrmax_section;
223   // END DATARATE CONTROL OPTIONS
224   // ----------------------------------------------------------------
225 
226   int enable_auto_arf;
227   int enable_auto_brf;  // (b)ackward (r)ef (f)rame
228 
229   /* Bitfield defining the error resiliency features to enable.
230    * Can provide decodable frames after losses in previous
231    * frames and decodable partitions after losses in the same frame.
232    */
233   unsigned int error_resilient_mode;
234 
235   unsigned int s_frame_mode;
236 
237   /* Bitfield defining the parallel decoding mode where the
238    * decoding in successive frames may be conducted in parallel
239    * just by decoding the frame headers.
240    */
241   unsigned int frame_parallel_decoding_mode;
242 
243   unsigned int limit;
244 
245   int arnr_max_frames;
246   int arnr_strength;
247 
248   int min_gf_interval;
249   int max_gf_interval;
250 
251   int row_mt;
252   int tile_columns;
253   int tile_rows;
254   int tile_width_count;
255   int tile_height_count;
256   int tile_widths[MAX_TILE_COLS];
257   int tile_heights[MAX_TILE_ROWS];
258 
259   int max_threads;
260 
261   aom_fixed_buf_t two_pass_stats_in;
262   struct aom_codec_pkt_list *output_pkt_list;
263 
264 #if CONFIG_FP_MB_STATS
265   aom_fixed_buf_t firstpass_mb_stats_in;
266 #endif
267 
268   aom_tune_metric tuning;
269   aom_tune_content content;
270   int use_highbitdepth;
271   aom_color_primaries_t color_primaries;
272   aom_transfer_characteristics_t transfer_characteristics;
273   aom_matrix_coefficients_t matrix_coefficients;
274   aom_chroma_sample_position_t chroma_sample_position;
275   int color_range;
276   int render_width;
277   int render_height;
278   aom_timing_info_type_t timing_info_type;
279   int timing_info_present;
280   aom_timing_info_t timing_info;
281   int decoder_model_info_present_flag;
282   int display_model_info_present_flag;
283   int buffer_removal_time_present;
284   aom_dec_model_info_t buffer_model;
285   aom_dec_model_op_parameters_t op_params[MAX_NUM_OPERATING_POINTS + 1];
286   aom_op_timing_info_t op_frame_timing[MAX_NUM_OPERATING_POINTS + 1];
287   int film_grain_test_vector;
288   const char *film_grain_table_filename;
289 
290   uint8_t cdf_update_mode;
291   aom_superblock_size_t superblock_size;
292   unsigned int large_scale_tile;
293   unsigned int single_tile_decoding;
294   int monochrome;
295   unsigned int full_still_picture_hdr;
296   int enable_dual_filter;
297   unsigned int motion_vector_unit_test;
298   const cfg_options_t *cfg;
299   int enable_order_hint;
300   int enable_jnt_comp;
301   int enable_ref_frame_mvs;
302   unsigned int allow_ref_frame_mvs;
303   int enable_warped_motion;
304   int allow_warped_motion;
305   int enable_superres;
306   unsigned int save_as_annexb;
307 
308 #if CONFIG_DENOISE
309   float noise_level;
310   int noise_block_size;
311 #endif
312 
313   unsigned int chroma_subsampling_x;
314   unsigned int chroma_subsampling_y;
315 } AV1EncoderConfig;
316 
is_lossless_requested(const AV1EncoderConfig * cfg)317 static INLINE int is_lossless_requested(const AV1EncoderConfig *cfg) {
318   return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
319 }
320 
321 typedef struct FRAME_COUNTS {
322 // Note: This structure should only contain 'unsigned int' fields, or
323 // aggregates built solely from 'unsigned int' fields/elements
324 #if CONFIG_ENTROPY_STATS
325   unsigned int kf_y_mode[KF_MODE_CONTEXTS][KF_MODE_CONTEXTS][INTRA_MODES];
326   unsigned int angle_delta[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
327   unsigned int y_mode[BLOCK_SIZE_GROUPS][INTRA_MODES];
328   unsigned int uv_mode[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
329   unsigned int cfl_sign[CFL_JOINT_SIGNS];
330   unsigned int cfl_alpha[CFL_ALPHA_CONTEXTS][CFL_ALPHABET_SIZE];
331   unsigned int palette_y_mode[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
332   unsigned int palette_uv_mode[PALETTE_UV_MODE_CONTEXTS][2];
333   unsigned int palette_y_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
334   unsigned int palette_uv_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
335   unsigned int palette_y_color_index[PALETTE_SIZES]
336                                     [PALETTE_COLOR_INDEX_CONTEXTS]
337                                     [PALETTE_COLORS];
338   unsigned int palette_uv_color_index[PALETTE_SIZES]
339                                      [PALETTE_COLOR_INDEX_CONTEXTS]
340                                      [PALETTE_COLORS];
341   unsigned int partition[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
342   unsigned int txb_skip[TOKEN_CDF_Q_CTXS][TX_SIZES][TXB_SKIP_CONTEXTS][2];
343   unsigned int eob_extra[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
344                         [EOB_COEF_CONTEXTS][2];
345   unsigned int dc_sign[PLANE_TYPES][DC_SIGN_CONTEXTS][2];
346   unsigned int coeff_lps[TX_SIZES][PLANE_TYPES][BR_CDF_SIZE - 1][LEVEL_CONTEXTS]
347                         [2];
348   unsigned int eob_flag[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS][2];
349   unsigned int eob_multi16[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][5];
350   unsigned int eob_multi32[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][6];
351   unsigned int eob_multi64[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][7];
352   unsigned int eob_multi128[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][8];
353   unsigned int eob_multi256[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][9];
354   unsigned int eob_multi512[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][10];
355   unsigned int eob_multi1024[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][11];
356   unsigned int coeff_lps_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
357                               [LEVEL_CONTEXTS][BR_CDF_SIZE];
358   unsigned int coeff_base_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
359                                [SIG_COEF_CONTEXTS][NUM_BASE_LEVELS + 2];
360   unsigned int coeff_base_eob_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
361                                    [SIG_COEF_CONTEXTS_EOB][NUM_BASE_LEVELS + 1];
362   unsigned int newmv_mode[NEWMV_MODE_CONTEXTS][2];
363   unsigned int zeromv_mode[GLOBALMV_MODE_CONTEXTS][2];
364   unsigned int refmv_mode[REFMV_MODE_CONTEXTS][2];
365   unsigned int drl_mode[DRL_MODE_CONTEXTS][2];
366   unsigned int inter_compound_mode[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
367   unsigned int wedge_idx[BLOCK_SIZES_ALL][16];
368   unsigned int interintra[BLOCK_SIZE_GROUPS][2];
369   unsigned int interintra_mode[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
370   unsigned int wedge_interintra[BLOCK_SIZES_ALL][2];
371   unsigned int compound_type[BLOCK_SIZES_ALL][COMPOUND_TYPES - 1];
372   unsigned int motion_mode[BLOCK_SIZES_ALL][MOTION_MODES];
373   unsigned int obmc[BLOCK_SIZES_ALL][2];
374   unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
375   unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
376   unsigned int comp_ref_type[COMP_REF_TYPE_CONTEXTS][2];
377   unsigned int uni_comp_ref[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1][2];
378   unsigned int single_ref[REF_CONTEXTS][SINGLE_REFS - 1][2];
379   unsigned int comp_ref[REF_CONTEXTS][FWD_REFS - 1][2];
380   unsigned int comp_bwdref[REF_CONTEXTS][BWD_REFS - 1][2];
381   unsigned int intrabc[2];
382 
383   unsigned int txfm_partition[TXFM_PARTITION_CONTEXTS][2];
384   unsigned int intra_tx_size[MAX_TX_CATS][TX_SIZE_CONTEXTS][MAX_TX_DEPTH + 1];
385   unsigned int skip_mode[SKIP_MODE_CONTEXTS][2];
386   unsigned int skip[SKIP_CONTEXTS][2];
387   unsigned int compound_index[COMP_INDEX_CONTEXTS][2];
388   unsigned int comp_group_idx[COMP_GROUP_IDX_CONTEXTS][2];
389   unsigned int delta_q[DELTA_Q_PROBS][2];
390   unsigned int delta_lf_multi[FRAME_LF_COUNT][DELTA_LF_PROBS][2];
391   unsigned int delta_lf[DELTA_LF_PROBS][2];
392 
393   unsigned int inter_ext_tx[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
394   unsigned int intra_ext_tx[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
395                            [TX_TYPES];
396   unsigned int filter_intra_mode[FILTER_INTRA_MODES];
397   unsigned int filter_intra[BLOCK_SIZES_ALL][2];
398   unsigned int switchable_restore[RESTORE_SWITCHABLE_TYPES];
399   unsigned int wiener_restore[2];
400   unsigned int sgrproj_restore[2];
401 #endif  // CONFIG_ENTROPY_STATS
402 
403   unsigned int switchable_interp[SWITCHABLE_FILTER_CONTEXTS]
404                                 [SWITCHABLE_FILTERS];
405 } FRAME_COUNTS;
406 
407 #if CONFIG_COLLECT_INTER_MODE_RD_STATS
408 #define INTER_MODE_RD_DATA_OVERALL_SIZE 6400
409 
410 typedef struct {
411   int ready;
412   double a;
413   double b;
414   double dist_mean;
415   double ld_mean;
416   double sse_mean;
417   double sse_sse_mean;
418   double sse_ld_mean;
419   int num;
420   double dist_sum;
421   double ld_sum;
422   double sse_sum;
423   double sse_sse_sum;
424   double sse_ld_sum;
425 } InterModeRdModel;
426 
427 typedef struct {
428   int idx;
429   int64_t rd;
430 } RdIdxPair;
431 // TODO(angiebird): This is an estimated size. We still need to figure what is
432 // the maximum number of modes.
433 #define MAX_INTER_MODES 1024
434 typedef struct inter_modes_info {
435   int num;
436   MB_MODE_INFO mbmi_arr[MAX_INTER_MODES];
437   int mode_rate_arr[MAX_INTER_MODES];
438   int64_t sse_arr[MAX_INTER_MODES];
439   int64_t est_rd_arr[MAX_INTER_MODES];
440   RdIdxPair rd_idx_pair_arr[MAX_INTER_MODES];
441 } InterModesInfo;
442 #endif
443 
444 // TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
445 typedef struct TileDataEnc {
446   TileInfo tile_info;
447   int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
448   int mode_map[BLOCK_SIZES_ALL][MAX_MODES];
449   int m_search_count;
450   int ex_search_count;
451   CFL_CTX cfl;
452   DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
453   uint8_t allow_update_cdf;
454 #if CONFIG_COLLECT_INTER_MODE_RD_STATS
455   InterModeRdModel inter_mode_rd_models[BLOCK_SIZES_ALL];
456   InterModesInfo inter_modes_info;
457 #endif
458 } TileDataEnc;
459 
460 typedef struct {
461   TOKENEXTRA *start;
462   TOKENEXTRA *stop;
463   unsigned int count;
464 } TOKENLIST;
465 
466 typedef struct RD_COUNTS {
467   int64_t comp_pred_diff[REFERENCE_MODES];
468   // Stores number of 4x4 blocks using global motion per reference frame.
469   int global_motion_used[REF_FRAMES];
470   int compound_ref_used_flag;
471   int skip_mode_used_flag;
472 } RD_COUNTS;
473 
474 typedef struct ThreadData {
475   MACROBLOCK mb;
476   RD_COUNTS rd_counts;
477   FRAME_COUNTS *counts;
478   PC_TREE *pc_tree;
479   PC_TREE *pc_root[MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2 + 1];
480   uint32_t *hash_value_buffer[2][2];
481   int32_t *wsrc_buf;
482   int32_t *mask_buf;
483   uint8_t *above_pred_buf;
484   uint8_t *left_pred_buf;
485   PALETTE_BUFFER *palette_buffer;
486   CONV_BUF_TYPE *tmp_conv_dst;
487   uint8_t *tmp_obmc_bufs[2];
488   int intrabc_used_this_tile;
489 } ThreadData;
490 
491 struct EncWorkerData;
492 
493 typedef struct ActiveMap {
494   int enabled;
495   int update;
496   unsigned char *map;
497 } ActiveMap;
498 
499 #if CONFIG_INTERNAL_STATS
500 // types of stats
501 typedef enum {
502   STAT_Y,
503   STAT_U,
504   STAT_V,
505   STAT_ALL,
506   NUM_STAT_TYPES  // This should always be the last member of the enum
507 } StatType;
508 
509 typedef struct IMAGE_STAT {
510   double stat[NUM_STAT_TYPES];
511   double worst;
512 } ImageStat;
513 #endif  // CONFIG_INTERNAL_STATS
514 
515 typedef struct {
516   int ref_count;
517   YV12_BUFFER_CONFIG buf;
518 } EncRefCntBuffer;
519 
520 typedef struct TileBufferEnc {
521   uint8_t *data;
522   size_t size;
523 } TileBufferEnc;
524 
525 typedef struct AV1_COMP {
526   QUANTS quants;
527   ThreadData td;
528   FRAME_COUNTS counts;
529   MB_MODE_INFO_EXT *mbmi_ext_base;
530   CB_COEFF_BUFFER *coeff_buffer_base;
531   Dequants dequants;
532   AV1_COMMON common;
533   AV1EncoderConfig oxcf;
534   struct lookahead_ctx *lookahead;
535   struct lookahead_entry *alt_ref_source;
536   int no_show_kf;
537 
538   int optimize_speed_feature;
539   int optimize_seg_arr[MAX_SEGMENTS];
540 
541   YV12_BUFFER_CONFIG *source;
542   YV12_BUFFER_CONFIG *last_source;  // NULL for first frame and alt_ref frames
543   YV12_BUFFER_CONFIG *unscaled_source;
544   YV12_BUFFER_CONFIG scaled_source;
545   YV12_BUFFER_CONFIG *unscaled_last_source;
546   YV12_BUFFER_CONFIG scaled_last_source;
547 
548   // For a still frame, this flag is set to 1 to skip partition search.
549   int partition_search_skippable_frame;
550   double csm_rate_array[32];
551   double m_rate_array[32];
552   int rate_size;
553   int rate_index;
554   hash_table *previous_hash_table;
555   int previous_index;
556   int cur_poc;  // DebugInfo
557 
558   unsigned int row_mt;
559   int scaled_ref_idx[REF_FRAMES];
560   int ref_fb_idx[REF_FRAMES];
561   int refresh_fb_idx;  // ref frame buffer index to refresh
562 
563   int last_show_frame_buf_idx;  // last show frame buffer index
564 
565   int refresh_last_frame;
566   int refresh_golden_frame;
567   int refresh_bwd_ref_frame;
568   int refresh_alt2_ref_frame;
569   int refresh_alt_ref_frame;
570 #if USE_SYMM_MULTI_LAYER
571   int new_bwdref_update_rule;
572 #endif
573 
574   int ext_refresh_frame_flags_pending;
575   int ext_refresh_last_frame;
576   int ext_refresh_golden_frame;
577   int ext_refresh_bwd_ref_frame;
578   int ext_refresh_alt2_ref_frame;
579   int ext_refresh_alt_ref_frame;
580 
581   int ext_refresh_frame_context_pending;
582   int ext_refresh_frame_context;
583   int ext_use_ref_frame_mvs;
584   int ext_use_error_resilient;
585   int ext_use_s_frame;
586   int ext_use_primary_ref_none;
587 
588   YV12_BUFFER_CONFIG last_frame_uf;
589   YV12_BUFFER_CONFIG trial_frame_rst;
590 
591   // Ambient reconstruction err target for force key frames
592   int64_t ambient_err;
593 
594   RD_OPT rd;
595 
596   CODING_CONTEXT coding_context;
597 
598   int gmtype_cost[TRANS_TYPES];
599   int gmparams_cost[REF_FRAMES];
600 
601   int nmv_costs[2][MV_VALS];
602   int nmv_costs_hp[2][MV_VALS];
603 
604   int64_t last_time_stamp_seen;
605   int64_t last_end_time_stamp_seen;
606   int64_t first_time_stamp_ever;
607 
608   RATE_CONTROL rc;
609   double framerate;
610 
611   // NOTE(zoeliu): Any inter frame allows maximum of REF_FRAMES inter
612   // references; Plus the currently coded frame itself, it is needed to allocate
613   // sufficient space to the size of the maximum possible number of frames.
614   int interp_filter_selected[REF_FRAMES + 1][SWITCHABLE];
615 
616   struct aom_codec_pkt_list *output_pkt_list;
617 
618   MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
619   int mbgraph_n_frames;  // number of frames filled in the above
620   int static_mb_pct;     // % forced skip mbs by segmentation
621   int ref_frame_flags;
622   int ext_ref_frame_flags;
623   RATE_FACTOR_LEVEL frame_rf_level[FRAME_BUFFERS];
624 
625   SPEED_FEATURES sf;
626 
627   unsigned int max_mv_magnitude;
628   int mv_step_param;
629 
630   int allow_comp_inter_inter;
631   int all_one_sided_refs;
632 
633   uint8_t *segmentation_map;
634 
635   CYCLIC_REFRESH *cyclic_refresh;
636   ActiveMap active_map;
637 
638   fractional_mv_step_fp *find_fractional_mv_step;
639   av1_diamond_search_fn_t diamond_search_sad;
640   aom_variance_fn_ptr_t fn_ptr[BLOCK_SIZES_ALL];
641   uint64_t time_receive_data;
642   uint64_t time_compress_data;
643   uint64_t time_pick_lpf;
644   uint64_t time_encode_sb_row;
645 
646 #if CONFIG_FP_MB_STATS
647   int use_fp_mb_stats;
648 #endif
649 
650   TWO_PASS twopass;
651 
652   YV12_BUFFER_CONFIG alt_ref_buffer;
653 
654 #if CONFIG_INTERNAL_STATS
655   unsigned int mode_chosen_counts[MAX_MODES];
656 
657   int count;
658   uint64_t total_sq_error;
659   uint64_t total_samples;
660   ImageStat psnr;
661 
662   double total_blockiness;
663   double worst_blockiness;
664 
665   int bytes;
666   double summed_quality;
667   double summed_weights;
668   unsigned int tot_recode_hits;
669   double worst_ssim;
670 
671   ImageStat fastssim;
672   ImageStat psnrhvs;
673 
674   int b_calculate_blockiness;
675   int b_calculate_consistency;
676 
677   double total_inconsistency;
678   double worst_consistency;
679   Ssimv *ssim_vars;
680   Metrics metrics;
681 #endif
682   int b_calculate_psnr;
683 
684   int droppable;
685 
686   int initial_width;
687   int initial_height;
688   int initial_mbs;  // Number of MBs in the full-size frame; to be used to
689                     // normalize the firstpass stats. This will differ from the
690                     // number of MBs in the current frame when the frame is
691                     // scaled.
692 
693   // When resize is triggered through external control, the desired width/height
694   // are stored here until use in the next frame coded. They are effective only
695   // for
696   // one frame and are reset after use.
697   int resize_pending_width;
698   int resize_pending_height;
699 
700   int frame_flags;
701 
702   search_site_config ss_cfg;
703 
704   TileDataEnc *tile_data;
705   int allocated_tiles;  // Keep track of memory allocated for tiles.
706 
707   TOKENEXTRA *tile_tok[MAX_TILE_ROWS][MAX_TILE_COLS];
708   unsigned int tok_count[MAX_TILE_ROWS][MAX_TILE_COLS];
709   TOKENLIST *tplist[MAX_TILE_ROWS][MAX_TILE_COLS];
710 
711   TileBufferEnc tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
712 
713   int resize_state;
714   int resize_avg_qp;
715   int resize_buffer_underflow;
716   int resize_count;
717 
718   // Sequence parameters have been transmitted already and locked
719   // or not. Once locked av1_change_config cannot change the seq
720   // parameters.
721   int seq_params_locked;
722 
723   // VARIANCE_AQ segment map refresh
724   int vaq_refresh;
725 
726   // Multi-threading
727   int num_workers;
728   AVxWorker *workers;
729   struct EncWorkerData *tile_thr_data;
730   int refresh_frame_mask;
731   int existing_fb_idx_to_show;
732   int is_arf_filter_off[MAX_EXT_ARFS + 1];
733   int num_extra_arfs;
734   int arf_pos_in_gf[MAX_EXT_ARFS + 1];
735   int arf_pos_for_ovrly[MAX_EXT_ARFS + 1];
736   int global_motion_search_done;
737   tran_low_t *tcoeff_buf[MAX_MB_PLANE];
738   int extra_arf_allowed;
739   // A flag to indicate if intrabc is ever used in current frame.
740   int intrabc_used;
741   int dv_cost[2][MV_VALS];
742   // TODO(huisu@google.com): we can update dv_joint_cost per SB.
743   int dv_joint_cost[MV_JOINTS];
744   int has_lossless_segment;
745 
746   // For frame refs short signaling:
747   //   A mapping of each reference frame from its encoder side value to the
748   //   decoder side value obtained following the short signaling procedure.
749   int ref_conv[REF_FRAMES];
750 
751   AV1LfSync lf_row_sync;
752   AV1LrSync lr_row_sync;
753   AV1LrStruct lr_ctxt;
754 
755   aom_film_grain_table_t *film_grain_table;
756 #if CONFIG_DENOISE
757   struct aom_denoise_and_model_t *denoise_and_model;
758 #endif
759   // Stores the default value of skip flag depending on chroma format
760   // Set as 1 for monochrome and 3 for other color formats
761   int default_interp_skip_flags;
762   int preserve_arf_as_gld;
763 } AV1_COMP;
764 
765 // Must not be called more than once.
766 void av1_initialize_enc(void);
767 
768 struct AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
769                                        BufferPool *const pool);
770 void av1_remove_compressor(AV1_COMP *cpi);
771 
772 void av1_change_config(AV1_COMP *cpi, const AV1EncoderConfig *oxcf);
773 
774 // receive a frames worth of data. caller can assume that a copy of this
775 // frame is made and not just a copy of the pointer..
776 int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
777                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
778                           int64_t end_time_stamp);
779 
780 int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
781                             size_t *size, uint8_t *dest, int64_t *time_stamp,
782                             int64_t *time_end, int flush,
783                             const aom_rational_t *timebase);
784 
785 int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest);
786 
787 int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame);
788 
789 aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
790                                        YV12_BUFFER_CONFIG *new_frame,
791                                        YV12_BUFFER_CONFIG *sd);
792 
793 int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags);
794 
795 void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags);
796 
797 int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
798 
799 int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
800 
801 int av1_update_entropy(AV1_COMP *cpi, int update);
802 
803 int av1_set_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
804 
805 int av1_get_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
806 
807 int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
808                           AOM_SCALING vert_mode);
809 
810 int av1_get_quantizer(struct AV1_COMP *cpi);
811 
812 int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *input_size);
813 
814 int64_t timebase_units_to_ticks(const aom_rational_t *timebase, int64_t n);
815 int64_t ticks_to_timebase_units(const aom_rational_t *timebase, int64_t n);
816 
frame_is_kf_gf_arf(const AV1_COMP * cpi)817 static INLINE int frame_is_kf_gf_arf(const AV1_COMP *cpi) {
818   return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
819          (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
820 }
821 
get_ref_frame_map_idx(const AV1_COMP * cpi,MV_REFERENCE_FRAME ref_frame)822 static INLINE int get_ref_frame_map_idx(const AV1_COMP *cpi,
823                                         MV_REFERENCE_FRAME ref_frame) {
824   return (ref_frame >= 1) ? cpi->ref_fb_idx[ref_frame - 1] : INVALID_IDX;
825 }
826 
get_ref_frame_buf_idx(const AV1_COMP * cpi,MV_REFERENCE_FRAME ref_frame)827 static INLINE int get_ref_frame_buf_idx(const AV1_COMP *cpi,
828                                         MV_REFERENCE_FRAME ref_frame) {
829   const AV1_COMMON *const cm = &cpi->common;
830   const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
831   return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX;
832 }
833 
834 // TODO(huisu@google.com, youzhou@microsoft.com): enable hash-me for HBD.
av1_use_hash_me(const AV1_COMMON * const cm)835 static INLINE int av1_use_hash_me(const AV1_COMMON *const cm) {
836   return cm->allow_screen_content_tools;
837 }
838 
av1_get_ref_frame_hash_map(const AV1_COMP * cpi,MV_REFERENCE_FRAME ref_frame)839 static INLINE hash_table *av1_get_ref_frame_hash_map(
840     const AV1_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
841   const AV1_COMMON *const cm = &cpi->common;
842   const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
843   return buf_idx != INVALID_IDX
844              ? &cm->buffer_pool->frame_bufs[buf_idx].hash_table
845              : NULL;
846 }
847 
get_ref_frame_buffer(const AV1_COMP * cpi,MV_REFERENCE_FRAME ref_frame)848 static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
849     const AV1_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
850   const AV1_COMMON *const cm = &cpi->common;
851   const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
852   return buf_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[buf_idx].buf
853                                 : NULL;
854 }
855 
enc_is_ref_frame_buf(AV1_COMP * cpi,RefCntBuffer * frame_buf)856 static INLINE int enc_is_ref_frame_buf(AV1_COMP *cpi, RefCntBuffer *frame_buf) {
857   MV_REFERENCE_FRAME ref_frame;
858   AV1_COMMON *const cm = &cpi->common;
859   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
860     const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
861     if (buf_idx == INVALID_IDX) continue;
862     if (frame_buf == &cm->buffer_pool->frame_bufs[buf_idx]) break;
863   }
864   return (ref_frame <= ALTREF_FRAME);
865 }
866 
867 // 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)868 static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols,
869                                            int sb_size_log2,
870                                            const int num_planes) {
871   // Calculate the maximum number of max superblocks in the image.
872   const int shift = sb_size_log2 - 4;
873   const int sb_size = 1 << sb_size_log2;
874   const int sb_size_square = sb_size * sb_size;
875   const int sb_rows = ALIGN_POWER_OF_TWO(mb_rows, shift) >> shift;
876   const int sb_cols = ALIGN_POWER_OF_TWO(mb_cols, shift) >> shift;
877 
878   // One palette token for each pixel. There can be palettes on two planes.
879   const int sb_palette_toks = AOMMIN(2, num_planes) * sb_size_square;
880 
881   return sb_rows * sb_cols * sb_palette_toks;
882 }
883 
884 // Get the allocated token size for a tile. It does the same calculation as in
885 // the frame token allocation.
allocated_tokens(TileInfo tile,int sb_size_log2,int num_planes)886 static INLINE unsigned int allocated_tokens(TileInfo tile, int sb_size_log2,
887                                             int num_planes) {
888   int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 2) >> 2;
889   int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 2) >> 2;
890 
891   return get_token_alloc(tile_mb_rows, tile_mb_cols, sb_size_log2, num_planes);
892 }
893 
get_start_tok(AV1_COMP * cpi,int tile_row,int tile_col,int mi_row,TOKENEXTRA ** tok,int sb_size_log2,int num_planes)894 static INLINE void get_start_tok(AV1_COMP *cpi, int tile_row, int tile_col,
895                                  int mi_row, TOKENEXTRA **tok, int sb_size_log2,
896                                  int num_planes) {
897   AV1_COMMON *const cm = &cpi->common;
898   const int tile_cols = cm->tile_cols;
899   TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
900   const TileInfo *const tile_info = &this_tile->tile_info;
901 
902   const int tile_mb_cols =
903       (tile_info->mi_col_end - tile_info->mi_col_start + 2) >> 2;
904   const int tile_mb_row = (mi_row - tile_info->mi_row_start + 2) >> 2;
905 
906   *tok = cpi->tile_tok[tile_row][tile_col] +
907          get_token_alloc(tile_mb_row, tile_mb_cols, sb_size_log2, num_planes);
908 }
909 
910 void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags);
911 
912 #define ALT_MIN_LAG 3
is_altref_enabled(const AV1_COMP * const cpi)913 static INLINE int is_altref_enabled(const AV1_COMP *const cpi) {
914   return cpi->oxcf.lag_in_frames >= ALT_MIN_LAG && cpi->oxcf.enable_auto_arf;
915 }
916 
917 // TODO(zoeliu): To set up cpi->oxcf.enable_auto_brf
918 
set_ref_ptrs(const AV1_COMMON * cm,MACROBLOCKD * xd,MV_REFERENCE_FRAME ref0,MV_REFERENCE_FRAME ref1)919 static INLINE void set_ref_ptrs(const AV1_COMMON *cm, MACROBLOCKD *xd,
920                                 MV_REFERENCE_FRAME ref0,
921                                 MV_REFERENCE_FRAME ref1) {
922   xd->block_refs[0] =
923       &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME : 0];
924   xd->block_refs[1] =
925       &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME : 0];
926 }
927 
get_chessboard_index(int frame_index)928 static INLINE int get_chessboard_index(int frame_index) {
929   return frame_index & 0x1;
930 }
931 
cond_cost_list(const struct AV1_COMP * cpi,int * cost_list)932 static INLINE int *cond_cost_list(const struct AV1_COMP *cpi, int *cost_list) {
933   return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
934 }
935 
936 void av1_new_framerate(AV1_COMP *cpi, double framerate);
937 
938 #define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
939 
940 // Update up-sampled reference frame index.
uref_cnt_fb(EncRefCntBuffer * ubufs,int * uidx,int new_uidx)941 static INLINE void uref_cnt_fb(EncRefCntBuffer *ubufs, int *uidx,
942                                int new_uidx) {
943   const int ref_index = *uidx;
944 
945   if (ref_index >= 0 && ubufs[ref_index].ref_count > 0)
946     ubufs[ref_index].ref_count--;
947 
948   *uidx = new_uidx;
949   ubufs[new_uidx].ref_count++;
950 }
951 
952 // Returns 1 if a frame is scaled and 0 otherwise.
av1_resize_scaled(const AV1_COMMON * cm)953 static INLINE int av1_resize_scaled(const AV1_COMMON *cm) {
954   return !(cm->superres_upscaled_width == cm->render_width &&
955            cm->superres_upscaled_height == cm->render_height);
956 }
957 
av1_frame_scaled(const AV1_COMMON * cm)958 static INLINE int av1_frame_scaled(const AV1_COMMON *cm) {
959   return !av1_superres_scaled(cm) && av1_resize_scaled(cm);
960 }
961 
962 // Don't allow a show_existing_frame to coincide with an error resilient
963 // frame. An exception can be made for a forward keyframe since it has no
964 // previous dependencies.
encode_show_existing_frame(const AV1_COMMON * cm)965 static INLINE int encode_show_existing_frame(const AV1_COMMON *cm) {
966   return cm->show_existing_frame &&
967          (!cm->error_resilient_mode || cm->frame_type == KEY_FRAME);
968 }
969 
970 // Returns a Sequence Header OBU stored in an aom_fixed_buf_t, or NULL upon
971 // failure. When a non-NULL aom_fixed_buf_t pointer is returned by this
972 // function, the memory must be freed by the caller. Both the buf member of the
973 // aom_fixed_buf_t, and the aom_fixed_buf_t pointer itself must be freed. Memory
974 // returned must be freed via call to free().
975 //
976 // Note: The OBU returned is in Low Overhead Bitstream Format. Specifically,
977 // the obu_has_size_field bit is set, and the buffer contains the obu_size
978 // field.
979 aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi);
980 
981 #ifdef __cplusplus
982 }  // extern "C"
983 #endif
984 
985 #endif  // AOM_AV1_ENCODER_ENCODER_H_
986