]> git.sesse.net Git - kdenlive/blob - src/timecode.cpp
Fix missing initiation of timecode widget.
[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     if (duration.isEmpty()) {
146         return 0;
147     }
148
149     int hours, minutes, seconds, frames;
150     int offset = 0;
151     if (duration.at(0) == '-') {
152         offset = 1;
153         hours = duration.mid(1, 2).toInt();
154     } else {
155         hours = duration.left(2).toInt();
156     }
157     minutes = duration.mid(3 + offset, 2).toInt();
158     seconds = duration.mid(6 + offset, 2).toInt();
159     frames = duration.right(2).toInt();
160     if (m_dropFrameTimecode) {
161         //CONVERT DROP FRAME TIMECODE TO A FRAME NUMBER
162         //Code by David Heidelberger, adapted from Andrew Duncan
163         //Given ints called hours, minutes, seconds, frames, and a double called framerate
164
165         int totalMinutes = (60 * hours) + minutes; //Total number of minutes
166         int frameNumber = ((m_displayedFramesPerSecond * 3600 * hours) + (m_displayedFramesPerSecond * 60 * minutes) + (m_displayedFramesPerSecond * seconds) + frames) - (m_dropFrames * (totalMinutes - floor(totalMinutes / 10)));
167         return frameNumber;
168     }
169     return (int)(hours * 3600.0 + minutes * 60.0 + seconds * m_realFps + frames);
170 }
171
172 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
173 {
174     if (frameDisplay) return QString::number((int) time.frames(m_realFps));
175     return getTimecode(time);
176 }
177
178 QString Timecode::getTimecode(const GenTime & time) const
179 {
180     switch (m_format) {
181     case HH_MM_SS_FF:
182         return getTimecodeHH_MM_SS_FF(time);
183         break;
184     case HH_MM_SS_HH:
185         return getTimecodeHH_MM_SS_HH(time);
186         break;
187     case Frames:
188         return getTimecodeFrames(time);
189         break;
190     case Seconds:
191         return getTimecodeSeconds(time);
192         break;
193     default:
194         kWarning() <<
195         "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
196         << endl;
197         return getTimecodeHH_MM_SS_FF(time);
198     }
199 }
200
201 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
202 {
203     if (frameDisplay) return QString::number(frames);
204     return getTimecodeHH_MM_SS_FF(frames);
205 }
206
207 const QString Timecode::getTimecodeFromFrames(int frames) const
208 {
209     return getTimecodeHH_MM_SS_FF(frames);
210 }
211
212
213 //static
214 QString Timecode::getStringTimecode(int frames, const double &fps)
215 {
216     // Returns the timecode in an hh:mm:ss format
217
218     bool negative = false;
219     if (frames < 0) {
220         negative = true;
221         frames = qAbs(frames);
222     }
223
224     int seconds = (int)(frames / fps);
225     int minutes = seconds / 60;
226     seconds = seconds % 60;
227     int hours = minutes / 60;
228     minutes = minutes % 60;
229     QString text;
230     if (negative)
231         text.append('-');
232     text.append(QString::number(hours).rightJustified(2, '0', false));
233     text.append(':');
234     text.append(QString::number(minutes).rightJustified(2, '0', false));
235     text.append(':');
236     text.append(QString::number(seconds).rightJustified(2, '0', false));
237     return text;
238 }
239
240
241 //static
242 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
243 {
244     // Returns the timecode in an easily read display, like 3 min. 5 sec.
245     int frames = (int) time.frames(fps);
246
247     bool negative = false;
248     if (frames < 0) {
249         negative = true;
250         frames = qAbs(frames);
251     }
252
253     int seconds = (int)(frames / fps);
254     frames = frames - ((int)(fps * seconds));
255
256     int minutes = seconds / 60;
257     seconds = seconds % 60;
258     int hours = minutes / 60;
259     minutes = minutes % 60;
260
261     QString text;
262     bool trim = false;
263
264     if (negative)
265         text.append('-');
266     if (hours != 0) {
267         text.append(QString::number(hours).rightJustified(2, '0', false));
268         text.append(' ' + i18n("hour") + ' ');
269         trim = true;
270     }
271     if (minutes != 0 || trim) {
272         if (!trim) {
273             text.append(QString::number(minutes));
274         } else
275             text.append(QString::number(minutes).rightJustified(2, '0', false));
276         text.append(' ' + i18n("min.") + ' ');
277         trim = true;
278     }
279     if (seconds != 0 || trim) {
280         if (!trim) {
281             text.append(QString::number(seconds));
282         } else
283             text.append(QString::number(seconds).rightJustified(2, '0', false));
284         text.append(' ' + i18n("sec."));
285         trim = true;
286     }
287     if (!trim) {
288         text.append(QString::number(frames));
289         text.append(' ' + i18n("frames"));
290     }
291
292     return text;
293 }
294
295
296 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
297 {
298     if (m_dropFrameTimecode) {
299         return getTimecodeDropFrame(time);
300     }
301     return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
302 }
303
304 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
305 {
306     if (m_dropFrameTimecode) {
307         return getTimecodeDropFrame(frames);
308     }
309
310     bool negative = false;
311     if (frames < 0) {
312         negative = true;
313         frames = qAbs(frames);
314     }
315
316     int seconds = frames / m_displayedFramesPerSecond;
317     frames = frames % m_displayedFramesPerSecond;
318
319     int minutes = seconds / 60;
320     seconds = seconds % 60;
321     int hours = minutes / 60;
322     minutes = minutes % 60;
323
324     QString text;
325     if (negative)
326         text.append('-');
327     text.append(QString::number(hours).rightJustified(2, '0', false));
328     text.append(':');
329     text.append(QString::number(minutes).rightJustified(2, '0', false));
330     text.append(':');
331     text.append(QString::number(seconds).rightJustified(2, '0', false));
332     text.append(':');
333     text.append(QString::number(frames).rightJustified(2, '0', false));
334
335     return text;
336 }
337
338 const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
339 {
340     int hundredths = (int)(time.seconds() * 100);
341
342     bool negative = false;
343     if (hundredths < 0) {
344         negative = true;
345         hundredths = qAbs(hundredths);
346     }
347
348     int seconds = hundredths / 100;
349     hundredths = hundredths % 100;
350     int minutes = seconds / 60;
351     seconds = seconds % 60;
352     int hours = minutes / 60;
353     minutes = minutes % 60;
354
355     QString text;
356     if (negative)
357         text.append('-');
358     text.append(QString::number(hours).rightJustified(2, '0', false));
359     text.append(':');
360     text.append(QString::number(minutes).rightJustified(2, '0', false));
361     text.append(':');
362     text.append(QString::number(seconds).rightJustified(2, '0', false));
363     if (m_dropFrameTimecode)
364         text.append(',');
365     else
366         text.append(':');
367     text.append(QString::number(hundredths).rightJustified(2, '0', false));
368
369     return text;
370 }
371
372 const QString Timecode::getTimecodeFrames(const GenTime & time) const
373 {
374     return QString::number((int) time.frames(m_realFps));
375 }
376
377 const QString Timecode::getTimecodeSeconds(const GenTime & time) const
378 {
379     QLocale locale;
380     return locale.toString(time.seconds());
381 }
382
383 const QString Timecode::getTimecodeDropFrame(const GenTime & time) const
384 {
385     return getTimecodeDropFrame((int)time.frames(m_realFps));
386 }
387
388 const QString Timecode::getTimecodeDropFrame(int framenumber) const
389 {
390     //CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE
391     //Based on code by David Heidelberger, adapted from Andrew Duncan
392     //Given an int called framenumber and a double called framerate
393     //Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
394
395     bool negative = false;
396     if (framenumber < 0) {
397         negative = true;
398         framenumber = qAbs(framenumber);
399     }
400
401     int d = floor(framenumber / m_framesPer10Minutes);
402     int m = framenumber % m_framesPer10Minutes;
403
404     if (m > m_dropFrames) {
405         framenumber += (m_dropFrames * 9 * d) + m_dropFrames * (floor((m - m_dropFrames) / (round(m_realFps * 60) - m_dropFrames)));
406     } else {
407         framenumber += m_dropFrames * 9 * d;
408     }
409
410     int frames = framenumber % m_displayedFramesPerSecond;
411     int seconds = (int) floor(framenumber / m_displayedFramesPerSecond) % 60;
412     int minutes = (int) floor(floor(framenumber / m_displayedFramesPerSecond) / 60) % 60;
413     int hours = floor(floor(floor(framenumber / m_displayedFramesPerSecond) / 60) / 60);
414
415     QString text;
416     if (negative)
417         text.append('-');
418     text.append(QString::number(hours).rightJustified(2, '0', false));
419     text.append(':');
420     text.append(QString::number(minutes).rightJustified(2, '0', false));
421     text.append(':');
422     text.append(QString::number(seconds).rightJustified(2, '0', false));
423     text.append(',');
424     text.append(QString::number(frames).rightJustified(2, '0', false));
425
426     return text;
427 }