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