使用 CMTime 而不进行舍入?

发布于 2024-10-25 08:45:21 字数 865 浏览 5 评论 0原文

我正在编写一个使用 AVFoundation 和 CMTime 的应用程序。我已记录使用 CMTimeMake() 创建的 CMTime 实例的值。该值似乎四舍五入为最接近的整数。我需要一个具有精确值的 CMTime 实例,无需舍入。

我在 CMTime 参考中看到了舍入常量:

enum {
   kCMTimeRoundingMethod_RoundHalfAwayFromZero = 1,
   kCMTimeRoundingMethod_RoundTowardZero = 2,
   kCMTimeRoundingMethod_RoundAwayFromZero = 3,
   kCMTimeRoundingMethod_QuickTime = 4,
   kCMTimeRoundingMethod_RoundTowardPositiveInfinity = 5,
   kCMTimeRoundingMethod_RoundTowardNegativeInfinity = 6,

   kCMTimeRoundingMethod_Default = kCMTimeRoundingMethod_RoundHalfAwayFromZero
};

没有任何示例说明如何控制将哪些策略应用于 CMTime 实例?或者,如果这不是正确的方法,我如何从 CMTime 实例中提取精确的值?


编辑:

我已经找到并测试了CMTIME_HAS_BEEN_ROUNDED()。我将 CMTime 实例传递给此函数,它返回 No(表明该值尚未四舍五入)。那么为什么我会失去精度呢?

I'm writing an application that uses AVFoundation and CMTime. I have logged the values of my CMTime instances created using CMTimeMake(). This value seems to be rounded to the nearest integer. I need a CMTime instance with a precise value, without rounding.

I have seen the rounding constants in the CMTime reference:

enum {
   kCMTimeRoundingMethod_RoundHalfAwayFromZero = 1,
   kCMTimeRoundingMethod_RoundTowardZero = 2,
   kCMTimeRoundingMethod_RoundAwayFromZero = 3,
   kCMTimeRoundingMethod_QuickTime = 4,
   kCMTimeRoundingMethod_RoundTowardPositiveInfinity = 5,
   kCMTimeRoundingMethod_RoundTowardNegativeInfinity = 6,

   kCMTimeRoundingMethod_Default = kCMTimeRoundingMethod_RoundHalfAwayFromZero
};

There aren't any example of how I can control which of these strategies is applied to a CMTime instance? Or, if this isn't the right way, how can I extract a precise value from a CMTime instance?


Edit:

I have found and tested CMTIME_HAS_BEEN_ROUNDED(). I passed my CMTime instance to this function and it returns No (indicating that the value has not been rounded). So why am I losing precision?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-11-01 08:45:21

如果您阅读 CMTime 的文档 您将看到它使用分子和分母将时间存储为有理数。分子为 int64_t,分母为 int32_t

分子指定经过了多少个“刻度”,分母指定了每秒多少个“刻度”。

因此 0.5 秒可以存储为:

  • 100/200:100 个刻度,每秒 200 个刻度
  • 500/1000:500 个刻度,每秒 1000 个刻度
  • 8/16,8 个刻度,每秒 16 个刻度

等等。您使用的方式

CMTimeMake([[Array objectAtIndex:i]floatValue], 1);

是说“每秒有一个刻度”,并且由于分子是整数,因此浮点值被截断,因此仅存储 1。因此,您将时间指定为:1/1,已过去一个刻度,每秒一个刻度,因此您实际上存储恰好 1 秒。

要解决这个问题,取决于您想要做什么以及您是否关心时间尺度。 Apple 建议时间刻度为 600,但如果您不在乎,则可以这样做:

CMTimeMake([[Array objectAtIndex:i]floatValue]*1000, 1000);

将时间刻度设置为 1000,即每秒 1000 个刻度,即每个刻度一毫秒。它还将以秒为单位的时间转换为毫秒。请注意,它会截断第 4 位数字,因此如果您有 1.2345,您只会得到 1.234,而不是 1.235。如果这对您很重要,请参阅roundf

If you read the documentation for CMTime you will see that it stores time as a rational number using a numerator and denominator. The numerator is int64_t while the denominator is int32_t.

The numerator specifies how many "ticks" have passed, and the denominator specifies how many "ticks" per second.

So 0.5 seconds can be stored as:

  • 100/200: 100 ticks, 200 ticks per second
  • 500/1000: 500 ticks, 1000 ticks per second
  • 8/16, 8 ticks, 16 ticks per second

And so forth. The way you have done it, using

CMTimeMake([[Array objectAtIndex:i]floatValue], 1);

Is saying "there is one tick per second", and since the numerator is an integer, the float value is truncated so only the 1 is stored. Therefore you are specifying time as: 1/1, one tick has elapsed, one tick per second, so you are actually storing exactly 1 second.

To fix this, it depends on what you want to do and whether you care about the timescale. Apple recommends a timescale of 600, but if you don't care, you can just do this:

CMTimeMake([[Array objectAtIndex:i]floatValue]*1000, 1000);

That sets timescale to 1000, so 1000 ticks per second, so one millisecond per tick. It also converts the time in seconds to milliseconds. Note that it truncates the 4th digit, so if you had 1.2345 you just get 1.234 not 1.235. If that matters to you, see roundf.

遗失的美好 2024-11-01 08:45:21

CMTimeMake([[Array objectAtIndex:i]floatValue]*1000, 1000);

这帮助我获得了以毫秒为单位的精确时间。干杯。

CMTimeMake([[Array objectAtIndex:i]floatValue]*1000, 1000);

This helped me to get exact time in milliseconds. Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文