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"
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 24
51 #define LF_ZEROMV_ZBIN_BOOST 12
52 #define MV_ZBIN_BOOST 4
53 #define ZBIN_OQ_MAX 192
55 #if !(CONFIG_REALTIME_ONLY)
56 #define VP8_TEMPORAL_ALT_REF 1
62 unsigned int frames_since_key;
63 unsigned int frames_since_golden;
65 int frames_till_gf_update_due;
66 int recent_ref_frame_usage[MAX_REF_FRAMES];
69 int mvcosts[2][MVvals+1];
76 int inter_y_modes[10];
77 int inter_uv_modes[4];
78 int inter_b_modes[10];
81 vp8_prob ymode_prob[4], uv_mode_prob[3]; /* interframe intra mode probs */
82 vp8_prob kf_ymode_prob[4], kf_uv_mode_prob[3]; /* keyframe "" */
84 int ymode_count[5], uv_mode_count[4]; /* intra MB type cts this frame */
86 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
88 int this_frame_percent_intra;
89 int last_frame_percent_intra;
99 double ssim_weighted_pred_err;
102 double pcnt_second_ref;
110 double mv_in_out_count;
119 double frame_intra_error;
120 double frame_coded_error;
121 double frame_pcnt_inter;
122 double frame_pcnt_motion;
124 double frame_mvr_abs;
126 double frame_mvc_abs;
128 } ONEPASS_FRAMESTATS;
174 SEARCH_METHODS search_method;
179 int iterative_sub_pixel;
180 int half_pixel_search;
181 int quarter_pixel_search;
182 int thresh_mult[MAX_MODES];
186 int max_step_search_steps;
188 int optimize_coefficients;
190 int use_fastquant_for_pick;
191 int no_skip_block4x4_search;
192 int improved_mv_pred;
199 int segment_counts[MAX_MB_SEGMENTS];
222 typedef struct VP8_ENCODER_RTCD
224 VP8_COMMON_RTCD *common;
225 vp8_variance_rtcd_vtable_t variance;
226 vp8_fdct_rtcd_vtable_t fdct;
227 vp8_encodemb_rtcd_vtable_t encodemb;
228 vp8_quantize_rtcd_vtable_t quantize;
229 vp8_search_rtcd_vtable_t search;
230 vp8_temporal_rtcd_vtable_t temporal;
243 typedef struct VP8_COMP
246 DECLARE_ALIGNED(16, short, Y1quant[QINDEX_RANGE][16]);
247 DECLARE_ALIGNED(16, unsigned char, Y1quant_shift[QINDEX_RANGE][16]);
248 DECLARE_ALIGNED(16, short, Y1zbin[QINDEX_RANGE][16]);
249 DECLARE_ALIGNED(16, short, Y1round[QINDEX_RANGE][16]);
251 DECLARE_ALIGNED(16, short, Y2quant[QINDEX_RANGE][16]);
252 DECLARE_ALIGNED(16, unsigned char, Y2quant_shift[QINDEX_RANGE][16]);
253 DECLARE_ALIGNED(16, short, Y2zbin[QINDEX_RANGE][16]);
254 DECLARE_ALIGNED(16, short, Y2round[QINDEX_RANGE][16]);
256 DECLARE_ALIGNED(16, short, UVquant[QINDEX_RANGE][16]);
257 DECLARE_ALIGNED(16, unsigned char, UVquant_shift[QINDEX_RANGE][16]);
258 DECLARE_ALIGNED(16, short, UVzbin[QINDEX_RANGE][16]);
259 DECLARE_ALIGNED(16, short, UVround[QINDEX_RANGE][16]);
261 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y1[QINDEX_RANGE][16]);
262 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y2[QINDEX_RANGE][16]);
263 DECLARE_ALIGNED(16, short, zrun_zbin_boost_uv[QINDEX_RANGE][16]);
264 DECLARE_ALIGNED(16, short, Y1quant_fast[QINDEX_RANGE][16]);
265 DECLARE_ALIGNED(16, short, Y2quant_fast[QINDEX_RANGE][16]);
266 DECLARE_ALIGNED(16, short, UVquant_fast[QINDEX_RANGE][16]);
276 struct lookahead_ctx *lookahead;
277 struct lookahead_entry *source;
278 struct lookahead_entry *alt_ref_source;
280 YV12_BUFFER_CONFIG *Source;
281 YV12_BUFFER_CONFIG *un_scaled_source;
282 YV12_BUFFER_CONFIG scaled_source;
284 int source_alt_ref_pending; // frame in src_buffers has been identified to be encoded as an alt ref
285 int source_alt_ref_active; // an alt ref frame has been encoded and is usable
287 int is_src_frame_alt_ref; // source of frame to encode is an exact copy of an alt ref frame
289 int gold_is_last; // golden frame same as last frame ( short circuit gold searches)
290 int alt_is_last; // Alt reference frame same as last ( short circuit altref search)
291 int gold_is_alt; // don't do both alt and gold search ( just do gold).
293 //int refresh_alt_ref_frame;
294 YV12_BUFFER_CONFIG last_frame_uf;
297 unsigned int tok_count;
300 unsigned int frames_since_key;
301 unsigned int key_frame_frequency;
302 unsigned int this_key_frame_forced;
303 unsigned int next_key_frame_forced;
305 // Ambient reconstruction err target for force key frames
308 unsigned int mode_check_freq[MAX_MODES];
309 unsigned int mode_test_hit_counts[MAX_MODES];
310 unsigned int mode_chosen_counts[MAX_MODES];
311 unsigned int mbs_tested_so_far;
313 int rd_thresh_mult[MAX_MODES];
314 int rd_baseline_thresh[MAX_MODES];
315 int rd_threshes[MAX_MODES];
320 CODING_CONTEXT coding_context;
322 // Rate targetting variables
323 long long prediction_error;
324 long long last_prediction_error;
325 long long intra_error;
326 long long last_intra_error;
328 int this_frame_target;
329 int projected_frame_size;
330 int last_q[2]; // Separate values for Intra/Inter
332 double rate_correction_factor;
333 double key_frame_rate_correction_factor;
334 double gf_rate_correction_factor;
335 double est_max_qcorrection_factor;
337 int frames_till_gf_update_due; // Count down till next GF
338 int current_gf_interval; // GF interval chosen when we coded the last GF
340 int gf_overspend_bits; // Total bits overspent becasue of GF boost (cumulative)
342 int gf_group_bits; // Projected Bits available for a group of frames including 1 GF or ARF
343 int gf_bits; // Bits for the golden frame or ARF - 2 pass only
344 int mid_gf_extra_bits; // A few extra bits for the frame half way between two gfs.
346 // Projected total bits available for a key frame group of frames
347 long long kf_group_bits;
349 // Error score of frames still to be coded in kf group
350 long long kf_group_error_left;
352 // Bits for the key frame in a key frame group - 2 pass only
355 int non_gf_bitrate_adjustment; // Used in the few frames following a GF to recover the extra bits spent in that GF
356 int initial_gf_use; // percentage use of gf 2 frames after gf
358 int gf_group_error_left; // Remaining error from uncoded frames in a gf group. Two pass use only
360 int kf_overspend_bits; // Extra bits spent on key frames that need to be recovered on inter frames
361 int kf_bitrate_adjustment; // Current number of bit s to try and recover on each inter frame.
363 int static_scene_max_gf_interval;
364 int baseline_gf_interval;
366 int active_arnr_frames; // <= cpi->oxcf.arnr_max_frames
368 INT64 key_frame_count;
369 int prior_key_frame_distance[KEY_FRAME_CONTEXT];
370 int per_frame_bandwidth; // Current section per frame bandwidth target
371 int av_per_frame_bandwidth; // Average frame size target for clip
372 int min_frame_bandwidth; // Minimum allocation that should be used for any frame
373 int inter_frame_target;
374 double output_frame_rate;
375 long long last_time_stamp_seen;
376 long long last_end_time_stamp_seen;
377 long long first_time_stamp_ever;
382 int avg_frame_qindex;
386 int zbin_mode_boost_enabled;
388 INT64 total_byte_count;
395 int rolling_target_bits;
396 int rolling_actual_bits;
398 int long_rolling_target_bits;
399 int long_rolling_actual_bits;
401 long long total_actual_bits;
402 int total_target_vs_actual; // debug stats
405 int active_worst_quality;
407 int active_best_quality;
409 int cq_target_quality;
413 int drop_frames_allowed; // Are we permitted to drop frames?
414 int drop_frame; // Drop this frame?
415 int drop_count; // How many frames have we dropped?
416 int max_drop_count; // How many frames should we drop?
417 int max_consec_dropped_frames; // Limit number of consecutive frames that can be dropped.
420 int ymode_count [VP8_YMODES]; /* intra MB type cts this frame */
421 int uv_mode_count[VP8_UV_MODES]; /* intra MB type cts this frame */
423 unsigned int MVcount [2] [MVvals]; /* (row,col) MV cts this frame */
425 unsigned int coef_counts [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens]; /* for this frame */
426 //DECLARE_ALIGNED(16, int, coef_counts_backup [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens]); //not used any more
427 //save vp8_tree_probs_from_distribution result for each frame to avoid repeat calculation
428 vp8_prob frame_coef_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1];
429 unsigned int frame_branch_ct [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1][2];
435 double total_error_left;
436 double total_intra_error_left;
437 double total_coded_error_left;
438 double start_tot_err_left;
439 double kf_intra_err_min;
440 double gf_intra_err_min;
442 double modified_error_total;
443 double modified_error_used;
444 double modified_error_left;
448 int target_bandwidth;
450 long long clip_bits_total;
451 FIRSTPASS_STATS *total_stats;
452 FIRSTPASS_STATS *this_frame_stats;
453 FIRSTPASS_STATS *stats_in, *stats_in_end;
454 struct vpx_codec_pkt_list *output_pkt_list;
458 // Experimental code for lagged and one pass
459 ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS];
460 int one_pass_frame_index;
463 int decimation_factor;
464 int decimation_count;
466 // for real time encoding
467 int avg_encode_time; //microsecond
468 int avg_pick_mode_time; //microsecond
470 unsigned int cpu_freq; //Mhz
471 int compressor_speed;
475 int auto_adjust_gold_quantizer;
484 int prob_intra_coded;
488 int last_skip_false_probs[3];
489 int last_skip_probs_q[3];
490 int recent_ref_frame_usage[MAX_REF_FRAMES];
492 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
493 int this_frame_percent_intra;
494 int last_frame_percent_intra;
499 int error_bins[1024];
501 unsigned int next_iiratio;
502 unsigned int this_iiratio;
504 // Data used for real time conferencing mode to help determine if it would be good to update the gf
507 int gf_update_recommended;
509 int skip_false_count;
511 unsigned char *segmentation_map;
512 signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; // Segment data (can be deltas or absolute values)
513 int segment_encode_breakout[MAX_MB_SEGMENTS]; // segment threashold for encode breakout
515 unsigned char *active_map;
516 unsigned int active_map_enabled;
517 // Video conferencing cyclic refresh mode flags etc
518 // This is a mode designed to clean up the background over time in live encoding scenarious. It uses segmentation
519 int cyclic_refresh_mode_enabled;
520 int cyclic_refresh_mode_max_mbs_perframe;
521 int cyclic_refresh_mode_index;
522 int cyclic_refresh_q;
523 signed char *cyclic_refresh_map;
525 #if CONFIG_MULTITHREAD
527 int * mt_current_mb_col;
529 int b_multi_threaded;
530 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 // end of multithread data
550 fractional_mv_step_fp *find_fractional_mv_step;
551 vp8_full_search_fn_t full_search_sad;
552 vp8_refining_search_fn_t refining_search_sad;
553 vp8_diamond_search_fn_t diamond_search_sad;
554 vp8_variance_fn_ptr_t fn_ptr[BLOCK_MAX_SEGMENTS];
555 unsigned int time_receive_data;
556 unsigned int time_compress_data;
557 unsigned int time_pick_lpf;
558 unsigned int time_encode_mb_row;
560 int base_skip_false_prob[128];
561 unsigned int section_intra_rating;
563 double section_max_qfactor;
566 #if CONFIG_RUNTIME_CPU_DETECT
567 VP8_ENCODER_RTCD rtcd;
569 #if VP8_TEMPORAL_ALT_REF
570 YV12_BUFFER_CONFIG alt_ref_buffer;
571 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
572 int fixed_divide[512];
575 #if CONFIG_INTERNAL_STATS
581 double total_sq_error;
586 double total_sq_error2;
588 double summed_quality;
589 double summed_weights;
590 unsigned int tot_recode_hits;
593 double total_ssimg_y;
594 double total_ssimg_u;
595 double total_ssimg_v;
596 double total_ssimg_all;
598 int b_calculate_ssimg;
600 int b_calculate_psnr;
602 // Per MB activity measurement
603 unsigned int activity_avg;
604 unsigned int * mb_activity_map;
605 int * mb_norm_activity_map;
607 // Record of which MBs still refer to last golden frame either
608 // directly or through 0,0
609 unsigned char *gf_active_flags;
612 //Store last frame's MV info for next frame MV prediction
614 int *lf_ref_frame_sign_bias;
617 #if CONFIG_REALTIME_ONLY
618 int force_next_frame_intra; /* force next frame to intra when kf_auto says so */
622 void control_data_rate(VP8_COMP *cpi);
624 void vp8_encode_frame(VP8_COMP *cpi);
626 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size);
628 void vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x);
630 int rd_cost_intra_mb(MACROBLOCKD *x);
632 void vp8_tokenize_mb(VP8_COMP *, MACROBLOCKD *, TOKENEXTRA **);
634 void vp8_set_speed_features(VP8_COMP *cpi);
637 #define CHECK_MEM_ERROR(lval,expr) do {\
640 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
641 "Failed to allocate "#lval" at %s:%d", \
645 #define CHECK_MEM_ERROR(lval,expr) do {\
648 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
649 "Failed to allocate "#lval);\