Debian Codegraph @

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