blob: ec6ff8a97eb8f12e99c5fe92110634dc2a2aca82 [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 Mahendrakarb49c2782022-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()) {
140 case C2StreamHdr10PlusInfo::CORE_INDEX:
Lajos Molnardb5751f2019-01-31 17:01:49 -0800141 supportsHdr10Plus = true;
142 break;
Lajos Molnar739fe732021-02-07 13:06:10 -0800143 case C2StreamHdrStaticInfo::CORE_INDEX:
Lajos Molnardb5751f2019-01-31 17:01:49 -0800144 supportsHdr = true;
145 break;
146 default:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800147 break;
148 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800149 }
150 }
151
Chong Zhang0702d1f2019-08-15 11:45:36 -0700152 // For VP9/AV1, the static info is always propagated by framework.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800153 supportsHdr |= (mediaType == MIMETYPE_VIDEO_VP9);
Chong Zhang0702d1f2019-08-15 11:45:36 -0700154 supportsHdr |= (mediaType == MIMETYPE_VIDEO_AV1);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800155
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700156 // HDR support implies 10-bit support.
157 // TODO: directly check this from the component interface
158 supports10Bit = (supportsHdr || supportsHdr10Plus);
159
Harish Mahendrakarb49c2782022-05-24 15:48:34 -0700160 // If the device doesn't support HDR display, then no codec on the device
161 // can advertise support for HDR profiles.
162 // Default to true to maintain backward compatibility
163 auto ret = sysprop::SurfaceFlingerProperties::has_HDR_display();
164 bool hasHDRDisplay = ret.has_value() ? *ret : true;
165
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700166 bool added = false;
167
Lajos Molnardb5751f2019-01-31 17:01:49 -0800168 for (C2Value::Primitive profile : profileQuery[0].values.values) {
169 pl.profile = (C2Config::profile_t)profile.ref<uint32_t>();
170 std::vector<std::unique_ptr<C2SettingResult>> failures;
171 err = intf->config({&pl}, C2_DONT_BLOCK, &failures);
172 ALOGV("set profile to %u -> %s", pl.profile, asString(err));
173 std::vector<C2FieldSupportedValuesQuery> levelQuery = {
174 C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level))
175 };
176 err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK);
177 ALOGV("query supported levels -> %s | %s", asString(err), asString(levelQuery[0].status));
178 if (err != C2_OK || levelQuery[0].status != C2_OK
179 || levelQuery[0].values.type != C2FieldSupportedValues::VALUES
180 || levelQuery[0].values.values.size() == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800181 continue;
182 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800183
184 C2Value::Primitive level = levelQuery[0].values.values.back();
185 pl.level = (C2Config::level_t)level.ref<uint32_t>();
186 ALOGV("supporting level: %u", pl.level);
187 int32_t sdkProfile, sdkLevel;
188 if (mapper && mapper->mapProfile(pl.profile, &sdkProfile)
189 && mapper->mapLevel(pl.level, &sdkLevel)) {
190 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
Harish Mahendrakarb49c2782022-05-24 15:48:34 -0700191 // also list HDR profiles if component supports HDR and device has HDR display
192 if (supportsHdr && hasHDRDisplay) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800193 auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper(trait.mediaType);
194 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
195 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
196 }
197 if (supportsHdr10Plus) {
198 hdrMapper = C2Mapper::GetHdrProfileLevelMapper(
199 trait.mediaType, true /*isHdr10Plus*/);
200 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
201 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
202 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800203 }
204 }
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700205 if (supports10Bit) {
206 auto bitnessMapper = C2Mapper::GetBitDepthProfileLevelMapper(trait.mediaType, 10);
207 if (bitnessMapper && bitnessMapper->mapProfile(pl.profile, &sdkProfile)) {
208 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
209 }
210 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800211 } else if (!mapper) {
212 caps->addProfileLevel(pl.profile, pl.level);
213 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700214 added = true;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800215
216 // for H.263 also advertise the second highest level if the
217 // codec supports level 45, as level 45 only covers level 10
218 // TODO: move this to some form of a setting so it does not
219 // have to be here
220 if (mediaType == MIMETYPE_VIDEO_H263) {
221 C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED;
222 for (C2Value::Primitive v : levelQuery[0].values.values) {
223 C2Config::level_t level = (C2Config::level_t)v.ref<uint32_t>();
224 if (level < C2Config::LEVEL_H263_45 && level > nextLevel) {
225 nextLevel = level;
226 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800227 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800228 if (nextLevel != C2Config::LEVEL_UNUSED
229 && nextLevel != pl.level
230 && mapper
231 && mapper->mapProfile(pl.profile, &sdkProfile)
232 && mapper->mapLevel(nextLevel, &sdkLevel)) {
233 caps->addProfileLevel(
234 (uint32_t)sdkProfile, (uint32_t)sdkLevel);
235 }
236 }
237 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700238 return added;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800239}
240
241void addSupportedColorFormats(
242 std::shared_ptr<Codec2Client::Interface> intf,
243 MediaCodecInfo::CapabilitiesWriter *caps,
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800244 const Traits& trait, const std::string &mediaType,
245 const PixelFormatMap &pixelFormatMap) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800246 // TODO: get this from intf() as well, but how do we map them to
247 // MediaCodec color formats?
248 bool encoder = trait.kind == C2Component::KIND_ENCODER;
Wonsik Kim16223262019-06-14 14:40:57 -0700249 if (mediaType.find("video") != std::string::npos
250 || mediaType.find("image") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800251
252 std::vector<C2FieldSupportedValuesQuery> query;
253 if (encoder) {
254 C2StreamPixelFormatInfo::input pixelFormat;
255 query.push_back(C2FieldSupportedValuesQuery::Possible(
256 C2ParamField::Make(pixelFormat, pixelFormat.value)));
257 } else {
258 C2StreamPixelFormatInfo::output pixelFormat;
259 query.push_back(C2FieldSupportedValuesQuery::Possible(
260 C2ParamField::Make(pixelFormat, pixelFormat.value)));
261 }
262 std::list<int32_t> supportedColorFormats;
263 if (intf->querySupportedValues(query, C2_DONT_BLOCK) == C2_OK) {
264 if (query[0].status == C2_OK) {
265 const C2FieldSupportedValues &fsv = query[0].values;
266 if (fsv.type == C2FieldSupportedValues::VALUES) {
267 for (C2Value::Primitive value : fsv.values) {
268 auto it = pixelFormatMap.find(value.u32);
269 if (it != pixelFormatMap.end()) {
270 auto it2 = std::find(
271 supportedColorFormats.begin(),
272 supportedColorFormats.end(),
273 it->second);
274 if (it2 == supportedColorFormats.end()) {
275 supportedColorFormats.push_back(it->second);
276 }
277 }
278 }
279 }
280 }
281 }
282 auto addDefaultColorFormat = [caps, &supportedColorFormats](int32_t colorFormat) {
283 caps->addColorFormat(colorFormat);
284 auto it = std::find(
285 supportedColorFormats.begin(), supportedColorFormats.end(), colorFormat);
286 if (it != supportedColorFormats.end()) {
287 supportedColorFormats.erase(it);
288 }
289 };
290
My Name298764f2022-03-25 15:07:51 -0700291 // The color format is ordered by preference. The intention here is to advertise:
292 // c2.android.* codecs: YUV420s, Surface, <the rest>
293 // all other codecs: Surface, YUV420s, <the rest>
294 // TODO: get this preference via Codec2 API
295
Lajos Molnardb5751f2019-01-31 17:01:49 -0800296 // vendor video codecs prefer opaque format
297 if (trait.name.find("android") == std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800298 addDefaultColorFormat(COLOR_FormatSurface);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800299 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800300 addDefaultColorFormat(COLOR_FormatYUV420Flexible);
301 addDefaultColorFormat(COLOR_FormatYUV420Planar);
302 addDefaultColorFormat(COLOR_FormatYUV420SemiPlanar);
303 addDefaultColorFormat(COLOR_FormatYUV420PackedPlanar);
304 addDefaultColorFormat(COLOR_FormatYUV420PackedSemiPlanar);
My Name298764f2022-03-25 15:07:51 -0700305 // Android video codecs prefer CPU-readable formats
306 if (trait.name.find("android") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800307 addDefaultColorFormat(COLOR_FormatSurface);
308 }
309 for (int32_t colorFormat : supportedColorFormats) {
310 caps->addColorFormat(colorFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800311 }
312 }
313}
314
Lajos Molnar424cfb52019-04-08 17:48:00 -0700315class Switch {
316 enum Flags : uint8_t {
317 // flags
318 IS_ENABLED = (1 << 0),
319 BY_DEFAULT = (1 << 1),
320 };
321
322 constexpr Switch(uint8_t flags) : mFlags(flags) {}
323
324 uint8_t mFlags;
325
326public:
327 // have to create class due to this bool conversion operator...
328 constexpr operator bool() const {
329 return mFlags & IS_ENABLED;
330 }
331
332 constexpr Switch operator!() const {
333 return Switch(mFlags ^ IS_ENABLED);
334 }
335
336 static constexpr Switch DISABLED() { return 0; };
337 static constexpr Switch ENABLED() { return IS_ENABLED; };
338 static constexpr Switch DISABLED_BY_DEFAULT() { return BY_DEFAULT; };
339 static constexpr Switch ENABLED_BY_DEFAULT() { return IS_ENABLED | BY_DEFAULT; };
340
341 const char *toString(const char *def = "??") const {
342 switch (mFlags) {
343 case 0: return "0";
344 case IS_ENABLED: return "1";
345 case BY_DEFAULT: return "(0)";
346 case IS_ENABLED | BY_DEFAULT: return "(1)";
347 default: return def;
348 }
349 }
350
351};
352
353const char *asString(const Switch &s, const char *def = "??") {
354 return s.toString(def);
355}
356
357Switch isSettingEnabled(
358 std::string setting, const MediaCodecsXmlParser::AttributeMap &settings,
359 Switch def = Switch::DISABLED_BY_DEFAULT()) {
360 const auto enablement = settings.find(setting);
361 if (enablement == settings.end()) {
362 return def;
363 }
364 return enablement->second == "1" ? Switch::ENABLED() : Switch::DISABLED();
365}
366
367Switch isVariantEnabled(
368 std::string variant, const MediaCodecsXmlParser::AttributeMap &settings) {
369 return isSettingEnabled("variant-" + variant, settings);
370}
371
372Switch isVariantExpressionEnabled(
373 std::string exp, const MediaCodecsXmlParser::AttributeMap &settings) {
374 if (!exp.empty() && exp.at(0) == '!') {
375 return !isVariantEnabled(exp.substr(1, exp.size() - 1), settings);
376 }
377 return isVariantEnabled(exp, settings);
378}
379
380Switch isDomainEnabled(
381 std::string domain, const MediaCodecsXmlParser::AttributeMap &settings) {
382 return isSettingEnabled("domain-" + domain, settings);
383}
384
Pawin Vongmasa36653902018-11-15 00:10:25 -0800385} // unnamed namespace
386
387status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
388 // TODO: Remove run-time configurations once all codecs are working
389 // properly. (Assume "full" behavior eventually.)
390 //
391 // debug.stagefright.ccodec supports 5 values.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800392 // 0 - No Codec 2.0 components are available.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800393 // 1 - Audio decoders and encoders with prefix "c2.android." are available
394 // and ranked first.
395 // All other components with prefix "c2.android." are available with
396 // their normal ranks.
397 // Components with prefix "c2.vda." are available with their normal
398 // ranks.
399 // All other components with suffix ".avc.decoder" or ".avc.encoder"
400 // are available but ranked last.
401 // 2 - Components with prefix "c2.android." are available and ranked
402 // first.
403 // Components with prefix "c2.vda." are available with their normal
404 // ranks.
405 // All other components with suffix ".avc.decoder" or ".avc.encoder"
406 // are available but ranked last.
407 // 3 - Components with prefix "c2.android." are available and ranked
408 // first.
409 // All other components are available with their normal ranks.
410 // 4 - All components are available with their normal ranks.
411 //
412 // The default value (boot time) is 1.
413 //
414 // Note: Currently, OMX components have default rank 0x100, while all
415 // Codec2.0 software components have default rank 0x200.
Lajos Molnar8635fc82019-05-17 17:35:10 +0000416 int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 4);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800417
418 // Obtain Codec2Client
419 std::vector<Traits> traits = Codec2Client::ListComponents();
420
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800421 // parse APEX XML first, followed by vendor XML.
422 // Note: APEX XML names do not depend on ro.media.xml_variant.* properties.
Lajos Molnarda666892019-04-08 17:25:34 -0700423 MediaCodecsXmlParser parser;
424 parser.parseXmlFilesInSearchDirs(
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800425 { "media_codecs.xml", "media_codecs_performance.xml" },
Lajos Molnar424cfb52019-04-08 17:48:00 -0700426 { "/apex/com.android.media.swcodec/etc" });
427
428 // TODO: remove these c2-specific files once product moved to default file names
429 parser.parseXmlFilesInSearchDirs(
Lajos Molnarda666892019-04-08 17:25:34 -0700430 { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
Lajos Molnar424cfb52019-04-08 17:48:00 -0700431
432 // parse default XML files
433 parser.parseXmlFilesInSearchDirs();
434
Ray Essick8c4e9c72021-03-15 15:25:21 -0700435 // The mainline modules for media may optionally include some codec shaping information.
436 // Based on vendor partition SDK, and the brand/product/device information
437 // (expect to be empty in almost always)
438 //
439 {
440 // get build info so we know what file to search
441 // ro.vendor.build.fingerprint
442 std::string fingerprint = base::GetProperty("ro.vendor.build.fingerprint",
443 "brand/product/device:");
444 ALOGV("property_get for ro.vendor.build.fingerprint == '%s'", fingerprint.c_str());
445
446 // ro.vendor.build.version.sdk
447 std::string sdk = base::GetProperty("ro.vendor.build.version.sdk", "0");
448 ALOGV("property_get for ro.vendor.build.version.sdk == '%s'", sdk.c_str());
449
450 std::string brand;
451 std::string product;
452 std::string device;
453 size_t pos1;
454 pos1 = fingerprint.find('/');
455 if (pos1 != std::string::npos) {
456 brand = fingerprint.substr(0, pos1);
457 size_t pos2 = fingerprint.find('/', pos1+1);
458 if (pos2 != std::string::npos) {
459 product = fingerprint.substr(pos1+1, pos2 - pos1 - 1);
460 size_t pos3 = fingerprint.find('/', pos2+1);
461 if (pos3 != std::string::npos) {
462 device = fingerprint.substr(pos2+1, pos3 - pos2 - 1);
463 size_t pos4 = device.find(':');
464 if (pos4 != std::string::npos) {
465 device.resize(pos4);
466 }
467 }
468 }
469 }
470
471 ALOGV("parsed: sdk '%s' brand '%s' product '%s' device '%s'",
472 sdk.c_str(), brand.c_str(), product.c_str(), device.c_str());
473
474 std::string base = "/apex/com.android.media/etc/formatshaper";
475
476 // looking in these directories within the apex
477 const std::vector<std::string> modulePathnames = {
478 base + "/" + sdk + "/" + brand + "/" + product + "/" + device,
479 base + "/" + sdk + "/" + brand + "/" + product,
480 base + "/" + sdk + "/" + brand,
481 base + "/" + sdk,
482 base
483 };
484
485 parser.parseXmlFilesInSearchDirs( { "media_codecs_shaping.xml" }, modulePathnames);
486 }
487
Pawin Vongmasa36653902018-11-15 00:10:25 -0800488 if (parser.getParsingStatus() != OK) {
489 ALOGD("XML parser no good");
490 return OK;
491 }
492
Lajos Molnar424cfb52019-04-08 17:48:00 -0700493 MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap();
494 for (const auto &v : settings) {
495 if (!hasPrefix(v.first, "media-type-")
496 && !hasPrefix(v.first, "domain-")
497 && !hasPrefix(v.first, "variant-")) {
498 writer->addGlobalSetting(v.first.c_str(), v.second.c_str());
499 }
500 }
501
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800502 std::map<std::string, PixelFormatMap> nameToPixelFormatMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800503 for (const Traits& trait : traits) {
504 C2Component::rank_t rank = trait.rank;
505
Lajos Molnardb5751f2019-01-31 17:01:49 -0800506 // Interface must be accessible for us to list the component, and there also
507 // must be an XML entry for the codec. Codec aliases listed in the traits
508 // allow additional XML entries to be specified for each alias. These will
509 // be listed as separate codecs. If no XML entry is specified for an alias,
510 // those will be treated as an additional alias specified in the XML entry
511 // for the interface name.
512 std::vector<std::string> nameAndAliases = trait.aliases;
513 nameAndAliases.insert(nameAndAliases.begin(), trait.name);
514 for (const std::string &nameOrAlias : nameAndAliases) {
515 bool isAlias = trait.name != nameOrAlias;
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800516 std::shared_ptr<Codec2Client> client;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800517 std::shared_ptr<Codec2Client::Interface> intf =
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800518 Codec2Client::CreateInterfaceByName(nameOrAlias.c_str(), &client);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800519 if (!intf) {
520 ALOGD("could not create interface for %s'%s'",
521 isAlias ? "alias " : "",
522 nameOrAlias.c_str());
523 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800524 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800525 if (parser.getCodecMap().count(nameOrAlias) == 0) {
526 if (isAlias) {
527 std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo =
528 writer->findMediaCodecInfo(trait.name.c_str());
529 if (!baseCodecInfo) {
530 ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing",
531 nameOrAlias.c_str(),
532 trait.name.c_str());
533 } else {
534 ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this",
535 nameOrAlias.c_str());
536 // merge alias into existing codec
537 baseCodecInfo->addAlias(nameOrAlias.c_str());
538 }
539 } else {
540 ALOGD("component '%s' not found in xml", trait.name.c_str());
541 }
542 continue;
543 }
544 std::string canonName = trait.name;
545
546 // TODO: Remove this block once all codecs are enabled by default.
547 switch (option) {
548 case 0:
549 continue;
550 case 1:
551 if (hasPrefix(canonName, "c2.vda.")) {
552 break;
553 }
554 if (hasPrefix(canonName, "c2.android.")) {
555 if (trait.domain == C2Component::DOMAIN_AUDIO) {
556 rank = 1;
557 break;
558 }
559 break;
560 }
561 if (hasSuffix(canonName, ".avc.decoder") ||
562 hasSuffix(canonName, ".avc.encoder")) {
563 rank = std::numeric_limits<decltype(rank)>::max();
564 break;
565 }
566 continue;
567 case 2:
568 if (hasPrefix(canonName, "c2.vda.")) {
569 break;
570 }
571 if (hasPrefix(canonName, "c2.android.")) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800572 rank = 1;
573 break;
574 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800575 if (hasSuffix(canonName, ".avc.decoder") ||
576 hasSuffix(canonName, ".avc.encoder")) {
577 rank = std::numeric_limits<decltype(rank)>::max();
578 break;
579 }
580 continue;
581 case 3:
582 if (hasPrefix(canonName, "c2.android.")) {
583 rank = 1;
584 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800585 break;
586 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800587
Lajos Molnar424cfb52019-04-08 17:48:00 -0700588 const MediaCodecsXmlParser::CodecProperties &codec =
589 parser.getCodecMap().at(nameOrAlias);
590
591 // verify that either the codec is explicitly enabled, or one of its domains is
592 bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end();
593 if (!codecEnabled) {
594 for (const std::string &domain : codec.domainSet) {
595 const Switch enabled = isDomainEnabled(domain, settings);
596 ALOGV("codec entry '%s' is in domain '%s' that is '%s'",
597 nameOrAlias.c_str(), domain.c_str(), asString(enabled));
598 if (enabled) {
599 codecEnabled = true;
600 break;
601 }
602 }
603 }
604 // if codec has variants, also check that at least one of them is enabled
605 bool variantEnabled = codec.variantSet.empty();
606 for (const std::string &variant : codec.variantSet) {
607 const Switch enabled = isVariantExpressionEnabled(variant, settings);
608 ALOGV("codec entry '%s' has a variant '%s' that is '%s'",
609 nameOrAlias.c_str(), variant.c_str(), asString(enabled));
610 if (enabled) {
611 variantEnabled = true;
612 break;
613 }
614 }
615 if (!codecEnabled || !variantEnabled) {
616 ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str());
617 continue;
618 }
619
Lajos Molnardb5751f2019-01-31 17:01:49 -0800620 ALOGV("adding codec entry for '%s'", nameOrAlias.c_str());
621 std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
622 codecInfo->setName(nameOrAlias.c_str());
623 codecInfo->setOwner(("codec2::" + trait.owner).c_str());
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800624
Lajos Molnardb5751f2019-01-31 17:01:49 -0800625 bool encoder = trait.kind == C2Component::KIND_ENCODER;
626 typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800627
Lajos Molnardb5751f2019-01-31 17:01:49 -0800628 if (encoder) {
629 attrs |= MediaCodecInfo::kFlagIsEncoder;
630 }
631 if (trait.owner == "software") {
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800632 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800633 } else {
634 attrs |= MediaCodecInfo::kFlagIsVendor;
635 if (trait.owner == "vendor-software") {
636 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
637 } else if (codec.quirkSet.find("attribute::software-codec")
638 == codec.quirkSet.end()) {
639 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800640 }
641 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800642 codecInfo->setAttributes(attrs);
643 if (!codec.rank.empty()) {
644 uint32_t xmlRank;
645 char dummy;
646 if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) {
647 rank = xmlRank;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800648 }
649 }
Lajos Molnar424cfb52019-04-08 17:48:00 -0700650 ALOGV("rank: %u", (unsigned)rank);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800651 codecInfo->setRank(rank);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800652
Lajos Molnardb5751f2019-01-31 17:01:49 -0800653 for (const std::string &alias : codec.aliases) {
654 ALOGV("adding alias '%s'", alias.c_str());
655 codecInfo->addAlias(alias.c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800656 }
657
Lajos Molnardb5751f2019-01-31 17:01:49 -0800658 for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
659 const std::string &mediaType = typeIt->first;
Lajos Molnar424cfb52019-04-08 17:48:00 -0700660 const Switch typeEnabled = isSettingEnabled(
661 "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT());
662 const Switch domainTypeEnabled = isSettingEnabled(
663 "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"),
664 settings, Switch::ENABLED_BY_DEFAULT());
665 ALOGV("type '%s-%s' is '%s/%s'",
666 mediaType.c_str(), (encoder ? "encoder" : "decoder"),
667 asString(typeEnabled), asString(domainTypeEnabled));
668 if (!typeEnabled || !domainTypeEnabled) {
669 ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(),
670 nameOrAlias.c_str());
671 continue;
672 }
673
674 ALOGI("adding type '%s'", typeIt->first.c_str());
Lajos Molnardb5751f2019-01-31 17:01:49 -0800675 const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
676 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
677 codecInfo->addMediaType(mediaType.c_str());
Lajos Molnar424cfb52019-04-08 17:48:00 -0700678 for (const auto &v : attrMap) {
679 std::string key = v.first;
680 std::string value = v.second;
681
682 size_t variantSep = key.find(":::");
683 if (variantSep != std::string::npos) {
684 std::string variant = key.substr(0, variantSep);
685 const Switch enabled = isVariantExpressionEnabled(variant, settings);
686 ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled));
687 if (!enabled) {
688 continue;
689 }
690 key = key.substr(variantSep + 3);
691 }
692
Lajos Molnardb5751f2019-01-31 17:01:49 -0800693 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
694 int32_t intValue = 0;
695 // Ignore trailing bad characters and default to 0.
696 (void)sscanf(value.c_str(), "%d", &intValue);
697 caps->addDetail(key.c_str(), intValue);
698 } else {
699 caps->addDetail(key.c_str(), value.c_str());
700 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800701 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800702
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700703 if (!addSupportedProfileLevels(intf, caps.get(), trait, mediaType)) {
704 // TODO(b/193279646) This will get fixed in C2InterfaceHelper
705 // Some components may not advertise supported values if they use a const
706 // param for profile/level (they support only one profile). For now cover
707 // only VP8 here until it is fixed.
708 if (mediaType == MIMETYPE_VIDEO_VP8) {
709 caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
710 }
711 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800712
713 auto it = nameToPixelFormatMap.find(client->getServiceName());
714 if (it == nameToPixelFormatMap.end()) {
715 it = nameToPixelFormatMap.try_emplace(client->getServiceName()).first;
716 PixelFormatMap &pixelFormatMap = it->second;
717 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_420_888] = COLOR_FormatYUV420Flexible;
718 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_P010] = COLOR_FormatYUVP010;
719 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_1010102] = COLOR_Format32bitABGR2101010;
720 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_FP16] = COLOR_Format64bitABGRFloat;
721
722 std::shared_ptr<C2StoreFlexiblePixelFormatDescriptorsInfo> pixelFormatInfo;
723 std::vector<std::unique_ptr<C2Param>> heapParams;
724 if (client->query(
725 {},
726 {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
727 C2_MAY_BLOCK,
728 &heapParams) == C2_OK
729 && heapParams.size() == 1u) {
730 pixelFormatInfo.reset(C2StoreFlexiblePixelFormatDescriptorsInfo::From(
731 heapParams[0].release()));
732 }
733 if (pixelFormatInfo && *pixelFormatInfo) {
734 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
735 C2FlexiblePixelFormatDescriptorStruct &desc =
736 pixelFormatInfo->m.values[i];
737 std::optional<int32_t> colorFormat = findFrameworkColorFormat(desc);
738 if (colorFormat) {
739 pixelFormatMap[desc.pixelFormat] = *colorFormat;
740 }
741 }
742 }
743 }
744 addSupportedColorFormats(
745 intf, caps.get(), trait, mediaType, it->second);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800746 }
747 }
748 }
749 return OK;
750}
751
752} // namespace android
753
754extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
755 return new android::Codec2InfoBuilder;
756}