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/
|
|
|
|
|
2014-05-22 01:23:28 +02:00
|
|
|
#include "async_video_provider.h"
|
2010-07-23 07:58:39 +02:00
|
|
|
|
|
|
|
#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"
|
2014-06-12 23:34:21 +02: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>
|
|
|
|
|
2012-12-29 17:40:10 +01:00
|
|
|
enum {
|
|
|
|
NEW_SUBS_FILE = -1,
|
|
|
|
SUBS_FILE_ALREADY_LOADED = -2
|
|
|
|
};
|
|
|
|
|
2014-05-22 01:23:28 +02:00
|
|
|
std::shared_ptr<VideoFrame> AsyncVideoProvider::ProcFrame(int frame_number, double time, bool raw) {
|
2014-06-12 23:34:21 +02:00
|
|
|
// Find an unused buffer to use or allocate a new one if needed
|
2013-07-01 05:15:43 +02:00
|
|
|
std::shared_ptr<VideoFrame> frame;
|
2014-06-12 23:34:21 +02:00
|
|
|
for (auto& buffer : buffers) {
|
|
|
|
if (buffer.use_count() == 1) {
|
|
|
|
frame = buffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frame) {
|
|
|
|
frame = std::make_shared<VideoFrame>();
|
|
|
|
buffers.push_back(frame);
|
|
|
|
}
|
2012-11-26 06:28:13 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
try {
|
2014-06-12 23:34:21 +02:00
|
|
|
source_provider->GetFrame(frame_number, *frame);
|
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 {
|
2014-07-14 17:40:13 +02:00
|
|
|
AssFixStylesFilter::ProcessSubs(subs.get());
|
2013-01-04 16:01:50 +01:00
|
|
|
single_frame = frame_number;
|
2014-04-30 16:22:41 +02:00
|
|
|
subs_provider->LoadSubtitles(subs.get(), time);
|
2012-12-17 17:27:11 +01:00
|
|
|
}
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-11 00:28:45 +02:00
|
|
|
catch (agi::Exception const& err) { throw SubtitlesProviderErrorEvent(err.GetMessage()); }
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2014-03-25 21:21:57 +01:00
|
|
|
try {
|
|
|
|
subs_provider->DrawSubtitles(*frame, time / 1000.);
|
|
|
|
}
|
|
|
|
catch (agi::UserCancelException const&) { }
|
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
|
|
|
}
|
|
|
|
|
2014-03-25 17:51:38 +01:00
|
|
|
static std::unique_ptr<SubtitlesProvider> get_subs_provider(wxEvtHandler *evt_handler, agi::BackgroundRunner *br) {
|
2011-12-22 22:29:29 +01:00
|
|
|
try {
|
2014-03-25 17:51:38 +01:00
|
|
|
return SubtitlesProviderFactory::GetProvider(br);
|
2011-12-22 22:29:29 +01:00
|
|
|
}
|
2014-06-11 00:28:45 +02:00
|
|
|
catch (agi::Exception const& err) {
|
|
|
|
evt_handler->AddPendingEvent(SubtitlesProviderErrorEvent(err.GetMessage()));
|
2013-11-21 18:13:36 +01:00
|
|
|
return nullptr;
|
2011-12-22 22:29:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 01:23:28 +02:00
|
|
|
AsyncVideoProvider::AsyncVideoProvider(agi::fs::path const& video_filename, std::string const& colormatrix, wxEvtHandler *parent, agi::BackgroundRunner *br)
|
2013-01-04 16:01:50 +01:00
|
|
|
: worker(agi::dispatch::Create())
|
2014-03-25 17:51:38 +01:00
|
|
|
, subs_provider(get_subs_provider(parent, br))
|
2014-05-22 01:23:28 +02:00
|
|
|
, source_provider(VideoProviderFactory::GetProvider(video_filename, colormatrix, br))
|
2010-07-23 07:58:39 +02:00
|
|
|
, parent(parent)
|
|
|
|
{
|
|
|
|
}
|
2012-05-01 04:49:26 +02:00
|
|
|
|
2014-05-22 01:23:28 +02:00
|
|
|
AsyncVideoProvider::~AsyncVideoProvider() {
|
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
|
|
|
|
2014-05-22 01:23:28 +02:00
|
|
|
void AsyncVideoProvider::LoadSubtitles(const AssFile *new_subs) throw() {
|
2013-01-04 16:01:50 +01:00
|
|
|
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;
|
2014-06-15 16:10:29 +02:00
|
|
|
ProcAsync(req_version, false);
|
2013-01-04 16:01:50 +01:00
|
|
|
});
|
2012-12-17 17:27:11 +01:00
|
|
|
}
|
|
|
|
|
2014-12-28 21:06:15 +01:00
|
|
|
void AsyncVideoProvider::UpdateSubtitles(const AssFile *new_subs, const AssDialogue *changed) throw() {
|
2013-01-04 16:01:50 +01:00
|
|
|
uint_fast32_t req_version = ++version;
|
|
|
|
|
2014-12-28 21:06:15 +01:00
|
|
|
// Copy just the line which were changed, then replace the line at the
|
|
|
|
// same index in the worker's copy of the file with the new entry
|
|
|
|
auto copy = new AssDialogue(*changed);
|
2013-01-04 16:01:50 +01:00
|
|
|
worker->Async([=]{
|
2014-06-12 19:31:49 +02:00
|
|
|
int i = 0;
|
2014-03-07 18:02:24 +01:00
|
|
|
auto it = subs->Events.begin();
|
2014-12-28 21:06:15 +01:00
|
|
|
std::advance(it, copy->Row - i);
|
|
|
|
i = copy->Row;
|
|
|
|
subs->Events.insert(it, *copy);
|
|
|
|
delete &*it--;
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
single_frame = NEW_SUBS_FILE;
|
2014-06-15 16:10:29 +02:00
|
|
|
ProcAsync(req_version, true);
|
2013-01-04 16:01:50 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-22 01:23:28 +02:00
|
|
|
void AsyncVideoProvider::RequestFrame(int new_frame, double new_time) throw() {
|
2013-01-04 16:01:50 +01:00
|
|
|
uint_fast32_t req_version = ++version;
|
|
|
|
|
|
|
|
worker->Async([=]{
|
|
|
|
time = new_time;
|
|
|
|
frame_number = new_frame;
|
2014-06-15 16:10:29 +02:00
|
|
|
ProcAsync(req_version, false);
|
2013-01-04 16:01:50 +01:00
|
|
|
});
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-07 02:20:01 +02:00
|
|
|
bool AsyncVideoProvider::NeedUpdate(std::vector<AssDialogueBase const*> const& visible_lines) {
|
|
|
|
// Always need to render after a seek
|
|
|
|
if (single_frame != NEW_SUBS_FILE || frame_number != last_rendered)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Obviously need to render if the number of visible lines has changed
|
|
|
|
if (visible_lines.size() != last_lines.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < last_lines.size(); ++i) {
|
|
|
|
auto const& last = last_lines[i];
|
|
|
|
auto const& cur = *visible_lines[i];
|
|
|
|
if (last.Layer != cur.Layer) return true;
|
|
|
|
if (last.Margin != cur.Margin) return true;
|
|
|
|
if (last.Style != cur.Style) return true;
|
|
|
|
if (last.Effect != cur.Effect) return true;
|
|
|
|
if (last.Text != cur.Text) return true;
|
|
|
|
|
|
|
|
// Changing the start/end time effects the appearance only if the
|
|
|
|
// line is animated. This is obviously not a very accurate check for
|
|
|
|
// animated lines, but false positives aren't the end of the world
|
|
|
|
if ((last.Start != cur.Start || last.End != cur.End) &&
|
|
|
|
(!cur.Effect.get().empty() || cur.Text.get().find('\\') != std::string::npos))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-15 16:10:29 +02:00
|
|
|
void AsyncVideoProvider::ProcAsync(uint_fast32_t req_version, bool check_updated) {
|
2013-01-04 16:01:50 +01:00
|
|
|
// Only actually produce the frame if there's no queued changes waiting
|
|
|
|
if (req_version < version || frame_number < 0) return;
|
|
|
|
|
2014-06-07 02:20:01 +02:00
|
|
|
std::vector<AssDialogueBase const*> visible_lines;
|
|
|
|
for (auto const& line : subs->Events) {
|
|
|
|
if (!line.Comment && !(line.Start > time || line.End <= time))
|
|
|
|
visible_lines.push_back(&line);
|
|
|
|
}
|
|
|
|
|
2014-06-15 16:10:29 +02:00
|
|
|
if (check_updated && !NeedUpdate(visible_lines)) return;
|
2014-06-07 02:20:01 +02:00
|
|
|
|
|
|
|
last_lines.clear();
|
|
|
|
last_lines.reserve(visible_lines.size());
|
|
|
|
for (auto line : visible_lines)
|
|
|
|
last_lines.push_back(*line);
|
|
|
|
last_rendered = frame_number;
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-22 01:23:28 +02:00
|
|
|
std::shared_ptr<VideoFrame> AsyncVideoProvider::GetFrame(int frame, double time, bool raw) {
|
2013-07-01 05:15:43 +02:00
|
|
|
std::shared_ptr<VideoFrame> ret;
|
2014-05-20 04:21:50 +02:00
|
|
|
worker->Sync([&]{ ret = ProcFrame(frame, time, raw); });
|
2013-01-04 16:01:50 +01:00
|
|
|
return ret;
|
2010-07-23 07:58:39 +02:00
|
|
|
}
|
|
|
|
|
2014-05-22 01:23:28 +02:00
|
|
|
void AsyncVideoProvider::SetColorSpace(std::string const& matrix) {
|
|
|
|
worker->Async([=] { source_provider->SetColorSpace(matrix); });
|
2014-05-20 04:21:50 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2014-05-29 14:57:27 +02:00
|
|
|
: agi::Exception(err.GetMessage())
|
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)
|
2014-05-29 14:57:27 +02:00
|
|
|
: agi::Exception(err)
|
2010-07-23 07:58:39 +02:00
|
|
|
{
|
|
|
|
SetEventType(EVT_SUBTITLES_ERROR);
|
|
|
|
}
|