From 1bb1543a88457920caa48cc96cee219cb313840b Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 21 Feb 2016 18:16:41 +0100 Subject: audio: move frame clipping to a generic function --- audio/audio.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'audio/audio.c') diff --git a/audio/audio.c b/audio/audio.c index 5b018e08f6..6f5e1e2df6 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -254,6 +254,39 @@ void mp_audio_skip_samples(struct mp_audio *data, int samples) data->samples -= samples; } +// Clip the given frame to the given timestamp range. Adjusts the frame size +// and timestamp. +void mp_audio_clip_timestamps(struct mp_audio *f, double start, double end) +{ + if (f->pts == MP_NOPTS_VALUE || f->rate < 1) + return; + double f_end = f->pts + f->samples / (double)f->rate; + if (end != MP_NOPTS_VALUE) { + if (f_end >= end) { + if (f->pts >= end) { + f->samples = 0; + } 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; + f->pts = f_end; + } 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 false if the frame data is shared, true otherwise. // Will return true for non-refcounted frames. bool mp_audio_is_writeable(struct mp_audio *data) -- cgit v1.2.3