Debian Codegraph @

media_softlet/agnostic/common/vp/hal/utils/vp_utils.cpp

raw
1 /*
2 * Copyright (c) 2021-2023, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <cmath>
24 #include <array>
25 #include "vp_utils.h"
26 #include "vp_common.h"
27 #include "hal_kerneldll_next.h"
28 #include "mos_interface.h"
29
30 MOS_STATUS VpUtils::ReAllocateSurface(
31 PMOS_INTERFACE osInterface,
32 PVPHAL_SURFACE surface,
33 PCCHAR surfaceName,
34 MOS_FORMAT format,
35 MOS_GFXRES_TYPE defaultResType,
36 MOS_TILE_TYPE defaultTileType,
37 uint32_t dwWidth,
38 uint32_t dwHeight,
39 bool bCompressible,
40 MOS_RESOURCE_MMC_MODE compressionMode,
41 bool *bAllocated,
42 MOS_HW_RESOURCE_DEF resUsageType,
43 MOS_TILE_MODE_GMM tileModeByForce,
44 Mos_MemPool memType,
45 bool isNotLockable)
46 {
47 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
48 VPHAL_GET_SURFACE_INFO info = {};
49 MOS_ALLOC_GFXRES_PARAMS allocParams = {};
50 MOS_GFXRES_FREE_FLAGS resFreeFlags = {0};
51
52 //---------------------------------
53 VP_PUBLIC_ASSERT(osInterface);
54 VP_PUBLIC_ASSERT(surface);
55 //---------------------------------
56
57 *bAllocated = false;
58
59 // bCompressible should be compared with bCompressible since it is inited by bCompressible in previous call
60 // TileType of surface should be compared since we need to reallocate surface if TileType changes
61 if (!Mos_ResourceIsNull(&surface->OsResource) &&
62 (surface->dwWidth == dwWidth) &&
63 (surface->dwHeight == dwHeight) &&
64 (surface->Format == format) &&
65 (surface->bCompressible == bCompressible) &&
66 (surface->CompressionMode == compressionMode) &&
67 (surface->TileType == defaultTileType))
68 {
69 goto finish;
70 }
71
72 // reuse the allocated buffer if the allocated size was larger than request size when OptimizeCpuTiming is enabled
73 if (osInterface->bOptimizeCpuTiming &&
74 !Mos_ResourceIsNull(&surface->OsResource) &&
75 (Format_Buffer == format) &&
76 (surface->dwWidth * surface->dwHeight >= dwWidth * dwHeight))
77 {
78 goto finish;
79 }
80
81 MOS_ZeroMemory(&allocParams, sizeof(MOS_ALLOC_GFXRES_PARAMS));
82
83 VpHal_AllocParamsInitType(&allocParams, surface, defaultResType, defaultTileType);
84
85 allocParams.dwWidth = dwWidth;
86 allocParams.dwHeight = dwHeight;
87 allocParams.Format = format;
88 allocParams.bIsCompressible = bCompressible;
89 allocParams.CompressionMode = compressionMode;
90 allocParams.pBufName = surfaceName;
91 allocParams.dwArraySize = 1;
92 allocParams.ResUsageType = resUsageType;
93 allocParams.m_tileModeByForce = tileModeByForce;
94 allocParams.dwMemType = memType;
95 allocParams.Flags.bNotLockable = isNotLockable;
96
97 // Delete resource if already allocated
98 //if free the compressed surface, need set the sync dealloc flag as 1 for sync dealloc for aux table update
99 if (IsSyncFreeNeededForMMCSurface(surface, osInterface))
100 {
101 resFreeFlags.SynchronousDestroy = 1;
102 VP_PUBLIC_NORMALMESSAGE("Set SynchronousDestroy flag for compressed resource %s", surfaceName);
103 }
104 osInterface->pfnFreeResourceWithFlag(osInterface, &(surface->OsResource), resFreeFlags.Value);
105
106 // Allocate surface
107 VP_PUBLIC_CHK_STATUS(osInterface->pfnAllocateResource(
108 osInterface,
109 &allocParams,
110 &surface->OsResource));
111
112 // Get surface information
113 MOS_ZeroMemory(&info, sizeof(VPHAL_GET_SURFACE_INFO));
114
115 // Pre-set to get surface info
116 surface->Format = format;
117
118 VP_PUBLIC_CHK_STATUS(VpHal_GetSurfaceInfo(osInterface, &info, surface));
119
120 *bAllocated = true;
121
122 MT_LOG7(MT_VP_HAL_REALLOC_SURF, MT_NORMAL, MT_VP_HAL_INTER_SURF_TYPE, surfaceName ? *((int64_t *)surfaceName) : 0, MT_SURF_WIDTH, dwWidth, MT_SURF_HEIGHT, dwHeight, MT_SURF_MOS_FORMAT, format, MT_SURF_TILE_MODE, surface->TileModeGMM, MT_SURF_COMP_ABLE, surface->bCompressible, MT_SURF_COMP_MODE, surface->CompressionMode);
123
124 finish:
125 VP_PUBLIC_ASSERT(eStatus == MOS_STATUS_SUCCESS);
126 return eStatus;
127 }
128
129 bool VpUtils::IsVerticalRotation(VPHAL_ROTATION rotation) {
130 return (rotation != VPHAL_ROTATION_IDENTITY &&
131 rotation != VPHAL_ROTATION_180 &&
132 rotation != VPHAL_MIRROR_VERTICAL &&
133 rotation != VPHAL_MIRROR_HORIZONTAL);
134 }
135
136 bool VpUtils::IsSyncFreeNeededForMMCSurface(PVPHAL_SURFACE surface, PMOS_INTERFACE osInterface)
137 {
138 if (nullptr == surface || nullptr == osInterface)
139 {
140 return false;
141 }
142
143 //Compressed surface aux table update is after resource dealloction, aux table update need wait the WLs complete
144 //the sync deallocation flag will make sure deallocation API return after all surface related WL been completed and resource been destroyed by OS
145 auto *pSkuTable = osInterface->pfnGetSkuTable(osInterface);
146 if (pSkuTable &&
147 MEDIA_IS_SKU(pSkuTable, FtrE2ECompression) && //Compression enabled platform
148 !MEDIA_IS_SKU(pSkuTable, FtrFlatPhysCCS) && //NOT DGPU compression
149 ((surface->bCompressible) && (surface->CompressionMode != MOS_MMC_DISABLED))) //Compressed enabled surface
150 {
151 return true;
152 }
153
154 return false;
155 }
156
157 void VpUtils::GetCscMatrixForVeSfc8Bit(
158 VPHAL_CSPACE srcCspace,
159 VPHAL_CSPACE dstCspace,
160 float *fCscCoeff,
161 float *fCscInOffset,
162 float *fCscOutOffset)
163 {
164 float fCscMatrix[12] = {0};
165 int32_t i = 0;
166
167 KernelDll_GetCSCMatrix(
168 srcCspace,
169 dstCspace,
170 fCscMatrix);
171
172 // Copy [3x3] into Coeff
173 for (i = 0; i < 3; i++)
174 {
175 MOS_SecureMemcpy(
176 &fCscCoeff[i * 3],
177 sizeof(float) * 3,
178 &fCscMatrix[i * 4],
179 sizeof(float) * 3);
180 }
181
182 // Get the input offsets
183 switch (srcCspace)
184 {
185 CASE_YUV_CSPACE_LIMITEDRANGE:
186 fCscInOffset[0] = -16.0F;
187 fCscInOffset[1] = -128.0F;
188 fCscInOffset[2] = -128.0F;
189 break;
190
191 CASE_YUV_CSPACE_FULLRANGE:
192 fCscInOffset[0] = 0.0F;
193 fCscInOffset[1] = -128.0F;
194 fCscInOffset[2] = -128.0F;
195 break;
196
197 case CSpace_sRGB:
198 fCscInOffset[0] = 0.0F;
199 fCscInOffset[1] = 0.0F;
200 fCscInOffset[2] = 0.0F;
201 break;
202
203 case CSpace_stRGB:
204 fCscInOffset[0] = -16.0F;
205 fCscInOffset[1] = -16.0F;
206 fCscInOffset[2] = -16.0F;
207 break;
208
209 //BT2020 YUV->RGB
210 case CSpace_BT2020:
211 fCscInOffset[0] = -16.0F;
212 fCscInOffset[1] = -128.0F;
213 fCscInOffset[2] = -128.0F;
214 break;
215
216 case CSpace_BT2020_FullRange:
217 fCscInOffset[0] = 0.0F;
218 fCscInOffset[1] = -128.0F;
219 fCscInOffset[2] = -128.0F;
220 break;
221
222 //BT2020 RGB->YUV
223 case CSpace_BT2020_RGB:
224 fCscInOffset[0] = 0.0F;
225 fCscInOffset[1] = 0.0F;
226 fCscInOffset[2] = 0.0F;
227 break;
228
229 //BT2020 RGB->YUV
230 case CSpace_BT2020_stRGB:
231 fCscInOffset[0] = -16.0F;
232 fCscInOffset[1] = -16.0F;
233 fCscInOffset[2] = -16.0F;
234 break;
235
236 default:
237 VP_PUBLIC_NORMALMESSAGE("Unsupported Input ColorSpace for Vebox %d.", (uint32_t)srcCspace);
238 }
239
240 // Get the output offsets
241 switch (dstCspace)
242 {
243 CASE_YUV_CSPACE_LIMITEDRANGE:
244 fCscOutOffset[0] = 16.0F;
245 fCscOutOffset[1] = 128.0F;
246 fCscOutOffset[2] = 128.0F;
247 break;
248
249 CASE_YUV_CSPACE_FULLRANGE:
250 fCscOutOffset[0] = 0.0F;
251 fCscOutOffset[1] = 128.0F;
252 fCscOutOffset[2] = 128.0F;
253 break;
254
255 case CSpace_sRGB:
256 fCscOutOffset[0] = 0.0F;
257 fCscOutOffset[1] = 0.0F;
258 fCscOutOffset[2] = 0.0F;
259 break;
260
261 case CSpace_stRGB:
262 fCscOutOffset[0] = 16.0F;
263 fCscOutOffset[1] = 16.0F;
264 fCscOutOffset[2] = 16.0F;
265 break;
266
267 //BT2020 RGB->YUV
268 case CSpace_BT2020:
269 fCscOutOffset[0] = 16.0F;
270 fCscOutOffset[1] = 128.0F;
271 fCscOutOffset[2] = 128.0F;
272 break;
273
274 case CSpace_BT2020_FullRange:
275 fCscOutOffset[0] = 0.0F;
276 fCscOutOffset[1] = 128.0F;
277 fCscOutOffset[2] = 128.0F;
278 break;
279
280 case CSpace_BT2020_RGB:
281 fCscOutOffset[0] = 0.0F;
282 fCscOutOffset[1] = 0.0F;
283 fCscOutOffset[2] = 0.0F;
284 break;
285
286 case CSpace_BT2020_stRGB:
287 fCscOutOffset[0] = 16.0F;
288 fCscOutOffset[1] = 16.0F;
289 fCscOutOffset[2] = 16.0F;
290 break;
291
292 default:
293 VP_PUBLIC_NORMALMESSAGE("Unsupported Output ColorSpace for Vebox %d.", (uint32_t)dstCspace);
294 }
295 }
296
297 bool VpUtils::GetCscMatrixForRender8Bit(
298 VPHAL_COLOR_SAMPLE_8 *output,
299 VPHAL_COLOR_SAMPLE_8 *input,
300 VPHAL_CSPACE srcCspace,
301 VPHAL_CSPACE dstCspace)
302 {
303 float pfCscMatrix[12] = {0};
304 int32_t iCscMatrix[12] = {0};
305 bool bResult = false;
306 int32_t i = 0;
307
308 KernelDll_GetCSCMatrix(srcCspace, dstCspace, pfCscMatrix);
309
310 // convert float to fixed point format for the 3x4 matrix
311 for (i = 0; i < 12; i++)
312 {
313 // multiply by 2^20 and round up
314 iCscMatrix[i] = (int32_t)((pfCscMatrix[i] * 1048576.0f) + 0.5f);
315 }
316
317 bResult = GetCscMatrixForRender8BitWithCoeff(output, input, srcCspace, dstCspace, iCscMatrix);
318
319 return bResult;
320 }
321
322 bool VpUtils::GetCscMatrixForRender8BitWithCoeff(
323 VPHAL_COLOR_SAMPLE_8 *output,
324 VPHAL_COLOR_SAMPLE_8 *input,
325 VPHAL_CSPACE srcCspace,
326 VPHAL_CSPACE dstCspace,
327 int32_t *iCscMatrix)
328 {
329 bool bResult = true;
330 int32_t a = 0, r = 0, g = 0, b = 0;
331 int32_t y1 = 0, u1 = 0, v1 = 0;
332
333 y1 = r = input->YY;
334 u1 = g = input->Cb;
335 v1 = b = input->Cr;
336 a = input->Alpha;
337
338 if (srcCspace == dstCspace)
339 {
340 // no conversion needed
341 if ((dstCspace == CSpace_sRGB) || (dstCspace == CSpace_stRGB) || IS_COLOR_SPACE_BT2020_RGB(dstCspace))
342 {
343 output->A = (uint8_t)a;
344 output->R = (uint8_t)r;
345 output->G = (uint8_t)g;
346 output->B = (uint8_t)b;
347 }
348 else
349 {
350 output->a = (uint8_t)a;
351 output->Y = (uint8_t)y1;
352 output->U = (uint8_t)u1;
353 output->V = (uint8_t)v1;
354 }
355 }
356 else
357 {
358 // conversion needed
359 r = (y1 * iCscMatrix[0] + u1 * iCscMatrix[1] +
360 v1 * iCscMatrix[2] + iCscMatrix[3] + 0x00080000) >>
361 20;
362 g = (y1 * iCscMatrix[4] + u1 * iCscMatrix[5] +
363 v1 * iCscMatrix[6] + iCscMatrix[7] + 0x00080000) >>
364 20;
365 b = (y1 * iCscMatrix[8] + u1 * iCscMatrix[9] +
366 v1 * iCscMatrix[10] + iCscMatrix[11] + 0x00080000) >>
367 20;
368
369 switch (dstCspace)
370 {
371 case CSpace_sRGB:
372 output->A = (uint8_t)a;
373 output->R = MOS_MIN(MOS_MAX(0, r), 255);
374 output->G = MOS_MIN(MOS_MAX(0, g), 255);
375 output->B = MOS_MIN(MOS_MAX(0, b), 255);
376 break;
377
378 case CSpace_stRGB:
379 output->A = (uint8_t)a;
380 output->R = MOS_MIN(MOS_MAX(16, r), 235);
381 output->G = MOS_MIN(MOS_MAX(16, g), 235);
382 output->B = MOS_MIN(MOS_MAX(16, b), 235);
383 break;
384
385 case CSpace_BT601:
386 case CSpace_BT709:
387 output->a = (uint8_t)a;
388 output->Y = MOS_MIN(MOS_MAX(16, r), 235);
389 output->U = MOS_MIN(MOS_MAX(16, g), 240);
390 output->V = MOS_MIN(MOS_MAX(16, b), 240);
391 break;
392
393 case CSpace_xvYCC601:
394 case CSpace_xvYCC709:
395 case CSpace_BT601_FullRange:
396 case CSpace_BT709_FullRange:
397 output->a = (uint8_t)a;
398 output->Y = MOS_MIN(MOS_MAX(0, r), 255);
399 output->U = MOS_MIN(MOS_MAX(0, g), 255);
400 output->V = MOS_MIN(MOS_MAX(0, b), 255);
401 break;
402
403 default:
404 VP_PUBLIC_NORMALMESSAGE("Unsupported Output ColorSpace %d.", (uint32_t)dstCspace);
405 bResult = false;
406 break;
407 }
408 }
409
410 return bResult;
411 }
412
413 MOS_STATUS VpUtils::GetPixelWithCSCForColorFillFloat(
414 float input[4],
415 float output[4],
416 VPHAL_CSPACE srcCspace,
417 VPHAL_CSPACE dstCspace)
418 {
419 if (srcCspace == dstCspace)
420 {
421 VP_PUBLIC_NORMALMESSAGE("Set input color to output color for fp16 usage.");
422 // no conversion needed
423 output[0] = input[0];
424 output[1] = input[1];
425 output[2] = input[2];
426 output[3] = input[3];
427 }
428 else
429 {
430 VP_PUBLIC_ASSERTMESSAGE("Not support this colorspace convert for Float Colorfill. Source Color Space: %d, Target Color Space: %d.",
431 srcCspace,
432 dstCspace);
433 }
434
435 return MOS_STATUS_SUCCESS;
436 }
437
438 MOS_STATUS VpUtils::GetPixelWithCSCForColorFill(
439 VPHAL_COLOR_SAMPLE_8 &input,
440 float output[4],
441 VPHAL_CSPACE srcCspace,
442 VPHAL_CSPACE dstCspace)
443 {
444 VPHAL_COLOR_SAMPLE_8 dstColor = {};
445 if (IS_COLOR_SPACE_BT2020(srcCspace))
446 {
447 VP_PUBLIC_ASSERTMESSAGE("Not support color fill input color space BT2020. Because DDI struct only contains 8 bit, which cannot accommodate BT2020");
448 }
449 else if (IS_COLOR_SPACE_BT2020(dstCspace))
450 {
451 // Target is BT2020, which is not supported by legacy convert
452 VP_PUBLIC_NORMALMESSAGE("Will do special convert to BT2020. Source Cspace %d. Target Cspace %d", srcCspace, dstCspace);
453 float pCscMatrix[12] = {};
454 auto SDRDegamma_sRGB_x1 = [](float c) -> float {
455 if (c <= VPHAL_HDR_EOTF_COEFF1_TRADITIONNAL_GAMMA_SRGB)
456 {
457 return c * VPHAL_HDR_EOTF_COEFF2_TRADITIONNAL_GAMMA_SRGB;
458 }
459 else
460 {
461 return pow(VPHAL_HDR_EOTF_COEFF3_TRADITIONNAL_GAMMA_SRGB * c + VPHAL_HDR_EOTF_COEFF4_TRADITIONNAL_GAMMA_SRGB, VPHAL_HDR_EOTF_COEFF5_TRADITIONNAL_GAMMA_SRGB);
462 }
463 };
464 auto HDRGamma_x1 = [](float c) -> float {
465 if (c <= 0.0181)
466 {
467 return c * 4.5f;
468 }
469 else
470 {
471 return 1.0993f * pow(c, 0.45f) - 0.0993f;
472 }
473 };
474
475 //Convert to sRGB
476 VPHAL_COLOR_SAMPLE_8 interColorRGB = {};
477 if (!GetCscMatrixForRender8Bit(&interColorRGB, &input, srcCspace, CSpace_sRGB))
478 {
479 VP_PUBLIC_CHK_STATUS_RETURN(MOS_STATUS_UNIMPLEMENTED);
480 }
481 //Degamma sRGB IEC 61966-2-1:1999
482 float R = SDRDegamma_sRGB_x1((float)interColorRGB.R / 255);
483 float G = SDRDegamma_sRGB_x1((float)interColorRGB.G / 255);
484 float B = SDRDegamma_sRGB_x1((float)interColorRGB.B / 255);
485 output[3] = (float)interColorRGB.A / 255;
486
487 //CCM ITU-R BT.2087-0
488 float R2020 = 0.329282097415f * G + 0.043313797587f * B + 0.627404078626f * R;
489 float G2020 = 0.919541035593f * G + 0.011361189924f * B + 0.069097233123f * R;
490 float B2020 = 0.088013255546f * G + 0.895595009604f * B + 0.016391587664f * R;
491
492 //Gamma BT2020 ITU-R BT.2020-2
493 R2020 = HDRGamma_x1(MOS_CLAMP_MIN_MAX(R2020, 0.f, 1.f));
494 G2020 = HDRGamma_x1(MOS_CLAMP_MIN_MAX(G2020, 0.f, 1.f));
495 B2020 = HDRGamma_x1(MOS_CLAMP_MIN_MAX(B2020, 0.f, 1.f));
496
497 KernelDll_GetCSCMatrix(CSpace_BT2020_RGB, dstCspace, pCscMatrix);
498 output[0] = pCscMatrix[0] * R2020 + pCscMatrix[1] * G2020 + pCscMatrix[2] * B2020 + pCscMatrix[3] / 1023;
499 output[1] = pCscMatrix[4] * R2020 + pCscMatrix[5] * G2020 + pCscMatrix[6] * B2020 + pCscMatrix[7] / 1023;
500 output[2] = pCscMatrix[8] * R2020 + pCscMatrix[9] * G2020 + pCscMatrix[10] * B2020 + pCscMatrix[11] / 1023;
501
502 //clamp
503 output[0] = MOS_CLAMP_MIN_MAX(output[0], 0.f, 1.f);
504 output[1] = MOS_CLAMP_MIN_MAX(output[1], 0.f, 1.f);
505 output[2] = MOS_CLAMP_MIN_MAX(output[2], 0.f, 1.f);
506 }
507 else if (srcCspace == dstCspace)
508 {
509 // no conversion needed
510 output[0] = (float)input.YY / 255; //R or Y
511 output[1] = (float)input.Cb / 255; //G or U
512 output[2] = (float)input.Cr / 255; //B or V
513 output[3] = (float)input.Alpha / 255; //A
514 }
515 else if (dstCspace == CSpace_BT601Gray || dstCspace == CSpace_BT601Gray_FullRange)
516 {
517 //Target is Gray Color Space, not supported by legacy convert
518 VP_PUBLIC_NORMALMESSAGE("Will do special convert to Gray CSpace. Source Cspace %d. Target Cspace %d", srcCspace, dstColor);
519 float pCscMatrix[12] = {};
520 int32_t iCscMatrix[12] = {};
521 VPHAL_CSPACE interCSpace = (dstCspace == CSpace_BT601Gray_FullRange ? CSpace_BT601_FullRange : CSpace_BT601);
522 KernelDll_GetCSCMatrix(srcCspace, interCSpace, pCscMatrix);
523
524 // convert float to fixed point format for the 3x4 matrix
525 for (int32_t i = 0; i < 12; ++i)
526 {
527 // multiply by 2^20 and round up
528 iCscMatrix[i] = (int32_t)((pCscMatrix[i] * 1048576.0f) + 0.5f);
529 }
530
531 int32_t luma = (input.YY * iCscMatrix[0] + input.Cb * iCscMatrix[1] + input.Cr * iCscMatrix[2] + iCscMatrix[3] + 0x00080000) >> 20;
532 output[0] = (float)MOS_CLAMP_MIN_MAX(luma, 0, 255) / 255;
533 output[1] = 0;
534 output[2] = 0;
535 output[3] = (float)input.Alpha / 255;
536 }
537 else
538 {
539 //legacy convert
540 if (VpUtils::GetCscMatrixForRender8Bit(&dstColor, &input, srcCspace, dstCspace))
541 {
542 if ((dstCspace == CSpace_sRGB) || (dstCspace == CSpace_stRGB) || IS_COLOR_SPACE_BT2020_RGB(dstCspace))
543 {
544 output[0] = (float)dstColor.R / 255;
545 output[1] = (float)dstColor.G / 255;
546 output[2] = (float)dstColor.B / 255;
547 output[3] = (float)dstColor.A / 255;
548 }
549 else
550 {
551 output[0] = (float)dstColor.Y / 255;
552 output[1] = (float)dstColor.U / 255;
553 output[2] = (float)dstColor.V / 255;
554 output[3] = (float)dstColor.a / 255;
555 }
556 }
557 else
558 {
559 VP_PUBLIC_ASSERTMESSAGE("Not supported color fill cspace convert. Source Cspace %d. Target Cspace %d", srcCspace, dstColor);
560 }
561 }
562
563 return MOS_STATUS_SUCCESS;
564 }
565
566 MOS_STATUS VpUtils::GetNormalizedCSCMatrix(
567 MEDIA_CSPACE src,
568 MEDIA_CSPACE dst,
569 float cscMatrix[12])
570 {
571 VP_PUBLIC_CHK_NULL_RETURN(cscMatrix);
572
573 if ((IS_COLOR_SPACE_BT2020(src) && !IS_COLOR_SPACE_BT2020(dst)) ||
574 (!IS_COLOR_SPACE_BT2020(src) && IS_COLOR_SPACE_BT2020(dst)))
575 {
576 VP_PUBLIC_ASSERTMESSAGE("Not support hdr to sdr or sdr to hdr csc convert. Src CSpace %d, Dst CSpace %d", src, dst);
577 }
578
579 KernelDll_GetCSCMatrix(src, dst, cscMatrix);
580
581 //for BT2020RGB convert to BT2020RGB, KernelDll_GetCSCMatrix use 1023 as max bias
582 //for other cases, such as sRGB/BT709/BT601 and BT2020YUV convert BT2020RGB, KernelDll_GetCSCMatrix use 255 as max bias
583 //so need to normalize w/ different value
584 if ((src == CSpace_BT2020_stRGB && dst == CSpace_BT2020_RGB) ||
585 (src == CSpace_BT2020_RGB && dst == CSpace_BT2020_stRGB))
586 {
587 cscMatrix[3] /= 1023.f;
588 cscMatrix[7] /= 1023.f;
589 cscMatrix[11] /= 1023.f;
590 }
591 else
592 {
593 cscMatrix[3] /= 255.f;
594 cscMatrix[7] /= 255.f;
595 cscMatrix[11] /= 255.f;
596 }
597
598 return MOS_STATUS_SUCCESS;
599 }
600
601 MOS_STATUS VpUtils::GetRgbRangeAndOffset(
602 VPHAL_CSPACE cspace,
603 float& rgbOffset,
604 float& rgbExcursion)
605 {
606 switch (cspace)
607 {
608 case CSpace_sRGB:
609 case CSpace_BT2020_RGB:
610 rgbOffset = 0.0f;
611 rgbExcursion = 255.0f;
612 break;
613
614 case CSpace_stRGB:
615 case CSpace_BT2020_stRGB:
616 rgbOffset = 16.0f;
617 rgbExcursion = 219.0f;
618 break;
619
620 default:
621 VP_PUBLIC_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
622 }
623
624 rgbOffset /= 255.0f;
625 rgbExcursion /= 255.0f;
626
627 return MOS_STATUS_SUCCESS;
628 }
629
630 MOS_STATUS VpUtils::GetYuvRangeAndOffset(
631 VPHAL_CSPACE cspace,
632 float& lumaOffset,
633 float& lumaExcursion,
634 float& chromaZero,
635 float& chromaExcursion)
636 {
637 switch (cspace)
638 {
639 case CSpace_BT601_FullRange:
640 case CSpace_BT709_FullRange:
641 case CSpace_BT601Gray_FullRange:
642 case CSpace_BT2020_FullRange:
643 lumaOffset = 0.0f;
644 lumaExcursion = 255.0f;
645 chromaZero = 128.0f;
646 chromaExcursion = 255.0f;
647 break;
648
649 case CSpace_BT601:
650 case CSpace_BT709:
651 case CSpace_xvYCC601: // since matrix is the same as 601, use the same range
652 case CSpace_xvYCC709: // since matrix is the same as 709, use the same range
653 case CSpace_BT601Gray:
654 case CSpace_BT2020:
655 lumaOffset = 16.0f;
656 lumaExcursion = 219.0f;
657 chromaZero = 128.0f;
658 chromaExcursion = 224.0f;
659 break;
660
661 default:
662 VP_PUBLIC_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
663 }
664
665 lumaOffset /= 255.0f;
666 lumaExcursion /= 255.0f;
667 chromaZero /= 255.0f;
668 chromaExcursion /= 255.0f;
669
670 return MOS_STATUS_SUCCESS;
671 }
672
673 MOS_STATUS VpUtils::GetCscMatrixForHDR(
674 VPHAL_CSPACE src,
675 VPHAL_CSPACE dst,
676 bool swapChannel,
677 float *outMatrix)
678 {
679 static auto TransferRgbToYuvMatrix = [](VPHAL_CSPACE src, VPHAL_CSPACE dst, std::array<float, 12> &transferMatrix, float *outMatrix) -> MOS_STATUS {
680 float Y_o = 0.0f, Y_e = 0.0f, C_z = 0.0f, C_e = 0.0f;
681 float R_o = 0.0f, R_e = 0.0f;
682 VP_FUNC_CALL();
683 VP_PUBLIC_CHK_NULL_RETURN(outMatrix);
684
685 VP_PUBLIC_CHK_STATUS_RETURN(GetRgbRangeAndOffset(src, R_o, R_e));
686 VP_PUBLIC_CHK_STATUS_RETURN(GetYuvRangeAndOffset(dst, Y_o, Y_e, C_z, C_e));
687
688 if (R_e == 0.0f)
689 {
690 VP_RENDER_ASSERTMESSAGE("R_e = %f should not be zero.", R_e);
691 return MOS_STATUS_INVALID_PARAMETER;
692 }
693 // multiplication of + onwards
694 outMatrix[0] = transferMatrix[0] * Y_e / R_e;
695 outMatrix[1] = transferMatrix[1] * Y_e / R_e;
696 outMatrix[2] = transferMatrix[2] * Y_e / R_e;
697 outMatrix[4] = transferMatrix[4] * C_e / R_e;
698 outMatrix[5] = transferMatrix[5] * C_e / R_e;
699 outMatrix[6] = transferMatrix[6] * C_e / R_e;
700 outMatrix[8] = transferMatrix[8] * C_e / R_e;
701 outMatrix[9] = transferMatrix[9] * C_e / R_e;
702 outMatrix[10] = transferMatrix[10] * C_e / R_e;
703
704 outMatrix[7] = Y_o - Y_e * R_o / R_e;
705 outMatrix[3] = C_z;
706 outMatrix[11] = C_z;
707
708 return MOS_STATUS_SUCCESS;
709 };
710
711 static auto TransferYuvToRgbMatrix = [](VPHAL_CSPACE src, VPHAL_CSPACE dst, std::array<float, 12> &transferMatrix, float *outMatrix) -> MOS_STATUS {
712 float Y_o = 0.0f, Y_e = 0.0f, C_z = 0.0f, C_e = 0.0f;
713 float R_o = 0.0f, R_e = 0.0f;
714
715 VP_PUBLIC_CHK_NULL_RETURN(outMatrix);
716
717 VP_PUBLIC_CHK_STATUS_RETURN(GetRgbRangeAndOffset(dst, R_o, R_e));
718 VP_PUBLIC_CHK_STATUS_RETURN(GetYuvRangeAndOffset(src, Y_o, Y_e, C_z, C_e));
719
720 // after + (3x3)(3x3)
721 if (Y_e == 0.0f || C_e == 0.0f)
722 {
723 VP_RENDER_ASSERTMESSAGE("Y_e = %f and C_e = %f, should not be zero.", Y_e, C_e);
724 return MOS_STATUS_INVALID_PARAMETER;
725 }
726 outMatrix[0] = transferMatrix[0] * R_e / Y_e;
727 outMatrix[4] = transferMatrix[4] * R_e / Y_e;
728 outMatrix[8] = transferMatrix[8] * R_e / Y_e;
729 outMatrix[1] = transferMatrix[1] * R_e / C_e;
730 outMatrix[5] = transferMatrix[5] * R_e / C_e;
731 outMatrix[9] = transferMatrix[9] * R_e / C_e;
732 outMatrix[2] = transferMatrix[2] * R_e / C_e;
733 outMatrix[6] = transferMatrix[6] * R_e / C_e;
734 outMatrix[10] = transferMatrix[10] * R_e / C_e;
735
736 // (3x1) - (3x3)(3x3)(3x1)
737 outMatrix[3] = R_o - (outMatrix[0] * Y_o + outMatrix[1] * C_z + outMatrix[2] * C_z);
738 outMatrix[7] = R_o - (outMatrix[4] * Y_o + outMatrix[5] * C_z + outMatrix[6] * C_z);
739 outMatrix[11] = R_o - (outMatrix[8] * Y_o + outMatrix[9] * C_z + outMatrix[10] * C_z);
740
741 return MOS_STATUS_SUCCESS;
742 };
743
744 std::array<float, 12> tempMatrix;
745 if (dst == CSpace_sRGB)
746 {
747 if (src == CSpace_BT601)
748 {
749 tempMatrix = {1.000000f, 0.000000f, 1.402000f, 0.000000f, 1.000000f, -0.344136f, -0.714136f, 0.000000f, 1.000000f, 1.772000f, 0.000000f, 0.000000f};
750 }
751 else if (src == CSpace_BT709)
752 {
753 tempMatrix = {1.000000f, 0.000000f, 1.574800f, 0.000000f, 1.000000f, -0.187324f, -0.468124f, 0.000000f, 1.000000f, 1.855600f, 0.000000f, 0.000000f};
754 }
755 else if (src == CSpace_BT2020)
756 {
757 tempMatrix = {1.000000f, 0.000000f, 1.474600f, 0.000000f, 1.000000f, -0.164550f, -0.571350f, 0.000000f, 1.000000f, 1.881400f, 0.000000f, 0.000000f};
758 }
759 else
760 {
761 VP_RENDER_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
762 }
763 VP_PUBLIC_CHK_STATUS_RETURN(TransferYuvToRgbMatrix(src, dst, tempMatrix, outMatrix));
764 }
765 else if (src == CSpace_sRGB)
766 {
767 if (dst == CSpace_BT601)
768 {
769 if (swapChannel)
770 {
771 tempMatrix = {0.500000f, -0.418688f, -0.081312f, 0.000000f, 0.299000f, 0.587000f, 0.114000f, 0.000000f, -0.168736f, -0.331264f, 0.500000f, 0.000000f};
772 }
773 else
774 {
775 tempMatrix = {-0.331264f, -0.168736f, 0.500000f, 0.000000f, 0.587000f, 0.299000f, 0.114000f, 0.000000f, -0.418688f, 0.500000f, -0.081312f, 0.000000f};
776 }
777 }
778 else if (dst == CSpace_BT709 || dst == CSpace_BT709_FullRange)
779 {
780 if (swapChannel)
781 {
782 tempMatrix = {0.500000f, -0.454153f, -0.045847f, 0.000000f, 0.212600f, 0.715200f, 0.072200f, 0.000000f, -0.114572f, -0.385428f, 0.500000f, 0.000000f};
783 }
784 else
785 {
786 tempMatrix = {-0.385428f, -0.114572f, 0.500000f, 0.000000f, 0.715200f, 0.212600f, 0.072200f, 0.000000f, -0.454153f, 0.500000f, -0.045847f, 0.000000f};
787 }
788 }
789 else if (dst == CSpace_BT2020)
790 {
791 if (swapChannel)
792 {
793 tempMatrix = {0.500000f, -0.459786f, -0.040214f, 0.000000f, 0.262700f, 0.678000f, 0.059300f, 0.000000f, -0.139630f, -0.360370f, 0.500000f, 0.000000f};
794 }
795 else
796 {
797 tempMatrix = {-0.360370f, -0.139630f, 0.500000f, 0.000000f, 0.678000f, 0.262700f, 0.059300f, 0.000000f, -0.459786f, 0.500000f, -0.040214f, 0.000000f};
798 }
799 }
800 else
801 {
802 VP_RENDER_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
803 }
804 VP_PUBLIC_CHK_STATUS_RETURN(TransferRgbToYuvMatrix(src, dst, tempMatrix, outMatrix));
805 }
806 else
807 {
808 VP_RENDER_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
809 }
810
811 return MOS_STATUS_SUCCESS;
812 }