forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

dec.rs64 lines, 2.1 KiBHistoryRawDownload
1//! Decode a raw DTS stream (or the STRMDATA of a `.dtshd` file) to
2//! interleaved `f32le` on stdout. Dev tool for comparing against ffmpeg.
3//!
4//! `dec <file> [out.f32]`; frames are split with `ec_dts::frame_size`.
5
6use ec_dts::DtsDecoder;
7use std::io::Write;
8
9fn strmdata(buf: &[u8]) -> &[u8] {
10 if !buf.starts_with(b"DTSHDHDR") {
11 return buf;
12 }
13 let mut p = 0;
14 while p + 16 <= buf.len() {
15 let tag = &buf[p..p + 8];
16 let size = u64::from_be_bytes(buf[p + 8..p + 16].try_into().unwrap()) as usize;
17 if tag == b"STRMDATA" {
18 return &buf[p + 16..(p + 16 + size).min(buf.len())];
19 }
20 p += 16 + size;
21 }
22 buf
23}
24
25fn main() {
26 let args: Vec<String> = std::env::args().collect();
27 let data = std::fs::read(&args[1]).unwrap();
28 let data = strmdata(&data);
29 let mut dec = DtsDecoder::new();
30 let mut out: Box<dyn Write> = match args.get(2) {
31 Some(p) => Box::new(std::io::BufWriter::new(std::fs::File::create(p).unwrap())),
32 None => Box::new(std::io::sink()),
33 };
34 let mut pos = 0;
35 let mut frames = 0;
36 let mut errors = 0;
37 while pos + 4 < data.len() {
38 let Some((s, _)) = ec_dts::find_sync(&data[pos..]) else { break };
39 pos += s;
40 let hdr = match ec_dts::parse_frame_header(&data[pos..]) {
41 Ok(h) => h,
42 Err(_) => {
43 pos += 1;
44 continue;
45 }
46 };
47 // The frame plus whatever follows up to the next core sync word.
48 let end = ec_dts::find_sync(&data[pos + hdr.stream_frame_size.min(data.len() - pos)..])
49 .map(|(o, _)| pos + hdr.stream_frame_size + o)
50 .unwrap_or(data.len());
51 match dec.decode_frame(&data[pos..end]) {
52 Ok(f) => {
53 frames += 1;
54 out.write_all(&f.data[0]).unwrap();
55 }
56 Err(e) => {
57 errors += 1;
58 eprintln!("frame at {pos}: {e}");
59 }
60 }
61 pos = end;
62 }
63 eprintln!("frames={frames} errors={errors} info={:?}", dec.frame_info().map(|i| (i.sample_rate, i.header.channels, i.extensions, i.xll)));
64}