2006-01-16 22:02:54 +01:00
|
|
|
// Copyright (c) 2005, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file ass_time.cpp
|
|
|
|
/// @brief Class for managing timestamps in subtitles
|
|
|
|
/// @ingroup subs_storage
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-10-09 18:34:38 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
#include "ass_time.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
#ifndef AGI_PRE
|
2006-01-16 22:02:54 +01:00
|
|
|
#include <algorithm>
|
2011-12-22 22:28:41 +01:00
|
|
|
#include <cmath>
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "utils.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:18:54 +01:00
|
|
|
AssTime::AssTime(int time) { SetMS(time); }
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
void AssTime::ParseASS(wxString const& text) {
|
|
|
|
int ms = 0;
|
|
|
|
size_t pos = 0, end = 0;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
int colons = text.Freq(':');
|
2007-06-23 08:07:32 +02:00
|
|
|
|
|
|
|
// Set start so that there are only two colons at most
|
2011-12-22 22:28:41 +01:00
|
|
|
for (; colons > 2; --colons) pos = text.find(':', pos) + 1;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
// Hours
|
|
|
|
if (colons == 2) {
|
|
|
|
while (text[end++] != ':') { }
|
|
|
|
ms += AegiStringToInt(text, pos, end) * 60 * 60 * 1000;
|
|
|
|
pos = end;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
// Minutes
|
|
|
|
if (colons >= 1) {
|
|
|
|
while (text[end++] != ':') { }
|
|
|
|
ms += AegiStringToInt(text, pos, end) * 60 * 1000;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
// Milliseconds (includes seconds)
|
|
|
|
ms += AegiStringToFix(text, 3, end, text.size());
|
|
|
|
|
|
|
|
SetMS(ms);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
int AssTime::GetMS() const {
|
2011-12-22 22:28:32 +01:00
|
|
|
return time / 10 * 10;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
void AssTime::SetMS(int ms) {
|
2010-12-31 22:03:03 +01:00
|
|
|
time = mid(0, ms, 10 * 60 * 60 * 1000 - 1);
|
2006-02-21 23:09:15 +01:00
|
|
|
}
|
|
|
|
|
2010-07-08 09:14:55 +02:00
|
|
|
wxString AssTime::GetASSFormated (bool msPrecision) const {
|
2011-12-22 22:28:32 +01:00
|
|
|
if (msPrecision)
|
2011-12-22 22:28:41 +01:00
|
|
|
return wxString::Format("%d:%02d:%02d.%03d", GetTimeHours(), GetTimeMinutes(), GetTimeSeconds(), GetTimeMiliseconds());
|
2011-12-22 22:28:32 +01:00
|
|
|
else
|
2011-12-22 22:28:41 +01:00
|
|
|
return wxString::Format("%d:%02d:%02d.%02d", GetTimeHours(), GetTimeMinutes(), GetTimeSeconds(), GetTimeCentiseconds());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
bool operator < (AssTime t1, AssTime t2) {
|
|
|
|
return t1.GetMS() < t2.GetMS();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
bool operator > (AssTime t1, AssTime t2) {
|
|
|
|
return t1.GetMS() > t2.GetMS();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
bool operator <= (AssTime t1, AssTime t2) {
|
|
|
|
return t1.GetMS() <= t2.GetMS();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
bool operator >= (AssTime t1, AssTime t2) {
|
|
|
|
return t1.GetMS() >= t2.GetMS();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
bool operator == (AssTime t1, AssTime t2) {
|
|
|
|
return t1.GetMS() == t2.GetMS();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
bool operator != (AssTime t1, AssTime t2) {
|
|
|
|
return t1.GetMS() != t2.GetMS();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
AssTime operator + (AssTime t1, AssTime t2) {
|
2010-07-20 05:11:11 +02:00
|
|
|
return AssTime(t1.GetMS() + t2.GetMS());
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
AssTime operator - (AssTime t1, AssTime t2) {
|
2010-07-20 05:11:11 +02:00
|
|
|
return AssTime(t1.GetMS() - t2.GetMS());
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
int AssTime::GetTimeHours() const { return time / 3600000; }
|
|
|
|
int AssTime::GetTimeMinutes() const { return (time % 3600000) / 60000; }
|
|
|
|
int AssTime::GetTimeSeconds() const { return (time % 60000) / 1000; }
|
|
|
|
int AssTime::GetTimeMiliseconds() const { return (time % 1000); }
|
|
|
|
int AssTime::GetTimeCentiseconds() const { return (time % 1000) / 10; }
|
2009-05-10 05:50:58 +02:00
|
|
|
|
2011-12-22 22:28:13 +01:00
|
|
|
FractionalTime::FractionalTime(agi::vfr::Framerate fps, bool dropframe)
|
|
|
|
: fps(fps)
|
2011-12-22 22:22:49 +01:00
|
|
|
, drop(dropframe)
|
|
|
|
{
|
2009-05-10 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:27:53 +01:00
|
|
|
wxString FractionalTime::FromAssTime(AssTime time, char sep) {
|
|
|
|
return FromMillisecs(time.GetMS(), sep);
|
2009-05-10 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:27:53 +01:00
|
|
|
wxString FractionalTime::FromMillisecs(int64_t msec, char sep) {
|
2009-05-13 22:24:21 +02:00
|
|
|
int h=0, m=0, s=0, f=0; // hours, minutes, seconds, fractions
|
2011-12-22 22:28:13 +01:00
|
|
|
int fn = fps.FrameAtTime(msec);
|
2009-05-15 14:18:33 +02:00
|
|
|
|
|
|
|
// return 00:00:00:00
|
2011-12-22 22:22:49 +01:00
|
|
|
if (msec <= 0) {
|
|
|
|
}
|
2009-05-13 22:24:21 +02:00
|
|
|
// dropframe?
|
2011-12-22 22:22:49 +01:00
|
|
|
else if (drop) {
|
2009-05-13 22:24:21 +02:00
|
|
|
fn += 2 * (fn / (30 * 60)) - 2 * (fn / (30 * 60 * 10));
|
|
|
|
h = fn / (30 * 60 * 60);
|
|
|
|
m = (fn / (30 * 60)) % 60;
|
|
|
|
s = (fn / 30) % 60;
|
|
|
|
f = fn % 30;
|
|
|
|
}
|
|
|
|
// no dropframe; h/m/s may or may not sync to wallclock time
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
This is truly the dumbest shit. What we're trying to ensure here
|
|
|
|
is that non-integer framerates are desynced from the wallclock
|
|
|
|
time by a correct amount of time. For example, in the
|
|
|
|
NTSC-without-dropframe case, 3600*num/den would be 107892
|
|
|
|
(when truncated to int), which is quite a good approximation of
|
|
|
|
how a long an hour is when counted in 30000/1001 frames per second.
|
|
|
|
Unfortunately, that's not what we want, since frame numbers will
|
|
|
|
still range from 00 to 29, meaning that we're really getting _30_
|
|
|
|
frames per second and not 29.97 and the full hour will be off by
|
|
|
|
almost 4 seconds (108000 frames versus 107892).
|
|
|
|
|
|
|
|
DEATH TO SMPTE
|
|
|
|
*/
|
2011-12-22 22:28:13 +01:00
|
|
|
int fps_approx = floor(fps.FPS() + 0.5);
|
2009-05-13 22:24:21 +02:00
|
|
|
int frames_per_h = 3600*fps_approx;
|
|
|
|
int frames_per_m = 60*fps_approx;
|
|
|
|
int frames_per_s = fps_approx;
|
2011-12-22 22:22:49 +01:00
|
|
|
|
|
|
|
h = fn / frames_per_h;
|
|
|
|
fn = fn % frames_per_h;
|
|
|
|
|
|
|
|
m = fn / frames_per_m;
|
|
|
|
fn = fn % frames_per_m;
|
|
|
|
|
|
|
|
s = fn / frames_per_s;
|
|
|
|
fn = fn % frames_per_s;
|
|
|
|
|
2009-05-13 22:24:21 +02:00
|
|
|
f = fn;
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:22:49 +01:00
|
|
|
return wxString::Format("%02i%c%02%c%02i%c%02i", h, sep, m, sep, s, sep, f);
|
2009-05-10 05:50:58 +02:00
|
|
|
}
|