avcodec/jpegxl_parse{,r}: fix integer overflow for some malformed files
[FFMpeg-mirror.git] / tests / checkasm / lpc.c
blob514a2cdb3c3879c0e0b8a19e428560b8e3095325
1 /*
2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "libavutil/avassert.h"
20 #include "libavutil/mem_internal.h"
22 #include "libavcodec/lpc.h"
24 #include "checkasm.h"
26 #define randomize_int32(buf, len) \
27 do { \
28 for (int i = 0; i < len; i++) { \
29 int32_t f = ((int)(UINT32_MAX >> 17)) - ((int)(rnd() >> 16)); \
30 buf[i] = f; \
31 } \
32 } while (0)
34 #define EPS 0.005
36 static void test_window(int len)
38 LOCAL_ALIGNED(16, int32_t, src, [5000]);
39 LOCAL_ALIGNED(16, double, dst0, [5000]);
40 LOCAL_ALIGNED(16, double, dst1, [5000]);
42 declare_func(void, const int32_t *in, ptrdiff_t len, double *out);
44 randomize_int32(src, len);
46 call_ref(src, len, dst0);
47 call_new(src, len, dst1);
49 for (int i = 0; i < len; i++) {
50 if (!double_near_abs_eps(dst0[i], dst1[i], EPS)) {
51 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
52 i, dst0[i], dst1[i], dst0[i] - dst1[i]);
53 fail();
54 break;
58 bench_new(src, 4608 + (len & 1), dst1);
61 static void test_compute_autocorr(ptrdiff_t len, int lag)
63 const double eps = EPS * (double)len;
64 LOCAL_ALIGNED(32, double, src, [5000 + 2 + MAX_LPC_ORDER]);
65 LOCAL_ALIGNED(16, double, dst0, [MAX_LPC_ORDER + 1]);
66 LOCAL_ALIGNED(16, double, dst1, [MAX_LPC_ORDER + 1]);
68 declare_func(void, const double *in, ptrdiff_t len, int lag, double *out);
70 av_assert0(lag >= 0 && lag <= MAX_LPC_ORDER);
72 for (int i = 0; i < MAX_LPC_ORDER; i++)
73 src[i] = 0.;
75 src += MAX_LPC_ORDER;
77 for (int i = 0; i < 5000 + 2; i++) {
78 src[i] = (double)rnd() / (double)UINT_MAX;
81 call_ref(src, len, lag, dst0);
82 call_new(src, len, lag, dst1);
84 for (size_t i = 0; i <= lag; i++) {
85 if (!double_near_abs_eps(dst0[i], dst1[i], eps)) {
86 fprintf(stderr, "%zu: %- .12f - %- .12f = % .12g\n",
87 i, dst0[i], dst1[i], dst0[i] - dst1[i]);
88 fail();
89 break;
93 bench_new(src, 4608 + (len & 1), lag, dst1);
96 void checkasm_check_lpc(void)
98 LPCContext ctx;
99 int len = 2000 + rnd() % 3000;
100 static const int lags[] = { 8, 12, };
102 ff_lpc_init(&ctx, 32, 16, FF_LPC_TYPE_DEFAULT);
104 if (check_func(ctx.lpc_apply_welch_window, "apply_welch_window_even")) {
105 test_window(len & ~1);
107 report("apply_welch_window_even");
109 if (check_func(ctx.lpc_apply_welch_window, "apply_welch_window_odd")) {
110 test_window(len | 1);
112 report("apply_welch_window_odd");
113 ff_lpc_end(&ctx);
115 for (size_t i = 0; i < FF_ARRAY_ELEMS(lags); i++) {
116 ff_lpc_init(&ctx, len, lags[i], FF_LPC_TYPE_DEFAULT);
117 if (check_func(ctx.lpc_compute_autocorr, "autocorr_%d_even", lags[i]))
118 test_compute_autocorr(len & ~1, lags[i]);
119 #if !ARCH_X86
120 if (check_func(ctx.lpc_compute_autocorr, "autocorr_%d_odd", lags[i]))
121 test_compute_autocorr(len | 1, lags[i]);
122 #endif
123 ff_lpc_end(&ctx);
125 report("compute_autocorr");