Skip to content

Commit 493395b

Browse files
committed
Make sure Config can be instantiated only through new()
In preparation of the rate control API update. Add Config::new(), Config::with_encoder_config() and Config::with_threads().
1 parent 02018bf commit 493395b

File tree

5 files changed

+133
-108
lines changed

5 files changed

+133
-108
lines changed

src/api/config/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,34 @@ pub struct Config {
9696
pub enc: EncoderConfig,
9797
/// The number of threads in the threadpool.
9898
pub threads: usize,
99+
_guard: (),
100+
}
101+
102+
impl Config {
103+
/// Create a default configuration
104+
///
105+
/// same as Default::default()
106+
pub fn new() -> Self {
107+
Config::default()
108+
}
109+
110+
/// Set the encoder configuration
111+
///
112+
/// EncoderConfig contains the settings impacting the
113+
/// codec features used in the produced bitstream.
114+
pub fn with_encoder_config(mut self, enc: EncoderConfig) -> Self {
115+
self.enc = enc;
116+
self
117+
}
118+
119+
/// Set the number of workers in the threadpool
120+
///
121+
/// The threadpool is shared across all the different parallel
122+
/// components in the encoder.
123+
pub fn with_threads(mut self, threads: usize) -> Self {
124+
self.threads = threads;
125+
self
126+
}
99127
}
100128

101129
fn check_tile_log2(n: usize) -> bool {

src/api/test.rs

Lines changed: 101 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn setup_encoder<T: Pixel>(
3535
enc.speed_settings.no_scene_detection = no_scene_detection;
3636
enc.rdo_lookahead_frames = rdo_lookahead_frames;
3737

38-
let cfg = Config { enc, threads: 0 };
38+
let cfg = Config::new().with_encoder_config(enc).with_threads(1);
3939

4040
cfg.new_context().unwrap()
4141
}
@@ -1724,60 +1724,58 @@ fn rdo_lookahead_frames_overflow() {
17241724

17251725
#[test]
17261726
fn log_q_exp_overflow() {
1727-
let config = Config {
1728-
enc: EncoderConfig {
1729-
width: 16,
1730-
height: 16,
1731-
bit_depth: 8,
1732-
chroma_sampling: ChromaSampling::Cs420,
1733-
chroma_sample_position: ChromaSamplePosition::Unknown,
1734-
pixel_range: PixelRange::Limited,
1735-
color_description: None,
1736-
mastering_display: None,
1737-
content_light: None,
1738-
enable_timing_info: false,
1739-
still_picture: false,
1740-
error_resilient: false,
1741-
switch_frame_interval: 0,
1742-
time_base: Rational { num: 1, den: 25 },
1743-
min_key_frame_interval: 12,
1744-
max_key_frame_interval: 240,
1745-
reservoir_frame_delay: None,
1746-
low_latency: false,
1747-
quantizer: 100,
1748-
min_quantizer: 64,
1749-
bitrate: 1,
1750-
tune: Tune::Psychovisual,
1751-
tile_cols: 0,
1752-
tile_rows: 0,
1753-
tiles: 0,
1754-
rdo_lookahead_frames: 40,
1755-
speed_settings: SpeedSettings {
1756-
partition_range: PartitionRange::new(
1757-
BlockSize::BLOCK_64X64,
1758-
BlockSize::BLOCK_64X64,
1759-
),
1760-
multiref: false,
1761-
fast_deblock: true,
1762-
reduced_tx_set: true,
1763-
tx_domain_distortion: true,
1764-
tx_domain_rate: false,
1765-
encode_bottomup: false,
1766-
rdo_tx_decision: false,
1767-
prediction_modes: PredictionModesSetting::Simple,
1768-
include_near_mvs: false,
1769-
no_scene_detection: true,
1770-
fast_scene_detection: false,
1771-
diamond_me: true,
1772-
cdef: true,
1773-
lrf: true,
1774-
use_satd_subpel: false,
1775-
non_square_partition: false,
1776-
..Default::default()
1777-
},
1727+
let enc = EncoderConfig {
1728+
width: 16,
1729+
height: 16,
1730+
bit_depth: 8,
1731+
chroma_sampling: ChromaSampling::Cs420,
1732+
chroma_sample_position: ChromaSamplePosition::Unknown,
1733+
pixel_range: PixelRange::Limited,
1734+
color_description: None,
1735+
mastering_display: None,
1736+
content_light: None,
1737+
enable_timing_info: false,
1738+
still_picture: false,
1739+
error_resilient: false,
1740+
switch_frame_interval: 0,
1741+
time_base: Rational { num: 1, den: 25 },
1742+
min_key_frame_interval: 12,
1743+
max_key_frame_interval: 240,
1744+
reservoir_frame_delay: None,
1745+
low_latency: false,
1746+
quantizer: 100,
1747+
min_quantizer: 64,
1748+
bitrate: 1,
1749+
tune: Tune::Psychovisual,
1750+
tile_cols: 0,
1751+
tile_rows: 0,
1752+
tiles: 0,
1753+
rdo_lookahead_frames: 40,
1754+
speed_settings: SpeedSettings {
1755+
partition_range: PartitionRange::new(
1756+
BlockSize::BLOCK_64X64,
1757+
BlockSize::BLOCK_64X64,
1758+
),
1759+
multiref: false,
1760+
fast_deblock: true,
1761+
reduced_tx_set: true,
1762+
tx_domain_distortion: true,
1763+
tx_domain_rate: false,
1764+
encode_bottomup: false,
1765+
rdo_tx_decision: false,
1766+
prediction_modes: PredictionModesSetting::Simple,
1767+
include_near_mvs: false,
1768+
no_scene_detection: true,
1769+
fast_scene_detection: false,
1770+
diamond_me: true,
1771+
cdef: true,
1772+
lrf: true,
1773+
use_satd_subpel: false,
1774+
non_square_partition: false,
1775+
..Default::default()
17781776
},
1779-
threads: 1,
17801777
};
1778+
let config = Config::new().with_encoder_config(enc).with_threads(1);
17811779

17821780
let mut ctx: Context<u8> = config.new_context().unwrap();
17831781
for _ in 0..2 {
@@ -1791,60 +1789,58 @@ fn log_q_exp_overflow() {
17911789

17921790
#[test]
17931791
fn guess_frame_subtypes_assert() {
1794-
let config = Config {
1795-
enc: EncoderConfig {
1796-
width: 16,
1797-
height: 16,
1798-
bit_depth: 8,
1799-
chroma_sampling: ChromaSampling::Cs420,
1800-
chroma_sample_position: ChromaSamplePosition::Unknown,
1801-
pixel_range: PixelRange::Limited,
1802-
color_description: None,
1803-
mastering_display: None,
1804-
content_light: None,
1805-
enable_timing_info: false,
1806-
still_picture: false,
1807-
error_resilient: false,
1808-
switch_frame_interval: 0,
1809-
time_base: Rational { num: 1, den: 25 },
1810-
min_key_frame_interval: 0,
1811-
max_key_frame_interval: 1,
1812-
reservoir_frame_delay: None,
1813-
low_latency: false,
1814-
quantizer: 100,
1815-
min_quantizer: 0,
1816-
bitrate: 16384,
1817-
tune: Tune::Psychovisual,
1818-
tile_cols: 0,
1819-
tile_rows: 0,
1820-
tiles: 0,
1821-
rdo_lookahead_frames: 40,
1822-
speed_settings: SpeedSettings {
1823-
partition_range: PartitionRange::new(
1824-
BlockSize::BLOCK_64X64,
1825-
BlockSize::BLOCK_64X64,
1826-
),
1827-
multiref: false,
1828-
fast_deblock: true,
1829-
reduced_tx_set: true,
1830-
tx_domain_distortion: true,
1831-
tx_domain_rate: false,
1832-
encode_bottomup: false,
1833-
rdo_tx_decision: false,
1834-
prediction_modes: PredictionModesSetting::Simple,
1835-
include_near_mvs: false,
1836-
no_scene_detection: true,
1837-
fast_scene_detection: false,
1838-
diamond_me: true,
1839-
cdef: true,
1840-
lrf: true,
1841-
use_satd_subpel: false,
1842-
non_square_partition: false,
1843-
..Default::default()
1844-
},
1792+
let enc = EncoderConfig {
1793+
width: 16,
1794+
height: 16,
1795+
bit_depth: 8,
1796+
chroma_sampling: ChromaSampling::Cs420,
1797+
chroma_sample_position: ChromaSamplePosition::Unknown,
1798+
pixel_range: PixelRange::Limited,
1799+
color_description: None,
1800+
mastering_display: None,
1801+
content_light: None,
1802+
enable_timing_info: false,
1803+
still_picture: false,
1804+
error_resilient: false,
1805+
switch_frame_interval: 0,
1806+
time_base: Rational { num: 1, den: 25 },
1807+
min_key_frame_interval: 0,
1808+
max_key_frame_interval: 1,
1809+
reservoir_frame_delay: None,
1810+
low_latency: false,
1811+
quantizer: 100,
1812+
min_quantizer: 0,
1813+
bitrate: 16384,
1814+
tune: Tune::Psychovisual,
1815+
tile_cols: 0,
1816+
tile_rows: 0,
1817+
tiles: 0,
1818+
rdo_lookahead_frames: 40,
1819+
speed_settings: SpeedSettings {
1820+
partition_range: PartitionRange::new(
1821+
BlockSize::BLOCK_64X64,
1822+
BlockSize::BLOCK_64X64,
1823+
),
1824+
multiref: false,
1825+
fast_deblock: true,
1826+
reduced_tx_set: true,
1827+
tx_domain_distortion: true,
1828+
tx_domain_rate: false,
1829+
encode_bottomup: false,
1830+
rdo_tx_decision: false,
1831+
prediction_modes: PredictionModesSetting::Simple,
1832+
include_near_mvs: false,
1833+
no_scene_detection: true,
1834+
fast_scene_detection: false,
1835+
diamond_me: true,
1836+
cdef: true,
1837+
lrf: true,
1838+
use_satd_subpel: false,
1839+
non_square_partition: false,
1840+
..Default::default()
18451841
},
1846-
threads: 1,
18471842
};
1843+
let config = Config::new().with_encoder_config(enc).with_threads(1);
18481844

18491845
let mut ctx: Context<u8> = config.new_context().unwrap();
18501846
ctx.send_frame(ctx.new_frame()).unwrap();

src/bin/rav1e.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ fn run() -> Result<(), error::CliError> {
398398
cli.enc.time_base = video_info.time_base;
399399
}
400400

401-
let cfg = Config { enc: cli.enc, threads: cli.threads };
401+
let cfg =
402+
Config::new().with_encoder_config(cli.enc).with_threads(cli.threads);
402403

403404
#[cfg(feature = "serialize")]
404405
{

src/capi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ pub unsafe extern fn rav1e_data_unref(data: *mut Data) {
349349
/// Create a RaConfig filled with default parameters.
350350
#[no_mangle]
351351
pub unsafe extern fn rav1e_config_default() -> *mut Config {
352-
let cfg = rav1e::Config { enc: rav1e::EncoderConfig::default(), threads: 0 };
352+
let cfg = rav1e::Config::default();
353353

354354
let c = Box::new(Config { cfg });
355355

src/test_encode_decode/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ fn setup_encoder<T: Pixel>(
192192
enc.tile_rows = 1 << tile_rows_log2;
193193
enc.still_picture = still_picture;
194194

195-
let cfg = Config { enc, threads: 0 };
195+
let cfg = Config::new().with_encoder_config(enc);
196196

197197
cfg.new_context().unwrap()
198198
}

0 commit comments

Comments
 (0)