Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 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 Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 46 | #include <media/stagefright/foundation/ALookup.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 47 | #include <media/stagefright/foundation/MediaDefs.h> |
| 48 | #include <media/stagefright/omx/OMXUtils.h> |
| 49 | #include <media/stagefright/xmlparser/MediaCodecsXmlParser.h> |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 50 | #include <media/stagefright/Codec2InfoBuilder.h> |
| 51 | #include <media/stagefright/MediaCodecConstants.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 52 | |
| 53 | namespace android { |
| 54 | |
| 55 | using Traits = C2Component::Traits; |
| 56 | |
| 57 | namespace /* unnamed */ { |
| 58 | |
| 59 | bool hasPrefix(const std::string& s, const char* prefix) { |
| 60 | size_t prefixLen = strlen(prefix); |
| 61 | return s.compare(0, prefixLen, prefix) == 0; |
| 62 | } |
| 63 | |
| 64 | bool 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 Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 70 | void addSupportedProfileLevels( |
| 71 | std::shared_ptr<Codec2Client::Interface> intf, |
| 72 | MediaCodecInfo::CapabilitiesWriter *caps, |
| 73 | const Traits& trait, const std::string &mediaType) { |
| 74 | std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper = |
| 75 | C2Mapper::GetProfileLevelMapper(trait.mediaType); |
| 76 | // if we don't know the media type, pass through all values unmapped |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 77 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 78 | // TODO: we cannot find levels that are local 'maxima' without knowing the coding |
| 79 | // e.g. H.263 level 45 and level 30 could be two values for highest level as |
| 80 | // they don't include one another. For now we use the last supported value. |
| 81 | bool encoder = trait.kind == C2Component::KIND_ENCODER; |
| 82 | C2StreamProfileLevelInfo pl(encoder /* output */, 0u); |
| 83 | std::vector<C2FieldSupportedValuesQuery> profileQuery = { |
| 84 | C2FieldSupportedValuesQuery::Possible(C2ParamField(&pl, &pl.profile)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 87 | c2_status_t err = intf->querySupportedValues(profileQuery, C2_DONT_BLOCK); |
| 88 | ALOGV("query supported profiles -> %s | %s", asString(err), asString(profileQuery[0].status)); |
| 89 | if (err != C2_OK || profileQuery[0].status != C2_OK) { |
| 90 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 93 | // we only handle enumerated values |
| 94 | if (profileQuery[0].values.type != C2FieldSupportedValues::VALUES) { |
| 95 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 98 | // determine if codec supports HDR |
| 99 | bool supportsHdr = false; |
| 100 | bool supportsHdr10Plus = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 101 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 102 | std::vector<std::shared_ptr<C2ParamDescriptor>> paramDescs; |
| 103 | c2_status_t err1 = intf->querySupportedParams(¶mDescs); |
| 104 | if (err1 == C2_OK) { |
| 105 | for (const std::shared_ptr<C2ParamDescriptor> &desc : paramDescs) { |
Lajos Molnar | 739fe73 | 2021-02-07 13:06:10 -0800 | [diff] [blame] | 106 | C2Param::Type type = desc->index(); |
| 107 | // only consider supported parameters on raw ports |
| 108 | if (!(encoder ? type.forInput() : type.forOutput())) { |
| 109 | continue; |
| 110 | } |
| 111 | switch (type.coreIndex()) { |
| 112 | case C2StreamHdr10PlusInfo::CORE_INDEX: |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 113 | supportsHdr10Plus = true; |
| 114 | break; |
Lajos Molnar | 739fe73 | 2021-02-07 13:06:10 -0800 | [diff] [blame] | 115 | case C2StreamHdrStaticInfo::CORE_INDEX: |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 116 | supportsHdr = true; |
| 117 | break; |
| 118 | default: |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 119 | break; |
| 120 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Chong Zhang | 0702d1f | 2019-08-15 11:45:36 -0700 | [diff] [blame] | 124 | // For VP9/AV1, the static info is always propagated by framework. |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 125 | supportsHdr |= (mediaType == MIMETYPE_VIDEO_VP9); |
Chong Zhang | 0702d1f | 2019-08-15 11:45:36 -0700 | [diff] [blame] | 126 | supportsHdr |= (mediaType == MIMETYPE_VIDEO_AV1); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 127 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 128 | for (C2Value::Primitive profile : profileQuery[0].values.values) { |
| 129 | pl.profile = (C2Config::profile_t)profile.ref<uint32_t>(); |
| 130 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 131 | err = intf->config({&pl}, C2_DONT_BLOCK, &failures); |
| 132 | ALOGV("set profile to %u -> %s", pl.profile, asString(err)); |
| 133 | std::vector<C2FieldSupportedValuesQuery> levelQuery = { |
| 134 | C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level)) |
| 135 | }; |
| 136 | err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK); |
| 137 | ALOGV("query supported levels -> %s | %s", asString(err), asString(levelQuery[0].status)); |
| 138 | if (err != C2_OK || levelQuery[0].status != C2_OK |
| 139 | || levelQuery[0].values.type != C2FieldSupportedValues::VALUES |
| 140 | || levelQuery[0].values.values.size() == 0) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 141 | continue; |
| 142 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 143 | |
| 144 | C2Value::Primitive level = levelQuery[0].values.values.back(); |
| 145 | pl.level = (C2Config::level_t)level.ref<uint32_t>(); |
| 146 | ALOGV("supporting level: %u", pl.level); |
| 147 | int32_t sdkProfile, sdkLevel; |
| 148 | if (mapper && mapper->mapProfile(pl.profile, &sdkProfile) |
| 149 | && mapper->mapLevel(pl.level, &sdkLevel)) { |
| 150 | caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel); |
| 151 | // also list HDR profiles if component supports HDR |
| 152 | if (supportsHdr) { |
| 153 | auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper(trait.mediaType); |
| 154 | if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) { |
| 155 | caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel); |
| 156 | } |
| 157 | if (supportsHdr10Plus) { |
| 158 | hdrMapper = C2Mapper::GetHdrProfileLevelMapper( |
| 159 | trait.mediaType, true /*isHdr10Plus*/); |
| 160 | if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) { |
| 161 | caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel); |
| 162 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 165 | } else if (!mapper) { |
| 166 | caps->addProfileLevel(pl.profile, pl.level); |
| 167 | } |
| 168 | |
| 169 | // for H.263 also advertise the second highest level if the |
| 170 | // codec supports level 45, as level 45 only covers level 10 |
| 171 | // TODO: move this to some form of a setting so it does not |
| 172 | // have to be here |
| 173 | if (mediaType == MIMETYPE_VIDEO_H263) { |
| 174 | C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED; |
| 175 | for (C2Value::Primitive v : levelQuery[0].values.values) { |
| 176 | C2Config::level_t level = (C2Config::level_t)v.ref<uint32_t>(); |
| 177 | if (level < C2Config::LEVEL_H263_45 && level > nextLevel) { |
| 178 | nextLevel = level; |
| 179 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 180 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 181 | if (nextLevel != C2Config::LEVEL_UNUSED |
| 182 | && nextLevel != pl.level |
| 183 | && mapper |
| 184 | && mapper->mapProfile(pl.profile, &sdkProfile) |
| 185 | && mapper->mapLevel(nextLevel, &sdkLevel)) { |
| 186 | caps->addProfileLevel( |
| 187 | (uint32_t)sdkProfile, (uint32_t)sdkLevel); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void addSupportedColorFormats( |
| 194 | std::shared_ptr<Codec2Client::Interface> intf, |
| 195 | MediaCodecInfo::CapabilitiesWriter *caps, |
| 196 | const Traits& trait, const std::string &mediaType) { |
| 197 | (void)intf; |
| 198 | |
| 199 | // TODO: get this from intf() as well, but how do we map them to |
| 200 | // MediaCodec color formats? |
| 201 | bool encoder = trait.kind == C2Component::KIND_ENCODER; |
Wonsik Kim | 1622326 | 2019-06-14 14:40:57 -0700 | [diff] [blame] | 202 | if (mediaType.find("video") != std::string::npos |
| 203 | || mediaType.find("image") != std::string::npos) { |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 204 | // vendor video codecs prefer opaque format |
| 205 | if (trait.name.find("android") == std::string::npos) { |
| 206 | caps->addColorFormat(COLOR_FormatSurface); |
| 207 | } |
| 208 | caps->addColorFormat(COLOR_FormatYUV420Flexible); |
| 209 | caps->addColorFormat(COLOR_FormatYUV420Planar); |
| 210 | caps->addColorFormat(COLOR_FormatYUV420SemiPlanar); |
| 211 | caps->addColorFormat(COLOR_FormatYUV420PackedPlanar); |
| 212 | caps->addColorFormat(COLOR_FormatYUV420PackedSemiPlanar); |
| 213 | // framework video encoders must support surface format, though it is unclear |
| 214 | // that they will be able to map it if it is opaque |
| 215 | if (encoder && trait.name.find("android") != std::string::npos) { |
| 216 | caps->addColorFormat(COLOR_FormatSurface); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 221 | class Switch { |
| 222 | enum Flags : uint8_t { |
| 223 | // flags |
| 224 | IS_ENABLED = (1 << 0), |
| 225 | BY_DEFAULT = (1 << 1), |
| 226 | }; |
| 227 | |
| 228 | constexpr Switch(uint8_t flags) : mFlags(flags) {} |
| 229 | |
| 230 | uint8_t mFlags; |
| 231 | |
| 232 | public: |
| 233 | // have to create class due to this bool conversion operator... |
| 234 | constexpr operator bool() const { |
| 235 | return mFlags & IS_ENABLED; |
| 236 | } |
| 237 | |
| 238 | constexpr Switch operator!() const { |
| 239 | return Switch(mFlags ^ IS_ENABLED); |
| 240 | } |
| 241 | |
| 242 | static constexpr Switch DISABLED() { return 0; }; |
| 243 | static constexpr Switch ENABLED() { return IS_ENABLED; }; |
| 244 | static constexpr Switch DISABLED_BY_DEFAULT() { return BY_DEFAULT; }; |
| 245 | static constexpr Switch ENABLED_BY_DEFAULT() { return IS_ENABLED | BY_DEFAULT; }; |
| 246 | |
| 247 | const char *toString(const char *def = "??") const { |
| 248 | switch (mFlags) { |
| 249 | case 0: return "0"; |
| 250 | case IS_ENABLED: return "1"; |
| 251 | case BY_DEFAULT: return "(0)"; |
| 252 | case IS_ENABLED | BY_DEFAULT: return "(1)"; |
| 253 | default: return def; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | }; |
| 258 | |
| 259 | const char *asString(const Switch &s, const char *def = "??") { |
| 260 | return s.toString(def); |
| 261 | } |
| 262 | |
| 263 | Switch isSettingEnabled( |
| 264 | std::string setting, const MediaCodecsXmlParser::AttributeMap &settings, |
| 265 | Switch def = Switch::DISABLED_BY_DEFAULT()) { |
| 266 | const auto enablement = settings.find(setting); |
| 267 | if (enablement == settings.end()) { |
| 268 | return def; |
| 269 | } |
| 270 | return enablement->second == "1" ? Switch::ENABLED() : Switch::DISABLED(); |
| 271 | } |
| 272 | |
| 273 | Switch isVariantEnabled( |
| 274 | std::string variant, const MediaCodecsXmlParser::AttributeMap &settings) { |
| 275 | return isSettingEnabled("variant-" + variant, settings); |
| 276 | } |
| 277 | |
| 278 | Switch isVariantExpressionEnabled( |
| 279 | std::string exp, const MediaCodecsXmlParser::AttributeMap &settings) { |
| 280 | if (!exp.empty() && exp.at(0) == '!') { |
| 281 | return !isVariantEnabled(exp.substr(1, exp.size() - 1), settings); |
| 282 | } |
| 283 | return isVariantEnabled(exp, settings); |
| 284 | } |
| 285 | |
| 286 | Switch isDomainEnabled( |
| 287 | std::string domain, const MediaCodecsXmlParser::AttributeMap &settings) { |
| 288 | return isSettingEnabled("domain-" + domain, settings); |
| 289 | } |
| 290 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 291 | } // unnamed namespace |
| 292 | |
| 293 | status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) { |
| 294 | // TODO: Remove run-time configurations once all codecs are working |
| 295 | // properly. (Assume "full" behavior eventually.) |
| 296 | // |
| 297 | // debug.stagefright.ccodec supports 5 values. |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 298 | // 0 - No Codec 2.0 components are available. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 299 | // 1 - Audio decoders and encoders with prefix "c2.android." are available |
| 300 | // and ranked first. |
| 301 | // All other components with prefix "c2.android." are available with |
| 302 | // their normal ranks. |
| 303 | // Components with prefix "c2.vda." are available with their normal |
| 304 | // ranks. |
| 305 | // All other components with suffix ".avc.decoder" or ".avc.encoder" |
| 306 | // are available but ranked last. |
| 307 | // 2 - Components with prefix "c2.android." are available and ranked |
| 308 | // first. |
| 309 | // Components with prefix "c2.vda." are available with their normal |
| 310 | // ranks. |
| 311 | // All other components with suffix ".avc.decoder" or ".avc.encoder" |
| 312 | // are available but ranked last. |
| 313 | // 3 - Components with prefix "c2.android." are available and ranked |
| 314 | // first. |
| 315 | // All other components are available with their normal ranks. |
| 316 | // 4 - All components are available with their normal ranks. |
| 317 | // |
| 318 | // The default value (boot time) is 1. |
| 319 | // |
| 320 | // Note: Currently, OMX components have default rank 0x100, while all |
| 321 | // Codec2.0 software components have default rank 0x200. |
Lajos Molnar | 8635fc8 | 2019-05-17 17:35:10 +0000 | [diff] [blame] | 322 | int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 4); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 323 | |
| 324 | // Obtain Codec2Client |
| 325 | std::vector<Traits> traits = Codec2Client::ListComponents(); |
| 326 | |
Pawin Vongmasa | 7f5e10e | 2020-03-07 04:09:55 -0800 | [diff] [blame] | 327 | // parse APEX XML first, followed by vendor XML. |
| 328 | // Note: APEX XML names do not depend on ro.media.xml_variant.* properties. |
Lajos Molnar | da66689 | 2019-04-08 17:25:34 -0700 | [diff] [blame] | 329 | MediaCodecsXmlParser parser; |
| 330 | parser.parseXmlFilesInSearchDirs( |
Pawin Vongmasa | 7f5e10e | 2020-03-07 04:09:55 -0800 | [diff] [blame] | 331 | { "media_codecs.xml", "media_codecs_performance.xml" }, |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 332 | { "/apex/com.android.media.swcodec/etc" }); |
| 333 | |
| 334 | // TODO: remove these c2-specific files once product moved to default file names |
| 335 | parser.parseXmlFilesInSearchDirs( |
Lajos Molnar | da66689 | 2019-04-08 17:25:34 -0700 | [diff] [blame] | 336 | { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" }); |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 337 | |
| 338 | // parse default XML files |
| 339 | parser.parseXmlFilesInSearchDirs(); |
| 340 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 341 | if (parser.getParsingStatus() != OK) { |
| 342 | ALOGD("XML parser no good"); |
| 343 | return OK; |
| 344 | } |
| 345 | |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 346 | MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap(); |
| 347 | for (const auto &v : settings) { |
| 348 | if (!hasPrefix(v.first, "media-type-") |
| 349 | && !hasPrefix(v.first, "domain-") |
| 350 | && !hasPrefix(v.first, "variant-")) { |
| 351 | writer->addGlobalSetting(v.first.c_str(), v.second.c_str()); |
| 352 | } |
| 353 | } |
| 354 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 355 | for (const Traits& trait : traits) { |
| 356 | C2Component::rank_t rank = trait.rank; |
| 357 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 358 | // Interface must be accessible for us to list the component, and there also |
| 359 | // must be an XML entry for the codec. Codec aliases listed in the traits |
| 360 | // allow additional XML entries to be specified for each alias. These will |
| 361 | // be listed as separate codecs. If no XML entry is specified for an alias, |
| 362 | // those will be treated as an additional alias specified in the XML entry |
| 363 | // for the interface name. |
| 364 | std::vector<std::string> nameAndAliases = trait.aliases; |
| 365 | nameAndAliases.insert(nameAndAliases.begin(), trait.name); |
| 366 | for (const std::string &nameOrAlias : nameAndAliases) { |
| 367 | bool isAlias = trait.name != nameOrAlias; |
| 368 | std::shared_ptr<Codec2Client::Interface> intf = |
| 369 | Codec2Client::CreateInterfaceByName(nameOrAlias.c_str()); |
| 370 | if (!intf) { |
| 371 | ALOGD("could not create interface for %s'%s'", |
| 372 | isAlias ? "alias " : "", |
| 373 | nameOrAlias.c_str()); |
| 374 | continue; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 375 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 376 | if (parser.getCodecMap().count(nameOrAlias) == 0) { |
| 377 | if (isAlias) { |
| 378 | std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo = |
| 379 | writer->findMediaCodecInfo(trait.name.c_str()); |
| 380 | if (!baseCodecInfo) { |
| 381 | ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing", |
| 382 | nameOrAlias.c_str(), |
| 383 | trait.name.c_str()); |
| 384 | } else { |
| 385 | ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this", |
| 386 | nameOrAlias.c_str()); |
| 387 | // merge alias into existing codec |
| 388 | baseCodecInfo->addAlias(nameOrAlias.c_str()); |
| 389 | } |
| 390 | } else { |
| 391 | ALOGD("component '%s' not found in xml", trait.name.c_str()); |
| 392 | } |
| 393 | continue; |
| 394 | } |
| 395 | std::string canonName = trait.name; |
| 396 | |
| 397 | // TODO: Remove this block once all codecs are enabled by default. |
| 398 | switch (option) { |
| 399 | case 0: |
| 400 | continue; |
| 401 | case 1: |
| 402 | if (hasPrefix(canonName, "c2.vda.")) { |
| 403 | break; |
| 404 | } |
| 405 | if (hasPrefix(canonName, "c2.android.")) { |
| 406 | if (trait.domain == C2Component::DOMAIN_AUDIO) { |
| 407 | rank = 1; |
| 408 | break; |
| 409 | } |
| 410 | break; |
| 411 | } |
| 412 | if (hasSuffix(canonName, ".avc.decoder") || |
| 413 | hasSuffix(canonName, ".avc.encoder")) { |
| 414 | rank = std::numeric_limits<decltype(rank)>::max(); |
| 415 | break; |
| 416 | } |
| 417 | continue; |
| 418 | case 2: |
| 419 | if (hasPrefix(canonName, "c2.vda.")) { |
| 420 | break; |
| 421 | } |
| 422 | if (hasPrefix(canonName, "c2.android.")) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 423 | rank = 1; |
| 424 | break; |
| 425 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 426 | if (hasSuffix(canonName, ".avc.decoder") || |
| 427 | hasSuffix(canonName, ".avc.encoder")) { |
| 428 | rank = std::numeric_limits<decltype(rank)>::max(); |
| 429 | break; |
| 430 | } |
| 431 | continue; |
| 432 | case 3: |
| 433 | if (hasPrefix(canonName, "c2.android.")) { |
| 434 | rank = 1; |
| 435 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 436 | break; |
| 437 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 438 | |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 439 | const MediaCodecsXmlParser::CodecProperties &codec = |
| 440 | parser.getCodecMap().at(nameOrAlias); |
| 441 | |
| 442 | // verify that either the codec is explicitly enabled, or one of its domains is |
| 443 | bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end(); |
| 444 | if (!codecEnabled) { |
| 445 | for (const std::string &domain : codec.domainSet) { |
| 446 | const Switch enabled = isDomainEnabled(domain, settings); |
| 447 | ALOGV("codec entry '%s' is in domain '%s' that is '%s'", |
| 448 | nameOrAlias.c_str(), domain.c_str(), asString(enabled)); |
| 449 | if (enabled) { |
| 450 | codecEnabled = true; |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | // if codec has variants, also check that at least one of them is enabled |
| 456 | bool variantEnabled = codec.variantSet.empty(); |
| 457 | for (const std::string &variant : codec.variantSet) { |
| 458 | const Switch enabled = isVariantExpressionEnabled(variant, settings); |
| 459 | ALOGV("codec entry '%s' has a variant '%s' that is '%s'", |
| 460 | nameOrAlias.c_str(), variant.c_str(), asString(enabled)); |
| 461 | if (enabled) { |
| 462 | variantEnabled = true; |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | if (!codecEnabled || !variantEnabled) { |
| 467 | ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str()); |
| 468 | continue; |
| 469 | } |
| 470 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 471 | ALOGV("adding codec entry for '%s'", nameOrAlias.c_str()); |
| 472 | std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo(); |
| 473 | codecInfo->setName(nameOrAlias.c_str()); |
| 474 | codecInfo->setOwner(("codec2::" + trait.owner).c_str()); |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 475 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 476 | bool encoder = trait.kind == C2Component::KIND_ENCODER; |
| 477 | typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0; |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 478 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 479 | if (encoder) { |
| 480 | attrs |= MediaCodecInfo::kFlagIsEncoder; |
| 481 | } |
| 482 | if (trait.owner == "software") { |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 483 | attrs |= MediaCodecInfo::kFlagIsSoftwareOnly; |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 484 | } else { |
| 485 | attrs |= MediaCodecInfo::kFlagIsVendor; |
| 486 | if (trait.owner == "vendor-software") { |
| 487 | attrs |= MediaCodecInfo::kFlagIsSoftwareOnly; |
| 488 | } else if (codec.quirkSet.find("attribute::software-codec") |
| 489 | == codec.quirkSet.end()) { |
| 490 | attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 493 | codecInfo->setAttributes(attrs); |
| 494 | if (!codec.rank.empty()) { |
| 495 | uint32_t xmlRank; |
| 496 | char dummy; |
| 497 | if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) { |
| 498 | rank = xmlRank; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 501 | ALOGV("rank: %u", (unsigned)rank); |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 502 | codecInfo->setRank(rank); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 503 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 504 | for (const std::string &alias : codec.aliases) { |
| 505 | ALOGV("adding alias '%s'", alias.c_str()); |
| 506 | codecInfo->addAlias(alias.c_str()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 509 | for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) { |
| 510 | const std::string &mediaType = typeIt->first; |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 511 | const Switch typeEnabled = isSettingEnabled( |
| 512 | "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT()); |
| 513 | const Switch domainTypeEnabled = isSettingEnabled( |
| 514 | "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"), |
| 515 | settings, Switch::ENABLED_BY_DEFAULT()); |
| 516 | ALOGV("type '%s-%s' is '%s/%s'", |
| 517 | mediaType.c_str(), (encoder ? "encoder" : "decoder"), |
| 518 | asString(typeEnabled), asString(domainTypeEnabled)); |
| 519 | if (!typeEnabled || !domainTypeEnabled) { |
| 520 | ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(), |
| 521 | nameOrAlias.c_str()); |
| 522 | continue; |
| 523 | } |
| 524 | |
| 525 | ALOGI("adding type '%s'", typeIt->first.c_str()); |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 526 | const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second; |
| 527 | std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps = |
| 528 | codecInfo->addMediaType(mediaType.c_str()); |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 529 | for (const auto &v : attrMap) { |
| 530 | std::string key = v.first; |
| 531 | std::string value = v.second; |
| 532 | |
| 533 | size_t variantSep = key.find(":::"); |
| 534 | if (variantSep != std::string::npos) { |
| 535 | std::string variant = key.substr(0, variantSep); |
| 536 | const Switch enabled = isVariantExpressionEnabled(variant, settings); |
| 537 | ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled)); |
| 538 | if (!enabled) { |
| 539 | continue; |
| 540 | } |
| 541 | key = key.substr(variantSep + 3); |
| 542 | } |
| 543 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 544 | if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) { |
| 545 | int32_t intValue = 0; |
| 546 | // Ignore trailing bad characters and default to 0. |
| 547 | (void)sscanf(value.c_str(), "%d", &intValue); |
| 548 | caps->addDetail(key.c_str(), intValue); |
| 549 | } else { |
| 550 | caps->addDetail(key.c_str(), value.c_str()); |
| 551 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 552 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 553 | |
| 554 | addSupportedProfileLevels(intf, caps.get(), trait, mediaType); |
| 555 | addSupportedColorFormats(intf, caps.get(), trait, mediaType); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | } |
| 559 | return OK; |
| 560 | } |
| 561 | |
| 562 | } // namespace android |
| 563 | |
| 564 | extern "C" android::MediaCodecListBuilderBase *CreateBuilder() { |
| 565 | return new android::Codec2InfoBuilder; |
| 566 | } |