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