AJA NTV2 SDK  17.1.1.1245
NTV2 SDK 17.1.1.1245
timebase.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
11 
13 {
15  int64_t frameTimeScale;
16  int64_t frameDuration;
17 };
19 {
20  {AJA_FrameRate_1498, 15000, 1001},
21  {AJA_FrameRate_1500, 15000, 1000},
22  {AJA_FrameRate_1798, 18000, 1001},
23  {AJA_FrameRate_1800, 18000, 1000},
24  {AJA_FrameRate_1898, 19000, 1001},
25  {AJA_FrameRate_1900, 19000, 1000},
26  {AJA_FrameRate_2398, 24000, 1001},
27  {AJA_FrameRate_2400, 24000, 1000},
28  {AJA_FrameRate_2500, 25000, 1000},
29  {AJA_FrameRate_2997, 30000, 1001},
30  {AJA_FrameRate_3000, 30000, 1000},
31  {AJA_FrameRate_4795, 48000, 1001},
32  {AJA_FrameRate_4800, 48000, 1000},
33  {AJA_FrameRate_5000, 50000, 1000},
34  {AJA_FrameRate_5994, 60000, 1001},
35  {AJA_FrameRate_6000, 60000, 1000},
36  {AJA_FrameRate_10000,100000,1000},
37  {AJA_FrameRate_11988,120000,1001},
38  {AJA_FrameRate_12000,120000,1000}
39 };
41 
42 const int64_t AJATimeBaseDefaultTimeScale = 30000;
43 const int64_t AJATimeBaseDefaultDuration = 1001;
44 const int64_t AJATimeBaseDefaultAudioRate = 48000;
45 
47 {
48  SetToDefault();
49 }
50 
52 {
53  SetToDefault();
54  // this will set the correct timeScale and duration
55  SetAJAFrameRatePrivate(ajaFrameRate);
56 }
57 
58 AJATimeBase::AJATimeBase(int64_t frameTimeScale, int64_t frameDuration)
59 {
60  SetToDefault();
61  mFrameTimeScale = frameTimeScale;
62  mFrameDuration = frameDuration;
63 }
64 
65 AJATimeBase::AJATimeBase(int64_t frameTimeScale, int64_t frameDuration, int64_t audioRate)
66 {
67  SetToDefault();
68  mFrameTimeScale = frameTimeScale;
69  mFrameDuration = frameDuration;
70  mAudioRate = audioRate;
71 }
72 
74 {
75  mFrameTimeScale = other.mFrameTimeScale;
76  mFrameDuration = other.mFrameDuration;
77  mAudioRate = other.mAudioRate;
78  mTickRate = other.mTickRate;
79 }
80 
82 {
83 }
84 
86 {
87  mFrameTimeScale = AJATimeBaseDefaultTimeScale;
88  mFrameDuration = AJATimeBaseDefaultDuration;
89  mAudioRate = AJATimeBaseDefaultAudioRate;
90  mTickRate = AJATime::GetSystemFrequency();
91 }
92 
94 {
95  if (this != &t)
96  {
97  mFrameTimeScale = t.mFrameTimeScale;
98  mFrameDuration = t.mFrameDuration;
99  mAudioRate = t.mAudioRate;
100  mTickRate = t.mTickRate;
101  }
102 
103  return *this;
104 }
105 
107 {
108  bool bIsSame = true;
109 
110  if (mFrameTimeScale != b.mFrameTimeScale)
111  bIsSame = false;
112  if (mFrameDuration != b.mFrameDuration)
113  bIsSame = false;
114  if (mAudioRate != b.mAudioRate)
115  bIsSame = false;
116  if (mTickRate != b.mTickRate)
117  bIsSame = false;
118 
119  return bIsSame;
120 }
121 
123 {
124  return !(*this == a);
125 }
126 
127 void AJATimeBase::SetFrameRate(int64_t frameTimeScale, int64_t frameDuration)
128 {
129  mFrameTimeScale = frameTimeScale;
130  mFrameDuration = frameDuration;
131 }
132 
133 void AJATimeBase::GetFrameRate(int64_t& frameTimeScale, int64_t& frameDuration) const
134 {
135  frameTimeScale = mFrameTimeScale;
136  frameDuration = mFrameDuration;
137 }
138 
140 {
141  return mFrameTimeScale;
142 }
143 
144 void AJATimeBase::SetFrameTimeScale(int64_t timeScale)
145 {
146  mFrameTimeScale = timeScale;
147 }
148 
149 int64_t AJATimeBase::GetFrameDuration(void) const
150 {
151  return mFrameDuration;
152 }
153 
154 void AJATimeBase::SetAudioRate(int64_t rate)
155 {
156  mAudioRate = rate;
157 }
158 
160 {
161  return mAudioRate;
162 }
163 
165 {
166  SetAJAFrameRatePrivate(ajaFrameRate);
167 }
168 
169 void AJATimeBase::SetTickRate(int64_t rate)
170 {
171  if (rate == 0)
172  {
173  // set to native rate
174  mTickRate = AJATime::GetSystemFrequency();
175  }
176  else
177  {
178  mTickRate = rate;
179  }
180 }
181 
183 {
184  return mTickRate;
185 }
186 
187 int64_t AJATimeBase::FramesToSamples(int64_t frames, bool round) const
188 {
189  return Convert(frames, mFrameTimeScale, mFrameDuration, mAudioRate, 1, round, true);
190 }
191 
192 int64_t AJATimeBase::FramesToTicks(int64_t frames, bool round) const
193 {
194  return Convert(frames, mFrameTimeScale, mFrameDuration, mTickRate, 1, round, true);
195 }
196 
197 double AJATimeBase::FramesToSeconds(int64_t frames) const
198 {
199  return (double)frames * (double)mFrameDuration / (double)mFrameTimeScale;
200 }
201 
202 double AJATimeBase::ScaleUnitsToSeconds(int64_t scaleUnits) const
203 {
204  return (double)scaleUnits / (double)mFrameTimeScale;
205 }
206 
208 {
209  float val = 0.f;
210  if (mFrameDuration)
211  val = (float)mFrameTimeScale / (float)mFrameDuration;
212  return val;
213 }
214 
216 {
217  double val = 0.;
218  if (mFrameDuration)
219  val = (double)mFrameTimeScale / (double)mFrameDuration;
220  return val;
221 }
222 
223 int64_t AJATimeBase::FramesToMicroseconds(int64_t frames, bool round) const
224 {
225  return Convert(frames, mFrameTimeScale, mFrameDuration, 1000000, 1, round, true);
226 }
227 
228 int64_t AJATimeBase::FramesToNanoseconds100(int64_t frames, bool round) const
229 {
230  return Convert(frames, mFrameTimeScale, mFrameDuration, 10000000, 1, round, true);
231 }
232 
233 int64_t AJATimeBase::SamplesToFrames(int64_t samples, bool round) const
234 {
235  return Convert(samples, mAudioRate, 1, mFrameTimeScale, mFrameDuration, round, true);
236 }
237 
238 int64_t AJATimeBase::SamplesToTicks(int64_t samples, bool round) const
239 {
240  return Convert(samples, mAudioRate, mTickRate, round, true);
241 }
242 
243 double AJATimeBase::SamplesToSeconds(int64_t samples)
244 {
245  return (double)samples / (double)mAudioRate;
246 }
247 
248 int64_t AJATimeBase::SamplesToMicroseconds(int64_t samples, bool round)
249 {
250  return Convert(samples, mAudioRate, 1000000, round, true);
251 }
252 
253 int64_t AJATimeBase::TicksToFrames(int64_t ticks, bool round)
254 {
255  return Convert(ticks, mTickRate, 1, mFrameTimeScale, mFrameDuration, round, true);
256 }
257 
258 int64_t AJATimeBase::TicksToSamples(int64_t ticks, bool round)
259 {
260  return Convert(ticks, mTickRate, mAudioRate, round, true);
261 }
262 
263 double AJATimeBase::TicksToSeconds(int64_t ticks)
264 {
265  return (double)ticks / (double)mTickRate;
266 }
267 
268 int64_t AJATimeBase::TicksToMicroseconds(int64_t ticks, bool round)
269 {
270  return Convert(ticks, mTickRate, 1000000, round, true);
271 }
272 
273 int64_t AJATimeBase::SecondsToFrames(double seconds, bool round)
274 {
275  return Convert((int64_t)(seconds * 1000000000.0), 1000000000, 1, mFrameTimeScale, mFrameDuration, round, true);
276 }
277 
278 int64_t AJATimeBase::SecondsToSamples(double seconds, bool round)
279 {
280  return Convert((int64_t)(seconds * 1000000000.0), 1000000000, mAudioRate, round, true);
281 }
282 
283 int64_t AJATimeBase::SecondsToTicks(double seconds, bool round)
284 {
285  return Convert((int64_t)(seconds * 1000000000.0), 1000000000, mTickRate, round, true);
286 }
287 
288 int64_t AJATimeBase::MicrosecondsToFrames(int64_t microseconds, bool round)
289 {
290  return Convert(microseconds, 1000000, 1, mFrameTimeScale, mFrameDuration, round, true);
291 }
292 
293 int64_t AJATimeBase::Nanoseconds100ToFrames(int64_t nanoseconds100, bool round)
294 {
295  return Convert(nanoseconds100, 10000000, 1, mFrameTimeScale, mFrameDuration, round, true);
296 }
297 
298 int64_t AJATimeBase::MicrosecondsToSamples(int64_t microseconds, bool round)
299 {
300  return Convert(microseconds, 1000000, mAudioRate, round, true);
301 }
302 
303 int64_t AJATimeBase::MicrosecondsToTicks(int64_t microseconds, bool round)
304 {
305  return Convert(microseconds, 1000000, mTickRate, round, true);
306 }
307 
308 int64_t AJATimeBase::SecondsToMicroseconds(double seconds, bool round)
309 {
310  return Convert((int64_t)(seconds * 1000000.0), 1000000, 1000000, round, false);
311 }
312 
313 double AJATimeBase::MicrosecondsToSeconds(int64_t microseconds)
314 {
315  return (double)microseconds / 1000000.0;
316 }
317 
318 bool AJATimeBase::IsCloseTo(const AJATimeBase &timeBase) const
319 {
320  bool bIsClose = false;
321  double rate1 = FramesToSeconds(1);
322  double rate2 = timeBase.FramesToSeconds(1);
323  double dif = rate1 / rate2;
324 
325  // this is just a guess at what is considered close enough
326  // it will differentiate between 30000/1001 and 30000/1000,
327  // but 2997/100 is *not* close to 30000/1001
328  if ((dif >= .9999) && (dif <= 1.0001))
329  bIsClose = true;
330  return bIsClose;
331 }
332 
333 bool AJATimeBase::IsCloseTo(int64_t frameTimeScale, int64_t frameDuration) const
334 {
335  AJATimeBase tb(frameTimeScale,frameDuration);
336  return IsCloseTo(tb);
337 }
338 
340 {
341  return ((mFrameTimeScale % mFrameDuration) != 0) ? true : false;
342 }
343 
345 {
346  int64_t ticks = AJATime::GetSystemCounter();
347  int64_t rate = AJATime::GetSystemFrequency();
348 
349  if (rate != mTickRate)
350  {
351  ticks = Convert(ticks, rate, mTickRate, false, true);
352  }
353 
354  return ticks;
355 }
356 
357 int64_t AJATimeBase:: Convert(int64_t inValue, int64_t inRate, int64_t outRate, bool round, bool large)
358 {
359  int64_t outValue = 0;
360 
361  if (round)
362  {
363  // convert half the out rate to the in rate and increase the in value
364  int64_t roundValue = inRate / (outRate * 2);
365  if (inValue > 0)
366  {
367  inValue += roundValue;
368  }
369  else
370  {
371  inValue -= roundValue;
372  }
373  }
374 
375  if (large)
376  {
377  // this will not overflow
378  outValue = inValue / inRate * outRate;
379  outValue += (inValue % inRate) * outRate / inRate;
380  }
381  else
382  {
383  // this has better performance
384  outValue = inValue * outRate / inRate;
385  }
386 
387  return outValue;
388 }
389 
390 int64_t AJATimeBase::Convert(int64_t inValue, int64_t inScale, int64_t inDuration,
391  int64_t outScale, int64_t outDuration, bool round, bool large)
392 {
393  int64_t outValue = 0;
394  int64_t roundValue = 0;
395  int64_t inScaleOutDuration = inScale * outDuration;
396  int64_t outScaleInDuration = outScale * inDuration;
397 
398  if (round)
399  {
400  // convert half the out scale to the in duration and increase the in value
401  roundValue = inScaleOutDuration / (outScaleInDuration * 2);
402  if (inValue > 0)
403  {
404  inValue += roundValue;
405  }
406  else
407  {
408  inValue -= roundValue;
409  }
410  }
411 
412  if (large)
413  {
414  // this will not overflow
415  outValue = inValue / inScaleOutDuration * outScaleInDuration;
416  outValue += (inValue % inScaleOutDuration) * outScaleInDuration / inScaleOutDuration;
417  }
418  else
419  {
420  // this has better performance
421  outValue = inValue * outScaleInDuration / inScaleOutDuration;
422  }
423 
424  return outValue;
425 }
426 
428 {
430  for (size_t i = 0; i < AJAFrameRateTableSize; i++)
431  {
432  if (IsCloseTo(AJAFrameRateTable[i].frameTimeScale,AJAFrameRateTable[i].frameDuration))
433  {
435  break;
436  }
437  }
438 
439  return fr;
440 }
441 
442 void AJATimeBase::SetAJAFrameRatePrivate(AJA_FrameRate ajaFrameRate)
443 {
444  mFrameTimeScale = AJATimeBaseDefaultTimeScale;
445  mFrameDuration = AJATimeBaseDefaultDuration;
446 
447  for (size_t i = 0; i < AJAFrameRateTableSize; i++)
448  {
449  if (AJAFrameRateTable[i].ajaFrameRate == ajaFrameRate)
450  {
451  mFrameTimeScale = AJAFrameRateTable[i].frameTimeScale;
452  mFrameDuration = AJAFrameRateTable[i].frameDuration;
453  break;
454  }
455  }
456 }
AJATimeBase::SamplesToTicks
int64_t SamplesToTicks(int64_t samples, bool round=false) const
Definition: timebase.cpp:238
AJATimeBase::SamplesToMicroseconds
int64_t SamplesToMicroseconds(int64_t samples, bool round=false)
Definition: timebase.cpp:248
AJA_FrameRate_1900
@ AJA_FrameRate_1900
Definition: videotypes.h:218
AJA_FrameRate_3000
@ AJA_FrameRate_3000
Definition: videotypes.h:223
AJATimeBase::SamplesToFrames
int64_t SamplesToFrames(int64_t samples, bool round=false) const
Definition: timebase.cpp:233
AJATimeBase::FramesToSamples
int64_t FramesToSamples(int64_t frames, bool round=false) const
Definition: timebase.cpp:187
AJATimeBase::~AJATimeBase
virtual ~AJATimeBase()
Definition: timebase.cpp:81
AJAFrameRateEntry::ajaFrameRate
AJA_FrameRate ajaFrameRate
Definition: timebase.cpp:14
AJAFrameRateEntry::frameTimeScale
int64_t frameTimeScale
Definition: timebase.cpp:15
AJATimeBase::IsCloseTo
bool IsCloseTo(const AJATimeBase &timeBase) const
Definition: timebase.cpp:318
AJA_FrameRate_11988
@ AJA_FrameRate_11988
Definition: videotypes.h:230
AJATimeBase::GetFrameRate
void GetFrameRate(int64_t &frameTimeScale, int64_t &frameDuration) const
Definition: timebase.cpp:133
AJA_FrameRate_1498
@ AJA_FrameRate_1498
Definition: videotypes.h:213
AJA_FrameRate_Unknown
@ AJA_FrameRate_Unknown
Definition: videotypes.h:212
AJATimeBase::TicksToMicroseconds
int64_t TicksToMicroseconds(int64_t ticks, bool round=false)
Definition: timebase.cpp:268
AJATimeBase::operator=
AJATimeBase & operator=(const AJATimeBase &t)
Definition: timebase.cpp:93
systemtime.h
Declares the AJATime class.
AJA_FrameRate_2997
@ AJA_FrameRate_2997
Definition: videotypes.h:222
AJATimeBase::SetFrameRate
void SetFrameRate(int64_t frameTimeScale, int64_t frameDuration)
Definition: timebase.cpp:127
AJAFrameRateEntry
Definition: timebase.cpp:12
AJATimeBase::MicrosecondsToTicks
int64_t MicrosecondsToTicks(int64_t microseconds, bool round=false)
Definition: timebase.cpp:303
AJA_FrameRate_2400
@ AJA_FrameRate_2400
Definition: videotypes.h:220
AJATimeBase::GetSystemTicks
int64_t GetSystemTicks()
Definition: timebase.cpp:344
AJATimeBase::Convert
static int64_t Convert(int64_t inValue, int64_t inRate, int64_t outRate, bool round, bool large)
Definition: timebase.cpp:357
AJAFrameRateTableSize
const size_t AJAFrameRateTableSize
Definition: timebase.cpp:40
AJA_FrameRate_1898
@ AJA_FrameRate_1898
Definition: videotypes.h:217
AJATimeBase::Nanoseconds100ToFrames
int64_t Nanoseconds100ToFrames(int64_t nanoseconds100, bool round=false)
Definition: timebase.cpp:293
AJA_FrameRate_1500
@ AJA_FrameRate_1500
Definition: videotypes.h:214
AJA_FrameRate
AJA_FrameRate
Definition: videotypes.h:210
AJATimeBase::MicrosecondsToFrames
int64_t MicrosecondsToFrames(int64_t microseconds, bool round=false)
Definition: timebase.cpp:288
AJA_FrameRate_10000
@ AJA_FrameRate_10000
Definition: videotypes.h:229
AJATimeBase::SetAJAFrameRate
void SetAJAFrameRate(AJA_FrameRate ajaFrameRate)
Definition: timebase.cpp:164
AJATimeBase::MicrosecondsToSeconds
static double MicrosecondsToSeconds(int64_t microseconds)
Definition: timebase.cpp:313
AJA_FrameRate_4795
@ AJA_FrameRate_4795
Definition: videotypes.h:224
AJA_FrameRate_2500
@ AJA_FrameRate_2500
Definition: videotypes.h:221
AJATimeBase
Definition: timebase.h:18
AJATimeBase::FramesToNanoseconds100
int64_t FramesToNanoseconds100(int64_t frames, bool round=false) const
Definition: timebase.cpp:228
AJATimeBase::operator==
bool operator==(const AJATimeBase &val) const
Definition: timebase.cpp:106
AJATimeBase::GetAJAFrameRate
AJA_FrameRate GetAJAFrameRate(void) const
Definition: timebase.cpp:427
AJATimeBase::GetFramesPerSecondDouble
double GetFramesPerSecondDouble(void) const
Definition: timebase.cpp:215
AJA_FrameRate_2398
@ AJA_FrameRate_2398
Definition: videotypes.h:219
AJATimeBase::AJATimeBase
AJATimeBase()
Definition: timebase.cpp:46
AJA_FrameRate_5000
@ AJA_FrameRate_5000
Definition: videotypes.h:226
AJATimeBase::IsNonIntegralRatio
bool IsNonIntegralRatio(void) const
Definition: timebase.cpp:339
AJATimeBase::SecondsToMicroseconds
static int64_t SecondsToMicroseconds(double seconds, bool round=false)
Definition: timebase.cpp:308
AJATimeBase::GetFrameDuration
int64_t GetFrameDuration(void) const
Definition: timebase.cpp:149
AJATimeBaseDefaultAudioRate
const int64_t AJATimeBaseDefaultAudioRate
Definition: timebase.cpp:44
AJATimeBase::TicksToSeconds
double TicksToSeconds(int64_t ticks)
Definition: timebase.cpp:263
AJATimeBase::MicrosecondsToSamples
int64_t MicrosecondsToSamples(int64_t microseconds, bool round=false)
Definition: timebase.cpp:298
AJA_FrameRate_5994
@ AJA_FrameRate_5994
Definition: videotypes.h:227
AJA_FrameRate_1800
@ AJA_FrameRate_1800
Definition: videotypes.h:216
AJA_FrameRate_4800
@ AJA_FrameRate_4800
Definition: videotypes.h:225
AJATimeBase::TicksToFrames
int64_t TicksToFrames(int64_t ticks, bool round=false)
Definition: timebase.cpp:253
common.h
Private include file for all ajabase sources.
AJATimeBase::SetFrameTimeScale
void SetFrameTimeScale(int64_t timeScale)
Definition: timebase.cpp:144
AJATimeBase::SetTickRate
void SetTickRate(int64_t rate)
Definition: timebase.cpp:169
AJA_FrameRate_12000
@ AJA_FrameRate_12000
Definition: videotypes.h:231
AJAFrameRateEntry::frameDuration
int64_t frameDuration
Definition: timebase.cpp:16
AJA_FrameRate_1798
@ AJA_FrameRate_1798
Definition: videotypes.h:215
AJATimeBase::GetFramesPerSecond
float GetFramesPerSecond(void) const
Definition: timebase.cpp:207
AJATimeBase::FramesToMicroseconds
int64_t FramesToMicroseconds(int64_t frames, bool round=false) const
Definition: timebase.cpp:223
AJATimeBase::operator!=
bool operator!=(const AJATimeBase &val) const
Definition: timebase.cpp:122
AJATimeBase::SetToDefault
void SetToDefault(void)
Definition: timebase.cpp:85
true
#define true
Definition: ntv2devicefeatures.h:26
AJATimeBase::ScaleUnitsToSeconds
double ScaleUnitsToSeconds(int64_t num) const
Definition: timebase.cpp:202
AJATimeBase::SecondsToFrames
int64_t SecondsToFrames(double seconds, bool round=false)
Definition: timebase.cpp:273
AJA_FrameRate_6000
@ AJA_FrameRate_6000
Definition: videotypes.h:228
AJATimeBase::GetFrameTimeScale
int64_t GetFrameTimeScale(void) const
Definition: timebase.cpp:139
AJATimeBase::FramesToSeconds
double FramesToSeconds(int64_t frames) const
Definition: timebase.cpp:197
AJATimeBase::FramesToTicks
int64_t FramesToTicks(int64_t frames, bool round=false) const
Definition: timebase.cpp:192
AJATimeBase::SamplesToSeconds
double SamplesToSeconds(int64_t samples)
Definition: timebase.cpp:243
AJATimeBase::SetAudioRate
void SetAudioRate(int64_t rate)
Definition: timebase.cpp:154
AJAFrameRateTable
const AJAFrameRateEntry AJAFrameRateTable[]
Definition: timebase.cpp:18
AJATimeBase::GetTickRate
int64_t GetTickRate() const
Definition: timebase.cpp:182
AJATimeBase::SecondsToSamples
int64_t SecondsToSamples(double seconds, bool round=false)
Definition: timebase.cpp:278
AJATimeBaseDefaultDuration
const int64_t AJATimeBaseDefaultDuration
Definition: timebase.cpp:43
AJATimeBase::SecondsToTicks
int64_t SecondsToTicks(double seconds, bool round=false)
Definition: timebase.cpp:283
AJATimeBase::GetAudioRate
int64_t GetAudioRate() const
Definition: timebase.cpp:159
AJATime::GetSystemCounter
static int64_t GetSystemCounter(void)
Returns the current value of the host system's high-resolution time counter.
Definition: systemtime.cpp:112
AJATimeBase::TicksToSamples
int64_t TicksToSamples(int64_t ticks, bool round=false)
Definition: timebase.cpp:258
AJATimeBaseDefaultTimeScale
const int64_t AJATimeBaseDefaultTimeScale
Definition: timebase.cpp:42
AJATime::GetSystemFrequency
static int64_t GetSystemFrequency(void)
Returns the frequency of the host system's high-resolution time counter.
Definition: systemtime.cpp:149
timebase.h
Declares the AJATimeBase class.