xref: /aosp_15_r20/external/intel-media-driver/media_driver/linux/common/vp/ddi/media_libva_vp.c (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2009-2022, 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 //! \file     media_libva_vp.c
24 //! \brief    LibVA Video Processing extension interface implementation
25 //!
26 
27 #include <va/va.h>
28 #include <va/va_vpp.h>
29 #include <va/va_backend.h>
30 
31 #include <dlfcn.h>
32 #include <limits.h>
33 
34 #include "vphal_ddi.h"
35 
36 #include "media_libva.h"
37 #include "media_libva_vp.h"
38 #include "media_libva_util.h"
39 #include "hwinfo_linux.h"
40 #include "mos_solo_generic.h"
41 
42 #include "media_libva_vp_tools.h"
43 #if (_DEBUG || _RELEASE_INTERNAL)
44 #if ANDROID
45 #include "media_libva_vp_tools_android.h"
46 #endif
47 #endif // #if (_DEBUG || _RELEASE_INTERNAL)
48 
49 #define VP_SETTING_MAX_PHASES                           1
50 #define VP_SETTING_MEDIA_STATES                         32
51 #define VP_SETTING_SAME_SAMPLE_THRESHOLD                1000
52 
53 // ITU-T H.265 Table E.3: Colour Primaries
54 #define COLOUR_PRIMARY_BT2020            9
55 #define COLOUR_PRIMARY_BT709             1
56 #define COLOUR_PRIMARY_BT601             5
57 // ITU-T H.265 Table E.4 Transfer characteristics
58 #define TRANSFER_CHARACTERISTICS_BT709         1
59 #define TRANSFER_CHARACTERISTICS_GMAMA2P2      4
60 #define TRANSFER_CHARACTERISTICS_LINEAR        8
61 #define TRANSFER_CHARACTERISTICS_BT2020        14
62 #define TRANSFER_CHARACTERISTICS_ST2084        16
63 
64 VAStatus DdiMedia_MapBuffer (
65     VADriverContextP    ctx,
66     VABufferID          buf_id,
67     void                **pbuf
68 );
69 VAStatus DdiMedia_UnMapBuffer (
70     VADriverContextP    ctx,
71     VABufferID          buf_id
72 );
73 
74 // VP internal APIs to access VP acceleration capability in VPG drivers
75 VAStatus     DdiVp_InitVpHal(PDDI_VP_CONTEXT);
76 VAStatus     DdiVp_DestroyVpHal(PDDI_VP_CONTEXT);
77 VAStatus     DdiVp_DestroyVpHalSurface(PVPHAL_SURFACE pSurf);
78 VAStatus     DdiVp_DestroySrcParams(PDDI_VP_CONTEXT pVpCtx);
79 VAStatus     DdiVp_DestroyTargetParams(PDDI_VP_CONTEXT pVpCtx);
80 VAStatus     DdiVp_DestroyRenderParams(PDDI_VP_CONTEXT pVpCtx);
81 VAStatus     DdiVp_InitCtx(VADriverContextP, PDDI_VP_CONTEXT);
82 VAStatus     DdiVp_UpdateFilterParamBuffer(VADriverContextP, PDDI_VP_CONTEXT, uint32_t, int32_t, void *, uint32_t, DDI_VP_STATE*);
83 VAStatus     DdiVp_ClearFilterParamBuffer(PDDI_VP_CONTEXT , uint32_t, DDI_VP_STATE);
84 VAStatus     DdiVp_SetProcFilterDinterlaceParams(PDDI_VP_CONTEXT, uint32_t, VAProcFilterParameterBufferDeinterlacing*);
85 VAStatus     DdiVp_SetProcFilterDenoiseParams(PDDI_VP_CONTEXT, uint32_t, VAProcFilterParameterBuffer*);
86 VAStatus     DdiVp_SetProcFilterHVSDenoiseParams(PDDI_VP_CONTEXT, uint32_t, VAProcFilterParameterBufferHVSNoiseReduction*);
87 VAStatus     DdiVp_SetProcFilterSharpnessParams(PDDI_VP_CONTEXT, uint32_t, VAProcFilterParameterBuffer*);
88 VAStatus     DdiVp_SetProcFilterColorBalanceParams(PDDI_VP_CONTEXT, uint32_t, VAProcFilterParameterBufferColorBalance*, uint32_t );
89 VAStatus     DdiVp_SetProcFilterSkinToneEnhancementParams(PDDI_VP_CONTEXT, uint32_t, VAProcFilterParameterBuffer*);
90 VAStatus     DdiVp_SetProcFilterTotalColorCorrectionParams(PDDI_VP_CONTEXT, uint32_t, VAProcFilterParameterBufferTotalColorCorrection*, uint32_t);
91 VAStatus     DdiVp_SetProcFilterHdrTmParams(PDDI_VP_CONTEXT, uint32_t, VAProcFilterParameterBufferHDRToneMapping*);
92 VAStatus     DdiVp_SetProcPipelineBlendingParams(PDDI_VP_CONTEXT pVpCtx, uint32_t uiSurfIndex, VAProcPipelineParameterBuffer* pPipelineParam);
93 VAStatus     DdiVp_ConvertSurface (VADriverContextP ctx, DDI_MEDIA_SURFACE  *srcSurface, int16_t srcx,  int16_t srcy, uint16_t srcw,  uint16_t srch,  DDI_MEDIA_SURFACE  *dstSurface,  int16_t destx,  int16_t desty, uint16_t destw, uint16_t desth );
94 VAStatus     DdiVp_UpdateProcPipelineFutureReferenceFrames(PDDI_VP_CONTEXT pVpCtx, VADriverContextP pVaDrvCtx, PVPHAL_SURFACE pVpHalSrcSurf, VAProcPipelineParameterBuffer* pPipelineParam);
95 VAStatus     DdiVp_UpdateProcPipelinePastReferenceFrames(PDDI_VP_CONTEXT pVpCtx, VADriverContextP pVaDrvCtx, PVPHAL_SURFACE pVpHalSrcSurf, VAProcPipelineParameterBuffer* pPipelineParam);
96 VAStatus     DdiVp_UpdateVphalTargetSurfColorSpace(VADriverContextP, PDDI_VP_CONTEXT, VAProcPipelineParameterBuffer*, uint32_t targetIndex);
97 VAStatus     DdiVp_BeginPictureInt(VADriverContextP pVaDrvCtx, PDDI_VP_CONTEXT pVpCtx, VASurfaceID vaSurfID);
98 #if VA_CHECK_VERSION(1, 12, 0)
99 VAStatus     DdiVp_SetProcFilter3DLutParams(VADriverContextP pVaDrvCtx, PDDI_VP_CONTEXT pVpCtx, uint32_t uSurfIndex, VAProcFilterParameterBuffer3DLUT* p3DLutParamBuff);
100 #endif
101 #if (VA_MAJOR_VERSION < 1)
102 VAStatus     DdiVp_GetColorSpace(PVPHAL_SURFACE pVpHalSurf, VAProcColorStandardType colorStandard, uint32_t flag);
103 #else
104 VAStatus     DdiVp_GetColorSpace(PVPHAL_SURFACE pVpHalSurf, VAProcColorStandardType colorStandard, VAProcColorProperties colorProperties);
105 #endif
106 
107 
108 /////////////////////////////////////////////////////////////////////////////
109 //! \purpose Get vp context form context ID
110 //! \params
111 //! [in]  VADriverContextP
112 //! [in]  VAContextID
113 //! \returns PDDI_VP_CONTEXT
114 /////////////////////////////////////////////////////////////////////////////
DdiVp_GetVpContextFromContextID(VADriverContextP ctx,VAContextID vaCtxID)115 PDDI_VP_CONTEXT DdiVp_GetVpContextFromContextID(VADriverContextP ctx, VAContextID vaCtxID)
116 {
117     uint32_t  uiCtxType;
118     return (PDDI_VP_CONTEXT)DdiMedia_GetContextFromContextID(ctx, vaCtxID, &uiCtxType);
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 //! \purpose judge whether the pitch size match 16aligned usrptr path require or not
123 //! \params
124 //! [in]  pitch: surface pitch size.
125 //! [in]  format : surface foramt
126 //! \returns true if matched
127 //! for YV12 format, if pitch aligned with 128, go legacy path; if aligned with 16/32/64, go 16usrptr path
128 //! for other formats, legcy path for aligned with 64, 16usrpt path for aligned with 16/32
129 ////////////////////////////////////////////////////////////////////////////////
VpIs16UsrPtrPitch(uint32_t pitch,DDI_MEDIA_FORMAT format)130 bool VpIs16UsrPtrPitch(uint32_t pitch, DDI_MEDIA_FORMAT format)
131 {
132     uint32_t PitchAligned = 64;
133     bool status = false;
134 
135     if (Media_Format_YV12 == format)
136     {
137         PitchAligned = 128;
138     }
139 
140     if (!(pitch % 16) && (pitch % PitchAligned))
141     {
142         status = true;
143     }
144     else
145     {
146         status = false;
147     }
148 
149     VP_DDI_NORMALMESSAGE("[VP] 16Usrptr check, surface pitch is %d, go to %s path.", pitch, status?"16Usrptr":"legacy");
150 
151     return status;
152 }
153 
154 /////////////////////////////////////////////////////////////////////////////
155 //! \purpose map from media format to vphal format
156 //! \params
157 //! [in]  DDI_MEDIA_FORMAT
158 //! [out] None
159 //! \returns MOS_FORMAT
160 /////////////////////////////////////////////////////////////////////////////
VpGetFormatFromMediaFormat(DDI_MEDIA_FORMAT mf)161 MOS_FORMAT VpGetFormatFromMediaFormat(DDI_MEDIA_FORMAT mf)
162 {
163     MOS_FORMAT format = Format_Invalid;
164 
165     VP_DDI_FUNCTION_ENTER;
166 
167     switch (mf)
168     {
169     case Media_Format_NV12:
170         format = Format_NV12;
171         break;
172     case Media_Format_NV21:
173         format = Format_NV21;
174         break;
175     case Media_Format_X8R8G8B8:
176         format = Format_X8R8G8B8;
177         break;
178     case Media_Format_X8B8G8R8:
179         format = Format_X8B8G8R8;
180         break;
181     case Media_Format_A8R8G8B8:
182         format = Format_A8R8G8B8;
183         break;
184     case Media_Format_A8B8G8R8:
185     case Media_Format_R8G8B8A8:
186         format = Format_A8B8G8R8;
187         break;
188     case Media_Format_R5G6B5:
189         format = Format_R5G6B5;
190         break;
191     case Media_Format_R8G8B8:
192         format = Format_R8G8B8;
193         break;
194     case Media_Format_YUY2 :
195         format = Format_YUY2;
196         break;
197     case Media_Format_UYVY:
198         format = Format_UYVY;
199         break;
200     case Media_Format_YV12 :
201         format = Format_YV12;
202         break;
203     case Media_Format_I420 :
204         format = Format_I420;
205         break;
206     case Media_Format_IYUV :
207         format = Format_IYUV;
208         break;
209     case Media_Format_422H:
210         format = Format_422H;
211         break;
212     case Media_Format_422V:
213         format = Format_422V;
214         break;
215     case Media_Format_400P:
216         format = Format_400P;
217         break;
218     case Media_Format_411P:
219         format = Format_411P;
220         break;
221     case Media_Format_444P:
222         format = Format_444P;
223         break;
224     case Media_Format_IMC3:
225         format = Format_IMC3;
226         break;
227     case Media_Format_P010:
228         format = Format_P010;
229         break;
230     case Media_Format_P012:
231     case Media_Format_P016:
232         format = Format_P016;
233         break;
234     case Media_Format_R10G10B10A2:
235     case Media_Format_R10G10B10X2:
236         format = Format_R10G10B10A2;
237         break;
238     case Media_Format_B10G10R10A2:
239     case Media_Format_B10G10R10X2:
240         format =Format_B10G10R10A2;
241         break;
242     case Media_Format_RGBP:
243         format = Format_RGBP;
244         break;
245     case Media_Format_BGRP:
246         format = Format_BGRP;
247         break;
248     case Media_Format_Y210:
249         format = Format_Y210;
250         break;
251 #if VA_CHECK_VERSION(1, 9, 0)
252     case Media_Format_Y212:
253 #endif
254     case Media_Format_Y216:
255         format = Format_Y216;
256         break;
257     case Media_Format_Y410:
258         format = Format_Y410;
259         break;
260 #if VA_CHECK_VERSION(1, 9, 0)
261     case Media_Format_Y412:
262 #endif
263     case Media_Format_Y416:
264         format = Format_Y416;
265         break;
266     case Media_Format_AYUV:
267 #if VA_CHECK_VERSION(1, 13, 0)
268     case Media_Format_XYUV:
269 #endif
270         format = Format_AYUV;
271         break;
272     case Media_Format_Y8:
273         format = Format_Y8;
274         break;
275     case Media_Format_Y16S:
276         format = Format_Y16S;
277         break;
278     case Media_Format_Y16U:
279         format = Format_Y16U;
280         break;
281     case Media_Format_VYUY:
282         format = Format_VYUY;
283         break;
284     case Media_Format_YVYU:
285         format = Format_YVYU;
286         break;
287     case Media_Format_A16R16G16B16:
288         format = Format_A16R16G16B16;
289         break;
290     case Media_Format_A16B16G16R16:
291         format = Format_A16B16G16R16;
292         break;
293     default:
294         VP_DDI_ASSERTMESSAGE("ERROR media format to vphal format.");
295         format = Format_Invalid;
296         break;
297     }
298 
299     return format;
300 }
301 
302 /////////////////////////////////////////////////////////////////////////////
303 //! \purpose Get the VP Tile Type from Media Tile Type.
304 //! \params
305 //! [in]  mediaTileType : input mediaTileType
306 //! [out] None
307 //! \returns Vp tile Type if call succeeds
308 /////////////////////////////////////////////////////////////////////////////
309 
VpGetTileTypeFromMediaTileType(uint32_t mediaTileType)310 MOS_TILE_TYPE VpGetTileTypeFromMediaTileType(uint32_t mediaTileType)
311 {
312     MOS_TILE_TYPE tileType;
313 
314     VP_DDI_FUNCTION_ENTER;
315 
316     switch(mediaTileType)
317     {
318        case TILING_Y:
319            tileType = MOS_TILE_Y;
320            break;
321        case TILING_X:
322            tileType = MOS_TILE_X;
323            break;
324        case TILING_NONE:
325            tileType = MOS_TILE_LINEAR;
326            break;
327         default:
328            tileType = MOS_TILE_LINEAR;
329     }
330 
331     return tileType;
332 }
333 
334 /////////////////////////////////////////////////////////////////////////////
335 //! \purpose Get the render parameters  from Va Driver Context.
336 //! \params
337 //! [in]  pVpCtx : VP context
338 //! [out] None
339 //! \returns Pointer of render parameters
340 /////////////////////////////////////////////////////////////////////////////
VpGetRenderParams(PDDI_VP_CONTEXT pVpCtx)341 PVPHAL_RENDER_PARAMS VpGetRenderParams(PDDI_VP_CONTEXT pVpCtx)
342 {
343     VP_DDI_FUNCTION_ENTER;
344 
345     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", nullptr);
346     return pVpCtx->pVpHalRenderParams;
347 }
348 
349 /////////////////////////////////////////////////////////////////////////////
350 //! \purpose Destroy VPHAL Driver Reference Params
351 //! \params
352 //! [in]  pSurf : VPHAL surface
353 //! [out] None
354 //! \returns VA_STATUS_SUCCESS if call succeeds
355 /////////////////////////////////////////////////////////////////////////////
DdiVp_DestroyVpHalSurface(PVPHAL_SURFACE pSurf)356 VAStatus DdiVp_DestroyVpHalSurface(PVPHAL_SURFACE pSurf)
357 {
358     VP_DDI_FUNCTION_ENTER;
359     DDI_CHK_NULL(pSurf, "Null pSurf.", VA_STATUS_ERROR_INVALID_PARAMETER);
360 
361     if (pSurf->pFwdRef)
362     {
363         DdiVp_DestroyVpHalSurface(pSurf->pFwdRef);
364     }
365     if (pSurf->pBwdRef)
366     {
367         DdiVp_DestroyVpHalSurface(pSurf->pBwdRef);
368     }
369 
370     MOS_FreeMemAndSetNull(pSurf);
371 
372     return VA_STATUS_SUCCESS;
373 }
374 
375 /////////////////////////////////////////////////////////////////////////////
376 //! \purpose Destroy VPHAL Driver Source Params
377 //! \params
378 //! [in]  pVpCtx : VP context
379 //! [out] None
380 //! \returns VA_STATUS_SUCCESS if call succeeds
381 /////////////////////////////////////////////////////////////////////////////
382 VAStatus
DdiVp_DestroySrcParams(PDDI_VP_CONTEXT pVpCtx)383 DdiVp_DestroySrcParams(PDDI_VP_CONTEXT pVpCtx)
384 {
385     uint32_t uSurfIndex;
386 
387     VP_DDI_FUNCTION_ENTER;
388 
389     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
390 
391     // initialize
392     uSurfIndex = 0;
393 
394     if (nullptr != pVpCtx)
395     {
396         if (nullptr != pVpCtx->pVpHalRenderParams)
397         {
398             for (uSurfIndex = 0; uSurfIndex < VPHAL_MAX_SOURCES; uSurfIndex++)
399             {
400                 if (nullptr != pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex])
401                 {
402                     MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pProcampParams);
403                     MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pDeinterlaceParams);
404                     MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pDenoiseParams);
405                     if (pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pIEFParams)
406                     {
407                         MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pIEFParams->pExtParam);
408                         MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pIEFParams);
409                     }
410                     MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pBlendingParams);
411                     MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pLumaKeyParams);
412                     MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pColorPipeParams);
413                     MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pHDRParams);
414                     if (pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->p3DLutParams)
415                     {
416                         MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->p3DLutParams->pExt3DLutSurface);
417                     }
418                     MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->p3DLutParams);
419                     DdiVp_DestroyVpHalSurface(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]);
420                     pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex] = nullptr;
421                 }
422             }
423         }
424     }
425 
426     return VA_STATUS_SUCCESS;
427 }
428 
429 /////////////////////////////////////////////////////////////////////////////
430 //! \purpose Destroy VPHAL Driver Target Params
431 //! \params
432 //! [in]  pVpCtx : VP context
433 //! [out] None
434 //! \returns VA_STATUS_SUCCESS if call succeeds
435 /////////////////////////////////////////////////////////////////////////////
436 VAStatus
DdiVp_DestroyTargetParams(PDDI_VP_CONTEXT pVpCtx)437 DdiVp_DestroyTargetParams(PDDI_VP_CONTEXT pVpCtx)
438 {
439     PVPHAL_RENDER_PARAMS pParams;
440     PVPHAL_SURFACE       pTarget;
441     uint32_t             targetIndex;
442 
443     VP_DDI_FUNCTION_ENTER;
444 
445     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
446 
447     if (pVpCtx->pVpHalRenderParams)
448     {
449         pParams = pVpCtx->pVpHalRenderParams;
450         for (targetIndex = 0; targetIndex < VPHAL_MAX_TARGETS; targetIndex++)
451         {
452             pTarget = pParams->pTarget[targetIndex];
453             if (pTarget)
454             {
455                 if (pTarget->OsResource.bo)
456                 {
457                     pTarget->OsResource.bo = nullptr;
458                 }
459                 if (pTarget->pProcampParams)
460                 {
461                     MOS_FreeMemAndSetNull(pTarget->pProcampParams);
462                 }
463                 if (pTarget->pDeinterlaceParams)
464                 {
465                     MOS_FreeMemAndSetNull(pTarget->pDeinterlaceParams);
466                 }
467                 if (pTarget->pDenoiseParams)
468                 {
469                     MOS_FreeMemAndSetNull(pTarget->pDenoiseParams);
470                 }
471                 if (pTarget->pHDRParams)
472                 {
473                     MOS_FreeMemAndSetNull(pTarget->pHDRParams);
474                 }
475 
476                 MOS_FreeMemAndSetNull(pParams->pTarget[targetIndex]);
477             }
478         }
479         // reset render target count
480         pParams->uDstCount = 0;
481     }
482 
483     return VA_STATUS_SUCCESS;
484 }
485 
486 /////////////////////////////////////////////////////////////////////////////
487 //! \purpose Destroy VPHAL Driver Render Params
488 //! \params
489 //! [in]  pVpCtx : VP context
490 //! [out] None
491 //! \returns VA_STATUS_SUCCESS if call succeeds
492 /////////////////////////////////////////////////////////////////////////////
493 VAStatus
DdiVp_DestroyRenderParams(PDDI_VP_CONTEXT pVpCtx)494 DdiVp_DestroyRenderParams(PDDI_VP_CONTEXT pVpCtx)
495 {
496     VP_DDI_FUNCTION_ENTER;
497 
498     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
499 
500     DdiVp_DestroySrcParams(pVpCtx);
501     DdiVp_DestroyTargetParams(pVpCtx);
502 
503     if (nullptr != pVpCtx)
504     {
505         if (nullptr != pVpCtx->pVpHalRenderParams)
506         {
507             MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSplitScreenDemoModeParams);
508             MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pCompAlpha);
509             if (nullptr != pVpCtx->pVpHalRenderParams->pColorFillParams)
510             {
511                 MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pColorFillParams);
512             }
513             MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams);
514         }
515     }
516 
517     return VA_STATUS_SUCCESS;
518 }
519 
520 /////////////////////////////////////////////////////////////////////////////
521 //! \purpose Destroy VPHAL Driver context
522 //! \params
523 //! [in]  pVpCtx : VP context
524 //! [out] None
525 //! \returns VA_STATUS_SUCCESS if call succeeds
526 /////////////////////////////////////////////////////////////////////////////
527 VAStatus
DdiVp_DestroyVpHal(PDDI_VP_CONTEXT pVpCtx)528 DdiVp_DestroyVpHal(PDDI_VP_CONTEXT pVpCtx)
529 {
530     VP_DDI_FUNCTION_ENTER;
531 
532     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
533 
534     DdiVp_DestroyRenderParams(pVpCtx);
535 
536     // Destroy VPHAL context
537     if (nullptr != pVpCtx)
538     {
539         pVpCtx->MosDrvCtx.m_skuTable.reset();
540         pVpCtx->MosDrvCtx.m_waTable.reset();
541         if (nullptr != pVpCtx->pVpHal)
542         {
543             MOS_Delete(pVpCtx->pVpHal);
544             pVpCtx->pVpHal = nullptr;
545         }
546 
547 #if (_DEBUG || _RELEASE_INTERNAL)
548         VpDestoryDumpConfig(pVpCtx);
549 #endif //(_DEBUG || _RELEASE_INTERNAL)
550     }
551 
552     return VA_STATUS_SUCCESS;
553 }
554 
555 //////////////////////////////////////////////////////////////////////////////////////////////
556 //! \purpose Map VA Rotation flags to appropriate VPHAL Rotation params
557 //! \params
558 //! [in]  pVpHalSrcSurf
559 //! [in]  rotation_state
560 //! [out] None
561 //! \returns VA_STATUS_SUCCESS if call succeeds
562 /////////////////////////////////////////////////////////////////////////////////////////////
563 VAStatus
VpUpdateProcRotateState(PVPHAL_SURFACE pVpHalSrcSurf,uint32_t rotation_state)564 VpUpdateProcRotateState(PVPHAL_SURFACE pVpHalSrcSurf, uint32_t rotation_state)
565 {
566     VP_DDI_FUNCTION_ENTER;
567     DDI_CHK_NULL(pVpHalSrcSurf,  "Null pVpHalSrcSurf.",  VA_STATUS_ERROR_INVALID_PARAMETER);
568 
569     switch(rotation_state)
570     {
571         case VA_ROTATION_NONE:
572             pVpHalSrcSurf->Rotation = VPHAL_ROTATION_IDENTITY;
573             break;
574         case VA_ROTATION_90:
575             pVpHalSrcSurf->Rotation = VPHAL_ROTATION_90;
576             break;
577         case VA_ROTATION_180:
578             pVpHalSrcSurf->Rotation = VPHAL_ROTATION_180;
579             break;
580         case VA_ROTATION_270:
581             pVpHalSrcSurf->Rotation = VPHAL_ROTATION_270;
582             break;
583         default:
584             VP_DDI_ASSERTMESSAGE("VpUpdateProcRotateState rotation_state = %d is out of range.", rotation_state);
585             return VA_STATUS_ERROR_INVALID_PARAMETER;
586     }
587 
588     return VA_STATUS_SUCCESS;
589 }
590 
591 //////////////////////////////////////////////////////////////////////////////////////////////
592 //! \purpose Map VA Mirroring flags to appropriate VPHAL Mirroring params
593 //! \params
594 //! [in]  pVpHalSrcSurf
595 //! [in]  mirror_state
596 //! [out] None
597 //! \returns VA_STATUS_SUCCESS if call succeeds
598 /////////////////////////////////////////////////////////////////////////////////////////////
599 VAStatus
VpUpdateProcMirrorState(PVPHAL_SURFACE pVpHalSrcSurf,uint32_t mirror_state)600 VpUpdateProcMirrorState(PVPHAL_SURFACE pVpHalSrcSurf, uint32_t mirror_state)
601 {
602     VP_DDI_FUNCTION_ENTER;
603     DDI_CHK_NULL(pVpHalSrcSurf,  "Null pVpHalSrcSurf.",  VA_STATUS_ERROR_INVALID_PARAMETER);
604 
605     if(mirror_state > VA_MIRROR_VERTICAL)
606     {
607         VP_DDI_ASSERTMESSAGE("VpUpdateProcMirrorState mirror_state = %d is out of range.", mirror_state);
608         VP_DDI_ASSERTMESSAGE("VpUpdateProcMirrorState reset mirror_state to VA_MIRROR_NONE.");
609         mirror_state = VA_MIRROR_NONE;
610     }
611 
612     // Rotation must be a valid angle
613     switch(pVpHalSrcSurf->Rotation)
614     {
615         case VPHAL_ROTATION_IDENTITY:
616             if(mirror_state == VA_MIRROR_HORIZONTAL)
617             {
618                 pVpHalSrcSurf->Rotation = VPHAL_MIRROR_HORIZONTAL;
619             }
620             else if(mirror_state == VA_MIRROR_VERTICAL)
621             {
622                 pVpHalSrcSurf->Rotation = VPHAL_MIRROR_VERTICAL;
623             }
624             break;
625         case VPHAL_ROTATION_90:
626             if(mirror_state == VA_MIRROR_HORIZONTAL)
627             {
628                 pVpHalSrcSurf->Rotation = VPHAL_ROTATE_90_MIRROR_HORIZONTAL;
629             }
630             else if(mirror_state == VA_MIRROR_VERTICAL)
631             {
632                 pVpHalSrcSurf->Rotation = VPHAL_ROTATE_90_MIRROR_VERTICAL;
633             }
634             break;
635         case VPHAL_ROTATION_180:
636             if(mirror_state == VA_MIRROR_HORIZONTAL)
637             {
638                 pVpHalSrcSurf->Rotation =  VPHAL_MIRROR_VERTICAL;
639             }
640             else if(mirror_state == VA_MIRROR_VERTICAL)
641             {
642                 pVpHalSrcSurf->Rotation =  VPHAL_MIRROR_HORIZONTAL;
643             }
644             break;
645         case VPHAL_ROTATION_270:
646             if(mirror_state == VA_MIRROR_HORIZONTAL)
647             {
648                 pVpHalSrcSurf->Rotation =  VPHAL_ROTATE_90_MIRROR_VERTICAL;
649             }
650             else if(mirror_state == VA_MIRROR_VERTICAL)
651             {
652                 pVpHalSrcSurf->Rotation =  VPHAL_ROTATE_90_MIRROR_HORIZONTAL;
653             }
654             break;
655         default:
656             VP_DDI_ASSERTMESSAGE("VpUpdateProcMirrorState Unexpected Invalid Rotation = %d.", pVpHalSrcSurf->Rotation);
657             return VA_STATUS_ERROR_INVALID_PARAMETER;
658 
659     }
660 
661     return VA_STATUS_SUCCESS;
662 }
663 
664 //////////////////////////////////////////////////////////////////////////////////////////////
665 //! \purpose Check whether there is only Procamp with adjusting Brightness
666 //! \params
667 //! [in]  pVpHalSrcSurf
668 //! [out] None
669 //! \returns true if call succeeds
670 /////////////////////////////////////////////////////////////////////////////////////////////
IsProcmpEnable(PVPHAL_SURFACE pVpHalSrcSurf)671 bool IsProcmpEnable(PVPHAL_SURFACE pVpHalSrcSurf)
672 {
673     DDI_CHK_NULL(pVpHalSrcSurf, "Null pVpHalSrcSurf.", VA_STATUS_ERROR_INVALID_PARAMETER);
674 
675     if ((pVpHalSrcSurf->pProcampParams && pVpHalSrcSurf->pProcampParams->bEnabled) &&
676         (pVpHalSrcSurf->pProcampParams->fContrast == 1 && pVpHalSrcSurf->pProcampParams->fHue == 0 && pVpHalSrcSurf->pProcampParams->fSaturation == 1) &&
677         !pVpHalSrcSurf->pBlendingParams && !pVpHalSrcSurf->pLumaKeyParams && (!pVpHalSrcSurf->pIEFParams || !pVpHalSrcSurf->pIEFParams->bEnabled) &&
678         !pVpHalSrcSurf->pDeinterlaceParams && (!pVpHalSrcSurf->pDenoiseParams || (!pVpHalSrcSurf->pDenoiseParams->bEnableChroma && !pVpHalSrcSurf->pDenoiseParams->bEnableLuma)) &&
679         (!pVpHalSrcSurf->pColorPipeParams || (!pVpHalSrcSurf->pColorPipeParams->bEnableACE && !pVpHalSrcSurf->pColorPipeParams->bEnableSTE && !pVpHalSrcSurf->pColorPipeParams->bEnableTCC)) &&
680         !pVpHalSrcSurf->pHDRParams)
681     {
682         return true;
683     }
684 
685     return false;
686 }
687 
688 //////////////////////////////////////////////////////////////////////////////////////////////
689 //! \purpose Map Chroma Sitting flags to appropriate VPHAL chroma sitting params
690 //! \params
691 //! [in]  pVpHalSurf
692 //! [in]  chromasiting_state
693 //! [out] None
694 //! \returns VA_STATUS_SUCCESS if call succeeds
695 /////////////////////////////////////////////////////////////////////////////////////////////
696 VAStatus
VpUpdateProcChromaSittingState(PVPHAL_SURFACE pVpHalSurf,uint8_t chromasiting_state)697 VpUpdateProcChromaSittingState(PVPHAL_SURFACE pVpHalSurf, uint8_t chromasiting_state)
698 {
699     uint32_t uChromaSitingFlags  = 0;
700 
701     VP_DDI_FUNCTION_ENTER;
702     DDI_CHK_NULL(pVpHalSurf, "Null pVpHalSurf.", VA_STATUS_ERROR_INVALID_PARAMETER);
703 
704     // Chroma siting
705     // The lower 4 bits are still used as chroma-siting flag for output/input_surface_flag
706     // Set the vertical chroma siting info at bit 1:0
707     uChromaSitingFlags = chromasiting_state & 0x3;
708 
709     switch (uChromaSitingFlags)
710     {
711     case VA_CHROMA_SITING_VERTICAL_TOP:
712         pVpHalSurf->ChromaSiting = CHROMA_SITING_VERT_TOP;
713         break;
714     case VA_CHROMA_SITING_VERTICAL_CENTER:
715         pVpHalSurf->ChromaSiting = CHROMA_SITING_VERT_CENTER;
716         break;
717     case VA_CHROMA_SITING_VERTICAL_BOTTOM:
718         pVpHalSurf->ChromaSiting = CHROMA_SITING_VERT_BOTTOM;
719         break;
720     default:
721         pVpHalSurf->ChromaSiting = CHROMA_SITING_NONE;
722         break;
723     }
724 
725     if (pVpHalSurf->ChromaSiting != CHROMA_SITING_NONE)
726     {
727         // Set the horizontal chroma siting info at bit 3:2
728         uChromaSitingFlags = chromasiting_state & 0xc;
729 
730         switch (uChromaSitingFlags)
731         {
732         case VA_CHROMA_SITING_HORIZONTAL_LEFT:
733             pVpHalSurf->ChromaSiting = pVpHalSurf->ChromaSiting | CHROMA_SITING_HORZ_LEFT;
734             break;
735         case VA_CHROMA_SITING_HORIZONTAL_CENTER:
736             pVpHalSurf->ChromaSiting = pVpHalSurf->ChromaSiting | CHROMA_SITING_HORZ_CENTER;
737             break;
738         default:
739             pVpHalSurf->ChromaSiting = CHROMA_SITING_NONE;
740             break;
741         }
742     }
743 
744     return VA_STATUS_SUCCESS;
745 }
746 
747 //////////////////////////////////////////////////////////////////////////////////////////////
748 //! \purpose Set the appropriate HDR params according to colour standard, HDR metadata.
749 //! \params
750 //! [in]  pVpHalSurf : VPHAL Surface
751 //! [in]  pHdrMetadata: HDR metadata
752 //! [out] None
753 //! \returns VA_STATUS_SUCCESS if call succeeds
754 /////////////////////////////////////////////////////////////////////////////////////////////
VpUpdateProcHdrState(const PVPHAL_SURFACE pVpHalSurf,const VAHdrMetaData * pHdrMetadata)755 VAStatus VpUpdateProcHdrState(
756     const PVPHAL_SURFACE               pVpHalSurf,
757     const VAHdrMetaData*               pHdrMetadata)
758 {
759     DDI_CHK_NULL(pVpHalSurf, "Null pVpHalSurf.", VA_STATUS_ERROR_INVALID_BUFFER);
760 
761     // pass HDR metadata
762     if ((pHdrMetadata != nullptr) && (pHdrMetadata->metadata_size != 0))
763     {
764         if (pVpHalSurf->pHDRParams == nullptr)
765         {
766             pVpHalSurf->pHDRParams = (PVPHAL_HDR_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_HDR_PARAMS));
767             DDI_CHK_NULL(pVpHalSurf->pHDRParams, "VPHAL_HDR_PARAMS MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
768         }
769 
770         // HDR10 Meta Data
771         if (pHdrMetadata->metadata_type == VAProcHighDynamicRangeMetadataHDR10)
772         {
773             VP_DDI_NORMALMESSAGE("VpSetHdrParams HDR10 metadata.");
774             VAHdrMetaDataHDR10* pHDR10MetaData = (VAHdrMetaDataHDR10*)pHdrMetadata->metadata;
775             if (pHDR10MetaData)
776             {
777                 pVpHalSurf->pHDRParams->white_point_x = pHDR10MetaData->white_point_x;
778                 pVpHalSurf->pHDRParams->white_point_y = pHDR10MetaData->white_point_y;
779                 VP_DDI_NORMALMESSAGE("pHDR10MetaData white_point_x %d, white_point_y %d.", pHDR10MetaData->white_point_x, pHDR10MetaData->white_point_y);
780 
781                 // From VAAPI defintion which is following video spec, max/min_display_mastering_luminance are in units of 0.0001 candelas per square metre.
782                 uint32_t max_display_mastering_luminance = (pHDR10MetaData->max_display_mastering_luminance > 655350000 ) ? 655350000 : pHDR10MetaData->max_display_mastering_luminance;
783                 uint32_t min_display_mastering_luminance = (pHDR10MetaData->min_display_mastering_luminance > 655350000 ) ? 655350000 : pHDR10MetaData->min_display_mastering_luminance;
784                 pVpHalSurf->pHDRParams->max_display_mastering_luminance = (uint16_t)(max_display_mastering_luminance / 10000);
785                 pVpHalSurf->pHDRParams->min_display_mastering_luminance = (uint16_t)(min_display_mastering_luminance / 10000);
786                 VP_DDI_NORMALMESSAGE("pHDR10MetaData max_display_mastering_luminance %d, min_display_mastering_luminance %d.", pHDR10MetaData->max_display_mastering_luminance, pHDR10MetaData->min_display_mastering_luminance);
787 
788                 pVpHalSurf->pHDRParams->MaxCLL  = pHDR10MetaData->max_content_light_level;
789                 pVpHalSurf->pHDRParams->MaxFALL = pHDR10MetaData->max_pic_average_light_level;
790                 VP_DDI_NORMALMESSAGE("pHDR10MetaData MaxCLL %d, MaxFALL %d.", pHDR10MetaData->max_content_light_level, pHDR10MetaData->max_pic_average_light_level);
791 
792                 pVpHalSurf->pHDRParams->bAutoMode = false;
793 
794                 pVpHalSurf->pHDRParams->MaxCLL  = (pVpHalSurf->pHDRParams->MaxCLL == 0) ? HDR_DEFAULT_MAXCLL : pVpHalSurf->pHDRParams->MaxCLL;
795                 pVpHalSurf->pHDRParams->MaxFALL = (pVpHalSurf->pHDRParams->MaxFALL == 0) ? HDR_DEFAULT_MAXFALL : pVpHalSurf->pHDRParams->MaxFALL;
796 
797                 MOS_SecureMemcpy(pVpHalSurf->pHDRParams->display_primaries_x, 3 * sizeof(uint16_t), pHDR10MetaData->display_primaries_x, 3 * sizeof(uint16_t));
798                 MOS_SecureMemcpy(pVpHalSurf->pHDRParams->display_primaries_y, 3 * sizeof(uint16_t), pHDR10MetaData->display_primaries_y, 3 * sizeof(uint16_t));
799 
800                 switch (pVpHalSurf->GammaType)
801                 {
802                 case VPHAL_GAMMA_SMPTE_ST2084:
803                     pVpHalSurf->pHDRParams->EOTF = VPHAL_HDR_EOTF_SMPTE_ST2084;
804                     break;
805                 case VPHAL_GAMMA_BT1886:
806                     pVpHalSurf->pHDRParams->EOTF = VPHAL_HDR_EOTF_BT1886;
807                     break;
808                 default:
809                     pVpHalSurf->pHDRParams->EOTF = VPHAL_HDR_EOTF_TRADITIONAL_GAMMA_SDR;
810                     break;
811                 }
812                 VP_DDI_NORMALMESSAGE("max_display_mastering_luminance %d.", pVpHalSurf->pHDRParams->max_display_mastering_luminance);
813                 VP_DDI_NORMALMESSAGE("min_display_mastering_luminance %d.", pVpHalSurf->pHDRParams->min_display_mastering_luminance);
814                 VP_DDI_NORMALMESSAGE("GammaType %d.", pVpHalSurf->GammaType);
815             }
816         }
817     }
818 
819     return VA_STATUS_SUCCESS;
820 }
821 
822 //////////////////////////////////////////////////////////////////////////////////////////////
823 //! \purpose Extract VAProcPipelineParameterBuffer params for target surface and set the appropriate VPHAL params
824 //! \params
825 //! [in]  pVaDrvCtx : VA Driver context
826 //! [in]  pVpCtx : VP context
827 //! [in]  pPipelineParam : Pipeline parameters from application (VAProcPipelineParameterBuffer)
828 //! [out] None
829 //! \returns VA_STATUS_SUCCESS if call succeeds
830 /////////////////////////////////////////////////////////////////////////////////////////////
831 
832 VAStatus
VpSetRenderTargetParams(VADriverContextP pVaDrvCtx,PDDI_VP_CONTEXT pVpCtx,VAProcPipelineParameterBuffer * pPipelineParam)833 VpSetRenderTargetParams(
834     VADriverContextP                pVaDrvCtx,
835     PDDI_VP_CONTEXT                 pVpCtx,
836     VAProcPipelineParameterBuffer*  pPipelineParam)
837 {
838     PVPHAL_RENDER_PARAMS    VpHalRenderParams;
839     PDDI_MEDIA_CONTEXT      pMediaCtx;
840     PDDI_MEDIA_SURFACE      pMediaSrcSurf;
841     PVPHAL_SURFACE          pVpHalTgtSurf;
842 
843     VP_DDI_FUNCTION_ENTER;
844     DDI_CHK_NULL(pVaDrvCtx, "Null pVaDrvCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
845     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
846     DDI_CHK_NULL(pPipelineParam, "Null pPipelineParam.", VA_STATUS_ERROR_INVALID_BUFFER);
847 
848     pMediaCtx     = DdiMedia_GetMediaContext(pVaDrvCtx);
849     DDI_CHK_NULL(pMediaCtx, "Null pMediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
850     pMediaSrcSurf = DdiMedia_GetSurfaceFromVASurfaceID(pMediaCtx, pPipelineParam->surface);
851     DDI_CHK_NULL(pMediaSrcSurf, "Null pMediaSrcSurf.", VA_STATUS_ERROR_INVALID_BUFFER);
852     VpHalRenderParams  = pVpCtx->pVpHalRenderParams;
853     DDI_CHK_NULL(VpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
854 
855     DDI_ASSERT(VpHalRenderParams->uDstCount >= 1);
856     DDI_CHK_NULL(VpHalRenderParams->pTarget, "Null VpHalRenderParams->pTarget.", VA_STATUS_ERROR_INVALID_BUFFER);
857     pVpHalTgtSurf = VpHalRenderParams->pTarget[VpHalRenderParams->uDstCount-1];
858     DDI_CHK_NULL(pVpHalTgtSurf, "Null pVpHalTgtSurf.", VA_STATUS_ERROR_INVALID_BUFFER);
859 
860     if (pPipelineParam->surface_region != nullptr)
861     {
862         pVpHalTgtSurf->rcSrc.left   = pPipelineParam->surface_region->x;
863         pVpHalTgtSurf->rcSrc.top    = pPipelineParam->surface_region->y;
864         pVpHalTgtSurf->rcSrc.right  = pPipelineParam->surface_region->x + pPipelineParam->surface_region->width;
865         pVpHalTgtSurf->rcSrc.bottom = pPipelineParam->surface_region->y + pPipelineParam->surface_region->height;
866 
867         if (pVpHalTgtSurf->rcSrc.top < 0)
868         {
869             pVpHalTgtSurf->rcSrc.top = 0;
870         }
871 
872         if (pVpHalTgtSurf->rcSrc.left < 0)
873         {
874             pVpHalTgtSurf->rcSrc.left = 0;
875         }
876 
877         if (pVpHalTgtSurf->rcSrc.right > pMediaSrcSurf->iWidth)
878         {
879             pVpHalTgtSurf->rcSrc.right = pMediaSrcSurf->iWidth;
880         }
881 
882         if (pVpHalTgtSurf->rcSrc.bottom > pMediaSrcSurf->iHeight)
883         {
884             pVpHalTgtSurf->rcSrc.bottom = pMediaSrcSurf->iHeight;
885         }
886     }
887 
888     // Set dest rect
889     if (pPipelineParam->output_region != nullptr)
890     {
891         pVpHalTgtSurf->rcDst.left   = pPipelineParam->output_region->x;
892         pVpHalTgtSurf->rcDst.top    = pPipelineParam->output_region->y;
893         pVpHalTgtSurf->rcDst.right  = pPipelineParam->output_region->x + pPipelineParam->output_region->width;
894         pVpHalTgtSurf->rcDst.bottom = pPipelineParam->output_region->y + pPipelineParam->output_region->height;
895         if (pVpHalTgtSurf->rcDst.top < 0)
896         {
897             pVpHalTgtSurf->rcDst.top = 0;
898         }
899 
900         if (pVpHalTgtSurf->rcDst.left < 0)
901         {
902             pVpHalTgtSurf->rcDst.left = 0;
903         }
904 
905         if (pVpHalTgtSurf->rcDst.right > pMediaSrcSurf->iWidth)
906         {
907             pVpHalTgtSurf->rcDst.right = pMediaSrcSurf->iWidth;
908         }
909 
910         if (pVpHalTgtSurf->rcDst.bottom > pMediaSrcSurf->iHeight)
911         {
912             pVpHalTgtSurf->rcDst.bottom = pMediaSrcSurf->iHeight;
913         }
914     }
915 
916     if (IsProcmpEnable(pVpHalTgtSurf))
917     {
918         // correct the ChromaSitting location if Procamp is enabled.
919 #if (VA_MAJOR_VERSION < 1)
920         pPipelineParam->output_surface_flag = VA_CHROMA_SITING_HORIZONTAL_LEFT | VA_CHROMA_SITING_VERTICAL_TOP;
921 #else
922         pPipelineParam->output_color_properties.chroma_sample_location = VA_CHROMA_SITING_HORIZONTAL_LEFT | VA_CHROMA_SITING_VERTICAL_TOP;
923 #endif
924     }
925 
926 #if (VA_MAJOR_VERSION < 1)
927     VpUpdateProcChromaSittingState(pVpHalTgtSurf, (uint8_t)(pPipelineParam->output_surface_flag & 0xff));
928 #else
929     VpUpdateProcChromaSittingState(pVpHalTgtSurf, pPipelineParam->output_color_properties.chroma_sample_location);
930 #endif
931     return VA_STATUS_SUCCESS;
932 
933 }
934 
935 //////////////////////////////////////////////////////////////////////////////////////////////
936 //! \purpose Set Interpolation Method according to the flag
937 //! \params
938 //! [in]  PVPHAL_SURFACE : VA Surface
939 //! [in]  uInterpolationflags : Interpolation Flag
940 //! [out] None
941 //! \returns VA_STATUS_SUCCESS if call succeeds
942 /////////////////////////////////////////////////////////////////////////////////////////////
943 VAStatus
VpSetInterpolationParams(PVPHAL_SURFACE pSurface,uint32_t uInterpolationflags)944 VpSetInterpolationParams(
945     PVPHAL_SURFACE                  pSurface,
946     uint32_t                        uInterpolationflags)
947 {
948     DDI_CHK_NULL(pSurface, "Null pSurface.", VA_STATUS_ERROR_INVALID_SURFACE);
949     switch (uInterpolationflags)
950     {
951 #if VA_CHECK_VERSION(1, 9, 0)
952     case VA_FILTER_INTERPOLATION_NEAREST_NEIGHBOR:
953         pSurface->ScalingMode       = VPHAL_SCALING_NEAREST;
954         break;
955     case VA_FILTER_INTERPOLATION_BILINEAR:
956         pSurface->ScalingMode       = VPHAL_SCALING_BILINEAR;
957         break;
958     case VA_FILTER_INTERPOLATION_ADVANCED:
959     case VA_FILTER_INTERPOLATION_DEFAULT:
960 #endif
961     default:
962         pSurface->ScalingMode       = VPHAL_SCALING_AVS;
963         break;
964     }
965 
966     return VA_STATUS_SUCCESS;
967 }
968 
969 //////////////////////////////////////////////////////////////////////////////////////////////
970 //! \purpose Extract VAProcPipelineParameterBuffer params and set the appropriate VPHAL params
971 //! \params
972 //! [in]  pVaDrvCtx : VA Driver context
973 //! [in]  pVpCtx : VP context
974 //! [in]  pPipelineParam : Pipeline parameters from application (VAProcPipelineParameterBuffer)
975 //! [out] None
976 //! \returns VA_STATUS_SUCCESS if call succeeds
977 /////////////////////////////////////////////////////////////////////////////////////////////
978 VAStatus
DdiVp_SetProcPipelineParams(VADriverContextP pVaDrvCtx,PDDI_VP_CONTEXT pVpCtx,VAProcPipelineParameterBuffer * pPipelineParam)979 DdiVp_SetProcPipelineParams(
980     VADriverContextP                pVaDrvCtx,
981     PDDI_VP_CONTEXT                 pVpCtx,
982     VAProcPipelineParameterBuffer*  pPipelineParam)
983 {
984     PVPHAL_RENDER_PARAMS        pVpHalRenderParams;
985     PDDI_MEDIA_CONTEXT          pMediaCtx;
986     PDDI_MEDIA_SURFACE          pMediaSrcSurf;
987     PDDI_MEDIA_BUFFER           pFilterBuf;
988     void                        *pData;
989     uint32_t                    i;
990     PVPHAL_SURFACE              pVpHalSrcSurf;
991     PVPHAL_SURFACE              pVpHalTgtSurf;
992     uint32_t                    uSurfIndex;
993     uint32_t                    uScalingflags;
994     uint32_t                    uInterpolationflags;
995     uint32_t                    uChromaSitingFlags;
996     VAStatus                    vaStatus;
997     MOS_STATUS                  eStatus;
998     DDI_VP_STATE                vpStateFlags = {};
999     PMOS_INTERFACE              pOsInterface;
1000 
1001     VP_DDI_FUNCTION_ENTER;
1002     DDI_CHK_NULL(pVaDrvCtx, "Null pVaDrvCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
1003 
1004     // initialize
1005     pMediaCtx           = DdiMedia_GetMediaContext(pVaDrvCtx);
1006     DDI_CHK_NULL(pMediaCtx, "Null pMediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
1007     pMediaSrcSurf       = DdiMedia_GetSurfaceFromVASurfaceID(pMediaCtx, pPipelineParam->surface);
1008     pVpHalRenderParams  = pVpCtx->pVpHalRenderParams;
1009     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
1010     pFilterBuf          = nullptr;
1011     pData               = nullptr;
1012     uSurfIndex          = 0;
1013     pOsInterface        = pVpCtx->pVpHal->GetOsInterface();
1014     uInterpolationflags = 0;
1015 
1016     DDI_CHK_NULL(pMediaSrcSurf, "Null pMediaSrcSurf.", VA_STATUS_ERROR_INVALID_BUFFER);
1017     DDI_CHK_NULL(pOsInterface, "Null pOsInterface.", VA_STATUS_ERROR_INVALID_BUFFER);
1018 
1019     // increment surface count
1020     pVpHalRenderParams->uSrcCount++;
1021 
1022     // check if the surface count exceeded maximum VPHAL surfaces
1023     DDI_CHK_CONDITION((pVpHalRenderParams->uSrcCount > VPHAL_MAX_SOURCES),
1024             "Surface count exceeds maximum!", VA_STATUS_ERROR_MAX_NUM_EXCEEDED);
1025 
1026     // update surface uSurfIndex
1027     uSurfIndex = pVpHalRenderParams->uSrcCount - 1;
1028 
1029     pVpHalSrcSurf = pVpHalRenderParams->pSrc[uSurfIndex];
1030 
1031     pVpHalSrcSurf->Format   = VpGetFormatFromMediaFormat(pMediaSrcSurf->format);
1032     pVpHalSrcSurf->TileType = VpGetTileTypeFromMediaTileType(pMediaSrcSurf->TileType);
1033 
1034     DDI_CHK_CONDITION((Format_Invalid == pVpHalSrcSurf->Format),
1035             "Invalid surface media format!", VA_STATUS_ERROR_INVALID_PARAMETER);
1036 
1037 #if (_DEBUG || _RELEASE_INTERNAL)
1038     if (pVpCtx->pCurVpDumpDDIParam != nullptr &&
1039         pVpCtx->pCurVpDumpDDIParam->pPipelineParamBuffers[uSurfIndex] != nullptr)
1040     {
1041         MOS_SecureMemcpy(pVpCtx->pCurVpDumpDDIParam->pPipelineParamBuffers[uSurfIndex], sizeof(VAProcPipelineParameterBuffer), pPipelineParam, sizeof(VAProcPipelineParameterBuffer));
1042         pVpCtx->pCurVpDumpDDIParam->SrcFormat[uSurfIndex] = pVpHalSrcSurf->Format;
1043     }
1044 #endif //(_DEBUG || _RELEASE_INTERNAL)
1045 
1046     // Currently we only support 1 primary surface in VP
1047     if (pVpCtx->iPriSurfs < VP_MAX_PRIMARY_SURFS)
1048     {
1049         pVpHalSrcSurf->SurfType = SURF_IN_PRIMARY;
1050         pVpCtx->iPriSurfs++;
1051     }
1052     else
1053     {
1054         pVpHalSrcSurf->SurfType = SURF_IN_SUBSTREAM;
1055     }
1056 
1057     // Set workload path using pipeline_flags VA_PROC_PIPELINE_FAST flag
1058     if (pPipelineParam->pipeline_flags & VA_PROC_PIPELINE_FAST)
1059     {
1060         pVpHalRenderParams->bForceToRender = true;
1061     }
1062 
1063     // Set src rect
1064     if (pPipelineParam->surface_region != nullptr)
1065     {
1066         pVpHalSrcSurf->rcSrc.top    = pPipelineParam->surface_region->y;
1067         pVpHalSrcSurf->rcSrc.left   = pPipelineParam->surface_region->x;
1068         pVpHalSrcSurf->rcSrc.right  = pPipelineParam->surface_region->x + pPipelineParam->surface_region->width;
1069         pVpHalSrcSurf->rcSrc.bottom = pPipelineParam->surface_region->y + pPipelineParam->surface_region->height;
1070 
1071         if (pVpHalSrcSurf->rcSrc.top < 0)
1072         {
1073             pVpHalSrcSurf->rcSrc.top = 0;
1074         }
1075 
1076         if (pVpHalSrcSurf->rcSrc.left < 0)
1077         {
1078             pVpHalSrcSurf->rcSrc.left = 0;
1079         }
1080 
1081         if (pVpHalSrcSurf->rcSrc.right > pMediaSrcSurf->iWidth)
1082         {
1083             pVpHalSrcSurf->rcSrc.right = pMediaSrcSurf->iWidth;
1084         }
1085 
1086         if (pVpHalSrcSurf->rcSrc.bottom > pMediaSrcSurf->iRealHeight)
1087         {
1088             pVpHalSrcSurf->rcSrc.bottom = pMediaSrcSurf->iRealHeight;
1089         }
1090     }
1091     else
1092     {
1093         // nullptr surface_region implies the whole surface
1094         pVpHalSrcSurf->rcSrc.top    = 0;
1095         pVpHalSrcSurf->rcSrc.left   = 0;
1096         pVpHalSrcSurf->rcSrc.right  = pMediaSrcSurf->iWidth;
1097         pVpHalSrcSurf->rcSrc.bottom = pMediaSrcSurf->iRealHeight;
1098     }
1099 
1100     pVpHalTgtSurf = pVpHalRenderParams->pTarget[0];
1101     DDI_CHK_NULL(pVpHalTgtSurf, "Null pVpHalTgtSurf.", VA_STATUS_ERROR_UNKNOWN);
1102     // Set dest rect
1103     if (pPipelineParam->output_region != nullptr)
1104     {
1105         pVpHalSrcSurf->rcDst.top    = pPipelineParam->output_region->y;
1106         pVpHalSrcSurf->rcDst.left   = pPipelineParam->output_region->x;
1107         pVpHalSrcSurf->rcDst.right  = pPipelineParam->output_region->x + pPipelineParam->output_region->width;
1108         pVpHalSrcSurf->rcDst.bottom = pPipelineParam->output_region->y + pPipelineParam->output_region->height;
1109     }
1110     else
1111     {
1112         pVpHalSrcSurf->rcDst.top    = 0;
1113         pVpHalSrcSurf->rcDst.left   = 0;
1114         pVpHalSrcSurf->rcDst.right  = pVpHalTgtSurf->rcDst.right;
1115         pVpHalSrcSurf->rcDst.bottom = pVpHalTgtSurf->rcDst.bottom;
1116     }
1117 
1118     if ((pVpHalTgtSurf->rcSrc.right < pVpHalSrcSurf->rcDst.right) ||
1119         (pVpHalTgtSurf->rcSrc.bottom < pVpHalSrcSurf->rcDst.bottom))
1120     {
1121         DDI_CHK_CONDITION(true, "Invalid color fill parameter!", VA_STATUS_ERROR_INVALID_PARAMETER);
1122     }
1123 
1124     //set the frame_id
1125     pVpHalSrcSurf->FrameID = pMediaSrcSurf->frame_idx;
1126 
1127     //In order to refresh the frame ID in DdiVp_UpdateFilterParamBuffer when run ADI case,
1128     //we need to set the OS resource here.
1129     pVpHalSrcSurf->OsResource.bo          = pMediaSrcSurf->bo;
1130 
1131     // csc option
1132     //---------------------------------------
1133     // Set color space for src
1134 #if (VA_MAJOR_VERSION < 1)
1135     vaStatus = DdiVp_GetColorSpace(pVpHalSrcSurf, pPipelineParam->surface_color_standard, pPipelineParam->input_surface_flag);
1136 #else
1137     vaStatus = DdiVp_GetColorSpace(pVpHalSrcSurf, pPipelineParam->surface_color_standard, pPipelineParam->input_color_properties);
1138 #endif
1139     DDI_CHK_RET(vaStatus, "Unsupport Color space!");
1140 
1141     if(pMediaSrcSurf->format == Media_Format_400P)
1142     {
1143         pVpHalSrcSurf->ColorSpace = CSpace_BT601Gray;
1144     }
1145 
1146     // extended gamut? RGB can't have extended gamut flag
1147     pVpHalSrcSurf->ExtendedGamut = false;
1148 
1149     // Background Colorfill
1150     // According to libva  definition, if alpha in output background color is zero, then colorfill is not needed
1151     if ((pPipelineParam->output_background_color >> 24) != 0 || pVpHalTgtSurf->ColorSpace == CSpace_sRGB)
1152     {
1153         if (pVpHalRenderParams->pColorFillParams == nullptr)
1154         {
1155             pVpHalRenderParams->pColorFillParams = (PVPHAL_COLORFILL_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_COLORFILL_PARAMS));
1156         }
1157 
1158         DDI_CHK_NULL(pVpHalRenderParams->pColorFillParams, "Null pColorFillParams.", VA_STATUS_ERROR_UNKNOWN);
1159 
1160         if (pVpHalTgtSurf->ColorSpace == CSpace_sRGB && (pPipelineParam->output_background_color >> 24) == 0)
1161         {
1162             // set color space for sRGB output
1163             pVpHalRenderParams->pColorFillParams->CSpace    = CSpace_sRGB;
1164         }
1165         else
1166         {
1167             // set background colorfill option
1168             pVpHalRenderParams->pColorFillParams->Color     = pPipelineParam->output_background_color;
1169             pVpHalRenderParams->pColorFillParams->bYCbCr    = false;
1170             pVpHalRenderParams->pColorFillParams->CSpace    = CSpace_sRGB;
1171         }
1172     }else
1173     {
1174         MOS_FreeMemAndSetNull(pVpHalRenderParams->pColorFillParams);
1175     }
1176 
1177     // Set Demo Mode option
1178     if (pVpHalRenderParams->bDisableDemoMode == false)
1179     {
1180         eStatus = VpHal_DdiSetupSplitScreenDemoMode(
1181             0,  // No DDI setting on Linux. Set it when Linux DDI supports it
1182             0,  // No DDI setting on Linux. Set it when Linux DDI supports it
1183             &pVpHalRenderParams->pSplitScreenDemoModeParams,
1184             &pVpHalRenderParams->bDisableDemoMode,
1185             pOsInterface);
1186         if (MOS_STATUS_SUCCESS != eStatus)
1187         {
1188             VP_DDI_ASSERTMESSAGE("Failed to setup Split-Screen Demo Mode.");
1189             MOS_FreeMemAndSetNull(pVpHalRenderParams->pSplitScreenDemoModeParams);
1190         }
1191     }
1192 
1193     // Update fwd and bkward ref frames: Required for Advanced processing - will be supported in the future
1194 
1195     pVpHalSrcSurf->uFwdRefCount  = pPipelineParam->num_backward_references;
1196 
1197     vaStatus = DdiVp_UpdateProcPipelineFutureReferenceFrames(pVpCtx, pVaDrvCtx, pVpHalSrcSurf, pPipelineParam);
1198     DDI_CHK_RET(vaStatus, "Failed to update future  reference frames!");
1199 
1200     pVpHalSrcSurf->uBwdRefCount  = pPipelineParam->num_forward_references;
1201 
1202     vaStatus = DdiVp_UpdateProcPipelinePastReferenceFrames(pVpCtx, pVaDrvCtx, pVpHalSrcSurf, pPipelineParam);
1203     DDI_CHK_RET(vaStatus, "Failed to update past reference frames!");
1204 
1205     // Check if filter values changed,if yes, then reset all filters for this surface
1206 
1207     // intialize the filter parameter
1208     // Parameters once set in the surface will be keep till video complete
1209     // so the parameter of rendered surface should be intialized every time
1210     if (pVpHalRenderParams->bStereoMode)
1211     {
1212         pVpHalSrcSurf->Rotation = VPHAL_ROTATION_IDENTITY;
1213     }
1214 
1215     // Progressive or interlaced - Check filter_flags
1216     // if the deinterlace parameters is not set, manipulate it as a progressive video.
1217     if (pPipelineParam->filter_flags & VA_TOP_FIELD)
1218     {
1219         pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_TOP_FIELD;
1220     }
1221     else if (pPipelineParam->filter_flags & VA_BOTTOM_FIELD)
1222     {
1223         pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_BOTTOM_FIELD;
1224     }
1225     else // VA_FRAME_PICTURE
1226     {
1227         pVpHalSrcSurf->SampleType = SAMPLE_PROGRESSIVE;
1228 
1229         //if the deinterlace parameters is set, clear it.
1230         if (pVpHalSrcSurf->pDeinterlaceParams != nullptr)
1231         {
1232             MOS_FreeMemAndSetNull(pVpHalSrcSurf->pDeinterlaceParams);
1233         }
1234     }
1235 
1236     for (i = 0; i < pPipelineParam->num_filters; i++)
1237     {
1238         VABufferID filter = pPipelineParam->filters[i];
1239 
1240         pFilterBuf = DdiMedia_GetBufferFromVABufferID(pMediaCtx, filter);
1241         DDI_CHK_NULL(pFilterBuf, "Null pFilterBuf!", VA_STATUS_ERROR_INVALID_BUFFER);
1242 
1243         DDI_CHK_CONDITION((VAProcFilterParameterBufferType != pFilterBuf->uiType),
1244                 "Invalid parameter buffer type!", VA_STATUS_ERROR_INVALID_PARAMETER);
1245 
1246         // Map Buffer data to virtual addres space
1247         DdiMedia_MapBuffer(pVaDrvCtx, filter, &pData);
1248 
1249         VAProcFilterParameterBufferBase* filter_param = (VAProcFilterParameterBufferBase*) pData;
1250 
1251         // HSBC can only be applied to the primary layer
1252         if (!((filter_param->type == VAProcFilterColorBalance)
1253               && pVpHalSrcSurf->SurfType != SURF_IN_PRIMARY))
1254         {
1255             // Pass the filter type
1256             vaStatus = DdiVp_UpdateFilterParamBuffer(pVaDrvCtx, pVpCtx, uSurfIndex, filter_param->type, pData, pFilterBuf->uiNumElements, &vpStateFlags);
1257             DDI_CHK_RET(vaStatus, "Failed to update parameter buffer!");
1258         }
1259     }
1260 
1261     DdiVp_ClearFilterParamBuffer(pVpCtx, uSurfIndex, vpStateFlags);
1262 
1263     // Update the Deinterlace params
1264     //vaStatus = DdiVp_UpdateProcDeinterlaceParams(pVaDrvCtx, pVpHalSrcSurf, pPipelineParam);
1265     //DDI_CHK_RET(vaStatus, "Failed to update vphal advance deinterlace!");
1266 
1267     // Use Render to do scaling for resolution larger than 8K
1268     if(MEDIA_IS_WA(&pMediaCtx->WaTable, WaDisableVeboxFor8K))
1269     {
1270         pVpHalRenderParams->bDisableVeboxFor8K = true;
1271     }
1272 
1273     // Scaling algorithm
1274     uScalingflags                    = pPipelineParam->filter_flags & VA_FILTER_SCALING_MASK;
1275     pVpHalSrcSurf->ScalingPreference = VPHAL_SCALING_PREFER_SFC;    // default
1276     // Interpolation method
1277 #if VA_CHECK_VERSION(1, 9, 0)
1278     uInterpolationflags              = pPipelineParam->filter_flags & VA_FILTER_INTERPOLATION_MASK;
1279 #endif
1280     switch (uScalingflags)
1281     {
1282     case VA_FILTER_SCALING_FAST:
1283         if (pVpHalSrcSurf->SurfType == SURF_IN_PRIMARY)
1284         {
1285             VpSetInterpolationParams(pVpHalSrcSurf, uInterpolationflags);
1286             pVpHalSrcSurf->ScalingPreference = VPHAL_SCALING_PREFER_SFC_FOR_VEBOX;
1287         }
1288         else
1289         {
1290             pVpHalSrcSurf->ScalingMode       = VPHAL_SCALING_BILINEAR;
1291         }
1292         break;
1293     case VA_FILTER_SCALING_HQ:
1294         if (pVpHalSrcSurf->SurfType == SURF_IN_PRIMARY)
1295         {
1296             VpSetInterpolationParams(pVpHalSrcSurf, uInterpolationflags);
1297             pVpHalSrcSurf->ScalingPreference = VPHAL_SCALING_PREFER_COMP;
1298         }
1299         else
1300         {
1301             pVpHalSrcSurf->ScalingMode       = VPHAL_SCALING_BILINEAR;
1302         }
1303         break;
1304     case VA_FILTER_SCALING_DEFAULT:
1305     case VA_FILTER_SCALING_NL_ANAMORPHIC:
1306     default:
1307         if (pVpHalSrcSurf->SurfType == SURF_IN_PRIMARY)
1308         {
1309             VpSetInterpolationParams(pVpHalSrcSurf, uInterpolationflags);
1310             pVpHalSrcSurf->ScalingPreference = VPHAL_SCALING_PREFER_SFC;
1311         }
1312         else
1313         {
1314             pVpHalSrcSurf->ScalingMode       = VPHAL_SCALING_BILINEAR;
1315         }
1316         break;
1317     }
1318     //init interlace scaling flag
1319     pVpHalSrcSurf->bInterlacedScaling = false;
1320     pVpHalSrcSurf->bFieldWeaving      = false;
1321 
1322     if (pVpHalSrcSurf->pDeinterlaceParams == nullptr)
1323     {
1324         if (pPipelineParam->input_surface_flag & VA_TOP_FIELD_FIRST)
1325         {
1326             if (pPipelineParam->output_surface_flag & VA_TOP_FIELD_FIRST)
1327             {
1328                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_INTERLEAVED_TO_INTERLEAVED;
1329                 pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_TOP_FIELD;
1330                 pVpHalSrcSurf->bInterlacedScaling = true;
1331                 pVpHalSrcSurf->bFieldWeaving = false;
1332             }else if (pPipelineParam->output_surface_flag & VA_TOP_FIELD)
1333             {
1334                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_INTERLEAVED_TO_FIELD;
1335                 pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_TOP_FIELD;
1336                 pVpHalTgtSurf->SampleType = SAMPLE_SINGLE_TOP_FIELD;
1337                 pVpHalSrcSurf->bInterlacedScaling = false;
1338                 pVpHalSrcSurf->bFieldWeaving = false;
1339             }else if (pPipelineParam->output_surface_flag & VA_BOTTOM_FIELD)
1340             {
1341                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_INTERLEAVED_TO_FIELD;
1342                 pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_BOTTOM_FIELD;
1343                 pVpHalTgtSurf->SampleType = SAMPLE_SINGLE_BOTTOM_FIELD;
1344                 pVpHalSrcSurf->bInterlacedScaling = false;
1345                 pVpHalSrcSurf->bFieldWeaving = false;
1346             }else
1347             {
1348                 VP_DDI_ASSERTMESSAGE("output_surface_flag need to be set for interlaced scaling.");
1349                 pVpHalSrcSurf->SampleType = SAMPLE_PROGRESSIVE;
1350                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_NONE;
1351                 pVpHalSrcSurf->bInterlacedScaling = false;
1352                 pVpHalSrcSurf->bFieldWeaving = false;
1353             }
1354         }
1355         else if (pPipelineParam->input_surface_flag & VA_BOTTOM_FIELD_FIRST)
1356         {
1357             if (pPipelineParam->output_surface_flag & VA_BOTTOM_FIELD_FIRST)
1358             {
1359                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_INTERLEAVED_TO_INTERLEAVED;
1360                 pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_ODD_FIRST_BOTTOM_FIELD;
1361                 pVpHalSrcSurf->bInterlacedScaling = true;
1362                 pVpHalSrcSurf->bFieldWeaving = false;
1363             }else if (pPipelineParam->output_surface_flag & VA_TOP_FIELD)
1364             {
1365                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_INTERLEAVED_TO_FIELD;
1366                 pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_ODD_FIRST_TOP_FIELD;
1367                 pVpHalTgtSurf->SampleType = SAMPLE_SINGLE_TOP_FIELD;
1368                 pVpHalSrcSurf->bInterlacedScaling = false;
1369                 pVpHalSrcSurf->bFieldWeaving = false;
1370             }else if (pPipelineParam->output_surface_flag & VA_BOTTOM_FIELD)
1371             {
1372                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_INTERLEAVED_TO_FIELD;
1373                 pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_ODD_FIRST_BOTTOM_FIELD;
1374                 pVpHalTgtSurf->SampleType = SAMPLE_SINGLE_BOTTOM_FIELD;
1375                 pVpHalSrcSurf->bInterlacedScaling = false;
1376                 pVpHalSrcSurf->bFieldWeaving = false;
1377             }else
1378             {
1379                 VP_DDI_ASSERTMESSAGE("output_surface_flag need to be set for interlaced scaling.");
1380                 pVpHalSrcSurf->SampleType = SAMPLE_PROGRESSIVE;
1381                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_NONE;
1382                 pVpHalSrcSurf->bInterlacedScaling = false;
1383                 pVpHalSrcSurf->bFieldWeaving = false;
1384             }
1385         }else if (pPipelineParam->input_surface_flag & VA_TOP_FIELD)
1386         {
1387             if (pPipelineParam->output_surface_flag & VA_TOP_FIELD_FIRST)
1388             {
1389                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_FIELD_TO_INTERLEAVED;
1390                 pVpHalSrcSurf->SampleType = SAMPLE_SINGLE_TOP_FIELD;
1391                 pVpHalTgtSurf->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_TOP_FIELD;
1392 
1393                 DDI_CHK_NULL(pVpHalSrcSurf->pBwdRef, "No Ref Field!", VA_STATUS_ERROR_UNIMPLEMENTED);
1394                 pVpHalSrcSurf->pBwdRef->InterlacedScalingType = ISCALING_FIELD_TO_INTERLEAVED;
1395                 pVpHalSrcSurf->pBwdRef->SampleType = SAMPLE_SINGLE_BOTTOM_FIELD;
1396 
1397                 pVpHalSrcSurf->bInterlacedScaling = false;
1398                 pVpHalSrcSurf->bFieldWeaving = true;
1399             }else if(pPipelineParam->output_surface_flag & VA_TOP_FIELD)
1400             {
1401                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_FIELD_TO_FIELD;
1402                 pVpHalSrcSurf->SampleType = SAMPLE_SINGLE_TOP_FIELD;
1403                 pVpHalSrcSurf->bInterlacedScaling = false;
1404                 pVpHalSrcSurf->bFieldWeaving = false;
1405             }else
1406             {
1407                 pVpHalSrcSurf->SampleType = SAMPLE_PROGRESSIVE;
1408                 pVpHalSrcSurf->bInterlacedScaling = false;
1409                 pVpHalSrcSurf->bFieldWeaving = false;
1410             }
1411         }else if (pPipelineParam->input_surface_flag & VA_BOTTOM_FIELD)
1412         {
1413             if (pPipelineParam->output_surface_flag & VA_BOTTOM_FIELD_FIRST)
1414             {
1415                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_FIELD_TO_INTERLEAVED;
1416                 pVpHalSrcSurf->SampleType = SAMPLE_SINGLE_BOTTOM_FIELD;
1417                 pVpHalTgtSurf->SampleType = SAMPLE_INTERLEAVED_ODD_FIRST_BOTTOM_FIELD;
1418 
1419                 DDI_CHK_NULL(pVpHalSrcSurf->pBwdRef, "No Ref Field!", VA_STATUS_ERROR_UNIMPLEMENTED);
1420                 pVpHalSrcSurf->pBwdRef->InterlacedScalingType = ISCALING_FIELD_TO_INTERLEAVED;
1421                 pVpHalSrcSurf->pBwdRef->SampleType = SAMPLE_SINGLE_TOP_FIELD;
1422 
1423                 pVpHalSrcSurf->bInterlacedScaling = false;
1424                 pVpHalSrcSurf->bFieldWeaving = true;
1425             }else if(pPipelineParam->output_surface_flag & VA_BOTTOM_FIELD)
1426             {
1427                 pVpHalSrcSurf->InterlacedScalingType = ISCALING_FIELD_TO_FIELD;
1428                 pVpHalSrcSurf->SampleType = SAMPLE_SINGLE_BOTTOM_FIELD;
1429                 pVpHalSrcSurf->bInterlacedScaling = false;
1430                 pVpHalSrcSurf->bFieldWeaving = false;
1431             }else
1432             {
1433                 pVpHalSrcSurf->SampleType = SAMPLE_PROGRESSIVE;
1434                 pVpHalSrcSurf->bInterlacedScaling = false;
1435                 pVpHalSrcSurf->bFieldWeaving = false;
1436             }
1437         }else
1438         {
1439             pVpHalSrcSurf->SampleType = SAMPLE_PROGRESSIVE;
1440             pVpHalSrcSurf->InterlacedScalingType = ISCALING_NONE;
1441             pVpHalSrcSurf->bInterlacedScaling = false;
1442             pVpHalSrcSurf->bFieldWeaving = false;
1443         }
1444     }
1445 
1446     // For legacy interlace scaling
1447     if (pVpHalSrcSurf->pDeinterlaceParams == nullptr && pVpHalSrcSurf->InterlacedScalingType == ISCALING_NONE)
1448     {
1449         if (pPipelineParam->filter_flags & VA_TOP_FIELD)
1450         {
1451             pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_TOP_FIELD;
1452             pVpHalSrcSurf->ScalingMode = VPHAL_SCALING_AVS;
1453             pVpHalSrcSurf->bInterlacedScaling = true;
1454             pVpHalSrcSurf->InterlacedScalingType = ISCALING_INTERLEAVED_TO_INTERLEAVED;
1455         }
1456         else if (pPipelineParam->filter_flags & VA_BOTTOM_FIELD)
1457         {
1458             pVpHalSrcSurf->SampleType = SAMPLE_INTERLEAVED_ODD_FIRST_BOTTOM_FIELD;
1459             pVpHalSrcSurf->ScalingMode = VPHAL_SCALING_AVS;
1460             pVpHalSrcSurf->bInterlacedScaling = true;
1461             pVpHalSrcSurf->InterlacedScalingType = ISCALING_INTERLEAVED_TO_INTERLEAVED;
1462         }
1463 
1464         // Kernel does not support 3-plane interlaced AVS, so for 3-plane interlaced scaling, need to use bilinear.
1465         if (pVpHalSrcSurf->bInterlacedScaling && IS_PL3_FORMAT(pVpHalSrcSurf->Format))
1466         {
1467             pVpHalSrcSurf->ScalingMode = VPHAL_SCALING_BILINEAR;
1468         }
1469     }
1470     // For weave DI
1471     if (pVpHalSrcSurf->pDeinterlaceParams == nullptr)
1472     {
1473        if ((pPipelineParam->filter_flags & 0x00000004) || ((pPipelineParam->filter_flags & VA_TOP_FIELD) && pVpHalSrcSurf->bFieldWeaving))
1474        {
1475            pVpHalSrcSurf->SampleType = SAMPLE_SINGLE_TOP_FIELD;
1476            if (pVpHalSrcSurf->pBwdRef != nullptr)
1477            {
1478                pVpHalSrcSurf->pBwdRef->SampleType = SAMPLE_SINGLE_BOTTOM_FIELD;
1479            }
1480            pVpHalSrcSurf->ScalingMode = VPHAL_SCALING_BILINEAR;
1481            pVpHalSrcSurf->bFieldWeaving = true;
1482        }
1483 
1484        if ((pPipelineParam->filter_flags & 0x00000008) || ((pPipelineParam->filter_flags & VA_BOTTOM_FIELD) && pVpHalSrcSurf->bFieldWeaving))
1485        {
1486            pVpHalSrcSurf->SampleType = SAMPLE_SINGLE_BOTTOM_FIELD;
1487            if (pVpHalSrcSurf->pBwdRef != nullptr)
1488            {
1489                pVpHalSrcSurf->pBwdRef->SampleType = SAMPLE_SINGLE_TOP_FIELD;
1490            }
1491            pVpHalSrcSurf->ScalingMode = VPHAL_SCALING_BILINEAR;
1492            pVpHalSrcSurf->bFieldWeaving = true;
1493        }
1494     }
1495 
1496     // Rotation
1497     vaStatus = VpUpdateProcRotateState(pVpHalSrcSurf,pPipelineParam->rotation_state);
1498     DDI_CHK_RET(vaStatus, "Failed to update rotate state!");
1499 
1500     // Mirror
1501     vaStatus = VpUpdateProcMirrorState(pVpHalSrcSurf,pPipelineParam->mirror_state);
1502     DDI_CHK_RET(vaStatus, "Failed to update mirror state!");
1503 
1504     // Alpha blending
1505     // Note: the alpha blending region cannot overlay
1506     vaStatus = DdiVp_SetProcPipelineBlendingParams(pVpCtx, uSurfIndex, pPipelineParam);
1507     DDI_CHK_RET(vaStatus, "Failed to update Alpha Blending parameter!");
1508 
1509     if (IsProcmpEnable(pVpHalSrcSurf))
1510     {
1511         // correct the ChromaSitting location if Procamp is enabled.
1512 #if (VA_MAJOR_VERSION < 1)
1513         pPipelineParam->input_surface_flag = VA_CHROMA_SITING_HORIZONTAL_LEFT | VA_CHROMA_SITING_VERTICAL_TOP;
1514         pPipelineParam->output_surface_flag = VA_CHROMA_SITING_HORIZONTAL_LEFT | VA_CHROMA_SITING_VERTICAL_TOP;
1515 #else
1516         pPipelineParam->input_color_properties.chroma_sample_location = VA_CHROMA_SITING_HORIZONTAL_LEFT | VA_CHROMA_SITING_VERTICAL_TOP;
1517         pPipelineParam->output_color_properties.chroma_sample_location = VA_CHROMA_SITING_HORIZONTAL_LEFT | VA_CHROMA_SITING_VERTICAL_TOP;
1518 #endif
1519     }
1520 
1521 #if (VA_MAJOR_VERSION < 1)
1522     VpUpdateProcChromaSittingState(pVpHalSrcSurf, (uint8_t)(pPipelineParam->input_surface_flag&0xff));
1523     VpUpdateProcChromaSittingState(pVpHalTgtSurf, (uint8_t)(pPipelineParam->output_surface_flag&0xff));
1524 #else
1525     VpUpdateProcChromaSittingState(pVpHalSrcSurf, pPipelineParam->input_color_properties.chroma_sample_location);
1526     VpUpdateProcChromaSittingState(pVpHalTgtSurf, pPipelineParam->output_color_properties.chroma_sample_location);
1527 #endif
1528     // update OsResource for src surface
1529     pVpHalSrcSurf->OsResource.Format      = VpGetFormatFromMediaFormat(pMediaSrcSurf->format);
1530     pVpHalSrcSurf->OsResource.iWidth      = pMediaSrcSurf->iWidth;
1531     pVpHalSrcSurf->OsResource.iHeight     = pMediaSrcSurf->iHeight;
1532     pVpHalSrcSurf->OsResource.iPitch      = pMediaSrcSurf->iPitch;
1533     pVpHalSrcSurf->OsResource.iCount      = pMediaSrcSurf->iRefCount;
1534     pVpHalSrcSurf->OsResource.bo          = pMediaSrcSurf->bo;
1535     pVpHalSrcSurf->OsResource.TileType    = VpGetTileTypeFromMediaTileType(pMediaSrcSurf->TileType);
1536     pVpHalSrcSurf->OsResource.pGmmResInfo = pMediaSrcSurf->pGmmResourceInfo;
1537 
1538     Mos_Solo_SetOsResource(pMediaSrcSurf->pGmmResourceInfo, &pVpHalSrcSurf->OsResource);
1539 
1540     //Set encryption bit for input surface
1541     //This setting only used for secure VPP test app which do secure VP and provide secure YUV as input
1542     //Since APP cannot set encryption flag, it will set input_surface_flag to ask driver add encryption bit to input surface
1543     if (pOsInterface->osCpInterface->IsHMEnabled() && (pPipelineParam->input_surface_flag & VPHAL_SURFACE_ENCRYPTION_FLAG))
1544     {
1545         pOsInterface->osCpInterface->SetResourceEncryption(&(pVpHalSrcSurf->OsResource), true);
1546     }
1547 
1548     // Update the Render Target params - this needs to be done once when Render Target is passed via BeginPicture
1549     vaStatus = DdiVp_UpdateVphalTargetSurfColorSpace(pVaDrvCtx, pVpCtx, pPipelineParam, 0);
1550     DDI_CHK_RET(vaStatus, "Failed to update vphal target surface color space!");
1551     // Update the Render Target HDR params - this needs to be done once when Render Target is passed via BeginPicture
1552     vaStatus = VpUpdateProcHdrState(pVpHalTgtSurf, pPipelineParam->output_hdr_metadata);
1553     DDI_CHK_RET(vaStatus, "Failed to update vphal target surface HDR metadata!");
1554 
1555     // Using additional_outputs processing as 1:N case.
1556     for (i = 0; i < pPipelineParam->num_additional_outputs; i++)
1557     {
1558         vaStatus = DdiVp_BeginPictureInt(pVaDrvCtx, pVpCtx, pPipelineParam->additional_outputs[i]);
1559         DDI_CHK_RET(vaStatus, "Failed to update vphal target surface buffers!");
1560         vaStatus = DdiVp_UpdateVphalTargetSurfColorSpace(pVaDrvCtx, pVpCtx, pPipelineParam, i+1);
1561         DDI_CHK_RET(vaStatus, "Failed to update vphal target surface color space!");
1562     }
1563 
1564     // add 16aligned UsrPtr mode support
1565     if (pMediaSrcSurf->pSurfDesc && (pMediaSrcSurf->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR))
1566     {
1567         pVpHalSrcSurf->b16UsrPtr = VpIs16UsrPtrPitch(pMediaSrcSurf->iPitch, pMediaSrcSurf->format);
1568     }
1569     else
1570     {
1571         pVpHalSrcSurf->b16UsrPtr = false;
1572     }
1573 
1574     return VA_STATUS_SUCCESS;
1575 }
1576 
1577 //////////////////////////////////////////////////////////////////////////////////////////////
1578 //! \purpose:    Get resource informaton from target surface, set OS Resource for VPHAL
1579 //! \[in]  pVpCtx : VP context
1580 //! \[in]  pboRt : media surface
1581 //! \[in]  targetIndex : index of target surface in VpHal RenderParams.
1582 //! \[out] None
1583 //! \returns:    Result of operation
1584 //////////////////////////////////////////////////////////////////////////////////////////////
VpSetOsResource(PDDI_VP_CONTEXT pVpCtx,PDDI_MEDIA_SURFACE pboRt,uint32_t targetIndex)1585 VAStatus VpSetOsResource(
1586     PDDI_VP_CONTEXT     pVpCtx,
1587     PDDI_MEDIA_SURFACE  pboRt,
1588     uint32_t            targetIndex)
1589 {
1590     PMOS_RESOURCE           pOsResource;
1591     PVPHAL_RENDER_PARAMS    pVpHalRenderParams;
1592 
1593     VP_DDI_FUNCTION_ENTER;
1594     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
1595 
1596     // get vphal render parameters
1597     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
1598     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
1599 
1600     // set render target os resource information according to target surface
1601     pOsResource              = &pVpHalRenderParams->pTarget[targetIndex]->OsResource;
1602     DDI_CHK_NULL(pOsResource, "Null  pOsResource.", VA_STATUS_ERROR_INVALID_PARAMETER);
1603     pOsResource->bo          = pboRt->bo;
1604     pOsResource->bMapped     = pboRt->bMapped;
1605     pOsResource->Format      = VpGetFormatFromMediaFormat( pboRt->format );
1606     pOsResource->iWidth      = pboRt->iWidth;
1607     pOsResource->iHeight     = pboRt->iHeight;
1608     pOsResource->iPitch      = pboRt->iPitch;
1609     pOsResource->iCount      = pboRt->iRefCount;
1610     pOsResource->TileType    = VpGetTileTypeFromMediaTileType(pboRt->TileType);
1611     pOsResource->pGmmResInfo = pboRt->pGmmResourceInfo;
1612 
1613     Mos_Solo_SetOsResource(pboRt->pGmmResourceInfo, pOsResource);
1614 
1615     return VA_STATUS_SUCCESS;
1616 }
1617 
1618 //////////////////////////////////////////////////////////////////////////////////////////////
1619 //! \purpose Helper function for VpAllocateDrvCtxExt to Allocate PDDI_VP_CONTEXT
1620 //! \params
1621 //! [in]  pVaDrvCtx : VA Driver context
1622 //! [in]  pVpCtx : VP context
1623 //! [out] None
1624 //! \returns VA_STATUS_SUCCESS if call succeeds
1625 /////////////////////////////////////////////////////////////////////////////////////////////
DdiVp_InitCtx(VADriverContextP pVaDrvCtx,PDDI_VP_CONTEXT pVpCtx)1626 VAStatus DdiVp_InitCtx(VADriverContextP pVaDrvCtx, PDDI_VP_CONTEXT pVpCtx)
1627 {
1628     PERF_UTILITY_AUTO(__FUNCTION__, PERF_VP, PERF_LEVEL_DDI);
1629 
1630     PVPHAL_RENDER_PARAMS            pVpHalRenderParams = nullptr;
1631     int32_t                         uSurfIndex;
1632     PDDI_MEDIA_CONTEXT              pMediaCtx;
1633     VAStatus                        vaStatus;
1634 
1635     VP_DDI_FUNCTION_ENTER;
1636     DDI_CHK_NULL(pVaDrvCtx, "Null pVaDrvCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
1637 
1638     pMediaCtx = DdiMedia_GetMediaContext(pVaDrvCtx);
1639     DDI_CHK_NULL(pMediaCtx, "Null pMediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
1640 
1641     // initialize VPHAL
1642     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
1643     pVpCtx->MosDrvCtx.bufmgr          = pMediaCtx->pDrmBufMgr;
1644     pVpCtx->MosDrvCtx.m_gpuContextMgr = pMediaCtx->m_gpuContextMgr;
1645     pVpCtx->MosDrvCtx.m_cmdBufMgr     = pMediaCtx->m_cmdBufMgr;
1646     pVpCtx->MosDrvCtx.fd              = pMediaCtx->fd;
1647     pVpCtx->MosDrvCtx.iDeviceId       = pMediaCtx->iDeviceId;
1648     pVpCtx->MosDrvCtx.m_skuTable      = pMediaCtx->SkuTable;
1649     pVpCtx->MosDrvCtx.m_waTable       = pMediaCtx->WaTable;
1650     pVpCtx->MosDrvCtx.m_gtSystemInfo  = *pMediaCtx->pGtSystemInfo;
1651     pVpCtx->MosDrvCtx.m_platform      = pMediaCtx->platform;
1652     pVpCtx->MosDrvCtx.m_auxTableMgr   = pMediaCtx->m_auxTableMgr;
1653     pVpCtx->MosDrvCtx.pGmmClientContext = pMediaCtx->pGmmClientContext;
1654     pVpCtx->MosDrvCtx.ppMediaMemDecompState = &pMediaCtx->pMediaMemDecompState;
1655     pVpCtx->MosDrvCtx.pfnMediaMemoryCopy    = pMediaCtx->pfnMediaMemoryCopy;
1656     pVpCtx->MosDrvCtx.pfnMediaMemoryCopy2D  = pMediaCtx->pfnMediaMemoryCopy2D;
1657     pVpCtx->MosDrvCtx.pfnMemoryDecompress   = pMediaCtx->pfnMemoryDecompress;
1658     pVpCtx->MosDrvCtx.ppMediaCopyState      = &pMediaCtx->pMediaCopyState;
1659 
1660     pVpCtx->MosDrvCtx.m_osDeviceContext     = pMediaCtx->m_osDeviceContext;
1661     pVpCtx->MosDrvCtx.m_apoMosEnabled       = pMediaCtx->m_apoMosEnabled;
1662     pVpCtx->MosDrvCtx.m_userSettingPtr      = pMediaCtx->m_userSettingPtr;
1663 
1664     pVpCtx->MosDrvCtx.pPerfData = (PERF_DATA *)MOS_AllocAndZeroMemory(sizeof(PERF_DATA));
1665     if (nullptr == pVpCtx->MosDrvCtx.pPerfData)
1666     {
1667         return VA_STATUS_ERROR_ALLOCATION_FAILED;
1668     }
1669 
1670     // initialize DDI level cp interface
1671     pVpCtx->pCpDdiInterface = Create_DdiCpInterface(pVpCtx->MosDrvCtx);
1672     if (nullptr == pVpCtx->pCpDdiInterface)
1673     {
1674          vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1675          goto FINISH;
1676     }
1677 
1678 
1679     DDI_CHK_RET(DdiVp_InitVpHal(pVpCtx), "Call DdiVp_InitVpHal failed");
1680 
1681     // allocate vphal render param
1682     pVpHalRenderParams = (PVPHAL_RENDER_PARAMS)(MOS_AllocAndZeroMemory(sizeof(VPHAL_RENDER_PARAMS)));
1683     if( nullptr == pVpHalRenderParams)
1684     {
1685         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1686         goto FINISH;
1687     }
1688 
1689     // initialize vphal render params
1690     for (uSurfIndex = 0; uSurfIndex < VPHAL_MAX_SOURCES; uSurfIndex++)
1691     {
1692         pVpHalRenderParams->pSrc[uSurfIndex] = (PVPHAL_SURFACE)MOS_AllocAndZeroMemory(sizeof(VPHAL_SURFACE));
1693         if( nullptr == pVpHalRenderParams->pSrc[uSurfIndex])
1694         {
1695             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1696             goto FINISH;
1697         }
1698     }
1699 
1700     for (uSurfIndex = 0; uSurfIndex < VPHAL_MAX_TARGETS; uSurfIndex++)
1701     {
1702         pVpHalRenderParams->pTarget[uSurfIndex] = (PVPHAL_SURFACE)MOS_AllocAndZeroMemory(sizeof(VPHAL_SURFACE));
1703         if( nullptr == pVpHalRenderParams->pTarget[uSurfIndex])
1704         {
1705             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1706             goto FINISH;
1707         }
1708     }
1709 
1710     // background Colorfill
1711     pVpHalRenderParams->pColorFillParams = (PVPHAL_COLORFILL_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_COLORFILL_PARAMS));
1712     if( nullptr == pVpHalRenderParams->pColorFillParams)
1713     {
1714         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1715         goto FINISH;
1716     }
1717 
1718     // reset source surface count
1719     pVpHalRenderParams->uSrcCount = 0;
1720     pVpCtx->MosDrvCtx.wRevision = 0;
1721     pVpCtx->iPriSurfs           = 0;
1722 
1723     // Add the render param for calculating alpha value.
1724     // Because can not pass the alpha calculate flag from iVP,
1725     // need to hardcode here for both Android and Linux.
1726     pVpHalRenderParams->bCalculatingAlpha = true;
1727 
1728     // put the render param in vp context
1729     pVpCtx->pVpHalRenderParams    = pVpHalRenderParams;
1730 
1731 #if (_DEBUG || _RELEASE_INTERNAL)
1732     vaStatus = VpInitDumpConfig(pVpCtx);
1733     DDI_CHK_RET( vaStatus, "Init Dump Config failed");
1734 #endif //(_DEBUG || _RELEASE_INTERNAL)
1735 
1736     return VA_STATUS_SUCCESS;
1737 
1738 FINISH:
1739 
1740     if(pVpHalRenderParams)
1741     {
1742         for (uSurfIndex = 0; uSurfIndex < VPHAL_MAX_SOURCES; uSurfIndex++)
1743         {
1744             MOS_FreeMemory(pVpHalRenderParams->pSrc[uSurfIndex]);
1745         }
1746         for (uSurfIndex = 0; uSurfIndex < VPHAL_MAX_TARGETS; uSurfIndex++)
1747         {
1748             MOS_FreeMemory(pVpHalRenderParams->pTarget[uSurfIndex]);
1749         }
1750 
1751         MOS_FreeMemory(pVpHalRenderParams->pColorFillParams);
1752         MOS_FreeMemory(pVpHalRenderParams);
1753     }
1754 
1755     if (pVpCtx->pCpDdiInterface)
1756     {
1757         Delete_DdiCpInterface(pVpCtx->pCpDdiInterface);
1758         pVpCtx->pCpDdiInterface = NULL;
1759     }
1760 
1761 #if (_DEBUG || _RELEASE_INTERNAL)
1762     VpDestoryDumpConfig(pVpCtx);
1763 #endif //(_DEBUG || _RELEASE_INTERNAL)
1764 
1765     return vaStatus;
1766 }
1767 
1768 //////////////////////////////////////////////////////////////////////////////////////////////
1769 //! \purpose Initialize VPHAL State and VPHAL settings per VP context
1770 //! \params
1771 //! [in]  pVpCtx : VP context
1772 //! [out] None
1773 //! \returns VA_STATUS_SUCCESS if call succeeds
1774 /////////////////////////////////////////////////////////////////////////////////////////////
1775 VAStatus
DdiVp_InitVpHal(PDDI_VP_CONTEXT pVpCtx)1776 DdiVp_InitVpHal(
1777     PDDI_VP_CONTEXT   pVpCtx
1778 )
1779 {
1780     PERF_UTILITY_AUTO(__FUNCTION__, PERF_VP, PERF_LEVEL_DDI);
1781 
1782     VpBase                    *pVpHal;
1783     VphalSettings             VpHalSettings;
1784 
1785     VAStatus                  vaStatus;
1786 
1787     VP_DDI_FUNCTION_ENTER;
1788     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
1789 
1790     // initialize
1791     vaStatus            = VA_STATUS_ERROR_UNKNOWN;
1792     pVpHal              = nullptr;
1793 
1794     // Create VpHal state
1795     MOS_STATUS eStatus = MOS_STATUS_UNKNOWN;
1796     pVpHal = VpBase::VphalStateFactory( nullptr, &(pVpCtx->MosDrvCtx), &eStatus);
1797 
1798     if (pVpHal && MOS_FAILED(eStatus))
1799     {
1800         MOS_Delete(pVpHal);
1801         pVpHal = nullptr;
1802     }
1803 
1804     if (!pVpHal)
1805     {
1806         VP_DDI_ASSERTMESSAGE("Failed to create vphal.");
1807         MOS_Delete(pVpCtx);
1808         return VA_STATUS_ERROR_ALLOCATION_FAILED;
1809     }
1810 
1811     if (nullptr != pVpHal)
1812     {
1813         VpHalSettings.maxPhases                = VP_SETTING_MAX_PHASES;
1814         VpHalSettings.mediaStates              = VP_SETTING_MEDIA_STATES;
1815         VpHalSettings.sameSampleThreshold      = VP_SETTING_SAME_SAMPLE_THRESHOLD;
1816         VpHalSettings.disableDnDi              = false;
1817 
1818         // Allocate resources (state heaps, resources, KDLL)
1819         if (MOS_FAILED(pVpHal->Allocate(&VpHalSettings)))
1820         {
1821             VP_DDI_ASSERTMESSAGE("Failed to allocate resources for vphal.");
1822             MOS_Delete(pVpHal);
1823             pVpHal = nullptr;
1824             return vaStatus;
1825         }
1826     }
1827 
1828     pVpCtx->pVpHal  = pVpHal;
1829 
1830     return VA_STATUS_SUCCESS;
1831 }
1832 
1833 //////////////////////////////////////////////////////////////////////////////////////////////
1834 ////! \purpose Set Color Standard Explictly.
1835 ////! \params
1836 ////! [in]  pVpHalSurf : src/target surface
1837 ////! [in]  colorStandard : VA color standard VAProcColorStandardType
1838 ////! [in]  colorProperties : input/output surface color properties
1839 ////! [out] None
1840 ////! \returns appropriate VA_STATUS_SUCCESS if call succeeds
1841 ///////////////////////////////////////////////////////////////////////////////////////////////
VpSetColorStandardExplictly(PVPHAL_SURFACE pVpHalSurf,VAProcColorStandardType colorStandard,VAProcColorProperties colorProperties)1842 VAStatus VpSetColorStandardExplictly(PVPHAL_SURFACE pVpHalSurf, VAProcColorStandardType colorStandard, VAProcColorProperties colorProperties)
1843 {
1844     DDI_CHK_NULL(pVpHalSurf, "Null pVpHalSurf.", VA_STATUS_ERROR_INVALID_SURFACE);
1845     DDI_CHK_CONDITION((colorStandard != VAProcColorStandardExplicit), "Not Explict color standard, Exit!", VA_STATUS_ERROR_INVALID_PARAMETER);
1846 
1847     if (IS_RGB_FORMAT(pVpHalSurf->Format))
1848     {
1849         switch(colorProperties.colour_primaries)
1850         {
1851         case COLOUR_PRIMARY_BT2020:
1852             pVpHalSurf->ColorSpace = (colorProperties.color_range & VA_SOURCE_RANGE_REDUCED) ? CSpace_BT2020_stRGB : CSpace_BT2020_RGB;
1853             break;
1854         case COLOUR_PRIMARY_BT709:
1855         case COLOUR_PRIMARY_BT601:
1856             pVpHalSurf->ColorSpace = (colorProperties.color_range & VA_SOURCE_RANGE_REDUCED) ? CSpace_stRGB: CSpace_sRGB;
1857             break;
1858         default:
1859             pVpHalSurf->ColorSpace = CSpace_sRGB;
1860             VP_DDI_ASSERTMESSAGE("unknown Color Standard for RGB format.");
1861             break;
1862         }
1863     }
1864 
1865     if (IS_YUV_FORMAT(pVpHalSurf->Format) || IS_ALPHA_YUV_FORMAT(pVpHalSurf->Format))
1866     {
1867         switch(colorProperties.colour_primaries)
1868         {
1869         case COLOUR_PRIMARY_BT2020:
1870             pVpHalSurf->ColorSpace = (colorProperties.color_range & VA_SOURCE_RANGE_FULL) ? CSpace_BT2020_FullRange : CSpace_BT2020;
1871             break;
1872         case COLOUR_PRIMARY_BT709:
1873             pVpHalSurf->ColorSpace = (colorProperties.color_range & VA_SOURCE_RANGE_FULL) ? CSpace_BT709_FullRange : CSpace_BT709;
1874             break;
1875         case COLOUR_PRIMARY_BT601:
1876             pVpHalSurf->ColorSpace = (colorProperties.color_range & VA_SOURCE_RANGE_FULL) ? CSpace_BT601_FullRange : CSpace_BT601;
1877             break;
1878         default:
1879             pVpHalSurf->ColorSpace = CSpace_BT601;
1880             VP_DDI_ASSERTMESSAGE("unknown Color Standard for YUV format.");
1881             break;
1882         }
1883     }
1884 
1885     switch(colorProperties.transfer_characteristics)
1886     {
1887     case TRANSFER_CHARACTERISTICS_ST2084:
1888         pVpHalSurf->GammaType = VPHAL_GAMMA_SMPTE_ST2084;
1889         break;
1890     default:
1891         pVpHalSurf->GammaType = VPHAL_GAMMA_TRADITIONAL_GAMMA;
1892         break;
1893     }
1894 
1895     return VA_STATUS_SUCCESS;
1896 }
1897 
1898 //////////////////////////////////////////////////////////////////////////////////////////////
1899 ////! \purpose Convert VAProcColorStandardType to VPHAL_CSPACE
1900 ////! \params
1901 ////! [in]  pVpHalSurf : src/target surface
1902 ////! [in]  colorStandard : VA color standard VAProcColorStandardType
1903 ////! [in]  flag : input/output surface flag for full/reduced color range
1904 ////! [out] None
1905 ////! \returns appropriate VPHAL_CSPACE if call succeeds
1906 ///////////////////////////////////////////////////////////////////////////////////////////////
1907 #if (VA_MAJOR_VERSION < 1)
DdiVp_GetColorSpace(PVPHAL_SURFACE pVpHalSurf,VAProcColorStandardType colorStandard,uint32_t flag)1908 VAStatus DdiVp_GetColorSpace(PVPHAL_SURFACE pVpHalSurf, VAProcColorStandardType colorStandard, uint32_t flag)
1909 #else
1910 VAStatus DdiVp_GetColorSpace(PVPHAL_SURFACE pVpHalSurf, VAProcColorStandardType colorStandard, VAProcColorProperties colorProperties)
1911 #endif
1912 {
1913     uint8_t color_range = colorProperties.color_range;
1914 
1915     pVpHalSurf->ColorSpace = CSpace_None;
1916 
1917     VP_DDI_FUNCTION_ENTER;
1918 
1919     // Convert VAProcColorStandardType to VPHAL_CSPACE
1920     if (IS_RGB_FORMAT(pVpHalSurf->Format) || (pVpHalSurf->Format == Format_P8))
1921     {
1922         switch (colorStandard)
1923         {
1924             case VAProcColorStandardBT2020:
1925 #if (VA_MAJOR_VERSION < 1)
1926                 if (flag & VA_SOURCE_RANGE_FULL)
1927 #else
1928                 if (color_range == VA_SOURCE_RANGE_FULL)
1929 #endif
1930                 {
1931                     pVpHalSurf->ColorSpace = CSpace_BT2020_RGB;
1932                 }
1933                 else
1934                 {
1935                     pVpHalSurf->ColorSpace = CSpace_BT2020_stRGB;
1936                 }
1937                 break;
1938             case VAProcColorStandardSTRGB:
1939                 pVpHalSurf->ColorSpace = CSpace_stRGB;
1940                 break;
1941             case VAProcColorStandardExplicit:
1942                 VpSetColorStandardExplictly(pVpHalSurf, colorStandard, colorProperties);
1943                 break;
1944             case VAProcColorStandardSRGB:
1945             default:
1946                 pVpHalSurf->ColorSpace = CSpace_sRGB;
1947                 break;
1948         }
1949     }
1950     else
1951     {
1952         // Set colorspace by default to avoid application don't set ColorStandard
1953         if (colorStandard == 0)
1954         {
1955             if ((pVpHalSurf->rcSrc.right - pVpHalSurf->rcSrc.left) <= 1280 && (pVpHalSurf->rcSrc.bottom - pVpHalSurf->rcSrc.top) <= 720)
1956             {
1957                 pVpHalSurf->ColorSpace = CSpace_BT601;
1958             }//720p
1959             else if ((pVpHalSurf->rcSrc.right - pVpHalSurf->rcSrc.left) <= 1920 && (pVpHalSurf->rcSrc.bottom - pVpHalSurf->rcSrc.top) <= 1080)
1960             {
1961                 pVpHalSurf->ColorSpace = CSpace_BT709;
1962             }//1080p
1963             else
1964             {
1965                 if (pVpHalSurf->Format == Format_P010 || pVpHalSurf->Format == Format_P016)
1966                 {
1967                     pVpHalSurf->ColorSpace = CSpace_BT2020;
1968                 }
1969                 else
1970                 {
1971                     pVpHalSurf->ColorSpace = CSpace_BT709;
1972                 }
1973             }//4K
1974         }
1975         else
1976         {
1977             switch (colorStandard)
1978             {
1979                 case VAProcColorStandardBT709:
1980 #if (VA_MAJOR_VERSION < 1)
1981                     if (flag & VA_SOURCE_RANGE_FULL)
1982 #else
1983                     if (color_range == VA_SOURCE_RANGE_FULL)
1984 #endif
1985                     {
1986                         pVpHalSurf->ColorSpace = CSpace_BT709_FullRange;
1987                     }
1988                     else
1989                     {
1990                         pVpHalSurf->ColorSpace = CSpace_BT709;
1991                     }
1992                     break;
1993                 case VAProcColorStandardBT601:
1994 #if (VA_MAJOR_VERSION < 1)
1995                     if (flag & VA_SOURCE_RANGE_FULL)
1996 #else
1997                     if (color_range == VA_SOURCE_RANGE_FULL)
1998 #endif
1999                     {
2000                         pVpHalSurf->ColorSpace = CSpace_BT601_FullRange;
2001                     }
2002                     else
2003                     {
2004                         pVpHalSurf->ColorSpace = CSpace_BT601;
2005                     }
2006                     break;
2007                 case VAProcColorStandardBT2020:
2008 #if (VA_MAJOR_VERSION < 1)
2009                     if (flag & VA_SOURCE_RANGE_FULL)
2010  #else
2011                     if (color_range == VA_SOURCE_RANGE_FULL)
2012  #endif
2013                     {
2014                         pVpHalSurf->ColorSpace = CSpace_BT2020_FullRange;
2015                     }
2016                     else
2017                     {
2018                         pVpHalSurf->ColorSpace = CSpace_BT2020;
2019                     }
2020                     break;
2021                 case VAProcColorStandardBT470M:
2022                 case VAProcColorStandardBT470BG:
2023                 case VAProcColorStandardSMPTE170M:
2024                 case VAProcColorStandardSMPTE240M:
2025                 case VAProcColorStandardGenericFilm:
2026                 case VAProcColorStandardXVYCC601:
2027                 case VAProcColorStandardXVYCC709:
2028                     pVpHalSurf->ColorSpace == CSpace_None;
2029                     break;
2030                 case VAProcColorStandardExplicit:
2031                     VpSetColorStandardExplictly(pVpHalSurf, colorStandard, colorProperties);
2032                     break;
2033                 default:
2034                     pVpHalSurf->ColorSpace == CSpace_BT601;
2035                     break;
2036             }
2037         }
2038     }
2039     DDI_CHK_CONDITION((pVpHalSurf->ColorSpace == CSpace_None), "Invalid color standard", VA_STATUS_ERROR_INVALID_PARAMETER);
2040 
2041     return VA_STATUS_SUCCESS;
2042 }
2043 
2044 
2045 //////////////////////////////////////////////////////////////////////////////////////////////
2046 //! \purpose Get ColorSpace from the media format
2047 //! \params
2048 //! [in]  format : media format
2049 //! [out] None
2050 //! \returns appropriate VPHAL_CSPACE if call succeeds, CSpace_None otherwise
2051 /////////////////////////////////////////////////////////////////////////////////////////////
DdiVp_GetColorSpaceFromMediaFormat(DDI_MEDIA_FORMAT format)2052 VPHAL_CSPACE DdiVp_GetColorSpaceFromMediaFormat(DDI_MEDIA_FORMAT format)
2053 {
2054     MOS_FORMAT mosFormat = VpGetFormatFromMediaFormat(format);
2055 
2056     if (IS_RGB_FORMAT(mosFormat))
2057     {
2058         return CSpace_sRGB;
2059     }
2060     else
2061     {
2062         return CSpace_BT601;
2063     }
2064 }
2065 
2066 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2067 //! \purpose Extract Render Target params from VAProcPipelineParameterBuffer and set the appropriate VPHAL params for RT
2068 //! \params
2069 //! [in]  pVaDrvCtx : VA Driver context
2070 //! [in]  pVpCtx : VP context
2071 //! [in]  pPipelineParam : VAProcPipelineParameterBuffer Pipeline paramseter from application
2072 //! [out] None
2073 //! \returns VA_STATUS_SUCCESS if call succeeds
2074 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2075 VAStatus
DdiVp_UpdateVphalTargetSurfColorSpace(VADriverContextP pVaDrvCtx,PDDI_VP_CONTEXT pVpCtx,VAProcPipelineParameterBuffer * pPipelineParam,uint32_t targetIndex)2076 DdiVp_UpdateVphalTargetSurfColorSpace(
2077     VADriverContextP                pVaDrvCtx,
2078     PDDI_VP_CONTEXT                 pVpCtx,
2079     VAProcPipelineParameterBuffer*  pPipelineParam,
2080     uint32_t                        targetIndex)
2081 {
2082     PVPHAL_RENDER_PARAMS      pVpHalRenderParams;
2083     PVPHAL_SURFACE            pVpHalSrcSurf;
2084     PVPHAL_SURFACE            pVpHalTgtSurf;
2085     VAStatus                  vaStatus;
2086     DDI_UNUSED(pVaDrvCtx);
2087 
2088     VP_DDI_FUNCTION_ENTER;
2089     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
2090 
2091     // initialize
2092     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2093     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
2094     pVpHalTgtSurf      = pVpHalRenderParams->pTarget[targetIndex];
2095     DDI_CHK_NULL(pVpHalTgtSurf, "Null pVpHalTgtSurf.", VA_STATUS_ERROR_INVALID_SURFACE);
2096 
2097     // update target surface color space
2098 #if (VA_MAJOR_VERSION < 1)
2099     vaStatus = DdiVp_GetColorSpace(pVpHalTgtSurf, pPipelineParam->output_color_standard, pPipelineParam->output_surface_flag);
2100 #else
2101     vaStatus = DdiVp_GetColorSpace(pVpHalTgtSurf, pPipelineParam->output_color_standard, pPipelineParam->output_color_properties);
2102 #endif
2103 
2104     pVpHalSrcSurf = pVpHalRenderParams->pSrc[0];
2105     // Not support BT601/BT709 -> BT2020 colorspace conversion, if colorspace is not set, will keep it same with input.
2106     if(pVpHalSrcSurf != nullptr &&
2107        pPipelineParam->output_color_standard == 0 &&
2108        IS_COLOR_SPACE_BT2020(pVpHalTgtSurf->ColorSpace) &&
2109        !IS_COLOR_SPACE_BT2020(pVpHalSrcSurf->ColorSpace))
2110     {
2111         pVpHalTgtSurf->ColorSpace = pVpHalSrcSurf->ColorSpace;
2112     }
2113 
2114     // extended gamut?
2115     pVpHalRenderParams->pTarget[0]->ExtendedGamut = false;
2116 
2117     return vaStatus;
2118 }
2119 
2120 //////////////////////////////////////////////////////////////////////////////////////////////
2121 //! \purpose Setup the appropriate filter params for VPHAL input surface based on Filter type
2122 //! \params
2123 //! [in]  pVaDrvCtx : Driver context
2124 //! [in]  pVpCtx : VP context
2125 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2126 //! [in]  FilterType : Filter type
2127 //! [in]  pData : Buffer data
2128 //! [in]  uElementNum : number of elements in the buffer(FilterParameter)
2129 //! [in]  vpStateFlags : filter enable status
2130 //! [out] None
2131 //! \returns VA_STATUS_SUCCESS if call succeeds
2132 //////////////////////////////////////////////////////////////////////////////////////////////
2133 VAStatus
DdiVp_UpdateFilterParamBuffer(VADriverContextP pVaDrvCtx,PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,int32_t FilterType,void * pData,uint32_t uElementNum,DDI_VP_STATE * vpStateFlags)2134 DdiVp_UpdateFilterParamBuffer(
2135         VADriverContextP    pVaDrvCtx,
2136         PDDI_VP_CONTEXT     pVpCtx,
2137         uint32_t            uSurfIndex,
2138         int32_t             FilterType,
2139         void                *pData,
2140         uint32_t            uElementNum,
2141         DDI_VP_STATE*       vpStateFlags)
2142 {
2143     VAStatus vaStatus;
2144 
2145     VP_DDI_FUNCTION_ENTER;
2146     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
2147 
2148     vaStatus = VA_STATUS_SUCCESS;
2149 
2150     switch (FilterType){
2151         case VAProcFilterDeinterlacing:
2152             vpStateFlags->bDeinterlaceEnable = true;
2153             vaStatus = DdiVp_SetProcFilterDinterlaceParams(
2154                                             pVpCtx,
2155                                             uSurfIndex,
2156                                             (VAProcFilterParameterBufferDeinterlacing*) pData);
2157             break;
2158         case VAProcFilterNoiseReduction:
2159             vpStateFlags->bDenoiseEnable = true;
2160             vaStatus = DdiVp_SetProcFilterDenoiseParams(
2161                                             pVpCtx,
2162                                             uSurfIndex,
2163                                             (VAProcFilterParameterBuffer*) pData);
2164             break;
2165         case VAProcFilterHVSNoiseReduction:
2166             vpStateFlags->bDenoiseEnable = true;
2167             vaStatus = DdiVp_SetProcFilterHVSDenoiseParams(
2168                                             pVpCtx,
2169                                             uSurfIndex,
2170                                             (VAProcFilterParameterBufferHVSNoiseReduction*) pData);
2171             break;
2172         case VAProcFilterSharpening:
2173             vpStateFlags->bIEFEnable = true;
2174             vaStatus = DdiVp_SetProcFilterSharpnessParams(
2175                                             pVpCtx,
2176                                             uSurfIndex,
2177                                             (VAProcFilterParameterBuffer*) pData);
2178             break;
2179         case VAProcFilterColorBalance:
2180             vpStateFlags->bProcampEnable = true;
2181             vaStatus = DdiVp_SetProcFilterColorBalanceParams(
2182                                             pVpCtx,
2183                                             uSurfIndex,
2184                                             (VAProcFilterParameterBufferColorBalance*) pData,
2185                                             uElementNum);
2186             break;
2187         case VAProcFilterSkinToneEnhancement:
2188             vaStatus = DdiVp_SetProcFilterSkinToneEnhancementParams(
2189                                             pVpCtx,
2190                                             uSurfIndex,
2191                                             (VAProcFilterParameterBuffer*) pData);
2192             break;
2193         case VAProcFilterTotalColorCorrection:
2194             vaStatus = DdiVp_SetProcFilterTotalColorCorrectionParams(
2195                                             pVpCtx,
2196                                             uSurfIndex,
2197                                             (VAProcFilterParameterBufferTotalColorCorrection*) pData,
2198                                             uElementNum);
2199             break;
2200         case VAProcFilterHighDynamicRangeToneMapping:
2201             vaStatus = DdiVp_SetProcFilterHdrTmParams(
2202                                             pVpCtx,
2203                                             uSurfIndex,
2204                                             (VAProcFilterParameterBufferHDRToneMapping*) pData);
2205             break;
2206 #if VA_CHECK_VERSION(1, 12, 0)
2207         case VAProcFilter3DLUT:
2208             vaStatus = DdiVp_SetProcFilter3DLutParams(
2209                                             pVaDrvCtx,
2210                                             pVpCtx,
2211                                             uSurfIndex,
2212                                             (VAProcFilterParameterBuffer3DLUT*) pData);
2213             break;
2214 #endif
2215         case VAProcFilterNone:
2216             vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
2217             break;
2218         default:
2219             VP_DDI_ASSERTMESSAGE("VAProcFilterType is unknown.");
2220             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_FILTER;
2221             break;
2222     }// switch (type)
2223 
2224     return vaStatus;
2225 }
2226 
2227 //////////////////////////////////////////////////////////////////////////////////////////////
2228 //! \purpose clear filter params which is disabled for VPHAL input surface
2229 //! \params
2230 //! [in]  pVpCtx : VP context
2231 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2232 //! [in]  vpStateFlags : filter enable status
2233 //! [out] None
2234 //! \returns VA_STATUS_SUCCESS if call succeeds
2235 //////////////////////////////////////////////////////////////////////////////////////////////
DdiVp_ClearFilterParamBuffer(PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,DDI_VP_STATE vpStateFlags)2236 VAStatus DdiVp_ClearFilterParamBuffer(
2237          PDDI_VP_CONTEXT     pVpCtx,
2238          uint32_t            uSurfIndex,
2239          DDI_VP_STATE        vpStateFlags)
2240 {
2241      if(!vpStateFlags.bProcampEnable)
2242      {
2243          MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pProcampParams);
2244      }
2245      if(!vpStateFlags.bDeinterlaceEnable)
2246      {
2247          MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pDeinterlaceParams);
2248      }
2249      if(!vpStateFlags.bDenoiseEnable)
2250      {
2251          MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pDenoiseParams);
2252      }
2253      if(!vpStateFlags.bIEFEnable)
2254      {
2255          if (pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pIEFParams)
2256          {
2257             MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pIEFParams->pExtParam);
2258             MOS_FreeMemAndSetNull(pVpCtx->pVpHalRenderParams->pSrc[uSurfIndex]->pIEFParams);
2259          }
2260      }
2261 
2262      return VA_STATUS_SUCCESS;
2263 }
2264 
2265 ////////////////////////////////////////////////////////////////////////////////
2266 //! \purpose Set DI filter params for input VPHAL surface
2267 //! \params
2268 //! [in]  pVpCtx : VP context
2269 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2270 //! [in]  pDiParamBuff : Pointer to DI param buffer data
2271 //! [out] None
2272 //! \returns VA_STATUS_SUCCESS if call succeeds
2273 ////////////////////////////////////////////////////////////////////////////////
2274 VAStatus
DdiVp_SetProcFilterDinterlaceParams(PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,VAProcFilterParameterBufferDeinterlacing * pDiParamBuff)2275 DdiVp_SetProcFilterDinterlaceParams(
2276     PDDI_VP_CONTEXT                             pVpCtx,
2277     uint32_t                                    uSurfIndex,
2278     VAProcFilterParameterBufferDeinterlacing*   pDiParamBuff)
2279 {
2280     PVPHAL_RENDER_PARAMS      pVpHalRenderParams;
2281     PVPHAL_SURFACE            pTarget;
2282     PVPHAL_SURFACE            pSrc;
2283     VPHAL_DI_MODE             DIMode;
2284 
2285     VP_DDI_FUNCTION_ENTER;
2286     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
2287     DDI_CHK_NULL(pDiParamBuff,
2288                     "Null pDiParamBuff.",
2289                     VA_STATUS_ERROR_INVALID_BUFFER);
2290 
2291     // initialize
2292     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2293     DDI_CHK_NULL(pVpHalRenderParams,
2294                     "Null pVpHalRenderParams.",
2295                     VA_STATUS_ERROR_INVALID_PARAMETER);
2296 
2297     pSrc    = pVpHalRenderParams->pSrc[uSurfIndex];
2298     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
2299     pTarget = pVpHalRenderParams->pTarget[0];
2300     DDI_CHK_NULL(pTarget, "Null pTarget.", VA_STATUS_ERROR_INVALID_SURFACE);
2301 
2302     switch (pDiParamBuff->algorithm)
2303     {
2304         case VAProcDeinterlacingBob:
2305             DIMode = DI_MODE_BOB;
2306             break;
2307         case VAProcDeinterlacingMotionAdaptive:
2308         case VAProcDeinterlacingMotionCompensated:
2309             DIMode = DI_MODE_ADI;
2310             break;
2311         case VAProcDeinterlacingWeave:
2312             pSrc->bFieldWeaving = true;;
2313             return VA_STATUS_SUCCESS;
2314         case VAProcDeinterlacingNone:
2315             return VA_STATUS_SUCCESS;
2316         default:
2317             VP_DDI_ASSERTMESSAGE("Deinterlacing type is unsupported.");
2318             return VA_STATUS_ERROR_UNIMPLEMENTED;
2319     }// switch (pDiParamBuff->algorithm)
2320 
2321     if (nullptr == pSrc->pDeinterlaceParams)
2322     {
2323         pSrc->pDeinterlaceParams    = (PVPHAL_DI_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_DI_PARAMS));
2324         DDI_CHK_NULL(pSrc->pDeinterlaceParams, "pSrc->pDeinterlaceParams is NULL", VA_STATUS_ERROR_ALLOCATION_FAILED);
2325     }
2326 
2327     if (nullptr == pTarget->pDeinterlaceParams)
2328     {
2329         pTarget->pDeinterlaceParams = (PVPHAL_DI_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_DI_PARAMS));
2330         DDI_CHK_NULL(pTarget->pDeinterlaceParams, "pTarget->pDeinterlaceParams is NULL", VA_STATUS_ERROR_ALLOCATION_FAILED);
2331     }
2332     //application detect scene change and then pass parameter to driver.
2333     if (pDiParamBuff->flags & VA_DEINTERLACING_SCD_ENABLE)
2334     {
2335         DIMode = DI_MODE_BOB;
2336         pSrc->pDeinterlaceParams->bSCDEnable = true;
2337     }
2338     else
2339     {
2340         pSrc->pDeinterlaceParams->bSCDEnable = false;
2341     }
2342 
2343     pSrc->pDeinterlaceParams->DIMode = DIMode;
2344 
2345     pSrc->pDeinterlaceParams->bSingleField = (pDiParamBuff->flags & VA_DEINTERLACING_ONE_FIELD) ? true : false;
2346 
2347     pSrc->pDeinterlaceParams->bEnableFMD   = (pDiParamBuff->flags & VA_DEINTERLACING_FMD_ENABLE) ? true : false;
2348 
2349     //update sample type
2350     if (pDiParamBuff->flags & VA_DEINTERLACING_BOTTOM_FIELD_FIRST)
2351     {
2352         if (pDiParamBuff->flags & VA_DEINTERLACING_BOTTOM_FIELD)
2353         {
2354             pSrc->SampleType = SAMPLE_INTERLEAVED_ODD_FIRST_BOTTOM_FIELD;
2355         }
2356         else
2357         {
2358             pSrc->SampleType = SAMPLE_INTERLEAVED_ODD_FIRST_TOP_FIELD;
2359         }
2360     }
2361     else
2362     {
2363         if (pDiParamBuff->flags & VA_DEINTERLACING_BOTTOM_FIELD)
2364         {
2365             pSrc->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_BOTTOM_FIELD;
2366         }
2367         else
2368         {
2369             pSrc->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_TOP_FIELD;
2370         }
2371     }
2372 
2373     if (pSrc->pDeinterlaceParams->DIMode == DI_MODE_ADI)
2374     {
2375         //When pBwdRef is not nullptr and uBwdRefCount is nonzero, ADI can use Bwd Ref frame.
2376         //Otherwise, ADI shouldn't use Bwd Ref frame.
2377         if (pSrc->uBwdRefCount && pSrc->pBwdRef != nullptr)
2378         {
2379             pSrc->uBwdRefCount           = 1;
2380 
2381             //When the bo of the current frame's SRC and reference are same with previous frame's,
2382             //we should set the frame ID same as previous frame's setting.
2383             if ( pVpCtx->FrameIDTracer.pLastSrcSurfBo == pSrc->OsResource.bo &&
2384                  pVpCtx->FrameIDTracer.pLastBwdSurfBo == pSrc->pBwdRef->OsResource.bo &&
2385                  pVpCtx->FrameIDTracer.uiLastSampleType != pSrc->SampleType)
2386             {
2387                 pSrc->FrameID          = pVpCtx->FrameIDTracer.uiLastSrcSurfFrameID;
2388                 pSrc->pBwdRef->FrameID = pVpCtx->FrameIDTracer.uiLastBwdSurfFrameID;
2389             }
2390             //Otherwise, we should update the values of frame ID and FrameID tracer.
2391             else
2392             {
2393                 pSrc->pBwdRef->FrameID       = (VP_SETTING_SAME_SAMPLE_THRESHOLD + 1) * pVpCtx->FrameIDTracer.uiFrameIndex;
2394                 pSrc->FrameID                = pSrc->pBwdRef->FrameID + ( VP_SETTING_SAME_SAMPLE_THRESHOLD + 1);
2395 
2396                 pVpCtx->FrameIDTracer.pLastSrcSurfBo = pSrc->OsResource.bo;
2397                 pVpCtx->FrameIDTracer.pLastBwdSurfBo = pSrc->pBwdRef->OsResource.bo;
2398 
2399                 pVpCtx->FrameIDTracer.uiLastSrcSurfFrameID = pSrc->FrameID;
2400                 pVpCtx->FrameIDTracer.uiLastBwdSurfFrameID = pSrc->pBwdRef->FrameID;
2401 
2402                 pVpCtx->FrameIDTracer.uiLastSampleType = pSrc->SampleType;
2403             }
2404         }
2405         else
2406         {
2407             //ADI no reference frame driver only care EVEN/ODD
2408             if (pDiParamBuff->flags & VA_DEINTERLACING_BOTTOM_FIELD)
2409             {
2410                 pSrc->SampleType = SAMPLE_INTERLEAVED_ODD_FIRST_BOTTOM_FIELD;
2411             }
2412             else
2413             {
2414                 pSrc->SampleType = SAMPLE_INTERLEAVED_EVEN_FIRST_TOP_FIELD;
2415             }
2416         }
2417 
2418         //int32_t overflow process
2419         pVpCtx->FrameIDTracer.uiFrameIndex = (pVpCtx->FrameIDTracer.uiFrameIndex + 1 == INT_MAX) ? 1 : pVpCtx->FrameIDTracer.uiFrameIndex + 1;
2420     }
2421 
2422     return VA_STATUS_SUCCESS;
2423 }
2424 
2425 ////////////////////////////////////////////////////////////////////////////////
2426 //! \purpose Set DN filter params for VPHAL input surface
2427 //! \params
2428 //! [in]  pVpCtx : VP context
2429 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2430 //! [in]  pDnParamBuff : Pointer to DN param buffer data
2431 //! [out] None
2432 //! \returns VA_STATUS_SUCCESS if call succeeds
2433 ////////////////////////////////////////////////////////////////////////////////
2434 VAStatus
DdiVp_SetProcFilterDenoiseParams(PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,VAProcFilterParameterBuffer * pDnParamBuff)2435 DdiVp_SetProcFilterDenoiseParams(
2436     PDDI_VP_CONTEXT                 pVpCtx,
2437     uint32_t                        uSurfIndex,
2438     VAProcFilterParameterBuffer*    pDnParamBuff)
2439 {
2440     PVPHAL_RENDER_PARAMS      pVpHalRenderParams;
2441     PVPHAL_SURFACE            pSrc;
2442 
2443     VP_DDI_FUNCTION_ENTER;
2444     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
2445 
2446     // initialize
2447     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2448     DDI_CHK_NULL(pVpHalRenderParams,
2449                     "Null pVpHalRenderParams.",
2450                      VA_STATUS_ERROR_INVALID_PARAMETER);
2451     pSrc = pVpHalRenderParams->pSrc[uSurfIndex];
2452     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
2453 
2454     if (nullptr == pSrc->pDenoiseParams)
2455     {
2456         pSrc->pDenoiseParams = (PVPHAL_DENOISE_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_DENOISE_PARAMS));
2457     }
2458     DDI_CHK_NULL(pSrc->pDenoiseParams, "pDenoiseParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2459 
2460     // denoise caps range is from 0 to 64, out of range parameter is treated as an error
2461     if (pDnParamBuff->value < NOISEREDUCTION_MIN || pDnParamBuff->value > NOISEREDUCTION_MAX)
2462     {
2463         return VA_STATUS_ERROR_INVALID_PARAMETER;
2464     }
2465 
2466     pSrc->pDenoiseParams->fDenoiseFactor = pDnParamBuff->value;
2467 
2468     // Luma and chroma denoise should be always enabled when noise reduction is needed
2469     pSrc->pDenoiseParams->bEnableLuma    = true;
2470     pSrc->pDenoiseParams->bEnableChroma  = true;
2471     pSrc->pDenoiseParams->bAutoDetect    = false;
2472     pSrc->pDenoiseParams->NoiseLevel     = NOISELEVEL_DEFAULT;
2473 
2474     return VA_STATUS_SUCCESS;
2475 }
2476 
2477 ////////////////////////////////////////////////////////////////////////////////
2478 //! \purpose Set DN filter params for VPHAL input surface
2479 //! \params
2480 //! [in]  pVpCtx : VP context
2481 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2482 //! [in]  pDnParamBuff : Pointer to DN param buffer data
2483 //! [out] None
2484 //! \returns VA_STATUS_SUCCESS if call succeeds
2485 ////////////////////////////////////////////////////////////////////////////////
2486 VAStatus
DdiVp_SetProcFilterHVSDenoiseParams(PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,VAProcFilterParameterBufferHVSNoiseReduction * pHVSDnParamBuff)2487 DdiVp_SetProcFilterHVSDenoiseParams(
2488     PDDI_VP_CONTEXT                                  pVpCtx,
2489     uint32_t                                         uSurfIndex,
2490     VAProcFilterParameterBufferHVSNoiseReduction*    pHVSDnParamBuff)
2491 {
2492     PVPHAL_RENDER_PARAMS      pVpHalRenderParams = nullptr;
2493     PVPHAL_SURFACE            pSrc = nullptr;
2494 
2495     VP_DDI_FUNCTION_ENTER;
2496     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
2497     DDI_CHK_NULL(pHVSDnParamBuff,
2498                 "Null pHVSDnParamBuff.",
2499                      VA_STATUS_ERROR_INVALID_PARAMETER);
2500 
2501     // initialize
2502     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2503     DDI_CHK_NULL(pVpHalRenderParams,
2504                     "Null pVpHalRenderParams.",
2505                      VA_STATUS_ERROR_INVALID_PARAMETER);
2506     pSrc = pVpHalRenderParams->pSrc[uSurfIndex];
2507     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
2508 
2509     if (nullptr == pSrc->pDenoiseParams)
2510     {
2511         pSrc->pDenoiseParams = (PVPHAL_DENOISE_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_DENOISE_PARAMS));
2512     }
2513     DDI_CHK_NULL(pSrc->pDenoiseParams, "pDenoiseParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2514 
2515     // Luma and chroma denoise should be always enabled when noise reduction is needed
2516     pSrc->pDenoiseParams->bEnableLuma       = true;
2517     pSrc->pDenoiseParams->bEnableChroma     = true;
2518     pSrc->pDenoiseParams->bEnableHVSDenoise = true;
2519 
2520     switch (pHVSDnParamBuff->mode)
2521     {
2522         case VA_PROC_HVS_DENOISE_AUTO_SUBJECTIVE:
2523             pSrc->pDenoiseParams->HVSDenoise.Mode = HVSDENOISE_AUTO_SUBJECTIVE;
2524             pSrc->pDenoiseParams->bAutoDetect     = true;
2525             break;
2526         case VA_PROC_HVS_DENOISE_MANUAL:
2527             pSrc->pDenoiseParams->HVSDenoise.Mode = HVSDENOISE_MANUAL;
2528             break;
2529         case VA_PROC_HVS_DENOISE_DEFAULT:
2530         case VA_PROC_HVS_DENOISE_AUTO_BDRATE:
2531         default:
2532             pSrc->pDenoiseParams->HVSDenoise.Mode = HVSDENOISE_AUTO_BDRATE;
2533             pSrc->pDenoiseParams->bAutoDetect     = true;
2534     }// switch (pHVSDnParamBuff->mode)
2535 
2536     if (pSrc->pDenoiseParams->HVSDenoise.Mode == HVSDENOISE_AUTO_BDRATE)
2537     {
2538         pSrc->pDenoiseParams->HVSDenoise.QP       = pHVSDnParamBuff->qp;
2539         if (pSrc->pDenoiseParams->HVSDenoise.QP == 0)
2540         {
2541             //If didn't set value, HVS Auto Bdrate Mode default qp 27
2542             pSrc->pDenoiseParams->HVSDenoise.QP   = 27;
2543         }
2544     }
2545     else if (pSrc->pDenoiseParams->HVSDenoise.Mode == HVSDENOISE_AUTO_SUBJECTIVE)
2546     {
2547         //HVS Subjective Mode default qp 32
2548         pSrc->pDenoiseParams->HVSDenoise.QP       = 32;
2549     }
2550     else
2551     {
2552         pSrc->pDenoiseParams->HVSDenoise.QP       = 32;
2553         pSrc->pDenoiseParams->HVSDenoise.Strength = pHVSDnParamBuff->strength;
2554     }
2555 
2556     VP_DDI_NORMALMESSAGE("HVS Denoise is enabled with qp %d, strength %d, mode %d!", pSrc->pDenoiseParams->HVSDenoise.QP, pSrc->pDenoiseParams->HVSDenoise.Strength, pSrc->pDenoiseParams->HVSDenoise.Mode);
2557 
2558     return VA_STATUS_SUCCESS;
2559 }
2560 /////////////////////////////////////////////////////////////////////////////////////////////////
2561 //! \purpose Set Sharpness (Image Enhancement Filter, IEF) filter params for VPHAL input surface
2562 //! \params
2563 //! [in]  pVpCtx : VP context
2564 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2565 //! [in]  pDnParamBuff : Pointer to Sharpness param buffer data
2566 //! [out] None
2567 //! \returns VA_STATUS_SUCCESS if call succeeds
2568 //////////////////////////////////////////////////////////////////////////////////////////////////
2569 VAStatus
DdiVp_SetProcFilterSharpnessParams(PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,VAProcFilterParameterBuffer * pSharpParamBuff)2570 DdiVp_SetProcFilterSharpnessParams(
2571     PDDI_VP_CONTEXT                 pVpCtx,
2572     uint32_t                        uSurfIndex,
2573     VAProcFilterParameterBuffer*    pSharpParamBuff)
2574 {
2575     PVPHAL_RENDER_PARAMS      pVpHalRenderParams;
2576     PVPHAL_SURFACE            pSrc;
2577 
2578     VP_DDI_FUNCTION_ENTER;
2579     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
2580     DDI_CHK_NULL(pSharpParamBuff, "Null pSharpParamBuff.", VA_STATUS_ERROR_INVALID_BUFFER);
2581 
2582     // initialize
2583     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2584     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
2585     pSrc = pVpHalRenderParams->pSrc[uSurfIndex];
2586     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
2587 
2588     if (nullptr == pSrc->pIEFParams)
2589     {
2590         pSrc->pIEFParams    = (PVPHAL_IEF_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_IEF_PARAMS));
2591         DDI_CHK_NULL(pSrc->pIEFParams, "pIEFParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2592     }
2593 
2594     // out of range parameter is treated as an error
2595     if (pSharpParamBuff->value < EDGEENHANCEMENT_MIN || pSharpParamBuff->value > EDGEENHANCEMENT_MAX)
2596     {
2597         return VA_STATUS_ERROR_INVALID_PARAMETER;
2598     }
2599 
2600     // sharpness option
2601     // setting with hard code in mplayer side.
2602     // refer to sharpening section of render_picture_vp() defined in vaapi.c.
2603     // change flag and factor for testing.
2604     VpHal_DdiInitIEFParams(pSrc->pIEFParams);
2605     pSrc->bIEF                      = true;
2606     pSrc->pIEFParams->bEnabled      = true;
2607     pSrc->pIEFParams->fIEFFactor    = pSharpParamBuff->value;
2608 
2609     return VA_STATUS_SUCCESS;
2610 }
2611 
2612 ////////////////////////////////////////////////////////////////////////////////
2613 //! \purpose Set Color Balance (procamp) filter params for VPHAL input surface
2614 //! \params
2615 //! [in]  pVpCtx : VP context
2616 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2617 //! [in]  pColorBalanceParamBuff : Pointer to Colorbalance param buffer data
2618 //! [in]  uElementNum : number of elements in the Colorbalance param buffer data
2619 //! [out] None
2620 //! \returns VA_STATUS_SUCCESS if call succeeds
2621 ////////////////////////////////////////////////////////////////////////////////
2622 VAStatus
DdiVp_SetProcFilterColorBalanceParams(PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,VAProcFilterParameterBufferColorBalance * pColorBalanceParamBuff,uint32_t uElementNum)2623 DdiVp_SetProcFilterColorBalanceParams(
2624     PDDI_VP_CONTEXT                             pVpCtx,
2625     uint32_t                                    uSurfIndex,
2626     VAProcFilterParameterBufferColorBalance*    pColorBalanceParamBuff,
2627     uint32_t                                    uElementNum)
2628 {
2629     PVPHAL_RENDER_PARAMS      pVpHalRenderParams;
2630     PVPHAL_SURFACE            pSrc;
2631     uint32_t                  i;
2632     bool                      bProcamp;
2633 
2634     VP_DDI_FUNCTION_ENTER;
2635     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
2636     DDI_CHK_NULL(pColorBalanceParamBuff,
2637                     "Null pColorBalanceParamBuff.",
2638                     VA_STATUS_ERROR_INVALID_BUFFER);
2639 
2640     // initialize
2641     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2642     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
2643     pSrc = pVpHalRenderParams->pSrc[uSurfIndex];
2644     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
2645 
2646     bProcamp = false;
2647     for (i = 0; i < uElementNum; i++)
2648     {
2649         if ((VAProcColorBalanceHue        == pColorBalanceParamBuff[i].attrib) ||
2650             (VAProcColorBalanceSaturation == pColorBalanceParamBuff[i].attrib) ||
2651             (VAProcColorBalanceBrightness == pColorBalanceParamBuff[i].attrib) ||
2652             (VAProcColorBalanceContrast   == pColorBalanceParamBuff[i].attrib))
2653         {
2654             bProcamp = true;
2655             break;
2656         }
2657     }
2658 
2659     if (nullptr == pSrc->pProcampParams &&
2660         true == bProcamp)
2661     {
2662         pSrc->pProcampParams  = (PVPHAL_PROCAMP_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_PROCAMP_PARAMS));
2663         DDI_CHK_NULL(pSrc->pProcampParams, "Source pProcampParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2664     }
2665 
2666     if (nullptr == pVpHalRenderParams->pTarget[0]->pProcampParams)
2667     {
2668         pVpHalRenderParams->pTarget[0]->pProcampParams = (PVPHAL_PROCAMP_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_PROCAMP_PARAMS));
2669         DDI_CHK_NULL(pVpHalRenderParams->pTarget[0]->pProcampParams, "Target pProcampParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2670     }
2671 
2672     // Needed for ACE
2673     if (nullptr         == pSrc->pColorPipeParams &&
2674         SURF_IN_PRIMARY == pSrc->SurfType)
2675     {
2676         pSrc->pColorPipeParams = (PVPHAL_COLORPIPE_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_COLORPIPE_PARAMS));
2677         DDI_CHK_NULL(pSrc->pColorPipeParams, "pColorPipeParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2678     }
2679 
2680     // set default value
2681     if (nullptr != pSrc->pProcampParams)
2682     {
2683         pSrc->pProcampParams->fHue          = PROCAMP_HUE_DEFAULT;
2684         pSrc->pProcampParams->fSaturation   = PROCAMP_SATURATION_DEFAULT;
2685         pSrc->pProcampParams->fBrightness   = PROCAMP_BRIGHTNESS_DEFAULT;
2686         pSrc->pProcampParams->fContrast     = PROCAMP_CONTRAST_DEFAULT;
2687     }
2688 
2689     for (i = 0; i < uElementNum; i++)
2690     {
2691         switch (pColorBalanceParamBuff[i].attrib)
2692         {
2693         case VAProcColorBalanceHue:
2694             if (pColorBalanceParamBuff[i].value < PROCAMP_HUE_MIN || pColorBalanceParamBuff[i].value > PROCAMP_HUE_MAX)
2695             {
2696                 VP_DDI_ASSERTMESSAGE("%d: Hue is out of bounds.", i);
2697                 return VA_STATUS_ERROR_INVALID_PARAMETER;
2698             }
2699             if (true == bProcamp)
2700             {
2701                 pSrc->pProcampParams->bEnabled     = true;
2702                 pSrc->pProcampParams->fHue         = pColorBalanceParamBuff[i].value;
2703             }
2704             break;
2705         case VAProcColorBalanceSaturation:
2706             if (pColorBalanceParamBuff[i].value < PROCAMP_SATURATION_MIN || pColorBalanceParamBuff[i].value > PROCAMP_SATURATION_MAX)
2707             {
2708                 VP_DDI_ASSERTMESSAGE("%d: Saturation is out of bounds.", i);
2709                 return VA_STATUS_ERROR_INVALID_PARAMETER;
2710             }
2711             if (true == bProcamp)
2712             {
2713                pSrc->pProcampParams->bEnabled     = true;
2714                pSrc->pProcampParams->fSaturation  = pColorBalanceParamBuff[i].value;
2715             }
2716             break;
2717         case VAProcColorBalanceBrightness:
2718             if (pColorBalanceParamBuff[i].value < PROCAMP_BRIGHTNESS_MIN || pColorBalanceParamBuff[i].value > PROCAMP_BRIGHTNESS_MAX)
2719             {
2720                 VP_DDI_ASSERTMESSAGE("%d: Brightness is out of bounds.", i);
2721                 return VA_STATUS_ERROR_INVALID_PARAMETER;
2722             }
2723             if (true == bProcamp)
2724             {
2725                 pSrc->pProcampParams->bEnabled     = true;
2726                 pSrc->pProcampParams->fBrightness  = pColorBalanceParamBuff[i].value;
2727             }
2728             break;
2729         case VAProcColorBalanceContrast:
2730             if (pColorBalanceParamBuff[i].value < PROCAMP_CONTRAST_MIN || pColorBalanceParamBuff[i].value > PROCAMP_CONTRAST_MAX)
2731             {
2732                 VP_DDI_ASSERTMESSAGE("%d: Contrast is out of bounds.", i);
2733                 return VA_STATUS_ERROR_INVALID_PARAMETER;
2734             }
2735             if (true == bProcamp)
2736             {
2737                 pSrc->pProcampParams->bEnabled     = true;
2738                 pSrc->pProcampParams->fContrast    = pColorBalanceParamBuff[i].value;
2739             }
2740             break;
2741         case VAProcColorBalanceAutoContrast:
2742             if (SURF_IN_PRIMARY == pSrc->SurfType)
2743             {
2744                 pSrc->pColorPipeParams->bEnableACE    = true;
2745                 pSrc->pColorPipeParams->dwAceLevel    = ACE_LEVEL_DEFAULT;
2746                 pSrc->pColorPipeParams->dwAceStrength = ACE_STRENGTH_DEFAULT;
2747             }
2748             break;
2749         case VAProcColorBalanceAutoSaturation:
2750         case VAProcColorBalanceAutoBrightness:
2751             return VA_STATUS_ERROR_UNIMPLEMENTED;
2752         case VAProcColorBalanceNone:
2753         case VAProcColorBalanceCount:
2754         default:
2755             VP_DDI_ASSERTMESSAGE("pColorBalanceParamBuff[%d].attrib is unknown.", i);
2756             return VA_STATUS_ERROR_INVALID_PARAMETER;
2757         }// switch (attrib)
2758     }
2759 
2760     return VA_STATUS_SUCCESS;
2761 }
2762 
2763 /////////////////////////////////////////////////////////////////////////////////////////////////
2764 //! \purpose Set Skin Tone Enhancement (STE) filter params for VPHAL input surface
2765 //! \params
2766 //! [in]  pVpCtx : VP context
2767 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2768 //! [in]  pSTEParamBuff : Pointer to Skin Tone Enhancement param buffer data
2769 //! [out] None
2770 //! \returns VA_STATUS_SUCCESS if call succeeds
2771 //////////////////////////////////////////////////////////////////////////////////////////////////
2772 VAStatus
DdiVp_SetProcFilterSkinToneEnhancementParams(PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,VAProcFilterParameterBuffer * pSTEParamBuff)2773 DdiVp_SetProcFilterSkinToneEnhancementParams(
2774     PDDI_VP_CONTEXT              pVpCtx,
2775     uint32_t                     uSurfIndex,
2776     VAProcFilterParameterBuffer* pSTEParamBuff)
2777 {
2778     PVPHAL_RENDER_PARAMS pVpHalRenderParams;
2779     PVPHAL_SURFACE       pSrc;
2780 
2781     VP_DDI_FUNCTION_ENTER;
2782     DDI_CHK_NULL(pSTEParamBuff, "Null pSTEParamBuff.", VA_STATUS_ERROR_INVALID_BUFFER);
2783 
2784     // initialize
2785     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2786     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
2787     pSrc = pVpHalRenderParams->pSrc[uSurfIndex];
2788     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
2789 
2790     if (SURF_IN_PRIMARY == pSrc->SurfType)
2791     {
2792         if (nullptr == pSrc->pColorPipeParams)
2793         {
2794             pSrc->pColorPipeParams = (PVPHAL_COLORPIPE_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_COLORPIPE_PARAMS));
2795             DDI_CHK_NULL(pSrc->pColorPipeParams, "pColorPipeParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2796         }
2797 
2798         // out of range parameter is treated as an error
2799         if (pSTEParamBuff->value < STE_MIN || pSTEParamBuff->value > STE_MAX)
2800         {
2801             return VA_STATUS_ERROR_INVALID_PARAMETER;
2802         }
2803 
2804         pSrc->pColorPipeParams->bEnableSTE = true;
2805         pSrc->pColorPipeParams->SteParams.dwSTEFactor = (uint32_t)pSTEParamBuff->value;
2806     }
2807 
2808     return VA_STATUS_SUCCESS;
2809 }
2810 
2811 /////////////////////////////////////////////////////////////////////////////////////////////////
2812 //! \purpose Total Color Correction (TCC) filter params for VPHAL input surface
2813 //! \params
2814 //! [in]  pVpCtx : VP context
2815 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2816 //! [in]  pTCCParamBuff : Pointer to Total Color Correction param buffer data
2817 //! [in]  uElementNum : number of elements in the Total Color Correction param buffer data
2818 //! [out] None
2819 //! \returns VA_STATUS_SUCCESS if call succeeds
2820 //////////////////////////////////////////////////////////////////////////////////////////////////
2821 VAStatus
DdiVp_SetProcFilterTotalColorCorrectionParams(PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,VAProcFilterParameterBufferTotalColorCorrection * pTCCParamBuff,uint32_t uElementNum)2822 DdiVp_SetProcFilterTotalColorCorrectionParams(
2823     PDDI_VP_CONTEXT                                  pVpCtx,
2824     uint32_t                                         uSurfIndex,
2825     VAProcFilterParameterBufferTotalColorCorrection* pTCCParamBuff,
2826     uint32_t                                         uElementNum)
2827 {
2828     PVPHAL_RENDER_PARAMS pVpHalRenderParams;
2829     PVPHAL_SURFACE       pSrc;
2830     uint32_t             i;
2831 
2832     VP_DDI_FUNCTION_ENTER;
2833     DDI_CHK_NULL(pTCCParamBuff, "Null pTCCParamBuff.", VA_STATUS_ERROR_INVALID_BUFFER);
2834 
2835     // initialize
2836     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2837     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
2838     pSrc = pVpHalRenderParams->pSrc[uSurfIndex];
2839     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
2840 
2841     if (SURF_IN_PRIMARY == pSrc->SurfType)
2842     {
2843         if (nullptr == pSrc->pColorPipeParams)
2844         {
2845             pSrc->pColorPipeParams = (PVPHAL_COLORPIPE_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_COLORPIPE_PARAMS));
2846             DDI_CHK_NULL(pSrc->pColorPipeParams, "pColorPipeParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2847         }
2848 
2849         // set default values
2850         pSrc->pColorPipeParams->TccParams.Red     = (uint8_t)TCC_DEFAULT;
2851         pSrc->pColorPipeParams->TccParams.Green   = (uint8_t)TCC_DEFAULT;
2852         pSrc->pColorPipeParams->TccParams.Blue    = (uint8_t)TCC_DEFAULT;
2853         pSrc->pColorPipeParams->TccParams.Cyan    = (uint8_t)TCC_DEFAULT;
2854         pSrc->pColorPipeParams->TccParams.Magenta = (uint8_t)TCC_DEFAULT;
2855         pSrc->pColorPipeParams->TccParams.Yellow  = (uint8_t)TCC_DEFAULT;
2856 
2857         for (i = 0; i < uElementNum; i++)
2858         {
2859             if (pTCCParamBuff[i].value < TCC_MIN || pTCCParamBuff[i].value > TCC_MAX)
2860                 return VA_STATUS_ERROR_INVALID_PARAMETER;
2861 
2862             pSrc->pColorPipeParams->bEnableTCC = true;
2863 
2864             switch (pTCCParamBuff[i].attrib)
2865             {
2866             case VAProcTotalColorCorrectionRed:
2867                 pSrc->pColorPipeParams->TccParams.Red     = (uint8_t)pTCCParamBuff[i].value;
2868                 break;
2869 
2870             case VAProcTotalColorCorrectionGreen:
2871                 pSrc->pColorPipeParams->TccParams.Green   = (uint8_t)pTCCParamBuff[i].value;
2872                 break;
2873 
2874             case VAProcTotalColorCorrectionBlue:
2875                 pSrc->pColorPipeParams->TccParams.Blue    = (uint8_t)pTCCParamBuff[i].value;
2876                 break;
2877 
2878             case VAProcTotalColorCorrectionCyan:
2879                 pSrc->pColorPipeParams->TccParams.Cyan    = (uint8_t)pTCCParamBuff[i].value;
2880                 break;
2881 
2882             case VAProcTotalColorCorrectionMagenta:
2883                 pSrc->pColorPipeParams->TccParams.Magenta = (uint8_t)pTCCParamBuff[i].value;
2884                 break;
2885 
2886             case VAProcTotalColorCorrectionYellow:
2887                 pSrc->pColorPipeParams->TccParams.Yellow  = (uint8_t)pTCCParamBuff[i].value;
2888                 break;
2889 
2890             case VAProcTotalColorCorrectionNone:
2891             case VAProcTotalColorCorrectionCount:
2892             default:
2893                 VP_DDI_ASSERTMESSAGE("pTCCParamBuff[%d].attrib is unknown.", i);
2894                 return VA_STATUS_ERROR_INVALID_PARAMETER;
2895             }
2896         }
2897     }
2898 
2899     return VA_STATUS_SUCCESS;
2900 }
2901 
2902 /////////////////////////////////////////////////////////////////////////////////////////////////
2903 //! \purpose High Dynamic Range (HDR) Tone Mapping filter params for VPHAL input surface
2904 //! \params
2905 //! [in]  pVpCtx : VP context
2906 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2907 //! [in]  pHDRParamBuff : Pointer to High Dynamic Range Tone Mapping param buffer data
2908 //! [in]  uElementNum : number of elements in the High Dynamic Range Tone Mapping param buffer data
2909 //! [out] None
2910 //! \returns VA_STATUS_SUCCESS if call succeeds
2911 //////////////////////////////////////////////////////////////////////////////////////////////////
2912 VAStatus
DdiVp_SetProcFilterHdrTmParams(PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,VAProcFilterParameterBufferHDRToneMapping * pHdrTmParamBuff)2913 DdiVp_SetProcFilterHdrTmParams(
2914     PDDI_VP_CONTEXT                                  pVpCtx,
2915     uint32_t                                         uSurfIndex,
2916     VAProcFilterParameterBufferHDRToneMapping*       pHdrTmParamBuff)
2917 {
2918     PVPHAL_RENDER_PARAMS pVpHalRenderParams  = nullptr;
2919     PVPHAL_SURFACE       pSrc = nullptr;
2920     VAStatus             eStatus;
2921 
2922     VP_DDI_FUNCTION_ENTER;
2923     DDI_CHK_NULL(pHdrTmParamBuff, "Null pHdrTmParamBuff.", VA_STATUS_ERROR_INVALID_BUFFER);
2924 
2925     // initialize
2926     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2927     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
2928     pSrc = pVpHalRenderParams->pSrc[uSurfIndex];
2929     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
2930 
2931     eStatus = VpUpdateProcHdrState(pSrc, &pHdrTmParamBuff->data);
2932 
2933     return eStatus;
2934 }
2935 #if VA_CHECK_VERSION(1, 12, 0)
2936 /////////////////////////////////////////////////////////////////////////////////////////////////
2937 //! \purpose Three Three-Dimensional Look Up Table(3DLUT) filter params for VPHAL input surface
2938 //! \params
2939 //! [in]  pVaDrvCtx: Driver context
2940 //! [in]  pVpCtx : VP context
2941 //! [in]  uSurfIndex : uSurfIndex to the input surface array
2942 //! [in]  p3DLutParamBuff : Pointer to 3DLUT param buffer data
2943 //! [out] None
2944 //! \returns VA_STATUS_SUCCESS if call succeeds
2945 //////////////////////////////////////////////////////////////////////////////////////////////////
2946 VAStatus
DdiVp_SetProcFilter3DLutParams(VADriverContextP pVaDrvCtx,PDDI_VP_CONTEXT pVpCtx,uint32_t uSurfIndex,VAProcFilterParameterBuffer3DLUT * p3DLutParamBuff)2947 DdiVp_SetProcFilter3DLutParams(
2948     VADriverContextP                                 pVaDrvCtx,
2949     PDDI_VP_CONTEXT                                  pVpCtx,
2950     uint32_t                                         uSurfIndex,
2951     VAProcFilterParameterBuffer3DLUT*                p3DLutParamBuff)
2952 {
2953     PVPHAL_RENDER_PARAMS pVpHalRenderParams  = nullptr;
2954     PVPHAL_SURFACE       pSrc                = nullptr;
2955     VAStatus             eStatus             = VA_STATUS_SUCCESS;
2956 
2957     PDDI_MEDIA_SURFACE   pMedia3DLutSurf     = nullptr;
2958     PDDI_MEDIA_CONTEXT   pMediaCtx           = nullptr;
2959 
2960     VP_DDI_FUNCTION_ENTER;
2961     DDI_CHK_NULL(p3DLutParamBuff, "Null p3DLutParamBuff.", VA_STATUS_ERROR_INVALID_BUFFER);
2962     DDI_CHK_NULL(pVaDrvCtx,       "Null pVaDrvCtx.",       VA_STATUS_ERROR_INVALID_BUFFER);
2963 
2964     pMediaCtx   = DdiMedia_GetMediaContext(pVaDrvCtx);
2965 
2966     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
2967     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
2968     pSrc = pVpHalRenderParams->pSrc[uSurfIndex];
2969     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
2970 
2971     pMedia3DLutSurf = DdiMedia_GetSurfaceFromVASurfaceID(pMediaCtx, p3DLutParamBuff->lut_surface);
2972     DDI_CHK_NULL(pMedia3DLutSurf, "Null pMedia3DLutSurf.", VA_STATUS_ERROR_INVALID_SURFACE);
2973 
2974     // only primary surface takes effect if 3DLUT filter
2975     if (SURF_IN_PRIMARY == pSrc->SurfType)
2976     {
2977         if (nullptr == pSrc->p3DLutParams)
2978         {
2979             pSrc->p3DLutParams = (PVPHAL_3DLUT_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_3DLUT_PARAMS));
2980             DDI_CHK_NULL(pSrc->p3DLutParams, "p3DLutParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2981         }
2982 
2983         pSrc->p3DLutParams->LutSize               = (uint32_t)p3DLutParamBuff->lut_size;
2984         pSrc->p3DLutParams->BitDepthPerChannel    = (uint16_t)p3DLutParamBuff->bit_depth;
2985         pSrc->p3DLutParams->ByteCountPerEntry     = (uint16_t)(p3DLutParamBuff->num_channel * (pSrc->p3DLutParams->BitDepthPerChannel / 8));
2986         if (nullptr == pSrc->p3DLutParams->pExt3DLutSurface)
2987         {
2988             pSrc->p3DLutParams->pExt3DLutSurface      = (PVPHAL_SURFACE)MOS_AllocAndZeroMemory(sizeof(VPHAL_SURFACE));
2989             DDI_CHK_NULL(pSrc->p3DLutParams->pExt3DLutSurface, "p3DLutParams MOS_AllocAndZeroMemory failed.", VA_STATUS_ERROR_ALLOCATION_FAILED);
2990         }
2991 
2992         if (pSrc->p3DLutParams->pExt3DLutSurface)
2993         {
2994             PMOS_RESOURCE pOsResource     = nullptr;
2995             pOsResource                   = &(pSrc->p3DLutParams->pExt3DLutSurface->OsResource);
2996             DDI_CHK_NULL(pOsResource, "Null  pOsResource.", VA_STATUS_ERROR_INVALID_PARAMETER);
2997 
2998             DdiMedia_MediaSurfaceToMosResource(pMedia3DLutSurf, pOsResource);
2999             Mos_Solo_SetOsResource(pMedia3DLutSurf->pGmmResourceInfo, pOsResource);
3000         }
3001     }
3002 
3003     return eStatus;
3004 }
3005 #endif
3006 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3007 //! \purpose Create Media Buffer
3008 //! \params
3009 //! [in]  pVaDrvCtx : VA Driver context
3010 //! [in]  pVpCtx : VP context
3011 //! [in]  vaBufType : VA buffer type
3012 //! [in]  uiSize : element size
3013 //! [in]  uiNumElements : number of elements
3014 //! [in]  pDataClient : client data
3015 //! [inout] pVaBufID : buffer ID
3016 //! \returns VA_STATUS_SUCCESS if call succeeds
3017 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DdiVp_CreateBuffer(VADriverContextP pVaDrvCtx,void * pCtx,VABufferType vaBufType,uint32_t uiSize,uint32_t uiNumElements,void * pDataClient,VABufferID * pVaBufID)3018 VAStatus DdiVp_CreateBuffer(
3019     VADriverContextP           pVaDrvCtx,
3020     void                       *pCtx,
3021     VABufferType               vaBufType,
3022     uint32_t                   uiSize,
3023     uint32_t                   uiNumElements,
3024     void                       *pDataClient,
3025     VABufferID*                pVaBufID
3026 )
3027 {
3028     PDDI_MEDIA_CONTEXT             pMediaCtx;
3029     PDDI_MEDIA_BUFFER              pBuf;
3030     VAStatus                       vaStatus;
3031     PDDI_MEDIA_BUFFER_HEAP_ELEMENT pBufferHeapElement;
3032     MOS_STATUS                     eStatus = MOS_STATUS_SUCCESS;
3033 
3034     VP_DDI_FUNCTION_ENTER;
3035     DDI_CHK_NULL(pCtx, "Null pCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
3036     DDI_CHK_NULL(pVaDrvCtx,
3037                     "Null pVaDrvCtx.",
3038                     VA_STATUS_ERROR_INVALID_CONTEXT);
3039 
3040     vaStatus    = VA_STATUS_SUCCESS;
3041     *pVaBufID   = VA_INVALID_ID;
3042 
3043     pMediaCtx   = DdiMedia_GetMediaContext(pVaDrvCtx);
3044     DDI_CHK_NULL(pMediaCtx,
3045                     "Null pMediaCtx.",
3046                     VA_STATUS_ERROR_INVALID_CONTEXT);
3047 
3048     PDDI_VP_CONTEXT  pVpCtx = (PDDI_VP_CONTEXT)pCtx;
3049 
3050     // only for VAProcFilterParameterBufferType and VAProcPipelineParameterBufferType
3051     if (vaBufType != VAProcFilterParameterBufferType
3052         && vaBufType != VAProcPipelineParameterBufferType
3053 #if VA_CHECK_VERSION(1, 10, 0)
3054         && vaBufType != VAContextParameterUpdateBufferType
3055 #endif
3056        )
3057     {
3058         VP_DDI_ASSERTMESSAGE("Unsupported Va Buffer Type.");
3059         return VA_STATUS_ERROR_INVALID_PARAMETER;
3060     }
3061 
3062     // allocate new buf and init
3063     pBuf               = (DDI_MEDIA_BUFFER *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_BUFFER));
3064     DDI_CHK_NULL(pBuf, "Null pBuf.", VA_STATUS_ERROR_ALLOCATION_FAILED);
3065     pBuf->pMediaCtx     = pMediaCtx;
3066     pBuf->iSize         = uiSize * uiNumElements;
3067     pBuf->uiNumElements = uiNumElements;
3068     pBuf->uiType        = vaBufType;
3069     pBuf->format        = Media_Format_Buffer;
3070     pBuf->uiOffset      = 0;
3071     pBuf->pData         = (uint8_t*)MOS_AllocAndZeroMemory(uiSize * uiNumElements);
3072     if (nullptr == pBuf->pData)
3073     {
3074         MOS_FreeMemAndSetNull(pBuf);
3075         return VA_STATUS_ERROR_ALLOCATION_FAILED;
3076     }
3077     pBuf->format       = Media_Format_CPU;
3078 
3079     pBufferHeapElement  = DdiMediaUtil_AllocPMediaBufferFromHeap(pMediaCtx->pBufferHeap);
3080     if (nullptr == pBufferHeapElement)
3081     {
3082         MOS_FreeMemAndSetNull(pBuf->pData);
3083         MOS_FreeMemAndSetNull(pBuf);
3084         VP_DDI_ASSERTMESSAGE("Invalid buffer index.");
3085         return VA_STATUS_ERROR_INVALID_BUFFER;
3086     }
3087     pBufferHeapElement->pBuffer      = pBuf;
3088     pBufferHeapElement->pCtx         = (void *)pVpCtx;
3089     pBufferHeapElement->uiCtxType    = DDI_MEDIA_CONTEXT_TYPE_VP;
3090     *pVaBufID                        = pBufferHeapElement->uiVaBufferID;
3091     pMediaCtx->uiNumBufs++;
3092 
3093     // if there is data from client, then dont need to copy data from client
3094     if (pDataClient)
3095     {
3096         // copy client data to new buf
3097         eStatus = MOS_SecureMemcpy(pBuf->pData, uiSize * uiNumElements, pDataClient, uiSize * uiNumElements);
3098         DDI_CHK_CONDITION((eStatus != MOS_STATUS_SUCCESS), "DDI: Failed to copy client data!", VA_STATUS_ERROR_MAX_NUM_EXCEEDED);
3099     }
3100     else
3101     {
3102         // do nothing if there is no data from client
3103         vaStatus = VA_STATUS_SUCCESS;
3104     }
3105 
3106     return vaStatus;
3107 }
3108 
3109 /////////////////////////////////////////////////////////////////////////////
3110 //! \purpose Create a new VP context, and put in pVpCtx array
3111 //! \params
3112 //! [in]  pVaDrvCtx : VA Driver context
3113 //! [in]  vaConfigID :
3114 //! [in]  iWidth :
3115 //! [in]  iHeight :
3116 //! [in]  iFlag :
3117 //! [in]  vaSurfIDs :
3118 //! [in]  iNumSurfs :
3119 //! [inout] pVaCtxID : VA context ID
3120 //! \returns VA_STATUS_SUCCESS if call succeeds
3121 /////////////////////////////////////////////////////////////////////////////
DdiVp_CreateContext(VADriverContextP pVaDrvCtx,VAConfigID vaConfigID,int32_t iWidth,int32_t iHeight,int32_t iFlag,VASurfaceID * vaSurfIDs,int32_t iNumSurfs,VAContextID * pVaCtxID)3122 VAStatus DdiVp_CreateContext (
3123     VADriverContextP    pVaDrvCtx,
3124     VAConfigID          vaConfigID,
3125     int32_t             iWidth,
3126     int32_t             iHeight,
3127     int32_t             iFlag,
3128     VASurfaceID        *vaSurfIDs,
3129     int32_t             iNumSurfs,
3130     VAContextID        *pVaCtxID
3131 )
3132 {
3133     PERF_UTILITY_AUTO(__FUNCTION__, PERF_VP, PERF_LEVEL_DDI);
3134 
3135     PDDI_MEDIA_CONTEXT                pMediaCtx;
3136     VAStatus                          vaStatus;
3137     PDDI_VP_CONTEXT                   pVpCtx;
3138     PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT pVaCtxHeapElmt;
3139     DDI_UNUSED(vaConfigID);
3140     DDI_UNUSED(iWidth);
3141     DDI_UNUSED(iHeight);
3142     DDI_UNUSED(iFlag);
3143     DDI_UNUSED(vaSurfIDs);
3144     DDI_UNUSED(iNumSurfs);
3145 
3146     VP_DDI_FUNCTION_ENTER;
3147     DDI_CHK_NULL(pVaDrvCtx,
3148                     "Null pVaDrvCtx.",
3149                     VA_STATUS_ERROR_INVALID_CONTEXT);
3150 
3151     pVpCtx    = nullptr;
3152     *pVaCtxID = VA_INVALID_ID;
3153 
3154     pMediaCtx = DdiMedia_GetMediaContext(pVaDrvCtx);
3155     DDI_CHK_NULL(pMediaCtx,
3156                     "Null pMediaCtx.",
3157                     VA_STATUS_ERROR_INVALID_CONTEXT);
3158 
3159     // allocate pVpCtx
3160     pVpCtx = MOS_New(DDI_VP_CONTEXT);
3161     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_ALLOCATION_FAILED);
3162 
3163     // init pVpCtx
3164     vaStatus = DdiVp_InitCtx(pVaDrvCtx, pVpCtx);
3165     DDI_CHK_RET(vaStatus, "VA_STATUS_ERROR_OPERATION_FAILED");
3166 
3167     DdiMediaUtil_LockMutex(&pMediaCtx->VpMutex);
3168 
3169     // get Free VP context index
3170     pVaCtxHeapElmt = DdiMediaUtil_AllocPVAContextFromHeap(pMediaCtx->pVpCtxHeap);
3171     if (nullptr == pVaCtxHeapElmt)
3172     {
3173         MOS_Delete(pVpCtx);
3174         DdiMediaUtil_UnLockMutex(&pMediaCtx->VpMutex);
3175         VP_DDI_ASSERTMESSAGE("VP Context number exceeds maximum.");
3176         return VA_STATUS_ERROR_INVALID_CONTEXT;
3177     }
3178 
3179     // store pVpCtx in pMedia
3180     pVaCtxHeapElmt->pVaContext    = (void *)pVpCtx;
3181     *pVaCtxID = (VAContextID)(pVaCtxHeapElmt->uiVaContextID + DDI_MEDIA_VACONTEXTID_OFFSET_VP);
3182 
3183     // increate VP context number
3184     pMediaCtx->uiNumVPs++;
3185 
3186     DdiMediaUtil_UnLockMutex(&pMediaCtx->VpMutex);
3187 
3188     return VA_STATUS_SUCCESS;
3189 }
3190 
3191 /////////////////////////////////////////////////////////////////////////////
3192 //! \purpose Destroy VP context
3193 //! \params
3194 //! [in]  pVaDrvCtx : VA Driver context
3195 //! [in]  vaCtxID : VA Context ID
3196 //! [out] None
3197 //! \returns VA_STATUS_SUCCESS if call succeeds
3198 /////////////////////////////////////////////////////////////////////////////
DdiVp_DestroyContext(VADriverContextP pVaDrvCtx,VAContextID vaCtxID)3199 VAStatus DdiVp_DestroyContext (
3200     VADriverContextP    pVaDrvCtx,
3201     VAContextID         vaCtxID
3202     )
3203 {
3204     PERF_UTILITY_AUTO(__FUNCTION__, PERF_VP, PERF_LEVEL_DDI);
3205 
3206     PDDI_MEDIA_CONTEXT       pMediaCtx;
3207     PDDI_VP_CONTEXT          pVpCtx;
3208     uint32_t                 uiVpIndex;
3209     uint32_t                 ctxType;
3210     VAStatus                 vaStatus;
3211 
3212     VP_DDI_FUNCTION_ENTER;
3213     DDI_CHK_NULL(pVaDrvCtx,
3214                     "Null pVaDrvCtx.",
3215                     VA_STATUS_ERROR_INVALID_CONTEXT);
3216 
3217     // initialize
3218     pMediaCtx = DdiMedia_GetMediaContext(pVaDrvCtx);
3219     DDI_CHK_NULL(pMediaCtx,
3220                     "Null pMediaCtx.",
3221                     VA_STATUS_ERROR_INVALID_CONTEXT);
3222     pVpCtx    = (PDDI_VP_CONTEXT)DdiMedia_GetContextFromContextID(pVaDrvCtx, vaCtxID, &ctxType);
3223     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
3224 
3225     MOS_FreeMemory(pVpCtx->MosDrvCtx.pPerfData);
3226     pVpCtx->MosDrvCtx.pPerfData = nullptr;
3227 
3228     if (pVpCtx->pCpDdiInterface)
3229     {
3230         Delete_DdiCpInterface(pVpCtx->pCpDdiInterface);
3231         pVpCtx->pCpDdiInterface = NULL;
3232     }
3233 
3234     // destroy vphal
3235     vaStatus  = DdiVp_DestroyVpHal(pVpCtx);
3236 
3237     // Get VP context index
3238     uiVpIndex = vaCtxID & DDI_MEDIA_MASK_VACONTEXTID;
3239 
3240     // remove from context array
3241     DdiMediaUtil_LockMutex(&pMediaCtx->VpMutex);
3242     // destroy vp context
3243     MOS_Delete(pVpCtx);
3244     DdiMediaUtil_ReleasePVAContextFromHeap(pMediaCtx->pVpCtxHeap, uiVpIndex);
3245 
3246     pMediaCtx->uiNumVPs--;
3247     DdiMediaUtil_UnLockMutex(&pMediaCtx->VpMutex);
3248 
3249     return vaStatus;
3250 }
3251 
3252 ////////////////////////////////////////////////////////////////////////////////
3253 //! \purpose Get ready to process a picture to a target surface
3254 //! \params
3255 //! [in]  pVaDrvCtx : VA Driver Context
3256 //! [in]  vaCtxID : VA context ID
3257 //! [in]  vaSurfID : target surface ID
3258 //! [out] None
3259 //! \returns VA_STATUS_SUCCESS if call succeeds
3260 ////////////////////////////////////////////////////////////////////////////////
DdiVp_BeginPicture(VADriverContextP pVaDrvCtx,VAContextID vaCtxID,VASurfaceID vaSurfID)3261 VAStatus DdiVp_BeginPicture(
3262         VADriverContextP    pVaDrvCtx,
3263         VAContextID         vaCtxID,
3264         VASurfaceID         vaSurfID)
3265 {
3266     PERF_UTILITY_AUTO(__FUNCTION__, PERF_VP, PERF_LEVEL_DDI);
3267 
3268     PDDI_MEDIA_CONTEXT          pMediaDrvCtx;
3269     PDDI_VP_CONTEXT             pVpCtx;
3270     uint32_t                    ctxType;
3271     VAStatus                    vaStatus;
3272     PVPHAL_RENDER_PARAMS        pVpHalRenderParams;
3273     PVPHAL_SURFACE              pVpHalTgtSurf;
3274     PDDI_MEDIA_SURFACE          pMediaTgtSurf;
3275 
3276     VP_DDI_FUNCTION_ENTER;
3277     DDI_CHK_NULL(pVaDrvCtx,
3278                     "Null pVaDrvCtx.",
3279                     VA_STATUS_ERROR_INVALID_CONTEXT);
3280 
3281     // initialize
3282     pMediaDrvCtx         = DdiMedia_GetMediaContext(pVaDrvCtx);
3283     DDI_CHK_NULL(pMediaDrvCtx,
3284                     "Null pMediaDrvCtx.",
3285                     VA_STATUS_ERROR_INVALID_CONTEXT);
3286     pVpCtx               = (PDDI_VP_CONTEXT)DdiMedia_GetContextFromContextID (
3287                                 pVaDrvCtx, vaCtxID, &ctxType);
3288     DDI_CHK_NULL(pVpCtx,
3289                     "Null pVpCtx.",
3290                     VA_STATUS_ERROR_INVALID_CONTEXT);
3291     pVpCtx->TargetSurfID = vaSurfID;
3292     pVpHalRenderParams   = VpGetRenderParams(pVpCtx);
3293     DDI_CHK_NULL(pVpHalRenderParams,
3294                     "Null pVpHalRenderParams.",
3295                     VA_STATUS_ERROR_INVALID_PARAMETER);
3296 
3297     // uDstCount == 0 means no render target is set yet.
3298     // uDstCount == 1 means 1 render target has been set already.
3299     // uDstCount == 2 means 2 render targets have been set already.
3300     DDI_CHK_LESS(pVpHalRenderParams->uDstCount, VPHAL_MAX_TARGETS,
3301         "Too many render targets for VP.",
3302         VA_STATUS_ERROR_INVALID_PARAMETER);
3303 
3304     pVpHalTgtSurf = pVpHalRenderParams->pTarget[pVpHalRenderParams->uDstCount];
3305     DDI_CHK_NULL(pVpHalTgtSurf,
3306                     "Null pVpHalTgtSurf.",
3307                     VA_STATUS_ERROR_INVALID_SURFACE);
3308     pMediaTgtSurf        = DdiMedia_GetSurfaceFromVASurfaceID(pMediaDrvCtx, vaSurfID);
3309     DDI_CHK_NULL(pMediaTgtSurf,
3310                     "Null pMediaTgtSurf.",
3311                     VA_STATUS_ERROR_INVALID_SURFACE);
3312 
3313     pMediaTgtSurf->pVpCtx = pVpCtx;
3314 
3315     // Setup Target VpHal Surface
3316     pVpHalTgtSurf->SurfType      = SURF_OUT_RENDERTARGET;
3317     pVpHalTgtSurf->rcSrc.top     = 0;
3318     pVpHalTgtSurf->rcSrc.left    = 0;
3319     pVpHalTgtSurf->rcSrc.right   = pMediaTgtSurf->iWidth;
3320     pVpHalTgtSurf->rcSrc.bottom  = pMediaTgtSurf->iRealHeight;
3321     pVpHalTgtSurf->rcDst.top     = 0;
3322     pVpHalTgtSurf->rcDst.left    = 0;
3323     pVpHalTgtSurf->rcDst.right   = pMediaTgtSurf->iWidth;
3324     pVpHalTgtSurf->rcDst.bottom  = pMediaTgtSurf->iRealHeight;
3325     pVpHalTgtSurf->ExtendedGamut = false;
3326 
3327     // Set os resource for VPHal render
3328     vaStatus = VpSetOsResource(pVpCtx, pMediaTgtSurf, pVpHalRenderParams->uDstCount);
3329     DDI_CHK_RET(vaStatus, "Call VpSetOsResource failed");
3330 
3331     pVpHalTgtSurf->Format   = pVpHalTgtSurf->OsResource.Format;
3332     pVpHalTgtSurf->TileType = pVpHalTgtSurf->OsResource.TileType;
3333 
3334     // reset source surface count
3335     pVpHalRenderParams->uSrcCount = 0;
3336 
3337     pVpHalRenderParams->bReportStatus    = true;
3338     pVpHalRenderParams->StatusFeedBackID = vaSurfID;
3339 
3340     if (pMediaTgtSurf->pSurfDesc && (pMediaTgtSurf->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR))
3341     {
3342         pVpHalRenderParams->pTarget[pVpHalRenderParams->uDstCount]->b16UsrPtr = VpIs16UsrPtrPitch(pMediaTgtSurf->iPitch, pMediaTgtSurf->format);
3343     }
3344     else
3345     {
3346         pVpHalRenderParams->pTarget[pVpHalRenderParams->uDstCount]->b16UsrPtr = false;
3347     }
3348 
3349     // increase render target count
3350     pVpHalRenderParams->uDstCount++;
3351 
3352     return VA_STATUS_SUCCESS;
3353 }
3354 
3355 ////////////////////////////////////////////////////////////////////////////////
3356 //! \purpose Get ready to process a picture to a target surface
3357 //! \params
3358 //! [in]  pVaDrvCtx : VA Driver Context
3359 //! [in]  vaCtxID : VA context ID
3360 //! [in]  vaSurfID : target surface ID
3361 //! [out] None
3362 //! \returns VA_STATUS_SUCCESS if call succeeds
3363 ////////////////////////////////////////////////////////////////////////////////
DdiVp_BeginPictureInt(VADriverContextP pVaDrvCtx,PDDI_VP_CONTEXT pVpCtx,VASurfaceID vaSurfID)3364 VAStatus DdiVp_BeginPictureInt(
3365         VADriverContextP    pVaDrvCtx,
3366         PDDI_VP_CONTEXT     pVpCtx,
3367         VASurfaceID         vaSurfID)
3368 {
3369     PDDI_MEDIA_CONTEXT          pMediaDrvCtx;
3370     uint32_t                    ctxType;
3371     VAStatus                    vaStatus;
3372     PVPHAL_RENDER_PARAMS        pVpHalRenderParams;
3373     PVPHAL_SURFACE              pVpHalTgtSurf;
3374     PDDI_MEDIA_SURFACE          pMediaTgtSurf;
3375 
3376     VP_DDI_FUNCTION_ENTER;
3377     DDI_CHK_NULL(pVaDrvCtx,
3378                     "Null pVaDrvCtx.",
3379                     VA_STATUS_ERROR_INVALID_CONTEXT);
3380 
3381     // initialize
3382     pMediaDrvCtx         = DdiMedia_GetMediaContext(pVaDrvCtx);
3383     DDI_CHK_NULL(pMediaDrvCtx,
3384                     "Null pMediaDrvCtx.",
3385                     VA_STATUS_ERROR_INVALID_CONTEXT);
3386     DDI_CHK_NULL(pVpCtx,
3387                     "Null pVpCtx.",
3388                     VA_STATUS_ERROR_INVALID_CONTEXT);
3389     pVpCtx->TargetSurfID = vaSurfID;
3390     pVpHalRenderParams   = VpGetRenderParams(pVpCtx);
3391     DDI_CHK_NULL(pVpHalRenderParams,
3392                     "Null pVpHalRenderParams.",
3393                     VA_STATUS_ERROR_INVALID_PARAMETER);
3394 
3395     // uDstCount == 0 means no render target is set yet.
3396     // uDstCount == 1 means 1 render target has been set already.
3397     // uDstCount == 2 means 2 render targets have been set already.
3398     DDI_CHK_LESS(pVpHalRenderParams->uDstCount, VPHAL_MAX_TARGETS,
3399         "Too many render targets for VP.",
3400         VA_STATUS_ERROR_INVALID_PARAMETER);
3401 
3402     pVpHalTgtSurf = pVpHalRenderParams->pTarget[pVpHalRenderParams->uDstCount];
3403     DDI_CHK_NULL(pVpHalTgtSurf,
3404                     "Null pVpHalTgtSurf.",
3405                     VA_STATUS_ERROR_INVALID_SURFACE);
3406     pMediaTgtSurf        = DdiMedia_GetSurfaceFromVASurfaceID(pMediaDrvCtx, vaSurfID);
3407     DDI_CHK_NULL(pMediaTgtSurf,
3408                     "Null pMediaTgtSurf.",
3409                     VA_STATUS_ERROR_INVALID_SURFACE);
3410 
3411     pMediaTgtSurf->pVpCtx = pVpCtx;
3412 
3413     // Setup Target VpHal Surface
3414     pVpHalTgtSurf->SurfType      = SURF_OUT_RENDERTARGET;
3415     pVpHalTgtSurf->rcSrc.top     = 0;
3416     pVpHalTgtSurf->rcSrc.left    = 0;
3417     pVpHalTgtSurf->rcSrc.right   = pMediaTgtSurf->iWidth;
3418     pVpHalTgtSurf->rcSrc.bottom  = pMediaTgtSurf->iRealHeight;
3419     pVpHalTgtSurf->rcDst.top     = 0;
3420     pVpHalTgtSurf->rcDst.left    = 0;
3421     pVpHalTgtSurf->rcDst.right   = pMediaTgtSurf->iWidth;
3422     pVpHalTgtSurf->rcDst.bottom  = pMediaTgtSurf->iRealHeight;
3423     pVpHalTgtSurf->ExtendedGamut = false;
3424 
3425     // Set os resource for VPHal render
3426     vaStatus = VpSetOsResource(pVpCtx, pMediaTgtSurf, pVpHalRenderParams->uDstCount);
3427     DDI_CHK_RET(vaStatus, "Call VpSetOsResource failed");
3428 
3429     pVpHalTgtSurf->Format = pVpHalTgtSurf->OsResource.Format;
3430 
3431     pVpHalRenderParams->bReportStatus    = true;
3432     pVpHalRenderParams->StatusFeedBackID = vaSurfID;
3433     if (pMediaTgtSurf->pSurfDesc && (pMediaTgtSurf->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR))
3434     {
3435         pVpHalRenderParams->pTarget[pVpHalRenderParams->uDstCount]->b16UsrPtr = VpIs16UsrPtrPitch(pMediaTgtSurf->iPitch, pMediaTgtSurf->format);
3436     }
3437     else
3438     {
3439         pVpHalRenderParams->pTarget[pVpHalRenderParams->uDstCount]->b16UsrPtr = false;
3440     }
3441     // increase render target count
3442     pVpHalRenderParams->uDstCount++;
3443 
3444     return VA_STATUS_SUCCESS;
3445 }
3446 
3447 ////////////////////////////////////////////////////////////////////////////////
3448 //! \purpose judge whether the PipelineParam buffer is for target or not
3449 //! \params
3450 //! [in]  pVaDrvCtx : VA Driver context
3451 //! [in]  pVpCtx : VP context
3452 //! [in]  pPipelineParam : Pipeline parameters from application (VAProcPipelineParameterBuffer)
3453 //! [out] None
3454 //! \returns VA_STATUS_SUCCESS if call succeeds
3455 ////////////////////////////////////////////////////////////////////////////////
VpIsRenderTarget(VADriverContextP pVaDrvCtx,PDDI_VP_CONTEXT pVpCtx,VAProcPipelineParameterBuffer * pPipelineParam)3456 bool VpIsRenderTarget(
3457     VADriverContextP                pVaDrvCtx,
3458     PDDI_VP_CONTEXT                 pVpCtx,
3459     VAProcPipelineParameterBuffer*  pPipelineParam)
3460 {
3461     PVPHAL_RENDER_PARAMS    pVpHalRenderParams;
3462     PDDI_MEDIA_CONTEXT      pMediaCtx;
3463     PDDI_MEDIA_SURFACE      pMediaSrcSurf;
3464     PVPHAL_SURFACE          pVpHalTgtSurf;
3465     bool                    IsTarget = false;
3466 
3467     VP_DDI_FUNCTION_ENTER;
3468     DDI_CHK_NULL(pVaDrvCtx, "Null pVaDrvCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
3469     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
3470     DDI_CHK_NULL(pPipelineParam, "Null pPipelineParam.", VA_STATUS_ERROR_INVALID_BUFFER);
3471 
3472     pMediaCtx       = DdiMedia_GetMediaContext(pVaDrvCtx);
3473     DDI_CHK_NULL(pMediaCtx, "Null pMediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
3474     pMediaSrcSurf   = DdiMedia_GetSurfaceFromVASurfaceID(pMediaCtx, pPipelineParam->surface);
3475     DDI_CHK_NULL(pMediaSrcSurf, "Null pMediaSrcSurf.", VA_STATUS_ERROR_INVALID_BUFFER);
3476     pVpHalRenderParams  = pVpCtx->pVpHalRenderParams;
3477     DDI_CHK_NULL(pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
3478 
3479     if(pPipelineParam->pipeline_flags == 0 && pVpHalRenderParams->uDstCount >= 1)
3480     {
3481         pVpHalTgtSurf = pVpHalRenderParams->pTarget[pVpHalRenderParams->uDstCount-1];
3482 
3483         IsTarget = ((pVpHalTgtSurf->OsResource.bo != nullptr)
3484                &&(pVpHalTgtSurf->OsResource.bo == pMediaSrcSurf->bo));
3485     }
3486 
3487     return IsTarget;
3488 }
3489 ////////////////////////////////////////////////////////////////////////////////
3490 //! \purpose Send buffers to the server
3491 //! \params
3492 //! [in]  pVaDrvCtx : VA Driver context
3493 //! [in]  vpCtxID : context ID
3494 //! [in]  buffers : array of buffer IDs
3495 //! [in]  num_buffers : number of buffers IDs in the array
3496 //! [out] None
3497 //! \returns VA_STATUS_SUCCESS if call succeeds
3498 ////////////////////////////////////////////////////////////////////////////////
DdiVp_RenderPicture(VADriverContextP pVaDrvCtx,VAContextID vpCtxID,VABufferID * buffers,int32_t num_buffers)3499 VAStatus DdiVp_RenderPicture (
3500     VADriverContextP    pVaDrvCtx,
3501     VAContextID         vpCtxID,
3502     VABufferID*         buffers,
3503     int32_t             num_buffers
3504 )
3505 {
3506     PERF_UTILITY_AUTO(__FUNCTION__, PERF_VP, PERF_LEVEL_DDI);
3507 
3508     PDDI_MEDIA_CONTEXT        pMediaCtx;
3509     PDDI_VP_CONTEXT           pVpCtx;
3510     PDDI_MEDIA_BUFFER         pBuf;
3511     int32_t                   i;
3512     void                      *pData;
3513     uint32_t                  ctxType;
3514     VAStatus                  vaStatus;
3515     int32_t                   numOfBuffers              = num_buffers;
3516     int32_t                   priority                  = 0;
3517     int32_t                   priorityIndexInBuffers    = -1;
3518     bool                      updatePriority            = false;
3519 
3520     VP_DDI_FUNCTION_ENTER;
3521     DDI_CHK_NULL(pVaDrvCtx,
3522                     "Null pVaDrvCtx.",
3523                     VA_STATUS_ERROR_INVALID_CONTEXT);
3524 
3525     vaStatus = VA_STATUS_SUCCESS;
3526 
3527     pMediaCtx = DdiMedia_GetMediaContext(pVaDrvCtx);
3528     DDI_CHK_NULL(pMediaCtx, "Null pMediaCtx", VA_STATUS_ERROR_OPERATION_FAILED);
3529 
3530     pVpCtx    = (PDDI_VP_CONTEXT)DdiMedia_GetContextFromContextID (pVaDrvCtx, vpCtxID, &ctxType);
3531     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
3532 
3533     //num_buffers check
3534     DDI_CHK_CONDITION(((numOfBuffers > VPHAL_MAX_SOURCES) || (numOfBuffers <= 0)),
3535                          "numOfBuffers is Invalid.",
3536                          VA_STATUS_ERROR_INVALID_PARAMETER);
3537 
3538     priorityIndexInBuffers = DdiMedia_GetGpuPriority(pVaDrvCtx, buffers, numOfBuffers, &updatePriority, &priority);
3539     if (priorityIndexInBuffers != -1)
3540     {
3541         if(updatePriority)
3542         {
3543             vaStatus = DdiVp_SetGpuPriority(pVpCtx, priority);
3544             if(vaStatus != VA_STATUS_SUCCESS)
3545                 return vaStatus;
3546         }
3547         MovePriorityBufferIdToEnd(buffers, priorityIndexInBuffers, numOfBuffers);
3548         numOfBuffers--;
3549     }
3550     if (numOfBuffers == 0)
3551         return vaStatus;
3552 
3553     for (i = 0; i < numOfBuffers; i++)
3554     {
3555         pBuf = DdiMedia_GetBufferFromVABufferID(pMediaCtx, buffers[i]);
3556         DDI_CHK_NULL(pBuf, "Null pBuf.", VA_STATUS_ERROR_INVALID_BUFFER);
3557 
3558         DdiMedia_MapBuffer(pVaDrvCtx, buffers[i], &pData);
3559         DDI_CHK_NULL(pData, "Null pData.", VA_STATUS_ERROR_INVALID_BUFFER);
3560 
3561         switch ((int32_t)pBuf->uiType)
3562         {
3563             // VP Buffer Types
3564             case VAProcPipelineParameterBufferType:
3565                 if(VpIsRenderTarget(pVaDrvCtx, pVpCtx,(VAProcPipelineParameterBuffer*)pData))
3566                 {
3567                     vaStatus=VpSetRenderTargetParams(pVaDrvCtx, pVpCtx,
3568                                                       (VAProcPipelineParameterBuffer*)pData);
3569                 }
3570                 else
3571                 {
3572                     vaStatus = DdiVp_SetProcPipelineParams(pVaDrvCtx, pVpCtx,
3573                                                       (VAProcPipelineParameterBuffer*)pData);
3574                     DDI_CHK_RET(vaStatus, "Unable to set pipeline parameters");
3575                 }
3576                 break;
3577             case VAProcFilterParameterBufferType:
3578                 // User is not supposed to pass this buffer type:Refer va_vpp.h
3579                 VP_DDI_ASSERTMESSAGE("Invalid buffer type.");
3580                 vaStatus = VA_STATUS_ERROR_INVALID_BUFFER;
3581                 break;
3582 
3583             default:
3584                 DDI_CHK_RET(vaStatus,"Unsupported buffer type!");
3585                 break;
3586         }
3587         DdiMedia_UnmapBuffer(pVaDrvCtx, buffers[i]);
3588     }
3589     return vaStatus;
3590 }
3591 
3592 ////////////////////////////////////////////////////////////////////////////////
3593 //! \purpose
3594 //!  Make the end of rendering for a picture.
3595 //!  The server should start processing all pending operations for this
3596 //!  surface. This call is non-blocking. The client can start another
3597 //!  Begin/Render/End sequence on a different render target.
3598 //! \params
3599 //! [in]  pVaDrvCtx : VA Driver Context
3600 //! [in]  vaCtxID : VA Context ID
3601 //! [out] None
3602 //! \returns VA_STATUS_SUCCESS if call succeeds
3603 ////////////////////////////////////////////////////////////////////////////////
DdiVp_EndPicture(VADriverContextP pVaDrvCtx,VAContextID vaCtxID)3604 VAStatus DdiVp_EndPicture (
3605         VADriverContextP    pVaDrvCtx,
3606         VAContextID         vaCtxID)
3607 {
3608     PERF_UTILITY_AUTO(__FUNCTION__, PERF_VP, PERF_LEVEL_DDI);
3609 
3610     PDDI_VP_CONTEXT         pVpCtx;
3611     uint32_t                uiCtxType;
3612     VpBase                  *pVpHal;
3613     MOS_STATUS              eStatus;
3614 
3615     VP_DDI_FUNCTION_ENTER;
3616     DDI_CHK_NULL(pVaDrvCtx,
3617                     "Null pVaDrvCtx.",
3618                     VA_STATUS_ERROR_INVALID_CONTEXT);
3619 
3620     //get VP Context
3621     pVpCtx = (PDDI_VP_CONTEXT)DdiMedia_GetContextFromContextID (pVaDrvCtx, vaCtxID, &uiCtxType);
3622     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
3623 
3624     //Add component tag for VP
3625     DDI_CHK_NULL(pVpCtx->pVpHalRenderParams, "Null pVpHalRenderParams.", VA_STATUS_ERROR_INVALID_PARAMETER);
3626     pVpCtx->pVpHalRenderParams->Component = COMPONENT_VPCommon;
3627 
3628     pVpHal  = pVpCtx->pVpHal;
3629     DDI_CHK_NULL(pVpHal, "Null pVpHal.", VA_STATUS_ERROR_INVALID_PARAMETER);
3630     eStatus = pVpHal->Render(pVpCtx->pVpHalRenderParams);
3631 
3632 #if (_DEBUG || _RELEASE_INTERNAL)
3633     VpDumpProcPipelineParams(pVaDrvCtx, pVpCtx);
3634 #endif //(_DEBUG || _RELEASE_INTERNAL)
3635 
3636     VpReportFeatureMode(pVpCtx);
3637 
3638     // Reset primary surface count for next render call
3639     pVpCtx->iPriSurfs = 0;
3640 
3641     // Reset render target count for next render call
3642     pVpCtx->pVpHalRenderParams->uDstCount = 0;
3643 
3644     if (MOS_FAILED(eStatus))
3645     {
3646         VP_DDI_ASSERTMESSAGE("Failed to call render function.");
3647         return VA_STATUS_ERROR_OPERATION_FAILED;
3648     }
3649 
3650     return VA_STATUS_SUCCESS;
3651 
3652 }
3653 
3654 ////////////////////////////////////////////////////////////////////////////////
3655 //! \purpose
3656 //!  Execute video processing pipeline.
3657 //!  For CSC/Scaling after decode by decode context.
3658 //! \params
3659 //! [in]  pVaDrvCtxv    : VA Driver Context
3660 //! [in]  vaCtxID       : VA Context ID
3661 //! [in]  srcSurface    : Input surface ID
3662 //! [in]  srcRect       : Rectangle of the input surface region
3663 //! [in]  dstSurface    : Output surface ID
3664 //! [in]  dstRect       : Rectangle of the output surface region
3665 //! [out] None
3666 //! \returns VA_STATUS_SUCCESS if call succeeds
3667 ////////////////////////////////////////////////////////////////////////////////
DdiVp_VideoProcessPipeline(VADriverContextP pVaDrvCtx,VAContextID vpCtxID,VASurfaceID srcSurface,VARectangle * srcRect,VASurfaceID dstSurface,VARectangle * dstRect)3668 VAStatus DdiVp_VideoProcessPipeline(
3669     VADriverContextP    pVaDrvCtx,
3670     VAContextID         vpCtxID,
3671     VASurfaceID         srcSurface,
3672     VARectangle         *srcRect,
3673     VASurfaceID         dstSurface,
3674     VARectangle         *dstRect)
3675 {
3676     PERF_UTILITY_AUTO(__FUNCTION__, PERF_VP, PERF_LEVEL_DDI);
3677 
3678     VAStatus            vaStatus;
3679     uint32_t            ctxType;
3680     PDDI_VP_CONTEXT     pVpCtx;
3681 
3682     VP_DDI_FUNCTION_ENTER;
3683     vaStatus = VA_STATUS_SUCCESS;
3684     DDI_CHK_NULL(pVaDrvCtx, "nullptr pVaDrvCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3685 
3686     pVpCtx = (PDDI_VP_CONTEXT)DdiMedia_GetContextFromContextID(pVaDrvCtx, vpCtxID, &ctxType);
3687     DDI_CHK_NULL(pVpCtx, "nullptr pVpCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3688 
3689     vaStatus = DdiVp_BeginPicture(pVaDrvCtx, vpCtxID, dstSurface);
3690     DDI_CHK_RET(vaStatus, "VP BeginPicture failed");
3691 
3692     //Set parameters
3693     VAProcPipelineParameterBuffer* pInputPipelineParam = (VAProcPipelineParameterBuffer*)MOS_AllocAndZeroMemory(sizeof(VAProcPipelineParameterBuffer));
3694     DDI_CHK_NULL(pInputPipelineParam, "nullptr pInputPipelineParam", VA_STATUS_ERROR_ALLOCATION_FAILED);
3695 
3696     pInputPipelineParam->surface_region = srcRect;
3697     pInputPipelineParam->output_region  = dstRect;
3698     pInputPipelineParam->surface        = srcSurface;
3699 
3700     vaStatus = DdiVp_SetProcPipelineParams(pVaDrvCtx, pVpCtx, pInputPipelineParam);
3701     if(vaStatus != VA_STATUS_SUCCESS)
3702     {
3703         MOS_FreeMemory(pInputPipelineParam);
3704         DDI_ASSERTMESSAGE("VP SetProcPipelineParams failed.");
3705         return vaStatus;
3706     }
3707 
3708     vaStatus = DdiVp_EndPicture(pVaDrvCtx, vpCtxID);
3709     if(vaStatus != VA_STATUS_SUCCESS)
3710     {
3711         MOS_FreeMemory(pInputPipelineParam);
3712         DDI_ASSERTMESSAGE("VP EndPicture failed.");
3713         return vaStatus;
3714     }
3715 
3716     MOS_FreeMemory(pInputPipelineParam);
3717     return vaStatus;
3718 }
3719 
3720 ////////////////////////////////////////////////////////////////////////////////
3721 //! \purpose Check if the format contains alpha channel
3722 //! \params
3723 //! [in]  pSurface : VpHal Surface
3724 //! [out] None
3725 //! \returns true if the format of surface contains alpha channel
3726 ////////////////////////////////////////////////////////////////////////////////
hasAlphaInSurface(PVPHAL_SURFACE pSurface)3727 static bool hasAlphaInSurface(PVPHAL_SURFACE pSurface)
3728 {
3729     if (pSurface != nullptr)
3730     {
3731         switch (pSurface->Format)
3732         {
3733             case Format_P8:            //P8_UNORM: An 8-bit color index which is used to lookup a 32-bit ARGB value in the texture palette.
3734             case Format_AI44:
3735             case Format_IA44:
3736             case Format_A8R8G8B8:
3737             case Format_A8B8G8R8:
3738             case Format_A8P8:
3739             case Format_AYUV:
3740             case Format_Y410:
3741             case Format_Y416:
3742             case Format_R10G10B10A2:
3743             case Format_B10G10R10A2:
3744                 return true;
3745             default:
3746                 return false;
3747         }
3748     }
3749 
3750     VP_DDI_ASSERTMESSAGE("No surface handle passed in.\n");
3751     return false;
3752 }
3753 
3754 ////////////////////////////////////////////////////////////////////////////////
3755 //! \purpose Set alpha blending params for VPHAL input surface
3756 //! \params
3757 //! [in]  pVpCtx : VP context
3758 //! [in]  uiSurfIndex : uiSurfIndex to the input surface array
3759 //! [in]  pPipelineParam : Pipeline paramseter from application (VAProcPipelineParameterBuffer)
3760 //! [out] None
3761 //! \returns VA_STATUS_SUCCESS if call succeeds
3762 ////////////////////////////////////////////////////////////////////////////////
3763 VAStatus
DdiVp_SetProcPipelineBlendingParams(PDDI_VP_CONTEXT pVpCtx,uint32_t uiSurfIndex,VAProcPipelineParameterBuffer * pPipelineParam)3764 DdiVp_SetProcPipelineBlendingParams(
3765     PDDI_VP_CONTEXT                         pVpCtx,
3766     uint32_t                                uiSurfIndex,
3767     VAProcPipelineParameterBuffer*          pPipelineParam)
3768 {
3769     PVPHAL_RENDER_PARAMS      pVpHalRenderParams;
3770     PVPHAL_SURFACE            pSrc;
3771     PVPHAL_SURFACE            pTarget;
3772     bool                      bGlobalAlpha;
3773     bool                      bPreMultAlpha;
3774 
3775     const VABlendState * blend_state = pPipelineParam->blend_state;
3776     bGlobalAlpha = false;
3777     bPreMultAlpha = false;
3778 
3779     VP_DDI_FUNCTION_ENTER;
3780     DDI_CHK_NULL(pVpCtx, "Null pVpCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
3781     DDI_CHK_NULL(pPipelineParam,
3782                     "Null pPipelineParam.",
3783                     VA_STATUS_ERROR_INVALID_BUFFER);
3784 
3785     // initialize
3786     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
3787     DDI_CHK_NULL(pVpHalRenderParams,
3788                     "Null pVpHalRenderParams.",
3789                     VA_STATUS_ERROR_INVALID_PARAMETER);
3790 
3791     pSrc = pVpHalRenderParams->pSrc[uiSurfIndex];
3792     DDI_CHK_NULL(pSrc, "Null pSrc.", VA_STATUS_ERROR_INVALID_SURFACE);
3793 
3794     if(nullptr == pVpHalRenderParams->pCompAlpha)
3795     {
3796         pVpHalRenderParams->pCompAlpha  = (PVPHAL_ALPHA_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_ALPHA_PARAMS));
3797         DDI_CHK_NULL(pVpHalRenderParams->pCompAlpha, "Null pCompAlpha.", VA_STATUS_ERROR_ALLOCATION_FAILED);
3798     }
3799 
3800     // So far vaapi does not have alpha fill mode API.
3801     // And for blending support, we use VPHAL_ALPHA_FILL_MODE_NONE by default.
3802     pTarget = pVpHalRenderParams->pTarget[0];
3803     DDI_CHK_NULL(pTarget, "Null pTarget.", VA_STATUS_ERROR_INVALID_SURFACE);
3804 
3805     //For surface with alpha, we need to bypass SFC that could change the output alpha.
3806     if (hasAlphaInSurface(pSrc) &&
3807         hasAlphaInSurface(pTarget))
3808     {
3809         pVpHalRenderParams->pCompAlpha->fAlpha      = 0.0f;
3810         pVpHalRenderParams->pCompAlpha->AlphaMode = VPHAL_ALPHA_FILL_MODE_SOURCE_STREAM;
3811     }
3812     else
3813     {
3814         pVpHalRenderParams->pCompAlpha->fAlpha      = 1.0f;
3815         pVpHalRenderParams->pCompAlpha->AlphaMode = VPHAL_ALPHA_FILL_MODE_NONE;
3816     }
3817 
3818     // First, no Blending
3819     if(!blend_state)
3820     {
3821         if (pSrc->pBlendingParams)
3822         {
3823             pSrc->pBlendingParams->BlendType = BLEND_NONE;
3824             pSrc->pBlendingParams->fAlpha    = 1.0;
3825         }
3826 
3827         if (pSrc->pLumaKeyParams)
3828         {
3829             pSrc->pLumaKeyParams->LumaLow    = 0;
3830             pSrc->pLumaKeyParams->LumaHigh    = 0;
3831         }
3832 
3833         return VA_STATUS_SUCCESS;
3834     }
3835 
3836     // Then, process all blending types
3837 
3838     if(blend_state->flags & VA_BLEND_GLOBAL_ALPHA)
3839         bGlobalAlpha  = true;
3840 
3841     if(blend_state->flags & VA_BLEND_PREMULTIPLIED_ALPHA)
3842         bPreMultAlpha = true;
3843 
3844     if(nullptr == pSrc->pBlendingParams)
3845     {
3846         pSrc->pBlendingParams  = (PVPHAL_BLENDING_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_BLENDING_PARAMS));
3847         DDI_CHK_NULL(pSrc->pBlendingParams, "Null pBlendingParams.", VA_STATUS_ERROR_ALLOCATION_FAILED);
3848     }
3849 
3850     ///////////////////////////////////////////////////////////////////////////////////////////////////
3851     // Surfaces contains alpha value, will be devide into premultiplied and non-premultiplied:
3852     //     For premultiplied surface, will support:
3853     //         a. per-pixel alpha blending;
3854     //         b. per-pixel plus per-plane alpha blending;
3855     //     For non-promultilied surface, will support:
3856     //         a. per-plane alpha blending;
3857     //         b. per-pixel alpha blending;
3858     //         c. NOT support per-pixel plus per-plane alpha blending;
3859     // Surfaces NOT contains alpha value, will not be devided into premultiplied or non-premultiplied:
3860     //     Will only support per-plane alpha blending.
3861     ///////////////////////////////////////////////////////////////////////////////////////////////////
3862     if(hasAlphaInSurface(pSrc))
3863     {
3864         // For premultiplied surface, we just support four blending types until now
3865         if(bPreMultAlpha && !bGlobalAlpha)            // Do per-pixel alpha blending for premultiplied surface.
3866         {
3867             pSrc->pBlendingParams->BlendType = BLEND_PARTIAL;
3868         }
3869         else if(bPreMultAlpha && bGlobalAlpha)        // Do per-pixel plus per-plane alpha blending For premultiplied surface.
3870         {
3871             if(blend_state->global_alpha < 1.0f)
3872             {
3873                 pSrc->pBlendingParams->BlendType = BLEND_CONSTANT_PARTIAL;
3874             }
3875             else
3876             {
3877                 // Optimization when per-plane alpha value is 1.0
3878                 pSrc->pBlendingParams->BlendType = BLEND_PARTIAL;
3879             }
3880             pSrc->pBlendingParams->fAlpha = blend_state->global_alpha;
3881         }
3882         else if(!bPreMultAlpha && bGlobalAlpha)       // Do per-plane alpha blending for non-premultiplied surface, not do per-pixel blending
3883         {
3884             pSrc->pBlendingParams->BlendType = BLEND_CONSTANT;
3885             pSrc->pBlendingParams->fAlpha    = blend_state->global_alpha;
3886             VP_DDI_ASSERTMESSAGE("BLEND_CONSTANT do not support alpha calculating.\n");
3887         }
3888         else if(!bPreMultAlpha && !bGlobalAlpha)      // Do per-pixel alpha blending for non-premultiplied surface
3889         {
3890             pSrc->pBlendingParams->BlendType = BLEND_SOURCE;
3891             VP_DDI_ASSERTMESSAGE("BLEND_SOURCE do not support alpha calculating.\n");
3892         }
3893 
3894         // Not support per-plane plus per-pixel alpha blending for non-premultiplied surface.
3895     }
3896     else
3897     {
3898         if(bGlobalAlpha)           // Do per-plane alpha blending for surface which not contain alpha value
3899         {
3900             if(blend_state->global_alpha < 1.0f)
3901             {
3902                 pSrc->pBlendingParams->BlendType = BLEND_CONSTANT;
3903                 pSrc->pBlendingParams->fAlpha    = blend_state->global_alpha;
3904                 VP_DDI_ASSERTMESSAGE("BLEND_CONSTANT do not support alpha calculating.\n");
3905             }
3906             else
3907             {
3908                 pSrc->pBlendingParams->BlendType = BLEND_PARTIAL;
3909                 VP_DDI_NORMALMESSAGE("Because BLEND_CONSTANT do not support alpha calculating, use BLEND_PARTIAL instead.\n");
3910             }
3911 
3912             // bPreMultAlpha should not be true for surfaces which do not contain alpha value,
3913             // but inorder to make the driver more usable, choose the most reasonable blending mode, but print out a message.
3914             if(bPreMultAlpha)
3915             {
3916                 VP_DDI_NORMALMESSAGE("Should not set VA_BLEND_PREMULTIPLIED_ALPHA for surface which do not contain alpha value.\n");
3917             }
3918         }
3919         else
3920         {
3921             pSrc->pBlendingParams->BlendType = BLEND_NONE;
3922             pSrc->pBlendingParams->fAlpha    = 1.0;
3923 
3924             // bPreMultAlpha should not be true for surfaces which do not contain alpha value,
3925             // but inorder to make the driver more usable, choose the most reasonable blending mode, but print out a message.
3926             if(bPreMultAlpha)
3927             {
3928                 VP_DDI_NORMALMESSAGE("Should not set VA_BLEND_PREMULTIPLIED_ALPHA for surface which do not contain alpha value.\n");
3929             }
3930         }
3931     }
3932 
3933     if(blend_state->flags & VA_BLEND_LUMA_KEY)
3934     {
3935         if(nullptr == pSrc->pLumaKeyParams)
3936         {
3937             pSrc->pLumaKeyParams   = (PVPHAL_LUMAKEY_PARAMS)MOS_AllocAndZeroMemory(sizeof(VPHAL_LUMAKEY_PARAMS));
3938             DDI_CHK_NULL(pSrc->pLumaKeyParams, "Null pLumaKeyParams.", VA_STATUS_ERROR_ALLOCATION_FAILED);
3939         }
3940         pSrc->pLumaKeyParams->LumaLow    = (int16_t)(pPipelineParam->blend_state->min_luma * 255);
3941         pSrc->pLumaKeyParams->LumaHigh   = (int16_t)(pPipelineParam->blend_state->max_luma * 255);
3942     }
3943 
3944     return VA_STATUS_SUCCESS;
3945 }
3946 
3947 ////////////////////////////////////////////////////////////////////////////////
3948 //! \purpose Update the future reference frames for VPHAL input surface
3949 //! \params
3950 //! [in]  pVpCtx : VP context
3951 //! [in]  pVaDrvCtx : VA Driver context
3952 //! [in]  pVpHalSrcSurf : VpHal source surface
3953 //! [in]  pPipelineParam : Pipeline parameter from application (VAProcPipelineParameterBuffer)
3954 //! [out] None
3955 //! \returns VA_STATUS_SUCCESS if call succeeds
3956 ////////////////////////////////////////////////////////////////////////////////
3957 VAStatus
DdiVp_UpdateProcPipelineFutureReferenceFrames(PDDI_VP_CONTEXT pVpCtx,VADriverContextP pVaDrvCtx,PVPHAL_SURFACE pVpHalSrcSurf,VAProcPipelineParameterBuffer * pPipelineParam)3958 DdiVp_UpdateProcPipelineFutureReferenceFrames(
3959     PDDI_VP_CONTEXT                 pVpCtx,
3960     VADriverContextP                pVaDrvCtx,
3961     PVPHAL_SURFACE                  pVpHalSrcSurf,
3962     VAProcPipelineParameterBuffer*  pPipelineParam)
3963 {
3964     PVPHAL_RENDER_PARAMS pVpHalRenderParams;
3965     PVPHAL_SURFACE       pSurface;
3966     uint32_t i;
3967 
3968     PDDI_MEDIA_CONTEXT pMediaCtx;
3969 
3970     VP_DDI_FUNCTION_ENTER;
3971 
3972     DDI_CHK_NULL(pVpCtx,         "Null pVpCtx!",         VA_STATUS_ERROR_INVALID_CONTEXT);
3973     DDI_CHK_NULL(pVaDrvCtx,      "Null pVaDrvCtx!",      VA_STATUS_ERROR_INVALID_CONTEXT);
3974     DDI_CHK_NULL(pVpHalSrcSurf,  "Null pVpHalSrcSurf!",  VA_STATUS_ERROR_INVALID_PARAMETER);
3975     DDI_CHK_NULL(pPipelineParam, "Null pPipelineParam!", VA_STATUS_ERROR_INVALID_PARAMETER);
3976 
3977     // initialize
3978     pMediaCtx = DdiMedia_GetMediaContext(pVaDrvCtx);
3979     DDI_CHK_NULL(pMediaCtx, "Null pMediaCtx!", VA_STATUS_ERROR_INVALID_CONTEXT);
3980 
3981     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
3982     DDI_CHK_NULL(pVpHalRenderParams,
3983                     "Null pVpHalRenderParams!",
3984                     VA_STATUS_ERROR_INVALID_PARAMETER);
3985 
3986     pSurface = pVpHalSrcSurf;
3987 
3988     // DDI regard backward_references as future frame, but VPHAL regard pFwdRef as future frame
3989     if (pPipelineParam->backward_references != nullptr)
3990     {
3991         for (i = 0;i < pPipelineParam->num_backward_references; i++)
3992         {
3993             PDDI_MEDIA_SURFACE pRefSurfBuffObj = nullptr;
3994             if(pSurface->pFwdRef == nullptr)
3995             {
3996                 pSurface->pFwdRef = (PVPHAL_SURFACE) MOS_AllocAndZeroMemory(sizeof(VPHAL_SURFACE));
3997                 DDI_CHK_NULL(pSurface->pFwdRef, "Null pSurface->pFwdRef!", VA_STATUS_ERROR_ALLOCATION_FAILED);
3998 
3999                 pSurface->pFwdRef->Format        = pVpHalSrcSurf->Format;
4000                 pSurface->pFwdRef->SurfType      = pVpHalSrcSurf->SurfType;
4001                 pSurface->pFwdRef->rcSrc         = pVpHalSrcSurf->rcSrc;
4002                 pSurface->pFwdRef->rcDst         = pVpHalSrcSurf->rcDst;
4003                 pSurface->pFwdRef->ColorSpace    = pVpHalSrcSurf->ColorSpace;
4004                 pSurface->pFwdRef->ExtendedGamut = pVpHalSrcSurf->ExtendedGamut;
4005                 pSurface->pFwdRef->SampleType    = pVpHalSrcSurf->SampleType;
4006                 pSurface->pFwdRef->ScalingMode   = pVpHalSrcSurf->ScalingMode;
4007                 pSurface->pFwdRef->OsResource    = pVpHalSrcSurf->OsResource;
4008                 pSurface->pFwdRef->dwWidth       = pVpHalSrcSurf->dwWidth;
4009                 pSurface->pFwdRef->dwHeight      = pVpHalSrcSurf->dwHeight;
4010                 pSurface->pFwdRef->dwPitch       = pVpHalSrcSurf->dwPitch;
4011                 pSurface->uFwdRefCount           = pPipelineParam->num_backward_references - i;
4012             }
4013             pRefSurfBuffObj = DdiMedia_GetSurfaceFromVASurfaceID(pMediaCtx, pPipelineParam->backward_references[i]);
4014             DDI_CHK_NULL(pRefSurfBuffObj,
4015                             "Null pRefSurfBuffObj!",
4016                              VA_STATUS_ERROR_INVALID_SURFACE);
4017 
4018             pSurface->pFwdRef->OsResource.bo          = pRefSurfBuffObj->bo;
4019             pSurface->pFwdRef->OsResource.Format      = VpGetFormatFromMediaFormat(pRefSurfBuffObj->format);
4020             pSurface->pFwdRef->OsResource.iWidth      = pRefSurfBuffObj->iWidth;
4021             pSurface->pFwdRef->OsResource.iHeight     = pRefSurfBuffObj->iHeight;
4022             pSurface->pFwdRef->OsResource.iPitch      = pRefSurfBuffObj->iPitch;
4023             pSurface->pFwdRef->OsResource.TileType    = VpGetTileTypeFromMediaTileType(pRefSurfBuffObj->TileType);
4024             pSurface->pFwdRef->OsResource.pGmmResInfo = pRefSurfBuffObj->pGmmResourceInfo;
4025 
4026             Mos_Solo_SetOsResource(pRefSurfBuffObj->pGmmResourceInfo, &pSurface->OsResource);
4027 
4028             pSurface->pFwdRef->FrameID                = pRefSurfBuffObj->frame_idx;
4029 
4030             pSurface = pSurface->pFwdRef;
4031         }
4032      }
4033 
4034      return VA_STATUS_SUCCESS;
4035 }
4036 
4037 ////////////////////////////////////////////////////////////////////////////////
4038 //! \purpose Update the past reference frames for VPHAL input surface
4039 //! \params
4040 //! [in]  pVpCtx : VP context
4041 //! [in]  pVaDrvCtx : VA Driver context
4042 //! [in]  pVpHalSrcSurf : VpHal source surface
4043 //! [in]  pPipelineParam : Pipeline parameter from application (VAProcPipelineParameterBuffer)
4044 //! [out] None
4045 //! \returns VA_STATUS_SUCCESS if call succeeds
4046 ////////////////////////////////////////////////////////////////////////////////
4047 VAStatus
DdiVp_UpdateProcPipelinePastReferenceFrames(PDDI_VP_CONTEXT pVpCtx,VADriverContextP pVaDrvCtx,PVPHAL_SURFACE pVpHalSrcSurf,VAProcPipelineParameterBuffer * pPipelineParam)4048 DdiVp_UpdateProcPipelinePastReferenceFrames(
4049     PDDI_VP_CONTEXT                 pVpCtx,
4050     VADriverContextP                pVaDrvCtx,
4051     PVPHAL_SURFACE                  pVpHalSrcSurf,
4052     VAProcPipelineParameterBuffer*  pPipelineParam)
4053 {
4054     PVPHAL_RENDER_PARAMS pVpHalRenderParams;
4055     PVPHAL_SURFACE       pSurface;
4056     uint32_t i;
4057 
4058     PDDI_MEDIA_CONTEXT pMediaCtx;
4059 
4060     VP_DDI_FUNCTION_ENTER;
4061 
4062     DDI_CHK_NULL(pVpCtx,         "Null pVpCtx!",         VA_STATUS_ERROR_INVALID_CONTEXT);
4063     DDI_CHK_NULL(pVaDrvCtx,      "Null pVaDrvCtx!",      VA_STATUS_ERROR_INVALID_CONTEXT);
4064     DDI_CHK_NULL(pVpHalSrcSurf,  "Null pVpHalSrcSurf!",  VA_STATUS_ERROR_INVALID_PARAMETER);
4065     DDI_CHK_NULL(pPipelineParam, "Null pPipelineParam!", VA_STATUS_ERROR_INVALID_PARAMETER);
4066 
4067     // initialize
4068     pMediaCtx = DdiMedia_GetMediaContext(pVaDrvCtx);
4069     DDI_CHK_NULL(pMediaCtx, "Null pMediaCtx!", VA_STATUS_ERROR_INVALID_CONTEXT);
4070 
4071     pVpHalRenderParams = VpGetRenderParams(pVpCtx);
4072     DDI_CHK_NULL(pVpHalRenderParams,
4073                     "Null pVpHalRenderParams!",
4074                     VA_STATUS_ERROR_INVALID_PARAMETER);
4075 
4076     pSurface = pVpHalSrcSurf;
4077 
4078     // DDI regard forward_references as past frame, but VPHAL regard pBwdRef as past frame
4079     if (pPipelineParam->forward_references  != nullptr)
4080     {
4081         for (i = 0;i < pPipelineParam->num_forward_references ; i++)
4082         {
4083             PDDI_MEDIA_SURFACE pRefSurfBuffObj = nullptr;
4084             if(pSurface->pBwdRef == nullptr)
4085             {
4086                 pSurface->pBwdRef = (PVPHAL_SURFACE) MOS_AllocAndZeroMemory(sizeof(VPHAL_SURFACE));
4087                 DDI_CHK_NULL(pSurface->pBwdRef, "Null pSurface->pBwdRef!", VA_STATUS_ERROR_ALLOCATION_FAILED);
4088 
4089                 pSurface->pBwdRef->Format        = pVpHalSrcSurf->Format;
4090                 pSurface->pBwdRef->SurfType      = pVpHalSrcSurf->SurfType;
4091                 pSurface->pBwdRef->rcSrc         = pVpHalSrcSurf->rcSrc;
4092                 pSurface->pBwdRef->rcDst         = pVpHalSrcSurf->rcDst;
4093                 pSurface->pBwdRef->ColorSpace    = pVpHalSrcSurf->ColorSpace;
4094                 pSurface->pBwdRef->ExtendedGamut = pVpHalSrcSurf->ExtendedGamut;
4095                 pSurface->pBwdRef->SampleType    = pVpHalSrcSurf->SampleType;
4096                 pSurface->pBwdRef->ScalingMode   = pVpHalSrcSurf->ScalingMode;
4097                 pSurface->pBwdRef->OsResource    = pVpHalSrcSurf->OsResource;
4098                 pSurface->pBwdRef->dwWidth       = pVpHalSrcSurf->dwWidth;
4099                 pSurface->pBwdRef->dwHeight      = pVpHalSrcSurf->dwHeight;
4100                 pSurface->pBwdRef->dwPitch       = pVpHalSrcSurf->dwPitch;
4101                 pSurface->uBwdRefCount           = pPipelineParam->num_forward_references  - i;;
4102             }
4103             pRefSurfBuffObj = DdiMedia_GetSurfaceFromVASurfaceID(pMediaCtx, pPipelineParam->forward_references [i]);
4104             DDI_CHK_NULL(pRefSurfBuffObj,
4105                             "Null pRefSurfBuffObj!",
4106                              VA_STATUS_ERROR_INVALID_SURFACE);
4107 
4108             pSurface->pBwdRef->OsResource.bo          = pRefSurfBuffObj->bo;
4109             pSurface->pBwdRef->OsResource.Format      = VpGetFormatFromMediaFormat(pRefSurfBuffObj->format);
4110             pSurface->pBwdRef->OsResource.iWidth      = pRefSurfBuffObj->iWidth;
4111             pSurface->pBwdRef->OsResource.iHeight     = pRefSurfBuffObj->iHeight;
4112             pSurface->pBwdRef->OsResource.iPitch      = pRefSurfBuffObj->iPitch;
4113             pSurface->pBwdRef->OsResource.TileType    = VpGetTileTypeFromMediaTileType(pRefSurfBuffObj->TileType);
4114             pSurface->pBwdRef->OsResource.pGmmResInfo = pRefSurfBuffObj->pGmmResourceInfo;
4115 
4116             Mos_Solo_SetOsResource(pRefSurfBuffObj->pGmmResourceInfo, &pSurface->OsResource);
4117 
4118             pSurface->pBwdRef->FrameID                = pRefSurfBuffObj->frame_idx;
4119 
4120             pSurface = pSurface->pBwdRef;
4121         }
4122      }
4123 
4124      return VA_STATUS_SUCCESS;
4125 }
4126 
4127 /** \brief Capabilities specification for the color balance filter. */
4128 static VAProcFilterCapColorBalance VpColorBalCap[] = {
4129     /** \brief Hue. */
4130     {VAProcColorBalanceHue,
4131         { PROCAMP_HUE_MIN,
4132           PROCAMP_HUE_MAX,
4133           PROCAMP_HUE_DEFAULT,
4134           PROCAMP_HUE_STEP }
4135     },
4136     /** \brief Saturation. */
4137     {VAProcColorBalanceSaturation,
4138         { PROCAMP_SATURATION_MIN,
4139           PROCAMP_SATURATION_MAX,
4140           PROCAMP_SATURATION_DEFAULT,
4141           PROCAMP_SATURATION_STEP }
4142     },
4143     /** \brief Brightness. */
4144     {VAProcColorBalanceBrightness,
4145         { PROCAMP_BRIGHTNESS_MIN,
4146           PROCAMP_BRIGHTNESS_MAX,
4147           PROCAMP_BRIGHTNESS_DEFAULT,
4148           PROCAMP_BRIGHTNESS_STEP }
4149     },
4150     /** \brief Contrast. */
4151     {VAProcColorBalanceContrast,
4152         { PROCAMP_CONTRAST_MIN,
4153           PROCAMP_CONTRAST_MAX,
4154           PROCAMP_CONTRAST_DEFAULT,
4155           PROCAMP_CONTRAST_STEP }
4156     },
4157     /** \brief Automatically adjusted contrast. */
4158     {VAProcColorBalanceAutoContrast,
4159         { 0.0F, 0.0F, 0.0F, 0.0F }
4160     }
4161 };
4162 
4163 ////////////////////////////////////////////////////////////////////////////////
4164 //! \purpose Query video processing filter capabilities
4165 //! \params
4166 //! [in]     pVaDrvCtx : VA Driver context
4167 //! [in]     pVaCtxID : VA context ID
4168 //! [in]     type :
4169 //! [inout]  filter_caps :
4170 //! [inout]  num_filter_caps :
4171 //! [out]    None
4172 //! \returns VA_STATUS_SUCCESS if call succeeds
4173 //! THIS
4174 ////////////////////////////////////////////////////////////////////////////////
4175 VAStatus
DdiVp_QueryVideoProcFilterCaps(VADriverContextP pVaDrvCtx,VAContextID pVaCtxID,int32_t type,void * filter_caps,uint32_t * num_filter_caps)4176 DdiVp_QueryVideoProcFilterCaps (
4177     VADriverContextP    pVaDrvCtx,
4178     VAContextID         pVaCtxID,
4179     int32_t             type,
4180     void                *filter_caps,
4181     uint32_t            *num_filter_caps
4182 )
4183 {
4184 #define QUERY_CAPS_ATTRIBUTE 1 /* query the actual filter caps attribute in vp module */
4185 
4186     uint32_t uCnt            = 0;   /* used for loop */
4187     uint32_t uQueryCapsNum   = 0;   /* the filter caps number queried by app layer */
4188     uint32_t uExistCapsNum   = 0;   /* the actual number of filters in vp module */
4189     uint32_t uQueryFlag      = 0;   /* QUERY_CAPS_ATTRIBUTE: search caps attribute */
4190     PDDI_MEDIA_CONTEXT mediaDrvCtx = nullptr;
4191 
4192     VP_DDI_FUNCTION_ENTER;
4193 
4194     DDI_CHK_NULL (num_filter_caps, "Null num_filter_caps.", VA_STATUS_ERROR_INVALID_PARAMETER);
4195     DDI_CHK_NULL (pVaDrvCtx, "Null pVaDrvCtx.", VA_STATUS_ERROR_INVALID_PARAMETER);
4196 
4197     mediaDrvCtx = DdiMedia_GetMediaContext(pVaDrvCtx);
4198     DDI_CHK_NULL (mediaDrvCtx, "Null mediaDrvCtx.", VA_STATUS_ERROR_INVALID_PARAMETER);
4199 
4200     if (*num_filter_caps != 0)
4201     {
4202         /* return filter caps attribute  */
4203         uQueryFlag = QUERY_CAPS_ATTRIBUTE;
4204         /* check filter_caps pointer to fill in the filter_caps */
4205         DDI_CHK_NULL (filter_caps, "Null filter_caps.", VA_STATUS_ERROR_INVALID_PARAMETER);
4206     }
4207 
4208     uQueryCapsNum = *num_filter_caps;
4209 
4210     switch (type)
4211     {
4212         /* Noise reduction filter */
4213         case VAProcFilterNoiseReduction:
4214             uExistCapsNum    = 1;
4215             /* set input filter caps number to the actual number of filters in vp module */
4216             *num_filter_caps = uExistCapsNum;
4217             /* set the actual filter caps attribute in vp module */
4218             if (uQueryFlag == QUERY_CAPS_ATTRIBUTE)
4219             {
4220                 VAProcFilterCap* baseCap     = (VAProcFilterCap*) filter_caps;
4221 
4222                 if (uQueryCapsNum < uExistCapsNum)
4223                 {
4224                     return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4225                 }
4226 
4227                 baseCap->range.min_value     = NOISEREDUCTION_MIN;
4228                 baseCap->range.max_value     = NOISEREDUCTION_MAX;
4229                 baseCap->range.default_value = NOISEREDUCTION_DEFAULT;
4230                 baseCap->range.step          = NOISEREDUCTION_STEP;
4231             }
4232             break;
4233 
4234         /* HVS Noise reduction filter */
4235         case VAProcFilterHVSNoiseReduction:
4236             if (mediaDrvCtx)
4237             {
4238                 if (MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrHVSDenoise))
4239                 {
4240                     uExistCapsNum = 4;
4241                     *num_filter_caps = uExistCapsNum;
4242                 }
4243                 else
4244                 {
4245                     uExistCapsNum = 0;
4246                     *num_filter_caps = uExistCapsNum;
4247                 }
4248             }
4249             else
4250             {
4251                 VP_DDI_ASSERTMESSAGE("mediaDrvCtx is null pointer.\n");
4252                 return VA_STATUS_ERROR_INVALID_CONTEXT;
4253             }
4254             break;
4255 
4256         /* Deinterlacing filter */
4257         case VAProcFilterDeinterlacing:
4258             uExistCapsNum = 3;
4259             /* set input filter caps number to the actual number of filters in vp module */
4260             *num_filter_caps = uExistCapsNum;
4261             /* set the actual filter caps attribute in vp module */
4262             if (uQueryFlag == QUERY_CAPS_ATTRIBUTE)
4263             {
4264                 VAProcFilterCapDeinterlacing* diCap = (VAProcFilterCapDeinterlacing*) filter_caps;
4265 
4266                 if (uQueryCapsNum < uExistCapsNum)
4267                 {
4268                     return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4269                 }
4270 
4271                 diCap[0].type = VAProcDeinterlacingBob;
4272                 diCap[1].type = VAProcDeinterlacingMotionAdaptive;
4273                 diCap[2].type = VAProcDeinterlacingMotionCompensated;
4274             }
4275             break;
4276 
4277         /* Sharpening filter. */
4278         case VAProcFilterSharpening:
4279             uExistCapsNum  = 1;
4280             /* set input filter caps number to the actual number of filters in vp module */
4281             *num_filter_caps = uExistCapsNum;
4282             /* set the actual filter caps attribute in vp module */
4283             if (uQueryFlag == QUERY_CAPS_ATTRIBUTE)
4284             {
4285                 VAProcFilterCap* baseCap;
4286 
4287                 if (uQueryCapsNum < uExistCapsNum)
4288                 {
4289                     return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4290                 }
4291 
4292                 baseCap                      = (VAProcFilterCap*) filter_caps;
4293                 baseCap->range.min_value     = EDGEENHANCEMENT_MIN;
4294                 baseCap->range.max_value     = EDGEENHANCEMENT_MAX;
4295                 baseCap->range.default_value = EDGEENHANCEMENT_DEFAULT;
4296                 baseCap->range.step          = EDGEENHANCEMENT_STEP;
4297             }
4298             break;
4299 
4300         /* Color balance parameters. */
4301         case VAProcFilterColorBalance:
4302             uExistCapsNum = sizeof (VpColorBalCap)/sizeof (VAProcFilterCapColorBalance);
4303             /* set input filter caps number to the actual number of filters in vp module */
4304             *num_filter_caps = uExistCapsNum;
4305             /* set the actual filter caps attribute in vp module */
4306             if (uQueryFlag == QUERY_CAPS_ATTRIBUTE)
4307             {
4308                 VAProcFilterCapColorBalance* ColorBalCap;
4309 
4310                 if (uQueryCapsNum < uExistCapsNum)
4311                 {
4312                     return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4313                 }
4314 
4315                 for (uCnt=0 ; uCnt<uQueryCapsNum ; uCnt++)
4316                 {
4317                     /* the actual filter caps attribute is not enough */
4318                     if (uCnt >= uExistCapsNum)
4319                     {
4320                         break;
4321                     }
4322 
4323                     ColorBalCap                         = (VAProcFilterCapColorBalance*) filter_caps+uCnt;
4324                     ColorBalCap->type                   = VpColorBalCap[uCnt].type;
4325                     ColorBalCap->range.default_value    = VpColorBalCap[uCnt].range.default_value;
4326                     ColorBalCap->range.max_value        = VpColorBalCap[uCnt].range.max_value;
4327                     ColorBalCap->range.min_value        = VpColorBalCap[uCnt].range.min_value;
4328                     ColorBalCap->range.step             = VpColorBalCap[uCnt].range.step;
4329                 }
4330             }
4331             break;
4332         case VAProcFilterSkinToneEnhancement:
4333             uExistCapsNum  = 1;
4334             *num_filter_caps = uExistCapsNum;
4335             if (uQueryFlag == QUERY_CAPS_ATTRIBUTE)
4336             {
4337                 VAProcFilterCap* baseCap     = (VAProcFilterCap *)filter_caps;
4338 
4339                 if (uQueryCapsNum < uExistCapsNum)
4340                 {
4341                     return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4342                 }
4343 
4344                 baseCap->range.min_value     = STE_MIN;
4345                 baseCap->range.max_value     = STE_MAX;
4346                 baseCap->range.default_value = STE_DEFAULT;
4347                 baseCap->range.step          = STE_STEP;
4348             }
4349             break;
4350 
4351         case VAProcFilterTotalColorCorrection:
4352             uExistCapsNum = 6;
4353             *num_filter_caps = uExistCapsNum;
4354             if (uQueryFlag == QUERY_CAPS_ATTRIBUTE)
4355             {
4356                 VAProcFilterCapTotalColorCorrection* TccCap;
4357 
4358                 if (uQueryCapsNum < uExistCapsNum)
4359                 {
4360                     return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4361                 }
4362 
4363                 for (uCnt = 0; uCnt < uQueryCapsNum; uCnt++)
4364                 {
4365                     if (uCnt >= uExistCapsNum)
4366                         break;
4367 
4368                     TccCap                      = (VAProcFilterCapTotalColorCorrection *)filter_caps + uCnt;
4369                     TccCap->type                = (VAProcTotalColorCorrectionType)((uint32_t)VAProcTotalColorCorrectionRed + uCnt);
4370                     TccCap->range.min_value     = TCC_MIN;
4371                     TccCap->range.max_value     = TCC_MAX;
4372                     TccCap->range.default_value = TCC_DEFAULT;
4373                     TccCap->range.step          = TCC_STEP;
4374                 }
4375             }
4376             break;
4377         case VAProcFilterHighDynamicRangeToneMapping:
4378         {
4379             if (mediaDrvCtx)
4380             {
4381                 if (MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrHDR))
4382                 {
4383                     uExistCapsNum = 1;
4384                     *num_filter_caps = uExistCapsNum;
4385                     if (uQueryFlag == QUERY_CAPS_ATTRIBUTE)
4386                     {
4387                         VAProcFilterCapHighDynamicRange *HdrTmCap = (VAProcFilterCapHighDynamicRange *)filter_caps;
4388 
4389                         if (uQueryCapsNum < uExistCapsNum)
4390                         {
4391                             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4392                         }
4393 
4394                         if (HdrTmCap)
4395                         {
4396                             HdrTmCap->metadata_type = VAProcHighDynamicRangeMetadataHDR10;
4397                             HdrTmCap->caps_flag = VA_TONE_MAPPING_HDR_TO_HDR | VA_TONE_MAPPING_HDR_TO_SDR | VA_TONE_MAPPING_HDR_TO_EDR;
4398                         }
4399                     }
4400                 }
4401                 else
4402                 {
4403                     uExistCapsNum = 0;
4404                     *num_filter_caps = uExistCapsNum;
4405                 }
4406             }
4407             else
4408             {
4409                 VP_DDI_ASSERTMESSAGE("mediaDrvCtx is null pointer.\n");
4410                 return VA_STATUS_ERROR_INVALID_CONTEXT;
4411             }
4412             break;
4413         }
4414 #if VA_CHECK_VERSION(1, 12, 0)
4415         case VAProcFilter3DLUT:
4416             uExistCapsNum = 0;
4417             *num_filter_caps = uExistCapsNum;
4418             /* 3DLUT is supported in VEBOX on Gen11+*/
4419             if (!MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrDisableVEBoxFeatures))
4420             {
4421                 /* 3DLUT exposes 3 kinds of caps on Gen11+*/
4422                 if (mediaDrvCtx->platform.eRenderCoreFamily >= IGFX_GEN12_CORE)
4423                 {
4424                     uExistCapsNum = 3;
4425                     *num_filter_caps = uExistCapsNum;
4426                     /* set the actual filter caps attribute in vp module */
4427                     if (uQueryFlag == QUERY_CAPS_ATTRIBUTE)
4428                     {
4429                         if (uQueryCapsNum < uExistCapsNum)
4430                         {
4431                             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4432                         }
4433                         uint32_t channel_mapping = VA_3DLUT_CHANNEL_RGB_RGB |
4434                                                    VA_3DLUT_CHANNEL_YUV_RGB |
4435                                                    VA_3DLUT_CHANNEL_VUY_RGB;
4436 
4437                         VAProcFilterCap3DLUT* lut3DCap = nullptr;
4438                         /* 17^3 */
4439                         lut3DCap                      = (VAProcFilterCap3DLUT *)filter_caps + 0;
4440                         lut3DCap->lut_size            = 17;
4441                         lut3DCap->lut_stride[0]       = 17;
4442                         lut3DCap->lut_stride[1]       = 17;
4443                         lut3DCap->lut_stride[2]       = 32;
4444                         lut3DCap->bit_depth           = 16;
4445                         lut3DCap->num_channel         = 4;
4446                         lut3DCap->channel_mapping     = channel_mapping;
4447                         /* 33^3 */
4448                         lut3DCap                      = (VAProcFilterCap3DLUT *)filter_caps + 1;
4449                         lut3DCap->lut_size            = 33;
4450                         lut3DCap->lut_stride[0]       = 33;
4451                         lut3DCap->lut_stride[1]       = 33;
4452                         lut3DCap->lut_stride[2]       = 64;
4453                         lut3DCap->bit_depth           = 16;
4454                         lut3DCap->num_channel         = 4;
4455                         lut3DCap->channel_mapping     = channel_mapping;
4456                         /* 65^3 */
4457                         lut3DCap                      = (VAProcFilterCap3DLUT *)filter_caps + 2;
4458                         lut3DCap->lut_size            = 65;
4459                         lut3DCap->lut_stride[0]       = 65;
4460                         lut3DCap->lut_stride[1]       = 65;
4461                         lut3DCap->lut_stride[2]       = 128;
4462                         lut3DCap->bit_depth           = 16;
4463                         lut3DCap->num_channel         = 4;
4464                         lut3DCap->channel_mapping     = channel_mapping;
4465                     }
4466                 }
4467             }
4468             break;
4469 #endif
4470         case VAProcFilterCount:
4471         case VAProcFilterNone:
4472             return VA_STATUS_ERROR_INVALID_VALUE;
4473 
4474         default:
4475             return VA_STATUS_ERROR_UNSUPPORTED_FILTER;
4476 
4477     }
4478 
4479     return VA_STATUS_SUCCESS;
4480 }// DdiVp_QueryVideoProcFilterCaps()
4481 
DdiVp_SetGpuPriority(PDDI_VP_CONTEXT pVpCtx,int32_t priority)4482 VAStatus DdiVp_SetGpuPriority(
4483     PDDI_VP_CONTEXT     pVpCtx,
4484     int32_t             priority
4485 )
4486 {
4487     DDI_CHK_NULL(pVpCtx, "nullptr pVpCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4488 
4489     //Set the priority for Gpu
4490     if(pVpCtx->pVpHal != nullptr)
4491     {
4492         PMOS_INTERFACE osInterface = pVpCtx->pVpHal->GetOsInterface();
4493         DDI_CHK_NULL(osInterface, "nullptr osInterface.", VA_STATUS_ERROR_ALLOCATION_FAILED);
4494         osInterface->pfnSetGpuPriority(osInterface, priority);
4495     }
4496 
4497     return VA_STATUS_SUCCESS;
4498 }
4499 
4500