2010-12-31 22:02:17 +01:00
|
|
|
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
/// @file keyframe.cpp
|
|
|
|
/// @see keyframe.h
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
///
|
|
|
|
|
|
|
|
|
2011-01-05 19:40:37 +01:00
|
|
|
#include "../config.h"
|
2010-12-31 22:02:17 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "libaegisub/io.h"
|
|
|
|
#include "libaegisub/line_iterator.h"
|
|
|
|
#include "libaegisub/keyframe.h"
|
|
|
|
#include "libaegisub/vfr.h"
|
|
|
|
|
2012-11-13 15:07:39 +01:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/range/algorithm.hpp>
|
|
|
|
|
2011-11-20 18:35:00 +01:00
|
|
|
namespace {
|
|
|
|
std::vector<int> agi_keyframes(std::istream &file) {
|
2010-12-31 22:02:17 +01:00
|
|
|
double fps;
|
|
|
|
std::string fps_str;
|
|
|
|
file >> fps_str;
|
|
|
|
file >> fps;
|
|
|
|
|
2012-11-13 15:07:39 +01:00
|
|
|
return std::vector<int>(std::istream_iterator<int>(file), std::istream_iterator<int>());
|
2010-12-31 22:02:17 +01:00
|
|
|
}
|
|
|
|
|
2011-11-20 18:35:00 +01:00
|
|
|
std::vector<int> other_keyframes(std::istream &file, char (*func)(std::string const&)) {
|
2010-12-31 22:02:17 +01:00
|
|
|
int count = 0;
|
|
|
|
std::vector<int> ret;
|
|
|
|
agi::line_iterator<std::string> end;
|
|
|
|
for (agi::line_iterator<std::string> iter(file); iter != end; ++iter) {
|
|
|
|
char c = tolower(func(*iter));
|
2012-11-13 15:07:39 +01:00
|
|
|
if (c == 'i')
|
2010-12-31 22:02:17 +01:00
|
|
|
ret.push_back(count++);
|
2012-11-13 15:07:39 +01:00
|
|
|
else if (c == 'p' || c == 'b')
|
2010-12-31 22:02:17 +01:00
|
|
|
++count;
|
|
|
|
}
|
2011-11-20 18:35:00 +01:00
|
|
|
return ret;
|
2010-12-31 22:02:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char xvid(std::string const& line) {
|
|
|
|
return line.empty() ? 0 : line[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
char divx(std::string const& line) {
|
|
|
|
char chrs[] = "IPB";
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
std::string::size_type pos = line.find(chrs[i]);
|
|
|
|
if (pos != line.npos)
|
|
|
|
return line[pos];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char x264(std::string const& line) {
|
|
|
|
std::string::size_type pos = line.find("type:");
|
|
|
|
if (pos == line.npos || pos + 5 >= line.size()) return 0;
|
|
|
|
return line[pos + 5];
|
|
|
|
}
|
2011-11-20 18:35:00 +01:00
|
|
|
}
|
2010-12-31 22:02:17 +01:00
|
|
|
|
|
|
|
namespace agi { namespace keyframe {
|
2012-11-13 15:07:39 +01:00
|
|
|
void Save(std::string const& filename, std::vector<int> const& keyframes) {
|
2010-12-31 22:02:17 +01:00
|
|
|
io::Save file(filename);
|
|
|
|
std::ofstream& of = file.Get();
|
|
|
|
of << "# keyframe format v1" << std::endl;
|
2011-11-20 18:35:00 +01:00
|
|
|
of << "fps " << 0 << std::endl;
|
2012-11-13 15:07:39 +01:00
|
|
|
boost::copy(keyframes, std::ostream_iterator<int>(of, "\n"));
|
2010-12-31 22:02:17 +01:00
|
|
|
}
|
|
|
|
|
2011-11-20 18:35:00 +01:00
|
|
|
std::vector<int> Load(std::string const& filename) {
|
2012-11-13 15:07:39 +01:00
|
|
|
std::unique_ptr<std::ifstream> file(io::Open(filename));
|
|
|
|
std::istream &is(*file);
|
2010-12-31 22:02:17 +01:00
|
|
|
|
|
|
|
std::string header;
|
2012-11-13 15:07:39 +01:00
|
|
|
getline(is, header);
|
2010-12-31 22:02:17 +01:00
|
|
|
|
|
|
|
if (header == "# keyframe format v1") return agi_keyframes(is);
|
2012-11-13 15:07:39 +01:00
|
|
|
if (boost::starts_with(header, "# XviD 2pass stat file")) return other_keyframes(is, xvid);
|
|
|
|
if (boost::starts_with(header, "##map version")) return other_keyframes(is, divx);
|
|
|
|
if (boost::starts_with(header, "#options:")) return other_keyframes(is, x264);
|
2010-12-31 22:02:17 +01:00
|
|
|
|
|
|
|
throw Error("Unknown keyframe format");
|
|
|
|
}
|
|
|
|
|
|
|
|
} }
|