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 //
38 // Stacking Input Buffer Driver implementations
39 //
40 //
41 // Derived from /c/ti/c6000/src/drivers/dgs.c
43 #include <xdc/std.h>
44 #include <xdc/cfg/global.h>
45 #include <xdc/runtime/Memory.h>
46 #include <xdc/runtime/System.h>
47 #include <xdc/runtime/Error.h>
48 #include <xdc/runtime/IHeap.h>
49 #include <ti/sysbios/knl/Queue.h>
50 #include <ti/sysbios/knl/Semaphore.h>
51 #include <ti/sysbios/heaps/HeapMem.h>
53 #include <dev2.h>
54 #include "mib.h"
55 #include "miberr.h"
56 #include <inpbuf.h>
57 #include <pafdec.h>
58 #include <pafsio.h>
59 #include <pafsio_ialg.h>
61 #include <limits.h> //INT_MAX
62 #include <string.h>
64 typedef xdc_Short MdInt;
65 typedef xdc_UShort MdUns;
66 typedef xdc_Char SmInt;
67 typedef xdc_UChar SmUns;
70 extern const ti_sysbios_heaps_HeapMem_Handle heapMemDdr3;
71 #define HEAPMALLOC (IHeap_Handle)heapMemDdr3
72 extern HeapMem_Handle DEV2_memSpaceToHeap (IALG_MemSpace space);
74 #if 0
75 #if ((PAF_DEVICE&0xFF000000) == 0xD8000000)
76 #ifndef dMAX_CFG
77 #include <dap_dmax.h>
78 #else
79 #include <dmax_struct.h>
80 #include <dmax_params.h>
81 #endif /* dMAX_CFG */
82 #include "psdkaf_hjt.h"
83 extern Uint32 DAT_cacheop_and_copy (void *src, void *dst, Uint16 byteCnt);
84 #undef DAT_copy
85 #define DAT_copy DAT_cacheop_and_copy
86 #else
87 #include "pafhjt.h"
88 #include "dmax_dat.h" // this has to come after pafhjt
89 #endif
91 #else
92 #include "pafhjt.h"
93 #endif
95 #include <ti/sysbios/knl/Clock.h>
96 #include <xdc/runtime/Log.h>
98 //grab from samrat.c
99 #include <paftyp.h>
100 #include <stdasp.h>
102 #include <logp.h>
103 #include <stdio.h> // printf in case of programming error
105 // allows you to set a different trace module in pa.cfg
106 #define TR_MOD trace
108 //#define TRACE_ENABLE
109 #ifdef TRACE_ENABLE
110 #include "dp.h"
111 #define TRACE(a) dp a // LOG_printf a
112 #else
113 #define TRACE(a)
114 #endif
116 extern const PAF_SampleRateHz
117 PAF_ASP_sampleRateHzTable[PAF_SAMPLERATE_N][PAF_SAMPLERATEHZ_N];
119 #define SCANATSAMPLERATELIMIT ((float) 48000.)
121 // Likely needs to be 7 when/if AC3 handling is changed to
122 // throw away fill.
123 #define NUM_CHILD_BUFFERS 6
125 Int DIB_ctrl (DEV2_Handle device, Uns code, Arg Arg);
126 Int DIB_idle (DEV2_Handle device, Bool Flush);
127 Int DIB_issue (DEV2_Handle device);
128 Int DIB_open (DEV2_Handle device, String Name);
129 Int DIB_reclaim (DEV2_Handle device);
130 Int DIB_getSync (DEV2_Handle device, PAF_InpBufConfig *pBufConfig);
131 Int DIB_initFrame (DEV2_Handle device, PAF_InpBufConfig *pBufConfig);
132 Int DIB_requestFrame (DEV2_Handle device, PAF_InpBufConfig *pBufConfig);
133 Int DIB_issueChild (DEV2_Handle device, PAF_InpBufConfig *pBufConfig, Int size, Int forTotal);
134 Int DIB_reclaimChild (DEV2_Handle device, PAF_InpBufConfig *pBufConfig);
135 Int DIB_reset (DEV2_Handle device, PAF_InpBufConfig *pBufConfig);
136 Int DIB_syncScan (DEV2_Handle device, PAF_InpBufConfig *pBufConfig, XDAS_UInt32 *pTimeout);
137 Int DIB_waitForData (DEV2_Handle device, PAF_InpBufConfig *pBufConfig, XDAS_UInt32 count);
138 Int DIB_syncScanDTS (DEV2_Handle device, PAF_InpBufConfig *pBufConfig, XDAS_UInt32 *pTimeout, XDAS_UInt16 *pHeaderEnd);
140 // Driver function table.
141 DIB_Fxns DIB_FXNS = {
142 NULL, // close not used in PA/F systems
143 DIB_ctrl,
144 DIB_idle,
145 DIB_issue,
146 DIB_open,
147 NULL, // ready not used in PA/F systems
148 DIB_reclaim,
149 DIB_getSync,
150 DIB_initFrame,
151 DIB_requestFrame,
152 DIB_issueChild,
153 DIB_reclaimChild,
154 DIB_reset,
155 DIB_syncScan,
156 DIB_waitForData,
157 DIB_syncScanDTS
158 };
160 // macros assume pDevExt is available and pDevExt->pFxns is valid
161 #define DIB_FTABLE_getSync(_a,_b) (*pDevExt->pFxns->getSync)(_a,_b)
162 #define DIB_FTABLE_initFrame(_a,_b) (*pDevExt->pFxns->initFrame)(_a,_b)
163 #define DIB_FTABLE_requestFrame(_a,_b) (*pDevExt->pFxns->requestFrame)(_a,_b)
164 #define DIB_FTABLE_issueChild(_a,_b,_c,_d) (*pDevExt->pFxns->issueChild)(_a,_b,_c,_d)
165 #define DIB_FTABLE_reclaimChild(_a,_b) (*pDevExt->pFxns->reclaimChild)(_a,_b)
166 #define DIB_FTABLE_reset(_a,_b) (*pDevExt->pFxns->reset)(_a,_b)
167 #define DIB_FTABLE_syncScan(_a,_b,_c) (*pDevExt->pFxns->syncScan)(_a,_b,_c)
168 #define DIB_FTABLE_waitForData(_a,_b,_c) (*pDevExt->pFxns->waitForData)(_a,_b,_c)
169 #define DIB_FTABLE_syncScanDTS(_a,_b,_c,_d) (*pDevExt->pFxns->syncScanDTS)(_a,_b,_c,_d)
171 // .............................................................................
173 //IBMODE
174 enum
175 {
176 MODE_DEFAULT = 0,
177 MODE_NO_ZERORUNRESTART = 1,
178 MODE_NO_ZERORUN = 2
179 };
181 // syncState
182 enum
183 {
184 SYNC_NONE,
185 SYNC_ONE,
186 SYNC_ONGOING,
187 SYNC_PCM,
188 SYNC_PCM_FORCED,
189 SYNC_AUTO
190 };
192 // scanState
193 enum
194 {
195 SCANNED_NONE,
196 SCANNED_IEC_PA,
197 SCANNED_IEC_PB,
198 SCANNED_IEC_PC,
199 SCANNED_DTS14_SYNC_A,
200 SCANNED_DTS14_SYNC_B,
201 SCANNED_DTS14_SYNC_C,
202 SCANNED_DTS14_SYNC_D,
203 SCANNED_DTS16_SYNC_A,
204 SCANNED_DTS16_SYNC_B,
205 SCANNED_DTS16_SYNC_C
206 };
208 // all sizes in number of 16bit words words
209 #define IEC_HEADER_SIZE 4 //PA PB PC PD
210 #define DTS14_HEADER_SIZE 6
211 #define DTS16_HEADER_SIZE 4
213 //table needed until PAF_SOURCE is reordered to match IEC numbering
214 const SmUns iecPafSource[23] =
215 {
216 PAF_SOURCE_UNKNOWN, // 0: IEC NULL Type
217 PAF_SOURCE_AC3, // 1: Comments on 1-15 match IEC 61937 part 2.
218 PAF_SOURCE_UNKNOWN, // 2: IEC reserved
219 PAF_SOURCE_UNKNOWN, // 3: IEC pause
220 PAF_SOURCE_UNKNOWN, // 4: MPEG 1 layer 1
221 PAF_SOURCE_MP3, // 5: MPEG layer 2 or 3
222 PAF_SOURCE_UNKNOWN, // 6: MPEG 2 data with extension
223 PAF_SOURCE_AAC, // 7: MPEG-2 AAC ADTS
224 PAF_SOURCE_UNKNOWN, // 8: MPEG 2 layer 1 low sampling frequency
225 PAF_SOURCE_UNKNOWN, // 9: MPEG 2 layer 2 or 3 low sampling frequency
226 PAF_SOURCE_UNKNOWN, // 10: reserved
227 PAF_SOURCE_DTS, // 11: DTS type 1 (11 bit: 512 sample repeat period)
228 PAF_SOURCE_DTS12, // 12: DTS type 2 (12 bit: 1024 sample repeat period)
229 PAF_SOURCE_DTS13, // 13: DTS type 3 (13 bit: 2048 sample repeat period)
230 PAF_SOURCE_DTS14, // 14: ATRAC
231 PAF_SOURCE_UNKNOWN, // 15: ATRAC 2/3
232 PAF_SOURCE_THD, // 16
233 PAF_SOURCE_DTSHD, // 17
234 PAF_SOURCE_WMA9PRO, // 18
235 PAF_SOURCE_UNKNOWN, // 19
236 PAF_SOURCE_UNKNOWN, // 20
237 PAF_SOURCE_DDP, // 21
238 PAF_SOURCE_THD, // 22
239 };
241 // IEC framelengths (in 16bit words)
242 static const MdUns iecFrameLength[23] =
243 {
244 0,
245 1536*2,
246 0, 0, 0,
247 1152*2,
248 0,
249 1024*2,
250 0, 0, 0,
251 512*2,
252 1024*2,
253 2048*2,
254 0, 0,
255 15*1024*2, //THD
256 1*1024, // DTSHD, actual framelength is adjusted by DTSsubType
257 4096*2,
258 0,0,
259 1536*2*4,
260 15*1024*2 //THD
261 };
263 #define IEC_PA ((short) 0xF872)
264 #define IEC_PB ((short) 0x4E1F)
265 #define DTS16_SYNC_A ((short) 0x7FFE)
266 #define DTS16_SYNC_B ((short) 0x8001)
267 #define DTS16_SYNC_C ((short) 0xFC00)
268 #define DTS14_SYNC_A ((short) 0x1FFF)
269 #define DTS14_SYNC_B ((short) 0xE800)
270 #define DTS14_SYNC_C ((short) 0x07F0)
272 #define DTS_BURST_TYPE_I 0x008B
273 #define DTS_BURST_TYPE_II 0x018C
274 #define DTS_BURST_TYPE_III 0x028D
275 #define DTS_BURST_TYPE_IV 0x0491
277 #define DTS_BURST_TYPE_IV_CBR 0x02
278 #define DTS_BURST_TYPE_IV_LBR 0x03
279 #define DTS_BURST_TYPE_IV_HBR 0x04
281 #define DEFAULT_AUTOREQUESTSIZE 128
282 //#define DEFAULT_AUTOREQUESTSIZE 256
284 // This is used at a couple of locations to ensure the transfer
285 // size is sufficiently long to be useful.
286 //#define NOMINAL_XFERSIZE 128
287 #define NOMINAL_XFERSIZE 128 //GJ Debug
288 //#define NOMINAL_XFERSIZE 512 //GJ Debug
290 #define min(a, b) (((a) < (b)) ? (a) : (b))
291 #define max(a, b) (((a) > (b)) ? (a) : (b))
293 // FL: (***) debug
294 #include "evmc66x_gpio_dbg.h"
296 // -----------------------------------------------------------------------------
298 inline void IncrementPtr (PAF_InpBufConfig *pBufConfig, Ptr *pPtr, int numWords)
299 {
300 int addr;
303 addr = (int) *pPtr + numWords*pBufConfig->sizeofElement;
304 if (addr > ((int) pBufConfig->base.pVoid + pBufConfig->sizeofBuffer - 1))
305 addr -= pBufConfig->sizeofBuffer;
307 *pPtr = (Ptr) addr;
308 return;
309 } // IncrementPtr
311 // -----------------------------------------------------------------------------
313 inline int GetNumAvail (PAF_InpBufConfig *pBufConfig)
314 {
315 int numBytes;
318 if ( (Uns) pBufConfig->head.pVoid >= (Uns) pBufConfig->pntr.pVoid)
319 numBytes = ((int)pBufConfig->head.pVoid - (int)pBufConfig->pntr.pVoid);
320 else
321 numBytes = ((int)pBufConfig->head.pVoid - (int)pBufConfig->pntr.pVoid + pBufConfig->sizeofBuffer);
323 // return in words
324 return (numBytes / pBufConfig->sizeofElement);
325 } // GetNumAvail
327 // -----------------------------------------------------------------------------
328 // compute how much data we have including outstanding requests
330 inline int GetFutureAvail (PAF_InpBufConfig *pBufConfig)
331 {
332 int numBytes;
335 if ( (Uns) pBufConfig->futureHead.pVoid >= (Uns) pBufConfig->pntr.pVoid)
336 numBytes = ((int)pBufConfig->futureHead.pVoid - (int)pBufConfig->pntr.pVoid);
337 else
338 numBytes = ((int)pBufConfig->futureHead.pVoid - (int)pBufConfig->pntr.pVoid + pBufConfig->sizeofBuffer);
340 // return in words
341 return (numBytes / pBufConfig->sizeofElement);
342 } // GetFutureAvail
344 // -----------------------------------------------------------------------------
346 Int DIB_issue (DEV2_Handle device)
347 {
348 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
349 DEV2_Frame *srcFrame;
350 Int status;
351 PAF_InpBufConfig *pBufConfig;
354 srcFrame = Queue_get (device->todevice);
355 pBufConfig = (PAF_InpBufConfig *) srcFrame->addr;
356 if (!pBufConfig || !pBufConfig->pBufStatus)
357 return SIO2_EINVAL;
359 Queue_put (device->fromdevice, (Queue_Elem *)srcFrame);
361 //GJ: PAF_SIO_REQUEST_AUTO, Not Supported, yet.
362 if (srcFrame->arg == PAF_SIO_REQUEST_AUTO) {
363 #if 0
364 // if not yet running, prime input buffer
365 if (pDevExt->syncState == SYNC_NONE) {
367 status = DIB_FTABLE_reset (device, pBufConfig);
368 if (status)
369 return status;
371 pDevExt->sourceProgram = PAF_SOURCE_UNKNOWN;
373 status = DIB_FTABLE_issueChild (device, &pDevExt->bufConfig, pDevExt->autoRequestSize, 0);
374 if (status)
375 return status;
377 // * also reset pDevExt->zeroCount, as in DIB_getSync()?
378 // just for PAF_SOURCE_PCMAUTO? *
379 pDevExt->pcmTimeout =
380 pDevExt->sourceSelect == PAF_SOURCE_PCMAUTO ? 0 :
381 2 * pBufConfig->pBufStatus->unknownTimeout;
382 pDevExt->syncState = SYNC_AUTO;
384 // allows PCM decoder to be used to generate zero-valued audio frames while scanning
385 pBufConfig->deliverZeros = 1;
386 }
387 // no action if already sync'ed
388 else if (pDevExt->syncState != SYNC_AUTO)
389 return 0;
391 status = DIB_FTABLE_issueChild (device, &pDevExt->bufConfig, pDevExt->autoRequestSize, 0);
392 if (status)
393 return status;
394 #endif
395 }
396 else if (srcFrame->arg == PAF_SIO_REQUEST_NEWFRAME) {
398 // do nothing if not synced since syncing done in reclaim
399 // This is executed in force modes from the first call to PAF_AST_decodeInit
400 // which calls SIO2_issue for NEWFRAME before all other calls.
401 if (pDevExt->syncState == SYNC_NONE)
402 return 0;
404 status = DIB_FTABLE_requestFrame (device, &pDevExt->bufConfig);
405 if (status)
406 return status;
407 }
409 return 0;
410 } // DIB_issue
412 // -----------------------------------------------------------------------------
413 // Although interface allows for arbitrary BufConfigs we only support 1 -- so
414 // we can assume the one on the fromdevice is the one we want
416 extern int gIsrInputCnt; // GJ Debug
417 extern int gIsrOutputCnt; // GJ Debug
419 Int DIB_reclaim (DEV2_Handle device)
420 {
421 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *) device->object;
422 DEV2_Frame *dstFrame;
423 Int status = 0;
424 PAF_InpBufConfig *pBufConfig;
426 dstFrame = (DEV2_Frame *) Queue_head (device->fromdevice);
428 if (dstFrame == (DEV2_Frame *) device->fromdevice)
429 return DIBERR_UNSPECIFIED;
430 if (!dstFrame->addr)
431 return DIBERR_UNSPECIFIED;
433 // if deferred error from last request frame then return now
434 status = pDevExt->deferredError;
435 if (status) {
436 pDevExt->deferredError = 0;
437 pDevExt->sourceProgram = PAF_SOURCE_UNKNOWN;
438 return status;
439 }
441 pBufConfig = (Ptr) dstFrame->addr;
442 dstFrame->size = sizeof (PAF_InpBufConfig);
444 // .........................................................................
446 if (dstFrame->arg == PAF_SIO_REQUEST_AUTO) {
447 //GJ: PAF_SIO_REQUEST_AUTO - not supported, yet.
448 #if 0
449 Log_info0("DIB: At case = PAF_SIO_REQUEST_AUTO"); // GJ Debug
451 // verify set up by prior DIB_issue()
452 // no action if already sync'ed
453 if (pDevExt->syncState == SYNC_NONE)
454 return DIBERR_AUTO;
455 else if (pDevExt->syncState != SYNC_AUTO)
456 return 0;
458 // get next block of data to scan
459 status = DIB_FTABLE_waitForData (device, &pDevExt->bufConfig, pDevExt->autoRequestSize);
460 if (status)
461 return status;
463 if (pDevExt->sourceSelect == PAF_SOURCE_NONE) {
464 IncrementPtr (pBufConfig, &pDevExt->bufConfig.pntr.pVoid, pDevExt->autoRequestSize);
465 return 0;
466 }
468 // this function updates the tail pointer; it WON'T reset to SYNC_NONE
469 status = DIB_FTABLE_syncScan (device, &pDevExt->bufConfig, &pDevExt->pcmTimeout);
470 if (status)
471 return status;
473 // if not yet sync'ed ...
474 if (pDevExt->syncState == SYNC_AUTO) {
475 // if timeout, harness tail of DIB_getSync()
476 if (!pDevExt->pcmTimeout) {
478 // checks pDevExt->zeroCount, runs DIB_reset(),
479 // updates pBufConfig->deliverZeros/sizeofElement/sizeofBuffer,
480 // sets pDevExt->sourceProgram = PAF_SOURCE_PCM/DSD?
481 // and pDevExt->syncState = SYNC_PCM[_FORCED]
482 status = DIB_FTABLE_getSync (device, &pDevExt->bufConfig);
483 if (status)
484 return status;
485 }
486 }
488 // if sync'ed to bitstream, this requests add'l input;
489 // if PCM, this "kickstarts" the input,
490 // using settings per above call to DIB_getSync()
491 if (pDevExt->syncState != SYNC_AUTO) {
493 status = DIB_FTABLE_initFrame (device, &pDevExt->bufConfig);
494 if (status)
495 return status;
497 // this situation may occur for various IEC types,
498 // w/ no matching PAF_SOURCE_ type is defined
499 // (see iecPafSource[] table for the latter mapping)
500 // most like cases are null & pause packets
501 if (pDevExt->sourceProgram == PAF_SOURCE_UNKNOWN)
502 return DIBERR_SYNC;
504 // request timing frame
505 status = DIB_FTABLE_requestFrame (device, &pDevExt->bufConfig);
506 if (status)
507 return status;
508 }
510 #endif
511 } //dstFrame->arg == PAF_SIO_REQUEST_AUTO
513 // .........................................................................
515 if ((pDevExt->syncState == SYNC_NONE) || (dstFrame->arg == PAF_SIO_REQUEST_SYNC)) {
517 Log_info0("DIB: At case: syncState == SYNC_NONE (or PAF_SIO_REQUEST_SYNC)"); // GJ Debug
519 // pass in external buffer config which used to initialize the internal view
520 status = DIB_FTABLE_reset (device, pBufConfig);
521 if (status)
522 return status;
524 status = DIB_FTABLE_getSync (device, &pDevExt->bufConfig);
525 if (status)
526 return status;
528 // since getSync resets sourceProgram to unknown at entry
529 // sourceProgram will remain unknown if no sync is returned
530 // (i.e. no need to reset it here)
531 if (pDevExt->syncState == SYNC_NONE)
532 {
533 Log_info0("Returning DIBERR_SYNC after DIB_FTABLE_getSync"); // GJ Debug
534 return DIBERR_SYNC;
535 }
537 // get input info (frameLength/etc)
538 status = DIB_FTABLE_initFrame (device, &pDevExt->bufConfig);
540 if (status)
541 {
542 Log_info1("Returning %d after DIB_FTABLE_initFrame", status); // GJ Debug
543 return status;
544 }
546 // request timing frame
547 status = DIB_FTABLE_requestFrame (device, &pDevExt->bufConfig);
548 if (status)
549 {
550 Log_info1("Returning %d after DIB_FTABLE_requestFrame", status); // GJ Debug
551 return status;
552 }
553 // update external view of bufConfig. In particular for slave force PCM
554 // this insures that the first decode will be processed with deliverZeros = 1
555 // which is necessary since the first decode of slave input occurs before the
556 // first slave input frame is actually captured.
557 *pBufConfig = pDevExt->bufConfig;
559 } //((pDevExt->syncState == SYNC_NONE) || (dstFrame->arg == PAF_SIO_REQUEST_SYNC))
561 // .........................................................................
563 if (dstFrame->arg == PAF_SIO_REQUEST_NEWFRAME) {
565 Log_info0("DIB: At case = PAF_SIO_REQUEST_NEWFRAME"); // GJ Debug
567 // wait for enough data to check for sync at expected location
568 status = DIB_FTABLE_waitForData (device, &pDevExt->bufConfig, pDevExt->bufConfig.frameLength);
569 if (status)
570 {
571 Log_info2("DIB_reclaim.%d DIB_FTABLE_waitForData returned %d", __LINE__, status); // GJ Debug
572 //TRACE((&TR_MOD, "DIB_reclaim.%d DIB_FTABLE_waitForData returned %d\n", __LINE__, status));
573 return status;
574 }
576 // if PCM, but not forced PCM, then scan for bitstream sync
577 // note that we we using the local view of bufConfig here and we update the
578 // public view afterwards.
579 if (pDevExt->syncState == SYNC_PCM) {
580 float sampleRate = PAF_ASP_sampleRateHzTable[pBufConfig->pBufStatus->sampleRateStatus][PAF_SAMPLERATEHZ_STD];
581 PAF_InpBufConfig *pBufConfig = &pDevExt->bufConfig;
582 Int ibMode = pBufConfig->pBufStatus->mode; // read mode register once
584 // normally no scanning done if sampleRate > 48kHz since bitstream input is highly unlikley.
585 if ((sampleRate <= SCANATSAMPLERATELIMIT) || pBufConfig->pBufStatus->scanAtHighSampleRateMode) {
586 if (!pBufConfig->deliverZeros) {
587 pDevExt->pcmTimeout = pBufConfig->lengthofData;
589 // check zeroRunTrigger if needed
590 if (ibMode != MODE_NO_ZERORUN) {
591 if(pDevExt->zeroCount >= 2 * pBufConfig->pBufStatus->zeroRunTrigger) {
592 // set this flag one block late to allow for
593 // transmission of all data in frame which contained zeroRunTrigger
594 pBufConfig->deliverZeros = 1;
595 pDevExt->pcmTimeout = 2 * pBufConfig->pBufStatus->unknownTimeout;
596 } // > zeroRunTrigger
597 } // !MODE_NO_ZERORUN
598 } // !pBufConfig->deliverZeros
600 // scan PCM data
601 status = DIB_FTABLE_syncScan (device, pBufConfig, &pDevExt->pcmTimeout);
602 if (status) {
603 pDevExt->sourceProgram = PAF_SOURCE_UNKNOWN;
604 Log_info2("DIB_reclaim.%d DIB_FTABLE_syncScan returned %d", __LINE__, status); // GJ Debug
605 //TRACE((&TR_MOD, "DIB_reclaim.%d DIB_FTABLE_syncScan returned %d\n", __LINE__, status));
606 return status;
607 }
609 // if scan found something other than PCM, then exit with error
610 if (pDevExt->syncState != SYNC_PCM)
611 {
612 Log_info1("DIB_reclaim.%d error: syncState != SYNC_PCM", __LINE__); // GJ Debug
613 //TRACE((&TR_MOD, "DIB_reclaim.%d error: syncState != SYNC_PCM\n", __LINE__));
614 return DIBERR_SYNC;
615 }
617 // if heeding zeroRunRestart control then return to unknown if necessary
618 if ((ibMode == MODE_DEFAULT) &&
619 (pDevExt->zeroCount >= 2*pBufConfig->pBufStatus->zeroRunRestart)) {
620 // if zeroRunRestart me then reset input to unknown
621 Log_info1("DIB_reclaim.%d error: zeroRunRestart, setting PAF_SOURCE_UNKNOWN", __LINE__); // GJ Debug
622 //TRACE((&TR_MOD, "DIB_reclaim.%d error: zeroRunRestart, setting PAF_SOURCE_UNKNOWN\n", __LINE__));
623 pDevExt->sourceProgram = PAF_SOURCE_UNKNOWN;
624 return DIBERR_SYNC;
625 }
627 // since in skeptical state we disallow returning to PCM when end of timeout contains zeros
628 // note that we need not check the mode here since above logic prevents deliverZeros state.
629 if (pBufConfig->deliverZeros && (pDevExt->zeroCount >= 2 * pBufConfig->pBufStatus->zeroRunTrigger))
630 {
631 //TRACE((&TR_MOD, "DIB_reclaim.%d set pcmTimeout = unknown timeout\n", __LINE__));
632 pDevExt->pcmTimeout = 2 * pBufConfig->pBufStatus->unknownTimeout;
633 }
634 } //scanAtHighSampleRate
635 } //SYNC_PCM
637 // clear this flag immediately to play new audio ASAP
638 // this check is OK for forced PCM since pcmTimeout and zeroCount are not
639 // updated
640 if (pDevExt->bufConfig.deliverZeros && !pDevExt->pcmTimeout &&
641 (pDevExt->zeroCount < 2 * pDevExt->bufConfig.pBufStatus->zeroRunTrigger))
642 {
643 pDevExt->bufConfig.deliverZeros = 0;
644 }
646 // update external view of buffer with local view we do this with current
647 // frame info before the following update to the local view for the next
648 // frame to be captured. Need to subtract headerSize from lengthofData
649 // since, at least for THD, it is needed by decoders.
650 *pBufConfig = pDevExt->bufConfig;
651 pBufConfig->lengthofData -= pDevExt->headerSize;
652 Log_info4("DIB_reclaim.%d lengthofData = %d; InISRCNT=%d; OutISRCNT=%d", __LINE__, pBufConfig->lengthofData, gIsrInputCnt, gIsrInputCnt); // GJ Debug
653 //TRACE((&TR_MOD, "DIB_reclaim.%d lengthofData = %d\n", __LINE__, pBufConfig->lengthofData));
655 // HACK: for DSD the frameLength needs to be the number of samples to generate.
656 if ((pDevExt->sourceSelect >= PAF_SOURCE_DSD1) &&
657 (pDevExt->sourceSelect <= PAF_SOURCE_DSD3))
658 pBufConfig->frameLength /= pBufConfig->stride;
659 Log_info2("DIB_reclaim.%d frameLength = %d", __LINE__, pBufConfig->frameLength); // GJ Debug
660 //TRACE((&TR_MOD, "DIB_reclaim.%d frameLength = %d\n", __LINE__, pBufConfig->frameLength));
662 // set external view to point at synch position of the frame guaranteed
663 // to be captured by above waitForData. Need to condition this against PCM since
664 // in that case pSync could point anywhere in the buffer to to syncScan. E.g.
665 // if there was a partial sync then pSync would point at that rather than
666 // at the beginning of the buffer.
667 // TODO: change syncScan to only update pDevExt->pSync when there is an actual sync
668 // so that we can use this for both PCM and bitstream
669 if (pDevExt->syncState == SYNC_ONGOING) {
670 pBufConfig->pntr = pDevExt->pSync;
671 IncrementPtr (pBufConfig, &pBufConfig->pntr.pVoid, pDevExt->headerSize);
672 } //SYNC_ONGOING
674 // if a bitstream then need to update tail to bitstream sync location
675 // and check for next sync at expected location
676 // quick scan for sync. if decoding IEC but sync is missing then
677 // defer error until the next frame so that we decode all frames
678 // TODO: does this assume decoders have copied the entire input frame
679 // i.e. it is ok to adjust the input buffer pointers.
680 // if successful then this sets
681 // pSync = address of PA (for IEC)
682 status = DIB_FTABLE_initFrame (device, &pDevExt->bufConfig);
683 if (status) {
684 if (pBufConfig->pBufStatus->lastFrameMask & (1 << pDevExt->sourceProgram)) {
685 pDevExt->deferredError = status;
686 Log_info1("DIB_reclaim.%d last frame\n", __LINE__); // GJ Debug
687 //TRACE((&TR_MOD, "DIB_reclaim.%d last frame\n", __LINE__));
688 pBufConfig->pBufStatus->lastFrameFlag = 1;
689 return 0;
690 }
691 else {
692 Log_info1("DIB_reclaim.%d setting PAF_SOURCE_UNKNOWN", __LINE__); // GJ Debug
693 //TRACE((&TR_MOD, "DIB_reclaim.%d setting PAF_SOURCE_UNKNOWN\n", __LINE__));
694 pDevExt->sourceProgram = PAF_SOURCE_UNKNOWN;
695 return status;
696 }
697 }
699 } //(dstFrame->arg == PAF_SIO_REQUEST_NEWFRAME)
701 // .........................................................................
703 Log_info4("DIB_reclaim.%d exit status = %d, InISRCNT = %d OutISRCNT = %d", __LINE__, status, gIsrInputCnt, gIsrOutputCnt); // GJ Debug
704 //TRACE((&TR_MOD, "DIB_reclaim.%d exit status = %d\n", __LINE__, status));
706 return status;
707 } // DIB_reclaim
709 // -----------------------------------------------------------------------------
711 Int DIB_ctrl (DEV2_Handle device, Uns code, Arg arg)
712 {
713 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
714 DEV2_Handle pChild = (DEV2_Handle)&pDevExt->child;
715 Int status = 0;
718 switch (code) {
720 case PAF_SIO_CONTROL_GET_CHILD_DEVICE:
721 *((Arg *)arg) = (Arg) pChild;
722 break;
724 //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
726 case PAF_SIO_CONTROL_SET_DECSTATUSADDR:
727 pDevExt->pDecodeStatus = (PAF_DecodeStatus *) arg;
728 break;
730 case PAF_SIO_CONTROL_SET_PCMFRAMELENGTH:
731 pDevExt->pcmFrameLength = (XDAS_Int32) arg;
732 break;
734 case PAF_SIO_CONTROL_SET_SOURCESELECT:
735 pDevExt->sourceSelect = (XDAS_Int8) arg;
736 break;
738 case PAF_SIO_CONTROL_SET_AUTOREQUESTSIZE:
739 pDevExt->autoRequestSize = (XDAS_Int16) arg;
740 break;
742 case PAF_SIO_CONTROL_GET_SOURCEPROGRAM:
743 if (!arg)
744 return DIBERR_UNSPECIFIED;
745 *((XDAS_Int8 *) arg) = pDevExt->sourceProgram;
746 break;
748 case PAF_SIO_CONTROL_SET_IALGADDR:
749 pDevExt->pSioIalg = (PAF_SIO_IALG_Obj *) arg;
750 break;
752 //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
754 case PAF_SIO_CONTROL_OPEN:
755 pDevExt->sourceProgram = PAF_SOURCE_UNKNOWN;
756 pDevExt->zeroCount = 0;
757 if (pDevExt->pInpBufStatus)
758 pDevExt->pInpBufStatus->zeroRun = 0;
759 status = DEV2_ctrl (pChild, code, arg);
760 break;
762 case PAF_SIO_CONTROL_CLOSE:
763 if (pDevExt->pInpBufStatus)
764 pDevExt->pInpBufStatus->zeroRun = 0;
765 status = DEV2_ctrl (pChild, code, arg);
766 break;
768 // return number of DMA events vs. request size
769 // only update if not in error state (i.e. status = 0)
770 case PAF_SIO_CONTROL_GET_NUM_EVENTS:
771 if (!arg)
772 return DIBERR_UNSPECIFIED;
773 status = DEV2_ctrl (pChild, code, arg);
774 if (!status)
775 *((Int *)arg) -= pDevExt->pcmFrameLength;
776 break;
778 // return net samples until next DIB_waitForData() should succeed w/o blocking
779 case PAF_SIO_CONTROL_GET_NUM_REMAINING:
780 if (!arg)
781 return DIBERR_UNSPECIFIED;
782 status = DEV2_ctrl (pChild, code, arg);
783 *((Int *)arg) -= pDevExt->numSamplesExtra;
784 break;
786 default:
787 status = DEV2_ctrl (pChild, code, arg);
788 break;
789 }
791 return status;
792 } // DIB_ctrl
794 // -----------------------------------------------------------------------------
796 Int DIB_idle (DEV2_Handle device, Bool flush)
797 {
798 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
799 Int status;
802 while (!Queue_empty (device->todevice))
803 Queue_enqueue (device->fromdevice, Queue_dequeue (device->todevice));
805 while (!Queue_empty (device->fromdevice))
806 Queue_enqueue (Queue_handle(&((SIO2_Handle) device)->framelist), Queue_dequeue (device->fromdevice));
808 status = DIB_FTABLE_reset (device, NULL);
809 if (status)
810 return status;
812 return 0;
813 } // DIB_idle
815 // -----------------------------------------------------------------------------
817 Int DIB_open (DEV2_Handle device, String name)
818 {
819 DIB_DeviceExtension *pDevExt;
820 DEV2_Handle pChild;
821 DEV2_Device *entry;
822 DEV2_Frame *frame;
823 Int status, i;
824 Error_Block eb;
826 Error_init(&eb);
828 name = DEV2_match (name, &entry);
829 if (entry == NULL) {
830 Log_info1("DEV2_match failed in DIB_open:", SIO2_ENODEV);
831 return SIO2_ENODEV;
832 }
834 // only one frame interface supported
835 if (device->nbufs != 1)
836 return SYS_EALLOC;
838 if (!(pDevExt = Memory_alloc (device->bufSeg, sizeof(DIB_DeviceExtension), 0, &eb)))
839 {
840 Log_info1("DIB Memory alloc failed in DIB_open:", SYS_EALLOC);
841 return SYS_EALLOC;
842 }
843 pDevExt->pcmFrameLength = 0;
844 pDevExt->sourceSelect = PAF_SOURCE_NONE;
845 pDevExt->autoRequestSize = DEFAULT_AUTOREQUESTSIZE;
846 pDevExt->pInpBufStatus = NULL;
847 pDevExt->pDecodeStatus = NULL;
848 device->object = (Ptr)pDevExt;
850 pChild = (DEV2_Handle)&pDevExt->child;
851 pChild->fromdevice = Queue_create (NULL, &eb);
852 pChild->todevice = Queue_create (NULL, &eb);
853 if (pChild->fromdevice == NULL || pChild->todevice == NULL) {
854 Log_info1 ("DIB Queue_create failed in DIB_open:", SYS_EALLOC);
855 return SYS_EALLOC;
856 }
858 pChild->bufsize = 0;
859 pChild->nbufs = NUM_CHILD_BUFFERS;
860 pChild->bufSeg = device->bufSeg;
861 pChild->mode = device->mode;
862 pChild->timeout = device->timeout;
863 pChild->align = device->align;
864 pChild->devid = entry->devid;
865 pChild->params = entry->params;
866 pChild->fxns = *(DEV2_Fxns *)(entry->fxns);
867 ((SIO2_Handle)pChild)->model = ((SIO2_Handle)device)->model;
869 //Create frames and put them on the correct device queue.
870 // We only support ISSUERECLAIM mode so size = 0
871 Queue_construct(&((SIO2_Handle)pChild)->framelist, NULL); //Queue_new (&((SIO2_Handle)pChild)->framelist);
872 for (i=0; i < pChild->nbufs; i++) {
873 frame = DEV2_mkframe (0, 0, 0);
874 if (!frame)
875 return SYS_EALLOC;
876 Queue_put (Queue_handle(&((SIO2_Handle)pChild)->framelist), (Queue_Elem *) frame);
877 }
879 // open underlying device
880 status = DEV2_open (pChild, name);
881 if (status)
882 return status;
884 // use dev match to fetch function table pointer for DIB
885 name = DEV2_match ("/DIB", &entry);
886 if (entry == NULL) {
887 Log_info1 ("DEV2_match for DIB in DIB_open:", SIO2_ENODEV);
888 return SIO2_ENODEV;
889 }
890 pDevExt->pFxns = (DIB_Fxns *) entry->fxns;
892 // set IEC frame length table pointer -- change in
893 // subsequent control call (not defined) to add new IEC type
894 pDevExt->pIECFrameLength = (XDAS_UInt16 *) iecFrameLength;
896 status = DIB_FTABLE_reset (device,NULL);
897 if (status)
898 return status;
900 return status;
901 } // DIB_open
903 // -----------------------------------------------------------------------------
904 // Although this is void it is still needed since BIOS calls all DEV inits on
905 // startup.
907 Void DIB_init (Void)
908 {
909 } // DIB_init
911 // -----------------------------------------------------------------------------
912 // Notes:
913 // 1. DIB_reset is called prior to this function being called.(see DIB_reclaim)
914 // 2. (1) ==> sizeofBuffer is an integral # of 2byte samples.
915 // 3. (2) ==> if we use a request size which is divisor of sizeofBuffer there
916 // will be no wrap-around when requesting data for scanning.
917 // 4. (3) ==> we meet the interface requirement for syncScan which has no
918 // circular arithmetic.
920 Int DIB_getSync (DEV2_Handle device, PAF_InpBufConfig *pBufConfig )
921 {
922 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
923 DEV2_Handle pChild = (DEV2_Handle)&pDevExt->child;
924 int status;
925 Uns timeout, syncBufSize, syncRequestSize;
926 Int ibMode = pBufConfig->pBufStatus->mode; // read mode register once
927 Int deliverZeros;
929 //..........................................................................
931 // implement local timeout so that we don't get 'stuck' here when input
932 // is all zeros + lock
933 if (pDevExt->syncState == SYNC_NONE) {
934 Uns localTimeout;
935 // latch value before resetting to PAF_SOURCE_UNKNOWN
936 Int sourceProgram = pDevExt->sourceProgram;
938 pDevExt->sourceProgram = PAF_SOURCE_UNKNOWN;
940 // if here then guaranteed to have fresh reset
942 if (pDevExt->sourceSelect == PAF_SOURCE_PCM || pDevExt->sourceSelect == PAF_SOURCE_DSD1 ||
943 pDevExt->sourceSelect == PAF_SOURCE_DSD2 || pDevExt->sourceSelect == PAF_SOURCE_DSD3) {
944 timeout = 0;
946 // no zero run counting in force modes since no scanning
947 pBufConfig->pBufStatus->zeroRun = 0;
948 pDevExt->zeroCount = 0;
949 }
950 else if ((pDevExt->sourceSelect == PAF_SOURCE_PCMAUTO) &&
951 ((sourceProgram == PAF_SOURCE_UNKNOWN) || (sourceProgram == PAF_SOURCE_PCM))) {
952 // if sourceselect is PCMAUTO then
953 // if current input is a bistream then scan for normal timeout period
954 // if current input is unknown or PCM then transition immediately to PCM
955 // this provides for *no* lossed PCM, detection of bitstreams, but some noise
956 // is possible.
957 timeout = 0;
958 pDevExt->zeroCount = 0;
959 }
960 else
961 timeout = 2*pBufConfig->pBufStatus->unknownTimeout;
963 // force request size to be a divisor of sizeofBuffer
964 syncRequestSize = NOMINAL_XFERSIZE;
965 if (pBufConfig->stride > 2)
966 syncRequestSize *= pBufConfig->stride;
967 syncBufSize = pBufConfig->sizeofBuffer / ((int) (pBufConfig->sizeofBuffer/syncRequestSize));
969 if (timeout) {
970 status = DIB_FTABLE_issueChild (device, pBufConfig, syncBufSize, 0);
971 if (status)
972 return status;
973 }
975 localTimeout = timeout;
976 while ((timeout) && (pDevExt->syncState != SYNC_ONE)) {
978 status = DIB_FTABLE_issueChild (device, pBufConfig, syncBufSize, 0);
979 if (status)
980 return status;
982 #if 0 // (***) FL: shows timing of Input Rx SIO reclaim during autodet
983 // (***) debug // B5
984 {
985 static Uint8 toggleState = 0;
986 if (toggleState == 0)
987 GPIOSetOutput(GPIO_PORT_0, GPIO_PIN_99);
988 else
989 GPIOClearOutput(GPIO_PORT_0, GPIO_PIN_99);
990 toggleState = ~(toggleState);
991 }
992 #endif
994 // get next block of data to scan
995 status = DIB_FTABLE_waitForData (device, pBufConfig, syncBufSize);
996 if (status)
997 return status;
999 // this function updates the tail pointer
1000 status = DIB_FTABLE_syncScan (device, pBufConfig, &timeout);
1001 if (status)
1002 return status;
1004 // if input is zero, i.e. haven't decremented at all,
1005 // then break out and use more logic and
1006 if (localTimeout < syncBufSize) {
1007 // Should this be 2 *? MAW
1008 if (timeout == 2*pBufConfig->pBufStatus->unknownTimeout)
1009 break;
1010 }
1011 else
1012 {
1013 Log_info3("DIB: Inside DIB_getSync with syncState != SYNC_ONE. localTimeout = %d, syncBufSize = %d, timeout = %d", localTimeout,syncBufSize, timeout); // GJ Debug
1014 localTimeout -= syncBufSize;
1015 }
1016 }
1018 // if found sync then return to caller who will call
1019 // initFrame to get bitstream info and requestFrame for data
1020 if (pDevExt->syncState != SYNC_NONE)
1021 return 0;
1023 } //pDevExt->syncState == SYNC_NONE
1025 //..........................................................................
1027 // set default to zero -- we assume decode calls will not be made
1028 // before data is available since this must be master input. Will be changed below
1029 // as needed
1030 deliverZeros = 0;
1032 if (pDevExt->syncState == SYNC_AUTO) {
1033 timeout = pDevExt->pcmTimeout;
1034 deliverZeros = 1;
1035 }
1037 // if in zero run, and heeding full zeroRun control, then return to unknown if # zeros > trigger/Restart
1038 if (ibMode == MODE_DEFAULT) {
1039 if ((pDevExt->zeroCount >= 2 * pBufConfig->pBufStatus->zeroRunTrigger) ||
1040 (pDevExt->zeroCount >= 2 * pBufConfig->pBufStatus->zeroRunRestart))
1041 return DIBERR_SYNC;
1042 // since we may have exited prematurely above we check timeout
1043 if (timeout)
1044 return DIBERR_SYNC;
1045 }
1046 // if heeding trigger but not restart then enter deliverZeros state of PCM
1047 else if (ibMode == MODE_NO_ZERORUNRESTART) {
1048 if (pDevExt->zeroCount >= 2 * pBufConfig->pBufStatus->zeroRunTrigger)
1049 deliverZeros = 1;
1050 }
1052 // here if timeout to PCM (includes force PCM)
1053 status = DIB_FTABLE_reset (device, pBufConfig);
1054 if (status)
1055 return status;
1057 // hack -- try 32bit then 16bit if necessary
1058 pBufConfig->sizeofElement = 4;
1059 status = SIO2_ctrl (pChild,(Uns)PAF_SIO_CONTROL_SET_WORDSIZE,pBufConfig->sizeofElement);
1060 if(status) {
1061 pBufConfig->sizeofElement = 2;
1062 status = SIO2_ctrl (pChild, (Uns)PAF_SIO_CONTROL_SET_WORDSIZE,pBufConfig->sizeofElement);
1063 if(status)
1064 return status;
1065 }
1067 // Force sizeofBuffer to be integral number of frame sizes. This ensures that the
1068 // pcm buffers will not need a circular wrap-around. We prevent this because
1069 // syncScan makes this assumption in order to perform an efficient scan.
1070 {
1071 int sizeofStride = pBufConfig->sizeofElement*pBufConfig->stride*pDevExt->pcmFrameLength;
1072 pBufConfig->sizeofBuffer = (pBufConfig->allocation)/sizeofStride*sizeofStride;
1073 }
1075 if (pDevExt->sourceSelect == PAF_SOURCE_DSD1 || pDevExt->sourceSelect == PAF_SOURCE_DSD2 ||
1076 pDevExt->sourceSelect == PAF_SOURCE_DSD3)
1077 pDevExt->sourceProgram = pDevExt->sourceSelect;
1078 else
1079 pDevExt->sourceProgram = PAF_SOURCE_PCM;
1081 if (pDevExt->sourceSelect == PAF_SOURCE_PCM || pDevExt->sourceSelect == PAF_SOURCE_DSD1 ||
1082 pDevExt->sourceSelect == PAF_SOURCE_DSD2 || pDevExt->sourceSelect == PAF_SOURCE_DSD3) {
1083 pDevExt->syncState = SYNC_PCM_FORCED;
1084 // set to one -- ensures that PCM decode calls made before data is
1085 // available will result in zero output.
1086 // (mostly needed for PA15 since, currently, all other frameworks
1087 // require a frame of data before the first decode call.
1088 deliverZeros = 1;
1089 }
1090 else
1091 pDevExt->syncState = SYNC_PCM;
1093 // update config struct
1094 pBufConfig->deliverZeros = deliverZeros;
1096 //..........................................................................
1098 return 0;
1099 } // DIB_getSync
1101 // -----------------------------------------------------------------------------
1103 int gWrapCtr=0;
1104 Int DIB_issueChild (DEV2_Handle device, PAF_InpBufConfig *pBufConfig, int size, int forTotal)
1105 {
1106 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *) device->object;
1107 DEV2_Handle pChild = (DEV2_Handle) &pDevExt->child;
1108 int bufEnd = (int) pBufConfig->base.pVoid + pBufConfig->sizeofBuffer;
1109 DEV2_Frame *dstFrame;
1110 int futureHead, status;
1111 int i, sizes[2];
1112 Ptr endAddr[2];
1116 // if seeking for total amount then adjust for difference
1117 if (forTotal)
1118 size -= GetFutureAvail (pBufConfig);
1120 // return success if we needn't make any requests
1121 if (size <= 0)
1122 return 0;
1124 // assume if eight channel then using optimized dMAX routine which requires
1125 // requests which are a mulitple of 8 to operate correctly. If not a proper
1126 // multiple then we increase the requested size as needed. This information
1127 // is communicated to other portions of DIB indirectly through the update
1128 // of the futureHead pointer (here) and head pointer (in reclaim). To these
1129 // other portions it is a don't care as we ensure enough data requested will
1130 // be available at, the now slightly deferred, reclaim point. We assume that
1131 // the buffer is a multiple of 8 and so, by using this single statement, we
1132 // ensure all requests are a mulitple 8 even if they need to be split across
1133 // the buffer wrap point.
1134 if (pBufConfig->stride == 8)
1135 size = (size + 7) & ~0x7;
1137 // convert to bytes
1138 size *= pBufConfig->sizeofElement;
1139 //size *= 4;
1141 // if request crosses circular buffer boundary then split into two requests
1142 futureHead = (int) pBufConfig->futureHead.pVoid + size;
1143 if (futureHead <= bufEnd) {
1144 sizes[0] = size;
1145 sizes[1] = 0;
1147 // If this request happens to be the rest of the buffer, then
1148 // futureHead must be set to the beginning of the buffer.
1149 if (futureHead != bufEnd)
1150 endAddr[0] = (Ptr) futureHead;
1151 else
1152 endAddr[0] = pBufConfig->base.pVoid;
1153 }
1154 else {
1155 sizes[0] = bufEnd - (int) pBufConfig->futureHead.pVoid;
1156 sizes[1] = futureHead - bufEnd;
1157 endAddr[0] = pBufConfig->base.pVoid;
1158 endAddr[1] = (Ptr) ((int)pBufConfig->base.pVoid + sizes[1]);
1159 }
1161 for (i=0; i < 2; i++) {
1162 if (sizes[i]) {
1163 dstFrame = Queue_get (Queue_handle(&((SIO2_Handle) pChild)->framelist));
1164 if (dstFrame == (DEV2_Frame *)&((SIO2_Handle) pChild)->framelist)
1165 return DIBERR_UNSPECIFIED;
1166 dstFrame->arg = (Arg) pBufConfig;
1168 dstFrame->addr = pBufConfig->futureHead.pVoid;
1169 dstFrame->size = sizes[i];
1170 Queue_put (pChild->todevice, (Queue_Elem *)dstFrame);
1171 status = DEV2_issue (pChild);
1172 if (status)
1173 return DIBERR_UNSPECIFIED;
1175 pBufConfig->futureHead.pVoid = endAddr[i];
1176 // GJ Debug
1177 if (i==1)
1178 {
1179 gWrapCtr++;
1180 Log_info4("DIB: Inside DIB_issueChild Wrap Around Point #%d, with Future Head: 0x%x, current addr: 0x%x, current size: %d", gWrapCtr, pBufConfig->futureHead.pVoid, dstFrame->addr, dstFrame->size ); // GJ Debug
1182 }
1183 }
1184 }
1186 return 0;
1187 } // DIB_issueChild
1189 // -----------------------------------------------------------------------------
1191 Int DIB_reclaimChild (DEV2_Handle device, PAF_InpBufConfig *pBufConfig)
1192 {
1193 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
1194 DEV2_Handle pChild = (DEV2_Handle)&pDevExt->child;
1195 DEV2_Frame *srcFrame;
1196 int status, bufEnd;
1198 //Log_info3("DIB_reclaimChild.%d: Inside DEV2_reclaim(pChild) pChild = 0x%x DEV2_reclaim = 0x%x", __LINE__, pChild, &pChild->fxns.reclaim); // GJ Debug
1199 //TRACE((&TR_MOD, "DIB_reclaimChild.%d: calling DEV2_reclaim(pChild) pChild = 0x%x DEV2_reclaim = 0x%x", __LINE__, pChild, &pChild->fxns.reclaim));
1200 status = DEV2_reclaim (pChild);
1201 if (status)
1202 {
1203 Log_info2("DIB_reclaimChild.%d DEV2_reclaim() returned (%d) DIBERR_UNSPECIFIED", __LINE__, status); // GJ Debug
1204 //TRACE((&TR_MOD, "DIB_reclaimChild.%d DEV2_reclaim() returned (%d) DIBERR_UNSPECIFIED \n", __LINE__, status));
1205 return DIBERR_UNSPECIFIED;
1206 }
1208 //Log_info1("DIB_reclaimChild.%d calling Queue_get()", __LINE__); // GJ Debug
1209 //TRACE((&TR_MOD, "DIB_reclaimChild.%d calling Queue_get()\n", __LINE__));
1210 srcFrame = Queue_get (pChild->fromdevice);
1211 //Log_info2("DIB_reclaimChild.%d calling Queue_put(), srcFrame = 0x%x", __LINE__, srcFrame); // GJ Debug
1212 //TRACE((&TR_MOD, "DIB_reclaimChild.%d calling Queue_put(), srcFrame = 0x%x\n", __LINE__, srcFrame));
1213 Queue_put (Queue_handle(&((SIO2_Handle) pChild)->framelist), (Queue_Elem *)srcFrame);
1215 // Only for non-fill requests do we update ptrs
1216 if (srcFrame->addr != NULL) {
1217 //Log_info2("DIB_reclaimChild.%d update pointers with srcFrame->size = %d", __LINE__, srcFrame->size); // GJ Debug
1218 //TRACE((&TR_MOD, "DIB_reclaimChild.%d update pointers\n", __LINE__));
1219 pBufConfig->head.pVoid = (Ptr) ((int)srcFrame->addr + srcFrame->size);
1221 // wrap, if necessary
1222 bufEnd = (int) pBufConfig->base.pVoid + pBufConfig->sizeofBuffer;
1223 if( (int) pBufConfig->head.pVoid >= bufEnd )
1224 {
1225 Log_info1("DIB_reclaimChild.%d wrap pointer", __LINE__); // GJ Debug
1226 //TRACE((&TR_MOD, "DIB_reclaimChild.%d wrap pointer\n", __LINE__));
1227 pBufConfig->head.pVoid = (Ptr) ((int) pBufConfig->base.pVoid + (int) pBufConfig->head.pVoid - bufEnd);
1228 }
1229 }
1231 Log_info2("DIB_reclaimChild.%d exit with status = %d", __LINE__, status); // GJ Debug
1232 //TRACE((&TR_MOD, "DIB_reclaimChild.%d exit with status = %d\n", __LINE__, status));
1234 return status;
1235 } // DIB_reclaimChild
1237 // -----------------------------------------------------------------------------
1238 // This function uses the local definition of frameLength and lengthofData in
1239 // pDevExt to request the next frame of data.
1241 Int DIB_requestFrame (DEV2_Handle device, PAF_InpBufConfig *pBufConfig)
1242 {
1243 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
1244 int status = 0;
1247 // if in steady state then update tail pointer to indicate we are done, i.e. no
1248 // longer own, the last frame of data.
1249 if (pDevExt->running > 1)
1250 IncrementPtr (pBufConfig, &pBufConfig->pntr.pVoid, pBufConfig->lengthofData);
1252 switch (pDevExt->syncState) {
1253 case SYNC_PCM:
1254 case SYNC_PCM_FORCED:
1256 if (pDevExt->sourceSelect == PAF_SOURCE_DSD1)
1257 pDevExt->frameLength = 256;
1258 else if (pDevExt->sourceSelect == PAF_SOURCE_DSD2)
1259 pDevExt->frameLength = 128;
1260 else if (pDevExt->sourceSelect == PAF_SOURCE_DSD3)
1261 pDevExt->frameLength = 64;
1262 else
1263 pDevExt->frameLength = pDevExt->pcmFrameLength;
1265 pDevExt->lengthofData = pBufConfig->stride*pDevExt->frameLength;
1266 pDevExt->frameLength = pDevExt->lengthofData;
1268 // note that the following issueChild
1269 // doesn't *fetch* the data which will next be consumed,
1270 // but rather *replenishes* what's about to be consumed
1271 status = DIB_FTABLE_issueChild (device, pBufConfig, pDevExt->lengthofData, 0);
1272 break;
1274 case SYNC_ONE:
1275 // for the first issue we need to set the tail pointer to the bitstream sync
1276 pBufConfig->pntr = pDevExt->pSync;
1277 IncrementPtr (pBufConfig, &pBufConfig->pntr.pVoid, pDevExt->headerSize);
1278 status = DIB_FTABLE_issueChild (device, pBufConfig, pDevExt->frameLength, 1);
1280 // HD codecs need extra time due to several factors
1281 // time between first info call and starting output is non-negligible
1282 // peak decoder MIPs
1283 // reset time for decoders/ASPs
1284 if ((pDevExt->sourceProgram == PAF_SOURCE_DDP) ||
1285 (pDevExt->sourceProgram == PAF_SOURCE_DTSHD) ||
1286 (pDevExt->sourceProgram == PAF_SOURCE_THD) ||
1287 (pDevExt->sourceProgram == PAF_SOURCE_DXP))
1288 status = DIB_FTABLE_issueChild (device, pBufConfig, pDevExt->lengthofData, 0);
1290 pDevExt->syncState = SYNC_ONGOING;
1291 break;
1293 case SYNC_ONGOING:
1294 status = DIB_FTABLE_issueChild (device, pBufConfig, pDevExt->lengthofData, 0);
1295 break;
1297 } //switch
1299 // update bufConfig with info for use in next reclaim call
1300 // the interface to DIB is based on a single frame. So the amount
1301 // of data requested in this issue is assumed to be what is wanted in the next
1302 // reclaim.
1303 pBufConfig->frameLength = pDevExt->frameLength;
1304 pBufConfig->lengthofData = pDevExt->lengthofData;
1305 // enable to inspect input buffer.
1306 // if (pDevExt->lengthofData > 512)
1307 // asm( " SWBP 0" ); // breakpoint
1310 if (pDevExt->running < 3)
1311 pDevExt->running++;
1313 // Goal is to align timing so synchronized with forthcoming
1314 // "DIB_waitForData (... pDevExt->lengthofData);"
1315 // in DIB_reclaim(), servicing PAF_SIO_REQUEST_NEWFRAME, for PAF_SOURCE_PCM/DSD?.
1316 // ** need to revise above DSD handling so it works w/ this calc. **
1317 {
1318 int futureAvail = GetFutureAvail (pBufConfig);
1319 // GetFutureAvail() returns 0 if full buffer requested or if no requests outstanding
1320 // -- 0 (empty) can't be right interpretation here, on account of foregoing issueChild()
1321 if( ! futureAvail)
1322 futureAvail = pBufConfig->sizeofBuffer / pBufConfig->sizeofElement; // total words in buffer
1323 pDevExt->numSamplesExtra = (XDAS_Int16) (futureAvail - pDevExt->frameLength);
1324 }
1326 return status;
1327 } // DIB_requestFrame
1329 // -----------------------------------------------------------------------------
1331 Int DIB_reset (DEV2_Handle device, PAF_InpBufConfig *pBufConfig)
1332 {
1333 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
1334 DEV2_Handle pChild = (DEV2_Handle)&pDevExt->child;
1335 int status, numChan;
1338 //?? Do we need a shutdown to handle queue problems?
1339 // or are there no problems since we use one frame
1340 status = DEV2_idle (pChild, 1);
1341 if(status)
1342 return status;
1344 if (pBufConfig) {
1345 int sizeofStride;
1346 pBufConfig->pntr = pBufConfig->base;
1347 pBufConfig->head = pBufConfig->base;
1348 pBufConfig->futureHead = pBufConfig->base;
1350 pBufConfig->lengthofData = 0;
1352 //devices must? support 2byte words
1353 pBufConfig->sizeofElement = 2;
1354 status = SIO2_ctrl (pChild, (Uns)PAF_SIO_CONTROL_SET_WORDSIZE,pBufConfig->sizeofElement);
1355 if(status)
1356 return status;
1358 status = DEV2_ctrl (pChild, PAF_SIO_CONTROL_GET_NUMCHANNELS, (Arg) &numChan);
1359 if(status)
1360 return status;
1361 pBufConfig->stride = numChan;
1363 // compute and use *effective buffer size*
1364 sizeofStride = pBufConfig->sizeofElement*pBufConfig->stride;
1365 pBufConfig->sizeofBuffer = (pBufConfig->allocation)/sizeofStride*sizeofStride; //GJ: Debug - Account for EDMA padding
1367 //hack -- save status context for use in close
1368 pDevExt->pInpBufStatus = pBufConfig->pBufStatus;
1369 pBufConfig->pBufStatus->lastFrameFlag = 0;
1371 pDevExt->bufConfig = *pBufConfig;
1372 pDevExt->pSync = pBufConfig->base;
1373 }
1375 pDevExt->syncState = SYNC_NONE;
1376 pDevExt->scanState = SCANNED_NONE;
1377 pDevExt->pcmTimeout = 0;
1378 pDevExt->deferredError = 0;
1380 pDevExt->numSamplesSinceDTS = 0;
1381 pDevExt->numSamplesExtra = 0;
1383 pDevExt->headerSize = 0;
1384 pDevExt->running = 0;
1386 return 0;
1387 } // DIB_reset
1389 // -----------------------------------------------------------------------------
1390 // Notes:
1391 // 1. The amount of data to be scanned will not result in a buffer wrap-around
1392 // 2. (1) is currently met from the two locations that call this function
1393 // a. DIB_getSync
1394 // b. DIB_reclaim (for PCM)
1395 // 3. We require that pTimeout != NULL since we dereference to make a const
1397 Int DIB_syncScan (DEV2_Handle device, PAF_InpBufConfig *pBufConfig, XDAS_UInt32 *pTimeout)
1398 {
1399 DIB_DeviceExtension * restrict pDevExt = (DIB_DeviceExtension *)device->object;
1400 MdInt * restrict pTail, * restrict pShadowTail, * restrict pSync;
1401 MdInt *pLocalTail, pc;
1402 XDAS_Int8 scanState;
1403 XDAS_UInt32 zeroCount;
1404 int stride, numLeft, i, datId;
1405 int status, foundDTS = 0;
1406 Uns scanCount, pageSize;
1407 PAF_SIO_IALG_Obj *pObj = pDevExt->pSioIalg;
1408 PAF_SIO_IALG_Config *pAlgConfig = &pObj->config;
1409 const int timeoutChanged = (*pTimeout != 2 * pBufConfig->pBufStatus->unknownTimeout);
1410 MdInt DTSHDSubType;
1412 // .........................................................................
1414 // works for both SYNC_NONE and SYNC_PCM
1415 numLeft = min (*pTimeout, GetNumAvail(pBufConfig));
1417 pTail = pBufConfig->pntr.pMdInt;
1418 pShadowTail = pBufConfig->pntr.pMdInt;
1420 // if scratch buffer present then assume it is needed for paging
1421 pageSize = numLeft*pBufConfig->sizeofElement;
1423 Log_info3("DIB: Entered DIB_syncScan with pTail = 0x%x & numLeft = %d, timeout = %d", pTail, numLeft, *pTimeout); // GJ Debug
1426 if ((pAlgConfig->numRec > 1) && pAlgConfig->pMemRec[1].base && (pAlgConfig->pMemRec[1].size >= pageSize)) {
1427 pTail = pAlgConfig->pMemRec[1].base;
1428 datId = DAT_copy ((void *) pShadowTail, (void *) pTail, pageSize);
1429 DAT_wait (datId);
1430 }
1432 if (pBufConfig->sizeofElement == 4) {
1433 Log_info0("DIB: SyncScan - Inside pBufConfig->sizeofElement == 4"); // GJ Debug
1434 stride = 2;
1435 pTail += 1; // point at MSB
1436 pShadowTail += 1;
1437 }
1438 else
1439 stride = 1;
1441 // .........................................................................
1443 // scan until out of available data or a sync found
1444 scanCount = 0;
1445 zeroCount = pDevExt->zeroCount;
1446 scanState = pDevExt->scanState;
1447 pSync = pDevExt->pSync.pMdInt;
1449 Log_info4("DIB: Entered DIB_syncScan with zeroCount = %d & scanState = %d, stride = %d, pSync = 0x%x", zeroCount, scanState, stride, pSync); // GJ Debug
1451 // scan until out of available data or a sync found
1452 for (i=0; i < numLeft; i++) {
1453 MdInt tail = pTail[i*stride];
1455 // assumes SCANNED_NONE = 0
1456 if (!scanState) {
1457 if (tail == IEC_PA) {
1458 // since above code handles ongoing sync we are
1459 // safe to check for extended sync here. i.e.
1460 // two zeros before PA.
1461 if (zeroCount >= 2) {
1462 scanState = SCANNED_IEC_PA;
1463 pSync = &pShadowTail[i*stride];
1464 }
1465 }
1466 else if (tail == DTS14_SYNC_A) {
1467 scanState = SCANNED_DTS14_SYNC_A;
1468 pSync = &pShadowTail[i*stride];
1469 }
1470 else if (tail == DTS16_SYNC_A) {
1471 scanState = SCANNED_DTS16_SYNC_A;
1472 pSync = &pShadowTail[i*stride];
1473 }
1475 // limit count to prevent wrap around
1476 zeroCount = min (zeroCount+1,INT_MAX - 1);
1477 if (tail != 0x0000)
1478 zeroCount = 0;
1480 // don't start counting until we get the first non-zero
1481 // sample while UNKNOWN. Note we don't have to worry
1482 // about the other scanCount increments since these
1483 // only occur after the first non-zero sample.
1484 //
1485 // so don't count unless
1486 // . we are already started counting (in this call) ||
1487 // . we started counting in an earlier scanForSync (timeout has changed) ||
1488 // . the last sample was non-zero
1489 if (scanCount || (tail != 0x0000) || timeoutChanged)
1490 {
1491 //Log_info3("DIB: DIB_syncScan scanCount = %d tail = %d timeoutChanged = %d", scanCount, tail, timeoutChanged); // GJ Debug
1492 scanCount += 1;
1493 }
1496 continue;
1497 }
1499 // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1501 switch (scanState) {
1503 case SCANNED_DTS16_SYNC_A:
1504 if (tail == DTS16_SYNC_B) {
1505 scanState = SCANNED_DTS16_SYNC_B;
1506 scanCount += 1;
1507 }
1508 else
1509 scanState = SCANNED_NONE;
1510 break;
1512 // wait for header data to get payload size via
1513 // nblks/fsize
1514 case SCANNED_DTS16_SYNC_B:
1515 // use extended sync
1517 if ((short)(tail & 0xFC00) == DTS16_SYNC_C) {
1518 scanState = SCANNED_DTS16_SYNC_C;
1519 scanCount += 1;
1520 }
1521 else
1522 scanState = SCANNED_NONE;
1523 break;
1525 // `````````````````````````````````````````````````````````````````````````
1526 // check for 2nd word of DTS-14 sync
1527 case SCANNED_DTS14_SYNC_A:
1528 if (tail == DTS14_SYNC_B) {
1529 scanState = SCANNED_DTS14_SYNC_B;
1530 scanCount += 1;
1531 }
1532 else
1533 scanState = SCANNED_NONE;
1534 break;
1536 // check for 3rd word of DTS-14 sync
1537 case SCANNED_DTS14_SYNC_B:
1538 // if here then looking for extended 38 bit sync
1539 if ((short)(tail & 0xFFF0) == DTS14_SYNC_C) {
1540 scanState = SCANNED_DTS14_SYNC_C;
1541 scanCount += 1;
1542 }
1543 else
1544 scanState = SCANNED_NONE;
1545 break;
1547 // wait for header data to get payload size via
1548 // nblks/fsize
1549 case SCANNED_DTS14_SYNC_C:
1550 scanState = SCANNED_DTS14_SYNC_D;
1551 scanCount += 1;
1552 break;
1554 // `````````````````````````````````````````````````````````````````````````
1555 // if here then all of header is buffered
1556 case SCANNED_DTS16_SYNC_C:
1557 case SCANNED_DTS14_SYNC_D:
1558 // update sync to point at beginning of DTS header as syncScanDTS uses this info
1559 pDevExt->scanState = scanState;
1560 pDevExt->pSync.pMdInt = pSync;
1561 status = DIB_FTABLE_syncScanDTS (device, pBufConfig, pTimeout, (XDAS_UInt16 *) &pShadowTail[i*stride]);
1562 scanState = pDevExt->scanState;
1563 if (status)
1564 return status;
1565 foundDTS = 1;
1566 if (pDevExt->syncState == SYNC_ONE)
1567 goto syncScanExit;
1568 break;
1570 // `````````````````````````````````````````````````````````````````````````
1572 // note that the round about detection of IEC only
1573 // happens for the first sync so the extra states are OK.
1574 case SCANNED_IEC_PA:
1575 if (tail == IEC_PB) {
1576 scanState = SCANNED_IEC_PB;
1577 scanCount += 1;
1578 Log_info0("DIB: SyncScan Inside case - SCANNED_IEC_PA - if path"); // GJ Debug
1579 }
1580 else
1581 {
1582 Log_info0("DIB: SyncScan Inside case - SCANNED_IEC_PA - else path"); // GJ Debug
1583 scanState = SCANNED_NONE;
1584 }
1585 break;
1587 case SCANNED_IEC_PB:
1588 // Update scanCount here since, at this point, we are confident that
1589 // this is a proper IEC stream. Regardless if we ignore it our not.
1590 // Therefore we want to properly signal that this data has been scanned.
1591 scanCount += 1;
1593 // check for IEC pause packets at this time and if required ignore them.
1594 // By construction we are guaranteed to have tail=PC at this time.
1595 if ((pBufConfig->pBufStatus->mode == MODE_NO_ZERORUNRESTART) ||
1596 (pBufConfig->pBufStatus->mode == MODE_NO_ZERORUN)) {
1597 MdInt pc = tail & 0x1F;
1599 if ((pc == 0) || (pc == 3)) {
1600 scanState = SCANNED_NONE;
1601 break;
1602 }
1603 }
1605 scanState = SCANNED_IEC_PC;
1606 break;
1608 case SCANNED_IEC_PC:
1609 pLocalTail = pSync;
1610 IncrementPtr (pBufConfig, (Ptr *) &pLocalTail, 2);
1611 pc = *pLocalTail & 0x1F;
1612 pDevExt->headerSize = IEC_HEADER_SIZE;
1614 Log_info0("DIB: Sync Scan - Inside case: SCANNED_IEC_PC"); // GJ Debug
1615 // Handle DTSHD subtype (LBR)
1616 if (pc == 0x11) {
1617 pDevExt->headerSize +=6;
1618 DTSHDSubType = (*pLocalTail & 0x700)>>8;
1619 }
1620 // DDP or THD
1621 if (pc == 21 || pc ==22) {
1622 TRACE((&TR_MOD, "Dolby: useIECSubType is 0x%x.\n", pBufConfig->pBufStatus->useIECSubType));
1623 if (pBufConfig->pBufStatus->useIECSubType == 1) {
1624 unsigned char IECSubType = *pLocalTail & 0x60;
1625 TRACE((&TR_MOD, "Dolby: IECSubType is 0x%x.\n", IECSubType));
1626 if (IECSubType != 0) {
1627 pDevExt->sourceProgram = PAF_SOURCE_UNKNOWN;
1628 pDevExt->frameLength = 0;
1629 }
1630 }
1631 }
1632 // don't know how to support other types
1633 if (pc > 22)
1634 {
1635 Log_info1("DIB: Unknown IEC type 0x%x encountered.\n", pc); // GJ Debug
1636 return DIBERR_SYNC;
1637 }
1639 pDevExt->syncState = SYNC_ONE;
1640 pBufConfig->pntr.pMdInt = pSync;
1641 pDevExt->sourceProgram = iecPafSource[pc];
1643 Log_info2("source is %d. pc is %d.", iecPafSource[pc], pc); // GJ Debug
1644 //TRACE((&TR_MOD, "source is %d. pc is %d.\n", iecPafSource[pc], pc));
1646 if (pc == 0x11 && DTSHDSubType == 3 && (PAF_ASP_sampleRateHzTable[pBufConfig->pBufStatus->sampleRateStatus][PAF_SAMPLERATEHZ_STD] <=48000.0))
1647 pDevExt->sourceProgram = PAF_SOURCE_DXP; // LBR is 23
1649 pDevExt->frameLength = pDevExt->pIECFrameLength[pc];
1650 pDevExt->lengthofData = pDevExt->frameLength;
1651 if (pc == 1)
1652 pDevExt->frameLength = 4288;
1653 else if (pc == 0x11) {
1654 pDevExt->frameLength = (pDevExt->pIECFrameLength[pc] << DTSHDSubType);
1655 pDevExt->lengthofData = pDevExt->frameLength;
1656 }
1658 goto syncScanExit;
1660 } // switch
1661 } // for
1663 // .............................................................................
1665 syncScanExit:
1666 Log_info4("DIB inside syncScanExit. pTimeout = %d, scanCount = %d, zeroCount = %d, numLeft = %d", *pTimeout,scanCount, zeroCount, numLeft ); // GJ Debug
1667 pDevExt->zeroCount = zeroCount;
1668 pDevExt->scanState = scanState;
1669 pDevExt->pSync.pMdInt = pSync;
1671 if (pDevExt->zeroCount >= 2 * pBufConfig->pBufStatus->zeroRunTrigger)
1672 pBufConfig->pBufStatus->zeroRun = 1;
1673 else
1674 pBufConfig->pBufStatus->zeroRun = 0;
1676 // If detected an initial DTS sync in a previous buffer then add the
1677 // number of samples in this buffer to the tally.
1678 // TODO: should we add numLeft instead of lengthofData?
1679 if (!foundDTS && pDevExt->numSamplesSinceDTS)
1680 pDevExt->numSamplesSinceDTS += pBufConfig->lengthofData;
1682 if (*pTimeout > scanCount)
1683 *pTimeout -= scanCount;
1684 else {
1685 *pTimeout = 0;
1686 return 0;
1687 }
1689 // This flushes the current scanned buffer if a sync is not found
1690 // Note that this code is not executed when *pTimeout = 0.
1691 // TODO: should this be moved elsewhere. Like in requestFrame?
1692 // seems like this should be done in request frame for continuous modes
1693 // and in getSync for traditional modes.
1694 // What does it mean that this is not executed when we have timed out to PCM
1695 if (pDevExt->syncState == SYNC_NONE || pDevExt->syncState == SYNC_AUTO) {
1696 IncrementPtr (pBufConfig, (Ptr *) &pBufConfig->pntr.pMdInt, numLeft);
1697 return 0;
1698 }
1700 return 0;
1701 } // DIB_syncScan
1703 // -----------------------------------------------------------------------------
1704 // Assumes scanState is SCANNED_DTS16_SYNC_C or SCANNED_DTS14_SYNC_D
1706 Int DIB_syncScanDTS (DEV2_Handle device, PAF_InpBufConfig *pBufConfig, XDAS_UInt32 *pTimeout, XDAS_UInt16 *pHeaderEnd)
1707 {
1708 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
1709 Int sourceProgram = (pDevExt->scanState == SCANNED_DTS14_SYNC_D) ? PAF_SOURCE_DTS14 : PAF_SOURCE_DTS16;
1710 float sampleRate = PAF_ASP_sampleRateHzTable[pBufConfig->pBufStatus->sampleRateStatus][PAF_SAMPLERATEHZ_STD];
1711 MdInt *pLocalTail, pc, pd;
1712 int nblks;
1715 // compute repetition rate as predicted by DTS header
1716 pLocalTail = pDevExt->pSync.pMdInt;
1717 IncrementPtr (pBufConfig, (Ptr *) &pLocalTail, 2);
1718 pc = *pLocalTail;
1719 if (pDevExt->scanState == SCANNED_DTS16_SYNC_C)
1720 nblks = (pc & 0x01FC) >> 2;
1721 else {
1722 IncrementPtr (pBufConfig, (Ptr *) &pLocalTail, 1);
1723 pd = *pLocalTail;
1724 nblks = (pc & 0x7) << 4;
1725 nblks |= (pd & 0x3C00) >> 10;
1726 }
1728 // if samplerate > 44.1k and DTS16 bit CD then report as DTS-DVD
1729 // this is a work around to the possibility that DTS-DVD is being
1730 // sent but, due to the variance in input recording, we may have
1731 // missed the IEC header. This is predicated on the enable register
1732 if (pBufConfig->pBufStatus->reportDTS16AsDTSForLargeSampleRate &&
1733 (sampleRate > 44100) && (sourceProgram == PAF_SOURCE_DTS16))
1734 sourceProgram = PAF_SOURCE_DTS;
1736 // point at LSB, if neceesary, so that space calculation is correct
1737 if (pBufConfig->sizeofElement == 4)
1738 pHeaderEnd -= 1;
1740 // If in PCM mode then require double sync, at an appropriate spacing,
1741 // in order to determine DTS validity.
1742 if (pDevExt->syncState == SYNC_PCM) {
1743 int diff;
1745 // If we have started counting, i.e. found a previous sync,
1746 // then compute sync spacing.
1747 if (pDevExt->numSamplesSinceDTS) {
1748 // determine distance since last sync
1749 // pHeaderEnd, which points at the end of the DTS header, is guaranteed
1750 // to be in the active buffer. Whereas the pointer to the beginning of the header (pSync)
1751 // may have occured in the previous buffer.
1752 diff = ((int) pHeaderEnd - (int) pBufConfig->pntr.pVoid);
1753 if (diff < 0)
1754 diff += pBufConfig->sizeofBuffer;
1755 diff /= pBufConfig->sizeofElement;
1756 diff += pDevExt->numSamplesSinceDTS;
1758 // if spacing incorrect then reset sample count to
1759 // force next conditional to be true.
1760 if (diff != (nblks+1)*32*2)
1761 pDevExt->numSamplesSinceDTS = 0;
1762 }
1764 // If this is the 1st sync detected or if this is the second sync
1765 // but the spacing between DTS syncs did not match that predicted by
1766 // NBLKS, then this is not DTS data. Therefore the previous DTS sync
1767 // word was not valid and so it is safe to reset the count based on
1768 // this secondary sync word. This latter sync may or may not be valid;
1769 // we don't know yet. In both cases init sync spacing count, reset
1770 // scan state, and continue. Note that there is a positive, albeit
1771 // quite small, probability of falsing in a pathological case where
1772 // the PCM data, interpreted as a DTS header and used to compute NBLKS,
1773 // actually matches the fake DTS syncs in the PCM file.
1774 if (!pDevExt->numSamplesSinceDTS) {
1775 diff = (int) pBufConfig->head.pVoid - (int) pHeaderEnd;
1776 if (diff <= 0)
1777 diff += pBufConfig->sizeofBuffer;
1778 diff /= pBufConfig->sizeofElement;
1779 pDevExt->numSamplesSinceDTS = diff;
1780 pDevExt->scanState = SCANNED_NONE;
1781 return 0;
1782 }
1783 } //SYNC_PCM
1785 pDevExt->lengthofData = (nblks+1)*32*2;
1786 if (pDevExt->scanState == SCANNED_DTS16_SYNC_C)
1787 pDevExt->frameLength = pDevExt->lengthofData + 4;
1788 else
1789 pDevExt->frameLength = pDevExt->lengthofData + 6;
1791 pDevExt->syncState = SYNC_ONE;
1792 pBufConfig->pntr = pDevExt->pSync;
1793 pDevExt->sourceProgram = sourceProgram;
1795 return 0;
1796 } //DIB_syncScanDTS
1798 // -----------------------------------------------------------------------------
1799 // This function is responsible for verifying bitstream sync (if applicable) and
1800 // configuring the sizes of the next frame of data.
1802 Int DIB_initFrame (DEV2_Handle device, PAF_InpBufConfig *pBufConfig)
1803 {
1804 DIB_DeviceExtension * restrict pDevExt = (DIB_DeviceExtension *) device->object;
1805 MdInt * restrict pTail;
1806 MdInt pa, pb, pc;
1807 unsigned char DTSHDSubType;
1808 PAF_UnionPointer ac3SearchPtr;
1811 // MID 810
1812 // TODO: is this needed anymore? can we combine above and this?
1813 // I don't think this is needed since pSync is guaranteed to be
1814 // valid under all cases where this function is called.
1815 if (!(pDevExt->scanState == SCANNED_DTS14_SYNC_D ||
1816 pDevExt->scanState == SCANNED_DTS16_SYNC_C ||
1817 pDevExt->scanState == SCANNED_IEC_PC ))
1818 return 0;
1820 // minimum possible distance from current IEC sync to next is 1856 words
1821 // capture this here before we update pSync following
1822 ac3SearchPtr = pDevExt->pSync;
1824 // for steady state compute expected sync location
1825 if (pDevExt->syncState == SYNC_ONGOING)
1826 IncrementPtr (pBufConfig, &pDevExt->pSync.pVoid, pBufConfig->lengthofData);
1828 pTail = pDevExt->pSync.pMdInt;
1829 pa = *pTail;
1830 IncrementPtr (pBufConfig, (Ptr *) &pTail, 1);
1831 pb = *pTail;
1832 IncrementPtr (pBufConfig, (Ptr *) &pTail, 1);
1833 pc = *pTail;
1834 IncrementPtr (pBufConfig, (Ptr *) &pTail, 1);
1836 switch (pDevExt->scanState) {
1837 // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1839 case SCANNED_DTS14_SYNC_D:
1840 // check sync (extended sync checked above for 1st sync)
1841 if ((pa != DTS14_SYNC_A) || (pb != DTS14_SYNC_B) || ((pc & 0xFC00) != (DTS14_SYNC_C & 0xFC00)))
1842 return DIBERR_SYNC;
1843 break;
1845 // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1847 case SCANNED_DTS16_SYNC_C:
1848 // check sync (extended sync checked above for 1st sync)
1849 if ((pa != DTS16_SYNC_A) || (pb != DTS16_SYNC_B))
1850 return DIBERR_SYNC;
1851 break;
1853 // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1855 case SCANNED_IEC_PC:
1856 // check for sync
1857 // special handling for AC3 variable bit rate (VBR)
1858 // start looking for sync at max payload sync location and
1859 // scan forward. Note that getSync() has waited for
1860 // sufficient data to arrive so that we can determine reliably
1861 // the presence or absence of a correct sync.
1863 if ((pa != IEC_PA) || (pb != IEC_PB)) {
1864 PAF_SIO_IALG_Obj *pObj = pDevExt->pSioIalg;
1865 PAF_SIO_IALG_Config *pAlgConfig = &pObj->config;
1866 int scan1, scan2, searchIdx, datId;
1867 const int bufEnd = (int) pBufConfig->base.pVoid + pBufConfig->sizeofBuffer;
1870 // only extend IEC search in the case of AC3
1871 if (pDevExt->sourceProgram != PAF_SOURCE_AC3)
1872 return DIBERR_SYNC;
1874 // move search ptr to earliest possible location of next sync
1875 IncrementPtr (pBufConfig, &ac3SearchPtr.pVoid, 1856);
1877 // compute number of samples between earliest possible sync location
1878 // (ac3SearchPtr) and latest possible sync location (head)
1879 scan1 = (int) pBufConfig->head.pVoid - (int) ac3SearchPtr.pVoid;
1880 if (scan1 < 0) {
1881 // here if search will wrap around so split search into two
1882 // halves to accomodate circular buffer
1883 scan1 = bufEnd - (int) ac3SearchPtr.pVoid;
1884 scan2 = (int) pBufConfig->head.pVoid - (int) pBufConfig->base.pVoid;
1885 }
1886 else
1887 scan2 = 0;
1889 // page if necessary (assume so if second memRec present)
1890 if (pAlgConfig->numRec > 1) {
1891 // if invalid buffer or if page buffer not big enough for either split then error
1892 if (!pAlgConfig->pMemRec[1].base ||
1893 (pAlgConfig->pMemRec[1].size < max(scan1,scan2)))
1894 return DIBERR_UNSPECIFIED;
1896 pTail = (MdInt *) pAlgConfig->pMemRec[1].base;
1897 datId = DAT_copy (ac3SearchPtr.pVoid, (void *) pTail, scan1);
1898 DAT_wait (datId);
1899 }
1900 else
1901 pTail = ac3SearchPtr.pMdInt;
1903 // convert to number of words
1904 scan1 /= pBufConfig->sizeofElement;
1906 // if non-zero must be IEC header, otherwise sync error
1907 // update pointer after check so that it remains
1908 // pointed at first non-zero word when breaking
1909 searchIdx = 0;
1910 while (scan1--) {
1911 if (*pTail != 0) {
1912 // force skip of any possible split scan since we found non-zero word
1913 scan2 = 0;
1914 break;
1915 }
1916 *pTail++;
1917 searchIdx++;
1918 }
1920 // perform second half of circular buffer search if necessary
1921 if (scan2) {
1922 // page if necessary, note no need to check valid buffer
1923 // or space since this is ensured in first scan
1924 if (pAlgConfig->numRec > 1) {
1925 pTail = (MdInt *) pAlgConfig->pMemRec[1].base;
1926 datId = DAT_copy (pBufConfig->base.pVoid, (void *) pTail, scan2);
1927 DAT_wait (datId);
1928 }
1929 else
1930 pTail = pBufConfig->base.pMdInt;
1932 // convert to number of words
1933 scan2 /= pBufConfig->sizeofElement;
1935 while (scan2--) {
1936 if (*pTail != 0)
1937 break;
1938 *pTail++;
1939 searchIdx++;
1940 }
1941 }
1943 // if using paging buffer then translate search pointer back into circular buffer
1944 if (pAlgConfig->numRec > 1) {
1945 pTail = ac3SearchPtr.pMdInt;
1946 IncrementPtr (pBufConfig, (Ptr *) &pTail, searchIdx);
1947 }
1949 // update sync in expection of success, if it is not a sync then no
1950 // harm since it will be ignored then reset
1951 pDevExt->pSync.pMdInt = pTail;
1953 // above search only scans for the first non-zero word.
1954 // here is common check to make sure that non-zero data is an IEC sync.
1955 pa = *pTail;
1956 IncrementPtr (pBufConfig, (Ptr *) &pTail, 1);
1957 pb = *pTail;
1958 IncrementPtr (pBufConfig, (Ptr *) &pTail, 1);
1959 pc = *pTail;
1960 IncrementPtr (pBufConfig, (Ptr *) &pTail, 1);
1961 if ((pa != IEC_PA) || (pb != IEC_PB))
1962 return DIBERR_SYNC;
1963 }
1965 // compute possible DTSHD sub type before masking pc
1966 DTSHDSubType = (pc & 0x700) >> 8;
1968 // mask pc to get data type only
1969 pc = pc & 0x1F;
1971 // don't know how to support other types
1972 // this also ensures that the below array access is bounded
1973 if (pc > 22)
1974 return DIBERR_SYNC;
1976 // continuing frame must be same as current type otherwise
1977 // we return error to force reset of decode and input
1978 // classification state machines
1979 if (pDevExt->sourceProgram != iecPafSource[pc]) {
1980 if (DTSHDSubType == 3 &&
1981 (PAF_ASP_sampleRateHzTable[pBufConfig->pBufStatus->sampleRateStatus][PAF_SAMPLERATEHZ_STD] <= 48000.0)) {
1982 if (pDevExt->sourceProgram != PAF_SOURCE_DXP)
1983 return DIBERR_SYNC;
1984 }
1985 else
1986 return DIBERR_SYNC;
1987 }
1988 break;
1990 // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1991 } //switch
1993 return 0;
1994 } // DIB_initFrame
1996 // -----------------------------------------------------------------------------
1998 Int DIB_waitForData (DEV2_Handle device, PAF_InpBufConfig *pBufConfig, XDAS_UInt32 count )
1999 {
2000 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *) device->object;
2001 DEV2_Handle pChild = (DEV2_Handle) &pDevExt->child;
2002 Int status, lock;
2004 Log_info2("DIB_waitForData.%d count = %d", __LINE__, count); // GJ Debug
2005 //TRACE((&TR_MOD, "DIB_waitForData.%d count = %d\n", __LINE__, count));
2007 while (GetNumAvail(pBufConfig) < count) {
2008 PAF_SIO_InputStatus inputStatus;
2010 // query underlying device for lock status & check lock override register
2011 // dont wait without lock
2012 status = SIO2_ctrl (pChild, (Uns)PAF_SIO_CONTROL_GET_INPUT_STATUS, (Arg) &inputStatus);
2013 if (status)
2014 {
2015 Log_info2("DIB_waitForData.%d SIO2_ctrl() returned %d", __LINE__, status); // GJ Debug
2016 //TRACE((&TR_MOD, "DIB_waitForData.%d SIO2_ctrl() returned %d\n", __LINE__, status));
2017 return status;
2018 }
2019 lock = inputStatus.lock;
2020 #ifndef IGNORE_LOCK_OVERRIDE
2021 if ((pBufConfig->pBufStatus->lockOverride & (XDAS_Int8)0x80) == 0)
2022 {
2023 Log_info1("DIB_waitForData.%d lock = lockOverride\n", __LINE__); // GJ Debug
2024 //TRACE((&TR_MOD, "DIB_waitForData.%d lock = lockOverride\n", __LINE__));
2025 lock = pBufConfig->pBufStatus->lockOverride;
2026 }
2027 #endif
2028 if (!lock)
2029 {
2030 Log_info1("DIB_waitForData.%d no lock, return DIBERR_SYNC\n", __LINE__); // GJ Debug
2031 //TRACE((&TR_MOD, "DIB_waitForData.%d no lock, return DIBERR_SYNC\n", __LINE__));
2032 return DIBERR_SYNC;
2033 }
2034 // check that decoding still requested -- allows for status
2035 // register to be updated via IOS to cancel autoProcessing
2036 if (pDevExt->pDecodeStatus) {
2037 if (pDevExt->pDecodeStatus->sourceSelect == PAF_SOURCE_NONE)
2038 {
2039 Log_info1("DIB_waitForData.%d sourceSelect is NONE, return DIBERR_SYNC", __LINE__); // GJ Debug
2040 //TRACE((&TR_MOD, "DIB_waitForData.%d sourceSelect is NONE, return DIBERR_SYNC\n", __LINE__));
2041 return DIBERR_SYNC;
2042 }
2043 }
2045 Log_info1("DIB_waitForData.%d calling DIB_FTABLE_reclaimChild()", __LINE__); // GJ Debug
2046 //TRACE((&TR_MOD, "DIB_waitForData.%d calling DIB_FTABLE_reclaimChild()\n", __LINE__));
2047 status = DIB_FTABLE_reclaimChild (device, pBufConfig);
2048 if(status)
2049 {
2050 Log_info2("DIB_waitForData.%d DIB_FTABLE_reclaimChild() returned %d", __LINE__, status); // GJ Debug
2051 //TRACE((&TR_MOD, "DIB_waitForData.%d DIB_FTABLE_reclaimChild() returned %d\n", __LINE__, status));
2052 return status;
2053 }
2054 }
2056 return 0;
2057 } // DIB_waitForData
2059 // -----------------------------------------------------------------------------
2060 #ifdef IEC_ENCODE
2062 // FS9 only supports PCM input so return error if not PCM.
2064 Int DIB_requestFrame_957 (DEV2_Handle device, PAF_InpBufConfig *pBufConfig)
2065 {
2066 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *) device->object;
2067 int i, head, tail, avail, status;
2070 if (pDevExt->sourceProgram != PAF_SOURCE_PCM)
2071 return DIBERR_SYNC;
2073 // if in steady state then update tail pointer to indicate we are done, i.e. no
2074 // longer own, the last frame of data.
2075 if (pDevExt->running > 1)
2076 IncrementPtr (pBufConfig, &pBufConfig->pntr.pVoid, pBufConfig->lengthofData);
2078 pDevExt->lengthofData = pBufConfig->stride*pDevExt->pcmFrameLength;
2079 pDevExt->frameLength = pDevExt->lengthofData;
2081 // note that due to MID 1037 it is required to check the return status after this call
2082 status = DIB_FTABLE_issueChild (device, pBufConfig, pDevExt->lengthofData, 0);
2083 if (status)
2084 return status;
2086 // add extra input delay to account for peak encoder mips
2087 for (i=0; i < 4; i++) {
2088 head = (int) pBufConfig->futureHead.pVoid;
2089 tail = (int) pBufConfig->pntr.pVoid;
2090 // compute how much data we have including outstanding requests
2091 if (head >= tail)
2092 avail = head - tail;
2093 else
2094 avail = head - tail + pBufConfig->sizeofBuffer;
2096 // convert to words
2097 avail /= pBufConfig->sizeofElement;
2098 if (avail < 4*pBufConfig->lengthofData) {
2099 status = DIB_FTABLE_issueChild (device, pBufConfig, pBufConfig->lengthofData, 0);
2100 if (status)
2101 return status;
2102 }
2103 }
2105 pBufConfig->frameLength = pDevExt->frameLength;
2106 pBufConfig->lengthofData = pDevExt->lengthofData;
2108 if (pDevExt->running < 3)
2109 pDevExt->running++;
2111 return 0;
2112 } // DIB_requestFrame_957
2114 #endif /* IEC_ENCODE */
2116 // -----------------------------------------------------------------------------
2118 #ifdef DSD_OVER_SPDIF
2120 Int DIB_requestFrame_patch (DEV2_Handle device, PAF_InpBufConfig *pBufConfig )
2121 {
2122 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
2123 int status;
2125 status = DIB_requestFrame (device, pBufConfig);
2126 // For testing DSD over SPDIF i.e. 1 pin vs actual over 6 pin
2127 pDevExt->numSamplesExtra += pDevExt->lengthofData;
2128 pDevExt->lengthofData *=6;
2129 pDevExt->numSamplesExtra -= pDevExt->lengthofData;
2131 return status;
2132 } // DIB_requestFrame
2134 Int DIB_getSync_patch (DEV2_Handle device, PAF_InpBufConfig *pBufConfig )
2135 {
2136 DIB_DeviceExtension *pDevExt = (DIB_DeviceExtension *)device->object;
2137 DEV2_Handle pChild = (DEV2_Handle)&pDevExt->child;
2138 int status;
2140 status=DIB_getSync (device, pBufConfig );
2141 if(status)
2142 return status;
2144 pBufConfig->sizeofElement = 2;
2145 status = SIO2_ctrl (pChild,(Uns)PAF_SIO_CONTROL_SET_WORDSIZE,pBufConfig->sizeofElement);
2146 if (status)
2147 return status;
2149 // compute and use *effective buffer size*
2150 {
2151 int sizeofStride = pBufConfig->sizeofElement*pBufConfig->stride;
2152 pBufConfig->sizeofBuffer = pBufConfig->allocation/sizeofStride*sizeofStride;
2153 }
2154 //..........................................................................
2156 return 0;
2157 } // DIB_getSync_patch
2159 #endif /* DSD_OVER_SPDIF */
2161 // -----------------------------------------------------------------------------