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