• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
47 #include <media/stagefright/foundation/ALookup.h>
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 
hasPrefix(const std::string & s,const char * prefix)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 
hasSuffix(const std::string & s,const char * suffix)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 
queryOmxCapabilities(const char * name,const char * mediaType,bool isEncoder,MediaCodecInfo::CapabilitiesWriter * caps)76 status_t queryOmxCapabilities(
77         const char* name, const char* mediaType, bool isEncoder,
78         MediaCodecInfo::CapabilitiesWriter* caps) {
79 
80     const char *role = GetComponentRole(isEncoder, mediaType);
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 
132     bool isVideo = hasPrefix(mediaType, "video/") == 0;
133     bool isImage = hasPrefix(mediaType, "image/") == 0;
134 
135     if (isVideo || isImage) {
136         OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
137         InitOMXParams(&param);
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                     &param, 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.
153             if (!isEncoder && strcasecmp(mediaType, MEDIA_MIMETYPE_VIDEO_AVC) == 0) {
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         }
197     } else if (strcasecmp(mediaType, MEDIA_MIMETYPE_AUDIO_AAC) == 0) {
198         // More audio codecs if they have profiles.
199         OMX_AUDIO_PARAM_ANDROID_PROFILETYPE param;
200         InitOMXParams(&param);
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                     &param, 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
232             caps->addDetail(MediaCodecInfo::Capabilities::FEATURE_ADAPTIVE_PLAYBACK, 1);
233             caps->addDetail(MediaCodecInfo::Capabilities::FEATURE_TUNNELED_PLAYBACK, 1);
234         } else if (omxNode->setPortMode(
235                 kPortIndexOutput, IOMX::kPortModeDynamicANWBuffer) == OK ||
236                 omxNode->prepareForAdaptivePlayback(
237                 kPortIndexOutput, OMX_TRUE,
238                 1280 /* width */, 720 /* height */) == OK) {
239             caps->addDetail(MediaCodecInfo::Capabilities::FEATURE_ADAPTIVE_PLAYBACK, 1);
240         }
241     }
242 
243     if (isVideo && isEncoder) {
244         OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE params;
245         InitOMXParams(&params);
246         params.nPortIndex = kPortIndexOutput;
247         // TODO: should we verify if fallback is supported?
248         if (omxNode->getConfig(
249                 (OMX_INDEXTYPE)OMX_IndexConfigAndroidIntraRefresh,
250                 &params, sizeof(params)) == OK) {
251             caps->addDetail(MediaCodecInfo::Capabilities::FEATURE_INTRA_REFRESH, 1);
252         }
253     }
254 
255     omxNode->freeNode();
256     return OK;
257 }
258 
buildOmxInfo(const MediaCodecsXmlParser & parser,MediaCodecListWriter * writer)259 void buildOmxInfo(const MediaCodecsXmlParser& parser,
260                   MediaCodecListWriter* writer) {
261     uint32_t omxRank = ::android::base::GetUintProperty(
262             "debug.stagefright.omx_default_rank", uint32_t(0x100));
263     for (const MediaCodecsXmlParser::Codec& codec : parser.getCodecMap()) {
264         const std::string &name = codec.first;
265         if (!hasPrefix(codec.first, "OMX.")) {
266             continue;
267         }
268         const MediaCodecsXmlParser::CodecProperties &properties = codec.second;
269         bool encoder = properties.isEncoder;
270         std::unique_ptr<MediaCodecInfoWriter> info =
271                 writer->addMediaCodecInfo();
272         info->setName(name.c_str());
273         info->setOwner("default");
274         typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
275         if (encoder) {
276             attrs |= MediaCodecInfo::kFlagIsEncoder;
277         }
278         // NOTE: we don't support software-only codecs in OMX
279         if (!hasPrefix(name, "OMX.google.")) {
280             attrs |= MediaCodecInfo::kFlagIsVendor;
281             if (properties.quirkSet.find("attribute::software-codec")
282                     == properties.quirkSet.end()) {
283                 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
284             }
285         }
286         info->setAttributes(attrs);
287         info->setRank(omxRank);
288         // OMX components don't have aliases
289         for (const MediaCodecsXmlParser::Type &type : properties.typeMap) {
290             const std::string &mediaType = type.first;
291             std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
292                     info->addMediaType(mediaType.c_str());
293             const MediaCodecsXmlParser::AttributeMap &attrMap = type.second;
294             for (const MediaCodecsXmlParser::Attribute& attr : attrMap) {
295                 const std::string &key = attr.first;
296                 const std::string &value = attr.second;
297                 if (hasPrefix(key, "feature-") &&
298                         !hasPrefix(key, "feature-bitrate-modes")) {
299                     caps->addDetail(key.c_str(), hasPrefix(value, "1") ? 1 : 0);
300                 } else {
301                     caps->addDetail(key.c_str(), value.c_str());
302                 }
303             }
304             status_t err = queryOmxCapabilities(
305                     name.c_str(),
306                     mediaType.c_str(),
307                     encoder,
308                     caps.get());
309             if (err != OK) {
310                 ALOGI("Failed to query capabilities for %s (media type: %s). Error: %d",
311                         name.c_str(),
312                         mediaType.c_str(),
313                         static_cast<int>(err));
314             }
315         }
316     }
317 }
318 
319 } // unnamed namespace
320 
buildMediaCodecList(MediaCodecListWriter * writer)321 status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
322     // TODO: Remove run-time configurations once all codecs are working
323     // properly. (Assume "full" behavior eventually.)
324     //
325     // debug.stagefright.ccodec supports 5 values.
326     //   0 - Only OMX components are available.
327     //   1 - Audio decoders and encoders with prefix "c2.android." are available
328     //       and ranked first.
329     //       All other components with prefix "c2.android." are available with
330     //       their normal ranks.
331     //       Components with prefix "c2.vda." are available with their normal
332     //       ranks.
333     //       All other components with suffix ".avc.decoder" or ".avc.encoder"
334     //       are available but ranked last.
335     //   2 - Components with prefix "c2.android." are available and ranked
336     //       first.
337     //       Components with prefix "c2.vda." are available with their normal
338     //       ranks.
339     //       All other components with suffix ".avc.decoder" or ".avc.encoder"
340     //       are available but ranked last.
341     //   3 - Components with prefix "c2.android." are available and ranked
342     //       first.
343     //       All other components are available with their normal ranks.
344     //   4 - All components are available with their normal ranks.
345     //
346     // The default value (boot time) is 1.
347     //
348     // Note: Currently, OMX components have default rank 0x100, while all
349     // Codec2.0 software components have default rank 0x200.
350     int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 1);
351 
352     // Obtain Codec2Client
353     std::vector<Traits> traits = Codec2Client::ListComponents();
354 
355     MediaCodecsXmlParser parser;
356     if (option == 0) {
357         parser.parseXmlFilesInSearchDirs();
358     } else {
359         parser.parseXmlFilesInSearchDirs(
360                 { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
361     }
362     if (parser.getParsingStatus() != OK) {
363         ALOGD("XML parser no good");
364         return OK;
365     }
366 
367     bool surfaceTest(Codec2Client::CreateInputSurface());
368     if (option == 0 || (option != 4 && !surfaceTest)) {
369         buildOmxInfo(parser, writer);
370     }
371 
372     for (const Traits& trait : traits) {
373         C2Component::rank_t rank = trait.rank;
374 
375         std::shared_ptr<Codec2Client::Interface> intf =
376             Codec2Client::CreateInterfaceByName(trait.name.c_str());
377         if (!intf || parser.getCodecMap().count(intf->getName()) == 0) {
378             ALOGD("%s not found in xml", trait.name.c_str());
379             continue;
380         }
381         std::string canonName = intf->getName();
382 
383         // TODO: Remove this block once all codecs are enabled by default.
384         switch (option) {
385         case 0:
386             continue;
387         case 1:
388             if (hasPrefix(canonName, "c2.vda.")) {
389                 break;
390             }
391             if (hasPrefix(canonName, "c2.android.")) {
392                 if (trait.domain == C2Component::DOMAIN_AUDIO) {
393                     rank = 1;
394                     break;
395                 }
396                 break;
397             }
398             if (hasSuffix(canonName, ".avc.decoder") ||
399                     hasSuffix(canonName, ".avc.encoder")) {
400                 rank = std::numeric_limits<decltype(rank)>::max();
401                 break;
402             }
403             continue;
404         case 2:
405             if (hasPrefix(canonName, "c2.vda.")) {
406                 break;
407             }
408             if (hasPrefix(canonName, "c2.android.")) {
409                 rank = 1;
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 3:
419             if (hasPrefix(canonName, "c2.android.")) {
420                 rank = 1;
421             }
422             break;
423         }
424 
425         ALOGV("canonName = %s", canonName.c_str());
426         std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
427         codecInfo->setName(trait.name.c_str());
428         codecInfo->setOwner(("codec2::" + trait.owner).c_str());
429         const MediaCodecsXmlParser::CodecProperties &codec = parser.getCodecMap().at(canonName);
430 
431         bool encoder = trait.kind == C2Component::KIND_ENCODER;
432         typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
433 
434         if (encoder) {
435             attrs |= MediaCodecInfo::kFlagIsEncoder;
436         }
437         if (trait.owner == "software") {
438             attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
439         } else {
440             attrs |= MediaCodecInfo::kFlagIsVendor;
441             if (trait.owner == "vendor-software") {
442                 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
443             } else if (codec.quirkSet.find("attribute::software-codec") == codec.quirkSet.end()) {
444                 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
445             }
446         }
447         codecInfo->setAttributes(attrs);
448         codecInfo->setRank(rank);
449 
450         for (const std::string &alias : codec.aliases) {
451             codecInfo->addAlias(alias.c_str());
452         }
453 
454         for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
455             const std::string &mediaType = typeIt->first;
456             const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
457             std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
458                 codecInfo->addMediaType(mediaType.c_str());
459             for (auto attrIt = attrMap.begin(); attrIt != attrMap.end(); ++attrIt) {
460                 std::string key, value;
461                 std::tie(key, value) = *attrIt;
462                 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
463                     caps->addDetail(key.c_str(), std::stoi(value));
464                 } else {
465                     caps->addDetail(key.c_str(), value.c_str());
466                 }
467             }
468 
469             bool gotProfileLevels = false;
470             if (intf) {
471                 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
472                     C2Mapper::GetProfileLevelMapper(trait.mediaType);
473                 // if we don't know the media type, pass through all values unmapped
474 
475                 // TODO: we cannot find levels that are local 'maxima' without knowing the coding
476                 // e.g. H.263 level 45 and level 30 could be two values for highest level as
477                 // they don't include one another. For now we use the last supported value.
478                 C2StreamProfileLevelInfo pl(encoder /* output */, 0u);
479                 std::vector<C2FieldSupportedValuesQuery> profileQuery = {
480                     C2FieldSupportedValuesQuery::Possible(C2ParamField(&pl, &pl.profile))
481                 };
482 
483                 c2_status_t err = intf->querySupportedValues(profileQuery, C2_DONT_BLOCK);
484                 ALOGV("query supported profiles -> %s | %s",
485                         asString(err), asString(profileQuery[0].status));
486                 if (err == C2_OK && profileQuery[0].status == C2_OK) {
487                     if (profileQuery[0].values.type == C2FieldSupportedValues::VALUES) {
488                         std::vector<std::shared_ptr<C2ParamDescriptor>> supportedParams;
489                         bool hdrSupported = false;
490                         err = intf->querySupportedParams(&supportedParams);
491                         if (err == C2_OK) {
492                             for (const std::shared_ptr<C2ParamDescriptor> &desc : supportedParams) {
493                                 if (desc->index().coreIndex() == C2StreamHdrStaticInfo::CORE_INDEX) {
494                                     hdrSupported = true;
495                                     break;
496                                 }
497                             }
498                         }
499                         ALOGV("HDR %ssupported", hdrSupported ? "" : "not ");
500                         for (C2Value::Primitive profile : profileQuery[0].values.values) {
501                             pl.profile = (C2Config::profile_t)profile.ref<uint32_t>();
502                             std::vector<std::unique_ptr<C2SettingResult>> failures;
503                             err = intf->config({&pl}, C2_DONT_BLOCK, &failures);
504                             ALOGV("set profile to %u -> %s", pl.profile, asString(err));
505                             std::vector<C2FieldSupportedValuesQuery> levelQuery = {
506                                 C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level))
507                             };
508                             err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK);
509                             ALOGV("query supported levels -> %s | %s",
510                                     asString(err), asString(levelQuery[0].status));
511                             if (err == C2_OK && levelQuery[0].status == C2_OK) {
512                                 if (levelQuery[0].values.type == C2FieldSupportedValues::VALUES
513                                         && levelQuery[0].values.values.size() > 0) {
514                                     C2Value::Primitive level = levelQuery[0].values.values.back();
515                                     pl.level = (C2Config::level_t)level.ref<uint32_t>();
516                                     ALOGV("supporting level: %u", pl.level);
517                                     bool added = false;
518                                     int32_t sdkProfile, sdkLevel;
519                                     if (mapper && mapper->mapProfile(pl.profile, &sdkProfile)
520                                             && mapper->mapLevel(pl.level, &sdkLevel)) {
521                                         caps->addProfileLevel(
522                                                 (uint32_t)sdkProfile, (uint32_t)sdkLevel);
523                                         gotProfileLevels = true;
524                                         added = true;
525                                     } else if (!mapper) {
526                                         sdkProfile = pl.profile;
527                                         sdkLevel = pl.level;
528                                         caps->addProfileLevel(pl.profile, pl.level);
529                                         gotProfileLevels = true;
530                                         added = true;
531                                     }
532                                     if (added && hdrSupported) {
533                                         static ALookup<int32_t, int32_t> sHdrProfileMap = {
534                                             { VP9Profile2, VP9Profile2HDR },
535                                             { VP9Profile3, VP9Profile3HDR },
536                                         };
537                                         int32_t sdkHdrProfile;
538                                         if (sHdrProfileMap.lookup(sdkProfile, &sdkHdrProfile)) {
539                                             caps->addProfileLevel(
540                                                     (uint32_t)sdkHdrProfile, (uint32_t)sdkLevel);
541                                         }
542                                     }
543 
544                                     // for H.263 also advertise the second highest level if the
545                                     // codec supports level 45, as level 45 only covers level 10
546                                     // TODO: move this to some form of a setting so it does not
547                                     // have to be here
548                                     if (mediaType == MIMETYPE_VIDEO_H263) {
549                                         C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED;
550                                         for (C2Value::Primitive v : levelQuery[0].values.values) {
551                                             C2Config::level_t level =
552                                                 (C2Config::level_t)v.ref<uint32_t>();
553                                             if (level < C2Config::LEVEL_H263_45
554                                                     && level > nextLevel) {
555                                                 nextLevel = level;
556                                             }
557                                         }
558                                         if (nextLevel != C2Config::LEVEL_UNUSED
559                                                 && nextLevel != pl.level
560                                                 && mapper
561                                                 && mapper->mapProfile(pl.profile, &sdkProfile)
562                                                 && mapper->mapLevel(nextLevel, &sdkLevel)) {
563                                             caps->addProfileLevel(
564                                                     (uint32_t)sdkProfile, (uint32_t)sdkLevel);
565                                         }
566                                     }
567                                 }
568                             }
569                         }
570                     }
571                 }
572             }
573 
574             if (!gotProfileLevels) {
575                 if (mediaType == MIMETYPE_VIDEO_VP9) {
576                     if (encoder) {
577                         caps->addProfileLevel(VP9Profile0,    VP9Level41);
578                     } else {
579                         caps->addProfileLevel(VP9Profile0,    VP9Level5);
580                         caps->addProfileLevel(VP9Profile2,    VP9Level5);
581                         caps->addProfileLevel(VP9Profile2HDR, VP9Level5);
582                     }
583                 } else if (mediaType == MIMETYPE_VIDEO_HEVC && !encoder) {
584                     caps->addProfileLevel(HEVCProfileMain,      HEVCMainTierLevel51);
585                     caps->addProfileLevel(HEVCProfileMainStill, HEVCMainTierLevel51);
586                 } else if (mediaType == MIMETYPE_VIDEO_VP8) {
587                     if (encoder) {
588                         caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
589                     } else {
590                         caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
591                     }
592                 } else if (mediaType == MIMETYPE_VIDEO_AVC) {
593                     if (encoder) {
594                         caps->addProfileLevel(AVCProfileBaseline,            AVCLevel41);
595 //                      caps->addProfileLevel(AVCProfileConstrainedBaseline, AVCLevel41);
596                         caps->addProfileLevel(AVCProfileMain,                AVCLevel41);
597                     } else {
598                         caps->addProfileLevel(AVCProfileBaseline,            AVCLevel52);
599                         caps->addProfileLevel(AVCProfileConstrainedBaseline, AVCLevel52);
600                         caps->addProfileLevel(AVCProfileMain,                AVCLevel52);
601                         caps->addProfileLevel(AVCProfileConstrainedHigh,     AVCLevel52);
602                         caps->addProfileLevel(AVCProfileHigh,                AVCLevel52);
603                     }
604                 } else if (mediaType == MIMETYPE_VIDEO_MPEG4) {
605                     if (encoder) {
606                         caps->addProfileLevel(MPEG4ProfileSimple,  MPEG4Level2);
607                     } else {
608                         caps->addProfileLevel(MPEG4ProfileSimple,  MPEG4Level3);
609                     }
610                 } else if (mediaType == MIMETYPE_VIDEO_H263) {
611                     if (encoder) {
612                         caps->addProfileLevel(H263ProfileBaseline, H263Level45);
613                     } else {
614                         caps->addProfileLevel(H263ProfileBaseline, H263Level30);
615                         caps->addProfileLevel(H263ProfileBaseline, H263Level45);
616                         caps->addProfileLevel(H263ProfileISWV2,    H263Level30);
617                         caps->addProfileLevel(H263ProfileISWV2,    H263Level45);
618                     }
619                 } else if (mediaType == MIMETYPE_VIDEO_MPEG2 && !encoder) {
620                     caps->addProfileLevel(MPEG2ProfileSimple, MPEG2LevelHL);
621                     caps->addProfileLevel(MPEG2ProfileMain,   MPEG2LevelHL);
622                 }
623             }
624 
625             // TODO: get this from intf() as well, but how do we map them to
626             // MediaCodec color formats?
627             if (mediaType.find("video") != std::string::npos) {
628                 // vendor video codecs prefer opaque format
629                 if (trait.name.find("android") == std::string::npos) {
630                     caps->addColorFormat(COLOR_FormatSurface);
631                 }
632                 caps->addColorFormat(COLOR_FormatYUV420Flexible);
633                 caps->addColorFormat(COLOR_FormatYUV420Planar);
634                 caps->addColorFormat(COLOR_FormatYUV420SemiPlanar);
635                 caps->addColorFormat(COLOR_FormatYUV420PackedPlanar);
636                 caps->addColorFormat(COLOR_FormatYUV420PackedSemiPlanar);
637                 // framework video encoders must support surface format, though it is unclear
638                 // that they will be able to map it if it is opaque
639                 if (encoder && trait.name.find("android") != std::string::npos) {
640                     caps->addColorFormat(COLOR_FormatSurface);
641                 }
642             }
643         }
644     }
645     return OK;
646 }
647 
648 }  // namespace android
649 
CreateBuilder()650 extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
651     return new android::Codec2InfoBuilder;
652 }
653 
654