1 /***************************************************************************
4 begin : Wed Dec 17 2003
5 copyright : (C) 2003 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
22 Timecode::Timecode(Formats format, double framesPerSecond, bool dropFrame) :
24 m_dropFrame(dropFrame),
25 m_displayedFramesPerSecond(framesPerSecond + 0.5),
26 m_realFps(framesPerSecond)
34 double Timecode::fps() const
36 return m_realFps; //m_displayedFramesPerSecond;
40 int Timecode::getDisplayFrameCount(const QString duration, bool frameDisplay) const
42 if (frameDisplay) return duration.toInt();
43 return getFrameCount(duration);
46 int Timecode::getFrameCount(const QString duration) const
49 // calculate how many frames need to be dropped every minute.
51 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - m_realFps) + 0.5);
53 int perMinute = toDrop / 9;
54 int tenthMinute = toDrop % 9;
56 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
57 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
58 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
60 // Number of actual frames in a 10 minute interval :
61 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
62 frames = 6 * duration.section(':', 0, 0).toInt() * tenMinutes;
63 int minutes = duration.section(':', 1, 1).toInt();
64 frames += ((int) minutes / 10) * tenMinutes;
65 int mins = minutes % 10;
67 frames += tenthMinuteFrames;
69 if (mins > 0) frames += mins * normalMinuteFrames;
71 if (minutes % 10 > 0) frames -= perMinute;
72 frames += duration.section(':', 2, 2).toInt() * m_displayedFramesPerSecond + duration.section(':', 3, 3).toInt();
75 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());
78 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
80 if (frameDisplay) return QString::number((int) time.frames(m_realFps));
81 return getTimecode(time);
84 QString Timecode::getTimecode(const GenTime & time) const
88 return getTimecodeHH_MM_SS_FF(time);
91 return getTimecodeHH_MM_SS_HH(time);
94 return getTimecodeFrames(time);
97 return getTimecodeSeconds(time);
101 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
103 return getTimecodeHH_MM_SS_FF(time);
107 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
109 if (frameDisplay) return QString::number(frames);
110 return getTimecodeHH_MM_SS_FF(frames);
113 const QString Timecode::getTimecodeFromFrames(int frames) const
115 return getTimecodeHH_MM_SS_FF(frames);
120 QString Timecode::getStringTimecode(int frames, const double &fps)
122 // Returns the timecode in an hh:mm:ss format
123 int seconds = (int)(frames / fps);
124 int minutes = seconds / 60;
125 seconds = seconds % 60;
126 int hours = minutes / 60;
127 minutes = minutes % 60;
129 text.append(QString::number(hours).rightJustified(2, '0', false));
131 text.append(QString::number(minutes).rightJustified(2, '0', false));
133 text.append(QString::number(seconds).rightJustified(2, '0', false));
139 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
141 // Returns the timecode in an easily read display, like 3 min. 5 sec.
142 int frames = (int) time.frames(fps);
143 int seconds = (int)(frames / fps);
144 frames = frames - ((int)(fps * seconds));
146 int minutes = seconds / 60;
147 seconds = seconds % 60;
148 int hours = minutes / 60;
149 minutes = minutes % 60;
155 text.append(QString::number(hours).rightJustified(2, '0', false));
156 text.append(' ' + i18n("hour") + ' ');
159 if (minutes != 0 || trim) {
161 text.append(QString::number(minutes));
163 text.append(QString::number(minutes).rightJustified(2, '0', false));
164 text.append(' ' + i18n("min.") + ' ');
167 if (seconds != 0 || trim) {
169 text.append(QString::number(seconds));
171 text.append(QString::number(seconds).rightJustified(2, '0', false));
172 text.append(' ' + i18n("sec."));
176 text.append(QString::number(frames));
177 text.append(' ' + i18n("frames"));
184 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
187 return getTimecodeDropFrame(time);
189 return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
192 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
195 return getTimecodeDropFrame(frames);
197 int seconds = frames / m_displayedFramesPerSecond;
198 frames = frames % m_displayedFramesPerSecond;
200 int minutes = seconds / 60;
201 seconds = seconds % 60;
202 int hours = minutes / 60;
203 minutes = minutes % 60;
206 text.append(QString::number(hours).rightJustified(2, '0', false));
208 text.append(QString::number(minutes).rightJustified(2, '0', false));
210 text.append(QString::number(seconds).rightJustified(2, '0', false));
212 text.append(QString::number(frames).rightJustified(2, '0', false));
217 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
219 int hundredths = (int)(time.seconds() * 100);
220 int seconds = hundredths / 100;
221 hundredths = hundredths % 100;
222 int minutes = seconds / 60;
223 seconds = seconds % 60;
224 int hours = minutes / 60;
225 minutes = minutes % 60;
229 text.append(QString::number(hours).rightJustified(2, '0', false));
231 text.append(QString::number(minutes).rightJustified(2, '0', false));
233 text.append(QString::number(seconds).rightJustified(2, '0', false));
235 text.append(QString::number(hundredths).rightJustified(2, '0', false));
240 QString Timecode::getTimecodeFrames(const GenTime & time) const
242 return QString::number(time.frames(m_realFps));
245 QString Timecode::getTimecodeSeconds(const GenTime & time) const
247 return QString::number(time.seconds());
250 QString Timecode::getTimecodeDropFrame(const GenTime & time) const
252 return getTimecodeDropFrame((int)time.frames(m_realFps));
255 QString Timecode::getTimecodeDropFrame(int frames) const
257 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
258 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
260 // calculate how many frames need to be dropped every minute.
261 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - m_realFps) + 0.5);
263 int perMinute = toDrop / 9;
264 int tenthMinute = toDrop % 9;
266 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
267 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
268 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
270 // Number of actual frames in a 10 minute interval :
271 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
273 int tenMinuteIntervals = frames / tenMinutes;
274 frames = frames % tenMinutes;
276 int hours = tenMinuteIntervals / 6;
277 tenMinuteIntervals = tenMinuteIntervals % 6;
279 // At the point, we have figured out HH:M?:??:??
283 if (frames < tenthMinuteFrames) {
284 // tenth minute logic applies.
287 // normal minute logic applies.
288 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
289 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
290 frames += tenthMinute + perMinute;
292 // We now have HH:MM:??:??
294 int seconds = frames / m_displayedFramesPerSecond;
295 frames = frames % m_displayedFramesPerSecond;
297 // We now have HH:MM:SS:FF
299 // THANK FUCK FOR THAT.
302 text.append(QString::number(hours).rightJustified(2, '0', false));
304 text.append(QString::number(tenMinuteIntervals));
305 text.append(QString::number(numMinutes));
307 text.append(QString::number(seconds).rightJustified(2, '0', false));
309 text.append(QString::number(frames).rightJustified(2, '0', false));