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
//! Subtitle index for efficient timestamp-based lookup.
//!
//! Provides a binary-search-backed index over subtitle cues so callers
//! can quickly find all cues that are active at any given playback
//! position without iterating the entire subtitle list linearly.
#![allow(dead_code)]
/// A single entry stored in the index, pairing a timestamp range with
/// the position of the corresponding cue in the source list.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexEntry {
/// Cue start time in milliseconds.
pub start_ms: i64,
/// Cue end time in milliseconds.
pub end_ms: i64,
/// Position (zero-based) of this cue in the original subtitle list.
pub cue_index: usize,
}
impl IndexEntry {
/// Create a new `IndexEntry`.
#[must_use]
pub fn new(start_ms: i64, end_ms: i64, cue_index: usize) -> Self {
Self {
start_ms,
end_ms,
cue_index,
}
}
/// Returns `true` if the given timestamp falls within `[start_ms, end_ms)`.
#[must_use]
pub fn is_active_at(&self, timestamp_ms: i64) -> bool {
timestamp_ms >= self.start_ms && timestamp_ms < self.end_ms
}
}
/// An index over a subtitle track that supports O(log n) lookups by
/// timestamp via binary search.
///
/// # Example
///
/// ```
/// use oximedia_subtitle::subtitle_index::{IndexEntry, SubtitleIndex};
///
/// let mut idx = SubtitleIndex::new();
/// idx.push(IndexEntry::new(0, 3000, 0));
/// idx.push(IndexEntry::new(3000, 6000, 1));
/// idx.build();
///
/// let active = idx.active_at(1500);
/// assert_eq!(active.len(), 1);
/// assert_eq!(active[0].cue_index, 0);
/// ```
#[derive(Debug, Default)]
pub struct SubtitleIndex {
entries: Vec<IndexEntry>,
/// Whether entries are currently sorted (index is "built").
is_built: bool,
}
impl SubtitleIndex {
/// Create an empty `SubtitleIndex`.
#[must_use]
pub fn new() -> Self {
Self {
entries: Vec::new(),
is_built: false,
}
}
/// Construct an index directly from a slice of `(start_ms, end_ms)` pairs,
/// assigning cue indices automatically.
#[must_use]
pub fn from_ranges(ranges: &[(i64, i64)]) -> Self {
let mut idx = Self::new();
for (i, &(start, end)) in ranges.iter().enumerate() {
idx.entries.push(IndexEntry::new(start, end, i));
}
idx.build();
idx
}
/// Append an entry. The caller must call [`build`](Self::build) before
/// querying if entries are not appended in sorted order.
pub fn push(&mut self, entry: IndexEntry) {
self.is_built = false;
self.entries.push(entry);
}
/// Sort entries by `start_ms` so binary search can be used.
pub fn build(&mut self) {
self.entries.sort_by_key(|e| e.start_ms);
self.is_built = true;
}
/// Return the number of indexed entries.
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
/// Returns `true` if the index contains no entries.
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
/// Find all cue entries whose time range contains `timestamp_ms`.
///
/// Uses binary search to locate the candidate region and then expands
/// left/right to collect all overlapping cues, so the operation is
/// O(log n + k) where k is the number of matches.
///
/// # Panics
///
/// Panics in debug builds if [`build`](Self::build) has not been called
/// after the last [`push`](Self::push).
#[must_use]
pub fn active_at(&self, timestamp_ms: i64) -> Vec<&IndexEntry> {
debug_assert!(self.is_built, "call build() before querying");
if self.entries.is_empty() {
return Vec::new();
}
// Binary search for the rightmost entry with start_ms <= timestamp_ms.
let pivot = self.entries.partition_point(|e| e.start_ms <= timestamp_ms);
let mut results = Vec::new();
// Scan backwards from pivot to find all overlapping entries.
// An entry overlaps if start_ms <= timestamp_ms AND end_ms > timestamp_ms.
let scan_end = pivot;
let mut i = scan_end;
while i > 0 {
i -= 1;
let e = &self.entries[i];
if e.start_ms > timestamp_ms {
continue;
}
if e.end_ms > timestamp_ms {
results.push(e);
}
// Entries before this can only have smaller start_ms; we still
// need to check them all because overlapping cues are possible.
}
// Sort results by cue_index for deterministic output.
results.sort_by_key(|e| e.cue_index);
results
}
/// Return all entries whose time range intersects `[from_ms, to_ms)`.
#[must_use]
pub fn range_query(&self, from_ms: i64, to_ms: i64) -> Vec<&IndexEntry> {
self.entries
.iter()
.filter(|e| e.start_ms < to_ms && e.end_ms > from_ms)
.collect()
}
/// Return the entry with the earliest `start_ms`, or `None` if empty.
#[must_use]
pub fn first_entry(&self) -> Option<&IndexEntry> {
self.entries.first()
}
/// Return the entry with the latest `start_ms`, or `None` if empty.
#[must_use]
pub fn last_entry(&self) -> Option<&IndexEntry> {
self.entries.last()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_index() -> SubtitleIndex {
SubtitleIndex::from_ranges(&[
(0, 2000),
(2000, 4000),
(4000, 6000),
(5000, 7000), // overlaps with previous
(8000, 10000),
])
}
#[test]
fn test_new_is_empty() {
let idx = SubtitleIndex::new();
assert!(idx.is_empty());
assert_eq!(idx.len(), 0);
}
#[test]
fn test_from_ranges_length() {
let idx = sample_index();
assert_eq!(idx.len(), 5);
}
#[test]
fn test_active_at_first_cue() {
let idx = sample_index();
let hits = idx.active_at(1000);
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].cue_index, 0);
}
#[test]
fn test_active_at_second_cue() {
let idx = sample_index();
let hits = idx.active_at(3000);
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].cue_index, 1);
}
#[test]
fn test_active_at_overlap() {
let idx = sample_index();
// timestamp 5500 is inside cue 2 (4000-6000) AND cue 3 (5000-7000).
let hits = idx.active_at(5500);
assert_eq!(hits.len(), 2);
let indices: Vec<usize> = hits.iter().map(|e| e.cue_index).collect();
assert!(indices.contains(&2));
assert!(indices.contains(&3));
}
#[test]
fn test_active_at_gap() {
let idx = sample_index();
// timestamp 7500 is between cues.
let hits = idx.active_at(7500);
assert!(hits.is_empty());
}
#[test]
fn test_active_at_before_start() {
let idx = sample_index();
// timestamp < 0 — nothing active.
let hits = idx.active_at(-100);
assert!(hits.is_empty());
}
#[test]
fn test_active_at_end_exclusive() {
let idx = sample_index();
// End time is exclusive: at t=2000 the first cue should NOT be active.
let hits = idx.active_at(2000);
assert!(!hits.iter().any(|e| e.cue_index == 0));
}
#[test]
fn test_range_query_basic() {
let idx = sample_index();
let hits = idx.range_query(3500, 5500);
// Should include cues that start before 5500 and end after 3500.
let indices: Vec<usize> = hits.iter().map(|e| e.cue_index).collect();
assert!(indices.contains(&1)); // 2000-4000 ends at 4000 > 3500
assert!(indices.contains(&2)); // 4000-6000
assert!(indices.contains(&3)); // 5000-7000 starts at 5000 < 5500
}
#[test]
fn test_range_query_no_match() {
let idx = sample_index();
let hits = idx.range_query(10000, 12000);
assert!(hits.is_empty());
}
#[test]
fn test_first_last_entry() {
let idx = sample_index();
assert_eq!(
idx.first_entry().expect("should succeed in test").cue_index,
0
);
// last entry by start_ms is cue 4 (8000-10000)
assert_eq!(
idx.last_entry().expect("should succeed in test").cue_index,
4
);
}
#[test]
fn test_index_entry_is_active_at() {
let e = IndexEntry::new(1000, 3000, 0);
assert!(e.is_active_at(1000));
assert!(e.is_active_at(2999));
assert!(!e.is_active_at(3000));
assert!(!e.is_active_at(999));
}
#[test]
fn test_push_and_build() {
let mut idx = SubtitleIndex::new();
idx.push(IndexEntry::new(5000, 8000, 1));
idx.push(IndexEntry::new(0, 3000, 0));
idx.build();
// After build, entries should be sorted.
assert_eq!(
idx.first_entry().expect("should succeed in test").start_ms,
0
);
assert_eq!(
idx.last_entry().expect("should succeed in test").start_ms,
5000
);
}
#[test]
fn test_empty_index_active_at() {
let idx = SubtitleIndex::new();
// Build an empty index — should not panic.
let mut idx2 = idx;
idx2.build();
assert!(idx2.active_at(0).is_empty());
}
}