Skip to content

Commit 9419dd7

Browse files
committed
Store quality as quantizer setting
1 parent e5ab383 commit 9419dd7

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

ravif/src/av1encoder.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ pub struct EncodedImage {
5353
/// Encoder config builder
5454
#[derive(Debug, Clone)]
5555
pub struct Encoder {
56-
/// 0-100 scale
57-
quality: f32,
58-
/// 0-100 scale
59-
alpha_quality: f32,
56+
/// 0-255 scale
57+
quantizer: u8,
58+
/// 0-255 scale
59+
alpha_quantizer: u8,
6060
/// rav1e preset 1 (slow) 10 (fast but crappy)
6161
speed: u8,
6262
/// True if RGBA input has already been premultiplied. It inserts appropriate metadata.
@@ -75,8 +75,8 @@ impl Encoder {
7575
#[must_use]
7676
pub fn new() -> Self {
7777
Self {
78-
quality: 80.,
79-
alpha_quality: 80.,
78+
quantizer: quality_to_quantizer(80.),
79+
alpha_quantizer: quality_to_quantizer(80.),
8080
speed: 5,
8181
premultiplied_alpha: false,
8282
color_space: ColorSpace::YCbCr,
@@ -91,7 +91,7 @@ impl Encoder {
9191
#[must_use]
9292
pub fn with_quality(mut self, quality: f32) -> Self {
9393
assert!(quality >= 1. && quality <= 100.);
94-
self.quality = quality;
94+
self.quantizer = quality_to_quantizer(quality);
9595
self
9696
}
9797

@@ -101,7 +101,7 @@ impl Encoder {
101101
#[must_use]
102102
pub fn with_alpha_quality(mut self, quality: f32) -> Self {
103103
assert!(quality >= 1. && quality <= 100.);
104-
self.alpha_quality = quality;
104+
self.alpha_quantizer = quality_to_quantizer(quality);
105105
self
106106
}
107107

@@ -280,11 +280,6 @@ pub fn encode_raw_planes_10_bit(&self, width: usize, height: usize, planes: impl
280280
}
281281

282282
fn encode_raw_planes<P: rav1e::Pixel + Default>(&self, width: usize, height: usize, planes: impl IntoIterator<Item=[P; 3]> + Send, alpha: Option<impl IntoIterator<Item=P> + Send>, color_pixel_range: PixelRange, matrix_coefficients: MatrixCoefficients, bit_depth: u8) -> Result<EncodedImage, Error> {
283-
284-
// quality setting
285-
let quantizer = quality_to_quantizer(self.quality);
286-
let alpha_quantizer = quality_to_quantizer(self.alpha_quality);
287-
288283
let color_description = Some(ColorDescription {
289284
transfer_characteristics: TransferCharacteristics::SRGB,
290285
color_primaries: ColorPrimaries::BT709, // sRGB-compatible
@@ -299,8 +294,8 @@ fn encode_raw_planes<P: rav1e::Pixel + Default>(&self, width: usize, height: usi
299294
width,
300295
height,
301296
bit_depth: bit_depth.into(),
302-
quantizer,
303-
speed: SpeedTweaks::from_my_preset(self.speed, self.quality as _),
297+
quantizer: self.quantizer.into(),
298+
speed: SpeedTweaks::from_my_preset(self.speed, self.quantizer),
304299
threads,
305300
pixel_range: color_pixel_range,
306301
chroma_sampling: ChromaSampling::Cs444,
@@ -310,8 +305,8 @@ fn encode_raw_planes<P: rav1e::Pixel + Default>(&self, width: usize, height: usi
310305
width,
311306
height,
312307
bit_depth: bit_depth.into(),
313-
quantizer: alpha_quantizer,
314-
speed: SpeedTweaks::from_my_preset(self.speed, self.alpha_quality as _),
308+
quantizer: self.alpha_quantizer.into(),
309+
speed: SpeedTweaks::from_my_preset(self.speed, self.alpha_quantizer),
315310
threads,
316311
pixel_range: PixelRange::Full,
317312
chroma_sampling: ChromaSampling::Cs400,
@@ -384,11 +379,10 @@ fn rgb_to_8_bit_ycbcr(px: rgb::RGB<u8>, matrix: [f32; 3]) -> (u8, u8, u8) {
384379
(y as u8, u as u8, v as u8)
385380
}
386381

387-
fn quality_to_quantizer(quality: f32) -> usize {
382+
fn quality_to_quantizer(quality: f32) -> u8 {
388383
let q = quality / 100.;
389384
let x = if q >= 0.85 { (1. - q) * 3. } else if q > 0.25 { 1. - 0.125 - q * 0.5 } else { 1. - q };
390-
391-
(x * 255.).round().max(0.).min(255.) as usize
385+
(x * 255.).round() as u8
392386
}
393387

394388
#[derive(Debug, Copy, Clone)]
@@ -414,9 +408,9 @@ struct SpeedTweaks {
414408
}
415409

416410
impl SpeedTweaks {
417-
pub fn from_my_preset(speed: u8, quality: u8) -> Self {
418-
let low_quality = quality < 55;
419-
let high_quality = quality > 80;
411+
pub fn from_my_preset(speed: u8, quantizer: u8) -> Self {
412+
let low_quality = quantizer < quality_to_quantizer(55.);
413+
let high_quality = quantizer > quality_to_quantizer(80.);
420414
let max_block_size = if high_quality { 16 } else { 64 };
421415

422416
Self {

0 commit comments

Comments
 (0)