Merge "stagefright: use frame height if slice height is 0" into lmp-dev
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
index cdb860c..1b1b1c8 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
@@ -53,6 +53,10 @@
 }
 
 NuPlayer::Decoder::~Decoder() {
+    mDecoderLooper->unregisterHandler(id());
+    mDecoderLooper->stop();
+
+    releaseAndResetMediaBuffers();
 }
 
 static
diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp
index 6c98c52..b56819c 100644
--- a/media/libstagefright/MediaCodec.cpp
+++ b/media/libstagefright/MediaCodec.cpp
@@ -589,7 +589,12 @@
     if (index < buffers->size()) {
         const BufferInfo &info = buffers->itemAt(index);
         if (info.mOwnedByClient) {
-            *buffer = info.mData;
+            // by the time buffers array is initialized, crypto is set
+            if (portIndex == kPortIndexInput && mCrypto != NULL) {
+                *buffer = info.mEncryptedData;
+            } else {
+                *buffer = info.mData;
+            }
             *format = info.mFormat;
         }
     }
diff --git a/media/libstagefright/codecs/m4v_h263/dec/SoftMPEG4.cpp b/media/libstagefright/codecs/m4v_h263/dec/SoftMPEG4.cpp
index 5b2ab84..d98fa80 100644
--- a/media/libstagefright/codecs/m4v_h263/dec/SoftMPEG4.cpp
+++ b/media/libstagefright/codecs/m4v_h263/dec/SoftMPEG4.cpp
@@ -295,19 +295,23 @@
     ALOGV("disp_width = %d, disp_height = %d, buf_width = %d, buf_height = %d",
             disp_width, disp_height, buf_width, buf_height);
 
-    bool cropChanged = false;
-    if (mCropWidth != disp_width || mCropHeight != disp_height) {
-        mCropLeft = 0;
-        mCropTop = 0;
-        mCropWidth = disp_width;
-        mCropHeight = disp_height;
-        cropChanged = true;
+    CropSettingsMode cropSettingsMode = kCropUnSet;
+    if (disp_width != buf_width || disp_height != buf_height) {
+        cropSettingsMode = kCropSet;
+
+        if (mCropWidth != disp_width || mCropHeight != disp_height) {
+            mCropLeft = 0;
+            mCropTop = 0;
+            mCropWidth = disp_width;
+            mCropHeight = disp_height;
+            cropSettingsMode = kCropChanged;
+        }
     }
 
     bool portWillReset = false;
     const bool fakeStride = true;
     SoftVideoDecoderOMXComponent::handlePortSettingsChange(
-            &portWillReset, buf_width, buf_height, cropChanged, fakeStride);
+            &portWillReset, buf_width, buf_height, cropSettingsMode, fakeStride);
     if (portWillReset) {
         if (mMode == MODE_H263) {
             PVCleanUpVideoDecoder(mHandle);
diff --git a/media/libstagefright/codecs/on2/h264dec/SoftAVC.cpp b/media/libstagefright/codecs/on2/h264dec/SoftAVC.cpp
index cf3c3e3..168208f 100644
--- a/media/libstagefright/codecs/on2/h264dec/SoftAVC.cpp
+++ b/media/libstagefright/codecs/on2/h264dec/SoftAVC.cpp
@@ -160,10 +160,11 @@
                     H264SwDecInfo decoderInfo;
                     CHECK(H264SwDecGetInfo(mHandle, &decoderInfo) == H264SWDEC_OK);
 
-                    bool cropChanged = handleCropChange(decoderInfo);
+                    SoftVideoDecoderOMXComponent::CropSettingsMode cropSettingsMode =
+                        handleCropParams(decoderInfo);
                     handlePortSettingsChange(
                             &portWillReset, decoderInfo.picWidth, decoderInfo.picHeight,
-                            cropChanged);
+                            cropSettingsMode);
                 }
             } else {
                 if (portWillReset) {
@@ -209,9 +210,10 @@
     }
 }
 
-bool SoftAVC::handleCropChange(const H264SwDecInfo& decInfo) {
+SoftVideoDecoderOMXComponent::CropSettingsMode SoftAVC::handleCropParams(
+        const H264SwDecInfo& decInfo) {
     if (!decInfo.croppingFlag) {
-        return false;
+        return kCropUnSet;
     }
 
     const CropParams& crop = decInfo.cropParams;
@@ -219,14 +221,14 @@
         mCropTop == crop.cropTopOffset &&
         mCropWidth == crop.cropOutWidth &&
         mCropHeight == crop.cropOutHeight) {
-        return false;
+        return kCropSet;
     }
 
     mCropLeft = crop.cropLeftOffset;
     mCropTop = crop.cropTopOffset;
     mCropWidth = crop.cropOutWidth;
     mCropHeight = crop.cropOutHeight;
-    return true;
+    return kCropChanged;
 }
 
 void SoftAVC::saveFirstOutputBuffer(int32_t picId, uint8_t *data) {
diff --git a/media/libstagefright/codecs/on2/h264dec/SoftAVC.h b/media/libstagefright/codecs/on2/h264dec/SoftAVC.h
index 253a406..069107d 100644
--- a/media/libstagefright/codecs/on2/h264dec/SoftAVC.h
+++ b/media/libstagefright/codecs/on2/h264dec/SoftAVC.h
@@ -73,7 +73,7 @@
     void drainAllOutputBuffers(bool eos);
     void drainOneOutputBuffer(int32_t picId, uint8_t *data);
     void saveFirstOutputBuffer(int32_t pidId, uint8_t *data);
-    bool handleCropChange(const H264SwDecInfo& decInfo);
+    CropSettingsMode handleCropParams(const H264SwDecInfo& decInfo);
 
     DISALLOW_EVIL_CONSTRUCTORS(SoftAVC);
 };
diff --git a/media/libstagefright/httplive/LiveSession.cpp b/media/libstagefright/httplive/LiveSession.cpp
index a289637..7a9a1a3 100644
--- a/media/libstagefright/httplive/LiveSession.cpp
+++ b/media/libstagefright/httplive/LiveSession.cpp
@@ -353,10 +353,6 @@
     sp<AMessage> response;
     status_t err = msg->postAndAwaitResponse(&response);
 
-    uint32_t replyID;
-    CHECK(response == mSeekReply && 0 != mSeekReplyID);
-    mSeekReply.clear();
-    mSeekReplyID = 0;
     return err;
 }
 
@@ -382,12 +378,16 @@
 
         case kWhatSeek:
         {
-            CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
+            uint32_t seekReplyID;
+            CHECK(msg->senderAwaitsResponse(&seekReplyID));
+            mSeekReplyID = seekReplyID;
+            mSeekReply = new AMessage;
 
             status_t err = onSeek(msg);
 
-            mSeekReply = new AMessage;
-            mSeekReply->setInt32("err", err);
+            if (err != OK) {
+                msg->post(50000);
+            }
             break;
         }
 
@@ -422,7 +422,10 @@
 
                             if (mSeekReplyID != 0) {
                                 CHECK(mSeekReply != NULL);
+                                mSeekReply->setInt32("err", OK);
                                 mSeekReply->postReply(mSeekReplyID);
+                                mSeekReplyID = 0;
+                                mSeekReply.clear();
                             }
                         }
                     }
@@ -1094,10 +1097,11 @@
     CHECK(msg->findInt64("timeUs", &timeUs));
 
     if (!mReconfigurationInProgress) {
-        changeConfiguration(timeUs, getBandwidthIndex());
+        changeConfiguration(timeUs, mCurBandwidthIndex);
+        return OK;
+    } else {
+        return -EWOULDBLOCK;
     }
-
-    return OK;
 }
 
 status_t LiveSession::getDuration(int64_t *durationUs) const {
@@ -1254,7 +1258,10 @@
 
         if (mSeekReplyID != 0) {
             CHECK(mSeekReply != NULL);
+            mSeekReply->setInt32("err", OK);
             mSeekReply->postReply(mSeekReplyID);
+            mSeekReplyID = 0;
+            mSeekReply.clear();
         }
     }
 }
diff --git a/media/libstagefright/include/SoftVideoDecoderOMXComponent.h b/media/libstagefright/include/SoftVideoDecoderOMXComponent.h
index 37b1fe1..9e97ebd 100644
--- a/media/libstagefright/include/SoftVideoDecoderOMXComponent.h
+++ b/media/libstagefright/include/SoftVideoDecoderOMXComponent.h
@@ -68,9 +68,14 @@
     uint32_t outputBufferWidth();
     uint32_t outputBufferHeight();
 
+    enum CropSettingsMode {
+        kCropUnSet = 0,
+        kCropSet,
+        kCropChanged,
+    };
     void handlePortSettingsChange(
             bool *portWillReset, uint32_t width, uint32_t height,
-            bool cropChanged = false, bool fakeStride = false);
+            CropSettingsMode cropSettingsMode = kCropUnSet, bool fakeStride = false);
 
     void copyYV12FrameToOutputBuffer(
             uint8_t *dst, const uint8_t *srcY, const uint8_t *srcU, const uint8_t *srcV,
diff --git a/media/libstagefright/omx/SoftVideoDecoderOMXComponent.cpp b/media/libstagefright/omx/SoftVideoDecoderOMXComponent.cpp
index 5853469..3d20a79 100644
--- a/media/libstagefright/omx/SoftVideoDecoderOMXComponent.cpp
+++ b/media/libstagefright/omx/SoftVideoDecoderOMXComponent.cpp
@@ -160,15 +160,17 @@
 }
 
 void SoftVideoDecoderOMXComponent::handlePortSettingsChange(
-        bool *portWillReset, uint32_t width, uint32_t height, bool cropChanged, bool fakeStride) {
+        bool *portWillReset, uint32_t width, uint32_t height,
+        CropSettingsMode cropSettingsMode, bool fakeStride) {
     *portWillReset = false;
     bool sizeChanged = (width != mWidth || height != mHeight);
+    bool updateCrop = (cropSettingsMode == kCropUnSet);
+    bool cropChanged = (cropSettingsMode == kCropChanged);
 
     if (sizeChanged || cropChanged) {
         mWidth = width;
         mHeight = height;
 
-        bool updateCrop = !cropChanged;
         if ((sizeChanged && !mIsAdaptive)
             || width > mAdaptiveMaxWidth
             || height > mAdaptiveMaxHeight) {
@@ -343,6 +345,40 @@
             return OMX_ErrorNone;
         }
 
+        case OMX_IndexParamPortDefinition:
+        {
+            OMX_PARAM_PORTDEFINITIONTYPE *newParams =
+                (OMX_PARAM_PORTDEFINITIONTYPE *)params;
+            OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &newParams->format.video;
+            OMX_PARAM_PORTDEFINITIONTYPE *def = &editPortInfo(newParams->nPortIndex)->mDef;
+
+            uint32_t oldWidth = def->format.video.nFrameWidth;
+            uint32_t oldHeight = def->format.video.nFrameHeight;
+            uint32_t newWidth = video_def->nFrameWidth;
+            uint32_t newHeight = video_def->nFrameHeight;
+            if (newWidth != oldWidth || newHeight != oldHeight) {
+                bool outputPort = (newParams->nPortIndex == kOutputPortIndex);
+                def->format.video.nFrameWidth =
+                    (mIsAdaptive && outputPort) ? mAdaptiveMaxWidth : newWidth;
+                def->format.video.nFrameHeight =
+                    (mIsAdaptive && outputPort) ? mAdaptiveMaxHeight : newHeight;
+                def->format.video.nStride = def->format.video.nFrameWidth;
+                def->format.video.nSliceHeight = def->format.video.nFrameHeight;
+                def->nBufferSize =
+                    def->format.video.nFrameWidth * def->format.video.nFrameHeight * 3 / 2;
+                if (outputPort) {
+                    mWidth = newWidth;
+                    mHeight = newHeight;
+                    mCropLeft = 0;
+                    mCropTop = 0;
+                    mCropWidth = newWidth;
+                    mCropHeight = newHeight;
+                }
+                newParams->nBufferSize = def->nBufferSize;
+            }
+            return SimpleSoftOMXComponent::internalSetParameter(index, params);
+        }
+
         default:
             return SimpleSoftOMXComponent::internalSetParameter(index, params);
     }