]> git.nihav.org Git - nihav-encoder.git/commitdiff
parse bitrate in a more flexible way
authorKostya Shishkov <kostya.shishkov@gmail.com>
Thu, 4 May 2023 16:22:39 +0000 (18:22 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Thu, 4 May 2023 16:22:39 +0000 (18:22 +0200)
src/main.rs

index 409c677a5c4f2e81179e1ccd9e4480df7746e4c2..a2889a86b6ab7df77251d3f9bf2f8aa5ba9d54c7 100644 (file)
@@ -50,6 +50,38 @@ fn print_options(name: &str, options: &[NAOptionDefinition]) {
     }
 }
 
+fn parse_bitrate(strval: &str) -> Result<u32, ()> {
+    let mut val = 0;
+    let mut has_suffix = false;
+    for ch in strval.chars() {
+        match ch {
+            _ if has_suffix => return Err(()),
+            '0'..='9' => {
+                if val >= std::u32::MAX / 100 {
+                    return Err(());
+                }
+                val = val * 10 + ch.to_digit(10).unwrap_or(0);
+            },
+            'k' | 'K' => {
+                if val >= std::u32::MAX / 1000 {
+                    return Err(());
+                }
+                val *= 1000;
+                has_suffix = true;
+            },
+            'm' | 'M' => {
+                if val >= std::u32::MAX / 1000000 {
+                    return Err(());
+                }
+                val *= 1000000;
+                has_suffix = true;
+            },
+            _ => return Err(()),
+        };
+    }
+    Ok(val)
+}
+
 struct OptionArgs {
     name:   String,
     value:  Option<String>,
@@ -358,7 +390,7 @@ impl Transcoder {
                         }
                     },*/
                     "bitrate" => {
-                        let ret = oval[1].parse::<u32>();
+                        let ret = parse_bitrate(oval[1]);
                         if let Ok(val) = ret {
                             ostr.enc_params.bitrate = val;
                         } else {