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.
145 USE_LARGESTINTRA_MODELINTER,
147 } TX_SIZE_SEARCH_METHOD;
151 RELAXED_NEIGHBORING_MIN_MAX = 1,
152 STRICT_NEIGHBORING_MIN_MAX = 2
156 // Values should be powers of 2 so that they can be selected as bits of
157 // an integer flags field
159 // terminate search early based on distortion so far compared to
160 // qp step, distortion in the neighborhood of the frame, etc.
161 FLAG_EARLY_TERMINATE = 1,
163 // skips comp inter modes if the best so far is an intra mode
164 FLAG_SKIP_COMP_BESTINTRA = 2,
166 // skips comp inter modes if the best single intermode so far does
167 // not have the same reference as one of the two references being
169 FLAG_SKIP_COMP_REFMISMATCH = 4,
171 // skips oblique intra modes if the best so far is an inter mode
172 FLAG_SKIP_INTRA_BESTINTER = 8,
174 // skips oblique intra modes at angles 27, 63, 117, 153 if the best
175 // intra so far is not one of the neighboring directions
176 FLAG_SKIP_INTRA_DIRMISMATCH = 16,
178 // skips intra modes other than DC_PRED if the source variance
180 FLAG_SKIP_INTRA_LOWVAR = 32,
181 } MODE_SEARCH_SKIP_LOGIC;
185 // Other methods to come
186 } SUBPEL_SEARCH_METHODS;
188 #define ALL_INTRA_MODES 0x3FF
189 #define INTRA_DC_ONLY 0x01
190 #define INTRA_DC_TM ((1 << TM_PRED) | (1 << DC_PRED))
191 #define INTRA_DC_H_V ((1 << DC_PRED) | (1 << V_PRED) | (1 << H_PRED))
192 #define INTRA_DC_TM_H_V (INTRA_DC_TM | (1 << V_PRED) | (1 << H_PRED))
195 LAST_FRAME_PARTITION_OFF = 0,
196 LAST_FRAME_PARTITION_LOW_MOTION = 1,
197 LAST_FRAME_PARTITION_ALL = 2
198 } LAST_FRAME_PARTITION_METHOD;
203 // Allow recode for KF and exceeding maximum frame bandwidth.
204 ALLOW_RECODE_KFMAXBW = 1,
205 // Allow recode only for KF/ARF/GF frames.
206 ALLOW_RECODE_KFARFGF = 2,
207 // Allow recode for all frames based on bitrate constraints.
212 // encode_breakout is disabled.
213 ENCODE_BREAKOUT_DISABLED = 0,
214 // encode_breakout is enabled.
215 ENCODE_BREAKOUT_ENABLED = 1,
216 // encode_breakout is enabled with small max_thresh limit.
217 ENCODE_BREAKOUT_LIMITED = 2
218 } ENCODE_BREAKOUT_TYPE;
221 // Frame level coding parameter update
222 int frame_parameter_update;
224 // Motion search method (Diamond, NSTEP, Hex, Big Diamond, Square, etc).
225 SEARCH_METHODS search_method;
227 RECODE_LOOP_TYPE recode_loop;
229 // Subpel_search_method can only be subpel_tree which does a subpixel
230 // logarithmic search that keeps stepping at 1/2 pixel units until
231 // you stop getting a gain, and then goes on to 1/4 and repeats
232 // the same process. Along the way it skips many diagonals.
233 SUBPEL_SEARCH_METHODS subpel_search_method;
235 // Maximum number of steps in logarithmic subpel search before giving up.
236 int subpel_iters_per_step;
238 // Control when to stop subpel search
239 int subpel_force_stop;
241 // Thresh_mult is used to set a threshold for the rd score. A higher value
242 // means that we will accept the best mode so far more often. This number
243 // is used in combination with the current block size, and thresh_freq_fact
244 // to pick a threshold.
245 int thresh_mult[MAX_MODES];
246 int thresh_mult_sub8x8[MAX_REFS];
248 // This parameter controls the number of steps we'll do in a diamond
250 int max_step_search_steps;
252 // This parameter controls which step in the n-step process we start at.
253 // It's changed adaptively based on circumstances.
254 int reduce_first_step_size;
256 // If this is set to 1, we limit the motion search range to 2 times the
257 // largest motion vector found in the last frame.
258 int auto_mv_step_size;
260 // Trellis (dynamic programming) optimization of quantized values (+1, 0).
261 int optimize_coefficients;
263 // Always set to 0. If on it enables 0 cost background transmission
264 // (except for the initial transmission of the segmentation). The feature is
265 // disabled because the addition of very large block sizes make the
266 // backgrounds very to cheap to encode, and the segmentation we have
268 int static_segmentation;
270 // If 1 we iterate finding a best reference for 2 ref frames together - via
271 // a log search that iterates 4 times (check around mv for last for best
272 // error of combined predictor then check around mv for alt). If 0 we
273 // we just use the best motion vector found for each frame by itself.
274 int comp_inter_joint_search_thresh;
276 // This variable is used to cap the maximum number of times we skip testing a
277 // mode to be evaluated. A high value means we will be faster.
278 int adaptive_rd_thresh;
280 // Enables skipping the reconstruction step (idct, recon) in the
281 // intermediate steps assuming the last frame didn't have too many intra
282 // blocks and the q is less than a threshold.
284 int skip_encode_frame;
286 // This variable allows us to reuse the last frames partition choices
287 // (64x64 v 32x32 etc) for this frame. It can be set to only use the last
288 // frame as a starting point in low motion scenes or always use it. If set
289 // we use last partitioning_redo frequency to determine how often to redo
290 // the partitioning from scratch. Adjust_partitioning_from_last_frame
291 // enables us to adjust up or down one partitioning from the last frames
293 LAST_FRAME_PARTITION_METHOD use_lastframe_partitioning;
295 // Determine which method we use to determine transform size. We can choose
296 // between options like full rd, largest for prediction size, largest
297 // for intra and model coefs for the rest.
298 TX_SIZE_SEARCH_METHOD tx_size_search_method;
300 // Low precision 32x32 fdct keeps everything in 16 bits and thus is less
301 // precise but significantly faster than the non lp version.
304 // TODO(JBB): remove this as its no longer used.
306 // If set partition size will always be always_this_block_size.
307 int use_one_partition_size_always;
309 // Skip rectangular partition test when partition type none gives better
310 // rd than partition type split.
311 int less_rectangular_check;
313 // Disable testing non square partitions. (eg 16x32)
314 int use_square_partition_only;
316 // After looking at the first set of modes (set by index here), skip
317 // checking modes for reference frames that don't match the reference frame
318 // of the best so far.
321 // TODO(JBB): Remove this.
322 int reference_masking;
324 // Used in conjunction with use_one_partition_size_always.
325 BLOCK_SIZE always_this_block_size;
327 // Sets min and max partition sizes for this 64x64 region based on the
328 // same 64x64 in last encoded frame, and the left and above neighbor.
329 AUTO_MIN_MAX_MODE auto_min_max_partition_size;
331 // Min and max partition size we enable (block_size) as per auto
332 // min max, but also used by adjust partitioning, and pick_partitioning.
333 BLOCK_SIZE min_partition_size;
334 BLOCK_SIZE max_partition_size;
336 // Whether or not we allow partitions one smaller or one greater than the last
337 // frame's partitioning. Only used if use_lastframe_partitioning is set.
338 int adjust_partitioning_from_last_frame;
340 // How frequently we re do the partitioning from scratch. Only used if
341 // use_lastframe_partitioning is set.
342 int last_partitioning_redo_frequency;
344 // Disables sub 8x8 blocksizes in different scenarios: Choices are to disable
345 // it always, to allow it for only Last frame and Intra, disable it for all
346 // inter modes or to enable it always.
347 int disable_split_mask;
349 // TODO(jingning): combine the related motion search speed features
350 // This allows us to use motion search at other sizes as a starting
351 // point for this motion search and limits the search range around it.
352 int adaptive_motion_search;
354 // Allows sub 8x8 modes to use the prediction filter that was determined
355 // best for 8x8 mode. If set to 0 we always re check all the filters for
356 // sizes less than 8x8, 1 means we check all filter modes if no 8x8 filter
357 // was selected, and 2 means we use 8 tap if no 8x8 filter mode was selected.
358 int adaptive_pred_interp_filter;
360 // Implements various heuristics to skip searching modes
361 // The heuristics selected are based on flags
362 // defined in the MODE_SEARCH_SKIP_HEURISTICS enum
363 unsigned int mode_search_skip_flags;
365 // A source variance threshold below which the split mode is disabled
366 unsigned int disable_split_var_thresh;
368 // A source variance threshold below which filter search is disabled
369 // Choose a very large value (UINT_MAX) to use 8-tap always
370 unsigned int disable_filter_search_var_thresh;
372 // These bit masks allow you to enable or disable intra modes for each
373 // transform size separately.
374 int intra_y_mode_mask[TX_SIZES];
375 int intra_uv_mode_mask[TX_SIZES];
377 // This variable enables an early break out of mode testing if the model for
378 // rd built from the prediction signal indicates a value that's much
379 // higher than the best rd we've seen so far.
382 // This enables us to use an estimate for intra rd based on dc mode rather
383 // than choosing an actual uv mode in the stage of encoding before the actual
385 int use_uv_intra_rd_estimate;
387 // This feature controls how the loop filter level is determined:
388 // 0: Try the full image with different values.
389 // 1: Try a small portion of the image with different values.
390 // 2: Estimate the level based on quantizer and frame type
391 int use_fast_lpf_pick;
393 // This feature limits the number of coefficients updates we actually do
394 // by only looking at counts from 1/2 the bands.
395 int use_fast_coef_updates; // 0: 2-loop, 1: 1-loop, 2: 1-loop reduced
397 // This flag controls the use of non-RD mode decision.
400 // This variable sets the encode_breakout threshold. Currently, it is only
401 // enabled in real time mode.
402 int encode_breakout_thresh;
407 int target_bandwidth;
408 int64_t starting_buffer_level;
409 int64_t optimal_buffer_level;
410 int64_t maximum_buffer_size;
415 typedef struct VP9_COMP {
416 DECLARE_ALIGNED(16, int16_t, y_quant[QINDEX_RANGE][8]);
417 DECLARE_ALIGNED(16, int16_t, y_quant_shift[QINDEX_RANGE][8]);
418 DECLARE_ALIGNED(16, int16_t, y_zbin[QINDEX_RANGE][8]);
419 DECLARE_ALIGNED(16, int16_t, y_round[QINDEX_RANGE][8]);
421 DECLARE_ALIGNED(16, int16_t, uv_quant[QINDEX_RANGE][8]);
422 DECLARE_ALIGNED(16, int16_t, uv_quant_shift[QINDEX_RANGE][8]);
423 DECLARE_ALIGNED(16, int16_t, uv_zbin[QINDEX_RANGE][8]);
424 DECLARE_ALIGNED(16, int16_t, uv_round[QINDEX_RANGE][8]);
427 DECLARE_ALIGNED(16, int16_t, a_quant[QINDEX_RANGE][8]);
428 DECLARE_ALIGNED(16, int16_t, a_quant_shift[QINDEX_RANGE][8]);
429 DECLARE_ALIGNED(16, int16_t, a_zbin[QINDEX_RANGE][8]);
430 DECLARE_ALIGNED(16, int16_t, a_round[QINDEX_RANGE][8]);
436 struct lookahead_ctx *lookahead;
437 struct lookahead_entry *source;
438 #if CONFIG_MULTIPLE_ARF
439 struct lookahead_entry *alt_ref_source[REF_FRAMES];
441 struct lookahead_entry *alt_ref_source;
444 YV12_BUFFER_CONFIG *Source;
445 YV12_BUFFER_CONFIG *un_scaled_source;
446 YV12_BUFFER_CONFIG scaled_source;
448 unsigned int key_frame_frequency;
450 int gold_is_last; // gold same as last frame ( short circuit gold searches)
451 int alt_is_last; // Alt same as last ( short circuit altref search)
452 int gold_is_alt; // don't do both alt and gold search ( just do gold).
454 int scaled_ref_idx[3];
459 #if CONFIG_MULTIPLE_ARF
460 int alt_ref_fb_idx[REF_FRAMES - 3];
462 int refresh_last_frame;
463 int refresh_golden_frame;
464 int refresh_alt_ref_frame;
466 int ext_refresh_frame_flags_pending;
467 int ext_refresh_last_frame;
468 int ext_refresh_golden_frame;
469 int ext_refresh_alt_ref_frame;
471 int ext_refresh_frame_context_pending;
472 int ext_refresh_frame_context;
474 YV12_BUFFER_CONFIG last_frame_uf;
477 unsigned int tok_count[4][1 << 6];
479 #if CONFIG_MULTIPLE_ARF
480 // Position within a frame coding order (including any additional ARF frames).
481 unsigned int sequence_number;
482 // Next frame in naturally occurring order that has not yet been coded.
483 int next_frame_in_order;
486 // Ambient reconstruction err target for force key frames
489 unsigned int mode_chosen_counts[MAX_MODES];
490 unsigned int sub8x8_mode_chosen_counts[MAX_REFS];
491 int64_t mode_skip_mask;
493 int set_ref_frame_mask;
495 int rd_threshes[MAX_SEGMENTS][BLOCK_SIZES][MAX_MODES];
496 int rd_thresh_freq_fact[BLOCK_SIZES][MAX_MODES];
497 int rd_thresh_sub8x8[MAX_SEGMENTS][BLOCK_SIZES][MAX_REFS];
498 int rd_thresh_freq_sub8x8[BLOCK_SIZES][MAX_REFS];
500 int64_t rd_comp_pred_diff[REFERENCE_MODES];
501 int64_t rd_prediction_type_threshes[4][REFERENCE_MODES];
502 int64_t rd_tx_select_diff[TX_MODES];
503 // FIXME(rbultje) can this overflow?
504 int rd_tx_select_threshes[4][TX_MODES];
506 int64_t rd_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
507 int64_t rd_filter_threshes[4][SWITCHABLE_FILTER_CONTEXTS];
508 int64_t rd_filter_cache[SWITCHABLE_FILTER_CONTEXTS];
509 int64_t mask_filter_rd;
514 CODING_CONTEXT coding_context;
517 int zbin_mode_boost_enabled;
518 int active_arnr_frames; // <= cpi->oxcf.arnr_max_frames
519 int active_arnr_strength; // <= cpi->oxcf.arnr_max_strength
521 double output_framerate;
522 int64_t last_time_stamp_seen;
523 int64_t last_end_time_stamp_seen;
524 int64_t first_time_stamp_ever;
528 int cq_target_quality;
530 vp9_coeff_count coef_counts[TX_SIZES][PLANE_TYPES];
531 vp9_coeff_probs_model frame_coef_probs[TX_SIZES][PLANE_TYPES];
532 vp9_coeff_stats frame_branch_ct[TX_SIZES][PLANE_TYPES];
534 struct vpx_codec_pkt_list *output_pkt_list;
536 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
537 int mbgraph_n_frames; // number of frames filled in the above
538 int static_mb_pct; // % forced skip mbs by segmentation
539 int seg0_progress, seg0_idx, seg0_cnt;
541 // for real time encoding
547 vp9_prob last_skip_false_probs[3][SKIP_CONTEXTS];
548 int last_skip_probs_q[3];
554 unsigned int max_mv_magnitude;
557 // Default value is 1. From first pass stats, encode_breakout may be disabled.
558 ENCODE_BREAKOUT_TYPE allow_encode_breakout;
560 // Get threshold from external input. In real time mode, it can be
561 // overwritten according to encoding speed.
564 unsigned char *segmentation_map;
566 // segment threashold for encode breakout
567 int segment_encode_breakout[MAX_SEGMENTS];
569 unsigned char *complexity_map;
571 unsigned char *active_map;
572 unsigned int active_map_enabled;
574 fractional_mv_step_fp *find_fractional_mv_step;
575 fractional_mv_step_comp_fp *find_fractional_mv_step_comp;
576 vp9_full_search_fn_t full_search_sad;
577 vp9_refining_search_fn_t refining_search_sad;
578 vp9_diamond_search_fn_t diamond_search_sad;
579 vp9_variance_fn_ptr_t fn_ptr[BLOCK_SIZES];
580 uint64_t time_receive_data;
581 uint64_t time_compress_data;
582 uint64_t time_pick_lpf;
583 uint64_t time_encode_sb_row;
585 struct twopass_rc twopass;
587 YV12_BUFFER_CONFIG alt_ref_buffer;
588 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
589 int fixed_divide[512];
591 #if CONFIG_INTERNAL_STATS
597 uint64_t total_sq_error;
598 uint64_t total_samples;
604 uint64_t totalp_sq_error;
605 uint64_t totalp_samples;
608 double summed_quality;
609 double summed_weights;
610 double summedp_quality;
611 double summedp_weights;
612 unsigned int tot_recode_hits;
615 double total_ssimg_y;
616 double total_ssimg_u;
617 double total_ssimg_v;
618 double total_ssimg_all;
620 int b_calculate_ssimg;
622 int b_calculate_psnr;
624 // Per MB activity measurement
625 unsigned int activity_avg;
626 unsigned int *mb_activity_map;
627 int *mb_norm_activity_map;
628 int output_partition;
630 // Force next frame to intra when kf_auto says so.
631 int force_next_frame_intra;
635 int dummy_packing; /* flag to indicate if packing is dummy */
637 unsigned int tx_stepdown_count[TX_SIZES];
645 int spatial_layer_id;
646 int temporal_layer_id;
647 int number_spatial_layers;
648 int number_temporal_layers;
649 // Layer context used for rate control in CBR mode, only defined for
650 // temporal layers for now.
651 LAYER_CONTEXT layer_context[VPX_TS_MAX_LAYERS];
654 #if CONFIG_MULTIPLE_ARF
655 // ARF tracking variables.
656 int multi_arf_enabled;
657 unsigned int frame_coding_order_period;
658 unsigned int new_frame_coding_order_period;
659 int frame_coding_order[MAX_LAG_BUFFERS * 2];
660 int arf_buffer_idx[MAX_LAG_BUFFERS * 3 / 2];
661 int arf_weight[MAX_LAG_BUFFERS];
663 int this_frame_weight;
668 int64_t mv_ref_stats[INTER_MODE_CONTEXTS][INTER_MODES - 1][2];
672 #ifdef MODE_TEST_HIT_STATS
673 // Debug / test stats
674 int64_t mode_test_hits[BLOCK_SIZES];
678 ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
679 ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16];
681 PARTITION_CONTEXT *above_seg_context;
682 PARTITION_CONTEXT left_seg_context[8];
685 static int get_ref_frame_idx(const VP9_COMP *cpi,
686 MV_REFERENCE_FRAME ref_frame) {
687 if (ref_frame == LAST_FRAME) {
688 return cpi->lst_fb_idx;
689 } else if (ref_frame == GOLDEN_FRAME) {
690 return cpi->gld_fb_idx;
692 return cpi->alt_fb_idx;
696 static YV12_BUFFER_CONFIG *get_ref_frame_buffer(VP9_COMP *cpi,
697 MV_REFERENCE_FRAME ref_frame) {
698 VP9_COMMON *const cm = &cpi->common;
699 return &cm->frame_bufs[cm->ref_frame_map[get_ref_frame_idx(cpi,
703 void vp9_encode_frame(VP9_COMP *cpi);
705 void vp9_pack_bitstream(VP9_COMP *cpi, uint8_t *dest, size_t *size);
707 void vp9_set_speed_features(VP9_COMP *cpi);
709 int vp9_calc_ss_err(const YV12_BUFFER_CONFIG *source,
710 const YV12_BUFFER_CONFIG *reference);
712 void vp9_alloc_compressor_data(VP9_COMP *cpi);
714 int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget);
716 static int get_token_alloc(int mb_rows, int mb_cols) {
717 return mb_rows * mb_cols * (48 * 16 + 4);
720 static void set_ref_ptrs(VP9_COMMON *cm, MACROBLOCKD *xd,
721 MV_REFERENCE_FRAME ref0, MV_REFERENCE_FRAME ref1) {
722 xd->block_refs[0] = &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME
724 xd->block_refs[1] = &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME
732 #endif // VP9_ENCODER_VP9_ONYX_INT_H_