• 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 "native_avbuffer.h"
17 #include <shared_mutex>
18 #include "common/log.h"
19 #include "common/native_mfmagic.h"
20 #include "meta/meta.h"
21 #include "native_window.h"
22 
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 
OH_AVBuffer_Create(int32_t capacity)26 OH_AVBuffer *OH_AVBuffer_Create(int32_t capacity)
27 {
28     FALSE_RETURN_V_MSG_E(capacity > 0, nullptr, "capacity %{public}d is error!", capacity);
29     auto allocator = AVAllocatorFactory::CreateSharedAllocator(MemoryFlag::MEMORY_READ_WRITE);
30     FALSE_RETURN_V_MSG_E(allocator != nullptr, nullptr, "create allocator failed");
31 
32     std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer(allocator, capacity);
33     FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "create OH_AVBuffer failed");
34     FALSE_RETURN_V_MSG_E(buffer->memory_ != nullptr, nullptr, "memory is nullptr");
35     FALSE_RETURN_V_MSG_E(buffer->memory_->GetAddr() != nullptr, nullptr, "memory's addr is nullptr");
36 
37     struct OH_AVBuffer *buf = new (std::nothrow) OH_AVBuffer(buffer);
38     FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "failed to new OH_AVBuffer");
39     buf->isUserCreated = true;
40     return buf;
41 }
42 
OH_AVBuffer_Destroy(struct OH_AVBuffer * buffer)43 OH_AVErrCode OH_AVBuffer_Destroy(struct OH_AVBuffer *buffer)
44 {
45     FALSE_RETURN_V_MSG_E(buffer != nullptr, AV_ERR_INVALID_VAL, "input buffer is nullptr!");
46     FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, AV_ERR_INVALID_VAL, "magic error!");
47     FALSE_RETURN_V_MSG_E(buffer->isUserCreated, AV_ERR_OPERATE_NOT_PERMIT, "input buffer is not user created!");
48     delete buffer;
49     return AV_ERR_OK;
50 }
51 
OH_AVBuffer_GetBufferAttr(OH_AVBuffer * buffer,OH_AVCodecBufferAttr * attr)52 OH_AVErrCode OH_AVBuffer_GetBufferAttr(OH_AVBuffer *buffer, OH_AVCodecBufferAttr *attr)
53 {
54     FALSE_RETURN_V_MSG_E(buffer != nullptr, AV_ERR_INVALID_VAL, "input buffer is nullptr!");
55     FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, AV_ERR_INVALID_VAL, "magic error!");
56     FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, AV_ERR_INVALID_VAL, "buffer is nullptr!");
57     FALSE_RETURN_V_MSG_E(attr != nullptr, AV_ERR_INVALID_VAL, "attr is nullptr!");
58     attr->pts = buffer->buffer_->pts_;
59     attr->flags = static_cast<uint32_t>(buffer->buffer_->flag_);
60     if (buffer->buffer_->memory_ != nullptr) {
61         attr->offset = buffer->buffer_->memory_->GetOffset();
62         attr->size = buffer->buffer_->memory_->GetSize();
63     } else {
64         attr->offset = 0;
65         attr->size = 0;
66     }
67     return AV_ERR_OK;
68 }
69 
OH_AVBuffer_SetBufferAttr(OH_AVBuffer * buffer,const OH_AVCodecBufferAttr * attr)70 OH_AVErrCode OH_AVBuffer_SetBufferAttr(OH_AVBuffer *buffer, const OH_AVCodecBufferAttr *attr)
71 {
72     FALSE_RETURN_V_MSG_E(buffer != nullptr, AV_ERR_INVALID_VAL, "input buffer is nullptr!");
73     FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, AV_ERR_INVALID_VAL, "magic error!");
74     FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, AV_ERR_INVALID_VAL, "buffer is nullptr!");
75     FALSE_RETURN_V_MSG_E(attr != nullptr, AV_ERR_INVALID_VAL, "attr is nullptr!");
76     buffer->buffer_->pts_ = attr->pts;
77     buffer->buffer_->flag_ = attr->flags;
78 
79     if (buffer->buffer_->memory_ != nullptr) {
80         Status ret = buffer->buffer_->memory_->SetSize(attr->size);
81         FALSE_RETURN_V_MSG_E(ret == Status::OK, AV_ERR_INVALID_VAL, "size is invalid!");
82 
83         ret = buffer->buffer_->memory_->SetOffset(attr->offset);
84         FALSE_RETURN_V_MSG_E(ret == Status::OK, AV_ERR_INVALID_VAL, "offset is invalid!");
85     }
86     return AV_ERR_OK;
87 }
88 
OH_AVBuffer_GetParameter(OH_AVBuffer * buffer)89 OH_AVFormat *OH_AVBuffer_GetParameter(OH_AVBuffer *buffer)
90 {
91     FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "input buffer is nullptr!");
92     FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, nullptr, "magic error!");
93     FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, nullptr, "buffer is nullptr!");
94     FALSE_RETURN_V_MSG_E(buffer->buffer_->meta_ != nullptr, nullptr, "buffer's meta is nullptr!");
95 
96     OH_AVFormat *avFormat = OH_AVFormat_Create();
97     if (!avFormat->format_.SetMeta(buffer->buffer_->meta_)) {
98         MEDIA_LOG_E("set meta failed!");
99         OH_AVFormat_Destroy(avFormat);
100         avFormat = nullptr;
101     }
102     return avFormat;
103 }
104 
OH_AVBuffer_SetParameter(OH_AVBuffer * buffer,const OH_AVFormat * format)105 OH_AVErrCode OH_AVBuffer_SetParameter(OH_AVBuffer *buffer, const OH_AVFormat *format)
106 {
107     FALSE_RETURN_V_MSG_E(buffer != nullptr, AV_ERR_INVALID_VAL, "input buffer is nullptr!");
108     FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, AV_ERR_INVALID_VAL, "magic error!");
109     FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, AV_ERR_INVALID_VAL, "buffer is nullptr!");
110     FALSE_RETURN_V_MSG_E(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
111     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
112 
113     auto formatRef = const_cast<OH_AVFormat *>(format);
114     std::shared_ptr<Meta> meta = formatRef->format_.GetMeta();
115     FALSE_RETURN_V_MSG_E(meta != nullptr, AV_ERR_INVALID_VAL, "input meta is nullptr!!");
116 
117     *(buffer->buffer_->meta_) = *(meta);
118     return AV_ERR_OK;
119 }
120 
OH_AVBuffer_GetAddr(OH_AVBuffer * buffer)121 uint8_t *OH_AVBuffer_GetAddr(OH_AVBuffer *buffer)
122 {
123     FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "input buffer is nullptr!");
124     FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, nullptr, "magic error!");
125     FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, nullptr, "buffer is nullptr!");
126     FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, nullptr, "buffer's memory is nullptr!");
127     return buffer->buffer_->memory_->GetAddr();
128 }
129 
OH_AVBuffer_GetCapacity(OH_AVBuffer * buffer)130 int32_t OH_AVBuffer_GetCapacity(OH_AVBuffer *buffer)
131 {
132     FALSE_RETURN_V_MSG_E(buffer != nullptr, -1, "input buffer is nullptr!");
133     FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, -1, "magic error!");
134     FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, -1, "buffer is nullptr!");
135     FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, -1, "buffer's memory is nullptr!");
136     return buffer->buffer_->memory_->GetCapacity();
137 }
138 
OH_AVBuffer_GetNativeBuffer(OH_AVBuffer * buffer)139 OH_NativeBuffer *OH_AVBuffer_GetNativeBuffer(OH_AVBuffer *buffer)
140 {
141     FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "input buffer is nullptr!");
142     FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, nullptr, "magic error!");
143     FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, nullptr, "buffer is nullptr!");
144     FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, nullptr, "buffer's memory is nullptr!");
145     sptr<SurfaceBuffer> surfaceBuffer = buffer->buffer_->memory_->GetSurfaceBuffer();
146     FALSE_RETURN_V_MSG_E(surfaceBuffer != nullptr, nullptr, "surfaceBuffer is nullptr!");
147     surfaceBuffer->IncStrongRef(surfaceBuffer.GetRefPtr());
148     return surfaceBuffer->SurfaceBufferToNativeBuffer();
149 }