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 #include "plugin_buffer.h"
17 #include "surface_memory.h"
18
19 namespace OHOS {
20 namespace Media {
21 namespace Plugin {
Memory(size_t capacity,std::shared_ptr<uint8_t> bufData,size_t align,MemoryType type)22 Memory::Memory(size_t capacity, std::shared_ptr<uint8_t> bufData, size_t align, MemoryType type)
23 : memoryType(type), capacity(capacity), alignment(align),
24 offset(0), size(0), allocator(nullptr), addr(std::move(bufData))
25 {
26 }
27
Memory(size_t capacity,std::shared_ptr<Allocator> allocator,size_t align,MemoryType type,bool allocMem)28 Memory::Memory(size_t capacity, std::shared_ptr<Allocator> allocator, size_t align, MemoryType type, bool allocMem)
29 : memoryType(type), capacity(capacity), alignment(align), offset(0),
30 size(0), allocator(std::move(allocator)), addr(nullptr)
31 {
32 if (!allocMem) { // SurfaceMemory alloc mem in subclass
33 return;
34 }
35 size_t allocSize = align ? (capacity + align - 1) : capacity;
36 if (this->allocator) {
37 addr = std::shared_ptr<uint8_t>(static_cast<uint8_t*>(this->allocator->Alloc(allocSize)),
38 [this](uint8_t* ptr) { this->allocator->Free((void*)ptr); });
39 } else {
40 addr = std::shared_ptr<uint8_t>(new uint8_t[allocSize], std::default_delete<uint8_t[]>());
41 }
42 offset = static_cast<size_t>(AlignUp((uintptr_t)addr.get(), (uintptr_t)align) - (uintptr_t)addr.get());
43 }
44
GetCapacity()45 size_t Memory::GetCapacity()
46 {
47 return capacity;
48 }
49
Reset()50 void Memory::Reset()
51 {
52 this->size = 0;
53 }
54
Write(const uint8_t * in,size_t writeSize,size_t position)55 size_t Memory::Write(const uint8_t* in, size_t writeSize, size_t position)
56 {
57 size_t start = 0;
58 if (position == INVALID_POSITION) {
59 start = size;
60 } else {
61 start = std::min(position, capacity);
62 }
63 size_t length = std::min(writeSize, capacity - start);
64 if (memcpy_s(GetRealAddr() + start, length, in, length) != EOK) {
65 return 0;
66 }
67 size = start + length;
68 return length;
69 }
70
Read(uint8_t * out,size_t readSize,size_t position)71 size_t Memory::Read(uint8_t* out, size_t readSize, size_t position)
72 {
73 size_t start = 0;
74 if (position != INVALID_POSITION) {
75 start = std::min(position, capacity);
76 }
77 size_t length = std::min(readSize, size);
78 if (memcpy_s(out, length, GetRealAddr() + start, length) != EOK) {
79 return 0;
80 }
81 return length;
82 }
83
GetReadOnlyData(size_t position)84 const uint8_t* Memory::GetReadOnlyData(size_t position)
85 {
86 if (position > capacity) {
87 return nullptr;
88 }
89 return GetRealAddr() + position;
90 }
91
GetWritableAddr(size_t estimatedWriteSize,size_t position)92 uint8_t* Memory::GetWritableAddr(size_t estimatedWriteSize, size_t position)
93 {
94 if (position + estimatedWriteSize > capacity) {
95 return nullptr;
96 }
97 uint8_t* ptr = GetRealAddr() + position;
98 size = (estimatedWriteSize + position);
99 return ptr;
100 }
101
UpdateDataSize(size_t realWriteSize,size_t position)102 void Memory::UpdateDataSize(size_t realWriteSize, size_t position)
103 {
104 if (position + realWriteSize > capacity) {
105 return;
106 }
107 size = (realWriteSize + position);
108 }
109
GetSize()110 size_t Memory::GetSize()
111 {
112 return size;
113 }
114
GetRealAddr() const115 uint8_t* Memory::GetRealAddr() const
116 {
117 return addr.get() + offset;
118 }
119
GetMemoryType()120 MemoryType Memory::GetMemoryType()
121 {
122 return memoryType;
123 }
124
BufferMeta(BufferMetaType type)125 BufferMeta::BufferMeta(BufferMetaType type) : type(type)
126 {
127 }
128
GetMeta(Tag tag)129 ValueType BufferMeta::GetMeta(Tag tag)
130 {
131 if (tags) {
132 return (*tags.get())[tag];
133 }
134 return ValueType();
135 }
136
SetMeta(Tag tag,ValueType value)137 void BufferMeta::SetMeta(Tag tag, ValueType value)
138 {
139 if (!tags) {
140 tags = std::make_shared<TagMap>();
141 }
142 (*tags.get())[tag] = value;
143 }
144
GetType() const145 BufferMetaType BufferMeta::GetType() const
146 {
147 return type;
148 }
149
Buffer(BufferMetaType type)150 Buffer::Buffer(BufferMetaType type) : trackID(0), pts(0), dts(0), duration(0), flag (0), meta()
151 {
152 if (type == BufferMetaType::AUDIO) {
153 meta = std::shared_ptr<AudioBufferMeta>(new AudioBufferMeta());
154 } else if (type == BufferMetaType::VIDEO) {
155 meta = std::shared_ptr<VideoBufferMeta>(new VideoBufferMeta());
156 }
157 }
158
CreateDefaultBuffer(BufferMetaType type,size_t capacity,std::shared_ptr<Allocator> allocator,size_t align)159 std::shared_ptr<Buffer> Buffer::CreateDefaultBuffer(BufferMetaType type, size_t capacity,
160 std::shared_ptr<Allocator> allocator, size_t align)
161 {
162 auto buffer = std::make_shared<Buffer>(type);
163 std::shared_ptr<Memory> memory = std::shared_ptr<Memory>(new Memory(capacity, allocator, align));
164 buffer->data.push_back(memory);
165 return buffer;
166 }
167
WrapMemory(uint8_t * data,size_t capacity,size_t size)168 std::shared_ptr<Memory> Buffer::WrapMemory(uint8_t* data, size_t capacity, size_t size)
169 {
170 auto memory = std::shared_ptr<Memory>(new Memory(capacity, std::shared_ptr<uint8_t>(data, [](void* ptr) {})));
171 memory->size = size;
172 this->data.push_back(memory);
173 return memory;
174 }
175
WrapMemoryPtr(std::shared_ptr<uint8_t> data,size_t capacity,size_t size)176 std::shared_ptr<Memory> Buffer::WrapMemoryPtr(std::shared_ptr<uint8_t> data, size_t capacity, size_t size)
177 {
178 auto memory = std::shared_ptr<Memory>(new Memory(capacity, data));
179 memory->size = size;
180 this->data.push_back(memory);
181 return memory;
182 }
183
AllocMemory(std::shared_ptr<Allocator> allocator,size_t capacity,size_t align)184 std::shared_ptr<Memory> Buffer::AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align)
185 {
186 auto type = (allocator != nullptr) ? allocator->GetMemoryType() : MemoryType::VIRTUAL_ADDR;
187 std::shared_ptr<Memory> memory = nullptr;
188 switch (type) {
189 case MemoryType::VIRTUAL_ADDR: {
190 memory = std::shared_ptr<Memory>(new Memory(capacity, allocator, align));
191 break;
192 }
193 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
194 case MemoryType::SURFACE_BUFFER: {
195 memory = std::shared_ptr<Memory>(new SurfaceMemory(capacity, allocator, align));
196 break;
197 }
198 #endif
199 case MemoryType::SHARE_MEMORY:
200 break;
201 default:
202 break;
203 }
204 if (memory == nullptr) {
205 return nullptr;
206 }
207 data.push_back(memory);
208 return memory;
209 }
210
GetMemoryCount()211 uint32_t Buffer::GetMemoryCount()
212 {
213 return data.size();
214 }
215
GetMemory(uint32_t index)216 std::shared_ptr<Memory> Buffer::GetMemory(uint32_t index)
217 {
218 if (data.size() <= index) {
219 return nullptr;
220 }
221 return data[index];
222 }
223
GetBufferMeta()224 std::shared_ptr<BufferMeta> Buffer::GetBufferMeta()
225 {
226 return meta;
227 }
228
IsEmpty()229 bool Buffer::IsEmpty()
230 {
231 return data.empty();
232 }
233
Reset()234 void Buffer::Reset()
235 {
236 data[0]->Reset();
237 trackID = 0;
238 pts = 0;
239 dts = 0;
240 duration = 0;
241 flag = 0;
242 BufferMetaType type = meta->GetType();
243 meta.reset();
244 if (type == BufferMetaType::AUDIO) {
245 meta = std::shared_ptr<AudioBufferMeta>(new AudioBufferMeta());
246 } else if (type == BufferMetaType::VIDEO) {
247 meta = std::shared_ptr<VideoBufferMeta>(new VideoBufferMeta());
248 }
249 }
250 } // namespace Plugin
251 } // namespace Media
252 } // namespace OHOS
253