shithub: openh264

Download patch

ref: 9a602cac1b52b22e4e1e2e925247263ad554b0da
parent: 397c7caa5682ff2794be268927b38abb535edbc9
parent: 887b4f47fca8bb13eb7075803fa227fa65383459
author: sijchen <sijchen@cisco.com>
date: Mon Dec 22 05:37:43 EST 2014

Merge pull request #1657 from mstorsjo/clip-bitrate

Make sure the random test bitrate is high enough

--- a/codec/common/inc/wels_common_defs.h
+++ b/codec/common/inc/wels_common_defs.h
@@ -65,6 +65,8 @@
 extern const uint8_t g_kuiCache30ScanIdx[16];
 extern const uint8_t g_kuiCache48CountScan4Idx[24];
 
+extern const uint8_t g_kuiDequantScaling4x4Default[2][16];
+extern const uint8_t g_kuiDequantScaling8x8Default[2][64];
 extern const  ALIGNED_DECLARE (uint16_t, g_kuiDequantCoeff[52][8], 16);
 extern const uint8_t g_kuiChromaQpTable[52];
 
--- a/codec/common/src/common_tables.cpp
+++ b/codec/common/src/common_tables.cpp
@@ -74,6 +74,7 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
+
 // extern at wels_common_defs.h
 const uint8_t g_kuiChromaQpTable[52] = {
   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
@@ -119,7 +120,29 @@
   { NON_VCL,	NON_VCL },	// 30: NAL_UNIT_UNSPEC_30
   { NON_VCL,	NON_VCL }	// 31: NAL_UNIT_UNSPEC_31
 };
-
+//default scaling list matrix value of 4x4
+const uint8_t g_kuiDequantScaling4x4Default[2][16]={
+  {  6, 13, 20, 28, 13, 20, 28, 32, 20, 28, 32, 37, 28, 32, 37, 42 },
+  { 10, 14, 20, 24, 14, 20, 24, 27, 20, 24, 27, 30, 24, 27, 30, 34 }
+};
+//default scaling list matrix value of 8x8
+const uint8_t g_kuiDequantScaling8x8Default[2][64]={
+  {  6, 10, 13, 16, 18, 23, 25, 27, 10, 11, 16, 18, 23, 25, 27, 29,
+      13, 16, 18, 23, 25, 27, 29, 31,
+      16, 18, 23, 25, 27, 29, 31, 33,
+      18, 23, 25, 27, 29, 31, 33, 36,
+      23, 25, 27, 29, 31, 33, 36, 38,
+      25, 27, 29, 31, 33, 36, 38, 40,
+      27, 29, 31, 33, 36, 38, 40, 42 },
+    {  9, 13, 15, 17, 19, 21, 22, 24,
+      13, 13, 17, 19, 21, 22, 24, 25,
+      15, 17, 19, 21, 22, 24, 25, 27,
+      17, 19, 21, 22, 24, 25, 27, 28,
+      19, 21, 22, 24, 25, 27, 28, 30,
+      21, 22, 24, 25, 27, 28, 30, 32,
+      22, 24, 25, 27, 28, 30, 32, 33,
+      24, 25, 27, 28, 30, 32, 33, 35 }
+};
 ALIGNED_DECLARE (const uint16_t, g_kuiDequantCoeff[52][8], 16) = {
   /* 0*/{   10,   13,   10,   13,   13,   16,   13,   16 },	/* 1*/{   11,   14,   11,   14,   14,   18,   14,   18 },
   /* 2*/{   13,   16,   13,   16,   16,   20,   16,   20 },	/* 3*/{   14,   18,   14,   18,   18,   23,   18,   23 },
--- a/codec/decoder/core/inc/au_parser.h
+++ b/codec/decoder/core/inc/au_parser.h
@@ -133,9 +133,24 @@
 
 /*!
  *************************************************************************************
+ * \brief to parse scaling list message payload
+ *
+ * \param  PPS SPS scaling list matrix     message to be parsed output
+ * \param pBsAux    bitstream reader auxiliary
+ *
+ * \return  0 - successed
+ *    1 - failed
+ *
+ * \note  Call it in case scaling matrix present at sps or pps
+ *************************************************************************************
+*/
+int32_t SetScalingListValue (uint8_t *pScalingList,int iScalingListNum,bool* bUseDefaultScalingMatrixFlag,PBitStringAux pBsAux);
+int32_t ParseScalingList(PSps pSps,PBitStringAux pBs,bool bPPS,bool *bScalingListPresentFlag,uint8_t(*iScalingList4x4)[16],uint8_t(*iScalingList8x8)[64]);
+/*!
+ *************************************************************************************
  * \brief	to parse SEI message payload
  *
- * \param 	pSei		sei message to be parsed output
+ * \param	pSei		sei message to be parsed output
  * \param	pBsAux		bitstream reader auxiliary
  *
  * \return	0 - successed
--- a/codec/decoder/core/inc/decode_slice.h
+++ b/codec/decoder/core/inc/decode_slice.h
@@ -59,7 +59,7 @@
 int32_t WelsMbInterSampleConstruction (PWelsDecoderContext pCtx, PDqLayer pCurLayer,
                                        uint8_t* pDstY, uint8_t* pDstU, uint8_t* pDstV, int32_t iStrideL, int32_t iStrideC);
 int32_t WelsMbInterConstruction (PWelsDecoderContext pCtx, PDqLayer pCurLayer);
-void WelsLumaDcDequantIdct (int16_t* pBlock, int32_t iQp);
+void WelsLumaDcDequantIdct (int16_t* pBlock, int32_t iQp,PWelsDecoderContext pCtx);
 int32_t WelsMbInterPrediction (PWelsDecoderContext pCtx, PDqLayer pCurLayer);
 void WelsChromaDcIdct (int16_t* pBlock);
 
--- a/codec/decoder/core/inc/decoder_context.h
+++ b/codec/decoder/core/inc/decoder_context.h
@@ -409,6 +409,15 @@
 int32_t iECMVs[16][2];
 PPicture pECRefPic[16];
 unsigned long long uiTimeStamp;
+// To support scaling list HP
+uint16_t  pDequant_coeff_buffer4x4[6][52][16];
+uint16_t  pDequant_coeff_buffer8x8[6][52][64];
+uint16_t (*pDequant_coeff4x4[6])[16];// 4x4 sclaing list value pointer
+uint16_t (*pDequant_coeff8x8[6])[64];//64 residual coeff ,with 6 kinds of residual type, 52 qp level
+int iDequantCoeffPpsid;//When a new pps actived, reinitialised the scaling list value
+bool bDequantCoeff4x4Init;
+bool bSpsLatePps;
+bool bUseScalingList;
 } SWelsDecoderContext, *PWelsDecoderContext;
 
 static inline void ResetActiveSPSForEachLayer (PWelsDecoderContext pCtx) {
--- a/codec/decoder/core/inc/error_code.h
+++ b/codec/decoder/core/inc/error_code.h
@@ -185,6 +185,8 @@
 ERR_INFO_INVALID_MMCO_LONG_TERM_IDX_EXCEED_MAX,
 //for CABAC
 ERR_CABAC_NO_BS_TO_READ,
+//for scaling list
+ERR_SCALING_LIST_DELTA_SCALE,
 };
 //-----------------------------------------------------------------------------------------------------------
 
--- a/codec/decoder/core/inc/parameter_sets.h
+++ b/codec/decoder/core/inc/parameter_sets.h
@@ -88,7 +88,11 @@
   bool		bQpPrimeYZeroTransfBypassFlag;
   bool		bSeqScalingMatrixPresentFlag;
   bool		bSeqScalingListPresentFlag[12];
-  const SLevelLimits* pSLevelLimits;
+  //Add scaling list supporting
+  uint8_t  iScalingList4x4[6][16];
+  uint8_t  iScalingList8x8[6][64];
+
+const SLevelLimits* pSLevelLimits;
 } SSps, *PSps;
 
 
@@ -163,7 +167,12 @@
   bool		bRedundantPicCntPresentFlag;
   bool		bWeightedPredFlag;
   uint8_t		uiWeightedBipredIdc;
-
+  bool bTransform_8x8_mode_flag;
+  //Add for scalinglist support
+  bool    bPicScalingMatrixPresentFlag;
+  bool    bPicScalingListPresentFlag[12];
+  uint8_t  iScalingList4x4[6][16];
+  uint8_t  iScalingList8x8[6][64];
 } SPps, *PPps;
 
 } // namespace WelsDec
--- a/codec/decoder/core/inc/wels_common_basis.h
+++ b/codec/decoder/core/inc/wels_common_basis.h
@@ -132,6 +132,12 @@
 #define CHROMA_DC_V  7
 #define CHROMA_AC_U  8
 #define CHROMA_AC_V  9
+#define LUMA_DC_AC_INTRA 10
+#define LUMA_DC_AC_INTER 11
+#define CHROMA_DC_U_INTER  12
+#define CHROMA_DC_V_INTER  13
+#define CHROMA_AC_U_INTER  14
+#define CHROMA_AC_V_INTER  15
 
 typedef struct TagReadBitsCache {
     uint32_t uiCache32Bit;
@@ -149,6 +155,59 @@
     7, 11, 14, 15,
 };
 
+
+static inline void GetMbResProperty(int32_t * pMBproperty,int32_t* pResidualProperty,bool bCavlc)
+{
+ switch(*pResidualProperty)
+  {
+  case CHROMA_AC_U:
+	  *pMBproperty = 1;
+	  *pResidualProperty = bCavlc ? CHROMA_AC : CHROMA_AC_U;
+	  break;
+  case CHROMA_AC_V:
+	  *pMBproperty = 2;
+	  *pResidualProperty = bCavlc ? CHROMA_AC : CHROMA_AC_V;
+	  break;
+  case LUMA_DC_AC_INTRA:
+	  *pMBproperty = 0;
+	  *pResidualProperty = LUMA_DC_AC;
+	  break;
+  case CHROMA_DC_U:
+      *pMBproperty = 1;
+	  *pResidualProperty =  bCavlc ? CHROMA_DC : CHROMA_DC_U;
+      break;
+ case CHROMA_DC_V:
+	  *pMBproperty = 2;
+	  *pResidualProperty =  bCavlc ? CHROMA_DC : CHROMA_DC_V;
+	  break;
+  case I16_LUMA_AC:
+	  *pMBproperty = 0;
+	  break;
+  case I16_LUMA_DC:
+	  *pMBproperty = 0;
+	  break;
+  case LUMA_DC_AC_INTER:
+	  *pMBproperty = 3;
+      *pResidualProperty = LUMA_DC_AC;
+	  break;
+  case CHROMA_DC_U_INTER:
+      *pMBproperty = 4;
+	  *pResidualProperty =  bCavlc ? CHROMA_DC : CHROMA_DC_U;
+      break;
+  case CHROMA_DC_V_INTER:
+	  *pMBproperty = 5;
+	  *pResidualProperty =  bCavlc ? CHROMA_DC : CHROMA_DC_V;
+	  break;
+ case CHROMA_AC_U_INTER:
+	  *pMBproperty = 4;
+	  *pResidualProperty =  bCavlc ? CHROMA_AC : CHROMA_AC_U;
+	  break;
+ case CHROMA_AC_V_INTER:
+	  *pMBproperty = 5;
+	  *pResidualProperty =  bCavlc ?CHROMA_AC:CHROMA_AC_V;
+	  break;
+ }
+  }
 
 typedef struct TagI16PredInfo {
     int8_t iPredMode;
--- a/codec/decoder/core/src/au_parser.cpp
+++ b/codec/decoder/core/src/au_parser.cpp
@@ -833,6 +833,8 @@
 #define  PPS_PIC_INIT_QP_QS_MAX 51
 #define  PPS_CHROMA_QP_INDEX_OFFSET_MIN -12
 #define  PPS_CHROMA_QP_INDEX_OFFSET_MAX 12
+#define  SCALING_LIST_DELTA_SCALE_MAX 127
+#define SCALING_LIST_DELTA_SCALE_MIN -128
 
 /*!
  *************************************************************************************
@@ -935,12 +937,16 @@
     WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //seq_scaling_matrix_present_flag
     pSps->bSeqScalingMatrixPresentFlag	= !!uiCode;
 
-    if (pSps->bSeqScalingMatrixPresentFlag) {	// For high profile, it is not used in current application. FIXME
-      WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,
-               "ParseSps(): seq_scaling_matrix_present_flag (%d). Feature not supported.",
-               pSps->bSeqScalingMatrixPresentFlag);
-      return GENERATE_ERROR_NO (ERR_LEVEL_PARAM_SETS, ERR_INFO_UNSUPPORTED_NON_BASELINE);
-    }
+    if (pSps->bSeqScalingMatrixPresentFlag)// For high profile, it is not used in current application. FIXME
+
+      WELS_READ_VERIFY (ParseScalingList (pSps, pBs, 0, pSps->bSeqScalingListPresentFlag, pSps->iScalingList4x4,
+                                          pSps->iScalingList8x8));
+    //if exist, to parse scalinglist matrix value
+
+    //  WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,
+    //         "ParseSps(): seq_scaling_matrix_present_flag (%d). Feature not supported.",
+    //       pSps->bSeqScalingMatrixPresentFlag);
+    //return GENERATE_ERROR_NO (ERR_LEVEL_PARAM_SETS, ERR_INFO_UNSUPPORTED_NON_BASELINE);
   }
   WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //log2_max_frame_num_minus4
   WELS_CHECK_SE_UPPER_ERROR (uiCode, SPS_LOG2_MAX_FRAME_NUM_MINUS4_MAX, "log2_max_frame_num_minus4",
@@ -1307,6 +1313,22 @@
   pPps->bConstainedIntraPredFlag              = !!uiCode;
   WELS_READ_VERIFY (BsGetOneBit (pBsAux, &uiCode)); //redundant_pic_cnt_present_flag
   pPps->bRedundantPicCntPresentFlag           = !!uiCode;
+//going on to parse high profile syntax, need fix me
+  WELS_READ_VERIFY (BsGetOneBit (pBsAux, &uiCode));
+  pPps->bTransform_8x8_mode_flag = !!uiCode;
+  WELS_READ_VERIFY (BsGetOneBit (pBsAux, &uiCode));
+  pPps->bPicScalingMatrixPresentFlag = !!uiCode;
+  if (pPps->bPicScalingMatrixPresentFlag) {
+    if (pCtx->bSpsAvailFlags[pPps->iSpsId])
+      WELS_READ_VERIFY (ParseScalingList (&pCtx->sSpsBuffer[pPps->iSpsId], pBsAux, 1, pPps->bPicScalingListPresentFlag,
+                                          pPps->iScalingList4x4, pPps->iScalingList8x8));
+    else {
+      pCtx->bSpsLatePps = true;
+      WELS_READ_VERIFY (ParseScalingList (NULL, pBsAux, 1, pPps->bPicScalingListPresentFlag, pPps->iScalingList4x4,
+                                          pPps->iScalingList8x8));
+    }
+  }
+
   if (pCtx->pAccessUnitList->uiAvailUnitsNum > 0) {
     PNalUnit pLastNalUnit = pCtx->pAccessUnitList->pNalUnitsList[pCtx->pAccessUnitList->uiAvailUnitsNum - 1];
     PPps pLastPps = pLastNalUnit->sNalData.sVclNal.sSliceHeaderExt.sSliceHeader.pPps;
@@ -1354,6 +1376,113 @@
 
 
   return ERR_NONE;
+}
+/*
+ *************************************************************************************
+ * \brief	to parse scalinglist message payload
+ *
+ * \param	pps sps scaling list matrix		 message to be parsed output
+ * \param	pBsAux		bitstream reader auxiliary
+ *
+ * \return	0 - successed
+ *		1 - failed
+ *
+ * \note	Call it in case scaling list matrix present at sps or pps level
+ *************************************************************************************
+ */
+int32_t SetScalingListValue (uint8_t* pScalingList, int iScalingListNum, bool* bUseDefaultScalingMatrixFlag,
+                             PBitStringAux pBsAux) {	// reserved Sei_Msg type
+  int iLastScale = 8;
+  int iNextScale = 8;
+  int iDeltaScale;
+  int32_t iCode;
+  for (int j = 0; j < iScalingListNum; j++) {
+    if (iNextScale != 0) {
+      WELS_READ_VERIFY (BsGetSe (pBsAux, &iCode));
+      WELS_CHECK_SE_BOTH_ERROR_NOLOG (iCode, SCALING_LIST_DELTA_SCALE_MIN, SCALING_LIST_DELTA_SCALE_MAX, "DeltaScale",
+                                      ERR_SCALING_LIST_DELTA_SCALE);
+      iDeltaScale = iCode;
+      iNextScale = (iLastScale + iDeltaScale + 256) % 256;
+      *bUseDefaultScalingMatrixFlag = (j == 0 && iNextScale == 0);
+      if (*bUseDefaultScalingMatrixFlag)
+        break;
+    }
+    pScalingList[g_kuiZigzagScan[j]] = (iNextScale == 0) ? iLastScale : iNextScale;
+    iLastScale = pScalingList[g_kuiZigzagScan[j]];
+  }
+
+
+  return ERR_NONE;
+}
+
+int32_t ParseScalingList (PSps pSps, PBitStringAux pBs, bool bPPS, bool* pScalingListPresentFlag,
+                          uint8_t (*iScalingList4x4)[16], uint8_t (*iScalingList8x8)[64]) {
+  uint32_t uiScalingListNum;
+  uint32_t uiCode;
+  int32_t iRetTmp;
+  bool bUseDefaultScalingMatrixFlag4x4 = false;
+  bool bUseDefaultScalingMatrixFlag8x8 = false;
+  bool bInit = false;
+  const uint8_t* defaultScaling[4];
+
+  if (pSps != NULL) {
+    uiScalingListNum = (pSps->uiChromaFormatIdc != 3) ? 8 : 12;
+    bInit = bPPS && pSps->bSeqScalingMatrixPresentFlag;
+  } else
+    uiScalingListNum = 12;
+
+//Init default_scaling_list value for sps or pps
+  defaultScaling[0] = bInit ? pSps->iScalingList4x4[0] : g_kuiDequantScaling4x4Default[0];
+  defaultScaling[1] = bInit ? pSps->iScalingList4x4[3] : g_kuiDequantScaling4x4Default[1];
+  defaultScaling[2] = bInit ? pSps->iScalingList8x8[0] : g_kuiDequantScaling8x8Default[0];
+  defaultScaling[3] = bInit ? pSps->iScalingList8x8[1] : g_kuiDequantScaling8x8Default[1];
+
+  for (int i = 0; i < uiScalingListNum; i++) {
+    WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode));
+    pScalingListPresentFlag[i] = !!uiCode;
+    if (!!uiCode) {
+      if (i < 6) {
+        iRetTmp = SetScalingListValue (iScalingList4x4[i], 16, &bUseDefaultScalingMatrixFlag4x4, pBs);
+        if (iRetTmp == ERR_NONE) {
+          if (bUseDefaultScalingMatrixFlag4x4) {
+            bUseDefaultScalingMatrixFlag4x4 = false;
+            memcpy (iScalingList4x4[i], g_kuiDequantScaling4x4Default[i / 3], sizeof (uint8_t) * 16);
+          }
+        } else
+          return iRetTmp;
+
+      } else {
+        SetScalingListValue (iScalingList8x8[i - 6], 64, &bUseDefaultScalingMatrixFlag8x8, pBs);
+        //if(iRetTmp == ERR_NONE)
+        //{
+        if (bUseDefaultScalingMatrixFlag8x8) {
+          bUseDefaultScalingMatrixFlag8x8 = false;
+          memcpy (iScalingList8x8[i - 6], g_kuiDequantScaling8x8Default[ (i - 6) & 1], sizeof (uint8_t) * 64);
+        }
+        // }
+
+        //else
+        // return iRetTmp;
+      }
+
+    } else {
+      if (i < 6) {
+        if ((i != 0) && (i != 3))
+          memcpy (iScalingList4x4[i], iScalingList4x4[i - 1], sizeof (uint8_t) * 16);
+        else
+          memcpy (iScalingList4x4[i], defaultScaling[i / 3], sizeof (uint8_t) * 16);
+
+      } else {
+        if ((i == 6) || (i == 7))
+          memcpy (iScalingList8x8[i - 6], defaultScaling[ (i & 1) + 2], sizeof (uint8_t) * 64);
+        else
+          memcpy (iScalingList8x8[i - 6], iScalingList8x8[ (i - 6) / 3], sizeof (uint8_t) * 64);
+
+      }
+    }
+  }
+  return ERR_NONE;
+
 }
 
 /*!
--- a/codec/decoder/core/src/decode_slice.cpp
+++ b/codec/decoder/core/src/decode_slice.cpp
@@ -197,8 +197,8 @@
   return 0;
 }
 
-void WelsLumaDcDequantIdct (int16_t* pBlock, int32_t iQp) {
-  const int32_t kiQMul = g_kuiDequantCoeff[iQp][0];
+void WelsLumaDcDequantIdct (int16_t* pBlock, int32_t iQp, PWelsDecoderContext pCtx) {
+  const int32_t kiQMul = pCtx->bUseScalingList ? pCtx->pDequant_coeff4x4[0][iQp][0] >> 4 : g_kuiDequantCoeff[iQp][0];
 #define STRIDE 16
   int32_t i;
   int32_t iTemp[16]; //FIXME check if this is a good idea
@@ -246,7 +246,7 @@
   WelsFillRecNeededMbInfo (pCtx, bOutput, pCurLayer);
 
   if (IS_INTRA16x16 (pCurLayer->pMbType[iMbXy])) {
-    WelsLumaDcDequantIdct (pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy]);
+    WelsLumaDcDequantIdct (pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx);
     RecI16x16Mb (iMbXy, pCtx, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer);
 
     return 0;
@@ -596,7 +596,8 @@
           for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
             //Luma (DC and AC decoding together)
             WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, iIdx, iScanIdxEnd - iScanIdxStart + 1,
-                              g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4), pCurLayer->pLumaQp[iMbXy],
+                              g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC_INTRA, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4),
+                              pCurLayer->pLumaQp[iMbXy],
                               pCtx));
             iIdx++;
           }
@@ -610,25 +611,26 @@
       ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
       ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
     }
-
+    int32_t iMbResProperty;
     //chroma
     //step1: DC
     if (1 == uiCbpChroma || 2 == uiCbpChroma) {
       //Cb Cr
-      WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 16 + (0 << 2), 4, g_kuiChromaDcScan,
-                        CHROMA_DC_V, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (0 << 6), pCurLayer->pChromaQp[iMbXy], pCtx));
-      WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 16 + (1 << 2), 4, g_kuiChromaDcScan,
-                        CHROMA_DC_U, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (1 << 6), pCurLayer->pChromaQp[iMbXy], pCtx));
+      for (i = 0; i < 2; i++) {
+        iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
+        WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 16 + (i << 2), 4, g_kuiChromaDcScan,
+                          iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy], pCtx));
+      }
     }
 
     //step2: AC
     if (2 == uiCbpChroma) {
       for (i = 0; i < 2; i++) { //Cb Cr
-        int32_t iResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
+        iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
         int32_t iIdx = 16 + (i << 2);
         for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
           WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, iIdx,
-                            iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), iResProperty,
+                            iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), iMbResProperty,
                             pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4), pCurLayer->pChromaQp[iMbXy], pCtx));
           iIdx++;
         }
@@ -673,7 +675,7 @@
   int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
   int32_t iScanIdxEnd   = pSlice->sSliceHeaderExt.uiScanIdxEnd;
   int32_t iMbXy = pCurLayer->iMbXyIndex;
-
+  int32_t iMbResProperty;
   int32_t i;
   uint32_t uiMbType = 0, uiCbp = 0, uiCbpLuma = 0, uiCbpChroma = 0;
 
@@ -723,6 +725,21 @@
     }
   }
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
   ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
   ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
   ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
@@ -774,6 +791,7 @@
         ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
       }
     } else { //non-MB_TYPE_INTRA16x16
+      iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA : LUMA_DC_AC_INTER;
       for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
         if (uiCbpLuma & (1 << iId8x8)) {
           int32_t iIdx = (iId8x8 << 2);
@@ -780,7 +798,8 @@
           for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
             //Luma (DC and AC decoding together)
             WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, iIdx, iScanIdxEnd - iScanIdxStart + 1,
-                              g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4), pCurLayer->pLumaQp[iMbXy],
+                              g_kuiZigzagScan + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4),
+                              pCurLayer->pLumaQp[iMbXy],
                               pCtx));
             iIdx++;
           }
@@ -799,20 +818,27 @@
     //step1: DC
     if (1 == uiCbpChroma || 2 == uiCbpChroma) {
       for (i = 0; i < 2; i++) {
-        int32_t iResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
+        if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
+          iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
+        else
+          iMbResProperty = i ? CHROMA_DC_V_INTER : CHROMA_DC_U_INTER;
+
         WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, 16 + (i << 2), 4, g_kuiChromaDcScan,
-                          iResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy], pCtx));
+                          iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy], pCtx));
       }
     }
     //step2: AC
     if (2 == uiCbpChroma) {
       for (i = 0; i < 2; i++) {
-        int32_t iResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
+        if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
+          iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
+        else
+          iMbResProperty = i ? CHROMA_AC_V_INTER : CHROMA_AC_U_INTER;
         int32_t index = 16 + (i << 2);
         for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
           WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, index,
                             iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1),
-                            iResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (index << 4), pCurLayer->pChromaQp[iMbXy], pCtx));
+                            iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (index << 4), pCurLayer->pChromaQp[iMbXy], pCtx));
           index++;
         }
       }
@@ -893,7 +919,47 @@
   WELS_READ_VERIFY (WelsDecodeMbCabacPSliceBaseMode0 (pCtx, &uiNeighAvail, uiEosFlag));
   return ERR_NONE;
 }
+// Calculate deqaunt coeff scaling list value
+int32_t  WelsCalcDeqCoeffScalingList (PWelsDecoderContext pCtx) {
+  int32_t iScalingListNum = 0;
+  if (pCtx->pSps->bSeqScalingMatrixPresentFlag || pCtx->pPps->bPicScalingMatrixPresentFlag) {
+    pCtx->bUseScalingList = true;
+    iScalingListNum = (pCtx->pSps->uiChromaFormatIdc != 3) ? 8 : 12;
 
+    if (!pCtx->bDequantCoeff4x4Init || (pCtx->iDequantCoeffPpsid != pCtx->pPps->iPpsId)) {
+      int i, q, x;
+      // Rewrite pps scaling list for scalingList present flag=0
+      if (pCtx->bSpsLatePps) {
+        for (i = 0; i < 6; i++) {
+          if (!pCtx->pSps->bSeqScalingListPresentFlag[i]) {
+            if (i == 0 || i == 3)
+              memcpy (pCtx->pPps->iScalingList4x4[i], pCtx->pSps->iScalingList4x4[i], 16 * sizeof (uint8_t));
+            else
+              memcpy (pCtx->pPps->iScalingList4x4[i], pCtx->pPps->iScalingList4x4[i - 1], 16 * sizeof (uint8_t));
+
+          }
+        }
+        //TO DO, SUPPORT 8x8 SCALINGlist
+
+      }
+      //Init dequant coeff value for different QP
+      for (i = 0; i < 6; i++) {
+        pCtx->pDequant_coeff4x4[i] = pCtx->pDequant_coeff_buffer4x4[i];
+        for (q = 0; q < 51; q++) {
+          for (x = 0; x < 16; x++) {
+            pCtx->pDequant_coeff4x4[i][q][x] = pCtx->pPps->bPicScalingMatrixPresentFlag ? pCtx->pPps->iScalingList4x4[i][x] *
+                                               g_kuiDequantCoeff[q][x & 0x07] : pCtx->pSps->iScalingList4x4[i][x] * g_kuiDequantCoeff[q][x & 0x07];
+          }
+        }
+      }
+
+      pCtx->bDequantCoeff4x4Init = true;
+    }
+  } else
+    pCtx->bUseScalingList = false;
+  return ERR_NONE;
+}
+
 int32_t WelsDecodeSlice (PWelsDecoderContext pCtx, bool bFirstSliceInLayer, PNalUnit pNalCur) {
   PDqLayer pCurLayer = pCtx->pCurDqLayer;
   PFmo pFmo = pCtx->pFmo;
@@ -950,6 +1016,8 @@
     pSlice->iLastDeltaQp = 0;
     WELS_READ_VERIFY (InitCabacDecEngineFromBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux));
   }
+  //try to calculate  the dequant_coeff
+  WelsCalcDeqCoeffScalingList (pCtx);
 
   iNextMbXyIndex = pSliceHeader->iFirstMbInSlice;
   iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;
@@ -1001,6 +1069,7 @@
   PSliceHeader pSliceHeader		     = &pSlice->sSliceHeaderExt.sSliceHeader;
 
   SWelsNeighAvail sNeighAvail;
+  int32_t iMbResProperty;
 
   int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
   int32_t iScanIdxEnd   = pSlice->sSliceHeaderExt.uiScanIdxEnd;
@@ -1038,6 +1107,7 @@
 
     uint8_t* pTmpBsBuf;
 
+
     int32_t i;
     int32_t iCopySizeY  = (sizeof (uint8_t) << 4);
     int32_t iCopySizeUV = (sizeof (uint8_t) << 3);
@@ -1165,7 +1235,7 @@
             //Luma (DC and AC decoding together)
             if (WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex,
                                         iScanIdxEnd - iScanIdxStart + 1, g_kuiZigzagScan + iScanIdxStart,
-                                        LUMA_DC_AC, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4), pCurLayer->pLumaQp[iMbXy], pCtx)) {
+                                        LUMA_DC_AC_INTRA, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4), pCurLayer->pLumaQp[iMbXy], pCtx)) {
               return -1;//abnormal
             }
             iIndex++;
@@ -1185,8 +1255,9 @@
     //step1: DC
     if (1 == uiCbpC || 2 == uiCbpC) {
       for (i = 0; i < 2; i++) { //Cb Cr
+        iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
         if (WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs,
-                                    16 + (i << 2), 4, g_kuiChromaDcScan, CHROMA_DC, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6),
+                                    16 + (i << 2), 4, g_kuiChromaDcScan, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6),
                                     pCurLayer->pChromaQp[iMbXy], pCtx)) {
           return -1;//abnormal
         }
@@ -1196,11 +1267,12 @@
     //step2: AC
     if (2 == uiCbpC) {
       for (i = 0; i < 2; i++) { //Cb Cr
+        iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
         int32_t iIndex = 16 + (i << 2);
         for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
           if (WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex,
                                       iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1),
-                                      CHROMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4), pCurLayer->pChromaQp[iMbXy], pCtx)) {
+                                      iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4), pCurLayer->pChromaQp[iMbXy], pCtx)) {
             return -1;//abnormal
           }
           iIndex++;
@@ -1277,6 +1349,8 @@
   uint32_t uiMbType = 0, uiCbp = 0, uiCbpL = 0, uiCbpC = 0;
   uint32_t uiCode;
   int32_t iCode;
+  int32_t iMbResProperty;
+
   GetNeighborAvailMbType (&sNeighAvail, pCurLayer);
   ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
   pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;//2009.10.23
@@ -1461,12 +1535,13 @@
       }
     } else { //non-MB_TYPE_INTRA16x16
       for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
+        iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA : LUMA_DC_AC_INTER;
         if (uiCbpL & (1 << iId8x8)) {
           int32_t iIndex = (iId8x8 << 2);
           for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
             //Luma (DC and AC decoding together)
             if (WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex,
-                                        iScanIdxEnd - iScanIdxStart + 1, g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC,
+                                        iScanIdxEnd - iScanIdxStart + 1, g_kuiZigzagScan + iScanIdxStart, iMbResProperty,
                                         pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4), pCurLayer->pLumaQp[iMbXy], pCtx)) {
               return -1;//abnormal
             }
@@ -1488,8 +1563,13 @@
     //step1: DC
     if (1 == uiCbpC || 2 == uiCbpC) {
       for (i = 0; i < 2; i++) { //Cb Cr
+        if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
+          iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
+        else
+          iMbResProperty = i ? CHROMA_DC_V_INTER : CHROMA_DC_U_INTER;
+
         if (WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs,
-                                    16 + (i << 2), 4, g_kuiChromaDcScan, CHROMA_DC, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6),
+                                    16 + (i << 2), 4, g_kuiChromaDcScan, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6),
                                     pCurLayer->pChromaQp[iMbXy], pCtx)) {
           return -1;//abnormal
         }
@@ -1499,11 +1579,16 @@
     //step2: AC
     if (2 == uiCbpC) {
       for (i = 0; i < 2; i++) { //Cb Cr
+        if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
+          iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
+        else
+          iMbResProperty = i ? CHROMA_AC_V_INTER : CHROMA_AC_U_INTER;
+
         int32_t iIndex = 16 + (i << 2);
         for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
           if (WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex,
                                       iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1),
-                                      CHROMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4), pCurLayer->pChromaQp[iMbXy], pCtx)) {
+                                      iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4), pCurLayer->pChromaQp[iMbXy], pCtx)) {
             return -1;//abnormal
           }
           iIndex++;
--- a/codec/decoder/core/src/decoder.cpp
+++ b/codec/decoder/core/src/decoder.cpp
@@ -309,6 +309,8 @@
   pCtx->eErrorConMethod = ERROR_CON_SLICE_COPY_CROSS_IDR_FREEZE_RES_CHANGE;
   pCtx->pPreviousDecodedPictureInDpb = NULL;
   pCtx->sDecoderStatistics.iAvgLumaQp = -1;
+  pCtx->bSpsLatePps = false;
+  pCtx->bUseScalingList = false;
 
 }
 
--- a/codec/decoder/core/src/parse_mb_syn_cabac.cpp
+++ b/codec/decoder/core/src/parse_mb_syn_cabac.cpp
@@ -821,11 +821,15 @@
                                  const uint8_t* pScanTable, int32_t iResProperty, short* sTCoeff, /*int mb_mode*/ uint8_t uiQp,
                                  PWelsDecoderContext pCtx) {
   int32_t iCurNzCacheIdx;
-  const uint16_t* pDeQuantMul = g_kuiDequantCoeff[uiQp];
   uint32_t uiTotalCoeffNum = 0;
   uint32_t uiCbpBit;
   int32_t pSignificantMap[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 
+  int32_t iMbResProperty;
+  GetMbResProperty (&iMbResProperty, &iResProperty, false);
+  const uint16_t* pDeQuantMul = (pCtx->bUseScalingList) ? pCtx->pDequant_coeff4x4[iMbResProperty][uiQp] :
+                                g_kuiDequantCoeff[uiQp];
+
   WELS_READ_VERIFY (ParseCbfInfoCabac (pNeighAvail, pNonZeroCountCache, iIndex, iResProperty, pCtx, uiCbpBit));
   if (uiCbpBit) { //has coeff
     WELS_READ_VERIFY (ParseSignificantMapCabac (pSignificantMap, iResProperty, pCtx, uiTotalCoeffNum));
@@ -847,13 +851,15 @@
   } else if (iResProperty == CHROMA_DC_U || iResProperty == CHROMA_DC_V) {
     do {
       if (pSignificantMap[j] != 0)
-        sTCoeff[pScanTable[j]] = pSignificantMap[j] * pDeQuantMul[0];
+        sTCoeff[pScanTable[j]] = pCtx->bUseScalingList ? (pSignificantMap[j] * pDeQuantMul[0]) >> 4 :
+                                 (pSignificantMap[j] * pDeQuantMul[0]);
       ++j;
     } while (j < 16);
   } else { //luma ac, chroma ac
     do {
       if (pSignificantMap[j] != 0)
-        sTCoeff[pScanTable[j]] = pSignificantMap[j] * pDeQuantMul[pScanTable[j] & 0x07];
+        sTCoeff[pScanTable[j]] = pCtx->bUseScalingList ? (pSignificantMap[j] * pDeQuantMul[pScanTable[j]] >> 4) :
+                                 pSignificantMap[j] * pDeQuantMul[pScanTable[j] & 0x07];
       ++j;
     } while (j < 16);
   }
--- a/codec/decoder/core/src/parse_mb_syn_cavlc.cpp
+++ b/codec/decoder/core/src/parse_mb_syn_cavlc.cpp
@@ -236,8 +236,8 @@
   }
 }
 
-void WelsFillCacheInterCabac(PWelsNeighAvail pNeighAvail, uint8_t* pNonZeroCount, int16_t iMvArray[LIST_A][30][MV_A], int16_t iMvdCache[LIST_A][30][MV_A], int8_t iRefIdxArray[LIST_A][30], PDqLayer pCurLayer)
-{
+void WelsFillCacheInterCabac (PWelsNeighAvail pNeighAvail, uint8_t* pNonZeroCount, int16_t iMvArray[LIST_A][30][MV_A],
+                              int16_t iMvdCache[LIST_A][30][MV_A], int8_t iRefIdxArray[LIST_A][30], PDqLayer pCurLayer) {
   int32_t iCurXy      = pCurLayer->iMbXyIndex;
   int32_t iTopXy      = 0;
   int32_t iLeftXy     = 0;
@@ -267,10 +267,10 @@
     ST32 (iMvArray[0][18], LD32 (pCurLayer->pMv[0][iLeftXy][11]));
     ST32 (iMvArray[0][24], LD32 (pCurLayer->pMv[0][iLeftXy][15]));
 
-    ST32(iMvdCache[0][ 6], LD32(pCurLayer->pMvd[0][iLeftXy][ 3]));
-    ST32(iMvdCache[0][12], LD32(pCurLayer->pMvd[0][iLeftXy][ 7]));
-    ST32(iMvdCache[0][18], LD32(pCurLayer->pMvd[0][iLeftXy][11]));
-    ST32(iMvdCache[0][24], LD32(pCurLayer->pMvd[0][iLeftXy][15]));
+    ST32 (iMvdCache[0][ 6], LD32 (pCurLayer->pMvd[0][iLeftXy][ 3]));
+    ST32 (iMvdCache[0][12], LD32 (pCurLayer->pMvd[0][iLeftXy][ 7]));
+    ST32 (iMvdCache[0][18], LD32 (pCurLayer->pMvd[0][iLeftXy][11]));
+    ST32 (iMvdCache[0][24], LD32 (pCurLayer->pMvd[0][iLeftXy][15]));
 
     iRefIdxArray[0][ 6] = pCurLayer->pRefIndex[0][iLeftXy][ 3];
     iRefIdxArray[0][12] = pCurLayer->pRefIndex[0][iLeftXy][ 7];
@@ -282,31 +282,31 @@
     ST32 (iMvArray[0][18], 0);
     ST32 (iMvArray[0][24], 0);
 
-    ST32(iMvdCache[0][ 6], 0);
-    ST32(iMvdCache[0][12], 0);
-    ST32(iMvdCache[0][18], 0);
-    ST32(iMvdCache[0][24], 0);
+    ST32 (iMvdCache[0][ 6], 0);
+    ST32 (iMvdCache[0][12], 0);
+    ST32 (iMvdCache[0][18], 0);
+    ST32 (iMvdCache[0][24], 0);
 
 
     if (0 == pNeighAvail->iLeftAvail) { //not available
       iRefIdxArray[0][ 6] =
-      iRefIdxArray[0][12] =
-      iRefIdxArray[0][18] =
-      iRefIdxArray[0][24] = REF_NOT_AVAIL;
+        iRefIdxArray[0][12] =
+          iRefIdxArray[0][18] =
+            iRefIdxArray[0][24] = REF_NOT_AVAIL;
     } else { //available but is intra mb type
       iRefIdxArray[0][ 6] =
-      iRefIdxArray[0][12] =
-      iRefIdxArray[0][18] =
-      iRefIdxArray[0][24] = REF_NOT_IN_LIST;
+        iRefIdxArray[0][12] =
+          iRefIdxArray[0][18] =
+            iRefIdxArray[0][24] = REF_NOT_IN_LIST;
     }
   }
   if (pNeighAvail->iLeftTopAvail && IS_INTER (pNeighAvail->iLeftTopType)) {
     ST32 (iMvArray[0][0], LD32 (pCurLayer->pMv[0][iLeftTopXy][15]));
-    ST32(iMvdCache[0][0], LD32(pCurLayer->pMvd[0][iLeftTopXy][15]));
+    ST32 (iMvdCache[0][0], LD32 (pCurLayer->pMvd[0][iLeftTopXy][15]));
     iRefIdxArray[0][0] = pCurLayer->pRefIndex[0][iLeftTopXy][15];
   } else {
     ST32 (iMvArray[0][0], 0);
-    ST32(iMvdCache[0][0], 0);
+    ST32 (iMvdCache[0][0], 0);
     if (0 == pNeighAvail->iLeftTopAvail) { //not available
       iRefIdxArray[0][0] = REF_NOT_AVAIL;
     } else { //available but is intra mb type
@@ -317,30 +317,30 @@
   if (pNeighAvail->iTopAvail && IS_INTER (pNeighAvail->iTopType)) {
     ST64 (iMvArray[0][1], LD64 (pCurLayer->pMv[0][iTopXy][12]));
     ST64 (iMvArray[0][3], LD64 (pCurLayer->pMv[0][iTopXy][14]));
-    ST64(iMvdCache[0][1], LD64(pCurLayer->pMvd[0][iTopXy][12]));
-    ST64(iMvdCache[0][3], LD64(pCurLayer->pMvd[0][iTopXy][14]));
+    ST64 (iMvdCache[0][1], LD64 (pCurLayer->pMvd[0][iTopXy][12]));
+    ST64 (iMvdCache[0][3], LD64 (pCurLayer->pMvd[0][iTopXy][14]));
     ST32 (&iRefIdxArray[0][1], LD32 (&pCurLayer->pRefIndex[0][iTopXy][12]));
   } else {
     ST64 (iMvArray[0][1], 0);
     ST64 (iMvArray[0][3], 0);
-    ST64(iMvdCache[0][1], 0);
-    ST64(iMvdCache[0][3], 0);
+    ST64 (iMvdCache[0][1], 0);
+    ST64 (iMvdCache[0][3], 0);
     if (0 == pNeighAvail->iTopAvail) { //not available
       iRefIdxArray[0][1] =
-      iRefIdxArray[0][2] =
-      iRefIdxArray[0][3] =
-      iRefIdxArray[0][4] = REF_NOT_AVAIL;
+        iRefIdxArray[0][2] =
+          iRefIdxArray[0][3] =
+            iRefIdxArray[0][4] = REF_NOT_AVAIL;
     } else { //available but is intra mb type
       iRefIdxArray[0][1] =
-      iRefIdxArray[0][2] =
-      iRefIdxArray[0][3] =
-      iRefIdxArray[0][4] = REF_NOT_IN_LIST;
+        iRefIdxArray[0][2] =
+          iRefIdxArray[0][3] =
+            iRefIdxArray[0][4] = REF_NOT_IN_LIST;
     }
   }
 
   if (pNeighAvail->iRightTopAvail && IS_INTER (pNeighAvail->iRightTopType)) {
     ST32 (iMvArray[0][5], LD32 (pCurLayer->pMv[0][iRightTopXy][12]));
-    ST32(iMvdCache[0][5], LD32(pCurLayer->pMvd[0][iRightTopXy][12]));
+    ST32 (iMvdCache[0][5], LD32 (pCurLayer->pMvd[0][iRightTopXy][12]));
     iRefIdxArray[0][5] = pCurLayer->pRefIndex[0][iRightTopXy][12];
   } else {
     ST32 (iMvArray[0][5], 0);
@@ -357,21 +357,20 @@
   ST32 (iMvArray[0][11], 0);
   ST32 (iMvArray[0][17], 0);
   ST32 (iMvArray[0][23], 0);
-  ST32(iMvdCache[0][ 9], 0);
-  ST32(iMvdCache[0][21], 0);
-  ST32(iMvdCache[0][11], 0);
-  ST32(iMvdCache[0][17], 0);
-  ST32(iMvdCache[0][23], 0);
+  ST32 (iMvdCache[0][ 9], 0);
+  ST32 (iMvdCache[0][21], 0);
+  ST32 (iMvdCache[0][11], 0);
+  ST32 (iMvdCache[0][17], 0);
+  ST32 (iMvdCache[0][23], 0);
   iRefIdxArray[0][ 9] =
-  iRefIdxArray[0][21] =
-  iRefIdxArray[0][11] =
-  iRefIdxArray[0][17] =
-  iRefIdxArray[0][23] = REF_NOT_AVAIL;
+    iRefIdxArray[0][21] =
+      iRefIdxArray[0][11] =
+        iRefIdxArray[0][17] =
+          iRefIdxArray[0][23] = REF_NOT_AVAIL;
 }
 
 void WelsFillCacheInter (PWelsNeighAvail pNeighAvail, uint8_t* pNonZeroCount,
-                         int16_t iMvArray[LIST_A][30][MV_A], int8_t iRefIdxArray[LIST_A][30], PDqLayer pCurLayer)
-{
+                         int16_t iMvArray[LIST_A][30][MV_A], int8_t iRefIdxArray[LIST_A][30], PDqLayer pCurLayer) {
   int32_t iCurXy      = pCurLayer->iMbXyIndex;
   int32_t iTopXy      = 0;
   int32_t iLeftXy     = 0;
@@ -798,7 +797,13 @@
   int32_t iLevel[16], iZerosLeft, iCoeffNum;
   int32_t  iRun[16];
   int32_t iCurNonZeroCacheIdx, i;
-  const uint16_t* kpDequantCoeff = g_kuiDequantCoeff[uiQp];
+
+
+  int32_t iMbResProperty = 0;
+  GetMbResProperty (&iMbResProperty, &iResidualProperty, 1);
+  const uint16_t* kpDequantCoeff = pCtx->bUseScalingList ? pCtx->pDequant_coeff4x4[iMbResProperty][uiQp] :
+                                   g_kuiDequantCoeff[uiQp];
+
   int8_t nA, nB, nC;
   uint8_t uiTotalCoeff, uiTrailingOnes;
   int32_t iUsedBits = 0;
@@ -867,7 +872,7 @@
       int32_t j;
       iCoeffNum += iRun[i] + 1; //FIXME add 1 earlier ?
       j          = kpZigzagTable[ iCoeffNum ];
-      pTCoeff[j] = iLevel[i] * kpDequantCoeff[0];
+      pTCoeff[j] = pCtx->bUseScalingList ? (iLevel[i] * kpDequantCoeff[0]) >> 4 : (iLevel[i] * kpDequantCoeff[0]);
     }
   } else if (iResidualProperty == I16_LUMA_DC) { //DC coefficent, only call in Intra_16x16, base_mode_flag = 0
     for (i = uiTotalCoeff - 1; i >= 0; --i) { //FIXME merge into rundecode?
@@ -881,7 +886,7 @@
       int32_t j;
       iCoeffNum += iRun[i] + 1; //FIXME add 1 earlier ?
       j          = kpZigzagTable[ iCoeffNum ];
-      pTCoeff[j] = iLevel[i] * kpDequantCoeff[j & 0x07];
+      pTCoeff[j] = pCtx->bUseScalingList ? (iLevel[i] * kpDequantCoeff[j]) >> 4 : (iLevel[i] * kpDequantCoeff[j & 0x07]);
     }
   }
 
binary files /dev/null b/res/test_scalinglist_jm.264 differ
--- /dev/null
+++ b/test/decoder/DecUT_ParseSyntax.cpp
@@ -1,0 +1,278 @@
+#include <gtest/gtest.h>
+#include "codec_app_def.h"
+#include "codec_api.h"
+#include "decoder_context.h"
+#include "decoder.h"
+#include "decoder_core.h"
+#include "welsCodecTrace.h"
+#include "../../common/src/welsCodecTrace.cpp"
+
+using namespace WelsDec;
+
+#define BUF_SIZE 100
+
+DECODING_STATE DecodeFrame (const unsigned char* kpSrc,
+                            const int kiSrcLen,
+                            unsigned char** ppDst,
+                            SBufferInfo* pDstInfo,
+                            PWelsDecoderContext pCtx) {
+
+  PWelsDecoderContext m_pDecContext = pCtx;
+  if (CheckBsBuffer (m_pDecContext, kiSrcLen)) {
+    return dsOutOfMemory;
+  }
+  if (kiSrcLen > 0 && kpSrc != NULL) {
+    m_pDecContext->bEndOfStreamFlag = false;
+  } else {
+    //For application MODE, the error detection should be added for safe.
+    //But for CONSOLE MODE, when decoding LAST AU, kiSrcLen==0 && kpSrc==NULL.
+    m_pDecContext->bEndOfStreamFlag = true;
+    m_pDecContext->bInstantDecFlag = true;
+  }
+
+
+  ppDst[0] = ppDst[1] = ppDst[2] = NULL;
+  m_pDecContext->iErrorCode             = dsErrorFree; //initialize at the starting of AU decoding.
+  m_pDecContext->iFeedbackVclNalInAu = FEEDBACK_UNKNOWN_NAL; //initialize
+  unsigned long long uiInBsTimeStamp = pDstInfo->uiInBsTimeStamp;
+  memset (pDstInfo, 0, sizeof (SBufferInfo));
+  pDstInfo->uiInBsTimeStamp = uiInBsTimeStamp;
+
+  m_pDecContext->bReferenceLostAtT0Flag       = false; //initialize for LTR
+  m_pDecContext->bCurAuContainLtrMarkSeFlag = false;
+  m_pDecContext->iFrameNumOfAuMarkedLtr      = 0;
+  m_pDecContext->iFrameNum                       = -1; //initialize
+
+
+  m_pDecContext->iFeedbackTidInAu             = -1; //initialize
+  if (pDstInfo) {
+    pDstInfo->uiOutYuvTimeStamp = 0;
+    m_pDecContext->uiTimeStamp = pDstInfo->uiInBsTimeStamp;
+  } else {
+    m_pDecContext->uiTimeStamp = 0;
+  }
+  WelsDecodeBs (m_pDecContext, kpSrc, kiSrcLen, ppDst,
+                pDstInfo, NULL); //iErrorCode has been modified in this function
+  m_pDecContext->bInstantDecFlag = false; //reset no-delay flag
+
+  return dsErrorFree;
+}
+
+
+int32_t InitDecoder (const bool bParseOnly, PWelsDecoderContext pCtx, SLogContext* pLogCtx) {
+
+
+  if (NULL == pCtx)
+    return cmMallocMemeError;
+
+
+  return WelsInitDecoder (pCtx, bParseOnly, pLogCtx);
+}
+
+long Initialize (const SDecodingParam* pParam, PWelsDecoderContext pCtx, SLogContext* pLogCtx) {
+  int iRet = ERR_NONE;
+  if (pParam == NULL) {
+    return cmInitParaError;
+  }
+
+  // H.264 decoder initialization,including memory allocation,then open it ready to decode
+  iRet = InitDecoder (pParam->bParseOnly, pCtx, pLogCtx);
+  if (iRet)
+    return iRet;
+
+  iRet = DecoderConfigParam (pCtx, pParam);
+  if (iRet)
+    return iRet;
+
+  return cmResultSuccess;
+}
+
+void UninitDecoder (PWelsDecoderContext pCtx) {
+  if (NULL == pCtx)
+    return;
+
+  WelsEndDecoder (pCtx);
+  if (NULL != pCtx) {
+    free (pCtx);
+    pCtx	= NULL;
+  }
+
+}
+class DecoderParseSyntaxTest : public ::testing::Test {
+ public:
+  virtual void SetUp() {
+
+    int rv = WelsCreateDecoder (&m_pDec);
+    ASSERT_EQ (0, rv);
+    ASSERT_TRUE (m_pDec != NULL);
+  }
+
+  virtual void TearDown() {
+    if (m_pDec) {
+      WelsDestroyDecoder (m_pDec);
+    }
+  }
+  //Init members
+  void Init();
+  //Uninit members
+  void Uninit();
+  //Decoder real bitstream
+// void DecoderBs (const char* sFileName);
+  //Parse real bitstream
+  void DecodeBs (const char* sFileName);
+  //Scalinglist
+  void TestScalingList();
+  //Do whole tests here
+  void DecoderParseSyntaxTestAll();
+
+
+ public:
+  ISVCDecoder* m_pDec;
+  SDecodingParam m_sDecParam;
+  SBufferInfo m_sBufferInfo;
+  SParserBsInfo m_sParserBsInfo;
+  uint8_t* m_pData[3];
+  unsigned char m_szBuffer[BUF_SIZE]; //for mocking packet
+  int m_iBufLength; //record the valid data in m_szBuffer
+  PWelsDecoderContext m_pCtx;
+  welsCodecTrace*	m_pWelsTrace;
+
+};
+
+//Init members
+void DecoderParseSyntaxTest::Init() {
+  memset (&m_sBufferInfo, 0, sizeof (SBufferInfo));
+  memset (&m_sDecParam, 0, sizeof (SDecodingParam));
+  memset (&m_sParserBsInfo, 0, sizeof (SParserBsInfo));
+  m_sDecParam.pFileNameRestructed = NULL;
+  m_sDecParam.eOutputColorFormat = videoFormatI420;
+  m_sDecParam.uiCpuLoad = rand() % 100;
+  m_sDecParam.uiTargetDqLayer = rand() % 100;
+  m_sDecParam.eEcActiveIdc = (ERROR_CON_IDC)7;
+  m_sDecParam.sVideoProperty.size = sizeof (SVideoProperty);
+  m_sDecParam.sVideoProperty.eVideoBsType = (VIDEO_BITSTREAM_TYPE) (rand() % 3);
+  m_sDecParam.bParseOnly = false;
+
+  m_pData[0] = m_pData[1] = m_pData[2] = NULL;
+  m_szBuffer[0] = m_szBuffer[1] = m_szBuffer[2] = 0;
+  m_szBuffer[3] = 1;
+  m_iBufLength = 4;
+  //
+  m_pCtx = (PWelsDecoderContext)malloc (sizeof (SWelsDecoderContext));
+
+  m_pWelsTrace	= new welsCodecTrace();
+  if (m_pWelsTrace != NULL) {
+    m_pWelsTrace->SetTraceLevel (WELS_LOG_ERROR);
+  }
+  CM_RETURN eRet = (CM_RETURN)Initialize (&m_sDecParam, m_pCtx, &m_pWelsTrace->m_sLogCtx);
+}
+
+void DecoderParseSyntaxTest::Uninit() {
+  if (m_pCtx) {
+    UninitDecoder (m_pCtx);
+  }
+  if (m_pWelsTrace) {
+    delete m_pWelsTrace;
+    m_pWelsTrace = NULL;
+  }
+  memset (&m_sDecParam, 0, sizeof (SDecodingParam));
+  memset (&m_sBufferInfo, 0, sizeof (SBufferInfo));
+  m_pData[0] = m_pData[1] = m_pData[2] = NULL;
+  m_iBufLength = 0;
+}
+
+void DecoderParseSyntaxTest::DecodeBs (const char* sFileName) {
+
+  uint8_t* pBuf = NULL;
+  int32_t iBufPos = 0;
+  int32_t iFileSize;
+  int32_t i = 0;
+  int32_t iSliceSize;
+  int32_t iSliceIndex = 0;
+  int32_t iEndOfStreamFlag = 0;
+  FILE* pH264File;
+  uint8_t uiStartCode[4] = {0, 0, 0, 1};
+
+#if defined(ANDROID_NDK)
+  std::string filename = std::string ("/sdcard/") + sFileName;
+  ASSERT_TRUE (pH264File = fopen (filename.c_str(), "rb"));
+#else
+  ASSERT_TRUE (pH264File = fopen (sFileName, "rb"));
+#endif
+  fseek (pH264File, 0L, SEEK_END);
+  iFileSize = (int32_t) ftell (pH264File);
+  fseek (pH264File, 0L, SEEK_SET);
+  pBuf = new uint8_t[iFileSize + 4];
+  ASSERT_EQ (fread (pBuf, 1, iFileSize, pH264File), (unsigned int) iFileSize);
+  memcpy (pBuf + iFileSize, &uiStartCode[0], 4); //confirmed_safe_unsafe_usage
+  while (true) {
+    if (iBufPos >= iFileSize) {
+      iEndOfStreamFlag = true;
+      if (iEndOfStreamFlag)
+        m_pDec->SetOption (DECODER_OPTION_END_OF_STREAM, (void*)&iEndOfStreamFlag);
+      break;
+    }
+    for (i = 0; i < iFileSize; i++) {
+      if ((pBuf[iBufPos + i] == 0 && pBuf[iBufPos + i + 1] == 0 && pBuf[iBufPos + i + 2] == 0 && pBuf[iBufPos + i + 3] == 1
+           && i > 0)) {
+        break;
+      }
+    }
+    iSliceSize = i;
+    DecodeFrame (pBuf + iBufPos, iSliceSize, m_pData, &m_sBufferInfo, m_pCtx);
+    iBufPos += iSliceSize;
+    ++ iSliceIndex;
+    if (iSliceIndex == 4)
+      break;
+  }
+
+  fclose (pH264File);
+  if (pBuf) {
+    delete[] pBuf;
+    pBuf = NULL;
+  }
+
+
+}
+void DecoderParseSyntaxTest::TestScalingList() {
+  uint8_t iScalingList[6][16] = {
+    {17, 17, 16, 16, 17, 16, 15, 15, 16, 15, 15, 15, 16, 15, 15, 15 },
+    { 6, 12, 19, 26, 12, 19, 26, 31, 19, 26, 31, 35, 26, 31, 35, 39 },
+    { 6, 12, 19, 26, 12, 19, 26, 31, 19, 26, 31, 35, 26, 31, 35, 40 },
+    {17, 17, 16, 16, 17, 16, 15, 15, 16, 15, 15, 15, 16, 15, 15, 14 },
+    {10, 14, 20, 24, 14, 20, 24, 27, 20, 24, 27, 30, 24, 27, 30, 34 },
+    { 9, 13, 18, 21, 13, 18, 21, 24, 18, 21, 24, 27, 21, 24, 27, 27 }
+  };
+  uint8_t iScalingListPPS[6][16];
+  memset (iScalingListPPS, 0, 6 * 16 * sizeof (uint8_t));
+  //Scalinglist matrix not written into sps or pps
+  Init();
+  DecodeBs ("res/BA_MW_D.264");
+  ASSERT_TRUE (m_pCtx->sSpsBuffer[0].bSeqScalingMatrixPresentFlag == false);
+  EXPECT_EQ (0, memcmp (iScalingListPPS, m_pCtx->sSpsBuffer[0].iScalingList4x4, 6 * 16 * sizeof (uint8_t)));;
+  ASSERT_TRUE (m_pCtx->sPpsBuffer[0].bSeqScalingMatrixPresentFlag == false);
+  EXPECT_EQ (0, memcmp (iScalingListPPS, m_pCtx->sPpsBuffer[0].iScalingList4x4, 6 * 16 * sizeof (uint8_t)));;
+  Uninit();
+  //Scalinglist value just written into sps and pps
+  Init();
+  DecodeBs ("test_scalinglist_jm.264");
+  ASSERT_TRUE (m_pCtx->sSpsBuffer[0].bSeqScalingMatrixPresentFlag);
+  for (int i = 0; i < 6; i++) {
+    EXPECT_EQ (0, memcmp (iScalingList[i], m_pCtx->sSpsBuffer[0].iScalingList4x4[i], 16 * sizeof (uint8_t)));
+  }
+
+  ASSERT_TRUE (m_pCtx->sPpsBuffer[0].bSeqScalingMatrixPresentFlag);
+  EXPECT_EQ (0, memcmp (iScalingList, m_pCtx->sPpsBuffer[0].iScalingList4x4, 6 * 16 * sizeof (uint8_t)));
+  Uninit();
+
+
+}
+
+//TEST here for whole tests
+TEST_F (DecoderParseSyntaxTest, DecoderParseSyntaxTestAll) {
+
+  TestScalingList();
+
+}
+
+
--- a/test/encoder/EncUT_EncoderExt.cpp
+++ b/test/encoder/EncUT_EncoderExt.cpp
@@ -643,6 +643,8 @@
   sEncParamExt.iPicWidth = MB_SIZE + abs ((rand() * 2) % (MAX_WIDTH - MB_SIZE));
   sEncParamExt.iPicHeight = MB_SIZE + abs ((rand() * 2) % (MAX_HEIGHT - MB_SIZE));
   sEncParamExt.iTargetBitrate = rand() + 1; //!=0
+  // Force a bitrate of at least w*h/50, otherwise we will only get skipped frames
+  sEncParamExt.iTargetBitrate = WELS_CLIP3 (sEncParamExt.iTargetBitrate, sEncParamExt.iPicWidth * sEncParamExt.iPicHeight / 50, 100000000);
   int32_t iLevelMaxBitrate = WelsCommon::g_ksLevelLimits[LEVEL_5_0 - 1].uiMaxBR * CpbBrNalFactor;
   if (sEncParamExt.iTargetBitrate > iLevelMaxBitrate)
     sEncParamExt.iTargetBitrate = iLevelMaxBitrate;