1
// Copyright (c) 2007, Alysson Souza e Silva
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
7
// * Redistributions of source code must retain the above copyright notice,
8
// this list of conditions and the following disclaimer.
9
// * Redistributions in binary form must reproduce the above copyright notice,
10
// this list of conditions and the following disclaimer in the documentation
11
// and/or other materials provided with the distribution.
12
// * Neither the name of the Aegisub Group nor the names of its contributors
13
// may be used to endorse or promote products derived from this software
14
// without specific prior written permission.
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
// POSSIBILITY OF SUCH DAMAGE.
28
// -----------------------------------------------------------------------------
32
// Website: http://aegisub.cellosoft.com
33
// Contact: mailto:zeratul@cellosoft.com
40
#include <wx/msgdlg.h>
41
#include "video_context.h"
43
#include "text_file_reader.h"
44
#include "text_file_writer.h"
51
void KeyFrameFile::Load(wxString filename) {
57
TextFileReader file(filename,_T("ASCII"));
59
wxString cur = file.ReadLineFromFile();
60
// Detect type (Only Xvid, DivX, x264 and Aegisub's keyframe files are currently supported)
61
if (cur == _T("# keyframe format v1")) { OpenAegiKeyFrames(file, keyFrames); }
62
else if (cur.StartsWith(_T("# XviD 2pass stat file"))) { OpenXviDKeyFrames(file, keyFrames); }
63
else if (cur.StartsWith(_T("##map version"))) { OpenDivXKeyFrames(file, keyFrames); }
64
else if (cur.StartsWith(_T("#options:"))) { Openx264KeyFrames(file, keyFrames); }
65
else { throw(_T("Invalid or unsupported keyframes file.")); }
68
VideoContext::Get()->SetOverKeyFrames(keyFrames);
69
VideoContext::Get()->SetKeyFramesName(filename);
72
Options.AddToRecentList(filename,_T("Recent keyframes"));
75
catch (const wchar_t *error) {
77
wxMessageBox(err, _T("Error opening keyframes file"), wxOK | wxICON_ERROR, NULL);
80
wxMessageBox(_T("Unknown error"), _T("Error opening keyframes file"), wxOK | wxICON_ERROR, NULL);
87
void KeyFrameFile::Save(wxString filename) {
89
wxArrayInt keyFrames = VideoContext::Get()->GetKeyFrames();
92
TextFileWriter file(filename,_T("ASCII"));
93
file.WriteLineToFile(_T("# keyframe format v1"));
94
// Write the "fps" line although we won't be using it ourselves
95
file.WriteLineToFile(wxString::Format(_T("fps %f"),VideoContext::Get()->GetFPS()));
98
for (unsigned int i=0;i<keyFrames.Count();i++) {
99
file.WriteLineToFile(wxString::Format(_T("%i"),keyFrames[i]));
103
Options.AddToRecentList(filename,_T("Recent keyframes"));
107
//////////////////////////
108
// Aegisub keyframes file
109
void KeyFrameFile::OpenAegiKeyFrames(TextFileReader& file, wxArrayInt& keyFrames)
111
// This function used to look for an "fps" line first, that part was removed
112
// since keyframe files should not affect the framerate.
113
// That's what timecode files are for.
114
while (file.HasMoreLines()) {
115
wxString cur = file.ReadLineFromFile();
116
if (!cur.IsEmpty() && !cur.StartsWith(_T("#")) && cur.IsNumber()) {
127
void KeyFrameFile::OpenXviDKeyFrames(TextFileReader& file, wxArrayInt& keyFrames)
129
wxString cur = file.ReadLineFromFile();
130
unsigned int count = 0;
133
while (file.HasMoreLines()) {
134
if (cur.StartsWith(_T("i"))) {
135
keyFrames.Add(count);
138
else if (cur.StartsWith(_T("p")) || cur.StartsWith(_T("b"))) {
141
cur = file.ReadLineFromFile();
147
void KeyFrameFile::OpenDivXKeyFrames(TextFileReader& file, wxArrayInt& keyFrames)
149
wxString cur = file.ReadLineFromFile();
150
unsigned int count = 0;
153
while (file.HasMoreLines())
155
if (cur.Contains(_T("I"))) {
156
keyFrames.Add(count);
159
else if (cur.Contains(_T("P")) || cur.Contains(_T("B"))) {
162
cur = file.ReadLineFromFile();
168
void KeyFrameFile::Openx264KeyFrames(TextFileReader& file, wxArrayInt& keyFrames)
170
wxString cur = file.ReadLineFromFile();
171
unsigned int count = 0;
175
while (file.HasMoreLines())
177
pos = cur.Find(_T("type:"));
178
if (cur.Mid(pos,6).Right(1).Lower() == (_T("i"))) {
179
keyFrames.Add(count);
182
else if (cur.Mid(pos,6).Right(1).Lower() == (_T("p")) || cur.Mid(pos,6).Right(1).Lower() == (_T("b"))) {
185
cur = file.ReadLineFromFile();