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