summaryrefslogtreecommitdiffstats
path: root/sub
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2016-02-15 21:04:07 +0100
committerwm4 <wm4@nowhere>2016-02-15 21:04:07 +0100
commit0af5335383887cda7650d4b33bc42759c1a5891f (patch)
tree356f5a1817f42ab5a24472f36234cd41c81cdd17 /sub
parentae55896f42329b29619bcf4ade6bb83c6940a0f8 (diff)
downloadmpv-0af5335383887cda7650d4b33bc42759c1a5891f.tar.bz2
mpv-0af5335383887cda7650d4b33bc42759c1a5891f.tar.xz
Rewrite ordered chapters and timeline stuff
This uses a different method to piece segments together. The old approach basically changes to a new file (with a new start offset) any time a segment ends. This meant waiting for audio/video end on segment end, and then changing to the new segment all at once. It had a very weird impact on the playback core, and some things (like truly gapless segment transitions, or frame backstepping) just didn't work. The new approach adds the demux_timeline pseudo-demuxer, which presents an uniform packet stream from the many segments. This is pretty similar to how ordered chapters are implemented everywhere else. It also reminds of the FFmpeg concat pseudo-demuxer. The "pure" version of this approach doesn't work though. Segments can actually have different codec configurations (different extradata), and subtitles are most likely broken too. (Subtitles have multiple corner cases which break the pure stream-concatenation approach completely.) To counter this, we do two things: - Reinit the decoder with each segment. We go as far as allowing concatenating files with completely different codecs for the sake of EDL (which also uses the timeline infrastructure). A "lighter" approach would try to make use of decoder mechanism to update e.g. the extradata, but that seems fragile. - Clip decoded data to segment boundaries. This is equivalent to normal playback core mechanisms like hr-seek, but now the playback core doesn't need to care about these things. These two mechanisms are equivalent to what happened in the old implementation, except they don't happen in the playback core anymore. In other words, the playback core is completely relieved from timeline implementation details. (Which honestly is exactly what I'm trying to do here. I don't think ordered chapter behavior deserves improvement, even if it's bad - but I want to get it out from the playback core.) There is code duplication between audio and video decoder common code. This is awful and could be shareable - but this will happen later. Note that the audio path has some code to clip audio frames for the purpose of codec preroll/gapless handling, but it's not shared as sharing it would cause more pain than it would help.
Diffstat (limited to 'sub')
-rw-r--r--sub/dec_sub.c59
1 files changed, 57 insertions, 2 deletions
diff --git a/sub/dec_sub.c b/sub/dec_sub.c
index 5a952e4fd5..58562140fa 100644
--- a/sub/dec_sub.c
+++ b/sub/dec_sub.c
@@ -50,12 +50,17 @@ struct dec_sub {
struct MPOpts *opts;
struct demuxer *demuxer;
- struct mp_codec_params *codec;
struct sh_stream *sh;
double last_pkt_pts;
+ struct mp_codec_params *codec;
+ double start, end;
+
+ double last_vo_pts;
struct sd *sd;
+
+ struct demux_packet *new_segment;
};
void sub_lock(struct dec_sub *sub)
@@ -121,6 +126,9 @@ struct dec_sub *sub_create(struct mpv_global *global, struct demuxer *demuxer,
.codec = sh->codec,
.demuxer = demuxer,
.last_pkt_pts = MP_NOPTS_VALUE,
+ .last_vo_pts = MP_NOPTS_VALUE,
+ .start = MP_NOPTS_VALUE,
+ .end = MP_NOPTS_VALUE,
};
mpthread_mutex_init_recursive(&sub->lock);
@@ -132,6 +140,31 @@ struct dec_sub *sub_create(struct mpv_global *global, struct demuxer *demuxer,
return NULL;
}
+// Called locked.
+static void update_segment(struct dec_sub *sub)
+{
+ if (sub->new_segment && sub->last_vo_pts != MP_NOPTS_VALUE &&
+ sub->last_vo_pts >= sub->new_segment->start)
+ {
+ sub->codec = sub->new_segment->codec;
+ sub->start = sub->new_segment->start;
+ sub->end = sub->new_segment->end;
+ struct sd *new = init_decoder(sub);
+ if (new) {
+ sub->sd->driver->uninit(sub->sd);
+ talloc_free(sub->sd);
+ sub->sd = new;
+ } else {
+ // We'll just keep the current decoder, and feed it possibly
+ // invalid data (not our fault if it crashes or something).
+ MP_ERR(sub, "Can't change to new codec.\n");
+ }
+ sub->sd->driver->decode(sub->sd, sub->new_segment);
+ talloc_free(sub->new_segment);
+ sub->new_segment = NULL;
+ }
+}
+
// Read all packets from the demuxer and decode/add them. Returns false if
// there are circumstances which makes this not possible.
bool sub_read_all_packets(struct dec_sub *sub)
@@ -170,6 +203,9 @@ bool sub_read_packets(struct dec_sub *sub, double video_pts)
if (!read_more)
break;
+ if (sub->new_segment)
+ break;
+
struct demux_packet *pkt;
int st = demux_read_packet_async(sub->sh, &pkt);
// Note: "wait" (st==0) happens with non-interleaved streams only, and
@@ -183,8 +219,16 @@ bool sub_read_packets(struct dec_sub *sub, double video_pts)
break;
}
- sub->sd->driver->decode(sub->sd, pkt);
sub->last_pkt_pts = pkt->pts;
+
+ if (pkt->new_segment) {
+ sub->new_segment = pkt;
+ // Note that this can be delayed to a much later point in time.
+ update_segment(sub);
+ break;
+ }
+
+ sub->sd->driver->decode(sub->sd, pkt);
talloc_free(pkt);
}
pthread_mutex_unlock(&sub->lock);
@@ -199,6 +243,9 @@ void sub_get_bitmaps(struct dec_sub *sub, struct mp_osd_res dim, double pts,
{
struct MPOpts *opts = sub->opts;
+ sub->last_vo_pts = pts;
+ update_segment(sub);
+
*res = (struct sub_bitmaps) {0};
if (opts->sub_visibility && sub->sd->driver->get_bitmaps)
sub->sd->driver->get_bitmaps(sub->sd, dim, pts, res);
@@ -212,6 +259,10 @@ char *sub_get_text(struct dec_sub *sub, double pts)
pthread_mutex_lock(&sub->lock);
struct MPOpts *opts = sub->opts;
char *text = NULL;
+
+ sub->last_vo_pts = pts;
+ update_segment(sub);
+
if (opts->sub_visibility && sub->sd->driver->get_text)
text = sub->sd->driver->get_text(sub->sd, pts);
pthread_mutex_unlock(&sub->lock);
@@ -224,6 +275,10 @@ void sub_reset(struct dec_sub *sub)
if (sub->sd->driver->reset)
sub->sd->driver->reset(sub->sd);
sub->last_pkt_pts = MP_NOPTS_VALUE;
+ sub->start = sub->end = MP_NOPTS_VALUE;
+ sub->last_vo_pts = MP_NOPTS_VALUE;
+ talloc_free(sub->new_segment);
+ sub->new_segment = NULL;
pthread_mutex_unlock(&sub->lock);
}