1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#ifndef HISTREAMER_PLUGIN_COMMON_BUFFER_H
17fa7767c5Sopenharmony_ci#define HISTREAMER_PLUGIN_COMMON_BUFFER_H
18fa7767c5Sopenharmony_ci
19fa7767c5Sopenharmony_ci#include <memory>
20fa7767c5Sopenharmony_ci#include <map>
21fa7767c5Sopenharmony_ci#include <vector>
22fa7767c5Sopenharmony_ci
23fa7767c5Sopenharmony_ci#include "plugin/common/plugin_memory.h"
24fa7767c5Sopenharmony_ci#include "plugin/common/plugin_meta.h"
25fa7767c5Sopenharmony_ci#if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
26fa7767c5Sopenharmony_ci#include "refbase.h"
27fa7767c5Sopenharmony_ci#include "surface/surface.h"
28fa7767c5Sopenharmony_ci#endif
29fa7767c5Sopenharmony_ci
30fa7767c5Sopenharmony_cinamespace OHOS {
31fa7767c5Sopenharmony_cinamespace Media {
32fa7767c5Sopenharmony_cinamespace Plugin {
33fa7767c5Sopenharmony_ci/// End of Stream Buffer Flag
34fa7767c5Sopenharmony_ci#define BUFFER_FLAG_EOS 0x00000001
35fa7767c5Sopenharmony_ci/// Video Key Frame Flag
36fa7767c5Sopenharmony_ci#define BUFFER_FLAG_KEY_FRAME 0x00000002
37fa7767c5Sopenharmony_ci
38fa7767c5Sopenharmony_ci// Align value template
39fa7767c5Sopenharmony_citemplate <typename T>
40fa7767c5Sopenharmony_ciusing MakeUnsigned = typename std::make_unsigned<T>::type;
41fa7767c5Sopenharmony_ci
42fa7767c5Sopenharmony_citemplate <typename T, typename U>
43fa7767c5Sopenharmony_ciconstexpr T AlignUp(T num, U alignment)
44fa7767c5Sopenharmony_ci{
45fa7767c5Sopenharmony_ci    return (alignment > 0) ? (static_cast<uint64_t>((static_cast<MakeUnsigned<T>>(num)
46fa7767c5Sopenharmony_ci        + static_cast<MakeUnsigned<T>>(alignment) - 1)) &
47fa7767c5Sopenharmony_ci        static_cast<uint64_t>((~(static_cast<MakeUnsigned<T>>(alignment) - 1)))) :
48fa7767c5Sopenharmony_ci        num;
49fa7767c5Sopenharmony_ci}
50fa7767c5Sopenharmony_ci
51fa7767c5Sopenharmony_ci/**
52fa7767c5Sopenharmony_ci * @enum Buffer Meta Type
53fa7767c5Sopenharmony_ci *
54fa7767c5Sopenharmony_ci * @since 1.0
55fa7767c5Sopenharmony_ci * @version 1.0
56fa7767c5Sopenharmony_ci */
57fa7767c5Sopenharmony_cienum struct BufferMetaType : uint32_t {
58fa7767c5Sopenharmony_ci    AUDIO,      ///< Meta used to describe audio data
59fa7767c5Sopenharmony_ci    VIDEO,      ///< Meta used to describe video data
60fa7767c5Sopenharmony_ci};
61fa7767c5Sopenharmony_ci
62fa7767c5Sopenharmony_ci/**
63fa7767c5Sopenharmony_ci * @brief Buffer Meta.
64fa7767c5Sopenharmony_ci * Base class that describes various media metadata.
65fa7767c5Sopenharmony_ci *
66fa7767c5Sopenharmony_ci * @since 1.0
67fa7767c5Sopenharmony_ci * @version 1.0
68fa7767c5Sopenharmony_ci */
69fa7767c5Sopenharmony_ciclass BufferMeta {
70fa7767c5Sopenharmony_cipublic:
71fa7767c5Sopenharmony_ci    /// Destructor
72fa7767c5Sopenharmony_ci    virtual ~BufferMeta() = default;
73fa7767c5Sopenharmony_ci
74fa7767c5Sopenharmony_ci    ValueType GetMeta(Tag tag);
75fa7767c5Sopenharmony_ci
76fa7767c5Sopenharmony_ci    void SetMeta(Tag tag, ValueType value);
77fa7767c5Sopenharmony_ci
78fa7767c5Sopenharmony_ci    BufferMetaType GetType() const;
79fa7767c5Sopenharmony_ci
80fa7767c5Sopenharmony_ci    bool IsExist(Tag tag);
81fa7767c5Sopenharmony_ci
82fa7767c5Sopenharmony_ci    void Update(const BufferMeta& bufferMeta);
83fa7767c5Sopenharmony_ci
84fa7767c5Sopenharmony_ci    virtual std::shared_ptr<BufferMeta> Clone() = 0;
85fa7767c5Sopenharmony_ci
86fa7767c5Sopenharmony_ciprotected:
87fa7767c5Sopenharmony_ci    /// Constructor
88fa7767c5Sopenharmony_ci    explicit BufferMeta(BufferMetaType type);
89fa7767c5Sopenharmony_ci
90fa7767c5Sopenharmony_ciprivate:
91fa7767c5Sopenharmony_ci    BufferMetaType type_;
92fa7767c5Sopenharmony_ci
93fa7767c5Sopenharmony_ci    /// Buffer metadata information of the buffer, which is represented by the key-value pair of the tag.
94fa7767c5Sopenharmony_ci    std::shared_ptr<Meta> tags_ {};
95fa7767c5Sopenharmony_ci};
96fa7767c5Sopenharmony_ci
97fa7767c5Sopenharmony_ci/**
98fa7767c5Sopenharmony_ci * @brief Audio buffer metadata.
99fa7767c5Sopenharmony_ci *
100fa7767c5Sopenharmony_ci * Buffer metadata describing how data is laid out inside the buffer.
101fa7767c5Sopenharmony_ci *
102fa7767c5Sopenharmony_ci * @since 1.0
103fa7767c5Sopenharmony_ci * @version 1.0
104fa7767c5Sopenharmony_ci */
105fa7767c5Sopenharmony_ciclass AudioBufferMeta : public BufferMeta {
106fa7767c5Sopenharmony_cipublic:
107fa7767c5Sopenharmony_ci    /// Destructor
108fa7767c5Sopenharmony_ci    ~AudioBufferMeta() override = default;
109fa7767c5Sopenharmony_ci
110fa7767c5Sopenharmony_ci    std::shared_ptr<BufferMeta> Clone() override;
111fa7767c5Sopenharmony_ci
112fa7767c5Sopenharmony_ci    /// the number of valid samples in the buffer
113fa7767c5Sopenharmony_ci    size_t samples {0};
114fa7767c5Sopenharmony_ci
115fa7767c5Sopenharmony_ci    /// Audio sample formats
116fa7767c5Sopenharmony_ci    AudioSampleFormat sampleFormat {AudioSampleFormat::S8};
117fa7767c5Sopenharmony_ci
118fa7767c5Sopenharmony_ci    /// the audio sample rate
119fa7767c5Sopenharmony_ci    uint32_t sampleRate {0};
120fa7767c5Sopenharmony_ci
121fa7767c5Sopenharmony_ci    /// the number of channels
122fa7767c5Sopenharmony_ci    uint32_t channels {0};
123fa7767c5Sopenharmony_ci
124fa7767c5Sopenharmony_ci    /// the number bytes for one frame, this is the size of one sample * channels
125fa7767c5Sopenharmony_ci    uint32_t bytesPreFrame {0};
126fa7767c5Sopenharmony_ci
127fa7767c5Sopenharmony_ci    /// Indicates that the channel order.
128fa7767c5Sopenharmony_ci    AudioChannelLayout channelLayout {AudioChannelLayout::MONO};
129fa7767c5Sopenharmony_ci
130fa7767c5Sopenharmony_ci    /// the offsets (in bytes) where each channel plane starts in the buffer.
131fa7767c5Sopenharmony_ci    std::vector<size_t> offsets {};
132fa7767c5Sopenharmony_ci
133fa7767c5Sopenharmony_ciprivate:
134fa7767c5Sopenharmony_ci    /// Constructor
135fa7767c5Sopenharmony_ci    AudioBufferMeta() : BufferMeta(BufferMetaType::AUDIO) {}
136fa7767c5Sopenharmony_ci
137fa7767c5Sopenharmony_ci    friend class Buffer;
138fa7767c5Sopenharmony_ci};
139fa7767c5Sopenharmony_ci
140fa7767c5Sopenharmony_ci/**
141fa7767c5Sopenharmony_ci * @brief Video buffer metadata.
142fa7767c5Sopenharmony_ci *
143fa7767c5Sopenharmony_ci *  Extra buffer metadata describing video properties.
144fa7767c5Sopenharmony_ci *
145fa7767c5Sopenharmony_ci *  @since 1.0
146fa7767c5Sopenharmony_ci *  @version 1.0
147fa7767c5Sopenharmony_ci */
148fa7767c5Sopenharmony_ciclass VideoBufferMeta : public BufferMeta {
149fa7767c5Sopenharmony_cipublic:
150fa7767c5Sopenharmony_ci    /// Destructor
151fa7767c5Sopenharmony_ci    ~VideoBufferMeta() override = default;
152fa7767c5Sopenharmony_ci
153fa7767c5Sopenharmony_ci    std::shared_ptr<BufferMeta> Clone() override;
154fa7767c5Sopenharmony_ci
155fa7767c5Sopenharmony_ci    /// describing video formats.
156fa7767c5Sopenharmony_ci    VideoPixelFormat videoPixelFormat {VideoPixelFormat::UNKNOWN};
157fa7767c5Sopenharmony_ci
158fa7767c5Sopenharmony_ci    /// identifier of the frame。
159fa7767c5Sopenharmony_ci    uint32_t id {0};
160fa7767c5Sopenharmony_ci
161fa7767c5Sopenharmony_ci    /// the video width.
162fa7767c5Sopenharmony_ci    uint32_t width {0};
163fa7767c5Sopenharmony_ci
164fa7767c5Sopenharmony_ci    /// the video height.
165fa7767c5Sopenharmony_ci    uint32_t height {0};
166fa7767c5Sopenharmony_ci
167fa7767c5Sopenharmony_ci    /// the number of planes in the image.
168fa7767c5Sopenharmony_ci    uint32_t planes {0};
169fa7767c5Sopenharmony_ci
170fa7767c5Sopenharmony_ci    /// array of strides for the planes.
171fa7767c5Sopenharmony_ci    std::vector<uint32_t> stride {};
172fa7767c5Sopenharmony_ci
173fa7767c5Sopenharmony_ci    /// array of offsets for the planes.
174fa7767c5Sopenharmony_ci    std::vector<uint32_t> offset {};
175fa7767c5Sopenharmony_ci
176fa7767c5Sopenharmony_ciprivate:
177fa7767c5Sopenharmony_ci    /// Constructor
178fa7767c5Sopenharmony_ci    VideoBufferMeta() : BufferMeta(BufferMetaType::VIDEO) {}
179fa7767c5Sopenharmony_ci
180fa7767c5Sopenharmony_ci    friend class Buffer;
181fa7767c5Sopenharmony_ci};
182fa7767c5Sopenharmony_ci
183fa7767c5Sopenharmony_ci/**
184fa7767c5Sopenharmony_ci* @brief Buffer base class.
185fa7767c5Sopenharmony_ci* Contains the data storage and metadata information of the buffer (buffer description information).
186fa7767c5Sopenharmony_ci*
187fa7767c5Sopenharmony_ci* @since 1.0
188fa7767c5Sopenharmony_ci* @version 1.0
189fa7767c5Sopenharmony_ci*/
190fa7767c5Sopenharmony_ciclass Buffer {
191fa7767c5Sopenharmony_cipublic:
192fa7767c5Sopenharmony_ci    /// Construct an empty buffer.
193fa7767c5Sopenharmony_ci    explicit Buffer(BufferMetaType type = BufferMetaType::AUDIO);
194fa7767c5Sopenharmony_ci
195fa7767c5Sopenharmony_ci    /// Destructor
196fa7767c5Sopenharmony_ci    ~Buffer() = default;
197fa7767c5Sopenharmony_ci
198fa7767c5Sopenharmony_ci    static std::shared_ptr<Buffer> CreateDefaultBuffer(BufferMetaType type, size_t capacity,
199fa7767c5Sopenharmony_ci                                                       std::shared_ptr<Allocator> allocator = nullptr,
200fa7767c5Sopenharmony_ci                                                       size_t align = 1);
201fa7767c5Sopenharmony_ci
202fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> WrapMemory(uint8_t* data, size_t capacity, size_t size);
203fa7767c5Sopenharmony_ci
204fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> WrapMemoryPtr(std::shared_ptr<uint8_t> data, size_t capacity, size_t size);
205fa7767c5Sopenharmony_ci
206fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align = 1);
207fa7767c5Sopenharmony_ci
208fa7767c5Sopenharmony_ci#if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
209fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> WrapSurfaceMemory(sptr<SurfaceBuffer> surfaceBuffer);
210fa7767c5Sopenharmony_ci#endif
211fa7767c5Sopenharmony_ci
212fa7767c5Sopenharmony_ci    uint32_t GetMemoryCount();
213fa7767c5Sopenharmony_ci
214fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> GetMemory(uint32_t index = 0);
215fa7767c5Sopenharmony_ci
216fa7767c5Sopenharmony_ci    std::shared_ptr<BufferMeta> GetBufferMeta();
217fa7767c5Sopenharmony_ci
218fa7767c5Sopenharmony_ci    void UpdateBufferMeta(const BufferMeta& bufferMeta);
219fa7767c5Sopenharmony_ci
220fa7767c5Sopenharmony_ci    void Reset();
221fa7767c5Sopenharmony_ci
222fa7767c5Sopenharmony_ci    /// no memory in the buffer.
223fa7767c5Sopenharmony_ci    bool IsEmpty();
224fa7767c5Sopenharmony_ci
225fa7767c5Sopenharmony_ci    void ChangeBufferMetaType(BufferMetaType type);
226fa7767c5Sopenharmony_ci
227fa7767c5Sopenharmony_ci    /// track index.
228fa7767c5Sopenharmony_ci    uint32_t trackID;
229fa7767c5Sopenharmony_ci
230fa7767c5Sopenharmony_ci    /// presentation timestamp of the buffer based on {@link HST_TIME_BASE}.
231fa7767c5Sopenharmony_ci    int64_t pts;
232fa7767c5Sopenharmony_ci
233fa7767c5Sopenharmony_ci    /// decoding timestamp of the buffer based on {@link HST_TIME_BASE}.
234fa7767c5Sopenharmony_ci    int64_t dts;
235fa7767c5Sopenharmony_ci
236fa7767c5Sopenharmony_ci    /// duration in time of the buffer data based on {@link HST_TIME_BASE}.
237fa7767c5Sopenharmony_ci    int64_t duration;
238fa7767c5Sopenharmony_ci
239fa7767c5Sopenharmony_ci    /// flag of the buffer, which is used to record extra information.
240fa7767c5Sopenharmony_ci    /// @see BUFFER_FLAG_EOS
241fa7767c5Sopenharmony_ci    uint64_t flag;
242fa7767c5Sopenharmony_ci
243fa7767c5Sopenharmony_ciprivate:
244fa7767c5Sopenharmony_ci    /// Data described by this buffer.
245fa7767c5Sopenharmony_ci    std::vector<std::shared_ptr<Memory>> data {};
246fa7767c5Sopenharmony_ci
247fa7767c5Sopenharmony_ci    /// The buffer meta information.
248fa7767c5Sopenharmony_ci    std::shared_ptr<BufferMeta> meta;
249fa7767c5Sopenharmony_ci};
250fa7767c5Sopenharmony_ci} // namespace Plugin
251fa7767c5Sopenharmony_ci} // namespace Media
252fa7767c5Sopenharmony_ci} // namespace OHOS
253fa7767c5Sopenharmony_ci#endif // HISTREAMER_PLUGIN_COMMON_BUFFER_H
254