summaryrefslogtreecommitdiffstats
path: root/audio/decode
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 /audio/decode
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 'audio/decode')
-rw-r--r--audio/decode/dec_audio.c74
-rw-r--r--audio/decode/dec_audio.h2
2 files changed, 75 insertions, 1 deletions
diff --git a/audio/decode/dec_audio.c b/audio/decode/dec_audio.c
index 6d5210d179..c2272c5242 100644
--- a/audio/decode/dec_audio.c
+++ b/audio/decode/dec_audio.c
@@ -168,6 +168,9 @@ void audio_reset_decoding(struct dec_audio *d_audio)
d_audio->current_frame = NULL;
talloc_free(d_audio->packet);
d_audio->packet = NULL;
+ talloc_free(d_audio->new_segment);
+ d_audio->new_segment = NULL;
+ d_audio->start = d_audio->end = MP_NOPTS_VALUE;
}
static void fix_audio_pts(struct dec_audio *da)
@@ -192,6 +195,38 @@ static void fix_audio_pts(struct dec_audio *da)
da->pts += da->current_frame->samples / (double)da->current_frame->rate;
}
+static bool clip_frame(struct mp_audio *f, double start, double end)
+{
+ if (f->pts == MP_NOPTS_VALUE)
+ return false;
+ double f_end = f->pts + f->samples / (double)f->rate;
+ bool ended = false;
+ if (end != MP_NOPTS_VALUE) {
+ if (f_end >= end) {
+ if (f->pts >= end) {
+ f->samples = 0;
+ ended = true;
+ } else {
+ int new = (end - f->pts) * f->rate;
+ f->samples = MPCLAMP(new, 0, f->samples);
+ }
+ }
+ }
+ if (start != MP_NOPTS_VALUE) {
+ if (f->pts < start) {
+ if (f_end <= start) {
+ f->samples = 0;
+ } else {
+ int skip = (start - f->pts) * f->rate;
+ skip = MPCLAMP(skip, 0, f->samples);
+ mp_audio_skip_samples(f, skip);
+ f->pts += skip / (double)f->rate;
+ }
+ }
+ }
+ return ended;
+}
+
void audio_work(struct dec_audio *da)
{
if (da->current_frame)
@@ -202,7 +237,13 @@ void audio_work(struct dec_audio *da)
return;
}
- bool had_packet = !!da->packet;
+ if (da->packet && da->packet->new_segment) {
+ assert(!da->new_segment);
+ da->new_segment = da->packet;
+ da->packet = NULL;
+ }
+
+ bool had_packet = da->packet || da->new_segment;
int ret = da->ad_driver->decode_packet(da, da->packet, &da->current_frame);
if (ret < 0 || (da->packet && da->packet->len == 0)) {
@@ -223,6 +264,37 @@ void audio_work(struct dec_audio *da)
}
fix_audio_pts(da);
+
+ bool segment_end = true;
+
+ if (da->current_frame) {
+ segment_end = clip_frame(da->current_frame, da->start, da->end);
+ if (da->current_frame->samples == 0) {
+ talloc_free(da->current_frame);
+ da->current_frame = NULL;
+ }
+ }
+
+ // If there's a new segment, start it as soon as we're drained/finished.
+ if (segment_end && da->new_segment) {
+ struct demux_packet *new_segment = da->new_segment;
+ da->new_segment = NULL;
+
+ // Could avoid decoder reinit; would still need flush.
+ da->codec = new_segment->codec;
+ if (da->ad_driver)
+ da->ad_driver->uninit(da);
+ da->ad_driver = NULL;
+ audio_init_best_codec(da);
+
+ da->start = new_segment->start;
+ da->end = new_segment->end;
+
+ new_segment->new_segment = false;
+
+ da->packet = new_segment;
+ da->current_state = DATA_AGAIN;
+ }
}
// Fetch an audio frame decoded with audio_work(). Returns one of:
diff --git a/audio/decode/dec_audio.h b/audio/decode/dec_audio.h
index 88fc40aec9..7bc8b00b0f 100644
--- a/audio/decode/dec_audio.h
+++ b/audio/decode/dec_audio.h
@@ -43,7 +43,9 @@ struct dec_audio {
// Strictly internal (dec_audio.c).
double pts; // endpts of previous frame
+ double start, end;
struct demux_packet *packet;
+ struct demux_packet *new_segment;
struct mp_audio *current_frame;
int current_state;
};