blob: 8dce7892de7e832f4495ef5a24ac27e7b5b19d07 [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
Arun Johnsonabfac482024-02-22 07:17:20 +000023#include <android_media_codec.h>
24
Pawin Vongmasa36653902018-11-15 00:10:25 -080025#include <C2Component.h>
26#include <C2Config.h>
27#include <C2Debug.h>
28#include <C2PlatformSupport.h>
29#include <Codec2Mapper.h>
30
31#include <OMX_Audio.h>
32#include <OMX_AudioExt.h>
33#include <OMX_IndexExt.h>
34#include <OMX_Types.h>
35#include <OMX_Video.h>
36#include <OMX_VideoExt.h>
37#include <OMX_AsString.h>
Harish Mahendrakar2c0fc8f2022-05-24 15:48:34 -070038#include <SurfaceFlingerProperties.sysprop.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080039
40#include <android/hardware/media/omx/1.0/IOmx.h>
41#include <android/hardware/media/omx/1.0/IOmxObserver.h>
42#include <android/hardware/media/omx/1.0/IOmxNode.h>
43#include <android/hardware/media/omx/1.0/types.h>
44
45#include <android-base/properties.h>
46#include <codec2/hidl/client.h>
47#include <cutils/native_handle.h>
48#include <media/omx/1.0/WOmxNode.h>
Pawin Vongmasa1f213362019-01-24 06:59:16 -080049#include <media/stagefright/foundation/ALookup.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080050#include <media/stagefright/foundation/MediaDefs.h>
51#include <media/stagefright/omx/OMXUtils.h>
52#include <media/stagefright/xmlparser/MediaCodecsXmlParser.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070053#include <media/stagefright/Codec2InfoBuilder.h>
54#include <media/stagefright/MediaCodecConstants.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080055
56namespace android {
57
58using Traits = C2Component::Traits;
59
Wonsik Kimf87cbc42022-01-24 09:49:12 -080060// HAL pixel format -> framework color format
61typedef std::map<uint32_t, int32_t> PixelFormatMap;
62
Pawin Vongmasa36653902018-11-15 00:10:25 -080063namespace /* unnamed */ {
64
65bool hasPrefix(const std::string& s, const char* prefix) {
66 size_t prefixLen = strlen(prefix);
67 return s.compare(0, prefixLen, prefix) == 0;
68}
69
70bool hasSuffix(const std::string& s, const char* suffix) {
71 size_t suffixLen = strlen(suffix);
72 return suffixLen > s.size() ? false :
73 s.compare(s.size() - suffixLen, suffixLen, suffix) == 0;
74}
75
Wonsik Kimf87cbc42022-01-24 09:49:12 -080076std::optional<int32_t> findFrameworkColorFormat(
77 const C2FlexiblePixelFormatDescriptorStruct &desc) {
78 switch (desc.bitDepth) {
79 case 8u:
80 if (desc.layout == C2Color::PLANAR_PACKED
81 || desc.layout == C2Color::SEMIPLANAR_PACKED) {
82 return COLOR_FormatYUV420Flexible;
83 }
84 break;
85 case 10u:
86 if (desc.layout == C2Color::SEMIPLANAR_PACKED) {
87 return COLOR_FormatYUVP010;
88 }
89 break;
90 default:
91 break;
92 }
93 return std::nullopt;
94}
95
Lajos Molnar59f4a4e2021-07-09 18:23:54 -070096// returns true if component advertised supported profile level(s)
97bool addSupportedProfileLevels(
Lajos Molnardb5751f2019-01-31 17:01:49 -080098 std::shared_ptr<Codec2Client::Interface> intf,
99 MediaCodecInfo::CapabilitiesWriter *caps,
100 const Traits& trait, const std::string &mediaType) {
101 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
102 C2Mapper::GetProfileLevelMapper(trait.mediaType);
103 // if we don't know the media type, pass through all values unmapped
Pawin Vongmasa36653902018-11-15 00:10:25 -0800104
Lajos Molnardb5751f2019-01-31 17:01:49 -0800105 // TODO: we cannot find levels that are local 'maxima' without knowing the coding
106 // e.g. H.263 level 45 and level 30 could be two values for highest level as
107 // they don't include one another. For now we use the last supported value.
108 bool encoder = trait.kind == C2Component::KIND_ENCODER;
109 C2StreamProfileLevelInfo pl(encoder /* output */, 0u);
110 std::vector<C2FieldSupportedValuesQuery> profileQuery = {
111 C2FieldSupportedValuesQuery::Possible(C2ParamField(&pl, &pl.profile))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800112 };
113
Lajos Molnardb5751f2019-01-31 17:01:49 -0800114 c2_status_t err = intf->querySupportedValues(profileQuery, C2_DONT_BLOCK);
115 ALOGV("query supported profiles -> %s | %s", asString(err), asString(profileQuery[0].status));
116 if (err != C2_OK || profileQuery[0].status != C2_OK) {
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700117 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800118 }
119
Lajos Molnardb5751f2019-01-31 17:01:49 -0800120 // we only handle enumerated values
121 if (profileQuery[0].values.type != C2FieldSupportedValues::VALUES) {
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700122 return false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800123 }
124
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700125 // determine if codec supports HDR; imply 10-bit support
Lajos Molnardb5751f2019-01-31 17:01:49 -0800126 bool supportsHdr = false;
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700127 // determine if codec supports HDR10Plus; imply 10-bit support
Lajos Molnardb5751f2019-01-31 17:01:49 -0800128 bool supportsHdr10Plus = false;
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700129 // determine if codec supports 10-bit format
130 bool supports10Bit = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800131
Lajos Molnardb5751f2019-01-31 17:01:49 -0800132 std::vector<std::shared_ptr<C2ParamDescriptor>> paramDescs;
133 c2_status_t err1 = intf->querySupportedParams(&paramDescs);
134 if (err1 == C2_OK) {
135 for (const std::shared_ptr<C2ParamDescriptor> &desc : paramDescs) {
Lajos Molnar739fe732021-02-07 13:06:10 -0800136 C2Param::Type type = desc->index();
137 // only consider supported parameters on raw ports
138 if (!(encoder ? type.forInput() : type.forOutput())) {
139 continue;
140 }
141 switch (type.coreIndex()) {
Taehwan Kim2d222b82022-05-12 14:19:26 +0900142 case C2StreamHdrDynamicMetadataInfo::CORE_INDEX:
143 [[fallthrough]];
144 case C2StreamHdr10PlusInfo::CORE_INDEX: // will be deprecated
Lajos Molnardb5751f2019-01-31 17:01:49 -0800145 supportsHdr10Plus = true;
146 break;
Lajos Molnar739fe732021-02-07 13:06:10 -0800147 case C2StreamHdrStaticInfo::CORE_INDEX:
Lajos Molnardb5751f2019-01-31 17:01:49 -0800148 supportsHdr = true;
149 break;
150 default:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800151 break;
152 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800153 }
154 }
155
Lajos Molnarb857cde2022-05-25 10:20:37 -0700156 // VP9 does not support HDR metadata in the bitstream and static metadata
157 // can always be carried by the framework. (The framework does not propagate
158 // dynamic metadata as that needs to be frame accurate.)
Lajos Molnardb5751f2019-01-31 17:01:49 -0800159 supportsHdr |= (mediaType == MIMETYPE_VIDEO_VP9);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800160
Wonsik Kima2d68d92022-06-23 14:56:51 -0700161 // HDR support implies 10-bit support. AV1 codecs are also required to
162 // support 10-bit per CDD.
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700163 // TODO: directly check this from the component interface
Wonsik Kima2d68d92022-06-23 14:56:51 -0700164 supports10Bit = (supportsHdr || supportsHdr10Plus) || (mediaType == MIMETYPE_VIDEO_AV1);
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700165
Harish Mahendrakar2c0fc8f2022-05-24 15:48:34 -0700166 // If the device doesn't support HDR display, then no codec on the device
167 // can advertise support for HDR profiles.
168 // Default to true to maintain backward compatibility
169 auto ret = sysprop::SurfaceFlingerProperties::has_HDR_display();
170 bool hasHDRDisplay = ret.has_value() ? *ret : true;
171
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700172 bool added = false;
173
Lajos Molnardb5751f2019-01-31 17:01:49 -0800174 for (C2Value::Primitive profile : profileQuery[0].values.values) {
175 pl.profile = (C2Config::profile_t)profile.ref<uint32_t>();
176 std::vector<std::unique_ptr<C2SettingResult>> failures;
177 err = intf->config({&pl}, C2_DONT_BLOCK, &failures);
178 ALOGV("set profile to %u -> %s", pl.profile, asString(err));
179 std::vector<C2FieldSupportedValuesQuery> levelQuery = {
180 C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level))
181 };
182 err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK);
183 ALOGV("query supported levels -> %s | %s", asString(err), asString(levelQuery[0].status));
184 if (err != C2_OK || levelQuery[0].status != C2_OK
185 || levelQuery[0].values.type != C2FieldSupportedValues::VALUES
186 || levelQuery[0].values.values.size() == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800187 continue;
188 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800189
190 C2Value::Primitive level = levelQuery[0].values.values.back();
191 pl.level = (C2Config::level_t)level.ref<uint32_t>();
192 ALOGV("supporting level: %u", pl.level);
193 int32_t sdkProfile, sdkLevel;
194 if (mapper && mapper->mapProfile(pl.profile, &sdkProfile)
195 && mapper->mapLevel(pl.level, &sdkLevel)) {
196 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
Harish Mahendrakar2c0fc8f2022-05-24 15:48:34 -0700197 // also list HDR profiles if component supports HDR and device has HDR display
198 if (supportsHdr && hasHDRDisplay) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800199 auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper(trait.mediaType);
200 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
201 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
202 }
203 if (supportsHdr10Plus) {
204 hdrMapper = C2Mapper::GetHdrProfileLevelMapper(
205 trait.mediaType, true /*isHdr10Plus*/);
206 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
207 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
208 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800209 }
210 }
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700211 if (supports10Bit) {
212 auto bitnessMapper = C2Mapper::GetBitDepthProfileLevelMapper(trait.mediaType, 10);
213 if (bitnessMapper && bitnessMapper->mapProfile(pl.profile, &sdkProfile)) {
214 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
215 }
216 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800217 } else if (!mapper) {
218 caps->addProfileLevel(pl.profile, pl.level);
219 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700220 added = true;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800221
222 // for H.263 also advertise the second highest level if the
223 // codec supports level 45, as level 45 only covers level 10
224 // TODO: move this to some form of a setting so it does not
225 // have to be here
226 if (mediaType == MIMETYPE_VIDEO_H263) {
227 C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED;
228 for (C2Value::Primitive v : levelQuery[0].values.values) {
229 C2Config::level_t level = (C2Config::level_t)v.ref<uint32_t>();
230 if (level < C2Config::LEVEL_H263_45 && level > nextLevel) {
231 nextLevel = level;
232 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800233 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800234 if (nextLevel != C2Config::LEVEL_UNUSED
235 && nextLevel != pl.level
236 && mapper
237 && mapper->mapProfile(pl.profile, &sdkProfile)
238 && mapper->mapLevel(nextLevel, &sdkLevel)) {
239 caps->addProfileLevel(
240 (uint32_t)sdkProfile, (uint32_t)sdkLevel);
241 }
242 }
243 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700244 return added;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800245}
246
247void addSupportedColorFormats(
248 std::shared_ptr<Codec2Client::Interface> intf,
249 MediaCodecInfo::CapabilitiesWriter *caps,
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800250 const Traits& trait, const std::string &mediaType,
251 const PixelFormatMap &pixelFormatMap) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800252 // TODO: get this from intf() as well, but how do we map them to
253 // MediaCodec color formats?
254 bool encoder = trait.kind == C2Component::KIND_ENCODER;
Wonsik Kim16223262019-06-14 14:40:57 -0700255 if (mediaType.find("video") != std::string::npos
256 || mediaType.find("image") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800257
258 std::vector<C2FieldSupportedValuesQuery> query;
259 if (encoder) {
260 C2StreamPixelFormatInfo::input pixelFormat;
261 query.push_back(C2FieldSupportedValuesQuery::Possible(
262 C2ParamField::Make(pixelFormat, pixelFormat.value)));
263 } else {
264 C2StreamPixelFormatInfo::output pixelFormat;
265 query.push_back(C2FieldSupportedValuesQuery::Possible(
266 C2ParamField::Make(pixelFormat, pixelFormat.value)));
267 }
268 std::list<int32_t> supportedColorFormats;
269 if (intf->querySupportedValues(query, C2_DONT_BLOCK) == C2_OK) {
270 if (query[0].status == C2_OK) {
271 const C2FieldSupportedValues &fsv = query[0].values;
272 if (fsv.type == C2FieldSupportedValues::VALUES) {
273 for (C2Value::Primitive value : fsv.values) {
274 auto it = pixelFormatMap.find(value.u32);
275 if (it != pixelFormatMap.end()) {
276 auto it2 = std::find(
277 supportedColorFormats.begin(),
278 supportedColorFormats.end(),
279 it->second);
280 if (it2 == supportedColorFormats.end()) {
281 supportedColorFormats.push_back(it->second);
282 }
283 }
284 }
285 }
286 }
287 }
288 auto addDefaultColorFormat = [caps, &supportedColorFormats](int32_t colorFormat) {
289 caps->addColorFormat(colorFormat);
290 auto it = std::find(
291 supportedColorFormats.begin(), supportedColorFormats.end(), colorFormat);
292 if (it != supportedColorFormats.end()) {
293 supportedColorFormats.erase(it);
294 }
295 };
296
My Name298764f2022-03-25 15:07:51 -0700297 // The color format is ordered by preference. The intention here is to advertise:
298 // c2.android.* codecs: YUV420s, Surface, <the rest>
299 // all other codecs: Surface, YUV420s, <the rest>
300 // TODO: get this preference via Codec2 API
301
Lajos Molnardb5751f2019-01-31 17:01:49 -0800302 // vendor video codecs prefer opaque format
303 if (trait.name.find("android") == std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800304 addDefaultColorFormat(COLOR_FormatSurface);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800305 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800306 addDefaultColorFormat(COLOR_FormatYUV420Flexible);
307 addDefaultColorFormat(COLOR_FormatYUV420Planar);
308 addDefaultColorFormat(COLOR_FormatYUV420SemiPlanar);
309 addDefaultColorFormat(COLOR_FormatYUV420PackedPlanar);
310 addDefaultColorFormat(COLOR_FormatYUV420PackedSemiPlanar);
My Name298764f2022-03-25 15:07:51 -0700311 // Android video codecs prefer CPU-readable formats
312 if (trait.name.find("android") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800313 addDefaultColorFormat(COLOR_FormatSurface);
314 }
Wonsik Kim1e16eb32022-06-03 16:32:19 -0700315
316 static const int kVendorSdkVersion = ::android::base::GetIntProperty(
317 "ro.vendor.build.version.sdk", android_get_device_api_level());
318 if (kVendorSdkVersion >= __ANDROID_API_T__) {
319 for (int32_t colorFormat : supportedColorFormats) {
320 caps->addColorFormat(colorFormat);
321 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800322 }
323 }
324}
325
Lajos Molnar424cfb52019-04-08 17:48:00 -0700326class Switch {
327 enum Flags : uint8_t {
328 // flags
329 IS_ENABLED = (1 << 0),
330 BY_DEFAULT = (1 << 1),
331 };
332
333 constexpr Switch(uint8_t flags) : mFlags(flags) {}
334
335 uint8_t mFlags;
336
337public:
338 // have to create class due to this bool conversion operator...
339 constexpr operator bool() const {
340 return mFlags & IS_ENABLED;
341 }
342
343 constexpr Switch operator!() const {
344 return Switch(mFlags ^ IS_ENABLED);
345 }
346
347 static constexpr Switch DISABLED() { return 0; };
348 static constexpr Switch ENABLED() { return IS_ENABLED; };
349 static constexpr Switch DISABLED_BY_DEFAULT() { return BY_DEFAULT; };
350 static constexpr Switch ENABLED_BY_DEFAULT() { return IS_ENABLED | BY_DEFAULT; };
351
352 const char *toString(const char *def = "??") const {
353 switch (mFlags) {
354 case 0: return "0";
355 case IS_ENABLED: return "1";
356 case BY_DEFAULT: return "(0)";
357 case IS_ENABLED | BY_DEFAULT: return "(1)";
358 default: return def;
359 }
360 }
361
362};
363
364const char *asString(const Switch &s, const char *def = "??") {
365 return s.toString(def);
366}
367
368Switch isSettingEnabled(
369 std::string setting, const MediaCodecsXmlParser::AttributeMap &settings,
370 Switch def = Switch::DISABLED_BY_DEFAULT()) {
371 const auto enablement = settings.find(setting);
372 if (enablement == settings.end()) {
373 return def;
374 }
375 return enablement->second == "1" ? Switch::ENABLED() : Switch::DISABLED();
376}
377
378Switch isVariantEnabled(
379 std::string variant, const MediaCodecsXmlParser::AttributeMap &settings) {
380 return isSettingEnabled("variant-" + variant, settings);
381}
382
383Switch isVariantExpressionEnabled(
384 std::string exp, const MediaCodecsXmlParser::AttributeMap &settings) {
385 if (!exp.empty() && exp.at(0) == '!') {
386 return !isVariantEnabled(exp.substr(1, exp.size() - 1), settings);
387 }
388 return isVariantEnabled(exp, settings);
389}
390
391Switch isDomainEnabled(
392 std::string domain, const MediaCodecsXmlParser::AttributeMap &settings) {
393 return isSettingEnabled("domain-" + domain, settings);
394}
395
Pawin Vongmasa36653902018-11-15 00:10:25 -0800396} // unnamed namespace
397
398status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
399 // TODO: Remove run-time configurations once all codecs are working
400 // properly. (Assume "full" behavior eventually.)
401 //
402 // debug.stagefright.ccodec supports 5 values.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800403 // 0 - No Codec 2.0 components are available.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800404 // 1 - Audio decoders and encoders with prefix "c2.android." are available
405 // and ranked first.
406 // All other components with prefix "c2.android." are available with
407 // their normal ranks.
408 // Components with prefix "c2.vda." are available with their normal
409 // ranks.
410 // All other components with suffix ".avc.decoder" or ".avc.encoder"
411 // are available but ranked last.
412 // 2 - Components with prefix "c2.android." are available and ranked
413 // first.
414 // Components with prefix "c2.vda." are available with their normal
415 // ranks.
416 // All other components with suffix ".avc.decoder" or ".avc.encoder"
417 // are available but ranked last.
418 // 3 - Components with prefix "c2.android." are available and ranked
419 // first.
420 // All other components are available with their normal ranks.
421 // 4 - All components are available with their normal ranks.
422 //
423 // The default value (boot time) is 1.
424 //
425 // Note: Currently, OMX components have default rank 0x100, while all
426 // Codec2.0 software components have default rank 0x200.
Lajos Molnar8635fc82019-05-17 17:35:10 +0000427 int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 4);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800428
429 // Obtain Codec2Client
430 std::vector<Traits> traits = Codec2Client::ListComponents();
431
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800432 // parse APEX XML first, followed by vendor XML.
433 // Note: APEX XML names do not depend on ro.media.xml_variant.* properties.
Lajos Molnarda666892019-04-08 17:25:34 -0700434 MediaCodecsXmlParser parser;
435 parser.parseXmlFilesInSearchDirs(
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800436 { "media_codecs.xml", "media_codecs_performance.xml" },
Lajos Molnar424cfb52019-04-08 17:48:00 -0700437 { "/apex/com.android.media.swcodec/etc" });
438
439 // TODO: remove these c2-specific files once product moved to default file names
440 parser.parseXmlFilesInSearchDirs(
Lajos Molnarda666892019-04-08 17:25:34 -0700441 { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
Lajos Molnar424cfb52019-04-08 17:48:00 -0700442
443 // parse default XML files
444 parser.parseXmlFilesInSearchDirs();
445
Ray Essick8c4e9c72021-03-15 15:25:21 -0700446 // The mainline modules for media may optionally include some codec shaping information.
447 // Based on vendor partition SDK, and the brand/product/device information
448 // (expect to be empty in almost always)
449 //
450 {
451 // get build info so we know what file to search
452 // ro.vendor.build.fingerprint
453 std::string fingerprint = base::GetProperty("ro.vendor.build.fingerprint",
454 "brand/product/device:");
455 ALOGV("property_get for ro.vendor.build.fingerprint == '%s'", fingerprint.c_str());
456
457 // ro.vendor.build.version.sdk
458 std::string sdk = base::GetProperty("ro.vendor.build.version.sdk", "0");
459 ALOGV("property_get for ro.vendor.build.version.sdk == '%s'", sdk.c_str());
460
461 std::string brand;
462 std::string product;
463 std::string device;
464 size_t pos1;
465 pos1 = fingerprint.find('/');
466 if (pos1 != std::string::npos) {
467 brand = fingerprint.substr(0, pos1);
468 size_t pos2 = fingerprint.find('/', pos1+1);
469 if (pos2 != std::string::npos) {
470 product = fingerprint.substr(pos1+1, pos2 - pos1 - 1);
471 size_t pos3 = fingerprint.find('/', pos2+1);
472 if (pos3 != std::string::npos) {
473 device = fingerprint.substr(pos2+1, pos3 - pos2 - 1);
474 size_t pos4 = device.find(':');
475 if (pos4 != std::string::npos) {
476 device.resize(pos4);
477 }
478 }
479 }
480 }
481
482 ALOGV("parsed: sdk '%s' brand '%s' product '%s' device '%s'",
483 sdk.c_str(), brand.c_str(), product.c_str(), device.c_str());
484
485 std::string base = "/apex/com.android.media/etc/formatshaper";
486
487 // looking in these directories within the apex
488 const std::vector<std::string> modulePathnames = {
489 base + "/" + sdk + "/" + brand + "/" + product + "/" + device,
490 base + "/" + sdk + "/" + brand + "/" + product,
491 base + "/" + sdk + "/" + brand,
492 base + "/" + sdk,
493 base
494 };
495
496 parser.parseXmlFilesInSearchDirs( { "media_codecs_shaping.xml" }, modulePathnames);
497 }
498
Pawin Vongmasa36653902018-11-15 00:10:25 -0800499 if (parser.getParsingStatus() != OK) {
500 ALOGD("XML parser no good");
501 return OK;
502 }
503
Lajos Molnar424cfb52019-04-08 17:48:00 -0700504 MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap();
505 for (const auto &v : settings) {
506 if (!hasPrefix(v.first, "media-type-")
507 && !hasPrefix(v.first, "domain-")
508 && !hasPrefix(v.first, "variant-")) {
509 writer->addGlobalSetting(v.first.c_str(), v.second.c_str());
510 }
511 }
512
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800513 std::map<std::string, PixelFormatMap> nameToPixelFormatMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800514 for (const Traits& trait : traits) {
515 C2Component::rank_t rank = trait.rank;
516
Lajos Molnardb5751f2019-01-31 17:01:49 -0800517 // Interface must be accessible for us to list the component, and there also
518 // must be an XML entry for the codec. Codec aliases listed in the traits
519 // allow additional XML entries to be specified for each alias. These will
520 // be listed as separate codecs. If no XML entry is specified for an alias,
521 // those will be treated as an additional alias specified in the XML entry
522 // for the interface name.
523 std::vector<std::string> nameAndAliases = trait.aliases;
524 nameAndAliases.insert(nameAndAliases.begin(), trait.name);
525 for (const std::string &nameOrAlias : nameAndAliases) {
526 bool isAlias = trait.name != nameOrAlias;
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800527 std::shared_ptr<Codec2Client> client;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800528 std::shared_ptr<Codec2Client::Interface> intf =
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800529 Codec2Client::CreateInterfaceByName(nameOrAlias.c_str(), &client);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800530 if (!intf) {
531 ALOGD("could not create interface for %s'%s'",
532 isAlias ? "alias " : "",
533 nameOrAlias.c_str());
534 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800535 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800536 if (parser.getCodecMap().count(nameOrAlias) == 0) {
537 if (isAlias) {
538 std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo =
539 writer->findMediaCodecInfo(trait.name.c_str());
540 if (!baseCodecInfo) {
541 ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing",
542 nameOrAlias.c_str(),
543 trait.name.c_str());
544 } else {
545 ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this",
546 nameOrAlias.c_str());
547 // merge alias into existing codec
548 baseCodecInfo->addAlias(nameOrAlias.c_str());
549 }
550 } else {
551 ALOGD("component '%s' not found in xml", trait.name.c_str());
552 }
553 continue;
554 }
555 std::string canonName = trait.name;
556
557 // TODO: Remove this block once all codecs are enabled by default.
558 switch (option) {
559 case 0:
560 continue;
561 case 1:
562 if (hasPrefix(canonName, "c2.vda.")) {
563 break;
564 }
565 if (hasPrefix(canonName, "c2.android.")) {
566 if (trait.domain == C2Component::DOMAIN_AUDIO) {
567 rank = 1;
568 break;
569 }
570 break;
571 }
572 if (hasSuffix(canonName, ".avc.decoder") ||
573 hasSuffix(canonName, ".avc.encoder")) {
574 rank = std::numeric_limits<decltype(rank)>::max();
575 break;
576 }
577 continue;
578 case 2:
579 if (hasPrefix(canonName, "c2.vda.")) {
580 break;
581 }
582 if (hasPrefix(canonName, "c2.android.")) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800583 rank = 1;
584 break;
585 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800586 if (hasSuffix(canonName, ".avc.decoder") ||
587 hasSuffix(canonName, ".avc.encoder")) {
588 rank = std::numeric_limits<decltype(rank)>::max();
589 break;
590 }
591 continue;
592 case 3:
593 if (hasPrefix(canonName, "c2.android.")) {
594 rank = 1;
595 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800596 break;
597 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800598
Lajos Molnar424cfb52019-04-08 17:48:00 -0700599 const MediaCodecsXmlParser::CodecProperties &codec =
600 parser.getCodecMap().at(nameOrAlias);
601
602 // verify that either the codec is explicitly enabled, or one of its domains is
603 bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end();
604 if (!codecEnabled) {
605 for (const std::string &domain : codec.domainSet) {
606 const Switch enabled = isDomainEnabled(domain, settings);
607 ALOGV("codec entry '%s' is in domain '%s' that is '%s'",
608 nameOrAlias.c_str(), domain.c_str(), asString(enabled));
609 if (enabled) {
610 codecEnabled = true;
611 break;
612 }
613 }
614 }
615 // if codec has variants, also check that at least one of them is enabled
616 bool variantEnabled = codec.variantSet.empty();
617 for (const std::string &variant : codec.variantSet) {
618 const Switch enabled = isVariantExpressionEnabled(variant, settings);
619 ALOGV("codec entry '%s' has a variant '%s' that is '%s'",
620 nameOrAlias.c_str(), variant.c_str(), asString(enabled));
621 if (enabled) {
622 variantEnabled = true;
623 break;
624 }
625 }
626 if (!codecEnabled || !variantEnabled) {
627 ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str());
628 continue;
629 }
630
Lajos Molnardb5751f2019-01-31 17:01:49 -0800631 ALOGV("adding codec entry for '%s'", nameOrAlias.c_str());
632 std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
633 codecInfo->setName(nameOrAlias.c_str());
634 codecInfo->setOwner(("codec2::" + trait.owner).c_str());
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800635
Lajos Molnardb5751f2019-01-31 17:01:49 -0800636 bool encoder = trait.kind == C2Component::KIND_ENCODER;
637 typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800638
Lajos Molnardb5751f2019-01-31 17:01:49 -0800639 if (encoder) {
640 attrs |= MediaCodecInfo::kFlagIsEncoder;
641 }
642 if (trait.owner == "software") {
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800643 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800644 } else {
645 attrs |= MediaCodecInfo::kFlagIsVendor;
646 if (trait.owner == "vendor-software") {
647 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
648 } else if (codec.quirkSet.find("attribute::software-codec")
649 == codec.quirkSet.end()) {
650 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800651 }
652 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800653 codecInfo->setAttributes(attrs);
654 if (!codec.rank.empty()) {
655 uint32_t xmlRank;
656 char dummy;
657 if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) {
658 rank = xmlRank;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800659 }
660 }
Lajos Molnar424cfb52019-04-08 17:48:00 -0700661 ALOGV("rank: %u", (unsigned)rank);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800662 codecInfo->setRank(rank);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800663
Lajos Molnardb5751f2019-01-31 17:01:49 -0800664 for (const std::string &alias : codec.aliases) {
665 ALOGV("adding alias '%s'", alias.c_str());
666 codecInfo->addAlias(alias.c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800667 }
668
Lajos Molnardb5751f2019-01-31 17:01:49 -0800669 for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
670 const std::string &mediaType = typeIt->first;
Lajos Molnar424cfb52019-04-08 17:48:00 -0700671 const Switch typeEnabled = isSettingEnabled(
672 "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT());
673 const Switch domainTypeEnabled = isSettingEnabled(
674 "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"),
675 settings, Switch::ENABLED_BY_DEFAULT());
676 ALOGV("type '%s-%s' is '%s/%s'",
677 mediaType.c_str(), (encoder ? "encoder" : "decoder"),
678 asString(typeEnabled), asString(domainTypeEnabled));
679 if (!typeEnabled || !domainTypeEnabled) {
680 ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(),
681 nameOrAlias.c_str());
682 continue;
683 }
684
685 ALOGI("adding type '%s'", typeIt->first.c_str());
Lajos Molnardb5751f2019-01-31 17:01:49 -0800686 const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
687 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
688 codecInfo->addMediaType(mediaType.c_str());
Lajos Molnar424cfb52019-04-08 17:48:00 -0700689 for (const auto &v : attrMap) {
690 std::string key = v.first;
691 std::string value = v.second;
692
693 size_t variantSep = key.find(":::");
694 if (variantSep != std::string::npos) {
695 std::string variant = key.substr(0, variantSep);
696 const Switch enabled = isVariantExpressionEnabled(variant, settings);
697 ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled));
698 if (!enabled) {
699 continue;
700 }
701 key = key.substr(variantSep + 3);
702 }
703
Lajos Molnardb5751f2019-01-31 17:01:49 -0800704 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
705 int32_t intValue = 0;
706 // Ignore trailing bad characters and default to 0.
707 (void)sscanf(value.c_str(), "%d", &intValue);
708 caps->addDetail(key.c_str(), intValue);
709 } else {
710 caps->addDetail(key.c_str(), value.c_str());
711 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800712 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800713
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700714 if (!addSupportedProfileLevels(intf, caps.get(), trait, mediaType)) {
715 // TODO(b/193279646) This will get fixed in C2InterfaceHelper
716 // Some components may not advertise supported values if they use a const
717 // param for profile/level (they support only one profile). For now cover
718 // only VP8 here until it is fixed.
719 if (mediaType == MIMETYPE_VIDEO_VP8) {
720 caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
721 }
722 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800723
724 auto it = nameToPixelFormatMap.find(client->getServiceName());
725 if (it == nameToPixelFormatMap.end()) {
726 it = nameToPixelFormatMap.try_emplace(client->getServiceName()).first;
727 PixelFormatMap &pixelFormatMap = it->second;
728 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_420_888] = COLOR_FormatYUV420Flexible;
729 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_P010] = COLOR_FormatYUVP010;
730 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_1010102] = COLOR_Format32bitABGR2101010;
731 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_FP16] = COLOR_Format64bitABGRFloat;
732
733 std::shared_ptr<C2StoreFlexiblePixelFormatDescriptorsInfo> pixelFormatInfo;
734 std::vector<std::unique_ptr<C2Param>> heapParams;
735 if (client->query(
736 {},
737 {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
738 C2_MAY_BLOCK,
739 &heapParams) == C2_OK
740 && heapParams.size() == 1u) {
741 pixelFormatInfo.reset(C2StoreFlexiblePixelFormatDescriptorsInfo::From(
742 heapParams[0].release()));
743 }
744 if (pixelFormatInfo && *pixelFormatInfo) {
745 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
746 C2FlexiblePixelFormatDescriptorStruct &desc =
747 pixelFormatInfo->m.values[i];
748 std::optional<int32_t> colorFormat = findFrameworkColorFormat(desc);
749 if (colorFormat) {
750 pixelFormatMap[desc.pixelFormat] = *colorFormat;
751 }
752 }
753 }
754 }
755 addSupportedColorFormats(
756 intf, caps.get(), trait, mediaType, it->second);
Arun Johnsonabfac482024-02-22 07:17:20 +0000757
758 if (android::media::codec::provider_->large_audio_frame_finish()) {
759 // Adding feature-multiple-frames when C2LargeFrame param is present
760 if (trait.domain == C2Component::DOMAIN_AUDIO) {
761 std::vector<std::shared_ptr<C2ParamDescriptor>> params;
762 c2_status_t err = intf->querySupportedParams(&params);
763 if (err == C2_OK) {
764 for (const auto &paramDesc : params) {
765 if (C2LargeFrame::output::PARAM_TYPE == paramDesc->index()) {
766 std::string featureMultipleFrames =
767 std::string(KEY_FEATURE_) + FEATURE_MultipleFrames;
768 caps->addDetail(featureMultipleFrames.c_str(), 0);
769 break;
770 }
771 }
772 }
773 }
774 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800775 }
776 }
777 }
778 return OK;
779}
780
781} // namespace android
782
783extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
784 return new android::Codec2InfoBuilder;
785}