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 //
38 // Performance Audio Framework Series 3 -- Audio Stream Processing Definitions
39 //
40 //
41 //
43 #include "fwkPort.h"
45 #include <xdc/std.h>
47 //#include <mem.h>
48 #include <xdc/runtime/Log.h>
49 #include <xdc/runtime/Memory.h>
50 #include <xdc/runtime/Error.h>
51 #include <xdc/runtime/IHeap.h>
52 #include <ti/sysbios/heaps/HeapMem.h>
54 //
55 #include "asp0.h"
56 //#include <paf_alg.h>
57 //#include <pafhjt.h>
59 #include <acp.h>
61 #include "logp.h"
63 //
64 // Audio Stream Processing Chain Function - Initialization
65 //
66 // Name: PAF_ASP_chainInit_
67 // Purpose: Initialize an ASP Chain by linking all elements of an
68 // initialization array onto it. If this is the first audio
69 // stream, perform base initialization of the ASP Chain, but
70 // otherwise do not.
71 // From: AST Parameter Chain Function -> chainInit
72 // Uses: AST Parameter Chain Function -> chainLink
73 // States: None.
74 // Return: ASP Chain.
75 // Trace: None.
76 //
78 PAF_ASP_Chain *
79 PAF_ASP_chainInit_ (
80 PAF_ASP_Chain *this,
81 const struct PAF_ASP_ChainFxns *fxns,
82 HeapMem_Handle heap, //int heap,
83 Uns stream,
84 ACP_Handle acp,
85 LOG_Obj *log,
86 const PAF_ASP_LinkInit *pInit,
87 PAF_ASP_Chain *from,
88 IALG_MemRec *common,
89 PAF_IALG_Config *pafConfig)
90 {
91 if (stream) {
92 this->fxns = fxns;
93 this->head = NULL;
94 this->stream = stream;
95 this->acp = acp;
96 this->log = log;
97 }
99 for ( ; pInit && pInit->thisCode.full; pInit++)
100 if (! this->fxns->chainLink (this, heap, pInit, from, common, pafConfig))
101 return NULL;
103 return this;
104 }
106 //
107 // Audio Stream Processing Chain Function - Link
108 //
109 // Name: PAF_ASP_chainLink_
110 // Purpose: Link an element onto an ASP Chain:
111 // (1) Allocate memory for the link, and
112 // (2) For the first gear, create the data for the link, but
113 // (3) For other than the first gear, simply link the data already
114 // created in the previous gear.
115 // From: AST Parameter Chain Function -> chainInit
116 // Uses: See code.
117 // States: None.
118 // Return: Success: ASP Chain.
119 // Failure: Null.
120 // Trace: Message Log "trace" in Debug Project Configuration reports:
121 // * Memory allocation errors.
122 // * Data creation errors.
123 // * Data linking errors.
124 //
127 PAF_ASP_Chain *
128 PAF_ASP_chainLink_ (
129 PAF_ASP_Chain *this,
130 HeapMem_Handle heap, //int heap,
131 const PAF_ASP_LinkInit *pInit,
132 PAF_ASP_Chain *from,
133 IALG_MemRec *common,
134 PAF_IALG_Config *pafConfig)
135 {
136 PAF_ASP_Link *pLink;
137 Error_Block eb;
139 if (! this || ! pInit)
140 return this;
142 // Initialize error block
143 Error_init(&eb);
145 //if (! (pLink = MEM_alloc (heap, sizeof(PAF_ASP_Link), 4))) {
146 if (!(pLink = Memory_alloc((IHeap_Handle)heap, sizeof(PAF_ASP_Link), 4, &eb))) {
147 if (this->log)
148 LOG_printf(this->log, "AS%d: ASP code 0x%08x alloc failure",
149 this->stream, pInit->thisCode.full);
150 return NULL;
151 }
153 if (! from) {
154 if (! this->fxns->linkData(pLink, pInit, this->stream, this->acp,
155 this->log,common, pafConfig)) {
156 if (this->log)
157 LOG_printf(this->log, "AS%d: ASP code 0x%08x link-init failure",
158 this->stream, pInit->thisCode.full);
159 return NULL;
160 }
161 }
162 else {
163 if (! this->fxns->linkCopy(pLink, pInit, from->head)) {
164 if (this->log)
165 LOG_printf(this->log, "AS%d: ASP code 0x%08x link-copy failure",
166 this->stream, pInit->thisCode.full);
167 return NULL;
168 }
169 }
171 {
172 PAF_ASP_Link **ppLink;
173 for (ppLink=&this->head; *ppLink; ppLink=&(*ppLink)->next)
174 if ((*ppLink)->code.full == pInit->linkCode.full)
175 break;
176 pLink->next = *ppLink;
177 *ppLink = pLink;
178 }
180 return this;
181 }
183 //
184 // Audio Stream Processing Chain Function - Find
185 //
186 // Name: PAF_ASP_chainFind_
187 // Purpose: Find an element in an ASP Chain.
188 // From: AST Parameter Chain Function -> chainFind
189 // Uses: None.
190 // States: None.
191 // Return: Success (found): Pointer to element.
192 // Failure (not found): Null.
193 // Trace: None.
194 //
196 PAF_ASP_Link *
197 PAF_ASP_chainFind_ (
198 PAF_ASP_Chain *this,
199 PAF_ASP_AlphaCode code)
200 {
201 PAF_ASP_Link *that = this->head;
203 for ( ; that; that = that->next) {
204 if (that->code.full == code.full)
205 break;
206 }
208 return that;
209 }
211 //
212 // Audio Stream Processing Chain Function - Reset
213 //
214 // Name: PAF_ASP_chainReset_
215 // Purpose: Reset an ASP Chain by invoking for each element:
216 // (1) The algorithm activation function (NOT PERFORMED), and
217 // (2) The algorithm reset function.
218 // From: AST Parameter Chain Function -> chainReset
219 // Uses: See code.
220 // States: None.
221 // Return: 0 on success.
222 // Error number reported by ASP Reset Function on failure.
223 // Trace: Message Log "trace" in Debug Project Configuration reports:
224 // * ASP Reset Function errors.
225 //
227 Int
228 PAF_ASP_chainReset_ (PAF_ASP_Chain *this, PAF_AudioFrame *pAudioFrame)
229 {
230 Int errno;
232 PAF_ASP_Link *pLink;
234 for (pLink=this->head; pLink; pLink=pLink->next) {
235 ASP_Handle alg = pLink->alg;
236 #if 0
237 /* ASP Algorithms are self-activating and not framework-activated */
238 if (((ALG_Handle )alg)->fxns->algActivate)
239 ((ALG_Handle )alg)->fxns->algActivate (((ALG_Handle )alg));
240 #endif
241 if (alg->fxns->reset && (errno = alg->fxns->reset(alg, pAudioFrame))) {
242 if (this->log) {
243 LOG_printf(this->log, "AS%d: ASP code 0x%08x reset error",
244 this->stream, pLink->code.full);
245 LOG_printf(this->log, "AS%d: errno = 0x%08x",
246 this->stream, errno);
247 }
248 return errno;
249 }
250 }
252 return 0;
253 }
255 //
256 // Audio Stream Processing Chain Function - Apply
257 //
258 // Name: PAF_ASP_chainApply_
259 // Purpose: Apply an ASP Chain by invoking for each:
260 // (1) The algorithm apply function.
261 // From: AST Parameter Chain Function -> chainApply
262 // Uses: See code.
263 // States: None.
264 // Return: 0 on success.
265 // Error number reported by ASP Apply Function on failure.
266 // Trace: Message Log "trace" in Debug Project Configuration reports:
267 // * ASP Apply Function errors.
268 //
270 Int
271 PAF_ASP_chainApply_ (PAF_ASP_Chain *this, PAF_AudioFrame *pAudioFrame)
272 {
273 Int errno;
274 int i = 0;
276 PAF_ASP_Link *pLink;
278 for (pLink=this->head; pLink; pLink=pLink->next)
279 {
280 ASP_Handle alg = pLink->alg;
282 i++;
283 if (this->log)
284 {
285 LOG_printf(&trace, "PAF_ASP_chainApply_: AS_%d. link %d. alg: 0x%x.",
286 this->stream, i, alg);
287 }
288 Log_info3("PAF_ASP_chainApply_: AS_%d. link %d. alg: 0x%x.", this->stream, i, (IArg)alg);
290 if (alg->fxns->apply && (errno = alg->fxns->apply(alg, pAudioFrame)))
291 {
292 if (this->log)
293 {
294 LOG_printf(this->log, "AS%d: ASP code 0x%08x apply error 0x%x.",
295 this->stream, pLink->code.full, errno);
296 LOG_printf(&trace, "AS%d: ASP code 0x%08x apply error 0x%x.",
297 this->stream, pLink->code.full, errno);
298 }
299 return errno;
300 }
301 }
303 return 0;
304 }
306 //
307 // Audio Stream Processing Chain Function - Final
308 //
309 // Name: PAF_ASP_chainFinal_
310 // Purpose: Finalize an ASP Chain by invoking for each:
311 // (1) The algorithm final function (NOT PERFORMED), and
312 // (2) The algorithm deactivation function.
313 // From: AST Parameter Chain Function -> chainFinal
314 // Uses: See code.
315 // States: None.
316 // Return: 0 on success.
317 // Error number reported by ASP Final Function on failure.
318 // Trace: Message Log "trace" in Debug Project Configuration reports:
319 // * ASP Final Function errors.
320 //
321 // Note: In Audio Framework #2, PAF_ASP_FINAL is not defined and there
322 // are no Final Functions supplied with ASP Algorithms.
324 Int
325 PAF_ASP_chainFinal_ (PAF_ASP_Chain *this, PAF_AudioFrame *pAudioFrame)
326 {
327 #ifdef PAF_ASP_FINAL
328 Int errno;
329 #endif /* PAF_ASP_FINAL */
331 PAF_ASP_Link *pLink;
333 for (pLink=this->head; pLink; pLink=pLink->next) {
334 ASP_Handle alg = pLink->alg;
335 #ifdef PAF_ASP_FINAL
336 if (alg->fxns->final && (errno = alg->fxns->final(alg, pAudioFrame))) {
337 if (this->log) {
338 LOG_printf(this->log, "AS%d: ASP code 0x%08x final error",
339 this->stream, pLink->code.full);
340 LOG_printf(this->log, "AS%d: errno = 0x%08x",
341 this->stream, errno);
342 }
343 return errno;
344 }
345 #endif /* PAF_ASP_FINAL */
346 #if 1
347 /* ASP Algorithms are self-deactivating and framework-deactivated */
348 if (((ALG_Handle )alg)->fxns->algDeactivate)
349 ((ALG_Handle )alg)->fxns->algDeactivate (((ALG_Handle )alg));
350 #endif
351 }
353 return 0;
354 }
356 //
357 // Audio Stream Processing Chain Function - Link Data
358 //
359 // Name: PAF_ASP_linkData
360 // Purpose: Create the data for a chain link:
361 // (1) Perform beta unit relocation,
362 // (2) Instantiate an algorithm, and
363 // (3) Attach the beta unit(s) of an algorithm to an ACP Algorithm
364 // instance.
365 // From: AST Parameter Chain Function -> linkData
366 // Uses: See code.
367 // States: None.
368 // Return: Null pointer on failure.
369 // Pointer to the chain link on success.
370 // Trace: Message Log "trace" in Debug Project Configuration reports:
371 // * Algorithm instance creation failure message.
372 // * Beta Unit initialization success message.
373 //
375 PAF_ASP_Link *
376 PAF_ASP_linkData (
377 PAF_ASP_Link *this,
378 const PAF_ASP_LinkInit *pInit,
379 Uns stream,
380 ACP_Handle acp,
381 LOG_Obj *log,
382 IALG_MemRec *common,
383 PAF_IALG_Config *pafConfig)
384 {
385 IALG_Status *pStatus;
387 ALG_Handle alg;
389 Int beta = pInit->thisCode.part.beta;
391 if (log)
392 LOG_printf(log, "AS%d: beta 0x%x initialization begins.", stream, beta);
393 {
394 ALG_Handle acpAlg = (ALG_Handle) acp;
395 Int betaPrimeBase;
396 Int betaPrimeOffset;
397 acpAlg->fxns->algControl (acpAlg, ACP_GETBETAPRIMEBASE,
398 (IALG_Status *) &betaPrimeBase);
399 acpAlg->fxns->algControl (acpAlg, ACP_GETBETAPRIMEOFFSET,
400 (IALG_Status *) &betaPrimeOffset);
401 if (beta >= betaPrimeBase)
402 beta += (stream - 1) * betaPrimeOffset;
403 }
405 this->next = NULL;
406 this->code = pInit->thisCode;
408 if (pInit->ialg_fxns) {
410 if (pInit->init_func)
411 (*pInit->init_func)();
413 if (! (alg = PAF_ALG_create(pInit->ialg_fxns,NULL,(IALG_Params *)pInit->ialg_prms,
414 common, pafConfig))) {
415 if (log) {
416 LOG_printf(log,
417 "AS%d: beta 0x%x algorithm instance creation failed",
418 stream, beta);
419 }
420 return NULL;
421 }
422 }
423 else
424 return NULL;
426 this->alg = (ASP_Handle )alg;
428 if (alg->fxns->algControl) {
429 if (! alg->fxns->algControl (alg, ACP_GETSTATUSADDRESS1, (IALG_Status *)&pStatus))
430 acp->fxns->attach (acp, pInit->thisCode.part.series, beta, pStatus);
431 if (! alg->fxns->algControl (alg, ACP_GETSTATUSADDRESS2, (IALG_Status *)&pStatus))
432 acp->fxns->attach (acp, pInit->thisCode.part.series, beta+1, pStatus);
433 }
435 if (log)
436 LOG_printf(log, "AS%d: beta 0x%x processing initialized", stream, beta);
438 return this;
439 }
441 //
442 // Audio Stream Processing Chain Function - Link Copy
443 //
444 // Name: PAF_ASP_linkCopy
445 // Purpose: Copy the data for a chain link:
446 // (1) Find an element on a chain, and
447 // (2) Copy the reference to the data of the link.
448 // From: AST Parameter Chain Function -> linkCopy
449 // Uses: See code.
450 // States: None.
451 // Return: Null pointer on failure.
452 // Pointer to the chain link on success.
453 // Trace: None.
454 //
456 PAF_ASP_Link *
457 PAF_ASP_linkCopy (
458 PAF_ASP_Link *this,
459 const PAF_ASP_LinkInit *pInit,
460 PAF_ASP_Link *from)
461 {
462 for ( ; from; from = from->next) {
463 if (from->code.full == pInit->thisCode.full)
464 break;
465 }
467 if (! from)
468 return NULL;
470 this->next = NULL;
471 this->code = pInit->thisCode;
472 this->alg = from->alg;
474 return this;
475 }
477 //
478 // Audio Stream Processing Chain Functions
479 //
480 // Name: PAF_ASP_chainFxns
481 // Purpose: Collect the chain functions that embody the implementation
482 // of Audio Framework Number 2 for use as a jump table.
483 // From: PAF_AST_Params
484 // Uses: See contents.
485 // States: N.A.
486 // Return: N.A.
487 // Trace: None.
488 //
490 const struct PAF_ASP_ChainFxns PAF_ASP_chainFxns =
491 {
492 PAF_ASP_chainInit_,
493 PAF_ASP_chainLink_,
494 PAF_ASP_chainFind_,
495 {
496 PAF_ASP_chainReset_,
497 PAF_ASP_chainApply_,
498 PAF_ASP_chainFinal_,
499 },
500 PAF_ASP_linkData,
501 PAF_ASP_linkCopy,
502 };