2010-07-23 07:58:39 +02:00
|
|
|
// Copyright (c) 2010, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file threaded_frame_source.cpp
|
|
|
|
/// @see threaded_frame_source.h
|
|
|
|
/// @ingroup video
|
|
|
|
///
|
|
|
|
|
|
|
|
#include "threaded_frame_source.h"
|
|
|
|
|
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <functional>
|
2012-10-12 19:16:39 +02:00
|
|
|
|
|
|
|
#include <boost/range/adaptor/indirected.hpp>
|
|
|
|
#include <boost/range/algorithm_ext.hpp>
|
2010-07-23 07:58:39 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_exporter.h"
|
|
|
|
#include "ass_file.h"
|
|
|
|
#include "compat.h"
|
2011-09-28 21:46:53 +02:00
|
|
|
#include "include/aegisub/context.h"
|
2010-08-02 08:31:38 +02:00
|
|
|
#include "include/aegisub/subtitles_provider.h"
|
2012-01-18 21:08:42 +01:00
|
|
|
#include "video_frame.h"
|
2010-07-23 07:58:39 +02:00
|
|
|
#include "video_provider_manager.h"
|
|
|
|
|
|
|
|
// Test if a line is a dialogue line which is not visible at the given time
|
2012-10-12 19:16:39 +02:00
|
|
|
struct invisible_line : public std::unary_function<AssEntry const&, bool> {
|
2010-07-23 07:58:39 +02:00
|
|
|
double time;
|
|
|
|
invisible_line(double time) : time(time * 1000.) { }
|
2012-10-12 19:16:39 +02:00
|
|
|
bool operator()(AssEntry const& entry) const {
|
|
|
|
const AssDialogue *diag = dynamic_cast<const AssDialogue*>(&entry);
|
2011-12-22 22:28:51 +01:00
|
|
|
return diag && (diag->Start > time || diag->End <= time);
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-09-16 00:10:48 +02:00
|
|
|
static void delete_frame(AegiVideoFrame *frame) {
|
|
|
|
frame->Clear();
|
|
|
|
delete frame;
|
|
|
|
}
|
|
|
|
|
2012-09-25 01:35:27 +02:00
|
|
|
std::shared_ptr<AegiVideoFrame> ThreadedFrameSource::ProcFrame(int frameNum, double time, bool raw) {
|
|
|
|
std::shared_ptr<AegiVideoFrame> frame(new AegiVideoFrame, delete_frame);
|
2010-07-23 07:58:39 +02:00
|
|
|
{
|
|
|
|
wxMutexLocker locker(providerMutex);
|
|
|
|
try {
|
|
|
|
frame->CopyFrom(videoProvider->GetFrame(frameNum));
|
|
|
|
}
|
2010-08-02 08:32:01 +02:00
|
|
|
catch (VideoProviderError const& err) { throw VideoProviderErrorEvent(err); }
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This deliberately results in a call to LoadSubtitles while a render
|
|
|
|
// is pending making the queued render use the new file
|
2011-12-22 22:29:29 +01:00
|
|
|
if (!raw && provider) {
|
2010-07-23 07:58:39 +02:00
|
|
|
try {
|
|
|
|
wxMutexLocker locker(fileMutex);
|
|
|
|
if (subs.get() && singleFrame != frameNum) {
|
|
|
|
// Generally edits and seeks come in groups; if the last thing done
|
|
|
|
// was seek it is more likely that the user will seek again and
|
|
|
|
// vice versa. As such, if this is the first frame requested after
|
|
|
|
// an edit, only export the currently visible lines (because the
|
|
|
|
// other lines will probably not be viewed before the file changes
|
|
|
|
// again), and if it's a different frame, export the entire file.
|
|
|
|
if (singleFrame == -1) {
|
2011-09-28 21:46:53 +02:00
|
|
|
// This will crash if any of the export filters try to use
|
|
|
|
// anything but the subtitles, but that wouldn't be safe to
|
|
|
|
// do anyway
|
2011-12-28 22:27:06 +01:00
|
|
|
agi::Context c;
|
|
|
|
memset(&c, 0, sizeof c);
|
2011-09-28 21:46:53 +02:00
|
|
|
c.ass = subs.get();
|
|
|
|
|
|
|
|
AssExporter exporter(&c);
|
2010-11-18 07:15:41 +01:00
|
|
|
exporter.AddAutoFilters();
|
|
|
|
exporter.ExportTransform();
|
|
|
|
|
2010-07-23 07:58:39 +02:00
|
|
|
singleFrame = frameNum;
|
|
|
|
// Copying a nontrivially sized AssFile is fairly slow, so
|
|
|
|
// instead muck around with its innards to just temporarily
|
|
|
|
// remove the non-visible lines without deleting them
|
2012-10-12 19:16:39 +02:00
|
|
|
std::deque<AssEntry*> full;
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto& line : subs->Line)
|
|
|
|
full.push_back(&line);
|
2012-10-12 19:16:39 +02:00
|
|
|
subs->Line.remove_if(invisible_line(time));
|
|
|
|
|
2010-07-23 07:58:39 +02:00
|
|
|
try {
|
|
|
|
provider->LoadSubtitles(subs.get());
|
2012-10-12 19:16:39 +02:00
|
|
|
|
|
|
|
subs->Line.clear();
|
|
|
|
boost::push_back(subs->Line, full | boost::adaptors::indirected);
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
catch(...) {
|
2012-10-12 19:16:39 +02:00
|
|
|
subs->Line.clear();
|
|
|
|
boost::push_back(subs->Line, full | boost::adaptors::indirected);
|
2010-07-23 07:58:39 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
provider->LoadSubtitles(subs.get());
|
|
|
|
subs.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (wxString const& err) { throw SubtitlesProviderErrorEvent(err); }
|
|
|
|
|
|
|
|
provider->DrawSubtitles(*frame, time);
|
|
|
|
}
|
2010-09-16 00:10:48 +02:00
|
|
|
return frame;
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *ThreadedFrameSource::Entry() {
|
2012-05-01 04:49:26 +02:00
|
|
|
while (!TestDestroy()) {
|
2010-09-16 00:10:42 +02:00
|
|
|
double time;
|
|
|
|
int frameNum;
|
2012-11-16 00:46:56 +01:00
|
|
|
std::unique_ptr<AssFile> newSubs;
|
2010-09-16 00:10:42 +02:00
|
|
|
{
|
|
|
|
wxMutexLocker jobLocker(jobMutex);
|
|
|
|
|
2012-05-01 04:49:26 +02:00
|
|
|
if (!run)
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
2010-09-16 00:10:42 +02:00
|
|
|
if (nextTime == -1.) {
|
|
|
|
jobReady.Wait();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
time = nextTime;
|
|
|
|
frameNum = nextFrame;
|
|
|
|
nextTime = -1.;
|
2012-11-16 00:46:56 +01:00
|
|
|
newSubs = move(nextSubs);
|
2010-12-30 23:19:36 +01:00
|
|
|
}
|
|
|
|
|
2012-11-16 00:46:56 +01:00
|
|
|
if (newSubs) {
|
2010-12-30 23:19:36 +01:00
|
|
|
wxMutexLocker fileLocker(fileMutex);
|
2012-11-16 00:46:56 +01:00
|
|
|
subs = move(newSubs);
|
2010-12-30 23:19:36 +01:00
|
|
|
singleFrame = -1;
|
2010-09-16 00:10:42 +02:00
|
|
|
}
|
2010-07-23 07:58:39 +02:00
|
|
|
|
|
|
|
try {
|
2010-09-16 00:10:48 +02:00
|
|
|
FrameReadyEvent *evt = new FrameReadyEvent(ProcFrame(frameNum, time), time);
|
2010-07-23 07:58:39 +02:00
|
|
|
evt->SetEventType(EVT_FRAME_READY);
|
|
|
|
parent->QueueEvent(evt);
|
|
|
|
}
|
|
|
|
catch (wxEvent const& err) {
|
|
|
|
// Pass error back to parent thread
|
|
|
|
parent->QueueEvent(err.Clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:29:29 +01:00
|
|
|
static SubtitlesProvider *get_subs_provider(wxEvtHandler *parent) {
|
|
|
|
try {
|
|
|
|
return SubtitlesProviderFactory::GetProvider();
|
|
|
|
}
|
|
|
|
catch (wxString const& err) {
|
|
|
|
parent->AddPendingEvent(SubtitlesProviderErrorEvent(err));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-23 07:58:39 +02:00
|
|
|
ThreadedFrameSource::ThreadedFrameSource(wxString videoFileName, wxEvtHandler *parent)
|
2010-08-27 03:01:35 +02:00
|
|
|
: wxThread(wxTHREAD_JOINABLE)
|
2011-12-22 22:29:29 +01:00
|
|
|
, provider(get_subs_provider(parent))
|
2010-08-02 08:31:38 +02:00
|
|
|
, videoProvider(VideoProviderFactory::GetProvider(videoFileName))
|
2010-07-23 07:58:39 +02:00
|
|
|
, parent(parent)
|
2012-10-15 06:37:14 +02:00
|
|
|
, nextFrame(-1)
|
2010-07-23 07:58:39 +02:00
|
|
|
, nextTime(-1.)
|
2012-10-15 06:37:14 +02:00
|
|
|
, singleFrame(-1)
|
2010-07-23 07:58:39 +02:00
|
|
|
, jobReady(jobMutex)
|
|
|
|
, run(true)
|
|
|
|
{
|
|
|
|
Create();
|
|
|
|
Run();
|
|
|
|
}
|
2012-05-01 04:49:26 +02:00
|
|
|
|
2010-08-27 03:01:35 +02:00
|
|
|
ThreadedFrameSource::~ThreadedFrameSource() {
|
2012-05-01 04:49:26 +02:00
|
|
|
{
|
|
|
|
wxMutexLocker locker(jobMutex);
|
|
|
|
run = false;
|
|
|
|
jobReady.Signal();
|
|
|
|
}
|
2010-08-27 03:01:35 +02:00
|
|
|
Wait();
|
|
|
|
}
|
2010-07-23 07:58:39 +02:00
|
|
|
|
2010-07-23 08:21:22 +02:00
|
|
|
void ThreadedFrameSource::LoadSubtitles(AssFile *subs) throw() {
|
2010-11-18 07:15:41 +01:00
|
|
|
subs = new AssFile(*subs);
|
2010-07-23 07:58:39 +02:00
|
|
|
wxMutexLocker locker(jobMutex);
|
|
|
|
// Set nextSubs and let the worker thread move it to subs so that we don't
|
|
|
|
// have to lock fileMutex on the GUI thread, as that can be locked for
|
|
|
|
// extended periods of time with slow-rendering subtitles
|
2010-11-18 07:15:41 +01:00
|
|
|
nextSubs.reset(subs);
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
|
2010-07-23 08:21:22 +02:00
|
|
|
void ThreadedFrameSource::RequestFrame(int frame, double time) throw() {
|
2010-07-23 07:58:39 +02:00
|
|
|
wxMutexLocker locker(jobMutex);
|
|
|
|
nextTime = time;
|
|
|
|
nextFrame = frame;
|
|
|
|
jobReady.Signal();
|
|
|
|
}
|
|
|
|
|
2012-09-25 01:35:27 +02:00
|
|
|
std::shared_ptr<AegiVideoFrame> ThreadedFrameSource::GetFrame(int frame, double time, bool raw) {
|
2010-07-23 07:58:39 +02:00
|
|
|
return ProcFrame(frame, time, raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDEFINE_EVENT(EVT_FRAME_READY, FrameReadyEvent);
|
|
|
|
wxDEFINE_EVENT(EVT_VIDEO_ERROR, VideoProviderErrorEvent);
|
|
|
|
wxDEFINE_EVENT(EVT_SUBTITLES_ERROR, SubtitlesProviderErrorEvent);
|
|
|
|
|
2010-08-02 08:32:01 +02:00
|
|
|
VideoProviderErrorEvent::VideoProviderErrorEvent(VideoProviderError const& err)
|
|
|
|
: agi::Exception(err.GetMessage(), &err)
|
2010-07-23 07:58:39 +02:00
|
|
|
{
|
|
|
|
SetEventType(EVT_VIDEO_ERROR);
|
|
|
|
}
|
2010-08-02 08:32:01 +02:00
|
|
|
SubtitlesProviderErrorEvent::SubtitlesProviderErrorEvent(wxString err)
|
2012-11-13 17:51:01 +01:00
|
|
|
: agi::Exception(STD_STR(err), nullptr)
|
2010-07-23 07:58:39 +02:00
|
|
|
{
|
|
|
|
SetEventType(EVT_SUBTITLES_ERROR);
|
|
|
|
}
|