Skip to content

Commit

Permalink
Reformat code and introduce rustfmt check in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Dec 12, 2024
1 parent 8c85531 commit 76273d6
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 129 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Rust

on: [push, pull_request]

jobs:
formatting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Format
run: cargo fmt --all -- --check
2 changes: 1 addition & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ binop_separator = "Back"
chain_width = 120
comment_width = 222
condense_wildcard_suffixes = true
disable_all_formatting = true
# disable_all_formatting = true
edition = "2021"
enum_discrim_align_threshold = 5
fn_call_width = 120
Expand Down
86 changes: 58 additions & 28 deletions ravif/src/av1encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ impl Encoder {
AlphaColorMode::UnassociatedDirty => None,
AlphaColorMode::UnassociatedClean => blurred_dirty_alpha(in_buffer),
AlphaColorMode::Premultiplied => {
let prem = in_buffer.pixels()
let prem = in_buffer
.pixels()
.filter(|px| px.a != 255)
.map(|px| {
if px.a == 0 {
Expand Down Expand Up @@ -361,9 +362,7 @@ impl Encoder {
matrix_coefficients,
});

let threads = self.threads.map(|threads| {
if threads > 0 { threads } else { rayon::current_num_threads() }
});
let threads = self.threads.map(|threads| if threads > 0 { threads } else { rayon::current_num_threads() });

let encode_color = move || {
encode_to_av1::<P>(
Expand Down Expand Up @@ -422,7 +421,9 @@ impl Encoder {
let alpha_byte_size = alpha.as_ref().map_or(0, |a| a.len());

Ok(EncodedImage {
avif_file, color_byte_size, alpha_byte_size,
avif_file,
color_byte_size,
alpha_byte_size,
})
}
}
Expand Down Expand Up @@ -470,7 +471,13 @@ fn rgb_to_8_bit_ycbcr(px: rgb::RGB<u8>, matrix: [f32; 3]) -> (u8, u8, u8) {

fn quality_to_quantizer(quality: f32) -> u8 {
let q = quality / 100.;
let x = if q >= 0.85 { (1. - q) * 3. } else if q > 0.25 { 1. - 0.125 - q * 0.5 } else { 1. - q };
let x = if q >= 0.85 {
(1. - q) * 3.
} else if q > 0.25 {
1. - 0.125 - q * 0.5
} else {
1. - q
};
(x * 255.).round() as u8
}

Expand Down Expand Up @@ -515,29 +522,28 @@ impl SpeedTweaks {
}),

complex_prediction_modes: Some(speed <= 1), // 2x-3x slower, 2% better
sgr_complexity_full: Some(speed <= 2), // 15% slower, barely improves anything -/+1%
sgr_complexity_full: Some(speed <= 2), // 15% slower, barely improves anything -/+1%

encode_bottomup: Some(speed <= 2), // may be costly (+60%), may even backfire

// big blocks disabled at 3

// these two are together?
rdo_tx_decision: Some(speed <= 4 && !high_quality), // it tends to blur subtle textures
reduced_tx_set: Some(speed == 4 || speed >= 9), // It interacts with tx_domain_distortion too?
reduced_tx_set: Some(speed == 4 || speed >= 9), // It interacts with tx_domain_distortion too?

// 4px blocks disabled at 5

fine_directional_intra: Some(speed <= 6),
fast_deblock: Some(speed >= 7 && !high_quality), // mixed bag?

// 8px blocks disabled at 8
lrf: Some(low_quality && speed <= 8), // hardly any help for hi-q images. recovers some q at low quality
lrf: Some(low_quality && speed <= 8), // hardly any help for hi-q images. recovers some q at low quality
cdef: Some(low_quality && speed <= 9), // hardly any help for hi-q images. recovers some q at low quality

inter_tx_split: Some(speed >= 9), // mixed bag even when it works, and it backfires if not used together with reduced_tx_set
tx_domain_rate: Some(speed >= 10), // 20% faster, but also 10% larger files!

tx_domain_distortion: None, // very mixed bag, sometimes helps speed sometimes it doesn't
tx_domain_distortion: None, // very mixed bag, sometimes helps speed sometimes it doesn't
use_satd_subpel: Some(false), // doesn't make sense
min_tile_size: match speed {
0 => 4096,
Expand All @@ -558,19 +564,45 @@ impl SpeedTweaks {
speed_settings.scene_detection_mode = SceneDetectionSpeed::None;
speed_settings.motion.include_near_mvs = false;

if let Some(v) = self.fast_deblock { speed_settings.fast_deblock = v; }
if let Some(v) = self.reduced_tx_set { speed_settings.transform.reduced_tx_set = v; }
if let Some(v) = self.tx_domain_distortion { speed_settings.transform.tx_domain_distortion = v; }
if let Some(v) = self.tx_domain_rate { speed_settings.transform.tx_domain_rate = v; }
if let Some(v) = self.encode_bottomup { speed_settings.partition.encode_bottomup = v; }
if let Some(v) = self.rdo_tx_decision { speed_settings.transform.rdo_tx_decision = v; }
if let Some(v) = self.cdef { speed_settings.cdef = v; }
if let Some(v) = self.lrf { speed_settings.lrf = v; }
if let Some(v) = self.inter_tx_split { speed_settings.transform.enable_inter_tx_split = v; }
if let Some(v) = self.sgr_complexity_full { speed_settings.sgr_complexity = if v { SGRComplexityLevel::Full } else { SGRComplexityLevel::Reduced } };
if let Some(v) = self.use_satd_subpel { speed_settings.motion.use_satd_subpel = v; }
if let Some(v) = self.fine_directional_intra { speed_settings.prediction.fine_directional_intra = v; }
if let Some(v) = self.complex_prediction_modes { speed_settings.prediction.prediction_modes = if v { PredictionModesSetting::ComplexAll } else { PredictionModesSetting::Simple} };
if let Some(v) = self.fast_deblock {
speed_settings.fast_deblock = v;
}
if let Some(v) = self.reduced_tx_set {
speed_settings.transform.reduced_tx_set = v;
}
if let Some(v) = self.tx_domain_distortion {
speed_settings.transform.tx_domain_distortion = v;
}
if let Some(v) = self.tx_domain_rate {
speed_settings.transform.tx_domain_rate = v;
}
if let Some(v) = self.encode_bottomup {
speed_settings.partition.encode_bottomup = v;
}
if let Some(v) = self.rdo_tx_decision {
speed_settings.transform.rdo_tx_decision = v;
}
if let Some(v) = self.cdef {
speed_settings.cdef = v;
}
if let Some(v) = self.lrf {
speed_settings.lrf = v;
}
if let Some(v) = self.inter_tx_split {
speed_settings.transform.enable_inter_tx_split = v;
}
if let Some(v) = self.sgr_complexity_full {
speed_settings.sgr_complexity = if v { SGRComplexityLevel::Full } else { SGRComplexityLevel::Reduced }
};
if let Some(v) = self.use_satd_subpel {
speed_settings.motion.use_satd_subpel = v;
}
if let Some(v) = self.fine_directional_intra {
speed_settings.prediction.fine_directional_intra = v;
}
if let Some(v) = self.complex_prediction_modes {
speed_settings.prediction.prediction_modes = if v { PredictionModesSetting::ComplexAll } else { PredictionModesSetting::Simple }
};
if let Some((min, max)) = self.partition_range {
debug_assert!(min <= max);
fn sz(s: u8) -> BlockSize {
Expand Down Expand Up @@ -612,8 +644,7 @@ fn rav1e_config(p: &Av1EncodeConfig) -> Config {
threads.min((p.width * p.height) / (p.speed.min_tile_size as usize).pow(2))
};
let speed_settings = p.speed.speed_settings();
let cfg = Config::new()
.with_encoder_config(EncoderConfig {
let cfg = Config::new().with_encoder_config(EncoderConfig {
width: p.width,
height: p.height,
time_base: Rational::new(1, 1),
Expand Down Expand Up @@ -708,8 +739,7 @@ fn encode_to_av1<P: rav1e::Pixel>(p: &Av1EncodeConfig, init: impl FnOnce(&mut Fr
},
_ => continue,
},
Err(EncoderStatus::Encoded) |
Err(EncoderStatus::LimitReached) => break,
Err(EncoderStatus::Encoded) | Err(EncoderStatus::LimitReached) => break,
Err(err) => Err(err)?,
}
}
Expand Down
20 changes: 9 additions & 11 deletions ravif/src/dirtyalpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ fn weighed_pixel(px: RGBA8) -> (u16, RGB<u32>) {
return (0, RGB::new(0, 0, 0));
}
let weight = 256 - u16::from(px.a);
(weight, RGB::new(
u32::from(px.r) * u32::from(weight),
u32::from(px.g) * u32::from(weight),
u32::from(px.b) * u32::from(weight)))
(
weight,
RGB::new(u32::from(px.r) * u32::from(weight), u32::from(px.g) * u32::from(weight), u32::from(px.b) * u32::from(weight)),
)
}

/// Clear/change RGB components of fully-transparent RGBA pixels to make them cheaper to encode with AV1
Expand Down Expand Up @@ -48,13 +48,11 @@ fn bleed_opaque_color(img: ImgRef<RGBA8>, bg: RGBA8) -> Img<Vec<RGBA8>> {
out.push(if mid.curr.a == 255 {
mid.curr
} else {
let (weights, sum) = chain(&top, &mid, &bot)
.map(|c| weighed_pixel(*c))
.fold((0u32, RGB::new(0,0,0)), |mut sum, item| {
sum.0 += u32::from(item.0);
sum.1 += item.1;
sum
});
let (weights, sum) = chain(&top, &mid, &bot).map(|c| weighed_pixel(*c)).fold((0u32, RGB::new(0, 0, 0)), |mut sum, item| {
sum.0 += u32::from(item.0);
sum.1 += item.1;
sum
});
if weights == 0 {
bg
} else {
Expand Down
Loading

0 comments on commit 76273d6

Please sign in to comment.