Skip to content

Commit

Permalink
loadfile: compute audio lang for sub selection when using lavfi-complex
Browse files Browse the repository at this point in the history
When using lavfi-complex, no single track populates current_track for audio.
We work around this by iterating over the list of tracks and using the first
non-null language from a selected track. If multiple tracks are selected
(i.e. used by the filter) and have conflicting language tags, we'll ignore
them all and maintain the previous behavior (null).
  • Loading branch information
rcombs committed Jul 17, 2023
1 parent 9e9abb1 commit 9e6c6c0
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions player/loadfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,35 @@ static char **process_langs(char **in)
return out;
}

const char *get_audio_lang(struct MPContext *mpctx)
{
// If we have a single current audio track, this is simple.
if (mpctx->current_track[0][STREAM_AUDIO])
return mpctx->current_track[0][STREAM_AUDIO]->lang;

const char *ret = NULL;

// Otherwise, we may be using a filter with multiple inputs.
// Iterate over the tracks and find the ones in use.
for (int i = 0; i < mpctx->num_tracks; i++) {
const struct track *t = mpctx->tracks[i];
if (t->type != STREAM_AUDIO || !t->selected)
continue;

// If we have input in multiple audio languages, bail out;
// we don't have a meaningful single language.
// Partial matches (e.g. en-US vs en-GB) are acceptable here.
if (ret && t->lang && !mp_match_lang_single(t->lang, ret))
return NULL;

// We'll return the first non-null tag we see
if (!ret)
ret = t->lang;
}

return ret;
}

struct track *select_default_track(struct MPContext *mpctx, int order,
enum stream_type type)
{
Expand All @@ -589,9 +618,7 @@ struct track *select_default_track(struct MPContext *mpctx, int order,
if (tid == -2)
return NULL;
char **langs = process_langs(opts->stream_lang[type]);
const char *audio_lang = mpctx->current_track[0][STREAM_AUDIO] ?
mpctx->current_track[0][STREAM_AUDIO]->lang :
NULL;
const char *audio_lang = get_audio_lang(mpctx);
bool audio_matches = match_lang(langs, audio_lang);
int prefer_forced = type == STREAM_SUB && !opts->subs_with_matching_audio && audio_matches;
bool select_fallback = type == STREAM_VIDEO || type == STREAM_AUDIO || (type == STREAM_SUB && opts->subs_fallback == 2);
Expand Down

0 comments on commit 9e6c6c0

Please sign in to comment.