alpha2-engine 0.6.2

Player and exporter facade for alpha2.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
//! The host-driven step machine one run goes through.

use super::descriptor::{
    AudioChannels, BLOCK_FRAMES_RANGE, Descriptor, Direction, ExtractRequest, InputFrom, InputSpec,
    MAX_EXTRACTOR_OUTPUTS, MAX_EXTRACTOR_PASSES, OutputSpec, Pass, VideoFormat,
};
use super::pixels::luma8_from_rgba8;
use alpha2_media::SourceId;
use alpha2_model::{
    DataAnchor, DataInterval, DataKind, DataSamples, DataSet, DataSetId, DataValues, DataWord,
    MadeBy, MediaKind, Project,
};
use alpha2_time::{FrameRate, MediaTime, TICKS_PER_SECOND, TimeRange};
use std::collections::{BTreeMap, HashSet};
use std::fmt;

/// Where a source frame is asked for, relative to what the pass has seen.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum FrameFrom {
    /// The frame shown at this time, the last with pts at or before it, else the first frame.
    Start(MediaTime),
    /// The next frame after the one with this pts, in the pass direction.
    After(MediaTime),
}

/// What the host must supply next.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Need {
    /// A source frame; [`ExtractSession::supply_video`] or [`ExtractSession::supply_end`] answers.
    SourceFrame {
        /// The source.
        source: SourceId,
        /// Which frame.
        from: FrameFrom,
        /// The pass direction `from` is relative to.
        direction: Direction,
        /// The longest edge of the delivered frame ([`super::delivered_size`]).
        size: u32,
        /// Always supplied as `rgba8`; the session converts.
        format: VideoFormat,
    },
    /// Source audio, answered by [`ExtractSession::supply_audio`]: exactly `frames` frames,
    /// silence past the end of the stream.
    SourceAudio {
        /// The source.
        source: SourceId,
        /// Its audio stream ordinal.
        stream: u32,
        /// The first frame, in samples at `rate` from source time zero.
        start_sample: i64,
        /// How many frames.
        frames: u32,
        /// The sample rate.
        rate: u32,
        /// The channel layout.
        channels: AudioChannels,
    },
    /// A rendered timeline frame, answered by [`ExtractSession::supply_video`].
    TimelineFrame {
        /// The frame's timeline time.
        time: MediaTime,
        /// The longest edge of the delivered frame.
        size: u32,
        /// Always supplied as `rgba8`; the session converts.
        format: VideoFormat,
    },
    /// Timeline mix audio, answered like [`Need::SourceAudio`].
    TimelineAudio {
        /// The first frame, in samples at `rate` from timeline time zero.
        start_sample: i64,
        /// How many frames.
        frames: u32,
        /// The sample rate.
        rate: u32,
        /// The channel layout.
        channels: AudioChannels,
    },
}

/// A supplied video frame: tightly packed straight-alpha sRGB RGBA8 at its delivered size.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SuppliedFrame {
    /// The frame's presentation time on the input's clock.
    pub pts: MediaTime,
    /// Width in pixels.
    pub width: u32,
    /// Height in pixels.
    pub height: u32,
    /// `width * height * 4` bytes, rows top to bottom.
    pub rgba: Vec<u8>,
}

/// A supplied audio block: planar f32, channel after channel.
#[derive(Clone, Debug, PartialEq)]
pub struct SuppliedAudio {
    /// The channel count.
    pub channels: u32,
    /// `channels * frames` samples.
    pub samples: Vec<f32>,
}

/// A frame handed to the extractor.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VideoInput {
    /// Presentation time on the input's clock.
    pub time: MediaTime,
    /// Width in pixels.
    pub width: u32,
    /// Height in pixels.
    pub height: u32,
    /// The pixel format of `bytes`.
    pub format: VideoFormat,
    /// Tightly packed pixels, rows top to bottom.
    pub bytes: Vec<u8>,
}

/// An audio block handed to the extractor.
#[derive(Clone, Debug, PartialEq)]
pub struct AudioInput {
    /// The block's first sample, at `rate` from the clock's zero.
    pub start_sample: i64,
    /// The block's first sample time, the tick at or before it.
    pub start: MediaTime,
    /// Frames in the block.
    pub frames: u32,
    /// The sample rate.
    pub rate: u32,
    /// The channel count.
    pub channels: u32,
    /// `channels * frames` planar samples, channel after channel.
    pub samples: Vec<f32>,
}

/// An input handed to the extractor.
#[derive(Clone, Debug, PartialEq)]
pub enum Delivery {
    /// A video frame.
    Video(VideoInput),
    /// An audio block.
    Audio(AudioInput),
}

/// What the session asks for or hands over next.
#[derive(Clone, Debug, PartialEq)]
pub enum Step {
    /// The host supplies this.
    Need(Need),
    /// The extractor reads this and emits through [`ExtractSession::emit`].
    Deliver(Delivery),
    /// A pass begins.
    PassBoundary {
        /// The pass's position in the request.
        pass: usize,
        /// Its direction.
        direction: Direction,
    },
    /// Every pass ended; the extractor's end and [`ExtractSession::finish`] follow.
    Done,
}

/// One value an extractor emits.
#[derive(Clone, Debug, PartialEq)]
pub enum Emit {
    /// A sample of a `points`, `scalars` or `similarity` output.
    Sample {
        /// The output's name.
        output: String,
        /// The sample time on the input's clock.
        time: MediaTime,
        /// The sample's numbers, `None` for a lost channel.
        values: Vec<Option<f32>>,
    },
    /// A word of a `words` output.
    Word {
        /// The output's name.
        output: String,
        /// The word.
        word: DataWord,
    },
    /// An interval of an `intervals` output.
    Interval {
        /// The output's name.
        output: String,
        /// The interval.
        interval: DataInterval,
    },
}

impl Emit {
    fn output(&self) -> &str {
        match self {
            Self::Sample { output, .. }
            | Self::Word { output, .. }
            | Self::Interval { output, .. } => output,
        }
    }
}

/// Why a session could not start.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum RequestError {
    /// The request names another extractor or version than the descriptor.
    WrongExtractor,
    /// The descriptor breaks a rule (outputs, block size, size).
    InvalidDescriptor(String),
    /// The request reads an input the descriptor does not, or a source the project lacks or
    /// that cannot feed the input.
    InvalidInput(String),
    /// The range is empty or negative, or a pass starts outside it.
    InvalidRange,
    /// The range ends past the source's fingerprinted duration, in ticks.
    RangePastSource(i64),
    /// `size` is zero or above the descriptor's `max_size`.
    InvalidSize,
    /// The audio rate or channels conflict with the descriptor's fixed ones, or are invalid.
    InvalidAudio,
    /// The outputs map names an undeclared output, or repeats a data set id.
    InvalidOutputs(String),
}

impl fmt::Display for RequestError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::WrongExtractor => {
                formatter.write_str("the request names another extractor or version")
            }
            Self::InvalidDescriptor(why) => write!(formatter, "invalid descriptor: {why}"),
            Self::InvalidInput(why) => write!(formatter, "invalid input: {why}"),
            Self::InvalidRange => {
                formatter.write_str("the range is empty or negative, or a pass starts outside it")
            }
            Self::RangePastSource(duration) => write!(
                formatter,
                "the range ends past the source's duration of {duration} ticks"
            ),
            Self::InvalidSize => {
                formatter.write_str("size is zero or above the descriptor's max_size")
            }
            Self::InvalidAudio => formatter.write_str(
                "the audio rate or channels are invalid or conflict with the descriptor",
            ),
            Self::InvalidOutputs(why) => write!(formatter, "invalid outputs: {why}"),
        }
    }
}

impl std::error::Error for RequestError {}

/// Why the session refused something.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum SessionError {
    /// A supply that does not answer the outstanding need.
    UnexpectedSupply(String),
    /// An emit that breaks a rule.
    InvalidEmit {
        /// Its position among the emits handed over at once.
        index: usize,
        /// The rule broken.
        reason: String,
    },
    /// `finish` before the run is done, or a time emitted twice.
    InvalidResult(String),
}

impl fmt::Display for SessionError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnexpectedSupply(why) => write!(formatter, "unexpected supply: {why}"),
            Self::InvalidEmit { index, reason } => {
                write!(formatter, "invalid emit {index}: {reason}")
            }
            Self::InvalidResult(why) => write!(formatter, "invalid result: {why}"),
        }
    }
}

impl std::error::Error for SessionError {}

/// Completed and total deliveries of one pass; the total is unknown for source frames.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct PassProgress {
    /// Deliveries handed over.
    pub completed: u64,
    /// Deliveries the pass will hand over, when known.
    pub total: Option<u64>,
}

/// What the extractor is told when the run begins.
#[derive(Clone, Debug, PartialEq)]
pub struct RunContext {
    /// The interval read.
    pub range: TimeRange,
    /// The passes, in order.
    pub passes: Vec<Pass>,
    /// The clock read.
    pub from: InputFrom,
    /// For video, the longest edge of delivered frames and their format.
    pub video: Option<(u32, VideoFormat)>,
    /// For audio, the rate, channel layout and block size.
    pub audio: Option<(u32, AudioChannels, u32)>,
    /// For timeline frames, their rate.
    pub frame_rate: Option<FrameRate>,
}

#[derive(Clone, Debug)]
enum Input {
    SourceVideo {
        source: SourceId,
        size: u32,
        format: VideoFormat,
    },
    TimelineVideo {
        size: u32,
        format: VideoFormat,
        rate: FrameRate,
    },
    SourceAudio {
        source: SourceId,
        stream: u32,
        rate: u32,
        channels: AudioChannels,
        block: u32,
    },
    TimelineAudio {
        rate: u32,
        channels: AudioChannels,
        block: u32,
    },
}

/// Where the session is.
#[derive(Clone, Debug)]
enum Phase {
    /// The next pass begins with a boundary.
    BeforePass(usize),
    /// In a pass, asking for its next input.
    Running(Cursor),
    /// A need is outstanding.
    Waiting(Cursor, Need),
    /// A supplied input waits to be handed over; the cursor continues after it.
    Delivering(Cursor, Delivery),
    /// All passes ended.
    Done,
    /// The result was taken.
    Finished,
}

#[derive(Clone, Copy, Debug)]
struct Cursor {
    pass: usize,
    /// The last delivered pts for source frames, the next frame index for timeline frames, and the
    /// next block's first sample (forward) or exclusive end (backward) for audio.
    position: Option<i64>,
}

/// The emits of one output, gathered across passes.
#[derive(Default, Debug)]
struct Gathered {
    samples: Vec<(MediaTime, Vec<Option<f32>>)>,
    words: Vec<DataWord>,
    intervals: Vec<DataInterval>,
}

impl Gathered {
    /// Items of the one kind an output gathers.
    fn len(&self) -> usize {
        self.samples.len() + self.words.len() + self.intervals.len()
    }
}

/// Why an emit to `output` was refused at its data limit.
fn over_limit(output: &OutputSpec) -> String {
    let (limit, what) = match output.kind {
        DataKind::Points | DataKind::Scalars | DataKind::Similarity => {
            (alpha2_model::MAX_DATA_VALUES, "values")
        }
        DataKind::Words => (alpha2_model::MAX_DATA_WORDS, "words"),
        DataKind::Intervals => (alpha2_model::MAX_DATA_INTERVALS, "intervals"),
    };
    format!("output {} would hold more than {limit} {what}", output.name)
}

/// One run of an extractor over a range, driven by the host.
///
/// The host loops on [`Self::next`]: it supplies each [`Need`], hands each [`Delivery`] to the
/// extractor and passes the extractor's [`Emit`]s to [`Self::emit`], and stops at
/// [`Step::Done`]. Cancelling is not calling `next` again. [`Self::finish`] returns validated
/// data sets. The session never decodes, renders or reads files.
#[derive(Debug)]
pub struct ExtractSession {
    descriptor: Descriptor,
    request: ExtractRequest,
    input: Input,
    anchor: DataAnchor,
    passes: Vec<Pass>,
    phase: Phase,
    progress: Vec<PassProgress>,
    /// The latest delivery's bound in the current pass, a frame pts or a block's edge.
    delivered: Option<MediaTime>,
    /// The pts of every frame delivered, which emits may name even before the range.
    delivered_pts: HashSet<MediaTime>,
    /// Per output, the latest emitted time in the current pass.
    last_time: BTreeMap<String, MediaTime>,
    gathered: BTreeMap<String, Gathered>,
}

pub(super) fn validate_descriptor(descriptor: &Descriptor) -> Result<(), RequestError> {
    let invalid = |why: &str| Err(RequestError::InvalidDescriptor(why.to_owned()));
    if descriptor.id.is_empty() {
        return invalid("empty id");
    }
    if descriptor.outputs.is_empty() || descriptor.outputs.len() > MAX_EXTRACTOR_OUTPUTS {
        return invalid("needs 1 to 32 outputs");
    }
    let mut names = HashSet::new();
    for output in &descriptor.outputs {
        if output.name.is_empty() || !names.insert(&output.name) {
            return invalid("an output name is empty or repeated");
        }
        let sampled = matches!(output.kind, DataKind::Points | DataKind::Scalars);
        if sampled == output.channels.is_empty() {
            return invalid("points and scalars outputs need channels; others take none");
        }
        if output.channels.len() > alpha2_model::MAX_DATA_CHANNELS {
            return invalid("an output has too many channels");
        }
        let mut channels = HashSet::new();
        if output.channels.iter().any(|channel| {
            channel.is_empty()
                || channel.len() > alpha2_model::MAX_DATA_LABEL_BYTES
                || !channels.insert(channel)
        }) {
            return invalid("a channel name is empty, too long or repeated");
        }
        if output.interpolation.is_some() && output.kind.arity().is_none() {
            return invalid("only sampled outputs take an interpolation");
        }
    }
    if descriptor.input.from().is_empty() {
        return invalid("reads no input");
    }
    match &descriptor.input {
        InputSpec::Video { max_size, .. } if *max_size == 0 => invalid("max_size is zero"),
        InputSpec::Audio {
            block_frames,
            sample_rate,
            ..
        } if !BLOCK_FRAMES_RANGE.contains(block_frames)
            || sample_rate.is_some_and(|rate| !alpha2_model::SAMPLE_RATE_RANGE.contains(&rate)) =>
        {
            invalid("block_frames or sample_rate out of range")
        }
        _ => Ok(()),
    }
}

impl ExtractSession {
    /// Starts a run of the extractor `descriptor` describes, as `request` asks, over `project`.
    ///
    /// # Errors
    /// [`RequestError`] when the descriptor or request breaks a rule.
    #[allow(clippy::too_many_lines)]
    pub fn new(
        project: &Project,
        descriptor: &Descriptor,
        request: ExtractRequest,
    ) -> Result<Self, RequestError> {
        validate_descriptor(descriptor)?;
        if request.extractor != descriptor.id || request.version != descriptor.version {
            return Err(RequestError::WrongExtractor);
        }
        let read = &request.input;
        if !descriptor.input.from().contains(&read.from) {
            return Err(RequestError::InvalidInput(
                "the descriptor does not read this input".to_owned(),
            ));
        }
        let range = read.range;
        if range.is_empty() || range.start().ticks() < 0 {
            return Err(RequestError::InvalidRange);
        }
        if read.passes.len() > MAX_EXTRACTOR_PASSES
            || read.passes.iter().any(|pass| !range.contains(pass.start))
        {
            return Err(RequestError::InvalidRange);
        }
        let source = match read.from {
            InputFrom::Source => {
                let id = read.source_id.as_ref().ok_or_else(|| {
                    RequestError::InvalidInput("source input needs source_id".to_owned())
                })?;
                let source = project
                    .sources
                    .iter()
                    .find(|source| &source.id == id)
                    .ok_or_else(|| RequestError::InvalidInput(format!("unknown source {id}")))?;
                let fits = match &descriptor.input {
                    InputSpec::Video { .. } => {
                        matches!(source.kind, MediaKind::Video | MediaKind::Image)
                    }
                    InputSpec::Audio { .. } => source.kind == MediaKind::Audio || source.has_audio,
                };
                if !fits {
                    return Err(RequestError::InvalidInput(format!(
                        "source {id} cannot feed this input"
                    )));
                }
                let duration = source
                    .fingerprint
                    .as_ref()
                    .and_then(|fingerprint| fingerprint.duration_ticks);
                if let Some(duration) = duration
                    && range.end().ticks() > duration
                {
                    return Err(RequestError::RangePastSource(duration));
                }
                Some(id.clone())
            }
            InputFrom::Timeline => {
                if read.source_id.is_some() {
                    return Err(RequestError::InvalidInput(
                        "timeline input takes no source_id".to_owned(),
                    ));
                }
                None
            }
        };
        let input = match (&descriptor.input, source.clone()) {
            (
                InputSpec::Video {
                    format, max_size, ..
                },
                source,
            ) => {
                if read.stream.is_some() || read.sample_rate.is_some() || read.channels.is_some() {
                    return Err(RequestError::InvalidInput(
                        "video input takes no audio fields".to_owned(),
                    ));
                }
                let size = read.size.unwrap_or(*max_size);
                if size == 0 || size > *max_size {
                    return Err(RequestError::InvalidSize);
                }
                match source {
                    Some(source) => {
                        if read.frame_rate.is_some() {
                            return Err(RequestError::InvalidInput(
                                "source input takes no frame_rate".to_owned(),
                            ));
                        }
                        Input::SourceVideo {
                            source,
                            size,
                            format: *format,
                        }
                    }
                    None => Input::TimelineVideo {
                        size,
                        format: *format,
                        rate: read.frame_rate.unwrap_or(project.settings.frame_rate),
                    },
                }
            }
            (
                InputSpec::Audio {
                    sample_rate,
                    channels,
                    block_frames,
                    ..
                },
                source,
            ) => {
                if read.size.is_some() || read.frame_rate.is_some() {
                    return Err(RequestError::InvalidInput(
                        "audio input takes no video fields".to_owned(),
                    ));
                }
                let rate = match (sample_rate, read.sample_rate) {
                    (Some(fixed), Some(asked)) if *fixed != asked => {
                        return Err(RequestError::InvalidAudio);
                    }
                    (Some(fixed), _) => *fixed,
                    (None, Some(asked)) => asked,
                    (None, None) => project.settings.sample_rate,
                };
                if !alpha2_model::SAMPLE_RATE_RANGE.contains(&rate) {
                    return Err(RequestError::InvalidAudio);
                }
                let layout = match (channels, read.channels) {
                    (Some(fixed), Some(asked)) if *fixed != asked => {
                        return Err(RequestError::InvalidAudio);
                    }
                    (Some(fixed), _) => *fixed,
                    (None, Some(asked)) => asked,
                    (None, None) => AudioChannels::Mono,
                };
                if let Some(source) = source {
                    Input::SourceAudio {
                        source,
                        stream: read.stream.unwrap_or(0),
                        rate,
                        channels: layout,
                        block: *block_frames,
                    }
                } else {
                    if read.stream.is_some() {
                        return Err(RequestError::InvalidInput(
                            "timeline input takes no stream".to_owned(),
                        ));
                    }
                    Input::TimelineAudio {
                        rate,
                        channels: layout,
                        block: *block_frames,
                    }
                }
            }
        };
        let mut ids = HashSet::new();
        for (name, id) in &request.outputs {
            if !descriptor.outputs.iter().any(|output| &output.name == name) {
                return Err(RequestError::InvalidOutputs(format!(
                    "undeclared output {name}"
                )));
            }
            if !ids.insert(id) {
                return Err(RequestError::InvalidOutputs(format!(
                    "data set id {id} repeated"
                )));
            }
        }
        let passes = if read.passes.is_empty() {
            vec![Pass {
                start: range.start(),
                direction: Direction::Forward,
            }]
        } else {
            read.passes.clone()
        };
        let anchor = source.map_or(DataAnchor::Timeline, |source_id| DataAnchor::Source {
            source_id,
        });
        let mut session = Self {
            descriptor: descriptor.clone(),
            request,
            input,
            anchor,
            progress: Vec::new(),
            passes,
            phase: Phase::BeforePass(0),
            delivered: None,
            delivered_pts: HashSet::new(),
            last_time: BTreeMap::new(),
            gathered: BTreeMap::new(),
        };
        session.progress = (0..session.passes.len())
            .map(|pass| PassProgress {
                completed: 0,
                total: session.pass_total(pass),
            })
            .collect();
        Ok(session)
    }

    /// The run's context, for the extractor's `begin`.
    #[must_use]
    pub fn context(&self) -> RunContext {
        let (video, audio, frame_rate) = match &self.input {
            Input::SourceVideo { size, format, .. } => (Some((*size, *format)), None, None),
            Input::TimelineVideo { size, format, rate } => {
                (Some((*size, *format)), None, Some(*rate))
            }
            Input::SourceAudio {
                rate,
                channels,
                block,
                ..
            }
            | Input::TimelineAudio {
                rate,
                channels,
                block,
            } => (None, Some((*rate, *channels, *block)), None),
        };
        RunContext {
            range: self.request.input.range,
            passes: self.passes.clone(),
            from: self.request.input.from,
            video,
            audio,
            frame_rate,
        }
    }

    /// The descriptor the session runs.
    #[must_use]
    pub const fn descriptor(&self) -> &Descriptor {
        &self.descriptor
    }

    /// The request the session runs.
    #[must_use]
    pub const fn request(&self) -> &ExtractRequest {
        &self.request
    }

    /// Completed and total deliveries of each pass.
    #[must_use]
    pub fn progress(&self) -> &[PassProgress] {
        &self.progress
    }

    fn range(&self) -> TimeRange {
        self.request.input.range
    }

    /// Samples of the audio grid at `rate` whose start lies in the range: `[first, last)`.
    fn sample_span(&self, rate: u32) -> (i64, i64) {
        let range = self.range();
        (
            samples_ceil(range.start(), rate),
            samples_ceil(range.end(), rate),
        )
    }

    fn pass_total(&self, pass: usize) -> Option<u64> {
        let Pass { start, direction } = self.passes[pass];
        match &self.input {
            Input::SourceVideo { .. } => None,
            Input::TimelineVideo { rate, .. } => {
                let (first, last) = timeline_frames(self.range(), *rate)?;
                let at = start_frame(start, *rate, first, last)?;
                let count = match direction {
                    Direction::Forward => last - at,
                    Direction::Backward => at - first + 1,
                };
                u64::try_from(count).ok()
            }
            Input::SourceAudio { rate, block, .. } | Input::TimelineAudio { rate, block, .. } => {
                let (first, last) = self.sample_span(*rate);
                let at = samples_ceil(start, *rate).clamp(first, last);
                let span = match direction {
                    Direction::Forward => last - at,
                    Direction::Backward => at - first,
                };
                u64::try_from(span)
                    .ok()
                    .map(|span| span.div_ceil(u64::from(*block)))
            }
        }
    }

    /// The next step. Calling it again before supplying an outstanding need repeats the need.
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> Step {
        loop {
            match std::mem::replace(&mut self.phase, Phase::Done) {
                Phase::BeforePass(pass) if pass >= self.passes.len() => {
                    self.phase = Phase::Done;
                    self.last_time.clear();
                    self.delivered = None;
                    return Step::Done;
                }
                Phase::BeforePass(pass) => {
                    self.phase = Phase::Running(Cursor {
                        pass,
                        position: None,
                    });
                    self.delivered = None;
                    self.last_time.clear();
                    return Step::PassBoundary {
                        pass,
                        direction: self.passes[pass].direction,
                    };
                }
                Phase::Running(cursor) => match self.need_at(cursor) {
                    Some(need) => {
                        self.phase = Phase::Waiting(cursor, need.clone());
                        return Step::Need(need);
                    }
                    None => self.phase = Phase::BeforePass(cursor.pass + 1),
                },
                Phase::Waiting(cursor, need) => {
                    self.phase = Phase::Waiting(cursor, need.clone());
                    return Step::Need(need);
                }
                Phase::Delivering(cursor, delivery) => {
                    self.progress[cursor.pass].completed += 1;
                    self.phase = Phase::Running(cursor);
                    return Step::Deliver(delivery);
                }
                Phase::Done => {
                    self.phase = Phase::Done;
                    return Step::Done;
                }
                Phase::Finished => {
                    self.phase = Phase::Finished;
                    return Step::Done;
                }
            }
        }
    }

    /// The need at `cursor`, or `None` when its pass has ended.
    fn need_at(&self, cursor: Cursor) -> Option<Need> {
        let Pass { start, direction } = self.passes[cursor.pass];
        match &self.input {
            Input::SourceVideo {
                source,
                size,
                format,
            } => {
                if cursor.position == Some(i64::MIN) {
                    return None;
                }
                let from = cursor.position.map_or(FrameFrom::Start(start), |pts| {
                    FrameFrom::After(MediaTime(pts))
                });
                Some(Need::SourceFrame {
                    source: source.clone(),
                    from,
                    direction,
                    size: *size,
                    format: *format,
                })
            }
            Input::TimelineVideo { size, format, rate } => {
                let (first, last) = timeline_frames(self.range(), *rate)?;
                let frame = match cursor.position {
                    Some(frame) => frame,
                    None => start_frame(start, *rate, first, last)?,
                };
                if frame < first || frame >= last {
                    return None;
                }
                Some(Need::TimelineFrame {
                    time: rate.frame_start(frame)?,
                    size: *size,
                    format: *format,
                })
            }
            Input::SourceAudio {
                rate,
                block,
                channels,
                ..
            }
            | Input::TimelineAudio {
                rate,
                block,
                channels,
            } => {
                let (first, last) = self.sample_span(*rate);
                let at = cursor
                    .position
                    .unwrap_or_else(|| samples_ceil(start, *rate).clamp(first, last));
                let block = i64::from(*block);
                let (start_sample, end_sample) = match direction {
                    Direction::Forward => (at, (at + block).min(last)),
                    Direction::Backward => ((at - block).max(first), at),
                };
                if start_sample >= end_sample {
                    return None;
                }
                let frames = u32::try_from(end_sample - start_sample).ok()?;
                Some(match &self.input {
                    Input::SourceAudio { source, stream, .. } => Need::SourceAudio {
                        source: source.clone(),
                        stream: *stream,
                        start_sample,
                        frames,
                        rate: *rate,
                        channels: *channels,
                    },
                    _ => Need::TimelineAudio {
                        start_sample,
                        frames,
                        rate: *rate,
                        channels: *channels,
                    },
                })
            }
        }
    }

    /// Answers an outstanding frame need.
    ///
    /// # Errors
    /// [`SessionError::UnexpectedSupply`] when no frame is asked for, or the frame's size, buffer
    /// or pts does not answer the need.
    pub fn supply_video(&mut self, frame: SuppliedFrame) -> Result<(), SessionError> {
        let Phase::Waiting(cursor, need) = &self.phase else {
            return Err(SessionError::UnexpectedSupply(
                "no frame is asked for".to_owned(),
            ));
        };
        let (cursor, need) = (*cursor, need.clone());
        let (size, format, expected_pts, from) = match &need {
            Need::SourceFrame {
                size, format, from, ..
            } => (*size, *format, None, Some(*from)),
            Need::TimelineFrame { time, size, format } => (*size, *format, Some(*time), None),
            _ => {
                return Err(SessionError::UnexpectedSupply(
                    "audio is asked for".to_owned(),
                ));
            }
        };
        if frame.width == 0 || frame.height == 0 || frame.width.max(frame.height) > size {
            return Err(SessionError::UnexpectedSupply(
                "the frame's size breaks the asked size".to_owned(),
            ));
        }
        let pixels = u64::from(frame.width) * u64::from(frame.height);
        if u64::try_from(frame.rgba.len()).ok() != pixels.checked_mul(4) {
            return Err(SessionError::UnexpectedSupply(
                "the buffer is not width * height * 4 bytes".to_owned(),
            ));
        }
        if expected_pts.is_some_and(|time| time != frame.pts) {
            return Err(SessionError::UnexpectedSupply(
                "the frame is not the asked time".to_owned(),
            ));
        }
        let Pass { direction, .. } = self.passes[cursor.pass];
        if let Some(FrameFrom::After(previous)) = from {
            let ordered = match direction {
                Direction::Forward => frame.pts > previous,
                Direction::Backward => frame.pts < previous,
            };
            if !ordered {
                return Err(SessionError::UnexpectedSupply(
                    "the frame does not follow the last in the pass direction".to_owned(),
                ));
            }
        }
        let range = self.range();
        // Which frames are shown inside the range, and when a pass ends.
        let (deliver, next) = match &self.input {
            Input::SourceVideo { .. } => match direction {
                Direction::Forward => {
                    if frame.pts >= range.end() {
                        (false, Some(i64::MIN))
                    } else {
                        (true, Some(frame.pts.ticks()))
                    }
                }
                Direction::Backward => {
                    let last = frame.pts <= range.start();
                    (true, Some(if last { i64::MIN } else { frame.pts.ticks() }))
                }
            },
            Input::TimelineVideo { rate, .. } => {
                let frame_index = rate.frame_at_or_before(frame.pts);
                let step = match direction {
                    Direction::Forward => 1,
                    Direction::Backward => -1,
                };
                (true, frame_index.map(|index| index + step))
            }
            _ => unreachable!("a frame need comes only from video input"),
        };
        if !deliver {
            self.phase = Phase::Running(Cursor {
                pass: cursor.pass,
                position: next,
            });
            return Ok(());
        }
        self.delivered = Some(frame.pts);
        self.delivered_pts.insert(frame.pts);
        let bytes = match format {
            VideoFormat::Rgba8 => frame.rgba,
            VideoFormat::Luma8 => luma8_from_rgba8(&frame.rgba),
        };
        let delivery = Delivery::Video(VideoInput {
            time: frame.pts,
            width: frame.width,
            height: frame.height,
            format,
            bytes,
        });
        self.phase = Phase::Delivering(
            Cursor {
                pass: cursor.pass,
                position: next,
            },
            delivery,
        );
        Ok(())
    }

    /// Answers an outstanding source-frame need: no such frame exists, so the pass ends.
    ///
    /// # Errors
    /// [`SessionError::UnexpectedSupply`] when no source frame is asked for.
    pub fn supply_end(&mut self) -> Result<(), SessionError> {
        match &self.phase {
            Phase::Waiting(cursor, Need::SourceFrame { .. }) => {
                self.phase = Phase::Running(Cursor {
                    pass: cursor.pass,
                    position: Some(i64::MIN),
                });
                Ok(())
            }
            _ => Err(SessionError::UnexpectedSupply(
                "no source frame is asked for".to_owned(),
            )),
        }
    }

    /// Answers an outstanding audio need with exactly the asked frames.
    ///
    /// # Errors
    /// [`SessionError::UnexpectedSupply`] when no audio is asked for, or the block's channel
    /// count or length does not answer the need.
    pub fn supply_audio(&mut self, block: SuppliedAudio) -> Result<(), SessionError> {
        let Phase::Waiting(cursor, need) = &self.phase else {
            return Err(SessionError::UnexpectedSupply(
                "no audio is asked for".to_owned(),
            ));
        };
        let (cursor, need) = (*cursor, need.clone());
        let (Need::SourceAudio {
            start_sample,
            frames,
            rate,
            channels,
            ..
        }
        | Need::TimelineAudio {
            start_sample,
            frames,
            rate,
            channels,
        }) = need
        else {
            return Err(SessionError::UnexpectedSupply(
                "a frame is asked for".to_owned(),
            ));
        };
        if block.channels == 0
            || (channels == AudioChannels::Mono && block.channels != 1)
            || block.channels > u32::try_from(alpha2_media::MAX_AUDIO_CHANNELS).unwrap_or(u32::MAX)
        {
            return Err(SessionError::UnexpectedSupply(
                "the block's channel count does not answer the need".to_owned(),
            ));
        }
        if block.samples.len()
            != usize::try_from(block.channels).unwrap_or(0) * usize::try_from(frames).unwrap_or(0)
        {
            return Err(SessionError::UnexpectedSupply(
                "the block is not channels * frames samples".to_owned(),
            ));
        }
        if block.samples.iter().any(|sample| !sample.is_finite()) {
            return Err(SessionError::UnexpectedSupply(
                "the block holds a sample that is not finite".to_owned(),
            ));
        }
        let end_sample = start_sample + i64::from(frames);
        let direction = self.passes[cursor.pass].direction;
        self.delivered = Some(MediaTime(match direction {
            Direction::Forward => samples_to_ticks_ceil(end_sample, rate),
            Direction::Backward => alpha2_media::sample_ticks_floor(start_sample, rate),
        }));
        let next = match direction {
            Direction::Forward => end_sample,
            Direction::Backward => start_sample,
        };
        let delivery = Delivery::Audio(AudioInput {
            start_sample,
            start: MediaTime(alpha2_media::sample_ticks_floor(start_sample, rate)),
            frames,
            rate,
            channels: block.channels,
            samples: block.samples,
        });
        self.phase = Phase::Delivering(
            Cursor {
                pass: cursor.pass,
                position: Some(next),
            },
            delivery,
        );
        Ok(())
    }

    /// Validates and records emits; on a refusal nothing of `emits` is recorded.
    ///
    /// During a pass, times lie inside the range (or are a delivered frame's pts), go no further
    /// than what the pass has delivered, and move in the pass direction per output. After
    /// [`Step::Done`] any time inside the range is accepted. A requested output holds at most
    /// [`OutputSpec::max_items`]; emits to a declared output the request does not name are
    /// checked and dropped.
    ///
    /// # Errors
    /// [`SessionError::InvalidEmit`] naming the first offending emit and the rule.
    pub fn emit(&mut self, emits: &[Emit]) -> Result<(), SessionError> {
        let mut last = self.last_time.clone();
        let mut added = BTreeMap::<&str, usize>::new();
        for (index, emit) in emits.iter().enumerate() {
            let invalid = |reason| SessionError::InvalidEmit { index, reason };
            self.check_emit(emit, &mut last).map_err(invalid)?;
            let name = emit.output();
            if !self.request.outputs.contains_key(name) {
                continue;
            }
            let count = added.entry(name).or_default();
            *count += 1;
            let held = self.gathered.get(name).map_or(0, Gathered::len);
            if let Some(output) = self.output(name)
                && held + *count > output.max_items()
            {
                return Err(invalid(over_limit(output)));
            }
        }
        self.last_time = last;
        for emit in emits {
            if !self.request.outputs.contains_key(emit.output()) {
                continue;
            }
            let gathered = self.gathered.entry(emit.output().to_owned()).or_default();
            match emit {
                Emit::Sample { time, values, .. } => gathered.samples.push((*time, values.clone())),
                Emit::Word { word, .. } => gathered.words.push(word.clone()),
                Emit::Interval { interval, .. } => gathered.intervals.push(interval.clone()),
            }
        }
        Ok(())
    }

    fn output(&self, name: &str) -> Option<&OutputSpec> {
        self.descriptor
            .outputs
            .iter()
            .find(|output| output.name == name)
    }

    #[allow(clippy::too_many_lines)]
    fn check_emit(
        &self,
        emit: &Emit,
        latest: &mut BTreeMap<String, MediaTime>,
    ) -> Result<(), String> {
        let output = self
            .output(emit.output())
            .ok_or_else(|| format!("undeclared output {}", emit.output()))?;
        let range = self.range();
        let in_pass = match &self.phase {
            Phase::Running(cursor) | Phase::Waiting(cursor, _) | Phase::Delivering(cursor, _) => {
                Some(cursor.pass)
            }
            Phase::Done => None,
            Phase::BeforePass(_) | Phase::Finished => {
                return Err("no input has been delivered".to_owned());
            }
        };
        let direction = in_pass.map(|pass| self.passes[pass].direction);
        let delivered = self.delivered;
        if in_pass.is_some() && delivered.is_none() {
            return Err("nothing has been delivered in this pass".to_owned());
        }
        let reached = |time: MediaTime| match (direction, delivered) {
            (Some(Direction::Forward), Some(bound)) => time <= bound,
            (Some(Direction::Backward), Some(bound)) => time >= bound,
            _ => true,
        };
        let (time, key) = match emit {
            Emit::Sample { time, values, .. } => {
                if output.kind.arity().is_none() {
                    return Err(format!(
                        "output {} takes {} items, not samples",
                        output.name, output.kind
                    ));
                }
                if values.len() != output.sample_width() {
                    return Err(format!(
                        "output {} takes {} numbers a sample",
                        output.name,
                        output.sample_width()
                    ));
                }
                let arity = output.kind.arity().unwrap_or(1);
                for (group, chunk) in values.chunks(arity).enumerate() {
                    let lost = chunk[0].is_none();
                    for (component, value) in chunk.iter().enumerate() {
                        let ok = match value {
                            None => lost,
                            Some(number) => {
                                !lost
                                    && alpha2_model::value_in_bounds(
                                        output.kind,
                                        component,
                                        *number,
                                    )
                            }
                        };
                        if !ok {
                            return Err(format!(
                                "value {} is lost in part, not finite or out of bounds",
                                group * arity + component
                            ));
                        }
                    }
                }
                let inside = (range.contains(*time) || self.delivered_pts.contains(time))
                    && time.ticks() >= 0;
                if !inside {
                    return Err("the time is outside the range".to_owned());
                }
                if !reached(*time) {
                    return Err("the time is beyond what this pass has delivered".to_owned());
                }
                (*time, true)
            }
            Emit::Word { word, .. } => {
                if output.kind != DataKind::Words {
                    return Err(format!("output {} takes no words", output.name));
                }
                if !alpha2_model::valid_word(word) {
                    return Err(
                        "the word is empty, too long, or does not end after it starts".to_owned(),
                    );
                }
                if word.start < range.start() || word.end > range.end() {
                    return Err("the word lies outside the range".to_owned());
                }
                if !reached(word.start) || !reached(word.end) {
                    return Err("the word is beyond what this pass has delivered".to_owned());
                }
                (word.start, false)
            }
            Emit::Interval { interval, .. } => {
                if output.kind != DataKind::Intervals {
                    return Err(format!("output {} takes no intervals", output.name));
                }
                if !alpha2_model::valid_interval(interval) {
                    return Err(
                        "the interval ends before it starts, or its label or score breaks a limit"
                            .to_owned(),
                    );
                }
                if interval.start < range.start() || interval.end > range.end() {
                    return Err("the interval lies outside the range".to_owned());
                }
                if !reached(interval.start) || !reached(interval.end) {
                    return Err("the interval is beyond what this pass has delivered".to_owned());
                }
                (interval.start, false)
            }
        };
        if let Some(direction) = direction
            && let Some(previous) = latest.get(&output.name)
        {
            // Samples move strictly; items by their starts, ties allowed.
            let ordered = match (direction, key) {
                (Direction::Forward, true) => time > *previous,
                (Direction::Backward, true) => time < *previous,
                (Direction::Forward, false) => time >= *previous,
                (Direction::Backward, false) => time <= *previous,
            };
            if !ordered {
                return Err("times do not move in the pass direction".to_owned());
            }
        }
        if direction.is_some() {
            latest.insert(output.name.clone(), time);
        }
        Ok(())
    }

    /// The recorded outputs as data sets: merged across passes, sorted, with `made_by` set and
    /// `made_from` left for the surface helper.
    ///
    /// # Errors
    /// [`SessionError::InvalidResult`] before [`Step::Done`], for a time emitted twice, or for a
    /// set that breaks a data limit.
    pub fn finish(&mut self) -> Result<Vec<DataSet>, SessionError> {
        if !matches!(self.phase, Phase::Done) {
            return Err(SessionError::InvalidResult(
                "the run is not done".to_owned(),
            ));
        }
        self.phase = Phase::Finished;
        let made_by = MadeBy {
            extractor: self.descriptor.id.clone(),
            version: self.descriptor.version,
            params: self.request.params.clone(),
            plugin: None,
        };
        let mut sets = Vec::new();
        for (name, id) in &self.request.outputs {
            let output = self
                .output(name)
                .cloned()
                .ok_or_else(|| SessionError::InvalidResult(format!("undeclared output {name}")))?;
            let gathered = self.gathered.remove(name).unwrap_or_default();
            let values = Self::values_of(&output, id, gathered)?;
            let set = DataSet {
                id: id.clone(),
                anchor: self.anchor.clone(),
                values,
                made_by: Some(made_by.clone()),
                made_from: None,
            };
            set.validate().map_err(|problem| {
                SessionError::InvalidResult(format!("data set {id} {problem}"))
            })?;
            sets.push(set);
        }
        Ok(sets)
    }

    fn values_of(
        output: &OutputSpec,
        id: &DataSetId,
        mut gathered: Gathered,
    ) -> Result<DataValues, SessionError> {
        Ok(match output.kind {
            DataKind::Points | DataKind::Scalars | DataKind::Similarity => {
                gathered.samples.sort_by_key(|(time, _)| *time);
                if let Some(pair) = gathered
                    .samples
                    .windows(2)
                    .find(|pair| pair[0].0 == pair[1].0)
                {
                    return Err(SessionError::InvalidResult(format!(
                        "output {} has time {} twice (data set {id})",
                        output.name,
                        pair[0].0.ticks()
                    )));
                }
                let samples = DataSamples {
                    interpolation: output.interpolation.unwrap_or_default(),
                    channels: output.channels.clone(),
                    times: gathered.samples.iter().map(|(time, _)| *time).collect(),
                    values: gathered
                        .samples
                        .into_iter()
                        .flat_map(|(_, values)| values)
                        .collect(),
                };
                match output.kind {
                    DataKind::Points => DataValues::Points(samples),
                    DataKind::Scalars => DataValues::Scalars(samples),
                    _ => DataValues::Similarity(samples),
                }
            }
            DataKind::Words => {
                gathered.words.sort_by_key(|word| (word.start, word.end));
                DataValues::Words(gathered.words)
            }
            DataKind::Intervals => {
                gathered
                    .intervals
                    .sort_by_key(|interval| (interval.start, interval.end));
                DataValues::Intervals(gathered.intervals)
            }
        })
    }
}

/// The first sample starting at or after `time` at `rate`.
fn samples_ceil(time: MediaTime, rate: u32) -> i64 {
    let numerator = i128::from(time.ticks()) * i128::from(rate);
    let denominator = i128::from(TICKS_PER_SECOND);
    let samples =
        numerator.div_euclid(denominator) + i128::from(numerator.rem_euclid(denominator) != 0);
    i64::try_from(samples).unwrap_or(i64::MAX)
}

/// The first tick at or after sample `sample` at `rate`.
fn samples_to_ticks_ceil(sample: i64, rate: u32) -> i64 {
    let numerator = i128::from(sample) * i128::from(TICKS_PER_SECOND);
    let denominator = i128::from(rate.max(1));
    let ticks =
        numerator.div_euclid(denominator) + i128::from(numerator.rem_euclid(denominator) != 0);
    i64::try_from(ticks).unwrap_or(i64::MAX)
}

/// The frames of `rate` whose start lies in `range`: `[first, last)`.
fn timeline_frames(range: TimeRange, rate: FrameRate) -> Option<(i64, i64)> {
    let first_at_or_before = rate.frame_at_or_before(range.start())?;
    let first = if rate.frame_start(first_at_or_before)? < range.start() {
        first_at_or_before + 1
    } else {
        first_at_or_before
    };
    let last = rate.frame_at_or_before(MediaTime(range.end().ticks() - 1))? + 1;
    Some((first, last.max(first)))
}

/// The frame a pass starting at `start` begins with, the one shown there, kept in `[first, last)`.
fn start_frame(start: MediaTime, rate: FrameRate, first: i64, last: i64) -> Option<i64> {
    let frame = rate.frame_at_or_before(start)?;
    (first < last).then(|| frame.clamp(first, last - 1))
}