diff --git a/src/api/config/encoder.rs b/src/api/config/encoder.rs index afdbb5e6fd..fec9c75103 100644 --- a/src/api/config/encoder.rs +++ b/src/api/config/encoder.rs @@ -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, @@ -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, diff --git a/src/api/test.rs b/src/api/test.rs index 131cd8ff9f..23ea4f9e21 100644 --- a/src/api/test.rs +++ b/src/api/test.rs @@ -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, @@ -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, diff --git a/src/bin/common.rs b/src/bin/common.rs index 795ee1a9ea..b25c993c2a 100644 --- a/src/bin/common.rs +++ b/src/bin/common.rs @@ -240,6 +240,20 @@ pub fn parse_cli() -> Result { .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\ @@ -655,6 +669,9 @@ fn parse_config(matches: &ArgMatches<'_>) -> Result { 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 { diff --git a/src/capi.rs b/src/capi.rs index 52ae062cd5..c71fcc4ffb 100644 --- a/src/capi.rs +++ b/src/capi.rs @@ -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(|_| ())?) diff --git a/src/encoder.rs b/src/encoder.rs index b893c5224c..e2c408e86c 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -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); @@ -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, diff --git a/src/header.rs b/src/header.rs index 9ee81b7d57..cd6ec39d48 100644 --- a/src/header.rs +++ b/src/header.rs @@ -868,10 +868,8 @@ impl UncompressedHeader for BitWriter { fn write_frame_size( &mut self, fi: &FrameInvariants, ) -> 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)?; @@ -884,10 +882,8 @@ impl UncompressedHeader for BitWriter { fn write_frame_size_override( &mut self, fi: &FrameInvariants, ) -> 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)?;