The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
feat(hdr): an HDR timeline exports as HDR - HEVC or AV1 in ten bits, BT.2020 with HLG or PQ - or tone-mapped SDR
Jareer2da3b9e8892a
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex f3a4a7f1..c3344305 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -114,9 +114,14 @@ flowchart LRtransfer undone, the primaries brought to Rec. 709's, and, on an SDRtimeline, conformed to it by BT.2390's roll-off. On an HDR timeline itkeeps its light above white (`FramePlan::output`), and the resolve rolls- the whole frame off for an SDR screen instead, with the same maths. A clip- with an FFmpeg chain or a cutout keeps the eight-bit tone map in the- decoder.+ the whole frame off for an SDR screen instead, with the same maths. An+ HDR file (`Compositor::deliver_hdr`) is resolved instead into its signal+ on Rec. 2020 - HLG for a 1000-nit display, or PQ - as sixteen-bit+ integers, read back as RGBA64 and written by `Encoder::create_hdr`: HEVC+ or AV1 in ten bits, BT.2020 tags, and for PQ the mastering display and+ the MaxCLL and MaxFALL measured as it is written. A clip with an FFmpeg+ chain or a cutout keeps the eight-bit tone map in the decoder, and an HDR+ export leaves an old package's FFmpeg chain out.Effect packages written forgamma-encoded `0..1` see that through the prelude's `legacy_in` and`legacy_out` (`concat-effects/src/shader.rs`) until the GPU-only effectsdiff --git a/src/crates/concat-api/src/lib.rs b/src/crates/concat-api/src/lib.rsindex fefa2509..d5e35802 100644--- a/src/crates/concat-api/src/lib.rs+++ b/src/crates/concat-api/src/lib.rs@@ -537,12 +537,19 @@ impl Api {let rate_num = spec.rate_num.unwrap_or(settings.rate_num);let rate_den = spec.rate_den.unwrap_or(settings.rate_den);checked_rate(rate_num, rate_den)?;+ let hdr = session.video().color_space.is_hdr() && spec.hdr.unwrap_or(true);let codec = match spec.codec.as_deref() {+ None if hdr => export::VideoCodec::Hevc,None => export::VideoCodec::H264,Some(name) => export::VideoCodec::parse(name).ok_or_else(|| {ApiError::invalid(format!("unknown codec {name:?}: h264, hevc or av1"))})?,};+ if hdr && codec == export::VideoCodec::H264 {+ return Err(ApiError::invalid(+ "H.264 does not carry HDR: hevc or av1, or hdr false for SDR".to_owned(),+ ));+ }let color_range = match spec.color_range.as_deref() {None => export::ColorRange::Limited,Some(name) => export::ColorRange::parse(name).ok_or_else(|| {@@ -561,6 +568,7 @@ impl Api {rate_mode: export::RateMode::Vbr,bitrate_kbps: 0,color_range,+ hdr,};let project_path = session.path().to_owned();diff --git a/src/crates/concat-api/src/message.rs b/src/crates/concat-api/src/message.rsindex 30eee713..6a532967 100644--- a/src/crates/concat-api/src/message.rs+++ b/src/crates/concat-api/src/message.rs@@ -271,7 +271,8 @@ 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.+ /// "h264", "hevc" or "av1". H.264 when absent - HEVC for an HDR file,+ /// which H.264 does not carry.#[serde(default)]pub codec: Option<String>,/// Ten bits a channel. Eight when absent.@@ -283,6 +284,11 @@ pub struct ExportSpec {/// https://github.com/jub0t/Concat/issues/103#[serde(default)]pub color_range: Option<String>,+ /// For an HDR timeline: written as HDR, its own HLG or PQ in ten bits+ /// (true, the default), or tone-mapped to SDR (false). Nothing for an+ /// SDR timeline.+ #[serde(default)]+ pub hdr: Option<bool>,}/// What a request hands back. Serialised as the payload alone: the variantdiff --git a/src/crates/concat-export/src/lib.rs b/src/crates/concat-export/src/lib.rsindex 594941f8..95188fbb 100644--- a/src/crates/concat-export/src/lib.rs+++ b/src/crates/concat-export/src/lib.rs@@ -358,10 +358,14 @@ pub struct ExportRequest {#[serde(default, deserialize_with = "range_by_name")]pub color_range: concat_media::ColorRange,/// What the timeline is output in. SDR when a request does not say.- /// Until the HDR export lands an HDR timeline is written tone-mapped- /// to SDR, as the monitor shows it.#[serde(default)]pub color_space: ColorSpace,+ /// An HDR timeline written as HDR - HEVC or AV1 in ten bits, BT.2020+ /// with its HLG or PQ - rather than tone-mapped to SDR as the monitor+ /// shows it. Nothing for an SDR timeline. False when a request does not+ /// say.+ #[serde(default)]+ pub hdr: bool,/// The flattened clip list to render.pub clips: Vec<ExportClip>,}@@ -1125,23 +1129,34 @@ fn render_picture(highlight: _,} = build_timeline(request, rate, visible, gpu, transitions);- let mut encoder = Encoder::create(- destination,- request.width,- request.height,- rate,- &EncodeOptions {- crf: request.crf,- preset: request.preset.clone(),- codec: request.codec,- rate_mode: request.rate_mode,- bitrate_kbps: request.bitrate_kbps,- ten_bit: request.ten_bit,- color_range: request.color_range,- hardware: true,- threads: 0,- },- )+ let options = EncodeOptions {+ crf: request.crf,+ preset: request.preset.clone(),+ codec: request.codec,+ rate_mode: request.rate_mode,+ bitrate_kbps: request.bitrate_kbps,+ ten_bit: request.ten_bit,+ color_range: request.color_range,+ hardware: true,+ threads: 0,+ };+ // An HDR file comes out of the compositor as its signal, sixteen bits a+ // channel; anything else, SDR - an HDR timeline's tone-mapped.+ let signal = output_of(request.color_space);+ let hdr = request.hdr && signal != Signal::Sdr;+ compositor.deliver_hdr(hdr);+ let mut encoder = if hdr {+ Encoder::create_hdr(+ destination,+ request.width,+ request.height,+ rate,+ &options,+ signal,+ )+ } else {+ Encoder::create(destination, request.width, request.height, rate, &options)+ }.map_err(|error| error.to_string())?;// One decoder per clip, opened at its in-point the first time the clip is@@ -1288,6 +1303,7 @@ fn render_picture(},&treatments,&transitions,+ hdr,);if compositor.lost() {return Err("the GPU device was lost part way through the export".to_owned());@@ -1352,6 +1368,7 @@ fn composite_treated(mut plan: FramePlan,treatments: &[Treatment],transitions: &[TransitionSpan],+ hdr: bool,) -> Frame {let time = plan.time;// A packaged transition live at this instant combines the outgoing stack@@ -1376,7 +1393,10 @@ fn composite_treated(return compositor.render(&plan);}live.sort_by_key(|treatment| treatment.track);- if live.iter().all(|treatment| treatment.chain.is_empty()) {+ // An HDR file stays on the GPU in its own light: an old package's+ // FFmpeg chain, which works on eight-bit SDR, is left out of it until+ // those packages are retired (step 5, slice 8).+ if hdr || live.iter().all(|treatment| treatment.chain.is_empty()) {plan.treatments = live.iter().map(|treatment| PlannedTreatment {@@ -1615,6 +1635,7 @@ impl PreviewSources {self.plan.clone(),&self.treatments,&self.transitions,+ false,)}@@ -1869,6 +1890,7 @@ pub fn preview_plan(bitrate_kbps: 0,color_range: concat_media::ColorRange::Limited,color_space,+ hdr: false,clips: Vec::new(),};PreviewPlan {@@ -2104,6 +2126,7 @@ mod tests {stack(Rational::from_int(1)),std::slice::from_ref(&negate),&[],+ false,);// Red negated is cyan where nothing sits on top...let at = |x: usize, y: usize| &out.pixels()[(y * 8 + x) * 4..(y * 8 + x) * 4 + 3];@@ -2116,7 +2139,13 @@ mod tests {strength: 0.5,..negate.clone()};- let out = composite_treated(&mut compositor, stack(Rational::from_int(1)), &[half], &[]);+ let out = composite_treated(+ &mut compositor,+ stack(Rational::from_int(1)),+ &[half],+ &[],+ false,+ );let pixel = &out.pixels()[(7 * 8 + 7) * 4..(7 * 8 + 7) * 4 + 3];assert!(pixel[0] > 120 && pixel[0] < 136, "{pixel:?}");assert!(pixel[1] > 120 && pixel[1] < 136, "{pixel:?}");@@ -2127,6 +2156,7 @@ mod tests {stack(Rational::from_int(20)),&[negate],&[],+ false,);assert_eq!(at_of(&out, 7, 7), [255, 0, 0]);fn at_of(frame: &Frame, x: usize, y: usize) -> [u8; 3] {diff --git a/src/crates/concat-host/src/export.rs b/src/crates/concat-host/src/export.rsindex fe3f9406..a4241d1b 100644--- a/src/crates/concat-host/src/export.rs+++ b/src/crates/concat-host/src/export.rs@@ -41,6 +41,9 @@ pub struct ExportSpec {/// 16-235, unless the sheet's Advanced section says full./// https://github.com/jub0t/Concat/issues/103pub color_range: ColorRange,+ /// An HDR timeline written as HDR, its own HLG or PQ, rather than+ /// tone-mapped to SDR. Nothing for an SDR timeline.+ pub hdr: bool,}/// One progress report: which frame of how many, in which stage.@@ -73,6 +76,7 @@ pub fn request(session: &Session, spec: &ExportSpec, titles: Vec<ExportClip>) ->bitrate_kbps: spec.bitrate_kbps,color_range: spec.color_range,color_space: session.video().color_space,+ hdr: spec.hdr,clips,}}diff --git a/src/crates/concat-host/tests/export.rs b/src/crates/concat-host/tests/export.rsindex 1823f124..6642c105 100644--- a/src/crates/concat-host/tests/export.rs+++ b/src/crates/concat-host/tests/export.rs@@ -577,6 +577,7 @@ impl Studio {rate_mode: RateMode::Vbr,bitrate_kbps: 0,color_range: self.range,+ hdr: false,};let titles = self.titles@@ -974,6 +975,126 @@ fn an_hlg_clip_exports_through_the_gpu_conversion() {);}+/// An iPhone's HLG clip turns the timeline HLG, and the timeline exports as+/// HDR: HEVC in ten bits, BT.2020 and HLG, the grey the clip was written+/// as coming back as the same signal. The same timeline written SDR is+/// BT.709 in eight bits, its grey tone-mapped as the monitor shows it.+#[test]+fn an_hlg_timeline_exports_as_hdr_or_sdr() {+ use concat_core::frame::{Depth, Signal};+ use concat_media::{DecodeOptions, Decoder, FrameSource};+ if !VideoCodec::Hevc.available() {+ eprintln!("no HEVC encoder in the linked FFmpeg; skipped");+ return;+ }+ const LEVEL: f64 = 0.6;+ let scratch = Scratch::new("hlg-out");+ let path = scratch.path().join("iphone.mp4");+ let options = EncodeOptions {+ codec: VideoCodec::Hevc,+ preset: "ultrafast".to_owned(),+ crf: 12,+ rate_mode: RateMode::Vbr,+ bitrate_kbps: 0,+ ten_bit: true,+ color_range: concat_media::ColorRange::Limited,+ hardware: false,+ threads: 0,+ };+ {+ let mut encoder = Encoder::create_hdr(+ &path,+ WIDTH,+ HEIGHT,+ FrameRate::THIRTY,+ &options,+ Signal::Hlg,+ )+ .expect("an HEVC encoder");+ let value = (LEVEL * 65_535.0).round() as u16;+ let pixel: Vec<u8> = [value, value, value, u16::MAX]+ .iter()+ .flat_map(|channel| channel.to_le_bytes())+ .collect();+ let frame = Frame::from_rgba64(+ WIDTH,+ HEIGHT,+ pixel.repeat((WIDTH * HEIGHT) as usize),+ Signal::Hlg,+ )+ .expect("a deep frame");+ for _ in 0..30 {+ encoder.write_frame(&frame).expect("writes");+ }+ encoder.finish().expect("finishes");+ }+ let mut studio = Studio::new(scratch.path(), "HLG out", video(WIDTH, HEIGHT, 30, 1));+ let media = studio.import(&path);+ studio.apply(Command::AddClipAtFirstFree {+ media_id: media,+ start: 0.0,+ });+ assert_eq!(+ studio.project().active().video.color_space,+ concat_project::model::ColorSpace::Hlg,+ "the timeline turned HLG with its first HDR clip"+ );++ let written = |hdr: bool, codec: VideoCodec| {+ let spec = ExportSpec {+ output: scratch+ .path()+ .join(format!("out-{hdr}.mp4"))+ .to_string_lossy()+ .into_owned(),+ crf: 12,+ preset: "ultrafast".to_owned(),+ codec,+ ten_bit: false,+ rate_mode: RateMode::Vbr,+ bitrate_kbps: 0,+ color_range: concat_media::ColorRange::Limited,+ hdr,+ };+ let request = export::request(&studio.session, &spec, Vec::new());+ export::run(&request, &AtomicBool::new(false), |_| {})+ .unwrap_or_else(|error| panic!("hdr {hdr}: the export failed: {error}"))+ };++ let hdr = written(true, VideoCodec::Hevc);+ let info = concat_media::probe::probe(&hdr).expect("probes");+ assert_eq!(info.video.expect("a picture").signal, Signal::Hlg);+ let mut decoder =+ Decoder::open(&hdr, &DecodeOptions::default().deep(true)).expect("opens the export");+ let frame = decoder.next_frame().expect("decodes").expect("a frame");+ assert_eq!(+ (frame.depth(), frame.signal()),+ (Depth::Sixteen, Signal::Hlg)+ );+ let at = ((HEIGHT / 2 * WIDTH + WIDTH / 2) * 8) as usize;+ let got = f64::from(u16::from_le_bytes([+ frame.pixels()[at],+ frame.pixels()[at + 1],+ ])) / 65_535.0;+ assert!(+ (got - LEVEL).abs() < 0.015,+ "the HLG grey {LEVEL} came back {got}"+ );++ let sdr = written(false, VideoCodec::H264);+ let info = concat_media::probe::probe(&sdr).expect("probes");+ assert_eq!(info.video.expect("a picture").signal, Signal::Sdr);+ let exported = Exported::read("SDR", Path::new(&sdr), (30, 1));+ let [r, g, b] = exported.colour_at(0.5);+ assert!(+ [r, g, b].iter().all(|channel| (60..=230).contains(channel))+ && r.abs_diff(g) <= 6+ && g.abs_diff(b) <= 6,+ "the HLG grey written SDR came out {:?}",+ [r, g, b]+ );+}+/// Issue #103: the levels a file is read as reach the export. A source/// written video range reads as it is by default; told it is full range,/// its levels expand and the colour of a second is no longer the colourdiff --git a/src/crates/concat-media/src/encode.rs b/src/crates/concat-media/src/encode.rsindex bdb3f1ef..2c01bc2f 100644--- a/src/crates/concat-media/src/encode.rs+++ b/src/crates/concat-media/src/encode.rs@@ -5,7 +5,7 @@use std::path::{Path, PathBuf};-use concat_core::frame::Frame;+use concat_core::frame::{Depth, Frame, Signal};use concat_core::time::FrameRate;use ffmpeg_the_third as ffmpeg;use ffmpeg_the_third::codec::encoder;@@ -256,6 +256,113 @@ pub struct Encoder {/// The range every converted frame is stamped with, matching the/// stream's tag and the scaler's conversion.color_range: ffmpeg::color::Range,+ /// The frames are sixteen bits a channel, Rec. 2020 with the transfer+ /// applied (an HDR file), rather than eight-bit sRGB.+ deep: bool,+ /// A PQ file's light, measured as it is written, for the metadata the+ /// file carries once it is finished.+ light: Option<LightLevel>,+}++/// The mastering display an HDR10 file names: a P3 display of 1000 nits+/// over 0.0001, the reference most HDR is graded on. FFmpeg's+/// `AVMasteringDisplayMetadata`, whose header the bindings leave out.+#[repr(C)]+struct MasteringDisplay {+ /// CIE 1931 xy of the red, green and blue primaries.+ display_primaries: [[ffmpeg::sys::AVRational; 2]; 3],+ white_point: [ffmpeg::sys::AVRational; 2],+ min_luminance: ffmpeg::sys::AVRational,+ max_luminance: ffmpeg::sys::AVRational,+ has_primaries: std::os::raw::c_int,+ has_luminance: std::os::raw::c_int,+}++impl MasteringDisplay {+ fn p3_1000() -> Self {+ let xy = |x: i32, y: i32| {+ [+ ffmpeg::sys::AVRational {+ num: x,+ den: 50_000,+ },+ ffmpeg::sys::AVRational {+ num: y,+ den: 50_000,+ },+ ]+ };+ MasteringDisplay {+ display_primaries: [xy(34_000, 16_000), xy(13_250, 34_500), xy(7_500, 3_000)],+ white_point: xy(15_635, 16_450),+ min_luminance: ffmpeg::sys::AVRational {+ num: 1,+ den: 10_000,+ },+ max_luminance: ffmpeg::sys::AVRational { num: 1_000, den: 1 },+ has_primaries: 1,+ has_luminance: 1,+ }+ }+}++/// The same display as x265 and SVT-AV1 spell it.+const X265_MASTER_DISPLAY: &str =+ "G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,1)";+const SVT_MASTER_DISPLAY: &str =+ "G(0.265,0.690)B(0.150,0.060)R(0.680,0.320)WP(0.3127,0.3290)L(1000,0.0001)";++/// FFmpeg's `AVContentLightMetadata`: the brightest pixel and the brightest+/// frame's average, in nits.+#[repr(C)]+struct ContentLight {+ max_cll: std::os::raw::c_uint,+ max_fall: std::os::raw::c_uint,+}++/// MaxCLL and MaxFALL as a PQ file is written: every pixel's brightest+/// channel in nits, through a table of the sixteen-bit signal.+struct LightLevel {+ nits: Box<[f32]>,+ max_cll: f32,+ max_fall: f32,+}++impl LightLevel {+ fn new() -> Self {+ // SMPTE ST 2084's EOTF, a signal to nits.+ let (m1, m2) = (0.159_301_76_f64, 78.843_75_f64);+ let (c1, c2, c3) = (0.835_937_5_f64, 18.851_562_5_f64, 18.687_5_f64);+ let nits = (0..=u16::MAX)+ .map(|code| {+ let e = (f64::from(code) / 65_535.0).powf(1.0 / m2);+ let y = ((e - c1).max(0.0) / (c2 - c3 * e)).powf(1.0 / m1);+ (y * 10_000.0) as f32+ })+ .collect();+ LightLevel {+ nits,+ max_cll: 0.0,+ max_fall: 0.0,+ }+ }++ /// One frame's pixels, RGBA sixteen bits a channel, little-endian.+ fn measure(&mut self, pixels: &[u8]) {+ let mut sum = 0.0f64;+ let mut count = 0usize;+ for pixel in pixels.chunks_exact(8) {+ let channel = |at: usize| u16::from_le_bytes([pixel[at], pixel[at + 1]]);+ let brightest = channel(0).max(channel(2)).max(channel(4));+ let nits = self.nits[usize::from(brightest)];+ self.max_cll = self.max_cll.max(nits);+ sum += f64::from(nits);+ count += 1;+ }+ if count > 0 {+ self.max_fall = self.max_fall.max((sum / count as f64) as f32);+ }+ }}impl Encoder {@@ -338,8 +445,78 @@ impl Encoder {ffmpeg::color::TransferCharacteristic,ffmpeg::color::Space,),+ ) -> Result<Self> {+ Self::open(path, width, height, frame_rate, options, tags, None)+ }++ /// Opens `path` for an HDR file: BT.2020, ten bits, HLG or PQ as+ /// `signal` says, written from frames that are that already - sixteen+ /// bits a channel, Rec. 2020, the transfer applied, which is what the+ /// compositor's HDR resolve hands back. A PQ file also names the+ /// display it was mastered on (a P3 display of 1000 nits) and, once it+ /// is finished, the brightest pixel and frame in it. H.264 is refused:+ /// the players that matter do not take HDR in it.+ pub fn create_hdr(+ path: impl AsRef<Path>,+ width: u32,+ height: u32,+ frame_rate: FrameRate,+ options: &EncodeOptions,+ signal: Signal,+ ) -> Result<Self> {+ use ffmpeg::color::{Primaries, Space, TransferCharacteristic};+ let path = path.as_ref();+ let transfer = match signal {+ Signal::Hlg => TransferCharacteristic::ARIB_STD_B67,+ Signal::Pq => TransferCharacteristic::SMPTE2084,+ Signal::Sdr | Signal::SdrWide => {+ return Err(Error::Ffi {+ operation: "open an HDR encoder",+ path: path.to_path_buf(),+ detail: "an HDR file is HLG or PQ".to_owned(),+ });+ }+ };+ if options.codec == VideoCodec::H264 {+ return Err(Error::Ffi {+ operation: "open an HDR encoder",+ path: path.to_path_buf(),+ detail: "H.264 does not carry HDR here: choose HEVC or AV1".to_owned(),+ });+ }+ let options = EncodeOptions {+ ten_bit: true,+ ..options.clone()+ };+ Self::open(+ path,+ width,+ height,+ frame_rate,+ &options,+ (Primaries::BT2020, transfer, Space::BT2020NCL),+ Some(signal),+ )+ }++ /// The one way in: `tags` on the file, and `hdr` the signal of deep+ /// frames when the file is HDR (see [`Encoder::create_hdr`]), else the+ /// frames are eight-bit sRGB.+ fn open(+ path: impl AsRef<Path>,+ width: u32,+ height: u32,+ frame_rate: FrameRate,+ options: &EncodeOptions,+ tags: (+ ffmpeg::color::Primaries,+ ffmpeg::color::TransferCharacteristic,+ ffmpeg::color::Space,+ ),+ hdr: Option<Signal>,) -> Result<Self> {ffi::init();+ let pq = hdr == Some(Signal::Pq);let path = path.as_ref();let fps = frame_rate.fps();let rate = ffmpeg::Rational::new(fps.numerator() as i32, fps.denominator() as i32);@@ -401,6 +578,28 @@ impl Encoder {if global_header {video.set_flags(ffmpeg::codec::Flags::GLOBAL_HEADER);}+ if pq {+ // The display it was mastered on, where an encoder that writes+ // it into the stream reads it.+ // SAFETY: `video` owns a live AVCodecContext; the side data is+ // allocated by FFmpeg at the struct's size and filled here.+ unsafe {+ let context = video.as_mut_ptr();+ let entry = ffmpeg::sys::av_frame_side_data_new(+ &mut (*context).decoded_side_data,+ &mut (*context).nb_decoded_side_data,+ ffmpeg::sys::AVFrameSideDataType::MASTERING_DISPLAY_METADATA,+ std::mem::size_of::<MasteringDisplay>(),+ 0,+ );+ if !entry.is_null() {+ std::ptr::write(+ (*entry).data.cast::<MasteringDisplay>(),+ MasteringDisplay::p3_1000(),+ );+ }+ }+ }let crf = options.crf.to_string();let cbr = options.rate_mode == RateMode::Cbr && options.bitrate_kbps > 0;@@ -460,6 +659,40 @@ impl Encoder {},_ => ffmpeg::dict! {},};+ // HDR's own words to the software encoders, besides the tags they+ // read off the context: headers with every keyframe, and a PQ+ // file's mastering display.+ let mut settings = settings;+ if hdr.is_some() {+ match encoder_name {+ "libx265" => {+ let mut params = settings+ .get("x265-params")+ .map(str::to_owned)+ .unwrap_or_default();+ for param in [+ Some("repeat-headers=1".to_owned()),+ pq.then(|| format!("hdr10=1:master-display={X265_MASTER_DISPLAY}")),+ ]+ .into_iter()+ .flatten()+ {+ if !params.is_empty() {+ params.push(':');+ }+ params.push_str(¶m);+ }+ settings.set("x265-params", ¶ms);+ }+ "libsvtav1" if pq => {+ settings.set(+ "svtav1-params",+ format!("mastering-display={SVT_MASTER_DISPLAY}"),+ );+ }+ _ => {}+ }+ }let encoder = video.open_with(settings).map_err(|error| ffi::fail("open encoder", path, error))?;@@ -470,6 +703,28 @@ impl Encoder {.map_err(|error| ffi::fail("add stream", path, error))?;stream.copy_parameters_from_context(&encoder);stream.set_time_base(time_base);+ if pq {+ // The same display in the container, for the players that+ // read it there ('mdcv').+ // SAFETY: the stream is live and its parameters were just+ // copied; FFmpeg allocates the entry at the struct's size.+ unsafe {+ let parameters = (*stream.as_mut_ptr()).codecpar;+ let entry = ffmpeg::sys::av_packet_side_data_new(+ &mut (*parameters).coded_side_data,+ &mut (*parameters).nb_coded_side_data,+ ffmpeg::sys::AVPacketSideDataType::MASTERING_DISPLAY_METADATA,+ std::mem::size_of::<MasteringDisplay>(),+ 0,+ );+ if !entry.is_null() {+ std::ptr::write(+ (*entry).data.cast::<MasteringDisplay>(),+ MasteringDisplay::p3_1000(),+ );+ }+ }+ }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.@@ -489,8 +744,9 @@ impl Encoder {.map(|stream| stream.time_base()).unwrap_or(time_base);+ let deep = hdr.is_some();let mut scaler = scaling::Context::get(- Pixel::RGBA,+ if deep { Pixel::RGBA64LE } else { Pixel::RGBA },width,height,pixel_format,@@ -506,7 +762,11 @@ impl Encoder {// 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);+ let coefficients = ffmpeg::sys::sws_getCoefficients(if deep {+ ffmpeg::sys::SWS_CS_BT2020+ } else {+ ffmpeg::sys::SWS_CS_ITU709+ });ffmpeg::sys::sws_setColorspaceDetails(scaler.as_mut_ptr(),coefficients,@@ -532,6 +792,8 @@ impl Encoder {finished: false,encoder_name,color_range: options.color_range.as_ffmpeg(),+ deep,+ light: pq.then(LightLevel::new),})}@@ -591,15 +853,34 @@ impl FrameSink for Encoder {});}- let mut rgba = Video::new(Pixel::RGBA, self.width, self.height);+ let deep = frame.depth() == Depth::Sixteen;+ if deep != self.deep {+ return Err(Error::Io {+ path: self.path.clone(),+ source: std::io::Error::other(if self.deep {+ "an HDR file takes sixteen-bit frames"+ } else {+ "an SDR file takes eight-bit frames"+ }),+ });+ }+ let (pixel, bytes) = if deep {+ (Pixel::RGBA64LE, 8)+ } else {+ (Pixel::RGBA, 4)+ };+ let mut rgba = Video::new(pixel, self.width, self.height);{let stride = rgba.stride(0);- let row = self.width as usize * 4;+ let row = self.width as usize * bytes;let data = rgba.data_mut(0);for (y, source) in frame.pixels().chunks_exact(row).enumerate() {data[y * stride..y * stride + row].copy_from_slice(source);}}+ if let Some(light) = &mut self.light {+ light.measure(frame.pixels());+ }let mut converted = Video::empty();self.scaler.run(&rgba, &mut converted)@@ -629,6 +910,33 @@ impl FrameSink for Encoder {.send_eof().map_err(|error| ffi::fail("encode", &self.path, error))?;self.drain()?;+ if let Some(light) = &self.light {+ // The light the file holds, measured as it was written, in the+ // container ('clli'), which the muxer writes with the index.+ // SAFETY: the stream is live; FFmpeg allocates the entry at the+ // struct's size.+ unsafe {+ if let Some(mut stream) = self.output.stream_mut(0) {+ let parameters = (*stream.as_mut_ptr()).codecpar;+ let entry = ffmpeg::sys::av_packet_side_data_new(+ &mut (*parameters).coded_side_data,+ &mut (*parameters).nb_coded_side_data,+ ffmpeg::sys::AVPacketSideDataType::CONTENT_LIGHT_LEVEL,+ std::mem::size_of::<ContentLight>(),+ 0,+ );+ if !entry.is_null() {+ std::ptr::write(+ (*entry).data.cast::<ContentLight>(),+ ContentLight {+ max_cll: light.max_cll.round() as u32,+ max_fall: light.max_fall.round() as u32,+ },+ );+ }+ }+ }+ }self.output.write_trailer().map_err(|error| ffi::fail("write trailer", &self.path, error))@@ -920,6 +1228,128 @@ mod tests {}}+ /// An HDR file is what it says it is: ten bits, BT.2020 with HLG or PQ,+ /// written from deep frames whose levels come back as they went in -+ /// HLG's reference white at 75 % of the signal, PQ's at 58 % - and an+ /// eight-bit frame is refused. A PQ file names its mastering display+ /// and the light it holds in the container; H.264 is refused outright.+ #[test]+ fn an_hdr_file_is_tagged_and_keeps_its_levels() {+ use crate::decode::{DecodeOptions, Decoder, FrameSource};+ let options = |codec| EncodeOptions {+ codec,+ preset: "ultrafast".to_owned(),+ crf: 16,+ rate_mode: RateMode::Vbr,+ bitrate_kbps: 0,+ ten_bit: false,+ color_range: ColorRange::Limited,+ hardware: true,+ threads: 0,+ };+ let refused = std::env::temp_dir().join("concat-encode-hdr-h264.mp4");+ assert!(+ Encoder::create_hdr(+ &refused,+ 64,+ 64,+ FrameRate::THIRTY,+ &options(VideoCodec::H264),+ Signal::Hlg+ )+ .is_err()+ );+ for codec in [VideoCodec::Hevc, VideoCodec::Av1] {+ if !codec.available() {+ eprintln!("{} not in the linked FFmpeg; skipped", codec.label());+ continue;+ }+ for (signal, level) in [(Signal::Hlg, 0.75f64), (Signal::Pq, 0.58)] {+ let path = std::env::temp_dir()+ .join(format!("concat-encode-hdr-{}-{signal:?}.mp4", codec.name()));+ let mut encoder =+ Encoder::create_hdr(&path, 64, 64, FrameRate::THIRTY, &options(codec), signal)+ .unwrap_or_else(|error| panic!("{} {signal:?}: {error}", codec.label()));+ let value = (level * 65_535.0).round() as u16;+ let pixel: Vec<u8> = [value, value, value, u16::MAX]+ .iter()+ .flat_map(|channel| channel.to_le_bytes())+ .collect();+ let frame = Frame::from_rgba64(64, 64, pixel.repeat(64 * 64), signal)+ .expect("a deep frame");+ for _ in 0..4 {+ encoder.write_frame(&frame).expect("writes");+ }+ assert!(+ encoder.write_frame(&Frame::black(64, 64)).is_err(),+ "an HDR file takes deep frames only"+ );+ encoder.finish().expect("finishes");++ let (_, _, primaries, transfer, space, format, _) = tags_of(&path);+ assert_eq!(primaries, ffmpeg::color::Primaries::BT2020);+ assert_eq!(+ transfer,+ match signal {+ Signal::Hlg => ffmpeg::color::TransferCharacteristic::ARIB_STD_B67,+ _ => ffmpeg::color::TransferCharacteristic::SMPTE2084,+ }+ );+ assert_eq!(space, ffmpeg::color::Space::BT2020NCL);+ assert!(+ matches!(format, Pixel::YUV420P10LE | Pixel::P010LE),+ "{format:?}"+ );++ let mut decoder = Decoder::open(&path, &DecodeOptions::default().deep(true))+ .expect("opens what was just written");+ let back = decoder.next_frame().expect("decodes").expect("a frame");+ assert_eq!(back.signal(), signal);+ assert_eq!(back.depth(), Depth::Sixteen);+ let got =+ f64::from(u16::from_le_bytes([back.pixels()[0], back.pixels()[1]])) / 65_535.0;+ assert!(+ (got - level).abs() < 0.01,+ "{} {signal:?}: {level} came back {got}",+ codec.label()+ );++ if signal == Signal::Pq {+ let input = ffmpeg::format::input(&path).expect("opens");+ let stream = input.stream(0).expect("a stream");+ // SAFETY: the parameters live as long as `input`; the+ // side data is read, not kept.+ unsafe {+ let parameters = stream.parameters().as_ptr();+ let found = |kind| {+ ffmpeg::sys::av_packet_side_data_get(+ (*parameters).coded_side_data,+ (*parameters).nb_coded_side_data,+ kind,+ )+ };+ let display =+ found(ffmpeg::sys::AVPacketSideDataType::MASTERING_DISPLAY_METADATA);+ assert!(+ !display.is_null(),+ "{}: the mastering display",+ codec.label()+ );+ let light = found(ffmpeg::sys::AVPacketSideDataType::CONTENT_LIGHT_LEVEL);+ assert!(!light.is_null(), "{}: the light level", codec.label());+ let light = &*(*light).data.cast::<ContentLight>();+ assert!(+ (190..=215).contains(&light.max_cll),+ "MaxCLL of a 203-nit grey: {}",+ light.max_cll+ );+ }+ }+ let _ = std::fs::remove_file(&path);+ }+ }+ }+/// A file written full range says so, and its levels come back as/// they went in - as do a video-range file's. The decoder reads the/// tag the encoder wrote, so a black that comes back black in bothdiff --git a/src/crates/concat-perf/src/main.rs b/src/crates/concat-perf/src/main.rsindex 6787a755..4c027a4f 100644--- a/src/crates/concat-perf/src/main.rs+++ b/src/crates/concat-perf/src/main.rs@@ -85,7 +85,10 @@ fn main() {results.push(measure);}results.extend(blur_4k());- results.push(export(&media));+ results.push(export(&media, false));+ if concat_media::VideoCodec::Hevc.available() {+ results.push(export(&media, true));+ }}println!();println!(@@ -948,9 +951,13 @@ fn blur_4k() -> Vec<Measure> {/// A real export of a three-second cut through the session, as the/// window runs it: frames a second at 720p.-fn export(media: &Media) -> Measure {+fn export(media: &Media, hdr: bool) -> Measure {use concat_host::{Session, projects};- let dir = media.path.parent().expect("a dir").join("project");+ let dir = media+ .path+ .parent()+ .expect("a dir")+ .join(if hdr { "project-hdr" } else { "project" });std::fs::create_dir_all(&dir).expect("a dir");let info = projects::create(&dir.to_string_lossy(), "perf", 1280, 720, 30, 1).expect("creates");let mut session = Session::open_info(&info).expect("opens");@@ -974,16 +981,28 @@ fn export(media: &Media) -> Measure {start: 1.0,}).expect("places a second, overlapping");+ if hdr {+ let video = concat_project::model::VideoSettings {+ color_space: concat_project::model::ColorSpace::Hlg,+ ..session.video()+ };+ session.set_video(video).expect("an HLG timeline");+ }let output = dir.join("out.mp4");let spec = concat_host::export::ExportSpec {output: output.to_string_lossy().into_owned(),crf: 23,preset: "veryfast".to_owned(),- codec: concat_media::VideoCodec::H264,+ codec: if hdr {+ concat_media::VideoCodec::Hevc+ } else {+ concat_media::VideoCodec::H264+ },ten_bit: false,rate_mode: concat_media::RateMode::Vbr,bitrate_kbps: 0,color_range: concat_media::ColorRange::Limited,+ hdr,};let request = concat_host::export::request(&session, &spec, Vec::new());let cancel = AtomicBool::new(false);@@ -1000,7 +1019,11 @@ fn export(media: &Media) -> Measure {.map(|meta| meta.len()).unwrap_or(0);Measure {- name: "export a 4 s 720p cut of two overlapping clips",+ name: if hdr {+ "export the same cut as HDR: HLG, HEVC 10-bit"+ } else {+ "export a 4 s 720p cut of two overlapping clips"+ },value: frames as f64 / elapsed.as_secs_f64().max(1e-9),unit: "fps",budget: Budget::AtLeast(30.0),diff --git a/src/crates/concat-render/src/compositor.rs b/src/crates/concat-render/src/compositor.rsindex 2ca9078e..0d116f19 100644--- a/src/crates/concat-render/src/compositor.rs+++ b/src/crates/concat-render/src/compositor.rs@@ -43,6 +43,13 @@ pub trait Compositor {None}+ /// From here on, frames of an HDR plan come back for an HDR file - the+ /// light on Rec. 2020's primaries, encoded HLG or PQ as the plan's+ /// output says, sixteen bits a channel - rather than rolled off into+ /// eight bits for an SDR one. A compositor that cannot does nothing,+ /// and its frames stay SDR.+ fn deliver_hdr(&mut self, _on: bool) {}+/// Whether the device this draws on has been lost - a reset, a hang, a/// driver gone - after which every frame it hands back is black. An/// export checks it after each frame and stops with an error ratherdiff --git a/src/crates/concat-render/src/gpu.rs b/src/crates/concat-render/src/gpu.rsindex 3a2e63a4..eb4f3fc7 100644--- a/src/crates/concat-render/src/gpu.rs+++ b/src/crates/concat-render/src/gpu.rs@@ -211,6 +211,47 @@ fn fs_resolve(@builtin(position) at: vec4<f32>) -> @location(0) vec4<f32> {return vec4<f32>(pow(clipped, vec3<f32>(1.0 / 2.4)), colour.a);}+// An HDR file: the light on Rec. 2020's primaries, encoded HLG or PQ, and+// handed back as sixteen-bit integers for the encoder, opaque.+fn to_2020(rgb: vec3<f32>) -> vec3<f32> {+ return vec3<f32>(+ dot(vec3<f32>(0.627403896, 0.329283039, 0.043313065), rgb),+ dot(vec3<f32>(0.069097289, 0.919540395, 0.011362316), rgb),+ dot(vec3<f32>(0.016391439, 0.088013308, 0.895595253), rgb),+ );+}++fn deep_out(signal: vec3<f32>) -> vec4<u32> {+ let clipped = clamp(signal, vec3<f32>(0.0), vec3<f32>(1.0));+ return vec4<u32>(vec3<u32>(round(clipped * 65535.0)), 65535u);+}++// BT.2100's HLG for a 1000-nit display, the inverse of the upload's: the+// display's light back to the scene's through the inverse OOTF (a system+// gamma of 1.2), then ARIB STD-B67's OETF - which puts SDR white, 203 nits,+// at 75 % of the signal.+@fragment+fn fs_resolve_hlg(@builtin(position) at: vec4<f32>) -> @location(0) vec4<u32> {+ let colour = textureLoad(picture, vec2<i32>(at.xy), 0);+ let nits = max(to_2020(colour.rgb), vec3<f32>(0.0)) * 203.0;+ let yd = max(dot(nits, vec3<f32>(0.2627, 0.6780, 0.0593)), 1e-6);+ let scene = clamp(nits / 1000.0 * pow(yd / 1000.0, -0.2 / 1.2), vec3<f32>(0.0), vec3<f32>(1.0));+ let a = 0.17883277;+ let b = 0.28466892;+ let c = 0.55991073;+ let low = sqrt(3.0 * scene);+ let high = a * log(max(12.0 * scene - b, vec3<f32>(1e-6))) + c;+ return deep_out(select(high, low, scene <= vec3<f32>(1.0 / 12.0)));+}++// SMPTE ST 2084, the light in nits as the signal.+@fragment+fn fs_resolve_pq(@builtin(position) at: vec4<f32>) -> @location(0) vec4<u32> {+ let colour = textureLoad(picture, vec2<i32>(at.xy), 0);+ let nits = max(to_2020(colour.rgb), vec3<f32>(0.0)) * 203.0;+ return deep_out(vec3<f32>(pq_signal(nits.r), pq_signal(nits.g), pq_signal(nits.b)));+}+// An HDR timeline for an SDR screen: its light rolled off by BT.2390 as an// HDR clip's is on an SDR timeline - the same maths, so a lone clip looks// the same on either - then encoded as SDR is.@@ -480,6 +521,15 @@ pub struct WgpuCompositor {/// What the plan being drawn is output in (`FramePlan::output`): taken/// up by `prepare`, and read by the uploads and the resolve after it.output: concat_core::frame::Signal,+ /// An HDR file out of the working space: HLG, then PQ, into sixteen-bit+ /// integers the encoder takes as they are.+ hdr_pipelines: [wgpu::RenderPipeline; 2],+ /// Frames of an HDR plan come back for an HDR file, not rolled off for+ /// an SDR one (`Compositor::deliver_hdr`).+ deliver_hdr: bool,+ /// The readback target an HDR frame is resolved into: sixteen-bit+ /// integers, eight bytes a pixel.+ deep_target: Option<Target>,/// The eight-bit textures frames are written into on their way to the/// pool, one a size, each with its bind group.staging: HashMap<(u32, u32), (wgpu::Texture, wgpu::BindGroup)>,@@ -875,6 +925,8 @@ impl WgpuCompositor {let resolve_pipeline = copy_into(wgpu::TextureFormat::Rgba8Unorm, "fs_resolve");let tone_mapped_pipeline =copy_into(wgpu::TextureFormat::Rgba8Unorm, "fs_resolve_tone_mapped");+ let hdr_pipelines = ["fs_resolve_hlg", "fs_resolve_pq"]+ .map(|entry| copy_into(wgpu::TextureFormat::Rgba16Uint, entry));let deep_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {label: Some("concat deep"),@@ -977,6 +1029,9 @@ impl WgpuCompositor {resolve_pipeline,tone_mapped_pipeline,output: concat_core::frame::Signal::Sdr,+ hdr_pipelines,+ deliver_hdr: false,+ deep_target: None,staging: HashMap::new(),deep_pipelines,deep_layout,@@ -1782,13 +1837,30 @@ impl WgpuCompositor {}}- /// The reusable render target for this output size.+ /// Whether the frame being drawn goes to an HDR file: an HDR plan, with+ /// HDR delivery on (`Compositor::deliver_hdr`).+ fn delivering_hdr(&self) -> bool {+ self.deliver_hdr && self.output != concat_core::frame::Signal::Sdr+ }++ /// The reusable readback target for this output size: eight bits a+ /// channel for an SDR frame, sixteen-bit integers for an HDR file's.fn target(&mut self, width: u32, height: u32) -> &Target {- let stale = self- .target+ let deep = self.delivering_hdr();+ let slot = if deep {+ &mut self.deep_target+ } else {+ &mut self.target+ };+ let stale = slot.as_ref().is_none_or(|target| target.width != width || target.height != height);if stale {+ let (format, bytes) = if deep {+ (wgpu::TextureFormat::Rgba16Uint, 8)+ } else {+ (wgpu::TextureFormat::Rgba8Unorm, 4)+ };let texture = self.device.create_texture(&wgpu::TextureDescriptor {label: Some("concat output"),size: wgpu::Extent3d {@@ -1799,18 +1871,18 @@ impl WgpuCompositor {mip_level_count: 1,sample_count: 1,dimension: wgpu::TextureDimension::D2,- format: wgpu::TextureFormat::Rgba8Unorm,+ format,usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,view_formats: &[],});- let padded_row = (width as usize * 4).div_ceil(ROW_ALIGN) * ROW_ALIGN;+ let padded_row = (width as usize * bytes).div_ceil(ROW_ALIGN) * ROW_ALIGN;let staging = self.device.create_buffer(&wgpu::BufferDescriptor {label: Some("concat readback"),size: (padded_row * height as usize) as u64,usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,mapped_at_creation: false,});- self.target = Some(Target {+ *slot = Some(Target {width,height,texture,@@ -1818,7 +1890,17 @@ impl WgpuCompositor {padded_row,});}- self.target.as_ref().expect("just ensured")+ slot.as_ref().expect("just ensured")+ }++ /// The readback target `target` last ensured for the frame in hand.+ fn current_target(&self) -> &Target {+ if self.delivering_hdr() {+ self.deep_target.as_ref()+ } else {+ self.target.as_ref()+ }+ .expect("ensured by target")}/// Claims a pooled texture of the layer's size holding the frame's@@ -1922,7 +2004,9 @@ impl WgpuCompositor {canvas: usize,into: &wgpu::TextureView,) {- let pipeline = if self.output == concat_core::frame::Signal::Sdr {+ let pipeline = if self.delivering_hdr() {+ &self.hdr_pipelines[usize::from(self.output == concat_core::frame::Signal::Pq)]+ } else if self.output == concat_core::frame::Signal::Sdr {&self.resolve_pipeline} else {&self.tone_mapped_pipeline@@ -2961,7 +3045,12 @@ impl WgpuCompositor {/// map's own result is what decides, not the poll's: a poll can return/// without the map having been served.fn read_back(&mut self) -> Option<Frame> {- let target = self.target.as_ref()?;+ let deep = self.delivering_hdr();+ let target = if deep {+ self.deep_target.as_ref()?+ } else {+ self.target.as_ref()?+ };let (width, height, padded_row) = (target.width, target.height, target.padded_row);let slice = target.staging.slice(..);@@ -2980,6 +3069,20 @@ impl WgpuCompositor {return None;}+ if deep {+ // Sixteen-bit integers, opaque already: the rows as they are.+ let row_bytes = width as usize * 8;+ let mut pixels = vec![0u8; row_bytes * height as usize];+ {+ let data = slice.get_mapped_range().ok()?;+ for row in 0..height as usize {+ let from = &data[row * padded_row..row * padded_row + row_bytes];+ pixels[row * row_bytes..(row + 1) * row_bytes].copy_from_slice(from);+ }+ }+ target.staging.unmap();+ return Frame::from_rgba64(width, height, pixels, self.output);+ }let mut frame = Frame::transparent(width, height);{// A range that will not map is a failed map, as above.@@ -3024,6 +3127,10 @@ impl Compositor for WgpuCompositor {self.dead}+ fn deliver_hdr(&mut self, on: bool) {+ self.deliver_hdr = on;+ }+fn combine(&mut self,width: u32,@@ -3050,7 +3157,7 @@ impl Compositor for WgpuCompositor {let to_view = view_of(self, to, to_index);let (canvas, _, canvas_view) = self.canvas(width, height);self.target(width, height);- let target_texture = self.target.as_ref().expect("just ensured").texture.clone();+ let target_texture = self.current_target().texture.clone();let view = target_texture.create_view(&wgpu::TextureViewDescriptor::default());let mut encoder = self.encode_transition(width,@@ -3063,7 +3170,7 @@ impl Compositor for WgpuCompositor {)?;self.resolve(&mut encoder, (width, height), canvas, &view);{- let target = self.target.as_ref().expect("just ensured");+ let target = self.current_target();encoder.copy_texture_to_buffer(wgpu::TexelCopyTextureInfo {texture: &target.texture,@@ -3101,7 +3208,7 @@ impl WgpuCompositor {fn render_and_read(&mut self, width: u32, height: u32, draws: &[Draw]) -> Option<Frame> {let (canvas, canvas_texture, canvas_view) = self.canvas(width, height);self.target(width, height);- let target = self.target.as_ref().expect("just ensured");+ let target = self.current_target();let view = target.texture.create_view(&wgpu::TextureViewDescriptor::default());diff --git a/src/crates/concat-render/src/gpu/tests.rs b/src/crates/concat-render/src/gpu/tests.rsindex d07d2d1f..43b063d9 100644--- a/src/crates/concat-render/src/gpu/tests.rs+++ b/src/crates/concat-render/src/gpu/tests.rs@@ -979,6 +979,55 @@ fn an_hdr_timeline_keeps_the_light_above_white() {);}+/// Delivered for an HDR file, an HDR timeline's frame comes back as the+/// signal it was made from: an HLG clip's levels through the working space+/// and out again, SDR white at HLG's reference white (75 %, 203 nits), and+/// the same for PQ, whose 203 nits are 58 % of its signal. Sixteen bits a+/// channel, opaque; an SDR timeline's frames stay eight-bit SDR.+#[test]+fn an_hdr_timeline_is_delivered_as_its_signal() {+ use concat_core::frame::{Depth, Signal};+ let Some(mut gpu) = gpu() else { return };+ gpu.deliver_hdr(true);+ let level = |frame: &Frame| {+ assert_eq!(frame.depth(), Depth::Sixteen);+ let at = (4 + 1) * 8;+ let channel = |offset: usize| {+ f64::from(u16::from_le_bytes([+ frame.pixels()[at + offset],+ frame.pixels()[at + offset + 1],+ ])) / 65_535.0+ };+ assert_eq!(channel(6), 1.0, "opaque");+ [channel(0), channel(2), channel(4)]+ };+ for signal in [Signal::Hlg, Signal::Pq] {+ let delivered = |gpu: &mut WgpuCompositor, frame: Frame| {+ gpu.render(&FramePlan {+ output: signal,+ ..plan(4, 4, vec![layer(frame)])+ })+ };+ for going in [0.25, 0.5, 0.75, 0.9] {+ let out = delivered(&mut gpu, deep(4, 4, going as f32, signal));+ assert_eq!(out.signal(), signal);+ let got = level(&out);+ assert!(+ got.iter().all(|channel| (channel - going).abs() < 0.003),+ "{signal:?} {going} came back {got:?}"+ );+ }+ let white = level(&delivered(&mut gpu, solid(4, 4, [255, 255, 255, 255])));+ let want = if signal == Signal::Hlg { 0.75 } else { 0.5807 };+ assert!(+ white.iter().all(|channel| (channel - want).abs() < 0.003),+ "{signal:?}: SDR white came out {white:?}"+ );+ }+ let sdr = gpu.render(&plan(4, 4, vec![layer(solid(4, 4, [255, 255, 255, 255]))]));+ assert_eq!(sdr.depth(), Depth::Eight, "an SDR timeline stays SDR");+}+/// A pass of a format 2 package - the scene-linear contract - with `body`/// as its shader and no knobs, at `intensity`.fn scene_linear(id: &str, body: &str, intensity: f32) -> ShaderPass {diff --git a/src/crates/concat/locales/de.json b/src/crates/concat/locales/de.jsonindex 0bb685ff..88584bc0 100644--- a/src/crates/concat/locales/de.json+++ b/src/crates/concat/locales/de.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "Ein Reißzoom aus der vorherigen Einstellung hinaus und in die nächste hinein.","effects.zoomTransition.name": "Zoom","export.advanced": "Advanced",+ "export.asTheTimeline": "Wie die Timeline","export.bitrate": "Bitrate","export.browse": "Durchsuchen","export.cancelExport": "Export abbrechen",@@ -734,6 +735,7 @@"export.summary": "Zusammenfassung","export.targetBitrate": "Target bitrate","export.tenBitColour": "10-Bit-Farbe",+ "export.toneMapped": "Tonegemappt, läuft überall","export.vbr": "VBR","export.videoYoutube": "Video, YouTube","format.almostDone": "fast fertig",diff --git a/src/crates/concat/locales/en.json b/src/crates/concat/locales/en.jsonindex 4ba63368..0895b260 100644--- a/src/crates/concat/locales/en.json+++ b/src/crates/concat/locales/en.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "A whip zoom out of the outgoing shot and into the incoming one.","effects.zoomTransition.name": "Zoom","export.advanced": "Advanced",+ "export.asTheTimeline": "As the timeline","export.bitrate": "Bitrate","export.browse": "Browse","export.cancelExport": "Cancel export",@@ -734,6 +735,7 @@"export.summary": "Summary","export.targetBitrate": "Target bitrate","export.tenBitColour": "10-bit colour",+ "export.toneMapped": "Tone-mapped, plays anywhere","export.vbr": "VBR","export.videoYoutube": "Video, YouTube","format.almostDone": "almost done",diff --git a/src/crates/concat/locales/es.json b/src/crates/concat/locales/es.jsonindex f18e6160..132255fc 100644--- a/src/crates/concat/locales/es.json+++ b/src/crates/concat/locales/es.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "Un zoom latigazo que sale del plano saliente y entra en el entrante.","effects.zoomTransition.name": "Zoom","export.advanced": "Advanced",+ "export.asTheTimeline": "Como la línea de tiempo","export.bitrate": "Bitrate","export.browse": "Examinar","export.cancelExport": "Cancelar exportación",@@ -734,6 +735,7 @@"export.summary": "Resumen","export.targetBitrate": "Target bitrate","export.tenBitColour": "Color de 10 bits",+ "export.toneMapped": "Convertido a SDR, se ve en todas partes","export.vbr": "VBR","export.videoYoutube": "Vídeo, YouTube","format.almostDone": "casi listo",diff --git a/src/crates/concat/locales/fa.json b/src/crates/concat/locales/fa.jsonindex f32a87e5..8bda9c23 100644--- a/src/crates/concat/locales/fa.json+++ b/src/crates/concat/locales/fa.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "یک Zoom سریع از نمای خروجی به بیرون و به داخل نمای ورودی.","effects.zoomTransition.name": "بزرگنمایی","export.advanced": "پیشرفته",+ "export.asTheTimeline": "مانند تایملاین","export.bitrate": "نرخ بیت (Bitrate)","export.browse": "مرور","export.cancelExport": "لغو خروجی گرفتن",@@ -734,6 +735,7 @@"export.summary": "خلاصه","export.targetBitrate": "نرخ بیت هدف","export.tenBitColour": "رنگ 10-bit",+ "export.toneMapped": "با Tone Mapping، همهجا پخش میشود","export.vbr": "VBR","export.videoYoutube": "ویدئو، YouTube","format.almostDone": "تقریباً تمام شد",diff --git a/src/crates/concat/locales/fr.json b/src/crates/concat/locales/fr.jsonindex 67bb2b3a..15fff548 100644--- a/src/crates/concat/locales/fr.json+++ b/src/crates/concat/locales/fr.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "Un zoom fouetté hors du plan sortant et dans le plan suivant.","effects.zoomTransition.name": "Zoom","export.advanced": "Advanced",+ "export.asTheTimeline": "Comme la timeline","export.bitrate": "Bitrate","export.browse": "Parcourir","export.cancelExport": "Annuler l'export",@@ -734,6 +735,7 @@"export.summary": "Résumé","export.targetBitrate": "Target bitrate","export.tenBitColour": "Couleur 10 bits",+ "export.toneMapped": "Ramené en SDR, lisible partout","export.vbr": "VBR","export.videoYoutube": "Vidéo, YouTube","format.almostDone": "presque fini",diff --git a/src/crates/concat/locales/hr.json b/src/crates/concat/locales/hr.jsonindex 3f8fe6e3..d5989b18 100644--- a/src/crates/concat/locales/hr.json+++ b/src/crates/concat/locales/hr.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "Brzi zum iz odlazećeg kadra u dolazeći.","effects.zoomTransition.name": "Zum","export.advanced": "Advanced",+ "export.asTheTimeline": "Kao vremenska crta","export.bitrate": "Bitrate","export.browse": "Pregledaj","export.cancelExport": "Prekini izvoz",@@ -734,6 +735,7 @@"export.summary": "Sažetak","export.targetBitrate": "Target bitrate","export.tenBitColour": "10-bitna boja",+ "export.toneMapped": "Mapirano u SDR, radi svugdje","export.vbr": "VBR","export.videoYoutube": "Video, YouTube","format.almostDone": "skoro gotovo",diff --git a/src/crates/concat/locales/it.json b/src/crates/concat/locales/it.jsonindex ec641ec0..067b69cc 100644--- a/src/crates/concat/locales/it.json+++ b/src/crates/concat/locales/it.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "Uno zoom a frusta fuori dall'inquadratura in uscita e dentro quella in entrata.","effects.zoomTransition.name": "Zoom","export.advanced": "Advanced",+ "export.asTheTimeline": "Come la timeline","export.bitrate": "Bitrate","export.browse": "Sfoglia","export.cancelExport": "Annulla esportazione",@@ -734,6 +735,7 @@"export.summary": "Riepilogo","export.targetBitrate": "Target bitrate","export.tenBitColour": "Colore a 10 bit",+ "export.toneMapped": "Convertito in SDR, si vede ovunque","export.vbr": "VBR","export.videoYoutube": "Video, YouTube","format.almostDone": "quasi fatto",diff --git a/src/crates/concat/locales/ja.json b/src/crates/concat/locales/ja.jsonindex f1c45b83..01a45d71 100644--- a/src/crates/concat/locales/ja.json+++ b/src/crates/concat/locales/ja.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "前のショットから勢いよくズームアウトし、次のショットへズームインします。","effects.zoomTransition.name": "ズーム","export.advanced": "Advanced",+ "export.asTheTimeline": "タイムラインのまま","export.bitrate": "Bitrate","export.browse": "参照","export.cancelExport": "書き出しをキャンセル",@@ -734,6 +735,7 @@"export.summary": "概要","export.targetBitrate": "Target bitrate","export.tenBitColour": "10ビットカラー",+ "export.toneMapped": "トーンマッピング済み、どこでも再生可","export.vbr": "VBR","export.videoYoutube": "ビデオ、YouTube","format.almostDone": "まもなく完了",diff --git a/src/crates/concat/locales/ko.json b/src/crates/concat/locales/ko.jsonindex 8225833b..cdf14297 100644--- a/src/crates/concat/locales/ko.json+++ b/src/crates/concat/locales/ko.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "나가는 샷에서 휙 줌 아웃했다가 들어오는 샷으로 줌 인합니다.","effects.zoomTransition.name": "줌","export.advanced": "Advanced",+ "export.asTheTimeline": "타임라인 그대로","export.bitrate": "Bitrate","export.browse": "찾아보기","export.cancelExport": "내보내기 취소",@@ -734,6 +735,7 @@"export.summary": "요약","export.targetBitrate": "Target bitrate","export.tenBitColour": "10비트 색상",+ "export.toneMapped": "톤 매핑, 어디서나 재생","export.vbr": "VBR","export.videoYoutube": "비디오, YouTube","format.almostDone": "거의 완료",diff --git a/src/crates/concat/locales/pt-BR.json b/src/crates/concat/locales/pt-BR.jsonindex ea2f5873..1c0aa2ea 100644--- a/src/crates/concat/locales/pt-BR.json+++ b/src/crates/concat/locales/pt-BR.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "Um zoom chicoteado para fora do plano que sai e para dentro do que entra.","effects.zoomTransition.name": "Zoom","export.advanced": "Advanced",+ "export.asTheTimeline": "Como a linha do tempo","export.bitrate": "Bitrate","export.browse": "Procurar","export.cancelExport": "Cancelar exportação",@@ -734,6 +735,7 @@"export.summary": "Resumo","export.targetBitrate": "Target bitrate","export.tenBitColour": "Cor de 10 bits",+ "export.toneMapped": "Convertido para SDR, toca em qualquer lugar","export.vbr": "VBR","export.videoYoutube": "Vídeo, YouTube","format.almostDone": "quase pronto",diff --git a/src/crates/concat/locales/ru.json b/src/crates/concat/locales/ru.jsonindex d1d9a72d..e400a4ba 100644--- a/src/crates/concat/locales/ru.json+++ b/src/crates/concat/locales/ru.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "Резкий зум из уходящего кадра в следующий.","effects.zoomTransition.name": "Зум","export.advanced": "Дополнительно",+ "export.asTheTimeline": "Как на таймлайне","export.bitrate": "Битрейт","export.browse": "Обзор","export.cancelExport": "Отменить экспорт",@@ -734,6 +735,7 @@"export.summary": "Сводка","export.targetBitrate": "Целевой битрейт","export.tenBitColour": "10-битный цвет",+ "export.toneMapped": "Приведено к SDR, играет везде","export.vbr": "VBR","export.videoYoutube": "Видео, YouTube","format.almostDone": "почти готово",diff --git a/src/crates/concat/locales/tr.json b/src/crates/concat/locales/tr.jsonindex f4a6575c..71ee879b 100644--- a/src/crates/concat/locales/tr.json+++ b/src/crates/concat/locales/tr.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "Giden çekimden dışarı, gelen çekimin içine hızlı bir yakınlaştırma savruluşu.","effects.zoomTransition.name": "Yakınlaştırma","export.advanced": "Advanced",+ "export.asTheTimeline": "Zaman çizelgesindeki gibi","export.bitrate": "Bitrate","export.browse": "Gözat","export.cancelExport": "Dışa aktarmayı iptal et",@@ -734,6 +735,7 @@"export.summary": "Özet","export.targetBitrate": "Target bitrate","export.tenBitColour": "10 bit renk",+ "export.toneMapped": "Ton eşlemeli, her yerde oynar","export.vbr": "VBR","export.videoYoutube": "Video, YouTube","format.almostDone": "neredeyse bitti",diff --git a/src/crates/concat/locales/zh-Hans.json b/src/crates/concat/locales/zh-Hans.jsonindex fbc2b51f..8b4035c2 100644--- a/src/crates/concat/locales/zh-Hans.json+++ b/src/crates/concat/locales/zh-Hans.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "快速变焦拉出上一个镜头,再推入下一个镜头。","effects.zoomTransition.name": "缩放","export.advanced": "高级",+ "export.asTheTimeline": "与时间线相同","export.bitrate": "码率","export.browse": "浏览","export.cancelExport": "取消导出",@@ -734,6 +735,7 @@"export.summary": "摘要","export.targetBitrate": "目标码率","export.tenBitColour": "10 位色彩",+ "export.toneMapped": "色调映射,随处可播","export.vbr": "VBR","export.videoYoutube": "视频、YouTube","format.almostDone": "即将完成",diff --git a/src/crates/concat/locales/zh-TW.json b/src/crates/concat/locales/zh-TW.jsonindex 2bc8b8d4..01547c43 100644--- a/src/crates/concat/locales/zh-TW.json+++ b/src/crates/concat/locales/zh-TW.json@@ -696,6 +696,7 @@"effects.zoomTransition.description": "快速變焦拉出上一個鏡頭,再推入下一個鏡頭。","effects.zoomTransition.name": "縮放","export.advanced": "進階",+ "export.asTheTimeline": "與時間軸相同","export.bitrate": "位元率","export.browse": "瀏覽","export.cancelExport": "取消匯出",@@ -734,6 +735,7 @@"export.summary": "摘要","export.targetBitrate": "目標位元率","export.tenBitColour": "10 位元色彩",+ "export.toneMapped": "色調映射,到處可播","export.vbr": "VBR","export.videoYoutube": "影片、YouTube","format.almostDone": "即將完成",diff --git a/src/crates/concat/src/lib.rs b/src/crates/concat/src/lib.rsindex 2a717383..3abdb0e9 100644--- a/src/crates/concat/src/lib.rs+++ b/src/crates/concat/src/lib.rs@@ -1291,6 +1291,9 @@ pub fn run() -> Result<(), slint::PlatformError> {app.on_export_codec_changed(on_window!(|state, index: i32| {state.handle(Msg::Export(ExportMsg::CodecChanged(index)));}));+ app.on_export_hdr_changed(on_window!(|state, on: bool| {+ state.handle(Msg::Export(ExportMsg::HdrChanged(on)));+ }));app.on_export_ten_bit_changed(on_window!(|state, on: bool| {state.handle(Msg::Export(ExportMsg::TenBitChanged(on)));}));diff --git a/src/crates/concat/src/panes/export.rs b/src/crates/concat/src/panes/export.rsindex 3688ea49..44c71279 100644--- a/src/crates/concat/src/panes/export.rs+++ b/src/crates/concat/src/panes/export.rs@@ -39,6 +39,8 @@ pub enum ExportMsg {QualityChanged(i32),CodecChanged(i32),TenBitChanged(bool),+ /// For an HDR timeline: written HDR (true), or tone-mapped to SDR.+ HdrChanged(bool),/// Limited or full range, as a row of the Advanced section's list.ColorRangeChanged(i32),/// The Advanced section is opened or closed.@@ -77,6 +79,8 @@ pub struct ExportPane {/// Index into `VideoCodec::ALL`.pub codec: usize,pub ten_bit: bool,+ /// An HDR timeline written tone-mapped to SDR rather than as it is.+ pub sdr: bool,/// Index into `ColorRange::ALL`: 0 limited, 1 full. Read only while/// the Advanced section is open, like the bitrate./// https://github.com/jub0t/Concat/issues/103@@ -109,6 +113,7 @@ impl Default for ExportPane {quality: 1,codec: 0,ten_bit: false,+ sdr: false,color_range: 0,advanced: false,rate_mode: 0,@@ -144,6 +149,7 @@ impl ExportPane {ExportMsg::QualityChanged(index) => self.quality = (index.max(0) as usize).min(2),ExportMsg::CodecChanged(index) => self.codec = (index.max(0) as usize).min(2),ExportMsg::TenBitChanged(on) => self.ten_bit = on,+ ExportMsg::HdrChanged(on) => self.sdr = !on,ExportMsg::ColorRangeChanged(index) => {self.color_range = (index.max(0) as usize).min(ColorRange::ALL.len() - 1);}@@ -233,15 +239,32 @@ impl ExportPane {* 1_000_000.0* pixels* (rate / 30.0)- * self.codec().size_factor()- * if self.ten_bit { 1.05 } else { 1.0 }+ * self.codec(studio).size_factor()+ * if self.ten_bit || self.hdr(studio) {+ 1.05+ } else {+ 1.0+ }};(video + AUDIO_BPS) * studio.duration().max(1.0) / 8.0}- /// The codec the sheet has chosen.- pub fn codec(&self) -> concat_media::VideoCodec {- concat_media::VideoCodec::ALL[self.codec.min(concat_media::VideoCodec::ALL.len() - 1)]+ /// The codec the sheet has chosen - HEVC for an HDR file where H.264+ /// was, since H.264 does not carry HDR.+ pub fn codec(&self, studio: &Studio) -> concat_media::VideoCodec {+ let codec =+ concat_media::VideoCodec::ALL[self.codec.min(concat_media::VideoCodec::ALL.len() - 1)];+ if self.hdr(studio) && codec == concat_media::VideoCodec::H264 {+ concat_media::VideoCodec::Hevc+ } else {+ codec+ }+ }++ /// Whether the file is written HDR: the timeline is, and the sheet was+ /// not told to tone-map it to SDR.+ pub fn hdr(&self, studio: &Studio) -> bool {+ studio.timeline().video.color_space.is_hdr() && !self.sdr}/// The range the file is written in: the Advanced section's choice@@ -282,8 +305,8 @@ impl ExportPane {output: output.clone(),crf: EXPORT_CRF[self.quality.min(2)],preset: "veryfast".into(),- codec: self.codec(),- ten_bit: self.ten_bit,+ codec: self.codec(studio),+ ten_bit: self.ten_bit || self.hdr(studio),rate_mode: if self.advanced && self.rate_mode == 1 {concat_media::RateMode::Cbr} else {@@ -295,6 +318,7 @@ impl ExportPane {0},color_range: self.color_range(),+ hdr: self.hdr(studio),};let (frame_w, frame_h) = studio.output_size();let titles = studio@@ -374,8 +398,18 @@ impl ExportPane {resolution: self.resolution as i32,rate: self.rate as i32,quality: self.quality as i32,- codec: self.codec as i32,+ codec: concat_media::VideoCodec::ALL+ .iter()+ .position(|codec| *codec == self.codec(studio))+ .unwrap_or(0) as i32,ten_bit: self.ten_bit,+ hdr_timeline: studio.timeline().video.color_space.is_hdr(),+ hdr: self.hdr(studio),+ hdr_label: match studio.timeline().video.color_space {+ concat_project::model::ColorSpace::Pq => "HDR (PQ)",+ _ => "HDR (HLG)",+ }+ .into(),color_range: self.color_range as i32,advanced: self.advanced,rate_mode: self.rate_mode as i32,@@ -384,11 +418,20 @@ impl ExportPane {// "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.codec();+ let codec = self.codec(studio);let mut words = vec![codec.label().to_owned()];- if self.ten_bit {+ if self.ten_bit || self.hdr(studio) {words.push("10-bit".to_owned());}+ if self.hdr(studio) {+ words.push(+ match studio.timeline().video.color_space {+ concat_project::model::ColorSpace::Pq => "PQ",+ _ => "HLG",+ }+ .to_owned(),+ );+ }if self.color_range() == ColorRange::Full {words.push(t("export.fullRange"));}diff --git a/src/crates/concat/ui/app.slint b/src/crates/concat/ui/app.slintindex 99b4be6b..d6575417 100644--- a/src/crates/concat/ui/app.slint+++ b/src/crates/concat/ui/app.slint@@ -206,6 +206,7 @@ export component App inherits Window {callback export-quality-changed(index: int);callback export-codec-changed(index: int);callback export-ten-bit-changed(on: bool);+ callback export-hdr-changed(on: bool);callback export-color-range-changed(index: int);callback export-advanced-toggled(on: bool);callback export-rate-mode-changed(index: int);@@ -556,6 +557,7 @@ export component App inherits Window {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); }+ hdr-changed(on) => { root.export-hdr-changed(on); }color-range-changed(index) => { root.export-color-range-changed(index); }advanced-toggled(on) => { root.export-advanced-toggled(on); }rate-mode-changed(index) => { root.export-rate-mode-changed(index); }diff --git a/src/crates/concat/ui/dialogs/export.slint b/src/crates/concat/ui/dialogs/export.slintindex 81420806..c66fa54b 100644--- a/src/crates/concat/ui/dialogs/export.slint+++ b/src/crates/concat/ui/dialogs/export.slint@@ -34,6 +34,12 @@ export struct ExportData {/// index into the codec list: H.264, HEVC, AV1codec: int,ten-bit: bool,+ /// The timeline is HDR, so the file can be written HDR or SDR.+ hdr-timeline: bool,+ /// It is being written HDR: ten bits, HEVC or AV1.+ hdr: bool,+ /// "HDR (HLG)" or "HDR (PQ)": the timeline's own.+ hdr-label: string,/// 0 = limited (video) range, 1 = full. Shown under Advanced.color-range: int,/// The Advanced section is open: bitrate controls show.@@ -168,6 +174,7 @@ export component ExportDialog inherits Modal {callback quality-changed(index: int);callback codec-changed(index: int);callback ten-bit-changed(on: bool);+ callback hdr-changed(on: bool);callback color-range-changed(index: int);callback advanced-toggled(on: bool);callback rate-mode-changed(index: int);@@ -259,10 +266,23 @@ export component ExportDialog inherits Modal {}}+ // An HDR timeline is written as it is by default, or rolled off+ // to SDR for wherever HDR does not play.+ if root.data.hdr-timeline: DialogRow {+ label: I18n.t("common.color");+ Select {+ current: root.data.hdr ? 0 : 1;+ options: [root.data.hdr-label, "SDR"];+ details: [I18n.t("export.asTheTimeline"), I18n.t("export.toneMapped")];+ changed(index) => { root.hdr-changed(index == 0); }+ }+ }+// 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 {+ // the hardware one will. H.264 is left out of an HDR file,+ // which it does not carry.+ if !root.data.hdr: DialogRow {label: I18n.t("export.codec");Select {current: root.data.codec;@@ -272,7 +292,18 @@ export component ExportDialog inherits Modal {}}- DialogRow {+ if root.data.hdr: DialogRow {+ label: I18n.t("export.codec");+ Select {+ current: root.data.codec - 1;+ options: ["HEVC", "AV1"];+ details: [I18n.t("export.halfTheSize"), I18n.t("export.smallest")];+ changed(index) => { root.codec-changed(index + 1); }+ }+ }++ // HDR is ten bits always, so the switch is for SDR alone.+ if !root.data.hdr: DialogRow {label: I18n.t("export.tenBitColour");HorizontalLayout {alignment: end;