blob: 7e0200906a1f4807a7af57f90bc69ea9242ce711 [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
Chong Zhang0702d1f2019-08-15 11:45:36 -0700153 // For VP9/AV1, the static info is always propagated by framework.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800154 supportsHdr |= (mediaType == MIMETYPE_VIDEO_VP9);
Chong Zhang0702d1f2019-08-15 11:45:36 -0700155 supportsHdr |= (mediaType == MIMETYPE_VIDEO_AV1);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800156
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700157 // HDR support implies 10-bit support.
158 // TODO: directly check this from the component interface
159 supports10Bit = (supportsHdr || supportsHdr10Plus);
160
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700161 bool added = false;
162
Lajos Molnardb5751f2019-01-31 17:01:49 -0800163 for (C2Value::Primitive profile : profileQuery[0].values.values) {
164 pl.profile = (C2Config::profile_t)profile.ref<uint32_t>();
165 std::vector<std::unique_ptr<C2SettingResult>> failures;
166 err = intf->config({&pl}, C2_DONT_BLOCK, &failures);
167 ALOGV("set profile to %u -> %s", pl.profile, asString(err));
168 std::vector<C2FieldSupportedValuesQuery> levelQuery = {
169 C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level))
170 };
171 err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK);
172 ALOGV("query supported levels -> %s | %s", asString(err), asString(levelQuery[0].status));
173 if (err != C2_OK || levelQuery[0].status != C2_OK
174 || levelQuery[0].values.type != C2FieldSupportedValues::VALUES
175 || levelQuery[0].values.values.size() == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800176 continue;
177 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800178
179 C2Value::Primitive level = levelQuery[0].values.values.back();
180 pl.level = (C2Config::level_t)level.ref<uint32_t>();
181 ALOGV("supporting level: %u", pl.level);
182 int32_t sdkProfile, sdkLevel;
183 if (mapper && mapper->mapProfile(pl.profile, &sdkProfile)
184 && mapper->mapLevel(pl.level, &sdkLevel)) {
185 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
186 // also list HDR profiles if component supports HDR
187 if (supportsHdr) {
188 auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper(trait.mediaType);
189 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
190 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
191 }
192 if (supportsHdr10Plus) {
193 hdrMapper = C2Mapper::GetHdrProfileLevelMapper(
194 trait.mediaType, true /*isHdr10Plus*/);
195 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
196 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
197 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800198 }
199 }
Wonsik Kim4b7bb0a2021-11-03 11:43:48 -0700200 if (supports10Bit) {
201 auto bitnessMapper = C2Mapper::GetBitDepthProfileLevelMapper(trait.mediaType, 10);
202 if (bitnessMapper && bitnessMapper->mapProfile(pl.profile, &sdkProfile)) {
203 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
204 }
205 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800206 } else if (!mapper) {
207 caps->addProfileLevel(pl.profile, pl.level);
208 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700209 added = true;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800210
211 // for H.263 also advertise the second highest level if the
212 // codec supports level 45, as level 45 only covers level 10
213 // TODO: move this to some form of a setting so it does not
214 // have to be here
215 if (mediaType == MIMETYPE_VIDEO_H263) {
216 C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED;
217 for (C2Value::Primitive v : levelQuery[0].values.values) {
218 C2Config::level_t level = (C2Config::level_t)v.ref<uint32_t>();
219 if (level < C2Config::LEVEL_H263_45 && level > nextLevel) {
220 nextLevel = level;
221 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800222 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800223 if (nextLevel != C2Config::LEVEL_UNUSED
224 && nextLevel != pl.level
225 && mapper
226 && mapper->mapProfile(pl.profile, &sdkProfile)
227 && mapper->mapLevel(nextLevel, &sdkLevel)) {
228 caps->addProfileLevel(
229 (uint32_t)sdkProfile, (uint32_t)sdkLevel);
230 }
231 }
232 }
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700233 return added;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800234}
235
236void addSupportedColorFormats(
237 std::shared_ptr<Codec2Client::Interface> intf,
238 MediaCodecInfo::CapabilitiesWriter *caps,
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800239 const Traits& trait, const std::string &mediaType,
240 const PixelFormatMap &pixelFormatMap) {
Lajos Molnardb5751f2019-01-31 17:01:49 -0800241 // TODO: get this from intf() as well, but how do we map them to
242 // MediaCodec color formats?
243 bool encoder = trait.kind == C2Component::KIND_ENCODER;
Wonsik Kim16223262019-06-14 14:40:57 -0700244 if (mediaType.find("video") != std::string::npos
245 || mediaType.find("image") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800246
247 std::vector<C2FieldSupportedValuesQuery> query;
248 if (encoder) {
249 C2StreamPixelFormatInfo::input pixelFormat;
250 query.push_back(C2FieldSupportedValuesQuery::Possible(
251 C2ParamField::Make(pixelFormat, pixelFormat.value)));
252 } else {
253 C2StreamPixelFormatInfo::output pixelFormat;
254 query.push_back(C2FieldSupportedValuesQuery::Possible(
255 C2ParamField::Make(pixelFormat, pixelFormat.value)));
256 }
257 std::list<int32_t> supportedColorFormats;
258 if (intf->querySupportedValues(query, C2_DONT_BLOCK) == C2_OK) {
259 if (query[0].status == C2_OK) {
260 const C2FieldSupportedValues &fsv = query[0].values;
261 if (fsv.type == C2FieldSupportedValues::VALUES) {
262 for (C2Value::Primitive value : fsv.values) {
263 auto it = pixelFormatMap.find(value.u32);
264 if (it != pixelFormatMap.end()) {
265 auto it2 = std::find(
266 supportedColorFormats.begin(),
267 supportedColorFormats.end(),
268 it->second);
269 if (it2 == supportedColorFormats.end()) {
270 supportedColorFormats.push_back(it->second);
271 }
272 }
273 }
274 }
275 }
276 }
277 auto addDefaultColorFormat = [caps, &supportedColorFormats](int32_t colorFormat) {
278 caps->addColorFormat(colorFormat);
279 auto it = std::find(
280 supportedColorFormats.begin(), supportedColorFormats.end(), colorFormat);
281 if (it != supportedColorFormats.end()) {
282 supportedColorFormats.erase(it);
283 }
284 };
285
My Name298764f2022-03-25 15:07:51 -0700286 // The color format is ordered by preference. The intention here is to advertise:
287 // c2.android.* codecs: YUV420s, Surface, <the rest>
288 // all other codecs: Surface, YUV420s, <the rest>
289 // TODO: get this preference via Codec2 API
290
Lajos Molnardb5751f2019-01-31 17:01:49 -0800291 // vendor video codecs prefer opaque format
292 if (trait.name.find("android") == std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800293 addDefaultColorFormat(COLOR_FormatSurface);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800294 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800295 addDefaultColorFormat(COLOR_FormatYUV420Flexible);
296 addDefaultColorFormat(COLOR_FormatYUV420Planar);
297 addDefaultColorFormat(COLOR_FormatYUV420SemiPlanar);
298 addDefaultColorFormat(COLOR_FormatYUV420PackedPlanar);
299 addDefaultColorFormat(COLOR_FormatYUV420PackedSemiPlanar);
My Name298764f2022-03-25 15:07:51 -0700300 // Android video codecs prefer CPU-readable formats
301 if (trait.name.find("android") != std::string::npos) {
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800302 addDefaultColorFormat(COLOR_FormatSurface);
303 }
304 for (int32_t colorFormat : supportedColorFormats) {
305 caps->addColorFormat(colorFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800306 }
307 }
308}
309
Lajos Molnar424cfb52019-04-08 17:48:00 -0700310class Switch {
311 enum Flags : uint8_t {
312 // flags
313 IS_ENABLED = (1 << 0),
314 BY_DEFAULT = (1 << 1),
315 };
316
317 constexpr Switch(uint8_t flags) : mFlags(flags) {}
318
319 uint8_t mFlags;
320
321public:
322 // have to create class due to this bool conversion operator...
323 constexpr operator bool() const {
324 return mFlags & IS_ENABLED;
325 }
326
327 constexpr Switch operator!() const {
328 return Switch(mFlags ^ IS_ENABLED);
329 }
330
331 static constexpr Switch DISABLED() { return 0; };
332 static constexpr Switch ENABLED() { return IS_ENABLED; };
333 static constexpr Switch DISABLED_BY_DEFAULT() { return BY_DEFAULT; };
334 static constexpr Switch ENABLED_BY_DEFAULT() { return IS_ENABLED | BY_DEFAULT; };
335
336 const char *toString(const char *def = "??") const {
337 switch (mFlags) {
338 case 0: return "0";
339 case IS_ENABLED: return "1";
340 case BY_DEFAULT: return "(0)";
341 case IS_ENABLED | BY_DEFAULT: return "(1)";
342 default: return def;
343 }
344 }
345
346};
347
348const char *asString(const Switch &s, const char *def = "??") {
349 return s.toString(def);
350}
351
352Switch isSettingEnabled(
353 std::string setting, const MediaCodecsXmlParser::AttributeMap &settings,
354 Switch def = Switch::DISABLED_BY_DEFAULT()) {
355 const auto enablement = settings.find(setting);
356 if (enablement == settings.end()) {
357 return def;
358 }
359 return enablement->second == "1" ? Switch::ENABLED() : Switch::DISABLED();
360}
361
362Switch isVariantEnabled(
363 std::string variant, const MediaCodecsXmlParser::AttributeMap &settings) {
364 return isSettingEnabled("variant-" + variant, settings);
365}
366
367Switch isVariantExpressionEnabled(
368 std::string exp, const MediaCodecsXmlParser::AttributeMap &settings) {
369 if (!exp.empty() && exp.at(0) == '!') {
370 return !isVariantEnabled(exp.substr(1, exp.size() - 1), settings);
371 }
372 return isVariantEnabled(exp, settings);
373}
374
375Switch isDomainEnabled(
376 std::string domain, const MediaCodecsXmlParser::AttributeMap &settings) {
377 return isSettingEnabled("domain-" + domain, settings);
378}
379
Pawin Vongmasa36653902018-11-15 00:10:25 -0800380} // unnamed namespace
381
382status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
383 // TODO: Remove run-time configurations once all codecs are working
384 // properly. (Assume "full" behavior eventually.)
385 //
386 // debug.stagefright.ccodec supports 5 values.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800387 // 0 - No Codec 2.0 components are available.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800388 // 1 - Audio decoders and encoders with prefix "c2.android." are available
389 // and ranked first.
390 // All other components with prefix "c2.android." are available with
391 // their normal ranks.
392 // Components with prefix "c2.vda." are available with their normal
393 // ranks.
394 // All other components with suffix ".avc.decoder" or ".avc.encoder"
395 // are available but ranked last.
396 // 2 - Components with prefix "c2.android." are available and ranked
397 // first.
398 // Components with prefix "c2.vda." are available with their normal
399 // ranks.
400 // All other components with suffix ".avc.decoder" or ".avc.encoder"
401 // are available but ranked last.
402 // 3 - Components with prefix "c2.android." are available and ranked
403 // first.
404 // All other components are available with their normal ranks.
405 // 4 - All components are available with their normal ranks.
406 //
407 // The default value (boot time) is 1.
408 //
409 // Note: Currently, OMX components have default rank 0x100, while all
410 // Codec2.0 software components have default rank 0x200.
Lajos Molnar8635fc82019-05-17 17:35:10 +0000411 int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 4);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800412
413 // Obtain Codec2Client
414 std::vector<Traits> traits = Codec2Client::ListComponents();
415
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800416 // parse APEX XML first, followed by vendor XML.
417 // Note: APEX XML names do not depend on ro.media.xml_variant.* properties.
Lajos Molnarda666892019-04-08 17:25:34 -0700418 MediaCodecsXmlParser parser;
419 parser.parseXmlFilesInSearchDirs(
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800420 { "media_codecs.xml", "media_codecs_performance.xml" },
Lajos Molnar424cfb52019-04-08 17:48:00 -0700421 { "/apex/com.android.media.swcodec/etc" });
422
423 // TODO: remove these c2-specific files once product moved to default file names
424 parser.parseXmlFilesInSearchDirs(
Lajos Molnarda666892019-04-08 17:25:34 -0700425 { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
Lajos Molnar424cfb52019-04-08 17:48:00 -0700426
427 // parse default XML files
428 parser.parseXmlFilesInSearchDirs();
429
Ray Essick8c4e9c72021-03-15 15:25:21 -0700430 // The mainline modules for media may optionally include some codec shaping information.
431 // Based on vendor partition SDK, and the brand/product/device information
432 // (expect to be empty in almost always)
433 //
434 {
435 // get build info so we know what file to search
436 // ro.vendor.build.fingerprint
437 std::string fingerprint = base::GetProperty("ro.vendor.build.fingerprint",
438 "brand/product/device:");
439 ALOGV("property_get for ro.vendor.build.fingerprint == '%s'", fingerprint.c_str());
440
441 // ro.vendor.build.version.sdk
442 std::string sdk = base::GetProperty("ro.vendor.build.version.sdk", "0");
443 ALOGV("property_get for ro.vendor.build.version.sdk == '%s'", sdk.c_str());
444
445 std::string brand;
446 std::string product;
447 std::string device;
448 size_t pos1;
449 pos1 = fingerprint.find('/');
450 if (pos1 != std::string::npos) {
451 brand = fingerprint.substr(0, pos1);
452 size_t pos2 = fingerprint.find('/', pos1+1);
453 if (pos2 != std::string::npos) {
454 product = fingerprint.substr(pos1+1, pos2 - pos1 - 1);
455 size_t pos3 = fingerprint.find('/', pos2+1);
456 if (pos3 != std::string::npos) {
457 device = fingerprint.substr(pos2+1, pos3 - pos2 - 1);
458 size_t pos4 = device.find(':');
459 if (pos4 != std::string::npos) {
460 device.resize(pos4);
461 }
462 }
463 }
464 }
465
466 ALOGV("parsed: sdk '%s' brand '%s' product '%s' device '%s'",
467 sdk.c_str(), brand.c_str(), product.c_str(), device.c_str());
468
469 std::string base = "/apex/com.android.media/etc/formatshaper";
470
471 // looking in these directories within the apex
472 const std::vector<std::string> modulePathnames = {
473 base + "/" + sdk + "/" + brand + "/" + product + "/" + device,
474 base + "/" + sdk + "/" + brand + "/" + product,
475 base + "/" + sdk + "/" + brand,
476 base + "/" + sdk,
477 base
478 };
479
480 parser.parseXmlFilesInSearchDirs( { "media_codecs_shaping.xml" }, modulePathnames);
481 }
482
Pawin Vongmasa36653902018-11-15 00:10:25 -0800483 if (parser.getParsingStatus() != OK) {
484 ALOGD("XML parser no good");
485 return OK;
486 }
487
Lajos Molnar424cfb52019-04-08 17:48:00 -0700488 MediaCodecsXmlParser::AttributeMap settings = parser.getServiceAttributeMap();
489 for (const auto &v : settings) {
490 if (!hasPrefix(v.first, "media-type-")
491 && !hasPrefix(v.first, "domain-")
492 && !hasPrefix(v.first, "variant-")) {
493 writer->addGlobalSetting(v.first.c_str(), v.second.c_str());
494 }
495 }
496
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800497 std::map<std::string, PixelFormatMap> nameToPixelFormatMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800498 for (const Traits& trait : traits) {
499 C2Component::rank_t rank = trait.rank;
500
Lajos Molnardb5751f2019-01-31 17:01:49 -0800501 // Interface must be accessible for us to list the component, and there also
502 // must be an XML entry for the codec. Codec aliases listed in the traits
503 // allow additional XML entries to be specified for each alias. These will
504 // be listed as separate codecs. If no XML entry is specified for an alias,
505 // those will be treated as an additional alias specified in the XML entry
506 // for the interface name.
507 std::vector<std::string> nameAndAliases = trait.aliases;
508 nameAndAliases.insert(nameAndAliases.begin(), trait.name);
509 for (const std::string &nameOrAlias : nameAndAliases) {
510 bool isAlias = trait.name != nameOrAlias;
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800511 std::shared_ptr<Codec2Client> client;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800512 std::shared_ptr<Codec2Client::Interface> intf =
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800513 Codec2Client::CreateInterfaceByName(nameOrAlias.c_str(), &client);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800514 if (!intf) {
515 ALOGD("could not create interface for %s'%s'",
516 isAlias ? "alias " : "",
517 nameOrAlias.c_str());
518 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800519 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800520 if (parser.getCodecMap().count(nameOrAlias) == 0) {
521 if (isAlias) {
522 std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo =
523 writer->findMediaCodecInfo(trait.name.c_str());
524 if (!baseCodecInfo) {
525 ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing",
526 nameOrAlias.c_str(),
527 trait.name.c_str());
528 } else {
529 ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this",
530 nameOrAlias.c_str());
531 // merge alias into existing codec
532 baseCodecInfo->addAlias(nameOrAlias.c_str());
533 }
534 } else {
535 ALOGD("component '%s' not found in xml", trait.name.c_str());
536 }
537 continue;
538 }
539 std::string canonName = trait.name;
540
541 // TODO: Remove this block once all codecs are enabled by default.
542 switch (option) {
543 case 0:
544 continue;
545 case 1:
546 if (hasPrefix(canonName, "c2.vda.")) {
547 break;
548 }
549 if (hasPrefix(canonName, "c2.android.")) {
550 if (trait.domain == C2Component::DOMAIN_AUDIO) {
551 rank = 1;
552 break;
553 }
554 break;
555 }
556 if (hasSuffix(canonName, ".avc.decoder") ||
557 hasSuffix(canonName, ".avc.encoder")) {
558 rank = std::numeric_limits<decltype(rank)>::max();
559 break;
560 }
561 continue;
562 case 2:
563 if (hasPrefix(canonName, "c2.vda.")) {
564 break;
565 }
566 if (hasPrefix(canonName, "c2.android.")) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800567 rank = 1;
568 break;
569 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800570 if (hasSuffix(canonName, ".avc.decoder") ||
571 hasSuffix(canonName, ".avc.encoder")) {
572 rank = std::numeric_limits<decltype(rank)>::max();
573 break;
574 }
575 continue;
576 case 3:
577 if (hasPrefix(canonName, "c2.android.")) {
578 rank = 1;
579 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800580 break;
581 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800582
Lajos Molnar424cfb52019-04-08 17:48:00 -0700583 const MediaCodecsXmlParser::CodecProperties &codec =
584 parser.getCodecMap().at(nameOrAlias);
585
586 // verify that either the codec is explicitly enabled, or one of its domains is
587 bool codecEnabled = codec.quirkSet.find("attribute::disabled") == codec.quirkSet.end();
588 if (!codecEnabled) {
589 for (const std::string &domain : codec.domainSet) {
590 const Switch enabled = isDomainEnabled(domain, settings);
591 ALOGV("codec entry '%s' is in domain '%s' that is '%s'",
592 nameOrAlias.c_str(), domain.c_str(), asString(enabled));
593 if (enabled) {
594 codecEnabled = true;
595 break;
596 }
597 }
598 }
599 // if codec has variants, also check that at least one of them is enabled
600 bool variantEnabled = codec.variantSet.empty();
601 for (const std::string &variant : codec.variantSet) {
602 const Switch enabled = isVariantExpressionEnabled(variant, settings);
603 ALOGV("codec entry '%s' has a variant '%s' that is '%s'",
604 nameOrAlias.c_str(), variant.c_str(), asString(enabled));
605 if (enabled) {
606 variantEnabled = true;
607 break;
608 }
609 }
610 if (!codecEnabled || !variantEnabled) {
611 ALOGD("codec entry for '%s' is disabled", nameOrAlias.c_str());
612 continue;
613 }
614
Lajos Molnardb5751f2019-01-31 17:01:49 -0800615 ALOGV("adding codec entry for '%s'", nameOrAlias.c_str());
616 std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
617 codecInfo->setName(nameOrAlias.c_str());
618 codecInfo->setOwner(("codec2::" + trait.owner).c_str());
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800619
Lajos Molnardb5751f2019-01-31 17:01:49 -0800620 bool encoder = trait.kind == C2Component::KIND_ENCODER;
621 typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800622
Lajos Molnardb5751f2019-01-31 17:01:49 -0800623 if (encoder) {
624 attrs |= MediaCodecInfo::kFlagIsEncoder;
625 }
626 if (trait.owner == "software") {
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800627 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800628 } else {
629 attrs |= MediaCodecInfo::kFlagIsVendor;
630 if (trait.owner == "vendor-software") {
631 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
632 } else if (codec.quirkSet.find("attribute::software-codec")
633 == codec.quirkSet.end()) {
634 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800635 }
636 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800637 codecInfo->setAttributes(attrs);
638 if (!codec.rank.empty()) {
639 uint32_t xmlRank;
640 char dummy;
641 if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) {
642 rank = xmlRank;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800643 }
644 }
Lajos Molnar424cfb52019-04-08 17:48:00 -0700645 ALOGV("rank: %u", (unsigned)rank);
Lajos Molnardb5751f2019-01-31 17:01:49 -0800646 codecInfo->setRank(rank);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800647
Lajos Molnardb5751f2019-01-31 17:01:49 -0800648 for (const std::string &alias : codec.aliases) {
649 ALOGV("adding alias '%s'", alias.c_str());
650 codecInfo->addAlias(alias.c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800651 }
652
Lajos Molnardb5751f2019-01-31 17:01:49 -0800653 for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
654 const std::string &mediaType = typeIt->first;
Lajos Molnar424cfb52019-04-08 17:48:00 -0700655 const Switch typeEnabled = isSettingEnabled(
656 "media-type-" + mediaType, settings, Switch::ENABLED_BY_DEFAULT());
657 const Switch domainTypeEnabled = isSettingEnabled(
658 "media-type-" + mediaType + (encoder ? "-encoder" : "-decoder"),
659 settings, Switch::ENABLED_BY_DEFAULT());
660 ALOGV("type '%s-%s' is '%s/%s'",
661 mediaType.c_str(), (encoder ? "encoder" : "decoder"),
662 asString(typeEnabled), asString(domainTypeEnabled));
663 if (!typeEnabled || !domainTypeEnabled) {
664 ALOGD("media type '%s' for codec entry '%s' is disabled", mediaType.c_str(),
665 nameOrAlias.c_str());
666 continue;
667 }
668
669 ALOGI("adding type '%s'", typeIt->first.c_str());
Lajos Molnardb5751f2019-01-31 17:01:49 -0800670 const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
671 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
672 codecInfo->addMediaType(mediaType.c_str());
Lajos Molnar424cfb52019-04-08 17:48:00 -0700673 for (const auto &v : attrMap) {
674 std::string key = v.first;
675 std::string value = v.second;
676
677 size_t variantSep = key.find(":::");
678 if (variantSep != std::string::npos) {
679 std::string variant = key.substr(0, variantSep);
680 const Switch enabled = isVariantExpressionEnabled(variant, settings);
681 ALOGV("variant '%s' is '%s'", variant.c_str(), asString(enabled));
682 if (!enabled) {
683 continue;
684 }
685 key = key.substr(variantSep + 3);
686 }
687
Lajos Molnardb5751f2019-01-31 17:01:49 -0800688 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
689 int32_t intValue = 0;
690 // Ignore trailing bad characters and default to 0.
691 (void)sscanf(value.c_str(), "%d", &intValue);
692 caps->addDetail(key.c_str(), intValue);
693 } else {
694 caps->addDetail(key.c_str(), value.c_str());
695 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800696 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800697
Lajos Molnar59f4a4e2021-07-09 18:23:54 -0700698 if (!addSupportedProfileLevels(intf, caps.get(), trait, mediaType)) {
699 // TODO(b/193279646) This will get fixed in C2InterfaceHelper
700 // Some components may not advertise supported values if they use a const
701 // param for profile/level (they support only one profile). For now cover
702 // only VP8 here until it is fixed.
703 if (mediaType == MIMETYPE_VIDEO_VP8) {
704 caps->addProfileLevel(VP8ProfileMain, VP8Level_Version0);
705 }
706 }
Wonsik Kimf87cbc42022-01-24 09:49:12 -0800707
708 auto it = nameToPixelFormatMap.find(client->getServiceName());
709 if (it == nameToPixelFormatMap.end()) {
710 it = nameToPixelFormatMap.try_emplace(client->getServiceName()).first;
711 PixelFormatMap &pixelFormatMap = it->second;
712 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_420_888] = COLOR_FormatYUV420Flexible;
713 pixelFormatMap[HAL_PIXEL_FORMAT_YCBCR_P010] = COLOR_FormatYUVP010;
714 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_1010102] = COLOR_Format32bitABGR2101010;
715 pixelFormatMap[HAL_PIXEL_FORMAT_RGBA_FP16] = COLOR_Format64bitABGRFloat;
716
717 std::shared_ptr<C2StoreFlexiblePixelFormatDescriptorsInfo> pixelFormatInfo;
718 std::vector<std::unique_ptr<C2Param>> heapParams;
719 if (client->query(
720 {},
721 {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
722 C2_MAY_BLOCK,
723 &heapParams) == C2_OK
724 && heapParams.size() == 1u) {
725 pixelFormatInfo.reset(C2StoreFlexiblePixelFormatDescriptorsInfo::From(
726 heapParams[0].release()));
727 }
728 if (pixelFormatInfo && *pixelFormatInfo) {
729 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
730 C2FlexiblePixelFormatDescriptorStruct &desc =
731 pixelFormatInfo->m.values[i];
732 std::optional<int32_t> colorFormat = findFrameworkColorFormat(desc);
733 if (colorFormat) {
734 pixelFormatMap[desc.pixelFormat] = *colorFormat;
735 }
736 }
737 }
738 }
739 addSupportedColorFormats(
740 intf, caps.get(), trait, mediaType, it->second);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800741 }
742 }
743 }
744 return OK;
745}
746
747} // namespace android
748
749extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
750 return new android::Codec2InfoBuilder;
751}