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 char *mediaTypePtr = new char[mediaType.size()+1];
70 strncpy(mediaTypePtr, mediaType.c_str(), mediaType.size()+1);
71 it = typesInfoMap.emplace(mediaType,
72 (AMediaCodecSupportedMediaType) { mediaTypePtr, 0 }).first;
73 mediaTypes.push_back(mediaType);
74 }
75 uint32_t &mode = it->second.mMode;
76 mode |= (codecInfo->isEncoder() ? AMediaCodecSupportedMediaType::FLAG_ENCODER
77 : AMediaCodecSupportedMediaType::FLAG_DECODER);
78 }
79 }
80
81 // sMediaTypes keeps the order of media types appearing in sCodecList.
82 for (std::string &type : mediaTypes) {
83 sMediaTypes.push_back(typesInfoMap.find(type)->second);
84 }
85 }
86
initCodecInfoMap()87 static void initCodecInfoMap() {
88 if (sCodecList == nullptr) {
89 sCodecList = MediaCodecList::getInstance();
90 }
91
92 for (size_t idx = 0; idx < sCodecList->countCodecs(); idx++) {
93 sp<MediaCodecInfo> codecInfo = sCodecList->getCodecInfo(idx);
94 if (codecInfo == nullptr) {
95 ALOGW("NULL MediaCodecInfo in MediaCodecList");
96 continue;
97 }
98
99 Vector<AString> codecMediaTypes;
100 codecInfo->getSupportedMediaTypes(&codecMediaTypes);
101 bool useTypeSuffix = codecMediaTypes.size() > 1;
102 for (AString codecMediaType : codecMediaTypes) {
103 std::string mediaType = std::string(codecMediaType.c_str());
104
105 // Excludes special codecs from NDK
106 const std::shared_ptr<CodecCapabilities> codecCaps
107 = codecInfo->getCodecCapsFor(mediaType.c_str());
108 if (codecCaps->isFeatureSupported(FEATURE_SpecialCodec)) {
109 continue;
110 }
111
112 // get the type name after the slash. e.g. video/x.on2.vp8
113 size_t slashIx = mediaType.find_last_of('/');
114 if (slashIx == std::string::npos) {
115 slashIx = 0;
116 } else {
117 slashIx++;
118 }
119 std::string ndkBaseName = std::string(codecInfo->getCodecName());
120 if (useTypeSuffix) {
121 // If there are multiple supported media types,
122 // add the type to the end of the name to disambiguate names.
123 ndkBaseName += "." + mediaType.substr(slashIx);
124 }
125
126 int32_t copyIx = 0;
127 std::string ndkName;
128 // if a name is already registered,
129 // add ".1", ".2", ... at the end to disambiguate names.
130 while (true) {
131 ndkName = ndkBaseName;
132 if (copyIx > 0) {
133 ndkName += "." + std::to_string(copyIx);
134 }
135 if (!sNameToInfoMap.contains(ndkName)) {
136 break;
137 }
138 copyIx++;
139 }
140
141 AMediaCodecInfo info = AMediaCodecInfo(ndkName, codecInfo, codecCaps, mediaType);
142 sCodecInfos.push_back(info);
143 sNameToInfoMap.emplace(ndkName, info);
144
145 auto it = sTypeToInfoList.find(mediaType);
146 if (it == sTypeToInfoList.end()) {
147 std::vector<AMediaCodecInfo> infoList;
148 infoList.push_back(info);
149 sTypeToInfoList.emplace(mediaType, infoList);
150 } else {
151 it->second.push_back(info);
152 }
153 }
154 }
155 }
156
findNextCodecForFormat(const AMediaFormat * format,bool isEncoder,const AMediaCodecInfo ** outCodecInfo)157 static media_status_t findNextCodecForFormat(
158 const AMediaFormat *format, bool isEncoder, const AMediaCodecInfo **outCodecInfo) {
159 if (outCodecInfo == nullptr) {
160 return AMEDIA_ERROR_INVALID_PARAMETER;
161 }
162
163 if (sCodecInfos.empty()) {
164 initCodecInfoMap();
165 }
166
167 std::vector<AMediaCodecInfo> *infos;
168 sp<AMessage> nativeFormat;
169 if (format == nullptr) {
170 infos = &sCodecInfos;
171 } else {
172 AMediaFormat_getFormat(format, &nativeFormat);
173 AString mime;
174 if (!nativeFormat->findString(KEY_MIME, &mime)) {
175 return AMEDIA_ERROR_INVALID_PARAMETER;
176 }
177
178 std::string mediaType = std::string(mime.c_str());
179 std::map<std::string, std::vector<AMediaCodecInfo>>::iterator it
180 = sTypeToInfoList.find(mediaType);
181 if (it == sTypeToInfoList.end()) {
182 return AMEDIA_ERROR_UNSUPPORTED;
183 }
184 infos = &(it->second);
185 }
186
187 bool found = *outCodecInfo == nullptr;
188 for (const AMediaCodecInfo &info : *infos) {
189 if (found && info.mCodecCaps->isEncoder() == isEncoder
190 && (format == nullptr || info.mCodecCaps->isFormatSupported(nativeFormat))) {
191 *outCodecInfo = &info;
192 return AMEDIA_OK;
193 }
194 if (*outCodecInfo == &info) {
195 found = true;
196 }
197 }
198 *outCodecInfo = nullptr;
199 return AMEDIA_ERROR_UNSUPPORTED;
200 }
201
202 extern "C" {
203
204 EXPORT
AMediaCodecStore_getSupportedMediaTypes(const AMediaCodecSupportedMediaType ** outMediaTypes,size_t * outCount)205 media_status_t AMediaCodecStore_getSupportedMediaTypes(
206 const AMediaCodecSupportedMediaType **outMediaTypes, size_t *outCount) {
207 if (outMediaTypes == nullptr || outCount == nullptr) {
208 return AMEDIA_ERROR_INVALID_PARAMETER;
209 }
210
211 if (sMediaTypes.empty()) {
212 initMediaTypes();
213 }
214
215 *outCount = sMediaTypes.size();
216 *outMediaTypes = sMediaTypes.data();
217
218 return AMEDIA_OK;
219 }
220
221 EXPORT
AMediaCodecStore_findNextDecoderForFormat(const AMediaFormat * format,const AMediaCodecInfo ** outCodecInfo)222 media_status_t AMediaCodecStore_findNextDecoderForFormat(
223 const AMediaFormat *format, const AMediaCodecInfo **outCodecInfo){
224 return findNextCodecForFormat(format, false, outCodecInfo);
225 }
226
227 EXPORT
AMediaCodecStore_findNextEncoderForFormat(const AMediaFormat * format,const AMediaCodecInfo ** outCodecInfo)228 media_status_t AMediaCodecStore_findNextEncoderForFormat(
229 const AMediaFormat *format, const AMediaCodecInfo **outCodecInfo){
230 return findNextCodecForFormat(format, true, outCodecInfo);
231 }
232
233 EXPORT
AMediaCodecStore_getCodecInfo(const char * name,const AMediaCodecInfo ** outCodecInfo)234 media_status_t AMediaCodecStore_getCodecInfo(
235 const char *name, const AMediaCodecInfo **outCodecInfo) {
236 if (outCodecInfo == nullptr || name == nullptr) {
237 return AMEDIA_ERROR_INVALID_PARAMETER;
238 }
239
240 if (sNameToInfoMap.empty()) {
241 initCodecInfoMap();
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 }