gstreamer/gst/audioconvert/gstfastrandom.h
Ilya Konstantinov 7b398701cf audioconvert: Avoid int division in quantization
Since range size is always 2^n, we can simply use modulo (implemented
with a bitmask).

The previous implementation used 64-bit integer division, which is
done in software on ARMv7. Although the divisor was constant, the
division could not be transformed into "multiplication by magic number"
since the dividend was 64-bit.

The now-unused and not-so-fast gst_fast_random_(u)int32_range functions
were removed.

Also, implementing bug fixes:

1) ADD_DITHER_TPDF_HF_I no longer discards bias.

2) We change TPDF's noise range to be the same as RPDF's. Previously,
RPDF's noise ranged:
  { bias - dither, bias + dither }
while TPDF's noise ranged:
  { bias/2 - dither/2, bias/2 + dither/2 - 1 } +
  { bias/2 - dither/2, bias/2 + dither/2 - 1 } =
  { bias - dither, bias + dither - 2 }
Now, both range:
  { bias - dither, bias + dither - 1 }

https://bugzilla.gnome.org/show_bug.cgi?id=746661
2015-03-24 16:52:07 +01:00

70 lines
1.9 KiB
C

/* GStreamer
* Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
*
* gstfastrandom.h: Fast, bad PNRG
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <glib.h>
#ifndef __GST_FAST_RANDOM__
#define __GST_FAST_RANDOM__
/* transform [0..2^32] -> [0..1] */
#define GST_RAND_DOUBLE_TRANSFORM 2.3283064365386962890625e-10
/* This is the base function, implementing a linear congruential generator
* and returning a pseudo random number between 0 and 2^32 - 1.
*/
static inline guint32
gst_fast_random_uint32 (void)
{
static guint32 state = 0xdeadbeef;
return (state = state * 1103515245 + 12345);
}
static inline gint32
gst_fast_random_int32 (void)
{
return (gint32) gst_fast_random_uint32 ();
}
static inline gdouble
gst_fast_random_double (void)
{
gdouble ret;
ret = gst_fast_random_uint32 () * GST_RAND_DOUBLE_TRANSFORM;
ret = (ret + gst_fast_random_uint32 ()) * GST_RAND_DOUBLE_TRANSFORM;
if (ret >= 1.0)
return gst_fast_random_double ();
return ret;
}
static inline gdouble
gst_fast_random_double_range (gdouble start, gdouble end)
{
return gst_fast_random_double () * (end - start) + start;
}
#undef GST_RAND_DOUBLE_TRANSFORM
#endif /* __GST_FAST_RANDOM__ */