]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - pasdk/test_arm/framework/aspDecOpCircBuf_slave.c
PASDK-277:Increase MAT-THD nominal delay to 3504.
[processor-sdk/performance-audio-sr.git] / pasdk / test_arm / framework / aspDecOpCircBuf_slave.c
2 /*
3 Copyright (c) 2017, Texas Instruments Incorporated - http://www.ti.com/
4 All rights reserved.
6 * Redistribution and use in source and binary forms, with or without 
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 #include <string.h> // for memset
37 #include <xdc/std.h>
38 #include <xdc/runtime/Log.h>
39 #include <ti/sysbios/hal/Cache.h>
40 #include <ti/uia/events/UIAEvt.h>
42 #include "common.h"
43 #include "paftyp.h"
44 //#include "pafdec.h"
45 //#include "pafsp.h"
46 #include "aspDecOpCircBuf_slave.h"
48 #include "evmc66x_gpio_dbg.h" // Debug
50 // Initialize circular buffer control
51 Int cbCtlInit(
52     PAF_AST_DecOpCircBufCtl *pCbCtl,    // decoder output circular buffer control
53     PAF_AST_DecOpCircBuf **pXDecOpCb    // address of decoder output circular buffer base pointer
54 )
55 {
56     GateMP_Handle gateHandle;
57     Int status;
58     
59     do {
60         status = GateMP_open(ASP_DECODE_CB_GATE_NAME, &gateHandle);
61     } while (status == GateMP_E_NOTFOUND);
62     if (status == GateMP_S_SUCCESS)
63     {
64         pCbCtl->gateHandle = gateHandle;
65     }
66     else
67     {
68         pCbCtl->gateHandle = NULL;
69         return ASP_DECOP_CB_CTL_INIT_INV_GATE;
70     }
71     
72     pCbCtl->pXDecOpCb = pXDecOpCb;
73     
74     return ASP_DECOP_CB_SOK;
75     
76 }
78 //Int8 gCbWriteStartCnt=0; // debug
80 // Start writes to circular buffer
81 Int cbWriteStart(
82     PAF_AST_DecOpCircBufCtl *pCbCtl,    // decoder output circular buffer control
83     Int8 cbIdx                          // decoder output circular buffer index
84 )
85 {
86     IArg key;
87     GateMP_Handle gateHandle;
88     PAF_AST_DecOpCircBuf *pCb;
89     PAF_AudioFrame *pAfCb;
90     Int8 n;
91     //Int8 i;
93     //gCbWriteStartCnt++; // debug
94     
95     // Get gate handle
96     gateHandle = pCbCtl->gateHandle;
97     // Enter gate
98     key = GateMP_enter(gateHandle);
100     // Get circular buffer base pointer
101     pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
103     // Invalidate circular buffer configuration.
104     // NOTE: Probably only a subset of this information needs to be updated.
105     Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
106     Cache_wait();
107     
108     //Log_info1("cbWriteStart:afCb=0x%04x", (IArg)pCb->afCb); // debug
109     
110     // Invalidate AF circular buffer
111     Cache_inv(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
112     for (n=0; n<pCb->maxNumAfCb; n++)
113     {
114         pAfCb = &pCb->afCb[n];
115         Cache_inv(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
116     }
117     Cache_wait();
118             
119     // update flags
120     pCb->writerActiveFlag = 1;
121     pCb->emptyFlag = 0;
122     //pCb->afLagIdx = 0;
123     
124     // Write back circular buffer configuration
125     Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
126     Cache_wait();
128     // Leave the gate
129     GateMP_leave(gateHandle, key);
131     return ASP_DECOP_CB_SOK;
132 };
134 // Stop writes to circular buffer
135 Int cbWriteStop(
136     PAF_AST_DecOpCircBufCtl *pCbCtl,    // decoder output circular buffer control
137     Int8 cbIdx                          // decoder output circular buffer index
140     IArg key;
141     GateMP_Handle gateHandle;
142     PAF_AST_DecOpCircBuf *pCb;
144     // Get gate handle
145     gateHandle = pCbCtl->gateHandle;
146     // Enter gate
147     key = GateMP_enter(gateHandle);
149     // Get circular buffer base pointer
150     pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
152     // Invalidate circular buffer configuration
153     Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
154     Cache_wait();
156     //Log_info1("cbWriteStop:afCb=0x%04x", (IArg)pCb->afCb);  // debug
157     
158     // update flags
159     pCb->writerActiveFlag = 0;
160     pCb->emptyFlag = 1;
162     // Write back circular buffer configuration
163     Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
164     Cache_wait();
165     
166     // Leave the gate
167     GateMP_leave(gateHandle, key);
169     return ASP_DECOP_CB_SOK;
172 // debug
173 //Int16 gSampleCountBuf[10];
174 //Int16 gCalcDeltaSampsBuf[10];
175 //Int8 gPrimedFlagCnt=0;
177 // (***) FL: revisit
178 // Write audio frame to circular buffer
179 Int cbWriteAf(
180     PAF_AST_DecOpCircBufCtl *pCbCtl,    // decoder output circular buffer control
181     Int8 cbIdx,                         // decoder output circular buffer index
182     PAF_AudioFrame *pAfWrt              // audio frame from which to write
185     IArg key;
186     GateMP_Handle gateHandle;
187     PAF_AST_DecOpCircBuf *pCb;
188     PAF_AudioFrame *pAfCb;
189     PAF_ChannelMask_HD streamMask;
190     Int8 i;
191     Int16 j;
192     PAF_AudioData *pPcmBuf;UInt8 *pMetaBuf; int nextWrtIdx;PAF_AudioFrame *pAfCbNextAf; 
194     // Get gate handle
195     gateHandle = pCbCtl->gateHandle;
196     // Enter gate
197     key = GateMP_enter(gateHandle);
199     //Log_info2("cbWriteAf:gate enter, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // debug
201     // Get circular buffer base pointer
202     pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
203     //Log_info1("cbWriteAf:pCb=0x%04x", (IArg)pCb); // debug
205     // Invalidate circular buffer configuration.
206     // NOTE: Probably only a subset of this information needs to be updated.
207     Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
208     Cache_wait();
210     //Log_info1("cbWriteAf:afCb=0x%04x", (IArg)pCb->afCb); // debug
211     //Log_info2("cbWriteAf:pCb->readerActiveFlag=%d, pCb->writerActiveFlag=%d", (IArg)pCb->readerActiveFlag, (IArg)pCb->writerActiveFlag); // debug
213     if ((pCb->readerActiveFlag == 1) && (pAfWrt->sampleCount)) //QIN ?
214     {
215         //
216         // Normal case, reader active.
217         // If reader not active, don't write to circular buffer or check OVRflow.
219 #if 0        
220         if (pCb->cbWriteAfInit == 0)
221         {
222             // Invalidate AF circular buffer
223             Cache_inv(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
224             for (n=0; n<pCb->maxNumAfCb; n++)
225             {
226                 pAfCb = &pCb->afCb[n];
227                 Cache_inv(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
228             }
229             Cache_wait();
231             pCb->cbWriteAfInit = 1;
232         }
233 #endif        
235         //Log_info2("cbWriteAf:pCb->numAfCb=%d, pCb->maxNumAfCb=%d", (IArg)pCb->readerActiveFlag, (IArg)pCb->maxNumAfCb); // debug
237         // check overflow
238         //while (pCb->numAfCb >= pCb->maxNumAfCb); // debug
239         if (pCb->numAfCb >= pCb->maxNumAfCb)
240         {
241             pCb->errOvrCnt++;
243             //SW_BREAKPOINT;
244             Log_info1("cbWriteAf: ERROR: overflow, numAfCb=%d", pCb->numAfCb);
246             // Write back circular buffer configuration
247             Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
249             // Leave the gate
250             GateMP_leave(gateHandle, key);
252             //Log_info2("cbWriteAf:gate leave, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // debug
254             return ASP_DECOP_CB_WRITE_OVERFLOW;
255         }
257         pAfCb = &pCb->afCb[pCb->afWrtIdx];
258         pPcmBuf = pAfCb->data.sample[0];
259         pMetaBuf = pAfCb->pafPrivateMetadata[0].pMdBuf;
260         if((pPcmBuf + (pAfWrt->sampleCount * pCb->maxAFChanNum )) > (pCb->pcmBufEnd))
261         {
262             pPcmBuf = pCb->pcmBuf;
263         }
265         for (i=0; i<pCb->maxAFChanNum; i++)
266         {
267             pAfCb->data.sample[i] = pPcmBuf;
268             pPcmBuf += pAfWrt->sampleCount;
269             pAfCb->data.samsiz[i] = 0;
270         }
271         Cache_inv(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0); // FL: this is write back and invalidate??
272         Cache_wait();
274         for (i=0; i<pCb->maxAFChanNum; i++){
275         }
276         for (i=0; i<PAF_MAX_NUM_PRIVATE_MD; i++)
277         {
278             pAfCb->pafPrivateMetadata[i].offset = 0;
279             pAfCb->pafPrivateMetadata[i].size   = 0;
280             pAfCb->pafPrivateMetadata[i].pMdBuf = pMetaBuf;
281             pMetaBuf += PAF_MAX_PRIVATE_MD_SZ;
282         }
284         nextWrtIdx = 0;
285         if ((pCb->afWrtIdx +1) >= pCb->maxNumAfCb)
286         {
287             //Log_info0("cbWriteAf: AF Wrap around **** ");
288             // next audio frame will be audio frame 0
289             nextWrtIdx = 0;
290         }else{
291             // next audio frame will be current audio frame + 1
292             nextWrtIdx = pCb->afWrtIdx + 1;
293         }
295         pAfCbNextAf = &pCb->afCb[nextWrtIdx]; // +1 or last AF if overflow
296         pAfCbNextAf->data.sample[0] = &pAfCb->data.sample[pCb->maxAFChanNum - 1][pAfWrt->sampleCount];// pAfCb->data.sample[15] + (pAfCb->sampleCount * sizeof(PAF_AudioData));
298         // write audio frame information updated by decoder
299         pAfCb->sampleDecode = pAfWrt->sampleDecode;
300         PAF_PROCESS_COPY(pAfCb->sampleProcess, pAfWrt->sampleProcess);
301         pAfCb->sampleRate = pAfWrt->sampleRate;
302         pAfCb->sampleCount = pAfWrt->sampleCount;
303         pAfCb->channelConfigurationRequest = pAfWrt->channelConfigurationRequest;
304         pAfCb->channelConfigurationStream = pAfWrt->channelConfigurationStream;
305         // write metadata information updated by decoder
306         pAfCb->bsMetadata_type     = pAfWrt->bsMetadata_type;        /* non zero if metadata is attached. */
307         pAfCb->pafBsMetadataUpdate = pAfWrt->pafBsMetadataUpdate;    /* indicates whether bit-stream metadata update */
308         pAfCb->numPrivateMetadata  = pAfWrt->numPrivateMetadata;     /* number of valid private metadata (0 or 1 if metadata filtering enabled) */
309         pAfCb->bsMetadata_offset   = pAfWrt->bsMetadata_offset;      /* offset into audio frame for change in bsMetadata_type field */
310         // write PCM samples
311         streamMask = pAfWrt->fxns->channelMask(pAfWrt, pAfCb->channelConfigurationStream);
312         for (i = 0; i < pCb->maxAFChanNum; i++)
313         {
314             if ((streamMask >> i) & 0x1)
315             {
316                 for (j = 0; j < pAfWrt->sampleCount; j++)
317                 {
318                     pAfCb->data.sample[i][j] = pAfWrt->data.sample[i][j];
319                 }
321                 pAfCb->data.samsiz[i] = pAfWrt->data.samsiz[i];
322             }
323         }
325         #ifdef CB_RW_OP_CAP_PP // debug
326         if (pCb->cb_opCnt < CB_OP_COUNT_MAX)
327         {
328             if ((pCb->cb_samples_op != NULL) && (pCb->cb_op_owner != NULL))
329             {
330                 // log sample count
331                 pCb->cb_samples_op[pCb->cb_opCnt] = pAfWrt->sampleCount;
332                 pCb->cb_op_owner[pCb->cb_opCnt] = CB_OP_W;
333                 // log idxs
334                 pCb->cb_afRdIdx[pCb->cb_opCnt] = pCb->afRdIdx;
335                 pCb->cb_afWrtIdx[pCb->cb_opCnt] = pCb->afWrtIdx;
336                 pCb->cb_numAfCb[pCb->cb_opCnt] = pCb->numAfCb; // numAfCb might not be pointing to this instance
337                 pCb->cb_opCnt++;
338             }
339         }
340         #endif
342         // prepare metadata buffer pointers according to the metadata and buffer sizes
343         for (i=0; i < pAfWrt->numPrivateMetadata; i++)
344         {
345             UInt8 *nextMdBuf;
346             if(i == 0)
347                 nextMdBuf = (pAfCb->pafPrivateMetadata[0].pMdBuf + pAfWrt->pafPrivateMetadata[0].size);
348             else
349                 nextMdBuf = (pAfCb->pafPrivateMetadata[i-1].pMdBuf + pAfWrt->pafPrivateMetadata[i-1].size);
350             if(nextMdBuf >= pCb->metaBufEnd) // metadata buffer overflow
351             {
352                 pAfCb->pafPrivateMetadata[i].pMdBuf = pCb->metaBuf;
353             }
354             else if(i != 0)
355             {
356                 pAfCb->pafPrivateMetadata[i].pMdBuf = nextMdBuf;
357             }
358             Cache_inv(pAfCb->pafPrivateMetadata[i].pMdBuf, sizeof(UInt8 *), Cache_Type_ALLD, 0);
359         }
361         // Write metadata to circular buffer
362         for (i = 0; i < pAfWrt->numPrivateMetadata; i++) // only copy numPrivateMetadata
363         {
364             pAfCb->pafPrivateMetadata[i].offset = pAfWrt->pafPrivateMetadata[i].offset;
365             pAfCb->pafPrivateMetadata[i].size   = pAfWrt->pafPrivateMetadata[i].size;
366             memcpy(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].size);
367         }
369         Cache_inv(pAfCb->pafPrivateMetadata, pAfWrt->numPrivateMetadata*sizeof(PAF_PrivateMetadata *), Cache_Type_ALLD, 0); // FL: this is write back and invalidate??
370         Cache_wait();
371         for (i=0; i<pAfCb->numPrivateMetadata; i++) // only write back numPrivateMetadata
372         {
373             //Log_info4("cbWriteAf: AF: %d nummd: %d offset: %d size: %d ", pCb->afWrtIdx, pAfCb->numPrivateMetadata, pAfCb->pafPrivateMetadata[i].offset,  pAfCb->pafPrivateMetadata[i].size);
374             Cache_wb(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfCb->pafPrivateMetadata[i].size, Cache_Type_ALLD, 0);
375         }
376         // update audio frame write index
377         pCb->afWrtIdx++;
378         if (pCb->afWrtIdx >= pCb->maxNumAfCb)
379         {
380             pCb->afWrtIdx = 0;
381         }
383         pCb->afCb[pCb->afWrtIdx].data.sample[0] = &pAfCb->data.sample[pCb->maxAFChanNum - 1][pAfWrt->sampleCount];
384         if(pAfWrt->numPrivateMetadata > 0)
385         {
386             pCb->afCb[pCb->afWrtIdx].pafPrivateMetadata[0].pMdBuf = pAfCb->pafPrivateMetadata[pAfWrt->numPrivateMetadata - 1].pMdBuf + pAfWrt->pafPrivateMetadata[pAfWrt->numPrivateMetadata - 1].size;
387         }
388         else
389         {
390             pCb->afCb[pCb->afWrtIdx].pafPrivateMetadata[0].pMdBuf = pAfCb->pafPrivateMetadata[0].pMdBuf;
391             Cache_wb(pCb->afCb , ASP_DECOP_CB_MAX_NUM_PCM_FRAMES*sizeof(PAF_AudioFrame *), Cache_Type_ALLD, 0);
392             Cache_wait();
393         }
394         Cache_inv(pCb->afCb[pCb->afWrtIdx].pafPrivateMetadata[0].pMdBuf, sizeof(UInt8 *), Cache_Type_ALLD, 0);
395         Cache_wait();
396         // update number of audio frames in circular buffer
397         pCb->numAfCb++;
398         
399         // Update CB Lag index 
400         //if (pCb->afLagIdx < pCb->afInitialLag)
401         //{
402         //    pCb->afLagIdx += 1;
403         //}
404         
405         // Update CB primed flag
406         // calculate number of delta samples before 
407         if (pCb->primedFlag == 0)
408         {
409             pCb->primedFlag = 1;
410             
411             // Calculate number of output frames to block reader.
412             // This is sample count reader waits before allowed to actually read samples from the CB.
413             pCb->deltaSamps = (pCb->targetNDSamps - pAfWrt->sampleCount + (pCb->strFrameLen-1)) / pCb->strFrameLen * pCb->strFrameLen;
414             
415             // debug
416             //gSampleCountBuf[gPrimedFlagCnt] = pAfWrt->sampleCount;
417             //gCalcDeltaSampsBuf[gPrimedFlagCnt] = pCb->deltaSamps;
418             //if (gPrimedFlagCnt < 10)
419             //    gPrimedFlagCnt++;
420         }
422         // Write back circular buffer configuration
423         Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
424         // write back audio frame
425         Cache_wb(pAfCb, sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
426         Cache_wb(pAfCb->data.samsiz, pCb->maxAFChanNum*sizeof(PAF_AudioSize), Cache_Type_ALLD, 0);
427         Cache_wb(pAfCb->pafPrivateMetadata, pAfWrt->numPrivateMetadata*sizeof(PAF_PrivateMetadata *), Cache_Type_ALLD, 0);
428         Cache_wait();
429         // write back PCM data
430         for (i = 0; i < pCb->maxAFChanNum; i++)
431         {
432             if ((streamMask >> i) & 0x1)
433             {
434                 Cache_wb(pAfCb->data.sample[i], pAfWrt->sampleCount * sizeof(PAF_AudioData), Cache_Type_ALLD, 0);
435             }
436         }
437         Cache_wait();
439         {
440             static Uint8 toggleState = 0;
441            if (toggleState == 0)
442                GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_99);
443            else
444                GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_99);
445            toggleState = ~(toggleState);
446         }
447         Log_info3("wrote %d samples into AF %d sourceSel: %d", pAfCb->sampleCount, pCb->afWrtIdx, pCb->sourceSel);
448         Log_info4("CBWMETA num=%d  size=%d  offset=%d chrequest=0x%04x", pAfCb->numPrivateMetadata, pAfCb->pafPrivateMetadata[0].size, pAfCb->pafPrivateMetadata[0].offset, pAfCb->channelConfigurationRequest.full);
449     }
451     // Leave the gate
452     GateMP_leave(gateHandle, key);
454     //Log_info2("cbWriteAf:gate leave, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // debug
456     return ASP_DECOP_CB_SOK;
459 // Get next audio frame to write in circular buffer
460 Int cbGetNextWriteAf(
461     PAF_AST_DecOpCircBufCtl *pCbCtl,    // decoder output circular buffer control
462     Int8 cbIdx,                         // decoder output circular buffer index
463     PAF_AudioFrame **ppAfWrt            // audio frame next to be written
466     IArg key;
467     GateMP_Handle gateHandle;
468     PAF_AST_DecOpCircBuf *pCb;
470     // Get gate handle
471     gateHandle = pCbCtl->gateHandle;
472     // Enter gate
473     key = GateMP_enter(gateHandle);
475     // Get circular buffer base pointer
476     pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
478     // get pointer to current audio frame in circular buffer
479     *ppAfWrt = &pCb->afCb[pCb->afWrtIdx];
480     
481     // update audio frame write index
482     pCb->afWrtIdx++;
483     if (pCb->afWrtIdx > pCb->maxNumAfCb)
484     {
485         pCb->afWrtIdx = 0;
486     }    
487     
488     // Leave the gate
489     GateMP_leave(gateHandle, key);
491     return ASP_DECOP_CB_SOK;