forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

hevc-bdrate.sh87 lines, 4.3 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# hevc-bdrate.sh: BD-rate of ec-h265's IDR+P sequence encoder against libx265 at the
3# same QPs, with x265 at bframes=0 and the same keyframe interval.
4#
5# Each side is encoded at QPS (default "22 27 32 37") on one clip; the points are
6# (bytes, Y and YUV PSNR of the decoded/reconstructed frames against the source,
7# from ffmpeg's psnr filter, see ffmpeg-psnr.sh). The
8# anchor is libx265 (-preset veryfast, -qp, -x265-params bframes=0:keyint=GOP), the
9# test is ec-h265. Positive bd_rate_pct means ec-h265 needs more bytes.
10#
11# The anchor is run with `-tune psnr` (X265_TUNE, default psnr; set X265_TUNE= to run
12# x265 at its default psy-rd/aq tuning). psy-rd and aq trade PSNR for perceptual detail, and
13# ec-h265 has neither, so the PSNR-only tune is the matched anchor for a BD-PSNR comparison.
14#
15# Each point file row: bytes, psnr_y, psnr_yuv, psnr_u, psnr_v, kbps (30 fps, so kbps is
16# bytes*8 divided by the clip duration in seconds).
17#
18# Usage: scripts/hevc-bdrate.sh <clip.yuv> <width> <height> [GOP] [QPS]
19# Environment: DATA (default $HOME/.cache/hevcencp-data), X265_TUNE (default psnr).
20set -euo pipefail
21
22here=$(cd "$(dirname "$0")/.." && pwd)
23clip=$1
24w=$2
25h=$3
26gop=${4:-8}
27qps=${5:-"22 27 32 37"}
28data=${DATA:-$HOME/.cache/hevcencp-data}
29mkdir -p "$data"
30[ -f "$clip" ] || clip="$data/$clip"
31tag=$(basename "$clip" .yuv)-${w}x${h}-k$gop
32# PIX=yuv420p10le runs the Main 10 comparison: 16-bit samples, EC_H265_BITDEPTH=10.
33pix=${PIX:-yuv420p}
34bps=1
35[ "$pix" = yuv420p10le ] && bps=2
36export EC_H265_BITDEPTH=$([ $bps = 2 ] && echo 10 || echo 8)
37frames=$(( $(stat -c %s "$clip") / (w * h * 3 / 2 * bps) ))
38
39cargo build --release -q -p ec-h265 --example hevc_gop --manifest-path "$here/Cargo.toml"
40target=$(cargo metadata --format-version 1 --no-deps --manifest-path "$here/Cargo.toml" \
41 | python3 -I -c 'import json,sys; print(json.load(sys.stdin)["target_directory"])')
42gopbin="$target/release/examples/hevc_gop"
43
44anchor="$data/$tag.x265.tsv"
45test_="$data/$tag.ec.tsv"
46: > "$anchor"
47: > "$test_"
48for qp in $qps; do
49 x265="$data/$tag-qp$qp.x265.hevc"
50 tune=()
51 [ -n "${X265_TUNE-psnr}" ] && tune=(-tune "${X265_TUNE-psnr}")
52 profile=()
53 [ "$bps" = 2 ] && profile=(-profile:v main10)
54 ffmpeg -v error -y -f rawvideo -pix_fmt "$pix" -s "${w}x${h}" -r 30 -i "$clip" \
55 -c:v libx265 -preset veryfast -qp "$qp" "${tune[@]}" "${profile[@]}" \
56 -x265-params "bframes=0:keyint=$gop:min-keyint=$gop:scenecut=0:log-level=error" \
57 -f hevc "$x265"
58 xdec="$data/$tag-qp$qp.x265.yuv"
59 ffmpeg -v error -y -i "$x265" -f rawvideo -pix_fmt "$pix" "$xdec"
60 xline=$(PIX=$pix "$here/scripts/ffmpeg-psnr.sh" "$clip" "$xdec" "$w" "$h")
61 xpy=$(echo "$xline" | sed -n 's/.*psnr_y=\([^ ]*\).*/\1/p')
62 xpsnr=$(echo "$xline" | sed -n 's/.*psnr_yuv=\([^ ]*\).*/\1/p')
63 xpu=$(echo "$xline" | sed -n 's/.*psnr_u=\([^ ]*\).*/\1/p')
64 xpv=$(echo "$xline" | sed -n 's/.*psnr_v=\([^ ]*\).*/\1/p')
65 xbytes=$(stat -c %s "$x265")
66 printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$xbytes" "$xpy" "$xpsnr" "$xpu" "$xpv" "$(awk -v b="$xbytes" -v f="$frames" 'BEGIN{printf "%.2f", b*8*30/f/1000}')" >> "$anchor"
67
68 ours="$data/$tag-qp$qp.ec.hevc"
69 recon="$data/$tag-qp$qp.ec.recon.yuv"
70 "$gopbin" encode "$clip" "$w" "$h" "$gop" "$qp" "$ours" "$recon" ${EXTRA:-} >/dev/null
71 oline=$(PIX=$pix "$here/scripts/ffmpeg-psnr.sh" "$clip" "$recon" "$w" "$h")
72 opy=$(echo "$oline" | sed -n 's/.*psnr_y=\([^ ]*\).*/\1/p')
73 opsnr=$(echo "$oline" | sed -n 's/.*psnr_yuv=\([^ ]*\).*/\1/p')
74 opu=$(echo "$oline" | sed -n 's/.*psnr_u=\([^ ]*\).*/\1/p')
75 opv=$(echo "$oline" | sed -n 's/.*psnr_v=\([^ ]*\).*/\1/p')
76 obytes=$(stat -c %s "$ours")
77 printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$obytes" "$opy" "$opsnr" "$opu" "$opv" "$(awk -v b="$obytes" -v f="$frames" 'BEGIN{printf "%.2f", b*8*30/f/1000}')" >> "$test_"
78 echo "qp=$qp x265: bytes=$(stat -c %s "$x265") psnr_y=$xpy psnr_yuv=$xpsnr | ec-h265: bytes=$(stat -c %s "$ours") psnr_y=$opy psnr_yuv=$opsnr" >&2
79done
80echo "clip=$(basename "$clip") size=${w}x${h} frames=$frames gop=$gop qps=[$qps]"
81echo "points (bytes, psnr_y, psnr_yuv):"
82echo " anchor x265:"; sed 's/^/ /' "$anchor"
83echo " test ec-h265:"; sed 's/^/ /' "$test_"
84echo "metric=yuv (6:1:1):"
85python3 -I "$here/scripts/bdrate.py" --metric yuv "$anchor" "$test_"
86echo "metric=y:"
87python3 -I "$here/scripts/bdrate.py" --metric y "$anchor" "$test_"