Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ jobs:
run: cargo build --verbose --features simd
- name: Run tests
run: cargo test --verbose --features simd
- name: Run tests use_wide
run: cargo test --verbose --features use_wide
- name: Run tests portable_simd
run: cargo test --verbose --no-default-features --features portable_simd

no_std:

Expand All @@ -51,6 +51,10 @@ jobs:
run: cargo build --verbose --no-default-features
- name: Run tests
run: cargo test --verbose --no-default-features --tests
- name: Build portable_simd_no_std
run: cargo build --verbose --no-default-features --features portable_simd_no_std
- name: Run tests portable_simd_no_std
run: cargo test --verbose --no-default-features --features portable_simd_no_std --tests

rustfmt:

Expand All @@ -62,4 +66,4 @@ jobs:
with:
components: rustfmt
- name: Run rustfmt check
run: cargo fmt -- --check
run: cargo fmt -- --check
14 changes: 10 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ rust-version = "1.89"
[features]
default = ["std"]
simd = ["std"]
std = []
use_wide = ["wide", "bytemuck"]
std = ["fearless_simd?/std"]
portable_simd = ["fearless_simd", "std"]
# without std there is no runtime CPU detection:
# x86 uses SSE2, Aarch64 uses NEON, others have no SIMD
# unless explicitly enabled with `-C target-cpu` or `-C target-feature`
portable_simd_no_std = ["fearless_simd", "fearless_simd/libm"]

# Internal implementation feature; use portable_simd or portable_simd_no_std instead.
fearless_simd = ["dep:fearless_simd"]
# DO NOT USE THIS IN PRODUCTION. Expose several internal functions for benchmark purposes.
benchmark = []

[dependencies]
wide = { version = "1.0", optional = true, default-features = false }
bytemuck = { version = "1.24", optional = true, default-features = false }
# pairwise_dot_product is not released yet: https://github.com/linebender/fearless_simd/pull/415
fearless_simd = { version = "1.2.0", optional = true, default-features = false }

[dev-dependencies]
jpeg-decoder = { version = "0.3", default-features = false }
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ encoder.encode(&data, 2, 2, ColorType::Rgb)?;
## Crate features
- `std` (default): Enables functionality dependent on the std lib
- `simd`: Enables SIMD optimizations (implies `std` and only AVX2 as for now)
- `portable_simd`: Enables portable SIMD using `fearless_simd` and enables `std`, including runtime CPU detection.
- `portable_simd_no_std`: Enables portable SIMD using `fearless_simd` and its `libm` math backend without enabling `std`. Uses compile-time target features when `std` is disabled.

To build with portable SIMD and `no_std`, use `--no-default-features --features portable_simd_no_std`.

## Minimum Supported Version of Rust (MSRV)

Expand Down
4 changes: 4 additions & 0 deletions criterion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ members = ["."]

[features]
simd = ["jpeg-encoder/simd"]
portable_simd = ["fearless_simd", "jpeg-encoder/portable_simd"]
portable_simd_no_std = ["fearless_simd", "jpeg-encoder/portable_simd_no_std"]
# Internal implementation feature shared by the portable SIMD benchmarks.
fearless_simd = ["jpeg-encoder/fearless_simd"]

[dependencies]
jpeg-encoder = {path = "..", features = ["benchmark"]}
Expand Down
6 changes: 3 additions & 3 deletions criterion/benches/fdct.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use jpeg_encoder::fdct;
use jpeg_encoder::{fdct, AlignedBlock};
use std::time::Duration;

const INPUT1: [i16; 64] = [
Expand All @@ -18,7 +18,7 @@ fn criterion_benchmark(c: &mut Criterion) {

group.bench_function("default fdct", |b| {
b.iter(|| {
let mut input = INPUT1.clone();
let mut input = AlignedBlock::new(INPUT1);
fdct(
black_box(&mut input),
);
Expand All @@ -31,7 +31,7 @@ fn criterion_benchmark(c: &mut Criterion) {
b.iter(|| {
use jpeg_encoder::fdct_avx2;

let mut input = INPUT1.clone();
let mut input = AlignedBlock::new(INPUT1);
fdct_avx2(
black_box(&mut input),
);
Expand Down
103 changes: 62 additions & 41 deletions criterion/benches/ycbcr.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{
black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion,
Throughput,
};

use jpeg_encoder::{ImageBuffer, RgbImage};
use jpeg_encoder::ImageBuffer;
use std::time::Duration;

fn create_bench_img() -> (Vec<u8>, u16, u16) {
let width = 1001;
let height = 500;

fn create_bench_img(width: usize, height: usize) -> Vec<u8> {
let mut data = Vec::with_capacity(width * height * 3);

for y in 0..height {
Expand Down Expand Up @@ -51,53 +51,74 @@ fn create_bench_img() -> (Vec<u8>, u16, u16) {
}
}

(data, width as u16, height as u16)
data
}

fn criterion_benchmark(c: &mut Criterion) {
let (data, width, height) = create_bench_img();
let res1 = Vec::with_capacity(usize::from(width));
let res2 = Vec::with_capacity(usize::from(width));
let res3 = Vec::with_capacity(usize::from(width));
let res4 = Vec::with_capacity(usize::from(width));

let mut res = [res1, res2, res3, res4];

let mut group = c.benchmark_group("ycbcr");
group.measurement_time(Duration::from_secs(45));
group.warm_up_time(Duration::from_secs(10));

group.bench_function("default ycbcr", |b| {
let image_buffer = RgbImage(&data, width, height);

fn bench_image(group: &mut BenchmarkGroup<'_, WallTime>, name: &str, image: impl ImageBuffer) {
let mut res = core::array::from_fn(|_| Vec::with_capacity(usize::from(image.width())));
group.bench_function(name, |b| {
b.iter(|| {
for y in 0..height {
for y in 0..image.height() {
res[0].clear();
res[1].clear();
res[2].clear();
image_buffer.fill_buffers(y, black_box(&mut res));
image.fill_buffers(y, black_box(&mut res));
}
black_box(&res);
});
});
}

#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
group.bench_function("ycbcr avx2", |b| {
use jpeg_encoder::RgbImageAVX2;
let image_buffer = RgbImageAVX2(&data, width, height);

b.iter(|| {
for y in 0..height {
res[0].clear();
res[1].clear();
res[2].clear();
image_buffer.fill_buffers(y, black_box(&mut res));
}
black_box(&res);
})
});
fn criterion_benchmark(c: &mut Criterion) {
for width in [1001, 1920] {
let height = 500;
let rgb = create_bench_img(width as usize, height as usize);
let rgba: Vec<u8> = rgb
.chunks_exact(3)
.flat_map(|p| [p[0], p[1], p[2], 255])
.collect();
let bgr: Vec<u8> = rgb
.chunks_exact(3)
.flat_map(|p| [p[2], p[1], p[0]])
.collect();
let bgra: Vec<u8> = rgb
.chunks_exact(3)
.flat_map(|p| [p[2], p[1], p[0], 255])
.collect();

group.finish();
macro_rules! compare {
($format:literal, $data:ident, $scalar:ident, $avx:ident, $portable:ident) => {{
let mut group = c.benchmark_group(format!("ycbcr/{}/{width}", $format));
group.measurement_time(Duration::from_secs(3));
group.warm_up_time(Duration::from_secs(1));
group.throughput(Throughput::Elements(u64::from(width) * u64::from(height)));
bench_image(
&mut group,
"scalar",
jpeg_encoder::$scalar(&$data, width, height),
);
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
if std::is_x86_feature_detected!("avx2") {
bench_image(
&mut group,
"avx2",
jpeg_encoder::$avx(&$data, width, height),
);
}
#[cfg(feature = "fearless_simd")]
bench_image(
&mut group,
"portable",
jpeg_encoder::$portable(&$data, width, height),
);
group.finish();
}};
}
compare!("rgb", rgb, RgbImage, RgbImageAVX2, RgbImageSimd);
compare!("rgba", rgba, RgbaImage, RgbaImageAVX2, RgbaImageSimd);
compare!("bgr", bgr, BgrImage, BgrImageAVX2, BgrImageSimd);
compare!("bgra", bgra, BgraImage, BgraImageAVX2, BgraImageSimd);
}
}

criterion_group!(benches, criterion_benchmark);
Expand Down
3 changes: 3 additions & 0 deletions src/avx2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
mod fdct;
mod ycbcr;

#[cfg(not(feature = "fearless_simd"))]
use crate::encoder::{AlignedBlock, Operations};
pub use fdct::fdct_avx2;
pub use ycbcr::*;

#[cfg(not(feature = "fearless_simd"))]
pub(crate) struct AVX2Operations;

#[cfg(not(feature = "fearless_simd"))]
impl Operations for AVX2Operations {
#[inline(always)]
fn fdct(data: &mut AlignedBlock) {
Expand Down
19 changes: 16 additions & 3 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use crate::huffman::{CodingClass, HuffmanTable};
use crate::image_buffer::*;
use crate::marker::Marker;
use crate::quantization::{QuantizationTable, QuantizationTableType};
#[cfg(feature = "fearless_simd")]
use crate::simd::{
BgrImageSimd as BgrImage, BgraImageSimd as BgraImage, RgbImageSimd as RgbImage,
RgbaImageSimd as RgbaImage,
};
use crate::writer::{JfifWrite, JfifWriter, ZIGZAG};
use crate::{EncodingError, PixelDensity};

Expand Down Expand Up @@ -36,7 +41,7 @@ pub enum JpegColorType {

#[derive(Copy, Clone)]
#[repr(C, align(32))]
pub(crate) struct AlignedBlock {
pub struct AlignedBlock {
pub data: [i16; 64],
}

Expand Down Expand Up @@ -484,7 +489,11 @@ impl<W: JfifWrite> Encoder<W> {
});
}

#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
#[cfg(all(
feature = "simd",
not(feature = "fearless_simd"),
any(target_arch = "x86", target_arch = "x86_64")
))]
{
if std::is_x86_feature_detected!("avx2") {
use crate::avx2::*;
Expand Down Expand Up @@ -535,7 +544,11 @@ impl<W: JfifWrite> Encoder<W> {

/// Encode an image
pub fn encode_image<I: ImageBuffer>(self, image: I) -> Result<(), EncodingError> {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
#[cfg(all(
feature = "simd",
not(feature = "fearless_simd"),
any(target_arch = "x86", target_arch = "x86_64")
))]
{
if std::is_x86_feature_detected!("avx2") {
use crate::avx2::*;
Expand Down
Loading
Loading