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 "avcodeclist_impl.h"
17 #include "avcodec_errors.h"
18 #include "avcodec_log.h"
19 #include "i_avcodec_service.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecListImpl"};
23 }
24 namespace OHOS {
25 namespace MediaAVCodec {
CreateAVCodecList()26 std::shared_ptr<AVCodecList> AVCodecListFactory::CreateAVCodecList()
27 {
28 static std::shared_ptr<AVCodecListImpl> impl = std::make_shared<AVCodecListImpl>();
29 static bool initialized = false;
30 if (!initialized) {
31 int32_t ret = impl->Init();
32 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init AVCodecListImpl failed");
33 initialized = true;
34 }
35 return impl;
36 }
37
Init()38 int32_t AVCodecListImpl::Init()
39 {
40 codecListService_ = AVCodecServiceFactory::GetInstance().CreateCodecListService();
41 CHECK_AND_RETURN_RET_LOG(codecListService_ != nullptr, AVCS_ERR_UNKNOWN, "Create AVCodecList service failed");
42 return AVCS_ERR_OK;
43 }
44
AVCodecListImpl()45 AVCodecListImpl::AVCodecListImpl()
46 {
47 AVCODEC_LOGD("Create AVCodecList instances successful");
48 }
49
~AVCodecListImpl()50 AVCodecListImpl::~AVCodecListImpl()
51 {
52 if (codecListService_ != nullptr) {
53 (void)AVCodecServiceFactory::GetInstance().DestroyCodecListService(codecListService_);
54 codecListService_ = nullptr;
55 }
56 for (auto iter = nameAddrMap_.begin(); iter != nameAddrMap_.end(); iter++) {
57 if (iter->second != nullptr) {
58 free(iter->second);
59 iter->second = nullptr;
60 }
61 }
62 nameAddrMap_.clear();
63 for (auto addr : bufAddrSet_) {
64 if (addr != nullptr) {
65 delete[] addr;
66 }
67 }
68 bufAddrSet_.clear();
69 for (auto iter = mimeCapsMap_.begin(); iter != mimeCapsMap_.end(); iter++) {
70 std::string mime = iter->first;
71 for (uint32_t i = 0; i < mimeCapsMap_[mime].size(); i++) {
72 delete mimeCapsMap_[mime][i];
73 mimeCapsMap_[mime][i] = nullptr;
74 }
75 mimeCapsMap_[mime].clear();
76 }
77 mimeCapsMap_.clear();
78 AVCODEC_LOGD("Destroy AVCodecList instances successful");
79 }
80
FindDecoder(const Format & format)81 std::string AVCodecListImpl::FindDecoder(const Format &format)
82 {
83 return codecListService_->FindDecoder(format);
84 }
85
FindEncoder(const Format & format)86 std::string AVCodecListImpl::FindEncoder(const Format &format)
87 {
88 return codecListService_->FindEncoder(format);
89 }
90
GetCapability(const std::string & mime,const bool isEncoder,const AVCodecCategory & category)91 CapabilityData *AVCodecListImpl::GetCapability(const std::string &mime, const bool isEncoder,
92 const AVCodecCategory &category)
93 {
94 std::lock_guard<std::mutex> lock(mutex_);
95 bool isVendor = (category == AVCodecCategory::AVCODEC_SOFTWARE) ? false : true;
96 AVCodecType codecType = AVCODEC_TYPE_NONE;
97 bool isVideo = mime.find("video") != std::string::npos;
98 if (isVideo) {
99 codecType = isEncoder ? AVCODEC_TYPE_VIDEO_ENCODER : AVCODEC_TYPE_VIDEO_DECODER;
100 } else {
101 codecType = isEncoder ? AVCODEC_TYPE_AUDIO_ENCODER : AVCODEC_TYPE_AUDIO_DECODER;
102 }
103 if (mimeCapsMap_.find(mime) != mimeCapsMap_.end()) {
104 for (uint32_t i = 0; i < mimeCapsMap_[mime].size(); i++) {
105 if (mimeCapsMap_[mime][i]->codecType == codecType && mimeCapsMap_[mime][i]->isVendor == isVendor) {
106 return mimeCapsMap_[mime][i];
107 }
108 }
109 } else {
110 std::vector<CapabilityData *> capsArray;
111 mimeCapsMap_.insert(std::make_pair(mime, capsArray));
112 }
113 CapabilityData capaDataIn;
114 int32_t ret = codecListService_->GetCapability(capaDataIn, mime, isEncoder, category);
115 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Get capability failed");
116 std::string name = capaDataIn.codecName;
117 CHECK_AND_RETURN_RET_LOG(!name.empty(), nullptr, "Get capability failed");
118 if (category == AVCodecCategory::AVCODEC_NONE && nameAddrMap_.find(name) != nameAddrMap_.end()) {
119 for (uint32_t i = 0; i < mimeCapsMap_[mime].size(); i++) {
120 if (mimeCapsMap_[mime][i]->codecType == codecType && mimeCapsMap_[mime][i]->codecName == name) {
121 return mimeCapsMap_[mime][i];
122 }
123 }
124 }
125 CapabilityData *cap = new CapabilityData(capaDataIn);
126 mimeCapsMap_.at(mime).emplace_back(cap);
127 uint32_t idx = mimeCapsMap_[mime].size() - 1;
128 return mimeCapsMap_[mime][idx];
129 }
130
GetBuffer(const std::string & name,uint32_t sizeOfCap)131 void *AVCodecListImpl::GetBuffer(const std::string &name, uint32_t sizeOfCap)
132 {
133 std::lock_guard<std::mutex> lock(mutex_);
134 if (nameAddrMap_.find(name) != nameAddrMap_.end()) {
135 return nameAddrMap_[name];
136 }
137 CHECK_AND_RETURN_RET_LOG(sizeOfCap > 0, nullptr, "Get capability buffer failed: invalid size");
138 nameAddrMap_[name] = static_cast<void *>(malloc(sizeOfCap));
139 return nameAddrMap_[name];
140 }
141
NewBuffer(size_t bufSize)142 void *AVCodecListImpl::NewBuffer(size_t bufSize)
143 {
144 std::lock_guard<std::mutex> lock(mutex_);
145 CHECK_AND_RETURN_RET_LOG(bufSize > 0, nullptr, "new buffer failed: invalid size");
146 uint8_t *temp = new uint8_t[bufSize];
147 CHECK_AND_RETURN_RET_LOG(temp != nullptr, nullptr, "new buffer failed: no memory");
148
149 bufAddrSet_.insert(temp);
150 return static_cast<void *>(temp);
151 }
152
DeleteBuffer(void * bufAddr)153 void AVCodecListImpl::DeleteBuffer(void *bufAddr)
154 {
155 std::lock_guard<std::mutex> lock(mutex_);
156 uint8_t *temp = static_cast<uint8_t *>(bufAddr);
157 bufAddrSet_.erase(temp);
158 delete[] temp;
159 }
160 } // namespace MediaAVCodec
161 } // namespace OHOS