xref: /aosp_15_r20/external/intel-media-driver/media_softlet/agnostic/common/codec/hal/enc/shared/encode_utils.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
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 __ENCODE_UTILS_H__
23 #define __ENCODE_UTILS_H__
24 #include <mutex>
25 #include "mos_util_debug.h"
26 #include "mos_utilities.h"
27 
28 #define _SW_BRC _MEDIA_RESERVED && (_DEBUG || _RELEASE_INTERNAL)
29 
30 enum HuCFunction
31 {
32     NONE_BRC = 0,
33     BRC_INIT,
34     BRC_RESET,
35     BRC_UPDATE,
36     LA_INIT,
37     LA_RESET,
38     LA_UPDATE,
39     PAK_INTEGRATE,
40     HPU_VP9
41 };
42 
43 //------------------------------------------------------------------------------
44 // Macros specific to MOS_CODEC_SUBCOMP_ENCODE sub-comp
45 //------------------------------------------------------------------------------
46 #define ENCODE_ASSERT(_expr)                                                   \
47     MOS_ASSERT(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _expr)
48 
49 #define ENCODE_ASSERTMESSAGE(_message, ...)                                    \
50     MOS_ASSERTMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _message, ##__VA_ARGS__)
51 
52 #define ENCODE_NORMALMESSAGE(_message, ...)                                    \
53     MOS_NORMALMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _message, ##__VA_ARGS__)
54 
55 #define ENCODE_VERBOSEMESSAGE(_message, ...)                                   \
56     MOS_VERBOSEMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _message, ##__VA_ARGS__)
57 
58 #define ENCODE_CHK_NULL_RETURN(_ptr)                                                  \
59     MOS_CHK_NULL_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _ptr)
60 
61 #define ENCODE_CHK_NULL_NO_STATUS_RETURN(_ptr)                                                  \
62     MOS_CHK_NULL_NO_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _ptr)
63 
64 #define ENCODE_CHK_STATUS_RETURN(_stmt)                                               \
65     MOS_CHK_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _stmt)
66 
67 #define ENCODE_CHK_STATUS_NO_STATUS_RETURN(_stmt)                                               \
68     MOS_CHK_STATUS_NO_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _stmt)
69 
70 #define ENCODE_CHK_COND_RETURN(_stmt, _message, ...)                                            \
71     MOS_CHK_COND_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _stmt, _message, ##__VA_ARGS__)
72 
73 #define ENCODE_CHK_STATUS_MESSAGE_RETURN(_stmt, _message, ...)                        \
74     MOS_CHK_STATUS_MESSAGE_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, _stmt, _message, ##__VA_ARGS__)
75 
76 #define ENCODE_CHK_NULL_WITH_DESTROY_RETURN_VALUE(_ptr, destroyFunction) \
77     MOS_CHK_COND_WITH_DESTROY_RETURN_VALUE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, (nullptr == _ptr), destroyFunction, MOS_STATUS_NULL_POINTER, "error nullptr!")
78 
79 #define ENCODE_CHK_STATUS_WITH_DESTROY_RETURN_VALUE(_stmt, destroyFunction)                                                                                     \
80 {                                                                                                                                                               \
81     MOS_STATUS sts = (MOS_STATUS)(_stmt);                                                                                                                       \
82     MOS_CHK_COND_WITH_DESTROY_RETURN_VALUE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, (MOS_STATUS_SUCCESS != sts), destroyFunction, sts, "error status!")   \
83 }
84 
85 namespace encode {
86 
87 class Trace
88 {
89 public:
Trace(const char * name)90     Trace(const char* name) : m_name(name)
91     {
92         ENCODE_VERBOSEMESSAGE("Enter function:%s\r\n", name);
93     }
94 
~Trace()95     ~Trace()
96     {
97         ENCODE_VERBOSEMESSAGE("Exit function:%s\r\n", m_name);
98     }
99 
100 protected:
101     const char* m_name;
102 };
103 
104 class AutoLock
105 {
106 public:
AutoLock(PMOS_MUTEX mutex)107     AutoLock(PMOS_MUTEX mutex) : m_mutex(mutex) { MosUtilities::MosLockMutex(mutex); }
~AutoLock()108     ~AutoLock() { MosUtilities::MosUnlockMutex(m_mutex); }
109 protected:
110     PMOS_MUTEX m_mutex;
111 MEDIA_CLASS_DEFINE_END(encode__AutoLock)
112 };
113 
114 class Condition
115 {
116 public:
Condition()117     Condition()
118     {
119         m_sem = MosUtilities::MosCreateSemaphore(0, 1);
120     }
121 
~Condition()122     ~Condition()
123     {
124         MosUtilities::MosDestroySemaphore(m_sem);
125     }
Wait(PMOS_MUTEX mutex)126     MOS_STATUS Wait(PMOS_MUTEX mutex)
127     {
128         MOS_STATUS status = MOS_STATUS_SUCCESS;
129         MosUtilities::MosUnlockMutex(mutex);
130         status = MosUtilities::MosWaitSemaphore(m_sem, 5000);
131         MosUtilities::MosLockMutex(mutex);
132         return status;
133     }
Signal()134     MOS_STATUS Signal()
135     {
136         MosUtilities::MosPostSemaphore(m_sem, 1);
137         return MOS_STATUS_SUCCESS;
138     }
139 
140 protected:
141     PMOS_SEMAPHORE m_sem;
142 MEDIA_CLASS_DEFINE_END(encode__Condition)
143 };
144 
145 //!
146 //! \brief    Allocate data list with specific type and length
147 //!
148 //! \param    [in,out] dataList
149 //!           Pointer to a type * pointer. Specify the address of the memory handles
150 //! \param    [in] length
151 //!           Specify the number of data members
152 //!
153 //! \return   MOS_STATUS
154 //!           MOS_STATUS_SUCCESS if success, else fail reason
155 //!
156 template <class type>
EncodeAllocateDataList(type ** dataList,uint32_t length)157 MOS_STATUS EncodeAllocateDataList(type **dataList, uint32_t length)
158 {
159     type *ptr;
160     ptr = (type *)MOS_AllocAndZeroMemory(sizeof(type) * length);
161     if (ptr == nullptr)
162     {
163         ENCODE_ASSERTMESSAGE("No memory to allocate CodecHal data list.");
164         return MOS_STATUS_NO_SPACE;
165     }
166     for (uint32_t i = 0; i < length; i++)
167     {
168         dataList[i] = &(ptr[i]);
169     }
170     return MOS_STATUS_SUCCESS;
171 }
172 
173 //!
174 //! \brief    Free data list
175 //!
176 //! \param    [in,out] dataList
177 //!           Pointer to a type * pointer. Specify the address of the memory handles
178 //! \param    [in] length
179 //!           Specify the number of data members
180 //!
181 //! \return   MOS_STATUS
182 //!           MOS_STATUS_SUCCESS if success, else fail reason
183 //!
184 template <class type>
EncodeFreeDataList(type ** dataList,uint32_t length)185 MOS_STATUS EncodeFreeDataList(type **dataList, uint32_t length)
186 {
187     type* ptr;
188     ptr = dataList[0];
189     if (ptr)
190     {
191         MOS_FreeMemory(ptr);
192     }
193     for (uint32_t i = 0; i < length; i++)
194     {
195         dataList[i] = nullptr;
196     }
197 
198     return MOS_STATUS_SUCCESS;
199 }
200 
201 }
202 
203 #define ENCODE_FUNC_CALL() encode::Trace trace(__FUNCTION__);
204 
205 #endif // !__ENCODE_UTILS_H__
206