1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #ifndef VPX_VP9_ENCODER_VP9_ENCODER_H_
12 #define VPX_VP9_ENCODER_VP9_ENCODER_H_
13
14 #include <stdio.h>
15 #define INLINE __inline
16
17 #include <stdint.h>
18 #include "vp9_onyxc_int.h"
19 #include "vp9_tokenize.h"
20 #include "vp9_block.h"
21 #include "vp9_speed_features.h"
22 #include "vp9_quantize.h"
23 #include "vp9_rd.h"
24 #include "vp9_ratectrl.h"
25
26 #if CONFIG_VP9_TEMPORAL_DENOISING
27 #include "vp9/encoder/vp9_denoiser.h"
28 #endif
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 #if 0
34 // vp9 uses 10,000,000 ticks/second as time stamp
35 #define TICKS_PER_SEC 10000000
36
37 typedef struct {
38 int nmvjointcost[MV_JOINTS];
39 int nmvcosts[2][MV_VALS];
40 int nmvcosts_hp[2][MV_VALS];
41
42 vpx_prob segment_pred_probs[PREDICTION_PROBS];
43
44 unsigned char *last_frame_seg_map_copy;
45
46 // 0 = Intra, Last, GF, ARF
47 signed char last_ref_lf_deltas[MAX_REF_LF_DELTAS];
48 // 0 = ZERO_MV, MV
49 signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
50
51 FRAME_CONTEXT fc;
52 } CODING_CONTEXT;
53
54 typedef enum {
55 // encode_breakout is disabled.
56 ENCODE_BREAKOUT_DISABLED = 0,
57 // encode_breakout is enabled.
58 ENCODE_BREAKOUT_ENABLED = 1,
59 // encode_breakout is enabled with small max_thresh limit.
60 ENCODE_BREAKOUT_LIMITED = 2
61 } ENCODE_BREAKOUT_TYPE;
62
63 typedef enum {
64 NORMAL = 0,
65 FOURFIVE = 1,
66 THREEFIVE = 2,
67 ONETWO = 3
68 } VPX_SCALING;
69
70 typedef enum {
71 // Good Quality Fast Encoding. The encoder balances quality with the amount of
72 // time it takes to encode the output. Speed setting controls how fast.
73 GOOD,
74
75 // The encoder places priority on the quality of the output over encoding
76 // speed. The output is compressed at the highest possible quality. This
77 // option takes the longest amount of time to encode. Speed setting ignored.
78 BEST,
79
80 // Realtime/Live Encoding. This mode is optimized for realtime encoding (for
81 // example, capturing a television signal or feed from a live camera). Speed
82 // setting controls how fast.
83 REALTIME
84 } MODE;
85
86 typedef enum {
87 FRAMEFLAGS_KEY = 1 << 0,
88 FRAMEFLAGS_GOLDEN = 1 << 1,
89 FRAMEFLAGS_ALTREF = 1 << 2,
90 } FRAMETYPE_FLAGS;
91
92 typedef enum {
93 NO_AQ = 0,
94 VARIANCE_AQ = 1,
95 COMPLEXITY_AQ = 2,
96 CYCLIC_REFRESH_AQ = 3,
97 EQUATOR360_AQ = 4,
98 // AQ based on lookahead temporal
99 // variance (only valid for altref frames)
100 LOOKAHEAD_AQ = 5,
101 AQ_MODE_COUNT // This should always be the last member of the enum
102 } AQ_MODE;
103
104 typedef enum {
105 RESIZE_NONE = 0, // No frame resizing allowed (except for SVC).
106 RESIZE_FIXED = 1, // All frames are coded at the specified dimension.
107 RESIZE_DYNAMIC = 2 // Coded size of each frame is determined by the codec.
108 } RESIZE_TYPE;
109
110 typedef enum {
111 kInvalid = 0,
112 kLowSadLowSumdiff = 1,
113 kLowSadHighSumdiff = 2,
114 kHighSadLowSumdiff = 3,
115 kHighSadHighSumdiff = 4,
116 kLowVarHighSumdiff = 5,
117 kVeryHighSad = 6,
118 } CONTENT_STATE_SB;
119
120 typedef struct VP9EncoderConfig {
121 BITSTREAM_PROFILE profile;
122 vpx_bit_depth_t bit_depth; // Codec bit-depth.
123 int width; // width of data passed to the compressor
124 int height; // height of data passed to the compressor
125 unsigned int input_bit_depth; // Input bit depth.
126 double init_framerate; // set to passed in frame_rate
127 int64_t target_bandwidth; // bandwidth to be used in bits per second
128
129 int noise_sensitivity; // pre processing blur: recommendation 0
130 int sharpness; // sharpening output: recommendation 0:
131 int speed;
132 // maximum allowed bitrate for any intra frame in % of bitrate target.
133 unsigned int rc_max_intra_bitrate_pct;
134 // maximum allowed bitrate for any inter frame in % of bitrate target.
135 unsigned int rc_max_inter_bitrate_pct;
136 // percent of rate boost for golden frame in CBR mode.
137 unsigned int gf_cbr_boost_pct;
138
139 MODE mode;
140 int pass;
141
142 // Key Framing Operations
143 int auto_key; // autodetect cut scenes and set the keyframes
144 int key_freq; // maximum distance to key frame.
145
146 int lag_in_frames; // how many frames lag before we start encoding
147
148 // ----------------------------------------------------------------
149 // DATARATE CONTROL OPTIONS
150
151 // vbr, cbr, constrained quality or constant quality
152 enum vpx_rc_mode rc_mode;
153
154 // buffer targeting aggressiveness
155 int under_shoot_pct;
156 int over_shoot_pct;
157
158 // buffering parameters
159 int64_t starting_buffer_level_ms;
160 int64_t optimal_buffer_level_ms;
161 int64_t maximum_buffer_size_ms;
162
163 // Frame drop threshold.
164 int drop_frames_water_mark;
165
166 // controlling quality
167 int fixed_q;
168 int worst_allowed_q;
169 int best_allowed_q;
170 int cq_level;
171 AQ_MODE aq_mode; // Adaptive Quantization mode
172
173 // Special handling of Adaptive Quantization for AltRef frames
174 int alt_ref_aq;
175
176 // Internal frame size scaling.
177 RESIZE_TYPE resize_mode;
178 int scaled_frame_width;
179 int scaled_frame_height;
180
181 // Enable feature to reduce the frame quantization every x frames.
182 int frame_periodic_boost;
183
184 // two pass datarate control
185 int two_pass_vbrbias; // two pass datarate control tweaks
186 int two_pass_vbrmin_section;
187 int two_pass_vbrmax_section;
188 int vbr_corpus_complexity; // 0 indicates corpus vbr disabled
189 // END DATARATE CONTROL OPTIONS
190 // ----------------------------------------------------------------
191
192 // Spatial and temporal scalability.
193 int ss_number_layers; // Number of spatial layers.
194 int ts_number_layers; // Number of temporal layers.
195 // Bitrate allocation for spatial layers.
196 int layer_target_bitrate[VPX_MAX_LAYERS];
197 int ss_target_bitrate[VPX_SS_MAX_LAYERS];
198 int ss_enable_auto_arf[VPX_SS_MAX_LAYERS];
199 // Bitrate allocation (CBR mode) and frame_rate factor, for temporal layers.
200 int ts_rate_decimator[VPX_TS_MAX_LAYERS];
201
202 int enable_auto_arf;
203
204 int encode_breakout; // early breakout : for video conf recommend 800
205
206 /* Bitfield defining the error resiliency features to enable.
207 * Can provide decodable frames after losses in previous
208 * frames and decodable partitions after losses in the same frame.
209 */
210 unsigned int error_resilient_mode;
211
212 /* Bitfield defining the parallel decoding mode where the
213 * decoding in successive frames may be conducted in parallel
214 * just by decoding the frame headers.
215 */
216 unsigned int frame_parallel_decoding_mode;
217
218 int arnr_max_frames;
219 int arnr_strength;
220
221 int min_gf_interval;
222 int max_gf_interval;
223
224 int tile_columns;
225 int tile_rows;
226
227 int enable_tpl_model;
228
229 int max_threads;
230
231 unsigned int target_level;
232
233 vpx_fixed_buf_t two_pass_stats_in;
234 struct vpx_codec_pkt_list *output_pkt_list;
235
236 #if CONFIG_FP_MB_STATS
237 vpx_fixed_buf_t firstpass_mb_stats_in;
238 #endif
239
240 vp8e_tuning tuning;
241 vp9e_tune_content content;
242 #if CONFIG_VP9_HIGHBITDEPTH
243 int use_highbitdepth;
244 #endif
245 vpx_color_space_t color_space;
246 vpx_color_range_t color_range;
247 int render_width;
248 int render_height;
249 VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode;
250
251 int row_mt;
252 unsigned int motion_vector_unit_test;
253 } VP9EncoderConfig;
254
255 static INLINE int is_lossless_requested(const VP9EncoderConfig *cfg) {
256 return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
257 }
258
259 typedef struct TplDepStats {
260 int64_t intra_cost;
261 int64_t inter_cost;
262 int64_t mc_flow;
263 int64_t mc_dep_cost;
264 int64_t mc_ref_cost;
265
266 int ref_frame_index;
267 int_mv mv;
268
269 #if CONFIG_NON_GREEDY_MV
270 int64_t inter_cost_arr[3];
271 int64_t recon_error_arr[3];
272 int64_t sse_arr[3];
273 int_mv mv_arr[3];
274 double feature_score;
275 #endif
276 } TplDepStats;
277
278 typedef struct TplDepFrame {
279 uint8_t is_valid;
280 TplDepStats *tpl_stats_ptr;
281 int stride;
282 int width;
283 int height;
284 int mi_rows;
285 int mi_cols;
286 int base_qindex;
287 } TplDepFrame;
288
289 #define TPL_DEP_COST_SCALE_LOG2 4
290
291 // TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
292 typedef struct TileDataEnc {
293 TileInfo tile_info;
294 int thresh_freq_fact[BLOCK_SIZES][MAX_MODES];
295 #if CONFIG_CONSISTENT_RECODE
296 int thresh_freq_fact_prev[BLOCK_SIZES][MAX_MODES];
297 #endif
298 int8_t mode_map[BLOCK_SIZES][MAX_MODES];
299 FIRSTPASS_DATA fp_data;
300 VP9RowMTSync row_mt_sync;
301
302 // Used for adaptive_rd_thresh with row multithreading
303 int *row_base_thresh_freq_fact;
304 } TileDataEnc;
305
306 typedef struct RowMTInfo {
307 JobQueueHandle job_queue_hdl;
308 #if CONFIG_MULTITHREAD
309 pthread_mutex_t job_mutex;
310 #endif
311 } RowMTInfo;
312 #endif
313 typedef struct {
314 TOKENEXTRA *start;
315 TOKENEXTRA *stop;
316 unsigned int count;
317 } TOKENLIST;
318 #if 0
319 typedef struct MultiThreadHandle {
320 int allocated_tile_rows;
321 int allocated_tile_cols;
322 int allocated_vert_unit_rows;
323
324 // Frame level params
325 int num_tile_vert_sbs[MAX_NUM_TILE_ROWS];
326
327 // Job Queue structure and handles
328 JobQueue *job_queue;
329
330 int jobs_per_tile_col;
331
332 RowMTInfo row_mt_info[MAX_NUM_TILE_COLS];
333 int thread_id_to_tile_id[MAX_NUM_THREADS]; // Mapping of threads to tiles
334 } MultiThreadHandle;
335 #endif
336 typedef struct RD_COUNTS {
337 vp9_coeff_count coef_counts[TX_SIZES][PLANE_TYPES];
338 int64_t comp_pred_diff[REFERENCE_MODES];
339 int64_t filter_diff[SWITCHABLE_FILTER_CONTEXTS];
340 } RD_COUNTS;
341
342 typedef struct ThreadData {
343 MACROBLOCK mb;
344 RD_COUNTS rd_counts;
345 FRAME_COUNTS *counts;
346 #if 0
347 PICK_MODE_CONTEXT *leaf_tree;
348 PC_TREE *pc_tree;
349 PC_TREE *pc_root;
350 #endif
351 } ThreadData;
352 #if 1
353 struct EncWorkerData;
354
355 typedef struct ActiveMap {
356 int enabled;
357 int update;
358 unsigned char *map;
359 } ActiveMap;
360
361 typedef enum { Y, U, V, ALL } STAT_TYPE;
362
363 typedef struct IMAGE_STAT {
364 double stat[ALL + 1];
365 double worst;
366 } ImageStat;
367
368 // Kf noise filtering currently disabled by default in build.
369 // #define ENABLE_KF_DENOISE 1
370
371 #define CPB_WINDOW_SIZE 4
372 #define FRAME_WINDOW_SIZE 128
373 #define SAMPLE_RATE_GRACE_P 0.015
374 #define VP9_LEVELS 14
375
376 typedef enum {
377 LEVEL_UNKNOWN = 0,
378 LEVEL_AUTO = 1,
379 LEVEL_1 = 10,
380 LEVEL_1_1 = 11,
381 LEVEL_2 = 20,
382 LEVEL_2_1 = 21,
383 LEVEL_3 = 30,
384 LEVEL_3_1 = 31,
385 LEVEL_4 = 40,
386 LEVEL_4_1 = 41,
387 LEVEL_5 = 50,
388 LEVEL_5_1 = 51,
389 LEVEL_5_2 = 52,
390 LEVEL_6 = 60,
391 LEVEL_6_1 = 61,
392 LEVEL_6_2 = 62,
393 LEVEL_MAX = 255
394 } VP9_LEVEL;
395
396 typedef struct {
397 VP9_LEVEL level;
398 uint64_t max_luma_sample_rate;
399 uint32_t max_luma_picture_size;
400 uint32_t max_luma_picture_breadth;
401 double average_bitrate; // in kilobits per second
402 double max_cpb_size; // in kilobits
403 double compression_ratio;
404 uint8_t max_col_tiles;
405 uint32_t min_altref_distance;
406 uint8_t max_ref_frame_buffers;
407 } Vp9LevelSpec;
408
409 extern const Vp9LevelSpec vp9_level_defs[VP9_LEVELS];
410
411 typedef struct {
412 int64_t ts; // timestamp
413 uint32_t luma_samples;
414 uint32_t size; // in bytes
415 } FrameRecord;
416
417 typedef struct {
418 FrameRecord buf[FRAME_WINDOW_SIZE];
419 uint8_t start;
420 uint8_t len;
421 } FrameWindowBuffer;
422
423 typedef struct {
424 uint8_t seen_first_altref;
425 uint32_t frames_since_last_altref;
426 uint64_t total_compressed_size;
427 uint64_t total_uncompressed_size;
428 double time_encoded; // in seconds
429 FrameWindowBuffer frame_window_buffer;
430 int ref_refresh_map;
431 } Vp9LevelStats;
432
433 typedef struct {
434 Vp9LevelStats level_stats;
435 Vp9LevelSpec level_spec;
436 } Vp9LevelInfo;
437
438 typedef enum {
439 BITRATE_TOO_LARGE = 0,
440 LUMA_PIC_SIZE_TOO_LARGE,
441 LUMA_PIC_BREADTH_TOO_LARGE,
442 LUMA_SAMPLE_RATE_TOO_LARGE,
443 CPB_TOO_LARGE,
444 COMPRESSION_RATIO_TOO_SMALL,
445 TOO_MANY_COLUMN_TILE,
446 ALTREF_DIST_TOO_SMALL,
447 TOO_MANY_REF_BUFFER,
448 TARGET_LEVEL_FAIL_IDS
449 } TARGET_LEVEL_FAIL_ID;
450
451 typedef struct {
452 int8_t level_index;
453 uint8_t rc_config_updated;
454 uint8_t fail_flag;
455 int max_frame_size; // in bits
456 double max_cpb_size; // in bits
457 } LevelConstraint;
458 #if 0
459 typedef struct ARNRFilterData {
460 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
461 int strength;
462 int frame_count;
463 int alt_ref_index;
464 struct scale_factors sf;
465 } ARNRFilterData;
466 #endif
467 #endif
468 typedef struct VP9_COMP {
469
470 QUANTS quants;
471
472 ThreadData td;
473
474 #if 0
475 MbModeInfoExt *mbmi_ext_base;
476 #endif
477 DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]);
478 DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]);
479
480 VP9_COMMON common;
481
482 #if 0
483 VP9EncoderConfig oxcf;
484 struct lookahead_ctx *lookahead;
485 struct lookahead_entry *alt_ref_source;
486 YV12_BUFFER_CONFIG *Source;
487 YV12_BUFFER_CONFIG *Last_Source; // NULL for first frame and alt_ref frames
488 YV12_BUFFER_CONFIG *un_scaled_source;
489 YV12_BUFFER_CONFIG scaled_source;
490 YV12_BUFFER_CONFIG *unscaled_last_source;
491 YV12_BUFFER_CONFIG scaled_last_source;
492 #ifdef ENABLE_KF_DENOISE
493 YV12_BUFFER_CONFIG raw_unscaled_source;
494 YV12_BUFFER_CONFIG raw_scaled_source;
495 #endif
496 YV12_BUFFER_CONFIG *raw_source_frame;
497
498 TplDepFrame tpl_stats[MAX_LAG_BUFFERS];
499 YV12_BUFFER_CONFIG *tpl_recon_frames[REFS_PER_FRAME + 1];
500
501 TileDataEnc *tile_data;
502 int allocated_tiles; // Keep track of memory allocated for tiles.
503
504 // For a still frame, this flag is set to 1 to skip partition search.
505 int partition_search_skippable_frame;
506
507 int scaled_ref_idx[MAX_REF_FRAMES];
508 #endif
509 int lst_fb_idx;
510 int gld_fb_idx;
511 int alt_fb_idx;
512 #if 0
513 int ref_fb_idx[REF_FRAMES];
514 int last_show_frame_buf_idx; // last show frame buffer index
515 #endif
516
517 #if 0 //Seli
518 int refresh_last_frame;
519 int refresh_golden_frame;
520 int refresh_alt_ref_frame;
521 #endif
522
523 #if 0
524 int ext_refresh_frame_flags_pending;
525 int ext_refresh_last_frame;
526 int ext_refresh_golden_frame;
527 int ext_refresh_alt_ref_frame;
528
529 int ext_refresh_frame_context_pending;
530 int ext_refresh_frame_context;
531 #endif
532 YV12_BUFFER_CONFIG last_frame_uf;
533 #if 0
534 TOKENEXTRA *tile_tok[4][1 << 6];
535 TOKENLIST *tplist[4][1 << 6];
536
537 // Ambient reconstruction err target for force key frames
538 int64_t ambient_err;
539 #endif
540
541 RD_OPT rd;
542
543 #if 0
544 CODING_CONTEXT coding_context;
545 #endif
546
547 int *nmvcosts[2];
548 int *nmvcosts_hp[2];
549 int *nmvsadcosts[2];
550 int *nmvsadcosts_hp[2];
551
552 #if 0
553
554 int64_t last_time_stamp_seen;
555 int64_t last_end_time_stamp_seen;
556 int64_t first_time_stamp_ever;
557 #endif
558 RATE_CONTROL rc;
559 #if 0
560 double frame_rate;
561 #endif
562 int interp_filter_selected[REF_FRAMES][SWITCHABLE];
563 #if 0
564 struct vpx_codec_pkt_list *output_pkt_list;
565
566 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
567 int mbgraph_n_frames; // number of frames filled in the above
568 int static_mb_pct; // % forced skip mbs by segmentation
569 int ref_frame_flags;
570 #endif
571
572 SPEED_FEATURES sf;
573
574 uint32_t max_mv_magnitude;
575 #if 0
576 int mv_step_param;
577 #endif
578
579 int allow_comp_inter_inter;
580
581 #if 0
582 // Default value is 1. From first pass stats, encode_breakout may be disabled.
583 ENCODE_BREAKOUT_TYPE allow_encode_breakout;
584
585 // Get threshold from external input. A suggested threshold is 800 for HD
586 // clips, and 300 for < HD clips.
587 int encode_breakout;
588 #endif
589 uint8_t *segmentation_map;
590 ActiveMap active_map;
591 #if 0
592 uint8_t *skin_map;
593
594 // segment threashold for encode breakout
595 int segment_encode_breakout[MAX_SEGMENTS];
596
597 CYCLIC_REFRESH *cyclic_refresh;
598
599 fractional_mv_step_fp *find_fractional_mv_step;
600 vp9_diamond_search_fn_t diamond_search_sad;
601 vp9_variance_fn_ptr_t fn_ptr[BLOCK_SIZES];
602 uint64_t time_receive_data;
603 uint64_t time_compress_data;
604 uint64_t time_pick_lpf;
605 uint64_t time_encode_sb_row;
606
607 #if CONFIG_FP_MB_STATS
608 int use_fp_mb_stats;
609 #endif
610
611 TWO_PASS twopass;
612
613 // Force recalculation of segment_ids for each mode info
614 uint8_t force_update_segmentation;
615
616 YV12_BUFFER_CONFIG alt_ref_buffer;
617
618 // class responsible for adaptive
619 // quantization of altref frames
620 struct ALT_REF_AQ *alt_ref_aq;
621
622 #if CONFIG_INTERNAL_STATS
623 unsigned int mode_chosen_counts[MAX_MODES];
624
625 int count;
626 uint64_t total_sq_error;
627 uint64_t total_samples;
628 ImageStat psnr;
629
630 uint64_t totalp_sq_error;
631 uint64_t totalp_samples;
632 ImageStat psnrp;
633
634 double total_blockiness;
635 double worst_blockiness;
636
637 int bytes;
638 double summed_quality;
639 double summed_weights;
640 double summedp_quality;
641 double summedp_weights;
642 unsigned int tot_recode_hits;
643 double worst_ssim;
644
645 ImageStat ssimg;
646 ImageStat fastssim;
647 ImageStat psnrhvs;
648
649 int b_calculate_ssimg;
650 int b_calculate_blockiness;
651
652 int b_calculate_consistency;
653
654 double total_inconsistency;
655 double worst_consistency;
656 Ssimv *ssim_vars;
657 Metrics metrics;
658 #endif
659 int b_calculate_psnr;
660
661 int droppable;
662
663 int initial_width;
664 int initial_height;
665 int initial_mbs; // Number of MBs in the full-size frame; to be used to
666 // normalize the firstpass stats. This will differ from the
667 // number of MBs in the current frame when the frame is
668 // scaled.
669 #endif
670
671 int use_svc;
672
673 #if 0
674 SVC svc;
675
676 // Store frame variance info in SOURCE_VAR_BASED_PARTITION search type.
677 diff *source_diff_var;
678 // The threshold used in SOURCE_VAR_BASED_PARTITION search type.
679 unsigned int source_var_thresh;
680 int frames_till_next_var_check;
681
682 int frame_flags;
683
684 search_site_config ss_cfg;
685 #endif
686
687 int mbmode_cost[INTRA_MODES];
688 unsigned int inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES];
689 int intra_uv_mode_cost[FRAME_TYPES][INTRA_MODES][INTRA_MODES];
690 int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
691 int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
692 int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
693 // Indices are: max_tx_size-1, tx_size_ctx, tx_size
694 int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
695 #if 0
696 #if CONFIG_VP9_TEMPORAL_DENOISING
697 VP9_DENOISER denoiser;
698 #endif
699
700 int resize_pending;
701 RESIZE_STATE resize_state;
702 int external_resize;
703 int resize_scale_num;
704 int resize_scale_den;
705 int resize_avg_qp;
706 int resize_buffer_underflow;
707 int resize_count;
708
709 int use_skin_detection;
710
711 int target_level;
712
713 NOISE_ESTIMATE noise_estimate;
714
715 // Count on how many consecutive times a block uses small/zeromv for encoding.
716 uint8_t *consec_zero_mv;
717
718 // VAR_BASED_PARTITION thresholds
719 // 0 - threshold_64x64; 1 - threshold_32x32;
720 // 2 - threshold_16x16; 3 - vbp_threshold_8x8;
721 int64_t vbp_thresholds[4];
722 int64_t vbp_threshold_minmax;
723 int64_t vbp_threshold_sad;
724 // Threshold used for partition copy
725 int64_t vbp_threshold_copy;
726 BLOCK_SIZE vbp_bsize_min;
727
728 // Multi-threading
729 int num_workers;
730 VPxWorker *workers;
731 struct EncWorkerData *tile_thr_data;
732 VP9LfSync lf_row_sync;
733 struct VP9BitstreamWorkerData *vp9_bitstream_worker_data;
734
735 int keep_level_stats;
736 Vp9LevelInfo level_info;
737 MultiThreadHandle multi_thread_ctxt;
738 void (*row_mt_sync_read_ptr)(VP9RowMTSync *const, int, int);
739 void (*row_mt_sync_write_ptr)(VP9RowMTSync *const, int, int, const int);
740 ARNRFilterData arnr_filter_data;
741
742 int row_mt;
743 unsigned int row_mt_bit_exact;
744
745 // Previous Partition Info
746 BLOCK_SIZE *prev_partition;
747 int8_t *prev_segment_id;
748 // Used to save the status of whether a block has a low variance in
749 // choose_partitioning. 0 for 64x64, 1~2 for 64x32, 3~4 for 32x64, 5~8 for
750 // 32x32, 9~24 for 16x16.
751 // This is for the last frame and is copied to the current frame
752 // when partition copy happens.
753 uint8_t *prev_variance_low;
754 uint8_t *copied_frame_cnt;
755 uint8_t max_copied_frame;
756 // If the last frame is dropped, we don't copy partition.
757 uint8_t last_frame_dropped;
758
759 // For each superblock: keeps track of the last time (in frame distance) the
760 // the superblock did not have low source sad.
761 uint8_t *content_state_sb_fd;
762
763 int compute_source_sad_onepass;
764
765 LevelConstraint level_constraint;
766
767 uint8_t *count_arf_frame_usage;
768 uint8_t *count_lastgolden_frame_usage;
769
770 int multi_layer_arf;
771 vpx_roi_map_t roi;
772 #endif
773 } VP9_COMP;
774 #if 0
775 void vp9_initialize_enc(void);
776
777 struct VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
778 BufferPool *const pool);
779 void vp9_remove_compressor(VP9_COMP *cpi);
780
781 void vp9_change_config(VP9_COMP *cpi, const VP9EncoderConfig *oxcf);
782
783 // receive a frames worth of data. caller can assume that a copy of this
784 // frame is made and not just a copy of the pointer..
785 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
786 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
787 int64_t end_time_stamp);
788
789 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
790 size_t *size, uint8_t *dest, int64_t *time_stamp,
791 int64_t *time_end, int flush);
792
793 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
794 vp9_ppflags_t *flags);
795
796 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags);
797
798 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags);
799
800 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
801 YV12_BUFFER_CONFIG *sd);
802
803 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
804 YV12_BUFFER_CONFIG *sd);
805
806 int vp9_update_entropy(VP9_COMP *cpi, int update);
807
808 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols);
809
810 int vp9_get_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols);
811
812 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
813 VPX_SCALING vert_mode);
814
815 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
816 unsigned int height);
817
818 void vp9_set_svc(VP9_COMP *cpi, int use_svc);
819
820 static INLINE int stack_pop(int *stack, int stack_size) {
821 int idx;
822 const int r = stack[0];
823 for (idx = 1; idx < stack_size; ++idx) stack[idx - 1] = stack[idx];
824
825 return r;
826 }
827
828 static INLINE int stack_top(const int *stack) { return stack[0]; }
829
830 static INLINE void stack_push(int *stack, int new_item, int stack_size) {
831 int idx;
832 for (idx = stack_size; idx > 0; --idx) stack[idx] = stack[idx - 1];
833 stack[0] = new_item;
834 }
835
836 static INLINE void stack_init(int *stack, int length) {
837 int idx;
838 for (idx = 0; idx < length; ++idx) stack[idx] = -1;
839 }
840
841 int vp9_get_quantizer(struct VP9_COMP *cpi);
842
843 static INLINE int frame_is_kf_gf_arf(const VP9_COMP *cpi) {
844 return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
845 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
846 }
847 #endif
get_ref_frame_map_idx(const VP9_COMP * cpi,MV_REFERENCE_FRAME ref_frame)848 static INLINE int get_ref_frame_map_idx(const VP9_COMP *cpi,
849 MV_REFERENCE_FRAME ref_frame) {
850 if (ref_frame == LAST_FRAME) {
851 return cpi->lst_fb_idx;
852 } else if (ref_frame == GOLDEN_FRAME) {
853 return cpi->gld_fb_idx;
854 } else {
855 return cpi->alt_fb_idx;
856 }
857 }
858 #if 0
859 static INLINE int get_ref_frame_buf_idx(const VP9_COMP *const cpi,
860 int ref_frame) {
861 const VP9_COMMON *const cm = &cpi->common;
862 const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
863 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX;
864 }
865
866 static INLINE RefCntBuffer *get_ref_cnt_buffer(VP9_COMMON *cm, int fb_idx) {
867 return fb_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[fb_idx] : NULL;
868 }
869
870 static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
871 VP9_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
872 VP9_COMMON *const cm = &cpi->common;
873 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
874 return buf_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[buf_idx].buf
875 : NULL;
876 }
877 #endif
get_token_alloc(int mb_rows,int mb_cols)878 static INLINE int get_token_alloc(int mb_rows, int mb_cols) {
879 // TODO(JBB): double check we can't exceed this token count if we have a
880 // 32x32 transform crossing a boundary at a multiple of 16.
881 // mb_rows, cols are in units of 16 pixels. We assume 3 planes all at full
882 // resolution. We assume up to 1 token per pixel, and then allow
883 // a head room of 4.
884 return mb_rows * mb_cols * (16 * 16 * 3 + 4);
885 }
886 #if 0
887 // Get the allocated token size for a tile. It does the same calculation as in
888 // the frame token allocation.
889 static INLINE int allocated_tokens(TileInfo tile) {
890 int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 1) >> 1;
891 int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 1) >> 1;
892
893 return get_token_alloc(tile_mb_rows, tile_mb_cols);
894 }
895
896 static INLINE void get_start_tok(VP9_COMP *cpi, int tile_row, int tile_col,
897 int mi_row, TOKENEXTRA **tok) {
898 VP9_COMMON *const cm = &cpi->common;
899 const int tile_cols = 1 << cm->log2_tile_cols;
900 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
901 const TileInfo *const tile_info = &this_tile->tile_info;
902
903 int tile_mb_cols = (tile_info->mi_col_end - tile_info->mi_col_start + 1) >> 1;
904 const int mb_row = (mi_row - tile_info->mi_row_start) >> 1;
905
906 *tok =
907 cpi->tile_tok[tile_row][tile_col] + get_token_alloc(mb_row, tile_mb_cols);
908 }
909
910 int64_t vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b);
911 #if CONFIG_VP9_HIGHBITDEPTH
912 int64_t vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
913 const YV12_BUFFER_CONFIG *b);
914 #endif // CONFIG_VP9_HIGHBITDEPTH
915
916 void vp9_scale_references(VP9_COMP *cpi);
917
918 void vp9_update_reference_frames(VP9_COMP *cpi);
919
920 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv);
921
922 YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(
923 VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
924 YV12_BUFFER_CONFIG *scaled_temp, INTERP_FILTER filter_type,
925 int phase_scaler, INTERP_FILTER filter_type2, int phase_scaler2);
926
927 YV12_BUFFER_CONFIG *vp9_scale_if_required(
928 VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
929 int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler);
930
931 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags);
932
933 static INLINE int is_one_pass_cbr_svc(const struct VP9_COMP *const cpi) {
934 return (cpi->use_svc && cpi->oxcf.pass == 0);
935 }
936
937 #if CONFIG_VP9_TEMPORAL_DENOISING
938 static INLINE int denoise_svc(const struct VP9_COMP *const cpi) {
939 return (!cpi->use_svc || (cpi->use_svc && cpi->svc.spatial_layer_id >=
940 cpi->svc.first_layer_denoise));
941 }
942 #endif
943
944 #define MIN_LOOKAHEAD_FOR_ARFS 4
945 static INLINE int is_altref_enabled(const VP9_COMP *const cpi) {
946 return !(cpi->oxcf.mode == REALTIME && cpi->oxcf.rc_mode == VPX_CBR) &&
947 cpi->oxcf.lag_in_frames >= MIN_LOOKAHEAD_FOR_ARFS &&
948 cpi->oxcf.enable_auto_arf;
949 }
950
951 static INLINE void set_ref_ptrs(VP9_COMMON *cm, MACROBLOCKD *xd,
952 MV_REFERENCE_FRAME ref0,
953 MV_REFERENCE_FRAME ref1) {
954 xd->block_refs[0] =
955 &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME : 0];
956 xd->block_refs[1] =
957 &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME : 0];
958 }
959
960 static INLINE int get_chessboard_index(const int frame_index) {
961 return frame_index & 0x1;
962 }
963
964 static INLINE int *cond_cost_list(const struct VP9_COMP *cpi, int *cost_list) {
965 return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
966 }
967
968 static INLINE int get_num_vert_units(TileInfo tile, int shift) {
969 int num_vert_units =
970 (tile.mi_row_end - tile.mi_row_start + (1 << shift) - 1) >> shift;
971 return num_vert_units;
972 }
973
974 static INLINE int get_num_cols(TileInfo tile, int shift) {
975 int num_cols =
976 (tile.mi_col_end - tile.mi_col_start + (1 << shift) - 1) >> shift;
977 return num_cols;
978 }
979
980 static INLINE int get_level_index(VP9_LEVEL level) {
981 int i;
982 for (i = 0; i < VP9_LEVELS; ++i) {
983 if (level == vp9_level_defs[i].level) return i;
984 }
985 return -1;
986 }
987
988 // Return the log2 value of max column tiles corresponding to the level that
989 // the picture size fits into.
990 static INLINE int log_tile_cols_from_picsize_level(uint32_t width,
991 uint32_t height) {
992 int i;
993 const uint32_t pic_size = width * height;
994 const uint32_t pic_breadth = VPXMAX(width, height);
995 for (i = LEVEL_1; i < LEVEL_MAX; ++i) {
996 if (vp9_level_defs[i].max_luma_picture_size >= pic_size &&
997 vp9_level_defs[i].max_luma_picture_breadth >= pic_breadth) {
998 return get_msb(vp9_level_defs[i].max_col_tiles);
999 }
1000 }
1001 return INT_MAX;
1002 }
1003
1004 VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec);
1005
1006 int vp9_set_roi_map(VP9_COMP *cpi, unsigned char *map, unsigned int rows,
1007 unsigned int cols, int delta_q[8], int delta_lf[8],
1008 int skip[8], int ref_frame[8]);
1009
1010 void vp9_new_framerate(VP9_COMP *cpi, double frame_rate);
1011
1012 void vp9_set_row_mt(VP9_COMP *cpi);
1013
1014 #define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
1015 #endif
1016
1017 /***********************************************************************
1018 * Read before modifying 'cal_nmvjointsadcost' or 'cal_nmvsadcosts' *
1019 ***********************************************************************
1020 * The following 2 functions ('cal_nmvjointsadcost' and *
1021 * 'cal_nmvsadcosts') are used to calculate cost lookup tables *
1022 * used by 'eb_vp9_diamond_search_sad'. The C implementation of the *
1023 * function is generic, but the AVX intrinsics optimised version *
1024 * relies on the following properties of the computed tables: *
1025 * For cal_nmvjointsadcost: *
1026 * - mvjointsadcost[1] == mvjointsadcost[2] == mvjointsadcost[3] *
1027 * For cal_nmvsadcosts: *
1028 * - For all i: mvsadcost[0][i] == mvsadcost[1][i] *
1029 * (Equal costs for both components) *
1030 * - For all i: mvsadcost[0][i] == mvsadcost[0][-i] *
1031 * (Cost function is even) *
1032 * If these do not hold, then the AVX optimised version of the *
1033 * 'eb_vp9_diamond_search_sad' function cannot be used as it is, in which *
1034 * case you can revert to using the C function instead. *
1035 ***********************************************************************/
1036
1037 void cal_nmvjointsadcost(int *mvjointsadcost);
1038
1039 void cal_nmvsadcosts(int *mvsadcost[2]);
1040
1041 void cal_nmvsadcosts_hp(int *mvsadcost[2]);
1042
1043 #ifdef __cplusplus
1044 } // extern "C"
1045 #endif
1046
1047 #endif // VPX_VP9_ENCODER_VP9_ENCODER_H_
1048