Skip to content

🎥 Make videos programmatically with React

tone map

Jonny Burgerad06cb78e6ee
diff --git a/packages/renderer/rust/scalable_frame.rs b/packages/renderer/rust/scalable_frame.rs
index 45a24fd7e7..23691e4cda 100644
--- a/packages/renderer/rust/scalable_frame.rs
+++ b/packages/renderer/rust/scalable_frame.rs
@@ -68,7 +68,8 @@ impl ScalableFrame {
let mut planes: Vec<Vec<u8>>;
let format: Pixel;
let linesize: [i32; 8];
- let (mut filter, should_filter) = make_tone_map_filtergraph(frame.filter_graph)?;
+ let (mut filter, should_filter, is_tagged_bt709) =
+ make_tone_map_filtergraph(frame.filter_graph)?;
if frame.tone_mapped && should_filter {
video = frame::Video::empty();
filter
@@ -102,8 +103,14 @@ impl ScalableFrame {
linesize = unsafe { (*frame.unscaled_frame.as_ptr()).linesize };
}
- let bitmap =
- scale_and_make_bitmap(&frame, planes, format, linesize, self.transparent)?;
+ let bitmap = scale_and_make_bitmap(
+ &frame,
+ planes,
+ format,
+ linesize,
+ self.transparent,
+ is_tagged_bt709,
+ )?;
self.rgb_frame = Some(RgbFrame { data: bitmap });
self.native_frame = None;
Ok(())
@@ -187,6 +194,7 @@ pub fn scale_and_make_bitmap(
src_format: Pixel,
linesize: [i32; 8],
transparent: bool,
+ should_be_tagged_bt709: bool,
) -> Result<Vec<u8>, ErrorWithBacktrace> {
let dst_format: Pixel = match transparent {
true => Pixel::RGBA,
@@ -280,6 +288,14 @@ pub fn scale_and_make_bitmap(
}
}
+ if should_be_tagged_bt709 {
+ _print_verbose(&format!(
+ "Source video {} is tagged as BT709. Converting to RGBA and returning PNG with BT709 color space for better color accuracy.",
+ native_frame.original_src
+ ))?;
+ return get_png_data(&convert_to_rgba(&rotated), rotated_width, rotated_height);
+ }
+
Ok(create_bmp_image_from_frame(
&rotated,
rotated_width,
@@ -288,6 +304,13 @@ pub fn scale_and_make_bitmap(
))
}
+pub fn convert_to_rgba(data: &[u8]) -> Vec<u8> {
+ let mut newrot = Vec::with_capacity(data.len() + data.len() / 3);
+ let chunks = data.chunks(3);
+ newrot.extend(chunks.flat_map(|chunk| std::iter::once(255).chain(chunk.iter().copied())));
+ newrot
+}
+
pub fn rotate_270(
data: &[u8],
width: u32,
diff --git a/packages/renderer/rust/tone_map.rs b/packages/renderer/rust/tone_map.rs
index ac1d11c9b5..04060d4b5a 100644
--- a/packages/renderer/rust/tone_map.rs
+++ b/packages/renderer/rust/tone_map.rs
@@ -20,7 +20,9 @@ pub struct FilterGraph {
pub aspect_ratio: Rational,
}
-pub fn make_tone_map_filtergraph(graph: FilterGraph) -> Result<(Graph, bool), ffmpeg_next::Error> {
+pub fn make_tone_map_filtergraph(
+ graph: FilterGraph,
+) -> Result<(Graph, bool, bool), ffmpeg_next::Error> {
let mut filter = filter::Graph::new();
let original_width = graph.original_width;
@@ -78,6 +80,8 @@ pub fn make_tone_map_filtergraph(graph: FilterGraph) -> Result<(Graph, bool), ff
_ => "input",
};
+ let is_tagged_bt709 = matrix_in == "709" && transfer_in == "709" && primaries == "709";
+
let matrix_is_target =
matrix_in == "input" || matrix_in == "470bg" || matrix_in == "709" || matrix_in == "170m";
let transfer_is_target = transfer_in == "input" || transfer_in == "709";
@@ -114,5 +118,5 @@ pub fn make_tone_map_filtergraph(graph: FilterGraph) -> Result<(Graph, bool), ff
filter.validate()?;
- Ok((filter, !needs_conversion))
+ Ok((filter, !needs_conversion, is_tagged_bt709))
}