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