Search code examples
c++algorithmframes

Dropping video frames


my question is pretty simple, I think. And it's more about math than coding. I receive 15 frames per second from web-cam. But I have to drop some of these. For example, if client is requesting 8 fps - I do drop each second frame and it's ok. But how should I drop if 12 fps or 6 fps are requested?

I think there's some common algo for values distribution. Thanks a lot!


Solution

  • By dropping every 2nd frame for 8 fps, if you are having 15 fps, you are introducing an error.

    This is a very simple algorithm, that would work in all cases :

    #include <iostream>
    
    double t = 0.0;
    
    const double fps1 = 15.0;
    const double fps2 = 12.0;
    
    const double t1 = 1.0 / fps1;
    const double t2 = 1.0 / fps2;
    
    // true - drop the frame
    // false - do NOT drop the frame
    bool NextTick()
    {
      t += t1;
      if ( t > t2 )
      {
        t -= t2;
        return false;
      }
      return true;
    }
    
    int main()
    {
      for ( unsigned int i =0;i<20;++i)
      {
          if ( NextTick() )
          {
              std::cout<<"dropping the frame"<<std::endl;
          }
          else
          {
              std::cout<<"display the frame"<<std::endl;
          }
      }
    }