@@ -703,11 +703,12 @@ impl RCState {
703703
704704 pub ( crate ) fn select_first_pass_qi (
705705 & self , bit_depth : usize , fti : usize , chroma_sampling : ChromaSampling ,
706+ ft_ratio : f64 ,
706707 ) -> QuantizerParameters {
707708 // Adjust the quantizer for the frame type, result is Q57:
708709 let log_q = ( ( self . pass1_log_base_q + ( 1i64 << 11 ) ) >> 12 )
709710 * ( MQP_Q12 [ fti] as i64 )
710- + DQP_Q57 [ fti] ;
711+ + ( DQP_Q57 [ fti] as f64 * ft_ratio ) as i64 ;
711712 QuantizerParameters :: new_from_log_q (
712713 self . pass1_log_base_q ,
713714 log_q,
@@ -723,14 +724,20 @@ impl RCState {
723724 & self , ctx : & ContextInner < T > , output_frameno : u64 , fti : usize ,
724725 maybe_prev_log_base_q : Option < i64 > , log_isqrt_mean_scale : i64 ,
725726 ) -> QuantizerParameters {
727+ let ft_ratio = ctx. config . advanced_flags . ip_qidx_ratio as f64 ;
728+
726729 // Is rate control active?
727730 if self . target_bitrate <= 0 {
728731 // Rate control is not active.
729732 // Derive quantizer directly from frame type.
730733 let bit_depth = ctx. config . bit_depth ;
731734 let chroma_sampling = ctx. config . chroma_sampling ;
732- let ( log_base_q, log_q) =
733- Self :: calc_flat_quantizer ( ctx. config . quantizer as u8 , bit_depth, fti) ;
735+ let ( log_base_q, log_q) = Self :: calc_flat_quantizer (
736+ ctx. config . quantizer as u8 ,
737+ bit_depth,
738+ fti,
739+ ft_ratio,
740+ ) ;
734741 QuantizerParameters :: new_from_log_q (
735742 log_base_q,
736743 log_q,
@@ -752,6 +759,7 @@ impl RCState {
752759 ctx. config . bit_depth ,
753760 fti,
754761 ctx. config . chroma_sampling ,
762+ ft_ratio,
755763 ) ;
756764 }
757765 // Second pass of 2-pass mode: we know exactly how much of each frame
@@ -925,7 +933,7 @@ impl RCState {
925933 // Modulate base quantizer by frame type.
926934 let log_q = ( ( log_base_q + ( 1i64 << 11 ) ) >> 12 )
927935 * ( MQP_Q12 [ ftj] as i64 )
928- + DQP_Q57 [ ftj] ;
936+ + ( DQP_Q57 [ ftj] as f64 * ft_ratio ) as i64 ;
929937 // All the fields here are Q57 except for the exponent, which is
930938 // Q6.
931939 bits += ( nframes[ ftj] as i64 )
@@ -959,7 +967,7 @@ impl RCState {
959967 // Modulate base quantizer by frame type.
960968 let mut log_q = ( ( log_base_q + ( 1i64 << 11 ) ) >> 12 )
961969 * ( MQP_Q12 [ fti] as i64 )
962- + DQP_Q57 [ fti] ;
970+ + ( DQP_Q57 [ fti] as f64 * ft_ratio ) as i64 ;
963971 // The above allocation looks only at the total rate we'll accumulate
964972 // in the next reservoir_frame_delay frames.
965973 // However, we could overflow the bit reservoir on the very next
@@ -1019,14 +1027,22 @@ impl RCState {
10191027 }
10201028
10211029 if let Some ( qi_max) = self . maybe_ac_qi_max {
1022- let ( max_log_base_q, max_log_q) =
1023- Self :: calc_flat_quantizer ( qi_max, ctx. config . bit_depth , fti) ;
1030+ let ( max_log_base_q, max_log_q) = Self :: calc_flat_quantizer (
1031+ qi_max,
1032+ ctx. config . bit_depth ,
1033+ fti,
1034+ ft_ratio,
1035+ ) ;
10241036 log_base_q = cmp:: min ( log_base_q, max_log_base_q) ;
10251037 log_q = cmp:: min ( log_q, max_log_q) ;
10261038 }
10271039 if self . ac_qi_min > 0 {
1028- let ( min_log_base_q, min_log_q) =
1029- Self :: calc_flat_quantizer ( self . ac_qi_min , ctx. config . bit_depth , fti) ;
1040+ let ( min_log_base_q, min_log_q) = Self :: calc_flat_quantizer (
1041+ self . ac_qi_min ,
1042+ ctx. config . bit_depth ,
1043+ fti,
1044+ ft_ratio,
1045+ ) ;
10301046 log_base_q = cmp:: max ( log_base_q, min_log_base_q) ;
10311047 log_q = cmp:: max ( log_q, min_log_q) ;
10321048 }
@@ -1044,7 +1060,7 @@ impl RCState {
10441060 // Computes a quantizer directly from the frame type and base quantizer index,
10451061 // without consideration for rate control.
10461062 fn calc_flat_quantizer (
1047- base_qi : u8 , bit_depth : usize , fti : usize ,
1063+ base_qi : u8 , bit_depth : usize , fti : usize , ft_ratio : f64 ,
10481064 ) -> ( i64 , i64 ) {
10491065 // TODO: Rename "quantizer" something that indicates it is a quantizer
10501066 // index, and move it somewhere more sensible (or choose a better way to
@@ -1063,7 +1079,7 @@ impl RCState {
10631079 let log_base_q = ( log_ac_q + log_dc_q + 1 ) >> 1 ;
10641080 // Adjust the quantizer for the frame type, result is Q57:
10651081 let log_q = ( ( log_base_q + ( 1i64 << 11 ) ) >> 12 ) * ( MQP_Q12 [ fti] as i64 )
1066- + DQP_Q57 [ fti] ;
1082+ + ( DQP_Q57 [ fti] as f64 * ft_ratio ) as i64 ;
10671083 ( log_base_q, log_q)
10681084 }
10691085
0 commit comments