1/*
2 * Copyright (c) 1998-2014 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License").  You may not use this file except in compliance with the
9 * License.  Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23#ifndef _IOAUDIOTIMEINTERVALFILTER_H
24#define _IOAUDIOTIMEINTERVALFILTER_H
25
26#include "BigNum128.h"
27
28/*!
29 @class IOAudioTimeIntervalFilter
30 @abstract An abstract class that provides a filtered timeline based on snapshots from jittery time captures
31 */
32class IOAudioTimeIntervalFilter : public OSObject
33{
34	OSDeclareAbstractStructors(IOAudioTimeIntervalFilter)
35
36public:
37	/*!
38	 @function reInitialiseFilter
39	 @abstract Restart a new timeline sequence, with a new expected interval spacing
40	 @param expectedInterval Expected interval of time captures. Pass zero to use the results from previous runs.
41	 @param multiIntervalCount Count of multiple intervals to return from getMultiIntervalTime.
42	 */
43	virtual IOReturn reInitialiseFilter(uint32_t expectedInterval = 0, uint32_t multiIntervalCount = 1 );
44
45    /*!
46     * @function free
47     * @abstract Frees all of the resources allocated by the IOAudioTimeIntervalFilter.
48     * @discussion Do not call this directly.  This is called automatically by the system when the instance's
49     *  refcount goes to 0.  To decrement the refcount, call release() on the object.
50     */
51    virtual void free();
52
53
54
55	/*!
56	 @function newTimePosition
57	 @abstract Pass in the raw measured time position
58	 @param rawSnapshot The raw time position. These should be approximately occurring every ExpectedInterval
59	 @result A filtered time position
60	 */
61
62	virtual AbsoluteTime newTimePosition(AbsoluteTime rawSnapshot);
63
64	/*!
65	 @function getMultiIntervalTime
66	 @abstract Return the time between the last MultiIntervalCount intervals of the filtered timeline
67	 @result Return the time between the last MultiIntervalCount intervals of the filtered timeline
68	 */
69	virtual uint64_t getMultiIntervalTime(void);
70
71
72	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 0 );
73	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 1 );
74	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 2 );
75	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 3 );
76	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 4 );
77	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 5 );
78	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 6 );
79	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 7 );
80	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 8 );
81	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 9 );
82	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 10 );
83	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 11 );
84	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 12 );
85	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 13 );
86	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 14 );
87	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilter, 15 );
88
89protected:
90	/* <rdar://12136103> */
91    struct ExpansionData
92	{
93	};
94
95    ExpansionData   *reserved;
96
97	/*!
98	 @function initFilter
99	 @abstract Construct a new instance of the TimeFilter class
100	 @param ExpectedInterval Expected interval of time captures
101	 @param MultiIntervalCount Optionally calculate the count between ExpectedInterval
102	 */
103	virtual bool initFilter(uint32_t expectedInterval, uint32_t multiIntervalCount = 1);
104
105	/*!
106	 @function calculateNewTimePosition
107	 @abstract abstract method to calculate the new time position based on the raw snapshot
108	 @param rawSnapshot Raw filter value
109	 @result filtered time value
110	 */
111	virtual uint64_t calculateNewTimePosition(uint64_t rawSnapshot) = 0;
112
113	inline int decCircularBufferPosition(int n, int dec = 1)	{ return (n + mMultiIntervalCount - dec) % mMultiIntervalCount;  }
114	inline int incCircularBufferPosition(int n, int inc = 1)	{ return (n + mMultiIntervalCount + inc) % mMultiIntervalCount;  }
115
116	uint32_t	mExpectedInterval;
117	uint32_t	mMultiIntervalCount;
118
119	uint64_t*	mIntervalTimeHistory;
120
121    /*!
122     * @var mIntervalTimeHistoryPointer
123     *  Points to the next time interval to be updated
124     */
125	int			mIntervalTimeHistoryPointer;
126
127    /*!
128     * @var mFilterCount
129     *  How many times the filter has been called since re-init
130     */
131	uint64_t	mFilterCount;
132
133    IOLock*		timeIntervalLock;
134};
135
136
137
138/*!
139 @class IOAudioTimeIntervalFilterIIR
140 @abstract A concrete IOAudioTimeIntervalFilter class that provides an IIR-based filtered timeline based on snapshots from jittery time captures
141 */
142
143class IOAudioTimeIntervalFilterIIR : public IOAudioTimeIntervalFilter
144{
145    OSDeclareDefaultStructors(IOAudioTimeIntervalFilterIIR)
146
147public:
148	/*!
149	 @function initFilter
150	 @abstract Construct a new instance of the IIR TimeFilter class
151	 @param ExpectedInterval Expected interval of time captures
152	 @param MultiIntervalCount Optionally calculate the count between ExpectedInterval
153	 @param filterCoef IIR filter coefficient. Increase this number for more aggressive smoothing
154	 */
155	virtual bool initFilter(uint32_t expectedInterval, uint32_t multiIntervalCount = 1, uint16_t filterCoef = 4);
156
157	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 0 );
158	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 1 );
159	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 2 );
160	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 3 );
161	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 4 );
162	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 5 );
163	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 6 );
164	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 7 );
165	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 8 );
166	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 9 );
167	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 10 );
168	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 11 );
169	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 12 );
170	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 13 );
171	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 14 );
172	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterIIR, 15 );
173
174protected:
175	virtual void IIR(U128* filterVal, U128 input, int shiftAmount);
176	virtual uint64_t calculateNewTimePosition(uint64_t rawSnapshot);
177
178	U128		mFilteredSnapshot;
179	U128		mFilteredOffset;
180	uint16_t	mIIRCoef;
181};
182
183
184
185/*!
186 @class IOAudioTimeIntervalFilterFIR
187 @abstract A concrete IOAudioTimeIntervalFilter class that provides an FIR-based filtered timeline based on snapshots from jittery time captures
188 */
189
190class IOAudioTimeIntervalFilterFIR : public IOAudioTimeIntervalFilter
191{
192    OSDeclareDefaultStructors(IOAudioTimeIntervalFilterFIR)
193
194public:
195	/*!
196	 @function initFilter
197	 @abstract Construct a new instance of the IIR TimeFilter class
198	 @param ExpectedInterval Expected interval of time captures
199	 @param MultiIntervalCount Optionally calculate the count between ExpectedInterval
200	 */
201	virtual bool initFilter(uint32_t expectedInterval, uint32_t multiIntervalCount = 1);
202
203    /*!
204     * @function free
205     * @abstract Frees all of the resources allocated by the IOAudioTimeIntervalFilter.
206     * @discussion Do not call this directly.  This is called automatically by the system when the instance's
207     *  refcount goes to 0.  To decrement the refcount, call release() on the object.
208     */
209    virtual void free();
210
211	IOReturn reInitialiseFilter(uint32_t expectedInterval = 0, uint32_t multiIntervalCount = 1 );
212
213	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 0 );
214	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 1 );
215	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 2 );
216	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 3 );
217	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 4 );
218	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 5 );
219	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 6 );
220	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 7 );
221	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 8 );
222	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 9 );
223	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 10 );
224	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 11 );
225	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 12 );
226	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 13 );
227	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 14 );
228	OSMetaClassDeclareReservedUnused (IOAudioTimeIntervalFilterFIR, 15 );
229protected:
230
231	virtual uint64_t calculateNewTimePosition(uint64_t rawSnapshot);
232	virtual IOReturn setNewFilter(uint32_t numCoeffs, const uint64_t* filterCoefficients, uint32_t scale);
233
234	U128 FIR(uint64_t *history, uint64_t input);
235
236	uint64_t*	mCoeffs;
237	uint64_t*	mDataOffsetHistory;
238	uint64_t*	mDataHistory;
239	uint32_t	mNumCoeffs;
240	uint32_t	mFilterScale;
241	uint32_t	mFilterWritePointer;
242};
243
244#endif		// _IOAUDIOTIMEINTERVALFILTER_H
245
246