Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] init Add support for Config::validate #1718

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/api/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,40 @@ pub enum InvalidConfig {
)]
InvalidReservoirFrameDelay(i32),

/// Bit Depth is Invalid
#[error(display = "invalid bit depth {} (expected 8, 10 or 12)", _0)]
InvalidBitDepth(u8),

/// Chroma Sampling is Invalid
#[error(
display = "invalid chroma sampling {} (expected 420, 422, 444 or 400)",
_0
)]
InvalidChromaSampling(i32),

/// Chroma Sampling Position is Invalid
#[error(
display = "invalid chroma sampling position {} (expected 0, 1, 2)",
_0
)]
InvalidChromaSamplingPosition(i32),

/// Pixel Range is Invalid
#[error(display = "invalid pixel range {} (expected 0, 1)", _0)]
InvalidPixelRange(i32),

/// Color Description is Invalid
#[error(display = "invalid Color Description")]
InvalidColorDescription(),

/// Color Description is Invalid
#[error(display = "invalid content light")]
InvalidContentLight(),

/// Color Description is Invalid
#[error(display = "invalid mastering display")]
InvalidMasteringDisplay(),

// This variant prevents people from exhaustively matching on this enum,
// which allows us to add more variants without it being a breaking change.
// This can be replaced with #[non_exhaustive] when it's stable:
Expand Down
24 changes: 22 additions & 2 deletions src/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use libc::size_t;
use num_derive::*;
use num_traits::cast::FromPrimitive;

use crate::api::config::InvalidConfig;
use crate::prelude as rav1e;

type PixelRange = rav1e::PixelRange;
Expand Down Expand Up @@ -114,6 +115,7 @@ impl From<Option<rav1e::EncoderStatus>> for EncoderStatus {
/// Use rav1e_config_unref() to free its memory.
pub struct Config {
cfg: rav1e::Config,
last_err: Option<rav1e::InvalidConfig>,
}

enum EncContext {
Expand Down Expand Up @@ -257,7 +259,7 @@ pub unsafe extern fn rav1e_data_unref(data: *mut Data) {
pub unsafe extern fn rav1e_config_default() -> *mut Config {
let cfg = rav1e::Config { enc: rav1e::EncoderConfig::default(), threads: 0 };

let c = Box::new(Config { cfg });
let c = Box::new(Config { cfg, last_err: None });

Box::into_raw(c)
}
Expand All @@ -269,7 +271,14 @@ pub unsafe extern fn rav1e_config_default() -> *mut Config {
pub unsafe extern fn rav1e_config_set_time_base(
cfg: *mut Config, time_base: Rational,
) {
(*cfg).cfg.enc.time_base = time_base
let temp_time_base = (*cfg).cfg.enc.time_base;

(*cfg).cfg.enc.time_base = time_base;

if let Err(err) = (*cfg).cfg.validate() {
(*cfg).last_err = Some(err);
(*cfg).cfg.enc.time_base = temp_time_base;
}
}

/// Set pixel format of the stream.
Expand All @@ -285,26 +294,34 @@ pub unsafe extern fn rav1e_config_set_pixel_format(
chroma_pos: ChromaSamplePosition, pixel_range: PixelRange,
) -> c_int {
if bit_depth != 8 && bit_depth != 10 && bit_depth != 12 {
(*cfg).last_err = Some(rav1e::InvalidConfig::InvalidBitDepth(bit_depth));
return -1;
}
(*cfg).cfg.enc.bit_depth = bit_depth as usize;

let subsampling_val =
std::mem::transmute::<ChromaSampling, i32>(subsampling);
if ChromaSampling::from_i32(subsampling_val).is_none() {
(*cfg).last_err =
Some(rav1e::InvalidConfig::InvalidChromaSampling(subsampling_val));
return -1;
}
(*cfg).cfg.enc.chroma_sampling = subsampling;

let chroma_pos_val =
std::mem::transmute::<ChromaSamplePosition, i32>(chroma_pos);
if ChromaSamplePosition::from_i32(chroma_pos_val).is_none() {
(*cfg).last_err = Some(
rav1e::InvalidConfig::InvalidChromaSamplingPosition(chroma_pos_val),
);
return -1;
}
(*cfg).cfg.enc.chroma_sample_position = chroma_pos;

let pixel_range_val = std::mem::transmute::<PixelRange, i32>(pixel_range);
if PixelRange::from_i32(pixel_range_val).is_none() {
(*cfg).last_err =
Some(rav1e::InvalidConfig::InvalidPixelRange(pixel_range_val));
return -1;
}
(*cfg).cfg.enc.pixel_range = pixel_range;
Expand Down Expand Up @@ -333,6 +350,7 @@ pub unsafe extern fn rav1e_config_set_color_description(
if (*cfg).cfg.enc.color_description.is_some() {
0
} else {
(*cfg).last_err = Some(rav1e::InvalidConfig::InvalidColorDescription());
-1
}
}
Expand All @@ -353,6 +371,7 @@ pub unsafe extern fn rav1e_config_set_content_light(
if (*cfg).cfg.enc.content_light.is_some() {
0
} else {
(*cfg).last_err = Some(rav1e::InvalidConfig::InvalidContentLight());
-1
}
}
Expand Down Expand Up @@ -381,6 +400,7 @@ pub unsafe extern fn rav1e_config_set_mastering_display(
if (*cfg).cfg.enc.mastering_display.is_some() {
0
} else {
(*cfg).last_err = Some(rav1e::InvalidConfig::InvalidMasteringDisplay());
-1
}
}
Expand Down