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 "avcodec_log.h"
17 #include "avcodec_errors.h"
18 #include "i_avcodec_service.h"
19 #include "avcodeclist_impl.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 iter = mimeCapsMap_.begin(); iter != mimeCapsMap_.end(); iter++) {
64 std::string mime = iter->first;
65 for (uint32_t i = 0; i < mimeCapsMap_[mime].size(); i++) {
66 delete mimeCapsMap_[mime][i];
67 mimeCapsMap_[mime][i] = nullptr;
68 }
69 mimeCapsMap_[mime].clear();
70 }
71 mimeCapsMap_.clear();
72 AVCODEC_LOGD("Destroy AVCodecList instances successful");
73 }
74
FindDecoder(const Format & format)75 std::string AVCodecListImpl::FindDecoder(const Format &format)
76 {
77 return codecListService_->FindDecoder(format);
78 }
79
FindEncoder(const Format & format)80 std::string AVCodecListImpl::FindEncoder(const Format &format)
81 {
82 return codecListService_->FindEncoder(format);
83 }
84
GetCapability(const std::string & mime,const bool isEncoder,const AVCodecCategory & category)85 CapabilityData *AVCodecListImpl::GetCapability(const std::string &mime, const bool isEncoder,
86 const AVCodecCategory &category)
87 {
88 std::lock_guard<std::mutex> lock(mutex_);
89 bool isVendor = (category == AVCodecCategory::AVCODEC_SOFTWARE) ? false : true;
90 AVCodecType codecType = AVCODEC_TYPE_NONE;
91 bool isVideo = mime.find("video") != std::string::npos;
92 if (isVideo) {
93 codecType = isEncoder ? AVCODEC_TYPE_VIDEO_ENCODER : AVCODEC_TYPE_VIDEO_DECODER;
94 } else {
95 codecType = isEncoder ? AVCODEC_TYPE_AUDIO_ENCODER : AVCODEC_TYPE_AUDIO_DECODER;
96 }
97 if (mimeCapsMap_.find(mime) != mimeCapsMap_.end()) {
98 for (uint32_t i = 0; i < mimeCapsMap_[mime].size(); i++) {
99 if (mimeCapsMap_[mime][i]->codecType == codecType && mimeCapsMap_[mime][i]->isVendor == isVendor) {
100 return mimeCapsMap_[mime][i];
101 }
102 }
103 } else {
104 std::vector<CapabilityData *> capsArray;
105 mimeCapsMap_.insert(std::make_pair(mime, capsArray));
106 }
107 CapabilityData capaDataIn;
108 int32_t ret = codecListService_->GetCapability(capaDataIn, mime, isEncoder, category);
109 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Get capability failed");
110 std::string name = capaDataIn.codecName;
111 CHECK_AND_RETURN_RET_LOG(!name.empty(), nullptr, "Get capability failed");
112 if (category == AVCodecCategory::AVCODEC_NONE && nameAddrMap_.find(name) != nameAddrMap_.end()) {
113 for (uint32_t i = 0; i < mimeCapsMap_[mime].size(); i++) {
114 if (mimeCapsMap_[mime][i]->codecType == codecType && mimeCapsMap_[mime][i]->codecName == name) {
115 return mimeCapsMap_[mime][i];
116 }
117 }
118 }
119 CapabilityData *cap = new CapabilityData(capaDataIn);
120 mimeCapsMap_.at(mime).emplace_back(cap);
121 uint32_t idx = mimeCapsMap_[mime].size() - 1;
122 return mimeCapsMap_[mime][idx];
123 }
124
GetBuffer(const std::string & name,uint32_t sizeOfCap)125 void *AVCodecListImpl::GetBuffer(const std::string &name, uint32_t sizeOfCap)
126 {
127 std::lock_guard<std::mutex> lock(mutex_);
128 if (nameAddrMap_.find(name) != nameAddrMap_.end()) {
129 return nameAddrMap_[name];
130 }
131 CHECK_AND_RETURN_RET_LOG(sizeOfCap > 0, nullptr, "Get capability buffer failed: invalid size");
132 nameAddrMap_[name] = (void *)malloc(sizeOfCap);
133 return nameAddrMap_[name];
134 }
135 } // namespace MediaAVCodec
136 } // namespace OHOS