rav1repl.rs 2.87 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 14
extern crate rustyline;
extern crate y4m;

extern crate rav1e;
15 16 17 18

mod common;
use common::*;

Guillaume Martres's avatar
Guillaume Martres committed
19 20 21 22 23 24
use rav1e::*;

use rustyline::error::ReadlineError;
use rustyline::Editor;

fn main() {
Luca Barbato's avatar
Luca Barbato committed
25
  let (mut io, enc) = EncoderConfig::from_cli();
26
  let mut y4m_dec = y4m::decode(&mut io.input).unwrap();
Michael Bebenita's avatar
Michael Bebenita committed
27 28 29
  let width = y4m_dec.get_width();
  let height = y4m_dec.get_height();
  let framerate = y4m_dec.get_framerate();
Luca Barbato's avatar
Luca Barbato committed
30 31
  let color_space = y4m_dec.get_colorspace();

32 33 34
  let mut y4m_enc = match io.rec.as_mut() {
    Some(rec) =>
      Some(y4m::encode(width, height, framerate).write_header(rec).unwrap()),
Michael Bebenita's avatar
Michael Bebenita committed
35 36 37
    None => None
  };
  write_ivf_header(
38
    &mut io.output,
Luca Barbato's avatar
Luca Barbato committed
39 40
    width,
    height,
Michael Bebenita's avatar
Michael Bebenita committed
41 42 43
    framerate.num,
    framerate.den
  );
Guillaume Martres's avatar
Guillaume Martres committed
44

Luca Barbato's avatar
Luca Barbato committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  let chroma_sampling = match color_space {
    y4m::Colorspace::C420 |
    y4m::Colorspace::C420jpeg |
    y4m::Colorspace::C420paldv |
    y4m::Colorspace::C420mpeg2 |
    y4m::Colorspace::C420p10 |
    y4m::Colorspace::C420p12 => ChromaSampling::Cs420,
    y4m::Colorspace::C422 |
    y4m::Colorspace::C422p10 |
    y4m::Colorspace::C422p12 => ChromaSampling::Cs422,
    y4m::Colorspace::C444 |
    y4m::Colorspace::C444p10 |
    y4m::Colorspace::C444p12 => ChromaSampling::Cs444,
    _ => {
        panic!("Chroma sampling unknown for the specified color space.")
    }
  };
  let bit_depth = color_space.get_bit_depth();

  let cfg = Config {width, height, bit_depth, chroma_sampling, timebase: Ratio::new(framerate.den, framerate.num), enc };
  let mut ctx = cfg.new_context();

Michael Bebenita's avatar
Michael Bebenita committed
67 68 69 70 71 72 73 74 75 76
  let mut rl = Editor::<()>::new();
  let _ = rl.load_history(".rav1e-history");
  let mut last_rec: Option<Frame> = None;
  loop {
    let readline = rl.readline(">> ");
    match readline {
      Ok(line) => {
        rl.add_history_entry(&line);
        match line.split_whitespace().next() {
          Some("process_frame") => {
Luca Barbato's avatar
Luca Barbato committed
77 78
            if !process_frame(
              &mut ctx,
79
              &mut io.output,
Michael Bebenita's avatar
Michael Bebenita committed
80
              &mut y4m_dec,
Luca Barbato's avatar
Luca Barbato committed
81 82
              y4m_enc.as_mut(),
            ) {
Michael Bebenita's avatar
Michael Bebenita committed
83 84
              break;
            }
Luca Barbato's avatar
Luca Barbato committed
85 86

            io.output.flush().unwrap();
Michael Bebenita's avatar
Michael Bebenita committed
87 88 89 90 91 92 93
          }
          Some("quit") => break,
          Some("exit") => break,
          Some(cmd) => {
            println!("Unrecognized command: {:?}", cmd);
          }
          None => {}
Guillaume Martres's avatar
Guillaume Martres committed
94
        }
Michael Bebenita's avatar
Michael Bebenita committed
95 96 97
      }
      Err(ReadlineError::Eof) => break,
      _ => {}
Guillaume Martres's avatar
Guillaume Martres committed
98
    }
Michael Bebenita's avatar
Michael Bebenita committed
99 100
  }
  rl.save_history(".rav1e-history").unwrap();
Guillaume Martres's avatar
Guillaume Martres committed
101
}