2007-01-21 07:30:19 +01:00
|
|
|
// Copyright (c) 2007, Rodrigo Braz Monteiro
|
|
|
|
// 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 subtitles_provider_csri.cpp
|
|
|
|
/// @brief Wrapper for CSRI-based subtitle renderers
|
|
|
|
/// @ingroup subtitle_rendering
|
|
|
|
///
|
2007-01-21 07:30:19 +01:00
|
|
|
|
2007-12-31 07:46:22 +01:00
|
|
|
#ifdef WITH_CSRI
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "subtitles_provider_csri.h"
|
2011-11-04 06:11:31 +01:00
|
|
|
|
2014-03-24 15:54:22 +01:00
|
|
|
#include "include/aegisub/subtitles_provider.h"
|
2014-04-30 02:05:53 +02:00
|
|
|
#include "subtitle_format_ass.h"
|
2010-08-02 08:32:01 +02:00
|
|
|
#include "video_frame.h"
|
2007-01-21 07:30:19 +01:00
|
|
|
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
2012-04-10 22:41:25 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
#define CSRIAPI
|
|
|
|
#endif
|
2007-01-21 07:30:19 +01:00
|
|
|
|
2014-03-11 20:11:05 +01:00
|
|
|
#include <csri/csri.h>
|
|
|
|
|
2014-03-24 15:54:22 +01:00
|
|
|
namespace {
|
2013-01-04 16:01:50 +01:00
|
|
|
// CSRI renderers are not required to be thread safe (and VSFilter very much
|
|
|
|
// is not)
|
2014-03-24 15:54:22 +01:00
|
|
|
std::mutex csri_mutex;
|
|
|
|
|
2014-04-30 02:05:53 +02:00
|
|
|
struct closer {
|
|
|
|
void operator()(csri_inst *inst) { if (inst) csri_close(inst); }
|
|
|
|
};
|
|
|
|
|
2014-03-24 15:54:22 +01:00
|
|
|
class CSRISubtitlesProvider final : public SubtitlesProvider {
|
2014-04-30 02:05:53 +02:00
|
|
|
std::unique_ptr<csri_inst, closer> instance;
|
|
|
|
csri_rend *renderer = nullptr;
|
2014-03-24 15:54:22 +01:00
|
|
|
|
2014-05-02 19:39:38 +02:00
|
|
|
void LoadSubtitles(const char *data, size_t len) override {
|
|
|
|
std::lock_guard<std::mutex> lock(csri_mutex);
|
|
|
|
instance.reset(csri_open_mem(renderer, data, len, nullptr));
|
|
|
|
}
|
|
|
|
|
2014-03-24 15:54:22 +01:00
|
|
|
public:
|
|
|
|
CSRISubtitlesProvider(std::string subType);
|
|
|
|
|
2014-05-02 19:39:38 +02:00
|
|
|
void DrawSubtitles(VideoFrame &dst, double time) override;
|
2014-03-24 15:54:22 +01:00
|
|
|
};
|
2007-01-21 07:30:19 +01:00
|
|
|
|
2014-04-30 02:05:53 +02:00
|
|
|
CSRISubtitlesProvider::CSRISubtitlesProvider(std::string type) {
|
|
|
|
std::lock_guard<std::mutex> lock(csri_mutex);
|
2012-04-14 05:17:15 +02:00
|
|
|
for (csri_rend *cur = csri_renderer_default(); cur; cur = csri_renderer_next(cur)) {
|
|
|
|
if (type == csri_renderer_info(cur)->name) {
|
2007-04-08 18:33:53 +02:00
|
|
|
renderer = cur;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 22:41:25 +02:00
|
|
|
if (!renderer)
|
2014-05-29 14:57:27 +02:00
|
|
|
throw agi::InternalError("CSRI renderer vanished between initial list and creation?");
|
2012-04-10 22:41:25 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:05:53 +02:00
|
|
|
void CSRISubtitlesProvider::DrawSubtitles(VideoFrame &dst, double time) {
|
2012-04-10 22:41:25 +02:00
|
|
|
if (!instance) return;
|
2011-11-04 06:11:31 +01:00
|
|
|
|
2007-01-21 07:30:19 +01:00
|
|
|
csri_frame frame;
|
2010-12-31 22:03:11 +01:00
|
|
|
if (dst.flipped) {
|
2013-07-01 05:15:43 +02:00
|
|
|
frame.planes[0] = dst.data.data() + (dst.height-1) * dst.width * 4;
|
|
|
|
frame.strides[0] = -(signed)dst.width * 4;
|
2007-01-21 07:30:19 +01:00
|
|
|
}
|
2010-12-31 22:03:11 +01:00
|
|
|
else {
|
2013-07-01 05:15:43 +02:00
|
|
|
frame.planes[0] = dst.data.data();
|
|
|
|
frame.strides[0] = dst.width * 4;
|
2007-01-21 07:30:19 +01:00
|
|
|
}
|
2010-12-31 22:03:11 +01:00
|
|
|
frame.pixfmt = CSRI_F_BGR_;
|
2007-01-21 07:30:19 +01:00
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
csri_fmt format = { frame.pixfmt, dst.width, dst.height };
|
2007-01-21 07:30:19 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::lock_guard<std::mutex> lock(csri_mutex);
|
2014-04-30 02:05:53 +02:00
|
|
|
if (!csri_request_fmt(instance.get(), &format))
|
|
|
|
csri_render(instance.get(), &frame, time);
|
2007-01-21 07:30:19 +01:00
|
|
|
}
|
2014-03-24 15:54:22 +01:00
|
|
|
}
|
2007-12-31 07:46:22 +01:00
|
|
|
|
2014-03-24 15:54:22 +01:00
|
|
|
namespace csri {
|
|
|
|
std::vector<std::string> List() {
|
2010-08-02 08:31:38 +02:00
|
|
|
std::vector<std::string> final;
|
2012-04-17 01:54:34 +02:00
|
|
|
for (csri_rend *cur = csri_renderer_default(); cur; cur = csri_renderer_next(cur)) {
|
|
|
|
std::string name(csri_renderer_info(cur)->name);
|
|
|
|
if (name.find("aegisub") != name.npos)
|
|
|
|
final.insert(final.begin(), name);
|
|
|
|
else
|
|
|
|
final.push_back(name);
|
2008-03-05 03:58:11 +01:00
|
|
|
}
|
|
|
|
return final;
|
|
|
|
}
|
|
|
|
|
2014-03-25 17:51:38 +01:00
|
|
|
std::unique_ptr<SubtitlesProvider> Create(std::string const& name, agi::BackgroundRunner *) {
|
2014-04-23 22:53:24 +02:00
|
|
|
return agi::make_unique<CSRISubtitlesProvider>(name);
|
2014-03-24 15:54:22 +01:00
|
|
|
}
|
|
|
|
}
|
2007-12-31 07:46:22 +01:00
|
|
|
#endif // WITH_CSRI
|