4040
4141
4242static const int MIN_SMOOTHING_WINDOW = 40 ;
43- static int smoothing_window = 40 ;
4443static const double MIN_LAMBDA = 0.1 ;
4544static const double MAX_LAMBDA = 10000 ;
4645#define BETA1 1.2517
4746
48- static kvz_rc_data * data ;
49-
50- static FILE * dist_file ;
51- static FILE * bits_file ;
52- static FILE * qp_file ;
53- static FILE * lambda_file ;
5447
5548/**
5649 * \brief Clip lambda value to a valid range.
@@ -61,9 +54,7 @@ static double clip_lambda(double lambda) {
6154}
6255
6356kvz_rc_data * kvz_get_rc_data (const encoder_control_t * const encoder ) {
64- if (data != NULL || encoder == NULL ) return data ;
65-
66- data = calloc (1 , sizeof (kvz_rc_data ));
57+ kvz_rc_data * data = calloc (1 , sizeof (kvz_rc_data ));
6758
6859 if (data == NULL ) return NULL ;
6960 if (pthread_mutex_init (& data -> ck_frame_lock , NULL ) != 0 ) return NULL ;
@@ -107,18 +98,21 @@ kvz_rc_data * kvz_get_rc_data(const encoder_control_t * const encoder) {
10798 if (encoder -> cfg .stats_file_prefix ) {
10899 char buff [128 ];
109100 sprintf (buff , "%sbits.txt" , encoder -> cfg .stats_file_prefix );
110- bits_file = fopen (buff , "w" );
101+ data -> bits_file = fopen (buff , "w" );
111102 sprintf (buff , "%sdist.txt" , encoder -> cfg .stats_file_prefix );
112- dist_file = fopen (buff , "w" );
103+ data -> dist_file = fopen (buff , "w" );
113104 sprintf (buff , "%sqp.txt" , encoder -> cfg .stats_file_prefix );
114- qp_file = fopen (buff , "w" );
105+ data -> qp_file = fopen (buff , "w" );
115106 sprintf (buff , "%slambda.txt" , encoder -> cfg .stats_file_prefix );
116- lambda_file = fopen (buff , "w" );
107+ data -> lambda_file = fopen (buff , "w" );
117108 }
109+
110+ data -> smoothing_window = MIN_SMOOTHING_WINDOW ;
111+
118112 return data ;
119113}
120114
121- void kvz_free_rc_data () {
115+ void kvz_free_rc_data (kvz_rc_data * data ) {
122116 if (data == NULL ) return ;
123117
124118 pthread_mutex_destroy (& data -> ck_frame_lock );
@@ -189,16 +183,16 @@ static double gop_allocate_bits(encoder_state_t * const state)
189183 bits_coded -= state -> frame -> cur_gop_bits_coded ;
190184 }
191185
192- smoothing_window = MAX (MIN_SMOOTHING_WINDOW , smoothing_window - MAX (encoder -> cfg .gop_len / 2 , 1 ));
186+ state -> frame -> new_ratecontrol -> smoothing_window = MAX (MIN_SMOOTHING_WINDOW , state -> frame -> new_ratecontrol -> smoothing_window - MAX (encoder -> cfg .gop_len / 2 , 1 ));
193187 double gop_target_bits = -1 ;
194188
195- while ( gop_target_bits < 0 && smoothing_window < 150 ) {
189+ while ( gop_target_bits < 0 && state -> frame -> new_ratecontrol -> smoothing_window < 150 ) {
196190 // Equation 12 from https://doi.org/10.1109/TIP.2014.2336550
197191 gop_target_bits =
198- (encoder -> target_avg_bppic * (pictures_coded + smoothing_window ) - bits_coded )
199- * MAX (1 , encoder -> cfg .gop_len ) / smoothing_window ;
192+ (encoder -> target_avg_bppic * (pictures_coded + state -> frame -> new_ratecontrol -> smoothing_window ) - bits_coded )
193+ * MAX (1 , encoder -> cfg .gop_len ) / state -> frame -> new_ratecontrol -> smoothing_window ;
200194 if (gop_target_bits < 0 ) {
201- smoothing_window += 10 ;
195+ state -> frame -> new_ratecontrol -> smoothing_window += 10 ;
202196 }
203197 }
204198 // Allocate at least 200 bits for each GOP like HM does.
@@ -924,10 +918,10 @@ void kvz_update_after_picture(encoder_state_t * const state) {
924918
925919 if (encoder -> cfg .stats_file_prefix ) {
926920 int poc = calc_poc (state );
927- fprintf (dist_file , "%d %d %d\n" , poc , encoder -> in .width_in_lcu , encoder -> in .height_in_lcu );
928- fprintf (bits_file , "%d %d %d\n" , poc , encoder -> in .width_in_lcu , encoder -> in .height_in_lcu );
929- fprintf (qp_file , "%d %d %d\n" , poc , encoder -> in .width_in_lcu , encoder -> in .height_in_lcu );
930- fprintf (lambda_file , "%d %d %d\n" , poc , encoder -> in .width_in_lcu , encoder -> in .height_in_lcu );
921+ fprintf (state -> frame -> new_ratecontrol -> dist_file , "%d %d %d\n" , poc , encoder -> in .width_in_lcu , encoder -> in .height_in_lcu );
922+ fprintf (state -> frame -> new_ratecontrol -> bits_file , "%d %d %d\n" , poc , encoder -> in .width_in_lcu , encoder -> in .height_in_lcu );
923+ fprintf (state -> frame -> new_ratecontrol -> qp_file , "%d %d %d\n" , poc , encoder -> in .width_in_lcu , encoder -> in .height_in_lcu );
924+ fprintf (state -> frame -> new_ratecontrol -> lambda_file , "%d %d %d\n" , poc , encoder -> in .width_in_lcu , encoder -> in .height_in_lcu );
931925 }
932926
933927 for (int y_ctu = 0 ; y_ctu < state -> encoder_control -> in .height_in_lcu ; y_ctu ++ ) {
@@ -945,17 +939,17 @@ void kvz_update_after_picture(encoder_state_t * const state) {
945939 total_distortion += (double )ctu_distortion / ctu -> pixels ;
946940 lambda += ctu -> lambda / (state -> encoder_control -> in .width_in_lcu * state -> encoder_control -> in .height_in_lcu );
947941 if (encoder -> cfg .stats_file_prefix ) {
948- fprintf (dist_file , "%f " , ctu -> distortion );
949- fprintf (bits_file , "%d " , ctu -> bits );
950- fprintf (qp_file , "%d " , ctu -> adjust_qp ? ctu -> adjust_qp : ctu -> qp );
951- fprintf (lambda_file , "%f " , ctu -> adjust_lambda ? ctu -> adjust_lambda : ctu -> lambda );
942+ fprintf (state -> frame -> new_ratecontrol -> dist_file , "%f " , ctu -> distortion );
943+ fprintf (state -> frame -> new_ratecontrol -> bits_file , "%d " , ctu -> bits );
944+ fprintf (state -> frame -> new_ratecontrol -> qp_file , "%d " , ctu -> adjust_qp ? ctu -> adjust_qp : ctu -> qp );
945+ fprintf (state -> frame -> new_ratecontrol -> lambda_file , "%f " , ctu -> adjust_lambda ? ctu -> adjust_lambda : ctu -> lambda );
952946 }
953947 }
954948 if (encoder -> cfg .stats_file_prefix ) {
955- fprintf (dist_file , "\n" );
956- fprintf (bits_file , "\n" );
957- fprintf (qp_file , "\n" );
958- fprintf (lambda_file , "\n" );
949+ fprintf (state -> frame -> new_ratecontrol -> dist_file , "\n" );
950+ fprintf (state -> frame -> new_ratecontrol -> bits_file , "\n" );
951+ fprintf (state -> frame -> new_ratecontrol -> qp_file , "\n" );
952+ fprintf (state -> frame -> new_ratecontrol -> lambda_file , "\n" );
959953 }
960954 }
961955
0 commit comments