aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-game/src/demuxers/hl_fmv.rs
blob: b511bb96aa4ffd064d000fc79e183acb4cdaa6e8 (plain) (blame)
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
use nihav_core::frame::*;
use nihav_core::demuxers::*;

#[allow(dead_code)]
struct HighlanderFMVDemuxer<'a> {
    src:        &'a mut ByteReader<'a>,
    vpts:       u64,
    apts:       u64,
}

impl<'a> DemuxCore<'a> for HighlanderFMVDemuxer<'a> {
    #[allow(unused_variables)]
    fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
        let src = &mut self.src;

        let tag                         = src.read_tag()?;
        validate!(&tag == b"FMV*");
        let size                        = src.read_u32le()?;
        validate!(size == 0);

        let vhdr = NAVideoInfo::new(320, 240, false, PAL8_FORMAT);
        let vci = NACodecTypeInfo::Video(vhdr);
        let vinfo = NACodecInfo::new("hl-fmv-video", vci, None);
        if strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 2, 25, 0)).is_none() {
            return Err(DemuxerError::MemoryError);
        }
        let ahdr = NAAudioInfo::new(22050, 1, SND_U8_FORMAT, 1);
        let ainfo = NACodecInfo::new("pcm", NACodecTypeInfo::Audio(ahdr), None);
        if strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, 22050, 0)).is_none() {
            return Err(DemuxerError::MemoryError);
        }

        self.apts = 0;
        self.vpts = 0;
        Ok(())
    }

    fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
        let tag                         = self.src.read_tag()?;
        let size                        = self.src.read_u32le()? as usize;
        match &tag {
            b"AUD1" => {
                let stream = strmgr.get_stream_by_id(1).unwrap();
                let (tb_num, tb_den) = stream.get_timebase();
                let ts = NATimeInfo::new(Some(self.apts), None, None, tb_num, tb_den);
                self.apts += size as u64;
                self.src.read_packet(stream, ts, true, size)
            },
            b"VID3" => {
                let stream = strmgr.get_stream_by_id(0).unwrap();
                let (tb_num, tb_den) = stream.get_timebase();
                let ts = NATimeInfo::new(Some(self.vpts), None, None, tb_num, tb_den);
                self.vpts += 1;
                self.src.read_packet(stream, ts, true, size)
            },
            b"END*" => Err(DemuxerError::EOF),
            _ => Err(DemuxerError::InvalidData),
        }
    }

    fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
        Err(DemuxerError::NotPossible)
    }
    fn get_duration(&self) -> u64 { 0 }
}
impl<'a> NAOptionHandler for HighlanderFMVDemuxer<'a> {
    fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
    fn set_options(&mut self, _options: &[NAOption]) { }
    fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
}
impl<'a> HighlanderFMVDemuxer<'a> {
    fn new(io: &'a mut ByteReader<'a>) -> Self {
        HighlanderFMVDemuxer {
            src:        io,
            vpts:       0,
            apts:       0,
        }
    }
}

pub struct HighlanderFMVDemuxerCreator { }

impl DemuxerCreator for HighlanderFMVDemuxerCreator {
    fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
        Box::new(HighlanderFMVDemuxer::new(br))
    }
    fn get_name(&self) -> &'static str { "hl-fmv" }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::fs::File;

    // sample extracted from Highlander: The Last of the MacLeods unpublished game
    #[test]
    fn test_highlander_fmv_demux() {
        let mut file = File::open("assets/Game/0010.fmv").unwrap();
        let mut fr = FileReader::new_read(&mut file);
        let mut br = ByteReader::new(&mut fr);
        let mut dmx = HighlanderFMVDemuxer::new(&mut br);
        let mut sm = StreamManager::new();
        let mut si = SeekIndex::new();
        dmx.open(&mut sm, &mut si).unwrap();
        loop {
            let pktres = dmx.get_frame(&mut sm);
            if let Err(e) = pktres {
                if (e as i32) == (DemuxerError::EOF as i32) { break; }
                panic!("error");
            }
            let pkt = pktres.unwrap();
            println!("Got {}", pkt);
        }
    }
}