Skip to content
Open
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
1 change: 1 addition & 0 deletions player/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ void run_playloop(struct MPContext *mpctx);
void mp_idle(struct MPContext *mpctx);
void idle_loop(struct MPContext *mpctx);
int handle_force_window(struct MPContext *mpctx, bool force);
void handle_playback_time(struct MPContext *mpctx);
void seek_to_last_frame(struct MPContext *mpctx);
void update_screensaver_state(struct MPContext *mpctx);
void update_ab_loop_clip(struct MPContext *mpctx);
Expand Down
2 changes: 1 addition & 1 deletion player/playloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ static void handle_dummy_ticks(struct MPContext *mpctx)
}

// Update current playback time.
static void handle_playback_time(struct MPContext *mpctx)
void handle_playback_time(struct MPContext *mpctx)
{
if (mpctx->vo_chain &&
!mpctx->vo_chain->is_sparse &&
Expand Down
3 changes: 3 additions & 0 deletions player/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -1344,8 +1344,11 @@ void write_video(struct MPContext *mpctx)

schedule_frame(mpctx, frame);

handle_playback_time(mpctx);
osd_frame_begin(mpctx->osd, mpctx->video_pts);
mpctx->osd_force_update = true;
update_osd_msg(mpctx);
osd_frame_end(mpctx->osd);

vo_queue_frame(vo, frame);

Expand Down
78 changes: 64 additions & 14 deletions sub/osd.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ struct osd_state *osd_create(struct mpv_global *global)
.global = global,
.log = mp_log_new(osd, global->log, "osd"),
.force_video_pts = MP_NOPTS_VALUE,
.frame_pts = MP_NOPTS_VALUE,
.last_frame_pts = MP_NOPTS_VALUE,
.stats = stats_ctx_create(osd, global, "osd"),
};
mp_mutex_init(&osd->lock);
Expand All @@ -176,6 +178,8 @@ struct osd_state *osd_create(struct mpv_global *global)
.type = n,
.text = talloc_strdup(obj, ""),
.progbar_state = {.type = -1},
.state_id = 1,
.prev_pts = MP_NOPTS_VALUE,
.vo_change_id = 1,
};
osd->objs[n] = obj;
Expand All @@ -198,16 +202,60 @@ void osd_free(struct osd_state *osd)
talloc_free(osd);
}

static void copy_progbar(struct osd_object *obj, struct osd_progbar_state *dst,
struct osd_progbar_state *src)
{
dst->type = src->type;
dst->value = src->value;
dst->num_stops = src->num_stops;
MP_TARRAY_GROW(obj, dst->stops, src->num_stops);
if (src->num_stops)
memcpy(dst->stops, src->stops, sizeof(dst->stops[0]) * src->num_stops);
}

void osd_frame_begin(struct osd_state *osd, double pts)
{
mp_mutex_lock(&osd->lock);
osd->frame_pts = pts;
mp_mutex_unlock(&osd->lock);
}

void osd_frame_end(struct osd_state *osd)
{
mp_mutex_lock(&osd->lock);
struct osd_object *obj = osd->objs[OSDTYPE_OSD];
// A state that is kept for an older frame is not longer shown.
if (obj->prev_pts != osd->last_frame_pts)
obj->prev_pts = MP_NOPTS_VALUE;
osd->last_frame_pts = osd->frame_pts;
osd->frame_pts = MP_NOPTS_VALUE;
mp_mutex_unlock(&osd->lock);
}

static void keep_frame_state(struct osd_state *osd, struct osd_object *obj)
{
if (osd->frame_pts == MP_NOPTS_VALUE || osd->frame_pts == osd->last_frame_pts) {
obj->prev_pts = MP_NOPTS_VALUE;
} else if (obj->prev_pts != osd->last_frame_pts) {
obj->prev_pts = osd->last_frame_pts;
obj->prev_id = obj->state_id;
talloc_free(obj->prev_text);
obj->prev_text = talloc_strdup(obj, obj->text);
copy_progbar(obj, &obj->prev_progbar_state, &obj->progbar_state);
}
}

void osd_set_text(struct osd_state *osd, const char *text)
{
mp_mutex_lock(&osd->lock);
struct osd_object *osd_obj = osd->objs[OSDTYPE_OSD];
if (!text)
text = "";
if (strcmp(osd_obj->text, text) != 0) {
keep_frame_state(osd, osd_obj);
talloc_free(osd_obj->text);
osd_obj->text = talloc_strdup(osd_obj, text);
osd_obj->osd_changed = true;
osd_obj->state_id += 1;
osd->want_redraw_notification = true;
}
mp_mutex_unlock(&osd->lock);
Expand Down Expand Up @@ -262,15 +310,9 @@ void osd_set_progbar(struct osd_state *osd, struct osd_progbar_state *s)
{
mp_mutex_lock(&osd->lock);
struct osd_object *osd_obj = osd->objs[OSDTYPE_OSD];
osd_obj->progbar_state.type = s->type;
osd_obj->progbar_state.value = s->value;
osd_obj->progbar_state.num_stops = s->num_stops;
MP_TARRAY_GROW(osd_obj, osd_obj->progbar_state.stops, s->num_stops);
if (s->num_stops) {
memcpy(osd_obj->progbar_state.stops, s->stops,
sizeof(osd_obj->progbar_state.stops[0]) * s->num_stops);
}
osd_obj->osd_changed = true;
keep_frame_state(osd, osd_obj);
copy_progbar(osd_obj, &osd_obj->progbar_state, s);
osd_obj->state_id += 1;
osd->want_redraw_notification = true;
mp_mutex_unlock(&osd->lock);
}
Expand All @@ -291,7 +333,7 @@ static void check_obj_resize(struct osd_state *osd, struct mp_osd_res res,
{
if (!osd_res_equals(res, obj->vo_res)) {
obj->vo_res = res;
obj->osd_changed = true;
obj->track_id = 0;
mp_client_broadcast_event_external(osd->global->client_api,
MP_EVENT_WIN_RESIZE, NULL);
}
Expand All @@ -317,6 +359,7 @@ void osd_resize(struct osd_state *osd, struct mp_osd_res res)
static struct sub_bitmaps *render_object(struct osd_state *osd,
struct osd_object *obj,
struct mp_osd_res osdres, double video_pts,
double frame_pts,
const bool sub_formats[SUBBITMAP_COUNT])
{
int format = SUBBITMAP_LIBASS;
Expand All @@ -340,7 +383,12 @@ static struct sub_bitmaps *render_object(struct osd_state *osd,
obj->image_overlay->change_id = 0;
}
} else {
res = osd_object_get_bitmaps(osd, obj, format);
// The frame before the one the OSD was changed for keeps its state
// until another frame is drawn.
bool prev = obj->prev_pts != MP_NOPTS_VALUE && obj->prev_pts == frame_pts;
if (!prev)
obj->prev_pts = MP_NOPTS_VALUE;
res = osd_object_get_bitmaps(osd, obj, format, prev);
}

if (obj->vo_had_output != !!res) {
Expand All @@ -362,6 +410,7 @@ static struct sub_bitmaps *render_object(struct osd_state *osd,
// refcounted. Typically you should hold it only for a short time, and then
// release it.
// draw_flags is a bit field of OSD_DRAW_* constants
// video_pts is the pts of the video frame the result is drawn with.
struct sub_bitmap_list *osd_render(struct osd_state *osd, struct mp_osd_res res,
double video_pts, int draw_flags,
const bool formats[SUBBITMAP_COUNT])
Expand All @@ -375,6 +424,7 @@ struct sub_bitmap_list *osd_render(struct osd_state *osd, struct mp_osd_res res,
list->w = res.w;
list->h = res.h;

double frame_pts = video_pts;
double force_video_pts = atomic_load(&osd->force_video_pts);
if (force_video_pts != MP_NOPTS_VALUE)
video_pts = force_video_pts;
Expand All @@ -398,7 +448,7 @@ struct sub_bitmap_list *osd_render(struct osd_state *osd, struct mp_osd_res res,
stats_time_start(osd->stats, stat_type_render);

struct sub_bitmaps *imgs =
render_object(osd, obj, res, video_pts, formats);
render_object(osd, obj, res, video_pts, frame_pts, formats);

stats_time_end(osd->stats, stat_type_render);

Expand Down Expand Up @@ -507,7 +557,7 @@ struct mp_osd_res osd_res_from_image_params(const struct mp_image_params *p)
void osd_changed(struct osd_state *osd)
{
mp_mutex_lock(&osd->lock);
osd->objs[OSDTYPE_OSD]->osd_changed = true;
osd->objs[OSDTYPE_OSD]->track_id = 0;
osd->want_redraw_notification = true;
// Done here for a lack of a better place.
m_config_cache_update(osd->opts_cache);
Expand Down
2 changes: 2 additions & 0 deletions sub/osd.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ void osd_free(struct osd_state *osd);

bool osd_query_and_reset_want_redraw(struct osd_state *osd);

void osd_frame_begin(struct osd_state *osd, double pts);
void osd_frame_end(struct osd_state *osd);
void osd_set_text(struct osd_state *osd, const char *text);
void osd_set_sub(struct osd_state *osd, int index, struct dec_sub *dec_sub);

Expand Down
41 changes: 22 additions & 19 deletions sub/osd_libass.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,15 @@ static ASS_Style *prepare_osd_ass(struct osd_state *osd, struct osd_object *obj)
return style;
}

static void update_osd_text(struct osd_state *osd, struct osd_object *obj)
static void update_osd_text(struct osd_state *osd, struct osd_object *obj,
const char *text)
{

if (!obj->text[0])
if (!text[0])
return;

prepare_osd_ass(osd, obj);
add_osd_ass_event_escaped(obj->ass.track, "OSD", obj->text);
add_osd_ass_event_escaped(obj->ass.track, "OSD", text);
}

void osd_get_text_size(struct osd_state *osd, int *out_screen_h, int *out_font_h)
Expand Down Expand Up @@ -415,9 +416,10 @@ static void get_osd_bar_box(struct osd_state *osd, struct osd_object *obj,
*o_y = get_align(bar_opts->align_y, track->PlayResY, *o_h, *o_border);
}

static void update_progbar(struct osd_state *osd, struct osd_object *obj)
static void update_progbar(struct osd_state *osd, struct osd_object *obj,
struct osd_progbar_state *bar)
{
if (obj->progbar_state.type < 0)
if (bar->type < 0)
return;

float px, py, width, height, border;
Expand All @@ -430,13 +432,13 @@ static void update_progbar(struct osd_state *osd, struct osd_object *obj)

bstr buf = bstr0(talloc_asprintf(NULL, "{\\an6\\pos(%f,%f)}", sx, sy));

if (obj->progbar_state.type == 0 || obj->progbar_state.type >= 256) {
if (bar->type == 0 || bar->type >= 256) {
// no sym
} else if (obj->progbar_state.type >= 32) {
mp_append_utf8_bstr(NULL, &buf, obj->progbar_state.type);
} else if (bar->type >= 32) {
mp_append_utf8_bstr(NULL, &buf, bar->type);
} else {
bstr_xappend(NULL, &buf, bstr0(ASS_USE_OSD_FONT));
mp_append_utf8_bstr(NULL, &buf, OSD_CODEPOINTS + obj->progbar_state.type);
mp_append_utf8_bstr(NULL, &buf, OSD_CODEPOINTS + bar->type);
bstr_xappend(NULL, &buf, bstr0("{\\r}"));
}

Expand All @@ -463,7 +465,7 @@ static void update_progbar(struct osd_state *osd, struct osd_object *obj)
// filled area
d->text = talloc_asprintf_append(d->text, "{\\bord0\\pos(%f,%f)}", px, py);
ass_draw_start(d);
float pos = obj->progbar_state.value * width - border / 2;
float pos = bar->value * width - border / 2;
ass_draw_rect_cw(d, 0, 0, pos, height);
ass_draw_stop(d);
add_osd_ass_event(track, "progbar", bstr0(d->text));
Expand Down Expand Up @@ -491,8 +493,8 @@ static void update_progbar(struct osd_state *osd, struct osd_object *obj)
struct osd_bar_style_opts *bar_opts = osd->opts->osd_bar_style;
// chapter marks
if (bar_opts->marker_style) {
for (int n = 0; n < obj->progbar_state.num_stops; n++) {
float s = obj->progbar_state.stops[n] * width;
for (int n = 0; n < bar->num_stops; n++) {
float s = bar->stops[n] * width;
float size = MPMAX(border * bar_opts->marker_scale,
bar_opts->marker_min_size);

Expand All @@ -517,12 +519,12 @@ static void update_progbar(struct osd_state *osd, struct osd_object *obj)
ass_draw_reset(d);
}

static void update_osd(struct osd_state *osd, struct osd_object *obj)
static void update_osd(struct osd_state *osd, struct osd_object *obj, bool prev)
{
obj->osd_changed = false;
obj->track_id = prev ? obj->prev_id : obj->state_id;
clear_ass(&obj->ass);
update_osd_text(osd, obj);
update_progbar(osd, obj);
update_osd_text(osd, obj, prev ? obj->prev_text : obj->text);
update_progbar(osd, obj, prev ? &obj->prev_progbar_state : &obj->progbar_state);
}

static void update_external(struct osd_state *osd, struct osd_object *obj,
Expand Down Expand Up @@ -685,11 +687,12 @@ static void append_ass(struct ass_state *ass, struct mp_osd_res *res,
}

struct sub_bitmaps *osd_object_get_bitmaps(struct osd_state *osd,
struct osd_object *obj, int format)
struct osd_object *obj, int format,
bool prev)
{
if (obj->type == OSDTYPE_OSD) {
if (obj->osd_changed) {
update_osd(osd, obj);
if (obj->track_id != (prev ? obj->prev_id : obj->state_id)) {
update_osd(osd, obj, prev);
} else {
mp_require(obj->sub_packer);
goto done;
Expand Down
13 changes: 11 additions & 2 deletions sub/osd_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ struct osd_object {
bool is_sub;

// OSDTYPE_OSD
bool osd_changed;
int state_id;
int track_id;
char *text;
struct osd_progbar_state progbar_state;
double prev_pts;
int prev_id;
char *prev_text;
struct osd_progbar_state prev_progbar_state;

// OSDTYPE_SUB/OSDTYPE_SUB2
struct dec_sub *sub;
Expand Down Expand Up @@ -77,6 +82,9 @@ struct osd_state {

bool want_redraw;
bool want_redraw_notification;
// The frame that OSD changes are made for, see osd_frame_begin(), and the
// frame before it
double frame_pts, last_frame_pts;

struct m_config_cache *opts_cache;
struct mp_osd_render_opts *opts;
Expand All @@ -89,7 +97,8 @@ struct osd_state {

// defined in osd_libass.c
struct sub_bitmaps *osd_object_get_bitmaps(struct osd_state *osd,
struct osd_object *obj, int format);
struct osd_object *obj, int format,
bool prev);
void osd_destroy_backend(struct osd_state *osd);

#endif
Loading