1 /*
2 * Copyright (c) 2023-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 #if defined(VIDEO_SUPPORT)
16
17 #define HST_LOG_TAG "CodecBuffer"
18
19 #include "codec_buffer.h"
20 #include "codec_utils.h"
21 #include "plugin/common/surface_memory.h"
22
23 namespace OHOS {
24 namespace Media {
25 namespace Plugin {
26 namespace CodecAdapter {
CodecBuffer(std::shared_ptr<Buffer> & buffer,CompVerInfo & verInfo,bool isInput,uint32_t bufferSize,MemoryType bufMemType)27 CodecBuffer::CodecBuffer(std::shared_ptr<Buffer>& buffer, CompVerInfo& verInfo,
28 bool isInput, uint32_t bufferSize, MemoryType bufMemType)
29 : buffer_(buffer), verInfo_(verInfo)
30 {
31 Init(isInput, bufferSize, bufMemType);
32 }
33
Init(bool isInput,uint32_t bufferSize,MemoryType bufMemType)34 void CodecBuffer::Init(bool isInput, uint32_t bufferSize, MemoryType bufMemType)
35 {
36 MEDIA_LOG_DD("CodecBuffer Init Start");
37 omxBuffer_ = std::make_shared<OmxCodecBuffer>();
38 omxBuffer_->size = sizeof(OmxCodecBuffer);
39 omxBuffer_->version.s.nVersionMajor = verInfo_.compVersion.s.nVersionMajor;
40 omxBuffer_->fenceFd = -1; // check use -1 first with no window
41 omxBuffer_->pts = 0;
42 omxBuffer_->flag = 0;
43 omxBuffer_->bufferType = GetOmxBufferType(bufMemType, isInput);
44 omxBuffer_->allocLen = bufferSize;
45 omxBuffer_->bufferLen = 0;
46 omxBuffer_->buffer = nullptr;
47 memory_ = buffer_->GetMemory();
48 FALSE_RETURN(memory_ != nullptr);
49 omxBuffer_->allocLen = memory_->GetCapacity();
50 switch (bufMemType) {
51 case MemoryType::SURFACE_BUFFER: {
52 BufferHandle* bufferHandle =
53 std::static_pointer_cast<Plugin::SurfaceMemory>(memory_)->GetSurfaceBuffer()->GetBufferHandle();
54 FALSE_LOG_MSG(bufferHandle != nullptr, "bufferHandle is null");
55 omxBuffer_->bufferLen =
56 sizeof(BufferHandle) + (sizeof(int32_t) * (bufferHandle->reserveFds + bufferHandle->reserveInts));
57 omxBuffer_->buffer = reinterpret_cast<uint8_t *>(bufferHandle);
58 break;
59 }
60 case MemoryType::SHARE_MEMORY: {
61 omxBuffer_->bufferLen = sizeof(int);
62 omxBuffer_->type = isInput ? READ_ONLY_TYPE : READ_WRITE_TYPE;
63 omxBuffer_->buffer = reinterpret_cast<uint8_t *>(static_cast<long long>(
64 std::static_pointer_cast<Plugin::ShareMemory>(memory_)->GetShareMemoryFd()));
65 MEDIA_LOG_D("share memory fd: " PUBLIC_LOG_D32,
66 std::static_pointer_cast<Plugin::ShareMemory>(memory_)->GetShareMemoryFd());
67 break;
68 }
69 default:
70 MEDIA_LOG_W("UnKnow MemoryType: " PUBLIC_LOG_D32, (int)memory_->GetMemoryType());
71 break;
72 }
73 }
74
GetOmxBuffer()75 std::shared_ptr<OmxCodecBuffer> CodecBuffer::GetOmxBuffer()
76 {
77 return omxBuffer_;
78 }
79
GetBufferId() const80 uint32_t CodecBuffer::GetBufferId() const
81 {
82 return omxBuffer_->bufferId;
83 }
84
Copy(const std::shared_ptr<Plugin::Buffer> & pluginBuffer)85 Status CodecBuffer::Copy(const std::shared_ptr<Plugin::Buffer>& pluginBuffer)
86 {
87 omxBuffer_->flag = Translate2omxFlagSet(pluginBuffer->flag);
88 omxBuffer_->pts = pluginBuffer->pts;
89 MEDIA_LOG_DD("plugin flag: " PUBLIC_LOG_U32 ", pts: " PUBLIC_LOG_D64, omxBuffer_->flag, omxBuffer_->pts);
90 if (pluginBuffer->flag & BUFFER_FLAG_EOS) {
91 MEDIA_LOG_D("EOS flag receive, return");
92 return Status::OK;
93 }
94 auto mem = pluginBuffer->GetMemory();
95 if (mem == nullptr) {
96 MEDIA_LOG_DD("pluginBuffer->GetMemory() return nullptr");
97 return Status::ERROR_INVALID_DATA;
98 }
99 const uint8_t* memAddr = mem->GetReadOnlyData();
100 if (memAddr == nullptr) {
101 MEDIA_LOG_DD("mem->GetReadOnlyData() return nullptr");
102 return Status::ERROR_INVALID_DATA;
103 }
104 memory_->Write(memAddr, mem->GetSize(), 0);
105 omxBuffer_->offset = 0;
106 omxBuffer_->filledLen = mem->GetSize();
107 MEDIA_LOG_DD("CopyBuffer end, bufferId: " PUBLIC_LOG_U32, omxBuffer_->bufferId);
108 return Status::OK;
109 }
110
Rebind(const std::shared_ptr<Plugin::Buffer> & pluginBuffer)111 Status CodecBuffer::Rebind(const std::shared_ptr<Plugin::Buffer>& pluginBuffer)
112 {
113 MEDIA_LOG_DD("Rebind Start");
114 omxBuffer_->flag = Translate2omxFlagSet(pluginBuffer->flag);
115 omxBuffer_->pts = pluginBuffer->pts;
116 MEDIA_LOG_DD("plugin flag: " PUBLIC_LOG_U32 ", pts: " PUBLIC_LOG_D64, omxBuffer_->flag, omxBuffer_->pts);
117 memory_ = pluginBuffer->GetMemory();
118 FALSE_RETURN_V_MSG_E(memory_ != nullptr, Status::ERROR_NULL_POINTER, "Call pluginBuffer->GetMemory() failed.");
119 switch (memory_->GetMemoryType()) {
120 case MemoryType::SURFACE_BUFFER: {
121 auto outMem = std::static_pointer_cast<Plugin::SurfaceMemory>(memory_);
122 auto surfaceBuf = outMem->GetSurfaceBuffer();
123 FALSE_RETURN_V_MSG_E(surfaceBuf != nullptr, Status::ERROR_NULL_POINTER, "GetSurfaceBuffer failed");
124 BufferHandle* bufferHandle = surfaceBuf->GetBufferHandle();
125 FALSE_RETURN_V_MSG_E(bufferHandle != nullptr, Status::ERROR_NULL_POINTER, "GetBufferHandle failed");
126 omxBuffer_->bufferLen =
127 sizeof(BufferHandle) + sizeof(int32_t) * (bufferHandle->reserveFds + bufferHandle->reserveInts);
128 omxBuffer_->buffer = reinterpret_cast<uint8_t *>(bufferHandle);
129 break;
130 }
131 case MemoryType::SHARE_MEMORY:
132 omxBuffer_->bufferLen = sizeof(int);
133 omxBuffer_->buffer = reinterpret_cast<uint8_t *>(static_cast<long long>(
134 std::static_pointer_cast<Plugin::ShareMemory>(memory_)->GetShareMemoryFd()));
135 omxBuffer_->offset = 0;
136 omxBuffer_->filledLen = 0;
137 break;
138 case MemoryType::VIRTUAL_ADDR:
139 MEDIA_LOG_E("Rebind pluginBuffer failed, MemoryType is VIRTUAL_ADDR");
140 break;
141 default:
142 MEDIA_LOG_E("MemoryType invalid!");
143 break;
144 }
145 // 这里buffer需要保存一下,为了方便往下一节点传数据,通过GetBuffer()获取
146 buffer_ = pluginBuffer;
147 MEDIA_LOG_DD("Rebind end, omxBufferId: " PUBLIC_LOG_U32, omxBuffer_->bufferId);
148 return Status::OK;
149 }
150
151 /**
152 * Receive buffer_ with data contained in CodecBuffer for transmission to the filter
153 * @param buffer Copy the value of buffer_
154 * @param omxBuffer
155 * @return
156 */
Unbind(std::shared_ptr<Plugin::Buffer> & buffer,const OmxCodecBuffer * omxBuffer)157 Status CodecBuffer::Unbind(std::shared_ptr<Plugin::Buffer>& buffer, const OmxCodecBuffer* omxBuffer)
158 {
159 // 因为Rebind()里面用buffer_保存了PluginBuf,所以这里的buffer_需要主动释放,减少智能指针的引用计数
160 // decoder 时,PluginBuf 的真正释放时机应该是在sink节点,该数据送显后才能释放
161 FALSE_RETURN_V_MSG_E(memory_ != nullptr, Status::ERROR_NULL_POINTER, "Param memory_ is nullptr");
162 if (memory_->GetMemoryType() == MemoryType::SHARE_MEMORY) {
163 memory_->UpdateDataSize(static_cast<size_t>(omxBuffer->filledLen - omxBuffer->offset), 0);
164 }
165 buffer = buffer_;
166 buffer->flag = Translate2PluginFlagSet(omxBuffer->flag);
167 buffer->pts = omxBuffer->pts;
168 buffer_ = nullptr;
169 return Status::OK;
170 }
171 } // namespace CodecAdapter
172 } // namespace Plugin
173 } // namespace Media
174 } // namespace OHOS
175 #endif