]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - pasdk/test_arm/framework/audioStreamDecodeProc.c
merging from dev_pasdk_frank
[processor-sdk/performance-audio-sr.git] / pasdk / test_arm / framework / audioStreamDecodeProc.c
2 /*
3 Copyright (c) 2016, Texas Instruments Incorporated - http://www.ti.com/
4 All rights reserved.
6 * Redistribution and use in source and binary forms, with or without 
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 /*
37  *  ======== audioStreamDecodeProc.c ========
38  */
40 #include <xdc/cfg/global.h>
41 #include <xdc/runtime/Error.h>
42 #include <xdc/runtime/Log.h>
43 #include <xdc/runtime/Memory.h>
44 #include <ti/sysbios/BIOS.h>
45 #include <ti/sysbios/hal/Cache.h>
46 #include <ti/sysbios/knl/Task.h>
47 #include <ti/ipc/Ipc.h>
48 #include <ti/ipc/MessageQ.h>
49 #include <ti/ipc/MultiProc.h>
51 #include <acp_mds.h>
52 #include <pcm.h>
53 #include "audioStreamProc_params.h"
54 #include "audioStreamProc_patchs.h"
55 #include "audioStreamProc_config.h"
57 #include "common.h"
58 #include "aspMsg_common.h"
59 #include "aspMsg_slave.h"
60 #include "aspDecOpCircBuf_slave.h"
61 #include "audioStreamProc_common.h"
62 #include "audioStreamDecodeProc.h"
64 // FL: debug
65 //#include "fwkSim.h"
66 #include "dbgCapAf.h"
67 #include "dbgDib.h"
70 //
71 // Decoder Definitions
72 //
73 #define decLinkInit pQ->i_decLinkInit
75 #define __TASK_NAME__  "TaskAsdp"
77 extern struct {
78     Int size;
79     IALG_Status *pStatus[512];
80 } IACP_STD_BETA_TABLE;
82 extern const char AFChanPtrMap[PAF_MAXNUMCHAN+1][PAF_MAXNUMCHAN];
83 extern PAF_ChannelConfigurationMaskTable PAF_ASP_stdCCMT;
85 LINNO_DEFN(TaskAsdp); /* Line number macros */
86 ERRNO_DEFN(TaskAsdp); /* Error number macros */
88 // ASDT configuration
89 PAF_ASDT_Config gPAF_ASDT_config 
90 __attribute__ ((section(".globalSectionPafAsdtConfig"))) = {
91     NULL,               // acp
92     {NULL, NULL},       // decOpCircBufCtl
93     &gPAF_AST_config    // ASIT/ASOT/ASDT shared configuration
94 };
96 PAF_AudioFrame *gpDecAudioFrame=NULL;
97 PAF_AudioData  *gDecAudioFrameChannelPointers[PAF_MAXNUMCHAN_AF];
98 PAF_AudioSize  gDecAudioFrameChannelSizes[PAF_MAXNUMCHAN_AF];
99 PAF_AudioData  *gDecOrigAudioFrameChannelPointers[PAF_MAXNUMCHAN_AF];
101 // Underflow threshold before returning error to Top-Level FSM
102 #define DEC_OP_CB_WRTAF_OVR_THR  ( 20 ) // FL: arbitrary setting
103 UInt32 gDecOpCbWrtAfOvr  =0; // decoder output circular buffer overflow count
105 // Global debug counters */
106 UInt32 gSlaveStartErrCnt        =0;
107 UInt32 gSlaveStartCnt           =0;
108 UInt32 gSlaveSourceSelectCnt    =0;
109 UInt32 gSlaveExitCnt            =0;
110 UInt32 gSlaveDecExitCnt         =0;
111 UInt32 gSlaveDecControlCnt      =0;
112 UInt32 gSlaveDecActivateCnt     =0;
113 UInt32 gSlaveDecResetCnt        =0;
114 UInt32 gSlaveDecInfoCnt         =0;
115 UInt32 gSlaveDecDecodeCnt       =0;
116 UInt32 gSlaveDecDeactivateCnt   =0;
119 /*
120  *  ======== taskAsdpFxn ========
121  *  Audio Stream Decode Processing task function
122  */
123 Void taskAsdpFxn(
124 //    Int betaPrimeValue, // FL: revisit
125     const PAF_ASDT_Params *pP,
126     const PAF_ASDT_Patchs *pQ
129     PAF_ASDT_Config *pC;            /* Local configuration pointer */
130     PAF_AST_Config *pAstCfg;        /* Common (shared) configuration pointer */
131     Int as;                         /* Audio Stream Number (1, 2, etc.) */
132     Int z;                          /* input/encode/stream/decode/output counter */
133     Int i;                          /* phase */
134     Int zMD, zMS;
135     Bool done;
136     Bool decDone;
137     ALG_Handle alg[DECODEN_MAX];
138     ASP_Slave_Cmd slaveCmd;
139     Int sourceSelect;
140     DEC_Handle dec;
141     IALG_Cmd decCtrlCmd;            // decoder control command
142     Int decCtrlRet;                 // decoder control return
143     Int errno;                      /* error number */
144     Int size;
145     Int argIdx;
146     // Decoder output circular buffer
147     PAF_AST_DecOpCircBufCtl *pCbCtl;    /* Decoder output circular buffer control */
148     //PAF_AST_DecOpCircBuf *pCb;
149     PAF_AudioFrame *pAfWrt;
150     Int cbErrno;    
151     // Messaging
152     PAF_InpBufConfig *pIpBufConfig;
153     ASP_Msg *pAspMsg;
154     MessageQ_QueueId queId;
155     Int status;
156     Int zI;
157     Int bufEnd, wrapSize, currentBufSize, chunkSize;
159     
160     Log_info0("Enter taskAsdpFxn()");
161     
162     //
163     // Audio Framework Parameters & Patch (*pP, *pQ):
164     //
165     if (!pP) 
166     {
167         TRACE_TERSE0("TaskAsdp: No Parameters defined. Exiting.");
168         LINNO_RPRT(TaskAsdp, -1);
169         return;
170     }
172     if (!pQ) 
173     {
174         TRACE_TERSE0("TaskAsdp: No Patchs defined. Exiting.");
175         LINNO_RPRT(TaskAsdp, -1);
176         return;
177     }    
179     //
180     // Audio Framework Configuration (*pC):
181     //
182     pC = &gPAF_ASDT_config;
183     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration 
184     pCbCtl = &pC->decOpCircBufCtl; // get pointer to circular buffer control
186     // wait for initialization message from master
187     do {
188         status = MessageQ_get(hAspMsgSlave->slaveQue, (MessageQ_Msg *)&pAspMsg, MessageQ_FOREVER);
189         //TRACE_TERSE1("Rx ASP message: status=%d", status);
190     //} while ((status != MessageQ_S_SUCCESS || (pAspMsg->cmd != ASP_SLAVE_START));
191     } while (status != MessageQ_S_SUCCESS);
192     if ((pAspMsg->procId != hAspMsgSlave->masterProcId) ||
193         (pAspMsg->cmd != ASP_SLAVE_START))
194     {
195         TRACE_TERSE3("ERROR: Rx ASP message: procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
196         SW_BREAKPOINT;
197     }
198     hAspMsgSlave->masterMessageId = pAspMsg->messageId; 
199     gSlaveStartCnt++;
200     TRACE_MSG3("Rx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
202     // invalidate AST shared configuration
203     Cache_inv(pAstCfg, sizeof(PAF_AST_Config), Cache_Type_ALLD, 0);
204     // FL: no need to share this pointer, can be local
205     //Cache_inv(&pC, sizeof(PAF_AST_Config *), Cache_Type_ALLD, 0);
206     Cache_wait();
207     
208     // (***) FL: revisit
209     // invalidate Dec configuration for all Decoder zones
210     Cache_inv(&pAstCfg->xDec[0], DECODEN*sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
211     Cache_wait();
213     // (***) FL: revisit
214     // invalidate Beta Table status pointers
215     Cache_inv((Ptr)(&IACP_STD_BETA_TABLE.pStatus[0]), 512*sizeof(IALG_Status *), Cache_Type_ALLD, 0); // invalidate entire beta table
216     Cache_wait();
218     /* Obtain Audio Stream Number (1, 2, etc.) */
219     as = pAstCfg->as;
220     TRACE_TERSE1("TaskAsdp: Started with AS%d.", as);
221     
222     //
223     // Initialize message log trace and line number reporting
224     //
225     for (z=STREAM1; z < STREAMN; z++)
226     {
227         TRACE_TERSE1("TaskAsdp: AS%d: initiated", as+z);
228     }
229     LINNO_RPRT(TaskAsdp, -1);
231     // Get decoder and stream index associated with the master input
232     zMD = pAstCfg->masterDec;
233     zMS = pAstCfg->masterStr;
235     // 
236     // Initialize per parameterized phases.
237     //   - Malloc: Memory Allocation
238     //   - Config: Configuration Initialization
239     //   - AcpAlg: ACP Algorithm Initialization and Local Attachment
240     //   - Common: Common Algorithm Initialization
241     //   - AlgKey: Dec/Enc chain to Array Initialization
242     //   - Unused: (available)
243     //   - Unused: (available)
244     //   - Unused: (available)
245     //
246     LINNO_RPRT(TaskAsdp, -2);
247     for (i=0; i < lengthof(pP->fxns->initPhase); i++)
248     {
249         Int linno;
250         if (pP->fxns->initPhase[i])
251         {
252             if ((linno = pP->fxns->initPhase[i](pP, pQ, pC)))
253             {
254                 LINNO_RPRT(TaskAsdp, linno);
255                 return;
256             }
257         }
258         else 
259         {
260             TRACE_TERSE1("TaskAsdp: AS%d: initialization phase - null", as+zMS);
261         }
262         TRACE_TERSE2("TaskAsdp: AS%d: initialization phase - %d completed", as+zMS, i);
263         LINNO_RPRT(TaskAsdp, -i-3);
264     }
265     
266     //
267     // End of Initialization -- final memory usage report.
268     //
269     if (pP->fxns->memStatusPrint)
270     {
271         pP->fxns->memStatusPrint(HEAP_INTERNAL, HEAP_INTERNAL1, HEAP_EXTERNAL, HEAP_INTERNAL1_SHM);
272     }
273     
274     // (***) FL: revisit
275     // write back Status structure addresses for Beta Units initialized on Slave
276     Cache_wb((Ptr)(&IACP_STD_BETA_TABLE.pStatus[STD_BETA_DECODE]), sizeof(IALG_Status *), Cache_Type_ALLD, 0);
277     Cache_wb((Ptr)(&IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM]), sizeof(IALG_Status *), Cache_Type_ALLD, 0);
278     Cache_wb((Ptr)(&IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM2]), sizeof(IALG_Status *), Cache_Type_ALLD, 0);
279     Cache_wb((Ptr)(&IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP]), sizeof(IALG_Status *), Cache_Type_ALLD, 0);
280     Cache_wb((Ptr)(&IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP2]), sizeof(IALG_Status *), Cache_Type_ALLD, 0);
281     Cache_wb((Ptr)(&IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD]), sizeof(IALG_Status *), Cache_Type_ALLD, 0);
282     Cache_wb((Ptr)(&IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD2]), sizeof(IALG_Status *), Cache_Type_ALLD, 0);
283     Cache_wait();
284     
285     // (***) FL: revisit
286     // write back Status structures for Beta Units initialized on Slave
287     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_DECODE]->size;
288     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_DECODE]), size, Cache_Type_ALLD, 0);
289     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM]->size;
290     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM]), size, Cache_Type_ALLD, 0);
291     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM2]->size;
292     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM2]), size, Cache_Type_ALLD, 0);
293     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP]->size;
294     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP]), size, Cache_Type_ALLD, 0);
295     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP2]->size;
296     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP2]), size, Cache_Type_ALLD, 0);
297     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD]->size;
298     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD]), size, Cache_Type_ALLD, 0);
299     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD2]->size;
300     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD2]), size, Cache_Type_ALLD, 0);
301     Cache_wait();
303     // (***) FL: revisit
304     // write back Dec configuration
305     Cache_wb(&pAstCfg->xDec[0], DECODEN*sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
306     Cache_wait();
307     
308     // Send initialization complete message to master
309     queId = MessageQ_getReplyQueue(pAspMsg);
310     pAspMsg->procId = hAspMsgSlave->slaveProcId;
311     pAspMsg->cmd = ASP_MASTER_START_DONE;
312     pAspMsg->messageId = hAspMsgSlave->masterMessageId | ((UInt32)1<<31);
313     TRACE_MSG3("Tx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
314     status = MessageQ_put(queId, (MessageQ_Msg)pAspMsg); /* send message back */
315     if (status != MessageQ_S_SUCCESS)
316     {
317         SW_BREAKPOINT;
318     }
320     done = FALSE;
321     while (done==FALSE)
322     {
323         // wait for source select message from master
324         do {
325             status = MessageQ_get(hAspMsgSlave->slaveQue, (MessageQ_Msg *)&pAspMsg, MessageQ_FOREVER);
326         } while ((status < 0) || (pAspMsg->cmd != ASP_SLAVE_DEC_SOURCE_SELECT));
327         if ((pAspMsg->procId != hAspMsgSlave->masterProcId) ||
328             (pAspMsg->cmd != ASP_SLAVE_DEC_SOURCE_SELECT))
329         {
330             TRACE_TERSE3("ERROR: Rx ASP message: procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
331             SW_BREAKPOINT;
332         }            
333         hAspMsgSlave->masterMessageId = pAspMsg->messageId; 
334         sourceSelect = *(Int32 *)&pAspMsg->buf[0];
335         TRACE_MSG3("Rx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
336         TRACE_MSG1("sourceSelect=%d.", sourceSelect);
337         // send source select complete message to master
338         queId = MessageQ_getReplyQueue(pAspMsg);
339         pAspMsg->procId = hAspMsgSlave->slaveProcId;
340         pAspMsg->cmd = ASP_MASTER_DEC_SOURCE_SELECT_DONE;
341         pAspMsg->messageId = hAspMsgSlave->masterMessageId | ((UInt32)1<<31);
342         gSlaveSourceSelectCnt++;
343         TRACE_MSG3("Tx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
344         MessageQ_put(queId, (MessageQ_Msg)pAspMsg); /* send message back */
345         
346         for (z=DECODE1; z < DECODEN; z++)
347         {
348             alg[z] = pAstCfg->xDec[z].decAlg[PAF_SOURCE_PCM];
349         }
350         alg[zMD] = pAstCfg->xDec[zMD].decAlg[sourceSelect];
352         // FL: debug, reset IB capture buffer
353         //capIbReset();
354         //Log_info0("capIbReset()");
355         // FL: debug, reset audio frame capture buffer
356         //capAfReset();
357         
358         decDone = FALSE;
359         while (decDone==FALSE)
360         {
361             // wait for received message from master
362             do {
363                 status = MessageQ_get(hAspMsgSlave->slaveQue, (MessageQ_Msg *)&pAspMsg, MessageQ_FOREVER);
364             } while (status < 0);
365             if (pAspMsg->procId != hAspMsgSlave->masterProcId)
366             {
367                 TRACE_TERSE3("ERROR: Rx ASP message: procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
368                 SW_BREAKPOINT;
369             }
370             hAspMsgSlave->masterMessageId = pAspMsg->messageId; 
371             slaveCmd = pAspMsg->cmd;
372             TRACE_MSG3("Rx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
374             switch (slaveCmd)
375             {
376                 case ASP_SLAVE_NULL:
377                 case ASP_SLAVE_START:
378                     gSlaveStartErrCnt++;
379                     TRACE_TERSE1("ERROR: unexpected slaveCmd=%d", slaveCmd);
380                     
381                     break;
382                     
383                 case ASP_SLAVE_EXIT:
384                     gSlaveExitCnt++;
385                     TRACE_TERSE1("slaveCmd=%d", slaveCmd);
386                     
387                     decDone = TRUE;
388                     done = TRUE;
389                     break;
390                     
391                 case ASP_SLAVE_DEC_EXIT:
392                     gSlaveDecExitCnt++;
393                     TRACE_TERSE1("slaveCmd=%d", slaveCmd);
394                     
395                     // send dec exit complete message to master
396                     queId = MessageQ_getReplyQueue(pAspMsg);
397                     pAspMsg->procId = hAspMsgSlave->slaveProcId;
398                     pAspMsg->cmd = ASP_MASTER_DEC_EXIT_DONE;
399                     pAspMsg->messageId = hAspMsgSlave->masterMessageId | ((UInt32)1<<31);
400                     TRACE_MSG3("Tx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
401                     status = MessageQ_put(queId, (MessageQ_Msg)pAspMsg); /* send message back */
402                     if (status != MessageQ_S_SUCCESS)
403                     {
404                         SW_BREAKPOINT;
405                     }
406                     
407                     decDone=TRUE;
408                     break;
409                     
410                 case ASP_SLAVE_DEC_CONTROL:
411                     gSlaveDecControlCnt++;
412                     // simulate dec control load
413                     //simLoad(DEC_CONTROL_LOAD);
414                     
415                     argIdx = 0; // get decIdx
416                     z = *(Int32 *)&pAspMsg->buf[argIdx];
417                     argIdx += sizeof(Int32);
418                     decCtrlCmd = *(IALG_Cmd *)&pAspMsg->buf[argIdx]; // get decCtrlCmd
419                     TRACE_MSG3("slaveCmd=%d, decIdx=%d, decCtrlCmd=%d", slaveCmd, z, decCtrlCmd);
420                     
421                     decCtrlRet = alg[z]->fxns->algControl(alg[z], decCtrlCmd, NULL);
423                     // send dec control complete message to master
424                     queId = MessageQ_getReplyQueue(pAspMsg);
425                     pAspMsg->procId = hAspMsgSlave->slaveProcId;
426                     pAspMsg->cmd = ASP_MASTER_DEC_CONTROL_DONE;
427                     pAspMsg->messageId = hAspMsgSlave->masterMessageId | ((UInt32)1<<31);
428                     argIdx = 0; // set decCtrlRet
429                     *(Int32 *)&pAspMsg->buf[argIdx] = decCtrlRet;
430                     TRACE_MSG3("Tx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
431                     status = MessageQ_put(queId, (MessageQ_Msg)pAspMsg); /* send message back */
432                     if (status != MessageQ_S_SUCCESS)
433                     {
434                         SW_BREAKPOINT;
435                     }
437                     break;
438                     
439                 case ASP_SLAVE_DEC_ACTIVATE:
440                     gSlaveDecActivateCnt++;
441                     // simulate dec activate load
442                     //simLoad(DEC_ACTIVATE_LOAD);
443                     
444                     // (***) FL: revisit
445                     // invalidate Status structures for shared Beta Units
446                     //size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_DECODE]->size;
447                     //Cache_inv((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_DECODE]), size, Cache_Type_ALLD, 0);
448                     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM]->size;
449                     Cache_inv((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM]), size, Cache_Type_ALLD, 0);
450                     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP]->size;
451                     Cache_inv((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP]), size, Cache_Type_ALLD, 0);
452                     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD]->size;
453                     Cache_inv((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD]), size, Cache_Type_ALLD, 0);
454                     Cache_wait();
455                     
456                     argIdx = 0; // get decIdx
457                     z = *(Int32 *)&pAspMsg->buf[argIdx];
458                     TRACE_MSG2("slaveCmd=%d, decIdx=%d", slaveCmd, z);
459                     
460                     // invalidate Dec configuration
461                     Cache_inv(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
462                     Cache_wait();
463                     
464                     if (alg[z]->fxns->algActivate)
465                     {
466                         alg[z]->fxns->algActivate(alg[z]);
467                     }
469                     // Start writes to circular buffer
470                     //pCb = &pAstCfg->xDecOpCb[z];
471                     cbErrno = cbWriteStart(pCbCtl, z);
472                     if (cbErrno < 0)
473                     {
474                         SW_BREAKPOINT;
475                     }
476                     // FL: debug, log circular buffer control variables
477                     cbLog(pCbCtl, z, 1, "cbWriteStart");
478                     // Reset audio frame
479                     resetAf(pP, z, sourceSelect);
480                     
481                     // send dec activate complete message to master
482                     queId = MessageQ_getReplyQueue(pAspMsg);
483                     pAspMsg->procId = hAspMsgSlave->slaveProcId;
484                     pAspMsg->cmd = ASP_MASTER_DEC_ACTIVATE_DONE;
485                     pAspMsg->messageId = hAspMsgSlave->masterMessageId | ((UInt32)1<<31);
486                     TRACE_MSG3("Tx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
487                     status = MessageQ_put(queId, (MessageQ_Msg)pAspMsg); /* send message back */
488                     if (status != MessageQ_S_SUCCESS)
489                     {
490                         SW_BREAKPOINT;
491                     }
492                    
493                     break;
494                     
495                 case ASP_SLAVE_DEC_RESET:
496                     gSlaveDecResetCnt++;
497                     // simulate dec reset load
498                     //simLoad(DEC_RESET_LOAD);
499                     
500                     argIdx = 0; // get decIdx
501                     z = *(Int32 *)&pAspMsg->buf[argIdx];
502                     TRACE_TERSE2("slaveCmd=%d,decIdx=%d", slaveCmd, z);
503                     
504                     dec = (DEC_Handle)alg[z];
505                     errno = 0;
506                     if (dec->fxns->reset)
507                     {
508                         errno = dec->fxns->reset(dec, NULL, &pAstCfg->xDec[z].decodeControl, &pAstCfg->xDec[z].decodeStatus);
509                     }
511                     // write back Dec configuration
512                     Cache_wb(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
513                     Cache_wait();            
515                     // (***) FL: revisit
516                     // write back Status structures for shared Beta Units
517                     //size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_DECODE]->size;
518                     //Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_DECODE]), size, Cache_Type_ALLD, 0);
519                     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM]->size;
520                     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_PCM]), size, Cache_Type_ALLD, 0);
521                     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP]->size;
522                     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_DDP]), size, Cache_Type_ALLD, 0);
523                     size = IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD]->size;
524                     Cache_wb((Ptr)(IACP_STD_BETA_TABLE.pStatus[STD_BETA_THD]), size, Cache_Type_ALLD, 0);
525                     Cache_wait();
526                     
527                     // send dec reset complete message to master
528                     queId = MessageQ_getReplyQueue(pAspMsg);
529                     pAspMsg->procId = hAspMsgSlave->slaveProcId;
530                     pAspMsg->cmd = ASP_MASTER_DEC_RESET_DONE;
531                     pAspMsg->messageId = hAspMsgSlave->masterMessageId | ((UInt32)1<<31);
532                     argIdx = 0; // set decErrno
533                     *(Int32 *)&pAspMsg->buf[argIdx] = errno;
534                     TRACE_MSG3("Tx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
535                     status = MessageQ_put(queId, (MessageQ_Msg)pAspMsg); /* send message back */
536                     if (status != MessageQ_S_SUCCESS)
537                     {
538                         SW_BREAKPOINT;
539                     }
541                     break;
542                     
543                 case ASP_SLAVE_DEC_INFO:
544                     gSlaveDecInfoCnt++;
545                     // simulate dec info load
546                     //simLoad(DEC_INFO_LOAD);
547                     
548                     argIdx = 0; // get decIdx
549                     z = *(Int32 *)&pAspMsg->buf[argIdx];
550                     TRACE_TERSE2("slaveCmd=%d,decIdx=%d", slaveCmd, z);
551                     // Get input associated w/ decoder
552                     zI = pP->inputsFromDecodes[z];
553                     
554                     // (***) FL: revisit
555                     // invalidate Inp configuration
556                     Cache_inv(&pAstCfg->xInp[zI], sizeof(PAF_AST_InpBuf), Cache_Type_ALLD, 0);
557                     Cache_wait();
558                     // invalidate input data
559                     pIpBufConfig = &pAstCfg->xInp[zI].inpBufConfig;
560                     size = pIpBufConfig->frameLength * pIpBufConfig->sizeofElement;
561                     if (sourceSelect == PAF_SOURCE_PCM)
562                     {
563                         size *= pIpBufConfig->stride;
564                     }
566                     bufEnd = (Int) pIpBufConfig->base.pVoid + pIpBufConfig->sizeofBuffer;
567                     currentBufSize = (bufEnd - (Int)pIpBufConfig->pntr.pSmInt);
568                     if (currentBufSize >= size)
569                     {
570                         chunkSize = size;
571                     }
572                     else
573                     {
574                         chunkSize = currentBufSize;
575                     }
576                     wrapSize = size - chunkSize;
577                     // invalidate input data
578                     Cache_inv((Ptr)pIpBufConfig->pntr.pSmInt, chunkSize, Cache_Type_ALLD, 0);
579                     // invalidate Dec configuration
580                     TRACE_MSG2("IBUF : pIpBufConfig->pntr.pSmInt: 0x%x and chunkSize: %d", (IArg)pIpBufConfig->pntr.pSmInt, chunkSize);
581                     Cache_inv(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
582                     // status for selected decoder should be invalidated
583                     Cache_wait();
585                     /* Circular buffer wrap condition*/
586                     //if(((Int) pIpBufConfig->head.pVoid + size)  > bufEnd)
587                     if(wrapSize > 0)
588                     {
589                         // invalidate input data
590                         Cache_inv((Ptr)pIpBufConfig->base.pSmInt, wrapSize, Cache_Type_ALLD, 0);
591                         TRACE_MSG2("IBUF : pIpBufConfig->base.pSmInt: 0x%x and wrapSize: %d", (IArg)pIpBufConfig->base.pSmInt, wrapSize);
592                         // status for selected decoder should be invalidated
593                         Cache_wait();
594                     }
595                     /* Circular buffer wrap condition */
597                     dec = (DEC_Handle)alg[z];
598                     errno = 0;
599                     if (dec->fxns->info)
600                     {
601                         errno = dec->fxns->info(dec, NULL, &pAstCfg->xDec[z].decodeControl, &pAstCfg->xDec[z].decodeStatus);
602                     }
603                     
604                     // write back Dec configuration
605                     Cache_wb(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
606                     Cache_wait();            
607                     
608                     // Re-initialize audio frame if decoder is disabled or 
609                     // doesn't have a valid input 
610                     if (!pAstCfg->xDec[z].decodeStatus.mode || !pAstCfg->xInp[zI].hRxSio)
611                     {
612                         pP->fxns->initFrame1(pP, pQ, pC, z, 0);
613                     }
615                     // send dec info complete message to master
616                     queId = MessageQ_getReplyQueue(pAspMsg);
617                     pAspMsg->procId = hAspMsgSlave->slaveProcId;
618                     pAspMsg->cmd = ASP_MASTER_DEC_INFO_DONE;
619                     pAspMsg->messageId = hAspMsgSlave->masterMessageId | ((UInt32)1<<31);
620                     argIdx = 0; // set decErrno
621                     *(Int32 *)&pAspMsg->buf[argIdx] = errno;
622                     TRACE_MSG3("Tx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
623                     status = MessageQ_put(queId, (MessageQ_Msg)pAspMsg); /* send message back */
624                     if (status != MessageQ_S_SUCCESS)
625                     {
626                         SW_BREAKPOINT;
627                     }
628                     
629                     break;
630                     
631                 case ASP_SLAVE_DEC_DECODE:
632                     gSlaveDecDecodeCnt++;
633                     // simulate dec info load
634                     //simLoad(DEC_DECODE_LOAD);
635                     
636                     argIdx = 0; // get decIdx
637                     z = *(Int32 *)&pAspMsg->buf[argIdx];
638                     TRACE_TERSE2("slaveCmd=%d, decIdx=%d", slaveCmd, z);
639                     // Get input associated w/ decoder
640                     zI = pP->inputsFromDecodes[z];
641                     
642                     // Reset AF samsiz
643                     resetAfSamsiz(z);
644                     
645                     // invalidate Dec configuration
646                     Cache_inv(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
647                     Cache_wait();
648                     //TRACE_TERSE0("Dec:cache wb done");
649             
650                     dec = (DEC_Handle)alg[z];
651                     //TRACE_TERSE1("Dec:dec handle=0x%04x", (IArg)dec);
653                     errno = 0;
654                     cbErrno = 0;
655                     if (dec->fxns->decode)
656                     {
657                         // FL: debug, capture input buffer
658                         //capIb(pAstCfg->xInp[z].pInpBuf);
659                     
660                         errno = dec->fxns->decode(dec, NULL, &pAstCfg->xDec[z].decodeInStruct, &pAstCfg->xDec[z].decodeOutStruct);
661                         if (errno < 0)
662                         {
663                             SW_BREAKPOINT;
664                         }
665                         TRACE_TERSE0("Dec:decode done");
666                         
667                         // copy decoder output to decoder output circular buffers
668                         //pCb = &pAstCfg->xDecOpCb[z];
669                         //TRACE_TERSE1("Dec:pCb=0x%04x", (IArg)pCb);
670                         
671                         pAfWrt = pAstCfg->xDec[z].decodeOutStruct.pAudioFrame;
672                         TRACE_TERSE1("Dec:pAfWrt=0x%04x", (IArg)pAfWrt);
673                         //TRACE_TERSE1("nSamples=%d",pAfWrt->data.nSamples);
674                         
675                         // FL: debug, capture audio frame
676                         //if (capAfWrite(pAfWrt, PAF_CNTR) != CAP_AF_SOK)
677                         //{
678                         //    Log_info0("capAfWrite() error");
679                         //}                        
681                         cbErrno = cbWriteAf(pCbCtl, z, pAfWrt);
682                         if (cbErrno < 0)
683                         {
684                             //SW_BREAKPOINT; // FL: debug
685                             
686                             if (cbErrno == ASP_DECOP_CB_WRITE_OVERFLOW)
687                             {
688                                 // Reset circular buffer
689                                 cbReset(pCbCtl, z);
690                                 // Update overflow count, return if above threshold
691                                 gDecOpCbWrtAfOvr++;
692                                 if (gDecOpCbWrtAfOvr < DEC_OP_CB_WRTAF_OVR_THR)
693                                 {
694                                     cbErrno = 0;
695                                 }
696                                 else 
697                                 {
698                                     gDecOpCbWrtAfOvr = 0;
699                                 }
700                             }
701                         }
702                         TRACE_TERSE0("Dec:cbWriteAf() complete");
703                         
704                         // FL: debug, log circular buffer control variables
705                         cbLog(pCbCtl, z, 1, "cbWriteAf");
706                     }
707                     
708                     // write back Dec configuration
709                     Cache_wb(&pAstCfg->xDec[z], sizeof(PAF_AST_Decode), Cache_Type_ALLD, 0);
710                     Cache_wait();
711                     
712                     // Re-initialize audio frame if decoder is disabled or 
713                     // doesn't have a valid input 
714                     if (!pAstCfg->xDec[z].decodeStatus.mode || !pAstCfg->xInp[zI].hRxSio)
715                     {
716                         pP->fxns->initFrame1(pP, pQ, pC, z, 0);
717                     }
719                     // send dec info complete message to master
720                     queId = MessageQ_getReplyQueue(pAspMsg);
721                     pAspMsg->procId = hAspMsgSlave->slaveProcId;
722                     pAspMsg->cmd = ASP_MASTER_DEC_DECODE_DONE;
723                     pAspMsg->messageId = hAspMsgSlave->masterMessageId | ((UInt32)1<<31);
724                     argIdx = 0; // set decErrno
725                     *(Int32 *)&pAspMsg->buf[argIdx] = errno;
726                     argIdx += sizeof(Int32); // set cbErrno
727                     *(Int32 *)&pAspMsg->buf[argIdx] = cbErrno;                    
728                     TRACE_MSG3("Tx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
729                     status = MessageQ_put(queId, (MessageQ_Msg)pAspMsg); /* send message back */
730                     if (status != MessageQ_S_SUCCESS)
731                     {
732                         SW_BREAKPOINT;
733                     }
734                     
735                     break;
736                     
737                 case ASP_SLAVE_DEC_DEACTIVATE:
738                     gSlaveDecDeactivateCnt++;
739                     // simulate dec info load
740                     //simLoad(DEC_DEACTIVATE_LOAD);
741                     
742                     argIdx = 0; // get decIdx
743                     z = *(Int32 *)&pAspMsg->buf[argIdx];
744                     TRACE_TERSE2("slaveCmd=%d, decIdx=%d", slaveCmd, z);
745                     
746                     if (alg[z]->fxns->algDeactivate)
747                     {
748                         alg[z]->fxns->algDeactivate(alg[z]);
749                     }
750                     
751                     // Stop writes to circular buffer
752                     //pCb = &pAstCfg->xDecOpCb[z];
753                     cbErrno = cbWriteStop(pCbCtl, z);
754                     if (cbErrno < 0)
755                     {
756                         SW_BREAKPOINT;
757                     }
758                     // FL: debug, log circular buffer control variables
759                     cbLog(pCbCtl, z, 1, "cbWriteStop");
760                     
761                     // send dec deactivate complete message to master
762                     queId = MessageQ_getReplyQueue(pAspMsg);
763                     pAspMsg->procId = hAspMsgSlave->slaveProcId;
764                     pAspMsg->cmd = ASP_MASTER_DEC_DEACTIVATE_DONE;
765                     pAspMsg->messageId = hAspMsgSlave->masterMessageId | ((UInt32)1<<31);
766                     TRACE_MSG3("Tx ASP message, procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
767                     status = MessageQ_put(queId, (MessageQ_Msg)pAspMsg); /* send message back */
768                     if (status != MessageQ_S_SUCCESS)
769                     {
770                         SW_BREAKPOINT;
771                     }
772                     
773                     break;
774                     
775                 default:
776                     TRACE_TERSE1("ERROR: invalid slaveCmd=%d", slaveCmd);
777                     break;
778             }
779         }
780     }
781     
782     Log_info0("exit taskAsdpFxn()");   
786 // -----------------------------------------------------------------------------
787 // AST Initialization Function - Memory Allocation
788 //
789 //   Name:      PAF_AST_initPhaseMalloc
790 //   Purpose:   Audio Stream Task Function for initialization of data pointers
791 //              by allocation of memory.
792 //   From:      audioStream1Task or equivalent
793 //   Uses:      See code.
794 //   States:    x
795 //   Return:    0 on success.
796 //              Source code line number on MEM_calloc failure.
797 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
798 //              * State information as per parent.
799 //              * Memory allocation errors.
800 //
801 Int
802 PAF_ASDT_initPhaseMalloc(
803     const PAF_ASDT_Params *pP, 
804     const PAF_ASDT_Patchs *pQ, 
805     PAF_ASDT_Config *pC
808     PAF_AST_Config *pAstCfg;
809     Int as;                     /* Audio Stream Number (1, 2, etc.) */
810     Int zMS;
811     Error_Block eb;
813     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
814     as = pAstCfg->as;
815     zMS = pAstCfg->masterStr;
816     
817     TRACE_TERSE1("PAF_ASDT_initPhaseMalloc: AS%d: initialization phase - memory allocation", as+zMS);
819     // Initialize error block
820     Error_init(&eb); 
822 //    if (!(gpDecAudioFrame = (PAF_AudioFrame *)Memory_calloc((IHeap_Handle)HEAP_INTERNAL1,
823     if (!(gpDecAudioFrame = (PAF_AudioFrame *)Memory_calloc((IHeap_Handle)HEAP_EXTERNAL, //Qin - moved to external memory
824         DECODEN * sizeof (PAF_AudioFrame), 4, &eb)))
825     {
826         TRACE_TERSE1("PAF_ASDT_initPhaseMalloc: AS%d: Memory_calloc failed", as+zMS);
827         SW_BREAKPOINT;
828         return __LINE__;
829     }
830     TRACE_TERSE3("PAF_ASDT_initPhaseMalloc. (gpAudioFrameSlave) %d bytes from space %d at 0x%x.",
831             DECODEN * sizeof (PAF_AudioFrame),
832             HEAP_ID_INTERNAL1, (IArg)gpDecAudioFrame);
834     TRACE_TERSE1("PAF_ASDT_initPhaseMalloc: AS%d: initialization phase - memory allocation complete.", as+zMS);
835     TRACE_TERSE1("PAF_ASDT_initPhaseMalloc: AS%d: initialization phase - memory allocation complete.", as+zMS);
836     return 0;
837 } //PAF_ASDT_initPhaseMalloc
839 // -----------------------------------------------------------------------------
840 // AST Initialization Function - Memory Initialization from Configuration
841 //
842 //   Name:      PAF_AST_initPhaseConfig
843 //   Purpose:   Audio Stream Task Function for initialization of data values
844 //              from parameters.
845 //   From:      audioStream1Task or equivalent
846 //   Uses:      See code.
847 //   States:    x
848 //   Return:    0 on success.
849 //              Other as per initFrame0 and initFrame1.
850 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
851 //              * State information as per parent.
852 //
854 Int
855 PAF_ASDT_initPhaseConfig(
856     const PAF_ASDT_Params *pP, 
857     const PAF_ASDT_Patchs *pQ, 
858     PAF_ASDT_Config *pC
861     PAF_AST_Config *pAstCfg;
862     Int as;                    /* Audio Stream Number (1, 2, etc.) */
863     Int zMS;
864     Int z;
866     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
867     as = pAstCfg->as;
868     zMS = pAstCfg->masterStr;
869     
870     TRACE_TERSE1("PAF_ASDT_initPhaseConfig: AS%d: initialization phase - configuration", as+zMS);
872     // overwrite pointer to audio frame in framework decode control
873     for (z=DECODE1; z < DECODEN; z++) 
874     {
875         //Int zS = pP->streamsFromDecodes[z]; // FL: formerly on master
876         //pC->xDec[z].decodeControl.pAudioFrame = pC->xStr[zS].pAudioFrame; FL: formerly on master
877         //pC->xDec[z].decodeInStruct.pAudioFrame = pC->xStr[zS].pAudioFrame; FL: formerly on master
878         pAstCfg->xDec[z].decodeControl.pAudioFrame = &gpDecAudioFrame[z];
879         pAstCfg->xDec[z].decodeInStruct.pAudioFrame = &gpDecAudioFrame[z];
880         pAstCfg->xDec[z].decodeStatus = *pP->z_pDecodeStatus[z];
881         pP->fxns->initFrame0(pP, pQ, pC, z);
882     }
884     return 0;
885 }  //PAF_ASDT_initPhaseConfig
887 // -----------------------------------------------------------------------------
888 // AST Initialization Function - ACP Algorithm Instantiation
889 //
890 //   Name:      PAF_AST_initPhaseAcpAlg
891 //   Purpose:   Audio Stream Task Function for initialization of ACP by
892 //              instantiation of the algorithm.
893 //   From:      audioStream1Task or equivalent
894 //   Uses:      See code.
895 //   States:    x
896 //   Return:    0 on success.
897 //              Source code line number on ACP Algorithm creation failure.
898 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
899 //              * State information as per parent.
900 //              * Memory allocation errors.
901 //
902 Int
903 PAF_ASDT_initPhaseAcpAlg(
904     const PAF_ASDT_Params *pP, 
905     const PAF_ASDT_Patchs *pQ, 
906     PAF_ASDT_Config *pC
909     PAF_AST_Config *pAstCfg;
910     Int as;                    /* Audio Stream Number (1, 2, etc.) */
911     Int z;                     /* input/encode/stream/decode/output counter */
912     Int betaPrimeOffset;
913     ACP_Handle acp;
914     Int zMS;
915     Int zS; //, zX;
917     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
918     as = pAstCfg->as;
919     zMS = pAstCfg->masterStr;    
921     TRACE_TERSE1("PAF_ASDT_initPhaseAcpAlg: AS%d: initialization phase - ACP Algorithm", as+zMS);
923     ACP_MDS_init();
925     if (!(acp = (ACP_Handle)ACP_MDS_create(NULL))) 
926     {
927         TRACE_TERSE1("PAF_ASDT_initPhaseAcpAlg: AS%d: ACP algorithm instance creation  failed", as+zMS);
928         return __LINE__;
929     }
930     pC->acp = acp;
932     ((ALG_Handle)acp)->fxns->algControl((ALG_Handle)acp,
933         ACP_GETBETAPRIMEOFFSET, (IALG_Status *)&betaPrimeOffset);
935     for (z=DECODE1; z < DECODEN; z++) 
936     {
937         zS = pP->streamsFromDecodes[z];
938         acp->fxns->attach(acp, ACP_SERIES_STD,
939             STD_BETA_DECODE + betaPrimeOffset * (as-1+zS),
940             (IALG_Status *)&pAstCfg->xDec[z].decodeStatus);
941         /* Ignore errors, not reported. */
942     }
944     TRACE_TERSE1("PAF_ASDT_initPhaseAcpAlg: AS%d: initialization phase - ACP Algorithm complete.", as+zMS);
946     return 0;
947 } //PAF_AST_initPhaseAcpAlg
949 // -----------------------------------------------------------------------------
950 // AST Initialization Function - Common Memory and Algorithms
951 //
952 //   Name:      PAF_AST_initPhaseCommon
953 //   Purpose:   Audio Stream Task Function for initialization of data pointers
954 //              by allocation for common memory and by instantiation for
955 //              algorithms.
956 //   From:      audioStream1Task or equivalent
957 //   Uses:      See code.
958 //   States:    x
959 //   Return:    0 on success.
960 //              Source code line number on PAF_ALG_alloc failure.
961 //              Source code line number on PAF_ALG_mallocMemory failure.
962 //              Source code line number on Decode Chain initialization failure.
963 //              Source code line number on ASP Chain initialization failure.
964 //              Source code line number on Encode Chain initialization failure.
965 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
966 //              * State information as per parent.
967 //              * Memory allocation errors.
968 //
970 #include <pafsio_ialg.h>
972 Int
973 PAF_ASDT_initPhaseCommon(
974     const PAF_ASDT_Params *pP, 
975     const PAF_ASDT_Patchs *pQ, 
976     PAF_ASDT_Config *pC
979     PAF_AST_Config *pAstCfg;
980     Int as;                         /* Audio Stream Number (1, 2, etc.) */
981     Int z;                          /* stream counter */
982     //Int g;                        /* gear */
983     ACP_Handle acp;
984     PAF_IALG_Config pafAlgConfig;
985     IALG_MemRec common[3][PAF_IALG_COMMON_MEMN+1];
986    
987     acp = pC->acp; // get acp handle
988     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
989     as = pAstCfg->as;
990     
991     TRACE_TERSE0("PAF_ASDT_initPhaseCommon: initialization phase - Common Memory");
993     //
994     // Determine memory needs and instantiate algorithms across audio streams
995     //
997     TRACE_TERSE0("PAF_ASDT_initPhaseCommon: calling PAF_ALG_setup.");
998     PAF_ALG_setup(&pafAlgConfig, 
999         HEAP_ID_INTERNAL,       HEAP_INTERNAL, 
1000         HEAP_ID_INTERNAL1,      HEAP_INTERNAL1, 
1001         HEAP_ID_EXTERNAL,       HEAP_EXTERNAL, 
1002         HEAP_ID_INTERNAL1_SHM,  HEAP_INTERNAL1_SHM, 
1003         HEAP_ID_EXTERNAL_SHM,   HEAP_EXTERNAL_SHM, 
1004         HEAP_CLEAR);
1006     if (pP->fxns->headerPrint)
1007         pP->fxns->headerPrint();
1009     for (z=STREAM1; z < STREAMN; z++) 
1010     {
1011         //Int zD, zE, zX;
1012         Int zD, zX;
1014         TRACE_TERSE1("PAF_ASDT_initPhaseCommon: AS%d: initialization phase - Common Algorithms", as+z);
1016         //
1017         // Determine common memory needs of Decode Algorithms
1018         //
1019         PAF_ALG_init (common[z], lengthof (common[z]), COMMONSPACE);
1021         zD = -1;
1022         for (zX = DECODE1; zX < DECODEN; zX++) 
1023         {
1024             if (pP->streamsFromDecodes[zX] == z) 
1025             {
1026                 zD = zX;
1027                 break;
1028             }
1029         }
1031         if (zD >= 0) 
1032         {
1033             TRACE_TERSE1("PAF_ASDT_initPhaseCommon: calling PAF_ALG_ALLOC for decoder common[%d].", z);
1034             if (PAF_ALG_ALLOC (decLinkInit[zD-DECODE1], common[z])) 
1035             {
1036                 TRACE_TERSE3("AS%d: %s.%d: PAF_ALG_alloc failed", as+z, (IArg)__FUNCTION__, __LINE__);
1037                 TRACE_TERSE2("Failed to alloc %d bytes from space %d", common[z]->size, common[z]->space);
1039                 SW_BREAKPOINT;
1040                 return __LINE__;
1041             }
1042             TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1043             if(pP->fxns->allocPrint)
1044                 pP->fxns->allocPrint ((const PAF_ALG_AllocInit *)(decLinkInit[z-DECODE1]),sizeof (*(decLinkInit[z-DECODE1])), &pafAlgConfig);
1045         }
1047 #if 0 // FL: master
1048         TRACE_TERSE3("%s.%d: calling PAF_ALG_ALLOC for stream common[%d].", (IArg)__FUNCTION__, __LINE__, z);
1049         TRACE_TERSE3("%s.%d: calling PAF_ALG_ALLOC for stream common[%d].", (IArg)__FUNCTION__, __LINE__, z);
1050         if (PAF_ALG_ALLOC (aspLinkInit[z-STREAM1][0], common[z])) 
1051         {
1052             TRACE_TERSE3("AS%d: %s.%d: PAF_ALG_alloc failed", as+z, (IArg)__FUNCTION__, __LINE__);
1053             TRACE_TERSE2("Failed to alloc %d bytes from space %d ", common[z]->size, common[z]->space);
1054             SW_BREAKPOINT;
1055             return __LINE__;
1056         }
1057         TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1058         if(pP->fxns->allocPrint)
1059             pP->fxns->allocPrint((const PAF_ALG_AllocInit *)(aspLinkInit[z-STREAM1][0]), sizeof (*(aspLinkInit[z-STREAM1][0])), &pafAlgConfig);
1060 #endif
1061         
1062     }
1063     {
1064         // Changes made to share scratch between zones
1065         // Assume maximum 3 zones and scratch common memory is at offset 0;
1066         int max=0;
1067         for (z=STREAM1; z < STREAMN; z++)
1068         {
1069             if (max<common[z][0].size)
1070                 max=common[z][0].size;
1071         }
1072         common[STREAM1][0].size=max;
1073         for (z=STREAM1+1; z < STREAMN; z++)
1074             common[z][0].size=0;
1075     }
1076     
1077     //
1078     // Provide common memory needs of Decode Algorithms
1079     //
1080     for (z=STREAM1; z < STREAMN; z++) 
1081     {
1082         //Int zD, zE, zX;
1083         Int zD, zX;
1085         zD = -1;        
1086         for (zX = DECODE1; zX < DECODEN; zX++) 
1087         {
1088            if (pP->streamsFromDecodes[zX] == z) 
1089            {
1090                zD = zX;
1091                break;
1092            }
1093         }
1095         TRACE_TERSE0("PAF_ASDT_initPhaseCommon: calling PAF_ALG_mallocMemory for common space.");
1096         if (PAF_ALG_mallocMemory (common[z], &pafAlgConfig)) 
1097         {
1098             TRACE_TERSE3("AS%d: %s.%d: PAF_ALG_mallocMemory failed", as+z, (IArg)__FUNCTION__, __LINE__);
1099             TRACE_TERSE3("AS%d: z: %d.  Size 0x%x", as+z, z, common[z][0].size);
1100             SW_BREAKPOINT;
1101             return __LINE__;
1102         }
1103         TRACE_TERSE3("alloced %d bytes from space %d at 0x%x", common[z]->size, common[z]->space, (IArg)common[z]->base);
1104         // share zone0 scratch with all zones 
1105         common[z][0].base=common[0][0].base;
1106         if (pP->fxns->commonPrint)
1107                 pP->fxns->commonPrint (common[z], &pafAlgConfig);
1109         //
1110         // Instantiate Decode Algorithms
1111         //
1112         if (zD >= 0) 
1113         {
1114             PAF_ASP_Chain *chain;
1115             TRACE_TERSE0("PAF_ASDT_initPhaseCommon: calling PAF_ASP_chainInit for decode.");
1116             chain =
1117                 PAF_ASP_chainInit (&pAstCfg->xDec[zD].decChainData, pP->pChainFxns,
1118                                    HEAP_INTERNAL, as+z, acp, &trace,
1119                                    decLinkInit[zD-DECODE1], NULL, common[z], &pafAlgConfig);
1120             if (!chain) 
1121             {
1122                 TRACE_TERSE1("PAF_ASDT_initPhaseCommon: AS%d: Decode chain initialization failed", as+z);
1123                 return __LINE__;
1124             }
1125         }
1127 #if 0 // FL: master
1128         pC->xStr[z].aspChain[0] = NULL;
1129         for (g=0; g < GEARS; g++) 
1130         {
1131             PAF_ASP_Chain *chain;
1132             TRACE_TERSE2("%s.%d: calling PAF_ASP_chainInit for ASPs.", (IArg)__FUNCTION__, __LINE__);
1133             chain =
1134                 PAF_ASP_chainInit (&pC->xStr[z].aspChainData[g], pP->pChainFxns,
1135                                    HEAP_INTERNAL, as+z, acp, &trace,
1136                                    aspLinkInit[z-STREAM1][g], pC->xStr[z].aspChain[0], common[z], &pafAlgConfig);
1137             if (! chain) 
1138             {
1139                 TRACE_TERSE2("AS%d: ASP chain %d initialization failed", as+z, g);
1140                 return __LINE__;
1141             }
1142             else
1143                 pC->xStr[z].aspChain[g] = chain;
1144         }
1145 #endif
1147     }
1148     TRACE_TERSE1("PAF_ASDT_initPhaseCommon: AS%d: Returning complete.", as+z);
1150     return 0;
1151 } //PAF_ASDT_initPhaseCommon
1153 // -----------------------------------------------------------------------------
1154 // AST Initialization Function - Algorithm Keys
1155 //
1156 //   Name:      PAF_AST_initPhaseAlgKey
1157 //   Purpose:   Audio Stream Task Function for initialization of data values
1158 //              from parameters for Algorithm Keys.
1159 //   From:      audioStream1Task or equivalent
1160 //   Uses:      See code.
1161 //   States:    x
1162 //   Return:    0.
1163 //   Trace:     Message Log "trace" in Debug Project Configuration reports:
1164 //              * State information as per parent.
1165 //
1166 // .............................................................................
1167 Int
1168 PAF_ASDT_initPhaseAlgKey(
1169     const PAF_ASDT_Params *pP, 
1170     const PAF_ASDT_Patchs *pQ, 
1171     PAF_ASDT_Config *pC
1174     PAF_AST_Config *pAstCfg;
1175     Int as;                     /* Audio Stream Number (1, 2, etc.) */
1176     Int z;                      /* decode/encode counter */
1177     Int s;                      /* key number */
1178     PAF_ASP_Link *that;
1180     (void)as;  // clear warning.
1182     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1183     as = pAstCfg->as;
1184     
1185     TRACE_VERBOSE1("PAF_ASDT_initPhaseAlgKey: AS%d: initialization phase - Algorithm Keys", as);
1187     for (z=DECODE1; z < DECODEN; z++) 
1188     {
1189         for (s=0; s < pP->pDecAlgKey->length; s++) 
1190         {
1191             if ((pP->pDecAlgKey->code[s].full != 0)
1192                 && (that = PAF_ASP_chainFind (&pAstCfg->xDec[z].decChainData, pP->pDecAlgKey->code[s])))
1193             {
1194                 pAstCfg->xDec[z].decAlg[s] = (ALG_Handle )that->alg;
1195                     /* Cast in interface, for now --Kurt */
1196             }
1197             else
1198                 pAstCfg->xDec[z].decAlg[s] = NULL;
1199         }
1200     }
1202     return 0;
1203 } //PAF_AST_initPhaseAlgKey
1205 //   Purpose:   Audio Stream Decode Task Function for initialization of the Audio
1206 //              Frame(s) by memory allocation and loading of data pointers
1207 //              and values.
1208 Int
1209 PAF_ASDT_initFrame0(
1210     const PAF_ASDT_Params *pP, 
1211     const PAF_ASDT_Patchs *pQ, 
1212     PAF_ASDT_Config *pC, 
1213     Int z
1216     PAF_AST_Config *pAstCfg;
1217     Int as;                    /* Audio Stream Number (1, 2, etc.) */
1218     Int aLen;
1219     Int aSize = sizeof(PAF_AudioData);
1220     Int aAlign = aSize < sizeof (int) ? sizeof (int) : aSize;
1221     Int maxFrameLength = pP->maxFramelength;
1222     PAF_AudioData *aBuf=NULL;
1223     XDAS_UInt8 *metadataBuf;
1224     Error_Block    eb;
1225     PAF_AudioFrame *pAudioFrame;
1226     Int i;
1228     pAstCfg = pC->pAstCfg; // get pointer to common (shared) configuration
1229     as = pAstCfg->as;
1231     pAudioFrame = &gpDecAudioFrame[z];
1232     if (pAudioFrame == NULL)
1233     {
1234         SW_BREAKPOINT;
1235     }
1236     
1237     // Initialize error block
1238     Error_init(&eb); 
1239     
1240     //maxFrameLength += PA_MODULO - maxFrameLength % PA_MODULO; // compute maximum framelength (needed for ARC support)
1241     //aLen = numchan[z] * maxFrameLength;
1242     aLen = numchan[z] * maxFrameLength + (maxFrameLength - FRAMELENGTH); /* Qin - need additional memory for starting offset
1243                                                                                       See line 1307 */
1245     //
1246     // Initialize audio frame elements directly
1247     //
1248     pAudioFrame->fxns = pP->pAudioFrameFunctions;
1249     pAudioFrame->data.nChannels = PAF_MAXNUMCHAN;
1250     pAudioFrame->data.nSamples = FRAMELENGTH;
1251     pAudioFrame->data.sample = gDecAudioFrameChannelPointers;
1252     pAudioFrame->data.samsiz = gDecAudioFrameChannelSizes;
1253     pAudioFrame->pChannelConfigurationMaskTable = &PAF_ASP_stdCCMT;
1255     //
1256     // Allocate memory for and initialize pointers to audio data buffers
1257     //
1258     //   The NUMCHANMASK is used to identify the channels for which data
1259     //   buffers can be allocated. Using this mask and switch statement
1260     //   rather than some other construct allows efficient code generation,
1261     //   providing just the code necessary (with significant savings).
1262     //
1263     if (pP->fxns->bufMemPrint)
1264     {
1265         pP->fxns->bufMemPrint(z, aLen*aSize, HEAP_ID_FRMBUF, 2);
1266     }
1268     TRACE_TERSE1("PAF_ASDT_initFrame0: AS%d: Memory_calloc for audio buffers", as+z);
1269     
1270     if (aLen != 0)
1271     {
1272         if (!(aBuf = (PAF_AudioData *)Memory_calloc((IHeap_Handle)HEAP_FRMBUF, aLen*aSize, aAlign, &eb)))
1273         {
1274             TRACE_TERSE1("PAF_ASDT_initFrame0: AS%d: Memory_calloc failed", as+z);
1275             TRACE_TERSE2("  maxFrameLength: %d.  aLen*aSize: %d", maxFrameLength, aLen*aSize);
1276             SW_BREAKPOINT;
1277             return __LINE__;
1278         }
1279     }
1281     TRACE_TERSE3("  maxFrameLength: %d.  aLen*aSize: %d.  aBuf: 0x%x", maxFrameLength, aLen*aSize, (IArg)aBuf);
1283     TRACE_TERSE1("PAF_ASDT_initFrame0: AS%d: Memory_calloc for metadata buffers", as+z);
1284     if (!(metadataBuf = (XDAS_UInt8 *)Memory_calloc((IHeap_Handle)HEAP_MDBUF, pP->pMetadataBufStatus->bufSize*pP->pMetadataBufStatus->NumBuf, pP->pMetadataBufStatus->alignment, &eb)))
1285     {
1286         TRACE_TERSE1("PAF_ASDT_initFrame0: AS%d: Memory_calloc failed", as+z);
1287         TRACE_TERSE1("  bufSize*NumBuf: %d", pP->pMetadataBufStatus->bufSize*pP->pMetadataBufStatus->NumBuf);
1288         SW_BREAKPOINT;
1289         return __LINE__;
1290     }
1292     for (i=0; i < PAF_MAXNUMCHAN_AF; i++)
1293     {
1294         gDecAudioFrameChannelPointers[i] = NULL;
1295     }
1297     if ((numchan[z] > PAF_MAXNUMCHAN) || (numchan[z] < 1)) 
1298     {
1299         TRACE_TERSE1("PAF_ASDT_initFrame0: AS%d: unsupported option", as+z);
1300         return __LINE__;
1301     }
1302     else 
1303     {
1304         Int j = 0;
1305         TRACE_TERSE1("PAF_ASDT_initFrame0: AFChanPtrMap[%d][i]", numchan[z]);
1306         for (i=0; i<numchan[z]; i++)
1307         {
1308             Int8 chan = AFChanPtrMap[numchan[z]][i];
1309             if (chan != -1)
1310             {
1311                 gDecAudioFrameChannelPointers[chan] = aBuf + maxFrameLength*(j+1) - FRAMELENGTH;
1312                 j++;
1313                 TRACE_TERSE3("PAF_ASDT_initFrame0: chan = %d = AFChanPtrMap[%d][%d].", chan, numchan[z], i);
1314                 TRACE_TERSE2("PAF_ASDT_initFrame0: audioFrameChannelPointers[%d]: 0x%x", chan, (IArg)gDecAudioFrameChannelPointers[chan]);
1315             }
1316         }
1317     }
1319     // Initialize original audio frame channel pointers
1320     for (i=PAF_LEFT; i < PAF_MAXNUMCHAN_AF; i++) 
1321     {
1322         if (gDecAudioFrameChannelPointers[i])
1323         {
1324             gDecOrigAudioFrameChannelPointers[i] = gDecAudioFrameChannelPointers[i];
1325         }
1326     }
1328     //
1329     // Initialize meta data elements
1330     //
1331     pAudioFrame->pafBsMetadataUpdate = XDAS_FALSE;
1332     pAudioFrame->numPrivateMetadata = 0;
1333     pAudioFrame->bsMetadata_offset = 0;
1334     pAudioFrame->bsMetadata_type = PAF_bsMetadata_channelData;
1335     pAudioFrame->privateMetadataBufSize = pP->pMetadataBufStatus->bufSize;
1336     for (i=0; i<pP->pMetadataBufStatus->NumBuf; i++)
1337     {
1338         pAudioFrame->pafPrivateMetadata[i].offset = 0;
1339         pAudioFrame->pafPrivateMetadata[i].size = 0;
1340         pAudioFrame->pafPrivateMetadata[i].pMdBuf = metadataBuf + pP->pMetadataBufStatus->bufSize*i;
1341     }
1343     return 0;
1344 } //PAF_ASDT_initFrame0
1346 // -----------------------------------------------------------------------------
1347 // ASOT Initialization Function Helper - Reinitialization of Audio Frame
1348 // AST Decoding Function              - Reinitialization of Audio Frame
1349 //
1350 //   Name:      PAF_ASOT_initFrame1
1351 //   Purpose:   Audio Stream Task Function for initialization or reinitiali-
1352 //              zation of the Audio Frame(s) by loading of data values of a
1353 //              time-varying nature.
1354 //   From:      audioStream1Task or equivalent
1355 //              AST Parameter Function -> decodeInfo
1356 //              AST Parameter Function -> decodeDecode
1357 //   Uses:      See code.
1358 //   States:    x
1359 //   Return:    0.
1360 //   Trace:     None.
1361 //
1362 Int
1363 PAF_ASDT_initFrame1(
1364     const PAF_ASDT_Params *pP, 
1365     const PAF_ASDT_Patchs *pQ, 
1366     PAF_ASDT_Config *pC, 
1367     Int z, 
1368     Int apply
1371     PAF_AudioFrame *pAudioFrame;
1373     //
1374     // Reinitialize audio frame elements:
1375     //
1376     //   Channel Configurations during sys init                 = Unknown
1377     //      "          "        during info or decode           = None
1378     //
1379     //   Sample Rate / Count    during sys init, info or decode = Unknown / 0
1380     //
1382     pAudioFrame = &gpDecAudioFrame[z];
1384     if (apply < 0) 
1385     {
1386         pAudioFrame->channelConfigurationRequest.legacy = PAF_CC_UNKNOWN;
1387         pAudioFrame->channelConfigurationStream.legacy = PAF_CC_UNKNOWN;
1388     }
1389     else 
1390     {
1391         pAudioFrame->channelConfigurationRequest.legacy = PAF_CC_NONE;
1392         pAudioFrame->channelConfigurationStream.legacy = PAF_CC_NONE;
1393     }
1395     if (apply < 1) 
1396     {
1397         pAudioFrame->sampleRate = PAF_SAMPLERATE_UNKNOWN;
1398         pAudioFrame->sampleCount = 0;
1399     }
1401     return 0;
1402 } //PAF_ASDT_initFrame1
1404 // Reset AF, invoked during decode ACTIVATE (during state=INIT on Master)
1405 Int resetAf(
1406     const PAF_ASDT_Params *pP, 
1407     Int z, 
1408     Int sourceSelect
1411     PAF_AudioFrame *pAudioFrame;
1412     Int ch;
1413     Int i;
1415     // Get audio frame
1416     pAudioFrame = &gpDecAudioFrame[z];
1417     if (pAudioFrame == NULL)
1418     {
1419         SW_BREAKPOINT;
1420     }
1422     // Reinitialize audio frame elements
1423     pAudioFrame->channelConfigurationRequest.legacy = PAF_CC_NONE;
1424     pAudioFrame->channelConfigurationStream.legacy = PAF_CC_NONE;
1425     pAudioFrame->sampleRate = PAF_SAMPLERATE_UNKNOWN;
1426     pAudioFrame->sampleCount = 0;
1427     pAudioFrame->data.nChannels = PAF_MAXNUMCHAN;
1428     
1429     switch (sourceSelect)
1430     {
1431         case PAF_SOURCE_PCM:
1432             pAudioFrame->data.nSamples = FRAMELENGTH;
1433             break;
1434         case PAF_SOURCE_AC3:
1435         case PAF_SOURCE_DDP:
1436             pAudioFrame->data.nSamples = 1536;
1437             break;
1438         case PAF_SOURCE_THD:
1439             pAudioFrame->data.nSamples = 5120; //QIN FIX ME
1440             break;
1441         default:  
1442             pAudioFrame->data.nSamples = FRAMELENGTH;
1443             break;
1444     }
1446     // Reset audio frame channel pointers
1447     for (ch=PAF_LEFT; ch < PAF_MAXNUMCHAN_AF; ch++) 
1448     {
1449         if (gDecAudioFrameChannelPointers[ch])
1450         {
1451             gDecAudioFrameChannelPointers[ch] = gDecOrigAudioFrameChannelPointers[ch];
1452         }
1453     }
1454     
1455     // Reset audio frame meta data elements
1456     pAudioFrame->pafBsMetadataUpdate = XDAS_FALSE;
1457     pAudioFrame->numPrivateMetadata = 0;
1458     pAudioFrame->bsMetadata_offset = 0;
1459     pAudioFrame->bsMetadata_type = PAF_bsMetadata_channelData;
1460     for (i=0; i<pP->pMetadataBufStatus->NumBuf; i++)
1461     {
1462         pAudioFrame->pafPrivateMetadata[i].offset = 0;
1463         pAudioFrame->pafPrivateMetadata[i].size = 0;
1464     }
1465     
1466     return 0;
1469 // Reset AF samsiz, invoked during DECODE
1470 Int resetAfSamsiz(
1471     Int z
1474     PAF_AudioFrame *pAudioFrame;
1475     Int ch;
1476     
1477     // Get audio frame
1478     pAudioFrame = &gpDecAudioFrame[z];
1479     if (pAudioFrame == NULL)
1480     {
1481         SW_BREAKPOINT;
1482     }
1483     
1484     // Clear samsiz for all channels - MID 208.
1485     for (ch=0; ch < PAF_MAXNUMCHAN_AF; ch++) 
1486     {
1487         pAudioFrame->data.samsiz[ch] = 0;
1488     }
1490     return 0;