]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - pasdk/test_dsp/framework/audioStreamOutProc.c
44040493f1fbc1d217e3cdf0a0448af0be6539c4
[processor-sdk/performance-audio-sr.git] / pasdk / test_dsp / framework / audioStreamOutProc.c
2 /*
3 Copyright (c) 2017, Texas Instruments Incorporated - http://www.ti.com/
4 All rights reserved.
6 * Redistribution and use in source and binary forms, with or without 
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 /*
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 "aspOutInitSync_master.h"
61 #include "audioStreamProc_common.h"
62 #include "audioStreamOutProc.h"
64 #include "pfp/pfp.h"
65 #include "pfp_app.h"        /* contains all PFP ID's */
66 Int32 gNumPfpAsot1=0; // debug
68 // FL: debug
69 #include "evmc66x_gpio_dbg.h"
70 #include "dbgCapAf.h"
72 #include "ioConfig.h"
74 // -----------------------------------------------------------------------------
75 // Debugging Trace Control, local to this file.
76 // 
77 #include "logp.h"
79 #define TRACE_TIME(a)
81 // Allow a developer to selectively enable tracing.
82 #define CURRENT_TRACE_MASK      0x07
84 #define TRACE_MASK_TERSE        0x01   // only flag errors and show init
85 #define TRACE_MASK_GENERAL      0x02   // half dozen lines per frame
86 #define TRACE_MASK_VERBOSE      0x04   // trace full operation
88 #if !(CURRENT_TRACE_MASK & TRACE_MASK_TERSE)
89     #undef  TRACE_TERSE0
90     #undef  TRACE_TERSE1
91     #undef  TRACE_TERSE2
92     #undef  TRACE_TERSE3
93     #undef  TRACE_TERSE4
94     #define TRACE_TERSE0(a)
95     #define TRACE_TERSE1(a,b)
96     #define TRACE_TERSE2(a,b,c)
97     #define TRACE_TERSE3(a,b,c,d)
98     #define TRACE_TERSE4(a,b,c,d,e)
99 #endif
100     
101 #if !(CURRENT_TRACE_MASK & TRACE_MASK_GENERAL)
102     #undef  TRACE_GEN0
103     #undef  TRACE_GEN1
104     #undef  TRACE_GEN2
105     #undef  TRACE_GEN3
106     #undef  TRACE_GEN4
107     #define TRACE_GEN0(a)
108     #define TRACE_GEN1(a,b)
109     #define TRACE_GEN2(a,b,c)
110     #define TRACE_GEN3(a,b,c,d)
111     #define TRACE_GEN4(a,b,c,d,e)
112 #endif
114 #if !(CURRENT_TRACE_MASK & TRACE_MASK_VERBOSE)
115     #undef  TRACE_VERBOSE0
116     #undef  TRACE_VERBOSE1
117     #undef  TRACE_VERBOSE2
118     #undef  TRACE_VERBOSE3
119     #undef  TRACE_VERBOSE4
120     #define TRACE_VERBOSE0(a)
121     #define TRACE_VERBOSE1(a,b)
122     #define TRACE_VERBOSE2(a,b,c)
123     #define TRACE_VERBOSE3(a,b,c,d)
124     #define TRACE_VERBOSE4(a,b,c,d,e)
125 #endif
127 // .............................................................................
129 //
130 // Audio Stream Definitions
131 //
133 //
134 // Audio Stream Processing Definitions
135 //
136 #define aspLinkInit pQ->i_aspLinkInit
138 //
139 // Encoder Definitions
140 //
141 #define encLinkInit pQ->i_encLinkInit
143 //
144 // Output Definitions
145 //
146 #define outLinkInit pP->i_outLinkInit
148 /* ---------------------------------------------------------------- */
149 /*              Parameter macro definitions end here.               */
150 /* ---------------------------------------------------------------- */
152 //
153 // Standardized Definitions
154 //
156 #define ENC_Handle PCE_Handle /* works for all: PCE */
158 #define __TASK_NAME__  "TaskAsop"
161 //
162 // Audio Stream Output Task definitions
163 //
165 // status codes
166 // Output FSM
167 #define ASOP_INITSYNC_NOTREADY      (  1 )  // ok, init-sync not ready
168 #define ASOP_SOK                    (  0 )  // ok
169 #define ASOP_FORWARD_ERR            ( -1 )  // forward (ASIT) error
170 #define ASOP_ENCRESET_ERR           ( -2 )  // enc reset error
171 #define ASOP_DECINFO1_ERR           ( -3 )  // dec info1 error
172 // Decode Processing
173 #define ASOP_DP_OUT_SIO_UPDATE      (  3 )  // new output selected
174 #define ASOP_DP_CB_DRAINED          (  2 )  // circular buffer drained
175 #define ASOP_DP_SOK                 (  0 )  // ok
176 #define ASOP_DP_FORWARD_ERR         ( -1 )  // forward (ASIT) error
177 #define ASOP_DP_DECINIT_ERR         ( -2 )  // decode init error
178 #define ASOP_DP_DECSTREAM_ERR       ( -3 )  // decode stream error
179 #define ASOP_DP_DECENC_ERR          ( -4 )  // decode encode error
180 #define ASOP_DP_DECINFO2_ERR        ( -5 )  // decode encode error
181 #define ASOP_DP_DECFINALTEST_ERR    ( -6 )  // decode final error
185 /* Check if at least one output selected */
186 static Int checkOutSel(
187     const PAF_ASOT_Params *pP, 
188     PAF_ASOT_Config *pAsotCfg,
189     Int *pOutSel
190 );
192 /* Check if at least one output sio changed */
193 static Int checkOutSio(
194     const PAF_ASOT_Params *pP, 
195     PAF_ASOT_Config *pAsotCfg,
196     Int *pOutSioUpdate
197 );
199 // Initialize Output Processing state function
200 //static Int PAF_ASOT_initOutProc(
201 // used by new OutProc.c, will be changed back to static once refactoring is done
202 Int PAF_ASOT_initOutProc(
203     const PAF_ASOT_Params *pP, 
204     PAF_AST_Stream *xStr
205 );
207 // Initialize Output Processing state function
208 // used by new OutProc.c, will be changed back to static once refactoring is done
209 Int PAF_ASOT_initSyncDecReset(
210     const PAF_ASOT_Params *pP, 
211     const PAF_ASOT_Patchs *pQ, 
212     PAF_ASOT_Config *pAsotCfg,
213     PAF_AudioFrame *pDecResetAf
214 );
216 //   Purpose:   ASOT Function for Output reset
217 static Int PAF_ASOT_outputReset(
218     const PAF_ASOT_Params *pP, 
219     const PAF_ASOT_Patchs *pQ, 
220     PAF_ASOT_Config *pAsotCfg
221 );
223 //   Purpose:   Init-Sync Dec Info1 state function.
224 //              Performes Dec Info1 Init-Sync.
225 Int PAF_ASOT_initSyncDecInfo1(
226     const PAF_ASOT_Params *pP, 
227     const PAF_ASOT_Patchs *pQ, 
228     PAF_ASOT_Config *pAsotCfg,
229     PAF_AudioFrame *pDecInfo1Af
230 );
232 //   Purpose:   ASOT function for ASP chain reset and ENC info
233 static Int PAF_ASOT_outputInfo1(
234     const PAF_ASOT_Params *pP, 
235     const PAF_ASOT_Patchs *pQ, 
236     PAF_ASOT_Config *pAsotCfg
237 );
239 //   Purpose:   Init-Sync Dec Decode1 state function.
240 //              Performes Dec Decode1 Init-Sync.
241 Int PAF_ASOT_initSyncDecDecode1(
242     const PAF_ASOT_Params *pP, 
243     const PAF_ASOT_Patchs *pQ, 
244     PAF_ASOT_Config *pAsotCfg
245 );
247 //   Purpose:   Init-Sync Re-Sync state function.
248 //              Peformed Init-Sync using stored Dec Reset/Info1 AFs.
249 Int PAF_ASOT_initSyncResync(
250     const PAF_ASOT_Params *pP, 
251     const PAF_ASOT_Patchs *pQ, 
252     PAF_ASOT_Config *pAsotCfg,
253     PAF_AudioFrame *pDecResetAf,
254     PAF_AudioFrame *pDecInfo1Af  
255 );
257 #if 0
258 // Init-Sync update audio frame
259 static Int initSyncUpdateAf(
260     PAF_AudioFrame *dstAf, 
261     PAF_AudioFrame *srcAf
262 );
263 #endif
265 // Reset audio frames
266 static Void resetAfs(
267     const PAF_ASOT_Params *pP, 
268     PAF_AST_Stream *xStr
269 );
271 // Reset audio frame pointers to original values
272 static Void resetAfPointers(
273     const PAF_ASOT_Params *pP, 
274     PAF_AST_Stream *xStr
275 );
277 // Reset audio frame meta data elements
278 static Void resetAfMetadata(
279     const PAF_ASOT_Params *pP, 
280     PAF_AST_Stream *xStr
281 );
284 LINNO_DEFN(TaskAsop); /* Line number macros */
285 ERRNO_DEFN(TaskAsop); /* Error number macros */
287 // ASOT configuration
288 #pragma DATA_SECTION(gPAF_ASOT_config, ".globalSectionPafAsotConfig")
289 PAF_ASOT_Config gPAF_ASOT_config = {
290     NULL,               // taskHandle
291     NULL,               // acp
292     0,0,0,              // CB drained flags (size DECODE_MAXN)
293     &gPAF_ASPM_config,  // pAspmCfg
294     &gPAF_AST_config    // pAstCfg
295 };
297 // Underflow threshold before circular buffer reset and return error to Top-Level FSM
298 #define DEC_OP_CB_RDAF_UND_THR  ( 80 ) // FL: arbitrary setting
299 UInt32 gCbReadAfErr         =0; // read circular buffer error count, not including underflows
300 UInt32 gDecOpCbRdAfUnd      =0; // decoder output circular buffer underflow count
301 UInt32 gMaxDecOpCbRdAfUnd   =0; // max (consecutive) decoder output circular buffer underflow count
302 UInt32 gMasterCbResetCnt    =0; // master circular buffer reset count
304 // For writeDECCommandRestart
305 extern volatile UInt32 gCommandOutputTask_SYNC;
306 extern volatile UInt32 gCommandOutputTask_ACK;
308 // Global debug counters */
309 UInt32 gAsopTxSioReclaimCnt         =0;
310 UInt32 gAsopInitOutProcCnt          =0;
311 UInt32 gAsopInitSyncDecResetCnt     =0;
312 UInt32 gAsopInitSyncDecInfo1Cnt     =0;
313 UInt32 gAsopInitSyncDecDecode1Cnt   =0;
314 UInt32 gAsopInitSyncResyncCnt       =0;
315 UInt32 gAsopOutProcCnt              =0;
316 UInt32 gAsopInitCnt                 =0;
317 UInt32 gAsopStreamCnt               =0;
318 UInt32 gAsopEncodeCnt               =0;
319 UInt32 gAsopFinalCnt                =0;
320 UInt32 gAsopOutSioUpdateCnt         =0;
321 UInt32 gAsopQuitCnt                 =0;
323 /*
324  *  ======== taskAsopFxnInit ========
325  *  Audio Stream Output Processing initialization function
326  */
327 Void taskAsopFxnInit(
328     const PAF_ASOT_Params *pP,
329     const PAF_ASOT_Patchs *pQ
332     PAF_ASOT_Config *pAsotCfg;      /* ASOT configuration pointer */
333     PAF_AST_Config *pAstCfg;        /* Common (shared) configuration pointer */
334     Int as;                         /* Audio Stream Number (1, 2, etc.) */
335     Int z;                          /* input/encode/stream/decode/output counter */
336     Int i;                          /* phase */
337     Int zMS;
339     Log_info0("Enter taskAsopFxnInit()");
341     //
342     // Audio Stream Output Task Parameters & Patch (*pP, *pQ)
343     //
344     if (!pP)
345     {
346         TRACE_TERSE0("TaskAsop: No Parameters defined. Exiting.");
347         LINNO_RPRT(TaskAsop, -1);
348         return;
349     }
351     if (!pQ)
352     {
353         TRACE_TERSE0("TaskAsop: No Patchs defined. Exiting.");
354         LINNO_RPRT(TaskAsop, -1);
355         return;
356     }
358     //
359     // Audio Stream Output Task Configuration (*pAsotCfg):
360     //
361     pAsotCfg = &gPAF_ASOT_config;       // initialize pointer to task configuration
362     pAsotCfg->taskHandle = Task_self(); // set task handle
363     pAstCfg = pAsotCfg->pAstCfg;        // get pointer to AST common (shared) configuration
365     /* Obtain Audio Stream Number (1, 2, etc.) */
366     as = pAstCfg->as;
367     TRACE_TERSE1("TaskAsop: Started with AS%d.", as);
369     //
370     // Initialize message log trace and line number reporting
371     //
372     for (z=STREAM1; z < STREAMN; z++)
373     {
374         TRACE_TERSE1("TaskAsop: AS%d: initiated", as+z);
375     }
376     LINNO_RPRT(TaskAsop, -1);
378     //
379     // Determine stream index
380     //
381     zMS = pAstCfg->masterStr;
383     // Initialize as per parametrized phases:
384     //
385     //   In standard form these are:
386     //      - Malloc: Memory Allocation
387     //      - Config: Configuration Initialization
388     //      - AcpAlg: ACP Algorithm Initialization and Local Attachment
389     //      - Common: Common Memory Initialization
390     //      - AlgKey: Dec/Enc chain to Array Initialization
391     //      - Device: I/O Device Initialization
392     //      - Unused: (available)
393     //      - Unused: (available)
394     //
395     LINNO_RPRT(TaskAsop, -2);
396     for (i=0; i < lengthof(pP->fxns->initPhase); i++)
397     {
398         Int linno;
399         if (pP->fxns->initPhase[i])
400         {
401             linno = pP->fxns->initPhase[i](pP, pQ, pAsotCfg);
402             if (linno)
403             {
404                 LINNO_RPRT(TaskAsop, linno);
405                 return;
406             }
407         }
408         else
409         {
410             TRACE_TERSE1("TaskAsop: AS%d: initialization phase - null", as+zMS);
411         }
412         TRACE_TERSE2("TaskAsop: AS%d: initialization phase - %d completed", as+zMS, i);
413         LINNO_RPRT(TaskAsop, -i-3);
414     }
416     //
417     // End of Initialization -- display memory usage report.
418     //
419     if (pP->fxns->memStatusPrint)
420     {
421         pP->fxns->memStatusPrint("ASOT MEMSTAT REPORT",
422             HEAP_INTERNAL, HEAP_INTERNAL1, HEAP_EXTERNAL,
423             HEAP_INTERNAL1_SHM, HEAP_EXTERNAL_SHM, HEAP_EXTERNAL_NONCACHED_SHM);
424     }
425 } /* taskAsopFxnInit */
428 /*
429  *  ======== taskAsopFxn ========
430  *  Audio Stream Output Processing task function
431  */
432 #ifdef PASDK_SIO_DEV
433 Void taskAsopFxn(
434 #else
435 #ifndef IO_LOOPBACK_TEST
436 Void taskAsopFxn_NOt_Used(
437 #else
438 Void taskAsopFxn_Not_Used(
439 #endif
440 #endif
441     const PAF_ASOT_Params *pP,
442     const PAF_ASOT_Patchs *pQ
445     PAF_ASOT_Config *pAsotCfg;      /* ASOT configuration pointer */
446     PAF_AST_Config *pAstCfg;        /* Common (shared) configuration pointer */
447     Int as;                         /* Audio Stream Number (1, 2, etc.) */
448     Int z;                          /* input/encode/stream/decode/output counter */
449     Int zMS;
450     Int errno;                      // error number
451     Int8 procSleep;                 // whether to sleep: 0: No, 1: Yes
452     Int8 procOutDevSel;             // whether to perform output device selection: 0: No, 1:Yes
453     Int outSel;                     // whether output device selected
454     enum { INIT_OUT_PROC_STATE, 
455            INITSYNC_DEC_RESET_STATE, INITSYNC_DEC_INFO1_STATE, INITSYNC_DEC_DECODE1_STATE, 
456            INITSYNC_RESYNC_STATE, 
457            OUT_PROC_STATE } state;
458     PAF_AudioFrame decResetAf;
459     PAF_AudioFrame decInfo1Af;
460     Int loopCount = 0;              // used to stop trace to see startup behavior.
462     Log_info0("Enter taskAsopFxn()");
464     taskAsopFxnInit(pP, pQ);    // initialization of output task
466     //
467     // Audio Stream Output Task Configuration (*pAsotCfg):
468     //
469     pAsotCfg = &gPAF_ASOT_config;       // initialize pointer to task configuration
470     pAstCfg = pAsotCfg->pAstCfg;        // get pointer to AST common (shared) configuration
472     /* Obtain Audio Stream Number (1, 2, etc.) */    
473     as = pAstCfg->as;
474     
475     zMS = pAstCfg->masterStr;
477     //
478     // Main processing loop
479     //   
480     for (z=STREAM1; z < STREAMN; z++)
481     {
482         TRACE_VERBOSE1("TaskAsop: AS%d: running", as+z);
483     }
484     
485     errno = 0;                      // init error indicator -- no error
486     procSleep = 1;                  // init sleep flag -- sleep
487     procOutDevSel = 1;              // init device output selection flag -- perform output device selection
488     state = INIT_OUT_PROC_STATE;    // init state
489     for (;;)
490     {
491         loopCount++;
492         TRACE_GEN2("TaskAsop (begin Main loop %d) (errno 0x%x)", loopCount, errno);
494         //
495         // Check forward (ASIT) error here, TBD
496         //
497         
498         // Even if we are not in error state, we check if writeDECCommandRestar issued or not
499         if (gCommandOutputTask_SYNC) {
500             TRACE_TERSE0("TaskAsop: ack for writeDECCommandRestart ... Wait for the command deasserted");
501             gCommandOutputTask_ACK = 1;
502             while (gCommandOutputTask_SYNC) {
503                 Task_sleep(1);
504             }
505             TRACE_TERSE0("TaskAsop: ack for writeDECCommandRestart ... Sync-ed! Startover the process");
506             procSleep = 1;                  // init sleep flag -- sleep
507             procOutDevSel = 1;              // init device output selection flag -- perform output device selection
508             state = INIT_OUT_PROC_STATE;    // init state -- starover
509             errno = ASOP_DP_FORWARD_ERR;    // Override the error -- for flushing SIO output device
510         }
511         
512         // any error forces idling of output
513         if (errno) 
514         {
515             for (z=OUTPUT1; z < OUTPUTN; z++)
516             {
517                 if (pAstCfg->xOut[z].hTxSio)
518                 {
519                     SIO_idle(pAstCfg->xOut[z].hTxSio);
520                 }
521             }
522         
523             TRACE_TERSE1("TaskAsop: Trace stopped at loop %d.", loopCount);
524             ERRNO_RPRT(TaskAsop, errno);
525         }
526         
527         if (procSleep == 1)
528         {
529             TRACE_VERBOSE1("TaskAsop: AS%d: ... sleeping ...", as+zMS);
530             Task_sleep(1);
531         }
533         if (procOutDevSel == 1)
534         {
535             // select output devices
536             TRACE_GEN1("TaskAsop: AS%d: Output device selection ...", as+zMS);
537             errno = pP->fxns->selectDevices(pP, pQ, pAsotCfg);
538             if (errno)
539             {
540                 TRACE_TERSE2("TaskAsop: AS%d: selectDevices returned errno = 0x%04x", as+zMS, errno);
542                 procSleep = 1;
543                 procOutDevSel = 1;
545                 continue;
546             }
548             // if no output selected skip remaining processing
549             errno = checkOutSel(pP, pAsotCfg, &outSel);
550             if (errno < 0)
551             {
552                 TRACE_TERSE2("TaskAsop: AS%d: checkOutSel returned errno = 0x%04x", as+zMS, errno);
554                 procSleep = 1;
555                 procOutDevSel = 1;
557                 continue;
558             }
559             else if (!outSel)
560             {
561                 TRACE_VERBOSE1("TaskAsop: AS%d: No output selected...", as+zMS);
563                 procSleep = 1;
564                 procOutDevSel = 1;
565                 
566                 continue;
567             }
568         }
569         
570         switch (state)
571         {
572             case INIT_OUT_PROC_STATE:
573                 gAsopInitOutProcCnt++;
574                 Log_info0("TaskAsop: state=INIT_OUT_PROC_STATE");
575                 
576                 //
577                 // Output Processing initialization.
578                 //
579                 errno = PAF_ASOT_initOutProc(pP, pAstCfg->xStr);
580                 if (errno < 0)
581                 {
582                     state = INIT_OUT_PROC_STATE;
583                     procSleep = 1;
584                     procOutDevSel = 1;
585                 }
586                 else
587                 {
588                     state = INITSYNC_DEC_RESET_STATE;
589                     procSleep = 0;
590                     procOutDevSel = 0;
591                 }
592             
593                 break;
594                 
595             case INITSYNC_DEC_RESET_STATE:
596                 gAsopInitSyncDecResetCnt++;
597                 Log_info0("TaskAsop: state=INITSYNC_DEC_RESET_STATE");
599                 //
600                 // Dec Reset Init-Sync.
601                 //  
602                 
603                 // Perform Dec Reset init-sync.
604                 // Latch Dec Reset AF.
605                 errno = PAF_ASOT_initSyncDecReset(pP, pQ, pAsotCfg, &decResetAf);
606                 if (errno < 0)
607                 {
608                     Log_info1("TaskAsop: state=INITSYNC_DEC_RESET_STATE errno=%d", errno);
610                     // sync error -- start over
611                     state = INIT_OUT_PROC_STATE;
612                     procSleep = 1;
613                     procOutDevSel = 1;
614                 }
615                 else if (errno == ASOP_INITSYNC_NOTREADY)
616                 {
617                     Log_info1("TaskAsop: state=INITSYNC_DEC_RESET_STATE not sync'd errno=%d", errno);
619                     // sync not ready -- try again
620                     state = INITSYNC_DEC_RESET_STATE;
621                     errno=0; // FL: temp hack
622                     procSleep = 1;
623                     procOutDevSel = 1;
624                 }
625                 else // errno==0
626                 {
627                     Log_info1("TaskAsop: state=INITSYNC_DEC_RESET_STATE sync'd, errno=%d", errno);
629                     // sync'd -- move on
630                     state = INITSYNC_DEC_INFO1_STATE;
631                     procSleep = 0;
632                     procOutDevSel = 0;
633                 }
634                 
635                 break;
636             
637             case INITSYNC_DEC_INFO1_STATE:
638                 gAsopInitSyncDecInfo1Cnt++;
639                 Log_info0("TaskAsop: state=INITSYNC_DEC_INFO1_STATE");
640                 
641                 //
642                 // Dec Info1 Init-Sync.
643                 //
645                 // Perform Dec Info1 init-sync.
646                 // Latch Dec Info1 AF.
647                 errno = PAF_ASOT_initSyncDecInfo1(pP, pQ, pAsotCfg, &decInfo1Af);
648                 if (errno < 0)
649                 {
650                     Log_info1("TaskAsop: state=INITSYNC_DEC_INFO1_STATE errno=%d", errno);
651                     
652                     // sync error -- start over
653                     state = INIT_OUT_PROC_STATE;
654                     procSleep = 1;
655                     procOutDevSel = 1;
656                 }
657                 else if (errno == ASOP_INITSYNC_NOTREADY)
658                 {
659                     Log_info1("TaskAsop: state=INITSYNC_DEC_INFO1_STATE not sync'd errno=%d", errno);
661                     // sync not ready -- try again
662                     state = INITSYNC_DEC_INFO1_STATE;
663                     errno=0; // FL: temp hack
664                     procSleep = 1;
665                     procOutDevSel = 0;
666                 }
667                 else // errno = 0
668                 {
669                     Log_info1("TaskAsop: state=INITSYNC_DEC_INFO1_STATE sync'd errno=%d", errno);
671                     // sync'd -- move on
672                     state = INITSYNC_DEC_DECODE1_STATE;
673                     procSleep = 0;
674                     procOutDevSel = 0;
675                 }
676                 
677                 break;
678             
679             case INITSYNC_DEC_DECODE1_STATE:
680                 gAsopInitSyncDecDecode1Cnt++;
681                 Log_info0("TaskAsop: state=INITSYNC_DEC_DECODE1_STATE");
682                 
683                 //
684                 // Dec Info1 Init-Sync.
685                 //
687                 // Perform Dec Info1 init-sync.
688                 // Latch Dec Info1 AF.
689                 errno = PAF_ASOT_initSyncDecDecode1(pP, pQ, pAsotCfg);
690                 if (errno < 0)
691                 {
692                     Log_info1("TaskAsop: state=INITSYNC_DEC_DECODE1_STATE errno=%d", errno);
694                     // sync error -- start over
695                     state = INIT_OUT_PROC_STATE;
696                     procSleep = 1;
697                     procOutDevSel = 1;
698                 }
699                 else if (errno == ASOP_INITSYNC_NOTREADY)
700                 {
701                     Log_info1("TaskAsop: state=INITSYNC_DEC_DECODE1_STATE not sync'd errno=%d", errno);
703                     // sync not ready -- try again
704                     state = INITSYNC_DEC_DECODE1_STATE;
705                     errno=0; // FL: temp hack
706                     procSleep = 1;
707                     procOutDevSel = 0;
708                 }
709                 else // errno = 0
710                 {
711                     Log_info1("TaskAsop: state=INITSYNC_DEC_DECODE1_STATE sync'd errno=%d", errno);
713                     // sync'd -- move on
714                     state = OUT_PROC_STATE;
715                     procSleep = 0;
716                     procOutDevSel = 0;
717                 }
718                 
719                 break;
720             
721             case INITSYNC_RESYNC_STATE:
722                 gAsopInitSyncResyncCnt++;
723                 Log_info0("TaskAsop: state=INITSYNC_RESYNC_STATE");
725                 //
726                 // Re-Sync.
727                 // Use stored AF info from init-sync.
728                 // This is done in case of local error.
729                 //
730                 
731                 // Perform Dec Info1 init-sync.
732                 errno = PAF_ASOT_initSyncResync(pP, pQ, pAsotCfg, &decResetAf, 
733                     &decInfo1Af);
734                 if (errno < 0)
735                 {
736                     Log_info1("TaskAsop: state=INITSYNC_RESYNC_STATE errno=%d", errno);
738                     // sync error -- start over
739                     state = INIT_OUT_PROC_STATE;
740                     procSleep = 1;
741                     procOutDevSel = 1;
742                 }
743                 else
744                 {
745                     Log_info1("TaskAsop: state=INITSYNC_RESYNC_STATE sync'd errno=%d", errno);
747                     // re-sync'd -- move on
748                     state = OUT_PROC_STATE;
749                     procSleep = 0;
750                     procOutDevSel = 0;
751                 }
752                     
753                 break;
754             
755             case OUT_PROC_STATE:        
756                 gAsopOutProcCnt++;
757                 Log_info0("TaskAsop: state=OUT_PROC_STATE");
758                 
759                 //
760                 // Output Processing.
761                 //
763                 TRACE_VERBOSE0("TaskAsop: calling decodeProcessing.");
764                 errno = pP->fxns->decodeProcessing(pP, pQ, pAsotCfg);
765                 if (errno < 0)
766                 {
767                     Log_info1("TaskAsop: state=OUT_PROC_STATE errno=%d", errno);
768                     
769                     //
770                     // Output Processing exit, due to error
771                     //
772                     
773                     TRACE_TERSE1("TaskAsop: decodeProcessing returns 0x%x, continue", errno);
774                     if (errno == ASOP_DP_FORWARD_ERR)
775                     {
776                         // forward (ASIT) error -- start over
777                         state = INIT_OUT_PROC_STATE;
778                         procSleep = 1;
779                         procOutDevSel = 1;
780                     }
781                     else
782                     {
783                         // local (ASOT) error
784                         state = INITSYNC_RESYNC_STATE;    
785                         procSleep = 1;
786                         procOutDevSel = 0; // disallow device re-select if local error during output processing
787                     }        
788                 }
789                 else if (errno > 0)
790                 {
791                     Log_info1("TaskAsop: state=OUT_PROC_STATE errno=%d", errno);
793                     //
794                     // Output Processing exit, not due to error
795                     //
797                     TRACE_TERSE1("TaskAsop: decodeProcessing returns 0x%x, continue", errno);                    
798                     if (errno == ASOP_DP_OUT_SIO_UPDATE)
799                     {
800                         // skip re-sync
801                         // resume output processing after new output selected
802                         state = OUT_PROC_STATE;
803                         procSleep = 1;
804                         procOutDevSel = 1;
805                     }
806                 }
807                 else
808                 {
809                     Log_info1("TaskAsop: state=OUT_PROC_STATE errno=%d", errno);
811                     //
812                     // Output Processing exit, normal
813                     //
814                     
815                     TRACE_VERBOSE0("TaskAsop: outputProcessing complete with no error.");
816                     
817                     // no error returned if CB drained 
818                     // (i.e. CB drained is normal behavior)
819                     state = INIT_OUT_PROC_STATE;
820                     procSleep = 1;
821                     procOutDevSel = 1;
822                 }
823                 
824                 break;
825             
826             default: // unknown state
827                 TRACE_TERSE2("TaskAsop: AS%d: state: unknown, 0x%x", as+zMS, state);
828                 break;
829         }
830     } // End of main processing loop for (;;)
831     
832     //Log_info0("Exit taskAsopFxn()");
835 Int PAF_ASOT_ioCompCreate(PAF_AST_IoOut *pIoOut, int numOut, IHeap_Handle iHeapHandle)
837     int i, j, num_alloc;
838     lib_mem_rec_t   *mem_rec;
839     ioBuffHandle_t  ioBufHandle;
840     ioPhyHandle_t   ioPhyHandle;
841     Error_Block     eb;
843     for(i=0; i<numOut; i++)
844     {
845         // Create an I/O BUFF instance
846         // Obtain number of memory blocks required by I/O BUFF
847         num_alloc = ioBuffNumAlloc();
849         // Obtain requirements of each memory block
850         mem_rec   = (lib_mem_rec_t *)malloc(sizeof(lib_mem_rec_t)*num_alloc);
851         if(ioBuffAlloc(mem_rec) != IOBUFF_NOERR) {
852             return __LINE__;
853         }
855         /* Allocate memory and create I/O BUFF instance */
856         for(j=0; j<num_alloc; j++) {
857             mem_rec[j].base = Memory_calloc(iHeapHandle, mem_rec[j].size,
858                                             (1<<mem_rec[j].alignment), &eb);
859         }
861         if(ioBuffCreate(&ioBufHandle, mem_rec) != IOBUFF_NOERR) {
862             return __LINE__;
863         }
865         pIoOut[i].hIoBuff = ioBufHandle;
867         free(mem_rec);
869         // Create an I/O PHY instance
870         // Obtain number of memory blocks required by I/O PHY
871         num_alloc = ioPhyNumAlloc();
873         // Obtain requirements of each memory block
874         mem_rec   = (lib_mem_rec_t *)malloc(sizeof(lib_mem_rec_t)*num_alloc);
875         if(ioPhyAlloc(mem_rec) != IOBUFF_NOERR) {
876             return __LINE__;
877         }
879         /* Allocate memory and create I/O PHY instance */
880         for(j=0; j<num_alloc; j++) {
881             mem_rec[j].base = Memory_calloc(iHeapHandle, mem_rec[j].size,
882                                             (1<<mem_rec[j].alignment), &eb);
883         }
885         if(ioPhyCreate(&ioPhyHandle, mem_rec) != IOBUFF_NOERR) {
886             return __LINE__;
887         }
889         pIoOut[i].hIoPhy = ioPhyHandle;
891         free(mem_rec);
893     } // end of for loop
895     return 0;
896 } // PAF_ASOT_ioCompCreate
898 // -----------------------------------------------------------------------------
899 // AST Initialization Function - Memory Allocation
900 //
901 //   Name:      PAF_ASOT_initPhaseMalloc
902 //   Purpose:   Audio Stream Output Task Function for initialization of data pointers
903 //              by allocation of memory.
904 //   From:      audioStream1Task or equivalent
905 //   Uses:      See code.
906 //   States:    x
907 //   Return:    0 on success.
908 //              Source code line number on MEM_calloc failure.
909 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
910 //              * State information as per parent.
911 //              * Memory allocation errors.
912 //
914 Int
915 PAF_ASOT_initPhaseMalloc (
916     const PAF_ASOT_Params *pP, 
917     const PAF_ASOT_Patchs *pQ, 
918     PAF_ASOT_Config *pAsotCfg
921     PAF_AST_Config *pAstCfg;
922     Int as;                     /* Audio Stream Number (1, 2, etc.) */
923     Int zMS, errLineNum;
924     Error_Block    eb;
925     //Int i;
927     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
928     as = pAstCfg->as;
929     zMS = pAstCfg->masterStr;
931     TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: initialization phase - memory allocation", as+zMS);
933     // Initialize error block
934     Error_init(&eb); 
936     /* Stream memory */
937     if (!(pAstCfg->xStr = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, STREAMN * sizeof (*pAstCfg->xStr), 4, &eb)))
938     {
939         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
940         SW_BREAKPOINT;
941         return __LINE__;
942     }
943     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAstCfg->xStr) %d bytes from space %d at 0x%x.",
944             STREAMN * sizeof (*pAstCfg->xStr),
945             HEAP_ID_INTERNAL, (IArg)pAstCfg->xStr);
947     {
948         Int z;                          /* stream counter */
950         PAF_AudioFrame *fBuf;
952         if (!(fBuf = (PAF_AudioFrame *)Memory_calloc((IHeap_Handle)HEAP_INTERNAL, STREAMS * sizeof (*fBuf), 4, &eb)))
953         {
954             TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
955             SW_BREAKPOINT;
956             return __LINE__;
957         }
958         TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (fBuf) %d bytes from space %d at 0x%x.",
959                 STREAMS * sizeof (*fBuf),
960                 HEAP_ID_INTERNAL, (IArg)fBuf);
962         for (z=STREAM1; z < STREAMN; z++)
963         {
964             pAstCfg->xStr[z].pAudioFrame = &fBuf[z-STREAM1];
965             TRACE_TERSE2("pAstCfg->xStr[%d].pAudioFrame = 0x%x", z, (IArg)pAstCfg->xStr[z].pAudioFrame);
966         }
967     }
969     /* Encode memory */
970     if (!(pAstCfg->xEnc = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, ENCODEN * sizeof (*pAstCfg->xEnc), 4, &eb)))
971     {
972         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
973         SW_BREAKPOINT;
974         return __LINE__;
975     }
976     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAstCfg->xEnc) %d bytes from space %d at 0x%x.",
977             ENCODEN * sizeof (*pAstCfg->xEnc),
978             HEAP_ID_INTERNAL, (IArg)pAstCfg->xEnc);
980     /* Output memory */
981     if (!(pAstCfg->xOut = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, OUTPUTN * sizeof (*pAstCfg->xOut), 4, &eb)))
982     {
983         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
984         SW_BREAKPOINT;
985         return __LINE__;
986     }
987     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAstCfg->xOut) %d bytes from space %d at 0x%x.",
988             OUTPUTN * sizeof (*pAstCfg->xOut),
989             HEAP_ID_INTERNAL, (IArg)pAstCfg->xOut);
991     /* Output I/O data structure memory */
992     if (!(pAsotCfg->pIoOut = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, OUTPUTN * sizeof (*pAsotCfg->pIoOut), 4, &eb)))
993     {
994         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
995         SW_BREAKPOINT;
996         return __LINE__;
997     }
998     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAsotCfg->pIoOut) %d bytes from space %d at 0x%x.",
999                  OUTPUTN * sizeof (*pAsotCfg->pIoOut),
1000                  HEAP_ID_INTERNAL, (IArg)pAsotCfg->pIoOut);
1002     /* I/O components memory for output */
1003     errLineNum = PAF_ASOT_ioCompCreate(pAsotCfg->pIoOut, OUTPUTN, (IHeap_Handle)HEAP_INTERNAL);
1004     if(errLineNum)
1005     {
1006         SW_BREAKPOINT;
1007         return errLineNum;
1008     }
1009     TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: initialization phase - memory allocation complete.", as+zMS);
1010     return 0;
1011 } //PAF_ASOT_initPhaseMalloc
1013 // -----------------------------------------------------------------------------
1014 // ASOT Initialization Function - Memory Initialization from Configuration
1015 //
1016 //   Name:      PAF_ASOT_initPhaseConfig
1017 //   Purpose:   Audio Stream Output Task Function for initialization of data values
1018 //              from parameters.
1019 //   From:      audioStream1Task or equivalent
1020 //   Uses:      See code.
1021 //   States:    x
1022 //   Return:    0 on success.
1023 //              Other as per initFrame0 and initFrame1.
1024 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1025 //              * State information as per parent.
1026 //
1027 Int
1028 PAF_ASOT_initPhaseConfig(
1029     const PAF_ASOT_Params *pP, 
1030     const PAF_ASOT_Patchs *pQ, 
1031     PAF_ASOT_Config *pAsotCfg
1034     PAF_AST_Config *pAstCfg;
1035     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1036     Int z;                      /* input/encode/stream/decode/output counter */
1037     Int zMS;
1039     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1040     as = pAstCfg->as;
1041     zMS = pAstCfg->masterStr;
1043     TRACE_TERSE1("PAF_ASOT_initPhaseConfig: AS%d: initialization phase - configuration", as+zMS);
1045     //
1046     // Unspecified elements have been initialized to zero during alloc
1047     //
1049     for (z=STREAM1; z < STREAMN; z++) 
1050     {
1051         Int linno;
1052         if (linno = pP->fxns->initFrame0(pP, pQ, pAsotCfg, z))
1053         {
1054             return linno;           
1055         }
1056         if (linno = pP->fxns->initFrame1(pP, pQ, pAsotCfg, z, -1))
1057         {
1058             return linno;
1059         }
1060     }
1062     for (z=ENCODE1; z < ENCODEN; z++) 
1063     {
1064         Int zO = pP->outputsFromEncodes[z];
1065         Int zS = pP->streamsFromEncodes[z];
1066         pAstCfg->xEnc[z].encodeControl.size = sizeof(pAstCfg->xEnc[z].encodeControl);
1067         pAstCfg->xEnc[z].encodeControl.pAudioFrame = pAstCfg->xStr[zS].pAudioFrame;
1068         pAstCfg->xEnc[z].encodeControl.pVolumeStatus = &pAstCfg->xEnc[z].volumeStatus;
1069         pAstCfg->xEnc[z].encodeControl.pOutBufConfig = &pAstCfg->xOut[zO].outBufConfig;
1070         pAstCfg->xEnc[z].encodeStatus = *pP->z_pEncodeStatus[z];
1071         pAstCfg->xEnc[z].encodeControl.encActive = pAstCfg->xEnc[z].encodeStatus.select;
1072         pAstCfg->xEnc[z].volumeStatus = *pP->pVolumeStatus;
1073         pAstCfg->xEnc[z].encodeInStruct.pAudioFrame = pAstCfg->xStr[zS].pAudioFrame;
1074     }
1076     for (z=OUTPUT1; z < OUTPUTN; z++)
1077     {
1078         pAstCfg->xOut[z].outBufStatus = *pP->pOutBufStatus;
1079     }
1081     TRACE_TERSE1("PAF_ASOT_initPhaseConfig: AS%d: initialization phase - configuration complete.", as+zMS);
1082     return 0;
1083 } //PAF_ASOT_initPhaseConfig
1085 // -----------------------------------------------------------------------------
1086 // ASOT Initialization Function - ACP Algorithm Instantiation
1087 //
1088 //   Name:      PAF_ASOT_initPhaseAcpAlg
1089 //   Purpose:   Audio Stream Input Task Function for initialization of ACP by
1090 //              instantiation of the algorithm.
1091 //   From:      audioStream1Task or equivalent
1092 //   Uses:      See code.
1093 //   States:    x
1094 //   Return:    0 on success.
1095 //              Source code line number on ACP Algorithm creation failure.
1096 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1097 //              * State information as per parent.
1098 //              * Memory allocation errors.
1099 //
1100 Int
1101 PAF_ASOT_initPhaseAcpAlg(
1102     const PAF_ASOT_Params *pP, 
1103     const PAF_ASOT_Patchs *pQ, 
1104     PAF_ASOT_Config *pAsotCfg
1107     PAF_AST_Config *pAstCfg;
1108     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1109     Int z;                      /* input/encode/stream/decode/output counter */
1110     Int betaPrimeOffset;
1111     ACP_Handle acp;
1112     Int zMS;
1113     Int zS, zX;
1115     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1116     as = pAstCfg->as;
1117     zMS = pAstCfg->masterStr;
1119     TRACE_TERSE1("PAF_ASOT_initPhaseAcpAlg: AS%d: initialization phase - ACP Algorithm", as+zMS);
1121     ACP_MDS_init();
1123     if (!(acp = (ACP_Handle )ACP_MDS_create(NULL))) 
1124     {
1125         TRACE_TERSE1("PAF_ASOT_initPhaseAcpAlg: AS%d: ACP algorithm instance creation  failed", as+zMS);
1126         return __LINE__;
1127     }
1128     pAsotCfg->acp = acp;
1130     ((ALG_Handle)acp)->fxns->algControl((ALG_Handle) acp,
1131         ACP_GETBETAPRIMEOFFSET, (IALG_Status *)&betaPrimeOffset);
1133     for (z=ENCODE1; z < ENCODEN; z++) 
1134     {
1135         zS = pP->streamsFromEncodes[z];
1136         acp->fxns->attach(acp, ACP_SERIES_STD,
1137             STD_BETA_ENCODE + betaPrimeOffset * (as-1+zS),
1138             (IALG_Status *)&pAstCfg->xEnc[z].encodeStatus);
1139         acp->fxns->attach(acp, ACP_SERIES_STD,
1140             STD_BETA_VOLUME + betaPrimeOffset * (as-1+zS),
1141             (IALG_Status *)&pAstCfg->xEnc[z].volumeStatus);
1142         /* Ignore errors, not reported. */
1143     }
1145     for (z=OUTPUT1; z < OUTPUTN; z++) 
1146     {
1147         zS = z;
1148         for (zX = ENCODE1; zX < ENCODEN; zX++) 
1149         {
1150             if (pP->outputsFromEncodes[zX] == z) 
1151             {
1152                 zS = pP->streamsFromEncodes[zX];
1153                 break;
1154             }
1155         }
1156         acp->fxns->attach(acp, ACP_SERIES_STD,
1157             STD_BETA_OB + betaPrimeOffset * (as-1+zS),
1158             (IALG_Status *)&pAstCfg->xOut[z].outBufStatus);
1159         /* Ignore errors, not reported. */
1160     }
1162     TRACE_TERSE1("PAF_ASOT_initPhaseAcpAlg: AS%d: initialization phase - ACP Algorithm complete.", as+zMS);
1164     return 0;
1165 } //PAF_ASOT_initPhaseAcpAlg
1167 // -----------------------------------------------------------------------------
1168 // ASOT Initialization Function - Common Memory
1169 //
1170 //   Name:      PAF_ASOT_initPhaseCommon
1171 //   Purpose:   Audio Stream Output Task Function for allocation of common memory.
1172 //   From:      audioStream1Task or equivalent
1173 //   Uses:      See code.
1174 //   States:    x
1175 //   Return:    0 on success.
1176 //              Source code line number on PAF_ALG_alloc failure.
1177 //              Source code line number on PAF_ALG_mallocMemory failure.
1178 //              Source code line number on Decode Chain initialization failure.
1179 //              Source code line number on ASP Chain initialization failure.
1180 //              Source code line number on Encode Chain initialization failure.
1181 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1182 //              * State information as per parent.
1183 //              * Memory allocation errors.
1184 //
1185 Int
1186 PAF_ASOT_initPhaseCommon(
1187     const PAF_ASOT_Params *pP, 
1188     const PAF_ASOT_Patchs *pQ, 
1189     PAF_ASOT_Config *pAsotCfg
1192     PAF_AST_Config *pAstCfg;
1193     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1194     Int z;                      /* stream counter */
1195     Int g;                      /* gear */
1196     ACP_Handle acp;
1197     PAF_IALG_Config pafAlgConfig;
1198     IALG_MemRec common[3][PAF_IALG_COMMON_MEMN+1];
1199    
1200     acp = pAsotCfg->acp;
1201     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1202     as = pAstCfg->as;
1204     TRACE_TERSE0("PAF_ASOT_initPhaseCommon: initialization phase - Common Memory");
1206     //
1207     // Determine memory needs and instantiate algorithms across audio streams
1208     //
1209     TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ALG_setup.");
1210     PAF_ALG_setup(&pafAlgConfig, 
1211         HEAP_ID_INTERNAL,                 HEAP_INTERNAL, 
1212         HEAP_ID_INTERNAL1,                HEAP_INTERNAL1, 
1213         HEAP_ID_EXTERNAL,                 HEAP_EXTERNAL, 
1214         HEAP_ID_INTERNAL1_SHM,            HEAP_INTERNAL1_SHM, 
1215         HEAP_ID_EXTERNAL_SHM,             HEAP_EXTERNAL_SHM, 
1216         HEAP_ID_EXTERNAL_NONCACHED_SHM,   HEAP_EXTERNAL_NONCACHED_SHM,
1217         HEAP_CLEAR);
1219     if (pP->fxns->headerPrint)
1220     {
1221         pP->fxns->headerPrint();        
1222     }
1224     for (z = STREAM1; z < STREAMN; z++) 
1225     {
1226         //Int zD, zE, zX;
1227         Int zE, zX;
1229         TRACE_TERSE1("PAF_ASOT_initPhaseCommon: AS%d: initialization phase - Common Memory", as+z);
1231         //
1232         // Determine common memory needs for:
1233         //  (1) ASP Algorithms
1234         //  (2) Encode Algorithms
1235         //  (3) Logical Output drivers
1236         //
1237         PAF_ALG_init(common[z], lengthof(common[z]), COMMONSPACE);
1239         zE = -1;
1240         for (zX = ENCODE1; zX < ENCODEN; zX++) 
1241         {
1242             if (pP->streamsFromEncodes[zX] == z) 
1243             {
1244                 zE = zX;
1245                 break;
1246             }
1247         }
1249         TRACE_TERSE1("Calling PAF_ALG_ALLOC for stream common[%d].", z);
1250         if (PAF_ALG_ALLOC(aspLinkInit[z-STREAM1][0], common[z])) 
1251         {
1252             TRACE_TERSE1("PAF_ASOT_initPhaseCommon: AS%d: PAF_ALG_alloc failed", as+z);
1253             TRACE_TERSE2("Failed to alloc %d bytes from space %d ", common[z]->size, common[z]->space);
1254             SW_BREAKPOINT;
1255             return __LINE__;
1256         }
1257         TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1258         if (pP->fxns->allocPrint)
1259         {
1260             pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(aspLinkInit[z-STREAM1][0]), sizeof (*(aspLinkInit[z-STREAM1][0])), &pafAlgConfig);
1261         }
1263         if (zE >= 0) 
1264         {
1265             TRACE_TERSE1("PAF_ASOT_initPhaseCommon: calling PAF_ALG_ALLOC/ for encoder common[%d].", z);
1266             if (PAF_ALG_ALLOC(encLinkInit[zE-ENCODE1], common[z])) 
1267             {
1268                 TRACE_TERSE1("PAF_ASOT_initPhaseCommon: AS%d: PAF_ALG_alloc failed", as+z);
1269                 SW_BREAKPOINT;
1270                 return __LINE__;
1271             }
1272             TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1273             if (pP->fxns->allocPrint)
1274             {
1275                 pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(encLinkInit[z-ENCODE1]), sizeof (*(encLinkInit[z-ENCODE1])), &pafAlgConfig);
1276             }
1277         }
1279         //
1280         // Determine common memory needs of Logical IO drivers
1281         //
1283         if (OUTPUT1 <= z && z < OUTPUTN)
1284         {
1285             TRACE_TERSE1("PAF_ASOT_initPhaseCommon: calling PAF_ALG_ALLOC outLinkInit common[%d].", z);
1286             if (PAF_ALG_ALLOC(outLinkInit[z-OUTPUT1], common[z]))
1287             {
1288                 TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: PAF_ALG_alloc failed", as+z);
1289                 TRACE_TERSE2("Failed to alloc %d bytes from space %d", common[z]->size, (IArg)common[z]->space);
1290                 SW_BREAKPOINT;
1291                 return __LINE__;
1292             }
1293             TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1294             if (pP->fxns->allocPrint)
1295             {
1296                 pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(outLinkInit[z-INPUT1]), sizeof (*(outLinkInit[z-INPUT1])), &pafAlgConfig);
1297             }
1298         }
1299     }
1300     {
1301         // Changes made to share scratch between zones
1302         // Assume maximum 3 zones and scratch common memory is at offset 0;
1303         int max=0;
1304         for (z=STREAM1; z<STREAMN; z++)
1305         {
1306             if (max < common[z][0].size)
1307             {
1308                 max = common[z][0].size;
1309             }
1310         }
1311         common[STREAM1][0].size=max;
1312         for (z=STREAM1+1; z<STREAMN; z++)
1313         {
1314             common[z][0].size = 0;            
1315         }
1316     }
1317         
1318     //
1319     // Allocate common memory for:
1320     //  (1) ASP Algorithms
1321     //  (2) Encode Algorithms
1322     //  (3) Logical Output drivers
1323     //
1324     for (z = STREAM1; z < STREAMN; z++) 
1325     {
1326         //Int zD, zE, zX;
1327         Int zE, zX;
1329         TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ALG_mallocMemory for common space.");
1330         if (PAF_ALG_mallocMemory(common[z], &pafAlgConfig)) 
1331         {
1332             TRACE_TERSE1("AS%d: PAF_ALG_mallocMemory failed", as+z);
1333             TRACE_TERSE3("AS%d: z: %d.  Size 0x%x", as+z, z, common[z][0].size);
1334             SW_BREAKPOINT;
1335             return __LINE__;
1336         }
1337         TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1338         // share zone0 scratch with all zones 
1339         common[z][0].base = common[0][0].base;
1340         if (pP->fxns->commonPrint)
1341         {
1342             pP->fxns->commonPrint(common[z], &pafAlgConfig);
1343         }
1345         zE = -1;
1346         for (zX = ENCODE1; zX < ENCODEN; zX++) 
1347         {
1348             if (pP->streamsFromEncodes[zX] == z) 
1349             {
1350                 zE = zX;
1351                 break;
1352             }
1353         }
1355         pAstCfg->xStr[z].aspChain[0] = NULL;
1356         for (g=0; g < GEARS; g++) 
1357         {
1358             PAF_ASP_Chain *chain;
1359             TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ASP_chainInit for ASPs.");
1360             chain = PAF_ASP_chainInit(&pAstCfg->xStr[z].aspChainData[g], pP->pChainFxns,
1361                 HEAP_INTERNAL, as+z, acp, &trace,
1362                 aspLinkInit[z-STREAM1][g], pAstCfg->xStr[z].aspChain[0], common[z], &pafAlgConfig);
1363             if (!chain) 
1364             {
1365                 TRACE_TERSE2("AS%d: ASP chain %d initialization failed", as+z, g);
1366                 return __LINE__;
1367             }
1368             else
1369             {
1370                 pAstCfg->xStr[z].aspChain[g] = chain;
1371             }
1372         }
1374         if (zE >= 0) 
1375         {
1376             PAF_ASP_Chain *chain;
1377             TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ASP_chainInit for encode.");
1378             chain = PAF_ASP_chainInit(&pAstCfg->xEnc[zE].encChainData, pP->pChainFxns,
1379                 HEAP_INTERNAL, as+z, acp, &trace,
1380                 encLinkInit[zE-ENCODE1], NULL, common[z], &pafAlgConfig);
1381             if (!chain) 
1382             {
1383                 TRACE_TERSE1("AS%d: Encode chain initialization failed", as+z);
1384                 return __LINE__;
1385             }
1386         }
1388         //
1389         // Allocate non-common memories for Logical IO drivers
1390         //    Since these structures are used at run-time we allocate from external memory
1391         if (OUTPUT1 <= z && z < OUTPUTN) 
1392         {
1393             PAF_ASP_Chain *chain;
1394             TRACE_TERSE2("PAF_ASOT_initPhaseMalloc: AS%d: non-common output chain init for %d",
1395                            as+z, z);
1396             chain = PAF_ASP_chainInit (&pAstCfg->xOut[z].outChainData, pP->pChainFxns,
1397                         HEAP_EXTERNAL, as+z, acp, &trace,
1398                         outLinkInit[z-OUTPUT1], NULL, common[z], &pafAlgConfig);
1399             if (!chain) 
1400             {
1401                 TRACE_TERSE1("AS%d: Output chain initialization failed", as+z);
1402                 return __LINE__;
1403             }
1404         }
1405     }
1406     TRACE_TERSE1("AS%d: PAF_ASOT_initPhaseCommon: Returning complete.", as+z);
1408     return 0;
1409 } //PAF_ASOT_initPhaseCommon
1411 // -----------------------------------------------------------------------------
1412 // ASOT Initialization Function - Algorithm Keys
1413 //
1414 //   Name:      PAF_ASOT_initPhaseAlgKey
1415 //   Purpose:   Audio Stream Output Task Function for initialization of data values
1416 //              from parameters for Algorithm Keys.
1417 //   From:      audioStream1Task or equivalent
1418 //   Uses:      See code.
1419 //   States:    x
1420 //   Return:    0.
1421 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1422 //              * State information as per parent.
1423 //
1424 // .............................................................................
1425 Int
1426 PAF_ASOT_initPhaseAlgKey(
1427     const PAF_ASOT_Params *pP, 
1428     const PAF_ASOT_Patchs *pQ, 
1429     PAF_ASOT_Config *pAsotCfg
1432     PAF_AST_Config *pAstCfg;
1433     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1434     Int z;                      /* decode/encode counter */
1435     Int s;                      /* key number */
1436     PAF_ASP_Link *that;
1438     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1439     as = pAstCfg->as;
1440     (void)as; // clear compiler warning in case not used with tracing disabled
1442     TRACE_VERBOSE1("PAF_ASOT_initPhaseAlgKey: AS%d: initialization phase - Algorithm Keys", as);
1444     for (z=ENCODE1; z < ENCODEN; z++) 
1445     {
1446         for (s=0; s < pP->pEncAlgKey->length; s++) 
1447         {
1448             if ((pP->pEncAlgKey->code[s].full != 0) && 
1449                 (that = PAF_ASP_chainFind(&pAstCfg->xEnc[z].encChainData, pP->pEncAlgKey->code[s])))
1450             {
1451                 pAstCfg->xEnc[z].encAlg[s] = (ALG_Handle )that->alg;
1452             }
1453             /* Cast in interface, for now --Kurt */
1454             else
1455             {
1456                 pAstCfg->xEnc[z].encAlg[s] = NULL;                
1457             }
1458         }
1459     }
1461     return 0;
1462 } //PAF_ASOT_initPhaseAlgKey
1464 // -----------------------------------------------------------------------------
1465 // ASOT Initialization Function - I/O Devices
1466 //
1467 //   Name:      PAF_ASOT_initPhaseDevice
1468 //   Purpose:   Audio Stream Output Task Function for initialization of I/O Devices.
1469 //   From:      audioStream1Task or equivalent
1470 //   Uses:      See code.
1471 //   States:    x
1472 //   Return:    0 on success.
1473 //              Source code line number on device allocation failure.
1474 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1475 //              * State information as per parent.
1476 //              * Memory allocation errors.
1477 //
1478 Int
1479 PAF_ASOT_initPhaseDevice(
1480     const PAF_ASOT_Params *pP, 
1481     const PAF_ASOT_Patchs *pQ, 
1482     PAF_ASOT_Config *pAsotCfg
1485     PAF_AST_Config *pAstCfg;
1486     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1487     Int z;                      /* input/output counter */
1488     PAF_SIO_IALG_Obj    *pObj;
1489     PAF_SIO_IALG_Config *pAlgConfig;
1490     PAF_IALG_Config pafAlgConfig;
1492     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1493     as = pAstCfg->as;
1494     (void)as; // clear compiler warning in case not used with tracing disabled
1496     TRACE_TERSE1("PAF_ASOT_initPhaseDevice: AS%d: initialization phase - I/O Devices", as);
1498     if(pP->fxns->bufMemPrint)
1499     {
1500         PAF_ALG_setup (&pafAlgConfig, 
1501             HEAP_ID_INTERNAL,               HEAP_INTERNAL, 
1502             HEAP_ID_INTERNAL1,              HEAP_INTERNAL1,
1503             HEAP_ID_EXTERNAL,               HEAP_EXTERNAL,
1504             HEAP_ID_INTERNAL1_SHM,          HEAP_INTERNAL1_SHM,
1505             HEAP_ID_EXTERNAL_SHM,           HEAP_EXTERNAL_SHM,
1506             HEAP_ID_EXTERNAL_NONCACHED_SHM, HEAP_EXTERNAL_NONCACHED_SHM,
1507             HEAP_CLEAR);
1508         TRACE_TERSE2("PAF_ASOT_initPhaseDevice: AS%d: calling PAF_ALG_setup with clear at %d.", as, HEAP_CLEAR);
1509     }
1510     
1511     for (z=OUTPUT1; z < OUTPUTN; z++) 
1512     {
1513         PAF_OutBufConfig *pConfig = &pAstCfg->xOut[z].outBufConfig;
1515         pObj = (PAF_SIO_IALG_Obj *)pAstCfg->xOut[z].outChainData.head->alg;
1516         pAlgConfig = &pObj->config;
1518         pAstCfg->xOut[z].hTxSio = NULL;
1519         pConfig->base.pVoid     = pAlgConfig->pMemRec[0].base;
1520         pConfig->pntr.pVoid     = pAlgConfig->pMemRec[0].base;
1521         pConfig->head.pVoid     = pAlgConfig->pMemRec[0].base;
1522         pConfig->allocation     = pAlgConfig->pMemRec[0].size;
1523         pConfig->sizeofElement  = 3;
1524         pConfig->precision      = 24;
1525         if(pP->fxns->bufMemPrint)
1526         {
1527             pP->fxns->bufMemPrint(z,pAlgConfig->pMemRec[0].size,PAF_ALG_memSpaceToHeapId(&pafAlgConfig,pAlgConfig->pMemRec[0].space),1);
1528         }
1529     }
1530     TRACE_TERSE1("PAF_ASOT_initPhaseDevice: AS%d: initialization phase - I/O Devices complete.", as);
1532     return 0;
1533 } //PAF_ASOT_initPhaseDevice
1535 // -----------------------------------------------------------------------------
1536 // ASOT Initialization Function Helper - Initialization of Audio Frame
1537 //
1538 //   Name:      PAF_ASOT_initFrame0
1539 //   Purpose:   Audio Stream Output Task Function for initialization of the Audio
1540 //              Frame(s) by memory allocation and loading of data pointers
1541 //              and values.
1542 //   From:      AST Parameter Function -> decodeInfo
1543 //   Uses:      See code.
1544 //   States:    x
1545 //   Return:    0 on success.
1546 //              Source code line number on MEM_calloc failure.
1547 //              Source code line number on unsupported option.
1548 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1549 //              * Memory allocation errors.
1550 //              * Unsupported option errors.
1551 //
1553 // MID 314
1554 extern const char AFChanPtrMap[PAF_MAXNUMCHAN+1][PAF_MAXNUMCHAN];
1555 extern PAF_ChannelConfigurationMaskTable PAF_ASP_stdCCMT_patch;
1557 Int
1558 PAF_ASOT_initFrame0(
1559     const PAF_ASOT_Params *pP, 
1560     const PAF_ASOT_Patchs *pQ, 
1561     PAF_ASOT_Config *pAsotCfg, 
1562     Int z
1565     PAF_AST_Config *pAstCfg;
1566     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1567     Int ch;
1568     //Int aLen;
1569     Int aLen_int=0,aLen_ext=0;
1570     Int aSize = sizeof(PAF_AudioData);
1571     Int aAlign = aSize < sizeof (int) ? sizeof (int) : aSize;
1572     Int maxFrameLength = pP->maxFramelength;
1573     Int zX;
1574     PAF_AudioData *aBuf_int=NULL;
1575     PAF_AudioData *aBuf_ext=NULL;
1576     XDAS_UInt8 *metadataBuf;
1577     char i;
1578     Error_Block    eb;
1580     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1581     as = pAstCfg->as;
1583     // Initialize error block
1584     Error_init(&eb); 
1586     // Compute maximum framelength (needed for ARC support)
1587     maxFrameLength += PA_MODULO - maxFrameLength % PA_MODULO;
1588     //aLen = numchan[z] * maxFrameLength;
1589     for (i=0; i < numchan[z]; i++)
1590     {
1591         if (pP->pAudioFrameBufStatus->space[i] == IALG_SARAM)
1592         {
1593             aLen_int += maxFrameLength;
1594         }
1595         else
1596         {
1597             aLen_ext += maxFrameLength;
1598         }
1599     }
1601     //
1602     // Initialize audio frame elements directly
1603     //
1604     pAstCfg->xStr[z].pAudioFrame->fxns = pP->pAudioFrameFunctions;
1605     pAstCfg->xStr[z].pAudioFrame->data.nChannels = PAF_MAXNUMCHAN; ///
1606 ///    pAstCfg->xStr[z].pAudioFrame->data.nChannels = PAF_MAXNUMCHAN_AF;
1607     pAstCfg->xStr[z].pAudioFrame->data.nSamples = FRAMELENGTH;
1608     pAstCfg->xStr[z].pAudioFrame->data.sample = pAstCfg->xStr[z].audioFrameChannelPointers;
1609     pAstCfg->xStr[z].pAudioFrame->data.samsiz = pAstCfg->xStr[z].audioFrameChannelSizes;
1610     pAstCfg->xStr[z].pAudioFrame->pChannelConfigurationMaskTable = &PAF_ASP_stdCCMT;
1612     //
1613     // Allocate memory for and initialize pointers to audio data buffers
1614     //
1615     //   The NUMCHANMASK is used to identify the channels for which data
1616     //   buffers can be allocated. Using this mask and switch statement
1617     //   rather than some other construct allows efficient code generation,
1618     //   providing just the code necessary (with significant savings).
1619     //
1620     if (pP->fxns->bufMemPrint)
1621     {
1622         pP->fxns->bufMemPrint(z, aLen_int*aSize, HEAP_ID_FRMBUF, 2);
1623         pP->fxns->bufMemPrint(z, aLen_ext*aSize, HEAP_ID_EXTERNAL, 2);
1624     }
1626     TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc for audio buffers", as+z);
1627     
1628     if (aLen_int*aSize!=0) // check size != 0, otherwise malloc throws fatal error
1629     {
1630         if (!(aBuf_int = (PAF_AudioData *)Memory_calloc((IHeap_Handle)HEAP_FRMBUF, (aLen_int+(maxFrameLength-FRAMELENGTH))*aSize, aAlign, &eb))) //Qin: Add start offset
1631         {
1632             TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc failed", as+z);
1633             TRACE_TERSE2("  maxFrameLength: %d.  aLen_int*aSize: %d", maxFrameLength, (aLen_int+(maxFrameLength-FRAMELENGTH))*aSize);
1634             SW_BREAKPOINT;
1635             return __LINE__;
1636         }
1637     }
1638         
1639     if (aLen_ext*aSize!=0)
1640     {
1641         if (!(aBuf_ext = (PAF_AudioData *)Memory_calloc((IHeap_Handle)HEAP_EXTERNAL, (aLen_ext+(maxFrameLength-FRAMELENGTH))*aSize, aAlign, &eb)))//Qin: Add start offset
1642         {
1643             TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc failed", as+z);
1644             TRACE_TERSE2("  maxFrameLength: %d.  aLen_ext*aSize: %d", maxFrameLength, (aLen_ext+(maxFrameLength-FRAMELENGTH))*aSize);
1645             SW_BREAKPOINT;
1646             return __LINE__;
1647         }
1648     }
1649     
1650     TRACE_TERSE3("  maxFrameLength: %d.  aLen_int*aSize: %d.  aBuf_int: 0x%x", maxFrameLength, (aLen_int+(maxFrameLength-FRAMELENGTH))*aSize, (IArg)aBuf_int);
1651     TRACE_TERSE3("  maxFrameLength: %d.  aLen_ext*aSize: %d.  aBuf_ext: 0x%x", maxFrameLength, (aLen_ext+(maxFrameLength-FRAMELENGTH))*aSize, (IArg)aBuf_ext);
1653     TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc for metadata buffers", as+z);
1654     if (!(metadataBuf = (XDAS_UInt8 *)Memory_calloc((IHeap_Handle)HEAP_MDBUF, pP->pMetadataBufStatus->bufSize*pP->pMetadataBufStatus->NumBuf, pP->pMetadataBufStatus->alignment, &eb)))
1655     {
1656         TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc failed", as+z);
1657         TRACE_TERSE1("  bufSize*NumBuf: %d", pP->pMetadataBufStatus->bufSize*pP->pMetadataBufStatus->NumBuf);
1658         SW_BREAKPOINT;
1659         return __LINE__;
1660     }
1662     {
1663         Int i;
1665 #pragma UNROLL(1)
1666         for (i=0; i < PAF_MAXNUMCHAN_AF; i++)
1667         {
1668             pAstCfg->xStr[z].audioFrameChannelPointers[i] = NULL;
1669         }
1670     }
1672     // MID 314
1673     if((numchan[z] > PAF_MAXNUMCHAN) || (numchan[z] < 1)) 
1674     {
1675         TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: unsupported option", as+z);
1676         return __LINE__;
1677     }
1678     else 
1679     {
1680         Int j = 0;
1681         Int k = 0;
1682         TRACE_TERSE1("PAF_ASOT_initFrame0: AFChanPtrMap[%d][i]", numchan[z]);
1683         for(i=0;i<numchan[z];i++)
1684         {
1685             char chan = AFChanPtrMap[numchan[z]][i];
1686             if(chan != -1)
1687             {
1688                 if(pP->pAudioFrameBufStatus->space[i] == IALG_SARAM)
1689                 {
1690                     pAstCfg->xStr[z].audioFrameChannelPointers[chan] = aBuf_int + maxFrameLength*(j+1) - FRAMELENGTH;
1691                     j++;
1692                 }
1693                 else
1694                 {        
1695                     pAstCfg->xStr[z].audioFrameChannelPointers[chan] = aBuf_ext + maxFrameLength*(k+1) - FRAMELENGTH;
1696                     k++;
1697                 }    
1698                 TRACE_TERSE3("PAF_ASOT_initFrame0: chan = %d = AFChanPtrMap[%d][%d].", chan, numchan[z], i);
1699                 TRACE_TERSE2("PAF_ASOT_initFrame0: audioFrameChannelPointers[%d]: 0x%x", chan, (IArg)pAstCfg->xStr[z].audioFrameChannelPointers[chan]);
1700             }
1701         }
1702     }
1704     for (ch=PAF_LEFT; ch < PAF_MAXNUMCHAN_AF; ch++) 
1705     {
1706         if (pAstCfg->xStr[z].audioFrameChannelPointers[ch])
1707         {
1708             pAstCfg->xStr[z].origAudioFrameChannelPointers[ch] = pAstCfg->xStr[z].audioFrameChannelPointers[ch];
1709         }
1710     }
1712     //
1713     // Initialize meta data elements
1714     //
1715     pAstCfg->xStr[z].pAudioFrame->pafBsMetadataUpdate = XDAS_FALSE;
1716     pAstCfg->xStr[z].pAudioFrame->numPrivateMetadata = 0;
1717     pAstCfg->xStr[z].pAudioFrame->bsMetadata_offset = 0;
1718     pAstCfg->xStr[z].pAudioFrame->bsMetadata_type = PAF_bsMetadata_channelData;
1719     pAstCfg->xStr[z].pAudioFrame->privateMetadataBufSize = pP->pMetadataBufStatus->bufSize;
1720     for(i=0;i<pP->pMetadataBufStatus->NumBuf;i++)
1721     {
1722         pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].offset = 0;
1723         pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].size = 0;
1724         pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].pMdBuf = metadataBuf + pP->pMetadataBufStatus->bufSize*i;
1725     }
1727     //
1728     // Initialize decoder elements directly
1729     //
1731     for (zX = DECODE1; zX < DECODEN; zX++) 
1732     {
1733         if (pP->streamsFromDecodes[zX] == z) 
1734         {
1735 #ifdef NOAUDIOSHARE
1736             pAstCfg->xDec[zX].decodeInStruct.audioShare.nSamples = 0;
1737             pAstCfg->xDec[zX].decodeInStruct.audioShare.sample = NULL;
1738 #else /* NOAUDIOSHARE */
1739             pAstCfg->xDec[zX].decodeInStruct.audioShare.nSamples = aLen_int;
1740             pAstCfg->xDec[zX].decodeInStruct.audioShare.sample = aBuf_int;
1741 #endif /* NOAUDIOSHARE */
1742         }
1743     }
1745     return 0;
1746 } //PAF_ASOT_initFrame0
1748 // -----------------------------------------------------------------------------
1749 // ASOT Initialization Function Helper - Reinitialization of Audio Frame
1750 // AST Decoding Function              - Reinitialization of Audio Frame
1751 //
1752 //   Name:      PAF_ASOT_initFrame1
1753 //   Purpose:   Audio Stream Task Function for initialization or reinitiali-
1754 //              zation of the Audio Frame(s) by loading of data values of a
1755 //              time-varying nature.
1756 //   From:      audioStream1Task or equivalent
1757 //              AST Parameter Function -> decodeInfo
1758 //              AST Parameter Function -> decodeDecode
1759 //   Uses:      See code.
1760 //   States:    x
1761 //   Return:    0.
1762 //   Trace:     None.
1763 //
1764 Int
1765 PAF_ASOT_initFrame1(
1766     const PAF_ASOT_Params *pP, 
1767     const PAF_ASOT_Patchs *pQ, 
1768     PAF_ASOT_Config *pAsotCfg, 
1769     Int z, 
1770     Int apply
1773     PAF_AST_Config *pAstCfg;
1775     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1777     //
1778     // Reinitialize audio frame elements:
1779     //
1780     //   Channel Configurations during sys init                 = Unknown
1781     //      "          "        during info or decode           = None
1782     //
1783     //   Sample Rate / Count    during sys init, info or decode = Unknown / 0
1784     //
1786     if (apply < 0) 
1787     {
1788         pAstCfg->xStr[z].pAudioFrame->channelConfigurationRequest.legacy = PAF_CC_UNKNOWN;
1789         pAstCfg->xStr[z].pAudioFrame->channelConfigurationStream.legacy = PAF_CC_UNKNOWN;
1790     }
1791     else 
1792     {
1793         pAstCfg->xStr[z].pAudioFrame->channelConfigurationRequest.legacy = PAF_CC_NONE;
1794         pAstCfg->xStr[z].pAudioFrame->channelConfigurationStream.legacy = PAF_CC_NONE;
1795     }
1797     if (apply < 1) 
1798     {
1799         pAstCfg->xStr[z].pAudioFrame->sampleRate = PAF_SAMPLERATE_UNKNOWN;
1800         pAstCfg->xStr[z].pAudioFrame->sampleCount = 0;
1801     }
1803     return 0;
1804 } //PAF_ASOT_initFrame1
1806 // -----------------------------------------------------------------------------
1807 // ASOT Selection Function - Output Device Selection
1808 //
1809 //   Name:      PAF_ASOT_selectDevices
1810 //   Purpose:   Audio Stream Output Task Function for selecting the devices used
1811 //              for output.
1812 //   From:      audioStream1Task or equivalent
1813 //   Uses:      See code.
1814 //   States:    x
1815 //   Return:    Error number in standard form (0 on success).
1816 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1817 //              * State information as per parent.
1818 //
1819 Int
1820 PAF_ASOT_selectDevices(
1821     const PAF_ASOT_Params *pP, 
1822     const PAF_ASOT_Patchs *pQ, 
1823     PAF_ASOT_Config *pAsotCfg
1826     PAF_AST_Config *pAstCfg;
1827     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1828     Int z;                      /* input/output counter */
1829     Int errno = 0;              /* error number */
1830     Int errme;                  /* error number, local */
1831     Int device;
1833     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1834     as = pAstCfg->as;
1835     (void)as;  // clear compiler warning in case not used with tracing disabled
1837     // Select output devices
1838     for (z=OUTPUT1; z < OUTPUTN; z++) 
1839     {
1840         if ((device = pAstCfg->xOut[z].outBufStatus.sioSelect) >= 0) 
1841         {
1842             TRACE_VERBOSE2("PAF_ASOT_selectDevices: AS%d: output device %d selecting ...", as+z, device);
1844             /* check for valid index into device array */
1845             if (device >= pQ->devout->n)
1846             {
1847                 device = 0; /* treat as device None */
1848             }
1850             errme = pP->fxns->deviceSelect(&pAstCfg->xOut[z].hTxSio, SIO_OUTPUT, 
1851                 HEAP_ID_OUTBUF, (Ptr)pQ->devout->x[device]);
1852             if (errme)
1853             {
1854                 TRACE_VERBOSE2("PAF_ASOT_selectDevices: errme 0x%x, errno 0x%x", errme, errno);
1855                 if (!errno)
1856                 {
1857                     errno = ASPERR_DEVOUT + errme;
1858                 }
1859                 pAstCfg->xOut[z].outBufStatus.sioSelect = 0x80;
1860             }
1861             else 
1862             {
1863                 Int zE;
1865                 pAstCfg->xOut[z].outBufStatus.sioSelect = device | 0x80;
1866                 // register outBufStatus and encodeStatus pointers with output devices
1867                 // This enables proper IEC encapsulation.
1868                 if (pAstCfg->xOut[z].hTxSio) 
1869                 {
1870                     // set max # of output buffers (use override if necessary)
1871                     if (pAstCfg->xOut[z].outBufStatus.maxNumBufOverride == 0)
1872                     {
1873                         SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_MAX_NUMBUF,
1874                             (Arg)pP->poutNumBufMap[z]->maxNumBuf);
1875                     }
1876                     else
1877                     {
1878                         SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_MAX_NUMBUF,
1879                             (Arg)pAstCfg->xOut[z].outBufStatus.maxNumBufOverride);
1880                     }
1882                     // register PAF_SIO_IALG object address
1883                     SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_IALGADDR,
1884                         (Arg)pAstCfg->xOut[z].outChainData.head->alg);
1885                     SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_BUFSTATUSADDR, 
1886                         (Arg)&pAstCfg->xOut[z].outBufStatus);
1887                     for (zE=ENCODE1; zE < ENCODEN; zE++) 
1888                     {
1889                         if (pP->outputsFromEncodes[zE] == z) 
1890                         {
1891                             SIO_ctrl(pAstCfg->xOut[z].hTxSio, 
1892                                 PAF_SIO_CONTROL_SET_ENCSTATUSADDR, 
1893                                 (Arg)&pAstCfg->xEnc[zE].encodeStatus);
1894                             break;
1895                         }
1896                     }
1897                 }
1898             }
1899         }
1901 /* No need to start-clocks here, since only selecting the device. */
1902 #if 0
1903         // if device selected and valid then enable stat tracking if
1904         // required and start clocking
1905         if ((pAstCfg->xOut[z].outBufStatus.sioSelect < 0) && (pAstCfg->xOut[z].hTxSio)) 
1906         {
1907             TRACE_VERBOSE0("PAF_ASOT_selectDevices: start SIO clocks");
1908             errme = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_OUTPUT_START_CLOCKS, 0);
1909             if (errme)
1910             {
1911                 TRACE_VERBOSE2("PAF_ASOT_selectDevices: errme 0x%x, errno 0x%x", errme, errno);
1912                 SIO_idle(pAstCfg->xOut[z].hTxSio);
1913                 if (!errno)
1914                 {
1915                     errno = ASPERR_DEVOUT + errme;
1916                 }
1917             }
1918         }
1919 #endif
1920     }
1922     return errno;
1923 } //PAF_ASOT_selectDevices
1926 // -----------------------------------------------------------------------------
1927 // ASOT Processing Function - Decode Processing
1928 //
1929 //   Name:      PAF_ASOT_decodeProcessing
1930 //   Purpose:   Audio Stream Output Task Function for processing audio data.
1931 //
1932 Int
1933 PAF_ASOT_decodeProcessing(
1934     const PAF_ASOT_Params *pP, 
1935     const PAF_ASOT_Patchs *pQ, 
1936     PAF_ASOT_Config *pAsotCfg 
1939     PAF_AST_Config *pAstCfg;
1940     Int errno;                          /* error number */
1941     Int getVal;
1942     enum { INIT, STREAM, ENCODE, FINAL, QUIT, OUT_SIO_UPDATE } state;
1943     state = INIT;
1944     errno = 0; /* error number */
1945     Int frame; // (***) FL: formerly -- decoder input frame count
1946     Int block; // decoder output block count / input frame
1947     Int outSioUpdate;
1948     
1949     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1951     for (;;) 
1952     {
1953         //
1954         // Check forward (ASIT) error here, TBD
1955         //
1956         
1957         // If writeDECCommandRestart issued, force exit the statemachine
1958         if (gCommandOutputTask_SYNC) {
1959             TRACE_VERBOSE1("PAF_ASOT_decodeProcessing: writeDECCommandRestart issued in state=0x%x ... exiting core loop", state);
1960             errno = ASOP_DP_FORWARD_ERR;
1961             break;
1962         }
1964         // Check if any change in output SIO, e.g. from Output shortcut.
1965         // Changes will break FSM and allow Output reconfiguration.
1966         errno = checkOutSio(pP, pAsotCfg, &outSioUpdate);
1967         if (errno < 0)
1968         {
1969             TRACE_TERSE1("PAF_ASOT_decodeProcessing: checkOutSio returned errno = 0x%04x", errno);
1970             break;
1971         }
1972         else if (outSioUpdate)
1973         {
1974             TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: Change in Output SIO selection");
1975             state = OUT_SIO_UPDATE;
1976         }
1977         
1978         // Process commands (encode)
1979         getVal = pP->fxns->encodeCommand(pP, pQ, pAsotCfg);
1980         if (getVal) 
1981         {
1982             /* ignore */;
1983         }
1985         // Process state (decode)
1986         switch (state) 
1987         {
1988             case INIT: // initial state
1989                 gAsopInitCnt++;
1990                 Log_info0("TaskAsop: state=INIT");
1991             
1992                 frame = 0;
1993                 block = 0;
1995 #if 0 // FL: moved to PAF_ASOT_initOutProc()
1996                 // Reset audio frame pointers to original values
1997                 // (may be needed if error occurred).
1998                 resetAfPointers(pP, pAstCfg->xStr);
1999                 // Reset audio frame meta data elements
2000                 resetAfMetadata(pP, pAstCfg->xStr);
2001 #endif
2003                 errno = pP->fxns->decodeInit(pP, pQ, pAsotCfg);
2004                 if (errno)
2005                 {
2006                     TRACE_VERBOSE1("PAF_ASOT_decodeProcessing: INIT, errno 0x%x.  break after decodeInit", errno);
2007                     errno = ASOP_DP_DECINIT_ERR;
2008                     break;
2009                 }
2010                 
2011                 //
2012                 // Setup output: setCheckRateX, start output
2013                 //
2014                 // Establish secondary timing
2015                 errno = pP->fxns->decodeInfo1(pP, pQ, pAsotCfg, frame, block);
2016                 if (errno)
2017                 {
2018                     TRACE_VERBOSE1("PAF_ASOT_decodeProcessing: INIT, errno 0x%x.  break after decodeInfo1", errno);
2019                     break;
2020                 }
2021                 
2022                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: INIT->STREAM");                
2023                 state = STREAM;
2024                 continue;
2025                 
2026             case STREAM: // stream state
2027                 gAsopStreamCnt++;
2028                 Log_info0("TaskAsop: state=STREAM");
2030                 errno = pP->fxns->decodeStream(pP, pQ, pAsotCfg, frame, block);
2031                 if (errno)
2032                 {
2033                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: state: STREAM.  decodeStream err 0x%x", errno);
2034                     errno = ASOP_DP_DECSTREAM_ERR;
2035                     break;
2036                 }
2038                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: STREAM->ENCODE");
2039                 state = ENCODE;
2040                 continue;
2041                 
2042             case ENCODE: // encode state
2043                 gAsopEncodeCnt++;
2044                 Log_info0("TaskAsop: state=ENCODE");
2046                 errno = pP->fxns->decodeEncode(pP, pQ, pAsotCfg, frame, block);
2047                 if (errno)
2048                 {
2049                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: state: ENCODE.  decodeEncode err 0x%x", errno);
2050                     errno = ASOP_DP_DECENC_ERR;
2051                     break;
2052                 }
2053                 
2054                 // Measure cycles in output processing loop.
2055                 // Only measures cycles spent in loop.
2056                 pfpEnd(PFP_ID_ASOT_1, PFP_FINISH_MEAS);
2057                 gNumPfpAsot1--;
2058                 pfpBegin(PFP_ID_ASOT_1, pAsotCfg->taskHandle);
2059                 gNumPfpAsot1++;
2060                 
2061                 // (***) FL: do we need this? 
2062                 //       AF pointers come from CB read, any resets occur in Decoder AF.
2063                 //
2064                 // Reset audio frame pointers to original values
2065                 // (may have been adjusted by ARC or the like).
2066                 resetAfPointers(pP, pAstCfg->xStr);
2068                 // (***) FL: update output (setCheckRateX)
2069                 //           Contained in INFO2 in combined FSM.
2070                 errno = pP->fxns->decodeInfo2(pP, pQ, pAsotCfg, frame, block);
2071                 if (errno)
2072                 {
2073                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: ENCODE break on decodeInfo2. errno 0x%x", errno);
2074                     errno = ASOP_DP_DECINFO2_ERR;
2075                     break;
2076                 }
2078                 block++;
2079                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: ENCODE->FINAL");
2080                 state = FINAL;
2081                 continue;
2082                 
2083             case FINAL:
2084                 gAsopFinalCnt++;
2085                 Log_info0("TaskAsop: state=FINAL");
2086                 
2087                 //
2088                 // (***) FL: this needs to be fixed.
2089                 //       (1) Only require selected Output to be in this FSM
2090                 //           => Dec Status checks aren't valid, 
2091                 //              will probably always exit FSM if only Output running
2092                 //       (2) Checking Dec Status info asych to input events (maybe ok)
2093                 //
2094                 // Check for final frame, and if indicated:
2095                 // - Update audio flag to cause output buffer flush rather than
2096                 //   the default truncate in "complete" processing.
2097                 // - Exit state machine to "complete" processing.
2098 #if 0
2099                 if (pP->fxns->decodeFinalTest(pP, pQ, pAsotCfg, frame, block)) 
2100                 {
2101                     for (z=OUTPUT1; z < OUTPUTN; z++)
2102                     {
2103                         if ((pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_SOUND)
2104                         {
2105                             TRACE_VERBOSE0("PAF_ASOT_outputProcessing: state: FINAL: SOUND -> QUIET");
2106                             pAstCfg->xOut[z].outBufStatus.audio++; // SOUND -> QUIET
2107                         }
2108                     }
2109                     break;
2110                 }
2111 #endif
2112                 errno = pP->fxns->decodeFinalTest(pP, pQ, pAsotCfg, frame, block);
2113                 if (errno < 0)
2114                 {
2115                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: DECODE FINAL break. errno 0x%x", errno);
2116                     errno = ASOP_DP_DECFINALTEST_ERR;
2117                     break;
2118                 }
2119                 else if (errno == ASOP_DP_CB_DRAINED)
2120                 {
2121                     // EOS, exit normally
2122                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: DECODE FINAL normal exit. errno 0x%x", errno);
2123                     errno = ASOP_DP_SOK;
2124                     break;
2125                 }
2127                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: FINAL->STREAM");
2128                 state = STREAM;
2129                 continue;
2130                 
2131             case OUT_SIO_UPDATE:
2132                 gAsopOutSioUpdateCnt++;
2133                 Log_info0("TaskAsop: state=OUT_SIO_UPDATE");
2135                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: OUT_SIO_UPDATE");
2136                 errno = ASOP_DP_OUT_SIO_UPDATE;
2137                 break;
2138                 
2139             case QUIT:
2140                 gAsopQuitCnt++;
2141                 Log_info0("TaskAsop: state=QUIT");
2143                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: QUIT");
2144                 errno = ASPERR_QUIT;
2145                 break;
2147             default: // unknown state
2148                 // Unknown:
2149                 // - Set error number registers.
2150                 // - Exit state machine to "complete" processing.
2152                 TRACE_TERSE1("PAF_ASOT_decodeProcessing: state: unknown, 0x%x", state);
2153                 errno = ASPERR_UNKNOWNSTATE;
2154                 break;
2156         }  // End of switch (state).
2157         
2158         TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: Calling decode complete");
2159         if (pP->fxns->decodeComplete(pP, pQ, pAsotCfg, NULL, frame, block))
2160         {
2161             /* ignored? */;
2162         }
2163         
2164         //pfpEnd(PFP_ID_ASOT_1, PFP_FINISH_MEAS); // PFP end -- outside of PFP for errors, EOS, or Output SIO change
2165         //gNumPfpAsot1--;
2166         
2167         //return errno;
2168         break;        
2169     } // End of for (;;)
2170         
2171     pfpEnd(PFP_ID_ASOT_1, PFP_FINISH_MEAS); // PFP end -- outside of PFP for errors, EOS, or Output SIO change
2172     gNumPfpAsot1--;
2173         
2174     return errno;
2177 // -----------------------------------------------------------------------------
2178 // ASOT Decoding Function - Encode Command Processing
2179 //
2180 //   Name:      PAF_ASOT_encodeCommand
2181 //   Purpose:   Decoding Function for processing Encode Commands.
2182 //   From:      AST Parameter Function -> decodeProcessing
2183 //   Uses:      See code.
2184 //   States:    x
2185 //   Return:    0.
2186 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2187 //              * Command execution.
2188 //              * SIO control errors.
2189 //              * Error number macros.
2190 //
2191 Int
2192 PAF_ASOT_encodeCommand(
2193     const PAF_ASOT_Params *pP, 
2194     const PAF_ASOT_Patchs *pQ, 
2195     PAF_ASOT_Config *pAsotCfg
2198     PAF_AST_Config *pAstCfg;
2199     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2200     Int z;                      /* encode counter */
2201     Int errno = 0;              /* error number */
2202     Int zO, zS;
2205     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2206     as = pAstCfg->as;
2208     for (z=ENCODE1; z < ENCODEN; z++) 
2209     {
2210         zO = pP->outputsFromEncodes[z];
2211         zS = pP->streamsFromEncodes[z];
2212         if (! (pAstCfg->xEnc[z].encodeStatus.command2 & 0x80)) 
2213         {
2214             switch (pAstCfg->xEnc[z].encodeStatus.command2) 
2215             {
2216                 case 0: // command none - process
2217                     pAstCfg->xEnc[z].encodeStatus.command2 |= 0x80;
2218                     break;
2219                 case 1: // mute command
2220                     TRACE_VERBOSE2("AS%d: PAF_ASOT_encodeCommand: encode command mute (0x%02x)", as+zS, 1);
2221                     if ((pAstCfg->xOut[zO].outBufStatus.audio & 0x0f) != PAF_OB_AUDIO_QUIET
2222                         && pAstCfg->xOut[zO].hTxSio
2223                         && (errno = SIO_ctrl (pAstCfg->xOut[zO].hTxSio, PAF_SIO_CONTROL_MUTE, 0))) 
2224                     {
2225                         errno = (errno & 0xff) | ASPERR_MUTE;
2226                         /* convert to sensical errno */
2227                         TRACE_TERSE1("AS%d: PAF_ASOT_encodeCommand: SIO control failed (mute)", as+zS);
2228                         TRACE_TERSE2("AS%d: PAF_ASOT_encodeCommand: errno = 0x%04x <ignored>", as+zS, errno);
2229                     }
2230                     else 
2231                     {
2232                         pAstCfg->xOut[zO].outBufStatus.audio |= PAF_OB_AUDIO_MUTED;
2233                     }
2234                     pAstCfg->xEnc[z].encodeStatus.command2 |= 0x80;
2235                     break;
2236                 case 2: // unmute command
2237                     TRACE_VERBOSE2("AS%d: PAF_ASOT_encodeCommand: encode command unmute (0x%02x)", as+zS, 2);
2238                     if ((pAstCfg->xOut[zO].outBufStatus.audio & 0x0f) != PAF_OB_AUDIO_QUIET
2239                         && pAstCfg->xOut[zO].hTxSio
2240                         && (errno = SIO_ctrl (pAstCfg->xOut[zO].hTxSio, PAF_SIO_CONTROL_UNMUTE, 0))) 
2241                     {
2242                         errno = (errno & 0xff) | ASPERR_MUTE;
2243                         /* convert to sensical errno */
2244                         TRACE_TERSE1("AS%d: PAF_ASOT_encodeCommand: SIO control failed (unmute)", as+zS);
2245                         TRACE_TERSE2("AS%d: PAF_ASOT_encodeCommand: errno = 0x%04x <ignored>", as+zS, errno);
2246                     }
2247                     else 
2248                     {
2249                         pAstCfg->xOut[zO].outBufStatus.audio &= ~PAF_OB_AUDIO_MUTED;
2250                     }
2251                     pAstCfg->xEnc[z].encodeStatus.command2 |= 0x80;
2252                     break;
2253                 default: // command unknown - ignore
2254                     break;
2255             }
2256         }
2257     }
2259     ERRNO_RPRT (TaskAsop, errno);
2261     return 0;
2262 } //PAF_ASOT_encodeCommand
2264 //debug -- allow dynamic config
2265 //Int16 gStrFrameLen=DEF_STR_FRAME_LEN; // stream frame length (PCM samples)
2267 //   Purpose:   Decoding Function for reinitializing the decoding process.
2268 Int
2269 PAF_ASOT_decodeInit(
2270     const PAF_ASOT_Params *pP, 
2271     const PAF_ASOT_Patchs *pQ, 
2272     PAF_ASOT_Config *pAsotCfg
2275     //PAF_AST_Config *pAstCfg;
2276     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
2277     //Int as;                             /* Audio Stream Number (1, 2, etc.) */
2278     Int z;                              /* decode/encode counter */
2279     Int errno;                          /* error number */
2280     //Int zO, zS;
2282     //pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2283     //as = pAstCfg->as;
2285     pCbCtl = &pAsotCfg->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
2287     for (z=DECODE1; z < DECODEN; z++)
2288     {
2289         // Initialize decoder output circular buffer for stream reads
2290         //errno = cbInitStreamRead(pCbCtl, z, gStrFrameLen);
2291         errno = cbInitStreamRead(pCbCtl, z);
2292         if (errno)
2293         {
2294             TRACE_TERSE1("PAF_ASOT_decodeInit:cbInitStreamRead() error=%d", errno);
2295             SW_BREAKPOINT; // debug
2296             return errno;
2297         }
2298         
2299         // Start decoder output circular buffer reads
2300         errno = cbReadStart(pCbCtl, z);
2301         if (errno)
2302         {
2303             TRACE_TERSE1("PAF_ASOT_decodeInit:cbReadStart() error=%d", errno);
2304             SW_BREAKPOINT; // debug
2305             return errno;
2306         }
2307         
2308         gCbReadAfErr=0;         // reset read circular buffer error count
2309         gDecOpCbRdAfUnd=0;      // reset decoder output circular buffer underflow count
2310         gMaxDecOpCbRdAfUnd=0;   // reset max decoder output circular buffer underflow count
2311         gMasterCbResetCnt=0;    // reset master circular buffer reset count
2313         // FL: debug, log circular buffer control variables
2314         cbLog(pCbCtl, z, 1, "PAF_ASOT_decodeInit:cbReadStart");
2315     }
2316     
2317 #if 0 // moved to PAF_ASOT_outputReset()
2318     // TODO: move this to start of this function so that it doesn't affect IO timing
2319     for (z=ENCODE1; z < ENCODEN; z++) 
2320     {
2321         zO = pP->outputsFromEncodes[z];
2322         zS = pP->streamsFromEncodes[z];
2323         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2324         {
2325             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2326             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2327             ENC_Handle enc = (ENC_Handle )encAlg;
2328             TRACE_VERBOSE1("AS%d: PAF_ASOT_decodeInit: initializing encode", as+zS);
2329             if (encAlg->fxns->algActivate)
2330             {
2331                 encAlg->fxns->algActivate (encAlg);
2332             }
2333             if (enc->fxns->reset)
2334             {
2335                 errno = enc->fxns->reset(enc, NULL, 
2336                     &pAstCfg->xEnc[z].encodeControl, &pAstCfg->xEnc[z].encodeStatus);
2337                 if (errno)
2338                 {
2339                     return errno;
2340                 }
2341             }
2342         }
2343     }
2344 #endif
2345     
2346     return 0;
2349 // -----------------------------------------------------------------------------
2350 // ASOT Decoding Function - Info Processing, Initial
2351 //
2352 //   Name:      PAF_ASOT_decodeInfo1
2353 //   Purpose:   Decoding Function for processing information in a manner that
2354 //              is unique to initial frames of input data.
2355 //   From:      AST Parameter Function -> decodeProcessing
2356 //   Uses:      See code.
2357 //   States:    x
2358 //   Return:    Error number in standard or SIO form (0 on success).
2359 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2360 //              * State information as per parent.
2361 //
2362 Int
2363 PAF_ASOT_decodeInfo1(
2364     const PAF_ASOT_Params *pP, 
2365     const PAF_ASOT_Patchs *pQ, 
2366     PAF_ASOT_Config *pAsotCfg, 
2367     Int frame, 
2368     Int block
2371     //PAF_AST_Config *pAstCfg;
2372     //Int z;                              /* decode/encode counter */
2373     Int errno;                          /* error number */
2375 #if 0 // moved to PAF_ASOT_outputInfo1()
2376     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2378     // run the chain of ASP's on the stream.
2379     TRACE_VERBOSE0("PAF_ASOT_decodeInfo1: calling streamChainFunction.");
2380     errno = pP->fxns->streamChainFunction(pP, pQ, pAsotCfg, 
2381         PAF_ASP_CHAINFRAMEFXNS_RESET, 1, frame);
2382     if (errno)
2383     {
2384         TRACE_TERSE1("PAF_ASOT_decodeInfo1: streamChainFunction returns errno 0x%x ", errno);
2385         return errno;
2386     }
2388     TRACE_VERBOSE0("PAF_ASOT_decodeInfo1: calling enc->info.");
2389     for (z=ENCODE1; z < ENCODEN; z++) 
2390     {
2391         Int zO = pP->outputsFromEncodes[z];
2392         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2393         {
2394             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2395             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2396             ENC_Handle enc = (ENC_Handle )encAlg;
2397             
2398             if (enc->fxns->info)
2399             {
2400                 errno = enc->fxns->info(enc, NULL,
2401                     &pAstCfg->xEnc[z].encodeControl, 
2402                     &pAstCfg->xEnc[z].encodeStatus);
2403                 if (errno)
2404                 {
2405                     TRACE_TERSE1("PAF_ASOT_decodeInfo1: info returns errno 0x%x ", errno);
2406                     return errno;
2407                 }
2408             }
2409         }
2410     }
2411 #endif    
2413     errno = pP->fxns->setCheckRateX(pP, pQ, pAsotCfg, 0);
2414     if (errno)
2415     {
2416         // ignore if rateX has changed since we haven't, but are about to,
2417         // start the output. If we didn't ignore this case then the state machine
2418         // would restart unnecessarily, e.g. in the case of SRC, resulting in
2419         // added latency.
2420         if (errno != ASPERR_INFO_RATECHANGE)
2421         {
2422             TRACE_TERSE1("PAF_ASOT_decodeInfo1: setCheckRateX returns errno 0x%x, not RATECHANGE", errno);
2423             return errno;
2424         }
2425         else
2426         {
2427             TRACE_TERSE0("PAF_ASOT_decodeInfo1: RATECHANGE returns RATECHANGE, ignoring");
2428         }
2429     }
2431     errno = pP->fxns->startOutput(pP, pQ, pAsotCfg);
2432     if (errno) 
2433     {
2434         if (errno == 0x105) 
2435         {
2436             TRACE_TERSE1("PAF_ASOT_decodeInfo1: startOutput returns RING BUFFER FULL (0x%x)", errno);
2437         }
2438         else
2439         {
2440             TRACE_TERSE1("PAF_ASOT_decodeInfo1: startOutput returns errno 0x%x", errno);
2441         }
2442         return errno;
2443     }
2444     
2445     return 0;
2448 // -----------------------------------------------------------------------------
2449 // ASOT Decoding Function - Info Processing, Subsequent
2450 //
2451 //   Name:      PAF_ASOT_decodeInfo2
2452 //   Purpose:   Decoding Function for processing information in a manner that
2453 //              is unique to frames of input data other than the initial one.
2454 //   From:      AST Parameter Function -> decodeProcessing
2455 //   Uses:      See code.
2456 //   States:    x
2457 //   Return:    Error number in standard form (0 on success).
2458 //   Trace:     None.
2459 //
2460 Int
2461 PAF_ASOT_decodeInfo2(
2462     const PAF_ASOT_Params *pP, 
2463     const PAF_ASOT_Patchs *pQ, 
2464     PAF_ASOT_Config *pAsotCfg, 
2465     Int frame, 
2466     Int block
2469     //PAF_AST_Config *pAstCfg;
2470     Int errno;
2472     
2473     //pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2475     errno = pP->fxns->setCheckRateX (pP, pQ, pAsotCfg, 1);
2476     TRACE_VERBOSE1("PAF_ASOT_decodeInfo2: return 0x%x", errno);
2477     return errno;
2478 } //PAF_ASOT_decodeInfo2
2481 PAF_AST_DecOpCircBufStats gCbStats; // FL: debug
2482 // -----------------------------------------------------------------------------
2483 // ASOT Decoding Function - Stream Processing
2484 //
2485 //   Name:      PAF_ASOT_decodeStream
2486 //   Purpose:   Decoding Function for processing of audio frame data by the
2487 //              ASP Algorithms.
2488 //   From:      AST Parameter Function -> decodeProcessing
2489 //   Uses:      See code.
2490 //   States:    x
2491 //   Return:    Error number in standard form (0 on success).
2492 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2493 //              * State information as per parent/child.
2494 //
2495 Int
2496 PAF_ASOT_decodeStream(
2497     const PAF_ASOT_Params *pP, 
2498     const PAF_ASOT_Patchs *pQ, 
2499     PAF_ASOT_Config *pAsotCfg, 
2500     Int frame, 
2501     Int block
2504     PAF_AST_Config *pAstCfg;
2505     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
2506     Int z;                              /* decode/stream counter */
2507     PAF_AudioFrame *pAfRd;
2508     Int cbErrno;
2509     //PAF_AST_DecOpCircBufStats cbStats;  /* circular buffer statistics */
2510     Int errno;
2513     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2514     
2515     pCbCtl = &pAsotCfg->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
2516     
2517     for (z=DECODE1; z < DECODEN; z++) 
2518     {
2519         Int zS = pP->streamsFromDecodes[z];
2520         
2521         //
2522         // Read decoder output circular buffer
2523         //
2524         pAfRd = pAstCfg->xStr[zS].pAudioFrame;
2525         //GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106);       // debug
2526         cbErrno = cbReadAf(pCbCtl, z, pAfRd);
2527         //GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);     // debug
2528         if ((cbErrno < 0) && 
2529             (cbErrno != ASP_DECOP_CB_AF_READ_UNDERFLOW) && 
2530             (cbErrno != ASP_DECOP_CB_PCM_READ_UNDERFLOW))
2531         {
2532             gCbReadAfErr++;
2533             TRACE_TERSE1("PAF_ASOT_decodeStream:cbReadAf() error=%d", cbErrno);
2534             //SW_BREAKPOINT; // debug
2535             return cbErrno;
2536         }
2538         // Handle underflows
2539         if ((cbErrno == ASP_DECOP_CB_AF_READ_UNDERFLOW) ||
2540             (cbErrno == ASP_DECOP_CB_PCM_READ_UNDERFLOW))
2541         {
2542             // (***) FL: Need to check behavior of cbReset().
2543             // Need to check behavior on exit/re-entry into Output processing.
2544             gDecOpCbRdAfUnd++; // increment circular buffer underflow count
2545             if (gDecOpCbRdAfUnd >= DEC_OP_CB_RDAF_UND_THR) 
2546             {
2547                 // Underflow count above threshold.
2548                 // (1) set max underflow count to threshold
2549                 // (2) reset underflow count
2550                 // (3) reset circular buffer
2551                 
2552                 gMaxDecOpCbRdAfUnd = DEC_OP_CB_RDAF_UND_THR; // update max underflow count
2553                 gDecOpCbRdAfUnd = 0; // reset underflow count
2555                 // Reset circular buffer
2556                 cbReset(pCbCtl, z);
2557                 gMasterCbResetCnt++; // increment master circular buffer reset count
2558                 Log_info0("ASOT:cbReset");
2559             
2560                 return cbErrno;
2561             }
2562         }
2563         else if ((cbErrno == ASP_DECOP_CB_SOK) && (gDecOpCbRdAfUnd > 0))
2564         {
2565             // No underflow detected.
2566             // update max underflow count,
2567             // reset underflow count
2568             
2569             // update max underflow count
2570             if (gDecOpCbRdAfUnd > gMaxDecOpCbRdAfUnd)
2571             {
2572                 gMaxDecOpCbRdAfUnd = gDecOpCbRdAfUnd;
2573             }
2574             gDecOpCbRdAfUnd = 0; // reset circular buffer underflow count
2575         }
2576         //Log_info0("PAF_ASOT_decodeStream:cbReadAf() complete.");
2577         //GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);   // debug
2578         Log_info0("PAF_ASOT_decodeStream:cbReadAf() complete.");
2579         
2580 #if 0 // (***) FL: shows timing of CB read
2581             // (***) debug // B8
2582             {
2583                 static Uint8 toggleState = 0;
2584                 if (toggleState == 0)
2585                     GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106);
2586                 else
2587                     GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);
2588                 toggleState = ~(toggleState);
2589             }
2590 #endif
2592         // debug, get circular buffer statistics
2593         //cbGetStats(pCbCtl, z, &cbStats);
2594         cbGetStats(pCbCtl, z, &gCbStats);
2596         // debug
2597         cbLog(pCbCtl, z, 1, "PAF_ASOT_decodeStream:cbReadAf");
2598         
2599 #if 0 // debug, capture audio frame
2600         if (capAfWrite(pAfRd, PAF_LEFT) != CAP_AF_SOK)
2601         {
2602             Log_info0("capAfWrite() error");
2603         }
2604 #endif
2605     }
2606             
2607     TRACE_VERBOSE0("PAF_ASOT_outputStream: calling streamChainFunction.");
2608     errno = pP->fxns->streamChainFunction(pP, pQ, pAsotCfg, 
2609         PAF_ASP_CHAINFRAMEFXNS_APPLY, 1, block);
2610     if (errno)
2611     {
2612         TRACE_TERSE1("PAF_ASOT_outputStream: streamChainFunction returns errno 0x%x ", errno);
2613         return errno;
2614     }
2616     return 0;
2618 } //PAF_ASOT_decodeStream
2620 // -----------------------------------------------------------------------------
2621 // ASOT Decoding Function - Encode Processing
2622 //
2623 //   Name:      PAF_ASOT_decodeEncode
2624 //   Purpose:   Decoding Function for processing of audio frame data by the
2625 //              Encode Algorithm.
2626 //   From:      AST Parameter Function -> decodeProcessing
2627 //   Uses:      See code.
2628 //   States:    x
2629 //   Return:    Error number in standard or SIO form (0 on success).
2630 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2631 //              * State information as per parent.
2632 //
2633 Int
2634 PAF_ASOT_decodeEncode(
2635     const PAF_ASOT_Params *pP, 
2636     const PAF_ASOT_Patchs *pQ, 
2637     PAF_ASOT_Config *pAsotCfg, 
2638     Int frame, 
2639     Int block
2642     PAF_AST_Config *pAstCfg;
2643     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2644     Int z;                      /* encode/output counter */
2645     Int errno;                  /* error number */
2646     Int zX, zE, zS;
2647     // debug
2648     //UInt32 curTime;
2650     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2651     as = pAstCfg->as;
2653     // Await output buffers (but not first time)
2654     for (z=OUTPUT1; z < OUTPUTN; z++) 
2655     {
2656         // determine encoder associated with this output
2657         zE = z;
2658         for (zX = ENCODE1; zX < ENCODEN; zX++) 
2659         {
2660             if (pP->outputsFromEncodes[zX] == z) 
2661             {
2662                 zE = zX;
2663                 break;
2664             }
2665         }
2666         zS = pP->streamsFromEncodes[zE];
2668         if (pAstCfg->xOut[z].hTxSio) 
2669         {
2670             // update length (e.g. ARC may have changed)
2671             pAstCfg->xOut[z].outBufConfig.lengthofFrame = 
2672                 pAstCfg->xEnc[zE].encodeInStruct.pAudioFrame->sampleCount;
2673             TRACE_GEN2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- idle", as+zS, block);
2674             //GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);   // debug
2675             errno = SIO_reclaim(pAstCfg->xOut[z].hTxSio,(Ptr *) &pAstCfg->xOut[z].pOutBuf, NULL);
2676             //GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106);     // debug
2677             if (errno < 0)
2678             {
2679                 SIO_idle(pAstCfg->xOut[z].hTxSio);
2680                 TRACE_TERSE2("PAF_ASOT_decodeEncode: AS%d: SIO_reclaim returns error %d", as+zS, -errno);
2681                 return -errno; // SIO negates error codes
2682             }
2683             // TODO: use pC->xOut[z].pOutBuf in following ->encode call
2685 #if 0 // (***) FL: shows timing of Output Tx SIO reclaim
2686             // (***) debug // B8
2687             {
2688                 static Uint8 toggleState = 0;
2689                 if (toggleState == 0)
2690                     GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106);
2691                 else
2692                     GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);
2693                 toggleState = ~(toggleState);
2694             }
2695 #endif            
2697             //
2698             // Simulate Tx SIO_reclaim() pend
2699             //
2700             //Semaphore_pend(semaphoreTxAudio, BIOS_WAIT_FOREVER);
2701             //curTime = Clock_getTicks();
2702             //System_printf("System time in TaskAsopFxn Tx audio = %lu\n", (ULong)curTime);
2703             //Log_info1("outputEncode():Tx SIO reclaim(), system time = %u", curTime);
2704             
2705             gAsopTxSioReclaimCnt++;
2706         }
2707         else 
2708         {
2709             TRACE_VERBOSE2("AS%d: PAF_ASOT_decodeEncode: processing block %d -- idle <ignored>", as+zS, block);
2710         }
2711     }
2713     // Encode data
2714     for (z=ENCODE1; z < ENCODEN; z++) 
2715     {
2716         Int zO = pP->outputsFromEncodes[z];
2717         Int zS = pP->streamsFromEncodes[z];
2718         (void)zS; // clear compiler warning in case not used with tracing disabled
2719         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2720         {
2721             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2722             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2723             ENC_Handle enc = (ENC_Handle )encAlg;
2724             if (select != pAstCfg->xEnc[z].encodeControl.encActive)
2725             {
2726                 pAstCfg->xEnc[z].encodeControl.encActive = select;
2727                 TRACE_TERSE0("PAF_ASOT_decodeEncode: return error");
2728                 return (-1);
2729             }
2730             TRACE_GEN2("AS%d: PAF_ASOT_decodeEncode: processing block %d -- encode", as+zS, block);
2732             // (MID 1933) temp. workaround for PCE2
2733             pAstCfg->xEnc[z].encodeInStruct.pAudioFrame->data.nChannels = PAF_MAXNUMCHAN;
2735           /*
2736           #if (CURRENT_TRACE_MASK & TRACE_MASK_DATA)
2737             {
2738                 PAF_AudioFrame *pAudioFrame = pC->xEnc[z].encodeInStruct.pAudioFrame;
2739                 int *wp;
2740                 wp = (int*)pAudioFrame->data.sample[0];
2741                 TRACE_DATA((&TR_MOD, "as1-f2: AS%d PAF_ASOT_outputEncode: encoding from ch 0 0x%x. line %d", z, wp, __LINE__));
2742                 TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x (ch0)", wp[0], wp[16], wp[99]));
2743                 wp = (int*)pAudioFrame->data.sample[1];
2744                 TRACE_DATA((&TR_MOD, "as1-f2: PAF_ASOT_outputEncode: encoding from ch 1 0x%x. line %d", wp, __LINE__));
2745                 TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x (ch1)", wp[0], wp[16], wp[99]));
2746                 wp = (int*)pAudioFrame->data.sample[2];
2747                 TRACE_DATA((&TR_MOD, "as1-f2: PAF_ASOT_outputEncode: encoding from ch 2 0x%x. line %d", wp, __LINE__));
2748                 TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x (ch2)", wp[0], wp[16], wp[99]));
2749             }
2750           #endif
2751           */
2753             if (enc->fxns->encode)
2754             {
2755                 pAstCfg->xEnc[z].encodeOutStruct.bypassFlag =
2756                         pP->z_pEncodeStatus[z]->encBypass;
2757                 errno = enc->fxns->encode(enc, NULL, 
2758                     &pAstCfg->xEnc[z].encodeInStruct, 
2759                     &pAstCfg->xEnc[z].encodeOutStruct);
2760                 if (errno)
2761                 {
2762                     if (errno != PCEERR_OUTPUT_POINTERNULL)
2763                     {
2764                         TRACE_TERSE1("PAF_ASOT_decodeEncode: return error %d line %d", errno);
2765                         return errno;
2766                     }
2767                 }
2768             /*  #if (CURRENT_TRACE_MASK & TRACE_MASK_DATA)
2769                 else
2770                 {
2771                     int *wp = (int*)pC->xOut[z].pOutBuf->pntr.pVoid;
2772                     TRACE_DATA((&TR_MOD, "as1-f2: PAF_ASOT_outputEncode: encoded to 0x%x. line %d", wp, __LINE__));
2773                     TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x", wp[0], wp[16], wp[99]));
2774                 }
2775               #endif
2776               */
2777             }
2778         }
2779         else 
2780         {
2781             TRACE_VERBOSE2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- encode <ignored>",
2782                 as+pP->streamsFromEncodes[z], block);
2783         }
2784     }
2786     // Transmit data
2787     for (z=OUTPUT1; z < OUTPUTN; z++) 
2788     {
2789         // determine encoder associated with this output
2790         zE = z;
2791         for (zX = ENCODE1; zX < ENCODEN; zX++) 
2792         {
2793             if (pP->outputsFromEncodes[zX] == z) 
2794             {
2795                 zE = zX;
2796                 break;
2797             }
2798         }
2799         zS = pP->streamsFromEncodes[zE];
2801         if (pAstCfg->xOut[z].hTxSio) 
2802         {
2803             TRACE_GEN2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- output", as+zS, block);
2804             //GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106);       // debug
2805             errno = SIO_issue(pAstCfg->xOut[z].hTxSio, 
2806                 &pAstCfg->xOut[z].outBufConfig, sizeof (pAstCfg->xOut[z].outBufConfig), 0);
2807             //GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);     // debug
2808             if (errno)
2809             {
2810                 SIO_idle(pAstCfg->xOut[z].hTxSio);
2811                 if (errno == 0x105)     // 0x105 == RINGIO_EBUFFULL
2812                 {
2813 //                    statStruct_LogFullRing(STATSTRUCT_AS1_F2);
2814                     TRACE_TERSE1("PAF_ASOT_decodeEncode: SIO_idle returned RINGIO_EBUFFULL (0x%x)", errno);
2815                 }
2816                 if (errno > 0)
2817                 {
2818                     TRACE_TERSE1("PAF_ASOT_decodeEncode: return error 0x%x line %d", errno);
2819                     return (ASPERR_ISSUE + (z << 4));
2820                 }
2821                 else if (errno < 0)
2822                 {
2823                     TRACE_TERSE1("PAF_ASOT_decodeEncode: return neg error 0x%x line %d", -errno);
2824                     return -errno; // SIO negates error codes
2825                 }
2826             }
2827             if (errno > 0)
2828             {
2829                 return (ASPERR_ISSUE + (z << 4));
2830             }
2831             else if (errno < 0)
2832             {
2833                 return -errno; // SIO negates error codes
2834             }
2835         }
2836         else 
2837         {
2838             TRACE_GEN2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- output <ignored>", as+zS, block);
2839         }
2840     }
2842     return 0;
2843 } //PAF_ASOT_decodeEncode
2845 // -----------------------------------------------------------------------------
2846 // ASOT Decoding Function - Stream-Final Processing
2847 //
2848 //   Name:      PAF_ASOT_decodeComplete
2849 //   Purpose:   Decoding Function for terminating the decoding process.
2850 //   From:      AST Parameter Function -> decodeProcessing
2851 //   Uses:      See code.
2852 //   States:    x
2853 //   Return:    0.
2854 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2855 //              * State information as per parent.
2856 //
2857 Int
2858 PAF_ASOT_decodeComplete(
2859     const PAF_ASOT_Params *pP, 
2860     const PAF_ASOT_Patchs *pQ, 
2861     PAF_ASOT_Config *pAsotCfg, 
2862     ALG_Handle decAlg[], 
2863     Int frame, 
2864     Int block
2867     PAF_AST_Config *pAstCfg;
2868     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
2869     Int as;                             /* Audio Stream Number (1, 2, etc.) */
2870     Int z;                              /* decode/encode counter */
2871     Int errno;                          /* error number */
2873     
2874     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2875     as = pAstCfg->as;
2876     (void)as;  // clear compiler warning in case not used with tracing disabled
2878     pCbCtl = &pAsotCfg->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
2879     
2880     for (z=DECODE1; z < DECODEN; z++)
2881     {
2882         // Stop decoder output circular buffer reads
2883         errno = cbReadStop(pCbCtl, z);
2884         if (errno)
2885         {
2886             TRACE_TERSE1("PAF_ASOT_decodeComplete:cbReadStop() error=%d", errno);
2887             SW_BREAKPOINT; // FL: debug
2888             return errno;
2889         }
2890         // FL: debug
2891         cbLog(pCbCtl, z, 1, "PAF_ASOT_decodeComplete:cbReadStop");
2892     }
2893     
2894     pP->fxns->streamChainFunction(pP, pQ, pAsotCfg, PAF_ASP_CHAINFRAMEFXNS_FINAL, 0, frame);
2896     for (z=ENCODE1; z < ENCODEN; z++) 
2897     {
2898         Int zO = pP->outputsFromEncodes[z];
2899         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2900         {
2901             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2902             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2903 #ifdef PAF_ASP_FINAL
2904             ENC_Handle enc = (ENC_Handle)encAlg;
2905 #endif /* PAF_ASP_FINAL */
2906             TRACE_VERBOSE1("PAF_ASOT_decodeComplete: AS%d: finalizing encode", as+z);
2907 #ifdef PAF_ASP_FINAL
2908             if (enc->fxns->final)
2909                 enc->fxns->final(enc, NULL, &pAstCfg->xEnc[z].encodeControl,
2910                                  &pAstCfg->xEnc[z].encodeStatus);
2911 #endif /* PAF_ASP_FINAL */
2912             if (encAlg->fxns->algDeactivate)
2913             {
2914                 encAlg->fxns->algDeactivate(encAlg);
2915             }
2916         }
2917         else 
2918         {
2919             TRACE_VERBOSE1("PAF_ASOT_decodeComplete: AS%d: finalizing encode <ignored>", as+z);
2920         }
2921     }
2923     // wait for remaining data to be output
2924     pP->fxns->stopOutput(pP, pQ, pAsotCfg);
2926     return 0;
2927 } //PAF_ASOT_decodeComplete
2929 // -----------------------------------------------------------------------------
2930 // ASOT Decoding Function Helper - SIO Driver Start
2931 //
2932 //   Name:      PAF_ASOT_startOutput
2933 //   Purpose:   Decoding Function for initiating output.
2934 //   From:      AST Parameter Function -> decodeInfo1
2935 //   Uses:      See code.
2936 //   States:    x
2937 //   Return:    Error number in standard or SIO form (0 on success).
2938 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2939 //              * State information as per parent.
2940 //              * SIO control errors.
2941 //
2942 #define DEC_OUTNUMBUF_MAP(X) \
2943       pP->poutNumBufMap[z]->map[(X) >= pP->poutNumBufMap[z]->length ? 0 : (X)]
2945 Int
2946 PAF_ASOT_startOutput(
2947     const PAF_ASOT_Params *pP, 
2948     const PAF_ASOT_Patchs *pQ, 
2949     PAF_ASOT_Config *pAsotCfg
2950
2952     PAF_AST_Config *pAstCfg;
2953     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2954     Int z;                      /* output counter */
2955     Int errno,nbufs, errme;            /* error number */
2956     Int zE, zS, zX;
2957     Int zMD;
2958     PAF_SIO_IALG_Obj    *pObj;
2959     PAF_SIO_IALG_Config *pAlgConfig;
2961     
2962     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2963     as = pAstCfg->as;
2964     zMD = pAstCfg->masterDec;
2966     for (z=OUTPUT1; z < OUTPUTN; z++) 
2967     {
2968         if (pAstCfg->xOut[z].hTxSio) 
2969         {
2970             // determine associated encoder and stream
2971             zE = z;
2972             zS = z;
2973             for (zX = ENCODE1; zX < ENCODEN; zX++) 
2974             {
2975                 if (pP->outputsFromEncodes[zX] == z) 
2976                 {
2977                     zE = zX;
2978                     zS = pP->streamsFromEncodes[zE];
2979                     break;
2980                 }
2981             }
2983 // Need to Revisit: Starting Clocks here seems logical & also manages the McASP without spurious underruns .
2985 #if 1
2986             // if device selected and valid then enable stat tracking if
2987                         // required and start clocking
2988                         if ((pAstCfg->xOut[z].outBufStatus.sioSelect < 0) && (pAstCfg->xOut[z].hTxSio))
2989                         {
2990                                 TRACE_VERBOSE0("PAF_ASOT_startOutput: start SIO clocks");
2991                                 errme = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_OUTPUT_START_CLOCKS, 0);
2992                                 if (errno)
2993                                 {
2994                                         TRACE_VERBOSE2("PAF_ASOT_startOutput: errme 0x%x, errno 0x%x", errme, errno);
2995                                         SIO_idle(pAstCfg->xOut[z].hTxSio);
2996                                         if (!errno)
2997                                         {
2998                                                 errno = ASPERR_DEVOUT + errme;
2999                                         }
3000                                 }
3001                         }
3002 #endif
3003             // Set sample count so that DOB knows how much data to send
3004             pAstCfg->xOut[z].outBufConfig.lengthofFrame =
3005                 pAstCfg->xEnc[zE].encodeInStruct.pAudioFrame->sampleCount;
3007             if (pAstCfg->xOut[z].outBufStatus.markerMode == PAF_OB_MARKER_ENABLED) 
3008             {
3009                 pObj = (PAF_SIO_IALG_Obj *) pAstCfg->xOut[z].outChainData.head->alg;
3010                 pAlgConfig = &pObj->config;
3011                 memset(pAstCfg->xOut[z].outBufConfig.base.pVoid, 0xAA, 
3012                     pAlgConfig->pMemRec[0].size);
3013             }
3015             // The index to DEC_OUTNUMBUF_MAP will always come from the primary/master
3016             // decoder. How should we handle the sourceProgram for multiple decoders?
3017             // Override as needed
3018             nbufs = DEC_OUTNUMBUF_MAP(pAstCfg->xDec[zMD].decodeStatus.sourceProgram);
3019             if (pAstCfg->xOut[z].outBufStatus.numBufOverride[pAstCfg->xDec[zMD].decodeStatus.sourceProgram] > 0)
3020             {
3021                 nbufs = pAstCfg->xOut[z].outBufStatus.numBufOverride[pAstCfg->xDec[zMD].decodeStatus.sourceProgram];
3022             }
3023             SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_NUMBUF, nbufs);
3025             if (errno = SIO_issue(pAstCfg->xOut[z].hTxSio,
3026                 &pAstCfg->xOut[z].outBufConfig, sizeof(pAstCfg->xOut[z].outBufConfig), 0)) 
3027             {
3028                 SIO_idle(pAstCfg->xOut[z].hTxSio);
3029                 TRACE_TERSE2("PAF_ASOT_startOutput: AS%d: SIO_issue failed (0x%x)", as+zS, errno);
3030                 return errno;
3031             }
3033             if (!(pAstCfg->xOut[z].outBufStatus.audio & 0xf0) && 
3034                 (errno =  SIO_ctrl (pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_UNMUTE, 0))) 
3035             {
3036                 errno = (errno & 0xff) | ASPERR_MUTE;
3037                 /* convert to sensical errno */
3038                 TRACE_TERSE2("as1-f2: PAF_ASOT_startOutput: AS%d: SIO control failed (unmute) 0x%x", as+zS, errno);
3039                 return (errno);
3040             }
3041             else
3042             {
3043                 pAstCfg->xOut[z].outBufStatus.audio
3044                     = (pAstCfg->xOut[z].outBufStatus.audio & 0xf0) | PAF_OB_AUDIO_SOUND;                
3045             }
3047             TRACE_VERBOSE1("PAF_ASOT_startOutput: AS%d: output started", as+zS);
3048         }
3049     }
3051     return 0;
3052 } //PAF_ASOT_startOutput
3054 // -----------------------------------------------------------------------------
3055 // ASOT Decoding Function Helper - SIO Driver Stop
3056 //
3057 //   Name:      PAF_ASOT_stopOutput
3058 //   Purpose:   Decoding Function for terminating output.
3059 //   From:      AST Parameter Function -> decodeProcessing
3060 //              AST Parameter Function -> decodeComplete
3061 //   Uses:      See code.
3062 //   States:    x
3063 //   Return:    Error number in standard or SIO form (0 on success).
3064 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
3065 //              * SIO control errors.
3066 //
3067 Int
3068 PAF_ASOT_stopOutput(
3069     const PAF_ASOT_Params *pP, 
3070     const PAF_ASOT_Patchs *pQ, 
3071     PAF_ASOT_Config *pAsotCfg
3074     PAF_AST_Config *pAstCfg;
3075     Int as;                     /* Audio Stream Number (1, 2, etc.) */
3076     Int z;                      /* output counter */
3077     Int errno = 0, getVal;
3078     Int zS, zX;
3079     PAF_SIO_IALG_Obj    *pObj;
3080     PAF_SIO_IALG_Config *pAlgConfig;
3082     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3083     as = pAstCfg->as;
3084     (void)as;  // clear compiler warning in case not used with tracing disabled
3086     for (z=OUTPUT1; z < OUTPUTN; z++) 
3087     {
3088         if (pAstCfg->xOut[z].hTxSio) 
3089         {
3090             // determine associated encoder and stream
3091             zS = z;
3092             (void)zS;
3093             for (zX = ENCODE1; zX < ENCODEN; zX++) 
3094             {
3095                 if (pP->outputsFromEncodes[zX] == z) 
3096                 {
3097                     zS = pP->streamsFromEncodes[zX];
3098                     break;
3099                 }
3100             }
3102             // Mute output before audio data termination in the usual case,
3103             // where such termination is due to decode error or user command.
3104             // Identification of this as the usual case is provided by the
3105             // "decode processing" state machine.
3106             if (!(pAstCfg->xOut[z].outBufStatus.audio & 0xf0) &&
3107                 ((pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_SOUND) &&
3108                 (getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_MUTE, 0))) 
3109             {
3110                 if (!errno) 
3111                 {
3112                     errno = (getVal & 0xff) | ASPERR_MUTE;
3113                     /* convert to sensical errno */
3114                 }
3115                 TRACE_VERBOSE1("PAF_ASOT_stopOutput:  AS%d: SIO control failed (mute)", as+zS);
3116             }
3118             TRACE_TIME((&TIME_MOD, "... + %d = %d (stopOutput -- begin PAF_SIO_CONTROL_IDLE)", dtime(), TSK_time()));
3120             // Terminate audio data output, truncating (ignore) or flushing
3121             // (play out) final samples as per (1) control register set by
3122             // the user and (2) the type of audio data termination:
3124 #if 0
3125             // This form is not used because driver support for truncating
3126             // data is not supported for internal clocks, although it is
3127             // for external clocks.
3128             getVal = SIO_ctrl(pC->xOut[z].hTxSio, PAF_SIO_CONTROL_IDLE,
3129                 pC->xOut[z].outBufStatus.flush
3130                 & (pC->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_FLUSH
3131                 ? 1 : 0);
3132             /* UNTESTED */
3133 #else
3134             // This form should be used when driver support for truncating
3135             // data is supported for both internal and external clocks.
3136             getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_IDLE,
3137                 pAstCfg->xOut[z].outBufStatus.flush ? 1 :
3138                 (pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_FLUSH
3139                 ? 1 : 0);
3140             /* TESTED */
3141 #endif
3143             TRACE_TIME((&TIME_MOD, "... + %d = %d (stopOutput -- after PAF_SIO_CONTROL_IDLE)", dtime(), TSK_time()));
3145             if (!errno)
3146             {
3147                 errno = getVal;
3148             }
3150             // Mute output after audio data termination in a special case,
3151             // where such termination is due to processing of a final frame
3152             // or user command. Identification of this as a special case is
3153             // provided by the "decode processing" state machine.
3154             if (!(pAstCfg->xOut[z].outBufStatus.audio & 0xf0) &&
3155                 ((pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_FLUSH) &&
3156                 (getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_MUTE, 0)))
3157             {
3158                 if (!errno) 
3159                 {
3160                     errno = (getVal & 0xff) | ASPERR_MUTE;
3161                     /* convert to sensical errno */
3162                 }
3163                 TRACE_VERBOSE1("as1-f2: PAF_ASOT_stopOutput:  AS%d: SIO control failed (mute)", as+zS);
3164             }
3166             pAstCfg->xOut[z].outBufStatus.audio &= ~0x0f;
3168             // zero output buffers
3169             pObj = (PAF_SIO_IALG_Obj *) pAstCfg->xOut[z].outChainData.head->alg;
3170             pAlgConfig = &pObj->config;
3171             memset (pAstCfg->xOut[z].outBufConfig.base.pVoid, 0, pAlgConfig->pMemRec[0].size);
3172         } //pAstCfg->xOut[z].hTxSio
3173     }//OUTPUT
3175     return errno;
3176 } //PAF_ASOT_stopOutput
3179 // -----------------------------------------------------------------------------
3180 // ASOT Decoding Function Helper - SIO Driver Change
3181 //
3182 //   Name:      PAF_ASOT_setCheckRateX
3183 //   Purpose:   Decoding Function for reinitiating output.
3184 //   From:      AST Parameter Function -> decodeInfo1
3185 //              AST Parameter Function -> decodeInfo2
3186 //   Uses:      See code.
3187 //   States:    x
3188 //   Return:    Error number in standard form (0 on success).
3189 //   Trace:     None.
3190 //
3192 /* 0: set, 1: check, unused for now. --Kurt */
3193 Int
3194 PAF_ASOT_setCheckRateX(
3195     const PAF_ASOT_Params *pP, 
3196     const PAF_ASOT_Patchs *pQ, 
3197     PAF_ASOT_Config *pAsotCfg, 
3198     Int check
3201     PAF_AST_Config *pAstCfg;
3202     float rateX;
3203     PAF_SampleRateHz rateO /* std */, rateI /* inv */;
3204     Int z;                              /* output counter */
3205     Int zx;                             /* output re-counter */
3206     Int getVal;
3207     int inputRate, inputCount, outputRate, outputCount;
3208     Int zMD;
3209     Int zMI;
3210     Int zMS;
3211     Int zE, zX;
3213     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3214     zMD = pAstCfg->masterDec;
3215     zMS = pAstCfg->masterStr;
3216     zMI = pP->zone.master;
3218     inputRate = pAstCfg->xInp[zMI].inpBufStatus.sampleRateStatus;
3219     inputCount = pAstCfg->xDec[zMD].decodeStatus.frameLength;
3220     rateI = pAstCfg->xStr[zMS].pAudioFrame->fxns->sampleRateHz
3221         (pAstCfg->xStr[zMS].pAudioFrame, inputRate, PAF_SAMPLERATEHZ_INV);
3223     for (z=OUTPUT1; z < OUTPUTN; z++) 
3224     {
3225         if (pAstCfg->xOut[z].hTxSio && (pAstCfg->xOut[z].outBufStatus.clock & 0x01)) 
3226         {
3227             // determine associated encoder
3228             zE = z;
3229             for (zX = ENCODE1; zX < ENCODEN; zX++) 
3230             {
3231                 if (pP->outputsFromEncodes[zX] == z) 
3232                 {
3233                     zE = zX;
3234                     break;
3235                 }
3236             }
3238             outputRate = pAstCfg->xEnc[zE].encodeStatus.sampleRate;
3239             outputCount = pAstCfg->xEnc[zE].encodeStatus.frameLength;
3240             rateO = pAstCfg->xStr[zMS].pAudioFrame->fxns->sampleRateHz
3241                 (pAstCfg->xStr[zMS].pAudioFrame, outputRate, PAF_SAMPLERATEHZ_STD);
3242             if ((rateI > 0) && (rateO > 0))
3243             {
3244                 rateX = rateO /* std */ * rateI /* inv */;
3245             }
3246             else if (inputCount != 0)
3247             {
3248                 rateX = (float )outputCount / inputCount;
3249             }
3250             else
3251             {
3252                 return ASPERR_INFO_RATERATIO;
3253             }
3255             getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_RATEX, (Arg)&rateX);
3256             if (getVal == DOBERR_RATECHANGE) 
3257             {
3258                 for (zx=OUTPUT1; zx < OUTPUTN; zx++)
3259                 {
3260                     if (pAstCfg->xOut[zx].hTxSio)
3261                     {
3262                         SIO_idle (pAstCfg->xOut[zx].hTxSio);
3263                     }
3264                 }
3266                 // this forces an exit from the calling state machine which will
3267                 // eventually call startOutput which calls setCheckRateX for all outputs
3268                 // and so it is ok, in the presence of a rate change on any output, to
3269                 // exit this loop /function early.
3270                 return ASPERR_INFO_RATECHANGE;
3271             }
3272             else if (getVal != SYS_OK)
3273             {
3274                 return ((getVal & 0xff) | ASPERR_RATE_CHECK);
3275             }
3276         }
3277     }
3279     return 0;
3280 } //PAF_ASOT_setCheckRateX
3282 // -----------------------------------------------------------------------------
3283 // ASOT Decoding Function Helper - Chain Processing
3284 //
3285 //   Name:      PAF_ASOT_streamChainFunction
3286 //   Purpose:   Common Function for processing algorithm chains.
3287 //   From:      AST Parameter Function -> decodeInfo1
3288 //              AST Parameter Function -> decodeStream
3289 //              AST Parameter Function -> decodeComplete
3290 //   Uses:      See code.
3291 //   States:    x
3292 //   Return:    Error number in standard form (0 on success).
3293 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
3294 //              * State information as per parent.
3295 //
3296 Int
3297 PAF_ASOT_streamChainFunction(
3298     const PAF_ASOT_Params *pP, 
3299     const PAF_ASOT_Patchs *pQ, 
3300     PAF_ASOT_Config *pAsotCfg, 
3301     Int iChainFrameFxns, 
3302     Int abortOnError, 
3303     Int logArg
3306     PAF_AST_Config *pAstCfg;
3307     Int as;                     /* Audio Stream Number (1, 2, etc.) */
3308     Int z;                      /* stream counter */
3309     Int errno;                  /* error number */
3310     Int dFlag, eFlag, gear;
3311     Int zX;
3312     Int zS;
3314     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3315     as = pAstCfg->as;
3316     (void)as; // clear compiler warning in case not used with tracing disabled
3318     for (zS = STREAM1; zS < STREAMN; zS++)
3319     {
3320         z = pP->streamOrder[zS];  // Select stream order from streamOrder parameter - MID 788
3322         // apply stream
3323         //      unless the stream is associated with a decoder and it is not running
3324         // or
3325         //      unless the stream is associated with an encoder and it is not running
3326         // Also gear control only works for streams with an associated decoder
3327         // if no such association exists then gear 0 (All) is used
3328         dFlag = 1;
3329         gear = 0;
3330         for (zX = DECODE1; zX < DECODEN; zX++) {
3331             if (pP->streamsFromDecodes[zX] == z) {
3332                 dFlag = pAstCfg->xDec[zX].decodeStatus.mode;
3333                 gear = pAstCfg->xDec[zX].decodeStatus.aspGearStatus;
3334                 break;
3335             }
3336         }
3337         eFlag = 1;
3338         for (zX = ENCODE1; zX < ENCODEN; zX++) {
3339             if (pP->streamsFromEncodes[zX] == z) {
3340                 eFlag = pAstCfg->xEnc[zX].encodeStatus.mode;
3341                 break;
3342             }
3343         }
3345         if (dFlag && eFlag) {
3346             PAF_ASP_Chain *chain = pAstCfg->xStr[z].aspChain[gear];
3347             PAF_AudioFrame *frame = pAstCfg->xStr[z].pAudioFrame;
3348             Int (*func) (PAF_ASP_Chain *, PAF_AudioFrame *) =
3349                 chain->fxns->chainFrameFunction[iChainFrameFxns];
3351             TRACE_GEN2(iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_RESET
3352                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (reset)"
3353                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_APPLY
3354                        ? "PAF_ASOT_streamChainFunction: AS%d: processing block %d -- audio stream (apply)"
3355                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_FINAL
3356                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (final)"
3357                        : "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (?????)",
3358                        as+z, logArg);
3359             errno = (*func) (chain, frame);
3360             TRACE_VERBOSE2("PAF_ASOT_streamChainFunction: AS%d: errno 0x%x.",
3361                 as+z, errno);
3363             if (errno && abortOnError)
3364                 return errno;
3365         }
3366         else {
3367             TRACE_GEN2(iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_RESET
3368                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (reset) <ignored>"
3369                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_APPLY
3370                        ? "PAF_ASOT_streamChainFunction: AS%d: processing block %d -- audio stream (apply) <ignored>"
3371                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_FINAL
3372                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (final) <ignored>"
3373                        : "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (?????) <ignored>",
3374                        as+z, logArg);
3375         }
3377         /*
3378         {
3379             void dp_tracePAF_Data(float *lBuf, float *rBuf, int count);
3380             PAF_AudioFrameData *afd;
3381             float ** afPtr;
3383             afd = &(pC->xStr->pAudioFrame->data);
3384             afPtr = (float**)afd->sample;
3385             dp_tracePAF_Data(afPtr[4], afPtr[5], 256);
3387         }
3388         */
3390     }
3392     return 0;
3393 } //PAF_ASOT_streamChainFunction
3395 /* Check if at least one output selected */
3396 static Int checkOutSel(
3397     const PAF_ASOT_Params *pP, 
3398     PAF_ASOT_Config *pAsotCfg,
3399     Int *pOutSel
3402     PAF_AST_Config *pAstCfg;
3403     Int outSel;
3404     Int z;
3405     
3406     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3408     outSel = 0;
3409     for (z=OUTPUT1; z < OUTPUTN; z++) 
3410     {
3411         if (pAstCfg->xOut[z].hTxSio)
3412         {
3413             outSel = 1;
3414             break;
3415         }
3416     }
3417     
3418     *pOutSel = outSel;
3420     return ASOP_SOK;
3423 /* Check if at least one output sio changed */
3424 static Int checkOutSio(
3425     const PAF_ASOT_Params *pP, 
3426     PAF_ASOT_Config *pAsotCfg,
3427     Int *pOutSioUpdate
3430     PAF_AST_Config *pAstCfg;
3431     Int outSioUpdate;
3432     Int z;
3433     
3434     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3436     outSioUpdate = 0;
3437     for (z=OUTPUT1; z < OUTPUTN; z++) 
3438     {
3439         if (pAstCfg->xOut[z].outBufStatus.sioSelect >= 0)
3440         {
3441             outSioUpdate = 1;
3442             break;
3443         }
3444     }
3445     
3446     *pOutSioUpdate = outSioUpdate;
3448     return ASOP_SOK;
3451 // Reset audio frames
3452 static Void resetAfs(
3453     const PAF_ASOT_Params *pP, 
3454     PAF_AST_Stream *xStr
3457     // Reset audio frame pointers to original values
3458     // (may be needed if error occurred).
3459     resetAfPointers(pP, xStr);
3460     // Reset audio frame meta data elements
3461     resetAfMetadata(pP, xStr);
3464 // Reset audio frame pointers to original values
3465 static Void resetAfPointers(
3466     const PAF_ASOT_Params *pP, 
3467     PAF_AST_Stream *xStr
3470     Int z;
3471     Int ch;
3473     // Reset audio frame pointers to original values
3474     for (z = STREAM1; z < STREAMN; z++) 
3475     {
3476         for (ch = PAF_LEFT; ch < PAF_MAXNUMCHAN_AF; ch++) 
3477         {
3478             if (xStr[z].audioFrameChannelPointers[ch])
3479             {
3480                 xStr[z].audioFrameChannelPointers[ch] = 
3481                     xStr[z].origAudioFrameChannelPointers[ch];
3482             }
3483         }
3484     }
3487 // Reset audio frame meta data elements
3488 static Void resetAfMetadata(
3489     const PAF_ASOT_Params *pP, 
3490     PAF_AST_Stream *xStr
3493     Int z;
3494     Int i;
3496     for (z = STREAM1; z < STREAMN; z++) 
3497     {
3498         xStr[z].pAudioFrame->pafBsMetadataUpdate = XDAS_FALSE;
3499         xStr[z].pAudioFrame->numPrivateMetadata = 0;
3500         xStr[z].pAudioFrame->bsMetadata_offset = 0;
3501         xStr[z].pAudioFrame->bsMetadata_type = PAF_bsMetadata_channelData;
3503         for (i = 0; i < pP->pMetadataBufStatus->NumBuf; i++)
3504         {
3505             xStr[z].pAudioFrame->pafPrivateMetadata[i].offset = 0;
3506             xStr[z].pAudioFrame->pafPrivateMetadata[i].size = 0;
3507         }
3508     }
3511 // Initialize Output Processing state function
3512 //static Int PAF_ASOT_initOutProc(
3513 Int PAF_ASOT_initOutProc(
3514     const PAF_ASOT_Params *pP, 
3515     PAF_AST_Stream *xStr
3518     // Reset audio frames
3519     resetAfs(pP, xStr);
3520     
3521     return ASOP_SOK;
3524 #if 0
3525 // Init-Sync update audio frame
3526 static Int initSyncUpdateAf(
3527     PAF_AudioFrame *dstAf, 
3528     PAF_AudioFrame *srcAf
3531     memcpy(dstAf, srcAf, sizeof(PAF_AudioFrame));
3532     
3533     return ASOP_SOK;
3535 #endif
3537 //   Purpose:   Init-Sync Dec Reset state function.
3538 //              Performes Dec Reset Init-Sync.
3539 //static Int PAF_ASOT_initSyncDecReset(
3540 // used by new OutProc.c, will be changed back to static once refactoring is done
3541 Int PAF_ASOT_initSyncDecReset(
3542     const PAF_ASOT_Params *pP, 
3543     const PAF_ASOT_Patchs *pQ, 
3544     PAF_ASOT_Config *pAsotCfg,
3545     PAF_AudioFrame *pDecResetAf
3548     PAF_AST_Config *pAstCfg;            // ASIT/ASOT/ASDT shared configuration
3549     Int zMD, zMS;                       // Dec and Stream Master indices
3550     PAF_AudioFrame *pStrAf;             // stream audio frame
3551     PAF_AST_OutInitSyncCtl *pOutIsCtl;  // Output Init-Sync control
3552     Int8 decFlag;                       // dec stage flag
3553     Int errno;                          // error number
3555     
3556     pAstCfg = pAsotCfg->pAstCfg;
3557     zMD = pAstCfg->masterDec;
3558     pOutIsCtl = &pAsotCfg->pAspmCfg->outIsCtl;
3559     
3560     // check for Dec Reset
3561     // store dec reset AF
3562     errno = outIsReadDecStageFlagAndAf(pOutIsCtl, zMD,
3563         ASP_OUTIS_DEC_STAGE_RESET_IDX, &decFlag, pDecResetAf);
3564     if (errno < 0)
3565     {
3566         return errno;
3567     }
3568     
3569     if (decFlag == 0)
3570     {
3571         return ASOP_INITSYNC_NOTREADY;
3572     }
3573     else
3574     {
3575         zMS = pAstCfg->masterStr;
3576         pStrAf = pAstCfg->xStr[zMS].pAudioFrame;
3577         
3578         // Update Stream Audio Frame.
3579         // Copy Dec Reset AF to Stream AF.
3580         //errno = initSyncUpdateAf(pStrAf, pDecResetAf);
3581         //if (errno < 0)
3582         //{
3583         //    return errno;
3584         //}
3585         outIsCpyAf(pDecResetAf, pStrAf);
3586         
3587         // Enc activate
3588         // Enc reset
3589         errno = PAF_ASOT_outputReset(pP, pQ, pAsotCfg);
3590         if (errno < 0)
3591         {
3592             return errno;
3593         }
3594         
3595         return ASOP_SOK;
3596     }
3599 //   Purpose:   ASOT Function for Output reset
3600 static Int PAF_ASOT_outputReset(
3601     const PAF_ASOT_Params *pP, 
3602     const PAF_ASOT_Patchs *pQ, 
3603     PAF_ASOT_Config *pAsotCfg
3606     PAF_AST_Config *pAstCfg;    // ASIT/ASOT/ASDT shared configuration
3607     Int as;                     // Audio Stream Number (1, 2, etc.) */
3608     Int z;                      // encode counter
3609     Int errno;                  // error number
3610     Int zO, zS;
3613     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3614     as = pAstCfg->as;
3616     for (z=ENCODE1; z < ENCODEN; z++) 
3617     {
3618         zO = pP->outputsFromEncodes[z];
3619         zS = pP->streamsFromEncodes[z];
3620         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
3621         {
3622             Int select = pAstCfg->xEnc[z].encodeStatus.select;
3623             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
3624             ENC_Handle enc = (ENC_Handle )encAlg;
3626             TRACE_VERBOSE1("AS%d: PAF_ASOT_outputReset: initializing encode", as+zS);
3628             if (encAlg->fxns->algActivate)
3629             {
3630                 encAlg->fxns->algActivate(encAlg);
3631             }
3632             
3633             if (enc->fxns->reset)
3634             {
3635                 errno = enc->fxns->reset(enc, NULL, 
3636                     &pAstCfg->xEnc[z].encodeControl, 
3637                     &pAstCfg->xEnc[z].encodeStatus);
3638                 if (errno)
3639                 {
3640                     return ASOP_ENCRESET_ERR;
3641                 }
3642             }
3643         }
3644     }    
3645     
3646     return ASOP_SOK;
3649 // FL: debug, allow modification of output frame length via JTAG
3650 Int16 gOutFrameLen=PAF_ASOT_FRAMELENGTH; // output frame length (PCM samples)
3652 //   Purpose:   Init-Sync Dec Info1 state function.
3653 //              Performes Dec Info1 Init-Sync.
3654 Int PAF_ASOT_initSyncDecInfo1(
3655     const PAF_ASOT_Params *pP, 
3656     const PAF_ASOT_Patchs *pQ, 
3657     PAF_ASOT_Config *pAsotCfg,
3658     PAF_AudioFrame *pDecInfo1Af
3661     PAF_AST_Config *pAstCfg;            // ASIT/ASOT/ASDT shared configuration
3662     Int zMD, zMS;                       // Dec and Stream Master indices
3663     PAF_AudioFrame *pStrAf;             // stream audio frame
3664     PAF_AST_OutInitSyncCtl *pOutIsCtl;  // Output Init-Sync control
3665     Int8 decFlag;                       // dec stage flag
3666     Int errno;                          // error number
3668     
3669     pAstCfg = pAsotCfg->pAstCfg;
3670     zMD = pAstCfg->masterDec;
3671     pOutIsCtl = &pAsotCfg->pAspmCfg->outIsCtl;
3672     
3673     // Check for Dec Reset, 
3674     // Store dec reset AF
3675     errno = outIsReadDecStageFlagAndAf(pOutIsCtl, zMD,
3676         ASP_OUTIS_DEC_STAGE_INFO1_IDX, &decFlag, pDecInfo1Af);
3677     if (errno < 0)
3678     {
3679         return errno;
3680     }
3681     
3682     if (decFlag == 0)
3683     {
3684         return ASOP_INITSYNC_NOTREADY;
3685     }
3686     else
3687     {
3688         zMS = pAstCfg->masterStr;
3689         pStrAf = pAstCfg->xStr[zMS].pAudioFrame;
3690         
3691         // Update Stream Audio Frame.
3692         // Copy Dec Reset AF to Stream AF.
3693         //errno = initSyncUpdateAf(pStrAf, pDecInfo1Af);
3694         //if (errno < 0)
3695         //{
3696         //    return errno;
3697         //}
3698         
3699         // Hack to set ASOT output frame length.
3700             // THD sets this to 256 (hard-coded in Dec Info)
3701             // DDP sets this to 0 (audio frame passthrough, 0 from ASDT AF frame length)
3702             // PCM sets this to 256 (decodeControl.frameLength)
3703         //pDecInfo1Af->sampleCount = 256;                       // !!!! GJ: Revisit !!!!
3704         pDecInfo1Af->sampleCount = gOutFrameLen;
3705         
3706         outIsCpyAf(pDecInfo1Af, pStrAf);
3708         // Hack to set ASOT output frame length
3709             // THD sets this to 256 (hard-coded in Dec Info)
3710             // DDP sets this to 0 (audio frame passthrough, 0 from ASDT AF frame length)
3711             // PCM sets this to 256 (decodeControl.frameLength)
3712         //pStrAf->sampleCount = 256;                    // !!!! GJ: Revisit !!!!
3714         // outputInfo1():
3715         //      - ASP chain reset,
3716         //      - Enc Info
3717         PAF_ASOT_outputInfo1(pP, pQ, pAsotCfg);
3718         if (errno)
3719         {
3720             return ASOP_DECINFO1_ERR;
3721         }
3722         
3723         return ASOP_SOK;
3724     }
3727 //   Purpose:   ASOT function for ASP chain reset and ENC info
3728 static Int PAF_ASOT_outputInfo1(
3729     const PAF_ASOT_Params *pP, 
3730     const PAF_ASOT_Patchs *pQ, 
3731     PAF_ASOT_Config *pAsotCfg
3734     PAF_AST_Config *pAstCfg;
3735     Int z;                              /* decode/encode counter */
3736     Int errno;                          /* error number */
3738     
3739     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3741     // FL: frame parameter hard-coded to 0
3742     // run the chain of ASP's on the stream.
3743     TRACE_VERBOSE0("PAF_ASOT_decodeInfo1: calling streamChainFunction.");
3744     errno = pP->fxns->streamChainFunction(pP, pQ, pAsotCfg, 
3745         PAF_ASP_CHAINFRAMEFXNS_RESET, 1, 0);
3746     if (errno)
3747     {
3748         TRACE_TERSE1("PAF_ASOT_outputInfo1: streamChainFunction returns errno 0x%x ", errno);
3749         return errno;
3750     }
3752     TRACE_VERBOSE0("PAF_ASOT_outputInfo1: calling enc->info.");
3753     for (z=ENCODE1; z < ENCODEN; z++) 
3754     {
3755         Int zO = pP->outputsFromEncodes[z];
3756         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
3757         {
3758             Int select = pAstCfg->xEnc[z].encodeStatus.select;
3759             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
3760             ENC_Handle enc = (ENC_Handle )encAlg;
3761             
3762             if (enc->fxns->info)
3763             {
3764                 errno = enc->fxns->info(enc, NULL,
3765                     &pAstCfg->xEnc[z].encodeControl, 
3766                     &pAstCfg->xEnc[z].encodeStatus);
3767                 if (errno)
3768                 {
3769                     TRACE_TERSE1("PAF_ASOT_outputInfo1: info returns errno 0x%x ", errno);
3770                     return errno;
3771                 }
3772             }
3773         }
3774     }
3775     
3776     return 0;
3779 //   Purpose:   Init-Sync Dec Decode1 state function.
3780 //              Performes Dec Decode1 Init-Sync.
3781 //static Int PAF_ASOT_initSyncDecDecode1(
3782 Int PAF_ASOT_initSyncDecDecode1(
3783     const PAF_ASOT_Params *pP, 
3784     const PAF_ASOT_Patchs *pQ, 
3785     PAF_ASOT_Config *pAsotCfg
3788     PAF_AST_Config *pAstCfg;            // ASIT/ASOT/ASDT shared configuration
3789     Int zMD;                            // Dec Master index
3790     PAF_AST_OutInitSyncCtl *pOutIsCtl;  // Output Init-Sync control
3791     Int8 decFlag;                       // dec stage flag
3792     Int errno;                          // error number
3794     
3795     pAstCfg = pAsotCfg->pAstCfg;
3796     zMD = pAstCfg->masterDec;
3797     pOutIsCtl = &pAsotCfg->pAspmCfg->outIsCtl;
3798     
3799     // Check for Dec Reset, 
3800     // Store dec reset AF
3801     errno = outIsReadDecStageFlag(pOutIsCtl, zMD,
3802         ASP_OUTIS_DEC_STAGE_DECODE1_IDX, &decFlag);
3803     if (errno < 0)
3804     {
3805         return errno;
3806     }
3807     
3808     if (decFlag == 0)
3809     {
3810         return ASOP_INITSYNC_NOTREADY;
3811     }
3812     else
3813     {
3814         return ASOP_SOK;
3815     }
3818 //   Purpose:   Init-Sync Re-Sync state function.
3819 //              Peformed Init-Sync using stored Dec Reset/Info1 AFs.
3820 Int PAF_ASOT_initSyncResync(
3821     const PAF_ASOT_Params *pP, 
3822     const PAF_ASOT_Patchs *pQ, 
3823     PAF_ASOT_Config *pAsotCfg,
3824     PAF_AudioFrame *pDecResetAf,
3825     PAF_AudioFrame *pDecInfo1Af    
3828     PAF_AST_Config *pAstCfg;    // ASIT/ASOT/ASDT shared configuration
3829     Int zMS;                    // Stream Master index
3830     PAF_AudioFrame *pStrAf;     // stream audio frame
3831     Int errno;                  // error number
3833     
3834     pAstCfg = pAsotCfg->pAstCfg;
3835     zMS = pAstCfg->masterStr;
3836     pStrAf = pAstCfg->xStr[zMS].pAudioFrame;
3837         
3838     // Reset audio frames
3839     resetAfs(pP, pAstCfg->xStr);
3840     
3841     //
3842     // Dec Reset re-sync using stored Dec Reset AF
3843     //
3844     
3845     // Update Stream Audio Frame.
3846     // Copy Dec Reset AF to Stream AF.
3847     //errno = initSyncUpdateAf(pStrAf, pDecResetAf);
3848     //if (errno < 0)
3849     //{
3850     //    return errno;
3851     //}
3852     outIsCpyAf(pDecResetAf, pStrAf);
3853     
3854     // Enc activate,
3855     // Enc reset
3856     errno = PAF_ASOT_outputReset(pP, pQ, pAsotCfg);
3857     if (errno)
3858     {
3859         return errno;
3860     }
3861     
3862     //
3863     // Dec Info1 re-sync using stored Dec Info1 AF
3864     //
3865     
3866     // Update Stream Audio Frame.
3867     // Copy Dec Info1 AF to Stream AF.
3868     //errno = initSyncUpdateAf(pStrAf, pDecInfo1Af);
3869     //if (errno < 0)
3870     //{
3871     //    return errno;
3872     //}
3873     outIsCpyAf(pDecInfo1Af, pStrAf);
3874     
3875     // decodeInfo1():
3876     //      - ASP chain reset,
3877     //      - Enc Info
3878     errno = PAF_ASOT_outputInfo1(pP, pQ, pAsotCfg);
3879     if (errno)
3880     {
3881         return errno;
3882     }
3883     
3884     return ASOP_SOK;    
3887 //   Purpose:   Decoding Function for determining whether processing of the
3888 //              current stream is complete.
3889 Int
3890 PAF_ASOT_decodeFinalTest(
3891     const struct PAF_ASOT_Params *pP, 
3892     const struct PAF_ASOT_Patchs *pQ, 
3893     struct PAF_ASOT_Config *pAsotCfg, 
3894     Int frame, 
3895     Int block
3898     PAF_AST_DecOpCircBufCtl *pCbCtl;    // decoder output circular buffer control
3899     Int8 drainedFlag;                   // CB drained indicator flag
3900     Int zMD;                            // master Dec index
3901     Int errno;                          // error number
3903     
3904     pCbCtl = &pAsotCfg->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
3905     zMD = pAsotCfg->pAstCfg->masterDec; // get master Dec index
3907     // Check circular buffer drain state
3908     //GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_107);     // debug
3909     errno = cbCheckDrainState(pCbCtl, zMD, &drainedFlag);
3910     //GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_107);   // debug
3911     if (errno < 0)
3912     {
3913         return errno;
3914     }
3915     
3916     if (drainedFlag == 1)
3917     {
3918         return ASOP_DP_CB_DRAINED;
3919     }
3920     
3921     return ASOP_DP_SOK;
3922 }