Skip to content

Commit 224f557

Browse files
committed
Support max_width/max_height options.
Fixes #2467
1 parent a811e9e commit 224f557

File tree

7 files changed

+52
-13
lines changed

7 files changed

+52
-13
lines changed

src/api/config/encoder.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ pub struct EncoderConfig {
2929
pub width: usize,
3030
/// Height of the frames in pixels.
3131
pub height: usize,
32+
/// Maximum width of the frames in pixels (for seq header)
33+
pub max_width: usize,
34+
/// Maximum height of the frames in pixels (for seq header)
35+
pub max_height: usize,
3236
/// Video time base.
3337
pub time_base: Rational,
3438

@@ -129,7 +133,8 @@ impl EncoderConfig {
129133
EncoderConfig {
130134
width: 640,
131135
height: 480,
132-
136+
max_width: 0,
137+
max_height: 0,
133138
bit_depth: 8,
134139
chroma_sampling: ChromaSampling::Cs420,
135140
chroma_sample_position: ChromaSamplePosition::Unknown,

src/api/config/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ impl Config {
240240
return Err(InvalidHeight(config.height));
241241
}
242242

243+
if config.max_width != 0 && config.width > config.max_width {
244+
return Err(InvalidWidth(config.width));
245+
}
246+
if config.max_height != 0 && config.height > config.max_height {
247+
return Err(InvalidHeight(config.height));
248+
}
249+
243250
if config.rdo_lookahead_frames > MAX_RDO_LOOKAHEAD_FRAMES
244251
|| config.rdo_lookahead_frames < 1
245252
{

src/api/test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,8 @@ fn log_q_exp_overflow() {
18021802
let enc = EncoderConfig {
18031803
width: 16,
18041804
height: 16,
1805+
max_width: 0,
1806+
max_height: 0,
18051807
bit_depth: 8,
18061808
chroma_sampling: ChromaSampling::Cs420,
18071809
chroma_sample_position: ChromaSamplePosition::Unknown,
@@ -1866,6 +1868,8 @@ fn guess_frame_subtypes_assert() {
18661868
let enc = EncoderConfig {
18671869
width: 16,
18681870
height: 16,
1871+
max_width: 0,
1872+
max_height: 0,
18691873
bit_depth: 8,
18701874
chroma_sampling: ChromaSampling::Cs420,
18711875
chroma_sample_position: ChromaSamplePosition::Unknown,

src/bin/common.rs

+17
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,20 @@ pub fn parse_cli() -> Result<CliOptions, CliError> {
240240
.takes_value(true)
241241
.default_value("0")
242242
)
243+
.arg(
244+
Arg::with_name("MAX_WIDTH")
245+
.help("Maximum width coded in the sequence header. 0 uses the input video width.")
246+
.long("max-width")
247+
.takes_value(true)
248+
.default_value("0")
249+
)
250+
.arg(
251+
Arg::with_name("MAX_HEIGHT")
252+
.help("Maximum height coded in the sequence header. 0 uses the input video width.")
253+
.long("max-height")
254+
.takes_value(true)
255+
.default_value("0")
256+
)
243257
.arg(
244258
Arg::with_name("TILES")
245259
.help("Number of tiles. Tile-cols and tile-rows are overridden\n\
@@ -655,6 +669,9 @@ fn parse_config(matches: &ArgMatches<'_>) -> Result<EncoderConfig, CliError> {
655669
cfg.tile_cols = matches.value_of("TILE_COLS").unwrap().parse().unwrap();
656670
cfg.tile_rows = matches.value_of("TILE_ROWS").unwrap().parse().unwrap();
657671

672+
cfg.max_width = matches.value_of("MAX_WIDTH").unwrap().parse().unwrap();
673+
cfg.max_height = matches.value_of("MAX_HEIGHT").unwrap().parse().unwrap();
674+
658675
cfg.tiles = matches.value_of("TILES").unwrap().parse().unwrap();
659676

660677
if cfg.tile_cols > 64 || cfg.tile_rows > 64 {

src/capi.rs

+6
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,12 @@ unsafe fn option_match(
632632
match key {
633633
"width" => enc.width = check_frame_size(value.parse().map_err(|_| ()))?,
634634
"height" => enc.height = check_frame_size(value.parse().map_err(|_| ()))?,
635+
"max_width" => {
636+
enc.max_width = check_frame_size(value.parse().map_err(|_| ()))?
637+
}
638+
"max_height" => {
639+
enc.max_height = check_frame_size(value.parse().map_err(|_| ()))?
640+
}
635641
"speed" => {
636642
enc.speed_settings =
637643
rav1e::SpeedSettings::from_preset(value.parse().map_err(|_| ())?)

src/encoder.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ pub struct Sequence {
182182

183183
impl Sequence {
184184
pub fn new(config: &EncoderConfig) -> Sequence {
185-
let width_bits = 32 - (config.width as u32).leading_zeros();
186-
let height_bits = 32 - (config.height as u32).leading_zeros();
185+
let max_width =
186+
if config.max_width > 0 { config.max_width } else { config.width };
187+
let max_height =
188+
if config.max_height > 0 { config.max_height } else { config.height };
189+
let width_bits = 32 - (max_width as u32).leading_zeros();
190+
let height_bits = 32 - (max_height as u32).leading_zeros();
187191
assert!(width_bits <= 16);
188192
assert!(height_bits <= 16);
189193

@@ -223,8 +227,8 @@ impl Sequence {
223227
color_description: config.color_description,
224228
mastering_display: config.mastering_display,
225229
content_light: config.content_light,
226-
max_frame_width: config.width as u32,
227-
max_frame_height: config.height as u32,
230+
max_frame_width: config.max_width as u32,
231+
max_frame_height: config.max_height as u32,
228232
frame_id_numbers_present_flag: false,
229233
frame_id_length: FRAME_ID_LENGTH,
230234
delta_frame_id_length: DELTA_FRAME_ID_LENGTH,

src/header.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -868,10 +868,8 @@ impl<W: io::Write> UncompressedHeader for BitWriter<W, BigEndian> {
868868
fn write_frame_size<T: Pixel>(
869869
&mut self, fi: &FrameInvariants<T>,
870870
) -> io::Result<()> {
871-
// width_bits and height_bits will have to be moved to the sequence header OBU
872-
// when we add support for it.
873-
let width_bits = 32 - (fi.width as u32).leading_zeros();
874-
let height_bits = 32 - (fi.height as u32).leading_zeros();
871+
let width_bits = fi.sequence.num_bits_width;
872+
let height_bits = fi.sequence.num_bits_height;
875873
assert!(width_bits <= 16);
876874
assert!(height_bits <= 16);
877875
self.write(4, width_bits - 1)?;
@@ -884,10 +882,8 @@ impl<W: io::Write> UncompressedHeader for BitWriter<W, BigEndian> {
884882
fn write_frame_size_override<T: Pixel>(
885883
&mut self, fi: &FrameInvariants<T>,
886884
) -> io::Result<()> {
887-
// width_bits and height_bits will have to be moved to the sequence header OBU
888-
// when we add support for it.
889-
let width_bits = 32 - (fi.width as u32).leading_zeros();
890-
let height_bits = 32 - (fi.height as u32).leading_zeros();
885+
let width_bits = fi.sequence.num_bits_width;
886+
let height_bits = fi.sequence.num_bits_height;
891887
assert!(width_bits <= 16);
892888
assert!(height_bits <= 16);
893889
self.write(width_bits, (fi.width - 1) as u16)?;

0 commit comments

Comments
 (0)