rav1e.rs 2.54 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
15
mod common;
use common::*;
Raphaël Zumer's avatar
Raphaël Zumer committed
16
use rav1e::*;
17

18
19
use std::io;
use std::io::Write;
Guillaume Martres's avatar
Guillaume Martres committed
20
21

fn main() {
22
23
  let mut cli = parse_cli();
  let mut y4m_dec = y4m::decode(&mut cli.io.input).unwrap();
Michael Bebenita's avatar
Michael Bebenita committed
24
25
26
  let width = y4m_dec.get_width();
  let height = y4m_dec.get_height();
  let framerate = y4m_dec.get_framerate();
27
  let color_space = y4m_dec.get_colorspace();
Luca Barbato's avatar
Luca Barbato committed
28

29
  let mut y4m_enc = match cli.io.rec.as_mut() {
30
31
32
33
34
35
    Some(rec) => Some(
      y4m::encode(width, height, framerate)
        .with_colorspace(color_space)
        .write_header(rec)
        .unwrap()
    ),
Michael Bebenita's avatar
Michael Bebenita committed
36
37
    None => None
  };
38

39
  let (chroma_sampling, chroma_sample_position) = map_y4m_color_space(color_space);
40

Luca Barbato's avatar
Luca Barbato committed
41
42
  let bit_depth = color_space.get_bit_depth();

Kyle Siefring's avatar
Kyle Siefring committed
43
  let cfg = Config {
44
    frame_info: FrameInfo { width, height, bit_depth, chroma_sampling, chroma_sample_position },
Luca Barbato's avatar
Luca Barbato committed
45
    timebase: Rational::new(framerate.den as u64, framerate.num as u64),
46
    enc: cli.enc
Kyle Siefring's avatar
Kyle Siefring committed
47
  };
Luca Barbato's avatar
Luca Barbato committed
48
49
50

  let mut ctx = cfg.new_context();

51
52
53
54
55
  let stderr = io::stderr();
  let mut err = stderr.lock();

  let _ = writeln!(err, "{}x{} @ {}/{} fps", width, height, framerate.num, framerate.den);

Michael Bebenita's avatar
Michael Bebenita committed
56
  write_ivf_header(
57
    &mut cli.io.output,
Michael Bebenita's avatar
Michael Bebenita committed
58
59
60
61
62
    width,
    height,
    framerate.num,
    framerate.den
  );
Guillaume Martres's avatar
Guillaume Martres committed
63

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

70
71
  ctx.set_frames_to_be_coded(cli.limit as u64);

72
73
  loop {
    match process_frame(&mut ctx, &mut cli.io.output, &mut y4m_dec, y4m_enc.as_mut()) {
74
75
76
77
78
79
80
81
82
      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)
          };
        }
83
84
85
      },
      Err(_) => break,
    };
Luca Barbato's avatar
Luca Barbato committed
86

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

91
    cli.io.output.flush().unwrap();
Michael Bebenita's avatar
Michael Bebenita committed
92
  }
93
94

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