ExportSettings for bitrate and software-encoder pin
Browse files at this commit2c9c3a24d0c9dd6e9afd9691d9aaf35d25665f58Parent: 3adbaf7- feat
- ExportSettings for bitrate and software-encoder pin
3 changed files +86 -10
- crates/engine/src/export.rs +39 -7
- crates/engine/src/playback.rs +15 -1
- crates/engine/tests/export.rs +32 -2
crates/engine/src/export.rsmodified+39 -7
| @@ -36,6 +36,20 @@ const BITS_PER_PIXEL: f64 = 0.1; | |||
| 36 | const MIN_BITRATE: u64 = 1_000_000; | 36 | const MIN_BITRATE: u64 = 1_000_000; |
| 37 | const MAX_BITRATE: u64 = 20_000_000; | 37 | const MAX_BITRATE: u64 = 20_000_000; |
| 38 | 38 | ||
| 39 | +/// What the caller gets to decide about the output. The codec and container are | ||
| 40 | +/// not among it: H.264 in mp4 is the only pair with both an encoder and a | ||
| 41 | +/// decoder under this project's no-install rule, so offering a choice would be | ||
| 42 | +/// offering files we cannot read back. | ||
| 43 | +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] | ||
| 44 | +pub struct ExportSettings { | ||
| 45 | + /// Bits per second, clamped to the same sane range the automatic value uses. | ||
| 46 | + /// `None` picks it from the picture size and frame rate. | ||
| 47 | + pub bitrate: Option<u64>, | ||
| 48 | + /// Skip the hardware encoder even where it is available -- an escape hatch | ||
| 49 | + /// for a driver that encodes badly, matching the `VE_SW_ENC` env pin. | ||
| 50 | + pub force_sw: bool, | ||
| 51 | +} | ||
| 52 | + | ||
| 39 | struct Shared { | 53 | struct Shared { |
| 40 | progress: AtomicU32, | 54 | progress: AtomicU32, |
| 41 | cancel: AtomicBool, | 55 | cancel: AtomicBool, |
| @@ -80,7 +94,13 @@ impl ExportHandle { | |||
| 80 | /// returned, so a caller has exactly one place to look. The files to read are | 94 | /// returned, so a caller has exactly one place to look. The files to read are |
| 81 | /// the project's own [`sources`](Project::sources) -- every clip names one -- | 95 | /// the project's own [`sources`](Project::sources) -- every clip names one -- |
| 82 | /// so nothing but the edit list decides what is decoded. | 96 | /// so nothing but the edit list decides what is decoded. |
| 83 | -pub fn start(project: Project, meta: VideoMeta, out: &Path) -> ExportHandle { | 97 | +pub fn start( |
| 98 | + project: Project, | ||
| 99 | + meta: VideoMeta, | ||
| 100 | + out: &Path, | ||
| 101 | + settings: &ExportSettings, | ||
| 102 | +) -> ExportHandle { | ||
| 103 | + let settings = *settings; | ||
| 84 | let shared = Arc::new(Shared { | 104 | let shared = Arc::new(Shared { |
| 85 | progress: AtomicU32::new(0), | 105 | progress: AtomicU32::new(0), |
| 86 | cancel: AtomicBool::new(false), | 106 | cancel: AtomicBool::new(false), |
| @@ -98,7 +118,7 @@ pub fn start(project: Project, meta: VideoMeta, out: &Path) -> ExportHandle { | |||
| 98 | // The rename is the last step and the only one that publishes a file | 118 | // The rename is the last step and the only one that publishes a file |
| 99 | // under the name the caller asked for; it stays on the same directory, | 119 | // under the name the caller asked for; it stays on the same directory, |
| 100 | // so it is atomic. | 120 | // so it is atomic. |
| 101 | - let result = run(&project, &meta, &part, &worker) | 121 | + let result = run(&project, &meta, &part, &worker, &settings) |
| 102 | .and_then(|()| std::fs::rename(&part, &out).map_err(Into::into)); | 122 | .and_then(|()| std::fs::rename(&part, &out).map_err(Into::into)); |
| 103 | if result.is_err() { | 123 | if result.is_err() { |
| 104 | // The muxer -- and with it the file handle -- died with `run`. | 124 | // The muxer -- and with it the file handle -- died with `run`. |
| @@ -118,7 +138,13 @@ fn settle(shared: &Shared, result: crate::Result<()>) { | |||
| 118 | shared.finished.store(true, Ordering::Release); | 138 | shared.finished.store(true, Ordering::Release); |
| 119 | } | 139 | } |
| 120 | 140 | ||
| 121 | -fn run(project: &Project, meta: &VideoMeta, out: &Path, shared: &Shared) -> crate::Result<()> { | 141 | +fn run( |
| 142 | + project: &Project, | ||
| 143 | + meta: &VideoMeta, | ||
| 144 | + out: &Path, | ||
| 145 | + shared: &Shared, | ||
| 146 | + settings: &ExportSettings, | ||
| 147 | +) -> crate::Result<()> { | ||
| 122 | let total = project.timeline_frames(); | 148 | let total = project.timeline_frames(); |
| 123 | let sources = project.sources(); | 149 | let sources = project.sources(); |
| 124 | // Audio first: a track has to be declared when the muxer is created, which | 150 | // Audio first: a track has to be declared when the muxer is created, which |
| @@ -140,7 +166,7 @@ fn run(project: &Project, meta: &VideoMeta, out: &Path, shared: &Shared) -> crat | |||
| 140 | chan_conf: track.chan_conf, | 166 | chan_conf: track.chan_conf, |
| 141 | }); | 167 | }); |
| 142 | 168 | ||
| 143 | - let mut encoder = Enc::open(meta)?; | 169 | + let mut encoder = Enc::open(meta, settings)?; |
| 144 | let mut muxer = None; | 170 | let mut muxer = None; |
| 145 | let mut done = 0u32; | 171 | let mut done = 0u32; |
| 146 | for clip in project.clips() { | 172 | for clip in project.clips() { |
| @@ -255,12 +281,18 @@ enum Enc { | |||
| 255 | } | 281 | } |
| 256 | 282 | ||
| 257 | impl Enc { | 283 | impl Enc { |
| 258 | - fn open(meta: &VideoMeta) -> crate::Result<Self> { | 284 | + fn open(meta: &VideoMeta, settings: &ExportSettings) -> crate::Result<Self> { |
| 259 | - let bitrate = bitrate_for(meta); | 285 | + // A caller's number goes through the same clamp as the computed one: a |
| 286 | + // zero bitrate switches the software encoder's lookahead on, which would | ||
| 287 | + // break the one-picture-per-call contract `encode` documents below. | ||
| 288 | + let bitrate = settings | ||
| 289 | + .bitrate | ||
| 290 | + .map_or_else(|| bitrate_for(meta), |b| b.clamp(MIN_BITRATE, MAX_BITRATE)); | ||
| 260 | // The plugin wants an exact rational; a thousandth of a frame per second | 291 | // The plugin wants an exact rational; a thousandth of a frame per second |
| 261 | // is finer than any container fps we can read back. | 292 | // is finer than any container fps we can read back. |
| 262 | let fps_num = (meta.frame_rate * 1_000.0).round().max(1.0) as u32; | 293 | let fps_num = (meta.frame_rate * 1_000.0).round().max(1.0) as u32; |
| 263 | - if !forced("VE_SW_ENC") | 294 | + if !settings.force_sw |
| 295 | + && !forced("VE_SW_ENC") | ||
| 264 | && let Some(hw) = HwEncoder::open(meta.width, meta.height, fps_num, 1_000, bitrate) | 296 | && let Some(hw) = HwEncoder::open(meta.width, meta.height, fps_num, 1_000, bitrate) |
| 265 | { | 297 | { |
| 266 | eprintln!("export encoder: hardware (VA-API plugin)"); | 298 | eprintln!("export encoder: hardware (VA-API plugin)"); |
crates/engine/src/playback.rsmodified+15 -1
| @@ -562,8 +562,22 @@ impl PlaybackSession { | |||
| 562 | /// | 562 | /// |
| 563 | /// Later edits do not reach a running export: it works from a snapshot of | 563 | /// Later edits do not reach a running export: it works from a snapshot of |
| 564 | /// the edit list taken here. | 564 | /// the edit list taken here. |
| 565 | + /// | ||
| 566 | + /// ponytail: this defaults the settings; the app still calls it that way, | ||
| 567 | + /// and the export options card replaces the call with | ||
| 568 | + /// [`export_to_with`](PlaybackSession::export_to_with). | ||
| 565 | pub fn export_to(&self, out: &Path) -> crate::ExportHandle { | 569 | pub fn export_to(&self, out: &Path) -> crate::ExportHandle { |
| 566 | - crate::export::start(self.project.clone(), self.meta, out) | 570 | + self.export_to_with(out, &crate::export::ExportSettings::default()) |
| 571 | + } | ||
| 572 | + | ||
| 573 | + /// [`export_to`](PlaybackSession::export_to) with the output settings spelled | ||
| 574 | + /// out rather than defaulted. | ||
| 575 | + pub fn export_to_with( | ||
| 576 | + &self, | ||
| 577 | + out: &Path, | ||
| 578 | + settings: &crate::export::ExportSettings, | ||
| 579 | + ) -> crate::ExportHandle { | ||
| 580 | + crate::export::start(self.project.clone(), self.meta, out, settings) | ||
| 567 | } | 581 | } |
| 568 | 582 | ||
| 569 | /// Moves the clock forward; the caller runs this once per rendered frame. | 583 | /// Moves the clock forward; the caller runs this once per rendered frame. |
crates/engine/tests/export.rsmodified+32 -2
| @@ -19,6 +19,7 @@ use std::path::{Path, PathBuf}; | |||
| 19 | use std::sync::Once; | 19 | use std::sync::Once; |
| 20 | use std::time::{Duration, Instant}; | 20 | use std::time::{Duration, Instant}; |
| 21 | 21 | ||
| 22 | +use engine::export::ExportSettings; | ||
| 22 | use engine::hw::HwEncoder; | 23 | use engine::hw::HwEncoder; |
| 23 | use engine::mux::{Mp4Muxer, VideoParams, parameter_sets}; | 24 | use engine::mux::{Mp4Muxer, VideoParams, parameter_sets}; |
| 24 | use engine::{DecodeSession, ExportHandle, PlaybackSession, Project}; | 25 | use engine::{DecodeSession, ExportHandle, PlaybackSession, Project}; |
| @@ -202,7 +203,7 @@ fn multi_source_round_trip(name: &str, limit: Duration) { | |||
| 202 | let out = out_path(name); | 203 | let out = out_path(name); |
| 203 | 204 | ||
| 204 | let started = Instant::now(); | 205 | let started = Instant::now(); |
| 205 | - let handle = engine::export::start(project.clone(), meta, &out); | 206 | + let handle = engine::export::start(project.clone(), meta, &out, &ExportSettings::default()); |
| 206 | wait(&handle, limit).expect("export"); | 207 | wait(&handle, limit).expect("export"); |
| 207 | println!( | 208 | println!( |
| 208 | "{name}: {total} frames of two sources in {:.2} s", | 209 | "{name}: {total} frames of two sources in {:.2} s", |
| @@ -274,6 +275,35 @@ fn exports_two_sources_as_one_timeline_in_hardware() { | |||
| 274 | multi_source_round_trip("multi_hw", Duration::from_secs(60)); | 275 | multi_source_round_trip("multi_hw", Duration::from_secs(60)); |
| 275 | } | 276 | } |
| 276 | 277 | ||
| 278 | +/// The bitrate setting is the one knob a user turns for quality, so it has to | ||
| 279 | +/// reach the encoder: the same 120-frame timeline at 1, 4 and 8 Mbps has to come | ||
| 280 | +/// back as three strictly growing files. `force_sw` pins the software encoder, | ||
| 281 | +/// whose rate control is the one every machine has. | ||
| 282 | +#[test] | ||
| 283 | +fn a_higher_bitrate_writes_a_bigger_file() { | ||
| 284 | + pin_software(); | ||
| 285 | + let mut sizes = Vec::new(); | ||
| 286 | + for bitrate in [1_000_000u64, 4_000_000, 8_000_000] { | ||
| 287 | + let mut session = edited(&asset("test_baseline.mp4")); | ||
| 288 | + session.pause(); | ||
| 289 | + let out = out_path(&format!("bitrate_{bitrate}")); | ||
| 290 | + let settings = ExportSettings { | ||
| 291 | + bitrate: Some(bitrate), | ||
| 292 | + force_sw: true, | ||
| 293 | + }; | ||
| 294 | + let handle = session.export_to_with(&out, &settings); | ||
| 295 | + wait(&handle, Duration::from_secs(180)).expect("export"); | ||
| 296 | + let size = std::fs::metadata(&out).expect("export exists").len(); | ||
| 297 | + println!("{} Mbps: {size} bytes", bitrate / 1_000_000); | ||
| 298 | + sizes.push(size); | ||
| 299 | + std::fs::remove_file(&out).unwrap(); | ||
| 300 | + } | ||
| 301 | + assert!( | ||
| 302 | + sizes[0] < sizes[1] && sizes[1] < sizes[2], | ||
| 303 | + "file sizes {sizes:?} are not strictly growing with the bitrate" | ||
| 304 | + ); | ||
| 305 | +} | ||
| 306 | + | ||
| 277 | /// A source that disappears between the edit and the export fails the export | 307 | /// A source that disappears between the edit and the export fails the export |
| 278 | /// rather than silently writing a shorter file -- and leaves neither an output | 308 | /// rather than silently writing a shorter file -- and leaves neither an output |
| 279 | /// nor a `.part`. Both sources are silent, so the failure is the per-clip open | 309 | /// nor a `.part`. Both sources are silent, so the failure is the per-clip open |
| @@ -298,7 +328,7 @@ fn a_vanished_source_fails_the_export() { | |||
| 298 | 328 | ||
| 299 | std::fs::remove_file(&doomed).expect("unlink the second source"); | 329 | std::fs::remove_file(&doomed).expect("unlink the second source"); |
| 300 | let out = out_path("vanished"); | 330 | let out = out_path("vanished"); |
| 301 | - let handle = engine::export::start(project, meta, &out); | 331 | + let handle = engine::export::start(project, meta, &out, &ExportSettings::default()); |
| 302 | let result = wait(&handle, Duration::from_secs(60)); | 332 | let result = wait(&handle, Duration::from_secs(60)); |
| 303 | let error = result.expect_err("an export of a missing file cannot succeed"); | 333 | let error = result.expect_err("an export of a missing file cannot succeed"); |
| 304 | println!("vanished source: {error}"); | 334 | println!("vanished source: {error}"); |