1 // Copyright (c) 2019 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include "mfxvideo.h"
22 #if (MFX_VERSION_MAJOR == 1) && (MFX_VERSION_MINOR < 8)
23 #include "mfxlinux.h"
24 #endif
25 
26 #include "common_utils.h"
27 #include "common_vaapi.h"
28 
29 /* =====================================================
30  * Linux implementation of OS-specific utility functions
31  */
32 
Initialize(mfxIMPL impl,mfxVersion ver,MFXVideoSession * pSession,mfxFrameAllocator * pmfxAllocator,bool bCreateSharedHandles)33 mfxStatus Initialize(mfxIMPL impl, mfxVersion ver, MFXVideoSession* pSession, mfxFrameAllocator* pmfxAllocator, bool bCreateSharedHandles)
34 {
35     mfxStatus sts = MFX_ERR_NONE;
36 
37     // Initialize Intel Media SDK Session
38     sts = pSession->Init(impl, &ver);
39     MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
40 
41     // Create VA display
42     mfxHDL displayHandle = { 0 };
43     sts = CreateVAEnvDRM(&displayHandle);
44     MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
45 
46     // Provide VA display handle to Media SDK
47     sts = pSession->SetHandle(static_cast < mfxHandleType >(MFX_HANDLE_VA_DISPLAY), displayHandle);
48     MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
49 
50     // If mfxFrameAllocator is provided it means we need to setup  memory allocator
51     if (pmfxAllocator) {
52         pmfxAllocator->pthis  = *pSession; // We use Media SDK session ID as the allocation identifier
53         pmfxAllocator->Alloc  = simple_alloc;
54         pmfxAllocator->Free   = simple_free;
55         pmfxAllocator->Lock   = simple_lock;
56         pmfxAllocator->Unlock = simple_unlock;
57         pmfxAllocator->GetHDL = simple_gethdl;
58 
59         // Since we are using video memory we must provide Media SDK with an external allocator
60         sts = pSession->SetFrameAllocator(pmfxAllocator);
61         MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
62     }
63 
64     return sts;
65 }
66 
Release()67 void Release()
68 {
69     CleanupVAEnvDRM();
70 }
71 
mfxGetTime(mfxTime * timestamp)72 void mfxGetTime(mfxTime* timestamp)
73 {
74     clock_gettime(CLOCK_REALTIME, timestamp);
75 }
76 
TimeDiffMsec(mfxTime tfinish,mfxTime tstart)77 double TimeDiffMsec(mfxTime tfinish, mfxTime tstart)
78 {
79     double result;
80     long long elapsed_nsec = tfinish.tv_nsec - tstart.tv_nsec;
81     long long elapsed_sec = tfinish.tv_sec - tstart.tv_sec;
82 
83     //if (tstart.tv_sec==0) return -1;
84 
85     //timespec uses two fields -- check if borrowing necessary
86     if (elapsed_nsec < 0) {
87         elapsed_sec -= 1;
88         elapsed_nsec += 1000000000;
89     }
90     //return total converted to milliseconds
91     result = (double)elapsed_sec *1000.0;
92     result += (double)elapsed_nsec / 1000000;
93 
94     return result;
95 }
96 
ClearYUVSurfaceVMem(mfxMemId memId)97 void ClearYUVSurfaceVMem(mfxMemId memId)
98 {
99     ClearYUVSurfaceVAAPI(memId);
100 }
101 
ClearRGBSurfaceVMem(mfxMemId memId)102 void ClearRGBSurfaceVMem(mfxMemId memId)
103 {
104     ClearRGBSurfaceVAAPI(memId);
105 }
106