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/ipc/GateMP.h>
42 #include "aspDecOpCircBuf_common.h"
44 // Initialize circular buffer control
45 Int cbCtlInit(
46 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
47 Int8 numDecOpCb, // number of circular buffers
48 PAF_AST_DecOpCircBuf **pXDecOpCb // address of decoder output circular buffer base pointer
49 )
50 {
51 #ifdef _TMS320C6X
52 GateMP_Params gateParams;
53 GateMP_Handle gateHandle;
55 GateMP_Params_init(&gateParams);
56 gateParams.localProtect = GateMP_LocalProtect_THREAD;
57 gateParams.remoteProtect = GateMP_RemoteProtect_SYSTEM;
58 gateParams.name = ASP_DECODE_CB_GATE_NAME;
59 gateParams.regionId = ASP_DECODE_CB_GATE_REGION_ID;
60 gateHandle = GateMP_create(&gateParams);
61 if (gateHandle != NULL)
62 {
63 pCbCtl->gateHandle = gateHandle;
64 }
65 else
66 {
67 pCbCtl->gateHandle = NULL;
68 return ASP_DECOP_CB_CTL_INIT_INV_GATE;
69 }
71 pCbCtl->numDecOpCb = numDecOpCb; // init number of circular buffers
72 pCbCtl->pXDecOpCb = pXDecOpCb; // init base address of circular buffers
74 return ASP_DECOP_CB_SOK;
76 #elif defined(ARMCOMPILE)
77 GateMP_Handle gateHandle;
78 Int status;
80 do {
81 status = GateMP_open(ASP_DECODE_CB_GATE_NAME, &gateHandle);
82 } while (status == GateMP_E_NOTFOUND);
83 if (status == GateMP_S_SUCCESS)
84 {
85 pCbCtl->gateHandle = gateHandle;
86 }
87 else
88 {
89 pCbCtl->gateHandle = NULL;
90 return ASP_DECOP_CB_CTL_INIT_INV_GATE;
91 }
93 pCbCtl->numDecOpCb = numDecOpCb; // init number of circular buffers
94 pCbCtl->pXDecOpCb = pXDecOpCb; // init base address of circular buffers
96 return ASP_DECOP_CB_SOK;
98 #else
99 #error "Unsupported platform"
101 #endif
102 }
104 // Reset circular buffer
105 Int cbReset(
106 PAF_AST_DecOpCircBufCtl *pCbCtl,
107 Int8 cbIdx
108 )
109 {
110 IArg key;
111 GateMP_Handle gateHandle;
112 PAF_AST_DecOpCircBuf *pCb;
113 PAF_AudioFrame *pAfCb;
114 Int8 n;
115 Int8 i;
117 // Get gate handle
118 gateHandle = pCbCtl->gateHandle;
119 // Enter gate
120 key = GateMP_enter(gateHandle);
122 // Get circular buffer base pointer
123 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
125 // Invalidate circular buffer configuration
126 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
127 Cache_wait();
129 if (pCb->sourceSel == PAF_SOURCE_PCM)
130 {
131 // 2*256 in behind
132 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_PCM;
133 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_PCM;
134 pCb->pcmRdIdx = 0;
135 }
136 else if (pCb->sourceSel == PAF_SOURCE_DDP)
137 {
138 // 4*256 in behind
139 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_DDP;
140 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_DDP;
141 pCb->pcmRdIdx = pCb->decOpFrameLen - ASP_DECOP_CB_INIT_LAG_DDP*pCb->strFrameLen;
142 }
143 else if (pCb->sourceSel == PAF_SOURCE_THD)
144 {
145 // 0 in behind
146 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_THD;
147 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_THD;
148 pCb->pcmRdIdx = 0;
149 }
151 // initialize circular buffer current number of frames
152 pCb->numAfCb = pCb->afWrtIdx - pCb->afRdIdx;
154 for (n=0; n<pCb->maxNumAfCb; n++)
155 {
156 pAfCb = &pCb->afCb[n];
158 // clear PCM data
159 for (i=0; i<pCb->maxAFChanNum; i++)
160 {
161 memset(pAfCb->data.sample[i], 0, pCb->maxAFSampCount);
162 }
164 // clear metadata
165 pAfCb->numPrivateMetadata = 0;
166 }
168 // reset stats
169 pCb->readAfWriterInactiveCnt = 0;
170 pCb->wrtAfReaderInactiveCnt = 0;
171 pCb->wrtAfZeroSampsCnt = 0;
172 pCb->errUndCnt = 0;
173 pCb->errOvrCnt = 0;
175 // Write back circular buffer configuration
176 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
178 // Write back AF circular buffer
179 Cache_wb(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
180 for (n=0; n<pCb->maxNumAfCb; n++)
181 {
182 pAfCb = &pCb->afCb[n];
183 for (i=0; i<pCb->maxAFChanNum; i++)
184 {
185 Cache_wb(pAfCb->data.sample[i], pCb->maxAFSampCount*sizeof(PAF_AudioData), Cache_Type_ALLD, 0);
186 }
187 }
188 Cache_wait();
190 // Leave the gate
191 GateMP_leave(gateHandle, key);
193 return ASP_DECOP_CB_SOK;
194 }
196 // Get circular buffer statistics
197 Int cbGetStats(
198 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
199 Int8 cbIdx, // decoder output circular buffer index
200 PAF_AST_DecOpCircBufStats *pCbStats // decoder output circular buffer statistics
202 )
203 {
204 IArg key;
205 GateMP_Handle gateHandle;
206 PAF_AST_DecOpCircBuf *pCb;
208 // Get gate handle
209 gateHandle = pCbCtl->gateHandle;
210 // Enter gate
211 key = GateMP_enter(gateHandle);
213 // Get circular buffer base pointer
214 pCb = &(*pCbCtl->pXDecOpCb)[cbIdx];
216 // Invalidate circular buffer configuration.
217 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
218 Cache_wait();
220 // Populate statistics
221 pCbStats->readAfWriterInactiveCnt = pCb->readAfWriterInactiveCnt;
222 pCbStats->wrtAfReaderInactiveCnt = pCb->wrtAfReaderInactiveCnt;
223 pCbStats->wrtAfZeroSampsCnt = pCb->wrtAfZeroSampsCnt;
224 pCbStats->errUndCnt = pCb->errUndCnt;
225 pCbStats->errOvrCnt = pCb->errOvrCnt;
227 // Leave the gate
228 GateMP_leave(gateHandle, key);
230 return ASP_DECOP_CB_SOK;
231 }
234 // Output log of circular buffer control variables (debug)
235 Int cbLog(
236 PAF_AST_DecOpCircBufCtl *pCbCtl,
237 Int8 cbIdx,
238 Int8 fullLog,
239 char *locInfo
240 )
241 {
242 IArg key;
243 GateMP_Handle gateHandle;
244 PAF_AST_DecOpCircBuf *pCb;
246 // Get gate handle
247 gateHandle = pCbCtl->gateHandle;
248 // Enter gate
249 key = GateMP_enter(gateHandle);
251 // Get circular buffer base pointer
252 pCb = &(*pCbCtl->pXDecOpCb)[cbIdx];
254 // Invalidate circular buffer configuration.
255 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
256 Cache_wait();
258 Log_info1("CB: %s", (IArg)locInfo);
259 Log_info3("CB: readerActiveFlag=%d, writerActiveFlag=%d, drainFlag=%d", pCb->readerActiveFlag, pCb->writerActiveFlag, pCb->drainFlag);
260 Log_info4("CB: afRdIdx=%d, pcmRdIdx=%d, afWrtIdx=%d, numAfCb=%d", pCb->afRdIdx, pCb->pcmRdIdx,
261 pCb->afWrtIdx,
262 pCb->numAfCb);
263 if (fullLog)
264 {
265 Log_info1("CB: maxNumAfCb=%d", pCb->maxNumAfCb);
266 Log_info2("CB: decOpFrameLen=%d, strFrameLen=%d", pCb->decOpFrameLen, pCb->strFrameLen);
267 //Log_info1("cbWriteInit=%d", pCb->cbWriteAfInit);
268 }
270 // Leave the gate
271 GateMP_leave(gateHandle, key);
273 return ASP_DECOP_CB_SOK;
274 }