]> git.sesse.net Git - kdenlive/blob - src/timecode.cpp
Progress in clip monitor and project switching
[kdenlive] / src / timecode.cpp
1 /***************************************************************************
2                           timecode  -  description
3                              -------------------
4     begin                : Wed Dec 17 2003
5     copyright            : (C) 2003 by Jason Wood
6     email                : jasonwood@blueyonder.co.uk
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 #include "timecode.h"
18
19 #include <kdebug.h>
20 #include <klocale.h>
21
22 Timecode::Timecode(Formats format, int framesPerSecond,
23     bool dropFrame):m_format(format), m_dropFrame(dropFrame),
24 m_displayedFramesPerSecond(framesPerSecond)
25 {
26 }
27
28 Timecode::~Timecode()
29 {
30 }
31
32
33 int Timecode::getFrameCount(const QString duration, double fps) const
34 {
35     if (m_dropFrame) {
36         // calculate how many frames need to be dropped every minute.
37         int frames;
38         int toDrop = (int) floor (600.0 * (m_displayedFramesPerSecond - fps)  + 0.5);
39
40         int perMinute = toDrop / 9;
41         int tenthMinute = toDrop % 9;
42
43         // calculate how many frames are in a normal minute, and how many are in a tenth minute.
44         int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
45         int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
46
47         // Number of actual frames in a 10 minute interval :
48         int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
49         frames = 6 * duration.section(":",0,0).toInt() * tenMinutes;
50         int minutes = duration.section(":",1,1).toInt();
51         frames += ((int) minutes / 10) * tenMinutes;
52         int mins = minutes % 10;
53         if (mins > 0) {
54             frames += tenthMinuteFrames;
55             mins--;
56             if (mins > 0) frames += mins * normalMinuteFrames;
57         }
58         if (minutes % 10 > 0) frames -= perMinute;
59         frames += duration.section(":",2,2).toInt() * m_displayedFramesPerSecond + duration.section(":",3,3).toInt();
60         return frames;  
61     }
62     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());
63 }
64
65 QString Timecode::getTimecode(const GenTime & time, double fps) const
66 {
67     switch (m_format) {
68     case HH_MM_SS_FF:
69         return getTimecodeHH_MM_SS_FF(time, fps);
70         break;
71     case HH_MM_SS_HH:
72         return getTimecodeHH_MM_SS_HH(time);
73         break;
74     case Frames:
75         return getTimecodeFrames(time, fps);
76         break;
77     case Seconds:
78         return getTimecodeSeconds(time);
79         break;
80     default:
81         kWarning() <<
82             "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
83             << endl;
84         return getTimecodeHH_MM_SS_FF(time, fps);
85     }
86 }
87
88 QString Timecode::getTimecodeFromFrames(int frames)
89 {
90     return getTimecodeHH_MM_SS_FF(frames);
91 }
92
93 //static 
94 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
95 {
96     // Returns the timecode in an easily read display, like 3 min. 5 sec.
97     int frames = (int)time.frames(fps);
98     int seconds = frames / (int) floor(fps + 0.5);
99     frames = frames % ((int) fps);
100
101     int minutes = seconds / 60;
102     seconds = seconds % 60;
103     int hours = minutes / 60;
104     minutes = minutes % 60;
105
106     QString text;
107     bool trim = false;
108
109     if (hours!= 0) {
110         text.append(QString::number(hours).rightJustified(2, '0', FALSE));
111         text.append(" " + i18n("hour") + " ");
112         trim = true;
113     }
114     if (minutes!= 0 || trim) {
115         if (!trim) {
116             text.append(QString::number(minutes));
117         }
118         else
119             text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
120         text.append(" " + i18n("min.") + " ");
121         trim = true;
122     }
123     if (seconds!= 0 || trim) {
124         if (!trim) {
125             text.append(QString::number(seconds));
126         }
127         else 
128             text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
129         text.append(" " + i18n("sec."));
130         trim = true;
131     }
132     if (!trim) {
133             text.append(QString::number(frames));
134             text.append(" " + i18n("frames"));
135     }
136
137     return text;
138 }
139
140
141 QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const
142 {
143     if (m_dropFrame)
144         return getTimecodeDropFrame(time, fps);
145
146     return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
147 }
148
149 QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
150 {
151     int seconds = frames / m_displayedFramesPerSecond;
152     frames = frames % m_displayedFramesPerSecond;
153
154     int minutes = seconds / 60;
155     seconds = seconds % 60;
156     int hours = minutes / 60;
157     minutes = minutes % 60;
158
159     QString text;
160
161     text.append(QString::number(hours).rightJustified(2, '0', FALSE));
162     text.append(":");
163     text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
164     text.append(":");
165     text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
166     text.append(":");
167     text.append(QString::number(frames).rightJustified(2, '0', FALSE));
168
169     return text;
170 }
171
172 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
173 {
174     int hundredths = (int)(time.seconds() * 100);
175     int seconds = hundredths / 100;
176     hundredths = hundredths % 100;
177     int minutes = seconds / 60;
178     seconds = seconds % 60;
179     int hours = minutes / 60;
180     minutes = minutes % 60;
181
182     QString text;
183
184     text.append(QString::number(hours).rightJustified(2, '0', FALSE));
185     text.append(":");
186     text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
187     text.append(":");
188     text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
189     text.append(":");
190     text.append(QString::number(hundredths).rightJustified(2, '0', FALSE));
191
192     return text;
193 }
194
195 QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const
196 {
197     return QString::number(time.frames(fps));
198 }
199
200 QString Timecode::getTimecodeSeconds(const GenTime & time) const
201 {
202     return QString::number(time.seconds());
203 }
204
205 QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const
206 {
207     // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
208     // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
209     int frames = (int)time.frames(fps);
210
211     // calculate how many frames need to be dropped every minute.
212     int toDrop = (int) floor (600.0 * (m_displayedFramesPerSecond - fps)  + 0.5);
213
214     int perMinute = toDrop / 9;
215     int tenthMinute = toDrop % 9;
216
217     // calculate how many frames are in a normal minute, and how many are in a tenth minute.
218     int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
219     int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
220
221     // Number of actual frames in a 10 minute interval :
222     int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
223
224     int tenMinuteIntervals = frames / tenMinutes;
225     frames = frames % tenMinutes;
226
227     int hours = tenMinuteIntervals / 6;
228     tenMinuteIntervals = tenMinuteIntervals % 6;
229
230     // At the point, we have figured out HH:M?:??:??
231
232     int numMinutes;
233
234     if (frames < tenthMinuteFrames) {
235         // tenth minute logic applies.
236         numMinutes = 0;
237     } else {
238         // normal minute logic applies.
239         numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
240         frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
241         frames +=  tenthMinute + perMinute;
242     }
243     // We now have HH:MM:??:??
244
245     int seconds = frames / m_displayedFramesPerSecond;
246     frames = frames % m_displayedFramesPerSecond;
247
248     // We now have HH:MM:SS:FF
249
250     // THANK FUCK FOR THAT.
251
252     QString text;
253     text.append(QString::number(hours).rightJustified(2, '0', FALSE));
254     text.append(":");
255     text.append(QString::number(tenMinuteIntervals));
256     text.append(QString::number(numMinutes));
257     text.append(":");
258     text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
259     text.append(":");
260     text.append(QString::number(frames).rightJustified(2, '0', FALSE));
261
262     return text;
263 }