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 VP8_ENCODER_ONYX_INT_H_
12 #define 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
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];
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;
96 double ssim_weighted_pred_err;
99 double pcnt_second_ref;
107 double mv_in_out_count;
115 double frame_intra_error;
116 double frame_coded_error;
117 double frame_pcnt_inter;
118 double frame_pcnt_motion;
120 double frame_mvr_abs;
122 double frame_mvc_abs;
124 } ONEPASS_FRAMESTATS;
157 typedef enum { DIAMOND = 0, NSTEP = 1, HEX = 2 } SEARCH_METHODS;
161 SEARCH_METHODS search_method;
166 int iterative_sub_pixel;
167 int half_pixel_search;
168 int quarter_pixel_search;
169 int thresh_mult[MAX_MODES];
170 int max_step_search_steps;
172 int optimize_coefficients;
174 int use_fastquant_for_pick;
175 int no_skip_block4x4_search;
176 int improved_mv_pred;
182 int segment_counts[MAX_MB_SEGMENTS];
211 /* Layer configuration */
213 int target_bandwidth;
215 /* Layer specific coding parameters */
216 int64_t starting_buffer_level;
217 int64_t optimal_buffer_level;
218 int64_t maximum_buffer_size;
219 int64_t starting_buffer_level_in_ms;
220 int64_t optimal_buffer_level_in_ms;
221 int64_t maximum_buffer_size_in_ms;
223 int avg_frame_size_for_layer;
225 int64_t buffer_level;
226 int64_t bits_off_target;
228 int64_t total_actual_bits;
229 int total_target_vs_actual;
232 int active_worst_quality;
234 int active_best_quality;
239 int avg_frame_qindex;
241 double rate_correction_factor;
242 double key_frame_rate_correction_factor;
243 double gf_rate_correction_factor;
247 int inter_frame_target;
248 int64_t total_byte_count;
252 int last_frame_percent_intra;
254 int count_mb_ref_frame_usage[MAX_REF_FRAMES];
258 typedef struct VP8_COMP {
259 DECLARE_ALIGNED(16, short, Y1quant[QINDEX_RANGE][16]);
260 DECLARE_ALIGNED(16, short, Y1quant_shift[QINDEX_RANGE][16]);
261 DECLARE_ALIGNED(16, short, Y1zbin[QINDEX_RANGE][16]);
262 DECLARE_ALIGNED(16, short, Y1round[QINDEX_RANGE][16]);
264 DECLARE_ALIGNED(16, short, Y2quant[QINDEX_RANGE][16]);
265 DECLARE_ALIGNED(16, short, Y2quant_shift[QINDEX_RANGE][16]);
266 DECLARE_ALIGNED(16, short, Y2zbin[QINDEX_RANGE][16]);
267 DECLARE_ALIGNED(16, short, Y2round[QINDEX_RANGE][16]);
269 DECLARE_ALIGNED(16, short, UVquant[QINDEX_RANGE][16]);
270 DECLARE_ALIGNED(16, short, UVquant_shift[QINDEX_RANGE][16]);
271 DECLARE_ALIGNED(16, short, UVzbin[QINDEX_RANGE][16]);
272 DECLARE_ALIGNED(16, short, UVround[QINDEX_RANGE][16]);
274 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y1[QINDEX_RANGE][16]);
275 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y2[QINDEX_RANGE][16]);
276 DECLARE_ALIGNED(16, short, zrun_zbin_boost_uv[QINDEX_RANGE][16]);
277 DECLARE_ALIGNED(16, short, Y1quant_fast[QINDEX_RANGE][16]);
278 DECLARE_ALIGNED(16, short, Y2quant_fast[QINDEX_RANGE][16]);
279 DECLARE_ALIGNED(16, short, UVquant_fast[QINDEX_RANGE][16]);
283 vp8_writer bc[9]; /* one boolcoder for each partition */
287 struct lookahead_ctx *lookahead;
288 struct lookahead_entry *source;
289 struct lookahead_entry *alt_ref_source;
290 struct lookahead_entry *last_source;
292 YV12_BUFFER_CONFIG *Source;
293 YV12_BUFFER_CONFIG *un_scaled_source;
294 YV12_BUFFER_CONFIG scaled_source;
295 YV12_BUFFER_CONFIG *last_frame_unscaled_source;
297 unsigned int frames_till_alt_ref_frame;
298 /* frame in src_buffers has been identified to be encoded as an alt ref */
299 int source_alt_ref_pending;
300 /* an alt ref frame has been encoded and is usable */
301 int source_alt_ref_active;
302 /* source of frame to encode is an exact copy of an alt ref frame */
303 int is_src_frame_alt_ref;
305 /* golden frame same as last frame ( short circuit gold searches) */
307 /* Alt reference frame same as last ( short circuit altref search) */
309 /* don't do both alt and gold search ( just do gold). */
312 YV12_BUFFER_CONFIG pick_lf_lvl_frame;
315 unsigned int tok_count;
317 unsigned int frames_since_key;
318 unsigned int key_frame_frequency;
319 unsigned int this_key_frame_forced;
320 unsigned int next_key_frame_forced;
322 /* Ambient reconstruction err target for force key frames */
325 unsigned int mode_check_freq[MAX_MODES];
327 int rd_baseline_thresh[MAX_MODES];
332 CODING_CONTEXT coding_context;
334 /* Rate targetting variables */
335 int64_t last_prediction_error;
336 int64_t last_intra_error;
338 int this_frame_target;
339 int projected_frame_size;
340 int last_q[2]; /* Separate values for Intra/Inter */
342 double rate_correction_factor;
343 double key_frame_rate_correction_factor;
344 double gf_rate_correction_factor;
346 int frames_since_golden;
347 /* Count down till next GF */
348 int frames_till_gf_update_due;
350 /* GF interval chosen when we coded the last GF */
351 int current_gf_interval;
353 /* Total bits overspent becasue of GF boost (cumulative) */
354 int gf_overspend_bits;
356 /* Used in the few frames following a GF to recover the extra bits
359 int non_gf_bitrate_adjustment;
361 /* Extra bits spent on key frames that need to be recovered */
362 int kf_overspend_bits;
364 /* Current number of bit s to try and recover on each inter frame. */
365 int kf_bitrate_adjustment;
367 int baseline_gf_interval;
368 int active_arnr_frames;
370 int64_t key_frame_count;
371 int prior_key_frame_distance[KEY_FRAME_CONTEXT];
372 /* Current section per frame bandwidth target */
373 int per_frame_bandwidth;
374 /* Average frame size target for clip */
375 int av_per_frame_bandwidth;
376 /* Minimum allocation that should be used for any frame */
377 int min_frame_bandwidth;
378 int inter_frame_target;
379 double output_framerate;
380 int64_t last_time_stamp_seen;
381 int64_t last_end_time_stamp_seen;
382 int64_t first_time_stamp_ever;
387 int avg_frame_qindex;
389 int64_t total_byte_count;
394 double ref_framerate;
395 int64_t buffer_level;
396 int64_t bits_off_target;
398 int rolling_target_bits;
399 int rolling_actual_bits;
401 int long_rolling_target_bits;
402 int long_rolling_actual_bits;
404 int64_t total_actual_bits;
405 int total_target_vs_actual; /* debug stats */
408 int active_worst_quality;
410 int active_best_quality;
412 int cq_target_quality;
414 int drop_frames_allowed; /* Are we permitted to drop frames? */
415 int drop_frame; /* Drop this frame? */
417 vp8_prob frame_coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS]
419 char update_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES];
421 unsigned int frame_branch_ct[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS]
428 int target_bandwidth;
429 struct vpx_codec_pkt_list *output_pkt_list;
432 /* Experimental code for lagged and one pass */
433 ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS];
434 int one_pass_frame_index;
437 int decimation_factor;
438 int decimation_count;
440 /* for real time encoding */
441 int avg_encode_time; /* microsecond */
442 int avg_pick_mode_time; /* microsecond */
444 int compressor_speed;
447 int auto_adjust_gold_quantizer;
452 int prob_intra_coded;
456 int last_skip_false_probs[3];
457 int last_skip_probs_q[3];
458 int recent_ref_frame_usage[MAX_REF_FRAMES];
460 int this_frame_percent_intra;
461 int last_frame_percent_intra;
467 /* Count ZEROMV on all reference frames. */
471 unsigned char *segmentation_map;
472 signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
473 int segment_encode_breakout[MAX_MB_SEGMENTS];
475 unsigned char *active_map;
476 unsigned int active_map_enabled;
478 /* Video conferencing cyclic refresh mode flags. This is a mode
479 * designed to clean up the background over time in live encoding
480 * scenarious. It uses segmentation.
482 int cyclic_refresh_mode_enabled;
483 int cyclic_refresh_mode_max_mbs_perframe;
484 int cyclic_refresh_mode_index;
485 int cyclic_refresh_q;
486 signed char *cyclic_refresh_map;
487 // Count on how many (consecutive) times a macroblock uses ZER0MV_LAST.
488 unsigned char *consec_zero_last;
489 // Counter that is reset when a block is checked for a mode-bias against
491 unsigned char *consec_zero_last_mvbias;
493 // Frame counter for the temporal pattern. Counter is rest when the temporal
494 // layers are changed dynamically (run-time change).
495 unsigned int temporal_pattern_counter;
496 // Temporal layer id.
497 int temporal_layer_id;
499 // Measure of average squared difference between source and denoised signal.
500 int mse_source_denoised;
504 #if CONFIG_MULTITHREAD
505 /* multithread data */
506 int *mt_current_mb_col;
508 int b_multi_threaded;
509 int encoding_thread_count;
512 pthread_t *h_encoding_thread;
513 pthread_t h_filter_thread;
515 MB_ROW_COMP *mb_row_ei;
516 ENCODETHREAD_DATA *en_thread_data;
517 LPFTHREAD_DATA lpf_thread_data;
520 sem_t *h_event_start_encoding;
521 sem_t h_event_end_encoding;
522 sem_t h_event_start_lpf;
523 sem_t h_event_end_lpf;
527 unsigned int partition_sz[MAX_PARTITIONS];
528 unsigned char *partition_d[MAX_PARTITIONS];
529 unsigned char *partition_d_end[MAX_PARTITIONS];
531 fractional_mv_step_fp *find_fractional_mv_step;
532 vp8_full_search_fn_t full_search_sad;
533 vp8_refining_search_fn_t refining_search_sad;
534 vp8_diamond_search_fn_t diamond_search_sad;
535 vp8_variance_fn_ptr_t fn_ptr[BLOCK_MAX_SEGMENTS];
536 uint64_t time_receive_data;
537 uint64_t time_compress_data;
538 uint64_t time_pick_lpf;
539 uint64_t time_encode_mb_row;
541 int base_skip_false_prob[128];
543 FRAME_CONTEXT lfc_n; /* last frame entropy */
544 FRAME_CONTEXT lfc_a; /* last alt ref entropy */
545 FRAME_CONTEXT lfc_g; /* last gold ref entropy */
548 unsigned int section_intra_rating;
549 double section_max_qfactor;
550 unsigned int next_iiratio;
551 unsigned int this_iiratio;
552 FIRSTPASS_STATS total_stats;
553 FIRSTPASS_STATS this_frame_stats;
554 FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
555 FIRSTPASS_STATS total_left_stats;
558 int64_t clip_bits_total;
560 double modified_error_total;
561 double modified_error_used;
562 double modified_error_left;
563 double kf_intra_err_min;
564 double gf_intra_err_min;
569 int static_scene_max_gf_interval;
571 /* Remaining error from uncoded frames in a gf group. */
572 int gf_group_error_left;
573 /* Projected total bits available for a key frame group of frames */
574 int64_t kf_group_bits;
575 /* Error score of frames still to be coded in kf group */
576 int64_t kf_group_error_left;
577 /* Projected Bits available for a group including 1 GF or ARF */
578 int64_t gf_group_bits;
579 /* Bits for the golden frame or ARF */
582 double est_max_qcorrection_factor;
585 #if VP8_TEMPORAL_ALT_REF
586 YV12_BUFFER_CONFIG alt_ref_buffer;
587 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
588 int fixed_divide[512];
591 #if CONFIG_INTERNAL_STATS
597 double total_sq_error;
602 double total_sq_error2;
604 double summed_quality;
605 double summed_weights;
606 unsigned int tot_recode_hits;
608 int b_calculate_ssimg;
610 int b_calculate_psnr;
612 /* Per MB activity measurement */
613 unsigned int activity_avg;
614 unsigned int *mb_activity_map;
616 /* Record of which MBs still refer to last golden frame either
617 * directly or through 0,0
619 unsigned char *gf_active_flags;
622 int output_partition;
624 /* Store last frame's MV info for next frame MV prediction */
626 int *lf_ref_frame_sign_bias;
629 /* force next frame to intra when kf_auto says so */
630 int force_next_frame_intra;
637 #if CONFIG_TEMPORAL_DENOISING
638 VP8_DENOISER denoiser;
641 /* Coding layer state variables */
642 unsigned int current_layer;
643 LAYER_CONTEXT layer_context[VPX_TS_MAX_LAYERS];
645 int64_t frames_in_layer[VPX_TS_MAX_LAYERS];
646 int64_t bytes_in_layer[VPX_TS_MAX_LAYERS];
647 double sum_psnr[VPX_TS_MAX_LAYERS];
648 double sum_psnr_p[VPX_TS_MAX_LAYERS];
649 double total_error2[VPX_TS_MAX_LAYERS];
650 double total_error2_p[VPX_TS_MAX_LAYERS];
651 double sum_ssim[VPX_TS_MAX_LAYERS];
652 double sum_weights[VPX_TS_MAX_LAYERS];
654 double total_ssimg_y_in_layer[VPX_TS_MAX_LAYERS];
655 double total_ssimg_u_in_layer[VPX_TS_MAX_LAYERS];
656 double total_ssimg_v_in_layer[VPX_TS_MAX_LAYERS];
657 double total_ssimg_all_in_layer[VPX_TS_MAX_LAYERS];
659 #if CONFIG_MULTI_RES_ENCODING
660 /* Number of MBs per row at lower-resolution level */
661 int mr_low_res_mb_cols;
662 /* Indicate if lower-res mv info is available */
663 unsigned char mr_low_res_mv_avail;
665 /* The frame number of each reference frames */
666 unsigned int current_ref_frames[MAX_REF_FRAMES];
667 // Closest reference frame to current frame.
668 MV_REFERENCE_FRAME closest_reference_frame;
670 struct rd_costs_struct {
671 int mvcosts[2][MVvals + 1];
672 int mvsadcosts[2][MVfpvals + 1];
673 int mbmode_cost[2][MB_MODE_COUNT];
674 int intra_uv_mode_cost[2][MB_MODE_COUNT];
675 int bmode_costs[10][10][10];
676 int inter_bmode_costs[B_MODE_COUNT];
677 int token_costs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS]
678 [MAX_ENTROPY_TOKENS];
682 void vp8_initialize_enc(void);
684 void vp8_alloc_compressor_data(VP8_COMP *cpi);
685 int vp8_reverse_trans(int x);
686 void vp8_new_framerate(VP8_COMP *cpi, double framerate);
687 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm);
689 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest,
690 unsigned char *dest_end, unsigned long *size);
692 void vp8_tokenize_mb(VP8_COMP *, MACROBLOCK *, TOKENEXTRA **);
694 void vp8_set_speed_features(VP8_COMP *cpi);
697 #define CHECK_MEM_ERROR(lval, expr) \
701 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, \
702 "Failed to allocate " #lval " at %s:%d", __FILE__, \
706 #define CHECK_MEM_ERROR(lval, expr) \
710 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, \
711 "Failed to allocate " #lval); \
718 #endif // VP8_ENCODER_ONYX_INT_H_