~ubuntu-branches/ubuntu/trusty/aegisub/trusty

« back to all changes in this revision

Viewing changes to src/keyframe.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel
  • Date: 2012-03-16 22:58:00 UTC
  • Revision ID: package-import@ubuntu.com-20120316225800-yfb8h9e5n04rk46a
Tags: upstream-2.1.9
ImportĀ upstreamĀ versionĀ 2.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2007, Alysson Souza e Silva
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are met:
 
6
//
 
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.
 
15
//
 
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.
 
27
//
 
28
// -----------------------------------------------------------------------------
 
29
//
 
30
// AEGISUB
 
31
//
 
32
// Website: http://aegisub.cellosoft.com
 
33
// Contact: mailto:zeratul@cellosoft.com
 
34
//
 
35
 
 
36
///////////
 
37
// Headers
 
38
#include "config.h"
 
39
 
 
40
#include <wx/msgdlg.h>
 
41
#include "video_context.h"
 
42
#include "keyframe.h"
 
43
#include "text_file_reader.h"
 
44
#include "text_file_writer.h"
 
45
#include "options.h"
 
46
#include "vfr.h"
 
47
 
 
48
 
 
49
//////////////////
 
50
// Load Keyframes
 
51
void KeyFrameFile::Load(wxString filename) {
 
52
        // Load
 
53
        try {
 
54
                // Open file
 
55
                wxArrayInt keyFrames;
 
56
                keyFrames.Empty();
 
57
                TextFileReader file(filename,_T("ASCII"));
 
58
 
 
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.")); }
 
66
 
 
67
                // Set keyframes
 
68
                VideoContext::Get()->SetOverKeyFrames(keyFrames);
 
69
                VideoContext::Get()->SetKeyFramesName(filename);
 
70
 
 
71
                // Add to recent
 
72
                Options.AddToRecentList(filename,_T("Recent keyframes"));
 
73
        }
 
74
        // Fail
 
75
        catch (const wchar_t *error) {
 
76
                wxString err(error);
 
77
                wxMessageBox(err, _T("Error opening keyframes file"), wxOK | wxICON_ERROR, NULL);
 
78
        }
 
79
        catch (...) {
 
80
                wxMessageBox(_T("Unknown error"), _T("Error opening keyframes file"), wxOK | wxICON_ERROR, NULL);
 
81
        }
 
82
}
 
83
 
 
84
 
 
85
//////////////////
 
86
// Save Keyframes
 
87
void KeyFrameFile::Save(wxString filename) {
 
88
        // Get keyframes
 
89
        wxArrayInt keyFrames = VideoContext::Get()->GetKeyFrames();
 
90
 
 
91
        // Write header
 
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()));
 
96
 
 
97
        // Write keyframes
 
98
        for (unsigned int i=0;i<keyFrames.Count();i++) {
 
99
                file.WriteLineToFile(wxString::Format(_T("%i"),keyFrames[i]));
 
100
        }
 
101
 
 
102
        // Add to recent
 
103
        Options.AddToRecentList(filename,_T("Recent keyframes"));
 
104
}
 
105
 
 
106
 
 
107
//////////////////////////
 
108
// Aegisub keyframes file
 
109
void KeyFrameFile::OpenAegiKeyFrames(TextFileReader& file, wxArrayInt& keyFrames)
 
110
{
 
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()) {
 
117
                        long temp;
 
118
                        cur.ToLong(&temp);
 
119
                        keyFrames.Add(temp);
 
120
                }
 
121
        }       
 
122
}
 
123
 
 
124
 
 
125
///////////////////
 
126
// XviD stats file
 
127
void KeyFrameFile::OpenXviDKeyFrames(TextFileReader& file, wxArrayInt& keyFrames)
 
128
{
 
129
        wxString cur = file.ReadLineFromFile();
 
130
        unsigned int count = 0;
 
131
 
 
132
        // Read lines
 
133
        while (file.HasMoreLines()) {
 
134
                if (cur.StartsWith(_T("i"))) {                  
 
135
                        keyFrames.Add(count);
 
136
                        count++;
 
137
                }
 
138
                else if (cur.StartsWith(_T("p")) || cur.StartsWith(_T("b"))) {
 
139
                        count++;
 
140
                }
 
141
                cur = file.ReadLineFromFile();
 
142
        }
 
143
}
 
144
 
 
145
///////////////////
 
146
// DivX stats file
 
147
void KeyFrameFile::OpenDivXKeyFrames(TextFileReader& file, wxArrayInt& keyFrames)
 
148
{
 
149
        wxString cur = file.ReadLineFromFile();
 
150
        unsigned int count = 0;
 
151
 
 
152
        // Read lines
 
153
        while (file.HasMoreLines())
 
154
        {
 
155
                if (cur.Contains(_T("I"))) {
 
156
                        keyFrames.Add(count);
 
157
                        count++;
 
158
                }
 
159
                else if (cur.Contains(_T("P")) || cur.Contains(_T("B"))) {
 
160
                        count++;
 
161
                }
 
162
                cur = file.ReadLineFromFile();
 
163
        }
 
164
}
 
165
 
 
166
///////////////////
 
167
// x264 stats file
 
168
void KeyFrameFile::Openx264KeyFrames(TextFileReader& file, wxArrayInt& keyFrames)
 
169
{
 
170
        wxString cur = file.ReadLineFromFile();
 
171
        unsigned int count = 0;
 
172
        size_t pos;
 
173
 
 
174
        // Read lines
 
175
        while (file.HasMoreLines())
 
176
        {
 
177
                pos = cur.Find(_T("type:"));
 
178
                if (cur.Mid(pos,6).Right(1).Lower() == (_T("i"))) {
 
179
                        keyFrames.Add(count);
 
180
                        count++;
 
181
                }
 
182
                else if (cur.Mid(pos,6).Right(1).Lower() == (_T("p")) || cur.Mid(pos,6).Right(1).Lower() == (_T("b"))) {
 
183
                        count++;
 
184
                }
 
185
                cur = file.ReadLineFromFile();
 
186
        }
 
187
}