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
//! High-level DVD-Video disc detection and `VIDEO_TS/` enumeration.
//!
//! Phase 1: open a file or block device, sniff for an ISO 9660 PVD
//! and a UDF AVDP, walk the volume to find `VIDEO_TS/`, and enumerate
//! the title-set files (IFO / VOB / BUP). **No IFO / VOB body parsing
//! yet** — that's Phase 2.
//!
//! ## DVD-Video file naming convention
//!
//! Per ECMA-268 §9 + mpucoder's `dvd.sourceforge.net/dvdinfo/ifo.html`:
//!
//! ```text
//! VIDEO_TS/ top-level mandatory directory
//! VIDEO_TS.IFO Video Manager information (always)
//! VIDEO_TS.VOB VMG menu video (optional)
//! VIDEO_TS.BUP backup of VIDEO_TS.IFO (always — per spec)
//! VTS_xx_0.IFO Video Title Set xx information (xx = 01..99)
//! VTS_xx_0.VOB VTS xx menu video (optional)
//! VTS_xx_1.VOB VTS xx title content
//! VTS_xx_2.VOB ...continued (up to .9 — 1 GB per VOB)
//! ...
//! VTS_xx_9.VOB
//! VTS_xx_0.BUP backup of VTS_xx_0.IFO (always)
//! AUDIO_TS/ empty on DVD-Video discs (or absent)
//! ```
//!
//! We classify each file we see into [`DvdFileKind`] and surface the
//! resulting list as [`DvdDisc`]. The discriminator for "this is a
//! DVD-Video disc" is `VIDEO_TS/VIDEO_TS.IFO`: a disc with `VIDEO_TS/`
//! but no `VIDEO_TS.IFO` is rejected as non-DVD-Video.
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
use crate::error::{Error, Result};
use crate::ifo::{
DvdTitleEntry, Pgc, PgciUt, TtSrpt, VmgIfo, VmgPtlMait, VmgVtsAtrt, VobuAdmap, VtsCAdt, VtsIfo,
};
use crate::iso9660::Iso9660Volume;
use crate::udf::{UdfFile, UdfVolume};
/// Top-level kind of a DVD-Video file. The discriminator is purely
/// lexical (filename pattern matching) per the spec naming
/// convention in `docs/container/dvd/application/mpucoder-ifo.html`;
/// we don't peek inside any of the bytes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DvdFileKind {
/// `VIDEO_TS.IFO` — Video Manager Information. Mandatory.
Vmgi,
/// `VIDEO_TS.VOB` — VMG menu video. Optional.
VmgMenu,
/// `VIDEO_TS.BUP` — backup of `VIDEO_TS.IFO`. Mandatory per spec.
VmgiBup,
/// `VTS_xx_0.IFO` — VTS information file. `xx` = 1..=99.
Vtsi(u8),
/// `VTS_xx_0.VOB` — VTS menu video. Optional.
VtsMenu(u8),
/// `VTS_xx_N.VOB` — VTS title content. `vob` = 1..=9.
VtsTitle { ts: u8, vob: u8 },
/// `VTS_xx_0.BUP` — backup of `VTS_xx_0.IFO`.
VtsiBup(u8),
}
/// A file we found in `VIDEO_TS/` (or `AUDIO_TS/`), enumerated.
#[derive(Debug, Clone)]
pub struct DvdFile {
pub kind: DvdFileKind,
pub name: String,
/// Logical Block Address (sector number) of the file's first extent.
pub lba: u32,
/// Total file length in bytes.
pub size: u64,
/// Title set this file belongs to (0 = VMG, 1..=99 = VTS).
pub title_set: u8,
/// 1-based VOB index for `VtsTitle` files (1..=9), `0` otherwise.
pub vob_index: u8,
}
/// A successfully-detected DVD-Video disc.
#[derive(Debug, Clone)]
pub struct DvdDisc {
/// Volume identifier (from ISO 9660 PVD if available, otherwise
/// from the UDF Primary Volume Descriptor).
pub volume_id: String,
/// 1..=99 — number of VTS title sets present.
pub title_set_count: u8,
/// All `VIDEO_TS/` files we found, sorted by `(title_set,
/// vob_index, kind)` for stable iteration.
pub video_ts_files: Vec<DvdFile>,
/// All `AUDIO_TS/` files (typically empty on DVD-Video).
pub audio_ts_files: Vec<DvdFile>,
}
impl DvdDisc {
/// Open and detect a DVD-Video disc at the given filesystem path
/// (an `.iso` image or a block-device path on Unix).
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
let f = File::open(path.as_ref())?;
Self::from_reader(f)
}
/// Detect a DVD-Video disc against an arbitrary `Read + Seek`
/// source. UDF is tried first (the DVD-mandated file system);
/// the ISO 9660 PVD fallback runs only if the UDF mount fails,
/// covering authoring-tool outputs that omit the AVDP.
pub fn from_reader<R: Read + Seek>(mut reader: R) -> Result<Self> {
// Strategy: try UDF first (the DVD spec mandates UDF 1.02 as
// the primary filesystem; ISO 9660 is only the bridge layer).
// Fall back to ISO 9660 if UDF mount fails — useful for
// authoring tools that omit the AVDP but still publish a
// readable VIDEO_TS through the bridge.
let udf_attempt = UdfVolume::open(&mut reader);
match udf_attempt {
Ok(mut udf) => Self::from_udf(&mut udf),
Err(_udf_err) => Self::from_iso9660(reader),
}
}
/// Build a `DvdDisc` from a UDF volume.
pub fn from_udf<R: Read + Seek>(udf: &mut UdfVolume<R>) -> Result<Self> {
let volume_id = if udf.volume_identifier.is_empty() {
udf.logical_volume_identifier.clone()
} else {
udf.volume_identifier.clone()
};
let root_entries = udf.read_directory(udf.root_directory_icb)?;
let video_ts_dir = root_entries
.iter()
.find(|e| e.is_dir && e.name.eq_ignore_ascii_case("VIDEO_TS"))
.ok_or(Error::NotDvdVideo("no VIDEO_TS/ directory at volume root"))?;
let audio_ts_dir = root_entries
.iter()
.find(|e| e.is_dir && e.name.eq_ignore_ascii_case("AUDIO_TS"));
let video_ts_entries = udf.read_directory(video_ts_dir.icb)?;
let audio_ts_entries = if let Some(at) = audio_ts_dir {
udf.read_directory(at.icb)?
} else {
Vec::new()
};
Self::classify(volume_id, &video_ts_entries, &audio_ts_entries)
}
/// Build a `DvdDisc` purely from the ISO 9660 bridge layer.
pub fn from_iso9660<R: Read + Seek>(reader: R) -> Result<Self> {
let mut iso = Iso9660Volume::open(reader)?;
let volume_id = iso.pvd.volume_id.clone();
let root_entries = iso.list_root()?;
let video_ts = root_entries
.iter()
.find(|e| e.is_dir && e.name.eq_ignore_ascii_case("VIDEO_TS"))
.ok_or(Error::NotDvdVideo("no VIDEO_TS/ directory at volume root"))?
.clone();
let audio_ts = root_entries
.iter()
.find(|e| e.is_dir && e.name.eq_ignore_ascii_case("AUDIO_TS"))
.cloned();
let video_ts_files = iso.list_dir(video_ts.lba, video_ts.size)?;
let audio_ts_files = if let Some(at) = audio_ts {
iso.list_dir(at.lba, at.size)?
} else {
Vec::new()
};
// Adapt ISO 9660 entries to the UDF-derived classification path.
let video_ts_udf: Vec<UdfFile> = video_ts_files
.into_iter()
.map(|e| UdfFile {
name: e.name,
is_dir: e.is_dir,
extents: Vec::new(),
length: e.size as u64,
icb: crate::udf::LongAd {
length: 0,
extent_type: 0,
location: crate::udf::LbAddr {
block: e.lba,
partition_ref: 0,
},
implementation_use: [0; 6],
},
})
.collect();
let audio_ts_udf: Vec<UdfFile> = audio_ts_files
.into_iter()
.map(|e| UdfFile {
name: e.name,
is_dir: e.is_dir,
extents: Vec::new(),
length: e.size as u64,
icb: crate::udf::LongAd {
length: 0,
extent_type: 0,
location: crate::udf::LbAddr {
block: e.lba,
partition_ref: 0,
},
implementation_use: [0; 6],
},
})
.collect();
Self::classify(volume_id, &video_ts_udf, &audio_ts_udf)
}
fn classify(volume_id: String, video_ts: &[UdfFile], audio_ts: &[UdfFile]) -> Result<Self> {
let mut video_ts_files: Vec<DvdFile> = video_ts
.iter()
.filter(|e| !e.is_dir)
.filter_map(|e| classify_video_ts_file(e).map(|kind| build_dvd_file(e, kind)))
.collect();
// Must include VIDEO_TS.IFO.
let has_vmgi = video_ts_files.iter().any(|f| f.kind == DvdFileKind::Vmgi);
if !has_vmgi {
return Err(Error::NotDvdVideo(
"VIDEO_TS/ present but VIDEO_TS.IFO is missing",
));
}
let audio_ts_files: Vec<DvdFile> = audio_ts
.iter()
.filter(|e| !e.is_dir)
.filter_map(|e| classify_audio_ts_file(e).map(|kind| build_dvd_file(e, kind)))
.collect();
video_ts_files.sort_by_key(|f| (f.title_set, f.vob_index, sort_kind_priority(f.kind)));
let title_set_count = video_ts_files
.iter()
.map(|f| f.title_set)
.filter(|ts| *ts > 0)
.max()
.unwrap_or(0);
Ok(Self {
volume_id,
title_set_count,
video_ts_files,
audio_ts_files,
})
}
/// Lookup the first VOB of a given VTS (1..=99), if present.
pub fn vts_title_vob(&self, ts: u8, vob: u8) -> Option<&DvdFile> {
self.video_ts_files
.iter()
.find(|f| f.kind == DvdFileKind::VtsTitle { ts, vob })
}
/// Lookup VIDEO_TS.IFO, if present.
pub fn vmgi(&self) -> Option<&DvdFile> {
self.video_ts_files
.iter()
.find(|f| f.kind == DvdFileKind::Vmgi)
}
/// Look up `VTS_xx_0.IFO` for a given title-set number (1..=99).
pub fn vtsi(&self, ts: u8) -> Option<&DvdFile> {
self.video_ts_files
.iter()
.find(|f| f.kind == DvdFileKind::Vtsi(ts))
}
/// Read `VIDEO_TS.IFO` from `reader` and parse the VMG IFO body.
///
/// This consumes the IFO's first sector(s) (currently only the
/// `VMGI_MAT` region at offset 0). Phase 3 will materialise the
/// remaining tables (`TT_SRPT`, `VMGM_PGCI_UT`, `VMG_PTL_MAIT`,
/// `VMG_VTS_ATRT`); for now we surface those via separate
/// [`Self::parse_vmg_tt_srpt`] / etc.
pub fn parse_vmg<R: Read + Seek>(&self, reader: &mut R) -> Result<VmgIfo> {
let f = self
.vmgi()
.ok_or(Error::NotDvdVideo("VIDEO_TS.IFO absent"))?;
let buf = read_sector_range(reader, f.lba, 1)?;
VmgIfo::parse(&buf)
}
/// Read `VTS_xx_0.IFO` and parse its body (VTSI_MAT + PTT_SRPT +
/// PGCI + C_ADT, all materialised through [`VtsIfo`]).
pub fn parse_vts<R: Read + Seek>(&self, reader: &mut R, ts_index: u8) -> Result<VtsIfo> {
let f = self
.vtsi(ts_index)
.ok_or(Error::NotDvdVideo("VTS_xx_0.IFO absent"))?;
// Read the whole IFO into RAM. IFO files are tiny (usually
// <512 KB even on multi-hour discs) so the simplicity of a
// single in-memory buffer outweighs streaming.
let sectors = (f.size.div_ceil(crate::ifo::DVD_SECTOR as u64)) as u32;
let buf = read_sector_range(reader, f.lba, sectors.max(1))?;
VtsIfo::parse(&buf, ts_index)
}
/// Read `VIDEO_TS.IFO`'s `TT_SRPT` table (the disc's title list)
/// and return its entries.
pub fn enumerate_titles<R: Read + Seek>(&self, reader: &mut R) -> Result<Vec<DvdTitleEntry>> {
let f = self
.vmgi()
.ok_or(Error::NotDvdVideo("VIDEO_TS.IFO absent"))?;
// Parse the MAT to find TT_SRPT's sector.
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = VmgIfo::parse(&mat_buf)?;
if mat.tt_srpt_sector == 0 {
return Err(Error::InvalidUdf("VMG_MAT: tt_srpt_sector is zero"));
}
// Read TT_SRPT — one sector covers up to ~170 titles (sector
// is 2048 bytes; entry is 12 bytes; 8-byte header). DVDs cap
// at 99 title sets and ~99 titles total per spec.
let tt_buf = read_sector_range(reader, f.lba + mat.tt_srpt_sector, 1)?;
let tt = TtSrpt::parse(&tt_buf)?;
Ok(tt.entries)
}
/// Stand-alone helper used by [`Self::parse_vmg`] callers who only
/// need the title list (and not the IFO's MAT structure).
pub fn parse_vmg_tt_srpt<R: Read + Seek>(&self, reader: &mut R) -> Result<TtSrpt> {
let f = self
.vmgi()
.ok_or(Error::NotDvdVideo("VIDEO_TS.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = VmgIfo::parse(&mat_buf)?;
let tt_buf = read_sector_range(reader, f.lba + mat.tt_srpt_sector, 1)?;
TtSrpt::parse(&tt_buf)
}
/// Read `VIDEO_TS.IFO`'s [`VmgVtsAtrt`] — the per-VTS attribute
/// table that mirrors each VTS IFO's attribute block (`0x100..`)
/// onto the VMG side, so a player can answer per-VTS attribute
/// queries without opening every `VTS_xx_0.IFO` individually.
///
/// Returns `Ok(None)` when the MAT's `vts_atrt_sector` is zero
/// (the spec allows the table to be elided when no VTSs are
/// authored — extremely rare in practice).
pub fn parse_vmg_vts_atrt<R: Read + Seek>(&self, reader: &mut R) -> Result<Option<VmgVtsAtrt>> {
let f = self
.vmgi()
.ok_or(Error::NotDvdVideo("VIDEO_TS.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = VmgIfo::parse(&mat_buf)?;
if mat.vts_atrt_sector == 0 {
return Ok(None);
}
// VTS_ATRT can span multiple sectors on discs with many VTSs
// (99 entries × ~0x308 bytes ≈ 76 KB ≈ 38 sectors). Bound the
// read at the IFO file's tail to avoid pulling past EOF.
let max_sectors = ((mat.last_sector_ifo + 1).saturating_sub(mat.vts_atrt_sector)).max(1);
let buf = read_sector_range(reader, f.lba + mat.vts_atrt_sector, max_sectors)?;
VmgVtsAtrt::parse(&buf).map(Some)
}
/// Read `VIDEO_TS.IFO`'s [`VmgPtlMait`] — the parental management
/// table that lists, per country, the 16-bit allow-mask of each
/// title set at each of the 8 parental levels.
///
/// Returns `Ok(None)` when the MAT's `ptl_mait_sector` is zero
/// (no parental management on this disc — common on unrated /
/// region-free authoring).
pub fn parse_vmg_ptl_mait<R: Read + Seek>(&self, reader: &mut R) -> Result<Option<VmgPtlMait>> {
let f = self
.vmgi()
.ok_or(Error::NotDvdVideo("VIDEO_TS.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = VmgIfo::parse(&mat_buf)?;
if mat.ptl_mait_sector == 0 {
return Ok(None);
}
// PTL_MAIT is bounded similarly to VTS_ATRT — sized by
// (num_countries × 8 + num_title_sets-derived body) but
// capped by the IFO file's tail. Read up to the next-table
// boundary so we don't pull garbage from a different table.
let next_table_sector = [
mat.vts_atrt_sector,
mat.txtdt_mg_sector,
mat.vmgm_c_adt_sector,
mat.vmgm_vobu_admap_sector,
mat.last_sector_ifo + 1,
]
.iter()
.copied()
.filter(|s| *s > mat.ptl_mait_sector)
.min()
.unwrap_or(mat.last_sector_ifo + 1);
let max_sectors = next_table_sector.saturating_sub(mat.ptl_mait_sector).max(1);
let buf = read_sector_range(reader, f.lba + mat.ptl_mait_sector, max_sectors)?;
VmgPtlMait::parse(&buf).map(Some)
}
/// Read `VIDEO_TS.IFO`'s **First-Play PGC** (`FP_PGC`) — the
/// program chain a player enters when the disc is inserted,
/// before any title or menu domain is active.
///
/// Per `docs/container/dvd/application/mpucoder-ifo.html` the
/// VMGI_MAT word at `0x0084` is the *start byte address* of
/// `FP_PGC` (same byte-address unit as the `0x0080`
/// "end byte address of VMGI_MAT" word — relative to the start of
/// `VIDEO_TS.IFO`), and the body is an ordinary PGC per
/// `mpucoder-pgc.html` (the MAT row links straight to the PGC
/// page), so [`Pgc::parse`] decodes it unchanged. On commercial
/// discs the FP_PGC typically carries no cells — just a
/// pre-command list ending in a `JumpSS` / `JumpTT`, which is the
/// disc's startup routing; feed [`Pgc::commands`]`.pre` through
/// [`crate::Vm::run_list`] to obtain the first [`crate::VmAction`].
///
/// Returns `Ok(None)` when the MAT's `fp_pgc_addr` is zero (no
/// First-Play PGC authored — the spec marks the field optional).
pub fn parse_fp_pgc<R: Read + Seek>(&self, reader: &mut R) -> Result<Option<Pgc>> {
let f = self
.vmgi()
.ok_or(Error::NotDvdVideo("VIDEO_TS.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = VmgIfo::parse(&mat_buf)?;
if mat.fp_pgc_addr == 0 {
return Ok(None);
}
// FP_PGC lives inside the IFO between the MAT and the first
// sector-aligned table (it is the only VMGI structure
// addressed in bytes rather than sectors). Bound the read at
// the first non-zero table sector so a malformed address
// can't pull bytes from an unrelated table; fall back to the
// IFO file's last sector.
let first_table_sector = [
mat.tt_srpt_sector,
mat.vmgm_pgci_ut_sector,
mat.ptl_mait_sector,
mat.vts_atrt_sector,
mat.txtdt_mg_sector,
mat.vmgm_c_adt_sector,
mat.vmgm_vobu_admap_sector,
]
.iter()
.copied()
.filter(|s| *s != 0)
.min()
.unwrap_or(mat.last_sector_ifo + 1)
.min(mat.last_sector_ifo + 1);
let start = mat.fp_pgc_addr as usize;
let end = first_table_sector as usize * crate::ifo::DVD_SECTOR;
if start >= end {
return Err(Error::InvalidUdf(
"VMGI_MAT: fp_pgc_addr points past the first VMG table",
));
}
let buf = read_sector_range(reader, f.lba, first_table_sector.max(1))?;
Pgc::parse(&buf[start..]).map(Some)
}
/// Read `VIDEO_TS.IFO`'s [`PgciUt`] — the VMGM (First-Play / VMG
/// menu) program-chain table indexed by ISO 639 language unit.
///
/// Per `docs/container/dvd/application/mpucoder-ifo_vmg.html`
/// §VMGM_PGCI_UT — two-level hierarchy: outer search-pointer list
/// keyed by language, each pointing at a Language Unit (`PGCI_LU`)
/// that lists the per-PGC search pointers + the PGC bodies
/// themselves (parsed via [`crate::ifo::Pgc::parse`]).
///
/// Returns `Ok(None)` when the MAT's `vmgm_pgci_ut_sector` is zero
/// (no VMG menu authored — possible on minimal discs).
pub fn parse_vmgm_pgci_ut<R: Read + Seek>(&self, reader: &mut R) -> Result<Option<PgciUt>> {
let f = self
.vmgi()
.ok_or(Error::NotDvdVideo("VIDEO_TS.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = VmgIfo::parse(&mat_buf)?;
if mat.vmgm_pgci_ut_sector == 0 {
return Ok(None);
}
// Bound the read at the next non-zero VMG table boundary so
// a malformed `end_address` field can't pull bytes from an
// unrelated table. Falls back to the IFO file's last sector.
let next_table_sector = [
mat.ptl_mait_sector,
mat.vts_atrt_sector,
mat.txtdt_mg_sector,
mat.vmgm_c_adt_sector,
mat.vmgm_vobu_admap_sector,
mat.last_sector_ifo + 1,
]
.iter()
.copied()
.filter(|s| *s > mat.vmgm_pgci_ut_sector)
.min()
.unwrap_or(mat.last_sector_ifo + 1);
let max_sectors = next_table_sector
.saturating_sub(mat.vmgm_pgci_ut_sector)
.max(1);
let buf = read_sector_range(reader, f.lba + mat.vmgm_pgci_ut_sector, max_sectors)?;
PgciUt::parse(&buf).map(Some)
}
/// Read `VTS_xx_0.IFO`'s [`PgciUt`] — the VTSM (per-title-set
/// menu) program-chain table indexed by ISO 639 language unit.
///
/// Per `docs/container/dvd/application/mpucoder-ifo_vts.html`
/// §VTSM_PGCI_UT — same shape as the VMG side except the per-LU
/// search-pointer carries a richer menu-existence flag byte
/// (root / sub-picture / audio / angle / PTT — see
/// [`crate::ifo::menu_existence`]).
///
/// Returns `Ok(None)` when the VTSI_MAT's `vtsm_pgci_ut_sector`
/// is zero (no per-title-set menus authored on this title set).
pub fn parse_vtsm_pgci_ut<R: Read + Seek>(
&self,
reader: &mut R,
ts_index: u8,
) -> Result<Option<PgciUt>> {
let f = self
.vtsi(ts_index)
.ok_or(Error::NotDvdVideo("VTS_xx_0.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = crate::ifo::VtsiMat::parse(&mat_buf)?;
if mat.vtsm_pgci_ut_sector == 0 {
return Ok(None);
}
// Same bounded-read pattern: clip at the next non-zero VTS
// table boundary so a malformed length field is harmless.
let next_table_sector = [
mat.vts_pgci_sector,
mat.vts_tmapti_sector,
mat.vtsm_c_adt_sector,
mat.vtsm_vobu_admap_sector,
mat.vts_c_adt_sector,
mat.vts_vobu_admap_sector,
mat.last_sector_ifo + 1,
]
.iter()
.copied()
.filter(|s| *s > mat.vtsm_pgci_ut_sector)
.min()
.unwrap_or(mat.last_sector_ifo + 1);
let max_sectors = next_table_sector
.saturating_sub(mat.vtsm_pgci_ut_sector)
.max(1);
let buf = read_sector_range(reader, f.lba + mat.vtsm_pgci_ut_sector, max_sectors)?;
PgciUt::parse(&buf).map(Some)
}
/// Read `VIDEO_TS.IFO`'s [`VtsCAdt`] for the VMG menu — the
/// `VMGM_C_ADT` cell-address table that maps each `(VOBidn,
/// CELLidn)` pair to its `[start_sector, end_sector]` range inside
/// the VMG menu VOB (`VIDEO_TS.VOB`).
///
/// Per `docs/container/dvd/application/mpucoder-ifo.html` §c_adt —
/// the `#c_adt` anchor documents `VMGM_C_ADT`, `VTSM_C_ADT`, and
/// `VTS_C_ADT` under one heading because all three share the wire
/// format (16-bit number-of-VOB-IDs + 32-bit end-address header
/// followed by 12-byte entries), so [`VtsCAdt::parse`] decodes the
/// VMG menu copy unchanged.
///
/// Returns `Ok(None)` when the MAT's `vmgm_c_adt_sector` is zero
/// (no VMG menu VOB authored on this disc).
pub fn parse_vmgm_c_adt<R: Read + Seek>(&self, reader: &mut R) -> Result<Option<VtsCAdt>> {
let f = self
.vmgi()
.ok_or(Error::NotDvdVideo("VIDEO_TS.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = VmgIfo::parse(&mat_buf)?;
if mat.vmgm_c_adt_sector == 0 {
return Ok(None);
}
// Bound the read at the next non-zero VMG table boundary so a
// malformed `end_address` can't pull bytes from an unrelated
// table.
let next_table_sector = [mat.vmgm_vobu_admap_sector, mat.last_sector_ifo + 1]
.iter()
.copied()
.filter(|s| *s > mat.vmgm_c_adt_sector)
.min()
.unwrap_or(mat.last_sector_ifo + 1);
let max_sectors = next_table_sector
.saturating_sub(mat.vmgm_c_adt_sector)
.max(1);
let buf = read_sector_range(reader, f.lba + mat.vmgm_c_adt_sector, max_sectors)?;
VtsCAdt::parse(&buf).map(Some)
}
/// Read `VIDEO_TS.IFO`'s [`VobuAdmap`] for the VMG menu — the
/// `VMGM_VOBU_ADMAP` absolute-sector list covering every VOBU in
/// the VMG menu VOB.
///
/// Per `docs/container/dvd/application/mpucoder-ifo.html` §vam —
/// the `#vam` anchor documents `VMGM_VOBU_ADMAP`,
/// `VTSM_VOBU_ADMAP`, and `VTS_VOBU_ADMAP` under one heading
/// because all three share the wire format (32-bit end-address
/// header followed by 4-byte VOB-relative sector words), so
/// [`VobuAdmap::parse`] decodes the VMG menu copy unchanged.
///
/// Returns `Ok(None)` when the MAT's `vmgm_vobu_admap_sector` is
/// zero (no VMG menu VOB authored on this disc).
pub fn parse_vmgm_vobu_admap<R: Read + Seek>(
&self,
reader: &mut R,
) -> Result<Option<VobuAdmap>> {
let f = self
.vmgi()
.ok_or(Error::NotDvdVideo("VIDEO_TS.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = VmgIfo::parse(&mat_buf)?;
if mat.vmgm_vobu_admap_sector == 0 {
return Ok(None);
}
// VMGM_VOBU_ADMAP is the last VMG table; bound at the IFO
// file's tail.
let max_sectors = (mat.last_sector_ifo + 1)
.saturating_sub(mat.vmgm_vobu_admap_sector)
.max(1);
let buf = read_sector_range(reader, f.lba + mat.vmgm_vobu_admap_sector, max_sectors)?;
VobuAdmap::parse(&buf).map(Some)
}
/// Read `VTS_xx_0.IFO`'s [`VtsCAdt`] for the per-title-set menu —
/// the `VTSM_C_ADT` cell-address table that maps each `(VOBidn,
/// CELLidn)` pair to its `[start_sector, end_sector]` range inside
/// the VTS menu VOB (`VTS_xx_0.VOB`).
///
/// Per `docs/container/dvd/application/mpucoder-ifo.html` §c_adt
/// (same shared heading as the VMG menu copy; see
/// [`Self::parse_vmgm_c_adt`]).
///
/// Returns `Ok(None)` when the VTSI_MAT's `vtsm_c_adt_sector` is
/// zero (no per-title-set menu VOB authored on this title set).
pub fn parse_vtsm_c_adt<R: Read + Seek>(
&self,
reader: &mut R,
ts_index: u8,
) -> Result<Option<VtsCAdt>> {
let f = self
.vtsi(ts_index)
.ok_or(Error::NotDvdVideo("VTS_xx_0.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = crate::ifo::VtsiMat::parse(&mat_buf)?;
if mat.vtsm_c_adt_sector == 0 {
return Ok(None);
}
// Bound at the next non-zero VTS table boundary after the
// menu C_ADT so a malformed length field is harmless.
let next_table_sector = [
mat.vtsm_vobu_admap_sector,
mat.vts_c_adt_sector,
mat.vts_vobu_admap_sector,
mat.last_sector_ifo + 1,
]
.iter()
.copied()
.filter(|s| *s > mat.vtsm_c_adt_sector)
.min()
.unwrap_or(mat.last_sector_ifo + 1);
let max_sectors = next_table_sector
.saturating_sub(mat.vtsm_c_adt_sector)
.max(1);
let buf = read_sector_range(reader, f.lba + mat.vtsm_c_adt_sector, max_sectors)?;
VtsCAdt::parse(&buf).map(Some)
}
/// Read `VTS_xx_0.IFO`'s [`VobuAdmap`] for the per-title-set menu —
/// the `VTSM_VOBU_ADMAP` absolute-sector list covering every VOBU
/// in the VTS menu VOB.
///
/// Per `docs/container/dvd/application/mpucoder-ifo.html` §vam
/// (same shared heading as the VMG menu copy; see
/// [`Self::parse_vmgm_vobu_admap`]).
///
/// Returns `Ok(None)` when the VTSI_MAT's `vtsm_vobu_admap_sector`
/// is zero (no per-title-set menu VOB authored on this title set).
pub fn parse_vtsm_vobu_admap<R: Read + Seek>(
&self,
reader: &mut R,
ts_index: u8,
) -> Result<Option<VobuAdmap>> {
let f = self
.vtsi(ts_index)
.ok_or(Error::NotDvdVideo("VTS_xx_0.IFO absent"))?;
let mat_buf = read_sector_range(reader, f.lba, 1)?;
let mat = crate::ifo::VtsiMat::parse(&mat_buf)?;
if mat.vtsm_vobu_admap_sector == 0 {
return Ok(None);
}
// Bound at the next non-zero VTS table boundary after the
// menu VOBU_ADMAP so a malformed length field is harmless.
let next_table_sector = [
mat.vts_c_adt_sector,
mat.vts_vobu_admap_sector,
mat.last_sector_ifo + 1,
]
.iter()
.copied()
.filter(|s| *s > mat.vtsm_vobu_admap_sector)
.min()
.unwrap_or(mat.last_sector_ifo + 1);
let max_sectors = next_table_sector
.saturating_sub(mat.vtsm_vobu_admap_sector)
.max(1);
let buf = read_sector_range(reader, f.lba + mat.vtsm_vobu_admap_sector, max_sectors)?;
VobuAdmap::parse(&buf).map(Some)
}
}
/// Read `count` consecutive 2048-byte logical sectors starting at
/// disc-LBA `start` from `reader`.
pub(crate) fn read_sector_range<R: Read + Seek>(
reader: &mut R,
start: u32,
count: u32,
) -> Result<Vec<u8>> {
let sector = crate::ifo::DVD_SECTOR as u64;
reader.seek(SeekFrom::Start(u64::from(start) * sector))?;
let mut buf = vec![0u8; (count as usize) * sector as usize];
reader.read_exact(&mut buf)?;
Ok(buf)
}
fn build_dvd_file(e: &UdfFile, kind: DvdFileKind) -> DvdFile {
let lba = e
.extents
.first()
.map(|x| x.block_location)
.unwrap_or(e.icb.location.block);
let (title_set, vob_index) = match kind {
DvdFileKind::Vmgi | DvdFileKind::VmgMenu | DvdFileKind::VmgiBup => (0, 0),
DvdFileKind::Vtsi(ts) | DvdFileKind::VtsMenu(ts) | DvdFileKind::VtsiBup(ts) => (ts, 0),
DvdFileKind::VtsTitle { ts, vob } => (ts, vob),
};
DvdFile {
kind,
name: e.name.clone(),
lba,
size: e.length,
title_set,
vob_index,
}
}
fn sort_kind_priority(k: DvdFileKind) -> u8 {
match k {
DvdFileKind::Vmgi => 0,
DvdFileKind::Vtsi(_) => 0,
DvdFileKind::VmgMenu => 1,
DvdFileKind::VtsMenu(_) => 1,
DvdFileKind::VtsTitle { .. } => 2,
DvdFileKind::VmgiBup => 3,
DvdFileKind::VtsiBup(_) => 3,
}
}
/// Classify a VIDEO_TS file name (case-insensitive).
pub fn classify_video_ts_file(e: &UdfFile) -> Option<DvdFileKind> {
let upper = e.name.to_uppercase();
match upper.as_str() {
"VIDEO_TS.IFO" => Some(DvdFileKind::Vmgi),
"VIDEO_TS.VOB" => Some(DvdFileKind::VmgMenu),
"VIDEO_TS.BUP" => Some(DvdFileKind::VmgiBup),
_ => parse_vts_filename(&upper),
}
}
/// Classify an AUDIO_TS file name — DVD-Video discs typically leave
/// `AUDIO_TS/` empty. DVD-Audio uses it; we don't claim DVD-Audio
/// support here so we surface unknowns as `None` and let the caller
/// drop them.
pub fn classify_audio_ts_file(_e: &UdfFile) -> Option<DvdFileKind> {
None
}
/// Parse `VTS_xx_y.{IFO,VOB,BUP}`. Returns `None` for any other
/// filename (no thrown error — the surrounding classifier silently
/// drops unknowns, since DVD authoring tools sometimes leave stray
/// files like `JACKET_P/`, but those don't break the disc).
fn parse_vts_filename(name: &str) -> Option<DvdFileKind> {
let rest = name.strip_prefix("VTS_")?;
// 2-digit title-set + underscore + 1-digit vob + dot + 3-char ext
// = 8 chars total.
if rest.len() != 8 {
return None;
}
let ts_str = rest.get(0..2)?;
if rest.as_bytes().get(2)? != &b'_' {
return None;
}
let vob_str = rest.get(3..4)?;
if rest.as_bytes().get(4)? != &b'.' {
return None;
}
let ext = rest.get(5..)?;
let ts = ts_str.parse::<u8>().ok()?;
let vob = vob_str.parse::<u8>().ok()?;
if !(1..=99).contains(&ts) {
return None;
}
match (vob, ext) {
(0, "IFO") => Some(DvdFileKind::Vtsi(ts)),
(0, "VOB") => Some(DvdFileKind::VtsMenu(ts)),
(0, "BUP") => Some(DvdFileKind::VtsiBup(ts)),
(1..=9, "VOB") => Some(DvdFileKind::VtsTitle { ts, vob }),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn fake_udf_file(name: &str) -> UdfFile {
UdfFile {
name: name.to_string(),
is_dir: false,
extents: Vec::new(),
length: 0,
icb: crate::udf::LongAd {
length: 0,
extent_type: 0,
location: crate::udf::LbAddr {
block: 0,
partition_ref: 0,
},
implementation_use: [0; 6],
},
}
}
#[test]
fn classify_vmgi_files() {
assert_eq!(
classify_video_ts_file(&fake_udf_file("VIDEO_TS.IFO")),
Some(DvdFileKind::Vmgi)
);
assert_eq!(
classify_video_ts_file(&fake_udf_file("video_ts.bup")),
Some(DvdFileKind::VmgiBup)
);
assert_eq!(
classify_video_ts_file(&fake_udf_file("VIDEO_TS.VOB")),
Some(DvdFileKind::VmgMenu)
);
}
#[test]
fn classify_vts_files() {
assert_eq!(
classify_video_ts_file(&fake_udf_file("VTS_01_0.IFO")),
Some(DvdFileKind::Vtsi(1))
);
assert_eq!(
classify_video_ts_file(&fake_udf_file("VTS_07_0.VOB")),
Some(DvdFileKind::VtsMenu(7))
);
assert_eq!(
classify_video_ts_file(&fake_udf_file("VTS_99_9.VOB")),
Some(DvdFileKind::VtsTitle { ts: 99, vob: 9 })
);
assert_eq!(
classify_video_ts_file(&fake_udf_file("VTS_03_0.BUP")),
Some(DvdFileKind::VtsiBup(3))
);
}
#[test]
fn classify_rejects_garbage() {
assert_eq!(classify_video_ts_file(&fake_udf_file("VTS_00_0.IFO")), None);
assert_eq!(classify_video_ts_file(&fake_udf_file("VTS_AB_0.IFO")), None);
assert_eq!(classify_video_ts_file(&fake_udf_file("VTS_01_0.XYZ")), None);
assert_eq!(classify_video_ts_file(&fake_udf_file("MENU.IFO")), None);
}
#[test]
fn classify_extracts_title_set_count() {
let video_ts = vec![
fake_udf_file("VIDEO_TS.IFO"),
fake_udf_file("VTS_01_0.IFO"),
fake_udf_file("VTS_01_1.VOB"),
fake_udf_file("VTS_02_0.IFO"),
fake_udf_file("VTS_02_1.VOB"),
];
let disc = DvdDisc::classify("DISC".to_string(), &video_ts, &[]).unwrap();
assert_eq!(disc.title_set_count, 2);
assert_eq!(disc.video_ts_files.len(), 5);
assert!(disc.vmgi().is_some());
assert!(disc.vts_title_vob(1, 1).is_some());
assert!(disc.vts_title_vob(2, 1).is_some());
}
#[test]
fn classify_rejects_when_vmgi_missing() {
let video_ts = vec![fake_udf_file("VTS_01_0.IFO"), fake_udf_file("VTS_01_1.VOB")];
let err = DvdDisc::classify("DISC".to_string(), &video_ts, &[]).unwrap_err();
match err {
Error::NotDvdVideo(_) => {}
other => panic!("expected NotDvdVideo, got {other:?}"),
}
}
#[test]
fn classify_handles_empty_audio_ts() {
let video_ts = vec![
fake_udf_file("VIDEO_TS.IFO"),
fake_udf_file("VIDEO_TS.BUP"),
fake_udf_file("VTS_01_0.IFO"),
fake_udf_file("VTS_01_1.VOB"),
fake_udf_file("VTS_01_0.BUP"),
];
let disc = DvdDisc::classify("EMPTY_AUDIO_TS".to_string(), &video_ts, &[]).unwrap();
assert!(disc.audio_ts_files.is_empty());
assert_eq!(disc.title_set_count, 1);
}
// ---- menu C_ADT / VOBU_ADMAP reader helpers --------------------
//
// The body decoders are exercised in `ifo.rs`; these tests pin the
// sector-pointer plumbing of the four `DvdDisc::parse_*` helpers
// (zero-pointer → None, non-zero → followed + parsed).
use crate::ifo::{DVD_SECTOR, VMG_MAGIC, VTS_MAGIC};
use std::io::Cursor;
/// A C_ADT body per `mpucoder-ifo.html` §c_adt: 2-byte
/// number-of-VOB-IDs + 2-byte reserved + 4-byte end_address, then
/// 12-byte entries (VOBidn:2 / CELLidn:1 / reserved:1 /
/// start_sector:4 / end_sector:4). One entry here.
fn synth_c_adt() -> Vec<u8> {
let mut b = vec![0u8; DVD_SECTOR];
b[0..2].copy_from_slice(&1u16.to_be_bytes()); // number of VOB IDs
b[4..8].copy_from_slice(&19u32.to_be_bytes()); // end_address = 8 + 12 - 1
// entry 0
b[8..10].copy_from_slice(&7u16.to_be_bytes()); // VOBidn
b[10] = 3; // CELLidn
b[12..16].copy_from_slice(&100u32.to_be_bytes()); // start sector
b[16..20].copy_from_slice(&199u32.to_be_bytes()); // end sector
b
}
/// A VOBU_ADMAP body per `mpucoder-ifo.html` §vam: 4-byte
/// end_address then 4-byte VOB-relative sector words. Two entries.
fn synth_vobu_admap() -> Vec<u8> {
let mut b = vec![0u8; DVD_SECTOR];
b[0..4].copy_from_slice(&11u32.to_be_bytes()); // end_address = 4 + 2*4 - 1
b[4..8].copy_from_slice(&0u32.to_be_bytes()); // VOBU 1 @ sector 0
b[8..12].copy_from_slice(&50u32.to_be_bytes()); // VOBU 2 @ sector 50
b
}
/// Build a single-VMGI / single-VTSI disc image laid out as:
/// VMGI MAT @ lba 0 (FP_PGC at byte 0x0400, inside sector 0),
/// VMGM_C_ADT @ 1, VMGM_VOBU_ADMAP @ 2, VTSI MAT @ 4,
/// VTSM_C_ADT @ 5, VTSM_VOBU_ADMAP @ 6. Zero pointers are
/// written when `populate` is false to drive the `None` paths.
fn synth_disc(populate: bool) -> (DvdDisc, Cursor<Vec<u8>>) {
let mut image = vec![0u8; DVD_SECTOR * 8];
// VMGI MAT @ sector 0.
let vmgi = &mut image[0..DVD_SECTOR];
vmgi[0..12].copy_from_slice(VMG_MAGIC);
vmgi[0x1C..0x20].copy_from_slice(&3u32.to_be_bytes()); // last_sector_ifo
if populate {
vmgi[0x84..0x88].copy_from_slice(&0x0400u32.to_be_bytes()); // FP_PGC byte addr
vmgi[0xD8..0xDC].copy_from_slice(&1u32.to_be_bytes()); // VMGM_C_ADT
vmgi[0xDC..0xE0].copy_from_slice(&2u32.to_be_bytes()); // VMGM_VOBU_ADMAP
// FP_PGC body @ byte 0x0400 (still sector 0, after the
// 0x0200-byte MAT region): a cell-less PGC whose only
// content is a one-entry pre-command list — `JumpTT 1`,
// the canonical "skip straight to the main feature"
// startup routing.
vmgi[0x0400 + 0xE4..0x0400 + 0xE6].copy_from_slice(&0x00ECu16.to_be_bytes());
let ct = 0x0400 + 0xEC; // command table, right after the header
vmgi[ct..ct + 2].copy_from_slice(&1u16.to_be_bytes()); // pre count
vmgi[ct + 6..ct + 8].copy_from_slice(&15u16.to_be_bytes()); // end address
vmgi[ct + 8..ct + 16].copy_from_slice(&[0x30, 0x02, 0, 0, 0, 0x01, 0, 0]);
}
image[DVD_SECTOR..2 * DVD_SECTOR].copy_from_slice(&synth_c_adt());
image[2 * DVD_SECTOR..3 * DVD_SECTOR].copy_from_slice(&synth_vobu_admap());
// VTSI MAT @ sector 4.
let vtsi = &mut image[4 * DVD_SECTOR..5 * DVD_SECTOR];
vtsi[0..12].copy_from_slice(VTS_MAGIC);
vtsi[0x1C..0x20].copy_from_slice(&3u32.to_be_bytes()); // last_sector_ifo
if populate {
vtsi[0xD8..0xDC].copy_from_slice(&1u32.to_be_bytes()); // VTSM_C_ADT
vtsi[0xDC..0xE0].copy_from_slice(&2u32.to_be_bytes()); // VTSM_VOBU_ADMAP
}
image[5 * DVD_SECTOR..6 * DVD_SECTOR].copy_from_slice(&synth_c_adt());
image[6 * DVD_SECTOR..7 * DVD_SECTOR].copy_from_slice(&synth_vobu_admap());
let disc = DvdDisc {
volume_id: "SYNTH".to_string(),
title_set_count: 1,
video_ts_files: vec![
DvdFile {
kind: DvdFileKind::Vmgi,
name: "VIDEO_TS.IFO".to_string(),
lba: 0,
size: DVD_SECTOR as u64,
title_set: 0,
vob_index: 0,
},
DvdFile {
kind: DvdFileKind::Vtsi(1),
name: "VTS_01_0.IFO".to_string(),
lba: 4,
size: DVD_SECTOR as u64,
title_set: 1,
vob_index: 0,
},
],
audio_ts_files: Vec::new(),
};
(disc, Cursor::new(image))
}
#[test]
fn parse_vmgm_c_adt_populated() {
let (disc, mut r) = synth_disc(true);
let c_adt = disc.parse_vmgm_c_adt(&mut r).unwrap().unwrap();
assert_eq!(c_adt.number_of_vob_ids, 1);
assert_eq!(c_adt.entries.len(), 1);
assert_eq!(c_adt.lookup(7, 3), Some((100, 199)));
}
#[test]
fn parse_vmgm_vobu_admap_populated() {
let (disc, mut r) = synth_disc(true);
let admap = disc.parse_vmgm_vobu_admap(&mut r).unwrap().unwrap();
assert_eq!(admap.vobu_count(), 2);
assert_eq!(admap.vobu_start_sector(2), Some(50));
}
#[test]
fn parse_vtsm_c_adt_populated() {
let (disc, mut r) = synth_disc(true);
let c_adt = disc.parse_vtsm_c_adt(&mut r, 1).unwrap().unwrap();
assert_eq!(c_adt.entries.len(), 1);
assert_eq!(c_adt.lookup(7, 3), Some((100, 199)));
}
#[test]
fn parse_vtsm_vobu_admap_populated() {
let (disc, mut r) = synth_disc(true);
let admap = disc.parse_vtsm_vobu_admap(&mut r, 1).unwrap().unwrap();
assert_eq!(admap.vobu_count(), 2);
assert_eq!(admap.vobu_start_sector(1), Some(0));
}
#[test]
fn menu_tables_none_when_pointer_zero() {
let (disc, mut r) = synth_disc(false);
assert!(disc.parse_vmgm_c_adt(&mut r).unwrap().is_none());
assert!(disc.parse_vmgm_vobu_admap(&mut r).unwrap().is_none());
assert!(disc.parse_vtsm_c_adt(&mut r, 1).unwrap().is_none());
assert!(disc.parse_vtsm_vobu_admap(&mut r, 1).unwrap().is_none());
}
// ---- First-Play PGC reader helper ------------------------------
#[test]
fn parse_fp_pgc_populated_and_routes_through_vm() {
let (disc, mut r) = synth_disc(true);
let fp = disc.parse_fp_pgc(&mut r).unwrap().unwrap();
// The synthetic FP_PGC is cell-less — startup routing only.
assert_eq!(fp.number_of_programs, 0);
assert_eq!(fp.number_of_cells, 0);
let commands = fp.commands.as_ref().unwrap();
assert_eq!(commands.pre.len(), 1);
assert!(commands.post.is_empty());
assert!(commands.cell.is_empty());
// Drive the disc-insertion path end-to-end: FP_PGC
// pre-commands through the Phase 3c VM must surface the
// `JumpTT 1` startup routing as a typed action.
let mut vm = crate::vm::Vm::new();
let (action, _pc) = vm.run_list(&commands.pre);
assert_eq!(action, crate::vm::VmAction::JumpTitle { ttn: 1 });
}
#[test]
fn parse_fp_pgc_none_when_pointer_zero() {
let (disc, mut r) = synth_disc(false);
assert!(disc.parse_fp_pgc(&mut r).unwrap().is_none());
}
#[test]
fn parse_fp_pgc_rejects_addr_past_first_table() {
// Point fp_pgc_addr at the VMGM_C_ADT sector (byte 0x0800 =
// sector 1) — the bounded read must refuse to parse a PGC out
// of an unrelated table.
let (disc, r) = synth_disc(true);
let mut image = r.into_inner();
image[0x84..0x88].copy_from_slice(&0x0800u32.to_be_bytes());
let mut r = Cursor::new(image);
assert!(disc.parse_fp_pgc(&mut r).is_err());
}
}