PASDK-218:CB updates for THD 192 kHz
[processor-sdk/performance-audio-sr.git] / pasdk / test_arm / framework / aspDecOpCircBuf_slave.c
index e32daef6ca7731a0ab612b534a423a4c6b5504de..79c645bf5bf92f409e311f979c3ef236091ad078 100644 (file)
@@ -441,7 +441,9 @@ Int cbWriteAf(
     PAF_ChannelMask_HD streamMask;
     Int8 i;
     Int16 j;
-    PAF_AudioData *pPcmBuf;UInt8 *pMetaBuf; int nextWrtIdx;PAF_AudioFrame *pAfCbNextAf; 
+    PAF_AudioData *pPcmBuf; UInt8 *pMetaBuf; int nextWrtIdx;PAF_AudioFrame *pAfCbNextAf; 
+    PAF_AudioFrame *pAfCbRd;
+    PAF_AudioData *pPcmBufRd, *pPcmBufWrt;
     
     // Get gate handle
     gateHandle = pCbCtl->gateHandle;
@@ -492,35 +494,81 @@ Int cbWriteAf(
                 return ASP_DECOP_CB_AF_WRITE_OVERFLOW;
             }
             
+            // FL: this won't reliably detect overflow because of PCM buffer write address wrap
             // check PCM overflow
-            if ((pCb->numPcmSampsPerCh + pAfWrt->sampleCount) > pCb->maxNumPcmSampsPerCh)
-            {
-                pCb->errPcmOvrCnt++;
-
-                Log_info3("cbWriteAf: ERROR: PCM CB overflow, sampleCount=%d, numPcmSampsPerCh=%d, maxNumPcmSampsPerCh=%d",
-                    pCb->numPcmSampsPerCh, pAfWrt->sampleCount, pCb->maxNumPcmSampsPerCh);
-
-                // Write back circular buffer configuration
-                Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
-                Cache_wait();
-
-                // Leave the gate
-                GateMP_leave(gateHandle, key);
+            //if ((pCb->numPcmSampsPerCh + pAfWrt->sampleCount) > pCb->maxNumPcmSampsPerCh)
+            //{
+            //    pCb->errPcmOvrCnt++;
+            //
+            //    Log_info3("cbWriteAf: ERROR: PCM CB overflow, sampleCount=%d, numPcmSampsPerCh=%d, maxNumPcmSampsPerCh=%d",
+            //        pCb->numPcmSampsPerCh, pAfWrt->sampleCount, pCb->maxNumPcmSampsPerCh);
+            //
+            //    // Write back circular buffer configuration
+            //    Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
+            //    Cache_wait();
+            //
+            //    // Leave the gate
+            //    GateMP_leave(gateHandle, key);
+            //
+            //    return ASP_DECOP_CB_PCM_WRITE_OVERFLOW;
+            //}
 
-                return ASP_DECOP_CB_PCM_WRITE_OVERFLOW;
-            }
-                
-            pAfCb = &pCb->afCb[pCb->afWrtIdx];
-            pPcmBuf = pAfCb->data.sample[0];
-            pMetaBuf = pAfCb->pafPrivateMetadata[0].pMdBuf;
-            if ((pPcmBuf + (pAfWrt->sampleCount * pCb->maxAFChanNum)) >= (pCb->pcmBufEnd))
+            pAfCb = &pCb->afCb[pCb->afWrtIdx]; // get CB AF to be written
+            
+            // Get reader current PCM buffer location for PCM buffer overflow check
+            pAfCbRd = &pCb->afCb[pCb->afRdIdx];
+            //pPcmBufRd = pAfCbRd->data.sample[0];                    // FL: starting location of PCM samples for AF being read
+            pPcmBufRd = pAfCbRd->data.sample[0] + pCb->pcmRdIdx;    // FL: current location of PCM samples for AF being read
+            
+            pPcmBufWrt = pAfCb->data.sample[0];             // get current location in PCM buffer to be written
+            // (***) FL: currently no metadata buffer overflow detection
+            pMetaBuf = pAfCb->pafPrivateMetadata[0].pMdBuf; // get current location in MD buffer to be written
+            
+            // check PCM buffer overflow
+            pPcmBuf = pPcmBufWrt;
+            for (i = 0; i < pCb->maxAFChanNum; i++)
             {
-                pPcmBuf = pCb->pcmBuf;
+                // check PCM buffer wrap
+                if ((pPcmBuf + pAfWrt->sampleCount) >= pCb->pcmBufEnd)
+                {
+                    pPcmBuf = pCb->pcmBuf;
+                }
+                
+                // check PCM buffer overflow
+                if ((pPcmBuf < pPcmBufRd) && 
+                    ((pPcmBuf + pAfWrt->sampleCount) >= pPcmBufRd))
+                {
+                    pCb->errPcmOvrCnt++;;
+                    
+                    Log_info2("cbWriteAf: ERROR: PCM CB overflow, sampleCount=%d, numPcmSampsPerCh=%d", 
+                         pAfWrt->sampleCount, pCb->numPcmSampsPerCh);
+            
+                    //SW_BREAKPOINT; // debug
+                    
+                    // Write back circular buffer configuration
+                    Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
+                    Cache_wait();
+            
+                    // Leave the gate
+                    GateMP_leave(gateHandle, key);
+            
+                    return ASP_DECOP_CB_PCM_WRITE_OVERFLOW;
+                }
+                
+                pPcmBuf += pAfWrt->sampleCount;
             }
             
-            for (i=0; i<pCb->maxAFChanNum; i++)
+            // configure AF sample pointers
+            pPcmBuf = pPcmBufWrt;
+            for (i = 0; i < pCb->maxAFChanNum; i++)
             {
-                pAfCb->data.sample[i] = pPcmBuf;
+                // check PCM buffer wrap
+                if ((pPcmBuf + pAfWrt->sampleCount) >= pCb->pcmBufEnd)
+                {
+                    pPcmBuf = pCb->pcmBuf;
+                }
+                
+                pAfCb->data.sample[i] = pPcmBuf;                
                 pPcmBuf += pAfWrt->sampleCount;
                 pAfCb->data.samsiz[i] = 0;
             }