]> git.sesse.net Git - kdenlive/blobdiff - src/timecode.cpp
code cleanup
[kdenlive] / src / timecode.cpp
index e2fe09e26a0eb38d0ece6b2ce324e33e6db3a577..1b25df21f13adaf5a5de50bd488ab890d36c4cab 100644 (file)
 #include <kdebug.h>
 #include <klocale.h>
 
-Timecode::Timecode(Formats format, int framesPerSecond, bool dropFrame) :
+Timecode::Timecode(Formats format, double framesPerSecond, bool dropFrame) :
         m_format(format),
         m_dropFrame(dropFrame),
-        m_displayedFramesPerSecond(framesPerSecond)
+        m_displayedFramesPerSecond(framesPerSecond + 0.5),
+        m_realFps(framesPerSecond)
 {
 }
 
@@ -30,18 +31,24 @@ Timecode::~Timecode()
 {
 }
 
-int Timecode::fps() const
+double Timecode::fps() const
 {
-    return m_displayedFramesPerSecond;
+    return m_realFps; //m_displayedFramesPerSecond;
 }
 
 
-int Timecode::getFrameCount(const QString duration, double fps) const
+int Timecode::getDisplayFrameCount(const QString duration, bool frameDisplay) const
+{
+    if (frameDisplay) return duration.toInt();
+    return getFrameCount(duration);
+}
+
+int Timecode::getFrameCount(const QString duration) const
 {
     if (m_dropFrame) {
         // calculate how many frames need to be dropped every minute.
         int frames;
-        int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps)  + 0.5);
+        int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - m_realFps)  + 0.5);
 
         int perMinute = toDrop / 9;
         int tenthMinute = toDrop % 9;
@@ -65,20 +72,26 @@ int Timecode::getFrameCount(const QString duration, double fps) const
         frames += duration.section(':', 2, 2).toInt() * m_displayedFramesPerSecond + duration.section(':', 3, 3).toInt();
         return frames;
     }
-    return (int)((duration.section(':', 0, 0).toInt()*3600.0 + duration.section(':', 1, 1).toInt()*60.0 + duration.section(':', 2, 2).toInt()) * fps + duration.section(':', 3, 3).toInt());
+    return (int)((duration.section(':', 0, 0).toInt()*3600.0 + duration.section(':', 1, 1).toInt()*60.0 + duration.section(':', 2, 2).toInt()) * m_realFps + duration.section(':', 3, 3).toInt());
+}
+
+QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
+{
+    if (frameDisplay) return QString::number((int) time.frames(m_realFps));
+    return getTimecode(time);
 }
 
-QString Timecode::getTimecode(const GenTime & time, double fps) const
+QString Timecode::getTimecode(const GenTime & time) const
 {
     switch (m_format) {
     case HH_MM_SS_FF:
-        return getTimecodeHH_MM_SS_FF(time, fps);
+        return getTimecodeHH_MM_SS_FF(time);
         break;
     case HH_MM_SS_HH:
         return getTimecodeHH_MM_SS_HH(time);
         break;
     case Frames:
-        return getTimecodeFrames(time, fps);
+        return getTimecodeFrames(time);
         break;
     case Seconds:
         return getTimecodeSeconds(time);
@@ -87,11 +100,17 @@ QString Timecode::getTimecode(const GenTime & time, double fps) const
         kWarning() <<
         "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
         << endl;
-        return getTimecodeHH_MM_SS_FF(time, fps);
+        return getTimecodeHH_MM_SS_FF(time);
     }
 }
 
-QString Timecode::getTimecodeFromFrames(int frames)
+const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
+{
+    if (frameDisplay) return QString::number(frames);
+    return getTimecodeHH_MM_SS_FF(frames);
+}
+
+const QString Timecode::getTimecodeFromFrames(int frames) const
 {
     return getTimecodeHH_MM_SS_FF(frames);
 }
@@ -101,7 +120,7 @@ QString Timecode::getTimecodeFromFrames(int frames)
 QString Timecode::getStringTimecode(int frames, const double &fps)
 {
     // Returns the timecode in an hh:mm:ss format
-    int seconds = frames / (int) floor(fps + 0.5);
+    int seconds = (int)(frames / fps);
     int minutes = seconds / 60;
     seconds = seconds % 60;
     int hours = minutes / 60;
@@ -120,9 +139,9 @@ QString Timecode::getStringTimecode(int frames, const double &fps)
 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
 {
     // Returns the timecode in an easily read display, like 3 min. 5 sec.
-    int frames = (int)time.frames(fps);
-    int seconds = frames / (int) floor(fps + 0.5);
-    frames = frames % ((int) fps);
+    int frames = (int) time.frames(fps);
+    int seconds = (int)(frames / fps);
+    frames = frames - ((int)(fps * seconds));
 
     int minutes = seconds / 60;
     seconds = seconds % 60;
@@ -162,16 +181,19 @@ QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
 }
 
 
-QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const
+const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
 {
     if (m_dropFrame)
-        return getTimecodeDropFrame(time, fps);
+        return getTimecodeDropFrame(time);
 
-    return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
+    return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
 }
 
-QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
+const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
 {
+    if (m_dropFrame) {
+        return getTimecodeDropFrame(frames);
+    }
     int seconds = frames / m_displayedFramesPerSecond;
     frames = frames % m_displayedFramesPerSecond;
 
@@ -215,9 +237,9 @@ QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
     return text;
 }
 
-QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const
+QString Timecode::getTimecodeFrames(const GenTime & time) const
 {
-    return QString::number(time.frames(fps));
+    return QString::number(time.frames(m_realFps));
 }
 
 QString Timecode::getTimecodeSeconds(const GenTime & time) const
@@ -225,14 +247,18 @@ QString Timecode::getTimecodeSeconds(const GenTime & time) const
     return QString::number(time.seconds());
 }
 
-QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const
+QString Timecode::getTimecodeDropFrame(const GenTime & time) const
+{
+    return getTimecodeDropFrame((int)time.frames(m_realFps));
+}
+
+QString Timecode::getTimecodeDropFrame(int frames) const
 {
     // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
     // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
-    int frames = (int)time.frames(fps);
 
     // calculate how many frames need to be dropped every minute.
-    int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps)  + 0.5);
+    int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - m_realFps)  + 0.5);
 
     int perMinute = toDrop / 9;
     int tenthMinute = toDrop % 9;