Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

player: refactor secondary subtitle options and properties #13091

Merged
merged 2 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,14 @@ const struct m_sub_options mp_sub_filter_opts = {

const struct m_sub_options mp_subtitle_sub_opts = {
.opts = (const struct m_option[]){
{"sub-delay", OPT_FLOAT(sub_delay[0])},
{"secondary-sub-delay", OPT_FLOAT(sub_delay[1])},
{"sub-fps", OPT_FLOAT(sub_fps)},
{"sub-speed", OPT_FLOAT(sub_speed)},
{"sub-visibility", OPT_BOOL(sub_visibility)},
{"secondary-sub-visibility", OPT_BOOL(sec_sub_visibility)},
{"sub-forced-events-only", OPT_BOOL(sub_forced_events_only)},
{"stretch-dvd-subs", OPT_BOOL(stretch_dvd_subs)},
{"stretch-image-subs-to-screen", OPT_BOOL(stretch_image_subs)},
{"image-subs-video-resolution", OPT_BOOL(image_subs_video_res)},
{"sub-fix-timing", OPT_BOOL(sub_fix_timing)},
{"sub-stretch-durations", OPT_BOOL(sub_stretch_durations)},
{"sub-pos", OPT_FLOAT(sub_pos), M_RANGE(0.0, 150.0)},
{"secondary-sub-pos", OPT_FLOAT(sec_sub_pos), M_RANGE(0.0, 150.0)},
{"sub-gauss", OPT_FLOAT(sub_gauss), M_RANGE(0.0, 3.0)},
{"sub-gray", OPT_BOOL(sub_gray)},
{"sub-ass", OPT_BOOL(ass_enabled), .flags = UPDATE_SUB_HARD},
Expand Down Expand Up @@ -334,10 +328,6 @@ const struct m_sub_options mp_subtitle_sub_opts = {
},
.size = sizeof(OPT_BASE_STRUCT),
.defaults = &(OPT_BASE_STRUCT){
.sub_visibility = true,
.sec_sub_visibility = true,
.sub_pos = 100,
.sec_sub_pos = 0,
.sub_speed = 1.0,
.ass_enabled = true,
.sub_scale_by_window = true,
Expand All @@ -355,6 +345,28 @@ const struct m_sub_options mp_subtitle_sub_opts = {
.change_flags = UPDATE_OSD,
};

#undef OPT_BASE_STRUCT
#define OPT_BASE_STRUCT struct mp_subtitle_shared_opts

const struct m_sub_options mp_subtitle_shared_sub_opts = {
.opts = (const struct m_option[]){
{"sub-delay", OPT_FLOAT(sub_delay[0])},
{"secondary-sub-delay", OPT_FLOAT(sub_delay[1])},
{"sub-pos", OPT_FLOAT(sub_pos[0]), M_RANGE(0.0, 150.0)},
{"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])},
{0}
},
.size = sizeof(OPT_BASE_STRUCT),
.defaults = &(OPT_BASE_STRUCT){
.sub_visibility[0] = true,
.sub_visibility[1] = true,
.sub_pos[0] = 100,
},
.change_flags = UPDATE_OSD,
};

#undef OPT_BASE_STRUCT
#define OPT_BASE_STRUCT struct mp_osd_render_opts

Expand Down Expand Up @@ -669,6 +681,7 @@ static const m_option_t mp_opts[] = {
{"cover-art-whitelist", OPT_BOOL(coverart_whitelist)},

{"", OPT_SUBSTRUCT(subs_rend, mp_subtitle_sub_opts)},
{"", OPT_SUBSTRUCT(subs_shared, mp_subtitle_shared_sub_opts)},
{"", OPT_SUBSTRUCT(subs_filt, mp_sub_filter_opts)},
{"", OPT_SUBSTRUCT(osd_rend, mp_osd_render_sub_opts)},

Expand Down
14 changes: 9 additions & 5 deletions options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ typedef struct mp_vo_opts {

// Subtitle options needed by the subtitle decoders/renderers.
struct mp_subtitle_opts {
bool sub_visibility;
bool sec_sub_visibility;
float sub_pos;
float sec_sub_pos;
float sub_delay[2];
float sub_fps;
float sub_speed;
bool sub_forced_events_only;
Expand Down Expand Up @@ -121,6 +116,13 @@ struct mp_subtitle_opts {
bool sub_past_video_end;
};

// Options for both primary and secondary subs.
struct mp_subtitle_shared_opts {
float sub_delay[2];
float sub_pos[2];
bool sub_visibility[2];
};

struct mp_sub_filter_opts {
bool sub_filter_SDH;
bool sub_filter_SDH_harder;
Expand Down Expand Up @@ -199,6 +201,7 @@ typedef struct MPOpts {
bool cursor_autohide_fs;

struct mp_subtitle_opts *subs_rend;
struct mp_subtitle_shared_opts *subs_shared;
struct mp_sub_filter_opts *subs_filt;
struct mp_osd_render_opts *osd_rend;

Expand Down Expand Up @@ -398,6 +401,7 @@ extern const struct m_sub_options vo_sub_opts;
extern const struct m_sub_options cuda_conf;
extern const struct m_sub_options dvd_conf;
extern const struct m_sub_options mp_subtitle_sub_opts;
extern const struct m_sub_options mp_subtitle_shared_sub_opts;
extern const struct m_sub_options mp_sub_filter_opts;
extern const struct m_sub_options mp_osd_render_sub_opts;
extern const struct m_sub_options filter_conf;
Expand Down
55 changes: 18 additions & 37 deletions player/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -2878,7 +2878,7 @@ static int mp_property_sub_delay(void *ctx, struct m_property *prop,
int track_ind = *(int *)prop->priv;
switch (action) {
case M_PROPERTY_PRINT:
*(char **)arg = format_delay(opts->subs_rend->sub_delay[track_ind]);
*(char **)arg = format_delay(opts->subs_shared->sub_delay[track_ind]);
return M_PROPERTY_OK;
}
return mp_property_generic_option(mpctx, prop, action, arg);
Expand All @@ -2903,20 +2903,9 @@ static int mp_property_sub_pos(void *ctx, struct m_property *prop,
{
MPContext *mpctx = ctx;
struct MPOpts *opts = mpctx->opts;
int track_ind = *(int *)prop->priv;
if (action == M_PROPERTY_PRINT) {
*(char **)arg = talloc_asprintf(NULL, "%4.2f%%/100", opts->subs_rend->sub_pos);
return M_PROPERTY_OK;
}
return mp_property_generic_option(mpctx, prop, action, arg);
}

static int mp_property_secondary_sub_pos(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
struct MPOpts *opts = mpctx->opts;
if (action == M_PROPERTY_PRINT) {
*(char **)arg = talloc_asprintf(NULL, "%4.2f%%/100", opts->subs_rend->sec_sub_pos);
*(char **)arg = talloc_asprintf(NULL, "%4.2f%%/100", opts->subs_shared->sub_pos[track_ind]);
return M_PROPERTY_OK;
}
return mp_property_generic_option(mpctx, prop, action, arg);
Expand Down Expand Up @@ -2945,11 +2934,14 @@ static int mp_property_sub_ass_extradata(void *ctx, struct m_property *prop,
return M_PROPERTY_NOT_IMPLEMENTED;
}

static int get_sub_text(void *ctx, struct m_property *prop,
int action, void *arg, int sub_index)
static int mp_property_sub_text(void *ctx, struct m_property *prop,
int action, void *arg)
{
int type = *(int *)prop->priv;
MPContext *mpctx = ctx;
const int *def = prop->priv;
int sub_index = def[0];
int type = def[1];

struct track *track = mpctx->current_track[sub_index][STREAM_SUB];
struct dec_sub *sub = track ? track->d_sub : NULL;
double pts = mpctx->playback_pts;
Expand All @@ -2971,18 +2963,6 @@ static int get_sub_text(void *ctx, struct m_property *prop,
return M_PROPERTY_NOT_IMPLEMENTED;
}

static int mp_property_sub_text(void *ctx, struct m_property *prop,
int action, void *arg)
{
return get_sub_text(ctx, prop, action, arg, 0);
}

static int mp_property_secondary_sub_text(void *ctx, struct m_property *prop,
int action, void *arg)
{
return get_sub_text(ctx, prop, action, arg, 1);
}

static struct sd_times get_times(void *ctx, struct m_property *prop,
int action, void *arg)
{
Expand Down Expand Up @@ -3947,15 +3927,16 @@ static const struct m_property mp_properties_base[] = {
{"secondary-sub-delay", mp_property_sub_delay,
.priv = (void *)&(const int){1}},
{"sub-speed", mp_property_sub_speed},
{"sub-pos", mp_property_sub_pos},
{"secondary-sub-pos", mp_property_secondary_sub_pos},
{"sub-pos", mp_property_sub_pos, .priv = (void *)&(const int){0}},
{"secondary-sub-pos", mp_property_sub_pos,
.priv = (void *)&(const int){1}},
{"sub-ass-extradata", mp_property_sub_ass_extradata},
{"sub-text", mp_property_sub_text,
.priv = (void *)&(const int){SD_TEXT_TYPE_PLAIN}},
{"secondary-sub-text", mp_property_secondary_sub_text,
.priv = (void *)&(const int){SD_TEXT_TYPE_PLAIN}},
.priv = (void *)&(const int[]){0, SD_TEXT_TYPE_PLAIN}},
{"secondary-sub-text", mp_property_sub_text,
.priv = (void *)&(const int[]){1, SD_TEXT_TYPE_PLAIN}},
{"sub-text-ass", mp_property_sub_text,
.priv = (void *)&(const int){SD_TEXT_TYPE_ASS}},
.priv = (void *)&(const int[]){0, SD_TEXT_TYPE_ASS}},
{"sub-start", mp_property_sub_start,
.priv = (void *)&(const int){0}},
{"secondary-sub-start", mp_property_sub_start,
Expand Down Expand Up @@ -5406,9 +5387,9 @@ static void cmd_sub_step_seek(void *p)
a[1] = cmd->args[0].v.i;
if (sub_control(sub, SD_CTRL_SUB_STEP, a) > 0) {
if (step) {
mpctx->opts->subs_rend->sub_delay[track_ind] -= a[0] - refpts;
mpctx->opts->subs_shared->sub_delay[track_ind] -= a[0] - refpts;
m_config_notify_change_opt_ptr_notify(mpctx->mconfig,
&mpctx->opts->subs_rend->sub_delay[track_ind]);
&mpctx->opts->subs_shared->sub_delay[track_ind]);
show_property_osd(
mpctx,
track_ind == 0 ? "sub-delay" : "secondary-sub-delay",
Expand Down
2 changes: 1 addition & 1 deletion player/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ double get_track_seek_offset(struct MPContext *mpctx, struct track *track)
{
for (int n = 0; n < num_ptracks[STREAM_SUB]; n++) {
if (mpctx->current_track[n][STREAM_SUB] == track)
return -opts->subs_rend->sub_delay[n];
return -opts->subs_shared->sub_delay[n];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion player/osd.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static void term_osd_update_title(struct MPContext *mpctx)

void term_osd_set_subs(struct MPContext *mpctx, const char *text)
{
if (mpctx->video_out || !text || !mpctx->opts->subs_rend->sub_visibility)
if (mpctx->video_out || !text || !mpctx->opts->subs_shared->sub_visibility[0])
text = ""; // disable
if (strcmp(mpctx->term_osd_subs ? mpctx->term_osd_subs : "", text) == 0)
return;
Expand Down
16 changes: 11 additions & 5 deletions sub/dec_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ struct dec_sub {
struct mp_log *log;
struct mpv_global *global;
struct mp_subtitle_opts *opts;
struct mp_subtitle_shared_opts *shared_opts;
struct m_config_cache *opts_cache;
struct m_config_cache *shared_opts_cache;

struct mp_recorder_sink *recorder_sink;

Expand Down Expand Up @@ -91,7 +93,7 @@ static void update_subtitle_speed(struct dec_sub *sub)
// Return the subtitle PTS used for a given video PTS.
static double pts_to_subtitle(struct dec_sub *sub, double pts)
{
struct mp_subtitle_opts *opts = sub->opts;
struct mp_subtitle_shared_opts *opts = sub->shared_opts;
float delay = sub->order < 0 ? 0.0f : opts->sub_delay[sub->order];

if (pts != MP_NOPTS_VALUE)
Expand All @@ -102,7 +104,7 @@ static double pts_to_subtitle(struct dec_sub *sub, double pts)

static double pts_from_subtitle(struct dec_sub *sub, double pts)
{
struct mp_subtitle_opts *opts = sub->opts;
struct mp_subtitle_shared_opts *opts = sub->shared_opts;
float delay = sub->order < 0 ? 0.0f : opts->sub_delay[sub->order];

if (pts != MP_NOPTS_VALUE)
Expand Down Expand Up @@ -140,6 +142,7 @@ static struct sd *init_decoder(struct dec_sub *sub)
.global = sub->global,
.log = mp_log_new(sd, sub->log, driver->name),
.opts = sub->opts,
.shared_opts = sub->shared_opts,
.driver = driver,
.attachments = sub->attachments,
.codec = sub->codec,
Expand Down Expand Up @@ -172,6 +175,7 @@ struct dec_sub *sub_create(struct mpv_global *global, struct track *track,
.log = mp_log_new(sub, global->log, "sub"),
.global = global,
.opts_cache = m_config_cache_alloc(sub, global, &mp_subtitle_sub_opts),
.shared_opts_cache = m_config_cache_alloc(sub, global, &mp_subtitle_shared_sub_opts),
.sh = track->stream,
.codec = track->stream->codec,
.attachments = talloc_steal(sub, attachments),
Expand All @@ -183,6 +187,7 @@ struct dec_sub *sub_create(struct mpv_global *global, struct track *track,
.end = MP_NOPTS_VALUE,
};
sub->opts = sub->opts_cache->opts;
sub->shared_opts = sub->shared_opts_cache->opts;
mp_mutex_init_type(&sub->lock, MP_MUTEX_RECURSIVE);

sub->sd = init_decoder(sub);
Expand Down Expand Up @@ -292,7 +297,7 @@ bool sub_read_packets(struct dec_sub *sub, double video_pts, bool force)
break;

// (Use this mechanism only if sub_delay matters to avoid corner cases.)
float delay = sub->order < 0 ? 0.0f : sub->opts->sub_delay[sub->order];
float delay = sub->order < 0 ? 0.0f : sub->shared_opts->sub_delay[sub->order];
double min_pts = delay < 0 || force ? video_pts : MP_NOPTS_VALUE;

struct demux_packet *pkt;
Expand Down Expand Up @@ -457,6 +462,7 @@ int sub_control(struct dec_sub *sub, enum sd_ctrl cmd, void *arg)
int flags = (uintptr_t)arg;
if (m_config_cache_update(sub->opts_cache))
update_subtitle_speed(sub);
m_config_cache_update(sub->shared_opts_cache);
propagate = true;
if (flags & UPDATE_SUB_HARD) {
// forget about the previous preload because
Expand Down Expand Up @@ -491,10 +497,10 @@ void sub_set_play_dir(struct dec_sub *sub, int dir)

bool sub_is_primary_visible(struct dec_sub *sub)
{
return !!sub->opts->sub_visibility;
return sub->shared_opts->sub_visibility[0];
}

bool sub_is_secondary_visible(struct dec_sub *sub)
{
return !!sub->opts->sec_sub_visibility;
return sub->shared_opts->sub_visibility[1];
}
1 change: 1 addition & 0 deletions sub/sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct sd {
struct mpv_global *global;
struct mp_log *log;
struct mp_subtitle_opts *opts;
struct mp_subtitle_shared_opts *shared_opts;

const struct sd_functions *driver;
void *priv;
Expand Down
10 changes: 3 additions & 7 deletions sub/sd_ass.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ struct sd_ass_priv {
struct sd_filter **filters;
int num_filters;
bool clear_once;
bool on_top;
struct mp_ass_packer *packer;
struct sub_bitmap_copy_cache *copy_cache;
char last_text[500];
Expand Down Expand Up @@ -382,6 +381,7 @@ static void configure_ass(struct sd *sd, struct mp_osd_res *dim,
bool converted, ASS_Track *track, int order)
{
struct mp_subtitle_opts *opts = sd->opts;
struct mp_subtitle_shared_opts *shared_opts = sd->shared_opts;
struct sd_ass_priv *ctx = sd->priv;
ASS_Renderer *priv = ctx->ass_renderer;

Expand All @@ -407,7 +407,7 @@ static void configure_ass(struct sd *sd, struct mp_osd_res *dim,
set_use_margins = opts->ass_use_margins;
}
if (converted || opts->ass_style_override) {
set_sub_pos = 100.0f - (order == 1 ? opts->sec_sub_pos : opts->sub_pos);
set_sub_pos = 100.0f - shared_opts->sub_pos[order];
set_line_spacing = opts->ass_line_spacing;
set_hinting = opts->ass_hinting;
set_font_scale = opts->sub_scale;
Expand Down Expand Up @@ -561,8 +561,7 @@ static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res dim,
{
struct sd_ass_priv *ctx = sd->priv;
struct mp_subtitle_opts *opts = sd->opts;
bool no_ass = !opts->ass_enabled || ctx->on_top ||
opts->ass_style_override == 5 || order == 1;
bool no_ass = !opts->ass_enabled || opts->ass_style_override == 5 || order == 1;
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 Down Expand Up @@ -785,9 +784,6 @@ static void fill_plaintext(struct sd *sd, double pts)

bstr dst = {0};

if (ctx->on_top)
bstr_xappend(NULL, &dst, bstr0("{\\a6}"));

while (*text) {
if (*text == '{')
bstr_xappend(NULL, &dst, bstr0("\\"));
Expand Down
5 changes: 3 additions & 2 deletions sub/sd_lavc.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res d,
{
struct sd_lavc_priv *priv = sd->priv;
struct mp_subtitle_opts *opts = sd->opts;
struct mp_subtitle_shared_opts *shared_opts = sd->shared_opts;

priv->current_pts = pts;

Expand Down Expand Up @@ -450,8 +451,8 @@ static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res d,
h = MPMAX(priv->video_params.h, current->src_h);
}

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

for (int n = 0; n < res->num_parts; n++) {
struct sub_bitmap *sub = &res->parts[n];
Expand Down