diff --git a/src-tauri/src/encode.rs b/src-tauri/src/encode.rs index e3d2f26..fde6484 100644 --- a/src-tauri/src/encode.rs +++ b/src-tauri/src/encode.rs @@ -56,6 +56,17 @@ pub(crate) fn round_i32(v: f64) -> i32 { out } +/// Byte counts and bitrates are many orders of magnitude below 2^53, so f64 +/// holds them exactly. This is the single place that conversion is justified. +pub(crate) fn bytes_f64(v: u64) -> f64 { + #[allow( + clippy::cast_precision_loss, + reason = "byte counts and bitrates in this app are far below f64's exact-integer range" + )] + let out = v as f64; + out +} + /// Progress update sent during encoding. /// pass: 1-based pass number (1 or 2 for two-pass video, 1 for GIF). /// `total_passes`: total number of passes. @@ -351,15 +362,9 @@ pub(crate) fn codec_availability(ffmpeg: &Path) -> CodecAvailability { } fn video_bitrate_kbps(req: &TranscodeRequest) -> Result { - // Byte and bitrate budgets are far below 2^53, so f64 represents them - // exactly; these casts are the only way to get u64 into the f64 maths. - #[allow( - clippy::cast_precision_loss, - reason = "target sizes and bitrates are many orders of magnitude below f64's exact-integer range" - )] - let target_bits = (req.target_bytes as f64) * 8.0 * 0.95; + let target_bits = bytes_f64(req.target_bytes) * 8.0 * 0.95; let audio_bits = if req.keep_audio { - (AUDIO_BITRATE_KBPS as f64) * 1000.0 * req.duration_secs + bytes_f64(AUDIO_BITRATE_KBPS) * 1000.0 * req.duration_secs } else { 0.0 }; @@ -548,7 +553,7 @@ fn calibrate_via_sample( let aspect = aspect_ratio(req.clip_width, req.clip_height); let sample_h = round_u32(f64::from(est_width) / aspect); let frames = (f64::from(est_fps) * sample_dur).max(1.0); - let measured_bpp = sample_bytes as f64 / (f64::from(est_width) * f64::from(sample_h) * frames); + let measured_bpp = bytes_f64(sample_bytes) / (f64::from(est_width) * f64::from(sample_h) * frames); // Blend toward the assumed 0.2 constant - don't over-trust a 2s sample. let blended_bpp = measured_bpp * 0.7 + 0.2 * 0.3; @@ -647,7 +652,7 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> { bail!("gif output is empty"); } - let ratio = target as f64 / actual as f64; + let ratio = bytes_f64(target) / bytes_f64(actual); // Accept if at or under target (even if significantly under - don't upscale) if ratio >= 1.0 { @@ -673,8 +678,8 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> { // the file, or moved us farther from the target, force the strongest // allowed shrink next time instead of creeping/oscillating. if let Some(prev) = prev_actual { - let prev_dist = (target as f64 - prev as f64).abs(); - let cur_dist = (target as f64 - actual as f64).abs(); + let prev_dist = (bytes_f64(target) - bytes_f64(prev)).abs(); + let cur_dist = (bytes_f64(target) - bytes_f64(actual)).abs(); if actual >= prev || cur_dist > prev_dist { tracing::warn!( "gif encode: retry trend worsened. prev={}B, now={}B. using stronger shrink.", @@ -812,7 +817,7 @@ pub(crate) fn gif_params(input: &GifBudgetInput, clip_width: u32, clip_height: u // 256 colors = 1.0x, 128 = ~0.71x, 64 = 0.5x, 32 = ~0.35x, 16 = 0.25x, 2 = ~0.13x let color_factor = (f64::from(input.gif_colors) / 256.0).sqrt().clamp(0.15, 1.0); let bytes_per_pixel_per_frame = 0.2 * color_factor; - let target_bytes = input.target_bytes as f64; + let target_bytes = bytes_f64(input.target_bytes); let requested_fps = f64::from(input.fps); let duration = input.duration_secs;