forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBf849ee9Tahinli23 days ago3917 commits

ffmpeg-psnr.sh40 lines, 1.7 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# ffmpeg-psnr.sh: per-plane PSNR of a decoded raw I420 file against its source,
3# computed by ffmpeg's own psnr filter (per-frame values from its stats file).
4#
5# Prints `frames psnr_y psnr_u psnr_v psnr_yuv` where each plane is the mean over
6# frames of ffmpeg's per-frame PSNR, and psnr_yuv is the JVET 6:1:1 weighting of
7# those plane means, (6 Y + U + V) / 8.
8#
9# Usage: scripts/ffmpeg-psnr.sh <ref.yuv> <dec.yuv> <width> <height>
10# Environment: PIX (default yuv420p; yuv420p10le for 10-bit files).
11set -euo pipefail
12
13ref=$1
14dec=$2
15w=$3
16h=$4
17stats=$(mktemp "$HOME/.cache/psnr-stats.XXXXXX")
18trap 'rm -f "$stats"' EXIT
19ffmpeg -v error -f rawvideo -pix_fmt ${PIX:-yuv420p} -s "${w}x${h}" -i "$dec" \
20 -f rawvideo -pix_fmt ${PIX:-yuv420p} -s "${w}x${h}" -i "$ref" \
21 -lavfi "[0:v][1:v]psnr=stats_file=$stats" -f null -
22awk '
23 # ffmpeg prints inf for a frame that matches exactly; such frames are counted
24 # apart and left out of the means, which is what the PSNR of an exact frame is.
25 {
26 for (i = 1; i <= NF; i++) {
27 split($i, kv, ":")
28 if (kv[1] == "psnr_y" || kv[1] == "psnr_u" || kv[1] == "psnr_v") {
29 if (kv[2] == "inf") { if (kv[1] == "psnr_y") exact++; continue }
30 if (kv[1] == "psnr_y") { sy += kv[2]; ny++ }
31 if (kv[1] == "psnr_u") { su += kv[2]; nu++ }
32 if (kv[1] == "psnr_v") { sv += kv[2]; nv++ }
33 }
34 }
35 }
36 END {
37 if (ny == 0) { printf "frames=%d exact=%d psnr_y=inf psnr_u=inf psnr_v=inf psnr_yuv=inf\n", exact, exact; exit 0 }
38 y = sy / ny; u = su / nu; v = sv / nv
39 printf "frames=%d exact=%d psnr_y=%.4f psnr_u=%.4f psnr_v=%.4f psnr_yuv=%.4f\n", ny + exact, exact, y, u, v, (6 * y + u + v) / 8
40 }' "$stats"