Merge "Merge dup code at thread entry and param change"
diff --git a/api/current.txt b/api/current.txt
index f4bfced..f7d6bce 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -22105,8 +22105,11 @@
   public class Gravity {
     ctor public Gravity();
     method public static void apply(int, int, int, android.graphics.Rect, android.graphics.Rect);
+    method public static void apply(int, int, int, android.graphics.Rect, android.graphics.Rect, int);
     method public static void apply(int, int, int, android.graphics.Rect, int, int, android.graphics.Rect);
+    method public static void apply(int, int, int, android.graphics.Rect, int, int, android.graphics.Rect, int);
     method public static void applyDisplay(int, android.graphics.Rect, android.graphics.Rect);
+    method public static void applyDisplay(int, android.graphics.Rect, android.graphics.Rect, int);
     method public static int getAbsoluteGravity(int, int);
     method public static boolean isHorizontal(int);
     method public static boolean isVertical(int);
diff --git a/core/java/android/view/Gravity.java b/core/java/android/view/Gravity.java
index 63f5ec1..f031fe7 100644
--- a/core/java/android/view/Gravity.java
+++ b/core/java/android/view/Gravity.java
@@ -153,7 +153,8 @@
      *                container.
      * @param layoutDirection The layout direction.
      *
-     * @hide
+     * @see {@link View#LAYOUT_DIRECTION_LTR}
+     * @see {@link View#LAYOUT_DIRECTION_RTL}
      */
     public static void apply(int gravity, int w, int h, Rect container,
             Rect outRect, int layoutDirection) {
@@ -268,6 +269,37 @@
     }
 
     /**
+     * Apply a gravity constant to an object.
+     *
+     * @param gravity The desired placement of the object, as defined by the
+     *                constants in this class.
+     * @param w The horizontal size of the object.
+     * @param h The vertical size of the object.
+     * @param container The frame of the containing space, in which the object
+     *                  will be placed.  Should be large enough to contain the
+     *                  width and height of the object.
+     * @param xAdj Offset to apply to the X axis.  If gravity is LEFT this
+     *             pushes it to the right; if gravity is RIGHT it pushes it to
+     *             the left; if gravity is CENTER_HORIZONTAL it pushes it to the
+     *             right or left; otherwise it is ignored.
+     * @param yAdj Offset to apply to the Y axis.  If gravity is TOP this pushes
+     *             it down; if gravity is BOTTOM it pushes it up; if gravity is
+     *             CENTER_VERTICAL it pushes it down or up; otherwise it is
+     *             ignored.
+     * @param outRect Receives the computed frame of the object in its
+     *                container.
+     * @param layoutDirection The layout direction.
+     *
+     * @see {@link View#LAYOUT_DIRECTION_LTR}
+     * @see {@link View#LAYOUT_DIRECTION_RTL}
+     */
+    public static void apply(int gravity, int w, int h, Rect container,
+                             int xAdj, int yAdj, Rect outRect, int layoutDirection) {
+        int absGravity = getAbsoluteGravity(gravity, layoutDirection);
+        apply(absGravity, w, h, container, xAdj, yAdj, outRect);
+    }
+
+    /**
      * Apply additional gravity behavior based on the overall "display" that an
      * object exists in.  This can be used after
      * {@link #apply(int, int, int, Rect, int, int, Rect)} to place the object
@@ -320,7 +352,32 @@
             }
         }
     }
-    
+
+    /**
+     * Apply additional gravity behavior based on the overall "display" that an
+     * object exists in.  This can be used after
+     * {@link #apply(int, int, int, Rect, int, int, Rect)} to place the object
+     * within a visible display.  By default this moves or clips the object
+     * to be visible in the display; the gravity flags
+     * {@link #DISPLAY_CLIP_HORIZONTAL} and {@link #DISPLAY_CLIP_VERTICAL}
+     * can be used to change this behavior.
+     *
+     * @param gravity Gravity constants to modify the placement within the
+     * display.
+     * @param display The rectangle of the display in which the object is
+     * being placed.
+     * @param inoutObj Supplies the current object position; returns with it
+     * modified if needed to fit in the display.
+     * @param layoutDirection The layout direction.
+     *
+     * @see {@link View#LAYOUT_DIRECTION_LTR}
+     * @see {@link View#LAYOUT_DIRECTION_RTL}
+     */
+    public static void applyDisplay(int gravity, Rect display, Rect inoutObj, int layoutDirection) {
+        int absGravity = getAbsoluteGravity(gravity, layoutDirection);
+        applyDisplay(absGravity, display, inoutObj);
+    }
+
     /**
      * <p>Indicate whether the supplied gravity has a vertical pull.</p>
      *
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 7a1923b..e7bc74c 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -9819,8 +9819,8 @@
      *
      * @param layoutDirection the direction of the layout
      *
-     * {@link #LAYOUT_DIRECTION_LTR}
-     * {@link #LAYOUT_DIRECTION_RTL}
+     * @see {@link #LAYOUT_DIRECTION_LTR}
+     * @see {@link #LAYOUT_DIRECTION_RTL}
      */
     public void onPaddingChanged(int layoutDirection) {
     }
diff --git a/core/tests/coretests/src/android/view/GravityTest.java b/core/tests/coretests/src/android/view/GravityTest.java
deleted file mode 100644
index d8ef650..0000000
--- a/core/tests/coretests/src/android/view/GravityTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright (C) 2011 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.
- */
-
-package android.view;
-
-import android.test.AndroidTestCase;
-import android.test.suitebuilder.annotation.SmallTest;
-
-public class GravityTest extends AndroidTestCase {
-
-    @SmallTest
-    public void testGetAbsoluteGravity() throws Exception {
-        assertOneGravity(Gravity.LEFT, Gravity.LEFT, false);
-        assertOneGravity(Gravity.LEFT, Gravity.LEFT, true);
-
-        assertOneGravity(Gravity.RIGHT, Gravity.RIGHT, false);
-        assertOneGravity(Gravity.RIGHT, Gravity.RIGHT, true);
-
-        assertOneGravity(Gravity.TOP, Gravity.TOP, false);
-        assertOneGravity(Gravity.TOP, Gravity.TOP, true);
-
-        assertOneGravity(Gravity.BOTTOM, Gravity.BOTTOM, false);
-        assertOneGravity(Gravity.BOTTOM, Gravity.BOTTOM, true);
-
-        assertOneGravity(Gravity.CENTER_VERTICAL, Gravity.CENTER_VERTICAL, false);
-        assertOneGravity(Gravity.CENTER_VERTICAL, Gravity.CENTER_VERTICAL, true);
-
-        assertOneGravity(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_HORIZONTAL, false);
-        assertOneGravity(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_HORIZONTAL, true);
-
-        assertOneGravity(Gravity.CENTER, Gravity.CENTER, false);
-        assertOneGravity(Gravity.CENTER, Gravity.CENTER, true);
-
-        assertOneGravity(Gravity.FILL_VERTICAL, Gravity.FILL_VERTICAL, false);
-        assertOneGravity(Gravity.FILL_VERTICAL, Gravity.FILL_VERTICAL, true);
-
-        assertOneGravity(Gravity.FILL_HORIZONTAL, Gravity.FILL_HORIZONTAL, false);
-        assertOneGravity(Gravity.FILL_HORIZONTAL, Gravity.FILL_HORIZONTAL, true);
-
-        assertOneGravity(Gravity.FILL, Gravity.FILL, false);
-        assertOneGravity(Gravity.FILL, Gravity.FILL, true);
-
-        assertOneGravity(Gravity.CLIP_HORIZONTAL, Gravity.CLIP_HORIZONTAL, false);
-        assertOneGravity(Gravity.CLIP_HORIZONTAL, Gravity.CLIP_HORIZONTAL, true);
-
-        assertOneGravity(Gravity.CLIP_VERTICAL, Gravity.CLIP_VERTICAL, false);
-        assertOneGravity(Gravity.CLIP_VERTICAL, Gravity.CLIP_VERTICAL, true);
-
-        assertOneGravity(Gravity.LEFT, Gravity.START, false);
-        assertOneGravity(Gravity.RIGHT, Gravity.START, true);
-
-        assertOneGravity(Gravity.RIGHT, Gravity.END, false);
-        assertOneGravity(Gravity.LEFT, Gravity.END, true);
-    }
-
-    private void assertOneGravity(int expected, int initial, boolean isRtl) {
-        final int layoutDirection = isRtl ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR;
-
-        assertEquals(expected, Gravity.getAbsoluteGravity(initial, layoutDirection));
-    }
-}
diff --git a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h b/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h
index 34804cf..c0e408e 100644
--- a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h
+++ b/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h
@@ -499,6 +499,14 @@
 
 private:
 
+    static const String8 Description;
+    static const String8 FileSuffixes[];
+    static const String8 MimeTypes[];
+    static bool IsFileSuffixSupported(const String8& suffix);
+    static bool IsMimeTypeSupported(const String8& mime);
+    static void AddSupportedMimeTypes(DrmSupportInfo *info);
+    static void AddSupportedFileSuffixes(DrmSupportInfo *info);
+
 /**
  * Session Class for Forward Lock Conversion. An object of this class is created
  * for every conversion.
diff --git a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngineConst.h b/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngineConst.h
deleted file mode 100644
index da95d60..0000000
--- a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngineConst.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (C) 2010 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 __FWDLOCKENGINECONST_H__
-#define __FWDLOCKENGINECONST_H__
-
-namespace android {
-
-/**
- * Constants for forward Lock Engine used for exposing engine's capabilities.
- */
-#define FWDLOCK_EXTENSION_FL           ("FL")
-#define FWDLOCK_DOTEXTENSION_FL        (".fl")
-#define FWDLOCK_MIMETYPE_FL            ("application/x-android-drm-fl")
-
-#define FWDLOCK_EXTENSION_DM           ("DM")
-#define FWDLOCK_DOTEXTENSION_DM        (".dm")
-#define FWDLOCK_MIMETYPE_DM            ("application/vnd.oma.drm.message")
-
-#define FWDLOCK_DESCRIPTION            ("OMA V1 Forward Lock")
-
-};
-
-#endif /* __FWDLOCKENGINECONST_H__ */
diff --git a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/src/FwdLockEngine.cpp b/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/src/FwdLockEngine.cpp
index 0273a4b..5ee41e6 100644
--- a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/src/FwdLockEngine.cpp
+++ b/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/src/FwdLockEngine.cpp
@@ -35,7 +35,6 @@
 #include "FwdLockConv.h"
 #include "FwdLockFile.h"
 #include "FwdLockGlue.h"
-#include "FwdLockEngineConst.h"
 #include "MimeTypeUtil.h"
 
 #undef LOG_TAG
@@ -160,6 +159,48 @@
     return DRM_NO_ERROR;
 }
 
+const String8 FwdLockEngine::FileSuffixes[] = {
+    String8(".fl"),
+    String8(".dm"),
+};
+
+const String8 FwdLockEngine::MimeTypes[] = {
+    String8("application/x-android-drm-fl"),
+    String8("application/vnd.oma.drm.message"),
+};
+
+const String8 FwdLockEngine::Description("OMA V1 Forward Lock");
+
+void FwdLockEngine::AddSupportedMimeTypes(DrmSupportInfo *info) {
+    for (size_t i = 0, n = sizeof(MimeTypes)/sizeof(MimeTypes[0]); i < n; ++i) {
+        info->addMimeType(MimeTypes[i]);
+    }
+}
+
+void FwdLockEngine::AddSupportedFileSuffixes(DrmSupportInfo *info) {
+    for (size_t i = 0, n = sizeof(FileSuffixes)/sizeof(FileSuffixes[0]); i < n; ++i) {
+        info->addFileSuffix(FileSuffixes[i]);
+    }
+}
+
+bool FwdLockEngine::IsMimeTypeSupported(const String8& mime) {
+    for (size_t i = 0, n = sizeof(MimeTypes)/sizeof(MimeTypes[0]); i < n; ++i) {
+        if (mime == MimeTypes[i]) {
+            return true;
+        }
+    }
+    return false;
+}
+
+bool FwdLockEngine::IsFileSuffixSupported(const String8& suffix) {
+    for (size_t i = 0, n = sizeof(FileSuffixes)/sizeof(FileSuffixes[0]); i < n; ++i) {
+        if (suffix == FileSuffixes[i]) {
+            return true;
+        }
+    }
+    return false;
+}
+
 DrmSupportInfo* FwdLockEngine::onGetSupportInfo(int uniqueId) {
     DrmSupportInfo* pSupportInfo = new DrmSupportInfo();
 
@@ -167,12 +208,9 @@
 
     // fill all Forward Lock mimetypes and extensions
     if (NULL != pSupportInfo) {
-        pSupportInfo->addMimeType(String8(FWDLOCK_MIMETYPE_FL));
-        pSupportInfo->addFileSuffix(String8(FWDLOCK_DOTEXTENSION_FL));
-        pSupportInfo->addMimeType(String8(FWDLOCK_MIMETYPE_DM));
-        pSupportInfo->addFileSuffix(String8(FWDLOCK_DOTEXTENSION_DM));
-
-        pSupportInfo->setDescription(String8(FWDLOCK_DESCRIPTION));
+        AddSupportedMimeTypes(pSupportInfo);
+        AddSupportedFileSuffixes(pSupportInfo);
+        pSupportInfo->setDescription(Description);
     }
 
     return pSupportInfo;
@@ -182,14 +220,8 @@
     bool result = false;
 
     String8 extString = path.getPathExtension();
-
     extString.toLower();
-
-    if ((extString == String8(FWDLOCK_DOTEXTENSION_FL)) ||
-        (extString == String8(FWDLOCK_DOTEXTENSION_DM))) {
-        result = true;
-    }
-    return result;
+    return IsFileSuffixSupported(extString);
 }
 
 DrmInfoStatus* FwdLockEngine::onProcessDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
@@ -308,8 +340,7 @@
     *    (regardless of the relation between them to make it compatible with other DRM Engines)
     */
     if (((0 == path.length()) || onCanHandle(uniqueId, path)) &&
-        ((0 == mimeType.length()) || ((mimeStr == String8(FWDLOCK_MIMETYPE_FL)) ||
-        (mimeStr == String8(FWDLOCK_MIMETYPE_DM)))) && (mimeType != path) ) {
+        ((0 == mimeType.length()) || IsMimeTypeSupported(mimeType)) && (mimeType != path) ) {
             return DrmObjectType::CONTENT;
     }
 
diff --git a/libs/rs/Android.mk b/libs/rs/Android.mk
index 45ed453..f92741c 100644
--- a/libs/rs/Android.mk
+++ b/libs/rs/Android.mk
@@ -1,6 +1,41 @@
 
 LOCAL_PATH:=$(call my-dir)
 
+include $(CLEAR_VARS)
+LOCAL_MODULE := libRSDriver
+
+LOCAL_SRC_FILES:= \
+	driver/rsdAllocation.cpp \
+	driver/rsdBcc.cpp \
+	driver/rsdCore.cpp \
+	driver/rsdFrameBuffer.cpp \
+	driver/rsdFrameBufferObj.cpp \
+	driver/rsdGL.cpp \
+	driver/rsdMesh.cpp \
+	driver/rsdMeshObj.cpp \
+	driver/rsdPath.cpp \
+	driver/rsdProgram.cpp \
+	driver/rsdProgramRaster.cpp \
+	driver/rsdProgramStore.cpp \
+	driver/rsdRuntimeMath.cpp \
+	driver/rsdRuntimeStubs.cpp \
+	driver/rsdSampler.cpp \
+	driver/rsdShader.cpp \
+	driver/rsdShaderCache.cpp \
+	driver/rsdVertexArray.cpp
+
+LOCAL_SHARED_LIBRARIES += libz libcutils libutils libEGL libGLESv1_CM libGLESv2
+LOCAL_SHARED_LIBRARIES += libbcc libbcinfo libgui
+
+LOCAL_C_INCLUDES += external/zlib dalvik
+LOCAL_C_INCLUDES += frameworks/compile/libbcc/include
+
+LOCAL_CFLAGS += -Werror -Wall -Wno-unused-parameter -Wno-unused-variable
+
+LOCAL_LDLIBS := -lpthread -ldl
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_STATIC_LIBRARY)
 
 # Build rsg-generator ====================
 include $(CLEAR_VARS)
@@ -23,19 +58,6 @@
 # TODO: This should go into build/core/config.mk
 RSG_GENERATOR:=$(LOCAL_BUILT_MODULE)
 
-# include $(CLEAR_VARS)
-# input_data_file := $(LOCAL_PATH)/rslib.bc
-# slangdata_output_var_name := rs_runtime_lib_bc
-# LOCAL_MODULE := librslib_rt
-
-# LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-
-# LOCAL_MODULE_TAGS := optional
-# include frameworks/compile/slang/SlangData.mk
-# include $(BUILD_STATIC_LIBRARY)
-
-# Build render script lib ====================
-
 include $(CLEAR_VARS)
 LOCAL_MODULE := libRS
 
@@ -110,24 +132,6 @@
 	rsStream.cpp \
 	rsThreadIO.cpp \
 	rsType.cpp \
-	driver/rsdAllocation.cpp \
-	driver/rsdBcc.cpp \
-	driver/rsdCore.cpp \
-	driver/rsdFrameBuffer.cpp \
-	driver/rsdFrameBufferObj.cpp \
-	driver/rsdGL.cpp \
-	driver/rsdMesh.cpp \
-	driver/rsdMeshObj.cpp \
-	driver/rsdPath.cpp \
-	driver/rsdProgram.cpp \
-	driver/rsdProgramRaster.cpp \
-	driver/rsdProgramStore.cpp \
-	driver/rsdRuntimeMath.cpp \
-	driver/rsdRuntimeStubs.cpp \
-	driver/rsdSampler.cpp \
-	driver/rsdShader.cpp \
-	driver/rsdShaderCache.cpp \
-	driver/rsdVertexArray.cpp \
 	RenderScript.cpp \
 	BaseObj.cpp \
 	Element.cpp \
@@ -136,9 +140,10 @@
 	Script.cpp \
 	ScriptC.cpp
 
-LOCAL_SHARED_LIBRARIES += libz libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libbcc libbcinfo libgui
+LOCAL_SHARED_LIBRARIES += libz libcutils libutils libEGL libGLESv1_CM libGLESv2 libbcc
+LOCAL_SHARED_LIBRARIES += libui libbcinfo libgui
 
-LOCAL_STATIC_LIBRARIES := libdex libft2
+LOCAL_STATIC_LIBRARIES := libdex libft2 libRSDriver
 
 LOCAL_C_INCLUDES += external/freetype/include external/zlib dalvik
 LOCAL_C_INCLUDES += frameworks/compile/libbcc/include
diff --git a/media/libstagefright/WAVExtractor.cpp b/media/libstagefright/WAVExtractor.cpp
index 501f4806..c35a77a 100644
--- a/media/libstagefright/WAVExtractor.cpp
+++ b/media/libstagefright/WAVExtractor.cpp
@@ -28,15 +28,22 @@
 #include <media/stagefright/MediaSource.h>
 #include <media/stagefright/MetaData.h>
 #include <utils/String8.h>
+#include <cutils/bitops.h>
+
+#define CHANNEL_MASK_USE_CHANNEL_ORDER 0
 
 namespace android {
 
 enum {
-    WAVE_FORMAT_PCM = 1,
-    WAVE_FORMAT_ALAW = 6,
-    WAVE_FORMAT_MULAW = 7,
+    WAVE_FORMAT_PCM        = 0x0001,
+    WAVE_FORMAT_ALAW       = 0x0006,
+    WAVE_FORMAT_MULAW      = 0x0007,
+    WAVE_FORMAT_EXTENSIBLE = 0xFFFE
 };
 
+static const char* WAVEEXT_SUBFORMAT = "\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71";
+
+
 static uint32_t U32_LE_AT(const uint8_t *ptr) {
     return ptr[3] << 24 | ptr[2] << 16 | ptr[1] << 8 | ptr[0];
 }
@@ -84,7 +91,8 @@
 
 WAVExtractor::WAVExtractor(const sp<DataSource> &source)
     : mDataSource(source),
-      mValidFormat(false) {
+      mValidFormat(false),
+      mChannelMask(CHANNEL_MASK_USE_CHANNEL_ORDER) {
     mInitCheck = init();
 }
 
@@ -161,21 +169,37 @@
                 return NO_INIT;
             }
 
-            uint8_t formatSpec[16];
-            if (mDataSource->readAt(offset, formatSpec, 16) < 16) {
+            uint8_t formatSpec[40];
+            if (mDataSource->readAt(offset, formatSpec, 2) < 2) {
                 return NO_INIT;
             }
 
             mWaveFormat = U16_LE_AT(formatSpec);
             if (mWaveFormat != WAVE_FORMAT_PCM
                     && mWaveFormat != WAVE_FORMAT_ALAW
-                    && mWaveFormat != WAVE_FORMAT_MULAW) {
+                    && mWaveFormat != WAVE_FORMAT_MULAW
+                    && mWaveFormat != WAVE_FORMAT_EXTENSIBLE) {
                 return ERROR_UNSUPPORTED;
             }
 
+            uint8_t fmtSize = 16;
+            if (mWaveFormat == WAVE_FORMAT_EXTENSIBLE) {
+                fmtSize = 40;
+            }
+            if (mDataSource->readAt(offset, formatSpec, fmtSize) < fmtSize) {
+                return NO_INIT;
+            }
+
             mNumChannels = U16_LE_AT(&formatSpec[2]);
-            if (mNumChannels != 1 && mNumChannels != 2) {
-                return ERROR_UNSUPPORTED;
+            if (mWaveFormat != WAVE_FORMAT_EXTENSIBLE) {
+                if (mNumChannels != 1 && mNumChannels != 2) {
+                    ALOGW("More than 2 channels (%d) in non-WAVE_EXT, unknown channel mask",
+                            mNumChannels);
+                }
+            } else {
+                if (mNumChannels < 1 && mNumChannels > 8) {
+                    return ERROR_UNSUPPORTED;
+                }
             }
 
             mSampleRate = U32_LE_AT(&formatSpec[4]);
@@ -186,7 +210,8 @@
 
             mBitsPerSample = U16_LE_AT(&formatSpec[14]);
 
-            if (mWaveFormat == WAVE_FORMAT_PCM) {
+            if (mWaveFormat == WAVE_FORMAT_PCM
+                    || mWaveFormat == WAVE_FORMAT_EXTENSIBLE) {
                 if (mBitsPerSample != 8 && mBitsPerSample != 16
                     && mBitsPerSample != 24) {
                     return ERROR_UNSUPPORTED;
@@ -199,6 +224,42 @@
                 }
             }
 
+            if (mWaveFormat == WAVE_FORMAT_EXTENSIBLE) {
+                uint16_t validBitsPerSample = U16_LE_AT(&formatSpec[18]);
+                if (validBitsPerSample != mBitsPerSample) {
+                    ALOGE("validBits(%d) != bitsPerSample(%d) are not supported",
+                            validBitsPerSample, mBitsPerSample);
+                    return ERROR_UNSUPPORTED;
+                }
+
+                mChannelMask = U32_LE_AT(&formatSpec[20]);
+                ALOGV("numChannels=%d channelMask=0x%x", mNumChannels, mChannelMask);
+                if ((mChannelMask >> 18) != 0) {
+                    ALOGE("invalid channel mask 0x%x", mChannelMask);
+                    return ERROR_MALFORMED;
+                }
+
+                if ((mChannelMask != CHANNEL_MASK_USE_CHANNEL_ORDER)
+                        && (popcount(mChannelMask) != mNumChannels)) {
+                    ALOGE("invalid number of channels (%d) in channel mask (0x%x)",
+                            popcount(mChannelMask), mChannelMask);
+                    return ERROR_MALFORMED;
+                }
+
+                // In a WAVE_EXT header, the first two bytes of the GUID stored at byte 24 contain
+                // the sample format, using the same definitions as a regular WAV header
+                mWaveFormat = U16_LE_AT(&formatSpec[24]);
+                if (mWaveFormat != WAVE_FORMAT_PCM
+                        && mWaveFormat != WAVE_FORMAT_ALAW
+                        && mWaveFormat != WAVE_FORMAT_MULAW) {
+                    return ERROR_UNSUPPORTED;
+                }
+                if (memcmp(&formatSpec[26], WAVEEXT_SUBFORMAT, 14)) {
+                    ALOGE("unsupported GUID");
+                    return ERROR_UNSUPPORTED;
+                }
+            }
+
             mValidFormat = true;
         } else if (!memcmp(chunkHeader, "data", 4)) {
             if (mValidFormat) {
@@ -224,6 +285,7 @@
                 }
 
                 mTrackMeta->setInt32(kKeyChannelCount, mNumChannels);
+                mTrackMeta->setInt32(kKeyChannelMask, mChannelMask);
                 mTrackMeta->setInt32(kKeySampleRate, mSampleRate);
 
                 size_t bytesPerSample = mBitsPerSample >> 3;
diff --git a/media/libstagefright/include/WAVExtractor.h b/media/libstagefright/include/WAVExtractor.h
index ce1f33a..c567ccd 100644
--- a/media/libstagefright/include/WAVExtractor.h
+++ b/media/libstagefright/include/WAVExtractor.h
@@ -47,6 +47,7 @@
     bool mValidFormat;
     uint16_t mWaveFormat;
     uint16_t mNumChannels;
+    uint32_t mChannelMask;
     uint32_t mSampleRate;
     uint16_t mBitsPerSample;
     off64_t mDataOffset;