blob: 1c362aece203d7fd66091850e9694a4972bb7ade [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 }
Wonsik Kim1e16eb32022-06-03 16:32:19 -0700312
313 static const int kVendorSdkVersion = ::android::base::GetIntProperty(
314 "ro.vendor.build.version.sdk", android_get_device_api_level());
315 if (kVendorSdkVersion >= __ANDROID_API_T__) {
316 for (int32_t colorFormat : supportedColorFormats) {
317 caps->addColorFormat(colorFormat);
318 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800319 }
320 }
321}
322
Lajos Molnar424cfb52019-04-08 17:48:00 -0700323class Switch {
324 enum Flags : uint8_t {
325 // flags
326 IS_ENABLED = (1 << 0),
327 BY_DEFAULT = (1 << 1),
328 };
329
330 constexpr Switch(uint8_t flags) : mFlags(flags) {}
331
332 uint8_t mFlags;
333
334public:
335 // have to create class due to this bool conversion operator...
336 constexpr operator bool() const {
337 return mFlags & IS_ENABLED;
338 }
339
340 constexpr Switch operator!() const {
341 return Switch(mFlags ^ IS_ENABLED);
342 }
343
344 static constexpr Switch DISABLED() { return 0; };
345 static constexpr Switch ENABLED() { return IS_ENABLED; };
346 static constexpr Switch DISABLED_BY_DEFAULT() { return BY_DEFAULT; };
347 static constexpr Switch ENABLED_BY_DEFAULT() { return IS_ENABLED | BY_DEFAULT; };
348
349 const char *toString(const char *def = "??") const {
350 switch (mFlags) {
351 case 0: return "0";
352 case IS_ENABLED: return "1";
353 case BY_DEFAULT: return "(0)";
354 case IS_ENABLED | BY_DEFAULT: return "(1)";
355 default: return def;
356 }
357 }
358
359};
360
361const char *asString(const Switch &s, const char *def = "??") {
362 return s.toString(def);
363}
364
365Switch isSettingEnabled(
366 std::string setting, const MediaCodecsXmlParser::AttributeMap &settings,
367 Switch def = Switch::DISABLED_BY_DEFAULT()) {
368 const auto enablement = settings.find(setting);
369 if (enablement == settings.end()) {
370 return def;
371 }
372 return enablement->second == "1" ? Switch::ENABLED() : Switch::DISABLED();
373}
374
375Switch isVariantEnabled(
376 std::string variant, const MediaCodecsXmlParser::AttributeMap &settings) {
377 return isSettingEnabled("variant-" + variant, settings);
378}
379
380Switch isVariantExpressionEnabled(
381 std::string exp, const MediaCodecsXmlParser::AttributeMap &settings) {
382 if (!exp.empty() && exp.at(0) == '!') {
383 return !isVariantEnabled(exp.substr(1, exp.size() - 1), settings);
384 }
385 return isVariantEnabled(exp, settings);
386}
387
388Switch isDomainEnabled(
389 std::string domain, const MediaCodecsXmlParser::AttributeMap &settings) {
390 return isSettingEnabled("domain-" + domain, settings);
391}
392
Pawin Vongmasa36653902018-11-15 00:10:25 -0800393} // unnamed namespace
394
395status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
396 // TODO: Remove run-time configurations once all codecs are working
397 // properly. (Assume "full" behavior eventually.)
398 //
399 // debug.stagefright.ccodec supports 5 values.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800400 // 0 - No Codec 2.0 components are available.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800401 // 1 - Audio decoders and encoders with prefix "c2.android." are available
402 // and ranked first.
403 // All other components with prefix "c2.android." are available with
404 // their normal ranks.
405 // Components with prefix "c2.vda." are available with their normal
406 // ranks.
407 // All other components with suffix ".avc.decoder" or ".avc.encoder"
408 // are available but ranked last.
409 // 2 - Components with prefix "c2.android." are available and ranked
410 // first.
411 // Components with prefix "c2.vda." are available with their normal
412 // ranks.
413 // All other components with suffix ".avc.decoder" or ".avc.encoder"
414 // are available but ranked last.
415 // 3 - Components with prefix "c2.android." are available and ranked
416 // first.
417 // All other components are available with their normal ranks.
418 // 4 - All components are available with their normal ranks.
419 //
420 // The default value (boot time) is 1.
421 //
422 // Note: Currently, OMX components have default rank 0x100, while all
423 // Codec2.0 software components have default rank 0x200.
Lajos Molnar8635fc82019-05-17 17:35:10 +0000424 int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 4);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800425
426 // Obtain Codec2Client
427 std::vector<Traits> traits = Codec2Client::ListComponents();
428
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800429 // parse APEX XML first, followed by vendor XML.
430 // Note: APEX XML names do not depend on ro.media.xml_variant.* properties.
Lajos Molnarda666892019-04-08 17:25:34 -0700431 MediaCodecsXmlParser parser;
432 parser.parseXmlFilesInSearchDirs(
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800433 { "media_codecs.xml", "media_codecs_performance.xml" },
Lajos Molnar424cfb52019-04-08 17:48:00 -0700434 { "/apex/com.android.media.swcodec/etc" });
435
436 // TODO: remove these c2-specific files once product moved to default file names
437 parser.parseXmlFilesInSearchDirs(
Lajos Molnarda666892019-04-08 17:25:34 -0700438 { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
Lajos Molnar424cfb52019-04-08 17:48:00 -0700439
440 // parse default XML files
441 parser.parseXmlFilesInSearchDirs();
442
Ray Essick8c4e9c72021-03-15 15:25:21 -0700443 // The mainline modules for media may optionally include some codec shaping information.
444 // Based on vendor partition SDK, and the brand/product/device information
445 // (expect to be empty in almost always)
446 //
447 {
448 // get build info so we know what file to search
449 // ro.vendor.build.fingerprint
450 std::string fingerprint = base::GetProperty("ro.vendor.build.fingerprint",
451 "brand/product/device:");
452 ALOGV("property_get for ro.vendor.build.fingerprint == '%s'", fingerprint.c_str());
453
454 // ro.vendor.build.version.sdk
455 std::string sdk = base::GetProperty("ro.vendor.build.version.sdk", "0");
456 ALOGV("property_get for ro.vendor.build.version.sdk == '%s'", sdk.c_str());
457
458 std::string brand;
459 std::string product;
460 std::string device;
461 size_t pos1;
462 pos1 = fingerprint.find('/');
463 if (pos1 != std::string::npos) {
464 brand = fingerprint.substr(0, pos1);
465 size_t pos2 = fingerprint.find('/', pos1+1);
466 if (pos2 != std::string::npos) {
467 product = fingerprint.substr(pos1+1, pos2 - pos1 - 1);
468 size_t pos3 = fingerprint.find('/', pos2+1);
469 if (pos3 != std::string::npos) {
470 device = fingerprint.substr(pos2+1, pos3 - pos2 - 1);
471 size_t pos4 = device.find(':');
472 if (pos4 != std::string::npos) {
473 device.resize(pos4);
474 }
475 }
476 }
477 }
478
479 ALOGV("parsed: sdk '%s' brand '%s' product '%s' device '%s'",
480 sdk.c_str(), brand.c_str(), product.c_str(), device.c_str());
481
482 std::string base = "/apex/com.android.media/etc/formatshaper";
483
484 // looking in these directories within the apex
485 const std::vector<std::string> modulePathnames = {
486 base + "/" + sdk + "/" + brand + "/" + product + "/" + device,
487 base + "/" + sdk + "/" + brand + "/" + product,
488 base + "/" + sdk + "/" + brand,
489 base + "/" + sdk,
490 base
491 };
492
493 parser.parseXmlFilesInSearchDirs( { "media_codecs_shaping.xml" }, modulePathnames);
494 }
495
Pawin Vongmasa36653902018-11-15 00:10:25 -0800496 if (parser.getParsingStatus() != OK) {
497 ALOGD("XML parser no good");
498 return OK;
499 }
500
Lajos Molnar424cfb52019-04-08 17:48:00 -0700501 MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap();
502 for (const auto &v : settings) {
503 if (!hasPrefix(v.first, "media-type-")
504 && !hasPrefix(v.first, "domain-")
505 && !hasPrefix(v.first, "variant-")) {
506 writer->addGlobalSetting(v.first.c_str(), v.second.c_str());
507 }
508 }
509
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800510 std::map<std::string, PixelFormatMap> nameToPixelFormatMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800511 for (const Traits& trait : traits) {
512 C2Component::rank_t rank = trait.rank;
513
Lajos Molnardb5751f2019-01-31 17:01:49 -0800514 // Interface must be accessible for us to list the component, and there also
515 // must be an XML entry for the codec. Codec aliases listed in the traits
516 // allow additional XML entries to be specified for each alias. These will
517 // be listed as separate codecs. If no XML entry is specified for an alias,
518 // those will be treated as an additional alias specified in the XML entry
519 // for the interface name.
520 std::vector<std::string> nameAndAliases = trait.aliases;
521 nameAndAliases.insert(nameAndAliases.begin(), trait.name);
522 for (const std::string &nameOrAlias : nameAndAliases) {
523 bool isAlias = trait.name != nameOrAlias;
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800524 std::shared_ptr<Codec2Client> client;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800525 std::shared_ptr<Codec2Client::Interface> intf =
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800526 Codec2Client::CreateInterfaceByName(nameOrAlias.c_str(), &client);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800527 if (!intf) {
528 ALOGD("could not create interface for %s'%s'",
529 isAlias ? "alias " : "",
530 nameOrAlias.c_str());
531 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800532 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800533 if (parser.getCodecMap().count(nameOrAlias) == 0) {
534 if (isAlias) {
535 std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo =
536 writer->findMediaCodecInfo(trait.name.c_str());
537 if (!baseCodecInfo) {
538 ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing",
539 nameOrAlias.c_str(),
540 trait.name.c_str());
541 } else {
542 ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this",
543 nameOrAlias.c_str());
544 // merge alias into existing codec
545 baseCodecInfo->addAlias(nameOrAlias.c_str());
546 }
547 } else {
548 ALOGD("component '%s' not found in xml", trait.name.c_str());
549 }
550 continue;
551 }
552 std::string canonName = trait.name;
553
554 // TODO: Remove this block once all codecs are enabled by default.
555 switch (option) {
556 case 0:
557 continue;
558 case 1:
559 if (hasPrefix(canonName, "c2.vda.")) {
560 break;
561 }
562 if (hasPrefix(canonName, "c2.android.")) {
563 if (trait.domain == C2Component::DOMAIN_AUDIO) {
564 rank = 1;
565 break;
566 }
567 break;
568 }
569 if (hasSuffix(canonName, ".avc.decoder") ||
570 hasSuffix(canonName, ".avc.encoder")) {
571 rank = std::numeric_limits<decltype(rank)>::max();
572 break;
573 }
574 continue;
575 case 2:
576 if (hasPrefix(canonName, "c2.vda.")) {
577 break;
578 }
579 if (hasPrefix(canonName, "c2.android.")) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800580 rank = 1;
581 break;
582 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800583 if (hasSuffix(canonName, ".avc.decoder") ||
584 hasSuffix(canonName, ".avc.encoder")) {
585 rank = std::numeric_limits<decltype(rank)>::max();
586 break;
587 }
588 continue;
589 case 3:
590 if (hasPrefix(canonName, "c2.android.")) {
591 rank = 1;
592 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800593 break;
594 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800595
Lajos Molnar424cfb52019-04-08 17:48:00 -0700596 const MediaCodecsXmlParser::CodecProperties &codec =
597 parser.getCodecMap().at(nameOrAlias);
598
599 // verify that either the codec is explicitly enabled, or one of its domains is
600 bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end();
601 if (!codecEnabled) {
602 for (const std::string &domain : codec.domainSet) {
603 const Switch enabled = isDomainEnabled(domain, settings);
604 ALOGV("codec entry '%s' is in domain '%s' that is '%s'",
605 nameOrAlias.c_str(), domain.c_str(), asString(enabled));
606 if (enabled) {
607 codecEnabled = true;
608 break;
609 }
610 }
611 }
612 // if codec has variants, also check that at least one of them is enabled
613 bool variantEnabled = codec.variantSet.empty();
614 for (const std::string &variant : codec.variantSet) {
615 const Switch enabled = isVariantExpressionEnabled(variant, settings);
616 ALOGV("codec entry '%s' has a variant '%s' that is '%s'",
617 nameOrAlias.c_str(), variant.c_str(), asString(enabled));
618 if (enabled) {
619 variantEnabled = true;
620 break;
621 }
622 }
623 if (!codecEnabled || !variantEnabled) {
624 ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str());
625 continue;
626 }
627
Lajos Molnardb5751f2019-01-31 17:01:49 -0800628 ALOGV("adding codec entry for '%s'", nameOrAlias.c_str());
629 std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
630 codecInfo->setName(nameOrAlias.c_str());
631 codecInfo->setOwner(("codec2::" + trait.owner).c_str());
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800632
Lajos Molnardb5751f2019-01-31 17:01:49 -0800633 bool encoder = trait.kind == C2Component::KIND_ENCODER;
634 typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800635
Lajos Molnardb5751f2019-01-31 17:01:49 -0800636 if (encoder) {
637 attrs |= MediaCodecInfo::kFlagIsEncoder;
638 }
639 if (trait.owner == "software") {
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800640 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800641 } else {
642 attrs |= MediaCodecInfo::kFlagIsVendor;
643 if (trait.owner == "vendor-software") {
644 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
645 } else if (codec.quirkSet.find("attribute::software-codec")
646 == codec.quirkSet.end()) {
647 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800648 }
649 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800650 codecInfo->setAttributes(attrs);
651 if (!codec.rank.empty()) {
652 uint32_t xmlRank;
653 char dummy;
654 if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) {
655 rank = xmlRank;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800656 }
657 }
Lajos Molnar424cfb52019-04-08 17:48:00 -0700658 ALOGV("rank: %u", (unsigned)rank);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800659 codecInfo->setRank(rank);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800660
Lajos Molnardb5751f2019-01-31 17:01:49 -0800661 for (const std::string &alias : codec.aliases) {
662 ALOGV("adding alias '%s'", alias.c_str());
663 codecInfo->addAlias(alias.c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800664 }
665
Lajos Molnardb5751f2019-01-31 17:01:49 -0800666 for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
667 const std::string &mediaType = typeIt->first;
Lajos Molnar424cfb52019-04-08 17:48:00 -0700668 const Switch typeEnabled = isSettingEnabled(
669 "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT());
670 const Switch domainTypeEnabled = isSettingEnabled(
671 "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"),
672 settings, Switch::ENABLED_BY_DEFAULT());
673 ALOGV("type '%s-%s' is '%s/%s'",
674 mediaType.c_str(), (encoder ? "encoder" : "decoder"),
675 asString(typeEnabled), asString(domainTypeEnabled));
676 if (!typeEnabled || !domainTypeEnabled) {
677 ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(),
678 nameOrAlias.c_str());
679 continue;
680 }
681
682 ALOGI("adding type '%s'", typeIt->first.c_str());
Lajos Molnardb5751f2019-01-31 17:01:49 -0800683 const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
684 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
685 codecInfo->addMediaType(mediaType.c_str());
Lajos Molnar424cfb52019-04-08 17:48:00 -0700686 for (const auto &v : attrMap) {
687 std::string key = v.first;
688 std::string value = v.second;
689
690 size_t variantSep = key.find(":::");
691 if (variantSep != std::string::npos) {
692 std::string variant = key.substr(0, variantSep);
693 const Switch enabled = isVariantExpressionEnabled(variant, settings);
694 ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled));
695 if (!enabled) {
696 continue;
697 }
698 key = key.substr(variantSep + 3);
699 }
700
Lajos Molnardb5751f2019-01-31 17:01:49 -0800701 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
702 int32_t intValue = 0;
703 // Ignore trailing bad characters and default to 0.
704 (void)sscanf(value.c_str(), "%d", &intValue);
705 caps->addDetail(key.c_str(), intValue);
706 } else {
707 caps->addDetail(key.c_str(), value.c_str());
708 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800709 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800710
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700711 if (!addSupportedProfileLevels(intf, caps.get(), trait, mediaType)) {
712 // TODO(b/193279646) This will get fixed in C2InterfaceHelper
713 // Some components may not advertise supported values if they use a const
714 // param for profile/level (they support only one profile). For now cover
715 // only VP8 here until it is fixed.
716 if (mediaType == MIMETYPE_VIDEO_VP8) {
717 caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
718 }
719 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800720
721 auto it = nameToPixelFormatMap.find(client->getServiceName());
722 if (it == nameToPixelFormatMap.end()) {
723 it = nameToPixelFormatMap.try_emplace(client->getServiceName()).first;
724 PixelFormatMap &pixelFormatMap = it->second;
725 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_420_888] = COLOR_FormatYUV420Flexible;
726 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_P010] = COLOR_FormatYUVP010;
727 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_1010102] = COLOR_Format32bitABGR2101010;
728 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_FP16] = COLOR_Format64bitABGRFloat;
729
730 std::shared_ptr<C2StoreFlexiblePixelFormatDescriptorsInfo> pixelFormatInfo;
731 std::vector<std::unique_ptr<C2Param>> heapParams;
732 if (client->query(
733 {},
734 {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
735 C2_MAY_BLOCK,
736 &heapParams) == C2_OK
737 && heapParams.size() == 1u) {
738 pixelFormatInfo.reset(C2StoreFlexiblePixelFormatDescriptorsInfo::From(
739 heapParams[0].release()));
740 }
741 if (pixelFormatInfo && *pixelFormatInfo) {
742 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
743 C2FlexiblePixelFormatDescriptorStruct &desc =
744 pixelFormatInfo->m.values[i];
745 std::optional<int32_t> colorFormat = findFrameworkColorFormat(desc);
746 if (colorFormat) {
747 pixelFormatMap[desc.pixelFormat] = *colorFormat;
748 }
749 }
750 }
751 }
752 addSupportedColorFormats(
753 intf, caps.get(), trait, mediaType, it->second);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800754 }
755 }
756 }
757 return OK;
758}
759
760} // namespace android
761
762extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
763 return new android::Codec2InfoBuilder;
764}