PASDK-516:First version of ASOT refactoring
[processor-sdk/performance-audio-sr.git] / pasdk / test_dsp / framework / audioStreamInpDec.c
1 /*
2 Copyright (c) 2016, Texas Instruments Incorporated - http://www.ti.com/
3 All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the
15 * distribution.
16 *
17 * Neither the name of Texas Instruments Incorporated nor the names of
18 * its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
35 /*
36  *  ======== audioStreamInpDec.c ========
37  */
38 #include <ti/sysbios/hal/Cache.h>
39 #include <xdc/runtime/Log.h>
41 #include "procsdk_audio_typ.h"
42 #include "audioStreamInpProc.h"
43 #include "audioStreamProc_common.h"
44 #include "aspMsg_common.h"
45 #include "aspMsg_master.h"
46 #include "asperr.h"
47 #include "common.h"
48 #include "as1-f2.h"
50 #include "ioConfig.h"    //TODO: remove this header
51 #include "ioBuff.h"
52 #include "ioPhy.h"
53 #include "ioData.h"
55 enum {
56     INFO1,
57     INFO2,
58     DECODE,
59     QUIT
60 };
62 static Int decDecodeInit(const PAF_ASIT_Params *pP, PAF_ASIT_Config *pAsitCfg,
63                          Int sourceSelect);
64 static Int decDecodeInfo(const PAF_ASIT_Params *pP, const PAF_ASIT_Patchs *pQ,
65                          PAF_ASIT_Config *pAsitCfg);
66 static Int decDecodeData(const PAF_ASIT_Params *pP, const PAF_ASIT_Patchs *pQ,
67                          PAF_ASIT_Config *pAsitCfg, Int sourceSelect);
68 static Int decDecodeComplete(const PAF_ASIT_Params *pP,
69                              PAF_ASIT_Config *pAsitCfg);
71 extern Int getFrameLengthSourceSel(const PAF_ASIT_Params *pP, Int8 sourceSelect);
73 extern UInt32 gCbWrtAfErrCnt;
77 Int asipDecodeInit(
78         const PAF_ASIT_Params *pP,
79         PAF_ASIT_Config *pAsitCfg,
80         Int sourceSelect)
81 {
82     Int errno;
84     errno = decDecodeInit(pP, pAsitCfg, sourceSelect);
86     if(errno) {
87         decDecodeComplete(pP, pAsitCfg);
89         return ASIP_ERR_DECODE_INIT;
90     }
92     return ASIP_NO_ERR;
93 }
96 // -----------------------------------------------------------------------------
97 // ASIT Processing Function - Decode Processing
98 //
99 //   Name:      asipDecodeProcessing
100 //   Purpose:
101 //   Return:    Error number in standard form (0 on success).
102 // -----------------------------------------------------------------------------
103 Int asipDecodeProcessing(
104         const PAF_ASIT_Params *pP,
105         const PAF_ASIT_Patchs *pQ,
106         PAF_ASIT_Config *pAsitCfg,
107         Int sourceSelect)
109     PAF_AST_Config *pAstCfg;
110     asipDecProc_t  *pDec;
111     Int decError, retVal, getVal;                          /* error number */
112     Int zMD, decProcDone;
113     Int8 tempVar8;
115     pAstCfg = pAsitCfg->pAstCfg;     // get pointer to common (shared) configuration
116     zMD = pAstCfg->masterDec;
117     pDec = &pAsitCfg->inpDec;
119     retVal = ASIP_NO_ERR;
121     // Initialization for decode processing
122     if(!pDec->initDone) {
123         // Initialize decoder and send message to decoder
124         retVal = decDecodeInit(pP, pAsitCfg, pAsitCfg->pIoInp[zMD].sourceSelect);
125         if(retVal != ASIP_NO_ERR) {
126             decDecodeComplete(pP, pAsitCfg);
128             return retVal;
129         }
131         pDec->initDone = TRUE;
132     }
134     // Check if source has configured to NONE
135     tempVar8 = sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceSelect),
136                                  GATEMP_INDEX_DEC);
137     if (tempVar8 == PAF_SOURCE_NONE || sourceSelect == PAF_SOURCE_NONE) {
138         TRACE_VERBOSE0("PAF_ASIT_decodeProcessing: sourceSelect == PAF_SOURCE_NONE");
139         pAsitCfg->inpDec.state = QUIT;   // skip processing, quit decoding
140         retVal = ASIP_ERR_DECODE_QUIT;
141     }
143     // Process commands (decode)
144     getVal = pP->fxns->decodeCommand(pP, pQ, pAsitCfg);
145     if (getVal) {
146         retVal = ASIP_ERR_DECODE_COMMAND;
147         if (getVal == ASPERR_QUIT) {
148             TRACE_VERBOSE0("PAF_ASIT_decodeProcessing. %d: state = QUIT");
150             // Don't return if ASPERR_QUIT, but skip decode processing and quit
151             pAsitCfg->inpDec.state = QUIT;
152         }
153         else if (getVal == ASPERR_ABORT) {
154             TRACE_VERBOSE0("PAF_ASIT_decodeProcessing. %d: return getVal");
156             // Return here if ASPERR_ABORT
157             return retVal;
158         }
159         else {
160             /* ignore */;
161         }
162     }
164     do {
165         decProcDone = TRUE;
167         switch(pAsitCfg->inpDec.state)
168         {
169             case INFO1:
170                 decError = decDecodeInfo(pP, pQ, pAsitCfg);
171                 if (decError) {
172                     //gAsipInfo1_PrimaryErrCnt++;
173                     TRACE_TERSE1("INFO1: decError 0x%x after decodeInfo, primary timing", decError);
174                     retVal = ASIP_ERR_DECODE_INFO1;
175                 }
176                 else {
177                     // Don't start decode until major access unit is found.
178                     Int8 sourceDecode, sampleRate;
180                     sourceDecode = sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceDecode),
181                                                      GATEMP_INDEX_DEC);
182                     sampleRate   = sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.sampleRate),
183                                                      GATEMP_INDEX_DEC);
184                     if ( ( (sourceDecode == PAF_SOURCE_THD)     ||
185                            (sourceDecode == PAF_SOURCE_DXP)     ||
186                            (sourceDecode == PAF_SOURCE_DTSHD) ) &&
187                          ( sampleRate == PAF_SAMPLERATE_UNKNOWN) ) {
188                         //gMajorAuMissed++; // debug
189                         pAsitCfg->inpDec.frame++;
190                         //pAsitCfg->inpDec.state = INFO1;    // stay in this state
191                     }
192                     else {
193                         decError = pP->fxns->decodeInfo1(pP, pQ, pAsitCfg, pAsitCfg->inpDec.frame, pAsitCfg->inpDec.block);
194                         if(decError) {
195                             retVal = ASIP_ERR_DECODE_INFO1;
196                         }
197                         else {
198                             pAsitCfg->inpDec.state = DECODE;
199                             decProcDone = FALSE;    // go to DECODE state before return
200                         }
201                     }
202                 }
203                 break;
205             case INFO2:
206                 decError = decDecodeInfo(pP, pQ, pAsitCfg);
207                 if (decError) {
208                     //gAsipInfo1_PrimaryErrCnt++;
209                     TRACE_TERSE1("INFO2: decError 0x%x after decodeInfo, primary timing", decError);
210                     retVal = ASIP_ERR_DECODE_INFO2;
211                 }
212                 else {
213                     pAsitCfg->inpDec.state = DECODE;
214                     decProcDone = FALSE;     // go to DECODE state before return
215                 }
216                 break;
218             case DECODE:
219                 decError = decDecodeData(pP, pQ, pAsitCfg, sourceSelect);
220                 if (decError) {
221                     //gAsipDecodeErrCnt++;
222                     TRACE_TERSE1("PAF_ASIT_decodeProcessing: state: DECODE.  decodeDecode err 0x%04x", decError);
223                     retVal = ASIP_ERR_DECODE_DATA;
224                 }
225                 else {
226                     decError = pP->fxns->decodeFinalTest(pP, pQ, pAsitCfg, pAsitCfg->inpDec.frame, pAsitCfg->inpDec.block);
227                     if (decError) {
228                         retVal = ASIP_ERR_DECODE_FINAL;
229                     }
230                     else {
231                         pAsitCfg->inpDec.frame++;
232                         pAsitCfg->inpDec.state = INFO2;
233                     }
234                 }
235                 break;
237             case QUIT:
238                 //gAsipQuitCnt++;
239                 Log_info0("TaskAsip: state=QUIT");
241                 // Quit:
242                 // - Set error number registers.
243                 // - Exit state machine to "decode complete" processing.
244                 TRACE_VERBOSE0("PAF_ASIT_decodeProcessing: state: QUIT");
245                 break;
247             default:
248                 break;
250         }  /* switch */
252     } while (!decProcDone);
254     if(retVal != ASIP_NO_ERR) {
255         decDecodeComplete(pP, pAsitCfg);
256     }
258     return retVal;
259 }  /* asipDecodeProcessing */
262 // -----------------------------------------------------------------------------
263 // ASIT Decoding Function - Reinitialization of Decode
264 //
265 //   Name:      decDecodeInit
266 //   Purpose:   Decoding Function for reinitializing the decoding process.
267 //   From:      AST Parameter Function -> decodeProcessing
268 //   Uses:      See code.
269 //   States:    x
270 //   Return:    Error number in standard or SIO form (0 on success).
271 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
272 //              * State information as per parent.
273 //
274 // -----------------------------------------------------------------------------
275 static Int decDecodeInit(
276         const PAF_ASIT_Params *pP,
277         PAF_ASIT_Config *pAsitCfg,
278         Int sourceSelect)
280     PAF_AST_Config *pAstCfg;
281     PAF_AST_IoInp  *pInp;
282     //PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
283     Int as;                             /* Audio Stream Number (1, 2, etc.) */
284     Int z;                              /* decode/encode counter */
285     Int errno;                          /* error number */
286     Int zI, zS;
287     Int argIdx;
288     Int8 tempVar8;
289     char decMsgBuf[ASP_MSG_BUF_LEN];
290     ioPhyCtl_t ioPhyCtl;
291     ioDataCtl_t ioDataCtl;
293     pAstCfg = pAsitCfg->pAstCfg; // get pointer to AST common (shared) configuration
294     pInp    = pAsitCfg->pIoInp;
296     as = pAstCfg->as;
297     (void)as;  // clear compiler warning in case not used with tracing disabled
299     //pCbCtl = &pAsitCfg->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
301     // reset frameCount
302     for (z=DECODE1; z < DECODEN; z++)
303     {
304         tempVar8 = sharedMemReadInt8(&(pAstCfg->xDec[z].decodeStatus.mode),
305                                      GATEMP_INDEX_DEC);
306         if (tempVar8)
307         {
308             sharedMemWriteInt(&(pAstCfg->xDec[z].decodeStatus.frameCount),
309                               (Int)0, GATEMP_INDEX_DEC);
310         }
311     }
313     // loop through all supported inputs
314     for (z=DECODE1; z < DECODEN; z++)
315     {
316         zI = pP->inputsFromDecodes[z];
317         zS = pP->streamsFromDecodes[z];
318         (void)zS; // clear compiler warning in case not used with tracing disabled
319         tempVar8 = sharedMemReadInt8(&(pAstCfg->xDec[z].decodeStatus.mode),
320                                      GATEMP_INDEX_DEC);
321         if (pInp[zI].hIoPhy && tempVar8)
322         {
323             Uns gear;
324             Int frameLength;
325             TRACE_VERBOSE1("AS%d: PAF_ASIT_decodeInit: initializing decode", as+zS);
327             // write back Dec configuration
328             Cache_wb(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
329             Cache_wait();
331             // send dec activate message to slave
332             argIdx = 0; // set decIdx (zone index)
333             *(Int32 *)&decMsgBuf[argIdx] = z;
334             if(AspMsgSend(ASP_SLAVE_DEC_ACTIVATE, ASP_MASTER_DEC_ACTIVATE_DONE,
335                           decMsgBuf, NULL) != ASP_MSG_NO_ERR)
336             {
337                 TRACE_TERSE0("decodeInit: error in sending DEC_ACTIVATE message ");
338                 SW_BREAKPOINT; // temporary
339                 return ASIP_ERR_DECODE_MSG; // temporary
340             }
342             // send dec reset message to slave
343             argIdx = 0; // set decIdx
344             *(Int32 *)&decMsgBuf[argIdx] = z;
345             if(AspMsgSend(ASP_SLAVE_DEC_RESET, ASP_MASTER_DEC_RESET_DONE,
346                           decMsgBuf, decMsgBuf) != ASP_MSG_NO_ERR)
347             {
348                 TRACE_TERSE0("decodeInit: error in sending DEC_RESET message ");
349                 SW_BREAKPOINT; // temporary
350                 return ASIP_ERR_DECODE_MSG; // temporary
351             }
352             else
353             {
354                 argIdx = 0; // get decErrno
355                 errno = *(Int32 *)&decMsgBuf[argIdx];
356             }
358             // invalidate Dec configuration
359             Cache_inv(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
360             Cache_wait();
362             if (errno != 0) {
363                 return ASIP_ERR_DECODE_MSG;
364             }
366             gear = (Uns)sharedMemReadInt8(&(pAstCfg->xDec[z].decodeStatus.aspGearControl),
367                                           GATEMP_INDEX_DEC);
368             tempVar8 = gear < GEARS ? gear : 0;
369             sharedMemWriteInt8(&(pAstCfg->xDec[z].decodeStatus.aspGearStatus),
370                                tempVar8, GATEMP_INDEX_DEC);
372             // Compute decoder frame length based on source selection
373             // JXU: what does this function do? why does it only return PCM frame length?
374             /*frameLength = getFrameLengthSourceSel(pP, sourceSelect);
376             pAstCfg->xDec[z].decodeControl.frameLength = frameLength;
377             pAstCfg->xDec[z].decodeInStruct.sampleCount = frameLength;
378             pAstCfg->xDec[z].decodeControl.sampleRate = PAF_SAMPLERATE_UNKNOWN;*/
380 /*
381             if (z != zMD) {    // JXTODO: implement similar thing with new I/O
382                 if (errno = SIO_idle(pAstCfg->xInp[zI].hRxSio)) {
383                     return errno;
384                 }
385             }
386 */
387 /*//JXTODO: find out if frameLength needs to be passed to I/O DATA or I/O PHY.
388             ioDataCtl.code = IODATA_CTL_SET_PCM_FRAME_LENGTH;
389             ioDataCtl.param.frameLengthPcm = frameLength;
390             ioDataControl(pInp[zI].hIoData, &ioDataCtl);
391 */
392             //JXTODO: do we need to update input status here again?
393             if (errno = asipUpdateInputStatus(pInp[zI].pRxParams,
394                                               &pAstCfg->xInp[zI].inpBufStatus,
395                                               &pAstCfg->xInp[zI].inpBufConfig)) {
396                 return ASIP_ERR_INPUT_CFG;
397             }
398         } /* end of if(hIoPhy && decodeStatus.mode) */
399     } /* end of for (z=DECODE1; z < DECODEN; z++) */
401     pAsitCfg->inpDec.frame = 0;
402     pAsitCfg->inpDec.block = 0;
403     pAsitCfg->inpDec.state = INFO1;
405     return ASIP_NO_ERR;
406 }  /* decDecodeInit */
409 // -----------------------------------------------------------------------------
410 // ASIT Decoding Function - Info Processing, Common
411 //
412 //   Name:      PAF_ASIT_decodeInfo
413 //   Purpose:   Decoding Function for processing information in a manner that
414 //              is common for both initial and subsequent frames of input data.
415 //   From:      AST Parameter Function -> decodeProcessing
416 //   Uses:      See code.
417 //   States:    x
418 //   Return:    Error number in standard form (0 on success).
419 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
420 //              * State information as per parent.
421 // -----------------------------------------------------------------------------
422 Int
423 decDecodeInfo(
424     const PAF_ASIT_Params *pP,
425     const PAF_ASIT_Patchs *pQ,
426     PAF_ASIT_Config *pAsitCfg
429     PAF_AST_Config *pAstCfg;
430     PAF_AST_IoInp  *pInp;
431     Int as;                    /* Audio Stream Number (1, 2, etc.) */
432     Int z;                     /* input/decode/stream counter */
433     Int errno;                 /* error number */
434     Int zD, zI, zS, zX;
435     Int zMD;
436     Int zMS;
437     Int argIdx;
438     Int8 tempVar8;
439     Int tempVar;
440     char decMsgBuf[ASP_MSG_BUF_LEN];
442     pAstCfg = pAsitCfg->pAstCfg;   // get pointer to common (shared) configuration
443     pInp    = pAsitCfg->pIoInp;
444     as = pAstCfg->as;
445     zMD = pAstCfg->masterDec;
446     zMS = pAstCfg->masterStr;
447     (void)zMS;  (void)as;  // clear compiler warning in case not used with tracing disabled
449     // Set decode control: sample rate, emphasis
450     for (z=INPUT1; z < INPUTN; z++)
451     {
452         zD = z;
453         for (zX = DECODE1; zX < DECODEN; zX++)
454         {
455             if (pP->inputsFromDecodes[zX] == z) {
456                 zD = zX;
457                 break;
458             }
459         }
461         if (pInp[z].hIoPhy) {
462             //determine associated decoder
463             if (pAstCfg->xInp[z].inpBufStatus.sampleRateStatus !=
464                 pAstCfg->xDec[zD].decodeControl.sampleRate) {
465                 if (pAstCfg->xDec[zD].decodeControl.sampleRate == PAF_SAMPLERATE_UNKNOWN) {
466                     pAstCfg->xDec[zD].decodeControl.sampleRate =
467                         pAstCfg->xInp[z].inpBufStatus.sampleRateStatus;
468                 }
469                 else {
470                     //TRACE_TERSE1("decDecodeInfo: AS%d: return error ASPERR_INFO_RATECHANGE", as+pAstCfg->masterStr);
471                     TRACE_TERSE2("inpBufStatus.sampleRateStatus: 0x%x, decodeControl.sampleRate: 0x%x",
472                         pAstCfg->xInp[z].inpBufStatus.sampleRateStatus,
473                         pAstCfg->xDec[zD].decodeControl.sampleRate);
474                     // return (ASPERR_INFO_RATECHANGE);
475                 }
476             }
478             tempVar8 = sharedMemReadInt8(&(pAstCfg->xDec[zD].decodeStatus.sourceDecode),
479                                          GATEMP_INDEX_DEC);
480             pAstCfg->xDec[zD].decodeControl.emphasis =
481                 tempVar8 != PAF_SOURCE_PCM
482                 ? PAF_IEC_PREEMPHASIS_NO // fix for Mantis ID #119
483                 : pAstCfg->xInp[z].inpBufStatus.emphasisStatus;
484         }
485         else {
486             pAstCfg->xDec[zD].decodeControl.sampleRate = PAF_SAMPLERATE_UNKNOWN;
487             pAstCfg->xDec[zD].decodeControl.emphasis = PAF_IEC_PREEMPHASIS_UNKNOWN;
488         }
489     } /* end of for (z=INPUT1; z < INPUTN; z++) */
491     // Wait for info input: (JX) all SIO_reclaim does is pend on data, and do sync scan or sync check,
492     //                           which is done in autoDetection routine now.
493     //if (pAstCfg->xInp[zMI].hRxSio)
494     //    sioErr = SIO_reclaim(pAstCfg->xInp[zMI].hRxSio, (Ptr)&pAstCfg->xInp[zMI].pInpBuf, NULL);
496 #if 0 // debug
497     // capture input buffer
498     capIb(pAstCfg->xInp[zMI].pInpBuf);
499     gCapIb_cnt++;
500 #endif
501    
502     // Decode info
503     for (z=DECODE1; z < DECODEN; z++)
504     {
505         zI = pP->inputsFromDecodes[z];
506         zS = pP->streamsFromDecodes[z];
507         (void)zS; // clear compiler warning in case not used with tracing disabled
509         tempVar8 = sharedMemReadInt8(&(pAstCfg->xDec[z].decodeStatus.mode),
510                                      GATEMP_INDEX_DEC);
511         if (pInp[zI].hIoPhy  && tempVar8)
512         {
513             TRACE_GEN2("PAF_ASIT_decodeInfo: AS%d: processing frame %d -- info", as+zS, pAsitCfg->inpDec.frame);
515             if (errno = asipUpdateInputStatus(pInp[zI].pRxParams,
516                                               &pAstCfg->xInp[zI].inpBufStatus,
517                                               &pAstCfg->xInp[zI].inpBufConfig)) {
518                 TRACE_TERSE1("return error errno 0x%x.", errno);
519                 return errno;
520             }
522 #if 0 // debug
523             // capture input buffer
524             capIb(pAstCfg->xInp[zI].pInpBuf);
525             gCapIb_cnt++;
526 #endif
528             // write back Inp configuration
529             Cache_wb(&pAstCfg->xInp[zI], sizeof(PAF_AST_InpBuf), Cache_Type_ALLD, 0);
530             // write back Dec configuration
531             Cache_wb(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
532             Cache_wait();
534             // send info message to slave
535             argIdx = 0; // set decIdx
536             *(Int32 *)&decMsgBuf[argIdx] = z;
537             if(AspMsgSend(ASP_SLAVE_DEC_INFO, ASP_MASTER_DEC_INFO_DONE,
538                           decMsgBuf, decMsgBuf) != ASP_MSG_NO_ERR)
539             {
540                 TRACE_TERSE0("decodeInfo: error in sending DEC_INFO message ");
541                 SW_BREAKPOINT; // temporary
542                 return -1;     // temporary
543             }
544             else
545             {
546                 argIdx = 0; // get decErrno
547                 errno = *(Int32 *)&decMsgBuf[argIdx];
548             }
550             // invalidate Dec configuration
551             Cache_inv(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
552             Cache_wait();
554             if (errno) {
555                 TRACE_TERSE1("return error errno 0x%x.", errno);
556                 return errno;
557             }
559             // increment decoded frame count
560             tempVar = sharedMemReadInt(&(pAstCfg->xDec[z].decodeStatus.frameCount),
561                                        GATEMP_INDEX_DEC);
562             tempVar += 1;
563             sharedMemWriteInt(&(pAstCfg->xDec[z].decodeStatus.frameCount),
564                               tempVar, GATEMP_INDEX_DEC);
565         }
566     } // z=DECODE1 to DECODEN
568     // query IB for latest sourceProgram (needed if we started decoding due to a force mode)
569     tempVar8 = sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.mode),
570                                  GATEMP_INDEX_DEC);
571     if (tempVar8)
572     {
573         sharedMemWriteInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceProgram),
574                            pInp[zMD].sourceProgram, GATEMP_INDEX_DEC);
575     }
577     // since now decoding update decode status for all enabled decoders
578     for (z=DECODE1; z < DECODEN; z++)
579     {
580         tempVar8 = sharedMemReadInt8(&(pAstCfg->xDec[z].decodeStatus.mode),
581                                      GATEMP_INDEX_DEC);
582         if (tempVar8)
583         {
584             tempVar8 = sharedMemReadInt8(&(pAstCfg->xDec[z].decodeStatus.sourceProgram),
585                                          GATEMP_INDEX_DEC);
586             sharedMemWriteInt8(&(pAstCfg->xDec[z].decodeStatus.sourceDecode),
587                                tempVar8, GATEMP_INDEX_DEC);
589             tempVar8 = sharedMemReadInt8(&(pAstCfg->xDec[z].decodeStatus.sourceSelect),
590                                          GATEMP_INDEX_DEC);
591             if (tempVar8 == PAF_SOURCE_SNG)
592             {
593                 tempVar8 = PAF_SOURCE_SNG;
594                 sharedMemWriteInt8(&(pAstCfg->xDec[z].decodeStatus.sourceDecode),
595                                    tempVar8, GATEMP_INDEX_DEC);
596             }
597         }
598     }
600     return ASIP_NO_ERR;
601 } /* decDecodeInfo */
604 // -----------------------------------------------------------------------------
605 // ASIT Decoding Function - Decode Processing
606 //
607 //   Name:      PAF_ASIT_decodeDecode
608 //   Purpose:   Decoding Function for processing of input data by the
609 //              Decode Algorithm.
610 //   From:      AST Parameter Function -> decodeProcessing
611 //   Uses:      See code.
612 //   States:    x
613 //   Return:    Error number in standard form (0 on success).
614 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
615 //              * State information as per parent.
616 // -----------------------------------------------------------------------------
617 static Int decDecodeData(const PAF_ASIT_Params *pP, const PAF_ASIT_Patchs *pQ,
618                          PAF_ASIT_Config *pAsitCfg, Int sourceSelect)
620     PAF_AST_Config *pAstCfg;
621     PAF_AST_IoInp  *pInp;
622     Int as;                     /* Audio Stream Number (1, 2, etc.) */
623     Int z;                      /* decode/stream counter */
624     Int errno;                  /* error number */
625     Int argIdx;
626     Int cbErrno;
627     Int frameLength;
628     char decMsgBuf[ASP_MSG_BUF_LEN];
629     ioPhyCtl_t  ioPhyCtl;
630     ioDataCtl_t ioDataCtl;
632     pAstCfg = pAsitCfg->pAstCfg; // get pointer to common (shared) configuration
633     pInp    = pAsitCfg->pIoInp;
634     as = pAstCfg->as;
635     (void)as; // clear compiler warning in case not used with tracing disabled
637     // Decode data
638     for (z=DECODE1; z < DECODEN; z++)
639     {
640         Int zI = pP->inputsFromDecodes[z];
641         Int zS = pP->streamsFromDecodes[z];
642         (void)zS; // clear compiler warning in case not used with tracing disabled
643         if ( pInp[zI].hIoPhy && pAstCfg->xDec[z].decodeStatus.mode )
644         {
645             TRACE_GEN2("PAF_ASIT_decodeDecode: AS%d: decodeDecode: processing block %d -- decode", as+zS, pAsitCfg->inpDec.block);
647             TRACE_VERBOSE3("PAF_ASIT_decodeDecode: AS%d: decodeDecode: decoding from 0x%x (base) 0x%x (ptr)",
648                     as+zS,
649                     (IArg)pAstCfg->xInp[z].pInpBuf->base.pVoid,
650                     (IArg)pAstCfg->xInp[z].pInpBuf->head.pVoid);
652 #if 0 // debug
653             // capture input buffer
654             capIbPcm(pAstCfg->xInp[z].pInpBuf);
655 #endif
656             
657             // write back Dec configuration
658             Cache_wb(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
659             Cache_wait();
661             // send decode message to slave
662             errno  = 0;
663             argIdx = 0; // set decIdx
664             *(Int32 *)&decMsgBuf[argIdx] = z;
665             if(AspMsgSend(ASP_SLAVE_DEC_DECODE, ASP_MASTER_DEC_DECODE_DONE,
666                           decMsgBuf, decMsgBuf) != ASP_MSG_NO_ERR)
667             {
668                 TRACE_TERSE0("decodeDecode: error in sending DEC_DECODE message ");
669                 SW_BREAKPOINT; // temporary
670                 return -1;     // temporary
671             }
672             else
673             {
674                 argIdx = 0; // get decErrno
675                 errno = *(Int32 *)&decMsgBuf[argIdx];
676                 argIdx += sizeof(Int32); // get cbErrno
677                 cbErrno = *(Int32 *)&decMsgBuf[argIdx];
678                 if (cbErrno != 0)
679                 {
680                     gCbWrtAfErrCnt++;
681                     TRACE_TERSE1("CB write error=%d", cbErrno);
682                     //SW_BREAKPOINT; // temporary
683                 }
684             }
686             // invalidate Dec configuration
687             Cache_inv(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
688             Cache_wait();
690             if (errno)
691             {
692                 TRACE_VERBOSE1("PAF_ASIT_decodeDecode: fxns->decode returns 0x%x", errno);
693                 return errno;
694             }
696 #if (CURRENT_TRACE_MASK & TRACE_MASK_DATA)
697             as_traceChannels(pAsitCfg, z);
698 #endif
700             // Compute decoder frame length based on source selection
701             // This is unnecessary as sourceSelect won't change until input changes
702             //frameLength = getFrameLengthSourceSel(pP, sourceSelect);
703             //pAstCfg->xDec[z].decodeControl.frameLength  = frameLength;
704             //pAstCfg->xDec[z].decodeInStruct.sampleCount = frameLength;
705         } // hIoPhy && decodeStatus.mode
706         else {
707             TRACE_VERBOSE2("AS%d: PAF_ASIT_decodeDecode: processing block %d -- decode <ignored>", as+zS, pAsitCfg->inpDec.block);
708         }
709     } // z=DECODE1 to DECODEN
711     return ASIP_NO_ERR;
713 } /* decDecodeData */
716 Int decDecodeComplete( const PAF_ASIT_Params *pP,
717                        PAF_ASIT_Config *pAsitCfg)
719     int z, zMD, decError;
720     ALG_Handle alg[DECODEN_MAX];
722     zMD = pAsitCfg->pAstCfg->masterDec;
724     for (z=DECODE1; z < DECODEN; z++)
725     {
726         alg[z] = pAsitCfg->pAstCfg->xDec[z].decAlg[PAF_SOURCE_PCM];
727     }
728     alg[zMD] = NULL; // decAlgMaster; // FL: alg[] init is on slave
730     // Call decodeComplete: 2nd argument is NOT used inside the function
731     decError = pP->fxns->decodeComplete(pP, NULL, pAsitCfg, alg,
732                                         pAsitCfg->inpDec.frame, pAsitCfg->inpDec.block);
734     if(decError) {
735         return ASIP_ERR_DECODE_COMPLETE;
736     }
738     return ASIP_NO_ERR;
739 }  /* decDecodeComplete */
742 #if 0
743 Int asipDecodeProcessing(
744         const PAF_ASIT_Params *pP,
745         const PAF_ASIT_Patchs *pQ,
746         PAF_ASIT_Config *pC,
747         Int sourceSelect)
749     PAF_AST_Config *pAstCfg;
750     Int errno, retVal;                          /* error number */
751     Int zMD;
753     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
754     zMD = pAstCfg->masterDec;
755     retVal = ASIP_NO_ERR;
757     //pAstCfg->xInp[0].inpBufStatus.sampleRateStatus = PAF_SAMPLERATE_192000HZ;  //JXTODO: make this correct
758     errno = decDecodeInfo(pP, pQ, pC);
759     if (errno != ASIP_NO_ERR) {
760         //gAsipInfo1_PrimaryErrCnt++;
761         TRACE_TERSE1("INFO1: errno 0x%x after decodeInfo, primary timing", errno);
762         retVal =  ASIP_ERR_DECODE_INFO;
763     }
764     else {
765         // Don't start decode until major access unit is found.
766         Int8 sourceDecode, sampleRate;
767         sourceDecode = sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceDecode),
768                                          GATEMP_INDEX_DEC);
769         sampleRate   = sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.sampleRate),
770                                          GATEMP_INDEX_DEC);
771         if ( ( (sourceDecode == PAF_SOURCE_THD)     ||
772                (sourceDecode == PAF_SOURCE_DXP)     ||
773                (sourceDecode == PAF_SOURCE_DTSHD) ) &&
774              ( sampleRate == PAF_SAMPLERATE_UNKNOWN) ) {
775             /* do nothing and return - waiting for major access unit */
776             pC->inpDec.frame++;
777             retVal =  ASIP_NO_ERR;
778         }
779         else {
780             errno = decDecodeData(pP, pQ, pC, sourceSelect);
781             if (errno != ASIP_NO_ERR) {
782                 //gAsipDecodeErrCnt++;
783                 TRACE_TERSE1("PAF_ASIT_decodeProcessing: state: DECODE.  decodeDecode err 0x%04x", errno);
784                 retVal = ASIP_ERR_DECODE_DATA;
785             }
786             else {
787                 errno = pP->fxns->decodeFinalTest(pP, pQ, pC, pC->inpDec.frame, pC->inpDec.block);
788                 if (errno) {
789                     retVal = ASIP_ERR_DECODE_FINAL;
790                 }
791                 else {
792                     retVal = ASIP_NO_ERR;
793                 }
794             }
795         }
796     }
798     return retVal;
799 }  /* asipDecodeProcessing */
800 #endif
802 /* Nothing past this line */