forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

dec.rs53 lines, 2.3 KiBHistoryRawDownload
1#![allow(clippy::needless_range_loop, clippy::field_reassign_with_default, clippy::too_many_arguments, clippy::type_complexity)]
2//! Decode an MPEG-1/2 elementary stream and print per-picture facts; with a
3//! second argument write raw planar frames. Also a throughput measurement.
4use std::io::Write;
5use std::time::Instant;
6
7use ec_core::registry::{CodecId, CodecParameters};
8use ec_mpeg2::Mpeg2Decoder;
9
10fn main() {
11 let args: Vec<String> = std::env::args().collect();
12 let data = std::fs::read(&args[1]).expect("read");
13 let mut out = args.get(2).map(|p| std::fs::File::create(p).expect("create"));
14 let quiet = std::env::var_os("QUIET").is_some();
15 let reps: usize = std::env::var("REPS").ok().and_then(|v| v.parse().ok()).unwrap_or(1);
16 let t0 = Instant::now();
17 let mut frames = 0usize;
18 for _ in 0..reps {
19 let mut dec = Mpeg2Decoder::new(CodecParameters::new(CodecId::Mpeg2Video)).unwrap();
20 for chunk in data.chunks(65536) {
21 if let Err(e) = dec.send_bytes(chunk, None) {
22 eprintln!("error: {e}");
23 }
24 while let Some((f, info)) = dec.pop_picture() {
25 frames += 1;
26 if !quiet {
27 println!("{:?} tr {} {}x{} {:?} corrupt {} tff {} rff {} prog {}", info.picture_type, info.temporal_reference, f.width, f.height, f.interlace, info.corrupt, info.top_field_first, info.repeat_first_field, info.progressive_frame);
28 }
29 if let Some(o) = out.as_mut() {
30 for p in &f.planes {
31 o.write_all(&p.data).unwrap();
32 }
33 }
34 }
35 }
36 if let Err(e) = dec.flush_stream() {
37 eprintln!("error: {e}");
38 }
39 while let Some((f, info)) = dec.pop_picture() {
40 frames += 1;
41 if !quiet {
42 println!("{:?} tr {} {}x{} {:?} corrupt {}", info.picture_type, info.temporal_reference, f.width, f.height, f.interlace, info.corrupt);
43 }
44 if let Some(o) = out.as_mut() {
45 for p in &f.planes {
46 o.write_all(&p.data).unwrap();
47 }
48 }
49 }
50 }
51 let dt = t0.elapsed().as_secs_f64();
52 eprintln!("{frames} frames in {dt:.3}s = {:.1} fps", frames as f64 / dt);
53}