]> git.sesse.net Git - kdenlive/blob - src/timecode.cpp
Fix timecode widget sometimes hard to edit
[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  *   Copyright (C) 2010 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
8  ***************************************************************************/
9
10 /***************************************************************************
11  *                                                                         *
12  *   This program is free software; you can redistribute it and/or modify  *
13  *   it under the terms of the GNU General Public License as published by  *
14  *   the Free Software Foundation; either version 2 of the License, or     *
15  *   (at your option) any later version.                                   *
16  *                                                                         *
17  ***************************************************************************/
18
19 /*
20
21  Timecode calculation code for reference
22  If we ever use Quicktime timecode with 50.94 Drop frame, keep in mind that there is a bug inthe Quicktime code
23
24 //CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE
25 //Code by David Heidelberger, adapted from Andrew Duncan
26 //Given an int called framenumber and a double called framerate
27 //Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
28
29 int d;
30 int m;
31
32 int dropFrames = round(framerate * .066666); //Number of frames to drop on the minute marks is the nearest integer to 6% of the framerate
33 int framesPerHour = round(framerate*60*60); //Number of frames in an hour
34 int framesPer24Hours = framesPerHour*24; //Number of frames in a day - timecode rolls over after 24 hours
35 int framesPer10Minutes = round(framerate * 60 * 10); //Number of frames per ten minutes
36 int framesPerMinute = round(framerate)*60)-  dropFrames; //Number of frames per minute is the round of the framerate * 60 minus the number of dropped frames
37
38 if (framenumber<0) //Negative time. Add 24 hours.
39 {
40     framenumber=framesPer24Hours+framenumber;
41 }
42
43 //If framenumber is greater than 24 hrs, next operation will rollover clock
44 framenumber = framenumber % framesPer24Hours; //% is the modulus operator, which returns a remainder. a % b = the remainder of a/b
45
46 d = framenumber\framesPer10Minutes; // \ means integer division, which is a/b without a remainder. Some languages you could use floor(a/b)
47 m = framenumber % framesPer10Minutes;
48
49 if (m>1)
50 {
51     framenumber=framenumber + (dropFrames*9*d) + dropFrames*((m-dropFrames)\framesPerMinute);
52 }
53 else
54 {
55     framenumber = framenumber + dropFrames*9*d;
56 }
57
58 int frRound = round(framerate);
59 int frames = framenumber % frRound;
60 int seconds = (framenumber \ frRound) % 60;
61 int minutes = ((framenumber \ frRound) \ 60) % 60;
62 int hours = (((framenumber \ frRound) \ 60) \ 60);
63
64
65 ------------------------------------------------------------------------------------
66
67 //CONVERT DROP FRAME TIMECODE TO A FRAME NUMBER
68 //Code by David Heidelberger, adapted from Andrew Duncan
69 //Given ints called hours, minutes, seconds, frames, and a double called framerate
70
71 int dropFrames = round(framerate*.066666); //Number of drop frames is 6% of framerate rounded to nearest integer
72 int timeBase = round(framerate); //We don’t need the exact framerate anymore, we just need it rounded to nearest integer
73
74 int hourFrames = timeBase*60*60; //Number of frames per hour (non-drop)
75 int minuteFrames = timeBase*60; //Number of frames per minute (non-drop)
76 int totalMinutes = (60*hours) + minutes; //Total number of minuts
77 int frameNumber = ((hourFrames * hours) + (minuteFrames * minutes) + (timeBase * seconds) + frames) - (dropFrames * (totalMinutes - (totalMinutes \ 10)));
78 return frameNumber;
79
80 */
81
82
83
84 #include <KDebug>
85 #include <KLocale>
86
87 #include "timecode.h"
88
89 Timecode::Timecode(Formats format, double framesPerSecond)
90 {
91     setFormat(framesPerSecond, format);
92 }
93
94 Timecode::~Timecode()
95 {
96 }
97
98 void Timecode::setFormat(double framesPerSecond, Formats format)
99 {
100     m_displayedFramesPerSecond = (int)(framesPerSecond + 0.5);
101     m_dropFrameTimecode = (framesPerSecond / 1.00 != (int)framesPerSecond) ;
102     m_format = format;
103     m_realFps = framesPerSecond;
104     if (m_dropFrameTimecode) {
105         m_dropFrames = round(m_realFps * .066666); //Number of frames to drop on the minute marks is the nearest integer to 6% of the framerate
106         m_framesPer10Minutes = round(m_realFps * 600); //Number of frames per ten minutes
107     }
108 }
109
110 double Timecode::fps() const
111 {
112     return m_realFps;
113 }
114
115 bool Timecode::df() const
116 {
117     return m_dropFrameTimecode;
118 }
119
120 const QString Timecode::mask(GenTime t) const
121 {
122     if (t < GenTime()) {
123         if (m_dropFrameTimecode) return "#99:99:99,99";
124         else return "#99:99:99:99";
125     }
126     if (m_dropFrameTimecode) return "99:99:99,99";
127     else return "99:99:99:99";
128 }
129
130 QString Timecode::reformatSeparators(QString duration) const
131 {
132     if (m_dropFrameTimecode)
133         return duration.replace(8, 1, ',');
134     return duration.replace(8, 1, ':');
135 }
136
137 int Timecode::getDisplayFrameCount(const QString &duration, bool frameDisplay) const
138 {
139     if (frameDisplay) return duration.toInt();
140     return getFrameCount(duration);
141 }
142
143 int Timecode::getFrameCount(const QString &duration) const
144 {
145     int hours, minutes, seconds, frames;
146     int offset = 0;
147     if (duration.at(0) == '-') {
148         offset = 1;
149         hours = duration.mid(1, 2).toInt();
150     }
151     else hours = duration.left(2).toInt();
152     minutes = duration.mid(3 + offset, 2).toInt();
153     seconds = duration.mid(6 + offset, 2).toInt();
154     frames = duration.right(2).toInt();
155     if (m_dropFrameTimecode) {
156         //CONVERT DROP FRAME TIMECODE TO A FRAME NUMBER
157         //Code by David Heidelberger, adapted from Andrew Duncan
158         //Given ints called hours, minutes, seconds, frames, and a double called framerate
159
160         int totalMinutes = (60 * hours) + minutes; //Total number of minutes
161         int frameNumber = ((m_displayedFramesPerSecond * 3600 * hours) + (m_displayedFramesPerSecond * 60 * minutes) + (m_displayedFramesPerSecond * seconds) + frames) - (m_dropFrames * (totalMinutes - floor(totalMinutes / 10)));
162         return frameNumber;
163     }
164     return (int)(hours * 3600.0 + minutes * 60.0 + seconds * m_realFps + frames);
165 }
166
167 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
168 {
169     if (frameDisplay) return QString::number((int) time.frames(m_realFps));
170     return getTimecode(time);
171 }
172
173 QString Timecode::getTimecode(const GenTime & time) const
174 {
175     switch (m_format) {
176     case HH_MM_SS_FF:
177         return getTimecodeHH_MM_SS_FF(time);
178         break;
179     case HH_MM_SS_HH:
180         return getTimecodeHH_MM_SS_HH(time);
181         break;
182     case Frames:
183         return getTimecodeFrames(time);
184         break;
185     case Seconds:
186         return getTimecodeSeconds(time);
187         break;
188     default:
189         kWarning() <<
190         "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
191         << endl;
192         return getTimecodeHH_MM_SS_FF(time);
193     }
194 }
195
196 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
197 {
198     if (frameDisplay) return QString::number(frames);
199     return getTimecodeHH_MM_SS_FF(frames);
200 }
201
202 const QString Timecode::getTimecodeFromFrames(int frames) const
203 {
204     return getTimecodeHH_MM_SS_FF(frames);
205 }
206
207
208 //static
209 QString Timecode::getStringTimecode(int frames, const double &fps)
210 {
211     // Returns the timecode in an hh:mm:ss format
212
213     bool negative = false;
214     if (frames < 0) {
215         negative = true;
216         frames = qAbs(frames);
217     }
218
219     int seconds = (int)(frames / fps);
220     int minutes = seconds / 60;
221     seconds = seconds % 60;
222     int hours = minutes / 60;
223     minutes = minutes % 60;
224     QString text;
225     if (negative)
226         text.append('-');
227     text.append(QString::number(hours).rightJustified(2, '0', false));
228     text.append(':');
229     text.append(QString::number(minutes).rightJustified(2, '0', false));
230     text.append(':');
231     text.append(QString::number(seconds).rightJustified(2, '0', false));
232     return text;
233 }
234
235
236 //static
237 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
238 {
239     // Returns the timecode in an easily read display, like 3 min. 5 sec.
240     int frames = (int) time.frames(fps);
241
242     bool negative = false;
243     if (frames < 0) {
244         negative = true;
245         frames = qAbs(frames);
246     }
247
248     int seconds = (int)(frames / fps);
249     frames = frames - ((int)(fps * seconds));
250
251     int minutes = seconds / 60;
252     seconds = seconds % 60;
253     int hours = minutes / 60;
254     minutes = minutes % 60;
255
256     QString text;
257     bool trim = false;
258
259     if (negative)
260         text.append('-');
261     if (hours != 0) {
262         text.append(QString::number(hours).rightJustified(2, '0', false));
263         text.append(' ' + i18n("hour") + ' ');
264         trim = true;
265     }
266     if (minutes != 0 || trim) {
267         if (!trim) {
268             text.append(QString::number(minutes));
269         } else
270             text.append(QString::number(minutes).rightJustified(2, '0', false));
271         text.append(' ' + i18n("min.") + ' ');
272         trim = true;
273     }
274     if (seconds != 0 || trim) {
275         if (!trim) {
276             text.append(QString::number(seconds));
277         } else
278             text.append(QString::number(seconds).rightJustified(2, '0', false));
279         text.append(' ' + i18n("sec."));
280         trim = true;
281     }
282     if (!trim) {
283         text.append(QString::number(frames));
284         text.append(' ' + i18n("frames"));
285     }
286
287     return text;
288 }
289
290
291 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
292 {
293     if (m_dropFrameTimecode) {
294         return getTimecodeDropFrame(time);
295     }
296     return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
297 }
298
299 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
300 {
301     if (m_dropFrameTimecode) {
302         return getTimecodeDropFrame(frames);
303     }
304
305     bool negative = false;
306     if (frames < 0) {
307         negative = true;
308         frames = qAbs(frames);
309     }
310
311     int seconds = frames / m_displayedFramesPerSecond;
312     frames = frames % m_displayedFramesPerSecond;
313
314     int minutes = seconds / 60;
315     seconds = seconds % 60;
316     int hours = minutes / 60;
317     minutes = minutes % 60;
318
319     QString text;
320     if (negative)
321         text.append('-');
322     text.append(QString::number(hours).rightJustified(2, '0', false));
323     text.append(':');
324     text.append(QString::number(minutes).rightJustified(2, '0', false));
325     text.append(':');
326     text.append(QString::number(seconds).rightJustified(2, '0', false));
327     text.append(':');
328     text.append(QString::number(frames).rightJustified(2, '0', false));
329
330     return text;
331 }
332
333 const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
334 {
335     int hundredths = (int)(time.seconds() * 100);
336
337     bool negative = false;
338     if (hundredths < 0) {
339         negative = true;
340         hundredths = qAbs(hundredths);
341     }
342
343     int seconds = hundredths / 100;
344     hundredths = hundredths % 100;
345     int minutes = seconds / 60;
346     seconds = seconds % 60;
347     int hours = minutes / 60;
348     minutes = minutes % 60;
349
350     QString text;
351     if (negative)
352         text.append('-');
353     text.append(QString::number(hours).rightJustified(2, '0', false));
354     text.append(':');
355     text.append(QString::number(minutes).rightJustified(2, '0', false));
356     text.append(':');
357     text.append(QString::number(seconds).rightJustified(2, '0', false));
358     if (m_dropFrameTimecode)
359         text.append(',');
360     else
361         text.append(':');
362     text.append(QString::number(hundredths).rightJustified(2, '0', false));
363
364     return text;
365 }
366
367 const QString Timecode::getTimecodeFrames(const GenTime & time) const
368 {
369     return QString::number((int) time.frames(m_realFps));
370 }
371
372 const QString Timecode::getTimecodeSeconds(const GenTime & time) const
373 {
374     QLocale locale;
375     return locale.toString(time.seconds());
376 }
377
378 const QString Timecode::getTimecodeDropFrame(const GenTime & time) const
379 {
380     return getTimecodeDropFrame((int)time.frames(m_realFps));
381 }
382
383 const QString Timecode::getTimecodeDropFrame(int framenumber) const
384 {
385     //CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE
386     //Based on code by David Heidelberger, adapted from Andrew Duncan
387     //Given an int called framenumber and a double called framerate
388     //Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
389
390     bool negative = false;
391     if (framenumber < 0) {
392         negative = true;
393         framenumber = qAbs(framenumber);
394     }
395
396     int d = floor(framenumber / m_framesPer10Minutes);
397     int m = framenumber % m_framesPer10Minutes;
398
399     if (m > m_dropFrames) {
400         framenumber += (m_dropFrames * 9 * d) + m_dropFrames * (floor((m - m_dropFrames) / (round(m_realFps * 60) - m_dropFrames)));
401     } else {
402         framenumber += m_dropFrames * 9 * d;
403     }
404
405     int frames = framenumber % m_displayedFramesPerSecond;
406     int seconds = (int) floor(framenumber / m_displayedFramesPerSecond) % 60;
407     int minutes = (int) floor(floor(framenumber / m_displayedFramesPerSecond) / 60) % 60;
408     int hours = floor(floor(floor(framenumber / m_displayedFramesPerSecond) / 60) / 60);
409
410     QString text;
411     if (negative)
412         text.append('-');
413     text.append(QString::number(hours).rightJustified(2, '0', false));
414     text.append(':');
415     text.append(QString::number(minutes).rightJustified(2, '0', false));
416     text.append(':');
417     text.append(QString::number(seconds).rightJustified(2, '0', false));
418     text.append(',');
419     text.append(QString::number(frames).rightJustified(2, '0', false));
420
421     return text;
422 }