]> git.sesse.net Git - kdenlive/blob - src/timecode.cpp
Reformat initializer lists in all constructors.
[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  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
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.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 #include "timecode.h"
18
19 #include <kdebug.h>
20 #include <klocale.h>
21
22 Timecode::Timecode(Formats format, int framesPerSecond, bool dropFrame) :
23         m_format(format),
24         m_dropFrame(dropFrame),
25         m_displayedFramesPerSecond(framesPerSecond)
26 {
27 }
28
29 Timecode::~Timecode()
30 {
31 }
32
33 int Timecode::fps()
34 {
35     return m_displayedFramesPerSecond;
36 }
37
38
39 int Timecode::getFrameCount(const QString duration, double fps) const
40 {
41     if (m_dropFrame) {
42         // calculate how many frames need to be dropped every minute.
43         int frames;
44         int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps)  + 0.5);
45
46         int perMinute = toDrop / 9;
47         int tenthMinute = toDrop % 9;
48
49         // calculate how many frames are in a normal minute, and how many are in a tenth minute.
50         int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
51         int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
52
53         // Number of actual frames in a 10 minute interval :
54         int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
55         frames = 6 * duration.section(':', 0, 0).toInt() * tenMinutes;
56         int minutes = duration.section(':', 1, 1).toInt();
57         frames += ((int) minutes / 10) * tenMinutes;
58         int mins = minutes % 10;
59         if (mins > 0) {
60             frames += tenthMinuteFrames;
61             mins--;
62             if (mins > 0) frames += mins * normalMinuteFrames;
63         }
64         if (minutes % 10 > 0) frames -= perMinute;
65         frames += duration.section(':', 2, 2).toInt() * m_displayedFramesPerSecond + duration.section(':', 3, 3).toInt();
66         return frames;
67     }
68     return (int)((duration.section(':', 0, 0).toInt()*3600.0 + duration.section(':', 1, 1).toInt()*60.0 + duration.section(':', 2, 2).toInt()) * fps + duration.section(':', 3, 3).toInt());
69 }
70
71 QString Timecode::getTimecode(const GenTime & time, double fps) const
72 {
73     switch (m_format) {
74     case HH_MM_SS_FF:
75         return getTimecodeHH_MM_SS_FF(time, fps);
76         break;
77     case HH_MM_SS_HH:
78         return getTimecodeHH_MM_SS_HH(time);
79         break;
80     case Frames:
81         return getTimecodeFrames(time, fps);
82         break;
83     case Seconds:
84         return getTimecodeSeconds(time);
85         break;
86     default:
87         kWarning() <<
88         "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
89         << endl;
90         return getTimecodeHH_MM_SS_FF(time, fps);
91     }
92 }
93
94 QString Timecode::getTimecodeFromFrames(int frames)
95 {
96     return getTimecodeHH_MM_SS_FF(frames);
97 }
98
99 //static
100 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
101 {
102     // Returns the timecode in an easily read display, like 3 min. 5 sec.
103     int frames = (int)time.frames(fps);
104     int seconds = frames / (int) floor(fps + 0.5);
105     frames = frames % ((int) fps);
106
107     int minutes = seconds / 60;
108     seconds = seconds % 60;
109     int hours = minutes / 60;
110     minutes = minutes % 60;
111
112     QString text;
113     bool trim = false;
114
115     if (hours != 0) {
116         text.append(QString::number(hours).rightJustified(2, '0', false));
117         text.append(' ' + i18n("hour") + ' ');
118         trim = true;
119     }
120     if (minutes != 0 || trim) {
121         if (!trim) {
122             text.append(QString::number(minutes));
123         } else
124             text.append(QString::number(minutes).rightJustified(2, '0', false));
125         text.append(' ' + i18n("min.") + ' ');
126         trim = true;
127     }
128     if (seconds != 0 || trim) {
129         if (!trim) {
130             text.append(QString::number(seconds));
131         } else
132             text.append(QString::number(seconds).rightJustified(2, '0', false));
133         text.append(' ' + i18n("sec."));
134         trim = true;
135     }
136     if (!trim) {
137         text.append(QString::number(frames));
138         text.append(' ' + i18n("frames"));
139     }
140
141     return text;
142 }
143
144
145 QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const
146 {
147     if (m_dropFrame)
148         return getTimecodeDropFrame(time, fps);
149
150     return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
151 }
152
153 QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
154 {
155     int seconds = frames / m_displayedFramesPerSecond;
156     frames = frames % m_displayedFramesPerSecond;
157
158     int minutes = seconds / 60;
159     seconds = seconds % 60;
160     int hours = minutes / 60;
161     minutes = minutes % 60;
162
163     QString text;
164
165     text.append(QString::number(hours).rightJustified(2, '0', false));
166     text.append(':');
167     text.append(QString::number(minutes).rightJustified(2, '0', false));
168     text.append(':');
169     text.append(QString::number(seconds).rightJustified(2, '0', false));
170     text.append(':');
171     text.append(QString::number(frames).rightJustified(2, '0', false));
172
173     return text;
174 }
175
176 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
177 {
178     int hundredths = (int)(time.seconds() * 100);
179     int seconds = hundredths / 100;
180     hundredths = hundredths % 100;
181     int minutes = seconds / 60;
182     seconds = seconds % 60;
183     int hours = minutes / 60;
184     minutes = minutes % 60;
185
186     QString text;
187
188     text.append(QString::number(hours).rightJustified(2, '0', false));
189     text.append(':');
190     text.append(QString::number(minutes).rightJustified(2, '0', false));
191     text.append(':');
192     text.append(QString::number(seconds).rightJustified(2, '0', false));
193     text.append(':');
194     text.append(QString::number(hundredths).rightJustified(2, '0', false));
195
196     return text;
197 }
198
199 QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const
200 {
201     return QString::number(time.frames(fps));
202 }
203
204 QString Timecode::getTimecodeSeconds(const GenTime & time) const
205 {
206     return QString::number(time.seconds());
207 }
208
209 QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const
210 {
211     // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
212     // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
213     int frames = (int)time.frames(fps);
214
215     // calculate how many frames need to be dropped every minute.
216     int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps)  + 0.5);
217
218     int perMinute = toDrop / 9;
219     int tenthMinute = toDrop % 9;
220
221     // calculate how many frames are in a normal minute, and how many are in a tenth minute.
222     int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
223     int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
224
225     // Number of actual frames in a 10 minute interval :
226     int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
227
228     int tenMinuteIntervals = frames / tenMinutes;
229     frames = frames % tenMinutes;
230
231     int hours = tenMinuteIntervals / 6;
232     tenMinuteIntervals = tenMinuteIntervals % 6;
233
234     // At the point, we have figured out HH:M?:??:??
235
236     int numMinutes;
237
238     if (frames < tenthMinuteFrames) {
239         // tenth minute logic applies.
240         numMinutes = 0;
241     } else {
242         // normal minute logic applies.
243         numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
244         frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
245         frames +=  tenthMinute + perMinute;
246     }
247     // We now have HH:MM:??:??
248
249     int seconds = frames / m_displayedFramesPerSecond;
250     frames = frames % m_displayedFramesPerSecond;
251
252     // We now have HH:MM:SS:FF
253
254     // THANK FUCK FOR THAT.
255
256     QString text;
257     text.append(QString::number(hours).rightJustified(2, '0', false));
258     text.append(':');
259     text.append(QString::number(tenMinuteIntervals));
260     text.append(QString::number(numMinutes));
261     text.append(':');
262     text.append(QString::number(seconds).rightJustified(2, '0', false));
263     text.append(':');
264     text.append(QString::number(frames).rightJustified(2, '0', false));
265
266     return text;
267 }