From 0673c9a84dbbca60ed4627154e824dbfdeed269b Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Sat, 12 Apr 2008 04:20:06 +0000
Subject: calculate bytes_per_channel, check for buffer flush once

Check for flushing the chunk buffer only once per sample, before
iterating over channels and bytes.  This saves another 5% CPU cycles.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7326 09075e82-0dd4-0310-85a5-a0d7c8717e4f
---
 src/inputPlugins/flac_plugin.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

(limited to 'src')

diff --git a/src/inputPlugins/flac_plugin.c b/src/inputPlugins/flac_plugin.c
index fa6304980..16f63dda0 100644
--- a/src/inputPlugins/flac_plugin.c
+++ b/src/inputPlugins/flac_plugin.c
@@ -217,6 +217,8 @@ static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
 	unsigned char *uc;
 	unsigned int c_samp, c_chan;
 	const unsigned int bytes_per_sample = (data->dc->audioFormat.bits / 8);
+	const unsigned int bytes_per_channel =
+		bytes_per_sample * frame->header.channels;
 	unsigned int i;
 	float timeChange;
 	FLAC__uint64 newPosition = 0;
@@ -238,22 +240,23 @@ static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
 	data->position = newPosition;
 
 	for (c_samp = 0; c_samp < frame->header.blocksize; c_samp++) {
+		if (data->chunk_length + bytes_per_channel >= FLAC_CHUNK_SIZE) {
+			if (flacSendChunk(data) < 0) {
+				return
+					FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+			}
+			data->chunk_length = 0;
+			if (data->dc->seek) {
+				return
+					FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
+			}
+		}
+
 		for (c_chan = 0; c_chan < frame->header.channels;
 		     c_chan++) {
 			u16 = buf[c_chan][c_samp];
 			uc = (unsigned char *)&u16;
 			for (i = 0; i < bytes_per_sample; i++) {
-				if (data->chunk_length >= FLAC_CHUNK_SIZE) {
-					if (flacSendChunk(data) < 0) {
-						return
-						    FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
-					}
-					data->chunk_length = 0;
-					if (data->dc->seek) {
-						return
-						    FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
-					}
-				}
 				data->chunk[data->chunk_length++] = *(uc++);
 			}
 		}
-- 
cgit v1.2.3