1 /***************************************************************************
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 ***************************************************************************/
10 /***************************************************************************
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. *
17 ***************************************************************************/
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
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.
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
38 if (framenumber<0) //Negative time. Add 24 hours.
40 framenumber=framesPer24Hours+framenumber;
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
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;
51 framenumber=framenumber + (dropFrames*9*d) + dropFrames*((m-dropFrames)\framesPerMinute);
55 framenumber = framenumber + dropFrames*9*d;
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);
65 ------------------------------------------------------------------------------------
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
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
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)));
89 Timecode::Timecode(Formats format, double framesPerSecond)
91 setFormat(framesPerSecond, format);
98 void Timecode::setFormat(double framesPerSecond, Formats format)
100 m_displayedFramesPerSecond = (int)(framesPerSecond + 0.5);
101 m_dropFrameTimecode = (framesPerSecond / 1.00 != (int)framesPerSecond) ;
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
110 double Timecode::fps() const
115 bool Timecode::df() const
117 return m_dropFrameTimecode;
120 const QString Timecode::mask(GenTime t) const
123 if (m_dropFrameTimecode) return "#99:99:99,99";
124 else return "#99:99:99:99";
126 if (m_dropFrameTimecode) return "99:99:99,99";
127 else return "99:99:99:99";
130 QString Timecode::reformatSeparators(QString duration) const
132 if (m_dropFrameTimecode)
133 return duration.replace(8, 1, ',');
134 return duration.replace(8, 1, ':');
137 int Timecode::getDisplayFrameCount(const QString &duration, bool frameDisplay) const
139 if (frameDisplay) return duration.toInt();
140 return getFrameCount(duration);
143 int Timecode::getFrameCount(const QString &duration) const
145 if (duration.isEmpty()) {
148 int hours, minutes, seconds, frames;
150 if (duration.at(0) == '-') {
152 hours = duration.mid(1, 2).toInt();
154 hours = duration.left(2).toInt();
156 minutes = duration.mid(3 + offset, 2).toInt();
157 seconds = duration.mid(6 + offset, 2).toInt();
158 frames = duration.right(2).toInt();
159 if (m_dropFrameTimecode) {
160 //CONVERT DROP FRAME TIMECODE TO A FRAME NUMBER
161 //Code by David Heidelberger, adapted from Andrew Duncan
162 //Given ints called hours, minutes, seconds, frames, and a double called framerate
164 int totalMinutes = (60 * hours) + minutes; //Total number of minutes
165 int frameNumber = ((m_displayedFramesPerSecond * 3600 * hours) + (m_displayedFramesPerSecond * 60 * minutes) + (m_displayedFramesPerSecond * seconds) + frames) - (m_dropFrames * (totalMinutes - floor(totalMinutes / 10)));
168 return (int)((hours * 3600.0 + minutes * 60.0 + seconds) * m_realFps + frames);
171 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
173 if (frameDisplay) return QString::number((int) time.frames(m_realFps));
174 return getTimecode(time);
177 QString Timecode::getTimecode(const GenTime & time) const
181 return getTimecodeHH_MM_SS_FF(time);
184 return getTimecodeHH_MM_SS_HH(time);
187 return getTimecodeFrames(time);
190 return getTimecodeSeconds(time);
194 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
196 return getTimecodeHH_MM_SS_FF(time);
200 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
202 if (frameDisplay) return QString::number(frames);
203 return getTimecodeHH_MM_SS_FF(frames);
206 const QString Timecode::getTimecodeFromFrames(int frames) const
208 return getTimecodeHH_MM_SS_FF(frames);
213 QString Timecode::getStringTimecode(int frames, const double &fps)
215 // Returns the timecode in an hh:mm:ss format
217 bool negative = false;
220 frames = qAbs(frames);
223 int seconds = (int)(frames / fps);
224 int minutes = seconds / 60;
225 seconds = seconds % 60;
226 int hours = minutes / 60;
227 minutes = minutes % 60;
231 text.append(QString::number(hours).rightJustified(2, '0', false));
233 text.append(QString::number(minutes).rightJustified(2, '0', false));
235 text.append(QString::number(seconds).rightJustified(2, '0', false));
241 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
243 // Returns the timecode in an easily read display, like 3 min. 5 sec.
244 int frames = (int) time.frames(fps);
246 bool negative = false;
249 frames = qAbs(frames);
252 int seconds = (int)(frames / fps);
253 frames = frames - ((int)(fps * seconds));
255 int minutes = seconds / 60;
256 seconds = seconds % 60;
257 int hours = minutes / 60;
258 minutes = minutes % 60;
266 text.append(QString::number(hours).rightJustified(2, '0', false));
267 text.append(' ' + i18n("hour") + ' ');
270 if (minutes != 0 || trim) {
272 text.append(QString::number(minutes));
274 text.append(QString::number(minutes).rightJustified(2, '0', false));
275 text.append(' ' + i18n("min.") + ' ');
278 if (seconds != 0 || trim) {
280 text.append(QString::number(seconds));
282 text.append(QString::number(seconds).rightJustified(2, '0', false));
283 text.append(' ' + i18n("sec."));
287 text.append(QString::number(frames));
288 text.append(' ' + i18n("frames"));
295 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
297 if (m_dropFrameTimecode) {
298 return getTimecodeDropFrame(time);
300 return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
303 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
305 if (m_dropFrameTimecode) {
306 return getTimecodeDropFrame(frames);
309 bool negative = false;
312 frames = qAbs(frames);
315 int seconds = frames / m_displayedFramesPerSecond;
316 frames = frames % m_displayedFramesPerSecond;
318 int minutes = seconds / 60;
319 seconds = seconds % 60;
320 int hours = minutes / 60;
321 minutes = minutes % 60;
326 text.append(QString::number(hours).rightJustified(2, '0', false));
328 text.append(QString::number(minutes).rightJustified(2, '0', false));
330 text.append(QString::number(seconds).rightJustified(2, '0', false));
332 text.append(QString::number(frames).rightJustified(2, '0', false));
337 const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
339 int hundredths = (int)(time.seconds() * 100);
341 bool negative = false;
342 if (hundredths < 0) {
344 hundredths = qAbs(hundredths);
347 int seconds = hundredths / 100;
348 hundredths = hundredths % 100;
349 int minutes = seconds / 60;
350 seconds = seconds % 60;
351 int hours = minutes / 60;
352 minutes = minutes % 60;
357 text.append(QString::number(hours).rightJustified(2, '0', false));
359 text.append(QString::number(minutes).rightJustified(2, '0', false));
361 text.append(QString::number(seconds).rightJustified(2, '0', false));
362 if (m_dropFrameTimecode)
366 text.append(QString::number(hundredths).rightJustified(2, '0', false));
371 const QString Timecode::getTimecodeFrames(const GenTime & time) const
373 return QString::number((int) time.frames(m_realFps));
376 const QString Timecode::getTimecodeSeconds(const GenTime & time) const
379 return locale.toString(time.seconds());
382 const QString Timecode::getTimecodeDropFrame(const GenTime & time) const
384 return getTimecodeDropFrame((int)time.frames(m_realFps));
387 const QString Timecode::getTimecodeDropFrame(int framenumber) const
389 //CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE
390 //Based on code by David Heidelberger, adapted from Andrew Duncan
391 //Given an int called framenumber and a double called framerate
392 //Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
394 bool negative = false;
395 if (framenumber < 0) {
397 framenumber = qAbs(framenumber);
400 int d = floor(framenumber / m_framesPer10Minutes);
401 int m = framenumber % m_framesPer10Minutes;
403 if (m > m_dropFrames) {
404 framenumber += (m_dropFrames * 9 * d) + m_dropFrames * (floor((m - m_dropFrames) / (round(m_realFps * 60) - m_dropFrames)));
406 framenumber += m_dropFrames * 9 * d;
409 int frames = framenumber % m_displayedFramesPerSecond;
410 int seconds = (int) floor(framenumber / m_displayedFramesPerSecond) % 60;
411 int minutes = (int) floor(floor(framenumber / m_displayedFramesPerSecond) / 60) % 60;
412 int hours = floor(floor(floor(framenumber / m_displayedFramesPerSecond) / 60) / 60);
417 text.append(QString::number(hours).rightJustified(2, '0', false));
419 text.append(QString::number(minutes).rightJustified(2, '0', false));
421 text.append(QString::number(seconds).rightJustified(2, '0', false));
423 text.append(QString::number(frames).rightJustified(2, '0', false));