2007-04-04 06:12:38 +02:00
|
|
|
// Copyright (c) 2006-2007, Rodrigo Braz Monteiro, Evgeniy Stepanov
|
|
|
|
// 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_libass.cpp
|
|
|
|
/// @brief libass-based subtitle renderer
|
|
|
|
/// @ingroup subtitle_rendering
|
|
|
|
///
|
2007-04-04 06:12:38 +02:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "subtitles_provider_libass.h"
|
2007-04-04 06:12:38 +02:00
|
|
|
|
2009-07-12 22:10:25 +02:00
|
|
|
#ifdef __APPLE__
|
2009-07-11 19:28:27 +02:00
|
|
|
#include <sys/param.h>
|
2010-08-14 19:42:37 +02:00
|
|
|
#include <libaegisub/util_osx.h>
|
2009-07-14 00:38:43 +02:00
|
|
|
#endif
|
2007-04-04 06:12:38 +02:00
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_file.h"
|
2010-09-01 08:50:35 +02:00
|
|
|
#include "dialog_progress.h"
|
|
|
|
#include "frame_main.h"
|
|
|
|
#include "main.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "utils.h"
|
2010-08-02 09:21:02 +02:00
|
|
|
#include "video_frame.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/dispatch.h>
|
|
|
|
#include <libaegisub/log.h>
|
2013-07-03 04:49:45 +02:00
|
|
|
#include <libaegisub/util.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
#include <boost/gil/gil_all.hpp>
|
2013-04-30 05:18:29 +02:00
|
|
|
#include <boost/range/algorithm_ext/push_back.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
std::unique_ptr<agi::dispatch::Queue> cache_queue;
|
|
|
|
ASS_Library *library;
|
|
|
|
|
|
|
|
void msg_callback(int level, const char *fmt, va_list args, void *) {
|
2010-10-11 22:48:29 +02:00
|
|
|
if (level >= 7) return;
|
2010-10-08 08:06:50 +02:00
|
|
|
char buf[1024];
|
2010-08-10 07:50:14 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
vsprintf_s(buf, sizeof(buf), fmt, args);
|
|
|
|
#else
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
#endif
|
2010-06-01 10:21:30 +02:00
|
|
|
|
2010-05-24 05:41:06 +02:00
|
|
|
if (level < 2) // warning/error
|
2010-06-01 10:21:30 +02:00
|
|
|
LOG_I("subtitle/provider/libass") << buf;
|
2010-10-11 22:48:29 +02:00
|
|
|
else // verbose
|
2010-06-01 10:21:30 +02:00
|
|
|
LOG_D("subtitle/provider/libass") << buf;
|
2010-05-24 05:41:06 +02:00
|
|
|
}
|
|
|
|
|
2012-06-12 05:13:49 +02:00
|
|
|
#ifdef __APPLE__
|
2013-10-22 03:10:21 +02:00
|
|
|
#define CONFIG_PATH (agi::util::GetBundleResourcesDirectory() + "/etc/fonts/fonts.conf").c_str()
|
2013-01-04 16:01:50 +01:00
|
|
|
#else
|
|
|
|
#define CONFIG_PATH nullptr
|
2009-07-11 19:28:27 +02:00
|
|
|
#endif
|
2013-01-04 16:01:50 +01:00
|
|
|
}
|
2009-07-11 19:28:27 +02:00
|
|
|
|
2013-12-12 01:29:48 +01:00
|
|
|
LibassSubtitlesProvider::LibassSubtitlesProvider(std::string) {
|
2013-01-04 16:01:50 +01:00
|
|
|
auto done = std::make_shared<bool>(false);
|
|
|
|
auto renderer = std::make_shared<ASS_Renderer*>(nullptr);
|
|
|
|
cache_queue->Async([=]{
|
|
|
|
auto ass_renderer = ass_renderer_init(library);
|
|
|
|
if (ass_renderer) {
|
|
|
|
ass_set_font_scale(ass_renderer, 1.);
|
|
|
|
ass_set_fonts(ass_renderer, nullptr, "Sans", 1, CONFIG_PATH, true);
|
|
|
|
}
|
|
|
|
*done = true;
|
|
|
|
*renderer = ass_renderer;
|
|
|
|
});
|
2010-10-17 04:36:28 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
DialogProgress progress(wxGetApp().frame, _("Updating font index"), _("This may take several minutes"));
|
2012-11-26 06:28:13 +01:00
|
|
|
progress.Run([=](agi::ProgressSink *ps) {
|
|
|
|
ps->SetIndeterminate();
|
2013-01-04 16:01:50 +01:00
|
|
|
while (!*done && !ps->IsCancelled())
|
2013-07-03 04:49:45 +02:00
|
|
|
agi::util::sleep_for(250);
|
2012-11-26 06:28:13 +01:00
|
|
|
});
|
2010-09-01 08:50:35 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
ass_renderer = *renderer;
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!ass_renderer) throw "ass_renderer_init failed";
|
2007-04-04 06:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LibassSubtitlesProvider::~LibassSubtitlesProvider() {
|
2010-10-20 02:28:28 +02:00
|
|
|
if (ass_track) ass_free_track(ass_track);
|
|
|
|
if (ass_renderer) ass_renderer_done(ass_renderer);
|
2007-04-04 06:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibassSubtitlesProvider::LoadSubtitles(AssFile *subs) {
|
|
|
|
std::vector<char> data;
|
2013-01-26 02:57:46 +01:00
|
|
|
data.clear();
|
|
|
|
data.reserve(0x4000);
|
|
|
|
|
2013-06-13 00:54:19 +02:00
|
|
|
AssEntryGroup group = AssEntryGroup::GROUP_MAX;
|
2013-01-26 02:57:46 +01:00
|
|
|
for (auto const& line : subs->Line) {
|
|
|
|
if (group != line.Group()) {
|
|
|
|
group = line.Group();
|
|
|
|
boost::push_back(data, line.GroupHeader() + "\r\n");
|
|
|
|
}
|
|
|
|
boost::push_back(data, line.GetEntryData() + "\r\n");
|
|
|
|
}
|
2007-04-04 06:12:38 +02:00
|
|
|
|
|
|
|
if (ass_track) ass_free_track(ass_track);
|
2013-01-04 16:01:50 +01:00
|
|
|
ass_track = ass_read_memory(library, &data[0], data.size(),(char *)"UTF-8");
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!ass_track) throw "libass failed to load subtitles.";
|
2007-04-04 06:12:38 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#define _r(c) ((c)>>24)
|
|
|
|
#define _g(c) (((c)>>16)&0xFF)
|
|
|
|
#define _b(c) (((c)>>8)&0xFF)
|
|
|
|
#define _a(c) ((c)&0xFF)
|
2007-04-04 06:12:38 +02:00
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
void LibassSubtitlesProvider::DrawSubtitles(VideoFrame &frame,double time) {
|
|
|
|
ass_set_frame_size(ass_renderer, frame.width, frame.height);
|
2007-04-04 06:12:38 +02:00
|
|
|
|
2012-11-13 17:51:01 +01:00
|
|
|
ASS_Image* img = ass_render_frame(ass_renderer, ass_track, int(time * 1000), nullptr);
|
2007-04-04 06:12:38 +02:00
|
|
|
|
|
|
|
// libass actually returns several alpha-masked monochrome images.
|
|
|
|
// Here, we loop through their linked list, get the colour of the current, and blend into the frame.
|
|
|
|
// This is repeated for all of them.
|
2013-01-24 17:16:23 +01:00
|
|
|
|
|
|
|
using namespace boost::gil;
|
2013-07-01 05:15:43 +02:00
|
|
|
auto dst = interleaved_view(frame.width, frame.height, (bgra8_pixel_t*)frame.data.data(), frame.width * 4);
|
2013-01-24 17:16:23 +01:00
|
|
|
if (frame.flipped)
|
|
|
|
dst = flipped_up_down_view(dst);
|
|
|
|
|
|
|
|
for (; img; img = img->next) {
|
2007-04-04 06:12:38 +02:00
|
|
|
unsigned int opacity = 255 - ((unsigned int)_a(img->color));
|
|
|
|
unsigned int r = (unsigned int)_r(img->color);
|
|
|
|
unsigned int g = (unsigned int)_g(img->color);
|
2013-01-04 16:01:50 +01:00
|
|
|
unsigned int b = (unsigned int)_b(img->color);
|
2007-04-04 06:12:38 +02:00
|
|
|
|
2013-01-24 17:16:23 +01:00
|
|
|
auto srcview = interleaved_view(img->w, img->h, (gray8_pixel_t*)img->bitmap, img->stride);
|
|
|
|
auto dstview = subimage_view(dst, img->dst_x, img->dst_y, img->w, img->h);
|
2011-10-01 20:35:25 +02:00
|
|
|
|
2013-01-24 17:16:23 +01:00
|
|
|
transform_pixels(dstview, srcview, dstview, [=](const bgra8_pixel_t frame, const gray8_pixel_t src) -> bgra8_pixel_t {
|
|
|
|
unsigned int k = ((unsigned)src) * opacity / 255;
|
|
|
|
unsigned int ck = 255 - k;
|
2007-04-04 06:12:38 +02:00
|
|
|
|
2013-01-24 17:16:23 +01:00
|
|
|
bgra8_pixel_t ret;
|
|
|
|
ret[0] = (k * b + ck * frame[0]) / 255;
|
|
|
|
ret[1] = (k * g + ck * frame[1]) / 255;
|
|
|
|
ret[2] = (k * r + ck * frame[2]) / 255;
|
|
|
|
ret[3] = 0;
|
|
|
|
return ret;
|
|
|
|
});
|
2007-04-04 06:12:38 +02:00
|
|
|
}
|
|
|
|
}
|
2007-12-31 07:46:22 +01:00
|
|
|
|
2010-09-01 08:50:35 +02:00
|
|
|
void LibassSubtitlesProvider::CacheFonts() {
|
2013-01-04 16:01:50 +01:00
|
|
|
// Initialize the cache worker thread
|
|
|
|
cache_queue = agi::dispatch::Create();
|
|
|
|
|
|
|
|
// Initialize libass
|
|
|
|
library = ass_library_init();
|
|
|
|
ass_set_message_cb(library, msg_callback, nullptr);
|
|
|
|
|
|
|
|
// Initialize a renderer to force fontconfig to update its cache
|
|
|
|
cache_queue->Async([]{
|
|
|
|
auto ass_renderer = ass_renderer_init(library);
|
|
|
|
ass_set_fonts(ass_renderer, nullptr, "Sans", 1, CONFIG_PATH, true);
|
|
|
|
ass_renderer_done(ass_renderer);
|
|
|
|
});
|
2010-09-01 08:50:35 +02:00
|
|
|
}
|