blob: ead0a9bd3b38528d72dd600d88cfab9e5d486818 [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>
46#include <media/stagefright/MediaCodecConstants.h>
Pawin Vongmasa1f213362019-01-24 06:59:16 -080047#include <media/stagefright/foundation/ALookup.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080048#include <media/stagefright/foundation/MediaDefs.h>
49#include <media/stagefright/omx/OMXUtils.h>
50#include <media/stagefright/xmlparser/MediaCodecsXmlParser.h>
51
52#include "Codec2InfoBuilder.h"
53
54namespace android {
55
56using Traits = C2Component::Traits;
57
58namespace /* unnamed */ {
59
60bool hasPrefix(const std::string& s, const char* prefix) {
61 size_t prefixLen = strlen(prefix);
62 return s.compare(0, prefixLen, prefix) == 0;
63}
64
65bool hasSuffix(const std::string& s, const char* suffix) {
66 size_t suffixLen = strlen(suffix);
67 return suffixLen > s.size() ? false :
68 s.compare(s.size() - suffixLen, suffixLen, suffix) == 0;
69}
70
Lajos Molnardb5751f2019-01-31 17:01:49 -080071void addSupportedProfileLevels(
72 std::shared_ptr<Codec2Client::Interface> intf,
73 MediaCodecInfo::CapabilitiesWriter *caps,
74 const Traits& trait, const std::string &mediaType) {
75 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
76 C2Mapper::GetProfileLevelMapper(trait.mediaType);
77 // if we don't know the media type, pass through all values unmapped
Pawin Vongmasa36653902018-11-15 00:10:25 -080078
Lajos Molnardb5751f2019-01-31 17:01:49 -080079 // TODO: we cannot find levels that are local 'maxima' without knowing the coding
80 // e.g. H.263 level 45 and level 30 could be two values for highest level as
81 // they don't include one another. For now we use the last supported value.
82 bool encoder = trait.kind == C2Component::KIND_ENCODER;
83 C2StreamProfileLevelInfo pl(encoder /* output */, 0u);
84 std::vector<C2FieldSupportedValuesQuery> profileQuery = {
85 C2FieldSupportedValuesQuery::Possible(C2ParamField(&pl, &pl.profile))
Pawin Vongmasa36653902018-11-15 00:10:25 -080086 };
87
Lajos Molnardb5751f2019-01-31 17:01:49 -080088 c2_status_t err = intf->querySupportedValues(profileQuery, C2_DONT_BLOCK);
89 ALOGV("query supported profiles -> %s | %s", asString(err), asString(profileQuery[0].status));
90 if (err != C2_OK || profileQuery[0].status != C2_OK) {
91 return;
Pawin Vongmasa36653902018-11-15 00:10:25 -080092 }
93
Lajos Molnardb5751f2019-01-31 17:01:49 -080094 // we only handle enumerated values
95 if (profileQuery[0].values.type != C2FieldSupportedValues::VALUES) {
96 return;
Pawin Vongmasa36653902018-11-15 00:10:25 -080097 }
98
Lajos Molnardb5751f2019-01-31 17:01:49 -080099 // determine if codec supports HDR
100 bool supportsHdr = false;
101 bool supportsHdr10Plus = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800102
Lajos Molnardb5751f2019-01-31 17:01:49 -0800103 std::vector<std::shared_ptr<C2ParamDescriptor>> paramDescs;
104 c2_status_t err1 = intf->querySupportedParams(&paramDescs);
105 if (err1 == C2_OK) {
106 for (const std::shared_ptr<C2ParamDescriptor> &desc : paramDescs) {
107 switch ((uint32_t)desc->index()) {
108 case C2StreamHdr10PlusInfo::output::PARAM_TYPE:
109 supportsHdr10Plus = true;
110 break;
111 case C2StreamHdrStaticInfo::output::PARAM_TYPE:
112 supportsHdr = true;
113 break;
114 default:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800115 break;
116 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800117 }
118 }
119
Lajos Molnardb5751f2019-01-31 17:01:49 -0800120 // For VP9, the static info is always propagated by framework.
121 supportsHdr |= (mediaType == MIMETYPE_VIDEO_VP9);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800122
Lajos Molnardb5751f2019-01-31 17:01:49 -0800123 for (C2Value::Primitive profile : profileQuery[0].values.values) {
124 pl.profile = (C2Config::profile_t)profile.ref<uint32_t>();
125 std::vector<std::unique_ptr<C2SettingResult>> failures;
126 err = intf->config({&pl}, C2_DONT_BLOCK, &failures);
127 ALOGV("set profile to %u -> %s", pl.profile, asString(err));
128 std::vector<C2FieldSupportedValuesQuery> levelQuery = {
129 C2FieldSupportedValuesQuery::Current(C2ParamField(&pl, &pl.level))
130 };
131 err = intf->querySupportedValues(levelQuery, C2_DONT_BLOCK);
132 ALOGV("query supported levels -> %s | %s", asString(err), asString(levelQuery[0].status));
133 if (err != C2_OK || levelQuery[0].status != C2_OK
134 || levelQuery[0].values.type != C2FieldSupportedValues::VALUES
135 || levelQuery[0].values.values.size() == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800136 continue;
137 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800138
139 C2Value::Primitive level = levelQuery[0].values.values.back();
140 pl.level = (C2Config::level_t)level.ref<uint32_t>();
141 ALOGV("supporting level: %u", pl.level);
142 int32_t sdkProfile, sdkLevel;
143 if (mapper && mapper->mapProfile(pl.profile, &sdkProfile)
144 && mapper->mapLevel(pl.level, &sdkLevel)) {
145 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
146 // also list HDR profiles if component supports HDR
147 if (supportsHdr) {
148 auto hdrMapper = C2Mapper::GetHdrProfileLevelMapper(trait.mediaType);
149 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
150 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
151 }
152 if (supportsHdr10Plus) {
153 hdrMapper = C2Mapper::GetHdrProfileLevelMapper(
154 trait.mediaType, true /*isHdr10Plus*/);
155 if (hdrMapper && hdrMapper->mapProfile(pl.profile, &sdkProfile)) {
156 caps->addProfileLevel((uint32_t)sdkProfile, (uint32_t)sdkLevel);
157 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800158 }
159 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800160 } else if (!mapper) {
161 caps->addProfileLevel(pl.profile, pl.level);
162 }
163
164 // for H.263 also advertise the second highest level if the
165 // codec supports level 45, as level 45 only covers level 10
166 // TODO: move this to some form of a setting so it does not
167 // have to be here
168 if (mediaType == MIMETYPE_VIDEO_H263) {
169 C2Config::level_t nextLevel = C2Config::LEVEL_UNUSED;
170 for (C2Value::Primitive v : levelQuery[0].values.values) {
171 C2Config::level_t level = (C2Config::level_t)v.ref<uint32_t>();
172 if (level < C2Config::LEVEL_H263_45 && level > nextLevel) {
173 nextLevel = level;
174 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800175 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800176 if (nextLevel != C2Config::LEVEL_UNUSED
177 && nextLevel != pl.level
178 && mapper
179 && mapper->mapProfile(pl.profile, &sdkProfile)
180 && mapper->mapLevel(nextLevel, &sdkLevel)) {
181 caps->addProfileLevel(
182 (uint32_t)sdkProfile, (uint32_t)sdkLevel);
183 }
184 }
185 }
186}
187
188void addSupportedColorFormats(
189 std::shared_ptr<Codec2Client::Interface> intf,
190 MediaCodecInfo::CapabilitiesWriter *caps,
191 const Traits& trait, const std::string &mediaType) {
192 (void)intf;
193
194 // TODO: get this from intf() as well, but how do we map them to
195 // MediaCodec color formats?
196 bool encoder = trait.kind == C2Component::KIND_ENCODER;
197 if (mediaType.find("video") != std::string::npos) {
198 // vendor video codecs prefer opaque format
199 if (trait.name.find("android") == std::string::npos) {
200 caps->addColorFormat(COLOR_FormatSurface);
201 }
202 caps->addColorFormat(COLOR_FormatYUV420Flexible);
203 caps->addColorFormat(COLOR_FormatYUV420Planar);
204 caps->addColorFormat(COLOR_FormatYUV420SemiPlanar);
205 caps->addColorFormat(COLOR_FormatYUV420PackedPlanar);
206 caps->addColorFormat(COLOR_FormatYUV420PackedSemiPlanar);
207 // framework video encoders must support surface format, though it is unclear
208 // that they will be able to map it if it is opaque
209 if (encoder && trait.name.find("android") != std::string::npos) {
210 caps->addColorFormat(COLOR_FormatSurface);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800211 }
212 }
213}
214
215} // unnamed namespace
216
217status_t Codec2InfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
218 // TODO: Remove run-time configurations once all codecs are working
219 // properly. (Assume "full" behavior eventually.)
220 //
221 // debug.stagefright.ccodec supports 5 values.
Lajos Molnardb5751f2019-01-31 17:01:49 -0800222 // 0 - No Codec 2.0 components are available.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800223 // 1 - Audio decoders and encoders with prefix "c2.android." are available
224 // and ranked first.
225 // All other components with prefix "c2.android." are available with
226 // their normal ranks.
227 // Components with prefix "c2.vda." are available with their normal
228 // ranks.
229 // All other components with suffix ".avc.decoder" or ".avc.encoder"
230 // are available but ranked last.
231 // 2 - Components with prefix "c2.android." are available and ranked
232 // first.
233 // Components with prefix "c2.vda." are available with their normal
234 // ranks.
235 // All other components with suffix ".avc.decoder" or ".avc.encoder"
236 // are available but ranked last.
237 // 3 - Components with prefix "c2.android." are available and ranked
238 // first.
239 // All other components are available with their normal ranks.
240 // 4 - All components are available with their normal ranks.
241 //
242 // The default value (boot time) is 1.
243 //
244 // Note: Currently, OMX components have default rank 0x100, while all
245 // Codec2.0 software components have default rank 0x200.
246 int option = ::android::base::GetIntProperty("debug.stagefright.ccodec", 1);
247
248 // Obtain Codec2Client
249 std::vector<Traits> traits = Codec2Client::ListComponents();
250
251 MediaCodecsXmlParser parser(
252 MediaCodecsXmlParser::defaultSearchDirs,
Lajos Molnardb5751f2019-01-31 17:01:49 -0800253 "media_codecs_c2.xml",
254 "media_codecs_performance_c2.xml");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800255 if (parser.getParsingStatus() != OK) {
256 ALOGD("XML parser no good");
257 return OK;
258 }
259
Pawin Vongmasa36653902018-11-15 00:10:25 -0800260 for (const Traits& trait : traits) {
261 C2Component::rank_t rank = trait.rank;
262
Lajos Molnardb5751f2019-01-31 17:01:49 -0800263 // Interface must be accessible for us to list the component, and there also
264 // must be an XML entry for the codec. Codec aliases listed in the traits
265 // allow additional XML entries to be specified for each alias. These will
266 // be listed as separate codecs. If no XML entry is specified for an alias,
267 // those will be treated as an additional alias specified in the XML entry
268 // for the interface name.
269 std::vector<std::string> nameAndAliases = trait.aliases;
270 nameAndAliases.insert(nameAndAliases.begin(), trait.name);
271 for (const std::string &nameOrAlias : nameAndAliases) {
272 bool isAlias = trait.name != nameOrAlias;
273 std::shared_ptr<Codec2Client::Interface> intf =
274 Codec2Client::CreateInterfaceByName(nameOrAlias.c_str());
275 if (!intf) {
276 ALOGD("could not create interface for %s'%s'",
277 isAlias ? "alias " : "",
278 nameOrAlias.c_str());
279 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800280 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800281 if (parser.getCodecMap().count(nameOrAlias) == 0) {
282 if (isAlias) {
283 std::unique_ptr<MediaCodecInfoWriter> baseCodecInfo =
284 writer->findMediaCodecInfo(trait.name.c_str());
285 if (!baseCodecInfo) {
286 ALOGD("alias '%s' not found in xml but canonical codec info '%s' missing",
287 nameOrAlias.c_str(),
288 trait.name.c_str());
289 } else {
290 ALOGD("alias '%s' not found in xml; use an XML <Alias> tag for this",
291 nameOrAlias.c_str());
292 // merge alias into existing codec
293 baseCodecInfo->addAlias(nameOrAlias.c_str());
294 }
295 } else {
296 ALOGD("component '%s' not found in xml", trait.name.c_str());
297 }
298 continue;
299 }
300 std::string canonName = trait.name;
301
302 // TODO: Remove this block once all codecs are enabled by default.
303 switch (option) {
304 case 0:
305 continue;
306 case 1:
307 if (hasPrefix(canonName, "c2.vda.")) {
308 break;
309 }
310 if (hasPrefix(canonName, "c2.android.")) {
311 if (trait.domain == C2Component::DOMAIN_AUDIO) {
312 rank = 1;
313 break;
314 }
315 break;
316 }
317 if (hasSuffix(canonName, ".avc.decoder") ||
318 hasSuffix(canonName, ".avc.encoder")) {
319 rank = std::numeric_limits<decltype(rank)>::max();
320 break;
321 }
322 continue;
323 case 2:
324 if (hasPrefix(canonName, "c2.vda.")) {
325 break;
326 }
327 if (hasPrefix(canonName, "c2.android.")) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800328 rank = 1;
329 break;
330 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800331 if (hasSuffix(canonName, ".avc.decoder") ||
332 hasSuffix(canonName, ".avc.encoder")) {
333 rank = std::numeric_limits<decltype(rank)>::max();
334 break;
335 }
336 continue;
337 case 3:
338 if (hasPrefix(canonName, "c2.android.")) {
339 rank = 1;
340 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800341 break;
342 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800343
Lajos Molnardb5751f2019-01-31 17:01:49 -0800344 ALOGV("adding codec entry for '%s'", nameOrAlias.c_str());
345 std::unique_ptr<MediaCodecInfoWriter> codecInfo = writer->addMediaCodecInfo();
346 codecInfo->setName(nameOrAlias.c_str());
347 codecInfo->setOwner(("codec2::" + trait.owner).c_str());
348 const MediaCodecsXmlParser::CodecProperties &codec =
349 parser.getCodecMap().at(nameOrAlias);
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800350
Lajos Molnardb5751f2019-01-31 17:01:49 -0800351 bool encoder = trait.kind == C2Component::KIND_ENCODER;
352 typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800353
Lajos Molnardb5751f2019-01-31 17:01:49 -0800354 if (encoder) {
355 attrs |= MediaCodecInfo::kFlagIsEncoder;
356 }
357 if (trait.owner == "software") {
Lajos Molnar8d4bdfd2018-11-13 14:23:49 -0800358 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
Lajos Molnardb5751f2019-01-31 17:01:49 -0800359 } else {
360 attrs |= MediaCodecInfo::kFlagIsVendor;
361 if (trait.owner == "vendor-software") {
362 attrs |= MediaCodecInfo::kFlagIsSoftwareOnly;
363 } else if (codec.quirkSet.find("attribute::software-codec")
364 == codec.quirkSet.end()) {
365 attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800366 }
367 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800368 codecInfo->setAttributes(attrs);
369 if (!codec.rank.empty()) {
370 uint32_t xmlRank;
371 char dummy;
372 if (sscanf(codec.rank.c_str(), "%u%c", &xmlRank, &dummy) == 1) {
373 rank = xmlRank;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800374 }
375 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800376 codecInfo->setRank(rank);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800377
Lajos Molnardb5751f2019-01-31 17:01:49 -0800378 for (const std::string &alias : codec.aliases) {
379 ALOGV("adding alias '%s'", alias.c_str());
380 codecInfo->addAlias(alias.c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800381 }
382
Lajos Molnardb5751f2019-01-31 17:01:49 -0800383 for (auto typeIt = codec.typeMap.begin(); typeIt != codec.typeMap.end(); ++typeIt) {
384 const std::string &mediaType = typeIt->first;
385 const MediaCodecsXmlParser::AttributeMap &attrMap = typeIt->second;
386 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
387 codecInfo->addMediaType(mediaType.c_str());
388 for (auto attrIt = attrMap.begin(); attrIt != attrMap.end(); ++attrIt) {
389 std::string key, value;
390 std::tie(key, value) = *attrIt;
391 if (key.find("feature-") == 0 && key.find("feature-bitrate-modes") != 0) {
392 int32_t intValue = 0;
393 // Ignore trailing bad characters and default to 0.
394 (void)sscanf(value.c_str(), "%d", &intValue);
395 caps->addDetail(key.c_str(), intValue);
396 } else {
397 caps->addDetail(key.c_str(), value.c_str());
398 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800399 }
Lajos Molnardb5751f2019-01-31 17:01:49 -0800400
401 addSupportedProfileLevels(intf, caps.get(), trait, mediaType);
402 addSupportedColorFormats(intf, caps.get(), trait, mediaType);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800403 }
404 }
405 }
406 return OK;
407}
408
409} // namespace android
410
411extern "C" android::MediaCodecListBuilderBase *CreateBuilder() {
412 return new android::Codec2InfoBuilder;
413}