1 /*
2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
17 #define HST_LOG_TAG "HdiUtils"
18
19 #include "hdi_utils.h"
20 #include "display_type.h"
21 #include "hdf_base.h"
22
23 namespace OHOS {
24 namespace Media {
25 namespace Plugin {
26 const static std::pair<::CodecType, Plugin::CodecType> codecTypeMap[] = {
27 {::CodecType::VIDEO_DECODER, Plugin::CodecType::VIDEO_DECODER},
28 {::CodecType::VIDEO_ENCODER, Plugin::CodecType::VIDEO_ENCODER},
29 {::CodecType::AUDIO_DECODER, Plugin::CodecType::AUDIO_DECODER},
30 {::CodecType::AUDIO_ENCODER, Plugin::CodecType::AUDIO_ENCODER},
31 };
32
33 const static std::pair<int32_t, Status> retStatusMap[] = {
34 {HDF_SUCCESS, Status::OK},
35 {HDF_FAILURE, Status::ERROR_UNKNOWN},
36 {HDF_ERR_NOT_SUPPORT, Status::ERROR_INVALID_OPERATION},
37 {HDF_ERR_INVALID_PARAM, Status::ERROR_INVALID_PARAMETER},
38 {HDF_ERR_NOT_SUPPORT, Status::ERROR_AGAIN},
39 {HDF_ERR_MALLOC_FAIL, Status::ERROR_NO_MEMORY},
40 };
41
42 const static std::map<HDF_STATUS, std::string> hdfStatusMap = {
43 {HDF_STATUS::HDF_SUCCESS, "HDF_SUCCESS"},
44 {HDF_STATUS::HDF_FAILURE, "HDF_FAILURE"},
45 {HDF_STATUS::HDF_ERR_NOT_SUPPORT, "HDF_ERR_NOT_SUPPORT"},
46 {HDF_STATUS::HDF_ERR_INVALID_PARAM, "HDF_ERR_INVALID_PARAM"},
47 {HDF_STATUS::HDF_ERR_INVALID_OBJECT, "HDF_ERR_INVALID_OBJECT"},
48 {HDF_STATUS::HDF_ERR_MALLOC_FAIL, "HDF_ERR_MALLOC_FAIL"},
49 {HDF_STATUS::HDF_ERR_TIMEOUT, "HDF_ERR_TIMEOUT"},
50 {HDF_STATUS::HDF_ERR_THREAD_CREATE_FAIL, "HDF_ERR_THREAD_CREATE_FAIL"},
51 {HDF_STATUS::HDF_ERR_QUEUE_FULL, "HDF_ERR_QUEUE_FULL"},
52 {HDF_STATUS::HDF_ERR_DEVICE_BUSY, "HDF_ERR_DEVICE_BUSY"},
53 {HDF_STATUS::HDF_ERR_IO, "HDF_ERR_IO"},
54 {HDF_STATUS::HDF_ERR_BAD_FD, "HDF_ERR_BAD_FD"},
55 {HDF_STATUS::HDF_ERR_NOPERM, "HDF_ERR_NOPERM"},
56 {HDF_STATUS::HDF_BSP_ERR_OP, "HDF_BSP_ERR_OP"},
57 {HDF_STATUS::HDF_ERR_BSP_PLT_API_ERR, "HDF_ERR_BSP_PLT_API_ERR"},
58 {HDF_STATUS::HDF_PAL_ERR_DEV_CREATE, "HDF_PAL_ERR_DEV_CREATE"},
59 {HDF_STATUS::HDF_PAL_ERR_INNER, "HDF_PAL_ERR_INNER"},
60 {HDF_STATUS::HDF_DEV_ERR_NO_MEMORY, "HDF_DEV_ERR_NO_MEMORY"},
61 {HDF_STATUS::HDF_DEV_ERR_NO_DEVICE, "HDF_DEV_ERR_NO_DEVICE"},
62 {HDF_STATUS::HDF_DEV_ERR_NO_DEVICE_SERVICE, "HDF_DEV_ERR_NO_DEVICE_SERVICE"},
63 {HDF_STATUS::HDF_DEV_ERR_DEV_INIT_FAIL, "HDF_DEV_ERR_DEV_INIT_FAIL"},
64 {HDF_STATUS::HDF_DEV_ERR_PUBLISH_FAIL, "HDF_DEV_ERR_PUBLISH_FAIL"},
65 {HDF_STATUS::HDF_DEV_ERR_ATTACHDEV_FAIL, "HDF_DEV_ERR_ATTACHDEV_FAIL"},
66 {HDF_STATUS::HDF_DEV_ERR_NODATA, "HDF_DEV_ERR_NODATA"},
67 {HDF_STATUS::HDF_DEV_ERR_NORANGE, "HDF_DEV_ERR_NORANGE"},
68 {HDF_STATUS::HDF_DEV_ERR_OP, "HDF_DEV_ERR_OP"},
69 };
70
71 const static std::map<OMX_ERRORTYPE, std::string> omxStatusMap = {
72 {OMX_ErrorPortUnresponsiveDuringStop, "OMX_ErrorPortUnresponsiveDuringStop"},
73 {OMX_ErrorIncorrectStateTransition, "OMX_ErrorIncorrectStateTransition"},
74 {OMX_ErrorIncorrectStateOperation, "OMX_ErrorIncorrectStateOperation"},
75 {OMX_ErrorUnsupportedSetting, "OMX_ErrorUnsupportedSetting"},
76 {OMX_ErrorUnsupportedIndex, "OMX_ErrorUnsupportedIndex"},
77 {OMX_ErrorBadPortIndex, "OMX_ErrorBadPortIndex"},
78 {OMX_ErrorPortUnpopulated, "OMX_ErrorPortUnpopulated"},
79 };
80
81 const static std::unordered_map<OMX_STATETYPE, std::string> omxStateMap = {
82 {OMX_StateInvalid, "OMX_StateInvalid"},
83 {OMX_StateLoaded, "OMX_StateLoaded"},
84 {OMX_StateLoaded, "OMX_StateLoaded"},
85 {OMX_StateIdle, "OMX_StateIdle"},
86 {OMX_StateExecuting, "OMX_StateExecuting"},
87 {OMX_StatePause, "OMX_StatePause"},
88 {OMX_StateWaitForResources, "OMX_StateWaitForResources"},
89 {OMX_StateKhronosExtensions, "OMX_StateKhronosExtensions"},
90 {OMX_StateVendorStartUnused, "OMX_StateVendorStartUnused"},
91 {OMX_StateMax, "OMX_StateMax"},
92 };
93
94 const std::map<VideoPixelFormat, PixelFormat> pixelFormatMap = {
95 {VideoPixelFormat::YUV420P, PIXEL_FMT_YCBCR_420_P},
96 {VideoPixelFormat::NV12, PIXEL_FMT_YCBCR_420_SP},
97 {VideoPixelFormat::NV21, PIXEL_FMT_YCRCB_420_SP},
98 {VideoPixelFormat::YUYV422, PIXEL_FMT_YUYV_422_PKG},
99 {VideoPixelFormat::YUV422P, PIXEL_FMT_YUV_422_I},
100 {VideoPixelFormat::RGBA, PIXEL_FMT_RGBA_8888},
101 {VideoPixelFormat::BGRA, PIXEL_FMT_BGRA_8888},
102 {VideoPixelFormat::RGB24, PIXEL_FMT_RGB_888},
103 };
104
OmxStateToString(OMX_STATETYPE state)105 std::string OmxStateToString(OMX_STATETYPE state)
106 {
107 auto iter = omxStateMap.find(state);
108 if (iter != omxStateMap.end()) {
109 return iter->second;
110 }
111 MEDIA_LOG_W("Not find value, maybe update the map");
112 return "null";
113 }
114
HdfStatus2String(int32_t status)115 std::string HdfStatus2String(int32_t status)
116 {
117 auto it1 = hdfStatusMap.find(static_cast<HDF_STATUS>(status));
118 auto it2 = omxStatusMap.find(static_cast<OMX_ERRORTYPE>(status));
119 if (it1 != hdfStatusMap.end()) {
120 return it1->second;
121 } else if (it2 != omxStatusMap.end()) {
122 return it2->second;
123 }
124 MEDIA_LOG_W("Not find value, maybe update the map");
125 return "null";
126 }
127
PortIndex2String(PortIndex portIndex)128 std::string PortIndex2String(PortIndex portIndex)
129 {
130 std::map<PortIndex, std::string> portIndexMap = {
131 {PortIndex::PORT_INDEX_INPUT, "PORT_INDEX_INPUT"},
132 {PortIndex::PORT_INDEX_OUTPUT, "PORT_INDEX_OUTPUT"},
133 };
134 auto it = portIndexMap.find(portIndex);
135 if (it != portIndexMap.end()) {
136 return it->second;
137 }
138 MEDIA_LOG_W("Not find value, maybe update the map");
139 return "null";
140 }
141
142 template<typename T, typename U>
TranslatesByMap(const T & t,U & u,const std::pair<T,U> * transMap,size_t mapSize)143 bool TranslatesByMap(const T& t, U& u, const std::pair<T, U>* transMap, size_t mapSize)
144 {
145 for (size_t cnt = 0; cnt < mapSize; cnt++) {
146 if (t == transMap[cnt].first) {
147 u = transMap[cnt].second;
148 return true;
149 }
150 }
151 return false;
152 }
153
TranslateRets(const int32_t & ret)154 Status TranslateRets(const int32_t& ret)
155 {
156 Status status = Status::ERROR_UNKNOWN;
157 TranslatesByMap(ret, status, retStatusMap, sizeof(retStatusMap) / sizeof(retStatusMap[0]));
158 return status;
159 }
160
161 /**
162 * if T and U are the same types, always assigns directly and returns true
163 *
164 * @tparam T
165 * @tparam U
166 * @param src
167 * @param dest
168 * @return
169 */
170 template<typename T, typename U,
171 typename std::enable_if<std::is_same<typename std::decay<T>::type,
172 typename std::decay<U>::type>::value, bool>::type = true>
Translates(const T & src,U & dest)173 bool Translates(const T& src, U& dest)
174 {
175 dest = src;
176 return true;
177 }
178
179 template<>
Translates(const::CodecType & codecType,Plugin::CodecType & pluginCodecType)180 bool Translates(const ::CodecType& codecType, Plugin::CodecType& pluginCodecType)
181 {
182 return TranslatesByMap(codecType, pluginCodecType, codecTypeMap, sizeof(codecTypeMap) / sizeof(codecTypeMap[0]));
183 }
184
Translate2PluginFlagSet(uint32_t omxBufFlag)185 uint64_t Translate2PluginFlagSet(uint32_t omxBufFlag)
186 {
187 uint64_t ret = 0;
188 if (omxBufFlag == OMX_BUFFERFLAG_EOS) {
189 ret = BUFFER_FLAG_EOS;
190 }
191 return ret;
192 }
193
Translate2omxFlagSet(uint64_t pluginFlags)194 uint32_t Translate2omxFlagSet(uint64_t pluginFlags)
195 {
196 uint32_t ret = 0;
197 if (pluginFlags == BUFFER_FLAG_EOS) {
198 ret = OMX_BUFFERFLAG_EOS;
199 }
200 return ret;
201 }
202
ConvertPixelFormatFromHdi(int32_t HdiPixelFormat)203 VideoPixelFormat ConvertPixelFormatFromHdi(int32_t HdiPixelFormat)
204 {
205 // Histreamer pixel format to HDI pixel format
206 auto iter = std::find_if(pixelFormatMap.begin(), pixelFormatMap.end(),
207 [&] (const std::pair<VideoPixelFormat, PixelFormat>& tmp) -> bool {
208 return tmp.second == HdiPixelFormat;
209 });
210 return iter == pixelFormatMap.end() ? VideoPixelFormat::UNKNOWN : iter->first;
211 }
212 } // namespace Plugin
213 } // namespace Media
214 } // namespace OHOS
215 #endif