219 lines
8.5 KiB
Python
219 lines
8.5 KiB
Python
"""原生音视频只认自己的审计报告及本机成功任务,不能借用静音证书。"""
|
||
|
||
import hashlib
|
||
import json
|
||
import re
|
||
import sqlite3
|
||
import wave
|
||
from contextlib import closing
|
||
from pathlib import Path
|
||
|
||
|
||
def capability(settings, profile):
|
||
from .video_profiles import (
|
||
H3_NATIVE_AV_TP2_PROFILE,
|
||
H3_REVISION,
|
||
H3_RUNTIME_COMMIT,
|
||
H3_TP2_RUNTIME_COMMIT,
|
||
)
|
||
|
||
pair = profile == H3_NATIVE_AV_TP2_PROFILE
|
||
maximum = 158 if pair else 345
|
||
result = {
|
||
"available": False,
|
||
"profile": profile,
|
||
"revision": H3_REVISION,
|
||
"runtime_revision": H3_TP2_RUNTIME_COMMIT if pair else H3_RUNTIME_COMMIT,
|
||
"adapter_contract_version": "h3-fl2va-native-av-tp2-v1"
|
||
if pair
|
||
else "h3-fl2va-native-av-v1",
|
||
"input_modes": ["first_frame"] if pair else ["first_frame", "first_last_frame"],
|
||
"output_policy": "full_native",
|
||
"native_frame_counts": list(range(124, maximum + 1, 17)),
|
||
"fps": 24,
|
||
"canvases": [],
|
||
"upscale": False,
|
||
"audio_output": True,
|
||
"audio_sample_rate": 32000,
|
||
"audio_channels": 2,
|
||
"gpu_count": 2 if pair else 1,
|
||
"report_sha256": None,
|
||
"decision": None,
|
||
"audited": False,
|
||
}
|
||
flag = "native_dialogue_tp2_enabled" if pair else "native_dialogue_enabled"
|
||
if not getattr(settings, flag, False):
|
||
return result
|
||
try:
|
||
from .store import open_confined_file
|
||
|
||
path = (
|
||
settings.app_root
|
||
/ "config"
|
||
/ ("native-av-tp2-profile.json" if pair else "native-av-profile.json")
|
||
)
|
||
with open_confined_file(path, settings.workspace_root) as stream:
|
||
raw = stream.read(65537)
|
||
if len(raw) > 65536:
|
||
raise ValueError("Native AV report is too large")
|
||
report = json.loads(raw)
|
||
fields = (
|
||
"profile",
|
||
"revision",
|
||
"runtime_revision",
|
||
"adapter_contract_version",
|
||
"input_modes",
|
||
"output_policy",
|
||
"native_frame_counts",
|
||
"fps",
|
||
"upscale",
|
||
"audio_output",
|
||
"audio_sample_rate",
|
||
"audio_channels",
|
||
"gpu_count",
|
||
)
|
||
if (
|
||
any(report.get(key) != result[key] for key in fields)
|
||
or report.get("decision") != "pass"
|
||
or report.get("audited") is not True
|
||
or report.get("task_type") != "video.generate"
|
||
):
|
||
raise ValueError("Native AV report identity differs from this contract")
|
||
declared_canvases = report.get("canvases")
|
||
allowed_canvases = [[1344, 768], [768, 1344]]
|
||
if (
|
||
not isinstance(declared_canvases, list)
|
||
or not 1 <= len(declared_canvases) <= len(allowed_canvases)
|
||
or any(
|
||
not isinstance(canvas, list)
|
||
or len(canvas) != 2
|
||
or any(type(value) is not int for value in canvas)
|
||
or canvas not in allowed_canvases
|
||
for canvas in declared_canvases
|
||
)
|
||
or declared_canvases
|
||
!= [canvas for canvas in allowed_canvases if canvas in declared_canvases]
|
||
):
|
||
raise ValueError("Native AV report has invalid or duplicate audited canvases")
|
||
checks = report.get("checks", {})
|
||
required = (
|
||
"native_audio",
|
||
"tail_preserved",
|
||
"av_sync",
|
||
"quality",
|
||
"memory",
|
||
"cancel",
|
||
"preemption",
|
||
)
|
||
if pair:
|
||
required += ("gpu_group",)
|
||
if not isinstance(checks, dict) or any(
|
||
checks.get(key) is not True for key in required
|
||
):
|
||
raise ValueError("Native AV report lacks independent acceptance checks")
|
||
ids = report.get("validation_job_ids")
|
||
if (
|
||
not isinstance(ids, list)
|
||
or not (1 if pair else 2) <= len(ids) <= 8
|
||
or len(set(ids)) != len(ids)
|
||
or any(
|
||
not isinstance(j, str) or re.fullmatch("[0-9a-f]{32}", j) is None
|
||
for j in ids
|
||
)
|
||
):
|
||
raise ValueError(
|
||
"Native AV report requires bounded calibration job identities"
|
||
)
|
||
canvases, modes = set(), set()
|
||
with closing(
|
||
sqlite3.connect(settings.db_path.as_uri() + "?mode=ro", uri=True, timeout=1)
|
||
) as db:
|
||
for job_id in ids:
|
||
row = db.execute(
|
||
"SELECT result_json,parameters_json FROM jobs WHERE id=? AND task_type='video.generate' AND state='succeeded'",
|
||
(job_id,),
|
||
).fetchone()
|
||
if row is None:
|
||
raise ValueError("Missing successful native AV calibration job")
|
||
stored, params = (json.loads(x) for x in row)
|
||
meta = stored["metadata"]
|
||
av = meta["native_av"]
|
||
if (
|
||
params.get("profile") != profile
|
||
or meta.get("profile") != profile
|
||
or meta.get("revision") != result["revision"]
|
||
or meta.get("diffusers_source_revision")
|
||
!= result["runtime_revision"]
|
||
or params.get("num_frames") != maximum
|
||
or meta.get("native_frames") != maximum
|
||
or meta.get("requested_frames") != maximum
|
||
or params.get("fps") != 24
|
||
or av.get("output_policy") != "full_native"
|
||
or av.get("audio_trimmed") is not False
|
||
or av.get("sample_rate") != 32000
|
||
or av.get("channels") != 2
|
||
or type(av.get("audio_samples")) is not int
|
||
or av["audio_samples"] <= 0
|
||
or abs(av["audio_samples"] / 32000 - maximum / 24)
|
||
> 1 / 80 + 1 / 32000
|
||
):
|
||
raise ValueError(
|
||
"Native AV calibration output does not prove full native audio/video"
|
||
)
|
||
# 队列记录的成片和独立原声必须同时存在;仅有metadata不能声明完成。
|
||
artifacts = {
|
||
Path(entry["path"]).name: Path(entry["path"])
|
||
for entry in stored["outputs"]
|
||
}
|
||
if not {"video_raw.mp4", "audio_native.wav"}.issubset(artifacts):
|
||
raise ValueError("Native AV calibration has no archived audio")
|
||
for name in ("video_raw.mp4", "audio_native.wav"):
|
||
path = artifacts[name]
|
||
if not path.is_absolute() or not path.resolve().is_relative_to(
|
||
settings.outputs_dir
|
||
):
|
||
raise ValueError(
|
||
"Native AV calibration artifact escaped managed outputs"
|
||
)
|
||
with open_confined_file(path, settings.workspace_root) as stream:
|
||
if name.endswith(".wav"):
|
||
with wave.open(stream, "rb") as wav:
|
||
if (
|
||
wav.getframerate() != 32000
|
||
or wav.getnchannels() != 2
|
||
or wav.getsampwidth() != 2
|
||
or wav.getnframes() != av["audio_samples"]
|
||
):
|
||
raise ValueError(
|
||
"Native AV archived audio geometry changed"
|
||
)
|
||
elif not stream.read(1):
|
||
raise ValueError("Native AV archived video is empty")
|
||
canvases.add((params["width"], params["height"]))
|
||
modes.add(params.get("conditioning_mode", "first_frame"))
|
||
if canvases != {tuple(c) for c in declared_canvases} or modes != set(
|
||
result["input_modes"]
|
||
):
|
||
raise ValueError(
|
||
"Native AV report does not cover all declared input modes/canvases"
|
||
)
|
||
result.update(
|
||
available=True,
|
||
decision="pass",
|
||
audited=True,
|
||
canvases=declared_canvases,
|
||
report_sha256=hashlib.sha256(raw).hexdigest(),
|
||
)
|
||
except (
|
||
OSError,
|
||
ValueError,
|
||
TypeError,
|
||
KeyError,
|
||
AttributeError,
|
||
sqlite3.Error,
|
||
wave.Error,
|
||
EOFError,
|
||
):
|
||
pass
|
||
return result
|