Merge "Snap for 6435660 from ca8931333214a559ec8c4fa6b795ca6c7ce0919e to sdk-release" into sdk-release
diff --git a/media/audioserver/audioserver.rc b/media/audioserver/audioserver.rc
index 5484613..48a92f9 100644
--- a/media/audioserver/audioserver.rc
+++ b/media/audioserver/audioserver.rc
@@ -17,5 +17,19 @@
 on property:vts.native_server.on=0
     start audioserver
 
+on property:init.svc.audioserver=stopped
+    stop vendor.audio-hal
+    stop vendor.audio-hal-4-0-msd
+    # Keep the original service names for backward compatibility
+    stop vendor.audio-hal-2-0
+    stop audio-hal-2-0
+
+on property:init.svc.audioserver=running
+    start vendor.audio-hal
+    start vendor.audio-hal-4-0-msd
+    # Keep the original service names for backward compatibility
+    start vendor.audio-hal-2-0
+    start audio-hal-2-0
+
 on init
     mkdir /dev/socket/audioserver 0775 audioserver audioserver
diff --git a/media/codec2/core/Android.bp b/media/codec2/core/Android.bp
index a7e8997..e3bbfd6 100644
--- a/media/codec2/core/Android.bp
+++ b/media/codec2/core/Android.bp
@@ -18,6 +18,10 @@
         "-Werror",
     ],
 
+    header_abi_checker: {
+        check_all_apis: true,
+    },
+
     header_libs: [
         "libcodec2_headers",
         "libhardware_headers",
diff --git a/media/extractors/fuzzers/Android.bp b/media/extractors/fuzzers/Android.bp
index 5fb8155..81fa29a 100644
--- a/media/extractors/fuzzers/Android.bp
+++ b/media/extractors/fuzzers/Android.bp
@@ -81,3 +81,32 @@
 
     dictionary: "mp4_extractor_fuzzer.dict",
 }
+
+cc_fuzz {
+    name: "wav_extractor_fuzzer",
+
+    srcs: [
+        "wav_extractor_fuzzer.cpp",
+    ],
+
+    include_dirs: [
+        "frameworks/av/media/extractors/wav",
+    ],
+
+    static_libs: [
+        "liblog",
+        "libstagefright_foundation",
+        "libmedia",
+        "libextractorfuzzerbase",
+        "libfifo",
+        "libwavextractor",
+    ],
+
+    shared_libs: [
+        "libutils",
+        "libmediandk",
+        "libbinder",
+        "libbinder_ndk",
+        "libbase",
+    ],
+}
diff --git a/media/extractors/fuzzers/README.md b/media/extractors/fuzzers/README.md
index a6c17c0..5f95075 100644
--- a/media/extractors/fuzzers/README.md
+++ b/media/extractors/fuzzers/README.md
@@ -3,6 +3,7 @@
 ## Table of contents
 1. [libextractorfuzzerbase](#ExtractorFuzzerBase)
 2. [libmp4extractor](#mp4ExtractorFuzzer)
+3. [libwavextractor](#wavExtractorFuzzer)
 
 # <a name="ExtractorFuzzerBase"></a> Fuzzer for libextractorfuzzerbase
 All the extractors have a common API - creating a data source, extraction
@@ -49,6 +50,35 @@
   $ adb shell /data/fuzz/arm64/mp4_extractor_fuzzer/mp4_extractor_fuzzer CORPUS_DIR
 ```
 
+# <a name="wavExtractorFuzzer"></a> Fuzzer for libwavextractor
+
+## Plugin Design Considerations
+The fuzzer plugin for WAV extractor uses the `ExtractorFuzzerBase` class and
+implements only the `createExtractor` to create the WAV extractor class.
+
+
+## Build
+
+This describes steps to build wav_extractor_fuzzer binary.
+
+### Android
+
+#### Steps to build
+Build the fuzzer
+```
+  $ mm -j$(nproc) wav_extractor_fuzzer
+```
+
+#### Steps to run
+Create a directory CORPUS_DIR and copy some wav files to that folder
+Push this directory to device.
+
+To run on device
+```
+  $ adb sync data
+  $ adb shell /data/fuzz/arm64/wav_extractor_fuzzer/wav_extractor_fuzzer CORPUS_DIR
+```
+
 ## References:
  * http://llvm.org/docs/LibFuzzer.html
  * https://github.com/google/oss-fuzz
diff --git a/media/extractors/fuzzers/wav_extractor_fuzzer.cpp b/media/extractors/fuzzers/wav_extractor_fuzzer.cpp
new file mode 100644
index 0000000..1397122
--- /dev/null
+++ b/media/extractors/fuzzers/wav_extractor_fuzzer.cpp
@@ -0,0 +1,62 @@
+/******************************************************************************
+ *
+ * Copyright (C) 2020 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.
+ *
+ *****************************************************************************
+ * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
+ */
+
+#include "ExtractorFuzzerBase.h"
+
+#include "WAVExtractor.h"
+
+using namespace android;
+
+class wavExtractor : public ExtractorFuzzerBase {
+ public:
+  wavExtractor() = default;
+  ~wavExtractor() = default;
+
+  bool createExtractor();
+};
+
+bool wavExtractor::createExtractor() {
+  mExtractor = new WAVExtractor(new DataSourceHelper(mDataSource->wrap()));
+  if (!mExtractor) {
+    return false;
+  }
+  mExtractor->name();
+  return true;
+}
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+  if ((!data) || (size == 0)) {
+    return 0;
+  }
+  wavExtractor* extractor = new wavExtractor();
+  if (!extractor) {
+    return 0;
+  }
+  if (extractor->setDataSource(data, size)) {
+    if (extractor->createExtractor()) {
+      extractor->getExtractorDef();
+      extractor->getMetadata();
+      extractor->extractTracks();
+      extractor->getTracksMetadata();
+    }
+  }
+  delete extractor;
+  return 0;
+}
diff --git a/media/libaudioclient/AudioRecord.cpp b/media/libaudioclient/AudioRecord.cpp
index 271e186..3d0d622 100644
--- a/media/libaudioclient/AudioRecord.cpp
+++ b/media/libaudioclient/AudioRecord.cpp
@@ -388,6 +388,9 @@
     mFramesReadServerOffset -= mFramesRead + framesFlushed;
     mFramesRead = 0;
     mProxy->clearTimestamp();  // timestamp is invalid until next server push
+    mPreviousTimestamp.clear();
+    mTimestampRetrogradePositionReported = false;
+    mTimestampRetrogradeTimeReported = false;
 
     // reset current position as seen by client to 0
     mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
@@ -570,6 +573,39 @@
                 timestamp->mPosition[i] += mFramesReadServerOffset;
             }
         }
+
+        bool timestampRetrogradeTimeReported = false;
+        bool timestampRetrogradePositionReported = false;
+        for (int i = 0; i < ExtendedTimestamp::LOCATION_MAX; ++i) {
+            if (timestamp->mTimeNs[i] >= 0 && mPreviousTimestamp.mTimeNs[i] >= 0) {
+                if (timestamp->mTimeNs[i] < mPreviousTimestamp.mTimeNs[i]) {
+                    if (!mTimestampRetrogradeTimeReported) {
+                        ALOGD("%s: retrograde time adjusting [%d] current:%lld to previous:%lld",
+                                __func__, i, (long long)timestamp->mTimeNs[i],
+                                (long long)mPreviousTimestamp.mTimeNs[i]);
+                        timestampRetrogradeTimeReported = true;
+                    }
+                    timestamp->mTimeNs[i] = mPreviousTimestamp.mTimeNs[i];
+                }
+                if (timestamp->mPosition[i] < mPreviousTimestamp.mPosition[i]) {
+                    if (!mTimestampRetrogradePositionReported) {
+                        ALOGD("%s: retrograde position"
+                                " adjusting [%d] current:%lld to previous:%lld",
+                                __func__, i, (long long)timestamp->mPosition[i],
+                                (long long)mPreviousTimestamp.mPosition[i]);
+                        timestampRetrogradePositionReported = true;
+                    }
+                    timestamp->mPosition[i] = mPreviousTimestamp.mPosition[i];
+                }
+            }
+        }
+        mPreviousTimestamp = *timestamp;
+        if (timestampRetrogradeTimeReported) {
+            mTimestampRetrogradeTimeReported = true;
+        }
+        if (timestampRetrogradePositionReported) {
+            mTimestampRetrogradePositionReported = true;
+        }
     }
     return status;
 }
diff --git a/media/libaudioclient/include/media/AudioRecord.h b/media/libaudioclient/include/media/AudioRecord.h
index 574302b..20a7d14 100644
--- a/media/libaudioclient/include/media/AudioRecord.h
+++ b/media/libaudioclient/include/media/AudioRecord.h
@@ -711,6 +711,10 @@
 
     bool                    mInOverrun;         // whether recorder is currently in overrun state
 
+    ExtendedTimestamp       mPreviousTimestamp{}; // used to detect retrograde motion
+    bool                    mTimestampRetrogradePositionReported = false; // reduce log spam
+    bool                    mTimestampRetrogradeTimeReported = false;     // reduce log spam
+
 private:
     class DeathNotifier : public IBinder::DeathRecipient {
     public:
diff --git a/media/libmedia/xsd/vts/Android.bp b/media/libmedia/xsd/vts/Android.bp
index 4739504..598e41b 100644
--- a/media/libmedia/xsd/vts/Android.bp
+++ b/media/libmedia/xsd/vts/Android.bp
@@ -31,4 +31,12 @@
         "-Wall",
         "-Werror",
     ],
+    data: [
+        ":media_profiles",
+    ],
+    test_suites: [
+        "general-tests",
+        "vts"
+    ],
+    test_config: "vts_mediaProfiles_validate_test.xml",
 }
diff --git a/media/libmedia/xsd/vts/vts_mediaProfiles_validate_test.xml b/media/libmedia/xsd/vts/vts_mediaProfiles_validate_test.xml
new file mode 100644
index 0000000..08ab8f4
--- /dev/null
+++ b/media/libmedia/xsd/vts/vts_mediaProfiles_validate_test.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 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.
+-->
+<configuration description="Runs vts_mediaProfiles_validate_test.">
+    <option name="test-suite-tag" value="apct" />
+    <option name="test-suite-tag" value="apct-native" />
+
+    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer">
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
+        <option name="cleanup" value="true" />
+        <option name="push" value="media_profiles.xsd->/data/local/tmp/media_profiles.xsd" />
+        <option name="push" value="vts_mediaProfiles_validate_test->/data/local/tmp/vts_mediaProfiles_validate_test" />
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.GTest" >
+        <option name="native-test-device-path" value="/data/local/tmp" />
+        <option name="module-name" value="vts_mediaProfiles_validate_test" />
+    </test>
+</configuration>
diff --git a/media/libmediahelper/Android.bp b/media/libmediahelper/Android.bp
index 72edeec..ae135af 100644
--- a/media/libmediahelper/Android.bp
+++ b/media/libmediahelper/Android.bp
@@ -2,6 +2,12 @@
     name: "libmedia_helper_headers",
     vendor_available: true,
     export_include_dirs: ["include"],
+    host_supported: true,
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
 }
 
 cc_library {
@@ -26,4 +32,10 @@
         "libmedia_helper_headers",
     ],
     clang: true,
+    host_supported: true,
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
 }
diff --git a/media/libstagefright/Android.bp b/media/libstagefright/Android.bp
index 18dacb8..abb58be 100644
--- a/media/libstagefright/Android.bp
+++ b/media/libstagefright/Android.bp
@@ -2,6 +2,12 @@
     name: "libstagefright_headers",
     export_include_dirs: ["include"],
     vendor_available: true,
+    host_supported: true,
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
 }
 
 cc_library_static {
diff --git a/media/libstagefright/MediaExtractorFactory.cpp b/media/libstagefright/MediaExtractorFactory.cpp
index 9e85475..8bce917 100644
--- a/media/libstagefright/MediaExtractorFactory.cpp
+++ b/media/libstagefright/MediaExtractorFactory.cpp
@@ -298,6 +298,12 @@
 #endif
             "/extractors", NULL, *newList);
 
+    RegisterExtractors("/system_ext/lib"
+#ifdef __LP64__
+            "64"
+#endif
+            "/extractors", NULL, *newList);
+
     newList->sort(compareFunc);
     gPlugins = newList;
 
diff --git a/media/libstagefright/codecs/m4v_h263/dec/Android.bp b/media/libstagefright/codecs/m4v_h263/dec/Android.bp
index b8b83d5..1d7c703 100644
--- a/media/libstagefright/codecs/m4v_h263/dec/Android.bp
+++ b/media/libstagefright/codecs/m4v_h263/dec/Android.bp
@@ -47,6 +47,7 @@
         "-DOSCL_IMPORT_REF=",
 
         "-Werror",
+        "-DDEC_NOPOSTPROC",
     ],
 
     version_script: "exports.lds",
diff --git a/media/libstagefright/codecs/m4v_h263/dec/src/pvdec_api.cpp b/media/libstagefright/codecs/m4v_h263/dec/src/pvdec_api.cpp
index 9c0fcfa..6c336c8 100644
--- a/media/libstagefright/codecs/m4v_h263/dec/src/pvdec_api.cpp
+++ b/media/libstagefright/codecs/m4v_h263/dec/src/pvdec_api.cpp
@@ -830,7 +830,14 @@
 OSCL_EXPORT_REF void PVSetPostProcType(VideoDecControls *decCtrl, int mode)
 {
     VideoDecData *video = (VideoDecData *)decCtrl->videoDecoderData;
+#ifdef PV_POSTPROC_ON
     video->postFilterType = mode;
+#else
+    if (mode != 0) {
+        ALOGE("Post processing filters are not supported");
+    }
+    video->postFilterType = 0;
+#endif
 }
 
 
diff --git a/media/libstagefright/foundation/Android.bp b/media/libstagefright/foundation/Android.bp
index effbb4e..5c927e6 100644
--- a/media/libstagefright/foundation/Android.bp
+++ b/media/libstagefright/foundation/Android.bp
@@ -23,7 +23,6 @@
     ],
 
     header_libs: [
-        "libhardware_headers",
         "libstagefright_foundation_headers",
         "media_ndk_headers",
         "media_plugin_headers",
diff --git a/media/libstagefright/foundation/avc_utils.cpp b/media/libstagefright/foundation/avc_utils.cpp
index e8a6083..8605925 100644
--- a/media/libstagefright/foundation/avc_utils.cpp
+++ b/media/libstagefright/foundation/avc_utils.cpp
@@ -546,11 +546,9 @@
     CHECK_NE(video_object_type_indication,
              0x21u /* Fine Granularity Scalable */);
 
-    unsigned video_object_layer_verid __unused;
-    unsigned video_object_layer_priority __unused;
     if (br.getBits(1)) {
-        video_object_layer_verid = br.getBits(4);
-        video_object_layer_priority = br.getBits(3);
+        br.skipBits(4); //video_object_layer_verid
+        br.skipBits(3); //video_object_layer_priority
     }
     unsigned aspect_ratio_info = br.getBits(4);
     if (aspect_ratio_info == 0x0f /* extended PAR */) {
@@ -609,7 +607,7 @@
     unsigned video_object_layer_height = br.getBits(13);
     CHECK(br.getBits(1));  // marker_bit
 
-    unsigned interlaced __unused = br.getBits(1);
+    br.skipBits(1); // interlaced
 
     *width = video_object_layer_width;
     *height = video_object_layer_height;
@@ -655,7 +653,7 @@
         return false;
     }
 
-    unsigned protection __unused = (header >> 16) & 1;
+    // we can get protection value from (header >> 16) & 1
 
     unsigned bitrate_index = (header >> 12) & 0x0f;
 
diff --git a/media/libstagefright/foundation/include/media/stagefright/foundation/ADebug.h b/media/libstagefright/foundation/include/media/stagefright/foundation/ADebug.h
index ab17a02..e4b99bf 100644
--- a/media/libstagefright/foundation/include/media/stagefright/foundation/ADebug.h
+++ b/media/libstagefright/foundation/include/media/stagefright/foundation/ADebug.h
@@ -148,7 +148,8 @@
     static char *GetDebugName(const char *name);
 
     inline static bool isExperimentEnabled(
-            const char *name __unused /* nonnull */, bool allow __unused = true) {
+            const char *name __attribute__((unused)) /* nonnull */,
+            bool allow __attribute__((unused)) = true) {
 #ifdef ENABLE_STAGEFRIGHT_EXPERIMENTS
         if (!strcmp(name, "legacy-adaptive")) {
             return getExperimentFlag(allow, name, 2, 1); // every other day
diff --git a/media/libstagefright/id3/test/ID3Test.cpp b/media/libstagefright/id3/test/ID3Test.cpp
index a8f1470..d32f9a6 100644
--- a/media/libstagefright/id3/test/ID3Test.cpp
+++ b/media/libstagefright/id3/test/ID3Test.cpp
@@ -64,7 +64,7 @@
     ID3 tag(file.get());
     ASSERT_TRUE(tag.isValid()) << "No valid ID3 tag found for " << path.c_str() << "\n";
     ASSERT_TRUE(tag.version() >= versionNumber)
-            << "Expected version: " << tag.version() << " Found version: " << versionNumber;
+            << "Found version: " << tag.version() << " Expected version: " << versionNumber;
 }
 
 TEST_P(ID3textTagTest, TextTagTest) {
@@ -109,7 +109,7 @@
         if (data) {
             ALOGV("Found album art: size = %zu mime = %s \n", dataSize, mime.string());
         }
-        ASSERT_NE(data, nullptr) << "Expected album art, found none!" << path;
+        ASSERT_NE(data, nullptr) << "Expected album art, found none! " << path;
     } else {
         ASSERT_EQ(data, nullptr) << "Found album art when expected none!";
     }
@@ -144,7 +144,7 @@
                 hexdump(data, dataSize > 128 ? 128 : dataSize);
 #endif
             }
-            ASSERT_NE(data, nullptr) << "Expected album art, found none!" << path;
+            ASSERT_NE(data, nullptr) << "Expected album art, found none! " << path;
         }
         it.next();
     }
@@ -162,16 +162,18 @@
                                            "bbb_44100hz_2ch_128kbps_mp3_5mins_largeSize.mp3",
                                            "bbb_44100hz_2ch_128kbps_mp3_30sec_moreTextFrames.mp3"));
 
+// TODO: need some data that is not V2.3
 INSTANTIATE_TEST_SUITE_P(
         id3TestAll, ID3versionTest,
-        ::testing::Values(make_pair("bbb_44100hz_2ch_128kbps_mp3_30sec.mp3", 4),
-                          make_pair("bbb_44100hz_2ch_128kbps_mp3_30sec_1_image.mp3", 4),
-                          make_pair("bbb_44100hz_2ch_128kbps_mp3_30sec_2_image.mp3", 4),
-                          make_pair("bbb_44100hz_2ch_128kbps_mp3_5mins.mp3", 4),
-                          make_pair("bbb_44100hz_2ch_128kbps_mp3_5mins_1_image.mp3", 4),
-                          make_pair("bbb_44100hz_2ch_128kbps_mp3_5mins_2_image.mp3", 4),
-                          make_pair("bbb_44100hz_2ch_128kbps_mp3_5mins_largeSize.mp3", 4),
-                          make_pair("bbb_44100hz_2ch_128kbps_mp3_30sec_moreTextFrames.mp3", 4)));
+        ::testing::Values(
+              make_pair("bbb_44100hz_2ch_128kbps_mp3_30sec.mp3", ID3::ID3_V2_3),
+              make_pair("bbb_44100hz_2ch_128kbps_mp3_30sec_1_image.mp3", ID3::ID3_V2_3),
+              make_pair("bbb_44100hz_2ch_128kbps_mp3_30sec_2_image.mp3", ID3::ID3_V2_3),
+              make_pair("bbb_44100hz_2ch_128kbps_mp3_5mins.mp3", ID3::ID3_V2_3),
+              make_pair("bbb_44100hz_2ch_128kbps_mp3_5mins_1_image.mp3", ID3::ID3_V2_3),
+              make_pair("bbb_44100hz_2ch_128kbps_mp3_5mins_2_image.mp3", ID3::ID3_V2_3),
+              make_pair("bbb_44100hz_2ch_128kbps_mp3_5mins_largeSize.mp3", ID3::ID3_V2_3),
+              make_pair("bbb_44100hz_2ch_128kbps_mp3_30sec_moreTextFrames.mp3", ID3::ID3_V2_3)));
 
 INSTANTIATE_TEST_SUITE_P(
         id3TestAll, ID3textTagTest,
diff --git a/media/libstagefright/tests/HEVC/Android.bp b/media/libstagefright/tests/HEVC/Android.bp
new file mode 100644
index 0000000..7a6b959
--- /dev/null
+++ b/media/libstagefright/tests/HEVC/Android.bp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+cc_test {
+    name: "HEVCUtilsUnitTest",
+    gtest: true,
+
+    srcs: [
+        "HEVCUtilsUnitTest.cpp",
+    ],
+
+    shared_libs: [
+        "libutils",
+        "liblog",
+    ],
+
+    static_libs: [
+        "libstagefright",
+        "libstagefright_foundation",
+    ],
+
+    include_dirs: [
+        "frameworks/av/media/libstagefright",
+    ],
+
+    cflags: [
+        "-Werror",
+        "-Wall",
+    ],
+
+    sanitize: {
+        cfi: true,
+        misc_undefined: [
+            "unsigned-integer-overflow",
+            "signed-integer-overflow",
+        ],
+    },
+}
diff --git a/media/libstagefright/tests/HEVC/AndroidTest.xml b/media/libstagefright/tests/HEVC/AndroidTest.xml
new file mode 100644
index 0000000..ff850a2
--- /dev/null
+++ b/media/libstagefright/tests/HEVC/AndroidTest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 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.
+-->
+<configuration description="Test module config for HEVC Utils unit tests">
+    <option name="test-suite-tag" value="HEVCUtilsUnitTest" />
+    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
+        <option name="cleanup" value="false" />
+        <option name="push" value="HEVCUtilsUnitTest->/data/local/tmp/HEVCUtilsUnitTest" />
+        <option name="push-file"
+            key="https://storage.googleapis.com/android_media/frameworks/av/media/libstagefright/foundation/tests/HEVCUtils/HEVCUtilsUnitTest.zip?unzip=true"
+            value="/data/local/tmp/HEVCUtilsUnitTest/" />
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.GTest" >
+        <option name="native-test-device-path" value="/data/local/tmp" />
+        <option name="module-name" value="HEVCUtilsUnitTest" />
+        <option name="native-test-flag" value="-P /data/local/tmp/HEVCUtilsUnitTest/" />
+    </test>
+</configuration>
diff --git a/media/libstagefright/tests/HEVC/HEVCUtilsTestEnvironment.h b/media/libstagefright/tests/HEVC/HEVCUtilsTestEnvironment.h
new file mode 100644
index 0000000..e4481e1
--- /dev/null
+++ b/media/libstagefright/tests/HEVC/HEVCUtilsTestEnvironment.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2020 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 __HEVC_UTILS_TEST_ENVIRONMENT_H__
+#define __HEVC_UTILS_TEST_ENVIRONMENT_H__
+
+#include <gtest/gtest.h>
+
+#include <getopt.h>
+
+using namespace std;
+
+class HEVCUtilsTestEnvironment : public::testing::Environment {
+  public:
+    HEVCUtilsTestEnvironment() : res("/data/local/tmp/") {}
+
+    // Parses the command line arguments
+    int initFromOptions(int argc, char **argv);
+
+    void setRes(const char *_res) { res = _res; }
+
+    const string getRes() const { return res; }
+
+  private:
+    string res;
+};
+
+int HEVCUtilsTestEnvironment::initFromOptions(int argc, char **argv) {
+    static struct option options[] = {{"path", required_argument, 0, 'P'}, {0, 0, 0, 0}};
+
+    while (true) {
+        int index = 0;
+        int c = getopt_long(argc, argv, "P:", options, &index);
+        if (c == -1) {
+            break;
+        }
+
+        switch (c) {
+            case 'P': {
+                setRes(optarg);
+                break;
+            }
+            default:
+                break;
+        }
+    }
+
+    if (optind < argc) {
+        fprintf(stderr,
+                "unrecognized option: %s\n\n"
+                "usage: %s <gtest options> <test options>\n\n"
+                "test options are:\n\n"
+                "-P, --path: Resource files directory location\n",
+                argv[optind ?: 1], argv[0]);
+        return 2;
+    }
+    return 0;
+}
+
+#endif  // __HEVC_UTILS_TEST_ENVIRONMENT_H__
diff --git a/media/libstagefright/tests/HEVC/HEVCUtilsUnitTest.cpp b/media/libstagefright/tests/HEVC/HEVCUtilsUnitTest.cpp
new file mode 100644
index 0000000..324a042
--- /dev/null
+++ b/media/libstagefright/tests/HEVC/HEVCUtilsUnitTest.cpp
@@ -0,0 +1,208 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "HevcUtilityTest"
+#include <utils/Log.h>
+
+#include <fstream>
+
+#include <media/stagefright/foundation/ABitReader.h>
+#include "include/HevcUtils.h"
+
+#include "HEVCUtilsTestEnvironment.h"
+
+using namespace android;
+
+// max size of hvcc box is 2 KB
+constexpr uint32_t kHvccBoxMaxSize = 2048;
+constexpr uint32_t kHvccBoxMinSize = 20;
+constexpr uint32_t kVPSCode = 32;
+constexpr uint32_t kSPSCode = 33;
+constexpr uint32_t kPPSCode = 34;
+constexpr uint32_t kNALSizeLength = 2;
+
+static HEVCUtilsTestEnvironment *gEnv = nullptr;
+
+class HEVCUtilsUnitTest
+    : public ::testing::TestWithParam<
+              tuple</*fileName*/ string, /*infoFileName*/ string, /*numVPSNals*/ size_t,
+                    /*numSPSNals*/ size_t, /*numPPSNals*/ size_t, /*frameRate*/ int16_t,
+                    /*isHdr*/ bool>> {
+  public:
+    ~HEVCUtilsUnitTest() {
+        if (mMediaFileStream.is_open()) mMediaFileStream.close();
+        if (mInfoFileStream.is_open()) mInfoFileStream.close();
+    }
+
+    virtual void SetUp() override {
+        tuple<string, string, size_t, size_t, size_t, int16_t, bool> params = GetParam();
+        string inputMediaFile = gEnv->getRes() + get<0>(params);
+        mMediaFileStream.open(inputMediaFile, ifstream::in);
+        ASSERT_TRUE(mMediaFileStream.is_open()) << "Failed to open media file: " << inputMediaFile;
+
+        string inputInfoFile = gEnv->getRes() + get<1>(params);
+        mInfoFileStream.open(inputInfoFile, ifstream::in);
+        ASSERT_TRUE(mInfoFileStream.is_open()) << "Failed to open info file: " << inputInfoFile;
+
+        mNumVPSNals = get<2>(params);
+        mNumSPSNals = get<3>(params);
+        mNumPPSNals = get<4>(params);
+        mFrameRate = get<5>(params);
+        mIsHDR = get<6>(params);
+    }
+
+    size_t mNumVPSNals;
+    size_t mNumSPSNals;
+    size_t mNumPPSNals;
+    int16_t mFrameRate;
+    bool mIsHDR;
+    ifstream mMediaFileStream;
+    ifstream mInfoFileStream;
+};
+
+TEST_P(HEVCUtilsUnitTest, NALUnitTest) {
+    HevcParameterSets hevcParams;
+
+    string line;
+    int32_t index = 0;
+    status_t err;
+    while (getline(mInfoFileStream, line)) {
+        string type;
+        int32_t chunkLength;
+
+        istringstream stringLine(line);
+        stringLine >> type >> chunkLength;
+        ASSERT_GT(chunkLength, 0) << "Length of data chunk must be greater than 0";
+
+        char *data = (char *)malloc(chunkLength);
+        ASSERT_NE(data, nullptr) << "Failed to allocate data buffer of size: " << chunkLength;
+
+        mMediaFileStream.read(data, chunkLength);
+        ASSERT_EQ(mMediaFileStream.gcount(), chunkLength)
+                << "Failed to read complete file, bytes read: " << mMediaFileStream.gcount();
+
+        // A valid startcode consists of at least two 0x00 bytes followed by 0x01.
+        int32_t offset = 0;
+        for (; offset + 2 < chunkLength; ++offset) {
+            if (data[offset + 2] == 0x01 && data[offset + 1] == 0x00 && data[offset] == 0x00) {
+                break;
+            }
+        }
+        offset += 3;
+        ASSERT_LE(offset, chunkLength) << "NAL unit offset must not exceed the chunk length";
+
+        uint8_t *nalUnit = (uint8_t *)(data + offset);
+        size_t nalUnitLength = chunkLength - offset;
+
+        // Add NAL units only if they're of type: VPS/SPS/PPS/SEI
+        if (!((type.compare("VPS") && type.compare("SPS") && type.compare("PPS") &&
+               type.compare("SEI")))) {
+            err = hevcParams.addNalUnit(nalUnit, nalUnitLength);
+            ASSERT_EQ(err, (status_t)OK)
+                    << "Failed to add NAL Unit type: " << type << " Size: " << nalUnitLength;
+
+            size_t sizeNalUnit = hevcParams.getSize(index);
+            ASSERT_EQ(sizeNalUnit, nalUnitLength) << "Invalid size returned for NAL: " << type;
+
+            uint8_t *destination = (uint8_t *)malloc(nalUnitLength);
+            ASSERT_NE(destination, nullptr)
+                    << "Failed to allocate buffer of size: " << nalUnitLength;
+
+            bool status = hevcParams.write(index, destination, nalUnitLength);
+            ASSERT_TRUE(status) << "Unable to write NAL Unit data";
+
+            free(destination);
+            index++;
+        } else {
+            err = hevcParams.addNalUnit(nalUnit, nalUnitLength);
+            ASSERT_NE(err, (status_t)OK) << "Invalid NAL Unit added, type: " << type;
+        }
+        free(data);
+    }
+
+    size_t numNalUnits = hevcParams.getNumNalUnitsOfType(kVPSCode);
+    ASSERT_EQ(numNalUnits, mNumVPSNals) << "Wrong number of VPS NAL Units";
+
+    numNalUnits = hevcParams.getNumNalUnitsOfType(kSPSCode);
+    ASSERT_EQ(numNalUnits, mNumSPSNals) << "Wrong number of SPS NAL Units";
+
+    numNalUnits = hevcParams.getNumNalUnitsOfType(kPPSCode);
+    ASSERT_EQ(numNalUnits, mNumPPSNals) << "Wrong number of PPS NAL Units";
+
+    HevcParameterSets::Info info = hevcParams.getInfo();
+    ASSERT_EQ(info & HevcParameterSets::kInfoIsHdr,
+              (mIsHDR ? HevcParameterSets::kInfoIsHdr : HevcParameterSets::kInfoNone))
+            << "Wrong info about HDR";
+
+    ASSERT_EQ(info & HevcParameterSets::kInfoHasColorDescription,
+              (mIsHDR ? HevcParameterSets::kInfoHasColorDescription : HevcParameterSets::kInfoNone))
+            << "Wrong info about color description";
+
+    // an HEVC file starts with VPS, SPS and PPS NAL units in sequence.
+    uint8_t typeNalUnit = hevcParams.getType(0);
+    ASSERT_EQ(typeNalUnit, kHevcNalUnitTypeVps)
+            << "Expected NAL type: 32(VPS), found: " << typeNalUnit;
+
+    typeNalUnit = hevcParams.getType(1);
+    ASSERT_EQ(typeNalUnit, kHevcNalUnitTypeSps)
+            << "Expected NAL type: 33(SPS), found: " << typeNalUnit;
+
+    typeNalUnit = hevcParams.getType(2);
+    ASSERT_EQ(typeNalUnit, kHevcNalUnitTypePps)
+            << "Expected NAL type: 34(PPS), found: " << typeNalUnit;
+
+    size_t hvccBoxSize = kHvccBoxMaxSize;
+    uint8_t *hvcc = (uint8_t *)malloc(kHvccBoxMaxSize);
+    ASSERT_NE(hvcc, nullptr) << "Failed to allocate a hvcc buffer of size: " << kHvccBoxMaxSize;
+
+    err = hevcParams.makeHvcc(hvcc, &hvccBoxSize, kNALSizeLength);
+    ASSERT_EQ(err, (status_t)OK) << "Unable to create hvcc box";
+
+    ASSERT_GT(hvccBoxSize, kHvccBoxMinSize)
+            << "Hvcc box size must be greater than " << kHvccBoxMinSize;
+
+    int16_t frameRate = hvcc[kHvccBoxMinSize - 1] | (hvcc[kHvccBoxMinSize] << 8);
+    if (frameRate != mFrameRate)
+        cout << "[   WARN   ] Expected frame rate: " << mFrameRate << " Found: " << frameRate
+             << endl;
+
+    free(hvcc);
+}
+
+// Info File contains the type and length for each chunk/frame
+INSTANTIATE_TEST_SUITE_P(
+        HEVCUtilsUnitTestAll, HEVCUtilsUnitTest,
+        ::testing::Values(make_tuple("crowd_3840x2160p50f300_32500kbps.hevc",
+                                     "crowd_3840x2160p50f300_32500kbps.info", 1, 1, 1, 50, false),
+                          make_tuple("crowd_1920x1080p24f300_4500kbps.hevc",
+                                     "crowd_1920x1080p24f300_4500kbps.info", 1, 1, 1, 24, false),
+                          make_tuple("crowd_1280x720p24f300_3000kbps.hevc",
+                                     "crowd_1280x720p24f300_3000kbps.info", 1, 1, 1, 24, false),
+                          make_tuple("crowd_640x360p24f300_500kbps.hevc",
+                                     "crowd_640x360p24f300_500kbps.info", 1, 1, 1, 24, false)));
+
+int main(int argc, char **argv) {
+    gEnv = new HEVCUtilsTestEnvironment();
+    ::testing::AddGlobalTestEnvironment(gEnv);
+    ::testing::InitGoogleTest(&argc, argv);
+    int status = gEnv->initFromOptions(argc, argv);
+    if (status == 0) {
+        status = RUN_ALL_TESTS();
+        ALOGV("Test result = %d\n", status);
+    }
+    return status;
+}
diff --git a/media/libstagefright/tests/HEVC/README.md b/media/libstagefright/tests/HEVC/README.md
new file mode 100644
index 0000000..fa0e99c
--- /dev/null
+++ b/media/libstagefright/tests/HEVC/README.md
@@ -0,0 +1,39 @@
+## Media Testing ##
+---
+#### HEVC Utils Test
+The HEVC Utility Unit Test Suite validates the HevcUtils library available in libstagefright.
+
+Run the following steps to build the test suite:
+```
+m HEVCUtilsUnitTest
+```
+
+The 32-bit binaries will be created in the following path : ${OUT}/data/nativetest/
+
+The 64-bit binaries will be created in the following path : ${OUT}/data/nativetest64/
+
+To test 64-bit binary push binaries from nativetest64.
+```
+adb push ${OUT}/data/nativetest64/HEVCUtilsUnitTest/HEVCUtilsUnitTest /data/local/tmp/
+```
+
+To test 32-bit binary push binaries from nativetest.
+```
+adb push ${OUT}/data/nativetest/HEVCUtilsUnitTest/HEVCUtilsUnitTest /data/local/tmp/
+```
+
+The resource file for the tests is taken from [here](https://storage.googleapis.com/android_media/frameworks/av/media/libstagefright/foundation/tests/HEVCUtils/HEVCUtilsUnitTest.zip). Download, unzip and push these files into device for testing.
+
+```
+adb push HEVCUtilsUnitTest /data/local/tmp/
+```
+
+usage: HEVCUtilsUnitTest -P \<path_to_folder\>
+```
+adb shell /data/local/tmp/HEVCUtilsUnitTest -P /data/local/tmp/HEVCUtilsUnitTest/
+```
+Alternatively, the test can also be run using atest command.
+
+```
+atest HEVCUtilsUnitTest -- --enable-module-dynamic-download=true
+```
diff --git a/media/libstagefright/tests/writer/AndroidTest.xml b/media/libstagefright/tests/writer/AndroidTest.xml
index d831555..a21be8a 100644
--- a/media/libstagefright/tests/writer/AndroidTest.xml
+++ b/media/libstagefright/tests/writer/AndroidTest.xml
@@ -19,12 +19,13 @@
         <option name="cleanup" value="true" />
         <option name="push" value="writerTest->/data/local/tmp/writerTest" />
         <option name="push-file"
-            key="https://storage.googleapis.com/android_media/frameworks/av/media/libstagefright/tests/writer/Writer.zip?unzip=true"
-            value="/data/local/tmp/writerTestRes/" />
+            key="https://storage.googleapis.com/android_media/frameworks/av/media/libstagefright/tests/writer/WriterTestRes.zip?unzip=true"
+            value="/data/local/tmp/WriterTestRes/" />
     </target_preparer>
     <test class="com.android.tradefed.testtype.GTest" >
         <option name="native-test-device-path" value="/data/local/tmp" />
         <option name="module-name" value="writerTest" />
-        <option name="native-test-flag" value="-P /data/local/tmp/writerTestRes/" />
+        <option name="native-test-flag" value="-P /data/local/tmp/WriterTestRes/" />
+        <option name="native-test-flag" value="-C true" />
     </test>
 </configuration>
diff --git a/media/libstagefright/tests/writer/README.md b/media/libstagefright/tests/writer/README.md
index ae07917..e103613 100644
--- a/media/libstagefright/tests/writer/README.md
+++ b/media/libstagefright/tests/writer/README.md
@@ -19,13 +19,18 @@
 
 adb push ${OUT}/data/nativetest/writerTest/writerTest /data/local/tmp/
 
-The resource file for the tests is taken from [here](https://storage.googleapis.com/android_media/frameworks/av/media/libstagefright/tests/writer/writerTestRes.zip).
+The resource file for the tests is taken from [here](https://storage.googleapis.com/android_media/frameworks/av/media/libstagefright/tests/writer/WriterTestRes.zip).
 Download and extract the folder. Push all the files in this folder to /data/local/tmp/ on the device.
 ```
-adb push writerTestRes /data/local/tmp/
+adb push WriterTestRes /data/local/tmp/
 ```
 
-usage: writerTest -P \<path_to_res_folder\>
+usage: writerTest -P \<path_to_res_folder\> -C <remove_output_file>
 ```
-adb shell /data/local/tmp/writerTest -P /data/local/tmp/
+adb shell /data/local/tmp/writerTest -P /data/local/tmp/WriterTestRes/ -C true
+```
+Alternatively, the test can also be run using atest command.
+
+```
+atest writerTest -- --enable-module-dynamic-download=true
 ```
diff --git a/media/libstagefright/tests/writer/WriterTest.cpp b/media/libstagefright/tests/writer/WriterTest.cpp
index 3fa2aa6..4d8df2d 100644
--- a/media/libstagefright/tests/writer/WriterTest.cpp
+++ b/media/libstagefright/tests/writer/WriterTest.cpp
@@ -107,6 +107,7 @@
         }
         mBufferInfo.clear();
         if (mInputStream.is_open()) mInputStream.close();
+        if (gEnv->cleanUp()) remove(OUTPUT_FILE_NAME);
     }
 
     void setupWriterType(string writerFormat) {
diff --git a/media/libstagefright/tests/writer/WriterTestEnvironment.h b/media/libstagefright/tests/writer/WriterTestEnvironment.h
index 99e686f..7da0a62 100644
--- a/media/libstagefright/tests/writer/WriterTestEnvironment.h
+++ b/media/libstagefright/tests/writer/WriterTestEnvironment.h
@@ -25,7 +25,7 @@
 
 class WriterTestEnvironment : public ::testing::Environment {
   public:
-    WriterTestEnvironment() : res("/data/local/tmp/") {}
+    WriterTestEnvironment() : res("/data/local/tmp/"), deleteOutput(true) {}
 
     // Parses the command line arguments
     int initFromOptions(int argc, char **argv);
@@ -34,16 +34,21 @@
 
     const string getRes() const { return res; }
 
+    bool cleanUp() const { return deleteOutput; }
+
   private:
     string res;
+    bool deleteOutput;
 };
 
 int WriterTestEnvironment::initFromOptions(int argc, char **argv) {
-    static struct option options[] = {{"res", required_argument, 0, 'P'}, {0, 0, 0, 0}};
+    static struct option options[] = {{"res", required_argument, 0, 'P'},
+                                      {"cleanUp", optional_argument, 0, 'C'},
+                                      {0, 0, 0, 0}};
 
     while (true) {
         int index = 0;
-        int c = getopt_long(argc, argv, "P:", options, &index);
+        int c = getopt_long(argc, argv, "P:C:", options, &index);
         if (c == -1) {
             break;
         }
@@ -52,6 +57,11 @@
             case 'P':
                 setRes(optarg);
                 break;
+            case 'C':
+                if (!strcmp(optarg, "false")) {
+                    deleteOutput = false;
+                }
+                break;
             default:
                 break;
         }
@@ -62,7 +72,8 @@
                 "unrecognized option: %s\n\n"
                 "usage: %s <gtest options> <test options>\n\n"
                 "test options are:\n\n"
-                "-P, --path: Resource files directory location\n",
+                "-P, --path: Resource files directory location\n"
+                "-C, default:true. Delete output file after test completes\n",
                 argv[optind ?: 1], argv[0]);
         return 2;
     }
diff --git a/media/libstagefright/xmlparser/vts/Android.bp b/media/libstagefright/xmlparser/vts/Android.bp
index 3f93e9e..132ce82 100644
--- a/media/libstagefright/xmlparser/vts/Android.bp
+++ b/media/libstagefright/xmlparser/vts/Android.bp
@@ -30,4 +30,12 @@
         "-Wall",
         "-Werror",
     ],
+    data: [
+        ":media_codecs",
+    ],
+    test_suites: [
+        "general-tests",
+        "vts"
+    ],
+    test_config: "vts_mediaCodecs_validate_test.xml",
 }
diff --git a/media/libstagefright/xmlparser/vts/vts_mediaCodecs_validate_test.xml b/media/libstagefright/xmlparser/vts/vts_mediaCodecs_validate_test.xml
new file mode 100644
index 0000000..fbb7cc6
--- /dev/null
+++ b/media/libstagefright/xmlparser/vts/vts_mediaCodecs_validate_test.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 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.
+-->
+<configuration description="Runs vts_mediaCodecs_validate_test.">
+    <option name="test-suite-tag" value="apct" />
+    <option name="test-suite-tag" value="apct-native" />
+
+    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer">
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
+        <option name="cleanup" value="true" />
+        <option name="push" value="media_codecs.xsd->/data/local/tmp/media_codecs.xsd" />
+        <option name="push" value="vts_mediaCodecs_validate_test->/data/local/tmp/vts_mediaCodecs_validate_test" />
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.GTest" >
+        <option name="native-test-device-path" value="/data/local/tmp" />
+        <option name="module-name" value="vts_mediaCodecs_validate_test" />
+    </test>
+</configuration>
diff --git a/media/mtp/MtpFfsHandle.cpp b/media/mtp/MtpFfsHandle.cpp
index bd6a6c6..c8b4a03 100644
--- a/media/mtp/MtpFfsHandle.cpp
+++ b/media/mtp/MtpFfsHandle.cpp
@@ -114,11 +114,11 @@
 void MtpFfsHandle::advise(int fd) {
     for (unsigned i = 0; i < NUM_IO_BUFS; i++) {
         if (posix_madvise(mIobuf[i].bufs.data(), MAX_FILE_CHUNK_SIZE,
-                POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) < 0)
+                POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) != 0)
             PLOG(ERROR) << "Failed to madvise";
     }
     if (posix_fadvise(fd, 0, 0,
-                POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE | POSIX_FADV_WILLNEED) < 0)
+                POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE | POSIX_FADV_WILLNEED) != 0)
         PLOG(ERROR) << "Failed to fadvise";
 }
 
diff --git a/media/ndk/Android.bp b/media/ndk/Android.bp
index ce91249..4b60475 100644
--- a/media/ndk/Android.bp
+++ b/media/ndk/Android.bp
@@ -35,7 +35,13 @@
 cc_library_headers {
     name: "media_ndk_headers",
     vendor_available: true,
-    export_include_dirs: ["include"]
+    export_include_dirs: ["include"],
+    host_supported: true,
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
 }
 
 cc_library_shared {
diff --git a/media/tests/benchmark/MediaBenchmarkTest/src/androidTest/java/com/android/media/benchmark/tests/EncoderTest.java b/media/tests/benchmark/MediaBenchmarkTest/src/androidTest/java/com/android/media/benchmark/tests/EncoderTest.java
index 48e1422..4202732 100644
--- a/media/tests/benchmark/MediaBenchmarkTest/src/androidTest/java/com/android/media/benchmark/tests/EncoderTest.java
+++ b/media/tests/benchmark/MediaBenchmarkTest/src/androidTest/java/com/android/media/benchmark/tests/EncoderTest.java
@@ -34,6 +34,7 @@
 import com.android.media.benchmark.library.Native;
 import com.android.media.benchmark.library.Stats;
 
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -57,40 +58,87 @@
 public class EncoderTest {
     private static final Context mContext =
             InstrumentationRegistry.getInstrumentation().getTargetContext();
+    private static final String mFileDirPath = mContext.getFilesDir() + "/";
     private static final String mInputFilePath = mContext.getString(R.string.input_file_path);
     private static final String mOutputFilePath = mContext.getString(R.string.output_file_path);
     private static final String mStatsFile =
             mContext.getExternalFilesDir(null) + "/Encoder." + System.currentTimeMillis() + ".csv";
     private static final String TAG = "EncoderTest";
-    private static final long PER_TEST_TIMEOUT_MS = 120000;
     private static final boolean DEBUG = false;
     private static final boolean WRITE_OUTPUT = false;
+    private static final long PER_TEST_TIMEOUT_MS = 120000;
     private static final int ENCODE_DEFAULT_FRAME_RATE = 25;
-    private static final int ENCODE_DEFAULT_BIT_RATE = 8000000 /* 8 Mbps */;
-    private static final int ENCODE_MIN_BIT_RATE = 600000 /* 600 Kbps */;
+    private static final int ENCODE_DEFAULT_VIDEO_BIT_RATE = 8000000 /* 8 Mbps */;
+    private static final int ENCODE_MIN_VIDEO_BIT_RATE = 600000 /* 600 Kbps */;
     private static final int ENCODE_DEFAULT_AUDIO_BIT_RATE = 128000 /* 128 Kbps */;
+    private static int mColorFormat = COLOR_FormatYUV420Flexible;
+    private static File mDecodedFileQcif;
+    private static File mDecodedFileFullHd;
+    private static File mDecodedFileAudio;
     private String mInputFile;
+    private String mMime;
+    private int mBitRate;
+    private int mIFrameInterval;
+    private int mWidth;
+    private int mHeight;
+    private int mProfile;
+    private int mLevel;
+    private int mSampleRate;
+    private int mNumChannel;
+    private static final String DECODE_FULLHD_INPUT = "crowd_1920x1080_25fps_4000kbps_h265.mkv";
+    private static final String DECODE_QCIF_INPUT = "crowd_176x144_25fps_6000kbps_mpeg4.mp4";
+    private static final String DECODE_AUDIO_INPUT = "bbb_48000hz_2ch_100kbps_opus_30sec.webm";
+    private static final String DECODE_FULLHD_UNPACKED = "crowd_1920x1080_25fps_4000kbps_h265.yuv";
+    private static final String DECODE_QCIF_UNPACKED = "crowd_176x144_25fps_6000kbps_mpeg4.yuv";
+    private static final String DECODE_AUDIO_UNPACKED = "bbb_48000hz_2ch_100kbps_opus_30sec.raw";
 
     @Parameterized.Parameters
     public static Collection<Object[]> inputFiles() {
         return Arrays.asList(new Object[][]{
                 // Audio Test
-                {"bbb_44100hz_2ch_128kbps_aac_30sec.mp4"},
-                {"bbb_8000hz_1ch_8kbps_amrnb_30sec.3gp"},
-                {"bbb_16000hz_1ch_9kbps_amrwb_30sec.3gp"},
-                {"bbb_44100hz_2ch_600kbps_flac_30sec.mp4"},
-                {"bbb_48000hz_2ch_100kbps_opus_30sec.webm"},
+                // Parameters: Filename, mimeType, bitrate, width, height, iFrameInterval,
+                // profile, level, sampleRate, channelCount
+                {DECODE_AUDIO_UNPACKED, MediaFormat.MIMETYPE_AUDIO_AAC,
+                        ENCODE_DEFAULT_AUDIO_BIT_RATE, -1, -1, -1, -1, -1, 44100, 2},
+                {DECODE_AUDIO_UNPACKED, MediaFormat.MIMETYPE_AUDIO_AMR_NB,
+                        ENCODE_DEFAULT_AUDIO_BIT_RATE, -1, -1, -1, -1, -1, 8000, 1},
+                {DECODE_AUDIO_UNPACKED, MediaFormat.MIMETYPE_AUDIO_AMR_WB,
+                        ENCODE_DEFAULT_AUDIO_BIT_RATE, -1, -1, -1, -1, -1, 16000, 1},
+                {DECODE_AUDIO_UNPACKED, MediaFormat.MIMETYPE_AUDIO_FLAC,
+                        ENCODE_DEFAULT_AUDIO_BIT_RATE, -1, -1, -1, -1, -1, 44100, 2},
+                {DECODE_AUDIO_UNPACKED, MediaFormat.MIMETYPE_AUDIO_OPUS,
+                        ENCODE_DEFAULT_AUDIO_BIT_RATE, -1, -1, -1, -1, -1, 48000, 2},
+
                 // Video Test
-                {"crowd_1920x1080_25fps_4000kbps_vp8.webm"},
-                {"crowd_1920x1080_25fps_6700kbps_h264.ts"},
-                {"crowd_1920x1080_25fps_4000kbps_h265.mkv"},
-                {"crowd_1920x1080_25fps_4000kbps_vp9.webm"},
-                {"crowd_176x144_25fps_6000kbps_mpeg4.mp4"},
-                {"crowd_176x144_25fps_6000kbps_h263.3gp"}});
+                // Parameters: Filename, mimeType, bitrate, width, height, iFrameInterval,
+                // profile, level, sampleRate, channelCount
+                {DECODE_FULLHD_UNPACKED, MediaFormat.MIMETYPE_VIDEO_VP8,
+                        ENCODE_DEFAULT_VIDEO_BIT_RATE, 1920, 1080, 1, -1, -1, -1, -1},
+                {DECODE_FULLHD_UNPACKED, MediaFormat.MIMETYPE_VIDEO_AVC,
+                        ENCODE_DEFAULT_VIDEO_BIT_RATE, 1920, 1080, 1, -1, -1, -1, -1},
+                {DECODE_FULLHD_UNPACKED, MediaFormat.MIMETYPE_VIDEO_HEVC,
+                        ENCODE_DEFAULT_VIDEO_BIT_RATE, 1920, 1080, 1, -1, -1, -1, -1},
+                {DECODE_FULLHD_UNPACKED, MediaFormat.MIMETYPE_VIDEO_VP9,
+                        ENCODE_DEFAULT_VIDEO_BIT_RATE, 1920, 1080, 1, -1, -1, -1, -1},
+                {DECODE_QCIF_UNPACKED, MediaFormat.MIMETYPE_VIDEO_MPEG4, ENCODE_MIN_VIDEO_BIT_RATE,
+                        176, 144, 1, -1, -1, -1, -1},
+                {DECODE_QCIF_UNPACKED, MediaFormat.MIMETYPE_VIDEO_H263, ENCODE_MIN_VIDEO_BIT_RATE,
+                        176, 144, 1, -1, -1, -1, -1}});
     }
 
-    public EncoderTest(String inputFileName) {
-        this.mInputFile = inputFileName;
+    public EncoderTest(String filename, String mime, int bitrate, int width, int height,
+                       int frameInterval, int profile, int level, int samplerate,
+                       int channelCount) {
+        this.mInputFile = filename;
+        this.mMime = mime;
+        this.mBitRate = bitrate;
+        this.mIFrameInterval = frameInterval;
+        this.mWidth = width;
+        this.mHeight = height;
+        this.mProfile = profile;
+        this.mLevel = level;
+        this.mSampleRate = samplerate;
+        this.mNumChannel = channelCount;
     }
 
     @BeforeClass
@@ -101,33 +149,36 @@
         Log.d(TAG, "Saving Benchmark results in: " + mStatsFile);
     }
 
-    @Test(timeout = PER_TEST_TIMEOUT_MS)
-    public void testEncoder() throws Exception {
-        int status;
-        int frameSize;
-        //Parameters for video
-        int width = 0;
-        int height = 0;
-        int profile = 0;
-        int level = 0;
-        int frameRate = 0;
+    @BeforeClass
+    public static void prepareInput() throws IOException {
 
-        //Parameters for audio
-        int bitRate = 0;
-        int sampleRate = 0;
-        int numChannels = 0;
-        File inputFile = new File(mInputFilePath + mInputFile);
-        assertTrue("Cannot find " + mInputFile + " in directory " + mInputFilePath,
-                inputFile.exists());
+        mDecodedFileFullHd = new File(mFileDirPath + DECODE_FULLHD_UNPACKED);
+        int status = decodeFile(mInputFilePath + DECODE_FULLHD_INPUT, mDecodedFileFullHd);
+        assertEquals("Decoder returned error " + status, 0, status);
+
+        mDecodedFileQcif = new File(mFileDirPath + DECODE_QCIF_UNPACKED);
+        status = decodeFile(mInputFilePath + DECODE_QCIF_INPUT, mDecodedFileQcif);
+        assertEquals("Decoder returned error " + status, 0, status);
+
+        mDecodedFileAudio = new File(mFileDirPath + DECODE_AUDIO_UNPACKED);
+        status = decodeFile(mInputFilePath + DECODE_AUDIO_INPUT, mDecodedFileAudio);
+        assertEquals("Decoder returned error " + status, 0, status);
+    }
+
+    private static int decodeFile(String inputFileName, File outputDecodeFile) throws IOException {
+        int status = -1;
+        File inputFile = new File(inputFileName);
+        assertTrue("Cannot open input file " + inputFileName, inputFile.exists());
         FileInputStream fileInput = new FileInputStream(inputFile);
         FileDescriptor fileDescriptor = fileInput.getFD();
+        FileOutputStream decodeOutputStream = new FileOutputStream(outputDecodeFile);
+
         Extractor extractor = new Extractor();
         int trackCount = extractor.setUpExtractor(fileDescriptor);
-        assertTrue("Extraction failed. No tracks for file: " + mInputFile, (trackCount > 0));
+        assertTrue("Extraction failed. No tracks for the given input file", (trackCount > 0));
         ArrayList<ByteBuffer> inputBuffer = new ArrayList<>();
         ArrayList<MediaCodec.BufferInfo> frameInfo = new ArrayList<>();
         for (int currentTrack = 0; currentTrack < trackCount; currentTrack++) {
-            int colorFormat = COLOR_FormatYUV420Flexible;
             extractor.selectExtractorTrack(currentTrack);
             MediaFormat format = extractor.getFormat(currentTrack);
             // Get samples from extractor
@@ -146,163 +197,135 @@
                             bufInfo.presentationTimeUs + " size = " + bufInfo.size);
                 }
             } while (sampleSize > 0);
-            int tid = android.os.Process.myTid();
-            File decodedFile = new File(mContext.getFilesDir() + "/decoder_" + tid + ".out");
-            FileOutputStream decodeOutputStream = new FileOutputStream(decodedFile);
             Decoder decoder = new Decoder();
             decoder.setupDecoder(decodeOutputStream);
             status = decoder.decode(inputBuffer, frameInfo, false, format, "");
-            assertEquals("Decoder returned error " + status + " for file: " + mInputFile, 0,
-                    status);
             MediaFormat decoderFormat = decoder.getFormat();
+            if (decoderFormat.containsKey(MediaFormat.KEY_COLOR_FORMAT)) {
+                mColorFormat = decoderFormat.getInteger(MediaFormat.KEY_COLOR_FORMAT);
+            }
             decoder.deInitCodec();
             extractor.unselectExtractorTrack(currentTrack);
             inputBuffer.clear();
             frameInfo.clear();
-            if (decodeOutputStream != null) {
-                decodeOutputStream.close();
-            }
-            String mime = format.getString(MediaFormat.KEY_MIME);
-            ArrayList<String> mediaCodecs = CodecUtils.selectCodecs(mime, true);
-            assertTrue("No suitable codecs found for file: " + mInputFile + " track : " +
-                    currentTrack + " mime: " + mime, (mediaCodecs.size() > 0));
-            Boolean[] encodeMode = {true, false};
-            /* Encoding the decoder's output */
-            for (Boolean asyncMode : encodeMode) {
-                for (String codecName : mediaCodecs) {
-                    FileOutputStream encodeOutputStream = null;
-                    if (WRITE_OUTPUT) {
-                        File outEncodeFile = new File(mOutputFilePath + "encoder.out");
-                        if (outEncodeFile.exists()) {
-                            assertTrue(" Unable to delete existing file" + outEncodeFile.toString(),
-                                    outEncodeFile.delete());
-                        }
-                        assertTrue("Unable to create file to write encoder output: " +
-                                outEncodeFile.toString(), outEncodeFile.createNewFile());
-                        encodeOutputStream = new FileOutputStream(outEncodeFile);
-                    }
-                    File rawFile = new File(mContext.getFilesDir() + "/decoder_" + tid + ".out");
-                    assertTrue("Cannot open file to write decoded output", rawFile.exists());
-                    if (DEBUG) {
-                        Log.i(TAG, "Path of decoded input file: " + rawFile.toString());
-                    }
-                    FileInputStream eleStream = new FileInputStream(rawFile);
-                    if (mime.startsWith("video/")) {
-                        width = format.getInteger(MediaFormat.KEY_WIDTH);
-                        height = format.getInteger(MediaFormat.KEY_HEIGHT);
-                        if (format.containsKey(MediaFormat.KEY_FRAME_RATE)) {
-                            frameRate = format.getInteger(MediaFormat.KEY_FRAME_RATE);
-                        } else if (frameRate <= 0) {
-                            frameRate = ENCODE_DEFAULT_FRAME_RATE;
-                        }
-                        if (format.containsKey(MediaFormat.KEY_BIT_RATE)) {
-                            bitRate = format.getInteger(MediaFormat.KEY_BIT_RATE);
-                        } else if (bitRate <= 0) {
-                            if (mime.contains("video/3gpp") || mime.contains("video/mp4v-es")) {
-                                bitRate = ENCODE_MIN_BIT_RATE;
-                            } else {
-                                bitRate = ENCODE_DEFAULT_BIT_RATE;
-                            }
-                        }
-                        if (format.containsKey(MediaFormat.KEY_PROFILE)) {
-                            profile = format.getInteger(MediaFormat.KEY_PROFILE);
-                        }
-                        if (format.containsKey(MediaFormat.KEY_PROFILE)) {
-                            level = format.getInteger(MediaFormat.KEY_LEVEL);
-                        }
-                        if (decoderFormat.containsKey(MediaFormat.KEY_COLOR_FORMAT)) {
-                            colorFormat = decoderFormat.getInteger(MediaFormat.KEY_COLOR_FORMAT);
-                        }
-                    } else {
-                        sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
-                        numChannels = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
-                        if (decoderFormat.containsKey(MediaFormat.KEY_BIT_RATE)) {
-                            bitRate = decoderFormat.getInteger(MediaFormat.KEY_BIT_RATE);
-                        } else {
-                            bitRate = ENCODE_DEFAULT_AUDIO_BIT_RATE;
-                        }
-                    }
-                    /*Setup Encode Format*/
-                    MediaFormat encodeFormat;
-                    if (mime.startsWith("video/")) {
-                        frameSize = width * height * 3 / 2;
-                        encodeFormat = MediaFormat.createVideoFormat(mime, width, height);
-                        encodeFormat.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate);
-                        encodeFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
-                        encodeFormat.setInteger(MediaFormat.KEY_PROFILE, profile);
-                        encodeFormat.setInteger(MediaFormat.KEY_LEVEL, level);
-                        encodeFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
-                        encodeFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, frameSize);
-                        encodeFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, colorFormat);
-                    } else {
-                        encodeFormat = MediaFormat.createAudioFormat(mime, sampleRate, numChannels);
-                        encodeFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
-                        frameSize = 4096;
-                    }
-                    Encoder encoder = new Encoder();
-                    encoder.setupEncoder(encodeOutputStream, eleStream);
-                    status = encoder.encode(codecName, encodeFormat, mime, frameRate, sampleRate,
-                            frameSize, asyncMode);
-                    encoder.deInitEncoder();
-                    assertEquals(
-                            codecName + " encoder returned error " + status + " for " + "file:" +
-                                    " " + mInputFile, 0, status);
-                    encoder.dumpStatistics(mInputFile, codecName, (asyncMode ? "async" : "sync"),
-                            extractor.getClipDuration(), mStatsFile);
-                    Log.i(TAG, "Encoding complete for file: " + mInputFile + " with codec: " +
-                            codecName + " for aSyncMode = " + asyncMode);
-                    encoder.resetEncoder();
-                    eleStream.close();
-                    if (encodeOutputStream != null) {
-                        encodeOutputStream.close();
-                    }
-
-                }
-            }
-            //Cleanup temporary input file
-            if (decodedFile.exists()) {
-                assertTrue(" Unable to delete decoded file" + decodedFile.toString(),
-                        decodedFile.delete());
-                Log.i(TAG, "Successfully deleted decoded file");
-            }
         }
         extractor.deinitExtractor();
         fileInput.close();
+        decodeOutputStream.close();
+        return status;
     }
 
     @Test(timeout = PER_TEST_TIMEOUT_MS)
-    public void testNativeEncoder() throws Exception {
-        File inputFile = new File(mInputFilePath + mInputFile);
-        assertTrue("Cannot find " + mInputFile + " in directory " + mInputFilePath,
-                inputFile.exists());
-        int tid = android.os.Process.myTid();
-        final String mDecodedFile = mContext.getFilesDir() + "/decoder_" + tid + ".out";
-        FileInputStream fileInput = new FileInputStream(inputFile);
-        FileDescriptor fileDescriptor = fileInput.getFD();
-        Extractor extractor = new Extractor();
-        int trackCount = extractor.setUpExtractor(fileDescriptor);
-        assertTrue("Extraction failed. No tracks for file: ", trackCount > 0);
-        for (int currentTrack = 0; currentTrack < trackCount; currentTrack++) {
-            extractor.selectExtractorTrack(currentTrack);
-            MediaFormat format = extractor.getFormat(currentTrack);
-            String mime = format.getString(MediaFormat.KEY_MIME);
-            ArrayList<String> mediaCodecs = CodecUtils.selectCodecs(mime, true);
-            // Encoding the decoder's output
+    public void testEncoder() throws Exception {
+        int status;
+        int frameSize;
+
+        ArrayList<String> mediaCodecs = CodecUtils.selectCodecs(mMime, true);
+        assertTrue("No suitable codecs found for mimetype: " + mMime, (mediaCodecs.size() > 0));
+        Boolean[] encodeMode = {true, false};
+        // Encoding the decoded input file
+        for (Boolean asyncMode : encodeMode) {
             for (String codecName : mediaCodecs) {
-                Native nativeEncoder = new Native();
-                int status = nativeEncoder
-                        .Encode(mInputFilePath, mInputFile, mDecodedFile, mStatsFile, codecName);
+                FileOutputStream encodeOutputStream = null;
+                if (WRITE_OUTPUT) {
+                    File outEncodeFile = new File(mOutputFilePath + "encoder.out");
+                    if (outEncodeFile.exists()) {
+                        assertTrue(" Unable to delete existing file" + outEncodeFile.toString(),
+                                outEncodeFile.delete());
+                    }
+                    assertTrue("Unable to create file to write encoder output: " +
+                            outEncodeFile.toString(), outEncodeFile.createNewFile());
+                    encodeOutputStream = new FileOutputStream(outEncodeFile);
+                }
+                File rawFile = new File(mFileDirPath + mInputFile);
+                assertTrue("Cannot open decoded input file", rawFile.exists());
+                if (DEBUG) {
+                    Log.i(TAG, "Path of decoded input file: " + rawFile.toString());
+                }
+                FileInputStream eleStream = new FileInputStream(rawFile);
+                // Setup Encode Format
+                MediaFormat encodeFormat;
+                if (mMime.startsWith("video/")) {
+                    frameSize = mWidth * mHeight * 3 / 2;
+                    encodeFormat = MediaFormat.createVideoFormat(mMime, mWidth, mHeight);
+                    encodeFormat.setInteger(MediaFormat.KEY_FRAME_RATE, ENCODE_DEFAULT_FRAME_RATE);
+                    encodeFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, mIFrameInterval);
+                    encodeFormat.setInteger(MediaFormat.KEY_BIT_RATE, mBitRate);
+                    encodeFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, mColorFormat);
+                    if (mProfile != -1 && mLevel != -1) {
+                        encodeFormat.setInteger(MediaFormat.KEY_PROFILE, mProfile);
+                        encodeFormat.setInteger(MediaFormat.KEY_LEVEL, mLevel);
+                    }
+                } else {
+                    frameSize = 4096;
+                    encodeFormat = MediaFormat.createAudioFormat(mMime, mSampleRate, mNumChannel);
+                    encodeFormat.setInteger(MediaFormat.KEY_BIT_RATE, mBitRate);
+                }
+                Encoder encoder = new Encoder();
+                encoder.setupEncoder(encodeOutputStream, eleStream);
+                status = encoder.encode(codecName, encodeFormat, mMime, ENCODE_DEFAULT_FRAME_RATE,
+                        mSampleRate, frameSize, asyncMode);
+                encoder.deInitEncoder();
                 assertEquals(
-                        codecName + " encoder returned error " + status + " for " + "file:" + " " +
-                                mInputFile, 0, status);
+                        codecName + " encoder returned error " + status + " for " + "mime:" + " " +
+                                mMime, 0, status);
+                String inputReference;
+                long durationUs;
+                if (mMime.startsWith("video/")) {
+                    inputReference =
+                            mInputFile + "_" + mWidth + "x" + mHeight + "_" + mBitRate + "bps";
+                    durationUs = (((eleStream.getChannel().size() + frameSize - 1) / frameSize) /
+                            ENCODE_DEFAULT_FRAME_RATE) * 1000000;
+                } else {
+                    inputReference = mInputFile + "_" + mSampleRate + "hz_" + mNumChannel + "ch_" +
+                            mBitRate + "bps";
+                    durationUs =
+                            (eleStream.getChannel().size() / (mSampleRate * mNumChannel)) * 1000000;
+                }
+                encoder.dumpStatistics(inputReference, codecName, (asyncMode ? "async" : "sync"),
+                        durationUs, mStatsFile);
+                Log.i(TAG, "Encoding complete for mime: " + mMime + " with codec: " + codecName +
+                        " for aSyncMode = " + asyncMode);
+                encoder.resetEncoder();
+                eleStream.close();
+                if (encodeOutputStream != null) {
+                    encodeOutputStream.close();
+                }
             }
         }
-        File decodedFile = new File(mDecodedFile);
-        // Cleanup temporary input file
-        if (decodedFile.exists()) {
-            assertTrue("Unable to delete - " + mDecodedFile, decodedFile.delete());
-            Log.i(TAG, "Successfully deleted - " + mDecodedFile);
+    }
+
+    @Test(timeout = PER_TEST_TIMEOUT_MS)
+    public void testNativeEncoder() {
+        ArrayList<String> mediaCodecs = CodecUtils.selectCodecs(mMime, true);
+        assertTrue("No suitable codecs found for mimetype: " + mMime, (mediaCodecs.size() > 0));
+        for (String codecName : mediaCodecs) {
+            Native nativeEncoder = new Native();
+            int status = nativeEncoder
+                    .Encode(mFileDirPath, mInputFile, mStatsFile, codecName, mMime, mBitRate,
+                            mColorFormat, mIFrameInterval, mWidth, mHeight, mProfile, mLevel,
+                            mSampleRate, mNumChannel);
+            assertEquals(codecName + " encoder returned error " + status + " for " + "mime:" + " " +
+                    mMime, 0, status);
         }
-        fileInput.close();
+    }
+
+    @AfterClass
+    public static void deleteDecodedFiles() {
+        if (mDecodedFileFullHd.exists()) {
+            assertTrue(" Unable to delete decoded file" + mDecodedFileFullHd.toString(),
+                    mDecodedFileFullHd.delete());
+            Log.i(TAG, "Successfully deleted decoded file" + mDecodedFileFullHd.toString());
+        }
+        if (mDecodedFileQcif.exists()) {
+            assertTrue(" Unable to delete decoded file" + mDecodedFileQcif.toString(),
+                    mDecodedFileQcif.delete());
+            Log.i(TAG, "Successfully deleted decoded file" + mDecodedFileQcif.toString());
+        }
+        if (mDecodedFileAudio.exists()) {
+            assertTrue(" Unable to delete decoded file" + mDecodedFileAudio.toString(),
+                    mDecodedFileAudio.delete());
+            Log.i(TAG, "Successfully deleted decoded file" + mDecodedFileAudio.toString());
+        }
     }
 }
diff --git a/media/tests/benchmark/MediaBenchmarkTest/src/main/cpp/NativeEncoder.cpp b/media/tests/benchmark/MediaBenchmarkTest/src/main/cpp/NativeEncoder.cpp
index 1277c8b..2f658e3 100644
--- a/media/tests/benchmark/MediaBenchmarkTest/src/main/cpp/NativeEncoder.cpp
+++ b/media/tests/benchmark/MediaBenchmarkTest/src/main/cpp/NativeEncoder.cpp
@@ -30,189 +30,81 @@
 #include <stdio.h>
 
 constexpr int32_t ENCODE_DEFAULT_FRAME_RATE = 25;
-constexpr int32_t ENCODE_DEFAULT_AUDIO_BIT_RATE = 128000 /* 128 Kbps */;
-constexpr int32_t ENCODE_DEFAULT_BIT_RATE = 8000000 /* 8 Mbps */;
-constexpr int32_t ENCODE_MIN_BIT_RATE = 600000 /* 600 Kbps */;
 
 extern "C" JNIEXPORT int JNICALL Java_com_android_media_benchmark_library_Native_Encode(
-        JNIEnv *env, jobject thiz, jstring jFilePath, jstring jFileName, jstring jOutFilePath,
-        jstring jStatsFile, jstring jCodecName) {
+        JNIEnv *env, jobject thiz, jstring jFilePath, jstring jFileName, jstring jStatsFile,
+        jstring jCodecName, jstring jMime, jint jBitRate, jint jColorFormat, jint jFrameInterval,
+        jint jWidth, jint jHeight, jint jProfile, jint jLevel, jint jSampleRate,
+        jint jNumChannels) {
+    UNUSED(thiz);
     const char *filePath = env->GetStringUTFChars(jFilePath, nullptr);
     const char *fileName = env->GetStringUTFChars(jFileName, nullptr);
-    string sFilePath = string(filePath) + string(fileName);
-    UNUSED(thiz);
-    FILE *inputFp = fopen(sFilePath.c_str(), "rb");
-    env->ReleaseStringUTFChars(jFileName, fileName);
-    env->ReleaseStringUTFChars(jFilePath, filePath);
-    if (!inputFp) {
-        ALOGE("Unable to open input file for reading");
+    string inputFile = string(filePath) + string(fileName);
+    const char *codecName = env->GetStringUTFChars(jCodecName, nullptr);
+    string sCodecName = string(codecName);
+    const char *mime = env->GetStringUTFChars(jMime, nullptr);
+
+    ifstream eleStream;
+    eleStream.open(inputFile, ifstream::binary | ifstream::ate);
+    if (!eleStream.is_open()) {
+        ALOGE("%s - File failed to open for reading!", fileName);
+        env->ReleaseStringUTFChars(jFileName, fileName);
         return -1;
     }
 
-    Decoder *decoder = new Decoder();
-    Extractor *extractor = decoder->getExtractor();
-    if (!extractor) {
-        ALOGE("Extractor creation failed");
-        return -1;
-    }
+    bool asyncMode[2] = {true, false};
+    for (bool mode : asyncMode) {
+        size_t eleSize = eleStream.tellg();
+        eleStream.seekg(0, ifstream::beg);
 
-    // Read file properties
-    struct stat buf;
-    stat(sFilePath.c_str(), &buf);
-    size_t fileSize = buf.st_size;
-    if (fileSize > kMaxBufferSize) {
-        ALOGE("File size greater than maximum buffer size");
-        return -1;
-    }
-    int32_t fd = fileno(inputFp);
-    int32_t trackCount = extractor->initExtractor(fd, fileSize);
-    if (trackCount <= 0) {
-        ALOGE("initExtractor failed");
-        return -1;
-    }
+        // Set encoder params
+        encParameter encParams;
+        encParams.width = jWidth;
+        encParams.height = jHeight;
+        encParams.bitrate = jBitRate;
+        encParams.iFrameInterval = jFrameInterval;
+        encParams.sampleRate = jSampleRate;
+        encParams.numChannels = jNumChannels;
+        encParams.frameRate = ENCODE_DEFAULT_FRAME_RATE;
+        encParams.colorFormat = jColorFormat;
+        encParams.profile = jProfile;
+        encParams.level = jLevel;
 
-    for (int curTrack = 0; curTrack < trackCount; curTrack++) {
-        int32_t status = extractor->setupTrackFormat(curTrack);
-        if (status != 0) {
-            ALOGE("Track Format invalid");
-            return -1;
-        }
-        uint8_t *inputBuffer = (uint8_t *)malloc(fileSize);
-        if (!inputBuffer) {
-            ALOGE("Insufficient memory");
-            return -1;
-        }
-        vector<AMediaCodecBufferInfo> frameInfo;
-        AMediaCodecBufferInfo info;
-        uint32_t inputBufferOffset = 0;
-
-        // Get frame data
-        while (1) {
-            status = extractor->getFrameSample(info);
-            if (status || !info.size) break;
-            // copy the meta data and buffer to be passed to decoder
-            if (inputBufferOffset + info.size > kMaxBufferSize) {
-                ALOGE("Memory allocated not sufficient");
-                free(inputBuffer);
-                return -1;
-            }
-            memcpy(inputBuffer + inputBufferOffset, extractor->getFrameBuf(), info.size);
-            frameInfo.push_back(info);
-            inputBufferOffset += info.size;
-        }
-        string decName = "";
-        const char *outputFilePath = env->GetStringUTFChars(jOutFilePath, nullptr);
-        FILE *outFp = fopen(outputFilePath, "wb");
-        if (outFp == nullptr) {
-            ALOGE("%s - File failed to open for writing!", outputFilePath);
-            free(inputBuffer);
-            return -1;
-        }
-        decoder->setupDecoder();
-        status = decoder->decode(inputBuffer, frameInfo, decName, false /*asyncMode */, outFp);
+        Encoder *encoder = new Encoder();
+        encoder->setupEncoder();
+        auto status = encoder->encode(sCodecName, eleStream, eleSize, mode, encParams,
+                                      const_cast<char *>(mime));
         if (status != AMEDIA_OK) {
-            ALOGE("Decode returned error");
-            free(inputBuffer);
+            ALOGE("Encoder returned error");
             return -1;
         }
-
-        AMediaFormat *decoderFormat = decoder->getFormat();
-        AMediaFormat *format = extractor->getFormat();
-        if (inputBuffer) {
-            free(inputBuffer);
-            inputBuffer = nullptr;
+        ALOGV("Encoding complete with codec %s for asyncMode = %d", sCodecName.c_str(), mode);
+        encoder->deInitCodec();
+        const char *statsFile = env->GetStringUTFChars(jStatsFile, nullptr);
+        string inputReference;
+        int64_t clipDurationUs;
+        if (!strncmp(mime, "video/", 6)) {
+            inputReference = string(fileName) + "_" + to_string(jWidth) + "x" + to_string(jHeight) +
+                             "_" + to_string(jBitRate) + "bps";
+            int32_t frameSize = jWidth * jHeight * 3 / 2;
+            clipDurationUs =
+                    (((eleSize + frameSize - 1) / frameSize) / ENCODE_DEFAULT_FRAME_RATE) * 1000000;
+        } else {
+            inputReference = string(fileName) + "_" + to_string(jSampleRate) + "hz_" +
+                             to_string(jNumChannels) + "ch_" + to_string(jBitRate) + "bps";
+            clipDurationUs = (eleSize / (jSampleRate * jNumChannels)) * 1000000;
         }
-        const char *mime = nullptr;
-        AMediaFormat_getString(format, AMEDIAFORMAT_KEY_MIME, &mime);
-        if (!mime) {
-            ALOGE("Error in AMediaFormat_getString");
-            return -1;
-        }
-        ifstream eleStream;
-        eleStream.open(outputFilePath, ifstream::binary | ifstream::ate);
-        if (!eleStream.is_open()) {
-            ALOGE("%s - File failed to open for reading!", outputFilePath);
-            env->ReleaseStringUTFChars(jOutFilePath, outputFilePath);
-            return -1;
-        }
-        const char *codecName = env->GetStringUTFChars(jCodecName, NULL);
-        const char *inputReference = env->GetStringUTFChars(jFileName, nullptr);
-        string sCodecName = string(codecName);
-        string sInputReference = string(inputReference);
-
-        bool asyncMode[2] = {true, false};
-        for (int i = 0; i < 2; i++) {
-            size_t eleSize = eleStream.tellg();
-            eleStream.seekg(0, ifstream::beg);
-
-            // Get encoder params
-            encParameter encParams;
-            if (!strncmp(mime, "video/", 6)) {
-                AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_WIDTH, &encParams.width);
-                AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_HEIGHT, &encParams.height);
-                AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_FRAME_RATE, &encParams.frameRate);
-                AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, &encParams.bitrate);
-                if (encParams.bitrate <= 0 || encParams.frameRate <= 0) {
-                    encParams.frameRate = ENCODE_DEFAULT_FRAME_RATE;
-                    if (!strcmp(mime, "video/3gpp") || !strcmp(mime, "video/mp4v-es")) {
-                        encParams.bitrate = ENCODE_MIN_BIT_RATE /* 600 Kbps */;
-                    } else {
-                        encParams.bitrate = ENCODE_DEFAULT_BIT_RATE /* 8 Mbps */;
-                    }
-                }
-                AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_PROFILE, &encParams.profile);
-                AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_LEVEL, &encParams.level);
-                AMediaFormat_getInt32(decoderFormat, AMEDIAFORMAT_KEY_COLOR_FORMAT,
-                                      &encParams.colorFormat);
-            } else {
-                AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, &encParams.sampleRate);
-                AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT,
-                                      &encParams.numChannels);
-                encParams.bitrate = ENCODE_DEFAULT_AUDIO_BIT_RATE;
-            }
-            Encoder *encoder = new Encoder();
-            encoder->setupEncoder();
-            status = encoder->encode(sCodecName, eleStream, eleSize, asyncMode[i], encParams,
-                                     (char *)mime);
-            if (status != AMEDIA_OK) {
-                ALOGE("Encoder returned error");
-                return -1;
-            }
-            ALOGV("Encoding complete with codec %s for asyncMode = %d", sCodecName.c_str(),
-                  asyncMode[i]);
-            encoder->deInitCodec();
-            const char *statsFile = env->GetStringUTFChars(jStatsFile, nullptr);
-            encoder->dumpStatistics(sInputReference, extractor->getClipDuration(), sCodecName,
-                                    (asyncMode[i] ? "async" : "sync"), statsFile);
-            env->ReleaseStringUTFChars(jStatsFile, statsFile);
-            encoder->resetEncoder();
-            delete encoder;
-            encoder = nullptr;
-        }
-        eleStream.close();
-        if (outFp) {
-            fclose(outFp);
-            outFp = nullptr;
-        }
-        env->ReleaseStringUTFChars(jFileName, inputReference);
-        env->ReleaseStringUTFChars(jCodecName, codecName);
-        env->ReleaseStringUTFChars(jOutFilePath, outputFilePath);
-        if (format) {
-            AMediaFormat_delete(format);
-            format = nullptr;
-        }
-        if (decoderFormat) {
-            AMediaFormat_delete(decoderFormat);
-            decoderFormat = nullptr;
-        }
-        decoder->deInitCodec();
-        decoder->resetDecoder();
+        encoder->dumpStatistics(inputReference, clipDurationUs, sCodecName,
+                                (mode ? "async" : "sync"), statsFile);
+        env->ReleaseStringUTFChars(jStatsFile, statsFile);
+        encoder->resetEncoder();
+        delete encoder;
+        encoder = nullptr;
     }
-    if (inputFp) {
-        fclose(inputFp);
-        inputFp = nullptr;
-    }
-    extractor->deInitExtractor();
-    delete decoder;
+    eleStream.close();
+    env->ReleaseStringUTFChars(jFilePath, filePath);
+    env->ReleaseStringUTFChars(jFileName, fileName);
+    env->ReleaseStringUTFChars(jMime, mime);
+    env->ReleaseStringUTFChars(jCodecName, codecName);
     return 0;
 }
diff --git a/media/tests/benchmark/MediaBenchmarkTest/src/main/java/com/android/media/benchmark/library/Native.java b/media/tests/benchmark/MediaBenchmarkTest/src/main/java/com/android/media/benchmark/library/Native.java
index 38b608a..3e3969c 100644
--- a/media/tests/benchmark/MediaBenchmarkTest/src/main/java/com/android/media/benchmark/library/Native.java
+++ b/media/tests/benchmark/MediaBenchmarkTest/src/main/java/com/android/media/benchmark/library/Native.java
@@ -27,6 +27,7 @@
     public native int Decode(String inputFilePath, String inputFileName, String statsFile,
             String codecName, boolean asyncMode);
 
-    public native int Encode(String inputFilePath, String inputFileName, String outputFilePath,
-            String statsFile, String codecName);
+    public native int Encode(String inputFilePath, String inputFileName, String statsFile,
+            String codecName, String mime, int bitRate, int colorFormat, int frameInterval,
+            int width, int height, int profile, int level, int sampleRate, int numChannel);
 }
diff --git a/media/tests/benchmark/src/native/encoder/Encoder.cpp b/media/tests/benchmark/src/native/encoder/Encoder.cpp
index 26fb1b9..15c479d 100644
--- a/media/tests/benchmark/src/native/encoder/Encoder.cpp
+++ b/media/tests/benchmark/src/native/encoder/Encoder.cpp
@@ -203,13 +203,13 @@
         AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_WIDTH, mParams.width);
         AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_HEIGHT, mParams.height);
         AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_FRAME_RATE, mParams.frameRate);
+        AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, mParams.iFrameInterval);
         AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_BIT_RATE, mParams.bitrate);
-        AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, 1);
-        if (mParams.profile && mParams.level) {
+        AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_COLOR_FORMAT, mParams.colorFormat);
+        if (mParams.profile != -1 && mParams.level != -1) {
             AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_PROFILE, mParams.profile);
             AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_LEVEL, mParams.level);
         }
-        AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_COLOR_FORMAT, mParams.colorFormat);
     } else {
         AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_SAMPLE_RATE, mParams.sampleRate);
         AMediaFormat_setInt32(mFormat, AMEDIAFORMAT_KEY_CHANNEL_COUNT, mParams.numChannels);
diff --git a/media/tests/benchmark/src/native/encoder/Encoder.h b/media/tests/benchmark/src/native/encoder/Encoder.h
index 5ad142b..324317c 100644
--- a/media/tests/benchmark/src/native/encoder/Encoder.h
+++ b/media/tests/benchmark/src/native/encoder/Encoder.h
@@ -23,10 +23,11 @@
 #include <queue>
 #include <thread>
 
-#include "media/NdkImage.h"
 #include "BenchmarkCommon.h"
 #include "Stats.h"
 
+// constant not defined in NDK api
+constexpr int32_t COLOR_FormatYUV420Flexible = 0x7F420888;
 
 struct encParameter {
     int32_t bitrate = -1;
@@ -38,9 +39,10 @@
     int32_t width = 0;
     int32_t height = 0;
     int32_t frameRate = -1;
-    int32_t profile = 0;
-    int32_t level = 0;
-    int32_t colorFormat = AIMAGE_FORMAT_YUV_420_888;
+    int32_t iFrameInterval = 0;
+    int32_t profile = -1;
+    int32_t level = -1;
+    int32_t colorFormat = COLOR_FormatYUV420Flexible;
 };
 
 class Encoder : public CallBackHandle {
diff --git a/media/tests/benchmark/tests/EncoderTest.cpp b/media/tests/benchmark/tests/EncoderTest.cpp
index faac847..7e1681d 100644
--- a/media/tests/benchmark/tests/EncoderTest.cpp
+++ b/media/tests/benchmark/tests/EncoderTest.cpp
@@ -23,6 +23,10 @@
 #include "Decoder.h"
 #include "Encoder.h"
 
+constexpr int32_t kEncodeDefaultVideoBitRate = 8000000 /* 8 Mbps */;
+constexpr int32_t kEncodeMinVideoBitRate = 600000 /* 600 Kbps */;
+constexpr int32_t kEncodeDefaultAudioBitRate = 128000 /* 128 Kbps */;
+
 static BenchmarkTestEnvironment *gEnv = nullptr;
 
 class EncoderTest : public ::testing::TestWithParam<tuple<string, string, bool>> {};
@@ -86,6 +90,7 @@
         decoder->setupDecoder();
         status = decoder->decode(inputBuffer, frameInfo, decName, false /*asyncMode */, outFp);
         ASSERT_EQ(status, AMEDIA_OK) << "Decode returned error : " << status;
+        AMediaFormat *decoderFormat = decoder->getFormat();
 
         ifstream eleStream;
         eleStream.open(outputFileName.c_str(), ifstream::binary | ifstream::ate);
@@ -108,11 +113,13 @@
             if (encParams.bitrate <= 0 || encParams.frameRate <= 0) {
                 encParams.frameRate = 25;
                 if (!strcmp(mime, "video/3gpp") || !strcmp(mime, "video/mp4v-es")) {
-                    encParams.bitrate = 600000 /* 600 Kbps */;
+                    encParams.bitrate = kEncodeMinVideoBitRate;
                 } else {
-                    encParams.bitrate = 8000000 /* 8 Mbps */;
+                    encParams.bitrate = kEncodeDefaultVideoBitRate;
                 }
             }
+            AMediaFormat_getInt32(decoderFormat, AMEDIAFORMAT_KEY_COLOR_FORMAT,
+                                  &encParams.colorFormat);
             AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_PROFILE, &encParams.profile);
             AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_LEVEL, &encParams.level);
         } else {
@@ -120,8 +127,7 @@
                                               &encParams.sampleRate));
             ASSERT_TRUE(AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT,
                                               &encParams.numChannels));
-            encParams.bitrate =
-                    encParams.sampleRate * encParams.numChannels * 16 /* bitsPerSample */;
+            encParams.bitrate = kEncodeDefaultAudioBitRate;
         }
 
         encoder->setupEncoder();
@@ -142,6 +148,10 @@
             AMediaFormat_delete(format);
             format = nullptr;
         }
+        if (decoderFormat) {
+            AMediaFormat_delete(decoderFormat);
+            decoderFormat = nullptr;
+        }
         encoder->resetEncoder();
         decoder->deInitCodec();
         free(inputBuffer);