Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

472 lines
16 KiB

  1. #ifndef NABTSAPI_H
  2. #define NABTSAPI_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif /*__cplusplus*/
  6. /* Useful constants */
  7. #define NABTS_BYTES_PER_LINE 36
  8. //#define NABTS_SAMPLES_PER_LINE (pState->SamplesPerLine) /* no longer constant */
  9. #define MAX_SAMPLE_RATE 6.0
  10. #define MAX_NABTS_SAMPLES_PER_LINE ((int)(1536.0*MAX_SAMPLE_RATE/5.0))
  11. /* +++++++ make sure that sampling
  12. rates >6 give errors, or increase
  13. this number */
  14. /* "Double" is the internally used floating-point representation
  15. (currently typedef'd to "float" below).
  16. All floating point numbers exposed to the outside through the API
  17. are actual "double" values (rather than the internal "Double") */
  18. typedef float Double;
  19. #define NDSP_SIGS_TO_ACQUIRE 1
  20. enum {
  21. NDSP_ERROR_ILLEGAL_NDSP_STATE= 1,
  22. NDSP_ERROR_ILLEGAL_STATS= 2,
  23. NDSP_ERROR_ILLEGAL_FILTER= 3,
  24. NDSP_ERROR_NO_GCR= 4,
  25. NDSP_ERROR_UNSUPPORTED_SAMPLING_RATE= 5,
  26. NDSP_ERROR_ILLEGAL_VBIINFOHEADER= 6
  27. };
  28. enum {
  29. NDSP_NO_FEC= 1,
  30. NDSP_BUNDLE_FEC_1= 2,
  31. NDSP_BUNDLE_FEC_2= 3
  32. };
  33. // FP helper routines
  34. extern long __cdecl float2long(float);
  35. extern unsigned short __cdecl floatSetup();
  36. extern void __cdecl floatRestore(unsigned short);
  37. /* Globals */
  38. extern int g_nNabtsRetrainDuration; /* in frames */
  39. extern int g_nNabtsRetrainDelay; /* in frames */
  40. extern BOOL g_bUseGCR; /* enable use of the GCR signal */
  41. extern Double g_pdSync[];
  42. extern int g_nNabtsSyncSize;
  43. extern Double g_pdGCRSignal1[];
  44. extern Double g_pdGCRSignal2[];
  45. extern int g_nNabtsGcrSize;
  46. /* extern Double g_pdSync5[], g_pdGCRSignal1_5[], g_pdSignal2_5[]; */
  47. /* Forward declarations of types. */
  48. typedef struct ndsp_decoder_str NDSPState;
  49. typedef struct nfec_state_str NFECState;
  50. typedef struct ndsp_line_stats_str NDSPLineStats;
  51. typedef struct ndsp_gcr_stats_str NDSPGCRStats;
  52. typedef struct nfec_line_stats_str NFECLineStats;
  53. typedef struct nfec_bundle_str NFECBundle;
  54. typedef void (NFECCallback)(void *pContext, NFECBundle *pBundle, int groupAddr,
  55. int nGoodLines);
  56. typedef struct ndsp_gcr_str NDSPSigMatch;
  57. /* Create a new "DSP state".
  58. A separate DSP state should be maintained for each separate
  59. simultaneous source to the DSP.
  60. NDSPStartRetrain() is implicitly called upon creation of a new state.
  61. The argument must be NULL or a pointer to sizeof(NDSPState) bytes
  62. of memory. In the latter case, NDSPStateDestroy will not free this
  63. memory.
  64. */
  65. NDSPState *NDSPStateNew(void *mem);
  66. /* Destroys the DSP state.
  67. Automatically disconnects from any FEC state.
  68. Returns 0 on success.
  69. Returns NDSP_ERROR_ILLEGAL_NDSP_STATE if illegal state.
  70. */
  71. int NDSPStateDestroy(NDSPState *pState);
  72. /*
  73. Connect the given NFECState and NDSPState
  74. For cases where the NDSP and NFEC modules are connected,
  75. giving pointers to the connected state may result in increased
  76. robustness and efficiency.
  77. Note that only one of
  78. NDSPStateConnectToFEC or
  79. NFECStateConnectToDSP
  80. need be called to connect the two states. (Calling both is OK).
  81. Returns 0 on success.
  82. Returns NDSP_ERROR_ILLEGAL_NDSP_STATE if illegal DSP state.
  83. */
  84. int NDSPStateConnectToFEC(NDSPState *pDSPState, NFECState *pFECState);
  85. int NFECStateConnectToDSP(NFECState *pFECState, NDSPState *pDSPState);
  86. /*
  87. Tells the DSP to initiate a "fast retrain". This is useful if you
  88. suspect that conditions have changed sufficiently to be worth spending
  89. significant CPU to quickly train on a signal.
  90. This should be called when the source of video changes.
  91. Returns 0 on success.
  92. Returns NDSP_ERROR_ILLEGAL_NDSP_STATE if illegal DSP state.
  93. */
  94. int NDSPStartRetrain(NDSPState *pState);
  95. /*
  96. * Inputs:
  97. * pbSamples: pointer to 8-bit raw NABTS samples
  98. * pState: NDSPState to use for decoding
  99. * nFECType: Can be set to:
  100. * NDSP_NO_FEC (don't use FEC information)
  101. * NDSP_BUNDLE_FEC_1 (use Norpak-style bundle FEC info)
  102. * NDSP_BUNDLE_FEC_2 (use Wavephore-style bundle FEC info)
  103. * nFieldNumber:
  104. * A number that increments by one for each successive field.
  105. * "Odd" fields (as defined by NTSC) must be odd numbers
  106. * "Even" fields must be even numbers.
  107. * nLineNumber:
  108. * The NTSC line (starting from the top of the field)
  109. * from which this sample was taken.
  110. *
  111. * Outputs:
  112. * pbDest: decoded data ("NABTS_BYTES_PER_LINE" (36) bytes long)
  113. * pLineStats: stats on decoded data
  114. *
  115. * Errors:
  116. *
  117. * Returns 0 if no error
  118. * Returns NDSP_ERROR_ILLEGAL_NDSP_STATE if state is illegal or uses
  119. * unsupported settings
  120. * Returns NDSP_ERROR_ILLEGAL_STATS if stats is passed incorrectly
  121. *
  122. * Notes:
  123. * pbDest must point to a buffer at least 36 bytes long
  124. * pLineStats->nSize must be set to sizeof(*pLineStats) prior to call
  125. * (to maintain backwards compatibility should we add fields in the future)
  126. * pLineStats->nSize will be set to the size actually filled in by
  127. * the call (the number will stay the same or get smaller)
  128. * Currently the routine only supports a pState with an FIR filter
  129. * that has 5 taps
  130. * nFECType is currently unused, but would potentially be used to give
  131. * FEC feedback to the DSP decode, for possible tuning and/or retry
  132. */
  133. int NDSPDecodeLine(unsigned char *pbDest, NDSPLineStats *pLineStats,
  134. unsigned char *pbSamples, NDSPState *pState,
  135. int nFECType,
  136. int nFieldNumber, int nLineNumber,
  137. KS_VBIINFOHEADER *pVBIINFO);
  138. /* typedef'd as NDSPLineStats above */
  139. struct ndsp_line_stats_str {
  140. int nSize; /* Should be set to the size of this structure.
  141. Used to maintain backwards compatibility as we add fields */
  142. int nConfidence; /* Set to 0-100 as a measure of expected reliability.
  143. Low numbers are caused either by a noisy signal, or
  144. if the line is in fact not NABTS */
  145. int nFirstBit; /* The position of the center of
  146. the first sync bit sample */
  147. double dDC; /* the calculated DC offset used to threshold samples.
  148. This is dependent on the current way we decode NABTS
  149. and may not be used in the future */
  150. };
  151. /* typedef'd as NDSPGCRStats above */
  152. struct ndsp_gcr_stats_str {
  153. int nSize; /* Should be set to the size of this structure.
  154. Used to maintain backwards compatibility as we add fields */
  155. BOOL bUsed; /* Was this line used by calculations?
  156. If FALSE, none of the values below are valid. */
  157. int nConfidence; /* Set to 0-100 as a measure of expected reliability.
  158. Low numbers are typically caused by a noisy signal.
  159. A confidence less than 50 means the algorithm
  160. decided that this line was far enough from being a
  161. GCR line that it wasn't used as such */
  162. };
  163. /* Like NDSPDecodeLine, but only decode only from nStart to nEnd bytes.
  164. By decoding the first 3 bytes (sync), a confidence measure can be made.
  165. By decoding the next 3 bytes, the group ID can be had.
  166. */
  167. int NDSPPartialDecodeLine(unsigned char *pbDest, NDSPLineStats *pLineStats,
  168. unsigned char *pbSamples, NDSPState *pState,
  169. int nFECType,
  170. int nFieldNumber, int nLineNumber,
  171. int nStart, int nEnd,
  172. KS_VBIINFOHEADER *pVBIINFO);
  173. /* Hamming-decode a single byte.
  174. Useful for decoding group addresses in NABTS data
  175. Hamming decoding can fix a one-bit error and detect (but not
  176. correct) a two-bit error.
  177. *nBitErrors is set to the number of detected bit errors in the byte.
  178. if *nBitErrors is 0 or 1, the hamming-decoded value (from 0 to 15)
  179. is returned.
  180. if *nBitErrors is 2, -1 is returned.
  181. */
  182. int NFECHammingDecode(unsigned char bByte, int *nBitErrors);
  183. /* Get NABTS group address from NABTS decoded line.
  184. *nBitErrors is set to the total number of errors detected in the three
  185. group address bytes.
  186. If address is correctable, *bCorrectable is set to TRUE, and an address
  187. between 0 and 4095 is returned.
  188. If address is not correctable, -1 is returned.
  189. This only works for standard, fully NABTS-compliant packets
  190. */
  191. int NFECGetGroupAddress(NFECState *nState, unsigned char *bData, int *nBitErrors);
  192. /*
  193. * Conditionally process raw samples from a GCR line and modify
  194. * the NDSP state accordingly.
  195. *
  196. * This routine should be called with the GCR line from each incoming field.
  197. * (NTSC line 19)
  198. *
  199. * Even if the GCR line is known to not be present in the signal, you
  200. * should call this function once per frame anyway. If no GCR is found,
  201. * this function will cause an equalization based on the NABTS sync bytes.
  202. *
  203. * This routine will not process all lines passed to it. The frequency
  204. * with which is processes lines depends on the current NDSP state,
  205. * including whether a fast retrain is currently being performed.
  206. *
  207. * Inputs:
  208. * pbSamples: pointer to 8-bit raw samples
  209. * pState: NDSPState to use for decoding
  210. * nFieldNumber:
  211. * A number that increments by one for each successive field.
  212. * "Odd" fields (as defined by NTSC) must be odd numbers
  213. * "Even" fields must be even numbers.
  214. * nLineNumber:
  215. * The NTSC line (starting from the top of the field)
  216. * from which this sample was taken.
  217. *
  218. * Outputs:
  219. * pbDest:
  220. * pLineStats: stats on decoded data
  221. * Errors:
  222. *
  223. * Returns 0 if no error
  224. * Returns NDSP_ERROR_ILLEGAL_NDSP_STATE if state is illegal or uses
  225. * unsupported settings
  226. * Returns NDSP_ERROR_ILLEGAL_STATS if stats is passed incorrectly
  227. *
  228. * Notes:
  229. * pbDest must point to a buffer at least 36 bytes long
  230. * pLineStats->nSize must be set to sizeof(*pLineStats) prior to call
  231. * (to maintain backwards compatibility should we add fields in the future)
  232. * pLineStats->nSize will be set to the size actually filled in by
  233. * the call (the number will stay the same or get smaller)
  234. */
  235. int NDSPProcessGCRLine(NDSPGCRStats *pLineStats,
  236. unsigned char *pbSamples, NDSPState *pState,
  237. int nFieldNumber, int nLineNumber,
  238. KS_VBIINFOHEADER *pVBIINFO);
  239. #define NDSP_MAX_FIR_COEFFS 50
  240. #define NDSP_MAX_FIR_TAIL MAX_NABTS_SAMPLES_PER_LINE
  241. struct variable_tap_str {
  242. int nTapLoc;
  243. Double dTap;
  244. };
  245. typedef struct fir_filter_str {
  246. BOOL bVariableTaps;
  247. int nTaps; /* must be of form 2n+1 if bVariableTaps is zero */
  248. Double dTapSpacing; /* Only for bVariableTaps = FALSE */
  249. int nMinTap; /* Only for bVariableTaps = TRUE */
  250. int nMaxTap; /* Only for bVariableTaps = TRUE */
  251. Double pdTaps[NDSP_MAX_FIR_COEFFS];
  252. int pnTapLocs[NDSP_MAX_FIR_COEFFS];
  253. } FIRFilter;
  254. /* typedef'd as NDSPSigMatch above */
  255. struct ndsp_gcr_str {
  256. Double dMaxval;
  257. int nOffset;
  258. unsigned char pbSamples[MAX_NABTS_SAMPLES_PER_LINE];
  259. };
  260. /* typedef'd as NDSPState above */
  261. struct ndsp_decoder_str {
  262. unsigned int uMagic; /* must be set to NDSP_STATE_MAGIC */
  263. int nRetrainState;
  264. int nUsingGCR; /* 0 for no GCR, > 0 means using GCR */
  265. FIRFilter filter;
  266. /*
  267. unsigned char pbBestGCRLine[MAX_NABTS_SAMPLES_PER_LINE];
  268. Double dBestGCRLineVal;
  269. */
  270. NDSPSigMatch psmPosGCRs[NDSP_SIGS_TO_ACQUIRE];
  271. NDSPSigMatch psmNegGCRs[NDSP_SIGS_TO_ACQUIRE];
  272. NDSPSigMatch psmSyncs[NDSP_SIGS_TO_ACQUIRE];
  273. BOOL bFreeStateMem;
  274. BOOL bUsingScratch1;
  275. Double pdScratch1[MAX_NABTS_SAMPLES_PER_LINE+NDSP_MAX_FIR_TAIL*2];
  276. BOOL bUsingScratch2;
  277. Double pdScratch2[MAX_NABTS_SAMPLES_PER_LINE+NDSP_MAX_FIR_TAIL*2];
  278. FIRFilter filterScratch3;
  279. FIRFilter filterGCREqualizeTemplate;
  280. FIRFilter filterNabsyncEqualizeTemplate;
  281. BOOL bUsingScratch3;
  282. Double pdScratch3[NDSP_MAX_FIR_COEFFS*NDSP_MAX_FIR_COEFFS];
  283. BOOL bUsingScratch4;
  284. Double pdScratch4[NDSP_MAX_FIR_COEFFS];
  285. BOOL bUsingScratch5;
  286. Double pdScratch5[MAX_NABTS_SAMPLES_PER_LINE+NDSP_MAX_FIR_TAIL*2];
  287. BOOL bUsingScratch6;
  288. Double pdScratch6[MAX_NABTS_SAMPLES_PER_LINE+NDSP_MAX_FIR_TAIL*2];
  289. BOOL bUsingScratch7;
  290. Double pdScratch7[MAX_NABTS_SAMPLES_PER_LINE+NDSP_MAX_FIR_TAIL*2];
  291. BOOL bUsingScratch8;
  292. Double pdScratch8[MAX_NABTS_SAMPLES_PER_LINE+NDSP_MAX_FIR_TAIL*2];
  293. Double dSampleRate;
  294. Double dSampleFreq;
  295. long SamplesPerLine;
  296. };
  297. /* Create a new "FEC state".
  298. A separate FEC state should be maintained for each separate
  299. simultaneous source to the FEC.
  300. Returns NULL on failure (i.e., out of memory).
  301. */
  302. NFECState *NFECStateNew();
  303. /* Set the sampling rate of a state to a new value */
  304. int NDSPStateSetSampleRate(NDSPState* pState, unsigned long samp_rate);
  305. /* Destroys the FEC state.
  306. Automatically disconnects from any DSP state. */
  307. void NFECStateDestroy(NFECState *nState);
  308. /* Set a list of group addresses to listen to. If pGroupAddrs is NULL,
  309. then listen to all group addresses. Returns nonzero on success, zero
  310. on failure. (On failure, the state is not changed.)
  311. */
  312. int NFECStateSetGroupAddrs(NFECState *pState, int *pGroupAddrs,
  313. int nGroupAddrs);
  314. /* You pass in a line (36 bytes, as received from the DSP), a pointer
  315. to a NFECLineStats structure, and a callback function (to say what to
  316. do with the data). The prototype for the callback function is given
  317. above. The callback is responsible for freeing its pBundle argument. */
  318. void NFECDecodeLine(unsigned char *line,
  319. int confidence,
  320. NFECState *pState,
  321. NFECLineStats *pLineStats,
  322. NFECCallback *cb,
  323. void *pContext);
  324. typedef enum {NFEC_OK, NFEC_GUESS, NFEC_BAD} NFECPacketStatus;
  325. typedef enum {NFEC_LINE_OK, NFEC_LINE_CHECKSUM_ERR, NFEC_LINE_CORRUPT} NFECLineStatus;
  326. struct nfec_line_stats_str {
  327. int nSize; /* Should be set to the size of this structure.
  328. Used to maintain backwards compatibility as we add fields */
  329. NFECLineStatus status; /* This will be filled in with the status of
  330. the passed-in line. The possible values are:
  331. NFEC_LINE_OK: The line passed all tests; it is
  332. almost certainly a valid, FEC'd NABTS line.
  333. NFEC_LINE_CHECKSUM_ERR: We were able to
  334. munge the line until it looked valid; we're going
  335. to attempt to make use of this line.
  336. NFEC_LINE_CORRUPT: We were unable to guess
  337. how to change this line to make it valid; it
  338. will be discarded.
  339. (The statistics here are subject to change.) */
  340. };
  341. typedef struct nfec_packet_str {
  342. unsigned char data[28]; /* The actual data in this packet. */
  343. int len; /* The amount of useful data in this packet
  344. (excluding FEC and filler). Will usually
  345. be either 0 or 26, but may vary in
  346. the presence of filler. */
  347. NFECPacketStatus status; /* The status of this packet; some indication
  348. of whether the packet is valid. (This
  349. is subject to change.) */
  350. #ifdef DEBUG_FEC
  351. unsigned char line;
  352. unsigned long frame;
  353. #endif //DEBUG_FEC
  354. } NFECPacket;
  355. /* typedef'd as NFECBundle above */
  356. struct nfec_bundle_str {
  357. NFECPacket packets[16];
  358. int nBitErrors; /* The number of bits changed by the FEC
  359. in this bundle (not counting bits
  360. corrected in missing lines). */
  361. int lineConfAvg; /* Average of all line confidences presented */
  362. };
  363. /* Flushes any remaining data from the FEC state (i.e. unfinished
  364. bundles). Calls "cb" with any data returned */
  365. void NFECStateFlush(NFECState *pState, NFECCallback *cb, void *pContext);
  366. /* Garbage collect streams. If you want to time out bundles,
  367. * call NFECGarbageCollect() every field.
  368. */
  369. void NFECGarbageCollect(NFECState *pState, NFECCallback *cb, void *pContext);
  370. #ifdef __cplusplus
  371. } // end - extern "C"
  372. #endif /*__cplusplus*/
  373. #endif /*NABTSAPI_H*/