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
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
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"
|
2006-12-17 06:32:18 +01:00
|
|
|
#include "ass_time.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"
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/algorithm/string/split.hpp>
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/range/irange.hpp>
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
#include <wx/choicdlg.h> // Keep this last so wxUSE_CHOICEDLG is set.
|
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
class MkvStdIO : public InputStream {
|
|
|
|
public:
|
2013-01-04 16:01:50 +01:00
|
|
|
MkvStdIO(agi::fs::path const& filename);
|
2011-09-28 21:47:21 +02:00
|
|
|
~MkvStdIO() { if (fp) fclose(fp); }
|
2006-02-25 21:48:32 +01:00
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
FILE *fp;
|
|
|
|
int error;
|
|
|
|
};
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2012-04-22 17:31:16 +02:00
|
|
|
#define CACHESIZE 1024
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2012-04-22 17:31:11 +02:00
|
|
|
#ifdef __VISUALC__
|
|
|
|
#define std_fseek _fseeki64
|
|
|
|
#define std_ftell _ftelli64
|
|
|
|
#else
|
|
|
|
#define std_fseek fseeko
|
|
|
|
#define std_ftell ftello
|
|
|
|
#endif
|
|
|
|
|
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;
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string readBuf;
|
2011-09-28 21:47:40 +02:00
|
|
|
|
|
|
|
// Load blocks
|
|
|
|
ulonglong startTime, endTime, filePos;
|
|
|
|
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
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
readBuf.resize(frameSize);
|
2012-04-22 17:31:11 +02:00
|
|
|
std_fseek(input->fp, filePos, SEEK_SET);
|
2013-01-04 16:01:50 +01:00
|
|
|
fread(&readBuf[0], 1, frameSize, input->fp);
|
2011-09-28 21:47:40 +02:00
|
|
|
|
|
|
|
// Get start and end times
|
|
|
|
longlong timecodeScaleLow = 1000000;
|
2011-12-22 22:28:51 +01:00
|
|
|
AssTime subStart = startTime / timecodeScaleLow;
|
|
|
|
AssTime subEnd = endTime / timecodeScaleLow;
|
2011-09-28 21:47:40 +02:00
|
|
|
|
|
|
|
// Process SSA/ASS
|
|
|
|
if (!srt) {
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<boost::iterator_range<std::string::iterator>> chunks;
|
|
|
|
boost::split(chunks, readBuf, boost::is_any_of(","));
|
|
|
|
|
2013-08-27 02:13:05 +02:00
|
|
|
subList.emplace_back(
|
|
|
|
boost::lexical_cast<int>(chunks[0]),
|
2013-01-04 16:01:50 +01:00
|
|
|
str(boost::format("Dialogue: %d,%s,%s,%s")
|
|
|
|
% boost::lexical_cast<int>(chunks[1])
|
|
|
|
% subStart.GetAssFormated()
|
|
|
|
% subEnd.GetAssFormated()
|
2013-08-27 02:13:05 +02:00
|
|
|
% boost::make_iterator_range(begin(chunks[2]), readBuf.end())));
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
|
|
|
// Process SRT
|
|
|
|
else {
|
2013-01-04 16:01:50 +01:00
|
|
|
readBuf = str(boost::format("Dialogue: 0,%s,%s,Default,,0,0,0,,%s") % subStart.GetAssFormated() % subEnd.GetAssFormated() % readBuf);
|
|
|
|
boost::replace_all(readBuf, "\r\n", "\\N");
|
|
|
|
boost::replace_all(readBuf, "\r", "\\N");
|
|
|
|
boost::replace_all(readBuf, "\n", "\\N");
|
2011-09-28 21:47:40 +02:00
|
|
|
|
2013-08-27 02:13:05 +02:00
|
|
|
subList.emplace_back(subList.size(), readBuf);
|
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];
|
2011-09-28 21:47:21 +02:00
|
|
|
MatroskaFile *file = mkv_Open(&input, err, sizeof(err));
|
|
|
|
if (!file) throw MatroskaException(err);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get info
|
|
|
|
unsigned tracks = mkv_GetNumTracks(file);
|
|
|
|
std::vector<unsigned> tracksFound;
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> tracksNames;
|
2011-09-28 21:47:21 +02:00
|
|
|
unsigned trackToRead;
|
|
|
|
|
|
|
|
// Find tracks
|
|
|
|
for (unsigned track = 0; track < tracks; track++) {
|
2013-01-04 16:01:50 +01:00
|
|
|
TrackInfo *trackInfo = mkv_GetTrackInfo(file, track);
|
|
|
|
if (trackInfo->Type != 0x11) continue;
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
// Known subtitle format
|
|
|
|
std::string CodecID(trackInfo->CodecID);
|
|
|
|
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8") {
|
|
|
|
tracksFound.push_back(track);
|
2013-02-14 05:22:02 +01:00
|
|
|
tracksNames.emplace_back(str(boost::format("%d (%s %s)") % track % CodecID % trackInfo->Language));
|
|
|
|
if (trackInfo->Name) {
|
|
|
|
tracksNames.back() += ": ";
|
|
|
|
tracksNames.back() += trackInfo->Name;
|
|
|
|
}
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
// No tracks found
|
|
|
|
if (tracksFound.empty())
|
|
|
|
throw MatroskaException("File has no recognised subtitle tracks.");
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
// Only one track found
|
2013-01-04 16:01:50 +01:00
|
|
|
if (tracksFound.size() == 1)
|
2011-09-28 21:47:21 +02:00
|
|
|
trackToRead = tracksFound[0];
|
|
|
|
// Pick a track
|
|
|
|
else {
|
2013-01-04 16:01:50 +01:00
|
|
|
int choice = wxGetSingleChoiceIndex(_("Choose which track to read:"), _("Multiple subtitle tracks found"), to_wx(tracksNames));
|
2011-09-28 21:47:21 +02:00
|
|
|
if (choice == -1)
|
2012-11-21 15:26:23 +01:00
|
|
|
throw agi::UserCancelException("canceled");
|
2006-12-17 06:32:18 +01:00
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
trackToRead = tracksFound[choice];
|
2006-12-17 06:32:18 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
// Picked track
|
2011-09-28 21:47:40 +02:00
|
|
|
mkv_SetTrackMask(file, ~(1 << trackToRead));
|
2013-01-04 16:01:50 +01:00
|
|
|
TrackInfo *trackInfo = mkv_GetTrackInfo(file, trackToRead);
|
|
|
|
std::string CodecID(trackInfo->CodecID);
|
2011-09-28 21:47:40 +02:00
|
|
|
bool srt = CodecID == "S_TEXT/UTF8";
|
|
|
|
bool ssa = CodecID == "S_TEXT/SSA";
|
2006-12-17 06:32:18 +01:00
|
|
|
|
2012-10-12 04:57:53 +02:00
|
|
|
AssParser parser(target, !ssa);
|
|
|
|
|
2006-12-17 06:32:18 +01:00
|
|
|
// Read private data if it's ASS/SSA
|
2011-09-28 21:47:40 +02:00
|
|
|
if (!srt) {
|
2006-12-17 06:32:18 +01:00
|
|
|
// Read raw data
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string priv((const char *)trackInfo->CodecPrivate, trackInfo->CodecPrivateSize);
|
2006-12-17 06:32:18 +01:00
|
|
|
|
|
|
|
// Load into file
|
2013-01-04 16:01:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
// Load default if it's SRT
|
|
|
|
else {
|
|
|
|
target->LoadDefault(false);
|
2012-11-11 02:43:12 +01:00
|
|
|
parser.AddLine("[Events]");
|
2006-12-17 06:32:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read timecode scale
|
|
|
|
SegmentInfo *segInfo = mkv_GetFileInfo(file);
|
2007-08-29 15:36:30 +02:00
|
|
|
longlong timecodeScale = mkv_TruncFloat(trackInfo->TimecodeScale) * segInfo->TimecodeScale;
|
2006-12-17 06:32:18 +01:00
|
|
|
|
|
|
|
// Progress bar
|
2011-11-28 23:16:58 +01:00
|
|
|
double totalTime = double(segInfo->Duration) / timecodeScale;
|
2012-11-13 17:51:01 +01:00
|
|
|
DialogProgress progress(nullptr, _("Parsing Matroska"), _("Reading subtitles from Matroska file."));
|
2012-11-01 17:07:38 +01:00
|
|
|
progress.Run([&](agi::ProgressSink *ps) { read_subtitles(ps, file, &input, srt, totalTime, &parser); });
|
2006-12-17 06:32:18 +01:00
|
|
|
}
|
2011-09-28 21:47:21 +02:00
|
|
|
catch (...) {
|
|
|
|
mkv_Close(file);
|
|
|
|
throw;
|
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-01-04 16:01:50 +01:00
|
|
|
auto file = mkv_Open(&input, err, sizeof(err));
|
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
|
|
|
|
|
|
|
if (trackInfo->Type == 0x11) {
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string CodecID(trackInfo->CodecID);
|
2011-09-28 21:47:21 +02:00
|
|
|
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8") {
|
|
|
|
mkv_Close(file);
|
|
|
|
return true;
|
|
|
|
}
|
2010-05-19 02:44:37 +02:00
|
|
|
}
|
|
|
|
}
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
mkv_Close(file);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// We don't care about why we couldn't read subtitles here
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-19 02:44:37 +02:00
|
|
|
}
|
2006-02-23 02:44:48 +01:00
|
|
|
|
|
|
|
int StdIoRead(InputStream *_st, ulonglong pos, void *buffer, int count) {
|
2013-01-04 16:01:50 +01:00
|
|
|
auto *st = static_cast<MkvStdIO*>(_st);
|
|
|
|
if (std_fseek(st->fp, pos, SEEK_SET)) {
|
|
|
|
st->error = errno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rd = fread(buffer, 1, count, st->fp);
|
|
|
|
if (rd == 0) {
|
|
|
|
if (feof(st->fp))
|
|
|
|
return 0;
|
|
|
|
st->error = errno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return rd;
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
/// @brief scan for a signature sig(big-endian) starting at file position pos
|
|
|
|
/// @return position of the first byte of signature or -1 if error/not found
|
2013-01-04 16:01:50 +01:00
|
|
|
longlong StdIoScan(InputStream *st, ulonglong start, unsigned signature) {
|
|
|
|
FILE *fp = static_cast<MkvStdIO*>(st)->fp;
|
|
|
|
|
|
|
|
if (std_fseek(fp, start, SEEK_SET))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int c;
|
|
|
|
unsigned cmp = 0;
|
|
|
|
while ((c = getc(fp)) != EOF) {
|
|
|
|
cmp = ((cmp << 8) | c) & 0xffffffff;
|
|
|
|
if (cmp == signature)
|
|
|
|
return std_ftell(fp) - 4;
|
|
|
|
}
|
2006-02-23 02:44:48 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
return -1;
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
longlong StdIoGetFileSize(InputStream *st) {
|
|
|
|
auto fp = static_cast<MkvStdIO*>(st)->fp;
|
|
|
|
auto cpos = std_ftell(fp);
|
|
|
|
std_fseek(fp, 0, SEEK_END);
|
|
|
|
auto epos = std_ftell(fp);
|
|
|
|
std_fseek(fp, cpos, SEEK_SET);
|
2009-05-02 21:40:55 +02:00
|
|
|
return epos;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
MkvStdIO::MkvStdIO(agi::fs::path const& filename)
|
2012-10-15 06:37:14 +02:00
|
|
|
: error(0)
|
|
|
|
{
|
2006-02-23 02:44:48 +01:00
|
|
|
read = StdIoRead;
|
|
|
|
scan = StdIoScan;
|
2013-01-04 16:01:50 +01:00
|
|
|
getcachesize = [](InputStream *) -> unsigned int { return CACHESIZE; };
|
|
|
|
geterror = [](InputStream *st) -> const char * { return strerror(((MkvStdIO *)st)->error); };
|
|
|
|
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); };
|
|
|
|
progress = [](InputStream *, ulonglong, ulonglong) { return 1; };
|
2009-05-02 21:40:55 +02:00
|
|
|
getfilesize = StdIoGetFileSize;
|
2011-09-28 21:47:21 +02:00
|
|
|
|
|
|
|
#ifdef __VISUALC__
|
2013-01-04 16:01:50 +01:00
|
|
|
fp = _wfopen(filename.c_str(), L"rb");
|
2011-09-28 21:47:21 +02:00
|
|
|
#else
|
2013-01-04 16:01:50 +01:00
|
|
|
fp = fopen(filename.c_str(), "rb");
|
2011-09-28 21:47:21 +02:00
|
|
|
#endif
|
2013-01-04 16:01:50 +01:00
|
|
|
if (!fp)
|
|
|
|
throw agi::fs::FileNotFound(filename);
|
|
|
|
|
|
|
|
setvbuf(fp, nullptr, _IOFBF, CACHESIZE);
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|