]> git.nihav.org Git - nihav.git/blob - nihav-llaudio/src/demuxers/tta.rs
add nihav-llaudio crate with FLAC, Monkey's Audio, TTA and WavPack support
[nihav.git] / nihav-llaudio / src / demuxers / tta.rs
1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
3
4 struct TTADemuxer<'a> {
5 src: &'a mut ByteReader<'a>,
6 cur_frame: u32,
7 nframes: u32,
8 nsamples: u32,
9 offtab: Vec<u64>,
10 sizetab: Vec<u32>,
11 framelen: u32,
12 }
13
14 impl<'a> TTADemuxer<'a> {
15 fn new(io: &'a mut ByteReader<'a>) -> Self {
16 Self {
17 src: io,
18 cur_frame: 0,
19 nframes: 0,
20 nsamples: 0,
21 offtab: Vec::new(),
22 sizetab: Vec::new(),
23 framelen: 0,
24 }
25 }
26 }
27
28 impl<'a> DemuxCore<'a> for TTADemuxer<'a> {
29 fn open(&mut self, strmgr: &mut StreamManager, seek_index: &mut SeekIndex) -> DemuxerResult<()> {
30
31 let mut hdr = vec![0; 22];
32 self.src.read_buf(&mut hdr)?;
33
34 validate!(&hdr[..4] == b"TTA1");
35 let _afmt = read_u16le(&hdr[4..])?;
36 let channels = read_u16le(&hdr[6..])?;
37 validate!(channels > 0 && channels < 256);
38 let bpp = read_u16le(&hdr[8..])?;
39 validate!(bpp > 0 && bpp <= 32);
40 let srate = read_u32le(&hdr[10..])?;
41 validate!(srate > 256 && srate < 1048576);
42 self.nsamples = read_u32le(&hdr[14..])?;
43 validate!(self.nsamples > 0);
44 let _crc = read_u32le(&hdr[18..])?;
45
46 self.framelen = srate * 256 / 245;
47
48 self.nframes = (self.nsamples + self.framelen - 1) / self.framelen;
49
50 seek_index.mode = SeekIndexMode::Present;
51 let mut off = u64::from(self.nframes) * 4 + 4 + 22;
52 let mut cur_time = 0;
53 for pts in 0..self.nframes {
54 let fsize = self.src.read_u32le()?;
55 self.sizetab.push(fsize);
56 self.offtab.push(off);
57 let time = u64::from(cur_time) * 1000 / u64::from(srate);
58 seek_index.add_entry(0, SeekEntry { time, pts: u64::from(pts), pos: off });
59 off += u64::from(fsize);
60 cur_time += self.framelen;
61 }
62
63 let ahdr = NAAudioInfo::new(srate, channels as u8, SND_S16P_FORMAT, 1);
64 let ainfo = NACodecInfo::new("tta", NACodecTypeInfo::Audio(ahdr), Some(hdr));
65 strmgr.add_stream(NAStream::new(StreamType::Audio, 0, ainfo, self.framelen, srate)).unwrap();
66
67 self.cur_frame = 0;
68
69 Ok(())
70 }
71 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
72 if self.cur_frame >= self.nframes {
73 return Err(DemuxerError::EOF);
74 }
75
76 let size = self.sizetab[self.cur_frame as usize] as usize;
77 let off = self.offtab[self.cur_frame as usize];
78 self.src.seek(SeekFrom::Start(off))?;
79
80 let stream = strmgr.get_stream(0).unwrap();
81 let (tb_num, tb_den) = stream.get_timebase();
82 let ts = NATimeInfo::new(Some(self.cur_frame.into()), None, None, tb_num, tb_den);
83 let pkt = self.src.read_packet(stream, ts, true, size)?;
84
85 self.cur_frame += 1;
86
87 Ok(pkt)
88 }
89 fn seek(&mut self, time: NATimePoint, seek_index: &SeekIndex) -> DemuxerResult<()> {
90 let ret = seek_index.find_pos(time);
91 if ret.is_none() {
92 return Err(DemuxerError::SeekError);
93 }
94 let seek_info = ret.unwrap();
95 self.cur_frame = seek_info.pts as u32;
96
97 Ok(())
98 }
99 }
100
101 impl<'a> NAOptionHandler for TTADemuxer<'a> {
102 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
103 fn set_options(&mut self, _options: &[NAOption]) { }
104 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
105 }
106
107 pub struct TTADemuxerCreator { }
108
109 impl DemuxerCreator for TTADemuxerCreator {
110 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
111 Box::new(TTADemuxer::new(br))
112 }
113 fn get_name(&self) -> &'static str { "tta" }
114 }
115
116 #[cfg(test)]
117 mod test {
118 use super::*;
119 use std::fs::File;
120
121 #[test]
122 fn test_tta_demux() {
123 let mut file = File::open("assets/LLaudio/luckynight.tta").unwrap();
124 let mut fr = FileReader::new_read(&mut file);
125 let mut br = ByteReader::new(&mut fr);
126 let mut dmx = TTADemuxer::new(&mut br);
127 let mut sm = StreamManager::new();
128 let mut si = SeekIndex::new();
129 dmx.open(&mut sm, &mut si).unwrap();
130 loop {
131 let pktres = dmx.get_frame(&mut sm);
132 if let Err(e) = pktres {
133 if (e as i32) == (DemuxerError::EOF as i32) { break; }
134 panic!("error");
135 }
136 let pkt = pktres.unwrap();
137 println!("Got {}", pkt);
138 }
139 }
140 }