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