]> git.sesse.net Git - kdenlive/blob - src/timecode.cpp
cc429252ca8eace7a3c3d5237a6b0adbae2c638a
[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
100 //static
101 QString Timecode::getStringTimecode(int frames, const double &fps)
102 {
103     // Returns the timecode in an hh:mm:ss format
104     int seconds = frames / (int) floor(fps + 0.5);
105     int minutes = seconds / 60;
106     seconds = seconds % 60;
107     int hours = minutes / 60;
108     minutes = minutes % 60;
109     QString text;
110     text.append(QString::number(hours).rightJustified(2, '0', false));
111     text.append(':');
112     text.append(QString::number(minutes).rightJustified(2, '0', false));
113     text.append(':');
114     text.append(QString::number(seconds).rightJustified(2, '0', false));
115     return text;
116 }
117
118
119 //static
120 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
121 {
122     // Returns the timecode in an easily read display, like 3 min. 5 sec.
123     int frames = (int)time.frames(fps);
124     int seconds = frames / (int) floor(fps + 0.5);
125     frames = frames % ((int) fps);
126
127     int minutes = seconds / 60;
128     seconds = seconds % 60;
129     int hours = minutes / 60;
130     minutes = minutes % 60;
131
132     QString text;
133     bool trim = false;
134
135     if (hours != 0) {
136         text.append(QString::number(hours).rightJustified(2, '0', false));
137         text.append(' ' + i18n("hour") + ' ');
138         trim = true;
139     }
140     if (minutes != 0 || trim) {
141         if (!trim) {
142             text.append(QString::number(minutes));
143         } else
144             text.append(QString::number(minutes).rightJustified(2, '0', false));
145         text.append(' ' + i18n("min.") + ' ');
146         trim = true;
147     }
148     if (seconds != 0 || trim) {
149         if (!trim) {
150             text.append(QString::number(seconds));
151         } else
152             text.append(QString::number(seconds).rightJustified(2, '0', false));
153         text.append(' ' + i18n("sec."));
154         trim = true;
155     }
156     if (!trim) {
157         text.append(QString::number(frames));
158         text.append(' ' + i18n("frames"));
159     }
160
161     return text;
162 }
163
164
165 QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const
166 {
167     if (m_dropFrame)
168         return getTimecodeDropFrame(time, fps);
169
170     return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
171 }
172
173 QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
174 {
175     int seconds = frames / m_displayedFramesPerSecond;
176     frames = frames % m_displayedFramesPerSecond;
177
178     int minutes = seconds / 60;
179     seconds = seconds % 60;
180     int hours = minutes / 60;
181     minutes = minutes % 60;
182
183     QString text;
184     text.append(QString::number(hours).rightJustified(2, '0', false));
185     text.append(':');
186     text.append(QString::number(minutes).rightJustified(2, '0', false));
187     text.append(':');
188     text.append(QString::number(seconds).rightJustified(2, '0', false));
189     text.append(':');
190     text.append(QString::number(frames).rightJustified(2, '0', false));
191
192     return text;
193 }
194
195 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
196 {
197     int hundredths = (int)(time.seconds() * 100);
198     int seconds = hundredths / 100;
199     hundredths = hundredths % 100;
200     int minutes = seconds / 60;
201     seconds = seconds % 60;
202     int hours = minutes / 60;
203     minutes = minutes % 60;
204
205     QString text;
206
207     text.append(QString::number(hours).rightJustified(2, '0', false));
208     text.append(':');
209     text.append(QString::number(minutes).rightJustified(2, '0', false));
210     text.append(':');
211     text.append(QString::number(seconds).rightJustified(2, '0', false));
212     text.append(':');
213     text.append(QString::number(hundredths).rightJustified(2, '0', false));
214
215     return text;
216 }
217
218 QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const
219 {
220     return QString::number(time.frames(fps));
221 }
222
223 QString Timecode::getTimecodeSeconds(const GenTime & time) const
224 {
225     return QString::number(time.seconds());
226 }
227
228 QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const
229 {
230     // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
231     // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
232     int frames = (int)time.frames(fps);
233
234     // calculate how many frames need to be dropped every minute.
235     int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps)  + 0.5);
236
237     int perMinute = toDrop / 9;
238     int tenthMinute = toDrop % 9;
239
240     // calculate how many frames are in a normal minute, and how many are in a tenth minute.
241     int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
242     int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
243
244     // Number of actual frames in a 10 minute interval :
245     int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
246
247     int tenMinuteIntervals = frames / tenMinutes;
248     frames = frames % tenMinutes;
249
250     int hours = tenMinuteIntervals / 6;
251     tenMinuteIntervals = tenMinuteIntervals % 6;
252
253     // At the point, we have figured out HH:M?:??:??
254
255     int numMinutes;
256
257     if (frames < tenthMinuteFrames) {
258         // tenth minute logic applies.
259         numMinutes = 0;
260     } else {
261         // normal minute logic applies.
262         numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
263         frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
264         frames +=  tenthMinute + perMinute;
265     }
266     // We now have HH:MM:??:??
267
268     int seconds = frames / m_displayedFramesPerSecond;
269     frames = frames % m_displayedFramesPerSecond;
270
271     // We now have HH:MM:SS:FF
272
273     // THANK FUCK FOR THAT.
274
275     QString text;
276     text.append(QString::number(hours).rightJustified(2, '0', false));
277     text.append(':');
278     text.append(QString::number(tenMinuteIntervals));
279     text.append(QString::number(numMinutes));
280     text.append(':');
281     text.append(QString::number(seconds).rightJustified(2, '0', false));
282     text.append(':');
283     text.append(QString::number(frames).rightJustified(2, '0', false));
284
285     return text;
286 }