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_FIRSTPASS_H_
12 #define VP9_ENCODER_VP9_FIRSTPASS_H_
13 
14 #include "vp9/encoder/vp9_lookahead.h"
15 #include "vp9/encoder/vp9_ratectrl.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #if CONFIG_FP_MB_STATS
22 
23 #define FPMB_DCINTRA_MASK 0x01
24 
25 #define FPMB_MOTION_ZERO_MASK 0x02
26 #define FPMB_MOTION_LEFT_MASK 0x04
27 #define FPMB_MOTION_RIGHT_MASK 0x08
28 #define FPMB_MOTION_UP_MASK 0x10
29 #define FPMB_MOTION_DOWN_MASK 0x20
30 
31 #define FPMB_ERROR_SMALL_MASK 0x40
32 #define FPMB_ERROR_LARGE_MASK 0x80
33 #define FPMB_ERROR_SMALL_TH 2000
34 #define FPMB_ERROR_LARGE_TH 48000
35 
36 typedef struct {
37   uint8_t *mb_stats_start;
38   uint8_t *mb_stats_end;
39 } FIRSTPASS_MB_STATS;
40 #endif
41 
42 typedef struct {
43   double frame;
44   double intra_error;
45   double coded_error;
46   double sr_coded_error;
47   double pcnt_inter;
48   double pcnt_motion;
49   double pcnt_second_ref;
50   double pcnt_neutral;
51   double MVr;
52   double mvr_abs;
53   double MVc;
54   double mvc_abs;
55   double MVrv;
56   double MVcv;
57   double mv_in_out_count;
58   double new_mv_count;
59   double duration;
60   double count;
61   int64_t spatial_layer_id;
62 } FIRSTPASS_STATS;
63 
64 typedef enum {
65   KF_UPDATE = 0,
66   LF_UPDATE = 1,
67   GF_UPDATE = 2,
68   ARF_UPDATE = 3,
69   OVERLAY_UPDATE = 4,
70   FRAME_UPDATE_TYPES = 5
71 } FRAME_UPDATE_TYPE;
72 
73 typedef struct {
74   unsigned char index;
75   RATE_FACTOR_LEVEL rf_level[(MAX_LAG_BUFFERS * 2) + 1];
76   FRAME_UPDATE_TYPE update_type[(MAX_LAG_BUFFERS * 2) + 1];
77   unsigned char arf_src_offset[(MAX_LAG_BUFFERS * 2) + 1];
78   unsigned char arf_update_idx[(MAX_LAG_BUFFERS * 2) + 1];
79   unsigned char arf_ref_idx[(MAX_LAG_BUFFERS * 2) + 1];
80   int bit_allocation[(MAX_LAG_BUFFERS * 2) + 1];
81 } GF_GROUP;
82 
83 typedef struct {
84   unsigned int section_intra_rating;
85   FIRSTPASS_STATS total_stats;
86   FIRSTPASS_STATS this_frame_stats;
87   const FIRSTPASS_STATS *stats_in;
88   const FIRSTPASS_STATS *stats_in_start;
89   const FIRSTPASS_STATS *stats_in_end;
90   FIRSTPASS_STATS total_left_stats;
91   int first_pass_done;
92   int64_t bits_left;
93   double modified_error_min;
94   double modified_error_max;
95   double modified_error_left;
96   double kf_intra_err_min;
97   double gf_intra_err_min;
98 
99 #if CONFIG_FP_MB_STATS
100   uint8_t *frame_mb_stats_buf;
101   uint8_t *this_frame_mb_stats;
102   FIRSTPASS_MB_STATS firstpass_mb_stats;
103 #endif
104 
105   // Projected total bits available for a key frame group of frames
106   int64_t kf_group_bits;
107 
108   // Error score of frames still to be coded in kf group
109   int64_t kf_group_error_left;
110   int sr_update_lag;
111 
112   int kf_zeromotion_pct;
113   int gf_zeromotion_pct;
114 
115   int active_worst_quality;
116 
117   GF_GROUP gf_group;
118 } TWO_PASS;
119 
120 struct VP9_COMP;
121 
122 void vp9_init_first_pass(struct VP9_COMP *cpi);
123 void vp9_rc_get_first_pass_params(struct VP9_COMP *cpi);
124 void vp9_first_pass(struct VP9_COMP *cpi);
125 void vp9_end_first_pass(struct VP9_COMP *cpi);
126 
127 void vp9_init_second_pass(struct VP9_COMP *cpi);
128 void vp9_rc_get_second_pass_params(struct VP9_COMP *cpi);
129 
130 // Post encode update of the rate control parameters for 2-pass
131 void vp9_twopass_postencode_update(struct VP9_COMP *cpi);
132 #ifdef __cplusplus
133 }  // extern "C"
134 #endif
135 
136 #endif  // VP9_ENCODER_VP9_FIRSTPASS_H_
137