• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_tags.h"
24 #include "plugin_audio_tags.h"
25 #include "plugin_video_tags.h"
26 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
27 #include "refbase.h"
28 #include "surface/surface.h"
29 #endif
30 
31 namespace OHOS {
32 namespace Media {
33 namespace Plugin {
34 constexpr size_t INVALID_POSITION = -1;
35 
36 /// End of Stream Buffer Flag
37 #define BUFFER_FLAG_EOS 0x00000001
38 /// Video Key Frame Flag
39 #define BUFFER_FLAG_KEY_FRAME 0x00000002
40 
41 // Align value template
42 template <typename T>
43 using MakeUnsigned = typename std::make_unsigned<T>::type;
44 
45 template <typename T, typename U>
AlignUp(T num,U alignment)46 constexpr T AlignUp(T num, U alignment)
47 {
48     return (alignment > 0) ? (static_cast<uint64_t>((num + static_cast<MakeUnsigned<T>>(alignment) - 1)) &
49         static_cast<uint64_t>((~(static_cast<MakeUnsigned<T>>(alignment) - 1)))) :
50         num;
51 }
52 
53 /**
54  * @enum MemoryType
55  *
56  * @since 1.0
57  * @version 1.0
58  */
59 enum struct MemoryType : uint8_t {
60     VIRTUAL_ADDR = 0,  ///< Virtual address
61     SURFACE_BUFFER,    ///< Surface
62     SHARE_MEMORY,   ///< Share Memory fd
63 };
64 
65 /**
66  * @brief Memory allocator, which is provided by the plugin implementer.
67  *
68  * @since 1.0
69  * @version 1.0
70  */
71 struct Allocator {
memoryTypeAllocator72     explicit Allocator(MemoryType type = MemoryType::VIRTUAL_ADDR) : memoryType(type) {}
73     virtual ~Allocator() = default;
74     /**
75      * @brief Allocates a buffer using the specified size
76      * .
77      * @param size  Allocation parameters.
78      * @return      Pointer of the allocated buffer.
79      */
80     virtual void* Alloc(size_t size) = 0;
81 
82     /**
83      * @brief Frees a buffer.
84      * Buffer handles returned by Alloc() must be freed with this function when no longer needed.
85      *
86      * @param ptr   Pointer of the allocated buffer.
87      */
88     virtual void Free(void* ptr) = 0; // NOLINT: intentionally using void* here
89 
GetMemoryTypeAllocator90     MemoryType GetMemoryType()
91     {
92         return memoryType;
93     }
94 
95 private:
96     MemoryType memoryType;
97 };
98 
99 /**
100  * @enum Buffer Meta Type
101  *
102  * @since 1.0
103  * @version 1.0
104  */
105 enum struct BufferMetaType : uint32_t {
106     AUDIO,      ///< Meta used to describe audio data
107     VIDEO,      ///< Meta used to describe video data
108 };
109 
110 /**
111 * @brief Memory description. Only manager the basic memory information.
112 *
113 *  here is the memory layout.
114 *            |            capacity                      |
115 *            |------------------------------------------|
116 *            |       buffer size       |
117 *            |-------------------------|
118 *  addr   offset                      buffer end
119 *  +---------+-------------------------+----------------+
120 *  |*********|        used buffer      |  unused buffer |
121 *  +---------+-------------------------+----------------+
122 *
123 *  operate position:
124 *                         position
125 *            +----------------+
126 *
127 * @since 1.0
128 * @version 1.0
129 */
130 class Memory {
131 public:
132     /// Destructor
133     virtual ~Memory() = default;
134 
135     // Todo: Add the documentation description.
136     size_t GetCapacity();
137 
138     size_t GetSize();
139 
140     const uint8_t* GetReadOnlyData(size_t position = 0);
141 
142     uint8_t *GetWritableAddr(size_t estimatedWriteSize, size_t position = 0);
143 
144     // If estimatedWriteSize doesn't equal to realWriteSize, should call UpdateDataSize
145     void UpdateDataSize(size_t realWriteSize, size_t position = 0);
146 
147     size_t Write(const uint8_t* in, size_t writeSize, size_t position = INVALID_POSITION);
148 
149     size_t Read(uint8_t* out, size_t readSize, size_t position = INVALID_POSITION);
150 
151     void Reset();
152 
153     MemoryType GetMemoryType();
154 
155 protected:
156     /**
157      * Allocates memory by the specified allocator.
158      * Allocation and release are the responsibility of the external allocator.
159      *
160      * @param capacity Allocated memory size.
161      * @param allocator External allocator.
162      * @param align The alignment of the memory.
163      */
164     explicit Memory(size_t capacity, std::shared_ptr<Allocator> allocator = nullptr,
165                     size_t align = 1, MemoryType type = MemoryType::VIRTUAL_ADDR, bool allocMem = true);
166 
167     /**
168      * Get real memory address, it is addr + offset, the offset is calculated according to alignment.
169      */
170     virtual uint8_t *GetRealAddr() const;
171 
172     /// Memory type
173     MemoryType memoryType;
174 
175     /// Allocated memory size.
176     size_t capacity;
177 
178     /// The alignment of the memory.
179 #if (defined(__GNUC__) || defined(__clang__)) && (!defined(WIN32))
180 __attribute__((unused))
181 #endif
182     size_t alignment;
183 
184     /// Offset of the buffer address to make sure access according to alignment.
185     size_t offset {0};
186 
187     /// Valid data size
188     size_t size;
189 
190     /// Externally specified allocator, optional.
191     std::shared_ptr<Allocator> allocator;
192 
193 private:
194     /**
195      * Create objects based on the external memory, use shared pointers,
196      * the allocation and release of memory are managed externally.
197      *
198      * @param capacity Allocated memory size.
199      * @param bufData External memory.
200      * @param align The alignment of the memory.
201      */
202     Memory(size_t capacity, std::shared_ptr<uint8_t> bufData,
203            size_t align = 1, MemoryType type = MemoryType::VIRTUAL_ADDR);
204 
205     /// Allocated virtual memory address.
206     std::shared_ptr<uint8_t> addr;
207 
208     friend class Buffer;
209 };
210 
211 /**
212  * @brief Buffer Meta.
213  * Base class that describes various media metadata.
214  *
215  * @since 1.0
216  * @version 1.0
217  */
218 class BufferMeta {
219 public:
220     /// Destructor
221     virtual ~BufferMeta() = default;
222 
223     ValueType GetMeta(Tag tag);
224 
225     void SetMeta(Tag tag, ValueType value);
226 
227     BufferMetaType GetType() const;
228 
229 protected:
230     /// Constructor
231     explicit BufferMeta(BufferMetaType type);
232 
233 private:
234     BufferMetaType type;
235 
236     /// Buffer metadata information of the buffer, which is represented by the key-value pair of the tag.
237     std::shared_ptr<TagMap> tags {};
238 };
239 
240 /**
241  * @brief Audio buffer metadata.
242  *
243  * Buffer metadata describing how data is laid out inside the buffer.
244  *
245  * @since 1.0
246  * @version 1.0
247  */
248 class AudioBufferMeta : public BufferMeta {
249 public:
250     /// Destructor
251     ~AudioBufferMeta() override = default;
252 
253     /// the number of valid samples in the buffer
254     size_t samples {0};
255 
256     /// Audio sample formats
257     AudioSampleFormat sampleFormat {AudioSampleFormat::S8};
258 
259     /// the audio sample rate
260     uint32_t sampleRate {0};
261 
262     /// the number of channels
263     uint32_t channels {0};
264 
265     /// the number bytes for one frame, this is the size of one sample * channels
266     uint32_t bytesPreFrame {0};
267 
268     /// Indicates that the channel order.
269     AudioChannelLayout channelLayout {AudioChannelLayout::MONO};
270 
271     /// the offsets (in bytes) where each channel plane starts in the buffer.
272     std::vector<size_t> offsets {};
273 
274 private:
275     /// Constructor
AudioBufferMeta()276     AudioBufferMeta() : BufferMeta(BufferMetaType::AUDIO) {}
277 
278     friend class Buffer;
279 };
280 
281 /**
282  * @brief Video buffer metadata.
283  *
284  *  Extra buffer metadata describing video properties.
285  *
286  *  @since 1.0
287  *  @version 1.0
288  */
289 class VideoBufferMeta : public BufferMeta {
290 public:
291     /// Destructor
292     ~VideoBufferMeta() override = default;
293 
294     /// describing video formats.
295     VideoPixelFormat videoPixelFormat {VideoPixelFormat::UNKNOWN};
296 
297     /// identifier of the frame。
298     uint32_t id {0};
299 
300     /// the video width.
301     uint32_t width {0};
302 
303     /// the video height.
304     uint32_t height {0};
305 
306     /// the number of planes in the image.
307     uint32_t planes {0};
308 
309     /// array of strides for the planes.
310     std::vector<uint32_t> stride {};
311 
312     /// array of offsets for the planes.
313     std::vector<uint32_t> offset {};
314 
315 private:
316     /// Constructor
VideoBufferMeta()317     VideoBufferMeta() : BufferMeta(BufferMetaType::VIDEO) {}
318 
319     friend class Buffer;
320 };
321 
322 /**
323 * @brief Buffer base class.
324 * Contains the data storage and metadata information of the buffer (buffer description information).
325 *
326 * @since 1.0
327 * @version 1.0
328 */
329 class Buffer {
330 public:
331     /// Construct an empty buffer.
332     explicit Buffer(BufferMetaType type = BufferMetaType::AUDIO);
333 
334     /// Destructor
335     ~Buffer() = default;
336 
337     static std::shared_ptr<Buffer> CreateDefaultBuffer(BufferMetaType type, size_t capacity,
338                                                        std::shared_ptr<Allocator> allocator = nullptr,
339                                                        size_t align = 1);
340 
341     std::shared_ptr<Memory> WrapMemory(uint8_t* data, size_t capacity, size_t size);
342 
343     std::shared_ptr<Memory> WrapMemoryPtr(std::shared_ptr<uint8_t> data, size_t capacity, size_t size);
344 
345     std::shared_ptr<Memory> AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align = 1);
346 
347     uint32_t GetMemoryCount();
348 
349     std::shared_ptr<Memory> GetMemory(uint32_t index = 0);
350 
351     std::shared_ptr<BufferMeta> GetBufferMeta();
352 
353     void Reset();
354 
355     /// no memory in the buffer.
356     bool IsEmpty();
357 
358     /// track index.
359     uint32_t trackID;
360 
361     /// presentation timestamp of the buffer based on {@link HST_TIME_BASE}.
362     uint64_t pts;
363 
364     /// decoding timestamp of the buffer based on {@link HST_TIME_BASE}.
365     uint64_t dts;
366 
367     /// duration in time of the buffer data based on {@link HST_TIME_BASE}.
368     uint64_t duration;
369 
370     /// flag of the buffer, which is used to record extra information.
371     /// @see BUFFER_FLAG_EOS
372     uint64_t flag;
373 
374 private:
375     /// Data described by this buffer.
376     std::vector<std::shared_ptr<Memory>> data {};
377 
378     /// The buffer meta information.
379     std::shared_ptr<BufferMeta> meta;
380 };
381 } // namespace Plugin
382 } // namespace Media
383 } // namespace OHOS
384 #endif // HISTREAMER_PLUGIN_COMMON_BUFFER_H
385