2013-01-04 16:01:50 +01:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
2010-07-23 07:58:39 +02:00
|
|
|
//
|
2012-12-02 22:14:24 +01:00
|
|
|
// 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.
|
2010-07-23 07:58:39 +02:00
|
|
|
//
|
2012-12-02 22:14:24 +01:00
|
|
|
// 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.
|
2010-07-23 07:58:39 +02:00
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file threaded_frame_source.cpp
|
|
|
|
/// @see threaded_frame_source.h
|
|
|
|
/// @ingroup video
|
|
|
|
///
|
|
|
|
|
|
|
|
#include "threaded_frame_source.h"
|
|
|
|
|
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
2013-01-03 04:22:04 +01:00
|
|
|
#include "export_fixstyle.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"
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/dispatch.h>
|
|
|
|
|
|
|
|
#include <boost/range/adaptor/indirected.hpp>
|
|
|
|
#include <boost/range/algorithm_ext.hpp>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
|
|
|
|
2012-12-29 17:40:10 +01:00
|
|
|
enum {
|
|
|
|
NEW_SUBS_FILE = -1,
|
|
|
|
SUBS_FILE_ALREADY_LOADED = -2
|
|
|
|
};
|
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
std::shared_ptr<VideoFrame> ThreadedFrameSource::ProcFrame(int frame_number, double time, bool raw) {
|
|
|
|
std::shared_ptr<VideoFrame> frame;
|
2012-11-26 06:28:13 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
try {
|
2013-07-01 05:15:43 +02:00
|
|
|
frame = video_provider->GetFrame(frame_number);
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
catch (VideoProviderError const& err) { throw VideoProviderErrorEvent(err); }
|
2010-09-16 00:10:42 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
if (raw || !subs_provider || !subs) return frame;
|
2012-05-01 04:49:26 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
try {
|
|
|
|
if (single_frame != frame_number && single_frame != SUBS_FILE_ALREADY_LOADED) {
|
|
|
|
// 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 (single_frame != NEW_SUBS_FILE) {
|
|
|
|
subs_provider->LoadSubtitles(subs.get());
|
|
|
|
single_frame = SUBS_FILE_ALREADY_LOADED;
|
2010-09-16 00:10:42 +02:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
else {
|
|
|
|
AssFixStylesFilter().ProcessSubs(subs.get(), nullptr);
|
|
|
|
|
|
|
|
single_frame = frame_number;
|
|
|
|
// 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
|
|
|
|
std::deque<AssEntry*> full;
|
|
|
|
for (auto& line : subs->Line)
|
|
|
|
full.push_back(&line);
|
|
|
|
subs->Line.remove_if([=](AssEntry const& e) -> bool {
|
|
|
|
const AssDialogue *diag = dynamic_cast<const AssDialogue*>(&e);
|
|
|
|
return diag && (diag->Start > time || diag->End <= time);
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
subs_provider->LoadSubtitles(subs.get());
|
|
|
|
|
|
|
|
subs->Line.clear();
|
|
|
|
boost::push_back(subs->Line, full | boost::adaptors::indirected);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
subs->Line.clear();
|
|
|
|
boost::push_back(subs->Line, full | boost::adaptors::indirected);
|
|
|
|
throw;
|
2012-12-17 17:27:11 +01:00
|
|
|
}
|
|
|
|
}
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
catch (std::string const& err) { throw SubtitlesProviderErrorEvent(err); }
|
|
|
|
|
|
|
|
subs_provider->DrawSubtitles(*frame, time / 1000.);
|
2010-07-23 07:58:39 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
return frame;
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
|
2013-06-08 06:19:40 +02:00
|
|
|
static std::unique_ptr<SubtitlesProvider> get_subs_provider(wxEvtHandler *parent) {
|
2011-12-22 22:29:29 +01:00
|
|
|
try {
|
|
|
|
return SubtitlesProviderFactory::GetProvider();
|
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
catch (std::string const& err) {
|
2011-12-22 22:29:29 +01:00
|
|
|
parent->AddPendingEvent(SubtitlesProviderErrorEvent(err));
|
2013-11-21 18:13:36 +01:00
|
|
|
return nullptr;
|
2011-12-22 22:29:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-17 16:23:45 +02:00
|
|
|
ThreadedFrameSource::ThreadedFrameSource(agi::fs::path const& video_filename, std::string const& colormatrix, wxEvtHandler *parent)
|
2013-01-04 16:01:50 +01:00
|
|
|
: worker(agi::dispatch::Create())
|
|
|
|
, subs_provider(get_subs_provider(parent))
|
2013-10-17 16:23:45 +02:00
|
|
|
, video_provider(VideoProviderFactory::GetProvider(video_filename, colormatrix))
|
2010-07-23 07:58:39 +02:00
|
|
|
, parent(parent)
|
|
|
|
{
|
|
|
|
}
|
2012-05-01 04:49:26 +02:00
|
|
|
|
2010-08-27 03:01:35 +02:00
|
|
|
ThreadedFrameSource::~ThreadedFrameSource() {
|
2013-02-09 06:04:19 +01:00
|
|
|
// Block until all currently queued jobs are complete
|
|
|
|
worker->Sync([]{});
|
2010-08-27 03:01:35 +02:00
|
|
|
}
|
2010-07-23 07:58:39 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void ThreadedFrameSource::LoadSubtitles(const AssFile *new_subs) throw() {
|
|
|
|
uint_fast32_t req_version = ++version;
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
auto copy = new AssFile(*new_subs);
|
2013-01-04 16:01:50 +01:00
|
|
|
worker->Async([=]{
|
|
|
|
subs.reset(copy);
|
|
|
|
single_frame = NEW_SUBS_FILE;
|
|
|
|
ProcAsync(req_version);
|
|
|
|
});
|
2012-12-17 17:27:11 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void ThreadedFrameSource::UpdateSubtitles(const AssFile *new_subs, std::set<const AssEntry*> const& changes) throw() {
|
|
|
|
uint_fast32_t req_version = ++version;
|
|
|
|
|
|
|
|
// Copy just the lines which were changed, then replace the lines at the
|
|
|
|
// same indices in the worker's copy of the file with the new entries
|
|
|
|
std::deque<std::pair<size_t, AssEntry*>> changed;
|
2012-12-17 17:27:11 +01:00
|
|
|
size_t i = 0;
|
2013-01-04 16:01:50 +01:00
|
|
|
for (auto const& e : new_subs->Line) {
|
2012-12-17 17:27:11 +01:00
|
|
|
if (changes.count(&e))
|
2013-01-04 16:01:50 +01:00
|
|
|
changed.emplace_back(i, e.Clone());
|
2012-12-17 17:27:11 +01:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
worker->Async([=]{
|
|
|
|
size_t i = 0;
|
|
|
|
auto it = subs->Line.begin();
|
|
|
|
for (auto& update : changed) {
|
|
|
|
advance(it, update.first - i);
|
|
|
|
i = update.first;
|
|
|
|
subs->Line.insert(it, *update.second);
|
|
|
|
delete &*it--;
|
|
|
|
}
|
|
|
|
|
|
|
|
single_frame = NEW_SUBS_FILE;
|
|
|
|
ProcAsync(req_version);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadedFrameSource::RequestFrame(int new_frame, double new_time) throw() {
|
|
|
|
uint_fast32_t req_version = ++version;
|
|
|
|
|
|
|
|
worker->Async([=]{
|
|
|
|
time = new_time;
|
|
|
|
frame_number = new_frame;
|
|
|
|
ProcAsync(req_version);
|
|
|
|
});
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void ThreadedFrameSource::ProcAsync(uint_fast32_t req_version) {
|
|
|
|
// Only actually produce the frame if there's no queued changes waiting
|
|
|
|
if (req_version < version || frame_number < 0) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
FrameReadyEvent *evt = new FrameReadyEvent(ProcFrame(frame_number, time), time);
|
|
|
|
evt->SetEventType(EVT_FRAME_READY);
|
|
|
|
parent->QueueEvent(evt);
|
|
|
|
}
|
|
|
|
catch (wxEvent const& err) {
|
|
|
|
// Pass error back to parent thread
|
|
|
|
parent->QueueEvent(err.Clone());
|
|
|
|
}
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
std::shared_ptr<VideoFrame> ThreadedFrameSource::GetFrame(int frame, double time, bool raw) {
|
|
|
|
std::shared_ptr<VideoFrame> ret;
|
2013-02-09 06:04:19 +01:00
|
|
|
worker->Sync([&]{
|
2013-01-04 16:01:50 +01:00
|
|
|
ret = ProcFrame(frame, time, raw);
|
|
|
|
});
|
|
|
|
return ret;
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
SubtitlesProviderErrorEvent::SubtitlesProviderErrorEvent(std::string const& err)
|
|
|
|
: agi::Exception(err, nullptr)
|
2010-07-23 07:58:39 +02:00
|
|
|
{
|
|
|
|
SetEventType(EVT_SUBTITLES_ERROR);
|
|
|
|
}
|