Skip to content

Commit

Permalink
Support max_width/max_height options.
Browse files Browse the repository at this point in the history
Fixes xiph#2467
  • Loading branch information
tdaede committed Aug 10, 2020
1 parent a811e9e commit 224f557
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/api/config/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub struct EncoderConfig {
pub width: usize,
/// Height of the frames in pixels.
pub height: usize,
/// Maximum width of the frames in pixels (for seq header)
pub max_width: usize,
/// Maximum height of the frames in pixels (for seq header)
pub max_height: usize,
/// Video time base.
pub time_base: Rational,

Expand Down Expand Up @@ -129,7 +133,8 @@ impl EncoderConfig {
EncoderConfig {
width: 640,
height: 480,

max_width: 0,
max_height: 0,
bit_depth: 8,
chroma_sampling: ChromaSampling::Cs420,
chroma_sample_position: ChromaSamplePosition::Unknown,
Expand Down
7 changes: 7 additions & 0 deletions src/api/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ impl Config {
return Err(InvalidHeight(config.height));
}

if config.max_width != 0 && config.width > config.max_width {
return Err(InvalidWidth(config.width));
}
if config.max_height != 0 && config.height > config.max_height {
return Err(InvalidHeight(config.height));
}

if config.rdo_lookahead_frames > MAX_RDO_LOOKAHEAD_FRAMES
|| config.rdo_lookahead_frames < 1
{
Expand Down
4 changes: 4 additions & 0 deletions src/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,8 @@ fn log_q_exp_overflow() {
let enc = EncoderConfig {
width: 16,
height: 16,
max_width: 0,
max_height: 0,
bit_depth: 8,
chroma_sampling: ChromaSampling::Cs420,
chroma_sample_position: ChromaSamplePosition::Unknown,
Expand Down Expand Up @@ -1866,6 +1868,8 @@ fn guess_frame_subtypes_assert() {
let enc = EncoderConfig {
width: 16,
height: 16,
max_width: 0,
max_height: 0,
bit_depth: 8,
chroma_sampling: ChromaSampling::Cs420,
chroma_sample_position: ChromaSamplePosition::Unknown,
Expand Down
17 changes: 17 additions & 0 deletions src/bin/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ pub fn parse_cli() -> Result<CliOptions, CliError> {
.takes_value(true)
.default_value("0")
)
.arg(
Arg::with_name("MAX_WIDTH")
.help("Maximum width coded in the sequence header. 0 uses the input video width.")
.long("max-width")
.takes_value(true)
.default_value("0")
)
.arg(
Arg::with_name("MAX_HEIGHT")
.help("Maximum height coded in the sequence header. 0 uses the input video width.")
.long("max-height")
.takes_value(true)
.default_value("0")
)
.arg(
Arg::with_name("TILES")
.help("Number of tiles. Tile-cols and tile-rows are overridden\n\
Expand Down Expand Up @@ -655,6 +669,9 @@ fn parse_config(matches: &ArgMatches<'_>) -> Result<EncoderConfig, CliError> {
cfg.tile_cols = matches.value_of("TILE_COLS").unwrap().parse().unwrap();
cfg.tile_rows = matches.value_of("TILE_ROWS").unwrap().parse().unwrap();

cfg.max_width = matches.value_of("MAX_WIDTH").unwrap().parse().unwrap();
cfg.max_height = matches.value_of("MAX_HEIGHT").unwrap().parse().unwrap();

cfg.tiles = matches.value_of("TILES").unwrap().parse().unwrap();

if cfg.tile_cols > 64 || cfg.tile_rows > 64 {
Expand Down
6 changes: 6 additions & 0 deletions src/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,12 @@ unsafe fn option_match(
match key {
"width" => enc.width = check_frame_size(value.parse().map_err(|_| ()))?,
"height" => enc.height = check_frame_size(value.parse().map_err(|_| ()))?,
"max_width" => {
enc.max_width = check_frame_size(value.parse().map_err(|_| ()))?
}
"max_height" => {
enc.max_height = check_frame_size(value.parse().map_err(|_| ()))?
}
"speed" => {
enc.speed_settings =
rav1e::SpeedSettings::from_preset(value.parse().map_err(|_| ())?)
Expand Down
12 changes: 8 additions & 4 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ pub struct Sequence {

impl Sequence {
pub fn new(config: &EncoderConfig) -> Sequence {
let width_bits = 32 - (config.width as u32).leading_zeros();
let height_bits = 32 - (config.height as u32).leading_zeros();
let max_width =
if config.max_width > 0 { config.max_width } else { config.width };
let max_height =
if config.max_height > 0 { config.max_height } else { config.height };
let width_bits = 32 - (max_width as u32).leading_zeros();
let height_bits = 32 - (max_height as u32).leading_zeros();
assert!(width_bits <= 16);
assert!(height_bits <= 16);

Expand Down Expand Up @@ -223,8 +227,8 @@ impl Sequence {
color_description: config.color_description,
mastering_display: config.mastering_display,
content_light: config.content_light,
max_frame_width: config.width as u32,
max_frame_height: config.height as u32,
max_frame_width: config.max_width as u32,
max_frame_height: config.max_height as u32,
frame_id_numbers_present_flag: false,
frame_id_length: FRAME_ID_LENGTH,
delta_frame_id_length: DELTA_FRAME_ID_LENGTH,
Expand Down
12 changes: 4 additions & 8 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,10 +868,8 @@ impl<W: io::Write> UncompressedHeader for BitWriter<W, BigEndian> {
fn write_frame_size<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()> {
// width_bits and height_bits will have to be moved to the sequence header OBU
// when we add support for it.
let width_bits = 32 - (fi.width as u32).leading_zeros();
let height_bits = 32 - (fi.height as u32).leading_zeros();
let width_bits = fi.sequence.num_bits_width;
let height_bits = fi.sequence.num_bits_height;
assert!(width_bits <= 16);
assert!(height_bits <= 16);
self.write(4, width_bits - 1)?;
Expand All @@ -884,10 +882,8 @@ impl<W: io::Write> UncompressedHeader for BitWriter<W, BigEndian> {
fn write_frame_size_override<T: Pixel>(
&mut self, fi: &FrameInvariants<T>,
) -> io::Result<()> {
// width_bits and height_bits will have to be moved to the sequence header OBU
// when we add support for it.
let width_bits = 32 - (fi.width as u32).leading_zeros();
let height_bits = 32 - (fi.height as u32).leading_zeros();
let width_bits = fi.sequence.num_bits_width;
let height_bits = fi.sequence.num_bits_height;
assert!(width_bits <= 16);
assert!(height_bits <= 16);
self.write(width_bits, (fi.width - 1) as u16)?;
Expand Down

0 comments on commit 224f557

Please sign in to comment.