• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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*>(&param);
50     std::copy(p, p + sizeof(T), vec.begin() + beforeSize);
51 }
52 
AppendArrayToVector(std::vector<uint8_t> & vec,const std::vector<uint8_t> & param)53 inline void AppendArrayToVector(std::vector<uint8_t>& vec, const std::vector<uint8_t>& param)
54 {
55     size_t beforeSize = vec.size();
56     size_t afterSize = beforeSize + param.size();
57     vec.resize(afterSize);
58     vec.insert(vec.begin() + beforeSize, param.begin(), param.end());
59 }
60 
61 struct BinaryReader {
BinaryReaderBinaryReader62     BinaryReader(uint8_t* data, size_t size) : mData(data), mSize(size) {}
63 
64     template<typename T>
ReadBinaryReader65     T* Read()
66     {
67         if (mData == nullptr) {
68             return nullptr;
69         }
70         size_t oldPos = mCurrPos;
71         size_t newPos = mCurrPos + sizeof(T);
72         if (newPos > mSize) {
73             return nullptr;
74         }
75         mCurrPos = newPos;
76         return reinterpret_cast<T*>(mData + oldPos);
77     }
78 
79 private:
80     uint8_t* mData = nullptr;
81     size_t mSize;
82     size_t mCurrPos = 0;
83 };
84 
85 std::string StringifyMeta(std::shared_ptr<Media::Meta> &meta);
86 
87 void SetThreadInteractiveQos(bool enable);
88 }
89 #endif // HCODEC_UTILS_H
90