From: Kostya Shishkov Date: Thu, 4 May 2023 16:22:39 +0000 (+0200) Subject: parse bitrate in a more flexible way X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=30611a63a7e339e63f05acb263a41307255040f1;p=nihav-encoder.git parse bitrate in a more flexible way --- diff --git a/src/main.rs b/src/main.rs index 409c677..a2889a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,6 +50,38 @@ fn print_options(name: &str, options: &[NAOptionDefinition]) { } } +fn parse_bitrate(strval: &str) -> Result { + 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, @@ -358,7 +390,7 @@ impl Transcoder { } },*/ "bitrate" => { - let ret = oval[1].parse::(); + let ret = parse_bitrate(oval[1]); if let Ok(val) = ret { ostr.enc_params.bitrate = val; } else {