forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

h264-lane-check.sh110 lines, 4.3 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# h264-lane-check.sh: recon-match, rate-control and thread-speed checks for the
3# ec-h264 encoder, driven through the h264_clip_encode example.
4#
5# Usage (from the repo root, normally via scripts/remote-run.sh):
6# scripts/h264-lane-check.sh recon md5 of ffmpeg's decode vs the encoder's reconstruction
7# scripts/h264-lane-check.sh rate [size:bitrate:kind ...]
8# 60 s rate-control runs, achieved vs target bitrate
9# scripts/h264-lane-check.sh fps 1080p30 encode fps at 1, 2, 4 and 6 threads
10#
11# Sources are ffmpeg testsrc2 with temporal noise. No real camera clip exists in
12# fixtures/video (every one is generated from testsrc2), so that is the input.
13# Work files go to ${H264_WORK:-$TMPDIR/h264-lane-check} and nothing is left in /tmp.
14set -euo pipefail
15
16mode=${1:-}
17root=$(cd "$(dirname "$0")/.." && pwd)
18work=${H264_WORK:-${TMPDIR:-$HOME/tmp}/h264-lane-check}
19mkdir -p "$work"
20cd "$root"
21
22cargo build --release -q -p ec-h264 --example h264_clip_encode
23enc="$root/target/release/examples/h264_clip_encode"
24[ -n "${CARGO_TARGET_DIR:-}" ] && enc="$CARGO_TARGET_DIR/release/examples/h264_clip_encode"
25
26# $1 size, $2 rate, $3 duration seconds, $4 source kind (testsrc2 | noisy)
27source_graph() {
28 local g="testsrc2=size=$1:rate=$2:duration=$3"
29 [ "$4" = noisy ] && g="$g,noise=alls=24:allf=t"
30 printf '%s' "$g"
31}
32
33recon_case() {
34 local size=$1 kind=$2 threads=$3 qp=$4 bitrate=$5 tag=$6
35 local prefix="$work/$tag"
36 ffmpeg -nostdin -v error -f lavfi -i "$(source_graph "$size" 30 1 "$kind")" \
37 -pix_fmt yuv420p -f rawvideo - \
38 | "$enc" - "${size%x*}" "${size#*x}" 30 "$bitrate" "$qp" "$threads" "$prefix" >/dev/null
39 ffmpeg -nostdin -v error -i "$prefix.h264" -f rawvideo -pix_fmt yuv420p "$prefix.dec.yuv"
40 local a b
41 a=$(md5sum < "$prefix.recon.yuv" | cut -d' ' -f1)
42 b=$(md5sum < "$prefix.dec.yuv" | cut -d' ' -f1)
43 local verdict=MATCH
44 [ "$a" = "$b" ] || verdict=MISMATCH
45 printf '%-10s %-8s t=%d qp=%-3s br=%-8s recon=%s decode=%s %s\n' \
46 "$size" "$kind" "$threads" "$qp" "$bitrate" "$a" "$b" "$verdict"
47 rm -f "$prefix.h264" "$prefix.recon.yuv" "$prefix.dec.yuv"
48 [ "$verdict" = MATCH ]
49}
50
51rate_case() {
52 # $1 size, $2 bitrate, $3 kind; a 60 s clip piped straight into the encoder.
53 local size=$1 target=$2 kind=$3 threads=${RATE_THREADS:-4}
54 local fps=30
55 local out
56 out=$(ffmpeg -nostdin -v error -f lavfi -i "$(source_graph "$size" "$fps" 60 "$kind")" \
57 -pix_fmt yuv420p -f rawvideo - \
58 | "$enc" - "${size%x*}" "${size#*x}" "$fps" "$target" 26 "$threads" none)
59 local bytes frames
60 bytes=$(echo "$out" | sed -n 's/.*bytes \([0-9]*\).*/\1/p')
61 frames=$(echo "$out" | sed -n 's/^frames \([0-9]*\).*/\1/p')
62 awk -v s="$size" -v k="$kind" -v t="$target" -v b="$bytes" -v f="$frames" -v r="$fps" '
63 BEGIN { a = b * 8 / (f / r); d = (a / t - 1) * 100;
64 printf "%-10s %-8s target=%9d achieved=%11.0f b/s error=%+6.2f%% %s\n",
65 s, k, t, a, d, (d >= -5 && d <= 5) ? "PASS" : "FAIL" }'
66}
67
68fps_case() {
69 # $1 threads: 10 s of 1080p30 noisy testsrc2 at 4 Mb/s, timed inside the encoder.
70 local threads=$1
71 ffmpeg -nostdin -v error -f lavfi -i "$(source_graph 1920x1080 30 10 noisy)" \
72 -pix_fmt yuv420p -f rawvideo - \
73 | "$enc" - 1920 1080 30 4000000 26 "$threads" none
74}
75
76case "$mode" in
77 recon)
78 # Even sizes only: the encoder refuses odd dimensions. Covers sizes that are
79 # not macroblock multiples, odd macroblock counts, and 1080p.
80 fail=0
81 for size in 176x144 64x48 350x242 210x118 352x288 1280x720 1920x1080; do
82 for kind in testsrc2 noisy; do
83 recon_case "$size" "$kind" 4 30 0 "rc-$size-$kind-cqp" || fail=1
84 recon_case "$size" "$kind" 4 0 2000000 "rc-$size-$kind-abr" || fail=1
85 done
86 done
87 exit $fail
88 ;;
89 rate)
90 # Cases are size:bitrate:kind arguments, or all six noisy cases by default.
91 cases=("${@:2}")
92 [ ${#cases[@]} -gt 0 ] || cases=(1280x720:1000000:noisy 1280x720:2000000:noisy
93 1280x720:4000000:noisy 1920x1080:1000000:noisy 1920x1080:4000000:noisy
94 1920x1080:8000000:noisy)
95 for c in "${cases[@]}"; do
96 IFS=: read -r size target kind <<<"$c"
97 rate_case "$size" "$target" "$kind"
98 done
99 ;;
100 fps)
101 for t in 1 2 4 6; do
102 printf 'threads=%d ' "$t"
103 fps_case "$t"
104 done
105 ;;
106 *)
107 sed -n '2,12p' "$0" >&2
108 exit 2
109 ;;
110esac