Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

feat(export): HEVC and AV1, ten-bit, colour tags, and HDR sources brought in right

Jareer9ef1b9466e8c
diff --git a/engine/crates/concat-api/src/lib.rs b/engine/crates/concat-api/src/lib.rs
index 2f92cb4d..86f1f2fc 100644
--- a/engine/crates/concat-api/src/lib.rs
+++ b/engine/crates/concat-api/src/lib.rs
@@ -309,6 +309,11 @@ impl Api {
let session = self.session(path)?;
let titles = self.title_clips(session, width, height);
+ let codec = match spec.codec.as_deref() {
+ None => export::VideoCodec::H264,
+ Some(name) => export::VideoCodec::parse(name)
+ .ok_or_else(|| format!("unknown codec {name:?}: h264, hevc or av1"))?,
+ };
let host_spec = export::ExportSpec {
output: spec.output.clone(),
crf: spec.crf.unwrap_or(DEFAULT_CRF),
@@ -316,6 +321,8 @@ impl Api {
.preset
.clone()
.unwrap_or_else(|| DEFAULT_PRESET.to_owned()),
+ codec,
+ ten_bit: spec.ten_bit.unwrap_or(false),
};
let mut request = export::request(session, &host_spec, titles);
request.width = width;
diff --git a/engine/crates/concat-api/src/message.rs b/engine/crates/concat-api/src/message.rs
index c8b2dc01..323ef8df 100644
--- a/engine/crates/concat-api/src/message.rs
+++ b/engine/crates/concat-api/src/message.rs
@@ -251,6 +251,12 @@ pub struct ExportSpec {
/// Frame rate denominator; the timeline's when absent.
#[serde(default)]
pub rate_den: Option<i64>,
+ /// "h264", "hevc" or "av1". H.264 when absent.
+ #[serde(default)]
+ pub codec: Option<String>,
+ /// Ten bits a channel. Eight when absent.
+ #[serde(default)]
+ pub ten_bit: Option<bool>,
}
/// What a request hands back. Serialised as the payload alone: the variant
diff --git a/engine/crates/concat-export/src/lib.rs b/engine/crates/concat-export/src/lib.rs
index eaa91974..80df302a 100644
--- a/engine/crates/concat-export/src/lib.rs
+++ b/engine/crates/concat-export/src/lib.rs
@@ -35,7 +35,9 @@ use concat_core::time::{FrameRate, Rational};
use concat_core::timeline::{Clip, ClipId, MediaRef, Timeline, Track, TrackKind, Transform};
use concat_effects::Catalogue;
use concat_media::audio::{self, AudioClip};
-use concat_media::{DecodeOptions, Decoder, EncodeOptions, Encoder, FrameSink, FrameSource};
+use concat_media::{
+ DecodeOptions, Decoder, EncodeOptions, Encoder, FrameSink, FrameSource, VideoCodec,
+};
use concat_project::model::{AppliedFilter, Cutout};
use concat_render::{
Compositor, CpuCompositor, Layer, Placement, Treatment as GpuTreatment, plan_frame,
@@ -271,10 +273,28 @@ pub struct ExportRequest {
pub crf: u8,
/// The x264 speed/size preset name, e.g. "medium".
pub preset: String,
+ /// What to encode to, by name: "h264", "hevc" or "av1". H.264 when a
+ /// request does not say.
+ #[serde(default, deserialize_with = "codec_by_name")]
+ pub codec: VideoCodec,
+ /// Ten bits a channel rather than eight.
+ #[serde(default)]
+ pub ten_bit: bool,
/// The flattened clip list to render.
pub clips: Vec<ExportClip>,
}
+/// A codec named the way [`VideoCodec::name`] names it, refusing a name
+/// the engine has no encoder for rather than quietly falling back.
+fn codec_by_name<'de, D: serde::Deserializer<'de>>(
+ deserializer: D,
+) -> Result<VideoCodec, D::Error> {
+ let name = String::deserialize(deserializer)?;
+ VideoCodec::parse(&name).ok_or_else(|| {
+ serde::de::Error::custom(format!("unknown codec {name:?}: h264, hevc or av1"))
+ })
+}
+
/// What the export loop calls to report and to ask "should I stop?".
pub struct Reporter<'a> {
/// Called with (frame, total, stage).
@@ -963,7 +983,9 @@ fn render_picture(
&EncodeOptions {
crf: request.crf,
preset: request.preset.clone(),
- ..EncodeOptions::default()
+ codec: request.codec,
+ ten_bit: request.ten_bit,
+ hardware: true,
},
)
.map_err(|error| error.to_string())?;
@@ -1885,6 +1907,8 @@ pub fn preview_plan(
rate_den,
crf: 18,
preset: String::new(),
+ codec: VideoCodec::H264,
+ ten_bit: false,
clips: Vec::new(),
};
PreviewPlan {
diff --git a/engine/crates/concat-host/src/export.rs b/engine/crates/concat-host/src/export.rs
index dbed3a7e..ab7d2f63 100644
--- a/engine/crates/concat-host/src/export.rs
+++ b/engine/crates/concat-host/src/export.rs
@@ -11,7 +11,8 @@
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
-pub use concat_export::{ExportClip, ExportRequest, Reporter, render};
+use concat_export::{ExportClip, ExportRequest, Reporter, render};
+pub use concat_media::VideoCodec;
use crate::jobs::{Job, SingleFlight};
use crate::session::Session;
@@ -26,6 +27,10 @@ pub struct ExportSpec {
pub crf: u8,
/// The x264 speed/size preset name, e.g. "medium".
pub preset: String,
+ /// What to encode to.
+ pub codec: VideoCodec,
+ /// Ten bits a channel rather than eight.
+ pub ten_bit: bool,
}
/// One progress report: which frame of how many, in which stage.
@@ -52,6 +57,8 @@ pub fn request(session: &Session, spec: &ExportSpec, titles: Vec<ExportClip>) ->
rate_den: settings.rate_den,
crf: spec.crf,
preset: spec.preset.clone(),
+ codec: spec.codec,
+ ten_bit: spec.ten_bit,
clips,
}
}
diff --git a/engine/crates/concat-media/src/decode.rs b/engine/crates/concat-media/src/decode.rs
index d1583a4c..26746c43 100644
--- a/engine/crates/concat-media/src/decode.rs
+++ b/engine/crates/concat-media/src/decode.rs
@@ -155,14 +155,93 @@ impl DecodeOptions {
}
}
+/// How a stream says its colour is encoded: the tags in its container,
+/// as libavcodec reads them. Unspecified where it says nothing, which is
+/// most SD and a lot of HD, and then it is taken for BT.709.
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub struct ColorSignal {
+ /// The gamut.
+ pub primaries: ffmpeg::color::Primaries,
+ /// The transfer curve: BT.709 for SDR, PQ or HLG for HDR.
+ pub transfer: ffmpeg::color::TransferCharacteristic,
+ /// The YCbCr matrix.
+ pub matrix: ffmpeg::color::Space,
+ /// Video (16-235) or full range.
+ pub range: ffmpeg::color::Range,
+}
+
+impl ColorSignal {
+ /// Whether the picture is outside BT.709 as the engine works in it:
+ /// an HDR transfer, or the BT.2020 gamut. Both need converting on
+ /// the way in, or they arrive flat, dim and the wrong colour - the
+ /// 10-bit PQ file from a phone, decoded as if it were a 709 one.
+ pub fn is_wide(&self) -> bool {
+ use ffmpeg::color::{Primaries, TransferCharacteristic as Transfer};
+ matches!(self.transfer, Transfer::SMPTE2084 | Transfer::ARIB_STD_B67)
+ || self.primaries == Primaries::BT2020
+ }
+
+ /// Whether the transfer is a high dynamic range one: PQ or HLG.
+ pub fn is_hdr(&self) -> bool {
+ use ffmpeg::color::TransferCharacteristic as Transfer;
+ matches!(self.transfer, Transfer::SMPTE2084 | Transfer::ARIB_STD_B67)
+ }
+
+ /// The `scale` filter's arguments that bring a wide picture into
+ /// BT.709: the source's tags named where it has them - `auto` reads
+ /// the frames' own where it does not - and BT.709 out, tone-mapped
+ /// perceptually rather than clipped. swscale has done this itself
+ /// since FFmpeg 7.1, so it costs no library the bundles lack.
+ fn bt709_args(self) -> String {
+ use ffmpeg::color::{Primaries, Range, Space, TransferCharacteristic as Transfer};
+ let primaries = match self.primaries {
+ Primaries::BT2020 => "bt2020",
+ Primaries::BT709 => "bt709",
+ Primaries::SMPTE432 => "smpte432",
+ _ => "auto",
+ };
+ let transfer = match self.transfer {
+ Transfer::SMPTE2084 => "smpte2084",
+ Transfer::ARIB_STD_B67 => "arib-std-b67",
+ Transfer::BT2020_10 => "bt2020-10",
+ Transfer::BT2020_12 => "bt2020-12",
+ Transfer::BT709 => "bt709",
+ _ => "auto",
+ };
+ let matrix = match self.matrix {
+ Space::BT2020NCL => "bt2020nc",
+ Space::BT2020CL => "bt2020c",
+ Space::BT709 => "bt709",
+ _ => "auto",
+ };
+ let range = match self.range {
+ Range::MPEG => "tv",
+ Range::JPEG => "pc",
+ _ => "auto",
+ };
+ format!(
+ ":in_primaries={primaries}:in_transfer={transfer}:in_color_matrix={matrix}\
+ :in_range={range}:out_primaries=bt709:out_transfer=bt709\
+ :out_color_matrix=bt709:out_range=tv:intent=perceptual"
+ )
+ }
+}
+
/// The filtergraph between the decoder and the caller, as one string.
///
/// Rotation first, so everything downstream sees the picture the way a
-/// player would; then scale-to-fit; then the effect chain at output
+/// player would; then scale-to-fit, which is also where a wide or HDR
+/// picture is brought into BT.709; then the effect chain at output
/// resolution (cheaper than filtering the source size, and parameters mean
/// the same thing at every export size); then the guard scale that pins the
/// frame size the caller was promised; then RGBA.
-fn video_filter(rotation: i64, options: &DecodeOptions, width: u32, height: u32) -> String {
+fn video_filter(
+ rotation: i64,
+ options: &DecodeOptions,
+ width: u32,
+ height: u32,
+ color: Option<&ColorSignal>,
+) -> String {
let mut parts: Vec<String> = Vec::new();
if let Some(turn) = ffi::rotation_filters(rotation) {
parts.push(turn.to_owned());
@@ -170,7 +249,11 @@ fn video_filter(rotation: i64, options: &DecodeOptions, width: u32, height: u32)
if let Some(pre) = &options.pre_chain {
parts.push(pre.clone());
}
- parts.push(format!("scale={width}:{height}:flags=bilinear"));
+ let convert = color
+ .filter(|signal| signal.is_wide())
+ .map(|signal| signal.bt709_args())
+ .unwrap_or_default();
+ parts.push(format!("scale={width}:{height}:flags=bilinear{convert}"));
if let Some(chain) = &options.filter_chain {
parts.push(chain.clone());
parts.push(format!("scale={width}:{height}:flags=bilinear"));
@@ -211,6 +294,8 @@ pub struct Decoder {
source_done: bool,
produced: u64,
position: Option<Rational>,
+ /// What the stream says its colour is.
+ color: ColorSignal,
}
impl Decoder {
@@ -246,6 +331,12 @@ impl Decoder {
if options.keyframes_only {
decoder.skip_frame(ffmpeg::Discard::NonKey);
}
+ let color = ColorSignal {
+ primaries: decoder.color_primaries(),
+ transfer: decoder.color_transfer_characteristic(),
+ matrix: decoder.color_space(),
+ range: decoder.color_range(),
+ };
let (width, height) = match options.size {
Some(size) => size,
@@ -277,6 +368,7 @@ impl Decoder {
source_done: false,
produced: 0,
position: None,
+ color,
};
if let Some(start) = options.start
&& !start.is_zero()
@@ -432,7 +524,13 @@ impl Decoder {
"",
)
.map_err(|error| ffi::fail("buffer sink", &self.path, error))?;
- let spec = video_filter(self.rotation, &self.options, self.width, self.height);
+ let spec = video_filter(
+ self.rotation,
+ &self.options,
+ self.width,
+ self.height,
+ Some(&self.color),
+ );
graph
.output("in", 0)
.and_then(|parser| parser.input("out", 0))
@@ -516,6 +614,13 @@ impl Decoder {
}
}
+impl Decoder {
+ /// What the stream says its colour is; see [`ColorSignal`].
+ pub fn color(&self) -> ColorSignal {
+ self.color
+ }
+}
+
impl FrameSource for Decoder {
fn width(&self) -> u32 {
self.width
@@ -575,7 +680,7 @@ mod tests {
.scaled_to(640, 360)
.filtered("hue=s=0");
assert_eq!(
- video_filter(0, &options, 640, 360),
+ video_filter(0, &options, 640, 360, None),
"scale=640:360:flags=bilinear,hue=s=0,scale=640:360:flags=bilinear,format=rgba",
);
}
@@ -583,8 +688,91 @@ mod tests {
#[test]
fn rotation_comes_first() {
let options = DecodeOptions::default();
- assert!(video_filter(90, &options, 1080, 1920).starts_with("transpose=clock,"));
- assert!(video_filter(0, &options, 1920, 1080).starts_with("scale="));
+ assert!(video_filter(90, &options, 1080, 1920, None).starts_with("transpose=clock,"));
+ assert!(video_filter(0, &options, 1920, 1080, None).starts_with("scale="));
+ }
+
+ #[test]
+ fn a_wide_picture_is_brought_into_bt709_and_a_709_one_is_left_alone() {
+ use ffmpeg::color::{Primaries, Range, Space, TransferCharacteristic as Transfer};
+ let options = DecodeOptions::default();
+ let sdr = ColorSignal {
+ primaries: Primaries::BT709,
+ transfer: Transfer::BT709,
+ matrix: Space::BT709,
+ range: Range::MPEG,
+ };
+ assert!(!sdr.is_wide());
+ assert_eq!(
+ video_filter(0, &options, 640, 360, Some(&sdr)),
+ "scale=640:360:flags=bilinear,format=rgba"
+ );
+ let hdr = ColorSignal {
+ primaries: Primaries::BT2020,
+ transfer: Transfer::SMPTE2084,
+ matrix: Space::BT2020NCL,
+ range: Range::MPEG,
+ };
+ assert!(hdr.is_wide() && hdr.is_hdr());
+ let spec = video_filter(0, &options, 640, 360, Some(&hdr));
+ assert!(spec.starts_with("scale=640:360:flags=bilinear:in_primaries=bt2020:in_transfer=smpte2084:in_color_matrix=bt2020nc:in_range=tv:out_primaries=bt709"), "{spec}");
+ assert!(spec.contains("intent=perceptual,format=rgba"), "{spec}");
+ // wide gamut with an SDR curve converts too, and the untagged
+ // half of it is left to the frames
+ let wide = ColorSignal {
+ primaries: Primaries::BT2020,
+ transfer: Transfer::Unspecified,
+ matrix: Space::Unspecified,
+ range: Range::Unspecified,
+ };
+ assert!(wide.is_wide() && !wide.is_hdr());
+ assert!(
+ video_filter(0, &options, 64, 64, Some(&wide))
+ .contains(":in_transfer=auto:in_color_matrix=auto:in_range=auto:")
+ );
+ }
+
+ /// A PQ-tagged ten-bit HEVC file, made with the encoder's own machinery,
+ /// decodes through the tone-map to frames that are neither the black
+ /// nor the blown-out white a curve mismatch gives.
+ #[test]
+ fn an_hdr_file_decodes_through_the_tone_map() {
+ use crate::encode::{EncodeOptions, Encoder, FrameSink, VideoCodec};
+ use ffmpeg::color::{Primaries, Space, TransferCharacteristic as Transfer};
+ if !VideoCodec::Hevc.available() {
+ eprintln!("no HEVC encoder in the linked FFmpeg; skipped");
+ return;
+ }
+ let path = std::env::temp_dir().join("concat-decode-hdr-test.mp4");
+ let options = EncodeOptions {
+ codec: VideoCodec::Hevc,
+ preset: "ultrafast".to_owned(),
+ crf: 20,
+ ten_bit: true,
+ hardware: false,
+ };
+ let mut encoder = Encoder::create_tagged(
+ &path,
+ 64,
+ 64,
+ FrameRate::THIRTY,
+ &options,
+ (Primaries::BT2020, Transfer::SMPTE2084, Space::BT2020NCL),
+ )
+ .expect("an HEVC encoder");
+ let mut frame = Frame::black(64, 64);
+ frame.fill([180, 180, 180, 255]);
+ for _ in 0..3 {
+ encoder.write_frame(&frame).expect("writes");
+ }
+ encoder.finish().expect("finishes");
+
+ let mut decoder = Decoder::open(&path, &DecodeOptions::default()).expect("opens");
+ assert!(decoder.color().is_hdr(), "{:?}", decoder.color());
+ let decoded = decoder.next_frame().expect("decodes").expect("a frame");
+ let _ = std::fs::remove_file(&path);
+ let luma = decoded.pixels()[0];
+ assert!(luma > 8 && luma < 250, "tone-mapped grey came out {luma}");
}
#[test]
diff --git a/engine/crates/concat-media/src/encode.rs b/engine/crates/concat-media/src/encode.rs
index 33334639..8aab0818 100644
--- a/engine/crates/concat-media/src/encode.rs
+++ b/engine/crates/concat-media/src/encode.rs
@@ -30,40 +30,159 @@ pub trait FrameSink {
fn finish(&mut self) -> Result<()>;
}
+/// The codecs an export can be asked for, by the name of the standard, not
+/// of an encoder: which encoder makes it is the platform's business - see
+/// [`VideoCodec::encoders`].
+#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
+pub enum VideoCodec {
+ /// H.264 / AVC: plays everywhere, the largest files.
+ #[default]
+ H264,
+ /// H.265 / HEVC: about half the size of H.264 at the same quality; the
+ /// phones' and cameras' own format, and the one Apple's hardware
+ /// encodes.
+ Hevc,
+ /// AV1: smaller again, royalty-free, the web's; slower to encode in
+ /// software and not every old device plays it.
+ Av1,
+}
+
+impl VideoCodec {
+ /// Every codec, in the order a menu lists them.
+ pub const ALL: [VideoCodec; 3] = [VideoCodec::H264, VideoCodec::Hevc, VideoCodec::Av1];
+
+ /// The name a document or a request stores.
+ pub fn name(self) -> &'static str {
+ match self {
+ VideoCodec::H264 => "h264",
+ VideoCodec::Hevc => "hevc",
+ VideoCodec::Av1 => "av1",
+ }
+ }
+
+ /// The codec a stored name means, or `None` for one nobody stores.
+ pub fn parse(name: &str) -> Option<Self> {
+ match name.trim().to_ascii_lowercase().as_str() {
+ "h264" | "h.264" | "avc" => Some(VideoCodec::H264),
+ "hevc" | "h265" | "h.265" => Some(VideoCodec::Hevc),
+ "av1" => Some(VideoCodec::Av1),
+ _ => None,
+ }
+ }
+
+ /// What the standard is called on a label.
+ pub fn label(self) -> &'static str {
+ match self {
+ VideoCodec::H264 => "H.264",
+ VideoCodec::Hevc => "HEVC",
+ VideoCodec::Av1 => "AV1",
+ }
+ }
+
+ /// The FFmpeg encoders that make this codec, best first. With
+ /// `hardware`, the platform's own encoder leads where there is one
+ /// worth leading with: VideoToolbox for HEVC on macOS, which is many
+ /// times faster than x265 and, at these rates, as good to look at.
+ /// H.264 stays with x264 everywhere: the hardware H.264 encoders
+ /// spend noticeably more bits for the same picture, and H.264 is the
+ /// choice made for compatibility, not speed.
+ pub fn encoders(self, hardware: bool) -> &'static [&'static str] {
+ match self {
+ VideoCodec::H264 => &["libx264"],
+ VideoCodec::Hevc if hardware && cfg!(target_os = "macos") => {
+ &["hevc_videotoolbox", "libx265"]
+ }
+ VideoCodec::Hevc => &["libx265"],
+ VideoCodec::Av1 => &["libsvtav1", "libaom-av1"],
+ }
+ }
+
+ /// Whether the linked FFmpeg carries an encoder for it.
+ pub fn available(self) -> bool {
+ ffi::init();
+ self.encoders(true)
+ .iter()
+ .any(|name| encoder::find_by_name(name).is_some())
+ }
+
+ /// Bytes per second relative to H.264 at the same quality, for a size
+ /// estimate: the rule of thumb the codecs are chosen by.
+ pub fn size_factor(self) -> f32 {
+ match self {
+ VideoCodec::H264 => 1.0,
+ VideoCodec::Hevc => 0.6,
+ VideoCodec::Av1 => 0.5,
+ }
+ }
+}
+
/// Encoder settings.
#[derive(Clone, Debug)]
pub struct EncodeOptions {
- /// Video codec, as FFmpeg names it.
- pub codec: String,
- /// x264-style speed/size tradeoff.
+ /// What to encode to.
+ pub codec: VideoCodec,
+ /// x264's speed/size tradeoff, by x264's names; the other encoders
+ /// are handed their own equivalent.
pub preset: String,
- /// Constant rate factor. Lower is better quality and a bigger file.
+ /// Constant rate factor on x264's scale, lower being better quality
+ /// and a bigger file; the other encoders are handed their own
+ /// equivalent.
pub crf: u8,
- /// Output pixel format. `yuv420p` is what players actually accept.
- pub pixel_format: String,
+ /// Ten bits a channel rather than eight: no banding in a sky or a
+ /// gradient, at a few percent more file. H.264 at ten bits plays on
+ /// less than HEVC or AV1 at ten bits do.
+ pub ten_bit: bool,
+ /// Let the platform's hardware encoder lead where there is one; see
+ /// [`VideoCodec::encoders`].
+ pub hardware: bool,
}
impl Default for EncodeOptions {
fn default() -> Self {
Self {
- codec: "libx264".to_owned(),
+ codec: VideoCodec::H264,
preset: "medium".to_owned(),
crf: 18,
- pixel_format: "yuv420p".to_owned(),
+ ten_bit: false,
+ hardware: true,
}
}
}
-/// The output pixel formats an encoder can be asked for, by FFmpeg's names.
-fn pixel_format(name: &str) -> Option<Pixel> {
- Some(match name {
- "yuv420p" => Pixel::YUV420P,
- "yuv422p" => Pixel::YUV422P,
- "yuv444p" => Pixel::YUV444P,
- "nv12" => Pixel::NV12,
- "rgba" => Pixel::RGBA,
- _ => return None,
- })
+/// The x264 presets in speed order, which is also how SVT-AV1 numbers its
+/// own: 13 is the fastest there and 0 the slowest.
+const X264_PRESETS: [&str; 10] = [
+ "ultrafast",
+ "superfast",
+ "veryfast",
+ "faster",
+ "fast",
+ "medium",
+ "slow",
+ "slower",
+ "veryslow",
+ "placebo",
+];
+
+/// SVT-AV1's preset for an x264 one: the same place on its own scale.
+fn svt_preset(preset: &str) -> u8 {
+ const SVT: [u8; 10] = [12, 11, 10, 9, 8, 6, 4, 3, 2, 1];
+ X264_PRESETS
+ .iter()
+ .position(|name| *name == preset)
+ .map_or(6, |index| SVT[index])
+}
+
+/// VideoToolbox's quality, 1 to 100 with 100 the best, for an x264 CRF:
+/// 16 lands at 65 and 26 at 43, which is where the files come out about
+/// the size x264 makes them.
+fn videotoolbox_quality(crf: u8) -> u8 {
+ (100.0 - f32::from(crf) * 2.2).round().clamp(1.0, 100.0) as u8
+}
+
+/// A four-character code as the muxer stores it.
+const fn fourcc(tag: [u8; 4]) -> u32 {
+ u32::from_le_bytes(tag)
}
/// Encodes through libavcodec into a container libavformat writes.
@@ -80,16 +199,55 @@ pub struct Encoder {
height: u32,
written: u64,
finished: bool,
+ /// Which FFmpeg encoder took the job.
+ encoder_name: &'static str,
}
impl Encoder {
/// Opens `path` for writing, overwriting anything already there.
+ ///
+ /// The file is tagged BT.709, full stop: primaries, transfer and
+ /// matrix, video range. Every frame this is given is an sRGB picture,
+ /// and a file that does not say what it holds is shown however each
+ /// player guesses - the washed-out-on-the-phone complaint. The RGB to
+ /// YUV conversion uses the matching coefficients, so the tag is true.
pub fn create(
path: impl AsRef<Path>,
width: u32,
height: u32,
frame_rate: FrameRate,
options: &EncodeOptions,
+ ) -> Result<Self> {
+ use ffmpeg::color::{Primaries, Space, TransferCharacteristic};
+ Self::create_tagged(
+ path,
+ width,
+ height,
+ frame_rate,
+ options,
+ (
+ Primaries::BT709,
+ TransferCharacteristic::BT709,
+ Space::BT709,
+ ),
+ )
+ }
+
+ /// [`Encoder::create`] with the colour the file is tagged as. Crate
+ /// private, and only the tests use it for anything but BT.709: the
+ /// frames are sRGB whatever the tag says, so any other tag is a lie
+ /// - which is exactly what a test of the decoder's HDR path needs.
+ pub(crate) fn create_tagged(
+ path: impl AsRef<Path>,
+ width: u32,
+ height: u32,
+ frame_rate: FrameRate,
+ options: &EncodeOptions,
+ tags: (
+ ffmpeg::color::Primaries,
+ ffmpeg::color::TransferCharacteristic,
+ ffmpeg::color::Space,
+ ),
) -> Result<Self> {
ffi::init();
let path = path.as_ref();
@@ -97,14 +255,25 @@ impl Encoder {
let rate = ffmpeg::Rational::new(fps.numerator() as i32, fps.denominator() as i32);
let time_base = rate.invert();
- let codec = encoder::find_by_name(&options.codec).ok_or_else(|| Error::Missing {
- what: "encoder",
- name: options.codec.clone(),
- })?;
- let pixel_format = pixel_format(&options.pixel_format).ok_or_else(|| Error::Missing {
- what: "pixel format",
- name: options.pixel_format.clone(),
- })?;
+ // The first encoder the linked FFmpeg has for the codec.
+ let (encoder_name, codec) = options
+ .codec
+ .encoders(options.hardware)
+ .iter()
+ .find_map(|name| encoder::find_by_name(name).map(|codec| (*name, codec)))
+ .ok_or_else(|| Error::Missing {
+ what: "encoder",
+ name: options.codec.label().to_owned(),
+ })?;
+ let videotoolbox = encoder_name.ends_with("_videotoolbox");
+ // VideoToolbox takes its pictures planar-chroma; the software
+ // encoders take the planar 4:2:0 they all read.
+ let pixel_format = match (videotoolbox, options.ten_bit) {
+ (true, true) => Pixel::P010LE,
+ (true, false) => Pixel::NV12,
+ (false, true) => Pixel::YUV420P10LE,
+ (false, false) => Pixel::YUV420P,
+ };
let mut output =
ffmpeg::format::output(path).map_err(|error| ffi::fail("create", path, error))?;
@@ -122,14 +291,47 @@ impl Encoder {
video.set_format(pixel_format);
video.set_time_base(time_base);
video.set_frame_rate(Some(rate));
+ let (primaries, transfer, matrix) = tags;
+ video.set_colorspace(matrix);
+ video.set_color_range(ffmpeg::color::Range::MPEG);
+ // SAFETY: `video` owns a live AVCodecContext; primaries and
+ // transfer have no setter in the bindings, and both are plain
+ // fields the encoder reads at open.
+ unsafe {
+ let context = video.as_mut_ptr();
+ (*context).color_primaries = primaries.into();
+ (*context).color_trc = transfer.into();
+ }
if global_header {
video.set_flags(ffmpeg::codec::Flags::GLOBAL_HEADER);
}
- let encoder = video
- .open_with(ffmpeg::dict! {
+
+ let crf = options.crf.to_string();
+ let settings = match encoder_name {
+ "libx264" | "libx265" => ffmpeg::dict! {
"preset" => options.preset.as_str(),
- "crf" => &options.crf.to_string(),
- })
+ "crf" => crf.as_str(),
+ },
+ "libsvtav1" => ffmpeg::dict! {
+ "preset" => &svt_preset(&options.preset).to_string(),
+ // AV1's CRF runs to 63 and reads a few steps coarser than
+ // x264's; eight on is where the pictures match.
+ "crf" => &options.crf.saturating_add(8).min(63).to_string(),
+ },
+ "libaom-av1" => ffmpeg::dict! {
+ "crf" => &options.crf.saturating_add(8).min(63).to_string(),
+ "cpu-used" => "6",
+ },
+ "hevc_videotoolbox" => ffmpeg::dict! {
+ "q:v" => &videotoolbox_quality(options.crf).to_string(),
+ "profile" => if options.ten_bit { "main10" } else { "main" },
+ // A machine without the hardware still gets a file.
+ "allow_sw" => "1",
+ },
+ _ => ffmpeg::dict! {},
+ };
+ let encoder = video
+ .open_with(settings)
.map_err(|error| ffi::fail("open encoder", path, error))?;
{
@@ -138,6 +340,16 @@ impl Encoder {
.map_err(|error| ffi::fail("add stream", path, error))?;
stream.copy_parameters_from_context(&encoder);
stream.set_time_base(time_base);
+ if options.codec == VideoCodec::Hevc {
+ // `hvc1`, not FFmpeg's default `hev1`: the tag Apple's
+ // players and QuickTime need to open an HEVC file at all.
+ // SAFETY: the stream is live and its parameters were just
+ // copied; the tag is a plain field the muxer reads at the
+ // header.
+ unsafe {
+ (*(*stream.as_mut_ptr()).codecpar).codec_tag = fourcc(*b"hvc1");
+ }
+ }
}
output
.write_header_with(ffmpeg::dict! { "movflags" => "+faststart" })
@@ -147,7 +359,7 @@ impl Encoder {
.map(|stream| stream.time_base())
.unwrap_or(time_base);
- let scaler = scaling::Context::get(
+ let mut scaler = scaling::Context::get(
Pixel::RGBA,
width,
height,
@@ -157,6 +369,24 @@ impl Encoder {
scaling::Flags::BILINEAR,
)
.map_err(|error| ffi::fail("scaler", path, error))?;
+ // BT.709 coefficients from full-range RGB to video-range YUV,
+ // rather than swscale's BT.601 default: the file says 709, so the
+ // numbers in it are 709.
+ // SAFETY: `scaler` owns a live SwsContext; the coefficient tables
+ // are static and the call only sets fields on the context.
+ unsafe {
+ let coefficients = ffmpeg::sys::sws_getCoefficients(ffmpeg::sys::SWS_CS_ITU709);
+ ffmpeg::sys::sws_setColorspaceDetails(
+ scaler.as_mut_ptr(),
+ coefficients,
+ 1,
+ coefficients,
+ 0,
+ 0,
+ 1 << 16,
+ 1 << 16,
+ );
+ }
Ok(Self {
path: path.to_path_buf(),
@@ -169,9 +399,15 @@ impl Encoder {
height,
written: 0,
finished: false,
+ encoder_name,
})
}
+ /// Which FFmpeg encoder is doing the work, e.g. "hevc_videotoolbox".
+ pub fn encoder_name(&self) -> &'static str {
+ self.encoder_name
+ }
+
/// How many frames have been accepted so far.
pub const fn written(&self) -> u64 {
self.written
@@ -336,11 +572,102 @@ mod tests {
#[test]
fn defaults_are_a_playable_h264_file() {
let options = EncodeOptions::default();
- assert_eq!(options.codec, "libx264");
- assert_eq!(
- options.pixel_format, "yuv420p",
- "yuv444 will not play in browsers"
- );
+ assert_eq!(options.codec, VideoCodec::H264);
+ assert!(!options.ten_bit, "eight-bit H.264 is what plays everywhere");
+ assert_eq!(VideoCodec::parse("H.265"), Some(VideoCodec::Hevc));
+ assert_eq!(VideoCodec::parse("mpeg2"), None);
+ for codec in VideoCodec::ALL {
+ assert_eq!(VideoCodec::parse(codec.name()), Some(codec));
+ }
+ }
+
+ #[test]
+ fn presets_and_quality_map_onto_the_other_encoders() {
+ assert_eq!(svt_preset("medium"), 6);
+ assert_eq!(svt_preset("veryfast"), 10);
+ assert_eq!(svt_preset("nonsense"), 6);
+ assert_eq!(videotoolbox_quality(16), 65);
+ assert_eq!(videotoolbox_quality(26), 43);
+ assert_eq!(fourcc(*b"hvc1"), 0x3163_7668);
+ }
+
+ /// The tags a file carries, read back through libavformat.
+ fn tags_of(
+ path: &Path,
+ ) -> (
+ String,
+ u32,
+ ffmpeg::color::Primaries,
+ ffmpeg::color::TransferCharacteristic,
+ ffmpeg::color::Space,
+ Pixel,
+ ) {
+ let input = ffmpeg::format::input(path).expect("opens");
+ let stream = input
+ .streams()
+ .best(ffmpeg::media::Type::Video)
+ .expect("video");
+ let parameters = stream.parameters();
+ // SAFETY: the parameters are live for as long as `input` is, and
+ // the tag is a plain field.
+ let tag = unsafe { (*parameters.as_ptr()).codec_tag };
+ let context = ffmpeg::codec::Context::from_parameters(parameters).expect("context");
+ let decoder = context.decoder().video().expect("decoder");
+ (
+ decoder.id().name().to_owned(),
+ tag,
+ decoder.color_primaries(),
+ decoder.color_transfer_characteristic(),
+ decoder.color_space(),
+ decoder.format(),
+ )
+ }
+
+ /// Every codec the linked FFmpeg has, at eight and ten bits, makes a
+ /// file that says what it is: the codec, BT.709 all the way through,
+ /// the bit depth asked for, and `hvc1` on HEVC.
+ #[test]
+ fn every_available_codec_writes_a_tagged_file() {
+ for codec in VideoCodec::ALL {
+ if !codec.available() {
+ eprintln!("{} not in the linked FFmpeg; skipped", codec.label());
+ continue;
+ }
+ for ten_bit in [false, true] {
+ let path = std::env::temp_dir().join(format!(
+ "concat-encode-{}-{}.mp4",
+ codec.name(),
+ ten_bit
+ ));
+ let options = EncodeOptions {
+ codec,
+ preset: "ultrafast".to_owned(),
+ crf: 24,
+ ten_bit,
+ hardware: true,
+ };
+ let mut encoder = Encoder::create(&path, 64, 64, FrameRate::THIRTY, &options)
+ .unwrap_or_else(|error| panic!("{} {ten_bit}: {error}", codec.label()));
+ let mut frame = Frame::black(64, 64);
+ frame.fill([200, 40, 40, 255]);
+ for _ in 0..4 {
+ encoder.write_frame(&frame).expect("writes");
+ }
+ encoder.finish().expect("finishes");
+
+ let (name, tag, primaries, transfer, space, format) = tags_of(&path);
+ let _ = std::fs::remove_file(&path);
+ assert_eq!(name, codec.name(), "the stream's codec");
+ assert_eq!(primaries, ffmpeg::color::Primaries::BT709);
+ assert_eq!(transfer, ffmpeg::color::TransferCharacteristic::BT709);
+ assert_eq!(space, ffmpeg::color::Space::BT709);
+ let deep = matches!(format, Pixel::YUV420P10LE | Pixel::P010LE);
+ assert_eq!(deep, ten_bit, "{} bit depth ({format:?})", codec.label());
+ if codec == VideoCodec::Hevc {
+ assert_eq!(tag, fourcc(*b"hvc1"), "HEVC in MP4 is tagged hvc1");
+ }
+ }
+ }
}
#[test]
diff --git a/engine/crates/concat-media/src/lib.rs b/engine/crates/concat-media/src/lib.rs
index 0acb73b1..e1cfc30c 100644
--- a/engine/crates/concat-media/src/lib.rs
+++ b/engine/crates/concat-media/src/lib.rs
@@ -24,8 +24,8 @@ pub mod probe;
pub mod samples;
pub mod treat;
-pub use decode::{DecodeOptions, Decoder, FrameSource, SeekableSource};
-pub use encode::{EncodeOptions, Encoder, FrameSink, jpeg};
+pub use decode::{ColorSignal, DecodeOptions, Decoder, FrameSource, SeekableSource};
+pub use encode::{EncodeOptions, Encoder, FrameSink, VideoCodec, jpeg};
pub use error::{Error, Result};
pub use ffi::{init, linked_version};
pub use peaks::Peaks;
diff --git a/engine/crates/concat/locales/de.json b/engine/crates/concat/locales/de.json
index 2ef6ee55..411cd7ad 100644
--- a/engine/crates/concat/locales/de.json
+++ b/engine/crates/concat/locales/de.json
@@ -126,6 +126,12 @@
"Default title": "Standardtitel",
"Delay": "Verzögerung",
"Delete": "Löschen",
+ "Codec": "Codec",
+ "Plays everywhere": "Läuft überall",
+ "Half the size": "Halb so groß",
+ "Smallest": "Am kleinsten",
+ "10-bit colour": "10-Bit-Farbe",
+ "hardware": "Hardware",
"Detach audio": "Audio trennen",
"Reattach audio": "Audio wieder anhängen",
"Delete clip": "Clip löschen",
diff --git a/engine/crates/concat/locales/en.json b/engine/crates/concat/locales/en.json
index 21818099..bcae3230 100644
--- a/engine/crates/concat/locales/en.json
+++ b/engine/crates/concat/locales/en.json
@@ -3,6 +3,7 @@
"name": "English"
},
"1 stroke": "1 stroke",
+ "10-bit colour": "10-bit colour",
"A brighter, lighter, closer voice. Pitch at 0 follows the amount.": "A brighter, lighter, closer voice. Pitch at 0 follows the amount.",
"A concert hall: long, even reflections.": "A concert hall: long, even reflections.",
"A film curve with a real toe and shoulder, warm halation around the lights, and grain that moves. Print stock, not a filter.": "A film curve with a real toe and shoulder, warm halation around the lights, and grain that moves. Print stock, not a filter.",
@@ -118,6 +119,7 @@
"Close project": "Close project",
"Close window": "Close window",
"Cloudless Sky": "Cloudless Sky",
+ "Codec": "Codec",
"Color": "Color",
"Colour": "Colour",
"Colour bleeding sideways, scan lines, softness, tape noise and too much saturation, with the slight vertical smear of a worn head.": "Colour bleeding sideways, scan lines, softness, tape noise and too much saturation, with the slight vertical smear of a worn head.",
@@ -270,7 +272,9 @@
"Green Screen": "Green Screen",
"Greens": "Greens",
"Half": "Half",
+ "Half the size": "Half the size",
"Hall": "Hall",
+ "hardware": "hardware",
"Haze": "Haze",
"Headline": "Headline",
"Height": "Height",
@@ -437,6 +441,7 @@
"Plate colour": "Plate colour",
"Play": "Play",
"Playhead": "Playhead",
+ "Plays everywhere": "Plays everywhere",
"Polaroid": "Polaroid",
"Pop": "Pop",
"Portrait": "Portrait",
@@ -547,6 +552,7 @@
"Slide Up": "Slide Up",
"Slower": "Slower",
"Small": "Small",
+ "Smallest": "Smallest",
"Smart brush": "Smart brush",
"Smart brush: {0}": "Smart brush: {0}",
"Smart eraser": "Smart eraser",
diff --git a/engine/crates/concat/locales/es.json b/engine/crates/concat/locales/es.json
index 08c7fbe7..7fd46594 100644
--- a/engine/crates/concat/locales/es.json
+++ b/engine/crates/concat/locales/es.json
@@ -126,6 +126,12 @@
"Default title": "Título predeterminado",
"Delay": "Retardo",
"Delete": "Eliminar",
+ "Codec": "Códec",
+ "Plays everywhere": "Se reproduce en todas partes",
+ "Half the size": "La mitad de tamaño",
+ "Smallest": "El más pequeño",
+ "10-bit colour": "Color de 10 bits",
+ "hardware": "hardware",
"Detach audio": "Separar audio",
"Reattach audio": "Volver a unir el audio",
"Delete clip": "Eliminar clip",
diff --git a/engine/crates/concat/locales/fr.json b/engine/crates/concat/locales/fr.json
index de981951..2521571b 100644
--- a/engine/crates/concat/locales/fr.json
+++ b/engine/crates/concat/locales/fr.json
@@ -126,6 +126,12 @@
"Default title": "Titre par défaut",
"Delay": "Délai",
"Delete": "Supprimer",
+ "Codec": "Codec",
+ "Plays everywhere": "Lisible partout",
+ "Half the size": "Deux fois plus petit",
+ "Smallest": "Le plus petit",
+ "10-bit colour": "Couleur 10 bits",
+ "hardware": "matériel",
"Detach audio": "Détacher l'audio",
"Reattach audio": "Rattacher l'audio",
"Delete clip": "Supprimer le clip",
diff --git a/engine/crates/concat/locales/hr.json b/engine/crates/concat/locales/hr.json
index 4b7be231..e15a03c3 100644
--- a/engine/crates/concat/locales/hr.json
+++ b/engine/crates/concat/locales/hr.json
@@ -160,6 +160,12 @@
"Default title": "Zadani naslov",
"Delay": "Kašnjenje",
"Delete": "Obriši",
+ "Codec": "Kodek",
+ "Plays everywhere": "Radi svugdje",
+ "Half the size": "Upola manje",
+ "Smallest": "Najmanje",
+ "10-bit colour": "10-bitna boja",
+ "hardware": "hardver",
"Detach audio": "Odvoji zvuk",
"Reattach audio": "Ponovno spoji zvuk",
"Delete clip": "Obriši klip",
diff --git a/engine/crates/concat/locales/it.json b/engine/crates/concat/locales/it.json
index 52f68e6e..dcb8f756 100644
--- a/engine/crates/concat/locales/it.json
+++ b/engine/crates/concat/locales/it.json
@@ -126,6 +126,12 @@
"Default title": "Titolo predefinito",
"Delay": "Ritardo",
"Delete": "Elimina",
+ "Codec": "Codec",
+ "Plays everywhere": "Si riproduce ovunque",
+ "Half the size": "Metà delle dimensioni",
+ "Smallest": "Il più piccolo",
+ "10-bit colour": "Colore a 10 bit",
+ "hardware": "hardware",
"Detach audio": "Separa l'audio",
"Reattach audio": "Ricollega l'audio",
"Delete clip": "Elimina clip",
diff --git a/engine/crates/concat/locales/ja.json b/engine/crates/concat/locales/ja.json
index 9ba4e343..1360936d 100644
--- a/engine/crates/concat/locales/ja.json
+++ b/engine/crates/concat/locales/ja.json
@@ -126,6 +126,12 @@
"Default title": "デフォルトのタイトル",
"Delay": "ディレイ",
"Delete": "削除",
+ "Codec": "コーデック",
+ "Plays everywhere": "どこでも再生可能",
+ "Half the size": "半分のサイズ",
+ "Smallest": "最小",
+ "10-bit colour": "10ビットカラー",
+ "hardware": "ハードウェア",
"Detach audio": "音声を分離",
"Reattach audio": "音声を再結合",
"Delete clip": "クリップを削除",
diff --git a/engine/crates/concat/locales/ko.json b/engine/crates/concat/locales/ko.json
index f8d7b657..a4b47571 100644
--- a/engine/crates/concat/locales/ko.json
+++ b/engine/crates/concat/locales/ko.json
@@ -126,6 +126,12 @@
"Default title": "기본 자막",
"Delay": "지연",
"Delete": "삭제",
+ "Codec": "코덱",
+ "Plays everywhere": "어디서나 재생",
+ "Half the size": "절반 크기",
+ "Smallest": "가장 작음",
+ "10-bit colour": "10비트 색상",
+ "hardware": "하드웨어",
"Detach audio": "오디오 분리",
"Reattach audio": "오디오 다시 연결",
"Delete clip": "클립 삭제",
diff --git a/engine/crates/concat/locales/pt-BR.json b/engine/crates/concat/locales/pt-BR.json
index 819bee41..5c45511c 100644
--- a/engine/crates/concat/locales/pt-BR.json
+++ b/engine/crates/concat/locales/pt-BR.json
@@ -126,6 +126,12 @@
"Default title": "Título padrão",
"Delay": "Atraso",
"Delete": "Excluir",
+ "Codec": "Codec",
+ "Plays everywhere": "Reproduz em qualquer lugar",
+ "Half the size": "Metade do tamanho",
+ "Smallest": "O menor",
+ "10-bit colour": "Cor de 10 bits",
+ "hardware": "hardware",
"Detach audio": "Separar áudio",
"Reattach audio": "Reanexar áudio",
"Delete clip": "Excluir clipe",
diff --git a/engine/crates/concat/locales/ru.json b/engine/crates/concat/locales/ru.json
index a6f43475..8f61a253 100644
--- a/engine/crates/concat/locales/ru.json
+++ b/engine/crates/concat/locales/ru.json
@@ -126,6 +126,12 @@
"Default title": "Титр по умолчанию",
"Delay": "Задержка",
"Delete": "Удалить",
+ "Codec": "Кодек",
+ "Plays everywhere": "Воспроизводится везде",
+ "Half the size": "Вдвое меньше",
+ "Smallest": "Самый маленький",
+ "10-bit colour": "10-битный цвет",
+ "hardware": "аппаратно",
"Detach audio": "Отделить звук",
"Reattach audio": "Присоединить звук обратно",
"Delete clip": "Удалить клип",
diff --git a/engine/crates/concat/locales/tr.json b/engine/crates/concat/locales/tr.json
index 3232a0e5..3b7b61d5 100644
--- a/engine/crates/concat/locales/tr.json
+++ b/engine/crates/concat/locales/tr.json
@@ -126,6 +126,12 @@
"Default title": "Varsayılan yazı",
"Delay": "Gecikme",
"Delete": "Sil",
+ "Codec": "Kodek",
+ "Plays everywhere": "Her yerde oynatılır",
+ "Half the size": "Yarı boyut",
+ "Smallest": "En küçük",
+ "10-bit colour": "10 bit renk",
+ "hardware": "donanım",
"Detach audio": "Sesi ayır",
"Reattach audio": "Sesi yeniden bağla",
"Delete clip": "Klibi sil",
diff --git a/engine/crates/concat/locales/zh-Hans.json b/engine/crates/concat/locales/zh-Hans.json
index 4042d5ef..26acd1dc 100644
--- a/engine/crates/concat/locales/zh-Hans.json
+++ b/engine/crates/concat/locales/zh-Hans.json
@@ -126,6 +126,12 @@
"Default title": "默认字幕",
"Delay": "延迟",
"Delete": "删除",
+ "Codec": "编码",
+ "Plays everywhere": "到处都能播放",
+ "Half the size": "体积减半",
+ "Smallest": "最小",
+ "10-bit colour": "10 位色彩",
+ "hardware": "硬件",
"Detach audio": "分离音频",
"Reattach audio": "重新附加音频",
"Delete clip": "删除片段",
diff --git a/engine/crates/concat/locales/zh-TW.json b/engine/crates/concat/locales/zh-TW.json
index 3a76914d..7c9e9acd 100644
--- a/engine/crates/concat/locales/zh-TW.json
+++ b/engine/crates/concat/locales/zh-TW.json
@@ -160,6 +160,12 @@
"Default title": "預設標題",
"Delay": "延遲時間",
"Delete": "刪除",
+ "Codec": "編碼",
+ "Plays everywhere": "到處都能播放",
+ "Half the size": "體積減半",
+ "Smallest": "最小",
+ "10-bit colour": "10 位元色彩",
+ "hardware": "硬體",
"Detach audio": "分離音訊",
"Reattach audio": "重新附加音訊",
"Delete clip": "刪除片段",
diff --git a/engine/crates/concat/src/lib.rs b/engine/crates/concat/src/lib.rs
index 1455f961..4e734512 100644
--- a/engine/crates/concat/src/lib.rs
+++ b/engine/crates/concat/src/lib.rs
@@ -935,6 +935,12 @@ pub fn run() -> Result<(), slint::PlatformError> {
app.on_export_quality_changed(on_window!(|state, index: i32| {
state.export.quality = (index.max(0) as usize).min(2);
}));
+ app.on_export_codec_changed(on_window!(|state, index: i32| {
+ state.export.codec = (index.max(0) as usize).min(2);
+ }));
+ app.on_export_ten_bit_changed(on_window!(|state, on: bool| {
+ state.export.ten_bit = on;
+ }));
app.on_export_again(on_window!(|state| {
state.export.phase = ExportPhase::Idle;
state.export.progress = 0.0;
diff --git a/engine/crates/concat/src/studio.rs b/engine/crates/concat/src/studio.rs
index 11f67e93..6aaeba2d 100644
--- a/engine/crates/concat/src/studio.rs
+++ b/engine/crates/concat/src/studio.rs
@@ -265,6 +265,9 @@ pub struct ExportState {
pub resolution: usize,
pub rate: usize,
pub quality: usize,
+ /// Index into `VideoCodec::ALL`.
+ pub codec: usize,
+ pub ten_bit: bool,
pub phase: ExportPhase,
pub progress: f32,
pub stage: String,
@@ -284,6 +287,8 @@ impl Default for ExportState {
resolution: 2,
rate: 1,
quality: 1,
+ codec: 0,
+ ten_bit: false,
phase: ExportPhase::Idle,
progress: 0.0,
stage: String::new(),
@@ -5137,10 +5142,23 @@ impl Studio {
let (num, den) = EXPORT_RATES[self.export.rate.min(2)];
let rate = num as f32 / den as f32;
let pixels = (width as f32 * height as f32) / (1920.0 * 1080.0);
- let video = EXPORT_TIERS[tier.min(2)] * 1_000_000.0 * pixels * (rate / 30.0);
+ let video = EXPORT_TIERS[tier.min(2)]
+ * 1_000_000.0
+ * pixels
+ * (rate / 30.0)
+ * self.export_codec().size_factor()
+ * if self.export.ten_bit { 1.05 } else { 1.0 };
(video + AUDIO_BPS) * self.duration().max(1.0) / 8.0
}
+ /// The codec the sheet has chosen.
+ pub fn export_codec(&self) -> concat_media::VideoCodec {
+ concat_media::VideoCodec::ALL[self
+ .export
+ .codec
+ .min(concat_media::VideoCodec::ALL.len() - 1)]
+ }
+
/// Starts the render on a worker, reporting into the sheet.
pub fn export_start(&mut self) {
let Some(session) = self.session.as_ref() else {
@@ -5168,6 +5186,8 @@ impl Studio {
output: output.clone(),
crf: EXPORT_CRF[self.export.quality.min(2)],
preset: "veryfast".into(),
+ codec: self.export_codec(),
+ ten_bit: self.export.ten_bit,
};
let (frame_w, frame_h) = self.output_size();
let titles = self
@@ -6948,6 +6968,27 @@ impl Studio {
resolution: self.export.resolution as i32,
rate: self.export.rate as i32,
quality: self.export.quality as i32,
+ codec: self.export.codec as i32,
+ ten_bit: self.export.ten_bit,
+ encoding: {
+ // "HEVC 10-bit · hardware": the standard, the depth when it
+ // is the deeper one, and whether the platform's own encoder
+ // will be doing it.
+ let codec = self.export_codec();
+ let mut words = vec![codec.label().to_owned()];
+ if self.export.ten_bit {
+ words.push("10-bit".to_owned());
+ }
+ if codec
+ .encoders(true)
+ .first()
+ .is_some_and(|name| name.ends_with("_videotoolbox"))
+ {
+ words.push(format!("· {}", t("hardware")));
+ }
+ words.join(" ")
+ }
+ .into(),
size_high: bytes(self.export_size_bytes(0)).into(),
size_balanced: bytes(self.export_size_bytes(1)).into(),
size_small: bytes(self.export_size_bytes(2)).into(),
diff --git a/engine/crates/concat/ui/app.slint b/engine/crates/concat/ui/app.slint
index de88a64b..860aa090 100644
--- a/engine/crates/concat/ui/app.slint
+++ b/engine/crates/concat/ui/app.slint
@@ -176,6 +176,8 @@ export component App inherits Window {
callback export-resolution-changed(index: int);
callback export-rate-changed(index: int);
callback export-quality-changed(index: int);
+ callback export-codec-changed(index: int);
+ callback export-ten-bit-changed(on: bool);
callback export-start();
callback export-cancel();
callback export-reveal();
@@ -441,6 +443,8 @@ export component App inherits Window {
resolution-changed(index) => { root.export-resolution-changed(index); }
rate-changed(index) => { root.export-rate-changed(index); }
quality-changed(index) => { root.export-quality-changed(index); }
+ codec-changed(index) => { root.export-codec-changed(index); }
+ ten-bit-changed(on) => { root.export-ten-bit-changed(on); }
begin => { root.export-start(); }
cancel => { root.export-cancel(); }
reveal => { root.export-reveal(); }
diff --git a/engine/crates/concat/ui/dialogs/export.slint b/engine/crates/concat/ui/dialogs/export.slint
index a5d2c99e..aee77560 100644
--- a/engine/crates/concat/ui/dialogs/export.slint
+++ b/engine/crates/concat/ui/dialogs/export.slint
@@ -6,6 +6,7 @@ import { Theme } from "../theme/theme.slint";
import { Glyph, Icon } from "../icons.slint";
import { Button, CopyChip, DialogRow, DialogSection, Field, Modal, Progress } from "../primitives/modal.slint";
import { Select } from "../primitives/select.slint";
+import { Switch } from "../inspector/controls.slint";
// The export dialog.
//
@@ -30,6 +31,11 @@ export struct ExportData {
resolution: int,
rate: int,
quality: int,
+ /// index into the codec list: H.264, HEVC, AV1
+ codec: int,
+ ten-bit: bool,
+ /// "HEVC 10-bit · hardware": what the file will be encoded as
+ encoding: string,
/// What each preset would weigh. Three strings rather than a list: a Slint
/// struct cannot hold an array.
size-high: string,
@@ -152,6 +158,8 @@ export component ExportDialog inherits Modal {
callback resolution-changed(index: int);
callback rate-changed(index: int);
callback quality-changed(index: int);
+ callback codec-changed(index: int);
+ callback ten-bit-changed(on: bool);
callback begin();
callback cancel();
callback reveal();
@@ -182,7 +190,7 @@ export component ExportDialog inherits Modal {
VerticalLayout {
spacing: 1px;
- Fact { label: I18n.t("Format"); value: root.data.format + " · H.264"; }
+ Fact { label: I18n.t("Format"); value: root.data.format + " · " + root.data.encoding; }
Fact { label: I18n.t("Duration"); value: root.data.duration; }
Fact { label: I18n.t("Contents"); value: root.data.contents; }
}
@@ -238,6 +246,33 @@ export component ExportDialog inherits Modal {
changed(index) => { root.rate-changed(index); }
}
}
+
+ // The standard, not the encoder: which encoder makes it is the
+ // platform's business, and the Format line above says when
+ // the hardware one will.
+ DialogRow {
+ label: I18n.t("Codec");
+ Select {
+ current: root.data.codec;
+ options: ["H.264", "HEVC", "AV1"];
+ details: [I18n.t("Plays everywhere"), I18n.t("Half the size"), I18n.t("Smallest")];
+ changed(index) => { root.codec-changed(index); }
+ }
+ }
+
+ DialogRow {
+ label: I18n.t("10-bit colour");
+ HorizontalLayout {
+ alignment: end;
+ VerticalLayout {
+ alignment: center;
+ Switch {
+ checked: root.data.ten-bit;
+ toggled(on) => { root.ten-bit-changed(on); }
+ }
+ }
+ }
+ }
}
if !root.running: DialogSection {