| 1 |
// Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved |
| 2 |
// Copyright (c) 2017-2022, The rav1e contributors. All rights reserved |
| 3 |
// |
| 4 |
// This source code is subject to the terms of the BSD 2 Clause License and |
| 5 |
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 |
// was not distributed with this source code in the LICENSE file, you can |
| 7 |
// obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 |
// Media Patent License 1.0 was not distributed with this source code in the |
| 9 |
// PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 10 |
|
| 11 |
use super::Muxer; |
| 12 |
use crate::error::*; |
| 13 |
use ivf::*; |
| 14 |
use rav1e::prelude::*; |
| 15 |
use std::fs; |
| 16 |
use std::fs::File; |
| 17 |
use std::io; |
| 18 |
use std::io::Write; |
| 19 |
use std::path::Path; |
| 20 |
|
| 21 |
pub struct IvfMuxer { |
| 22 |
output: Box<dyn Write + Send>, |
| 23 |
} |
| 24 |
|
| 25 |
impl Muxer for IvfMuxer { |
| 26 |
fn write_header( |
| 27 |
&mut self, width: usize, height: usize, framerate_num: usize, |
| 28 |
framerate_den: usize, |
| 29 |
) { |
| 30 |
write_ivf_header( |
| 31 |
&mut self.output, |
| 32 |
width, |
| 33 |
height, |
| 34 |
framerate_num, |
| 35 |
framerate_den, |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
#[profiling::function] |
| 40 |
fn write_frame(&mut self, pts: u64, data: &[u8], _frame_type: FrameType) { |
| 41 |
write_ivf_frame(&mut self.output, pts, data); |
| 42 |
} |
| 43 |
|
| 44 |
fn flush(&mut self) -> io::Result<()> { |
| 45 |
self.output.flush() |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
cfg_if::cfg_if! { |
| 50 |
if #[cfg(unix)] { |
| 51 |
use std::os::unix::fs::*; |
| 52 |
fn is_file<P: AsRef<Path>>(path: P) -> bool { |
| 53 |
fs::metadata(path).map(|meta| { |
| 54 |
!meta.file_type().is_char_device() && !meta.file_type().is_socket() |
| 55 |
}).unwrap_or(false) |
| 56 |
} |
| 57 |
} else { |
| 58 |
fn is_file<P: AsRef<Path>>(path: P) -> bool { |
| 59 |
fs::metadata(path).is_ok() |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl IvfMuxer { |
| 65 |
pub fn check_file<P: AsRef<Path>>(path: P) -> Result<(), CliError> { |
| 66 |
if is_file(path.as_ref()) { |
| 67 |
eprint!( |
| 68 |
"File '{}' already exists. Overwrite ? [y/N] ", |
| 69 |
path.as_ref().display() |
| 70 |
); |
| 71 |
io::stdout().flush().unwrap(); |
| 72 |
|
| 73 |
let mut option_input = String::new(); |
| 74 |
io::stdin() |
| 75 |
.read_line(&mut option_input) |
| 76 |
.expect("Failed to read option."); |
| 77 |
|
| 78 |
match option_input.as_str().trim() { |
| 79 |
"y" | "Y" => return Ok(()), |
| 80 |
_ => return Err(CliError::new("Not overwriting, exiting.")), |
| 81 |
}; |
| 82 |
} |
| 83 |
Ok(()) |
| 84 |
} |
| 85 |
|
| 86 |
pub fn open<P: AsRef<Path>>( |
| 87 |
path: P, |
| 88 |
) -> Result<Box<dyn Muxer + Send>, CliError> { |
| 89 |
let ivf = IvfMuxer { |
| 90 |
output: match path.as_ref().to_str() { |
| 91 |
Some("-") => Box::new(std::io::stdout()), |
| 92 |
_ => Box::new( |
| 93 |
File::create(path) |
| 94 |
.map_err(|e| e.context("Cannot open output file"))?, |
| 95 |
), |
| 96 |
}, |
| 97 |
}; |
| 98 |
Ok(Box::new(ivf)) |
| 99 |
} |
| 100 |
} |