From c2882c3d812e156dc77554c083530cd5fd9ed1ca Mon Sep 17 00:00:00 2001 From: Filip Hejsek Date: Fri, 23 Jan 2026 21:05:00 +0100 Subject: [PATCH] player/loadfile: never autoselect tracks with wrong PID Currently, we prefer selecting a track that matches the video program ID, but can fall back on a track with a different program ID when there is no such track or when no such track matches subs-fallback criteria. Change this to instead select no track when only tracks with wrong PID are available. Program ID will now act as a filter, not as a preference, and only files with either matching or unset PID will be considered for autoselect. Also, ignore the program ID of external tracks, as it doesn't make much sense to compare PIDs from different files. --- player/loadfile.c | 12 +++++------- test/libmpv_test_track_selection.c | 9 +++++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/player/loadfile.c b/player/loadfile.c index e73f6f9f83..1071ec2488 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -473,7 +473,7 @@ void add_demuxer_tracks(struct MPContext *mpctx, struct demuxer *demuxer) */ // Return whether t1 is preferred over t2 static bool compare_track(struct track *t1, struct track *t2, char **langs, bool os_langs, - struct MPOpts *opts, int preferred_program) + struct MPOpts *opts) { bool sub = t2->type == STREAM_SUB; bool ext1 = t1->is_external && !t1->no_default; @@ -486,11 +486,6 @@ static bool compare_track(struct track *t1, struct track *t2, char **langs, bool } if (t1->auto_loaded != t2->auto_loaded) return !t1->auto_loaded; - if (preferred_program != -1 && t1->program_id != -1 && t2->program_id != -1) { - if ((t1->program_id == preferred_program) != - (t2->program_id == preferred_program)) - return t1->program_id == preferred_program; - } int l1 = mp_match_lang(langs, t1->lang), l2 = mp_match_lang(langs, t2->lang); if (!os_langs && l1 != l2) return l1 > l2; @@ -605,6 +600,9 @@ struct track *select_default_track(struct MPContext *mpctx, int order, continue; if (!opts->autoload_files && track->is_external) continue; + if (preferred_program != -1 && !track->is_external && + track->program_id != -1 && track->program_id != preferred_program) + continue; if (duplicate_track(mpctx, order, type, track)) continue; if (sub) { @@ -623,7 +621,7 @@ struct track *select_default_track(struct MPContext *mpctx, int order, continue; track->forced_select = forced; } - if (!pick || compare_track(track, pick, langs, os_langs, mpctx->opts, preferred_program)) { + if (!pick || compare_track(track, pick, langs, os_langs, mpctx->opts)) { pick = track; } } diff --git a/test/libmpv_test_track_selection.c b/test/libmpv_test_track_selection.c index 85f5cc553e..9a517aafb7 100644 --- a/test/libmpv_test_track_selection.c +++ b/test/libmpv_test_track_selection.c @@ -309,12 +309,13 @@ static void test_track_selection(char *file, char *path) check_string("track-list/1/selected", "no"); check_string("track-list/3/selected", "no"); - // because subs-fallback=default, the jpn track cannot be chosen - // so we choose the eng track despite it having the wrong program ID + // the eng track cannot be selected due to having the wrong program ID + // because subs-fallback=default, the jpn track cannot be chosen either + // so we get no subs set_property_string("slang", "eng"); reload_file(path); - check_string("current-tracks/sub/lang", "eng"); - check_string("track-list/1/selected", "yes"); + check_string("track-list/1/selected", "no"); + check_string("track-list/3/selected", "no"); // the jpn track is selected to match video program ID // even though the user requested eng subtitles