forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

hevc-rc-bdrate.sh66 lines, 3.0 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# hevc-rc-bdrate.sh: BD-rate of ec-h265's bitrate-controlled sequence encoder against libx265
3# at the same bitrates, with x265 at bframes=0 and the same keyframe interval.
4#
5# The bitrate counterpart of hevc-bdrate.sh (which sweeps constant QP). Each side is encoded at
6# the KBPS targets (ec-h265 through the hevc_rc example, x265 with -b:v at veryfast); the points
7# are (bytes, Y and YUV PSNR against the source, from scripts/ffmpeg-psnr.sh). Positive
8# bd_rate_pct means ec-h265 needs more bytes at the same quality.
9#
10# Usage: scripts/hevc-rc-bdrate.sh <clip.yuv> <width> <height> [GOP] [KBPS list]
11# Environment: DATA (default $HOME/.cache/hevcencrc-data), FPS (default 30).
12set -euo pipefail
13
14here=$(cd "$(dirname "$0")/.." && pwd)
15clip=$1
16w=$2
17h=$3
18gop=${4:-30}
19kbpss=${5:-"1000 2000 4000 8000"}
20fps=${FPS:-30}
21data=${DATA:-$HOME/.cache/hevcencrc-data}
22mkdir -p "$data"
23[ -f "$clip" ] || clip="$data/$clip"
24tag=$(basename "$clip" .yuv)-${w}x${h}-k$gop-rc
25
26cargo build --release -q -p ec-h265 --example hevc_rc --manifest-path "$here/Cargo.toml"
27target=$(cargo metadata --format-version 1 --no-deps --manifest-path "$here/Cargo.toml" \
28 | python3 -I -c 'import json,sys; print(json.load(sys.stdin)["target_directory"])')
29rcbin="$target/release/examples/hevc_rc"
30
31anchor="$data/$tag.x265.tsv"
32test_="$data/$tag.ec.tsv"
33: > "$anchor"
34: > "$test_"
35for kbps in $kbpss; do
36 x265="$data/$tag-$kbps.x265.hevc"
37 ffmpeg -v error -y -f rawvideo -pix_fmt yuv420p -s "${w}x${h}" -r "$fps" -i "$clip" \
38 -c:v libx265 -preset veryfast -b:v "${kbps}k" \
39 -x265-params "bframes=0:keyint=$gop:min-keyint=$gop:scenecut=0:log-level=error" \
40 -f hevc "$x265"
41 xdec="$data/$tag-$kbps.x265.yuv"
42 ffmpeg -v error -y -i "$x265" -f rawvideo -pix_fmt yuv420p "$xdec"
43 xline=$("$here/scripts/ffmpeg-psnr.sh" "$clip" "$xdec" "$w" "$h")
44 xpy=$(echo "$xline" | sed -n 's/.*psnr_y=\([^ ]*\).*/\1/p')
45 xpsnr=$(echo "$xline" | sed -n 's/.*psnr_yuv=\([^ ]*\).*/\1/p')
46 rm -f "$xdec"
47 printf '%s\t%s\t%s\n' "$(stat -c %s "$x265")" "$xpy" "$xpsnr" >> "$anchor"
48
49 ours="$data/$tag-$kbps.ec.hevc"
50 recon="$data/$tag-$kbps.ec.recon.yuv"
51 "$rcbin" encode "$clip" "$w" "$h" "$gop" "$kbps" "$fps" "$ours" "$recon" >/dev/null
52 oline=$("$here/scripts/ffmpeg-psnr.sh" "$clip" "$recon" "$w" "$h")
53 rm -f "$recon"
54 opy=$(echo "$oline" | sed -n 's/.*psnr_y=\([^ ]*\).*/\1/p')
55 opsnr=$(echo "$oline" | sed -n 's/.*psnr_yuv=\([^ ]*\).*/\1/p')
56 printf '%s\t%s\t%s\n' "$(stat -c %s "$ours")" "$opy" "$opsnr" >> "$test_"
57 echo "kbps=$kbps x265: bytes=$(stat -c %s "$x265") psnr_y=$xpy | ec-h265: bytes=$(stat -c %s "$ours") psnr_y=$opy" >&2
58done
59echo "clip=$(basename "$clip") size=${w}x${h} gop=$gop kbps=[$kbpss]"
60echo "points (bytes, psnr_y, psnr_yuv):"
61echo " anchor x265:"; sed 's/^/ /' "$anchor"
62echo " test ec-h265:"; sed 's/^/ /' "$test_"
63echo "metric=yuv (6:1:1):"
64python3 -I "$here/scripts/bdrate.py" --metric yuv "$anchor" "$test_"
65echo "metric=y:"
66python3 -I "$here/scripts/bdrate.py" --metric y "$anchor" "$test_"