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