1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
4 struct TTADemuxer<'a> {
5 src: &'a mut ByteReader<'a>,
15 impl<'a> TTADemuxer<'a> {
16 fn new(io: &'a mut ByteReader<'a>) -> Self {
30 impl<'a> DemuxCore<'a> for TTADemuxer<'a> {
31 fn open(&mut self, strmgr: &mut StreamManager, seek_index: &mut SeekIndex) -> DemuxerResult<()> {
33 let mut hdr = vec![0; 22];
34 self.src.read_buf(&mut hdr)?;
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..])?;
48 self.framelen = srate * 256 / 245;
50 self.nframes = (self.nsamples + self.framelen - 1) / self.framelen;
51 self.duration = u64::from(self.nsamples) * 1000 / u64::from(srate);
53 seek_index.mode = SeekIndexMode::Present;
54 let mut off = u64::from(self.nframes) * 4 + 4 + 22;
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;
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();
74 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
75 if self.cur_frame >= self.nframes {
76 return Err(DemuxerError::EOF);
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))?;
83 let stream = strmgr.get_stream(0).unwrap();
84 let ts = stream.make_ts(Some(self.cur_frame.into()), None, None);
85 let pkt = self.src.read_packet(stream, ts, true, size)?;
91 fn seek(&mut self, time: NATimePoint, seek_index: &SeekIndex) -> DemuxerResult<()> {
92 let ret = seek_index.find_pos(time);
94 return Err(DemuxerError::SeekError);
96 let seek_info = ret.unwrap();
97 self.cur_frame = seek_info.pts as u32;
101 fn get_duration(&self) -> u64 { self.duration }
104 impl<'a> NAOptionHandler for TTADemuxer<'a> {
105 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
106 fn set_options(&mut self, _options: &[NAOption]) { }
107 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
110 pub struct TTADemuxerCreator { }
112 impl DemuxerCreator for TTADemuxerCreator {
113 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
114 Box::new(TTADemuxer::new(br))
116 fn get_name(&self) -> &'static str { "tta" }
125 fn test_tta_demux() {
126 // sample: https://samples.mplayerhq.hu/A-codecs/lossless/luckynight.tta
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();
135 let pktres = dmx.get_frame(&mut sm);
136 if let Err(e) = pktres {
137 if (e as i32) == (DemuxerError::EOF as i32) { break; }
140 let pkt = pktres.unwrap();
141 println!("Got {}", pkt);