Debian Codegraph @

.cargo/vendor/rav1e/src/bin/rav1e-ch.rs

raw
1 // Copyright (c) 2017-2023, The rav1e contributors. All rights reserved
2 //
3 // This source code is subject to the terms of the BSD 2 Clause License and
4 // the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
5 // was not distributed with this source code in the LICENSE file, you can
6 // obtain it at www.aomedia.org/license/software. If the Alliance for Open
7 // Media Patent License 1.0 was not distributed with this source code in the
8 // PATENTS file, you can obtain it at www.aomedia.org/license/patent.
9
10 #[macro_use]
11 extern crate log;
12
13 mod common;
14 mod decoder;
15 mod error;
16 #[cfg(feature = "serialize")]
17 mod kv;
18 mod muxer;
19 mod stats;
20
21 use crate::common::*;
22 use crate::error::*;
23 use crate::stats::*;
24 use rav1e::config::CpuFeatureLevel;
25 use rav1e::prelude::*;
26
27 use crate::decoder::{Decoder, FrameBuilder, VideoDetails};
28 use crate::muxer::*;
29 use std::fs::File;
30 use std::io::{Read, Seek, Write};
31 use std::sync::Arc;
32
33 impl<T: Pixel> FrameBuilder<T> for FrameSender<T> {
34 fn new_frame(&self) -> Frame<T> {
35 FrameSender::new_frame(self)
36 }
37 }
38
39 struct Source<D: Decoder> {
40 limit: usize,
41 count: usize,
42 input: D,
43 #[cfg(all(unix, feature = "signal-hook"))]
44 exit_requested: Arc<std::sync::atomic::AtomicBool>,
45 }
46
47 impl<D: Decoder> Source<D> {
48 cfg_if::cfg_if! {
49 if #[cfg(all(unix, feature = "signal-hook"))] {
50 fn new(limit: usize, input: D) -> Self {
51 use signal_hook::{flag, consts};
52
53 // Make sure double CTRL+C and similar kills
54 let exit_requested = Arc::new(std::sync::atomic::AtomicBool::new(false));
55 for sig in consts::TERM_SIGNALS {
56 // When terminated by a second term signal, exit with exit code 1.
57 // This will do nothing the first time (because term_now is false).
58 flag::register_conditional_shutdown(*sig, 1, Arc::clone(&exit_requested)).unwrap();
59 // But this will "arm" the above for the second time, by setting it to true.
60 // The order of registering these is important, if you put this one first, it will
61 // first arm and then terminate ‒ all in the first round.
62 flag::register(*sig, Arc::clone(&exit_requested)).unwrap();
63 }
64 Self { limit, input, count: 0, exit_requested, }
65 }
66 } else {
67 fn new(limit: usize, input: D) -> Self {
68 Self { limit, input, count: 0, }
69 }
70 }
71 }
72
73 fn read_frame<T: Pixel>(
74 &mut self, send_frame: &mut FrameSender<T>, video_info: VideoDetails,
75 ) -> bool {
76 if self.limit != 0 && self.count == self.limit {
77 return false;
78 }
79
80 #[cfg(all(unix, feature = "signal-hook"))]
81 {
82 if self.exit_requested.load(std::sync::atomic::Ordering::SeqCst) {
83 return false;
84 }
85 }
86 match self.input.read_frame(send_frame, &video_info) {
87 Ok(frame) => {
88 self.count += 1;
89 let _ = send_frame.send(frame);
90 true
91 }
92 _ => false,
93 }
94 }
95 }
96
97 fn do_encode<T: Pixel, D: Decoder>(
98 cfg: Config, verbose: Verboseness, mut progress: ProgressInfo,
99 output: &mut dyn Muxer, mut source: Source<D>, pass1file: Option<File>,
100 pass2file: Option<File>,
101 mut y4m_enc: Option<y4m::Encoder<Box<dyn Write + Send>>>,
102 metrics_enabled: MetricsEnabled,
103 ) -> Result<(), CliError> {
104 let ((mut send_frame, receive_packet), (send_rc, receive_rc)) =
105 match (pass1file.is_some(), pass2file.is_some()) {
106 (true, true) => {
107 let (channel, (send_rc, receive_rc)) = cfg
108 .new_multipass_channel::<T>()
109 .map_err(|e| e.context("Invalid setup"))?;
110 (channel, (Some(send_rc), Some(receive_rc)))
111 }
112 (true, false) => {
113 let (channel, receive_rc) = cfg
114 .new_firstpass_channel()
115 .map_err(|e| e.context("Invalid setup"))?;
116 (channel, (None, Some(receive_rc)))
117 }
118 (false, true) => {
119 let (channel, send_rc) = cfg
120 .new_secondpass_channel()
121 .map_err(|e| e.context("Invalid setup"))?;
122 (channel, (Some(send_rc), None))
123 }
124 (false, false) => {
125 let channel =
126 cfg.new_channel().map_err(|e| e.context("Invalid setup"))?;
127 (channel, (None, None))
128 }
129 };
130
131 let y4m_details = source.input.get_video_details();
132
133 crossbeam::thread::scope(move |s| -> Result<(), CliError> {
134 // Receive pass data
135 let receive_pass_data = s.spawn(move |_| -> Result<(), CliError> {
136 if let (Some(mut passfile), Some(receive_rc)) = (pass1file, receive_rc) {
137 let len = receive_rc.summary_size();
138 let buf = vec![0u8; len];
139
140 passfile
141 .write_all(&(len as u64).to_be_bytes())
142 .map_err(|e| e.context("Unable to write to two-pass data file."))?;
143
144 passfile
145 .write_all(&buf)
146 .map_err(|e| e.context("Unable to write to two-pass data file."))?;
147 for data in receive_rc.iter() {
148 match data {
149 RcData::Frame(outbuf) => {
150 let len = outbuf.len() as u64;
151 passfile.write_all(&len.to_be_bytes()).map_err(|e| {
152 e.context("Unable to write to two-pass data file.")
153 })?;
154
155 passfile.write_all(&outbuf).map_err(|e| {
156 e.context("Unable to write to two-pass data file.")
157 })?;
158 }
159 RcData::Summary(outbuf) => {
160 // Write an end marker
161 passfile.write_all(&0u64.to_be_bytes()).map_err(|e| {
162 e.context("Unable to write to two-pass data file.")
163 })?;
164
165 // The last packet of rate control data we get is the summary data.
166 // Let's put it at the start of the file.
167 passfile.seek(std::io::SeekFrom::Start(0)).map_err(|e| {
168 e.context("Unable to seek in the two-pass data file.")
169 })?;
170 let len = outbuf.len() as u64;
171
172 passfile.write_all(&len.to_be_bytes()).map_err(|e| {
173 e.context("Unable to write to two-pass data file.")
174 })?;
175
176 passfile.write_all(&outbuf).map_err(|e| {
177 e.context("Unable to write to two-pass data file.")
178 })?;
179 }
180 }
181 }
182 }
183 Ok(())
184 });
185
186 // Send frames
187 let send_frames = s.spawn(move |_| -> Result<(), CliError> {
188 while source.read_frame(&mut send_frame, y4m_details) {}
189
190 // send_frame.result()
191 Ok(())
192 });
193
194 // Send pass data
195 let send_pass_data = s.spawn(move |_| -> Result<(), CliError> {
196 if let (Some(mut passfile), Some(mut send_rc)) = (pass2file, send_rc) {
197 let mut buflen = [0u8; 8];
198
199 passfile
200 .read_exact(&mut buflen)
201 .map_err(|e| e.context("Unable to read the two-pass data file."))?;
202
203 let mut data = vec![0u8; u64::from_be_bytes(buflen) as usize];
204
205 passfile
206 .read_exact(&mut data)
207 .map_err(|e| e.context("Unable to read the two-pass data file."))?;
208
209 while send_rc.send(RcData::Frame(data.into_boxed_slice())).is_ok() {
210 passfile.read_exact(&mut buflen).map_err(|e| {
211 e.context("Unable to read the two-pass data file.")
212 })?;
213
214 if u64::from_be_bytes(buflen) == 0 {
215 break;
216 }
217
218 data = vec![0u8; u64::from_be_bytes(buflen) as usize];
219
220 passfile.read_exact(&mut data).map_err(|e| {
221 e.context("Unable to read the two-pass data file.")
222 })?;
223 }
224 }
225
226 Ok(())
227 });
228
229 // Receive Packets
230 let receive_packets = s.spawn(move |_| -> Result<(), CliError> {
231 for pkt in receive_packet.iter() {
232 output.write_frame(
233 pkt.input_frameno as u64,
234 pkt.data.as_ref(),
235 pkt.frame_type,
236 );
237 output.flush().unwrap();
238 if let (Some(ref mut y4m_enc_uw), Some(ref rec)) =
239 (y4m_enc.as_mut(), &pkt.rec)
240 {
241 write_y4m_frame(y4m_enc_uw, rec, y4m_details);
242 }
243 let summary = build_frame_summary(
244 pkt,
245 y4m_details.bit_depth,
246 y4m_details.chroma_sampling,
247 metrics_enabled,
248 );
249
250 if verbose != Verboseness::Quiet {
251 progress.add_frame(summary.clone());
252 if verbose == Verboseness::Verbose {
253 info!("{} - {}", summary, progress);
254 } else {
255 // Print a one-line progress indicator that overrides itself with every update
256 eprint!("\r{} ", progress);
257 };
258 }
259 }
260
261 if verbose != Verboseness::Quiet {
262 if verbose == Verboseness::Verbose {
263 // Clear out the temporary progress indicator
264 eprint!("\r");
265 }
266 progress.print_summary(verbose == Verboseness::Verbose);
267 }
268
269 // receive_packet.result()
270 Ok(())
271 });
272
273 send_pass_data.join().expect("The send pass data thread panicked ")?;
274 receive_pass_data
275 .join()
276 .expect("The receive pass data thread panicked")?;
277 send_frames.join().expect("The send frames thread panicked")?;
278 receive_packets.join().expect("The receive packets thread panicked")?;
279
280 Ok(())
281 })
282 .unwrap()
283 }
284
285 fn main() -> Result<(), Box<dyn std::error::Error>> {
286 init_logger();
287
288 #[cfg(feature = "tracing")]
289 let (chrome_layer, _guard) =
290 tracing_chrome::ChromeLayerBuilder::new().build();
291
292 #[cfg(feature = "tracing")]
293 {
294 use tracing_subscriber::layer::SubscriberExt;
295 tracing::subscriber::set_global_default(
296 tracing_subscriber::registry().with(chrome_layer),
297 )
298 .unwrap();
299 }
300
301 run().map_err(|e| {
302 error::print_error(&e);
303 Box::new(e) as Box<dyn std::error::Error>
304 })
305 }
306
307 fn init_logger() {
308 use std::str::FromStr;
309 fn level_colored(l: log::Level) -> console::StyledObject<&'static str> {
310 use console::style;
311 use log::Level;
312 match l {
313 Level::Trace => style("??").dim(),
314 Level::Debug => style("? ").dim(),
315 Level::Info => style("> ").green(),
316 Level::Warn => style("! ").yellow(),
317 Level::Error => style("!!").red(),
318 }
319 }
320
321 let level = std::env::var("RAV1E_LOG")
322 .ok()
323 .and_then(|l| log::LevelFilter::from_str(&l).ok())
324 .unwrap_or(log::LevelFilter::Info);
325
326 fern::Dispatch::new()
327 .format(move |out, message, record| {
328 out.finish(format_args!(
329 "{level} {message}",
330 level = level_colored(record.level()),
331 message = message,
332 ));
333 })
334 // set the default log level. to filter out verbose log messages from dependencies, set
335 // this to Warn and overwrite the log level for your crate.
336 .level(log::LevelFilter::Warn)
337 // change log levels for individual modules. Note: This looks for the record's target
338 // field which defaults to the module path but can be overwritten with the `target`
339 // parameter:
340 // `info!(target="special_target", "This log message is about special_target");`
341 .level_for("rav1e", level)
342 .level_for("rav1e_ch", level)
343 // output to stdout
344 .chain(std::io::stderr())
345 .apply()
346 .unwrap();
347 }
348
349 cfg_if::cfg_if! {
350 if #[cfg(any(target_os = "windows", target_arch = "wasm32"))] {
351 fn print_rusage() {
352 eprintln!("Resource usage reporting is not currently supported on this platform");
353 }
354 } else {
355 fn print_rusage() {
356 // SAFETY: This uses an FFI, it is safe because we call it correctly.
357 let (utime, stime, maxrss) = unsafe {
358 let mut usage = std::mem::zeroed();
359 let _ = libc::getrusage(libc::RUSAGE_SELF, &mut usage);
360 (usage.ru_utime, usage.ru_stime, usage.ru_maxrss)
361 };
362 eprintln!(
363 "user time: {} s",
364 utime.tv_sec as f64 + utime.tv_usec as f64 / 1_000_000f64
365 );
366 eprintln!(
367 "system time: {} s",
368 stime.tv_sec as f64 + stime.tv_usec as f64 / 1_000_000f64
369 );
370 eprintln!("maximum rss: {} KB", maxrss);
371 }
372 }
373 }
374
375 fn run() -> Result<(), error::CliError> {
376 let mut cli = parse_cli()?;
377 // Maximum frame size by specification + maximum y4m header
378 let limit = y4m::Limits {
379 // Use saturating operations to gracefully handle 32-bit architectures
380 bytes: 64usize
381 .saturating_mul(64)
382 .saturating_mul(4096)
383 .saturating_mul(2304)
384 .saturating_add(1024),
385 };
386 let mut y4m_dec = match y4m::Decoder::new_with_limits(cli.io.input, limit) {
387 Err(e) => {
388 return Err(CliError::new(match e {
389 y4m::Error::ParseError(_) => {
390 "Could not parse input video. Is it a y4m file?"
391 }
392 y4m::Error::IoError(_) => {
393 "Could not read input file. Check that the path is correct and you have read permissions."
394 }
395 y4m::Error::UnknownColorspace => {
396 "Unknown colorspace or unsupported bit depth."
397 }
398 y4m::Error::OutOfMemory => "The video's frame size exceeds the limit.",
399 y4m::Error::EOF => "Unexpected end of input.",
400 y4m::Error::BadInput => "Bad y4m input parameters provided.",
401 }))
402 }
403 Ok(d) => d,
404 };
405 let video_info = y4m_dec.get_video_details();
406 let y4m_enc = cli.io.rec.map(|rec| {
407 y4m::encode(
408 video_info.width,
409 video_info.height,
410 y4m::Ratio::new(
411 video_info.time_base.den as usize,
412 video_info.time_base.num as usize,
413 ),
414 )
415 .with_colorspace(y4m_dec.get_colorspace())
416 .with_pixel_aspect(y4m::Ratio {
417 num: video_info.sample_aspect_ratio.num as usize,
418 den: video_info.sample_aspect_ratio.den as usize,
419 })
420 .write_header(rec)
421 .unwrap()
422 });
423
424 match video_info.bit_depth {
425 8 | 10 | 12 => {}
426 _ => return Err(CliError::new("Unsupported bit depth")),
427 }
428
429 cli.enc.width = video_info.width;
430 cli.enc.height = video_info.height;
431 cli.enc.bit_depth = video_info.bit_depth;
432 cli.enc.sample_aspect_ratio = video_info.sample_aspect_ratio;
433 cli.enc.chroma_sampling = video_info.chroma_sampling;
434 cli.enc.chroma_sample_position = video_info.chroma_sample_position;
435
436 // If no pixel range is specified via CLI, assume limited,
437 // as it is the default for the Y4M format.
438 if !cli.color_range_specified {
439 cli.enc.pixel_range = PixelRange::Limited;
440 }
441
442 if !cli.override_time_base {
443 cli.enc.time_base = video_info.time_base;
444 }
445
446 if cli.photon_noise > 0 && cli.enc.film_grain_params.is_none() {
447 cli.enc.film_grain_params = Some(vec![generate_photon_noise_params(
448 0,
449 u64::MAX,
450 NoiseGenArgs {
451 iso_setting: cli.photon_noise as u32 * 100,
452 width: video_info.width as u32,
453 height: video_info.height as u32,
454 transfer_function: if cli.enc.is_hdr() {
455 TransferFunction::SMPTE2084
456 } else {
457 TransferFunction::BT1886
458 },
459 chroma_grain: false,
460 random_seed: None,
461 },
462 )]);
463 }
464
465 let mut rc = RateControlConfig::new();
466
467 let pass2file = match cli.pass2file_name {
468 Some(f) => {
469 let mut f = File::open(f).map_err(|e| {
470 e.context("Unable to open file for reading two-pass data")
471 })?;
472 let mut buflen = [0u8; 8];
473 f.read_exact(&mut buflen)
474 .map_err(|e| e.context("Summary data too short"))?;
475 let len = i64::from_be_bytes(buflen);
476 let mut buf = vec![0u8; len as usize];
477
478 f.read_exact(&mut buf)
479 .map_err(|e| e.context("Summary data too short"))?;
480
481 rc = RateControlConfig::from_summary_slice(&buf)
482 .map_err(|e| e.context("Invalid summary"))?;
483
484 Some(f)
485 }
486 None => None,
487 };
488
489 let pass1file = match cli.pass1file_name {
490 Some(f) => {
491 let f = File::create(f).map_err(|e| {
492 e.context("Unable to open file for writing two-pass data")
493 })?;
494 rc = rc.with_emit_data(true);
495 Some(f)
496 }
497 None => None,
498 };
499
500 let cfg = Config::new()
501 .with_encoder_config(cli.enc.clone())
502 .with_threads(cli.threads)
503 .with_rate_control(rc)
504 .with_parallel_gops(cli.slots);
505
506 #[cfg(feature = "serialize")]
507 {
508 if let Some(save_config) = cli.save_config {
509 let mut out = File::create(save_config)
510 .map_err(|e| e.context("Cannot create configuration file"))?;
511 let s = toml::to_string(&cli.enc).unwrap();
512 out
513 .write_all(s.as_bytes())
514 .map_err(|e| e.context("Cannot write the configuration file"))?
515 }
516 }
517
518 cli.io.output.write_header(
519 video_info.width,
520 video_info.height,
521 cli.enc.time_base.den as usize,
522 cli.enc.time_base.num as usize,
523 );
524
525 let tiling =
526 cfg.tiling_info().map_err(|e| e.context("Invalid configuration"))?;
527 if cli.verbose != Verboseness::Quiet {
528 info!("CPU Feature Level: {}", CpuFeatureLevel::default());
529
530 info!(
531 "Using y4m decoder: {}x{}p @ {}/{} fps, {}, {}-bit",
532 video_info.width,
533 video_info.height,
534 video_info.time_base.den,
535 video_info.time_base.num,
536 video_info.chroma_sampling,
537 video_info.bit_depth
538 );
539 info!("Encoding settings: {}", cli.enc);
540
541 if tiling.tile_count() == 1 {
542 info!("Using 1 tile");
543 } else {
544 info!(
545 "Using {} tiles ({}x{})",
546 tiling.tile_count(),
547 tiling.cols,
548 tiling.rows
549 );
550 }
551 }
552
553 let progress = ProgressInfo::new(
554 Rational { num: video_info.time_base.den, den: video_info.time_base.num },
555 if cli.limit == 0 { None } else { Some(cli.limit) },
556 cli.metrics_enabled,
557 );
558
559 for _ in 0..cli.skip {
560 match y4m_dec.read_frame() {
561 Ok(f) => f,
562 Err(_) => {
563 return Err(CliError::new("Skipped more frames than in the input"))
564 }
565 };
566 }
567
568 let source = Source::new(cli.limit, y4m_dec);
569
570 if video_info.bit_depth == 8 && !cli.force_highbitdepth {
571 do_encode::<u8, y4m::Decoder<Box<dyn Read + Send>>>(
572 cfg,
573 cli.verbose,
574 progress,
575 &mut *cli.io.output,
576 source,
577 pass1file,
578 pass2file,
579 y4m_enc,
580 cli.metrics_enabled,
581 )?
582 } else {
583 do_encode::<u16, y4m::Decoder<Box<dyn Read + Send>>>(
584 cfg,
585 cli.verbose,
586 progress,
587 &mut *cli.io.output,
588 source,
589 pass1file,
590 pass2file,
591 y4m_enc,
592 cli.metrics_enabled,
593 )?
594 }
595 if cli.benchmark {
596 print_rusage();
597 }
598
599 Ok(())
600 }