mirror of
https://github.com/mpv-player/mpv
synced 2024-12-26 17:12:36 +00:00
sub: always recreate ASS_Renderer on subtitle decoder reinit
This includes the case of switching ordered chapter boundaries. It will now be recreated on each timeline part switch. This shouldn't be much of a problem with modern libass. (Older libass versions use fontconfig for memory fonts, and will be very slow to reinitialize memory fonts.)
This commit is contained in:
parent
ce8524cb47
commit
8d4a179c14
@ -365,15 +365,6 @@ typedef struct MPContext {
|
||||
int last_chapter_seek;
|
||||
double last_chapter_pts;
|
||||
|
||||
/* Subtitle renderer. This is separate, because we want to keep fonts
|
||||
* loaded across ordered chapters, instead of reloading and rescanning
|
||||
* them on each transition. (Both of these objects contain this state.)
|
||||
*/
|
||||
pthread_mutex_t ass_lock;
|
||||
struct ass_renderer *ass_renderer;
|
||||
struct ass_library *ass_library;
|
||||
struct mp_log *ass_log;
|
||||
|
||||
int last_dvb_step;
|
||||
|
||||
bool paused;
|
||||
@ -525,7 +516,6 @@ void uninit_sub(struct MPContext *mpctx, int order);
|
||||
void uninit_sub_all(struct MPContext *mpctx);
|
||||
void update_osd_msg(struct MPContext *mpctx);
|
||||
void update_subtitles(struct MPContext *mpctx);
|
||||
void uninit_sub_renderer(struct MPContext *mpctx);
|
||||
|
||||
// video.c
|
||||
void reset_video_state(struct MPContext *mpctx);
|
||||
|
@ -1254,7 +1254,6 @@ terminate_playback:
|
||||
uninit_audio_chain(mpctx);
|
||||
uninit_video_chain(mpctx);
|
||||
uninit_sub_all(mpctx);
|
||||
uninit_sub_renderer(mpctx);
|
||||
uninit_demuxer(mpctx);
|
||||
uninit_stream(mpctx);
|
||||
if (!opts->gapless_audio && !mpctx->encode_lavc_ctx)
|
||||
|
@ -222,7 +222,6 @@ void mp_destroy(struct MPContext *mpctx)
|
||||
pthread_detach(pthread_self());
|
||||
|
||||
mp_msg_uninit(mpctx->global);
|
||||
pthread_mutex_destroy(&mpctx->ass_lock);
|
||||
talloc_free(mpctx);
|
||||
}
|
||||
|
||||
@ -330,8 +329,6 @@ struct MPContext *mp_create(void)
|
||||
.playback_abort = mp_cancel_new(mpctx),
|
||||
};
|
||||
|
||||
pthread_mutex_init(&mpctx->ass_lock, NULL);
|
||||
|
||||
mpctx->global = talloc_zero(mpctx, struct mpv_global);
|
||||
|
||||
// Nothing must call mp_msg*() and related before this
|
||||
|
99
player/sub.c
99
player/sub.c
@ -30,7 +30,6 @@
|
||||
#include "common/global.h"
|
||||
|
||||
#include "stream/stream.h"
|
||||
#include "sub/ass_mp.h"
|
||||
#include "sub/dec_sub.h"
|
||||
#include "demux/demux.h"
|
||||
#include "video/mp_image.h"
|
||||
@ -39,98 +38,6 @@
|
||||
|
||||
#include "core.h"
|
||||
|
||||
#if HAVE_LIBASS
|
||||
|
||||
static const char *const font_mimetypes[] = {
|
||||
"application/x-truetype-font",
|
||||
"application/vnd.ms-opentype",
|
||||
"application/x-font-ttf",
|
||||
"application/x-font", // probably incorrect
|
||||
NULL
|
||||
};
|
||||
|
||||
static const char *const font_exts[] = {".ttf", ".ttc", ".otf", NULL};
|
||||
|
||||
static bool attachment_is_font(struct mp_log *log, struct demux_attachment *att)
|
||||
{
|
||||
if (!att->name || !att->type || !att->data || !att->data_size)
|
||||
return false;
|
||||
for (int n = 0; font_mimetypes[n]; n++) {
|
||||
if (strcmp(font_mimetypes[n], att->type) == 0)
|
||||
return true;
|
||||
}
|
||||
// fallback: match against file extension
|
||||
char *ext = strlen(att->name) > 4 ? att->name + strlen(att->name) - 4 : "";
|
||||
for (int n = 0; font_exts[n]; n++) {
|
||||
if (strcasecmp(ext, font_exts[n]) == 0) {
|
||||
mp_warn(log, "Loading font attachment '%s' with MIME type %s. "
|
||||
"Assuming this is a broken Matroska file, which was "
|
||||
"muxed without setting a correct font MIME type.\n",
|
||||
att->name, att->type);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void add_subtitle_fonts_from_sources(struct MPContext *mpctx)
|
||||
{
|
||||
if (mpctx->opts->ass_enabled) {
|
||||
for (int j = 0; j < mpctx->num_sources; j++) {
|
||||
struct demuxer *d = mpctx->sources[j];
|
||||
for (int i = 0; i < d->num_attachments; i++) {
|
||||
struct demux_attachment *att = d->attachments + i;
|
||||
if (mpctx->opts->use_embedded_fonts &&
|
||||
attachment_is_font(mpctx->log, att))
|
||||
{
|
||||
ass_add_font(mpctx->ass_library, att->name, att->data,
|
||||
att->data_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void init_sub_renderer(struct MPContext *mpctx)
|
||||
{
|
||||
struct MPOpts *opts = mpctx->opts;
|
||||
|
||||
if (mpctx->ass_renderer)
|
||||
return;
|
||||
|
||||
if (!mpctx->ass_log)
|
||||
mpctx->ass_log = mp_log_new(mpctx, mpctx->global->log, "!libass");
|
||||
|
||||
mpctx->ass_library = mp_ass_init(mpctx->global, mpctx->ass_log);
|
||||
|
||||
add_subtitle_fonts_from_sources(mpctx);
|
||||
|
||||
if (opts->ass_style_override)
|
||||
ass_set_style_overrides(mpctx->ass_library, opts->ass_force_style_list);
|
||||
|
||||
mpctx->ass_renderer = ass_renderer_init(mpctx->ass_library);
|
||||
|
||||
mp_ass_configure_fonts(mpctx->ass_renderer, opts->sub_text_style,
|
||||
mpctx->global, mpctx->ass_log);
|
||||
}
|
||||
|
||||
void uninit_sub_renderer(struct MPContext *mpctx)
|
||||
{
|
||||
if (mpctx->ass_renderer)
|
||||
ass_renderer_done(mpctx->ass_renderer);
|
||||
mpctx->ass_renderer = NULL;
|
||||
if (mpctx->ass_library)
|
||||
ass_library_done(mpctx->ass_library);
|
||||
mpctx->ass_library = NULL;
|
||||
}
|
||||
|
||||
#else /* HAVE_LIBASS */
|
||||
|
||||
static void init_sub_renderer(struct MPContext *mpctx) {}
|
||||
void uninit_sub_renderer(struct MPContext *mpctx) {}
|
||||
|
||||
#endif
|
||||
|
||||
static void reset_subtitles(struct MPContext *mpctx, int order)
|
||||
{
|
||||
if (mpctx->d_sub[order])
|
||||
@ -248,12 +155,8 @@ static void reinit_subdec(struct MPContext *mpctx, struct track *track)
|
||||
mpctx->d_video ? mpctx->d_video->header->video : NULL;
|
||||
float fps = sh_video ? sh_video->fps : 25;
|
||||
|
||||
init_sub_renderer(mpctx);
|
||||
|
||||
sub_set_video_fps(dec_sub, fps);
|
||||
sub_set_ass_renderer(dec_sub, mpctx->ass_library, mpctx->ass_renderer,
|
||||
&mpctx->ass_lock);
|
||||
sub_init_from_sh(dec_sub, track->stream);
|
||||
sub_init(dec_sub, track->demuxer, track->stream);
|
||||
|
||||
// Don't do this if the file has video/audio streams. Don't do it even
|
||||
// if it has only sub streams, because reading packets will change the
|
||||
|
@ -80,6 +80,7 @@ struct dec_sub *sub_create(struct mpv_global *global)
|
||||
sub->log = mp_log_new(sub, global->log, "sub");
|
||||
sub->opts = global->opts;
|
||||
sub->init_sd.opts = sub->opts;
|
||||
sub->init_sd.global = global;
|
||||
|
||||
mpthread_mutex_init_recursive(&sub->lock);
|
||||
|
||||
@ -119,17 +120,6 @@ void sub_set_video_fps(struct dec_sub *sub, double fps)
|
||||
pthread_mutex_unlock(&sub->lock);
|
||||
}
|
||||
|
||||
void sub_set_ass_renderer(struct dec_sub *sub, struct ass_library *ass_library,
|
||||
struct ass_renderer *ass_renderer,
|
||||
pthread_mutex_t *ass_lock)
|
||||
{
|
||||
pthread_mutex_lock(&sub->lock);
|
||||
sub->init_sd.ass_library = ass_library;
|
||||
sub->init_sd.ass_renderer = ass_renderer;
|
||||
sub->init_sd.ass_lock = ass_lock;
|
||||
pthread_mutex_unlock(&sub->lock);
|
||||
}
|
||||
|
||||
static int sub_init_decoder(struct dec_sub *sub, struct sd *sd)
|
||||
{
|
||||
sd->driver = NULL;
|
||||
@ -150,7 +140,7 @@ static int sub_init_decoder(struct dec_sub *sub, struct sd *sd)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sub_init_from_sh(struct dec_sub *sub, struct sh_stream *sh)
|
||||
void sub_init(struct dec_sub *sub, struct demuxer *demuxer, struct sh_stream *sh)
|
||||
{
|
||||
assert(!sub->sd);
|
||||
assert(sh && sh->sub);
|
||||
@ -160,6 +150,7 @@ void sub_init_from_sh(struct dec_sub *sub, struct sh_stream *sh)
|
||||
sub->sh = sh;
|
||||
|
||||
struct sd init_sd = sub->init_sd;
|
||||
init_sd.demuxer = demuxer;
|
||||
init_sd.sh = sh;
|
||||
|
||||
struct sd *sd = talloc(NULL, struct sd);
|
||||
|
@ -3,10 +3,10 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "osd.h"
|
||||
|
||||
struct demuxer;
|
||||
struct sh_stream;
|
||||
struct ass_track;
|
||||
struct mpv_global;
|
||||
@ -30,10 +30,7 @@ void sub_lock(struct dec_sub *sub);
|
||||
void sub_unlock(struct dec_sub *sub);
|
||||
|
||||
void sub_set_video_fps(struct dec_sub *sub, double fps);
|
||||
void sub_set_ass_renderer(struct dec_sub *sub, struct ass_library *ass_library,
|
||||
struct ass_renderer *ass_renderer,
|
||||
pthread_mutex_t *ass_lock);
|
||||
void sub_init_from_sh(struct dec_sub *sub, struct sh_stream *sh);
|
||||
void sub_init(struct dec_sub *sub, struct demuxer *demuxer, struct sh_stream *sh);
|
||||
|
||||
bool sub_is_initialized(struct dec_sub *sub);
|
||||
|
||||
|
7
sub/sd.h
7
sub/sd.h
@ -10,19 +10,16 @@
|
||||
#define SUB_GAP_KEEP 0.4
|
||||
|
||||
struct sd {
|
||||
struct mpv_global *global;
|
||||
struct mp_log *log;
|
||||
struct MPOpts *opts;
|
||||
|
||||
const struct sd_functions *driver;
|
||||
void *priv;
|
||||
|
||||
struct demuxer *demuxer;
|
||||
struct sh_stream *sh;
|
||||
double video_fps;
|
||||
|
||||
// Shared renderer for ASS - done to avoid reloading embedded fonts.
|
||||
struct ass_library *ass_library;
|
||||
struct ass_renderer *ass_renderer;
|
||||
pthread_mutex_t *ass_lock;
|
||||
};
|
||||
|
||||
struct sd_functions {
|
||||
|
83
sub/sd_ass.c
83
sub/sd_ass.c
@ -29,7 +29,7 @@
|
||||
#include "options/options.h"
|
||||
#include "common/common.h"
|
||||
#include "common/msg.h"
|
||||
#include "demux/stheader.h"
|
||||
#include "demux/demux.h"
|
||||
#include "video/csputils.h"
|
||||
#include "video/mp_image.h"
|
||||
#include "dec_sub.h"
|
||||
@ -37,6 +37,8 @@
|
||||
#include "sd.h"
|
||||
|
||||
struct sd_ass_priv {
|
||||
struct ass_library *ass_library;
|
||||
struct ass_renderer *ass_renderer;
|
||||
struct ass_track *ass_track;
|
||||
struct ass_track *shadow_track; // for --sub-ass=no rendering
|
||||
bool is_converted;
|
||||
@ -78,6 +80,51 @@ static void mp_ass_add_default_styles(ASS_Track *track, struct MPOpts *opts)
|
||||
ass_process_force_style(track);
|
||||
}
|
||||
|
||||
static const char *const font_mimetypes[] = {
|
||||
"application/x-truetype-font",
|
||||
"application/vnd.ms-opentype",
|
||||
"application/x-font-ttf",
|
||||
"application/x-font", // probably incorrect
|
||||
NULL
|
||||
};
|
||||
|
||||
static const char *const font_exts[] = {".ttf", ".ttc", ".otf", NULL};
|
||||
|
||||
static bool attachment_is_font(struct mp_log *log, struct demux_attachment *f)
|
||||
{
|
||||
if (!f->name || !f->type || !f->data || !f->data_size)
|
||||
return false;
|
||||
for (int n = 0; font_mimetypes[n]; n++) {
|
||||
if (strcmp(font_mimetypes[n], f->type) == 0)
|
||||
return true;
|
||||
}
|
||||
// fallback: match against file extension
|
||||
char *ext = strlen(f->name) > 4 ? f->name + strlen(f->name) - 4 : "";
|
||||
for (int n = 0; font_exts[n]; n++) {
|
||||
if (strcasecmp(ext, font_exts[n]) == 0) {
|
||||
mp_warn(log, "Loading font attachment '%s' with MIME type %s. "
|
||||
"Assuming this is a broken Matroska file, which was "
|
||||
"muxed without setting a correct font MIME type.\n",
|
||||
f->name, f->type);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void add_subtitle_fonts(struct sd *sd)
|
||||
{
|
||||
struct sd_ass_priv *ctx = sd->priv;
|
||||
struct MPOpts *opts = sd->opts;
|
||||
if (!opts->ass_enabled || !sd->demuxer)
|
||||
return;
|
||||
for (int i = 0; i < sd->demuxer->num_attachments; i++) {
|
||||
struct demux_attachment *f = &sd->demuxer->attachments[i];
|
||||
if (opts->use_embedded_fonts && attachment_is_font(sd->log, f))
|
||||
ass_add_font(ctx->ass_library, f->name, f->data, f->data_size);
|
||||
}
|
||||
}
|
||||
|
||||
static bool supports_format(const char *format)
|
||||
{
|
||||
return (format && strcmp(format, "ass") == 0) ||
|
||||
@ -87,9 +134,6 @@ static bool supports_format(const char *format)
|
||||
static int init(struct sd *sd)
|
||||
{
|
||||
struct MPOpts *opts = sd->opts;
|
||||
if (!sd->ass_library || !sd->ass_renderer || !sd->ass_lock)
|
||||
return -1;
|
||||
|
||||
struct sd_ass_priv *ctx = talloc_zero(sd, struct sd_ass_priv);
|
||||
sd->priv = ctx;
|
||||
|
||||
@ -106,13 +150,23 @@ static int init(struct sd *sd)
|
||||
extradata_size = extradata ? strlen(extradata) : 0;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(sd->ass_lock);
|
||||
ctx->ass_library = mp_ass_init(sd->global, sd->log);
|
||||
|
||||
ctx->ass_track = ass_new_track(sd->ass_library);
|
||||
add_subtitle_fonts(sd);
|
||||
|
||||
if (opts->ass_style_override)
|
||||
ass_set_style_overrides(ctx->ass_library, opts->ass_force_style_list);
|
||||
|
||||
ctx->ass_renderer = ass_renderer_init(ctx->ass_library);
|
||||
|
||||
mp_ass_configure_fonts(ctx->ass_renderer, opts->sub_text_style,
|
||||
sd->global, sd->log);
|
||||
|
||||
ctx->ass_track = ass_new_track(ctx->ass_library);
|
||||
if (!ctx->is_converted)
|
||||
ctx->ass_track->track_type = TRACK_TYPE_ASS;
|
||||
|
||||
ctx->shadow_track = ass_new_track(sd->ass_library);
|
||||
ctx->shadow_track = ass_new_track(ctx->ass_library);
|
||||
ctx->shadow_track->PlayResX = 384;
|
||||
ctx->shadow_track->PlayResY = 288;
|
||||
mp_ass_add_default_styles(ctx->shadow_track, opts);
|
||||
@ -122,8 +176,6 @@ static int init(struct sd *sd)
|
||||
|
||||
mp_ass_add_default_styles(ctx->ass_track, opts);
|
||||
|
||||
pthread_mutex_unlock(sd->ass_lock);
|
||||
|
||||
ctx->sub_speed = 1.0;
|
||||
|
||||
if (sd->video_fps && sd->sh && sd->sh->sub->frame_based > 0) {
|
||||
@ -186,7 +238,8 @@ static void configure_ass(struct sd *sd, struct mp_osd_res *dim,
|
||||
bool converted, ASS_Track *track)
|
||||
{
|
||||
struct MPOpts *opts = sd->opts;
|
||||
ASS_Renderer *priv = sd->ass_renderer;
|
||||
struct sd_ass_priv *ctx = sd->priv;
|
||||
ASS_Renderer *priv = ctx->ass_renderer;
|
||||
|
||||
ass_set_frame_size(priv, dim->w, dim->h);
|
||||
ass_set_margins(priv, dim->mt, dim->mb, dim->ml, dim->mr);
|
||||
@ -332,12 +385,10 @@ static void get_bitmaps(struct sd *sd, struct mp_osd_res dim, double pts,
|
||||
bool converted = ctx->is_converted || no_ass;
|
||||
ASS_Track *track = no_ass ? ctx->shadow_track : ctx->ass_track;
|
||||
|
||||
if (pts == MP_NOPTS_VALUE || !sd->ass_renderer)
|
||||
if (pts == MP_NOPTS_VALUE)
|
||||
return;
|
||||
|
||||
pthread_mutex_lock(sd->ass_lock);
|
||||
|
||||
ASS_Renderer *renderer = sd->ass_renderer;
|
||||
ASS_Renderer *renderer = ctx->ass_renderer;
|
||||
double scale = dim.display_par;
|
||||
if (!converted && (!opts->ass_style_override ||
|
||||
opts->ass_vsfilter_aspect_compat))
|
||||
@ -364,8 +415,6 @@ static void get_bitmaps(struct sd *sd, struct mp_osd_res dim, double pts,
|
||||
|
||||
if (!converted)
|
||||
mangle_colors(sd, res);
|
||||
|
||||
pthread_mutex_unlock(sd->ass_lock);
|
||||
}
|
||||
|
||||
struct buf {
|
||||
@ -528,6 +577,8 @@ static void uninit(struct sd *sd)
|
||||
if (ctx->converter)
|
||||
lavc_conv_uninit(ctx->converter);
|
||||
ass_free_track(ctx->ass_track);
|
||||
ass_renderer_done(ctx->ass_renderer);
|
||||
ass_library_done(ctx->ass_library);
|
||||
}
|
||||
|
||||
static int control(struct sd *sd, enum sd_ctrl cmd, void *arg)
|
||||
|
Loading…
Reference in New Issue
Block a user