summaryrefslogtreecommitdiffstats
path: root/mplayer.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2012-08-30 16:43:31 +0200
committerwm4 <wm4@nowhere>2012-09-18 21:04:45 +0200
commitd21b109bf7d3f36c13298d794c856eff834946c3 (patch)
tree86e8bfcdcc61aa75af10402e6b921bc5ad21224a /mplayer.c
parentfbc424ef35d4c76235feb3aa8295bcb8a3ce9443 (diff)
downloadmpv-d21b109bf7d3f36c13298d794c856eff834946c3.tar.bz2
mpv-d21b109bf7d3f36c13298d794c856eff834946c3.tar.xz
core: fix DVD subtitle selection
Add all subtitle tracks as reported by libdvdread at playback start. Display language for subtitle and audio tracks. This commit restores these features to the state when demux_mpg was default for DVD playback, and makes them work with demux_lavf and the recent changes to subtitle selection in the frontend. demux_mpg, which was the default demuxer for DVD playback, reordered the subtitle streams according to the "logical" subtitle track number, which conforms to the track layout reported by libdvdread, and is what stream_dvd expects for the STREAM_CTRL_GET_LANG call. demux_lavf, on the other hand, adds the streams in the order it encounters them in the MPEG stream. It seems this order is essentially random, and can't be mapped easily to what stream_dvd expects. Solve this by making demux_lavf hand out the MPEG stream IDs (using the demuxer_id field). The MPEG IDs are mapped by mplayer.c by special casing DVD playback (map_id_from/to_demuxer() functions). This mapping is essentially the same what demux_mpg did. Making demux_lavf reorder the streams is out of the question, because its stream handling is already messy enough. (Note that demux_lavf doesn't export stream IDs for other formats, because most time libavformat demuxers do not set AVStream.id, and we don't know which demuxers do. But we know that MPEG is safe.) Another major complication is that subtitle tracks are added lazily, as soon as the demuxer encounters the first subtitle packet for a given subtitle stream. Add the streams in advance. If a yet non-existent stream is selected, demux_lavf must be made to auto-select that subtitle stream as soon as it is added. Otherwise, the first subtitle packet would be lost. This is done by DEMUXER_CTRL_PRESELECT_SUBTITLE. demux_mpg didn't need this: the frontend code could just set ds->id to the desired stream number. But demux_lavf's stream IDs don't map directly to the stream number as used by libdvdread, which is why this hack is needed.
Diffstat (limited to 'mplayer.c')
-rw-r--r--mplayer.c87
1 files changed, 81 insertions, 6 deletions
diff --git a/mplayer.c b/mplayer.c
index 0bf52dc9ab..b17b13c9c3 100644
--- a/mplayer.c
+++ b/mplayer.c
@@ -252,6 +252,7 @@ int use_filedir_conf;
#include "metadata.h"
static void reset_subtitles(struct MPContext *mpctx);
+static void reinit_subs(struct MPContext *mpctx);
static float get_relative_time(struct MPContext *mpctx)
{
@@ -941,6 +942,20 @@ static int find_new_tid(struct MPContext *mpctx, enum stream_type t)
return new_id + 1;
}
+// Map stream number (as used by libdvdread) to MPEG IDs (as used by demuxer).
+static int map_id_from_demuxer(struct demuxer *d, enum stream_type type, int id)
+{
+ if (d->stream->type == STREAMTYPE_DVD && type == STREAM_SUB)
+ id = id & 0x1F;
+ return id;
+}
+static int map_id_to_demuxer(struct demuxer *d, enum stream_type type, int id)
+{
+ if (d->stream->type == STREAMTYPE_DVD && type == STREAM_SUB)
+ id = id | 0x20;
+ return id;
+}
+
static struct track *add_stream_track(struct MPContext *mpctx,
struct sh_stream *stream,
bool under_timeline)
@@ -949,7 +964,21 @@ static struct track *add_stream_track(struct MPContext *mpctx,
struct track *track = mpctx->tracks[i];
if (track->stream == stream)
return track;
+ // DVD subtitle track that was added later
+ if (stream->type == STREAM_SUB && track->type == STREAM_SUB &&
+ map_id_from_demuxer(stream->demuxer, stream->type,
+ stream->demuxer_id) == track->demuxer_id
+ && !track->stream)
+ {
+ track->stream = stream;
+ track->demuxer_id = stream->demuxer_id;
+ // Initialize lazily selected track
+ if (track == mpctx->current_track[STREAM_SUB])
+ reinit_subs(mpctx);
+ return track;
+ }
}
+
struct track *track = talloc_ptrtype(NULL, track);
*track = (struct track) {
.type = stream->type,
@@ -964,12 +993,16 @@ static struct track *add_stream_track(struct MPContext *mpctx,
};
MP_TARRAY_APPEND(mpctx, mpctx->tracks, mpctx->num_tracks, track);
- // Needed for DVD and Blu-Ray. (Note that at least with DVDs and demux_lavf,
- // this code is broken: unlike demux_mpg, the demuxer streams are not
- // directly mapped to MPEG stream IDs.)
- if (!track->lang)
- track->lang = talloc_steal(track, demuxer_stream_lang(track->demuxer,
- track->stream));
+ // Needed for DVD and Blu-ray.
+ if (!track->lang) {
+ struct stream_lang_req req = {
+ .type = track->type,
+ .id = map_id_from_demuxer(track->demuxer, track->type,
+ track->demuxer_id)
+ };
+ stream_control(track->demuxer->stream, STREAM_CTRL_GET_LANG, &req);
+ track->lang = talloc_steal(track, req.name);
+ }
return track;
}
@@ -980,6 +1013,31 @@ static void add_demuxer_tracks(struct MPContext *mpctx, struct demuxer *demuxer)
add_stream_track(mpctx, demuxer->streams[n], !!mpctx->timeline);
}
+static void add_dvd_tracks(struct MPContext *mpctx)
+{
+#ifdef CONFIG_DVDREAD
+ struct demuxer *demuxer = mpctx->demuxer;
+ struct stream *stream = demuxer->stream;
+ if (stream->type == STREAMTYPE_DVD) {
+ int n_subs = dvd_number_of_subs(stream);
+ for (int n = 0; n < n_subs; n++) {
+ struct track *track = talloc_ptrtype(NULL, track);
+ *track = (struct track) {
+ .type = STREAM_SUB,
+ .user_tid = find_new_tid(mpctx, STREAM_SUB),
+ .demuxer_id = n,
+ .demuxer = mpctx->demuxer,
+ };
+ MP_TARRAY_APPEND(mpctx, mpctx->tracks, mpctx->num_tracks, track);
+
+ struct stream_lang_req req = {.type = STREAM_SUB, .id = n};
+ stream_control(stream, STREAM_CTRL_GET_LANG, &req);
+ track->lang = talloc_steal(track, req.name);
+ }
+ }
+#endif
+}
+
void add_subtitles(struct MPContext *mpctx, char *filename, float fps,
int noerr)
{
@@ -1901,6 +1959,22 @@ static void reinit_subs(struct MPContext *mpctx)
if (!track)
return;
+ if (track->demuxer && !track->stream) {
+ // Lazily added DVD track - we must not miss the first subtitle packet,
+ // which makes the demuxer create the sh_stream, and contains the first
+ // subtitle event.
+
+ // demux_mpg - maps IDs directly to the logical stream number
+ track->demuxer->sub->id = track->demuxer_id;
+
+ // demux_lavf - IDs are essentially random, have to use MPEG IDs
+ int id = map_id_to_demuxer(track->demuxer, track->type,
+ track->demuxer_id);
+ demux_control(track->demuxer, DEMUXER_CTRL_AUTOSELECT_SUBTITLE, &id);
+
+ return;
+ }
+
mpctx->initialized_flags |= INITIALIZED_SUB;
if (track->vobsub_id_plus_one) {
@@ -3795,6 +3869,7 @@ goto_enable_cache:
// On the contrary, the EDL and CUE demuxers are empty wrappers.
mpctx->demuxer = mpctx->timeline[0].source;
}
+ add_dvd_tracks(mpctx);
add_demuxer_tracks(mpctx, mpctx->demuxer);
mpctx->timeline_part = 0;