libfreemkv 0.10.0

Open source raw disc access library for optical drives
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
//! DiscStream — read sectors from an optical disc drive.
//!
//! `DiscStream::open()` does the full init sequence:
//!   drive open → wait_ready → init → probe_disc → scan
//!
//! Then reads title extents or full-disc sequentially.
//! No decryption — that's a caller concern.

use super::IOStream;
use crate::disc::{
    detect_max_batch_sectors, Disc, DiscTitle, Extent, ScanOptions,
};
use crate::drive::Drive;
use crate::error::{Error, Result};
use crate::event::{Event, EventKind};
use std::io::{self, Read, Write};
use std::path::Path;

/// Optical disc stream. Read-only — yields raw sector bytes.
///
/// Created from an initialized Drive + title extents or full-disc mode.
/// Error recovery (batch reduction, retry, zero-fill) is handled internally.
pub struct DiscStream {
    drive: Drive,
    title: DiscTitle,
    decrypt_keys: crate::decrypt::DecryptKeys,

    // What to read
    mode: ReadMode,

    // Position
    current_lba: u32,
    current_extent: usize,
    current_offset: u32,

    // Buffer
    read_buf: Vec<u8>,
    buf_valid: usize,
    buf_cursor: usize,

    // Batch size for reads
    batch_sectors: u16,
    pub errors: u64,
    eof: bool,

    // PES output
    ts_demuxer: Option<super::ts::TsDemuxer>,
    ps_demuxer: Option<super::ps::PsDemuxer>,
    parsers: Vec<(u16, Box<dyn super::codec::CodecParser>)>,
    pending_frames: std::collections::VecDeque<crate::pes::PesFrame>,
    pid_to_track: Vec<(u16, usize)>,
}

enum ReadMode {
    /// Read title extents (for MKV, M2TS, etc.)
    Extents(Vec<Extent>),
    /// Read LBA 0 to capacity (for ISO)
    Sequential { capacity: u32 },
}

/// Result of opening a DiscStream.
pub struct DiscOpenResult {
    pub stream: DiscStream,
    pub disc: Disc,
}

impl DiscStream {
    /// Open a disc drive, init, scan, and prepare to read a title.
    ///
    /// Steps (each does one thing):
    ///   1. Drive::open (or find_drive)
    ///   2. wait_ready
    ///   3. init (non-fatal)
    ///   4. probe_disc (non-fatal)
    ///   5. Disc::scan
    ///
    /// Pass an event callback for status reporting, or None.
    pub fn open(
        device: Option<&Path>,
        keydb_path: Option<&str>,
        title_index: usize,
        on_event: Option<&dyn Fn(Event)>,
    ) -> Result<DiscOpenResult> {
        let emit = |kind: EventKind| {
            if let Some(cb) = &on_event {
                cb(Event { kind });
            }
        };

        // 1. Open
        let mut drive = match device {
            Some(d) => Drive::open(d)?,
            None => crate::drive::find_drive().ok_or_else(|| Error::DeviceNotFound {
                path: String::new(),
            })?,
        };
        emit(EventKind::DriveOpened {
            device: drive.device_path().to_string(),
        });

        // 2. Wait
        let _ = drive.wait_ready();
        emit(EventKind::DriveReady);

        // 3. Init
        let init_ok = drive.init().is_ok();
        emit(EventKind::InitComplete { success: init_ok });

        // 4. Probe
        let probe_ok = drive.probe_disc().is_ok();
        emit(EventKind::ProbeComplete { success: probe_ok });

        // 5. Scan
        let scan_opts = match keydb_path {
            Some(kp) => ScanOptions::with_keydb(kp),
            None => ScanOptions::default(),
        };
        let disc = Disc::scan(&mut drive, &scan_opts)?;
        emit(EventKind::ScanComplete {
            titles: disc.titles.len(),
        });

        if title_index >= disc.titles.len() {
            return Err(Error::DiscTitleRange {
                index: title_index,
                count: disc.titles.len(),
            });
        }

        let title = disc.titles[title_index].clone();
        let keys = disc.decrypt_keys();
        let mut stream = Self::title(drive, title);
        stream.decrypt_keys = keys;

        // DVD: use program stream demuxer instead of transport stream
        if disc.content_format == crate::disc::ContentFormat::MpegPs {
            stream.ts_demuxer = None;
            stream.ps_demuxer = Some(super::ps::PsDemuxer::new());
        }

        Ok(DiscOpenResult { stream, disc })
    }

    /// Create a stream that reads a title's extents.
    /// Use this when you already have an initialized Drive.
    pub fn title(drive: Drive, title: DiscTitle) -> Self {
        let max_batch = detect_max_batch_sectors(drive.device_path());
        let extents = title.extents.clone();
        Self::new(drive, title, ReadMode::Extents(extents), max_batch)
    }

    /// Create a stream that reads the full disc sequentially (for ISO).
    pub fn full_disc(drive: Drive, title: DiscTitle, capacity: u32) -> Self {
        let max_batch = detect_max_batch_sectors(drive.device_path());
        Self::new(drive, title, ReadMode::Sequential { capacity }, max_batch)
    }

    /// Resume a full disc read from a given LBA (for ISO resume).
    /// Use after checking an existing partial file:
    ///   start_lba = (file_size / 2048) - safety_margin
    pub fn full_disc_resume(drive: Drive, title: DiscTitle, capacity: u32, start_lba: u32) -> Self {
        let max_batch = detect_max_batch_sectors(drive.device_path());
        let mut stream = Self::new(drive, title, ReadMode::Sequential { capacity }, max_batch);
        stream.current_lba = start_lba;
        stream
    }

    /// Set SCSI read timeout (default 30s).
    fn new(drive: Drive, title: DiscTitle, mode: ReadMode, max_batch: u16) -> Self {
        // Set up PES demux from title stream PIDs
        let mut pids = Vec::new();
        let mut parsers = Vec::new();
        let mut pid_to_track = Vec::new();
        for (idx, s) in title.streams.iter().enumerate() {
            let (pid, codec) = match s {
                crate::disc::Stream::Video(v) => (v.pid, v.codec),
                crate::disc::Stream::Audio(a) => (a.pid, a.codec),
                crate::disc::Stream::Subtitle(s) => (s.pid, s.codec),
            };
            pids.push(pid);
            pid_to_track.push((pid, idx));
            parsers.push((pid, super::codec::parser_for_codec(codec)));
        }

        Self {
            drive,
            title,
            decrypt_keys: crate::decrypt::DecryptKeys::None,
            mode,
            current_lba: 0,
            current_extent: 0,
            current_offset: 0,
            read_buf: Vec::with_capacity(max_batch as usize * 2048),
            buf_valid: 0,
            buf_cursor: 0,
            batch_sectors: max_batch,
            errors: 0,
            eof: false,
            ts_demuxer: if pids.is_empty() { None } else { Some(super::ts::TsDemuxer::new(&pids)) },
            ps_demuxer: None, // set by caller for DVD content
            parsers,
            pending_frames: std::collections::VecDeque::new(),
            pid_to_track,
        }
    }

    /// Skip decryption — return raw encrypted bytes.
    pub fn set_raw(&mut self) {
        self.decrypt_keys = crate::decrypt::DecryptKeys::None;
    }

    /// Lock the tray.
    pub fn lock_tray(&mut self) {
        self.drive.lock_tray();
    }

    /// Unlock the tray.
    pub fn unlock_tray(&mut self) {
        self.drive.unlock_tray();
    }

    /// Recover the drive (for batch: switch to another title).
    pub fn into_drive(self) -> Drive {
        self.drive
    }

    // ── Fill ─────────────────────────────────────────────────────────────

    fn fill(&mut self) -> bool {
        match &self.mode {
            ReadMode::Extents(_) => self.fill_extents(),
            ReadMode::Sequential { .. } => self.fill_sequential(),
        }
    }

    fn fill_extents(&mut self) -> bool {
        let (ext_start, ext_sectors) = match &self.mode {
            ReadMode::Extents(exts) => {
                if self.current_extent >= exts.len() {
                    return false;
                }
                (
                    exts[self.current_extent].start_lba,
                    exts[self.current_extent].sector_count,
                )
            }
            _ => unreachable!(),
        };

        let remaining = ext_sectors.saturating_sub(self.current_offset);
        let sectors = remaining.min(self.batch_sectors as u32) as u16;
        let sectors = sectors - (sectors % 3);
        if sectors == 0 {
            self.current_extent += 1;
            self.current_offset = 0;
            return self.fill_extents(); // next extent
        }

        let lba = ext_start + self.current_offset;
        let bytes = sectors as usize * 2048;
        self.read_buf.resize(bytes, 0);

        // Drive handles all error recovery internally.
        match self.drive.read(
            lba,
            sectors,
            &mut self.read_buf[..bytes],
        ) {
            Ok(_) => {
                self.buf_valid = bytes;
                self.buf_cursor = 0;
                self.current_offset += sectors as u32;
                if self.current_offset >= ext_sectors {
                    self.current_extent += 1;
                    self.current_offset = 0;
                }
                true
            }
            Err(_) => false, // drive gone — EOF
        }
    }

    fn fill_sequential(&mut self) -> bool {
        let capacity = match &self.mode {
            ReadMode::Sequential { capacity } => *capacity,
            _ => unreachable!(),
        };

        if self.current_lba >= capacity {
            return false;
        }

        let remaining = capacity - self.current_lba;
        let count = remaining.min(self.batch_sectors as u32) as u16;
        let bytes = count as usize * 2048;
        self.read_buf.resize(bytes, 0);

        // Drive handles all error recovery internally —
        // retries, speed changes, zero-fill on unreadable sectors.
        match self.drive.read(
            self.current_lba,
            count,
            &mut self.read_buf[..bytes],
        ) {
            Ok(_) => {
                self.buf_valid = bytes;
                self.buf_cursor = 0;
                self.current_lba += count as u32;
                true
            }
            Err(_) => false, // drive gone — EOF
        }
    }

}

// ── IOStream ─────────────────────────────────────────────────────────────────

impl IOStream for DiscStream {
    fn info(&self) -> &DiscTitle {
        &self.title
    }

    fn finish(&mut self) -> io::Result<()> {
        self.drive.unlock_tray();
        Ok(())
    }

    fn total_bytes(&self) -> Option<u64> {
        match &self.mode {
            ReadMode::Extents(extents) => {
                Some(extents.iter().map(|e| e.sector_count as u64 * 2048).sum())
            }
            ReadMode::Sequential { capacity } => Some(*capacity as u64 * 2048),
        }
    }

    fn keys(&self) -> crate::decrypt::DecryptKeys {
        self.decrypt_keys.clone()
    }
}

impl crate::pes::Stream for DiscStream {
    fn read(&mut self) -> io::Result<Option<crate::pes::PesFrame>> {
        // Return buffered frame if available
        if let Some(frame) = self.pending_frames.pop_front() {
            return Ok(Some(frame));
        }

        if self.eof {
            return Ok(None);
        }

        // Read sectors until we produce at least one frame
        loop {
            // Fill the read buffer with next batch of sectors
            let got_data = match &self.mode {
                ReadMode::Extents(_) => self.fill_extents(),
                ReadMode::Sequential { .. } => self.fill_sequential(),
            };

            if !got_data {
                self.eof = true;
                return Ok(None);
            }

            // Decrypt
            let bytes = self.buf_valid;
            if let Err(e) = crate::decrypt::decrypt_sectors(
                &mut self.read_buf[..bytes],
                &self.decrypt_keys,
                0,
            ) {
                return Err(io::Error::other(e.to_string()));
            }

            // Demux into packets, parse into frames
            if let Some(ref mut demuxer) = self.ts_demuxer {
                // BD: transport stream demux
                let packets = demuxer.feed(&self.read_buf[..bytes]);
                for pes in &packets {
                    if let Some((_, track)) = self.pid_to_track.iter().find(|(pid, _)| *pid == pes.pid) {
                        if let Some((_, parser)) = self.parsers.iter_mut().find(|(pid, _)| *pid == pes.pid) {
                            for frame in parser.parse(pes) {
                                self.pending_frames.push_back(
                                    crate::pes::PesFrame::from_codec_frame(*track, frame)
                                );
                            }
                        }
                    }
                }
            } else if let Some(ref mut demuxer) = self.ps_demuxer {
                // DVD: program stream demux
                let packets = demuxer.feed(&self.read_buf[..bytes]);
                for ps in &packets {
                    // Map PS stream_id to track index
                    let track = match ps.stream_id {
                        0xE0..=0xEF => 0, // video
                        0xC0..=0xDF => 1, // audio
                        0xBD => ps.sub_stream_id.map(|s| (s & 0x1F) as usize + 1).unwrap_or(1),
                        _ => continue,
                    };
                    if track < self.title.streams.len() {
                        let pts_ns = ps.pts.map(|p| (p as i64) * 1_000_000_000 / 90_000).unwrap_or(0);
                        self.pending_frames.push_back(crate::pes::PesFrame {
                            track,
                            pts: pts_ns,
                            keyframe: true, // PS doesn't have keyframe flag easily
                            data: ps.data.clone(),
                        });
                    }
                }
            }

            // Reset buffer for next read
            self.buf_valid = 0;
            self.buf_cursor = 0;

            if let Some(frame) = self.pending_frames.pop_front() {
                return Ok(Some(frame));
            }
            // No frames produced — read more data
        }
    }

    fn write(&mut self, _frame: &crate::pes::PesFrame) -> io::Result<()> {
        Err(io::Error::new(io::ErrorKind::Unsupported, "disc is read-only"))
    }

    fn finish(&mut self) -> io::Result<()> {
        self.drive.unlock_tray();
        Ok(())
    }

    fn info(&self) -> &DiscTitle {
        &self.title
    }

    fn codec_private(&self, track: usize) -> Option<Vec<u8>> {
        let pid = self.pid_to_track.iter()
            .find(|(_, idx)| *idx == track)
            .map(|(pid, _)| *pid)?;
        self.parsers.iter()
            .find(|(p, _)| *p == pid)
            .and_then(|(_, parser)| parser.codec_private())
    }

    fn headers_ready(&self) -> bool {
        for (idx, s) in self.title.streams.iter().enumerate() {
            if let crate::disc::Stream::Video(v) = s {
                if !v.secondary && self.codec_private(idx).is_none() {
                    return false;
                }
            }
        }
        true
    }
}

impl Read for DiscStream {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        // Drain current buffer
        if self.buf_cursor < self.buf_valid {
            let n = (self.buf_valid - self.buf_cursor).min(buf.len());
            buf[..n].copy_from_slice(&self.read_buf[self.buf_cursor..self.buf_cursor + n]);
            self.buf_cursor += n;
            return Ok(n);
        }

        if self.eof {
            return Ok(0);
        }

        // Fill next batch
        if self.fill() {
            let n = self.buf_valid.min(buf.len());
            buf[..n].copy_from_slice(&self.read_buf[..n]);
            self.buf_cursor = n;
            Ok(n)
        } else {
            self.eof = true;
            Ok(0)
        }
    }
}

impl Write for DiscStream {
    fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "disc is read-only",
        ))
    }
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}