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 VP8_ENCODER_ONYX_INT_H_
13 #define VP8_ENCODER_ONYX_INT_H_
16 #include "vpx_config.h"
17 #include "vp8/common/onyx.h"
18 #include "treewriter.h"
20 #include "vp8/common/onyxc_int.h"
21 #include "vpx_dsp/variance.h"
23 #include "vp8/encoder/quantize.h"
24 #include "vp8/common/entropy.h"
25 #include "vp8/common/threading.h"
26 #include "vpx_ports/mem.h"
27 #include "vpx/internal/vpx_codec_internal.h"
30 #include "vp8/common/findnearmv.h"
31 #include "lookahead.h"
32 #if CONFIG_TEMPORAL_DENOISING
33 #include "vp8/encoder/denoising.h"
40 #define MIN_GF_INTERVAL 4
41 #define DEFAULT_GF_INTERVAL 7
43 #define KEY_FRAME_CONTEXT 5
45 #define MAX_LAG_BUFFERS (CONFIG_REALTIME_ONLY? 1 : 25)
48 #define AF_THRESH2 100
49 #define ARF_DECAY_THRESH 12
52 #define MIN_THRESHMULT 32
53 #define MAX_THRESHMULT 512
55 #define GF_ZEROMV_ZBIN_BOOST 12
56 #define LF_ZEROMV_ZBIN_BOOST 6
57 #define MV_ZBIN_BOOST 4
58 #define ZBIN_OQ_MAX 192
60 #if !(CONFIG_REALTIME_ONLY)
61 #define VP8_TEMPORAL_ALT_REF 1
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];
80 int inter_y_modes
[10];
81 int inter_uv_modes
[4];
82 int inter_b_modes
[10];
85 vp8_prob ymode_prob
[4], uv_mode_prob
[3]; /* interframe intra mode probs */
86 vp8_prob kf_ymode_prob
[4], kf_uv_mode_prob
[3]; /* keyframe "" */
88 int ymode_count
[5], uv_mode_count
[4]; /* intra MB type cts this frame */
90 int count_mb_ref_frame_usage
[MAX_REF_FRAMES
];
92 int this_frame_percent_intra
;
93 int last_frame_percent_intra
;
103 double ssim_weighted_pred_err
;
106 double pcnt_second_ref
;
114 double mv_in_out_count
;
124 double frame_intra_error
;
125 double frame_coded_error
;
126 double frame_pcnt_inter
;
127 double frame_pcnt_motion
;
129 double frame_mvr_abs
;
131 double frame_mvc_abs
;
133 } ONEPASS_FRAMESTATS
;
179 SEARCH_METHODS search_method
;
184 int iterative_sub_pixel
;
185 int half_pixel_search
;
186 int quarter_pixel_search
;
187 int thresh_mult
[MAX_MODES
];
188 int max_step_search_steps
;
190 int optimize_coefficients
;
192 int use_fastquant_for_pick
;
193 int no_skip_block4x4_search
;
194 int improved_mv_pred
;
201 int segment_counts
[MAX_MB_SEGMENTS
];
235 /* Layer configuration */
237 int target_bandwidth
;
239 /* Layer specific coding parameters */
240 int64_t starting_buffer_level
;
241 int64_t optimal_buffer_level
;
242 int64_t maximum_buffer_size
;
243 int64_t starting_buffer_level_in_ms
;
244 int64_t optimal_buffer_level_in_ms
;
245 int64_t maximum_buffer_size_in_ms
;
247 int avg_frame_size_for_layer
;
249 int64_t buffer_level
;
250 int64_t bits_off_target
;
252 int64_t total_actual_bits
;
253 int total_target_vs_actual
;
256 int active_worst_quality
;
258 int active_best_quality
;
263 int avg_frame_qindex
;
265 double rate_correction_factor
;
266 double key_frame_rate_correction_factor
;
267 double gf_rate_correction_factor
;
271 int inter_frame_target
;
272 int64_t total_byte_count
;
276 int last_frame_percent_intra
;
278 int count_mb_ref_frame_usage
[MAX_REF_FRAMES
];
282 typedef struct VP8_COMP
285 DECLARE_ALIGNED(16, short, Y1quant
[QINDEX_RANGE
][16]);
286 DECLARE_ALIGNED(16, short, Y1quant_shift
[QINDEX_RANGE
][16]);
287 DECLARE_ALIGNED(16, short, Y1zbin
[QINDEX_RANGE
][16]);
288 DECLARE_ALIGNED(16, short, Y1round
[QINDEX_RANGE
][16]);
290 DECLARE_ALIGNED(16, short, Y2quant
[QINDEX_RANGE
][16]);
291 DECLARE_ALIGNED(16, short, Y2quant_shift
[QINDEX_RANGE
][16]);
292 DECLARE_ALIGNED(16, short, Y2zbin
[QINDEX_RANGE
][16]);
293 DECLARE_ALIGNED(16, short, Y2round
[QINDEX_RANGE
][16]);
295 DECLARE_ALIGNED(16, short, UVquant
[QINDEX_RANGE
][16]);
296 DECLARE_ALIGNED(16, short, UVquant_shift
[QINDEX_RANGE
][16]);
297 DECLARE_ALIGNED(16, short, UVzbin
[QINDEX_RANGE
][16]);
298 DECLARE_ALIGNED(16, short, UVround
[QINDEX_RANGE
][16]);
300 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y1
[QINDEX_RANGE
][16]);
301 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y2
[QINDEX_RANGE
][16]);
302 DECLARE_ALIGNED(16, short, zrun_zbin_boost_uv
[QINDEX_RANGE
][16]);
303 DECLARE_ALIGNED(16, short, Y1quant_fast
[QINDEX_RANGE
][16]);
304 DECLARE_ALIGNED(16, short, Y2quant_fast
[QINDEX_RANGE
][16]);
305 DECLARE_ALIGNED(16, short, UVquant_fast
[QINDEX_RANGE
][16]);
310 vp8_writer bc
[9]; /* one boolcoder for each partition */
314 struct lookahead_ctx
*lookahead
;
315 struct lookahead_entry
*source
;
316 struct lookahead_entry
*alt_ref_source
;
317 struct lookahead_entry
*last_source
;
319 YV12_BUFFER_CONFIG
*Source
;
320 YV12_BUFFER_CONFIG
*un_scaled_source
;
321 YV12_BUFFER_CONFIG scaled_source
;
322 YV12_BUFFER_CONFIG
*last_frame_unscaled_source
;
324 unsigned int frames_till_alt_ref_frame
;
325 /* frame in src_buffers has been identified to be encoded as an alt ref */
326 int source_alt_ref_pending
;
327 /* an alt ref frame has been encoded and is usable */
328 int source_alt_ref_active
;
329 /* source of frame to encode is an exact copy of an alt ref frame */
330 int is_src_frame_alt_ref
;
332 /* golden frame same as last frame ( short circuit gold searches) */
334 /* Alt reference frame same as last ( short circuit altref search) */
336 /* don't do both alt and gold search ( just do gold). */
339 YV12_BUFFER_CONFIG pick_lf_lvl_frame
;
342 unsigned int tok_count
;
345 unsigned int frames_since_key
;
346 unsigned int key_frame_frequency
;
347 unsigned int this_key_frame_forced
;
348 unsigned int next_key_frame_forced
;
350 /* Ambient reconstruction err target for force key frames */
353 unsigned int mode_check_freq
[MAX_MODES
];
355 int rd_baseline_thresh
[MAX_MODES
];
360 CODING_CONTEXT coding_context
;
362 /* Rate targetting variables */
363 int64_t last_prediction_error
;
364 int64_t last_intra_error
;
366 int this_frame_target
;
367 int projected_frame_size
;
368 int last_q
[2]; /* Separate values for Intra/Inter */
370 double rate_correction_factor
;
371 double key_frame_rate_correction_factor
;
372 double gf_rate_correction_factor
;
374 unsigned int frames_since_golden
;
375 /* Count down till next GF */
376 int frames_till_gf_update_due
;
378 /* GF interval chosen when we coded the last GF */
379 int current_gf_interval
;
381 /* Total bits overspent becasue of GF boost (cumulative) */
382 int gf_overspend_bits
;
384 /* Used in the few frames following a GF to recover the extra bits
387 int non_gf_bitrate_adjustment
;
389 /* Extra bits spent on key frames that need to be recovered */
390 int kf_overspend_bits
;
392 /* Current number of bit s to try and recover on each inter frame. */
393 int kf_bitrate_adjustment
;
395 int baseline_gf_interval
;
396 int active_arnr_frames
;
398 int64_t key_frame_count
;
399 int prior_key_frame_distance
[KEY_FRAME_CONTEXT
];
400 /* Current section per frame bandwidth target */
401 int per_frame_bandwidth
;
402 /* Average frame size target for clip */
403 int av_per_frame_bandwidth
;
404 /* Minimum allocation that should be used for any frame */
405 int min_frame_bandwidth
;
406 int inter_frame_target
;
407 double output_framerate
;
408 int64_t last_time_stamp_seen
;
409 int64_t last_end_time_stamp_seen
;
410 int64_t first_time_stamp_ever
;
415 int avg_frame_qindex
;
417 int64_t total_byte_count
;
422 double ref_framerate
;
423 int64_t buffer_level
;
424 int64_t bits_off_target
;
426 int rolling_target_bits
;
427 int rolling_actual_bits
;
429 int long_rolling_target_bits
;
430 int long_rolling_actual_bits
;
432 int64_t total_actual_bits
;
433 int total_target_vs_actual
; /* debug stats */
436 int active_worst_quality
;
438 int active_best_quality
;
440 int cq_target_quality
;
442 int drop_frames_allowed
; /* Are we permitted to drop frames? */
443 int drop_frame
; /* Drop this frame? */
445 vp8_prob frame_coef_probs
[BLOCK_TYPES
] [COEF_BANDS
] [PREV_COEF_CONTEXTS
] [ENTROPY_NODES
];
446 char update_probs
[BLOCK_TYPES
] [COEF_BANDS
] [PREV_COEF_CONTEXTS
] [ENTROPY_NODES
];
448 unsigned int frame_branch_ct
[BLOCK_TYPES
] [COEF_BANDS
] [PREV_COEF_CONTEXTS
] [ENTROPY_NODES
][2];
454 int target_bandwidth
;
455 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 int compressor_speed
;
473 int auto_adjust_gold_quantizer
;
479 int prob_intra_coded
;
483 int last_skip_false_probs
[3];
484 int last_skip_probs_q
[3];
485 int recent_ref_frame_usage
[MAX_REF_FRAMES
];
487 int this_frame_percent_intra
;
488 int last_frame_percent_intra
;
494 /* Count ZEROMV on all reference frames. */
498 unsigned char *segmentation_map
;
499 signed char segment_feature_data
[MB_LVL_MAX
][MAX_MB_SEGMENTS
];
500 int segment_encode_breakout
[MAX_MB_SEGMENTS
];
502 unsigned char *active_map
;
503 unsigned int active_map_enabled
;
505 /* Video conferencing cyclic refresh mode flags. This is a mode
506 * designed to clean up the background over time in live encoding
507 * scenarious. It uses segmentation.
509 int cyclic_refresh_mode_enabled
;
510 int cyclic_refresh_mode_max_mbs_perframe
;
511 int cyclic_refresh_mode_index
;
512 int cyclic_refresh_q
;
513 signed char *cyclic_refresh_map
;
514 // Count on how many (consecutive) times a macroblock uses ZER0MV_LAST.
515 unsigned char *consec_zero_last
;
516 // Counter that is reset when a block is checked for a mode-bias against
518 unsigned char *consec_zero_last_mvbias
;
520 // Frame counter for the temporal pattern. Counter is rest when the temporal
521 // layers are changed dynamically (run-time change).
522 unsigned int temporal_pattern_counter
;
523 // Temporal layer id.
524 int temporal_layer_id
;
526 // Measure of average squared difference between source and denoised signal.
527 int mse_source_denoised
;
531 #if CONFIG_MULTITHREAD
532 /* multithread data */
533 pthread_mutex_t
*pmutex
;
534 pthread_mutex_t mt_mutex
; /* mutex for b_multi_threaded */
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 unsigned char *partition_d
[MAX_PARTITIONS
];
557 unsigned char *partition_d_end
[MAX_PARTITIONS
];
560 fractional_mv_step_fp
*find_fractional_mv_step
;
561 vp8_full_search_fn_t full_search_sad
;
562 vp8_refining_search_fn_t refining_search_sad
;
563 vp8_diamond_search_fn_t diamond_search_sad
;
564 vp8_variance_fn_ptr_t fn_ptr
[BLOCK_MAX_SEGMENTS
];
565 uint64_t time_receive_data
;
566 uint64_t time_compress_data
;
567 uint64_t time_pick_lpf
;
568 uint64_t time_encode_mb_row
;
570 int base_skip_false_prob
[128];
572 FRAME_CONTEXT lfc_n
; /* last frame entropy */
573 FRAME_CONTEXT lfc_a
; /* last alt ref entropy */
574 FRAME_CONTEXT lfc_g
; /* last gold ref entropy */
579 unsigned int section_intra_rating
;
580 double section_max_qfactor
;
581 unsigned int next_iiratio
;
582 unsigned int this_iiratio
;
583 FIRSTPASS_STATS total_stats
;
584 FIRSTPASS_STATS this_frame_stats
;
585 FIRSTPASS_STATS
*stats_in
, *stats_in_end
, *stats_in_start
;
586 FIRSTPASS_STATS total_left_stats
;
589 int64_t clip_bits_total
;
591 double modified_error_total
;
592 double modified_error_used
;
593 double modified_error_left
;
594 double kf_intra_err_min
;
595 double gf_intra_err_min
;
600 int static_scene_max_gf_interval
;
602 /* Remaining error from uncoded frames in a gf group. */
603 int gf_group_error_left
;
604 /* Projected total bits available for a key frame group of frames */
605 int64_t kf_group_bits
;
606 /* Error score of frames still to be coded in kf group */
607 int64_t kf_group_error_left
;
608 /* Projected Bits available for a group including 1 GF or ARF */
609 int64_t gf_group_bits
;
610 /* Bits for the golden frame or ARF */
613 double est_max_qcorrection_factor
;
616 #if VP8_TEMPORAL_ALT_REF
617 YV12_BUFFER_CONFIG alt_ref_buffer
;
618 YV12_BUFFER_CONFIG
*frames
[MAX_LAG_BUFFERS
];
619 int fixed_divide
[512];
622 #if CONFIG_INTERNAL_STATS
628 double total_sq_error
;
633 double total_sq_error2
;
635 double summed_quality
;
636 double summed_weights
;
637 unsigned int tot_recode_hits
;
640 double total_ssimg_y
;
641 double total_ssimg_u
;
642 double total_ssimg_v
;
643 double total_ssimg_all
;
645 int b_calculate_ssimg
;
647 int b_calculate_psnr
;
649 /* Per MB activity measurement */
650 unsigned int activity_avg
;
651 unsigned int * mb_activity_map
;
653 /* Record of which MBs still refer to last golden frame either
654 * directly or through 0,0
656 unsigned char *gf_active_flags
;
659 int output_partition
;
661 /* Store last frame's MV info for next frame MV prediction */
663 int *lf_ref_frame_sign_bias
;
666 /* force next frame to intra when kf_auto says so */
667 int force_next_frame_intra
;
674 #if CONFIG_TEMPORAL_DENOISING
675 VP8_DENOISER denoiser
;
678 /* Coding layer state variables */
679 unsigned int current_layer
;
680 LAYER_CONTEXT layer_context
[VPX_TS_MAX_LAYERS
];
682 int64_t frames_in_layer
[VPX_TS_MAX_LAYERS
];
683 int64_t bytes_in_layer
[VPX_TS_MAX_LAYERS
];
684 double sum_psnr
[VPX_TS_MAX_LAYERS
];
685 double sum_psnr_p
[VPX_TS_MAX_LAYERS
];
686 double total_error2
[VPX_TS_MAX_LAYERS
];
687 double total_error2_p
[VPX_TS_MAX_LAYERS
];
688 double sum_ssim
[VPX_TS_MAX_LAYERS
];
689 double sum_weights
[VPX_TS_MAX_LAYERS
];
691 double total_ssimg_y_in_layer
[VPX_TS_MAX_LAYERS
];
692 double total_ssimg_u_in_layer
[VPX_TS_MAX_LAYERS
];
693 double total_ssimg_v_in_layer
[VPX_TS_MAX_LAYERS
];
694 double total_ssimg_all_in_layer
[VPX_TS_MAX_LAYERS
];
696 #if CONFIG_MULTI_RES_ENCODING
697 /* Number of MBs per row at lower-resolution level */
698 int mr_low_res_mb_cols
;
699 /* Indicate if lower-res mv info is available */
700 unsigned char mr_low_res_mv_avail
;
702 /* The frame number of each reference frames */
703 unsigned int current_ref_frames
[MAX_REF_FRAMES
];
704 // Closest reference frame to current frame.
705 MV_REFERENCE_FRAME closest_reference_frame
;
707 struct rd_costs_struct
709 int mvcosts
[2][MVvals
+1];
710 int mvsadcosts
[2][MVfpvals
+1];
711 int mbmode_cost
[2][MB_MODE_COUNT
];
712 int intra_uv_mode_cost
[2][MB_MODE_COUNT
];
713 int bmode_costs
[10][10][10];
714 int inter_bmode_costs
[B_MODE_COUNT
];
715 int token_costs
[BLOCK_TYPES
][COEF_BANDS
]
716 [PREV_COEF_CONTEXTS
][MAX_ENTROPY_TOKENS
];
720 void vp8_initialize_enc(void);
722 void vp8_alloc_compressor_data(VP8_COMP
*cpi
);
723 int vp8_reverse_trans(int x
);
724 void vp8_new_framerate(VP8_COMP
*cpi
, double framerate
);
725 void vp8_loopfilter_frame(VP8_COMP
*cpi
, VP8_COMMON
*cm
);
727 void vp8_pack_bitstream(VP8_COMP
*cpi
, unsigned char *dest
,
728 unsigned char *dest_end
, unsigned long *size
);
730 void vp8_tokenize_mb(VP8_COMP
*, MACROBLOCK
*, TOKENEXTRA
**);
732 void vp8_set_speed_features(VP8_COMP
*cpi
);
735 #define CHECK_MEM_ERROR(lval,expr) do {\
738 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
739 "Failed to allocate "#lval" at %s:%d", \
743 #define CHECK_MEM_ERROR(lval,expr) do {\
746 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
747 "Failed to allocate "#lval);\
754 #endif // VP8_ENCODER_ONYX_INT_H_