[CmdletBinding()]
param(
  [Parameter(Mandatory = $true)][string]$InputHevc,
  [Parameter(Mandatory = $true)][string]$RpuFile,
  [Parameter(Mandatory = $true)][string]$OutputHevc,
  [string]$DoviTool = ""
)

$projectRoot = Split-Path -Parent $PSScriptRoot
if ([string]::IsNullOrWhiteSpace($DoviTool)) { $DoviTool = Join-Path $projectRoot "artifacts\m1\bin\dovi_tool.exe" }
foreach ($path in @($DoviTool, $InputHevc, $RpuFile)) {
  if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { throw "Missing file: $path" }
}
$resolvedOutput = [System.IO.Path]::GetFullPath($OutputHevc)
$outputDirectory = Split-Path -Parent $resolvedOutput
if (-not (Test-Path -LiteralPath $outputDirectory -PathType Container)) { New-Item -ItemType Directory -Path $outputDirectory -Force | Out-Null }

& $DoviTool inject-rpu --input $InputHevc --rpu-in $RpuFile --output $resolvedOutput
if ($LASTEXITCODE -ne 0) { throw "dovi_tool inject-rpu failed with exit code $LASTEXITCODE" }
Write-Output "Injected frame-indexed RPU stream: $resolvedOutput"
