1 /*
2 * Copyright (c) 2021-2021 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 "tag_map.h"
25 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
26 #include "refbase.h"
27 #include "surface/surface.h"
28 #endif
29
30 namespace OHOS {
31 namespace Media {
32 namespace Plugin {
33 /// End of Stream Buffer Flag
34 #define BUFFER_FLAG_EOS 0x00000001
35 /// Video Key Frame Flag
36 #define BUFFER_FLAG_KEY_FRAME 0x00000002
37
38 // Align value template
39 template <typename T>
40 using MakeUnsigned = typename std::make_unsigned<T>::type;
41
42 template <typename T, typename U>
AlignUp(T num,U alignment)43 constexpr T AlignUp(T num, U alignment)
44 {
45 return (alignment > 0) ? (static_cast<uint64_t>((num + static_cast<MakeUnsigned<T>>(alignment) - 1)) &
46 static_cast<uint64_t>((~(static_cast<MakeUnsigned<T>>(alignment) - 1)))) :
47 num;
48 }
49
50 /**
51 * @enum Buffer Meta Type
52 *
53 * @since 1.0
54 * @version 1.0
55 */
56 enum struct BufferMetaType : uint32_t {
57 AUDIO, ///< Meta used to describe audio data
58 VIDEO, ///< Meta used to describe video data
59 };
60
61 /**
62 * @brief Buffer Meta.
63 * Base class that describes various media metadata.
64 *
65 * @since 1.0
66 * @version 1.0
67 */
68 class BufferMeta {
69 public:
70 /// Destructor
71 virtual ~BufferMeta() = default;
72
73 ValueType GetMeta(Tag tag);
74
75 void SetMeta(Tag tag, ValueType value);
76
77 BufferMetaType GetType() const;
78
79 protected:
80 /// Constructor
81 explicit BufferMeta(BufferMetaType type);
82
83 private:
84 BufferMetaType type;
85
86 /// Buffer metadata information of the buffer, which is represented by the key-value pair of the tag.
87 std::shared_ptr<TagMap> tags {};
88 };
89
90 /**
91 * @brief Audio buffer metadata.
92 *
93 * Buffer metadata describing how data is laid out inside the buffer.
94 *
95 * @since 1.0
96 * @version 1.0
97 */
98 class AudioBufferMeta : public BufferMeta {
99 public:
100 /// Destructor
101 ~AudioBufferMeta() override = default;
102
103 /// the number of valid samples in the buffer
104 size_t samples {0};
105
106 /// Audio sample formats
107 AudioSampleFormat sampleFormat {AudioSampleFormat::S8};
108
109 /// the audio sample rate
110 uint32_t sampleRate {0};
111
112 /// the number of channels
113 uint32_t channels {0};
114
115 /// the number bytes for one frame, this is the size of one sample * channels
116 uint32_t bytesPreFrame {0};
117
118 /// Indicates that the channel order.
119 AudioChannelLayout channelLayout {AudioChannelLayout::MONO};
120
121 /// the offsets (in bytes) where each channel plane starts in the buffer.
122 std::vector<size_t> offsets {};
123
124 private:
125 /// Constructor
AudioBufferMeta()126 AudioBufferMeta() : BufferMeta(BufferMetaType::AUDIO) {}
127
128 friend class Buffer;
129 };
130
131 /**
132 * @brief Video buffer metadata.
133 *
134 * Extra buffer metadata describing video properties.
135 *
136 * @since 1.0
137 * @version 1.0
138 */
139 class VideoBufferMeta : public BufferMeta {
140 public:
141 /// Destructor
142 ~VideoBufferMeta() override = default;
143
144 /// describing video formats.
145 VideoPixelFormat videoPixelFormat {VideoPixelFormat::UNKNOWN};
146
147 /// identifier of the frame。
148 uint32_t id {0};
149
150 /// the video width.
151 uint32_t width {0};
152
153 /// the video height.
154 uint32_t height {0};
155
156 /// the number of planes in the image.
157 uint32_t planes {0};
158
159 /// array of strides for the planes.
160 std::vector<uint32_t> stride {};
161
162 /// array of offsets for the planes.
163 std::vector<uint32_t> offset {};
164
165 private:
166 /// Constructor
VideoBufferMeta()167 VideoBufferMeta() : BufferMeta(BufferMetaType::VIDEO) {}
168
169 friend class Buffer;
170 };
171
172 /**
173 * @brief Buffer base class.
174 * Contains the data storage and metadata information of the buffer (buffer description information).
175 *
176 * @since 1.0
177 * @version 1.0
178 */
179 class Buffer {
180 public:
181 /// Construct an empty buffer.
182 explicit Buffer(BufferMetaType type = BufferMetaType::AUDIO);
183
184 /// Destructor
185 ~Buffer() = default;
186
187 static std::shared_ptr<Buffer> CreateDefaultBuffer(BufferMetaType type, size_t capacity,
188 std::shared_ptr<Allocator> allocator = nullptr,
189 size_t align = 1);
190
191 std::shared_ptr<Memory> WrapMemory(uint8_t* data, size_t capacity, size_t size);
192
193 std::shared_ptr<Memory> WrapMemoryPtr(std::shared_ptr<uint8_t> data, size_t capacity, size_t size);
194
195 std::shared_ptr<Memory> AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align = 1);
196
197 uint32_t GetMemoryCount();
198
199 std::shared_ptr<Memory> GetMemory(uint32_t index = 0);
200
201 std::shared_ptr<BufferMeta> GetBufferMeta();
202
203 void Reset();
204
205 /// no memory in the buffer.
206 bool IsEmpty();
207
208 /// track index.
209 uint32_t trackID;
210
211 /// presentation timestamp of the buffer based on {@link HST_TIME_BASE}.
212 int64_t pts;
213
214 /// decoding timestamp of the buffer based on {@link HST_TIME_BASE}.
215 int64_t dts;
216
217 /// duration in time of the buffer data based on {@link HST_TIME_BASE}.
218 int64_t duration;
219
220 /// flag of the buffer, which is used to record extra information.
221 /// @see BUFFER_FLAG_EOS
222 uint64_t flag;
223
224 private:
225 /// Data described by this buffer.
226 std::vector<std::shared_ptr<Memory>> data {};
227
228 /// The buffer meta information.
229 std::shared_ptr<BufferMeta> meta;
230 };
231 } // namespace Plugin
232 } // namespace Media
233 } // namespace OHOS
234 #endif // HISTREAMER_PLUGIN_COMMON_BUFFER_H
235