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 // Framework support function (implementation)
38 //
41 #include <xdc/std.h> //<std.h>
42 #include <xdc/runtime/Error.h>
43 #include <xdc/runtime/Log.h>
44 #include <xdc/runtime/Memory.h>
45 #include <ti/sysbios/BIOS.h>
46 #include <ti/sysbios/heaps/HeapMem.h>
47 #include <ti/ipc/MessageQ.h>
48 #include <ti/ipc/MultiProc.h>
50 //#include <ti/procsdk_audio/procsdk_audio_typ.h>
51 #include <procsdk_audio_typ.h>
53 #include <sio.h>
55 #include "as0.h"
57 #include "inpbuf.h"
58 #include <pafdec.h>
59 #include <pafenc.h>
60 #include "outbuf.h"
62 #include <asperr.h> /* ASPERR_*OUT_* */
63 #include <pafsio.h>
64 //#include "pafhjt.h"
66 #include "common.h"
67 #include "paf_heapMgr.h"
68 #include "aspMsg_common.h"
69 #include "aspMsg_master.h"
70 #include "audioStreamProc_common.h"
72 #define PAF_DEVICE_VERSION (PAF_DEVICE & 0xffff)
73 #if PAF_DEVICE_VERSION == 0xE000
74 #define _DEBUG // This is to enable log_printfs
75 #endif /* PAF_DEVICE_VERSION */
76 #include <logp.h>
78 // allows you to set a different trace module in pa.cfg
79 #define TR_MOD trace
81 // Allow a developer to selectively enable tracing.
82 // For release, set mask to 1 to make it easier to catch any errors.
83 #define CURRENT_TRACE_MASK 1 // terse only
85 #define TRACE_MASK_TERSE 1 // only flag errors
86 #define TRACE_MASK_GENERAL 2 // log a half dozen lines per loop
87 #define TRACE_MASK_VERBOSE 4 // trace full operation
89 #if (CURRENT_TRACE_MASK & TRACE_MASK_TERSE)
90 #define TRACE_TERSE(a) LOG_printf a
91 #else
92 #define TRACE_TERSE(a)
93 #endif
95 #if (CURRENT_TRACE_MASK & TRACE_MASK_GENERAL)
96 #define TRACE_GEN(a) LOG_printf a
97 #else
98 #define TRACE_GEN(a)
99 #endif
101 #if (CURRENT_TRACE_MASK & TRACE_MASK_VERBOSE)
102 #define TRACE_VERBOSE(a) LOG_printf a
103 #else
104 #define TRACE_VERBOSE(a)
105 #endif
107 //
108 // Audio Stream Task / Decode Processing - Device Allocation Function
109 //
110 // Name: PAF_DEC_deviceAllocate
111 // Purpose: Decode Function for "device allocation" to
112 // (1) allocate buffer memory, and
113 // (2) initialize a Buffer Configuration, including pointers
114 // to that buffer memory.
115 // From: AST Parameter Function -> deviceAllocate
116 // Uses: None.
117 // States: None.
118 // Return: 0 on success.
119 // 1 on MEM_calloc failure.
120 // Trace: None.
121 //
123 Int
124 PAF_DEC_deviceAllocate(
125 SIO_Handle *pHandle,
126 int mode,
127 int heapID,
128 int bufSize,
129 Ptr pBufCfg )
130 {
131 Ptr pBuf;
132 Error_Block eb;
134 *pHandle = NULL;
136 // Initialize error block
137 Error_init(&eb);
139 //#ifdef _TMS320C6X
140 //#warn Changed back to MEM_calloc until Mantis ID 81 resolved -- mwatson
141 //#endif
142 // if (! (pBuf = (Ptr )MEM_alloc (heapID, bufSize, 128)))
143 //if (! (pBuf = (Ptr )MEM_calloc (heapID, bufSize, 128)))
144 if (!(pBuf = (Ptr )Memory_calloc((IHeap_Handle)pafHeapMgr_readHeapHandle(heapID), bufSize, 128, &eb)))
145 return (SYS_EALLOC);
147 if( mode == SIO_OUTPUT ) {
148 PAF_OutBufConfig *pConfig = (PAF_OutBufConfig *)pBufCfg;
150 pConfig->base.pVoid = pBuf;
151 pConfig->pntr.pVoid = pBuf;
152 pConfig->head.pVoid = pBuf;
153 pConfig->allocation = bufSize;
154 pConfig->precision = 24;
155 }
156 else {
157 PAF_InpBufConfig *pConfig = (PAF_InpBufConfig *)pBufCfg;
159 pConfig->base.pVoid = pBuf;
160 pConfig->pntr.pVoid = pBuf;
161 pConfig->head.pVoid = pBuf;
162 pConfig->allocation = bufSize;
163 pConfig->precision = 16;
164 }
166 return SYS_OK;
167 } /* PAF_DEC_deviceAllocate */
169 //
170 // Audio Stream Task / Decode Processing - Device Selection Function
171 //
172 // Name: PAF_DEC_deviceSelect
173 // Purpose: Decode Function for "device selection" which
174 // (1) "closes" any open device on that handle, and
175 // (2) "opens" a new device on that handle if not None.
176 // From: AST Parameter Function -> deviceSelect
177 // Uses: See code.
178 // States: None.
179 // Return: 0 on success.
180 // 1 on error in device create.
181 // 2 on error in device open.
182 // 3 on error in device close.
183 // 4 on error in device idle.
184 // Trace: None.
185 //
187 Int
188 PAF_DEC_deviceSelect(
189 SIO_Handle *pHandle,
190 int mode,
191 int heapID,
192 Ptr pParams )
193 {
194 // Device must be idled, closed, and freed to select new:
195 if (*pHandle) {
196 if (SIO_idle (*pHandle))
197 return ASPERR_DEVINP_IDLE-ASPERR_DEVINP;
198 if (SIO_ctrl (*pHandle, PAF_SIO_CONTROL_CLOSE, 0))
199 return ASPERR_DEVINP_CLOSE-ASPERR_DEVINP;
200 PAF_SIO_free (*pHandle, mode );
201 /* For consistency, should return integer. --Kurt */
202 }
204 // Select new device:
205 if (! pParams) {
206 /* input/output device None */
207 *pHandle = NULL;
208 }
209 else if (! (*pHandle = PAF_SIO_recreate (pParams, mode, &trace, heapID)))
210 return ASPERR_DEVINP_CREATE-ASPERR_DEVINP;
211 else if (SIO_ctrl (*pHandle, PAF_SIO_CONTROL_OPEN, (Arg) pParams))
212 return ASPERR_DEVINP_OPEN-ASPERR_DEVINP;
214 return 0;
215 } /* PAF_DEC_deviceSelect */
217 //
218 // Audio Stream Task / Decode Processing - Frame Length Computation Function
219 //
220 // Name: PAF_DEC_computeFrameLength
221 // Purpose: Compute frame length to be generated by a decode algorithm,
222 // including effects of the "buffer ratio" which is used to
223 // indicate possible subsequent up-sampling by 2 or 4 (-2 or
224 // -4) or down-sampling by 2 or 4 (2 or 4).
225 // From: AST Parameter Function -> computeFrameLength
226 // Uses: See code.
227 // States: None.
228 // Return: Frame length.
229 // Trace: None.
230 //
232 #include <pcm.h>
233 #if 0 /* commenting IP component header file inclusion */
234 #include <ac3.h>
235 #include <dts.h>
236 #include <aac.h>
238 #define DEC_MINSAMGEN PCM_MINSAMGEN
240 #if DEC_MINSAMGEN != AC3_MINSAMGEN
241 #error internal error
242 #elif DEC_MINSAMGEN != DTS_MINSAMGEN
243 #error internal error
244 #elif DEC_MINSAMGEN != AAC_MINSAMGEN
245 #error internal error
246 #endif /* DEC_MINSAMGEN */
248 #define DEC_MAXSAMGEN PCM_MAXSAMGEN
250 #if DEC_MAXSAMGEN != AC3_MAXSAMGEN
251 #error internal error
252 #elif DEC_MAXSAMGEN != DTS_MAXSAMGEN
253 #error internal error
254 #elif defined (AAC_SUPPORT) && DEC_MAXSAMGEN != AAC_MAXSAMGEN
255 #error internal error
256 #endif /* DEC_MAXSAMGEN */
257 #else
258 #define DEC_MINSAMGEN PCM_MINSAMGEN
259 #define DEC_MAXSAMGEN PCM_MAXSAMGEN
260 #endif
262 // FL: change alg handle to decIdx, index is w.r.t. dec zones
263 //Int
264 //PAF_DEC_computeFrameLength(
265 // ALG_Handle alg,
266 // Int frameLength,
267 // Int bufferRatio
268 //)
269 Int
270 PAF_DEC_computeFrameLength(
271 Int decIdx,
272 Int frameLength,
273 Int bufferRatio
274 )
275 {
276 Int m, n, o, p;
277 // Messaging
278 Int argIdx;
279 Int errno; /* error number */
280 char decMsgBuf[ASP_MSG_BUF_LEN];
282 // FL: dec control message to slave
283 argIdx = 0; // set decIdx
284 *(Int *)&decMsgBuf[argIdx] = decIdx;
285 argIdx += sizeof(Int); // set decCtrlCmd
286 *(IALG_Cmd *)&decMsgBuf[argIdx] = DEC_MINSAMGEN;
287 if(AspMsgSend(ASP_SLAVE_DEC_CONTROL, ASP_MASTER_DEC_CONTROL_DONE,
288 decMsgBuf, decMsgBuf) != ASP_MSG_NO_ERR)
289 {
290 SW_BREAKPOINT;
291 return -1; // temporary
292 }
293 else
294 {
295 argIdx = 0; // get decCtrlRet
296 m = *(Int *)&decMsgBuf[argIdx];
297 TRACE_TERSE1("decCtrlRet (m)=%d", m);
298 }
300 #if 0 // FL: decoder control call, slave
301 m = alg->fxns->algControl(alg, DEC_MINSAMGEN, NULL);
302 #endif
304 // FL: dec control message to slave
305 argIdx = 0; // set decIdx
306 *(Int *)&decMsgBuf[argIdx] = decIdx;
307 argIdx += sizeof(Int); // set decCtrlCmd
308 *(IALG_Cmd *)&decMsgBuf[argIdx] = DEC_MAXSAMGEN;
309 if(AspMsgSend(ASP_SLAVE_DEC_CONTROL, ASP_MASTER_DEC_CONTROL_DONE,
310 decMsgBuf, decMsgBuf) != ASP_MSG_NO_ERR)
311 {
312 SW_BREAKPOINT;
313 return -1; // temporary
314 }
315 else
316 {
317 argIdx = 0; // get decCtrlRet
318 n = *(Int *)&decMsgBuf[argIdx];
319 TRACE_TERSE1("decCtrlRet (n)=%d", n);
320 }
322 #if 0 // FL: decoder control call, slave
323 n = alg->fxns->algControl(alg, DEC_MAXSAMGEN, NULL);
324 #endif
327 if (m != n) {
328 o = n < frameLength ? n : frameLength;
329 o = o / m * m;
330 if (bufferRatio > 0) {
331 if (bufferRatio == 4 && (p = o / 4) > m && p % 8 == 0)
332 return p;
333 else if (bufferRatio == 4 && (p = o / 2) > m && p % 8 == 0)
334 return p;
335 else if (bufferRatio == 2 && (p = o / 2) > m && p % 8 == 0)
336 return p;
337 }
338 else if (bufferRatio < 0) {
339 if (bufferRatio == -4 && (p = o * 4) > m && p % 8 == 0)
340 return p;
341 else if (bufferRatio == -4 && (p = o * 2) > m && p % 8 == 0)
342 return p;
343 else if (bufferRatio == -2 && (p = o * 2) > m && p % 8 == 0)
344 return p;
345 }
346 if (o % 8 == 0)
347 return o;
348 }
350 return m;
351 } /* PAF_DEC_computeFrameLength */
353 //
354 // Audio Stream Task / Decode Processing - Input Status Update
355 //
356 // Name: PAF_DEC_updateInputStatus
357 // Purpose: Decode Function for maintaining Input Status.
358 // From: AST Parameter Function -> updateInputStatus
359 // Uses: See code.
360 // States: None.
361 // Return: 0 on success.
362 // Other on SIO Control failure (using SIO error numbers).
363 // Trace: None.
364 //
366 Int
367 PAF_DEC_updateInputStatus(
368 SIO_Handle hSio,
369 PAF_InpBufStatus *pStatus,
370 PAF_InpBufConfig *pConfig)
371 {
372 Int errno;
375 PAF_SIO_InputStatus inputStatus;
377 // initialize all values to unknown so that device specific
378 // driver layer need only fill in those entries that it is aware of.
379 // This allows extensibility of the structure without requiring users
380 // to re-code.
381 inputStatus.lock = 0;
382 inputStatus.sampleRateData = PAF_SAMPLERATE_UNKNOWN;
383 inputStatus.sampleRateMeasured = PAF_SAMPLERATE_UNKNOWN;
384 inputStatus.nonaudio = PAF_IEC_AUDIOMODE_UNKNOWN;
385 inputStatus.emphasis = PAF_IEC_PREEMPHASIS_UNKNOWN;
387 errno = SIO_ctrl (hSio, (Uns)PAF_SIO_CONTROL_GET_INPUT_STATUS, (Arg) &inputStatus);
388 if (errno)
389 return errno;
390 pStatus->sampleRateData = inputStatus.sampleRateData;
391 pStatus->sampleRateMeasured = inputStatus.sampleRateMeasured;
392 pStatus->nonaudio = inputStatus.nonaudio;
393 pStatus->emphasisData = inputStatus.emphasis;
395 // if MSB of override clear then use as reported lock
396 // if = 0x80 then use default [0x81]
397 // if = 0x81 then use measured (from device)
398 // others not defined or implemented
399 if ((pStatus->lockOverride & (XDAS_Int8)0x80) == 0)
400 pStatus->lock = pStatus->lockOverride;
401 else if (pStatus->lockOverride == (XDAS_Int8)0x80)
402 pStatus->lock = inputStatus.lock;
403 else if (pStatus->lockOverride == (XDAS_Int8)0x81)
404 pStatus->lock = inputStatus.lock;
406 // if MSB of override clear then use it as sample rate for system,
407 // if = 0x80 then use default [0x82]
408 // if = 0x81 then use data
409 // if = 0x82 then use measured
410 // others not defined or implemented
411 if ((pStatus->sampleRateOverride & (XDAS_Int8)0x80) == 0)
412 pStatus->sampleRateStatus = pStatus->sampleRateOverride;
413 else if (pStatus->sampleRateOverride == (XDAS_Int8)0x80)
414 pStatus->sampleRateStatus = pStatus->sampleRateMeasured;
415 else if (pStatus->sampleRateOverride == (XDAS_Int8)0x81)
416 pStatus->sampleRateStatus = pStatus->sampleRateData;
417 else if (pStatus->sampleRateOverride == (XDAS_Int8)0x82)
418 pStatus->sampleRateStatus = pStatus->sampleRateMeasured;
420 // Update emphasis status:
421 if ((pStatus->emphasisOverride & (XDAS_Int8)0x80) == 0) {
422 if (pStatus->emphasisData == PAF_IEC_PREEMPHASIS_YES)
423 pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_YES;
424 else
425 pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_NO;
426 }
427 else if (pStatus->emphasisOverride ==
428 (XDAS_Int8 )(0x80+PAF_IEC_PREEMPHASIS_YES))
429 pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_YES;
430 else /* IBEmphasisOverrideNo or other */
431 pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_NO;
433 // Update precision control
434 pConfig->precision = pStatus->precisionInput =
435 pStatus->precisionOverride < 0
436 ? pStatus->precisionDefault
437 : pStatus->precisionOverride > 0
438 ? pStatus->precisionOverride
439 : pStatus->precisionDetect > 0
440 ? pStatus->precisionDetect
441 : pStatus->precisionDefault;
443 return 0;
445 } /* PAF_DEC_updateInputStatus */
447 // ----------------------------------------------------------------------------
449 #ifndef HSE
451 // ............................................................................
453 //
454 // Audio Stream Task / Asynchronous Rate Conversion (ARC) - Control
455 //
456 // Name: PAF_ARC_controlRate
457 // Purpose: Control ARC conversion rate
458 // From: AST Parameter Function -> controlRate
459 // Uses: See code.
460 // States: None.
461 // Return: 0 on success.
462 // Other on SIO Control failure (using SIO error numbers).
463 // Trace: None.
464 //
466 #include <math.h> /* ldexp() */
468 #include <acp.h>
470 #include <arc_a.h>
472 Int
473 PAF_ARC_controlRate(
474 SIO_Handle hRxSio,
475 SIO_Handle hTxSio,
476 ACP_Handle acp,
477 double arcRatio) // KR032013
478 {
479 Int errno;
482 if (hRxSio && hTxSio) {
483 PAF_SIO_Stats *pRxStats, *pTxStats;
484 //extern double arcRatio; // output / input rate // KR032013
485 XDAS_UInt32 inputsPerOutputQ24 = (XDAS_UInt32) ldexp( arcRatio, 24);
487 static const ACP_Unit
488 readARCOutputsRemainingQ24_s[] = { readARCOutputsRemainingQ24 },
489 wroteARCInputsPerOutputQ24_s[] = { wroteARCInputsPerOutputQ24 };
491 ACP_Unit y4[4];
494 if (errno = SIO_ctrl (hRxSio, PAF_SIO_CONTROL_GET_STATS, (Arg) &pRxStats)) {
495 TRACE_TERSE ((&TR_MOD, "ARC: Error retrieving Rx xfer stats"
496 " (0x%04x)", errno));
497 return errno;
498 }
500 if (errno = SIO_ctrl (hTxSio, PAF_SIO_CONTROL_GET_STATS, (Arg) &pTxStats)) {
501 TRACE_TERSE ((&TR_MOD, "ARC: Error retrieving Tx xfer stats"
502 " (0x%04x)", errno));
503 return errno;
504 }
506 y4[0] = wroteARCInputsPerOutputQ24_s[0];
507 y4[1] = wroteARCInputsPerOutputQ24_s[1];
508 y4[2] = (MdInt) inputsPerOutputQ24;
509 y4[3] = (MdInt)(inputsPerOutputQ24 >> 16);
511 if (errno = acp->fxns->apply (acp, y4, NULL)) {
512 TRACE_TERSE ((&TR_MOD, "ARC: Error sending ARC rate ratio"
513 " (0x%04x)", errno));
514 return errno;
515 }
517 if (errno = acp->fxns->apply (acp, readARCOutputsRemainingQ24_s, y4)) {
518 TRACE_TERSE ((&TR_MOD, "ARC: Error retrieving ARC timing"
519 " (0x%04x)", errno));
520 return errno;
521 }
523 {
524 static XDAS_UInt32 outputsRemainingQ24[2];
526 const double arcDiff = pRxStats->dpll.v
527 - (pTxStats->dpll.v + ldexp( outputsRemainingQ24[1], -24) * pTxStats->dpll.dv);
528 outputsRemainingQ24[1] = outputsRemainingQ24[0];
529 outputsRemainingQ24[0] = ((XDAS_UInt32) y4[3] << 16) + y4[2];
531 TRACE_GEN ((&TR_MOD, "time dif %d.%08d",
532 (int) arcDiff, (int) (1.e8 * (arcDiff - (int) arcDiff))));
534 }
535 }
537 return 0;
538 }
540 // ............................................................................
541 #else
542 Int
543 PAF_ARC_controlRate(
544 SIO_Handle hRxSio,
545 SIO_Handle hTxSio,
546 ACP_Handle acp,
547 double arcRatio) // KR032013
548 {
549 return 1;
550 }
551 #endif //HSE
553 // ----------------------------------------------------------------------------
555 //
556 // Audio Stream Task / Pass Processing - Buffer Copy
557 //
558 // Name: PAF_BUF_copy
559 // Purpose: Decode Function for copying the data in the input buffer
560 // to the output buffer.
561 // From: AST Parameter Function -> copy
562 // Uses: None.
563 // States: None.
564 // Return: 0 on success.
565 // 0x80000000 for errors in required arguments.
566 // Trace: None.
567 //
569 #define min(a, b) (((a) < (b)) ? (a) : (b))
571 #include <pafdec.h>
572 #include <pafenc.h>
574 /* DO NOT REMOVE THIS CODE_SECTION. --Kurt */
575 #pragma CODE_SECTION(PAF_BUF_copy,".text:_PAF_BUF_copy")
577 Int
578 PAF_BUF_copy(
579 Uns inpChan,
580 PAF_InpBufConfig *pInpCfg,
581 Uns outChan,
582 PAF_OutBufConfig *pOutCfg)
583 {
584 Int i;
585 Int *pInBuf, *pOutBuf;
586 Int numSamples;
588 #ifndef __TI_EABI__
589 asm (" .clink");
590 #endif
592 if( (pInpCfg == NULL) || (pOutCfg == NULL) )
593 return ASPERR_UNSPECIFIED;
595 if( (inpChan > pInpCfg->stride) || (outChan > pOutCfg->stride) )
596 return ASPERR_UNSPECIFIED;
598 //for now assume =32bit words
599 if( (pInpCfg->sizeofElement != 4) ||
600 (pInpCfg->sizeofElement != pOutCfg->sizeofElement) )
601 return ASPERR_UNSPECIFIED;
603 pInBuf = pInpCfg->pntr.pLgInt;
604 pOutBuf = pOutCfg->pntr.pLgInt;
606 numSamples = min(pInpCfg->frameLength, pOutCfg->lengthofFrame);
607 pInBuf += inpChan;
608 pOutBuf += outChan;
609 for( i=0; i < numSamples; i++ )
610 {
611 *pOutBuf = *pInBuf;
612 pInBuf += pInpCfg->stride;
613 pOutBuf += pOutCfg->stride;
614 }
616 return 0;
617 } /* PAF_BUF_copy */
619 // EOF