Bulk update copyright dates
[flac.git] / src / plugin_common / dither.c
index 4a54e2d..b20ab51 100644 (file)
@@ -1,5 +1,6 @@
 /* plugin_common - Routines common to several plugins
 /* plugin_common - Routines common to several plugins
- * Copyright (C) 2002  Josh Coalson
+ * Copyright (C) 2002-2009  Josh Coalson
+ * Copyright (C) 2011-2016  Xiph.Org Foundation
  *
  * dithering routine derived from (other GPLed source):
  * mad - MPEG audio decoder
  *
  * dithering routine derived from (other GPLed source):
  * mad - MPEG audio decoder
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
  */
 
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include "dither.h"
 #include "FLAC/assert.h"
 
 #include "dither.h"
 #include "FLAC/assert.h"
 
 #endif
 #define max(a,b) ((a)>(b)?(a):(b))
 
 #endif
 #define max(a,b) ((a)>(b)?(a):(b))
 
-
-#if defined _MSC_VER || defined __MINGW32__
-#define FLAC__INLINE __inline
-#else
-#define FLAC__INLINE
+#ifndef FLaC__INLINE
+#define FLaC__INLINE
 #endif
 
 #endif
 
-/* 32-bit pseudo-random number generator */
-static FLAC__INLINE FLAC__uint32 prng(FLAC__uint32 state)
+
+/* 32-bit pseudo-random number generator
+ *
+ * @@@ According to Miroslav, this one is poor quality, the one from the
+ * @@@ original replaygain code is much better
+ */
+static FLaC__INLINE FLAC__uint32 prng(FLAC__uint32 state)
 {
        return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
 }
 {
        return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
 }
@@ -48,7 +55,7 @@ typedef struct {
        FLAC__int32 random;
 } dither_state;
 
        FLAC__int32 random;
 } dither_state;
 
-static FLAC__INLINE FLAC__int32 linear_dither(unsigned source_bps, unsigned target_bps, FLAC__int32 sample, dither_state *dither, const FLAC__int32 MIN, const FLAC__int32 MAX)
+static FLAC__int32 linear_dither(unsigned source_bps, unsigned target_bps, FLAC__int32 sample, dither_state *dither, const FLAC__int32 MIN, const FLAC__int32 MAX)
 {
        unsigned scalebits;
        FLAC__int32 output, mask, random;
 {
        unsigned scalebits;
        FLAC__int32 output, mask, random;
@@ -99,15 +106,16 @@ static FLAC__INLINE FLAC__int32 linear_dither(unsigned source_bps, unsigned targ
        return output >> scalebits;
 }
 
        return output >> scalebits;
 }
 
-unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, FLAC__int32 *input, unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps)
+size_t FLAC__plugin_common__pack_pcm_signed_big_endian(FLAC__byte *data, const FLAC__int32 * const input[], unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps)
 {
        static dither_state dither[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
        FLAC__byte * const start = data;
        FLAC__int32 sample;
 {
        static dither_state dither[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
        FLAC__byte * const start = data;
        FLAC__int32 sample;
-       unsigned samples = wide_samples * channels;
+       const FLAC__int32 *input_;
+       unsigned samples, channel;
        const unsigned bytes_per_sample = target_bps / 8;
        const unsigned bytes_per_sample = target_bps / 8;
+       const unsigned incr = bytes_per_sample * channels;
 
 
-       FLAC__ASSERT(FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS == 2);
        FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
        FLAC__ASSERT(source_bps < 32);
        FLAC__ASSERT(target_bps <= 24);
        FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
        FLAC__ASSERT(source_bps < 32);
        FLAC__ASSERT(target_bps <= 24);
@@ -116,49 +124,140 @@ unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, FL
        FLAC__ASSERT((target_bps & 7) == 0);
 
        if(source_bps != target_bps) {
        FLAC__ASSERT((target_bps & 7) == 0);
 
        if(source_bps != target_bps) {
-               const FLAC__int32 MIN = -(1L << source_bps);
+               const FLAC__int32 MIN = -(1L << (source_bps - 1));
                const FLAC__int32 MAX = ~MIN; /*(1L << (source_bps-1)) - 1 */
                const FLAC__int32 MAX = ~MIN; /*(1L << (source_bps-1)) - 1 */
-               const unsigned dither_twiggle = channels - 1;
-               unsigned dither_source = 0;
-
-               while(samples--) {
-                       sample = linear_dither(source_bps, target_bps, *input++, &dither[dither_source], MIN, MAX);
-                       dither_source ^= dither_twiggle;
-
-                       switch(target_bps) {
-                               case 8:
-                                       data[0] = sample ^ 0x80;
-                                       break;
-                               case 24:
-                                       data[2] = (FLAC__byte)(sample >> 16);
-                                       /* fall through */
-                               case 16:
-                                       data[1] = (FLAC__byte)(sample >> 8);
-                                       data[0] = (FLAC__byte)sample;
-                       }
 
 
-                       data += bytes_per_sample;
+               for(channel = 0; channel < channels; channel++) {
+
+                       samples = wide_samples;
+                       data = start + bytes_per_sample * channel;
+                       input_ = input[channel];
+
+                       while(samples--) {
+                               sample = linear_dither(source_bps, target_bps, *input_++, &dither[channel], MIN, MAX);
+
+                               switch(target_bps) {
+                                       case 8:
+                                               data[0] = sample ^ 0x80;
+                                               break;
+                                       case 16:
+                                               data[0] = (FLAC__byte)(sample >> 8);
+                                               data[1] = (FLAC__byte)sample;
+                                               break;
+                                       case 24:
+                                               data[0] = (FLAC__byte)(sample >> 16);
+                                               data[1] = (FLAC__byte)(sample >> 8);
+                                               data[2] = (FLAC__byte)sample;
+                                               break;
+                               }
+
+                               data += incr;
+                       }
                }
        }
        else {
                }
        }
        else {
-               while(samples--) {
-                       sample = *input++;
-
-                       switch(target_bps) {
-                               case 8:
-                                       data[0] = sample ^ 0x80;
-                                       break;
-                               case 24:
-                                       data[2] = (FLAC__byte)(sample >> 16);
-                                       /* fall through */
-                               case 16:
-                                       data[1] = (FLAC__byte)(sample >> 8);
-                                       data[0] = (FLAC__byte)sample;
+               for(channel = 0; channel < channels; channel++) {
+                       samples = wide_samples;
+                       data = start + bytes_per_sample * channel;
+                       input_ = input[channel];
+
+                       while(samples--) {
+                               sample = *input_++;
+
+                               switch(target_bps) {
+                                       case 8:
+                                               data[0] = sample ^ 0x80;
+                                               break;
+                                       case 16:
+                                               data[0] = (FLAC__byte)(sample >> 8);
+                                               data[1] = (FLAC__byte)sample;
+                                               break;
+                                       case 24:
+                                               data[0] = (FLAC__byte)(sample >> 16);
+                                               data[1] = (FLAC__byte)(sample >> 8);
+                                               data[2] = (FLAC__byte)sample;
+                                               break;
+                               }
+
+                               data += incr;
                        }
                        }
+               }
+       }
+
+       return wide_samples * channels * (target_bps/8);
+}
 
 
-                       data += bytes_per_sample;
+size_t FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, const FLAC__int32 * const input[], unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps)
+{
+       static dither_state dither[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
+       FLAC__byte * const start = data;
+       FLAC__int32 sample;
+       const FLAC__int32 *input_;
+       unsigned samples, channel;
+       const unsigned bytes_per_sample = target_bps / 8;
+       const unsigned incr = bytes_per_sample * channels;
+
+       FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
+       FLAC__ASSERT(source_bps < 32);
+       FLAC__ASSERT(target_bps <= 24);
+       FLAC__ASSERT(target_bps <= source_bps);
+       FLAC__ASSERT((source_bps & 7) == 0);
+       FLAC__ASSERT((target_bps & 7) == 0);
+
+       if(source_bps != target_bps) {
+               const FLAC__int32 MIN = -(1L << (source_bps - 1));
+               const FLAC__int32 MAX = ~MIN; /*(1L << (source_bps-1)) - 1 */
+
+               for(channel = 0; channel < channels; channel++) {
+
+                       samples = wide_samples;
+                       data = start + bytes_per_sample * channel;
+                       input_ = input[channel];
+
+                       while(samples--) {
+                               sample = linear_dither(source_bps, target_bps, *input_++, &dither[channel], MIN, MAX);
+
+                               switch(target_bps) {
+                                       case 8:
+                                               data[0] = sample ^ 0x80;
+                                               break;
+                                       case 24:
+                                               data[2] = (FLAC__byte)(sample >> 16);
+                                               /* fall through */
+                                       case 16:
+                                               data[1] = (FLAC__byte)(sample >> 8);
+                                               data[0] = (FLAC__byte)sample;
+                               }
+
+                               data += incr;
+                       }
+               }
+       }
+       else {
+               for(channel = 0; channel < channels; channel++) {
+                       samples = wide_samples;
+                       data = start + bytes_per_sample * channel;
+                       input_ = input[channel];
+
+                       while(samples--) {
+                               sample = *input_++;
+
+                               switch(target_bps) {
+                                       case 8:
+                                               data[0] = sample ^ 0x80;
+                                               break;
+                                       case 24:
+                                               data[2] = (FLAC__byte)(sample >> 16);
+                                               /* fall through */
+                                       case 16:
+                                               data[1] = (FLAC__byte)(sample >> 8);
+                                               data[0] = (FLAC__byte)sample;
+                               }
+
+                               data += incr;
+                       }
                }
        }
 
                }
        }
 
-       return data - start;
+       return wide_samples * channels * (target_bps/8);
 }
 }