blob: 3f9a40dd94551031844b88da0c15dc1ca7fb9b79 [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>
Harish Mahendrakar2c0fc8f2022-05-24 15:48:34 -070036#include <SurfaceFlingerProperties.sysprop.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080037
38#include <android/hardware/media/omx/1.0/IOmx.h>
39#include <android/hardware/media/omx/1.0/IOmxObserver.h>
40#include <android/hardware/media/omx/1.0/IOmxNode.h>
41#include <android/hardware/media/omx/1.0/types.h>
42
43#include <android-base/properties.h>
44#include <codec2/hidl/client.h>
45#include <cutils/native_handle.h>
46#include <media/omx/1.0/WOmxNode.h>
Pawin Vongmasa1f213362019-01-24 06:59:16 -080047#include <media/stagefright/foundation/ALookup.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080048#include <media/stagefright/foundation/MediaDefs.h>
49#include <media/stagefright/omx/OMXUtils.h>
50#include <media/stagefright/xmlparser/MediaCodecsXmlParser.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070051#include <media/stagefright/Codec2InfoBuilder.h>
52#include <media/stagefright/MediaCodecConstants.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080053
54namespace android {
55
56using Traits = C2Component::Traits;
57
Wonsik Kimf87cbc42022-01-24 09:49:12 -080058// HAL pixel format -> framework color format
59typedef std::map<uint32_t, int32_t> PixelFormatMap;
60
Pawin Vongmasa36653902018-11-15 00:10:25 -080061namespace /* unnamed */ {
62
63bool hasPrefix(const std::string& s, const char* prefix) {
64 size_t prefixLen = strlen(prefix);
65 return s.compare(0, prefixLen, prefix) == 0;
66}
67
68bool hasSuffix(const std::string& s, const char* suffix) {
69 size_t suffixLen = strlen(suffix);
70 return suffixLen > s.size() ? false :
71 s.compare(s.size() - suffixLen, suffixLen, suffix) == 0;
72}
73
Wonsik Kimf87cbc42022-01-24 09:49:12 -080074std::optional<int32_t> findFrameworkColorFormat(
75 const C2FlexiblePixelFormatDescriptorStruct &desc) {
76 switch (desc.bitDepth) {
77 case 8u:
78 if (desc.layout == C2Color::PLANAR_PACKED
79 || desc.layout == C2Color::SEMIPLANAR_PACKED) {
80 return COLOR_FormatYUV420Flexible;
81 }
82 break;
83 case 10u:
84 if (desc.layout == C2Color::SEMIPLANAR_PACKED) {
85 return COLOR_FormatYUVP010;
86 }
87 break;
88 default:
89 break;
90 }
91 return std::nullopt;
92}
93
Lajos Molnar59f4a4e2021-07-09 18:23:54 -070094// returns true if component advertised supported profile level(s)
95bool addSupportedProfileLevels(
Lajos Molnardb5751f2019-01-31 17:01:49 -080096 std::shared_ptr<Codec2Client::Interface> intf,
97 MediaCodecInfo::CapabilitiesWriter *caps,
98 const Traits& trait, const std::string &mediaType) {
99 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
100 C2Mapper::GetProfileLevelMapper(trait.mediaType);
101 // if we don't know the media type, pass through all values unmapped
Pawin Vongmasa36653902018-11-15 00:10:25 -0800102
Lajos Molnardb5751f2019-01-31 17:01:49 -0800103 // TODO: we cannot find levels that are local 'maxima' without knowing the coding
104 // e.g. H.263 level 45 and level 30 could be two values for highest level as
105 // they don't include one another. For now we use the last supported value.
106 bool encoder = trait.kind == C2Component::KIND_ENCODER;
107 C2StreamProfileLevelInfo pl(encoder /* output */, 0u);
108 std::vector<C2FieldSupportedValuesQuery> profileQuery = {
109 C2FieldSupportedValuesQuery::Possible(C2ParamField(&pl, &pl.profile))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800110 };
111
Lajos Molnardb5751f2019-01-31 17:01:49 -0800112 c2_status_t err = intf->querySupportedValues(profileQuery, C2_DONT_BLOCK);
113 ALOGV("query supported profiles -> %s | %s", asString(err), asString(profileQuery[0].status));
114 if (err != C2_OK || profileQuery[0].status != C2_OK) {
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700115 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800116 }
117
Lajos Molnardb5751f2019-01-31 17:01:49 -0800118 // we only handle enumerated values
119 if (profileQuery[0].values.type != C2FieldSupportedValues::VALUES) {
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700120 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800121 }
122
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700123 // determine if codec supports HDR; imply 10-bit support
Lajos Molnardb5751f2019-01-31 17:01:49 -0800124 bool supportsHdr = false;
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700125 // determine if codec supports HDR10Plus; imply 10-bit support
Lajos Molnardb5751f2019-01-31 17:01:49 -0800126 bool supportsHdr10Plus = false;
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700127 // determine if codec supports 10-bit format
128 bool supports10Bit = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800129
Lajos Molnardb5751f2019-01-31 17:01:49 -0800130 std::vector<std::shared_ptr<C2ParamDescriptor>> paramDescs;
131 c2_status_t err1 = intf->querySupportedParams(&paramDescs);
132 if (err1 == C2_OK) {
133 for (const std::shared_ptr<C2ParamDescriptor> &desc : paramDescs) {
Lajos Molnar739fe732021-02-07 13:06:10 -0800134 C2Param::Type type = desc->index();
135 // only consider supported parameters on raw ports
136 if (!(encoder ? type.forInput() : type.forOutput())) {
137 continue;
138 }
139 switch (type.coreIndex()) {
Taehwan Kim2d222b82022-05-12 14:19:26 +0900140 case C2StreamHdrDynamicMetadataInfo::CORE_INDEX:
141 [[fallthrough]];
142 case C2StreamHdr10PlusInfo::CORE_INDEX: // will be deprecated
Lajos Molnardb5751f2019-01-31 17:01:49 -0800143 supportsHdr10Plus = true;
144 break;
Lajos Molnar739fe732021-02-07 13:06:10 -0800145 case C2StreamHdrStaticInfo::CORE_INDEX:
Lajos Molnardb5751f2019-01-31 17:01:49 -0800146 supportsHdr = true;
147 break;
148 default:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800149 break;
150 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800151 }
152 }
153
Lajos Molnarb857cde2022-05-25 10:20:37 -0700154 // VP9 does not support HDR metadata in the bitstream and static metadata
155 // can always be carried by the framework. (The framework does not propagate
156 // dynamic metadata as that needs to be frame accurate.)
Lajos Molnardb5751f2019-01-31 17:01:49 -0800157 supportsHdr |= (mediaType == MIMETYPE_VIDEO_VP9);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800158
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700159 // HDR support implies 10-bit support.
160 // TODO: directly check this from the component interface
161 supports10Bit = (supportsHdr || supportsHdr10Plus);
162
Harish Mahendrakar2c0fc8f2022-05-24 15:48:34 -0700163 // If the device doesn't support HDR display, then no codec on the device
164 // can advertise support for HDR profiles.
165 // Default to true to maintain backward compatibility
166 auto ret = sysprop::SurfaceFlingerProperties::has_HDR_display();
167 bool hasHDRDisplay = ret.has_value() ? *ret : true;
168
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700169 bool added = false;
170
Lajos Molnardb5751f2019-01-31 17:01:49 -0800171 for (C2Value::Primitive profile : profileQuery[0].values.values) {
172 pl.profile = (C2Config::profile_t)profile.ref<uint32_t>();
173 std::vector<std::unique_ptr<C2SettingResult>> failures;
174 err = intf->config({&pl}, C2_DONT_BLOCK, &failures);
175 ALOGV("set profile to %u -> %s", pl.profile, asString(err));
176 std::vector<C2FieldSupportedValuesQuery> levelQuery = {
177 C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level))
178 };
179 err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK);
180 ALOGV("query supported levels -> %s | %s", asString(err), asString(levelQuery[0].status));
181 if (err != C2_OK || levelQuery[0].status != C2_OK
182 || levelQuery[0].values.type != C2FieldSupportedValues::VALUES
183 || levelQuery[0].values.values.size() == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800184 continue;
185 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800186
187 C2Value::Primitive level = levelQuery[0].values.values.back();
188 pl.level = (C2Config::level_t)level.ref<uint32_t>();
189 ALOGV("supporting level: %u", pl.level);
190 int32_t sdkProfile, sdkLevel;
191 if (mapper && mapper->mapProfile(pl.profile, &sdkProfile)
192 && mapper->mapLevel(pl.level, &sdkLevel)) {
193 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
Harish Mahendrakar2c0fc8f2022-05-24 15:48:34 -0700194 // also list HDR profiles if component supports HDR and device has HDR display
195 if (supportsHdr && hasHDRDisplay) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800196 auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper(trait.mediaType);
197 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
198 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
199 }
200 if (supportsHdr10Plus) {
201 hdrMapper = C2Mapper::GetHdrProfileLevelMapper(
202 trait.mediaType, true /*isHdr10Plus*/);
203 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
204 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
205 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800206 }
207 }
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700208 if (supports10Bit) {
209 auto bitnessMapper = C2Mapper::GetBitDepthProfileLevelMapper(trait.mediaType, 10);
210 if (bitnessMapper && bitnessMapper->mapProfile(pl.profile, &sdkProfile)) {
211 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
212 }
213 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800214 } else if (!mapper) {
215 caps->addProfileLevel(pl.profile, pl.level);
216 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700217 added = true;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800218
219 // for H.263 also advertise the second highest level if the
220 // codec supports level 45, as level 45 only covers level 10
221 // TODO: move this to some form of a setting so it does not
222 // have to be here
223 if (mediaType == MIMETYPE_VIDEO_H263) {
224 C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED;
225 for (C2Value::Primitive v : levelQuery[0].values.values) {
226 C2Config::level_t level = (C2Config::level_t)v.ref<uint32_t>();
227 if (level < C2Config::LEVEL_H263_45 && level > nextLevel) {
228 nextLevel = level;
229 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800230 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800231 if (nextLevel != C2Config::LEVEL_UNUSED
232 && nextLevel != pl.level
233 && mapper
234 && mapper->mapProfile(pl.profile, &sdkProfile)
235 && mapper->mapLevel(nextLevel, &sdkLevel)) {
236 caps->addProfileLevel(
237 (uint32_t)sdkProfile, (uint32_t)sdkLevel);
238 }
239 }
240 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700241 return added;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800242}
243
244void addSupportedColorFormats(
245 std::shared_ptr<Codec2Client::Interface> intf,
246 MediaCodecInfo::CapabilitiesWriter *caps,
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800247 const Traits& trait, const std::string &mediaType,
248 const PixelFormatMap &pixelFormatMap) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800249 // TODO: get this from intf() as well, but how do we map them to
250 // MediaCodec color formats?
251 bool encoder = trait.kind == C2Component::KIND_ENCODER;
Wonsik Kim16223262019-06-14 14:40:57 -0700252 if (mediaType.find("video") != std::string::npos
253 || mediaType.find("image") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800254
255 std::vector<C2FieldSupportedValuesQuery> query;
256 if (encoder) {
257 C2StreamPixelFormatInfo::input pixelFormat;
258 query.push_back(C2FieldSupportedValuesQuery::Possible(
259 C2ParamField::Make(pixelFormat, pixelFormat.value)));
260 } else {
261 C2StreamPixelFormatInfo::output pixelFormat;
262 query.push_back(C2FieldSupportedValuesQuery::Possible(
263 C2ParamField::Make(pixelFormat, pixelFormat.value)));
264 }
265 std::list<int32_t> supportedColorFormats;
266 if (intf->querySupportedValues(query, C2_DONT_BLOCK) == C2_OK) {
267 if (query[0].status == C2_OK) {
268 const C2FieldSupportedValues &fsv = query[0].values;
269 if (fsv.type == C2FieldSupportedValues::VALUES) {
270 for (C2Value::Primitive value : fsv.values) {
271 auto it = pixelFormatMap.find(value.u32);
272 if (it != pixelFormatMap.end()) {
273 auto it2 = std::find(
274 supportedColorFormats.begin(),
275 supportedColorFormats.end(),
276 it->second);
277 if (it2 == supportedColorFormats.end()) {
278 supportedColorFormats.push_back(it->second);
279 }
280 }
281 }
282 }
283 }
284 }
285 auto addDefaultColorFormat = [caps, &supportedColorFormats](int32_t colorFormat) {
286 caps->addColorFormat(colorFormat);
287 auto it = std::find(
288 supportedColorFormats.begin(), supportedColorFormats.end(), colorFormat);
289 if (it != supportedColorFormats.end()) {
290 supportedColorFormats.erase(it);
291 }
292 };
293
My Name298764f2022-03-25 15:07:51 -0700294 // The color format is ordered by preference. The intention here is to advertise:
295 // c2.android.* codecs: YUV420s, Surface, <the rest>
296 // all other codecs: Surface, YUV420s, <the rest>
297 // TODO: get this preference via Codec2 API
298
Lajos Molnardb5751f2019-01-31 17:01:49 -0800299 // vendor video codecs prefer opaque format
300 if (trait.name.find("android") == std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800301 addDefaultColorFormat(COLOR_FormatSurface);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800302 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800303 addDefaultColorFormat(COLOR_FormatYUV420Flexible);
304 addDefaultColorFormat(COLOR_FormatYUV420Planar);
305 addDefaultColorFormat(COLOR_FormatYUV420SemiPlanar);
306 addDefaultColorFormat(COLOR_FormatYUV420PackedPlanar);
307 addDefaultColorFormat(COLOR_FormatYUV420PackedSemiPlanar);
My Name298764f2022-03-25 15:07:51 -0700308 // Android video codecs prefer CPU-readable formats
309 if (trait.name.find("android") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800310 addDefaultColorFormat(COLOR_FormatSurface);
311 }
312 for (int32_t colorFormat : supportedColorFormats) {
313 caps->addColorFormat(colorFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800314 }
315 }
316}
317
Lajos Molnar424cfb52019-04-08 17:48:00 -0700318class Switch {
319 enum Flags : uint8_t {
320 // flags
321 IS_ENABLED = (1 << 0),
322 BY_DEFAULT = (1 << 1),
323 };
324
325 constexpr Switch(uint8_t flags) : mFlags(flags) {}
326
327 uint8_t mFlags;
328
329public:
330 // have to create class due to this bool conversion operator...
331 constexpr operator bool() const {
332 return mFlags & IS_ENABLED;
333 }
334
335 constexpr Switch operator!() const {
336 return Switch(mFlags ^ IS_ENABLED);
337 }
338
339 static constexpr Switch DISABLED() { return 0; };
340 static constexpr Switch ENABLED() { return IS_ENABLED; };
341 static constexpr Switch DISABLED_BY_DEFAULT() { return BY_DEFAULT; };
342 static constexpr Switch ENABLED_BY_DEFAULT() { return IS_ENABLED | BY_DEFAULT; };
343
344 const char *toString(const char *def = "??") const {
345 switch (mFlags) {
346 case 0: return "0";
347 case IS_ENABLED: return "1";
348 case BY_DEFAULT: return "(0)";
349 case IS_ENABLED | BY_DEFAULT: return "(1)";
350 default: return def;
351 }
352 }
353
354};
355
356const char *asString(const Switch &s, const char *def = "??") {
357 return s.toString(def);
358}
359
360Switch isSettingEnabled(
361 std::string setting, const MediaCodecsXmlParser::AttributeMap &settings,
362 Switch def = Switch::DISABLED_BY_DEFAULT()) {
363 const auto enablement = settings.find(setting);
364 if (enablement == settings.end()) {
365 return def;
366 }
367 return enablement->second == "1" ? Switch::ENABLED() : Switch::DISABLED();
368}
369
370Switch isVariantEnabled(
371 std::string variant, const MediaCodecsXmlParser::AttributeMap &settings) {
372 return isSettingEnabled("variant-" + variant, settings);
373}
374
375Switch isVariantExpressionEnabled(
376 std::string exp, const MediaCodecsXmlParser::AttributeMap &settings) {
377 if (!exp.empty() && exp.at(0) == '!') {
378 return !isVariantEnabled(exp.substr(1, exp.size() - 1), settings);
379 }
380 return isVariantEnabled(exp, settings);
381}
382
383Switch isDomainEnabled(
384 std::string domain, const MediaCodecsXmlParser::AttributeMap &settings) {
385 return isSettingEnabled("domain-" + domain, settings);
386}
387
Pawin Vongmasa36653902018-11-15 00:10:25 -0800388} // unnamed namespace
389
390status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
391 // TODO: Remove run-time configurations once all codecs are working
392 // properly. (Assume "full" behavior eventually.)
393 //
394 // debug.stagefright.ccodec supports 5 values.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800395 // 0 - No Codec 2.0 components are available.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800396 // 1 - Audio decoders and encoders with prefix "c2.android." are available
397 // and ranked first.
398 // All other components with prefix "c2.android." are available with
399 // their normal ranks.
400 // Components with prefix "c2.vda." are available with their normal
401 // ranks.
402 // All other components with suffix ".avc.decoder" or ".avc.encoder"
403 // are available but ranked last.
404 // 2 - Components with prefix "c2.android." are available and ranked
405 // first.
406 // Components with prefix "c2.vda." are available with their normal
407 // ranks.
408 // All other components with suffix ".avc.decoder" or ".avc.encoder"
409 // are available but ranked last.
410 // 3 - Components with prefix "c2.android." are available and ranked
411 // first.
412 // All other components are available with their normal ranks.
413 // 4 - All components are available with their normal ranks.
414 //
415 // The default value (boot time) is 1.
416 //
417 // Note: Currently, OMX components have default rank 0x100, while all
418 // Codec2.0 software components have default rank 0x200.
Lajos Molnar8635fc82019-05-17 17:35:10 +0000419 int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 4);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800420
421 // Obtain Codec2Client
422 std::vector<Traits> traits = Codec2Client::ListComponents();
423
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800424 // parse APEX XML first, followed by vendor XML.
425 // Note: APEX XML names do not depend on ro.media.xml_variant.* properties.
Lajos Molnarda666892019-04-08 17:25:34 -0700426 MediaCodecsXmlParser parser;
427 parser.parseXmlFilesInSearchDirs(
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800428 { "media_codecs.xml", "media_codecs_performance.xml" },
Lajos Molnar424cfb52019-04-08 17:48:00 -0700429 { "/apex/com.android.media.swcodec/etc" });
430
431 // TODO: remove these c2-specific files once product moved to default file names
432 parser.parseXmlFilesInSearchDirs(
Lajos Molnarda666892019-04-08 17:25:34 -0700433 { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
Lajos Molnar424cfb52019-04-08 17:48:00 -0700434
435 // parse default XML files
436 parser.parseXmlFilesInSearchDirs();
437
Ray Essick8c4e9c72021-03-15 15:25:21 -0700438 // The mainline modules for media may optionally include some codec shaping information.
439 // Based on vendor partition SDK, and the brand/product/device information
440 // (expect to be empty in almost always)
441 //
442 {
443 // get build info so we know what file to search
444 // ro.vendor.build.fingerprint
445 std::string fingerprint = base::GetProperty("ro.vendor.build.fingerprint",
446 "brand/product/device:");
447 ALOGV("property_get for ro.vendor.build.fingerprint == '%s'", fingerprint.c_str());
448
449 // ro.vendor.build.version.sdk
450 std::string sdk = base::GetProperty("ro.vendor.build.version.sdk", "0");
451 ALOGV("property_get for ro.vendor.build.version.sdk == '%s'", sdk.c_str());
452
453 std::string brand;
454 std::string product;
455 std::string device;
456 size_t pos1;
457 pos1 = fingerprint.find('/');
458 if (pos1 != std::string::npos) {
459 brand = fingerprint.substr(0, pos1);
460 size_t pos2 = fingerprint.find('/', pos1+1);
461 if (pos2 != std::string::npos) {
462 product = fingerprint.substr(pos1+1, pos2 - pos1 - 1);
463 size_t pos3 = fingerprint.find('/', pos2+1);
464 if (pos3 != std::string::npos) {
465 device = fingerprint.substr(pos2+1, pos3 - pos2 - 1);
466 size_t pos4 = device.find(':');
467 if (pos4 != std::string::npos) {
468 device.resize(pos4);
469 }
470 }
471 }
472 }
473
474 ALOGV("parsed: sdk '%s' brand '%s' product '%s' device '%s'",
475 sdk.c_str(), brand.c_str(), product.c_str(), device.c_str());
476
477 std::string base = "/apex/com.android.media/etc/formatshaper";
478
479 // looking in these directories within the apex
480 const std::vector<std::string> modulePathnames = {
481 base + "/" + sdk + "/" + brand + "/" + product + "/" + device,
482 base + "/" + sdk + "/" + brand + "/" + product,
483 base + "/" + sdk + "/" + brand,
484 base + "/" + sdk,
485 base
486 };
487
488 parser.parseXmlFilesInSearchDirs( { "media_codecs_shaping.xml" }, modulePathnames);
489 }
490
Pawin Vongmasa36653902018-11-15 00:10:25 -0800491 if (parser.getParsingStatus() != OK) {
492 ALOGD("XML parser no good");
493 return OK;
494 }
495
Lajos Molnar424cfb52019-04-08 17:48:00 -0700496 MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap();
497 for (const auto &v : settings) {
498 if (!hasPrefix(v.first, "media-type-")
499 && !hasPrefix(v.first, "domain-")
500 && !hasPrefix(v.first, "variant-")) {
501 writer->addGlobalSetting(v.first.c_str(), v.second.c_str());
502 }
503 }
504
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800505 std::map<std::string, PixelFormatMap> nameToPixelFormatMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800506 for (const Traits& trait : traits) {
507 C2Component::rank_t rank = trait.rank;
508
Lajos Molnardb5751f2019-01-31 17:01:49 -0800509 // Interface must be accessible for us to list the component, and there also
510 // must be an XML entry for the codec. Codec aliases listed in the traits
511 // allow additional XML entries to be specified for each alias. These will
512 // be listed as separate codecs. If no XML entry is specified for an alias,
513 // those will be treated as an additional alias specified in the XML entry
514 // for the interface name.
515 std::vector<std::string> nameAndAliases = trait.aliases;
516 nameAndAliases.insert(nameAndAliases.begin(), trait.name);
517 for (const std::string &nameOrAlias : nameAndAliases) {
518 bool isAlias = trait.name != nameOrAlias;
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800519 std::shared_ptr<Codec2Client> client;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800520 std::shared_ptr<Codec2Client::Interface> intf =
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800521 Codec2Client::CreateInterfaceByName(nameOrAlias.c_str(), &client);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800522 if (!intf) {
523 ALOGD("could not create interface for %s'%s'",
524 isAlias ? "alias " : "",
525 nameOrAlias.c_str());
526 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800527 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800528 if (parser.getCodecMap().count(nameOrAlias) == 0) {
529 if (isAlias) {
530 std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo =
531 writer->findMediaCodecInfo(trait.name.c_str());
532 if (!baseCodecInfo) {
533 ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing",
534 nameOrAlias.c_str(),
535 trait.name.c_str());
536 } else {
537 ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this",
538 nameOrAlias.c_str());
539 // merge alias into existing codec
540 baseCodecInfo->addAlias(nameOrAlias.c_str());
541 }
542 } else {
543 ALOGD("component '%s' not found in xml", trait.name.c_str());
544 }
545 continue;
546 }
547 std::string canonName = trait.name;
548
549 // TODO: Remove this block once all codecs are enabled by default.
550 switch (option) {
551 case 0:
552 continue;
553 case 1:
554 if (hasPrefix(canonName, "c2.vda.")) {
555 break;
556 }
557 if (hasPrefix(canonName, "c2.android.")) {
558 if (trait.domain == C2Component::DOMAIN_AUDIO) {
559 rank = 1;
560 break;
561 }
562 break;
563 }
564 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 2:
571 if (hasPrefix(canonName, "c2.vda.")) {
572 break;
573 }
574 if (hasPrefix(canonName, "c2.android.")) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800575 rank = 1;
576 break;
577 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800578 if (hasSuffix(canonName, ".avc.decoder") ||
579 hasSuffix(canonName, ".avc.encoder")) {
580 rank = std::numeric_limits<decltype(rank)>::max();
581 break;
582 }
583 continue;
584 case 3:
585 if (hasPrefix(canonName, "c2.android.")) {
586 rank = 1;
587 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800588 break;
589 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800590
Lajos Molnar424cfb52019-04-08 17:48:00 -0700591 const MediaCodecsXmlParser::CodecProperties &codec =
592 parser.getCodecMap().at(nameOrAlias);
593
594 // verify that either the codec is explicitly enabled, or one of its domains is
595 bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end();
596 if (!codecEnabled) {
597 for (const std::string &domain : codec.domainSet) {
598 const Switch enabled = isDomainEnabled(domain, settings);
599 ALOGV("codec entry '%s' is in domain '%s' that is '%s'",
600 nameOrAlias.c_str(), domain.c_str(), asString(enabled));
601 if (enabled) {
602 codecEnabled = true;
603 break;
604 }
605 }
606 }
607 // if codec has variants, also check that at least one of them is enabled
608 bool variantEnabled = codec.variantSet.empty();
609 for (const std::string &variant : codec.variantSet) {
610 const Switch enabled = isVariantExpressionEnabled(variant, settings);
611 ALOGV("codec entry '%s' has a variant '%s' that is '%s'",
612 nameOrAlias.c_str(), variant.c_str(), asString(enabled));
613 if (enabled) {
614 variantEnabled = true;
615 break;
616 }
617 }
618 if (!codecEnabled || !variantEnabled) {
619 ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str());
620 continue;
621 }
622
Lajos Molnardb5751f2019-01-31 17:01:49 -0800623 ALOGV("adding codec entry for '%s'", nameOrAlias.c_str());
624 std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
625 codecInfo->setName(nameOrAlias.c_str());
626 codecInfo->setOwner(("codec2::" + trait.owner).c_str());
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800627
Lajos Molnardb5751f2019-01-31 17:01:49 -0800628 bool encoder = trait.kind == C2Component::KIND_ENCODER;
629 typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800630
Lajos Molnardb5751f2019-01-31 17:01:49 -0800631 if (encoder) {
632 attrs |= MediaCodecInfo::kFlagIsEncoder;
633 }
634 if (trait.owner == "software") {
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800635 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800636 } else {
637 attrs |= MediaCodecInfo::kFlagIsVendor;
638 if (trait.owner == "vendor-software") {
639 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
640 } else if (codec.quirkSet.find("attribute::software-codec")
641 == codec.quirkSet.end()) {
642 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800643 }
644 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800645 codecInfo->setAttributes(attrs);
646 if (!codec.rank.empty()) {
647 uint32_t xmlRank;
648 char dummy;
649 if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) {
650 rank = xmlRank;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800651 }
652 }
Lajos Molnar424cfb52019-04-08 17:48:00 -0700653 ALOGV("rank: %u", (unsigned)rank);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800654 codecInfo->setRank(rank);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800655
Lajos Molnardb5751f2019-01-31 17:01:49 -0800656 for (const std::string &alias : codec.aliases) {
657 ALOGV("adding alias '%s'", alias.c_str());
658 codecInfo->addAlias(alias.c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800659 }
660
Lajos Molnardb5751f2019-01-31 17:01:49 -0800661 for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
662 const std::string &mediaType = typeIt->first;
Lajos Molnar424cfb52019-04-08 17:48:00 -0700663 const Switch typeEnabled = isSettingEnabled(
664 "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT());
665 const Switch domainTypeEnabled = isSettingEnabled(
666 "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"),
667 settings, Switch::ENABLED_BY_DEFAULT());
668 ALOGV("type '%s-%s' is '%s/%s'",
669 mediaType.c_str(), (encoder ? "encoder" : "decoder"),
670 asString(typeEnabled), asString(domainTypeEnabled));
671 if (!typeEnabled || !domainTypeEnabled) {
672 ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(),
673 nameOrAlias.c_str());
674 continue;
675 }
676
677 ALOGI("adding type '%s'", typeIt->first.c_str());
Lajos Molnardb5751f2019-01-31 17:01:49 -0800678 const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
679 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
680 codecInfo->addMediaType(mediaType.c_str());
Lajos Molnar424cfb52019-04-08 17:48:00 -0700681 for (const auto &v : attrMap) {
682 std::string key = v.first;
683 std::string value = v.second;
684
685 size_t variantSep = key.find(":::");
686 if (variantSep != std::string::npos) {
687 std::string variant = key.substr(0, variantSep);
688 const Switch enabled = isVariantExpressionEnabled(variant, settings);
689 ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled));
690 if (!enabled) {
691 continue;
692 }
693 key = key.substr(variantSep + 3);
694 }
695
Lajos Molnardb5751f2019-01-31 17:01:49 -0800696 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
697 int32_t intValue = 0;
698 // Ignore trailing bad characters and default to 0.
699 (void)sscanf(value.c_str(), "%d", &intValue);
700 caps->addDetail(key.c_str(), intValue);
701 } else {
702 caps->addDetail(key.c_str(), value.c_str());
703 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800704 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800705
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700706 if (!addSupportedProfileLevels(intf, caps.get(), trait, mediaType)) {
707 // TODO(b/193279646) This will get fixed in C2InterfaceHelper
708 // Some components may not advertise supported values if they use a const
709 // param for profile/level (they support only one profile). For now cover
710 // only VP8 here until it is fixed.
711 if (mediaType == MIMETYPE_VIDEO_VP8) {
712 caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
713 }
714 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800715
716 auto it = nameToPixelFormatMap.find(client->getServiceName());
717 if (it == nameToPixelFormatMap.end()) {
718 it = nameToPixelFormatMap.try_emplace(client->getServiceName()).first;
719 PixelFormatMap &pixelFormatMap = it->second;
720 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_420_888] = COLOR_FormatYUV420Flexible;
721 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_P010] = COLOR_FormatYUVP010;
722 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_1010102] = COLOR_Format32bitABGR2101010;
723 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_FP16] = COLOR_Format64bitABGRFloat;
724
725 std::shared_ptr<C2StoreFlexiblePixelFormatDescriptorsInfo> pixelFormatInfo;
726 std::vector<std::unique_ptr<C2Param>> heapParams;
727 if (client->query(
728 {},
729 {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
730 C2_MAY_BLOCK,
731 &heapParams) == C2_OK
732 && heapParams.size() == 1u) {
733 pixelFormatInfo.reset(C2StoreFlexiblePixelFormatDescriptorsInfo::From(
734 heapParams[0].release()));
735 }
736 if (pixelFormatInfo && *pixelFormatInfo) {
737 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
738 C2FlexiblePixelFormatDescriptorStruct &desc =
739 pixelFormatInfo->m.values[i];
740 std::optional<int32_t> colorFormat = findFrameworkColorFormat(desc);
741 if (colorFormat) {
742 pixelFormatMap[desc.pixelFormat] = *colorFormat;
743 }
744 }
745 }
746 }
747 addSupportedColorFormats(
748 intf, caps.get(), trait, mediaType, it->second);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800749 }
750 }
751 }
752 return OK;
753}
754
755} // namespace android
756
757extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
758 return new android::Codec2InfoBuilder;
759}