From 0246082b9b91f184657257444250575de94129e5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 10 Jul 2016 21:47:17 +0200 Subject: [PATCH] decoder/flac: move position code to FlacDecoder::GetDeltaPosition() --- src/decoder/plugins/FlacCommon.cxx | 18 ++++++++++++++++++ src/decoder/plugins/FlacCommon.hxx | 6 ++++++ src/decoder/plugins/FlacDecoderPlugin.cxx | 15 ++------------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/decoder/plugins/FlacCommon.cxx b/src/decoder/plugins/FlacCommon.cxx index b70b87f81..943225328 100644 --- a/src/decoder/plugins/FlacCommon.cxx +++ b/src/decoder/plugins/FlacCommon.cxx @@ -151,6 +151,24 @@ flac_got_first_frame(FlacDecoder *data, const FLAC__FrameHeader *header) 0); } +FLAC__uint64 +FlacDecoder::GetDeltaPosition(const FLAC__StreamDecoder &sd) +{ + FLAC__uint64 nbytes; + if (!FLAC__stream_decoder_get_decode_position(&sd, &nbytes)) + return 0; + + if (position > 0 && nbytes > position) { + nbytes -= position; + position += nbytes; + } else { + position = nbytes; + nbytes = 0; + } + + return nbytes; +} + FLAC__StreamDecoderWriteStatus flac_common_write(FlacDecoder *data, const FLAC__Frame * frame, const FLAC__int32 *const buf[], diff --git a/src/decoder/plugins/FlacCommon.hxx b/src/decoder/plugins/FlacCommon.hxx index 6e86faaa5..6d0eddd79 100644 --- a/src/decoder/plugins/FlacCommon.hxx +++ b/src/decoder/plugins/FlacCommon.hxx @@ -69,6 +69,12 @@ struct FlacDecoder : public FlacInput { */ bool Initialize(unsigned sample_rate, unsigned bits_per_sample, unsigned channels, FLAC__uint64 total_frames); + + /** + * Calculate the delta (in bytes) between the last frame and + * the current frame. + */ + FLAC__uint64 GetDeltaPosition(const FLAC__StreamDecoder &sd); }; void flac_metadata_common_cb(const FLAC__StreamMetadata * block, diff --git a/src/decoder/plugins/FlacDecoderPlugin.cxx b/src/decoder/plugins/FlacDecoderPlugin.cxx index d8a44ea5e..81481625f 100644 --- a/src/decoder/plugins/FlacDecoderPlugin.cxx +++ b/src/decoder/plugins/FlacDecoderPlugin.cxx @@ -65,20 +65,9 @@ flac_write_cb(const FLAC__StreamDecoder *dec, const FLAC__Frame *frame, const FLAC__int32 *const buf[], void *vdata) { FlacDecoder *data = (FlacDecoder *) vdata; - FLAC__uint64 nbytes = 0; - if (FLAC__stream_decoder_get_decode_position(dec, &nbytes)) { - if (data->position > 0 && nbytes > data->position) { - nbytes -= data->position; - data->position += nbytes; - } else { - data->position = nbytes; - nbytes = 0; - } - } else - nbytes = 0; - - return flac_common_write(data, frame, buf, nbytes); + return flac_common_write(data, frame, buf, + data->GetDeltaPosition(*dec)); } static bool