aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-rad/src/demuxers/bink.rs
blob: a5d8169240f9159f4bf8f0a244ca711eb50fe989 (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
use nihav_core::demuxers::*;
use nihav_core::io::byteio::ByteReader;

struct AudioTrack {
    id:             usize,
}

const BINK_AUD_FLAG_DCT:    u8 = 0x10;
const BINK_AUD_FLAG_STEREO: u8 = 0x20;

impl AudioTrack {
    fn new(strmgr: &mut StreamManager, srate: u32, flags: u8, str_id: usize, magic: [u8; 4]) -> DemuxerResult<Self> {
        let channels = if (flags & BINK_AUD_FLAG_STEREO) != 0 { 2 } else { 1 };
        let codecname = if (flags & BINK_AUD_FLAG_DCT) != 0 {
                "bink-audio-dct"
            } else {
                "bink-audio-rdft"
            };
        let ahdr = NAAudioInfo::new(srate, channels, SND_F32P_FORMAT, 1);
        let mut edata: Vec<u8> = Vec::with_capacity(4);
        edata.extend_from_slice(&magic);
        let ainfo = NACodecInfo::new(codecname, NACodecTypeInfo::Audio(ahdr), Some(edata));
        let res = strmgr.add_stream(NAStream::new(StreamType::Audio, (str_id + 1) as u32, ainfo, 1, srate));
        validate!(res.is_some());
        let id = res.unwrap();
        Ok(Self{ id })
    }
}

struct BinkDemuxer<'a> {
    src:            &'a mut ByteReader<'a>,
    cur_frame:      usize,
    frames:         usize,
    video_id:       usize,
    tb_num:         u32,
    tb_den:         u32,
    ainfo:          Vec<AudioTrack>,
    queued_packets: Vec<NAPacket>,
    frame_pos:      Vec<u32>,
}

macro_rules! mktag {
    ($a:expr, $b:expr, $c:expr, $d:expr) => ({
        (($a as u32) << 24) | (($b as u32) << 16) | (($c as u32) << 8) | ($d as u32)
    });
    ($arr:expr) => ({
        (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
    });
}

impl<'a> DemuxCore<'a> for BinkDemuxer<'a> {
    fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
        let src = &mut self.src;
        let mut magic: [u8; 4] = [0; 4];
                                              src.read_buf(&mut magic)?;
        let magic_tag = mktag!(magic);
        validate!((magic_tag >= mktag!(b"BIKb") && magic_tag <= mktag!(b"BIKk")) ||
                  (magic_tag >= mktag!(b"KB2a") && magic_tag <= mktag!(b"KB2k")));
        let fsize                           = (src.read_u32le()? as usize) + 8;
        self.frames                         = src.read_u32le()? as usize;
        let max_size                        = src.read_u32le()? as usize;
        let _frames2                        = src.read_u32le()? as usize;
        let width                           = src.read_u32le()? as usize;
        let height                          = src.read_u32le()? as usize;
        let tb_den                          = src.read_u32le()?;
        let tb_num                          = src.read_u32le()?;
        validate!((width > 0) && (height > 0) && (width <= 7680) && (height <= 4800));
        validate!((self.frames > 0) && (tb_num > 0) && (tb_den > 0) && (max_size < fsize));
        self.tb_num = tb_num;
        self.tb_den = tb_den;
        let mut flags: [u8; 4] = [0; 4];
                                              src.read_buf(&mut flags)?;
        let mut edata: Vec<u8> = vec![0; 8];
        let p0 = &mut edata[0..4];
        p0.copy_from_slice(&magic);
        let p1 = &mut edata[4..][..4];
        p1.copy_from_slice(&flags);
        let vhdr = NAVideoInfo::new(width, height, false, YUV420_FORMAT);
        let codec = if magic[0] == b'K' && magic[1] == b'B' && magic[2] == b'2' { "bink2-video" } else { "bink-video" };
        let vinfo = NACodecInfo::new(codec, NACodecTypeInfo::Video(vhdr), Some(edata));
        let res = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, self.tb_num, self.tb_den));
        validate!(res.is_some());
        self.video_id = res.unwrap();

        let num_audio                       = src.read_u32le()? as usize;
        if magic_tag >= mktag!(b"KB2i") {
                                              src.read_skip(4)?;
        }
        validate!(num_audio < 256);
                                              src.read_skip(num_audio * 4)?; // audio max output frame size?
        self.ainfo = Vec::with_capacity(num_audio);
        for i in 0..num_audio {
            let srate                       = src.read_u24le()?;
            let flags                       = src.read_byte()?;
            validate!(srate > 0);
            self.ainfo.push(AudioTrack::new(strmgr, srate, flags, i, magic)?);
        }
        for _ in 0..num_audio {
            let _trk_id                     = src.read_u32le()?;
        }

        seek_idx.mode = SeekIndexMode::Present;
        seek_idx.add_stream(0);
        self.frame_pos = Vec::with_capacity(self.frames + 1);
        for fno in 0..=self.frames {
            let pos                         = src.read_u32le()?;
            self.frame_pos.push(pos);
            if (pos & 1) != 0 {
                let time = (fno as u64) * 1000 * (tb_num as u64) / (tb_den as u64);
                seek_idx.seek_info[0].add_entry(SeekEntry { time, pts: fno as u64, pos: (pos & !1) as u64 });
            }
        }
        validate!((src.tell() as u32) == (self.frame_pos[0] & !1));
        seek_idx.seek_info[0].filled = true;

        self.cur_frame = 0;

        Ok(())
    }
    fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
        if !self.queued_packets.is_empty() {
            let pkt = self.queued_packets.pop().unwrap();
            return Ok(pkt);
        }
        if self.cur_frame >= self.frames { return Err(DemuxerError::EOF); }
        let mut payload_size = ((self.frame_pos[self.cur_frame + 1] & !1) - (self.frame_pos[self.cur_frame] & !1)) as usize;
        validate!(payload_size > self.ainfo.len() * 4);
        for atrk in self.ainfo.iter() {
            let size                            = self.src.read_u32le()? as usize;
            validate!(payload_size > size + 4);
            payload_size -= size + 4;

            if size > 0 {
                let strres = strmgr.get_stream(atrk.id);
                validate!(strres.is_some());
                let stream = strres.unwrap();
                let ts = NATimeInfo::new(Some(self.cur_frame as u64), None, None, self.tb_num, self.tb_den);
                let pkt = self.src.read_packet(stream.clone(), ts, true, size)?;
                self.queued_packets.push(pkt);
            }
        }
        self.queued_packets.reverse();

        let strres = strmgr.get_stream(self.video_id);
        validate!(strres.is_some());
        let stream = strres.unwrap();
        let keyframe = (self.frame_pos[self.cur_frame] & 1) != 0;
        let ts = NATimeInfo::new(Some(self.cur_frame as u64), None, None, self.tb_num, self.tb_den);
        let pkt = self.src.read_packet(stream.clone(), ts, keyframe, payload_size)?;

        self.cur_frame += 1;

        Ok(pkt)
    }
    fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> {
        let ret = seek_idx.find_pos(time);
        if ret.is_none() {
            return Err(DemuxerError::SeekError);
        }
        let seek_info = ret.unwrap();
        self.src.seek(SeekFrom::Start(seek_info.pos))?;
        self.queued_packets.clear();
        self.cur_frame = seek_info.pts as usize;
        Ok(())
    }
}

impl<'a> NAOptionHandler for BinkDemuxer<'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> BinkDemuxer<'a> {
    fn new(io: &'a mut ByteReader<'a>) -> Self {
        Self {
            src:            io,
            cur_frame:      0,
            frames:         0,
            video_id:       0,
            tb_num:         0,
            tb_den:         0,
            ainfo:          Vec::new(),
            queued_packets: Vec::new(),
            frame_pos:      Vec::new(),
        }
    }
}


pub struct BinkDemuxerCreator { }

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

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

    #[test]
    fn test_bink_demux() {
        let mut file = File::open("assets/RAD/ActivisionLogo.bik").unwrap();
//        let mut file = File::open("assets/RAD/original.bik").unwrap();
//        let mut file = File::open("assets/RAD/Snd0a110c51.dee").unwrap();
//        let mut file = File::open("assets/RAD/NEW.BIK").unwrap();
//        let mut file = File::open("assets/RAD/ge_video_86l.bk2").unwrap();
        let mut fr = FileReader::new_read(&mut file);
        let mut br = ByteReader::new(&mut fr);
        let mut dmx = BinkDemuxer::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);
        }
    }
}