vp9_firstpass.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #ifndef VPX_VP9_ENCODER_VP9_FIRSTPASS_H_
  11. #define VPX_VP9_ENCODER_VP9_FIRSTPASS_H_
  12. #include <assert.h>
  13. #include "vp9/encoder/vp9_lookahead.h"
  14. #include "vp9/encoder/vp9_ratectrl.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #if CONFIG_FP_MB_STATS
  19. #define FPMB_DCINTRA_MASK 0x01
  20. #define FPMB_MOTION_ZERO_MASK 0x02
  21. #define FPMB_MOTION_LEFT_MASK 0x04
  22. #define FPMB_MOTION_RIGHT_MASK 0x08
  23. #define FPMB_MOTION_UP_MASK 0x10
  24. #define FPMB_MOTION_DOWN_MASK 0x20
  25. #define FPMB_ERROR_SMALL_MASK 0x40
  26. #define FPMB_ERROR_LARGE_MASK 0x80
  27. #define FPMB_ERROR_SMALL_TH 2000
  28. #define FPMB_ERROR_LARGE_TH 48000
  29. typedef struct {
  30. uint8_t *mb_stats_start;
  31. uint8_t *mb_stats_end;
  32. } FIRSTPASS_MB_STATS;
  33. #endif
  34. #define INVALID_ROW (-1)
  35. #define MAX_ARF_LAYERS 6
  36. #define SECTION_NOISE_DEF 250.0
  37. typedef struct {
  38. double frame_mb_intra_factor;
  39. double frame_mb_brightness_factor;
  40. double frame_mb_neutral_count;
  41. } FP_MB_FLOAT_STATS;
  42. typedef struct {
  43. double intra_factor;
  44. double brightness_factor;
  45. int64_t coded_error;
  46. int64_t sr_coded_error;
  47. int64_t frame_noise_energy;
  48. int64_t intra_error;
  49. int intercount;
  50. int second_ref_count;
  51. double neutral_count;
  52. double intra_count_low; // Coded intra but low variance
  53. double intra_count_high; // Coded intra high variance
  54. int intra_skip_count;
  55. int image_data_start_row;
  56. int mvcount;
  57. int sum_mvr;
  58. int sum_mvr_abs;
  59. int sum_mvc;
  60. int sum_mvc_abs;
  61. int64_t sum_mvrs;
  62. int64_t sum_mvcs;
  63. int sum_in_vectors;
  64. int intra_smooth_count;
  65. } FIRSTPASS_DATA;
  66. typedef struct {
  67. double frame;
  68. double weight;
  69. double intra_error;
  70. double coded_error;
  71. double sr_coded_error;
  72. double frame_noise_energy;
  73. double pcnt_inter;
  74. double pcnt_motion;
  75. double pcnt_second_ref;
  76. double pcnt_neutral;
  77. double pcnt_intra_low; // Coded intra but low variance
  78. double pcnt_intra_high; // Coded intra high variance
  79. double intra_skip_pct;
  80. double intra_smooth_pct; // % of blocks that are smooth
  81. double inactive_zone_rows; // Image mask rows top and bottom.
  82. double inactive_zone_cols; // Image mask columns at left and right edges.
  83. double MVr;
  84. double mvr_abs;
  85. double MVc;
  86. double mvc_abs;
  87. double MVrv;
  88. double MVcv;
  89. double mv_in_out_count;
  90. double duration;
  91. double count;
  92. int64_t spatial_layer_id;
  93. } FIRSTPASS_STATS;
  94. typedef enum {
  95. KF_UPDATE = 0,
  96. LF_UPDATE = 1,
  97. GF_UPDATE = 2,
  98. ARF_UPDATE = 3,
  99. OVERLAY_UPDATE = 4,
  100. MID_OVERLAY_UPDATE = 5,
  101. USE_BUF_FRAME = 6, // Use show existing frame, no ref buffer update
  102. FRAME_UPDATE_TYPES = 7
  103. } FRAME_UPDATE_TYPE;
  104. #define FC_ANIMATION_THRESH 0.15
  105. typedef enum {
  106. FC_NORMAL = 0,
  107. FC_GRAPHICS_ANIMATION = 1,
  108. FRAME_CONTENT_TYPES = 2
  109. } FRAME_CONTENT_TYPE;
  110. typedef struct {
  111. unsigned char index;
  112. RATE_FACTOR_LEVEL rf_level[MAX_STATIC_GF_GROUP_LENGTH + 2];
  113. FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH + 2];
  114. unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH + 2];
  115. unsigned char layer_depth[MAX_STATIC_GF_GROUP_LENGTH + 2];
  116. unsigned char frame_gop_index[MAX_STATIC_GF_GROUP_LENGTH + 2];
  117. int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH + 2];
  118. int gfu_boost[MAX_STATIC_GF_GROUP_LENGTH + 2];
  119. int frame_start;
  120. int frame_end;
  121. // TODO(jingning): The array size of arf_stack could be reduced.
  122. int arf_index_stack[MAX_LAG_BUFFERS * 2];
  123. int top_arf_idx;
  124. int stack_size;
  125. int gf_group_size;
  126. int max_layer_depth;
  127. int allowed_max_layer_depth;
  128. int group_noise_energy;
  129. } GF_GROUP;
  130. typedef struct {
  131. unsigned int section_intra_rating;
  132. unsigned int key_frame_section_intra_rating;
  133. FIRSTPASS_STATS total_stats;
  134. FIRSTPASS_STATS this_frame_stats;
  135. const FIRSTPASS_STATS *stats_in;
  136. const FIRSTPASS_STATS *stats_in_start;
  137. const FIRSTPASS_STATS *stats_in_end;
  138. FIRSTPASS_STATS total_left_stats;
  139. int first_pass_done;
  140. int64_t bits_left;
  141. double mean_mod_score;
  142. double normalized_score_left;
  143. double mb_av_energy;
  144. double mb_smooth_pct;
  145. #if CONFIG_FP_MB_STATS
  146. uint8_t *frame_mb_stats_buf;
  147. uint8_t *this_frame_mb_stats;
  148. FIRSTPASS_MB_STATS firstpass_mb_stats;
  149. #endif
  150. FP_MB_FLOAT_STATS *fp_mb_float_stats;
  151. // An indication of the content type of the current frame
  152. FRAME_CONTENT_TYPE fr_content_type;
  153. // Projected total bits available for a key frame group of frames
  154. int64_t kf_group_bits;
  155. // Error score of frames still to be coded in kf group
  156. double kf_group_error_left;
  157. double bpm_factor;
  158. int rolling_arf_group_target_bits;
  159. int rolling_arf_group_actual_bits;
  160. int sr_update_lag;
  161. int kf_zeromotion_pct;
  162. int last_kfgroup_zeromotion_pct;
  163. int active_worst_quality;
  164. int baseline_active_worst_quality;
  165. int extend_minq;
  166. int extend_maxq;
  167. int extend_minq_fast;
  168. int arnr_strength_adjustment;
  169. GF_GROUP gf_group;
  170. } TWO_PASS;
  171. struct VP9_COMP;
  172. struct ThreadData;
  173. struct TileDataEnc;
  174. void vp9_init_first_pass(struct VP9_COMP *cpi);
  175. void vp9_first_pass(struct VP9_COMP *cpi, const struct lookahead_entry *source);
  176. void vp9_end_first_pass(struct VP9_COMP *cpi);
  177. void vp9_first_pass_encode_tile_mb_row(struct VP9_COMP *cpi,
  178. struct ThreadData *td,
  179. FIRSTPASS_DATA *fp_acc_data,
  180. struct TileDataEnc *tile_data,
  181. MV *best_ref_mv, int mb_row);
  182. void vp9_init_second_pass(struct VP9_COMP *cpi);
  183. void vp9_rc_get_second_pass_params(struct VP9_COMP *cpi);
  184. // Post encode update of the rate control parameters for 2-pass
  185. void vp9_twopass_postencode_update(struct VP9_COMP *cpi);
  186. void calculate_coded_size(struct VP9_COMP *cpi, int *scaled_frame_width,
  187. int *scaled_frame_height);
  188. #ifdef __cplusplus
  189. } // extern "C"
  190. #endif
  191. #endif // VPX_VP9_ENCODER_VP9_FIRSTPASS_H_