From 6c02555397ca599a7ec9ea781e492631ecf1c7f2 Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 15 Apr 2020 17:03:37 +0200 Subject: player: slightly improve use of secondary track selection limits Apparently, this was a bit of a mess, which caused the bug fixed by commit ec7f2388af2df. Try to improve this, and only use track selection entries that exist. --- options/options.c | 1 + player/command.c | 6 +++--- player/core.h | 9 ++++++--- player/loadfile.c | 20 +++++++++----------- player/misc.c | 6 ++++++ player/sub.c | 6 +++--- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/options/options.c b/options/options.c index 3536e439a0..881810589d 100644 --- a/options/options.c +++ b/options/options.c @@ -467,6 +467,7 @@ static const m_option_t mp_opts[] = { {"index", OPT_CHOICE(index_mode, {"default", 1}, {"recreate", 0})}, // select audio/video/subtitle stream + // keep in sync with num_ptracks[] and MAX_PTRACKS {"aid", OPT_TRACKCHOICE(stream_id[0][STREAM_AUDIO])}, {"vid", OPT_TRACKCHOICE(stream_id[0][STREAM_VIDEO])}, {"sid", OPT_TRACKCHOICE(stream_id[0][STREAM_SUB])}, diff --git a/player/command.c b/player/command.c index 8c383a930b..c10c4374aa 100644 --- a/player/command.c +++ b/player/command.c @@ -6224,7 +6224,7 @@ void mp_option_change_callback(void *ctx, struct m_config_option *co, int flags, mp_update_logging(mpctx, false); if (flags & (UPDATE_OSD | UPDATE_SUB_FILT)) { - for (int n = 0; n < NUM_PTRACKS; n++) { + for (int n = 0; n < num_ptracks[STREAM_SUB]; n++) { struct track *track = mpctx->current_track[n][STREAM_SUB]; struct dec_sub *sub = track ? track->d_sub : NULL; if (sub) { @@ -6352,8 +6352,8 @@ void mp_option_change_callback(void *ctx, struct m_config_option *co, int flags, if (opt_ptr == &opts->af_settings) set_filters(mpctx, STREAM_AUDIO, opts->af_settings); - for (int order = 0; order < NUM_PTRACKS; order++) { - for (int type = 0; type < STREAM_TYPE_COUNT; type++) { + for (int type = 0; type < STREAM_TYPE_COUNT; type++) { + for (int order = 0; order < num_ptracks[type]; order++) { if (opt_ptr == &opts->stream_id[order][type] && mpctx->playback_initialized) { diff --git a/player/core.h b/player/core.h index a13fa2852b..bbd3f5ce33 100644 --- a/player/core.h +++ b/player/core.h @@ -232,7 +232,10 @@ enum playback_status { const char *mp_status_str(enum playback_status st); -#define NUM_PTRACKS 2 +extern const int num_ptracks[STREAM_TYPE_COUNT]; + +// Maximum of all num_ptracks[] values. +#define MAX_PTRACKS 2 typedef struct MPContext { bool initialized; @@ -309,9 +312,9 @@ typedef struct MPContext { char *track_layout_hash; // Selected tracks. NULL if no track selected. - // There can be NUM_PTRACKS of the same STREAM_TYPE selected at once. + // There can be num_ptracks[type] of the same STREAM_TYPE selected at once. // Currently, this is used for the secondary subtitle track only. - struct track *current_track[NUM_PTRACKS][STREAM_TYPE_COUNT]; + struct track *current_track[MAX_PTRACKS][STREAM_TYPE_COUNT]; struct mp_filter *filter_root; diff --git a/player/loadfile.c b/player/loadfile.c index 5972c634be..8461731518 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -193,8 +193,8 @@ static void kill_demuxers_reentrant(struct MPContext *mpctx, static void uninit_demuxer(struct MPContext *mpctx) { - for (int r = 0; r < NUM_PTRACKS; r++) { - for (int t = 0; t < STREAM_TYPE_COUNT; t++) + for (int t = 0; t < STREAM_TYPE_COUNT; t++) { + for (int r = 0; r < num_ptracks[t]; r++) mpctx->current_track[r][t] = NULL; } @@ -572,7 +572,7 @@ static void check_previous_track_selection(struct MPContext *mpctx) // Reset selection, but only if they're not "auto" or "off". The // defaults are -1 (default selection), or -2 (off) for secondary tracks. for (int t = 0; t < STREAM_TYPE_COUNT; t++) { - for (int i = 0; i < NUM_PTRACKS; i++) { + for (int i = 0; i < num_ptracks[t]; i++) { if (opts->stream_id[i][t] >= 0) { opts->stream_id[i][t] = i == 0 ? -1 : -2; m_config_notify_change_opt_ptr(mpctx->mconfig, @@ -589,10 +589,8 @@ static void check_previous_track_selection(struct MPContext *mpctx) static void mark_track_selection(struct MPContext *mpctx, int order, enum stream_type type, int value) { - assert(order >= 0 && order < NUM_PTRACKS); + assert(order >= 0 && order < num_ptracks[type]); mpctx->opts->stream_id[order][type] = value; - if (type != STREAM_SUB && order != 0) - return; // mconfig only contains one track for vid/aid m_config_notify_change_opt_ptr(mpctx->mconfig, &mpctx->opts->stream_id[order][type]); } @@ -601,7 +599,8 @@ void mp_switch_track_n(struct MPContext *mpctx, int order, enum stream_type type struct track *track, int flags) { assert(!track || track->type == type); - assert(order >= 0 && order < NUM_PTRACKS); + assert(type >= 0 && type < STREAM_TYPE_COUNT); + assert(order >= 0 && order < num_ptracks[type]); // Mark the current track selection as explicitly user-requested. (This is // different from auto-selection or disabling a track due to errors.) @@ -690,7 +689,7 @@ void mp_switch_track(struct MPContext *mpctx, enum stream_type type, void mp_deselect_track(struct MPContext *mpctx, struct track *track) { if (track && track->selected) { - for (int t = 0; t < NUM_PTRACKS; t++) { + for (int t = 0; t < num_ptracks[track->type]; t++) { mp_switch_track_n(mpctx, t, track->type, NULL, 0); mark_track_selection(mpctx, t, track->type, -1); // default } @@ -1525,9 +1524,8 @@ static void play_current_file(struct MPContext *mpctx) if (reinit_complex_filters(mpctx, false) < 0) goto terminate_playback; - assert(NUM_PTRACKS == 2); // opts->stream_id is hardcoded to 2 for (int t = 0; t < STREAM_TYPE_COUNT; t++) { - for (int i = 0; i < NUM_PTRACKS; i++) { + for (int i = 0; i < num_ptracks[t]; i++) { struct track *sel = NULL; bool taken = (t == STREAM_VIDEO && mpctx->vo_chain) || (t == STREAM_AUDIO && mpctx->ao_chain); @@ -1537,7 +1535,7 @@ static void play_current_file(struct MPContext *mpctx) } } for (int t = 0; t < STREAM_TYPE_COUNT; t++) { - for (int i = 0; i < NUM_PTRACKS; i++) { + for (int i = 0; i < num_ptracks[t]; i++) { // One track can strictly feed at most 1 decoder struct track *track = mpctx->current_track[i][t]; if (track) { diff --git a/player/misc.c b/player/misc.c index 4246c1c6f9..e8cebbc2e6 100644 --- a/player/misc.c +++ b/player/misc.c @@ -45,6 +45,12 @@ #include "core.h" #include "command.h" +const int num_ptracks[STREAM_TYPE_COUNT] = { + [STREAM_VIDEO] = 1, + [STREAM_AUDIO] = 1, + [STREAM_SUB] = 2, +}; + double rel_time_to_abs(struct MPContext *mpctx, struct m_rel_time t) { double length = get_time_length(mpctx); diff --git a/player/sub.c b/player/sub.c index 599430ac21..a40a6d5e9d 100644 --- a/player/sub.c +++ b/player/sub.c @@ -39,7 +39,7 @@ // 0: primary sub, 1: secondary sub, -1: not selected static int get_order(struct MPContext *mpctx, struct track *track) { - for (int n = 0; n < NUM_PTRACKS; n++) { + for (int n = 0; n < num_ptracks[STREAM_SUB]; n++) { if (mpctx->current_track[n][STREAM_SUB] == track) return n; } @@ -129,7 +129,7 @@ static bool update_subtitle(struct MPContext *mpctx, double video_pts, bool update_subtitles(struct MPContext *mpctx, double video_pts) { bool ok = true; - for (int n = 0; n < NUM_PTRACKS; n++) + for (int n = 0; n < num_ptracks[STREAM_SUB]; n++) ok &= update_subtitle(mpctx, video_pts, mpctx->current_track[n][STREAM_SUB]); return ok; } @@ -201,6 +201,6 @@ void reinit_sub(struct MPContext *mpctx, struct track *track) void reinit_sub_all(struct MPContext *mpctx) { - for (int n = 0; n < NUM_PTRACKS; n++) + for (int n = 0; n < num_ptracks[STREAM_SUB]; n++) reinit_sub(mpctx, mpctx->current_track[n][STREAM_SUB]); } -- cgit v1.2.3