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