media: expand media_codecs.xml to support variants and domains

Added support to variant, domain and enabled attributes for <MediaCodec>.
Added support for <Variant> tag.

Bug: 129710438
Change-Id: Icf04665fdf36d8a3e9a74918978b0eb03f0e4f6c
diff --git a/media/codec2/sfplugin/Codec2InfoBuilder.cpp b/media/codec2/sfplugin/Codec2InfoBuilder.cpp
index ead0a9b..5231b63 100644
--- a/media/codec2/sfplugin/Codec2InfoBuilder.cpp
+++ b/media/codec2/sfplugin/Codec2InfoBuilder.cpp
@@ -248,10 +248,9 @@
     // Obtain Codec2Client
     std::vector<Traits> traits = Codec2Client::ListComponents();
 
-    MediaCodecsXmlParser parser(
-            MediaCodecsXmlParser::defaultSearchDirs,
-            "media_codecs_c2.xml",
-            "media_codecs_performance_c2.xml");
+    MediaCodecsXmlParser parser;
+    parser.parseXmlFilesInSearchDirs(
+            { "media_codecs_c2.xml", "media_codecs_performance_c2.xml" });
     if (parser.getParsingStatus() != OK) {
         ALOGD("XML parser no good");
         return OK;
diff --git a/media/libstagefright/foundation/include/media/stagefright/foundation/ADebug.h b/media/libstagefright/foundation/include/media/stagefright/foundation/ADebug.h
index a8b88fd..ab17a02 100644
--- a/media/libstagefright/foundation/include/media/stagefright/foundation/ADebug.h
+++ b/media/libstagefright/foundation/include/media/stagefright/foundation/ADebug.h
@@ -53,9 +53,11 @@
 #define LITERAL_TO_STRING_INTERNAL(x)    #x
 #define LITERAL_TO_STRING(x) LITERAL_TO_STRING_INTERNAL(x)
 
-#ifdef CHECK
-#undef CHECK
-#endif
+// allow to use CHECK_OP from android-base/logging.h
+// TODO: longterm replace this with android-base/logging.h, but there are some nuances, e.g.
+// android-base CHECK_OP requires a copy constructor, whereas we don't.
+#ifndef CHECK_OP
+
 #define CHECK(condition)                                \
     LOG_ALWAYS_FATAL_IF(                                \
             !(condition),                               \
@@ -63,10 +65,6 @@
             __FILE__ ":" LITERAL_TO_STRING(__LINE__)    \
             " CHECK(" #condition ") failed.")
 
-#ifdef CHECK_OP
-#undef CHECK_OP
-#endif
-
 #define CHECK_OP(x,y,suffix,op)                                         \
     do {                                                                \
         const auto &a = x;                                              \
@@ -82,15 +80,6 @@
         }                                                               \
     } while (false)
 
-#ifdef CHECK_EQ
-#undef CHECK_EQ
-#undef CHECK_NE
-#undef CHECK_LE
-#undef CHECK_LT
-#undef CHECK_GE
-#undef CHECK_GT
-#endif
-
 #define CHECK_EQ(x,y)   CHECK_OP(x,y,EQ,==)
 #define CHECK_NE(x,y)   CHECK_OP(x,y,NE,!=)
 #define CHECK_LE(x,y)   CHECK_OP(x,y,LE,<=)
@@ -98,6 +87,8 @@
 #define CHECK_GE(x,y)   CHECK_OP(x,y,GE,>=)
 #define CHECK_GT(x,y)   CHECK_OP(x,y,GT,>)
 
+#endif
+
 #define TRESPASS(...) \
         LOG_ALWAYS_FATAL(                                       \
             __FILE__ ":" LITERAL_TO_STRING(__LINE__)            \
diff --git a/media/libstagefright/omx/1.0/Omx.cpp b/media/libstagefright/omx/1.0/Omx.cpp
index 121bb1a..556a694 100644
--- a/media/libstagefright/omx/1.0/Omx.cpp
+++ b/media/libstagefright/omx/1.0/Omx.cpp
@@ -45,6 +45,8 @@
 Omx::Omx() :
     mMaster(new OMXMaster()),
     mParser() {
+    (void)mParser.parseXmlFilesInSearchDirs();
+    (void)mParser.parseXmlPath(mParser.defaultProfilingResultsXmlPath);
 }
 
 Omx::~Omx() {
diff --git a/media/libstagefright/omx/1.0/OmxStore.cpp b/media/libstagefright/omx/1.0/OmxStore.cpp
index 89292e8..67f478e 100644
--- a/media/libstagefright/omx/1.0/OmxStore.cpp
+++ b/media/libstagefright/omx/1.0/OmxStore.cpp
@@ -38,9 +38,8 @@
 OmxStore::OmxStore(
         const sp<IOmx> &omx,
         const char* owner,
-        const char* const* searchDirs,
-        const char* mainXmlName,
-        const char* performanceXmlName,
+        const std::vector<std::string> &searchDirs,
+        const std::vector<std::string> &xmlNames,
         const char* profilingResultsXmlPath) {
     // retrieve list of omx nodes
     std::set<std::string> nodes;
@@ -55,10 +54,11 @@
         });
     }
 
-    MediaCodecsXmlParser parser(searchDirs,
-            mainXmlName,
-            performanceXmlName,
-            profilingResultsXmlPath);
+    MediaCodecsXmlParser parser;
+    parser.parseXmlFilesInSearchDirs(xmlNames, searchDirs);
+    if (profilingResultsXmlPath != nullptr) {
+        parser.parseXmlPath(profilingResultsXmlPath);
+    }
     mParsingStatus = toStatus(parser.getParsingStatus());
 
     const auto& serviceAttributeMap = parser.getServiceAttributeMap();
diff --git a/media/libstagefright/omx/include/media/stagefright/omx/1.0/OmxStore.h b/media/libstagefright/omx/include/media/stagefright/omx/1.0/OmxStore.h
index 0fcea48..15d46991 100644
--- a/media/libstagefright/omx/include/media/stagefright/omx/1.0/OmxStore.h
+++ b/media/libstagefright/omx/include/media/stagefright/omx/1.0/OmxStore.h
@@ -46,14 +46,12 @@
     OmxStore(
             const sp<IOmx> &omx = nullptr,
             const char* owner = "default",
-            const char* const* searchDirs
-                = MediaCodecsXmlParser::defaultSearchDirs,
-            const char* mainXmlName
-                = MediaCodecsXmlParser::defaultMainXmlName,
-            const char* performanceXmlName
-                = MediaCodecsXmlParser::defaultPerformanceXmlName,
-            const char* profilingResultsXmlPath
-                = MediaCodecsXmlParser::defaultProfilingResultsXmlPath);
+            const std::vector<std::string> &searchDirs =
+                MediaCodecsXmlParser::getDefaultSearchDirs(),
+            const std::vector<std::string> &xmlFiles =
+                MediaCodecsXmlParser::getDefaultXmlNames(),
+            const char *xmlProfilingResultsPath =
+                MediaCodecsXmlParser::defaultProfilingResultsXmlPath);
 
     virtual ~OmxStore();
 
diff --git a/media/libstagefright/xmlparser/Android.bp b/media/libstagefright/xmlparser/Android.bp
index 202e964..38de831 100644
--- a/media/libstagefright/xmlparser/Android.bp
+++ b/media/libstagefright/xmlparser/Android.bp
@@ -20,15 +20,12 @@
     ],
 
     shared_libs: [
+        "libbase",
         "libexpat",
         "liblog",
         "libstagefright_omx_utils",
     ],
 
-    header_libs: [
-        "libbase_headers",
-    ],
-
     cflags: [
         "-Werror",
         "-Wall",
diff --git a/media/libstagefright/xmlparser/MediaCodecsXmlParser.cpp b/media/libstagefright/xmlparser/MediaCodecsXmlParser.cpp
index 7046f61..6a1b9a8 100644
--- a/media/libstagefright/xmlparser/MediaCodecsXmlParser.cpp
+++ b/media/libstagefright/xmlparser/MediaCodecsXmlParser.cpp
@@ -19,12 +19,18 @@
 
 #include <media/stagefright/xmlparser/MediaCodecsXmlParser.h>
 
+#include <android-base/logging.h>
 #include <android-base/macros.h>
 #include <utils/Log.h>
+
 #include <media/stagefright/MediaErrors.h>
+#include <media/stagefright/foundation/ADebug.h>
 #include <media/stagefright/omx/OMXUtils.h>
-#include <sys/stat.h>
+
 #include <expat.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
 
 #include <algorithm>
 #include <cctype>
@@ -32,8 +38,15 @@
 
 namespace android {
 
+using MCXP = MediaCodecsXmlParser;
+
 namespace {
 
+bool fileExists(const std::string &path) {
+    struct stat fileStat;
+    return stat(path.c_str(), &fileStat) == 0 && S_ISREG(fileStat.st_mode);
+}
+
 /**
  * Search for a file in a list of search directories.
  *
@@ -44,7 +57,7 @@
  * search continues until the `nullptr` element in `searchDirs` is reached, at
  * which point the function returns `false`.
  *
- * \param[in] searchDirs Null-terminated array of search paths.
+ * \param[in] searchDirs array of search paths.
  * \param[in] fileName Name of the file to search.
  * \param[out] outPath Full path of the file. `outPath` will hold a valid file
  * name if the return value of this function is `true`.
@@ -52,14 +65,13 @@
  * valid file name; `false` otherwise.
  */
 bool findFileInDirs(
-        const char* const* searchDirs,
-        const char *fileName,
+        const std::vector<std::string> &searchDirs,
+        const std::string &fileName,
         std::string *outPath) {
-    for (; *searchDirs != nullptr; ++searchDirs) {
-        *outPath = std::string(*searchDirs) + "/" + fileName;
-        struct stat fileStat;
-        if (stat(outPath->c_str(), &fileStat) == 0 &&
-                S_ISREG(fileStat.st_mode)) {
+    for (const std::string &searchDir : searchDirs) {
+        std::string path = searchDir + "/" + fileName;
+        if (fileExists(path)) {
+            *outPath = path;
             return true;
         }
     }
@@ -85,123 +97,435 @@
 bool parseBoolean(const char* s) {
     return striEq(s, "y") ||
             striEq(s, "yes") ||
+            striEq(s, "enabled") ||
             striEq(s, "t") ||
             striEq(s, "true") ||
             striEq(s, "1");
 }
 
-status_t limitFoundMissingAttr(const char* name, const char *attr, bool found = true) {
-    ALOGE("limit '%s' with %s'%s' attribute", name,
-            (found ? "" : "no "), attr);
-    return BAD_VALUE;
+
+status_t combineStatus(status_t a, status_t b) {
+    if (a == NO_INIT) {
+        return b;
+    } else if ((a == OK && (b == NAME_NOT_FOUND || b == ALREADY_EXISTS || b == NO_INIT))
+            || (b == OK && (a == NAME_NOT_FOUND || a == ALREADY_EXISTS))) {
+        // ignore NAME_NOT_FOUND and ALREADY_EXIST errors as long as the other error is OK
+        // also handle OK + NO_INIT here
+        return OK;
+    } else {
+        // prefer the first error result
+        return a ? : b;
+    }
 }
 
-status_t limitError(const char* name, const char *msg) {
-    ALOGE("limit '%s' %s", name, msg);
-    return BAD_VALUE;
+MCXP::StringSet parseCommaSeparatedStringSet(const char *s) {
+    MCXP::StringSet result;
+    for (const char *ptr = s ? : ""; *ptr; ) {
+        const char *end = strchrnul(ptr, ',');
+        if (ptr != end) { // skip empty values
+            result.emplace(ptr, end - ptr);
+        }
+        ptr = end + ('\0' != *end);
+    }
+    return result;
 }
 
-status_t limitInvalidAttr(const char* name, const char* attr, const char* value) {
-    ALOGE("limit '%s' with invalid '%s' attribute (%s)", name,
-            attr, value);
-    return BAD_VALUE;
-}
+#define PLOGD(msg, ...) \
+        ALOGD(msg " at line %zu of %s", ##__VA_ARGS__, \
+                (size_t)::XML_GetCurrentLineNumber(mParser.get()), mPath.c_str());
 
-}; // unnamed namespace
+}  // unnamed namespace
 
-constexpr char const* MediaCodecsXmlParser::defaultSearchDirs[];
-constexpr char const* MediaCodecsXmlParser::defaultMainXmlName;
-constexpr char const* MediaCodecsXmlParser::defaultPerformanceXmlName;
+struct MediaCodecsXmlParser::Impl {
+    // status + error message
+    struct Result {
+    private:
+        status_t mStatus;
+        std::string mError;
+
+    public:
+        Result(status_t s, std::string error = "")
+            : mStatus(s),
+              mError(error) {
+            if (error.empty() && s) {
+                error = "Failed (" + std::string(asString(s)) + ")";
+            }
+        }
+        operator status_t() const { return mStatus; }
+        std::string error() const { return mError; }
+    };
+
+
+    // Parsed data
+    struct Data {
+        // Service attributes
+        AttributeMap mServiceAttributeMap;
+        CodecMap mCodecMap;
+        Result addGlobal(std::string key, std::string value, bool updating);
+    };
+
+    enum Section {
+        SECTION_TOPLEVEL,
+        SECTION_SETTINGS,
+        SECTION_DECODERS,
+        SECTION_DECODER,
+        SECTION_DECODER_TYPE,
+        SECTION_ENCODERS,
+        SECTION_ENCODER,
+        SECTION_ENCODER_TYPE,
+        SECTION_INCLUDE,
+        SECTION_VARIANT,
+        SECTION_UNKNOWN,
+    };
+
+    // XML parsing state
+    struct State {
+    private:
+        Data *mData;
+
+        // current codec and/or type, plus whether we are updating
+        struct CodecAndType {
+            std::string mName;
+            CodecMap::iterator mCodec;
+            TypeMap::iterator mType;
+            bool mUpdating;
+        };
+
+        // using vectors as we need to reset their sizes
+        std::vector<std::string> mIncludeStack;
+        std::vector<Section> mSectionStack;
+        std::vector<StringSet> mVariantsStack;
+        std::vector<CodecAndType> mCurrent;
+
+    public:
+        State(Data *data);
+
+        Data &data() { return *mData; }
+
+        // used to restore parsing state at XML include boundaries, in case parsing the included
+        // file fails.
+        struct RestorePoint {
+            size_t numIncludes;
+            size_t numSections;
+            size_t numVariantSets;
+            size_t numCodecAndTypes;
+        };
+
+        // method manipulating restore points (all state stacks)
+        RestorePoint createRestorePoint() const {
+            return {
+                mIncludeStack.size(), mSectionStack.size(), mVariantsStack.size(), mCurrent.size()
+            };
+        }
+
+        void restore(RestorePoint rp) {
+            CHECK_GE(mIncludeStack.size(), rp.numIncludes);
+            CHECK_GE(mSectionStack.size(), rp.numSections);
+            CHECK_GE(mVariantsStack.size(), rp.numVariantSets);
+            CHECK_GE(mCurrent.size(), rp.numCodecAndTypes);
+
+            mIncludeStack.resize(rp.numIncludes);
+            mSectionStack.resize(rp.numSections);
+            mVariantsStack.resize(rp.numVariantSets);
+            mCurrent.resize(rp.numCodecAndTypes);
+        }
+
+        // methods manipulating the include stack
+        Result enterInclude(const std::string &path);
+        void exitInclude() {
+            mIncludeStack.pop_back();
+        }
+
+        // methods manipulating the codec/type stack/state
+        bool inCodec() const {
+            return !mCurrent.empty() && mCurrent.back().mCodec != mData->mCodecMap.end();
+        }
+
+        bool inType() const {
+            return inCodec()
+                    && mCurrent.back().mType != mCurrent.back().mCodec->second.typeMap.end();
+        }
+
+        Result enterMediaCodec(bool encoder, const char *name, const char *type, bool update);
+        Result enterType(const char *name, bool update);
+        void exitCodecOrType() {
+            mCurrent.pop_back();
+        }
+
+        // can only be called when inCodec()
+        MediaCodecsXmlParser::CodecProperties &codec() {
+            return mCurrent.back().mCodec->second;
+        }
+        // can only be called when inCodec()
+        std::string codecName() const {
+            return mCurrent.back().mName;
+        }
+        // can only be called when inCodec()
+        bool updating() const {
+            return mCurrent.back().mUpdating;
+        }
+        // can only be called when inType()
+        MediaCodecsXmlParser::AttributeMap &type() {
+            return mCurrent.back().mType->second;
+        }
+
+        // methods manipulating the section stack
+        Section section() const {
+            return mSectionStack.back();
+        }
+        Section lastNonIncludeSection() const;
+        void enterSection(Section s) {
+            mSectionStack.push_back(s);
+        }
+        void exitSection() {
+            mSectionStack.pop_back();
+            CHECK(!mSectionStack.empty());
+        }
+
+        // methods manipulating the variants stack
+        StringSet variants() const {
+            return mVariantsStack.back();
+        }
+        void enterVariants(StringSet variants) {
+            mVariantsStack.push_back(variants);
+        }
+        void exitVariants() {
+            mVariantsStack.pop_back();
+        }
+
+        // utility methods
+
+        // updates rank, domains, variants and enabledness on the current codec/type
+        Result updateCodec(
+                const char *rank, StringSet domains, StringSet variants, const char *enabled);
+        // adds a key-value attribute detail to the current type of the current codec
+        void addDetail(const std::string &key, const std::string &value);
+    };
+
+    /** XML Parser (state) */
+    struct Parser {
+        State *mState;
+
+        Parser(State *state, std::string path);
+
+        // keep track of the parser state
+        std::shared_ptr<XML_ParserStruct> mParser;
+        std::string mPath;
+        std::string mHrefBase;
+        status_t mStatus;
+
+        void parseXmlFile();
+
+        // XML parser callbacks
+        static void StartElementHandlerWrapper(void *me, const char *name, const char **attrs);
+        static void EndElementHandlerWrapper(void *me, const char *name);
+
+        void startElementHandler(const char *name, const char **attrs);
+        void endElementHandler(const char *name);
+
+        void updateStatus(status_t status);
+        void logAnyErrors(const Result &status) const;
+        status_t getStatus() const { return mStatus; }
+
+        status_t addAlias(const char **attrs);
+        status_t addFeature(const char **attrs);
+        status_t addLimit(const char **attrs);
+        status_t addQuirk(const char **attrs, const char *prefix = nullptr);
+        status_t addSetting(const char **attrs, const char *prefix = nullptr);
+        status_t enterMediaCodec(const char **attrs, bool encoder);
+        status_t enterType(const char **attrs);
+        status_t includeXmlFile(const char **attrs);
+        status_t limitVariants(const char **attrs);
+
+        status_t updateMediaCodec(
+                const char *rank, const StringSet &domain, const StringSet &variants,
+                const char *enabled);
+    };
+
+    status_t parseXmlFilesInSearchDirs(
+        const std::vector<std::string> &fileNames,
+        const std::vector<std::string> &searchDirs);
+
+    status_t parseXmlPath(const std::string &path);
+
+    // Computed longest common prefix
+    Data mData;
+    State mState;
+
+    // Role map
+    mutable std::string mCommonPrefix;
+    mutable RoleMap mRoleMap;
+    mutable std::mutex mLock;
+
+    status_t mParsingStatus;
+
+    Impl()
+        : mState(&mData),
+          mParsingStatus(NO_INIT) {
+    }
+
+    void generateRoleMap() const;
+    void generateCommonPrefix() const;
+
+    const AttributeMap& getServiceAttributeMap() const {
+        std::lock_guard<std::mutex> guard(mLock);
+        return mData.mServiceAttributeMap;
+    }
+
+    const CodecMap& getCodecMap() const {
+        std::lock_guard<std::mutex> guard(mLock);
+        return mData.mCodecMap;
+    }
+
+    const RoleMap& getRoleMap() const;
+    const char* getCommonPrefix() const;
+
+    status_t getParsingStatus() const {
+        std::lock_guard<std::mutex> guard(mLock);
+        return mParsingStatus;
+    }
+};
+
 constexpr char const* MediaCodecsXmlParser::defaultProfilingResultsXmlPath;
 
-MediaCodecsXmlParser::MediaCodecsXmlParser(
-        const char* const* searchDirs,
-        const char* mainXmlName,
-        const char* performanceXmlName,
-        const char* profilingResultsXmlPath) :
-    mParsingStatus(NO_INIT),
-    mUpdate(false),
-    mCodecCounter(0) {
-    std::string path;
-    if (findFileInDirs(searchDirs, mainXmlName, &path)) {
-        parseTopLevelXMLFile(path.c_str(), false);
-    } else {
-        ALOGE("Cannot find %s", mainXmlName);
-        mParsingStatus = NAME_NOT_FOUND;
-    }
-    if (findFileInDirs(searchDirs, performanceXmlName, &path)) {
-        parseTopLevelXMLFile(path.c_str(), true);
-    }
-    if (profilingResultsXmlPath != nullptr) {
-        parseTopLevelXMLFile(profilingResultsXmlPath, true);
-    }
+MediaCodecsXmlParser::MediaCodecsXmlParser()
+    : mImpl(new Impl()) {
 }
 
-bool MediaCodecsXmlParser::parseTopLevelXMLFile(
-        const char *codecs_xml,
-        bool ignore_errors) {
-    // get href_base
-    const char *href_base_end = strrchr(codecs_xml, '/');
-    if (href_base_end != nullptr) {
-        mHrefBase = std::string(codecs_xml, href_base_end - codecs_xml + 1);
-    }
+status_t MediaCodecsXmlParser::parseXmlFilesInSearchDirs(
+        const std::vector<std::string> &fileNames,
+        const std::vector<std::string> &searchDirs) {
+    return mImpl->parseXmlFilesInSearchDirs(fileNames, searchDirs);
+}
 
-    mParsingStatus = OK; // keeping this here for safety
-    mCurrentSection = SECTION_TOPLEVEL;
+status_t MediaCodecsXmlParser::parseXmlPath(const std::string &path) {
+    return mImpl->parseXmlPath(path);
+}
 
-    parseXMLFile(codecs_xml);
-
-    if (mParsingStatus != OK) {
-        ALOGW("parseTopLevelXMLFile(%s) failed", codecs_xml);
-        if (ignore_errors) {
-            mParsingStatus = OK;
-            return false;
+status_t MediaCodecsXmlParser::Impl::parseXmlFilesInSearchDirs(
+        const std::vector<std::string> &fileNames,
+        const std::vector<std::string> &searchDirs) {
+    status_t res = NO_INIT;
+    for (const std::string fileName : fileNames) {
+        status_t err = NO_INIT;
+        std::string path;
+        if (findFileInDirs(searchDirs, fileName, &path)) {
+            err = parseXmlPath(path);
+        } else {
+            ALOGD("Cannot find %s", path.c_str());
         }
-        mCodecMap.clear();
-        return false;
+        res = combineStatus(res, err);
     }
-    return true;
+    return res;
+}
+
+status_t MediaCodecsXmlParser::Impl::parseXmlPath(const std::string &path) {
+    std::lock_guard<std::mutex> guard(mLock);
+    if (!fileExists(path)) {
+        ALOGD("Cannot find %s", path.c_str());
+        mParsingStatus = combineStatus(mParsingStatus, NAME_NOT_FOUND);
+        return NAME_NOT_FOUND;
+    }
+
+    // save state (even though we should always be at toplevel here)
+    State::RestorePoint rp = mState.createRestorePoint();
+    Parser parser(&mState, path);
+    parser.parseXmlFile();
+    mState.restore(rp);
+
+    if (parser.getStatus() != OK) {
+        ALOGD("parseXmlPath(%s) failed with %s", path.c_str(), asString(parser.getStatus()));
+    }
+    mParsingStatus = combineStatus(mParsingStatus, parser.getStatus());
+    return parser.getStatus();
 }
 
 MediaCodecsXmlParser::~MediaCodecsXmlParser() {
 }
 
-void MediaCodecsXmlParser::parseXMLFile(const char *path) {
+MediaCodecsXmlParser::Impl::State::State(MediaCodecsXmlParser::Impl::Data *data)
+    : mData(data) {
+    mSectionStack.emplace_back(SECTION_TOPLEVEL);
+}
+
+MediaCodecsXmlParser::Impl::Section
+MediaCodecsXmlParser::Impl::State::lastNonIncludeSection() const {
+    for (auto it = mSectionStack.end(); it != mSectionStack.begin(); --it) {
+        if (it[-1] != SECTION_INCLUDE) {
+            return it[-1];
+        }
+    }
+    TRESPASS("must have non-include section");
+}
+
+void MediaCodecsXmlParser::Impl::Parser::updateStatus(status_t status) {
+    mStatus = combineStatus(mStatus, status);
+}
+
+void MediaCodecsXmlParser::Impl::Parser::logAnyErrors(const Result &status) const {
+    if (status) {
+        if (status.error().empty()) {
+            PLOGD("error %s", asString((status_t)status));
+        } else {
+            PLOGD("%s", status.error().c_str());
+        }
+    }
+}
+
+MediaCodecsXmlParser::Impl::Parser::Parser(State *state, std::string path)
+    : mState(state),
+      mPath(path),
+      mStatus(NO_INIT) {
+    // determine href_base
+    std::string::size_type end = path.rfind("/");
+    if (end != std::string::npos) {
+        mHrefBase = path.substr(0, end + 1);
+    }
+}
+
+void MediaCodecsXmlParser::Impl::Parser::parseXmlFile() {
+    const char *path = mPath.c_str();
+    ALOGD("parsing %s...", path);
     FILE *file = fopen(path, "r");
 
     if (file == nullptr) {
-        ALOGW("unable to open media codecs configuration xml file: %s", path);
-        mParsingStatus = NAME_NOT_FOUND;
+        ALOGD("unable to open media codecs configuration xml file: %s", path);
+        mStatus = NAME_NOT_FOUND;
         return;
     }
 
-    XML_Parser parser = ::XML_ParserCreate(nullptr);
-    LOG_FATAL_IF(parser == nullptr, "XML_MediaCodecsXmlParserCreate() failed.");
+    mParser = std::shared_ptr<XML_ParserStruct>(
+        ::XML_ParserCreate(nullptr),
+        [](XML_ParserStruct *parser) { ::XML_ParserFree(parser); });
+    LOG_FATAL_IF(!mParser, "XML_MediaCodecsXmlParserCreate() failed.");
 
-    ::XML_SetUserData(parser, this);
-    ::XML_SetElementHandler(
-            parser, StartElementHandlerWrapper, EndElementHandlerWrapper);
+    ::XML_SetUserData(mParser.get(), this);
+    ::XML_SetElementHandler(mParser.get(), StartElementHandlerWrapper, EndElementHandlerWrapper);
 
     static constexpr int BUFF_SIZE = 512;
-    while (mParsingStatus == OK) {
-        void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
+    // updateStatus(OK);
+    if (mStatus == NO_INIT) {
+        mStatus = OK;
+    }
+    while (mStatus == OK) {
+        void *buff = ::XML_GetBuffer(mParser.get(), BUFF_SIZE);
         if (buff == nullptr) {
-            ALOGE("failed in call to XML_GetBuffer()");
-            mParsingStatus = UNKNOWN_ERROR;
+            ALOGD("failed in call to XML_GetBuffer()");
+            mStatus = UNKNOWN_ERROR;
             break;
         }
 
         int bytes_read = ::fread(buff, 1, BUFF_SIZE, file);
         if (bytes_read < 0) {
-            ALOGE("failed in call to read");
-            mParsingStatus = ERROR_IO;
+            ALOGD("failed in call to read");
+            mStatus = ERROR_IO;
             break;
         }
 
-        XML_Status status = ::XML_ParseBuffer(parser, bytes_read, bytes_read == 0);
+        XML_Status status = ::XML_ParseBuffer(mParser.get(), bytes_read, bytes_read == 0);
         if (status != XML_STATUS_OK) {
-            ALOGE("malformed (%s)", ::XML_ErrorString(::XML_GetErrorCode(parser)));
-            mParsingStatus = ERROR_MALFORMED;
+            PLOGD("malformed (%s)", ::XML_ErrorString(::XML_GetErrorCode(mParser.get())));
+            mStatus = ERROR_MALFORMED;
             break;
         }
 
@@ -210,39 +534,47 @@
         }
     }
 
-    ::XML_ParserFree(parser);
+    mParser.reset();
 
     fclose(file);
     file = nullptr;
 }
 
 // static
-void MediaCodecsXmlParser::StartElementHandlerWrapper(
+void MediaCodecsXmlParser::Impl::Parser::StartElementHandlerWrapper(
         void *me, const char *name, const char **attrs) {
-    static_cast<MediaCodecsXmlParser*>(me)->startElementHandler(name, attrs);
+    static_cast<MediaCodecsXmlParser::Impl::Parser*>(me)->startElementHandler(name, attrs);
 }
 
 // static
-void MediaCodecsXmlParser::EndElementHandlerWrapper(void *me, const char *name) {
-    static_cast<MediaCodecsXmlParser*>(me)->endElementHandler(name);
+void MediaCodecsXmlParser::Impl::Parser::EndElementHandlerWrapper(void *me, const char *name) {
+    static_cast<MediaCodecsXmlParser::Impl::Parser*>(me)->endElementHandler(name);
 }
 
-status_t MediaCodecsXmlParser::includeXMLFile(const char **attrs) {
+status_t MediaCodecsXmlParser::Impl::Parser::includeXmlFile(const char **attrs) {
     const char *href = nullptr;
     size_t i = 0;
     while (attrs[i] != nullptr) {
-        if (strEq(attrs[i], "href")) {
-            if (attrs[++i] == nullptr) {
-                return BAD_VALUE;
-            }
-            href = attrs[i];
-        } else {
-            ALOGE("includeXMLFile: unrecognized attribute: %s", attrs[i]);
+        CHECK((i & 1) == 0);
+        if (attrs[i + 1] == nullptr) {
+            PLOGD("Include: attribute '%s' is null", attrs[i]);
             return BAD_VALUE;
         }
+
+        if (strEq(attrs[i], "href")) {
+            href = attrs[++i];
+        } else {
+            PLOGD("Include: ignoring unrecognized attribute '%s'", attrs[i]);
+            ++i;
+        }
         ++i;
     }
 
+    if (href == nullptr) {
+        PLOGD("Include with no 'href' attribute");
+        return BAD_VALUE;
+    }
+
     // For security reasons and for simplicity, file names can only contain
     // [a-zA-Z0-9_.] and must start with  media_codecs_ and end with .xml
     for (i = 0; href[i] != '\0'; i++) {
@@ -252,74 +584,114 @@
                 (href[i] >= 'a' && href[i] <= 'z')) {
             continue;
         }
-        ALOGE("invalid include file name: %s", href);
+        PLOGD("invalid include file name: %s", href);
         return BAD_VALUE;
     }
 
     std::string filename = href;
     if (filename.compare(0, 13, "media_codecs_") != 0 ||
             filename.compare(filename.size() - 4, 4, ".xml") != 0) {
-        ALOGE("invalid include file name: %s", href);
+        PLOGD("invalid include file name: %s", href);
         return BAD_VALUE;
     }
     filename.insert(0, mHrefBase);
 
-    status_t oldParsingStatus = mParsingStatus;
+    Result res = mState->enterInclude(filename);
+    if (res) {
+        logAnyErrors(res);
+        return res;
+    }
 
-    parseXMLFile(filename.c_str());
-
-    status_t newParsingStatus = mParsingStatus;
-    mParsingStatus = oldParsingStatus;
-    return newParsingStatus;
+    // save state so that we can resume even if XML parsing of the included file failed midway
+    State::RestorePoint rp = mState->createRestorePoint();
+    Parser parser(mState, filename);
+    parser.parseXmlFile();
+    mState->restore(rp);
+    mState->exitInclude();
+    return parser.getStatus();
 }
 
-void MediaCodecsXmlParser::startElementHandler(
+MediaCodecsXmlParser::Impl::Result
+MediaCodecsXmlParser::Impl::State::enterInclude(const std::string &fileName) {
+    if (std::find(mIncludeStack.begin(), mIncludeStack.end(), fileName)
+            != mIncludeStack.end()) {
+        return { BAD_VALUE, "recursive include chain" };
+    }
+    mIncludeStack.emplace_back(fileName);
+    return OK;
+}
+
+void MediaCodecsXmlParser::Impl::Parser::startElementHandler(
         const char *name, const char **attrs) {
     bool inType = true;
+    Result err = NO_INIT;
 
+    Section section = mState->section();
+
+    // handle include at any level
     if (strEq(name, "Include")) {
-        if (includeXMLFile(attrs) == OK) {
-            mSectionStack.push_back(mCurrentSection);
-            mCurrentSection = SECTION_INCLUDE;
-        }
+        mState->enterSection(SECTION_INCLUDE);
+        updateStatus(includeXmlFile(attrs));
         return;
     }
 
-    switch (mCurrentSection) {
+    // handle include section (top level)
+    if (section == SECTION_INCLUDE) {
+        if (strEq(name, "Included")) {
+            return;
+        }
+        // imitate prior level
+        section = mState->lastNonIncludeSection();
+    }
+
+    switch (section) {
         case SECTION_TOPLEVEL:
         {
+            Section nextSection;
             if (strEq(name, "Decoders")) {
-                mCurrentSection = SECTION_DECODERS;
+                nextSection = SECTION_DECODERS;
             } else if (strEq(name, "Encoders")) {
-                mCurrentSection = SECTION_ENCODERS;
+                nextSection = SECTION_ENCODERS;
             } else if (strEq(name, "Settings")) {
-                mCurrentSection = SECTION_SETTINGS;
+                nextSection = SECTION_SETTINGS;
+            } else if (strEq(name, "MediaCodecs")) {
+                return;
+            } else {
+                break;
             }
-            break;
+            mState->enterSection(nextSection);
+            return;
         }
 
         case SECTION_SETTINGS:
         {
             if (strEq(name, "Setting")) {
-                (void)addSettingFromAttributes(attrs);
+                err = addSetting(attrs);
+            } else if (strEq(name, "Variant")) {
+                err = addSetting(attrs, "variant-");
+            } else if (strEq(name, "Domain")) {
+                err = addSetting(attrs, "domain-");
+            } else {
+                break;
             }
-            break;
+            updateStatus(err);
+            return;
         }
 
         case SECTION_DECODERS:
-        {
-            if (strEq(name, "MediaCodec")) {
-                (void)addMediaCodecFromAttributes(false /* encoder */, attrs);
-                mCurrentSection = SECTION_DECODER;
-            }
-            break;
-        }
-
         case SECTION_ENCODERS:
         {
             if (strEq(name, "MediaCodec")) {
-                (void)addMediaCodecFromAttributes(true /* encoder */, attrs);
-                mCurrentSection = SECTION_ENCODER;
+                err = enterMediaCodec(attrs, section == SECTION_ENCODERS);
+                updateStatus(err);
+                if (err != OK) { // skip this element on error
+                    mState->enterSection(SECTION_UNKNOWN);
+                } else {
+                    mState->enterVariants(mState->codec().variantSet);
+                    mState->enterSection(
+                            section == SECTION_DECODERS ? SECTION_DECODER : SECTION_ENCODER);
+                }
+                return;
             }
             break;
         }
@@ -327,14 +699,21 @@
         case SECTION_DECODER:
         case SECTION_ENCODER:
         {
-            if (strEq(name, "Quirk") || strEq(name, "Attribute")) {
-                (void)addQuirk(attrs, name);
+            if (strEq(name, "Quirk")) {
+                err = addQuirk(attrs, "quirk::");
+            } else if (strEq(name, "Attribute")) {
+                err = addQuirk(attrs, "attribute::");
+            } else if (strEq(name, "Alias")) {
+                err = addAlias(attrs);
             } else if (strEq(name, "Type")) {
-                (void)addTypeFromAttributes(attrs,
-                        (mCurrentSection == SECTION_ENCODER));
-                mCurrentSection =
-                        (mCurrentSection == SECTION_DECODER ?
-                        SECTION_DECODER_TYPE : SECTION_ENCODER_TYPE);
+                err = enterType(attrs);
+                if (err != OK) { // skip this element on error
+                    mState->enterSection(SECTION_UNKNOWN);
+                } else {
+                    mState->enterSection(
+                            section == SECTION_DECODER
+                                    ? SECTION_DECODER_TYPE : SECTION_ENCODER_TYPE);
+                }
             }
         }
         inType = false;
@@ -342,91 +721,102 @@
 
         case SECTION_DECODER_TYPE:
         case SECTION_ENCODER_TYPE:
+        case SECTION_VARIANT:
         {
             // ignore limits and features specified outside of type
-            bool outside = !inType &&
-                    mCurrentType == mCurrentCodec->second.typeMap.end();
-            if (outside &&
-                    (strEq(name, "Limit") || strEq(name, "Feature"))) {
-                ALOGW("ignoring %s specified outside of a Type", name);
-            } else if (strEq(name, "Alias")) {
-                (void)addAlias(attrs);
+            if (!mState->inType()
+                    && (strEq(name, "Limit") || strEq(name, "Feature") || strEq(name, "Variant"))) {
+                PLOGD("ignoring %s specified outside of a Type", name);
+                return;
             } else if (strEq(name, "Limit")) {
-                (void)addLimit(attrs);
+                err = addLimit(attrs);
             } else if (strEq(name, "Feature")) {
-                (void)addFeature(attrs);
+                err = addFeature(attrs);
+            } else if (strEq(name, "Variant") && section != SECTION_VARIANT) {
+                err = limitVariants(attrs);
+                mState->enterSection(err == OK ? SECTION_VARIANT : SECTION_UNKNOWN);
+            } else if (inType
+                    && (strEq(name, "Alias") || strEq(name, "Attribute") || strEq(name, "Quirk"))) {
+                PLOGD("ignoring %s specified not directly in a MediaCodec", name);
+                return;
+            } else if (err == NO_INIT) {
+                break;
             }
-            break;
+            updateStatus(err);
+            return;
         }
 
         default:
             break;
     }
 
+    if (section != SECTION_UNKNOWN) {
+        PLOGD("Ignoring unrecognized tag <%s>", name);
+    }
+    mState->enterSection(SECTION_UNKNOWN);
 }
 
-void MediaCodecsXmlParser::endElementHandler(const char *name) {
-    switch (mCurrentSection) {
+void MediaCodecsXmlParser::Impl::Parser::endElementHandler(const char *name) {
+    // XMLParser handles tag matching, so we really just need to handle the section state here
+    Section section = mState->section();
+    switch (section) {
+        case SECTION_INCLUDE:
+        {
+            // this could also be any of: Included, MediaCodecs
+            if (strEq(name, "Include")) {
+                mState->exitSection();
+                return;
+            }
+            break;
+        }
+
         case SECTION_SETTINGS:
         {
+            // this could also be any of: Domain, Variant, Setting
             if (strEq(name, "Settings")) {
-                mCurrentSection = SECTION_TOPLEVEL;
+                mState->exitSection();
             }
             break;
         }
 
         case SECTION_DECODERS:
-        {
-            if (strEq(name, "Decoders")) {
-                mCurrentSection = SECTION_TOPLEVEL;
-            }
-            break;
-        }
-
         case SECTION_ENCODERS:
+        case SECTION_UNKNOWN:
         {
-            if (strEq(name, "Encoders")) {
-                mCurrentSection = SECTION_TOPLEVEL;
-            }
+            mState->exitSection();
             break;
         }
 
         case SECTION_DECODER_TYPE:
         case SECTION_ENCODER_TYPE:
         {
+            // this could also be any of: Alias, Limit, Feature
             if (strEq(name, "Type")) {
-                mCurrentSection =
-                        (mCurrentSection == SECTION_DECODER_TYPE ?
-                        SECTION_DECODER : SECTION_ENCODER);
-
-                mCurrentType = mCurrentCodec->second.typeMap.end();
+                mState->exitSection();
+                mState->exitCodecOrType();
             }
             break;
         }
 
         case SECTION_DECODER:
-        {
-            if (strEq(name, "MediaCodec")) {
-                mCurrentSection = SECTION_DECODERS;
-                mCurrentName.clear();
-            }
-            break;
-        }
-
         case SECTION_ENCODER:
         {
+            // this could also be any of: Alias, Limit, Quirk, Variant
             if (strEq(name, "MediaCodec")) {
-                mCurrentSection = SECTION_ENCODERS;
-                mCurrentName.clear();
+                mState->exitSection();
+                mState->exitCodecOrType();
+                mState->exitVariants();
             }
             break;
         }
 
-        case SECTION_INCLUDE:
+        case SECTION_VARIANT:
         {
-            if (strEq(name, "Include") && (mSectionStack.size() > 0)) {
-                mCurrentSection = mSectionStack.back();
-                mSectionStack.pop_back();
+            // this could also be any of: Alias, Limit, Quirk
+            if (strEq(name, "Variant")) {
+                mState->exitSection();
+                mState->exitVariants();
+                return;
             }
             break;
         }
@@ -434,264 +824,302 @@
         default:
             break;
     }
-
 }
 
-status_t MediaCodecsXmlParser::addSettingFromAttributes(const char **attrs) {
-    const char *name = nullptr;
-    const char *value = nullptr;
-    const char *update = nullptr;
+status_t MediaCodecsXmlParser::Impl::Parser::addSetting(const char **attrs, const char *prefix) {
+    const char *a_name = nullptr;
+    const char *a_value = nullptr;
+    const char *a_update = nullptr;
+    bool isBoolean = false;
 
     size_t i = 0;
     while (attrs[i] != nullptr) {
-        if (strEq(attrs[i], "name")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addSettingFromAttributes: name is null");
-                return BAD_VALUE;
-            }
-            name = attrs[i];
-        } else if (strEq(attrs[i], "value")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addSettingFromAttributes: value is null");
-                return BAD_VALUE;
-            }
-            value = attrs[i];
-        } else if (strEq(attrs[i], "update")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addSettingFromAttributes: update is null");
-                return BAD_VALUE;
-            }
-            update = attrs[i];
-        } else {
-            ALOGE("addSettingFromAttributes: unrecognized attribute: %s", attrs[i]);
+        CHECK((i & 1) == 0);
+        if (attrs[i + 1] == nullptr) {
+            PLOGD("Setting: attribute '%s' is null", attrs[i]);
             return BAD_VALUE;
         }
+
+        if (strEq(attrs[i], "name")) {
+            a_name = attrs[++i];
+        } else if (strEq(attrs[i], "value") || strEq(attrs[i], "enabled")) {
+            if (a_value) {
+                PLOGD("Setting: redundant attribute '%s'", attrs[i]);
+                return BAD_VALUE;
+            }
+            isBoolean = strEq(attrs[i], "enabled");
+            a_value = attrs[++i];
+        } else if (strEq(attrs[i], "update")) {
+            a_update = attrs[++i];
+        } else {
+            PLOGD("Setting: ignoring unrecognized attribute '%s'", attrs[i]);
+            ++i;
+        }
         ++i;
     }
 
-    if (name == nullptr || value == nullptr) {
-        ALOGE("addSettingFromAttributes: name or value unspecified");
+    if (a_name == nullptr || a_value == nullptr) {
+        PLOGD("Setting with no 'name' or 'value' attribute");
         return BAD_VALUE;
     }
 
     // Boolean values are converted to "0" or "1".
-    if (strHasPrefix(name, "supports-")) {
-        value = parseBoolean(value) ? "1" : "0";
+    if (strHasPrefix(a_name, "supports-") || isBoolean) {
+        a_value = parseBoolean(a_value) ? "1" : "0";
     }
 
-    mUpdate = (update != nullptr) && parseBoolean(update);
-    auto attribute = mServiceAttributeMap.find(name);
+    bool update = (a_update != nullptr) && parseBoolean(a_update);
+    Result res = mState->data().addGlobal(std::string(prefix ? : "") + a_name, a_value, update);
+    if (res != OK) {
+        PLOGD("Setting: %s", res.error().c_str());
+    }
+    return res;
+}
+
+MediaCodecsXmlParser::Impl::Result MediaCodecsXmlParser::Impl::Data::addGlobal(
+        std::string key, std::string value, bool updating) {
+    auto attribute = mServiceAttributeMap.find(key);
     if (attribute == mServiceAttributeMap.end()) { // New attribute name
-        if (mUpdate) {
-            ALOGE("addSettingFromAttributes: updating non-existing setting");
-            return BAD_VALUE;
+        if (updating) {
+            return { NAME_NOT_FOUND, "cannot update non-existing setting" };
         }
-        mServiceAttributeMap.insert(Attribute(name, value));
+        mServiceAttributeMap.insert(Attribute(key, value));
     } else { // Existing attribute name
-        if (!mUpdate) {
-            ALOGE("addSettingFromAttributes: adding existing setting");
-        }
         attribute->second = value;
+        if (!updating) {
+            return { ALREADY_EXISTS, "updating existing setting" };
+        }
     }
 
     return OK;
 }
 
-status_t MediaCodecsXmlParser::addMediaCodecFromAttributes(
-        bool encoder, const char **attrs) {
-    const char *name = nullptr;
-    const char *type = nullptr;
-    const char *update = nullptr;
-    const char *rank = nullptr;
+status_t MediaCodecsXmlParser::Impl::Parser::enterMediaCodec(
+        const char **attrs, bool encoder) {
+    const char *a_name = nullptr;
+    const char *a_type = nullptr;
+    const char *a_update = nullptr;
+    const char *a_rank = nullptr;
+    const char *a_domain = nullptr;
+    const char *a_variant = nullptr;
+    const char *a_enabled = nullptr;
 
     size_t i = 0;
     while (attrs[i] != nullptr) {
-        if (strEq(attrs[i], "name")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addMediaCodecFromAttributes: name is null");
-                return BAD_VALUE;
-            }
-            name = attrs[i];
-        } else if (strEq(attrs[i], "type")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addMediaCodecFromAttributes: type is null");
-                return BAD_VALUE;
-            }
-            type = attrs[i];
-        } else if (strEq(attrs[i], "update")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addMediaCodecFromAttributes: update is null");
-                return BAD_VALUE;
-            }
-            update = attrs[i];
-        } else if (strEq(attrs[i], "rank")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addMediaCodecFromAttributes: rank is null");
-                return BAD_VALUE;
-            }
-            rank = attrs[i];
-        } else {
-            ALOGE("addMediaCodecFromAttributes: unrecognized attribute: %s", attrs[i]);
+        CHECK((i & 1) == 0);
+        if (attrs[i + 1] == nullptr) {
+            PLOGD("MediaCodec: attribute '%s' is null", attrs[i]);
             return BAD_VALUE;
         }
+
+        if (strEq(attrs[i], "name")) {
+            a_name = attrs[++i];
+        } else if (strEq(attrs[i], "type")) {
+            a_type = attrs[++i];
+        } else if (strEq(attrs[i], "update")) {
+            a_update = attrs[++i];
+        } else if (strEq(attrs[i], "rank")) {
+            a_rank = attrs[++i];
+        } else if (strEq(attrs[i], "domain")) {
+            a_domain = attrs[++i];
+        } else if (strEq(attrs[i], "variant")) {
+            a_variant = attrs[++i];
+        } else if (strEq(attrs[i], "enabled")) {
+            a_enabled = attrs[++i];
+        } else {
+            PLOGD("MediaCodec: ignoring unrecognized attribute '%s'", attrs[i]);
+            ++i;
+        }
         ++i;
     }
 
-    if (name == nullptr) {
-        ALOGE("addMediaCodecFromAttributes: name not found");
+    if (a_name == nullptr) {
+        PLOGD("MediaCodec with no 'name' attribute");
         return BAD_VALUE;
     }
 
-    mUpdate = (update != nullptr) && parseBoolean(update);
-    mCurrentCodec = mCodecMap.find(name);
-    if (mCurrentCodec == mCodecMap.end()) { // New codec name
-        if (mUpdate) {
-            ALOGW("addMediaCodecFromAttributes: cannot update "
-                  "non-existing codec \"%s\".", name);
-            return BAD_VALUE;
+    bool update = (a_update != nullptr) && parseBoolean(a_update);
+    if (a_domain != nullptr) {
+        // disable codecs with domain by default (unless updating)
+        if (!a_enabled && !update) {
+            a_enabled = "false";
+        }
+    }
+
+    Result res = mState->enterMediaCodec(encoder, a_name, a_type, update);
+    if (res != OK) {
+        logAnyErrors(res);
+        return res;
+    }
+
+    return updateMediaCodec(
+            a_rank, parseCommaSeparatedStringSet(a_domain),
+            parseCommaSeparatedStringSet(a_variant), a_enabled);
+}
+
+MediaCodecsXmlParser::Impl::Result
+MediaCodecsXmlParser::Impl::State::enterMediaCodec(
+        bool encoder, const char *name, const char *type, bool updating) {
+    // store name even in case of an error
+    CodecMap::iterator codecIt = mData->mCodecMap.find(name);
+    TypeMap::iterator typeIt;
+    if (codecIt == mData->mCodecMap.end()) { // New codec name
+        if (updating) {
+            return { NAME_NOT_FOUND, "MediaCodec: cannot update non-existing codec" };
         }
         // Create a new codec in mCodecMap
-        mCurrentCodec = mCodecMap.insert(
-                Codec(name, CodecProperties())).first;
+        codecIt = mData->mCodecMap.insert(Codec(name, CodecProperties())).first;
         if (type != nullptr) {
-            mCurrentType = mCurrentCodec->second.typeMap.insert(
-                    Type(type, AttributeMap())).first;
+            typeIt = codecIt->second.typeMap.insert(Type(type, AttributeMap())).first;
         } else {
-            mCurrentType = mCurrentCodec->second.typeMap.end();
+            typeIt = codecIt->second.typeMap.end();
         }
-        mCurrentCodec->second.isEncoder = encoder;
-        mCurrentCodec->second.order = mCodecCounter++;
+        codecIt->second.isEncoder = encoder;
+        codecIt->second.order = mData->mCodecMap.size();
     } else { // Existing codec name
-        if (!mUpdate) {
-            ALOGW("addMediaCodecFromAttributes: trying to add "
-                  "existing codec \"%s\"", name);
-            return ALREADY_EXISTS;
+        if (!updating) {
+            return { ALREADY_EXISTS, "MediaCodec: cannot add existing codec" };
         }
         if (type != nullptr) {
-            mCurrentType = mCurrentCodec->second.typeMap.find(type);
-            if (mCurrentType == mCurrentCodec->second.typeMap.end()) {
-                ALOGE("addMediaCodecFromAttributes: cannot update "
-                      "non-existing type \"%s\" for codec \"%s\"",
-                        type, name);
-                return BAD_VALUE;
+            typeIt = codecIt->second.typeMap.find(type);
+            if (typeIt == codecIt->second.typeMap.end()) {
+                return { NAME_NOT_FOUND, "MediaCodec: cannot update non-existing type for codec" };
             }
         } else {
             // This should happen only when the codec has at most one type.
-            mCurrentType = mCurrentCodec->second.typeMap.begin();
-            if (mCurrentType == mCurrentCodec->second.typeMap.end()) {
-                ALOGE("addMediaCodecFromAttributes: cannot update "
-                      "codec \"%s\" without type specified", name);
-                return BAD_VALUE;
+            typeIt = codecIt->second.typeMap.begin();
+            if (typeIt == codecIt->second.typeMap.end()
+                    || codecIt->second.typeMap.size() != 1) {
+                return { BAD_VALUE, "MediaCodec: cannot update codec without type specified" };
             }
         }
     }
+    mCurrent.emplace_back(CodecAndType{name, codecIt, typeIt, updating});
+    return OK;
+}
+
+status_t MediaCodecsXmlParser::Impl::Parser::updateMediaCodec(
+        const char *rank, const StringSet &domains, const StringSet &variants,
+        const char *enabled) {
+    CHECK(mState->inCodec());
+    CodecProperties &codec = mState->codec();
 
     if (rank != nullptr) {
-        if (!mCurrentCodec->second.rank.empty() && mCurrentCodec->second.rank != rank) {
-            ALOGE("addMediaCodecFromAttributes: code \"%s\" rank changed from \"%s\" to \"%s\"",
-                    name, mCurrentCodec->second.rank.c_str(), rank);
-            return BAD_VALUE;
-        }
-        mCurrentCodec->second.rank = rank;
+        ALOGD_IF(!codec.rank.empty() && codec.rank != rank,
+                "codec '%s' rank changed from '%s' to '%s'",
+                mState->codecName().c_str(), codec.rank.c_str(), rank);
+        codec.rank = rank;
     }
 
+    codec.variantSet = variants;
+
+    for (const std::string &domain : domains) {
+        if (domain.size() && domain.at(0) == '!') {
+            codec.domainSet.erase(domain.substr(1));
+        } else {
+            codec.domainSet.emplace(domain);
+        }
+    }
+
+    if (enabled != nullptr) {
+        if (parseBoolean(enabled)) {
+            codec.quirkSet.erase("attribute::disabled");
+            ALOGD("enabling %s", mState->codecName().c_str());
+        } else {
+            codec.quirkSet.emplace("attribute::disabled");
+            ALOGD("disabling %s", mState->codecName().c_str());
+        }
+    }
     return OK;
 }
 
-status_t MediaCodecsXmlParser::addQuirk(const char **attrs, const char *tag) {
-    if (mCurrentCodec == mCodecMap.end()) {
-        return BAD_VALUE;
-    }
-
-    const char *name = nullptr;
+status_t MediaCodecsXmlParser::Impl::Parser::addQuirk(const char **attrs, const char *prefix) {
+    CHECK(mState->inCodec());
+    const char *a_name = nullptr;
 
     size_t i = 0;
     while (attrs[i] != nullptr) {
-        if (strEq(attrs[i], "name")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addQuirk: name is null");
-                return BAD_VALUE;
-            }
-            name = attrs[i];
-        } else {
-            ALOGE("addQuirk: unrecognized attribute: %s", attrs[i]);
+        CHECK((i & 1) == 0);
+        if (attrs[i + 1] == nullptr) {
+            PLOGD("Quirk: attribute '%s' is null", attrs[i]);
             return BAD_VALUE;
         }
+
+        if (strEq(attrs[i], "name")) {
+            a_name = attrs[++i];
+        } else {
+            PLOGD("Quirk: ignoring unrecognized attribute '%s'", attrs[i]);
+            ++i;
+        }
         ++i;
     }
 
-    if (name == nullptr) {
-        ALOGE("addQuirk: name not found");
+    if (a_name == nullptr) {
+        PLOGD("Quirk with no 'name' attribute");
         return BAD_VALUE;
     }
 
-    std::string tagString = tag;
-    std::transform(tagString.begin(), tagString.end(), tagString.begin(), ::tolower);
-    tagString.append("::");
-    tagString.append(name);
-    mCurrentCodec->second.quirkSet.emplace(tagString.c_str());
-    ALOGI("adding %s to %s", tagString.c_str(), mCurrentCodec->first.c_str());
+    std::string key = std::string(prefix ? : "") + a_name;
+    mState->codec().quirkSet.emplace(key);
+    ALOGV("adding %s to %s", key.c_str(), mState->codecName().c_str());
     return OK;
 }
 
-status_t MediaCodecsXmlParser::addTypeFromAttributes(const char **attrs, bool encoder) {
-    if (mCurrentCodec == mCodecMap.end()) {
-        return BAD_VALUE;
-    }
+status_t MediaCodecsXmlParser::Impl::Parser::enterType(const char **attrs) {
+    CHECK(mState->inCodec());
 
-    const char *name = nullptr;
-    const char *update = nullptr;
+    const char *a_name = nullptr;
+    const char *a_update = nullptr;
 
     size_t i = 0;
     while (attrs[i] != nullptr) {
+        CHECK((i & 1) == 0);
+        if (attrs[i + 1] == nullptr) {
+            PLOGD("Type: attribute '%s' is null", attrs[i]);
+            return BAD_VALUE;
+        }
+
         if (strEq(attrs[i], "name")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addTypeFromAttributes: name is null");
-                return BAD_VALUE;
-            }
-            name = attrs[i];
+            a_name = attrs[++i];
         } else if (strEq(attrs[i], "update")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addTypeFromAttributes: update is null");
-                return BAD_VALUE;
-            }
-            update = attrs[i];
+            a_update = attrs[++i];
         } else {
-            ALOGE("addTypeFromAttributes: unrecognized attribute: %s", attrs[i]);
-            return BAD_VALUE;
+            PLOGD("Type: ignoring unrecognized attribute '%s'", attrs[i]);
+            ++i;
         }
         ++i;
     }
 
-    if (name == nullptr) {
+    if (a_name == nullptr) {
+        PLOGD("Type with no 'name' attribute");
         return BAD_VALUE;
     }
 
-    mCurrentCodec->second.isEncoder = encoder;
-    mCurrentType = mCurrentCodec->second.typeMap.find(name);
-    if (!mUpdate) {
-        if (mCurrentType != mCurrentCodec->second.typeMap.end()) {
-            ALOGW("addTypeFromAttributes: trying to update "
-                  "existing type \"%s\"", name);
-            return ALREADY_EXISTS;
+    bool update = (a_update != nullptr) && parseBoolean(a_update);
+    return mState->enterType(a_name, update);
+}
+
+MediaCodecsXmlParser::Impl::Result
+MediaCodecsXmlParser::Impl::State::enterType(const char *name, bool update) {
+    update = update || updating(); // handle parent
+
+    CodecMap::iterator codecIt = mCurrent.back().mCodec;
+    TypeMap::iterator typeIt = codecIt->second.typeMap.find(name);
+    if (!update) {
+        if (typeIt != codecIt->second.typeMap.end()) {
+            return { ALREADY_EXISTS, "trying to update existing type '" + std::string(name) + "'" };
         }
-        mCurrentType = mCurrentCodec->second.typeMap.insert(
-                Type(name, AttributeMap())).first;
-    } else if (mCurrentType == mCurrentCodec->second.typeMap.end()) {
-        ALOGE("addTypeFromAttributes: updating non-existing type");
-        return BAD_VALUE;
+        typeIt = codecIt->second.typeMap.insert(Type(name, AttributeMap())).first;
+    } else if (typeIt == codecIt->second.typeMap.end()) {
+        return { NAME_NOT_FOUND, "addType: updating non-existing type" };
     }
+    mCurrent.push_back({ codecName(), codecIt, typeIt, update });
+    CHECK(inType());
     return OK;
 }
 
-status_t MediaCodecsXmlParser::addLimit(const char **attrs) {
-    if (mCurrentCodec == mCodecMap.end()) {
-        return BAD_VALUE;
-    }
-    if (mCurrentType == mCurrentCodec->second.typeMap.end()) {
-        return BAD_VALUE;
-    }
-
+status_t MediaCodecsXmlParser::Impl::Parser::addLimit(const char **attrs) {
+    CHECK(mState->inType());
     const char* a_name = nullptr;
     const char* a_default = nullptr;
     const char* a_in = nullptr;
@@ -704,69 +1132,39 @@
 
     size_t i = 0;
     while (attrs[i] != nullptr) {
-        if (strEq(attrs[i], "name")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addLimit: name is null");
-                return BAD_VALUE;
-            }
-            a_name = attrs[i];
-        } else if (strEq(attrs[i], "default")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addLimit: default is null");
-                return BAD_VALUE;
-            }
-            a_default = attrs[i];
-        } else if (strEq(attrs[i], "in")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addLimit: in is null");
-                return BAD_VALUE;
-            }
-            a_in = attrs[i];
-        } else if (strEq(attrs[i], "max")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addLimit: max is null");
-                return BAD_VALUE;
-            }
-            a_max = attrs[i];
-        } else if (strEq(attrs[i], "min")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addLimit: min is null");
-                return BAD_VALUE;
-            }
-            a_min = attrs[i];
-        } else if (strEq(attrs[i], "range")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addLimit: range is null");
-                return BAD_VALUE;
-            }
-            a_range = attrs[i];
-        } else if (strEq(attrs[i], "ranges")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addLimit: ranges is null");
-                return BAD_VALUE;
-            }
-            a_ranges = attrs[i];
-        } else if (strEq(attrs[i], "scale")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addLimit: scale is null");
-                return BAD_VALUE;
-            }
-            a_scale = attrs[i];
-        } else if (strEq(attrs[i], "value")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addLimit: value is null");
-                return BAD_VALUE;
-            }
-            a_value = attrs[i];
-        } else {
-            ALOGE("addLimit: unrecognized limit: %s", attrs[i]);
+        CHECK((i & 1) == 0);
+        if (attrs[i + 1] == nullptr) {
+            PLOGD("Limit: attribute '%s' is null", attrs[i]);
             return BAD_VALUE;
         }
+
+        if (strEq(attrs[i], "name")) {
+            a_name = attrs[++i];
+        } else if (strEq(attrs[i], "default")) {
+            a_default = attrs[++i];
+        } else if (strEq(attrs[i], "in")) {
+            a_in = attrs[++i];
+        } else if (strEq(attrs[i], "max")) {
+            a_max = attrs[++i];
+        } else if (strEq(attrs[i], "min")) {
+            a_min = attrs[++i];
+        } else if (strEq(attrs[i], "range")) {
+            a_range = attrs[++i];
+        } else if (strEq(attrs[i], "ranges")) {
+            a_ranges = attrs[++i];
+        } else if (strEq(attrs[i], "scale")) {
+            a_scale = attrs[++i];
+        } else if (strEq(attrs[i], "value")) {
+            a_value = attrs[++i];
+        } else {
+            PLOGD("Limit: ignoring unrecognized limit: %s", attrs[i]);
+            ++i;
+        }
         ++i;
     }
 
     if (a_name == nullptr) {
-        ALOGE("limit with no 'name' attribute");
+        PLOGD("Limit with no 'name' attribute");
         return BAD_VALUE;
     }
 
@@ -774,8 +1172,50 @@
     // measured-frame-rate, measured-blocks-per-second: range
     // quality: range + default + [scale]
     // complexity: range + default
-    std::string range;
-    if (strEq(a_name, "aspect-ratio") ||
+    std::string key = a_name, value;
+
+    // don't allow specifying more than one of value, range or min/max
+    if ((a_value != nullptr) + (a_range != nullptr) + (a_ranges != nullptr)
+            + (a_min != nullptr || a_max != nullptr) > 1) {
+        PLOGD("Limit '%s' has multiple 'min'/'max', 'range', 'ranges' or 'value' attributes",
+                a_name);
+        return BAD_VALUE;
+    }
+
+    // Min/max limits (only containing min or max attribute)
+    //
+    // Current "max" limits are "channel-count", "concurrent-instances".
+    // There are no current "min" limits
+    //
+    // Range limits. "range" is specified in exactly one of the following forms:
+    // 1) min-max
+    // 2) value-value
+    // 3) range
+    //
+    // Current range limits are "aspect-ratio", "bitrate", "block-count", "blocks-per-second",
+    // "complexity", "frame-rate", "quality", "size", "measured-blocks-per-second",
+    // "performance-point-*", "measured-frame-rate-*"
+    //
+    // Other limits (containing only value or ranges)
+    //
+    // Current ranges limit is "sample-rate"
+    if ((a_min != nullptr) ^ (a_max != nullptr)) {
+        // min/max limit
+        if (a_max != nullptr) {
+            key = "max-" + key;
+            value = a_max;
+        } else if (a_min != nullptr) {
+            key = "min-" + key;
+            value = a_min;
+        }
+    } else if (a_min != nullptr && a_max != nullptr) {
+        // min-max
+        key += "-range";
+        value = a_min + std::string("-") + a_max;
+    } else if (a_value != nullptr) {
+        // value-value or value
+        value = a_value;
+        if (strEq(a_name, "aspect-ratio") ||
             strEq(a_name, "bitrate") ||
             strEq(a_name, "block-count") ||
             strEq(a_name, "blocks-per-second") ||
@@ -786,249 +1226,199 @@
             strEq(a_name, "measured-blocks-per-second") ||
             strHasPrefix(a_name, "performance-point-") ||
             strHasPrefix(a_name, "measured-frame-rate-")) {
-        // "range" is specified in exactly one of the following forms:
-        // 1) min-max
-        // 2) value-value
-        // 3) range
-        if (a_min != nullptr && a_max != nullptr) {
-            // min-max
-            if (a_range != nullptr || a_value != nullptr) {
-                return limitError(a_name, "has 'min' and 'max' as well as 'range' or "
-                        "'value' attributes");
-            }
-            range = a_min;
-            range += '-';
-            range += a_max;
-        } else if (a_min != nullptr || a_max != nullptr) {
-            return limitError(a_name, "has only 'min' or 'max' attribute");
-        } else if (a_value != nullptr) {
-            // value-value
-            if (a_range != nullptr) {
-                return limitError(a_name, "has both 'range' and 'value' attributes");
-            }
-            range = a_value;
-            range += '-';
-            range += a_value;
-        } else if (a_range == nullptr) {
-            return limitError(a_name, "with no 'range', 'value' or 'min'/'max' attributes");
-        } else {
-            // range
-            range = a_range;
+            key += "-range";
+            value += std::string("-") + a_value;
         }
-
-        // "aspect-ratio" requires some special treatment.
-        if (strEq(a_name, "aspect-ratio")) {
-            // "aspect-ratio" must have "in".
-            if (a_in == nullptr) {
-                return limitFoundMissingAttr(a_name, "in", false);
-            }
-            // "in" must be either "pixels" or "blocks".
-            if (!strEq(a_in, "pixels") && !strEq(a_in, "blocks")) {
-                return limitInvalidAttr(a_name, "in", a_in);
-            }
-            // name will be "pixel-aspect-ratio-range" or
-            // "block-aspect-ratio-range".
-            mCurrentType->second[
-                    std::string(a_in).substr(0, strlen(a_in) - 1) +
-                    "-aspect-ratio-range"] = range;
-        } else {
-            // For everything else (apart from "aspect-ratio"), simply append
-            // "-range" to the name for the range-type property.
-            mCurrentType->second[std::string(a_name) + "-range"] = range;
-
-            // Only "quality" may have "scale".
-            if (!strEq(a_name, "quality") && a_scale != nullptr) {
-                return limitFoundMissingAttr(a_name, "scale");
-            } else if (strEq(a_name, "quality")) {
-                // The default value of "quality-scale" is "linear".
-                mCurrentType->second["quality-scale"] = a_scale == nullptr ?
-                        "linear" : a_scale;
-            }
-
-            // "quality" and "complexity" must have "default".
-            // Other limits must not have "default".
-            if (strEq(a_name, "quality") || strEq(a_name, "complexity")) {
-                if (a_default == nullptr) {
-                    return limitFoundMissingAttr(a_name, "default", false);
-                }
-                // name will be "quality-default" or "complexity-default".
-                mCurrentType->second[std::string(a_name) + "-default"] = a_default;
-            } else if (a_default != nullptr) {
-                return limitFoundMissingAttr(a_name, "default", true);
-            }
-        }
+    } else if (a_range != nullptr) {
+        // range
+        key += "-range";
+        value = a_range;
+    } else if (a_ranges != nullptr) {
+        // ranges
+        key += "-ranges";
+        value = a_ranges;
     } else {
-        if (a_default != nullptr) {
-            return limitFoundMissingAttr(a_name, "default");
-        }
-        if (a_in != nullptr) {
-            return limitFoundMissingAttr(a_name, "in");
-        }
-        if (a_scale != nullptr) {
-            return limitFoundMissingAttr(a_name, "scale");
-        }
-        if (a_range != nullptr) {
-            return limitFoundMissingAttr(a_name, "range");
-        }
-        if (a_min != nullptr) {
-            return limitFoundMissingAttr(a_name, "min");
-        }
-
-        if (a_max != nullptr) {
-            // "max" must exist if and only if name is "channel-count" or
-            // "concurrent-instances".
-            // "min" is not ncessary.
-            if (strEq(a_name, "channel-count") ||
-                    strEq(a_name, "concurrent-instances")) {
-                mCurrentType->second[std::string("max-") + a_name] = a_max;
-            } else {
-                return limitFoundMissingAttr(a_name, "max", false);
-            }
-        } else if (strEq(a_name, "channel-count") ||
-                strEq(a_name, "concurrent-instances")) {
-            return limitFoundMissingAttr(a_name, "max");
-        }
-
-        if (a_ranges != nullptr) {
-            // "ranges" must exist if and only if name is "sample-rate".
-            if (strEq(a_name, "sample-rate")) {
-                mCurrentType->second["sample-rate-ranges"] = a_ranges;
-            } else {
-                return limitFoundMissingAttr(a_name, "ranges", false);
-            }
-        } else if (strEq(a_name, "sample-rate")) {
-            return limitFoundMissingAttr(a_name, "ranges");
-        }
-
-        if (a_value != nullptr) {
-            // "value" must exist if and only if name is "alignment" or
-            // "block-size".
-            if (strEq(a_name, "alignment") || strEq(a_name, "block-size")) {
-                mCurrentType->second[a_name] = a_value;
-            } else {
-                return limitFoundMissingAttr(a_name, "value", false);
-            }
-        } else if (strEq(a_name, "alignment") || strEq(a_name, "block-size")) {
-            return limitFoundMissingAttr(a_name, "value", false);
-        }
-
+        PLOGD("Limit '%s' with no 'range', 'value' or 'min'/'max' attributes", a_name);
+        return BAD_VALUE;
     }
 
+    // handle 'in' attribute - this changes the key
+    if (a_in != nullptr) {
+        // Currently "aspect-ratio" uses in attribute
+        const size_t a_in_len = strlen(a_in);
+        key = std::string(a_in, a_in_len - a_in[a_in_len] == 's') + '-' + key;
+    }
+
+    // handle 'scale' attribute - this adds a new detail
+    if (a_scale != nullptr) {
+        mState->addDetail(a_name + std::string("-scale"), a_scale);
+    } else if (strEq(a_name, "quality")) {
+        // The default value of "quality-scale" is "linear" even if unspecified.
+        mState->addDetail(a_name + std::string("-scale"), "linear");
+    }
+
+    // handle 'default' attribute - this adds a new detail
+    if (a_default != nullptr) {
+        mState->addDetail(a_name + std::string("-default"), a_default);
+    }
+
+    mState->addDetail(key, value);
     return OK;
 }
 
-status_t MediaCodecsXmlParser::addFeature(const char **attrs) {
-    if (mCurrentCodec == mCodecMap.end()) {
-        return BAD_VALUE;
+void MediaCodecsXmlParser::Impl::State::addDetail(
+        const std::string &key, const std::string &value) {
+    CHECK(inType());
+    ALOGI("limit: %s = %s", key.c_str(), value.c_str());
+    const StringSet &variants = mVariantsStack.back();
+    if (variants.empty()) {
+        type()[key] = value;
+    } else {
+        for (const std::string &variant : variants) {
+            type()[variant + ":::" + key] = value;
+        }
     }
-    if (mCurrentType == mCurrentCodec->second.typeMap.end()) {
+}
+
+status_t MediaCodecsXmlParser::Impl::Parser::limitVariants(const char **attrs) {
+    const char* a_name = nullptr;
+
+    size_t i = 0;
+    while (attrs[i] != nullptr) {
+        CHECK((i & 1) == 0);
+        if (attrs[i + 1] == nullptr) {
+            PLOGD("Variant: attribute '%s' is null", attrs[i]);
+            return BAD_VALUE;
+        }
+        if (strEq(attrs[i], "name")) {
+            a_name = attrs[++i];
+        } else {
+            PLOGD("Variant: ignoring unrecognized attribute: %s", attrs[i]);
+            ++i;
+        }
+        ++i;
+    }
+
+    if (a_name == nullptr || *a_name == '\0') {
+        PLOGD("Variant with no or empty 'name' attribute");
         return BAD_VALUE;
     }
 
+    StringSet variants;
+    for (const std::string &variant : parseCommaSeparatedStringSet(a_name)) {
+        if (mState->variants().count(variant)) {
+            variants.emplace(variant);
+        } else {
+            PLOGD("Variant: variant '%s' not in parent variants", variant.c_str());
+            return BAD_VALUE;
+        }
+    }
+    mState->enterVariants(variants);
+    return OK;
+}
+
+status_t MediaCodecsXmlParser::Impl::Parser::addFeature(const char **attrs) {
+    CHECK(mState->inType());
     size_t i = 0;
-    const char *name = nullptr;
+    const char *a_name = nullptr;
     int32_t optional = -1;
     int32_t required = -1;
-    const char *value = nullptr;
+    const char *a_value = nullptr;
 
     while (attrs[i] != nullptr) {
-        if (strEq(attrs[i], "name")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addFeature: name is null");
-                return BAD_VALUE;
-            }
-            name = attrs[i];
-        } else if (strEq(attrs[i], "optional")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addFeature: optional is null");
-                return BAD_VALUE;
-            }
-            optional = parseBoolean(attrs[i]) ? 1 : 0;
-        } else if (strEq(attrs[i], "required")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addFeature: required is null");
-                return BAD_VALUE;
-            }
-            required = parseBoolean(attrs[i]) ? 1 : 0;
-        } else if (strEq(attrs[i], "value")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addFeature: value is null");
-                return BAD_VALUE;
-            }
-            value = attrs[i];
-        } else {
-            ALOGE("addFeature: unrecognized attribute: %s", attrs[i]);
+        CHECK((i & 1) == 0);
+        if (attrs[i + 1] == nullptr) {
+            PLOGD("Feature: attribute '%s' is null", attrs[i]);
             return BAD_VALUE;
         }
+
+        if (strEq(attrs[i], "name")) {
+            a_name = attrs[++i];
+        } else if (strEq(attrs[i], "optional")) {
+            optional = parseBoolean(attrs[++i]) ? 1 : 0;
+        } else if (strEq(attrs[i], "required")) {
+            required = parseBoolean(attrs[++i]) ? 1 : 0;
+        } else if (strEq(attrs[i], "value")) {
+            a_value = attrs[++i];
+        } else {
+            PLOGD("Feature: ignoring unrecognized attribute '%s'", attrs[i]);
+            ++i;
+        }
         ++i;
     }
 
     // Every feature must have a name.
-    if (name == nullptr) {
-        ALOGE("feature with no 'name' attribute");
+    if (a_name == nullptr) {
+        PLOGD("Feature with no 'name' attribute");
         return BAD_VALUE;
     }
 
-    if ((optional != -1) || (required != -1)) {
-        if (optional == required) {
-            ALOGE("feature '%s' is both/neither optional and required", name);
+    if (a_value != nullptr) {
+        if (optional != -1 || required != -1) {
+            PLOGD("Feature '%s' has both value and optional/required attributes", a_name);
             return BAD_VALUE;
         }
-        if ((optional == 1) || (required == 1)) {
-            if (value != nullptr) {
-                ALOGE("feature '%s' cannot have extra 'value'", name);
-                return BAD_VALUE;
-            }
-            mCurrentType->second[std::string("feature-") + name] =
-                    optional == 1 ? "0" : "1";
-            return OK;
+    } else {
+        if (optional == required && optional != -1) {
+            PLOGD("Feature '%s' is both/neither optional and required", a_name);
+            return BAD_VALUE;
         }
+        a_value = (required == 1 || optional == 0) ? "1" : "0";
     }
-    mCurrentType->second[std::string("feature-") + name] = value == nullptr ?
-            "0" : value;
+
+    mState->addDetail(std::string("feature-") + a_name, a_value ? : "0");
     return OK;
 }
 
-status_t MediaCodecsXmlParser::addAlias(const char **attrs) {
+status_t MediaCodecsXmlParser::Impl::Parser::addAlias(const char **attrs) {
+    CHECK(mState->inCodec());
     size_t i = 0;
-    const char *name = nullptr;
+    const char *a_name = nullptr;
 
     while (attrs[i] != nullptr) {
-        if (strEq(attrs[i], "name")) {
-            if (attrs[++i] == nullptr) {
-                ALOGE("addAlias: name is null");
-                return BAD_VALUE;
-            }
-            name = attrs[i];
-        } else {
-            ALOGE("addAlias: unrecognized attribute: %s", attrs[i]);
+        CHECK((i & 1) == 0);
+        if (attrs[i + 1] == nullptr) {
+            PLOGD("Alias: attribute '%s' is null", attrs[i]);
             return BAD_VALUE;
         }
+
+        if (strEq(attrs[i], "name")) {
+            a_name = attrs[++i];
+        } else {
+            PLOGD("Alias: ignoring unrecognized attribute '%s'", attrs[i]);
+            ++i;
+        }
         ++i;
     }
 
     // Every feature must have a name.
-    if (name == nullptr) {
-        ALOGE("alias with no 'name' attribute");
+    if (a_name == nullptr) {
+        PLOGD("Alias with no 'name' attribute");
         return BAD_VALUE;
     }
 
-    mCurrentCodec->second.aliases.emplace_back(name);
+    mState->codec().aliases.emplace_back(a_name);
     return OK;
 }
 
 const MediaCodecsXmlParser::AttributeMap&
-        MediaCodecsXmlParser::getServiceAttributeMap() const {
-    return mServiceAttributeMap;
+MediaCodecsXmlParser::getServiceAttributeMap() const {
+    return mImpl->getServiceAttributeMap();
 }
 
 const MediaCodecsXmlParser::CodecMap&
-        MediaCodecsXmlParser::getCodecMap() const {
-    return mCodecMap;
+MediaCodecsXmlParser::getCodecMap() const {
+    return mImpl->getCodecMap();
 }
 
 const MediaCodecsXmlParser::RoleMap&
-        MediaCodecsXmlParser::getRoleMap() const {
+MediaCodecsXmlParser::getRoleMap() const {
+    return mImpl->getRoleMap();
+}
+
+const MediaCodecsXmlParser::RoleMap&
+MediaCodecsXmlParser::Impl::getRoleMap() const {
+    std::lock_guard<std::mutex> guard(mLock);
     if (mRoleMap.empty()) {
         generateRoleMap();
     }
@@ -1036,6 +1426,11 @@
 }
 
 const char* MediaCodecsXmlParser::getCommonPrefix() const {
+    return mImpl->getCommonPrefix();
+}
+
+const char* MediaCodecsXmlParser::Impl::getCommonPrefix() const {
+    std::lock_guard<std::mutex> guard(mLock);
     if (mCommonPrefix.empty()) {
         generateCommonPrefix();
     }
@@ -1043,12 +1438,15 @@
 }
 
 status_t MediaCodecsXmlParser::getParsingStatus() const {
-    return mParsingStatus;
+    return mImpl->getParsingStatus();
 }
 
-void MediaCodecsXmlParser::generateRoleMap() const {
-    for (const auto& codec : mCodecMap) {
-        const auto& codecName = codec.first;
+void MediaCodecsXmlParser::Impl::generateRoleMap() const {
+    for (const auto& codec : mData.mCodecMap) {
+        const auto &codecName = codec.first;
+        if (codecName == "<dummy>") {
+            continue;
+        }
         bool isEncoder = codec.second.isEncoder;
         size_t order = codec.second.order;
         std::string rank = codec.second.rank;
@@ -1116,14 +1514,14 @@
     }
 }
 
-void MediaCodecsXmlParser::generateCommonPrefix() const {
-    if (mCodecMap.empty()) {
+void MediaCodecsXmlParser::Impl::generateCommonPrefix() const {
+    if (mData.mCodecMap.empty()) {
         return;
     }
-    auto i = mCodecMap.cbegin();
+    auto i = mData.mCodecMap.cbegin();
     auto first = i->first.cbegin();
     auto last = i->first.cend();
-    for (++i; i != mCodecMap.cend(); ++i) {
+    for (++i; i != mData.mCodecMap.cend(); ++i) {
         last = std::mismatch(
                 first, last, i->first.cbegin(), i->first.cend()).first;
     }
@@ -1131,4 +1529,3 @@
 }
 
 } // namespace android
-
diff --git a/media/libstagefright/xmlparser/include/media/stagefright/xmlparser/MediaCodecsXmlParser.h b/media/libstagefright/xmlparser/include/media/stagefright/xmlparser/MediaCodecsXmlParser.h
index 7a986b7..b666de4 100644
--- a/media/libstagefright/xmlparser/include/media/stagefright/xmlparser/MediaCodecsXmlParser.h
+++ b/media/libstagefright/xmlparser/include/media/stagefright/xmlparser/MediaCodecsXmlParser.h
@@ -19,34 +19,31 @@
 
 #include <sys/types.h>
 #include <utils/Errors.h>
-#include <utils/Vector.h>
-#include <utils/StrongPointer.h>
 
-#include <string>
-#include <set>
 #include <map>
+#include <mutex>
+#include <set>
+#include <string>
 #include <vector>
 
+struct XML_ParserStruct; // from expat library
+
 namespace android {
 
 class MediaCodecsXmlParser {
 public:
 
     // Treblized media codec list will be located in /odm/etc or /vendor/etc.
-    static constexpr char const* defaultSearchDirs[] =
-            {"/odm/etc", "/vendor/etc", "/etc", nullptr};
-    static constexpr char const* defaultMainXmlName =
-            "media_codecs.xml";
-    static constexpr char const* defaultPerformanceXmlName =
-            "media_codecs_performance.xml";
+    static std::vector<std::string> getDefaultSearchDirs() {
+            return { "/odm/etc", "/vendor/etc", "/etc" };
+    }
+    static std::vector<std::string> getDefaultXmlNames() {
+            return { "media_codecs.xml", "media_codecs_performance.xml" };
+    }
     static constexpr char const* defaultProfilingResultsXmlPath =
             "/data/misc/media/media_codecs_profiling_results.xml";
 
-    MediaCodecsXmlParser(
-            const char* const* searchDirs = defaultSearchDirs,
-            const char* mainXmlName = defaultMainXmlName,
-            const char* performanceXmlName = defaultPerformanceXmlName,
-            const char* profilingResultsXmlPath = defaultProfilingResultsXmlPath);
+    MediaCodecsXmlParser();
     ~MediaCodecsXmlParser();
 
     typedef std::pair<std::string, std::string> Attribute;
@@ -55,7 +52,7 @@
     typedef std::pair<std::string, AttributeMap> Type;
     typedef std::map<std::string, AttributeMap> TypeMap;
 
-    typedef std::set<std::string> QuirkSet;
+    typedef std::set<std::string> StringSet;
 
     /**
      * Properties of a codec (node)
@@ -63,7 +60,9 @@
     struct CodecProperties {
         bool isEncoder;    ///< Whether this codec is an encoder or a decoder
         size_t order;      ///< Order of appearance in the file (starting from 0)
-        QuirkSet quirkSet; ///< Set of quirks requested by this codec
+        StringSet quirkSet; ///< Set of quirks requested by this codec
+        StringSet domainSet; ///< Set of domains this codec is in
+        StringSet variantSet; ///< Set of variants this codec is enabled on
         TypeMap typeMap;   ///< Map of types supported by this codec
         std::vector<std::string> aliases; ///< Name aliases for this codec
         std::string rank;  ///< Rank of this codec. This is a numeric string.
@@ -119,70 +118,31 @@
 
     status_t getParsingStatus() const;
 
+    /**
+     * Parse top level XML files from a group of search directories.
+     *
+     * @param xmlFiles ordered list of XML file names (no paths)
+     * @param searchDirs ordered list of paths to consider
+     *
+     * @return parsing status
+     */
+    status_t parseXmlFilesInSearchDirs(
+            const std::vector<std::string> &xmlFiles = getDefaultXmlNames(),
+            const std::vector<std::string> &searchDirs = getDefaultSearchDirs());
+
+
+    /**
+     * Parse a top level XML file.
+     *
+     * @param path XML file path
+     *
+     * @return parsing status
+     */
+    status_t parseXmlPath(const std::string &path);
+
 private:
-    enum Section {
-        SECTION_TOPLEVEL,
-        SECTION_SETTINGS,
-        SECTION_DECODERS,
-        SECTION_DECODER,
-        SECTION_DECODER_TYPE,
-        SECTION_ENCODERS,
-        SECTION_ENCODER,
-        SECTION_ENCODER_TYPE,
-        SECTION_INCLUDE,
-    };
-
-    status_t mParsingStatus;
-    Section mCurrentSection;
-    bool mUpdate;
-    std::vector<Section> mSectionStack;
-    std::string mHrefBase;
-
-    // Service attributes
-    AttributeMap mServiceAttributeMap;
-
-    // Codec attributes
-    std::string mCurrentName;
-    std::set<std::string> mCodecSet;
-    Codec mCodecListTemp[2048];
-    CodecMap mCodecMap;
-    size_t mCodecCounter;
-    CodecMap::iterator mCurrentCodec;
-    TypeMap::iterator mCurrentType;
-
-    // Role map
-    mutable RoleMap mRoleMap;
-
-    // Computed longest common prefix
-    mutable std::string mCommonPrefix;
-
-    bool parseTopLevelXMLFile(const char *path, bool ignore_errors = false);
-
-    void parseXMLFile(const char *path);
-
-    static void StartElementHandlerWrapper(
-            void *me, const char *name, const char **attrs);
-
-    static void EndElementHandlerWrapper(void *me, const char *name);
-
-    void startElementHandler(const char *name, const char **attrs);
-    void endElementHandler(const char *name);
-
-    status_t includeXMLFile(const char **attrs);
-    status_t addSettingFromAttributes(const char **attrs);
-    status_t addMediaCodecFromAttributes(bool encoder, const char **attrs);
-    void addMediaCodec(bool encoder, const char *name,
-            const char *type = nullptr);
-
-    status_t addQuirk(const char **attrs, const char *tag);
-    status_t addTypeFromAttributes(const char **attrs, bool encoder);
-    status_t addAlias(const char **attrs);
-    status_t addLimit(const char **attrs);
-    status_t addFeature(const char **attrs);
-    void addType(const char *name);
-
-    void generateRoleMap() const;
-    void generateCommonPrefix() const;
+    struct Impl;
+    std::shared_ptr<Impl> mImpl;
 
     MediaCodecsXmlParser(const MediaCodecsXmlParser&) = delete;
     MediaCodecsXmlParser& operator=(const MediaCodecsXmlParser&) = delete;
@@ -191,4 +151,3 @@
 } // namespace android
 
 #endif // MEDIA_STAGEFRIGHT_XMLPARSER_H_
-