2 /*
3 Copyright (c) 2016, 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/ipc/GateMP.h>
42 #include "aspDecOpCircBuf_common.h"
44 // Reset circular buffer
45 Int cbReset(
46 PAF_AST_DecOpCircBufCtl *pCbCtl,
47 Int8 cbIdx
48 )
49 {
50 IArg key;
51 GateMP_Handle gateHandle;
52 PAF_AST_DecOpCircBuf *pCb;
53 PAF_AudioFrame *pAfCb;
54 Int8 n;
55 Int8 i;
57 // Get gate handle
58 gateHandle = pCbCtl->gateHandle;
59 // Enter gate
60 key = GateMP_enter(gateHandle);
62 // Get circular buffer base pointer
63 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
65 // Invalidate circular buffer configuration
66 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
67 Cache_wait();
69 if (pCb->sourceSel == PAF_SOURCE_PCM)
70 {
71 // 2*256 in behind
72 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_PCM;
73 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_PCM;
74 pCb->pcmRdIdx = 0;
75 }
76 else if (pCb->sourceSel == PAF_SOURCE_DDP)
77 {
78 // 4*256 in behind
79 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_DDP;
80 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_DDP;
81 pCb->pcmRdIdx = pCb->decOpFrameLen - ASP_DECOP_CB_INIT_LAG_DDP*pCb->strFrameLen;
82 }
83 else if (pCb->sourceSel == PAF_SOURCE_THD)
84 {
85 // 0 in behind
86 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_THD;
87 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_THD;
88 pCb->pcmRdIdx = 0;
89 }
91 // initialize circular buffer current number of frames
92 pCb->numAfCb = pCb->afWrtIdx - pCb->afRdIdx;
94 for (n=0; n<pCb->maxNumAfCb; n++)
95 {
96 pAfCb = &pCb->afCb[n];
98 // clear PCM data
99 for (i=0; i<pCb->maxAFChanNum; i++)
100 {
101 memset(pAfCb->data.sample[i], 0, pCb->maxAFSampCount);
102 }
104 // clear metadata
105 pAfCb->numPrivateMetadata = 0;
106 }
108 // reset error counts
109 pCb->errUndCnt = 0;
110 pCb->errOvrCnt = 0;
112 // Write back circular buffer configuration
113 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
115 // Write back AF circular buffer
116 Cache_wb(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
117 for (n=0; n<pCb->maxNumAfCb; n++)
118 {
119 pAfCb = &pCb->afCb[n];
120 for (i=0; i<pCb->maxAFChanNum; i++)
121 {
122 Cache_wb(pAfCb->data.sample[i], pCb->maxAFSampCount*sizeof(PAF_AudioData), Cache_Type_ALLD, 0);
123 }
124 }
125 Cache_wait();
127 // Leave the gate
128 GateMP_leave(gateHandle, key);
130 return ASP_DECOP_CB_SOK;
131 }
133 // Output log of circular buffer control variables (debug)
134 Int cbLog(
135 PAF_AST_DecOpCircBufCtl *pCbCtl,
136 Int8 cbIdx,
137 Int8 fullLog,
138 char *locInfo
139 )
140 {
141 IArg key;
142 GateMP_Handle gateHandle;
143 PAF_AST_DecOpCircBuf *pCb;
145 // Get gate handle
146 gateHandle = pCbCtl->gateHandle;
147 // Enter gate
148 key = GateMP_enter(gateHandle);
150 // Get circular buffer base pointer
151 pCb = &(*pCbCtl->pXDecOpCb)[cbIdx];
153 // Invalidate circular buffer configuration.
154 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
155 Cache_wait();
157 Log_info1("CB: %s", (IArg)locInfo);
158 Log_info3("CB: readerActiveFlag=%d, writerActiveFlag=%d, emptyFlag=%d", pCb->readerActiveFlag, pCb->writerActiveFlag, pCb->emptyFlag);
159 Log_info4("CB: afRdIdx=%d, pcmRdIdx=%d, afWrtIdx=%d, numAfCb=%d", pCb->afRdIdx, pCb->pcmRdIdx,
160 pCb->afWrtIdx,
161 pCb->numAfCb);
162 if (fullLog)
163 {
164 Log_info1("CB: maxNumAfCb=%d", pCb->maxNumAfCb);
165 Log_info2("CB: decOpFrameLen=%d, strFrameLen=%d", pCb->decOpFrameLen, pCb->strFrameLen);
166 //Log_info1("cbWriteInit=%d", pCb->cbWriteAfInit);
167 }
169 // Leave the gate
170 GateMP_leave(gateHandle, key);
172 return ASP_DECOP_CB_SOK;
173 }