1 /*
2 * Copyright (c) 2018-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 #ifndef __DECODE_UTILS_H__
23 #define __DECODE_UTILS_H__
24 #include <mutex>
25 #include "mos_util_debug.h"
26 #include "mos_utilities.h"
27 #include "media_class_trace.h"
28 
29 //------------------------------------------------------------------------------
30 // Macros specific to MOS_CODEC_SUBCOMP_DECODE sub-comp
31 //------------------------------------------------------------------------------
32 #define DECODE_ASSERT(_expr)                                                   \
33     MOS_ASSERT(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DECODE, _expr)
34 
35 #define DECODE_ASSERTMESSAGE(_message, ...)                                    \
36     MOS_ASSERTMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DECODE, _message, ##__VA_ARGS__)
37 
38 #define DECODE_NORMALMESSAGE(_message, ...)                                    \
39     MOS_NORMALMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DECODE, _message, ##__VA_ARGS__)
40 
41 #define DECODE_VERBOSEMESSAGE(_message, ...)                                   \
42     MOS_VERBOSEMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DECODE, _message, ##__VA_ARGS__)
43 
44 #define DECODE_CHK_NULL(_ptr)                                                  \
45     MOS_CHK_NULL_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DECODE, _ptr)
46 
47 #define DECODE_CHK_STATUS(_stmt)                                               \
48     MOS_CHK_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DECODE, _stmt)
49 
50 #define DECODE_CHK_STATUS_MESSAGE(_stmt, _message, ...)                        \
51     MOS_CHK_STATUS_MESSAGE_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DECODE, _stmt, _message, ##__VA_ARGS__)
52 
53 #define DECODE_CHK_COND(_expr, _message, ...)                                  \
54     MOS_CHK_COND_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DECODE,_expr,_message, ##__VA_ARGS__)
55 
56 #define DECODE_CHK_NULL_NO_STATUS_RETURN(_ptr) \
57     MOS_CHK_NULL_NO_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DECODE, _ptr)
58 
59 namespace decode {
60 
61 class Trace
62 {
63 public:
Trace(const char * name)64     Trace(const char* name) : m_name(name)
65     {
66         DECODE_VERBOSEMESSAGE("Enter function:%s\r\n", name);
67     }
68 
~Trace()69     ~Trace()
70     {
71         DECODE_VERBOSEMESSAGE("Exit function:%s\r\n", m_name);
72     }
73 
74 protected:
75     const char* m_name;
76 };
77 
78 class Mutex
79 {
80 public:
Mutex()81     Mutex()
82     {
83         m_mutex = MosUtilities::MosCreateMutex();
84         DECODE_ASSERT(m_mutex != nullptr);
85     }
~Mutex()86     ~Mutex()
87     {
88         MosUtilities::MosDestroyMutex(m_mutex);
89     }
Get()90     PMOS_MUTEX Get()
91     {
92         return m_mutex;
93     }
94 protected:
95     PMOS_MUTEX m_mutex;
96 };
97 
98 class AutoLock
99 {
100 public:
AutoLock(Mutex & mutex)101     AutoLock(Mutex &mutex) : m_mutex(mutex) { MosUtilities::MosLockMutex(m_mutex.Get()); }
~AutoLock()102     ~AutoLock() { MosUtilities::MosUnlockMutex(m_mutex.Get()); }
103 protected:
104     Mutex &m_mutex;
105 };
106 
107 class Condition
108 {
109 public:
Condition()110     Condition()
111     {
112         m_sem = MosUtilities::MosCreateSemaphore(0, 1);
113     }
114 
~Condition()115     ~Condition()
116     {
117         MosUtilities::MosDestroySemaphore(m_sem);
118     }
Wait(PMOS_MUTEX mutex)119     MOS_STATUS Wait(PMOS_MUTEX mutex)
120     {
121         MOS_STATUS status = MOS_STATUS_SUCCESS;
122         MosUtilities::MosUnlockMutex(mutex);
123         status = MosUtilities::MosWaitSemaphore(m_sem, 5000);
124         MosUtilities::MosLockMutex(mutex);
125         return status;
126     }
Signal()127     MOS_STATUS Signal()
128     {
129         MosUtilities::MosPostSemaphore(m_sem, 1);
130         return MOS_STATUS_SUCCESS;
131     }
132 
133 protected:
134     PMOS_SEMAPHORE m_sem;
135 };
136 
137 //!
138 //! \brief    Allocate data list with specific type and length
139 //!
140 //! \param    [in,out] dataList
141 //!           Pointer to a type * pointer. Specify the address of the memory handles
142 //! \param    [in] length
143 //!           Specify the number of data members
144 //!
145 //! \return   MOS_STATUS
146 //!           MOS_STATUS_SUCCESS if success, else fail reason
147 //!
148 template <class type>
AllocateDataList(type ** dataList,uint32_t length)149 MOS_STATUS AllocateDataList(type **dataList, uint32_t length)
150 {
151     type *ptr;
152     ptr = (type *)MOS_AllocAndZeroMemory(sizeof(type) * length);
153     if (ptr == nullptr)
154     {
155         DECODE_ASSERTMESSAGE("No memory to allocate CodecHal data list.");
156         return MOS_STATUS_NO_SPACE;
157     }
158     for (uint32_t i = 0; i < length; i++)
159     {
160         dataList[i] = &(ptr[i]);
161     }
162     return MOS_STATUS_SUCCESS;
163 }
164 
165 //!
166 //! \brief    Free data list
167 //!
168 //! \param    [in,out] dataList
169 //!           Pointer to a type * pointer. Specify the address of the memory handles
170 //! \param    [in] length
171 //!           Specify the number of data members
172 //!
173 //! \return   MOS_STATUS
174 //!           MOS_STATUS_SUCCESS if success, else fail reason
175 //!
176 template <class type>
FreeDataList(type ** dataList,uint32_t length)177 MOS_STATUS FreeDataList(type **dataList, uint32_t length)
178 {
179     type* ptr;
180     ptr = dataList[0];
181     if (ptr)
182     {
183         MOS_FreeMemory(ptr);
184     }
185     for (uint32_t i = 0; i < length; i++)
186     {
187         dataList[i] = nullptr;
188     }
189 
190     return MOS_STATUS_SUCCESS;
191 }
192 
ReadUserFeature(uint32_t id,MOS_CONTEXT_HANDLE mosCtx)193 inline MOS_USER_FEATURE_VALUE_DATA ReadUserFeature(uint32_t id, MOS_CONTEXT_HANDLE mosCtx)
194 {
195     MOS_USER_FEATURE_VALUE_DATA userFeatureData;
196     MOS_ZeroMemory(&userFeatureData, sizeof(userFeatureData));
197     MOS_UserFeature_ReadValue_ID(
198         nullptr,
199         id,
200         &userFeatureData,
201         mosCtx);
202     return userFeatureData;
203 }
204 
205 }
206 
207 #define DECODE_FUNC_CALL() decode::Trace trace(__FUNCTION__);
208 
209 #endif // !__DECODE_UTILS_H__
210