summaryrefslogtreecommitdiffstats
path: root/video
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 /video
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 'video')
-rw-r--r--video/decode/dec_video.c71
-rw-r--r--video/decode/dec_video.h3
2 files changed, 67 insertions, 7 deletions
diff --git a/video/decode/dec_video.c b/video/decode/dec_video.c
index 0487ac383f..0a33adfc28 100644
--- a/video/decode/dec_video.c
+++ b/video/decode/dec_video.c
@@ -68,6 +68,11 @@ void video_reset(struct dec_video *d_video)
d_video->dropped_frames = 0;
d_video->current_state = DATA_AGAIN;
mp_image_unrefp(&d_video->current_mpi);
+ talloc_free(d_video->packet);
+ d_video->packet = NULL;
+ talloc_free(d_video->new_segment);
+ d_video->new_segment = NULL;
+ d_video->start = d_video->end = MP_NOPTS_VALUE;
}
int video_vd_control(struct dec_video *d_video, int cmd, void *arg)
@@ -88,6 +93,8 @@ void video_uninit(struct dec_video *d_video)
MP_VERBOSE(d_video, "Uninit video.\n");
d_video->vd_driver->uninit(d_video);
}
+ talloc_free(d_video->packet);
+ talloc_free(d_video->new_segment);
talloc_free(d_video);
}
@@ -243,6 +250,9 @@ static struct mp_image *decode_packet(struct dec_video *d_video,
struct MPOpts *opts = d_video->opts;
bool avi_pts = d_video->codec->avi_dts && opts->correct_pts;
+ if (!d_video->vd_driver)
+ return NULL;
+
struct demux_packet packet_copy;
if (packet && packet->dts == MP_NOPTS_VALUE && !avi_pts) {
packet_copy = *packet;
@@ -371,22 +381,37 @@ void video_work(struct dec_video *d_video)
return;
}
- struct demux_packet *pkt;
- if (demux_read_packet_async(d_video->header, &pkt) == 0) {
+ if (!d_video->packet && !d_video->new_segment &&
+ demux_read_packet_async(d_video->header, &d_video->packet) == 0)
+ {
d_video->current_state = DATA_WAIT;
return;
}
+ if (d_video->packet && d_video->packet->new_segment) {
+ assert(!d_video->new_segment);
+ d_video->new_segment = d_video->packet;
+ d_video->packet = NULL;
+ }
+
+ bool had_input_packet = !!d_video->packet;
+ bool had_packet = had_input_packet || d_video->new_segment;
+
+ double start_pts = d_video->start_pts;
+ if (d_video->start != MP_NOPTS_VALUE && (start_pts == MP_NOPTS_VALUE ||
+ d_video->start > start_pts))
+ start_pts = d_video->start;
+
int framedrop_type = d_video->framedrop_enabled ? 1 : 0;
- if (d_video->start_pts != MP_NOPTS_VALUE && pkt &&
- pkt->pts < d_video->start_pts - .005 &&
+ if (start_pts != MP_NOPTS_VALUE && d_video->packet &&
+ d_video->packet->pts < start_pts - .005 &&
!d_video->has_broken_packet_pts)
{
framedrop_type = 2;
}
- d_video->current_mpi = decode_packet(d_video, pkt, framedrop_type);
- bool had_packet = !!pkt;
- talloc_free(pkt);
+ d_video->current_mpi = decode_packet(d_video, d_video->packet, framedrop_type);
+ talloc_free(d_video->packet); // always fully consumed
+ d_video->packet = NULL;
d_video->current_state = DATA_OK;
if (!d_video->current_mpi) {
@@ -397,6 +422,38 @@ void video_work(struct dec_video *d_video)
d_video->current_state = DATA_AGAIN;
}
}
+
+ bool segment_ended = !d_video->current_mpi && !had_input_packet;
+
+ if (d_video->current_mpi && d_video->current_mpi->pts != MP_NOPTS_VALUE) {
+ double vpts = d_video->current_mpi->pts;
+ segment_ended = d_video->end != MP_NOPTS_VALUE && vpts >= d_video->end;
+ if ((start_pts != MP_NOPTS_VALUE && vpts < start_pts) || segment_ended) {
+ talloc_free(d_video->current_mpi);
+ d_video->current_mpi = NULL;
+ }
+ }
+
+ // If there's a new segment, start it as soon as we're drained/finished.
+ if (segment_ended && d_video->new_segment) {
+ struct demux_packet *new_segment = d_video->new_segment;
+ d_video->new_segment = NULL;
+
+ // Could avoid decoder reinit; would still need flush.
+ d_video->codec = new_segment->codec;
+ if (d_video->vd_driver)
+ d_video->vd_driver->uninit(d_video);
+ d_video->vd_driver = NULL;
+ video_init_best_codec(d_video);
+
+ d_video->start = new_segment->start;
+ d_video->end = new_segment->end;
+
+ new_segment->new_segment = false;
+
+ d_video->packet = new_segment;
+ d_video->current_state = DATA_AGAIN;
+ }
}
// Fetch an image decoded with video_work(). Returns one of:
diff --git a/video/decode/dec_video.h b/video/decode/dec_video.h
index d4c12a7bc4..f4646a97d0 100644
--- a/video/decode/dec_video.h
+++ b/video/decode/dec_video.h
@@ -69,6 +69,9 @@ struct dec_video {
float initial_decoder_aspect;
double start_pts;
+ double start, end;
+ struct demux_packet *new_segment;
+ struct demux_packet *packet;
bool framedrop_enabled;
struct mp_image *cover_art_mpi;
struct mp_image *current_mpi;