Repository navigation
Conversation
|
Thanks for the fix and the detailed CML testing. The matrix mapping and the mixed BT.2020 safeguard look reasonable. One compatibility case is worth checking: a BT.2020 layer can cause multilayer composition to use the HDR render path, even for SDR content. With this change, a matrix-only, full-range BT.709 layer maps to CSpace_BT709_FullRange. The HDR prior-CSC code appears not to handle that color space, so the composition may fail with an invalid-parameter error. Could you add a CML multilayer test combining a BT.2020 layer with a matrix-only, full-range BT.709 layer, and confirm that the composition completes successfully? |
|
Hey @VickyZengg, I'm AFK for a couple of days, I'll come back to this PR when I'm back. |
The HDR kernel path converts a YUV input layer to RGB with a prior CSC and the result back to YUV with a post CSC, both chosen from the surface's colour space. The prior CSC handled only CSpace_BT601, CSpace_BT709 and the two BT.2020 spaces, and the post CSC had no case for CSpace_BT601_FullRange. Any other space made VpHal_HdrUpdatePerLayerPipelineStates return MOS_STATUS_INVALID_PARAMETER and the whole render failed. This path is taken when a BT.2020 layer is composed with other layers and when the output is BT.2020, so a full-range BT.709 layer next to a BT.2020 layer, or a full-range BT.709 input converted to BT.2020, could not be rendered. Add prior CSC types for full-range BT.601 and BT.709 input and a post CSC type for full-range BT.601 output. They use the same matrix coefficients as their limited-range counterparts; VpHal_HdrCalcYuvToRgbMatrix and VpHal_HdrCalcRgbToYuvMatrix already derive offset and excursion from the colour space they are given. The new enumerators are appended so the existing values do not change. CSpace_BT2020_FullRange keeps using the limited-range BT.2020 matrix, as before. Signed-off-by: Iouri Goussev <i.gouss@gmail.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Same change as the previous commit for the softlet HDR kernel packet and the HDR lite filter: a YUV input layer in CSpace_BT601_FullRange or CSpace_BT709_FullRange, or an output in CSpace_BT601_FullRange, was rejected with MOS_STATUS_INVALID_PARAMETER when the prior or post CSC was selected. VpUtils::GetCscMatrixForHDR now accepts the full-range spaces; GetYuvRangeAndOffset already knows their range. The post CSC of the 3D LUT calculation in the HDR kernel packet gets the full-range BT.601 case too, and the full-range BT.709 case it was missing: that output was already accepted when the post CSC was selected, but VpHal_HdrColorTransfer3dLut returned an error that its callers ignore, which left the LUT zeroed. Signed-off-by: Iouri Goussev <i.gouss@gmail.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
With VAProcColorStandardExplicit, VpSetColorStandardExplictly chose the YUV surface colour space from colour_primaries only and never read matrix_coefficients. va_vpp.h documents matrix_coefficients as the H.273 matrix used for the explicit standard. An app that sets matrix_coefficients=1 (BT.709) and leaves colour_primaries unspecified, as ffmpeg scale_vaapi does for out_color_matrix=bt709, got BT.601 output. Use matrix_coefficients when it names BT.709, BT.601 (5, 6) or BT.2020 NCL (9), and fall back to colour_primaries otherwise. Fixes intel#2066. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Iouri Goussev <i.gouss@gmail.com>
The VPHAL colour space holds matrix and gamut in one value, and choosing a BT.2020 space switches BT.2020 gamut conversion and HDR handling on or off. Taking the space from matrix_coefficients alone changed that choice when the two fields disagree about BT.2020: an input with matrix BT.709 and primaries BT.2020 lost its BT.2020 to BT.709 conversion, and an output with matrix BT.2020 NCL and primaries BT.709 went down the BT.2020 output path. When colour_primaries is specified and only one of matrix_coefficients and colour_primaries names BT.2020, choose from colour_primaries, as before the previous commit. All other cases still follow matrix_coefficients. Signed-off-by: Iouri Goussev <i.gouss@gmail.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
df8bab8 to
4caf472
Compare
|
Thanks, that was a real problem. I ran the case on CML and the PR as you reviewed it did break it: with a BT.2020 layer present, a matrix-only full-range BT.709 layer went from success to The cause is what you described. The prior CSC in the HDR path only accepts I pushed an updated branch, rebased on master, with two commits in front of the original two:
The HDR commits come first so that the mapping change does not land before the path can take its result. I built and ran master, the original two commits, and the full series, not each commit on its own. Results on UHD 630 (CML), three drivers built from source, ffmpeg
To check that the new prior CSC is numerically right and not only accepted, I sent seven colours through the one-layer case as limited range and as full range. The full-range outputs match the limited-range outputs within 1 on every component (28 full-range runs against 14 limited-range ones). One thing I have to flag so the table is not read as more than it is: whenever a two-layer call with a BT.2020 layer succeeds, on master as well, the second layer is not drawn (its area is all zero), and the first layer's colours shift. That is independent of this PR, so "success" above means the return status and the first layer. I filed it separately as #2076. On softlet the multi-layer BT.2020 trigger is not active (commented out in How to reproduce the main rowsNeeds ffmpeg with VAAPI and python3. Both inputs are the same colour (RGB 200,120,40), once as BT.709 full range and once as limited range. # Usage: LIBVA_DRIVERS_PATH=<build>/media_driver LIBVA_DRIVER_NAME=iHD sh hdr-fullrange-test.sh
python3 -c 'import sys; sys.stdout.buffer.write(bytes([131])*4096 + bytes([79,172])*1024)' > full.nv12 # RGB 200,120,40 as BT.709 full range
python3 -c 'import sys; sys.stdout.buffer.write(bytes([129])*4096 + bytes([85,166])*1024)' > lim.nv12 # the same colour, limited range
VA="-nostdin -y -loglevel error -init_hw_device vaapi=va:/dev/dri/renderD128 -filter_hw_device va"
IN="-f rawvideo -pix_fmt nv12 -s 64x64 -i"
BT2020="out_color_matrix=bt2020nc:out_color_primaries=bt2020:out_color_transfer=bt709:out_range=tv"
px() { od -An -tu1 -v "$1" | tr -s ' \n' ' ' | awk -v w="$2" '{printf "Y=%d U=%d V=%d", $1, $(w*64+1), $(w*64+2)}'; }
# 1. Two layers: a BT.2020 layer and a BT.709 full-range layer tagged with the matrix only
ffmpeg $VA $IN lim.nv12 $IN full.nv12 -filter_complex "[0:v]hwupload,scale_vaapi=$BT2020[a];[1:v]hwupload,scale_vaapi=out_color_matrix=bt709:out_range=full[b];[a][b]hstack_vaapi,hwdownload,format=nv12" -frames:v 1 -f rawvideo two.nv12 2>/dev/null
echo "two layers, 709 full matrix-only: exit=$?"
# 2. One layer to a BT.2020 output: limited range, then full range tagged with the matrix only, then fully tagged
for t in "lim.nv12 colorspace=bt709:color_primaries=bt709:range=tv" "full.nv12 colorspace=bt709:range=pc" "full.nv12 colorspace=bt709:color_primaries=bt709:range=pc"; do
set -- $t; rm -f one.nv12
ffmpeg $VA $IN $1 -vf "setparams=$2,hwupload,scale_vaapi=format=nv12:$BT2020,hwdownload,format=nv12" -frames:v 1 -f rawvideo one.nv12 2>/dev/null
echo "one layer, $2: exit=$? $([ -s one.nv12 ] && px one.nv12 64)"
doneWhat I get:
Full test script (54 runs) and how to run itBuild and select a driver, then run the script once per build: git clone https://github.com/intel/media-driver.git && cd media-driver # then check out the commit to test
mkdir ../build && cd ../build
cmake -G Ninja ../media-driver -DCMAKE_BUILD_TYPE=Release -DENABLE_KERNELS=ON -DENABLE_NONFREE_KERNELS=ON -DMEDIA_BUILD_FATAL_WARNINGS=OFF
ninja
LIBVA_DRIVERS_PATH=$PWD/media_driver LIBVA_DRIVER_NAME=iHD sh hdr-fullrange-matrix.sh /dev/dri/renderD128The first line the script prints is the driver version string, which contains the commit, so each run shows which build it used.
#!/bin/sh
# HDR kernel path with full-range BT.601/BT.709 layers. Needs ffmpeg with VAAPI, vainfo and python3.
# Usage: LIBVA_DRIVERS_PATH=<build>/media_driver LIBVA_DRIVER_NAME=iHD sh hdr-fullrange-matrix.sh [/dev/dri/renderD128]
# Each line: case, ffmpeg exit code, then Y U V of the output (two-layer cases: left layer | right layer).
DEV=${1:-/dev/dri/renderD128}
vainfo --display drm --device "$DEV" 2>&1 | grep "Driver version"
mk() { # mk R G B {709|601} {limited|full} file: flat 64x64 NV12 frame of one colour
python3 - "$@" <<'PY'
import sys
r,g,b=[int(x)/255 for x in sys.argv[1:4]]; kr,kb={"709":(0.2126,0.0722),"601":(0.299,0.114)}[sys.argv[4]]
y=kr*r+(1-kr-kb)*g+kb*b; cb=(b-y)/(2*(1-kb)); cr=(r-y)/(2*(1-kr))
Y,U,V=(255*y,128+255*cb,128+255*cr) if sys.argv[5]=="full" else (16+219*y,128+224*cb,128+224*cr)
Y,U,V=[min(255,max(0,round(v))) for v in (Y,U,V)]
open(sys.argv[6],"wb").write(bytes([Y])*4096+bytes([U,V])*1024)
PY
}
px() { # px file width: most common Y U V of each 64-pixel-wide layer, with the share of pixels if not uniform
python3 - "$@" <<'PY'
import sys,collections
f,W,H=sys.argv[1],int(sys.argv[2]),64
try: d=open(f,'rb').read()
except OSError: d=b''
if len(d)<W*H*3//2: print("no output"); sys.exit()
out=[]
for x0 in range(0,W,64):
c=collections.Counter((d[y*W+x],d[W*H+(y//2)*W+(x//2)*2],d[W*H+(y//2)*W+(x//2)*2+1]) for y in range(H) for x in range(x0,x0+64))
(v,n),=c.most_common(1); out.append("%d %d %d%s"%(*v,"" if n==64*H else " (%d%%)"%(100*n//(64*H))))
print(" | ".join(out))
PY
}
FF="ffmpeg -nostdin -y -loglevel error -init_hw_device vaapi=va:$DEV -filter_hw_device va"
IN="-f rawvideo -pix_fmt nv12 -s 64x64 -i"
one() { # one name file "setparams tags" "scale_vaapi options": one layer through scale_vaapi
rm -f o.nv12; $FF $IN "$2" -vf "setparams=$3,hwupload,scale_vaapi=format=nv12:$4,hwdownload,format=nv12" -frames:v 1 -f rawvideo o.nv12 2>/dev/null
echo "$1: exit=$? $(px o.nv12 64)"
}
two() { # two name fileA "tags A" fileB "tags B": each layer is tagged by scale_vaapi after upload, then hstack_vaapi
rm -f o.nv12; $FF $IN "$2" $IN "$4" -filter_complex "[0:v]hwupload,scale_vaapi=$3[a];[1:v]hwupload,scale_vaapi=$5[b];[a][b]hstack_vaapi,hwdownload,format=nv12" -frames:v 1 -f rawvideo o.nv12 2>/dev/null
echo "$1: exit=$? $(px o.nv12 128)"
}
O2020="out_color_matrix=bt2020nc:out_color_primaries=bt2020:out_color_transfer=bt709:out_range=tv"
T709="out_color_matrix=bt709:out_color_primaries=bt709:out_color_transfer=bt709"
T601="out_color_matrix=bt470bg:out_color_primaries=bt470bg"
echo "# S: one layer, BT.709 or BT.601 input, BT.2020 output"
for c in "255 0 0" "0 255 0" "0 0 255" "128 128 128" "200 120 40" "16 16 16" "235 235 235"; do
for m in 709 601; do
if [ $m = 709 ]; then cs=bt709; pr=bt709; else cs=smpte170m; pr=bt470bg; fi
mk $c $m limited lim.nv12; mk $c $m full full.nv12
one "S rgb($c) $m limited, tagged " lim.nv12 "colorspace=$cs:color_primaries=$pr:range=tv" "$O2020"
one "S rgb($c) $m full, tagged " full.nv12 "colorspace=$cs:color_primaries=$pr:range=pc" "$O2020"
one "S rgb($c) $m full, matrix only " full.nv12 "colorspace=$cs:range=pc" "$O2020"
done
done
echo "# M: two layers, layer A | layer B (the first layer's tags become the output's)"
mk 200 120 40 709 limited lim.nv12; mk 200 120 40 709 full full.nv12; mk 200 120 40 601 limited lim601.nv12; mk 200 120 40 601 full full601.nv12
two "M 709 limited | 709 limited (control, no HDR path)" lim.nv12 "$T709:out_range=tv" lim.nv12 "$T709:out_range=tv"
two "M 2020 | 709 limited, tagged " lim.nv12 "$O2020" lim.nv12 "$T709:out_range=tv"
two "M 2020 | 709 full, tagged " lim.nv12 "$O2020" full.nv12 "$T709:out_range=full"
two "M 2020 | 709 full, matrix only " lim.nv12 "$O2020" full.nv12 "out_color_matrix=bt709:out_range=full"
two "M 2020 | 601 full, tagged " lim.nv12 "$O2020" full601.nv12 "$T601:out_range=full"
two "M 2020 | 601 full, matrix only " lim.nv12 "$O2020" full601.nv12 "out_color_matrix=bt470bg:out_range=full"
two "M 709 limited, tagged | 2020 " lim.nv12 "$T709:out_range=tv" lim.nv12 "$O2020"
two "M 709 full, tagged | 2020 " full.nv12 "$T709:out_range=full" lim.nv12 "$O2020"
two "M 709 full, matrix only | 2020 " full.nv12 "out_color_matrix=bt709:out_range=full" lim.nv12 "$O2020"
two "M 601 limited, tagged | 2020 " lim601.nv12 "$T601:out_range=tv" lim.nv12 "$O2020"
two "M 601 full, tagged | 2020 " full601.nv12 "$T601:out_range=full" lim.nv12 "$O2020"
two "M 601 full, matrix only | 2020 " full601.nv12 "out_color_matrix=bt470bg:out_range=full" lim.nv12 "$O2020"Full results on the three buildsValues are Y U V of the output; two-layer rows show left layer / right layer. "failed (251)" is ffmpeg's exit code when
Environment: UHD Graphics 630 (CML, 8086:9bc5), libva 2.24.1, gmmlib 22.10.0, ffmpeg 9.0.2, Linux 7.2.15. In addition I ran 15 two-layer cases with a full-range first layer (which makes the output full range) for three colours, and the ten cases from the description; all succeed on the updated branch and the ten cases match the description's table. |
With
VAProcColorStandardExplicit,VpSetColorStandardExplictlypicked the YUV surface colour space fromcolour_primariesonly and never readmatrix_coefficients.va_vpp.hdocumentsmatrix_coefficientsas the H.273 matrix for the explicit standard. So an app that setsmatrix_coefficients=1(BT.709) and leavescolour_primariesunspecified got BT.601 output. ffmpeg'sscale_vaapidoes this forout_color_matrix=bt709when the input frame carries no primaries.What changes
The YUV colour space now comes from
matrix_coefficientswhen it names BT.709 (1), BT.601 (5, 6) or BT.2020 NCL (9). Otherwisecolour_primariesdecides, as before. The legacy (media_libva_vp.c) and softlet (ddi_vp_functions.cpp) DDI files get the same change.One case keeps the old behaviour on purpose.
MEDIA_CSPACEholds matrix and gamut in one value, and choosing a BT.2020 space switches the BT.2020 gamut conversion and HDR handling on or off. So whencolour_primariesis specified and only one of the two fields says BT.2020,colour_primariesstill decides. I did not want this fix to trade a matrix error for a gamut error in those mixed cases.RGB surfaces are not touched.
HDR kernel path (added after review)
Choosing the colour space from
matrix_coefficientsmeans a full-range layer tagged with the matrix only now becomesCSpace_BT601_FullRangeorCSpace_BT709_FullRangeinstead of falling back to BT.601 limited. The HDR kernel path did not accept those spaces for its prior CSC (nor BT.601 full range for its post CSC), so such a layer next to a BT.2020 layer, or converted to a BT.2020 output, failed withVA_STATUS_ERROR_OPERATION_FAILED. Fully tagged full-range layers already failed that way on master.Two commits in front of the mapping change add the missing cases: one for the legacy path (
vp_common_hdr.h,vphal_render_hdr_base.cpp,vphal_render_hdr_g9_base.cpp), one for the softlet HDR kernel packet and HDR lite filter. They use the limited-range coefficients; the existing helpers take offset and excursion from the colour space. The test script and full results for this part are in the review reply below.References
libva,
va/va_vpp.h, onmatrix_coefficientsinVAProcColorProperties:and on the colour standards in general:
Rec. ITU-T H.273 (V4, 07/2024) defines colour primaries (8.1, Table 2), transfer characteristics (8.2, Table 3) and matrix coefficients (8.3, Table 4) as three separate code points. Section 8.3:
Table 4 rows this PR uses:
Table 2 gives the same meaning to colour primaries value 2 ("Image characteristics are unknown or are determined by the application"), which is why unspecified primaries do not count as a disagreement with the matrix.
Behaviour changes to review
matrix_coefficients/colour_primariesThe two 170M rows stand for every primaries value the driver does not recognise (anything other than 1, 5 and 9). Such surfaces used to fall to the default arm, BT.601 limited, whatever the matrix and range said. Now the matrix decides when it is one of 1, 5, 6 or 9. Measured on CML: red
81 90 240tagged 709 / 170M and sent to a 709 / 709 output was converted (62 102 240) and is now left alone; tagged 601 / 170M full range and sent to a limited output it was left alone and is now converted to86 95 226.How to test
Save this as
vp-matrix-test.sh. It needs only ffmpeg with VAAPI. It sends a solid red frame throughscale_vaapiand prints the first output pixel. The input pixels are always BT.601 limited red (Y=81 U=90 V=240); only the colour tags change, so the output shows which conversion the driver chose.Each case is labelled
matrix/primariesfor the input and the output surface (-is unspecified). How to read the result:Y=81 U=90 V=240: no conversionY=62 U=102 V=240: BT.601 to BT.709Y=68 U=99 V=236: BT.2020 to BT.709 (what this CML hardware outputs; I did not derive it)Y=86 U=95 V=226: full range to limited rangeWhat I get on UHD Graphics 630 (CML, 8086:9bc5), both drivers built from source:
6c82293Cases 2 and 5 are fully tagged controls. Cases 6, 7 and 8 are the mixed BT.2020 cases that must not change; the last commit is what keeps them unchanged.
Second repro: a YUV input layer in a multi-layer render
The same function maps input surfaces, so the bug also shows on a layer of
hstack_vaapi. Layer 0 is fully tagged BT.709; layer 1 has the same pixels tagged with the BT.709 matrix only.Expected: both layers stay
Y=81 U=90 V=240, because input and output matrix are both BT.709.On master layer 1 comes out
Y=62 U=102 V=241(converted as if it were BT.601). With this PR it staysY=81 U=90 V=240.What is and is not tested
media_libva_vp.c.ddi_vp_functions.cpp). I have no Meteor Lake or newer machine. The new helper is the same in both files; I compiled it out of each file and checked 21 matrix/primaries combinations. The switch it feeds in the softlet file was checked by reading only. A run of the script on MTL or newer would be very welcome.Fixes #2066
🤖 Generated with Claude Code