forge
Explore
Log in

Don't you dare go Hollow

apply(decode): stretch the anamorphic picture once, at the single post-decode step

Browse files at this commit

b3a5acb21e15906718a0375ad1112a58ec70b1beAhmet Kaan GümüşParent: 66a1c40
ffmpeg applies the sample aspect exactly once, at the conversion to the
square-pixel raster; so does this engine now. Render::frame takes the
file's pixel_aspect beside its rotation and, only where the samples are
not square, resamples the coded picture to the displayed width with the
existing scale_i420 -- the same bilinear the canvas places through. A
square-pixel file takes the branch by a bool and not one byte is
touched: the passthrough fast path, the tonemap FNV constants and the
graded path are all exactly what they were (matrix_render and
tonemap_render green unchanged).

The export takes the same two facts through place_picture -- stretch
after turn, the order ffmpeg composes -- and an anamorphic span is
barred from the DMA fast path, whose whole premise is that no seat
touches a sample before the encoder. Dissolve's B half carries its own
file's aspect beside its own rotation.

VhMeta grows the coded pair and the aspect ratio, so the plugin ABI
answers both questions a display size may now raise: what to draw and
what the samples are stored at.
crates/engine-hw/src/lib.rsmodified+12 -0
@@ -403,6 +403,10 @@ impl Session {
403 height: meta.height,403 height: meta.height,
404 frame_rate: meta.frame_rate,404 frame_rate: meta.frame_rate,
405 frame_count: meta.frame_count,405 frame_count: meta.frame_count,
406+ coded_width: meta.coded_width,
407+ coded_height: meta.coded_height,
408+ pixel_aspect_h: meta.pixel_aspect.h,
409+ pixel_aspect_v: meta.pixel_aspect.v,
406 },410 },
407 ready: VecDeque::new(),411 ready: VecDeque::new(),
408 pending: Vec::new(),412 pending: Vec::new(),
@@ -935,6 +939,10 @@ impl VpxSession {
935 height: meta.height,939 height: meta.height,
936 frame_rate: meta.frame_rate,940 frame_rate: meta.frame_rate,
937 frame_count: meta.frame_count,941 frame_count: meta.frame_count,
942+ coded_width: meta.coded_width,
943+ coded_height: meta.coded_height,
944+ pixel_aspect_h: meta.pixel_aspect.h,
945+ pixel_aspect_v: meta.pixel_aspect.v,
938 },946 },
939 flushed: false,947 flushed: false,
940 skip: 0,948 skip: 0,
@@ -1110,6 +1118,10 @@ pub unsafe extern "C" fn vh_meta(session: *mut c_void, out: *mut VhMeta) -> i32
1110 (*out).height = meta.height;1118 (*out).height = meta.height;
1111 (*out).frame_rate = meta.frame_rate;1119 (*out).frame_rate = meta.frame_rate;
1112 (*out).frame_count = meta.frame_count;1120 (*out).frame_count = meta.frame_count;
1121+ (*out).coded_width = meta.coded_width;
1122+ (*out).coded_height = meta.coded_height;
1123+ (*out).pixel_aspect_h = meta.pixel_aspect_h;
1124+ (*out).pixel_aspect_v = meta.pixel_aspect_v;
1113 }1125 }
1114 01126 0
1115 }))1127 }))
crates/engine/src/decode.rsmodified+54 -1
@@ -18,7 +18,7 @@ use ec_h264::H264Decoder;
18 use crate::color::ColorParams;18 use crate::color::ColorParams;
19 use crate::colorspace::{ColorDescription, Matrix, Transfer};19 use crate::colorspace::{ColorDescription, Matrix, Transfer};
20 use crate::convert::{i420_to_bgra, i420_to_bgra_with};20 use crate::convert::{i420_to_bgra, i420_to_bgra_with};
21-use crate::demux::{Codec, Demuxer, Rotation, VideoMeta};21+use crate::demux::{Codec, Demuxer, PixelAspect, Rotation, VideoMeta};
22 use crate::hw::HwSession;22 use crate::hw::HwSession;
23 use crate::project::Speed;23 use crate::project::Speed;
24 use crate::scale::Composer;24 use crate::scale::Composer;
@@ -562,6 +562,8 @@ impl DecodeSession {
562 transform,562 transform,
563 // A still has no display matrix: what the file holds is what it is.563 // A still has no display matrix: what the file holds is what it is.
564 Rotation::None,564 Rotation::None,
565+ // ...and no sample aspect either: a still is authored square.
566+ PixelAspect::SQUARE,
565 ColorDescription::default(),567 ColorDescription::default(),
566 canvas,568 canvas,
567 tonemap::Preset::default(),569 tonemap::Preset::default(),
@@ -691,6 +693,9 @@ impl DecodeSession {
691 transform,693 transform,
692 // Painted here, at the canvas's own size: nothing turned it.694 // Painted here, at the canvas's own size: nothing turned it.
693 Rotation::None,695 Rotation::None,
696+ // ...and nothing drew it anamorphic: it is square by
697+ // construction.
698+ PixelAspect::SQUARE,
694 ColorDescription::default(),699 ColorDescription::default(),
695 canvas,700 canvas,
696 tonemap::Preset::default(),701 tonemap::Preset::default(),
@@ -1108,6 +1113,7 @@ fn run_span(
1108 color,1113 color,
1109 transform,1114 transform,
1110 opened.meta.rotation,1115 opened.meta.rotation,
1116+ opened.meta.pixel_aspect,
1111 opened.meta.color,1117 opened.meta.color,
1112 canvas,1118 canvas,
1113 tone,1119 tone,
@@ -1197,6 +1203,12 @@ struct Render {
1197 /// every file that states no turn, which is the whole of an ordinary1203 /// every file that states no turn, which is the whole of an ordinary
1198 /// library.1204 /// library.
1199 rotation: Rotation,1205 rotation: Rotation,
1206+ /// The ratio one coded pixel is drawn at ([`crate::demux::PixelAspect`]):
1207+ /// the file's sample aspect ratio, applied exactly once, at this one
1208+ /// conversion to the square-pixel raster -- the same place ffmpeg's swscale
1209+ /// applies it, and nowhere else. Square for every file that states no
1210+ /// aspect, which takes the branch by a bool and touches no byte.
1211+ pixel_aspect: PixelAspect,
1200 /// What the source's samples mean -- the stream's own matrix and range,1212 /// What the source's samples mean -- the stream's own matrix and range,
1201 /// which is what the conversion below is done in rather than the BT.601 it1213 /// which is what the conversion below is done in rather than the BT.601 it
1202 /// used to assume of every file.1214 /// used to assume of every file.
@@ -1215,6 +1227,10 @@ struct Render {
1215 /// asks for none, and resized once for one that does -- a steady-state1227 /// asks for none, and resized once for one that does -- a steady-state
1216 /// buffer rather than three plane allocations per decoded frame.1228 /// buffer rather than three plane allocations per decoded frame.
1217 turned: (Vec<u8>, Vec<u8>, Vec<u8>),1229 turned: (Vec<u8>, Vec<u8>, Vec<u8>),
1230+ /// The same, for the pixel-aspect stretch: the coded picture widened to
1231+ /// the displayed width, empty for every square-pixel file and sized once
1232+ /// for one that is not.
1233+ stretched: (Vec<u8>, Vec<u8>, Vec<u8>),
1218 /// Where [`Composer::place_transformed`]'s owned picture lands, so a1234 /// Where [`Composer::place_transformed`]'s owned picture lands, so a
1219 /// transformed frame's planes live as long as `self` -- matching what1235 /// transformed frame's planes live as long as `self` -- matching what
1220 /// [`Composer::place`] already hands back by borrowing `self.canvas`.1236 /// [`Composer::place`] already hands back by borrowing `self.canvas`.
@@ -1238,6 +1254,7 @@ impl Render {
1238 color: ColorParams,1254 color: ColorParams,
1239 transform: TransformParams,1255 transform: TransformParams,
1240 rotation: Rotation,1256 rotation: Rotation,
1257+ pixel_aspect: PixelAspect,
1241 desc: ColorDescription,1258 desc: ColorDescription,
1242 canvas: Composer,1259 canvas: Composer,
1243 preset: tonemap::Preset,1260 preset: tonemap::Preset,
@@ -1247,6 +1264,7 @@ impl Render {
1247 color,1264 color,
1248 transform,1265 transform,
1249 rotation,1266 rotation,
1267+ pixel_aspect,
1250 desc,1268 desc,
1251 canvas,1269 canvas,
1252 // corner-cut: the ceiling is that the map reads limited-range codes1270 // corner-cut: the ceiling is that the map reads limited-range codes
@@ -1261,6 +1279,7 @@ impl Render {
1261 },1279 },
1262 graded: (Vec::new(), Vec::new(), Vec::new()),1280 graded: (Vec::new(), Vec::new(), Vec::new()),
1263 turned: (Vec::new(), Vec::new(), Vec::new()),1281 turned: (Vec::new(), Vec::new(), Vec::new()),
1282+ stretched: (Vec::new(), Vec::new(), Vec::new()),
1264 placed: (Vec::new(), Vec::new(), Vec::new()),1283 placed: (Vec::new(), Vec::new(), Vec::new()),
1265 }1284 }
1266 }1285 }
@@ -1302,6 +1321,40 @@ impl Render {
1302 )1321 )
1303 }1322 }
1304 };1323 };
1324+ // The pixel aspect, applied exactly once -- the single conversion to
1325+ // the square-pixel raster this engine makes, where ffmpeg's swscale
1326+ // makes its own. Only a file whose samples are not square reaches the
1327+ // resample: a square one takes the branch by a bool and not one byte
1328+ // is touched, which is what keeps every existing picture identical to
1329+ // the day before anamorphic sources existed.
1330+ let (y, u, v, width, height) = if self.pixel_aspect.is_square() {
1331+ (y, u, v, width, height)
1332+ } else {
1333+ // The aspect widens the *width*, always: 4:3 SAR over 1440x1080
1334+ // shows 1920x1080. `width_factor` lands on the even grid 4:2:0
1335+ // composes on, and a height is never touched by it.
1336+ let display_width = self.pixel_aspect.width_factor(width);
1337+ let (sy, su, sv) = &mut self.stretched;
1338+ crate::scale::scale_i420(
1339+ y,
1340+ u,
1341+ v,
1342+ width as usize,
1343+ height as usize,
1344+ sy,
1345+ su,
1346+ sv,
1347+ display_width as usize,
1348+ height as usize,
1349+ );
1350+ (
1351+ &sy[..display_width as usize * height as usize],
1352+ &su[..display_width.div_ceil(2) as usize * height.div_ceil(2) as usize],
1353+ &sv[..display_width.div_ceil(2) as usize * height.div_ceil(2) as usize],
1354+ display_width,
1355+ height,
1356+ )
1357+ };
1305 let passthrough = self.canvas.is_passthrough(width, height);1358 let passthrough = self.canvas.is_passthrough(width, height);
1306 // A transform still has a picture to place even where the canvas1359 // A transform still has a picture to place even where the canvas
1307 // itself would hand this size back untouched -- moving, scaling,1360 // itself would hand this size back untouched -- moving, scaling,
crates/engine/src/export.rsmodified+102 -16
@@ -1229,10 +1229,16 @@ struct Dissolve<'a> {
1229 /// B's own file's display-matrix turn, [`place_picture`]'s first stage;1229 /// B's own file's display-matrix turn, [`place_picture`]'s first stage;
1230 /// B is a different file and may be turned differently from A.1230 /// B is a different file and may be turned differently from A.
1231 rotation: Rotation,1231 rotation: Rotation,
1232+ /// B's own file's sample aspect, [`place_picture`]'s second stage, for
1233+ /// the same reason the turn is per file.
1234+ pixel_aspect: crate::demux::PixelAspect,
1232 canvas: Composer,1235 canvas: Composer,
1233 /// The turn's, the grade's and the transform's scratch, exactly as A's1236 /// The turn's, the grade's and the transform's scratch, exactly as A's
1234 /// span keeps them: refilled per picture, allocated once.1237 /// span keeps them: refilled per picture, allocated once.
1235 rotated: (Vec<u8>, Vec<u8>, Vec<u8>),1238 rotated: (Vec<u8>, Vec<u8>, Vec<u8>),
1239+ /// The stretch's own scratch, [`place_picture`]'s second stage -- the
1240+ /// pixel aspect's, beside the turn's.
1241+ stretched: (Vec<u8>, Vec<u8>, Vec<u8>),
1236 graded: (Vec<u8>, Vec<u8>, Vec<u8>),1242 graded: (Vec<u8>, Vec<u8>, Vec<u8>),
1237 transformed: (Vec<u8>, Vec<u8>, Vec<u8>),1243 transformed: (Vec<u8>, Vec<u8>, Vec<u8>),
1238 }1244 }
@@ -2721,7 +2727,13 @@ fn run(
2721 // samples *mean* ([`ColorDescription`]), which is what decides whether they2727 // samples *mean* ([`ColorDescription`]), which is what decides whether they
2722 // are remapped on the way in. Real time for a still, a song and a2728 // are remapped on the way in. Real time for a still, a song and a
2723 // single-rate project, where every conversion below is the identity.2729 // single-rate project, where every conversion below is the identity.
2724- let rates: Vec<(Rate, ColorDescription, Option<f32>, Rotation)> = sources2730+ let rates: Vec<(
2731+ Rate,
2732+ ColorDescription,
2733+ Option<f32>,
2734+ Rotation,
2735+ crate::demux::PixelAspect,
2736+ )> = sources
2725 .iter()2737 .iter()
2726 .map(|source| source_rate(&source.path, meta.frame_rate))2738 .map(|source| source_rate(&source.path, meta.frame_rate))
2727 .collect();2739 .collect();
@@ -2740,7 +2752,7 @@ fn run(
2740 let preset = project.tone();2752 let preset = project.tone();
2741 let tone: Vec<Option<ToneMapper>> = rates2753 let tone: Vec<Option<ToneMapper>> = rates
2742 .iter()2754 .iter()
2743- .map(|(_, color, peak, _)| match color.transfer {2755+ .map(|(_, color, peak, _, _)| match color.transfer {
2744 // A preview file keeps the film's own curve and is tone-mapped when2756 // A preview file keeps the film's own curve and is tone-mapped when
2745 // it is *shown* ([`ExportSettings::keep_source_colour`]), which is2757 // it is *shown* ([`ExportSettings::keep_source_colour`]), which is
2746 // both cheaper and the only way the preset can still be changed2758 // both cheaper and the only way the preset can still be changed
@@ -2806,12 +2818,14 @@ fn run(
2806 // A's pixels on the CPU, so a dissolving span never asks for a GPU2818 // A's pixels on the CPU, so a dissolving span never asks for a GPU
2807 // buffer, not even outside its own window.2819 // buffer, not even outside its own window.
2808 let dissolve = dissolve_window(project, &span);2820 let dissolve = dissolve_window(project, &span);
2809- let (mut pictures, rate, in_frame, color, mapper, rotation) = match span.from {2821+ let (mut pictures, rate, in_frame, color, mapper, rotation, pixel_aspect) =
2822+ match span.from
2823+ {
2810 Some((source, in_frame)) => {2824 Some((source, in_frame)) => {
2811 let entry = sources2825 let entry = sources
2812 .get(source)2826 .get(source)
2813 .ok_or_else(|| format!("clip names source {source} of {}", sources.len()))?;2827 .ok_or_else(|| format!("clip names source {source} of {}", sources.len()))?;
2814- let (rate, color, _peak, rotation) = rates[source];2828+ let (rate, color, _peak, rotation, pixel_aspect) = rates[source];
2815 let mapper = tone[source].as_ref();2829 let mapper = tone[source].as_ref();
2816 // Nothing on the way from this span's decoder to the encoder2830 // Nothing on the way from this span's decoder to the encoder
2817 // touches a sample: no tone map, no matrix remap, no grade --2831 // touches a sample: no tone map, no matrix remap, no grade --
@@ -2831,8 +2845,12 @@ fn run(
2831 // A file whose display matrix asks for a turn is *not*2845 // A file whose display matrix asks for a turn is *not*
2832 // untouched, whatever else the timeline says: its pictures2846 // untouched, whatever else the timeline says: its pictures
2833 // have to be turned, which is a read-back no GPU buffer2847 // have to be turned, which is a read-back no GPU buffer
2834- // survives.2848+ // survives. The sample aspect is the same refusal one
2849+ // stage later: its pictures have to be stretched to the
2850+ // square-pixel raster before an encoder sees them, which
2851+ // is the same kind of touch.
2835 && rotation.is_none()2852 && rotation.is_none()
2853+ && pixel_aspect.is_square()
2836 && dissolve.is_none();2854 && dissolve.is_none();
2837 let want = untouched.then(|| encoder.dma_want(meta)).flatten();2855 let want = untouched.then(|| encoder.dma_want(meta)).flatten();
2838 // Opened at the file's own frame, which is the only place the2856 // Opened at the file's own frame, which is the only place the
@@ -2852,13 +2870,21 @@ fn run(
2852 } else {2870 } else {
2853 ClipDecoder::open(&entry.path, rate.source_at(in_frame), want)?2871 ClipDecoder::open(&entry.path, rate.source_at(in_frame), want)?
2854 };2872 };
2855- (Some(pictures), rate, in_frame, Some(color), mapper, rotation)2873+ (Some(pictures), rate, in_frame, Some(color), mapper, rotation, pixel_aspect)
2856 }2874 }
2857 // A gap's black is 16/128/128, which is black in every matrix here2875 // A gap's black is 16/128/128, which is black in every matrix here
2858 // and on every curve: nothing to remap and nothing to tone-map,2876 // and on every curve: nothing to remap and nothing to tone-map,
2859 // which is what `None` says. A gap is no file either, so nothing2877 // which is what `None` says. A gap is no file either, so nothing
2860 // turns it.2878 // turns it.
2861- None => (None, Rate::REAL_TIME, 0, None, None, Rotation::None),2879+ None => (
2880+ None,
2881+ Rate::REAL_TIME,
2882+ 0,
2883+ None,
2884+ None,
2885+ Rotation::None,
2886+ crate::demux::PixelAspect::SQUARE,
2887+ ),
2862 };2888 };
2863 // Mixed spaces on one timeline: a clip coded against another matrix than2889 // Mixed spaces on one timeline: a clip coded against another matrix than
2864 // the one this file declares is rewritten into it, *after* the grade --2890 // the one this file declares is rewritten into it, *after* the grade --
@@ -2885,8 +2911,10 @@ fn run(
2885 let mut transformed = (Vec::new(), Vec::new(), Vec::new());2911 let mut transformed = (Vec::new(), Vec::new(), Vec::new());
2886 // ...and one more for the file's own display-matrix turn, which happens2912 // ...and one more for the file's own display-matrix turn, which happens
2887 // before all of those (see [`place_picture`]): empty for every source2913 // before all of those (see [`place_picture`]): empty for every source
2888- // that states no turn.2914+ // that states no turn. `stretched` is its twin for the pixel aspect,
2915+ // which happens right after it.
2889 let mut rotated = (Vec::new(), Vec::new(), Vec::new());2916 let mut rotated = (Vec::new(), Vec::new(), Vec::new());
2917+ let mut stretched = (Vec::new(), Vec::new(), Vec::new());
2890 // ...and the canvas it is placed on, which is where a source of another2918 // ...and the canvas it is placed on, which is where a source of another
2891 // resolution becomes a picture at the project's. The same `Composer`2919 // resolution becomes a picture at the project's. The same `Composer`
2892 // playback composes with, given the same policy, so an export is what2920 // playback composes with, given the same policy, so an export is what
@@ -2905,7 +2933,7 @@ fn run(
2905 let entry = sources.get(b_source).ok_or_else(|| {2933 let entry = sources.get(b_source).ok_or_else(|| {
2906 format!("clip names source {b_source} of {}", sources.len())2934 format!("clip names source {b_source} of {}", sources.len())
2907 })?;2935 })?;
2908- let (b_rate, b_color, _peak, b_rotation) = rates[b_source];2936+ let (b_rate, b_color, _peak, b_rotation, b_pixel_aspect) = rates[b_source];
2909 let b_mapper = tone[b_source].as_ref();2937 let b_mapper = tone[b_source].as_ref();
2910 let b_remap = remap_into(Some(b_color), b_mapper.is_some(), out_color.matrix);2938 let b_remap = remap_into(Some(b_color), b_mapper.is_some(), out_color.matrix);
2911 Ok(Dissolve {2939 Ok(Dissolve {
@@ -2935,12 +2963,14 @@ fn run(
2935 .copied()2963 .copied()
2936 .unwrap_or_default(),2964 .unwrap_or_default(),
2937 rotation: b_rotation,2965 rotation: b_rotation,
2966+ pixel_aspect: b_pixel_aspect,
2938 canvas: Composer::new(2967 canvas: Composer::new(
2939 meta.width,2968 meta.width,
2940 meta.height,2969 meta.height,
2941 project.composite_fit_at(b_start),2970 project.composite_fit_at(b_start),
2942 ),2971 ),
2943 rotated: (Vec::new(), Vec::new(), Vec::new()),2972 rotated: (Vec::new(), Vec::new(), Vec::new()),
2973+ stretched: (Vec::new(), Vec::new(), Vec::new()),
2944 graded: (Vec::new(), Vec::new(), Vec::new()),2974 graded: (Vec::new(), Vec::new(), Vec::new()),
2945 transformed: (Vec::new(), Vec::new(), Vec::new()),2975 transformed: (Vec::new(), Vec::new(), Vec::new()),
2946 })2976 })
@@ -3045,11 +3075,13 @@ fn run(
3045 width,3075 width,
3046 height,3076 height,
3047 rotation,3077 rotation,
3078+ pixel_aspect,
3048 grade,3079 grade,
3049 transform,3080 transform,
3050 remap,3081 remap,
3051 mapper,3082 mapper,
3052 &mut rotated,3083 &mut rotated,
3084+ &mut stretched,
3053 &mut graded,3085 &mut graded,
3054 &mut transformed,3086 &mut transformed,
3055 &mut canvas,3087 &mut canvas,
@@ -3079,9 +3111,10 @@ fn run(
3079 }3111 }
3080 Some(Frame::Pixels(by, bu, bv, bw, bh)) => {3112 Some(Frame::Pixels(by, bu, bv, bw, bh)) => {
3081 let (by, bu, bv, _, _) = place_picture(3113 let (by, bu, bv, _, _) = place_picture(
3082- by, bu, bv, bw, bh, d.rotation, d.grade, d.transform,3114+ by, bu, bv, bw, bh, d.rotation, d.pixel_aspect, d.grade,
3083- d.remap, d.mapper, &mut d.rotated, &mut d.graded,3115+ d.transform, d.remap, d.mapper, &mut d.rotated,
3084- &mut d.transformed, &mut d.canvas,3116+ &mut d.stretched, &mut d.graded, &mut d.transformed,
3117+ &mut d.canvas,
3085 );3118 );
3086 let idx_in_window = done_here + r - d.tail_start;3119 let idx_in_window = done_here + r - d.tail_start;
3087 let t = dissolve_weight(idx_in_window, d.window);3120 let t = dissolve_weight(idx_in_window, d.window);
@@ -3236,11 +3269,13 @@ fn place_picture<'a>(
3236 width: u32,3269 width: u32,
3237 height: u32,3270 height: u32,
3238 rotation: Rotation,3271 rotation: Rotation,
3272+ pixel_aspect: crate::demux::PixelAspect,
3239 grade: Option<crate::color::ColorParams>,3273 grade: Option<crate::color::ColorParams>,
3240 transform: TransformParams,3274 transform: TransformParams,
3241 remap: Option<(Matrix, Matrix)>,3275 remap: Option<(Matrix, Matrix)>,
3242 mapper: Option<&ToneMapper>,3276 mapper: Option<&ToneMapper>,
3243 turned: &'a mut (Vec<u8>, Vec<u8>, Vec<u8>),3277 turned: &'a mut (Vec<u8>, Vec<u8>, Vec<u8>),
3278+ stretched: &'a mut (Vec<u8>, Vec<u8>, Vec<u8>),
3244 graded: &'a mut (Vec<u8>, Vec<u8>, Vec<u8>),3279 graded: &'a mut (Vec<u8>, Vec<u8>, Vec<u8>),
3245 transformed: &'a mut (Vec<u8>, Vec<u8>, Vec<u8>),3280 transformed: &'a mut (Vec<u8>, Vec<u8>, Vec<u8>),
3246 canvas: &'a mut Composer,3281 canvas: &'a mut Composer,
@@ -3264,6 +3299,35 @@ fn place_picture<'a>(
3264 (&turned.0[..], &turned.1[..], &turned.2[..], w, h)3299 (&turned.0[..], &turned.1[..], &turned.2[..], w, h)
3265 }3300 }
3266 };3301 };
3302+ // The pixel aspect, applied exactly once -- the same single conversion to
3303+ // the square-pixel raster the decode funnel makes, in the same order
3304+ // (stretch after turn), so the encoder is fed the pixels the preview
3305+ // showed to the byte. A square-pixel file -- every file whose samples are
3306+ // one to one -- borrows the planes through.
3307+ let (y, u, v, width, height) = if pixel_aspect.is_square() {
3308+ (y, u, v, width, height)
3309+ } else {
3310+ let display_width = pixel_aspect.width_factor(width);
3311+ crate::scale::scale_i420(
3312+ y,
3313+ u,
3314+ v,
3315+ width as usize,
3316+ height as usize,
3317+ &mut stretched.0,
3318+ &mut stretched.1,
3319+ &mut stretched.2,
3320+ display_width as usize,
3321+ height as usize,
3322+ );
3323+ (
3324+ &stretched.0[..],
3325+ &stretched.1[..],
3326+ &stretched.2[..],
3327+ display_width,
3328+ height,
3329+ )
3330+ };
3267 // The planes are borrowed from the decoder (and `Black::picture` hands the3331 // The planes are borrowed from the decoder (and `Black::picture` hands the
3268 // same slice as both u and v), so a grade cannot be applied in place: it3332 // same slice as both u and v), so a grade cannot be applied in place: it
3269 // goes onto a copy, which the encoder then reads instead.3333 // goes onto a copy, which the encoder then reads instead.
@@ -3334,13 +3398,26 @@ fn remap_into(
3334 fn source_rate(3398 fn source_rate(
3335 path: &Path,3399 path: &Path,
3336 timeline_fps: f64,3400 timeline_fps: f64,
3337-) -> (Rate, ColorDescription, Option<f32>, Rotation) {3401+) -> (
3402+ Rate,
3403+ ColorDescription,
3404+ Option<f32>,
3405+ Rotation,
3406+ crate::demux::PixelAspect,
3407+) {
3338 if crate::is_image(path) || crate::is_audio(path) {3408 if crate::is_image(path) || crate::is_audio(path) {
3339 // A still is BT.601 by construction whatever it was authored as:3409 // A still is BT.601 by construction whatever it was authored as:
3340 // `decode::rgb_to_i420` is the one matrix that turns its pixels into3410 // `decode::rgb_to_i420` is the one matrix that turns its pixels into
3341 // planes. A song has no picture and the answer is never read. Neither3411 // planes. A song has no picture and the answer is never read. Neither
3342- // has a display matrix to be turned by.3412+ // has a display matrix to be turned by, nor an aspect to be widened
3343- return (Rate::REAL_TIME, ColorDescription::default(), None, Rotation::None);3413+ // by: both are authored square.
3414+ return (
3415+ Rate::REAL_TIME,
3416+ ColorDescription::default(),
3417+ None,
3418+ Rotation::None,
3419+ crate::demux::PixelAspect::SQUARE,
3420+ );
3344 }3421 }
3345 match Demuxer::open(path) {3422 match Demuxer::open(path) {
3346 // ...and one whose rate cannot be named against the timeline's, which3423 // ...and one whose rate cannot be named against the timeline's, which
@@ -3357,10 +3434,19 @@ fn source_rate(
3357 // The file's own turn, so the encoder is fed the picture the3434 // The file's own turn, so the encoder is fed the picture the
3358 // preview shows: the same header read, one more answer.3435 // preview shows: the same header read, one more answer.
3359 meta.rotation,3436 meta.rotation,
3437+ // ...and its own sample aspect, for the same reason on the same
3438+ // read: the encoder is fed the shape the preview showed.
3439+ meta.pixel_aspect,
3360 ),3440 ),
3361 // Unreadable here means unreadable below too, where the span dies with a3441 // Unreadable here means unreadable below too, where the span dies with a
3362 // real message; the file's own space is the least of that.3442 // real message; the file's own space is the least of that.
3363- Err(_) => (Rate::REAL_TIME, ColorDescription::default(), None, Rotation::None),3443+ Err(_) => (
3444+ Rate::REAL_TIME,
3445+ ColorDescription::default(),
3446+ None,
3447+ Rotation::None,
3448+ crate::demux::PixelAspect::SQUARE,
3449+ ),
3364 }3450 }
3365 }3451 }
3366 3452
crates/engine/src/hw.rsmodified+26 -5
@@ -15,7 +15,7 @@
15 //! path -- the same "no" it gives for an unsupported profile, and the caller's15 //! path -- the same "no" it gives for an unsupported profile, and the caller's
16 //! honest [`crate::demux::Codec::needs_plugin`] refusal covers both.16 //! honest [`crate::demux::Codec::needs_plugin`] refusal covers both.
17 17
18-use std::ffi::{CString, c_char, c_void};18+use std::ffi::{c_char, c_void, CString};
19 use std::os::fd::FromRawFd;19 use std::os::fd::FromRawFd;
20 use std::path::{Path, PathBuf};20 use std::path::{Path, PathBuf};
21 use std::sync::OnceLock;21 use std::sync::OnceLock;
@@ -26,7 +26,18 @@ use crate::colorspace::ColorDescription;
26 26
27 const LIB_NAME: &str = "libengine_hw.so";27 const LIB_NAME: &str = "libengine_hw.so";
28 28
29-/// Stream metadata, C layout. Mirrors [`crate::VideoMeta`].29+/// Stream metadata, C layout. Mirrors [`crate::VideoMeta`]'s picture half --
30+/// the four fields a session was opened with, plus the coded pair and the
31+/// sample aspect a display size can now differ from the coded one by.
32+///
33+/// `width`/`height` are the *displayed* shape, exactly as [`crate::VideoMeta`]
34+/// promises: a plugin opened on a rotated file has always been handed a
35+/// swapped pair here, and the pictures it hands back are sized by its own
36+/// decoder's output, which is the coded pair. `coded_width`/`coded_height`
37+/// name that pair explicitly, and `pixel_aspect_h`/`_v` carry the ratio one
38+/// coded pixel is drawn at, so a caller that needs the stored shape (a copy
39+/// declaring a track, an encoder agreeing on a surface) asks the coded one and
40+/// a caller that draws asks the display one -- both answered, neither derived.
30 #[repr(C)]41 #[repr(C)]
31 #[derive(Default)]42 #[derive(Default)]
32 pub struct VhMeta {43 pub struct VhMeta {
@@ -34,6 +45,13 @@ pub struct VhMeta {
34 pub height: u32,45 pub height: u32,
35 pub frame_rate: f64,46 pub frame_rate: f64,
36 pub frame_count: u32,47 pub frame_count: u32,
48+ /// What the samples are stored at, before any turn or stretch.
49+ pub coded_width: u32,
50+ pub coded_height: u32,
51+ /// The sample aspect ratio, h over v, square as 1 and 1 -- or 0 and 0,
52+ /// which is `Default` and reads square ([`crate::demux::PixelAspect`]).
53+ pub pixel_aspect_h: u32,
54+ pub pixel_aspect_v: u32,
37 }55 }
38 56
39 /// One decoded picture as planar I420. Pointers belong to the plugin and stay57 /// One decoded picture as planar I420. Pointers belong to the plugin and stay
@@ -371,7 +389,9 @@ impl HwSession {
371 /// for a few pictures only: encode it before asking this for a few more.389 /// for a few pictures only: encode it before asking this for a few more.
372 pub fn next_frame_dma(&mut self, want: DmaWant) -> crate::Result<Option<HwPicture<'_>>> {390 pub fn next_frame_dma(&mut self, want: DmaWant) -> crate::Result<Option<HwPicture<'_>>> {
373 let Some(next_dma) = self.plugin.next_frame_dma else {391 let Some(next_dma) = self.plugin.next_frame_dma else {
374- return Ok(self.next_frame()?.map(|(y, u, v, w, h)| HwPicture::Pixels(y, u, v, w, h)));392+ return Ok(self
393+ .next_frame()?
394+ .map(|(y, u, v, w, h)| HwPicture::Pixels(y, u, v, w, h)));
375 };395 };
376 let mut frame = VhFrame::default();396 let mut frame = VhFrame::default();
377 let mut dma = VhDma::default();397 let mut dma = VhDma::default();
@@ -482,8 +502,9 @@ struct EncPlugin {
482 /// neither and every picture reaches it as bytes, which is what it always502 /// neither and every picture reaches it as bytes, which is what it always
483 /// did.503 /// did.
484 dma_geometry: Option<unsafe extern "C" fn(*mut c_void, *mut u32, *mut u32) -> i32>,504 dma_geometry: Option<unsafe extern "C" fn(*mut c_void, *mut u32, *mut u32) -> i32>,
485- frame_dma:505+ frame_dma: Option<
486- Option<unsafe extern "C" fn(*mut c_void, *const VhDma, i32, *mut *const u8, *mut usize) -> i32>,506+ unsafe extern "C" fn(*mut c_void, *const VhDma, i32, *mut *const u8, *mut usize) -> i32,
507+ >,
487 drain: unsafe extern "C" fn(*mut c_void, *mut *const u8, *mut usize) -> i32,508 drain: unsafe extern "C" fn(*mut c_void, *mut *const u8, *mut usize) -> i32,
488 close: unsafe extern "C" fn(*mut c_void),509 close: unsafe extern "C" fn(*mut c_void),
489 // Never dropped (lives in a static), so the fn pointers above stay valid.510 // Never dropped (lives in a static), so the fn pointers above stay valid.