00001 // FrameRate.h 00002 // 00004 // 00005 // Copyright (c) 2002 David Ward 00006 // 00008 00009 #ifndef __FrameRate_h__ 00010 #define __FrameRate_h__ 00011 00012 #include "MSVC_Unannoy.h" 00013 00014 // keeps track of framerate 00015 // computes the Steps parameter 00016 // computes RXmax - which controls the maximum rate of zooming in 00017 00018 const double LN2= log(2); 00019 00020 class CFrameRate { 00021 public: 00022 CFrameRate() ; 00023 ~CFrameRate() {}; 00024 const double Rxmax() const {return m_dRXmax;} 00025 const int Steps() const {return m_iSteps;} 00026 const double Framerate() const {return m_dFr;} 00027 void Reset(unsigned long Time); 00028 void NewFrame(unsigned long Time); 00029 00030 // TODO: These two shouldn't be the same thing: 00031 void SetBitrate(double TargetRate); 00032 void SetMaxBitrate(double MaxRate); 00033 private: 00034 double m_dFr; 00035 double m_dMaxbitrate; // the maximum rate of entering information 00036 double m_dRXmax; // the maximum zoomin per frame 00037 int m_iFrames,m_iTime,m_iTime2,m_iSamples; 00038 int m_iSteps; // the 'Steps' parameter. See djw thesis. 00039 }; 00040 00041 inline CFrameRate::CFrameRate() { 00042 00043 // maxbitrate should be user-defined and/or adaptive. Currently it is not. 00044 #if defined(_WIN32_WCE) 00045 m_dMaxbitrate=5; 00046 #else 00047 m_dMaxbitrate=5.5; 00048 #endif 00049 00050 m_dRXmax=2; // only a transient effect 00051 m_iFrames=0; 00052 m_iSamples=1; 00053 00054 // we dont know the framerate yet - play it safe by setting it high 00055 m_dFr=1<<5; 00056 00057 // start off very slow until we have sampled framerate adequately 00058 m_iSteps=2000; 00059 m_iTime=0; // Hmmm, User must reset framerate before starting. 00060 } 00061 00062 inline void CFrameRate::NewFrame(unsigned long Time) 00063 // compute framerate if we have sampled enough frames 00064 { 00065 00066 m_iFrames++; 00067 00068 if (m_iFrames==m_iSamples) { 00069 m_iTime2=Time; 00070 if (m_iTime2-m_iTime < 50) 00071 m_iSamples++; // increase sample size 00072 else if (m_iTime2-m_iTime > 80) { 00073 m_iSamples--; 00074 if (m_iSamples <2) 00075 m_iSamples=2; 00076 } 00077 if (m_iTime2-m_iTime) { 00078 m_dFr=m_iFrames*1000.0/(m_iTime2-m_iTime); 00079 m_iTime=m_iTime2; 00080 m_iFrames=0; 00081 00082 } 00083 m_dRXmax=exp(m_dMaxbitrate*LN2/m_dFr); 00084 m_iSteps=m_iSteps/2+(int)(-log(0.2)*m_dFr/LN2/m_dMaxbitrate)/2; 00085 // dchar debug[256]; 00086 // _stprintf(debug,TEXT("fr %f Steps %d samples %d time2 %d rxmax %f\n"),fr,Steps,samples,time2,RXmax); 00087 // OutputDebugString(debug); 00088 00089 } 00090 } 00091 00092 inline void CFrameRate::Reset(unsigned long Time) 00093 { 00094 m_iFrames=0; 00095 m_iTime=Time; 00096 } 00097 00098 00099 inline void CFrameRate::SetBitrate(double TargetRate) 00100 { 00101 m_dMaxbitrate = TargetRate; 00102 } 00103 00104 00105 inline void CFrameRate::SetMaxBitrate(double MaxRate) 00106 { 00107 m_dMaxbitrate = MaxRate; 00108 } 00109 00110 00111 00112 #endif /* #ifndef __FrameRate_h__ */
1.4.2