Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
d98517a
dec_sub: give the decoder of a new segment the video params
kasper93 Nov 28, 2026
3df4f86
bitmap_packer: don't try sizes smaller than the area of all rectangles
kasper93 Nov 28, 2026
c244c4d
sub/packer: fall back to RGBA when the libass images don't fit
kasper93 Nov 28, 2026
dfe2c6a
sub/packer: skip the rows without coverage when blending into RGBA
kasper93 Nov 28, 2026
67e86ef
sub/packer: take the image of the atlas from a pool
kasper93 Nov 28, 2026
09eb53d
osd_libass: keep the images of a track until its events change
kasper93 Nov 28, 2026
5ee9116
dec_sub: check the subtitle visibility in sub_get_bitmaps()
kasper93 Nov 28, 2026
8c4a3f1
osd: leave the subtitle visibility to sub_get_bitmaps()
kasper93 Nov 28, 2026
3b06b1a
dec_sub: remove sub_is_primary_visible() and sub_is_secondary_visible()
kasper93 Nov 28, 2026
abd4aae
dec_sub: feed the decoders through one function
kasper93 Nov 28, 2026
c57b0eb
sub: let the decoders tell from which time a packet changes the output
kasper93 Nov 28, 2026
7e6f4b0
dec_sub: make the change id of the bitmaps an identity
kasper93 Nov 28, 2026
63f4522
dec_sub: keep the time sub_get_bitmaps() is called with
kasper93 Nov 28, 2026
2d488bc
dec_sub: keep rendered subtitles until the decoder state changes
kasper93 Nov 28, 2026
0e12067
sd_ass: tell until when the bitmaps of a time stay the same
kasper93 Nov 28, 2026
067d21f
dec_sub: keep a result for as long as the decoder shows the same
kasper93 Nov 28, 2026
c7e0ee7
osd_libass: get the OSD text size without the libass track
kasper93 Nov 28, 2026
d295d8e
osd_libass: pass the size to the functions that build the tracks
kasper93 Nov 28, 2026
9ec4883
osd_libass: mark an overlay as changed after its data is set
kasper93 Nov 28, 2026
b10338e
osd: report the changes of the OSD objects through one function
kasper93 Nov 28, 2026
3fd31cf
osd: keep the result of the OSD objects with the state it shows
kasper93 Nov 28, 2026
47dfab9
osd: remove the osd_changed flag
kasper93 Nov 28, 2026
eb88792
osd_libass: take the size for the bounds of an overlay from the object
kasper93 Nov 28, 2026
6731e59
osd_libass: build the track of an overlay when it is rendered
kasper93 Nov 28, 2026
af4d501
osd_libass: split osd_object_get_bitmaps() into two functions
kasper93 Nov 28, 2026
215840c
osd_libass: render the OSD text and the bar from a copy
kasper93 Nov 28, 2026
575ba0d
osd_libass: build the tracks of the overlays from a copy
kasper93 Nov 28, 2026
6da0688
osd: add osd_wait_object_idle()
kasper93 Nov 28, 2026
91955e0
dec_sub: don't render what another thread rendered meanwhile
kasper93 Nov 28, 2026
b805d40
osd: don't hold the lock while a subtitle track renders
kasper93 Nov 28, 2026
3ea0d81
osd: update the options while no render reads them
kasper93 Nov 28, 2026
d9e6571
osd_libass: check for the removal of an unknown overlay first
kasper93 Nov 28, 2026
149c7a4
osd_libass: change the set of overlays while no render reads it
kasper93 Nov 28, 2026
5de923c
osd: let one thread at a time render an object
kasper93 Nov 28, 2026
003fc54
osd: don't hold the lock while rendering the OSD
kasper93 Nov 28, 2026
35b4fe7
osd_libass: don't hold the lock for the bounds of an overlay
kasper93 Nov 28, 2026
0aaa80c
osd: select the format of osd_render() in a function
kasper93 Nov 28, 2026
a45badf
osd: select the objects of osd_render() in a function
kasper93 Nov 28, 2026
13138ce
dec_sub: add sub_has_bitmaps()
kasper93 Nov 28, 2026
c1b603c
osd: add osd_prefetch()
kasper93 Nov 28, 2026
766ae7e
vo_gpu_next: move the size of blended subtitles to a function
kasper93 Nov 28, 2026
b15945e
vo_gpu_next: ask for the subtitles of the next frame ahead
kasper93 Nov 28, 2026
3f93cf0
osd: add osd_set_wakeup_cb()
kasper93 Nov 28, 2026
3d2dad3
player: let the OSD wake the core up
kasper93 Nov 28, 2026
059c427
osd: count the threads that wait for an object
kasper93 Nov 28, 2026
4e45ccb
osd: add OSD_DRAW_OSD_LATEST
kasper93 Nov 28, 2026
6a331c1
vo_gpu_next: don't wait for the OSD
kasper93 Nov 28, 2026
4364aba
vo_gpu_next: update the overlays of the target in a function
kasper93 Nov 28, 2026
555dcc5
vo_gpu_next: take the overlays when libplacebo draws them
kasper93 Nov 28, 2026
e53d484
vo_gpu_next: update the blended subtitles of a frame in a function
kasper93 Nov 28, 2026
eced9e5
vo_gpu_next: take the blended subtitles when libplacebo draws them
kasper93 Nov 28, 2026
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 DOCS/interface-changes/osd-thread.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add `--osd-thread`
8 changes: 8 additions & 0 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5094,6 +5094,14 @@ OSD
Set the text layout engine used by libass for the OSD. Default: complex.
See also ``--sub-shaper``

``--osd-thread=<yes|no>``
Render the subtitles of a video frame on a separate thread before the
frame is drawn, if the VO supports it (default: yes).
(``gpu-next`` only)
With this option it also renders the OSD on a separate thread when the
OSD changes, and shows the change once it is rendered instead of delaying
a frame for it.

Screenshot
----------

Expand Down
2 changes: 2 additions & 0 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,12 @@ const struct m_sub_options mp_osd_render_sub_opts = {
{"osd-glyph-limit", OPT_INT(osd_glyph_limit)},
{"osd-bitmap-max-size", OPT_INT(osd_bitmap_max_size)},
{"osd-shaper", OPT_CHOICE(osd_shaper, {"simple", 0}, {"complex", 1})},
{"osd-thread", OPT_BOOL(osd_thread)},
{0}
},
.size = sizeof(OPT_BASE_STRUCT),
.defaults = &(OPT_BASE_STRUCT){
.osd_thread = true,
.osd_scale = 1,
.osd_scale_by_window = true,
.osd_selected_color = {250, 189, 47, 255},
Expand Down
1 change: 1 addition & 0 deletions options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ struct mp_osd_render_opts {
int osd_glyph_limit;
int osd_bitmap_max_size;
int osd_shaper;
bool osd_thread;
};

struct mp_bluray_opts {
Expand Down
1 change: 1 addition & 0 deletions player/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ struct MPContext *mp_create(void)
init_libav(mpctx->global);
mp_clients_init(mpctx);
mpctx->osd = osd_create(mpctx->global);
osd_set_wakeup_cb(mpctx->osd, mp_wakeup_core_cb, mpctx);

#if HAVE_COCOA
cocoa_set_input_context(mpctx->input);
Expand Down
185 changes: 159 additions & 26 deletions sub/dec_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "common/recorder.h"
#include "misc/dispatch.h"
#include "osdep/threads.h"
#include "video/mp_image.h"

extern const struct sd_functions sd_ass;
extern const struct sd_functions sd_lavc;
Expand All @@ -49,9 +50,26 @@ static const struct sd_functions *const sd_list[] = {
NULL
};

#define SUB_CACHE_SIZE 4

struct sub_cache_entry {
struct sub_bitmaps *res; // NULL if nothing is shown
struct mp_osd_res dim;
int format;
double pts; // as passed to sub_get_bitmaps()
double pts_end; // the result holds from pts until before this
double sub_pts, sub_end;
uint64_t age; // 0 if unused
};

struct dec_sub {
mp_mutex lock;

mp_mutex cache_lock;
struct sub_cache_entry cache[SUB_CACHE_SIZE];
uint64_t cache_age;
int change_id;

struct mp_log *log;
struct mpv_global *global;
struct demux_packet_pool *packet_pool;
Expand All @@ -70,6 +88,7 @@ struct dec_sub {
double last_pkt_pts;
bool preload_attempted;
double video_fps;
struct mp_image_params video_params;
double sub_speed;
bool sub_visible;

Expand Down Expand Up @@ -126,6 +145,69 @@ static double pts_from_subtitle(struct dec_sub *sub, double pts)
return pts;
}

// Drop the results that hold at or after the given subtitle time.
static void drop_results(struct dec_sub *sub, double sub_pts)
{
mp_mutex_lock(&sub->cache_lock);
for (int n = 0; n < SUB_CACHE_SIZE; n++) {
struct sub_cache_entry *e = &sub->cache[n];
if (e->sub_pts >= sub_pts || e->sub_end > sub_pts) {
talloc_free(e->res);
*e = (struct sub_cache_entry){0};
}
}
mp_mutex_unlock(&sub->cache_lock);
}

// pts_end lies before pts with backward playback.
static bool result_holds(struct sub_cache_entry *e, double pts)
{
return e->pts == pts || (pts > MPMIN(e->pts, e->pts_end) && pts < MPMAX(e->pts, e->pts_end));
}

static bool get_result(struct dec_sub *sub, struct mp_osd_res dim, int format,
double pts, struct sub_bitmaps **res)
{
bool found = false;
mp_mutex_lock(&sub->cache_lock);
for (int n = 0; n < SUB_CACHE_SIZE && !found; n++) {
struct sub_cache_entry *e = &sub->cache[n];
if (e->age && result_holds(e, pts) && e->format == format && osd_res_equals(e->dim, dim)) {
e->age = ++sub->cache_age;
if (res)
*res = sub_bitmaps_copy(NULL, e->res);
found = true;
}
}
mp_mutex_unlock(&sub->cache_lock);
return found;
}

// Takes the place of the entry that was not used for the longest time.
static void put_result(struct dec_sub *sub, struct sub_cache_entry entry)
{
mp_mutex_lock(&sub->cache_lock);
struct sub_cache_entry *e = &sub->cache[0];
for (int n = 1; n < SUB_CACHE_SIZE; n++) {
if (sub->cache[n].age < e->age)
e = &sub->cache[n];
}
talloc_free(e->res);
*e = entry;
e->age = ++sub->cache_age;
mp_mutex_unlock(&sub->cache_lock);
}

static void decode_packet(struct dec_sub *sub, struct demux_packet *pkt)
{
double from = sub->sd->driver->decode(sub->sd, pkt);
// The timing fixes reach back from there. The decoders round times down
// to ms.
from -= (sub->opts->sub_fix_timing_threshold + 1) / 1000.0;
// Results before the start of the segment are from the previous decoder.
drop_results(sub, MPMAX(from, sub->start));
}

static void wakeup_demux(void *ctx)
{
struct mp_dispatch_queue *q = ctx;
Expand Down Expand Up @@ -154,6 +236,7 @@ void sub_destroy(struct dec_sub *sub)
sub->sd->driver->uninit(sub->sd);
}
talloc_free(sub->sd);
mp_mutex_destroy(&sub->cache_lock);
mp_mutex_destroy(&sub->lock);
talloc_free(sub);
}
Expand Down Expand Up @@ -218,6 +301,7 @@ struct dec_sub *sub_create(struct mpv_global *global, struct track *track,
sub->opts = sub->opts_cache->opts;
sub->shared_opts = sub->shared_opts_cache->opts;
mp_mutex_init(&sub->lock);
mp_mutex_init(&sub->cache_lock);

sub->sd = init_decoder(sub);
if (sub->sd) {
Expand All @@ -238,6 +322,7 @@ static void update_segment(struct dec_sub *sub)
MP_VERBOSE(sub, "Switch segment: %f at %f\n", sub->new_segment->start,
sub->last_vo_pts);

drop_results(sub, sub->new_segment->start);
sub->codec = sub->new_segment->codec;
sub->start = sub->new_segment->start;
sub->end = sub->new_segment->end;
Expand All @@ -247,12 +332,14 @@ static void update_segment(struct dec_sub *sub)
talloc_free(sub->sd);
sub->sd = new;
update_subtitle_speed(sub);
if (sub->sd->driver->control)
sub->sd->driver->control(sub->sd, SD_CTRL_SET_VIDEO_PARAMS, &sub->video_params);
} else {
// We'll just keep the current decoder, and feed it possibly
// invalid data (not our fault if it crashes or something).
MP_ERR(sub, "Can't change to new codec.\n");
}
sub->sd->driver->decode(sub->sd, sub->new_segment);
decode_packet(sub, sub->new_segment);
talloc_free(sub->new_segment);
sub->new_segment = NULL;
}
Expand Down Expand Up @@ -285,7 +372,7 @@ void sub_preload(struct dec_sub *sub)
}
if (!pkt)
break;
sub->sd->driver->decode(sub->sd, pkt);
decode_packet(sub, pkt);
MP_TARRAY_APPEND(sub, sub->cached_pkts, sub->num_cached_pkts, pkt);
}

Expand Down Expand Up @@ -395,7 +482,7 @@ void sub_read_packets(struct dec_sub *sub, double video_pts, bool force,
}

if (!(sub->preload_attempted && sub->sd->preload_ok))
sub->sd->driver->decode(sub->sd, pkt);
decode_packet(sub, pkt);
}
if (sub->cached_pkts && sub->num_cached_pkts) {
bool visible = is_packet_visible(sub->cached_pkts[sub->cached_pkt_pos], video_pts);
Expand All @@ -412,29 +499,72 @@ void sub_redecode_cached_packets(struct dec_sub *sub)
mp_mutex_lock(&sub->lock);
int index = sub->cached_pkt_pos;
while (index < sub->num_cached_pkts) {
sub->sd->driver->decode(sub->sd, sub->cached_pkts[index]);
decode_packet(sub, sub->cached_pkts[index]);
++index;
}
mp_mutex_unlock(&sub->lock);
}

bool sub_has_bitmaps(struct dec_sub *sub, struct mp_osd_res dim, int format,
double pts)
{
return get_result(sub, dim, format, pts, NULL);
}

// Unref sub_bitmaps.rc to free the result. May return NULL.
struct sub_bitmaps *sub_get_bitmaps(struct dec_sub *sub, struct mp_osd_res dim,
int format, double pts)
{
struct sub_bitmaps *res = NULL;
if (get_result(sub, dim, format, pts, &res))
return res;

mp_mutex_lock(&sub->lock);

pts = pts_to_subtitle(sub, pts);
// Another thread may have rendered it while this one waited for the lock.
if (get_result(sub, dim, format, pts, &res)) {
mp_mutex_unlock(&sub->lock);
return res;
}

sub->last_vo_pts = pts;
update_segment(sub);
double sub_pts = pts_to_subtitle(sub, pts);
double sub_end = sub_pts;
if (!sub->shared_opts->sub_visibility[sub->order])
goto done;

struct sub_bitmaps *res = NULL;
sub->last_vo_pts = sub_pts;
update_segment(sub);

if (!(sub->end != MP_NOPTS_VALUE && pts >= sub->end) &&
if (!(sub->end != MP_NOPTS_VALUE && sub_pts >= sub->end) &&
sub->sd->driver->get_bitmaps)
res = sub->sd->driver->get_bitmaps(sub->sd, dim, format, pts);
{
res = sub->sd->driver->get_bitmaps(sub->sd, dim, format, sub_pts);
if (sub->sd->driver->get_bitmaps_end) {
sub_end = sub->sd->driver->get_bitmaps_end(sub->sd, sub_pts);
// Segments switch only when a render reaches them.
if (sub->end != MP_NOPTS_VALUE)
sub_end = MPMIN(sub_end, sub->end);
if (sub->new_segment)
sub_end = MPMIN(sub_end, sub->new_segment->start);
}
}

// The decoders tell whether the bitmaps differ from the previous ones.
// A result that is kept needs an identity.
if (res) {
sub->change_id += res->change_id;
res->change_id = sub->change_id;
}

done:
// Without a time the result could not be dropped by time.
if (pts != MP_NOPTS_VALUE) {
put_result(sub, (struct sub_cache_entry){
.res = sub_bitmaps_copy(NULL, res), .dim = dim, .format = format,
.pts = pts, .pts_end = pts_from_subtitle(sub, sub_end),
.sub_pts = sub_pts, .sub_end = sub_end,
});
}
mp_mutex_unlock(&sub->lock);
return res;
}
Expand Down Expand Up @@ -490,6 +620,7 @@ struct sd_times sub_get_times(struct dec_sub *sub, double pts)
void sub_reset(struct dec_sub *sub)
{
mp_mutex_lock(&sub->lock);
drop_results(sub, -INFINITY);
if (sub->sd->driver->reset)
sub->sd->driver->reset(sub->sd);
sub->last_pkt_pts = MP_NOPTS_VALUE;
Expand All @@ -503,6 +634,7 @@ void sub_reset(struct dec_sub *sub)
void sub_select(struct dec_sub *sub, bool selected)
{
mp_mutex_lock(&sub->lock);
drop_results(sub, -INFINITY);
if (sub->sd->driver->select)
sub->sd->driver->select(sub->sd, selected);
mp_mutex_unlock(&sub->lock);
Expand All @@ -513,12 +645,25 @@ int sub_control(struct dec_sub *sub, enum sd_ctrl cmd, void *arg)
int r = CONTROL_UNKNOWN;
mp_mutex_lock(&sub->lock);
bool propagate = false;
// The player repeats some of these for every frame.
bool changed = true;
switch (cmd) {
case SD_CTRL_SET_VIDEO_DEF_FPS:
changed = sub->video_fps != *(double *)arg;
sub->video_fps = *(double *)arg;
update_subtitle_speed(sub);
break;
case SD_CTRL_SET_VIDEO_PARAMS:
changed = !mp_image_params_static_equal(&sub->video_params, arg);
sub->video_params = *(struct mp_image_params *)arg;
propagate = true;
break;
case SD_CTRL_SET_ANIMATED_CHECK:
changed = false;
propagate = true;
break;
case SD_CTRL_SUB_STEP: {
changed = false;
double *a = arg;
double arg2[2] = {a[0], a[1]};
arg2[0] = pts_to_subtitle(sub, arg2[0]);
Expand All @@ -545,6 +690,8 @@ int sub_control(struct dec_sub *sub, enum sd_ctrl cmd, void *arg)
default:
propagate = true;
}
if (changed)
drop_results(sub, -INFINITY);
if (propagate && sub->sd->driver->control)
r = sub->sd->driver->control(sub->sd, cmd, arg);
mp_mutex_unlock(&sub->lock);
Expand All @@ -561,26 +708,12 @@ void sub_set_recorder_sink(struct dec_sub *sub, struct mp_recorder_sink *sink)
void sub_set_play_dir(struct dec_sub *sub, int dir)
{
mp_mutex_lock(&sub->lock);
if (sub->play_dir != dir)
drop_results(sub, -INFINITY);
sub->play_dir = dir;
mp_mutex_unlock(&sub->lock);
}

bool sub_is_primary_visible(struct dec_sub *sub)
{
mp_mutex_lock(&sub->lock);
bool ret = sub->shared_opts->sub_visibility[0];
mp_mutex_unlock(&sub->lock);
return ret;
}

bool sub_is_secondary_visible(struct dec_sub *sub)
{
mp_mutex_lock(&sub->lock);
bool ret = sub->shared_opts->sub_visibility[1];
mp_mutex_unlock(&sub->lock);
return ret;
}

static int sub_line_cmp(const void *a, const void *b)
{
const struct sub_line *la = a, *lb = b;
Expand Down
4 changes: 2 additions & 2 deletions sub/dec_sub.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ void sub_read_packets(struct dec_sub *sub, double video_pts, bool force,
bool *packets_read, bool *sub_updated);
struct sub_bitmaps *sub_get_bitmaps(struct dec_sub *sub, struct mp_osd_res dim,
int format, double pts);
bool sub_has_bitmaps(struct dec_sub *sub, struct mp_osd_res dim, int format,
double pts);
char *sub_get_text(struct dec_sub *sub, double pts, enum sd_text_type type);
char *sub_ass_get_extradata(struct dec_sub *sub);
struct sd_times sub_get_times(struct dec_sub *sub, double pts);
Expand All @@ -73,8 +75,6 @@ void sub_reset(struct dec_sub *sub);
void sub_select(struct dec_sub *sub, bool selected);
void sub_set_recorder_sink(struct dec_sub *sub, struct mp_recorder_sink *sink);
void sub_set_play_dir(struct dec_sub *sub, int dir);
bool sub_is_primary_visible(struct dec_sub *sub);
bool sub_is_secondary_visible(struct dec_sub *sub);

int sub_control(struct dec_sub *sub, enum sd_ctrl cmd, void *arg);

Expand Down
Loading
Loading