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"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
2006-02-26 03:52:29 +01:00
|
|
|
#include <errno.h>
|
2012-09-25 02:09:42 +02:00
|
|
|
#include <cstdint>
|
2011-09-28 21:47:21 +02:00
|
|
|
#include <stdio.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
2010-07-08 06:29:04 +02:00
|
|
|
#include <iterator>
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2006-12-26 02:08:46 +01:00
|
|
|
#include <wx/filename.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#include <wx/tokenzr.h>
|
2009-09-11 00:48:29 +02:00
|
|
|
#include <wx/choicdlg.h> // Keep this last so wxUSE_CHOICEDLG is set.
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
class MkvStdIO : public InputStream {
|
|
|
|
public:
|
|
|
|
MkvStdIO(wxString filename);
|
|
|
|
~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) {
|
2011-09-28 21:47:40 +02:00
|
|
|
std::map<int, wxString> subList;
|
|
|
|
char *readBuf = 0;
|
|
|
|
size_t readBufSize = 0;
|
|
|
|
|
|
|
|
// Load blocks
|
|
|
|
ulonglong startTime, endTime, filePos;
|
|
|
|
unsigned int rt, frameSize, frameFlags;
|
|
|
|
|
|
|
|
while (mkv_ReadFrame(file,0,&rt,&startTime,&endTime,&filePos,&frameSize,&frameFlags) == 0) {
|
|
|
|
if (ps->IsCancelled()) {
|
2012-03-13 00:34:05 +01:00
|
|
|
delete[] readBuf;
|
2011-09-28 21:47:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read to temp
|
|
|
|
if (frameSize > readBufSize) {
|
2011-12-26 23:20:49 +01:00
|
|
|
delete[] readBuf;
|
2011-09-28 21:47:40 +02:00
|
|
|
readBufSize = frameSize * 2;
|
|
|
|
readBuf = new char[readBufSize];
|
|
|
|
}
|
2011-12-26 23:20:49 +01:00
|
|
|
else if (frameSize == 0)
|
|
|
|
continue;
|
2011-09-28 21:47:40 +02:00
|
|
|
|
2012-04-22 17:31:11 +02:00
|
|
|
std_fseek(input->fp, filePos, SEEK_SET);
|
2011-09-28 21:47:40 +02:00
|
|
|
fread(readBuf, 1, frameSize, input->fp);
|
|
|
|
wxString blockString(readBuf, wxConvUTF8, frameSize);
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
long order = 0, layer = 0;
|
2011-10-29 06:16:31 +02:00
|
|
|
wxString afterOrder, afterLayer;
|
|
|
|
blockString.BeforeFirst(',', &afterOrder).ToLong(&order);
|
|
|
|
afterOrder.BeforeFirst(',', &afterLayer).ToLong(&layer);
|
2011-09-28 21:47:40 +02:00
|
|
|
|
2012-10-12 03:52:36 +02:00
|
|
|
subList[order] = wxString::Format("Dialogue: %d,%s,%s,%s", (int)layer, subStart.GetAssFormated(), subEnd.GetAssFormated(), afterLayer);
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
|
|
|
// Process SRT
|
|
|
|
else {
|
2012-11-11 02:53:57 +01:00
|
|
|
blockString = wxString::Format("Dialogue: 0,%s,%s,Default,,0,0,0,,%s", subStart.GetAssFormated(), subEnd.GetAssFormated(), blockString);
|
2011-09-28 21:47:40 +02:00
|
|
|
blockString.Replace("\r\n","\\N");
|
|
|
|
blockString.Replace("\r","\\N");
|
|
|
|
blockString.Replace("\n","\\N");
|
|
|
|
|
|
|
|
subList[subList.size()] = blockString;
|
|
|
|
}
|
|
|
|
|
2011-11-28 23:16:58 +01:00
|
|
|
ps->SetProgress(startTime / timecodeScaleLow, totalTime);
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2011-12-26 23:20:49 +01:00
|
|
|
delete[] readBuf;
|
2011-09-28 21:47:40 +02:00
|
|
|
|
|
|
|
// Insert into file
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto order_value_pair : subList) {
|
|
|
|
parser->AddLine(order_value_pair.second);
|
2011-09-28 21:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
void MatroskaWrapper::GetSubtitles(wxString const& filename, AssFile *target) {
|
|
|
|
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);
|
|
|
|
TrackInfo *trackInfo;
|
|
|
|
std::vector<unsigned> tracksFound;
|
|
|
|
wxArrayString tracksNames;
|
|
|
|
unsigned trackToRead;
|
|
|
|
|
|
|
|
// Find tracks
|
|
|
|
for (unsigned track = 0; track < tracks; track++) {
|
|
|
|
trackInfo = mkv_GetTrackInfo(file,track);
|
|
|
|
|
|
|
|
// Subtitle track
|
|
|
|
if (trackInfo->Type == 0x11) {
|
|
|
|
wxString CodecID = wxString(trackInfo->CodecID,*wxConvCurrent);
|
|
|
|
wxString TrackName = wxString(trackInfo->Name,*wxConvCurrent);
|
|
|
|
wxString TrackLanguage = wxString(trackInfo->Language,*wxConvCurrent);
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
// Known subtitle format
|
|
|
|
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8") {
|
|
|
|
tracksFound.push_back(track);
|
|
|
|
tracksNames.Add(wxString::Format("%d (%s %s): %s", track, CodecID, TrackLanguage, TrackName));
|
2006-02-23 03:18:32 +01:00
|
|
|
}
|
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
|
|
|
|
if (tracksFound.size() == 1) {
|
|
|
|
trackToRead = tracksFound[0];
|
2006-12-17 06:32:18 +01:00
|
|
|
}
|
2011-09-28 21:47:21 +02:00
|
|
|
// Pick a track
|
|
|
|
else {
|
|
|
|
int choice = wxGetSingleChoiceIndex(_("Choose which track to read:"), _("Multiple subtitle tracks found"), tracksNames);
|
|
|
|
if (choice == -1)
|
|
|
|
throw agi::UserCancelException("cancelled");
|
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));
|
2006-12-17 06:32:18 +01:00
|
|
|
trackInfo = mkv_GetTrackInfo(file,trackToRead);
|
|
|
|
wxString CodecID = wxString(trackInfo->CodecID,*wxConvCurrent);
|
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
|
|
|
|
trackInfo = mkv_GetTrackInfo(file,trackToRead);
|
2011-09-28 21:47:21 +02:00
|
|
|
wxString privString((const char *)trackInfo->CodecPrivate, wxConvUTF8, trackInfo->CodecPrivateSize);
|
2006-12-17 06:32:18 +01:00
|
|
|
|
|
|
|
// Load into file
|
2012-01-08 02:34:30 +01:00
|
|
|
wxStringTokenizer token(privString, "\r\n", wxTOKEN_STRTOK);
|
|
|
|
while (token.HasMoreTokens())
|
2012-10-12 04:12:42 +02:00
|
|
|
parser.AddLine(token.GetNextToken());
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-19 02:44:37 +02:00
|
|
|
bool MatroskaWrapper::HasSubtitles(wxString const& filename) {
|
|
|
|
char err[2048];
|
2011-09-28 21:47:21 +02:00
|
|
|
try {
|
|
|
|
MkvStdIO input(filename);
|
|
|
|
MatroskaFile* file = mkv_Open(&input, err, sizeof(err));
|
|
|
|
if (!file) return false;
|
|
|
|
|
|
|
|
// Find tracks
|
|
|
|
int tracks = mkv_GetNumTracks(file);
|
|
|
|
for (int track = 0; track < tracks; track++) {
|
|
|
|
TrackInfo *trackInfo = mkv_GetTrackInfo(file, track);
|
|
|
|
|
|
|
|
if (trackInfo->Type == 0x11) {
|
|
|
|
wxString CodecID = wxString(trackInfo->CodecID, *wxConvCurrent);
|
|
|
|
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) {
|
|
|
|
MkvStdIO *st = (MkvStdIO *) _st;
|
|
|
|
size_t rd;
|
2012-04-22 17:31:11 +02:00
|
|
|
if (std_fseek(st->fp, pos, SEEK_SET)) {
|
2006-02-23 02:44:48 +01:00
|
|
|
st->error = errno;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-28 21:47:21 +02:00
|
|
|
rd = fread(buffer, 1, count, st->fp);
|
2006-02-23 02:44:48 +01:00
|
|
|
if (rd == 0) {
|
|
|
|
if (feof(st->fp))
|
|
|
|
return 0;
|
|
|
|
st->error = errno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return rd;
|
|
|
|
}
|
|
|
|
|
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
|
2006-02-23 02:44:48 +01:00
|
|
|
longlong StdIoScan(InputStream *_st, ulonglong start, unsigned signature) {
|
|
|
|
MkvStdIO *st = (MkvStdIO *) _st;
|
|
|
|
int c;
|
|
|
|
unsigned cmp = 0;
|
|
|
|
FILE *fp = st->fp;
|
|
|
|
|
2007-11-13 21:28:09 +01:00
|
|
|
if (std_fseek(fp, start, SEEK_SET))
|
2006-02-23 02:44:48 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
while ((c = getc(fp)) != EOF) {
|
|
|
|
cmp = ((cmp << 8) | c) & 0xffffffff;
|
|
|
|
if (cmp == signature)
|
2007-11-13 21:28:09 +01:00
|
|
|
return std_ftell(fp) - 4;
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
/// @brief This is used to limit readahead.
|
2011-09-28 21:47:21 +02:00
|
|
|
unsigned StdIoGetCacheSize(InputStream *st) {
|
2006-02-23 02:44:48 +01:00
|
|
|
return CACHESIZE;
|
|
|
|
}
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
/// @brief Get last error message
|
2011-09-28 21:47:21 +02:00
|
|
|
const char *StdIoGetLastError(InputStream *st) {
|
|
|
|
return strerror(((MkvStdIO *)st)->error);
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
/// @brief Memory allocation, this is done via stdlib
|
2011-09-28 21:47:21 +02:00
|
|
|
void *StdIoMalloc(InputStream *, size_t size) {
|
2006-02-23 02:44:48 +01:00
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
void *StdIoRealloc(InputStream *, void *mem, size_t size) {
|
|
|
|
return realloc(mem, size);
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
void StdIoFree(InputStream *, void *mem) {
|
2006-02-23 02:44:48 +01:00
|
|
|
free(mem);
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:21 +02:00
|
|
|
int StdIoProgress(InputStream *, ulonglong cur, ulonglong max) {
|
2006-02-23 02:44:48 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-02 21:40:55 +02:00
|
|
|
longlong StdIoGetFileSize(InputStream *_st) {
|
|
|
|
MkvStdIO *st = (MkvStdIO *) _st;
|
|
|
|
longlong epos = 0;
|
2009-05-02 22:06:06 +02:00
|
|
|
longlong cpos = std_ftell(st->fp);
|
|
|
|
std_fseek(st->fp, 0, SEEK_END);
|
|
|
|
epos = std_ftell(st->fp);
|
|
|
|
std_fseek(st->fp, cpos, SEEK_SET);
|
2009-05-02 21:40:55 +02:00
|
|
|
return epos;
|
|
|
|
}
|
|
|
|
|
2012-10-15 06:37:14 +02:00
|
|
|
MkvStdIO::MkvStdIO(wxString filename)
|
|
|
|
: error(0)
|
|
|
|
{
|
2006-02-23 02:44:48 +01:00
|
|
|
read = StdIoRead;
|
|
|
|
scan = StdIoScan;
|
|
|
|
getcachesize = StdIoGetCacheSize;
|
|
|
|
geterror = StdIoGetLastError;
|
|
|
|
memalloc = StdIoMalloc;
|
|
|
|
memrealloc = StdIoRealloc;
|
|
|
|
memfree = StdIoFree;
|
|
|
|
progress = StdIoProgress;
|
2009-05-02 21:40:55 +02:00
|
|
|
getfilesize = StdIoGetFileSize;
|
2011-09-28 21:47:21 +02:00
|
|
|
|
2006-12-26 02:08:46 +01:00
|
|
|
wxFileName fname(filename);
|
2011-09-28 21:47:21 +02:00
|
|
|
#ifdef __VISUALC__
|
|
|
|
fp = _wfopen(fname.GetFullPath().wc_str(), L"rb");
|
|
|
|
#else
|
|
|
|
fp = fopen(fname.GetFullPath().utf8_str(), "rb");
|
|
|
|
#endif
|
2006-02-23 02:44:48 +01:00
|
|
|
if (fp) {
|
2012-11-13 17:51:01 +01:00
|
|
|
setvbuf(fp, nullptr, _IOFBF, CACHESIZE);
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|
2011-09-28 21:47:21 +02:00
|
|
|
else {
|
|
|
|
throw agi::FileNotFoundError(STD_STR(filename));
|
|
|
|
}
|
2006-02-23 02:44:48 +01:00
|
|
|
}
|