• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 
18 namespace OHOS {
19 namespace MediaAVCodec {
20 namespace Plugin {
Memory(size_t capacity,std::shared_ptr<uint8_t> bufData,size_t align,MemoryType type)21 Memory::Memory(size_t capacity, std::shared_ptr<uint8_t> bufData, size_t align, MemoryType type)
22     : memoryType(type), capacity(capacity), alignment(align),
23       offset(0), size(0), allocator(nullptr), addr(std::move(bufData))
24 {
25 }
26 
Memory(size_t capacity,std::shared_ptr<Allocator> allocator,size_t align,MemoryType type,bool allocMem)27 Memory::Memory(size_t capacity, std::shared_ptr<Allocator> allocator, size_t align, MemoryType type, bool allocMem)
28     : memoryType(type), capacity(capacity), alignment(align), offset(0),
29       size(0), allocator(std::move(allocator)), addr(nullptr)
30 {
31     if (!allocMem) {
32         return;
33     }
34     size_t allocSize = align ? (capacity + align - 1) : capacity;
35     if (this->allocator) {
36         addr = std::shared_ptr<uint8_t>(static_cast<uint8_t*>(this->allocator->Alloc(allocSize)),
37                                         [this](uint8_t* ptr) { this->allocator->Free(static_cast<void*>(ptr)); });
38     } else {
39         addr = std::shared_ptr<uint8_t>(new uint8_t[allocSize], std::default_delete<uint8_t[]>());
40     }
41     offset = static_cast<size_t>(AlignUp(reinterpret_cast<uintptr_t>(addr.get()), static_cast<uintptr_t>(align)) -
42         reinterpret_cast<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     size_t maxLength = size;
75     if (position != INVALID_POSITION) {
76         start = std::min(position, size);
77         maxLength = size - start;
78     }
79     size_t length = std::min(readSize, maxLength);
80     if (memcpy_s(out, length, GetRealAddr() + start, length) != EOK) {
81         return 0;
82     }
83     return length;
84 }
85 
GetReadOnlyData(size_t position)86 const uint8_t* Memory::GetReadOnlyData(size_t position)
87 {
88     if (position > capacity) {
89         return nullptr;
90     }
91     return GetRealAddr() + position;
92 }
93 
GetWritableAddr(size_t estimatedWriteSize,size_t position)94 uint8_t* Memory::GetWritableAddr(size_t estimatedWriteSize, size_t position)
95 {
96     if (position + estimatedWriteSize > capacity) {
97         return nullptr;
98     }
99     uint8_t* ptr = GetRealAddr() + position;
100     size = (estimatedWriteSize + position);
101     return ptr;
102 }
103 
UpdateDataSize(size_t realWriteSize,size_t position)104 void Memory::UpdateDataSize(size_t realWriteSize, size_t position)
105 {
106     if (position + realWriteSize > capacity) {
107         return;
108     }
109     size = (realWriteSize + position);
110 }
111 
GetSize()112 size_t Memory::GetSize()
113 {
114     return size;
115 }
116 
GetRealAddr() const117 uint8_t* Memory::GetRealAddr() const
118 {
119     return addr.get() + offset;
120 }
121 
GetMemoryType()122 MemoryType Memory::GetMemoryType()
123 {
124     return memoryType;
125 }
126 
Buffer(BufferMetaType type)127 Buffer::Buffer(BufferMetaType type) : trackID(0), pts(0), dts(0), duration(0), flag (0)
128 {
129     (void)type;
130 }
131 
CreateDefaultBuffer(BufferMetaType type,size_t capacity,std::shared_ptr<Allocator> allocator,size_t align)132 std::shared_ptr<Buffer> Buffer::CreateDefaultBuffer(BufferMetaType type, size_t capacity,
133                                                     std::shared_ptr<Allocator> allocator, size_t align)
134 {
135     auto buffer = std::make_shared<Buffer>(type);
136     std::shared_ptr<Memory> memory = std::shared_ptr<Memory>(new Memory(capacity, allocator, align));
137     buffer->data.push_back(memory);
138     return buffer;
139 }
140 
WrapMemory(uint8_t * dataptr,size_t capacity,size_t size)141 std::shared_ptr<Memory> Buffer::WrapMemory(uint8_t* dataptr, size_t capacity, size_t size)
142 {
143     auto memory = std::shared_ptr<Memory>(
144             new Memory(capacity, std::shared_ptr<uint8_t>(dataptr, [](void* ptr) {(void)ptr;})));
145     memory->size = size;
146     this->data.push_back(memory);
147     return memory;
148 }
149 
WrapMemoryPtr(std::shared_ptr<uint8_t> dataptr,size_t capacity,size_t size)150 std::shared_ptr<Memory> Buffer::WrapMemoryPtr(std::shared_ptr<uint8_t> dataptr, size_t capacity, size_t size)
151 {
152     auto memory = std::shared_ptr<Memory>(new Memory(capacity, dataptr));
153     memory->size = size;
154     this->data.push_back(memory);
155     return memory;
156 }
157 
AllocMemory(std::shared_ptr<Allocator> allocator,size_t capacity,size_t align)158 std::shared_ptr<Memory> Buffer::AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align)
159 {
160     auto type = (allocator != nullptr) ? allocator->GetMemoryType() : MemoryType::VIRTUAL_ADDR;
161     std::shared_ptr<Memory> memory = nullptr;
162     switch (type) {
163         case MemoryType::VIRTUAL_ADDR: {
164             memory = std::shared_ptr<Memory>(new Memory(capacity, allocator, align));
165             break;
166         }
167         default:
168             break;
169     }
170     if (memory == nullptr) {
171         return nullptr;
172     }
173     data.push_back(memory);
174     return memory;
175 }
176 
GetMemoryCount()177 uint32_t Buffer::GetMemoryCount()
178 {
179     return data.size();
180 }
181 
GetMemory(uint32_t index)182 std::shared_ptr<Memory> Buffer::GetMemory(uint32_t index)
183 {
184     if (data.size() <= index) {
185         return nullptr;
186     }
187     return data[index];
188 }
189 
190 
IsEmpty()191 bool Buffer::IsEmpty()
192 {
193     return data.empty();
194 }
195 
Reset()196 void Buffer::Reset()
197 {
198     data[0]->Reset();
199     trackID = 0;
200     pts = 0;
201     dts = 0;
202     duration = 0;
203     flag = 0;
204 }
205 } // namespace Plugin
206 } // namespace MediaAVCodec
207 } // namespace OHOS
208