forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

hevc-gop-roundtrip.sh60 lines, 2.7 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# hevc-gop-roundtrip.sh: encode lavfi clips with ec-h265's IDR+P encoder, decode the
3# stream with ec-h265 and with ffmpeg, and compare each against the encoder's
4# reconstruction. Also reports the size and PSNR of the GOP against an intra-only
5# stream at the same QP.
6#
7# Usage: scripts/hevc-gop-roundtrip.sh (run it where the cargo build happens;
8# scripts/remote-run.sh hevcencp -- scripts/hevc-gop-roundtrip.sh)
9#
10# Environment: DATA (default $HOME/.cache/hevcencp-data), GOP (keyframe interval,
11# default 8), QP (default 30), FRAMES (default 30), CLIPS (default "352x288 1280x720 350x286"),
12# EXTRA (encoder flags, e.g. "sh" for sign data hiding).
13set -euo pipefail
14
15here=$(cd "$(dirname "$0")/.." && pwd)
16data=${DATA:-$HOME/.cache/hevcencp-data}
17gop=${GOP:-8}
18qp=${QP:-30}
19frames=${FRAMES:-30}
20clips=${CLIPS:-"352x288 1280x720 350x286"}
21mkdir -p "$data"
22
23cargo build --release -q -p ec-h265 --example hevc_gop --manifest-path "$here/Cargo.toml"
24target=$(cargo metadata --format-version 1 --no-deps --manifest-path "$here/Cargo.toml" \
25 | python3 -I -c 'import json,sys; print(json.load(sys.stdin)["target_directory"])')
26gopbin="$target/release/examples/hevc_gop"
27
28status=0
29printf '%-10s %-6s %-8s %-7s %-12s %-12s %-18s %-18s\n' clip gop bytes ratio psnr_y psnr_yuv ec_h265_decode ffmpeg_decode
30for size in $clips; do
31 w=${size%x*}
32 h=${size#*x}
33 src="$data/src-$size.yuv"
34 if [ ! -s "$src" ]; then
35 # testsrc2 with a slow vertical scroll: real motion, not a static chart.
36 ffmpeg -v error -y -f lavfi -i "testsrc2=size=${size}:rate=30" \
37 -vf "scroll=v=0.02,format=yuv420p" -frames:v "$frames" -f rawvideo "$src"
38 fi
39 for keyint in 1 "$gop"; do
40 tag="$size-k$keyint"
41 stream="$data/$tag.hevc"
42 recon="$data/$tag.recon.yuv"
43 dec="$data/$tag.ec.yuv"
44 ff="$data/$tag.ff.yuv"
45 "$gopbin" encode "$src" "$w" "$h" "$keyint" "$qp" "$stream" "$recon" ${EXTRA:-} >/dev/null
46 "$gopbin" decode "$stream" "$dec" >/dev/null
47 ffmpeg -v error -y -i "$stream" -f rawvideo -pix_fmt yuv420p "$ff"
48 if cmp -s "$dec" "$recon"; then ec=exact; else ec=MISMATCH; status=1; fi
49 if cmp -s "$ff" "$recon"; then fv=exact; else fv=MISMATCH; status=1; fi
50 bytes=$(stat -c %s "$stream")
51 if [ "$keyint" = 1 ]; then intra_bytes=$bytes; fi
52 ratio=$(python3 -I -c "print(f'{$bytes/$intra_bytes:.3f}')")
53 metrics=$(python3 -I "$here/scripts/yuv_psnr.py" "$w" "$h" "$src" "$recon")
54 py=$(sed -n 's/.*psnr_y=\([^ ]*\).*/\1/p' <<<"$metrics")
55 pyuv=$(sed -n 's/.*psnr_yuv=\([^ ]*\).*/\1/p' <<<"$metrics")
56 printf '%-10s %-6s %-8s %-7s %-12s %-12s %-18s %-18s\n' \
57 "$size" "$keyint" "$bytes" "$ratio" "$py" "$pyuv" "$ec" "$fv"
58 done
59done
60exit $status