Skip to content

[VP] Use matrix_coefficients for explicit YUV colour space - #2067

Open
igouss wants to merge 4 commits into
intel:masterfrom
igouss:vp-explicit-matrix
Open

igouss wants to merge 4 commits into
intel:masterfrom
igouss:vp-explicit-matrix

Conversation

@igouss

@igouss igouss commented Nov 10, 2026 •

Copy link
Copy Markdown

With VAProcColorStandardExplicit, VpSetColorStandardExplictly picked 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 for the explicit standard. So an app that sets matrix_coefficients=1 (BT.709) and leaves colour_primaries unspecified got BT.601 output. ffmpeg's scale_vaapi does this for out_color_matrix=bt709 when the input frame carries no primaries.

What changes

The YUV colour space now comes from matrix_coefficients when it names BT.709 (1), BT.601 (5, 6) or BT.2020 NCL (9). Otherwise colour_primaries decides, 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_CSPACE holds 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 when colour_primaries is specified and only one of the two fields says BT.2020, colour_primaries still 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_coefficients means a full-range layer tagged with the matrix only now becomes CSpace_BT601_FullRange or CSpace_BT709_FullRange instead 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 with VA_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, on matrix_coefficients in VAProcColorProperties:

Matrix coefficients.
See ISO/IEC 23001-8 or ITU H.273, section 8.3 and table 4.
Only used if the color standard in use is \c VAColorStandardExplicit.

and on the colour standards in general:

Where matrix_coefficients is specified, it applies only to YUV data - RGB data always use the identity matrix (matrix_coefficients = 0).

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:

MatrixCoefficients describes the matrix coefficients used in deriving luma and chroma signals from the green, blue and red or X, Y and Z primaries, as specified in Table 4 and equations 11 to 87.

Table 4 rows this PR uses:

Value Matrix Informative remark
1 KR = 0.2126; KB = 0.0722 Rec. ITU-R BT.709-6
2 Unspecified Image characteristics are unknown or are determined by the application
5 KR = 0.299; KB = 0.114 Rec. ITU-R BT.601-7 625 (functionally the same as the value 6)
6 KR = 0.299; KB = 0.114 Rec. ITU-R BT.601-7 525 (functionally the same as the value 5)
9 KR = 0.2627; KB = 0.0593 Rec. ITU-R BT.2020-2 (non-constant luminance)

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_primaries Before Now
709 / unspecified (#2066) BT.601 BT.709
2020 NCL / unspecified BT.601 BT.2020
601 / 709 (JPEG-style) BT.709 BT.601
601 / unspecified, full range BT.601 limited BT.601 full range
709 / 170M (primaries 6) BT.601 BT.709
601 / 170M (primaries 6), full range BT.601 limited BT.601 full range
709 / 2020, or 2020 / 709 from primaries unchanged
both unspecified BT.601 limited unchanged

The 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 240 tagged 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 to 86 95 226.

How to test

Save this as vp-matrix-test.sh. It needs only ffmpeg with VAAPI. It sends a solid red frame through scale_vaapi and 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.

#!/bin/sh
# Usage: LIBVA_DRIVERS_PATH=<build>/media_driver LIBVA_DRIVER_NAME=iHD sh vp-matrix-test.sh [/dev/dri/renderD128]
DEV=${1:-/dev/dri/renderD128}

run() {
  name=$1 src=$2 intags=$3 outopts=$4
  ffmpeg -nostdin -loglevel error -init_hw_device vaapi=va:"$DEV" -filter_hw_device va \
    -f lavfi -i "color=c=red:s=64x64,format=$src" -frames:v 1 \
    -vf "setparams=$intags,hwupload,scale_vaapi=format=nv12:$outopts,hwdownload,format=nv12" \
    -f rawvideo - |
  od -An -tu1 -v | tr -s ' \n' ' ' |
  awk -v n="$name" '{ if (NF < 4098) print n ": FAILED"; else print n ": Y=" $1, "U=" $4097, "V=" $4098 }'
}

U=unknown
run "1 in -/-        out 709/-    " nv12 "colorspace=$U:color_primaries=$U:range=limited"            "out_color_matrix=bt709"
run "2 in 601/601    out 709/709  " nv12 "colorspace=smpte170m:color_primaries=smpte170m:range=limited" "out_color_matrix=bt709:out_color_primaries=bt709"
run "3 in 601/709    out 709/709  " nv12 "colorspace=smpte170m:color_primaries=bt709:range=limited"   "out_color_matrix=bt709:out_color_primaries=bt709"
run "4 in 2020/-     out 709/709  " nv12 "colorspace=bt2020nc:color_primaries=$U:range=limited"       "out_color_matrix=bt709:out_color_primaries=bt709"
run "5 in 2020/2020  out 709/709  " nv12 "colorspace=bt2020nc:color_primaries=bt2020:range=limited"   "out_color_matrix=bt709:out_color_primaries=bt709"
run "6 in 709/2020   out 709/709  " nv12 "colorspace=bt709:color_primaries=bt2020:range=limited"      "out_color_matrix=bt709:out_color_primaries=bt709"
run "7 in 2020/2020  out 709/2020 " nv12 "colorspace=bt2020nc:color_primaries=bt2020:range=limited"   "out_color_matrix=bt709"
run "8 in 709/709    out 2020/709 " nv12 "colorspace=bt709:color_primaries=bt709:range=limited"       "out_color_matrix=bt2020nc"
run "9 in 601/- full out 601/601  " nv12 "colorspace=smpte170m:color_primaries=$U:range=full"         "out_color_matrix=smpte170m:out_color_primaries=smpte170m:out_range=limited"
run "10 in RGB       out 709/-    " bgra "range=full"                                                 "out_color_matrix=bt709:out_range=limited"

Each case is labelled matrix/primaries for the input and the output surface (- is unspecified). How to read the result:

  • Y=81 U=90 V=240: no conversion
  • Y=62 U=102 V=240: BT.601 to BT.709
  • Y=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 range

What I get on UHD Graphics 630 (CML, 8086:9bc5), both drivers built from source:

Case master 6c82293 this PR
1 in -/-, out 709/- 81 90 240 62 102 240
2 in 601/601, out 709/709 62 102 240 62 102 240
3 in 601/709, out 709/709 81 90 240 62 102 240
4 in 2020/-, out 709/709 62 102 240 68 99 236
5 in 2020/2020, out 709/709 68 99 236 68 99 236
6 in 709/2020, out 709/709 68 99 236 68 99 236
7 in 2020/2020, out 709/2020 81 90 240 81 90 240
8 in 709/709, out 2020/709 81 90 240 81 90 240
9 in 601/- full, out 601/601 81 90 240 86 95 226
10 in RGB, out 709/- (#2066) 82 90 240 62 102 240

Cases 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.

python3 -c 'import sys; sys.stdout.buffer.write(bytes([81])*4096 + bytes([90,240])*1024)' > red.nv12
ffmpeg -nostdin -y -loglevel error -init_hw_device vaapi=va:/dev/dri/renderD128 -filter_hw_device va \
  -f rawvideo -pix_fmt nv12 -s 64x64 -i red.nv12 -f rawvideo -pix_fmt nv12 -s 64x64 -i red.nv12 \
  -filter_complex "[0:v]setparams=colorspace=bt709:color_primaries=bt709:color_trc=bt709:range=tv,hwupload[a];[1:v]setparams=colorspace=bt709:range=tv,hwupload[b];[a][b]hstack_vaapi,hwdownload,format=nv12" \
  -frames:v 1 -f rawvideo out.nv12 &&
od -An -tu1 -v out.nv12 | tr -s ' \n' ' ' | awk '{print "layer 0: Y=" $1, "U=" $8193, "V=" $8194 "   layer 1: Y=" $65, "U=" $8257, "V=" $8258}'

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 stays Y=81 U=90 V=240.

What is and is not tested

  • Run on hardware: the 10 cases above, on CML only. CML uses the legacy DDI, so this exercises media_libva_vp.c.
  • Not run on hardware: the softlet DDI (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.
  • HDR path, run on CML (legacy): BT.2020 + full-range BT.601/BT.709 layer in both orders, and one full-range layer to a BT.2020 output, matrix-only and fully tagged; master, the original two commits, and the full series. Seven colours sent as limited and as full range agree within 1 after the fix.
  • HDR path, softlet: compiled only.
  • Known and unrelated: with two layers and a BT.2020 input the second layer is not drawn on CML (gen9 HDR kernel path), on master too ([Bug]: VPP composition with a BT.2020 input layer leaves the second layer black (gen9 HDR kernel path) #2076).
  • Versions: libva 2.24.1, gmmlib 22.10.0, ffmpeg 9.0.2, Linux 7.2.15. The tables above were re-run on master 6c82293 and on the current head.

Fixes #2066

🤖 Generated with Claude Code

@XinfengZhang XinfengZhang added the VP Video Processing label Nov 12, 2026
@VickyZengg

Copy link
Copy Markdown
Contributor

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?

@igouss

igouss commented Nov 19, 2026

Copy link
Copy Markdown
Author

Hey @VickyZengg, I'm AFK for a couple of days, I'll come back to this PR when I'm back.

igouss and others added 4 commits November 21, 2026 20:29
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>
@igouss

igouss commented Nov 22, 2026

Copy link
Copy Markdown
Author

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 VA_STATUS_ERROR_OPERATION_FAILED. The same happened for matrix-only full-range BT.601, and for a single full-range layer converted to a BT.2020 output, which uses the same HDR kernel path.

The cause is what you described. The prior CSC in the HDR path only accepts CSpace_BT601, CSpace_BT709 and the BT.2020 spaces. On master the fully tagged versions of these inputs already fail for that reason; my change made the matrix-only versions reach the same code.

I pushed an updated branch, rebased on master, with two commits in front of the original two:

  1. [VP] Accept full-range BT.601 and BT.709 in the HDR kernel path adds prior CSC types for full-range BT.601/BT.709 input and a post CSC type for full-range BT.601 output (a full-range BT.601 output hit the same rejection). Same coefficients as limited range; the existing matrix helpers already take the range from the colour space.
  2. [VP] Accept full-range BT.601 and BT.709 in the softlet HDR paths is the same for the softlet HDR kernel packet and the HDR lite filter. It also adds the full-range output cases to the post CSC of the 3D LUT calculation there: full-range BT.709 output was already accepted at selection, but VpHal_HdrColorTransfer3dLut returned an error its callers ignore, which leaves the LUT zeroed. Compiled only; I have no hardware that runs softlet, so this commit needs your eyes most.

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 hstack_vaapi / scale_vaapi:

Case master 6c82293 PR as reviewed, rebased updated branch
BT.2020 + BT.709 full, matrix only (your case) success failed success
BT.2020 + BT.709 full, fully tagged failed failed success
BT.2020 + BT.601 full, matrix only, either order (as first layer it sets a BT.601 full output) success failed success
BT.2020 + BT.601 full, fully tagged failed failed success
one layer, BT.709 or BT.601 full, matrix only, to BT.2020 output success failed success
one layer, BT.709 or BT.601 full, fully tagged, to BT.2020 output failed failed success

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 VPFeatureManager::IsHdrNeeded, never set in SwFilterHdrHandler::IsFeatureEnabled), so there I expect this code to be reached through BT.2020 output, tone mapping, FP16 and the other HDR triggers rather than through your two-layer case. That is from reading, not from a run.

How to reproduce the main rows

Needs 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)"
done

What I get:

master 6c82293 PR as reviewed, rebased updated branch
two layers, 709 full matrix-only exit=0 exit=251 exit=0
one layer, limited, tagged Y=125 U=97 V=154 Y=125 U=97 V=154 Y=125 U=97 V=154
one layer, full, matrix only Y=123 U=96 V=156 (converted as BT.601 limited) exit=251 Y=124 U=97 V=155
one layer, full, fully tagged exit=251 exit=251 Y=124 U=97 V=155
Full test script (54 runs) and how to run it

Build 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/renderD128

The first line the script prints is the driver version string, which contains the commit, so each run shows which build it used.

hdr-fullrange-matrix.sh:

#!/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 builds

Values are Y U V of the output; two-layer rows show left layer / right layer. "failed (251)" is ffmpeg's exit code when vaEndPicture returns VA_STATUS_ERROR_OPERATION_FAILED. In the S rows the same colour is sent as limited and as full range, so on the updated branch the three lines of each group should agree, and they do within 1.

Case master 6c82293 original two commits, rebased (d5830e2) updated branch (4caf472)
S: one layer, BT.709 or BT.601 input, BT.2020 output
S rgb(255 0 0) 709 limited, tagged 79 106 190 79 106 190 79 106 190
S rgb(255 0 0) 709 full, tagged failed (251) failed (251) 79 106 190
S rgb(255 0 0) 709 full, matrix only 77 106 189 failed (251) 79 106 190
S rgb(255 0 0) 601 limited, tagged 79 106 190 79 106 190 79 106 190
S rgb(255 0 0) 601 full, tagged failed (251) failed (251) 79 106 190
S rgb(255 0 0) 601 full, matrix only 79 106 190 failed (251) 79 106 190
S rgb(0 255 0) 709 limited, tagged 190 60 108 190 60 108 190 60 108
S rgb(0 255 0) 709 full, tagged failed (251) failed (251) 190 60 109
S rgb(0 255 0) 709 full, matrix only 190 60 109 failed (251) 190 60 109
S rgb(0 255 0) 601 limited, tagged 190 60 108 190 60 108 190 60 108
S rgb(0 255 0) 601 full, tagged failed (251) failed (251) 190 60 108
S rgb(0 255 0) 601 full, matrix only 190 60 109 failed (251) 190 60 108
S rgb(0 0 255) 709 limited, tagged 92 222 116 92 222 116 92 222 116
S rgb(0 0 255) 709 full, tagged failed (251) failed (251) 91 221 116
S rgb(0 0 255) 709 full, matrix only 91 222 115 failed (251) 91 221 116
S rgb(0 0 255) 601 limited, tagged 91 222 116 91 222 116 91 222 116
S rgb(0 0 255) 601 full, tagged failed (251) failed (251) 92 222 115
S rgb(0 0 255) 601 full, matrix only 91 222 115 failed (251) 92 222 115
S rgb(128 128 128) 709 limited, tagged 139 128 128 139 128 128 139 128 128
S rgb(128 128 128) 709 full, tagged failed (251) failed (251) 139 128 128
S rgb(128 128 128) 709 full, matrix only 141 128 128 failed (251) 139 128 128
S rgb(128 128 128) 601 limited, tagged 139 128 128 139 128 128 139 128 128
S rgb(128 128 128) 601 full, tagged failed (251) failed (251) 139 128 128
S rgb(128 128 128) 601 full, matrix only 141 128 128 failed (251) 139 128 128
S rgb(200 120 40) 709 limited, tagged 125 97 154 125 97 154 125 97 154
S rgb(200 120 40) 709 full, tagged failed (251) failed (251) 124 97 155
S rgb(200 120 40) 709 full, matrix only 123 96 156 failed (251) 124 97 155
S rgb(200 120 40) 601 limited, tagged 125 97 155 125 97 155 125 97 155
S rgb(200 120 40) 601 full, tagged failed (251) failed (251) 125 98 155
S rgb(200 120 40) 601 full, matrix only 126 94 158 failed (251) 125 98 155
S rgb(16 16 16) 709 limited, tagged 43 128 128 43 128 128 43 128 128
S rgb(16 16 16) 709 full, tagged failed (251) failed (251) 43 128 128
S rgb(16 16 16) 709 full, matrix only 16 128 128 failed (251) 43 128 128
S rgb(16 16 16) 601 limited, tagged 43 128 128 43 128 128 43 128 128
S rgb(16 16 16) 601 full, tagged failed (251) failed (251) 43 128 128
S rgb(16 16 16) 601 full, matrix only 16 128 128 failed (251) 43 128 128
S rgb(235 235 235) 709 limited, tagged 225 128 128 225 128 128 225 128 128
S rgb(235 235 235) 709 full, tagged failed (251) failed (251) 225 128 128
S rgb(235 235 235) 709 full, matrix only 240 128 128 failed (251) 225 128 128
S rgb(235 235 235) 601 limited, tagged 225 128 128 225 128 128 225 128 128
S rgb(235 235 235) 601 full, tagged failed (251) failed (251) 225 128 128
S rgb(235 235 235) 601 full, matrix only 240 128 128 failed (251) 225 128 128
M: two layers, layer A / layer B (the first layer's tags become the output's)
M 709 limited + 709 limited (control, no HDR path) 126 89 164 / 126 89 164 126 89 164 / 126 89 164 126 89 164 / 126 89 164
M 2020 + 709 limited, tagged 107 104 157 / 0 0 0 107 104 157 / 0 0 0 107 104 157 / 0 0 0
M 2020 + 709 full, tagged failed (251) failed (251) 107 104 157 / 0 0 0
M 2020 + 709 full, matrix only 107 104 157 / 0 0 0 failed (251) 107 104 157 / 0 0 0
M 2020 + 601 full, tagged failed (251) failed (251) 107 104 157 / 0 0 0
M 2020 + 601 full, matrix only 107 104 157 / 0 0 0 failed (251) 107 104 157 / 0 0 0
M 709 limited, tagged + 2020 110 93 168 / 0 0 0 110 93 168 / 0 0 0 110 93 168 / 0 0 0
M 709 full, tagged + 2020 failed (251) failed (251) 106 83 181 / 0 0 0
M 709 full, matrix only + 2020 106 85 178 / 0 0 0 failed (251) 106 83 181 / 0 0 0
M 601 limited, tagged + 2020 108 87 174 / 0 0 0 108 87 174 / 0 0 0 108 87 174 / 0 0 0
M 601 full, tagged + 2020 failed (251) failed (251) 105 76 190 / 0 0 0
M 601 full, matrix only + 2020 108 82 181 / 0 0 0 failed (251) 105 76 190 / 0 0 0

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.

@igouss igouss closed this Nov 22, 2026
@igouss igouss reopened this Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

VP Video Processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: VPP RGB to NV12 gives BT.601 when matrix_coefficients is BT.709 and colour_primaries is unspecified

3 participants