Grouped caption edits reseek, and the link counter clears caption ids
Browse files at this commit25855e817cd6c5a8df49b269fbfbc9523f21d255Parent: 66d5395- fix
- grouped caption edits reseek, and the link counter clears caption ids
4 changed files +211 -32
- crates/app/src/player/timeline_edit.rs +41 -16
- crates/engine/src/edith.rs +64 -0
- crates/engine/src/playback.rs +33 -14
- crates/engine/src/project.rs +73 -2
crates/app/src/player/timeline_edit.rsmodified+41 -16
| @@ -396,23 +396,39 @@ impl Player { | |||
| 396 | // ([`Project::place_sub`]'s sorted insert), so one placed *before* | 396 | // ([`Project::place_sub`]'s sorted insert), so one placed *before* |
| 397 | // the marked caption slid it one along and the index left behind | 397 | // the marked caption slid it one along and the index left behind |
| 398 | // names its new neighbour -- the caption the next Delete would take. | 398 | // names its new neighbour -- the caption the next Delete would take. |
| 399 | - // The mark goes back on the caption it was on. | 399 | + // The mark goes back on the caption it was on, every *other* pick |
| 400 | + // this lane held goes with the indices that moved (they name | ||
| 401 | + // neighbours now), and picks on other lanes keep what they name: | ||
| 402 | + // a placement renumbers this lane alone. | ||
| 400 | (None, Some(start)) => { | 403 | (None, Some(start)) => { |
| 401 | let mark = self | 404 | let mark = self |
| 402 | .session | 405 | .session |
| 403 | .as_ref() | 406 | .as_ref() |
| 404 | .and_then(|session| sub_mark(session.sub_lane(to), start)) | 407 | .and_then(|session| sub_mark(session.sub_lane(to), start)) |
| 405 | .map(|i| (to, i)); | 408 | .map(|i| (to, i)); |
| 406 | - self.selected = match mark { | 409 | + let mut kept = Selection::new(); |
| 407 | - Some(mark) => { | 410 | + for &(lane, idx) in self.selected.picks() { |
| 408 | - let mut sel = Selection::new(); | 411 | + if lane != to { |
| 409 | - sel.set_one(mark); | 412 | + kept.add((lane, idx)); |
| 410 | - sel | ||
| 411 | } | 413 | } |
| 412 | - None => Selection::new(), | 414 | + } |
| 413 | - }; | 415 | + if let Some(mark) = mark { |
| 416 | + kept.add(mark); | ||
| 417 | + } | ||
| 418 | + self.selected = kept; | ||
| 419 | + } | ||
| 420 | + // Nothing marked on this lane, but the placement renumbered it all | ||
| 421 | + // the same: a pick left on a slid index names the wrong caption, | ||
| 422 | + // and the discipline of every renumbering edit applies. | ||
| 423 | + (None, None) => { | ||
| 424 | + let mut kept = Selection::new(); | ||
| 425 | + for &(lane, idx) in self.selected.picks() { | ||
| 426 | + if lane != to { | ||
| 427 | + kept.add((lane, idx)); | ||
| 428 | + } | ||
| 429 | + } | ||
| 430 | + self.selected = kept; | ||
| 414 | } | 431 | } |
| 415 | - (None, None) => {} | ||
| 416 | } | 432 | } |
| 417 | cx.notify(); | 433 | cx.notify(); |
| 418 | } | 434 | } |
| @@ -458,7 +474,9 @@ impl Player { | |||
| 458 | // A lane holds its captions in start order, so the drop moved the | 474 | // A lane holds its captions in start order, so the drop moved the |
| 459 | // mark as well as the box: it is re-read off where the caption | 475 | // mark as well as the box: it is re-read off where the caption |
| 460 | // landed rather than left on the index it had, which after the move | 476 | // landed rather than left on the index it had, which after the move |
| 461 | - // is a neighbour's. | 477 | + // is a neighbour's. A grouped caption dragged its clips with it -- |
| 478 | + // the engine reseeks itself, and this owes the flag reset a clip's | ||
| 479 | + // drop pays at the same moment. | ||
| 462 | Some(Ok(())) => { | 480 | Some(Ok(())) => { |
| 463 | let mark = self | 481 | let mark = self |
| 464 | .session | 482 | .session |
| @@ -473,6 +491,9 @@ impl Player { | |||
| 473 | } | 491 | } |
| 474 | None => Selection::new(), | 492 | None => Selection::new(), |
| 475 | }; | 493 | }; |
| 494 | + if drag.sub.link.is_some() { | ||
| 495 | + self.reset_after_reseek(); | ||
| 496 | + } | ||
| 476 | } | 497 | } |
| 477 | None => {} | 498 | None => {} |
| 478 | } | 499 | } |
| @@ -1103,15 +1124,19 @@ impl Player { | |||
| 1103 | // A caption's edge reaches the engine through its own door, at this | 1124 | // A caption's edge reaches the engine through its own door, at this |
| 1104 | // timeline's rate. An `Ok` is *not* "something changed" -- an edge that | 1125 | // timeline's rate. An `Ok` is *not* "something changed" -- an edge that |
| 1105 | // stopped at a wall it already stood against is `Ok` with no undo step | 1126 | // stopped at a wall it already stood against is `Ok` with no undo step |
| 1106 | - // ([`Project::trim_sub`]) -- so only the refusal is ever said out loud, | 1127 | + // ([`Project::trim_sub`]) -- so only the refusal is ever said out loud. |
| 1107 | - // and nothing reseeks: cues are drawn over whatever is decoded. | 1128 | + // A lone caption's trim moves nothing that plays, and nothing reseeks; |
| 1129 | + // a grouped one trimmed its clips with it, and owes the same flag | ||
| 1130 | + // reset a clip's trim pays. | ||
| 1108 | if trim.lane.kind == LaneKind::Subtitle { | 1131 | if trim.lane.kind == LaneKind::Subtitle { |
| 1109 | - if let Some(Err(e)) = self | 1132 | + let trimmed = self |
| 1110 | .session | 1133 | .session |
| 1111 | .as_mut() | 1134 | .as_mut() |
| 1112 | - .map(|session| session.trim_sub(trim.lane, trim.idx, trim.edge, trim.to)) | 1135 | + .map(|session| session.trim_sub(trim.lane, trim.idx, trim.edge, trim.to)); |
| 1113 | - { | 1136 | + match trimmed { |
| 1114 | - self.notify_user(format!("NOT TRIMMED — {e}").into()); | 1137 | + Some(Err(e)) => self.notify_user(format!("NOT TRIMMED — {e}").into()), |
| 1138 | + Some(Ok(())) if trim.link.is_some() => self.reset_after_reseek(), | ||
| 1139 | + _ => {} | ||
| 1115 | } | 1140 | } |
| 1116 | cx.notify(); | 1141 | cx.notify(); |
| 1117 | return; | 1142 | return; |
crates/engine/src/edith.rsmodified+64 -0
| @@ -1485,6 +1485,70 @@ mod tests { | |||
| 1485 | } | 1485 | } |
| 1486 | } | 1486 | } |
| 1487 | 1487 | ||
| 1488 | + /// The caption's group field (v16): written only when the caption is in a | ||
| 1489 | + /// group -- so a project nobody grouped a caption in is the same bytes it | ||
| 1490 | + /// was in v15 -- read back as the id, and refused in an older dialect, | ||
| 1491 | + /// which never wrote one. | ||
| 1492 | + #[test] | ||
| 1493 | + fn a_caption_group_round_trips_and_an_old_dialect_refuses_the_field() { | ||
| 1494 | + let dir = PathBuf::from("/proj"); | ||
| 1495 | + let caption = |start: u32, frames: u32, link| SubClip { | ||
| 1496 | + start, | ||
| 1497 | + frames, | ||
| 1498 | + track: 0, | ||
| 1499 | + in_us: 0, | ||
| 1500 | + out_us: i64::from(frames) * 1_000_000, | ||
| 1501 | + link, | ||
| 1502 | + }; | ||
| 1503 | + let lanes = vec![ | ||
| 1504 | + (LaneKind::Video, vec![clip(0, 0, 30, 0, Some(7))]), | ||
| 1505 | + (LaneKind::Audio, vec![clip(0, 0, 30, 0, Some(7))]), | ||
| 1506 | + (LaneKind::Subtitle, Vec::new()), | ||
| 1507 | + ]; | ||
| 1508 | + let subs = vec![vec![], vec![], vec![caption(0, 30, Some(7)), caption(40, 10, None)]]; | ||
| 1509 | + let bytes = super::emit( | ||
| 1510 | + &dir, | ||
| 1511 | + &doc().1, | ||
| 1512 | + &lanes, | ||
| 1513 | + &[], | ||
| 1514 | + &subs, | ||
| 1515 | + &[subtitle("/proj/subs.srt", None)], | ||
| 1516 | + &[], | ||
| 1517 | + &[], | ||
| 1518 | + (1280, 720), | ||
| 1519 | + None, | ||
| 1520 | + crate::tonemap::Preset::default(), | ||
| 1521 | + false, | ||
| 1522 | + true, | ||
| 1523 | + crate::export::EncoderSeat::default(), | ||
| 1524 | + Limiter::default(), | ||
| 1525 | + 0, | ||
| 1526 | + ); | ||
| 1527 | + let text = String::from_utf8_lossy(&bytes); | ||
| 1528 | + assert!( | ||
| 1529 | + text.contains("sub 1 0 30 0 0 30000000 7\n"), | ||
| 1530 | + "the grouped caption carries its id: {text}" | ||
| 1531 | + ); | ||
| 1532 | + assert!( | ||
| 1533 | + text.contains("sub 1 40 10 0 0 10000000\n"), | ||
| 1534 | + "a caption in no group writes no field: {text}" | ||
| 1535 | + ); | ||
| 1536 | + let back = parse(&bytes, &dir).expect("v16 parses"); | ||
| 1537 | + assert_eq!(back.subs[2][0].link, Some(7)); | ||
| 1538 | + assert_eq!(back.subs[2][1].link, None); | ||
| 1539 | + | ||
| 1540 | + // The older dialect never wrote a sixth field, and one claiming to be | ||
| 1541 | + // it is a file this parser refuses by name. | ||
| 1542 | + let v15 = parse( | ||
| 1543 | + b"edith 15\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\nsubtitle - subs.srt\n\ | ||
| 1544 | + sub 1 0 30 0 0 30000000 7\n", | ||
| 1545 | + &dir, | ||
| 1546 | + ) | ||
| 1547 | + .unwrap_err() | ||
| 1548 | + .to_string(); | ||
| 1549 | + assert_eq!(v15, "line 6: sub wants 5 fields, found 6"); | ||
| 1550 | + } | ||
| 1551 | + | ||
| 1488 | /// The v12 line: whether the project is cut on the stand-ins, written only | 1552 | /// The v12 line: whether the project is cut on the stand-ins, written only |
| 1489 | /// when it is -- and the dialect before it, which had no stand-ins and | 1553 | /// when it is -- and the dialect before it, which had no stand-ins and |
| 1490 | /// whose bytes are therefore unchanged bar the version. | 1554 | /// whose bytes are therefore unchanged bar the version. |
crates/engine/src/playback.rsmodified+33 -14
| @@ -1753,9 +1753,16 @@ impl PlaybackSession { | |||
| 1753 | 1753 | ||
| 1754 | /// Drags a placement to another subtitle lane or another frame | 1754 | /// Drags a placement to another subtitle lane or another frame |
| 1755 | /// ([`Project::move_sub`]). One undo step, and `Ok` with none for a drop | 1755 | /// ([`Project::move_sub`]). One undo step, and `Ok` with none for a drop |
| 1756 | - /// that changed nothing. | 1756 | + /// that changed nothing. A caption in a group drags the group's clips with |
| 1757 | + /// it -- a mapping change -- so this reseeks like a clip's drag does; a | ||
| 1758 | + /// caption in no group moves nothing that plays and disturbs nothing. | ||
| 1757 | pub fn move_sub(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> crate::Result<()> { | 1759 | pub fn move_sub(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> crate::Result<()> { |
| 1758 | - self.project.move_sub(from, idx, to, start) | 1760 | + let grouped = self.caption_grouped_with_clips(from, idx); |
| 1761 | + let moved = self.project.move_sub(from, idx, to, start); | ||
| 1762 | + if moved.is_ok() && grouped { | ||
| 1763 | + self.invalidate(Dirty::Both); | ||
| 1764 | + } | ||
| 1765 | + moved | ||
| 1759 | } | 1766 | } |
| 1760 | 1767 | ||
| 1761 | /// Drags one edge of a placement to timeline frame `to` | 1768 | /// Drags one edge of a placement to timeline frame `to` |
| @@ -1763,9 +1770,16 @@ impl PlaybackSession { | |||
| 1763 | /// placement's position is in frames while its words are in microseconds, | 1770 | /// placement's position is in frames while its words are in microseconds, |
| 1764 | /// and the rate that joins them is the one thing a [`Project`] does not | 1771 | /// and the rate that joins them is the one thing a [`Project`] does not |
| 1765 | /// know. That is why a front-end trims through this door and never converts | 1772 | /// know. That is why a front-end trims through this door and never converts |
| 1766 | - /// a cue's microseconds itself. One undo step. | 1773 | + /// a cue's microseconds itself. One undo step. A caption in a group trims |
| 1774 | + /// the group's clips with it, and that half reseeked; a lone caption's trim | ||
| 1775 | + /// changes only the words, which are drawn over whatever is decoded. | ||
| 1767 | pub fn trim_sub(&mut self, lane: Lane, idx: usize, edge: Edge, to: u32) -> crate::Result<()> { | 1776 | pub fn trim_sub(&mut self, lane: Lane, idx: usize, edge: Edge, to: u32) -> crate::Result<()> { |
| 1768 | - self.project.trim_sub(lane, idx, edge, to, self.meta.frame_rate) | 1777 | + let grouped = self.caption_grouped_with_clips(lane, idx); |
| 1778 | + let trimmed = self.project.trim_sub(lane, idx, edge, to, self.meta.frame_rate); | ||
| 1779 | + if trimmed.is_ok() && grouped { | ||
| 1780 | + self.invalidate(Dirty::Both); | ||
| 1781 | + } | ||
| 1782 | + trimmed | ||
| 1769 | } | 1783 | } |
| 1770 | 1784 | ||
| 1771 | /// How far that edge may travel, `(first, last)` timeline frame inclusive | 1785 | /// How far that edge may travel, `(first, last)` timeline frame inclusive |
| @@ -1841,8 +1855,20 @@ impl PlaybackSession { | |||
| 1841 | pub fn delete_sub(&mut self, lane: Lane, idx: usize) -> bool { | 1855 | pub fn delete_sub(&mut self, lane: Lane, idx: usize) -> bool { |
| 1842 | // Which of the two, asked before anything moves: the reseek a grouped | 1856 | // Which of the two, asked before anything moves: the reseek a grouped |
| 1843 | // delete owes is the whole difference between the paths. | 1857 | // delete owes is the whole difference between the paths. |
| 1844 | - let grouped = self | 1858 | + match self.caption_grouped_with_clips(lane, idx) { |
| 1845 | - .project | 1859 | + true => self.edit(Dirty::Both, |p| p.delete_sub_in(lane, idx)), |
| 1860 | + // A caption in no group -- or one grouped only with other captions, | ||
| 1861 | + // which lift and move nothing that plays -- is the lift it always | ||
| 1862 | + // was. | ||
| 1863 | + false => self.project.delete_sub_in(lane, idx), | ||
| 1864 | + } | ||
| 1865 | + } | ||
| 1866 | + | ||
| 1867 | + /// Whether the caption at `idx` of `lane` is grouped with clips on other | ||
| 1868 | + /// lanes: the one question every caption edit that can move media has to | ||
| 1869 | + // ask first, because the answer is whether the edit owes a reseek. | ||
| 1870 | + fn caption_grouped_with_clips(&self, lane: Lane, idx: usize) -> bool { | ||
| 1871 | + self.project | ||
| 1846 | .sub_lane(lane) | 1872 | .sub_lane(lane) |
| 1847 | .get(idx) | 1873 | .get(idx) |
| 1848 | .and_then(|s| s.link) | 1874 | .and_then(|s| s.link) |
| @@ -1851,14 +1877,7 @@ impl PlaybackSession { | |||
| 1851 | .lanes() | 1877 | .lanes() |
| 1852 | .into_iter() | 1878 | .into_iter() |
| 1853 | .any(|l| l != lane && self.project.lane(l).iter().any(|c| c.link == Some(id))) | 1879 | .any(|l| l != lane && self.project.lane(l).iter().any(|c| c.link == Some(id))) |
| 1854 | - }); | 1880 | + }) |
| 1855 | - match grouped { | ||
| 1856 | - true => self.edit(Dirty::Both, |p| p.delete_sub_in(lane, idx)), | ||
| 1857 | - // A caption in no group -- or one grouped only with other captions, | ||
| 1858 | - // which lift and move nothing that plays -- is the lift it always | ||
| 1859 | - // was. | ||
| 1860 | - false => self.project.delete_sub_in(lane, idx), | ||
| 1861 | - } | ||
| 1862 | } | 1881 | } |
| 1863 | 1882 | ||
| 1864 | /// What the clip at `idx` of `lane` is equalized with, or `None` for one | 1883 | /// What the clip at `idx` of `lane` is equalized with, or `None` for one |
crates/engine/src/project.rsmodified+73 -2
| @@ -919,10 +919,17 @@ impl Project { | |||
| 919 | } | 919 | } |
| 920 | } | 920 | } |
| 921 | links_are_consistent(&lanes)?; | 921 | links_are_consistent(&lanes)?; |
| 922 | + // The counter sits past every id the file names, on either of a lane's | ||
| 923 | + // lists: a v16 caption may carry the highest link there is, and a | ||
| 924 | + // re-issued id would silently group it with the next placement that | ||
| 925 | + // asks for one -- the loader refusing a save the editor just wrote. | ||
| 922 | let next_link = lanes | 926 | let next_link = lanes |
| 923 | .iter() | 927 | .iter() |
| 924 | - .flat_map(|l| &l.clips) | 928 | + .flat_map(|l| { |
| 925 | - .filter_map(|c| c.link) | 929 | + let clips = l.clips.iter().filter_map(|c| c.link); |
| 930 | + let subs = l.subs.iter().filter_map(|s| s.link); | ||
| 931 | + clips.chain(subs) | ||
| 932 | + }) | ||
| 926 | .max() | 933 | .max() |
| 927 | // Saturating so a crafted file cannot make the counter wrap: at the | 934 | // Saturating so a crafted file cannot make the counter wrap: at the |
| 928 | // ceiling ids stop being fresh, which loses grouping, not memory. | 935 | // ceiling ids stop being fresh, which loses grouping, not memory. |
| @@ -1445,6 +1452,17 @@ impl Project { | |||
| 1445 | return Err(format!("the {name} lane is out of order or overlaps itself").into()); | 1452 | return Err(format!("the {name} lane is out of order or overlaps itself").into()); |
| 1446 | } | 1453 | } |
| 1447 | data.subs = list; | 1454 | data.subs = list; |
| 1455 | + // The counter also has to clear the ids these captions carry: this | ||
| 1456 | + // is the door a load's placements come through, *after* | ||
| 1457 | + // [`from_parts`](Project::from_parts) seeded it from the clips, and | ||
| 1458 | + // a hand-grouped caption may carry the highest id in the file. | ||
| 1459 | + self.next_link = self.next_link.max( | ||
| 1460 | + data.subs | ||
| 1461 | + .iter() | ||
| 1462 | + .filter_map(|s| s.link) | ||
| 1463 | + .max() | ||
| 1464 | + .map_or(0, |m| m.saturating_add(1)), | ||
| 1465 | + ); | ||
| 1448 | } | 1466 | } |
| 1449 | Ok(self) | 1467 | Ok(self) |
| 1450 | } | 1468 | } |
| @@ -4802,6 +4820,59 @@ mod tests { | |||
| 4802 | assert_eq!(shape(&p), before); | 4820 | assert_eq!(shape(&p), before); |
| 4803 | } | 4821 | } |
| 4804 | 4822 | ||
| 4823 | + /// The link counter a load seeds sits past every id the file names, | ||
| 4824 | + /// captions included: a hand-grouped caption may carry the highest id there | ||
| 4825 | + /// is, and a counter that read the clips alone would hand it to the next | ||
| 4826 | + /// group that asked -- silently grouping the caption with clips it never | ||
| 4827 | + /// met. | ||
| 4828 | + #[test] | ||
| 4829 | + fn the_link_counter_sits_past_a_caption_only_id() { | ||
| 4830 | + let track = || SubtitleTrack { | ||
| 4831 | + path: FILE.into(), | ||
| 4832 | + track: None, | ||
| 4833 | + language: "eng".into(), | ||
| 4834 | + name: String::new(), | ||
| 4835 | + label: "eng".into(), | ||
| 4836 | + cues: Vec::new(), | ||
| 4837 | + bitmap: false, | ||
| 4838 | + refused: None, | ||
| 4839 | + }; | ||
| 4840 | + let mut p = Project::single(FILE, 9); | ||
| 4841 | + p.add_lane(LaneKind::Subtitle); | ||
| 4842 | + let caption = SubClip { | ||
| 4843 | + start: 0, | ||
| 4844 | + frames: 6, | ||
| 4845 | + track: 0, | ||
| 4846 | + in_us: 0, | ||
| 4847 | + out_us: 6_000_000, | ||
| 4848 | + // The highest id in the file, and only a caption carries it. | ||
| 4849 | + link: Some(1), | ||
| 4850 | + }; | ||
| 4851 | + let mut p = p | ||
| 4852 | + .with_subtitles(vec![track()]) | ||
| 4853 | + .with_subs(vec![Vec::new(), Vec::new(), vec![caption]]) | ||
| 4854 | + .expect("a caption may carry the file's highest id"); | ||
| 4855 | + | ||
| 4856 | + // A new group on lanes the caption is not on mints the next id -- which | ||
| 4857 | + // must not be the caption's. | ||
| 4858 | + let v2 = p.add_lane(LaneKind::Video); | ||
| 4859 | + assert!(p.place(v2, 0, clip(0, 0, 9, 0))); | ||
| 4860 | + p.group_all(&[(Lane::V1, 0), (v2, 0)]) | ||
| 4861 | + .expect("the clips group"); | ||
| 4862 | + let minted = p.lane(Lane::V1)[0].link.expect("grouped"); | ||
| 4863 | + assert_ne!( | ||
| 4864 | + minted, | ||
| 4865 | + 1, | ||
| 4866 | + "the caption's id is not re-issued to clips" | ||
| 4867 | + ); | ||
| 4868 | + assert_eq!( | ||
| 4869 | + p.sub_lane(Lane::new(LaneKind::Subtitle, 0))[0].link, | ||
| 4870 | + Some(1), | ||
| 4871 | + "the caption keeps its own group" | ||
| 4872 | + ); | ||
| 4873 | + assert!(links_are_consistent(&p.lanes).is_ok()); | ||
| 4874 | + } | ||
| 4875 | + | ||
| 4805 | /// The caption a hand groups: `group_all` over clips *and* a caption, the | 4876 | /// The caption a hand groups: `group_all` over clips *and* a caption, the |
| 4806 | /// mates riding along, the offsets kept, the refusals in words. | 4877 | /// mates riding along, the offsets kept, the refusals in words. |
| 4807 | #[test] | 4878 | #[test] |