]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blobdiff - pasdk/test_arm/framework/aspDecOpCircBuf_slave.c
PASDK-218:CB updates for THD 192 kHz
[processor-sdk/performance-audio-sr.git] / pasdk / test_arm / framework / aspDecOpCircBuf_slave.c
index 7a80156fa85a29a7dac4066619ee2ccfbe28d8c5..79c645bf5bf92f409e311f979c3ef236091ad078 100644 (file)
@@ -141,6 +141,7 @@ Int cbInitDecWrite(
     //  - target nominal delay
     //  - AF write, read indices
     //  - maximum AF channel and sample counts
+    //  - maximum number of PCM samples per channel
     if (sourceSelect == PAF_SOURCE_PCM)
     {
         pCb->maxNumAfCb = ASP_DECOP_CB_MAX_NUM_AF_PCM;
@@ -150,11 +151,11 @@ Int cbInitDecWrite(
         
         pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_PCM;
         pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_PCM;
-        pCb->pcmRdIdx = 0;
-        pCb->prvMdRdIdx = 0;
         
         pCb->maxAFChanNum = ASP_DECOP_CB_MAX_NUM_PCM_CH;
-        pCb->maxAFSampCount = DEF_DEC_OP_FRAME_LEN;        
+        pCb->maxAFSampCount = DEF_DEC_OP_FRAME_LEN; 
+
+        pCb->maxNumPcmSampsPerCh = (Int32)(pCb->pcmBufEnd - pCb->pcmBuf)/pCb->maxAFChanNum;
     }
     else if ((sourceSelect == PAF_SOURCE_DDP) || (sourceSelect == PAF_SOURCE_AC3))
     {
@@ -165,11 +166,11 @@ Int cbInitDecWrite(
         
         pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_DDP;
         pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_DDP;
-        pCb->pcmRdIdx = 0;
-        pCb->prvMdRdIdx = 0;
         
         pCb->maxAFChanNum = ASP_DECOP_CB_MAX_NUM_PCM_CH_DDP;
         pCb->maxAFSampCount = ASP_DECOP_CB_MAX_PCM_FRAME_LEN_48kDDP;
+
+        pCb->maxNumPcmSampsPerCh = (Int32)(pCb->pcmBufEnd - pCb->pcmBuf)/pCb->maxAFChanNum;
     }
     else if (sourceSelect == PAF_SOURCE_THD)
     {
@@ -200,11 +201,11 @@ Int cbInitDecWrite(
         
         pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_THD;
         pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_THD;
-        pCb->pcmRdIdx = 0;
-        pCb->prvMdRdIdx = 0;
         
         pCb->maxAFChanNum = ASP_DECOP_CB_MAX_NUM_PCM_CH_MAT;
         pCb->maxAFSampCount = ASP_DECOP_CB_MAX_PCM_FRAME_LEN_48kMAT;        
+
+        pCb->maxNumPcmSampsPerCh = (Int32)(pCb->pcmBufEnd - pCb->pcmBuf)/pCb->maxAFChanNum;
     }
     else
     {
@@ -219,7 +220,15 @@ Int cbInitDecWrite(
 
         return ASP_DECOP_CB_INIT_INV_SOURCE_SEL;
     }
-    
+
+    // Initialize circular buffer:
+    //  - PCM read index
+    //  - Private metadata read index
+    //  - number of PCM samples in CB
+    pCb->pcmRdIdx = 0;
+    pCb->prvMdRdIdx = 0;
+    pCb->numPcmSampsPerCh = 0;
+
     // Initialize audio frames
     for (n = 0; n < pCb->maxNumAfCb; n++)
     {
@@ -290,8 +299,10 @@ Int cbInitDecWrite(
     pCb->readAfNdCnt = 0;
     pCb->wrtAfReaderInactiveCnt = 0;
     pCb->wrtAfZeroSampsCnt = 0;
-    pCb->errUndCnt = 0;
-    pCb->errOvrCnt = 0;
+    pCb->errAfUndCnt = 0;
+    pCb->errAfOvrCnt = 0;
+    pCb->errPcmUndCnt = 0;
+    pCb->errPcmOvrCnt = 0;
     
     // Write back circular buffer configuration
     Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
@@ -430,8 +441,10 @@ 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;
     // Enter gate
@@ -461,14 +474,13 @@ Int cbWriteAf(
         {
             //Log_info2("cbWriteAf:pCb->numAfCb=%d, pCb->maxNumAfCb=%d", (IArg)pCb->readerActiveFlag, (IArg)pCb->maxNumAfCb); // debug
 
-            // check overflow
-            //while (pCb->numAfCb >= pCb->maxNumAfCb); // debug
+            // check AF overflow
             if (pCb->numAfCb >= pCb->maxNumAfCb)
             {
-                pCb->errOvrCnt++;
+                pCb->errAfOvrCnt++;
 
                 //SW_BREAKPOINT;
-                Log_info1("cbWriteAf: ERROR: overflow, numAfCb=%d", pCb->numAfCb);
+                Log_info1("cbWriteAf: ERROR: AF CB overflow, numAfCb=%d", pCb->numAfCb);
 
                 // Write back circular buffer configuration
                 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
@@ -479,28 +491,91 @@ Int cbWriteAf(
 
                 //Log_info2("cbWriteAf:gate leave, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // debug
 
-                return ASP_DECOP_CB_WRITE_OVERFLOW;
+                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);
+            //
+            //    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;
             }
             Cache_inv(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0); // FL: this is write back and invalidate??
             Cache_wait();
 
-            //for (i=0; i<pCb->maxAFChanNum; i++){
-            //}
+            // FL: brute force reset of all metadata in CB AF?
             for (i=0; i<PAF_MAX_NUM_PRIVATE_MD; i++)
             {
                 pAfCb->pafPrivateMetadata[i].offset = 0;
@@ -510,7 +585,7 @@ Int cbWriteAf(
             }
 
             nextWrtIdx = 0;
-            if ((pCb->afWrtIdx +1) >= pCb->maxNumAfCb)
+            if ((pCb->afWrtIdx + 1) >= pCb->maxNumAfCb)
             {
                 //Log_info0("cbWriteAf: AF Wrap around **** ");
                 // next audio frame will be audio frame 0
@@ -551,7 +626,10 @@ Int cbWriteAf(
                     pAfCb->data.samsiz[i] = pAfWrt->data.samsiz[i];
                 }
             }
-
+            
+            // Update PCM samples per channel
+            pCb->numPcmSampsPerCh += pAfWrt->sampleCount;
+            
             #ifdef CB_RW_OP_CAP_PP // debug
             if (pCb->cb_opCnt < CB_OP_COUNT_MAX)
             {