SoftMPEG4: Check the buffer size before writing the reference frame.

Also prevent overflow in SoftMPEG4 and division by zero in SoftMPEG4Encoder.

Bug: 30033990
Change-Id: I7701f5fc54c2670587d122330e5dc851f64ed3c2
(cherry picked from commit 695123195034402ca76169b195069c28c30342d3)

bool SoftMPEG4::handlePortSettingsChange() {
 uint32_t disp_width, disp_height;
 PVGetVideoDimensions(mHandle, (int32 *)&disp_width, (int32 *)&disp_height);

 uint32_t buf_width, buf_height;
 PVGetBufferDimensions(mHandle, (int32 *)&buf_width, (int32 *)&buf_height);

    CHECK_LE(disp_width, buf_width);
    CHECK_LE(disp_height, buf_height);

    ALOGV("disp_width = %d, disp_height = %d, buf_width = %d, buf_height = %d",
            disp_width, disp_height, buf_width, buf_height);

 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, cropSettingsMode, fakeStride);
 if (portWillReset) {
 if (mMode == MODE_H263) {
 PVCleanUpVideoDecoder(mHandle);

 uint8_t *vol_data[1];
 int32_t vol_size = 0;

            vol_data[0] = NULL;
 if (!PVInitVideoDecoder(
                    mHandle, vol_data, &vol_size, 1, outputBufferWidth(), outputBufferHeight(),
                    H263_MODE)) {
                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
                mSignalledError = true;
 return true;
 }
 }

        mFramesConfigured = false;
 }

 return portWillReset;
}

bool SoftMPEG4::handlePortSettingsChange() {
 uint32_t disp_width, disp_height;
 PVGetVideoDimensions(mHandle, (int32 *)&disp_width, (int32 *)&disp_height);

 uint32_t buf_width, buf_height;
 PVGetBufferDimensions(mHandle, (int32 *)&buf_width, (int32 *)&buf_height);

    CHECK_LE(disp_width, buf_width);
    CHECK_LE(disp_height, buf_height);

    ALOGV("disp_width = %d, disp_height = %d, buf_width = %d, buf_height = %d",
            disp_width, disp_height, buf_width, buf_height);

 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, cropSettingsMode, fakeStride);
 if (portWillReset) {
 if (mMode == MODE_H263) {
 PVCleanUpVideoDecoder(mHandle);

 uint8_t *vol_data[1];
 int32_t vol_size = 0;

            vol_data[0] = NULL;
 if (!PVInitVideoDecoder(
                    mHandle, vol_data, &vol_size, 1, outputBufferWidth(), outputBufferHeight(),
                    H263_MODE)) {
                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
                mSignalledError = true;
 return true;
 }
 }

        mFramesConfigured = false;
 }

 return portWillReset;
}
