Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions apps/desktop-gpui/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1948,9 +1948,6 @@ fn frame_timestamp(frame: &ffmpeg::frame::Video, time_base: ffmpeg::Rational) ->
Duration::from_secs_f64(seconds.max(0.0))
}

/// The Y'CbCr matrix an imported display.mp4 is written and tagged with. The
/// renderer decodes with BT.709 for a BT.709 tag and BT.601 for anything
/// else, so the tag has to describe the samples the scaler wrote.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ImportMatrix {
Bt601,
Expand Down Expand Up @@ -1979,9 +1976,12 @@ impl SourceColor {
_ if rgb => ImportMatrix::Bt709,
ffmpeg::color::Space::BT709 => ImportMatrix::Bt709,
ffmpeg::color::Space::BT470BG | ffmpeg::color::Space::SMPTE170M => ImportMatrix::Bt601,
// Untagged video follows the usual convention: BT.601 for
// standard definition, BT.709 for HD.
ffmpeg::color::Space::Unspecified if height < 720 => ImportMatrix::Bt601,
ffmpeg::color::Space::Unspecified => {
match cap_rendering::cpu_yuv::YuvMatrix::from_color_space(space, height) {
cap_rendering::cpu_yuv::YuvMatrix::Bt709 => ImportMatrix::Bt709,
cap_rendering::cpu_yuv::YuvMatrix::Bt601 => ImportMatrix::Bt601,
}
}
_ => ImportMatrix::Bt709,
};
let full_range = rgb
Expand Down
5 changes: 0 additions & 5 deletions crates/export/src/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,6 @@ const SWS_CS_ITU709: i32 = 1;
const SWS_CS_ITU601: i32 = 5;

impl Nv12Converter {
/// Writes `decoded` into `target`, NV12 at the track's size, in BT.709
/// because that is how the encoder tags every track. A BT.709 frame of
/// the same size is a plane copy; anything else (a BT.601 or untagged
/// source, which the renderer also reads as BT.601, or a camera swapped
/// mid-recording) goes through swscale with the source's matrix.
fn fill(&mut self, decoded: &DecodedFrame, target: &mut frame::Video) -> Result<(), String> {
let (width, height) = (decoded.width(), decoded.height());
if width == 0 || height == 0 {
Expand Down
46 changes: 33 additions & 13 deletions crates/rendering/src/cpu_yuv.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

/// The Y'CbCr matrix a limited-range picture was encoded with, which decides
/// how it converts back to RGB. Decoders take it from the source's colour tag:
/// BT.709 when the stream says so, BT.601 otherwise (untagged sources keep the
/// BT.601 decode they always had).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum YuvMatrix {
#[default]
Bt601,
Bt709,
}

pub const UNTAGGED_HD_MIN_HEIGHT: u32 = 720;

impl YuvMatrix {
pub fn from_color_space(space: ffmpeg::util::color::Space) -> Self {
pub fn from_color_space(space: ffmpeg::util::color::Space, height: u32) -> Self {
match space {
ffmpeg::util::color::Space::BT709 => Self::Bt709,
ffmpeg::util::color::Space::Unspecified if height >= UNTAGGED_HD_MIN_HEIGHT => {
Self::Bt709
}
_ => Self::Bt601,
}
}
Expand Down Expand Up @@ -2468,14 +2469,33 @@ mod tests {
#[test]
fn matrix_follows_the_colour_tag() {
use ffmpeg::util::color::Space;
assert_eq!(YuvMatrix::from_color_space(Space::BT709), YuvMatrix::Bt709);
for space in [
Space::Unspecified,
Space::BT470BG,
Space::SMPTE170M,
Space::RGB,
] {
assert_eq!(YuvMatrix::from_color_space(space), YuvMatrix::Bt601);
assert_eq!(
YuvMatrix::from_color_space(Space::BT709, 480),
YuvMatrix::Bt709
);
assert_eq!(
YuvMatrix::from_color_space(Space::BT709, 1080),
YuvMatrix::Bt709
);
assert_eq!(
YuvMatrix::from_color_space(Space::Unspecified, UNTAGGED_HD_MIN_HEIGHT),
YuvMatrix::Bt709
);
assert_eq!(
YuvMatrix::from_color_space(Space::Unspecified, 1080),
YuvMatrix::Bt709
);
assert_eq!(
YuvMatrix::from_color_space(Space::Unspecified, 480),
YuvMatrix::Bt601
);
assert_eq!(
YuvMatrix::from_color_space(Space::Unspecified, 0),
YuvMatrix::Bt601
);
for space in [Space::BT470BG, Space::SMPTE170M, Space::RGB] {
assert_eq!(YuvMatrix::from_color_space(space, 1080), YuvMatrix::Bt601);
assert_eq!(YuvMatrix::from_color_space(space, 480), YuvMatrix::Bt601);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rendering/src/decoder/avassetreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ impl AVAssetReaderDecoder {
Ok((
video.width(),
video.height(),
YuvMatrix::from_color_space(video.color_space()),
YuvMatrix::from_color_space(video.color_space(), video.height()),
))
})();
let (width, height, matrix) = match dimensions {
Expand Down
7 changes: 5 additions & 2 deletions crates/rendering/src/decoder/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ impl CachedFrame {
width: frame.width(),
height: frame.height(),
format: PixelFormat::Nv12,
matrix: YuvMatrix::from_color_space(frame.color_space()),
matrix: YuvMatrix::from_color_space(
frame.color_space(),
frame.height(),
),
y_stride: cuda.y_pitch as u32,
uv_stride: cuda.uv_pitch as u32,
planes: Arc::clone(converter.plane_pool()),
Expand All @@ -344,7 +347,7 @@ impl CachedFrame {
let planes = Arc::clone(converter.plane_pool());
let width = frame.width();
let height = frame.height();
let matrix = YuvMatrix::from_color_space(frame.color_space());
let matrix = YuvMatrix::from_color_space(frame.color_space(), frame.height());

if let Some((pixel_format, y_stride, uv_stride)) = borrowable_yuv_layout(frame) {
let bytes = yuv_plane_bytes(pixel_format, y_stride, uv_stride, height);
Expand Down
34 changes: 32 additions & 2 deletions crates/rendering/src/decoder/frame_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl FrameConverter {
width,
height,
&mut output,
YuvMatrix::from_color_space(frame.color_space()),
YuvMatrix::from_color_space(frame.color_space(), frame.height()),
);
return output;
}
Expand All @@ -137,7 +137,7 @@ impl FrameConverter {

self.ensure_scaler(
frame.format(),
YuvMatrix::from_color_space(frame.color_space()),
YuvMatrix::from_color_space(frame.color_space(), frame.height()),
width,
height,
);
Expand Down Expand Up @@ -500,6 +500,36 @@ mod tests {
}
}

#[test]
fn untagged_height_selects_the_matrix_swscale_uses() {
ffmpeg::init().unwrap();
let orange = [224u8, 122, 48];
let bt709 = [133u8, 85, 175];
let bt601 = [140u8, 80, 178];
let cases = [
(720u32, bt709, ffmpeg::color::Space::Unspecified),
(480, bt601, ffmpeg::color::Space::Unspecified),
(720, bt601, ffmpeg::color::Space::SMPTE170M),
];
for (height, [y, cb, cr], space) in cases {
let mut source = frame::Video::new(format::Pixel::YUV420P, 32, height);
source.data_mut(0).fill(y);
source.data_mut(1).fill(cb);
source.data_mut(2).fill(cr);
source.set_color_space(space);
source.set_color_range(ffmpeg::color::Range::MPEG);
let rgba = FrameConverter::new().convert(&mut source);
let index = ((height as usize / 2) * 32 + 16) * 4;
let pixel = [rgba[index], rgba[index + 1], rgba[index + 2]];
for (channel, (actual, expected)) in pixel.into_iter().zip(orange).enumerate() {
assert!(
actual.abs_diff(expected) <= 8,
"{height} {space:?} channel {channel}: {pixel:?}"
);
}
}
}

#[test]
fn spare_planes_never_keep_a_buffer_above_the_frame_limit() {
let limit = MAX_SPARE_PLANE_BYTES;
Expand Down
5 changes: 1 addition & 4 deletions crates/rendering/src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,6 @@ pub enum VideoDecoderMessage {
GetFrame(f32, u32, tokio::sync::oneshot::Sender<DecodedFrame>),
}

/// The source's Y'CbCr matrix from its colour tag, for decoders that do not
/// see FFmpeg frames (Media Foundation), and whether its chroma is 4:2:0.
/// BT.601 and unknown chroma when the file cannot be read.
#[cfg(target_os = "windows")]
pub(crate) fn probe_yuv_layout(path: &std::path::Path) -> (YuvMatrix, Option<bool>) {
::ffmpeg::format::input(path)
Expand All @@ -806,7 +803,7 @@ pub(crate) fn probe_yuv_layout(path: &std::path::Path) -> (YuvMatrix, Option<boo
let context = ::ffmpeg::codec::Context::from_parameters(stream.parameters()).ok()?;
let video = context.decoder().video().ok()?;
Some((
YuvMatrix::from_color_space(video.color_space()),
YuvMatrix::from_color_space(video.color_space(), video.height()),
cap_video_decode::ffmpeg::chroma_is_420(video.format(), unsafe {
&*stream.parameters().as_ptr()
}),
Expand Down
4 changes: 2 additions & 2 deletions crates/rendering/src/project_recordings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) fn header_video_metadata(path: &Path) -> Option<HeaderVideoMetadata>
Some(HeaderVideoMetadata {
width: video.width(),
height: video.height(),
matrix: crate::cpu_yuv::YuvMatrix::from_color_space(color_space),
matrix: crate::cpu_yuv::YuvMatrix::from_color_space(color_space, video.height()),
chroma_is_420,
})
}
Expand Down Expand Up @@ -951,7 +951,7 @@ mod ordinary_media_access_tests {
.unwrap()
.color_space();
assert_eq!(
YuvMatrix::from_color_space(probed),
YuvMatrix::from_color_space(probed, 48),
expected,
"{name} full probe"
);
Expand Down
Loading
Loading