3545757375b0d9e634dd0ccd04b64b21dd25e132
[processor-sdk/performance-audio-sr.git] / processor_audio_sdk_1_00_00_00 / pasdk / test_arm / framework / aspDecOpCircBuf_slave.c
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/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 // Initialize circular buffer control
49 Int cbCtlInit(
50 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
51 PAF_AST_DecOpCircBuf **pXDecOpCb // address of decoder output circular buffer base pointer
52 )
53 {
54 GateMP_Handle gateHandle;
55 Int status;
57 do {
58 status = GateMP_open(ASP_DECODE_CB_GATE_NAME, &gateHandle);
59 } while (status == GateMP_E_NOTFOUND);
60 if (status == GateMP_S_SUCCESS)
61 {
62 pCbCtl->gateHandle = gateHandle;
63 }
64 else
65 {
66 pCbCtl->gateHandle = NULL;
67 return ASP_DECOP_CB_CTL_INIT_INV_GATE;
68 }
70 pCbCtl->pXDecOpCb = pXDecOpCb;
72 return ASP_DECOP_CB_SOK;
74 }
76 // Start writes to circular buffer
77 Int cbWriteStart(
78 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
79 Int8 cbIdx // decoder output circular buffer index
80 )
81 {
82 IArg key;
83 GateMP_Handle gateHandle;
84 PAF_AST_DecOpCircBuf *pCb;
85 PAF_AudioFrame *pAfCb;
86 Int8 n;
87 //Int8 i;
89 // Get gate handle
90 gateHandle = pCbCtl->gateHandle;
91 // Enter gate
92 key = GateMP_enter(gateHandle);
94 // Get circular buffer base pointer
95 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
97 // (***) FL: revisit
98 // Invalidate circular buffer configuration.
99 // NOTE: Probably only a subset of this information needs to be updated.
100 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
101 Cache_wait();
103 //Log_info1("cbWriteStart:afCb=0x%04x", (IArg)pCb->afCb); // FL: debug
105 // Invalidate AF circular buffer
106 Cache_inv(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
107 for (n=0; n<pCb->maxNumAfCb; n++)
108 {
109 pAfCb = &pCb->afCb[n];
110 Cache_inv(pAfCb->data.sample, ASP_DECOP_CB_MAX_NUM_PCM_CH*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
111 // FL: unnecessary since part of AF
112 //for (i=0; i<PAF_MAX_NUM_PRIVATE_MD; i++) //QIN
113 //{
114 // Cache_inv(&pAfCb->pafPrivateMetadata[i], sizeof(PAF_PrivateMetadata), Cache_Type_ALLD, 0);
115 //}
116 }
117 Cache_wait();
119 // update flags
120 pCb->writerActiveFlag = 1;
121 pCb->emptyFlag = 0;
123 // (***) FL: revisit
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
138 )
139 {
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); // FL: debug
158 // update flags
159 pCb->writerActiveFlag = 0;
160 pCb->emptyFlag = 1;
162 // (***) FL: revisit
163 // Write back circular buffer configuration
164 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
165 Cache_wait();
167 // Leave the gate
168 GateMP_leave(gateHandle, key);
170 return ASP_DECOP_CB_SOK;
171 }
173 // Write audio frame to circular buffer
174 Int cbWriteAf(
175 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
176 Int8 cbIdx, // decoder output circular buffer index
177 PAF_AudioFrame *pAfWrt // audio frame from which to write
178 )
179 {
180 IArg key;
181 GateMP_Handle gateHandle;
182 PAF_AST_DecOpCircBuf *pCb;
183 PAF_AudioFrame *pAfCb;
184 PAF_ChannelMask_HD streamMask;
185 Int8 i;
186 Int16 j;
188 // Get gate handle
189 gateHandle = pCbCtl->gateHandle;
190 // Enter gate
191 key = GateMP_enter(gateHandle);
193 //Log_info2("cbWriteAf:gate enter, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // FL: debug
195 // Get circular buffer base pointer
196 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
197 //Log_info1("cbWriteAf:pCb=0x%04x", (IArg)pCb); // FL: debug
199 // (***) FL: revisit
200 // Invalidate circular buffer configuration.
201 // NOTE: Probably only a subset of this information nexeds to be updated.
202 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
203 Cache_wait();
205 //Log_info1("cbWriteAf:afCb=0x%04x", (IArg)pCb->afCb); // FL: debug
206 //Log_info2("cbWriteAf:pCb->readerActiveFlag=%d, pCb->writerActiveFlag=%d", (IArg)pCb->readerActiveFlag, (IArg)pCb->writerActiveFlag); // FL: debug
208 if (pCb->readerActiveFlag == 1)
209 {
210 //
211 // Normal case, reader active.
212 // If reader not active, don't write to circular buffer or check OVRflow.
214 #if 0
215 if (pCb->cbWriteAfInit == 0)
216 {
217 // Invalidate AF circular buffer
218 Cache_inv(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
219 for (n=0; n<pCb->maxNumAfCb; n++)
220 {
221 pAfCb = &pCb->afCb[n];
222 Cache_inv(pAfCb->data.sample, ASP_DECOP_CB_MAX_NUM_PCM_CH*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
223 }
224 Cache_wait();
226 pCb->cbWriteAfInit = 1;
227 }
228 #endif
230 //Log_info2("cbWriteAf:pCb->numAfCb=%d, pCb->maxNumAfCb=%d", (IArg)pCb->readerActiveFlag, (IArg)pCb->maxNumAfCb); // FL: debug
232 // check overflow
233 if (pCb->numAfCb >= pCb->maxNumAfCb)
234 {
235 pCb->errOvrCnt++;
237 //SW_BREAKPOINT;
238 Log_info1("cbWriteAf: ERROR: overflow, numAfCb=%d", pCb->numAfCb);
240 // Write back circular buffer configuration
241 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
243 // Leave the gate
244 GateMP_leave(gateHandle, key);
246 //Log_info2("cbWriteAf:gate leave, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // FL: debug
248 return ASP_DECOP_CB_WRITE_OVERFLOW;
249 }
251 // get pointer to current audio frame in circular buffer
252 //Log_info2("cbWriteAf:afCb=0x%04x, pCb->afWrtIdx=%d", (IArg)pCb->afCb, (IArg)pCb->afWrtIdx); // FL: debug
253 pAfCb = &pCb->afCb[pCb->afWrtIdx];
254 //Log_info1("cbWriteAf:pAfCb=0x%04x", (IArg)pAfCb); // FL: debug
256 // write audio frame information updated by decoder
257 pAfCb->sampleDecode = pAfWrt->sampleDecode;
258 PAF_PROCESS_COPY(pAfCb->sampleProcess, pAfWrt->sampleProcess);
259 pAfCb->sampleRate = pAfWrt->sampleRate;
260 pAfCb->sampleCount = pAfWrt->sampleCount;
261 pAfCb->channelConfigurationRequest = pAfWrt->channelConfigurationRequest;
262 pAfCb->channelConfigurationStream = pAfWrt->channelConfigurationStream;
263 // write metadata information updated by decoder //QIN
264 pAfCb->bsMetadata_type = pAfWrt->bsMetadata_type; /* non zero if metadata is attached. */
265 pAfCb->pafBsMetadataUpdate = pAfWrt->pafBsMetadataUpdate; /* indicates whether bit-stream metadata update */
266 pAfCb->numPrivateMetadata = pAfWrt->numPrivateMetadata; /* number of valid private metadata (0 or 1 if metadata filtering enabled) */
267 pAfCb->bsMetadata_offset = pAfWrt->bsMetadata_offset; /* offset into audio frame for change in bsMetadata_type field */
268 // write PCM samples
269 streamMask = pAfWrt->fxns->channelMask(pAfWrt, pAfCb->channelConfigurationStream);
270 //Log_info1("cbWriteAf:streamMask=0x%04x", (IArg)streamMask); // FL: debug
271 //Log_info1("cbWriteAf:pCb->decOpFrameLen=%d", (IArg)pCb->decOpFrameLen); // FL: debug
272 //Log_info2("cbWriteAf:pAfCb->data.sample=0x%04x, pAfWrt->data.sample=0x%04x", (IArg)pAfCb->data.sample, (IArg)pAfWrt->data.sample); // FL: debug
273 //Log_info2("cbWriteAf:pAfCb->data.samsiz=0x%04x, pAfWrt->data.samsiz=0x%04x", (IArg)pAfCb->data.samsiz, (IArg)pAfWrt->data.samsiz); // FL: debug
274 for (i = 0; i < ASP_DECOP_CB_MAX_NUM_PCM_CH; i++)
275 {
276 if ((streamMask >> i) & 0x1)
277 {
278 for (j = 0; j < pCb->decOpFrameLen; j++)
279 {
280 pAfCb->data.sample[i][j] = pAfWrt->data.sample[i][j];
281 }
283 pAfCb->data.samsiz[i] = pAfWrt->data.samsiz[i];
284 }
285 }
287 //Log_info1("cbWriteAf:pAfCb->numPrivateMetadata=%d", (IArg)pAfCb->numPrivateMetadata); // FL: debug
288 // Write metadata to circular buffer
289 for (i = 0; i < pAfCb->numPrivateMetadata; i++) // FL: only copy numPrivateMetadata
290 {
291 pAfCb->pafPrivateMetadata[i].offset = pAfWrt->pafPrivateMetadata[i].offset;
292 pAfCb->pafPrivateMetadata[i].size = pAfWrt->pafPrivateMetadata[i].size;
293 memcpy(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].size);
294 }
296 // update audio frame write index
297 //Log_info2("cbWriteAf:pCb->afWrtIdx=%d, pCb->maxNumAfCb", (IArg)pCb->afWrtIdx, (IArg)pCb->maxNumAfCb); // FL: debug
298 pCb->afWrtIdx++;
299 if (pCb->afWrtIdx >= pCb->maxNumAfCb)
300 {
301 pCb->afWrtIdx = 0;
302 }
303 //Log_info2("cbWriteAf:pCb->afWrtIdx=%d, pCb->maxNumAfCb", (IArg)pCb->afWrtIdx, (IArg)pCb->maxNumAfCb); // FL: debug
305 // update number of audio frames in circular buffer
306 //Log_info1("cbWriteAf:pCb->numAfCb=%d", (IArg)pCb->numAfCb); // FL: debug
307 pCb->numAfCb++;
308 //Log_info1("cbWriteAf:pCb->numAfCb=%d", (IArg)pCb->numAfCb); // FL: debug
310 // (***) FL: revisit
311 // Write back circular buffer configuration
312 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
313 // write back audio frame
314 Cache_wb(pAfCb, sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
315 Cache_wb(pAfCb->data.samsiz, ASP_DECOP_CB_MAX_NUM_PCM_CH*sizeof(PAF_AudioSize), Cache_Type_ALLD, 0);
316 // write back PCM data
317 for (i = 0; i < ASP_DECOP_CB_MAX_NUM_PCM_CH; i++)
318 {
319 if ((streamMask >> i) & 0x1)
320 {
321 Cache_wb(pAfCb->data.sample[i], pCb->decOpFrameLen*sizeof(PAF_AudioData), Cache_Type_ALLD, 0);
322 }
323 }
325 // write back private metadata // QIN
326 for (i=0; i<pAfCb->numPrivateMetadata; i++) // FL: only write back numPrivateMetadata
327 {
328 //Cache_wb(&pAfCb->pafPrivateMetadata[i], sizeof(PAF_PrivateMetadata), Cache_Type_ALLD, 0); // FL: unnecessary since part of AF
329 Cache_wb(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfCb->pafPrivateMetadata[i].size, Cache_Type_ALLD, 0);
330 }
331 Cache_wait();
332 }
334 // Leave the gate
335 GateMP_leave(gateHandle, key);
337 //Log_info2("cbWriteAf:gate leave, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // FL: debug
339 return ASP_DECOP_CB_SOK;
340 }
342 // Get next audio frame to write in circular buffer
343 Int cbGetNextWriteAf(
344 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
345 Int8 cbIdx, // decoder output circular buffer index
346 PAF_AudioFrame **ppAfWrt // audio frame next to be written
347 )
348 {
349 IArg key;
350 GateMP_Handle gateHandle;
351 PAF_AST_DecOpCircBuf *pCb;
353 // Get gate handle
354 gateHandle = pCbCtl->gateHandle;
355 // Enter gate
356 key = GateMP_enter(gateHandle);
358 // Get circular buffer base pointer
359 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
361 // get pointer to current audio frame in circular buffer
362 *ppAfWrt = &pCb->afCb[pCb->afWrtIdx];
364 // update audio frame write index
365 pCb->afWrtIdx++;
366 if (pCb->afWrtIdx > pCb->maxNumAfCb)
367 {
368 pCb->afWrtIdx = 0;
369 }
371 // Leave the gate
372 GateMP_leave(gateHandle, key);
374 return ASP_DECOP_CB_SOK;
375 }