-2

I am searching for optimal sourcecode/algorithm in c++ to convert frames count into time code hh:mm:ss:ff in given fps, ex. 25fps...

this code is preety good - http://www.andrewduncan.ws/Timecodes/Timecodes.html (bottom of page) but it is expensive - it contains 4 mod and 6 div operations

I need to show time code on every frame, so calculating this algorithm could take some time.

I can of course store evaluated timecode to avoid calculations.

But it would be very helpful to know better algorithm...

thanks in advance yours m.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
mateusz_s
  • 401
  • 1
  • 5
  • 11
  • 10
    4 mod and 6 div is not a problem for every frame. Period – Armen Tsirunyan Nov 22 '11 at 15:56
  • but mods and divs are the most extensive operations, so i think it could take some perormance during playback of 25 frames per sec... – mateusz_s Nov 22 '11 at 15:59
  • 3
    25 fps is 1 frame every 40 ms. That's 40,000,000 clock cycles on a 1 GHz processor (like the one in my phone). Don't worry about optimizing this stuff unless you already know it's a problem (because you've run it, it's too slow, and profiling tells you that this is where your time is being spent). – nmichaels Nov 22 '11 at 16:01
  • 2
    I would suggest measuring what the actual performance impact is before assuming you need to optimize. – Scott Nov 22 '11 at 16:01
  • 2
    lol we play at 120fps and I can assure you, a couple of mods and divs is *nothing* in comparision with what gets done here in that timespan. – stijn Nov 22 '11 at 16:04
  • 1
    @mateusz_s: decoding one frame of video will take orders of magnitude more CPU time that a few arithmetic operations. Just copying data to the screen will take more than these muls/divs. You're most likely optimizing too early or the wrong thing. – Alexey Frunze Nov 22 '11 at 16:10
  • 1
    The example code link is dead. One downvote for you! – zrajm Dec 12 '18 at 00:12

4 Answers4

6

General rule of thumb: In image-processing systems, which include video players, you sweat blood over the operations that run once per pixel, then you sweat over the operations that run once per image "patch" (typically a line of pixels), and you don't sweat the stuff that runs once per frame.

The reason is that the per-pixel stuff will run hundreds, maybe thousands, of times as often as the per-patch stuff, and the per-patch stuff will run hundreds, maybe thousands, of times as often as the per-frame stuff.

This means that the per-pixel stuff may run millions of times as often as the per-frame stuff. One instruction per pixel may cost millions of instructions per frame, and a few hundred, or even a few thousand, instructions per frame is lost in the noise floor against the per-pixel instruction counts.

In other words, you can probably afford the mods and divs.

Having said that, it MIGHT be reasonable to run custom counters instead of doing mods and divs.

John R. Strohm
  • 7,547
  • 2
  • 28
  • 33
2

First of all, the question might better be optimal code for conversion between seconds to hours, minutes, seconds. At this point, if frames come in order, you can simply use addition to increase the previous time.

perreal
  • 94,503
  • 21
  • 155
  • 181
2

First off, I agree with everyone else that you probably don't need to optimize this, unless you are specifically seeing problems. However, since it's entertaining to try to find ways to, I'll give you something I saw at first glance to reduce the number of divides.

seconds = framenumber div 30
minutes = seconds div 60
hours = minutes div 60
frames = frameNumber mod 30
seconds = seconds mod 60
minutes = minutes mod 60
hours = hours mod 24

It's more lines of code, but fewer divides. Basically, since seconds, minutes and hours use some of the same math, I use the results from one in the formula for the next.

Derek
  • 3,087
  • 1
  • 21
  • 23
1

Mod and div operations (to a small constant value) may be effectively performed with multiplication to some precalculated reciprocal. So they are not expensive.

Evgeny Kluev
  • 24,287
  • 7
  • 55
  • 98