b258f747b6d0c6c6e5a2f13466da37bd288c71f5
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 #if 0 // FL: moved to common
51 // Initialize circular buffer control
52 Int cbCtlInit(
53 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
54 PAF_AST_DecOpCircBuf **pXDecOpCb // address of decoder output circular buffer base pointer
55 )
56 {
57 GateMP_Handle gateHandle;
58 Int status;
60 do {
61 status = GateMP_open(ASP_DECODE_CB_GATE_NAME, &gateHandle);
62 } while (status == GateMP_E_NOTFOUND);
63 if (status == GateMP_S_SUCCESS)
64 {
65 pCbCtl->gateHandle = gateHandle;
66 }
67 else
68 {
69 pCbCtl->gateHandle = NULL;
70 return ASP_DECOP_CB_CTL_INIT_INV_GATE;
71 }
73 pCbCtl->numCb = numCb; // init number of circular buffers
74 pCbCtl->pXDecOpCb = pXDecOpCb; // init base address of circular buffers
76 return ASP_DECOP_CB_SOK;
77 }
78 #endif
80 // debug
81 //Int8 gCbInitSourceSelCnt=0;
82 //Int8 gCbInitSourceSelThdCnt=0;
84 // Initialize circular buffer based on selected source
85 Int cbInitSourceSel(
86 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
87 Int8 cbIdx, // decoder output circular buffer index
88 Int8 sourceSelect, // source select (PCM, DDP, etc.)
89 Int16 decOpFrameLen, // decoder output frame length (PCM samples)
90 Int16 strFrameLen, // stream frame length (PCM samples)
91 Int8 resetRwFlags // whether to reset reader, writer, and drain flags
92 )
93 {
94 IArg key;
95 GateMP_Handle gateHandle;
96 PAF_AST_DecOpCircBuf *pCb;
97 PAF_AudioFrame *pAfCb;
98 PAF_AudioData *pPcmBuf;
99 UInt8 *pMetaBuf;
100 Int8 n;
101 Int8 i;
103 //gCbInitSourceSelCnt++; // debug
105 // Get gate handle
106 gateHandle = pCbCtl->gateHandle;
107 // Enter gate
108 key = GateMP_enter(gateHandle);
110 // Get circular buffer base pointer
111 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
113 // Invalidate circular buffer configuration
114 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
115 Cache_wait();
117 //Log_info1("cbInitSourceSel:afCb=0x%04x", (IArg)pCb->afCb); // debug
119 // set source select
120 pCb->sourceSel = sourceSelect;
122 // set input frame length
123 pCb->decOpFrameLen = decOpFrameLen;
125 // set output frame length
126 pCb->strFrameLen = strFrameLen;
128 //pCb->afInitialLag = 0; // default No lag
129 //pCb->afLagIdx = 0;
130 // Initialize CB primed flag
131 pCb->primedFlag = 0;
132 // Initialize delta samples
133 pCb->deltaSamps = 0;
135 // initialize circular buffer maximum number of audio frames
136 if (sourceSelect == PAF_SOURCE_PCM)
137 {
138 pCb->maxNumAfCb = ASP_DECOP_CB_MAX_NUM_AF_PCM;
140 //pCb->afInitialLag = ASP_DECOP_CB_INIT_LAG_PCM;
141 // Initialize target nominal delay
142 pCb->targetNDSamps = ASP_DECOP_CB_TARGET_ND_SAMPS_48kPCM;
144 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_PCM;
145 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_PCM;
146 pCb->pcmRdIdx = 0;
148 pCb->maxAFChanNum = ASP_DECOP_CB_MAX_NUM_PCM_CH;
149 pCb->maxAFSampCount = DEF_DEC_OP_FRAME_LEN;
151 // initialize audio frames
152 for (n=0; n<pCb->maxNumAfCb; n++)
153 {
154 pAfCb = &pCb->afCb[n];
155 pAfCb->sampleDecode = sourceSelect;
156 PAF_PROCESS_ZERO(pAfCb->sampleProcess);
157 pAfCb->sampleRate = PAF_SAMPLERATE_48000HZ;
158 pAfCb->sampleCount = decOpFrameLen;
159 pAfCb->channelConfigurationRequest.full = 0;
160 pAfCb->channelConfigurationRequest.part.sat = PAF_CC_SAT_SURROUND4;
161 pAfCb->channelConfigurationRequest.part.sub = PAF_CC_SUB_ONE;
162 pAfCb->channelConfigurationStream.full = 0;
163 pAfCb->channelConfigurationStream.part.sat = PAF_CC_SAT_SURROUND4;
164 pAfCb->channelConfigurationStream.part.sub = PAF_CC_SUB_ONE;
166 // write metadata information updated by decoder
167 pAfCb->bsMetadata_type = PAF_bsMetadata_channelData; /* non zero if metadata is attached. */
168 pAfCb->pafBsMetadataUpdate = 0; /* indicates whether bit-stream metadata update */
169 pAfCb->numPrivateMetadata = 0; /* number of valid private metadata (0 or 1 if metadata filtering enabled) */
170 pAfCb->bsMetadata_offset = 0; /* offset into audio frame for change in bsMetadata_type field */
171 }
172 }
173 else if ((sourceSelect == PAF_SOURCE_DDP) || (sourceSelect == PAF_SOURCE_AC3))
174 {
175 pCb->maxNumAfCb = ASP_DECOP_CB_MAX_NUM_AF_DDP;
177 //pCb->afInitialLag = ASP_DECOP_CB_INIT_LAG_DDP;
178 // Initialize target nominal delay
179 pCb->targetNDSamps = ASP_DECOP_CB_TARGET_ND_SAMPS_48kDDP;
181 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_DDP;
182 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_DDP;
183 pCb->pcmRdIdx = 0;
185 pCb->maxAFChanNum = ASP_DECOP_CB_MAX_NUM_PCM_CH_DDP;
186 pCb->maxAFSampCount = ASP_DECOP_CB_MAX_PCM_FRAME_LEN_48kDDP;
188 // initialize audio frames
189 for (n=0; n<pCb->maxNumAfCb; n++)
190 {
191 pAfCb = &pCb->afCb[n];
192 pAfCb->sampleDecode = sourceSelect;
193 PAF_PROCESS_ZERO(pAfCb->sampleProcess);
194 pAfCb->sampleRate = PAF_SAMPLERATE_48000HZ;
195 pAfCb->sampleCount = decOpFrameLen;
196 pAfCb->channelConfigurationRequest.full = 0;
197 pAfCb->channelConfigurationRequest.part.sat = PAF_CC_SAT_SURROUND4;
198 pAfCb->channelConfigurationRequest.part.sub = PAF_CC_SUB_ONE;
199 pAfCb->channelConfigurationStream.full = 0;
200 pAfCb->channelConfigurationStream.part.sat = PAF_CC_SAT_SURROUND4;
201 pAfCb->channelConfigurationStream.part.sub = PAF_CC_SUB_ONE;
203 // write metadata information updated by decoder
204 pAfCb->bsMetadata_type = PAF_bsMetadata_channelData; /* non zero if metadata is attached. */
205 pAfCb->pafBsMetadataUpdate = 0; /* indicates whether bit-stream metadata update */
206 pAfCb->numPrivateMetadata = 0; /* number of valid private metadata (0 or 1 if metadata filtering enabled) */
207 pAfCb->bsMetadata_offset = 0; /* offset into audio frame for change in bsMetadata_type field */
208 }
209 }
210 else if (sourceSelect == PAF_SOURCE_THD)
211 {
212 //gCbInitSourceSelThdCnt++; //debug
214 pCb->maxNumAfCb = ASP_DECOP_CB_MAX_NUM_AF_THD;
216 //pCb->afInitialLag = ASP_DECOP_CB_INIT_LAG_THD;
217 // Initialize target nominal delay
218 pCb->targetNDSamps = ASP_DECOP_CB_TARGET_ND_SAMPS_48kTHD;
220 pCb->afWrtIdx = ASP_DECOP_CB_INIT_WRTIDX_THD;
221 pCb->afRdIdx = ASP_DECOP_CB_INIT_RDIDX_THD;
222 pCb->pcmRdIdx = 0;
224 pCb->maxAFChanNum = ASP_DECOP_CB_MAX_NUM_PCM_CH_MAT;
225 pCb->maxAFSampCount = ASP_DECOP_CB_MAX_PCM_FRAME_LEN_48kMAT;
227 // initialize audio frames
228 for (n=0; n<pCb->maxNumAfCb; n++)
229 {
230 pAfCb = &pCb->afCb[n];
231 pAfCb->sampleDecode = sourceSelect;
232 PAF_PROCESS_ZERO(pAfCb->sampleProcess);
233 pAfCb->sampleRate = PAF_SAMPLERATE_48000HZ;
234 pAfCb->sampleCount = decOpFrameLen;
235 pAfCb->channelConfigurationRequest.full = 0;
236 pAfCb->channelConfigurationRequest.part.sat = PAF_CC_SAT_SURROUND4;
237 pAfCb->channelConfigurationRequest.part.sub = PAF_CC_SUB_ONE;
238 pAfCb->channelConfigurationStream.full = 0;
239 pAfCb->channelConfigurationStream.part.sat = PAF_CC_SAT_SURROUND4;
240 pAfCb->channelConfigurationStream.part.sub = PAF_CC_SUB_ONE;
242 // write metadata information updated by decoder
243 pAfCb->bsMetadata_type = PAF_bsMetadata_channelData; /* non zero if metadata is attached. */
244 pAfCb->pafBsMetadataUpdate = 0; /* indicates whether bit-stream metadata update */
245 pAfCb->numPrivateMetadata = 0; /* number of valid private metadata (0 or 1 if metadata filtering enabled) */
246 pAfCb->bsMetadata_offset = 0; /* offset into audio frame for change in bsMetadata_type field */
247 }
248 }
249 else
250 {
251 SW_BREAKPOINT;
253 // Leave the gate
254 GateMP_leave(gateHandle, key);
256 return ASP_DECOP_CB_INIT_INV_SOURCE_SEL;
257 }
259 // initialize circular buffer current number of frames
260 pCb->numAfCb = pCb->afWrtIdx - pCb->afRdIdx;
262 // initialize audio frame PCM buffers
263 pPcmBuf = pCb->pcmBuf;
264 pMetaBuf = pCb->metaBuf;
265 for (n=0; n<pCb->maxNumAfCb; n++)
266 {
267 pAfCb = &pCb->afCb[n];
268 pAfCb->data.nChannels = pCb->maxAFChanNum;
269 pAfCb->data.nSamples = decOpFrameLen;
270 for (i=0; i<pCb->maxAFChanNum; i++)
271 {
272 pAfCb->data.sample[i] = pPcmBuf;
273 memset(pAfCb->data.sample[i], 0, pCb->maxAFSampCount);
274 pPcmBuf += pCb->maxAFSampCount;
276 pAfCb->data.samsiz[i] = 0;
277 }
279 // Initialize metadata buffers
280 for (i=0; i<PAF_MAX_NUM_PRIVATE_MD; i++)
281 {
282 pAfCb->pafPrivateMetadata[i].offset = 0;
283 pAfCb->pafPrivateMetadata[i].size = 0;
284 pAfCb->pafPrivateMetadata[i].pMdBuf = pMetaBuf;
285 pMetaBuf += PAF_MAX_PRIVATE_MD_SZ;
286 }
287 }
289 // reset read/write flags
290 if (resetRwFlags)
291 {
292 pCb->writerActiveFlag = 0;
293 pCb->readerActiveFlag = 0;
294 pCb->drainFlag = 0;
295 }
297 // reset stats
298 pCb->readAfWriterInactiveCnt = 0;
299 pCb->wrtAfReaderInactiveCnt = 0;
300 pCb->wrtAfZeroSampsCnt = 0;
301 pCb->errUndCnt = 0;
302 pCb->errOvrCnt = 0;
304 // Write back circular buffer configuration
305 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
306 // Write back AF circular buffer
307 Cache_wb(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
308 // Write back PCM data
309 for (n=0; n<pCb->maxNumAfCb; n++)
310 {
311 pAfCb = &pCb->afCb[n];
312 Cache_wb(pAfCb->data.samsiz, pCb->maxAFChanNum*sizeof(PAF_AudioSize), Cache_Type_ALLD, 0);
313 Cache_wb(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
314 for (i=0; i<pCb->maxAFChanNum; i++)
315 {
316 Cache_wb(pAfCb->data.sample[i], pCb->maxAFSampCount*sizeof(PAF_AudioData), Cache_Type_ALLD, 0);
317 }
318 }
319 Cache_wait();
321 // Leave the gate
322 GateMP_leave(gateHandle, key);
324 return ASP_DECOP_CB_SOK;
325 }
327 //Int8 gCbWriteStartCnt=0; // debug
329 // Start writes to circular buffer
330 Int cbWriteStart(
331 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
332 Int8 cbIdx // decoder output circular buffer index
333 )
334 {
335 IArg key;
336 GateMP_Handle gateHandle;
337 PAF_AST_DecOpCircBuf *pCb;
338 PAF_AudioFrame *pAfCb;
339 Int8 n;
340 //Int8 i;
342 //gCbWriteStartCnt++; // debug
344 // Get gate handle
345 gateHandle = pCbCtl->gateHandle;
346 // Enter gate
347 key = GateMP_enter(gateHandle);
349 // Get circular buffer base pointer
350 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
352 // Invalidate circular buffer configuration.
353 // NOTE: Probably only a subset of this information needs to be updated.
354 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
355 Cache_wait();
357 //Log_info1("cbWriteStart:afCb=0x%04x", (IArg)pCb->afCb); // debug
359 // Invalidate AF circular buffer
360 Cache_inv(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
361 for (n=0; n<pCb->maxNumAfCb; n++)
362 {
363 pAfCb = &pCb->afCb[n];
364 Cache_inv(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
365 }
366 Cache_wait();
368 // update flags
369 pCb->writerActiveFlag = 1;
370 pCb->drainFlag = 0;
371 //pCb->afLagIdx = 0;
373 // Write back circular buffer configuration
374 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
375 Cache_wait();
377 // Leave the gate
378 GateMP_leave(gateHandle, key);
380 return ASP_DECOP_CB_SOK;
381 };
383 // Stop writes to circular buffer
384 Int cbWriteStop(
385 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
386 Int8 cbIdx // decoder output circular buffer index
387 )
388 {
389 IArg key;
390 GateMP_Handle gateHandle;
391 PAF_AST_DecOpCircBuf *pCb;
393 // Get gate handle
394 gateHandle = pCbCtl->gateHandle;
395 // Enter gate
396 key = GateMP_enter(gateHandle);
398 // Get circular buffer base pointer
399 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
401 // Invalidate circular buffer configuration
402 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
403 Cache_wait();
405 //Log_info1("cbWriteStop:afCb=0x%04x", (IArg)pCb->afCb); // debug
407 // update flags
408 pCb->writerActiveFlag = 0;
409 pCb->drainFlag = 1;
411 // Write back circular buffer configuration
412 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
413 Cache_wait();
415 // Leave the gate
416 GateMP_leave(gateHandle, key);
418 return ASP_DECOP_CB_SOK;
419 }
421 // debug
422 //Int16 gSampleCountBuf[10];
423 //Int16 gCalcDeltaSampsBuf[10];
424 //Int8 gPrimedFlagCnt=0;
426 // (***) FL: revisit
427 // Write audio frame to circular buffer
428 Int cbWriteAf(
429 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
430 Int8 cbIdx, // decoder output circular buffer index
431 PAF_AudioFrame *pAfWrt // audio frame from which to write
432 )
433 {
434 IArg key;
435 GateMP_Handle gateHandle;
436 PAF_AST_DecOpCircBuf *pCb;
437 PAF_AudioFrame *pAfCb;
438 PAF_ChannelMask_HD streamMask;
439 Int8 i;
440 Int16 j;
441 PAF_AudioData *pPcmBuf;UInt8 *pMetaBuf; int nextWrtIdx;PAF_AudioFrame *pAfCbNextAf;
443 // Get gate handle
444 gateHandle = pCbCtl->gateHandle;
445 // Enter gate
446 key = GateMP_enter(gateHandle);
448 //Log_info2("cbWriteAf:gate enter, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // debug
450 // Get circular buffer base pointer
451 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
452 //Log_info1("cbWriteAf:pCb=0x%04x", (IArg)pCb); // debug
454 // Invalidate circular buffer configuration.
455 // NOTE: Probably only a subset of this information needs to be updated.
456 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
457 Cache_wait();
459 //Log_info1("cbWriteAf:afCb=0x%04x", (IArg)pCb->afCb); // debug
460 //Log_info2("cbWriteAf:pCb->readerActiveFlag=%d, pCb->writerActiveFlag=%d", (IArg)pCb->readerActiveFlag, (IArg)pCb->writerActiveFlag); // debug
462 //if (pCb->readerActiveFlag == 1)
463 //{
464 // //
465 // // Normal case, reader active.
466 // //
468 if (pAfWrt->sampleCount != 0)
469 {
470 //Log_info2("cbWriteAf:pCb->numAfCb=%d, pCb->maxNumAfCb=%d", (IArg)pCb->readerActiveFlag, (IArg)pCb->maxNumAfCb); // debug
472 // check overflow
473 //while (pCb->numAfCb >= pCb->maxNumAfCb); // debug
474 if (pCb->numAfCb >= pCb->maxNumAfCb)
475 {
476 pCb->errOvrCnt++;
478 //SW_BREAKPOINT;
479 Log_info1("cbWriteAf: ERROR: overflow, numAfCb=%d", pCb->numAfCb);
481 // Write back circular buffer configuration
482 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
483 Cache_wait();
485 // Leave the gate
486 GateMP_leave(gateHandle, key);
488 //Log_info2("cbWriteAf:gate leave, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // debug
490 return ASP_DECOP_CB_WRITE_OVERFLOW;
491 }
493 pAfCb = &pCb->afCb[pCb->afWrtIdx];
494 pPcmBuf = pAfCb->data.sample[0];
495 pMetaBuf = pAfCb->pafPrivateMetadata[0].pMdBuf;
496 if((pPcmBuf + (pAfWrt->sampleCount * pCb->maxAFChanNum )) > (pCb->pcmBufEnd))
497 {
498 pPcmBuf = pCb->pcmBuf;
499 }
501 for (i=0; i<pCb->maxAFChanNum; i++)
502 {
503 pAfCb->data.sample[i] = pPcmBuf;
504 pPcmBuf += pAfWrt->sampleCount;
505 pAfCb->data.samsiz[i] = 0;
506 }
507 Cache_inv(pAfCb->data.sample, pCb->maxAFChanNum*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0); // FL: this is write back and invalidate??
508 Cache_wait();
510 //for (i=0; i<pCb->maxAFChanNum; i++){
511 //}
512 for (i=0; i<PAF_MAX_NUM_PRIVATE_MD; i++)
513 {
514 pAfCb->pafPrivateMetadata[i].offset = 0;
515 pAfCb->pafPrivateMetadata[i].size = 0;
516 pAfCb->pafPrivateMetadata[i].pMdBuf = pMetaBuf;
517 pMetaBuf += PAF_MAX_PRIVATE_MD_SZ;
518 }
520 nextWrtIdx = 0;
521 if ((pCb->afWrtIdx +1) >= pCb->maxNumAfCb)
522 {
523 //Log_info0("cbWriteAf: AF Wrap around **** ");
524 // next audio frame will be audio frame 0
525 nextWrtIdx = 0;
526 }
527 else
528 {
529 // next audio frame will be current audio frame + 1
530 nextWrtIdx = pCb->afWrtIdx + 1;
531 }
533 pAfCbNextAf = &pCb->afCb[nextWrtIdx]; // +1 or last AF if overflow
534 pAfCbNextAf->data.sample[0] = &pAfCb->data.sample[pCb->maxAFChanNum - 1][pAfWrt->sampleCount];// pAfCb->data.sample[15] + (pAfCb->sampleCount * sizeof(PAF_AudioData));
536 // write audio frame information updated by decoder
537 pAfCb->sampleDecode = pAfWrt->sampleDecode;
538 PAF_PROCESS_COPY(pAfCb->sampleProcess, pAfWrt->sampleProcess);
539 pAfCb->sampleRate = pAfWrt->sampleRate;
540 pAfCb->sampleCount = pAfWrt->sampleCount;
541 pAfCb->channelConfigurationRequest = pAfWrt->channelConfigurationRequest;
542 pAfCb->channelConfigurationStream = pAfWrt->channelConfigurationStream;
543 // write metadata information updated by decoder
544 pAfCb->bsMetadata_type = pAfWrt->bsMetadata_type; /* non zero if metadata is attached. */
545 pAfCb->pafBsMetadataUpdate = pAfWrt->pafBsMetadataUpdate; /* indicates whether bit-stream metadata update */
546 pAfCb->numPrivateMetadata = pAfWrt->numPrivateMetadata; /* number of valid private metadata (0 or 1 if metadata filtering enabled) */
547 pAfCb->bsMetadata_offset = pAfWrt->bsMetadata_offset; /* offset into audio frame for change in bsMetadata_type field */
548 // write PCM samples
549 streamMask = pAfWrt->fxns->channelMask(pAfWrt, pAfCb->channelConfigurationStream);
550 for (i = 0; i < pCb->maxAFChanNum; i++)
551 {
552 if ((streamMask >> i) & 0x1)
553 {
554 for (j = 0; j < pAfWrt->sampleCount; j++)
555 {
556 pAfCb->data.sample[i][j] = pAfWrt->data.sample[i][j];
557 }
559 pAfCb->data.samsiz[i] = pAfWrt->data.samsiz[i];
560 }
561 }
563 #ifdef CB_RW_OP_CAP_PP // debug
564 if (pCb->cb_opCnt < CB_OP_COUNT_MAX)
565 {
566 if ((pCb->cb_samples_op != NULL) && (pCb->cb_op_owner != NULL))
567 {
568 // log sample count
569 pCb->cb_samples_op[pCb->cb_opCnt] = pAfWrt->sampleCount;
570 pCb->cb_op_owner[pCb->cb_opCnt] = CB_OP_W;
571 // log idxs
572 pCb->cb_afRdIdx[pCb->cb_opCnt] = pCb->afRdIdx;
573 pCb->cb_afWrtIdx[pCb->cb_opCnt] = pCb->afWrtIdx;
574 pCb->cb_numAfCb[pCb->cb_opCnt] = pCb->numAfCb; // numAfCb might not be pointing to this instance
575 pCb->cb_opCnt++;
576 }
577 }
578 #endif
580 // prepare metadata buffer pointers according to the metadata and buffer sizes
581 for (i=0; i < pAfWrt->numPrivateMetadata; i++)
582 {
583 UInt8 *nextMdBuf;
584 if(i == 0)
585 nextMdBuf = (pAfCb->pafPrivateMetadata[0].pMdBuf + pAfWrt->pafPrivateMetadata[0].size);
586 else
587 nextMdBuf = (pAfCb->pafPrivateMetadata[i-1].pMdBuf + pAfWrt->pafPrivateMetadata[i-1].size);
588 if(nextMdBuf >= pCb->metaBufEnd) // metadata buffer overflow
589 {
590 pAfCb->pafPrivateMetadata[i].pMdBuf = pCb->metaBuf;
591 }
592 else if(i != 0)
593 {
594 pAfCb->pafPrivateMetadata[i].pMdBuf = nextMdBuf;
595 }
596 Cache_inv(pAfCb->pafPrivateMetadata[i].pMdBuf, sizeof(UInt8 *), Cache_Type_ALLD, 0);
597 }
599 // Write metadata to circular buffer
600 for (i = 0; i < pAfWrt->numPrivateMetadata; i++) // only copy numPrivateMetadata
601 {
602 pAfCb->pafPrivateMetadata[i].offset = pAfWrt->pafPrivateMetadata[i].offset;
603 pAfCb->pafPrivateMetadata[i].size = pAfWrt->pafPrivateMetadata[i].size;
604 memcpy(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].size);
605 }
607 Cache_inv(pAfCb->pafPrivateMetadata, pAfWrt->numPrivateMetadata*sizeof(PAF_PrivateMetadata *), Cache_Type_ALLD, 0); // FL: this is write back and invalidate??
608 Cache_wait();
609 for (i=0; i<pAfCb->numPrivateMetadata; i++) // only write back numPrivateMetadata
610 {
611 //Log_info4("cbWriteAf: AF: %d nummd: %d offset: %d size: %d ", pCb->afWrtIdx, pAfCb->numPrivateMetadata, pAfCb->pafPrivateMetadata[i].offset, pAfCb->pafPrivateMetadata[i].size);
612 Cache_wb(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfCb->pafPrivateMetadata[i].size, Cache_Type_ALLD, 0);
613 }
614 // update audio frame write index
615 pCb->afWrtIdx++;
616 if (pCb->afWrtIdx >= pCb->maxNumAfCb)
617 {
618 pCb->afWrtIdx = 0;
619 }
621 pCb->afCb[pCb->afWrtIdx].data.sample[0] = &pAfCb->data.sample[pCb->maxAFChanNum - 1][pAfWrt->sampleCount];
622 if(pAfWrt->numPrivateMetadata > 0)
623 {
624 pCb->afCb[pCb->afWrtIdx].pafPrivateMetadata[0].pMdBuf = pAfCb->pafPrivateMetadata[pAfWrt->numPrivateMetadata - 1].pMdBuf + pAfWrt->pafPrivateMetadata[pAfWrt->numPrivateMetadata - 1].size;
625 }
626 else
627 {
628 pCb->afCb[pCb->afWrtIdx].pafPrivateMetadata[0].pMdBuf = pAfCb->pafPrivateMetadata[0].pMdBuf;
629 Cache_wb(pCb->afCb , ASP_DECOP_CB_MAX_NUM_PCM_FRAMES*sizeof(PAF_AudioFrame *), Cache_Type_ALLD, 0);
630 Cache_wait();
631 }
632 Cache_inv(pCb->afCb[pCb->afWrtIdx].pafPrivateMetadata[0].pMdBuf, sizeof(UInt8 *), Cache_Type_ALLD, 0);
633 Cache_wait();
634 // update number of audio frames in circular buffer
635 pCb->numAfCb++;
637 // Update CB Lag index
638 //if (pCb->afLagIdx < pCb->afInitialLag)
639 //{
640 // pCb->afLagIdx += 1;
641 //}
643 // Update CB primed flag
644 // calculate number of delta samples before
645 if (pCb->primedFlag == 0)
646 {
647 pCb->primedFlag = 1;
649 // Calculate number of output frames to block reader.
650 // This is sample count reader waits before allowed to actually read samples from the CB.
651 pCb->deltaSamps = (pCb->targetNDSamps - pAfWrt->sampleCount + (pCb->strFrameLen-1)) / pCb->strFrameLen * pCb->strFrameLen;
653 // debug
654 //gSampleCountBuf[gPrimedFlagCnt] = pAfWrt->sampleCount;
655 //gCalcDeltaSampsBuf[gPrimedFlagCnt] = pCb->deltaSamps;
656 //if (gPrimedFlagCnt < 10)
657 // gPrimedFlagCnt++;
658 }
660 // Write back circular buffer configuration
661 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
662 // write back audio frame
663 Cache_wb(pAfCb, sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
664 Cache_wb(pAfCb->data.samsiz, pCb->maxAFChanNum*sizeof(PAF_AudioSize), Cache_Type_ALLD, 0);
665 Cache_wb(pAfCb->pafPrivateMetadata, pAfWrt->numPrivateMetadata*sizeof(PAF_PrivateMetadata *), Cache_Type_ALLD, 0);
666 Cache_wait();
667 // write back PCM data
668 for (i = 0; i < pCb->maxAFChanNum; i++)
669 {
670 if ((streamMask >> i) & 0x1)
671 {
672 Cache_wb(pAfCb->data.sample[i], pAfWrt->sampleCount * sizeof(PAF_AudioData), Cache_Type_ALLD, 0);
673 }
674 }
675 Cache_wait();
677 {
678 static Uint8 toggleState = 0;
679 if (toggleState == 0)
680 GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_99);
681 else
682 GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_99);
683 toggleState = ~(toggleState);
684 }
685 Log_info3("wrote %d samples into AF %d sourceSel: %d", pAfCb->sampleCount, pCb->afWrtIdx, pCb->sourceSel);
686 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);
687 }
688 else
689 {
690 //
691 // Skip write in case of 0 sample count
692 //
694 // writing audio frame w/ zero samples
695 // update stat
696 pCb->wrtAfZeroSampsCnt++;
698 // Write back circular buffer configuration
699 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
700 Cache_wait();
701 }
702 //}
703 //else if (pCb->readerActiveFlag == 0)
704 if (pCb->readerActiveFlag == 0)
705 {
706 //
707 // Reader inactive, don't write to circular buffer or check OVRflow.
708 //
710 // writing AF w/ inactive reader
711 // update stat
712 pCb->wrtAfReaderInactiveCnt++;
714 // Write back circular buffer configuration
715 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
716 Cache_wait();
717 }
719 // Leave the gate
720 GateMP_leave(gateHandle, key);
722 //Log_info2("cbWriteAf:gate leave, gateHandle=0x%04x, key=%d", (IArg)gateHandle, (IArg)key); // debug
724 return ASP_DECOP_CB_SOK;
725 }
727 #if 0
728 // Get next audio frame to write in circular buffer
729 Int cbGetNextWriteAf(
730 PAF_AST_DecOpCircBufCtl *pCbCtl, // decoder output circular buffer control
731 Int8 cbIdx, // decoder output circular buffer index
732 PAF_AudioFrame **ppAfWrt // audio frame next to be written
733 )
734 {
735 IArg key;
736 GateMP_Handle gateHandle;
737 PAF_AST_DecOpCircBuf *pCb;
739 // Get gate handle
740 gateHandle = pCbCtl->gateHandle;
741 // Enter gate
742 key = GateMP_enter(gateHandle);
744 // Get circular buffer base pointer
745 pCb = &((*pCbCtl->pXDecOpCb)[cbIdx]);
747 // get pointer to current audio frame in circular buffer
748 *ppAfWrt = &pCb->afCb[pCb->afWrtIdx];
750 // update audio frame write index
751 pCb->afWrtIdx++;
752 if (pCb->afWrtIdx > pCb->maxNumAfCb)
753 {
754 pCb->afWrtIdx = 0;
755 }
757 // Leave the gate
758 GateMP_leave(gateHandle, key);
760 return ASP_DECOP_CB_SOK;
761 }
762 #endif