forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

h264_clip_encode.rs107 lines, 4.2 KiBHistoryRawDownload
1//! Encode a raw I420 clip and write the bitstream and the encoder's own
2//! reconstruction, for the ffmpeg recon-match and rate-control checks in
3//! `scripts/h264-lane-check.sh`. Not a product surface.
4//!
5//! Usage: h264_clip_encode <in.yuv|-> <width> <height> <fps> <bitrate|0> <qp> <threads> <out-prefix|none>
6//!
7//! `-` reads raw I420 from stdin, so a 60 s 1080p clip never has to exist on
8//! disk. Writes `<out-prefix>.h264` (Annex B), and `<out-prefix>.recon.yuv`
9//! (every reconstructed picture, display order, I420 at the coded size) unless
10//! the prefix is `none`. Prints frames, bytes and the wall time spent inside
11//! `encode` on stdout.
12use ec_h264::{Encoder, EncoderConfig, PictureView};
13use std::io::Write;
14use std::time::Instant;
15
16fn main() {
17 let args: Vec<String> = std::env::args().collect();
18 if args.len() != 9 {
19 eprintln!(
20 "usage: h264_clip_encode <in.yuv|-> <w> <h> <fps> <bitrate> <qp> <threads> <out-prefix|none>"
21 );
22 std::process::exit(2);
23 }
24 let parse = |i: usize| -> f64 { args[i].parse().expect("numeric argument") };
25 let (w, h) = (parse(2) as u32, parse(3) as u32);
26 let fps = parse(4) as f32;
27 let bitrate = parse(5) as u32;
28 let qp = parse(6) as i32;
29 let threads = parse(7) as usize;
30 let prefix = &args[8];
31
32 let (wu, hu) = (w as usize, h as usize);
33 let cw = wu.div_ceil(2);
34 let ch = hu.div_ceil(2);
35 let frame_len = wu * hu + 2 * cw * ch;
36 let mut input: Box<dyn std::io::Read> = if args[1] == "-" {
37 Box::new(std::io::stdin().lock())
38 } else {
39 Box::new(std::fs::File::open(&args[1]).expect("open raw i420 input"))
40 };
41
42 let mut cfg = EncoderConfig::new(w, h);
43 cfg.framerate = fps;
44 cfg.bitrate = bitrate;
45 cfg.qp = qp;
46 cfg.threads = threads;
47 // H264_CAVLC=1 codes CAVLC instead of the default CABAC (identity checks).
48 cfg.cabac = std::env::var_os("H264_CAVLC").is_none();
49 cfg.gop_size = (fps.round() as u32).max(1) * 2;
50 let mut enc = Encoder::new(cfg).expect("h264 encoder");
51
52 let mut stream_out = (prefix != "none")
53 .then(|| std::fs::File::create(format!("{prefix}.h264")).expect("create bitstream"));
54 let mut recon_out = (prefix != "none")
55 .then(|| std::fs::File::create(format!("{prefix}.recon.yuv")).expect("create recon"));
56 // H264_TRACE=1 prints one line per picture: its QP and size.
57 let trace = std::env::var_os("H264_TRACE").is_some();
58 let mut frame = vec![0u8; frame_len];
59 let mut frames = 0usize;
60 let mut bytes = 0usize;
61 let mut spent = std::time::Duration::ZERO;
62 while input.read_exact(&mut frame).is_ok() {
63 let (y, rest) = frame.split_at(wu * hu);
64 let (u, v) = rest.split_at(cw * ch);
65 let view = PictureView::i420(w, h, y, u, v);
66 let t0 = Instant::now();
67 let pic = enc.encode(&view).expect("h264 encode");
68 spent += t0.elapsed();
69 if trace {
70 println!(
71 "trace {frames} key {} qp {} bytes {}",
72 u8::from(pic.key_frame),
73 pic.qp,
74 pic.au.len()
75 );
76 }
77 frames += 1;
78 bytes += pic.au.len();
79 if let Some(out) = stream_out.as_mut() {
80 out.write_all(&pic.au).expect("write bitstream");
81 }
82 if let Some(out) = recon_out.as_mut() {
83 let (ry, ru, rv) = enc.reconstruction().expect("a reconstruction");
84 out.write_all(&ry)
85 .and_then(|_| out.write_all(&ru))
86 .and_then(|_| out.write_all(&rv))
87 .expect("write reconstruction");
88 }
89 }
90 println!(
91 "frames {frames} bytes {bytes} encode_secs {:.3} fps {:.2}",
92 spent.as_secs_f64(),
93 frames as f64 / spent.as_secs_f64()
94 );
95 let t = enc.timings();
96 println!(
97 "stages pad {:.3} bands_wall {:.3} bands_busy {:.3} bands_slowest {:.3} stitch {:.3} deblock {:.3} output {:.3}",
98 t.pad, t.bands_wall, t.bands_busy, t.bands_slowest, t.stitch, t.deblock, t.output
99 );
100 #[cfg(feature = "enc-profile")]
101 {
102 let snap = ec_h264::mb_stages::snapshot();
103 for (name, ns) in ec_h264::mb_stages::NAMES.iter().zip(snap) {
104 println!("mb_stage {name} {:.3}", ns as f64 / 1e9);
105 }
106 }
107}