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_ports/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"
33 //#define SPEEDSTATS 1
34 #define MIN_GF_INTERVAL 4
35 #define DEFAULT_GF_INTERVAL 7
37 #define KEY_FRAME_CONTEXT 5
39 #define MAX_LAG_BUFFERS (CONFIG_REALTIME_ONLY? 1 : 25)
42 #define AF_THRESH2 100
43 #define ARF_DECAY_THRESH 12
46 #define MIN_THRESHMULT 32
47 #define MAX_THRESHMULT 512
49 #define GF_ZEROMV_ZBIN_BOOST 24
50 #define LF_ZEROMV_ZBIN_BOOST 12
51 #define MV_ZBIN_BOOST 4
52 #define ZBIN_OQ_MAX 192
54 #if !(CONFIG_REALTIME_ONLY)
55 #define VP8_TEMPORAL_ALT_REF 1
61 unsigned int frames_since_key
;
62 unsigned int frames_since_golden
;
64 int frames_till_gf_update_due
;
65 int recent_ref_frame_usage
[MAX_REF_FRAMES
];
68 int mvcosts
[2][MVvals
+1];
75 int inter_y_modes
[10];
76 int inter_uv_modes
[4];
77 int inter_b_modes
[10];
80 vp8_prob ymode_prob
[4], uv_mode_prob
[3]; /* interframe intra mode probs */
81 vp8_prob kf_ymode_prob
[4], kf_uv_mode_prob
[3]; /* keyframe "" */
83 int ymode_count
[5], uv_mode_count
[4]; /* intra MB type cts this frame */
85 int count_mb_ref_frame_usage
[MAX_REF_FRAMES
];
87 int this_frame_percent_intra
;
88 int last_frame_percent_intra
;
98 double ssim_weighted_pred_err
;
101 double pcnt_second_ref
;
109 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
;
173 SEARCH_METHODS search_method
;
178 int iterative_sub_pixel
;
179 int half_pixel_search
;
180 int quarter_pixel_search
;
181 int thresh_mult
[MAX_MODES
];
185 int max_step_search_steps
;
187 int optimize_coefficients
;
189 int use_fastquant_for_pick
;
190 int no_skip_block4x4_search
;
191 int improved_mv_pred
;
198 int segment_counts
[MAX_MB_SEGMENTS
];
222 INT64 source_time_stamp
;
223 INT64 source_end_time_stamp
;
225 DECLARE_ALIGNED(16, YV12_BUFFER_CONFIG
, source_buffer
);
226 unsigned int source_frame_flags
;
229 typedef struct VP8_ENCODER_RTCD
231 VP8_COMMON_RTCD
*common
;
232 vp8_variance_rtcd_vtable_t variance
;
233 vp8_fdct_rtcd_vtable_t fdct
;
234 vp8_encodemb_rtcd_vtable_t encodemb
;
235 vp8_quantize_rtcd_vtable_t quantize
;
236 vp8_search_rtcd_vtable_t search
;
237 vp8_temporal_rtcd_vtable_t temporal
;
253 DECLARE_ALIGNED(16, short, Y1quant
[QINDEX_RANGE
][16]);
254 DECLARE_ALIGNED(16, short, Y1quant_shift
[QINDEX_RANGE
][16]);
255 DECLARE_ALIGNED(16, short, Y1zbin
[QINDEX_RANGE
][16]);
256 DECLARE_ALIGNED(16, short, Y1round
[QINDEX_RANGE
][16]);
258 DECLARE_ALIGNED(16, short, Y2quant
[QINDEX_RANGE
][16]);
259 DECLARE_ALIGNED(16, short, Y2quant_shift
[QINDEX_RANGE
][16]);
260 DECLARE_ALIGNED(16, short, Y2zbin
[QINDEX_RANGE
][16]);
261 DECLARE_ALIGNED(16, short, Y2round
[QINDEX_RANGE
][16]);
263 DECLARE_ALIGNED(16, short, UVquant
[QINDEX_RANGE
][16]);
264 DECLARE_ALIGNED(16, short, UVquant_shift
[QINDEX_RANGE
][16]);
265 DECLARE_ALIGNED(16, short, UVzbin
[QINDEX_RANGE
][16]);
266 DECLARE_ALIGNED(16, short, UVround
[QINDEX_RANGE
][16]);
268 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y1
[QINDEX_RANGE
][16]);
269 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y2
[QINDEX_RANGE
][16]);
270 DECLARE_ALIGNED(16, short, zrun_zbin_boost_uv
[QINDEX_RANGE
][16]);
271 DECLARE_ALIGNED(16, short, Y1quant_fast
[QINDEX_RANGE
][16]);
272 DECLARE_ALIGNED(16, short, Y2quant_fast
[QINDEX_RANGE
][16]);
273 DECLARE_ALIGNED(16, short, UVquant_fast
[QINDEX_RANGE
][16]);
283 YV12_BUFFER_CONFIG
*Source
;
284 YV12_BUFFER_CONFIG
*un_scaled_source
;
285 INT64 source_time_stamp
;
286 INT64 source_end_time_stamp
;
287 unsigned int source_frame_flags
;
288 YV12_BUFFER_CONFIG scaled_source
;
290 int source_buffer_count
; // number of src_buffers in use for lagged encoding
291 int source_encode_index
; // index of buffer in src_buffer to encode
292 int source_alt_ref_pending
; // frame in src_buffers has been identified to be encoded as an alt ref
293 int source_alt_ref_active
; // an alt ref frame has been encoded and is usable
295 int last_alt_ref_sei
; // index into src_buffers of frame used as alt reference
296 int is_src_frame_alt_ref
; // source of frame to encode is an exact copy of an alt ref frame
297 int is_next_src_alt_ref
; // source of next frame to encode is an exact copy of an alt ref frame
299 int gold_is_last
; // golden frame same as last frame ( short circuit gold searches)
300 int alt_is_last
; // Alt reference frame same as last ( short circuit altref search)
301 int gold_is_alt
; // don't do both alt and gold search ( just do gold).
303 //int refresh_alt_ref_frame;
304 SOURCE_SAMPLE src_buffer
[MAX_LAG_BUFFERS
];
306 YV12_BUFFER_CONFIG last_frame_uf
;
309 unsigned int tok_count
;
312 unsigned int frames_since_key
;
313 unsigned int key_frame_frequency
;
314 unsigned int this_key_frame_forced
;
315 unsigned int next_key_frame_forced
;
317 // Ambient reconstruction err target for force key frames
320 unsigned int mode_check_freq
[MAX_MODES
];
321 unsigned int mode_test_hit_counts
[MAX_MODES
];
322 unsigned int mode_chosen_counts
[MAX_MODES
];
323 unsigned int mbs_tested_so_far
;
325 unsigned int check_freq
[2];
326 unsigned int do_full
[2];
328 int rd_thresh_mult
[MAX_MODES
];
329 int rd_baseline_thresh
[MAX_MODES
];
330 int rd_threshes
[MAX_MODES
];
332 int mvcostmultiplier
;
333 int subseqblockweight
;
335 unsigned int activity_avg
;
340 CODING_CONTEXT coding_context
;
342 // Rate targetting variables
343 long long prediction_error
;
344 long long last_prediction_error
;
345 long long intra_error
;
346 long long last_intra_error
;
349 // Experimental RD code
350 long long frame_distortion
;
351 long long last_frame_distortion
;
354 int last_mb_distortion
;
356 int frames_since_auto_filter
;
358 int this_frame_target
;
359 int projected_frame_size
;
360 int last_q
[2]; // Separate values for Intra/Inter
362 double rate_correction_factor
;
363 double key_frame_rate_correction_factor
;
364 double gf_rate_correction_factor
;
365 double est_max_qcorrection_factor
;
367 int frames_till_gf_update_due
; // Count down till next GF
368 int current_gf_interval
; // GF interval chosen when we coded the last GF
370 int gf_overspend_bits
; // Total bits overspent becasue of GF boost (cumulative)
372 int gf_group_bits
; // Projected Bits available for a group of frames including 1 GF or ARF
373 int gf_bits
; // Bits for the golden frame or ARF - 2 pass only
374 int mid_gf_extra_bits
; // A few extra bits for the frame half way between two gfs.
376 // Projected total bits available for a key frame group of frames
377 long long kf_group_bits
;
379 // Error score of frames still to be coded in kf group
380 long long kf_group_error_left
;
382 // Bits for the key frame in a key frame group - 2 pass only
385 int non_gf_bitrate_adjustment
; // Used in the few frames following a GF to recover the extra bits spent in that GF
386 int initial_gf_use
; // percentage use of gf 2 frames after gf
388 int gf_group_error_left
; // Remaining error from uncoded frames in a gf group. Two pass use only
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 static_scene_max_gf_interval
;
394 int baseline_gf_interval
;
396 int active_arnr_frames
; // <= cpi->oxcf.arnr_max_frames
398 INT64 key_frame_count
;
399 INT64 tot_key_frame_bits
;
400 int prior_key_frame_size
[KEY_FRAME_CONTEXT
];
401 int prior_key_frame_distance
[KEY_FRAME_CONTEXT
];
402 int per_frame_bandwidth
; // Current section per frame bandwidth target
403 int av_per_frame_bandwidth
; // Average frame size target for clip
404 int min_frame_bandwidth
; // Minimum allocation that should be used for any frame
405 int last_key_frame_size
;
406 int intra_frame_target
;
407 int inter_frame_target
;
408 double output_frame_rate
;
409 long long last_time_stamp_seen
;
410 long long last_end_time_stamp_seen
;
411 long long first_time_stamp_ever
;
416 int avg_frame_qindex
;
420 int zbin_mode_boost_enabled
;
422 INT64 total_byte_count
;
429 int rolling_target_bits
;
430 int rolling_actual_bits
;
432 int long_rolling_target_bits
;
433 int long_rolling_actual_bits
;
435 long long total_actual_bits
;
436 int total_target_vs_actual
; // debug stats
439 int active_worst_quality
;
441 int active_best_quality
;
443 int cq_target_quality
;
447 int drop_frames_allowed
; // Are we permitted to drop frames?
448 int drop_frame
; // Drop this frame?
449 int drop_count
; // How many frames have we dropped?
450 int max_drop_count
; // How many frames should we drop?
451 int max_consec_dropped_frames
; // Limit number of consecutive frames that can be dropped.
454 int ymode_count
[VP8_YMODES
]; /* intra MB type cts this frame */
455 int uv_mode_count
[VP8_UV_MODES
]; /* intra MB type cts this frame */
457 unsigned int MVcount
[2] [MVvals
]; /* (row,col) MV cts this frame */
459 unsigned int coef_counts
[BLOCK_TYPES
] [COEF_BANDS
] [PREV_COEF_CONTEXTS
] [vp8_coef_tokens
]; /* for this frame */
460 //DECLARE_ALIGNED(16, int, coef_counts_backup [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens]); //not used any more
461 //save vp8_tree_probs_from_distribution result for each frame to avoid repeat calculation
462 vp8_prob frame_coef_probs
[BLOCK_TYPES
] [COEF_BANDS
] [PREV_COEF_CONTEXTS
] [vp8_coef_tokens
-1];
463 unsigned int frame_branch_ct
[BLOCK_TYPES
] [COEF_BANDS
] [PREV_COEF_CONTEXTS
] [vp8_coef_tokens
-1][2];
465 /* Second compressed data partition contains coefficient data. */
467 unsigned char *output_partition2
;
468 size_t output_partition2size
;
474 double total_error_left
;
475 double total_intra_error_left
;
476 double total_coded_error_left
;
477 double start_tot_err_left
;
479 double kf_intra_err_min
;
480 double gf_intra_err_min
;
482 double modified_error_total
;
483 double modified_error_used
;
484 double modified_error_left
;
490 int target_bandwidth
;
492 long long clip_bits_total
;
493 FIRSTPASS_STATS
*total_stats
;
494 FIRSTPASS_STATS
*this_frame_stats
;
495 FIRSTPASS_STATS
*stats_in
, *stats_in_end
;
496 struct vpx_codec_pkt_list
*output_pkt_list
;
500 // Experimental code for lagged and one pass
501 ONEPASS_FRAMESTATS one_pass_frame_stats
[MAX_LAG_BUFFERS
];
502 int one_pass_frame_index
;
505 int decimation_factor
;
506 int decimation_count
;
508 // for real time encoding
509 int avg_encode_time
; //microsecond
510 int avg_pick_mode_time
; //microsecond
512 unsigned int cpu_freq
; //Mhz
513 int compressor_speed
;
517 int auto_adjust_gold_quantizer
;
520 int auto_adjust_key_quantizer
;
531 int prob_intra_coded
;
535 int last_skip_false_probs
[3];
536 int last_skip_probs_q
[3];
537 int recent_ref_frame_usage
[MAX_REF_FRAMES
];
539 int count_mb_ref_frame_usage
[MAX_REF_FRAMES
];
540 int this_frame_percent_intra
;
541 int last_frame_percent_intra
;
543 int last_key_frame_q
;
549 int error_bins
[1024];
556 unsigned int next_iiratio
;
557 unsigned int this_iiratio
;
558 int this_frame_modified_error
;
560 double norm_intra_err_per_mb
;
561 double norm_inter_err_per_mb
;
562 double norm_iidiff_per_mb
;
564 int last_best_mode_index
; // Record of mode index chosen for previous macro block.
565 int last_auto_filt_val
;
566 int last_auto_filt_q
;
568 // Data used for real time conferencing mode to help determine if it would be good to update the gf
571 int gf_update_recommended
;
573 int skip_false_count
;
577 int ready_for_new_frame
;
579 unsigned char *segmentation_map
;
580 signed char segment_feature_data
[MB_LVL_MAX
][MAX_MB_SEGMENTS
]; // Segment data (can be deltas or absolute values)
581 int segment_encode_breakout
[MAX_MB_SEGMENTS
]; // segment threashold for encode breakout
583 unsigned char *active_map
;
584 unsigned int active_map_enabled
;
585 // Video conferencing cyclic refresh mode flags etc
586 // This is a mode designed to clean up the background over time in live encoding scenarious. It uses segmentation
587 int cyclic_refresh_mode_enabled
;
588 int cyclic_refresh_mode_max_mbs_perframe
;
589 int cyclic_refresh_mode_index
;
590 int cyclic_refresh_q
;
591 signed char *cyclic_refresh_map
;
593 #if CONFIG_MULTITHREAD
595 int * mt_current_mb_col
;
597 int processor_core_count
;
598 int b_multi_threaded
;
599 int encoding_thread_count
;
601 pthread_t
*h_encoding_thread
;
602 pthread_t h_filter_thread
;
604 MB_ROW_COMP
*mb_row_ei
;
605 ENCODETHREAD_DATA
*en_thread_data
;
606 LPFTHREAD_DATA lpf_thread_data
;
609 sem_t
*h_event_start_encoding
;
610 sem_t h_event_end_encoding
;
611 sem_t h_event_start_lpf
;
612 sem_t h_event_end_lpf
;
616 // end of multithread data
619 fractional_mv_step_fp
*find_fractional_mv_step
;
620 vp8_full_search_fn_t full_search_sad
;
621 vp8_diamond_search_fn_t diamond_search_sad
;
622 vp8_variance_fn_ptr_t fn_ptr
[BLOCK_MAX_SEGMENTS
];
623 unsigned int time_receive_data
;
624 unsigned int time_compress_data
;
625 unsigned int time_pick_lpf
;
626 unsigned int time_encode_mb_row
;
628 unsigned int tempdata1
;
629 unsigned int tempdata2
;
631 int base_skip_false_prob
[128];
632 unsigned int section_intra_rating
;
634 double section_max_qfactor
;
637 #if CONFIG_RUNTIME_CPU_DETECT
638 VP8_ENCODER_RTCD rtcd
;
640 #if VP8_TEMPORAL_ALT_REF
641 SOURCE_SAMPLE alt_ref_buffer
;
642 YV12_BUFFER_CONFIG
*frames
[MAX_LAG_BUFFERS
];
643 int fixed_divide
[512];
652 double total_sq_error
;
657 double total_sq_error2
;
659 double summed_quality
;
660 double summed_weights
;
661 unsigned int tot_recode_hits
;
664 double total_ssimg_y
;
665 double total_ssimg_u
;
666 double total_ssimg_v
;
667 double total_ssimg_all
;
669 int b_calculate_ssimg
;
671 int b_calculate_psnr
;
674 unsigned char *gf_active_flags
; // Record of which MBs still refer to last golden frame either directly or through 0,0
677 //Store last frame's MV info for next frame MV prediction
679 int *lf_ref_frame_sign_bias
;
682 #if CONFIG_REALTIME_ONLY
683 int force_next_frame_intra
; /* force next frame to intra when kf_auto says so */
687 void control_data_rate(VP8_COMP
*cpi
);
689 void vp8_encode_frame(VP8_COMP
*cpi
);
691 void vp8_pack_bitstream(VP8_COMP
*cpi
, unsigned char *dest
, unsigned long *size
);
693 unsigned int vp8_activity_masking(VP8_COMP
*cpi
, MACROBLOCK
*x
);
695 int rd_cost_intra_mb(MACROBLOCKD
*x
);
697 void vp8_tokenize_mb(VP8_COMP
*, MACROBLOCKD
*, TOKENEXTRA
**);
699 void vp8_set_speed_features(VP8_COMP
*cpi
);
702 #define CHECK_MEM_ERROR(lval,expr) do {\
705 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
706 "Failed to allocate "#lval" at %s:%d", \
710 #define CHECK_MEM_ERROR(lval,expr) do {\
713 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
714 "Failed to allocate "#lval);\