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 __INC_VP8_INT_H
13 #define __INC_VP8_INT_H
16 #include "vpx_config.h"
17 #include "vp8/common/onyx.h"
18 #include "treewriter.h"
20 #include "vp8/common/onyxc_int.h"
25 #include "vp8/common/entropy.h"
26 #include "vp8/common/threading.h"
27 #include "vpx_ports/mem.h"
28 #include "vpx/internal/vpx_codec_internal.h"
30 #include "temporal_filter.h"
31 #include "vp8/common/findnearmv.h"
32 #include "lookahead.h"
34 //#define SPEEDSTATS 1
35 #define MIN_GF_INTERVAL 4
36 #define DEFAULT_GF_INTERVAL 7
38 #define KEY_FRAME_CONTEXT 5
40 #define MAX_LAG_BUFFERS (CONFIG_REALTIME_ONLY? 1 : 25)
43 #define AF_THRESH2 100
44 #define ARF_DECAY_THRESH 12
47 #define MIN_THRESHMULT 32
48 #define MAX_THRESHMULT 512
50 #define GF_ZEROMV_ZBIN_BOOST 12
51 #define LF_ZEROMV_ZBIN_BOOST 6
52 #define MV_ZBIN_BOOST 4
53 #define ZBIN_OQ_MAX 192
55 #if !(CONFIG_REALTIME_ONLY)
56 #define VP8_TEMPORAL_ALT_REF 1
59 #define MAX_PERIODICITY 16
61 #define MAX(x,y) (((x)>(y))?(x):(y))
62 #define MIN(x,y) (((x)<(y))?(x):(y))
67 unsigned int frames_since_key;
68 unsigned int frames_since_golden;
70 int frames_till_gf_update_due;
71 int recent_ref_frame_usage[MAX_REF_FRAMES];
74 int mvcosts[2][MVvals+1];
81 int inter_y_modes[10];
82 int inter_uv_modes[4];
83 int inter_b_modes[10];
86 vp8_prob ymode_prob[4], uv_mode_prob[3]; /* interframe intra mode probs */
87 vp8_prob kf_ymode_prob[4], kf_uv_mode_prob[3]; /* keyframe "" */
89 int ymode_count[5], uv_mode_count[4]; /* intra MB type cts this frame */
91 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
93 int this_frame_percent_intra;
94 int last_frame_percent_intra;
104 double ssim_weighted_pred_err;
107 double pcnt_second_ref;
115 double mv_in_out_count;
125 double frame_intra_error;
126 double frame_coded_error;
127 double frame_pcnt_inter;
128 double frame_pcnt_motion;
130 double frame_mvr_abs;
132 double frame_mvc_abs;
134 } ONEPASS_FRAMESTATS;
180 SEARCH_METHODS search_method;
185 int iterative_sub_pixel;
186 int half_pixel_search;
187 int quarter_pixel_search;
188 int thresh_mult[MAX_MODES];
189 int max_step_search_steps;
191 int optimize_coefficients;
193 int use_fastquant_for_pick;
194 int no_skip_block4x4_search;
195 int improved_mv_pred;
202 int segment_counts[MAX_MB_SEGMENTS];
225 typedef struct VP8_ENCODER_RTCD
227 VP8_COMMON_RTCD *common;
228 vp8_variance_rtcd_vtable_t variance;
229 vp8_fdct_rtcd_vtable_t fdct;
230 vp8_encodemb_rtcd_vtable_t encodemb;
231 vp8_quantize_rtcd_vtable_t quantize;
232 vp8_search_rtcd_vtable_t search;
233 vp8_temporal_rtcd_vtable_t temporal;
248 // Layer configuration
250 int target_bandwidth;
252 // Layer specific coding parameters
253 int starting_buffer_level;
254 int optimal_buffer_level;
255 int maximum_buffer_size;
257 int avg_frame_size_for_layer;
262 int64_t total_actual_bits;
263 int total_target_vs_actual;
266 int active_worst_quality;
268 int active_best_quality;
273 int avg_frame_qindex;
275 double rate_correction_factor;
276 double key_frame_rate_correction_factor;
277 double gf_rate_correction_factor;
281 int inter_frame_target;
282 int64_t total_byte_count;
286 int last_frame_percent_intra;
288 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
292 typedef struct VP8_COMP
295 DECLARE_ALIGNED(16, short, Y1quant[QINDEX_RANGE][16]);
296 DECLARE_ALIGNED(16, unsigned char, Y1quant_shift[QINDEX_RANGE][16]);
297 DECLARE_ALIGNED(16, short, Y1zbin[QINDEX_RANGE][16]);
298 DECLARE_ALIGNED(16, short, Y1round[QINDEX_RANGE][16]);
300 DECLARE_ALIGNED(16, short, Y2quant[QINDEX_RANGE][16]);
301 DECLARE_ALIGNED(16, unsigned char, Y2quant_shift[QINDEX_RANGE][16]);
302 DECLARE_ALIGNED(16, short, Y2zbin[QINDEX_RANGE][16]);
303 DECLARE_ALIGNED(16, short, Y2round[QINDEX_RANGE][16]);
305 DECLARE_ALIGNED(16, short, UVquant[QINDEX_RANGE][16]);
306 DECLARE_ALIGNED(16, unsigned char, UVquant_shift[QINDEX_RANGE][16]);
307 DECLARE_ALIGNED(16, short, UVzbin[QINDEX_RANGE][16]);
308 DECLARE_ALIGNED(16, short, UVround[QINDEX_RANGE][16]);
310 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y1[QINDEX_RANGE][16]);
311 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y2[QINDEX_RANGE][16]);
312 DECLARE_ALIGNED(16, short, zrun_zbin_boost_uv[QINDEX_RANGE][16]);
313 DECLARE_ALIGNED(16, short, Y1quant_fast[QINDEX_RANGE][16]);
314 DECLARE_ALIGNED(16, short, Y2quant_fast[QINDEX_RANGE][16]);
315 DECLARE_ALIGNED(16, short, UVquant_fast[QINDEX_RANGE][16]);
320 vp8_writer bc[9]; // one boolcoder for each partition
324 struct lookahead_ctx *lookahead;
325 struct lookahead_entry *source;
326 struct lookahead_entry *alt_ref_source;
328 YV12_BUFFER_CONFIG *Source;
329 YV12_BUFFER_CONFIG *un_scaled_source;
330 YV12_BUFFER_CONFIG scaled_source;
332 int source_alt_ref_pending; // frame in src_buffers has been identified to be encoded as an alt ref
333 int source_alt_ref_active; // an alt ref frame has been encoded and is usable
335 int is_src_frame_alt_ref; // source of frame to encode is an exact copy of an alt ref frame
337 int gold_is_last; // golden frame same as last frame ( short circuit gold searches)
338 int alt_is_last; // Alt reference frame same as last ( short circuit altref search)
339 int gold_is_alt; // don't do both alt and gold search ( just do gold).
341 //int refresh_alt_ref_frame;
342 YV12_BUFFER_CONFIG pick_lf_lvl_frame;
345 unsigned int tok_count;
348 unsigned int frames_since_key;
349 unsigned int key_frame_frequency;
350 unsigned int this_key_frame_forced;
351 unsigned int next_key_frame_forced;
353 // Ambient reconstruction err target for force key frames
356 unsigned int mode_check_freq[MAX_MODES];
357 unsigned int mode_test_hit_counts[MAX_MODES];
358 unsigned int mode_chosen_counts[MAX_MODES];
359 unsigned int mbs_tested_so_far;
361 int rd_thresh_mult[MAX_MODES];
362 int rd_baseline_thresh[MAX_MODES];
363 int rd_threshes[MAX_MODES];
368 CODING_CONTEXT coding_context;
370 // Rate targetting variables
371 int64_t prediction_error;
372 int64_t last_prediction_error;
374 int64_t last_intra_error;
376 int this_frame_target;
377 int projected_frame_size;
378 int last_q[2]; // Separate values for Intra/Inter
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
385 int current_gf_interval; // GF interval chosen when we coded the last GF
387 int gf_overspend_bits; // Total bits overspent becasue of GF boost (cumulative)
389 int non_gf_bitrate_adjustment; // Used in the few frames following a GF to recover the extra bits spent in that GF
391 int kf_overspend_bits; // Extra bits spent on key frames that need to be recovered on inter frames
392 int kf_bitrate_adjustment; // Current number of bit s to try and recover on each inter frame.
394 int baseline_gf_interval;
395 int active_arnr_frames; // <= cpi->oxcf.arnr_max_frames
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;
415 int zbin_mode_boost_enabled;
416 int last_zbin_over_quant;
417 int last_zbin_mode_boost;
419 int64_t total_byte_count;
424 int64_t buffer_level;
427 int rolling_target_bits;
428 int rolling_actual_bits;
430 int long_rolling_target_bits;
431 int long_rolling_actual_bits;
433 int64_t total_actual_bits;
434 int total_target_vs_actual; // debug stats
437 int active_worst_quality;
439 int active_best_quality;
441 int cq_target_quality;
443 int drop_frames_allowed; // Are we permitted to drop frames?
444 int drop_frame; // Drop this frame?
445 int drop_count; // How many frames have we dropped?
446 int max_drop_count; // How many frames should we drop?
447 int max_consec_dropped_frames; // Limit number of consecutive frames that can be dropped.
450 int ymode_count [VP8_YMODES]; /* intra MB type cts this frame */
451 int uv_mode_count[VP8_UV_MODES]; /* intra MB type cts this frame */
453 unsigned int MVcount [2] [MVvals]; /* (row,col) MV cts this frame */
455 unsigned int coef_counts [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS]; /* for this frame */
456 //DECLARE_ALIGNED(16, int, coef_counts_backup [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS]); //not used any more
457 //save vp8_tree_probs_from_distribution result for each frame to avoid repeat calculation
458 vp8_prob frame_coef_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
459 unsigned int frame_branch_ct [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES][2];
465 int target_bandwidth;
466 struct vpx_codec_pkt_list *output_pkt_list;
469 // Experimental code for lagged and one pass
470 ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS];
471 int one_pass_frame_index;
474 int decimation_factor;
475 int decimation_count;
477 // for real time encoding
478 int avg_encode_time; //microsecond
479 int avg_pick_mode_time; //microsecond
481 unsigned int cpu_freq; //Mhz
482 int compressor_speed;
486 int auto_adjust_gold_quantizer;
495 int prob_intra_coded;
499 int last_skip_false_probs[3];
500 int last_skip_probs_q[3];
501 int recent_ref_frame_usage[MAX_REF_FRAMES];
503 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
504 int this_frame_percent_intra;
505 int last_frame_percent_intra;
510 int error_bins[1024];
512 // Data used for real time conferencing mode to help determine if it would be good to update the gf
515 int gf_update_recommended;
517 int skip_false_count;
519 unsigned char *segmentation_map;
520 signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; // Segment data (can be deltas or absolute values)
521 int segment_encode_breakout[MAX_MB_SEGMENTS]; // segment threashold for encode breakout
523 unsigned char *active_map;
524 unsigned int active_map_enabled;
525 // Video conferencing cyclic refresh mode flags etc
526 // This is a mode designed to clean up the background over time in live encoding scenarious. It uses segmentation
527 int cyclic_refresh_mode_enabled;
528 int cyclic_refresh_mode_max_mbs_perframe;
529 int cyclic_refresh_mode_index;
530 int cyclic_refresh_q;
531 signed char *cyclic_refresh_map;
533 #if CONFIG_MULTITHREAD
535 int * mt_current_mb_col;
537 int b_multi_threaded;
538 int encoding_thread_count;
540 pthread_t *h_encoding_thread;
541 pthread_t h_filter_thread;
543 MB_ROW_COMP *mb_row_ei;
544 ENCODETHREAD_DATA *en_thread_data;
545 LPFTHREAD_DATA lpf_thread_data;
548 sem_t *h_event_start_encoding;
549 sem_t h_event_end_encoding;
550 sem_t h_event_start_lpf;
551 sem_t h_event_end_lpf;
555 unsigned int partition_sz[MAX_PARTITIONS];
556 // end of multithread data
559 fractional_mv_step_fp *find_fractional_mv_step;
560 vp8_full_search_fn_t full_search_sad;
561 vp8_refining_search_fn_t refining_search_sad;
562 vp8_diamond_search_fn_t diamond_search_sad;
563 vp8_variance_fn_ptr_t fn_ptr[BLOCK_MAX_SEGMENTS];
564 unsigned int time_receive_data;
565 unsigned int time_compress_data;
566 unsigned int time_pick_lpf;
567 unsigned int time_encode_mb_row;
569 int base_skip_false_prob[128];
571 FRAME_CONTEXT lfc_n; /* last frame entropy */
572 FRAME_CONTEXT lfc_a; /* last alt ref entropy */
573 FRAME_CONTEXT lfc_g; /* last gold ref entropy */
578 unsigned int section_intra_rating;
579 double section_max_qfactor;
580 unsigned int next_iiratio;
581 unsigned int this_iiratio;
582 FIRSTPASS_STATS total_stats;
583 FIRSTPASS_STATS this_frame_stats;
584 FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
585 FIRSTPASS_STATS total_left_stats;
588 int64_t clip_bits_total;
590 double modified_error_total;
591 double modified_error_used;
592 double modified_error_left;
593 double kf_intra_err_min;
594 double gf_intra_err_min;
599 int static_scene_max_gf_interval;
601 int gf_group_error_left; // Remaining error from uncoded frames in a gf group. Two pass use only
603 // Projected total bits available for a key frame group of frames
604 int64_t kf_group_bits;
606 // Error score of frames still to be coded in kf group
607 int64_t kf_group_error_left;
609 int gf_group_bits; // Projected Bits available for a group of frames including 1 GF or ARF
610 int gf_bits; // Bits for the golden frame or ARF - 2 pass only
612 double est_max_qcorrection_factor;
615 #if CONFIG_RUNTIME_CPU_DETECT
616 VP8_ENCODER_RTCD rtcd;
618 #if VP8_TEMPORAL_ALT_REF
619 YV12_BUFFER_CONFIG alt_ref_buffer;
620 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
621 int fixed_divide[512];
624 #if CONFIG_INTERNAL_STATS
630 double total_sq_error;
635 double total_sq_error2;
637 double summed_quality;
638 double summed_weights;
639 unsigned int tot_recode_hits;
642 double total_ssimg_y;
643 double total_ssimg_u;
644 double total_ssimg_v;
645 double total_ssimg_all;
647 int b_calculate_ssimg;
649 int b_calculate_psnr;
651 // Per MB activity measurement
652 unsigned int activity_avg;
653 unsigned int * mb_activity_map;
654 int * mb_norm_activity_map;
656 // Record of which MBs still refer to last golden frame either
657 // directly or through 0,0
658 unsigned char *gf_active_flags;
661 int output_partition;
663 //Store last frame's MV info for next frame MV prediction
665 int *lf_ref_frame_sign_bias;
668 int force_next_frame_intra; /* force next frame to intra when kf_auto says so */
672 // Coding layer state variables
673 unsigned int current_layer;
674 LAYER_CONTEXT layer_context[MAX_LAYERS];
676 int64_t frames_in_layer[MAX_LAYERS];
677 int64_t bytes_in_layer[MAX_LAYERS];
678 double sum_psnr[MAX_LAYERS];
679 double sum_psnr_p[MAX_LAYERS];
680 double total_error2[MAX_LAYERS];
681 double total_error2_p[MAX_LAYERS];
682 double sum_ssim[MAX_LAYERS];
683 double sum_weights[MAX_LAYERS];
685 double total_ssimg_y_in_layer[MAX_LAYERS];
686 double total_ssimg_u_in_layer[MAX_LAYERS];
687 double total_ssimg_v_in_layer[MAX_LAYERS];
688 double total_ssimg_all_in_layer[MAX_LAYERS];
690 #if CONFIG_MULTI_RES_ENCODING
691 /* Number of MBs per row at lower-resolution level */
692 int mr_low_res_mb_cols;
697 void control_data_rate(VP8_COMP *cpi);
699 void vp8_encode_frame(VP8_COMP *cpi);
701 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned char *dest_end, unsigned long *size);
703 void vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x);
705 int rd_cost_intra_mb(MACROBLOCKD *x);
707 void vp8_tokenize_mb(VP8_COMP *, MACROBLOCKD *, TOKENEXTRA **);
709 void vp8_set_speed_features(VP8_COMP *cpi);
712 #define CHECK_MEM_ERROR(lval,expr) do {\
715 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
716 "Failed to allocate "#lval" at %s:%d", \
720 #define CHECK_MEM_ERROR(lval,expr) do {\
723 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
724 "Failed to allocate "#lval);\