1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * This file is part of FFmpeg.
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
5cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
6cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
7cabdff1aSopenharmony_ci * version 2.1 of the License, or (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 GNU
12cabdff1aSopenharmony_ci * Lesser General Public License for more details.
13cabdff1aSopenharmony_ci *
14cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
15cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
16cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17cabdff1aSopenharmony_ci */
18cabdff1aSopenharmony_ci
19cabdff1aSopenharmony_ci#include "config.h"
20cabdff1aSopenharmony_ci
21cabdff1aSopenharmony_ci#if HAVE_SCHED_GETAFFINITY
22cabdff1aSopenharmony_ci#ifndef _GNU_SOURCE
23cabdff1aSopenharmony_ci# define _GNU_SOURCE
24cabdff1aSopenharmony_ci#endif
25cabdff1aSopenharmony_ci#include <sched.h>
26cabdff1aSopenharmony_ci#endif
27cabdff1aSopenharmony_ci
28cabdff1aSopenharmony_ci#include <stddef.h>
29cabdff1aSopenharmony_ci#include <stdint.h>
30cabdff1aSopenharmony_ci#include <stdatomic.h>
31cabdff1aSopenharmony_ci
32cabdff1aSopenharmony_ci#include "attributes.h"
33cabdff1aSopenharmony_ci#include "cpu.h"
34cabdff1aSopenharmony_ci#include "cpu_internal.h"
35cabdff1aSopenharmony_ci#include "opt.h"
36cabdff1aSopenharmony_ci#include "common.h"
37cabdff1aSopenharmony_ci
38cabdff1aSopenharmony_ci#if HAVE_GETPROCESSAFFINITYMASK || HAVE_WINRT
39cabdff1aSopenharmony_ci#include <windows.h>
40cabdff1aSopenharmony_ci#endif
41cabdff1aSopenharmony_ci#if HAVE_SYSCTL
42cabdff1aSopenharmony_ci#if HAVE_SYS_PARAM_H
43cabdff1aSopenharmony_ci#include <sys/param.h>
44cabdff1aSopenharmony_ci#endif
45cabdff1aSopenharmony_ci#include <sys/types.h>
46cabdff1aSopenharmony_ci#include <sys/sysctl.h>
47cabdff1aSopenharmony_ci#endif
48cabdff1aSopenharmony_ci#if HAVE_UNISTD_H
49cabdff1aSopenharmony_ci#include <unistd.h>
50cabdff1aSopenharmony_ci#endif
51cabdff1aSopenharmony_ci
52cabdff1aSopenharmony_cistatic atomic_int cpu_flags = ATOMIC_VAR_INIT(-1);
53cabdff1aSopenharmony_cistatic atomic_int cpu_count = ATOMIC_VAR_INIT(-1);
54cabdff1aSopenharmony_ci
55cabdff1aSopenharmony_cistatic int get_cpu_flags(void)
56cabdff1aSopenharmony_ci{
57cabdff1aSopenharmony_ci#if ARCH_MIPS
58cabdff1aSopenharmony_ci    return ff_get_cpu_flags_mips();
59cabdff1aSopenharmony_ci#elif ARCH_AARCH64
60cabdff1aSopenharmony_ci    return ff_get_cpu_flags_aarch64();
61cabdff1aSopenharmony_ci#elif ARCH_ARM
62cabdff1aSopenharmony_ci    return ff_get_cpu_flags_arm();
63cabdff1aSopenharmony_ci#elif ARCH_PPC
64cabdff1aSopenharmony_ci    return ff_get_cpu_flags_ppc();
65cabdff1aSopenharmony_ci#elif ARCH_X86
66cabdff1aSopenharmony_ci    return ff_get_cpu_flags_x86();
67cabdff1aSopenharmony_ci#elif ARCH_LOONGARCH
68cabdff1aSopenharmony_ci    return ff_get_cpu_flags_loongarch();
69cabdff1aSopenharmony_ci#endif
70cabdff1aSopenharmony_ci    return 0;
71cabdff1aSopenharmony_ci}
72cabdff1aSopenharmony_ci
73cabdff1aSopenharmony_civoid av_force_cpu_flags(int arg){
74cabdff1aSopenharmony_ci    if (ARCH_X86 &&
75cabdff1aSopenharmony_ci           (arg & ( AV_CPU_FLAG_3DNOW    |
76cabdff1aSopenharmony_ci                    AV_CPU_FLAG_3DNOWEXT |
77cabdff1aSopenharmony_ci                    AV_CPU_FLAG_MMXEXT   |
78cabdff1aSopenharmony_ci                    AV_CPU_FLAG_SSE      |
79cabdff1aSopenharmony_ci                    AV_CPU_FLAG_SSE2     |
80cabdff1aSopenharmony_ci                    AV_CPU_FLAG_SSE2SLOW |
81cabdff1aSopenharmony_ci                    AV_CPU_FLAG_SSE3     |
82cabdff1aSopenharmony_ci                    AV_CPU_FLAG_SSE3SLOW |
83cabdff1aSopenharmony_ci                    AV_CPU_FLAG_SSSE3    |
84cabdff1aSopenharmony_ci                    AV_CPU_FLAG_SSE4     |
85cabdff1aSopenharmony_ci                    AV_CPU_FLAG_SSE42    |
86cabdff1aSopenharmony_ci                    AV_CPU_FLAG_AVX      |
87cabdff1aSopenharmony_ci                    AV_CPU_FLAG_AVXSLOW  |
88cabdff1aSopenharmony_ci                    AV_CPU_FLAG_XOP      |
89cabdff1aSopenharmony_ci                    AV_CPU_FLAG_FMA3     |
90cabdff1aSopenharmony_ci                    AV_CPU_FLAG_FMA4     |
91cabdff1aSopenharmony_ci                    AV_CPU_FLAG_AVX2     |
92cabdff1aSopenharmony_ci                    AV_CPU_FLAG_AVX512   ))
93cabdff1aSopenharmony_ci        && !(arg & AV_CPU_FLAG_MMX)) {
94cabdff1aSopenharmony_ci        av_log(NULL, AV_LOG_WARNING, "MMX implied by specified flags\n");
95cabdff1aSopenharmony_ci        arg |= AV_CPU_FLAG_MMX;
96cabdff1aSopenharmony_ci    }
97cabdff1aSopenharmony_ci
98cabdff1aSopenharmony_ci    atomic_store_explicit(&cpu_flags, arg, memory_order_relaxed);
99cabdff1aSopenharmony_ci}
100cabdff1aSopenharmony_ci
101cabdff1aSopenharmony_ciint av_get_cpu_flags(void)
102cabdff1aSopenharmony_ci{
103cabdff1aSopenharmony_ci    int flags = atomic_load_explicit(&cpu_flags, memory_order_relaxed);
104cabdff1aSopenharmony_ci    if (flags == -1) {
105cabdff1aSopenharmony_ci        flags = get_cpu_flags();
106cabdff1aSopenharmony_ci        atomic_store_explicit(&cpu_flags, flags, memory_order_relaxed);
107cabdff1aSopenharmony_ci    }
108cabdff1aSopenharmony_ci    return flags;
109cabdff1aSopenharmony_ci}
110cabdff1aSopenharmony_ci
111cabdff1aSopenharmony_ciint av_parse_cpu_caps(unsigned *flags, const char *s)
112cabdff1aSopenharmony_ci{
113cabdff1aSopenharmony_ci        static const AVOption cpuflags_opts[] = {
114cabdff1aSopenharmony_ci        { "flags"   , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
115cabdff1aSopenharmony_ci#if   ARCH_PPC
116cabdff1aSopenharmony_ci        { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC  },    .unit = "flags" },
117cabdff1aSopenharmony_ci#elif ARCH_X86
118cabdff1aSopenharmony_ci        { "mmx"     , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX      },    .unit = "flags" },
119cabdff1aSopenharmony_ci        { "mmx2"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX2     },    .unit = "flags" },
120cabdff1aSopenharmony_ci        { "mmxext"  , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX2     },    .unit = "flags" },
121cabdff1aSopenharmony_ci        { "sse"     , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE      },    .unit = "flags" },
122cabdff1aSopenharmony_ci        { "sse2"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE2     },    .unit = "flags" },
123cabdff1aSopenharmony_ci        { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE2SLOW },    .unit = "flags" },
124cabdff1aSopenharmony_ci        { "sse3"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE3     },    .unit = "flags" },
125cabdff1aSopenharmony_ci        { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE3SLOW },    .unit = "flags" },
126cabdff1aSopenharmony_ci        { "ssse3"   , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSSE3    },    .unit = "flags" },
127cabdff1aSopenharmony_ci        { "atom"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM     },    .unit = "flags" },
128cabdff1aSopenharmony_ci        { "sse4.1"  , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE4     },    .unit = "flags" },
129cabdff1aSopenharmony_ci        { "sse4.2"  , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE42    },    .unit = "flags" },
130cabdff1aSopenharmony_ci        { "avx"     , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX      },    .unit = "flags" },
131cabdff1aSopenharmony_ci        { "avxslow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVXSLOW  },    .unit = "flags" },
132cabdff1aSopenharmony_ci        { "xop"     , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_XOP      },    .unit = "flags" },
133cabdff1aSopenharmony_ci        { "fma3"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_FMA3     },    .unit = "flags" },
134cabdff1aSopenharmony_ci        { "fma4"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_FMA4     },    .unit = "flags" },
135cabdff1aSopenharmony_ci        { "avx2"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX2     },    .unit = "flags" },
136cabdff1aSopenharmony_ci        { "bmi1"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_BMI1     },    .unit = "flags" },
137cabdff1aSopenharmony_ci        { "bmi2"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_BMI2     },    .unit = "flags" },
138cabdff1aSopenharmony_ci        { "3dnow"   , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_3DNOW    },    .unit = "flags" },
139cabdff1aSopenharmony_ci        { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_3DNOWEXT },    .unit = "flags" },
140cabdff1aSopenharmony_ci        { "cmov",     NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV     },    .unit = "flags" },
141cabdff1aSopenharmony_ci        { "aesni",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AESNI    },    .unit = "flags" },
142cabdff1aSopenharmony_ci        { "avx512"  , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX512   },    .unit = "flags" },
143cabdff1aSopenharmony_ci        { "avx512icl",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX512ICL   }, .unit = "flags" },
144cabdff1aSopenharmony_ci        { "slowgather", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SLOW_GATHER }, .unit = "flags" },
145cabdff1aSopenharmony_ci
146cabdff1aSopenharmony_ci#define CPU_FLAG_P2 AV_CPU_FLAG_CMOV | AV_CPU_FLAG_MMX
147cabdff1aSopenharmony_ci#define CPU_FLAG_P3 CPU_FLAG_P2 | AV_CPU_FLAG_MMX2 | AV_CPU_FLAG_SSE
148cabdff1aSopenharmony_ci#define CPU_FLAG_P4 CPU_FLAG_P3| AV_CPU_FLAG_SSE2
149cabdff1aSopenharmony_ci        { "pentium2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P2          },    .unit = "flags" },
150cabdff1aSopenharmony_ci        { "pentium3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P3          },    .unit = "flags" },
151cabdff1aSopenharmony_ci        { "pentium4", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P4          },    .unit = "flags" },
152cabdff1aSopenharmony_ci
153cabdff1aSopenharmony_ci#define CPU_FLAG_K62 AV_CPU_FLAG_MMX | AV_CPU_FLAG_3DNOW
154cabdff1aSopenharmony_ci#define CPU_FLAG_ATHLON   CPU_FLAG_K62 | AV_CPU_FLAG_CMOV | AV_CPU_FLAG_3DNOWEXT | AV_CPU_FLAG_MMX2
155cabdff1aSopenharmony_ci#define CPU_FLAG_ATHLONXP CPU_FLAG_ATHLON | AV_CPU_FLAG_SSE
156cabdff1aSopenharmony_ci#define CPU_FLAG_K8  CPU_FLAG_ATHLONXP | AV_CPU_FLAG_SSE2
157cabdff1aSopenharmony_ci        { "k6",       NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX      },    .unit = "flags" },
158cabdff1aSopenharmony_ci        { "k62",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_K62         },    .unit = "flags" },
159cabdff1aSopenharmony_ci        { "athlon",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLON      },    .unit = "flags" },
160cabdff1aSopenharmony_ci        { "athlonxp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLONXP    },    .unit = "flags" },
161cabdff1aSopenharmony_ci        { "k8",       NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_K8          },    .unit = "flags" },
162cabdff1aSopenharmony_ci#elif ARCH_ARM
163cabdff1aSopenharmony_ci        { "armv5te",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE  },    .unit = "flags" },
164cabdff1aSopenharmony_ci        { "armv6",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6    },    .unit = "flags" },
165cabdff1aSopenharmony_ci        { "armv6t2",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2  },    .unit = "flags" },
166cabdff1aSopenharmony_ci        { "vfp",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP      },    .unit = "flags" },
167cabdff1aSopenharmony_ci        { "vfp_vm",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP_VM   },    .unit = "flags" },
168cabdff1aSopenharmony_ci        { "vfpv3",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3    },    .unit = "flags" },
169cabdff1aSopenharmony_ci        { "neon",     NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON     },    .unit = "flags" },
170cabdff1aSopenharmony_ci        { "setend",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SETEND   },    .unit = "flags" },
171cabdff1aSopenharmony_ci#elif ARCH_AARCH64
172cabdff1aSopenharmony_ci        { "armv8",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV8    },    .unit = "flags" },
173cabdff1aSopenharmony_ci        { "neon",     NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON     },    .unit = "flags" },
174cabdff1aSopenharmony_ci        { "vfp",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP      },    .unit = "flags" },
175cabdff1aSopenharmony_ci#elif ARCH_MIPS
176cabdff1aSopenharmony_ci        { "mmi",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMI      },    .unit = "flags" },
177cabdff1aSopenharmony_ci        { "msa",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MSA      },    .unit = "flags" },
178cabdff1aSopenharmony_ci#elif ARCH_LOONGARCH
179cabdff1aSopenharmony_ci        { "lsx",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_LSX      },    .unit = "flags" },
180cabdff1aSopenharmony_ci        { "lasx",     NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_LASX     },    .unit = "flags" },
181cabdff1aSopenharmony_ci#endif
182cabdff1aSopenharmony_ci        { NULL },
183cabdff1aSopenharmony_ci    };
184cabdff1aSopenharmony_ci    static const AVClass class = {
185cabdff1aSopenharmony_ci        .class_name = "cpuflags",
186cabdff1aSopenharmony_ci        .item_name  = av_default_item_name,
187cabdff1aSopenharmony_ci        .option     = cpuflags_opts,
188cabdff1aSopenharmony_ci        .version    = LIBAVUTIL_VERSION_INT,
189cabdff1aSopenharmony_ci    };
190cabdff1aSopenharmony_ci    const AVClass *pclass = &class;
191cabdff1aSopenharmony_ci
192cabdff1aSopenharmony_ci    return av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, flags);
193cabdff1aSopenharmony_ci}
194cabdff1aSopenharmony_ci
195cabdff1aSopenharmony_ciint av_cpu_count(void)
196cabdff1aSopenharmony_ci{
197cabdff1aSopenharmony_ci    static atomic_int printed = ATOMIC_VAR_INIT(0);
198cabdff1aSopenharmony_ci
199cabdff1aSopenharmony_ci    int nb_cpus = 1;
200cabdff1aSopenharmony_ci    int count   = 0;
201cabdff1aSopenharmony_ci#if HAVE_WINRT
202cabdff1aSopenharmony_ci    SYSTEM_INFO sysinfo;
203cabdff1aSopenharmony_ci#endif
204cabdff1aSopenharmony_ci#if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
205cabdff1aSopenharmony_ci    cpu_set_t cpuset;
206cabdff1aSopenharmony_ci
207cabdff1aSopenharmony_ci    CPU_ZERO(&cpuset);
208cabdff1aSopenharmony_ci
209cabdff1aSopenharmony_ci    if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
210cabdff1aSopenharmony_ci        nb_cpus = CPU_COUNT(&cpuset);
211cabdff1aSopenharmony_ci#elif HAVE_GETPROCESSAFFINITYMASK
212cabdff1aSopenharmony_ci    DWORD_PTR proc_aff, sys_aff;
213cabdff1aSopenharmony_ci    if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
214cabdff1aSopenharmony_ci        nb_cpus = av_popcount64(proc_aff);
215cabdff1aSopenharmony_ci#elif HAVE_SYSCTL && defined(HW_NCPUONLINE)
216cabdff1aSopenharmony_ci    int mib[2] = { CTL_HW, HW_NCPUONLINE };
217cabdff1aSopenharmony_ci    size_t len = sizeof(nb_cpus);
218cabdff1aSopenharmony_ci
219cabdff1aSopenharmony_ci    if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
220cabdff1aSopenharmony_ci        nb_cpus = 0;
221cabdff1aSopenharmony_ci#elif HAVE_SYSCTL && defined(HW_NCPU)
222cabdff1aSopenharmony_ci    int mib[2] = { CTL_HW, HW_NCPU };
223cabdff1aSopenharmony_ci    size_t len = sizeof(nb_cpus);
224cabdff1aSopenharmony_ci
225cabdff1aSopenharmony_ci    if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
226cabdff1aSopenharmony_ci        nb_cpus = 0;
227cabdff1aSopenharmony_ci#elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
228cabdff1aSopenharmony_ci    nb_cpus = sysconf(_SC_NPROC_ONLN);
229cabdff1aSopenharmony_ci#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
230cabdff1aSopenharmony_ci    nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
231cabdff1aSopenharmony_ci#elif HAVE_WINRT
232cabdff1aSopenharmony_ci    GetNativeSystemInfo(&sysinfo);
233cabdff1aSopenharmony_ci    nb_cpus = sysinfo.dwNumberOfProcessors;
234cabdff1aSopenharmony_ci#endif
235cabdff1aSopenharmony_ci
236cabdff1aSopenharmony_ci    if (!atomic_exchange_explicit(&printed, 1, memory_order_relaxed))
237cabdff1aSopenharmony_ci        av_log(NULL, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
238cabdff1aSopenharmony_ci
239cabdff1aSopenharmony_ci    count = atomic_load_explicit(&cpu_count, memory_order_relaxed);
240cabdff1aSopenharmony_ci
241cabdff1aSopenharmony_ci    if (count > 0) {
242cabdff1aSopenharmony_ci        nb_cpus = count;
243cabdff1aSopenharmony_ci        av_log(NULL, AV_LOG_DEBUG, "overriding to %d logical cores\n", nb_cpus);
244cabdff1aSopenharmony_ci    }
245cabdff1aSopenharmony_ci
246cabdff1aSopenharmony_ci    return nb_cpus;
247cabdff1aSopenharmony_ci}
248cabdff1aSopenharmony_ci
249cabdff1aSopenharmony_civoid av_cpu_force_count(int count)
250cabdff1aSopenharmony_ci{
251cabdff1aSopenharmony_ci    atomic_store_explicit(&cpu_count, count, memory_order_relaxed);
252cabdff1aSopenharmony_ci}
253cabdff1aSopenharmony_ci
254cabdff1aSopenharmony_cisize_t av_cpu_max_align(void)
255cabdff1aSopenharmony_ci{
256cabdff1aSopenharmony_ci#if ARCH_MIPS
257cabdff1aSopenharmony_ci    return ff_get_cpu_max_align_mips();
258cabdff1aSopenharmony_ci#elif ARCH_AARCH64
259cabdff1aSopenharmony_ci    return ff_get_cpu_max_align_aarch64();
260cabdff1aSopenharmony_ci#elif ARCH_ARM
261cabdff1aSopenharmony_ci    return ff_get_cpu_max_align_arm();
262cabdff1aSopenharmony_ci#elif ARCH_PPC
263cabdff1aSopenharmony_ci    return ff_get_cpu_max_align_ppc();
264cabdff1aSopenharmony_ci#elif ARCH_X86
265cabdff1aSopenharmony_ci    return ff_get_cpu_max_align_x86();
266cabdff1aSopenharmony_ci#elif ARCH_LOONGARCH
267cabdff1aSopenharmony_ci    return ff_get_cpu_max_align_loongarch();
268cabdff1aSopenharmony_ci#endif
269cabdff1aSopenharmony_ci
270cabdff1aSopenharmony_ci    return 8;
271cabdff1aSopenharmony_ci}
272