Skip to content

Commit

Permalink
dynamic load libass. (use capi project)
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Nov 21, 2014
1 parent 8630801 commit fd229ac
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "contrib/libchardet"]
path = contrib/libchardet
url = https://github.com/wang-bin/libchardet.git
[submodule "contrib/capi"]
path = contrib/capi
url = https://github.com/wang-bin/capi.git
1 change: 1 addition & 0 deletions contrib/capi
Submodule capi added at 1b971d
14 changes: 13 additions & 1 deletion src/libQtAV.pro
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ LIBS += -lavcodec -lavformat -lavutil -lswscale
exists($$PROJECTROOT/contrib/libchardet/libchardet.pri) {
include($$PROJECTROOT/contrib/libchardet/libchardet.pri)
DEFINES += QTAV_HAVE_CHARDET=1 BUILD_CHARDET_STATIC
} else {
error("contrib/libchardet is missing. run 'git submodule update --init' first")
}
exists($$PROJECTROOT/contrib/capi/capi.pri) {
include($$PROJECTROOT/contrib/capi/capi.pri)
DEFINES += QTAV_HAVE_CAPI=1 BUILD_CAPI_STATIC
} else {
error("contrib/capi is missing. run 'git submodule update --init' first")
}
config_avfilter {
DEFINES += QTAV_HAVE_AVFILTER=1
Expand Down Expand Up @@ -261,9 +269,13 @@ config_openglwindow {
}
config_libass {
#link against libass instead of dynamic load
LIBS += -lass
#LIBS += -lass
#DEFINES += CAPI_LINK_ASS
SOURCES *= subtitle/SubtitleProcessorLibASS.cpp
HEADERS *= subtitle/ass_api.h
SOURCES *= subtitle/ass_api.cpp
}

SOURCES += \
AVCompat.cpp \
QtAV_Global.cpp \
Expand Down
16 changes: 15 additions & 1 deletion src/subtitle/SubtitleProcessorLibASS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@

#include <stdarg.h>
#include <string>
#ifdef CAPI_LINK_ASS
extern "C" {
#include <ass/ass.h>
}
#else
#include "ass_api.h"
#endif
#include "QtAV/private/SubtitleProcessor.h"
#include "QtAV/private/prepost.h"
#include "QtAV/Packet.h"
Expand All @@ -32,7 +36,7 @@ extern "C" {

namespace QtAV {

class SubtitleProcessorLibASS : public SubtitleProcessor
class SubtitleProcessorLibASS : public SubtitleProcessor, public ass::api
{
public:
SubtitleProcessorLibASS();
Expand Down Expand Up @@ -101,6 +105,8 @@ SubtitleProcessorLibASS::SubtitleProcessorLibASS()
, m_renderer(0)
, m_track(0)
{
if (!ass::api::loaded())
return;
m_ass = ass_library_init();
if (!m_ass) {
qWarning("ass_library_init failed!");
Expand All @@ -122,6 +128,8 @@ SubtitleProcessorLibASS::SubtitleProcessorLibASS()

SubtitleProcessorLibASS::~SubtitleProcessorLibASS()
{
if (!ass::api::loaded())
return;
if (m_track) {
ass_free_track(m_track);
m_track = 0;
Expand Down Expand Up @@ -161,6 +169,8 @@ QList<SubtitleFrame> SubtitleProcessorLibASS::frames() const

bool SubtitleProcessorLibASS::process(QIODevice *dev)
{
if (!ass::api::loaded())
return false;
if (m_track) {
ass_free_track(m_track);
m_track = 0;
Expand All @@ -184,6 +194,8 @@ bool SubtitleProcessorLibASS::process(QIODevice *dev)

bool SubtitleProcessorLibASS::process(const QString &path)
{
if (!ass::api::loaded())
return false;
if (m_track) {
ass_free_track(m_track);
m_track = 0;
Expand Down Expand Up @@ -228,6 +240,8 @@ QString SubtitleProcessorLibASS::getText(qreal pts) const

QImage SubtitleProcessorLibASS::getImage(qreal pts, QRect *boundingRect)
{
if (!ass::api::loaded())
return QImage();
if (!m_ass) {
qWarning("ass library not available");
return QImage();
Expand Down
158 changes: 158 additions & 0 deletions src/subtitle/ass_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/******************************************************************************
mkapi dynamic load code generation for capi template
Copyright (C) 2014 Wang Bin <[email protected]>
https://github.com/wang-bin/mkapi
https://github.com/wang-bin/capi
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
//#define DEBUG_RESOLVE
#define DEBUG_LOAD

#include "ass_api.h"
#include "capi.h"

namespace ass {
static const char* names[] = {
"ass",
NULL
};

#define CAPI_ASS_VERSION
#ifdef CAPI_ASS_VERSION
static const int versions[] = {
capi::NoVersion,
5,
4,
capi::EndVersion
};
CAPI_BEGIN_DLL_VER(names, versions)
#else
CAPI_BEGIN_DLL(names)
#endif
#ifndef CAPI_LINK_ASS
// CAPI_DEFINE_RESOLVER(argc, return_type, name, argv_no_name)
// mkapi code generation BEGIN
CAPI_DEFINE_RESOLVER(0, int, ass_library_version)
CAPI_DEFINE_RESOLVER(0, ASS_Library *, ass_library_init)
CAPI_DEFINE_RESOLVER(1, void, ass_library_done, ASS_Library *)
CAPI_DEFINE_RESOLVER(2, void, ass_set_fonts_dir, ASS_Library *, const char *)
CAPI_DEFINE_RESOLVER(2, void, ass_set_extract_fonts, ASS_Library *, int)
CAPI_DEFINE_RESOLVER(2, void, ass_set_style_overrides, ASS_Library *, char **)
CAPI_DEFINE_RESOLVER(1, void, ass_process_force_style, ASS_Track *)
CAPI_DEFINE_RESOLVER(3, void, ass_set_message_cb, ASS_Library *, void (*msg_cb)
(int level, const char *fmt, va_list args, void *data), void *)
CAPI_DEFINE_RESOLVER(1, ASS_Renderer *, ass_renderer_init, ASS_Library *)
CAPI_DEFINE_RESOLVER(1, void, ass_renderer_done, ASS_Renderer *)
CAPI_DEFINE_RESOLVER(3, void, ass_set_frame_size, ASS_Renderer *, int, int)
CAPI_DEFINE_RESOLVER(3, void, ass_set_storage_size, ASS_Renderer *, int, int)
CAPI_DEFINE_RESOLVER(2, void, ass_set_shaper, ASS_Renderer *, ASS_ShapingLevel)
CAPI_DEFINE_RESOLVER(5, void, ass_set_margins, ASS_Renderer *, int, int, int, int)
CAPI_DEFINE_RESOLVER(2, void, ass_set_use_margins, ASS_Renderer *, int)
CAPI_DEFINE_RESOLVER(2, void, ass_set_pixel_aspect, ASS_Renderer *, double)
CAPI_DEFINE_RESOLVER(3, void, ass_set_aspect_ratio, ASS_Renderer *, double, double)
CAPI_DEFINE_RESOLVER(2, void, ass_set_font_scale, ASS_Renderer *, double)
CAPI_DEFINE_RESOLVER(2, void, ass_set_hinting, ASS_Renderer *, ASS_Hinting)
CAPI_DEFINE_RESOLVER(2, void, ass_set_line_spacing, ASS_Renderer *, double)
CAPI_DEFINE_RESOLVER(2, void, ass_set_line_position, ASS_Renderer *, double)
CAPI_DEFINE_RESOLVER(6, void, ass_set_fonts, ASS_Renderer *, const char *, const char *, int, const char *, int)
CAPI_DEFINE_RESOLVER(2, void, ass_set_selective_style_override_enabled, ASS_Renderer *, int)
CAPI_DEFINE_RESOLVER(2, void, ass_set_selective_style_override, ASS_Renderer *, ASS_Style *)
CAPI_DEFINE_RESOLVER(1, int, ass_fonts_update, ASS_Renderer *)
CAPI_DEFINE_RESOLVER(3, void, ass_set_cache_limits, ASS_Renderer *, int, int)
CAPI_DEFINE_RESOLVER(4, ASS_Image *, ass_render_frame, ASS_Renderer *, ASS_Track *, long long, int *)
CAPI_DEFINE_RESOLVER(1, ASS_Track *, ass_new_track, ASS_Library *)
CAPI_DEFINE_RESOLVER(1, void, ass_free_track, ASS_Track *)
CAPI_DEFINE_RESOLVER(1, int, ass_alloc_style, ASS_Track *)
CAPI_DEFINE_RESOLVER(1, int, ass_alloc_event, ASS_Track *)
CAPI_DEFINE_RESOLVER(2, void, ass_free_style, ASS_Track *, int)
CAPI_DEFINE_RESOLVER(2, void, ass_free_event, ASS_Track *, int)
CAPI_DEFINE_RESOLVER(3, void, ass_process_data, ASS_Track *, char *, int)
CAPI_DEFINE_RESOLVER(3, void, ass_process_codec_private, ASS_Track *, char *, int)
CAPI_DEFINE_RESOLVER(5, void, ass_process_chunk, ASS_Track *, char *, int, long long, long long)
CAPI_DEFINE_RESOLVER(1, void, ass_flush_events, ASS_Track *)
CAPI_DEFINE_RESOLVER(3, ASS_Track *, ass_read_file, ASS_Library *, char *, char *)
CAPI_DEFINE_RESOLVER(4, ASS_Track *, ass_read_memory, ASS_Library *, char *, size_t, char *)
CAPI_DEFINE_RESOLVER(3, int, ass_read_styles, ASS_Track *, char *, char *)
CAPI_DEFINE_RESOLVER(4, void, ass_add_font, ASS_Library *, char *, char *, int)
CAPI_DEFINE_RESOLVER(1, void, ass_clear_fonts, ASS_Library *)
CAPI_DEFINE_RESOLVER(3, long long, ass_step_sub, ASS_Track *, long long, int)
// mkapi code generation END
#endif //CAPI_LINK_ASS
CAPI_END_DLL()

api::api() : dll(new api_dll()) {
qDebug("capi::version: %s build %s", capi::version::name, capi::version::build());
}
api::~api() { delete dll;}
bool api::loaded() const {
#ifdef CAPI_LINK_ASS
return true;
#else
return dll->isLoaded();
#endif //CAPI_LINK_ASS
}

#ifndef CAPI_LINK_ASS
// CAPI_DEFINE(argc, return_type, name, argv_no_name)
typedef void (*msg_cb)
(int level, const char *fmt, va_list args, void *data);
// mkapi code generation BEGIN
CAPI_DEFINE(0, int, ass_library_version)
CAPI_DEFINE(0, ASS_Library *, ass_library_init)
CAPI_DEFINE(1, void, ass_library_done, ASS_Library *)
CAPI_DEFINE(2, void, ass_set_fonts_dir, ASS_Library *, const char *)
CAPI_DEFINE(2, void, ass_set_extract_fonts, ASS_Library *, int)
CAPI_DEFINE(2, void, ass_set_style_overrides, ASS_Library *, char **)
CAPI_DEFINE(1, void, ass_process_force_style, ASS_Track *)
CAPI_DEFINE(3, void, ass_set_message_cb, ASS_Library *, msg_cb, void *)
CAPI_DEFINE(1, ASS_Renderer *, ass_renderer_init, ASS_Library *)
CAPI_DEFINE(1, void, ass_renderer_done, ASS_Renderer *)
CAPI_DEFINE(3, void, ass_set_frame_size, ASS_Renderer *, int, int)
CAPI_DEFINE(3, void, ass_set_storage_size, ASS_Renderer *, int, int)
CAPI_DEFINE(2, void, ass_set_shaper, ASS_Renderer *, ASS_ShapingLevel)
CAPI_DEFINE(5, void, ass_set_margins, ASS_Renderer *, int, int, int, int)
CAPI_DEFINE(2, void, ass_set_use_margins, ASS_Renderer *, int)
CAPI_DEFINE(2, void, ass_set_pixel_aspect, ASS_Renderer *, double)
CAPI_DEFINE(3, void, ass_set_aspect_ratio, ASS_Renderer *, double, double)
CAPI_DEFINE(2, void, ass_set_font_scale, ASS_Renderer *, double)
CAPI_DEFINE(2, void, ass_set_hinting, ASS_Renderer *, ASS_Hinting)
CAPI_DEFINE(2, void, ass_set_line_spacing, ASS_Renderer *, double)
CAPI_DEFINE(2, void, ass_set_line_position, ASS_Renderer *, double)
CAPI_DEFINE(6, void, ass_set_fonts, ASS_Renderer *, const char *, const char *, int, const char *, int)
CAPI_DEFINE(2, void, ass_set_selective_style_override_enabled, ASS_Renderer *, int)
CAPI_DEFINE(2, void, ass_set_selective_style_override, ASS_Renderer *, ASS_Style *)
CAPI_DEFINE(1, int, ass_fonts_update, ASS_Renderer *)
CAPI_DEFINE(3, void, ass_set_cache_limits, ASS_Renderer *, int, int)
CAPI_DEFINE(4, ASS_Image *, ass_render_frame, ASS_Renderer *, ASS_Track *, long long, int *)
CAPI_DEFINE(1, ASS_Track *, ass_new_track, ASS_Library *)
CAPI_DEFINE(1, void, ass_free_track, ASS_Track *)
CAPI_DEFINE(1, int, ass_alloc_style, ASS_Track *)
CAPI_DEFINE(1, int, ass_alloc_event, ASS_Track *)
CAPI_DEFINE(2, void, ass_free_style, ASS_Track *, int)
CAPI_DEFINE(2, void, ass_free_event, ASS_Track *, int)
CAPI_DEFINE(3, void, ass_process_data, ASS_Track *, char *, int)
CAPI_DEFINE(3, void, ass_process_codec_private, ASS_Track *, char *, int)
CAPI_DEFINE(5, void, ass_process_chunk, ASS_Track *, char *, int, long long, long long)
CAPI_DEFINE(1, void, ass_flush_events, ASS_Track *)
CAPI_DEFINE(3, ASS_Track *, ass_read_file, ASS_Library *, char *, char *)
CAPI_DEFINE(4, ASS_Track *, ass_read_memory, ASS_Library *, char *, size_t, char *)
CAPI_DEFINE(3, int, ass_read_styles, ASS_Track *, char *, char *)
CAPI_DEFINE(4, void, ass_add_font, ASS_Library *, char *, char *, int)
CAPI_DEFINE(1, void, ass_clear_fonts, ASS_Library *)
CAPI_DEFINE(3, long long, ass_step_sub, ASS_Track *, long long, int)
// mkapi code generation END
#endif //CAPI_LINK_ASS
} //namespace ass
92 changes: 92 additions & 0 deletions src/subtitle/ass_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/******************************************************************************
mkapi dynamic load code generation for capi template
Copyright (C) 2014 Wang Bin <[email protected]>
https://github.com/wang-bin/mkapi
https://github.com/wang-bin/capi
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/

#ifndef ASS_API_H
#define ASS_API_H

// no need to include the C header if only functions declared there
extern "C" {
#include "ass/ass.h"
}

namespace ass {
class api_dll;
class api
{
public:
api();
~api();
bool loaded() const;
#ifndef CAPI_LINK_ASS
// mkapi code generation BEGIN
int ass_library_version();
ASS_Library * ass_library_init();
void ass_library_done(ASS_Library * priv);
void ass_set_fonts_dir(ASS_Library * priv, const char * fonts_dir);
void ass_set_extract_fonts(ASS_Library * priv, int extract);
void ass_set_style_overrides(ASS_Library * priv, char ** list);
void ass_process_force_style(ASS_Track * track);
void ass_set_message_cb(ASS_Library * priv, void (*msg_cb)
(int level, const char *fmt, va_list args, void *data), void * data);
ASS_Renderer * ass_renderer_init(ASS_Library *);
void ass_renderer_done(ASS_Renderer * priv);
void ass_set_frame_size(ASS_Renderer * priv, int w, int h);
void ass_set_storage_size(ASS_Renderer * priv, int w, int h);
void ass_set_shaper(ASS_Renderer * priv, ASS_ShapingLevel level);
void ass_set_margins(ASS_Renderer * priv, int t, int b, int l, int r);
void ass_set_use_margins(ASS_Renderer * priv, int use);
void ass_set_pixel_aspect(ASS_Renderer * priv, double par);
void ass_set_aspect_ratio(ASS_Renderer * priv, double dar, double sar);
void ass_set_font_scale(ASS_Renderer * priv, double font_scale);
void ass_set_hinting(ASS_Renderer * priv, ASS_Hinting ht);
void ass_set_line_spacing(ASS_Renderer * priv, double line_spacing);
void ass_set_line_position(ASS_Renderer * priv, double line_position);
void ass_set_fonts(ASS_Renderer * priv, const char * default_font, const char * default_family, int fc, const char * config, int update);
void ass_set_selective_style_override_enabled(ASS_Renderer * priv, int bits);
void ass_set_selective_style_override(ASS_Renderer * priv, ASS_Style * style);
int ass_fonts_update(ASS_Renderer * priv);
void ass_set_cache_limits(ASS_Renderer * priv, int glyph_max, int bitmap_max_size);
ASS_Image * ass_render_frame(ASS_Renderer * priv, ASS_Track * track, long long now, int * detect_change);
ASS_Track * ass_new_track(ASS_Library *);
void ass_free_track(ASS_Track * track);
int ass_alloc_style(ASS_Track * track);
int ass_alloc_event(ASS_Track * track);
void ass_free_style(ASS_Track * track, int sid);
void ass_free_event(ASS_Track * track, int eid);
void ass_process_data(ASS_Track * track, char * data, int size);
void ass_process_codec_private(ASS_Track * track, char * data, int size);
void ass_process_chunk(ASS_Track * track, char * data, int size, long long timecode, long long duration);
void ass_flush_events(ASS_Track * track);
ASS_Track * ass_read_file(ASS_Library * library, char * fname, char * codepage);
ASS_Track * ass_read_memory(ASS_Library * library, char * buf, size_t bufsize, char * codepage);
int ass_read_styles(ASS_Track * track, char * fname, char * codepage);
void ass_add_font(ASS_Library * library, char * name, char * data, int data_size);
void ass_clear_fonts(ASS_Library * library);
long long ass_step_sub(ASS_Track * track, long long now, int movement);
// mkapi code generation END
#endif //CAPI_LINK_ASS
private:
api_dll *dll;
};
} //namespace ass


#endif // ASS_API_H

0 comments on commit fd229ac

Please sign in to comment.