Skip to content

Commit

Permalink
options: add --secondary-sub-ass-override
Browse files Browse the repository at this point in the history
Default: strip. preserve the old behavior
  • Loading branch information
dyphire committed Dec 17, 2023
1 parent 1112de2 commit 9f5e2bd
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
6 changes: 6 additions & 0 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,12 @@ Subtitles
This also controls some bitmap subtitle overrides, as well as HTML tags in
formats like SRT, despite the name of the option.

``--secondary-sub-ass-override=<yes|no|force|scale|strip>``
Control whether user secondary substyle overrides should be applied. This
works exactly like ``--sub-ass-override``.

Default: strip.

``--sub-ass-force-margins``
Enables placing toptitles and subtitles in black borders when they are
available, if the subtitles are in the ASS format.
Expand Down
13 changes: 9 additions & 4 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,6 @@ const struct m_sub_options mp_subtitle_sub_opts = {
{"sub-ass-shaper", OPT_CHOICE(ass_shaper,
{"simple", 0}, {"complex", 1})},
{"sub-ass-justify", OPT_BOOL(ass_justify)},
{"sub-ass-override", OPT_CHOICE(ass_style_override,
{"no", 0}, {"yes", 1}, {"force", 3}, {"scale", 4}, {"strip", 5}),
.flags = UPDATE_SUB_HARD},
{"sub-scale-by-window", OPT_BOOL(sub_scale_by_window)},
{"sub-scale-with-window", OPT_BOOL(sub_scale_with_window)},
{"sub-ass-scale-with-window", OPT_BOOL(ass_scale_with_window)},
Expand All @@ -338,7 +335,6 @@ const struct m_sub_options mp_subtitle_sub_opts = {
.ass_vsfilter_aspect_compat = true,
.ass_vsfilter_color_compat = 1,
.ass_vsfilter_blur_compat = true,
.ass_style_override = 1,
.ass_shaper = 1,
.use_embedded_fonts = true,
},
Expand All @@ -356,13 +352,21 @@ const struct m_sub_options mp_subtitle_shared_sub_opts = {
{"secondary-sub-pos", OPT_FLOAT(sub_pos[1]), M_RANGE(0.0, 150.0)},
{"sub-visibility", OPT_BOOL(sub_visibility[0])},
{"secondary-sub-visibility", OPT_BOOL(sub_visibility[1])},
{"sub-ass-override", OPT_CHOICE(ass_style_override[0],
{"no", 0}, {"yes", 1}, {"force", 3}, {"scale", 4}, {"strip", 5}),
.flags = UPDATE_SUB_HARD},
{"secondary-sub-ass-override", OPT_CHOICE(ass_style_override[1],
{"no", 0}, {"yes", 1}, {"force", 3}, {"scale", 4}, {"strip", 5}),
.flags = UPDATE_SUB_HARD},
{0}
},
.size = sizeof(OPT_BASE_STRUCT),
.defaults = &(OPT_BASE_STRUCT){
.sub_visibility[0] = true,
.sub_visibility[1] = true,
.sub_pos[0] = 100,
.ass_style_override[0] = 1,
.ass_style_override[1] = 5,
},
.change_flags = UPDATE_OSD,
};
Expand Down Expand Up @@ -1090,6 +1094,7 @@ static const struct MPOpts mp_default_opts = {
"sub-ass-force-margins",
"sub-ass-vsfilter-aspect-compat",
"sub-ass-override",
"secondary-sub-ass-override",
"secondary-sub-visibility",
"ab-loop-a",
"ab-loop-b",
Expand Down
2 changes: 1 addition & 1 deletion options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ struct mp_subtitle_opts {
bool use_embedded_fonts;
char **ass_style_override_list;
char *ass_styles_file;
int ass_style_override;
int ass_hinting;
int ass_shaper;
bool ass_justify;
Expand All @@ -121,6 +120,7 @@ struct mp_subtitle_shared_opts {
float sub_delay[2];
float sub_pos[2];
bool sub_visibility[2];
int ass_style_override[2];
};

struct mp_sub_filter_opts {
Expand Down
1 change: 1 addition & 0 deletions player/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -4257,6 +4257,7 @@ static const struct property_osd_display {
{"sub-scale", "Sub Scale"},
{"sub-ass-vsfilter-aspect-compat", "Subtitle VSFilter aspect compat"},
{"sub-ass-override", "ASS subtitle style override"},
{"secondary-sub-ass-override", "Secondary sub ASS subtitle style override"},
{"vf", "Video filters", .msg = "Video filters:\n${vf}"},
{"af", "Audio filters", .msg = "Audio filters:\n${af}"},
{"ab-loop-a", "A-B loop start"},
Expand Down
3 changes: 2 additions & 1 deletion sub/dec_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ static struct sd *init_decoder(struct dec_sub *sub)
.opts = sub->opts,
.shared_opts = sub->shared_opts,
.driver = driver,
.order = sub->order,
.attachments = sub->attachments,
.codec = sub->codec,
.preload_ok = true,
Expand Down Expand Up @@ -368,7 +369,7 @@ struct sub_bitmaps *sub_get_bitmaps(struct dec_sub *sub, struct mp_osd_res dim,

if (!(sub->end != MP_NOPTS_VALUE && pts >= sub->end) &&
sub->sd->driver->get_bitmaps)
res = sub->sd->driver->get_bitmaps(sub->sd, dim, format, pts, sub->order);
res = sub->sd->driver->get_bitmaps(sub->sd, dim, format, pts);

mp_mutex_unlock(&sub->lock);
return res;
Expand Down
3 changes: 2 additions & 1 deletion sub/sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct sd {

const struct sd_functions *driver;
void *priv;
int order;

struct attachment_list *attachments;
struct mp_codec_params *codec;
Expand All @@ -42,7 +43,7 @@ struct sd_functions {
int (*control)(struct sd *sd, enum sd_ctrl cmd, void *arg);

struct sub_bitmaps *(*get_bitmaps)(struct sd *sd, struct mp_osd_res dim,
int format, double pts, int order);
int format, double pts);
char *(*get_text)(struct sd *sd, double pts, enum sd_text_type type);
struct sd_times (*get_times)(struct sd *sd, double pts);
};
Expand Down
39 changes: 21 additions & 18 deletions sub/sd_ass.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ static const struct sd_filter_functions *const filters[] = {

// Add default styles, if the track does not have any styles yet.
// Apply style overrides if the user provides any.
static void mp_ass_add_default_styles(ASS_Track *track, struct mp_subtitle_opts *opts)
static void mp_ass_add_default_styles(ASS_Track *track, struct mp_subtitle_opts *opts,
struct mp_subtitle_shared_opts *shared_opts, int order)
{
if (opts->ass_styles_file && opts->ass_style_override)
if (opts->ass_styles_file && shared_opts->ass_style_override[order])
ass_read_styles(track, opts->ass_styles_file, NULL);

if (track->n_styles == 0) {
Expand All @@ -95,7 +96,7 @@ static void mp_ass_add_default_styles(ASS_Track *track, struct mp_subtitle_opts
mp_ass_set_style(style, track->PlayResY, opts->sub_style);
}

if (opts->ass_style_override)
if (shared_opts->ass_style_override[order])
ass_process_force_style(track);
}

Expand Down Expand Up @@ -206,13 +207,14 @@ static void assobjects_init(struct sd *sd)
{
struct sd_ass_priv *ctx = sd->priv;
struct mp_subtitle_opts *opts = sd->opts;
struct mp_subtitle_shared_opts *shared_opts = sd->shared_opts;

ctx->ass_library = mp_ass_init(sd->global, sd->opts->sub_style, sd->log);
ass_set_extract_fonts(ctx->ass_library, opts->use_embedded_fonts);

add_subtitle_fonts(sd);

if (opts->ass_style_override)
if (shared_opts->ass_style_override[sd->order])
ass_set_style_overrides(ctx->ass_library, opts->ass_style_override_list);

ctx->ass_track = ass_new_track(ctx->ass_library);
Expand All @@ -221,7 +223,7 @@ static void assobjects_init(struct sd *sd)
ctx->shadow_track = ass_new_track(ctx->ass_library);
ctx->shadow_track->PlayResX = MP_ASS_FONT_PLAYRESX;
ctx->shadow_track->PlayResY = MP_ASS_FONT_PLAYRESY;
mp_ass_add_default_styles(ctx->shadow_track, opts);
mp_ass_add_default_styles(ctx->shadow_track, opts, shared_opts, sd->order);

char *extradata = sd->codec->extradata;
int extradata_size = sd->codec->extradata_size;
Expand All @@ -232,7 +234,7 @@ static void assobjects_init(struct sd *sd)
if (extradata)
ass_process_codec_private(ctx->ass_track, extradata, extradata_size);

mp_ass_add_default_styles(ctx->ass_track, opts);
mp_ass_add_default_styles(ctx->ass_track, opts, shared_opts, sd->order);

#if LIBASS_VERSION >= 0x01302000
ass_set_check_readorder(ctx->ass_track, sd->opts->sub_clear_on_seek ? 0 : 1);
Expand Down Expand Up @@ -378,7 +380,7 @@ static void decode(struct sd *sd, struct demux_packet *packet)
}

static void configure_ass(struct sd *sd, struct mp_osd_res *dim,
bool converted, ASS_Track *track, int order)
bool converted, ASS_Track *track)
{
struct mp_subtitle_opts *opts = sd->opts;
struct mp_subtitle_shared_opts *shared_opts = sd->shared_opts;
Expand All @@ -397,7 +399,7 @@ static void configure_ass(struct sd *sd, struct mp_osd_res *dim,
bool set_scale_by_window = true;
bool total_override = false;
// With forced overrides, apply the --sub-* specific options
if (converted || opts->ass_style_override == 3) { // 'force'
if (converted || shared_opts->ass_style_override[sd->order] == 3) { // 'force'
set_scale_with_window = opts->sub_scale_with_window;
set_use_margins = opts->sub_use_margins;
set_scale_by_window = opts->sub_scale_by_window;
Expand All @@ -406,8 +408,8 @@ static void configure_ass(struct sd *sd, struct mp_osd_res *dim,
set_scale_with_window = opts->ass_scale_with_window;
set_use_margins = opts->ass_use_margins;
}
if (converted || opts->ass_style_override) {
set_sub_pos = 100.0f - shared_opts->sub_pos[order];
if (converted || shared_opts->ass_style_override[sd->order]) {
set_sub_pos = 100.0f - shared_opts->sub_pos[sd->order];
set_line_spacing = opts->ass_line_spacing;
set_hinting = opts->ass_hinting;
set_font_scale = opts->sub_scale;
Expand All @@ -427,12 +429,12 @@ static void configure_ass(struct sd *sd, struct mp_osd_res *dim,
int set_force_flags = 0;
if (total_override)
set_force_flags |= ASS_OVERRIDE_BIT_STYLE | ASS_OVERRIDE_BIT_SELECTIVE_FONT_SCALE;
if (opts->ass_style_override == 4) // 'scale'
if (shared_opts->ass_style_override[sd->order] == 4) // 'scale'
set_force_flags |= ASS_OVERRIDE_BIT_SELECTIVE_FONT_SCALE;
if (converted)
set_force_flags |= ASS_OVERRIDE_BIT_ALIGNMENT;
#ifdef ASS_JUSTIFY_AUTO
if ((converted || opts->ass_style_override) && opts->ass_justify)
if ((converted || shared_opts->ass_style_override[sd->order]) && opts->ass_justify)
set_force_flags |= ASS_OVERRIDE_BIT_JUSTIFY;
#endif
ass_set_selective_style_override_enabled(priv, set_force_flags);
Expand Down Expand Up @@ -499,7 +501,7 @@ static long long find_timestamp(struct sd *sd, double pts)

long long ts = llrint(pts * 1000);

if (!sd->opts->sub_fix_timing || sd->opts->ass_style_override == 0)
if (!sd->opts->sub_fix_timing || sd->shared_opts->ass_style_override[sd->order] == 0)
return ts;

// Try to fix small gaps and overlaps.
Expand Down Expand Up @@ -557,11 +559,12 @@ static long long find_timestamp(struct sd *sd, double pts)
#undef END

static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res dim,
int format, double pts, int order)
int format, double pts)
{
struct sd_ass_priv *ctx = sd->priv;
struct mp_subtitle_opts *opts = sd->opts;
bool no_ass = !opts->ass_enabled || opts->ass_style_override == 5 || order == 1;
struct mp_subtitle_shared_opts *shared_opts = sd->shared_opts;
bool no_ass = !opts->ass_enabled || shared_opts->ass_style_override[sd->order] == 5;
bool converted = ctx->is_converted || no_ass;
ASS_Track *track = no_ass ? ctx->shadow_track : ctx->ass_track;
ASS_Renderer *renderer = ctx->ass_renderer;
Expand All @@ -581,7 +584,7 @@ static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res dim,
goto done;

double scale = dim.display_par;
if (!converted && (!opts->ass_style_override ||
if (!converted && (!shared_opts->ass_style_override[sd->order] ||
opts->ass_vsfilter_aspect_compat))
{
// Let's use the original video PAR for vsfilter compatibility:
Expand All @@ -590,11 +593,11 @@ static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res dim,
scale *= par;
}
if (!ctx->ass_configured || !osd_res_equals(old_osd, ctx->osd)) {
configure_ass(sd, &dim, converted, track, order);
configure_ass(sd, &dim, converted, track);
ctx->ass_configured = true;
}
ass_set_pixel_aspect(renderer, scale);
if (!converted && (!opts->ass_style_override ||
if (!converted && (!shared_opts->ass_style_override[sd->order] ||
opts->ass_vsfilter_blur_compat))
{
ass_set_storage_size(renderer, ctx->video_params.w, ctx->video_params.h);
Expand Down
6 changes: 3 additions & 3 deletions sub/sd_lavc.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ static struct sub *get_current(struct sd_lavc_priv *priv, double pts)
}

static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res d,
int format, double pts, int order)
int format, double pts)
{
struct sd_lavc_priv *priv = sd->priv;
struct mp_subtitle_opts *opts = sd->opts;
Expand Down Expand Up @@ -451,7 +451,7 @@ static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res d,
h = MPMAX(priv->video_params.h, current->src_h);
}

if (shared_opts->sub_pos[0] != 100.0f && opts->ass_style_override) {
if (shared_opts->sub_pos[0] != 100.0f && shared_opts->ass_style_override[0]) {
float offset = (100.0f - shared_opts->sub_pos[0]) / 100.0f * h;

for (int n = 0; n < res->num_parts; n++) {
Expand All @@ -470,7 +470,7 @@ static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res d,

osd_rescale_bitmaps(res, w, h, d, video_par);

if (opts->sub_scale != 1.0 && opts->ass_style_override) {
if (opts->sub_scale != 1.0 && shared_opts->ass_style_override[0]) {
for (int n = 0; n < res->num_parts; n++) {
struct sub_bitmap *sub = &res->parts[n];

Expand Down

0 comments on commit 9f5e2bd

Please sign in to comment.