/*/////////////////////////////////////////////////////////////////////////////
  C++ Version - MdpPathJpgEncodeScale
  /////////////////////////////////////////////////////////////////////////////*/
int MdpPathJpgEncodeScale::_StartPre( void* p_parameter )
{
#if defined( MDP_FLAG_1_SUPPORT_JPEGCODEC )

    if( b_jpg_path_disen_ == 0 )
    {
    //1.Lock Jpeg Encorder    
    if(JPEG_ENC_STATUS_OK != jpegEncLockEncoder(&jpg_encID_))
    {
        MDP_ERROR("Lock Jpg Encoder Fail");
        return -1;
    }

    //2.Config Jpg Enc HW
    if(JPEG_ENC_STATUS_OK != jpegEncConfigEncoder(jpg_encID_, jpg_inJpgEncParam_))
    {
        MDP_ERROR("Config Jpg Encoder Fail");
        return false;
    }

    //3.Trigger Jpeg Encoder before MDP
    if(JPEG_ENC_STATUS_OK != jpegEncStart(jpg_encID_))
    {
        MDP_ERROR("Trigger Jpg Encoder Fail");
        return false;
    }
    }

    return 0;
#else

    return -1;

#endif
}
JPEG_STATUS_ENUM jpeg_wrapper_start(JPEG_ENCODER_HANDLE jpegWrapperHandle, JPEG_WRAPPER *wrapperPtr)
{
   JPEG_STATUS_ENUM status = JPEG_STATUS_OK;
   kal_uint32 intMemSizeBest, extMemSizeBest;
   kal_uint32 intMemSizeMinimum, extMemSizeMinimum, extMemSizeOffset;
   kal_uint32 dstBufferSize, dstBufferSizeOffset;
   kal_uint8 *extMemoryAddr, *extMemoryTmpAddr;
   kal_uint8 *dstBufferAddr, *dstBufferTmpAddr;


   /// src mode
   status = jpegEncSetSrcMode(jpegWrapperHandle, wrapperPtr->srcMode);
   if (JPEG_STATUS_OK != status)
      return status;

   if (JPEG_ENCODE_SOURCE_YUV_BUFFER == (wrapperPtr->srcMode))
   {
      status = jpegEncSetSrcYUVFormat(jpegWrapperHandle, wrapperPtr->srcSamplingFormat);
      if (JPEG_STATUS_OK != status)
         return status;

      status = jpegEncSetYUVBuffer(jpegWrapperHandle,
                                   wrapperPtr->srcYUVBuffer[0], wrapperPtr->srcYUVBuffer[1], wrapperPtr->srcYUVBuffer[2],
                                   wrapperPtr->srcBufferSize[0], wrapperPtr->srcBufferSize[1], wrapperPtr->srcBufferSize[2]);
      if (JPEG_STATUS_OK != status)
         return status;
   }
   else if (JPEG_ENCODE_SOURCE_RGB_BUFFER == (wrapperPtr->srcMode))
   {
      status = jpegEncSetSrcRGBFormat(jpegWrapperHandle, wrapperPtr->srcRGBFormat);
      if (JPEG_STATUS_OK != status)
         return status;

      status = jpegEncSetRGBBuffer(jpegWrapperHandle, wrapperPtr->srcRGBBuffer, wrapperPtr->srcBufferSize[0]);
      if (JPEG_STATUS_OK != status)
         return status;
   }

   // maybe there is no source window
   if ((0 != wrapperPtr->srcWindow.w * wrapperPtr->srcWindow.h)
      && ((wrapperPtr->srcWidth != wrapperPtr->srcWindow.w) || (wrapperPtr->srcHeight != wrapperPtr->srcWindow.h))
      )
   {
      status = jpegEncSetSrcWindow(jpegWrapperHandle, &(wrapperPtr->srcWindow));
      if (JPEG_STATUS_OK != status)
         return status;
   }

   status = jpegEncSetSrcDimension(jpegWrapperHandle, wrapperPtr->srcWidth, wrapperPtr->srcHeight);
   if (JPEG_STATUS_OK != status)
      return status;

   status = jpegEncSetDstDimension(jpegWrapperHandle, wrapperPtr->dstWidth, wrapperPtr->dstHeight);
   if (JPEG_STATUS_OK != status)
      return status;

#if (defined(__DYNAMIC_SWITCH_CACHEABILITY__) && defined(__MTK_TARGET__))
   dstBufferAddr = (kal_uint8*)(((kal_uint32)wrapperPtr->dstBufferAddr + 31) & (~31));
   dstBufferSize = (kal_uint32)dstBufferAddr - (kal_uint32)wrapperPtr->dstBufferAddr;
   dstBufferSize = (wrapperPtr->dstBufferSize - dstBufferSize) & (~31);
#else
   dstBufferAddr = (kal_uint8*)wrapperPtr->dstBufferAddr;
   dstBufferSize = wrapperPtr->dstBufferSize;
#endif

   status = jpegEncSetDstBuffer(jpegWrapperHandle, dstBufferAddr, dstBufferSize);
   if (JPEG_STATUS_OK != status)
      return status;

   status = jpegEncSetDirectFileOutputHandle(jpegWrapperHandle, wrapperPtr->pDstFileHandle);
   if (JPEG_STATUS_OK != status)
      return status;

   status = jpegEncSetDstFileFormat(jpegWrapperHandle, wrapperPtr->dstFileFormat);
      if (JPEG_STATUS_OK != status)
         return status;

   status = jpegEncSetDstQuality(jpegWrapperHandle, wrapperPtr->dstQuality);
   if (JPEG_STATUS_OK != status)
      return status;

   status = jpegEncSetDstSampleFormat(jpegWrapperHandle, wrapperPtr->dstSamplingFormat);
   if (JPEG_STATUS_OK != status)
      return status;

   status = jpegEncSetDstThumbnail(jpegWrapperHandle, wrapperPtr->enableThumbnail, wrapperPtr->thumbnailWidth, wrapperPtr->thumbnailHeight);
   if (JPEG_STATUS_OK != status)
      return status;
   
   status = jpegEncSetRotation(jpegWrapperHandle, wrapperPtr->rotation);
   if (JPEG_STATUS_OK != status)
      return status;

   /// Camera will always give enough working buffer
   /// jpeg_status = jpegEncQueryWorkingMemory(jpegWrapperHandle, &intMemSize, &extMemSize);
   status = jpegEncQueryWorkingMemory(jpegWrapperHandle, &intMemSizeBest, &intMemSizeMinimum, &extMemSizeBest, &extMemSizeMinimum);
   if (JPEG_STATUS_OK != status)
      return status;

   extMemoryAddr = (kal_uint8*)wrapperPtr->extMemoryAddr;
   extMemSizeMinimum = wrapperPtr->extMemorySize;
#if (defined(__DYNAMIC_SWITCH_CACHEABILITY__) && defined(__MTK_TARGET__))
   if (extMemSizeMinimum <= wrapperPtr->extMemorySize)
   {
      extMemoryTmpAddr = (kal_uint8*)(((kal_uint32)wrapperPtr->extMemoryAddr + 31) & (~31));
      extMemSizeOffset = (kal_uint32)extMemoryTmpAddr - (kal_uint32)wrapperPtr->extMemoryAddr;
   
      extMemSizeOffset = (wrapperPtr->extMemorySize - extMemSizeOffset) & (~31);
      if (extMemSizeMinimum <= extMemSizeOffset)
      {
         extMemoryAddr = extMemoryTmpAddr;
         extMemSizeMinimum = extMemSizeOffset;
      }
   }
   else if (extMemSizeMinimum > wrapperPtr->extMemorySize)
   {
      ASSERT(0);
   }
#endif

   status = jpegEncSetWorkingMemory(jpegWrapperHandle,
                                    wrapperPtr->intMemoryAddr, wrapperPtr->intMemorySize,
                                    (kal_uint32*)extMemoryAddr, extMemSizeMinimum);
   if (JPEG_STATUS_OK != status)
      return status;

   status = jpegEncSetCallbackFunction(jpegWrapperHandle, wrapperPtr->callback_function);
   if (JPEG_STATUS_OK != status)
      return status;

   status = jpegEncStart(jpegWrapperHandle);
   return status;
}