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