FFmpeg
vf_colordetect_init.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025 Niklas Haas
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/attributes.h"
22 #include "libavutil/x86/cpu.h"
24 
25 #define DETECT_RANGE_FUNC(FUNC_NAME, ASM_FUNC_NAME, C_FUNC_NAME, SHIFT, MMSIZE) \
26 int ASM_FUNC_NAME(const uint8_t *src, ptrdiff_t stride, \
27  ptrdiff_t width, ptrdiff_t height, int min, int max); \
28  \
29 static int FUNC_NAME(const uint8_t *src, ptrdiff_t stride, \
30  ptrdiff_t width, ptrdiff_t height, int min, int max) \
31 { \
32  ptrdiff_t bytes = (width << SHIFT) & ~(MMSIZE - 1); \
33  int ret = ASM_FUNC_NAME(src, stride, bytes, height, min, max); \
34  if (ret == FF_ALPHA_STRAIGHT) \
35  return ret; \
36  \
37  return ret | C_FUNC_NAME(src + bytes, stride, width - (bytes >> SHIFT), \
38  height, min, max); \
39 }
40 
41 #define DETECT_ALPHA_FUNC(FUNC_NAME, ASM_FUNC_NAME, C_FUNC_NAME, SHIFT, MMSIZE) \
42 int ASM_FUNC_NAME(const uint8_t *color, ptrdiff_t color_stride, \
43  const uint8_t *alpha, ptrdiff_t alpha_stride, \
44  ptrdiff_t width, ptrdiff_t height, int p, int q, int k); \
45  \
46 static int FUNC_NAME(const uint8_t *color, ptrdiff_t color_stride, \
47  const uint8_t *alpha, ptrdiff_t alpha_stride, \
48  ptrdiff_t width, ptrdiff_t height, int p, int q, int k) \
49 { \
50  ptrdiff_t bytes = (width << SHIFT) & ~(MMSIZE - 1); \
51  int ret = ASM_FUNC_NAME(color, color_stride, alpha, alpha_stride, \
52  bytes, height, p, q, k); \
53  if (ret == FF_ALPHA_STRAIGHT) \
54  return ret; \
55  \
56  return ret | C_FUNC_NAME(color + bytes, color_stride, alpha + bytes, \
57  alpha_stride, width - (bytes >> SHIFT), height, \
58  p, q, k); \
59 }
60 
61 #if HAVE_X86ASM
62 #if HAVE_AVX512ICL_EXTERNAL
63 DETECT_RANGE_FUNC(detect_range_avx512icl, ff_detect_rangeb_avx512icl, ff_detect_range_c, 0, 64)
64 DETECT_RANGE_FUNC(detect_range16_avx512icl, ff_detect_rangew_avx512icl, ff_detect_range16_c, 1, 64)
65 DETECT_ALPHA_FUNC(detect_alpha_full_avx512icl, ff_detect_alphab_full_avx512icl, ff_detect_alpha_full_c, 0, 64)
66 DETECT_ALPHA_FUNC(detect_alpha16_full_avx512icl, ff_detect_alphaw_full_avx512icl, ff_detect_alpha16_full_c, 1, 64)
67 DETECT_ALPHA_FUNC(detect_alpha_limited_avx512icl, ff_detect_alphab_limited_avx512icl, ff_detect_alpha_limited_c, 0, 64)
68 DETECT_ALPHA_FUNC(detect_alpha16_limited_avx512icl, ff_detect_alphaw_limited_avx512icl, ff_detect_alpha16_limited_c, 1, 64)
69 #endif
70 #if HAVE_AVX2_EXTERNAL
71 DETECT_RANGE_FUNC(detect_range_avx2, ff_detect_rangeb_avx2, ff_detect_range_c, 0, 32)
72 DETECT_RANGE_FUNC(detect_range16_avx2, ff_detect_rangew_avx2, ff_detect_range16_c, 1, 32)
73 DETECT_ALPHA_FUNC(detect_alpha_full_avx2, ff_detect_alphab_full_avx2, ff_detect_alpha_full_c, 0, 32)
74 DETECT_ALPHA_FUNC(detect_alpha16_full_avx2, ff_detect_alphaw_full_avx2, ff_detect_alpha16_full_c, 1, 32)
75 DETECT_ALPHA_FUNC(detect_alpha_limited_avx2, ff_detect_alphab_limited_avx2, ff_detect_alpha_limited_c, 0, 32)
76 DETECT_ALPHA_FUNC(detect_alpha16_limited_avx2, ff_detect_alphaw_limited_avx2, ff_detect_alpha16_limited_c, 1, 32)
77 #endif
78 #endif
79 
82 {
83 #if HAVE_X86ASM
85 #if HAVE_AVX2_EXTERNAL
87  dsp->detect_range = depth > 8 ? detect_range16_avx2 : detect_range_avx2;
89  dsp->detect_alpha = depth > 8 ? detect_alpha16_full_avx2 : detect_alpha_full_avx2;
90  } else {
91  dsp->detect_alpha = depth > 8 ? detect_alpha16_limited_avx2 : detect_alpha_limited_avx2;
92  }
93  }
94 #endif
95 #if HAVE_AVX512ICL_EXTERNAL
97  dsp->detect_range = depth > 8 ? detect_range16_avx512icl : detect_range_avx512icl;
99  dsp->detect_alpha = depth > 8 ? detect_alpha16_full_avx512icl : detect_alpha_full_avx512icl;
100  } else {
101  dsp->detect_alpha = depth > 8 ? detect_alpha16_limited_avx512icl : detect_alpha_limited_avx512icl;
102  }
103  }
104 #endif
105 #endif
106 }
cpu.h
ff_color_detect_dsp_init_x86
av_cold void ff_color_detect_dsp_init_x86(FFColorDetectDSPContext *dsp, int depth, enum AVColorRange color_range)
Definition: vf_colordetect_init.c:80
EXTERNAL_AVX2_FAST
#define EXTERNAL_AVX2_FAST(flags)
Definition: cpu.h:79
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:767
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:109
cpu_flags
static atomic_int cpu_flags
Definition: cpu.c:56
DETECT_RANGE_FUNC
#define DETECT_RANGE_FUNC(FUNC_NAME, ASM_FUNC_NAME, C_FUNC_NAME, SHIFT, MMSIZE)
Definition: vf_colordetect_init.c:25
DETECT_ALPHA_FUNC
#define DETECT_ALPHA_FUNC(FUNC_NAME, ASM_FUNC_NAME, C_FUNC_NAME, SHIFT, MMSIZE)
Definition: vf_colordetect_init.c:41
av_cold
#define av_cold
Definition: attributes.h:100
vf_colordetectdsp.h
ff_detect_alpha_limited_c
static int ff_detect_alpha_limited_c(const uint8_t *color, ptrdiff_t color_stride, const uint8_t *alpha, ptrdiff_t alpha_stride, ptrdiff_t width, ptrdiff_t height, int alpha_max, int mpeg_range, int offset)
Definition: vf_colordetectdsp.h:134
color_range
color_range
Definition: vf_selectivecolor.c:43
ff_detect_alpha16_full_c
static int ff_detect_alpha16_full_c(const uint8_t *color, ptrdiff_t color_stride, const uint8_t *alpha, ptrdiff_t alpha_stride, ptrdiff_t width, ptrdiff_t height, int alpha_max, int mpeg_range, int offset)
Definition: vf_colordetectdsp.h:155
FFColorDetectDSPContext::detect_alpha
int(* detect_alpha)(const uint8_t *color, ptrdiff_t color_stride, const uint8_t *alpha, ptrdiff_t alpha_stride, ptrdiff_t width, ptrdiff_t height, int alpha_max, int mpeg_range, int offset)
Definition: vf_colordetectdsp.h:46
ff_detect_alpha16_limited_c
static int ff_detect_alpha16_limited_c(const uint8_t *color, ptrdiff_t color_stride, const uint8_t *alpha, ptrdiff_t alpha_stride, ptrdiff_t width, ptrdiff_t height, int alpha_max, int mpeg_range, int offset)
Definition: vf_colordetectdsp.h:178
ff_detect_alpha_full_c
static int ff_detect_alpha_full_c(const uint8_t *color, ptrdiff_t color_stride, const uint8_t *alpha, ptrdiff_t alpha_stride, ptrdiff_t width, ptrdiff_t height, int alpha_max, int mpeg_range, int offset)
Definition: vf_colordetectdsp.h:113
attributes.h
FFColorDetectDSPContext::detect_range
int(* detect_range)(const uint8_t *data, ptrdiff_t stride, ptrdiff_t width, ptrdiff_t height, int mpeg_min, int mpeg_max)
Definition: vf_colordetectdsp.h:41
ff_detect_range16_c
static int ff_detect_range16_c(const uint8_t *data, ptrdiff_t stride, ptrdiff_t width, ptrdiff_t height, int mpeg_min, int mpeg_max)
Definition: vf_colordetectdsp.h:103
ff_detect_range_c
static int ff_detect_range_c(const uint8_t *data, ptrdiff_t stride, ptrdiff_t width, ptrdiff_t height, int mpeg_min, int mpeg_max)
Definition: vf_colordetectdsp.h:75
EXTERNAL_AVX512ICL
#define EXTERNAL_AVX512ICL(flags)
Definition: cpu.h:83
FFColorDetectDSPContext
Definition: vf_colordetectdsp.h:39
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:732