aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-game/src/demuxers/fst.rs
blob: c70584ad7e2a106a8a14bef78be2733599130a9a (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
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
use nihav_core::frame::*;
use nihav_core::demuxers::*;

#[allow(dead_code)]
struct FutureVisionVideoDemuxer<'a> {
    src:        &'a mut ByteReader<'a>,
    cur_frame:  usize,
    apos:       u64,
    vsize:      Vec<usize>,
    asize:      Vec<usize>,
    a_id:       Option<usize>,
    v_id:       Option<usize>,
    vframe:     bool,
}

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

        let magic                       = src.read_tag()?;
        validate!(&magic == b"2TSF");
        let width                       = src.read_u32le()? as usize;
        let height                      = src.read_u32le()? as usize;
        validate!(width != 0 && height != 0);
        let _flags                      = src.read_u32le()?;
        let nframes                     = src.read_u32le()? as usize;
        let fps                         = src.read_u32le()?;
        let arate                       = src.read_u32le()?;
        let abits                       = src.read_u32le()?;

        let vhdr = NAVideoInfo::new(width, height, false, PAL8_FORMAT);
        let vci = NACodecTypeInfo::Video(vhdr);
        let vinfo = NACodecInfo::new("fst-video", vci, None);
        self.v_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, fps, nframes as u64));
        if arate != 0 {
            validate!(abits == 8 || abits == 16);
            let ahdr = NAAudioInfo::new(arate, 1, if abits == 16 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2);
            let ainfo = NACodecInfo::new("pcm", NACodecTypeInfo::Audio(ahdr), None);
            self.a_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, arate, 2));
        }
        self.vsize = Vec::with_capacity(nframes);
        self.asize = Vec::with_capacity(nframes);
        for _ in 0..nframes {
            let vsize                   = src.read_u32le()? as usize;
            let asize                   = src.read_u16le()? as usize;
            self.vsize.push(vsize);
            self.asize.push(asize);
        }
        self.vframe = true;
        self.cur_frame = 0;
        self.apos = 0;
        Ok(())
    }

    #[allow(unused_variables)]
    fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
        if self.cur_frame >= self.vsize.len() { return Err(DemuxerError::EOF); }
        let (id, size, pts) = if self.vframe {
                self.vframe = self.a_id.is_none();

                (self.v_id.unwrap_or(0), self.vsize[self.cur_frame], self.cur_frame as u64)
            } else {
                self.vframe = true;
                let apos = self.apos;
                self.apos += (self.asize[self.cur_frame] as u64) * 2;

                (self.a_id.unwrap_or(0), self.asize[self.cur_frame], apos)
            };

        if self.vframe {
            self.cur_frame += 1;
        }

        let str = strmgr.get_stream(id).unwrap();
        let (tb_num, tb_den) = str.get_timebase();
        let ts = NATimeInfo::new(Some(pts), None, None, tb_num, tb_den);
        self.src.read_packet(str, ts, true, size)
    }

    fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
        Err(DemuxerError::NotPossible)
    }
    fn get_duration(&self) -> u64 { 0 }
}
impl<'a> NAOptionHandler for FutureVisionVideoDemuxer<'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> FutureVisionVideoDemuxer<'a> {
    fn new(io: &'a mut ByteReader<'a>) -> Self {
        FutureVisionVideoDemuxer {
            src:        io,
            cur_frame:  0,
            apos:       0,
            vsize:      Vec::new(),
            asize:      Vec::new(),
            a_id:       None,
            v_id:       None,
            vframe:     false,
        }
    }
}

pub struct FSTDemuxerCreator { }

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

#[allow(dead_code)]
struct FutureVisionAudioDemuxer<'a> {
    src:        &'a mut ByteReader<'a>,
    a_id:       usize,
    end:        u64,
    arate:      u32,
}

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

        let magic                       = src.read_tag()?;
        validate!(&magic == b"FCMP");
        let size                        = u64::from(src.read_u32le()?);
        let arate                       = src.read_u32le()?;
        validate!(arate != 0);
        let abits                       = src.read_u16le()?;
        validate!(abits == 8 || abits == 16);

        let ahdr = NAAudioInfo::new(arate, 1, if abits == 16 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2);
        let ainfo = NACodecInfo::new("fst-audio", NACodecTypeInfo::Audio(ahdr), None);
        self.a_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 0, ainfo, 1, arate, 2)).unwrap();
        self.end = self.src.tell() + size;
        self.arate = arate;
        Ok(())
    }

    #[allow(unused_variables)]
    fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
        if self.src.tell() >= self.end { return Err(DemuxerError::EOF); }
        let size = (self.end - self.src.tell()).min(0x2000) as usize;
        let pts = (self.src.tell() - 14) * 2;

        let str = strmgr.get_stream(self.a_id).unwrap();
        let (tb_num, tb_den) = str.get_timebase();
        let ts = NATimeInfo::new(Some(pts), None, None, tb_num, tb_den);
        self.src.read_packet(str, ts, true, size)
    }

    fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
        Err(DemuxerError::NotPossible)
    }
    fn get_duration(&self) -> u64 { (self.end - 14) * 2000 / u64::from(self.arate) }
}
impl<'a> NAOptionHandler for FutureVisionAudioDemuxer<'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> FutureVisionAudioDemuxer<'a> {
    fn new(io: &'a mut ByteReader<'a>) -> Self {
        FutureVisionAudioDemuxer {
            src:        io,
            a_id:       0,
            end:        0,
            arate:      0,
        }
    }
}

pub struct FCMPDemuxerCreator { }

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

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

    // samples from the Harvester game
    #[test]
    fn test_fst_demux() {
        let mut file = File::open("assets/Game/c007.fst").unwrap();
        let mut fr = FileReader::new_read(&mut file);
        let mut br = ByteReader::new(&mut fr);
        let mut dmx = FutureVisionVideoDemuxer::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);
        }
    }

    #[test]
    fn test_fcmp_demux() {
        let mut file = File::open("assets/Game/anxiety.cmp").unwrap();
        let mut fr = FileReader::new_read(&mut file);
        let mut br = ByteReader::new(&mut fr);
        let mut dmx = FutureVisionAudioDemuxer::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);
        }
    }
}