disclady 0.4.0

Pure Rust library and CLI utility for optical and storage disc image heuristics, inspection, and format detection (CD, DVD, GDI, Hard Disk, LaserDisc, CHD).
Documentation
//! LaserDisc media inspection (MAME createld compatible AVI captures).

use crate::types::{
    Confidence, DiscDescriptor, DiscType, Platform, TrackDescriptor, TrackType, VolumeInfo,
};
use std::io::{Read, Seek, SeekFrom};

/// Inspects a seekable stream to detect LaserDisc AVI video/audio captures.
pub fn inspect_laserdisc<R: Read + Seek>(reader: &mut R, file_size: u64) -> Option<DiscDescriptor> {
    if file_size < 128 {
        return None;
    }

    if reader.seek(SeekFrom::Start(0)).is_err() {
        return None;
    }

    let buf_size = (file_size as usize).min(65536);
    let mut header = vec![0u8; buf_size];
    if reader.read_exact(&mut header).is_err() {
        return None;
    }

    if header.len() < 12 || &header[0..4] != b"RIFF" || &header[8..12] != b"AVI " {
        return None;
    }

    let mut width = 0u32;
    let mut height = 0u32;
    let mut total_frames = 0u32;
    let mut video_codec = [0u8; 4];
    let mut has_video = false;
    let mut has_audio = false;

    let mut pos = 12;
    while pos + 8 <= header.len() {
        let chunk_id = &header[pos..pos + 4];
        let chunk_size = u32::from_le_bytes([
            header[pos + 4],
            header[pos + 5],
            header[pos + 6],
            header[pos + 7],
        ]) as usize;

        if chunk_id == b"LIST" && pos + 12 <= header.len() {
            let list_type = &header[pos + 8..pos + 12];
            if list_type == b"hdrl" {
                let list_end = pos
                    .saturating_add(8)
                    .saturating_add(chunk_size)
                    .min(header.len());
                let mut sub_pos = pos + 12;

                while sub_pos + 8 <= list_end {
                    let sub_id = &header[sub_pos..sub_pos + 4];
                    let sub_size = u32::from_le_bytes([
                        header[sub_pos + 4],
                        header[sub_pos + 5],
                        header[sub_pos + 6],
                        header[sub_pos + 7],
                    ]) as usize;

                    if sub_id == b"avih" && sub_pos + 8 + 40 <= header.len() {
                        let avih = &header[sub_pos + 8..];
                        total_frames = u32::from_le_bytes([avih[16], avih[17], avih[18], avih[19]]);
                        width = u32::from_le_bytes([avih[32], avih[33], avih[34], avih[35]]);
                        height = u32::from_le_bytes([avih[36], avih[37], avih[38], avih[39]]);
                    } else if sub_id == b"LIST" && sub_pos + 12 <= list_end {
                        let sub_list_type = &header[sub_pos + 8..sub_pos + 12];
                        if sub_list_type == b"strl" {
                            let strl_end = sub_pos
                                .saturating_add(8)
                                .saturating_add(sub_size)
                                .min(header.len());
                            let mut strl_pos = sub_pos + 12;

                            while strl_pos + 8 <= strl_end {
                                let stream_chunk_id = &header[strl_pos..strl_pos + 4];
                                let stream_chunk_size = u32::from_le_bytes([
                                    header[strl_pos + 4],
                                    header[strl_pos + 5],
                                    header[strl_pos + 6],
                                    header[strl_pos + 7],
                                ]) as usize;

                                if stream_chunk_id == b"strh" && strl_pos + 8 + 16 <= header.len() {
                                    let stream_type = &header[strl_pos + 8..strl_pos + 12];
                                    if stream_type == b"vids" {
                                        has_video = true;
                                        video_codec
                                            .copy_from_slice(&header[strl_pos + 12..strl_pos + 16]);
                                    } else if stream_type == b"auds" {
                                        has_audio = true;
                                    }
                                } else if stream_chunk_id == b"strf"
                                    && strl_pos + 8 + 40 <= header.len()
                                    && has_video
                                    && (width == 0 || height == 0)
                                {
                                    let bi_w = i32::from_le_bytes([
                                        header[strl_pos + 12],
                                        header[strl_pos + 13],
                                        header[strl_pos + 14],
                                        header[strl_pos + 15],
                                    ]);
                                    let bi_h = i32::from_le_bytes([
                                        header[strl_pos + 16],
                                        header[strl_pos + 17],
                                        header[strl_pos + 18],
                                        header[strl_pos + 19],
                                    ]);
                                    if bi_w > 0 {
                                        width = bi_w as u32;
                                    }
                                    if bi_h > 0 {
                                        height = bi_h.unsigned_abs();
                                    }
                                }

                                let padded =
                                    ((stream_chunk_size as u64).saturating_add(1) & !1) as usize;
                                match strl_pos.checked_add(8).and_then(|p| p.checked_add(padded)) {
                                    Some(next) => strl_pos = next,
                                    None => break,
                                }
                            }
                        }
                    }

                    let padded = ((sub_size as u64).saturating_add(1) & !1) as usize;
                    match sub_pos.checked_add(8).and_then(|p| p.checked_add(padded)) {
                        Some(next) => sub_pos = next,
                        None => break,
                    }
                }
            }
        }

        let padded = ((chunk_size as u64).saturating_add(1) & !1) as usize;
        match pos.checked_add(8).and_then(|p| p.checked_add(padded)) {
            Some(next) => pos = next,
            None => break,
        }
    }

    if has_video {
        let codec_str = String::from_utf8_lossy(&video_codec);
        let track = TrackDescriptor {
            track_number: 1,
            track_type: TrackType::LaserDiscFrame,
            sector_size: 0,
            start_lba: 0,
            sector_count: if total_frames > 0 {
                Some(total_frames as u64)
            } else {
                None
            },
            file_path: None,
            pregap_sectors: None,
        };

        let mut vol = VolumeInfo {
            platform: Some(Platform::LaserDiscArcade),
            ..Default::default()
        };
        if width > 0 && height > 0 {
            vol.volume_identifier = Some(format!("{width}x{height}"));
        }

        let summary = format!(
            "LaserDisc video capture in AVI container: {}x{} (codec: {}), {} frames{}",
            if width > 0 { width } else { 720 },
            if height > 0 { height } else { 480 },
            codec_str.trim(),
            total_frames,
            if has_audio { " with audio stream" } else { "" }
        );

        Some(DiscDescriptor {
            disc_type: DiscType::LaserDisc,
            confidence: Confidence::Definitive,
            primary_sector_size: 0,
            total_sectors: if total_frames > 0 {
                Some(total_frames as u64)
            } else {
                None
            },
            total_size_bytes: Some(file_size),
            tracks: vec![track],
            volume_info: Some(Box::new(vol)),
            summary,
            chd_plan: None,
        })
    } else {
        None
    }
}