Skip to content

🎥 Make videos programmatically with React

`@remotion/compositor`: Calculate frame size based on line size

Jonny Burger4fbf923946d2
diff --git a/packages/compositor-darwin-arm64/remotion b/packages/compositor-darwin-arm64/remotion
index 813349628f..20e7cb469c 100755
Binary files a/packages/compositor-darwin-arm64/remotion and b/packages/compositor-darwin-arm64/remotion differ
diff --git a/packages/compositor-darwin-x64/remotion b/packages/compositor-darwin-x64/remotion
index ccedac0cf4..4596167e2e 100755
Binary files a/packages/compositor-darwin-x64/remotion and b/packages/compositor-darwin-x64/remotion differ
diff --git a/packages/compositor-linux-arm64-gnu/remotion b/packages/compositor-linux-arm64-gnu/remotion
index 418926f54a..ea877b7028 100755
Binary files a/packages/compositor-linux-arm64-gnu/remotion and b/packages/compositor-linux-arm64-gnu/remotion differ
diff --git a/packages/compositor-linux-arm64-musl/remotion b/packages/compositor-linux-arm64-musl/remotion
index 23dcaf11d5..82fe48e2d4 100755
Binary files a/packages/compositor-linux-arm64-musl/remotion and b/packages/compositor-linux-arm64-musl/remotion differ
diff --git a/packages/compositor-linux-x64-gnu/remotion b/packages/compositor-linux-x64-gnu/remotion
index d09b2ab1e9..42442f15b6 100755
Binary files a/packages/compositor-linux-x64-gnu/remotion and b/packages/compositor-linux-x64-gnu/remotion differ
diff --git a/packages/compositor-linux-x64-musl/remotion b/packages/compositor-linux-x64-musl/remotion
index 528e181bc9..0f1d56250f 100755
Binary files a/packages/compositor-linux-x64-musl/remotion and b/packages/compositor-linux-x64-musl/remotion differ
diff --git a/packages/compositor/rust/fix_dimensions.rs b/packages/compositor/rust/fix_dimensions.rs
new file mode 100644
index 0000000000..a6ae74b065
--- /dev/null
+++ b/packages/compositor/rust/fix_dimensions.rs
@@ -0,0 +1,56 @@
+use std::io::ErrorKind;
+
+use ffmpeg_next::format::Pixel;
+
+// Calculate dimensions based on linesize, not based on metadata
+pub fn get_dimensions_from_planes(
+ pixel_format: Pixel,
+ planes: &[Vec<u8>],
+ linesizes: &[i32; 8],
+ original_width: u32,
+ original_height: u32,
+) -> Result<(u32, u32), std::io::Error> {
+ if pixel_format != Pixel::YUV420P {
+ return Ok((original_width, original_height));
+ }
+
+ if planes.len() < 3 || linesizes.len() < 3 {
+ return Err(std::io::Error::new(
+ ErrorKind::Other,
+ "Not enough planes or linesizes",
+ ));
+ }
+
+ // Calculate dimensions for each plane based on linesizes
+ let y_height = planes[0].len() as u32 / linesizes[0] as u32;
+ let y_width = linesizes[0] as u32;
+
+ let u_height = planes[1].len() as u32 / linesizes[1] as u32;
+ let u_width = linesizes[1] as u32;
+
+ let v_height = planes[2].len() as u32 / linesizes[2] as u32;
+ let v_width = linesizes[2] as u32;
+
+ // For YUV420P, U and V planes should be half the size of Y plane
+ let expected_u_width = y_width / 2;
+ let expected_u_height = y_height / 2;
+ let expected_v_width = y_width / 2;
+ let expected_v_height = y_height / 2;
+
+ // Verify all planes have consistent dimensions
+ if u_width != expected_u_width || u_height != expected_u_height {
+ return Err(std::io::Error::new(
+ ErrorKind::Other,
+ "U plane dimensions mismatch",
+ ));
+ }
+
+ if v_width != expected_v_width || v_height != expected_v_height {
+ return Err(std::io::Error::new(
+ ErrorKind::Other,
+ "V plane dimensions mismatch",
+ ));
+ }
+
+ Ok((y_width, y_height))
+}
diff --git a/packages/compositor/rust/main.rs b/packages/compositor/rust/main.rs
index ab42015e5e..7c987d63b1 100644
--- a/packages/compositor/rust/main.rs
+++ b/packages/compositor/rust/main.rs
@@ -2,6 +2,7 @@ mod commands;
mod errors;
mod extract_audio;
mod ffmpeg;
+mod fix_dimensions;
mod frame_cache;
mod frame_cache_manager;
mod get_silent_parts;
diff --git a/packages/compositor/rust/scalable_frame.rs b/packages/compositor/rust/scalable_frame.rs
index a3d387b861..de3531f169 100644
--- a/packages/compositor/rust/scalable_frame.rs
+++ b/packages/compositor/rust/scalable_frame.rs
@@ -9,6 +9,7 @@ use ffmpeg_next::{
use crate::{
errors::ErrorWithBacktrace,
+ fix_dimensions,
global_printer::_print_verbose,
image::get_png_data,
max_cache_size,
@@ -107,6 +108,7 @@ impl ScalableFrame {
let data = d.clone();
planes.push(data);
}
+
format = video.format();
linesize = unsafe { (*video.as_ptr()).linesize };
@@ -125,6 +127,7 @@ impl ScalableFrame {
let bitmap =
scale_and_make_bitmap(&frame, planes, format, linesize, self.transparent)?;
+
self.rgb_frame = Some(RgbFrame { data: bitmap });
self.native_frame = None;
let size_after = self.get_size();
@@ -238,10 +241,18 @@ pub fn scale_and_make_bitmap(
false => Pixel::BGR24,
};
- let mut scaler = Context::get(
+ let (fixed_width, fixed_height) = fix_dimensions::get_dimensions_from_planes(
src_format,
+ &planes,
+ &linesize,
native_frame.original_width,
native_frame.original_height,
+ )?;
+
+ let mut scaler = Context::get(
+ src_format,
+ fixed_width,
+ fixed_height,
dst_format,
native_frame.scaled_width,
native_frame.scaled_height,
@@ -274,8 +285,8 @@ pub fn scale_and_make_bitmap(
let mut scaled = Video::empty();
scaler.run(
src_format,
- native_frame.original_width,
- native_frame.original_height,
+ fixed_width,
+ fixed_height,
ptr,
linesize.as_ptr(),
&mut scaled,