[FFmpeg-devel] [PATCH 1/4] all: use predefined mathematics macros
Ganesh Ajjanagadde
gajjanagadde at gmail.com
Fri Nov 13 17:42:28 CET 2015
This uses M_SQRT1_2, M_SQRT2, M_PI, and M_E 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/mpegaudioenc_template.c | 4 ++--
libavcodec/mpegaudiotab.h | 2 --
libavcodec/mpegvideo.c | 4 ++--
libavcodec/ppc/fdctdsp.c | 21 ++++++++++-----------
libavcodec/ratecontrol.c | 4 ----
libavcodec/simple_idct.c | 4 ++--
libavfilter/af_dynaudnorm.c | 4 +---
libavfilter/vf_fspp.h | 6 +++---
11 files changed, 27 insertions(+), 34 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/mpegaudioenc_template.c b/libavcodec/mpegaudioenc_template.c
index ce93cc7..b91d0a8 100644
--- a/libavcodec/mpegaudioenc_template.c
+++ b/libavcodec/mpegaudioenc_template.c
@@ -244,11 +244,11 @@ static void idct32(int *out, int *tab)
do {
int x1, x2, x3, x4;
- x3 = MUL(t[16], FIX(SQRT2*0.5));
+ x3 = MUL(t[16], FIX(M_SQRT2*0.5));
x4 = t[0] - x3;
x3 = t[0] + x3;
- x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
+ x2 = MUL(-(t[24] + t[8]), FIX(M_SQRT2*0.5));
x1 = MUL((t[8] - x2), xp[0]);
x2 = MUL((t[8] + x2), xp[1]);
diff --git a/libavcodec/mpegaudiotab.h b/libavcodec/mpegaudiotab.h
index 42d42d8..bb2e5de 100644
--- a/libavcodec/mpegaudiotab.h
+++ b/libavcodec/mpegaudiotab.h
@@ -33,8 +33,6 @@
#include <stdint.h>
#include "mpegaudio.h"
-#define SQRT2 1.41421356237309514547
-
static const int costab32[30] = {
FIX(0.54119610014619701222),
FIX(1.3065629648763763537),
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 96634ec..69e0595 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -1870,8 +1870,8 @@ void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_
uint64_t u,v;
int y;
#define COLOR(theta, r) \
- u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
- v = (int)(128 + r * sin(theta * 3.141592 / 180));
+ u = (int)(128 + r * cos(theta * M_PI / 180)); \
+ v = (int)(128 + r * sin(theta * M_PI / 180));
u = v = 128;
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/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index 308e34e..e96550c 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -35,10 +35,6 @@
#include "mpegvideo.h"
#include "libavutil/eval.h"
-#ifndef M_E
-#define M_E 2.718281828
-#endif
-
static int init_pass2(MpegEncContext *s);
static double get_qscale(MpegEncContext *s, RateControlEntry *rce,
double rate_factor, int frame_num);
diff --git a/libavcodec/simple_idct.c b/libavcodec/simple_idct.c
index 4d6d20d..0711e16 100644
--- a/libavcodec/simple_idct.c
+++ b/libavcodec/simple_idct.c
@@ -132,7 +132,7 @@ void ff_simple_idct248_put(uint8_t *dest, int line_size, int16_t *block)
#undef C1
#undef C2
#define CN_SHIFT 12
-#define C_FIX(x) ((int)((x) * 1.414213562 * (1 << CN_SHIFT) + 0.5))
+#define C_FIX(x) ((int)((x) * M_SQRT2 * (1 << CN_SHIFT) + 0.5))
#define C1 C_FIX(0.6532814824)
#define C2 C_FIX(0.2705980501)
#define C3 C_FIX(0.5)
@@ -159,7 +159,7 @@ static inline void idct4col_add(uint8_t *dest, int line_size, const int16_t *col
}
#define RN_SHIFT 15
-#define R_FIX(x) ((int)((x) * 1.414213562 * (1 << RN_SHIFT) + 0.5))
+#define R_FIX(x) ((int)((x) * M_SQRT2 * (1 << RN_SHIFT) + 0.5))
#define R1 R_FIX(0.6532814824)
#define R2 R_FIX(0.2705980501)
#define R3 R_FIX(0.5)
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
More information about the ffmpeg-devel
mailing list