1 /*
2 * Copyright (c) 2021, 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     ddi_encode_functions.cpp
24 //! \brief    ddi encode functions implementaion.
25 //!
26 #include "ddi_encode_functions.h"
27 #include "media_libva_common_next.h"
28 #include "ddi_encode_hevc_specific.h"
29 #include "ddi_register_components_specific.h"
30 #include "codechal_memdecomp.h"
31 #include "media_interfaces_codechal_next.h"
32 #include "media_interfaces_mmd.h"
33 #include "media_libva_interface_next.h"
34 
CreateConfig(VADriverContextP ctx,VAProfile profile,VAEntrypoint entrypoint,VAConfigAttrib * attribList,int32_t numAttribs,VAConfigID * configId)35 VAStatus DdiEncodeFunctions::CreateConfig (
36     VADriverContextP  ctx,
37     VAProfile         profile,
38     VAEntrypoint      entrypoint,
39     VAConfigAttrib   *attribList,
40     int32_t           numAttribs,
41     VAConfigID       *configId)
42 {
43     VAStatus status = VA_STATUS_SUCCESS;
44     DDI_CODEC_CHK_NULL(configId,   "nullptr configId",   VA_STATUS_ERROR_INVALID_PARAMETER);
45 
46     PDDI_MEDIA_CONTEXT mediaCtx = GetMediaContext(ctx);
47     DDI_CODEC_CHK_NULL(mediaCtx,             "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
48     DDI_CODEC_CHK_NULL(mediaCtx->m_capsNext, "nullptr m_caps",   VA_STATUS_ERROR_INVALID_PARAMETER);
49     DDI_CODEC_CHK_NULL(mediaCtx->m_capsNext->m_capsTable, "nullptr m_capsTable",   VA_STATUS_ERROR_INVALID_PARAMETER);
50 
51     status = mediaCtx->m_capsNext->CreateConfig(profile, entrypoint, attribList, numAttribs, configId);
52     DDI_CODEC_CHK_RET(status, "Create common config failed");
53 
54     uint32_t rcMode = VA_RC_CQP;
55     uint32_t feiFunction = 0;
56 
57     if((entrypoint == VAEntrypointStats) || (entrypoint == VAEntrypointEncPicture))
58     {
59         rcMode = VA_RC_NONE;
60     }
61 
62     for(int i = 0; i < numAttribs; i++)
63     {
64         if(attribList[i].type == VAConfigAttribFEIFunctionType)
65         {
66             feiFunction = attribList[i].value;
67         }
68 
69         if(attribList[i].type == VAConfigAttribRateControl && attribList[i].value != VA_RC_MB)
70         {
71             rcMode = attribList[i].value;
72         }
73     }
74 
75     auto configList = mediaCtx->m_capsNext->GetConfigList();
76     DDI_CODEC_CHK_NULL(configList, "Get configList failed", VA_STATUS_ERROR_INVALID_PARAMETER);
77 
78     for(int i = 0; i < configList->size(); i++)
79     {
80         if((configList->at(i).profile == profile)        &&
81            (configList->at(i).entrypoint == entrypoint))
82         {
83             if((rcMode      == configList->at(i).componentData.data.rcMode)       &&
84                (feiFunction == configList->at(i).componentData.data.feiFunction))
85             {
86                 uint32_t curConfigID = ADD_CONFIG_ID_ENC_OFFSET(i);
87                 if(!mediaCtx->m_capsNext->m_capsTable->IsEncConfigId(curConfigID))
88                 {
89                     DDI_CODEC_ASSERTMESSAGE("DDI: Invalid configID.");
90                     return VA_STATUS_ERROR_INVALID_CONFIG;
91                 }
92 
93                 *configId = curConfigID;
94                 return VA_STATUS_SUCCESS;
95             }
96         }
97     }
98 
99     *configId = 0xFFFFFFFF;
100     return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
101 }
102 
CreateContext(VADriverContextP ctx,VAConfigID configId,int32_t pictureWidth,int32_t pictureHeight,int32_t flag,VASurfaceID * renderTargets,int32_t renderTargetsNum,VAContextID * context)103 VAStatus DdiEncodeFunctions::CreateContext (
104     VADriverContextP  ctx,
105     VAConfigID        configId,
106     int32_t           pictureWidth,
107     int32_t           pictureHeight,
108     int32_t           flag,
109     VASurfaceID       *renderTargets,
110     int32_t           renderTargetsNum,
111     VAContextID       *context)
112 {
113     PERF_UTILITY_AUTO(__FUNCTION__, PERF_ENCODE, PERF_LEVEL_DDI);
114 
115     DDI_CODEC_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
116     DDI_CODEC_CHK_NULL(ctx->pDriverData, "nullptr ctx->pDriverData", VA_STATUS_ERROR_INVALID_CONTEXT);
117 
118     PDDI_MEDIA_CONTEXT mediaCtx = GetMediaContext(ctx);
119     DDI_CODEC_CHK_NULL(mediaCtx->m_capsNext, "nullptr m_capsNext", VA_STATUS_ERROR_INVALID_CONTEXT);
120     DDI_CODEC_CHK_NULL(mediaCtx->m_capsNext->m_capsTable, "nullptr m_capsTable", VA_STATUS_ERROR_INVALID_CONTEXT);
121 
122     ConfigLinux*  configItem = nullptr;
123     configItem = mediaCtx->m_capsNext->m_capsTable->QueryConfigItemFromIndex(configId);
124     DDI_CODEC_CHK_NULL(configItem, "Invalid config id!", VA_STATUS_ERROR_INVALID_PARAMETER);
125 
126     if (renderTargetsNum > DDI_MEDIA_MAX_SURFACE_NUMBER_CONTEXT)
127     {
128         return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
129     }
130 
131     encode::DdiEncodeBase *ddiEncode = DdiEncodeFactory::Create(ComponentInfo{configItem->profile, configItem->entrypoint});
132     DDI_CODEC_CHK_NULL(ddiEncode, "nullptr ddiEncode", VA_STATUS_ERROR_UNIMPLEMENTED);
133     VAStatus vaStatus = ddiEncode->CheckEncodeResolution(
134         mediaCtx->m_capsNext,
135         configId,
136         pictureWidth,
137         pictureHeight);
138     if (vaStatus != VA_STATUS_SUCCESS)
139     {
140         return VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
141     }
142 
143     // first create encoder context
144     ddiEncode->m_encodeCtx = (decltype(ddiEncode->m_encodeCtx))MOS_AllocAndZeroMemory(sizeof(encode::DDI_ENCODE_CONTEXT));
145     DDI_CODEC_CHK_NULL(ddiEncode->m_encodeCtx, "nullptr ddiEncode->m_encodeCtx", VA_STATUS_ERROR_ALLOCATION_FAILED);
146     DDI_CODEC_VERBOSEMESSAGE(" DdiEncodeFunctions::CreateContext encCtx %p ddiEncode %p", ddiEncode->m_encodeCtx, ddiEncode);
147 
148     encode::PDDI_ENCODE_CONTEXT encCtx = ddiEncode->m_encodeCtx;
149     encCtx->m_encode           = ddiEncode;
150 
151     //initialize DDI level cp interface
152     MOS_CONTEXT mosCtx      = {};
153     encCtx->pCpDdiInterfaceNext = CreateDdiCpNext(&mosCtx);
154     if (nullptr == encCtx->pCpDdiInterfaceNext)
155     {
156         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
157         CleanUp(encCtx);
158         return vaStatus;
159     }
160 
161     // Get the buf manager for codechal create
162     mosCtx.bufmgr          = mediaCtx->pDrmBufMgr;
163     mosCtx.fd              = mediaCtx->fd;
164     mosCtx.iDeviceId       = mediaCtx->iDeviceId;
165     mosCtx.m_skuTable      = mediaCtx->SkuTable;
166     mosCtx.m_waTable       = mediaCtx->WaTable;
167     mosCtx.m_gtSystemInfo  = *mediaCtx->pGtSystemInfo;
168     mosCtx.m_platform      = mediaCtx->platform;
169 
170     mosCtx.ppMediaMemDecompState = &mediaCtx->pMediaMemDecompState;
171     mosCtx.pfnMemoryDecompress   = mediaCtx->pfnMemoryDecompress;
172     mosCtx.pfnMediaMemoryCopy    = mediaCtx->pfnMediaMemoryCopy;
173     mosCtx.pfnMediaMemoryCopy2D  = mediaCtx->pfnMediaMemoryCopy2D;
174     mosCtx.ppMediaCopyState      = &mediaCtx->pMediaCopyState;
175     mosCtx.m_gtSystemInfo        = *mediaCtx->pGtSystemInfo;
176     mosCtx.m_auxTableMgr         = mediaCtx->m_auxTableMgr;
177     mosCtx.pGmmClientContext     = mediaCtx->pGmmClientContext;
178 
179     mosCtx.m_osDeviceContext = mediaCtx->m_osDeviceContext;
180     mosCtx.m_apoMosEnabled   = true;
181     mosCtx.m_userSettingPtr = mediaCtx->m_userSettingPtr;
182 
183     mosCtx.pPerfData = (PERF_DATA *)MOS_AllocAndZeroMemory(sizeof(PERF_DATA));
184     if (nullptr == mosCtx.pPerfData)
185     {
186         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
187         CleanUp(encCtx);
188         return vaStatus;
189     }
190 
191     if (configItem->entrypoint == VAEntrypointEncSlice)
192     {
193         encCtx->bVdencActive = true;
194     }
195 
196     encCtx->vaEntrypoint  = configItem->entrypoint;
197     encCtx->vaProfile     = configItem->profile;
198     encCtx->uiRCMethod    = configItem->componentData.data.rcMode;
199     encCtx->wModeType     = ddiEncode->GetEncodeCodecMode(configItem->profile, configItem->entrypoint);
200     encCtx->codecFunction = ddiEncode->GetEncodeCodecFunction(configItem->profile, configItem->entrypoint, encCtx->bVdencActive);
201 
202     CODECHAL_STANDARD_INFO standardInfo;
203     MOS_ZeroMemory(&standardInfo, sizeof(CODECHAL_STANDARD_INFO));
204     standardInfo.CodecFunction = encCtx->codecFunction;
205     standardInfo.Mode          = encCtx->wModeType;
206     Codechal *pCodecHal        = CodechalDeviceNext::CreateFactory(
207         nullptr,
208         &mosCtx,
209         &standardInfo,
210         nullptr);
211     if (pCodecHal == nullptr)
212     {
213         // add anything necessary here to free the resource
214         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
215         CleanUp(encCtx);
216         return vaStatus;
217     }
218 
219     encCtx->pCodecHal = pCodecHal;
220 
221     // Setup some initial data
222     encCtx->dworiFrameWidth  = pictureWidth;
223     encCtx->dworiFrameHeight = pictureHeight;
224     encCtx->wPicWidthInMB    = (uint16_t)(DDI_CODEC_NUM_MACROBLOCKS_WIDTH(pictureWidth));
225     encCtx->wPicHeightInMB   = (uint16_t)(DDI_CODEC_NUM_MACROBLOCKS_HEIGHT(pictureHeight));
226     encCtx->dwFrameWidth     = encCtx->wPicWidthInMB * CODECHAL_MACROBLOCK_WIDTH;
227     encCtx->dwFrameHeight    = encCtx->wPicHeightInMB * CODECHAL_MACROBLOCK_HEIGHT;
228     //recoder old resolution for dynamic resolution  change
229     encCtx->wContextPicWidthInMB  = encCtx->wPicWidthInMB;
230     encCtx->wContextPicHeightInMB = encCtx->wPicHeightInMB;
231     encCtx->wOriPicWidthInMB      = encCtx->wPicWidthInMB;
232     encCtx->wOriPicHeightInMB     = encCtx->wPicHeightInMB;
233     encCtx->targetUsage           = TARGETUSAGE_RT_SPEED;
234     // Attach PMEDIDA_DRIVER_CONTEXT
235     encCtx->pMediaCtx = mediaCtx;
236 
237     encCtx->pCpDdiInterfaceNext->SetCpFlags(flag);
238     encCtx->pCpDdiInterfaceNext->SetCpParams(CP_TYPE_NONE, encCtx->m_encode->m_codechalSettings);
239 
240     vaStatus = encCtx->m_encode->ContextInitialize(encCtx->m_encode->m_codechalSettings);
241 
242     if (vaStatus != VA_STATUS_SUCCESS)
243     {
244         CleanUp(encCtx);
245         return vaStatus;
246     }
247 
248     MOS_STATUS eStatus = pCodecHal->Allocate(encCtx->m_encode->m_codechalSettings);
249 
250 #ifdef _MMC_SUPPORTED
251     PMOS_INTERFACE osInterface = pCodecHal->GetOsInterface();
252     if (osInterface != nullptr &&
253         !osInterface->apoMosEnabled &&
254         MEDIA_IS_SKU(osInterface->pfnGetSkuTable(osInterface), FtrMemoryCompression) &&
255         !mediaCtx->pMediaMemDecompState)
256     {
257         mediaCtx->pMediaMemDecompState =
258             static_cast<MediaMemDecompState *>(MmdDevice::CreateFactory(&mosCtx));
259     }
260 #endif
261 
262     if (eStatus != MOS_STATUS_SUCCESS)
263     {
264         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
265         CleanUp(encCtx);
266         return vaStatus;
267     }
268 
269     vaStatus = encCtx->m_encode->InitCompBuffer();
270     if (vaStatus != VA_STATUS_SUCCESS)
271     {
272         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
273         CleanUp(encCtx);
274         return vaStatus;
275     }
276 
277     // register the render target surfaces for this encoder instance
278     // This is a must as driver has the constraint, 127 surfaces per context
279     for (int32_t i = 0; i < renderTargetsNum; i++)
280     {
281         DDI_MEDIA_SURFACE *surface;
282 
283         surface = MediaLibvaCommonNext::GetSurfaceFromVASurfaceID(mediaCtx, renderTargets[i]);
284         if (nullptr == surface)
285         {
286             DDI_CODEC_ASSERTMESSAGE("DDI: invalid render target %d in vpgEncodeCreateContext.", i);
287             vaStatus = VA_STATUS_ERROR_INVALID_SURFACE;
288             CleanUp(encCtx);
289             return vaStatus;
290         }
291         encCtx->RTtbl.pRT[i] = surface;
292         encCtx->RTtbl.iNumRenderTargets++;
293     }
294 
295     // convert PDDI_ENCODE_CONTEXT to VAContextID
296     MosUtilities::MosLockMutex(&mediaCtx->EncoderMutex);
297     PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT vaContextHeapElmt = MediaLibvaUtilNext::DdiAllocPVAContextFromHeap(mediaCtx->pEncoderCtxHeap);
298     if (nullptr == vaContextHeapElmt)
299     {
300         MosUtilities::MosUnlockMutex(&mediaCtx->EncoderMutex);
301         vaStatus = VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
302         CleanUp(encCtx);
303         return vaStatus;
304     }
305 
306     vaContextHeapElmt->pVaContext = (void *)encCtx;
307     mediaCtx->uiNumEncoders++;
308     *context = (VAContextID)(vaContextHeapElmt->uiVaContextID + DDI_MEDIA_SOFTLET_VACONTEXTID_ENCODER_OFFSET);
309     MosUtilities::MosUnlockMutex(&mediaCtx->EncoderMutex);
310     DDI_CODEC_VERBOSEMESSAGE(" DdiEncodeFunctions::CreateContext ctx %p  *context %d", ctx, *context);
311     return vaStatus;
312 }
313 
DestroyContext(VADriverContextP ctx,VAContextID context)314 VAStatus DdiEncodeFunctions::DestroyContext (
315     VADriverContextP  ctx,
316     VAContextID       context)
317 {
318     DDI_CODEC_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
319     DDI_CODEC_CHK_NULL(ctx->pDriverData, "nullptr ctx->pDriverData", VA_STATUS_ERROR_INVALID_CONTEXT);
320 
321     PDDI_MEDIA_CONTEXT mediaCtx = GetMediaContext(ctx);
322     DDI_CODEC_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
323 
324     // assume the VAContextID is encoder ID
325     uint32_t        ctxType                   = 0;
326     encode::PDDI_ENCODE_CONTEXT encCtx  = (decltype(encCtx))MediaLibvaCommonNext::GetContextFromContextID(ctx, context, &ctxType);
327     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
328     DDI_CODEC_CHK_NULL(encCtx->pCodecHal, "nullptr encCtx->pCodecHal", VA_STATUS_ERROR_INVALID_CONTEXT);
329 
330     Codechal *codecHal = encCtx->pCodecHal;
331 
332     if (nullptr != encCtx->m_encode)
333     {
334         encCtx->m_encode->FreeCompBuffer();
335         if(nullptr != encCtx->m_encode->m_codechalSettings)
336         {
337             MOS_Delete(encCtx->m_encode->m_codechalSettings);
338             encCtx->m_encode->m_codechalSettings = nullptr;
339         }
340     }
341 
342     if (codecHal->GetOsInterface() && codecHal->GetOsInterface()->pOsContext)
343     {
344         MOS_FreeMemory(codecHal->GetOsInterface()->pOsContext->pPerfData);
345         codecHal->GetOsInterface()->pOsContext->pPerfData = nullptr;
346     }
347 
348     // destroy codechal
349     codecHal->Destroy();
350     MOS_Delete(codecHal);
351 
352     if (encCtx->pCpDdiInterfaceNext)
353     {
354         MOS_Delete(encCtx->pCpDdiInterfaceNext);
355     }
356 
357     if (nullptr != encCtx->m_encode)
358     {
359         MOS_Delete(encCtx->m_encode);
360         encCtx->m_encode = nullptr;
361     }
362 
363     MOS_FreeMemory(encCtx);
364     encCtx = nullptr;
365 
366     uint32_t encIndex = (uint32_t)context;
367     encIndex &= DDI_MEDIA_MASK_VACONTEXTID;
368 
369     MosUtilities::MosLockMutex(&mediaCtx->EncoderMutex);
370     MediaLibvaUtilNext::DdiReleasePVAContextFromHeap(mediaCtx->pEncoderCtxHeap, encIndex);
371     mediaCtx->uiNumEncoders--;
372     MosUtilities::MosUnlockMutex(&mediaCtx->EncoderMutex);
373     return VA_STATUS_SUCCESS;
374 }
375 
CreateBuffer(VADriverContextP ctx,VAContextID context,VABufferType type,uint32_t size,uint32_t elementsNum,void * data,VABufferID * bufId)376 VAStatus DdiEncodeFunctions::CreateBuffer (
377     VADriverContextP  ctx,
378     VAContextID       context,
379     VABufferType      type,
380     uint32_t          size,
381     uint32_t          elementsNum,
382     void              *data,
383     VABufferID        *bufId)
384 {
385     DDI_CODEC_CHK_NULL(ctx, "nullptr context!", VA_STATUS_ERROR_INVALID_CONTEXT);
386 
387     uint32_t        ctxType                   = 0;
388     encode::PDDI_ENCODE_CONTEXT encCtx = (decltype(encCtx))MediaLibvaCommonNext::GetContextFromContextID(ctx, context, &ctxType);
389     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
390     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
391 
392     VAStatus vaStatus = encCtx->m_encode->CreateBuffer(ctx, type, size, elementsNum, data, bufId);
393 
394     return VA_STATUS_SUCCESS;
395 }
396 
MapBufferInternal(DDI_MEDIA_CONTEXT * mediaCtx,VABufferID buf_id,void ** pbuf,uint32_t flag)397 VAStatus DdiEncodeFunctions::MapBufferInternal(
398     DDI_MEDIA_CONTEXT   *mediaCtx,
399     VABufferID          buf_id,
400     void              **pbuf,
401     uint32_t            flag
402 )
403 {
404     DDI_CODEC_FUNC_ENTER;
405     MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_START, &buf_id, sizeof(buf_id), &flag, sizeof(flag));
406 
407     DDI_CODEC_CHK_NULL(pbuf,  "nullptr pbuf",    VA_STATUS_ERROR_INVALID_PARAMETER);
408 
409     DDI_CODEC_CHK_NULL(mediaCtx,              "nullptr mediaCtx",              VA_STATUS_ERROR_INVALID_CONTEXT);
410 
411     DDI_CODEC_CHK_NULL(mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
412     DDI_CODEC_CHK_LESS((uint32_t)buf_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid bufferId", VA_STATUS_ERROR_INVALID_CONTEXT);
413 
414     DDI_MEDIA_BUFFER   *buf     = MediaLibvaCommonNext::GetBufferFromVABufferID(mediaCtx, buf_id);
415     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_BUFFER);
416 
417     // The context is nullptr when the buffer is created from DeriveImage
418     // So doesn't need to check the context for all cases
419     // Only check the context in dec/enc mode
420     VAStatus                 vaStatus  = VA_STATUS_SUCCESS;
421     uint32_t                 ctxType = MediaLibvaCommonNext::GetCtxTypeFromVABufferID(mediaCtx, buf_id);
422     void                     *ctxPtr = nullptr;
423     DDI_CODEC_COM_BUFFER_MGR *bufMgr = nullptr;
424     encode::PDDI_ENCODE_CONTEXT      encCtx  = nullptr;
425 
426     ctxPtr = MediaLibvaCommonNext::GetCtxFromVABufferID(mediaCtx, buf_id);
427     DDI_CODEC_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
428 
429     encCtx = encode::GetEncContextFromPVOID(ctxPtr);
430     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
431     bufMgr = &(encCtx->BufMgr);
432 
433     MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_INFO, &ctxType, sizeof(ctxType), &buf->uiType, sizeof(uint32_t));
434     switch ((int32_t)buf->uiType)
435     {
436         case VAEncCodedBufferType:
437             DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
438 
439             if( CodedBufferExistInStatusReport( encCtx, buf ) )
440             {
441                 vaStatus = StatusReport(encCtx, buf, pbuf);
442                 MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
443                 return vaStatus;
444             }
445             // so far a coded buffer that has NOT been added into status report is skipped frame in non-CP case
446             // but this can change in future if new usage models come up
447             encCtx->BufMgr.pCodedBufferSegment->buf  = MediaLibvaUtilNext::LockBuffer(buf, flag);
448             encCtx->BufMgr.pCodedBufferSegment->size = buf->iSize;
449             *pbuf =  encCtx->BufMgr.pCodedBufferSegment;
450 
451             break;
452 
453         case VAStatsStatisticsBufferType:
454         case VAStatsStatisticsBottomFieldBufferType:
455         case VAStatsMVBufferType:
456             {
457                 DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT)
458                 encode::DDI_ENCODE_PRE_ENC_BUFFER_TYPE idx = (buf->uiType == VAStatsMVBufferType) ? encode::PRE_ENC_BUFFER_TYPE_MVDATA :
459                                                     ((buf->uiType == VAStatsStatisticsBufferType)   ? encode::PRE_ENC_BUFFER_TYPE_STATS
460                                                                                                           : encode::PRE_ENC_BUFFER_TYPE_STATS_BOT);
461                 if((encCtx->codecFunction == CODECHAL_FUNCTION_FEI_PRE_ENC) && PreEncBufferExistInStatusReport( encCtx, buf, idx))
462                 {
463                     vaStatus = PreEncStatusReport(encCtx, buf, pbuf);
464                     MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
465                     return vaStatus;
466                 }
467                 if(buf->bo)
468                 {
469                     *pbuf = MediaLibvaUtilNext::LockBuffer(buf, flag);
470                 }
471                 break;
472             }
473         case VAStatsMVPredictorBufferType:
474         case VAEncFEIMBControlBufferType:
475         case VAEncFEIMVPredictorBufferType:
476         case VAEncQPBufferType:
477             if(buf->bo)
478             {
479                 *pbuf = MediaLibvaUtilNext::LockBuffer(buf, flag);
480             }
481             break;
482         case VAEncFEIMVBufferType:
483         case VAEncFEIMBCodeBufferType:
484         case VAEncFEICURecordBufferType:
485         case VAEncFEICTBCmdBufferType:
486         case VAEncFEIDistortionBufferType:
487             {
488                 DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT)
489                 if(encCtx->wModeType == CODECHAL_ENCODE_MODE_AVC)
490                 {
491                     CodecEncodeAvcFeiPicParams *feiPicParams = (CodecEncodeAvcFeiPicParams *)encCtx->pFeiPicParams;
492 
493                     encode::DDI_ENCODE_FEI_ENC_BUFFER_TYPE idx = (buf->uiType == VAEncFEIMVBufferType) ? encode::FEI_ENC_BUFFER_TYPE_MVDATA :
494                                        ((buf->uiType == VAEncFEIMBCodeBufferType) ? encode::FEI_ENC_BUFFER_TYPE_MBCODE :
495                                                                                         encode::FEI_ENC_BUFFER_TYPE_DISTORTION);
496                     if((feiPicParams != nullptr) && (encCtx->codecFunction == CODECHAL_FUNCTION_FEI_ENC) && EncBufferExistInStatusReport(encCtx, buf, idx))
497                     {
498                         vaStatus = EncStatusReport(encCtx, buf, pbuf);
499                         MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
500                         return vaStatus;
501                     }
502                 }
503                 else if(encCtx->wModeType == CODECHAL_ENCODE_MODE_HEVC)
504 
505                 {
506                     CodecEncodeHevcFeiPicParams *feiPicParams = (CodecEncodeHevcFeiPicParams *)encCtx->pFeiPicParams;
507                     encode::DDI_ENCODE_FEI_ENC_BUFFER_TYPE idx = (buf->uiType == VAEncFEICTBCmdBufferType) ? encode::FEI_ENC_BUFFER_TYPE_MVDATA   :
508                                                       ((buf->uiType == VAEncFEICURecordBufferType) ? encode::FEI_ENC_BUFFER_TYPE_MBCODE :
509                                                                                                       encode::FEI_ENC_BUFFER_TYPE_DISTORTION);
510                     if((feiPicParams != nullptr) && (encCtx->codecFunction == CODECHAL_FUNCTION_FEI_ENC) && EncBufferExistInStatusReport( encCtx, buf, idx))
511                     {
512                         vaStatus = EncStatusReport(encCtx, buf, pbuf);
513                         MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
514                         return vaStatus;
515                     }
516                 }
517                 if(buf->bo)
518                 {
519                     *pbuf = MediaLibvaUtilNext::LockBuffer(buf, flag);
520                 }
521             }
522             break;
523         case VAStatsStatisticsParameterBufferType:
524             *pbuf = (void *)(buf->pData + buf->uiOffset);
525             break;
526         case VAEncMacroblockMapBufferType:
527             MosUtilities::MosLockMutex(&mediaCtx->BufferMutex);
528             *pbuf = MediaLibvaUtilNext::LockBuffer(buf, flag);
529             MosUtilities::MosUnlockMutex(&mediaCtx->BufferMutex);
530             MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
531             if (nullptr == (*pbuf))
532             {
533                 return VA_STATUS_ERROR_OPERATION_FAILED;
534             }
535             else
536             {
537                 return VA_STATUS_SUCCESS;
538             }
539             break;
540 
541         case VAProbabilityBufferType:
542             *pbuf = (void *)(buf->pData + buf->uiOffset);
543 
544             break;
545 
546         case VAEncMacroblockDisableSkipMapBufferType:
547             if(buf->bo)
548             {
549                 *pbuf = MediaLibvaUtilNext::LockBuffer(buf, flag);
550             }
551             break;
552 
553         case VAImageBufferType:
554         default:
555             vaStatus = DdiMediaFunctions::MapBufferInternal(mediaCtx, buf_id, pbuf, MOS_LOCKFLAG_READONLY | MOS_LOCKFLAG_WRITEONLY);
556             break;
557     }
558 
559     MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
560     return vaStatus;
561 }
562 
UnmapBuffer(DDI_MEDIA_CONTEXT * mediaCtx,VABufferID buf_id)563 VAStatus DdiEncodeFunctions::UnmapBuffer (
564     DDI_MEDIA_CONTEXT   *mediaCtx,
565     VABufferID          buf_id
566 )
567 {
568     DDI_CODEC_FUNC_ENTER;
569     MOS_TraceEventExt(EVENT_VA_UNMAP, EVENT_TYPE_START, &buf_id, sizeof(buf_id), nullptr, 0);
570 
571     DDI_CODEC_CHK_NULL(mediaCtx,               "nullptr mediaCtx",               VA_STATUS_ERROR_INVALID_CONTEXT);
572 
573     DDI_CODEC_CHK_NULL( mediaCtx->pBufferHeap, "nullptr  mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
574     DDI_CODEC_CHK_LESS((uint32_t)buf_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid buf_id", VA_STATUS_ERROR_INVALID_BUFFER);
575 
576     DDI_MEDIA_BUFFER   *buf     = MediaLibvaCommonNext::GetBufferFromVABufferID(mediaCtx,  buf_id);
577     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_BUFFER);
578 
579     // The context is nullptr when the buffer is created from DeriveImage
580     // So doesn't need to check the context for all cases
581     // Only check the context in enc mode
582     void     *ctxPtr = nullptr;
583     uint32_t ctxType = MediaLibvaCommonNext::GetCtxTypeFromVABufferID(mediaCtx, buf_id);
584     DDI_CODEC_COM_BUFFER_MGR *bufMgr = nullptr;
585     encode::PDDI_ENCODE_CONTEXT encCtx = nullptr;
586 
587     ctxPtr = MediaLibvaCommonNext::GetCtxFromVABufferID(mediaCtx, buf_id);
588     DDI_CODEC_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
589 
590     encCtx = encode::GetEncContextFromPVOID(ctxPtr);
591     bufMgr = &(encCtx->BufMgr);
592 
593     switch ((int32_t)buf->uiType)
594     {
595         case VASliceDataBufferType:
596         case VAProtectedSliceDataBufferType:
597         case VABitPlaneBufferType:
598             break;
599         case VAEncCodedBufferType:
600         case VAStatsStatisticsBufferType:
601         case VAStatsStatisticsBottomFieldBufferType:
602         case VAStatsMVBufferType:
603         case VAStatsMVPredictorBufferType:
604         case VAEncFEIMBControlBufferType:
605         case VAEncFEIMVPredictorBufferType:
606         case VAEncFEIMVBufferType:
607         case VAEncFEIMBCodeBufferType:
608         case VAEncFEICURecordBufferType:
609         case VAEncFEICTBCmdBufferType:
610         case VAEncFEIDistortionBufferType:
611         case VAEncQPBufferType:
612         case VAEncMacroblockDisableSkipMapBufferType:
613             if(buf->bo)
614             {
615                 MediaLibvaUtilNext::UnlockBuffer(buf);
616             }
617             break;
618 
619         default:
620             DdiMediaFunctions::UnmapBuffer(mediaCtx, buf_id);
621             break;
622     }
623 
624     MOS_TraceEventExt(EVENT_VA_UNMAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
625     return VA_STATUS_SUCCESS;
626 }
627 
DestroyBuffer(DDI_MEDIA_CONTEXT * mediaCtx,VABufferID buffer_id)628 VAStatus DdiEncodeFunctions::DestroyBuffer (
629     DDI_MEDIA_CONTEXT  *mediaCtx,
630     VABufferID          buffer_id
631 )
632 {
633     DDI_CODEC_FUNC_ENTER;
634     MOS_TraceEventExt(EVENT_VA_FREE_BUFFER, EVENT_TYPE_START, &buffer_id, sizeof(buffer_id), nullptr, 0);
635 
636     DDI_CODEC_CHK_NULL(mediaCtx,              "nullptr mediaCtx",              VA_STATUS_ERROR_INVALID_CONTEXT);
637 
638     DDI_CODEC_CHK_NULL(mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
639     DDI_CODEC_CHK_LESS((uint32_t)buffer_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid bufferId", VA_STATUS_ERROR_INVALID_CONTEXT);
640 
641     DDI_MEDIA_BUFFER   *buf     = MediaLibvaCommonNext::GetBufferFromVABufferID(mediaCtx,  buffer_id);
642     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_BUFFER);
643 
644     void     *ctxPtr = MediaLibvaCommonNext::GetCtxFromVABufferID(mediaCtx,     buffer_id);
645     uint32_t ctxType = MediaLibvaCommonNext::GetCtxTypeFromVABufferID(mediaCtx, buffer_id);
646 
647     DDI_CODEC_COM_BUFFER_MGR *bufMgr  = nullptr;
648     encode::PDDI_ENCODE_CONTEXT encCtx  = nullptr;
649 
650     encCtx = encode::GetEncContextFromPVOID(ctxPtr);
651     bufMgr = &(encCtx->BufMgr);
652 
653     switch ((int32_t)buf->uiType)
654     {
655         case VAImageBufferType:
656             if(buf->format == Media_Format_CPU)
657             {
658                 MOS_DeleteArray(buf->pData);
659             }
660             else
661             {
662                 MediaLibvaUtilNext::UnRefBufObjInMediaBuffer(buf);
663 
664                 if (buf->uiExportcount) {
665                     buf->bPostponedBufFree = true;
666                     MOS_TraceEventExt(EVENT_VA_FREE_BUFFER, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
667                     return VA_STATUS_SUCCESS;
668                 }
669             }
670             break;
671         case VAIQMatrixBufferType:
672         case VAHuffmanTableBufferType:
673         case VAEncSliceParameterBufferType:
674         case VAEncPictureParameterBufferType:
675         case VAEncSequenceParameterBufferType:
676         case VAEncPackedHeaderDataBufferType:
677         case VAEncPackedHeaderParameterBufferType:
678             MOS_DeleteArray(buf->pData);
679             break;
680         case VAEncMacroblockMapBufferType:
681             MediaLibvaUtilNext::FreeBuffer(buf);
682             break;
683 #ifdef ENABLE_ENC_UNLIMITED_OUTPUT
684         case VAEncCodedBufferType:
685             if(nullptr == encCtx)
686             {
687                 encCtx = encode::GetEncContextFromPVOID(ctxPtr);
688                 if(nullptr == encCtx)
689                     return VA_STATUS_ERROR_INVALID_CONTEXT;
690             }
691             MediaLibvaUtilNext::FreeBuffer(buf);
692             break;
693 #endif
694         case VAStatsStatisticsParameterBufferType:
695             MOS_DeleteArray(buf->pData);
696             break;
697         case VAStatsStatisticsBufferType:
698         case VAStatsStatisticsBottomFieldBufferType:
699         case VAStatsMVBufferType:
700             {
701                 if(nullptr == encCtx)
702                 {
703                     encCtx = encode::GetEncContextFromPVOID(ctxPtr);
704                     if(nullptr == encCtx)
705                         return VA_STATUS_ERROR_INVALID_CONTEXT;
706                 }
707                 if(encCtx->codecFunction == CODECHAL_FUNCTION_FEI_PRE_ENC)
708                 {
709                     encode::DDI_ENCODE_PRE_ENC_BUFFER_TYPE idx = (buf->uiType == VAStatsMVBufferType) ? encode::PRE_ENC_BUFFER_TYPE_MVDATA :
710                                                         ((buf->uiType == VAStatsStatisticsBufferType)   ? encode::PRE_ENC_BUFFER_TYPE_STATS
711                                                                                                               : encode::PRE_ENC_BUFFER_TYPE_STATS_BOT);
712                     RemoveFromPreEncStatusReportQueue(encCtx, buf, idx);
713                 }
714             }
715             MediaLibvaUtilNext::FreeBuffer(buf);
716             break;
717         case VAStatsMVPredictorBufferType:
718         case VAEncFEIMBControlBufferType:
719         case VAEncFEIMVPredictorBufferType:
720         case VAEncQPBufferType:
721         case VADecodeStreamoutBufferType:
722             MediaLibvaUtilNext::FreeBuffer(buf);
723             break;
724         case VAEncFEIMVBufferType:
725         case VAEncFEIMBCodeBufferType:
726         case VAEncFEICURecordBufferType:
727         case VAEncFEICTBCmdBufferType:
728         case VAEncFEIDistortionBufferType:
729             {
730                 if(nullptr == encCtx)
731                 {
732                     encCtx = encode::GetEncContextFromPVOID(ctxPtr);
733                     if(nullptr == encCtx)
734                         return VA_STATUS_ERROR_INVALID_CONTEXT;
735                 }
736                 if(encCtx->wModeType == CODECHAL_ENCODE_MODE_AVC)
737                 {
738                     CodecEncodeAvcFeiPicParams *feiPicParams;
739                     feiPicParams = (CodecEncodeAvcFeiPicParams *)(encCtx->pFeiPicParams);
740                     if((feiPicParams != nullptr) && (encCtx->codecFunction == CODECHAL_FUNCTION_FEI_ENC))
741                     {
742                         encode::DDI_ENCODE_FEI_ENC_BUFFER_TYPE idx = (buf->uiType == VAEncFEIMVBufferType) ? encode::FEI_ENC_BUFFER_TYPE_MVDATA :
743                                                         ((buf->uiType == VAEncFEIMBCodeBufferType) ? encode::FEI_ENC_BUFFER_TYPE_MBCODE :
744                                                                                                       encode::FEI_ENC_BUFFER_TYPE_DISTORTION);
745                         RemoveFromEncStatusReportQueue(encCtx, buf, idx);
746                     }
747 
748                 }
749                 else if(encCtx->wModeType == CODECHAL_ENCODE_MODE_HEVC)
750                 {
751                     CodecEncodeHevcFeiPicParams *feiPicParams;
752                     feiPicParams = (CodecEncodeHevcFeiPicParams *)(encCtx->pFeiPicParams);
753                     if((feiPicParams != nullptr) && (encCtx->codecFunction == CODECHAL_FUNCTION_FEI_ENC))
754                     {
755                         encode::DDI_ENCODE_FEI_ENC_BUFFER_TYPE idx = (buf->uiType == VAEncFEICTBCmdBufferType) ? encode::FEI_ENC_BUFFER_TYPE_MVDATA   :
756                                                           ((buf->uiType == VAEncFEICURecordBufferType) ? encode::FEI_ENC_BUFFER_TYPE_MBCODE :
757                                                                                                           encode::FEI_ENC_BUFFER_TYPE_DISTORTION);
758                         RemoveFromEncStatusReportQueue(encCtx, buf, idx);
759                     }
760                 }
761             }
762             MediaLibvaUtilNext::FreeBuffer(buf);
763             break;
764         default: // do not handle any un-listed buffer type
765             MOS_DeleteArray(buf->pData);
766             break;
767             //return va_STATUS_SUCCESS;
768     }
769     MOS_Delete(buf);
770 
771     MediaLibvaInterfaceNext::DestroyBufFromVABufferID(mediaCtx, buffer_id);
772     MOS_TraceEventExt(EVENT_VA_FREE_BUFFER, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
773     return VA_STATUS_SUCCESS;
774 }
775 
BeginPicture(VADriverContextP ctx,VAContextID context,VASurfaceID renderTarget)776 VAStatus DdiEncodeFunctions::BeginPicture (
777     VADriverContextP  ctx,
778     VAContextID       context,
779     VASurfaceID       renderTarget)
780 {
781     PERF_UTILITY_AUTO(__FUNCTION__, PERF_ENCODE, PERF_LEVEL_DDI);
782     DDI_CODEC_FUNC_ENTER;
783 
784     DDI_CODEC_CHK_NULL(ctx, "nullptr context in vpgEncodeBeginPicture!", VA_STATUS_ERROR_INVALID_CONTEXT);
785 
786     // assume the VAContextID is encoder ID
787     uint32_t        ctxType                   = 0;
788     encode::PDDI_ENCODE_CONTEXT encCtx = (decltype(encCtx))MediaLibvaCommonNext::GetContextFromContextID(ctx, context, &ctxType);
789     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
790     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
791 
792     VAStatus vaStatus = encCtx->m_encode->BeginPicture(ctx, context, renderTarget);
793     return vaStatus;
794 }
795 
RenderPicture(VADriverContextP ctx,VAContextID context,VABufferID * buffers,int32_t buffersNum)796 VAStatus DdiEncodeFunctions::RenderPicture (
797     VADriverContextP  ctx,
798     VAContextID       context,
799     VABufferID        *buffers,
800     int32_t           buffersNum)
801 {
802     VAStatus        vaStatus                  = VA_STATUS_SUCCESS;
803     int32_t         numOfBuffers              = buffersNum;
804     int32_t         priority                  = 0;
805     int32_t         priorityIndexInBuffers    = -1;
806     bool            updatePriority            = false;
807     PERF_UTILITY_AUTO(__FUNCTION__, PERF_ENCODE, PERF_LEVEL_DDI);
808     DDI_CODEC_FUNC_ENTER;
809 
810     DDI_CODEC_CHK_NULL(ctx, "nullptr context in vpgEncodeRenderPicture!", VA_STATUS_ERROR_INVALID_CONTEXT);
811 
812     // assume the VAContextID is encoder ID
813     uint32_t        ctxType                   = 0;
814     encode::PDDI_ENCODE_CONTEXT encCtx = (decltype(encCtx))MediaLibvaCommonNext::GetContextFromContextID(ctx, context, &ctxType);
815     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
816     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
817 
818     priorityIndexInBuffers = MediaLibvaCommonNext::GetGpuPriority(ctx, buffers, numOfBuffers, &updatePriority, &priority);
819     if (priorityIndexInBuffers != -1)
820     {
821         if(updatePriority)
822         {
823             vaStatus = SetGpuPriority(encCtx, priority);
824             if(vaStatus != VA_STATUS_SUCCESS)
825                 return vaStatus;
826         }
827         MediaLibvaCommonNext::MovePriorityBufferIdToEnd(buffers, priorityIndexInBuffers, numOfBuffers);
828         numOfBuffers--;
829     }
830     if (numOfBuffers == 0)
831         return vaStatus;
832 
833     vaStatus = encCtx->m_encode->RenderPicture(ctx, context, buffers, numOfBuffers);
834     return vaStatus;
835 }
836 
EndPicture(VADriverContextP ctx,VAContextID context)837 VAStatus DdiEncodeFunctions::EndPicture (
838     VADriverContextP  ctx,
839     VAContextID       context)
840 {
841     PERF_UTILITY_AUTO(__FUNCTION__, PERF_ENCODE, PERF_LEVEL_DDI);
842 
843     DDI_CODEC_FUNC_ENTER;
844     DDI_CODEC_CHK_NULL(ctx, "nullptr context in vpgEncodeEndPicture!", VA_STATUS_ERROR_INVALID_CONTEXT);
845 
846     // assume the VAContextID is encoder ID
847     uint32_t        ctxType                   = 0;
848     encode::PDDI_ENCODE_CONTEXT encCtx = (decltype(encCtx))MediaLibvaCommonNext::GetContextFromContextID(ctx, context, &ctxType);
849     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
850     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
851 
852     VAStatus vaStatus = encCtx->m_encode->EndPicture(ctx, context);
853     return vaStatus;
854 }
855 
856 //!
857 //! \brief  Clean and free encode context structure
858 //!
859 //! \param  [in] encCtx
860 //!     Pointer to ddi encode context
861 //!
CleanUp(encode::PDDI_ENCODE_CONTEXT encCtx)862 void DdiEncodeFunctions::CleanUp(encode::PDDI_ENCODE_CONTEXT encCtx)
863 {
864     if (encCtx->m_encode)
865     {
866         MOS_Delete(encCtx->m_encode);
867         encCtx->m_encode = nullptr;
868     }
869 
870     if (encCtx->pCpDdiInterfaceNext)
871     {
872         MOS_Delete(encCtx->pCpDdiInterfaceNext);
873     }
874 
875     MOS_FreeMemory(encCtx);
876     encCtx = nullptr;
877 
878     return;
879 }
880 
SetGpuPriority(encode::PDDI_ENCODE_CONTEXT encCtx,int32_t priority)881 VAStatus DdiEncodeFunctions::SetGpuPriority(
882     encode::PDDI_ENCODE_CONTEXT encCtx,
883     int32_t             priority
884 )
885 {
886     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
887 
888     if(encCtx->pCodecHal != nullptr)
889     {
890         PMOS_INTERFACE osInterface = encCtx->pCodecHal->GetOsInterface();
891         DDI_CODEC_CHK_NULL(osInterface, "nullptr osInterface.", VA_STATUS_ERROR_ALLOCATION_FAILED);
892 
893         //Set Gpu priority for encoder
894         osInterface->pfnSetGpuPriority(osInterface, priority);
895     }
896 
897     return VA_STATUS_SUCCESS;
898 }
899 
CodedBufferExistInStatusReport(encode::PDDI_ENCODE_CONTEXT encCtx,PDDI_MEDIA_BUFFER buf)900 bool DdiEncodeFunctions::CodedBufferExistInStatusReport(
901     encode::PDDI_ENCODE_CONTEXT encCtx,
902     PDDI_MEDIA_BUFFER   buf)
903 {
904     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
905     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
906     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_PARAMETER);
907 
908     return encCtx->m_encode->CodedBufferExistInStatusReport(buf);
909 }
910 
StatusReport(encode::PDDI_ENCODE_CONTEXT encCtx,DDI_MEDIA_BUFFER * mediaBuf,void ** buf)911 VAStatus DdiEncodeFunctions::StatusReport (
912         encode::PDDI_ENCODE_CONTEXT encCtx,
913         DDI_MEDIA_BUFFER    *mediaBuf,
914         void                **buf
915     )
916 {
917    PERF_UTILITY_AUTO(__FUNCTION__, PERF_ENCODE, PERF_LEVEL_DDI);
918 
919     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
920     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
921     DDI_CODEC_CHK_NULL(mediaBuf, "nullptr mediaBuf", VA_STATUS_ERROR_INVALID_PARAMETER);
922     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_PARAMETER);
923 
924     VAStatus vaStatus = encCtx->m_encode->StatusReport(mediaBuf, buf);
925 
926     return vaStatus;
927 }
928 
PreEncBufferExistInStatusReport(encode::PDDI_ENCODE_CONTEXT encCtx,PDDI_MEDIA_BUFFER buf,encode::DDI_ENCODE_PRE_ENC_BUFFER_TYPE typeIdx)929 bool DdiEncodeFunctions::PreEncBufferExistInStatusReport(
930     encode::PDDI_ENCODE_CONTEXT            encCtx,
931     PDDI_MEDIA_BUFFER              buf,
932     encode:: DDI_ENCODE_PRE_ENC_BUFFER_TYPE typeIdx)
933 {
934     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
935     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
936     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_PARAMETER);
937 
938     return encCtx->m_encode->PreEncBufferExistInStatusReport(buf, typeIdx);
939 }
940 
PreEncStatusReport(encode::PDDI_ENCODE_CONTEXT encCtx,DDI_MEDIA_BUFFER * mediaBuf,void ** buf)941 VAStatus DdiEncodeFunctions::PreEncStatusReport(
942     encode::PDDI_ENCODE_CONTEXT encCtx,
943     DDI_MEDIA_BUFFER    *mediaBuf,
944     void                **buf)
945 {
946     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
947     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
948     DDI_CODEC_CHK_NULL(mediaBuf, "nullptr mediaBuf", VA_STATUS_ERROR_INVALID_PARAMETER);
949     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_PARAMETER);
950 
951     VAStatus vaStatus = encCtx->m_encode->PreEncStatusReport(mediaBuf, buf);
952 
953     return vaStatus;
954 }
955 
EncBufferExistInStatusReport(encode::PDDI_ENCODE_CONTEXT encCtx,PDDI_MEDIA_BUFFER buf,encode::DDI_ENCODE_FEI_ENC_BUFFER_TYPE typeIdx)956 bool DdiEncodeFunctions::EncBufferExistInStatusReport(
957     encode::PDDI_ENCODE_CONTEXT            encCtx,
958     PDDI_MEDIA_BUFFER              buf,
959     encode::DDI_ENCODE_FEI_ENC_BUFFER_TYPE typeIdx)
960 {
961     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
962     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
963     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_PARAMETER);
964 
965     return encCtx->m_encode->EncBufferExistInStatusReport(buf, typeIdx);
966 }
967 
EncStatusReport(encode::PDDI_ENCODE_CONTEXT encCtx,DDI_MEDIA_BUFFER * mediaBuf,void ** buf)968 VAStatus DdiEncodeFunctions::EncStatusReport(
969     encode::PDDI_ENCODE_CONTEXT encCtx,
970     DDI_MEDIA_BUFFER    *mediaBuf,
971     void                **buf)
972 {
973     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
974     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
975     DDI_CODEC_CHK_NULL(mediaBuf, "nullptr mediaBuf", VA_STATUS_ERROR_INVALID_PARAMETER);
976     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_PARAMETER);
977 
978     VAStatus vaStatus = encCtx->m_encode->EncStatusReport(mediaBuf, buf);
979 
980     return vaStatus;
981 }
982 
RemoveFromPreEncStatusReportQueue(encode::PDDI_ENCODE_CONTEXT encCtx,PDDI_MEDIA_BUFFER buf,encode::DDI_ENCODE_PRE_ENC_BUFFER_TYPE typeIdx)983 VAStatus DdiEncodeFunctions::RemoveFromPreEncStatusReportQueue(
984     encode::PDDI_ENCODE_CONTEXT            encCtx,
985     PDDI_MEDIA_BUFFER              buf,
986     encode::DDI_ENCODE_PRE_ENC_BUFFER_TYPE typeIdx)
987 {
988     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
989     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
990     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_PARAMETER);
991 
992     VAStatus vaStatus = encCtx->m_encode->RemoveFromPreEncStatusReportQueue(buf, typeIdx);
993 
994     return vaStatus;
995 }
996 
RemoveFromEncStatusReportQueue(encode::PDDI_ENCODE_CONTEXT encCtx,PDDI_MEDIA_BUFFER buf,encode::DDI_ENCODE_FEI_ENC_BUFFER_TYPE typeIdx)997 VAStatus DdiEncodeFunctions::RemoveFromEncStatusReportQueue(
998     encode::PDDI_ENCODE_CONTEXT            encCtx,
999     PDDI_MEDIA_BUFFER              buf,
1000     encode::DDI_ENCODE_FEI_ENC_BUFFER_TYPE typeIdx)
1001 {
1002     DDI_CODEC_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
1003     DDI_CODEC_CHK_NULL(encCtx->m_encode, "nullptr encCtx->m_encode", VA_STATUS_ERROR_INVALID_CONTEXT);
1004     DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_PARAMETER);
1005 
1006     VAStatus vaStatus = encCtx->m_encode->RemoveFromEncStatusReportQueue(buf, typeIdx);
1007 
1008     return vaStatus;
1009 }
1010