xref: /third_party/ffmpeg/tests/checkasm/opusdsp.c (revision cabdff1a)
1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * This file is part of FFmpeg.
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or modify
5cabdff1aSopenharmony_ci * it under the terms of the GNU General Public License as published by
6cabdff1aSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
7cabdff1aSopenharmony_ci * (at your option) any later version.
8cabdff1aSopenharmony_ci *
9cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
10cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
11cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12cabdff1aSopenharmony_ci * GNU General Public License for more details.
13cabdff1aSopenharmony_ci *
14cabdff1aSopenharmony_ci * You should have received a copy of the GNU General Public License along
15cabdff1aSopenharmony_ci * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16cabdff1aSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17cabdff1aSopenharmony_ci */
18cabdff1aSopenharmony_ci
19cabdff1aSopenharmony_ci#include "libavutil/mem_internal.h"
20cabdff1aSopenharmony_ci
21cabdff1aSopenharmony_ci#include "libavcodec/opusdsp.h"
22cabdff1aSopenharmony_ci
23cabdff1aSopenharmony_ci#include "checkasm.h"
24cabdff1aSopenharmony_ci
25cabdff1aSopenharmony_ci#define randomize_float(buf, len)                               \
26cabdff1aSopenharmony_ci    do {                                                        \
27cabdff1aSopenharmony_ci        for (int i = 0; i < len; i++) {                         \
28cabdff1aSopenharmony_ci            float f = (float)rnd() / (UINT_MAX >> 5) - 16.0f;   \
29cabdff1aSopenharmony_ci            buf[i] = f;                                         \
30cabdff1aSopenharmony_ci        }                                                       \
31cabdff1aSopenharmony_ci    } while (0)
32cabdff1aSopenharmony_ci
33cabdff1aSopenharmony_ci#define EPS 0.005
34cabdff1aSopenharmony_ci#define MAX_SIZE (960)
35cabdff1aSopenharmony_ci
36cabdff1aSopenharmony_ci/* period is between 15 and 1022, inclusive */
37cabdff1aSopenharmony_cistatic void test_postfilter(int period)
38cabdff1aSopenharmony_ci{
39cabdff1aSopenharmony_ci    LOCAL_ALIGNED(16, float, data0, [MAX_SIZE + 1024]);
40cabdff1aSopenharmony_ci    LOCAL_ALIGNED(16, float, data1, [MAX_SIZE + 1024]);
41cabdff1aSopenharmony_ci
42cabdff1aSopenharmony_ci    /* This filter can explode very easily, so use a tapset from the codec.
43cabdff1aSopenharmony_ci     * In the codec these are usually multiplied by at least 0.09375f,
44cabdff1aSopenharmony_ci     * so its outside the largest filter value, but the filter is still stable
45cabdff1aSopenharmony_ci     * so use it. */
46cabdff1aSopenharmony_ci    float gains[3] = { 0.3066406250f, 0.2170410156f, 0.1296386719f };
47cabdff1aSopenharmony_ci
48cabdff1aSopenharmony_ci    /* The codec will always call with an offset which is aligned once
49cabdff1aSopenharmony_ci     * (period + 2) is subtracted, but here we have to align it outselves. */
50cabdff1aSopenharmony_ci    int offset = FFALIGN(period + 2, 4);
51cabdff1aSopenharmony_ci
52cabdff1aSopenharmony_ci    declare_func(void, float *data, int period, float *gains, int len);
53cabdff1aSopenharmony_ci
54cabdff1aSopenharmony_ci    randomize_float(data0, MAX_SIZE + 1024);
55cabdff1aSopenharmony_ci    memcpy(data1, data0, (MAX_SIZE + 1024)*sizeof(float));
56cabdff1aSopenharmony_ci
57cabdff1aSopenharmony_ci    call_ref(data0 + offset, period, gains, MAX_SIZE);
58cabdff1aSopenharmony_ci    call_new(data1 + offset, period, gains, MAX_SIZE);
59cabdff1aSopenharmony_ci
60cabdff1aSopenharmony_ci    if (!float_near_abs_eps_array(data0 + offset, data1 + offset, EPS, MAX_SIZE))
61cabdff1aSopenharmony_ci        fail();
62cabdff1aSopenharmony_ci    bench_new(data1 + offset, period, gains, MAX_SIZE);
63cabdff1aSopenharmony_ci}
64cabdff1aSopenharmony_ci
65cabdff1aSopenharmony_cistatic void test_deemphasis(void)
66cabdff1aSopenharmony_ci{
67cabdff1aSopenharmony_ci    LOCAL_ALIGNED(16, float, src, [FFALIGN(MAX_SIZE, 4)]);
68cabdff1aSopenharmony_ci    LOCAL_ALIGNED(16, float, dst0, [FFALIGN(MAX_SIZE, 4)]);
69cabdff1aSopenharmony_ci    LOCAL_ALIGNED(16, float, dst1, [FFALIGN(MAX_SIZE, 4)]);
70cabdff1aSopenharmony_ci    float coeff0 = (float)rnd() / (UINT_MAX >> 5) - 16.0f, coeff1 = coeff0;
71cabdff1aSopenharmony_ci
72cabdff1aSopenharmony_ci    declare_func_float(float, float *out, float *in, float coeff, int len);
73cabdff1aSopenharmony_ci
74cabdff1aSopenharmony_ci    randomize_float(src, MAX_SIZE);
75cabdff1aSopenharmony_ci
76cabdff1aSopenharmony_ci    coeff0 = call_ref(dst0, src, coeff0, MAX_SIZE);
77cabdff1aSopenharmony_ci    coeff1 = call_new(dst1, src, coeff1, MAX_SIZE);
78cabdff1aSopenharmony_ci
79cabdff1aSopenharmony_ci    if (!float_near_abs_eps(coeff0, coeff1, EPS) ||
80cabdff1aSopenharmony_ci        !float_near_abs_eps_array(dst0, dst1, EPS, MAX_SIZE))
81cabdff1aSopenharmony_ci        fail();
82cabdff1aSopenharmony_ci    bench_new(dst1, src, coeff1, MAX_SIZE);
83cabdff1aSopenharmony_ci}
84cabdff1aSopenharmony_ci
85cabdff1aSopenharmony_civoid checkasm_check_opusdsp(void)
86cabdff1aSopenharmony_ci{
87cabdff1aSopenharmony_ci    OpusDSP ctx;
88cabdff1aSopenharmony_ci    ff_opus_dsp_init(&ctx);
89cabdff1aSopenharmony_ci
90cabdff1aSopenharmony_ci    if (check_func(ctx.postfilter, "postfilter_15"))
91cabdff1aSopenharmony_ci        test_postfilter(15);
92cabdff1aSopenharmony_ci    report("postfilter_15");
93cabdff1aSopenharmony_ci
94cabdff1aSopenharmony_ci    if (check_func(ctx.postfilter, "postfilter_512"))
95cabdff1aSopenharmony_ci        test_postfilter(512);
96cabdff1aSopenharmony_ci    report("postfilter_512");
97cabdff1aSopenharmony_ci
98cabdff1aSopenharmony_ci    if (check_func(ctx.postfilter, "postfilter_1022"))
99cabdff1aSopenharmony_ci        test_postfilter(1022);
100cabdff1aSopenharmony_ci    report("postfilter_1022");
101cabdff1aSopenharmony_ci
102cabdff1aSopenharmony_ci    if (check_func(ctx.deemphasis, "deemphasis"))
103cabdff1aSopenharmony_ci        test_deemphasis();
104cabdff1aSopenharmony_ci    report("deemphasis");
105cabdff1aSopenharmony_ci}
106