forge
Explore
Log in

Don't you dare go Hollow

Clamp a caption window on GROWTH, and raise the ceiling

Browse files at this commit

f23dd8fe2779056c5a83b9cb4646da8f86c1220dAhmet Kaan GümüşParent: d964f6c
  • fix
    • clamp a caption window on GROWTH, and raise the ceiling
Show full message
fix(engine): clamp a caption window on GROWTH, and raise the ceiling

A ceiling is only half an invariant. Every door -- the parse,
`with_subs`, `place_sub` -- sees a window on the way IN, so on its own it
left two holes, both of which cost a user their saved work.

**The app could write a file it refused to open.** `trim_sub`'s End arm
GROWS `out_us`, and it grew past the ceiling: a caption trimmed out to
the room the engine itself offered reached 25 hours, `save_project`
wrote `sub 1 0 3240000 0 0 90039000000`, and `open_project` refused
that file with "past the 86400000000us a caption can hold". The trim is
committed and undo does not survive a restart, so there is no in-app way
back -- the editor wrote work it then could not reopen. The `saturating_add`
that killed the overflow panic did not keep the value inside the bound.

**The ceiling was below real use.** A caption's window is a window into
SOURCE media, so its width is the length of the recording it was cut
from, and that is not bounded by a day. A 25-hour conference stream or a
lecture archive measures its own palette row in tens of hours, so the
ceiling refused the user's own media back at them, at `place_sub`, with
nothing placed. "A window this wide is a number nobody meant" holds for
i64::MAX; it does not hold for a long recording.

So:

- `MAX_WINDOW_US` is 30 days, and the reason is arithmetic rather than
  editorial. `SubClip::window_at`'s proportion cut saturates rather than
  wrapping, so the ceiling is about PRECISION: f64 holds an integer
  exactly to 2^53, and 30 days is 2.6e12 -- nine orders of magnitude
  inside it, so the cut is exact rather than merely finite. It is 3.5e6x
  below i64::MAX, and exact in `sub_lane_cues`'s cue mapping for the same
  reason. No real recording reaches it; a typed number does not survive it.

- `grow_window` is the growth law, and is now the single function that may
  write `out_us`. `trim_sub`'s End arm and the group-follow
  `write_sub_edge` both go through it. Growth CLAMPS and entry REFUSES,
  and the asymmetry is the point: a trim is a continuous gesture whose
  every intermediate frame the user sees, so clamping is a drag that
  stops -- exactly like the frame walls `trim_sub` already applies. A
  value arriving from a file or a foreign caller is not mid-gesture, so
  it is named and refused rather than quietly changed.

- `trim_sub_room` is walled by the same number. A front-end asks it to
  draw the drag and then trims to whatever it is told, so an engine that
  offers a trim its own `trim_sub` would clamp is a drag that snaps back
  at the end of the gesture. The room and the trim now agree by
  construction.

Witnesses, each with its own mutation (saveload.rs):

- `a_trim_grown_to_the_offered_room_saves_and_reopens` -- 40-day track,
  trim to the offered room, save, reopen. Clamp removed: "gave a
  2592027000000us window, past the 2592000000000us ceiling".
- `a_group_trim_carries_its_sibling_no_further_than_the_ceiling` --
  `follow_group` SKIPS the dragged member, so a primary-trim-only fix
  misses this entirely: the route is a sibling on a second lane, placed
  20 days into the track so the same delta overshoots. Clamp removed:
  "the group carried the sibling to a 25919974080030us window" -- 10x the
  ceiling.
- `a_caption_over_a_day_of_track_places_and_round_trips` -- a 25-hour
  track placed exactly as `Player::sub_of_track` builds it. Ceiling
  lowered to a day: refused at `place_sub`, nothing placed.

Swept every other writer of `in_us`/`out_us`, all now inside the bound:

- `sub_clear` (both halves) and `sub_open_room` (both halves): the values
  are `window_at` results, which `SubClip::window_at` already clamps into
  the window -- a split cannot widen what it splits.
- `Project::close_gaps`-style join: takes the neighbour's own `out_us`.
- `trim_sub` Start and `write_sub_edge` Start: `in_us` is clamped to
  `out_us - 1`, which is below the ceiling.
- `app`'s `trimmed_sub`: moves `frames` only, never a clock.
- `Project::sub_lane_cues`: reads the window, writes nothing.
crates/engine/src/project.rsmodified+80 -18
@@ -93,19 +93,37 @@ pub const MIN_GAIN_DB: f32 = -60.0;
93 pub const MAX_GAIN_DB: f32 = 12.0;93 pub const MAX_GAIN_DB: f32 = 12.0;
94 94
95 /// The longest `[in_us, out_us)` window a [`SubClip`] may show, in95 /// The longest `[in_us, out_us)` window a [`SubClip`] may show, in
96-/// microseconds: a day of track, and past it the number is not a caption any96+/// microseconds: **30 days** of track.
97-/// project holds -- a `.edith` a hand edited (or a hostile actor wrote) reaches
98-/// this with a line the never-empty check at the parser cannot see past,
99-/// because `out_us > in_us` says the window is *open* and never how wide it is.
100 ///97 ///
101-/// A ceiling rather than a clamp for the reason [`crate::is_resolution`] is98+/// A window is a stretch of *source media*, not of the timeline, so its width
102-/// one: a window this wide is a number nobody meant, and quietly turning it99+/// is the length of the recording a caption was cut from -- and that is not
103-/// into a day-long caption would put words on screen for a day. The loader100+/// bounded by anything a day. A two-day conference stream, a lecture
104-/// refuses it by name instead, next to every other range check a `.edith` line101+/// archive, a long interview session: all of them produce a palette row whose
105-/// has to pass -- and the arithmetic that cuts a window ([`SubClip::window_at`])102+/// `out_us` is measured in tens of hours, and a ceiling that refuses those is
106-/// saturates besides, so no caller can reintroduce the wrap this door is here103+/// refusing the user's own media back at them. So the ceiling is set where no
107-/// to stop.104+/// real recording reaches and a typed number does not survive.
108-pub const MAX_WINDOW_US: i64 = 86_400_000_000;105+///
106+/// What it has to clear, concretely:
107+///
108+/// - [`SubClip::window_at`]'s proportion cut. It saturates rather than
109+/// wrapping, so this is about *precision*, not safety: `f64` holds an
110+/// integer exactly to 2^53, and 30 days is 2.6e12 -- nine orders of magnitude
111+/// inside that, so the cut is exact rather than merely finite.
112+/// - The `i64` multiply underneath it, which `saturating_mul` already makes
113+/// total. 30 days is 3.5e6x below `i64::MAX`, so it never saturates in
114+/// practice either.
115+/// - `sub_lane_cues`'s f64 mapping of cues onto the timeline, likewise exact.
116+///
117+/// Past it, the value is not a caption any project holds -- a `.edith` a hand
118+/// edited (or a hostile actor wrote) reaches this with a line the
119+/// never-empty check at the parser cannot see past, because `out_us > in_us`
120+/// says the window is *open* and never how wide it is. That line is refused by
121+/// name, next to every other range check a `.edith` line has to pass. The
122+/// bounds that *grow* a window rather than read one -- [`Project::trim_sub`]
123+/// and the group-follow twin it calls -- clamp to this rather than refuse,
124+/// because a trim that ran out of window should stop at the window's end, not
125+/// fail: see [`MAX_WINDOW_US`'s growth law](MAX_WINDOW_US).
126+pub const MAX_WINDOW_US: i64 = 30 * 24 * 60 * 60 * 1_000_000;
109 127
110 /// How fast a clip plays, in **thousandths of real time**: 1000 is the speed it128 /// How fast a clip plays, in **thousandths of real time**: 1000 is the speed it
111 /// was shot at, 2000 twice that, 500 half. An integer and not an `f32` for129 /// was shot at, 2000 twice that, 500 half. An integer and not an `f32` for
@@ -2257,10 +2275,18 @@ impl Project {
2257 s.start = to;2275 s.start = to;
2258 }2276 }
2259 Edge::End => {2277 Edge::End => {
2260- s.out_us = s2278+ // ...and `grow_window`, because this arm is where a caption's
2261- .out_us2279+ // clock grows and the doors only ever *read* one. Without it a
2262- .saturating_add(us_of(i64::from(to) - i64::from(s.end()), fps))2280+ // trim out to the end of a long track produced a window past
2263- .max(s.in_us + 1);2281+ // [`MAX_WINDOW_US`], `save_project` wrote it, and the loader
2282+ // then refused the file the editor had just written -- a user's
2283+ // saved work that stops opening, with no in-app way back. The
2284+ // trim stops at the ceiling instead, which is the same answer
2285+ // the frame walls above already give for running out of room.
2286+ s.out_us = grow_window(
2287+ s.in_us,
2288+ s.out_us.saturating_add(us_of(i64::from(to) - i64::from(s.end()), fps)),
2289+ );
2264 s.frames = to - s.start;2290 s.frames = to - s.start;
2265 }2291 }
2266 }2292 }
@@ -2311,10 +2337,17 @@ impl Project {
2311 .get(s.track)2337 .get(s.track)
2312 .and_then(|t| t.cues.iter().map(|c| c.end_us).max())2338 .and_then(|t| t.cues.iter().map(|c| c.end_us).max())
2313 .unwrap_or(s.out_us);2339 .unwrap_or(s.out_us);
2340+ // ...and never past [`grow_window`]'s ceiling, which is the
2341+ // wall that matters here: a front-end asks this to draw the
2342+ // drag, then trims to whatever it is told. An engine that
2343+ // offers room its own trim would refuse is a drag that snaps
2344+ // back at the end of the gesture -- so the room stops where
2345+ // the window stops, and the two agree by construction.
2346+ let wall_us = track_end.min(s.in_us.saturating_add(MAX_WINDOW_US));
2314 (2347 (
2315 s.start + 1,2348 s.start + 1,
2316 s.start2349 s.start
2317- .saturating_add(frames_of(track_end - s.in_us).max(1))2350+ .saturating_add(frames_of((wall_us - s.in_us).max(1)))
2318 .min(subs.get(idx + 1).map_or(u32::MAX, |n| n.start)),2351 .min(subs.get(idx + 1).map_or(u32::MAX, |n| n.start)),
2319 )2352 )
2320 }2353 }
@@ -5644,6 +5677,31 @@ fn us_of(frames: i64, fps: f64) -> i64 {
5644 }5677 }
5645 }5678 }
5646 5679
5680+/// The growth law, and the one function here that may write `out_us` past what
5681+/// a caller asked for: the ceiling a caption's window may reach, given the
5682+/// `in_us` it starts from.
5683+///
5684+/// Every *door* -- the parser, [`Project::with_subs`], [`Project::place_sub`]
5685+/// -- refuses a window wider than [`MAX_WINDOW_US`] by name. A door only sees
5686+/// a value on the way IN, so on its own it is not enough: [`Project::trim_sub`]
5687+/// and the group-follow [`write_sub_edge`] both *grow* `out_us`, and a project
5688+/// could be trimmed past the ceiling after loading legally. That produced a
5689+/// file `save_project` wrote and `open_project` then refused -- the user's own
5690+/// saved work, stopping, with no way back in the app.
5691+///
5692+/// So growth clamps and entry refuses, and that asymmetry is the point: a trim
5693+/// is a continuous gesture whose every intermediate frame the user sees, so
5694+/// clamping it to the end of the window is a drag that stops -- exactly like
5695+/// the frame walls `trim_sub` already applies. A value *arriving* from a file
5696+/// or a foreign caller is a different thing, where nobody was mid-gesture, so
5697+/// it is named and refused rather than quietly changed.
5698+///
5699+/// The `+ 1` is the never-empty invariant: the window is open, always, so a
5700+/// growth that would otherwise clamp it shut lands on one microsecond instead.
5701+fn grow_window(in_us: i64, out_us: i64) -> i64 {
5702+ out_us.clamp(in_us + 1, in_us.saturating_add(MAX_WINDOW_US))
5703+}
5704+
5647 /// [`clear`] for a subtitle lane: removes the timeline frames `[start, end)`,5705 /// [`clear`] for a subtitle lane: removes the timeline frames `[start, end)`,
5648 /// dropping what is inside the hole, splitting what straddles it and trimming5706 /// dropping what is inside the hole, splitting what straddles it and trimming
5649 /// what overlaps an edge. The windows follow the cut by proportion5707 /// what overlaps an edge. The windows follow the cut by proportion
@@ -5757,7 +5815,11 @@ fn write_sub_edge(s: &mut SubClip, edge: Edge, to: u32) {
5757 s.start = to;5815 s.start = to;
5758 }5816 }
5759 Edge::End => {5817 Edge::End => {
5760- s.out_us = s.out_us.saturating_add(by(s.end(), to)).max(s.in_us + 1);5818+ // `grow_window`, for [`Project::trim_sub`]'s reason: this is the
5819+ // group-follow twin and it reaches the same field, so a group trim
5820+ // would otherwise be a way round the ceiling the primary trim
5821+ // respects.
5822+ s.out_us = grow_window(s.in_us, s.out_us.saturating_add(by(s.end(), to)));
5761 s.frames = to - s.start;5823 s.frames = to - s.start;
5762 }5824 }
5763 }5825 }
crates/engine/tests/saveload.rsmodified+218 -0
@@ -1083,3 +1083,221 @@ fn a_caption_window_no_project_can_hold_is_refused_by_name() {
1083 session.sub_lane(s1)1083 session.sub_lane(s1)
1084 );1084 );
1085 }1085 }
1086+
1087+/// A subtitle track whose last cue ends `hours` in, written into `dir`.
1088+///
1089+/// A caption's window is a window into SOURCE media, so its width is however
1090+/// long the recording was. Nothing in the format bounds that, and a lecture
1091+/// archive or a conference stream is tens of hours -- which is what a ceiling
1092+/// under a day refuses back at the user. The hour field is parsed as a plain
1093+/// `u32` with no digit limit, so 960 is a legal 40 days.
1094+fn long_srt(dir: &Path, hours: u32) -> PathBuf {
1095+ let path = dir.join(format!("subs_{hours}h.srt"));
1096+ std::fs::write(
1097+ &path,
1098+ format!(
1099+ "1\n00:00:00,500 --> 00:00:01,500\nfirst line\n\n\
1100+ 2\n00:00:01,600 --> {hours}:00:00,000\nlast line of a long recording\n"
1101+ ),
1102+ )
1103+ .expect("write a long subtitle track");
1104+ path
1105+}
1106+
1107+/// The round trip a trim has to survive: grow a caption's window to the room
1108+/// the engine itself offers, save, and open the file that was just written.
1109+///
1110+/// This is the hole a ceiling on its own leaves. Every door -- the parse,
1111+/// `with_subs`, `place_sub` -- only ever sees a window on the way *in*, so a
1112+/// project could load legally and then be trimmed past the ceiling: `save`
1113+/// wrote the line and the loader refused it again, which is the user's own
1114+/// saved work stopping, with no in-app way back (the trim is committed and the
1115+/// undo does not survive a restart). So growth clamps (`grow_window`) and the
1116+/// room the front-end drags against is walled by the same number, and the two
1117+/// agree by construction rather than by luck.
1118+#[test]
1119+fn a_trim_grown_to_the_offered_room_saves_and_reopens() {
1120+ use engine::project::{Edge, MAX_WINDOW_US, SubClip};
1121+
1122+ let dir = scratch("caption_growth");
1123+ let media = copy_in(&dir, "test_av.mp4");
1124+ // 40 days of track, comfortably past the 30-day ceiling: only the growth
1125+ // clamp can keep this inside it, never the ceiling itself.
1126+ let subs = long_srt(&dir, 960);
1127+
1128+ let mut session = PlaybackSession::open(&media).expect("open the fixture");
1129+ session
1130+ .import_subtitles(&subs)
1131+ .expect("a 40-day subtitle track imports");
1132+ let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1133+ session
1134+ .place_sub(
1135+ s1,
1136+ 60,
1137+ SubClip {
1138+ start: 0,
1139+ frames: 90,
1140+ track: 0,
1141+ in_us: 0,
1142+ out_us: 30_000_000,
1143+ link: None,
1144+ },
1145+ )
1146+ .expect("a 30s window places");
1147+
1148+ // The room the drag is drawn against. It must itself stop at the ceiling:
1149+ // an engine that offers a trim its own `trim_sub` would clamp is a drag
1150+ // that snaps back at the end of the gesture.
1151+ let (_, last) = session
1152+ .trim_sub_room(s1, 0, Edge::End)
1153+ .expect("a room for a caption that is there");
1154+ session
1155+ .trim_sub(s1, 0, Edge::End, last)
1156+ .expect("the trim to its own offered room");
1157+
1158+ let grown = session.sub_lane(s1)[0];
1159+ assert!(
1160+ (grown.out_us - grown.in_us) <= MAX_WINDOW_US,
1161+ "trimming to the offered room gave a {}us window, past the {}us ceiling",
1162+ grown.out_us - grown.in_us,
1163+ MAX_WINDOW_US
1164+ );
1165+
1166+ let saved = dir.join("grown.edith");
1167+ session.save_project(&saved).expect("save");
1168+ let reopened = PlaybackSession::open_project(&saved)
1169+ .expect("a project the editor wrote must open again -- that is the whole claim");
1170+ let back = reopened.sub_lane(s1)[0];
1171+ assert_eq!(
1172+ (back.in_us, back.out_us),
1173+ (grown.in_us, grown.out_us),
1174+ "and the window came back as it was saved"
1175+ );
1176+}
1177+
1178+/// The ceiling itself, against the case it was measured on: a 25-hour
1179+/// recording. `place_sub` is the door a palette row's drag comes through
1180+/// (`sub_of_track` builds the placement from the track's own last cue), so a
1181+/// ceiling under the length of real media refuses the user's own file back at
1182+/// them, with nothing placed.
1183+#[test]
1184+fn a_caption_over_a_day_of_track_places_and_round_trips() {
1185+ use engine::project::MAX_WINDOW_US;
1186+
1187+ let dir = scratch("caption_long_track");
1188+ let media = copy_in(&dir, "test_av.mp4");
1189+ let subs = long_srt(&dir, 25);
1190+ let whole_track_us = 25i64 * 60 * 60 * 1_000_000;
1191+ assert!(
1192+ whole_track_us > 86_400_000_000,
1193+ "the fixture must really be over a day, or this test proves nothing"
1194+ );
1195+ assert!(
1196+ whole_track_us < MAX_WINDOW_US,
1197+ "and comfortably under the ceiling, or the test is not about the ceiling"
1198+ );
1199+
1200+ let mut session = PlaybackSession::open(&media).expect("open the fixture");
1201+ assert_eq!(
1202+ session.import_subtitles(&subs).expect("a 25-hour track imports"),
1203+ 1,
1204+ "one track"
1205+ );
1206+ let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1207+ // Exactly what `Player::sub_of_track` hands `place_sub` for palette row 0.
1208+ let fps = f64::from(session.meta().frame_rate);
1209+ session
1210+ .place_sub(
1211+ s1,
1212+ 0,
1213+ engine::project::SubClip {
1214+ start: 0,
1215+ frames: ((whole_track_us as f64) * fps / 1e6).round() as u32,
1216+ track: 0,
1217+ in_us: 0,
1218+ out_us: whole_track_us,
1219+ link: None,
1220+ },
1221+ )
1222+ .expect("a caption over a 25-hour recording places -- that is the claim");
1223+
1224+ let saved = dir.join("long.edith");
1225+ session.save_project(&saved).expect("save");
1226+ let reopened = PlaybackSession::open_project(&saved).expect("and reopens");
1227+ assert_eq!(
1228+ reopened.sub_lane(s1)[0].out_us, whole_track_us,
1229+ "the whole window survived the round trip"
1230+ );
1231+}
1232+
1233+/// The second writer, which is the one a primary-trim-only fix would miss.
1234+///
1235+/// [`write_sub_edge`] carries a *sibling* in a group by the same delta as the
1236+/// member that was dragged, and reaches the same `out_us`. `follow_group`
1237+/// skips the dragged member, so the route here is a second caption in the
1238+/// group on another lane: it is not the one whose `trim_sub` arm is fixed, so
1239+/// nothing about the dragged caption's own clamp bounds it.
1240+///
1241+/// The sibling starts deep into the track -- 20 days in -- so the same delta
1242+/// that stops the dragged caption exactly at the ceiling carries the sibling
1243+/// past it. Without the clamp on this writer the sibling is 20 days wider than
1244+/// the ceiling when `save_project` runs, and the file it writes will not open.
1245+#[test]
1246+fn a_group_trim_carries_its_sibling_no_further_than_the_ceiling() {
1247+ use engine::project::{Edge, LaneKind, MAX_WINDOW_US, SubClip};
1248+
1249+ let dir = scratch("caption_group_growth");
1250+ let media = copy_in(&dir, "test_av.mp4");
1251+ let subs = long_srt(&dir, 960);
1252+ let day = 24 * 60 * 60 * 1_000_000i64;
1253+
1254+ let mut session = PlaybackSession::open(&media).expect("open the fixture");
1255+ session.import_subtitles(&subs).expect("a 40-day track imports");
1256+ let s1 = session.add_lane(LaneKind::Subtitle);
1257+ let s2 = session.add_lane(LaneKind::Subtitle);
1258+ let caption = |in_us| SubClip {
1259+ start: 0,
1260+ frames: 90,
1261+ track: 0,
1262+ in_us,
1263+ out_us: in_us + 30_000_000,
1264+ link: None,
1265+ };
1266+ session
1267+ .place_sub(s1, 60, caption(0))
1268+ .expect("the dragged caption places");
1269+ session
1270+ .place_sub(s2, 60, caption(20 * day))
1271+ .expect("its sibling, 20 days into the track");
1272+ session
1273+ .group_all(&[(s1, 0), (s2, 0)])
1274+ .expect("the two captions into one group");
1275+
1276+ // Drag the first caption's tail out to the room the engine offers it, which
1277+ // is a 40-day track so the room is the ceiling, not the track.
1278+ let (_, last) = session
1279+ .trim_sub_room(s1, 0, Edge::End)
1280+ .expect("a room for the dragged caption");
1281+ session
1282+ .trim_sub(s1, 0, Edge::End, last)
1283+ .expect("the drag commits");
1284+
1285+ let dragged = session.sub_lane(s1)[0];
1286+ assert_eq!(
1287+ dragged.out_us - dragged.in_us,
1288+ MAX_WINDOW_US,
1289+ "the dragged caption stops exactly at the ceiling, which is the room it was offered"
1290+ );
1291+ let sibling = session.sub_lane(s2)[0];
1292+ assert!(
1293+ (sibling.out_us - sibling.in_us) <= MAX_WINDOW_US,
1294+ "the group carried the sibling to a {}us window, past the {}us ceiling -- \
1295+ the dragged caption's own clamp does not reach here",
1296+ sibling.out_us - sibling.in_us,
1297+ MAX_WINDOW_US
1298+ );
1299+
1300+ let saved = dir.join("group.edith");
1301+ session.save_project(&saved).expect("save");
1302+ PlaybackSession::open_project(&saved).expect("and the file the editor wrote reopens");
1303+}