]> git.sesse.net Git - kdenlive/blob - src/timecode.cpp
cleanup
[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 #include <QValidator>
84
85 #include <KDebug>
86 #include <KLocale>
87
88 #include "timecode.h"
89
90 Timecode::Timecode(Formats format, double framesPerSecond)
91 {
92     m_validator = new QRegExpValidator(0);
93     setFormat(framesPerSecond, format);
94 }
95
96 Timecode::~Timecode()
97 {
98 }
99
100 void Timecode::setFormat(double framesPerSecond, Formats format)
101 {
102     m_displayedFramesPerSecond = (int)(framesPerSecond + 0.5);
103     m_dropFrameTimecode = (framesPerSecond / 1.00 != (int)framesPerSecond) ;
104     m_format = format;
105     m_realFps = framesPerSecond;
106     if (m_dropFrameTimecode) {
107         m_dropFrames = round(m_realFps * .066666); //Number of frames to drop on the minute marks is the nearest integer to 6% of the framerate
108         m_framesPer10Minutes = round(m_realFps * 600); //Number of frames per ten minutes
109     }
110     QRegExp regExp;
111     if (m_dropFrameTimecode)
112         regExp.setPattern("^\\d{2}:\\d{2}:\\d{2};\\d{2}$");
113     else
114         regExp.setPattern("^\\d{2}:\\d{2}:\\d{2}:\\d{2}$");
115     m_validator->setRegExp(regExp);
116 }
117
118 double Timecode::fps() const
119 {
120     return m_realFps;
121 }
122
123 bool Timecode::df() const
124 {
125     return m_dropFrameTimecode;
126 }
127
128 const QValidator *Timecode::validator() const
129 {
130     return m_validator;
131 }
132
133 QString Timecode::reformatSeparators(QString duration) const
134 {
135     if (m_dropFrameTimecode)
136         return duration.replace(8, 1, ';');
137     return duration.replace(8, 1, ':');
138 }
139
140 int Timecode::getDisplayFrameCount(const QString duration, bool frameDisplay) const
141 {
142     if (frameDisplay) return duration.toInt();
143     return getFrameCount(duration);
144 }
145
146 int Timecode::getFrameCount(const QString duration) const
147 {
148     if (m_dropFrameTimecode) {
149
150         //CONVERT DROP FRAME TIMECODE TO A FRAME NUMBER
151         //Code by David Heidelberger, adapted from Andrew Duncan
152         //Given ints called hours, minutes, seconds, frames, and a double called framerate
153
154         //Get Hours, Minutes, Seconds, Frames from timecode
155         int hours, minutes, seconds, frames;
156
157         hours = duration.section(':', 0, 0).toInt();
158         minutes = duration.section(':', 1, 1).toInt();
159         if (duration.contains(';')) {
160             seconds = duration.section(';', 0, 0).section(':', 2, 2).toInt();
161             frames = duration.section(';', 1, 1).toInt();
162         } else {
163             //Handle Drop Frame timecode frame calculations, even if the timecode supplied uses incorrect "99:99:99:99" format instead of "99:99:99;99"
164             seconds = duration.section(':', 2, 2).toInt();
165             frames = duration.section(':', 3, 3).toInt();
166         }
167
168         int totalMinutes = (60 * hours) + minutes; //Total number of minutes
169         int frameNumber = ((m_displayedFramesPerSecond * 3600 * hours) + (m_displayedFramesPerSecond * 60 * minutes) + (m_displayedFramesPerSecond * seconds) + frames) - (m_dropFrames * (totalMinutes - floor(totalMinutes / 10)));
170         return frameNumber;
171     }
172     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());
173 }
174
175 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
176 {
177     if (frameDisplay) return QString::number((int) time.frames(m_realFps));
178     return getTimecode(time);
179 }
180
181 QString Timecode::getTimecode(const GenTime & time) const
182 {
183     switch (m_format) {
184     case HH_MM_SS_FF:
185         return getTimecodeHH_MM_SS_FF(time);
186         break;
187     case HH_MM_SS_HH:
188         return getTimecodeHH_MM_SS_HH(time);
189         break;
190     case Frames:
191         return getTimecodeFrames(time);
192         break;
193     case Seconds:
194         return getTimecodeSeconds(time);
195         break;
196     default:
197         kWarning() <<
198         "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
199         << endl;
200         return getTimecodeHH_MM_SS_FF(time);
201     }
202 }
203
204 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
205 {
206     if (frameDisplay) return QString::number(frames);
207     return getTimecodeHH_MM_SS_FF(frames);
208 }
209
210 const QString Timecode::getTimecodeFromFrames(int frames) const
211 {
212     return getTimecodeHH_MM_SS_FF(frames);
213 }
214
215
216 //static
217 QString Timecode::getStringTimecode(int frames, const double &fps)
218 {
219     // Returns the timecode in an hh:mm:ss format
220     int seconds = (int)(frames / fps);
221     int minutes = seconds / 60;
222     seconds = seconds % 60;
223     int hours = minutes / 60;
224     minutes = minutes % 60;
225     QString text;
226     text.append(QString::number(hours).rightJustified(2, '0', false));
227     text.append(':');
228     text.append(QString::number(minutes).rightJustified(2, '0', false));
229     text.append(':');
230     text.append(QString::number(seconds).rightJustified(2, '0', false));
231     return text;
232 }
233
234
235 //static
236 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
237 {
238     // Returns the timecode in an easily read display, like 3 min. 5 sec.
239     int frames = (int) time.frames(fps);
240     int seconds = (int)(frames / fps);
241     frames = frames - ((int)(fps * seconds));
242
243     int minutes = seconds / 60;
244     seconds = seconds % 60;
245     int hours = minutes / 60;
246     minutes = minutes % 60;
247
248     QString text;
249     bool trim = false;
250
251     if (hours != 0) {
252         text.append(QString::number(hours).rightJustified(2, '0', false));
253         text.append(' ' + i18n("hour") + ' ');
254         trim = true;
255     }
256     if (minutes != 0 || trim) {
257         if (!trim) {
258             text.append(QString::number(minutes));
259         } else
260             text.append(QString::number(minutes).rightJustified(2, '0', false));
261         text.append(' ' + i18n("min.") + ' ');
262         trim = true;
263     }
264     if (seconds != 0 || trim) {
265         if (!trim) {
266             text.append(QString::number(seconds));
267         } else
268             text.append(QString::number(seconds).rightJustified(2, '0', false));
269         text.append(' ' + i18n("sec."));
270         trim = true;
271     }
272     if (!trim) {
273         text.append(QString::number(frames));
274         text.append(' ' + i18n("frames"));
275     }
276
277     return text;
278 }
279
280
281 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
282 {
283     if (m_dropFrameTimecode) {
284         return getTimecodeDropFrame(time);
285     }
286     return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
287 }
288
289 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
290 {
291     if (m_dropFrameTimecode) {
292         return getTimecodeDropFrame(frames);
293     }
294     int seconds = frames / m_displayedFramesPerSecond;
295     frames = frames % m_displayedFramesPerSecond;
296
297     int minutes = seconds / 60;
298     seconds = seconds % 60;
299     int hours = minutes / 60;
300     minutes = minutes % 60;
301
302     QString text;
303     text.append(QString::number(hours).rightJustified(2, '0', false));
304     text.append(':');
305     text.append(QString::number(minutes).rightJustified(2, '0', false));
306     text.append(':');
307     text.append(QString::number(seconds).rightJustified(2, '0', false));
308     text.append(':');
309     text.append(QString::number(frames).rightJustified(2, '0', false));
310
311     return text;
312 }
313
314 const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
315 {
316     int hundredths = (int)(time.seconds() * 100);
317     int seconds = hundredths / 100;
318     hundredths = hundredths % 100;
319     int minutes = seconds / 60;
320     seconds = seconds % 60;
321     int hours = minutes / 60;
322     minutes = minutes % 60;
323
324     QString text;
325
326     text.append(QString::number(hours).rightJustified(2, '0', false));
327     text.append(':');
328     text.append(QString::number(minutes).rightJustified(2, '0', false));
329     text.append(':');
330     text.append(QString::number(seconds).rightJustified(2, '0', false));
331     if (m_dropFrameTimecode)
332         text.append(';');
333     else
334         text.append(':');
335     text.append(QString::number(hundredths).rightJustified(2, '0', false));
336
337     return text;
338 }
339
340 const QString Timecode::getTimecodeFrames(const GenTime & time) const
341 {
342     return QString::number(time.frames(m_realFps));
343 }
344
345 const QString Timecode::getTimecodeSeconds(const GenTime & time) const
346 {
347     return QString::number(time.seconds());
348 }
349
350 const QString Timecode::getTimecodeDropFrame(const GenTime & time) const
351 {
352     return getTimecodeDropFrame((int)time.frames(m_realFps));
353 }
354
355 const QString Timecode::getTimecodeDropFrame(int framenumber) const
356 {
357     //CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE
358     //Based on code by David Heidelberger, adapted from Andrew Duncan
359     //Given an int called framenumber and a double called framerate
360     //Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
361
362     int d = floor(framenumber / m_framesPer10Minutes);
363     int m = framenumber % m_framesPer10Minutes;
364
365     if (m > m_dropFrames) {
366         framenumber += (m_dropFrames * 9 * d) + m_dropFrames * (floor((m - m_dropFrames) / (round(m_realFps * 60) - m_dropFrames)));
367     } else {
368         framenumber += m_dropFrames * 9 * d;
369     }
370
371     int frames = framenumber % m_displayedFramesPerSecond;
372     int seconds = (int) floor(framenumber / m_displayedFramesPerSecond) % 60;
373     int minutes = (int) floor(floor(framenumber / m_displayedFramesPerSecond) / 60) % 60;
374     int hours = floor(floor(floor(framenumber / m_displayedFramesPerSecond) / 60) / 60);
375
376     QString text;
377     text.append(QString::number(hours).rightJustified(2, '0', false));
378     text.append(':');
379     text.append(QString::number(minutes).rightJustified(2, '0', false));
380     text.append(':');
381     text.append(QString::number(seconds).rightJustified(2, '0', false));
382     text.append(';');
383     text.append(QString::number(frames).rightJustified(2, '0', false));
384
385     return text;
386 }