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