]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - pasdk/test_dsp/framework/audioStreamOutProc.c
PASDK-87:Merge remote-tracking branch 'origin/PASDK-367-update-customized-pdk-1.0...
[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"
73 // -----------------------------------------------------------------------------
74 // Debugging Trace Control, local to this file.
75 // 
76 #include "logp.h"
78 #define TRACE_TIME(a)
80 // Allow a developer to selectively enable tracing.
81 #define CURRENT_TRACE_MASK      0x07
83 #define TRACE_MASK_TERSE        0x01   // only flag errors and show init
84 #define TRACE_MASK_GENERAL      0x02   // half dozen lines per frame
85 #define TRACE_MASK_VERBOSE      0x04   // trace full operation
87 #if !(CURRENT_TRACE_MASK & TRACE_MASK_TERSE)
88     #undef  TRACE_TERSE0
89     #undef  TRACE_TERSE1
90     #undef  TRACE_TERSE2
91     #undef  TRACE_TERSE3
92     #undef  TRACE_TERSE4
93     #define TRACE_TERSE0(a)
94     #define TRACE_TERSE1(a,b)
95     #define TRACE_TERSE2(a,b,c)
96     #define TRACE_TERSE3(a,b,c,d)
97     #define TRACE_TERSE4(a,b,c,d,e)
98 #endif
99     
100 #if !(CURRENT_TRACE_MASK & TRACE_MASK_GENERAL)
101     #undef  TRACE_GEN0
102     #undef  TRACE_GEN1
103     #undef  TRACE_GEN2
104     #undef  TRACE_GEN3
105     #undef  TRACE_GEN4
106     #define TRACE_GEN0(a)
107     #define TRACE_GEN1(a,b)
108     #define TRACE_GEN2(a,b,c)
109     #define TRACE_GEN3(a,b,c,d)
110     #define TRACE_GEN4(a,b,c,d,e)
111 #endif
113 #if !(CURRENT_TRACE_MASK & TRACE_MASK_VERBOSE)
114     #undef  TRACE_VERBOSE0
115     #undef  TRACE_VERBOSE1
116     #undef  TRACE_VERBOSE2
117     #undef  TRACE_VERBOSE3
118     #undef  TRACE_VERBOSE4
119     #define TRACE_VERBOSE0(a)
120     #define TRACE_VERBOSE1(a,b)
121     #define TRACE_VERBOSE2(a,b,c)
122     #define TRACE_VERBOSE3(a,b,c,d)
123     #define TRACE_VERBOSE4(a,b,c,d,e)
124 #endif
126 // .............................................................................
128 //
129 // Audio Stream Definitions
130 //
132 //
133 // Audio Stream Processing Definitions
134 //
135 #define aspLinkInit pQ->i_aspLinkInit
137 //
138 // Encoder Definitions
139 //
140 #define encLinkInit pQ->i_encLinkInit
142 //
143 // Output Definitions
144 //
145 #define outLinkInit pP->i_outLinkInit
147 /* ---------------------------------------------------------------- */
148 /*              Parameter macro definitions end here.               */
149 /* ---------------------------------------------------------------- */
151 //
152 // Standardized Definitions
153 //
155 #define ENC_Handle PCE_Handle /* works for all: PCE */
157 #define __TASK_NAME__  "TaskAsop"
160 //
161 // Audio Stream Output Task definitions
162 //
164 // status codes
165 // Output FSM
166 #define ASOP_INITSYNC_NOTREADY      (  1 )  // ok, init-sync not ready
167 #define ASOP_SOK                    (  0 )  // ok
168 #define ASOP_FORWARD_ERR            ( -1 )  // forward (ASIT) error
169 #define ASOP_ENCRESET_ERR           ( -2 )  // enc reset error
170 #define ASOP_DECINFO1_ERR           ( -3 )  // dec info1 error
171 // Decode Processing
172 #define ASOP_DP_OUT_SIO_UPDATE      (  3 )  // new output selected
173 #define ASOP_DP_CB_DRAINED          (  2 )  // circular buffer drained
174 #define ASOP_DP_SOK                 (  0 )  // ok
175 #define ASOP_DP_FORWARD_ERR         ( -1 )  // forward (ASIT) error
176 #define ASOP_DP_DECINIT_ERR         ( -2 )  // decode init error
177 #define ASOP_DP_DECSTREAM_ERR       ( -3 )  // decode stream error
178 #define ASOP_DP_DECENC_ERR          ( -4 )  // decode encode error
179 #define ASOP_DP_DECINFO2_ERR        ( -5 )  // decode encode error
180 #define ASOP_DP_DECFINALTEST_ERR    ( -6 )  // decode final error
184 /* Check if at least one output selected */
185 static Int checkOutSel(
186     const PAF_ASOT_Params *pP, 
187     PAF_ASOT_Config *pAsotCfg,
188     Int *pOutSel
189 );
191 /* Check if at least one output sio changed */
192 static Int checkOutSio(
193     const PAF_ASOT_Params *pP, 
194     PAF_ASOT_Config *pAsotCfg,
195     Int *pOutSioUpdate
196 );
198 // Initialize Output Processing state function
199 static Int PAF_ASOT_initOutProc(
200     const PAF_ASOT_Params *pP, 
201     PAF_AST_Stream *xStr
202 );
204 // Initialize Output Processing state function
205 static Int PAF_ASOT_initSyncDecReset(
206     const PAF_ASOT_Params *pP, 
207     const PAF_ASOT_Patchs *pQ, 
208     PAF_ASOT_Config *pAsotCfg,
209     PAF_AudioFrame *pDecResetAf
210 );
212 //   Purpose:   ASOT Function for Output reset
213 static Int PAF_ASOT_outputReset(
214     const PAF_ASOT_Params *pP, 
215     const PAF_ASOT_Patchs *pQ, 
216     PAF_ASOT_Config *pAsotCfg
217 );
219 //   Purpose:   Init-Sync Dec Info1 state function.
220 //              Performes Dec Info1 Init-Sync.
221 static Int PAF_ASOT_initSyncDecInfo1(
222     const PAF_ASOT_Params *pP, 
223     const PAF_ASOT_Patchs *pQ, 
224     PAF_ASOT_Config *pAsotCfg,
225     PAF_AudioFrame *pDecInfo1Af
226 );
228 //   Purpose:   ASOT function for ASP chain reset and ENC info
229 static Int PAF_ASOT_outputInfo1(
230     const PAF_ASOT_Params *pP, 
231     const PAF_ASOT_Patchs *pQ, 
232     PAF_ASOT_Config *pAsotCfg
233 );
235 //   Purpose:   Init-Sync Dec Decode1 state function.
236 //              Performes Dec Decode1 Init-Sync.
237 static Int PAF_ASOT_initSyncDecDecode1(
238     const PAF_ASOT_Params *pP, 
239     const PAF_ASOT_Patchs *pQ, 
240     PAF_ASOT_Config *pAsotCfg
241 );
243 //   Purpose:   Init-Sync Re-Sync state function.
244 //              Peformed Init-Sync using stored Dec Reset/Info1 AFs.
245 static Int PAF_ASOT_initSyncResync(
246     const PAF_ASOT_Params *pP, 
247     const PAF_ASOT_Patchs *pQ, 
248     PAF_ASOT_Config *pAsotCfg,
249     PAF_AudioFrame *pDecResetAf,
250     PAF_AudioFrame *pDecInfo1Af  
251 );
253 #if 0
254 // Init-Sync update audio frame
255 static Int initSyncUpdateAf(
256     PAF_AudioFrame *dstAf, 
257     PAF_AudioFrame *srcAf
258 );
259 #endif
261 // Reset audio frames
262 static Void resetAfs(
263     const PAF_ASOT_Params *pP, 
264     PAF_AST_Stream *xStr
265 );
267 // Reset audio frame pointers to original values
268 static Void resetAfPointers(
269     const PAF_ASOT_Params *pP, 
270     PAF_AST_Stream *xStr
271 );
273 // Reset audio frame meta data elements
274 static Void resetAfMetadata(
275     const PAF_ASOT_Params *pP, 
276     PAF_AST_Stream *xStr
277 );
280 LINNO_DEFN(TaskAsop); /* Line number macros */
281 ERRNO_DEFN(TaskAsop); /* Error number macros */
283 // ASOT configuration
284 #pragma DATA_SECTION(gPAF_ASOT_config, ".globalSectionPafAsotConfig")
285 PAF_ASOT_Config gPAF_ASOT_config = {
286     NULL,               // taskHandle
287     NULL,               // acp
288     0,0,0,              // CB drained flags (size DECODE_MAXN)
289     &gPAF_ASPM_config,  // pAspmCfg
290     &gPAF_AST_config    // pAstCfg
291 };
293 // Underflow threshold before circular buffer reset and return error to Top-Level FSM
294 #define DEC_OP_CB_RDAF_UND_THR  ( 80 ) // FL: arbitrary setting
295 UInt32 gCbReadAfErr         =0; // read circular buffer error count, not including underflows
296 UInt32 gDecOpCbRdAfUnd      =0; // decoder output circular buffer underflow count
297 UInt32 gMaxDecOpCbRdAfUnd   =0; // max (consecutive) decoder output circular buffer underflow count
298 UInt32 gMasterCbResetCnt    =0; // master circular buffer reset count
300 // Global debug counters */
301 UInt32 gAsopInitOutProcCnt          =0;
302 UInt32 gAsopInitSyncDecResetCnt     =0;
303 UInt32 gAsopInitSyncDecInfo1Cnt     =0;
304 UInt32 gAsopInitSyncDecDecode1Cnt   =0;
305 UInt32 gAsopInitSyncResyncCnt       =0;
306 UInt32 gAsopOutProcCnt              =0;
307 UInt32 gAsopInitCnt                 =0;
308 UInt32 gAsopStreamCnt               =0;
309 UInt32 gAsopEncodeCnt               =0;
310 UInt32 gAsopFinalCnt                =0;
311 UInt32 gAsopOutSioUpdateCnt         =0;
312 UInt32 gAsopQuitCnt                 =0;
313 UInt32 gAsopTxSioReclaimCnt         =0;
315 /*
316  *  ======== taskAsopFxn ========
317  *  Audio Stream Output Processing task function
318  */
319 Void taskAsopFxn(
320 //    Int betaPrimeValue, // FL: revisit
321     const PAF_ASOT_Params *pP,
322     const PAF_ASOT_Patchs *pQ
325     PAF_ASOT_Config *pAsotCfg;      /* ASOT configuration pointer */
326     PAF_AST_Config *pAstCfg;        /* Common (shared) configuration pointer */
327     Int as;                         /* Audio Stream Number (1, 2, etc.) */
328     Int z;                          /* input/encode/stream/decode/output counter */
329     Int i;                          /* phase */
330     Int zMS;
331     Int errno;                      // error number
332     Int8 procSleep;                 // whether to sleep: 0: No, 1: Yes
333     Int8 procOutDevSel;             // whether to perform output device selection: 0: No, 1:Yes
334     Int outSel;                     // whether output device selected
335     enum { INIT_OUT_PROC_STATE, 
336            INITSYNC_DEC_RESET_STATE, INITSYNC_DEC_INFO1_STATE, INITSYNC_DEC_DECODE1_STATE, 
337            INITSYNC_RESYNC_STATE, 
338            OUT_PROC_STATE } state;
339     PAF_AudioFrame decResetAf;
340     PAF_AudioFrame decInfo1Af;
341     Int loopCount = 0;              // used to stop trace to see startup behavior.
342     
344     Log_info0("Enter taskAsopFxn()");    
346     //
347     // Audio Stream Output Task Parameters & Patch (*pP, *pQ)
348     //
349     if (!pP) 
350     {
351         TRACE_TERSE0("TaskAsop: No Parameters defined. Exiting.");
352         LINNO_RPRT(TaskAsop, -1);
353         return;
354     }
356     if (!pQ)
357     {
358         TRACE_TERSE0("TaskAsop: No Patchs defined. Exiting.");
359         LINNO_RPRT(TaskAsop, -1);
360         return;
361     }    
363     //
364     // Audio Stream Output Task Configuration (*pAsotCfg):
365     //
366     pAsotCfg = &gPAF_ASOT_config;       // initialize pointer to task configuration
367     pAsotCfg->taskHandle = Task_self(); // set task handle
368     pAstCfg = pAsotCfg->pAstCfg;        // get pointer to AST common (shared) configuration
370     /* Obtain Audio Stream Number (1, 2, etc.) */    
371     as = pAstCfg->as;
372     TRACE_TERSE1("TaskAsop: Started with AS%d.", as);
374     //
375     // Initialize message log trace and line number reporting
376     //
377     for (z=STREAM1; z < STREAMN; z++)
378     {
379         TRACE_TERSE1("TaskAsop: AS%d: initiated", as+z);
380     }
381     LINNO_RPRT(TaskAsop, -1);
382     
383     //
384     // Determine stream index
385     //
386     zMS = pAstCfg->masterStr;
388     // Initialize as per parametrized phases:
389     //
390     //   In standard form these are:
391     //      - Malloc: Memory Allocation
392     //      - Config: Configuration Initialization
393     //      - AcpAlg: ACP Algorithm Initialization and Local Attachment
394     //      - Common: Common Memory Initialization
395     //      - AlgKey: Dec/Enc chain to Array Initialization
396     //      - Device: I/O Device Initialization
397     //      - Unused: (available)
398     //      - Unused: (available)
399     //
400     LINNO_RPRT(TaskAsop, -2);
401     for (i=0; i < lengthof(pP->fxns->initPhase); i++)
402     {
403         Int linno;
404         if (pP->fxns->initPhase[i])
405         {
406             linno = pP->fxns->initPhase[i](pP, pQ, pAsotCfg);
407             if (linno) 
408             {
409                 LINNO_RPRT(TaskAsop, linno);
410                 return;
411             }
412         }
413         else 
414         {
415             TRACE_TERSE1("TaskAsop: AS%d: initialization phase - null", as+zMS);
416         }
417         TRACE_TERSE2("TaskAsop: AS%d: initialization phase - %d completed", as+zMS, i);
418         LINNO_RPRT(TaskAsop, -i-3);
419     }
420   
421     //
422     // End of Initialization -- display memory usage report.
423     //
424     if (pP->fxns->memStatusPrint)
425     {
426         pP->fxns->memStatusPrint("ASOT MEMSTAT REPORT",
427             HEAP_INTERNAL, HEAP_INTERNAL1, HEAP_EXTERNAL, 
428             HEAP_INTERNAL1_SHM, HEAP_EXTERNAL_SHM, HEAP_EXTERNAL_NONCACHED_SHM);
429     }
431     //
432     // Main processing loop
433     //   
434     for (z=STREAM1; z < STREAMN; z++)
435     {
436         TRACE_VERBOSE1("TaskAsop: AS%d: running", as+z);
437     }
438     
439     errno = 0;                      // init error indicator -- no error
440     procSleep = 1;                  // init sleep flag -- sleep
441     procOutDevSel = 1;              // init device output selection flag -- perform output device selection
442     state = INIT_OUT_PROC_STATE;    // init state
443     for (;;)
444     {
445         loopCount++;
446         TRACE_GEN2("TaskAsop (begin Main loop %d) (errno 0x%x)", loopCount, errno);
448         //
449         // Check forward (ASIT) error here, TBD
450         //
451         
452         // any error forces idling of output
453         if (errno) 
454         {
455             for (z=OUTPUT1; z < OUTPUTN; z++)
456             {
457                 if (pAstCfg->xOut[z].hTxSio)
458                 {
459                     SIO_idle(pAstCfg->xOut[z].hTxSio);
460                 }
461             }
462         
463             TRACE_TERSE1("TaskAsop: Trace stopped at loop %d.", loopCount);
464             ERRNO_RPRT(TaskAsop, errno);
465         }        
466     
467         if (procSleep == 1)
468         {
469             TRACE_VERBOSE1("TaskAsop: AS%d: ... sleeping ...", as+zMS);
470             Task_sleep(1);
471         }
473         if (procOutDevSel == 1)
474         {
475             // select output devices
476             TRACE_GEN1("TaskAsop: AS%d: Output device selection ...", as+zMS);
477             errno = pP->fxns->selectDevices(pP, pQ, pAsotCfg);
478             if (errno)
479             {
480                 TRACE_TERSE2("TaskAsop: AS%d: selectDevices returned errno = 0x%04x", as+zMS, errno);
482                 procSleep = 1;
483                 procOutDevSel = 1;
485                 continue;
486             }
488             // if no output selected skip remaining processing
489             errno = checkOutSel(pP, pAsotCfg, &outSel);
490             if (errno < 0)
491             {
492                 TRACE_TERSE2("TaskAsop: AS%d: checkOutSel returned errno = 0x%04x", as+zMS, errno);
494                 procSleep = 1;
495                 procOutDevSel = 1;
497                 continue;
498             }
499             else if (!outSel)
500             {
501                 TRACE_VERBOSE1("TaskAsop: AS%d: No output selected...", as+zMS);
503                 procSleep = 1;
504                 procOutDevSel = 1;
505                 
506                 continue;
507             }
508         }
509         
510         switch (state)
511         {
512             case INIT_OUT_PROC_STATE:
513                 gAsopInitOutProcCnt++;
514                 Log_info0("TaskAsop: state=INIT_OUT_PROC_STATE");
515                 
516                 //
517                 // Output Processing initialization.
518                 //
519                 errno = PAF_ASOT_initOutProc(pP, pAstCfg->xStr);
520                 if (errno < 0)
521                 {
522                     state = INIT_OUT_PROC_STATE;
523                     procSleep = 1;
524                     procOutDevSel = 1;
525                 }
526                 else
527                 {
528                     state = INITSYNC_DEC_RESET_STATE;
529                     procSleep = 0;
530                     procOutDevSel = 0;
531                 }
532             
533                 break;
534                 
535             case INITSYNC_DEC_RESET_STATE:
536                 gAsopInitSyncDecResetCnt++;
537                 Log_info0("TaskAsop: state=INITSYNC_DEC_RESET_STATE");
539                 //
540                 // Dec Reset Init-Sync.
541                 //  
542                 
543                 // Perform Dec Reset init-sync.
544                 // Latch Dec Reset AF.
545                 errno = PAF_ASOT_initSyncDecReset(pP, pQ, pAsotCfg, &decResetAf);
546                 if (errno < 0)
547                 {
548                     Log_info1("TaskAsop: state=INITSYNC_DEC_RESET_STATE errno=%d", errno);
550                     // sync error -- start over
551                     state = INIT_OUT_PROC_STATE;
552                     procSleep = 1;
553                     procOutDevSel = 1;
554                 }
555                 else if (errno == ASOP_INITSYNC_NOTREADY)
556                 {
557                     Log_info1("TaskAsop: state=INITSYNC_DEC_RESET_STATE not sync'd errno=%d", errno);
559                     // sync not ready -- try again
560                     state = INITSYNC_DEC_RESET_STATE;
561                     errno=0; // FL: temp hack
562                     procSleep = 1;
563                     procOutDevSel = 1;
564                 }
565                 else // errno==0
566                 {
567                     Log_info1("TaskAsop: state=INITSYNC_DEC_RESET_STATE sync'd, errno=%d", errno);
569                     // sync'd -- move on
570                     state = INITSYNC_DEC_INFO1_STATE;
571                     procSleep = 0;
572                     procOutDevSel = 0;
573                 }
574                 
575                 break;
576             
577             case INITSYNC_DEC_INFO1_STATE:
578                 gAsopInitSyncDecInfo1Cnt++;
579                 Log_info0("TaskAsop: state=INITSYNC_DEC_INFO1_STATE");
580                 
581                 //
582                 // Dec Info1 Init-Sync.
583                 //
585                 // Perform Dec Info1 init-sync.
586                 // Latch Dec Info1 AF.
587                 errno = PAF_ASOT_initSyncDecInfo1(pP, pQ, pAsotCfg, &decInfo1Af);
588                 if (errno < 0)
589                 {
590                     Log_info1("TaskAsop: state=INITSYNC_DEC_INFO1_STATE errno=%d", errno);
591                     
592                     // sync error -- start over
593                     state = INIT_OUT_PROC_STATE;
594                     procSleep = 1;
595                     procOutDevSel = 1;
596                 }
597                 else if (errno == ASOP_INITSYNC_NOTREADY)
598                 {
599                     Log_info1("TaskAsop: state=INITSYNC_DEC_INFO1_STATE not sync'd errno=%d", errno);
601                     // sync not ready -- try again
602                     state = INITSYNC_DEC_INFO1_STATE;
603                     errno=0; // FL: temp hack
604                     procSleep = 1;
605                     procOutDevSel = 0;
606                 }
607                 else // errno = 0
608                 {
609                     Log_info1("TaskAsop: state=INITSYNC_DEC_INFO1_STATE sync'd errno=%d", errno);
611                     // sync'd -- move on
612                     state = INITSYNC_DEC_DECODE1_STATE;
613                     procSleep = 0;
614                     procOutDevSel = 0;
615                 }
616                 
617                 break;
618             
619             case INITSYNC_DEC_DECODE1_STATE:
620                 gAsopInitSyncDecDecode1Cnt++;
621                 Log_info0("TaskAsop: state=INITSYNC_DEC_DECODE1_STATE");
622                 
623                 //
624                 // Dec Info1 Init-Sync.
625                 //
627                 // Perform Dec Info1 init-sync.
628                 // Latch Dec Info1 AF.
629                 errno = PAF_ASOT_initSyncDecDecode1(pP, pQ, pAsotCfg);
630                 if (errno < 0)
631                 {
632                     Log_info1("TaskAsop: state=INITSYNC_DEC_DECODE1_STATE errno=%d", errno);
634                     // sync error -- start over
635                     state = INIT_OUT_PROC_STATE;
636                     procSleep = 1;
637                     procOutDevSel = 1;
638                 }
639                 else if (errno == ASOP_INITSYNC_NOTREADY)
640                 {
641                     Log_info1("TaskAsop: state=INITSYNC_DEC_DECODE1_STATE not sync'd errno=%d", errno);
643                     // sync not ready -- try again
644                     state = INITSYNC_DEC_DECODE1_STATE;
645                     errno=0; // FL: temp hack
646                     procSleep = 1;
647                     procOutDevSel = 0;
648                 }
649                 else // errno = 0
650                 {
651                     Log_info1("TaskAsop: state=INITSYNC_DEC_DECODE1_STATE sync'd errno=%d", errno);
653                     // sync'd -- move on
654                     state = OUT_PROC_STATE;
655                     procSleep = 0;
656                     procOutDevSel = 0;
657                 }
658                 
659                 break;
660             
661             case INITSYNC_RESYNC_STATE:
662                 gAsopInitSyncResyncCnt++;
663                 Log_info0("TaskAsop: state=INITSYNC_RESYNC_STATE");
665                 //
666                 // Re-Sync.
667                 // Use stored AF info from init-sync.
668                 // This is done in case of local error.
669                 //
670                 
671                 // Perform Dec Info1 init-sync.
672                 errno = PAF_ASOT_initSyncResync(pP, pQ, pAsotCfg, &decResetAf, 
673                     &decInfo1Af);
674                 if (errno < 0)
675                 {
676                     Log_info1("TaskAsop: state=INITSYNC_RESYNC_STATE errno=%d", errno);
678                     // sync error -- start over
679                     state = INIT_OUT_PROC_STATE;
680                     procSleep = 1;
681                     procOutDevSel = 1;
682                 }
683                 else
684                 {
685                     Log_info1("TaskAsop: state=INITSYNC_RESYNC_STATE sync'd errno=%d", errno);
687                     // re-sync'd -- move on
688                     state = OUT_PROC_STATE;
689                     procSleep = 0;
690                     procOutDevSel = 0;
691                 }
692                     
693                 break;
694             
695             case OUT_PROC_STATE:        
696                 gAsopOutProcCnt++;
697                 Log_info0("TaskAsop: state=OUT_PROC_STATE");
698                 
699                 //
700                 // Output Processing.
701                 //
703                 TRACE_VERBOSE0("TaskAsop: calling decodeProcessing.");
704                 errno = pP->fxns->decodeProcessing(pP, pQ, pAsotCfg);
705                 if (errno < 0)
706                 {
707                     Log_info1("TaskAsop: state=OUT_PROC_STATE errno=%d", errno);
708                     
709                     //
710                     // Output Processing exit, due to error
711                     //
712                     
713                     TRACE_TERSE1("TaskAsop: decodeProcessing returns 0x%x, continue", errno);
714                     if (errno == ASOP_DP_FORWARD_ERR)
715                     {
716                         // forward (ASIT) error -- start over
717                         state = INIT_OUT_PROC_STATE;
718                         procSleep = 1;
719                         procOutDevSel = 1;
720                     }
721                     else
722                     {
723                         // local (ASOT) error
724                         state = INITSYNC_RESYNC_STATE;    
725                         procSleep = 1;
726                         procOutDevSel = 0; // disallow device re-select if local error during output processing
727                     }        
728                 }
729                 else if (errno > 0)
730                 {
731                     Log_info1("TaskAsop: state=OUT_PROC_STATE errno=%d", errno);
733                     //
734                     // Output Processing exit, not due to error
735                     //
737                     TRACE_TERSE1("TaskAsop: decodeProcessing returns 0x%x, continue", errno);                    
738                     if (errno == ASOP_DP_OUT_SIO_UPDATE)
739                     {
740                         // skip re-sync
741                         // resume output processing after new output selected
742                         state = OUT_PROC_STATE;
743                         procSleep = 1;
744                         procOutDevSel = 1;
745                     }
746                 }
747                 else
748                 {
749                     Log_info1("TaskAsop: state=OUT_PROC_STATE errno=%d", errno);
751                     //
752                     // Output Processing exit, normal
753                     //
754                     
755                     TRACE_VERBOSE0("TaskAsop: outputProcessing complete with no error.");
756                     
757                     // no error returned if CB drained 
758                     // (i.e. CB drained is normal behavior)
759                     state = INIT_OUT_PROC_STATE;
760                     procSleep = 1;
761                     procOutDevSel = 1;
762                 }
763                 
764                 break;
765             
766             default: // unknown state
767                 TRACE_TERSE2("TaskAsop: AS%d: state: unknown, 0x%x", as+zMS, state);
768                 break;
769         }
770     } // End of main processing loop for (;;)
771     
772     //Log_info0("Exit taskAsopFxn()");
775 // -----------------------------------------------------------------------------
776 // AST Initialization Function - Memory Allocation
777 //
778 //   Name:      PAF_ASOT_initPhaseMalloc
779 //   Purpose:   Audio Stream Output Task Function for initialization of data pointers
780 //              by allocation of memory.
781 //   From:      audioStream1Task or equivalent
782 //   Uses:      See code.
783 //   States:    x
784 //   Return:    0 on success.
785 //              Source code line number on MEM_calloc failure.
786 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
787 //              * State information as per parent.
788 //              * Memory allocation errors.
789 //
791 Int
792 PAF_ASOT_initPhaseMalloc (
793     const PAF_ASOT_Params *pP, 
794     const PAF_ASOT_Patchs *pQ, 
795     PAF_ASOT_Config *pAsotCfg
798     PAF_AST_Config *pAstCfg;
799     Int as;                     /* Audio Stream Number (1, 2, etc.) */
800     Int zMS;
801     Error_Block    eb;
802     //Int i;
804     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
805     as = pAstCfg->as;
806     zMS = pAstCfg->masterStr;
808     TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: initialization phase - memory allocation", as+zMS);
810     // Initialize error block
811     Error_init(&eb); 
813     /* Stream memory */
814     if (!(pAstCfg->xStr = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, STREAMN * sizeof (*pAstCfg->xStr), 4, &eb)))
815     {
816         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
817         SW_BREAKPOINT;
818         return __LINE__;
819     }
820     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAstCfg->xStr) %d bytes from space %d at 0x%x.",
821             STREAMN * sizeof (*pAstCfg->xStr),
822             HEAP_ID_INTERNAL, (IArg)pAstCfg->xStr);
824     {
825         Int z;                          /* stream counter */
827         PAF_AudioFrame *fBuf;
829         if (!(fBuf = (PAF_AudioFrame *)Memory_calloc((IHeap_Handle)HEAP_INTERNAL, STREAMS * sizeof (*fBuf), 4, &eb)))
830         {
831             TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
832             SW_BREAKPOINT;
833             return __LINE__;
834         }
835         TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (fBuf) %d bytes from space %d at 0x%x.",
836                 STREAMS * sizeof (*fBuf),
837                 HEAP_ID_INTERNAL, (IArg)fBuf);
839         for (z=STREAM1; z < STREAMN; z++)
840         {
841             pAstCfg->xStr[z].pAudioFrame = &fBuf[z-STREAM1];
842             TRACE_TERSE2("pAstCfg->xStr[%d].pAudioFrame = 0x%x", z, (IArg)pAstCfg->xStr[z].pAudioFrame);
843         }
844     }
846     /* Encode memory */
847     if (!(pAstCfg->xEnc = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, ENCODEN * sizeof (*pAstCfg->xEnc), 4, &eb)))
848     {
849         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
850         SW_BREAKPOINT;
851         return __LINE__;
852     }
853     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAstCfg->xEnc) %d bytes from space %d at 0x%x.",
854             ENCODEN * sizeof (*pAstCfg->xEnc),
855             HEAP_ID_INTERNAL, (IArg)pAstCfg->xEnc);
857     /* Output memory */
858     if (!(pAstCfg->xOut = Memory_calloc((IHeap_Handle)HEAP_INTERNAL, OUTPUTN * sizeof (*pAstCfg->xOut), 4, &eb)))
859     {
860         TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
861         SW_BREAKPOINT;
862         return __LINE__;
863     }
864     TRACE_TERSE3("PAF_ASOT_initPhaseMalloc. (pAstCfg->xOut) %d bytes from space %d at 0x%x.",
865             OUTPUTN * sizeof (*pAstCfg->xOut),
866             HEAP_ID_INTERNAL, (IArg)pAstCfg->xOut);
868     TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: initialization phase - memory allocation complete.", as+zMS);
869     return 0;
870 } //PAF_ASOT_initPhaseMalloc
872 // -----------------------------------------------------------------------------
873 // ASOT Initialization Function - Memory Initialization from Configuration
874 //
875 //   Name:      PAF_ASOT_initPhaseConfig
876 //   Purpose:   Audio Stream Output Task Function for initialization of data values
877 //              from parameters.
878 //   From:      audioStream1Task or equivalent
879 //   Uses:      See code.
880 //   States:    x
881 //   Return:    0 on success.
882 //              Other as per initFrame0 and initFrame1.
883 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
884 //              * State information as per parent.
885 //
886 Int
887 PAF_ASOT_initPhaseConfig(
888     const PAF_ASOT_Params *pP, 
889     const PAF_ASOT_Patchs *pQ, 
890     PAF_ASOT_Config *pAsotCfg
893     PAF_AST_Config *pAstCfg;
894     Int as;                     /* Audio Stream Number (1, 2, etc.) */
895     Int z;                      /* input/encode/stream/decode/output counter */
896     Int zMS;
898     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
899     as = pAstCfg->as;
900     zMS = pAstCfg->masterStr;
902     TRACE_TERSE1("PAF_ASOT_initPhaseConfig: AS%d: initialization phase - configuration", as+zMS);
904     //
905     // Unspecified elements have been initialized to zero during alloc
906     //
908     for (z=STREAM1; z < STREAMN; z++) 
909     {
910         Int linno;
911         if (linno = pP->fxns->initFrame0(pP, pQ, pAsotCfg, z))
912         {
913             return linno;           
914         }
915         if (linno = pP->fxns->initFrame1(pP, pQ, pAsotCfg, z, -1))
916         {
917             return linno;
918         }
919     }
921     for (z=ENCODE1; z < ENCODEN; z++) 
922     {
923         Int zO = pP->outputsFromEncodes[z];
924         Int zS = pP->streamsFromEncodes[z];
925         pAstCfg->xEnc[z].encodeControl.size = sizeof(pAstCfg->xEnc[z].encodeControl);
926         pAstCfg->xEnc[z].encodeControl.pAudioFrame = pAstCfg->xStr[zS].pAudioFrame;
927         pAstCfg->xEnc[z].encodeControl.pVolumeStatus = &pAstCfg->xEnc[z].volumeStatus;
928         pAstCfg->xEnc[z].encodeControl.pOutBufConfig = &pAstCfg->xOut[zO].outBufConfig;
929         pAstCfg->xEnc[z].encodeStatus = *pP->z_pEncodeStatus[z];
930         pAstCfg->xEnc[z].encodeControl.encActive = pAstCfg->xEnc[z].encodeStatus.select;
931         pAstCfg->xEnc[z].volumeStatus = *pP->pVolumeStatus;
932         pAstCfg->xEnc[z].encodeInStruct.pAudioFrame = pAstCfg->xStr[zS].pAudioFrame;
933     }
935     for (z=OUTPUT1; z < OUTPUTN; z++)
936     {
937         pAstCfg->xOut[z].outBufStatus = *pP->pOutBufStatus;
938     }
940     TRACE_TERSE1("PAF_ASOT_initPhaseConfig: AS%d: initialization phase - configuration complete.", as+zMS);
941     return 0;
942 } //PAF_ASOT_initPhaseConfig
944 // -----------------------------------------------------------------------------
945 // ASOT Initialization Function - ACP Algorithm Instantiation
946 //
947 //   Name:      PAF_ASOT_initPhaseAcpAlg
948 //   Purpose:   Audio Stream Input Task Function for initialization of ACP by
949 //              instantiation of the algorithm.
950 //   From:      audioStream1Task or equivalent
951 //   Uses:      See code.
952 //   States:    x
953 //   Return:    0 on success.
954 //              Source code line number on ACP Algorithm creation failure.
955 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
956 //              * State information as per parent.
957 //              * Memory allocation errors.
958 //
959 Int
960 PAF_ASOT_initPhaseAcpAlg(
961     const PAF_ASOT_Params *pP, 
962     const PAF_ASOT_Patchs *pQ, 
963     PAF_ASOT_Config *pAsotCfg
966     PAF_AST_Config *pAstCfg;
967     Int as;                     /* Audio Stream Number (1, 2, etc.) */
968     Int z;                      /* input/encode/stream/decode/output counter */
969     Int betaPrimeOffset;
970     ACP_Handle acp;
971     Int zMS;
972     Int zS, zX;
974     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
975     as = pAstCfg->as;
976     zMS = pAstCfg->masterStr;
978     TRACE_TERSE1("PAF_ASOT_initPhaseAcpAlg: AS%d: initialization phase - ACP Algorithm", as+zMS);
980     ACP_MDS_init();
982     if (!(acp = (ACP_Handle )ACP_MDS_create(NULL))) 
983     {
984         TRACE_TERSE1("PAF_ASOT_initPhaseAcpAlg: AS%d: ACP algorithm instance creation  failed", as+zMS);
985         return __LINE__;
986     }
987     pAsotCfg->acp = acp;
989     ((ALG_Handle)acp)->fxns->algControl((ALG_Handle) acp,
990         ACP_GETBETAPRIMEOFFSET, (IALG_Status *)&betaPrimeOffset);
992     for (z=ENCODE1; z < ENCODEN; z++) 
993     {
994         zS = pP->streamsFromEncodes[z];
995         acp->fxns->attach(acp, ACP_SERIES_STD,
996             STD_BETA_ENCODE + betaPrimeOffset * (as-1+zS),
997             (IALG_Status *)&pAstCfg->xEnc[z].encodeStatus);
998         acp->fxns->attach(acp, ACP_SERIES_STD,
999             STD_BETA_VOLUME + betaPrimeOffset * (as-1+zS),
1000             (IALG_Status *)&pAstCfg->xEnc[z].volumeStatus);
1001         /* Ignore errors, not reported. */
1002     }
1004     for (z=OUTPUT1; z < OUTPUTN; z++) 
1005     {
1006         zS = z;
1007         for (zX = ENCODE1; zX < ENCODEN; zX++) 
1008         {
1009             if (pP->outputsFromEncodes[zX] == z) 
1010             {
1011                 zS = pP->streamsFromEncodes[zX];
1012                 break;
1013             }
1014         }
1015         acp->fxns->attach(acp, ACP_SERIES_STD,
1016             STD_BETA_OB + betaPrimeOffset * (as-1+zS),
1017             (IALG_Status *)&pAstCfg->xOut[z].outBufStatus);
1018         /* Ignore errors, not reported. */
1019     }
1021     TRACE_TERSE1("PAF_ASOT_initPhaseAcpAlg: AS%d: initialization phase - ACP Algorithm complete.", as+zMS);
1023     return 0;
1024 } //PAF_ASOT_initPhaseAcpAlg
1026 // -----------------------------------------------------------------------------
1027 // ASOT Initialization Function - Common Memory
1028 //
1029 //   Name:      PAF_ASOT_initPhaseCommon
1030 //   Purpose:   Audio Stream Output Task Function for allocation of common memory.
1031 //   From:      audioStream1Task or equivalent
1032 //   Uses:      See code.
1033 //   States:    x
1034 //   Return:    0 on success.
1035 //              Source code line number on PAF_ALG_alloc failure.
1036 //              Source code line number on PAF_ALG_mallocMemory failure.
1037 //              Source code line number on Decode Chain initialization failure.
1038 //              Source code line number on ASP Chain initialization failure.
1039 //              Source code line number on Encode Chain initialization failure.
1040 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1041 //              * State information as per parent.
1042 //              * Memory allocation errors.
1043 //
1044 Int
1045 PAF_ASOT_initPhaseCommon(
1046     const PAF_ASOT_Params *pP, 
1047     const PAF_ASOT_Patchs *pQ, 
1048     PAF_ASOT_Config *pAsotCfg
1051     PAF_AST_Config *pAstCfg;
1052     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1053     Int z;                      /* stream counter */
1054     Int g;                      /* gear */
1055     ACP_Handle acp;
1056     PAF_IALG_Config pafAlgConfig;
1057     IALG_MemRec common[3][PAF_IALG_COMMON_MEMN+1];
1058    
1059     acp = pAsotCfg->acp;
1060     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1061     as = pAstCfg->as;
1063     TRACE_TERSE0("PAF_ASOT_initPhaseCommon: initialization phase - Common Memory");
1065     //
1066     // Determine memory needs and instantiate algorithms across audio streams
1067     //
1068     TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ALG_setup.");
1069     PAF_ALG_setup(&pafAlgConfig, 
1070         HEAP_ID_INTERNAL,                 HEAP_INTERNAL, 
1071         HEAP_ID_INTERNAL1,                HEAP_INTERNAL1, 
1072         HEAP_ID_EXTERNAL,                 HEAP_EXTERNAL, 
1073         HEAP_ID_INTERNAL1_SHM,            HEAP_INTERNAL1_SHM, 
1074         HEAP_ID_EXTERNAL_SHM,             HEAP_EXTERNAL_SHM, 
1075         HEAP_ID_EXTERNAL_NONCACHED_SHM,   HEAP_EXTERNAL_NONCACHED_SHM,
1076         HEAP_CLEAR);
1078     if (pP->fxns->headerPrint)
1079     {
1080         pP->fxns->headerPrint();        
1081     }
1083     for (z = STREAM1; z < STREAMN; z++) 
1084     {
1085         //Int zD, zE, zX;
1086         Int zE, zX;
1088         TRACE_TERSE1("PAF_ASOT_initPhaseCommon: AS%d: initialization phase - Common Memory", as+z);
1090         //
1091         // Determine common memory needs for:
1092         //  (1) ASP Algorithms
1093         //  (2) Encode Algorithms
1094         //  (3) Logical Output drivers
1095         //
1096         PAF_ALG_init(common[z], lengthof(common[z]), COMMONSPACE);
1098         zE = -1;
1099         for (zX = ENCODE1; zX < ENCODEN; zX++) 
1100         {
1101             if (pP->streamsFromEncodes[zX] == z) 
1102             {
1103                 zE = zX;
1104                 break;
1105             }
1106         }
1108         TRACE_TERSE1("Calling PAF_ALG_ALLOC for stream common[%d].", z);
1109         if (PAF_ALG_ALLOC(aspLinkInit[z-STREAM1][0], common[z])) 
1110         {
1111             TRACE_TERSE1("PAF_ASOT_initPhaseCommon: AS%d: PAF_ALG_alloc failed", as+z);
1112             TRACE_TERSE2("Failed to alloc %d bytes from space %d ", common[z]->size, common[z]->space);
1113             SW_BREAKPOINT;
1114             return __LINE__;
1115         }
1116         TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1117         if (pP->fxns->allocPrint)
1118         {
1119             pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(aspLinkInit[z-STREAM1][0]), sizeof (*(aspLinkInit[z-STREAM1][0])), &pafAlgConfig);
1120         }
1122         if (zE >= 0) 
1123         {
1124             TRACE_TERSE1("PAF_ASOT_initPhaseCommon: calling PAF_ALG_ALLOC/ for encoder common[%d].", z);
1125             if (PAF_ALG_ALLOC(encLinkInit[zE-ENCODE1], common[z])) 
1126             {
1127                 TRACE_TERSE1("PAF_ASOT_initPhaseCommon: AS%d: PAF_ALG_alloc failed", as+z);
1128                 SW_BREAKPOINT;
1129                 return __LINE__;
1130             }
1131             TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1132             if (pP->fxns->allocPrint)
1133             {
1134                 pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(encLinkInit[z-ENCODE1]), sizeof (*(encLinkInit[z-ENCODE1])), &pafAlgConfig);
1135             }
1136         }
1138         //
1139         // Determine common memory needs of Logical IO drivers
1140         //
1142         if (OUTPUT1 <= z && z < OUTPUTN)
1143         {
1144             TRACE_TERSE1("PAF_ASOT_initPhaseCommon: calling PAF_ALG_ALLOC outLinkInit common[%d].", z);
1145             if (PAF_ALG_ALLOC(outLinkInit[z-OUTPUT1], common[z]))
1146             {
1147                 TRACE_TERSE1("PAF_ASOT_initPhaseMalloc: AS%d: PAF_ALG_alloc failed", as+z);
1148                 TRACE_TERSE2("Failed to alloc %d bytes from space %d", common[z]->size, (IArg)common[z]->space);
1149                 SW_BREAKPOINT;
1150                 return __LINE__;
1151             }
1152             TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1153             if (pP->fxns->allocPrint)
1154             {
1155                 pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(outLinkInit[z-INPUT1]), sizeof (*(outLinkInit[z-INPUT1])), &pafAlgConfig);
1156             }
1157         }
1158     }
1159     {
1160         // Changes made to share scratch between zones
1161         // Assume maximum 3 zones and scratch common memory is at offset 0;
1162         int max=0;
1163         for (z=STREAM1; z<STREAMN; z++)
1164         {
1165             if (max < common[z][0].size)
1166             {
1167                 max = common[z][0].size;
1168             }
1169         }
1170         common[STREAM1][0].size=max;
1171         for (z=STREAM1+1; z<STREAMN; z++)
1172         {
1173             common[z][0].size = 0;            
1174         }
1175     }
1176         
1177     //
1178     // Allocate common memory for:
1179     //  (1) ASP Algorithms
1180     //  (2) Encode Algorithms
1181     //  (3) Logical Output drivers
1182     //
1183     for (z = STREAM1; z < STREAMN; z++) 
1184     {
1185         //Int zD, zE, zX;
1186         Int zE, zX;
1188         TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ALG_mallocMemory for common space.");
1189         if (PAF_ALG_mallocMemory(common[z], &pafAlgConfig)) 
1190         {
1191             TRACE_TERSE1("AS%d: PAF_ALG_mallocMemory failed", as+z);
1192             TRACE_TERSE3("AS%d: z: %d.  Size 0x%x", as+z, z, common[z][0].size);
1193             SW_BREAKPOINT;
1194             return __LINE__;
1195         }
1196         TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1197         // share zone0 scratch with all zones 
1198         common[z][0].base = common[0][0].base;
1199         if (pP->fxns->commonPrint)
1200         {
1201             pP->fxns->commonPrint(common[z], &pafAlgConfig);
1202         }
1204         zE = -1;
1205         for (zX = ENCODE1; zX < ENCODEN; zX++) 
1206         {
1207             if (pP->streamsFromEncodes[zX] == z) 
1208             {
1209                 zE = zX;
1210                 break;
1211             }
1212         }
1214         pAstCfg->xStr[z].aspChain[0] = NULL;
1215         for (g=0; g < GEARS; g++) 
1216         {
1217             PAF_ASP_Chain *chain;
1218             TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ASP_chainInit for ASPs.");
1219             chain = PAF_ASP_chainInit(&pAstCfg->xStr[z].aspChainData[g], pP->pChainFxns,
1220                 HEAP_INTERNAL, as+z, acp, &trace,
1221                 aspLinkInit[z-STREAM1][g], pAstCfg->xStr[z].aspChain[0], common[z], &pafAlgConfig);
1222             if (!chain) 
1223             {
1224                 TRACE_TERSE2("AS%d: ASP chain %d initialization failed", as+z, g);
1225                 return __LINE__;
1226             }
1227             else
1228             {
1229                 pAstCfg->xStr[z].aspChain[g] = chain;
1230             }
1231         }
1233         if (zE >= 0) 
1234         {
1235             PAF_ASP_Chain *chain;
1236             TRACE_TERSE0("PAF_ASOT_initPhaseCommon: calling PAF_ASP_chainInit for encode.");
1237             chain = PAF_ASP_chainInit(&pAstCfg->xEnc[zE].encChainData, pP->pChainFxns,
1238                 HEAP_INTERNAL, as+z, acp, &trace,
1239                 encLinkInit[zE-ENCODE1], NULL, common[z], &pafAlgConfig);
1240             if (!chain) 
1241             {
1242                 TRACE_TERSE1("AS%d: Encode chain initialization failed", as+z);
1243                 return __LINE__;
1244             }
1245         }
1247         //
1248         // Allocate non-common memories for Logical IO drivers
1249         //    Since these structures are used at run-time we allocate from external memory
1250         if (OUTPUT1 <= z && z < OUTPUTN) 
1251         {
1252             PAF_ASP_Chain *chain;
1253             TRACE_TERSE2("PAF_ASOT_initPhaseMalloc: AS%d: non-common output chain init for %d",
1254                            as+z, z);
1255             chain = PAF_ASP_chainInit (&pAstCfg->xOut[z].outChainData, pP->pChainFxns,
1256                         HEAP_EXTERNAL, as+z, acp, &trace,
1257                         outLinkInit[z-OUTPUT1], NULL, common[z], &pafAlgConfig);
1258             if (!chain) 
1259             {
1260                 TRACE_TERSE1("AS%d: Output chain initialization failed", as+z);
1261                 return __LINE__;
1262             }
1263         }
1264     }
1265     TRACE_TERSE1("AS%d: PAF_ASOT_initPhaseCommon: Returning complete.", as+z);
1267     return 0;
1268 } //PAF_ASOT_initPhaseCommon
1270 // -----------------------------------------------------------------------------
1271 // ASOT Initialization Function - Algorithm Keys
1272 //
1273 //   Name:      PAF_ASOT_initPhaseAlgKey
1274 //   Purpose:   Audio Stream Output Task Function for initialization of data values
1275 //              from parameters for Algorithm Keys.
1276 //   From:      audioStream1Task or equivalent
1277 //   Uses:      See code.
1278 //   States:    x
1279 //   Return:    0.
1280 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1281 //              * State information as per parent.
1282 //
1283 // .............................................................................
1284 Int
1285 PAF_ASOT_initPhaseAlgKey(
1286     const PAF_ASOT_Params *pP, 
1287     const PAF_ASOT_Patchs *pQ, 
1288     PAF_ASOT_Config *pAsotCfg
1291     PAF_AST_Config *pAstCfg;
1292     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1293     Int z;                      /* decode/encode counter */
1294     Int s;                      /* key number */
1295     PAF_ASP_Link *that;
1297     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1298     as = pAstCfg->as;
1299     (void)as; // clear compiler warning in case not used with tracing disabled
1301     TRACE_VERBOSE1("PAF_ASOT_initPhaseAlgKey: AS%d: initialization phase - Algorithm Keys", as);
1303     for (z=ENCODE1; z < ENCODEN; z++) 
1304     {
1305         for (s=0; s < pP->pEncAlgKey->length; s++) 
1306         {
1307             if ((pP->pEncAlgKey->code[s].full != 0) && 
1308                 (that = PAF_ASP_chainFind(&pAstCfg->xEnc[z].encChainData, pP->pEncAlgKey->code[s])))
1309             {
1310                 pAstCfg->xEnc[z].encAlg[s] = (ALG_Handle )that->alg;
1311             }
1312             /* Cast in interface, for now --Kurt */
1313             else
1314             {
1315                 pAstCfg->xEnc[z].encAlg[s] = NULL;                
1316             }
1317         }
1318     }
1320     return 0;
1321 } //PAF_ASOT_initPhaseAlgKey
1323 // -----------------------------------------------------------------------------
1324 // ASOT Initialization Function - I/O Devices
1325 //
1326 //   Name:      PAF_ASOT_initPhaseDevice
1327 //   Purpose:   Audio Stream Output Task Function for initialization of I/O Devices.
1328 //   From:      audioStream1Task or equivalent
1329 //   Uses:      See code.
1330 //   States:    x
1331 //   Return:    0 on success.
1332 //              Source code line number on device allocation failure.
1333 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1334 //              * State information as per parent.
1335 //              * Memory allocation errors.
1336 //
1337 Int
1338 PAF_ASOT_initPhaseDevice(
1339     const PAF_ASOT_Params *pP, 
1340     const PAF_ASOT_Patchs *pQ, 
1341     PAF_ASOT_Config *pAsotCfg
1344     PAF_AST_Config *pAstCfg;
1345     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1346     Int z;                      /* input/output counter */
1347     PAF_SIO_IALG_Obj    *pObj;
1348     PAF_SIO_IALG_Config *pAlgConfig;
1349     PAF_IALG_Config pafAlgConfig;
1351     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1352     as = pAstCfg->as;
1353     (void)as; // clear compiler warning in case not used with tracing disabled
1355     TRACE_TERSE1("PAF_ASOT_initPhaseDevice: AS%d: initialization phase - I/O Devices", as);
1357     if(pP->fxns->bufMemPrint)
1358     {
1359         PAF_ALG_setup (&pafAlgConfig, 
1360             HEAP_ID_INTERNAL,               HEAP_INTERNAL, 
1361             HEAP_ID_INTERNAL1,              HEAP_INTERNAL1,
1362             HEAP_ID_EXTERNAL,               HEAP_EXTERNAL,
1363             HEAP_ID_INTERNAL1_SHM,          HEAP_INTERNAL1_SHM,
1364             HEAP_ID_EXTERNAL_SHM,           HEAP_EXTERNAL_SHM,
1365             HEAP_ID_EXTERNAL_NONCACHED_SHM, HEAP_EXTERNAL_NONCACHED_SHM,
1366             HEAP_CLEAR);
1367         TRACE_TERSE2("PAF_ASOT_initPhaseDevice: AS%d: calling PAF_ALG_setup with clear at %d.", as, HEAP_CLEAR);
1368     }
1369     
1370     for (z=OUTPUT1; z < OUTPUTN; z++) 
1371     {
1372         PAF_OutBufConfig *pConfig = &pAstCfg->xOut[z].outBufConfig;
1374         pObj = (PAF_SIO_IALG_Obj *)pAstCfg->xOut[z].outChainData.head->alg;
1375         pAlgConfig = &pObj->config;
1377         pAstCfg->xOut[z].hTxSio = NULL;
1378         pConfig->base.pVoid     = pAlgConfig->pMemRec[0].base;
1379         pConfig->pntr.pVoid     = pAlgConfig->pMemRec[0].base;
1380         pConfig->head.pVoid     = pAlgConfig->pMemRec[0].base;
1381         pConfig->allocation     = pAlgConfig->pMemRec[0].size;
1382         pConfig->sizeofElement  = 3;
1383         pConfig->precision      = 24;
1384         if(pP->fxns->bufMemPrint)
1385         {
1386             pP->fxns->bufMemPrint(z,pAlgConfig->pMemRec[0].size,PAF_ALG_memSpaceToHeapId(&pafAlgConfig,pAlgConfig->pMemRec[0].space),1);
1387         }
1388     }
1389     TRACE_TERSE1("PAF_ASOT_initPhaseDevice: AS%d: initialization phase - I/O Devices complete.", as);
1391     return 0;
1392 } //PAF_ASOT_initPhaseDevice
1394 // -----------------------------------------------------------------------------
1395 // ASOT Initialization Function Helper - Initialization of Audio Frame
1396 //
1397 //   Name:      PAF_ASOT_initFrame0
1398 //   Purpose:   Audio Stream Output Task Function for initialization of the Audio
1399 //              Frame(s) by memory allocation and loading of data pointers
1400 //              and values.
1401 //   From:      AST Parameter Function -> decodeInfo
1402 //   Uses:      See code.
1403 //   States:    x
1404 //   Return:    0 on success.
1405 //              Source code line number on MEM_calloc failure.
1406 //              Source code line number on unsupported option.
1407 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1408 //              * Memory allocation errors.
1409 //              * Unsupported option errors.
1410 //
1412 // MID 314
1413 extern const char AFChanPtrMap[PAF_MAXNUMCHAN+1][PAF_MAXNUMCHAN];
1414 extern PAF_ChannelConfigurationMaskTable PAF_ASP_stdCCMT_patch;
1416 Int
1417 PAF_ASOT_initFrame0(
1418     const PAF_ASOT_Params *pP, 
1419     const PAF_ASOT_Patchs *pQ, 
1420     PAF_ASOT_Config *pAsotCfg, 
1421     Int z
1424     PAF_AST_Config *pAstCfg;
1425     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1426     Int ch;
1427     //Int aLen;
1428     Int aLen_int=0,aLen_ext=0;
1429     Int aSize = sizeof(PAF_AudioData);
1430     Int aAlign = aSize < sizeof (int) ? sizeof (int) : aSize;
1431     Int maxFrameLength = pP->maxFramelength;
1432     Int zX;
1433     PAF_AudioData *aBuf_int=NULL;
1434     PAF_AudioData *aBuf_ext=NULL;
1435     XDAS_UInt8 *metadataBuf;
1436     char i;
1437     Error_Block    eb;
1439     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1440     as = pAstCfg->as;
1442     // Initialize error block
1443     Error_init(&eb); 
1445     // Compute maximum framelength (needed for ARC support)
1446     maxFrameLength += PA_MODULO - maxFrameLength % PA_MODULO;
1447     //aLen = numchan[z] * maxFrameLength;
1448     for (i=0; i < numchan[z]; i++)
1449     {
1450         if (pP->pAudioFrameBufStatus->space[i] == IALG_SARAM)
1451         {
1452             aLen_int += maxFrameLength;
1453         }
1454         else
1455         {
1456             aLen_ext += maxFrameLength;
1457         }
1458     }
1460     //
1461     // Initialize audio frame elements directly
1462     //
1463     pAstCfg->xStr[z].pAudioFrame->fxns = pP->pAudioFrameFunctions;
1464     pAstCfg->xStr[z].pAudioFrame->data.nChannels = PAF_MAXNUMCHAN; ///
1465 ///    pAstCfg->xStr[z].pAudioFrame->data.nChannels = PAF_MAXNUMCHAN_AF;
1466     pAstCfg->xStr[z].pAudioFrame->data.nSamples = FRAMELENGTH;
1467     pAstCfg->xStr[z].pAudioFrame->data.sample = pAstCfg->xStr[z].audioFrameChannelPointers;
1468     pAstCfg->xStr[z].pAudioFrame->data.samsiz = pAstCfg->xStr[z].audioFrameChannelSizes;
1469     pAstCfg->xStr[z].pAudioFrame->pChannelConfigurationMaskTable = &PAF_ASP_stdCCMT;
1471     //
1472     // Allocate memory for and initialize pointers to audio data buffers
1473     //
1474     //   The NUMCHANMASK is used to identify the channels for which data
1475     //   buffers can be allocated. Using this mask and switch statement
1476     //   rather than some other construct allows efficient code generation,
1477     //   providing just the code necessary (with significant savings).
1478     //
1479     if (pP->fxns->bufMemPrint)
1480     {
1481         pP->fxns->bufMemPrint(z, aLen_int*aSize, HEAP_ID_FRMBUF, 2);
1482         pP->fxns->bufMemPrint(z, aLen_ext*aSize, HEAP_ID_EXTERNAL, 2);
1483     }
1485     TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc for audio buffers", as+z);
1486     
1487     if (aLen_int*aSize!=0) // check size != 0, otherwise malloc throws fatal error
1488     {
1489         if (!(aBuf_int = (PAF_AudioData *)Memory_calloc((IHeap_Handle)HEAP_FRMBUF, (aLen_int+(maxFrameLength-FRAMELENGTH))*aSize, aAlign, &eb))) //Qin: Add start offset
1490         {
1491             TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc failed", as+z);
1492             TRACE_TERSE2("  maxFrameLength: %d.  aLen_int*aSize: %d", maxFrameLength, (aLen_int+(maxFrameLength-FRAMELENGTH))*aSize);
1493             SW_BREAKPOINT;
1494             return __LINE__;
1495         }
1496     }
1497         
1498     if (aLen_ext*aSize!=0)
1499     {
1500         if (!(aBuf_ext = (PAF_AudioData *)Memory_calloc((IHeap_Handle)HEAP_EXTERNAL, (aLen_ext+(maxFrameLength-FRAMELENGTH))*aSize, aAlign, &eb)))//Qin: Add start offset
1501         {
1502             TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc failed", as+z);
1503             TRACE_TERSE2("  maxFrameLength: %d.  aLen_ext*aSize: %d", maxFrameLength, (aLen_ext+(maxFrameLength-FRAMELENGTH))*aSize);
1504             SW_BREAKPOINT;
1505             return __LINE__;
1506         }
1507     }
1508     
1509     TRACE_TERSE3("  maxFrameLength: %d.  aLen_int*aSize: %d.  aBuf_int: 0x%x", maxFrameLength, (aLen_int+(maxFrameLength-FRAMELENGTH))*aSize, (IArg)aBuf_int);
1510     TRACE_TERSE3("  maxFrameLength: %d.  aLen_ext*aSize: %d.  aBuf_ext: 0x%x", maxFrameLength, (aLen_ext+(maxFrameLength-FRAMELENGTH))*aSize, (IArg)aBuf_ext);
1512     TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc for metadata buffers", as+z);
1513     if (!(metadataBuf = (XDAS_UInt8 *)Memory_calloc((IHeap_Handle)HEAP_MDBUF, pP->pMetadataBufStatus->bufSize*pP->pMetadataBufStatus->NumBuf, pP->pMetadataBufStatus->alignment, &eb)))
1514     {
1515         TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: Memory_calloc failed", as+z);
1516         TRACE_TERSE1("  bufSize*NumBuf: %d", pP->pMetadataBufStatus->bufSize*pP->pMetadataBufStatus->NumBuf);
1517         SW_BREAKPOINT;
1518         return __LINE__;
1519     }
1521     {
1522         Int i;
1524 #pragma UNROLL(1)
1525         for (i=0; i < PAF_MAXNUMCHAN_AF; i++)
1526         {
1527             pAstCfg->xStr[z].audioFrameChannelPointers[i] = NULL;
1528         }
1529     }
1531     // MID 314
1532     if((numchan[z] > PAF_MAXNUMCHAN) || (numchan[z] < 1)) 
1533     {
1534         TRACE_TERSE1("PAF_ASOT_initFrame0: AS%d: unsupported option", as+z);
1535         return __LINE__;
1536     }
1537     else 
1538     {
1539         Int j = 0;
1540         Int k = 0;
1541         TRACE_TERSE1("PAF_ASOT_initFrame0: AFChanPtrMap[%d][i]", numchan[z]);
1542         for(i=0;i<numchan[z];i++)
1543         {
1544             char chan = AFChanPtrMap[numchan[z]][i];
1545             if(chan != -1)
1546             {
1547                 if(pP->pAudioFrameBufStatus->space[i] == IALG_SARAM)
1548                 {
1549                     pAstCfg->xStr[z].audioFrameChannelPointers[chan] = aBuf_int + maxFrameLength*(j+1) - FRAMELENGTH;
1550                     j++;
1551                 }
1552                 else
1553                 {        
1554                     pAstCfg->xStr[z].audioFrameChannelPointers[chan] = aBuf_ext + maxFrameLength*(k+1) - FRAMELENGTH;
1555                     k++;
1556                 }    
1557                 TRACE_TERSE3("PAF_ASOT_initFrame0: chan = %d = AFChanPtrMap[%d][%d].", chan, numchan[z], i);
1558                 TRACE_TERSE2("PAF_ASOT_initFrame0: audioFrameChannelPointers[%d]: 0x%x", chan, (IArg)pAstCfg->xStr[z].audioFrameChannelPointers[chan]);
1559             }
1560         }
1561     }
1563     for (ch=PAF_LEFT; ch < PAF_MAXNUMCHAN_AF; ch++) 
1564     {
1565         if (pAstCfg->xStr[z].audioFrameChannelPointers[ch])
1566         {
1567             pAstCfg->xStr[z].origAudioFrameChannelPointers[ch] = pAstCfg->xStr[z].audioFrameChannelPointers[ch];
1568         }
1569     }
1571     //
1572     // Initialize meta data elements
1573     //
1574     pAstCfg->xStr[z].pAudioFrame->pafBsMetadataUpdate = XDAS_FALSE;
1575     pAstCfg->xStr[z].pAudioFrame->numPrivateMetadata = 0;
1576     pAstCfg->xStr[z].pAudioFrame->bsMetadata_offset = 0;
1577     pAstCfg->xStr[z].pAudioFrame->bsMetadata_type = PAF_bsMetadata_channelData;
1578     pAstCfg->xStr[z].pAudioFrame->privateMetadataBufSize = pP->pMetadataBufStatus->bufSize;
1579     for(i=0;i<pP->pMetadataBufStatus->NumBuf;i++)
1580     {
1581         pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].offset = 0;
1582         pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].size = 0;
1583         pAstCfg->xStr[z].pAudioFrame->pafPrivateMetadata[i].pMdBuf = metadataBuf + pP->pMetadataBufStatus->bufSize*i;
1584     }
1586     //
1587     // Initialize decoder elements directly
1588     //
1590     for (zX = DECODE1; zX < DECODEN; zX++) 
1591     {
1592         if (pP->streamsFromDecodes[zX] == z) 
1593         {
1594 #ifdef NOAUDIOSHARE
1595             pAstCfg->xDec[zX].decodeInStruct.audioShare.nSamples = 0;
1596             pAstCfg->xDec[zX].decodeInStruct.audioShare.sample = NULL;
1597 #else /* NOAUDIOSHARE */
1598             pAstCfg->xDec[zX].decodeInStruct.audioShare.nSamples = aLen_int;
1599             pAstCfg->xDec[zX].decodeInStruct.audioShare.sample = aBuf_int;
1600 #endif /* NOAUDIOSHARE */
1601         }
1602     }
1604     return 0;
1605 } //PAF_ASOT_initFrame0
1607 // -----------------------------------------------------------------------------
1608 // ASOT Initialization Function Helper - Reinitialization of Audio Frame
1609 // AST Decoding Function              - Reinitialization of Audio Frame
1610 //
1611 //   Name:      PAF_ASOT_initFrame1
1612 //   Purpose:   Audio Stream Task Function for initialization or reinitiali-
1613 //              zation of the Audio Frame(s) by loading of data values of a
1614 //              time-varying nature.
1615 //   From:      audioStream1Task or equivalent
1616 //              AST Parameter Function -> decodeInfo
1617 //              AST Parameter Function -> decodeDecode
1618 //   Uses:      See code.
1619 //   States:    x
1620 //   Return:    0.
1621 //   Trace:     None.
1622 //
1623 Int
1624 PAF_ASOT_initFrame1(
1625     const PAF_ASOT_Params *pP, 
1626     const PAF_ASOT_Patchs *pQ, 
1627     PAF_ASOT_Config *pAsotCfg, 
1628     Int z, 
1629     Int apply
1632     PAF_AST_Config *pAstCfg;
1634     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1636     //
1637     // Reinitialize audio frame elements:
1638     //
1639     //   Channel Configurations during sys init                 = Unknown
1640     //      "          "        during info or decode           = None
1641     //
1642     //   Sample Rate / Count    during sys init, info or decode = Unknown / 0
1643     //
1645     if (apply < 0) 
1646     {
1647         pAstCfg->xStr[z].pAudioFrame->channelConfigurationRequest.legacy = PAF_CC_UNKNOWN;
1648         pAstCfg->xStr[z].pAudioFrame->channelConfigurationStream.legacy = PAF_CC_UNKNOWN;
1649     }
1650     else 
1651     {
1652         pAstCfg->xStr[z].pAudioFrame->channelConfigurationRequest.legacy = PAF_CC_NONE;
1653         pAstCfg->xStr[z].pAudioFrame->channelConfigurationStream.legacy = PAF_CC_NONE;
1654     }
1656     if (apply < 1) 
1657     {
1658         pAstCfg->xStr[z].pAudioFrame->sampleRate = PAF_SAMPLERATE_UNKNOWN;
1659         pAstCfg->xStr[z].pAudioFrame->sampleCount = 0;
1660     }
1662     return 0;
1663 } //PAF_ASOT_initFrame1
1665 // -----------------------------------------------------------------------------
1666 // ASOT Selection Function - Output Device Selection
1667 //
1668 //   Name:      PAF_ASOT_selectDevices
1669 //   Purpose:   Audio Stream Output Task Function for selecting the devices used
1670 //              for output.
1671 //   From:      audioStream1Task or equivalent
1672 //   Uses:      See code.
1673 //   States:    x
1674 //   Return:    Error number in standard form (0 on success).
1675 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1676 //              * State information as per parent.
1677 //
1678 Int
1679 PAF_ASOT_selectDevices(
1680     const PAF_ASOT_Params *pP, 
1681     const PAF_ASOT_Patchs *pQ, 
1682     PAF_ASOT_Config *pAsotCfg
1685     PAF_AST_Config *pAstCfg;
1686     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1687     Int z;                      /* input/output counter */
1688     Int errno = 0;              /* error number */
1689     Int errme;                  /* error number, local */
1690     Int device;
1692     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1693     as = pAstCfg->as;
1694     (void)as;  // clear compiler warning in case not used with tracing disabled
1696     // Select output devices
1697     for (z=OUTPUT1; z < OUTPUTN; z++) 
1698     {
1699         if ((device = pAstCfg->xOut[z].outBufStatus.sioSelect) >= 0) 
1700         {
1701             TRACE_VERBOSE2("PAF_ASOT_selectDevices: AS%d: output device %d selecting ...", as+z, device);
1703             /* check for valid index into device array */
1704             if (device >= pQ->devout->n)
1705             {
1706                 device = 0; /* treat as device None */
1707             }
1709             errme = pP->fxns->deviceSelect(&pAstCfg->xOut[z].hTxSio, SIO_OUTPUT, 
1710                 HEAP_ID_OUTBUF, (Ptr)pQ->devout->x[device]);
1711             if (errme)
1712             {
1713                 TRACE_VERBOSE2("PAF_ASOT_selectDevices: errme 0x%x, errno 0x%x", errme, errno);
1714                 if (!errno)
1715                 {
1716                     errno = ASPERR_DEVOUT + errme;
1717                 }
1718                 pAstCfg->xOut[z].outBufStatus.sioSelect = 0x80;
1719             }
1720             else 
1721             {
1722                 Int zE;
1724                 pAstCfg->xOut[z].outBufStatus.sioSelect = device | 0x80;
1725                 // register outBufStatus and encodeStatus pointers with output devices
1726                 // This enables proper IEC encapsulation.
1727                 if (pAstCfg->xOut[z].hTxSio) 
1728                 {
1729                     // set max # of output buffers (use override if necessary)
1730                     if (pAstCfg->xOut[z].outBufStatus.maxNumBufOverride == 0)
1731                     {
1732                         SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_MAX_NUMBUF,
1733                             (Arg)pP->poutNumBufMap[z]->maxNumBuf);
1734                     }
1735                     else
1736                     {
1737                         SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_MAX_NUMBUF,
1738                             (Arg)pAstCfg->xOut[z].outBufStatus.maxNumBufOverride);
1739                     }
1741                     // register PAF_SIO_IALG object address
1742                     SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_IALGADDR,
1743                         (Arg)pAstCfg->xOut[z].outChainData.head->alg);
1744                     SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_BUFSTATUSADDR, 
1745                         (Arg)&pAstCfg->xOut[z].outBufStatus);
1746                     for (zE=ENCODE1; zE < ENCODEN; zE++) 
1747                     {
1748                         if (pP->outputsFromEncodes[zE] == z) 
1749                         {
1750                             SIO_ctrl(pAstCfg->xOut[z].hTxSio, 
1751                                 PAF_SIO_CONTROL_SET_ENCSTATUSADDR, 
1752                                 (Arg)&pAstCfg->xEnc[zE].encodeStatus);
1753                             break;
1754                         }
1755                     }
1756                 }
1757             }
1758         }
1760 /* No need to start-clocks here, since only selecting the device. */
1761 #if 0
1762         // if device selected and valid then enable stat tracking if
1763         // required and start clocking
1764         if ((pAstCfg->xOut[z].outBufStatus.sioSelect < 0) && (pAstCfg->xOut[z].hTxSio)) 
1765         {
1766             TRACE_VERBOSE0("PAF_ASOT_selectDevices: start SIO clocks");
1767             errme = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_OUTPUT_START_CLOCKS, 0);
1768             if (errme)
1769             {
1770                 TRACE_VERBOSE2("PAF_ASOT_selectDevices: errme 0x%x, errno 0x%x", errme, errno);
1771                 SIO_idle(pAstCfg->xOut[z].hTxSio);
1772                 if (!errno)
1773                 {
1774                     errno = ASPERR_DEVOUT + errme;
1775                 }
1776             }
1777         }
1778 #endif
1779     }
1781     return errno;
1782 } //PAF_ASOT_selectDevices
1785 // -----------------------------------------------------------------------------
1786 // ASOT Processing Function - Decode Processing
1787 //
1788 //   Name:      PAF_ASOT_decodeProcessing
1789 //   Purpose:   Audio Stream Output Task Function for processing audio data.
1790 //
1791 Int
1792 PAF_ASOT_decodeProcessing(
1793     const PAF_ASOT_Params *pP, 
1794     const PAF_ASOT_Patchs *pQ, 
1795     PAF_ASOT_Config *pAsotCfg 
1798     PAF_AST_Config *pAstCfg;
1799     Int errno;                          /* error number */
1800     Int getVal;
1801     enum { INIT, STREAM, ENCODE, FINAL, QUIT, OUT_SIO_UPDATE } state;
1802     state = INIT;
1803     errno = 0; /* error number */
1804     Int frame; // (***) FL: formerly -- decoder input frame count
1805     Int block; // decoder output block count / input frame
1806     Int outSioUpdate;
1807     
1808     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
1810     for (;;) 
1811     {
1812         //
1813         // Check forward (ASIT) error here, TBD
1814         //
1815         
1816         
1817         // Check if any change in output SIO, e.g. from Output shortcut.
1818         // Changes will break FSM and allow Output reconfiguration.
1819         errno = checkOutSio(pP, pAsotCfg, &outSioUpdate);
1820         if (errno < 0)
1821         {
1822             TRACE_TERSE1("PAF_ASOT_decodeProcessing: checkOutSio returned errno = 0x%04x", errno);
1823             break;
1824         }
1825         else if (outSioUpdate)
1826         {
1827             TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: Change in Output SIO selection");
1828             state = OUT_SIO_UPDATE;
1829         }
1830         
1831         // Process commands (encode)
1832         getVal = pP->fxns->encodeCommand(pP, pQ, pAsotCfg);
1833         if (getVal) 
1834         {
1835             /* ignore */;
1836         }
1838         // Process state (decode)
1839         switch (state) 
1840         {
1841             case INIT: // initial state
1842                 gAsopInitCnt++;
1843                 Log_info0("TaskAsop: state=INIT");
1844             
1845                 frame = 0;
1846                 block = 0;
1848 #if 0 // FL: moved to PAF_ASOT_initOutProc()
1849                 // Reset audio frame pointers to original values
1850                 // (may be needed if error occurred).
1851                 resetAfPointers(pP, pAstCfg->xStr);
1852                 // Reset audio frame meta data elements
1853                 resetAfMetadata(pP, pAstCfg->xStr);
1854 #endif
1856                 errno = pP->fxns->decodeInit(pP, pQ, pAsotCfg);
1857                 if (errno)
1858                 {
1859                     TRACE_VERBOSE1("PAF_ASOT_decodeProcessing: INIT, errno 0x%x.  break after decodeInit", errno);
1860                     errno = ASOP_DP_DECINIT_ERR;
1861                     break;
1862                 }
1863                 
1864                 //
1865                 // Setup output: setCheckRateX, start output
1866                 //
1867                 // Establish secondary timing
1868                 errno = pP->fxns->decodeInfo1(pP, pQ, pAsotCfg, frame, block);
1869                 if (errno)
1870                 {
1871                     TRACE_VERBOSE1("PAF_ASOT_decodeProcessing: INIT, errno 0x%x.  break after decodeInfo1", errno);
1872                     break;
1873                 }
1874                 
1875                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: INIT->STREAM");                
1876                 state = STREAM;
1877                 continue;
1878                 
1879             case STREAM: // stream state
1880                 gAsopStreamCnt++;
1881                 Log_info0("TaskAsop: state=STREAM");
1883                 errno = pP->fxns->decodeStream(pP, pQ, pAsotCfg, frame, block);
1884                 if (errno)
1885                 {
1886                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: state: STREAM.  decodeStream err 0x%x", errno);
1887                     errno = ASOP_DP_DECSTREAM_ERR;
1888                     break;
1889                 }
1891                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: STREAM->ENCODE");
1892                 state = ENCODE;
1893                 continue;
1894                 
1895             case ENCODE: // encode state
1896                 gAsopEncodeCnt++;
1897                 Log_info0("TaskAsop: state=ENCODE");
1899                 errno = pP->fxns->decodeEncode(pP, pQ, pAsotCfg, frame, block);
1900                 if (errno)
1901                 {
1902                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: state: ENCODE.  decodeEncode err 0x%x", errno);
1903                     errno = ASOP_DP_DECENC_ERR;
1904                     break;
1905                 }
1906                 
1907                 // Measure cycles in output processing loop.
1908                 // Only measures cycles spent in loop.
1909                 pfpEnd(PFP_ID_ASOT_1, PFP_FINISH_MEAS);
1910                 gNumPfpAsot1--;
1911                 pfpBegin(PFP_ID_ASOT_1, pAsotCfg->taskHandle);
1912                 gNumPfpAsot1++;
1913                 
1914                 // (***) FL: do we need this? 
1915                 //       AF pointers come from CB read, any resets occur in Decoder AF.
1916                 //
1917                 // Reset audio frame pointers to original values
1918                 // (may have been adjusted by ARC or the like).
1919                 resetAfPointers(pP, pAstCfg->xStr);
1921                 // (***) FL: update output (setCheckRateX)
1922                 //           Contained in INFO2 in combined FSM.
1923                 errno = pP->fxns->decodeInfo2(pP, pQ, pAsotCfg, frame, block);
1924                 if (errno)
1925                 {
1926                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: ENCODE break on decodeInfo2. errno 0x%x", errno);
1927                     errno = ASOP_DP_DECINFO2_ERR;
1928                     break;
1929                 }
1931                 block++;
1932                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: ENCODE->FINAL");
1933                 state = FINAL;
1934                 continue;
1935                 
1936             case FINAL:
1937                 gAsopFinalCnt++;
1938                 Log_info0("TaskAsop: state=FINAL");
1939                 
1940                 //
1941                 // (***) FL: this needs to be fixed.
1942                 //       (1) Only require selected Output to be in this FSM
1943                 //           => Dec Status checks aren't valid, 
1944                 //              will probably always exit FSM if only Output running
1945                 //       (2) Checking Dec Status info asych to input events (maybe ok)
1946                 //
1947                 // Check for final frame, and if indicated:
1948                 // - Update audio flag to cause output buffer flush rather than
1949                 //   the default truncate in "complete" processing.
1950                 // - Exit state machine to "complete" processing.
1951 #if 0
1952                 if (pP->fxns->decodeFinalTest(pP, pQ, pAsotCfg, frame, block)) 
1953                 {
1954                     for (z=OUTPUT1; z < OUTPUTN; z++)
1955                     {
1956                         if ((pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_SOUND)
1957                         {
1958                             TRACE_VERBOSE0("PAF_ASOT_outputProcessing: state: FINAL: SOUND -> QUIET");
1959                             pAstCfg->xOut[z].outBufStatus.audio++; // SOUND -> QUIET
1960                         }
1961                     }
1962                     break;
1963                 }
1964 #endif
1965                 errno = pP->fxns->decodeFinalTest(pP, pQ, pAsotCfg, frame, block);
1966                 if (errno < 0)
1967                 {
1968                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: DECODE FINAL break. errno 0x%x", errno);
1969                     errno = ASOP_DP_DECFINALTEST_ERR;
1970                     break;
1971                 }
1972                 else if (errno == ASOP_DP_CB_DRAINED)
1973                 {
1974                     // EOS, exit normally
1975                     TRACE_TERSE1("PAF_ASOT_decodeProcessing: DECODE FINAL normal exit. errno 0x%x", errno);
1976                     errno = ASOP_DP_SOK;
1977                     break;
1978                 }
1980                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: FINAL->STREAM");
1981                 state = STREAM;
1982                 continue;
1983                 
1984             case OUT_SIO_UPDATE:
1985                 gAsopOutSioUpdateCnt++;
1986                 Log_info0("TaskAsop: state=OUT_SIO_UPDATE");
1988                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: OUT_SIO_UPDATE");
1989                 errno = ASOP_DP_OUT_SIO_UPDATE;
1990                 break;
1991                 
1992             case QUIT:
1993                 gAsopQuitCnt++;
1994                 Log_info0("TaskAsop: state=QUIT");
1996                 TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: state: QUIT");
1997                 errno = ASPERR_QUIT;
1998                 break;
2000             default: // unknown state
2001                 // Unknown:
2002                 // - Set error number registers.
2003                 // - Exit state machine to "complete" processing.
2005                 TRACE_TERSE1("PAF_ASOT_decodeProcessing: state: unknown, 0x%x", state);
2006                 errno = ASPERR_UNKNOWNSTATE;
2007                 break;
2009         }  // End of switch (state).
2010         
2011         TRACE_VERBOSE0("PAF_ASOT_decodeProcessing: Calling decode complete");
2012         if (pP->fxns->decodeComplete(pP, pQ, pAsotCfg, NULL, frame, block))
2013         {
2014             /* ignored? */;
2015         }
2016         
2017         //pfpEnd(PFP_ID_ASOT_1, PFP_FINISH_MEAS); // PFP end -- outside of PFP for errors, EOS, or Output SIO change
2018         //gNumPfpAsot1--;
2019         
2020         //return errno;
2021         break;        
2022     } // End of for (;;)
2023         
2024     pfpEnd(PFP_ID_ASOT_1, PFP_FINISH_MEAS); // PFP end -- outside of PFP for errors, EOS, or Output SIO change
2025     gNumPfpAsot1--;
2026         
2027     return errno;
2030 // -----------------------------------------------------------------------------
2031 // ASOT Decoding Function - Encode Command Processing
2032 //
2033 //   Name:      PAF_ASOT_encodeCommand
2034 //   Purpose:   Decoding Function for processing Encode Commands.
2035 //   From:      AST Parameter Function -> decodeProcessing
2036 //   Uses:      See code.
2037 //   States:    x
2038 //   Return:    0.
2039 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2040 //              * Command execution.
2041 //              * SIO control errors.
2042 //              * Error number macros.
2043 //
2044 Int
2045 PAF_ASOT_encodeCommand(
2046     const PAF_ASOT_Params *pP, 
2047     const PAF_ASOT_Patchs *pQ, 
2048     PAF_ASOT_Config *pAsotCfg
2051     PAF_AST_Config *pAstCfg;
2052     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2053     Int z;                      /* encode counter */
2054     Int errno = 0;              /* error number */
2055     Int zO, zS;
2058     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2059     as = pAstCfg->as;
2061     for (z=ENCODE1; z < ENCODEN; z++) 
2062     {
2063         zO = pP->outputsFromEncodes[z];
2064         zS = pP->streamsFromEncodes[z];
2065         if (! (pAstCfg->xEnc[z].encodeStatus.command2 & 0x80)) 
2066         {
2067             switch (pAstCfg->xEnc[z].encodeStatus.command2) 
2068             {
2069                 case 0: // command none - process
2070                     pAstCfg->xEnc[z].encodeStatus.command2 |= 0x80;
2071                     break;
2072                 case 1: // mute command
2073                     TRACE_VERBOSE2("AS%d: PAF_ASOT_encodeCommand: encode command mute (0x%02x)", as+zS, 1);
2074                     if ((pAstCfg->xOut[zO].outBufStatus.audio & 0x0f) != PAF_OB_AUDIO_QUIET
2075                         && pAstCfg->xOut[zO].hTxSio
2076                         && (errno = SIO_ctrl (pAstCfg->xOut[zO].hTxSio, PAF_SIO_CONTROL_MUTE, 0))) 
2077                     {
2078                         errno = (errno & 0xff) | ASPERR_MUTE;
2079                         /* convert to sensical errno */
2080                         TRACE_TERSE1("AS%d: PAF_ASOT_encodeCommand: SIO control failed (mute)", as+zS);
2081                         TRACE_TERSE2("AS%d: PAF_ASOT_encodeCommand: errno = 0x%04x <ignored>", as+zS, errno);
2082                     }
2083                     else 
2084                     {
2085                         pAstCfg->xOut[zO].outBufStatus.audio |= PAF_OB_AUDIO_MUTED;
2086                     }
2087                     pAstCfg->xEnc[z].encodeStatus.command2 |= 0x80;
2088                     break;
2089                 case 2: // unmute command
2090                     TRACE_VERBOSE2("AS%d: PAF_ASOT_encodeCommand: encode command unmute (0x%02x)", as+zS, 2);
2091                     if ((pAstCfg->xOut[zO].outBufStatus.audio & 0x0f) != PAF_OB_AUDIO_QUIET
2092                         && pAstCfg->xOut[zO].hTxSio
2093                         && (errno = SIO_ctrl (pAstCfg->xOut[zO].hTxSio, PAF_SIO_CONTROL_UNMUTE, 0))) 
2094                     {
2095                         errno = (errno & 0xff) | ASPERR_MUTE;
2096                         /* convert to sensical errno */
2097                         TRACE_TERSE1("AS%d: PAF_ASOT_encodeCommand: SIO control failed (unmute)", as+zS);
2098                         TRACE_TERSE2("AS%d: PAF_ASOT_encodeCommand: errno = 0x%04x <ignored>", as+zS, errno);
2099                     }
2100                     else 
2101                     {
2102                         pAstCfg->xOut[zO].outBufStatus.audio &= ~PAF_OB_AUDIO_MUTED;
2103                     }
2104                     pAstCfg->xEnc[z].encodeStatus.command2 |= 0x80;
2105                     break;
2106                 default: // command unknown - ignore
2107                     break;
2108             }
2109         }
2110     }
2112     ERRNO_RPRT (TaskAsop, errno);
2114     return 0;
2115 } //PAF_ASOT_encodeCommand
2117 //debug -- allow dynamic config
2118 //Int16 gStrFrameLen=DEF_STR_FRAME_LEN; // stream frame length (PCM samples)
2120 //   Purpose:   Decoding Function for reinitializing the decoding process.
2121 Int
2122 PAF_ASOT_decodeInit(
2123     const PAF_ASOT_Params *pP, 
2124     const PAF_ASOT_Patchs *pQ, 
2125     PAF_ASOT_Config *pAsotCfg
2128     //PAF_AST_Config *pAstCfg;
2129     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
2130     //Int as;                             /* Audio Stream Number (1, 2, etc.) */
2131     Int z;                              /* decode/encode counter */
2132     Int errno;                          /* error number */
2133     //Int zO, zS;
2135     //pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2136     //as = pAstCfg->as;
2138     pCbCtl = &pAsotCfg->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
2140     for (z=DECODE1; z < DECODEN; z++)
2141     {
2142         // Initialize decoder output circular buffer for stream reads
2143         //errno = cbInitStreamRead(pCbCtl, z, gStrFrameLen);
2144         errno = cbInitStreamRead(pCbCtl, z);
2145         if (errno)
2146         {
2147             TRACE_TERSE1("PAF_ASOT_decodeInit:cbInitStreamRead() error=%d", errno);
2148             SW_BREAKPOINT; // debug
2149             return errno;
2150         }
2151         
2152         // Start decoder output circular buffer reads
2153         errno = cbReadStart(pCbCtl, z);
2154         if (errno)
2155         {
2156             TRACE_TERSE1("PAF_ASOT_decodeInit:cbReadStart() error=%d", errno);
2157             SW_BREAKPOINT; // debug
2158             return errno;
2159         }
2160         
2161         gCbReadAfErr=0;         // reset read circular buffer error count
2162         gDecOpCbRdAfUnd=0;      // reset decoder output circular buffer underflow count
2163         gMaxDecOpCbRdAfUnd=0;   // reset max decoder output circular buffer underflow count
2164         gMasterCbResetCnt=0;    // reset master circular buffer reset count
2166         // FL: debug, log circular buffer control variables
2167         cbLog(pCbCtl, z, 1, "PAF_ASOT_decodeInit:cbReadStart");
2168     }
2169     
2170 #if 0 // moved to PAF_ASOT_outputReset()
2171     // TODO: move this to start of this function so that it doesn't affect IO timing
2172     for (z=ENCODE1; z < ENCODEN; z++) 
2173     {
2174         zO = pP->outputsFromEncodes[z];
2175         zS = pP->streamsFromEncodes[z];
2176         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2177         {
2178             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2179             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2180             ENC_Handle enc = (ENC_Handle )encAlg;
2181             TRACE_VERBOSE1("AS%d: PAF_ASOT_decodeInit: initializing encode", as+zS);
2182             if (encAlg->fxns->algActivate)
2183             {
2184                 encAlg->fxns->algActivate (encAlg);
2185             }
2186             if (enc->fxns->reset)
2187             {
2188                 errno = enc->fxns->reset(enc, NULL, 
2189                     &pAstCfg->xEnc[z].encodeControl, &pAstCfg->xEnc[z].encodeStatus);
2190                 if (errno)
2191                 {
2192                     return errno;
2193                 }
2194             }
2195         }
2196     }
2197 #endif
2198     
2199     return 0;
2202 // -----------------------------------------------------------------------------
2203 // ASOT Decoding Function - Info Processing, Initial
2204 //
2205 //   Name:      PAF_ASOT_decodeInfo1
2206 //   Purpose:   Decoding Function for processing information in a manner that
2207 //              is unique to initial frames of input data.
2208 //   From:      AST Parameter Function -> decodeProcessing
2209 //   Uses:      See code.
2210 //   States:    x
2211 //   Return:    Error number in standard or SIO form (0 on success).
2212 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2213 //              * State information as per parent.
2214 //
2215 Int
2216 PAF_ASOT_decodeInfo1(
2217     const PAF_ASOT_Params *pP, 
2218     const PAF_ASOT_Patchs *pQ, 
2219     PAF_ASOT_Config *pAsotCfg, 
2220     Int frame, 
2221     Int block
2224     //PAF_AST_Config *pAstCfg;
2225     //Int z;                              /* decode/encode counter */
2226     Int errno;                          /* error number */
2228 #if 0 // moved to PAF_ASOT_outputInfo1()
2229     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2231     // run the chain of ASP's on the stream.
2232     TRACE_VERBOSE0("PAF_ASOT_decodeInfo1: calling streamChainFunction.");
2233     errno = pP->fxns->streamChainFunction(pP, pQ, pAsotCfg, 
2234         PAF_ASP_CHAINFRAMEFXNS_RESET, 1, frame);
2235     if (errno)
2236     {
2237         TRACE_TERSE1("PAF_ASOT_decodeInfo1: streamChainFunction returns errno 0x%x ", errno);
2238         return errno;
2239     }
2241     TRACE_VERBOSE0("PAF_ASOT_decodeInfo1: calling enc->info.");
2242     for (z=ENCODE1; z < ENCODEN; z++) 
2243     {
2244         Int zO = pP->outputsFromEncodes[z];
2245         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2246         {
2247             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2248             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2249             ENC_Handle enc = (ENC_Handle )encAlg;
2250             
2251             if (enc->fxns->info)
2252             {
2253                 errno = enc->fxns->info(enc, NULL,
2254                     &pAstCfg->xEnc[z].encodeControl, 
2255                     &pAstCfg->xEnc[z].encodeStatus);
2256                 if (errno)
2257                 {
2258                     TRACE_TERSE1("PAF_ASOT_decodeInfo1: info returns errno 0x%x ", errno);
2259                     return errno;
2260                 }
2261             }
2262         }
2263     }
2264 #endif    
2266     errno = pP->fxns->setCheckRateX(pP, pQ, pAsotCfg, 0);
2267     if (errno)
2268     {
2269         // ignore if rateX has changed since we haven't, but are about to,
2270         // start the output. If we didn't ignore this case then the state machine
2271         // would restart unnecessarily, e.g. in the case of SRC, resulting in
2272         // added latency.
2273         if (errno != ASPERR_INFO_RATECHANGE)
2274         {
2275             TRACE_TERSE1("PAF_ASOT_decodeInfo1: setCheckRateX returns errno 0x%x, not RATECHANGE", errno);
2276             return errno;
2277         }
2278         else
2279         {
2280             TRACE_TERSE0("PAF_ASOT_decodeInfo1: RATECHANGE returns RATECHANGE, ignoring");
2281         }
2282     }
2284     errno = pP->fxns->startOutput(pP, pQ, pAsotCfg);
2285     if (errno) 
2286     {
2287         if (errno == 0x105) 
2288         {
2289             TRACE_TERSE1("PAF_ASOT_decodeInfo1: startOutput returns RING BUFFER FULL (0x%x)", errno);
2290         }
2291         else
2292         {
2293             TRACE_TERSE1("PAF_ASOT_decodeInfo1: startOutput returns errno 0x%x", errno);
2294         }
2295         return errno;
2296     }
2297     
2298     return 0;
2301 // -----------------------------------------------------------------------------
2302 // ASOT Decoding Function - Info Processing, Subsequent
2303 //
2304 //   Name:      PAF_ASOT_decodeInfo2
2305 //   Purpose:   Decoding Function for processing information in a manner that
2306 //              is unique to frames of input data other than the initial one.
2307 //   From:      AST Parameter Function -> decodeProcessing
2308 //   Uses:      See code.
2309 //   States:    x
2310 //   Return:    Error number in standard form (0 on success).
2311 //   Trace:     None.
2312 //
2313 Int
2314 PAF_ASOT_decodeInfo2(
2315     const PAF_ASOT_Params *pP, 
2316     const PAF_ASOT_Patchs *pQ, 
2317     PAF_ASOT_Config *pAsotCfg, 
2318     Int frame, 
2319     Int block
2322     //PAF_AST_Config *pAstCfg;
2323     Int errno;
2325     
2326     //pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2328     errno = pP->fxns->setCheckRateX (pP, pQ, pAsotCfg, 1);
2329     TRACE_VERBOSE1("PAF_ASOT_decodeInfo2: return 0x%x", errno);
2330     return errno;
2331 } //PAF_ASOT_decodeInfo2
2334 PAF_AST_DecOpCircBufStats gCbStats; // FL: debug
2335 // -----------------------------------------------------------------------------
2336 // ASOT Decoding Function - Stream Processing
2337 //
2338 //   Name:      PAF_ASOT_decodeStream
2339 //   Purpose:   Decoding Function for processing of audio frame data by the
2340 //              ASP Algorithms.
2341 //   From:      AST Parameter Function -> decodeProcessing
2342 //   Uses:      See code.
2343 //   States:    x
2344 //   Return:    Error number in standard form (0 on success).
2345 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2346 //              * State information as per parent/child.
2347 //
2348 Int
2349 PAF_ASOT_decodeStream(
2350     const PAF_ASOT_Params *pP, 
2351     const PAF_ASOT_Patchs *pQ, 
2352     PAF_ASOT_Config *pAsotCfg, 
2353     Int frame, 
2354     Int block
2357     PAF_AST_Config *pAstCfg;
2358     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
2359     Int z;                              /* decode/stream counter */
2360     PAF_AudioFrame *pAfRd;
2361     Int cbErrno;
2362     PAF_AST_DecOpCircBufStats cbStats;  /* circular buffer statistics */
2363     Int errno;
2366     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2367     
2368     pCbCtl = &pAsotCfg->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
2369     
2370     for (z=DECODE1; z < DECODEN; z++) 
2371     {
2372         Int zS = pP->streamsFromDecodes[z];
2373         
2374         //
2375         // Read decoder output circular buffer
2376         //
2377         pAfRd = pAstCfg->xStr[zS].pAudioFrame;
2378         GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106);   // debug
2379         cbErrno = cbReadAf(pCbCtl, z, pAfRd);
2380         GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);
2381         if ((cbErrno < 0) && 
2382             (cbErrno != ASP_DECOP_CB_AF_READ_UNDERFLOW) && 
2383             (cbErrno != ASP_DECOP_CB_PCM_READ_UNDERFLOW))
2384         {
2385             gCbReadAfErr++;
2386             TRACE_TERSE1("PAF_ASOT_decodeStream:cbReadAf() error=%d", cbErrno);
2387             //SW_BREAKPOINT; // debug
2388             return cbErrno;
2389         }
2391         // Handle underflows
2392         if ((cbErrno == ASP_DECOP_CB_AF_READ_UNDERFLOW) ||
2393             (cbErrno == ASP_DECOP_CB_PCM_READ_UNDERFLOW))
2394         {
2395             // (***) FL: Need to check behavior of cbReset().
2396             // Need to check behavior on exit/re-entry into Output processing.
2397             gDecOpCbRdAfUnd++; // increment circular buffer underflow count
2398             if (gDecOpCbRdAfUnd >= DEC_OP_CB_RDAF_UND_THR) 
2399             {
2400                 // Underflow count above threshold.
2401                 // (1) set max underflow count to threshold
2402                 // (2) reset underflow count
2403                 // (3) reset circular buffer
2404                 
2405                 gMaxDecOpCbRdAfUnd = DEC_OP_CB_RDAF_UND_THR; // update max underflow count
2406                 gDecOpCbRdAfUnd = 0; // reset underflow count
2408                 // Reset circular buffer
2409                 cbReset(pCbCtl, z);
2410                 gMasterCbResetCnt++; // increment master circular buffer reset count
2411                 Log_info0("ASOT:cbReset");
2412             
2413                 return cbErrno;
2414             }
2415         }
2416         else if ((cbErrno == ASP_DECOP_CB_SOK) && (gDecOpCbRdAfUnd > 0))
2417         {
2418             // No underflow detected.
2419             // update max underflow count,
2420             // reset underflow count
2421             
2422             // update max underflow count
2423             if (gDecOpCbRdAfUnd > gMaxDecOpCbRdAfUnd)
2424             {
2425                 gMaxDecOpCbRdAfUnd = gDecOpCbRdAfUnd;
2426             }
2427             gDecOpCbRdAfUnd = 0; // reset circular buffer underflow count
2428         }
2429         //Log_info0("PAF_ASOT_decodeStream:cbReadAf() complete.");
2430         //GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);
2431         Log_info0("PAF_ASOT_decodeStream:cbReadAf() complete.");
2432         
2433 #if 0 // (***) FL: shows timing of CB read
2434             // (***) debug // B8
2435             {
2436                 static Uint8 toggleState = 0;
2437                 if (toggleState == 0)
2438                     GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106);
2439                 else
2440                     GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);
2441                 toggleState = ~(toggleState);
2442             }
2443 #endif
2445         // FL: debug
2446         // Get circular buffer statistics (debug)
2447         //cbGetStats(pCbCtl, z, &cbStats);
2448         cbGetStats(pCbCtl, z, &gCbStats);
2450         // debug
2451         cbLog(pCbCtl, z, 1, "PAF_ASOT_decodeStream:cbReadAf");
2452         //if (capAfWrite(pAfRd, PAF_LEFT) != CAP_AF_SOK)
2453         //{
2454         //    Log_info0("capAfWrite() error");
2455         //}
2456     }
2457             
2458     TRACE_VERBOSE0("PAF_ASOT_outputStream: calling streamChainFunction.");
2459     errno = pP->fxns->streamChainFunction(pP, pQ, pAsotCfg, 
2460         PAF_ASP_CHAINFRAMEFXNS_APPLY, 1, block);
2461     if (errno)
2462     {
2463         TRACE_TERSE1("PAF_ASOT_outputStream: streamChainFunction returns errno 0x%x ", errno);
2464         return errno;
2465     }
2467     return 0;
2469 } //PAF_ASOT_decodeStream
2471 // -----------------------------------------------------------------------------
2472 // ASOT Decoding Function - Encode Processing
2473 //
2474 //   Name:      PAF_ASOT_decodeEncode
2475 //   Purpose:   Decoding Function for processing of audio frame data by the
2476 //              Encode Algorithm.
2477 //   From:      AST Parameter Function -> decodeProcessing
2478 //   Uses:      See code.
2479 //   States:    x
2480 //   Return:    Error number in standard or SIO form (0 on success).
2481 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2482 //              * State information as per parent.
2483 //
2484 Int
2485 PAF_ASOT_decodeEncode(
2486     const PAF_ASOT_Params *pP, 
2487     const PAF_ASOT_Patchs *pQ, 
2488     PAF_ASOT_Config *pAsotCfg, 
2489     Int frame, 
2490     Int block
2493     PAF_AST_Config *pAstCfg;
2494     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2495     Int z;                      /* encode/output counter */
2496     Int errno;                  /* error number */
2497     Int zX, zE, zS;
2498     UInt32 curTime;
2500     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2501     as = pAstCfg->as;
2503     // Await output buffers (but not first time)
2504     for (z=OUTPUT1; z < OUTPUTN; z++) 
2505     {
2506         // determine encoder associated with this output
2507         zE = z;
2508         for (zX = ENCODE1; zX < ENCODEN; zX++) 
2509         {
2510             if (pP->outputsFromEncodes[zX] == z) 
2511             {
2512                 zE = zX;
2513                 break;
2514             }
2515         }
2516         zS = pP->streamsFromEncodes[zE];
2518         if (pAstCfg->xOut[z].hTxSio) 
2519         {
2520             // update length (e.g. ARC may have changed)
2521             pAstCfg->xOut[z].outBufConfig.lengthofFrame = 
2522                 pAstCfg->xEnc[zE].encodeInStruct.pAudioFrame->sampleCount;
2523             TRACE_GEN2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- idle", as+zS, block);
2524             //GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);
2525             errno = SIO_reclaim(pAstCfg->xOut[z].hTxSio,(Ptr *) &pAstCfg->xOut[z].pOutBuf, NULL);
2526             //GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106); // debug
2527             if (errno < 0)
2528             {
2529                 SIO_idle(pAstCfg->xOut[z].hTxSio);
2530                 TRACE_TERSE2("PAF_ASOT_decodeEncode: AS%d: SIO_reclaim returns error %d", as+zS, -errno);
2531                 return -errno; // SIO negates error codes
2532             }
2533             // TODO: use pC->xOut[z].pOutBuf in following ->encode call
2535 #if 0 // (***) FL: shows timing of Output Rx SIO reclaim
2536             // (***) debug // B8
2537             {
2538                 static Uint8 toggleState = 0;
2539                 if (toggleState == 0)
2540                     GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106);
2541                 else
2542                     GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);
2543                 toggleState = ~(toggleState);
2544             }
2545 #endif            
2547             gAsopTxSioReclaimCnt++;
2549             //
2550             // Simulate Tx SIO_reclaim() pend
2551             //
2552             //Semaphore_pend(semaphoreTxAudio, BIOS_WAIT_FOREVER);
2553             curTime = Clock_getTicks();
2554             //System_printf("System time in TaskAsopFxn Tx audio = %lu\n", (ULong)curTime);
2555             //Log_info1("outputEncode():Tx SIO reclaim(), system time = %u", curTime);
2556         }
2557         else 
2558         {
2559             TRACE_VERBOSE2("AS%d: PAF_ASOT_decodeEncode: processing block %d -- idle <ignored>", as+zS, block);
2560         }
2561     }
2563     // Encode data
2564     for (z=ENCODE1; z < ENCODEN; z++) 
2565     {
2566         Int zO = pP->outputsFromEncodes[z];
2567         Int zS = pP->streamsFromEncodes[z];
2568         (void)zS; // clear compiler warning in case not used with tracing disabled
2569         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2570         {
2571             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2572             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2573             ENC_Handle enc = (ENC_Handle )encAlg;
2574             if (select != pAstCfg->xEnc[z].encodeControl.encActive)
2575             {
2576                 pAstCfg->xEnc[z].encodeControl.encActive = select;
2577                 TRACE_TERSE0("PAF_ASOT_decodeEncode: return error");
2578                 return (-1);
2579             }
2580             TRACE_GEN2("AS%d: PAF_ASOT_decodeEncode: processing block %d -- encode", as+zS, block);
2582             // (MID 1933) temp. workaround for PCE2
2583             pAstCfg->xEnc[z].encodeInStruct.pAudioFrame->data.nChannels = PAF_MAXNUMCHAN;
2585           /*
2586           #if (CURRENT_TRACE_MASK & TRACE_MASK_DATA)
2587             {
2588                 PAF_AudioFrame *pAudioFrame = pC->xEnc[z].encodeInStruct.pAudioFrame;
2589                 int *wp;
2590                 wp = (int*)pAudioFrame->data.sample[0];
2591                 TRACE_DATA((&TR_MOD, "as1-f2: AS%d PAF_ASOT_outputEncode: encoding from ch 0 0x%x. line %d", z, wp, __LINE__));
2592                 TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x (ch0)", wp[0], wp[16], wp[99]));
2593                 wp = (int*)pAudioFrame->data.sample[1];
2594                 TRACE_DATA((&TR_MOD, "as1-f2: PAF_ASOT_outputEncode: encoding from ch 1 0x%x. line %d", wp, __LINE__));
2595                 TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x (ch1)", wp[0], wp[16], wp[99]));
2596                 wp = (int*)pAudioFrame->data.sample[2];
2597                 TRACE_DATA((&TR_MOD, "as1-f2: PAF_ASOT_outputEncode: encoding from ch 2 0x%x. line %d", wp, __LINE__));
2598                 TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x (ch2)", wp[0], wp[16], wp[99]));
2599             }
2600           #endif
2601           */
2603             if (enc->fxns->encode)
2604             {
2605                 pAstCfg->xEnc[z].encodeOutStruct.bypassFlag =
2606                         pP->z_pEncodeStatus[z]->encBypass;
2607                 errno = enc->fxns->encode(enc, NULL, 
2608                     &pAstCfg->xEnc[z].encodeInStruct, 
2609                     &pAstCfg->xEnc[z].encodeOutStruct);
2610                 if (errno)
2611                 {
2612                     if (errno != PCEERR_OUTPUT_POINTERNULL)
2613                     {
2614                         TRACE_TERSE1("PAF_ASOT_decodeEncode: return error %d line %d", errno);
2615                         return errno;
2616                     }
2617                 }
2618             /*  #if (CURRENT_TRACE_MASK & TRACE_MASK_DATA)
2619                 else
2620                 {
2621                     int *wp = (int*)pC->xOut[z].pOutBuf->pntr.pVoid;
2622                     TRACE_DATA((&TR_MOD, "as1-f2: PAF_ASOT_outputEncode: encoded to 0x%x. line %d", wp, __LINE__));
2623                     TRACE_DATA((&TR_MOD, "as1-f2: [0]: 0x%x, [16]: 0x%x, [99]: 0x%x", wp[0], wp[16], wp[99]));
2624                 }
2625               #endif
2626               */
2627             }
2628         }
2629         else 
2630         {
2631             TRACE_VERBOSE2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- encode <ignored>",
2632                 as+pP->streamsFromEncodes[z], block);
2633         }
2634     }
2636     // Transmit data
2637     for (z=OUTPUT1; z < OUTPUTN; z++) 
2638     {
2639         // determine encoder associated with this output
2640         zE = z;
2641         for (zX = ENCODE1; zX < ENCODEN; zX++) 
2642         {
2643             if (pP->outputsFromEncodes[zX] == z) 
2644             {
2645                 zE = zX;
2646                 break;
2647             }
2648         }
2649         zS = pP->streamsFromEncodes[zE];
2651         if (pAstCfg->xOut[z].hTxSio) 
2652         {
2653             TRACE_GEN2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- output", as+zS, block);
2654             //GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_106); // debug
2655             errno = SIO_issue(pAstCfg->xOut[z].hTxSio, 
2656                 &pAstCfg->xOut[z].outBufConfig, sizeof (pAstCfg->xOut[z].outBufConfig), 0);
2657             //GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_106);
2658             if (errno)
2659             {
2660                 SIO_idle(pAstCfg->xOut[z].hTxSio);
2661                 if (errno == 0x105)     // 0x105 == RINGIO_EBUFFULL
2662                 {
2663 //                    statStruct_LogFullRing(STATSTRUCT_AS1_F2);
2664                     TRACE_TERSE1("PAF_ASOT_decodeEncode: SIO_idle returned RINGIO_EBUFFULL (0x%x)", errno);
2665                 }
2666                 if (errno > 0)
2667                 {
2668                     TRACE_TERSE1("PAF_ASOT_decodeEncode: return error 0x%x line %d", errno);
2669                     return (ASPERR_ISSUE + (z << 4));
2670                 }
2671                 else if (errno < 0)
2672                 {
2673                     TRACE_TERSE1("PAF_ASOT_decodeEncode: return neg error 0x%x line %d", -errno);
2674                     return -errno; // SIO negates error codes
2675                 }
2676             }
2677             if (errno > 0)
2678             {
2679                 return (ASPERR_ISSUE + (z << 4));
2680             }
2681             else if (errno < 0)
2682             {
2683                 return -errno; // SIO negates error codes
2684             }
2685         }
2686         else 
2687         {
2688             TRACE_GEN2("PAF_ASOT_decodeEncode: AS%d: processing block %d -- output <ignored>", as+zS, block);
2689         }
2690     }
2692     return 0;
2693 } //PAF_ASOT_decodeEncode
2695 // -----------------------------------------------------------------------------
2696 // ASOT Decoding Function - Stream-Final Processing
2697 //
2698 //   Name:      PAF_ASOT_decodeComplete
2699 //   Purpose:   Decoding Function for terminating the decoding process.
2700 //   From:      AST Parameter Function -> decodeProcessing
2701 //   Uses:      See code.
2702 //   States:    x
2703 //   Return:    0.
2704 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2705 //              * State information as per parent.
2706 //
2707 Int
2708 PAF_ASOT_decodeComplete(
2709     const PAF_ASOT_Params *pP, 
2710     const PAF_ASOT_Patchs *pQ, 
2711     PAF_ASOT_Config *pAsotCfg, 
2712     ALG_Handle decAlg[], 
2713     Int frame, 
2714     Int block
2717     PAF_AST_Config *pAstCfg;
2718     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
2719     Int as;                             /* Audio Stream Number (1, 2, etc.) */
2720     Int z;                              /* decode/encode counter */
2721     Int errno;                          /* error number */
2723     
2724     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2725     as = pAstCfg->as;
2726     (void)as;  // clear compiler warning in case not used with tracing disabled
2728     pCbCtl = &pAsotCfg->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
2729     
2730     for (z=DECODE1; z < DECODEN; z++)
2731     {
2732         // Stop decoder output circular buffer reads
2733         errno = cbReadStop(pCbCtl, z);
2734         if (errno)
2735         {
2736             TRACE_TERSE1("PAF_ASOT_decodeComplete:cbReadStop() error=%d", errno);
2737             SW_BREAKPOINT; // FL: debug
2738             return errno;
2739         }
2740         // FL: debug
2741         cbLog(pCbCtl, z, 1, "PAF_ASOT_decodeComplete:cbReadStop");
2742     }
2743     
2744     pP->fxns->streamChainFunction(pP, pQ, pAsotCfg, PAF_ASP_CHAINFRAMEFXNS_FINAL, 0, frame);
2746     for (z=ENCODE1; z < ENCODEN; z++) 
2747     {
2748         Int zO = pP->outputsFromEncodes[z];
2749         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
2750         {
2751             Int select = pAstCfg->xEnc[z].encodeStatus.select;
2752             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
2753 #ifdef PAF_ASP_FINAL
2754             ENC_Handle enc = (ENC_Handle)encAlg;
2755 #endif /* PAF_ASP_FINAL */
2756             TRACE_VERBOSE1("PAF_ASOT_decodeComplete: AS%d: finalizing encode", as+z);
2757 #ifdef PAF_ASP_FINAL
2758             if (enc->fxns->final)
2759                 enc->fxns->final(enc, NULL, &pAstCfg->xEnc[z].encodeControl,
2760                                  &pAstCfg->xEnc[z].encodeStatus);
2761 #endif /* PAF_ASP_FINAL */
2762             if (encAlg->fxns->algDeactivate)
2763             {
2764                 encAlg->fxns->algDeactivate(encAlg);
2765             }
2766         }
2767         else 
2768         {
2769             TRACE_VERBOSE1("PAF_ASOT_decodeComplete: AS%d: finalizing encode <ignored>", as+z);
2770         }
2771     }
2773     // wait for remaining data to be output
2774     pP->fxns->stopOutput(pP, pQ, pAsotCfg);
2776     return 0;
2777 } //PAF_ASOT_decodeComplete
2779 // -----------------------------------------------------------------------------
2780 // ASOT Decoding Function Helper - SIO Driver Start
2781 //
2782 //   Name:      PAF_ASOT_startOutput
2783 //   Purpose:   Decoding Function for initiating output.
2784 //   From:      AST Parameter Function -> decodeInfo1
2785 //   Uses:      See code.
2786 //   States:    x
2787 //   Return:    Error number in standard or SIO form (0 on success).
2788 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2789 //              * State information as per parent.
2790 //              * SIO control errors.
2791 //
2792 #define DEC_OUTNUMBUF_MAP(X) \
2793       pP->poutNumBufMap[z]->map[(X) >= pP->poutNumBufMap[z]->length ? 0 : (X)]
2795 Int
2796 PAF_ASOT_startOutput(
2797     const PAF_ASOT_Params *pP, 
2798     const PAF_ASOT_Patchs *pQ, 
2799     PAF_ASOT_Config *pAsotCfg
2800
2802     PAF_AST_Config *pAstCfg;
2803     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2804     Int z;                      /* output counter */
2805     Int errno,nbufs, errme;            /* error number */
2806     Int zE, zS, zX;
2807     Int zMD;
2808     PAF_SIO_IALG_Obj    *pObj;
2809     PAF_SIO_IALG_Config *pAlgConfig;
2811     
2812     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2813     as = pAstCfg->as;
2814     zMD = pAstCfg->masterDec;
2816     for (z=OUTPUT1; z < OUTPUTN; z++) 
2817     {
2818         if (pAstCfg->xOut[z].hTxSio) 
2819         {
2820             // determine associated encoder and stream
2821             zE = z;
2822             zS = z;
2823             for (zX = ENCODE1; zX < ENCODEN; zX++) 
2824             {
2825                 if (pP->outputsFromEncodes[zX] == z) 
2826                 {
2827                     zE = zX;
2828                     zS = pP->streamsFromEncodes[zE];
2829                     break;
2830                 }
2831             }
2833 // Need to Revisit: Starting Clocks here seems logical & also manages the McASP without spurious underruns .
2835 #if 1
2836             // if device selected and valid then enable stat tracking if
2837                         // required and start clocking
2838                         if ((pAstCfg->xOut[z].outBufStatus.sioSelect < 0) && (pAstCfg->xOut[z].hTxSio))
2839                         {
2840                                 TRACE_VERBOSE0("PAF_ASOT_startOutput: start SIO clocks");
2841                                 errme = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_OUTPUT_START_CLOCKS, 0);
2842                                 if (errno)
2843                                 {
2844                                         TRACE_VERBOSE2("PAF_ASOT_startOutput: errme 0x%x, errno 0x%x", errme, errno);
2845                                         SIO_idle(pAstCfg->xOut[z].hTxSio);
2846                                         if (!errno)
2847                                         {
2848                                                 errno = ASPERR_DEVOUT + errme;
2849                                         }
2850                                 }
2851                         }
2852 #endif
2853             // Set sample count so that DOB knows how much data to send
2854             pAstCfg->xOut[z].outBufConfig.lengthofFrame =
2855                 pAstCfg->xEnc[zE].encodeInStruct.pAudioFrame->sampleCount;
2857             if (pAstCfg->xOut[z].outBufStatus.markerMode == PAF_OB_MARKER_ENABLED) 
2858             {
2859                 pObj = (PAF_SIO_IALG_Obj *) pAstCfg->xOut[z].outChainData.head->alg;
2860                 pAlgConfig = &pObj->config;
2861                 memset(pAstCfg->xOut[z].outBufConfig.base.pVoid, 0xAA, 
2862                     pAlgConfig->pMemRec[0].size);
2863             }
2865             // The index to DEC_OUTNUMBUF_MAP will always come from the primary/master
2866             // decoder. How should we handle the sourceProgram for multiple decoders?
2867             // Override as needed
2868             nbufs = DEC_OUTNUMBUF_MAP(pAstCfg->xDec[zMD].decodeStatus.sourceProgram);
2869             if (pAstCfg->xOut[z].outBufStatus.numBufOverride[pAstCfg->xDec[zMD].decodeStatus.sourceProgram] > 0)
2870             {
2871                 nbufs = pAstCfg->xOut[z].outBufStatus.numBufOverride[pAstCfg->xDec[zMD].decodeStatus.sourceProgram];
2872             }
2873             SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_NUMBUF, nbufs);
2875             if (errno = SIO_issue(pAstCfg->xOut[z].hTxSio,
2876                 &pAstCfg->xOut[z].outBufConfig, sizeof(pAstCfg->xOut[z].outBufConfig), 0)) 
2877             {
2878                 SIO_idle(pAstCfg->xOut[z].hTxSio);
2879                 TRACE_TERSE2("PAF_ASOT_startOutput: AS%d: SIO_issue failed (0x%x)", as+zS, errno);
2880                 return errno;
2881             }
2883             if (!(pAstCfg->xOut[z].outBufStatus.audio & 0xf0) && 
2884                 (errno =  SIO_ctrl (pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_UNMUTE, 0))) 
2885             {
2886                 errno = (errno & 0xff) | ASPERR_MUTE;
2887                 /* convert to sensical errno */
2888                 TRACE_TERSE2("as1-f2: PAF_ASOT_startOutput: AS%d: SIO control failed (unmute) 0x%x", as+zS, errno);
2889                 return (errno);
2890             }
2891             else
2892             {
2893                 pAstCfg->xOut[z].outBufStatus.audio
2894                     = (pAstCfg->xOut[z].outBufStatus.audio & 0xf0) | PAF_OB_AUDIO_SOUND;                
2895             }
2897             TRACE_VERBOSE1("PAF_ASOT_startOutput: AS%d: output started", as+zS);
2898         }
2899     }
2901     return 0;
2902 } //PAF_ASOT_startOutput
2904 // -----------------------------------------------------------------------------
2905 // ASOT Decoding Function Helper - SIO Driver Stop
2906 //
2907 //   Name:      PAF_ASOT_stopOutput
2908 //   Purpose:   Decoding Function for terminating output.
2909 //   From:      AST Parameter Function -> decodeProcessing
2910 //              AST Parameter Function -> decodeComplete
2911 //   Uses:      See code.
2912 //   States:    x
2913 //   Return:    Error number in standard or SIO form (0 on success).
2914 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
2915 //              * SIO control errors.
2916 //
2917 Int
2918 PAF_ASOT_stopOutput(
2919     const PAF_ASOT_Params *pP, 
2920     const PAF_ASOT_Patchs *pQ, 
2921     PAF_ASOT_Config *pAsotCfg
2924     PAF_AST_Config *pAstCfg;
2925     Int as;                     /* Audio Stream Number (1, 2, etc.) */
2926     Int z;                      /* output counter */
2927     Int errno = 0, getVal;
2928     Int zS, zX;
2929     PAF_SIO_IALG_Obj    *pObj;
2930     PAF_SIO_IALG_Config *pAlgConfig;
2932     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
2933     as = pAstCfg->as;
2934     (void)as;  // clear compiler warning in case not used with tracing disabled
2936     for (z=OUTPUT1; z < OUTPUTN; z++) 
2937     {
2938         if (pAstCfg->xOut[z].hTxSio) 
2939         {
2940             // determine associated encoder and stream
2941             zS = z;
2942             (void)zS;
2943             for (zX = ENCODE1; zX < ENCODEN; zX++) 
2944             {
2945                 if (pP->outputsFromEncodes[zX] == z) 
2946                 {
2947                     zS = pP->streamsFromEncodes[zX];
2948                     break;
2949                 }
2950             }
2952             // Mute output before audio data termination in the usual case,
2953             // where such termination is due to decode error or user command.
2954             // Identification of this as the usual case is provided by the
2955             // "decode processing" state machine.
2956             if (!(pAstCfg->xOut[z].outBufStatus.audio & 0xf0) &&
2957                 ((pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_SOUND) &&
2958                 (getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_MUTE, 0))) 
2959             {
2960                 if (!errno) 
2961                 {
2962                     errno = (getVal & 0xff) | ASPERR_MUTE;
2963                     /* convert to sensical errno */
2964                 }
2965                 TRACE_VERBOSE1("PAF_ASOT_stopOutput:  AS%d: SIO control failed (mute)", as+zS);
2966             }
2968             TRACE_TIME((&TIME_MOD, "... + %d = %d (stopOutput -- begin PAF_SIO_CONTROL_IDLE)", dtime(), TSK_time()));
2970             // Terminate audio data output, truncating (ignore) or flushing
2971             // (play out) final samples as per (1) control register set by
2972             // the user and (2) the type of audio data termination:
2974 #if 0
2975             // This form is not used because driver support for truncating
2976             // data is not supported for internal clocks, although it is
2977             // for external clocks.
2978             getVal = SIO_ctrl(pC->xOut[z].hTxSio, PAF_SIO_CONTROL_IDLE,
2979                 pC->xOut[z].outBufStatus.flush
2980                 & (pC->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_FLUSH
2981                 ? 1 : 0);
2982             /* UNTESTED */
2983 #else
2984             // This form should be used when driver support for truncating
2985             // data is supported for both internal and external clocks.
2986             getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_IDLE,
2987                 pAstCfg->xOut[z].outBufStatus.flush ? 1 :
2988                 (pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_FLUSH
2989                 ? 1 : 0);
2990             /* TESTED */
2991 #endif
2993             TRACE_TIME((&TIME_MOD, "... + %d = %d (stopOutput -- after PAF_SIO_CONTROL_IDLE)", dtime(), TSK_time()));
2995             if (!errno)
2996             {
2997                 errno = getVal;
2998             }
3000             // Mute output after audio data termination in a special case,
3001             // where such termination is due to processing of a final frame
3002             // or user command. Identification of this as a special case is
3003             // provided by the "decode processing" state machine.
3004             if (!(pAstCfg->xOut[z].outBufStatus.audio & 0xf0) &&
3005                 ((pAstCfg->xOut[z].outBufStatus.audio & 0x0f) == PAF_OB_AUDIO_FLUSH) &&
3006                 (getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_MUTE, 0)))
3007             {
3008                 if (!errno) 
3009                 {
3010                     errno = (getVal & 0xff) | ASPERR_MUTE;
3011                     /* convert to sensical errno */
3012                 }
3013                 TRACE_VERBOSE1("as1-f2: PAF_ASOT_stopOutput:  AS%d: SIO control failed (mute)", as+zS);
3014             }
3016             pAstCfg->xOut[z].outBufStatus.audio &= ~0x0f;
3018             // zero output buffers
3019             pObj = (PAF_SIO_IALG_Obj *) pAstCfg->xOut[z].outChainData.head->alg;
3020             pAlgConfig = &pObj->config;
3021             memset (pAstCfg->xOut[z].outBufConfig.base.pVoid, 0, pAlgConfig->pMemRec[0].size);
3022         } //pAstCfg->xOut[z].hTxSio
3023     }//OUTPUT
3025     return errno;
3026 } //PAF_ASOT_stopOutput
3029 // -----------------------------------------------------------------------------
3030 // ASOT Decoding Function Helper - SIO Driver Change
3031 //
3032 //   Name:      PAF_ASOT_setCheckRateX
3033 //   Purpose:   Decoding Function for reinitiating output.
3034 //   From:      AST Parameter Function -> decodeInfo1
3035 //              AST Parameter Function -> decodeInfo2
3036 //   Uses:      See code.
3037 //   States:    x
3038 //   Return:    Error number in standard form (0 on success).
3039 //   Trace:     None.
3040 //
3042 /* 0: set, 1: check, unused for now. --Kurt */
3043 Int
3044 PAF_ASOT_setCheckRateX(
3045     const PAF_ASOT_Params *pP, 
3046     const PAF_ASOT_Patchs *pQ, 
3047     PAF_ASOT_Config *pAsotCfg, 
3048     Int check
3051     PAF_AST_Config *pAstCfg;
3052     float rateX;
3053     PAF_SampleRateHz rateO /* std */, rateI /* inv */;
3054     Int z;                              /* output counter */
3055     Int zx;                             /* output re-counter */
3056     Int getVal;
3057     int inputRate, inputCount, outputRate, outputCount;
3058     Int zMD;
3059     Int zMI;
3060     Int zMS;
3061     Int zE, zX;
3063     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3064     zMD = pAstCfg->masterDec;
3065     zMS = pAstCfg->masterStr;
3066     zMI = pP->zone.master;
3068     inputRate = pAstCfg->xInp[zMI].inpBufStatus.sampleRateStatus;
3069     inputCount = pAstCfg->xDec[zMD].decodeStatus.frameLength;
3070     rateI = pAstCfg->xStr[zMS].pAudioFrame->fxns->sampleRateHz
3071         (pAstCfg->xStr[zMS].pAudioFrame, inputRate, PAF_SAMPLERATEHZ_INV);
3073     for (z=OUTPUT1; z < OUTPUTN; z++) 
3074     {
3075         if (pAstCfg->xOut[z].hTxSio && (pAstCfg->xOut[z].outBufStatus.clock & 0x01)) 
3076         {
3077             // determine associated encoder
3078             zE = z;
3079             for (zX = ENCODE1; zX < ENCODEN; zX++) 
3080             {
3081                 if (pP->outputsFromEncodes[zX] == z) 
3082                 {
3083                     zE = zX;
3084                     break;
3085                 }
3086             }
3088             outputRate = pAstCfg->xEnc[zE].encodeStatus.sampleRate;
3089             outputCount = pAstCfg->xEnc[zE].encodeStatus.frameLength;
3090             rateO = pAstCfg->xStr[zMS].pAudioFrame->fxns->sampleRateHz
3091                 (pAstCfg->xStr[zMS].pAudioFrame, outputRate, PAF_SAMPLERATEHZ_STD);
3092             if ((rateI > 0) && (rateO > 0))
3093             {
3094                 rateX = rateO /* std */ * rateI /* inv */;
3095             }
3096             else if (inputCount != 0)
3097             {
3098                 rateX = (float )outputCount / inputCount;
3099             }
3100             else
3101             {
3102                 return ASPERR_INFO_RATERATIO;
3103             }
3105             getVal = SIO_ctrl(pAstCfg->xOut[z].hTxSio, PAF_SIO_CONTROL_SET_RATEX, (Arg)&rateX);
3106             if (getVal == DOBERR_RATECHANGE) 
3107             {
3108                 for (zx=OUTPUT1; zx < OUTPUTN; zx++)
3109                 {
3110                     if (pAstCfg->xOut[zx].hTxSio)
3111                     {
3112                         SIO_idle (pAstCfg->xOut[zx].hTxSio);
3113                     }
3114                 }
3116                 // this forces an exit from the calling state machine which will
3117                 // eventually call startOutput which calls setCheckRateX for all outputs
3118                 // and so it is ok, in the presence of a rate change on any output, to
3119                 // exit this loop /function early.
3120                 return ASPERR_INFO_RATECHANGE;
3121             }
3122             else if (getVal != SYS_OK)
3123             {
3124                 return ((getVal & 0xff) | ASPERR_RATE_CHECK);
3125             }
3126         }
3127     }
3129     return 0;
3130 } //PAF_ASOT_setCheckRateX
3132 // -----------------------------------------------------------------------------
3133 // ASOT Decoding Function Helper - Chain Processing
3134 //
3135 //   Name:      PAF_ASOT_streamChainFunction
3136 //   Purpose:   Common Function for processing algorithm chains.
3137 //   From:      AST Parameter Function -> decodeInfo1
3138 //              AST Parameter Function -> decodeStream
3139 //              AST Parameter Function -> decodeComplete
3140 //   Uses:      See code.
3141 //   States:    x
3142 //   Return:    Error number in standard form (0 on success).
3143 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
3144 //              * State information as per parent.
3145 //
3146 Int
3147 PAF_ASOT_streamChainFunction(
3148     const PAF_ASOT_Params *pP, 
3149     const PAF_ASOT_Patchs *pQ, 
3150     PAF_ASOT_Config *pAsotCfg, 
3151     Int iChainFrameFxns, 
3152     Int abortOnError, 
3153     Int logArg
3156     PAF_AST_Config *pAstCfg;
3157     Int as;                     /* Audio Stream Number (1, 2, etc.) */
3158     Int z;                      /* stream counter */
3159     Int errno;                  /* error number */
3160     Int dFlag, eFlag, gear;
3161     Int zX;
3162     Int zS;
3164     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3165     as = pAstCfg->as;
3166     (void)as; // clear compiler warning in case not used with tracing disabled
3168     for (zS = STREAM1; zS < STREAMN; zS++)
3169     {
3170         z = pP->streamOrder[zS];  // Select stream order from streamOrder parameter - MID 788
3172         // apply stream
3173         //      unless the stream is associated with a decoder and it is not running
3174         // or
3175         //      unless the stream is associated with an encoder and it is not running
3176         // Also gear control only works for streams with an associated decoder
3177         // if no such association exists then gear 0 (All) is used
3178         dFlag = 1;
3179         gear = 0;
3180         for (zX = DECODE1; zX < DECODEN; zX++) {
3181             if (pP->streamsFromDecodes[zX] == z) {
3182                 dFlag = pAstCfg->xDec[zX].decodeStatus.mode;
3183                 gear = pAstCfg->xDec[zX].decodeStatus.aspGearStatus;
3184                 break;
3185             }
3186         }
3187         eFlag = 1;
3188         for (zX = ENCODE1; zX < ENCODEN; zX++) {
3189             if (pP->streamsFromEncodes[zX] == z) {
3190                 eFlag = pAstCfg->xEnc[zX].encodeStatus.mode;
3191                 break;
3192             }
3193         }
3195         if (dFlag && eFlag) {
3196             PAF_ASP_Chain *chain = pAstCfg->xStr[z].aspChain[gear];
3197             PAF_AudioFrame *frame = pAstCfg->xStr[z].pAudioFrame;
3198             Int (*func) (PAF_ASP_Chain *, PAF_AudioFrame *) =
3199                 chain->fxns->chainFrameFunction[iChainFrameFxns];
3201             TRACE_GEN2(iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_RESET
3202                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (reset)"
3203                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_APPLY
3204                        ? "PAF_ASOT_streamChainFunction: AS%d: processing block %d -- audio stream (apply)"
3205                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_FINAL
3206                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (final)"
3207                        : "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (?????)",
3208                        as+z, logArg);
3209             errno = (*func) (chain, frame);
3210             TRACE_VERBOSE2("PAF_ASOT_streamChainFunction: AS%d: errno 0x%x.",
3211                 as+z, errno);
3213             if (errno && abortOnError)
3214                 return errno;
3215         }
3216         else {
3217             TRACE_GEN2(iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_RESET
3218                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (reset) <ignored>"
3219                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_APPLY
3220                        ? "PAF_ASOT_streamChainFunction: AS%d: processing block %d -- audio stream (apply) <ignored>"
3221                        : iChainFrameFxns == PAF_ASP_CHAINFRAMEFXNS_FINAL
3222                        ? "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (final) <ignored>"
3223                        : "PAF_ASOT_streamChainFunction: AS%d: processing frame %d -- audio stream (?????) <ignored>",
3224                        as+z, logArg);
3225         }
3227         /*
3228         {
3229             void dp_tracePAF_Data(float *lBuf, float *rBuf, int count);
3230             PAF_AudioFrameData *afd;
3231             float ** afPtr;
3233             afd = &(pC->xStr->pAudioFrame->data);
3234             afPtr = (float**)afd->sample;
3235             dp_tracePAF_Data(afPtr[4], afPtr[5], 256);
3237         }
3238         */
3240     }
3242     return 0;
3243 } //PAF_ASOT_streamChainFunction
3245 /* Check if at least one output selected */
3246 static Int checkOutSel(
3247     const PAF_ASOT_Params *pP, 
3248     PAF_ASOT_Config *pAsotCfg,
3249     Int *pOutSel
3252     PAF_AST_Config *pAstCfg;
3253     Int outSel;
3254     Int z;
3255     
3256     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3258     outSel = 0;
3259     for (z=OUTPUT1; z < OUTPUTN; z++) 
3260     {
3261         if (pAstCfg->xOut[z].hTxSio)
3262         {
3263             outSel = 1;
3264             break;
3265         }
3266     }
3267     
3268     *pOutSel = outSel;
3270     return ASOP_SOK;
3273 /* Check if at least one output sio changed */
3274 static Int checkOutSio(
3275     const PAF_ASOT_Params *pP, 
3276     PAF_ASOT_Config *pAsotCfg,
3277     Int *pOutSioUpdate
3280     PAF_AST_Config *pAstCfg;
3281     Int outSioUpdate;
3282     Int z;
3283     
3284     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3286     outSioUpdate = 0;
3287     for (z=OUTPUT1; z < OUTPUTN; z++) 
3288     {
3289         if (pAstCfg->xOut[z].outBufStatus.sioSelect >= 0)
3290         {
3291             outSioUpdate = 1;
3292             break;
3293         }
3294     }
3295     
3296     *pOutSioUpdate = outSioUpdate;
3298     return ASOP_SOK;
3301 // Reset audio frames
3302 static Void resetAfs(
3303     const PAF_ASOT_Params *pP, 
3304     PAF_AST_Stream *xStr
3307     // Reset audio frame pointers to original values
3308     // (may be needed if error occurred).
3309     resetAfPointers(pP, xStr);
3310     // Reset audio frame meta data elements
3311     resetAfMetadata(pP, xStr);
3314 // Reset audio frame pointers to original values
3315 static Void resetAfPointers(
3316     const PAF_ASOT_Params *pP, 
3317     PAF_AST_Stream *xStr
3320     Int z;
3321     Int ch;
3323     // Reset audio frame pointers to original values
3324     for (z = STREAM1; z < STREAMN; z++) 
3325     {
3326         for (ch = PAF_LEFT; ch < PAF_MAXNUMCHAN_AF; ch++) 
3327         {
3328             if (xStr[z].audioFrameChannelPointers[ch])
3329             {
3330                 xStr[z].audioFrameChannelPointers[ch] = 
3331                     xStr[z].origAudioFrameChannelPointers[ch];
3332             }
3333         }
3334     }
3337 // Reset audio frame meta data elements
3338 static Void resetAfMetadata(
3339     const PAF_ASOT_Params *pP, 
3340     PAF_AST_Stream *xStr
3343     Int z;
3344     Int i;
3346     for (z = STREAM1; z < STREAMN; z++) 
3347     {
3348         xStr[z].pAudioFrame->pafBsMetadataUpdate = XDAS_FALSE;
3349         xStr[z].pAudioFrame->numPrivateMetadata = 0;
3350         xStr[z].pAudioFrame->bsMetadata_offset = 0;
3351         xStr[z].pAudioFrame->bsMetadata_type = PAF_bsMetadata_channelData;
3353         for (i = 0; i < pP->pMetadataBufStatus->NumBuf; i++)
3354         {
3355             xStr[z].pAudioFrame->pafPrivateMetadata[i].offset = 0;
3356             xStr[z].pAudioFrame->pafPrivateMetadata[i].size = 0;
3357         }
3358     }
3361 // Initialize Output Processing state function
3362 static Int PAF_ASOT_initOutProc(
3363     const PAF_ASOT_Params *pP, 
3364     PAF_AST_Stream *xStr
3367     // Reset audio frames
3368     resetAfs(pP, xStr);
3369     
3370     return ASOP_SOK;
3373 #if 0
3374 // Init-Sync update audio frame
3375 static Int initSyncUpdateAf(
3376     PAF_AudioFrame *dstAf, 
3377     PAF_AudioFrame *srcAf
3380     memcpy(dstAf, srcAf, sizeof(PAF_AudioFrame));
3381     
3382     return ASOP_SOK;
3384 #endif
3386 //   Purpose:   Init-Sync Dec Reset state function.
3387 //              Performes Dec Reset Init-Sync.
3388 static Int PAF_ASOT_initSyncDecReset(
3389     const PAF_ASOT_Params *pP, 
3390     const PAF_ASOT_Patchs *pQ, 
3391     PAF_ASOT_Config *pAsotCfg,
3392     PAF_AudioFrame *pDecResetAf
3395     PAF_AST_Config *pAstCfg;            // ASIT/ASOT/ASDT shared configuration
3396     Int zMD, zMS;                       // Dec and Stream Master indices
3397     PAF_AudioFrame *pStrAf;             // stream audio frame
3398     PAF_AST_OutInitSyncCtl *pOutIsCtl;  // Output Init-Sync control
3399     Int8 decFlag;                       // dec stage flag
3400     Int errno;                          // error number
3402     
3403     pAstCfg = pAsotCfg->pAstCfg;
3404     zMD = pAstCfg->masterDec;
3405     pOutIsCtl = &pAsotCfg->pAspmCfg->outIsCtl;
3406     
3407     // check for Dec Reset
3408     // store dec reset AF
3409     errno = outIsReadDecStageFlagAndAf(pOutIsCtl, zMD,
3410         ASP_OUTIS_DEC_STAGE_RESET_IDX, &decFlag, pDecResetAf);
3411     if (errno < 0)
3412     {
3413         return errno;
3414     }
3415     
3416     if (decFlag == 0)
3417     {
3418         return ASOP_INITSYNC_NOTREADY;
3419     }
3420     else
3421     {
3422         zMS = pAstCfg->masterStr;
3423         pStrAf = pAstCfg->xStr[zMS].pAudioFrame;
3424         
3425         // Update Stream Audio Frame.
3426         // Copy Dec Reset AF to Stream AF.
3427         //errno = initSyncUpdateAf(pStrAf, pDecResetAf);
3428         //if (errno < 0)
3429         //{
3430         //    return errno;
3431         //}
3432         outIsCpyAf(pDecResetAf, pStrAf);
3433         
3434         // Enc activate
3435         // Enc reset
3436         errno = PAF_ASOT_outputReset(pP, pQ, pAsotCfg);
3437         if (errno < 0)
3438         {
3439             return errno;
3440         }
3441         
3442         return ASOP_SOK;
3443     }
3446 //   Purpose:   ASOT Function for Output reset
3447 static Int PAF_ASOT_outputReset(
3448     const PAF_ASOT_Params *pP, 
3449     const PAF_ASOT_Patchs *pQ, 
3450     PAF_ASOT_Config *pAsotCfg
3453     PAF_AST_Config *pAstCfg;    // ASIT/ASOT/ASDT shared configuration
3454     Int as;                     // Audio Stream Number (1, 2, etc.) */
3455     Int z;                      // encode counter
3456     Int errno;                  // error number
3457     Int zO, zS;
3460     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3461     as = pAstCfg->as;
3463     for (z=ENCODE1; z < ENCODEN; z++) 
3464     {
3465         zO = pP->outputsFromEncodes[z];
3466         zS = pP->streamsFromEncodes[z];
3467         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
3468         {
3469             Int select = pAstCfg->xEnc[z].encodeStatus.select;
3470             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
3471             ENC_Handle enc = (ENC_Handle )encAlg;
3473             TRACE_VERBOSE1("AS%d: PAF_ASOT_outputReset: initializing encode", as+zS);
3475             if (encAlg->fxns->algActivate)
3476             {
3477                 encAlg->fxns->algActivate(encAlg);
3478             }
3479             
3480             if (enc->fxns->reset)
3481             {
3482                 errno = enc->fxns->reset(enc, NULL, 
3483                     &pAstCfg->xEnc[z].encodeControl, 
3484                     &pAstCfg->xEnc[z].encodeStatus);
3485                 if (errno)
3486                 {
3487                     return ASOP_ENCRESET_ERR;
3488                 }
3489             }
3490         }
3491     }    
3492     
3493     return ASOP_SOK;
3496 // FL: debug, allow modification of output frame length via JTAG
3497 Int16 gOutFrameLen=PAF_ASOT_FRAMELENGTH; // output frame length (PCM samples)
3499 //   Purpose:   Init-Sync Dec Info1 state function.
3500 //              Performes Dec Info1 Init-Sync.
3501 static Int PAF_ASOT_initSyncDecInfo1(
3502     const PAF_ASOT_Params *pP, 
3503     const PAF_ASOT_Patchs *pQ, 
3504     PAF_ASOT_Config *pAsotCfg,
3505     PAF_AudioFrame *pDecInfo1Af
3508     PAF_AST_Config *pAstCfg;            // ASIT/ASOT/ASDT shared configuration
3509     Int zMD, zMS;                       // Dec and Stream Master indices
3510     PAF_AudioFrame *pStrAf;             // stream audio frame
3511     PAF_AST_OutInitSyncCtl *pOutIsCtl;  // Output Init-Sync control
3512     Int8 decFlag;                       // dec stage flag
3513     Int errno;                          // error number
3515     
3516     pAstCfg = pAsotCfg->pAstCfg;
3517     zMD = pAstCfg->masterDec;
3518     pOutIsCtl = &pAsotCfg->pAspmCfg->outIsCtl;
3519     
3520     // Check for Dec Reset, 
3521     // Store dec reset AF
3522     errno = outIsReadDecStageFlagAndAf(pOutIsCtl, zMD,
3523         ASP_OUTIS_DEC_STAGE_INFO1_IDX, &decFlag, pDecInfo1Af);
3524     if (errno < 0)
3525     {
3526         return errno;
3527     }
3528     
3529     if (decFlag == 0)
3530     {
3531         return ASOP_INITSYNC_NOTREADY;
3532     }
3533     else
3534     {
3535         zMS = pAstCfg->masterStr;
3536         pStrAf = pAstCfg->xStr[zMS].pAudioFrame;
3537         
3538         // Update Stream Audio Frame.
3539         // Copy Dec Reset AF to Stream AF.
3540         //errno = initSyncUpdateAf(pStrAf, pDecInfo1Af);
3541         //if (errno < 0)
3542         //{
3543         //    return errno;
3544         //}
3545         
3546         // Hack to set ASOT output frame length.
3547             // THD sets this to 256 (hard-coded in Dec Info)
3548             // DDP sets this to 0 (audio frame passthrough, 0 from ASDT AF frame length)
3549             // PCM sets this to 256 (decodeControl.frameLength)
3550         //pDecInfo1Af->sampleCount = 256;                       // !!!! GJ: Revisit !!!!
3551         pDecInfo1Af->sampleCount = gOutFrameLen;
3552         
3553         outIsCpyAf(pDecInfo1Af, pStrAf);
3555         // Hack to set ASOT output frame length
3556             // THD sets this to 256 (hard-coded in Dec Info)
3557             // DDP sets this to 0 (audio frame passthrough, 0 from ASDT AF frame length)
3558             // PCM sets this to 256 (decodeControl.frameLength)
3559         //pStrAf->sampleCount = 256;                    // !!!! GJ: Revisit !!!!
3561         // outputInfo1():
3562         //      - ASP chain reset,
3563         //      - Enc Info
3564         PAF_ASOT_outputInfo1(pP, pQ, pAsotCfg);
3565         if (errno)
3566         {
3567             return ASOP_DECINFO1_ERR;
3568         }
3569         
3570         return ASOP_SOK;
3571     }
3574 //   Purpose:   ASOT function for ASP chain reset and ENC info
3575 static Int PAF_ASOT_outputInfo1(
3576     const PAF_ASOT_Params *pP, 
3577     const PAF_ASOT_Patchs *pQ, 
3578     PAF_ASOT_Config *pAsotCfg
3581     PAF_AST_Config *pAstCfg;
3582     Int z;                              /* decode/encode counter */
3583     Int errno;                          /* error number */
3585     
3586     pAstCfg = pAsotCfg->pAstCfg; // get pointer to AST common (shared) configuration
3588     // FL: frame parameter hard-coded to 0
3589     // run the chain of ASP's on the stream.
3590     TRACE_VERBOSE0("PAF_ASOT_decodeInfo1: calling streamChainFunction.");
3591     errno = pP->fxns->streamChainFunction(pP, pQ, pAsotCfg, 
3592         PAF_ASP_CHAINFRAMEFXNS_RESET, 1, 0);
3593     if (errno)
3594     {
3595         TRACE_TERSE1("PAF_ASOT_outputInfo1: streamChainFunction returns errno 0x%x ", errno);
3596         return errno;
3597     }
3599     TRACE_VERBOSE0("PAF_ASOT_outputInfo1: calling enc->info.");
3600     for (z=ENCODE1; z < ENCODEN; z++) 
3601     {
3602         Int zO = pP->outputsFromEncodes[z];
3603         if (pAstCfg->xOut[zO].hTxSio && pAstCfg->xEnc[z].encodeStatus.mode) 
3604         {
3605             Int select = pAstCfg->xEnc[z].encodeStatus.select;
3606             ALG_Handle encAlg = pAstCfg->xEnc[z].encAlg[select];
3607             ENC_Handle enc = (ENC_Handle )encAlg;
3608             
3609             if (enc->fxns->info)
3610             {
3611                 errno = enc->fxns->info(enc, NULL,
3612                     &pAstCfg->xEnc[z].encodeControl, 
3613                     &pAstCfg->xEnc[z].encodeStatus);
3614                 if (errno)
3615                 {
3616                     TRACE_TERSE1("PAF_ASOT_outputInfo1: info returns errno 0x%x ", errno);
3617                     return errno;
3618                 }
3619             }
3620         }
3621     }
3622     
3623     return 0;
3626 //   Purpose:   Init-Sync Dec Decode1 state function.
3627 //              Performes Dec Decode1 Init-Sync.
3628 static Int PAF_ASOT_initSyncDecDecode1(
3629     const PAF_ASOT_Params *pP, 
3630     const PAF_ASOT_Patchs *pQ, 
3631     PAF_ASOT_Config *pAsotCfg
3634     PAF_AST_Config *pAstCfg;            // ASIT/ASOT/ASDT shared configuration
3635     Int zMD;                            // Dec Master index
3636     PAF_AST_OutInitSyncCtl *pOutIsCtl;  // Output Init-Sync control
3637     Int8 decFlag;                       // dec stage flag
3638     Int errno;                          // error number
3640     
3641     pAstCfg = pAsotCfg->pAstCfg;
3642     zMD = pAstCfg->masterDec;
3643     pOutIsCtl = &pAsotCfg->pAspmCfg->outIsCtl;
3644     
3645     // Check for Dec Reset, 
3646     // Store dec reset AF
3647     errno = outIsReadDecStageFlag(pOutIsCtl, zMD,
3648         ASP_OUTIS_DEC_STAGE_DECODE1_IDX, &decFlag);
3649     if (errno < 0)
3650     {
3651         return errno;
3652     }
3653     
3654     if (decFlag == 0)
3655     {
3656         return ASOP_INITSYNC_NOTREADY;
3657     }
3658     else
3659     {
3660         return ASOP_SOK;
3661     }
3664 //   Purpose:   Init-Sync Re-Sync state function.
3665 //              Peformed Init-Sync using stored Dec Reset/Info1 AFs.
3666 static Int PAF_ASOT_initSyncResync(
3667     const PAF_ASOT_Params *pP, 
3668     const PAF_ASOT_Patchs *pQ, 
3669     PAF_ASOT_Config *pAsotCfg,
3670     PAF_AudioFrame *pDecResetAf,
3671     PAF_AudioFrame *pDecInfo1Af    
3674     PAF_AST_Config *pAstCfg;    // ASIT/ASOT/ASDT shared configuration
3675     Int zMS;                    // Stream Master index
3676     PAF_AudioFrame *pStrAf;     // stream audio frame
3677     Int errno;                  // error number
3679     
3680     pAstCfg = pAsotCfg->pAstCfg;
3681     zMS = pAstCfg->masterStr;
3682     pStrAf = pAstCfg->xStr[zMS].pAudioFrame;
3683         
3684     // Reset audio frames
3685     resetAfs(pP, pAstCfg->xStr);
3686     
3687     //
3688     // Dec Reset re-sync using stored Dec Reset AF
3689     //
3690     
3691     // Update Stream Audio Frame.
3692     // Copy Dec Reset AF to Stream AF.
3693     //errno = initSyncUpdateAf(pStrAf, pDecResetAf);
3694     //if (errno < 0)
3695     //{
3696     //    return errno;
3697     //}
3698     outIsCpyAf(pDecResetAf, pStrAf);
3699     
3700     // Enc activate,
3701     // Enc reset
3702     errno = PAF_ASOT_outputReset(pP, pQ, pAsotCfg);
3703     if (errno)
3704     {
3705         return errno;
3706     }
3707     
3708     //
3709     // Dec Info1 re-sync using stored Dec Info1 AF
3710     //
3711     
3712     // Update Stream Audio Frame.
3713     // Copy Dec Info1 AF to Stream AF.
3714     //errno = initSyncUpdateAf(pStrAf, pDecInfo1Af);
3715     //if (errno < 0)
3716     //{
3717     //    return errno;
3718     //}
3719     outIsCpyAf(pDecInfo1Af, pStrAf);
3720     
3721     // decodeInfo1():
3722     //      - ASP chain reset,
3723     //      - Enc Info
3724     errno = PAF_ASOT_outputInfo1(pP, pQ, pAsotCfg);
3725     if (errno)
3726     {
3727         return errno;
3728     }
3729     
3730     return ASOP_SOK;    
3733 //   Purpose:   Decoding Function for determining whether processing of the
3734 //              current stream is complete.
3735 Int
3736 PAF_ASOT_decodeFinalTest(
3737     const struct PAF_ASOT_Params *pP, 
3738     const struct PAF_ASOT_Patchs *pQ, 
3739     struct PAF_ASOT_Config *pAsotCfg, 
3740     Int frame, 
3741     Int block
3744     PAF_AST_DecOpCircBufCtl *pCbCtl;    // decoder output circular buffer control
3745     Int8 drainedFlag;                   // CB drained indicator flag
3746     Int zMD;                            // master Dec index
3747     Int errno;                          // error number
3749     
3750     pCbCtl = &pAsotCfg->pAspmCfg->decOpCircBufCtl; // get pointer to circular buffer control
3751     zMD = pAsotCfg->pAstCfg->masterDec; // get master Dec index
3753     // Check circular buffer drain state
3754     //GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_107);
3755     errno = cbCheckDrainState(pCbCtl, zMD, &drainedFlag);
3756     //GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_107);
3757     if (errno < 0)
3758     {
3759         return errno;
3760     }
3761     
3762     if (drainedFlag == 1)
3763     {
3764         return ASOP_DP_CB_DRAINED;
3765     }
3766     
3767     return ASOP_DP_SOK;
3768 }