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> |
| 46 | #include <media/stagefright/MediaCodecConstants.h> |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 47 | #include <media/stagefright/foundation/ALookup.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 48 | #include <media/stagefright/foundation/MediaDefs.h> |
| 49 | #include <media/stagefright/omx/OMXUtils.h> |
| 50 | #include <media/stagefright/xmlparser/MediaCodecsXmlParser.h> |
| 51 | |
| 52 | #include "Codec2InfoBuilder.h" |
| 53 | |
| 54 | namespace android { |
| 55 | |
| 56 | using Traits = C2Component::Traits; |
| 57 | |
| 58 | namespace /* unnamed */ { |
| 59 | |
| 60 | bool hasPrefix(const std::string& s, const char* prefix) { |
| 61 | size_t prefixLen = strlen(prefix); |
| 62 | return s.compare(0, prefixLen, prefix) == 0; |
| 63 | } |
| 64 | |
| 65 | bool hasSuffix(const std::string& s, const char* suffix) { |
| 66 | size_t suffixLen = strlen(suffix); |
| 67 | return suffixLen > s.size() ? false : |
| 68 | s.compare(s.size() - suffixLen, suffixLen, suffix) == 0; |
| 69 | } |
| 70 | |
| 71 | // Constants from ACodec |
| 72 | constexpr OMX_U32 kPortIndexInput = 0; |
| 73 | constexpr OMX_U32 kPortIndexOutput = 1; |
| 74 | constexpr OMX_U32 kMaxIndicesToCheck = 32; |
| 75 | |
| 76 | status_t queryOmxCapabilities( |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 77 | const char* name, const char* mediaType, bool isEncoder, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 78 | MediaCodecInfo::CapabilitiesWriter* caps) { |
| 79 | |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 80 | const char *role = GetComponentRole(isEncoder, mediaType); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 81 | if (role == nullptr) { |
| 82 | return BAD_VALUE; |
| 83 | } |
| 84 | |
| 85 | using namespace ::android::hardware::media::omx::V1_0; |
| 86 | using ::android::hardware::Return; |
| 87 | using ::android::hardware::Void; |
| 88 | using ::android::hardware::hidl_vec; |
| 89 | using ::android::hardware::media::omx::V1_0::utils::LWOmxNode; |
| 90 | |
| 91 | sp<IOmx> omx = IOmx::getService(); |
| 92 | if (!omx) { |
| 93 | ALOGW("Could not obtain IOmx service."); |
| 94 | return NO_INIT; |
| 95 | } |
| 96 | |
| 97 | struct Observer : IOmxObserver { |
| 98 | virtual Return<void> onMessages(const hidl_vec<Message>&) override { |
| 99 | return Void(); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | sp<Observer> observer = new Observer(); |
| 104 | Status status; |
| 105 | sp<IOmxNode> tOmxNode; |
| 106 | Return<void> transStatus = omx->allocateNode( |
| 107 | name, observer, |
| 108 | [&status, &tOmxNode](Status s, const sp<IOmxNode>& n) { |
| 109 | status = s; |
| 110 | tOmxNode = n; |
| 111 | }); |
| 112 | if (!transStatus.isOk()) { |
| 113 | ALOGW("IOmx::allocateNode -- transaction failed."); |
| 114 | return NO_INIT; |
| 115 | } |
| 116 | if (status != Status::OK) { |
| 117 | ALOGW("IOmx::allocateNode -- error returned: %d.", |
| 118 | static_cast<int>(status)); |
| 119 | return NO_INIT; |
| 120 | } |
| 121 | |
| 122 | sp<LWOmxNode> omxNode = new LWOmxNode(tOmxNode); |
| 123 | |
| 124 | status_t err = SetComponentRole(omxNode, role); |
| 125 | if (err != OK) { |
| 126 | omxNode->freeNode(); |
| 127 | ALOGW("Failed to SetComponentRole: component = %s, role = %s.", |
| 128 | name, role); |
| 129 | return err; |
| 130 | } |
| 131 | |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 132 | bool isVideo = hasPrefix(mediaType, "video/") == 0; |
| 133 | bool isImage = hasPrefix(mediaType, "image/") == 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 134 | |
| 135 | if (isVideo || isImage) { |
| 136 | OMX_VIDEO_PARAM_PROFILELEVELTYPE param; |
| 137 | InitOMXParams(¶m); |
| 138 | param.nPortIndex = isEncoder ? kPortIndexOutput : kPortIndexInput; |
| 139 | |
| 140 | for (OMX_U32 index = 0; index <= kMaxIndicesToCheck; ++index) { |
| 141 | param.nProfileIndex = index; |
| 142 | status_t err = omxNode->getParameter( |
| 143 | OMX_IndexParamVideoProfileLevelQuerySupported, |
| 144 | ¶m, sizeof(param)); |
| 145 | if (err != OK) { |
| 146 | break; |
| 147 | } |
| 148 | caps->addProfileLevel(param.eProfile, param.eLevel); |
| 149 | |
| 150 | // AVC components may not list the constrained profiles explicitly, but |
| 151 | // decoders that support a profile also support its constrained version. |
| 152 | // Encoders must explicitly support constrained profiles. |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 153 | if (!isEncoder && strcasecmp(mediaType, MEDIA_MIMETYPE_VIDEO_AVC) == 0) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 154 | if (param.eProfile == OMX_VIDEO_AVCProfileHigh) { |
| 155 | caps->addProfileLevel(OMX_VIDEO_AVCProfileConstrainedHigh, param.eLevel); |
| 156 | } else if (param.eProfile == OMX_VIDEO_AVCProfileBaseline) { |
| 157 | caps->addProfileLevel(OMX_VIDEO_AVCProfileConstrainedBaseline, param.eLevel); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (index == kMaxIndicesToCheck) { |
| 162 | ALOGW("[%s] stopping checking profiles after %u: %x/%x", |
| 163 | name, index, |
| 164 | param.eProfile, param.eLevel); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Color format query |
| 169 | // return colors in the order reported by the OMX component |
| 170 | // prefix "flexible" standard ones with the flexible equivalent |
| 171 | OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat; |
| 172 | InitOMXParams(&portFormat); |
| 173 | portFormat.nPortIndex = isEncoder ? kPortIndexInput : kPortIndexOutput; |
| 174 | for (OMX_U32 index = 0; index <= kMaxIndicesToCheck; ++index) { |
| 175 | portFormat.nIndex = index; |
| 176 | status_t err = omxNode->getParameter( |
| 177 | OMX_IndexParamVideoPortFormat, |
| 178 | &portFormat, sizeof(portFormat)); |
| 179 | if (err != OK) { |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | OMX_U32 flexibleEquivalent; |
| 184 | if (IsFlexibleColorFormat( |
| 185 | omxNode, portFormat.eColorFormat, false /* usingNativeWindow */, |
| 186 | &flexibleEquivalent)) { |
| 187 | caps->addColorFormat(flexibleEquivalent); |
| 188 | } |
| 189 | caps->addColorFormat(portFormat.eColorFormat); |
| 190 | |
| 191 | if (index == kMaxIndicesToCheck) { |
| 192 | ALOGW("[%s] stopping checking formats after %u: %s(%x)", |
| 193 | name, index, |
| 194 | asString(portFormat.eColorFormat), portFormat.eColorFormat); |
| 195 | } |
| 196 | } |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 197 | } else if (strcasecmp(mediaType, MEDIA_MIMETYPE_AUDIO_AAC) == 0) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 198 | // More audio codecs if they have profiles. |
| 199 | OMX_AUDIO_PARAM_ANDROID_PROFILETYPE param; |
| 200 | InitOMXParams(¶m); |
| 201 | param.nPortIndex = isEncoder ? kPortIndexOutput : kPortIndexInput; |
| 202 | for (OMX_U32 index = 0; index <= kMaxIndicesToCheck; ++index) { |
| 203 | param.nProfileIndex = index; |
| 204 | status_t err = omxNode->getParameter( |
| 205 | (OMX_INDEXTYPE)OMX_IndexParamAudioProfileQuerySupported, |
| 206 | ¶m, sizeof(param)); |
| 207 | if (err != OK) { |
| 208 | break; |
| 209 | } |
| 210 | // For audio, level is ignored. |
| 211 | caps->addProfileLevel(param.eProfile, 0 /* level */); |
| 212 | |
| 213 | if (index == kMaxIndicesToCheck) { |
| 214 | ALOGW("[%s] stopping checking profiles after %u: %x", |
| 215 | name, index, |
| 216 | param.eProfile); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // NOTE: Without Android extensions, OMX does not provide a way to query |
| 221 | // AAC profile support |
| 222 | if (param.nProfileIndex == 0) { |
| 223 | ALOGW("component %s doesn't support profile query.", name); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (isVideo && !isEncoder) { |
| 228 | native_handle_t *sidebandHandle = nullptr; |
| 229 | if (omxNode->configureVideoTunnelMode( |
| 230 | kPortIndexOutput, OMX_TRUE, 0, &sidebandHandle) == OK) { |
| 231 | // tunneled playback includes adaptive playback |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 232 | } else { |
| 233 | // tunneled playback is not supported |
| 234 | caps->removeDetail(MediaCodecInfo::Capabilities::FEATURE_TUNNELED_PLAYBACK); |
| 235 | if (omxNode->setPortMode( |
| 236 | kPortIndexOutput, IOMX::kPortModeDynamicANWBuffer) == OK || |
| 237 | omxNode->prepareForAdaptivePlayback( |
| 238 | kPortIndexOutput, OMX_TRUE, |
| 239 | 1280 /* width */, 720 /* height */) != OK) { |
| 240 | // adaptive playback is not supported |
| 241 | caps->removeDetail(MediaCodecInfo::Capabilities::FEATURE_ADAPTIVE_PLAYBACK); |
| 242 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | if (isVideo && isEncoder) { |
| 247 | OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE params; |
| 248 | InitOMXParams(¶ms); |
| 249 | params.nPortIndex = kPortIndexOutput; |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 250 | |
| 251 | OMX_VIDEO_PARAM_INTRAREFRESHTYPE fallbackParams; |
| 252 | InitOMXParams(&fallbackParams); |
| 253 | fallbackParams.nPortIndex = kPortIndexOutput; |
| 254 | fallbackParams.eRefreshMode = OMX_VIDEO_IntraRefreshCyclic; |
| 255 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 256 | if (omxNode->getConfig( |
| 257 | (OMX_INDEXTYPE)OMX_IndexConfigAndroidIntraRefresh, |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 258 | ¶ms, sizeof(params)) != OK && |
| 259 | omxNode->getParameter( |
| 260 | OMX_IndexParamVideoIntraRefresh, &fallbackParams, |
| 261 | sizeof(fallbackParams)) != OK) { |
| 262 | // intra refresh is not supported |
| 263 | caps->removeDetail(MediaCodecInfo::Capabilities::FEATURE_INTRA_REFRESH); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | omxNode->freeNode(); |
| 268 | return OK; |
| 269 | } |
| 270 | |
| 271 | void buildOmxInfo(const MediaCodecsXmlParser& parser, |
| 272 | MediaCodecListWriter* writer) { |
| 273 | uint32_t omxRank = ::android::base::GetUintProperty( |
| 274 | "debug.stagefright.omx_default_rank", uint32_t(0x100)); |
| 275 | for (const MediaCodecsXmlParser::Codec& codec : parser.getCodecMap()) { |
| 276 | const std::string &name = codec.first; |
| 277 | if (!hasPrefix(codec.first, "OMX.")) { |
| 278 | continue; |
| 279 | } |
| 280 | const MediaCodecsXmlParser::CodecProperties &properties = codec.second; |
| 281 | bool encoder = properties.isEncoder; |
| 282 | std::unique_ptr<MediaCodecInfoWriter> info = |
| 283 | writer->addMediaCodecInfo(); |
| 284 | info->setName(name.c_str()); |
| 285 | info->setOwner("default"); |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 286 | typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0; |
| 287 | if (encoder) { |
| 288 | attrs |= MediaCodecInfo::kFlagIsEncoder; |
| 289 | } |
| 290 | // NOTE: we don't support software-only codecs in OMX |
| 291 | if (!hasPrefix(name, "OMX.google.")) { |
| 292 | attrs |= MediaCodecInfo::kFlagIsVendor; |
| 293 | if (properties.quirkSet.find("attribute::software-codec") |
| 294 | == properties.quirkSet.end()) { |
| 295 | attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated; |
| 296 | } |
| 297 | } |
| 298 | info->setAttributes(attrs); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 299 | info->setRank(omxRank); |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 300 | // OMX components don't have aliases |
| 301 | for (const MediaCodecsXmlParser::Type &type : properties.typeMap) { |
| 302 | const std::string &mediaType = type.first; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 303 | std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps = |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 304 | info->addMediaType(mediaType.c_str()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 305 | const MediaCodecsXmlParser::AttributeMap &attrMap = type.second; |
| 306 | for (const MediaCodecsXmlParser::Attribute& attr : attrMap) { |
| 307 | const std::string &key = attr.first; |
| 308 | const std::string &value = attr.second; |
| 309 | if (hasPrefix(key, "feature-") && |
| 310 | !hasPrefix(key, "feature-bitrate-modes")) { |
| 311 | caps->addDetail(key.c_str(), hasPrefix(value, "1") ? 1 : 0); |
| 312 | } else { |
| 313 | caps->addDetail(key.c_str(), value.c_str()); |
| 314 | } |
| 315 | } |
| 316 | status_t err = queryOmxCapabilities( |
| 317 | name.c_str(), |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 318 | mediaType.c_str(), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 319 | encoder, |
| 320 | caps.get()); |
| 321 | if (err != OK) { |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 322 | ALOGI("Failed to query capabilities for %s (media type: %s). Error: %d", |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 323 | name.c_str(), |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 324 | mediaType.c_str(), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 325 | static_cast<int>(err)); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | } // unnamed namespace |
| 332 | |
| 333 | status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) { |
| 334 | // TODO: Remove run-time configurations once all codecs are working |
| 335 | // properly. (Assume "full" behavior eventually.) |
| 336 | // |
| 337 | // debug.stagefright.ccodec supports 5 values. |
| 338 | // 0 - Only OMX components are available. |
| 339 | // 1 - Audio decoders and encoders with prefix "c2.android." are available |
| 340 | // and ranked first. |
| 341 | // All other components with prefix "c2.android." are available with |
| 342 | // their normal ranks. |
| 343 | // Components with prefix "c2.vda." are available with their normal |
| 344 | // ranks. |
| 345 | // All other components with suffix ".avc.decoder" or ".avc.encoder" |
| 346 | // are available but ranked last. |
| 347 | // 2 - Components with prefix "c2.android." are available and ranked |
| 348 | // first. |
| 349 | // Components with prefix "c2.vda." are available with their normal |
| 350 | // ranks. |
| 351 | // All other components with suffix ".avc.decoder" or ".avc.encoder" |
| 352 | // are available but ranked last. |
| 353 | // 3 - Components with prefix "c2.android." are available and ranked |
| 354 | // first. |
| 355 | // All other components are available with their normal ranks. |
| 356 | // 4 - All components are available with their normal ranks. |
| 357 | // |
| 358 | // The default value (boot time) is 1. |
| 359 | // |
| 360 | // Note: Currently, OMX components have default rank 0x100, while all |
| 361 | // Codec2.0 software components have default rank 0x200. |
| 362 | int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 1); |
| 363 | |
| 364 | // Obtain Codec2Client |
| 365 | std::vector<Traits> traits = Codec2Client::ListComponents(); |
| 366 | |
| 367 | MediaCodecsXmlParser parser( |
| 368 | MediaCodecsXmlParser::defaultSearchDirs, |
| 369 | option == 0 ? "media_codecs.xml" : |
| 370 | "media_codecs_c2.xml", |
| 371 | option == 0 ? "media_codecs_performance.xml" : |
| 372 | "media_codecs_performance_c2.xml"); |
| 373 | if (parser.getParsingStatus() != OK) { |
| 374 | ALOGD("XML parser no good"); |
| 375 | return OK; |
| 376 | } |
| 377 | |
| 378 | bool surfaceTest(Codec2Client::CreateInputSurface()); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 379 | if (option == 0 || (option != 4 && !surfaceTest)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 380 | buildOmxInfo(parser, writer); |
| 381 | } |
| 382 | |
| 383 | for (const Traits& trait : traits) { |
| 384 | C2Component::rank_t rank = trait.rank; |
| 385 | |
| 386 | std::shared_ptr<Codec2Client::Interface> intf = |
| 387 | Codec2Client::CreateInterfaceByName(trait.name.c_str()); |
| 388 | if (!intf || parser.getCodecMap().count(intf->getName()) == 0) { |
| 389 | ALOGD("%s not found in xml", trait.name.c_str()); |
| 390 | continue; |
| 391 | } |
| 392 | std::string canonName = intf->getName(); |
| 393 | |
| 394 | // TODO: Remove this block once all codecs are enabled by default. |
| 395 | switch (option) { |
| 396 | case 0: |
| 397 | continue; |
| 398 | case 1: |
| 399 | if (hasPrefix(canonName, "c2.vda.")) { |
| 400 | break; |
| 401 | } |
| 402 | if (hasPrefix(canonName, "c2.android.")) { |
| 403 | if (trait.domain == C2Component::DOMAIN_AUDIO) { |
| 404 | rank = 1; |
| 405 | break; |
| 406 | } |
| 407 | break; |
| 408 | } |
| 409 | if (hasSuffix(canonName, ".avc.decoder") || |
| 410 | hasSuffix(canonName, ".avc.encoder")) { |
| 411 | rank = std::numeric_limits<decltype(rank)>::max(); |
| 412 | break; |
| 413 | } |
| 414 | continue; |
| 415 | case 2: |
| 416 | if (hasPrefix(canonName, "c2.vda.")) { |
| 417 | break; |
| 418 | } |
| 419 | if (hasPrefix(canonName, "c2.android.")) { |
| 420 | rank = 1; |
| 421 | break; |
| 422 | } |
| 423 | if (hasSuffix(canonName, ".avc.decoder") || |
| 424 | hasSuffix(canonName, ".avc.encoder")) { |
| 425 | rank = std::numeric_limits<decltype(rank)>::max(); |
| 426 | break; |
| 427 | } |
| 428 | continue; |
| 429 | case 3: |
| 430 | if (hasPrefix(canonName, "c2.android.")) { |
| 431 | rank = 1; |
| 432 | } |
| 433 | break; |
| 434 | } |
| 435 | |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 436 | ALOGV("canonName = %s", canonName.c_str()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 437 | std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo(); |
| 438 | codecInfo->setName(trait.name.c_str()); |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 439 | codecInfo->setOwner(("codec2::" + trait.owner).c_str()); |
| 440 | const MediaCodecsXmlParser::CodecProperties &codec = parser.getCodecMap().at(canonName); |
| 441 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 442 | bool encoder = trait.kind == C2Component::KIND_ENCODER; |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 443 | typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0; |
| 444 | |
| 445 | if (encoder) { |
| 446 | attrs |= MediaCodecInfo::kFlagIsEncoder; |
| 447 | } |
| 448 | if (trait.owner == "software") { |
| 449 | attrs |= MediaCodecInfo::kFlagIsSoftwareOnly; |
| 450 | } else { |
| 451 | attrs |= MediaCodecInfo::kFlagIsVendor; |
| 452 | if (trait.owner == "vendor-software") { |
| 453 | attrs |= MediaCodecInfo::kFlagIsSoftwareOnly; |
| 454 | } else if (codec.quirkSet.find("attribute::software-codec") == codec.quirkSet.end()) { |
| 455 | attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated; |
| 456 | } |
| 457 | } |
| 458 | codecInfo->setAttributes(attrs); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 459 | codecInfo->setRank(rank); |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 460 | |
| 461 | for (const std::string &alias : codec.aliases) { |
| 462 | codecInfo->addAlias(alias.c_str()); |
| 463 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 464 | |
| 465 | for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) { |
| 466 | const std::string &mediaType = typeIt->first; |
| 467 | const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second; |
| 468 | std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps = |
Lajos Molnar | 8d4bdfd | 2018-11-13 14:23:49 -0800 | [diff] [blame] | 469 | codecInfo->addMediaType(mediaType.c_str()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 470 | for (auto attrIt = attrMap.begin(); attrIt != attrMap.end(); ++attrIt) { |
| 471 | std::string key, value; |
| 472 | std::tie(key, value) = *attrIt; |
| 473 | if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) { |
| 474 | caps->addDetail(key.c_str(), std::stoi(value)); |
| 475 | } else { |
| 476 | caps->addDetail(key.c_str(), value.c_str()); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | bool gotProfileLevels = false; |
| 481 | if (intf) { |
| 482 | std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper = |
| 483 | C2Mapper::GetProfileLevelMapper(trait.mediaType); |
| 484 | // if we don't know the media type, pass through all values unmapped |
| 485 | |
| 486 | // TODO: we cannot find levels that are local 'maxima' without knowing the coding |
| 487 | // e.g. H.263 level 45 and level 30 could be two values for highest level as |
| 488 | // they don't include one another. For now we use the last supported value. |
| 489 | C2StreamProfileLevelInfo pl(encoder /* output */, 0u); |
| 490 | std::vector<C2FieldSupportedValuesQuery> profileQuery = { |
| 491 | C2FieldSupportedValuesQuery::Possible(C2ParamField(&pl, &pl.profile)) |
| 492 | }; |
| 493 | |
| 494 | c2_status_t err = intf->querySupportedValues(profileQuery, C2_DONT_BLOCK); |
| 495 | ALOGV("query supported profiles -> %s | %s", |
| 496 | asString(err), asString(profileQuery[0].status)); |
| 497 | if (err == C2_OK && profileQuery[0].status == C2_OK) { |
| 498 | if (profileQuery[0].values.type == C2FieldSupportedValues::VALUES) { |
Chong Zhang | a8d5b4f | 2018-12-04 17:05:29 -0800 | [diff] [blame] | 499 | std::vector<std::shared_ptr<C2ParamDescriptor>> paramDescs; |
| 500 | c2_status_t err1 = intf->querySupportedParams(¶mDescs); |
| 501 | bool isHdr = false, isHdr10Plus = false; |
| 502 | if (err1 == C2_OK) { |
| 503 | for (const std::shared_ptr<C2ParamDescriptor> &desc : paramDescs) { |
| 504 | if ((uint32_t)desc->index() == |
| 505 | C2StreamHdr10PlusInfo::output::PARAM_TYPE) { |
| 506 | isHdr10Plus = true; |
| 507 | } else if ((uint32_t)desc->index() == |
| 508 | C2StreamHdrStaticInfo::output::PARAM_TYPE) { |
| 509 | isHdr = true; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | // For VP9, the static info is always propagated by framework. |
| 514 | isHdr |= (mediaType == MIMETYPE_VIDEO_VP9); |
| 515 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 516 | for (C2Value::Primitive profile : profileQuery[0].values.values) { |
| 517 | pl.profile = (C2Config::profile_t)profile.ref<uint32_t>(); |
| 518 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 519 | err = intf->config({&pl}, C2_DONT_BLOCK, &failures); |
| 520 | ALOGV("set profile to %u -> %s", pl.profile, asString(err)); |
| 521 | std::vector<C2FieldSupportedValuesQuery> levelQuery = { |
| 522 | C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level)) |
| 523 | }; |
| 524 | err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK); |
| 525 | ALOGV("query supported levels -> %s | %s", |
| 526 | asString(err), asString(levelQuery[0].status)); |
| 527 | if (err == C2_OK && levelQuery[0].status == C2_OK) { |
| 528 | if (levelQuery[0].values.type == C2FieldSupportedValues::VALUES |
| 529 | && levelQuery[0].values.values.size() > 0) { |
| 530 | C2Value::Primitive level = levelQuery[0].values.values.back(); |
| 531 | pl.level = (C2Config::level_t)level.ref<uint32_t>(); |
| 532 | ALOGV("supporting level: %u", pl.level); |
| 533 | int32_t sdkProfile, sdkLevel; |
| 534 | if (mapper && mapper->mapProfile(pl.profile, &sdkProfile) |
| 535 | && mapper->mapLevel(pl.level, &sdkLevel)) { |
| 536 | caps->addProfileLevel( |
| 537 | (uint32_t)sdkProfile, (uint32_t)sdkLevel); |
| 538 | gotProfileLevels = true; |
Chong Zhang | a8d5b4f | 2018-12-04 17:05:29 -0800 | [diff] [blame] | 539 | if (isHdr) { |
| 540 | auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper( |
| 541 | trait.mediaType); |
| 542 | if (hdrMapper && hdrMapper->mapProfile( |
| 543 | pl.profile, &sdkProfile)) { |
| 544 | caps->addProfileLevel( |
| 545 | (uint32_t)sdkProfile, |
| 546 | (uint32_t)sdkLevel); |
| 547 | } |
| 548 | if (isHdr10Plus) { |
| 549 | hdrMapper = C2Mapper::GetHdrProfileLevelMapper( |
| 550 | trait.mediaType, true /*isHdr10Plus*/); |
| 551 | if (hdrMapper && hdrMapper->mapProfile( |
| 552 | pl.profile, &sdkProfile)) { |
| 553 | caps->addProfileLevel( |
| 554 | (uint32_t)sdkProfile, |
| 555 | (uint32_t)sdkLevel); |
| 556 | } |
| 557 | } |
| 558 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 559 | } else if (!mapper) { |
| 560 | caps->addProfileLevel(pl.profile, pl.level); |
| 561 | gotProfileLevels = true; |
| 562 | } |
| 563 | |
| 564 | // for H.263 also advertise the second highest level if the |
| 565 | // codec supports level 45, as level 45 only covers level 10 |
| 566 | // TODO: move this to some form of a setting so it does not |
| 567 | // have to be here |
| 568 | if (mediaType == MIMETYPE_VIDEO_H263) { |
| 569 | C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED; |
| 570 | for (C2Value::Primitive v : levelQuery[0].values.values) { |
| 571 | C2Config::level_t level = |
| 572 | (C2Config::level_t)v.ref<uint32_t>(); |
| 573 | if (level < C2Config::LEVEL_H263_45 |
| 574 | && level > nextLevel) { |
| 575 | nextLevel = level; |
| 576 | } |
| 577 | } |
| 578 | if (nextLevel != C2Config::LEVEL_UNUSED |
| 579 | && nextLevel != pl.level |
| 580 | && mapper |
| 581 | && mapper->mapProfile(pl.profile, &sdkProfile) |
| 582 | && mapper->mapLevel(nextLevel, &sdkLevel)) { |
| 583 | caps->addProfileLevel( |
| 584 | (uint32_t)sdkProfile, (uint32_t)sdkLevel); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | if (!gotProfileLevels) { |
| 595 | if (mediaType == MIMETYPE_VIDEO_VP9) { |
| 596 | if (encoder) { |
| 597 | caps->addProfileLevel(VP9Profile0, VP9Level41); |
| 598 | } else { |
| 599 | caps->addProfileLevel(VP9Profile0, VP9Level5); |
| 600 | caps->addProfileLevel(VP9Profile2, VP9Level5); |
| 601 | caps->addProfileLevel(VP9Profile2HDR, VP9Level5); |
| 602 | } |
Ray Essick | 707c146 | 2018-12-05 15:21:35 -0800 | [diff] [blame] | 603 | } else if (mediaType == MIMETYPE_VIDEO_AV1 && !encoder) { |
| 604 | caps->addProfileLevel(AV1Profile0, AV1Level2); |
| 605 | caps->addProfileLevel(AV1Profile0, AV1Level21); |
| 606 | caps->addProfileLevel(AV1Profile1, AV1Level22); |
| 607 | caps->addProfileLevel(AV1Profile1, AV1Level3); |
| 608 | caps->addProfileLevel(AV1Profile2, AV1Level31); |
| 609 | caps->addProfileLevel(AV1Profile2, AV1Level32); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 610 | } else if (mediaType == MIMETYPE_VIDEO_HEVC && !encoder) { |
| 611 | caps->addProfileLevel(HEVCProfileMain, HEVCMainTierLevel51); |
| 612 | caps->addProfileLevel(HEVCProfileMainStill, HEVCMainTierLevel51); |
| 613 | } else if (mediaType == MIMETYPE_VIDEO_VP8) { |
| 614 | if (encoder) { |
| 615 | caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0); |
| 616 | } else { |
| 617 | caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0); |
| 618 | } |
| 619 | } else if (mediaType == MIMETYPE_VIDEO_AVC) { |
| 620 | if (encoder) { |
| 621 | caps->addProfileLevel(AVCProfileBaseline, AVCLevel41); |
| 622 | // caps->addProfileLevel(AVCProfileConstrainedBaseline, AVCLevel41); |
| 623 | caps->addProfileLevel(AVCProfileMain, AVCLevel41); |
| 624 | } else { |
| 625 | caps->addProfileLevel(AVCProfileBaseline, AVCLevel52); |
| 626 | caps->addProfileLevel(AVCProfileConstrainedBaseline, AVCLevel52); |
| 627 | caps->addProfileLevel(AVCProfileMain, AVCLevel52); |
| 628 | caps->addProfileLevel(AVCProfileConstrainedHigh, AVCLevel52); |
| 629 | caps->addProfileLevel(AVCProfileHigh, AVCLevel52); |
| 630 | } |
| 631 | } else if (mediaType == MIMETYPE_VIDEO_MPEG4) { |
| 632 | if (encoder) { |
| 633 | caps->addProfileLevel(MPEG4ProfileSimple, MPEG4Level2); |
| 634 | } else { |
| 635 | caps->addProfileLevel(MPEG4ProfileSimple, MPEG4Level3); |
| 636 | } |
| 637 | } else if (mediaType == MIMETYPE_VIDEO_H263) { |
| 638 | if (encoder) { |
| 639 | caps->addProfileLevel(H263ProfileBaseline, H263Level45); |
| 640 | } else { |
| 641 | caps->addProfileLevel(H263ProfileBaseline, H263Level30); |
| 642 | caps->addProfileLevel(H263ProfileBaseline, H263Level45); |
| 643 | caps->addProfileLevel(H263ProfileISWV2, H263Level30); |
| 644 | caps->addProfileLevel(H263ProfileISWV2, H263Level45); |
| 645 | } |
| 646 | } else if (mediaType == MIMETYPE_VIDEO_MPEG2 && !encoder) { |
| 647 | caps->addProfileLevel(MPEG2ProfileSimple, MPEG2LevelHL); |
| 648 | caps->addProfileLevel(MPEG2ProfileMain, MPEG2LevelHL); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // TODO: get this from intf() as well, but how do we map them to |
| 653 | // MediaCodec color formats? |
| 654 | if (mediaType.find("video") != std::string::npos) { |
| 655 | // vendor video codecs prefer opaque format |
| 656 | if (trait.name.find("android") == std::string::npos) { |
| 657 | caps->addColorFormat(COLOR_FormatSurface); |
| 658 | } |
| 659 | caps->addColorFormat(COLOR_FormatYUV420Flexible); |
| 660 | caps->addColorFormat(COLOR_FormatYUV420Planar); |
| 661 | caps->addColorFormat(COLOR_FormatYUV420SemiPlanar); |
| 662 | caps->addColorFormat(COLOR_FormatYUV420PackedPlanar); |
| 663 | caps->addColorFormat(COLOR_FormatYUV420PackedSemiPlanar); |
| 664 | // framework video encoders must support surface format, though it is unclear |
| 665 | // that they will be able to map it if it is opaque |
| 666 | if (encoder && trait.name.find("android") != std::string::npos) { |
| 667 | caps->addColorFormat(COLOR_FormatSurface); |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | return OK; |
| 673 | } |
| 674 | |
| 675 | } // namespace android |
| 676 | |
| 677 | extern "C" android::MediaCodecListBuilderBase *CreateBuilder() { |
| 678 | return new android::Codec2InfoBuilder; |
| 679 | } |
| 680 | |