From ce8524cb479f3b3339c6d2b3e0f5a45051145204 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 26 Dec 2015 18:32:27 +0100 Subject: sub: cache subtitle state per track instead of per demuxer stream Since commit 6d9cb893, subtitle state doesn't survive timeline switches (ordered chapters etc.). So there is no point in caching the state per sh_stream anymore (which would be required to deal with multiple segments). Move the cache to struct track. (Whether it's worth caching the subtitle state just for the situation when subtitle tracks get reselected is questionable. But for now, it's nice to have the subtitles immediately show up when reselecting a subtitle.) --- player/core.h | 4 +++- player/loadfile.c | 24 ++++++++++++++---------- player/sub.c | 34 ++++++++++------------------------ 3 files changed, 27 insertions(+), 35 deletions(-) (limited to 'player') diff --git a/player/core.h b/player/core.h index db453e097e..1d68019c1a 100644 --- a/player/core.h +++ b/player/core.h @@ -139,6 +139,9 @@ struct track { // Invariant: !stream || stream->demuxer == demuxer struct sh_stream *stream; + // Current subtitle state (or cached state if selected==false). + struct dec_sub *dec_sub; + // For external subtitles, which are read fully on init. Do not attempt // to read packets from them. bool preloaded; @@ -517,7 +520,6 @@ void mp_load_scripts(struct MPContext *mpctx); // sub.c void reset_subtitle_state(struct MPContext *mpctx); -void uninit_stream_sub_decoders(struct demuxer *demuxer); void reinit_subs(struct MPContext *mpctx, int order); void uninit_sub(struct MPContext *mpctx, int order); void uninit_sub_all(struct MPContext *mpctx); diff --git a/player/loadfile.c b/player/loadfile.c index 8b35209d9f..f1664b2d9d 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -72,17 +72,15 @@ static void uninit_demuxer(struct MPContext *mpctx) mpctx->chapters = NULL; mpctx->num_chapters = 0; - // per-stream cached subtitle state - for (int i = 0; i < mpctx->num_sources; i++) - uninit_stream_sub_decoders(mpctx->sources[i]); - // close demuxers for external tracks for (int n = mpctx->num_tracks - 1; n >= 0; n--) { mpctx->tracks[n]->selected = false; mp_remove_track(mpctx, mpctx->tracks[n]); } - for (int i = 0; i < mpctx->num_tracks; i++) + for (int i = 0; i < mpctx->num_tracks; i++) { + sub_destroy(mpctx->tracks[i]->dec_sub); talloc_free(mpctx->tracks[i]); + } mpctx->num_tracks = 0; mpctx->timeline = NULL; @@ -337,10 +335,15 @@ bool timeline_switch_to_time(struct MPContext *mpctx, double pts) track->user_tid - 1); } - if (track->type == STREAM_SUB && track->stream) { - struct dec_sub *dec = track->stream->sub->dec_sub; - if (dec) - sub_control(dec, SD_CTRL_CLEAR, NULL); + if (track->dec_sub) { + for (int order = 0; order < 2; order++) { + if (mpctx->d_sub[order] == track->dec_sub) { + mpctx->d_sub[order] = NULL; + osd_set_sub(mpctx->osd, OSDTYPE_SUB + order, NULL); + } + } + sub_destroy(track->dec_sub); + track->dec_sub = NULL; } } } @@ -654,6 +657,8 @@ bool mp_remove_track(struct MPContext *mpctx, struct track *track) struct demuxer *d = track->demuxer; + sub_destroy(track->dec_sub); + int index = 0; while (index < mpctx->num_tracks && mpctx->tracks[index] != track) index++; @@ -674,7 +679,6 @@ bool mp_remove_track(struct MPContext *mpctx, struct track *track) break; } } - uninit_stream_sub_decoders(d); free_demuxer_and_stream(d); } diff --git a/player/sub.c b/player/sub.c index df84e910af..25af9e5c15 100644 --- a/player/sub.c +++ b/player/sub.c @@ -144,22 +144,11 @@ void reset_subtitle_state(struct MPContext *mpctx) reset_subtitles(mpctx, 1); } -void uninit_stream_sub_decoders(struct demuxer *demuxer) -{ - for (int i = 0; i < demux_get_num_stream(demuxer); i++) { - struct sh_stream *sh = demux_get_stream(demuxer, i); - if (sh->sub) { - sub_destroy(sh->sub->dec_sub); - sh->sub->dec_sub = NULL; - } - } -} - void uninit_sub(struct MPContext *mpctx, int order) { if (mpctx->d_sub[order]) { reset_subtitles(mpctx, order); - mpctx->d_sub[order] = NULL; // Note: not free'd. + mpctx->d_sub[order] = NULL; // not destroyed osd_set_sub(mpctx->osd, OSDTYPE_SUB + order, NULL); reselect_demux_streams(mpctx); } @@ -211,8 +200,6 @@ static void update_subtitle(struct MPContext *mpctx, int order) struct sh_stream *sh_stream = track->stream; bool interleaved = is_interleaved(mpctx, track); - assert(sh_stream->sub->dec_sub == dec_sub); - while (1) { if (interleaved && !demux_has_packet(sh_stream)) break; @@ -248,11 +235,12 @@ void update_subtitles(struct MPContext *mpctx) update_subtitle(mpctx, 1); } -static void reinit_subdec(struct MPContext *mpctx, struct track *track, - struct dec_sub *dec_sub) +static void reinit_subdec(struct MPContext *mpctx, struct track *track) { struct MPOpts *opts = mpctx->opts; + struct dec_sub *dec_sub = track->dec_sub; + if (sub_is_initialized(dec_sub)) return; @@ -288,13 +276,11 @@ void reinit_subs(struct MPContext *mpctx, int order) if (!sh) return; - // The decoder is cached in the stream header in order to make ordered - // chapters work better. - if (!sh->sub->dec_sub) - sh->sub->dec_sub = sub_create(mpctx->global); - mpctx->d_sub[order] = sh->sub->dec_sub; + if (!track->dec_sub) + track->dec_sub = sub_create(mpctx->global); + mpctx->d_sub[order] = track->dec_sub; - reinit_subdec(mpctx, track, sh->sub->dec_sub); - osd_set_sub(mpctx->osd, OSDTYPE_SUB + order, sh->sub->dec_sub); - sub_control(sh->sub->dec_sub, SD_CTRL_SET_TOP, &(bool){!!order}); + reinit_subdec(mpctx, track); + osd_set_sub(mpctx->osd, OSDTYPE_SUB + order, track->dec_sub); + sub_control(track->dec_sub, SD_CTRL_SET_TOP, &(bool){!!order}); } -- cgit v1.2.3