blob: 58e1c7fddaa7f348b40b3534f64db0b1f5e20710 [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()) {
Taehwan Kim2d222b82022-05-12 14:19:26 +0900139 case C2StreamHdrDynamicMetadataInfo::CORE_INDEX:
140 [[fallthrough]];
141 case C2StreamHdr10PlusInfo::CORE_INDEX: // will be deprecated
Lajos Molnardb5751f2019-01-31 17:01:49 -0800142 supportsHdr10Plus = true;
143 break;
Lajos Molnar739fe732021-02-07 13:06:10 -0800144 case C2StreamHdrStaticInfo::CORE_INDEX:
Lajos Molnardb5751f2019-01-31 17:01:49 -0800145 supportsHdr = true;
146 break;
147 default:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800148 break;
149 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800150 }
151 }
152
Lajos Molnarb857cde2022-05-25 10:20:37 -0700153 // VP9 does not support HDR metadata in the bitstream and static metadata
154 // can always be carried by the framework. (The framework does not propagate
155 // dynamic metadata as that needs to be frame accurate.)
Lajos Molnardb5751f2019-01-31 17:01:49 -0800156 supportsHdr |= (mediaType == MIMETYPE_VIDEO_VP9);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800157
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700158 // HDR support implies 10-bit support.
159 // TODO: directly check this from the component interface
160 supports10Bit = (supportsHdr || supportsHdr10Plus);
161
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700162 bool added = false;
163
Lajos Molnardb5751f2019-01-31 17:01:49 -0800164 for (C2Value::Primitive profile : profileQuery[0].values.values) {
165 pl.profile = (C2Config::profile_t)profile.ref<uint32_t>();
166 std::vector<std::unique_ptr<C2SettingResult>> failures;
167 err = intf->config({&pl}, C2_DONT_BLOCK, &failures);
168 ALOGV("set profile to %u -> %s", pl.profile, asString(err));
169 std::vector<C2FieldSupportedValuesQuery> levelQuery = {
170 C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level))
171 };
172 err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK);
173 ALOGV("query supported levels -> %s | %s", asString(err), asString(levelQuery[0].status));
174 if (err != C2_OK || levelQuery[0].status != C2_OK
175 || levelQuery[0].values.type != C2FieldSupportedValues::VALUES
176 || levelQuery[0].values.values.size() == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800177 continue;
178 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800179
180 C2Value::Primitive level = levelQuery[0].values.values.back();
181 pl.level = (C2Config::level_t)level.ref<uint32_t>();
182 ALOGV("supporting level: %u", pl.level);
183 int32_t sdkProfile, sdkLevel;
184 if (mapper && mapper->mapProfile(pl.profile, &sdkProfile)
185 && mapper->mapLevel(pl.level, &sdkLevel)) {
186 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
187 // also list HDR profiles if component supports HDR
188 if (supportsHdr) {
189 auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper(trait.mediaType);
190 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
191 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
192 }
193 if (supportsHdr10Plus) {
194 hdrMapper = C2Mapper::GetHdrProfileLevelMapper(
195 trait.mediaType, true /*isHdr10Plus*/);
196 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
197 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
198 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800199 }
200 }
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700201 if (supports10Bit) {
202 auto bitnessMapper = C2Mapper::GetBitDepthProfileLevelMapper(trait.mediaType, 10);
203 if (bitnessMapper && bitnessMapper->mapProfile(pl.profile, &sdkProfile)) {
204 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
205 }
206 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800207 } else if (!mapper) {
208 caps->addProfileLevel(pl.profile, pl.level);
209 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700210 added = true;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800211
212 // for H.263 also advertise the second highest level if the
213 // codec supports level 45, as level 45 only covers level 10
214 // TODO: move this to some form of a setting so it does not
215 // have to be here
216 if (mediaType == MIMETYPE_VIDEO_H263) {
217 C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED;
218 for (C2Value::Primitive v : levelQuery[0].values.values) {
219 C2Config::level_t level = (C2Config::level_t)v.ref<uint32_t>();
220 if (level < C2Config::LEVEL_H263_45 && level > nextLevel) {
221 nextLevel = level;
222 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800223 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800224 if (nextLevel != C2Config::LEVEL_UNUSED
225 && nextLevel != pl.level
226 && mapper
227 && mapper->mapProfile(pl.profile, &sdkProfile)
228 && mapper->mapLevel(nextLevel, &sdkLevel)) {
229 caps->addProfileLevel(
230 (uint32_t)sdkProfile, (uint32_t)sdkLevel);
231 }
232 }
233 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700234 return added;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800235}
236
237void addSupportedColorFormats(
238 std::shared_ptr<Codec2Client::Interface> intf,
239 MediaCodecInfo::CapabilitiesWriter *caps,
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800240 const Traits& trait, const std::string &mediaType,
241 const PixelFormatMap &pixelFormatMap) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800242 // TODO: get this from intf() as well, but how do we map them to
243 // MediaCodec color formats?
244 bool encoder = trait.kind == C2Component::KIND_ENCODER;
Wonsik Kim16223262019-06-14 14:40:57 -0700245 if (mediaType.find("video") != std::string::npos
246 || mediaType.find("image") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800247
248 std::vector<C2FieldSupportedValuesQuery> query;
249 if (encoder) {
250 C2StreamPixelFormatInfo::input pixelFormat;
251 query.push_back(C2FieldSupportedValuesQuery::Possible(
252 C2ParamField::Make(pixelFormat, pixelFormat.value)));
253 } else {
254 C2StreamPixelFormatInfo::output pixelFormat;
255 query.push_back(C2FieldSupportedValuesQuery::Possible(
256 C2ParamField::Make(pixelFormat, pixelFormat.value)));
257 }
258 std::list<int32_t> supportedColorFormats;
259 if (intf->querySupportedValues(query, C2_DONT_BLOCK) == C2_OK) {
260 if (query[0].status == C2_OK) {
261 const C2FieldSupportedValues &fsv = query[0].values;
262 if (fsv.type == C2FieldSupportedValues::VALUES) {
263 for (C2Value::Primitive value : fsv.values) {
264 auto it = pixelFormatMap.find(value.u32);
265 if (it != pixelFormatMap.end()) {
266 auto it2 = std::find(
267 supportedColorFormats.begin(),
268 supportedColorFormats.end(),
269 it->second);
270 if (it2 == supportedColorFormats.end()) {
271 supportedColorFormats.push_back(it->second);
272 }
273 }
274 }
275 }
276 }
277 }
278 auto addDefaultColorFormat = [caps, &supportedColorFormats](int32_t colorFormat) {
279 caps->addColorFormat(colorFormat);
280 auto it = std::find(
281 supportedColorFormats.begin(), supportedColorFormats.end(), colorFormat);
282 if (it != supportedColorFormats.end()) {
283 supportedColorFormats.erase(it);
284 }
285 };
286
My Name298764f2022-03-25 15:07:51 -0700287 // The color format is ordered by preference. The intention here is to advertise:
288 // c2.android.* codecs: YUV420s, Surface, <the rest>
289 // all other codecs: Surface, YUV420s, <the rest>
290 // TODO: get this preference via Codec2 API
291
Lajos Molnardb5751f2019-01-31 17:01:49 -0800292 // vendor video codecs prefer opaque format
293 if (trait.name.find("android") == std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800294 addDefaultColorFormat(COLOR_FormatSurface);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800295 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800296 addDefaultColorFormat(COLOR_FormatYUV420Flexible);
297 addDefaultColorFormat(COLOR_FormatYUV420Planar);
298 addDefaultColorFormat(COLOR_FormatYUV420SemiPlanar);
299 addDefaultColorFormat(COLOR_FormatYUV420PackedPlanar);
300 addDefaultColorFormat(COLOR_FormatYUV420PackedSemiPlanar);
My Name298764f2022-03-25 15:07:51 -0700301 // Android video codecs prefer CPU-readable formats
302 if (trait.name.find("android") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800303 addDefaultColorFormat(COLOR_FormatSurface);
304 }
305 for (int32_t colorFormat : supportedColorFormats) {
306 caps->addColorFormat(colorFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800307 }
308 }
309}
310
Lajos Molnar424cfb52019-04-08 17:48:00 -0700311class Switch {
312 enum Flags : uint8_t {
313 // flags
314 IS_ENABLED = (1 << 0),
315 BY_DEFAULT = (1 << 1),
316 };
317
318 constexpr Switch(uint8_t flags) : mFlags(flags) {}
319
320 uint8_t mFlags;
321
322public:
323 // have to create class due to this bool conversion operator...
324 constexpr operator bool() const {
325 return mFlags & IS_ENABLED;
326 }
327
328 constexpr Switch operator!() const {
329 return Switch(mFlags ^ IS_ENABLED);
330 }
331
332 static constexpr Switch DISABLED() { return 0; };
333 static constexpr Switch ENABLED() { return IS_ENABLED; };
334 static constexpr Switch DISABLED_BY_DEFAULT() { return BY_DEFAULT; };
335 static constexpr Switch ENABLED_BY_DEFAULT() { return IS_ENABLED | BY_DEFAULT; };
336
337 const char *toString(const char *def = "??") const {
338 switch (mFlags) {
339 case 0: return "0";
340 case IS_ENABLED: return "1";
341 case BY_DEFAULT: return "(0)";
342 case IS_ENABLED | BY_DEFAULT: return "(1)";
343 default: return def;
344 }
345 }
346
347};
348
349const char *asString(const Switch &s, const char *def = "??") {
350 return s.toString(def);
351}
352
353Switch isSettingEnabled(
354 std::string setting, const MediaCodecsXmlParser::AttributeMap &settings,
355 Switch def = Switch::DISABLED_BY_DEFAULT()) {
356 const auto enablement = settings.find(setting);
357 if (enablement == settings.end()) {
358 return def;
359 }
360 return enablement->second == "1" ? Switch::ENABLED() : Switch::DISABLED();
361}
362
363Switch isVariantEnabled(
364 std::string variant, const MediaCodecsXmlParser::AttributeMap &settings) {
365 return isSettingEnabled("variant-" + variant, settings);
366}
367
368Switch isVariantExpressionEnabled(
369 std::string exp, const MediaCodecsXmlParser::AttributeMap &settings) {
370 if (!exp.empty() && exp.at(0) == '!') {
371 return !isVariantEnabled(exp.substr(1, exp.size() - 1), settings);
372 }
373 return isVariantEnabled(exp, settings);
374}
375
376Switch isDomainEnabled(
377 std::string domain, const MediaCodecsXmlParser::AttributeMap &settings) {
378 return isSettingEnabled("domain-" + domain, settings);
379}
380
Pawin Vongmasa36653902018-11-15 00:10:25 -0800381} // unnamed namespace
382
383status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
384 // TODO: Remove run-time configurations once all codecs are working
385 // properly. (Assume "full" behavior eventually.)
386 //
387 // debug.stagefright.ccodec supports 5 values.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800388 // 0 - No Codec 2.0 components are available.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800389 // 1 - Audio decoders and encoders with prefix "c2.android." are available
390 // and ranked first.
391 // All other components with prefix "c2.android." are available with
392 // their normal ranks.
393 // Components with prefix "c2.vda." are available with their normal
394 // ranks.
395 // All other components with suffix ".avc.decoder" or ".avc.encoder"
396 // are available but ranked last.
397 // 2 - Components with prefix "c2.android." are available and ranked
398 // first.
399 // Components with prefix "c2.vda." are available with their normal
400 // ranks.
401 // All other components with suffix ".avc.decoder" or ".avc.encoder"
402 // are available but ranked last.
403 // 3 - Components with prefix "c2.android." are available and ranked
404 // first.
405 // All other components are available with their normal ranks.
406 // 4 - All components are available with their normal ranks.
407 //
408 // The default value (boot time) is 1.
409 //
410 // Note: Currently, OMX components have default rank 0x100, while all
411 // Codec2.0 software components have default rank 0x200.
Lajos Molnar8635fc82019-05-17 17:35:10 +0000412 int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 4);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800413
414 // Obtain Codec2Client
415 std::vector<Traits> traits = Codec2Client::ListComponents();
416
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800417 // parse APEX XML first, followed by vendor XML.
418 // Note: APEX XML names do not depend on ro.media.xml_variant.* properties.
Lajos Molnarda666892019-04-08 17:25:34 -0700419 MediaCodecsXmlParser parser;
420 parser.parseXmlFilesInSearchDirs(
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800421 { "media_codecs.xml", "media_codecs_performance.xml" },
Lajos Molnar424cfb52019-04-08 17:48:00 -0700422 { "/apex/com.android.media.swcodec/etc" });
423
424 // TODO: remove these c2-specific files once product moved to default file names
425 parser.parseXmlFilesInSearchDirs(
Lajos Molnarda666892019-04-08 17:25:34 -0700426 { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
Lajos Molnar424cfb52019-04-08 17:48:00 -0700427
428 // parse default XML files
429 parser.parseXmlFilesInSearchDirs();
430
Ray Essick8c4e9c72021-03-15 15:25:21 -0700431 // The mainline modules for media may optionally include some codec shaping information.
432 // Based on vendor partition SDK, and the brand/product/device information
433 // (expect to be empty in almost always)
434 //
435 {
436 // get build info so we know what file to search
437 // ro.vendor.build.fingerprint
438 std::string fingerprint = base::GetProperty("ro.vendor.build.fingerprint",
439 "brand/product/device:");
440 ALOGV("property_get for ro.vendor.build.fingerprint == '%s'", fingerprint.c_str());
441
442 // ro.vendor.build.version.sdk
443 std::string sdk = base::GetProperty("ro.vendor.build.version.sdk", "0");
444 ALOGV("property_get for ro.vendor.build.version.sdk == '%s'", sdk.c_str());
445
446 std::string brand;
447 std::string product;
448 std::string device;
449 size_t pos1;
450 pos1 = fingerprint.find('/');
451 if (pos1 != std::string::npos) {
452 brand = fingerprint.substr(0, pos1);
453 size_t pos2 = fingerprint.find('/', pos1+1);
454 if (pos2 != std::string::npos) {
455 product = fingerprint.substr(pos1+1, pos2 - pos1 - 1);
456 size_t pos3 = fingerprint.find('/', pos2+1);
457 if (pos3 != std::string::npos) {
458 device = fingerprint.substr(pos2+1, pos3 - pos2 - 1);
459 size_t pos4 = device.find(':');
460 if (pos4 != std::string::npos) {
461 device.resize(pos4);
462 }
463 }
464 }
465 }
466
467 ALOGV("parsed: sdk '%s' brand '%s' product '%s' device '%s'",
468 sdk.c_str(), brand.c_str(), product.c_str(), device.c_str());
469
470 std::string base = "/apex/com.android.media/etc/formatshaper";
471
472 // looking in these directories within the apex
473 const std::vector<std::string> modulePathnames = {
474 base + "/" + sdk + "/" + brand + "/" + product + "/" + device,
475 base + "/" + sdk + "/" + brand + "/" + product,
476 base + "/" + sdk + "/" + brand,
477 base + "/" + sdk,
478 base
479 };
480
481 parser.parseXmlFilesInSearchDirs( { "media_codecs_shaping.xml" }, modulePathnames);
482 }
483
Pawin Vongmasa36653902018-11-15 00:10:25 -0800484 if (parser.getParsingStatus() != OK) {
485 ALOGD("XML parser no good");
486 return OK;
487 }
488
Lajos Molnar424cfb52019-04-08 17:48:00 -0700489 MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap();
490 for (const auto &v : settings) {
491 if (!hasPrefix(v.first, "media-type-")
492 && !hasPrefix(v.first, "domain-")
493 && !hasPrefix(v.first, "variant-")) {
494 writer->addGlobalSetting(v.first.c_str(), v.second.c_str());
495 }
496 }
497
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800498 std::map<std::string, PixelFormatMap> nameToPixelFormatMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800499 for (const Traits& trait : traits) {
500 C2Component::rank_t rank = trait.rank;
501
Lajos Molnardb5751f2019-01-31 17:01:49 -0800502 // Interface must be accessible for us to list the component, and there also
503 // must be an XML entry for the codec. Codec aliases listed in the traits
504 // allow additional XML entries to be specified for each alias. These will
505 // be listed as separate codecs. If no XML entry is specified for an alias,
506 // those will be treated as an additional alias specified in the XML entry
507 // for the interface name.
508 std::vector<std::string> nameAndAliases = trait.aliases;
509 nameAndAliases.insert(nameAndAliases.begin(), trait.name);
510 for (const std::string &nameOrAlias : nameAndAliases) {
511 bool isAlias = trait.name != nameOrAlias;
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800512 std::shared_ptr<Codec2Client> client;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800513 std::shared_ptr<Codec2Client::Interface> intf =
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800514 Codec2Client::CreateInterfaceByName(nameOrAlias.c_str(), &client);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800515 if (!intf) {
516 ALOGD("could not create interface for %s'%s'",
517 isAlias ? "alias " : "",
518 nameOrAlias.c_str());
519 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800520 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800521 if (parser.getCodecMap().count(nameOrAlias) == 0) {
522 if (isAlias) {
523 std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo =
524 writer->findMediaCodecInfo(trait.name.c_str());
525 if (!baseCodecInfo) {
526 ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing",
527 nameOrAlias.c_str(),
528 trait.name.c_str());
529 } else {
530 ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this",
531 nameOrAlias.c_str());
532 // merge alias into existing codec
533 baseCodecInfo->addAlias(nameOrAlias.c_str());
534 }
535 } else {
536 ALOGD("component '%s' not found in xml", trait.name.c_str());
537 }
538 continue;
539 }
540 std::string canonName = trait.name;
541
542 // TODO: Remove this block once all codecs are enabled by default.
543 switch (option) {
544 case 0:
545 continue;
546 case 1:
547 if (hasPrefix(canonName, "c2.vda.")) {
548 break;
549 }
550 if (hasPrefix(canonName, "c2.android.")) {
551 if (trait.domain == C2Component::DOMAIN_AUDIO) {
552 rank = 1;
553 break;
554 }
555 break;
556 }
557 if (hasSuffix(canonName, ".avc.decoder") ||
558 hasSuffix(canonName, ".avc.encoder")) {
559 rank = std::numeric_limits<decltype(rank)>::max();
560 break;
561 }
562 continue;
563 case 2:
564 if (hasPrefix(canonName, "c2.vda.")) {
565 break;
566 }
567 if (hasPrefix(canonName, "c2.android.")) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800568 rank = 1;
569 break;
570 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800571 if (hasSuffix(canonName, ".avc.decoder") ||
572 hasSuffix(canonName, ".avc.encoder")) {
573 rank = std::numeric_limits<decltype(rank)>::max();
574 break;
575 }
576 continue;
577 case 3:
578 if (hasPrefix(canonName, "c2.android.")) {
579 rank = 1;
580 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800581 break;
582 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800583
Lajos Molnar424cfb52019-04-08 17:48:00 -0700584 const MediaCodecsXmlParser::CodecProperties &codec =
585 parser.getCodecMap().at(nameOrAlias);
586
587 // verify that either the codec is explicitly enabled, or one of its domains is
588 bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end();
589 if (!codecEnabled) {
590 for (const std::string &domain : codec.domainSet) {
591 const Switch enabled = isDomainEnabled(domain, settings);
592 ALOGV("codec entry '%s' is in domain '%s' that is '%s'",
593 nameOrAlias.c_str(), domain.c_str(), asString(enabled));
594 if (enabled) {
595 codecEnabled = true;
596 break;
597 }
598 }
599 }
600 // if codec has variants, also check that at least one of them is enabled
601 bool variantEnabled = codec.variantSet.empty();
602 for (const std::string &variant : codec.variantSet) {
603 const Switch enabled = isVariantExpressionEnabled(variant, settings);
604 ALOGV("codec entry '%s' has a variant '%s' that is '%s'",
605 nameOrAlias.c_str(), variant.c_str(), asString(enabled));
606 if (enabled) {
607 variantEnabled = true;
608 break;
609 }
610 }
611 if (!codecEnabled || !variantEnabled) {
612 ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str());
613 continue;
614 }
615
Lajos Molnardb5751f2019-01-31 17:01:49 -0800616 ALOGV("adding codec entry for '%s'", nameOrAlias.c_str());
617 std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
618 codecInfo->setName(nameOrAlias.c_str());
619 codecInfo->setOwner(("codec2::" + trait.owner).c_str());
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800620
Lajos Molnardb5751f2019-01-31 17:01:49 -0800621 bool encoder = trait.kind == C2Component::KIND_ENCODER;
622 typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800623
Lajos Molnardb5751f2019-01-31 17:01:49 -0800624 if (encoder) {
625 attrs |= MediaCodecInfo::kFlagIsEncoder;
626 }
627 if (trait.owner == "software") {
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800628 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800629 } else {
630 attrs |= MediaCodecInfo::kFlagIsVendor;
631 if (trait.owner == "vendor-software") {
632 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
633 } else if (codec.quirkSet.find("attribute::software-codec")
634 == codec.quirkSet.end()) {
635 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800636 }
637 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800638 codecInfo->setAttributes(attrs);
639 if (!codec.rank.empty()) {
640 uint32_t xmlRank;
641 char dummy;
642 if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) {
643 rank = xmlRank;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800644 }
645 }
Lajos Molnar424cfb52019-04-08 17:48:00 -0700646 ALOGV("rank: %u", (unsigned)rank);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800647 codecInfo->setRank(rank);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800648
Lajos Molnardb5751f2019-01-31 17:01:49 -0800649 for (const std::string &alias : codec.aliases) {
650 ALOGV("adding alias '%s'", alias.c_str());
651 codecInfo->addAlias(alias.c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800652 }
653
Lajos Molnardb5751f2019-01-31 17:01:49 -0800654 for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
655 const std::string &mediaType = typeIt->first;
Lajos Molnar424cfb52019-04-08 17:48:00 -0700656 const Switch typeEnabled = isSettingEnabled(
657 "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT());
658 const Switch domainTypeEnabled = isSettingEnabled(
659 "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"),
660 settings, Switch::ENABLED_BY_DEFAULT());
661 ALOGV("type '%s-%s' is '%s/%s'",
662 mediaType.c_str(), (encoder ? "encoder" : "decoder"),
663 asString(typeEnabled), asString(domainTypeEnabled));
664 if (!typeEnabled || !domainTypeEnabled) {
665 ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(),
666 nameOrAlias.c_str());
667 continue;
668 }
669
670 ALOGI("adding type '%s'", typeIt->first.c_str());
Lajos Molnardb5751f2019-01-31 17:01:49 -0800671 const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
672 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
673 codecInfo->addMediaType(mediaType.c_str());
Lajos Molnar424cfb52019-04-08 17:48:00 -0700674 for (const auto &v : attrMap) {
675 std::string key = v.first;
676 std::string value = v.second;
677
678 size_t variantSep = key.find(":::");
679 if (variantSep != std::string::npos) {
680 std::string variant = key.substr(0, variantSep);
681 const Switch enabled = isVariantExpressionEnabled(variant, settings);
682 ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled));
683 if (!enabled) {
684 continue;
685 }
686 key = key.substr(variantSep + 3);
687 }
688
Lajos Molnardb5751f2019-01-31 17:01:49 -0800689 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
690 int32_t intValue = 0;
691 // Ignore trailing bad characters and default to 0.
692 (void)sscanf(value.c_str(), "%d", &intValue);
693 caps->addDetail(key.c_str(), intValue);
694 } else {
695 caps->addDetail(key.c_str(), value.c_str());
696 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800697 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800698
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700699 if (!addSupportedProfileLevels(intf, caps.get(), trait, mediaType)) {
700 // TODO(b/193279646) This will get fixed in C2InterfaceHelper
701 // Some components may not advertise supported values if they use a const
702 // param for profile/level (they support only one profile). For now cover
703 // only VP8 here until it is fixed.
704 if (mediaType == MIMETYPE_VIDEO_VP8) {
705 caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
706 }
707 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800708
709 auto it = nameToPixelFormatMap.find(client->getServiceName());
710 if (it == nameToPixelFormatMap.end()) {
711 it = nameToPixelFormatMap.try_emplace(client->getServiceName()).first;
712 PixelFormatMap &pixelFormatMap = it->second;
713 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_420_888] = COLOR_FormatYUV420Flexible;
714 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_P010] = COLOR_FormatYUVP010;
715 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_1010102] = COLOR_Format32bitABGR2101010;
716 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_FP16] = COLOR_Format64bitABGRFloat;
717
718 std::shared_ptr<C2StoreFlexiblePixelFormatDescriptorsInfo> pixelFormatInfo;
719 std::vector<std::unique_ptr<C2Param>> heapParams;
720 if (client->query(
721 {},
722 {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
723 C2_MAY_BLOCK,
724 &heapParams) == C2_OK
725 && heapParams.size() == 1u) {
726 pixelFormatInfo.reset(C2StoreFlexiblePixelFormatDescriptorsInfo::From(
727 heapParams[0].release()));
728 }
729 if (pixelFormatInfo && *pixelFormatInfo) {
730 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
731 C2FlexiblePixelFormatDescriptorStruct &desc =
732 pixelFormatInfo->m.values[i];
733 std::optional<int32_t> colorFormat = findFrameworkColorFormat(desc);
734 if (colorFormat) {
735 pixelFormatMap[desc.pixelFormat] = *colorFormat;
736 }
737 }
738 }
739 }
740 addSupportedColorFormats(
741 intf, caps.get(), trait, mediaType, it->second);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800742 }
743 }
744 }
745 return OK;
746}
747
748} // namespace android
749
750extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
751 return new android::Codec2InfoBuilder;
752}