forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

hevc-rc.sh75 lines, 3.2 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# hevc-rc.sh: bitrate control of ec-h265's sequence encoder on a real clip.
3#
4# For each target bitrate: encode the clip with RateControl::Bitrate, decode the
5# stream with ec-h265 and with ffmpeg, and check both decodes byte-for-byte against
6# the encoder's own reconstruction. Reports the achieved bitrate and the PSNR of the
7# reconstruction against the source.
8#
9# Usage: scripts/hevc-rc.sh <clip> [SECONDS] [KBPS list]
10# clip any video ffmpeg can read; it is cut to SECONDS and scaled to 1280x720 at 30 fps
11# SECONDS clip length (default 60)
12# KBPS targets in kbit/s (default "1000 2500 5000")
13# Environment: DATA (default $HOME/.cache/hevcencrc-data), KEYINT (default 30).
14#
15# Run it where cargo builds: scripts/remote-run.sh hevcencrc -- scripts/hevc-rc.sh <clip>
16# Intermediate YUV files are deleted as soon as they are checked: the 60 s 720p source is
17# 2.5 GB and each decode is the same again.
18set -euo pipefail
19
20here=$(cd "$(dirname "$0")/.." && pwd)
21clip=$1
22secs=${2:-60}
23kbpss=${3:-"1000 2500 5000"}
24data=${DATA:-$HOME/.cache/hevcencrc-data}
25keyint=${KEYINT:-30}
26fps=30
27w=1280
28h=720
29mkdir -p "$data"
30
31src="$data/src-${w}x${h}-${fps}-${secs}s.yuv"
32if [ ! -f "$src" ]; then
33 ffmpeg -v error -y -i "$clip" -t "$secs" \
34 -vf "scale=${w}:${h}:flags=bicubic,fps=${fps}" -pix_fmt yuv420p -f rawvideo "$src"
35fi
36frames=$(( $(stat -c %s "$src") / (w * h * 3 / 2) ))
37
38cargo build --release -q -p ec-h265 --example hevc_rc --manifest-path "$here/Cargo.toml"
39target=$(cargo metadata --format-version 1 --no-deps --manifest-path "$here/Cargo.toml" \
40 | python3 -I -c 'import json,sys; print(json.load(sys.stdin)["target_directory"])')
41bin="$target/release/examples/hevc_rc"
42
43# PSNR of a raw I420 file against the source, the average over Y, U and V as ffmpeg reports it.
44psnr() {
45 ffmpeg -v info -f rawvideo -pix_fmt yuv420p -video_size ${w}x${h} -framerate $fps -i "$1" \
46 -f rawvideo -pix_fmt yuv420p -video_size ${w}x${h} -framerate $fps -i "$src" \
47 -lavfi psnr -f null - 2>&1 | sed -n 's/.*PSNR .* average:\([0-9.inf]*\).*/\1/p' | tail -1
48}
49
50status=0
51echo "clip=$clip secs=$secs frames=$frames keyint=$keyint size=${w}x${h}@${fps}"
52printf '%-8s %-12s %-10s %-10s %-8s %-10s %-12s\n' target_kbps achieved_kbps err_pct psnr_y_avg mean_qp ec_decode ffmpeg_decode
53for kbps in $kbpss; do
54 stream="$data/rc-${kbps}.hevc"
55 rec="$data/rc-${kbps}-recon.yuv"
56 dec="$data/rc-${kbps}-ec.yuv"
57 ffdec="$data/rc-${kbps}-ff.yuv"
58 line=$("$bin" encode "$src" $w $h "$keyint" "$kbps" "$fps" "$stream" "$rec")
59 achieved=$(sed -n 's/.*kbps=\([0-9.]*\).*/\1/p' <<<"$line")
60 qp=$(sed -n 's/.*mean_qp=\([0-9.]*\).*/\1/p' <<<"$line")
61 err=$(python3 -I -c "print(f'{($achieved - $kbps) / $kbps * 100:+.2f}')")
62
63 "$bin" decode "$stream" "$dec" >/dev/null
64 if cmp -s "$rec" "$dec"; then ec_ok=match; else ec_ok=DIFFER; status=1; fi
65 rm -f "$dec"
66
67 ffmpeg -v error -y -i "$stream" -fps_mode passthrough -f rawvideo -pix_fmt yuv420p "$ffdec"
68 if cmp -s "$rec" "$ffdec"; then ff_ok=match; else ff_ok=DIFFER; status=1; fi
69 rm -f "$ffdec"
70
71 y_psnr=$(psnr "$rec")
72 rm -f "$rec"
73 printf '%-8s %-12s %-10s %-10s %-8s %-10s %-12s\n' "$kbps" "$achieved" "$err" "$y_psnr" "$qp" "$ec_ok" "$ff_ok"
74done
75exit $status