Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9d301ec7 authored by Lajos Molnar's avatar Lajos Molnar
Browse files

stagefright: rework and simplify OmxInfoBuilder

- allow building only codecs that do not use input surface (when
  using C2 HAL input surface)
- remove preferPlatform logic as we now have rank for this

Bug: 119631295
Change-Id: I3b4cdb7db27351ee3f13d17dfa6879418d4097f6
parent 4711827d
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -77,7 +77,8 @@ bool isProfilingNeeded() {
    return profilingNeeded;
}

OmxInfoBuilder sOmxInfoBuilder;
OmxInfoBuilder sOmxInfoBuilder{true /* allowSurfaceEncoders */};
OmxInfoBuilder sOmxNoSurfaceEncoderInfoBuilder{false /* allowSurfaceEncoders */};

Mutex sCodec2InfoBuilderMutex;
std::unique_ptr<MediaCodecListBuilderBase> sCodec2InfoBuilder;
@@ -98,7 +99,11 @@ std::vector<MediaCodecListBuilderBase *> GetBuilders() {
    sp<PersistentSurface> surfaceTest =
        StagefrightPluginLoader::GetCCodecInstance()->createInputSurface();
    if (surfaceTest == nullptr) {
        ALOGD("Allowing all OMX codecs");
        builders.push_back(&sOmxInfoBuilder);
    } else {
        ALOGD("Allowing only non-surface-encoder OMX codecs");
        builders.push_back(&sOmxNoSurfaceEncoderInfoBuilder);
    }
    builders.push_back(GetCodec2InfoBuilder());
    return builders;
+67 −113
Original line number Diff line number Diff line
@@ -21,8 +21,8 @@
#define OMX_ANDROID_COMPILE_AS_32BIT_ON_64BIT_PLATFORMS
#endif

#include <android-base/properties.h>
#include <utils/Log.h>
#include <cutils/properties.h>

#include <media/stagefright/foundation/MediaDefs.h>
#include <media/stagefright/OmxInfoBuilder.h>
@@ -53,7 +53,7 @@ using namespace ::android::hardware::media::omx::V1_0;
namespace /* unnamed */ {

bool hasPrefix(const hidl_string& s, const char* prefix) {
    return strncmp(s.c_str(), prefix, strlen(prefix)) == 0;
    return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0;
}

status_t queryCapabilities(
@@ -87,7 +87,8 @@ status_t queryCapabilities(

}  // unnamed namespace

OmxInfoBuilder::OmxInfoBuilder() {
OmxInfoBuilder::OmxInfoBuilder(bool allowSurfaceEncoders)
    : mAllowSurfaceEncoders(allowSurfaceEncoders) {
}

status_t OmxInfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
@@ -135,60 +136,63 @@ status_t OmxInfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
    // Convert roles to lists of codecs

    // codec name -> index into swCodecs/hwCodecs
    std::map<hidl_string, std::unique_ptr<MediaCodecInfoWriter>>
            swCodecName2Info, hwCodecName2Info;
    std::map<hidl_string, std::unique_ptr<MediaCodecInfoWriter>> codecName2Info;

    uint32_t defaultRank =
        ::android::base::GetUintProperty("debug.stagefright.omx_default_rank", 0x100u);
    uint32_t defaultSwAudioRank =
        ::android::base::GetUintProperty("debug.stagefright.omx_default_rank.sw-audio", 0x10u);
    uint32_t defaultSwOtherRank =
        ::android::base::GetUintProperty("debug.stagefright.omx_default_rank.sw-other", 0x210u);

    char rank[PROPERTY_VALUE_MAX];
    uint32_t defaultRank = 0x100;
    if (property_get("debug.stagefright.omx_default_rank", rank, nullptr)) {
        defaultRank = std::strtoul(rank, nullptr, 10);
    }
    for (const IOmxStore::RoleInfo& role : roles) {
        const hidl_string& typeName = role.type;
        bool isEncoder = role.isEncoder;
        bool preferPlatformNodes = role.preferPlatformNodes;
        // If preferPlatformNodes is true, hardware nodes must be added after
        // platform (software) nodes. hwCodecs is used to hold hardware nodes
        // that need to be added after software nodes for the same role.
        std::vector<const IOmxStore::NodeInfo*> hwCodecs;
        bool isAudio = hasPrefix(role.type, "audio/");
        bool isVideoOrImage = hasPrefix(role.type, "video/") || hasPrefix(role.type, "image/");

        for (const IOmxStore::NodeInfo &node : role.nodes) {
            const hidl_string& nodeName = node.name;

            // currently image and video encoders use surface input
            if (!mAllowSurfaceEncoders && isVideoOrImage && isEncoder) {
                ALOGD("disabling %s for media type %s because we are not using OMX input surface",
                        nodeName.c_str(), role.type.c_str());
                continue;
            }

            bool isSoftware = hasPrefix(nodeName, "OMX.google");
            uint32_t rank = isSoftware
                    ? (isAudio ? defaultSwAudioRank : defaultSwOtherRank)
                    : defaultRank;
            // get rank from IOmxStore via attribute
            for (const IOmxStore::Attribute& attribute : node.attributes) {
                if (attribute.key == "rank") {
                    uint32_t oldRank = rank;
                    char dummy;
                    if (sscanf(attribute.value.c_str(), "%u%c", &rank, &dummy) != 1) {
                        rank = oldRank;
                    }
                    break;
                }
            }

            MediaCodecInfoWriter* info;
            if (isSoftware) {
                auto c2i = swCodecName2Info.find(nodeName);
                if (c2i == swCodecName2Info.end()) {
            auto c2i = codecName2Info.find(nodeName);
            if (c2i == codecName2Info.end()) {
                // Create a new MediaCodecInfo for a new node.
                    c2i = swCodecName2Info.insert(std::make_pair(
                c2i = codecName2Info.insert(std::make_pair(
                        nodeName, writer->addMediaCodecInfo())).first;
                info = c2i->second.get();
                info->setName(nodeName.c_str());
                info->setOwner(node.owner.c_str());
                    info->setAttributes(
                info->setRank(rank);

                typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs = 0;
                // all OMX codecs are vendor codecs (in the vendor partition), but
                // treat OMX.google codecs as non-hardware-accelerated and non-vendor
                            (isEncoder ? MediaCodecInfo::kFlagIsEncoder : 0));
                    info->setRank(defaultRank);
                } else {
                    // The node has been seen before. Simply retrieve the
                    // existing MediaCodecInfoWriter.
                    info = c2i->second.get();
                }
            } else {
                auto c2i = hwCodecName2Info.find(nodeName);
                if (c2i == hwCodecName2Info.end()) {
                    // Create a new MediaCodecInfo for a new node.
                    if (!preferPlatformNodes) {
                        c2i = hwCodecName2Info.insert(std::make_pair(
                                nodeName, writer->addMediaCodecInfo())).first;
                        info = c2i->second.get();
                        info->setName(nodeName.c_str());
                        info->setOwner(node.owner.c_str());
                        typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs =
                            MediaCodecInfo::kFlagIsVendor;
                        if (isEncoder) {
                            attrs |= MediaCodecInfo::kFlagIsEncoder;
                        }
                if (!isSoftware) {
                    attrs |= MediaCodecInfo::kFlagIsVendor;
                    if (std::count_if(
                            node.attributes.begin(), node.attributes.end(),
                            [](const IOmxStore::Attribute &i) -> bool {
@@ -196,58 +200,11 @@ status_t OmxInfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
                                                                      })) {
                        attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
                    }
                        info->setAttributes(attrs);
                        info->setRank(defaultRank);
                    } else {
                        // If preferPlatformNodes is true, this node must be
                        // added after all software nodes.
                        hwCodecs.push_back(&node);
                        continue;
                }
                } else {
                    // The node has been seen before. Simply retrieve the
                    // existing MediaCodecInfoWriter.
                    info = c2i->second.get();
                }
            }
            std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
                    info->addMediaType(typeName.c_str());
            if (queryCapabilities(
                    node, typeName.c_str(), isEncoder, caps.get()) != OK) {
                ALOGW("Fail to add media type %s to codec %s",
                        typeName.c_str(), nodeName.c_str());
                info->removeMediaType(typeName.c_str());
            }
        }

        // If preferPlatformNodes is true, hardware nodes will not have been
        // added in the loop above, but rather saved in hwCodecs. They are
        // going to be added here.
        if (preferPlatformNodes) {
            for (const IOmxStore::NodeInfo *node : hwCodecs) {
                MediaCodecInfoWriter* info;
                const hidl_string& nodeName = node->name;
                auto c2i = hwCodecName2Info.find(nodeName);
                if (c2i == hwCodecName2Info.end()) {
                    // Create a new MediaCodecInfo for a new node.
                    c2i = hwCodecName2Info.insert(std::make_pair(
                            nodeName, writer->addMediaCodecInfo())).first;
                    info = c2i->second.get();
                    info->setName(nodeName.c_str());
                    info->setOwner(node->owner.c_str());
                    typename std::underlying_type<MediaCodecInfo::Attributes>::type attrs =
                        MediaCodecInfo::kFlagIsVendor;
                if (isEncoder) {
                    attrs |= MediaCodecInfo::kFlagIsEncoder;
                }
                    if (std::count_if(
                            node->attributes.begin(), node->attributes.end(),
                            [](const IOmxStore::Attribute &i) -> bool {
                                return i.key == "attribute::software-codec";
                                                                      })) {
                        attrs |= MediaCodecInfo::kFlagIsHardwareAccelerated;
                    }
                    info->setRank(defaultRank);
                info->setAttributes(attrs);
            } else {
                // The node has been seen before. Simply retrieve the
                // existing MediaCodecInfoWriter.
@@ -256,17 +213,14 @@ status_t OmxInfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
            std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
                    info->addMediaType(typeName.c_str());
            if (queryCapabilities(
                        *node, typeName.c_str(), isEncoder, caps.get()) != OK) {
                    ALOGW("Fail to add media type %s to codec %s "
                          "after software codecs",
                    node, typeName.c_str(), isEncoder, caps.get()) != OK) {
                ALOGW("Fail to add media type %s to codec %s",
                        typeName.c_str(), nodeName.c_str());
                info->removeMediaType(typeName.c_str());
            }
        }
    }
    }
    return OK;
}

}  // namespace android
+4 −1
Original line number Diff line number Diff line
@@ -23,8 +23,11 @@
namespace android {

class OmxInfoBuilder : public MediaCodecListBuilderBase {
private:
    bool mAllowSurfaceEncoders; // allow surface encoders

public:
    OmxInfoBuilder();
    explicit OmxInfoBuilder(bool allowSurfaceEncoders);
    ~OmxInfoBuilder() override = default;
    status_t buildMediaCodecList(MediaCodecListWriter* writer) override;
};
+1 −4
Original line number Diff line number Diff line
@@ -61,10 +61,7 @@ OmxStore::OmxStore(
        role.role = rolePair.first;
        role.type = rolePair.second.type;
        role.isEncoder = rolePair.second.isEncoder;
        // TODO: Currently, preferPlatformNodes information is not available in
        // the xml file. Once we have a way to provide this information, it
        // should be parsed properly.
        role.preferPlatformNodes = rolePair.first.compare(0, 5, "audio") == 0;
        role.preferPlatformNodes = false; // deprecated and ignored, using rank instead
        hidl_vec<NodeInfo>& nodeList = role.nodes;
        nodeList.resize(rolePair.second.nodeList.size());
        size_t j = 0;