]> git.sesse.net Git - kdenlive/blob - src/timecode.cpp
Use KLocalizedString (for i18n only, in kf5 it will necessary => use a script for...
[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
84 #include <KDebug>
85 #include <KLocalizedString>
86
87 #include "timecode.h"
88
89 Timecode::Timecode(Formats format, double framesPerSecond)
90 {
91     setFormat(framesPerSecond, format);
92 }
93
94 Timecode::~Timecode()
95 {
96 }
97
98 void Timecode::setFormat(double framesPerSecond, Formats format)
99 {
100     m_displayedFramesPerSecond = (int)(framesPerSecond + 0.5);
101     m_dropFrameTimecode = (framesPerSecond / 1.00 != (int)framesPerSecond) ;
102     m_format = format;
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
107     }
108 }
109
110 double Timecode::fps() const
111 {
112     return m_realFps;
113 }
114
115 bool Timecode::df() const
116 {
117     return m_dropFrameTimecode;
118 }
119
120 const QString Timecode::mask(const GenTime& t) const
121 {
122     if (t < GenTime()) {
123         if (m_dropFrameTimecode) return "#99:99:99,99";
124         else return "#99:99:99:99";
125     }
126     if (m_dropFrameTimecode) return "99:99:99,99";
127     else return "99:99:99:99";
128 }
129
130 QString Timecode::reformatSeparators(QString duration) const
131 {
132     if (m_dropFrameTimecode)
133         return duration.replace(8, 1, ',');
134     return duration.replace(8, 1, ':');
135 }
136
137 int Timecode::getDisplayFrameCount(const QString &duration, bool frameDisplay) const
138 {
139     if (frameDisplay) return duration.toInt();
140     return getFrameCount(duration);
141 }
142
143 int Timecode::getFrameCount(const QString &duration) const
144 {
145     if (duration.isEmpty()) {
146         return 0;
147     }
148     int hours, minutes, seconds, frames;
149     int offset = 0;
150     if (duration.at(0) == '-') {
151         offset = 1;
152         hours = duration.mid(1, 2).toInt();
153     } else {
154         hours = duration.left(2).toInt();
155     }
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
163
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)));
166         return frameNumber;
167     }
168     return (int)((hours * 3600.0 + minutes * 60.0 + seconds) * m_realFps + frames);
169 }
170
171 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
172 {
173     if (frameDisplay) return QString::number((int) time.frames(m_realFps));
174     return getTimecode(time);
175 }
176
177 QString Timecode::getTimecode(const GenTime & time) const
178 {
179     switch (m_format) {
180     case HH_MM_SS_FF:
181         return getTimecodeHH_MM_SS_FF(time);
182         break;
183     case HH_MM_SS_HH:
184         return getTimecodeHH_MM_SS_HH(time);
185         break;
186     case Frames:
187         return getTimecodeFrames(time);
188         break;
189     case Seconds:
190         return getTimecodeSeconds(time);
191         break;
192     default:
193         kWarning() <<
194         "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
195         << endl;
196         return getTimecodeHH_MM_SS_FF(time);
197     }
198 }
199
200 const QString Timecode::getDisplayTimecodeFromFrames(int frames, bool frameDisplay) const
201 {
202     if (frameDisplay) return QString::number(frames);
203     return getTimecodeHH_MM_SS_FF(frames);
204 }
205
206 const QString Timecode::getTimecodeFromFrames(int frames) const
207 {
208     return getTimecodeHH_MM_SS_FF(frames);
209 }
210
211
212 //static
213 QString Timecode::getStringTimecode(int frames, const double &fps, bool showFrames)
214 {
215     // Returns the timecode in an hh:mm:ss format
216
217     bool negative = false;
218     if (frames < 0) {
219         negative = true;
220         frames = qAbs(frames);
221     }
222
223     int seconds = (int)(frames / fps);
224     int frms = frames % (int) (fps + 0.5);
225     int minutes = seconds / 60;
226     seconds = seconds % 60;
227     int hours = minutes / 60;
228     minutes = minutes % 60;
229     QString text;
230     if (negative)
231         text.append('-');
232     text.append(QString::number(hours).rightJustified(2, '0', false));
233     text.append(':');
234     text.append(QString::number(minutes).rightJustified(2, '0', false));
235     text.append(':');
236     text.append(QString::number(seconds).rightJustified(2, '0', false));
237     if (showFrames) {
238         text.append('.');
239         text.append(QString::number(frms).rightJustified(2, '0', false));
240     }
241     return text;
242 }
243
244
245 //static
246 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
247 {
248     // Returns the timecode in an easily read display, like 3 min. 5 sec.
249     int frames = (int) time.frames(fps);
250
251     bool negative = false;
252     if (frames < 0) {
253         negative = true;
254         frames = qAbs(frames);
255     }
256
257     int seconds = (int)(frames / fps);
258     frames = frames - ((int)(fps * seconds));
259
260     int minutes = seconds / 60;
261     seconds = seconds % 60;
262     int hours = minutes / 60;
263     minutes = minutes % 60;
264
265     QString text;
266     bool trim = false;
267
268     if (negative)
269         text.append('-');
270     if (hours != 0) {
271         text.append(QString::number(hours).rightJustified(2, '0', false));
272         text.append(' ' + i18n("hour") + ' ');
273         trim = true;
274     }
275     if (minutes != 0 || trim) {
276         if (!trim) {
277             text.append(QString::number(minutes));
278         } else
279             text.append(QString::number(minutes).rightJustified(2, '0', false));
280         text.append(' ' + i18n("min.") + ' ');
281         trim = true;
282     }
283     if (seconds != 0 || trim) {
284         if (!trim) {
285             text.append(QString::number(seconds));
286         } else
287             text.append(QString::number(seconds).rightJustified(2, '0', false));
288         text.append(' ' + i18n("sec."));
289         trim = true;
290     }
291     if (!trim) {
292         text.append(QString::number(frames));
293         text.append(' ' + i18n("frames"));
294     }
295
296     return text;
297 }
298
299
300 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
301 {
302     if (m_dropFrameTimecode) {
303         return getTimecodeDropFrame(time);
304     }
305     return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
306 }
307
308 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
309 {
310     if (m_dropFrameTimecode) {
311         return getTimecodeDropFrame(frames);
312     }
313
314     bool negative = false;
315     if (frames < 0) {
316         negative = true;
317         frames = qAbs(frames);
318     }
319
320     int seconds = frames / m_displayedFramesPerSecond;
321     frames = frames % m_displayedFramesPerSecond;
322
323     int minutes = seconds / 60;
324     seconds = seconds % 60;
325     int hours = minutes / 60;
326     minutes = minutes % 60;
327
328     QString text;
329     if (negative)
330         text.append('-');
331     text.append(QString::number(hours).rightJustified(2, '0', false));
332     text.append(':');
333     text.append(QString::number(minutes).rightJustified(2, '0', false));
334     text.append(':');
335     text.append(QString::number(seconds).rightJustified(2, '0', false));
336     text.append(':');
337     text.append(QString::number(frames).rightJustified(2, '0', false));
338
339     return text;
340 }
341
342 const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
343 {
344     int hundredths = (int)(time.seconds() * 100);
345
346     bool negative = false;
347     if (hundredths < 0) {
348         negative = true;
349         hundredths = qAbs(hundredths);
350     }
351
352     int seconds = hundredths / 100;
353     hundredths = hundredths % 100;
354     int minutes = seconds / 60;
355     seconds = seconds % 60;
356     int hours = minutes / 60;
357     minutes = minutes % 60;
358
359     QString text;
360     if (negative)
361         text.append('-');
362     text.append(QString::number(hours).rightJustified(2, '0', false));
363     text.append(':');
364     text.append(QString::number(minutes).rightJustified(2, '0', false));
365     text.append(':');
366     text.append(QString::number(seconds).rightJustified(2, '0', false));
367     if (m_dropFrameTimecode)
368         text.append(',');
369     else
370         text.append(':');
371     text.append(QString::number(hundredths).rightJustified(2, '0', false));
372
373     return text;
374 }
375
376 const QString Timecode::getTimecodeFrames(const GenTime & time) const
377 {
378     return QString::number((int) time.frames(m_realFps));
379 }
380
381 const QString Timecode::getTimecodeSeconds(const GenTime & time) const
382 {
383     QLocale locale;
384     return locale.toString(time.seconds());
385 }
386
387 const QString Timecode::getTimecodeDropFrame(const GenTime & time) const
388 {
389     return getTimecodeDropFrame((int)time.frames(m_realFps));
390 }
391
392 const QString Timecode::getTimecodeDropFrame(int framenumber) const
393 {
394     //CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE
395     //Based on code by David Heidelberger, adapted from Andrew Duncan
396     //Given an int called framenumber and a double called framerate
397     //Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
398
399     bool negative = false;
400     if (framenumber < 0) {
401         negative = true;
402         framenumber = qAbs(framenumber);
403     }
404
405     int d = floor(framenumber / m_framesPer10Minutes);
406     int m = framenumber % m_framesPer10Minutes;
407
408     if (m > m_dropFrames) {
409         framenumber += (m_dropFrames * 9 * d) + m_dropFrames * (floor((m - m_dropFrames) / (round(m_realFps * 60) - m_dropFrames)));
410     } else {
411         framenumber += m_dropFrames * 9 * d;
412     }
413
414     int frames = framenumber % m_displayedFramesPerSecond;
415     int seconds = (int) floor(framenumber / m_displayedFramesPerSecond) % 60;
416     int minutes = (int) floor(floor(framenumber / m_displayedFramesPerSecond) / 60) % 60;
417     int hours = floor(floor(floor(framenumber / m_displayedFramesPerSecond) / 60) / 60);
418
419     QString text;
420     if (negative)
421         text.append('-');
422     text.append(QString::number(hours).rightJustified(2, '0', false));
423     text.append(':');
424     text.append(QString::number(minutes).rightJustified(2, '0', false));
425     text.append(':');
426     text.append(QString::number(seconds).rightJustified(2, '0', false));
427     text.append(',');
428     text.append(QString::number(frames).rightJustified(2, '0', false));
429
430     return text;
431 }