@@ -703,11 +703,12 @@ impl RCState {
703
703
704
704
pub ( crate ) fn select_first_pass_qi (
705
705
& self , bit_depth : usize , fti : usize , chroma_sampling : ChromaSampling ,
706
+ ft_ratio : f64 ,
706
707
) -> QuantizerParameters {
707
708
// Adjust the quantizer for the frame type, result is Q57:
708
709
let log_q = ( ( self . pass1_log_base_q + ( 1i64 << 11 ) ) >> 12 )
709
710
* ( MQP_Q12 [ fti] as i64 )
710
- + DQP_Q57 [ fti] ;
711
+ + ( DQP_Q57 [ fti] as f64 * ft_ratio ) as i64 ;
711
712
QuantizerParameters :: new_from_log_q (
712
713
self . pass1_log_base_q ,
713
714
log_q,
@@ -723,14 +724,20 @@ impl RCState {
723
724
& self , ctx : & ContextInner < T > , output_frameno : u64 , fti : usize ,
724
725
maybe_prev_log_base_q : Option < i64 > , log_isqrt_mean_scale : i64 ,
725
726
) -> QuantizerParameters {
727
+ let ft_ratio = ctx. config . advanced_flags . ip_qidx_ratio as f64 ;
728
+
726
729
// Is rate control active?
727
730
if self . target_bitrate <= 0 {
728
731
// Rate control is not active.
729
732
// Derive quantizer directly from frame type.
730
733
let bit_depth = ctx. config . bit_depth ;
731
734
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
+ ) ;
734
741
QuantizerParameters :: new_from_log_q (
735
742
log_base_q,
736
743
log_q,
@@ -752,6 +759,7 @@ impl RCState {
752
759
ctx. config . bit_depth ,
753
760
fti,
754
761
ctx. config . chroma_sampling ,
762
+ ft_ratio,
755
763
) ;
756
764
}
757
765
// Second pass of 2-pass mode: we know exactly how much of each frame
@@ -925,7 +933,7 @@ impl RCState {
925
933
// Modulate base quantizer by frame type.
926
934
let log_q = ( ( log_base_q + ( 1i64 << 11 ) ) >> 12 )
927
935
* ( MQP_Q12 [ ftj] as i64 )
928
- + DQP_Q57 [ ftj] ;
936
+ + ( DQP_Q57 [ ftj] as f64 * ft_ratio ) as i64 ;
929
937
// All the fields here are Q57 except for the exponent, which is
930
938
// Q6.
931
939
bits += ( nframes[ ftj] as i64 )
@@ -959,7 +967,7 @@ impl RCState {
959
967
// Modulate base quantizer by frame type.
960
968
let mut log_q = ( ( log_base_q + ( 1i64 << 11 ) ) >> 12 )
961
969
* ( MQP_Q12 [ fti] as i64 )
962
- + DQP_Q57 [ fti] ;
970
+ + ( DQP_Q57 [ fti] as f64 * ft_ratio ) as i64 ;
963
971
// The above allocation looks only at the total rate we'll accumulate
964
972
// in the next reservoir_frame_delay frames.
965
973
// However, we could overflow the bit reservoir on the very next
@@ -1019,14 +1027,22 @@ impl RCState {
1019
1027
}
1020
1028
1021
1029
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
+ ) ;
1024
1036
log_base_q = cmp:: min ( log_base_q, max_log_base_q) ;
1025
1037
log_q = cmp:: min ( log_q, max_log_q) ;
1026
1038
}
1027
1039
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
+ ) ;
1030
1046
log_base_q = cmp:: max ( log_base_q, min_log_base_q) ;
1031
1047
log_q = cmp:: max ( log_q, min_log_q) ;
1032
1048
}
@@ -1044,7 +1060,7 @@ impl RCState {
1044
1060
// Computes a quantizer directly from the frame type and base quantizer index,
1045
1061
// without consideration for rate control.
1046
1062
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 ,
1048
1064
) -> ( i64 , i64 ) {
1049
1065
// TODO: Rename "quantizer" something that indicates it is a quantizer
1050
1066
// index, and move it somewhere more sensible (or choose a better way to
@@ -1063,7 +1079,7 @@ impl RCState {
1063
1079
let log_base_q = ( log_ac_q + log_dc_q + 1 ) >> 1 ;
1064
1080
// Adjust the quantizer for the frame type, result is Q57:
1065
1081
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 ;
1067
1083
( log_base_q, log_q)
1068
1084
}
1069
1085
0 commit comments