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;
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 }
72 pCbCtl->pXDecOpCb = pXDecOpCb;
74 return ASP_DECOP_CB_SOK;
76 }
78 // Start writes to circular buffer
79 Int cbWriteStart(
80 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
81 Int8 cbIdx // decoder output circular buffer index
82 )
83 {
84 IArg key;
85 GateMP_Handle gateHandle;
86 PAF_AST_DecOpCircBuf *pCb;
87 PAF_AudioFrame *pAfCb;
88 Int8 n;
89 //Int8 i;
91 // Get gate handle
92 gateHandle = pCbCtl->gateHandle;
93 // Enter gate
94 key = GateMP_enter(gateHandle);
96 // Get circular buffer base pointer
97 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
99 // Invalidate circular buffer configuration.
100 // NOTE: Probably only a subset of this information needs to be updated.
101 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
102 Cache_wait();
104 //Log_info1("cbWriteStart:afCb=0x%04x", (IArg)pCb->afCb); // debug
106 // Invalidate AF circular buffer
107 Cache_inv(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
108 for (n=0; n<pCb->maxNumAfCb; n++)
109 {
110 pAfCb = &pCb->afCb[n];
111 Cache_inv(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
112 }
113 Cache_wait();
115 // update flags
116 pCb->writerActiveFlag = 1;
117 pCb->emptyFlag = 0;
118 //pCb->afLagIdx = 0;
120 // Write back circular buffer configuration
121 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
122 Cache_wait();
124 // Leave the gate
125 GateMP_leave(gateHandle, key);
127 return ASP_DECOP_CB_SOK;
128 };
130 // Stop writes to circular buffer
131 Int cbWriteStop(
132 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
133 Int8 cbIdx // decoder output circular buffer index
134 )
135 {
136 IArg key;
137 GateMP_Handle gateHandle;
138 PAF_AST_DecOpCircBuf *pCb;
140 // Get gate handle
141 gateHandle = pCbCtl->gateHandle;
142 // Enter gate
143 key = GateMP_enter(gateHandle);
145 // Get circular buffer base pointer
146 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
148 // Invalidate circular buffer configuration
149 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
150 Cache_wait();
152 //Log_info1("cbWriteStop:afCb=0x%04x", (IArg)pCb->afCb); // debug
154 // update flags
155 pCb->writerActiveFlag = 0;
156 pCb->emptyFlag = 1;
158 // Write back circular buffer configuration
159 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
160 Cache_wait();
162 // Leave the gate
163 GateMP_leave(gateHandle, key);
165 return ASP_DECOP_CB_SOK;
166 }
168 // Write audio frame to circular buffer
169 Int cbWriteAf(
170 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
171 Int8 cbIdx, // decoder output circular buffer index
172 PAF_AudioFrame *pAfWrt // audio frame from which to write
173 )
174 {
175 IArg key;
176 GateMP_Handle gateHandle;
177 PAF_AST_DecOpCircBuf *pCb;
178 PAF_AudioFrame *pAfCb;
179 PAF_ChannelMask_HD streamMask;
180 Int8 i;
181 Int16 j;
182 PAF_AudioData *pPcmBuf;UInt8 *pMetaBuf; int nextWrtIdx;PAF_AudioFrame *pAfCbNextAf;
184 // Get gate handle
185 gateHandle = pCbCtl->gateHandle;
186 // Enter gate
187 key = GateMP_enter(gateHandle);
189 //Log_info2("cbWriteAf:gate enter, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // debug
191 // Get circular buffer base pointer
192 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
193 //Log_info1("cbWriteAf:pCb=0x%04x", (IArg)pCb); // debug
195 // Invalidate circular buffer configuration.
196 // NOTE: Probably only a subset of this information needs to be updated.
197 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
198 Cache_wait();
200 //Log_info1("cbWriteAf:afCb=0x%04x", (IArg)pCb->afCb); // debug
201 //Log_info2("cbWriteAf:pCb->readerActiveFlag=%d, pCb->writerActiveFlag=%d", (IArg)pCb->readerActiveFlag, (IArg)pCb->writerActiveFlag); // debug
203 if ((pCb->readerActiveFlag == 1) && (pAfWrt->sampleCount)) //QIN ?
204 {
205 //
206 // Normal case, reader active.
207 // If reader not active, don't write to circular buffer or check OVRflow.
209 #if 0
210 if (pCb->cbWriteAfInit == 0)
211 {
212 // Invalidate AF circular buffer
213 Cache_inv(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
214 for (n=0; n<pCb->maxNumAfCb; n++)
215 {
216 pAfCb = &pCb->afCb[n];
217 Cache_inv(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
218 }
219 Cache_wait();
221 pCb->cbWriteAfInit = 1;
222 }
223 #endif
225 //Log_info2("cbWriteAf:pCb->numAfCb=%d, pCb->maxNumAfCb=%d", (IArg)pCb->readerActiveFlag, (IArg)pCb->maxNumAfCb); // FL: debug
227 // check overflow
228 //while (pCb->numAfCb >= pCb->maxNumAfCb); // debug
229 if (pCb->numAfCb >= pCb->maxNumAfCb)
230 {
231 pCb->errOvrCnt++;
233 //SW_BREAKPOINT;
234 Log_info1("cbWriteAf: ERROR: overflow, numAfCb=%d", pCb->numAfCb);
236 // Write back circular buffer configuration
237 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
239 // Leave the gate
240 GateMP_leave(gateHandle, key);
242 //Log_info2("cbWriteAf:gate leave, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // FL: debug
244 return ASP_DECOP_CB_WRITE_OVERFLOW;
245 }
247 pAfCb = &pCb->afCb[pCb->afWrtIdx];
248 pPcmBuf = pAfCb->data.sample[0];
249 pMetaBuf = pAfCb->pafPrivateMetadata[0].pMdBuf;
250 if((pPcmBuf + (pAfWrt->sampleCount * pCb->maxAFChanNum )) > (pCb->pcmBufEnd))
251 {
252 pPcmBuf = pCb->pcmBuf;
253 }
255 for (i=0; i<pCb->maxAFChanNum; i++)
256 {
257 pAfCb->data.sample[i] = pPcmBuf;
258 pPcmBuf += pAfWrt->sampleCount;
259 pAfCb->data.samsiz[i] = 0;
260 }
261 Cache_inv(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0); // FL: this is write back and invalidate??
262 Cache_wait();
264 for (i=0; i<pCb->maxAFChanNum; i++){
265 }
266 for (i=0; i<PAF_MAX_NUM_PRIVATE_MD; i++)
267 {
268 pAfCb->pafPrivateMetadata[i].offset = 0;
269 pAfCb->pafPrivateMetadata[i].size = 0;
270 pAfCb->pafPrivateMetadata[i].pMdBuf = pMetaBuf;
271 pMetaBuf += PAF_MAX_PRIVATE_MD_SZ;
272 }
274 nextWrtIdx = 0;
275 if ((pCb->afWrtIdx +1) >= pCb->maxNumAfCb)
276 {
277 //Log_info0("cbWriteAf: AF Wrap around **** ");
278 // next audio frame will be audio frame 0
279 nextWrtIdx = 0;
280 }else{
281 // next audio frame will be current audio frame + 1
282 nextWrtIdx = pCb->afWrtIdx + 1;
283 }
285 pAfCbNextAf = &pCb->afCb[nextWrtIdx]; // +1 or last AF if overflow
286 pAfCbNextAf->data.sample[0] = &pAfCb->data.sample[pCb->maxAFChanNum - 1][pAfWrt->sampleCount];// pAfCb->data.sample[15] + (pAfCb->sampleCount * sizeof(PAF_AudioData));
288 // write audio frame information updated by decoder
289 pAfCb->sampleDecode = pAfWrt->sampleDecode;
290 PAF_PROCESS_COPY(pAfCb->sampleProcess, pAfWrt->sampleProcess);
291 pAfCb->sampleRate = pAfWrt->sampleRate;
292 pAfCb->sampleCount = pAfWrt->sampleCount;
293 pAfCb->channelConfigurationRequest = pAfWrt->channelConfigurationRequest;
294 pAfCb->channelConfigurationStream = pAfWrt->channelConfigurationStream;
295 // write metadata information updated by decoder
296 pAfCb->bsMetadata_type = pAfWrt->bsMetadata_type; /* non zero if metadata is attached. */
297 pAfCb->pafBsMetadataUpdate = pAfWrt->pafBsMetadataUpdate; /* indicates whether bit-stream metadata update */
298 pAfCb->numPrivateMetadata = pAfWrt->numPrivateMetadata; /* number of valid private metadata (0 or 1 if metadata filtering enabled) */
299 pAfCb->bsMetadata_offset = pAfWrt->bsMetadata_offset; /* offset into audio frame for change in bsMetadata_type field */
300 // write PCM samples
301 streamMask = pAfWrt->fxns->channelMask(pAfWrt, pAfCb->channelConfigurationStream);
302 for (i = 0; i < pCb->maxAFChanNum; i++)
303 {
304 if ((streamMask >> i) & 0x1)
305 {
306 for (j = 0; j < pAfWrt->sampleCount; j++)
307 {
308 pAfCb->data.sample[i][j] = pAfWrt->data.sample[i][j];
309 }
311 pAfCb->data.samsiz[i] = pAfWrt->data.samsiz[i];
312 }
313 }
315 #ifdef CB_RW_OP_CAP_PP // debug
316 if (pCb->cb_opCnt < CB_OP_COUNT_MAX)
317 {
318 if ((pCb->cb_samples_op != NULL) && (pCb->cb_op_owner != NULL))
319 {
320 // log sample count
321 pCb->cb_samples_op[pCb->cb_opCnt] = pAfWrt->sampleCount;
322 pCb->cb_op_owner[pCb->cb_opCnt] = CB_OP_W;
323 // log idxs
324 pCb->cb_afRdIdx[pCb->cb_opCnt] = pCb->afRdIdx;
325 pCb->cb_afWrtIdx[pCb->cb_opCnt] = pCb->afWrtIdx;
326 pCb->cb_numAfCb[pCb->cb_opCnt] = pCb->numAfCb; // numAfCb might not be pointing to this instance
327 pCb->cb_opCnt++;
328 }
329 }
330 #endif
332 // prepare metadata buffer pointers according to the metadata and buffer sizes
333 for (i=0; i < pAfWrt->numPrivateMetadata; i++)
334 {
335 UInt8 *nextMdBuf;
336 if(i == 0)
337 nextMdBuf = (pAfCb->pafPrivateMetadata[0].pMdBuf + pAfWrt->pafPrivateMetadata[0].size);
338 else
339 nextMdBuf = (pAfCb->pafPrivateMetadata[i-1].pMdBuf + pAfWrt->pafPrivateMetadata[i-1].size);
340 if(nextMdBuf >= pCb->metaBufEnd) // metadata buffer overflow
341 {
342 pAfCb->pafPrivateMetadata[i].pMdBuf = pCb->metaBuf;
343 }
344 else if(i != 0)
345 {
346 pAfCb->pafPrivateMetadata[i].pMdBuf = nextMdBuf;
347 }
348 Cache_inv(pAfCb->pafPrivateMetadata[i].pMdBuf, sizeof(UInt8 *), Cache_Type_ALLD, 0);
349 }
351 // Write metadata to circular buffer
352 for (i = 0; i < pAfWrt->numPrivateMetadata; i++) // only copy numPrivateMetadata
353 {
354 pAfCb->pafPrivateMetadata[i].offset = pAfWrt->pafPrivateMetadata[i].offset;
355 pAfCb->pafPrivateMetadata[i].size = pAfWrt->pafPrivateMetadata[i].size;
356 memcpy(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].size);
357 }
359 Cache_inv(pAfCb->pafPrivateMetadata, pAfWrt->numPrivateMetadata*sizeof(PAF_PrivateMetadata *), Cache_Type_ALLD, 0); // FL: this is write back and invalidate??
360 Cache_wait();
361 for (i=0; i<pAfCb->numPrivateMetadata; i++) // only write back numPrivateMetadata
362 {
363 //Log_info4("cbWriteAf: AF: %d nummd: %d offset: %d size: %d ", pCb->afWrtIdx, pAfCb->numPrivateMetadata, pAfCb->pafPrivateMetadata[i].offset, pAfCb->pafPrivateMetadata[i].size);
364 Cache_wb(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfCb->pafPrivateMetadata[i].size, Cache_Type_ALLD, 0);
365 }
366 // update audio frame write index
367 pCb->afWrtIdx++;
368 if (pCb->afWrtIdx >= pCb->maxNumAfCb)
369 {
370 pCb->afWrtIdx = 0;
371 }
373 pCb->afCb[pCb->afWrtIdx].data.sample[0] = &pAfCb->data.sample[pCb->maxAFChanNum - 1][pAfWrt->sampleCount];
374 if(pAfWrt->numPrivateMetadata > 0)
375 {
376 pCb->afCb[pCb->afWrtIdx].pafPrivateMetadata[0].pMdBuf = pAfCb->pafPrivateMetadata[pAfWrt->numPrivateMetadata - 1].pMdBuf + pAfWrt->pafPrivateMetadata[pAfWrt->numPrivateMetadata - 1].size;
377 }
378 else
379 {
380 pCb->afCb[pCb->afWrtIdx].pafPrivateMetadata[0].pMdBuf = pAfCb->pafPrivateMetadata[0].pMdBuf;
381 Cache_wb(pCb->afCb , ASP_DECOP_CB_MAX_NUM_PCM_FRAMES*sizeof(PAF_AudioFrame *), Cache_Type_ALLD, 0);
382 Cache_wait();
383 }
384 Cache_inv(pCb->afCb[pCb->afWrtIdx].pafPrivateMetadata[0].pMdBuf, sizeof(UInt8 *), Cache_Type_ALLD, 0);
385 Cache_wait();
386 // update number of audio frames in circular buffer
387 pCb->numAfCb++;
389 // Update CB Lag index
390 //if (pCb->afLagIdx < pCb->afInitialLag)
391 //{
392 // pCb->afLagIdx += 1;
393 //}
395 // Update CB primed flag
396 // calculate number of delta samples before
397 if (pCb->primedFlag == 0)
398 {
399 pCb->primedFlag = 1;
401 // Calculate number of output frames to block reader.
402 // This is sample count reader waits before allowed to actually read samples from the CB.
403 pCb->deltaSamps = (pCb->targetNDSamps - pAfWrt->sampleCount + (pCb->strFrameLen-1)) / pCb->strFrameLen * pCb->strFrameLen;
404 }
406 // (***) FL: revisit
407 // Write back circular buffer configuration
408 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
409 // write back audio frame
410 Cache_wb(pAfCb, sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
411 Cache_wb(pAfCb->data.samsiz, pCb->maxAFChanNum*sizeof(PAF_AudioSize), Cache_Type_ALLD, 0);
412 Cache_wb(pAfCb->pafPrivateMetadata, pAfWrt->numPrivateMetadata*sizeof(PAF_PrivateMetadata *), Cache_Type_ALLD, 0);
413 Cache_wait();
414 // write back PCM data
415 for (i = 0; i < pCb->maxAFChanNum; i++)
416 {
417 if ((streamMask >> i) & 0x1)
418 {
419 Cache_wb(pAfCb->data.sample[i], pAfWrt->sampleCount * sizeof(PAF_AudioData), Cache_Type_ALLD, 0);
420 }
421 }
422 Cache_wait();
424 {
425 static Uint8 toggleState = 0;
426 if (toggleState == 0)
427 GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_99);
428 else
429 GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_99);
430 toggleState = ~(toggleState);
431 }
432 Log_info3("wrote %d samples into AF %d sourceSel: %d", pAfCb->sampleCount, pCb->afWrtIdx, pCb->sourceSel);
433 // write back private metadata // QIN
434 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);
435 }
437 // Leave the gate
438 GateMP_leave(gateHandle, key);
440 //Log_info2("cbWriteAf:gate leave, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // FL: debug
442 return ASP_DECOP_CB_SOK;
443 }
445 // Get next audio frame to write in circular buffer
446 Int cbGetNextWriteAf(
447 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
448 Int8 cbIdx, // decoder output circular buffer index
449 PAF_AudioFrame **ppAfWrt // audio frame next to be written
450 )
451 {
452 IArg key;
453 GateMP_Handle gateHandle;
454 PAF_AST_DecOpCircBuf *pCb;
456 // Get gate handle
457 gateHandle = pCbCtl->gateHandle;
458 // Enter gate
459 key = GateMP_enter(gateHandle);
461 // Get circular buffer base pointer
462 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
464 // get pointer to current audio frame in circular buffer
465 *ppAfWrt = &pCb->afCb[pCb->afWrtIdx];
467 // update audio frame write index
468 pCb->afWrtIdx++;
469 if (pCb->afWrtIdx > pCb->maxNumAfCb)
470 {
471 pCb->afWrtIdx = 0;
472 }
474 // Leave the gate
475 GateMP_leave(gateHandle, key);
477 return ASP_DECOP_CB_SOK;
478 }