1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
4 struct TTADemuxer<'a> {
5 src: &'a mut ByteReader<'a>,
14 impl<'a> TTADemuxer<'a> {
15 fn new(io: &'a mut ByteReader<'a>) -> Self {
28 impl<'a> DemuxCore<'a> for TTADemuxer<'a> {
29 fn open(&mut self, strmgr: &mut StreamManager, seek_index: &mut SeekIndex) -> DemuxerResult<()> {
31 let mut hdr = vec![0; 22];
32 self.src.read_buf(&mut hdr)?;
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..])?;
46 self.framelen = srate * 256 / 245;
48 self.nframes = (self.nsamples + self.framelen - 1) / self.framelen;
50 seek_index.mode = SeekIndexMode::Present;
51 let mut off = u64::from(self.nframes) * 4 + 4 + 22;
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;
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, u64::from(self.nframes))).unwrap();
71 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
72 if self.cur_frame >= self.nframes {
73 return Err(DemuxerError::EOF);
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))?;
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)?;
89 fn seek(&mut self, time: NATimePoint, seek_index: &SeekIndex) -> DemuxerResult<()> {
90 let ret = seek_index.find_pos(time);
92 return Err(DemuxerError::SeekError);
94 let seek_info = ret.unwrap();
95 self.cur_frame = seek_info.pts as u32;
99 fn get_duration(&self) -> u64 { 0 }
102 impl<'a> NAOptionHandler for TTADemuxer<'a> {
103 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
104 fn set_options(&mut self, _options: &[NAOption]) { }
105 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
108 pub struct TTADemuxerCreator { }
110 impl DemuxerCreator for TTADemuxerCreator {
111 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
112 Box::new(TTADemuxer::new(br))
114 fn get_name(&self) -> &'static str { "tta" }
123 fn test_tta_demux() {
124 let mut file = File::open("assets/LLaudio/luckynight.tta").unwrap();
125 let mut fr = FileReader::new_read(&mut file);
126 let mut br = ByteReader::new(&mut fr);
127 let mut dmx = TTADemuxer::new(&mut br);
128 let mut sm = StreamManager::new();
129 let mut si = SeekIndex::new();
130 dmx.open(&mut sm, &mut si).unwrap();
132 let pktres = dmx.get_frame(&mut sm);
133 if let Err(e) = pktres {
134 if (e as i32) == (DemuxerError::EOF as i32) { break; }
137 let pkt = pktres.unwrap();
138 println!("Got {}", pkt);