2 /*
3 Copyright (c) 2017, Texas Instruments Incorporated - http://www.ti.com/
4 All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 //
37 // 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 char decMsgBuf[ASP_MSG_BUF_LEN];
281 // FL: dec control message to slave
282 argIdx = 0; // set decIdx
283 *(Int *)&decMsgBuf[argIdx] = decIdx;
284 argIdx += sizeof(Int); // set decCtrlCmd
285 *(IALG_Cmd *)&decMsgBuf[argIdx] = DEC_MINSAMGEN;
286 if(AspMsgSend(ASP_SLAVE_DEC_CONTROL, ASP_MASTER_DEC_CONTROL_DONE,
287 decMsgBuf, decMsgBuf) != ASP_MSG_NO_ERR)
288 {
289 SW_BREAKPOINT;
290 return -1; // temporary
291 }
292 else
293 {
294 argIdx = 0; // get decCtrlRet
295 m = *(Int *)&decMsgBuf[argIdx];
296 TRACE_TERSE1("decCtrlRet (m)=%d", m);
297 }
299 #if 0 // FL: decoder control call, slave
300 m = alg->fxns->algControl(alg, DEC_MINSAMGEN, NULL);
301 #endif
303 // FL: dec control message to slave
304 argIdx = 0; // set decIdx
305 *(Int *)&decMsgBuf[argIdx] = decIdx;
306 argIdx += sizeof(Int); // set decCtrlCmd
307 *(IALG_Cmd *)&decMsgBuf[argIdx] = DEC_MAXSAMGEN;
308 if(AspMsgSend(ASP_SLAVE_DEC_CONTROL, ASP_MASTER_DEC_CONTROL_DONE,
309 decMsgBuf, decMsgBuf) != ASP_MSG_NO_ERR)
310 {
311 SW_BREAKPOINT;
312 return -1; // temporary
313 }
314 else
315 {
316 argIdx = 0; // get decCtrlRet
317 n = *(Int *)&decMsgBuf[argIdx];
318 TRACE_TERSE1("decCtrlRet (n)=%d", n);
319 }
321 #if 0 // FL: decoder control call, slave
322 n = alg->fxns->algControl(alg, DEC_MAXSAMGEN, NULL);
323 #endif
326 if (m != n) {
327 o = n < frameLength ? n : frameLength;
328 o = o / m * m;
329 if (bufferRatio > 0) {
330 if (bufferRatio == 4 && (p = o / 4) > m && p % 8 == 0)
331 return p;
332 else if (bufferRatio == 4 && (p = o / 2) > m && p % 8 == 0)
333 return p;
334 else if (bufferRatio == 2 && (p = o / 2) > m && p % 8 == 0)
335 return p;
336 }
337 else if (bufferRatio < 0) {
338 if (bufferRatio == -4 && (p = o * 4) > m && p % 8 == 0)
339 return p;
340 else if (bufferRatio == -4 && (p = o * 2) > m && p % 8 == 0)
341 return p;
342 else if (bufferRatio == -2 && (p = o * 2) > m && p % 8 == 0)
343 return p;
344 }
345 if (o % 8 == 0)
346 return o;
347 }
349 return m;
350 } /* PAF_DEC_computeFrameLength */
352 //
353 // Audio Stream Task / Decode Processing - Input Status Update
354 //
355 // Name: PAF_DEC_updateInputStatus
356 // Purpose: Decode Function for maintaining Input Status.
357 // From: AST Parameter Function -> updateInputStatus
358 // Uses: See code.
359 // States: None.
360 // Return: 0 on success.
361 // Other on SIO Control failure (using SIO error numbers).
362 // Trace: None.
363 //
365 Int
366 PAF_DEC_updateInputStatus(
367 SIO_Handle hSio,
368 PAF_InpBufStatus *pStatus,
369 PAF_InpBufConfig *pConfig)
370 {
371 Int errno;
374 PAF_SIO_InputStatus inputStatus;
376 // initialize all values to unknown so that device specific
377 // driver layer need only fill in those entries that it is aware of.
378 // This allows extensibility of the structure without requiring users
379 // to re-code.
380 inputStatus.lock = 0;
381 inputStatus.sampleRateData = PAF_SAMPLERATE_UNKNOWN;
382 inputStatus.sampleRateMeasured = PAF_SAMPLERATE_UNKNOWN;
383 inputStatus.nonaudio = PAF_IEC_AUDIOMODE_UNKNOWN;
384 inputStatus.emphasis = PAF_IEC_PREEMPHASIS_UNKNOWN;
386 errno = SIO_ctrl (hSio, (Uns)PAF_SIO_CONTROL_GET_INPUT_STATUS, (Arg) &inputStatus);
387 if (errno)
388 return errno;
389 pStatus->sampleRateData = inputStatus.sampleRateData;
390 pStatus->sampleRateMeasured = inputStatus.sampleRateMeasured;
391 pStatus->nonaudio = inputStatus.nonaudio;
392 pStatus->emphasisData = inputStatus.emphasis;
394 // if MSB of override clear then use as reported lock
395 // if = 0x80 then use default [0x81]
396 // if = 0x81 then use measured (from device)
397 // others not defined or implemented
398 if ((pStatus->lockOverride & (XDAS_Int8)0x80) == 0)
399 pStatus->lock = pStatus->lockOverride;
400 else if (pStatus->lockOverride == (XDAS_Int8)0x80)
401 pStatus->lock = inputStatus.lock;
402 else if (pStatus->lockOverride == (XDAS_Int8)0x81)
403 pStatus->lock = inputStatus.lock;
405 // if MSB of override clear then use it as sample rate for system,
406 // if = 0x80 then use default [0x82]
407 // if = 0x81 then use data
408 // if = 0x82 then use measured
409 // others not defined or implemented
410 if ((pStatus->sampleRateOverride & (XDAS_Int8)0x80) == 0)
411 pStatus->sampleRateStatus = pStatus->sampleRateOverride;
412 else if (pStatus->sampleRateOverride == (XDAS_Int8)0x80)
413 pStatus->sampleRateStatus = pStatus->sampleRateMeasured;
414 else if (pStatus->sampleRateOverride == (XDAS_Int8)0x81)
415 pStatus->sampleRateStatus = pStatus->sampleRateData;
416 else if (pStatus->sampleRateOverride == (XDAS_Int8)0x82)
417 pStatus->sampleRateStatus = pStatus->sampleRateMeasured;
419 // Update emphasis status:
420 if ((pStatus->emphasisOverride & (XDAS_Int8)0x80) == 0) {
421 if (pStatus->emphasisData == PAF_IEC_PREEMPHASIS_YES)
422 pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_YES;
423 else
424 pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_NO;
425 }
426 else if (pStatus->emphasisOverride ==
427 (XDAS_Int8 )(0x80+PAF_IEC_PREEMPHASIS_YES))
428 pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_YES;
429 else /* IBEmphasisOverrideNo or other */
430 pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_NO;
432 // Update precision control
433 pConfig->precision = pStatus->precisionInput =
434 pStatus->precisionOverride < 0
435 ? pStatus->precisionDefault
436 : pStatus->precisionOverride > 0
437 ? pStatus->precisionOverride
438 : pStatus->precisionDetect > 0
439 ? pStatus->precisionDetect
440 : pStatus->precisionDefault;
442 return 0;
444 } /* PAF_DEC_updateInputStatus */
446 // ----------------------------------------------------------------------------
448 #if 0
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
552 #endif
554 // ----------------------------------------------------------------------------
556 //
557 // Audio Stream Task / Pass Processing - Buffer Copy
558 //
559 // Name: PAF_BUF_copy
560 // Purpose: Decode Function for copying the data in the input buffer
561 // to the output buffer.
562 // From: AST Parameter Function -> copy
563 // Uses: None.
564 // States: None.
565 // Return: 0 on success.
566 // 0x80000000 for errors in required arguments.
567 // Trace: None.
568 //
570 #define min(a, b) (((a) < (b)) ? (a) : (b))
572 #include <pafdec.h>
573 #include <pafenc.h>
575 /* DO NOT REMOVE THIS CODE_SECTION. --Kurt */
576 #pragma CODE_SECTION(PAF_BUF_copy,".text:_PAF_BUF_copy")
578 Int
579 PAF_BUF_copy(
580 Uns inpChan,
581 PAF_InpBufConfig *pInpCfg,
582 Uns outChan,
583 PAF_OutBufConfig *pOutCfg)
584 {
585 Int i;
586 Int *pInBuf, *pOutBuf;
587 Int numSamples;
589 #ifndef __TI_EABI__
590 asm (" .clink");
591 #endif
593 if( (pInpCfg == NULL) || (pOutCfg == NULL) )
594 return ASPERR_UNSPECIFIED;
596 if( (inpChan > pInpCfg->stride) || (outChan > pOutCfg->stride) )
597 return ASPERR_UNSPECIFIED;
599 //for now assume =32bit words
600 if( (pInpCfg->sizeofElement != 4) ||
601 (pInpCfg->sizeofElement != pOutCfg->sizeofElement) )
602 return ASPERR_UNSPECIFIED;
604 pInBuf = pInpCfg->pntr.pLgInt;
605 pOutBuf = pOutCfg->pntr.pLgInt;
607 numSamples = min(pInpCfg->frameLength, pOutCfg->lengthofFrame);
608 pInBuf += inpChan;
609 pOutBuf += outChan;
610 for( i=0; i < numSamples; i++ )
611 {
612 *pOutBuf = *pInBuf;
613 pInBuf += pInpCfg->stride;
614 pOutBuf += pOutCfg->stride;
615 }
617 return 0;
618 } /* PAF_BUF_copy */
620 // EOF