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 | |
Ray Essick | 8c4e9c7 | 2021-03-15 15:25:21 -0700 | [diff] [blame^] | 341 | // The mainline modules for media may optionally include some codec shaping information. |
| 342 | // Based on vendor partition SDK, and the brand/product/device information |
| 343 | // (expect to be empty in almost always) |
| 344 | // |
| 345 | { |
| 346 | // get build info so we know what file to search |
| 347 | // ro.vendor.build.fingerprint |
| 348 | std::string fingerprint = base::GetProperty("ro.vendor.build.fingerprint", |
| 349 | "brand/product/device:"); |
| 350 | ALOGV("property_get for ro.vendor.build.fingerprint == '%s'", fingerprint.c_str()); |
| 351 | |
| 352 | // ro.vendor.build.version.sdk |
| 353 | std::string sdk = base::GetProperty("ro.vendor.build.version.sdk", "0"); |
| 354 | ALOGV("property_get for ro.vendor.build.version.sdk == '%s'", sdk.c_str()); |
| 355 | |
| 356 | std::string brand; |
| 357 | std::string product; |
| 358 | std::string device; |
| 359 | size_t pos1; |
| 360 | pos1 = fingerprint.find('/'); |
| 361 | if (pos1 != std::string::npos) { |
| 362 | brand = fingerprint.substr(0, pos1); |
| 363 | size_t pos2 = fingerprint.find('/', pos1+1); |
| 364 | if (pos2 != std::string::npos) { |
| 365 | product = fingerprint.substr(pos1+1, pos2 - pos1 - 1); |
| 366 | size_t pos3 = fingerprint.find('/', pos2+1); |
| 367 | if (pos3 != std::string::npos) { |
| 368 | device = fingerprint.substr(pos2+1, pos3 - pos2 - 1); |
| 369 | size_t pos4 = device.find(':'); |
| 370 | if (pos4 != std::string::npos) { |
| 371 | device.resize(pos4); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | ALOGV("parsed: sdk '%s' brand '%s' product '%s' device '%s'", |
| 378 | sdk.c_str(), brand.c_str(), product.c_str(), device.c_str()); |
| 379 | |
| 380 | std::string base = "/apex/com.android.media/etc/formatshaper"; |
| 381 | |
| 382 | // looking in these directories within the apex |
| 383 | const std::vector<std::string> modulePathnames = { |
| 384 | base + "/" + sdk + "/" + brand + "/" + product + "/" + device, |
| 385 | base + "/" + sdk + "/" + brand + "/" + product, |
| 386 | base + "/" + sdk + "/" + brand, |
| 387 | base + "/" + sdk, |
| 388 | base |
| 389 | }; |
| 390 | |
| 391 | parser.parseXmlFilesInSearchDirs( { "media_codecs_shaping.xml" }, modulePathnames); |
| 392 | } |
| 393 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 394 | if (parser.getParsingStatus() != OK) { |
| 395 | ALOGD("XML parser no good"); |
| 396 | return OK; |
| 397 | } |
| 398 | |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 399 | MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap(); |
| 400 | for (const auto &v : settings) { |
| 401 | if (!hasPrefix(v.first, "media-type-") |
| 402 | && !hasPrefix(v.first, "domain-") |
| 403 | && !hasPrefix(v.first, "variant-")) { |
| 404 | writer->addGlobalSetting(v.first.c_str(), v.second.c_str()); |
| 405 | } |
| 406 | } |
| 407 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 408 | for (const Traits& trait : traits) { |
| 409 | C2Component::rank_t rank = trait.rank; |
| 410 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 411 | // Interface must be accessible for us to list the component, and there also |
| 412 | // must be an XML entry for the codec. Codec aliases listed in the traits |
| 413 | // allow additional XML entries to be specified for each alias. These will |
| 414 | // be listed as separate codecs. If no XML entry is specified for an alias, |
| 415 | // those will be treated as an additional alias specified in the XML entry |
| 416 | // for the interface name. |
| 417 | std::vector<std::string> nameAndAliases = trait.aliases; |
| 418 | nameAndAliases.insert(nameAndAliases.begin(), trait.name); |
| 419 | for (const std::string &nameOrAlias : nameAndAliases) { |
| 420 | bool isAlias = trait.name != nameOrAlias; |
| 421 | std::shared_ptr<Codec2Client::Interface> intf = |
| 422 | Codec2Client::CreateInterfaceByName(nameOrAlias.c_str()); |
| 423 | if (!intf) { |
| 424 | ALOGD("could not create interface for %s'%s'", |
| 425 | isAlias ? "alias " : "", |
| 426 | nameOrAlias.c_str()); |
| 427 | continue; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 428 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 429 | if (parser.getCodecMap().count(nameOrAlias) == 0) { |
| 430 | if (isAlias) { |
| 431 | std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo = |
| 432 | writer->findMediaCodecInfo(trait.name.c_str()); |
| 433 | if (!baseCodecInfo) { |
| 434 | ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing", |
| 435 | nameOrAlias.c_str(), |
| 436 | trait.name.c_str()); |
| 437 | } else { |
| 438 | ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this", |
| 439 | nameOrAlias.c_str()); |
| 440 | // merge alias into existing codec |
| 441 | baseCodecInfo->addAlias(nameOrAlias.c_str()); |
| 442 | } |
| 443 | } else { |
| 444 | ALOGD("component '%s' not found in xml", trait.name.c_str()); |
| 445 | } |
| 446 | continue; |
| 447 | } |
| 448 | std::string canonName = trait.name; |
| 449 | |
| 450 | // TODO: Remove this block once all codecs are enabled by default. |
| 451 | switch (option) { |
| 452 | case 0: |
| 453 | continue; |
| 454 | case 1: |
| 455 | if (hasPrefix(canonName, "c2.vda.")) { |
| 456 | break; |
| 457 | } |
| 458 | if (hasPrefix(canonName, "c2.android.")) { |
| 459 | if (trait.domain == C2Component::DOMAIN_AUDIO) { |
| 460 | rank = 1; |
| 461 | break; |
| 462 | } |
| 463 | break; |
| 464 | } |
| 465 | if (hasSuffix(canonName, ".avc.decoder") || |
| 466 | hasSuffix(canonName, ".avc.encoder")) { |
| 467 | rank = std::numeric_limits<decltype(rank)>::max(); |
| 468 | break; |
| 469 | } |
| 470 | continue; |
| 471 | case 2: |
| 472 | if (hasPrefix(canonName, "c2.vda.")) { |
| 473 | break; |
| 474 | } |
| 475 | if (hasPrefix(canonName, "c2.android.")) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 476 | rank = 1; |
| 477 | break; |
| 478 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 479 | if (hasSuffix(canonName, ".avc.decoder") || |
| 480 | hasSuffix(canonName, ".avc.encoder")) { |
| 481 | rank = std::numeric_limits<decltype(rank)>::max(); |
| 482 | break; |
| 483 | } |
| 484 | continue; |
| 485 | case 3: |
| 486 | if (hasPrefix(canonName, "c2.android.")) { |
| 487 | rank = 1; |
| 488 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 489 | break; |
| 490 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 491 | |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 492 | const MediaCodecsXmlParser::CodecProperties &codec = |
| 493 | parser.getCodecMap().at(nameOrAlias); |
| 494 | |
| 495 | // verify that either the codec is explicitly enabled, or one of its domains is |
| 496 | bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end(); |
| 497 | if (!codecEnabled) { |
| 498 | for (const std::string &domain : codec.domainSet) { |
| 499 | const Switch enabled = isDomainEnabled(domain, settings); |
| 500 | ALOGV("codec entry '%s' is in domain '%s' that is '%s'", |
| 501 | nameOrAlias.c_str(), domain.c_str(), asString(enabled)); |
| 502 | if (enabled) { |
| 503 | codecEnabled = true; |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | // if codec has variants, also check that at least one of them is enabled |
| 509 | bool variantEnabled = codec.variantSet.empty(); |
| 510 | for (const std::string &variant : codec.variantSet) { |
| 511 | const Switch enabled = isVariantExpressionEnabled(variant, settings); |
| 512 | ALOGV("codec entry '%s' has a variant '%s' that is '%s'", |
| 513 | nameOrAlias.c_str(), variant.c_str(), asString(enabled)); |
| 514 | if (enabled) { |
| 515 | variantEnabled = true; |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | if (!codecEnabled || !variantEnabled) { |
| 520 | ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str()); |
| 521 | continue; |
| 522 | } |
| 523 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 524 | ALOGV("adding codec entry for '%s'", nameOrAlias.c_str()); |
| 525 | std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo(); |
| 526 | codecInfo->setName(nameOrAlias.c_str()); |
| 527 | codecInfo->setOwner(("codec2::" + trait.owner).c_str()); |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 528 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 529 | bool encoder = trait.kind == C2Component::KIND_ENCODER; |
| 530 | typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0; |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 531 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 532 | if (encoder) { |
| 533 | attrs |= MediaCodecInfo::kFlagIsEncoder; |
| 534 | } |
| 535 | if (trait.owner == "software") { |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 536 | attrs |= MediaCodecInfo::kFlagIsSoftwareOnly; |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 537 | } else { |
| 538 | attrs |= MediaCodecInfo::kFlagIsVendor; |
| 539 | if (trait.owner == "vendor-software") { |
| 540 | attrs |= MediaCodecInfo::kFlagIsSoftwareOnly; |
| 541 | } else if (codec.quirkSet.find("attribute::software-codec") |
| 542 | == codec.quirkSet.end()) { |
| 543 | attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 544 | } |
| 545 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 546 | codecInfo->setAttributes(attrs); |
| 547 | if (!codec.rank.empty()) { |
| 548 | uint32_t xmlRank; |
| 549 | char dummy; |
| 550 | if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) { |
| 551 | rank = xmlRank; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 552 | } |
| 553 | } |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 554 | ALOGV("rank: %u", (unsigned)rank); |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 555 | codecInfo->setRank(rank); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 556 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 557 | for (const std::string &alias : codec.aliases) { |
| 558 | ALOGV("adding alias '%s'", alias.c_str()); |
| 559 | codecInfo->addAlias(alias.c_str()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 562 | for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) { |
| 563 | const std::string &mediaType = typeIt->first; |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 564 | const Switch typeEnabled = isSettingEnabled( |
| 565 | "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT()); |
| 566 | const Switch domainTypeEnabled = isSettingEnabled( |
| 567 | "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"), |
| 568 | settings, Switch::ENABLED_BY_DEFAULT()); |
| 569 | ALOGV("type '%s-%s' is '%s/%s'", |
| 570 | mediaType.c_str(), (encoder ? "encoder" : "decoder"), |
| 571 | asString(typeEnabled), asString(domainTypeEnabled)); |
| 572 | if (!typeEnabled || !domainTypeEnabled) { |
| 573 | ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(), |
| 574 | nameOrAlias.c_str()); |
| 575 | continue; |
| 576 | } |
| 577 | |
| 578 | ALOGI("adding type '%s'", typeIt->first.c_str()); |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 579 | const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second; |
| 580 | std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps = |
| 581 | codecInfo->addMediaType(mediaType.c_str()); |
Lajos Molnar | 424cfb5 | 2019-04-08 17:48:00 -0700 | [diff] [blame] | 582 | for (const auto &v : attrMap) { |
| 583 | std::string key = v.first; |
| 584 | std::string value = v.second; |
| 585 | |
| 586 | size_t variantSep = key.find(":::"); |
| 587 | if (variantSep != std::string::npos) { |
| 588 | std::string variant = key.substr(0, variantSep); |
| 589 | const Switch enabled = isVariantExpressionEnabled(variant, settings); |
| 590 | ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled)); |
| 591 | if (!enabled) { |
| 592 | continue; |
| 593 | } |
| 594 | key = key.substr(variantSep + 3); |
| 595 | } |
| 596 | |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 597 | if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) { |
| 598 | int32_t intValue = 0; |
| 599 | // Ignore trailing bad characters and default to 0. |
| 600 | (void)sscanf(value.c_str(), "%d", &intValue); |
| 601 | caps->addDetail(key.c_str(), intValue); |
| 602 | } else { |
| 603 | caps->addDetail(key.c_str(), value.c_str()); |
| 604 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 605 | } |
Lajos Molnar | db5751f | 2019-01-31 17:01:49 -0800 | [diff] [blame] | 606 | |
| 607 | addSupportedProfileLevels(intf, caps.get(), trait, mediaType); |
| 608 | addSupportedColorFormats(intf, caps.get(), trait, mediaType); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | } |
| 612 | return OK; |
| 613 | } |
| 614 | |
| 615 | } // namespace android |
| 616 | |
| 617 | extern "C" android::MediaCodecListBuilderBase *CreateBuilder() { |
| 618 | return new android::Codec2InfoBuilder; |
| 619 | } |