forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB8dbd683Tahinli3 months ago856 commits

ac3dec.rs135 lines, 4.2 KiBHistoryRawDownload
1//! Decode an AC-3 / E-AC-3 elementary stream to interleaved 32-bit float.
2//!
3//! ```text
4//! cargo run --release --example ac3dec -- in.ac3 out.f32 [--downmix stereo|mono] [--drc 0.0]
5//! ```
6//!
7//! Prints the stream's layout and the decode speed relative to realtime, which
8//! is how the crate's perf claim is measured.
9
10use std::io::{BufWriter, Write};
11use std::time::Instant;
12
13use ec_ac3::{Ac3Decoder, Downmix, Options};
14
15fn main() {
16 let argv: Vec<String> = std::env::args().skip(1).collect();
17 if argv.len() < 2 {
18 eprintln!(
19 "usage: ac3dec <in.ac3> <out.f32> [--downmix stereo|mono] [--drc SCALE] \
20 [--dither on|off]"
21 );
22 std::process::exit(2);
23 }
24 let mut options = Options::default();
25 let mut i = 2;
26 while i + 1 < argv.len() {
27 match argv[i].as_str() {
28 "--downmix" => {
29 options.downmix = match argv[i + 1].as_str() {
30 "stereo" => Downmix::Stereo,
31 "mono" => Downmix::Mono,
32 other => {
33 eprintln!("ac3dec: unknown downmix {other}");
34 std::process::exit(2);
35 }
36 }
37 }
38 "--drc" => options.drc_scale = argv[i + 1].parse().unwrap_or(1.0),
39 "--dither" => options.dither = argv[i + 1] != "off",
40 other => {
41 eprintln!("ac3dec: unknown flag {other}");
42 std::process::exit(2);
43 }
44 }
45 i += 2;
46 }
47
48 let data = match std::fs::read(&argv[0]) {
49 Ok(d) => d,
50 Err(e) => {
51 eprintln!("ac3dec: {}: {e}", argv[0]);
52 std::process::exit(2);
53 }
54 };
55 let mut decoder = Ac3Decoder::with_options(options);
56 let file = match std::fs::File::create(&argv[1]) {
57 Ok(f) => f,
58 Err(e) => {
59 eprintln!("ac3dec: {}: {e}", argv[1]);
60 std::process::exit(2);
61 }
62 };
63 // Streamed, because a two-hour 5.1 track is ten gigabytes of `f32`.
64 let mut out = BufWriter::with_capacity(1 << 20, file);
65 let (mut pos, mut frames, mut samples, mut errors) = (0usize, 0u64, 0u64, Vec::new());
66 let mut rate = 0;
67 let mut channels = 0;
68 let start = Instant::now();
69 while pos + 6 <= data.len() {
70 if data[pos] != 0x0B || data[pos + 1] != 0x77 {
71 pos += 1;
72 continue;
73 }
74 let size = match ec_ac3::frame_size(&data[pos..]) {
75 Ok(size) if pos + size <= data.len() => size,
76 Ok(_) => break,
77 Err(e) => {
78 errors.push(format!("frame at {pos}: {e}"));
79 pos += 2;
80 continue;
81 }
82 };
83 match decoder.decode_frame(&data[pos..pos + size]) {
84 Ok(frame) => {
85 rate = frame.rate;
86 channels = frame.channels();
87 samples += frame.samples as u64;
88 frames += 1;
89 let _ = out.write_all(&frame.data[0]);
90 }
91 Err(e) => {
92 // Keep the timeline: a frame that will not decode becomes the
93 // silence it represents, so a comparison stays sample-aligned.
94 errors.push(format!("frame at {pos}: {e}"));
95 if channels > 0 {
96 let _ = out.write_all(&vec![0u8; channels * 1536 * 4]);
97 samples += 1536;
98 }
99 }
100 }
101 pos += size;
102 }
103 let elapsed = start.elapsed().as_secs_f64();
104
105 if let Err(e) = out.flush() {
106 eprintln!("ac3dec: {}: {e}", argv[1]);
107 std::process::exit(2);
108 }
109 let seconds = if rate > 0 {
110 samples as f64 / f64::from(rate)
111 } else {
112 0.0
113 };
114 println!("frames\t{frames}");
115 println!("channels\t{channels}");
116 println!("sample_rate\t{rate}");
117 println!("samples\t{samples}");
118 println!("seconds\t{seconds:.3}");
119 println!("decode_seconds\t{elapsed:.3}");
120 println!(
121 "realtime\t{:.1}",
122 if elapsed > 0.0 {
123 seconds / elapsed
124 } else {
125 0.0
126 }
127 );
128 println!("errors\t{}", errors.len());
129 for e in errors.iter().take(5) {
130 println!("error\t{e}");
131 }
132 if frames == 0 {
133 std::process::exit(1);
134 }
135}