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 // Initialize CB primed flag
130 pCb->primedFlag = 0;
131 // Initialize delta samples
132 pCb->deltaSamps = 0;
134 // Reset circular buffer:
135 // - AF write, read indices
136 if (pCb->sourceSel == PAF_SOURCE_PCM)
137 {
138 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_PCM;
139 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_PCM;
140 }
141 else if (pCb->sourceSel == PAF_SOURCE_DDP)
142 {
143 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_DDP;
144 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_DDP;
145 }
146 else if (pCb->sourceSel == PAF_SOURCE_THD)
147 {
148 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_THD;
149 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_THD;
150 }
151 else
152 {
153 //
154 // Currently unsupported source select
155 //
156 return ASP_DECOP_CB_RESET_INV_SOURCE_SEL;
157 }
159 // Reset circular buffer:
160 // - PCM read index
161 // - Private metadata read index
162 // - number of PCM samples in CB
163 pCb->pcmRdIdx = 0;
164 pCb->prvMdRdIdx = 0;
165 pCb->numPcmSampsPerCh = 0;
167 // initialize circular buffer current number of frames
168 pCb->numAfCb = pCb->afWrtIdx - pCb->afRdIdx;
170 for (n=0; n<pCb->maxNumAfCb; n++)
171 {
172 pAfCb = &pCb->afCb[n];
174 // Clear PCM data
175 for (i=0; i<pCb->maxAFChanNum; i++)
176 {
177 memset(pAfCb->data.sample[i], 0, pCb->maxAFSampCount);
178 pAfCb->data.samsiz[i] = 0;
179 }
181 // Clear metadata
182 pAfCb->bsMetadata_type = PAF_bsMetadata_none; /* non zero if metadata is attached. */
183 pAfCb->pafBsMetadataUpdate = 0; /* indicates whether bit-stream metadata update */
184 pAfCb->numPrivateMetadata = 0; /* number of valid private metadata (0 or 1 if metadata filtering enabled) */
185 pAfCb->bsMetadata_offset = 0; /* offset into audio frame for change in bsMetadata_type field */
186 for (i=0; i<PAF_MAX_NUM_PRIVATE_MD; i++)
187 {
188 pAfCb->pafPrivateMetadata[i].offset = 0;
189 pAfCb->pafPrivateMetadata[i].size = 0;
190 }
191 }
193 // reset stats
194 pCb->readAfWriterInactiveCnt = 0;
195 pCb->readAfNdCnt = 0;
196 pCb->wrtAfReaderInactiveCnt = 0;
197 pCb->wrtAfZeroSampsCnt = 0;
198 pCb->errAfUndCnt = 0;
199 pCb->errAfOvrCnt = 0;
200 pCb->errPcmUndCnt = 0;
201 pCb->errPcmOvrCnt = 0;
203 // Write back circular buffer configuration
204 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
206 // Write back AF circular buffer
207 Cache_wb(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
208 for (n=0; n<pCb->maxNumAfCb; n++)
209 {
210 pAfCb = &pCb->afCb[n];
211 for (i=0; i<pCb->maxAFChanNum; i++)
212 {
213 Cache_wb(pAfCb->data.sample[i], pCb->maxAFSampCount*sizeof(PAF_AudioData), Cache_Type_ALLD, 0);
214 }
215 }
216 Cache_wait();
218 // Leave the gate
219 GateMP_leave(gateHandle, key);
221 return ASP_DECOP_CB_SOK;
222 }
224 // Get circular buffer statistics
225 Int cbGetStats(
226 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
227 Int8 cbIdx, // decoder output circular buffer index
228 PAF_AST_DecOpCircBufStats *pCbStats // decoder output circular buffer statistics
230 )
231 {
232 IArg key;
233 GateMP_Handle gateHandle;
234 PAF_AST_DecOpCircBuf *pCb;
236 // Get gate handle
237 gateHandle = pCbCtl->gateHandle;
238 // Enter gate
239 key = GateMP_enter(gateHandle);
241 // Get circular buffer base pointer
242 pCb = &(*pCbCtl->pXDecOpCb)[cbIdx];
244 // Invalidate circular buffer configuration.
245 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
246 Cache_wait();
248 // Populate statistics
249 pCbStats->readAfWriterInactiveCnt = pCb->readAfWriterInactiveCnt;
250 pCbStats->readAfNdCnt = pCb->readAfNdCnt;
251 pCbStats->wrtAfReaderInactiveCnt = pCb->wrtAfReaderInactiveCnt;
252 pCbStats->wrtAfZeroSampsCnt = pCb->wrtAfZeroSampsCnt;
253 pCbStats->errAfUndCnt = pCb->errAfUndCnt;
254 pCbStats->errAfOvrCnt = pCb->errAfOvrCnt;
255 pCbStats->errPcmUndCnt = pCb->errPcmUndCnt;
256 pCbStats->errPcmOvrCnt = pCb->errPcmOvrCnt;
258 // Leave the gate
259 GateMP_leave(gateHandle, key);
261 return ASP_DECOP_CB_SOK;
262 }
265 // Output log of circular buffer control variables (debug)
266 Int cbLog(
267 PAF_AST_DecOpCircBufCtl *pCbCtl,
268 Int8 cbIdx,
269 Int8 fullLog,
270 char *locInfo
271 )
272 {
273 IArg key;
274 GateMP_Handle gateHandle;
275 PAF_AST_DecOpCircBuf *pCb;
277 // Get gate handle
278 gateHandle = pCbCtl->gateHandle;
279 // Enter gate
280 key = GateMP_enter(gateHandle);
282 // Get circular buffer base pointer
283 pCb = &(*pCbCtl->pXDecOpCb)[cbIdx];
285 // Invalidate circular buffer configuration.
286 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
287 Cache_wait();
289 Log_info1("CB: %s", (IArg)locInfo);
290 Log_info3("CB: readerActiveFlag=%d, writerActiveFlag=%d, drainFlag=%d", pCb->readerActiveFlag, pCb->writerActiveFlag, pCb->drainFlag);
291 Log_info5("CB: afRdIdx=%d, pcmRdIdx=%d, prvMdRdIdx=%d, afWrtIdx=%d, numAfCb=%d", pCb->afRdIdx, pCb->pcmRdIdx, pCb->prvMdRdIdx, pCb->afWrtIdx, pCb->numAfCb);
292 if (fullLog)
293 {
294 Log_info1("CB: maxNumAfCb=%d", pCb->maxNumAfCb);
295 Log_info2("CB: decOpFrameLen=%d, strFrameLen=%d", pCb->decOpFrameLen, pCb->strFrameLen);
296 //Log_info1("cbWriteInit=%d", pCb->cbWriteAfInit);
297 }
299 // Leave the gate
300 GateMP_leave(gateHandle, key);
302 return ASP_DECOP_CB_SOK;
303 }