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
220 int seconds = (int)(frames / fps);
221 int minutes = seconds / 60;
222 seconds = seconds % 60;
223 int hours = minutes / 60;
224 minutes = minutes % 60;
226 text.append(QString::number(hours).rightJustified(2, '0', false));
228 text.append(QString::number(minutes).rightJustified(2, '0', false));
230 text.append(QString::number(seconds).rightJustified(2, '0', false));
236 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
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));
243 int minutes = seconds / 60;
244 seconds = seconds % 60;
245 int hours = minutes / 60;
246 minutes = minutes % 60;
252 text.append(QString::number(hours).rightJustified(2, '0', false));
253 text.append(' ' + i18n("hour") + ' ');
256 if (minutes != 0 || trim) {
258 text.append(QString::number(minutes));
260 text.append(QString::number(minutes).rightJustified(2, '0', false));
261 text.append(' ' + i18n("min.") + ' ');
264 if (seconds != 0 || trim) {
266 text.append(QString::number(seconds));
268 text.append(QString::number(seconds).rightJustified(2, '0', false));
269 text.append(' ' + i18n("sec."));
273 text.append(QString::number(frames));
274 text.append(' ' + i18n("frames"));
281 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
283 if (m_dropFrameTimecode) {
284 return getTimecodeDropFrame(time);
286 return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
289 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
291 if (m_dropFrameTimecode) {
292 return getTimecodeDropFrame(frames);
294 int seconds = frames / m_displayedFramesPerSecond;
295 frames = frames % m_displayedFramesPerSecond;
297 int minutes = seconds / 60;
298 seconds = seconds % 60;
299 int hours = minutes / 60;
300 minutes = minutes % 60;
303 text.append(QString::number(hours).rightJustified(2, '0', false));
305 text.append(QString::number(minutes).rightJustified(2, '0', false));
307 text.append(QString::number(seconds).rightJustified(2, '0', false));
309 text.append(QString::number(frames).rightJustified(2, '0', false));
314 const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
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;
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));
331 if (m_dropFrameTimecode)
335 text.append(QString::number(hundredths).rightJustified(2, '0', false));
340 const QString Timecode::getTimecodeFrames(const GenTime & time) const
342 return QString::number(time.frames(m_realFps));
345 const QString Timecode::getTimecodeSeconds(const GenTime & time) const
347 return QString::number(time.seconds());
350 const QString Timecode::getTimecodeDropFrame(const GenTime & time) const
352 return getTimecodeDropFrame((int)time.frames(m_realFps));
355 const QString Timecode::getTimecodeDropFrame(int framenumber) const
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.
362 int d = floor(framenumber / m_framesPer10Minutes);
363 int m = framenumber % m_framesPer10Minutes;
365 if (m > m_dropFrames) {
366 framenumber += (m_dropFrames * 9 * d) + m_dropFrames * (floor((m - m_dropFrames) / (round(m_realFps * 60) - m_dropFrames)));
368 framenumber += m_dropFrames * 9 * d;
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);
377 text.append(QString::number(hours).rightJustified(2, '0', false));
379 text.append(QString::number(minutes).rightJustified(2, '0', false));
381 text.append(QString::number(seconds).rightJustified(2, '0', false));
383 text.append(QString::number(frames).rightJustified(2, '0', false));