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 HCODEC_UTILS_H
17 #define HCODEC_UTILS_H
18
19 #include <vector>
20 #include <algorithm>
21
22 namespace OHOS::MediaAVCodec {
23 inline constexpr int TIME_RATIO_S_TO_MS = 1000;
24 inline constexpr double US_TO_MS = 1000.0;
25 inline constexpr double US_TO_S = 1000000.0;
26
GetYuv420Size(uint32_t w,uint32_t h)27 inline uint32_t GetYuv420Size(uint32_t w, uint32_t h)
28 {
29 return w * h * 3 / 2; // 3: nom of ratio, 2: denom of ratio
30 }
31
IsSecureMode(const std::string & name)32 inline bool IsSecureMode(const std::string &name)
33 {
34 std::string prefix = ".secure";
35 if (name.length() <= prefix.length()) {
36 return false;
37 }
38 return (name.rfind(prefix) == (name.length() - prefix.length()));
39 }
40
41 template <typename T>
AppendToVector(std::vector<uint8_t> & vec,const T & param)42 void AppendToVector(std::vector<uint8_t>& vec, const T& param)
43 {
44 size_t beforeSize = vec.size();
45 size_t afterSize = beforeSize + sizeof(T);
46 vec.resize(afterSize);
47
48 const uint8_t* p = reinterpret_cast<const uint8_t*>(¶m);
49 std::copy(p, p + sizeof(T), vec.begin() + beforeSize);
50 }
51
52 struct BinaryReader {
BinaryReaderBinaryReader53 BinaryReader(uint8_t* data, size_t size) : mData(data), mSize(size) {}
54
55 template<typename T>
ReadBinaryReader56 T* Read()
57 {
58 if (mData == nullptr) {
59 return nullptr;
60 }
61 size_t oldPos = mCurrPos;
62 size_t newPos = mCurrPos + sizeof(T);
63 if (newPos > mSize) {
64 return nullptr;
65 }
66 mCurrPos = newPos;
67 return reinterpret_cast<T*>(mData + oldPos);
68 }
69
70 private:
71 uint8_t* mData = nullptr;
72 size_t mSize;
73 size_t mCurrPos = 0;
74 };
75
76 }
77 #endif // HCODEC_UTILS_H
78