AJA NTV2 SDK  18.1.0.2439
NTV2 SDK 18.1.0.2439
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 
139 #if !defined(NTV2_DEPRECATE_18_1)
140 void AJATimeBase::GetFrameRate(uint32_t& frameTimeScale, uint32_t& frameDuration) const
141 {
142  frameTimeScale = static_cast<uint32_t>(mFrameTimeScale);
143  frameDuration = static_cast<uint32_t>(mFrameDuration);
144 }
145 #endif
146 
148 {
149  return mFrameTimeScale;
150 }
151 
152 void AJATimeBase::SetFrameTimeScale(int64_t timeScale)
153 {
154  mFrameTimeScale = timeScale;
155 }
156 
157 int64_t AJATimeBase::GetFrameDuration(void) const
158 {
159  return mFrameDuration;
160 }
161 
162 void AJATimeBase::SetAudioRate(int64_t rate)
163 {
164  mAudioRate = rate;
165 }
166 
168 {
169  return mAudioRate;
170 }
171 
173 {
174  SetAJAFrameRatePrivate(ajaFrameRate);
175 }
176 
177 void AJATimeBase::SetTickRate(int64_t rate)
178 {
179  if (rate == 0)
180  {
181  // set to native rate
182  mTickRate = AJATime::GetSystemFrequency();
183  }
184  else
185  {
186  mTickRate = rate;
187  }
188 }
189 
191 {
192  return mTickRate;
193 }
194 
195 int64_t AJATimeBase::FramesToSamples(int64_t frames, bool round) const
196 {
197  return Convert(frames, mFrameTimeScale, mFrameDuration, mAudioRate, 1, round, true);
198 }
199 
200 int64_t AJATimeBase::FramesToTicks(int64_t frames, bool round) const
201 {
202  return Convert(frames, mFrameTimeScale, mFrameDuration, mTickRate, 1, round, true);
203 }
204 
205 double AJATimeBase::FramesToSeconds(int64_t frames) const
206 {
207  return (double)frames * (double)mFrameDuration / (double)mFrameTimeScale;
208 }
209 
210 double AJATimeBase::ScaleUnitsToSeconds(int64_t scaleUnits) const
211 {
212  return (double)scaleUnits / (double)mFrameTimeScale;
213 }
214 
216 {
217  float val = 0.f;
218  if (mFrameDuration)
219  val = (float)mFrameTimeScale / (float)mFrameDuration;
220  return val;
221 }
222 
224 {
225  double val = 0.;
226  if (mFrameDuration)
227  val = (double)mFrameTimeScale / (double)mFrameDuration;
228  return val;
229 }
230 
231 int64_t AJATimeBase::FramesToMicroseconds(int64_t frames, bool round) const
232 {
233  return Convert(frames, mFrameTimeScale, mFrameDuration, 1000000, 1, round, true);
234 }
235 
236 int64_t AJATimeBase::FramesToNanoseconds100(int64_t frames, bool round) const
237 {
238  return Convert(frames, mFrameTimeScale, mFrameDuration, 10000000, 1, round, true);
239 }
240 
241 int64_t AJATimeBase::SamplesToFrames(int64_t samples, bool round) const
242 {
243  return Convert(samples, mAudioRate, 1, mFrameTimeScale, mFrameDuration, round, true);
244 }
245 
246 int64_t AJATimeBase::SamplesToTicks(int64_t samples, bool round) const
247 {
248  return Convert(samples, mAudioRate, mTickRate, round, true);
249 }
250 
251 double AJATimeBase::SamplesToSeconds(int64_t samples)
252 {
253  return (double)samples / (double)mAudioRate;
254 }
255 
256 int64_t AJATimeBase::SamplesToMicroseconds(int64_t samples, bool round)
257 {
258  return Convert(samples, mAudioRate, 1000000, round, true);
259 }
260 
261 int64_t AJATimeBase::TicksToFrames(int64_t ticks, bool round)
262 {
263  return Convert(ticks, mTickRate, 1, mFrameTimeScale, mFrameDuration, round, true);
264 }
265 
266 int64_t AJATimeBase::TicksToSamples(int64_t ticks, bool round)
267 {
268  return Convert(ticks, mTickRate, mAudioRate, round, true);
269 }
270 
271 double AJATimeBase::TicksToSeconds(int64_t ticks)
272 {
273  return (double)ticks / (double)mTickRate;
274 }
275 
276 int64_t AJATimeBase::TicksToMicroseconds(int64_t ticks, bool round)
277 {
278  return Convert(ticks, mTickRate, 1000000, round, true);
279 }
280 
281 int64_t AJATimeBase::SecondsToFrames(double seconds, bool round)
282 {
283  return Convert((int64_t)(seconds * 1000000000.0), 1000000000, 1, mFrameTimeScale, mFrameDuration, round, true);
284 }
285 
286 int64_t AJATimeBase::SecondsToSamples(double seconds, bool round)
287 {
288  return Convert((int64_t)(seconds * 1000000000.0), 1000000000, mAudioRate, round, true);
289 }
290 
291 int64_t AJATimeBase::SecondsToTicks(double seconds, bool round)
292 {
293  return Convert((int64_t)(seconds * 1000000000.0), 1000000000, mTickRate, round, true);
294 }
295 
296 int64_t AJATimeBase::MicrosecondsToFrames(int64_t microseconds, bool round)
297 {
298  return Convert(microseconds, 1000000, 1, mFrameTimeScale, mFrameDuration, round, true);
299 }
300 
301 int64_t AJATimeBase::Nanoseconds100ToFrames(int64_t nanoseconds100, bool round)
302 {
303  return Convert(nanoseconds100, 10000000, 1, mFrameTimeScale, mFrameDuration, round, true);
304 }
305 
306 int64_t AJATimeBase::MicrosecondsToSamples(int64_t microseconds, bool round)
307 {
308  return Convert(microseconds, 1000000, mAudioRate, round, true);
309 }
310 
311 int64_t AJATimeBase::MicrosecondsToTicks(int64_t microseconds, bool round)
312 {
313  return Convert(microseconds, 1000000, mTickRate, round, true);
314 }
315 
316 int64_t AJATimeBase::SecondsToMicroseconds(double seconds, bool round)
317 {
318  return Convert((int64_t)(seconds * 1000000.0), 1000000, 1000000, round, false);
319 }
320 
321 double AJATimeBase::MicrosecondsToSeconds(int64_t microseconds)
322 {
323  return (double)microseconds / 1000000.0;
324 }
325 
326 bool AJATimeBase::IsCloseTo(const AJATimeBase &timeBase) const
327 {
328  bool bIsClose = false;
329  double rate1 = FramesToSeconds(1);
330  double rate2 = timeBase.FramesToSeconds(1);
331  double dif = rate1 / rate2;
332 
333  // this is just a guess at what is considered close enough
334  // it will differentiate between 30000/1001 and 30000/1000,
335  // but 2997/100 is *not* close to 30000/1001
336  if ((dif >= .9999) && (dif <= 1.0001))
337  bIsClose = true;
338  return bIsClose;
339 }
340 
341 bool AJATimeBase::IsCloseTo(int64_t frameTimeScale, int64_t frameDuration) const
342 {
343  AJATimeBase tb(frameTimeScale,frameDuration);
344  return IsCloseTo(tb);
345 }
346 
348 {
349  return ((mFrameTimeScale % mFrameDuration) != 0) ? true : false;
350 }
351 
353 {
354  int64_t ticks = AJATime::GetSystemCounter();
355  int64_t rate = AJATime::GetSystemFrequency();
356 
357  if (rate != mTickRate)
358  {
359  ticks = Convert(ticks, rate, mTickRate, false, true);
360  }
361 
362  return ticks;
363 }
364 
365 int64_t AJATimeBase:: Convert(int64_t inValue, int64_t inRate, int64_t outRate, bool round, bool large)
366 {
367  int64_t outValue = 0;
368 
369  if (round)
370  {
371  // convert half the out rate to the in rate and increase the in value
372  int64_t roundValue = inRate / (outRate * 2);
373  if (inValue > 0)
374  {
375  inValue += roundValue;
376  }
377  else
378  {
379  inValue -= roundValue;
380  }
381  }
382 
383  if (large)
384  {
385  // this will not overflow
386  outValue = inValue / inRate * outRate;
387  outValue += (inValue % inRate) * outRate / inRate;
388  }
389  else
390  {
391  // this has better performance
392  outValue = inValue * outRate / inRate;
393  }
394 
395  return outValue;
396 }
397 
398 int64_t AJATimeBase::Convert(int64_t inValue, int64_t inScale, int64_t inDuration,
399  int64_t outScale, int64_t outDuration, bool round, bool large)
400 {
401  int64_t outValue = 0;
402  int64_t roundValue = 0;
403  int64_t inScaleOutDuration = inScale * outDuration;
404  int64_t outScaleInDuration = outScale * inDuration;
405 
406  if (round)
407  {
408  // convert half the out scale to the in duration and increase the in value
409  roundValue = inScaleOutDuration / (outScaleInDuration * 2);
410  if (inValue > 0)
411  {
412  inValue += roundValue;
413  }
414  else
415  {
416  inValue -= roundValue;
417  }
418  }
419 
420  if (large)
421  {
422  // this will not overflow
423  outValue = inValue / inScaleOutDuration * outScaleInDuration;
424  outValue += (inValue % inScaleOutDuration) * outScaleInDuration / inScaleOutDuration;
425  }
426  else
427  {
428  // this has better performance
429  outValue = inValue * outScaleInDuration / inScaleOutDuration;
430  }
431 
432  return outValue;
433 }
434 
436 {
438  for (size_t i = 0; i < AJAFrameRateTableSize; i++)
439  {
440  if (IsCloseTo(AJAFrameRateTable[i].frameTimeScale,AJAFrameRateTable[i].frameDuration))
441  {
443  break;
444  }
445  }
446 
447  return fr;
448 }
449 
450 void AJATimeBase::SetAJAFrameRatePrivate(AJA_FrameRate ajaFrameRate)
451 {
452  mFrameTimeScale = AJATimeBaseDefaultTimeScale;
453  mFrameDuration = AJATimeBaseDefaultDuration;
454 
455  for (size_t i = 0; i < AJAFrameRateTableSize; i++)
456  {
457  if (AJAFrameRateTable[i].ajaFrameRate == ajaFrameRate)
458  {
459  mFrameTimeScale = AJAFrameRateTable[i].frameTimeScale;
460  mFrameDuration = AJAFrameRateTable[i].frameDuration;
461  break;
462  }
463  }
464 }
const int64_t AJATimeBaseDefaultTimeScale
Definition: timebase.cpp:42
int64_t SamplesToFrames(int64_t samples, bool round=false) const
Definition: timebase.cpp:241
double FramesToSeconds(int64_t frames) const
Definition: timebase.cpp:205
double GetFramesPerSecondDouble(void) const
Definition: timebase.cpp:223
void SetAJAFrameRate(AJA_FrameRate ajaFrameRate)
Definition: timebase.cpp:172
int64_t SamplesToTicks(int64_t samples, bool round=false) const
Definition: timebase.cpp:246
const AJAFrameRateEntry AJAFrameRateTable[]
Definition: timebase.cpp:18
int64_t SecondsToSamples(double seconds, bool round=false)
Definition: timebase.cpp:286
int64_t MicrosecondsToTicks(int64_t microseconds, bool round=false)
Definition: timebase.cpp:311
int64_t Nanoseconds100ToFrames(int64_t nanoseconds100, bool round=false)
Definition: timebase.cpp:301
bool operator!=(const AJATimeBase &val) const
Definition: timebase.cpp:122
Declares the AJATime class.
double ScaleUnitsToSeconds(int64_t num) const
Definition: timebase.cpp:210
int64_t TicksToFrames(int64_t ticks, bool round=false)
Definition: timebase.cpp:261
AJA_FrameRate GetAJAFrameRate(void) const
Definition: timebase.cpp:435
double TicksToSeconds(int64_t ticks)
Definition: timebase.cpp:271
int64_t FramesToMicroseconds(int64_t frames, bool round=false) const
Definition: timebase.cpp:231
int64_t frameTimeScale
Definition: timebase.cpp:15
int64_t frameDuration
Definition: timebase.cpp:16
Declares the AJATimeBase class.
#define true
Definition: timebase.cpp:12
void GetFrameRate(int64_t &frameTimeScale, int64_t &frameDuration) const
Definition: timebase.cpp:133
void SetTickRate(int64_t rate)
Definition: timebase.cpp:177
float GetFramesPerSecond(void) const
Definition: timebase.cpp:215
virtual ~AJATimeBase()
Definition: timebase.cpp:81
int64_t GetTickRate() const
Definition: timebase.cpp:190
int64_t FramesToNanoseconds100(int64_t frames, bool round=false) const
Definition: timebase.cpp:236
int64_t MicrosecondsToSamples(int64_t microseconds, bool round=false)
Definition: timebase.cpp:306
static int64_t GetSystemCounter(void)
Returns the current value of the host system&#39;s high-resolution time counter.
Definition: systemtime.cpp:112
static double MicrosecondsToSeconds(int64_t microseconds)
Definition: timebase.cpp:321
static int64_t SecondsToMicroseconds(double seconds, bool round=false)
Definition: timebase.cpp:316
static int64_t GetSystemFrequency(void)
Returns the frequency of the host system&#39;s high-resolution time counter.
Definition: systemtime.cpp:149
int64_t GetFrameTimeScale(void) const
Definition: timebase.cpp:147
int64_t SecondsToFrames(double seconds, bool round=false)
Definition: timebase.cpp:281
bool IsCloseTo(const AJATimeBase &timeBase) const
Definition: timebase.cpp:326
int64_t GetFrameDuration(void) const
Definition: timebase.cpp:157
int64_t MicrosecondsToFrames(int64_t microseconds, bool round=false)
Definition: timebase.cpp:296
int64_t SamplesToMicroseconds(int64_t samples, bool round=false)
Definition: timebase.cpp:256
int64_t TicksToSamples(int64_t ticks, bool round=false)
Definition: timebase.cpp:266
void SetToDefault(void)
Definition: timebase.cpp:85
void SetAudioRate(int64_t rate)
Definition: timebase.cpp:162
const int64_t AJATimeBaseDefaultDuration
Definition: timebase.cpp:43
double SamplesToSeconds(int64_t samples)
Definition: timebase.cpp:251
int64_t FramesToTicks(int64_t frames, bool round=false) const
Definition: timebase.cpp:200
bool operator==(const AJATimeBase &val) const
Definition: timebase.cpp:106
bool IsNonIntegralRatio(void) const
Definition: timebase.cpp:347
static int64_t Convert(int64_t inValue, int64_t inRate, int64_t outRate, bool round, bool large)
Definition: timebase.cpp:365
Private include file for all ajabase sources.
int64_t GetSystemTicks()
Definition: timebase.cpp:352
int64_t FramesToSamples(int64_t frames, bool round=false) const
Definition: timebase.cpp:195
int64_t GetAudioRate() const
Definition: timebase.cpp:167
void SetFrameRate(int64_t frameTimeScale, int64_t frameDuration)
Definition: timebase.cpp:127
int64_t TicksToMicroseconds(int64_t ticks, bool round=false)
Definition: timebase.cpp:276
void SetFrameTimeScale(int64_t timeScale)
Definition: timebase.cpp:152
AJATimeBase & operator=(const AJATimeBase &t)
Definition: timebase.cpp:93
AJA_FrameRate ajaFrameRate
Definition: timebase.cpp:14
AJA_FrameRate
Definition: videotypes.h:210
int64_t SecondsToTicks(double seconds, bool round=false)
Definition: timebase.cpp:291
const size_t AJAFrameRateTableSize
Definition: timebase.cpp:40
const int64_t AJATimeBaseDefaultAudioRate
Definition: timebase.cpp:44