Encoding

Encoding tests for convex-hull graphs done on a Core i7-13700k running Arch Linux on kernel 7.1.8-arch1-3

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:

⎗
✓
cjpegli SRC.pam OUT.jpg -q Q -p P --chroma_subsampling=420

libjxl: presets e1–e10, q ∈ {35,45,…,95}, reads the PAM directly:

⎗
✓
cjxl SRC.pam OUT.jxl -q Q -e E

SVT-AV1: presets p0–p9 (p10+ are identical to p9), q (crf) ∈ {5,12,19,26,33,40,47}:

⎗
✓
SvtAv1EncApp -i tmp.y4m -b tmp.ivf --lp 1 --avif 1 --rtc 0  --color-primaries 2 --transfer-characteristics 2 --matrix-coefficients 6 --color-range 0  --crf Q --preset P --tune 4      # svtav1; svtav1_iq uses --tune 3
ffmpeg -i tmp.ivf -c:v copy OUT.avif

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):

⎗
✓
aomenc tmp.y4m -o tmp.ivf --ivf --codec=av1 --bit-depth=10 --allintra --end-usage=q  --cq-level=Q --threads=1 --cpu-used C --tune=ssimulacra2
ffmpeg -i tmp.ivf -c:v copy OUT.avif

libwebp: method 0 through 6, q ∈ {40, 50, 60, 70, 80, 90, 100}:

⎗
✓
ffmpeg -hide_banner -loglevel error -y -i SRC.pam -vf scale=...,format=yuv420p TMP.yuv
cwebp -q Q -s 768 512 -m M TMP.yuv -o OUT.webp

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.

Source Image

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:

⎗
✓
hyperfine -N --warmup 1 "jxl_cli --num-threads 1 --speedtest -n 1 --warmup-reps 0 16b.jxl" "djxl --disable_output --num_threads=0 16b.jxl" "djpegli --disable_output 16b.jpg" "avifdec -j 1 16b.avif 16b.y4m" "$HOME/git-cloning/wpd/build/wpd --muxer raw 16b.webp 16b.raw"

Method

  • Every encode is transcoded to PAM (ffmpeg … -vf SCALE,format=rgb24 -c pam; JPEG XL via djxl 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_simpson in scripts/main.py), then averaged over the 24 Kodak images against the cjpegli_p0 reference. 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:
    ⎗
    ✓
    1
    2
    3
    4
    bd-rate stats (svtav1_base vs svtav1_tiles):
    ssimu2         : avg  +0.620 ± 0.428 min  -0.011 max  +1.511 final  +0.625
    ms_ssim        : avg  +0.774 ± 0.272 min  +0.137 max  +1.271 final  +0.785
    cvvdp          : avg  +0.691 ± 0.293 min  +0.171 max  +1.237 final  +0.696
    

As promised, size-matching script for testing decode:

⎗
✓
#!/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
Edit

Pub: 13 Sep 2026 09:17 UTC

Edit: 17 Sep 2026 06:00 UTC

Views: 710