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 HISTREAMER_PLUGIN_COMMON_BUFFER_H
17 #define HISTREAMER_PLUGIN_COMMON_BUFFER_H
18
19 #include <memory>
20 #include <map>
21 #include <vector>
22
23 #include "plugin_memory.h"
24 #include "securec.h"
25
26 namespace OHOS {
27 namespace MediaAVCodec {
28 namespace Plugin {
29 /// End of Stream Buffer Flag
30 #define BUFFER_FLAG_EOS 0x00000001
31 /// Video Key Frame Flag
32 #define BUFFER_FLAG_KEY_FRAME 0x00000002
33
34 // Align value template
35 template <typename T>
36 using MakeUnsigned = typename std::make_unsigned<T>::type;
37
38 template <typename T, typename U>
AlignUp(T num,U alignment)39 constexpr T AlignUp(T num, U alignment)
40 {
41 return (alignment > 0) ? (static_cast<uint64_t>((num + static_cast<MakeUnsigned<T>>(alignment) - 1)) &
42 static_cast<uint64_t>((~(static_cast<MakeUnsigned<T>>(alignment) - 1)))) :
43 num;
44 }
45
46 /**
47 * @enum Buffer Meta Type
48 *
49 * @since 1.0
50 * @version 1.0
51 */
52 enum struct BufferMetaType : uint32_t {
53 AUDIO, ///< Meta used to describe audio data
54 VIDEO, ///< Meta used to describe video data
55 };
56
57
58 /**
59 * @brief Buffer base class.
60 * Contains the data storage and metadata information of the buffer (buffer description information).
61 *
62 * @since 1.0
63 * @version 1.0
64 */
65 class Buffer {
66 public:
67 /// Construct an empty buffer.
68 explicit Buffer(BufferMetaType type = BufferMetaType::AUDIO);
69
70 /// Destructor
71 ~Buffer() = default;
72
73 static std::shared_ptr<Buffer> CreateDefaultBuffer(BufferMetaType type, size_t capacity,
74 std::shared_ptr<Allocator> allocator = nullptr,
75 size_t align = 1);
76
77 std::shared_ptr<Memory> WrapMemory(uint8_t* dataptr, size_t capacity, size_t size);
78
79 std::shared_ptr<Memory> WrapMemoryPtr(std::shared_ptr<uint8_t> dataptr, size_t capacity, size_t size);
80
81 std::shared_ptr<Memory> AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align = 1);
82
83 uint32_t GetMemoryCount();
84
85 std::shared_ptr<Memory> GetMemory(uint32_t index = 0);
86
87 void Reset();
88
89 /// no memory in the buffer.
90 bool IsEmpty();
91
92 /// track index.
93 uint32_t trackID;
94
95 /// presentation timestamp of the buffer based on {@link HST_TIME_BASE}.
96 int64_t pts;
97
98 /// decoding timestamp of the buffer based on {@link HST_TIME_BASE}.
99 int64_t dts;
100
101 /// duration in time of the buffer data based on {@link HST_TIME_BASE}.
102 int64_t duration;
103
104 /// flag of the buffer, which is used to record extra information.
105 /// @see BUFFER_FLAG_EOS
106 uint64_t flag;
107
108 private:
109 /// Data described by this buffer.
110 std::vector<std::shared_ptr<Memory>> data {};
111 };
112 } // namespace Plugin
113 } // namespace MediaAVCodec
114 } // namespace OHOS
115 #endif // HISTREAMER_PLUGIN_COMMON_BUFFER_H
116