blob: 9b3d3fee6f6508a330610ddda16b011471da4f89 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright (C) 2018 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 "Codec2InfoBuilder"
19#include <log/log.h>
20
21#include <strings.h>
22
23#include <C2Component.h>
24#include <C2Config.h>
25#include <C2Debug.h>
26#include <C2PlatformSupport.h>
27#include <Codec2Mapper.h>
28
29#include <OMX_Audio.h>
30#include <OMX_AudioExt.h>
31#include <OMX_IndexExt.h>
32#include <OMX_Types.h>
33#include <OMX_Video.h>
34#include <OMX_VideoExt.h>
35#include <OMX_AsString.h>
36
37#include <android/hardware/media/omx/1.0/IOmx.h>
38#include <android/hardware/media/omx/1.0/IOmxObserver.h>
39#include <android/hardware/media/omx/1.0/IOmxNode.h>
40#include <android/hardware/media/omx/1.0/types.h>
41
42#include <android-base/properties.h>
43#include <codec2/hidl/client.h>
44#include <cutils/native_handle.h>
45#include <media/omx/1.0/WOmxNode.h>
Pawin Vongmasa1f213362019-01-24 06:59:16 -080046#include <media/stagefright/foundation/ALookup.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080047#include <media/stagefright/foundation/MediaDefs.h>
48#include <media/stagefright/omx/OMXUtils.h>
49#include <media/stagefright/xmlparser/MediaCodecsXmlParser.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070050#include <media/stagefright/Codec2InfoBuilder.h>
51#include <media/stagefright/MediaCodecConstants.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080052
53namespace android {
54
55using Traits = C2Component::Traits;
56
57namespace /* unnamed */ {
58
59bool hasPrefix(const std::string& s, const char* prefix) {
60 size_t prefixLen = strlen(prefix);
61 return s.compare(0, prefixLen, prefix) == 0;
62}
63
64bool hasSuffix(const std::string& s, const char* suffix) {
65 size_t suffixLen = strlen(suffix);
66 return suffixLen > s.size() ? false :
67 s.compare(s.size() - suffixLen, suffixLen, suffix) == 0;
68}
69
Lajos Molnar3e0ff012021-07-09 18:23:54 -070070// returns true if component advertised supported profile level(s)
71bool addSupportedProfileLevels(
Lajos Molnardb5751f2019-01-31 17:01:49 -080072 std::shared_ptr<Codec2Client::Interface> intf,
73 MediaCodecInfo::CapabilitiesWriter *caps,
74 const Traits& trait, const std::string &mediaType) {
75 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
76 C2Mapper::GetProfileLevelMapper(trait.mediaType);
77 // if we don't know the media type, pass through all values unmapped
Pawin Vongmasa36653902018-11-15 00:10:25 -080078
Lajos Molnardb5751f2019-01-31 17:01:49 -080079 // TODO: we cannot find levels that are local 'maxima' without knowing the coding
80 // e.g. H.263 level 45 and level 30 could be two values for highest level as
81 // they don't include one another. For now we use the last supported value.
82 bool encoder = trait.kind == C2Component::KIND_ENCODER;
83 C2StreamProfileLevelInfo pl(encoder /* output */, 0u);
84 std::vector<C2FieldSupportedValuesQuery> profileQuery = {
85 C2FieldSupportedValuesQuery::Possible(C2ParamField(&pl, &pl.profile))
Pawin Vongmasa36653902018-11-15 00:10:25 -080086 };
87
Lajos Molnardb5751f2019-01-31 17:01:49 -080088 c2_status_t err = intf->querySupportedValues(profileQuery, C2_DONT_BLOCK);
89 ALOGV("query supported profiles -> %s | %s", asString(err), asString(profileQuery[0].status));
90 if (err != C2_OK || profileQuery[0].status != C2_OK) {
Lajos Molnar3e0ff012021-07-09 18:23:54 -070091 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -080092 }
93
Lajos Molnardb5751f2019-01-31 17:01:49 -080094 // we only handle enumerated values
95 if (profileQuery[0].values.type != C2FieldSupportedValues::VALUES) {
Lajos Molnar3e0ff012021-07-09 18:23:54 -070096 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -080097 }
98
Lajos Molnardb5751f2019-01-31 17:01:49 -080099 // determine if codec supports HDR
100 bool supportsHdr = false;
101 bool supportsHdr10Plus = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800102
Lajos Molnardb5751f2019-01-31 17:01:49 -0800103 std::vector<std::shared_ptr<C2ParamDescriptor>> paramDescs;
104 c2_status_t err1 = intf->querySupportedParams(&paramDescs);
105 if (err1 == C2_OK) {
106 for (const std::shared_ptr<C2ParamDescriptor> &desc : paramDescs) {
Lajos Molnar739fe732021-02-07 13:06:10 -0800107 C2Param::Type type = desc->index();
108 // only consider supported parameters on raw ports
109 if (!(encoder ? type.forInput() : type.forOutput())) {
110 continue;
111 }
112 switch (type.coreIndex()) {
113 case C2StreamHdr10PlusInfo::CORE_INDEX:
Lajos Molnardb5751f2019-01-31 17:01:49 -0800114 supportsHdr10Plus = true;
115 break;
Lajos Molnar739fe732021-02-07 13:06:10 -0800116 case C2StreamHdrStaticInfo::CORE_INDEX:
Lajos Molnardb5751f2019-01-31 17:01:49 -0800117 supportsHdr = true;
118 break;
119 default:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800120 break;
121 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800122 }
123 }
124
Chong Zhang0702d1f2019-08-15 11:45:36 -0700125 // For VP9/AV1, the static info is always propagated by framework.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800126 supportsHdr |= (mediaType == MIMETYPE_VIDEO_VP9);
Chong Zhang0702d1f2019-08-15 11:45:36 -0700127 supportsHdr |= (mediaType == MIMETYPE_VIDEO_AV1);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800128
Lajos Molnar3e0ff012021-07-09 18:23:54 -0700129 bool added = false;
130
Lajos Molnardb5751f2019-01-31 17:01:49 -0800131 for (C2Value::Primitive profile : profileQuery[0].values.values) {
132 pl.profile = (C2Config::profile_t)profile.ref<uint32_t>();
133 std::vector<std::unique_ptr<C2SettingResult>> failures;
134 err = intf->config({&pl}, C2_DONT_BLOCK, &failures);
135 ALOGV("set profile to %u -> %s", pl.profile, asString(err));
136 std::vector<C2FieldSupportedValuesQuery> levelQuery = {
137 C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level))
138 };
139 err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK);
140 ALOGV("query supported levels -> %s | %s", asString(err), asString(levelQuery[0].status));
141 if (err != C2_OK || levelQuery[0].status != C2_OK
142 || levelQuery[0].values.type != C2FieldSupportedValues::VALUES
143 || levelQuery[0].values.values.size() == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800144 continue;
145 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800146
147 C2Value::Primitive level = levelQuery[0].values.values.back();
148 pl.level = (C2Config::level_t)level.ref<uint32_t>();
149 ALOGV("supporting level: %u", pl.level);
150 int32_t sdkProfile, sdkLevel;
151 if (mapper && mapper->mapProfile(pl.profile, &sdkProfile)
152 && mapper->mapLevel(pl.level, &sdkLevel)) {
153 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
154 // also list HDR profiles if component supports HDR
155 if (supportsHdr) {
156 auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper(trait.mediaType);
157 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
158 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
159 }
160 if (supportsHdr10Plus) {
161 hdrMapper = C2Mapper::GetHdrProfileLevelMapper(
162 trait.mediaType, true /*isHdr10Plus*/);
163 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
164 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
165 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800166 }
167 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800168 } else if (!mapper) {
169 caps->addProfileLevel(pl.profile, pl.level);
170 }
Lajos Molnar3e0ff012021-07-09 18:23:54 -0700171 added = true;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800172
173 // for H.263 also advertise the second highest level if the
174 // codec supports level 45, as level 45 only covers level 10
175 // TODO: move this to some form of a setting so it does not
176 // have to be here
177 if (mediaType == MIMETYPE_VIDEO_H263) {
178 C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED;
179 for (C2Value::Primitive v : levelQuery[0].values.values) {
180 C2Config::level_t level = (C2Config::level_t)v.ref<uint32_t>();
181 if (level < C2Config::LEVEL_H263_45 && level > nextLevel) {
182 nextLevel = level;
183 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800184 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800185 if (nextLevel != C2Config::LEVEL_UNUSED
186 && nextLevel != pl.level
187 && mapper
188 && mapper->mapProfile(pl.profile, &sdkProfile)
189 && mapper->mapLevel(nextLevel, &sdkLevel)) {
190 caps->addProfileLevel(
191 (uint32_t)sdkProfile, (uint32_t)sdkLevel);
192 }
193 }
194 }
Lajos Molnar3e0ff012021-07-09 18:23:54 -0700195 return added;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800196}
197
198void addSupportedColorFormats(
199 std::shared_ptr<Codec2Client::Interface> intf,
200 MediaCodecInfo::CapabilitiesWriter *caps,
201 const Traits& trait, const std::string &mediaType) {
202 (void)intf;
203
204 // TODO: get this from intf() as well, but how do we map them to
205 // MediaCodec color formats?
206 bool encoder = trait.kind == C2Component::KIND_ENCODER;
Wonsik Kim16223262019-06-14 14:40:57 -0700207 if (mediaType.find("video") != std::string::npos
208 || mediaType.find("image") != std::string::npos) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800209 // vendor video codecs prefer opaque format
210 if (trait.name.find("android") == std::string::npos) {
211 caps->addColorFormat(COLOR_FormatSurface);
212 }
213 caps->addColorFormat(COLOR_FormatYUV420Flexible);
214 caps->addColorFormat(COLOR_FormatYUV420Planar);
215 caps->addColorFormat(COLOR_FormatYUV420SemiPlanar);
216 caps->addColorFormat(COLOR_FormatYUV420PackedPlanar);
217 caps->addColorFormat(COLOR_FormatYUV420PackedSemiPlanar);
218 // framework video encoders must support surface format, though it is unclear
219 // that they will be able to map it if it is opaque
220 if (encoder && trait.name.find("android") != std::string::npos) {
221 caps->addColorFormat(COLOR_FormatSurface);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800222 }
223 }
224}
225
Lajos Molnar424cfb52019-04-08 17:48:00 -0700226class Switch {
227 enum Flags : uint8_t {
228 // flags
229 IS_ENABLED = (1 << 0),
230 BY_DEFAULT = (1 << 1),
231 };
232
233 constexpr Switch(uint8_t flags) : mFlags(flags) {}
234
235 uint8_t mFlags;
236
237public:
238 // have to create class due to this bool conversion operator...
239 constexpr operator bool() const {
240 return mFlags & IS_ENABLED;
241 }
242
243 constexpr Switch operator!() const {
244 return Switch(mFlags ^ IS_ENABLED);
245 }
246
247 static constexpr Switch DISABLED() { return 0; };
248 static constexpr Switch ENABLED() { return IS_ENABLED; };
249 static constexpr Switch DISABLED_BY_DEFAULT() { return BY_DEFAULT; };
250 static constexpr Switch ENABLED_BY_DEFAULT() { return IS_ENABLED | BY_DEFAULT; };
251
252 const char *toString(const char *def = "??") const {
253 switch (mFlags) {
254 case 0: return "0";
255 case IS_ENABLED: return "1";
256 case BY_DEFAULT: return "(0)";
257 case IS_ENABLED | BY_DEFAULT: return "(1)";
258 default: return def;
259 }
260 }
261
262};
263
264const char *asString(const Switch &s, const char *def = "??") {
265 return s.toString(def);
266}
267
268Switch isSettingEnabled(
269 std::string setting, const MediaCodecsXmlParser::AttributeMap &settings,
270 Switch def = Switch::DISABLED_BY_DEFAULT()) {
271 const auto enablement = settings.find(setting);
272 if (enablement == settings.end()) {
273 return def;
274 }
275 return enablement->second == "1" ? Switch::ENABLED() : Switch::DISABLED();
276}
277
278Switch isVariantEnabled(
279 std::string variant, const MediaCodecsXmlParser::AttributeMap &settings) {
280 return isSettingEnabled("variant-" + variant, settings);
281}
282
283Switch isVariantExpressionEnabled(
284 std::string exp, const MediaCodecsXmlParser::AttributeMap &settings) {
285 if (!exp.empty() && exp.at(0) == '!') {
286 return !isVariantEnabled(exp.substr(1, exp.size() - 1), settings);
287 }
288 return isVariantEnabled(exp, settings);
289}
290
291Switch isDomainEnabled(
292 std::string domain, const MediaCodecsXmlParser::AttributeMap &settings) {
293 return isSettingEnabled("domain-" + domain, settings);
294}
295
Pawin Vongmasa36653902018-11-15 00:10:25 -0800296} // unnamed namespace
297
298status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
299 // TODO: Remove run-time configurations once all codecs are working
300 // properly. (Assume "full" behavior eventually.)
301 //
302 // debug.stagefright.ccodec supports 5 values.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800303 // 0 - No Codec 2.0 components are available.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800304 // 1 - Audio decoders and encoders with prefix "c2.android." are available
305 // and ranked first.
306 // All other components with prefix "c2.android." are available with
307 // their normal ranks.
308 // Components with prefix "c2.vda." are available with their normal
309 // ranks.
310 // All other components with suffix ".avc.decoder" or ".avc.encoder"
311 // are available but ranked last.
312 // 2 - Components with prefix "c2.android." are available and ranked
313 // first.
314 // Components with prefix "c2.vda." are available with their normal
315 // ranks.
316 // All other components with suffix ".avc.decoder" or ".avc.encoder"
317 // are available but ranked last.
318 // 3 - Components with prefix "c2.android." are available and ranked
319 // first.
320 // All other components are available with their normal ranks.
321 // 4 - All components are available with their normal ranks.
322 //
323 // The default value (boot time) is 1.
324 //
325 // Note: Currently, OMX components have default rank 0x100, while all
326 // Codec2.0 software components have default rank 0x200.
Lajos Molnar8635fc82019-05-17 17:35:10 +0000327 int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 4);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800328
329 // Obtain Codec2Client
330 std::vector<Traits> traits = Codec2Client::ListComponents();
331
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800332 // parse APEX XML first, followed by vendor XML.
333 // Note: APEX XML names do not depend on ro.media.xml_variant.* properties.
Lajos Molnarda666892019-04-08 17:25:34 -0700334 MediaCodecsXmlParser parser;
335 parser.parseXmlFilesInSearchDirs(
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800336 { "media_codecs.xml", "media_codecs_performance.xml" },
Lajos Molnar424cfb52019-04-08 17:48:00 -0700337 { "/apex/com.android.media.swcodec/etc" });
338
339 // TODO: remove these c2-specific files once product moved to default file names
340 parser.parseXmlFilesInSearchDirs(
Lajos Molnarda666892019-04-08 17:25:34 -0700341 { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
Lajos Molnar424cfb52019-04-08 17:48:00 -0700342
343 // parse default XML files
344 parser.parseXmlFilesInSearchDirs();
345
Pawin Vongmasa36653902018-11-15 00:10:25 -0800346 if (parser.getParsingStatus() != OK) {
347 ALOGD("XML parser no good");
348 return OK;
349 }
350
Lajos Molnar424cfb52019-04-08 17:48:00 -0700351 MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap();
352 for (const auto &v : settings) {
353 if (!hasPrefix(v.first, "media-type-")
354 && !hasPrefix(v.first, "domain-")
355 && !hasPrefix(v.first, "variant-")) {
356 writer->addGlobalSetting(v.first.c_str(), v.second.c_str());
357 }
358 }
359
Pawin Vongmasa36653902018-11-15 00:10:25 -0800360 for (const Traits& trait : traits) {
361 C2Component::rank_t rank = trait.rank;
362
Lajos Molnardb5751f2019-01-31 17:01:49 -0800363 // Interface must be accessible for us to list the component, and there also
364 // must be an XML entry for the codec. Codec aliases listed in the traits
365 // allow additional XML entries to be specified for each alias. These will
366 // be listed as separate codecs. If no XML entry is specified for an alias,
367 // those will be treated as an additional alias specified in the XML entry
368 // for the interface name.
369 std::vector<std::string> nameAndAliases = trait.aliases;
370 nameAndAliases.insert(nameAndAliases.begin(), trait.name);
371 for (const std::string &nameOrAlias : nameAndAliases) {
372 bool isAlias = trait.name != nameOrAlias;
373 std::shared_ptr<Codec2Client::Interface> intf =
374 Codec2Client::CreateInterfaceByName(nameOrAlias.c_str());
375 if (!intf) {
376 ALOGD("could not create interface for %s'%s'",
377 isAlias ? "alias " : "",
378 nameOrAlias.c_str());
379 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800380 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800381 if (parser.getCodecMap().count(nameOrAlias) == 0) {
382 if (isAlias) {
383 std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo =
384 writer->findMediaCodecInfo(trait.name.c_str());
385 if (!baseCodecInfo) {
386 ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing",
387 nameOrAlias.c_str(),
388 trait.name.c_str());
389 } else {
390 ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this",
391 nameOrAlias.c_str());
392 // merge alias into existing codec
393 baseCodecInfo->addAlias(nameOrAlias.c_str());
394 }
395 } else {
396 ALOGD("component '%s' not found in xml", trait.name.c_str());
397 }
398 continue;
399 }
400 std::string canonName = trait.name;
401
402 // TODO: Remove this block once all codecs are enabled by default.
403 switch (option) {
404 case 0:
405 continue;
406 case 1:
407 if (hasPrefix(canonName, "c2.vda.")) {
408 break;
409 }
410 if (hasPrefix(canonName, "c2.android.")) {
411 if (trait.domain == C2Component::DOMAIN_AUDIO) {
412 rank = 1;
413 break;
414 }
415 break;
416 }
417 if (hasSuffix(canonName, ".avc.decoder") ||
418 hasSuffix(canonName, ".avc.encoder")) {
419 rank = std::numeric_limits<decltype(rank)>::max();
420 break;
421 }
422 continue;
423 case 2:
424 if (hasPrefix(canonName, "c2.vda.")) {
425 break;
426 }
427 if (hasPrefix(canonName, "c2.android.")) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800428 rank = 1;
429 break;
430 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800431 if (hasSuffix(canonName, ".avc.decoder") ||
432 hasSuffix(canonName, ".avc.encoder")) {
433 rank = std::numeric_limits<decltype(rank)>::max();
434 break;
435 }
436 continue;
437 case 3:
438 if (hasPrefix(canonName, "c2.android.")) {
439 rank = 1;
440 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800441 break;
442 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800443
Lajos Molnar424cfb52019-04-08 17:48:00 -0700444 const MediaCodecsXmlParser::CodecProperties &codec =
445 parser.getCodecMap().at(nameOrAlias);
446
447 // verify that either the codec is explicitly enabled, or one of its domains is
448 bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end();
449 if (!codecEnabled) {
450 for (const std::string &domain : codec.domainSet) {
451 const Switch enabled = isDomainEnabled(domain, settings);
452 ALOGV("codec entry '%s' is in domain '%s' that is '%s'",
453 nameOrAlias.c_str(), domain.c_str(), asString(enabled));
454 if (enabled) {
455 codecEnabled = true;
456 break;
457 }
458 }
459 }
460 // if codec has variants, also check that at least one of them is enabled
461 bool variantEnabled = codec.variantSet.empty();
462 for (const std::string &variant : codec.variantSet) {
463 const Switch enabled = isVariantExpressionEnabled(variant, settings);
464 ALOGV("codec entry '%s' has a variant '%s' that is '%s'",
465 nameOrAlias.c_str(), variant.c_str(), asString(enabled));
466 if (enabled) {
467 variantEnabled = true;
468 break;
469 }
470 }
471 if (!codecEnabled || !variantEnabled) {
472 ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str());
473 continue;
474 }
475
Lajos Molnardb5751f2019-01-31 17:01:49 -0800476 ALOGV("adding codec entry for '%s'", nameOrAlias.c_str());
477 std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
478 codecInfo->setName(nameOrAlias.c_str());
479 codecInfo->setOwner(("codec2::" + trait.owner).c_str());
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800480
Lajos Molnardb5751f2019-01-31 17:01:49 -0800481 bool encoder = trait.kind == C2Component::KIND_ENCODER;
482 typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800483
Lajos Molnardb5751f2019-01-31 17:01:49 -0800484 if (encoder) {
485 attrs |= MediaCodecInfo::kFlagIsEncoder;
486 }
487 if (trait.owner == "software") {
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800488 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800489 } else {
490 attrs |= MediaCodecInfo::kFlagIsVendor;
491 if (trait.owner == "vendor-software") {
492 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
493 } else if (codec.quirkSet.find("attribute::software-codec")
494 == codec.quirkSet.end()) {
495 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800496 }
497 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800498 codecInfo->setAttributes(attrs);
499 if (!codec.rank.empty()) {
500 uint32_t xmlRank;
501 char dummy;
502 if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) {
503 rank = xmlRank;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800504 }
505 }
Lajos Molnar424cfb52019-04-08 17:48:00 -0700506 ALOGV("rank: %u", (unsigned)rank);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800507 codecInfo->setRank(rank);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800508
Lajos Molnardb5751f2019-01-31 17:01:49 -0800509 for (const std::string &alias : codec.aliases) {
510 ALOGV("adding alias '%s'", alias.c_str());
511 codecInfo->addAlias(alias.c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800512 }
513
Lajos Molnardb5751f2019-01-31 17:01:49 -0800514 for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
515 const std::string &mediaType = typeIt->first;
Lajos Molnar424cfb52019-04-08 17:48:00 -0700516 const Switch typeEnabled = isSettingEnabled(
517 "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT());
518 const Switch domainTypeEnabled = isSettingEnabled(
519 "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"),
520 settings, Switch::ENABLED_BY_DEFAULT());
521 ALOGV("type '%s-%s' is '%s/%s'",
522 mediaType.c_str(), (encoder ? "encoder" : "decoder"),
523 asString(typeEnabled), asString(domainTypeEnabled));
524 if (!typeEnabled || !domainTypeEnabled) {
525 ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(),
526 nameOrAlias.c_str());
527 continue;
528 }
529
530 ALOGI("adding type '%s'", typeIt->first.c_str());
Lajos Molnardb5751f2019-01-31 17:01:49 -0800531 const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
532 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
533 codecInfo->addMediaType(mediaType.c_str());
Lajos Molnar424cfb52019-04-08 17:48:00 -0700534 for (const auto &v : attrMap) {
535 std::string key = v.first;
536 std::string value = v.second;
537
538 size_t variantSep = key.find(":::");
539 if (variantSep != std::string::npos) {
540 std::string variant = key.substr(0, variantSep);
541 const Switch enabled = isVariantExpressionEnabled(variant, settings);
542 ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled));
543 if (!enabled) {
544 continue;
545 }
546 key = key.substr(variantSep + 3);
547 }
548
Lajos Molnardb5751f2019-01-31 17:01:49 -0800549 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
550 int32_t intValue = 0;
551 // Ignore trailing bad characters and default to 0.
552 (void)sscanf(value.c_str(), "%d", &intValue);
553 caps->addDetail(key.c_str(), intValue);
554 } else {
555 caps->addDetail(key.c_str(), value.c_str());
556 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800557 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800558
Lajos Molnar3e0ff012021-07-09 18:23:54 -0700559 if (!addSupportedProfileLevels(intf, caps.get(), trait, mediaType)) {
560 // TODO(b/193279646) This will get fixed in C2InterfaceHelper
561 // Some components may not advertise supported values if they use a const
562 // param for profile/level (they support only one profile). For now cover
563 // only VP8 here until it is fixed.
564 if (mediaType == MIMETYPE_VIDEO_VP8) {
565 caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
566 }
567 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800568 addSupportedColorFormats(intf, caps.get(), trait, mediaType);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800569 }
570 }
571 }
572 return OK;
573}
574
575} // namespace android
576
577extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
578 return new android::Codec2InfoBuilder;
579}