rav1e.rs 2.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
// Copyright (c) 2017-2018, 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.

10
extern crate clap;
Guillaume Martres's avatar
Guillaume Martres committed
11
12
13
extern crate rav1e;
extern crate y4m;

14
mod common;
15
mod decoder;
16
use common::*;
Raphaël Zumer's avatar
Raphaël Zumer committed
17
use rav1e::*;
18

19
20
use std::io;
use std::io::Write;
21
use decoder::Decoder;
Guillaume Martres's avatar
Guillaume Martres committed
22
23

fn main() {
24
25
  let mut cli = parse_cli();
  let mut y4m_dec = y4m::decode(&mut cli.io.input).unwrap();
26
  let video_info = y4m_dec.get_video_details();
27
  let mut y4m_enc = match cli.io.rec.as_mut() {
28
    Some(rec) => Some(
29
30
31
32
      y4m::encode(
        video_info.width,
        video_info.height,
        y4m::Ratio::new(video_info.framerate.num as usize, video_info.framerate.den as usize)
33
      ).with_colorspace(y4m_dec.get_colorspace())
34
35
36
        .write_header(rec)
        .unwrap()
    ),
Michael Bebenita's avatar
Michael Bebenita committed
37
38
    None => None
  };
39

Kyle Siefring's avatar
Kyle Siefring committed
40
  let cfg = Config {
41
    video_info,
42
    enc: cli.enc
Kyle Siefring's avatar
Kyle Siefring committed
43
  };
Luca Barbato's avatar
Luca Barbato committed
44
45
46

  let mut ctx = cfg.new_context();

47
48
49
  let stderr = io::stderr();
  let mut err = stderr.lock();

50
51
52
53
54
55
56
57
  let _ = writeln!(
    err,
    "{}x{} @ {}/{} fps",
    video_info.width,
    video_info.height,
    video_info.framerate.num,
    video_info.framerate.den
  );
58

Michael Bebenita's avatar
Michael Bebenita committed
59
  write_ivf_header(
60
    &mut cli.io.output,
61
62
63
64
    video_info.width,
    video_info.height,
    video_info.framerate.num as usize,
    video_info.framerate.den as usize
Michael Bebenita's avatar
Michael Bebenita committed
65
  );
Guillaume Martres's avatar
Guillaume Martres committed
66

67
  let mut progress = ProgressInfo::new(
68
    video_info.framerate,
69
70
    if cli.limit == 0 { None } else { Some(cli.limit) },
      cfg.enc.show_psnr
71
  );
Luca Barbato's avatar
Luca Barbato committed
72

73
74
  ctx.set_frames_to_be_coded(cli.limit as u64);

75
76
  loop {
    match process_frame(&mut ctx, &mut cli.io.output, &mut y4m_dec, y4m_enc.as_mut()) {
77
78
79
80
81
82
83
84
85
      Ok(frame_info) => {
        for frame in frame_info {
          progress.add_frame(frame);
          let _ = if cli.verbose {
            writeln!(err, "{} - {}", frame, progress)
          } else {
            write!(err, "\r{}                    ", progress)
          };
        }
86
87
88
      },
      Err(_) => break,
    };
Luca Barbato's avatar
Luca Barbato committed
89

90
    if !ctx.needs_more_frames(progress.frames_encoded() as u64) {
Michael Bebenita's avatar
Michael Bebenita committed
91
      break;
Guillaume Martres's avatar
Guillaume Martres committed
92
    }
Luca Barbato's avatar
Luca Barbato committed
93

94
    cli.io.output.flush().unwrap();
Michael Bebenita's avatar
Michael Bebenita committed
95
  }
96
97

  let _ = write!(err, "\n{}\n", progress.print_stats());
Guillaume Martres's avatar
Guillaume Martres committed
98
}