2006-02-21 19:49:57 +01:00
|
|
|
// Copyright (c) 2005-2006, Rodrigo Braz Monteiro, Fredrik Mellbin
|
2006-01-16 22:02:54 +01:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// AEGISUB
|
|
|
|
//
|
|
|
|
// Website: http://aegisub.cellosoft.com
|
|
|
|
// Contact: mailto:zeratul@cellosoft.com
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Headers
|
|
|
|
#include "vfr.h"
|
2006-02-21 19:49:57 +01:00
|
|
|
#include "utils.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
#include <wx/filename.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
// V2 Clear function
|
2006-02-21 19:49:57 +01:00
|
|
|
void FrameRate::Clear () {
|
2006-01-16 22:02:54 +01:00
|
|
|
Frame.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// V2 Add frame
|
2006-02-21 19:49:57 +01:00
|
|
|
void FrameRate::AddFrame(int ms) {
|
|
|
|
Frame.push_back(ms);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
// V2 Get Average
|
2006-02-21 19:49:57 +01:00
|
|
|
void FrameRate::CalcAverage() {
|
|
|
|
|
2006-02-24 00:24:35 +01:00
|
|
|
if (Frame.size() <= 1)
|
2006-02-24 00:13:56 +01:00
|
|
|
throw _("No timecodes to average");
|
|
|
|
|
2006-02-24 00:24:35 +01:00
|
|
|
AverageFrameRate = double(Frame.back()) / (Frame.size()-1);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////// FrameRate //////////////////////
|
|
|
|
///////////////
|
|
|
|
// Constructor
|
|
|
|
FrameRate::FrameRate() {
|
2006-02-21 19:49:57 +01:00
|
|
|
Unload();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////
|
|
|
|
// Destructor
|
|
|
|
FrameRate::~FrameRate() {
|
2006-02-24 17:45:10 +01:00
|
|
|
Unload();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
// Loads VFR file
|
|
|
|
void FrameRate::Load(wxString filename) {
|
|
|
|
using namespace std;
|
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
Unload();
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
// Check if file exists
|
|
|
|
wxFileName filetest(filename);
|
|
|
|
if (!filetest.FileExists()) throw _T("File not found.");
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
ifstream file;
|
|
|
|
file.open(filename.mb_str(wxConvLocal));
|
|
|
|
if (!file.is_open()) throw _T("Could not open file.");
|
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
try {
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
// Read header
|
|
|
|
char buffer[65536];
|
|
|
|
file.getline(buffer,65536);
|
|
|
|
wxString header(buffer,wxConvUTF8);
|
|
|
|
|
|
|
|
// V1, code converted from avcvfr9
|
|
|
|
if (header == _T("# timecode format v1")) {
|
|
|
|
//locate the default fps line
|
|
|
|
|
|
|
|
while (!file.eof()) {
|
|
|
|
file.getline(buffer,65536);
|
|
|
|
wxString curLine(buffer,wxConvUTF8);
|
|
|
|
|
|
|
|
//skip empty lines and comments
|
|
|
|
if (curLine == _T("") || curLine.Left(1) == _T("#"))
|
|
|
|
continue;
|
|
|
|
//fix me? should be case insensitive comparison
|
|
|
|
else if (curLine.Left(7) != _T("Assume "))
|
|
|
|
throw _T("Encountered data before 'Assume <fps>' line");
|
|
|
|
else {
|
|
|
|
if (!curLine.Mid(6).ToDouble(&AverageFrameRate) || AverageFrameRate <= 0)
|
|
|
|
throw _T("Invalid 'Assume <fps>' line");
|
|
|
|
break;
|
|
|
|
}
|
2006-02-21 19:49:57 +01:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
//read and expand all timecodes to v2
|
|
|
|
wxString curline;
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
double currenttime = 0;
|
|
|
|
int lposition = -1;
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
long lstart;
|
|
|
|
long lend;
|
|
|
|
double lfps;
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
while (!file.eof()) {
|
|
|
|
file.getline(buffer,65536);
|
|
|
|
wxString curLine(buffer,wxConvUTF8);
|
|
|
|
|
|
|
|
//skip empty lines and comments
|
|
|
|
if (curLine == _T("") || curLine.Left(1) == _T("#"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
wxString tmp = curLine.AfterFirst(_T(','));
|
|
|
|
wxString temp = curLine.BeforeFirst(_T(','));
|
|
|
|
if (!temp.ToLong(&lstart) || lstart < 0)
|
|
|
|
throw _T("Timecode parsing error, invalid start format found");
|
|
|
|
temp = tmp.BeforeLast(_T(','));
|
|
|
|
if (!temp.ToLong(&lend) || lend < 0)
|
|
|
|
throw _T("Timecode parsing error, invalid end format found");
|
|
|
|
temp = tmp.AfterLast(_T(','));
|
|
|
|
if (!temp.ToDouble(&lfps) || lfps <= 0)
|
|
|
|
throw _T("Timecode parsing error, invalid fps format found");
|
2006-02-21 19:49:57 +01:00
|
|
|
|
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
for (int i = 0; i <= lstart - lposition - 2; i++)
|
|
|
|
AddFrame(floor(currenttime+(i*1000) / AverageFrameRate));
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
currenttime += ((lstart - lposition - 1)*1000) / AverageFrameRate;
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
for (int i = 0; i <= lend - lstart; i++)
|
|
|
|
AddFrame(floor(currenttime+(i*1000) / lfps));
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
currenttime += ((lend - lstart + 1)*1000) / lfps;
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
lposition = lend;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_time = currenttime;
|
|
|
|
last_frame = Frame.size();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
// V2
|
|
|
|
else if (header == _T("# timecode format v2")) {
|
|
|
|
// Assigns new VFR file
|
|
|
|
FrameRateType = VFR;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
long lftime = -1;
|
|
|
|
long cftime = 0;
|
|
|
|
last_frame = 0;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
// Reads body
|
|
|
|
while (!file.eof()) {
|
|
|
|
file.getline (buffer,65536);
|
|
|
|
wxString curLine(buffer,wxConvUTF8);
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
//skip empty lines and comments
|
|
|
|
if (curLine == _T("") || curLine.Left(1) == _T("#"))
|
|
|
|
continue;
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
wxString tmp = curLine.BeforeFirst(_T('.'));
|
|
|
|
tmp.ToLong(&cftime);
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
if (lftime >= cftime)
|
|
|
|
throw _T("Out of order/too close timecodes found");
|
2006-02-24 00:13:56 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
AddFrame(cftime);
|
|
|
|
lftime = cftime;
|
2006-02-24 00:13:56 +01:00
|
|
|
}
|
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
last_time = cftime;
|
|
|
|
last_frame = Frame.size();
|
2006-02-21 19:49:57 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
CalcAverage();
|
2006-02-24 00:13:56 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-02-24 17:45:10 +01:00
|
|
|
// Unknown
|
|
|
|
else
|
|
|
|
throw _T("Unknown file format.");
|
|
|
|
|
|
|
|
// Run test
|
|
|
|
/*bool doTest = false;
|
|
|
|
if (doTest) {
|
|
|
|
int fail = 0;
|
|
|
|
int res;
|
|
|
|
for (int i=0;i<1000;i++) {
|
|
|
|
res = GetFrameAtTime(GetTimeAtFrame(i));
|
|
|
|
if (res != i) {
|
|
|
|
wxLogMessage(wxString::Format(_T("Expected %i but got %i (%i)"),i,res,GetTimeAtFrame(i)));
|
|
|
|
fail++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fail) wxLogMessage(wxString::Format(_T("Failed %i times"),fail));
|
|
|
|
else wxLogMessage(_T("VFR passes test"));
|
|
|
|
}*/
|
|
|
|
} catch (wchar_t *) {
|
2006-01-16 22:02:54 +01:00
|
|
|
file.close();
|
2006-02-21 19:49:57 +01:00
|
|
|
Unload();
|
2006-02-24 17:45:10 +01:00
|
|
|
throw;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
file.close();
|
|
|
|
loaded = true;
|
|
|
|
vfrFile = filename;
|
2006-02-21 19:49:57 +01:00
|
|
|
FrameRateType = VFR;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////
|
|
|
|
// Unload
|
|
|
|
void FrameRate::Unload () {
|
|
|
|
FrameRateType = NONE;
|
|
|
|
AverageFrameRate = 0;
|
2006-02-21 19:49:57 +01:00
|
|
|
last_time = 0;
|
|
|
|
last_frame = 0;
|
|
|
|
Clear();
|
2006-01-16 22:02:54 +01:00
|
|
|
loaded = false;
|
|
|
|
vfrFile = _T("");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Sets to CFR
|
2006-02-24 17:45:10 +01:00
|
|
|
void FrameRate::SetCFR(double fps) {
|
2006-02-21 19:49:57 +01:00
|
|
|
Unload();
|
2006-01-16 22:02:54 +01:00
|
|
|
loaded = true;
|
|
|
|
FrameRateType = CFR;
|
|
|
|
AverageFrameRate = fps;
|
|
|
|
}
|
|
|
|
|
2006-02-23 02:44:48 +01:00
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Sets to VFR
|
|
|
|
void FrameRate::SetVFR(std::vector<int> newTimes) {
|
|
|
|
// Prepare
|
|
|
|
Unload();
|
2006-02-24 00:13:56 +01:00
|
|
|
|
2006-02-23 02:44:48 +01:00
|
|
|
loaded = true;
|
|
|
|
FrameRateType = VFR;
|
|
|
|
|
2006-02-24 00:13:56 +01:00
|
|
|
// Set new VFR;
|
2006-02-23 02:44:48 +01:00
|
|
|
Frame = newTimes;
|
2006-02-24 00:13:56 +01:00
|
|
|
CalcAverage();
|
2006-02-23 02:44:48 +01:00
|
|
|
last_time = newTimes.back();
|
2006-02-24 00:13:56 +01:00
|
|
|
last_frame = newTimes.size();
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-24 23:46:21 +01:00
|
|
|
/////////////////////////////
|
|
|
|
// Gets frame number at time
|
|
|
|
int FrameRate::PFrameAtTime(int ms,bool useceil) {
|
|
|
|
//wxASSERT(loaded);
|
|
|
|
|
|
|
|
if (!loaded) return -1;
|
|
|
|
|
|
|
|
ms = MAX(ms,0); //fix me, unsafe for GetFrame... for frame 0?
|
|
|
|
|
|
|
|
if (FrameRateType == CFR) {
|
|
|
|
if (useceil) return ceil(double(ms)/1000.0 * AverageFrameRate);
|
|
|
|
else return floor(double(ms)/1000.0 * AverageFrameRate);
|
|
|
|
}
|
|
|
|
else if (FrameRateType == VFR) {
|
|
|
|
if (ms < floor(last_time)) {
|
|
|
|
// Binary search
|
|
|
|
size_t start = 0;
|
|
|
|
size_t end = last_frame;
|
|
|
|
size_t cur;
|
|
|
|
bool largerEqual;
|
|
|
|
while (start <= end) {
|
|
|
|
cur = (start+end)>>1;
|
|
|
|
largerEqual = Frame[cur] >= ms;
|
|
|
|
|
|
|
|
// Found
|
|
|
|
if (largerEqual && (cur == 0 || Frame[cur-1] < ms))
|
|
|
|
return cur;
|
|
|
|
|
|
|
|
// Not found
|
|
|
|
if (largerEqual) end = cur-1;
|
|
|
|
else start = cur+1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return last_frame + floor((ms-last_time) * AverageFrameRate / 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
// Gets time at frame
|
|
|
|
int FrameRate::PTimeAtFrame(int frame) {
|
|
|
|
//wxASSERT(loaded);
|
|
|
|
|
|
|
|
if (!loaded) return -1;
|
|
|
|
if (frame <= 0) return 0;
|
|
|
|
|
|
|
|
if (FrameRateType == CFR) {
|
|
|
|
return floor(double(frame) / AverageFrameRate * 1000.0);
|
|
|
|
} else if (FrameRateType == VFR) {
|
|
|
|
if (frame < last_frame)
|
|
|
|
return Frame.at(frame);
|
|
|
|
else
|
|
|
|
return floor(last_time + double(frame-last_frame) / AverageFrameRate * 1000.0);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
/////////////////////////////
|
|
|
|
// Get correct frame at time
|
2006-02-21 19:49:57 +01:00
|
|
|
// returns the adjusted time for end frames when start=false
|
|
|
|
// otherwise for start frames
|
2006-02-24 17:45:10 +01:00
|
|
|
int FrameRate::GetFrameAtTime(int ms,bool start) {
|
2006-02-24 23:14:37 +01:00
|
|
|
if (start) {
|
2006-02-24 23:46:21 +01:00
|
|
|
if (FrameRateType == VFR) return PFrameAtTime(ms);
|
|
|
|
else return PFrameAtTime(ms,start);
|
2006-02-24 23:14:37 +01:00
|
|
|
}
|
|
|
|
else {
|
2006-02-24 17:45:10 +01:00
|
|
|
return PFrameAtTime(ms);
|
2006-02-24 23:14:37 +01:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////
|
|
|
|
// Get correct time at frame
|
2006-02-21 19:49:57 +01:00
|
|
|
// compensates and returns an end time when start=false
|
2006-02-24 17:45:10 +01:00
|
|
|
int FrameRate::GetTimeAtFrame(int frame,bool start) {
|
2006-02-24 23:14:37 +01:00
|
|
|
int finalTime;
|
|
|
|
if (start) {
|
|
|
|
finalTime = (PTimeAtFrame(frame-1) + PTimeAtFrame(frame))/2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (FrameRateType == VFR) finalTime = PTimeAtFrame(frame);
|
|
|
|
else finalTime = (PTimeAtFrame(frame) + PTimeAtFrame(frame+1))/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return finalTime;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Globals
|
|
|
|
FrameRate VFR_Output;
|
|
|
|
FrameRate VFR_Input;
|