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 VP9_ENCODER_VP9_ENCODER_H_
12 #define VP9_ENCODER_VP9_ENCODER_H_
16 #include "./vpx_config.h"
17 #include "vpx/internal/vpx_codec_internal.h"
18 #include "vpx/vp8cx.h"
20 #include "vp9/common/vp9_ppflags.h"
21 #include "vp9/common/vp9_entropymode.h"
22 #include "vp9/common/vp9_onyxc_int.h"
24 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
25 #include "vp9/encoder/vp9_context_tree.h"
26 #include "vp9/encoder/vp9_encodemb.h"
27 #include "vp9/encoder/vp9_firstpass.h"
28 #include "vp9/encoder/vp9_lookahead.h"
29 #include "vp9/encoder/vp9_mbgraph.h"
30 #include "vp9/encoder/vp9_mcomp.h"
31 #include "vp9/encoder/vp9_quantize.h"
32 #include "vp9/encoder/vp9_ratectrl.h"
33 #include "vp9/encoder/vp9_rd.h"
34 #include "vp9/encoder/vp9_speed_features.h"
35 #include "vp9/encoder/vp9_svc_layercontext.h"
36 #include "vp9/encoder/vp9_tokenize.h"
37 #include "vp9/encoder/vp9_variance.h"
38 #if CONFIG_VP9_TEMPORAL_DENOISING
39 #include "vp9/encoder/vp9_denoiser.h"
46 #define DEFAULT_GF_INTERVAL 10
49 int nmvjointcost[MV_JOINTS];
50 int nmvcosts[2][MV_VALS];
51 int nmvcosts_hp[2][MV_VALS];
53 vp9_prob segment_pred_probs[PREDICTION_PROBS];
55 unsigned char *last_frame_seg_map_copy;
57 // 0 = Intra, Last, GF, ARF
58 signed char last_ref_lf_deltas[MAX_REF_LF_DELTAS];
60 signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
67 // encode_breakout is disabled.
68 ENCODE_BREAKOUT_DISABLED = 0,
69 // encode_breakout is enabled.
70 ENCODE_BREAKOUT_ENABLED = 1,
71 // encode_breakout is enabled with small max_thresh limit.
72 ENCODE_BREAKOUT_LIMITED = 2
73 } ENCODE_BREAKOUT_TYPE;
83 // Good Quality Fast Encoding. The encoder balances quality with the amount of
84 // time it takes to encode the output. Speed setting controls how fast.
87 // The encoder places priority on the quality of the output over encoding
88 // speed. The output is compressed at the highest possible quality. This
89 // option takes the longest amount of time to encode. Speed setting ignored.
92 // Realtime/Live Encoding. This mode is optimized for realtime encoding (for
93 // example, capturing a television signal or feed from a live camera). Speed
94 // setting controls how fast.
99 FRAMEFLAGS_KEY = 1 << 0,
100 FRAMEFLAGS_GOLDEN = 1 << 1,
101 FRAMEFLAGS_ALTREF = 1 << 2,
108 CYCLIC_REFRESH_AQ = 3,
109 AQ_MODE_COUNT // This should always be the last member of the enum
113 typedef struct VP9EncoderConfig {
114 BITSTREAM_PROFILE profile;
115 vpx_bit_depth_t bit_depth; // Codec bit-depth.
116 int width; // width of data passed to the compressor
117 int height; // height of data passed to the compressor
118 unsigned int input_bit_depth; // Input bit depth.
119 double init_framerate; // set to passed in framerate
120 int64_t target_bandwidth; // bandwidth to be used in kilobits per second
122 int noise_sensitivity; // pre processing blur: recommendation 0
123 int sharpness; // sharpening output: recommendation 0:
125 unsigned int rc_max_intra_bitrate_pct;
130 // Key Framing Operations
131 int auto_key; // autodetect cut scenes and set the keyframes
132 int key_freq; // maximum distance to key frame.
134 int lag_in_frames; // how many frames lag before we start encoding
136 // ----------------------------------------------------------------
137 // DATARATE CONTROL OPTIONS
139 // vbr, cbr, constrained quality or constant quality
140 enum vpx_rc_mode rc_mode;
142 // buffer targeting aggressiveness
146 // buffering parameters
147 int64_t starting_buffer_level_ms;
148 int64_t optimal_buffer_level_ms;
149 int64_t maximum_buffer_size_ms;
151 // Frame drop threshold.
152 int drop_frames_water_mark;
154 // controlling quality
159 AQ_MODE aq_mode; // Adaptive Quantization mode
161 // Internal frame size scaling.
162 int allow_spatial_resampling;
163 int scaled_frame_width;
164 int scaled_frame_height;
166 // Enable feature to reduce the frame quantization every x frames.
167 int frame_periodic_boost;
169 // two pass datarate control
170 int two_pass_vbrbias; // two pass datarate control tweaks
171 int two_pass_vbrmin_section;
172 int two_pass_vbrmax_section;
173 // END DATARATE CONTROL OPTIONS
174 // ----------------------------------------------------------------
176 // Spatial and temporal scalability.
177 int ss_number_layers; // Number of spatial layers.
178 int ts_number_layers; // Number of temporal layers.
179 // Bitrate allocation for spatial layers.
180 int ss_target_bitrate[VPX_SS_MAX_LAYERS];
181 int ss_play_alternate[VPX_SS_MAX_LAYERS];
182 // Bitrate allocation (CBR mode) and framerate factor, for temporal layers.
183 int ts_target_bitrate[VPX_TS_MAX_LAYERS];
184 int ts_rate_decimator[VPX_TS_MAX_LAYERS];
186 // these parameters aren't to be used in final build don't use!!!
189 int encode_breakout; // early breakout : for video conf recommend 800
191 /* Bitfield defining the error resiliency features to enable.
192 * Can provide decodable frames after losses in previous
193 * frames and decodable partitions after losses in the same frame.
195 unsigned int error_resilient_mode;
197 /* Bitfield defining the parallel decoding mode where the
198 * decoding in successive frames may be conducted in parallel
199 * just by decoding the frame headers.
201 unsigned int frame_parallel_decoding_mode;
209 vpx_fixed_buf_t two_pass_stats_in;
210 struct vpx_codec_pkt_list *output_pkt_list;
212 #if CONFIG_FP_MB_STATS
213 vpx_fixed_buf_t firstpass_mb_stats_in;
217 vp9e_tune_content content;
218 #if CONFIG_VP9_HIGHBITDEPTH
219 int use_highbitdepth;
223 static INLINE int is_lossless_requested(const VP9EncoderConfig *cfg) {
224 return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
227 typedef struct VP9_COMP {
231 VP9EncoderConfig oxcf;
232 struct lookahead_ctx *lookahead;
233 struct lookahead_entry *alt_ref_source;
235 YV12_BUFFER_CONFIG *Source;
236 YV12_BUFFER_CONFIG *Last_Source; // NULL for first frame and alt_ref frames
237 YV12_BUFFER_CONFIG *un_scaled_source;
238 YV12_BUFFER_CONFIG scaled_source;
239 YV12_BUFFER_CONFIG *unscaled_last_source;
240 YV12_BUFFER_CONFIG scaled_last_source;
242 // For a still frame, this flag is set to 1 to skip partition search.
243 int partition_search_skippable_frame;
245 int scaled_ref_idx[3];
250 int refresh_last_frame;
251 int refresh_golden_frame;
252 int refresh_alt_ref_frame;
254 int ext_refresh_frame_flags_pending;
255 int ext_refresh_last_frame;
256 int ext_refresh_golden_frame;
257 int ext_refresh_alt_ref_frame;
259 int ext_refresh_frame_context_pending;
260 int ext_refresh_frame_context;
262 YV12_BUFFER_CONFIG last_frame_uf;
265 unsigned int tok_count[4][1 << 6];
267 // Ambient reconstruction err target for force key frames
272 CODING_CONTEXT coding_context;
277 int *nmvsadcosts_hp[2];
280 int zbin_mode_boost_enabled;
282 int64_t last_time_stamp_seen;
283 int64_t last_end_time_stamp_seen;
284 int64_t first_time_stamp_ever;
289 vp9_coeff_count coef_counts[TX_SIZES][PLANE_TYPES];
290 int interp_filter_selected[MAX_REF_FRAMES][SWITCHABLE];
292 struct vpx_codec_pkt_list *output_pkt_list;
294 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
295 int mbgraph_n_frames; // number of frames filled in the above
296 int static_mb_pct; // % forced skip mbs by segmentation
301 unsigned int max_mv_magnitude;
304 // Default value is 1. From first pass stats, encode_breakout may be disabled.
305 ENCODE_BREAKOUT_TYPE allow_encode_breakout;
307 // Get threshold from external input. A suggested threshold is 800 for HD
308 // clips, and 300 for < HD clips.
311 unsigned char *segmentation_map;
313 // segment threashold for encode breakout
314 int segment_encode_breakout[MAX_SEGMENTS];
316 unsigned char *complexity_map;
318 CYCLIC_REFRESH *cyclic_refresh;
320 fractional_mv_step_fp *find_fractional_mv_step;
321 vp9_full_search_fn_t full_search_sad;
322 vp9_refining_search_fn_t refining_search_sad;
323 vp9_diamond_search_fn_t diamond_search_sad;
324 vp9_variance_fn_ptr_t fn_ptr[BLOCK_SIZES];
325 uint64_t time_receive_data;
326 uint64_t time_compress_data;
327 uint64_t time_pick_lpf;
328 uint64_t time_encode_sb_row;
330 #if CONFIG_FP_MB_STATS
336 YV12_BUFFER_CONFIG alt_ref_buffer;
339 #if CONFIG_INTERNAL_STATS
340 unsigned int mode_chosen_counts[MAX_MODES];
347 uint64_t total_sq_error;
348 uint64_t total_samples;
354 uint64_t totalp_sq_error;
355 uint64_t totalp_samples;
358 double summed_quality;
359 double summed_weights;
360 double summedp_quality;
361 double summedp_weights;
362 unsigned int tot_recode_hits;
365 double total_ssimg_y;
366 double total_ssimg_u;
367 double total_ssimg_v;
368 double total_ssimg_all;
370 int b_calculate_ssimg;
372 int b_calculate_psnr;
383 // Store frame variance info in SOURCE_VAR_BASED_PARTITION search type.
384 diff *source_diff_var;
385 // The threshold used in SOURCE_VAR_BASED_PARTITION search type.
386 unsigned int source_var_thresh;
387 int frames_till_next_var_check;
391 search_site_config ss_cfg;
393 int mbmode_cost[INTRA_MODES];
394 unsigned int inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES];
395 int intra_uv_mode_cost[FRAME_TYPES][INTRA_MODES];
396 int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
397 int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
399 PICK_MODE_CONTEXT *leaf_tree;
402 int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
404 int multi_arf_allowed;
405 int multi_arf_enabled;
406 int multi_arf_last_grp_enabled;
408 #if CONFIG_VP9_TEMPORAL_DENOISING
409 VP9_DENOISER denoiser;
413 void vp9_initialize_enc();
415 struct VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf);
416 void vp9_remove_compressor(VP9_COMP *cpi);
418 void vp9_change_config(VP9_COMP *cpi, const VP9EncoderConfig *oxcf);
420 // receive a frames worth of data. caller can assume that a copy of this
421 // frame is made and not just a copy of the pointer..
422 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
423 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
424 int64_t end_time_stamp);
426 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
427 size_t *size, uint8_t *dest,
428 int64_t *time_stamp, int64_t *time_end, int flush);
430 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
431 vp9_ppflags_t *flags);
433 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags);
435 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags);
437 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
438 YV12_BUFFER_CONFIG *sd);
440 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
441 YV12_BUFFER_CONFIG *sd);
443 int vp9_update_entropy(VP9_COMP *cpi, int update);
445 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols);
447 int vp9_set_internal_size(VP9_COMP *cpi,
448 VPX_SCALING horiz_mode, VPX_SCALING vert_mode);
450 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
451 unsigned int height);
453 void vp9_set_svc(VP9_COMP *cpi, int use_svc);
455 int vp9_get_quantizer(struct VP9_COMP *cpi);
457 static INLINE int get_ref_frame_idx(const VP9_COMP *cpi,
458 MV_REFERENCE_FRAME ref_frame) {
459 if (ref_frame == LAST_FRAME) {
460 return cpi->lst_fb_idx;
461 } else if (ref_frame == GOLDEN_FRAME) {
462 return cpi->gld_fb_idx;
464 return cpi->alt_fb_idx;
468 static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
469 VP9_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
470 VP9_COMMON * const cm = &cpi->common;
471 return &cm->frame_bufs[cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)]]
475 static INLINE int get_token_alloc(int mb_rows, int mb_cols) {
476 // TODO(JBB): double check we can't exceed this token count if we have a
477 // 32x32 transform crossing a boundary at a multiple of 16.
478 // mb_rows, cols are in units of 16 pixels. We assume 3 planes all at full
479 // resolution. We assume up to 1 token per pixel, and then allow
481 return mb_rows * mb_cols * (16 * 16 * 3 + 4);
484 // Get the allocated token size for a tile. It does the same calculation as in
485 // the frame token allocation.
486 static INLINE int allocated_tokens(TileInfo tile) {
487 int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 1) >> 1;
488 int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 1) >> 1;
490 return get_token_alloc(tile_mb_rows, tile_mb_cols);
493 int vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b);
494 #if CONFIG_VP9_HIGHBITDEPTH
495 int vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
496 const YV12_BUFFER_CONFIG *b,
497 vpx_bit_depth_t bit_depth);
498 #endif // CONFIG_VP9_HIGHBITDEPTH
500 void vp9_alloc_compressor_data(VP9_COMP *cpi);
502 void vp9_scale_references(VP9_COMP *cpi);
504 void vp9_update_reference_frames(VP9_COMP *cpi);
506 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv);
508 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
509 YV12_BUFFER_CONFIG *unscaled,
510 YV12_BUFFER_CONFIG *scaled);
512 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags);
514 static INLINE int is_two_pass_svc(const struct VP9_COMP *const cpi) {
515 return cpi->use_svc &&
516 (cpi->svc.number_temporal_layers > 1 ||
517 cpi->svc.number_spatial_layers > 1) &&
518 (cpi->oxcf.pass == 1 || cpi->oxcf.pass == 2);
521 static INLINE int is_altref_enabled(const VP9_COMP *const cpi) {
522 return cpi->oxcf.mode != REALTIME && cpi->oxcf.lag_in_frames > 0 &&
523 (cpi->oxcf.play_alternate &&
524 (!is_two_pass_svc(cpi) ||
525 cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id]));
528 static INLINE void set_ref_ptrs(VP9_COMMON *cm, MACROBLOCKD *xd,
529 MV_REFERENCE_FRAME ref0,
530 MV_REFERENCE_FRAME ref1) {
531 xd->block_refs[0] = &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME
533 xd->block_refs[1] = &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME
537 static INLINE int get_chessboard_index(const int frame_index) {
538 return frame_index & 0x1;
541 static INLINE int *cond_cost_list(const struct VP9_COMP *cpi, int *cost_list) {
542 return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
549 #endif // VP9_ENCODER_VP9_ENCODER_H_