1 /*
2  * Copyright (C) 2023 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 #ifndef UTILS_H
17 #define UTILS_H
18 
19 #include <unistd.h>
20 
21 namespace OHOS {
22 namespace MediaAVCodec {
SleepFor(unsigned ms)23 inline void SleepFor(unsigned ms)
24 {
25     constexpr int factor = 1000;
26     usleep(ms * factor);
27 }
28 
29 template <typename T>
30 using MakeUnsigned = typename std::make_unsigned<T>::type;
31 
32 template <typename T, typename U>
AlignUp(T num,U alignment)33 constexpr T AlignUp(T num, U alignment)
34 {
35     return (alignment > 0) ? (static_cast<uint64_t>((num + static_cast<MakeUnsigned<T>>(alignment) - 1)) &
36                               static_cast<uint64_t>((~(static_cast<MakeUnsigned<T>>(alignment) - 1))))
37                            : num;
38 }
39 
40 template<typename T, typename U>
ReinterpretPointerCast(const std::shared_ptr<U> & ptr)41 inline std::shared_ptr<T> ReinterpretPointerCast(const std::shared_ptr<U>& ptr) noexcept
42 {
43     return std::shared_ptr<T>(ptr, reinterpret_cast<T*>(ptr.get()));
44 }
45 } // namespace MediaAVCodec
46 } // namespace OHOS
47 #endif