Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_encoder.h
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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  */
10
11 #ifndef VP9_ENCODER_VP9_ENCODER_H_
12 #define VP9_ENCODER_VP9_ENCODER_H_
13
14 #include <stdio.h>
15
16 #include "./vpx_config.h"
17 #include "vpx/internal/vpx_codec_internal.h"
18 #include "vpx/vp8cx.h"
19
20 #include "vp9/common/vp9_ppflags.h"
21 #include "vp9/common/vp9_entropymode.h"
22 #include "vp9/common/vp9_onyxc_int.h"
23
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"
40 #endif
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 #define DEFAULT_GF_INTERVAL         10
47
48 typedef struct {
49   int nmvjointcost[MV_JOINTS];
50   int nmvcosts[2][MV_VALS];
51   int nmvcosts_hp[2][MV_VALS];
52
53   vp9_prob segment_pred_probs[PREDICTION_PROBS];
54
55   unsigned char *last_frame_seg_map_copy;
56
57   // 0 = Intra, Last, GF, ARF
58   signed char last_ref_lf_deltas[MAX_REF_LF_DELTAS];
59   // 0 = ZERO_MV, MV
60   signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
61
62   FRAME_CONTEXT fc;
63 } CODING_CONTEXT;
64
65
66 typedef enum {
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;
74
75 typedef enum {
76   NORMAL      = 0,
77   FOURFIVE    = 1,
78   THREEFIVE   = 2,
79   ONETWO      = 3
80 } VPX_SCALING;
81
82 typedef enum {
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.
85   GOOD,
86
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.
90   BEST,
91
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.
95   REALTIME
96 } MODE;
97
98 typedef enum {
99   FRAMEFLAGS_KEY    = 1 << 0,
100   FRAMEFLAGS_GOLDEN = 1 << 1,
101   FRAMEFLAGS_ALTREF = 1 << 2,
102 } FRAMETYPE_FLAGS;
103
104 typedef enum {
105   NO_AQ = 0,
106   VARIANCE_AQ = 1,
107   COMPLEXITY_AQ = 2,
108   CYCLIC_REFRESH_AQ = 3,
109   AQ_MODE_COUNT  // This should always be the last member of the enum
110 } AQ_MODE;
111
112
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
121
122   int noise_sensitivity;  // pre processing blur: recommendation 0
123   int sharpness;  // sharpening output: recommendation 0:
124   int speed;
125   unsigned int rc_max_intra_bitrate_pct;
126
127   MODE mode;
128   int pass;
129
130   // Key Framing Operations
131   int auto_key;  // autodetect cut scenes and set the keyframes
132   int key_freq;  // maximum distance to key frame.
133
134   int lag_in_frames;  // how many frames lag before we start encoding
135
136   // ----------------------------------------------------------------
137   // DATARATE CONTROL OPTIONS
138
139   // vbr, cbr, constrained quality or constant quality
140   enum vpx_rc_mode rc_mode;
141
142   // buffer targeting aggressiveness
143   int under_shoot_pct;
144   int over_shoot_pct;
145
146   // buffering parameters
147   int64_t starting_buffer_level_ms;
148   int64_t optimal_buffer_level_ms;
149   int64_t maximum_buffer_size_ms;
150
151   // Frame drop threshold.
152   int drop_frames_water_mark;
153
154   // controlling quality
155   int fixed_q;
156   int worst_allowed_q;
157   int best_allowed_q;
158   int cq_level;
159   AQ_MODE aq_mode;  // Adaptive Quantization mode
160
161   // Internal frame size scaling.
162   int allow_spatial_resampling;
163   int scaled_frame_width;
164   int scaled_frame_height;
165
166   // Enable feature to reduce the frame quantization every x frames.
167   int frame_periodic_boost;
168
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   // ----------------------------------------------------------------
175
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];
185
186   // these parameters aren't to be used in final build don't use!!!
187   int play_alternate;
188
189   int encode_breakout;  // early breakout : for video conf recommend 800
190
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.
194    */
195   unsigned int error_resilient_mode;
196
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.
200    */
201   unsigned int frame_parallel_decoding_mode;
202
203   int arnr_max_frames;
204   int arnr_strength;
205
206   int tile_columns;
207   int tile_rows;
208
209   vpx_fixed_buf_t two_pass_stats_in;
210   struct vpx_codec_pkt_list *output_pkt_list;
211
212 #if CONFIG_FP_MB_STATS
213   vpx_fixed_buf_t firstpass_mb_stats_in;
214 #endif
215
216   vp8e_tuning tuning;
217   vp9e_tune_content content;
218 #if CONFIG_VP9_HIGHBITDEPTH
219   int use_highbitdepth;
220 #endif
221 } VP9EncoderConfig;
222
223 static INLINE int is_lossless_requested(const VP9EncoderConfig *cfg) {
224   return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
225 }
226
227 typedef struct VP9_COMP {
228   QUANTS quants;
229   MACROBLOCK mb;
230   VP9_COMMON common;
231   VP9EncoderConfig oxcf;
232   struct lookahead_ctx    *lookahead;
233   struct lookahead_entry  *alt_ref_source;
234
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;
241
242   // For a still frame, this flag is set to 1 to skip partition search.
243   int partition_search_skippable_frame;
244
245   int scaled_ref_idx[3];
246   int lst_fb_idx;
247   int gld_fb_idx;
248   int alt_fb_idx;
249
250   int refresh_last_frame;
251   int refresh_golden_frame;
252   int refresh_alt_ref_frame;
253
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;
258
259   int ext_refresh_frame_context_pending;
260   int ext_refresh_frame_context;
261
262   YV12_BUFFER_CONFIG last_frame_uf;
263
264   TOKENEXTRA *tok;
265   unsigned int tok_count[4][1 << 6];
266
267   // Ambient reconstruction err target for force key frames
268   int ambient_err;
269
270   RD_OPT rd;
271
272   CODING_CONTEXT coding_context;
273
274   int *nmvcosts[2];
275   int *nmvcosts_hp[2];
276   int *nmvsadcosts[2];
277   int *nmvsadcosts_hp[2];
278
279   int zbin_mode_boost;
280   int zbin_mode_boost_enabled;
281
282   int64_t last_time_stamp_seen;
283   int64_t last_end_time_stamp_seen;
284   int64_t first_time_stamp_ever;
285
286   RATE_CONTROL rc;
287   double framerate;
288
289   vp9_coeff_count coef_counts[TX_SIZES][PLANE_TYPES];
290   int interp_filter_selected[MAX_REF_FRAMES][SWITCHABLE];
291
292   struct vpx_codec_pkt_list  *output_pkt_list;
293
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
297   int ref_frame_flags;
298
299   SPEED_FEATURES sf;
300
301   unsigned int max_mv_magnitude;
302   int mv_step_param;
303
304   // Default value is 1. From first pass stats, encode_breakout may be disabled.
305   ENCODE_BREAKOUT_TYPE allow_encode_breakout;
306
307   // Get threshold from external input. A suggested threshold is 800 for HD
308   // clips, and 300 for < HD clips.
309   int encode_breakout;
310
311   unsigned char *segmentation_map;
312
313   // segment threashold for encode breakout
314   int  segment_encode_breakout[MAX_SEGMENTS];
315
316   unsigned char *complexity_map;
317
318   CYCLIC_REFRESH *cyclic_refresh;
319
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;
329
330 #if CONFIG_FP_MB_STATS
331   int use_fp_mb_stats;
332 #endif
333
334   TWO_PASS twopass;
335
336   YV12_BUFFER_CONFIG alt_ref_buffer;
337
338
339 #if CONFIG_INTERNAL_STATS
340   unsigned int mode_chosen_counts[MAX_MODES];
341
342   int    count;
343   double total_y;
344   double total_u;
345   double total_v;
346   double total;
347   uint64_t total_sq_error;
348   uint64_t total_samples;
349
350   double totalp_y;
351   double totalp_u;
352   double totalp_v;
353   double totalp;
354   uint64_t totalp_sq_error;
355   uint64_t totalp_samples;
356
357   int    bytes;
358   double summed_quality;
359   double summed_weights;
360   double summedp_quality;
361   double summedp_weights;
362   unsigned int tot_recode_hits;
363
364
365   double total_ssimg_y;
366   double total_ssimg_u;
367   double total_ssimg_v;
368   double total_ssimg_all;
369
370   int b_calculate_ssimg;
371 #endif
372   int b_calculate_psnr;
373
374   int droppable;
375
376   int initial_width;
377   int initial_height;
378
379   int use_svc;
380
381   SVC svc;
382
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;
388
389   int frame_flags;
390
391   search_site_config ss_cfg;
392
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];
398
399   PICK_MODE_CONTEXT *leaf_tree;
400   PC_TREE *pc_tree;
401   PC_TREE *pc_root;
402   int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
403
404   int multi_arf_allowed;
405   int multi_arf_enabled;
406   int multi_arf_last_grp_enabled;
407
408 #if CONFIG_VP9_TEMPORAL_DENOISING
409   VP9_DENOISER denoiser;
410 #endif
411 } VP9_COMP;
412
413 void vp9_initialize_enc();
414
415 struct VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf);
416 void vp9_remove_compressor(VP9_COMP *cpi);
417
418 void vp9_change_config(VP9_COMP *cpi, const VP9EncoderConfig *oxcf);
419
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);
425
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);
429
430 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
431                               vp9_ppflags_t *flags);
432
433 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags);
434
435 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags);
436
437 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
438                            YV12_BUFFER_CONFIG *sd);
439
440 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
441                           YV12_BUFFER_CONFIG *sd);
442
443 int vp9_update_entropy(VP9_COMP *cpi, int update);
444
445 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols);
446
447 int vp9_set_internal_size(VP9_COMP *cpi,
448                           VPX_SCALING horiz_mode, VPX_SCALING vert_mode);
449
450 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
451                          unsigned int height);
452
453 void vp9_set_svc(VP9_COMP *cpi, int use_svc);
454
455 int vp9_get_quantizer(struct VP9_COMP *cpi);
456
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;
463   } else {
464     return cpi->alt_fb_idx;
465   }
466 }
467
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)]]
472       .buf;
473 }
474
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
480   // a head room of 4.
481   return mb_rows * mb_cols * (16 * 16 * 3 + 4);
482 }
483
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;
489
490   return get_token_alloc(tile_mb_rows, tile_mb_cols);
491 }
492
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
499
500 void vp9_alloc_compressor_data(VP9_COMP *cpi);
501
502 void vp9_scale_references(VP9_COMP *cpi);
503
504 void vp9_update_reference_frames(VP9_COMP *cpi);
505
506 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv);
507
508 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
509                                           YV12_BUFFER_CONFIG *unscaled,
510                                           YV12_BUFFER_CONFIG *scaled);
511
512 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags);
513
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);
519 }
520
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]));
526 }
527
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
532                                                          : 0];
533   xd->block_refs[1] = &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME
534                                                          : 0];
535 }
536
537 static INLINE int get_chessboard_index(const int frame_index) {
538   return frame_index & 0x1;
539 }
540
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;
543 }
544
545 #ifdef __cplusplus
546 }  // extern "C"
547 #endif
548
549 #endif  // VP9_ENCODER_VP9_ENCODER_H_