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 int unused_mode_skip_lvl;
239 int reference_masking;
240 BLOCK_SIZE_TYPE always_this_block_size;
241 int use_partitions_greater_than;
242 BLOCK_SIZE_TYPE greater_than_block_size;
243 int use_partitions_less_than;
244 BLOCK_SIZE_TYPE less_than_block_size;
245 int adjust_partitioning_from_last_frame;
246 int last_partitioning_redo_frequency;
248 // Search the D27, D63, D117 and D153 modes
249 // only if the best intra mode so far is one
250 // of the two directional modes nearest to each.
251 int conditional_oblique_intramodes;
268 BLOCK_MAX_SB_SEGMENTS,
271 typedef struct VP9_COMP {
273 DECLARE_ALIGNED(16, int16_t, y_quant[QINDEX_RANGE][8]);
274 DECLARE_ALIGNED(16, int16_t, y_quant_shift[QINDEX_RANGE][8]);
275 DECLARE_ALIGNED(16, int16_t, y_zbin[QINDEX_RANGE][8]);
276 DECLARE_ALIGNED(16, int16_t, y_round[QINDEX_RANGE][8]);
278 DECLARE_ALIGNED(16, int16_t, uv_quant[QINDEX_RANGE][8]);
279 DECLARE_ALIGNED(16, int16_t, uv_quant_shift[QINDEX_RANGE][8]);
280 DECLARE_ALIGNED(16, int16_t, uv_zbin[QINDEX_RANGE][8]);
281 DECLARE_ALIGNED(16, int16_t, uv_round[QINDEX_RANGE][8]);
284 DECLARE_ALIGNED(16, int16_t, a_quant[QINDEX_RANGE][8]);
285 DECLARE_ALIGNED(16, int16_t, a_quant_shift[QINDEX_RANGE][8]);
286 DECLARE_ALIGNED(16, int16_t, a_zbin[QINDEX_RANGE][8]);
287 DECLARE_ALIGNED(16, int16_t, a_round[QINDEX_RANGE][8]);
294 struct lookahead_ctx *lookahead;
295 struct lookahead_entry *source;
296 #if CONFIG_MULTIPLE_ARF
297 struct lookahead_entry *alt_ref_source[NUM_REF_FRAMES];
299 struct lookahead_entry *alt_ref_source;
302 YV12_BUFFER_CONFIG *Source;
303 YV12_BUFFER_CONFIG *un_scaled_source;
304 YV12_BUFFER_CONFIG scaled_source;
306 int source_alt_ref_pending; // frame in src_buffers has been identified to be encoded as an alt ref
307 int source_alt_ref_active; // an alt ref frame has been encoded and is usable
309 int is_src_frame_alt_ref; // source of frame to encode is an exact copy of an alt ref frame
311 int gold_is_last; // golden frame same as last frame ( short circuit gold searches)
312 int alt_is_last; // Alt reference frame same as last ( short circuit altref search)
313 int gold_is_alt; // don't do both alt and gold search ( just do gold).
315 int scaled_ref_idx[3];
319 #if CONFIG_MULTIPLE_ARF
320 int alt_ref_fb_idx[NUM_REF_FRAMES - 3];
322 int refresh_last_frame;
323 int refresh_golden_frame;
324 int refresh_alt_ref_frame;
325 YV12_BUFFER_CONFIG last_frame_uf;
328 unsigned int tok_count[4][1 << 6];
331 unsigned int frames_since_key;
332 unsigned int key_frame_frequency;
333 unsigned int this_key_frame_forced;
334 unsigned int next_key_frame_forced;
335 #if CONFIG_MULTIPLE_ARF
336 // Position within a frame coding order (including any additional ARF frames).
337 unsigned int sequence_number;
338 // Next frame in naturally occurring order that has not yet been coded.
339 int next_frame_in_order;
342 // Ambient reconstruction err target for force key frames
345 unsigned int mode_check_freq[MAX_MODES];
346 unsigned int mode_test_hit_counts[MAX_MODES];
347 unsigned int mode_chosen_counts[MAX_MODES];
348 int64_t unused_mode_skip_mask;
350 int set_ref_frame_mask;
352 int rd_thresh_mult[MAX_MODES];
353 int rd_baseline_thresh[BLOCK_SIZE_TYPES][MAX_MODES];
354 int rd_threshes[BLOCK_SIZE_TYPES][MAX_MODES];
355 int rd_thresh_freq_fact[BLOCK_SIZE_TYPES][MAX_MODES];
357 int64_t rd_comp_pred_diff[NB_PREDICTION_TYPES];
358 int rd_prediction_type_threshes[4][NB_PREDICTION_TYPES];
359 unsigned int intra_inter_count[INTRA_INTER_CONTEXTS][2];
360 unsigned int comp_inter_count[COMP_INTER_CONTEXTS][2];
361 unsigned int single_ref_count[REF_CONTEXTS][2][2];
362 unsigned int comp_ref_count[REF_CONTEXTS][2];
364 // FIXME contextualize
366 int64_t rd_tx_select_diff[NB_TXFM_MODES];
367 int rd_tx_select_threshes[4][NB_TXFM_MODES];
372 CODING_CONTEXT coding_context;
374 // Rate targetting variables
375 int this_frame_target;
376 int projected_frame_size;
377 int last_q[2]; // Separate values for Intra/Inter
378 int last_boosted_qindex; // Last boosted GF/KF/ARF q
380 double rate_correction_factor;
381 double key_frame_rate_correction_factor;
382 double gf_rate_correction_factor;
384 int frames_till_gf_update_due; // Count down till next GF
386 int gf_overspend_bits; // Total bits overspent becasue of GF boost (cumulative)
388 int non_gf_bitrate_adjustment; // Used in the few frames following a GF to recover the extra bits spent in that GF
390 int kf_overspend_bits; // Extra bits spent on key frames that need to be recovered on inter frames
391 int kf_bitrate_adjustment; // Current number of bit s to try and recover on each inter frame.
393 int baseline_gf_interval;
394 int active_arnr_frames; // <= cpi->oxcf.arnr_max_frames
395 int active_arnr_strength; // <= cpi->oxcf.arnr_max_strength
397 int64_t key_frame_count;
398 int prior_key_frame_distance[KEY_FRAME_CONTEXT];
399 int per_frame_bandwidth; // Current section per frame bandwidth target
400 int av_per_frame_bandwidth; // Average frame size target for clip
401 int min_frame_bandwidth; // Minimum allocation that should be used for any frame
402 int inter_frame_target;
403 double output_frame_rate;
404 int64_t last_time_stamp_seen;
405 int64_t last_end_time_stamp_seen;
406 int64_t first_time_stamp_ever;
411 int avg_frame_qindex;
416 int zbin_mode_boost_enabled;
418 int64_t total_byte_count;
425 int rolling_target_bits;
426 int rolling_actual_bits;
428 int long_rolling_target_bits;
429 int long_rolling_actual_bits;
431 int64_t total_actual_bits;
432 int total_target_vs_actual; // debug stats
435 int active_worst_quality;
437 int active_best_quality;
439 int cq_target_quality;
441 int y_mode_count[4][VP9_INTRA_MODES];
442 int y_uv_mode_count[VP9_INTRA_MODES][VP9_INTRA_MODES];
443 unsigned int partition_count[NUM_PARTITION_CONTEXTS][PARTITION_TYPES];
445 nmv_context_counts NMVcount;
447 vp9_coeff_count coef_counts[TX_SIZE_MAX_SB][BLOCK_TYPES];
448 vp9_coeff_probs_model frame_coef_probs[TX_SIZE_MAX_SB][BLOCK_TYPES];
449 vp9_coeff_stats frame_branch_ct[TX_SIZE_MAX_SB][BLOCK_TYPES];
454 int kf_zeromotion_pct;
456 int64_t target_bandwidth;
457 struct vpx_codec_pkt_list *output_pkt_list;
460 // Experimental code for lagged and one pass
461 ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS];
462 int one_pass_frame_index;
464 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
465 int mbgraph_n_frames; // number of frames filled in the above
466 int static_mb_pct; // % forced skip mbs by segmentation
467 int seg0_progress, seg0_idx, seg0_cnt;
469 int decimation_factor;
470 int decimation_count;
472 // for real time encoding
473 int avg_encode_time; // microsecond
474 int avg_pick_mode_time; // microsecond
476 unsigned int cpu_freq; // Mhz
477 int compressor_speed;
485 vp9_prob last_skip_false_probs[3][MBSKIP_CONTEXTS];
486 int last_skip_probs_q[3];
491 int error_bins[1024];
493 unsigned int max_mv_magnitude;
495 // Data used for real time conferencing mode to help determine if it would be good to update the gf
498 int gf_update_recommended;
500 unsigned char *segmentation_map;
502 // segment threashold for encode breakout
503 int segment_encode_breakout[MAX_MB_SEGMENTS];
505 unsigned char *active_map;
506 unsigned int active_map_enabled;
508 fractional_mv_step_fp *find_fractional_mv_step;
509 vp9_full_search_fn_t full_search_sad;
510 vp9_refining_search_fn_t refining_search_sad;
511 vp9_diamond_search_fn_t diamond_search_sad;
512 vp9_variance_fn_ptr_t fn_ptr[BLOCK_MAX_SB_SEGMENTS];
513 uint64_t time_receive_data;
514 uint64_t time_compress_data;
515 uint64_t time_pick_lpf;
516 uint64_t time_encode_mb_row;
519 unsigned int section_intra_rating;
520 unsigned int next_iiratio;
521 unsigned int this_iiratio;
522 FIRSTPASS_STATS total_stats;
523 FIRSTPASS_STATS this_frame_stats;
524 FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
525 FIRSTPASS_STATS total_left_stats;
528 int64_t clip_bits_total;
530 double modified_error_total;
531 double modified_error_used;
532 double modified_error_left;
533 double kf_intra_err_min;
534 double gf_intra_err_min;
538 int static_scene_max_gf_interval;
540 // Remaining error from uncoded frames in a gf group. Two pass use only
541 int64_t gf_group_error_left;
543 // Projected total bits available for a key frame group of frames
544 int64_t kf_group_bits;
546 // Error score of frames still to be coded in kf group
547 int64_t kf_group_error_left;
549 // Projected Bits available for a group of frames including 1 GF or ARF
550 int64_t gf_group_bits;
551 // Bits for the golden frame or ARF - 2 pass only
556 double est_max_qcorrection_factor;
559 YV12_BUFFER_CONFIG alt_ref_buffer;
560 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
561 int fixed_divide[512];
563 #if CONFIG_INTERNAL_STATS
569 double total_sq_error;
574 double total_sq_error2;
576 double summed_quality;
577 double summed_weights;
578 double summedp_quality;
579 double summedp_weights;
580 unsigned int tot_recode_hits;
583 double total_ssimg_y;
584 double total_ssimg_u;
585 double total_ssimg_v;
586 double total_ssimg_all;
588 int b_calculate_ssimg;
590 int b_calculate_psnr;
592 // Per MB activity measurement
593 unsigned int activity_avg;
594 unsigned int *mb_activity_map;
595 int *mb_norm_activity_map;
596 int output_partition;
598 /* force next frame to intra when kf_auto says so */
599 int force_next_frame_intra;
603 int dummy_packing; /* flag to indicate if packing is dummy */
605 unsigned int switchable_interp_count[VP9_SWITCHABLE_FILTERS + 1]
606 [VP9_SWITCHABLE_FILTERS];
607 unsigned int best_switchable_interp_count[VP9_SWITCHABLE_FILTERS];
609 unsigned int txfm_stepdown_count[TX_SIZE_MAX_SB];
614 #if CONFIG_MULTIPLE_ARF
615 // ARF tracking variables.
616 int multi_arf_enabled;
617 unsigned int frame_coding_order_period;
618 unsigned int new_frame_coding_order_period;
619 int frame_coding_order[MAX_LAG_BUFFERS * 2];
620 int arf_buffer_idx[MAX_LAG_BUFFERS * 3 / 2];
621 int arf_weight[MAX_LAG_BUFFERS];
623 int this_frame_weight;
628 int64_t mv_ref_stats[INTER_MODE_CONTEXTS][VP9_INTER_MODES - 1][2];
632 static int get_ref_frame_idx(VP9_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
633 if (ref_frame == LAST_FRAME) {
634 return cpi->lst_fb_idx;
635 } else if (ref_frame == GOLDEN_FRAME) {
636 return cpi->gld_fb_idx;
638 return cpi->alt_fb_idx;
642 void vp9_encode_frame(VP9_COMP *cpi);
644 void vp9_pack_bitstream(VP9_COMP *cpi, unsigned char *dest,
645 unsigned long *size);
647 void vp9_activity_masking(VP9_COMP *cpi, MACROBLOCK *x);
649 void vp9_set_speed_features(VP9_COMP *cpi);
651 extern int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source,
652 YV12_BUFFER_CONFIG *dest);
654 extern void vp9_alloc_compressor_data(VP9_COMP *cpi);
656 #endif // VP9_ENCODER_VP9_ONYX_INT_H_