diff options
author | wm4 <wm4@nowhere> | 2016-02-21 18:16:41 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2016-02-21 18:16:41 +0100 |
commit | 1bb1543a88457920caa48cc96cee219cb313840b (patch) | |
tree | 95b331a9290a855995e919e67dd602f9dd7bf236 /audio/audio.c | |
parent | 943f76e6ce979ce1e1e7e29e4e20f8ec56682df7 (diff) | |
download | mpv-1bb1543a88457920caa48cc96cee219cb313840b.tar.bz2 mpv-1bb1543a88457920caa48cc96cee219cb313840b.tar.xz |
audio: move frame clipping to a generic function
Diffstat (limited to 'audio/audio.c')
-rw-r--r-- | audio/audio.c | 33 |
1 files changed, 33 insertions, 0 deletions
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) |