libpgs 0.6.0

Fast PGS subtitle extraction, encoding, and round-trip transformation for MKV and M2TS containers
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
//! Integration test: verify the streaming Extractor API against the batch API
//! using real media files across all extraction paths.
//!
//! Fixture files are expected in `tests/fixtures/` but are not tracked in git.
//! Tests are skipped at runtime if the fixtures are not present.

use std::collections::HashMap;
use std::path::Path;

const FIXTURES: &[&str] = &[
    "tests/fixtures/matroska-with-cues.mkv",
    "tests/fixtures/matroska-no-cues.mkv",
    "tests/fixtures/mpeg-transport-stream.m2ts",
    "tests/fixtures/mpeg-transport-stream-descriptors.m2ts",
];

/// Returns only the fixture paths that exist on disk.
fn available_fixtures() -> Vec<&'static str> {
    FIXTURES
        .iter()
        .copied()
        .filter(|p| Path::new(p).exists())
        .collect()
}

/// Batch extraction baseline — used to compare against streaming results.
fn batch_baseline(path: &str) -> Vec<libpgs::TrackDisplaySets> {
    libpgs::extract_all_display_sets(Path::new(path)).expect("batch extraction should succeed")
}

#[test]
fn streaming_matches_batch_totals() {
    let fixtures = available_fixtures();
    if fixtures.is_empty() {
        return;
    }

    for fixture in fixtures {
        let batch = batch_baseline(fixture);
        let batch_total: usize = batch.iter().map(|t| t.display_sets.len()).sum();

        let extractor = libpgs::Extractor::open(fixture).expect("open");
        let mut count = 0usize;
        let mut track_counts: HashMap<u32, usize> = HashMap::new();
        let mut track_segments: HashMap<u32, usize> = HashMap::new();

        for result in extractor {
            let tds = result.expect("streaming item should be Ok");
            count += 1;
            *track_counts.entry(tds.track_id).or_default() += 1;
            *track_segments.entry(tds.track_id).or_default() += tds.display_set.segments.len();
        }

        assert_eq!(
            count, batch_total,
            "{fixture}: streaming total ({count}) != batch total ({batch_total})"
        );

        for bt in &batch {
            let stream_count = track_counts.get(&bt.track.track_id).copied().unwrap_or(0);
            assert_eq!(
                stream_count,
                bt.display_sets.len(),
                "{fixture}: track {} display set mismatch: streaming={stream_count}, batch={}",
                bt.track.track_id,
                bt.display_sets.len()
            );

            let batch_segs: usize = bt.display_sets.iter().map(|ds| ds.segments.len()).sum();
            let stream_segs = track_segments
                .get(&bt.track.track_id)
                .copied()
                .unwrap_or(0);
            assert_eq!(
                stream_segs, batch_segs,
                "{fixture}: track {} segment mismatch: streaming={stream_segs}, batch={batch_segs}",
                bt.track.track_id
            );
        }
    }
}

#[test]
fn history_accumulates_correctly() {
    let fixtures = available_fixtures();
    if fixtures.is_empty() {
        return;
    }

    for fixture in fixtures {
        let mut extractor = libpgs::Extractor::open(fixture).expect("open");

        // Consume all display sets.
        let mut total = 0usize;
        while let Some(result) = extractor.next() {
            result.expect("should be Ok");
            total += 1;

            // History length should always match items yielded so far.
            assert_eq!(extractor.history().len(), total);
        }

        // history_for_track should partition correctly.
        let track_ids: Vec<u32> = extractor.tracks().iter().map(|t| t.track_id).collect();
        let mut sum = 0usize;
        for &tid in &track_ids {
            sum += extractor.history_for_track(tid).len();
        }
        assert_eq!(
            sum, total,
            "{fixture}: sum of per-track history != total history"
        );
    }
}

#[test]
fn with_history_false_skips_catalog_but_yields_same_data() {
    let fixtures = available_fixtures();
    if fixtures.is_empty() {
        return;
    }

    for fixture in fixtures {
        // Control: default (history on).
        let control: Vec<(u32, u64)> = libpgs::Extractor::open(fixture)
            .expect("open")
            .map(|r| r.expect("ok"))
            .map(|tds| (tds.track_id, tds.display_set.pts))
            .collect();

        // Opted-out.
        let mut ext = libpgs::Extractor::open(fixture)
            .expect("open")
            .with_history(false);
        let mut seen: Vec<(u32, u64)> = Vec::new();
        while let Some(r) = ext.next() {
            let tds = r.expect("ok");
            seen.push((tds.track_id, tds.display_set.pts));
            assert!(
                ext.history().is_empty(),
                "{fixture}: history must stay empty when disabled"
            );
        }
        assert!(ext.drain_history().is_empty());
        ext.clear_history();
        let mut seen_sorted = seen.clone();
        let mut control_sorted = control.clone();
        seen_sorted.sort();
        control_sorted.sort();
        assert_eq!(
            seen_sorted, control_sorted,
            "{fixture}: yielded data must match regardless of history flag"
        );
    }
}

#[test]
fn drain_history_clears_and_returns() {
    let fixtures = available_fixtures();
    if fixtures.is_empty() {
        return;
    }

    for fixture in fixtures {
        let mut extractor = libpgs::Extractor::open(fixture).expect("open");

        // Consume 10 items.
        for _ in 0..10 {
            extractor.next().expect("should have items").expect("Ok");
        }
        assert_eq!(extractor.history().len(), 10);

        // Drain and verify.
        let drained = extractor.drain_history();
        assert_eq!(drained.len(), 10);
        assert!(
            extractor.history().is_empty(),
            "history should be empty after drain"
        );

        // Continue streaming — new items should accumulate fresh.
        let mut remaining = 0usize;
        for result in extractor.by_ref() {
            result.expect("Ok");
            remaining += 1;
        }
        assert_eq!(extractor.history().len(), remaining);
    }
}

#[test]
fn early_termination_with_take() {
    let fixtures = available_fixtures();
    if fixtures.is_empty() {
        return;
    }

    for fixture in fixtures {
        let mut extractor = libpgs::Extractor::open(fixture).expect("open");

        // Take only the first 5 display sets.
        let first_five: Vec<_> = extractor
            .by_ref()
            .take(5)
            .collect::<Result<Vec<_>, _>>()
            .expect("Ok");
        assert_eq!(first_five.len(), 5);

        // Stats should show partial read — less than a full extraction.
        let partial_bytes = extractor.stats().bytes_read;
        assert!(partial_bytes > 0, "should have read some bytes");

        // History should have exactly 5.
        assert_eq!(extractor.history().len(), 5);
    }
}

#[test]
fn track_filter_restricts_output() {
    let fixtures = available_fixtures();
    if fixtures.is_empty() {
        return;
    }

    for fixture in fixtures {
        let batch = batch_baseline(fixture);
        // Pick one track.
        let target = &batch[0];
        let tid = target.track.track_id;

        let extractor = libpgs::Extractor::open(fixture)
            .expect("open")
            .with_track_filter(&[tid]);

        let mut count = 0usize;
        let mut seg_count = 0usize;
        for result in extractor {
            let tds = result.expect("Ok");
            assert_eq!(tds.track_id, tid, "should only yield filtered track");
            count += 1;
            seg_count += tds.display_set.segments.len();
        }

        let batch_segs: usize = target.display_sets.iter().map(|ds| ds.segments.len()).sum();
        assert_eq!(
            count,
            target.display_sets.len(),
            "{fixture}: display set count mismatch for filtered track"
        );
        assert_eq!(
            seg_count, batch_segs,
            "{fixture}: segment count mismatch for filtered track"
        );
    }
}

#[test]
fn stats_update_during_streaming() {
    let fixtures = available_fixtures();
    if fixtures.is_empty() {
        return;
    }

    for fixture in fixtures {
        let mut extractor = libpgs::Extractor::open(fixture).expect("open");

        let initial_bytes = extractor.stats().bytes_read;

        // Read one item.
        extractor.next().expect("should have items").expect("Ok");
        let after_one = extractor.stats().bytes_read;
        assert!(
            after_one > initial_bytes,
            "bytes_read should increase after yielding a display set"
        );

        // Exhaust remaining.
        for result in extractor.by_ref() {
            result.expect("Ok");
        }
        let final_bytes = extractor.stats().bytes_read;
        assert!(final_bytes >= after_one, "bytes_read should not decrease");
        assert!(extractor.stats().file_size > 0, "file_size should be set");
    }
}

#[test]
fn collect_by_track_matches_batch() {
    let fixtures = available_fixtures();
    if fixtures.is_empty() {
        return;
    }

    for fixture in fixtures {
        let batch = batch_baseline(fixture);
        let collected = libpgs::Extractor::open(fixture)
            .expect("open")
            .collect_by_track()
            .expect("collect_by_track should succeed");

        assert_eq!(
            collected.len(),
            batch.len(),
            "{fixture}: track count mismatch"
        );

        let batch_map: HashMap<u32, usize> = batch
            .iter()
            .map(|t| (t.track.track_id, t.display_sets.len()))
            .collect();

        for t in &collected {
            let expected = batch_map.get(&t.track.track_id).copied().unwrap_or(0);
            assert_eq!(
                t.display_sets.len(),
                expected,
                "{fixture}: collect_by_track: track {} has {} ds, batch has {}",
                t.track.track_id,
                t.display_sets.len(),
                expected
            );
        }
    }
}

#[test]
fn all_fixtures_produce_same_totals() {
    let fixtures = available_fixtures();
    if fixtures.len() < 2 {
        return;
    }

    // Collect totals per fixture: (display sets, segments, track count, per-track ds counts sorted)
    let mut results: Vec<(&str, usize, usize, usize, Vec<usize>)> = Vec::new();

    for fixture in &fixtures {
        let by_track = batch_baseline(fixture);
        let track_count = by_track.len();
        let total_ds: usize = by_track.iter().map(|t| t.display_sets.len()).sum();
        let total_segs: usize = by_track
            .iter()
            .map(|t| {
                t.display_sets
                    .iter()
                    .map(|ds| ds.segments.len())
                    .sum::<usize>()
            })
            .sum();
        let mut per_track: Vec<usize> = by_track.iter().map(|t| t.display_sets.len()).collect();
        per_track.sort();

        results.push((fixture, total_ds, total_segs, track_count, per_track));
    }

    let (ref_fixture, ref_ds, ref_segs, ref_tracks, ref_per_track) = &results[0];

    for (fixture, total_ds, total_segs, track_count, per_track) in &results[1..] {
        assert_eq!(
            track_count, ref_tracks,
            "{fixture} has {track_count} tracks, but {ref_fixture} has {ref_tracks}"
        );
        assert_eq!(
            total_ds, ref_ds,
            "{fixture} has {total_ds} display sets, but {ref_fixture} has {ref_ds}"
        );
        assert_eq!(
            total_segs, ref_segs,
            "{fixture} has {total_segs} segments, but {ref_fixture} has {ref_segs}"
        );
        assert_eq!(
            per_track, ref_per_track,
            "{fixture} per-track display set distribution differs from {ref_fixture}"
        );
    }
}

/// Roundtrip test: extract MKV → .sup via write_sup_file, then re-read via Extractor.
#[test]
fn sup_roundtrip_from_mkv() {
    let mkv = "tests/fixtures/matroska-with-cues.mkv";
    if !Path::new(mkv).exists() {
        return;
    }

    let batch = batch_baseline(mkv);
    assert!(!batch.is_empty(), "should have at least one track");

    // Write first track to a temp .sup file.
    let sup_path = std::env::temp_dir().join("libpgs_test_roundtrip.sup");
    let source = &batch[0];
    libpgs::write_sup_file(&source.display_sets, &sup_path).expect("write_sup_file");

    // Re-read via Extractor.
    let extractor = libpgs::Extractor::open(&sup_path).expect("open .sup");
    assert_eq!(extractor.tracks().len(), 1);
    assert_eq!(extractor.tracks()[0].track_id, 0);
    assert_eq!(
        extractor.tracks()[0].container,
        libpgs::ContainerFormat::Sup
    );

    let mut ds_count = 0usize;
    let mut seg_count = 0usize;
    for result in extractor {
        let tds = result.expect("streaming item should be Ok");
        assert_eq!(tds.track_id, 0);
        ds_count += 1;
        seg_count += tds.display_set.segments.len();
    }

    assert_eq!(
        ds_count,
        source.display_sets.len(),
        "roundtrip display set count mismatch"
    );

    let source_segs: usize = source.display_sets.iter().map(|ds| ds.segments.len()).sum();
    assert_eq!(seg_count, source_segs, "roundtrip segment count mismatch");

    // Verify PTS values match.
    let extractor2 = libpgs::Extractor::open(&sup_path).expect("reopen .sup");
    for (i, (result, orig_ds)) in extractor2.zip(source.display_sets.iter()).enumerate() {
        let tds = result.expect("Ok");
        assert_eq!(
            tds.display_set.pts, orig_ds.pts,
            "roundtrip PTS mismatch at display set {i}"
        );
        assert_eq!(
            tds.display_set.segments.len(),
            orig_ds.segments.len(),
            "roundtrip segment count mismatch at display set {i}"
        );
    }

    let _ = std::fs::remove_file(&sup_path);
}