开发者

CMTime / CMTimeMake noob question

开发者 https://www.devze.com 2023-03-06 08:05 出处:网络
CMTimeMake is not giving me the results I expect.The following code: CMTime testTime = CMTimeMake(0, 30);

CMTimeMake is not giving me the results I expect. The following code:

CMTime testTime = CMTimeMake(0, 30);
NSLog(@"testTime w/ input 0, 30: value: %d, timescale %d, seconds: %f",
       testTime.value, testTime.timescale,
      (float) testTime.value / testTime.timescale);

testTime = CMTimeMake(1, 30);
NSLog(@"testTime w/ input 1, 30: value: %d, timescale %d, seconds: %f",
      testTime.value, testTime.timescale,
      (float) testTim开发者_如何学Pythone.value / testTime.timescale);

testTime = CMTimeMake(15, 30);
NSLog(@"testTime w/ input 15, 30: value: %d, timescale %d, seconds: %f",
      testTime.value, testTime.timescale,
      (float) testTime.value / testTime.timescale);

produces the following output:

testTime w/ input 0, 30: value: 0, timescale 0, seconds: 0.000000

testTime w/ input 1, 60: value: 1, timescale 0, seconds: 0.000000

testTime w/ input 15, 60: value: 15, timescale 0, seconds: 0.000000

Why is testTime.timescale always zero?


This is a problem with your format string for NSLog. Since your question title indicates that you're a "noob", I'll take some time to explain what's going on here.

Functions that take a variable number of arguments like NSLog(NSString* format, ...) need to read the extra arguments based on the format string...

  • %d means: Read four bytes (32-bits) and treat it as a decimal integer.
  • %f means: Read four bytes (32-bits) and treat it as a floating point number.

Let's examine your last example:

You are passing %d %d %f in the format string, followed by:

testTime.value     // A 64-bit integer (8 bytes) with the value 15
testTime.timescale // A 32-bit integer (4-bytes) with the value 30
(float)15 / 30     // A 32-bit float (4-bytes) with the value 0.5f

Due to the way these numbers get passed in, you end up reading the least significant 32-bits of testTime.value for the first %d which happens to be interpreted correctly as 15, then for the second %d and the %f you are reading the upper 32-bits (0) and probably some padding bytes to get 0.0. I'm actually a little puzzled why you're getting 0.0 instead of some small number as I would expect the 30 to get interpreted as a float, which would be 4.2E-44 - if anyone knows please let me know.

Anyway, the way to solve it is to change the first %d into %lld and this will display the values correctly. The testTime variable actually held the right values all along.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号