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)));
90 Timecode::Timecode(Formats format, double framesPerSecond)
92 m_validator = new QRegExpValidator(0);
93 setFormat(framesPerSecond, format);
100 void Timecode::setFormat(double framesPerSecond, Formats format)
102 m_displayedFramesPerSecond = (int)(framesPerSecond + 0.5);
103 m_dropFrameTimecode = (framesPerSecond / 1.00 != (int)framesPerSecond) ;
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
111 if (m_dropFrameTimecode)
112 regExp.setPattern("^\\d{2}:\\d{2}:\\d{2};\\d{2}$");
114 regExp.setPattern("^\\d{2}:\\d{2}:\\d{2}:\\d{2}$");
115 m_validator->setRegExp(regExp);
118 double Timecode::fps() const
123 bool Timecode::df() const
125 return m_dropFrameTimecode;
128 const QValidator *Timecode::validator() const
133 QString Timecode::reformatSeparators(QString duration) const
135 if (m_dropFrameTimecode)
136 return duration.replace(8, 1, ';');
137 return duration.replace(8, 1, ':');
140 int Timecode::getDisplayFrameCount(const QString &duration, bool frameDisplay) const
142 if (frameDisplay) return duration.toInt();
143 return getFrameCount(duration);
146 int Timecode::getFrameCount(const QString &duration) const
148 if (m_dropFrameTimecode) {
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
154 //Get Hours, Minutes, Seconds, Frames from timecode
155 int hours, minutes, seconds, frames;
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();
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();
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)));
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());
175 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
177 if (frameDisplay) return QString::number((int) time.frames(m_realFps));
178 return getTimecode(time);
181 QString Timecode::getTimecode(const GenTime & time) const
185 return getTimecodeHH_MM_SS_FF(time);
188 return getTimecodeHH_MM_SS_HH(time);
191 return getTimecodeFrames(time);
194 return getTimecodeSeconds(time);
198 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
200 return getTimecodeHH_MM_SS_FF(time);
204 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
206 if (frameDisplay) return QString::number(frames);
207 return getTimecodeHH_MM_SS_FF(frames);
210 const QString Timecode::getTimecodeFromFrames(int frames) const
212 return getTimecodeHH_MM_SS_FF(frames);
217 QString Timecode::getStringTimecode(int frames, const double &fps)
219 // Returns the timecode in an hh:mm:ss format
221 bool negative = false;
224 frames = qAbs(frames);
227 int seconds = (int)(frames / fps);
228 int minutes = seconds / 60;
229 seconds = seconds % 60;
230 int hours = minutes / 60;
231 minutes = minutes % 60;
235 text.append(QString::number(hours).rightJustified(2, '0', false));
237 text.append(QString::number(minutes).rightJustified(2, '0', false));
239 text.append(QString::number(seconds).rightJustified(2, '0', false));
245 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
247 // Returns the timecode in an easily read display, like 3 min. 5 sec.
248 int frames = (int) time.frames(fps);
250 bool negative = false;
253 frames = qAbs(frames);
256 int seconds = (int)(frames / fps);
257 frames = frames - ((int)(fps * seconds));
259 int minutes = seconds / 60;
260 seconds = seconds % 60;
261 int hours = minutes / 60;
262 minutes = minutes % 60;
270 text.append(QString::number(hours).rightJustified(2, '0', false));
271 text.append(' ' + i18n("hour") + ' ');
274 if (minutes != 0 || trim) {
276 text.append(QString::number(minutes));
278 text.append(QString::number(minutes).rightJustified(2, '0', false));
279 text.append(' ' + i18n("min.") + ' ');
282 if (seconds != 0 || trim) {
284 text.append(QString::number(seconds));
286 text.append(QString::number(seconds).rightJustified(2, '0', false));
287 text.append(' ' + i18n("sec."));
291 text.append(QString::number(frames));
292 text.append(' ' + i18n("frames"));
299 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
301 if (m_dropFrameTimecode) {
302 return getTimecodeDropFrame(time);
304 return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
307 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
309 if (m_dropFrameTimecode) {
310 return getTimecodeDropFrame(frames);
313 bool negative = false;
316 frames = qAbs(frames);
319 int seconds = frames / m_displayedFramesPerSecond;
320 frames = frames % m_displayedFramesPerSecond;
322 int minutes = seconds / 60;
323 seconds = seconds % 60;
324 int hours = minutes / 60;
325 minutes = minutes % 60;
330 text.append(QString::number(hours).rightJustified(2, '0', false));
332 text.append(QString::number(minutes).rightJustified(2, '0', false));
334 text.append(QString::number(seconds).rightJustified(2, '0', false));
336 text.append(QString::number(frames).rightJustified(2, '0', false));
341 const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
343 int hundredths = (int)(time.seconds() * 100);
345 bool negative = false;
346 if (hundredths < 0) {
348 hundredths = qAbs(hundredths);
351 int seconds = hundredths / 100;
352 hundredths = hundredths % 100;
353 int minutes = seconds / 60;
354 seconds = seconds % 60;
355 int hours = minutes / 60;
356 minutes = minutes % 60;
361 text.append(QString::number(hours).rightJustified(2, '0', false));
363 text.append(QString::number(minutes).rightJustified(2, '0', false));
365 text.append(QString::number(seconds).rightJustified(2, '0', false));
366 if (m_dropFrameTimecode)
370 text.append(QString::number(hundredths).rightJustified(2, '0', false));
375 const QString Timecode::getTimecodeFrames(const GenTime & time) const
377 return QString::number((int) time.frames(m_realFps));
380 const QString Timecode::getTimecodeSeconds(const GenTime & time) const
383 return locale.toString(time.seconds());
386 const QString Timecode::getTimecodeDropFrame(const GenTime & time) const
388 return getTimecodeDropFrame((int)time.frames(m_realFps));
391 const QString Timecode::getTimecodeDropFrame(int framenumber) const
393 //CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE
394 //Based on code by David Heidelberger, adapted from Andrew Duncan
395 //Given an int called framenumber and a double called framerate
396 //Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
398 bool negative = false;
399 if (framenumber < 0) {
401 framenumber = qAbs(framenumber);
404 int d = floor(framenumber / m_framesPer10Minutes);
405 int m = framenumber % m_framesPer10Minutes;
407 if (m > m_dropFrames) {
408 framenumber += (m_dropFrames * 9 * d) + m_dropFrames * (floor((m - m_dropFrames) / (round(m_realFps * 60) - m_dropFrames)));
410 framenumber += m_dropFrames * 9 * d;
413 int frames = framenumber % m_displayedFramesPerSecond;
414 int seconds = (int) floor(framenumber / m_displayedFramesPerSecond) % 60;
415 int minutes = (int) floor(floor(framenumber / m_displayedFramesPerSecond) / 60) % 60;
416 int hours = floor(floor(floor(framenumber / m_displayedFramesPerSecond) / 60) / 60);
421 text.append(QString::number(hours).rightJustified(2, '0', false));
423 text.append(QString::number(minutes).rightJustified(2, '0', false));
425 text.append(QString::number(seconds).rightJustified(2, '0', false));
427 text.append(QString::number(frames).rightJustified(2, '0', false));