Merge "Revert "Codec2: separate platform dependency""
diff --git a/media/libstagefright/codec2/1.0/InputSurfaceConnection.cpp b/media/libstagefright/codec2/1.0/InputSurfaceConnection.cpp
index 08b5d65..32d6404 100644
--- a/media/libstagefright/codec2/1.0/InputSurfaceConnection.cpp
+++ b/media/libstagefright/codec2/1.0/InputSurfaceConnection.cpp
@@ -22,6 +22,7 @@
 #include <C2BlockInternal.h>
 #include <C2PlatformSupport.h>
 
+#include <gui/Surface.h>
 #include <media/stagefright/codec2/1.0/InputSurfaceConnection.h>
 #include <system/window.h>
 
diff --git a/media/libstagefright/codec2/C2.cpp b/media/libstagefright/codec2/C2.cpp
index 359d4e5..a51b073 100644
--- a/media/libstagefright/codec2/C2.cpp
+++ b/media/libstagefright/codec2/C2.cpp
@@ -22,6 +22,8 @@
 #include <C2ParamDef.h>
 #include <C2Work.h>
 
+namespace android {
+
 /**
  * There is nothing here yet. This library is built to see what symbols and methods get
  * defined as part of the API include files.
@@ -30,4 +32,5 @@
  * Codec2 clients.
  */
 
+} // namespace android
 
diff --git a/media/libstagefright/codec2/include/C2.h b/media/libstagefright/codec2/include/C2.h
index 508f9ae..00e3924 100644
--- a/media/libstagefright/codec2/include/C2.h
+++ b/media/libstagefright/codec2/include/C2.h
@@ -17,18 +17,40 @@
 #ifndef C2_H_
 #define C2_H_
 
-#include <errno.h>
-
 #include <string>
+#include <vector>
+#include <list>
 
-/** nanoseconds with arbitrary origin. */
-typedef int64_t c2_nsecs_t;
+#ifdef __ANDROID__
+
+#include <utils/Errors.h>       // for status_t
+#include <utils/Timers.h>       // for nsecs_t
+
+namespace android {
+
+#else
+
+#include <errno.h>
+typedef int64_t nsecs_t;
+
+enum {
+    GRALLOC_USAGE_SW_READ_OFTEN,
+    GRALLOC_USAGE_RENDERSCRIPT,
+    GRALLOC_USAGE_HW_TEXTURE,
+    GRALLOC_USAGE_HW_COMPOSER,
+    GRALLOC_USAGE_HW_VIDEO_ENCODER,
+    GRALLOC_USAGE_PROTECTED,
+    GRALLOC_USAGE_SW_WRITE_OFTEN,
+    GRALLOC_USAGE_HW_RENDER,
+};
+
+#endif
 
 /** \mainpage Codec2
  *
- * Codec2 is a generic frame-based data processing API.
+ * Codec2 is a frame-based data processing API used by android.
  *
- * The media subsystem accesses components via the \ref API.
+ * The framework accesses components via the \ref API.
  */
 
 /** \ingroup API
@@ -87,35 +109,53 @@
  * c2_status_t: status codes used.
  */
 enum c2_status_t : int32_t {
+
 /*
- * Use POSIX errno constants.
+ * Use android status constants if available. Otherwise, define the android status constants as
+ * additional enum values using POSIX errno constants.
  */
-    C2_OK        = 0,            ///< operation completed successfully
+#ifndef __ANDROID__
+    ALREADY_EXISTS      = -EEXIST,
+    BAD_VALUE           = -EINVAL,
+    BAD_INDEX           = -EOVERFLOW,
+    FAILED_TRANSACTION  = -ENOTSUP,
+    INVALID_OPERATION   = -ENOSYS,
+    NAME_NOT_FOUND      = -ENOENT,
+    NO_MEMORY           = -ENOMEM,
+    NO_INIT             = -ENODEV,
+    OK                  = 0,
+    PERMISSION_DENIED   = -EPERM,
+    TIMED_OUT           = -ETIMEDOUT,
+    UNKNOWN_ERROR       = -EFAULT,
+    UNKNOWN_TRANSACTION = -EBADMSG,
+    WOULD_BLOCK         = -EWOULDBLOCK,
+#endif
+
+    C2_OK        = OK,                   ///< operation completed successfully
 
     // bad input
-    C2_BAD_VALUE = EINVAL,       ///< argument has invalid value (user error)
-    C2_BAD_INDEX = ENXIO,        ///< argument uses invalid index (user error)
-    C2_CANNOT_DO = ENOTSUP,      ///< argument/index is valid but not possible
+    C2_BAD_VALUE = BAD_VALUE,            ///< argument has invalid value (user error)
+    C2_BAD_INDEX = BAD_INDEX,            ///< argument uses invalid index (user error)
+    C2_CANNOT_DO = FAILED_TRANSACTION,   ///< argument/index is valid but not possible
 
     // bad sequencing of events
-    C2_DUPLICATE = EEXIST,       ///< object already exists
-    C2_NOT_FOUND = ENOENT,       ///< object not found
-    C2_BAD_STATE = EPERM,        ///< operation is not permitted in the current state
-    C2_BLOCKING  = EWOULDBLOCK,  ///< operation would block but blocking is not permitted
-    C2_CANCELED  = EINTR,        ///< operation interrupted/canceled
+    C2_DUPLICATE = ALREADY_EXISTS,       ///< object already exists
+    C2_NOT_FOUND = NAME_NOT_FOUND,       ///< object not found
+    C2_BAD_STATE = INVALID_OPERATION,    ///< operation is not permitted in the current state
+    C2_BLOCKING  = WOULD_BLOCK,          ///< operation would block but blocking is not permitted
 
     // bad environment
-    C2_NO_MEMORY = ENOMEM,       ///< not enough memory to complete operation
-    C2_REFUSED   = EACCES,       ///< missing permission to complete operation
+    C2_NO_MEMORY = NO_MEMORY,            ///< not enough memory to complete operation
+    C2_REFUSED   = PERMISSION_DENIED,    ///< missing permission to complete operation
 
-    C2_TIMED_OUT = ETIMEDOUT,    ///< operation did not complete within timeout
+    C2_TIMED_OUT = TIMED_OUT,            ///< operation did not complete within timeout
 
     // bad versioning
-    C2_OMITTED   = ENOSYS,       ///< operation is not implemented/supported (optional only)
+    C2_OMITTED   = UNKNOWN_TRANSACTION,  ///< operation is not implemented/supported (optional only)
 
     // unknown fatal
-    C2_CORRUPTED = EFAULT,       ///< some unexpected error prevented the operation
-    C2_NO_INIT   = ENODEV,       ///< status has not been initialized
+    C2_CORRUPTED = UNKNOWN_ERROR,        ///< some unexpected error prevented the operation
+    C2_NO_INIT   = NO_INIT,              ///< status has not been initialized
 };
 
 /**
@@ -145,12 +185,11 @@
     type args& operator=(const type args&) = delete; \
     type(const type args&) = delete; \
 
-#define C2_ALLOW_OVERFLOW __attribute__((no_sanitize("integer")))
-#define C2_CONST    __attribute__((const))
-#define C2_HIDE     __attribute__((visibility("hidden")))
+#define C2_PURE __attribute__((pure))
+#define C2_CONST __attribute__((const))
+#define C2_HIDE __attribute__((visibility("hidden")))
 #define C2_INTERNAL __attribute__((internal_linkage))
-#define C2_PACK     __attribute__((packed))
-#define C2_PURE     __attribute__((pure))
+#define C2_ALLOW_OVERFLOW __attribute__((no_sanitize("integer")))
 
 #define DEFINE_OTHER_COMPARISON_OPERATORS(type) \
     inline bool operator!=(const type &other) const { return !(*this == other); } \
@@ -509,28 +548,32 @@
 
 /// @}
 
+#ifdef __ANDROID__
+} // namespace android
+#endif
+
 #include <functional>
 template<typename T>
-struct std::less<::c2_cntr_t<T>> {
-    constexpr bool operator()(const ::c2_cntr_t<T> &lh, const ::c2_cntr_t<T> &rh) const {
+struct std::less<::android::c2_cntr_t<T>> {
+    constexpr bool operator()(const ::android::c2_cntr_t<T> &lh, const ::android::c2_cntr_t<T> &rh) const {
         return lh.peeku() < rh.peeku();
     }
 };
 template<typename T>
-struct std::less_equal<::c2_cntr_t<T>> {
-    constexpr bool operator()(const ::c2_cntr_t<T> &lh, const ::c2_cntr_t<T> &rh) const {
+struct std::less_equal<::android::c2_cntr_t<T>> {
+    constexpr bool operator()(const ::android::c2_cntr_t<T> &lh, const ::android::c2_cntr_t<T> &rh) const {
         return lh.peeku() <= rh.peeku();
     }
 };
 template<typename T>
-struct std::greater<::c2_cntr_t<T>> {
-    constexpr bool operator()(const ::c2_cntr_t<T> &lh, const ::c2_cntr_t<T> &rh) const {
+struct std::greater<::android::c2_cntr_t<T>> {
+    constexpr bool operator()(const ::android::c2_cntr_t<T> &lh, const ::android::c2_cntr_t<T> &rh) const {
         return lh.peeku() > rh.peeku();
     }
 };
 template<typename T>
-struct std::greater_equal<::c2_cntr_t<T>> {
-    constexpr bool operator()(const ::c2_cntr_t<T> &lh, const ::c2_cntr_t<T> &rh) const {
+struct std::greater_equal<::android::c2_cntr_t<T>> {
+    constexpr bool operator()(const ::android::c2_cntr_t<T> &lh, const ::android::c2_cntr_t<T> &rh) const {
         return lh.peeku() >= rh.peeku();
     }
 };
diff --git a/media/libstagefright/codec2/include/C2Buffer.h b/media/libstagefright/codec2/include/C2Buffer.h
index e49f82b..034075f 100644
--- a/media/libstagefright/codec2/include/C2Buffer.h
+++ b/media/libstagefright/codec2/include/C2Buffer.h
@@ -18,20 +18,27 @@
 #define C2BUFFER_H_
 
 #include <C2.h>
-#include <C2BufferBase.h>
 #include <C2Param.h> // for C2Info
 
+#include <list>
 #include <memory>
-#include <vector>
 
 #ifdef __ANDROID__
-#include <android-C2Buffer.h>
+
+// #include <system/window.h>
+#include <cutils/native_handle.h>
+#include <hardware/gralloc.h> // TODO: remove
+
+typedef native_handle_t C2Handle;
+
 #else
 
 typedef void* C2Handle;
 
 #endif
 
+namespace android {
+
 /// \defgroup buffer Buffers
 /// @{
 
@@ -82,7 +89,7 @@
      * \retval C2_REFUSED       no permission to wait for the fence (unexpected - system)
      * \retval C2_CORRUPTED     some unknown error prevented waiting for the fence (unexpected)
      */
-    c2_status_t wait(c2_nsecs_t timeoutNs);
+    c2_status_t wait(nsecs_t timeoutNs);
 
     /**
      * Used to check if this fence is valid (if there is a chance for it to be signaled.)
@@ -543,9 +550,41 @@
   ALLOCATIONS
 **************************************************************************************************/
 
-/// \ingroup allocator Allocation and memory placement
+/// \defgroup allocator Allocation and memory placement
 /// @{
 
+/**
+ * Buffer/memory usage bits. These are used by the allocators to select optimal memory type/pool and
+ * buffer layout.
+ *
+ * \note This struct has public fields without getters/setters. All methods are inline.
+ */
+struct C2MemoryUsage {
+// public:
+    // TODO: match these to gralloc1.h
+    enum Consumer : uint64_t {
+        // \todo do we need to distinguish often from rarely?
+        CPU_READ          = GRALLOC_USAGE_SW_READ_OFTEN,
+        RENDERSCRIPT_READ = GRALLOC_USAGE_RENDERSCRIPT,
+        HW_TEXTURE_READ   = GRALLOC_USAGE_HW_TEXTURE,
+        HW_COMPOSER_READ  = GRALLOC_USAGE_HW_COMPOSER,
+        HW_CODEC_READ     = GRALLOC_USAGE_HW_VIDEO_ENCODER,
+        READ_PROTECTED    = GRALLOC_USAGE_PROTECTED,
+    };
+
+    enum Producer : uint64_t {
+        CPU_WRITE          = GRALLOC_USAGE_SW_WRITE_OFTEN,
+        RENDERSCRIPT_WRITE = GRALLOC_USAGE_RENDERSCRIPT,
+        HW_TEXTURE_WRITE   = GRALLOC_USAGE_HW_RENDER,
+        HW_COMPOSER_WRITE  = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER,
+        HW_CODEC_WRITE     = GRALLOC_USAGE_HW_VIDEO_ENCODER,
+        WRITE_PROTECTED    = GRALLOC_USAGE_PROTECTED,
+    };
+
+    uint64_t consumer; // e.g. input
+    uint64_t producer; // e.g. output
+};
+
 class C2LinearAllocation;
 class C2GraphicAllocation;
 
@@ -2262,10 +2301,6 @@
 
 /// @}
 
-// expose some objects in android namespace
-namespace android {
-    /// \deprecated
-    typedef ::C2Fence C2Fence;
-}
+}  // namespace android
 
 #endif  // C2BUFFER_H_
diff --git a/media/libstagefright/codec2/include/C2BufferBase.h b/media/libstagefright/codec2/include/C2BufferBase.h
deleted file mode 100644
index 68411f2..0000000
--- a/media/libstagefright/codec2/include/C2BufferBase.h
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef C2BUFFER_BASE_H_
-#define C2BUFFER_BASE_H_
-
-/// \defgroup allocator Allocation and memory placement
-/// @{
-
-/**
- * Buffer/memory usage bits. These shall be used by the allocators to select optimal memory type/
- * pool and buffer layout. Usage bits are conceptually separated into read and write usage, while
- * the buffer use life-cycle is separated into producers (writers) and consumers (readers).
- * These two concepts are related but not equivalent: consumers may only read buffers and only
- * producers may write to buffers; note, however, that buffer producers may also want or need to
- * read the buffers.
- *
- * Read and write buffer usage bits shall be or-ed to arrive at the full buffer usage. Admittedly,
- * this does not account for the amount of reading and writing (e.g. a buffer may have one or more
- * readers); however, the proper information necessary to properly weigh the various usages would be
- * the amount of data read/written for each usage type. This would result in an integer array of
- * size 64 (or the number of distinct usages) for memory usage, and likely such detailed information
- * would not always be available.
- *
- * That platform-agnostic Codec 2.0 API only defines the bare minimum usages. Platforms shall define
- * usage bits that are appropriate for the platform.
- */
-struct C2MemoryUsage {
-// public:
-    /**
-     * Buffer read usage.
-     */
-    enum Read : uint64_t {
-        /** Buffer is read by the CPU. */
-        CPU_READ        = 1 << 0,
-        /**
-         * Buffer shall only be read by trusted hardware. The definition of trusted hardware is
-         * platform specific, but this flag is reserved to prevent mapping this block into CPU
-         * readable memory resulting in bus fault. This flag can be used when buffer access must be
-         * protected.
-         */
-        READ_PROTECTED  = 1 << 1,
-    };
-
-    /**
-     * Buffer write usage.
-     */
-    enum Write : uint64_t {
-        /** Buffer is writted to by the CPU. */
-        CPU_WRITE        = 1 << 2,
-        /**
-         * Buffer shall only be written to by trusted hardware. The definition of trusted hardware
-         * is platform specific, but this flag is reserved to prevent mapping this block into CPU
-         * writable memory resulting in bus fault. This flag can be used when buffer integrity must
-         * be protected.
-         */
-        WRITE_PROTECTED  = 1 << 3,
-    };
-
-    enum : uint64_t {
-        /**
-         * Buffer usage bits reserved for the platform. We don't separately reserve read and
-         * write usages as platforms may have asymmetric distribution between them.
-         */
-        PLATFORM_MASK     = ~(CPU_READ | CPU_WRITE | READ_PROTECTED | WRITE_PROTECTED),
-    };
-
-    /** Create a usage from separate consumer and producer usage mask. \deprecated */
-    inline C2MemoryUsage(uint64_t consumer, uint64_t producer)
-        : expected(consumer | producer) { }
-
-    inline explicit C2MemoryUsage(uint64_t expected_)
-        : expected(expected_) { }
-
-    uint64_t expected; // expected buffer usage
-};
-
-/// @}
-
-#endif  // C2BUFFER_BASE_H_
-
diff --git a/media/libstagefright/codec2/include/C2Component.h b/media/libstagefright/codec2/include/C2Component.h
index 64bd1cb..721966b 100644
--- a/media/libstagefright/codec2/include/C2Component.h
+++ b/media/libstagefright/codec2/include/C2Component.h
@@ -29,9 +29,13 @@
 #include <C2Param.h>
 #include <C2Work.h>
 
+namespace android {
+
 /// \defgroup components Components
 /// @{
 
+class C2Component;
+
 struct C2FieldSupportedValuesQuery {
     enum type_t : uint32_t {
         POSSIBLE, ///< query all possible values regardless of other settings
@@ -943,11 +947,6 @@
 
 /// @}
 
-namespace android {
-    /// \deprecated
-    typedef ::C2Component C2Component;
-    /// \deprecated
-    typedef ::C2ComponentInterface C2ComponentInterface;
-}
+}  // namespace android
 
 #endif  // C2COMPONENT_H_
diff --git a/media/libstagefright/codec2/include/C2Config.h b/media/libstagefright/codec2/include/C2Config.h
index 3f149bb..2a2b9de 100644
--- a/media/libstagefright/codec2/include/C2Config.h
+++ b/media/libstagefright/codec2/include/C2Config.h
@@ -19,6 +19,8 @@
 
 #include <C2ParamDef.h>
 
+namespace android {
+
 /// \defgroup config Component configuration
 /// @{
 
@@ -260,4 +262,6 @@
 
 /// @}
 
+} // namespace android
+
 #endif
diff --git a/media/libstagefright/codec2/include/C2Param.h b/media/libstagefright/codec2/include/C2Param.h
index 181697d..4d9f707 100644
--- a/media/libstagefright/codec2/include/C2Param.h
+++ b/media/libstagefright/codec2/include/C2Param.h
@@ -23,10 +23,13 @@
 #include <stdint.h>
 
 #include <algorithm>
+#include <list>
 #include <string>
 #include <type_traits>
-#include <utility>
-#include <vector>
+
+#define C2_PACK __attribute__((packed))
+
+namespace android {
 
 /// \addtogroup Parameters
 /// @{
@@ -86,6 +89,7 @@
  */
 
 /// \ingroup internal
+struct _C2ParamManipulator;
 
 /**
  * Parameter base class.
@@ -729,6 +733,17 @@
 };
 
 /**
+ * Structure uniquely specifying a field, an array element of a field, or a
+ * parameter in a configuration
+ */
+struct C2ParamOrField : public C2ParamField {
+//public:
+    template<typename S>
+    inline C2ParamOrField(S* param)
+        : C2ParamField(param->index(), 0u, param->size()) {}
+};
+
+/**
  * A shared (union) representation of numeric values
  */
 class C2Value {
@@ -1361,4 +1376,6 @@
 
 /// @}
 
+}  // namespace android
+
 #endif  // C2PARAM_H_
diff --git a/media/libstagefright/codec2/include/C2ParamDef.h b/media/libstagefright/codec2/include/C2ParamDef.h
index f0b6223..3691e01 100644
--- a/media/libstagefright/codec2/include/C2ParamDef.h
+++ b/media/libstagefright/codec2/include/C2ParamDef.h
@@ -24,6 +24,8 @@
 
 #include <C2Param.h>
 
+namespace android {
+
 /// \addtogroup Parameters
 /// @{
 
@@ -903,4 +905,6 @@
 
 /// @}
 
+}  // namespace android
+
 #endif  // C2PARAM_DEF_H_
diff --git a/media/libstagefright/codec2/include/C2Work.h b/media/libstagefright/codec2/include/C2Work.h
index a2f02e5..b6c5814 100644
--- a/media/libstagefright/codec2/include/C2Work.h
+++ b/media/libstagefright/codec2/include/C2Work.h
@@ -28,6 +28,8 @@
 #include <list>
 #include <vector>
 
+namespace android {
+
 /// \defgroup work Work and data processing
 /// @{
 
@@ -73,8 +75,9 @@
 //  WORK
 // ================================================================================================
 
-/** Unique ID for a processing node. */
+// c2_node_id_t-s
 typedef uint32_t c2_node_id_t;
+typedef c2_node_id_t c2_node_id_t;
 
 enum {
     kParamIndexWorkOrdinal,
@@ -208,4 +211,6 @@
 
 /// @}
 
+}  // namespace android
+
 #endif  // C2WORK_H_
diff --git a/media/libstagefright/codec2/include/android-C2Buffer.h b/media/libstagefright/codec2/include/android-C2Buffer.h
deleted file mode 100644
index c71f2cf..0000000
--- a/media/libstagefright/codec2/include/android-C2Buffer.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_C2BUFFER_H_
-#define ANDROID_C2BUFFER_H_
-
-#include <cutils/native_handle.h>
-#include <hardware/gralloc.h>
-
-/* Use android native handle for C2Handle */
-typedef ::native_handle_t C2Handle;
-
-namespace android {
-
-/**
- * Android platform buffer/memory usage bits.
- */
-struct C2AndroidMemoryUsage : public C2MemoryUsage {
-// public:
-    /**
-     * Reuse gralloc flags where possible, as Codec 2.0 API only uses bits 0 and 1.
-     */
-    enum Consumer : uint64_t {
-        RENDERSCRIPT_READ = GRALLOC_USAGE_RENDERSCRIPT,
-        HW_TEXTURE_READ   = GRALLOC_USAGE_HW_TEXTURE,
-        HW_COMPOSER_READ  = GRALLOC_USAGE_HW_COMPOSER,
-        HW_CODEC_READ     = GRALLOC_USAGE_HW_VIDEO_ENCODER,
-        READ_PROTECTED    = GRALLOC_USAGE_PROTECTED,
-    };
-
-    enum Producer : uint64_t {
-        RENDERSCRIPT_WRITE = GRALLOC_USAGE_RENDERSCRIPT,
-        HW_TEXTURE_WRITE   = GRALLOC_USAGE_HW_RENDER,
-        HW_COMPOSER_WRITE  = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER,
-        HW_CODEC_WRITE     = GRALLOC_USAGE_HW_VIDEO_ENCODER,
-        WRITE_PROTECTED    = GRALLOC_USAGE_PROTECTED,
-    };
-
-    /**
-     * Convert from gralloc usage.
-     */
-    static C2MemoryUsage FromGrallocUsage(uint64_t usage);
-
-    /**
-     * Convert to gralloc usage.
-     */
-    uint64_t asGrallocUsage() const;
-};
-
-}  // namespace android
-
-#endif  // ANDROID_C2BUFFER_H_
diff --git a/media/libstagefright/codec2/include/media/stagefright/codec2/1.0/InputSurfaceConnection.h b/media/libstagefright/codec2/include/media/stagefright/codec2/1.0/InputSurfaceConnection.h
index b24a416..fc19acd 100644
--- a/media/libstagefright/codec2/include/media/stagefright/codec2/1.0/InputSurfaceConnection.h
+++ b/media/libstagefright/codec2/include/media/stagefright/codec2/1.0/InputSurfaceConnection.h
@@ -25,6 +25,9 @@
 #include <media/stagefright/codec2/1.0/InputSurfaceConnection.h>
 
 namespace android {
+
+class C2Allocator;
+
 namespace hardware {
 namespace media {
 namespace c2 {
diff --git a/media/libstagefright/codec2/tests/C2Param_test.cpp b/media/libstagefright/codec2/tests/C2Param_test.cpp
index 168b889..1a29add 100644
--- a/media/libstagefright/codec2/tests/C2Param_test.cpp
+++ b/media/libstagefright/codec2/tests/C2Param_test.cpp
@@ -23,6 +23,8 @@
 #include <util/C2ParamUtils.h>
 #include <C2ParamDef.h>
 
+namespace android {
+
 void PrintTo(const _C2FieldId &id, ::std::ostream* os) {
     *os << "@" << id._mOffset << "+" << id._mSize;
 }
@@ -2395,11 +2397,15 @@
 
 // ***********************
 
+}
+
 #include <util/C2ParamUtils.h>
 #include <C2Config.h>
 #include <C2Component.h>
 #include <unordered_map>
 
+namespace android {
+
 C2ENUM(
     MetadataType, int32_t,
     kInvalid = -1,
@@ -2929,3 +2935,4 @@
     EXPECT_EQ(15.25f, fp);
 }
 
+} // namespace android
diff --git a/media/libstagefright/codec2/vndk/C2AllocatorGralloc.cpp b/media/libstagefright/codec2/vndk/C2AllocatorGralloc.cpp
index b255eec..b287ca8 100644
--- a/media/libstagefright/codec2/vndk/C2AllocatorGralloc.cpp
+++ b/media/libstagefright/codec2/vndk/C2AllocatorGralloc.cpp
@@ -29,36 +29,6 @@
 
 namespace android {
 
-namespace {
-    enum : uint64_t {
-        /**
-         * Usage mask that is passed through from gralloc to Codec 2.0 usage.
-         */
-        PASSTHROUGH_USAGE_MASK =
-            ~(GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_PROTECTED)
-    };
-
-    // verify that passthrough mask is within the platform mask
-    static_assert((~C2MemoryUsage::PLATFORM_MASK & PASSTHROUGH_USAGE_MASK) == 0, "");
-}
-
-C2MemoryUsage C2AndroidMemoryUsage::FromGrallocUsage(uint64_t usage) {
-    // gralloc does not support WRITE_PROTECTED
-    return C2MemoryUsage(
-            ((usage & GRALLOC_USAGE_SW_READ_MASK) ? C2MemoryUsage::CPU_READ : 0) |
-            ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ? C2MemoryUsage::CPU_WRITE : 0) |
-            ((usage & GRALLOC_USAGE_PROTECTED) ? C2MemoryUsage::READ_PROTECTED : 0) |
-            (usage & PASSTHROUGH_USAGE_MASK));
-}
-
-uint64_t C2AndroidMemoryUsage::asGrallocUsage() const {
-    // gralloc does not support WRITE_PROTECTED
-    return (((expected & C2MemoryUsage::CPU_READ) ? GRALLOC_USAGE_SW_READ_MASK : 0) |
-            ((expected & C2MemoryUsage::CPU_WRITE) ? GRALLOC_USAGE_SW_WRITE_MASK : 0) |
-            ((expected & C2MemoryUsage::READ_PROTECTED) ? GRALLOC_USAGE_PROTECTED : 0) |
-            (expected & PASSTHROUGH_USAGE_MASK));
-}
-
 using ::android::hardware::graphics::allocator::V2_0::IAllocator;
 using ::android::hardware::graphics::common::V1_0::BufferUsage;
 using ::android::hardware::graphics::common::V1_0::PixelFormat;
diff --git a/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp b/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp
index 18dad46..4328a8d 100644
--- a/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp
+++ b/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp
@@ -232,10 +232,10 @@
 
         int prot = PROT_NONE;
         int flags = MAP_PRIVATE;
-        if (usage.expected & C2MemoryUsage::CPU_READ) {
+        if (usage.consumer & C2MemoryUsage::CPU_READ) {
             prot |= PROT_READ;
         }
-        if (usage.expected & C2MemoryUsage::CPU_WRITE) {
+        if (usage.producer & C2MemoryUsage::CPU_WRITE) {
             prot |= PROT_WRITE;
             flags = MAP_SHARED;
         }
diff --git a/media/libstagefright/codec2/vndk/C2Buffer.cpp b/media/libstagefright/codec2/vndk/C2Buffer.cpp
index 47fdca1..511ffe0 100644
--- a/media/libstagefright/codec2/vndk/C2Buffer.cpp
+++ b/media/libstagefright/codec2/vndk/C2Buffer.cpp
@@ -18,13 +18,14 @@
 #define LOG_TAG "C2Buffer"
 #include <utils/Log.h>
 
-#include <list>
 #include <map>
 #include <mutex>
 
 #include <C2BufferPriv.h>
 #include <C2BlockInternal.h>
 
+namespace android {
+
 namespace {
 
 // This anonymous namespace contains the helper classes that allow our implementation to create
@@ -33,63 +34,63 @@
 // Inherit from the parent, share with the friend.
 class ReadViewBuddy : public C2ReadView {
     using C2ReadView::C2ReadView;
-    friend class ::C2ConstLinearBlock;
+    friend class ::android::C2ConstLinearBlock;
 };
 
 class WriteViewBuddy : public C2WriteView {
     using C2WriteView::C2WriteView;
-    friend class ::C2LinearBlock;
+    friend class ::android::C2LinearBlock;
 };
 
 class ConstLinearBlockBuddy : public C2ConstLinearBlock {
     using C2ConstLinearBlock::C2ConstLinearBlock;
-    friend class ::C2LinearBlock;
+    friend class ::android::C2LinearBlock;
 };
 
 class LinearBlockBuddy : public C2LinearBlock {
     using C2LinearBlock::C2LinearBlock;
-    friend class ::C2BasicLinearBlockPool;
+    friend class ::android::C2BasicLinearBlockPool;
 };
 
 class AcquirableReadViewBuddy : public C2Acquirable<C2ReadView> {
     using C2Acquirable::C2Acquirable;
-    friend class ::C2ConstLinearBlock;
+    friend class ::android::C2ConstLinearBlock;
 };
 
 class AcquirableWriteViewBuddy : public C2Acquirable<C2WriteView> {
     using C2Acquirable::C2Acquirable;
-    friend class ::C2LinearBlock;
+    friend class ::android::C2LinearBlock;
 };
 
 class GraphicViewBuddy : public C2GraphicView {
     using C2GraphicView::C2GraphicView;
-    friend class ::C2ConstGraphicBlock;
-    friend class ::C2GraphicBlock;
+    friend class ::android::C2ConstGraphicBlock;
+    friend class ::android::C2GraphicBlock;
 };
 
 class AcquirableConstGraphicViewBuddy : public C2Acquirable<const C2GraphicView> {
     using C2Acquirable::C2Acquirable;
-    friend class ::C2ConstGraphicBlock;
+    friend class ::android::C2ConstGraphicBlock;
 };
 
 class AcquirableGraphicViewBuddy : public C2Acquirable<C2GraphicView> {
     using C2Acquirable::C2Acquirable;
-    friend class ::C2GraphicBlock;
+    friend class ::android::C2GraphicBlock;
 };
 
 class ConstGraphicBlockBuddy : public C2ConstGraphicBlock {
     using C2ConstGraphicBlock::C2ConstGraphicBlock;
-    friend class ::C2GraphicBlock;
+    friend class ::android::C2GraphicBlock;
 };
 
 class GraphicBlockBuddy : public C2GraphicBlock {
     using C2GraphicBlock::C2GraphicBlock;
-    friend class ::C2BasicGraphicBlockPool;
+    friend class ::android::C2BasicGraphicBlockPool;
 };
 
 class BufferDataBuddy : public C2BufferData {
     using C2BufferData::C2BufferData;
-    friend class ::C2Buffer;
+    friend class ::android::C2Buffer;
 };
 
 }  // namespace
@@ -802,3 +803,4 @@
     return std::shared_ptr<C2Buffer>(new C2Buffer({ block }));
 }
 
+} // namespace android
diff --git a/media/libstagefright/codec2/vndk/C2Store.cpp b/media/libstagefright/codec2/vndk/C2Store.cpp
index c4ed2f4..05b46c3 100644
--- a/media/libstagefright/codec2/vndk/C2Store.cpp
+++ b/media/libstagefright/codec2/vndk/C2Store.cpp
@@ -136,13 +136,13 @@
     switch (id) {
     case C2BlockPool::BASIC_LINEAR:
         res = allocatorStore->fetchAllocator(C2AllocatorStore::DEFAULT_LINEAR, &allocator);
-        if (res == C2_OK) {
+        if (res == OK) {
             *pool = std::make_shared<C2BasicLinearBlockPool>(allocator);
         }
         break;
     case C2BlockPool::BASIC_GRAPHIC:
         res = allocatorStore->fetchAllocator(C2AllocatorStore::DEFAULT_GRAPHIC, &allocator);
-        if (res == C2_OK) {
+        if (res == OK) {
             *pool = std::make_shared<C2BasicGraphicBlockPool>(allocator);
         }
         break;
@@ -345,7 +345,7 @@
 
 c2_status_t C2PlatformComponentStore::ComponentModule::createInterface(
         c2_node_id_t id, std::shared_ptr<C2ComponentInterface> *interface,
-        std::function<void(::C2ComponentInterface*)> deleter) {
+        std::function<void(::android::C2ComponentInterface*)> deleter) {
     interface->reset();
     if (mInit != C2_OK) {
         return mInit;
@@ -362,7 +362,7 @@
 
 c2_status_t C2PlatformComponentStore::ComponentModule::createComponent(
         c2_node_id_t id, std::shared_ptr<C2Component> *component,
-        std::function<void(::C2Component*)> deleter) {
+        std::function<void(::android::C2Component*)> deleter) {
     component->reset();
     if (mInit != C2_OK) {
         return mInit;
diff --git a/media/libstagefright/codec2/vndk/include/C2BufferPriv.h b/media/libstagefright/codec2/vndk/include/C2BufferPriv.h
index 977cf7b..875a8c2 100644
--- a/media/libstagefright/codec2/vndk/include/C2BufferPriv.h
+++ b/media/libstagefright/codec2/vndk/include/C2BufferPriv.h
@@ -21,6 +21,8 @@
 
 #include <C2Buffer.h>
 
+namespace android {
+
 class C2BasicLinearBlockPool : public C2BlockPool {
 public:
     explicit C2BasicLinearBlockPool(const std::shared_ptr<C2Allocator> &allocator);
@@ -71,4 +73,6 @@
     const std::shared_ptr<C2Allocator> mAllocator;
 };
 
+} // namespace android
+
 #endif // STAGEFRIGHT_CODEC2_BUFFER_PRIV_H_
diff --git a/media/libstagefright/codec2/vndk/include/C2ComponentFactory.h b/media/libstagefright/codec2/vndk/include/C2ComponentFactory.h
index 7168498..cfea104 100644
--- a/media/libstagefright/codec2/vndk/include/C2ComponentFactory.h
+++ b/media/libstagefright/codec2/vndk/include/C2ComponentFactory.h
@@ -22,6 +22,8 @@
 #include <functional>
 #include <memory>
 
+namespace android {
+
 /**
  * Component factory object that enables to create a component and/or interface from a dynamically
  * linked library. This is needed because the component/interfaces are managed objects, but we
@@ -34,8 +36,8 @@
  */
 class C2ComponentFactory {
 public:
-    typedef std::function<void(::C2Component*)> ComponentDeleter;
-    typedef std::function<void(::C2ComponentInterface*)> InterfaceDeleter;
+    typedef std::function<void(::android::C2Component*)> ComponentDeleter;
+    typedef std::function<void(::android::C2ComponentInterface*)> InterfaceDeleter;
 
     /**
      * Creates a component.
@@ -79,12 +81,9 @@
 
     virtual ~C2ComponentFactory() = default;
 
-    typedef ::C2ComponentFactory* (*CreateCodec2FactoryFunc)(void);
-    typedef void (*DestroyCodec2FactoryFunc)(::C2ComponentFactory*);
+    typedef ::android::C2ComponentFactory* (*CreateCodec2FactoryFunc)(void);
+    typedef void (*DestroyCodec2FactoryFunc)(::android::C2ComponentFactory*);
 };
-
-namespace android {
-    typedef ::C2ComponentFactory C2ComponentFactory;
-}
+} // namespace android
 
 #endif // STAGEFRIGHT_CODEC2_COMPONENT_FACTORY_H_
diff --git a/media/libstagefright/codec2/vndk/include/C2ErrnoUtils.h b/media/libstagefright/codec2/vndk/include/C2ErrnoUtils.h
index 5b995f6..41132b9 100644
--- a/media/libstagefright/codec2/vndk/include/C2ErrnoUtils.h
+++ b/media/libstagefright/codec2/vndk/include/C2ErrnoUtils.h
@@ -20,6 +20,8 @@
 #include <errno.h>
 #include <C2.h>
 
+namespace android {
+
 // standard ERRNO mappings
 template<int N> constexpr c2_status_t _c2_errno2status_impl();
 template<> constexpr c2_status_t _c2_errno2status_impl<0>()       { return C2_OK; }
@@ -50,5 +52,7 @@
     return _c2_map_errno_impl<N...>::map(result);
 }
 
+} // namespace android
+
 #endif // STAGEFRIGHT_CODEC2_ERRNO_UTILS_H_
 
diff --git a/media/libstagefright/codec2/vndk/include/C2PlatformSupport.h b/media/libstagefright/codec2/vndk/include/C2PlatformSupport.h
index afa51ee..76b02ed 100644
--- a/media/libstagefright/codec2/vndk/include/C2PlatformSupport.h
+++ b/media/libstagefright/codec2/vndk/include/C2PlatformSupport.h
@@ -86,7 +86,6 @@
  * \retval nullptr if the platform component store could not be obtained
  */
 std::shared_ptr<C2ComponentStore> GetCodec2PlatformComponentStore();
-
 } // namespace android
 
 #endif // STAGEFRIGHT_CODEC2_PLATFORM_SUPPORT_H_
diff --git a/media/libstagefright/codec2/vndk/include/util/C2ParamUtils.h b/media/libstagefright/codec2/vndk/include/util/C2ParamUtils.h
index 1accc2c..3168248 100644
--- a/media/libstagefright/codec2/vndk/include/util/C2ParamUtils.h
+++ b/media/libstagefright/codec2/vndk/include/util/C2ParamUtils.h
@@ -21,14 +21,13 @@
 #include <util/_C2MacroUtils.h>
 
 #include <iostream>
-#include <list>
-#include <utility>
-#include <vector>
 
 /** \file
  * Utilities for parameter handling to be used by Codec2 implementations.
  */
 
+namespace android {
+
 /// \cond INTERNAL
 
 /* ---------------------------- UTILITIES FOR ENUMERATION REFLECTION ---------------------------- */
@@ -314,5 +313,7 @@
 
 /* ---------------------------- UTILITIES FOR ENUMERATION REFLECTION ---------------------------- */
 
+}  // namespace android
+
 #endif  // C2UTILS_PARAM_UTILS_H_
 
diff --git a/media/libstagefright/codec2/vndk/internal/C2BlockInternal.h b/media/libstagefright/codec2/vndk/internal/C2BlockInternal.h
index 25003cb..9c68369 100644
--- a/media/libstagefright/codec2/vndk/internal/C2BlockInternal.h
+++ b/media/libstagefright/codec2/vndk/internal/C2BlockInternal.h
@@ -19,6 +19,8 @@
 
 #include <C2Buffer.h>
 
+namespace android {
+
 struct _C2BlockPoolData;
 
 /**
@@ -62,5 +64,7 @@
             const C2Rect &allottedCrop = C2Rect(~0u, ~0u));
 };
 
+}
+
 #endif // ANDROID_STAGEFRIGHT_C2BLOCK_INTERNAL_H_
 
diff --git a/media/libstagefright/codec2/vndk/internal/C2ParamInternal.h b/media/libstagefright/codec2/vndk/internal/C2ParamInternal.h
index c805830..5bf3009 100644
--- a/media/libstagefright/codec2/vndk/internal/C2ParamInternal.h
+++ b/media/libstagefright/codec2/vndk/internal/C2ParamInternal.h
@@ -19,6 +19,8 @@
 
 #include <C2Param.h>
 
+namespace android {
+
 struct C2_HIDE _C2ParamInspector {
     inline static uint32_t getIndex(const C2ParamField &pf) {
         return pf._mIndex;
@@ -42,6 +44,7 @@
     }
 };
 
+}
 
 #endif // ANDROID_STAGEFRIGHT_C2PARAM_INTERNAL_H_
 
diff --git a/media/libstagefright/codecs/cmds/codec2.cpp b/media/libstagefright/codecs/cmds/codec2.cpp
index 95b0c61..8022b84 100644
--- a/media/libstagefright/codecs/cmds/codec2.cpp
+++ b/media/libstagefright/codecs/cmds/codec2.cpp
@@ -59,6 +59,11 @@
 #include <C2PlatformSupport.h>
 #include <C2Work.h>
 
+extern "C" ::android::C2ComponentFactory *CreateCodec2Factory();
+extern "C" void DestroyCodec2Factory(::android::C2ComponentFactory *);
+
+#include "../avcdec/C2SoftAvcDec.h"
+
 using namespace android;
 using namespace std::chrono_literals;