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