]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - pasdk/test_dsp/framework/audioStreamOutProc.c
e53574063771b45a87dece673c4ac91a9090a6dc
[processor-sdk/performance-audio-sr.git] / pasdk / test_dsp / framework / audioStreamOutProc.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 /*
37  *  ======== audioStreamOutProc.c ========
38  */
40 #include <string.h> // for memset
41 #include <xdc/runtime/Log.h>
42 #include <xdc/runtime/Error.h>
43 #include <xdc/runtime/Memory.h>
44 #include <ti/sysbios/knl/Clock.h>
45 #include <ti/sysbios/knl/Task.h>
47 #include "paferr.h"
48 #include <acp_mds.h>
49 #include <pcm.h>
50 #include <pce.h>
51 #include <pafsio_ialg.h>
52 #include <stdasp.h>
53 #include <doberr.h>
54 #include "asperr.h"
56 #include "common.h"
57 #include "aspMsg_common.h"
58 #include "aspMsg_master.h"
59 #include "aspDecOpCircBuf_master.h"
60 #include "audioStreamProc_common.h"
61 #include "audioStreamOutProc.h"
63 #define TRACE_TIME(a)
65 //
66 // Audio Stream Definitions
67 //
69 //
70 // Audio Stream Processing Definitions
71 //
72 #define aspLinkInit pQ->i_aspLinkInit
74 //
75 // Encoder Definitions
76 //
77 #define encLinkInit pQ->i_encLinkInit
79 //
80 // Output Definitions
81 //
82 #define outLinkInit pP->i_outLinkInit
84 /* ---------------------------------------------------------------- */
85 /*              Parameter macro definitions end here.               */
86 /* ---------------------------------------------------------------- */
88 //
89 // Standardized Definitions
90 //
92 #define ENC_Handle PCE_Handle /* works for all: PCE */
94 #define __TASK_NAME__  "TaskAsop"
97 /* FL: Check if at least one output selected */
98 static Int checkOutSel(
99     const PAF_ASOT_Params *pP, 
100     PAF_ASOT_Config *pC,
101     Int *pOutSel
102 );
104 /* FL: Check if at least one output sio changed */
105 static Int checkOutSio(
106     const PAF_ASOT_Params *pP, 
107     PAF_ASOT_Config *pC,
108     Int *pOutSioUpdate
109 );
111 LINNO_DEFN(TaskAsop); /* Line number macros */
112 ERRNO_DEFN(TaskAsop); /* Error number macros */
114 // ASOT configuration
115 #pragma DATA_SECTION(gPAF_ASOT_config, ".globalSectionPafAsotConfig")
116 PAF_ASOT_Config gPAF_ASOT_config = {
117     NULL,               // acp
118     &gPAF_ASPM_config,  // pAspmCfg
119     &gPAF_AST_config    // pAstCfg
120 };
122 // Underflow threshold before circular buffer reset and return error to Top-Level FSM
123 #define DEC_OP_CB_RDAF_UND_THR  ( 80 ) // FL: arbitrary setting
124 UInt32 gCbReadAfErr         =0; // read circular buffer error count, not including underflows
125 UInt32 gDecOpCbRdAfUnd      =0; // decoder output circular buffer underflow count
126 UInt32 gMaxDecOpCbRdAfUnd   =0; // max (consecutive) decoder output circular buffer underflow count
128 // Global debug counters */
129 UInt32 gTaskAsopCnt=0; // debug
130 UInt32 gAsopInitCnt         =0;
131 UInt32 gAsopStreamCnt       =0;
132 UInt32 gAsopEncodeCnt       =0;
133 UInt32 gAsopFinalCnt        =0;
134 UInt32 gAsopQuitCnt         =0;
136 // FL: (***) debug
137 #include "evmc66x_gpio_dbg.h"
139 /*
140  *  ======== taskAsopFxn ========
141  *  Audio Stream Output Processing task function
142  */
143 Void taskAsopFxn(
144 //    Int betaPrimeValue, // FL: revisit
145     const PAF_ASOT_Params *pP,
146     const PAF_ASOT_Patchs *pQ
149     PAF_ASOT_Config *pC;            /* Local configuration pointer */
150     PAF_AST_Config *pAstCfg;        /* Common (shared) configuration pointer */
151     Int as;                         /* Audio Stream Number (1, 2, etc.) */
152     Int z;                          /* input/encode/stream/decode/output counter */
153     Int i;                          /* phase */
154     Int errno;                      /* error number */
155     Int zMS;
156     Int loopCount = 0;  // used to stop trace to see startup behavior.        
158     Log_info0("Enter taskAsopFxn()");    
160     //
161     // Audio Framework Parameters & Patch (*pP, *pQ)
162     //
163     if (!pP) 
164     {
165         TRACE_TERSE0("TaskAsop: No Parameters defined. Exiting.");
166         LINNO_RPRT(TaskAsop, -1);
167         return;
168     }
170     if (!pQ)
171     {
172         TRACE_TERSE0("TaskAsip: No Patchs defined. Exiting.");
173         LINNO_RPRT(TaskAsop, -1);
174         return;
175     }    
177     //
178     // Audio Framework Configuration (*pC):
179     //
180     pC = &gPAF_ASOT_config;
181     pAstCfg = pC->pAstCfg;
183     /* Obtain Audio Stream Number (1, 2, etc.) */    
184     as = pAstCfg->as;
185     TRACE_TERSE1("TaskAsop: Started with AS%d.", as);
187     //
188     // Initialize message log trace and line number reporting
189     //
190     for (z=STREAM1; z < STREAMN; z++)
191     {
192         TRACE_TERSE1("TaskAsop: AS%d: initiated", as+z);
193     }
194     LINNO_RPRT(TaskAsop, -1);
195     
196     //
197     // Determine stream index
198     //
199     zMS = pAstCfg->masterStr;
201     // Initialize as per parametrized phases:
202     //
203     //   In standard form these are:
204     //      - Malloc: Memory Allocation
205     //      - Config: Configuration Initialization
206     //      - AcpAlg: ACP Algorithm Initialization and Local Attachment
207     //      - Common: Common Memory Initialization
208     //      - AlgKey: Dec/Enc chain to Array Initialization
209     //      - Device: I/O Device Initialization
210     //      - Unused: (available)
211     //      - Unused: (available)
212     //
213     LINNO_RPRT(TaskAsop, -2);
214     for (i=0; i < lengthof(pP->fxns->initPhase); i++)
215     {
216         Int linno;
217         if (pP->fxns->initPhase[i])
218         {
219             if (linno = pP->fxns->initPhase[i](pP, pQ, pC)) 
220             {
221                 LINNO_RPRT(TaskAsop, linno);
222                 return;
223             }
224         }
225         else 
226         {
227             TRACE_TERSE1("TaskAsop: AS%d: initialization phase - null", as+zMS);
228         }
229         TRACE_TERSE2("TaskAsop: AS%d: initialization phase - %d completed", as+zMS, i);
230         LINNO_RPRT(TaskAsop, -i-3);
231     }
232   
233     //
234     // End of Initialization -- display memory usage report.
235     //
236     if (pP->fxns->memStatusPrint)
237     {
238         pP->fxns->memStatusPrint(HEAP_INTERNAL, HEAP_INTERNAL1, HEAP_EXTERNAL, HEAP_INTERNAL1_SHM);
239     }
241     //
242     // Main processing loop
243     //   
244     for (z=STREAM1; z < STREAMN; z++)
245     {
246         TRACE_VERBOSE1("TaskAsip: AS%d: running", as+z);
247     }
248     
249     errno = 0;
250     for (;;)
251     {
252         Int outSel;
254         loopCount++;
255         TRACE_GEN2("TaskAsop (begin Main loop %d) (errno 0x%x)", loopCount, errno);
257         // any error forces idling of output
258         if (errno) 
259         {
260             for (z=OUTPUT1; z < OUTPUTN; z++)
261             {
262                 if (pAstCfg->xOut[z].hTxSio)
263                 {
264                     SIO_idle(pAstCfg->xOut[z].hTxSio);
265                 }
266             }
267         
268             TRACE_TERSE1("TaskAsop: Trace stopped at loop %d.", loopCount);
269             ERRNO_RPRT(TaskAsop, errno);
270         }        
271         
272         TRACE_VERBOSE1("TaskAsop: AS%d: ... sleeping ...", as+zMS);
273         Task_sleep(1);
275         TRACE_GEN1("TaskAsop: AS%d: Output device selection ...", as+zMS);
276         if (errno = pP->fxns->selectDevices(pP, pQ, pC))
277         {
278             TRACE_TERSE2("TaskAsop: AS%d: selectDevices returned errno = 0x%04x", as+zMS, errno);
279             continue;
280         }
282         // if no output selected skip any remaining processing
283         if (errno = checkOutSel(pP, pC, &outSel))
284         {
285             TRACE_TERSE2("TaskAsop: AS%d: checkOutSel returned errno = 0x%04x", as+zMS, errno);
286             continue;
287         }
288         else if (!outSel)
289         {
290             TRACE_VERBOSE1("TaskAsop: AS%d: No output selected...", as+zMS);            
291             continue;
292         }
293         
294         TRACE_VERBOSE0("TaskAsop: calling outputProcessing.");
295         errno = pP->fxns->decodeProcessing(pP, pQ, pC, -1);
296         if (errno) 
297         {
298             TRACE_TERSE1("TaskAsop: outputProcessing returns 0x%x, continue", errno);
299         }
300         else
301         {
302             TRACE_VERBOSE0("TaskAsop: outputProcessing complete with no error.");
303         }        
304     } // End of main processing loop for (;;)
305     
306     Log_info0("Exit taskAsopFxn()");
309 // -----------------------------------------------------------------------------
310 // AST Initialization Function - Memory Allocation
311 //
312 //   Name:      PAF_ASOT_initPhaseMalloc
313 //   Purpose:   Audio Stream Output Task Function for initialization of data pointers
314 //              by allocation of memory.
315 //   From:      audioStream1Task or equivalent
316 //   Uses:      See code.
317 //   States:    x
318 //   Return:    0 on success.
319 //              Source code line number on MEM_calloc failure.
320 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
321 //              * State information as per parent.
322 //              * Memory allocation errors.
323 //
325 Int
326 PAF_ASOT_initPhaseMalloc (
327     const PAF_ASOT_Params *pP, 
328     const PAF_ASOT_Patchs *pQ, 
329     PAF_ASOT_Config *pC
332     PAF_AST_Config *pAstCfg;
333     Int as;                     /* Audio Stream Number (1, 2, etc.) */
334     Int zMS;
335     Error_Block    eb;
336     //Int i;
338     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
339     as = pAstCfg->as;
340     zMS = pAstCfg->masterStr;
342     TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: initialization phase - memory allocation", as+zMS);
344     // Initialize error block
345     Error_init(&eb); 
347     /* Stream memory */
348     if (!(pAstCfg->xStr = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, STREAMN * sizeof (*pAstCfg->xStr), 4, &eb)))
349     {
350         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
351         SW_BREAKPOINT;
352         return __LINE__;
353     }
354     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAstCfg->xStr) %d bytes from space %d at 0x%x.",
355             STREAMN * sizeof (*pAstCfg->xStr),
356             HEAP_ID_INTERNAL, (IArg)pAstCfg->xStr);
358     {
359         Int z;                          /* stream counter */
361         PAF_AudioFrame *fBuf;
363         if (!(fBuf = (PAF_AudioFrame *)Memory_calloc((IHeap_Handle)HEAP_INTERNAL, STREAMS * sizeof (*fBuf), 4, &eb)))
364         {
365             TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
366             SW_BREAKPOINT;
367             return __LINE__;
368         }
369         TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (fBuf) %d bytes from space %d at 0x%x.",
370                 STREAMS * sizeof (*fBuf),
371                 HEAP_ID_INTERNAL, (IArg)fBuf);
373         for (z=STREAM1; z < STREAMN; z++)
374         {
375             pAstCfg->xStr[z].pAudioFrame = &fBuf[z-STREAM1];
376             TRACE_TERSE2("pAstCfg->xStr[%d].pAudioFrame = 0x%x", z, (IArg)pAstCfg->xStr[z].pAudioFrame);
377         }
378     }
380     /* Encode memory */
381     if (!(pAstCfg->xEnc = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, ENCODEN * sizeof (*pAstCfg->xEnc), 4, &eb)))
382     {
383         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
384         SW_BREAKPOINT;
385         return __LINE__;
386     }
387     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAstCfg->xEnc) %d bytes from space %d at 0x%x.",
388             ENCODEN * sizeof (*pAstCfg->xEnc),
389             HEAP_ID_INTERNAL, (IArg)pAstCfg->xEnc);
391     /* Output memory */
392     if (!(pAstCfg->xOut = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, OUTPUTN * sizeof (*pAstCfg->xOut), 4, &eb)))
393     {
394         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
395         SW_BREAKPOINT;
396         return __LINE__;
397     }
398     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAstCfg->xOut) %d bytes from space %d at 0x%x.",
399             OUTPUTN * sizeof (*pAstCfg->xOut),
400             HEAP_ID_INTERNAL, (IArg)pAstCfg->xOut);
402     TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: initialization phase - memory allocation complete.", as+zMS);
403     return 0;
404 } //PAF_ASOT_initPhaseMalloc
406 // -----------------------------------------------------------------------------
407 // ASOT Initialization Function - Memory Initialization from Configuration
408 //
409 //   Name:      PAF_ASOT_initPhaseConfig
410 //   Purpose:   Audio Stream Output Task Function for initialization of data values
411 //              from parameters.
412 //   From:      audioStream1Task or equivalent
413 //   Uses:      See code.
414 //   States:    x
415 //   Return:    0 on success.
416 //              Other as per initFrame0 and initFrame1.
417 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
418 //              * State information as per parent.
419 //
420 Int
421 PAF_ASOT_initPhaseConfig(
422     const PAF_ASOT_Params *pP, 
423     const PAF_ASOT_Patchs *pQ, 
424     PAF_ASOT_Config *pC
427     PAF_AST_Config *pAstCfg;
428     Int as;                     /* Audio Stream Number (1, 2, etc.) */
429     Int z;                      /* input/encode/stream/decode/output counter */
430     Int zMS;
432     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
433     as = pAstCfg->as;
434     zMS = pAstCfg->masterStr;
436     TRACE_TERSE1("PAF_ASOT_initPhaseConfig: AS%d: initialization phase - configuration", as+zMS);
438     //
439     // Unspecified elements have been initialized to zero during alloc
440     //
442     for (z=STREAM1; z < STREAMN; z++) 
443     {
444         Int linno;
445         if (linno = pP->fxns->initFrame0(pP, pQ, pC, z))
446         {
447             return linno;           
448         }
449         if (linno = pP->fxns->initFrame1(pP, pQ, pC, z, -1))
450         {
451             return linno;
452         }
453     }
455     for (z=ENCODE1; z < ENCODEN; z++) 
456     {
457         Int zO = pP->outputsFromEncodes[z];
458         Int zS = pP->streamsFromEncodes[z];
459         pAstCfg->xEnc[z].encodeControl.size = sizeof(pAstCfg->xEnc[z].encodeControl);
460         pAstCfg->xEnc[z].encodeControl.pAudioFrame = pAstCfg->xStr[zS].pAudioFrame;
461         pAstCfg->xEnc[z].encodeControl.pVolumeStatus = &pAstCfg->xEnc[z].volumeStatus;
462         pAstCfg->xEnc[z].encodeControl.pOutBufConfig = &pAstCfg->xOut[zO].outBufConfig;
463         pAstCfg->xEnc[z].encodeStatus = *pP->z_pEncodeStatus[z];
464         pAstCfg->xEnc[z].encodeControl.encActive = pAstCfg->xEnc[z].encodeStatus.select;
465         pAstCfg->xEnc[z].volumeStatus = *pP->pVolumeStatus;
466         pAstCfg->xEnc[z].encodeInStruct.pAudioFrame = pAstCfg->xStr[zS].pAudioFrame;
467     }
469     for (z=OUTPUT1; z < OUTPUTN; z++)
470     {
471         pAstCfg->xOut[z].outBufStatus = *pP->pOutBufStatus;
472     }
474     TRACE_TERSE1("PAF_ASOT_initPhaseConfig: AS%d: initialization phase - configuration complete.", as+zMS);
475     return 0;
476 } //PAF_ASOT_initPhaseConfig
478 // -----------------------------------------------------------------------------
479 // ASOT Initialization Function - ACP Algorithm Instantiation
480 //
481 //   Name:      PAF_ASOT_initPhaseAcpAlg
482 //   Purpose:   Audio Stream Input Task Function for initialization of ACP by
483 //              instantiation of the algorithm.
484 //   From:      audioStream1Task or equivalent
485 //   Uses:      See code.
486 //   States:    x
487 //   Return:    0 on success.
488 //              Source code line number on ACP Algorithm creation failure.
489 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
490 //              * State information as per parent.
491 //              * Memory allocation errors.
492 //
493 Int
494 PAF_ASOT_initPhaseAcpAlg(
495     const PAF_ASOT_Params *pP, 
496     const PAF_ASOT_Patchs *pQ, 
497     PAF_ASOT_Config *pC
500     PAF_AST_Config *pAstCfg;
501     Int as;                     /* Audio Stream Number (1, 2, etc.) */
502     Int z;                      /* input/encode/stream/decode/output counter */
503     Int betaPrimeOffset;
504     ACP_Handle acp;
505     Int zMS;
506     Int zS, zX;
508     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
509     as = pAstCfg->as;
510     zMS = pAstCfg->masterStr;
512     TRACE_TERSE1("PAF_ASOT_initPhaseAcpAlg: AS%d: initialization phase - ACP Algorithm", as+zMS);
514     ACP_MDS_init();
516     if (!(acp = (ACP_Handle )ACP_MDS_create(NULL))) 
517     {
518         TRACE_TERSE1("PAF_ASOT_initPhaseAcpAlg: AS%d: ACP algorithm instance creation  failed", as+zMS);
519         return __LINE__;
520     }
521     pC->acp = acp;
523     ((ALG_Handle)acp)->fxns->algControl((ALG_Handle) acp,
524         ACP_GETBETAPRIMEOFFSET, (IALG_Status *)&betaPrimeOffset);
526     for (z=ENCODE1; z < ENCODEN; z++) 
527     {
528         zS = pP->streamsFromEncodes[z];
529         acp->fxns->attach(acp, ACP_SERIES_STD,
530             STD_BETA_ENCODE + betaPrimeOffset * (as-1+zS),
531             (IALG_Status *)&pAstCfg->xEnc[z].encodeStatus);
532         acp->fxns->attach(acp, ACP_SERIES_STD,
533             STD_BETA_VOLUME + betaPrimeOffset * (as-1+zS),
534             (IALG_Status *)&pAstCfg->xEnc[z].volumeStatus);
535         /* Ignore errors, not reported. */
536     }
538     for (z=OUTPUT1; z < OUTPUTN; z++) 
539     {
540         zS = z;
541         for (zX = ENCODE1; zX < ENCODEN; zX++) 
542         {
543             if (pP->outputsFromEncodes[zX] == z) 
544             {
545                 zS = pP->streamsFromEncodes[zX];
546                 break;
547             }
548         }
549         acp->fxns->attach(acp, ACP_SERIES_STD,
550             STD_BETA_OB + betaPrimeOffset * (as-1+zS),
551             (IALG_Status *)&pAstCfg->xOut[z].outBufStatus);
552         /* Ignore errors, not reported. */
553     }
555     TRACE_TERSE1("PAF_ASOT_initPhaseAcpAlg: AS%d: initialization phase - ACP Algorithm complete.", as+zMS);
557     return 0;
558 } //PAF_ASOT_initPhaseAcpAlg
560 // -----------------------------------------------------------------------------
561 // ASOT Initialization Function - Common Memory
562 //
563 //   Name:      PAF_ASOT_initPhaseCommon
564 //   Purpose:   Audio Stream Output Task Function for allocation of common memory.
565 //   From:      audioStream1Task or equivalent
566 //   Uses:      See code.
567 //   States:    x
568 //   Return:    0 on success.
569 //              Source code line number on PAF_ALG_alloc failure.
570 //              Source code line number on PAF_ALG_mallocMemory failure.
571 //              Source code line number on Decode Chain initialization failure.
572 //              Source code line number on ASP Chain initialization failure.
573 //              Source code line number on Encode Chain initialization failure.
574 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
575 //              * State information as per parent.
576 //              * Memory allocation errors.
577 //
578 Int
579 PAF_ASOT_initPhaseCommon(
580     const PAF_ASOT_Params *pP, 
581     const PAF_ASOT_Patchs *pQ, 
582     PAF_ASOT_Config *pC
585     PAF_AST_Config *pAstCfg;
586     Int as;                     /* Audio Stream Number (1, 2, etc.) */
587     Int z;                      /* stream counter */
588     Int g;                      /* gear */
589     ACP_Handle acp;
590     PAF_IALG_Config pafAlgConfig;
591     IALG_MemRec common[3][PAF_IALG_COMMON_MEMN+1];
592    
593     acp = pC->acp;
594     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
595     as = pAstCfg->as;
597     TRACE_TERSE0("PAF_ASOT_initPhaseCommon: initialization phase - Common Memory");
599     //
600     // Determine memory needs and instantiate algorithms across audio streams
601     //
602     TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ALG_setup.");
603     PAF_ALG_setup(&pafAlgConfig, 
604         HEAP_ID_INTERNAL,                 HEAP_INTERNAL, 
605         HEAP_ID_INTERNAL1,                HEAP_INTERNAL1, 
606         HEAP_ID_EXTERNAL,                 HEAP_EXTERNAL, 
607         HEAP_ID_INTERNAL1_SHM,            HEAP_INTERNAL1_SHM, 
608         HEAP_ID_EXTERNAL_SHM,             HEAP_EXTERNAL_SHM, 
609         HEAP_ID_EXTERNAL_NONCACHED_SHM,   HEAP_EXTERNAL_NONCACHED_SHM,
610         HEAP_CLEAR);
612     if (pP->fxns->headerPrint)
613     {
614         pP->fxns->headerPrint();        
615     }
617     for (z = STREAM1; z < STREAMN; z++) 
618     {
619         //Int zD, zE, zX;
620         Int zE, zX;
622         TRACE_TERSE1("PAF_ASOT_initPhaseCommon: AS%d: initialization phase - Common Memory", as+z);
624         //
625         // Determine common memory needs for:
626         //  (1) ASP Algorithms
627         //  (2) Encode Algorithms
628         //  (3) Logical Output drivers
629         //
630         PAF_ALG_init(common[z], lengthof(common[z]), COMMONSPACE);
632         zE = -1;
633         for (zX = ENCODE1; zX < ENCODEN; zX++) 
634         {
635             if (pP->streamsFromEncodes[zX] == z) 
636             {
637                 zE = zX;
638                 break;
639             }
640         }
642         TRACE_TERSE1("Calling PAF_ALG_ALLOC for stream common[%d].", z);
643         if (PAF_ALG_ALLOC(aspLinkInit[z-STREAM1][0], common[z])) 
644         {
645             TRACE_TERSE1("PAF_ASOT_initPhaseCommon: AS%d: PAF_ALG_alloc failed", as+z);
646             TRACE_TERSE2("Failed to alloc %d bytes from space %d ", common[z]->size, common[z]->space);
647             SW_BREAKPOINT;
648             return __LINE__;
649         }
650         TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
651         if (pP->fxns->allocPrint)
652         {
653             pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(aspLinkInit[z-STREAM1][0]), sizeof (*(aspLinkInit[z-STREAM1][0])), &pafAlgConfig);
654         }
656         if (zE >= 0) 
657         {
658             TRACE_TERSE1("PAF_ASOT_initPhaseCommon: calling PAF_ALG_ALLOC/ for encoder common[%d].", z);
659             if (PAF_ALG_ALLOC(encLinkInit[zE-ENCODE1], common[z])) 
660             {
661                 TRACE_TERSE1("PAF_ASOT_initPhaseCommon: AS%d: PAF_ALG_alloc failed", as+z);
662                 SW_BREAKPOINT;
663                 return __LINE__;
664             }
665             TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
666             if (pP->fxns->allocPrint)
667             {
668                 pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(encLinkInit[z-ENCODE1]), sizeof (*(encLinkInit[z-ENCODE1])), &pafAlgConfig);
669             }
670         }
672         //
673         // Determine common memory needs of Logical IO drivers
674         //
676         if (OUTPUT1 <= z && z < OUTPUTN)
677         {
678             TRACE_TERSE1("PAF_ASOT_initPhaseCommon: calling PAF_ALG_ALLOC outLinkInit common[%d].", z);
679             if (PAF_ALG_ALLOC(outLinkInit[z-OUTPUT1], common[z]))
680             {
681                 TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: PAF_ALG_alloc failed", as+z);
682                 TRACE_TERSE2("Failed to alloc %d bytes from space %d", common[z]->size, (IArg)common[z]->space);
683                 SW_BREAKPOINT;
684                 return __LINE__;
685             }
686             TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
687             if (pP->fxns->allocPrint)
688             {
689                 pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(outLinkInit[z-INPUT1]), sizeof (*(outLinkInit[z-INPUT1])), &pafAlgConfig);
690             }
691         }
692     }
693     {
694         // Changes made to share scratch between zones
695         // Assume maximum 3 zones and scratch common memory is at offset 0;
696         int max=0;
697         for (z=STREAM1; z<STREAMN; z++)
698         {
699             if (max < common[z][0].size)
700             {
701                 max = common[z][0].size;
702             }
703         }
704         common[STREAM1][0].size=max;
705         for (z=STREAM1+1; z<STREAMN; z++)
706         {
707             common[z][0].size = 0;            
708         }
709     }
710         
711     //
712     // Allocate common memory for:
713     //  (1) ASP Algorithms
714     //  (2) Encode Algorithms
715     //  (3) Logical Output drivers
716     //
717     for (z = STREAM1; z < STREAMN; z++) 
718     {
719         //Int zD, zE, zX;
720         Int zE, zX;
722         TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ALG_mallocMemory for common space.");
723         if (PAF_ALG_mallocMemory(common[z], &pafAlgConfig)) 
724         {
725             TRACE_TERSE1("AS%d: PAF_ALG_mallocMemory failed", as+z);
726             TRACE_TERSE3("AS%d: z: %d.  Size 0x%x", as+z, z, common[z][0].size);
727             SW_BREAKPOINT;
728             return __LINE__;
729         }
730         TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
731         // share zone0 scratch with all zones 
732         common[z][0].base = common[0][0].base;
733         if (pP->fxns->commonPrint)
734         {
735             pP->fxns->commonPrint(common[z], &pafAlgConfig);
736         }
738         zE = -1;
739         for (zX = ENCODE1; zX < ENCODEN; zX++) 
740         {
741             if (pP->streamsFromEncodes[zX] == z) 
742             {
743                 zE = zX;
744                 break;
745             }
746         }
748         pAstCfg->xStr[z].aspChain[0] = NULL;
749         for (g=0; g < GEARS; g++) 
750         {
751             PAF_ASP_Chain *chain;
752             TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ASP_chainInit for ASPs.");
753             chain = PAF_ASP_chainInit(&pAstCfg->xStr[z].aspChainData[g], pP->pChainFxns,
754                 HEAP_INTERNAL, as+z, acp, &trace,
755                 aspLinkInit[z-STREAM1][g], pAstCfg->xStr[z].aspChain[0], common[z], &pafAlgConfig);
756             if (!chain) 
757             {
758                 TRACE_TERSE2("AS%d: ASP chain %d initialization failed", as+z, g);
759                 return __LINE__;
760             }
761             else
762             {
763                 pAstCfg->xStr[z].aspChain[g] = chain;
764             }
765         }
767         if (zE >= 0) 
768         {
769             PAF_ASP_Chain *chain;
770             TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ASP_chainInit for encode.");
771             chain = PAF_ASP_chainInit(&pAstCfg->xEnc[zE].encChainData, pP->pChainFxns,
772                 HEAP_INTERNAL, as+z, acp, &trace,
773                 encLinkInit[zE-ENCODE1], NULL, common[z], &pafAlgConfig);
774             if (!chain) 
775             {
776                 TRACE_TERSE1("AS%d: Encode chain initialization failed", as+z);
777                 return __LINE__;
778             }
779         }
781         //
782         // Allocate non-common memories for Logical IO drivers
783         //    Since these structures are used at run-time we allocate from external memory
784         if (OUTPUT1 <= z && z < OUTPUTN) 
785         {
786             PAF_ASP_Chain *chain;
787             TRACE_TERSE2("PAF_ASOT_initPhaseMalloc: AS%d: non-common output chain init for %d",
788                            as+z, z);
789             chain = PAF_ASP_chainInit (&pAstCfg->xOut[z].outChainData, pP->pChainFxns,
790                         HEAP_EXTERNAL, as+z, acp, &trace,
791                         outLinkInit[z-OUTPUT1], NULL, common[z], &pafAlgConfig);
792             if (!chain) 
793             {
794                 TRACE_TERSE1("AS%d: Output chain initialization failed", as+z);
795                 return __LINE__;
796             }
797         }
798     }
799     TRACE_TERSE1("AS%d: PAF_ASOT_initPhaseCommon: Returning complete.", as+z);
801     return 0;
802 } //PAF_ASOT_initPhaseCommon
804 // -----------------------------------------------------------------------------
805 // ASOT Initialization Function - Algorithm Keys
806 //
807 //   Name:      PAF_ASOT_initPhaseAlgKey
808 //   Purpose:   Audio Stream Output Task Function for initialization of data values
809 //              from parameters for Algorithm Keys.
810 //   From:      audioStream1Task or equivalent
811 //   Uses:      See code.
812 //   States:    x
813 //   Return:    0.
814 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
815 //              * State information as per parent.
816 //
817 // .............................................................................
818 Int
819 PAF_ASOT_initPhaseAlgKey(
820     const PAF_ASOT_Params *pP, 
821     const PAF_ASOT_Patchs *pQ, 
822     PAF_ASOT_Config *pC
825     PAF_AST_Config *pAstCfg;
826     Int as;                     /* Audio Stream Number (1, 2, etc.) */
827     Int z;                      /* decode/encode counter */
828     Int s;                      /* key number */
829     PAF_ASP_Link *that;
831     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
832     as = pAstCfg->as;
833     (void)as; // clear compiler warning in case not used with tracing disabled
835     TRACE_VERBOSE1("PAF_ASOT_initPhaseAlgKey: AS%d: initialization phase - Algorithm Keys", as);
837     for (z=ENCODE1; z < ENCODEN; z++) 
838     {
839         for (s=0; s < pP->pEncAlgKey->length; s++) 
840         {
841             if ((pP->pEncAlgKey->code[s].full != 0) && 
842                 (that = PAF_ASP_chainFind(&pAstCfg->xEnc[z].encChainData, pP->pEncAlgKey->code[s])))
843             {
844                 pAstCfg->xEnc[z].encAlg[s] = (ALG_Handle )that->alg;
845             }
846             /* Cast in interface, for now --Kurt */
847             else
848             {
849                 pAstCfg->xEnc[z].encAlg[s] = NULL;                
850             }
851         }
852     }
854     return 0;
855 } //PAF_ASOT_initPhaseAlgKey
857 // -----------------------------------------------------------------------------
858 // ASOT Initialization Function - I/O Devices
859 //
860 //   Name:      PAF_ASOT_initPhaseDevice
861 //   Purpose:   Audio Stream Output Task Function for initialization of I/O Devices.
862 //   From:      audioStream1Task or equivalent
863 //   Uses:      See code.
864 //   States:    x
865 //   Return:    0 on success.
866 //              Source code line number on device allocation failure.
867 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
868 //              * State information as per parent.
869 //              * Memory allocation errors.
870 //
871 Int
872 PAF_ASOT_initPhaseDevice(
873     const PAF_ASOT_Params *pP, 
874     const PAF_ASOT_Patchs *pQ, 
875     PAF_ASOT_Config *pC
878     PAF_AST_Config *pAstCfg;
879     Int as;                     /* Audio Stream Number (1, 2, etc.) */
880     Int z;                      /* input/output counter */
881     PAF_SIO_IALG_Obj    *pObj;
882     PAF_SIO_IALG_Config *pAlgConfig;
883     PAF_IALG_Config pafAlgConfig;
885     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
886     as = pAstCfg->as;
887     (void)as; // clear compiler warning in case not used with tracing disabled
889     TRACE_TERSE1("PAF_ASOT_initPhaseDevice: AS%d: initialization phase - I/O Devices", as);
891     if(pP->fxns->bufMemPrint)
892     {
893         PAF_ALG_setup (&pafAlgConfig, 
894             HEAP_ID_INTERNAL,               HEAP_INTERNAL, 
895             HEAP_ID_INTERNAL1,              HEAP_INTERNAL1,
896             HEAP_ID_EXTERNAL,               HEAP_EXTERNAL,
897             HEAP_ID_INTERNAL1_SHM,          HEAP_INTERNAL1_SHM,
898             HEAP_ID_EXTERNAL_SHM,           HEAP_EXTERNAL_SHM,
899             HEAP_ID_EXTERNAL_NONCACHED_SHM, HEAP_EXTERNAL_NONCACHED_SHM,
900             HEAP_CLEAR);
901         TRACE_TERSE2("PAF_ASOT_initPhaseDevice: AS%d: calling PAF_ALG_setup with clear at %d.", as, HEAP_CLEAR);
902     }
903     
904     for (z=OUTPUT1; z < OUTPUTN; z++) 
905     {
906         PAF_OutBufConfig *pConfig = &pAstCfg->xOut[z].outBufConfig;
908         pObj = (PAF_SIO_IALG_Obj *)pAstCfg->xOut[z].outChainData.head->alg;
909         pAlgConfig = &pObj->config;
911         pAstCfg->xOut[z].hTxSio = NULL;
912         pConfig->base.pVoid     = pAlgConfig->pMemRec[0].base;
913         pConfig->pntr.pVoid     = pAlgConfig->pMemRec[0].base;
914         pConfig->head.pVoid     = pAlgConfig->pMemRec[0].base;
915         pConfig->allocation     = pAlgConfig->pMemRec[0].size;
916         pConfig->sizeofElement  = 3;
917         pConfig->precision      = 24;
918         if(pP->fxns->bufMemPrint)
919         {
920             pP->fxns->bufMemPrint(z,pAlgConfig->pMemRec[0].size,PAF_ALG_memSpaceToHeapId(&pafAlgConfig,pAlgConfig->pMemRec[0].space),1);
921         }
922     }
923     TRACE_TERSE1("PAF_ASOT_initPhaseDevice: AS%d: initialization phase - I/O Devices complete.", as);
925     return 0;
926 } //PAF_ASOT_initPhaseDevice
928 // -----------------------------------------------------------------------------
929 // ASOT Initialization Function Helper - Initialization of Audio Frame
930 //
931 //   Name:      PAF_ASOT_initFrame0
932 //   Purpose:   Audio Stream Output Task Function for initialization of the Audio
933 //              Frame(s) by memory allocation and loading of data pointers
934 //              and values.
935 //   From:      AST Parameter Function -> decodeInfo
936 //   Uses:      See code.
937 //   States:    x
938 //   Return:    0 on success.
939 //              Source code line number on MEM_calloc failure.
940 //              Source code line number on unsupported option.
941 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
942 //              * Memory allocation errors.
943 //              * Unsupported option errors.
944 //
946 // MID 314
947 extern const char AFChanPtrMap[PAF_MAXNUMCHAN+1][PAF_MAXNUMCHAN];
948 extern PAF_ChannelConfigurationMaskTable PAF_ASP_stdCCMT_patch;
950 Int
951 PAF_ASOT_initFrame0(
952     const PAF_ASOT_Params *pP, 
953     const PAF_ASOT_Patchs *pQ, 
954     PAF_ASOT_Config *pC, 
955     Int z
958     PAF_AST_Config *pAstCfg;
959     Int as;                     /* Audio Stream Number (1, 2, etc.) */
960     Int ch;
961     //Int aLen;
962     Int aLen_int=0,aLen_ext=0;
963     Int aSize = sizeof(PAF_AudioData);
964     Int aAlign = aSize < sizeof (int) ? sizeof (int) : aSize;
965     Int maxFrameLength = pP->maxFramelength;
966     Int zX;
967     PAF_AudioData *aBuf_int=NULL;
968     PAF_AudioData *aBuf_ext=NULL;
969     XDAS_UInt8 *metadataBuf;
970     char i;
971     Error_Block    eb;
973     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
974     as = pAstCfg->as;
976     // Initialize error block
977     Error_init(&eb); 
979     // Compute maximum framelength (needed for ARC support)
980     maxFrameLength += PA_MODULO - maxFrameLength % PA_MODULO;
981     //aLen = numchan[z] * maxFrameLength;
982     for (i=0; i < numchan[z]; i++)
983     {
984         if (pP->pAudioFrameBufStatus->space[i] == IALG_SARAM)
985         {
986             aLen_int += maxFrameLength;
987         }
988         else
989         {
990             aLen_ext += maxFrameLength;
991         }
992     }
994     //
995     // Initialize audio frame elements directly
996     //
997     pAstCfg->xStr[z].pAudioFrame->fxns = pP->pAudioFrameFunctions;
998     pAstCfg->xStr[z].pAudioFrame->data.nChannels = PAF_MAXNUMCHAN; ///
999 ///    pAstCfg->xStr[z].pAudioFrame->data.nChannels = PAF_MAXNUMCHAN_AF;
1000     pAstCfg->xStr[z].pAudioFrame->data.nSamples = FRAMELENGTH;
1001     pAstCfg->xStr[z].pAudioFrame->data.sample = pAstCfg->xStr[z].audioFrameChannelPointers;
1002     pAstCfg->xStr[z].pAudioFrame->data.samsiz = pAstCfg->xStr[z].audioFrameChannelSizes;
1003     pAstCfg->xStr[z].pAudioFrame->pChannelConfigurationMaskTable = &PAF_ASP_stdCCMT;
1005     //
1006     // Allocate memory for and initialize pointers to audio data buffers
1007     //
1008     //   The NUMCHANMASK is used to identify the channels for which data
1009     //   buffers can be allocated. Using this mask and switch statement
1010     //   rather than some other construct allows efficient code generation,
1011     //   providing just the code necessary (with significant savings).
1012     //
1013     if (pP->fxns->bufMemPrint)
1014     {
1015         pP->fxns->bufMemPrint(z, aLen_int*aSize, HEAP_ID_FRMBUF, 2);
1016         pP->fxns->bufMemPrint(z, aLen_ext*aSize, HEAP_ID_EXTERNAL, 2);
1017     }
1019     TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc for audio buffers", as+z);
1020     
1021     if (aLen_int*aSize!=0) // check size != 0, otherwise malloc throws fatal error
1022     {
1023         if (!(aBuf_int = (PAF_AudioData *)Memory_calloc((IHeap_Handle)HEAP_FRMBUF, (aLen_int+(maxFrameLength-FRAMELENGTH))*aSize, aAlign, &eb))) //Qin: Add start offset
1024         {
1025             TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc failed", as+z);
1026             TRACE_TERSE2("  maxFrameLength: %d.  aLen_int*aSize: %d", maxFrameLength, (aLen_int+(maxFrameLength-FRAMELENGTH))*aSize);
1027             SW_BREAKPOINT;
1028             return __LINE__;
1029         }
1030     }
1031         
1032     if (aLen_ext*aSize!=0)
1033     {
1034         if (!(aBuf_ext = (PAF_AudioData *)Memory_calloc((IHeap_Handle)HEAP_EXTERNAL, (aLen_ext+(maxFrameLength-FRAMELENGTH))*aSize, aAlign, &eb)))//Qin: Add start offset
1035         {
1036             TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc failed", as+z);
1037             TRACE_TERSE2("  maxFrameLength: %d.  aLen_ext*aSize: %d", maxFrameLength, (aLen_ext+(maxFrameLength-FRAMELENGTH))*aSize);
1038             SW_BREAKPOINT;
1039             return __LINE__;
1040         }
1041     }
1042     
1043     TRACE_TERSE3("  maxFrameLength: %d.  aLen_int*aSize: %d.  aBuf_int: 0x%x", maxFrameLength, (aLen_int+(maxFrameLength-FRAMELENGTH))*aSize, (IArg)aBuf_int);
1044     TRACE_TERSE3("  maxFrameLength: %d.  aLen_ext*aSize: %d.  aBuf_ext: 0x%x", maxFrameLength, (aLen_ext+(maxFrameLength-FRAMELENGTH))*aSize, (IArg)aBuf_ext);
1046     TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc for metadata buffers", as+z);
1047     if (!(metadataBuf = (XDAS_UInt8 *)Memory_calloc((IHeap_Handle)HEAP_MDBUF, pP->pMetadataBufStatus->bufSize*pP->pMetadataBufStatus->NumBuf, pP->pMetadataBufStatus->alignment, &eb)))
1048     {
1049         TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc failed", as+z);
1050         TRACE_TERSE1("  bufSize*NumBuf: %d", pP->pMetadataBufStatus->bufSize*pP->pMetadataBufStatus->NumBuf);
1051         SW_BREAKPOINT;
1052         return __LINE__;
1053     }
1055     {
1056         Int i;
1058 #pragma UNROLL(1)
1059         for (i=0; i < PAF_MAXNUMCHAN_AF; i++)
1060         {
1061             pAstCfg->xStr[z].audioFrameChannelPointers[i] = NULL;
1062         }
1063     }
1065     // MID 314
1066     if((numchan[z] > PAF_MAXNUMCHAN) || (numchan[z] < 1)) 
1067     {
1068         TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: unsupported option", as+z);
1069         return __LINE__;
1070     }
1071     else 
1072     {
1073         Int j = 0;
1074         Int k = 0;
1075         TRACE_TERSE1("PAF_ASOT_initFrame0: AFChanPtrMap[%d][i]", numchan[z]);
1076         for(i=0;i<numchan[z];i++)
1077         {
1078             char chan = AFChanPtrMap[numchan[z]][i];
1079             if(chan != -1)
1080             {
1081                 if(pP->pAudioFrameBufStatus->space[i] == IALG_SARAM)
1082                 {
1083                     pAstCfg->xStr[z].audioFrameChannelPointers[chan] = aBuf_int + maxFrameLength*(j+1) - FRAMELENGTH;
1084                     j++;
1085                 }
1086                 else
1087                 {        
1088                     pAstCfg->xStr[z].audioFrameChannelPointers[chan] = aBuf_ext + maxFrameLength*(k+1) - FRAMELENGTH;
1089                     k++;
1090                 }    
1091                 TRACE_TERSE3("PAF_ASOT_initFrame0: chan = %d = AFChanPtrMap[%d][%d].", chan, numchan[z], i);
1092                 TRACE_TERSE2("PAF_ASOT_initFrame0: audioFrameChannelPointers[%d]: 0x%x", chan, (IArg)pAstCfg->xStr[z].audioFrameChannelPointers[chan]);
1093             }
1094         }
1095     }
1097     for (ch=PAF_LEFT; ch < PAF_MAXNUMCHAN_AF; ch++) 
1098     {
1099         if (pAstCfg->xStr[z].audioFrameChannelPointers[ch])
1100         {
1101             pAstCfg->xStr[z].origAudioFrameChannelPointers[ch] = pAstCfg->xStr[z].audioFrameChannelPointers[ch];
1102         }
1103     }
1105     //
1106     // Initialize meta data elements
1107     //
1108     pAstCfg->xStr[z].pAudioFrame->pafBsMetadataUpdate = XDAS_FALSE;
1109     pAstCfg->xStr[z].pAudioFrame->numPrivateMetadata = 0;
1110     pAstCfg->xStr[z].pAudioFrame->bsMetadata_offset = 0;
1111     pAstCfg->xStr[z].pAudioFrame->bsMetadata_type = PAF_bsMetadata_channelData;
1112     pAstCfg->xStr[z].pAudioFrame->privateMetadataBufSize = pP->pMetadataBufStatus->bufSize;
1113     for(i=0;i<pP->pMetadataBufStatus->NumBuf;i++)
1114     {
1115         pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].offset = 0;
1116         pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].size = 0;
1117         pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].pMdBuf = metadataBuf + pP->pMetadataBufStatus->bufSize*i;
1118     }
1120     //
1121     // Initialize decoder elements directly
1122     //
1124     for (zX = DECODE1; zX < DECODEN; zX++) 
1125     {
1126         if (pP->streamsFromDecodes[zX] == z) 
1127         {
1128 #ifdef NOAUDIOSHARE
1129             pAstCfg->xDec[zX].decodeInStruct.audioShare.nSamples = 0;
1130             pAstCfg->xDec[zX].decodeInStruct.audioShare.sample = NULL;
1131 #else /* NOAUDIOSHARE */
1132             pAstCfg->xDec[zX].decodeInStruct.audioShare.nSamples = aLen_int;
1133             pAstCfg->xDec[zX].decodeInStruct.audioShare.sample = aBuf_int;
1134 #endif /* NOAUDIOSHARE */
1135         }
1136     }
1138     return 0;
1139 } //PAF_ASOT_initFrame0
1141 // -----------------------------------------------------------------------------
1142 // ASOT Initialization Function Helper - Reinitialization of Audio Frame
1143 // AST Decoding Function              - Reinitialization of Audio Frame
1144 //
1145 //   Name:      PAF_ASOT_initFrame1
1146 //   Purpose:   Audio Stream Task Function for initialization or reinitiali-
1147 //              zation of the Audio Frame(s) by loading of data values of a
1148 //              time-varying nature.
1149 //   From:      audioStream1Task or equivalent
1150 //              AST Parameter Function -> decodeInfo
1151 //              AST Parameter Function -> decodeDecode
1152 //   Uses:      See code.
1153 //   States:    x
1154 //   Return:    0.
1155 //   Trace:     None.
1156 //
1157 Int
1158 PAF_ASOT_initFrame1(
1159     const PAF_ASOT_Params *pP, 
1160     const PAF_ASOT_Patchs *pQ, 
1161     PAF_ASOT_Config *pC, 
1162     Int z, 
1163     Int apply
1166     PAF_AST_Config *pAstCfg;
1168     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1170     //
1171     // Reinitialize audio frame elements:
1172     //
1173     //   Channel Configurations during sys init                 = Unknown
1174     //      "          "        during info or decode           = None
1175     //
1176     //   Sample Rate / Count    during sys init, info or decode = Unknown / 0
1177     //
1179     if (apply < 0) 
1180     {
1181         pAstCfg->xStr[z].pAudioFrame->channelConfigurationRequest.legacy = PAF_CC_UNKNOWN;
1182         pAstCfg->xStr[z].pAudioFrame->channelConfigurationStream.legacy = PAF_CC_UNKNOWN;
1183     }
1184     else 
1185     {
1186         pAstCfg->xStr[z].pAudioFrame->channelConfigurationRequest.legacy = PAF_CC_NONE;
1187         pAstCfg->xStr[z].pAudioFrame->channelConfigurationStream.legacy = PAF_CC_NONE;
1188     }
1190     if (apply < 1) 
1191     {
1192         pAstCfg->xStr[z].pAudioFrame->sampleRate = PAF_SAMPLERATE_UNKNOWN;
1193         pAstCfg->xStr[z].pAudioFrame->sampleCount = 0;
1194     }
1196     return 0;
1197 } //PAF_ASOT_initFrame1
1199 // -----------------------------------------------------------------------------
1200 // ASOT Selection Function - Output Device Selection
1201 //
1202 //   Name:      PAF_ASOT_selectDevices
1203 //   Purpose:   Audio Stream Output Task Function for selecting the devices used
1204 //              for output.
1205 //   From:      audioStream1Task or equivalent
1206 //   Uses:      See code.
1207 //   States:    x
1208 //   Return:    Error number in standard form (0 on success).
1209 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1210 //              * State information as per parent.
1211 //
1212 Int
1213 PAF_ASOT_selectDevices(
1214     const PAF_ASOT_Params *pP, 
1215     const PAF_ASOT_Patchs *pQ, 
1216     PAF_ASOT_Config *pC
1219     PAF_AST_Config *pAstCfg;
1220     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1221     Int z;                      /* input/output counter */
1222     Int errno = 0;              /* error number */
1223     Int errme;                  /* error number, local */
1224     Int device;
1226     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1227     as = pAstCfg->as;
1228     (void)as;  // clear compiler warning in case not used with tracing disabled
1230     // Select output devices
1231     for (z=OUTPUT1; z < OUTPUTN; z++) 
1232     {
1233         if ((device = pAstCfg->xOut[z].outBufStatus.sioSelect) >= 0) 
1234         {
1235             TRACE_VERBOSE2("PAF_ASOT_selectDevices: AS%d: output device %d selecting ...", as+z, device);
1237             /* check for valid index into device array */
1238             if (device >= pQ->devout->n)
1239             {
1240                 device = 0; /* treat as device None */
1241             }
1243             errme = pP->fxns->deviceSelect(&pAstCfg->xOut[z].hTxSio, SIO_OUTPUT, 
1244                 HEAP_ID_OUTBUF, (Ptr)pQ->devout->x[device]);
1245             if (errme)
1246             {
1247                 TRACE_VERBOSE2("PAF_ASOT_selectDevices: errme 0x%x, errno 0x%x", errme, errno);
1248                 if (!errno)
1249                 {
1250                     errno = ASPERR_DEVOUT + errme;
1251                 }
1252                 pAstCfg->xOut[z].outBufStatus.sioSelect = 0x80;
1253             }
1254             else 
1255             {
1256                 Int zE;
1258                 pAstCfg->xOut[z].outBufStatus.sioSelect = device | 0x80;
1259                 // register outBufStatus and encodeStatus pointers with output devices
1260                 // This enables proper IEC encapsulation.
1261                 if (pAstCfg->xOut[z].hTxSio) 
1262                 {
1263                     // set max # of output buffers (use override if necessary)
1264                     if (pAstCfg->xOut[z].outBufStatus.maxNumBufOverride == 0)
1265                     {
1266                         SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_MAX_NUMBUF,
1267                             (Arg)pP->poutNumBufMap[z]->maxNumBuf);
1268                     }
1269                     else
1270                     {
1271                         SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_MAX_NUMBUF,
1272                             (Arg)pAstCfg->xOut[z].outBufStatus.maxNumBufOverride);
1273                     }
1275                     // register PAF_SIO_IALG object address
1276                     SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_IALGADDR,
1277                         (Arg)pAstCfg->xOut[z].outChainData.head->alg);
1278                     SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_BUFSTATUSADDR, 
1279                         (Arg)&pAstCfg->xOut[z].outBufStatus);
1280                     for (zE=ENCODE1; zE < ENCODEN; zE++) 
1281                     {
1282                         if (pP->outputsFromEncodes[zE] == z) 
1283                         {
1284                             SIO_ctrl(pAstCfg->xOut[z].hTxSio, 
1285                                 PAF_SIO_CONTROL_SET_ENCSTATUSADDR, 
1286                                 (Arg)&pAstCfg->xEnc[zE].encodeStatus);
1287                             break;
1288                         }
1289                     }
1290                 }
1291             }
1292         }
1294         // if device selected and valid then enable stat tracking if
1295         // required and start clocking
1296         if ((pAstCfg->xOut[z].outBufStatus.sioSelect < 0) && (pAstCfg->xOut[z].hTxSio)) 
1297         {
1298             TRACE_VERBOSE0("PAF_ASOT_selectDevices: start SIO clocks");
1299             errme = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_OUTPUT_START_CLOCKS, 0);
1300             if (errme)
1301             {
1302                 TRACE_VERBOSE2("PAF_ASOT_selectDevices: errme 0x%x, errno 0x%x", errme, errno);
1303                 SIO_idle(pAstCfg->xOut[z].hTxSio);
1304                 if (!errno)
1305                 {
1306                     errno = ASPERR_DEVOUT + errme;
1307                 }
1308             }
1309         }
1310     }
1312     return errno;
1313 } //PAF_ASOT_selectDevices
1316 // -----------------------------------------------------------------------------
1317 // ASOT Processing Function - Decode Processing
1318 //
1319 //   Name:      PAF_ASOT_decodeProcessing
1320 //   Purpose:   Audio Stream Output Task Function for processing audio data.
1321 //
1322 Int
1323 PAF_ASOT_decodeProcessing(
1324     const PAF_ASOT_Params *pP, 
1325     const PAF_ASOT_Patchs *pQ, 
1326     PAF_ASOT_Config *pC, 
1327     Int sourceSelect
1330     PAF_AST_Config *pAstCfg;
1331     Int z;                              /* decode counter */
1332     Int errno;                          /* error number */
1333     Int getVal;
1334     enum { INIT, STREAM, ENCODE, FINAL, QUIT } state;
1335     state = INIT;
1336     errno = 0; /* error number */
1337     Int frame; // (***) FL: formerly -- decoder input frame count
1338     Int block; // decoder output block count / input frame
1339     Int outSioUpdate;
1340     
1341     
1342     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1344     for (;;) 
1345     {
1346         // FL: Check if any change in output SIO, e.g. from Output shortcut.
1347         // Changes will break FSM and allow Output reconfiguration.
1348         if (errno = checkOutSio(pP, pC, &outSioUpdate))
1349         {
1350             TRACE_TERSE1("PAF_ASOT_decodeProcessing: checkOutSio returned errno = 0x%04x", errno);
1351             break;
1352         }
1353         else if (outSioUpdate)
1354         {
1355             TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: Change in Output SIO selection");
1356             state = QUIT;
1357         }
1358         
1359         // Process commands (encode)
1360         if (getVal = pP->fxns->encodeCommand(pP, pQ, pC)) 
1361         {
1362             /* ignore */;
1363         }
1365         // Process state (decode)
1366         switch (state) 
1367         {
1368             case INIT: // initial state
1369                 gAsopInitCnt++;
1370                 Log_info0("TaskAsop: state=INIT");
1371             
1372                 // Reset audio frame pointers to original values
1373                 // (may be needed if error occurred).
1374                 for (z=STREAM1; z < STREAMN; z++) 
1375                 {
1376                     Int ch;
1377                     for (ch=PAF_LEFT; ch < PAF_MAXNUMCHAN_AF; ch++) 
1378                     {
1379                         if (pAstCfg->xStr[z].audioFrameChannelPointers[ch])
1380                         {
1381                             pAstCfg->xStr[z].audioFrameChannelPointers[ch] = 
1382                                 pAstCfg->xStr[z].origAudioFrameChannelPointers[ch];
1383                         }
1384                     }
1385                 }
1386                 
1387                 // Reset audio frame meta data elements
1388                 {
1389                     Int i;
1390                     
1391                     for (z=STREAM1; z < STREAMN; z++) 
1392                     {
1393                         pAstCfg->xStr[z].pAudioFrame->pafBsMetadataUpdate = XDAS_FALSE;
1394                         pAstCfg->xStr[z].pAudioFrame->numPrivateMetadata = 0;
1395                         pAstCfg->xStr[z].pAudioFrame->bsMetadata_offset = 0;
1396                         pAstCfg->xStr[z].pAudioFrame->bsMetadata_type = PAF_bsMetadata_channelData;
1398                         for (i=0; i<pP->pMetadataBufStatus->NumBuf; i++)
1399                         {
1400                             pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].offset = 0;
1401                             pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].size = 0;
1402                         }
1403                     }
1404                 }
1405                 
1406                 if (errno = pP->fxns->decodeInit(pP, pQ, pC, sourceSelect))
1407                 {
1408                     TRACE_VERBOSE1("PAF_ASOT_decodeProcessing: INIT, errno 0x%x.  break after decodeInit", errno);
1409                     break;
1410                 }
1411                 
1412                 // (***) FL: setup output (ASP chain reset, ENC reset, setCheckRateX, start output)
1413                 //           Contained in INFO1 in combined FSM.
1414                 // Establish secondary timing
1415                 if (errno = pP->fxns->decodeInfo1(pP, pQ, pC, frame, block))
1416                 {
1417                     TRACE_VERBOSE1("PAF_ASOT_decodeProcessing: INIT, errno 0x%x.  break after decodeInfo1", errno);
1418                     break;
1419                 }
1421                 frame = 0;
1422                 block = 0;
1424                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: INIT->STREAM");                
1425                 state = STREAM;
1426                 continue;
1427                 
1428             case STREAM: // stream state
1429                 gAsopStreamCnt++;
1430                 Log_info0("TaskAsop: state=STREAM");
1432                 if (errno = pP->fxns->decodeStream(pP, pQ, pC, frame, block))
1433                 {
1434                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: state: STREAM.  decodeStream err 0x%x", errno);
1435                     break;
1436                 }
1438                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: STREAM->ENCODE");
1439                 state = ENCODE;
1440                 continue;
1441                 
1442             case ENCODE: // encode state
1443                 gAsopEncodeCnt++;
1444                 Log_info0("TaskAsop: state=ENCODE");
1446                 if (errno = pP->fxns->decodeEncode(pP, pQ, pC, frame, block))
1447                 {
1448                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: state: ENCODE.  decodeEncode err 0x%x", errno);
1449                     break;
1450                 }
1453                 // (***) FL: do we need this? 
1454                 //       AF pointers come from CB read, any resets occur in Decoder AF.
1455                 // Reset audio frame pointers to original values
1456                 // (may have been adjusted by ARC or the like).
1457                 for (z=STREAM1; z < STREAMN; z++) 
1458                 {
1459                     Int ch;
1460                     for (ch=PAF_LEFT; ch < PAF_MAXNUMCHAN_AF; ch++) 
1461                     {
1462                         if (pAstCfg->xStr[z].audioFrameChannelPointers[ch])
1463                         {
1464                             pAstCfg->xStr[z].audioFrameChannelPointers[ch] = 
1465                                 pAstCfg->xStr[z].origAudioFrameChannelPointers[ch];
1466                         }
1467                     }
1468                 }
1470                 //
1471                 // (***) FL: update output (setCheckRateX)
1472                 //           Contained in INFO2 in combined FSM.
1473                 if (errno = pP->fxns->decodeInfo2(pP, pQ, pC, frame, block))
1474                 {
1475                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: ENCODE break on decodeInfo2. errno 0x%x", errno);
1476                     break;
1477                 }
1479                 block++;
1480                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: ENCODE->FINAL");
1481                 state = FINAL;
1482                 continue;
1483                 
1484             case FINAL:
1485                 gAsopFinalCnt++;
1486                 Log_info0("TaskAsop: state=FINAL");
1487                 
1488                 //
1489                 // (***) FL: this needs to be fixed.
1490                 //       (1) Only require selected Output to be in this FSM
1491                 //           => Dec Status checks aren't valid, 
1492                 //              will probably always exit FSM if only Output running
1493                 //       (2) Checking Dec Status info asych to input events (maybe ok)
1494                 //
1495 #if 0
1496                 // Check for final frame, and if indicated:
1497                 // - Update audio flag to cause output buffer flush rather than
1498                 //   the default truncate in "complete" processing.
1499                 // - Exit state machine to "complete" processing.
1500                 if (pP->fxns->decodeFinalTest(pP, pQ, pC, frame, block)) 
1501                 {
1502                     for (z=OUTPUT1; z < OUTPUTN; z++)
1503                     {
1504                         if ((pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_SOUND)
1505                         {
1506                             TRACE_VERBOSE0("PAF_ASOT_outputProcessing: state: FINAL: SOUND -> QUIET");
1507                             pAstCfg->xOut[z].outBufStatus.audio++; // SOUND -> QUIET
1508                         }
1509                     }
1510                     break;
1511                 }
1512 #endif                
1514                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: FINAL->STREAM");
1515                 state = STREAM;
1516                 continue;
1517                 
1518             case QUIT:
1519                 gAsopQuitCnt++;
1520                 Log_info0("TaskAsop: state=QUIT");
1522                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: QUIT");
1523                 errno = ASPERR_QUIT;
1524                 break;
1526             default: // unknown state
1527                 // Unknown:
1528                 // - Set error number registers.
1529                 // - Exit state machine to "complete" processing.
1531                 TRACE_TERSE1("PAF_ASOT_decodeProcessing: state: unknown, 0x%x", state);
1532                 errno = ASPERR_UNKNOWNSTATE;
1533                 break;
1535         }  // End of switch (state).
1536         
1537         TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: Calling decode complete");
1538         if (pP->fxns->decodeComplete(pP, pQ, pC, NULL, frame, block))
1539         {
1540             /* ignored? */;
1541         }
1542         
1543         return errno;
1544     } // End of for (;;)
1545         
1546     return errno;
1549 // -----------------------------------------------------------------------------
1550 // ASOT Decoding Function - Encode Command Processing
1551 //
1552 //   Name:      PAF_ASOT_encodeCommand
1553 //   Purpose:   Decoding Function for processing Encode Commands.
1554 //   From:      AST Parameter Function -> decodeProcessing
1555 //   Uses:      See code.
1556 //   States:    x
1557 //   Return:    0.
1558 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1559 //              * Command execution.
1560 //              * SIO control errors.
1561 //              * Error number macros.
1562 //
1563 Int
1564 PAF_ASOT_encodeCommand(
1565     const PAF_ASOT_Params *pP, 
1566     const PAF_ASOT_Patchs *pQ, 
1567     PAF_ASOT_Config *pC
1570     PAF_AST_Config *pAstCfg;
1571     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1572     Int z;                      /* encode counter */
1573     Int errno = 0;              /* error number */
1574     Int zO, zS;
1577     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1578     as = pAstCfg->as;
1580     for (z=ENCODE1; z < ENCODEN; z++) 
1581     {
1582         zO = pP->outputsFromEncodes[z];
1583         zS = pP->streamsFromEncodes[z];
1584         if (! (pAstCfg->xEnc[z].encodeStatus.command2 & 0x80)) 
1585         {
1586             switch (pAstCfg->xEnc[z].encodeStatus.command2) 
1587             {
1588                 case 0: // command none - process
1589                     pAstCfg->xEnc[z].encodeStatus.command2 |= 0x80;
1590                     break;
1591                 case 1: // mute command
1592                     TRACE_VERBOSE2("AS%d: PAF_ASOT_encodeCommand: encode command mute (0x%02x)", as+zS, 1);
1593                     if ((pAstCfg->xOut[zO].outBufStatus.audio & 0x0f) != PAF_OB_AUDIO_QUIET
1594                         && pAstCfg->xOut[zO].hTxSio
1595                         && (errno = SIO_ctrl (pAstCfg->xOut[zO].hTxSio, PAF_SIO_CONTROL_MUTE, 0))) 
1596                     {
1597                         errno = (errno & 0xff) | ASPERR_MUTE;
1598                         /* convert to sensical errno */
1599                         TRACE_TERSE1("AS%d: PAF_ASOT_encodeCommand: SIO control failed (mute)", as+zS);
1600                         TRACE_TERSE2("AS%d: PAF_ASOT_encodeCommand: errno = 0x%04x <ignored>", as+zS, errno);
1601                     }
1602                     else 
1603                     {
1604                         pAstCfg->xOut[zO].outBufStatus.audio |= PAF_OB_AUDIO_MUTED;
1605                     }
1606                     pAstCfg->xEnc[z].encodeStatus.command2 |= 0x80;
1607                     break;
1608                 case 2: // unmute command
1609                     TRACE_VERBOSE2("AS%d: PAF_ASOT_encodeCommand: encode command unmute (0x%02x)", as+zS, 2);
1610                     if ((pAstCfg->xOut[zO].outBufStatus.audio & 0x0f) != PAF_OB_AUDIO_QUIET
1611                         && pAstCfg->xOut[zO].hTxSio
1612                         && (errno = SIO_ctrl (pAstCfg->xOut[zO].hTxSio, PAF_SIO_CONTROL_UNMUTE, 0))) 
1613                     {
1614                         errno = (errno & 0xff) | ASPERR_MUTE;
1615                         /* convert to sensical errno */
1616                         TRACE_TERSE1("AS%d: PAF_ASOT_encodeCommand: SIO control failed (unmute)", as+zS);
1617                         TRACE_TERSE2("AS%d: PAF_ASOT_encodeCommand: errno = 0x%04x <ignored>", as+zS, errno);
1618                     }
1619                     else 
1620                     {
1621                         pAstCfg->xOut[zO].outBufStatus.audio &= ~PAF_OB_AUDIO_MUTED;
1622                     }
1623                     pAstCfg->xEnc[z].encodeStatus.command2 |= 0x80;
1624                     break;
1625                 default: // command unknown - ignore
1626                     break;
1627             }
1628         }
1629     }
1631     ERRNO_RPRT (TaskAsop, errno);
1633     return 0;
1634 } //PAF_ASOT_encodeCommand
1636 //   Purpose:   Decoding Function for reinitializing the decoding process.
1637 Int
1638 PAF_ASOT_decodeInit(
1639     const PAF_ASOT_Params *pP, 
1640     const PAF_ASOT_Patchs *pQ, 
1641     PAF_ASOT_Config *pC, 
1642     Int sourceSelect
1645     PAF_AST_Config *pAstCfg;
1646     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
1647     Int as;                             /* Audio Stream Number (1, 2, etc.) */
1648     Int z;                              /* decode/encode counter */
1649     Int errno;                          /* error number */
1650     Int zO, zS;
1653     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1654     as = pAstCfg->as;
1656     pCbCtl = &pC->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
1658     for (z=DECODE1; z < DECODEN; z++)
1659     {
1660         // Start decoder output circular buffer reads
1661         errno = cbReadStart(pCbCtl, z);
1662         if (errno)
1663         {
1664             TRACE_TERSE1("PAF_ASOT_decodeInit:cbReadStart() error=%d", errno);
1665             SW_BREAKPOINT; // FL: debug
1666             return errno;
1667         }
1668         gCbReadAfErr=0;         // reset read circular buffer error count
1669         gDecOpCbRdAfUnd=0;      // reset decoder output circular buffer underflow count
1670         gMaxDecOpCbRdAfUnd=0;   // reset max decoder output circular buffer underflow count
1672         // FL: debug
1673         cbLog(pCbCtl, z, 1, "PAF_ASOT_decodeInit:cbReadStart");
1674     }
1675     
1676     // TODO: move this to start of this function so that it doesn't affect IO timing
1677     for (z=ENCODE1; z < ENCODEN; z++) 
1678     {
1679         zO = pP->outputsFromEncodes[z];
1680         zS = pP->streamsFromEncodes[z];
1681         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
1682         {
1683             Int select = pAstCfg->xEnc[z].encodeStatus.select;
1684             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
1685             ENC_Handle enc = (ENC_Handle )encAlg;
1686             TRACE_VERBOSE1("AS%d: PAF_ASOT_decodeInit: initializing encode", as+zS);
1687             if (encAlg->fxns->algActivate)
1688             {
1689                 encAlg->fxns->algActivate (encAlg);
1690             }
1691             if (enc->fxns->reset
1692                 && (errno = enc->fxns->reset(enc, NULL,
1693                     &pAstCfg->xEnc[z].encodeControl, &pAstCfg->xEnc[z].encodeStatus)))
1694             {
1695                 return errno;
1696             }
1697         }
1698     }
1699     
1700     
1701     return 0;
1704 // -----------------------------------------------------------------------------
1705 // ASOT Decoding Function - Info Processing, Initial
1706 //
1707 //   Name:      PAF_ASOT_decodeInfo1
1708 //   Purpose:   Decoding Function for processing information in a manner that
1709 //              is unique to initial frames of input data.
1710 //   From:      AST Parameter Function -> decodeProcessing
1711 //   Uses:      See code.
1712 //   States:    x
1713 //   Return:    Error number in standard or SIO form (0 on success).
1714 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1715 //              * State information as per parent.
1716 //
1717 Int
1718 PAF_ASOT_decodeInfo1(
1719     const PAF_ASOT_Params *pP, 
1720     const PAF_ASOT_Patchs *pQ, 
1721     PAF_ASOT_Config *pC, 
1722     Int frame, 
1723     Int block
1726     PAF_AST_Config *pAstCfg;
1727     Int z;                              /* decode/encode counter */
1728     Int errno;                          /* error number */
1730     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1732     // run the chain of ASP's on the stream.
1733     TRACE_VERBOSE0("PAF_ASOT_decodeInfo1: calling streamChainFunction.");
1734     if (errno = pP->fxns->streamChainFunction(pP, pQ, pC, 
1735         PAF_ASP_CHAINFRAMEFXNS_RESET, 1, frame))
1736     {
1737         TRACE_TERSE1("PAF_ASOT_decodeInfo1: streamChainFunction returns errno 0x%x ", errno);
1738         return errno;
1739     }
1741     TRACE_VERBOSE0("PAF_ASOT_decodeInfo1: calling enc->info.");
1742     for (z=ENCODE1; z < ENCODEN; z++) 
1743     {
1744         Int zO = pP->outputsFromEncodes[z];
1745         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
1746         {
1747             Int select = pAstCfg->xEnc[z].encodeStatus.select;
1748             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
1749             ENC_Handle enc = (ENC_Handle )encAlg;
1750             if (enc->fxns->info
1751                 && (errno = enc->fxns->info(enc, NULL,
1752                     &pAstCfg->xEnc[z].encodeControl, &pAstCfg->xEnc[z].encodeStatus)))
1753             {
1754                 TRACE_TERSE1("PAF_ASOT_decodeInfo1: info returns errno 0x%x ", errno);
1755                 return errno;
1756             }
1757         }
1758     }
1760     if (errno = pP->fxns->setCheckRateX(pP, pQ, pC, 0))
1761     {
1762         // ignore if rateX has changed since we haven't, but are about to,
1763         // start the output. If we didn't ignore this case then the state machine
1764         // would restart unnecessarily, e.g. in the case of SRC, resulting in
1765         // added latency.
1766         if (errno != ASPERR_INFO_RATECHANGE)
1767         {
1768             TRACE_TERSE1("PAF_ASOT_decodeInfo1: setCheckRateX returns errno 0x%x, not RATECHANGE", errno);
1769             return errno;
1770         }
1771         else
1772         {
1773             TRACE_TERSE0("PAF_ASOT_decodeInfo1: RATECHANGE returns RATECHANGE, ignoring");
1774         }
1775     }
1777     if (errno = pP->fxns->startOutput(pP, pQ, pC)) 
1778     {
1779         if (errno == 0x105) 
1780         {
1781             TRACE_TERSE1("PAF_ASOT_decodeInfo1: startOutput returns RING BUFFER FULL (0x%x)", errno);
1782         }
1783         else
1784         {
1785             TRACE_TERSE1("PAF_ASOT_decodeInfo1: startOutput returns errno 0x%x", errno);
1786         }
1787         return errno;
1788     }    
1789     
1790     return 0;
1793 // -----------------------------------------------------------------------------
1794 // ASOT Decoding Function - Info Processing, Subsequent
1795 //
1796 //   Name:      PAF_ASOT_decodeInfo2
1797 //   Purpose:   Decoding Function for processing information in a manner that
1798 //              is unique to frames of input data other than the initial one.
1799 //   From:      AST Parameter Function -> decodeProcessing
1800 //   Uses:      See code.
1801 //   States:    x
1802 //   Return:    Error number in standard form (0 on success).
1803 //   Trace:     None.
1804 //
1805 Int
1806 PAF_ASOT_decodeInfo2(
1807     const PAF_ASOT_Params *pP, 
1808     const PAF_ASOT_Patchs *pQ, 
1809     PAF_ASOT_Config *pC, 
1810     Int frame, 
1811     Int block
1814     //PAF_AST_Config *pAstCfg;
1815     Int errno;
1817     
1818     //pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1820     errno = pP->fxns->setCheckRateX (pP, pQ, pC, 1);
1821     TRACE_VERBOSE1("PAF_ASOT_decodeInfo2: return 0x%x", errno);
1822     return errno;
1823 } //PAF_ASOT_decodeInfo2
1825 // -----------------------------------------------------------------------------
1826 // ASOT Decoding Function - Stream Processing
1827 //
1828 //   Name:      PAF_ASOT_decodeStream
1829 //   Purpose:   Decoding Function for processing of audio frame data by the
1830 //              ASP Algorithms.
1831 //   From:      AST Parameter Function -> decodeProcessing
1832 //   Uses:      See code.
1833 //   States:    x
1834 //   Return:    Error number in standard form (0 on success).
1835 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1836 //              * State information as per parent/child.
1837 //
1838 Int
1839 PAF_ASOT_decodeStream(
1840     const PAF_ASOT_Params *pP, 
1841     const PAF_ASOT_Patchs *pQ, 
1842     PAF_ASOT_Config *pC, 
1843     Int frame, 
1844     Int block
1847     PAF_AST_Config *pAstCfg;
1848     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
1849     Int z;                              /* decode/stream counter */
1850     PAF_AudioFrame *pAfRd;
1851     Int cbErrno;
1852     Int errno;
1855     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1856     
1857     pCbCtl = &pC->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
1859     for (z=DECODE1; z < DECODEN; z++) 
1860     {
1861         Int zS = pP->streamsFromDecodes[z];
1862         
1863         //
1864         // Read decoder output circular buffer
1865         //
1866         pAfRd = pAstCfg->xStr[zS].pAudioFrame;
1867         cbErrno = cbReadAf(pCbCtl, z, pAfRd);
1868         if ((cbErrno < 0) && (cbErrno != ASP_DECOP_CB_READ_UNDERFLOW))
1869         {
1870             gCbReadAfErr++;
1871             TRACE_TERSE1("PAF_ASOT_decodeStream:cbReadAf() error=%d", cbErrno);
1872             //SW_BREAKPOINT; // FL: debug
1873             return cbErrno;
1874         }
1876         // Handle underflows
1877         if (cbErrno == ASP_DECOP_CB_READ_UNDERFLOW)
1878         {
1879             // FL: some number of underflows alway occur on start of stream when ASOT only depends on configured Output.
1880             //     DDP: ~2 underflows
1881             //     MAT-THD: ~16 underflows
1882             // Need to check behavior of cbReset().
1883             // Need to check behavior on exit/re-entry into Output processing.
1884             
1885             gDecOpCbRdAfUnd++; // increment circular buffer underflow count
1886             if (gDecOpCbRdAfUnd >= DEC_OP_CB_RDAF_UND_THR) 
1887             {
1888                 // Underflow count above threshold.
1889                 // (1) set max underflow count to threshold
1890                 // (2) reset underflow count
1891                 // (3) reset circular buffer
1892                 
1893                 gMaxDecOpCbRdAfUnd = DEC_OP_CB_RDAF_UND_THR; // update max underflow count
1894                 gDecOpCbRdAfUnd = 0; // reset underflow count
1896                 // Reset circular buffer
1897                 cbReset(pCbCtl, z);
1898                 Log_info0("PAF_ASOT_decodeStream:cbReset");
1899             
1900                 return cbErrno;
1901             }
1902         }
1903         else if ((cbErrno == ASP_DECOP_CB_SOK) && (gDecOpCbRdAfUnd > 0))
1904         {
1905             // No underflow detected.
1906             // update max underflow count,
1907             // reset underflow count
1908             
1909             // update max underflow count
1910             if (gDecOpCbRdAfUnd > gMaxDecOpCbRdAfUnd)
1911             {
1912                 gMaxDecOpCbRdAfUnd = gDecOpCbRdAfUnd;
1913             }
1914             gDecOpCbRdAfUnd = 0; // reset circular buffer underflow count
1915         }
1916         //Log_info0("PAF_ASOT_decodeStream:cbReadAf() complete.");
1918         // FL: debug
1919         cbLog(pCbCtl, z, 1, "PAF_ASOT_decodeStream:cbReadAf");
1920         //if (capAfWrite(pAfRd, 0) != CAP_AF_SOK)
1921         //{
1922         //    Log_info0("capAfWrite() error");
1923         //}
1924     }
1925             
1926     TRACE_VERBOSE0("PAF_ASOT_outputStream: calling streamChainFunction.");
1927     errno = pP->fxns->streamChainFunction(pP, pQ, pC, 
1928         PAF_ASP_CHAINFRAMEFXNS_APPLY, 1, block);
1929     if (errno)
1930     {
1931         TRACE_TERSE1("PAF_ASOT_outputStream: streamChainFunction returns errno 0x%x ", errno);
1932         return errno;
1933     }
1935     return 0;
1937 } //PAF_ASOT_decodeStream
1939 // -----------------------------------------------------------------------------
1940 // ASOT Decoding Function - Encode Processing
1941 //
1942 //   Name:      PAF_ASOT_decodeEncode
1943 //   Purpose:   Decoding Function for processing of audio frame data by the
1944 //              Encode Algorithm.
1945 //   From:      AST Parameter Function -> decodeProcessing
1946 //   Uses:      See code.
1947 //   States:    x
1948 //   Return:    Error number in standard or SIO form (0 on success).
1949 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1950 //              * State information as per parent.
1951 //
1952 Int
1953 PAF_ASOT_decodeEncode(
1954     const PAF_ASOT_Params *pP, 
1955     const PAF_ASOT_Patchs *pQ, 
1956     PAF_ASOT_Config *pC, 
1957     Int frame, 
1958     Int block
1961     PAF_AST_Config *pAstCfg;
1962     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1963     Int z;                      /* encode/output counter */
1964     Int errno;                  /* error number */
1965     Int zX, zE, zS;
1966     UInt32 curTime;
1968     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1969     as = pAstCfg->as;
1971     // Await output buffers (but not first time)
1972     for (z=OUTPUT1; z < OUTPUTN; z++) 
1973     {
1974         // determine encoder associated with this output
1975         zE = z;
1976         for (zX = ENCODE1; zX < ENCODEN; zX++) 
1977         {
1978             if (pP->outputsFromEncodes[zX] == z) 
1979             {
1980                 zE = zX;
1981                 break;
1982             }
1983         }
1984         zS = pP->streamsFromEncodes[zE];
1986         if (pAstCfg->xOut[z].hTxSio) 
1987         {
1988             // update length (e.g. ARC may have changed)
1989             pAstCfg->xOut[z].outBufConfig.lengthofFrame = 
1990                 pAstCfg->xEnc[zE].encodeInStruct.pAudioFrame->sampleCount;
1991             TRACE_GEN2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- idle", as+zS, block);
1992             errno = SIO_reclaim(pAstCfg->xOut[z].hTxSio,(Ptr *) &pAstCfg->xOut[z].pOutBuf, NULL);
1993             if (errno < 0)
1994             {
1995                 SIO_idle(pAstCfg->xOut[z].hTxSio);
1996                 TRACE_TERSE2("PAF_ASOT_decodeEncode: AS%d: SIO_reclaim returns error %d", as+zS, -errno);
1997                 return -errno; // SIO negates error codes
1998             }
1999             // TODO: use pC->xOut[z].pOutBuf in following ->encode call
2001 #if 1 // (***) FL: shows timing of Input Rx SIO reclaim after decoding has started (autodet complete)
2002             // (***) debug // B8
2003             {
2004                 static Uint8 toggleState = 0;
2005                 if (toggleState == 0)
2006                     GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106);
2007                 else
2008                     GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);
2009                 toggleState = ~(toggleState);
2010             }
2011 #endif            
2013             //
2014             // Simulate Tx SIO_reclaim() pend
2015             //
2016             //Semaphore_pend(semaphoreTxAudio, BIOS_WAIT_FOREVER); 
2017             gTaskAsopCnt++;
2018             curTime = Clock_getTicks();
2019             //System_printf("System time in TaskAsipFxn Tx audio = %lu\n", (ULong)curTime);
2020             //Log_info1("outputEncode():Tx SIO reclaim(), system time = %u", curTime);
2021         }
2022         else 
2023         {
2024             TRACE_VERBOSE2("AS%d: PAF_ASOT_decodeEncode: processing block %d -- idle <ignored>", as+zS, block);
2025         }
2026     }
2028     // Encode data
2029     for (z=ENCODE1; z < ENCODEN; z++) 
2030     {
2031         Int zO = pP->outputsFromEncodes[z];
2032         Int zS = pP->streamsFromEncodes[z];
2033         (void)zS; // clear compiler warning in case not used with tracing disabled
2034         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2035         {
2036             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2037             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2038             ENC_Handle enc = (ENC_Handle )encAlg;
2039             if (select != pAstCfg->xEnc[z].encodeControl.encActive)
2040             {
2041                 pAstCfg->xEnc[z].encodeControl.encActive = select;
2042                 TRACE_TERSE0("PAF_ASOT_decodeEncode: return error");
2043                 return (-1);
2044             }
2045             TRACE_GEN2("AS%d: PAF_ASOT_decodeEncode: processing block %d -- encode", as+zS, block);
2047             // (MID 1933) temp. workaround for PCE2
2048             pAstCfg->xEnc[z].encodeInStruct.pAudioFrame->data.nChannels = PAF_MAXNUMCHAN;
2050           /*
2051           #if (CURRENT_TRACE_MASK & TRACE_MASK_DATA)
2052             {
2053                 PAF_AudioFrame *pAudioFrame = pC->xEnc[z].encodeInStruct.pAudioFrame;
2054                 int *wp;
2055                 wp = (int*)pAudioFrame->data.sample[0];
2056                 TRACE_DATA((&TR_MOD, "as1-f2: AS%d PAF_ASOT_outputEncode: encoding from ch 0 0x%x. line %d", z, wp, __LINE__));
2057                 TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x (ch0)", wp[0], wp[16], wp[99]));
2058                 wp = (int*)pAudioFrame->data.sample[1];
2059                 TRACE_DATA((&TR_MOD, "as1-f2: PAF_ASOT_outputEncode: encoding from ch 1 0x%x. line %d", wp, __LINE__));
2060                 TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x (ch1)", wp[0], wp[16], wp[99]));
2061                 wp = (int*)pAudioFrame->data.sample[2];
2062                 TRACE_DATA((&TR_MOD, "as1-f2: PAF_ASOT_outputEncode: encoding from ch 2 0x%x. line %d", wp, __LINE__));
2063                 TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x (ch2)", wp[0], wp[16], wp[99]));
2064             }
2065           #endif
2066           */
2068             if (enc->fxns->encode)
2069             {
2070                 pAstCfg->xEnc[z].encodeOutStruct.bypassFlag =
2071                         pP->z_pEncodeStatus[z]->encBypass;
2072                 if (errno = enc->fxns->encode(enc, NULL, 
2073                     &pAstCfg->xEnc[z].encodeInStruct, &pAstCfg->xEnc[z].encodeOutStruct))
2074                 {
2075                     if (errno != PCEERR_OUTPUT_POINTERNULL)
2076                     {
2077                         TRACE_TERSE1("PAF_ASOT_decodeEncode: return error %d line %d", errno);
2078                         return errno;
2079                     }
2080                 }
2081             /*  #if (CURRENT_TRACE_MASK & TRACE_MASK_DATA)
2082                 else
2083                 {
2084                     int *wp = (int*)pC->xOut[z].pOutBuf->pntr.pVoid;
2085                     TRACE_DATA((&TR_MOD, "as1-f2: PAF_ASOT_outputEncode: encoded to 0x%x. line %d", wp, __LINE__));
2086                     TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x", wp[0], wp[16], wp[99]));
2087                 }
2088               #endif
2089               */
2090             }
2091         }
2092         else 
2093         {
2094             TRACE_VERBOSE2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- encode <ignored>",
2095                 as+pP->streamsFromEncodes[z], block);
2096         }
2097     }
2099     // Transmit data
2100     for (z=OUTPUT1; z < OUTPUTN; z++) 
2101     {
2102         // determine encoder associated with this output
2103         zE = z;
2104         for (zX = ENCODE1; zX < ENCODEN; zX++) 
2105         {
2106             if (pP->outputsFromEncodes[zX] == z) 
2107             {
2108                 zE = zX;
2109                 break;
2110             }
2111         }
2112         zS = pP->streamsFromEncodes[zE];
2114         if (pAstCfg->xOut[z].hTxSio) 
2115         {
2116             TRACE_GEN2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- output", as+zS, block);
2117             errno = SIO_issue(pAstCfg->xOut[z].hTxSio, 
2118                 &pAstCfg->xOut[z].outBufConfig, sizeof (pAstCfg->xOut[z].outBufConfig), 0);
2119             if (errno)
2120             {
2121                 SIO_idle(pAstCfg->xOut[z].hTxSio);
2122                 if (errno == 0x105)     // 0x105 == RINGIO_EBUFFULL
2123                 {
2124 //                    statStruct_LogFullRing(STATSTRUCT_AS1_F2);
2125                     TRACE_TERSE1("PAF_ASOT_decodeEncode: SIO_idle returned RINGIO_EBUFFULL (0x%x)", errno);
2126                 }
2127                 if (errno > 0)
2128                 {
2129                     TRACE_TERSE1("PAF_ASOT_decodeEncode: return error 0x%x line %d", errno);
2130                     return (ASPERR_ISSUE + (z << 4));
2131                 }
2132                 else if (errno < 0)
2133                 {
2134                     TRACE_TERSE1("PAF_ASOT_decodeEncode: return neg error 0x%x line %d", -errno);
2135                     return -errno; // SIO negates error codes
2136                 }
2137             }
2138             if (errno > 0)
2139             {
2140                 return (ASPERR_ISSUE + (z << 4));
2141             }
2142             else if (errno < 0)
2143             {
2144                 return -errno; // SIO negates error codes
2145             }
2146         }
2147         else 
2148         {
2149             TRACE_GEN2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- output <ignored>", as+zS, block);
2150         }
2151     }
2153     return 0;
2154 } //PAF_ASOT_decodeEncode
2156 // -----------------------------------------------------------------------------
2157 // ASOT Decoding Function - Stream-Final Processing
2158 //
2159 //   Name:      PAF_ASOT_decodeComplete
2160 //   Purpose:   Decoding Function for terminating the decoding process.
2161 //   From:      AST Parameter Function -> decodeProcessing
2162 //   Uses:      See code.
2163 //   States:    x
2164 //   Return:    0.
2165 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2166 //              * State information as per parent.
2167 //
2168 Int
2169 PAF_ASOT_decodeComplete(
2170     const PAF_ASOT_Params *pP, 
2171     const PAF_ASOT_Patchs *pQ, 
2172     PAF_ASOT_Config *pC, 
2173     ALG_Handle decAlg[], 
2174     Int frame, 
2175     Int block
2178     PAF_AST_Config *pAstCfg;
2179     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
2180     Int as;                             /* Audio Stream Number (1, 2, etc.) */
2181     Int z;                              /* decode/encode counter */
2182     Int errno;                          /* error number */
2184     
2185     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
2186     as = pAstCfg->as;
2187     (void)as;  // clear compiler warning in case not used with tracing disabled
2189     pCbCtl = &pC->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
2190     
2191     for (z=DECODE1; z < DECODEN; z++)
2192     {
2193         // Stop decoder output circular buffer reads
2194         errno = cbReadStop(pCbCtl, z);
2195         if (errno)
2196         {
2197             TRACE_TERSE1("PAF_ASOT_decodeComplete:cbReadStop() error=%d", errno);
2198             SW_BREAKPOINT; // FL: debug
2199             return errno;
2200         }
2201         // FL: debug
2202         cbLog(pCbCtl, z, 1, "PAF_ASOT_decodeComplete:cbReadStop");
2203     }
2204     
2205     pP->fxns->streamChainFunction(pP, pQ, pC, PAF_ASP_CHAINFRAMEFXNS_FINAL, 0, frame);
2207     for (z=ENCODE1; z < ENCODEN; z++) 
2208     {
2209         Int zO = pP->outputsFromEncodes[z];
2210         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2211         {
2212             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2213             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2214 #ifdef PAF_ASP_FINAL
2215             ENC_Handle enc = (ENC_Handle)encAlg;
2216 #endif /* PAF_ASP_FINAL */
2217             TRACE_VERBOSE1("PAF_ASOT_decodeComplete: AS%d: finalizing encode", as+z);
2218 #ifdef PAF_ASP_FINAL
2219             if (enc->fxns->final)
2220                 enc->fxns->final(enc, NULL, &pAstCfg->xEnc[z].encodeControl,
2221                                  &pAstCfg->xEnc[z].encodeStatus);
2222 #endif /* PAF_ASP_FINAL */
2223             if (encAlg->fxns->algDeactivate)
2224             {
2225                 encAlg->fxns->algDeactivate(encAlg);
2226             }
2227         }
2228         else 
2229         {
2230             TRACE_VERBOSE1("PAF_ASOT_decodeComplete: AS%d: finalizing encode <ignored>", as+z);
2231         }
2232     }
2234     // wait for remaining data to be output
2235     pP->fxns->stopOutput(pP, pQ, pC);
2237     return 0;
2238 } //PAF_ASOT_decodeComplete
2240 // -----------------------------------------------------------------------------
2241 // ASOT Decoding Function Helper - SIO Driver Start
2242 //
2243 //   Name:      PAF_ASOT_startOutput
2244 //   Purpose:   Decoding Function for initiating output.
2245 //   From:      AST Parameter Function -> decodeInfo1
2246 //   Uses:      See code.
2247 //   States:    x
2248 //   Return:    Error number in standard or SIO form (0 on success).
2249 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2250 //              * State information as per parent.
2251 //              * SIO control errors.
2252 //
2253 #define DEC_OUTNUMBUF_MAP(X) \
2254       pP->poutNumBufMap[z]->map[(X) >= pP->poutNumBufMap[z]->length ? 0 : (X)]
2256 Int
2257 PAF_ASOT_startOutput(
2258     const PAF_ASOT_Params *pP, 
2259     const PAF_ASOT_Patchs *pQ, 
2260     PAF_ASOT_Config *pC
2261
2263     PAF_AST_Config *pAstCfg;
2264     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2265     Int z;                      /* output counter */
2266     Int errno,nbufs;            /* error number */
2267     Int zE, zS, zX;
2268     Int zMD;
2269     PAF_SIO_IALG_Obj    *pObj;
2270     PAF_SIO_IALG_Config *pAlgConfig;
2272     
2273     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
2274     as = pAstCfg->as;
2275     zMD = pAstCfg->masterDec;
2277     for (z=OUTPUT1; z < OUTPUTN; z++) 
2278     {
2279         if (pAstCfg->xOut[z].hTxSio) 
2280         {
2281             // determine associated encoder and stream
2282             zE = z;
2283             zS = z;
2284             for (zX = ENCODE1; zX < ENCODEN; zX++) 
2285             {
2286                 if (pP->outputsFromEncodes[zX] == z) 
2287                 {
2288                     zE = zX;
2289                     zS = pP->streamsFromEncodes[zE];
2290                     break;
2291                 }
2292             }
2294             // Set sample count so that DOB knows how much data to send
2295             pAstCfg->xOut[z].outBufConfig.lengthofFrame =
2296                 pAstCfg->xEnc[zE].encodeInStruct.pAudioFrame->sampleCount;
2298             if (pAstCfg->xOut[z].outBufStatus.markerMode == PAF_OB_MARKER_ENABLED) 
2299             {
2300                 pObj = (PAF_SIO_IALG_Obj *) pAstCfg->xOut[z].outChainData.head->alg;
2301                 pAlgConfig = &pObj->config;
2302                 memset(pAstCfg->xOut[z].outBufConfig.base.pVoid, 0xAA, 
2303                     pAlgConfig->pMemRec[0].size);
2304             }
2306             // The index to DEC_OUTNUMBUF_MAP will always come from the primary/master
2307             // decoder. How should we handle the sourceProgram for multiple decoders?
2308             // Override as needed
2309             nbufs = DEC_OUTNUMBUF_MAP(pAstCfg->xDec[zMD].decodeStatus.sourceProgram);
2310             if (pAstCfg->xOut[z].outBufStatus.numBufOverride[pAstCfg->xDec[zMD].decodeStatus.sourceProgram] > 0)
2311             {
2312                 nbufs = pAstCfg->xOut[z].outBufStatus.numBufOverride[pAstCfg->xDec[zMD].decodeStatus.sourceProgram];
2313             }
2314             SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_NUMBUF, nbufs);
2316             if (errno = SIO_issue(pAstCfg->xOut[z].hTxSio,
2317                 &pAstCfg->xOut[z].outBufConfig, sizeof(pAstCfg->xOut[z].outBufConfig), 0)) 
2318             {
2319                 SIO_idle(pAstCfg->xOut[z].hTxSio);
2320                 TRACE_TERSE2("PAF_ASOT_startOutput: AS%d: SIO_issue failed (0x%x)", as+zS, errno);
2321                 return errno;
2322             }
2324             if (!(pAstCfg->xOut[z].outBufStatus.audio & 0xf0) && 
2325                 (errno =  SIO_ctrl (pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_UNMUTE, 0))) 
2326             {
2327                 errno = (errno & 0xff) | ASPERR_MUTE;
2328                 /* convert to sensical errno */
2329                 TRACE_TERSE2("as1-f2: PAF_ASOT_startOutput: AS%d: SIO control failed (unmute) 0x%x", as+zS, errno);
2330                 return (errno);
2331             }
2332             else
2333             {
2334                 pAstCfg->xOut[z].outBufStatus.audio
2335                     = (pAstCfg->xOut[z].outBufStatus.audio & 0xf0) | PAF_OB_AUDIO_SOUND;                
2336             }
2338             TRACE_VERBOSE1("PAF_ASOT_startOutput: AS%d: output started", as+zS);
2339         }
2340     }
2342     return 0;
2343 } //PAF_ASOT_startOutput
2345 // -----------------------------------------------------------------------------
2346 // ASOT Decoding Function Helper - SIO Driver Stop
2347 //
2348 //   Name:      PAF_ASOT_stopOutput
2349 //   Purpose:   Decoding Function for terminating output.
2350 //   From:      AST Parameter Function -> decodeProcessing
2351 //              AST Parameter Function -> decodeComplete
2352 //   Uses:      See code.
2353 //   States:    x
2354 //   Return:    Error number in standard or SIO form (0 on success).
2355 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2356 //              * SIO control errors.
2357 //
2358 Int
2359 PAF_ASOT_stopOutput(
2360     const PAF_ASOT_Params *pP, 
2361     const PAF_ASOT_Patchs *pQ, 
2362     PAF_ASOT_Config *pC
2365     PAF_AST_Config *pAstCfg;
2366     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2367     Int z;                      /* output counter */
2368     Int errno = 0, getVal;
2369     Int zS, zX;
2370     PAF_SIO_IALG_Obj    *pObj;
2371     PAF_SIO_IALG_Config *pAlgConfig;
2373     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
2374     as = pAstCfg->as;
2375     (void)as;  // clear compiler warning in case not used with tracing disabled
2377     for (z=OUTPUT1; z < OUTPUTN; z++) 
2378     {
2379         if (pAstCfg->xOut[z].hTxSio) 
2380         {
2381             // determine associated encoder and stream
2382             zS = z;
2383             (void)zS;
2384             for (zX = ENCODE1; zX < ENCODEN; zX++) 
2385             {
2386                 if (pP->outputsFromEncodes[zX] == z) 
2387                 {
2388                     zS = pP->streamsFromEncodes[zX];
2389                     break;
2390                 }
2391             }
2393             // Mute output before audio data termination in the usual case,
2394             // where such termination is due to decode error or user command.
2395             // Identification of this as the usual case is provided by the
2396             // "decode processing" state machine.
2397             if (!(pAstCfg->xOut[z].outBufStatus.audio & 0xf0) &&
2398                 ((pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_SOUND) &&
2399                 (getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_MUTE, 0))) 
2400             {
2401                 if (!errno) 
2402                 {
2403                     errno = (getVal & 0xff) | ASPERR_MUTE;
2404                     /* convert to sensical errno */
2405                 }
2406                 TRACE_VERBOSE1("PAF_ASOT_stopOutput:  AS%d: SIO control failed (mute)", as+zS);
2407             }
2409             TRACE_TIME((&TIME_MOD, "... + %d = %d (stopOutput -- begin PAF_SIO_CONTROL_IDLE)", dtime(), TSK_time()));
2411             // Terminate audio data output, truncating (ignore) or flushing
2412             // (play out) final samples as per (1) control register set by
2413             // the user and (2) the type of audio data termination:
2415 #if 0
2416             // This form is not used because driver support for truncating
2417             // data is not supported for internal clocks, although it is
2418             // for external clocks.
2419             getVal = SIO_ctrl(pC->xOut[z].hTxSio, PAF_SIO_CONTROL_IDLE,
2420                 pC->xOut[z].outBufStatus.flush
2421                 & (pC->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_FLUSH
2422                 ? 1 : 0);
2423             /* UNTESTED */
2424 #else
2425             // This form should be used when driver support for truncating
2426             // data is supported for both internal and external clocks.
2427             getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_IDLE,
2428                 pAstCfg->xOut[z].outBufStatus.flush ? 1 :
2429                 (pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_FLUSH
2430                 ? 1 : 0);
2431             /* TESTED */
2432 #endif
2434             TRACE_TIME((&TIME_MOD, "... + %d = %d (stopOutput -- after PAF_SIO_CONTROL_IDLE)", dtime(), TSK_time()));
2436             if (!errno)
2437             {
2438                 errno = getVal;
2439             }
2441             // Mute output after audio data termination in a special case,
2442             // where such termination is due to processing of a final frame
2443             // or user command. Identification of this as a special case is
2444             // provided by the "decode processing" state machine.
2445             if (!(pAstCfg->xOut[z].outBufStatus.audio & 0xf0) &&
2446                 ((pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_FLUSH) &&
2447                 (getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_MUTE, 0)))
2448             {
2449                 if (!errno) 
2450                 {
2451                     errno = (getVal & 0xff) | ASPERR_MUTE;
2452                     /* convert to sensical errno */
2453                 }
2454                 TRACE_VERBOSE1("as1-f2: PAF_ASOT_stopOutput:  AS%d: SIO control failed (mute)", as+zS);
2455             }
2457             pAstCfg->xOut[z].outBufStatus.audio &= ~0x0f;
2459             // zero output buffers
2460             pObj = (PAF_SIO_IALG_Obj *) pAstCfg->xOut[z].outChainData.head->alg;
2461             pAlgConfig = &pObj->config;
2462             memset (pAstCfg->xOut[z].outBufConfig.base.pVoid, 0, pAlgConfig->pMemRec[0].size);
2463         } //pAstCfg->xOut[z].hTxSio
2464     }//OUTPUT
2466     return errno;
2467 } //PAF_ASOT_stopOutput
2469 // -----------------------------------------------------------------------------
2470 // ASOT Decoding Function Helper - SIO Driver Change
2471 //
2472 //   Name:      PAF_ASOT_setCheckRateX
2473 //   Purpose:   Decoding Function for reinitiating output.
2474 //   From:      AST Parameter Function -> decodeInfo1
2475 //              AST Parameter Function -> decodeInfo2
2476 //   Uses:      See code.
2477 //   States:    x
2478 //   Return:    Error number in standard form (0 on success).
2479 //   Trace:     None.
2480 //
2482 /* 0: set, 1: check, unused for now. --Kurt */
2483 Int
2484 PAF_ASOT_setCheckRateX(
2485     const PAF_ASOT_Params *pP, 
2486     const PAF_ASOT_Patchs *pQ, 
2487     PAF_ASOT_Config *pC, 
2488     Int check
2491     PAF_AST_Config *pAstCfg;
2492     float rateX;
2493     PAF_SampleRateHz rateO /* std */, rateI /* inv */;
2494     Int z;                              /* output counter */
2495     Int zx;                             /* output re-counter */
2496     Int getVal;
2497     int inputRate, inputCount, outputRate, outputCount;
2498     Int zMD;
2499     Int zMI;
2500     Int zMS;
2501     Int zE, zX;
2503     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
2504     zMD = pAstCfg->masterDec;
2505     zMS = pAstCfg->masterStr;
2506     zMI = pP->zone.master;
2508     inputRate = pAstCfg->xInp[zMI].inpBufStatus.sampleRateStatus;
2509     inputCount = pAstCfg->xDec[zMD].decodeStatus.frameLength; // FL: This should be protected
2510     rateI = pAstCfg->xStr[zMS].pAudioFrame->fxns->sampleRateHz
2511         (pAstCfg->xStr[zMS].pAudioFrame, inputRate, PAF_SAMPLERATEHZ_INV);
2513     for (z=OUTPUT1; z < OUTPUTN; z++) {
2514         if (pAstCfg->xOut[z].hTxSio && (pAstCfg->xOut[z].outBufStatus.clock & 0x01)) {
2516             // determine associated encoder
2517             zE = z;
2518             for (zX = ENCODE1; zX < ENCODEN; zX++) {
2519                 if (pP->outputsFromEncodes[zX] == z) {
2520                     zE = zX;
2521                     break;
2522                 }
2523             }
2525             outputRate = pAstCfg->xEnc[zE].encodeStatus.sampleRate;
2526             outputCount = pAstCfg->xEnc[zE].encodeStatus.frameLength;
2527             rateO = pAstCfg->xStr[zMS].pAudioFrame->fxns->sampleRateHz
2528                 (pAstCfg->xStr[zMS].pAudioFrame, outputRate, PAF_SAMPLERATEHZ_STD);
2529             if (rateI > 0 && rateO > 0)
2530                 rateX = rateO /* std */ * rateI /* inv */;
2531             else if (inputCount != 0)
2532                 rateX = (float )outputCount / inputCount;
2533             else
2534                 return ( ASPERR_INFO_RATERATIO );
2536             getVal = SIO_ctrl (pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_RATEX, (Arg) &rateX);
2537             if (getVal == DOBERR_RATECHANGE) {
2538                 for (zx=OUTPUT1; zx < OUTPUTN; zx++)
2539                     if (pAstCfg->xOut[zx].hTxSio)
2540                         SIO_idle (pAstCfg->xOut[zx].hTxSio);
2542                 // this forces an exit from the calling state machine which will
2543                 // eventually call startOutput which calls setCheckRateX for all outputs
2544                 // and so it is ok, in the presence of a rate change on any output, to
2545                 // exit this loop /function early.
2546                 return ASPERR_INFO_RATECHANGE;
2547             }
2548             else if( getVal != SYS_OK )
2549                 return ((getVal & 0xff) | ASPERR_RATE_CHECK);
2550         }
2551     }
2553     return 0;
2554 } //PAF_ASOT_setCheckRateX
2556 // -----------------------------------------------------------------------------
2557 // ASOT Decoding Function Helper - Chain Processing
2558 //
2559 //   Name:      PAF_ASOT_streamChainFunction
2560 //   Purpose:   Common Function for processing algorithm chains.
2561 //   From:      AST Parameter Function -> decodeInfo1
2562 //              AST Parameter Function -> decodeStream
2563 //              AST Parameter Function -> decodeComplete
2564 //   Uses:      See code.
2565 //   States:    x
2566 //   Return:    Error number in standard form (0 on success).
2567 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2568 //              * State information as per parent.
2569 //
2570 Int
2571 PAF_ASOT_streamChainFunction(
2572     const PAF_ASOT_Params *pP, 
2573     const PAF_ASOT_Patchs *pQ, 
2574     PAF_ASOT_Config *pC, 
2575     Int iChainFrameFxns, 
2576     Int abortOnError, 
2577     Int logArg
2580     PAF_AST_Config *pAstCfg;
2581     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2582     Int z;                      /* stream counter */
2583     Int errno;                  /* error number */
2584     Int dFlag, eFlag, gear;
2585     Int zX;
2586     Int zS;
2588     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
2589     as = pAstCfg->as;
2590     (void)as; // clear compiler warning in case not used with tracing disabled
2592     for (zS = STREAM1; zS < STREAMN; zS++)
2593     {
2594         z = pP->streamOrder[zS];  // Select stream order from streamOrder parameter - MID 788
2596         // apply stream
2597         //      unless the stream is associated with a decoder and it is not running
2598         // or
2599         //      unless the stream is associated with an encoder and it is not running
2600         // Also gear control only works for streams with an associated decoder
2601         // if no such association exists then gear 0 (All) is used
2602         dFlag = 1;
2603         gear = 0;
2604         for (zX = DECODE1; zX < DECODEN; zX++) {
2605             if (pP->streamsFromDecodes[zX] == z) {
2606                 dFlag = pAstCfg->xDec[zX].decodeStatus.mode;
2607                 gear = pAstCfg->xDec[zX].decodeStatus.aspGearStatus;
2608                 break;
2609             }
2610         }
2611         eFlag = 1;
2612         for (zX = ENCODE1; zX < ENCODEN; zX++) {
2613             if (pP->streamsFromEncodes[zX] == z) {
2614                 eFlag = pAstCfg->xEnc[zX].encodeStatus.mode;
2615                 break;
2616             }
2617         }
2619         if (dFlag && eFlag) {
2620             PAF_ASP_Chain *chain = pAstCfg->xStr[z].aspChain[gear];
2621             PAF_AudioFrame *frame = pAstCfg->xStr[z].pAudioFrame;
2622             Int (*func) (PAF_ASP_Chain *, PAF_AudioFrame *) =
2623                 chain->fxns->chainFrameFunction[iChainFrameFxns];
2625             TRACE_GEN2(iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_RESET
2626                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (reset)"
2627                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_APPLY
2628                        ? "PAF_ASOT_streamChainFunction: AS%d: processing block %d -- audio stream (apply)"
2629                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_FINAL
2630                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (final)"
2631                        : "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (?????)",
2632                        as+z, logArg);
2633             errno = (*func) (chain, frame);
2634             TRACE_VERBOSE2("PAF_ASOT_streamChainFunction: AS%d: errno 0x%x.",
2635                 as+z, errno);
2637             if (errno && abortOnError)
2638                 return errno;
2639         }
2640         else {
2641             TRACE_GEN2(iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_RESET
2642                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (reset) <ignored>"
2643                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_APPLY
2644                        ? "PAF_ASOT_streamChainFunction: AS%d: processing block %d -- audio stream (apply) <ignored>"
2645                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_FINAL
2646                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (final) <ignored>"
2647                        : "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (?????) <ignored>",
2648                        as+z, logArg);
2649         }
2651         /*
2652         {
2653             void dp_tracePAF_Data(float *lBuf, float *rBuf, int count);
2654             PAF_AudioFrameData *afd;
2655             float ** afPtr;
2657             afd = &(pC->xStr->pAudioFrame->data);
2658             afPtr = (float**)afd->sample;
2659             dp_tracePAF_Data(afPtr[4], afPtr[5], 256);
2661         }
2662         */
2664     }
2666     return 0;
2667 } //PAF_ASOT_streamChainFunction
2669 /* FL: Check if at least one output selected */
2670 static Int checkOutSel(
2671     const PAF_ASOT_Params *pP, 
2672     PAF_ASOT_Config *pC,
2673     Int *pOutSel
2676     PAF_AST_Config *pAstCfg;
2677     Int outSel;
2678     Int z;
2679     
2680     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
2682     outSel = 0;
2683     for (z=OUTPUT1; z < OUTPUTN; z++) 
2684     {
2685         if (pAstCfg->xOut[z].hTxSio)
2686         {
2687             outSel = 1;
2688             break;
2689         }
2690     }
2691     
2692     *pOutSel = outSel;
2694     return 0;
2697 /* FL: Check if at least one output sio changed */
2698 static Int checkOutSio(
2699     const PAF_ASOT_Params *pP, 
2700     PAF_ASOT_Config *pC,
2701     Int *pOutSioUpdate
2704     PAF_AST_Config *pAstCfg;
2705     Int outSioUpdate;
2706     Int z;
2707     
2708     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
2710     outSioUpdate = 0;
2711     for (z=OUTPUT1; z < OUTPUTN; z++) 
2712     {
2713         if (pAstCfg->xOut[z].outBufStatus.sioSelect >= 0)
2714         {
2715             outSioUpdate = 1;
2716             break;
2717         }
2718     }
2719     
2720     *pOutSioUpdate = outSioUpdate;
2722     return 0;