From 68064f1aa60d30febc69557b982057bb404e4e5e Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Fri, 8 Jul 2016 22:44:23 +0200
Subject: [PATCH] decoder/flac: move duplicate code to flac_data::Initialize()

---
 src/decoder/plugins/FlacCommon.cxx        | 78 ++++++++++++-----------
 src/decoder/plugins/FlacCommon.hxx        | 13 ++--
 src/decoder/plugins/FlacDecoderPlugin.cxx |  9 ---
 3 files changed, 47 insertions(+), 53 deletions(-)

diff --git a/src/decoder/plugins/FlacCommon.cxx b/src/decoder/plugins/FlacCommon.cxx
index 8aa84201d..eaf4d8b2f 100644
--- a/src/decoder/plugins/FlacCommon.cxx
+++ b/src/decoder/plugins/FlacCommon.cxx
@@ -59,6 +59,38 @@ flac_sample_format(unsigned bits_per_sample)
 	}
 }
 
+bool
+flac_data::Initialize(unsigned sample_rate, unsigned bits_per_sample,
+		      unsigned channels, FLAC__uint64 total_frames)
+{
+	assert(!initialized);
+	assert(!unsupported);
+
+	::Error error;
+	if (!audio_format_init_checked(audio_format,
+				       sample_rate,
+				       flac_sample_format(bits_per_sample),
+				       channels, error)) {
+		LogError(error);
+		unsupported = true;
+		return false;
+	}
+
+	frame_size = audio_format.GetFrameSize();
+
+	const auto duration = total_frames > 0
+		? SignedSongTime::FromScale<uint64_t>(total_frames,
+						      audio_format.sample_rate)
+		: SignedSongTime::Negative();
+
+	decoder_initialized(decoder, audio_format,
+			    input_stream.IsSeekable(),
+			    duration);
+
+	initialized = true;
+	return true;
+}
+
 static void
 flac_got_stream_info(struct flac_data *data,
 		     const FLAC__StreamMetadata_StreamInfo *stream_info)
@@ -66,20 +98,10 @@ flac_got_stream_info(struct flac_data *data,
 	if (data->initialized || data->unsupported)
 		return;
 
-	Error error;
-	if (!audio_format_init_checked(data->audio_format,
-				       stream_info->sample_rate,
-				       flac_sample_format(stream_info->bits_per_sample),
-				       stream_info->channels, error)) {
-		LogError(error);
-		data->unsupported = true;
-		return;
-	}
-
-	data->frame_size = data->audio_format.GetFrameSize();
-	data->total_frames = stream_info->total_samples;
-
-	data->initialized = true;
+	data->Initialize(stream_info->sample_rate,
+			 stream_info->bits_per_sample,
+			 stream_info->channels,
+			 stream_info->total_samples);
 }
 
 void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
@@ -123,29 +145,11 @@ flac_got_first_frame(struct flac_data *data, const FLAC__FrameHeader *header)
 	if (data->unsupported)
 		return false;
 
-	Error error;
-	if (!audio_format_init_checked(data->audio_format,
-				       header->sample_rate,
-				       flac_sample_format(header->bits_per_sample),
-				       header->channels, error)) {
-		LogError(error);
-		data->unsupported = true;
-		return false;
-	}
-
-	data->frame_size = data->audio_format.GetFrameSize();
-
-	const auto duration = SongTime::FromScale<uint64_t>(data->total_frames,
-							    data->audio_format.sample_rate);
-
-	decoder_initialized(data->decoder, data->audio_format,
-			    data->input_stream.IsSeekable(),
-			    duration);
-
-	data->total_frames = 0; /* unkown duration */
-	data->initialized = true;
-
-	return true;
+	return data->Initialize(header->sample_rate,
+				header->bits_per_sample,
+				header->channels,
+				/* unknown duration */
+				0);
 }
 
 FLAC__StreamDecoderWriteStatus
diff --git a/src/decoder/plugins/FlacCommon.hxx b/src/decoder/plugins/FlacCommon.hxx
index 2d424cbe5..593a5addc 100644
--- a/src/decoder/plugins/FlacCommon.hxx
+++ b/src/decoder/plugins/FlacCommon.hxx
@@ -54,13 +54,6 @@ struct flac_data : public FlacInput {
 	 */
 	AudioFormat audio_format;
 
-	/**
-	 * The total number of frames in this song.  0 means unknown.
-	 *
-	 * This attribute is defined if "initialized" is true.
-	 */
-	FLAC__uint64 total_frames;
-
 	/**
 	 * End of last frame's position within the stream.  This is
 	 * used for bit rate calculations.
@@ -73,6 +66,12 @@ struct flac_data : public FlacInput {
 	Tag tag;
 
 	flac_data(Decoder &decoder, InputStream &input_stream);
+
+	/**
+	 * Wrapper for decoder_initialized().
+	 */
+	bool Initialize(unsigned sample_rate, unsigned bits_per_sample,
+			unsigned channels, FLAC__uint64 total_frames);
 };
 
 void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
diff --git a/src/decoder/plugins/FlacDecoderPlugin.cxx b/src/decoder/plugins/FlacDecoderPlugin.cxx
index 25049dfb7..35bb041f0 100644
--- a/src/decoder/plugins/FlacDecoderPlugin.cxx
+++ b/src/decoder/plugins/FlacDecoderPlugin.cxx
@@ -141,15 +141,6 @@ flac_decoder_initialize(struct flac_data *data, FLAC__StreamDecoder *sd)
 
 	if (data->initialized) {
 		/* done */
-
-		const auto duration2 = data->total_frames > 0
-			? SignedSongTime::FromScale<uint64_t>(data->total_frames,
-							      data->audio_format.sample_rate)
-			: SignedSongTime::Negative();
-
-		decoder_initialized(data->decoder, data->audio_format,
-				    data->input_stream.IsSeekable(),
-				    duration2);
 		return true;
 	}