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 VPX_VP8_ENCODER_ONYX_INT_H_
12 #define VPX_VP8_ENCODER_ONYX_INT_H_
15 #include "vpx_config.h"
16 #include "vp8/common/onyx.h"
17 #include "treewriter.h"
19 #include "vp8/common/onyxc_int.h"
20 #include "vpx_dsp/variance.h"
22 #include "vp8/encoder/quantize.h"
23 #include "vp8/common/entropy.h"
24 #include "vp8/common/threading.h"
25 #include "vpx_ports/mem.h"
26 #include "vpx/internal/vpx_codec_internal.h"
29 #include "vp8/common/findnearmv.h"
30 #include "lookahead.h"
31 #if CONFIG_TEMPORAL_DENOISING
32 #include "vp8/encoder/denoising.h"
39 #define MIN_GF_INTERVAL 4
40 #define DEFAULT_GF_INTERVAL 7
42 #define KEY_FRAME_CONTEXT 5
44 #define MAX_LAG_BUFFERS (CONFIG_REALTIME_ONLY ? 1 : 25)
47 #define AF_THRESH2 100
48 #define ARF_DECAY_THRESH 12
50 #define MIN_THRESHMULT 32
51 #define MAX_THRESHMULT 512
53 #define GF_ZEROMV_ZBIN_BOOST 12
54 #define LF_ZEROMV_ZBIN_BOOST 6
55 #define MV_ZBIN_BOOST 4
56 #define ZBIN_OQ_MAX 192
58 #define VP8_TEMPORAL_ALT_REF !CONFIG_REALTIME_ONLY
60 /* vp8 uses 10,000,000 ticks/second as time stamp */
61 #define TICKS_PER_SEC 10000000
65 unsigned int frames_since_key;
66 unsigned int frames_since_golden;
68 int frames_till_gf_update_due;
69 int recent_ref_frame_usage[MAX_REF_FRAMES];
72 int mvcosts[2][MVvals + 1];
78 int inter_y_modes[10];
79 int inter_uv_modes[4];
80 int inter_b_modes[10];
83 vp8_prob ymode_prob[4], uv_mode_prob[3]; /* interframe intra mode probs */
84 vp8_prob kf_ymode_prob[4], kf_uv_mode_prob[3]; /* keyframe "" */
86 int ymode_count[5], uv_mode_count[4]; /* intra MB type cts this frame */
88 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
90 int this_frame_percent_intra;
91 int last_frame_percent_intra;
99 double ssim_weighted_pred_err;
102 double pcnt_second_ref;
110 double mv_in_out_count;
118 double frame_intra_error;
119 double frame_coded_error;
120 double frame_pcnt_inter;
121 double frame_pcnt_motion;
123 double frame_mvr_abs;
125 double frame_mvc_abs;
127 } ONEPASS_FRAMESTATS;
160 typedef enum { DIAMOND = 0, NSTEP = 1, HEX = 2 } SEARCH_METHODS;
164 SEARCH_METHODS search_method;
169 int iterative_sub_pixel;
170 int half_pixel_search;
171 int quarter_pixel_search;
172 int thresh_mult[MAX_MODES];
173 int max_step_search_steps;
175 int optimize_coefficients;
177 int use_fastquant_for_pick;
178 int no_skip_block4x4_search;
179 int improved_mv_pred;
185 int segment_counts[MAX_MB_SEGMENTS];
214 /* Layer configuration */
216 int target_bandwidth;
218 /* Layer specific coding parameters */
219 int64_t starting_buffer_level;
220 int64_t optimal_buffer_level;
221 int64_t maximum_buffer_size;
222 int64_t starting_buffer_level_in_ms;
223 int64_t optimal_buffer_level_in_ms;
224 int64_t maximum_buffer_size_in_ms;
226 int avg_frame_size_for_layer;
228 int64_t buffer_level;
229 int64_t bits_off_target;
231 int64_t total_actual_bits;
232 int total_target_vs_actual;
235 int active_worst_quality;
237 int active_best_quality;
242 int avg_frame_qindex;
244 double rate_correction_factor;
245 double key_frame_rate_correction_factor;
246 double gf_rate_correction_factor;
250 int inter_frame_target;
251 int64_t total_byte_count;
255 int frames_since_last_drop_overshoot;
259 int last_frame_percent_intra;
261 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
266 typedef struct VP8_COMP {
267 DECLARE_ALIGNED(16, short, Y1quant[QINDEX_RANGE][16]);
268 DECLARE_ALIGNED(16, short, Y1quant_shift[QINDEX_RANGE][16]);
269 DECLARE_ALIGNED(16, short, Y1zbin[QINDEX_RANGE][16]);
270 DECLARE_ALIGNED(16, short, Y1round[QINDEX_RANGE][16]);
272 DECLARE_ALIGNED(16, short, Y2quant[QINDEX_RANGE][16]);
273 DECLARE_ALIGNED(16, short, Y2quant_shift[QINDEX_RANGE][16]);
274 DECLARE_ALIGNED(16, short, Y2zbin[QINDEX_RANGE][16]);
275 DECLARE_ALIGNED(16, short, Y2round[QINDEX_RANGE][16]);
277 DECLARE_ALIGNED(16, short, UVquant[QINDEX_RANGE][16]);
278 DECLARE_ALIGNED(16, short, UVquant_shift[QINDEX_RANGE][16]);
279 DECLARE_ALIGNED(16, short, UVzbin[QINDEX_RANGE][16]);
280 DECLARE_ALIGNED(16, short, UVround[QINDEX_RANGE][16]);
282 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y1[QINDEX_RANGE][16]);
283 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y2[QINDEX_RANGE][16]);
284 DECLARE_ALIGNED(16, short, zrun_zbin_boost_uv[QINDEX_RANGE][16]);
285 DECLARE_ALIGNED(16, short, Y1quant_fast[QINDEX_RANGE][16]);
286 DECLARE_ALIGNED(16, short, Y2quant_fast[QINDEX_RANGE][16]);
287 DECLARE_ALIGNED(16, short, UVquant_fast[QINDEX_RANGE][16]);
291 vp8_writer bc[9]; /* one boolcoder for each partition */
295 struct lookahead_ctx *lookahead;
296 struct lookahead_entry *source;
297 struct lookahead_entry *alt_ref_source;
298 struct lookahead_entry *last_source;
300 YV12_BUFFER_CONFIG *Source;
301 YV12_BUFFER_CONFIG *un_scaled_source;
302 YV12_BUFFER_CONFIG scaled_source;
303 YV12_BUFFER_CONFIG *last_frame_unscaled_source;
305 unsigned int frames_till_alt_ref_frame;
306 /* frame in src_buffers has been identified to be encoded as an alt ref */
307 int source_alt_ref_pending;
308 /* an alt ref frame has been encoded and is usable */
309 int source_alt_ref_active;
310 /* source of frame to encode is an exact copy of an alt ref frame */
311 int is_src_frame_alt_ref;
313 /* golden frame same as last frame ( short circuit gold searches) */
315 /* Alt reference frame same as last ( short circuit altref search) */
317 /* don't do both alt and gold search ( just do gold). */
320 YV12_BUFFER_CONFIG pick_lf_lvl_frame;
323 unsigned int tok_count;
325 unsigned int frames_since_key;
326 unsigned int key_frame_frequency;
327 unsigned int this_key_frame_forced;
328 unsigned int next_key_frame_forced;
330 /* Ambient reconstruction err target for force key frames */
333 unsigned int mode_check_freq[MAX_MODES];
335 int rd_baseline_thresh[MAX_MODES];
340 CODING_CONTEXT coding_context;
342 /* Rate targeting variables */
343 int64_t last_prediction_error;
344 int64_t last_intra_error;
346 int this_frame_target;
347 int projected_frame_size;
348 int last_q[2]; /* Separate values for Intra/Inter */
350 double rate_correction_factor;
351 double key_frame_rate_correction_factor;
352 double gf_rate_correction_factor;
354 int frames_since_golden;
355 /* Count down till next GF */
356 int frames_till_gf_update_due;
358 /* GF interval chosen when we coded the last GF */
359 int current_gf_interval;
361 /* Total bits overspent becasue of GF boost (cumulative) */
362 int gf_overspend_bits;
364 /* Used in the few frames following a GF to recover the extra bits
367 int non_gf_bitrate_adjustment;
369 /* Extra bits spent on key frames that need to be recovered */
370 int kf_overspend_bits;
372 /* Current number of bit s to try and recover on each inter frame. */
373 int kf_bitrate_adjustment;
375 int baseline_gf_interval;
376 int active_arnr_frames;
378 int64_t key_frame_count;
379 int prior_key_frame_distance[KEY_FRAME_CONTEXT];
380 /* Current section per frame bandwidth target */
381 int per_frame_bandwidth;
382 /* Average frame size target for clip */
383 int av_per_frame_bandwidth;
384 /* Minimum allocation that should be used for any frame */
385 int min_frame_bandwidth;
386 int inter_frame_target;
387 double output_framerate;
388 int64_t last_time_stamp_seen;
389 int64_t last_end_time_stamp_seen;
390 int64_t first_time_stamp_ever;
395 int avg_frame_qindex;
397 int64_t total_byte_count;
402 double ref_framerate;
403 int64_t buffer_level;
404 int64_t bits_off_target;
406 int rolling_target_bits;
407 int rolling_actual_bits;
409 int long_rolling_target_bits;
410 int long_rolling_actual_bits;
412 int64_t total_actual_bits;
413 int total_target_vs_actual; /* debug stats */
416 int active_worst_quality;
418 int active_best_quality;
420 int cq_target_quality;
422 int drop_frames_allowed; /* Are we permitted to drop frames? */
423 int drop_frame; /* Drop this frame? */
424 #if defined(DROP_UNCODED_FRAMES)
425 int drop_frame_count;
428 vp8_prob frame_coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS]
430 char update_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES];
432 unsigned int frame_branch_ct[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS]
439 int target_bandwidth;
440 struct vpx_codec_pkt_list *output_pkt_list;
443 /* Experimental code for lagged and one pass */
444 ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS];
445 int one_pass_frame_index;
448 int decimation_factor;
449 int decimation_count;
451 /* for real time encoding */
452 int avg_encode_time; /* microsecond */
453 int avg_pick_mode_time; /* microsecond */
455 int compressor_speed;
458 int auto_adjust_gold_quantizer;
463 int prob_intra_coded;
467 int last_skip_false_probs[3];
468 int last_skip_probs_q[3];
469 int recent_ref_frame_usage[MAX_REF_FRAMES];
471 int this_frame_percent_intra;
472 int last_frame_percent_intra;
478 /* Count ZEROMV on all reference frames. */
482 unsigned char *skin_map;
484 unsigned char *segmentation_map;
485 signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
486 int segment_encode_breakout[MAX_MB_SEGMENTS];
488 unsigned char *active_map;
489 unsigned int active_map_enabled;
491 /* Video conferencing cyclic refresh mode flags. This is a mode
492 * designed to clean up the background over time in live encoding
493 * scenarious. It uses segmentation.
495 int cyclic_refresh_mode_enabled;
496 int cyclic_refresh_mode_max_mbs_perframe;
497 int cyclic_refresh_mode_index;
498 int cyclic_refresh_q;
499 signed char *cyclic_refresh_map;
500 // Count on how many (consecutive) times a macroblock uses ZER0MV_LAST.
501 unsigned char *consec_zero_last;
502 // Counter that is reset when a block is checked for a mode-bias against
504 unsigned char *consec_zero_last_mvbias;
506 // Frame counter for the temporal pattern. Counter is rest when the temporal
507 // layers are changed dynamically (run-time change).
508 unsigned int temporal_pattern_counter;
509 // Temporal layer id.
510 int temporal_layer_id;
512 // Measure of average squared difference between source and denoised signal.
513 int mse_source_denoised;
516 int frames_since_last_drop_overshoot;
517 int last_pred_err_mb;
519 // GF update for 1 pass cbr.
520 int gf_update_onepass_cbr;
521 int gf_interval_onepass_cbr;
522 int gf_noboost_onepass_cbr;
524 #if CONFIG_MULTITHREAD
525 /* multithread data */
526 vpx_atomic_int *mt_current_mb_col;
528 vpx_atomic_int b_multi_threaded;
529 int encoding_thread_count;
532 pthread_t *h_encoding_thread;
533 pthread_t h_filter_thread;
535 MB_ROW_COMP *mb_row_ei;
536 ENCODETHREAD_DATA *en_thread_data;
537 LPFTHREAD_DATA lpf_thread_data;
540 sem_t *h_event_start_encoding;
541 sem_t *h_event_end_encoding;
542 sem_t h_event_start_lpf;
543 sem_t h_event_end_lpf;
547 unsigned int partition_sz[MAX_PARTITIONS];
548 unsigned char *partition_d[MAX_PARTITIONS];
549 unsigned char *partition_d_end[MAX_PARTITIONS];
551 fractional_mv_step_fp *find_fractional_mv_step;
552 vp8_full_search_fn_t full_search_sad;
553 vp8_refining_search_fn_t refining_search_sad;
554 vp8_diamond_search_fn_t diamond_search_sad;
555 vp8_variance_fn_ptr_t fn_ptr[BLOCK_MAX_SEGMENTS];
556 uint64_t time_receive_data;
557 uint64_t time_compress_data;
558 uint64_t time_pick_lpf;
559 uint64_t time_encode_mb_row;
561 int base_skip_false_prob[128];
563 FRAME_CONTEXT lfc_n; /* last frame entropy */
564 FRAME_CONTEXT lfc_a; /* last alt ref entropy */
565 FRAME_CONTEXT lfc_g; /* last gold ref entropy */
568 unsigned int section_intra_rating;
569 double section_max_qfactor;
570 unsigned int next_iiratio;
571 unsigned int this_iiratio;
572 FIRSTPASS_STATS total_stats;
573 FIRSTPASS_STATS this_frame_stats;
574 FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
575 FIRSTPASS_STATS total_left_stats;
578 int64_t clip_bits_total;
580 double modified_error_total;
581 double modified_error_used;
582 double modified_error_left;
583 double kf_intra_err_min;
584 double gf_intra_err_min;
589 int static_scene_max_gf_interval;
591 /* Remaining error from uncoded frames in a gf group. */
592 int gf_group_error_left;
593 /* Projected total bits available for a key frame group of frames */
594 int64_t kf_group_bits;
595 /* Error score of frames still to be coded in kf group */
596 int64_t kf_group_error_left;
597 /* Projected Bits available for a group including 1 GF or ARF */
598 int64_t gf_group_bits;
599 /* Bits for the golden frame or ARF */
602 double est_max_qcorrection_factor;
605 #if VP8_TEMPORAL_ALT_REF
606 YV12_BUFFER_CONFIG alt_ref_buffer;
607 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
608 int fixed_divide[512];
611 #if CONFIG_INTERNAL_STATS
617 double total_sq_error;
622 double total_sq_error2;
624 double summed_quality;
625 double summed_weights;
626 unsigned int tot_recode_hits;
628 int b_calculate_ssimg;
630 int b_calculate_psnr;
632 /* Per MB activity measurement */
633 unsigned int activity_avg;
634 unsigned int *mb_activity_map;
636 /* Record of which MBs still refer to last golden frame either
637 * directly or through 0,0
639 unsigned char *gf_active_flags;
642 int output_partition;
644 /* Store last frame's MV info for next frame MV prediction */
646 int *lf_ref_frame_sign_bias;
649 /* force next frame to intra when kf_auto says so */
650 int force_next_frame_intra;
657 #if CONFIG_TEMPORAL_DENOISING
658 VP8_DENOISER denoiser;
661 /* Coding layer state variables */
662 unsigned int current_layer;
663 LAYER_CONTEXT layer_context[VPX_TS_MAX_LAYERS];
665 int64_t frames_in_layer[VPX_TS_MAX_LAYERS];
666 int64_t bytes_in_layer[VPX_TS_MAX_LAYERS];
667 double sum_psnr[VPX_TS_MAX_LAYERS];
668 double sum_psnr_p[VPX_TS_MAX_LAYERS];
669 double total_error2[VPX_TS_MAX_LAYERS];
670 double total_error2_p[VPX_TS_MAX_LAYERS];
671 double sum_ssim[VPX_TS_MAX_LAYERS];
672 double sum_weights[VPX_TS_MAX_LAYERS];
674 double total_ssimg_y_in_layer[VPX_TS_MAX_LAYERS];
675 double total_ssimg_u_in_layer[VPX_TS_MAX_LAYERS];
676 double total_ssimg_v_in_layer[VPX_TS_MAX_LAYERS];
677 double total_ssimg_all_in_layer[VPX_TS_MAX_LAYERS];
679 #if CONFIG_MULTI_RES_ENCODING
680 /* Number of MBs per row at lower-resolution level */
681 int mr_low_res_mb_cols;
682 /* Indicate if lower-res mv info is available */
683 unsigned char mr_low_res_mv_avail;
685 /* The frame number of each reference frames */
686 unsigned int current_ref_frames[MAX_REF_FRAMES];
687 // Closest reference frame to current frame.
688 MV_REFERENCE_FRAME closest_reference_frame;
690 struct rd_costs_struct {
691 int mvcosts[2][MVvals + 1];
692 int mvsadcosts[2][MVfpvals + 1];
693 int mbmode_cost[2][MB_MODE_COUNT];
694 int intra_uv_mode_cost[2][MB_MODE_COUNT];
695 int bmode_costs[10][10][10];
696 int inter_bmode_costs[B_MODE_COUNT];
697 int token_costs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS]
698 [MAX_ENTROPY_TOKENS];
701 // Use the static threshold from ROI settings.
702 int use_roi_static_threshold;
704 int ext_refresh_frame_flags_pending;
706 // Always update correction factor used for rate control after each frame for
707 // realtime encoding.
708 int rt_always_update_correction_factor;
711 void vp8_initialize_enc(void);
713 void vp8_alloc_compressor_data(VP8_COMP *cpi);
714 int vp8_reverse_trans(int x);
715 void vp8_init_temporal_layer_context(VP8_COMP *cpi, VP8_CONFIG *oxcf,
717 double prev_layer_framerate);
718 void vp8_update_layer_contexts(VP8_COMP *cpi);
719 void vp8_save_layer_context(VP8_COMP *cpi);
720 void vp8_restore_layer_context(VP8_COMP *cpi, const int layer);
721 void vp8_new_framerate(VP8_COMP *cpi, double framerate);
722 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm);
724 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest,
725 unsigned char *dest_end, size_t *size);
727 void vp8_tokenize_mb(VP8_COMP *, MACROBLOCK *, TOKENEXTRA **);
729 void vp8_set_speed_features(VP8_COMP *cpi);
732 #define CHECK_MEM_ERROR(lval, expr) \
736 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, \
737 "Failed to allocate " #lval " at %s:%d", __FILE__, \
741 #define CHECK_MEM_ERROR(lval, expr) \
745 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, \
746 "Failed to allocate " #lval); \
753 #endif // VPX_VP8_ENCODER_ONYX_INT_H_