Bulk update copyright dates
[flac.git] / src / plugin_common / dither.c
index 2af13e2..b20ab51 100644 (file)
@@ -1,5 +1,6 @@
 /* plugin_common - Routines common to several plugins
- * Copyright (C) 2002,2003  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
  * 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"
 
 #endif
 #define max(a,b) ((a)>(b)?(a):(b))
 
-
-#if defined _MSC_VER
-#define FLAC__INLINE __inline
-#else
-#define FLAC__INLINE
+#ifndef FLaC__INLINE
+#define FLaC__INLINE
 #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;
 }
@@ -48,7 +55,7 @@ typedef struct {
        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;
@@ -99,7 +106,7 @@ static FLAC__INLINE FLAC__int32 linear_dither(unsigned source_bps, unsigned targ
        return output >> scalebits;
 }
 
-unsigned 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)
+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;
@@ -107,7 +114,7 @@ unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, co
        const FLAC__int32 *input_;
        unsigned samples, channel;
        const unsigned bytes_per_sample = target_bps / 8;
-       unsigned inc = bytes_per_sample * channels;
+       const unsigned incr = bytes_per_sample * channels;
 
        FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
        FLAC__ASSERT(source_bps < 32);
@@ -121,7 +128,88 @@ unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, co
                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 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 {
+               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);
+}
+
+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];
@@ -141,7 +229,7 @@ unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, co
                                                data[0] = (FLAC__byte)sample;
                                }
 
-                               data += inc;
+                               data += incr;
                        }
                }
        }
@@ -166,10 +254,10 @@ unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, co
                                                data[0] = (FLAC__byte)sample;
                                }
 
-                               data += inc;
+                               data += incr;
                        }
                }
        }
 
-       return data - start;
+       return wide_samples * channels * (target_bps/8);
 }