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 +++++++++++++++++++++++++++++++++ audio/audio.h | 1 + audio/decode/dec_audio.c | 36 +++--------------------------------- 3 files changed, 37 insertions(+), 33 deletions(-) (limited to 'audio') 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) diff --git a/audio/audio.h b/audio/audio.h index a4cce55af7..c469f7a21e 100644 --- a/audio/audio.h +++ b/audio/audio.h @@ -72,6 +72,7 @@ void mp_audio_copy(struct mp_audio *dst, int dst_offset, struct mp_audio *src, int src_offset, int length); void mp_audio_copy_attributes(struct mp_audio *dst, struct mp_audio *src); void mp_audio_skip_samples(struct mp_audio *data, int samples); +void mp_audio_clip_timestamps(struct mp_audio *f, double start, double end); bool mp_audio_is_writeable(struct mp_audio *data); int mp_audio_make_writeable(struct mp_audio *data); diff --git a/audio/decode/dec_audio.c b/audio/decode/dec_audio.c index c2272c5242..e60ebe370f 100644 --- a/audio/decode/dec_audio.c +++ b/audio/decode/dec_audio.c @@ -195,38 +195,6 @@ 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) @@ -268,7 +236,9 @@ void audio_work(struct dec_audio *da) bool segment_end = true; if (da->current_frame) { - segment_end = clip_frame(da->current_frame, da->start, da->end); + mp_audio_clip_timestamps(da->current_frame, da->start, da->end); + if (da->current_frame->pts != MP_NOPTS_VALUE && da->start != MP_NOPTS_VALUE) + segment_end = da->current_frame->pts >= da->start; if (da->current_frame->samples == 0) { talloc_free(da->current_frame); da->current_frame = NULL; -- cgit v1.2.3