blob: 63bd64b55df0e9eadefdd6682c516654a4fe72dd [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "Codec2InfoBuilder"
19#include <log/log.h>
20
21#include <strings.h>
22
23#include <C2Component.h>
24#include <C2Config.h>
25#include <C2Debug.h>
26#include <C2PlatformSupport.h>
27#include <Codec2Mapper.h>
28
29#include <OMX_Audio.h>
30#include <OMX_AudioExt.h>
31#include <OMX_IndexExt.h>
32#include <OMX_Types.h>
33#include <OMX_Video.h>
34#include <OMX_VideoExt.h>
35#include <OMX_AsString.h>
36
37#include <android/hardware/media/omx/1.0/IOmx.h>
38#include <android/hardware/media/omx/1.0/IOmxObserver.h>
39#include <android/hardware/media/omx/1.0/IOmxNode.h>
40#include <android/hardware/media/omx/1.0/types.h>
41
42#include <android-base/properties.h>
43#include <codec2/hidl/client.h>
44#include <cutils/native_handle.h>
45#include <media/omx/1.0/WOmxNode.h>
Pawin Vongmasa1f213362019-01-24 06:59:16 -080046#include <media/stagefright/foundation/ALookup.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080047#include <media/stagefright/foundation/MediaDefs.h>
48#include <media/stagefright/omx/OMXUtils.h>
49#include <media/stagefright/xmlparser/MediaCodecsXmlParser.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070050#include <media/stagefright/Codec2InfoBuilder.h>
51#include <media/stagefright/MediaCodecConstants.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080052
53namespace android {
54
55using Traits = C2Component::Traits;
56
Wonsik Kimf87cbc42022-01-24 09:49:12 -080057// HAL pixel format -> framework color format
58typedef std::map<uint32_t, int32_t> PixelFormatMap;
59
Pawin Vongmasa36653902018-11-15 00:10:25 -080060namespace /* unnamed */ {
61
62bool hasPrefix(const std::string& s, const char* prefix) {
63 size_t prefixLen = strlen(prefix);
64 return s.compare(0, prefixLen, prefix) == 0;
65}
66
67bool hasSuffix(const std::string& s, const char* suffix) {
68 size_t suffixLen = strlen(suffix);
69 return suffixLen > s.size() ? false :
70 s.compare(s.size() - suffixLen, suffixLen, suffix) == 0;
71}
72
Wonsik Kimf87cbc42022-01-24 09:49:12 -080073std::optional<int32_t> findFrameworkColorFormat(
74 const C2FlexiblePixelFormatDescriptorStruct &desc) {
75 switch (desc.bitDepth) {
76 case 8u:
77 if (desc.layout == C2Color::PLANAR_PACKED
78 || desc.layout == C2Color::SEMIPLANAR_PACKED) {
79 return COLOR_FormatYUV420Flexible;
80 }
81 break;
82 case 10u:
83 if (desc.layout == C2Color::SEMIPLANAR_PACKED) {
84 return COLOR_FormatYUVP010;
85 }
86 break;
87 default:
88 break;
89 }
90 return std::nullopt;
91}
92
Lajos Molnar59f4a4e2021-07-09 18:23:54 -070093// returns true if component advertised supported profile level(s)
94bool addSupportedProfileLevels(
Lajos Molnardb5751f2019-01-31 17:01:49 -080095 std::shared_ptr<Codec2Client::Interface> intf,
96 MediaCodecInfo::CapabilitiesWriter *caps,
97 const Traits& trait, const std::string &mediaType) {
98 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
99 C2Mapper::GetProfileLevelMapper(trait.mediaType);
100 // if we don't know the media type, pass through all values unmapped
Pawin Vongmasa36653902018-11-15 00:10:25 -0800101
Lajos Molnardb5751f2019-01-31 17:01:49 -0800102 // TODO: we cannot find levels that are local 'maxima' without knowing the coding
103 // e.g. H.263 level 45 and level 30 could be two values for highest level as
104 // they don't include one another. For now we use the last supported value.
105 bool encoder = trait.kind == C2Component::KIND_ENCODER;
106 C2StreamProfileLevelInfo pl(encoder /* output */, 0u);
107 std::vector<C2FieldSupportedValuesQuery> profileQuery = {
108 C2FieldSupportedValuesQuery::Possible(C2ParamField(&pl, &pl.profile))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800109 };
110
Lajos Molnardb5751f2019-01-31 17:01:49 -0800111 c2_status_t err = intf->querySupportedValues(profileQuery, C2_DONT_BLOCK);
112 ALOGV("query supported profiles -> %s | %s", asString(err), asString(profileQuery[0].status));
113 if (err != C2_OK || profileQuery[0].status != C2_OK) {
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700114 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800115 }
116
Lajos Molnardb5751f2019-01-31 17:01:49 -0800117 // we only handle enumerated values
118 if (profileQuery[0].values.type != C2FieldSupportedValues::VALUES) {
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700119 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800120 }
121
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700122 // determine if codec supports HDR; imply 10-bit support
Lajos Molnardb5751f2019-01-31 17:01:49 -0800123 bool supportsHdr = false;
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700124 // determine if codec supports HDR10Plus; imply 10-bit support
Lajos Molnardb5751f2019-01-31 17:01:49 -0800125 bool supportsHdr10Plus = false;
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700126 // determine if codec supports 10-bit format
127 bool supports10Bit = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800128
Lajos Molnardb5751f2019-01-31 17:01:49 -0800129 std::vector<std::shared_ptr<C2ParamDescriptor>> paramDescs;
130 c2_status_t err1 = intf->querySupportedParams(&paramDescs);
131 if (err1 == C2_OK) {
132 for (const std::shared_ptr<C2ParamDescriptor> &desc : paramDescs) {
Lajos Molnar739fe732021-02-07 13:06:10 -0800133 C2Param::Type type = desc->index();
134 // only consider supported parameters on raw ports
135 if (!(encoder ? type.forInput() : type.forOutput())) {
136 continue;
137 }
138 switch (type.coreIndex()) {
139 case C2StreamHdr10PlusInfo::CORE_INDEX:
Lajos Molnardb5751f2019-01-31 17:01:49 -0800140 supportsHdr10Plus = true;
141 break;
Lajos Molnar739fe732021-02-07 13:06:10 -0800142 case C2StreamHdrStaticInfo::CORE_INDEX:
Lajos Molnardb5751f2019-01-31 17:01:49 -0800143 supportsHdr = true;
144 break;
145 default:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800146 break;
147 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800148 }
149 }
150
Chong Zhang0702d1f2019-08-15 11:45:36 -0700151 // For VP9/AV1, the static info is always propagated by framework.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800152 supportsHdr |= (mediaType == MIMETYPE_VIDEO_VP9);
Chong Zhang0702d1f2019-08-15 11:45:36 -0700153 supportsHdr |= (mediaType == MIMETYPE_VIDEO_AV1);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800154
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700155 // HDR support implies 10-bit support.
156 // TODO: directly check this from the component interface
157 supports10Bit = (supportsHdr || supportsHdr10Plus);
158
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700159 bool added = false;
160
Lajos Molnardb5751f2019-01-31 17:01:49 -0800161 for (C2Value::Primitive profile : profileQuery[0].values.values) {
162 pl.profile = (C2Config::profile_t)profile.ref<uint32_t>();
163 std::vector<std::unique_ptr<C2SettingResult>> failures;
164 err = intf->config({&pl}, C2_DONT_BLOCK, &failures);
165 ALOGV("set profile to %u -> %s", pl.profile, asString(err));
166 std::vector<C2FieldSupportedValuesQuery> levelQuery = {
167 C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level))
168 };
169 err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK);
170 ALOGV("query supported levels -> %s | %s", asString(err), asString(levelQuery[0].status));
171 if (err != C2_OK || levelQuery[0].status != C2_OK
172 || levelQuery[0].values.type != C2FieldSupportedValues::VALUES
173 || levelQuery[0].values.values.size() == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800174 continue;
175 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800176
177 C2Value::Primitive level = levelQuery[0].values.values.back();
178 pl.level = (C2Config::level_t)level.ref<uint32_t>();
179 ALOGV("supporting level: %u", pl.level);
180 int32_t sdkProfile, sdkLevel;
181 if (mapper && mapper->mapProfile(pl.profile, &sdkProfile)
182 && mapper->mapLevel(pl.level, &sdkLevel)) {
183 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
184 // also list HDR profiles if component supports HDR
185 if (supportsHdr) {
186 auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper(trait.mediaType);
187 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
188 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
189 }
190 if (supportsHdr10Plus) {
191 hdrMapper = C2Mapper::GetHdrProfileLevelMapper(
192 trait.mediaType, true /*isHdr10Plus*/);
193 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
194 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
195 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800196 }
197 }
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700198 if (supports10Bit) {
199 auto bitnessMapper = C2Mapper::GetBitDepthProfileLevelMapper(trait.mediaType, 10);
200 if (bitnessMapper && bitnessMapper->mapProfile(pl.profile, &sdkProfile)) {
201 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
202 }
203 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800204 } else if (!mapper) {
205 caps->addProfileLevel(pl.profile, pl.level);
206 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700207 added = true;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800208
209 // for H.263 also advertise the second highest level if the
210 // codec supports level 45, as level 45 only covers level 10
211 // TODO: move this to some form of a setting so it does not
212 // have to be here
213 if (mediaType == MIMETYPE_VIDEO_H263) {
214 C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED;
215 for (C2Value::Primitive v : levelQuery[0].values.values) {
216 C2Config::level_t level = (C2Config::level_t)v.ref<uint32_t>();
217 if (level < C2Config::LEVEL_H263_45 && level > nextLevel) {
218 nextLevel = level;
219 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800220 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800221 if (nextLevel != C2Config::LEVEL_UNUSED
222 && nextLevel != pl.level
223 && mapper
224 && mapper->mapProfile(pl.profile, &sdkProfile)
225 && mapper->mapLevel(nextLevel, &sdkLevel)) {
226 caps->addProfileLevel(
227 (uint32_t)sdkProfile, (uint32_t)sdkLevel);
228 }
229 }
230 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700231 return added;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800232}
233
234void addSupportedColorFormats(
235 std::shared_ptr<Codec2Client::Interface> intf,
236 MediaCodecInfo::CapabilitiesWriter *caps,
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800237 const Traits& trait, const std::string &mediaType,
238 const PixelFormatMap &pixelFormatMap) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800239 // TODO: get this from intf() as well, but how do we map them to
240 // MediaCodec color formats?
241 bool encoder = trait.kind == C2Component::KIND_ENCODER;
Wonsik Kim16223262019-06-14 14:40:57 -0700242 if (mediaType.find("video") != std::string::npos
243 || mediaType.find("image") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800244
245 std::vector<C2FieldSupportedValuesQuery> query;
246 if (encoder) {
247 C2StreamPixelFormatInfo::input pixelFormat;
248 query.push_back(C2FieldSupportedValuesQuery::Possible(
249 C2ParamField::Make(pixelFormat, pixelFormat.value)));
250 } else {
251 C2StreamPixelFormatInfo::output pixelFormat;
252 query.push_back(C2FieldSupportedValuesQuery::Possible(
253 C2ParamField::Make(pixelFormat, pixelFormat.value)));
254 }
255 std::list<int32_t> supportedColorFormats;
256 if (intf->querySupportedValues(query, C2_DONT_BLOCK) == C2_OK) {
257 if (query[0].status == C2_OK) {
258 const C2FieldSupportedValues &fsv = query[0].values;
259 if (fsv.type == C2FieldSupportedValues::VALUES) {
260 for (C2Value::Primitive value : fsv.values) {
261 auto it = pixelFormatMap.find(value.u32);
262 if (it != pixelFormatMap.end()) {
263 auto it2 = std::find(
264 supportedColorFormats.begin(),
265 supportedColorFormats.end(),
266 it->second);
267 if (it2 == supportedColorFormats.end()) {
268 supportedColorFormats.push_back(it->second);
269 }
270 }
271 }
272 }
273 }
274 }
275 auto addDefaultColorFormat = [caps, &supportedColorFormats](int32_t colorFormat) {
276 caps->addColorFormat(colorFormat);
277 auto it = std::find(
278 supportedColorFormats.begin(), supportedColorFormats.end(), colorFormat);
279 if (it != supportedColorFormats.end()) {
280 supportedColorFormats.erase(it);
281 }
282 };
283
Lajos Molnardb5751f2019-01-31 17:01:49 -0800284 // vendor video codecs prefer opaque format
285 if (trait.name.find("android") == std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800286 addDefaultColorFormat(COLOR_FormatSurface);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800287 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800288 addDefaultColorFormat(COLOR_FormatYUV420Flexible);
289 addDefaultColorFormat(COLOR_FormatYUV420Planar);
290 addDefaultColorFormat(COLOR_FormatYUV420SemiPlanar);
291 addDefaultColorFormat(COLOR_FormatYUV420PackedPlanar);
292 addDefaultColorFormat(COLOR_FormatYUV420PackedSemiPlanar);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800293 // framework video encoders must support surface format, though it is unclear
294 // that they will be able to map it if it is opaque
295 if (encoder && trait.name.find("android") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800296 addDefaultColorFormat(COLOR_FormatSurface);
297 }
298 for (int32_t colorFormat : supportedColorFormats) {
299 caps->addColorFormat(colorFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800300 }
301 }
302}
303
Lajos Molnar424cfb52019-04-08 17:48:00 -0700304class Switch {
305 enum Flags : uint8_t {
306 // flags
307 IS_ENABLED = (1 << 0),
308 BY_DEFAULT = (1 << 1),
309 };
310
311 constexpr Switch(uint8_t flags) : mFlags(flags) {}
312
313 uint8_t mFlags;
314
315public:
316 // have to create class due to this bool conversion operator...
317 constexpr operator bool() const {
318 return mFlags & IS_ENABLED;
319 }
320
321 constexpr Switch operator!() const {
322 return Switch(mFlags ^ IS_ENABLED);
323 }
324
325 static constexpr Switch DISABLED() { return 0; };
326 static constexpr Switch ENABLED() { return IS_ENABLED; };
327 static constexpr Switch DISABLED_BY_DEFAULT() { return BY_DEFAULT; };
328 static constexpr Switch ENABLED_BY_DEFAULT() { return IS_ENABLED | BY_DEFAULT; };
329
330 const char *toString(const char *def = "??") const {
331 switch (mFlags) {
332 case 0: return "0";
333 case IS_ENABLED: return "1";
334 case BY_DEFAULT: return "(0)";
335 case IS_ENABLED | BY_DEFAULT: return "(1)";
336 default: return def;
337 }
338 }
339
340};
341
342const char *asString(const Switch &s, const char *def = "??") {
343 return s.toString(def);
344}
345
346Switch isSettingEnabled(
347 std::string setting, const MediaCodecsXmlParser::AttributeMap &settings,
348 Switch def = Switch::DISABLED_BY_DEFAULT()) {
349 const auto enablement = settings.find(setting);
350 if (enablement == settings.end()) {
351 return def;
352 }
353 return enablement->second == "1" ? Switch::ENABLED() : Switch::DISABLED();
354}
355
356Switch isVariantEnabled(
357 std::string variant, const MediaCodecsXmlParser::AttributeMap &settings) {
358 return isSettingEnabled("variant-" + variant, settings);
359}
360
361Switch isVariantExpressionEnabled(
362 std::string exp, const MediaCodecsXmlParser::AttributeMap &settings) {
363 if (!exp.empty() && exp.at(0) == '!') {
364 return !isVariantEnabled(exp.substr(1, exp.size() - 1), settings);
365 }
366 return isVariantEnabled(exp, settings);
367}
368
369Switch isDomainEnabled(
370 std::string domain, const MediaCodecsXmlParser::AttributeMap &settings) {
371 return isSettingEnabled("domain-" + domain, settings);
372}
373
Pawin Vongmasa36653902018-11-15 00:10:25 -0800374} // unnamed namespace
375
376status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
377 // TODO: Remove run-time configurations once all codecs are working
378 // properly. (Assume "full" behavior eventually.)
379 //
380 // debug.stagefright.ccodec supports 5 values.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800381 // 0 - No Codec 2.0 components are available.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800382 // 1 - Audio decoders and encoders with prefix "c2.android." are available
383 // and ranked first.
384 // All other components with prefix "c2.android." are available with
385 // their normal ranks.
386 // Components with prefix "c2.vda." are available with their normal
387 // ranks.
388 // All other components with suffix ".avc.decoder" or ".avc.encoder"
389 // are available but ranked last.
390 // 2 - Components with prefix "c2.android." are available and ranked
391 // first.
392 // Components with prefix "c2.vda." are available with their normal
393 // ranks.
394 // All other components with suffix ".avc.decoder" or ".avc.encoder"
395 // are available but ranked last.
396 // 3 - Components with prefix "c2.android." are available and ranked
397 // first.
398 // All other components are available with their normal ranks.
399 // 4 - All components are available with their normal ranks.
400 //
401 // The default value (boot time) is 1.
402 //
403 // Note: Currently, OMX components have default rank 0x100, while all
404 // Codec2.0 software components have default rank 0x200.
Lajos Molnar8635fc82019-05-17 17:35:10 +0000405 int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 4);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800406
407 // Obtain Codec2Client
408 std::vector<Traits> traits = Codec2Client::ListComponents();
409
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800410 // parse APEX XML first, followed by vendor XML.
411 // Note: APEX XML names do not depend on ro.media.xml_variant.* properties.
Lajos Molnarda666892019-04-08 17:25:34 -0700412 MediaCodecsXmlParser parser;
413 parser.parseXmlFilesInSearchDirs(
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800414 { "media_codecs.xml", "media_codecs_performance.xml" },
Lajos Molnar424cfb52019-04-08 17:48:00 -0700415 { "/apex/com.android.media.swcodec/etc" });
416
417 // TODO: remove these c2-specific files once product moved to default file names
418 parser.parseXmlFilesInSearchDirs(
Lajos Molnarda666892019-04-08 17:25:34 -0700419 { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
Lajos Molnar424cfb52019-04-08 17:48:00 -0700420
421 // parse default XML files
422 parser.parseXmlFilesInSearchDirs();
423
Ray Essick8c4e9c72021-03-15 15:25:21 -0700424 // The mainline modules for media may optionally include some codec shaping information.
425 // Based on vendor partition SDK, and the brand/product/device information
426 // (expect to be empty in almost always)
427 //
428 {
429 // get build info so we know what file to search
430 // ro.vendor.build.fingerprint
431 std::string fingerprint = base::GetProperty("ro.vendor.build.fingerprint",
432 "brand/product/device:");
433 ALOGV("property_get for ro.vendor.build.fingerprint == '%s'", fingerprint.c_str());
434
435 // ro.vendor.build.version.sdk
436 std::string sdk = base::GetProperty("ro.vendor.build.version.sdk", "0");
437 ALOGV("property_get for ro.vendor.build.version.sdk == '%s'", sdk.c_str());
438
439 std::string brand;
440 std::string product;
441 std::string device;
442 size_t pos1;
443 pos1 = fingerprint.find('/');
444 if (pos1 != std::string::npos) {
445 brand = fingerprint.substr(0, pos1);
446 size_t pos2 = fingerprint.find('/', pos1+1);
447 if (pos2 != std::string::npos) {
448 product = fingerprint.substr(pos1+1, pos2 - pos1 - 1);
449 size_t pos3 = fingerprint.find('/', pos2+1);
450 if (pos3 != std::string::npos) {
451 device = fingerprint.substr(pos2+1, pos3 - pos2 - 1);
452 size_t pos4 = device.find(':');
453 if (pos4 != std::string::npos) {
454 device.resize(pos4);
455 }
456 }
457 }
458 }
459
460 ALOGV("parsed: sdk '%s' brand '%s' product '%s' device '%s'",
461 sdk.c_str(), brand.c_str(), product.c_str(), device.c_str());
462
463 std::string base = "/apex/com.android.media/etc/formatshaper";
464
465 // looking in these directories within the apex
466 const std::vector<std::string> modulePathnames = {
467 base + "/" + sdk + "/" + brand + "/" + product + "/" + device,
468 base + "/" + sdk + "/" + brand + "/" + product,
469 base + "/" + sdk + "/" + brand,
470 base + "/" + sdk,
471 base
472 };
473
474 parser.parseXmlFilesInSearchDirs( { "media_codecs_shaping.xml" }, modulePathnames);
475 }
476
Pawin Vongmasa36653902018-11-15 00:10:25 -0800477 if (parser.getParsingStatus() != OK) {
478 ALOGD("XML parser no good");
479 return OK;
480 }
481
Lajos Molnar424cfb52019-04-08 17:48:00 -0700482 MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap();
483 for (const auto &v : settings) {
484 if (!hasPrefix(v.first, "media-type-")
485 && !hasPrefix(v.first, "domain-")
486 && !hasPrefix(v.first, "variant-")) {
487 writer->addGlobalSetting(v.first.c_str(), v.second.c_str());
488 }
489 }
490
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800491 std::map<std::string, PixelFormatMap> nameToPixelFormatMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800492 for (const Traits& trait : traits) {
493 C2Component::rank_t rank = trait.rank;
494
Lajos Molnardb5751f2019-01-31 17:01:49 -0800495 // Interface must be accessible for us to list the component, and there also
496 // must be an XML entry for the codec. Codec aliases listed in the traits
497 // allow additional XML entries to be specified for each alias. These will
498 // be listed as separate codecs. If no XML entry is specified for an alias,
499 // those will be treated as an additional alias specified in the XML entry
500 // for the interface name.
501 std::vector<std::string> nameAndAliases = trait.aliases;
502 nameAndAliases.insert(nameAndAliases.begin(), trait.name);
503 for (const std::string &nameOrAlias : nameAndAliases) {
504 bool isAlias = trait.name != nameOrAlias;
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800505 std::shared_ptr<Codec2Client> client;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800506 std::shared_ptr<Codec2Client::Interface> intf =
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800507 Codec2Client::CreateInterfaceByName(nameOrAlias.c_str(), &client);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800508 if (!intf) {
509 ALOGD("could not create interface for %s'%s'",
510 isAlias ? "alias " : "",
511 nameOrAlias.c_str());
512 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800513 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800514 if (parser.getCodecMap().count(nameOrAlias) == 0) {
515 if (isAlias) {
516 std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo =
517 writer->findMediaCodecInfo(trait.name.c_str());
518 if (!baseCodecInfo) {
519 ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing",
520 nameOrAlias.c_str(),
521 trait.name.c_str());
522 } else {
523 ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this",
524 nameOrAlias.c_str());
525 // merge alias into existing codec
526 baseCodecInfo->addAlias(nameOrAlias.c_str());
527 }
528 } else {
529 ALOGD("component '%s' not found in xml", trait.name.c_str());
530 }
531 continue;
532 }
533 std::string canonName = trait.name;
534
535 // TODO: Remove this block once all codecs are enabled by default.
536 switch (option) {
537 case 0:
538 continue;
539 case 1:
540 if (hasPrefix(canonName, "c2.vda.")) {
541 break;
542 }
543 if (hasPrefix(canonName, "c2.android.")) {
544 if (trait.domain == C2Component::DOMAIN_AUDIO) {
545 rank = 1;
546 break;
547 }
548 break;
549 }
550 if (hasSuffix(canonName, ".avc.decoder") ||
551 hasSuffix(canonName, ".avc.encoder")) {
552 rank = std::numeric_limits<decltype(rank)>::max();
553 break;
554 }
555 continue;
556 case 2:
557 if (hasPrefix(canonName, "c2.vda.")) {
558 break;
559 }
560 if (hasPrefix(canonName, "c2.android.")) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800561 rank = 1;
562 break;
563 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800564 if (hasSuffix(canonName, ".avc.decoder") ||
565 hasSuffix(canonName, ".avc.encoder")) {
566 rank = std::numeric_limits<decltype(rank)>::max();
567 break;
568 }
569 continue;
570 case 3:
571 if (hasPrefix(canonName, "c2.android.")) {
572 rank = 1;
573 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800574 break;
575 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800576
Lajos Molnar424cfb52019-04-08 17:48:00 -0700577 const MediaCodecsXmlParser::CodecProperties &codec =
578 parser.getCodecMap().at(nameOrAlias);
579
580 // verify that either the codec is explicitly enabled, or one of its domains is
581 bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end();
582 if (!codecEnabled) {
583 for (const std::string &domain : codec.domainSet) {
584 const Switch enabled = isDomainEnabled(domain, settings);
585 ALOGV("codec entry '%s' is in domain '%s' that is '%s'",
586 nameOrAlias.c_str(), domain.c_str(), asString(enabled));
587 if (enabled) {
588 codecEnabled = true;
589 break;
590 }
591 }
592 }
593 // if codec has variants, also check that at least one of them is enabled
594 bool variantEnabled = codec.variantSet.empty();
595 for (const std::string &variant : codec.variantSet) {
596 const Switch enabled = isVariantExpressionEnabled(variant, settings);
597 ALOGV("codec entry '%s' has a variant '%s' that is '%s'",
598 nameOrAlias.c_str(), variant.c_str(), asString(enabled));
599 if (enabled) {
600 variantEnabled = true;
601 break;
602 }
603 }
604 if (!codecEnabled || !variantEnabled) {
605 ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str());
606 continue;
607 }
608
Lajos Molnardb5751f2019-01-31 17:01:49 -0800609 ALOGV("adding codec entry for '%s'", nameOrAlias.c_str());
610 std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
611 codecInfo->setName(nameOrAlias.c_str());
612 codecInfo->setOwner(("codec2::" + trait.owner).c_str());
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800613
Lajos Molnardb5751f2019-01-31 17:01:49 -0800614 bool encoder = trait.kind == C2Component::KIND_ENCODER;
615 typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800616
Lajos Molnardb5751f2019-01-31 17:01:49 -0800617 if (encoder) {
618 attrs |= MediaCodecInfo::kFlagIsEncoder;
619 }
620 if (trait.owner == "software") {
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800621 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800622 } else {
623 attrs |= MediaCodecInfo::kFlagIsVendor;
624 if (trait.owner == "vendor-software") {
625 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
626 } else if (codec.quirkSet.find("attribute::software-codec")
627 == codec.quirkSet.end()) {
628 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800629 }
630 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800631 codecInfo->setAttributes(attrs);
632 if (!codec.rank.empty()) {
633 uint32_t xmlRank;
634 char dummy;
635 if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) {
636 rank = xmlRank;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800637 }
638 }
Lajos Molnar424cfb52019-04-08 17:48:00 -0700639 ALOGV("rank: %u", (unsigned)rank);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800640 codecInfo->setRank(rank);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800641
Lajos Molnardb5751f2019-01-31 17:01:49 -0800642 for (const std::string &alias : codec.aliases) {
643 ALOGV("adding alias '%s'", alias.c_str());
644 codecInfo->addAlias(alias.c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800645 }
646
Lajos Molnardb5751f2019-01-31 17:01:49 -0800647 for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
648 const std::string &mediaType = typeIt->first;
Lajos Molnar424cfb52019-04-08 17:48:00 -0700649 const Switch typeEnabled = isSettingEnabled(
650 "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT());
651 const Switch domainTypeEnabled = isSettingEnabled(
652 "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"),
653 settings, Switch::ENABLED_BY_DEFAULT());
654 ALOGV("type '%s-%s' is '%s/%s'",
655 mediaType.c_str(), (encoder ? "encoder" : "decoder"),
656 asString(typeEnabled), asString(domainTypeEnabled));
657 if (!typeEnabled || !domainTypeEnabled) {
658 ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(),
659 nameOrAlias.c_str());
660 continue;
661 }
662
663 ALOGI("adding type '%s'", typeIt->first.c_str());
Lajos Molnardb5751f2019-01-31 17:01:49 -0800664 const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
665 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
666 codecInfo->addMediaType(mediaType.c_str());
Lajos Molnar424cfb52019-04-08 17:48:00 -0700667 for (const auto &v : attrMap) {
668 std::string key = v.first;
669 std::string value = v.second;
670
671 size_t variantSep = key.find(":::");
672 if (variantSep != std::string::npos) {
673 std::string variant = key.substr(0, variantSep);
674 const Switch enabled = isVariantExpressionEnabled(variant, settings);
675 ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled));
676 if (!enabled) {
677 continue;
678 }
679 key = key.substr(variantSep + 3);
680 }
681
Lajos Molnardb5751f2019-01-31 17:01:49 -0800682 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
683 int32_t intValue = 0;
684 // Ignore trailing bad characters and default to 0.
685 (void)sscanf(value.c_str(), "%d", &intValue);
686 caps->addDetail(key.c_str(), intValue);
687 } else {
688 caps->addDetail(key.c_str(), value.c_str());
689 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800690 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800691
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700692 if (!addSupportedProfileLevels(intf, caps.get(), trait, mediaType)) {
693 // TODO(b/193279646) This will get fixed in C2InterfaceHelper
694 // Some components may not advertise supported values if they use a const
695 // param for profile/level (they support only one profile). For now cover
696 // only VP8 here until it is fixed.
697 if (mediaType == MIMETYPE_VIDEO_VP8) {
698 caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
699 }
700 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800701
702 auto it = nameToPixelFormatMap.find(client->getServiceName());
703 if (it == nameToPixelFormatMap.end()) {
704 it = nameToPixelFormatMap.try_emplace(client->getServiceName()).first;
705 PixelFormatMap &pixelFormatMap = it->second;
706 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_420_888] = COLOR_FormatYUV420Flexible;
707 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_P010] = COLOR_FormatYUVP010;
708 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_1010102] = COLOR_Format32bitABGR2101010;
709 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_FP16] = COLOR_Format64bitABGRFloat;
710
711 std::shared_ptr<C2StoreFlexiblePixelFormatDescriptorsInfo> pixelFormatInfo;
712 std::vector<std::unique_ptr<C2Param>> heapParams;
713 if (client->query(
714 {},
715 {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
716 C2_MAY_BLOCK,
717 &heapParams) == C2_OK
718 && heapParams.size() == 1u) {
719 pixelFormatInfo.reset(C2StoreFlexiblePixelFormatDescriptorsInfo::From(
720 heapParams[0].release()));
721 }
722 if (pixelFormatInfo && *pixelFormatInfo) {
723 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
724 C2FlexiblePixelFormatDescriptorStruct &desc =
725 pixelFormatInfo->m.values[i];
726 std::optional<int32_t> colorFormat = findFrameworkColorFormat(desc);
727 if (colorFormat) {
728 pixelFormatMap[desc.pixelFormat] = *colorFormat;
729 }
730 }
731 }
732 }
733 addSupportedColorFormats(
734 intf, caps.get(), trait, mediaType, it->second);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800735 }
736 }
737 }
738 return OK;
739}
740
741} // namespace android
742
743extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
744 return new android::Codec2InfoBuilder;
745}