2006-02-23 02:44:48 +01:00
|
|
|
// Copyright (c) 2004-2006, Rodrigo Braz Monteiro, Mike Matsnev
|
|
|
|
// 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/
|
|
|
|
|
|
|
|
/// @file mkv_wrap.cpp
|
|
|
|
/// @brief High-level interface for obtaining various data from Matroska files
|
|
|
|
/// @ingroup video_input
|
|
|
|
///
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
#include "mkv_wrap.h"
|
|
|
|
|
2006-12-17 06:32:18 +01:00
|
|
|
#include "ass_file.h"
|
2012-10-12 04:12:42 +02:00
|
|
|
#include "ass_parser.h"
|
2011-09-28 21:47:21 +02:00
|
|
|
#include "compat.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "dialog_progress.h"
|
2011-09-28 21:47:21 +02:00
|
|
|
#include "MatroskaParser.h"
|
2014-06-02 22:01:46 +02:00
|
|
|
#include "options.h"
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2014-07-06 16:28:58 +02:00
|
|
|
#include <libaegisub/ass/time.h>
|
2014-03-21 03:40:05 +01:00
|
|
|
#include <libaegisub/file_mapping.h>
|
2014-05-29 00:19:05 +02:00
|
|
|
#include <libaegisub/format.h>
|
2013-09-29 17:00:36 +02:00
|
|
|
#include <libaegisub/scoped_ptr.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/range/irange.hpp>
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
#include <wx/choicdlg.h> // Keep this last so wxUSE_CHOICEDLG is set.
|
|
|
|
|
2014-03-21 03:40:05 +01:00
|
|
|
struct MkvStdIO final : InputStream {
|
|
|
|
agi::read_file_mapping file;
|
|
|
|
std::string error;
|
2006-02-25 21:48:32 +01:00
|
|
|
|
2014-03-23 17:01:25 +01:00
|
|
|
static int Read(InputStream *st, uint64_t pos, void *buffer, int count) {
|
2014-03-21 03:40:05 +01:00
|
|
|
auto *self = static_cast<MkvStdIO*>(st);
|
2014-05-20 17:41:52 +02:00
|
|
|
if (pos >= self->file.size())
|
2014-03-21 03:40:05 +01:00
|
|
|
return 0;
|
|
|
|
|
2014-03-28 17:15:09 +01:00
|
|
|
auto remaining = self->file.size() - pos;
|
|
|
|
if (remaining < INT_MAX)
|
|
|
|
count = std::min(static_cast<int>(remaining), count);
|
|
|
|
|
2014-03-21 03:40:05 +01:00
|
|
|
try {
|
|
|
|
memcpy(buffer, self->file.read(pos, count), count);
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
2014-05-29 14:57:27 +02:00
|
|
|
self->error = e.GetMessage();
|
2014-03-21 03:40:05 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2014-03-21 03:40:05 +01:00
|
|
|
return count;
|
|
|
|
}
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2014-03-23 17:01:25 +01:00
|
|
|
static int64_t Scan(InputStream *st, uint64_t start, unsigned signature) {
|
2014-03-21 03:40:05 +01:00
|
|
|
auto *self = static_cast<MkvStdIO*>(st);
|
|
|
|
try {
|
|
|
|
unsigned cmp = 0;
|
|
|
|
for (auto i : boost::irange(start, self->file.size())) {
|
|
|
|
int c = *self->file.read(i, 1);
|
|
|
|
cmp = ((cmp << 8) | c) & 0xffffffff;
|
|
|
|
if (cmp == signature)
|
|
|
|
return i - 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
2014-05-29 14:57:27 +02:00
|
|
|
self->error = e.GetMessage();
|
2014-03-21 03:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-23 17:01:25 +01:00
|
|
|
static int64_t Size(InputStream *st) {
|
2014-03-21 03:40:05 +01:00
|
|
|
return static_cast<MkvStdIO*>(st)->file.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
MkvStdIO(agi::fs::path const& filename) : file(filename) {
|
|
|
|
read = &MkvStdIO::Read;
|
|
|
|
scan = &MkvStdIO::Scan;
|
|
|
|
getcachesize = [](InputStream *) -> unsigned int { return 16 * 1024 * 1024; };
|
|
|
|
geterror = [](InputStream *st) -> const char * { return ((MkvStdIO *)st)->error.c_str(); };
|
|
|
|
memalloc = [](InputStream *, size_t size) { return malloc(size); };
|
|
|
|
memrealloc = [](InputStream *, void *mem, size_t size) { return realloc(mem, size); };
|
|
|
|
memfree = [](InputStream *, void *mem) { free(mem); };
|
2014-03-23 17:01:25 +01:00
|
|
|
progress = [](InputStream *, uint64_t, uint64_t) { return 1; };
|
2014-03-21 03:40:05 +01:00
|
|
|
getfilesize = &MkvStdIO::Size;
|
|
|
|
}
|
|
|
|
};
|
2012-04-22 17:31:11 +02:00
|
|
|
|
2012-10-12 04:57:53 +02:00
|
|
|
static void read_subtitles(agi::ProgressSink *ps, MatroskaFile *file, MkvStdIO *input, bool srt, double totalTime, AssParser *parser) {
|
2013-08-27 02:13:05 +02:00
|
|
|
std::vector<std::pair<int, std::string>> subList;
|
2011-09-28 21:47:40 +02:00
|
|
|
|
|
|
|
// Load blocks
|
2014-03-23 17:01:25 +01:00
|
|
|
uint64_t startTime, endTime, filePos;
|
2011-09-28 21:47:40 +02:00
|
|
|
unsigned int rt, frameSize, frameFlags;
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
while (mkv_ReadFrame(file, 0, &rt, &startTime, &endTime, &filePos, &frameSize, &frameFlags) == 0) {
|
|
|
|
if (ps->IsCancelled()) return;
|
|
|
|
if (frameSize == 0) continue;
|
2011-09-28 21:47:40 +02:00
|
|
|
|
2014-03-21 03:40:05 +01:00
|
|
|
const auto readBuf = input->file.read(filePos, frameSize);
|
|
|
|
const auto readBufEnd = readBuf + frameSize;
|
2011-09-28 21:47:40 +02:00
|
|
|
|
|
|
|
// Get start and end times
|
2014-03-23 17:01:25 +01:00
|
|
|
int64_t timecodeScaleLow = 1000000;
|
2014-07-06 16:28:58 +02:00
|
|
|
agi::Time subStart = startTime / timecodeScaleLow;
|
|
|
|
agi::Time subEnd = endTime / timecodeScaleLow;
|
2011-09-28 21:47:40 +02:00
|
|
|
|
2014-03-21 03:40:05 +01:00
|
|
|
using str_range = boost::iterator_range<const char *>;
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
// Process SSA/ASS
|
|
|
|
if (!srt) {
|
2014-03-21 03:40:05 +01:00
|
|
|
auto first = std::find(readBuf, readBufEnd, ',');
|
|
|
|
if (first == readBufEnd) continue;
|
|
|
|
auto second = std::find(first + 1, readBufEnd, ',');
|
|
|
|
if (second == readBufEnd) continue;
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2013-08-27 02:13:05 +02:00
|
|
|
subList.emplace_back(
|
2014-03-21 03:40:05 +01:00
|
|
|
boost::lexical_cast<int>(str_range(readBuf, first)),
|
2014-05-29 00:19:05 +02:00
|
|
|
agi::format("Dialogue: %d,%s,%s,%s"
|
|
|
|
, boost::lexical_cast<int>(str_range(first + 1, second))
|
2014-07-06 16:28:58 +02:00
|
|
|
, subStart.GetAssFormatted()
|
|
|
|
, subEnd.GetAssFormatted()
|
2014-05-29 00:19:05 +02:00
|
|
|
, str_range(second + 1, readBufEnd)));
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
|
|
|
// Process SRT
|
|
|
|
else {
|
2014-05-29 00:19:05 +02:00
|
|
|
auto line = agi::format("Dialogue: 0,%s,%s,Default,,0,0,0,,%s"
|
2014-07-06 16:28:58 +02:00
|
|
|
, subStart.GetAssFormatted()
|
|
|
|
, subEnd.GetAssFormatted()
|
2014-05-29 00:19:05 +02:00
|
|
|
, str_range(readBuf, readBufEnd));
|
2014-03-21 03:40:05 +01:00
|
|
|
boost::replace_all(line, "\r\n", "\\N");
|
|
|
|
boost::replace_all(line, "\r", "\\N");
|
|
|
|
boost::replace_all(line, "\n", "\\N");
|
|
|
|
|
|
|
|
subList.emplace_back(subList.size(), std::move(line));
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2011-11-28 23:16:58 +01:00
|
|
|
ps->SetProgress(startTime / timecodeScaleLow, totalTime);
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert into file
|
2013-08-27 02:13:05 +02:00
|
|
|
sort(begin(subList), end(subList));
|
2013-01-04 16:01:50 +01:00
|
|
|
for (auto order_value_pair : subList)
|
2012-11-04 04:53:03 +01:00
|
|
|
parser->AddLine(order_value_pair.second);
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *target) {
|
2011-09-28 21:47:21 +02:00
|
|
|
MkvStdIO input(filename);
|
2006-02-23 02:44:48 +01:00
|
|
|
char err[2048];
|
2013-09-29 17:00:36 +02:00
|
|
|
agi::scoped_holder<MatroskaFile*, decltype(&mkv_Close)> file(mkv_Open(&input, err, sizeof(err)), mkv_Close);
|
2011-09-28 21:47:21 +02:00
|
|
|
if (!file) throw MatroskaException(err);
|
|
|
|
|
2013-09-29 17:00:36 +02:00
|
|
|
// Get info
|
|
|
|
unsigned tracks = mkv_GetNumTracks(file);
|
|
|
|
std::vector<unsigned> tracksFound;
|
|
|
|
std::vector<std::string> tracksNames;
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2013-09-29 17:00:36 +02:00
|
|
|
// Find tracks
|
|
|
|
for (auto track : boost::irange(0u, tracks)) {
|
|
|
|
auto trackInfo = mkv_GetTrackInfo(file, track);
|
2014-04-28 17:43:25 +02:00
|
|
|
if (trackInfo->Type != 0x11 || trackInfo->CompEnabled) continue;
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2013-09-29 17:00:36 +02:00
|
|
|
// Known subtitle format
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string CodecID(trackInfo->CodecID);
|
2013-09-29 17:00:36 +02:00
|
|
|
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8") {
|
|
|
|
tracksFound.push_back(track);
|
2014-05-29 00:19:05 +02:00
|
|
|
tracksNames.emplace_back(agi::format("%d (%s %s)", track, CodecID, trackInfo->Language));
|
2013-09-29 17:00:36 +02:00
|
|
|
if (trackInfo->Name) {
|
|
|
|
tracksNames.back() += ": ";
|
|
|
|
tracksNames.back() += trackInfo->Name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-12-17 06:32:18 +01:00
|
|
|
|
2013-09-29 17:00:36 +02:00
|
|
|
// No tracks found
|
|
|
|
if (tracksFound.empty())
|
|
|
|
throw MatroskaException("File has no recognised subtitle tracks.");
|
|
|
|
|
|
|
|
unsigned trackToRead;
|
|
|
|
// Only one track found
|
|
|
|
if (tracksFound.size() == 1)
|
|
|
|
trackToRead = tracksFound[0];
|
|
|
|
// Pick a track
|
|
|
|
else {
|
|
|
|
int choice = wxGetSingleChoiceIndex(_("Choose which track to read:"), _("Multiple subtitle tracks found"), to_wx(tracksNames));
|
|
|
|
if (choice == -1)
|
|
|
|
throw agi::UserCancelException("canceled");
|
|
|
|
|
|
|
|
trackToRead = tracksFound[choice];
|
|
|
|
}
|
2012-10-12 04:57:53 +02:00
|
|
|
|
2013-09-29 17:00:36 +02:00
|
|
|
// Picked track
|
|
|
|
mkv_SetTrackMask(file, ~(1 << trackToRead));
|
|
|
|
auto trackInfo = mkv_GetTrackInfo(file, trackToRead);
|
|
|
|
std::string CodecID(trackInfo->CodecID);
|
|
|
|
bool srt = CodecID == "S_TEXT/UTF8";
|
|
|
|
bool ssa = CodecID == "S_TEXT/SSA";
|
2006-12-17 06:32:18 +01:00
|
|
|
|
2013-09-29 17:00:36 +02:00
|
|
|
AssParser parser(target, !ssa);
|
2006-12-17 06:32:18 +01:00
|
|
|
|
2013-09-29 17:00:36 +02:00
|
|
|
// Read private data if it's ASS/SSA
|
|
|
|
if (!srt) {
|
|
|
|
// Read raw data
|
|
|
|
std::string priv((const char *)trackInfo->CodecPrivate, trackInfo->CodecPrivateSize);
|
2006-12-17 06:32:18 +01:00
|
|
|
|
2013-09-29 17:00:36 +02:00
|
|
|
// Load into file
|
|
|
|
boost::char_separator<char> sep("\r\n");
|
|
|
|
for (auto const& cur : boost::tokenizer<boost::char_separator<char>>(priv, sep))
|
|
|
|
parser.AddLine(cur);
|
2006-12-17 06:32:18 +01:00
|
|
|
}
|
2013-09-29 17:00:36 +02:00
|
|
|
// Load default if it's SRT
|
2014-08-30 17:10:53 +02:00
|
|
|
else
|
2014-06-02 22:01:46 +02:00
|
|
|
target->LoadDefault(false, OPT_GET("Subtitle Format/SRT/Default Style Catalog")->GetString());
|
2014-08-30 17:10:53 +02:00
|
|
|
|
|
|
|
parser.AddLine("[Events]");
|
2013-09-29 17:00:36 +02:00
|
|
|
|
|
|
|
// Read timecode scale
|
|
|
|
auto segInfo = mkv_GetFileInfo(file);
|
2014-03-23 17:01:25 +01:00
|
|
|
int64_t timecodeScale = mkv_TruncFloat(trackInfo->TimecodeScale) * segInfo->TimecodeScale;
|
2013-09-29 17:00:36 +02:00
|
|
|
|
|
|
|
// Progress bar
|
|
|
|
auto totalTime = double(segInfo->Duration) / timecodeScale;
|
|
|
|
DialogProgress progress(nullptr, _("Parsing Matroska"), _("Reading subtitles from Matroska file."));
|
|
|
|
progress.Run([&](agi::ProgressSink *ps) { read_subtitles(ps, file, &input, srt, totalTime, &parser); });
|
2006-12-17 06:32:18 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
bool MatroskaWrapper::HasSubtitles(agi::fs::path const& filename) {
|
2010-05-19 02:44:37 +02:00
|
|
|
char err[2048];
|
2011-09-28 21:47:21 +02:00
|
|
|
try {
|
|
|
|
MkvStdIO input(filename);
|
2013-09-29 17:00:36 +02:00
|
|
|
agi::scoped_holder<MatroskaFile*, decltype(&mkv_Close)> file(mkv_Open(&input, err, sizeof(err)), mkv_Close);
|
2011-09-28 21:47:21 +02:00
|
|
|
if (!file) return false;
|
|
|
|
|
|
|
|
// Find tracks
|
2013-01-04 16:01:50 +01:00
|
|
|
auto tracks = mkv_GetNumTracks(file);
|
|
|
|
for (auto track : boost::irange(0u, tracks)) {
|
|
|
|
auto trackInfo = mkv_GetTrackInfo(file, track);
|
2011-09-28 21:47:21 +02:00
|
|
|
|
2014-04-28 17:43:25 +02:00
|
|
|
if (trackInfo->Type == 0x11 && !trackInfo->CompEnabled) {
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string CodecID(trackInfo->CodecID);
|
2013-09-29 17:00:36 +02:00
|
|
|
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8")
|
2011-09-28 21:47:21 +02:00
|
|
|
return true;
|
2010-05-19 02:44:37 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-28 21:47:21 +02:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// We don't care about why we couldn't read subtitles here
|
|
|
|
}
|
2013-09-29 17:00:36 +02:00
|
|
|
|
|
|
|
return false;
|
2010-05-19 02:44:37 +02:00
|
|
|
}
|