Adjustment to boost calculation in two pass.
[libvpx.git] / vp8 / encoder / onyx_int.h
blob2f9cc4776b7947309ff21d87c48cc01c6e388615
1 /*
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.
9 */
12 #ifndef __INC_VP8_INT_H
13 #define __INC_VP8_INT_H
15 #include <stdio.h>
16 #include "vpx_ports/config.h"
17 #include "onyx.h"
18 #include "treewriter.h"
19 #include "tokenize.h"
20 #include "onyxc_int.h"
21 #include "variance.h"
22 #include "dct.h"
23 #include "encodemb.h"
24 #include "quantize.h"
25 #include "entropy.h"
26 #include "threading.h"
27 #include "vpx_ports/mem.h"
28 #include "vpx/internal/vpx_codec_internal.h"
29 #include "mcomp.h"
30 #include "temporal_filter.h"
32 //#define SPEEDSTATS 1
33 #define MIN_GF_INTERVAL 4
34 #define DEFAULT_GF_INTERVAL 7
36 #define KEY_FRAME_CONTEXT 5
38 #define MAX_LAG_BUFFERS (CONFIG_REALTIME_ONLY? 1 : 25)
40 #define AF_THRESH 25
41 #define AF_THRESH2 100
42 #define ARF_DECAY_THRESH 12
43 #define MAX_MODES 20
45 #define MIN_THRESHMULT 32
46 #define MAX_THRESHMULT 512
48 #define GF_ZEROMV_ZBIN_BOOST 24
49 #define LF_ZEROMV_ZBIN_BOOST 12
50 #define MV_ZBIN_BOOST 4
51 #define ZBIN_OQ_MAX 192
53 #define VP8_TEMPORAL_ALT_REF 1
55 typedef struct
57 int kf_indicated;
58 unsigned int frames_since_key;
59 unsigned int frames_since_golden;
60 int filter_level;
61 int frames_till_gf_update_due;
62 int recent_ref_frame_usage[MAX_REF_FRAMES];
64 MV_CONTEXT mvc[2];
65 int mvcosts[2][MVvals+1];
67 #ifdef MODE_STATS
68 // Stats
69 int y_modes[5];
70 int uv_modes[4];
71 int b_modes[10];
72 int inter_y_modes[10];
73 int inter_uv_modes[4];
74 int inter_b_modes[10];
75 #endif
77 vp8_prob ymode_prob[4], uv_mode_prob[3]; /* interframe intra mode probs */
78 vp8_prob kf_ymode_prob[4], kf_uv_mode_prob[3]; /* keyframe "" */
80 int ymode_count[5], uv_mode_count[4]; /* intra MB type cts this frame */
82 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
84 int this_frame_percent_intra;
85 int last_frame_percent_intra;
88 } CODING_CONTEXT;
90 typedef struct
92 double frame;
93 double intra_error;
94 double coded_error;
95 double ssim_weighted_pred_err;
96 double pcnt_inter;
97 double pcnt_motion;
98 double pcnt_second_ref;
99 double MVr;
100 double mvr_abs;
101 double MVc;
102 double mvc_abs;
103 double MVrv;
104 double MVcv;
105 double mv_in_out_count;
106 double duration;
107 double count;
109 FIRSTPASS_STATS;
111 typedef struct
113 int frames_so_far;
114 double frame_intra_error;
115 double frame_coded_error;
116 double frame_pcnt_inter;
117 double frame_pcnt_motion;
118 double frame_mvr;
119 double frame_mvr_abs;
120 double frame_mvc;
121 double frame_mvc_abs;
123 } ONEPASS_FRAMESTATS;
126 typedef enum
128 THR_ZEROMV = 0,
129 THR_DC = 1,
131 THR_NEARESTMV = 2,
132 THR_NEARMV = 3,
134 THR_ZEROG = 4,
135 THR_NEARESTG = 5,
137 THR_ZEROA = 6,
138 THR_NEARESTA = 7,
140 THR_NEARG = 8,
141 THR_NEARA = 9,
143 THR_V_PRED = 10,
144 THR_H_PRED = 11,
145 THR_TM = 12,
147 THR_NEWMV = 13,
148 THR_NEWG = 14,
149 THR_NEWA = 15,
151 THR_SPLITMV = 16,
152 THR_SPLITG = 17,
153 THR_SPLITA = 18,
155 THR_B_PRED = 19,
157 THR_MODES;
159 typedef enum
161 DIAMOND = 0,
162 NSTEP = 1,
163 HEX = 2
164 } SEARCH_METHODS;
166 typedef struct
168 int RD;
169 SEARCH_METHODS search_method;
170 int improved_quant;
171 int improved_dct;
172 int auto_filter;
173 int recode_loop;
174 int iterative_sub_pixel;
175 int half_pixel_search;
176 int quarter_pixel_search;
177 int thresh_mult[MAX_MODES];
178 int full_freq[2];
179 int min_fs_radius;
180 int max_fs_radius;
181 int max_step_search_steps;
182 int first_step;
183 int optimize_coefficients;
185 int use_fastquant_for_pick;
187 } SPEED_FEATURES;
189 typedef struct
191 MACROBLOCK mb;
192 int mb_row;
193 TOKENEXTRA *tp;
194 int segment_counts[MAX_MB_SEGMENTS];
195 int totalrate;
196 int current_mb_col;
197 } MB_ROW_COMP;
199 typedef struct
201 TOKENEXTRA *start;
202 TOKENEXTRA *stop;
203 } TOKENLIST;
205 typedef struct
207 int ithread;
208 void *ptr1;
209 void *ptr2;
210 } ENCODETHREAD_DATA;
211 typedef struct
213 int ithread;
214 void *ptr1;
215 } LPFTHREAD_DATA;
217 typedef struct
219 INT64 source_time_stamp;
220 INT64 source_end_time_stamp;
222 DECLARE_ALIGNED(16, YV12_BUFFER_CONFIG, source_buffer);
223 unsigned int source_frame_flags;
224 } SOURCE_SAMPLE;
226 typedef struct VP8_ENCODER_RTCD
228 VP8_COMMON_RTCD *common;
229 vp8_variance_rtcd_vtable_t variance;
230 vp8_fdct_rtcd_vtable_t fdct;
231 vp8_encodemb_rtcd_vtable_t encodemb;
232 vp8_quantize_rtcd_vtable_t quantize;
233 vp8_search_rtcd_vtable_t search;
234 vp8_temporal_rtcd_vtable_t temporal;
235 } VP8_ENCODER_RTCD;
237 enum
239 BLOCK_16X8,
240 BLOCK_8X16,
241 BLOCK_8X8,
242 BLOCK_4X4,
243 BLOCK_16X16,
244 BLOCK_MAX_SEGMENTS
247 typedef union
249 unsigned int as_int;
250 MV as_mv;
251 } int_mv; /* facilitates rapid equality tests */
253 typedef struct
256 DECLARE_ALIGNED(16, short, Y1quant[QINDEX_RANGE][16]);
257 DECLARE_ALIGNED(16, short, Y1quant_shift[QINDEX_RANGE][16]);
258 DECLARE_ALIGNED(16, short, Y1zbin[QINDEX_RANGE][16]);
259 DECLARE_ALIGNED(16, short, Y1round[QINDEX_RANGE][16]);
261 DECLARE_ALIGNED(16, short, Y2quant[QINDEX_RANGE][16]);
262 DECLARE_ALIGNED(16, short, Y2quant_shift[QINDEX_RANGE][16]);
263 DECLARE_ALIGNED(16, short, Y2zbin[QINDEX_RANGE][16]);
264 DECLARE_ALIGNED(16, short, Y2round[QINDEX_RANGE][16]);
266 DECLARE_ALIGNED(16, short, UVquant[QINDEX_RANGE][16]);
267 DECLARE_ALIGNED(16, short, UVquant_shift[QINDEX_RANGE][16]);
268 DECLARE_ALIGNED(16, short, UVzbin[QINDEX_RANGE][16]);
269 DECLARE_ALIGNED(16, short, UVround[QINDEX_RANGE][16]);
271 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y1[QINDEX_RANGE][16]);
272 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y2[QINDEX_RANGE][16]);
273 DECLARE_ALIGNED(16, short, zrun_zbin_boost_uv[QINDEX_RANGE][16]);
274 DECLARE_ALIGNED(16, short, Y1quant_fast[QINDEX_RANGE][16]);
275 DECLARE_ALIGNED(16, short, Y2quant_fast[QINDEX_RANGE][16]);
276 DECLARE_ALIGNED(16, short, UVquant_fast[QINDEX_RANGE][16]);
279 MACROBLOCK mb;
280 VP8_COMMON common;
281 vp8_writer bc, bc2;
282 // bool_writer *bc2;
284 VP8_CONFIG oxcf;
286 YV12_BUFFER_CONFIG *Source;
287 YV12_BUFFER_CONFIG *un_scaled_source;
288 INT64 source_time_stamp;
289 INT64 source_end_time_stamp;
290 unsigned int source_frame_flags;
291 YV12_BUFFER_CONFIG scaled_source;
293 int source_buffer_count; // number of src_buffers in use for lagged encoding
294 int source_encode_index; // index of buffer in src_buffer to encode
295 int source_alt_ref_pending; // frame in src_buffers has been identified to be encoded as an alt ref
296 int source_alt_ref_active; // an alt ref frame has been encoded and is usable
298 int last_alt_ref_sei; // index into src_buffers of frame used as alt reference
299 int is_src_frame_alt_ref; // source of frame to encode is an exact copy of an alt ref frame
300 int is_next_src_alt_ref; // source of next frame to encode is an exact copy of an alt ref frame
302 int gold_is_last; // golden frame same as last frame ( short circuit gold searches)
303 int alt_is_last; // Alt reference frame same as last ( short circuit altref search)
304 int gold_is_alt; // don't do both alt and gold search ( just do gold).
306 //int refresh_alt_ref_frame;
307 SOURCE_SAMPLE src_buffer[MAX_LAG_BUFFERS];
309 YV12_BUFFER_CONFIG last_frame_uf;
311 char *Dest;
313 TOKENEXTRA *tok;
314 unsigned int tok_count;
317 unsigned int frames_since_key;
318 unsigned int key_frame_frequency;
319 unsigned int next_key;
321 unsigned int mode_check_freq[MAX_MODES];
322 unsigned int mode_test_hit_counts[MAX_MODES];
323 unsigned int mode_chosen_counts[MAX_MODES];
324 unsigned int mbs_tested_so_far;
326 unsigned int check_freq[2];
327 unsigned int do_full[2];
329 int rd_thresh_mult[MAX_MODES];
330 int rd_baseline_thresh[MAX_MODES];
331 int rd_threshes[MAX_MODES];
332 int mvcostbase;
333 int mvcostmultiplier;
334 int subseqblockweight;
335 int errthresh;
336 unsigned int activity_avg;
338 int RDMULT;
339 int RDDIV ;
341 TOKENEXTRA *rdtok;
342 vp8_writer rdbc;
343 int intra_mode_costs[10];
346 CODING_CONTEXT coding_context;
348 // Rate targetting variables
349 long long prediction_error;
350 long long last_prediction_error;
351 long long intra_error;
352 long long last_intra_error;
353 long long last_auto_filter_prediction_error;
355 #if 0
356 // Experimental RD code
357 long long frame_distortion;
358 long long last_frame_distortion;
359 #endif
361 int last_mb_distortion;
363 int frames_since_auto_filter;
365 int this_frame_target;
366 int projected_frame_size;
367 int last_q[2]; // Separate values for Intra/Inter
368 int target_bits_per_mb;
370 double rate_correction_factor;
371 double key_frame_rate_correction_factor;
372 double gf_rate_correction_factor;
373 double est_max_qcorrection_factor;
375 int frames_till_gf_update_due; // Count down till next GF
376 int current_gf_interval; // GF interval chosen when we coded the last GF
378 int gf_overspend_bits; // Total bits overspent becasue of GF boost (cumulative)
380 int gf_group_bits; // Projected Bits available for a group of frames including 1 GF or ARF
381 int gf_bits; // Bits for the golden frame or ARF - 2 pass only
382 int mid_gf_extra_bits; // A few extra bits for the frame half way between two gfs.
384 // Projected total bits available for a key frame group of frames
385 long long kf_group_bits;
387 // Error score of frames still to be coded in kf group
388 long long kf_group_error_left;
390 // Bits for the key frame in a key frame group - 2 pass only
391 int kf_bits;
393 int non_gf_bitrate_adjustment; // Used in the few frames following a GF to recover the extra bits spent in that GF
394 int initial_gf_use; // percentage use of gf 2 frames after gf
396 int gf_group_error_left; // Remaining error from uncoded frames in a gf group. Two pass use only
398 int kf_overspend_bits; // Extra bits spent on key frames that need to be recovered on inter frames
399 int kf_bitrate_adjustment; // Current number of bit s to try and recover on each inter frame.
400 int max_gf_interval;
401 int baseline_gf_interval;
402 int gf_decay_rate;
403 int active_arnr_frames; // <= cpi->oxcf.arnr_max_frames
405 INT64 key_frame_count;
406 INT64 tot_key_frame_bits;
407 int prior_key_frame_size[KEY_FRAME_CONTEXT];
408 int prior_key_frame_distance[KEY_FRAME_CONTEXT];
409 int per_frame_bandwidth; // Current section per frame bandwidth target
410 int av_per_frame_bandwidth; // Average frame size target for clip
411 int min_frame_bandwidth; // Minimum allocation that should be used for any frame
412 int last_key_frame_size;
413 int intra_frame_target;
414 int inter_frame_target;
415 double output_frame_rate;
416 long long last_time_stamp_seen;
417 long long last_end_time_stamp_seen;
418 long long first_time_stamp_ever;
420 int ni_av_qi;
421 int ni_tot_qi;
422 int ni_frames;
423 int avg_frame_qindex;
425 int zbin_over_quant;
426 int zbin_mode_boost;
427 int zbin_mode_boost_enabled;
429 INT64 total_byte_count;
431 int buffered_mode;
433 int buffer_level;
434 int bits_off_target;
436 int rolling_target_bits;
437 int rolling_actual_bits;
439 int long_rolling_target_bits;
440 int long_rolling_actual_bits;
442 long long total_actual_bits;
443 int total_target_vs_actual; // debug stats
445 int worst_quality;
446 int active_worst_quality;
447 int best_quality;
448 int active_best_quality;
450 int drop_frames_allowed; // Are we permitted to drop frames?
451 int drop_frame; // Drop this frame?
452 int drop_count; // How many frames have we dropped?
453 int max_drop_count; // How many frames should we drop?
454 int max_consec_dropped_frames; // Limit number of consecutive frames that can be dropped.
457 int ymode_count [VP8_YMODES]; /* intra MB type cts this frame */
458 int uv_mode_count[VP8_UV_MODES]; /* intra MB type cts this frame */
460 unsigned int MVcount [2] [MVvals]; /* (row,col) MV cts this frame */
462 unsigned int coef_counts [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens]; /* for this frame */
463 //DECLARE_ALIGNED(16, int, coef_counts_backup [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens]); //not used any more
464 //save vp8_tree_probs_from_distribution result for each frame to avoid repeat calculation
465 vp8_prob frame_coef_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1];
466 unsigned int frame_branch_ct [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1][2];
468 /* Second compressed data partition contains coefficient data. */
470 unsigned char *output_partition2;
471 size_t output_partition2size;
473 int frames_to_key;
474 int gfu_boost;
475 int kf_boost;
476 int last_boost;
477 double total_error_left;
478 double total_intra_error_left;
479 double total_coded_error_left;
480 double start_tot_err_left;
481 double min_error;
482 double kf_intra_err_min;
483 double gf_intra_err_min;
485 double modified_error_total;
486 double modified_error_used;
487 double modified_error_left;
488 double clip_bpe;
489 double observed_bpe;
491 double avg_iiratio;
493 int target_bandwidth;
494 long long bits_left;
495 long long clip_bits_total;
496 FIRSTPASS_STATS *total_stats;
497 FIRSTPASS_STATS *this_frame_stats;
498 FIRSTPASS_STATS *stats_in, *stats_in_end;
499 struct vpx_codec_pkt_list *output_pkt_list;
500 int first_pass_done;
501 unsigned char *fp_motion_map;
503 unsigned char *fp_motion_map_stats, *fp_motion_map_stats_save;
505 #if 0
506 // Experimental code for lagged and one pass
507 ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS];
508 int one_pass_frame_index;
509 #endif
511 int decimation_factor;
512 int decimation_count;
514 // for real time encoding
515 int avg_encode_time; //microsecond
516 int avg_pick_mode_time; //microsecond
517 int Speed;
518 unsigned int cpu_freq; //Mhz
519 int compressor_speed;
521 int interquantizer;
522 int auto_gold;
523 int auto_adjust_gold_quantizer;
524 int goldquantizer;
525 int goldfreq;
526 int auto_adjust_key_quantizer;
527 int keyquantizer;
528 int auto_worst_q;
529 int filter_type;
530 int cpu_used;
531 int chroma_boost;
532 int horiz_scale;
533 int vert_scale;
534 int pass;
537 int prob_intra_coded;
538 int prob_last_coded;
539 int prob_gf_coded;
540 int prob_skip_false;
541 int last_skip_false_probs[3];
542 int last_skip_probs_q[3];
543 int recent_ref_frame_usage[MAX_REF_FRAMES];
545 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
546 int this_frame_percent_intra;
547 int last_frame_percent_intra;
549 int last_key_frame_q;
550 int last_kffilt_lvl;
552 int ref_frame_flags;
554 int exp[512];
556 SPEED_FEATURES sf;
557 int error_bins[1024];
559 int inter_lvl;
560 int intra_lvl;
561 int motion_lvl;
562 int motion_speed;
563 int motion_var;
564 unsigned int next_iiratio;
565 unsigned int this_iiratio;
566 int this_frame_modified_error;
568 double norm_intra_err_per_mb;
569 double norm_inter_err_per_mb;
570 double norm_iidiff_per_mb;
572 int last_best_mode_index; // Record of mode index chosen for previous macro block.
573 int last_auto_filt_val;
574 int last_auto_filt_q;
576 // Data used for real time conferencing mode to help determine if it would be good to update the gf
577 int inter_zz_count;
578 int gf_bad_count;
579 int gf_update_recommended;
580 int skip_true_count;
581 int skip_false_count;
583 int alt_qcount;
585 int ready_for_new_frame;
587 unsigned char *segmentation_map;
588 signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; // Segment data (can be deltas or absolute values)
589 int segment_encode_breakout[MAX_MB_SEGMENTS]; // segment threashold for encode breakout
591 unsigned char *active_map;
592 unsigned int active_map_enabled;
593 // Video conferencing cyclic refresh mode flags etc
594 // This is a mode designed to clean up the background over time in live encoding scenarious. It uses segmentation
595 int cyclic_refresh_mode_enabled;
596 int cyclic_refresh_mode_max_mbs_perframe;
597 int cyclic_refresh_mode_index;
598 int cyclic_refresh_q;
599 signed char *cyclic_refresh_map;
601 // multithread data
602 int current_mb_col_main;
603 int processor_core_count;
604 int b_multi_threaded;
605 int encoding_thread_count;
607 #if CONFIG_MULTITHREAD
608 pthread_t *h_encoding_thread;
609 #endif
610 MB_ROW_COMP *mb_row_ei;
611 ENCODETHREAD_DATA *en_thread_data;
613 #if CONFIG_MULTITHREAD
614 //events
615 sem_t *h_event_mbrencoding;
616 sem_t h_event_main;
617 #endif
619 TOKENLIST *tplist;
620 // end of multithread data
623 fractional_mv_step_fp *find_fractional_mv_step;
624 vp8_full_search_fn_t full_search_sad;
625 vp8_diamond_search_fn_t diamond_search_sad;
626 vp8_variance_fn_ptr_t fn_ptr[BLOCK_MAX_SEGMENTS];
627 unsigned int time_receive_data;
628 unsigned int time_compress_data;
629 unsigned int time_pick_lpf;
630 unsigned int time_encode_mb_row;
632 unsigned int tempdata1;
633 unsigned int tempdata2;
635 int base_skip_false_prob[128];
636 unsigned int section_intra_rating;
638 double section_max_qfactor;
641 #if CONFIG_RUNTIME_CPU_DETECT
642 VP8_ENCODER_RTCD rtcd;
643 #endif
644 #if VP8_TEMPORAL_ALT_REF
645 SOURCE_SAMPLE alt_ref_buffer;
646 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
647 int fixed_divide[512];
648 #endif
649 // Flag to indicate temporal filter method
650 int use_weighted_temporal_filter;
652 #if CONFIG_PSNR
653 int count;
654 double total_y;
655 double total_u;
656 double total_v;
657 double total ;
658 double total_sq_error;
659 double totalp_y;
660 double totalp_u;
661 double totalp_v;
662 double totalp;
663 double total_sq_error2;
664 int bytes;
665 double summed_quality;
666 double summed_weights;
667 unsigned int tot_recode_hits;
670 double total_ssimg_y;
671 double total_ssimg_u;
672 double total_ssimg_v;
673 double total_ssimg_all;
675 int b_calculate_ssimg;
676 #endif
677 int b_calculate_psnr;
680 unsigned char *gf_active_flags; // Record of which MBs still refer to last golden frame either directly or through 0,0
681 int gf_active_count;
683 //Store last frame's MV info for next frame MV prediction
684 int_mv *lfmv;
685 int *lf_ref_frame_sign_bias;
686 int *lf_ref_frame;
688 } VP8_COMP;
690 void control_data_rate(VP8_COMP *cpi);
692 void vp8_encode_frame(VP8_COMP *cpi);
694 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size);
696 unsigned int vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x);
698 int rd_cost_intra_mb(MACROBLOCKD *x);
700 void vp8_tokenize_mb(VP8_COMP *, MACROBLOCKD *, TOKENEXTRA **);
702 void vp8_set_speed_features(VP8_COMP *cpi);
704 #if CONFIG_DEBUG
705 #define CHECK_MEM_ERROR(lval,expr) do {\
706 lval = (expr); \
707 if(!lval) \
708 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
709 "Failed to allocate "#lval" at %s:%d", \
710 __FILE__,__LINE__);\
711 } while(0)
712 #else
713 #define CHECK_MEM_ERROR(lval,expr) do {\
714 lval = (expr); \
715 if(!lval) \
716 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
717 "Failed to allocate "#lval);\
718 } while(0)
719 #endif
720 #endif