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
16 #include "./vpx_config.h"
17 #include "./vpx_dsp_rtcd.h"
18 #include "vpx/internal/vpx_codec_internal.h"
19 #include "vpx/vpx_ext_ratectrl.h"
20 #include "vpx/vp8cx.h"
21 #include "vpx/vpx_tpl.h"
22 #if CONFIG_INTERNAL_STATS
23 #include "vpx_dsp/ssim.h"
24 #endif
25 #include "vpx_dsp/variance.h"
26 #include "vpx_dsp/psnr.h"
27 #include "vpx_ports/system_state.h"
28 #include "vpx_util/vpx_pthread.h"
29 #include "vpx_util/vpx_thread.h"
30 #include "vpx_util/vpx_timestamp.h"
31
32 #include "vp9/common/vp9_alloccommon.h"
33 #include "vp9/common/vp9_ppflags.h"
34 #include "vp9/common/vp9_entropymode.h"
35 #include "vp9/common/vp9_thread_common.h"
36 #include "vp9/common/vp9_onyxc_int.h"
37
38 #if !CONFIG_REALTIME_ONLY
39 #include "vp9/encoder/vp9_alt_ref_aq.h"
40 #endif
41 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
42 #include "vp9/encoder/vp9_context_tree.h"
43 #include "vp9/encoder/vp9_encodemb.h"
44 #include "vp9/encoder/vp9_ethread.h"
45 #include "vp9/encoder/vp9_ext_ratectrl.h"
46 #include "vp9/encoder/vp9_firstpass.h"
47 #include "vp9/encoder/vp9_job_queue.h"
48 #include "vp9/encoder/vp9_lookahead.h"
49 #include "vp9/encoder/vp9_mbgraph.h"
50 #include "vp9/encoder/vp9_mcomp.h"
51 #include "vp9/encoder/vp9_noise_estimate.h"
52 #include "vp9/encoder/vp9_quantize.h"
53 #include "vp9/encoder/vp9_ratectrl.h"
54 #include "vp9/encoder/vp9_rd.h"
55 #include "vp9/encoder/vp9_speed_features.h"
56 #include "vp9/encoder/vp9_svc_layercontext.h"
57 #include "vp9/encoder/vp9_tokenize.h"
58
59 #if CONFIG_VP9_TEMPORAL_DENOISING
60 #include "vp9/encoder/vp9_denoiser.h"
61 #endif
62
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66
67 // vp9 uses 10,000,000 ticks/second as time stamp
68 #define TICKS_PER_SEC 10000000
69
70 typedef struct {
71 int nmvjointcost[MV_JOINTS];
72 int nmvcosts[2][MV_VALS];
73 int nmvcosts_hp[2][MV_VALS];
74
75 vpx_prob segment_pred_probs[PREDICTION_PROBS];
76
77 unsigned char *last_frame_seg_map_copy;
78
79 // 0 = Intra, Last, GF, ARF
80 signed char last_ref_lf_deltas[MAX_REF_LF_DELTAS];
81 // 0 = ZERO_MV, MV
82 signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
83
84 FRAME_CONTEXT fc;
85 } CODING_CONTEXT;
86
87 typedef enum {
88 // encode_breakout is disabled.
89 ENCODE_BREAKOUT_DISABLED = 0,
90 // encode_breakout is enabled.
91 ENCODE_BREAKOUT_ENABLED = 1,
92 // encode_breakout is enabled with small max_thresh limit.
93 ENCODE_BREAKOUT_LIMITED = 2
94 } ENCODE_BREAKOUT_TYPE;
95
96 typedef enum {
97 // Good Quality Fast Encoding. The encoder balances quality with the amount of
98 // time it takes to encode the output. Speed setting controls how fast.
99 GOOD,
100
101 // The encoder places priority on the quality of the output over encoding
102 // speed. The output is compressed at the highest possible quality. This
103 // option takes the longest amount of time to encode. Speed setting ignored.
104 BEST,
105
106 // Realtime/Live Encoding. This mode is optimized for realtime encoding (for
107 // example, capturing a television signal or feed from a live camera). Speed
108 // setting controls how fast.
109 REALTIME
110 } MODE;
111
112 typedef enum {
113 FRAMEFLAGS_KEY = 1 << 0,
114 FRAMEFLAGS_GOLDEN = 1 << 1,
115 FRAMEFLAGS_ALTREF = 1 << 2,
116 } FRAMETYPE_FLAGS;
117
118 typedef enum {
119 NO_AQ = 0,
120 VARIANCE_AQ = 1,
121 COMPLEXITY_AQ = 2,
122 CYCLIC_REFRESH_AQ = 3,
123 EQUATOR360_AQ = 4,
124 PERCEPTUAL_AQ = 5,
125 PSNR_AQ = 6,
126 // AQ based on lookahead temporal
127 // variance (only valid for altref frames)
128 LOOKAHEAD_AQ = 7,
129 AQ_MODE_COUNT // This should always be the last member of the enum
130 } AQ_MODE;
131
132 typedef enum {
133 RESIZE_NONE = 0, // No frame resizing allowed (except for SVC).
134 RESIZE_FIXED = 1, // All frames are coded at the specified dimension.
135 RESIZE_DYNAMIC = 2 // Coded size of each frame is determined by the codec.
136 } RESIZE_TYPE;
137
138 typedef enum {
139 kInvalid = 0,
140 kLowSadLowSumdiff = 1,
141 kLowSadHighSumdiff = 2,
142 kHighSadLowSumdiff = 3,
143 kHighSadHighSumdiff = 4,
144 kLowVarHighSumdiff = 5,
145 kVeryHighSad = 6,
146 } CONTENT_STATE_SB;
147
148 typedef enum {
149 LOOPFILTER_ALL = 0,
150 LOOPFILTER_REFERENCE = 1, // Disable loopfilter on non reference frames.
151 NO_LOOPFILTER = 2, // Disable loopfilter on all frames.
152 } LOOPFILTER_CONTROL;
153
154 typedef struct VP9EncoderConfig {
155 BITSTREAM_PROFILE profile;
156 vpx_bit_depth_t bit_depth; // Codec bit-depth.
157 int width; // width of data passed to the compressor
158 int height; // height of data passed to the compressor
159 unsigned int input_bit_depth; // Input bit depth.
160 double init_framerate; // set to passed in framerate
161 vpx_rational_t g_timebase; // equivalent to g_timebase in vpx_codec_enc_cfg_t
162 vpx_rational64_t g_timebase_in_ts; // g_timebase * TICKS_PER_SEC
163
164 int64_t target_bandwidth; // bandwidth to be used in bits per second
165
166 int noise_sensitivity; // pre processing blur: recommendation 0
167 int sharpness; // sharpening output: recommendation 0:
168 int speed;
169 // maximum allowed bitrate for any intra frame in % of bitrate target.
170 unsigned int rc_max_intra_bitrate_pct;
171 // maximum allowed bitrate for any inter frame in % of bitrate target.
172 unsigned int rc_max_inter_bitrate_pct;
173 // percent of rate boost for golden frame in CBR mode.
174 unsigned int gf_cbr_boost_pct;
175
176 MODE mode;
177 int pass;
178
179 // Key Framing Operations
180 int auto_key; // autodetect cut scenes and set the keyframes
181 int key_freq; // maximum distance to key frame.
182
183 int lag_in_frames; // how many frames lag before we start encoding
184
185 // ----------------------------------------------------------------
186 // DATARATE CONTROL OPTIONS
187
188 // vbr, cbr, constrained quality or constant quality
189 enum vpx_rc_mode rc_mode;
190
191 // buffer targeting aggressiveness
192 int under_shoot_pct;
193 int over_shoot_pct;
194
195 // buffering parameters
196 int64_t starting_buffer_level_ms;
197 int64_t optimal_buffer_level_ms;
198 int64_t maximum_buffer_size_ms;
199
200 // Frame drop threshold.
201 int drop_frames_water_mark;
202
203 // controlling quality
204 int fixed_q;
205 int worst_allowed_q;
206 int best_allowed_q;
207 int cq_level;
208 AQ_MODE aq_mode; // Adaptive Quantization mode
209
210 // Special handling of Adaptive Quantization for AltRef frames
211 int alt_ref_aq;
212
213 // Internal frame size scaling.
214 RESIZE_TYPE resize_mode;
215 int scaled_frame_width;
216 int scaled_frame_height;
217
218 // Enable feature to reduce the frame quantization every x frames.
219 int frame_periodic_boost;
220
221 // two pass datarate control
222 int two_pass_vbrbias; // two pass datarate control tweaks
223 int two_pass_vbrmin_section;
224 int two_pass_vbrmax_section;
225 int vbr_corpus_complexity; // 0 indicates corpus vbr disabled
226 // END DATARATE CONTROL OPTIONS
227 // ----------------------------------------------------------------
228
229 // Spatial and temporal scalability.
230 int ss_number_layers; // Number of spatial layers.
231 int ts_number_layers; // Number of temporal layers.
232 // Bitrate allocation for spatial layers.
233 int layer_target_bitrate[VPX_MAX_LAYERS];
234 int ss_target_bitrate[VPX_SS_MAX_LAYERS];
235 int ss_enable_auto_arf[VPX_SS_MAX_LAYERS];
236 // Bitrate allocation (CBR mode) and framerate factor, for temporal layers.
237 int ts_rate_decimator[VPX_TS_MAX_LAYERS];
238
239 int enable_auto_arf;
240
241 int encode_breakout; // early breakout : for video conf recommend 800
242
243 /* Bitfield defining the error resiliency features to enable.
244 * Can provide decodable frames after losses in previous
245 * frames and decodable partitions after losses in the same frame.
246 */
247 unsigned int error_resilient_mode;
248
249 /* Bitfield defining the parallel decoding mode where the
250 * decoding in successive frames may be conducted in parallel
251 * just by decoding the frame headers.
252 */
253 unsigned int frame_parallel_decoding_mode;
254
255 int arnr_max_frames;
256 int arnr_strength;
257
258 int min_gf_interval;
259 int max_gf_interval;
260
261 int tile_columns;
262 int tile_rows;
263
264 int enable_tpl_model;
265
266 int enable_keyframe_filtering;
267
268 int max_threads;
269
270 unsigned int target_level;
271
272 vpx_fixed_buf_t two_pass_stats_in;
273
274 vp8e_tuning tuning;
275 vp9e_tune_content content;
276 #if CONFIG_VP9_HIGHBITDEPTH
277 int use_highbitdepth;
278 #endif
279 vpx_color_space_t color_space;
280 vpx_color_range_t color_range;
281 int render_width;
282 int render_height;
283 VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode;
284
285 int row_mt;
286 unsigned int motion_vector_unit_test;
287 int delta_q_uv;
288 int use_simple_encode_api; // Use SimpleEncode APIs or not
289 } VP9EncoderConfig;
290
is_lossless_requested(const VP9EncoderConfig * cfg)291 static INLINE int is_lossless_requested(const VP9EncoderConfig *cfg) {
292 return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
293 }
294
295 typedef struct TplDepStats {
296 int64_t intra_cost;
297 int64_t inter_cost;
298 int64_t mc_flow;
299 int64_t mc_dep_cost;
300 int64_t mc_ref_cost;
301
302 int ref_frame_index;
303 int_mv mv;
304 } TplDepStats;
305
306 #if CONFIG_NON_GREEDY_MV
307
308 #define ZERO_MV_MODE 0
309 #define NEW_MV_MODE 1
310 #define NEAREST_MV_MODE 2
311 #define NEAR_MV_MODE 3
312 #define MAX_MV_MODE 4
313 #endif
314
315 typedef struct TplDepFrame {
316 uint8_t is_valid;
317 TplDepStats *tpl_stats_ptr;
318 int stride;
319 int width;
320 int height;
321 int mi_rows;
322 int mi_cols;
323 int base_qindex;
324 #if CONFIG_NON_GREEDY_MV
325 int lambda;
326 int *mv_mode_arr[3];
327 double *rd_diff_arr[3];
328 #endif
329 } TplDepFrame;
330
331 #define TPL_DEP_COST_SCALE_LOG2 4
332
333 // TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
334 typedef struct TileDataEnc {
335 TileInfo tile_info;
336 int thresh_freq_fact[BLOCK_SIZES][MAX_MODES];
337 int thresh_freq_fact_prev[BLOCK_SIZES][MAX_MODES];
338 int8_t mode_map[BLOCK_SIZES][MAX_MODES];
339 FIRSTPASS_DATA fp_data;
340 VP9RowMTSync row_mt_sync;
341
342 // Used for adaptive_rd_thresh with row multithreading
343 int *row_base_thresh_freq_fact;
344 // The value of sb_rows when row_base_thresh_freq_fact is allocated.
345 // The row_base_thresh_freq_fact array has sb_rows * BLOCK_SIZES * MAX_MODES
346 // elements.
347 int sb_rows;
348 MV firstpass_top_mv;
349 } TileDataEnc;
350
351 typedef struct RowMTInfo {
352 JobQueueHandle job_queue_hdl;
353 #if CONFIG_MULTITHREAD
354 pthread_mutex_t job_mutex;
355 #endif
356 } RowMTInfo;
357
358 typedef struct {
359 TOKENEXTRA *start;
360 TOKENEXTRA *stop;
361 unsigned int count;
362 } TOKENLIST;
363
364 typedef struct MultiThreadHandle {
365 int allocated_tile_rows;
366 int allocated_tile_cols;
367 int allocated_vert_unit_rows;
368
369 // Frame level params
370 int num_tile_vert_sbs[MAX_NUM_TILE_ROWS];
371
372 // Job Queue structure and handles
373 JobQueue *job_queue;
374
375 int jobs_per_tile_col;
376
377 RowMTInfo row_mt_info[MAX_NUM_TILE_COLS];
378 int thread_id_to_tile_id[MAX_NUM_THREADS]; // Mapping of threads to tiles
379 } MultiThreadHandle;
380
381 typedef struct RD_COUNTS {
382 vp9_coeff_count coef_counts[TX_SIZES][PLANE_TYPES];
383 int64_t comp_pred_diff[REFERENCE_MODES];
384 int64_t filter_diff[SWITCHABLE_FILTER_CONTEXTS];
385 } RD_COUNTS;
386
387 typedef struct ThreadData {
388 MACROBLOCK mb;
389 RD_COUNTS rd_counts;
390 FRAME_COUNTS *counts;
391
392 PICK_MODE_CONTEXT *leaf_tree;
393 PC_TREE *pc_tree;
394 PC_TREE *pc_root;
395 } ThreadData;
396
397 struct EncWorkerData;
398
399 typedef struct ActiveMap {
400 int enabled;
401 int update;
402 unsigned char *map;
403 } ActiveMap;
404
405 typedef enum { Y, U, V, ALL } STAT_TYPE;
406
407 typedef struct IMAGE_STAT {
408 double stat[ALL + 1];
409 double worst;
410 } ImageStat;
411
412 // Kf noise filtering currently disabled by default in build.
413 // #define ENABLE_KF_DENOISE 1
414
415 #define CPB_WINDOW_SIZE 4
416 #define FRAME_WINDOW_SIZE 128
417 #define SAMPLE_RATE_GRACE_P 0.015
418 #define VP9_LEVELS 14
419
420 typedef enum {
421 LEVEL_UNKNOWN = 0,
422 LEVEL_AUTO = 1,
423 LEVEL_1 = 10,
424 LEVEL_1_1 = 11,
425 LEVEL_2 = 20,
426 LEVEL_2_1 = 21,
427 LEVEL_3 = 30,
428 LEVEL_3_1 = 31,
429 LEVEL_4 = 40,
430 LEVEL_4_1 = 41,
431 LEVEL_5 = 50,
432 LEVEL_5_1 = 51,
433 LEVEL_5_2 = 52,
434 LEVEL_6 = 60,
435 LEVEL_6_1 = 61,
436 LEVEL_6_2 = 62,
437 LEVEL_MAX = 255
438 } VP9_LEVEL;
439
440 typedef struct {
441 VP9_LEVEL level;
442 uint64_t max_luma_sample_rate;
443 uint32_t max_luma_picture_size;
444 uint32_t max_luma_picture_breadth;
445 double average_bitrate; // in kilobits per second
446 double max_cpb_size; // in kilobits
447 double compression_ratio;
448 uint8_t max_col_tiles;
449 uint32_t min_altref_distance;
450 uint8_t max_ref_frame_buffers;
451 } Vp9LevelSpec;
452
453 extern const Vp9LevelSpec vp9_level_defs[VP9_LEVELS];
454
455 typedef struct {
456 int64_t ts; // timestamp
457 uint32_t luma_samples;
458 uint32_t size; // in bytes
459 } FrameRecord;
460
461 typedef struct {
462 FrameRecord buf[FRAME_WINDOW_SIZE];
463 uint8_t start;
464 uint8_t len;
465 } FrameWindowBuffer;
466
467 typedef struct {
468 uint8_t seen_first_altref;
469 uint32_t frames_since_last_altref;
470 uint64_t total_compressed_size;
471 uint64_t total_uncompressed_size;
472 double time_encoded; // in seconds
473 FrameWindowBuffer frame_window_buffer;
474 int ref_refresh_map;
475 } Vp9LevelStats;
476
477 typedef struct {
478 Vp9LevelStats level_stats;
479 Vp9LevelSpec level_spec;
480 } Vp9LevelInfo;
481
482 typedef enum {
483 BITRATE_TOO_LARGE = 0,
484 LUMA_PIC_SIZE_TOO_LARGE,
485 LUMA_PIC_BREADTH_TOO_LARGE,
486 LUMA_SAMPLE_RATE_TOO_LARGE,
487 CPB_TOO_LARGE,
488 COMPRESSION_RATIO_TOO_SMALL,
489 TOO_MANY_COLUMN_TILE,
490 ALTREF_DIST_TOO_SMALL,
491 TOO_MANY_REF_BUFFER,
492 TARGET_LEVEL_FAIL_IDS
493 } TARGET_LEVEL_FAIL_ID;
494
495 typedef struct {
496 int8_t level_index;
497 uint8_t fail_flag;
498 int max_frame_size; // in bits
499 double max_cpb_size; // in bits
500 } LevelConstraint;
501
502 typedef struct ARNRFilterData {
503 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
504 int strength;
505 int frame_count;
506 int alt_ref_index;
507 struct scale_factors sf;
508 YV12_BUFFER_CONFIG *dst;
509 } ARNRFilterData;
510
511 typedef struct EncFrameBuf {
512 int mem_valid;
513 int released;
514 YV12_BUFFER_CONFIG frame;
515 } EncFrameBuf;
516
517 // Maximum operating frame buffer size needed for a GOP using ARF reference.
518 // This is used to allocate the memory for TPL stats for a GOP.
519 #define MAX_ARF_GOP_SIZE (2 * MAX_LAG_BUFFERS)
520 #define MAX_KMEANS_GROUPS 8
521
522 typedef struct KMEANS_DATA {
523 double value;
524 int pos;
525 int group_idx;
526 } KMEANS_DATA;
527
528 #if CONFIG_RATE_CTRL
529 typedef struct PARTITION_INFO {
530 int row; // row pixel offset of current 4x4 block
531 int column; // column pixel offset of current 4x4 block
532 int row_start; // row pixel offset of the start of the prediction block
533 int column_start; // column pixel offset of the start of the prediction block
534 int width; // prediction block width
535 int height; // prediction block height
536 } PARTITION_INFO;
537
538 typedef struct MOTION_VECTOR_INFO {
539 MV_REFERENCE_FRAME ref_frame[2];
540 int_mv mv[2];
541 } MOTION_VECTOR_INFO;
542
543 typedef struct GOP_COMMAND {
544 int use; // use this command to set gop or not. If not, use vp9's decision.
545 int show_frame_count;
546 int use_alt_ref;
547 } GOP_COMMAND;
548
gop_command_on(GOP_COMMAND * gop_command,int show_frame_count,int use_alt_ref)549 static INLINE void gop_command_on(GOP_COMMAND *gop_command,
550 int show_frame_count, int use_alt_ref) {
551 gop_command->use = 1;
552 gop_command->show_frame_count = show_frame_count;
553 gop_command->use_alt_ref = use_alt_ref;
554 }
555
gop_command_off(GOP_COMMAND * gop_command)556 static INLINE void gop_command_off(GOP_COMMAND *gop_command) {
557 gop_command->use = 0;
558 gop_command->show_frame_count = 0;
559 gop_command->use_alt_ref = 0;
560 }
561
gop_command_coding_frame_count(const GOP_COMMAND * gop_command)562 static INLINE int gop_command_coding_frame_count(
563 const GOP_COMMAND *gop_command) {
564 if (gop_command->use == 0) {
565 assert(0);
566 return -1;
567 }
568 return gop_command->show_frame_count + gop_command->use_alt_ref;
569 }
570
571 // TODO(angiebird): See if we can merge this one with FrameType in
572 // simple_encode.h
573 typedef enum ENCODE_FRAME_TYPE {
574 ENCODE_FRAME_TYPE_KEY,
575 ENCODE_FRAME_TYPE_INTER,
576 ENCODE_FRAME_TYPE_ALTREF,
577 ENCODE_FRAME_TYPE_OVERLAY,
578 ENCODE_FRAME_TYPE_GOLDEN,
579 ENCODE_FRAME_TYPES,
580 } ENCODE_FRAME_TYPE;
581
582 // TODO(angiebird): Merge this function with get_frame_type_from_update_type()
583 static INLINE ENCODE_FRAME_TYPE
get_encode_frame_type(FRAME_UPDATE_TYPE update_type)584 get_encode_frame_type(FRAME_UPDATE_TYPE update_type) {
585 switch (update_type) {
586 case KF_UPDATE: return ENCODE_FRAME_TYPE_KEY;
587 case ARF_UPDATE: return ENCODE_FRAME_TYPE_ALTREF;
588 case GF_UPDATE: return ENCODE_FRAME_TYPE_GOLDEN;
589 case OVERLAY_UPDATE: return ENCODE_FRAME_TYPE_OVERLAY;
590 case LF_UPDATE: return ENCODE_FRAME_TYPE_INTER;
591 default:
592 fprintf(stderr, "Unsupported update_type %d\n", update_type);
593 abort();
594 return ENCODE_FRAME_TYPE_INTER;
595 }
596 }
597
598 typedef struct RATE_QSTEP_MODEL {
599 // The rq model predicts the bit usage as follows.
600 // rate = bias - ratio * log2(q_step)
601 int ready;
602 double bias;
603 double ratio;
604 } RATE_QSTEP_MODEL;
605
606 typedef struct ENCODE_COMMAND {
607 int use_external_quantize_index;
608 int external_quantize_index;
609
610 int use_external_target_frame_bits;
611 int target_frame_bits;
612 double target_frame_bits_error_percent;
613
614 GOP_COMMAND gop_command;
615 } ENCODE_COMMAND;
616
encode_command_set_gop_command(ENCODE_COMMAND * encode_command,GOP_COMMAND gop_command)617 static INLINE void encode_command_set_gop_command(
618 ENCODE_COMMAND *encode_command, GOP_COMMAND gop_command) {
619 encode_command->gop_command = gop_command;
620 }
621
encode_command_set_external_quantize_index(ENCODE_COMMAND * encode_command,int quantize_index)622 static INLINE void encode_command_set_external_quantize_index(
623 ENCODE_COMMAND *encode_command, int quantize_index) {
624 encode_command->use_external_quantize_index = 1;
625 encode_command->external_quantize_index = quantize_index;
626 }
627
encode_command_reset_external_quantize_index(ENCODE_COMMAND * encode_command)628 static INLINE void encode_command_reset_external_quantize_index(
629 ENCODE_COMMAND *encode_command) {
630 encode_command->use_external_quantize_index = 0;
631 encode_command->external_quantize_index = -1;
632 }
633
encode_command_set_target_frame_bits(ENCODE_COMMAND * encode_command,int target_frame_bits,double target_frame_bits_error_percent)634 static INLINE void encode_command_set_target_frame_bits(
635 ENCODE_COMMAND *encode_command, int target_frame_bits,
636 double target_frame_bits_error_percent) {
637 encode_command->use_external_target_frame_bits = 1;
638 encode_command->target_frame_bits = target_frame_bits;
639 encode_command->target_frame_bits_error_percent =
640 target_frame_bits_error_percent;
641 }
642
encode_command_reset_target_frame_bits(ENCODE_COMMAND * encode_command)643 static INLINE void encode_command_reset_target_frame_bits(
644 ENCODE_COMMAND *encode_command) {
645 encode_command->use_external_target_frame_bits = 0;
646 encode_command->target_frame_bits = -1;
647 encode_command->target_frame_bits_error_percent = 0;
648 }
649
encode_command_init(ENCODE_COMMAND * encode_command)650 static INLINE void encode_command_init(ENCODE_COMMAND *encode_command) {
651 vp9_zero(*encode_command);
652 encode_command_reset_external_quantize_index(encode_command);
653 encode_command_reset_target_frame_bits(encode_command);
654 gop_command_off(&encode_command->gop_command);
655 }
656
657 // Returns number of units in size of 4, if not multiple not a multiple of 4,
658 // round it up. For example, size is 7, return 2.
get_num_unit_4x4(int size)659 static INLINE int get_num_unit_4x4(int size) { return (size + 3) >> 2; }
660 // Returns number of units in size of 16, if not multiple not a multiple of 16,
661 // round it up. For example, size is 17, return 2.
get_num_unit_16x16(int size)662 static INLINE int get_num_unit_16x16(int size) { return (size + 15) >> 4; }
663 #endif // CONFIG_RATE_CTRL
664
665 #if CONFIG_COLLECT_COMPONENT_TIMING
666 #include "vpx_ports/vpx_timer.h"
667 // Adjust the following to add new components.
668 typedef enum {
669 vp9_get_compressed_data_time,
670 vp9_temporal_filter_time,
671 vp9_rc_get_second_pass_params_time,
672 setup_tpl_stats_time,
673 Pass2Encode_time,
674
675 encode_with_recode_loop_time,
676 loopfilter_frame_time,
677 vp9_pack_bitstream_time,
678
679 encode_frame_internal_time,
680 rd_pick_partition_time,
681 rd_pick_sb_modes_time,
682 encode_sb_time,
683
684 vp9_rd_pick_inter_mode_sb_time,
685 vp9_rd_pick_inter_mode_sub8x8_time,
686
687 intra_mode_search_time,
688 handle_inter_mode_time,
689 single_motion_search_time,
690 joint_motion_search_time,
691 interp_filter_time,
692
693 kTimingComponents,
694 } TIMING_COMPONENT;
695
get_component_name(int index)696 static INLINE char const *get_component_name(int index) {
697 switch (index) {
698 case vp9_get_compressed_data_time: return "vp9_get_compressed_data_time";
699 case vp9_temporal_filter_time: return "vp9_temporal_filter_time";
700 case vp9_rc_get_second_pass_params_time:
701 return "vp9_rc_get_second_pass_params_time";
702 case setup_tpl_stats_time: return "setup_tpl_stats_time";
703 case Pass2Encode_time: return "Pass2Encode_time";
704
705 case encode_with_recode_loop_time: return "encode_with_recode_loop_time";
706 case loopfilter_frame_time: return "loopfilter_frame_time";
707 case vp9_pack_bitstream_time: return "vp9_pack_bitstream_time";
708
709 case encode_frame_internal_time: return "encode_frame_internal_time";
710 case rd_pick_partition_time: return "rd_pick_partition_time";
711 case rd_pick_sb_modes_time: return "rd_pick_sb_modes_time";
712 case encode_sb_time: return "encode_sb_time";
713
714 case vp9_rd_pick_inter_mode_sb_time:
715 return "vp9_rd_pick_inter_mode_sb_time";
716 case vp9_rd_pick_inter_mode_sub8x8_time:
717 return "vp9_rd_pick_inter_mode_sub8x8_time";
718
719 case intra_mode_search_time: return "intra_mode_search_time";
720 case handle_inter_mode_time: return "handle_inter_mode_time";
721 case single_motion_search_time: return "single_motion_search_time";
722 case joint_motion_search_time: return "joint_motion_search_time";
723 case interp_filter_time: return "interp_filter_time";
724
725 default: assert(0);
726 }
727 return "error";
728 }
729 #endif
730
731 typedef struct VP9_COMP {
732 FRAME_INFO frame_info;
733 QUANTS quants;
734 ThreadData td;
735 MB_MODE_INFO_EXT *mbmi_ext_base;
736 DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]);
737 DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]);
738 VP9_COMMON common;
739 VP9EncoderConfig oxcf;
740 struct lookahead_ctx *lookahead;
741 struct lookahead_entry *alt_ref_source;
742
743 YV12_BUFFER_CONFIG *Source;
744 YV12_BUFFER_CONFIG *Last_Source; // NULL for first frame and alt_ref frames
745 YV12_BUFFER_CONFIG *un_scaled_source;
746 YV12_BUFFER_CONFIG scaled_source;
747 YV12_BUFFER_CONFIG *unscaled_last_source;
748 YV12_BUFFER_CONFIG scaled_last_source;
749 #ifdef ENABLE_KF_DENOISE
750 YV12_BUFFER_CONFIG raw_unscaled_source;
751 YV12_BUFFER_CONFIG raw_scaled_source;
752 #endif
753 YV12_BUFFER_CONFIG *raw_source_frame;
754
755 BLOCK_SIZE tpl_bsize;
756 TplDepFrame tpl_stats[MAX_ARF_GOP_SIZE];
757 // Used to store TPL stats before propagation
758 VpxTplGopStats tpl_gop_stats;
759 YV12_BUFFER_CONFIG *tpl_recon_frames[REF_FRAMES];
760 EncFrameBuf enc_frame_buf[REF_FRAMES];
761 #if CONFIG_MULTITHREAD
762 pthread_mutex_t kmeans_mutex;
763 #endif
764 int kmeans_data_arr_alloc;
765 KMEANS_DATA *kmeans_data_arr;
766 int kmeans_data_size;
767 int kmeans_data_stride;
768 double kmeans_ctr_ls[MAX_KMEANS_GROUPS];
769 double kmeans_boundary_ls[MAX_KMEANS_GROUPS];
770 int kmeans_count_ls[MAX_KMEANS_GROUPS];
771 int kmeans_ctr_num;
772 #if CONFIG_NON_GREEDY_MV
773 MotionFieldInfo motion_field_info;
774 int tpl_ready;
775 int_mv *select_mv_arr;
776 #endif
777
778 TileDataEnc *tile_data;
779 int allocated_tiles; // Keep track of memory allocated for tiles.
780
781 int scaled_ref_idx[REFS_PER_FRAME];
782 int lst_fb_idx;
783 int gld_fb_idx;
784 int alt_fb_idx;
785
786 int ref_fb_idx[REF_FRAMES];
787
788 int refresh_last_frame;
789 int refresh_golden_frame;
790 int refresh_alt_ref_frame;
791
792 int ext_refresh_frame_flags_pending;
793 int ext_refresh_last_frame;
794 int ext_refresh_golden_frame;
795 int ext_refresh_alt_ref_frame;
796
797 int ext_refresh_frame_context_pending;
798 int ext_refresh_frame_context;
799
800 int64_t norm_wiener_variance;
801 int64_t *mb_wiener_variance;
802 int mb_wiener_var_rows;
803 int mb_wiener_var_cols;
804 double *mi_ssim_rdmult_scaling_factors;
805
806 YV12_BUFFER_CONFIG last_frame_uf;
807
808 TOKENEXTRA *tile_tok[4][1 << 6];
809 TOKENLIST *tplist[4][1 << 6];
810
811 // Ambient reconstruction err target for force key frames
812 int64_t ambient_err;
813
814 RD_CONTROL rd_ctrl;
815 RD_OPT rd;
816
817 CODING_CONTEXT coding_context;
818
819 int *nmvcosts[2];
820 int *nmvcosts_hp[2];
821 int *nmvsadcosts[2];
822 int *nmvsadcosts_hp[2];
823
824 int64_t last_time_stamp_seen;
825 int64_t last_end_time_stamp_seen;
826 int64_t first_time_stamp_ever;
827
828 RATE_CONTROL rc;
829 double framerate;
830
831 int interp_filter_selected[REF_FRAMES][SWITCHABLE];
832
833 struct vpx_codec_pkt_list *output_pkt_list;
834
835 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
836 int mbgraph_n_frames; // number of frames filled in the above
837 int static_mb_pct; // % forced skip mbs by segmentation
838 int ref_frame_flags;
839
840 SPEED_FEATURES sf;
841
842 uint32_t max_mv_magnitude;
843 int mv_step_param;
844
845 int allow_comp_inter_inter;
846
847 // Default value is 1. From first pass stats, encode_breakout may be disabled.
848 ENCODE_BREAKOUT_TYPE allow_encode_breakout;
849
850 // Get threshold from external input. A suggested threshold is 800 for HD
851 // clips, and 300 for < HD clips.
852 int encode_breakout;
853
854 uint8_t *segmentation_map;
855
856 uint8_t *skin_map;
857
858 // segment threshold for encode breakout
859 int segment_encode_breakout[MAX_SEGMENTS];
860
861 CYCLIC_REFRESH *cyclic_refresh;
862 ActiveMap active_map;
863
864 fractional_mv_step_fp *find_fractional_mv_step;
865 struct scale_factors me_sf;
866 vp9_diamond_search_fn_t diamond_search_sad;
867 vp9_variance_fn_ptr_t fn_ptr[BLOCK_SIZES];
868 uint64_t time_receive_data;
869 uint64_t time_compress_data;
870 uint64_t time_pick_lpf;
871 uint64_t time_encode_sb_row;
872
873 TWO_PASS twopass;
874
875 // Force recalculation of segment_ids for each mode info
876 uint8_t force_update_segmentation;
877
878 YV12_BUFFER_CONFIG tf_buffer;
879
880 // class responsible for adaptive
881 // quantization of altref frames
882 struct ALT_REF_AQ *alt_ref_aq;
883
884 #if CONFIG_INTERNAL_STATS
885 unsigned int mode_chosen_counts[MAX_MODES];
886
887 int count;
888 uint64_t total_sq_error;
889 uint64_t total_samples;
890 ImageStat psnr;
891
892 uint64_t totalp_sq_error;
893 uint64_t totalp_samples;
894 ImageStat psnrp;
895
896 double total_blockiness;
897 double worst_blockiness;
898
899 uint64_t bytes;
900 double summed_quality;
901 double summed_weights;
902 double summedp_quality;
903 double summedp_weights;
904 unsigned int tot_recode_hits;
905 double worst_ssim;
906
907 ImageStat ssimg;
908 ImageStat fastssim;
909 ImageStat psnrhvs;
910
911 int b_calculate_ssimg;
912 int b_calculate_blockiness;
913
914 int b_calculate_consistency;
915
916 double total_inconsistency;
917 double worst_consistency;
918 Ssimv *ssim_vars;
919 Metrics metrics;
920 #endif
921 int b_calculate_psnr;
922
923 int droppable;
924
925 int initial_width;
926 int initial_height;
927 int initial_mbs; // Number of MBs in the full-size frame; to be used to
928 // normalize the firstpass stats. This will differ from the
929 // number of MBs in the current frame when the frame is
930 // scaled.
931
932 int last_coded_width;
933 int last_coded_height;
934
935 int use_svc;
936
937 SVC svc;
938
939 // Store frame variance info in SOURCE_VAR_BASED_PARTITION search type.
940 Diff *source_diff_var;
941 // The threshold used in SOURCE_VAR_BASED_PARTITION search type.
942 unsigned int source_var_thresh;
943 int frames_till_next_var_check;
944
945 int frame_flags;
946
947 search_site_config ss_cfg;
948
949 int mbmode_cost[INTRA_MODES];
950 unsigned int inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES];
951 int intra_uv_mode_cost[FRAME_TYPES][INTRA_MODES][INTRA_MODES];
952 int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
953 int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
954 int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
955 // Indices are: max_tx_size-1, tx_size_ctx, tx_size
956 int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
957
958 #if CONFIG_VP9_TEMPORAL_DENOISING
959 VP9_DENOISER denoiser;
960 #endif
961
962 int resize_pending;
963 RESIZE_STATE resize_state;
964 int external_resize;
965 int resize_scale_num;
966 int resize_scale_den;
967 int resize_avg_qp;
968 int resize_buffer_underflow;
969 int resize_count;
970
971 int use_skin_detection;
972
973 int target_level;
974
975 NOISE_ESTIMATE noise_estimate;
976
977 // Count on how many consecutive times a block uses small/zeromv for encoding.
978 uint8_t *consec_zero_mv;
979
980 // VAR_BASED_PARTITION thresholds
981 // 0 - threshold_64x64; 1 - threshold_32x32;
982 // 2 - threshold_16x16; 3 - vbp_threshold_8x8;
983 int64_t vbp_thresholds[4];
984 int64_t vbp_threshold_minmax;
985 int64_t vbp_threshold_sad;
986 // Threshold used for partition copy
987 int64_t vbp_threshold_copy;
988 BLOCK_SIZE vbp_bsize_min;
989
990 // Multi-threading
991 int num_workers;
992 VPxWorker *workers;
993 struct EncWorkerData *tile_thr_data;
994 VP9LfSync lf_row_sync;
995 struct VP9BitstreamWorkerData *vp9_bitstream_worker_data;
996
997 int keep_level_stats;
998 Vp9LevelInfo level_info;
999 MultiThreadHandle multi_thread_ctxt;
1000 void (*row_mt_sync_read_ptr)(VP9RowMTSync *const, int, int);
1001 void (*row_mt_sync_write_ptr)(VP9RowMTSync *const, int, int, const int);
1002 ARNRFilterData arnr_filter_data;
1003
1004 int row_mt;
1005 unsigned int row_mt_bit_exact;
1006
1007 // Previous Partition Info
1008 BLOCK_SIZE *prev_partition;
1009 int8_t *prev_segment_id;
1010 // Used to save the status of whether a block has a low variance in
1011 // choose_partitioning. 0 for 64x64, 1~2 for 64x32, 3~4 for 32x64, 5~8 for
1012 // 32x32, 9~24 for 16x16.
1013 // This is for the last frame and is copied to the current frame
1014 // when partition copy happens.
1015 uint8_t *prev_variance_low;
1016 uint8_t *copied_frame_cnt;
1017 uint8_t max_copied_frame;
1018 // If the last frame is dropped, we don't copy partition.
1019 uint8_t last_frame_dropped;
1020
1021 // For each superblock: keeps track of the last time (in frame distance) the
1022 // the superblock did not have low source sad.
1023 uint8_t *content_state_sb_fd;
1024
1025 int compute_source_sad_onepass;
1026
1027 int compute_frame_low_motion_onepass;
1028
1029 LevelConstraint level_constraint;
1030
1031 uint8_t *count_arf_frame_usage;
1032 uint8_t *count_lastgolden_frame_usage;
1033
1034 int multi_layer_arf;
1035 vpx_roi_map_t roi;
1036
1037 LOOPFILTER_CONTROL loopfilter_ctrl;
1038 #if CONFIG_RATE_CTRL
1039 ENCODE_COMMAND encode_command;
1040 PARTITION_INFO *partition_info;
1041 MOTION_VECTOR_INFO *motion_vector_info;
1042 MOTION_VECTOR_INFO *fp_motion_vector_info;
1043 TplDepStats *tpl_stats_info;
1044
1045 RATE_QSTEP_MODEL rq_model[ENCODE_FRAME_TYPES];
1046 #endif
1047 EXT_RATECTRL ext_ratectrl;
1048
1049 int fixed_qp_onepass;
1050
1051 // Flag to keep track of dynamic change in deadline mode
1052 // (good/best/realtime).
1053 MODE deadline_mode_previous_frame;
1054
1055 // Flag to disable scene detection when rtc rate control library is used.
1056 int disable_scene_detection_rtc_ratectrl;
1057
1058 #if CONFIG_COLLECT_COMPONENT_TIMING
1059 /*!
1060 * component_time[] are initialized to zero while encoder starts.
1061 */
1062 uint64_t component_time[kTimingComponents];
1063 /*!
1064 * Stores timing for individual components between calls of start_timing()
1065 * and end_timing().
1066 */
1067 struct vpx_usec_timer component_timer[kTimingComponents];
1068 /*!
1069 * frame_component_time[] are initialized to zero at beginning of each frame.
1070 */
1071 uint64_t frame_component_time[kTimingComponents];
1072 #endif
1073 } VP9_COMP;
1074
1075 #if CONFIG_RATE_CTRL
1076 // Allocates memory for the partition information.
1077 // The unit size is each 4x4 block.
1078 // Only called once in vp9_create_compressor().
partition_info_init(struct VP9_COMP * cpi)1079 static INLINE void partition_info_init(struct VP9_COMP *cpi) {
1080 VP9_COMMON *const cm = &cpi->common;
1081 const int unit_width = get_num_unit_4x4(cpi->frame_info.frame_width);
1082 const int unit_height = get_num_unit_4x4(cpi->frame_info.frame_height);
1083 CHECK_MEM_ERROR(&cm->error, cpi->partition_info,
1084 (PARTITION_INFO *)vpx_calloc(unit_width * unit_height,
1085 sizeof(PARTITION_INFO)));
1086 memset(cpi->partition_info, 0,
1087 unit_width * unit_height * sizeof(PARTITION_INFO));
1088 }
1089
1090 // Frees memory of the partition information.
1091 // Only called once in dealloc_compressor_data().
free_partition_info(struct VP9_COMP * cpi)1092 static INLINE void free_partition_info(struct VP9_COMP *cpi) {
1093 vpx_free(cpi->partition_info);
1094 cpi->partition_info = NULL;
1095 }
1096
reset_mv_info(MOTION_VECTOR_INFO * mv_info)1097 static INLINE void reset_mv_info(MOTION_VECTOR_INFO *mv_info) {
1098 mv_info->ref_frame[0] = NO_REF_FRAME;
1099 mv_info->ref_frame[1] = NO_REF_FRAME;
1100 mv_info->mv[0].as_int = INVALID_MV;
1101 mv_info->mv[1].as_int = INVALID_MV;
1102 }
1103
1104 // Allocates memory for the motion vector information.
1105 // The unit size is each 4x4 block.
1106 // Only called once in vp9_create_compressor().
motion_vector_info_init(struct VP9_COMP * cpi)1107 static INLINE void motion_vector_info_init(struct VP9_COMP *cpi) {
1108 VP9_COMMON *const cm = &cpi->common;
1109 const int unit_width = get_num_unit_4x4(cpi->frame_info.frame_width);
1110 const int unit_height = get_num_unit_4x4(cpi->frame_info.frame_height);
1111 CHECK_MEM_ERROR(&cm->error, cpi->motion_vector_info,
1112 (MOTION_VECTOR_INFO *)vpx_calloc(unit_width * unit_height,
1113 sizeof(MOTION_VECTOR_INFO)));
1114 memset(cpi->motion_vector_info, 0,
1115 unit_width * unit_height * sizeof(MOTION_VECTOR_INFO));
1116 }
1117
1118 // Frees memory of the motion vector information.
1119 // Only called once in dealloc_compressor_data().
free_motion_vector_info(struct VP9_COMP * cpi)1120 static INLINE void free_motion_vector_info(struct VP9_COMP *cpi) {
1121 vpx_free(cpi->motion_vector_info);
1122 cpi->motion_vector_info = NULL;
1123 }
1124
1125 // Allocates memory for the tpl stats information.
1126 // Only called once in vp9_create_compressor().
tpl_stats_info_init(struct VP9_COMP * cpi)1127 static INLINE void tpl_stats_info_init(struct VP9_COMP *cpi) {
1128 VP9_COMMON *const cm = &cpi->common;
1129 CHECK_MEM_ERROR(
1130 &cm->error, cpi->tpl_stats_info,
1131 (TplDepStats *)vpx_calloc(MAX_LAG_BUFFERS, sizeof(TplDepStats)));
1132 memset(cpi->tpl_stats_info, 0, MAX_LAG_BUFFERS * sizeof(TplDepStats));
1133 }
1134
1135 // Frees memory of the tpl stats information.
1136 // Only called once in dealloc_compressor_data().
free_tpl_stats_info(struct VP9_COMP * cpi)1137 static INLINE void free_tpl_stats_info(struct VP9_COMP *cpi) {
1138 vpx_free(cpi->tpl_stats_info);
1139 cpi->tpl_stats_info = NULL;
1140 }
1141
1142 // Allocates memory for the first pass motion vector information.
1143 // The unit size is each 16x16 block.
1144 // Only called once in vp9_create_compressor().
fp_motion_vector_info_init(struct VP9_COMP * cpi)1145 static INLINE void fp_motion_vector_info_init(struct VP9_COMP *cpi) {
1146 VP9_COMMON *const cm = &cpi->common;
1147 const int unit_width = get_num_unit_16x16(cpi->frame_info.frame_width);
1148 const int unit_height = get_num_unit_16x16(cpi->frame_info.frame_height);
1149 CHECK_MEM_ERROR(&cm->error, cpi->fp_motion_vector_info,
1150 (MOTION_VECTOR_INFO *)vpx_calloc(unit_width * unit_height,
1151 sizeof(MOTION_VECTOR_INFO)));
1152 }
1153
fp_motion_vector_info_reset(int frame_width,int frame_height,MOTION_VECTOR_INFO * fp_motion_vector_info)1154 static INLINE void fp_motion_vector_info_reset(
1155 int frame_width, int frame_height,
1156 MOTION_VECTOR_INFO *fp_motion_vector_info) {
1157 const int unit_width = get_num_unit_16x16(frame_width);
1158 const int unit_height = get_num_unit_16x16(frame_height);
1159 int i;
1160 for (i = 0; i < unit_width * unit_height; ++i) {
1161 reset_mv_info(fp_motion_vector_info + i);
1162 }
1163 }
1164
1165 // Frees memory of the first pass motion vector information.
1166 // Only called once in dealloc_compressor_data().
free_fp_motion_vector_info(struct VP9_COMP * cpi)1167 static INLINE void free_fp_motion_vector_info(struct VP9_COMP *cpi) {
1168 vpx_free(cpi->fp_motion_vector_info);
1169 cpi->fp_motion_vector_info = NULL;
1170 }
1171
1172 // This is the c-version counter part of ImageBuffer
1173 typedef struct IMAGE_BUFFER {
1174 int allocated;
1175 int plane_width[3];
1176 int plane_height[3];
1177 uint8_t *plane_buffer[3];
1178 } IMAGE_BUFFER;
1179
1180 #define RATE_CTRL_MAX_RECODE_NUM 7
1181
1182 typedef struct RATE_QINDEX_HISTORY {
1183 int recode_count;
1184 int q_index_history[RATE_CTRL_MAX_RECODE_NUM];
1185 int rate_history[RATE_CTRL_MAX_RECODE_NUM];
1186 int q_index_high;
1187 int q_index_low;
1188 } RATE_QINDEX_HISTORY;
1189
1190 #endif // CONFIG_RATE_CTRL
1191
1192 typedef struct ENCODE_FRAME_RESULT {
1193 int show_idx;
1194 FRAME_UPDATE_TYPE update_type;
1195 #if CONFIG_RATE_CTRL
1196 int frame_coding_index;
1197 int ref_frame_coding_indexes[MAX_INTER_REF_FRAMES];
1198 int ref_frame_valid_list[MAX_INTER_REF_FRAMES];
1199 double psnr;
1200 uint64_t sse;
1201 FRAME_COUNTS frame_counts;
1202 const PARTITION_INFO *partition_info;
1203 const MOTION_VECTOR_INFO *motion_vector_info;
1204 const TplDepStats *tpl_stats_info;
1205 IMAGE_BUFFER coded_frame;
1206 RATE_QINDEX_HISTORY rq_history;
1207 #endif // CONFIG_RATE_CTRL
1208 int quantize_index;
1209 } ENCODE_FRAME_RESULT;
1210
1211 void vp9_init_encode_frame_result(ENCODE_FRAME_RESULT *encode_frame_result);
1212
1213 void vp9_initialize_enc(void);
1214
1215 void vp9_update_compressor_with_img_fmt(VP9_COMP *cpi, vpx_img_fmt_t img_fmt);
1216 struct VP9_COMP *vp9_create_compressor(const VP9EncoderConfig *oxcf,
1217 BufferPool *const pool);
1218 void vp9_remove_compressor(VP9_COMP *cpi);
1219
1220 void vp9_change_config(VP9_COMP *cpi, const VP9EncoderConfig *oxcf);
1221
1222 // receive a frames worth of data. caller can assume that a copy of this
1223 // frame is made and not just a copy of the pointer..
1224 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
1225 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
1226 int64_t end_time);
1227
1228 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
1229 size_t *size, uint8_t *dest, size_t dest_size,
1230 int64_t *time_stamp, int64_t *time_end, int flush,
1231 ENCODE_FRAME_RESULT *encode_frame_result);
1232
1233 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
1234 vp9_ppflags_t *flags);
1235
1236 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags);
1237
1238 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags);
1239
1240 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1241 YV12_BUFFER_CONFIG *sd);
1242
1243 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1244 YV12_BUFFER_CONFIG *sd);
1245
1246 int vp9_update_entropy(VP9_COMP *cpi, int update);
1247
1248 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
1249 int cols);
1250
1251 int vp9_get_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
1252 int cols);
1253
1254 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING_MODE horiz_mode,
1255 VPX_SCALING_MODE vert_mode);
1256
1257 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
1258 unsigned int height);
1259
1260 void vp9_set_svc(VP9_COMP *cpi, int use_svc);
1261
1262 // Check for resetting the rc flags (rc_1_frame, rc_2_frame) if the
1263 // configuration change has a large change in avg_frame_bandwidth.
1264 // For SVC check for resetting based on spatial layer average bandwidth.
1265 // Also reset buffer level to optimal level.
1266 void vp9_check_reset_rc_flag(VP9_COMP *cpi);
1267
1268 void vp9_set_rc_buffer_sizes(VP9_COMP *cpi);
1269
stack_pop(int * stack,int stack_size)1270 static INLINE int stack_pop(int *stack, int stack_size) {
1271 int idx;
1272 const int r = stack[0];
1273 for (idx = 1; idx < stack_size; ++idx) stack[idx - 1] = stack[idx];
1274
1275 return r;
1276 }
1277
stack_top(const int * stack)1278 static INLINE int stack_top(const int *stack) { return stack[0]; }
1279
stack_push(int * stack,int new_item,int stack_size)1280 static INLINE void stack_push(int *stack, int new_item, int stack_size) {
1281 int idx;
1282 for (idx = stack_size; idx > 0; --idx) stack[idx] = stack[idx - 1];
1283 stack[0] = new_item;
1284 }
1285
stack_init(int * stack,int length)1286 static INLINE void stack_init(int *stack, int length) {
1287 int idx;
1288 for (idx = 0; idx < length; ++idx) stack[idx] = -1;
1289 }
1290
1291 int vp9_get_quantizer(const VP9_COMP *cpi);
1292
frame_is_kf_gf_arf(const VP9_COMP * cpi)1293 static INLINE int frame_is_kf_gf_arf(const VP9_COMP *cpi) {
1294 return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
1295 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
1296 }
1297
ref_frame_to_flag(int8_t ref_frame)1298 static INLINE int ref_frame_to_flag(int8_t ref_frame) {
1299 static const int kVp9RefFlagList[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
1300 VP9_ALT_FLAG };
1301 assert(ref_frame >= LAST_FRAME && ref_frame <= ALTREF_FRAME);
1302 return kVp9RefFlagList[ref_frame];
1303 }
1304
get_ref_frame_map_idx(const VP9_COMP * cpi,MV_REFERENCE_FRAME ref_frame)1305 static INLINE int get_ref_frame_map_idx(const VP9_COMP *cpi,
1306 MV_REFERENCE_FRAME ref_frame) {
1307 if (ref_frame == LAST_FRAME) {
1308 return cpi->lst_fb_idx;
1309 } else if (ref_frame == GOLDEN_FRAME) {
1310 return cpi->gld_fb_idx;
1311 } else {
1312 return cpi->alt_fb_idx;
1313 }
1314 }
1315
get_ref_frame_buf_idx(const VP9_COMP * const cpi,int ref_frame)1316 static INLINE int get_ref_frame_buf_idx(const VP9_COMP *const cpi,
1317 int ref_frame) {
1318 const VP9_COMMON *const cm = &cpi->common;
1319 const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
1320 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX;
1321 }
1322
get_ref_cnt_buffer(const VP9_COMMON * cm,int fb_idx)1323 static INLINE RefCntBuffer *get_ref_cnt_buffer(const VP9_COMMON *cm,
1324 int fb_idx) {
1325 return fb_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[fb_idx] : NULL;
1326 }
1327
get_ref_frame_bufs(const VP9_COMP * cpi,RefCntBuffer * ref_frame_bufs[MAX_INTER_REF_FRAMES])1328 static INLINE void get_ref_frame_bufs(
1329 const VP9_COMP *cpi, RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES]) {
1330 const VP9_COMMON *const cm = &cpi->common;
1331 MV_REFERENCE_FRAME ref_frame;
1332 for (ref_frame = LAST_FRAME; ref_frame < MAX_REF_FRAMES; ++ref_frame) {
1333 int ref_frame_buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
1334 int inter_ref_idx = mv_ref_frame_to_inter_ref_idx(ref_frame);
1335 ref_frame_bufs[inter_ref_idx] = get_ref_cnt_buffer(cm, ref_frame_buf_idx);
1336 }
1337 }
1338
get_ref_frame_buffer(const VP9_COMP * const cpi,MV_REFERENCE_FRAME ref_frame)1339 static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
1340 const VP9_COMP *const cpi, MV_REFERENCE_FRAME ref_frame) {
1341 const VP9_COMMON *const cm = &cpi->common;
1342 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
1343 return buf_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[buf_idx].buf
1344 : NULL;
1345 }
1346
get_token_alloc(int mb_rows,int mb_cols)1347 static INLINE int get_token_alloc(int mb_rows, int mb_cols) {
1348 // TODO(JBB): double check we can't exceed this token count if we have a
1349 // 32x32 transform crossing a boundary at a multiple of 16.
1350 // mb_rows, cols are in units of 16 pixels. We assume 3 planes all at full
1351 // resolution. We assume up to 1 token per pixel, and then allow
1352 // a head room of 4.
1353
1354 // Use aligned mb_rows and mb_cols to better align with actual token sizes.
1355 const int aligned_mb_rows =
1356 ALIGN_POWER_OF_TWO(mb_rows, MI_BLOCK_SIZE_LOG2 - 1);
1357 const int aligned_mb_cols =
1358 ALIGN_POWER_OF_TWO(mb_cols, MI_BLOCK_SIZE_LOG2 - 1);
1359 return aligned_mb_rows * aligned_mb_cols * (16 * 16 * 3 + 4);
1360 }
1361
1362 // Get the allocated token size for a tile. It does the same calculation as in
1363 // the frame token allocation.
allocated_tokens(TileInfo tile)1364 static INLINE int allocated_tokens(TileInfo tile) {
1365 int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 1) >> 1;
1366 int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 1) >> 1;
1367
1368 return get_token_alloc(tile_mb_rows, tile_mb_cols);
1369 }
1370
get_start_tok(VP9_COMP * cpi,int tile_row,int tile_col,int mi_row,TOKENEXTRA ** tok)1371 static INLINE void get_start_tok(VP9_COMP *cpi, int tile_row, int tile_col,
1372 int mi_row, TOKENEXTRA **tok) {
1373 VP9_COMMON *const cm = &cpi->common;
1374 const int tile_cols = 1 << cm->log2_tile_cols;
1375 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
1376 const TileInfo *const tile_info = &this_tile->tile_info;
1377
1378 int tile_mb_cols = (tile_info->mi_col_end - tile_info->mi_col_start + 1) >> 1;
1379 const int mb_row = (mi_row - tile_info->mi_row_start) >> 1;
1380
1381 *tok =
1382 cpi->tile_tok[tile_row][tile_col] + get_token_alloc(mb_row, tile_mb_cols);
1383 }
1384
1385 int64_t vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b);
1386 #if CONFIG_VP9_HIGHBITDEPTH
1387 int64_t vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
1388 const YV12_BUFFER_CONFIG *b);
1389 #endif // CONFIG_VP9_HIGHBITDEPTH
1390
1391 void vp9_scale_references(VP9_COMP *cpi);
1392
1393 void vp9_update_reference_frames(VP9_COMP *cpi);
1394
1395 void vp9_get_ref_frame_info(FRAME_UPDATE_TYPE update_type, int ref_frame_flags,
1396 RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES],
1397 int *ref_frame_coding_indexes,
1398 int *ref_frame_valid_list);
1399
1400 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv);
1401
1402 #if CONFIG_VP9_HIGHBITDEPTH
1403 void vp9_scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
1404 YV12_BUFFER_CONFIG *dst, int bd);
1405 #else
1406 void vp9_scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
1407 YV12_BUFFER_CONFIG *dst);
1408 #endif // CONFIG_VP9_HIGHBITDEPTH
1409
1410 YV12_BUFFER_CONFIG *vp9_scale_if_required(
1411 VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
1412 int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler);
1413
1414 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags);
1415
is_one_pass_svc(const struct VP9_COMP * const cpi)1416 static INLINE int is_one_pass_svc(const struct VP9_COMP *const cpi) {
1417 return (cpi->use_svc && cpi->oxcf.pass == 0);
1418 }
1419
1420 #if CONFIG_VP9_TEMPORAL_DENOISING
denoise_svc(const struct VP9_COMP * const cpi)1421 static INLINE int denoise_svc(const struct VP9_COMP *const cpi) {
1422 return (!cpi->use_svc || (cpi->use_svc && cpi->svc.spatial_layer_id >=
1423 cpi->svc.first_layer_denoise));
1424 }
1425 #endif
1426
1427 #define MIN_LOOKAHEAD_FOR_ARFS 4
is_altref_enabled(const VP9_COMP * const cpi)1428 static INLINE int is_altref_enabled(const VP9_COMP *const cpi) {
1429 return !(cpi->oxcf.mode == REALTIME && cpi->oxcf.rc_mode == VPX_CBR) &&
1430 cpi->oxcf.lag_in_frames >= MIN_LOOKAHEAD_FOR_ARFS &&
1431 cpi->oxcf.enable_auto_arf;
1432 }
1433
set_ref_ptrs(const VP9_COMMON * const cm,MACROBLOCKD * xd,MV_REFERENCE_FRAME ref0,MV_REFERENCE_FRAME ref1)1434 static INLINE void set_ref_ptrs(const VP9_COMMON *const cm, MACROBLOCKD *xd,
1435 MV_REFERENCE_FRAME ref0,
1436 MV_REFERENCE_FRAME ref1) {
1437 xd->block_refs[0] =
1438 &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME : 0];
1439 xd->block_refs[1] =
1440 &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME : 0];
1441 }
1442
get_chessboard_index(const int frame_index)1443 static INLINE int get_chessboard_index(const int frame_index) {
1444 return frame_index & 0x1;
1445 }
1446
cond_cost_list(const struct VP9_COMP * cpi,int * cost_list)1447 static INLINE int *cond_cost_list(const struct VP9_COMP *cpi, int *cost_list) {
1448 return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
1449 }
1450
get_num_vert_units(TileInfo tile,int shift)1451 static INLINE int get_num_vert_units(TileInfo tile, int shift) {
1452 int num_vert_units =
1453 (tile.mi_row_end - tile.mi_row_start + (1 << shift) - 1) >> shift;
1454 return num_vert_units;
1455 }
1456
get_num_cols(TileInfo tile,int shift)1457 static INLINE int get_num_cols(TileInfo tile, int shift) {
1458 int num_cols =
1459 (tile.mi_col_end - tile.mi_col_start + (1 << shift) - 1) >> shift;
1460 return num_cols;
1461 }
1462
get_level_index(VP9_LEVEL level)1463 static INLINE int get_level_index(VP9_LEVEL level) {
1464 int i;
1465 for (i = 0; i < VP9_LEVELS; ++i) {
1466 if (level == vp9_level_defs[i].level) return i;
1467 }
1468 return -1;
1469 }
1470
1471 // Return the log2 value of max column tiles corresponding to the level that
1472 // the picture size fits into.
log_tile_cols_from_picsize_level(uint32_t width,uint32_t height)1473 static INLINE int log_tile_cols_from_picsize_level(uint32_t width,
1474 uint32_t height) {
1475 int i;
1476 const uint32_t pic_size = width * height;
1477 const uint32_t pic_breadth = VPXMAX(width, height);
1478 for (i = LEVEL_1; i < LEVEL_MAX; ++i) {
1479 if (vp9_level_defs[i].max_luma_picture_size >= pic_size &&
1480 vp9_level_defs[i].max_luma_picture_breadth >= pic_breadth) {
1481 return get_msb(vp9_level_defs[i].max_col_tiles);
1482 }
1483 }
1484 return INT_MAX;
1485 }
1486
1487 VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec);
1488
1489 vpx_codec_err_t vp9_set_roi_map(VP9_COMP *cpi, unsigned char *map,
1490 unsigned int rows, unsigned int cols,
1491 int delta_q[8], int delta_lf[8], int skip[8],
1492 int ref_frame[8]);
1493
1494 void vp9_new_framerate(VP9_COMP *cpi, double framerate);
1495
1496 void vp9_set_row_mt(VP9_COMP *cpi);
1497
1498 int vp9_get_psnr(const VP9_COMP *cpi, PSNR_STATS *psnr);
1499
1500 #define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
1501
alloc_frame_mvs(VP9_COMMON * const cm,int buffer_idx)1502 static INLINE void alloc_frame_mvs(VP9_COMMON *const cm, int buffer_idx) {
1503 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
1504 if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
1505 new_fb_ptr->mi_cols < cm->mi_cols) {
1506 vpx_free(new_fb_ptr->mvs);
1507 CHECK_MEM_ERROR(&cm->error, new_fb_ptr->mvs,
1508 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
1509 sizeof(*new_fb_ptr->mvs)));
1510 new_fb_ptr->mi_rows = cm->mi_rows;
1511 new_fb_ptr->mi_cols = cm->mi_cols;
1512 }
1513 }
1514
mv_cost(const MV * mv,const int * joint_cost,int * const comp_cost[2])1515 static INLINE int mv_cost(const MV *mv, const int *joint_cost,
1516 int *const comp_cost[2]) {
1517 assert(mv->row >= -MV_MAX && mv->row < MV_MAX);
1518 assert(mv->col >= -MV_MAX && mv->col < MV_MAX);
1519 return joint_cost[vp9_get_mv_joint(mv)] + comp_cost[0][mv->row] +
1520 comp_cost[1][mv->col];
1521 }
1522
mvsad_err_cost(const MACROBLOCK * x,const MV * mv,const MV * ref,int sad_per_bit)1523 static INLINE int mvsad_err_cost(const MACROBLOCK *x, const MV *mv,
1524 const MV *ref, int sad_per_bit) {
1525 MV diff;
1526 diff.row = mv->row - ref->row;
1527 diff.col = mv->col - ref->col;
1528 return ROUND_POWER_OF_TWO(
1529 (unsigned)mv_cost(&diff, x->nmvjointsadcost, x->nmvsadcost) * sad_per_bit,
1530 VP9_PROB_COST_SHIFT);
1531 }
1532
get_start_mv_sad(const MACROBLOCK * x,const MV * mvp_full,const MV * ref_mv_full,vpx_sad_fn_t sad_fn_ptr,int sadpb)1533 static INLINE uint32_t get_start_mv_sad(const MACROBLOCK *x, const MV *mvp_full,
1534 const MV *ref_mv_full,
1535 vpx_sad_fn_t sad_fn_ptr, int sadpb) {
1536 const int src_buf_stride = x->plane[0].src.stride;
1537 const uint8_t *const src_buf = x->plane[0].src.buf;
1538 const MACROBLOCKD *const xd = &x->e_mbd;
1539 const int pred_buf_stride = xd->plane[0].pre[0].stride;
1540 const uint8_t *const pred_buf =
1541 xd->plane[0].pre[0].buf + mvp_full->row * pred_buf_stride + mvp_full->col;
1542 uint32_t start_mv_sad =
1543 sad_fn_ptr(src_buf, src_buf_stride, pred_buf, pred_buf_stride);
1544 start_mv_sad += mvsad_err_cost(x, mvp_full, ref_mv_full, sadpb);
1545
1546 return start_mv_sad;
1547 }
1548
num_4x4_to_edge(int plane_4x4_dim,int mb_to_edge_dim,int subsampling_dim,int blk_dim)1549 static INLINE int num_4x4_to_edge(int plane_4x4_dim, int mb_to_edge_dim,
1550 int subsampling_dim, int blk_dim) {
1551 return plane_4x4_dim + (mb_to_edge_dim >> (5 + subsampling_dim)) - blk_dim;
1552 }
1553
1554 // Compute the sum of squares on all visible 4x4s in the transform block.
sum_squares_visible(const MACROBLOCKD * xd,const struct macroblockd_plane * const pd,const int16_t * diff,const int diff_stride,int blk_row,int blk_col,const BLOCK_SIZE plane_bsize,const BLOCK_SIZE tx_bsize,int * visible_width,int * visible_height)1555 static int64_t sum_squares_visible(const MACROBLOCKD *xd,
1556 const struct macroblockd_plane *const pd,
1557 const int16_t *diff, const int diff_stride,
1558 int blk_row, int blk_col,
1559 const BLOCK_SIZE plane_bsize,
1560 const BLOCK_SIZE tx_bsize,
1561 int *visible_width, int *visible_height) {
1562 int64_t sse;
1563 const int plane_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
1564 const int plane_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
1565 const int tx_4x4_w = num_4x4_blocks_wide_lookup[tx_bsize];
1566 const int tx_4x4_h = num_4x4_blocks_high_lookup[tx_bsize];
1567 const int b4x4s_to_right_edge = num_4x4_to_edge(
1568 plane_4x4_w, xd->mb_to_right_edge, pd->subsampling_x, blk_col);
1569 const int b4x4s_to_bottom_edge = num_4x4_to_edge(
1570 plane_4x4_h, xd->mb_to_bottom_edge, pd->subsampling_y, blk_row);
1571 if (tx_bsize == BLOCK_4X4 ||
1572 (b4x4s_to_right_edge >= tx_4x4_w && b4x4s_to_bottom_edge >= tx_4x4_h)) {
1573 assert(tx_4x4_w == tx_4x4_h);
1574 sse = (int64_t)vpx_sum_squares_2d_i16(diff, diff_stride, tx_4x4_w << 2);
1575 *visible_width = tx_4x4_w << 2;
1576 *visible_height = tx_4x4_h << 2;
1577 } else {
1578 int r, c;
1579 const int max_r = VPXMIN(b4x4s_to_bottom_edge, tx_4x4_h);
1580 const int max_c = VPXMIN(b4x4s_to_right_edge, tx_4x4_w);
1581 sse = 0;
1582 // if we are in the unrestricted motion border.
1583 for (r = 0; r < max_r; ++r) {
1584 // Skip visiting the sub blocks that are wholly within the UMV.
1585 for (c = 0; c < max_c; ++c) {
1586 sse += (int64_t)vpx_sum_squares_2d_i16(
1587 diff + r * diff_stride * 4 + c * 4, diff_stride, 4);
1588 }
1589 }
1590 *visible_width = max_c << 2;
1591 *visible_height = max_r << 2;
1592 }
1593 return sse;
1594 }
1595
1596 // Check if trellis coefficient optimization of the transform block is enabled.
do_trellis_opt(const struct macroblockd_plane * pd,const int16_t * src_diff,int diff_stride,int blk_row,int blk_col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)1597 static INLINE int do_trellis_opt(const struct macroblockd_plane *pd,
1598 const int16_t *src_diff, int diff_stride,
1599 int blk_row, int blk_col,
1600 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
1601 void *arg) {
1602 const struct encode_b_args *const args = (struct encode_b_args *)arg;
1603 const MACROBLOCK *const x = args->x;
1604
1605 switch (args->enable_trellis_opt) {
1606 case DISABLE_TRELLIS_OPT: return 0;
1607 case ENABLE_TRELLIS_OPT: return 1;
1608 case ENABLE_TRELLIS_OPT_TX_RD_SRC_VAR: {
1609 vpx_clear_system_state();
1610
1611 return (args->trellis_opt_thresh > 0.0)
1612 ? (x->log_block_src_var <= args->trellis_opt_thresh)
1613 : 1;
1614 }
1615 case ENABLE_TRELLIS_OPT_TX_RD_RESIDUAL_MSE: {
1616 const MACROBLOCKD *const xd = &x->e_mbd;
1617 const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size];
1618 #if CONFIG_VP9_HIGHBITDEPTH
1619 const int dequant_shift =
1620 (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd - 5 : 3;
1621 #else
1622 const int dequant_shift = 3;
1623 #endif // CONFIG_VP9_HIGHBITDEPTH
1624 const int qstep = pd->dequant[1] >> dequant_shift;
1625 int *sse_calc_done = args->sse_calc_done;
1626 int64_t *sse = args->sse;
1627 int visible_width = 0, visible_height = 0;
1628
1629 // TODO: Enable the sf for high bit-depth case
1630 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) || !sse ||
1631 !sse_calc_done)
1632 return 1;
1633
1634 *sse = sum_squares_visible(xd, pd, src_diff, diff_stride, blk_row,
1635 blk_col, plane_bsize, tx_bsize, &visible_width,
1636 &visible_height);
1637 *sse_calc_done = 1;
1638
1639 vpx_clear_system_state();
1640
1641 return (*(sse) <= (int64_t)visible_width * visible_height * qstep *
1642 qstep * args->trellis_opt_thresh);
1643 }
1644 default: assert(0 && "Invalid trellis optimization method."); return 1;
1645 }
1646 }
1647
1648 #if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(VP9_COMP * cpi,int component)1649 static INLINE void start_timing(VP9_COMP *cpi, int component) {
1650 vpx_usec_timer_start(&cpi->component_timer[component]);
1651 }
end_timing(VP9_COMP * cpi,int component)1652 static INLINE void end_timing(VP9_COMP *cpi, int component) {
1653 vpx_usec_timer_mark(&cpi->component_timer[component]);
1654 cpi->frame_component_time[component] +=
1655 vpx_usec_timer_elapsed(&cpi->component_timer[component]);
1656 }
get_frame_type_enum(int type)1657 static INLINE char const *get_frame_type_enum(int type) {
1658 switch (type) {
1659 case 0: return "KEY_FRAME";
1660 case 1: return "INTER_FRAME";
1661 default: assert(0);
1662 }
1663 return "error";
1664 }
1665 #endif
1666
1667 #ifdef __cplusplus
1668 } // extern "C"
1669 #endif
1670
1671 #endif // VPX_VP9_ENCODER_VP9_ENCODER_H_
1672