[FFmpeg-devel] [PATCH] all: use M_SQRT1_2, M_SQRT2, M_PI

Ganesh Ajjanagadde gajjanagadde at gmail.com
Thu Nov 19 13:52:57 CET 2015


On Sat, Nov 14, 2015 at 11:15 AM, Ganesh Ajjanagadde
<gajjanagadde at gmail.com> wrote:
>
> This uses M_SQRT1_2, M_SQRT2, and M_PI instead of the actual literals.
> Benefits include:
> 1. Reduced scope for copy/paste errors and improved readability.
> 2. Consistency across the codebase.
> 3. Actually fixes an incorrect sqrt(2) in avcodec/ppc.
> 4. Greater precision in avcodec/ac3.
>
> Patch tested with FATE on x86, ppc untested.
>
> Signed-off-by: Ganesh Ajjanagadde <gajjanagadde at gmail.com>
> ---
>  libavcodec/ac3.h            |  6 +++---
>  libavcodec/cos_tablegen.c   |  4 +++-
>  libavcodec/dct32_template.c |  2 +-
>  libavcodec/ppc/fdctdsp.c    | 21 ++++++++++-----------
>  libavfilter/af_dynaudnorm.c |  4 +---
>  libavfilter/vf_fspp.h       |  6 +++---
>  6 files changed, 21 insertions(+), 22 deletions(-)
>
> diff --git a/libavcodec/ac3.h b/libavcodec/ac3.h
> index 3f67e09..3676b4e 100644
> --- a/libavcodec/ac3.h
> +++ b/libavcodec/ac3.h
> @@ -97,13 +97,13 @@
>
>  #endif /* USE_FIXED */
>
> -#define AC3_LEVEL(x)            ROUND15((x) * FIXR15(0.7071067811865476))
> +#define AC3_LEVEL(x)            ROUND15((x) * FIXR15(M_SQRT1_2))
>
>  /* pre-defined gain values */
> -#define LEVEL_PLUS_3DB          1.4142135623730950
> +#define LEVEL_PLUS_3DB          M_SQRT2
>  #define LEVEL_PLUS_1POINT5DB    1.1892071150027209
>  #define LEVEL_MINUS_1POINT5DB   0.8408964152537145
> -#define LEVEL_MINUS_3DB         0.7071067811865476
> +#define LEVEL_MINUS_3DB         M_SQRT1_2
>  #define LEVEL_MINUS_4POINT5DB   0.5946035575013605
>  #define LEVEL_MINUS_6DB         0.5000000000000000
>  #define LEVEL_MINUS_9DB         0.3535533905932738
> diff --git a/libavcodec/cos_tablegen.c b/libavcodec/cos_tablegen.c
> index 9af83f4..dbd0cc0 100644
> --- a/libavcodec/cos_tablegen.c
> +++ b/libavcodec/cos_tablegen.c
> @@ -24,6 +24,8 @@
>  #include <string.h>
>  #include <math.h>
>
> +#include "libavutil/mathematics.h"
> +
>  #define BITS 16
>  #define FLOATFMT "%.18e"
>  #define FIXEDFMT "%6d"
> @@ -61,7 +63,7 @@ int main(int argc, char *argv[])
>      printf("#include \"libavcodec/%s\"\n", do_sin ? "rdft.h" : "fft.h");
>      for (i = 4; i <= BITS; i++) {
>          int m = 1 << i;
> -        double freq = 2*3.14159265358979323846/m;
> +        double freq = 2*M_PI/m;
>          printf("%s(%i) = {\n   ", do_sin ? "SINTABLE" : "COSTABLE", m);
>          for (j = 0; j < m/2 - 1; j++) {
>              int idx = j > m/4 ? m/2 - j : j;
> diff --git a/libavcodec/dct32_template.c b/libavcodec/dct32_template.c
> index fb53d53..c70396e 100644
> --- a/libavcodec/dct32_template.c
> +++ b/libavcodec/dct32_template.c
> @@ -73,7 +73,7 @@
>  #define COS3_0 FIXHR(0.54119610014619698439/2)
>  #define COS3_1 FIXHR(1.30656296487637652785/4)
>
> -#define COS4_0 FIXHR(0.70710678118654752439/2)
> +#define COS4_0 FIXHR(M_SQRT1_2/2)
>
>  /* butterfly operator */
>  #define BF(a, b, c, s)\
> diff --git a/libavcodec/ppc/fdctdsp.c b/libavcodec/ppc/fdctdsp.c
> index 40f4c6c..924d12c 100644
> --- a/libavcodec/ppc/fdctdsp.c
> +++ b/libavcodec/ppc/fdctdsp.c
> @@ -44,20 +44,19 @@
>  #define C5     0.55557024478912353515625000 /* cos(5 * PI / 16) */
>  #define C6     0.38268342614173889160156250 /* cos(6 * PI / 16) */
>  #define C7     0.19509032368659973144531250 /* cos(7 * PI / 16) */
> -#define SQRT_2 1.41421353816986083984375000 /* sqrt(2)          */
>
>  #define W0 -(2 * C2)
>  #define W1  (2 * C6)
> -#define W2 (SQRT_2 * C6)
> -#define W3 (SQRT_2 * C3)
> -#define W4 (SQRT_2 * (-C1 + C3 + C5 - C7))
> -#define W5 (SQRT_2 *  (C1 + C3 - C5 + C7))
> -#define W6 (SQRT_2 *  (C1 + C3 + C5 - C7))
> -#define W7 (SQRT_2 *  (C1 + C3 - C5 - C7))
> -#define W8 (SQRT_2 *  (C7 - C3))
> -#define W9 (SQRT_2 * (-C1 - C3))
> -#define WA (SQRT_2 * (-C3 - C5))
> -#define WB (SQRT_2 *  (C5 - C3))
> +#define W2 (M_SQRT2 * C6)
> +#define W3 (M_SQRT2 * C3)
> +#define W4 (M_SQRT2 * (-C1 + C3 + C5 - C7))
> +#define W5 (M_SQRT2 *  (C1 + C3 - C5 + C7))
> +#define W6 (M_SQRT2 *  (C1 + C3 + C5 - C7))
> +#define W7 (M_SQRT2 *  (C1 + C3 - C5 - C7))
> +#define W8 (M_SQRT2 *  (C7 - C3))
> +#define W9 (M_SQRT2 * (-C1 - C3))
> +#define WA (M_SQRT2 * (-C3 - C5))
> +#define WB (M_SQRT2 *  (C5 - C3))
>
>  static const vector float fdctconsts[3] = {
>      { W0, W1, W2, W3 },
> diff --git a/libavfilter/af_dynaudnorm.c b/libavfilter/af_dynaudnorm.c
> index 8f0c2d0..62a2653 100644
> --- a/libavfilter/af_dynaudnorm.c
> +++ b/libavfilter/af_dynaudnorm.c
> @@ -227,8 +227,6 @@ static int cqueue_pop(cqueue *q)
>      return 0;
>  }
>
> -static const double s_pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
> -
>  static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
>  {
>      double total_weight = 0.0;
> @@ -238,7 +236,7 @@ static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
>
>      // Pre-compute constants
>      const int offset = s->filter_size / 2;
> -    const double c1 = 1.0 / (sigma * sqrt(2.0 * s_pi));
> +    const double c1 = 1.0 / (sigma * sqrt(2.0 * M_PI));
>      const double c2 = 2.0 * pow(sigma, 2.0);
>
>      // Compute weights
> diff --git a/libavfilter/vf_fspp.h b/libavfilter/vf_fspp.h
> index 237ffb1..74a3447 100644
> --- a/libavfilter/vf_fspp.h
> +++ b/libavfilter/vf_fspp.h
> @@ -44,12 +44,12 @@
>  typedef int32_t int_simd16_t;
>  static const int16_t FIX_0_382683433   = FIX(0.382683433, 14);
>  static const int16_t FIX_0_541196100   = FIX(0.541196100, 14);
> -static const int16_t FIX_0_707106781   = FIX(0.707106781, 14);
> +static const int16_t FIX_0_707106781   = FIX(M_SQRT1_2  , 14);
>  static const int16_t FIX_1_306562965   = FIX(1.306562965, 14);
> -static const int16_t FIX_1_414213562_A = FIX(1.414213562, 14);
> +static const int16_t FIX_1_414213562_A = FIX(M_SQRT2    , 14);
>  static const int16_t FIX_1_847759065   = FIX(1.847759065, 13);
>  static const int16_t FIX_2_613125930   = FIX(-2.613125930, 13);
> -static const int16_t FIX_1_414213562   = FIX(1.414213562, 13);
> +static const int16_t FIX_1_414213562   = FIX(M_SQRT2    , 13);
>  static const int16_t FIX_1_082392200   = FIX(1.082392200, 13);
>
>  typedef struct FSPPContext {
> --
> 2.6.2
>

ping for any last reviews on this: similar changes have been accepted
and pushed, so I will wait another day for a response to this.


More information about the ffmpeg-devel mailing list