Encoding
Encoding tests for convex-hull graphs done on a Core i7-13700k running Arch Linux on kernel 7.1.8-arch1-3
- Kodak Lossless True Color Image Suite: https://github.com/MohamedBakrAli/Kodak-Lossless-True-Color-Image-Suite/tree/master/PhotoCD_PCD0992
- Iris-WebP latest
- aperture-alpha latest
- cjpegli 0.12.0 –
031a0077 - libjxl 0.12.0 –
a7a9c787 - SvtAv1EncApp v4.2.0 (release)
- libaom 3.14.2
- libwebp v1.6.0
- FFmpeg n9.0.1
RGB-to-YUV conversion is controlled for WebP & AVIF with
scale=flags=lanczos+accurate_rnd+full_chroma_int:sws_dither=none:param0=5– see https://halide.cx/blog/chroma-handling
Some speeds are omitted in the actual graphs for readability
cjpegli: presets p0–p2, q ∈ {30,40,50,60,70,80,90}, reads the PAM directly:
libjxl: presets e1–e10, q ∈ {35,45,…,95}, reads the PAM directly:
SVT-AV1: presets p0–p9 (p10+ are identical to p9), q (crf) ∈ {5,12,19,26,33,40,47}:
libaom: presets s4–s9, q (cq-level) ∈ {5,12,19,26,33,40,47}. 10-bit Y4M
(scale=…,format=yuv420p10le, colorimetry from the Y4M header):
libwebp: method 0 through 6, q ∈ {40, 50, 60, 70, 80, 90, 100}:
For the BD-rate graph at the end, Daala subset1 4:4:4 was used. Same encoder versions as specified above. Q values are shown in the graph.
Decoding
Decoding tests done on a MacBook Pro M5, 18-core.
Vibed size-matched encoding script at bottom of page.
- libjxl 0.12.0 (release)
- djpegli 0.12.0
- avifdec 1.4.2 + dav1d 1.5.4
- fmetrics 0.0.2
- wpd
f201769
Decode speed test:
Method
- Every encode is transcoded to PAM (
ffmpeg … -vf SCALE,format=rgb24 -c pam; JPEG XL viadjxl OUT.jxl OUT.pam) and compared against the source PAM - SSIMULACRA2, CVVDP, & MS-SSIM computed via fmetrics
- BD-rate: per image, PCHIP interpolation of log-bitrate vs metric over the overlapping metric interval, Simpson integration (
bd_rate_simpsoninscripts/main.py), then averaged over the 24 Kodak images against thecjpegli_p0reference. Positive = better speed_ms= mean wall-clock of the encode command only (ffmpeg conversions and IVF→AVIF muxing are outside the timer), averaged over all image × q runs of that preset- As you can see above, the encoder binaries were called directly for SVT-AV1 & libaom. Normally, usage would be inside libavif or with an external AVIF muxer of some kind. This incurs minimal overhead that the benchmark doesn't capture, but doesn't meaningfully move the needle for speed (I was unable to measure a difference outside of noise in my own testing).
- Tiling was left at default; libavif has an auto-tiling mechanism that costs ~0.5-1% BD-rate for better multi-threaded decoding. Since all of our decode tests are single-threaded, I decided not to test this, and the tiny BD-rate difference wouldn't have shown up on the graphs anyway. Here's the difference on Kodak:
As promised, size-matching script for testing decode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | #!/usr/bin/env bash
set -euo pipefail
# Usage:
# ./encode_same_size.sh <input> [output_basename]
#
# Example:
# ./encode_same_size.sh input.png output
#
# Outputs:
# output.jpg
# output.webp
# output.avif
# output.jxl
#
# The JPEG is encoded first and becomes the target size.
# WebP, AVIF, and JXL are then encoded as close as possible to that
# size WITHOUT EVER GOING BELOW the JPEG size.
#
# Matching strategy:
# 1. Binary search quality 0-100.
# 2. Sweep nearby quality values around the binary-search result.
# 3. Choose the smallest output whose size is >= the JPEG target.
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <input> [output_basename]"
exit 1
fi
INPUT="$1"
if [[ ! -f "$INPUT" ]]; then
echo "ERROR: Input file does not exist: $INPUT" >&2
exit 1
fi
if [[ $# -ge 2 ]]; then
BASE="$2"
else
BASE="${INPUT%.*}"
fi
JPEG="${BASE}.jpg"
WEBP="${BASE}.webp"
AVIF="${BASE}.avif"
JXL="${BASE}.jxl"
# Number of quality values to test on either side of the
# binary-search result.
SWEEP_RADIUS=5
# ------------------------------------------------------------
# Dependency checks
# ------------------------------------------------------------
for cmd in cjpegli cwebp avifenc cjxl; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "ERROR: Required command not found: $cmd" >&2
exit 1
fi
done
# ------------------------------------------------------------
# Portable file-size function
# ------------------------------------------------------------
filesize() {
local file="$1"
if stat -c %s "$file" >/dev/null 2>&1; then
# GNU/Linux
stat -c %s "$file"
else
# macOS / BSD
stat -f %z "$file"
fi
}
# ------------------------------------------------------------
# Encode reference JPEG
# ------------------------------------------------------------
echo "Encoding reference JPEG..."
cjpegli "$INPUT" "$JPEG"
TARGET_SIZE="$(filesize "$JPEG")"
echo
echo "JPEG target:"
echo " file: $JPEG"
echo " size: $TARGET_SIZE bytes"
echo
# ------------------------------------------------------------
# Search for closest encode >= JPEG target
# ------------------------------------------------------------
search_quality() {
local codec="$1"
local output="$2"
local low=0
local high=100
local best_q=-1
local best_size=0
local tmp="${output}.tmp"
# Keep track of qualities already tested during binary search,
# so the neighborhood sweep does not need to encode them again.
declare -A tested_sizes=()
encode_quality() {
local q="$1"
rm -f "$tmp"
case "$codec" in
webp)
cwebp -quiet -m 6 -q "$q" -mt "$INPUT" -o "$tmp"
;;
avif)
avifenc -j 18 -d 10 -y 420 -s 6 -q "$q" -a tune=iq "$INPUT" "$tmp" >/dev/null
;;
jxl)
cjxl -q "$q" "$INPUT" "$tmp" >/dev/null
;;
*)
echo "ERROR: Unknown codec: $codec" >&2
exit 1
;;
esac
}
consider_candidate() {
local q="$1"
local size="$2"
printf "%-5s q=%3d %12d bytes delta=%+d\n" "$codec" "$q" "$size" "$(( size - TARGET_SIZE ))"
# Outputs smaller than the JPEG are never valid.
if (( size < TARGET_SIZE )); then
return
fi
# Select the smallest valid output.
if (( best_q == -1 || size < best_size )); then
best_q="$q"
best_size="$size"
cp "$tmp" "$output"
fi
}
# --------------------------------------------------------
# Phase 1: binary search
# --------------------------------------------------------
echo "Binary search:"
while (( low <= high )); do
local q=$(( (low + high) / 2 ))
encode_quality "$q"
local size
size="$(filesize "$tmp")"
tested_sizes["$q"]="$size"
consider_candidate "$q" "$size"
if (( size < TARGET_SIZE )); then
# Too small. Raise quality.
low=$(( q + 1 ))
else
# Valid, but try lower quality / potentially smaller size.
high=$(( q - 1 ))
fi
done
# If binary search found no valid result, explicitly test q=100
# in case unusual non-monotonic behavior caused it to be skipped.
if (( best_q == -1 )); then
echo
echo "Testing maximum quality..."
encode_quality 100
local size
size="$(filesize "$tmp")"
tested_sizes["100"]="$size"
consider_candidate 100 "$size"
fi
if (( best_q == -1 )); then
rm -f "$tmp"
echo
echo "ERROR: $codec cannot reach the JPEG target size." >&2
echo "Target: $TARGET_SIZE bytes" >&2
return 1
fi
# --------------------------------------------------------
# Phase 2: sweep around binary-search result
# --------------------------------------------------------
local binary_best_q="$best_q"
local start=$(( binary_best_q - SWEEP_RADIUS ))
local end=$(( binary_best_q + SWEEP_RADIUS ))
(( start < 0 )) && start=0
(( end > 100 )) && end=100
echo
echo "Neighborhood sweep: q=$start..$end"
for (( q=start; q<=end; q++ )); do
# If this quality was already tested, use the known size.
# The candidate file itself may no longer exist, however, so
# re-encode only if it could improve the current winner.
if [[ -n "${tested_sizes[$q]+x}" ]]; then
local size="${tested_sizes[$q]}"
printf "%-5s q=%3d %12d bytes delta=%+d [cached]\n" "$codec" "$q" "$size" "$(( size - TARGET_SIZE ))"
if (( size >= TARGET_SIZE && size < best_size )); then
encode_quality "$q"
cp "$tmp" "$output"
best_q="$q"
best_size="$size"
fi
continue
fi
encode_quality "$q"
local size
size="$(filesize "$tmp")"
tested_sizes["$q"]="$size"
consider_candidate "$q" "$size"
done
rm -f "$tmp"
echo
echo "$codec best:"
echo " quality: $best_q"
echo " size: $best_size bytes"
echo " target: $TARGET_SIZE bytes"
echo " excess: +$(( best_size - TARGET_SIZE )) bytes"
echo
}
# ------------------------------------------------------------
# Encode alternate formats
# ------------------------------------------------------------
echo "============================================================"
echo "WebP"
echo "============================================================"
search_quality webp "$WEBP"
echo "============================================================"
echo "AVIF"
echo "============================================================"
search_quality avif "$AVIF"
echo "============================================================"
echo "JPEG XL"
echo "============================================================"
search_quality jxl "$JXL"
# ------------------------------------------------------------
# Final results
# ------------------------------------------------------------
echo "============================================================"
echo "Final results"
echo "============================================================"
printf "%-8s %14s %14s\n" "Format" "Bytes" "vs JPEG"
printf "%-8s %14d %+14d\n" "JPEG" "$TARGET_SIZE" 0
for file in "$WEBP" "$AVIF" "$JXL"; do
size="$(filesize "$file")"
case "$file" in
*.webp)
name="WebP"
;;
*.avif)
name="AVIF"
;;
*.jxl)
name="JXL"
;;
esac
printf "%-8s %14d %+14d\n" "$name" "$size" "$(( size - TARGET_SIZE ))"
done
|