aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKostya Shishkov <kostya.shishkov@gmail.com>2023-05-04 18:22:39 +0200
committerKostya Shishkov <kostya.shishkov@gmail.com>2023-05-04 18:22:39 +0200
commit30611a63a7e339e63f05acb263a41307255040f1 (patch)
tree9071c7db894ce63db357e622d11bd63500f488a5 /src
parent343a59ecb05378c762181bccbec02a2ed15100e4 (diff)
downloadnihav-encoder-30611a63a7e339e63f05acb263a41307255040f1.tar.gz
parse bitrate in a more flexible way
Diffstat (limited to 'src')
-rw-r--r--src/main.rs34
1 files changed, 33 insertions, 1 deletions
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<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 {