1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NdkMediaCodecStore"
19
20 #include "NdkMediaCodecInfoPriv.h"
21
22 #include <media/NdkMediaCodecStore.h>
23 #include <media/NdkMediaFormatPriv.h>
24
25 #include <media/IMediaCodecList.h>
26
27 #include <media/MediaCodecInfo.h>
28 #include <media/stagefright/foundation/AMessage.h>
29 #include <media/stagefright/foundation/ADebug.h>
30 #include <media/stagefright/MediaCodecConstants.h>
31 #include <media/stagefright/MediaCodecList.h>
32
33 using namespace android;
34
35 static sp<IMediaCodecList> sCodecList;
36 static std::vector<AMediaCodecSupportedMediaType> sMediaTypes;
37 static std::vector<AMediaCodecInfo> sCodecInfos;
38
39 static std::map<std::string, AMediaCodecInfo> sNameToInfoMap;
40 static std::map<std::string, std::vector<AMediaCodecInfo>> sTypeToInfoList;
41
initMediaTypes()42 static void initMediaTypes() {
43 if (sCodecList == nullptr) {
44 sCodecList = MediaCodecList::getInstance();
45 }
46
47 std::map<std::string, AMediaCodecSupportedMediaType> typesInfoMap;
48 std::vector<std::string> mediaTypes; // Keep the order of media types appearing in sCodecList.
49 for (size_t idx = 0; idx < sCodecList->countCodecs(); idx++) {
50 sp<MediaCodecInfo> codecInfo = sCodecList->getCodecInfo(idx);
51 if (codecInfo == nullptr) {
52 ALOGW("NULL MediaCodecInfo in MediaCodecList");
53 continue;
54 }
55 Vector<AString> codecMediaTypes;
56 codecInfo->getSupportedMediaTypes(&codecMediaTypes);
57 for (AString codecMediaType : codecMediaTypes) {
58 std::string mediaType = std::string(codecMediaType.c_str());
59
60 // Excludes special codecs from NDK
61 const std::shared_ptr<CodecCapabilities> codecCaps
62 = codecInfo->getCodecCapsFor(mediaType.c_str());
63 if (codecCaps->isFeatureSupported(FEATURE_SpecialCodec)) {
64 continue;
65 }
66
67 auto it = typesInfoMap.find(mediaType);
68 if (it == typesInfoMap.end()) {
69 AMediaCodecSupportedMediaType supportedType = { mediaType.c_str(), 0 };
70 it = typesInfoMap.emplace(mediaType, supportedType).first;
71 mediaTypes.push_back(mediaType);
72 }
73 uint32_t &mode = it->second.mMode;
74 mode |= (codecInfo->isEncoder() ? AMediaCodecSupportedMediaType::FLAG_ENCODER
75 : AMediaCodecSupportedMediaType::FLAG_DECODER);
76 }
77 }
78
79 // sMediaTypes keeps the order of media types appearing in sCodecList.
80 for (std::string &type : mediaTypes) {
81 sMediaTypes.push_back(typesInfoMap.find(type)->second);
82 }
83 }
84
initCodecInfoMap()85 static void initCodecInfoMap() {
86 if (sCodecList == nullptr) {
87 sCodecList = MediaCodecList::getInstance();
88 }
89
90 for (size_t idx = 0; idx < sCodecList->countCodecs(); idx++) {
91 sp<MediaCodecInfo> codecInfo = sCodecList->getCodecInfo(idx);
92 if (codecInfo == nullptr) {
93 ALOGW("NULL MediaCodecInfo in MediaCodecList");
94 continue;
95 }
96
97 Vector<AString> codecMediaTypes;
98 codecInfo->getSupportedMediaTypes(&codecMediaTypes);
99 bool useTypeSuffix = codecMediaTypes.size() > 1;
100 for (AString codecMediaType : codecMediaTypes) {
101 std::string mediaType = std::string(codecMediaType.c_str());
102
103 // Excludes special codecs from NDK
104 const std::shared_ptr<CodecCapabilities> codecCaps
105 = codecInfo->getCodecCapsFor(mediaType.c_str());
106 if (codecCaps->isFeatureSupported(FEATURE_SpecialCodec)) {
107 continue;
108 }
109
110 // get the type name after the slash. e.g. video/x.on2.vp8
111 size_t slashIx = mediaType.find_last_of('/');
112 if (slashIx == std::string::npos) {
113 slashIx = 0;
114 } else {
115 slashIx++;
116 }
117 std::string ndkBaseName = std::string(codecInfo->getCodecName());
118 if (useTypeSuffix) {
119 // If there are multiple supported media types,
120 // add the type to the end of the name to disambiguate names.
121 ndkBaseName += "." + mediaType.substr(slashIx);
122 }
123
124 int32_t copyIx = 0;
125 std::string ndkName;
126 // if a name is already registered,
127 // add ".1", ".2", ... at the end to disambiguate names.
128 while (true) {
129 ndkName = ndkBaseName;
130 if (copyIx > 0) {
131 ndkName += "." + std::to_string(copyIx);
132 }
133 if (!sNameToInfoMap.contains(ndkName)) {
134 break;
135 }
136 copyIx++;
137 }
138
139 AMediaCodecInfo info = AMediaCodecInfo(ndkName, codecInfo, codecCaps, mediaType);
140 sCodecInfos.push_back(info);
141 sNameToInfoMap.emplace(ndkName, info);
142
143 auto it = sTypeToInfoList.find(mediaType);
144 if (it == sTypeToInfoList.end()) {
145 std::vector<AMediaCodecInfo> infoList;
146 infoList.push_back(info);
147 sTypeToInfoList.emplace(mediaType, infoList);
148 } else {
149 it->second.push_back(info);
150 }
151 }
152 }
153 }
154
codecHandlesFormat(const AMediaCodecInfo codecInfo,sp<AMessage> format,bool isEncoder)155 static bool codecHandlesFormat(const AMediaCodecInfo codecInfo,
156 sp<AMessage> format, bool isEncoder) {
157 return codecInfo.mCodecCaps->isEncoder() == isEncoder
158 && codecInfo.mCodecCaps->isFormatSupported(format);
159 }
160
findNextCodecForFormat(const AMediaFormat * format,bool isEncoder,const AMediaCodecInfo ** outCodecInfo)161 static media_status_t findNextCodecForFormat(
162 const AMediaFormat *format, bool isEncoder, const AMediaCodecInfo **outCodecInfo) {
163 if (outCodecInfo == nullptr) {
164 return AMEDIA_ERROR_INVALID_PARAMETER;
165 }
166
167 if (sCodecInfos.empty()) {
168 initCodecInfoMap();
169 }
170
171 std::unique_ptr<std::vector<AMediaCodecInfo>> infos;
172 sp<AMessage> nativeFormat;
173 if (format == nullptr) {
174 infos = std::unique_ptr<std::vector<AMediaCodecInfo>>(&sCodecInfos);
175 } else {
176 AMediaFormat_getFormat(format, &nativeFormat);
177 AString mime;
178 if (!nativeFormat->findString(KEY_MIME, &mime)) {
179 return AMEDIA_ERROR_INVALID_PARAMETER;
180 }
181
182 std::string mediaType = std::string(mime.c_str());
183 auto it = sTypeToInfoList.find(mediaType);
184 if (it == sTypeToInfoList.end()) {
185 return AMEDIA_ERROR_UNSUPPORTED;
186 }
187 infos = std::unique_ptr<std::vector<AMediaCodecInfo>>(&(it->second));
188 }
189
190 bool found = *outCodecInfo == nullptr;
191 for (const AMediaCodecInfo &info : *infos) {
192 if (found && (format == nullptr
193 || codecHandlesFormat(info, nativeFormat, isEncoder))) {
194 *outCodecInfo = &info;
195 return AMEDIA_OK;
196 }
197 if (*outCodecInfo == &info) {
198 found = true;
199 }
200
201 }
202 *outCodecInfo = nullptr;
203 return AMEDIA_ERROR_UNSUPPORTED;
204 }
205
206 extern "C" {
207
208 EXPORT
AMediaCodecStore_getSupportedMediaTypes(const AMediaCodecSupportedMediaType ** outMediaTypes,size_t * outCount)209 media_status_t AMediaCodecStore_getSupportedMediaTypes(
210 const AMediaCodecSupportedMediaType **outMediaTypes, size_t *outCount) {
211 if (outMediaTypes == nullptr) {
212 return AMEDIA_ERROR_INVALID_PARAMETER;
213 }
214
215 if (sMediaTypes.empty()) {
216 initMediaTypes();
217 }
218
219 *outCount = sMediaTypes.size();
220 *outMediaTypes = sMediaTypes.data();
221
222 return AMEDIA_OK;
223 }
224
225 EXPORT
AMediaCodecStore_findNextDecoderForFormat(const AMediaFormat * format,const AMediaCodecInfo ** outCodecInfo)226 media_status_t AMediaCodecStore_findNextDecoderForFormat(
227 const AMediaFormat *format, const AMediaCodecInfo **outCodecInfo){
228 return findNextCodecForFormat(format, false, outCodecInfo);
229 }
230
231 EXPORT
AMediaCodecStore_findNextEncoderForFormat(const AMediaFormat * format,const AMediaCodecInfo ** outCodecInfo)232 media_status_t AMediaCodecStore_findNextEncoderForFormat(
233 const AMediaFormat *format, const AMediaCodecInfo **outCodecInfo){
234 return findNextCodecForFormat(format, true, outCodecInfo);
235 }
236
237 EXPORT
AMediaCodecStore_getCodecInfo(const char * name,const AMediaCodecInfo ** outCodecInfo)238 media_status_t AMediaCodecStore_getCodecInfo(
239 const char *name, const AMediaCodecInfo **outCodecInfo) {
240 if (outCodecInfo == nullptr || name == nullptr) {
241 return AMEDIA_ERROR_INVALID_PARAMETER;
242 }
243
244 auto it = sNameToInfoMap.find(std::string(name));
245 if (it == sNameToInfoMap.end()) {
246 *outCodecInfo = nullptr;
247 return AMEDIA_ERROR_UNSUPPORTED;
248 } else {
249 *outCodecInfo = &(it->second);
250 return AMEDIA_OK;
251 }
252 }
253
254 }