forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB3d2e08fTahinli6 days ago3929 commits

yuv_psnr.py54 lines, 1.8 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""Per-frame and mean PSNR between two raw I420 files of the same geometry.
3
4Usage: python3 -I yuv_psnr.py <width> <height> <ref.yuv> <test.yuv> [bits]
5
6`bits` is 8 (default) or 10; at 10 the files hold 16-bit little-endian samples
7and the peak is 1023.
8
9Prints the mean luma PSNR over frames, the mean PSNR over all three planes
10(sample-weighted), and the frame count. Identical files give inf.
11"""
12import array
13import math
14import sys
15
16
17def main() -> int:
18 w, h = int(sys.argv[1]), int(sys.argv[2])
19 bits = int(sys.argv[5]) if len(sys.argv) > 5 else 8
20 peak = (1 << bits) - 1
21 cw, ch = (w + 1) // 2, (h + 1) // 2
22 sizes = [w * h, cw * ch, cw * ch]
23 frame = sum(sizes)
24 code = "H" if bits > 8 else "B"
25 ref = array.array(code)
26 ref.frombytes(open(sys.argv[3], "rb").read())
27 test = array.array(code)
28 test.frombytes(open(sys.argv[4], "rb").read())
29 n = min(len(ref), len(test)) // frame
30 if len(ref) != len(test):
31 print(f"length differs: ref={len(ref)} test={len(test)}")
32 y_psnr, all_sse, all_count = [], 0, 0
33 for f in range(n):
34 base = f * frame
35 sse_planes = []
36 off = base
37 for size in sizes:
38 a = ref[off:off + size]
39 b = test[off:off + size]
40 sse = sum((x - y) * (x - y) for x, y in zip(a, b))
41 sse_planes.append((sse, size))
42 off += size
43 sse_y, cnt_y = sse_planes[0]
44 y_psnr.append(10 * math.log10(peak * peak * cnt_y / sse_y) if sse_y else math.inf)
45 all_sse += sum(s for s, _ in sse_planes)
46 all_count += frame
47 mean_y = sum(y_psnr) / len(y_psnr) if y_psnr else 0.0
48 all_psnr = 10 * math.log10(peak * peak * all_count / all_sse) if all_sse else math.inf
49 print(f"frames={n} psnr_y={mean_y:.4f} psnr_yuv={all_psnr:.4f}")
50 return 0
51
52
53if __name__ == "__main__":
54 sys.exit(main())