]> git.nihav.org Git - nihav.git/commitdiff
indeo3enc: try to improve rate control a bit more
authorKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 8 Sep 2025 17:03:47 +0000 (19:03 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 8 Sep 2025 17:03:47 +0000 (19:03 +0200)
nihav-indeo/src/codecs/indeo3enc/mod.rs
nihav-indeo/src/codecs/indeo3enc/ratectl.rs

index 156418a946855a86f839878e7a15b6bd9c4d3116..2df61c761d9f529c97e2c9b80ebfa2263ba7a3f3 100644 (file)
@@ -361,19 +361,15 @@ impl NAEncoder for Indeo3Encoder {
             let mut skipped = self.encode_planes(&mut dbuf, &trees, is_intra)?;
 
             let cur_quant = self.cenc.quant.unwrap_or(42);
-            if self.try_again && !is_intra && cur_quant < 8 {
+            if self.try_again && !is_intra && cur_quant < 7 {
                 let expected_size = self.rc.get_expected_size();
                 if expected_size > 0 {
                     let cur_size = dbuf.len() as u32;
                     // try re-encoding frame if possible
                     if cur_size > expected_size * 3 / 2 {
                         self.cframe.fill(vbuf);
-                        let new_quant = if cur_quant < 7 {
-                                cur_quant + 1
-                            } else {
-                                cur_quant - 1
-                            };
-                        self.cenc.quant = Some(new_quant);
+                        self.cenc.quant = Some(cur_quant + 1);
+                        self.rc.increase_quant();
                         dbuf.truncate(OS_HEADER_SIZE + BITSTREAM_HEADER_SIZE);
                         skipped = self.encode_planes(&mut dbuf, &trees, is_intra)?;
                     }
@@ -596,7 +592,7 @@ mod test {
         let enc_options = &[
                 NAOption { name: super::TRY_AGAIN_OPTION, value: NAValue::Bool(true) },
             ];
-        encode_test("indeo3.avi", enc_options, Some(4), &[0x17d742bc, 0x6f4c1200, 0x79422bac, 0xc46b5dd0]);
+        encode_test("indeo3.avi", enc_options, Some(4), &[0xb7a78851, 0x194f8842, 0x3d05071d, 0x3029ab72]);
     }
     /*#[test]
     fn test_indeo3_roundtrip() {
index 72bed261b0e4bc967b127c23d6be23fb2a37ef77..1142ff7359649297a1b0ce0e01be7a765c3097c7 100644 (file)
@@ -69,9 +69,16 @@ impl RateControl {
             self.br_pool = self.br_pool.saturating_sub(size).min(self.bitrate * 3);
         }
     }
+    pub fn increase_quant(&mut self) {
+        if let Some(q) = self.quality {
+            if q < 8 {
+                self.quality = Some(q + 1);
+            }
+        }
+    }
     pub fn get_expected_size(&self) -> u32 {
         if self.bitrate != 0 {
-            self.br_pool * self.tb_num / self.fracs
+            (u64::from(self.br_pool) * u64::from(self.tb_num) / u64::from(self.fracs)) as u32
         } else {
             0
         }