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.
12 #ifndef VP9_ENCODER_VP9_ONYX_INT_H_
13 #define VP9_ENCODER_VP9_ONYX_INT_H_
16 #include "./vpx_config.h"
17 #include "vp9/common/vp9_onyx.h"
18 #include "vp9/encoder/vp9_treewriter.h"
19 #include "vp9/encoder/vp9_tokenize.h"
20 #include "vp9/common/vp9_onyxc_int.h"
21 #include "vp9/encoder/vp9_variance.h"
22 #include "vp9/encoder/vp9_encodemb.h"
23 #include "vp9/encoder/vp9_quantize.h"
24 #include "vp9/common/vp9_entropy.h"
25 #include "vp9/common/vp9_entropymode.h"
26 #include "vpx_ports/mem.h"
27 #include "vpx/internal/vpx_codec_internal.h"
28 #include "vp9/encoder/vp9_mcomp.h"
29 #include "vp9/common/vp9_findnearmv.h"
30 #include "vp9/encoder/vp9_lookahead.h"
32 // Experimental rate control switches
34 #define ONE_SHOT_Q_ESTIMATE 0
35 #define STRICT_ONE_SHOT_Q 0
36 #define DISABLE_RC_LONG_TERM_MEM 0
39 // #define SPEEDSTATS 1
40 #if CONFIG_MULTIPLE_ARF
41 // Set MIN_GF_INTERVAL to 1 for the full decomposition.
42 #define MIN_GF_INTERVAL 2
44 #define MIN_GF_INTERVAL 4
46 #define DEFAULT_GF_INTERVAL 7
48 #define KEY_FRAME_CONTEXT 5
52 #define MIN_THRESHMULT 32
53 #define MAX_THRESHMULT 512
55 #define GF_ZEROMV_ZBIN_BOOST 0
56 #define LF_ZEROMV_ZBIN_BOOST 0
57 #define MV_ZBIN_BOOST 0
58 #define SPLIT_MV_ZBIN_BOOST 0
59 #define INTRA_ZBIN_BOOST 0
63 int nmvjointcost[MV_JOINTS];
64 int nmvcosts[2][MV_VALS];
65 int nmvcosts_hp[2][MV_VALS];
67 vp9_prob segment_pred_probs[PREDICTION_PROBS];
68 vp9_prob intra_inter_prob[INTRA_INTER_CONTEXTS];
69 vp9_prob comp_inter_prob[COMP_INTER_CONTEXTS];
70 vp9_prob single_ref_prob[REF_CONTEXTS][2];
71 vp9_prob comp_ref_prob[REF_CONTEXTS];
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];
80 vp9_coeff_probs_model coef_probs[TX_SIZE_MAX_SB][BLOCK_TYPES];
82 vp9_prob y_mode_prob[4][VP9_INTRA_MODES - 1];
83 vp9_prob uv_mode_prob[VP9_INTRA_MODES][VP9_INTRA_MODES - 1];
84 vp9_prob partition_prob[2][NUM_PARTITION_CONTEXTS][PARTITION_TYPES - 1];
86 vp9_prob switchable_interp_prob[VP9_SWITCHABLE_FILTERS + 1]
87 [VP9_SWITCHABLE_FILTERS - 1];
89 int inter_mode_counts[INTER_MODE_CONTEXTS][VP9_INTER_MODES - 1][2];
90 vp9_prob inter_mode_probs[INTER_MODE_CONTEXTS][VP9_INTER_MODES - 1];
92 vp9_prob tx_probs_8x8p[TX_SIZE_CONTEXTS][TX_SIZE_MAX_SB - 3];
93 vp9_prob tx_probs_16x16p[TX_SIZE_CONTEXTS][TX_SIZE_MAX_SB - 2];
94 vp9_prob tx_probs_32x32p[TX_SIZE_CONTEXTS][TX_SIZE_MAX_SB - 1];
95 vp9_prob mbskip_probs[MBSKIP_CONTEXTS];
102 double sr_coded_error;
103 double ssim_weighted_pred_err;
106 double pcnt_second_ref;
114 double mv_in_out_count;
122 double frame_intra_error;
123 double frame_coded_error;
124 double frame_pcnt_inter;
125 double frame_pcnt_motion;
127 double frame_mvr_abs;
129 double frame_mvc_abs;
130 } ONEPASS_FRAMESTATS;
137 MB_PREDICTION_MODE mode;
139 } ref[MAX_REF_FRAMES];
143 MBGRAPH_MB_STATS *mb_stats;
144 } MBGRAPH_FRAME_STATS;
206 USE_LARGESTINTRA_MODELINTER,
208 } TX_SIZE_SEARCH_METHOD;
212 SEARCH_METHODS search_method;
215 int iterative_sub_pixel;
216 int half_pixel_search;
217 int quarter_pixel_search;
218 int thresh_mult[MAX_MODES];
219 int max_step_search_steps;
220 int reduce_first_step_size;
221 int auto_mv_step_size;
222 int optimize_coefficients;
223 int search_best_filter;
224 int static_segmentation;
225 int comp_inter_joint_search_thresh;
226 int adaptive_rd_thresh;
228 int use_lastframe_partitioning;
229 TX_SIZE_SEARCH_METHOD tx_size_search_method;
231 int use_avoid_tested_higherror;
232 int skip_lots_of_modes;
233 int adjust_thresholds_by_speed;
234 int partition_by_variance;
235 int use_one_partition_size_always;
236 int less_rectangular_check;
237 int use_square_partition_only;
238 BLOCK_SIZE_TYPE always_this_block_size;
239 int use_partitions_greater_than;
240 BLOCK_SIZE_TYPE greater_than_block_size;
241 int use_partitions_less_than;
242 BLOCK_SIZE_TYPE less_than_block_size;
243 int adjust_partitioning_from_last_frame;
244 int last_partitioning_redo_frequency;
246 // Search the D27, D63, D117 and D153 modes
247 // only if the best intra mode so far is one
248 // of the two directional modes nearest to each.
249 int conditional_oblique_intramodes;
266 BLOCK_MAX_SB_SEGMENTS,
269 typedef struct VP9_COMP {
271 DECLARE_ALIGNED(16, int16_t, y_quant[QINDEX_RANGE][8]);
272 DECLARE_ALIGNED(16, int16_t, y_quant_shift[QINDEX_RANGE][8]);
273 DECLARE_ALIGNED(16, int16_t, y_zbin[QINDEX_RANGE][8]);
274 DECLARE_ALIGNED(16, int16_t, y_round[QINDEX_RANGE][8]);
276 DECLARE_ALIGNED(16, int16_t, uv_quant[QINDEX_RANGE][8]);
277 DECLARE_ALIGNED(16, int16_t, uv_quant_shift[QINDEX_RANGE][8]);
278 DECLARE_ALIGNED(16, int16_t, uv_zbin[QINDEX_RANGE][8]);
279 DECLARE_ALIGNED(16, int16_t, uv_round[QINDEX_RANGE][8]);
282 DECLARE_ALIGNED(16, int16_t, a_quant[QINDEX_RANGE][8]);
283 DECLARE_ALIGNED(16, int16_t, a_quant_shift[QINDEX_RANGE][8]);
284 DECLARE_ALIGNED(16, int16_t, a_zbin[QINDEX_RANGE][8]);
285 DECLARE_ALIGNED(16, int16_t, a_round[QINDEX_RANGE][8]);
292 struct lookahead_ctx *lookahead;
293 struct lookahead_entry *source;
294 #if CONFIG_MULTIPLE_ARF
295 struct lookahead_entry *alt_ref_source[NUM_REF_FRAMES];
297 struct lookahead_entry *alt_ref_source;
300 YV12_BUFFER_CONFIG *Source;
301 YV12_BUFFER_CONFIG *un_scaled_source;
302 YV12_BUFFER_CONFIG scaled_source;
304 int source_alt_ref_pending; // frame in src_buffers has been identified to be encoded as an alt ref
305 int source_alt_ref_active; // an alt ref frame has been encoded and is usable
307 int is_src_frame_alt_ref; // source of frame to encode is an exact copy of an alt ref frame
309 int gold_is_last; // golden frame same as last frame ( short circuit gold searches)
310 int alt_is_last; // Alt reference frame same as last ( short circuit altref search)
311 int gold_is_alt; // don't do both alt and gold search ( just do gold).
313 int scaled_ref_idx[3];
317 #if CONFIG_MULTIPLE_ARF
318 int alt_ref_fb_idx[NUM_REF_FRAMES - 3];
320 int refresh_last_frame;
321 int refresh_golden_frame;
322 int refresh_alt_ref_frame;
323 YV12_BUFFER_CONFIG last_frame_uf;
326 unsigned int tok_count[4][1 << 6];
329 unsigned int frames_since_key;
330 unsigned int key_frame_frequency;
331 unsigned int this_key_frame_forced;
332 unsigned int next_key_frame_forced;
333 #if CONFIG_MULTIPLE_ARF
334 // Position within a frame coding order (including any additional ARF frames).
335 unsigned int sequence_number;
336 // Next frame in naturally occurring order that has not yet been coded.
337 int next_frame_in_order;
340 // Ambient reconstruction err target for force key frames
343 unsigned int mode_check_freq[MAX_MODES];
344 unsigned int mode_test_hit_counts[MAX_MODES];
345 unsigned int mode_chosen_counts[MAX_MODES];
347 int rd_thresh_mult[MAX_MODES];
348 int rd_baseline_thresh[BLOCK_SIZE_TYPES][MAX_MODES];
349 int rd_threshes[BLOCK_SIZE_TYPES][MAX_MODES];
350 int rd_thresh_freq_fact[BLOCK_SIZE_TYPES][MAX_MODES];
352 int64_t rd_comp_pred_diff[NB_PREDICTION_TYPES];
353 int rd_prediction_type_threshes[4][NB_PREDICTION_TYPES];
354 unsigned int intra_inter_count[INTRA_INTER_CONTEXTS][2];
355 unsigned int comp_inter_count[COMP_INTER_CONTEXTS][2];
356 unsigned int single_ref_count[REF_CONTEXTS][2][2];
357 unsigned int comp_ref_count[REF_CONTEXTS][2];
359 // FIXME contextualize
361 int64_t rd_tx_select_diff[NB_TXFM_MODES];
362 int rd_tx_select_threshes[4][NB_TXFM_MODES];
367 CODING_CONTEXT coding_context;
369 // Rate targetting variables
370 int this_frame_target;
371 int projected_frame_size;
372 int last_q[2]; // Separate values for Intra/Inter
373 int last_boosted_qindex; // Last boosted GF/KF/ARF q
375 double rate_correction_factor;
376 double key_frame_rate_correction_factor;
377 double gf_rate_correction_factor;
379 int frames_till_gf_update_due; // Count down till next GF
381 int gf_overspend_bits; // Total bits overspent becasue of GF boost (cumulative)
383 int non_gf_bitrate_adjustment; // Used in the few frames following a GF to recover the extra bits spent in that GF
385 int kf_overspend_bits; // Extra bits spent on key frames that need to be recovered on inter frames
386 int kf_bitrate_adjustment; // Current number of bit s to try and recover on each inter frame.
388 int baseline_gf_interval;
389 int active_arnr_frames; // <= cpi->oxcf.arnr_max_frames
390 int active_arnr_strength; // <= cpi->oxcf.arnr_max_strength
392 int64_t key_frame_count;
393 int prior_key_frame_distance[KEY_FRAME_CONTEXT];
394 int per_frame_bandwidth; // Current section per frame bandwidth target
395 int av_per_frame_bandwidth; // Average frame size target for clip
396 int min_frame_bandwidth; // Minimum allocation that should be used for any frame
397 int inter_frame_target;
398 double output_frame_rate;
399 int64_t last_time_stamp_seen;
400 int64_t last_end_time_stamp_seen;
401 int64_t first_time_stamp_ever;
406 int avg_frame_qindex;
411 int zbin_mode_boost_enabled;
413 int64_t total_byte_count;
420 int rolling_target_bits;
421 int rolling_actual_bits;
423 int long_rolling_target_bits;
424 int long_rolling_actual_bits;
426 int64_t total_actual_bits;
427 int total_target_vs_actual; // debug stats
430 int active_worst_quality;
432 int active_best_quality;
434 int cq_target_quality;
436 int y_mode_count[4][VP9_INTRA_MODES];
437 int y_uv_mode_count[VP9_INTRA_MODES][VP9_INTRA_MODES];
438 unsigned int partition_count[NUM_PARTITION_CONTEXTS][PARTITION_TYPES];
440 nmv_context_counts NMVcount;
442 vp9_coeff_count coef_counts[TX_SIZE_MAX_SB][BLOCK_TYPES];
443 vp9_coeff_probs_model frame_coef_probs[TX_SIZE_MAX_SB][BLOCK_TYPES];
444 vp9_coeff_stats frame_branch_ct[TX_SIZE_MAX_SB][BLOCK_TYPES];
449 int kf_zeromotion_pct;
451 int64_t target_bandwidth;
452 struct vpx_codec_pkt_list *output_pkt_list;
455 // Experimental code for lagged and one pass
456 ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS];
457 int one_pass_frame_index;
459 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
460 int mbgraph_n_frames; // number of frames filled in the above
461 int static_mb_pct; // % forced skip mbs by segmentation
462 int seg0_progress, seg0_idx, seg0_cnt;
464 int decimation_factor;
465 int decimation_count;
467 // for real time encoding
468 int avg_encode_time; // microsecond
469 int avg_pick_mode_time; // microsecond
471 unsigned int cpu_freq; // Mhz
472 int compressor_speed;
480 vp9_prob last_skip_false_probs[3][MBSKIP_CONTEXTS];
481 int last_skip_probs_q[3];
486 int error_bins[1024];
488 unsigned int max_mv_magnitude;
490 // Data used for real time conferencing mode to help determine if it would be good to update the gf
493 int gf_update_recommended;
495 unsigned char *segmentation_map;
497 // segment threashold for encode breakout
498 int segment_encode_breakout[MAX_MB_SEGMENTS];
500 unsigned char *active_map;
501 unsigned int active_map_enabled;
503 fractional_mv_step_fp *find_fractional_mv_step;
504 vp9_full_search_fn_t full_search_sad;
505 vp9_refining_search_fn_t refining_search_sad;
506 vp9_diamond_search_fn_t diamond_search_sad;
507 vp9_variance_fn_ptr_t fn_ptr[BLOCK_MAX_SB_SEGMENTS];
508 uint64_t time_receive_data;
509 uint64_t time_compress_data;
510 uint64_t time_pick_lpf;
511 uint64_t time_encode_mb_row;
514 unsigned int section_intra_rating;
515 unsigned int next_iiratio;
516 unsigned int this_iiratio;
517 FIRSTPASS_STATS total_stats;
518 FIRSTPASS_STATS this_frame_stats;
519 FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
520 FIRSTPASS_STATS total_left_stats;
523 int64_t clip_bits_total;
525 double modified_error_total;
526 double modified_error_used;
527 double modified_error_left;
528 double kf_intra_err_min;
529 double gf_intra_err_min;
533 int static_scene_max_gf_interval;
535 // Remaining error from uncoded frames in a gf group. Two pass use only
536 int64_t gf_group_error_left;
538 // Projected total bits available for a key frame group of frames
539 int64_t kf_group_bits;
541 // Error score of frames still to be coded in kf group
542 int64_t kf_group_error_left;
544 // Projected Bits available for a group of frames including 1 GF or ARF
545 int64_t gf_group_bits;
546 // Bits for the golden frame or ARF - 2 pass only
551 double est_max_qcorrection_factor;
554 YV12_BUFFER_CONFIG alt_ref_buffer;
555 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
556 int fixed_divide[512];
558 #if CONFIG_INTERNAL_STATS
564 double total_sq_error;
569 double total_sq_error2;
571 double summed_quality;
572 double summed_weights;
573 double summedp_quality;
574 double summedp_weights;
575 unsigned int tot_recode_hits;
578 double total_ssimg_y;
579 double total_ssimg_u;
580 double total_ssimg_v;
581 double total_ssimg_all;
583 int b_calculate_ssimg;
585 int b_calculate_psnr;
587 // Per MB activity measurement
588 unsigned int activity_avg;
589 unsigned int *mb_activity_map;
590 int *mb_norm_activity_map;
591 int output_partition;
593 /* force next frame to intra when kf_auto says so */
594 int force_next_frame_intra;
598 int dummy_packing; /* flag to indicate if packing is dummy */
600 unsigned int switchable_interp_count[VP9_SWITCHABLE_FILTERS + 1]
601 [VP9_SWITCHABLE_FILTERS];
602 unsigned int best_switchable_interp_count[VP9_SWITCHABLE_FILTERS];
604 unsigned int txfm_stepdown_count[TX_SIZE_MAX_SB];
609 #if CONFIG_MULTIPLE_ARF
610 // ARF tracking variables.
611 int multi_arf_enabled;
612 unsigned int frame_coding_order_period;
613 unsigned int new_frame_coding_order_period;
614 int frame_coding_order[MAX_LAG_BUFFERS * 2];
615 int arf_buffer_idx[MAX_LAG_BUFFERS * 3 / 2];
616 int arf_weight[MAX_LAG_BUFFERS];
618 int this_frame_weight;
623 int64_t mv_ref_stats[INTER_MODE_CONTEXTS][VP9_INTER_MODES - 1][2];
627 static int get_ref_frame_idx(VP9_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
628 if (ref_frame == LAST_FRAME) {
629 return cpi->lst_fb_idx;
630 } else if (ref_frame == GOLDEN_FRAME) {
631 return cpi->gld_fb_idx;
633 return cpi->alt_fb_idx;
637 void vp9_encode_frame(VP9_COMP *cpi);
639 void vp9_pack_bitstream(VP9_COMP *cpi, unsigned char *dest,
640 unsigned long *size);
642 void vp9_activity_masking(VP9_COMP *cpi, MACROBLOCK *x);
644 void vp9_set_speed_features(VP9_COMP *cpi);
646 extern int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source,
647 YV12_BUFFER_CONFIG *dest);
649 extern void vp9_alloc_compressor_data(VP9_COMP *cpi);
651 #endif // VP9_ENCODER_VP9_ONYX_INT_H_