1 /*
2  * Copyright © 2018-2022, VideoLAN and dav1d authors
3  * Copyright © 2018-2022, Two Orioles, LLC
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *    list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "src/cpu.h"
29 #include "src/filmgrain.h"
30 
31 #define decl_fg_fns(ext)                                         \
32 decl_generate_grain_y_fn(BF(dav1d_generate_grain_y, ext));       \
33 decl_generate_grain_uv_fn(BF(dav1d_generate_grain_uv_420, ext)); \
34 decl_generate_grain_uv_fn(BF(dav1d_generate_grain_uv_422, ext)); \
35 decl_generate_grain_uv_fn(BF(dav1d_generate_grain_uv_444, ext)); \
36 decl_fgy_32x32xn_fn(BF(dav1d_fgy_32x32xn, ext));                 \
37 decl_fguv_32x32xn_fn(BF(dav1d_fguv_32x32xn_i420, ext));          \
38 decl_fguv_32x32xn_fn(BF(dav1d_fguv_32x32xn_i422, ext));          \
39 decl_fguv_32x32xn_fn(BF(dav1d_fguv_32x32xn_i444, ext))
40 
41 decl_fg_fns(ssse3);
42 decl_fg_fns(avx2);
43 decl_fg_fns(avx512icl);
44 
bitfn(dav1d_film_grain_dsp_init_x86)45 COLD void bitfn(dav1d_film_grain_dsp_init_x86)(Dav1dFilmGrainDSPContext *const c) {
46     const unsigned flags = dav1d_get_cpu_flags();
47 
48     if (!(flags & DAV1D_X86_CPU_FLAG_SSSE3)) return;
49 
50     c->generate_grain_y = BF(dav1d_generate_grain_y, ssse3);
51     c->generate_grain_uv[DAV1D_PIXEL_LAYOUT_I420 - 1] = BF(dav1d_generate_grain_uv_420, ssse3);
52     c->fgy_32x32xn = BF(dav1d_fgy_32x32xn, ssse3);
53     c->fguv_32x32xn[DAV1D_PIXEL_LAYOUT_I420 - 1] = BF(dav1d_fguv_32x32xn_i420, ssse3);
54     c->generate_grain_uv[DAV1D_PIXEL_LAYOUT_I422 - 1] = BF(dav1d_generate_grain_uv_422, ssse3);
55     c->generate_grain_uv[DAV1D_PIXEL_LAYOUT_I444 - 1] = BF(dav1d_generate_grain_uv_444, ssse3);
56     c->fguv_32x32xn[DAV1D_PIXEL_LAYOUT_I422 - 1] = BF(dav1d_fguv_32x32xn_i422, ssse3);
57     c->fguv_32x32xn[DAV1D_PIXEL_LAYOUT_I444 - 1] = BF(dav1d_fguv_32x32xn_i444, ssse3);
58 
59 #if ARCH_X86_64
60     if (!(flags & DAV1D_X86_CPU_FLAG_AVX2)) return;
61 
62     c->generate_grain_y = BF(dav1d_generate_grain_y, avx2);
63     c->generate_grain_uv[DAV1D_PIXEL_LAYOUT_I420 - 1] = BF(dav1d_generate_grain_uv_420, avx2);
64     c->generate_grain_uv[DAV1D_PIXEL_LAYOUT_I422 - 1] = BF(dav1d_generate_grain_uv_422, avx2);
65     c->generate_grain_uv[DAV1D_PIXEL_LAYOUT_I444 - 1] = BF(dav1d_generate_grain_uv_444, avx2);
66 
67     if (!(flags & DAV1D_X86_CPU_FLAG_SLOW_GATHER)) {
68         c->fgy_32x32xn = BF(dav1d_fgy_32x32xn, avx2);
69         c->fguv_32x32xn[DAV1D_PIXEL_LAYOUT_I420 - 1] = BF(dav1d_fguv_32x32xn_i420, avx2);
70         c->fguv_32x32xn[DAV1D_PIXEL_LAYOUT_I422 - 1] = BF(dav1d_fguv_32x32xn_i422, avx2);
71         c->fguv_32x32xn[DAV1D_PIXEL_LAYOUT_I444 - 1] = BF(dav1d_fguv_32x32xn_i444, avx2);
72     }
73 
74     if (!(flags & DAV1D_X86_CPU_FLAG_AVX512ICL)) return;
75 
76     c->fgy_32x32xn = BF(dav1d_fgy_32x32xn, avx512icl);
77     c->fguv_32x32xn[DAV1D_PIXEL_LAYOUT_I420 - 1] = BF(dav1d_fguv_32x32xn_i420, avx512icl);
78     c->fguv_32x32xn[DAV1D_PIXEL_LAYOUT_I422 - 1] = BF(dav1d_fguv_32x32xn_i422, avx512icl);
79     c->fguv_32x32xn[DAV1D_PIXEL_LAYOUT_I444 - 1] = BF(dav1d_fguv_32x32xn_i444, avx512icl);
80 #endif
81 }
82