blob: 1838b9202dad483706c9d442f19430fb2c2b1b51 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AV1_ENCODER_ENCODER_H_
13#define AV1_ENCODER_ENCODER_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include <stdio.h>
16
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "./aom_config.h"
18#include "aom/aomcx.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
20#include "av1/common/alloccommon.h"
21#include "av1/common/entropymode.h"
22#include "av1/common/thread_common.h"
23#include "av1/common/onyxc_int.h"
24#include "av1/encoder/aq_cyclicrefresh.h"
25#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -070026#include "aom_dsp/buf_ans.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#endif
28#include "av1/encoder/context_tree.h"
29#include "av1/encoder/encodemb.h"
30#include "av1/encoder/firstpass.h"
31#include "av1/encoder/lookahead.h"
32#include "av1/encoder/mbgraph.h"
33#include "av1/encoder/mcomp.h"
34#include "av1/encoder/quantize.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#include "av1/encoder/variance_tree.h"
40
41#if CONFIG_INTERNAL_STATS
42#include "aom_dsp/ssim.h"
43#endif
44#include "aom_dsp/variance.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070045#include "aom/internal/aom_codec_internal.h"
46#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52typedef struct {
53 int nmvjointcost[MV_JOINTS];
54 int nmvcosts[2][MV_VALS];
55 int nmvcosts_hp[2][MV_VALS];
56
57#if CONFIG_REF_MV
58 int nmv_vec_cost[NMV_CONTEXTS][MV_JOINTS];
59 int nmv_costs[NMV_CONTEXTS][2][MV_VALS];
60 int nmv_costs_hp[NMV_CONTEXTS][2][MV_VALS];
61#endif
62
Yaowu Xuc27fc142016-08-22 16:08:15 -070063 // 0 = Intra, Last, GF, ARF
64 signed char last_ref_lf_deltas[TOTAL_REFS_PER_FRAME];
65 // 0 = ZERO_MV, MV
66 signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
67
68 FRAME_CONTEXT fc;
69} CODING_CONTEXT;
70
71typedef enum {
72 // regular inter frame
73 REGULAR_FRAME = 0,
74 // alternate reference frame
75 ARF_FRAME = 1,
76 // overlay frame
77 OVERLAY_FRAME = 2,
78 // golden frame
79 GLD_FRAME = 3,
80#if CONFIG_EXT_REFS
81 // backward reference frame
82 BRF_FRAME = 4,
83 // extra alternate reference frame
84 EXT_ARF_FRAME = 5
85#endif
86} FRAME_CONTEXT_INDEX;
87
88typedef enum {
Yaowu Xuc27fc142016-08-22 16:08:15 -070089 NORMAL = 0,
90 FOURFIVE = 1,
91 THREEFIVE = 2,
92 ONETWO = 3
Yaowu Xuf883b422016-08-30 14:01:10 -070093} AOM_SCALING;
Yaowu Xuc27fc142016-08-22 16:08:15 -070094
95typedef enum {
96 // Good Quality Fast Encoding. The encoder balances quality with the amount of
97 // time it takes to encode the output. Speed setting controls how fast.
98 GOOD,
99
100 // The encoder places priority on the quality of the output over encoding
101 // speed. The output is compressed at the highest possible quality. This
102 // option takes the longest amount of time to encode. Speed setting ignored.
103 BEST,
104
105 // Realtime/Live Encoding. This mode is optimized for realtime encoding (for
106 // example, capturing a television signal or feed from a live camera). Speed
107 // setting controls how fast.
108 REALTIME
109} MODE;
110
111typedef enum {
112 FRAMEFLAGS_KEY = 1 << 0,
113 FRAMEFLAGS_GOLDEN = 1 << 1,
114#if CONFIG_EXT_REFS
115 FRAMEFLAGS_BWDREF = 1 << 2,
116 FRAMEFLAGS_ALTREF = 1 << 3,
117#else
118 FRAMEFLAGS_ALTREF = 1 << 2,
119#endif // CONFIG_EXT_REFS
120} FRAMETYPE_FLAGS;
121
122typedef enum {
123 NO_AQ = 0,
124 VARIANCE_AQ = 1,
125 COMPLEXITY_AQ = 2,
126 CYCLIC_REFRESH_AQ = 3,
Arild Fuldseth07441162016-08-15 15:07:52 +0200127#if CONFIG_DELTA_Q
128 DELTA_AQ = 4,
129#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130 AQ_MODE_COUNT // This should always be the last member of the enum
131} AQ_MODE;
132
133typedef enum {
134 RESIZE_NONE = 0, // No frame resizing allowed.
135 RESIZE_FIXED = 1, // All frames are coded at the specified dimension.
136 RESIZE_DYNAMIC = 2 // Coded size of each frame is determined by the codec.
137} RESIZE_TYPE;
138
Yaowu Xuf883b422016-08-30 14:01:10 -0700139typedef struct AV1EncoderConfig {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140 BITSTREAM_PROFILE profile;
Yaowu Xuf883b422016-08-30 14:01:10 -0700141 aom_bit_depth_t bit_depth; // Codec bit-depth.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700142 int width; // width of data passed to the compressor
143 int height; // height of data passed to the compressor
144 unsigned int input_bit_depth; // Input bit depth.
145 double init_framerate; // set to passed in framerate
146 int64_t target_bandwidth; // bandwidth to be used in bits per second
147
148 int noise_sensitivity; // pre processing blur: recommendation 0
149 int sharpness; // sharpening output: recommendation 0:
150 int speed;
151 // maximum allowed bitrate for any intra frame in % of bitrate target.
152 unsigned int rc_max_intra_bitrate_pct;
153 // maximum allowed bitrate for any inter frame in % of bitrate target.
154 unsigned int rc_max_inter_bitrate_pct;
155 // percent of rate boost for golden frame in CBR mode.
156 unsigned int gf_cbr_boost_pct;
157
158 MODE mode;
159 int pass;
160
161 // Key Framing Operations
162 int auto_key; // autodetect cut scenes and set the keyframes
163 int key_freq; // maximum distance to key frame.
164
165 int lag_in_frames; // how many frames lag before we start encoding
166
167 // ----------------------------------------------------------------
168 // DATARATE CONTROL OPTIONS
169
170 // vbr, cbr, constrained quality or constant quality
Yaowu Xuf883b422016-08-30 14:01:10 -0700171 enum aom_rc_mode rc_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172
173 // buffer targeting aggressiveness
174 int under_shoot_pct;
175 int over_shoot_pct;
176
177 // buffering parameters
178 int64_t starting_buffer_level_ms;
179 int64_t optimal_buffer_level_ms;
180 int64_t maximum_buffer_size_ms;
181
182 // Frame drop threshold.
183 int drop_frames_water_mark;
184
185 // controlling quality
186 int fixed_q;
187 int worst_allowed_q;
188 int best_allowed_q;
189 int cq_level;
190 AQ_MODE aq_mode; // Adaptive Quantization mode
191#if CONFIG_AOM_QM
192 int using_qm;
193 int qm_minlevel;
194 int qm_maxlevel;
195#endif
196
197 // Internal frame size scaling.
198 RESIZE_TYPE resize_mode;
199 int scaled_frame_width;
200 int scaled_frame_height;
201
202 // Enable feature to reduce the frame quantization every x frames.
203 int frame_periodic_boost;
204
205 // two pass datarate control
206 int two_pass_vbrbias; // two pass datarate control tweaks
207 int two_pass_vbrmin_section;
208 int two_pass_vbrmax_section;
209 // END DATARATE CONTROL OPTIONS
210 // ----------------------------------------------------------------
211
212 int enable_auto_arf;
213#if CONFIG_EXT_REFS
214 int enable_auto_brf; // (b)ackward (r)ef (f)rame
215#endif // CONFIG_EXT_REFS
216
Yaowu Xuc27fc142016-08-22 16:08:15 -0700217 /* Bitfield defining the error resiliency features to enable.
218 * Can provide decodable frames after losses in previous
219 * frames and decodable partitions after losses in the same frame.
220 */
221 unsigned int error_resilient_mode;
222
223 /* Bitfield defining the parallel decoding mode where the
224 * decoding in successive frames may be conducted in parallel
225 * just by decoding the frame headers.
226 */
227 unsigned int frame_parallel_decoding_mode;
228
229 int arnr_max_frames;
230 int arnr_strength;
231
232 int min_gf_interval;
233 int max_gf_interval;
234
235 int tile_columns;
236 int tile_rows;
237
238 int max_threads;
239
Yaowu Xuf883b422016-08-30 14:01:10 -0700240 aom_fixed_buf_t two_pass_stats_in;
241 struct aom_codec_pkt_list *output_pkt_list;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242
243#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -0700244 aom_fixed_buf_t firstpass_mb_stats_in;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245#endif
246
Yaowu Xuf883b422016-08-30 14:01:10 -0700247 aom_tune_metric tuning;
248 aom_tune_content content;
249#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250 int use_highbitdepth;
251#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700252 aom_color_space_t color_space;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253 int color_range;
254 int render_width;
255 int render_height;
256
257#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700258 aom_superblock_size_t superblock_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700260} AV1EncoderConfig;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262static INLINE int is_lossless_requested(const AV1EncoderConfig *cfg) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
264}
265
266// TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
267typedef struct TileDataEnc {
268 TileInfo tile_info;
269 int thresh_freq_fact[BLOCK_SIZES][MAX_MODES];
270 int mode_map[BLOCK_SIZES][MAX_MODES];
Yunqing Wang8c1e57c2016-10-25 15:15:23 -0700271 int m_search_count;
272 int ex_search_count;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700273#if CONFIG_PVQ
274 PVQ_QUEUE pvq_q;
275#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276} TileDataEnc;
277
278typedef struct RD_COUNTS {
Yaowu Xuf883b422016-08-30 14:01:10 -0700279 av1_coeff_count coef_counts[TX_SIZES][PLANE_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280 int64_t comp_pred_diff[REFERENCE_MODES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281} RD_COUNTS;
282
283typedef struct ThreadData {
284 MACROBLOCK mb;
285 RD_COUNTS rd_counts;
286 FRAME_COUNTS *counts;
287
288 PICK_MODE_CONTEXT *leaf_tree;
289 PC_TREE *pc_tree;
290 PC_TREE *pc_root[MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2 + 1];
291
292 VAR_TREE *var_tree;
293 VAR_TREE *var_root[MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2 + 1];
294} ThreadData;
295
296struct EncWorkerData;
297
298typedef struct ActiveMap {
299 int enabled;
300 int update;
301 unsigned char *map;
302} ActiveMap;
303
Urvang Joshib5ed3502016-10-17 16:38:05 -0700304#define NUM_STAT_TYPES 4 // types of stats: Y, U, V and ALL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700305
306typedef struct IMAGE_STAT {
Urvang Joshib5ed3502016-10-17 16:38:05 -0700307 double stat[NUM_STAT_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700308 double worst;
309} ImageStat;
310
Urvang Joshib5ed3502016-10-17 16:38:05 -0700311#undef NUM_STAT_TYPES
312
Yaowu Xuc27fc142016-08-22 16:08:15 -0700313typedef struct {
314 int ref_count;
315 YV12_BUFFER_CONFIG buf;
316} EncRefCntBuffer;
317
318#if CONFIG_ENTROPY
319typedef struct SUBFRAME_STATS {
Yaowu Xuf883b422016-08-30 14:01:10 -0700320 av1_coeff_probs_model coef_probs_buf[COEF_PROBS_BUFS][TX_SIZES][PLANE_TYPES];
321 av1_coeff_count coef_counts_buf[COEF_PROBS_BUFS][TX_SIZES][PLANE_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700322 unsigned int eob_counts_buf[COEF_PROBS_BUFS][TX_SIZES][PLANE_TYPES][REF_TYPES]
323 [COEF_BANDS][COEFF_CONTEXTS];
Yaowu Xuf883b422016-08-30 14:01:10 -0700324 av1_coeff_probs_model enc_starting_coef_probs[TX_SIZES][PLANE_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700325} SUBFRAME_STATS;
326#endif // CONFIG_ENTROPY
327
328typedef struct TileBufferEnc {
329 uint8_t *data;
330 size_t size;
331} TileBufferEnc;
332
Yaowu Xuf883b422016-08-30 14:01:10 -0700333typedef struct AV1_COMP {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700334 QUANTS quants;
335 ThreadData td;
336 MB_MODE_INFO_EXT *mbmi_ext_base;
337 DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]); // 8: SIMD width
338 DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]); // 8: SIMD width
339#if CONFIG_NEW_QUANT
340 DECLARE_ALIGNED(16, dequant_val_type_nuq,
341 y_dequant_val_nuq[QUANT_PROFILES][QINDEX_RANGE][COEF_BANDS]);
342 DECLARE_ALIGNED(16, dequant_val_type_nuq,
343 uv_dequant_val_nuq[QUANT_PROFILES][QINDEX_RANGE][COEF_BANDS]);
344#endif // CONFIG_NEW_QUANT
Yaowu Xuf883b422016-08-30 14:01:10 -0700345 AV1_COMMON common;
346 AV1EncoderConfig oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347 struct lookahead_ctx *lookahead;
348 struct lookahead_entry *alt_ref_source;
349
350 YV12_BUFFER_CONFIG *Source;
351 YV12_BUFFER_CONFIG *Last_Source; // NULL for first frame and alt_ref frames
352 YV12_BUFFER_CONFIG *un_scaled_source;
353 YV12_BUFFER_CONFIG scaled_source;
354 YV12_BUFFER_CONFIG *unscaled_last_source;
355 YV12_BUFFER_CONFIG scaled_last_source;
356
357 // Up-sampled reference buffers
358 // NOTE(zoeliu): It is needed to allocate sufficient space to the up-sampled
359 // reference buffers, which should include the up-sampled version of all the
360 // possibly stored references plus the currently coded frame itself.
361 EncRefCntBuffer upsampled_ref_bufs[REF_FRAMES + 1];
362 int upsampled_ref_idx[REF_FRAMES + 1];
363
364 // For a still frame, this flag is set to 1 to skip partition search.
365 int partition_search_skippable_frame;
366
367 int scaled_ref_idx[TOTAL_REFS_PER_FRAME];
368#if CONFIG_EXT_REFS
369 int lst_fb_idxes[LAST_REF_FRAMES];
370#else
371 int lst_fb_idx;
372#endif // CONFIG_EXT_REFS
373 int gld_fb_idx;
374#if CONFIG_EXT_REFS
375 int bwd_fb_idx; // BWD_REF_FRAME
376#endif // CONFIG_EXT_REFS
377 int alt_fb_idx;
378
379 int last_show_frame_buf_idx; // last show frame buffer index
380
381 int refresh_last_frame;
382 int refresh_golden_frame;
383#if CONFIG_EXT_REFS
384 int refresh_bwd_ref_frame;
385#endif // CONFIG_EXT_REFS
386 int refresh_alt_ref_frame;
387
388 int ext_refresh_frame_flags_pending;
389 int ext_refresh_last_frame;
390 int ext_refresh_golden_frame;
391 int ext_refresh_alt_ref_frame;
392
393 int ext_refresh_frame_context_pending;
394 int ext_refresh_frame_context;
395
396 YV12_BUFFER_CONFIG last_frame_uf;
397#if CONFIG_LOOP_RESTORATION
398 YV12_BUFFER_CONFIG last_frame_db;
399#endif // CONFIG_LOOP_RESTORATION
400
401 // Ambient reconstruction err target for force key frames
402 int64_t ambient_err;
403
404 RD_OPT rd;
405
406 CODING_CONTEXT coding_context;
407
408#if CONFIG_REF_MV
Urvang Joshibffc0b52016-07-25 13:38:49 -0700409 int nmv_costs[NMV_CONTEXTS][2][MV_VALS];
410 int nmv_costs_hp[NMV_CONTEXTS][2][MV_VALS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700411#endif
412
Urvang Joshibffc0b52016-07-25 13:38:49 -0700413 int nmvcosts[2][MV_VALS];
414 int nmvcosts_hp[2][MV_VALS];
415 int nmvsadcosts[2][MV_VALS];
416 int nmvsadcosts_hp[2][MV_VALS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417
418 int64_t last_time_stamp_seen;
419 int64_t last_end_time_stamp_seen;
420 int64_t first_time_stamp_ever;
421
422 RATE_CONTROL rc;
423 double framerate;
424
425 // NOTE(zoeliu): Any inter frame allows maximum of REF_FRAMES inter
426 // references; Plus the currently coded frame itself, it is needed to allocate
427 // sufficient space to the size of the maximum possible number of frames.
428 int interp_filter_selected[REF_FRAMES + 1][SWITCHABLE];
429
Yaowu Xuf883b422016-08-30 14:01:10 -0700430 struct aom_codec_pkt_list *output_pkt_list;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700431
432 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
433 int mbgraph_n_frames; // number of frames filled in the above
434 int static_mb_pct; // % forced skip mbs by segmentation
435 int ref_frame_flags;
436
437 SPEED_FEATURES sf;
438
439 unsigned int max_mv_magnitude;
440 int mv_step_param;
441
442 int allow_comp_inter_inter;
443
Yaowu Xuc27fc142016-08-22 16:08:15 -0700444 uint8_t *segmentation_map;
445
Yaowu Xuc27fc142016-08-22 16:08:15 -0700446 CYCLIC_REFRESH *cyclic_refresh;
447 ActiveMap active_map;
448
449 fractional_mv_step_fp *find_fractional_mv_step;
Yaowu Xuf883b422016-08-30 14:01:10 -0700450 av1_full_search_fn_t full_search_sad; // It is currently unused.
451 av1_diamond_search_fn_t diamond_search_sad;
452 aom_variance_fn_ptr_t fn_ptr[BLOCK_SIZES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700453 uint64_t time_receive_data;
454 uint64_t time_compress_data;
455 uint64_t time_pick_lpf;
456 uint64_t time_encode_sb_row;
457
458#if CONFIG_FP_MB_STATS
459 int use_fp_mb_stats;
460#endif
461
462 TWO_PASS twopass;
463
464 YV12_BUFFER_CONFIG alt_ref_buffer;
465
466#if CONFIG_INTERNAL_STATS
467 unsigned int mode_chosen_counts[MAX_MODES];
468
469 int count;
470 uint64_t total_sq_error;
471 uint64_t total_samples;
472 ImageStat psnr;
473
474 double total_blockiness;
475 double worst_blockiness;
476
477 int bytes;
478 double summed_quality;
479 double summed_weights;
480 unsigned int tot_recode_hits;
481 double worst_ssim;
482
483 ImageStat fastssim;
484 ImageStat psnrhvs;
485
486 int b_calculate_blockiness;
487 int b_calculate_consistency;
488
489 double total_inconsistency;
490 double worst_consistency;
491 Ssimv *ssim_vars;
492 Metrics metrics;
493#endif
494 int b_calculate_psnr;
495
496 int droppable;
497
498 int initial_width;
499 int initial_height;
500 int initial_mbs; // Number of MBs in the full-size frame; to be used to
501 // normalize the firstpass stats. This will differ from the
502 // number of MBs in the current frame when the frame is
503 // scaled.
504
505 // Store frame variance info in SOURCE_VAR_BASED_PARTITION search type.
Urvang Joshi454280d2016-10-14 16:51:44 -0700506 DIFF *source_diff_var;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700507 // The threshold used in SOURCE_VAR_BASED_PARTITION search type.
508 unsigned int source_var_thresh;
509 int frames_till_next_var_check;
510
511 int frame_flags;
512
513 search_site_config ss_cfg;
514
515 int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
516#if CONFIG_REF_MV
517 int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
518 int zeromv_mode_cost[ZEROMV_MODE_CONTEXTS][2];
519 int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
520 int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
521#if CONFIG_EXT_INTER
522 int new2mv_mode_cost[2];
523#endif // CONFIG_EXT_INTER
524#endif
525
526 unsigned int inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES];
527#if CONFIG_EXT_INTER
clang-format67948d32016-09-07 22:40:40 -0700528 unsigned int inter_compound_mode_cost[INTER_MODE_CONTEXTS]
529 [INTER_COMPOUND_MODES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700530 unsigned int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
531#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -0700532#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
533 int motion_mode_cost[BLOCK_SIZES][MOTION_MODES];
534#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -0700535 int intra_uv_mode_cost[INTRA_MODES][INTRA_MODES];
536 int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
537 int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
538#if CONFIG_EXT_PARTITION_TYPES
539 int partition_cost[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
540#else
541 int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
542#endif
Urvang Joshib100db72016-10-12 16:28:56 -0700543#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544 int palette_y_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
545 int palette_uv_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
clang-format67948d32016-09-07 22:40:40 -0700546 int palette_y_color_cost[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS]
547 [PALETTE_COLORS];
548 int palette_uv_color_cost[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS]
549 [PALETTE_COLORS];
Urvang Joshib100db72016-10-12 16:28:56 -0700550#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551 int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
552#if CONFIG_EXT_TX
553 int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
554 int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
555 [TX_TYPES];
556#else
557 int intra_tx_type_costs[EXT_TX_SIZES][TX_TYPES][TX_TYPES];
558 int inter_tx_type_costs[EXT_TX_SIZES][TX_TYPES];
559#endif // CONFIG_EXT_TX
560#if CONFIG_EXT_INTRA
561 int intra_filter_cost[INTRA_FILTERS + 1][INTRA_FILTERS];
562#endif // CONFIG_EXT_INTRA
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700563#if CONFIG_LOOP_RESTORATION
564 int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
565#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuc27fc142016-08-22 16:08:15 -0700566
567 int multi_arf_allowed;
568 int multi_arf_enabled;
569 int multi_arf_last_grp_enabled;
570
571 TileDataEnc *tile_data;
572 int allocated_tiles; // Keep track of memory allocated for tiles.
573
574 TOKENEXTRA *tile_tok[MAX_TILE_ROWS][MAX_TILE_COLS];
575 unsigned int tok_count[MAX_TILE_ROWS][MAX_TILE_COLS];
576
577 TileBufferEnc tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
578
579 int resize_pending;
580 int resize_state;
581 int resize_scale_num;
582 int resize_scale_den;
583 int resize_avg_qp;
584 int resize_buffer_underflow;
585 int resize_count;
586
587 // VAR_BASED_PARTITION thresholds
588 // 0 - threshold_128x128;
589 // 1 - threshold_64x64;
590 // 2 - threshold_32x32;
591 // 3 - threshold_16x16;
592 // 4 - threshold_8x8;
593 int64_t vbp_thresholds[5];
594 int64_t vbp_threshold_minmax;
595 int64_t vbp_threshold_sad;
596 BLOCK_SIZE vbp_bsize_min;
597
598 // VARIANCE_AQ segment map refresh
599 int vaq_refresh;
600
601 // Multi-threading
602 int num_workers;
Yaowu Xuf883b422016-08-30 14:01:10 -0700603 AVxWorker *workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 struct EncWorkerData *tile_thr_data;
Yaowu Xuf883b422016-08-30 14:01:10 -0700605 AV1LfSync lf_row_sync;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606#if CONFIG_ENTROPY
607 SUBFRAME_STATS subframe_stats;
608 // TODO(yaowu): minimize the size of count buffers
609 SUBFRAME_STATS wholeframe_stats;
Yaowu Xuf883b422016-08-30 14:01:10 -0700610 av1_coeff_stats branch_ct_buf[COEF_PROBS_BUFS][TX_SIZES][PLANE_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700611#endif // CONFIG_ENTROPY
612#if CONFIG_ANS
613 struct BufAnsCoder buf_ans;
614#endif
615#if CONFIG_EXT_REFS
616 int refresh_frame_mask;
617 int existing_fb_idx_to_show;
618 int is_arf_filter_off[MAX_EXT_ARFS + 1];
619 int num_extra_arfs;
620 int arf_map[MAX_EXT_ARFS + 1];
621#endif // CONFIG_EXT_REFS
622#if CONFIG_GLOBAL_MOTION
623 int global_motion_used[TOTAL_REFS_PER_FRAME];
624#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100625#if CONFIG_REFERENCE_BUFFER
626 SequenceHeader seq_params;
627#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700628} AV1_COMP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629
Yaowu Xuf883b422016-08-30 14:01:10 -0700630void av1_initialize_enc(void);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700631
Yaowu Xuf883b422016-08-30 14:01:10 -0700632struct AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
633 BufferPool *const pool);
634void av1_remove_compressor(AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700635
Yaowu Xuf883b422016-08-30 14:01:10 -0700636void av1_change_config(AV1_COMP *cpi, const AV1EncoderConfig *oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637
638// receive a frames worth of data. caller can assume that a copy of this
639// frame is made and not just a copy of the pointer..
Yaowu Xuf883b422016-08-30 14:01:10 -0700640int av1_receive_raw_frame(AV1_COMP *cpi, unsigned int frame_flags,
641 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
642 int64_t end_time_stamp);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700643
Yaowu Xuf883b422016-08-30 14:01:10 -0700644int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
645 size_t *size, uint8_t *dest, int64_t *time_stamp,
646 int64_t *time_end, int flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647
Yaowu Xuf883b422016-08-30 14:01:10 -0700648int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700649
Yaowu Xuf883b422016-08-30 14:01:10 -0700650int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651
Yaowu Xuf883b422016-08-30 14:01:10 -0700652int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700653
Yaowu Xuf883b422016-08-30 14:01:10 -0700654void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655
Yaowu Xuf883b422016-08-30 14:01:10 -0700656int av1_copy_reference_enc(AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657 YV12_BUFFER_CONFIG *sd);
658
Yaowu Xuf883b422016-08-30 14:01:10 -0700659int av1_set_reference_enc(AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag,
660 YV12_BUFFER_CONFIG *sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700661
Yaowu Xuf883b422016-08-30 14:01:10 -0700662int av1_update_entropy(AV1_COMP *cpi, int update);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700663
Yaowu Xuf883b422016-08-30 14:01:10 -0700664int av1_set_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700665
Yaowu Xuf883b422016-08-30 14:01:10 -0700666int av1_get_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700667
Yaowu Xuf883b422016-08-30 14:01:10 -0700668int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
669 AOM_SCALING vert_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700670
Yaowu Xuf883b422016-08-30 14:01:10 -0700671int av1_set_size_literal(AV1_COMP *cpi, unsigned int width,
672 unsigned int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673
Yaowu Xuf883b422016-08-30 14:01:10 -0700674int av1_get_quantizer(struct AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700675
Yaowu Xuf883b422016-08-30 14:01:10 -0700676void av1_full_to_model_counts(av1_coeff_count_model *model_count,
677 av1_coeff_count *full_count);
678
679static INLINE int frame_is_kf_gf_arf(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700680 return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
681 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
682}
683
Yaowu Xuf883b422016-08-30 14:01:10 -0700684static INLINE int get_ref_frame_map_idx(const AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700685 MV_REFERENCE_FRAME ref_frame) {
686#if CONFIG_EXT_REFS
687 if (ref_frame >= LAST_FRAME && ref_frame <= LAST3_FRAME)
688 return cpi->lst_fb_idxes[ref_frame - 1];
689#else
690 if (ref_frame == LAST_FRAME) return cpi->lst_fb_idx;
691#endif // CONFIG_EXT_REFS
692 else if (ref_frame == GOLDEN_FRAME)
693 return cpi->gld_fb_idx;
694#if CONFIG_EXT_REFS
695 else if (ref_frame == BWDREF_FRAME)
696 return cpi->bwd_fb_idx;
697#endif // CONFIG_EXT_REFS
698 else
699 return cpi->alt_fb_idx;
700}
701
Urvang Joshi52648442016-10-13 17:27:51 -0700702static INLINE int get_ref_frame_buf_idx(const AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703 MV_REFERENCE_FRAME ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700705 const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
706 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX;
707}
708
709static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
Urvang Joshi52648442016-10-13 17:27:51 -0700710 const AV1_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
711 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700712 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
713 return buf_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[buf_idx].buf
714 : NULL;
715}
716
717static INLINE const YV12_BUFFER_CONFIG *get_upsampled_ref(
Urvang Joshi52648442016-10-13 17:27:51 -0700718 const AV1_COMP *cpi, const MV_REFERENCE_FRAME ref_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719 // Use up-sampled reference frames.
720 const int buf_idx =
721 cpi->upsampled_ref_idx[get_ref_frame_map_idx(cpi, ref_frame)];
722 return &cpi->upsampled_ref_bufs[buf_idx].buf;
723}
724
725#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -0700726static INLINE int enc_is_ref_frame_buf(AV1_COMP *cpi, RefCntBuffer *frame_buf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -0700728 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
730 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
731 if (buf_idx == INVALID_IDX) continue;
732 if (frame_buf == &cm->buffer_pool->frame_bufs[buf_idx]) break;
733 }
734 return (ref_frame <= ALTREF_FRAME);
735}
736#endif // CONFIG_EXT_REFS
737
738static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols) {
Jingning Han7f76d472016-07-29 10:48:34 -0700739 // We assume 3 planes all at full resolution. We assume up to 1 token per
740 // pixel, and then allow a head room of 1 EOSB token per 4x4 block per plane,
741 // plus EOSB_TOKEN per plane.
742 return mb_rows * mb_cols * (16 * 16 + 17) * 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743}
744
745// Get the allocated token size for a tile. It does the same calculation as in
746// the frame token allocation.
747static INLINE unsigned int allocated_tokens(TileInfo tile) {
748 int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 1) >> 1;
749 int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 1) >> 1;
750
751 return get_token_alloc(tile_mb_rows, tile_mb_cols);
752}
753
Yaowu Xuf883b422016-08-30 14:01:10 -0700754void av1_alloc_compressor_data(AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700755
Yaowu Xuf883b422016-08-30 14:01:10 -0700756void av1_scale_references(AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700757
Yaowu Xuf883b422016-08-30 14:01:10 -0700758void av1_update_reference_frames(AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759
Yaowu Xuf883b422016-08-30 14:01:10 -0700760void av1_set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700761
Yaowu Xuf883b422016-08-30 14:01:10 -0700762YV12_BUFFER_CONFIG *av1_scale_if_required_fast(AV1_COMMON *cm,
763 YV12_BUFFER_CONFIG *unscaled,
764 YV12_BUFFER_CONFIG *scaled);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765
Yaowu Xuf883b422016-08-30 14:01:10 -0700766YV12_BUFFER_CONFIG *av1_scale_if_required(AV1_COMMON *cm,
767 YV12_BUFFER_CONFIG *unscaled,
768 YV12_BUFFER_CONFIG *scaled);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700769
Yaowu Xuf883b422016-08-30 14:01:10 -0700770void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771
Yaowu Xuf883b422016-08-30 14:01:10 -0700772static INLINE int is_altref_enabled(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700773 return cpi->oxcf.mode != REALTIME && cpi->oxcf.lag_in_frames > 0 &&
774 cpi->oxcf.enable_auto_arf;
775}
776
777// TODO(zoeliu): To set up cpi->oxcf.enable_auto_brf
778#if 0 && CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -0700779static INLINE int is_bwdref_enabled(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 // NOTE(zoeliu): The enabling of bi-predictive frames depends on the use of
781 // alt_ref, and now will be off when the alt_ref interval is
782 // not sufficiently large.
783 return is_altref_enabled(cpi) && cpi->oxcf.enable_auto_brf;
784}
785#endif // CONFIG_EXT_REFS
786
Urvang Joshi52648442016-10-13 17:27:51 -0700787static INLINE void set_ref_ptrs(const AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788 MV_REFERENCE_FRAME ref0,
789 MV_REFERENCE_FRAME ref1) {
790 xd->block_refs[0] =
791 &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME : 0];
792 xd->block_refs[1] =
793 &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME : 0];
794}
795
796static INLINE int get_chessboard_index(const int frame_index) {
797 return frame_index & 0x1;
798}
799
Yaowu Xuf883b422016-08-30 14:01:10 -0700800static INLINE int *cond_cost_list(const struct AV1_COMP *cpi, int *cost_list) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700801 return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
802}
803
Yaowu Xuf883b422016-08-30 14:01:10 -0700804void av1_new_framerate(AV1_COMP *cpi, double framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805
806#define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
807
808// Update up-sampled reference frame index.
809static INLINE void uref_cnt_fb(EncRefCntBuffer *ubufs, int *uidx,
810 int new_uidx) {
811 const int ref_index = *uidx;
812
813 if (ref_index >= 0 && ubufs[ref_index].ref_count > 0)
814 ubufs[ref_index].ref_count--;
815
816 *uidx = new_uidx;
817 ubufs[new_uidx].ref_count++;
818}
819
820#ifdef __cplusplus
821} // extern "C"
822#endif
823
Yaowu Xuf883b422016-08-30 14:01:10 -0700824#endif // AV1_ENCODER_ENCODER_H_