2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
11 #ifndef VP9_ENCODER_VP9_ONYX_INT_H_
12 #define VP9_ENCODER_VP9_ONYX_INT_H_
16 #include "./vpx_config.h"
17 #include "vpx_ports/mem.h"
18 #include "vpx/internal/vpx_codec_internal.h"
20 #include "vp9/common/vp9_entropy.h"
21 #include "vp9/common/vp9_entropymode.h"
22 #include "vp9/common/vp9_onyx.h"
23 #include "vp9/common/vp9_onyxc_int.h"
25 #include "vp9/encoder/vp9_encodemb.h"
26 #include "vp9/encoder/vp9_firstpass.h"
27 #include "vp9/encoder/vp9_lookahead.h"
28 #include "vp9/encoder/vp9_mbgraph.h"
29 #include "vp9/encoder/vp9_mcomp.h"
30 #include "vp9/encoder/vp9_quantize.h"
31 #include "vp9/encoder/vp9_ratectrl.h"
32 #include "vp9/encoder/vp9_tokenize.h"
33 #include "vp9/encoder/vp9_treewriter.h"
34 #include "vp9/encoder/vp9_variance.h"
40 // #define MODE_TEST_HIT_STATS
42 #if CONFIG_MULTIPLE_ARF
43 // Set MIN_GF_INTERVAL to 1 for the full decomposition.
44 #define MIN_GF_INTERVAL 2
46 #define MIN_GF_INTERVAL 4
48 #define DEFAULT_GF_INTERVAL 10
49 #define DEFAULT_KF_BOOST 2000
50 #define DEFAULT_GF_BOOST 2000
52 #define KEY_FRAME_CONTEXT 5
57 #define MIN_THRESHMULT 32
58 #define MAX_THRESHMULT 512
60 #define GF_ZEROMV_ZBIN_BOOST 0
61 #define LF_ZEROMV_ZBIN_BOOST 0
62 #define MV_ZBIN_BOOST 0
63 #define SPLIT_MV_ZBIN_BOOST 0
64 #define INTRA_ZBIN_BOOST 0
67 int nmvjointcost[MV_JOINTS];
68 int nmvcosts[2][MV_VALS];
69 int nmvcosts_hp[2][MV_VALS];
71 vp9_prob segment_pred_probs[PREDICTION_PROBS];
73 unsigned char *last_frame_seg_map_copy;
75 // 0 = Intra, Last, GF, ARF
76 signed char last_ref_lf_deltas[MAX_REF_LF_DELTAS];
78 signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
83 // This enumerator type needs to be kept aligned with the mode order in
84 // const MODE_DEFINITION vp9_mode_order[MAX_MODES] used in the rd code.
146 USE_LARGESTINTRA_MODELINTER,
148 } TX_SIZE_SEARCH_METHOD;
152 RELAXED_NEIGHBORING_MIN_MAX = 1,
153 STRICT_NEIGHBORING_MIN_MAX = 2
157 // Values should be powers of 2 so that they can be selected as bits of
158 // an integer flags field
160 // terminate search early based on distortion so far compared to
161 // qp step, distortion in the neighborhood of the frame, etc.
162 FLAG_EARLY_TERMINATE = 1,
164 // skips comp inter modes if the best so far is an intra mode
165 FLAG_SKIP_COMP_BESTINTRA = 2,
167 // skips comp inter modes if the best single intermode so far does
168 // not have the same reference as one of the two references being
170 FLAG_SKIP_COMP_REFMISMATCH = 4,
172 // skips oblique intra modes if the best so far is an inter mode
173 FLAG_SKIP_INTRA_BESTINTER = 8,
175 // skips oblique intra modes at angles 27, 63, 117, 153 if the best
176 // intra so far is not one of the neighboring directions
177 FLAG_SKIP_INTRA_DIRMISMATCH = 16,
179 // skips intra modes other than DC_PRED if the source variance
181 FLAG_SKIP_INTRA_LOWVAR = 32,
182 } MODE_SEARCH_SKIP_LOGIC;
186 // Other methods to come
187 } SUBPEL_SEARCH_METHODS;
189 #define ALL_INTRA_MODES 0x3FF
190 #define INTRA_DC_ONLY 0x01
191 #define INTRA_DC_TM ((1 << TM_PRED) | (1 << DC_PRED))
192 #define INTRA_DC_H_V ((1 << DC_PRED) | (1 << V_PRED) | (1 << H_PRED))
193 #define INTRA_DC_TM_H_V (INTRA_DC_TM | (1 << V_PRED) | (1 << H_PRED))
196 LAST_FRAME_PARTITION_OFF = 0,
197 LAST_FRAME_PARTITION_LOW_MOTION = 1,
198 LAST_FRAME_PARTITION_ALL = 2
199 } LAST_FRAME_PARTITION_METHOD;
204 // Allow recode for KF and exceeding maximum frame bandwidth.
205 ALLOW_RECODE_KFMAXBW = 1,
206 // Allow recode only for KF/ARF/GF frames.
207 ALLOW_RECODE_KFARFGF = 2,
208 // Allow recode for all frames based on bitrate constraints.
213 // encode_breakout is disabled.
214 ENCODE_BREAKOUT_DISABLED = 0,
215 // encode_breakout is enabled.
216 ENCODE_BREAKOUT_ENABLED = 1,
217 // encode_breakout is enabled with small max_thresh limit.
218 ENCODE_BREAKOUT_LIMITED = 2
219 } ENCODE_BREAKOUT_TYPE;
222 // Search partitions using RD/NONRD criterion
223 SEARCH_PARTITION = 0,
225 // Always use a fixed size partition
228 // Use a fixed size partition in every 64X64 SB, where the size is
229 // determined based on source variance
230 VAR_BASED_FIXED_PARTITION = 2,
232 // Use an arbitrary partitioning scheme based on source variance within
235 } PARTITION_SEARCH_TYPE;
238 // Frame level coding parameter update
239 int frame_parameter_update;
241 // Motion search method (Diamond, NSTEP, Hex, Big Diamond, Square, etc).
242 SEARCH_METHODS search_method;
244 RECODE_LOOP_TYPE recode_loop;
246 // Subpel_search_method can only be subpel_tree which does a subpixel
247 // logarithmic search that keeps stepping at 1/2 pixel units until
248 // you stop getting a gain, and then goes on to 1/4 and repeats
249 // the same process. Along the way it skips many diagonals.
250 SUBPEL_SEARCH_METHODS subpel_search_method;
252 // Maximum number of steps in logarithmic subpel search before giving up.
253 int subpel_iters_per_step;
255 // Control when to stop subpel search
256 int subpel_force_stop;
258 // Thresh_mult is used to set a threshold for the rd score. A higher value
259 // means that we will accept the best mode so far more often. This number
260 // is used in combination with the current block size, and thresh_freq_fact
261 // to pick a threshold.
262 int thresh_mult[MAX_MODES];
263 int thresh_mult_sub8x8[MAX_REFS];
265 // This parameter controls the number of steps we'll do in a diamond
267 int max_step_search_steps;
269 // This parameter controls which step in the n-step process we start at.
270 // It's changed adaptively based on circumstances.
271 int reduce_first_step_size;
273 // If this is set to 1, we limit the motion search range to 2 times the
274 // largest motion vector found in the last frame.
275 int auto_mv_step_size;
277 // Trellis (dynamic programming) optimization of quantized values (+1, 0).
278 int optimize_coefficients;
280 // Always set to 0. If on it enables 0 cost background transmission
281 // (except for the initial transmission of the segmentation). The feature is
282 // disabled because the addition of very large block sizes make the
283 // backgrounds very to cheap to encode, and the segmentation we have
285 int static_segmentation;
287 // If 1 we iterate finding a best reference for 2 ref frames together - via
288 // a log search that iterates 4 times (check around mv for last for best
289 // error of combined predictor then check around mv for alt). If 0 we
290 // we just use the best motion vector found for each frame by itself.
291 int comp_inter_joint_search_thresh;
293 // This variable is used to cap the maximum number of times we skip testing a
294 // mode to be evaluated. A high value means we will be faster.
295 int adaptive_rd_thresh;
297 // Enables skipping the reconstruction step (idct, recon) in the
298 // intermediate steps assuming the last frame didn't have too many intra
299 // blocks and the q is less than a threshold.
301 int skip_encode_frame;
303 // This variable allows us to reuse the last frames partition choices
304 // (64x64 v 32x32 etc) for this frame. It can be set to only use the last
305 // frame as a starting point in low motion scenes or always use it. If set
306 // we use last partitioning_redo frequency to determine how often to redo
307 // the partitioning from scratch. Adjust_partitioning_from_last_frame
308 // enables us to adjust up or down one partitioning from the last frames
310 LAST_FRAME_PARTITION_METHOD use_lastframe_partitioning;
312 // Determine which method we use to determine transform size. We can choose
313 // between options like full rd, largest for prediction size, largest
314 // for intra and model coefs for the rest.
315 TX_SIZE_SEARCH_METHOD tx_size_search_method;
317 // Low precision 32x32 fdct keeps everything in 16 bits and thus is less
318 // precise but significantly faster than the non lp version.
321 // TODO(JBB): remove this as its no longer used.
323 // After looking at the first set of modes (set by index here), skip
324 // checking modes for reference frames that don't match the reference frame
325 // of the best so far.
328 // TODO(JBB): Remove this.
329 int reference_masking;
331 PARTITION_SEARCH_TYPE partition_search_type;
333 // Used if partition_search_type = FIXED_SIZE_PARTITION
334 BLOCK_SIZE always_this_block_size;
336 // Skip rectangular partition test when partition type none gives better
337 // rd than partition type split.
338 int less_rectangular_check;
340 // Disable testing non square partitions. (eg 16x32)
341 int use_square_partition_only;
343 // Sets min and max partition sizes for this 64x64 region based on the
344 // same 64x64 in last encoded frame, and the left and above neighbor.
345 AUTO_MIN_MAX_MODE auto_min_max_partition_size;
347 // Min and max partition size we enable (block_size) as per auto
348 // min max, but also used by adjust partitioning, and pick_partitioning.
349 BLOCK_SIZE min_partition_size;
350 BLOCK_SIZE max_partition_size;
352 // Whether or not we allow partitions one smaller or one greater than the last
353 // frame's partitioning. Only used if use_lastframe_partitioning is set.
354 int adjust_partitioning_from_last_frame;
356 // How frequently we re do the partitioning from scratch. Only used if
357 // use_lastframe_partitioning is set.
358 int last_partitioning_redo_frequency;
360 // Disables sub 8x8 blocksizes in different scenarios: Choices are to disable
361 // it always, to allow it for only Last frame and Intra, disable it for all
362 // inter modes or to enable it always.
363 int disable_split_mask;
365 // TODO(jingning): combine the related motion search speed features
366 // This allows us to use motion search at other sizes as a starting
367 // point for this motion search and limits the search range around it.
368 int adaptive_motion_search;
370 // Allows sub 8x8 modes to use the prediction filter that was determined
371 // best for 8x8 mode. If set to 0 we always re check all the filters for
372 // sizes less than 8x8, 1 means we check all filter modes if no 8x8 filter
373 // was selected, and 2 means we use 8 tap if no 8x8 filter mode was selected.
374 int adaptive_pred_interp_filter;
376 // Implements various heuristics to skip searching modes
377 // The heuristics selected are based on flags
378 // defined in the MODE_SEARCH_SKIP_HEURISTICS enum
379 unsigned int mode_search_skip_flags;
381 // A source variance threshold below which the split mode is disabled
382 unsigned int disable_split_var_thresh;
384 // A source variance threshold below which filter search is disabled
385 // Choose a very large value (UINT_MAX) to use 8-tap always
386 unsigned int disable_filter_search_var_thresh;
388 // These bit masks allow you to enable or disable intra modes for each
389 // transform size separately.
390 int intra_y_mode_mask[TX_SIZES];
391 int intra_uv_mode_mask[TX_SIZES];
393 // This variable enables an early break out of mode testing if the model for
394 // rd built from the prediction signal indicates a value that's much
395 // higher than the best rd we've seen so far.
398 // This enables us to use an estimate for intra rd based on dc mode rather
399 // than choosing an actual uv mode in the stage of encoding before the actual
401 int use_uv_intra_rd_estimate;
403 // This feature controls how the loop filter level is determined:
404 // 0: Try the full image with different values.
405 // 1: Try a small portion of the image with different values.
406 // 2: Estimate the level based on quantizer and frame type
407 int use_fast_lpf_pick;
409 // This feature limits the number of coefficients updates we actually do
410 // by only looking at counts from 1/2 the bands.
411 int use_fast_coef_updates; // 0: 2-loop, 1: 1-loop, 2: 1-loop reduced
413 // This flag controls the use of non-RD mode decision.
414 int use_nonrd_pick_mode;
416 // This variable sets the encode_breakout threshold. Currently, it is only
417 // enabled in real time mode.
418 int encode_breakout_thresh;
420 // A binary mask indicating if NEARESTMV, NEARMV, ZEROMV, NEWMV
421 // modes are disabled in order from LSB to MSB for each BLOCK_SIZE.
422 int disable_inter_mode_mask[BLOCK_SIZES];
427 int target_bandwidth;
428 int64_t starting_buffer_level;
429 int64_t optimal_buffer_level;
430 int64_t maximum_buffer_size;
435 typedef struct VP9_COMP {
436 DECLARE_ALIGNED(16, int16_t, y_quant[QINDEX_RANGE][8]);
437 DECLARE_ALIGNED(16, int16_t, y_quant_shift[QINDEX_RANGE][8]);
438 DECLARE_ALIGNED(16, int16_t, y_zbin[QINDEX_RANGE][8]);
439 DECLARE_ALIGNED(16, int16_t, y_round[QINDEX_RANGE][8]);
441 DECLARE_ALIGNED(16, int16_t, uv_quant[QINDEX_RANGE][8]);
442 DECLARE_ALIGNED(16, int16_t, uv_quant_shift[QINDEX_RANGE][8]);
443 DECLARE_ALIGNED(16, int16_t, uv_zbin[QINDEX_RANGE][8]);
444 DECLARE_ALIGNED(16, int16_t, uv_round[QINDEX_RANGE][8]);
447 DECLARE_ALIGNED(16, int16_t, a_quant[QINDEX_RANGE][8]);
448 DECLARE_ALIGNED(16, int16_t, a_quant_shift[QINDEX_RANGE][8]);
449 DECLARE_ALIGNED(16, int16_t, a_zbin[QINDEX_RANGE][8]);
450 DECLARE_ALIGNED(16, int16_t, a_round[QINDEX_RANGE][8]);
456 struct lookahead_ctx *lookahead;
457 struct lookahead_entry *source;
458 #if CONFIG_MULTIPLE_ARF
459 struct lookahead_entry *alt_ref_source[REF_FRAMES];
461 struct lookahead_entry *alt_ref_source;
464 YV12_BUFFER_CONFIG *Source;
465 YV12_BUFFER_CONFIG *un_scaled_source;
466 YV12_BUFFER_CONFIG scaled_source;
468 int key_frame_frequency;
470 int gold_is_last; // gold same as last frame ( short circuit gold searches)
471 int alt_is_last; // Alt same as last ( short circuit altref search)
472 int gold_is_alt; // don't do both alt and gold search ( just do gold).
474 int scaled_ref_idx[3];
479 #if CONFIG_MULTIPLE_ARF
480 int alt_ref_fb_idx[REF_FRAMES - 3];
482 int refresh_last_frame;
483 int refresh_golden_frame;
484 int refresh_alt_ref_frame;
486 int ext_refresh_frame_flags_pending;
487 int ext_refresh_last_frame;
488 int ext_refresh_golden_frame;
489 int ext_refresh_alt_ref_frame;
491 int ext_refresh_frame_context_pending;
492 int ext_refresh_frame_context;
494 YV12_BUFFER_CONFIG last_frame_uf;
497 unsigned int tok_count[4][1 << 6];
499 #if CONFIG_MULTIPLE_ARF
500 // Position within a frame coding order (including any additional ARF frames).
501 unsigned int sequence_number;
502 // Next frame in naturally occurring order that has not yet been coded.
503 int next_frame_in_order;
506 // Ambient reconstruction err target for force key frames
509 int rd_threshes[MAX_SEGMENTS][BLOCK_SIZES][MAX_MODES];
510 int rd_thresh_freq_fact[BLOCK_SIZES][MAX_MODES];
511 int rd_thresh_sub8x8[MAX_SEGMENTS][BLOCK_SIZES][MAX_REFS];
512 int rd_thresh_freq_sub8x8[BLOCK_SIZES][MAX_REFS];
514 int64_t rd_comp_pred_diff[REFERENCE_MODES];
515 int64_t rd_prediction_type_threshes[4][REFERENCE_MODES];
516 int64_t rd_tx_select_diff[TX_MODES];
517 // FIXME(rbultje) can this overflow?
518 int rd_tx_select_threshes[4][TX_MODES];
520 int64_t rd_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
521 int64_t rd_filter_threshes[4][SWITCHABLE_FILTER_CONTEXTS];
522 int64_t rd_filter_cache[SWITCHABLE_FILTER_CONTEXTS];
523 int64_t mask_filter_rd;
528 CODING_CONTEXT coding_context;
531 int zbin_mode_boost_enabled;
532 int active_arnr_frames; // <= cpi->oxcf.arnr_max_frames
533 int active_arnr_strength; // <= cpi->oxcf.arnr_max_strength
535 double output_framerate;
536 int64_t last_time_stamp_seen;
537 int64_t last_end_time_stamp_seen;
538 int64_t first_time_stamp_ever;
542 int cq_target_quality;
544 vp9_coeff_count coef_counts[TX_SIZES][PLANE_TYPES];
545 vp9_coeff_probs_model frame_coef_probs[TX_SIZES][PLANE_TYPES];
546 vp9_coeff_stats frame_branch_ct[TX_SIZES][PLANE_TYPES];
548 struct vpx_codec_pkt_list *output_pkt_list;
550 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
551 int mbgraph_n_frames; // number of frames filled in the above
552 int static_mb_pct; // % forced skip mbs by segmentation
553 int seg0_progress, seg0_idx, seg0_cnt;
555 // for real time encoding
561 vp9_prob last_skip_false_probs[3][SKIP_CONTEXTS];
562 int last_skip_probs_q[3];
568 unsigned int max_mv_magnitude;
571 // Default value is 1. From first pass stats, encode_breakout may be disabled.
572 ENCODE_BREAKOUT_TYPE allow_encode_breakout;
574 // Get threshold from external input. In real time mode, it can be
575 // overwritten according to encoding speed.
578 unsigned char *segmentation_map;
580 // segment threashold for encode breakout
581 int segment_encode_breakout[MAX_SEGMENTS];
583 unsigned char *complexity_map;
585 unsigned char *active_map;
586 unsigned int active_map_enabled;
588 fractional_mv_step_fp *find_fractional_mv_step;
589 fractional_mv_step_comp_fp *find_fractional_mv_step_comp;
590 vp9_full_search_fn_t full_search_sad;
591 vp9_refining_search_fn_t refining_search_sad;
592 vp9_diamond_search_fn_t diamond_search_sad;
593 vp9_variance_fn_ptr_t fn_ptr[BLOCK_SIZES];
594 uint64_t time_receive_data;
595 uint64_t time_compress_data;
596 uint64_t time_pick_lpf;
597 uint64_t time_encode_sb_row;
599 struct twopass_rc twopass;
601 YV12_BUFFER_CONFIG alt_ref_buffer;
602 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
603 int fixed_divide[512];
605 #if CONFIG_INTERNAL_STATS
606 unsigned int mode_chosen_counts[MAX_MODES];
613 uint64_t total_sq_error;
614 uint64_t total_samples;
620 uint64_t totalp_sq_error;
621 uint64_t totalp_samples;
624 double summed_quality;
625 double summed_weights;
626 double summedp_quality;
627 double summedp_weights;
628 unsigned int tot_recode_hits;
631 double total_ssimg_y;
632 double total_ssimg_u;
633 double total_ssimg_v;
634 double total_ssimg_all;
636 int b_calculate_ssimg;
638 int b_calculate_psnr;
640 // Per MB activity measurement
641 unsigned int activity_avg;
642 unsigned int *mb_activity_map;
643 int *mb_norm_activity_map;
644 int output_partition;
646 // Force next frame to intra when kf_auto says so.
647 int force_next_frame_intra;
651 int dummy_packing; /* flag to indicate if packing is dummy */
653 unsigned int tx_stepdown_count[TX_SIZES];
661 int spatial_layer_id;
662 int temporal_layer_id;
663 int number_spatial_layers;
664 int number_temporal_layers;
665 // Layer context used for rate control in CBR mode, only defined for
666 // temporal layers for now.
667 LAYER_CONTEXT layer_context[VPX_TS_MAX_LAYERS];
670 #if CONFIG_MULTIPLE_ARF
671 // ARF tracking variables.
672 int multi_arf_enabled;
673 unsigned int frame_coding_order_period;
674 unsigned int new_frame_coding_order_period;
675 int frame_coding_order[MAX_LAG_BUFFERS * 2];
676 int arf_buffer_idx[MAX_LAG_BUFFERS * 3 / 2];
677 int arf_weight[MAX_LAG_BUFFERS];
679 int this_frame_weight;
684 int64_t mv_ref_stats[INTER_MODE_CONTEXTS][INTER_MODES - 1][2];
688 #ifdef MODE_TEST_HIT_STATS
689 // Debug / test stats
690 int64_t mode_test_hits[BLOCK_SIZES];
694 ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
695 ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16];
697 PARTITION_CONTEXT *above_seg_context;
698 PARTITION_CONTEXT left_seg_context[8];
701 static int get_ref_frame_idx(const VP9_COMP *cpi,
702 MV_REFERENCE_FRAME ref_frame) {
703 if (ref_frame == LAST_FRAME) {
704 return cpi->lst_fb_idx;
705 } else if (ref_frame == GOLDEN_FRAME) {
706 return cpi->gld_fb_idx;
708 return cpi->alt_fb_idx;
712 static YV12_BUFFER_CONFIG *get_ref_frame_buffer(VP9_COMP *cpi,
713 MV_REFERENCE_FRAME ref_frame) {
714 VP9_COMMON *const cm = &cpi->common;
715 return &cm->frame_bufs[cm->ref_frame_map[get_ref_frame_idx(cpi,
719 void vp9_encode_frame(VP9_COMP *cpi);
721 void vp9_set_speed_features(VP9_COMP *cpi);
723 int vp9_calc_ss_err(const YV12_BUFFER_CONFIG *source,
724 const YV12_BUFFER_CONFIG *reference);
726 void vp9_alloc_compressor_data(VP9_COMP *cpi);
728 int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget);
730 static int get_token_alloc(int mb_rows, int mb_cols) {
731 return mb_rows * mb_cols * (48 * 16 + 4);
734 static void set_ref_ptrs(VP9_COMMON *cm, MACROBLOCKD *xd,
735 MV_REFERENCE_FRAME ref0, MV_REFERENCE_FRAME ref1) {
736 xd->block_refs[0] = &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME
738 xd->block_refs[1] = &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME
746 #endif // VP9_ENCODER_VP9_ONYX_INT_H_