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