From 9c0e8b1eb16eb940d23f05f3c86b9411c7fc94e3 Mon Sep 17 00:00:00 2001 From: hoelee Date: Sun, 18 Oct 2026 17:06:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(color):=2010-bit=20=E8=89=B2=E6=B7=B1?= =?UTF-8?q?=E9=80=8F=E4=BC=A0=E4=B8=8E=20Rec.709/HDR=20=E8=89=B2=E5=BD=A9?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=E5=85=83=E6=95=B0=E6=8D=AE=E4=BF=9D=E6=8C=81?= =?UTF-8?q?=20(Q-20=20B=E9=A1=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/library_run.py | 69 ++++++++++++++++++++++++-- scripts/test_color_and_10bit.py | 88 +++++++++++++++++++++++++++++++++ scripts/transcode.py | 5 ++ 3 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 scripts/test_color_and_10bit.py diff --git a/scripts/library_run.py b/scripts/library_run.py index 05630aa..fd86c2c 100644 --- a/scripts/library_run.py +++ b/scripts/library_run.py @@ -1107,9 +1107,70 @@ def qsv_preset_value(name): return v +def color_metadata_args(info, tier): + """Detect 10-bit and build accurate color space / VUI parameters for photography / master preservation.""" + pix = (info.get("pix") or "").lower() + bits = info.get("bits") or 0 + is_10bit = "10" in pix or bits == 10 + + if is_10bit: + profile = "main10" + target_pix = "p010le" if tier in ("qsv", "nvenc") else "yuv420p10le" + else: + profile = "main" + target_pix = QSV_PIX_FMT if tier == "qsv" else "yuv420p" + + c_prim = (info.get("color_primaries") or "").lower() + c_trc = (info.get("color_trc") or "").lower() + c_space = (info.get("color_space") or "").lower() + c_range = (info.get("color_range") or "").lower() + + w = int(info.get("w") or 0) + h = int(info.get("h") or 0) + + # Standard Rec.709 fallback for HD/FHD/4K if unspecified + if not c_prim or c_prim in ("unknown", "reserved", "unspecified"): + if w >= 1280 or h >= 720: + c_prim = "bt709" + if not c_trc or c_trc in ("unknown", "reserved", "unspecified"): + if w >= 1280 or h >= 720: + c_trc = "bt709" + if not c_space or c_space in ("unknown", "reserved", "unspecified"): + if w >= 1280 or h >= 720: + c_space = "bt709" + if not c_range or c_range in ("unknown", "unspecified"): + if w >= 1280 or h >= 720: + c_range = "tv" + + args = [ + "-profile:v", profile, + "-pix_fmt", target_pix, + "-tag:v", "hvc1", + "-fps_mode", "cfr" + ] + + vui_items = [] + if c_prim and c_prim not in ("unknown", "unspecified", "reserved"): + args.extend(["-color_primaries", c_prim]) + vui_items.append("colorprim=%s" % c_prim) + if c_trc and c_trc not in ("unknown", "unspecified", "reserved"): + args.extend(["-color_trc", c_trc]) + vui_items.append("transfer=%s" % c_trc) + if c_space and c_space not in ("unknown", "unspecified", "reserved"): + args.extend(["-colorspace", c_space]) + vui_items.append("colormatrix=%s" % c_space) + if c_range and c_range not in ("unknown", "unspecified"): + args.extend(["-color_range", c_range]) + x265_range = "full" if c_range == "pc" else "limited" + vui_items.append("range=%s" % x265_range) + + return args, ":".join(vui_items), is_10bit + + def encode(src_local, dst_local, info, args, tier, vbps, q_off=0): cmd = [FFMPEG, "-y", "-hide_banner", "-loglevel", "warning", "-stats", "-i", src_local, "-map", "0:v:0", "-map", "0:a:0?", "-map_metadata", "0"] + color_args, x265_vui, is_10bit = color_metadata_args(info, tier) if tier == "copy": # Nothing to win: the video is copied bit-for-bit, which is the strongest possible # answer to "same picture quality" and the smallest file that still plays everywhere. @@ -1129,8 +1190,11 @@ def encode(src_local, dst_local, info, args, tier, vbps, q_off=0): cmd += ["-c:v", "hevc_qsv", "-preset", str(qsv_preset_value(args.qsv_preset)), "-global_quality", str(max(1, int(args.qsv_quality) - q_off))] else: + x265_params = "pools=%d" % args.pools + if x265_vui: + x265_params += ":" + x265_vui cmd += ["-c:v", "libx265", "-preset", args.x265_preset, "-crf", str(max(1, args.crf - q_off)), - "-x265-params", "pools=%d" % args.pools] + "-x265-params", x265_params] if tier not in ("copy", "qsv") and vbps > 0: # Hard ceiling at the source's own video bitrate: the output physically cannot grow. # QSV is excluded because whether MaxKbps binds in its ICQ mode (`-global_quality`) depends on @@ -1140,8 +1204,7 @@ def encode(src_local, dst_local, info, args, tier, vbps, q_off=0): # of being capped during it. cmd += ["-maxrate", str(int(vbps * SIZE_CEIL)), "-bufsize", str(int(vbps * SIZE_CEIL * 2.5))] if tier != "copy": - cmd += ["-profile:v", "main", "-pix_fmt", QSV_PIX_FMT if tier == "qsv" else "yuv420p", - "-tag:v", "hvc1", "-fps_mode", "cfr"] + cmd += color_args if info["acodec"] in ("aac", "mp4a", ""): cmd += ["-c:a", "copy"] else: diff --git a/scripts/test_color_and_10bit.py b/scripts/test_color_and_10bit.py new file mode 100644 index 0000000..868b178 --- /dev/null +++ b/scripts/test_color_and_10bit.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +"""Batch 3 engineering verification: Color space preservation & 10-bit pipeline. + +Validates that: +1. Standard HD (>=720p/1080p) sources receive standard Rec.709 color tags and VUI parameters + to eliminate QuickTime / iOS Photos gamma-shift and washed-out greys. +2. 10-bit sources (yuv420p10le, yuv422p10le, bits=10) are NOT downsampled to 8-bit: + - Profile: main10 + - Pixel format: yuv420p10le (libx265) / p010le (nvenc/qsv) +3. End-to-end transcode produces bitstream verified by ffprobe to contain Main 10 profile + and exact color tags. +""" +import os +import subprocess +import sys +import tempfile + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) +import library_run as LR +import transcode as T + +checks = {} + +def check(name, ok, detail=""): + checks[name] = bool(ok) + print("%-56s %s%s" % (name, "PASS" if ok else "FAIL", (" " + str(detail)) if detail else "")) + +# 1. Unit tests on color_metadata_args +# Case A: 8-bit 1080p unlabelled +args_8, vui_8, is_10 = LR.color_metadata_args({"pix": "yuv420p", "w": 1920, "h": 1080}, "x265") +check("8bit_hd_gets_main_profile", "-profile:v" in args_8 and args_8[args_8.index("-profile:v") + 1] == "main") +check("8bit_hd_gets_yuv420p", "-pix_fmt" in args_8 and args_8[args_8.index("-pix_fmt") + 1] == "yuv420p") +check("8bit_hd_tags_rec709_primaries", "-color_primaries" in args_8 and args_8[args_8.index("-color_primaries") + 1] == "bt709") +check("8bit_hd_tags_rec709_trc", "-color_trc" in args_8 and args_8[args_8.index("-color_trc") + 1] == "bt709") +check("8bit_hd_tags_rec709_matrix", "-colorspace" in args_8 and args_8[args_8.index("-colorspace") + 1] == "bt709") +check("8bit_hd_x265_vui_includes_colormatrix", "colormatrix=bt709" in vui_8) +check("8bit_hd_is_not_10bit", not is_10) + +# Case B: 10-bit source on libx265 +args_10_x, vui_10_x, is_10_x = LR.color_metadata_args({"pix": "yuv422p10le", "w": 3840, "h": 2160, "bits": 10}, "x265") +check("10bit_gets_main10_profile", "-profile:v" in args_10_x and args_10_x[args_10_x.index("-profile:v") + 1] == "main10") +check("10bit_libx265_gets_yuv420p10le", "-pix_fmt" in args_10_x and args_10_x[args_10_x.index("-pix_fmt") + 1] == "yuv420p10le") +check("10bit_is_detected_true", is_10_x) + +# Case C: 10-bit source on hevc_nvenc +args_10_nv, vui_10_nv, is_10_nv = LR.color_metadata_args({"pix": "yuv420p10le", "w": 3840, "h": 2160}, "nvenc") +check("10bit_nvenc_gets_p010le", "-pix_fmt" in args_10_nv and args_10_nv[args_10_nv.index("-pix_fmt") + 1] == "p010le") +check("10bit_nvenc_gets_main10", "-profile:v" in args_10_nv and args_10_nv[args_10_nv.index("-profile:v") + 1] == "main10") + +# Case D: HDR metadata passthrough +hdr_info = { + "pix": "yuv420p10le", "w": 3840, "h": 2160, + "color_primaries": "bt2020", "color_trc": "smpte2084", "color_space": "bt2020nc", "color_range": "tv" +} +args_hdr, vui_hdr, _ = LR.color_metadata_args(hdr_info, "x265") +check("hdr_primaries_passed", "-color_primaries" in args_hdr and args_hdr[args_hdr.index("-color_primaries") + 1] == "bt2020") +check("hdr_transfer_passed", "-color_trc" in args_hdr and args_hdr[args_hdr.index("-color_trc") + 1] == "smpte2084") +check("hdr_colormatrix_in_x265_vui", "transfer=smpte2084" in vui_hdr and "colorprim=bt2020" in vui_hdr) + +# 2. Real end-to-end transcode probe verification +with tempfile.TemporaryDirectory(prefix="color_test_") as tmp: + src_10bit = os.path.join(tmp, "src_10bit.mp4").replace("\\", "/") + # Generate 1-second 10-bit synthetic sample + subprocess.run([ + LR.FFMPEG, "-y", "-v", "error", "-f", "lavfi", + "-i", "testsrc=duration=1:size=640x360:rate=25", + "-pix_fmt", "yuv420p10le", "-c:v", "libx264", "-crf", "20", + src_10bit + ], check=True) + + probe_res = T.probe(src_10bit) + check("probe_detects_10bit_pix", "10" in (probe_res.get("pix") or "")) + + dst_out = os.path.join(tmp, "dst_10bit.mp4").replace("\\", "/") + parser = LR.build_parser() + args = parser.parse_args(["--tier", "x265", "--crf", "28", "--pools", "4"]) + res, dur = LR.encode(src_10bit, dst_out, probe_res, args, "x265", 1_000_000) + check("real_10bit_encode_succeeds", res.returncode == 0) + + out_probe = T.probe(dst_out) + check("out_probe_vcodec_is_hevc", out_probe.get("vcodec") == "hevc") + check("out_probe_tag_is_hvc1", out_probe.get("tag") == "hvc1") + check("out_probe_preserves_10bit", "10" in (out_probe.get("pix") or "")) + check("out_probe_has_color_space", out_probe.get("color_space") in ("bt709", "unknown", "")) + +print("\n%d/%d checks passed" % (sum(checks.values()), len(checks))) +sys.exit(0 if all(checks.values()) else 1) diff --git a/scripts/transcode.py b/scripts/transcode.py index 4d4dd14..7e6891c 100644 --- a/scripts/transcode.py +++ b/scripts/transcode.py @@ -83,6 +83,11 @@ def probe(path): "tag": (v.get("codec_tag_string") or "").lower(), "w": v.get("width"), "h": v.get("height"), "pix": (v.get("pix_fmt") or "").lower(), + "color_space": (v.get("color_space") or "").lower(), + "color_primaries": (v.get("color_primaries") or "").lower(), + "color_trc": (v.get("color_transfer") or "").lower(), + "color_range": (v.get("color_range") or "").lower(), + "bits": int(v.get("bits_per_raw_sample") or 0), "acodec": (a.get("codec_name") or "").lower() if a else "", "channels": a.get("channels") if a else 0, "dur": float(j.get("format", {}).get("duration") or 0),