image_av1/encoder/
mod.rs

1use std::{
2    fs::{remove_file, File},
3    io::ErrorKind,
4    path::Path,
5};
6
7use image::ImageError;
8use rav1e::{color::ChromaSampling, config::RateControlConfig, data::Rational, EncoderConfig};
9
10use crate::utils::io_error;
11
12mod encoding;
13
14pub struct Av1Encoder {
15    output: File,
16    config: EncoderConfig,
17    rate_control: RateControlConfig,
18}
19
20impl Av1Encoder {
21    /// Export images to file
22    ///
23    /// # Arguments
24    ///
25    /// * `output`:
26    /// * `overwrite`:
27    ///
28    /// returns: Result<Av1Encoder, ImageError>
29    ///
30    /// # Examples
31    ///
32    /// ```
33    /// # use image_av1::Av1Encoder;
34    /// # #[allow(unused_must_use)]
35    /// Av1Encoder::new("output.ivf", true);
36    /// ```
37    pub fn new<P: AsRef<Path>>(output: P, overwrite: bool) -> Result<Self, ImageError> {
38        let path = output.as_ref();
39        if path.exists() {
40            if overwrite {
41                remove_file(path)?;
42            }
43            else {
44                io_error(ErrorKind::AlreadyExists, format!("File already exists: {}", path.display()))?;
45            }
46        }
47        let mut encoder = EncoderConfig::default();
48        encoder.chroma_sampling = ChromaSampling::Cs444;
49        Ok(Self { output: File::create(path)?, config: encoder, rate_control: Default::default() })
50    }
51    pub fn mut_config(&mut self) -> &mut EncoderConfig {
52        &mut self.config
53    }
54    pub fn with_config(mut self, config: EncoderConfig) -> Self {
55        self.config = config;
56        self
57    }
58    pub fn with_size(mut self, width: usize, height: usize) -> Self {
59        self.config.width = width;
60        self.config.height = height;
61        self
62    }
63    pub fn with_fps(mut self, fps: usize) -> Self {
64        self.config.time_base = Rational { num: 1, den: fps as u64 };
65        self
66    }
67    pub fn with_rate_control(mut self, rate_control: RateControlConfig) -> Self {
68        self.rate_control = rate_control;
69        self
70    }
71    // pub fn with_parallel_gops(mut self, config: usize) -> Self {
72    //     self.encoder = self.encoder.with_parallel_gops(config);
73    //     self
74    // }
75}