Zax
Zax

Reputation: 3010

How to calculate FPS of a video

I have a video decoder. Using openGL I'm rendering the decoded frames on the screen using glfw library. I have to display the FPS while the decoder is decoding and rendering the frames. I know how to display something on the screen.

What i know:

I know the time taken to decode each frame. i.e. i have put a clock() function before and after the decoding function and displaying function. That is i know the time taken to decode and display each frame (say x milli seconds). Now using this data i need to calculate FPS of the video and display it.

What i tried:

Time taken to decode a 1 frame=x milli seconds

How many frames are decoded (y) in 1 second (this is the definition of FPS)

Therefore, FPS=1*1/(x*milli)

=> FPS=1000/x

So for every frame i'm calculating using the above formula, and displaying it. But on verification i got to know that its wrong. Can anyone please tell me what is wrong in the above method? How do i calculate FPS in realtime, while i know time taken to decode each frame?

Upvotes: 0

Views: 2695

Answers (1)

radical7
radical7

Reputation: 9114

Generally speaking, the approach you're using using is the right method. It does assume that each frame takes the same time to decode and display, which may cause inaccuracies in the exact per-frame timings. Timing the decode section will tell you how long it took to decode the frame, but your OpenGL timings may be inaccurate unless you include a glFinish, which guarantees that OpenGL has completed the submission and execution of all OpenGL calls up to when you called glFinish.

If you put a clock() at the start of the decode section, and then another after a call to glFinish, you should get more accurate timings. Each frame will likely take different times, so your FPS may fluctuate. Additionally, if you're syncing to vertical retrace, your FPS may vary as well, since you'll need to wait for the hardware to update and swap the buffers.

Upvotes: 1

Related Questions