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/common/plugin_memory.h"
24 #include "plugin/common/plugin_meta.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 bool IsExist(Tag tag);
80
81 void Update(const BufferMeta& bufferMeta);
82
83 virtual std::shared_ptr<BufferMeta> Clone() = 0;
84
85 protected:
86 /// Constructor
87 explicit BufferMeta(BufferMetaType type);
88
89 private:
90 BufferMetaType type_;
91
92 /// Buffer metadata information of the buffer, which is represented by the key-value pair of the tag.
93 std::shared_ptr<Meta> tags_ {};
94 };
95
96 /**
97 * @brief Audio buffer metadata.
98 *
99 * Buffer metadata describing how data is laid out inside the buffer.
100 *
101 * @since 1.0
102 * @version 1.0
103 */
104 class AudioBufferMeta : public BufferMeta {
105 public:
106 /// Destructor
107 ~AudioBufferMeta() override = default;
108
109 std::shared_ptr<BufferMeta> Clone() override;
110
111 /// the number of valid samples in the buffer
112 size_t samples {0};
113
114 /// Audio sample formats
115 AudioSampleFormat sampleFormat {AudioSampleFormat::S8};
116
117 /// the audio sample rate
118 uint32_t sampleRate {0};
119
120 /// the number of channels
121 uint32_t channels {0};
122
123 /// the number bytes for one frame, this is the size of one sample * channels
124 uint32_t bytesPreFrame {0};
125
126 /// Indicates that the channel order.
127 AudioChannelLayout channelLayout {AudioChannelLayout::MONO};
128
129 /// the offsets (in bytes) where each channel plane starts in the buffer.
130 std::vector<size_t> offsets {};
131
132 private:
133 /// Constructor
AudioBufferMeta()134 AudioBufferMeta() : BufferMeta(BufferMetaType::AUDIO) {}
135
136 friend class Buffer;
137 };
138
139 /**
140 * @brief Video buffer metadata.
141 *
142 * Extra buffer metadata describing video properties.
143 *
144 * @since 1.0
145 * @version 1.0
146 */
147 class VideoBufferMeta : public BufferMeta {
148 public:
149 /// Destructor
150 ~VideoBufferMeta() override = default;
151
152 std::shared_ptr<BufferMeta> Clone() override;
153
154 /// describing video formats.
155 VideoPixelFormat videoPixelFormat {VideoPixelFormat::UNKNOWN};
156
157 /// identifier of the frame。
158 uint32_t id {0};
159
160 /// the video width.
161 uint32_t width {0};
162
163 /// the video height.
164 uint32_t height {0};
165
166 /// the number of planes in the image.
167 uint32_t planes {0};
168
169 /// array of strides for the planes.
170 std::vector<uint32_t> stride {};
171
172 /// array of offsets for the planes.
173 std::vector<uint32_t> offset {};
174
175 private:
176 /// Constructor
VideoBufferMeta()177 VideoBufferMeta() : BufferMeta(BufferMetaType::VIDEO) {}
178
179 friend class Buffer;
180 };
181
182 /**
183 * @brief Buffer base class.
184 * Contains the data storage and metadata information of the buffer (buffer description information).
185 *
186 * @since 1.0
187 * @version 1.0
188 */
189 class Buffer {
190 public:
191 /// Construct an empty buffer.
192 explicit Buffer(BufferMetaType type = BufferMetaType::AUDIO);
193
194 /// Destructor
195 ~Buffer() = default;
196
197 static std::shared_ptr<Buffer> CreateDefaultBuffer(BufferMetaType type, size_t capacity,
198 std::shared_ptr<Allocator> allocator = nullptr,
199 size_t align = 1);
200
201 std::shared_ptr<Memory> WrapMemory(uint8_t* data, size_t capacity, size_t size);
202
203 std::shared_ptr<Memory> WrapMemoryPtr(std::shared_ptr<uint8_t> data, size_t capacity, size_t size);
204
205 std::shared_ptr<Memory> AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align = 1);
206
207 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
208 std::shared_ptr<Memory> WrapSurfaceMemory(sptr<SurfaceBuffer> surfaceBuffer);
209 #endif
210
211 uint32_t GetMemoryCount();
212
213 std::shared_ptr<Memory> GetMemory(uint32_t index = 0);
214
215 std::shared_ptr<BufferMeta> GetBufferMeta();
216
217 void UpdateBufferMeta(const BufferMeta& bufferMeta);
218
219 void Reset();
220
221 /// no memory in the buffer.
222 bool IsEmpty();
223
224 void ChangeBufferMetaType(BufferMetaType type);
225
226 /// track index.
227 uint32_t trackID;
228
229 /// presentation timestamp of the buffer based on {@link HST_TIME_BASE}.
230 int64_t pts;
231
232 /// decoding timestamp of the buffer based on {@link HST_TIME_BASE}.
233 int64_t dts;
234
235 /// duration in time of the buffer data based on {@link HST_TIME_BASE}.
236 int64_t duration;
237
238 /// flag of the buffer, which is used to record extra information.
239 /// @see BUFFER_FLAG_EOS
240 uint64_t flag;
241
242 private:
243 /// Data described by this buffer.
244 std::vector<std::shared_ptr<Memory>> data {};
245
246 /// The buffer meta information.
247 std::shared_ptr<BufferMeta> meta;
248 };
249 } // namespace Plugin
250 } // namespace Media
251 } // namespace OHOS
252 #endif // HISTREAMER_PLUGIN_COMMON_BUFFER_H
253