FFmpeg
vf_colordetectdsp.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVFILTER_COLORDETECTDSP_H
20 #define AVFILTER_COLORDETECTDSP_H
21 
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 #include "config.h"
26 
27 #include "libavutil/attributes.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/pixfmt.h"
30 
34  FF_ALPHA_TRANSPARENT = 1 << 0, ///< alpha < alpha_max
35  FF_ALPHA_STRAIGHT = (1 << 1) | FF_ALPHA_TRANSPARENT, ///< alpha < pixel
36  /* No way to positively identify premultiplied alpha */
37 };
38 
39 typedef struct FFColorDetectDSPContext {
40  /* Returns 1 if an out-of-range value was detected, 0 otherwise */
41  int (*detect_range)(const uint8_t *data, ptrdiff_t stride,
42  ptrdiff_t width, ptrdiff_t height,
43  int mpeg_min, int mpeg_max);
44 
45  /* Returns an FFAlphaDetect enum value */
46  int (*detect_alpha)(const uint8_t *color, ptrdiff_t color_stride,
47  const uint8_t *alpha, ptrdiff_t alpha_stride,
48  ptrdiff_t width, ptrdiff_t height,
49  int alpha_max, int mpeg_range, int offset);
51 
56 
57 static inline int ff_detect_range_impl_c(const uint8_t *data, ptrdiff_t stride,
58  ptrdiff_t width, ptrdiff_t height,
59  uint8_t mpeg_min, uint8_t mpeg_max)
60 {
61  while (height--) {
62  uint8_t cond = 0;
63  for (int x = 0; x < width; x++) {
64  const uint8_t val = data[x];
65  cond |= val < mpeg_min || val > mpeg_max;
66  }
67  if (cond)
68  return 1;
69  data += stride;
70  }
71 
72  return 0;
73 }
74 
75 static inline int ff_detect_range_c(const uint8_t *data, ptrdiff_t stride,
76  ptrdiff_t width, ptrdiff_t height,
77  int mpeg_min, int mpeg_max)
78 {
79  av_assume(mpeg_min >= 0 && mpeg_min <= UINT8_MAX);
80  av_assume(mpeg_max >= 0 && mpeg_max <= UINT8_MAX);
81  return ff_detect_range_impl_c(data, stride, width, height, mpeg_min, mpeg_max);
82 }
83 
84 static inline int ff_detect_range16_impl_c(const uint8_t *data, ptrdiff_t stride,
85  ptrdiff_t width, ptrdiff_t height,
86  uint16_t mpeg_min, uint16_t mpeg_max)
87 {
88  while (height--) {
89  const uint16_t *data16 = (const uint16_t *) data;
90  uint8_t cond = 0;
91  for (int x = 0; x < width; x++) {
92  const uint16_t val = data16[x];
93  cond |= val < mpeg_min || val > mpeg_max;
94  }
95  if (cond)
96  return 1;
97  data += stride;
98  }
99 
100  return 0;
101 }
102 
103 static inline int ff_detect_range16_c(const uint8_t *data, ptrdiff_t stride,
104  ptrdiff_t width, ptrdiff_t height,
105  int mpeg_min, int mpeg_max)
106 {
107  av_assume(mpeg_min >= 0 && mpeg_min <= UINT16_MAX);
108  av_assume(mpeg_max >= 0 && mpeg_max <= UINT16_MAX);
109  return ff_detect_range16_impl_c(data, stride, width, height, mpeg_min, mpeg_max);
110 }
111 
112 static inline int
113 ff_detect_alpha_full_c(const uint8_t *color, ptrdiff_t color_stride,
114  const uint8_t *alpha, ptrdiff_t alpha_stride,
115  ptrdiff_t width, ptrdiff_t height,
116  int alpha_max, int mpeg_range, int offset)
117 {
118  uint8_t transparent = 0;
119  while (height--) {
120  uint8_t straight = 0;
121  for (int x = 0; x < width; x++) {
122  straight |= color[x] > alpha[x];
123  transparent |= alpha[x] != alpha_max;
124  }
125  if (straight)
126  return FF_ALPHA_STRAIGHT;
127  color += color_stride;
128  alpha += alpha_stride;
129  }
130  return transparent ? FF_ALPHA_TRANSPARENT : 0;
131 }
132 
133 static inline int
134 ff_detect_alpha_limited_c(const uint8_t *color, ptrdiff_t color_stride,
135  const uint8_t *alpha, ptrdiff_t alpha_stride,
136  ptrdiff_t width, ptrdiff_t height,
137  int alpha_max, int mpeg_range, int offset)
138 {
139  uint8_t transparent = 0;
140  while (height--) {
141  uint8_t straight = 0;
142  for (int x = 0; x < width; x++) {
143  straight |= alpha_max * color[x] - offset > mpeg_range * alpha[x];
144  transparent |= alpha[x] != alpha_max;
145  }
146  if (straight)
147  return FF_ALPHA_STRAIGHT;
148  color += color_stride;
149  alpha += alpha_stride;
150  }
151  return transparent ? FF_ALPHA_TRANSPARENT : 0;
152 }
153 
154 static inline int
155 ff_detect_alpha16_full_c(const uint8_t *color, ptrdiff_t color_stride,
156  const uint8_t *alpha, ptrdiff_t alpha_stride,
157  ptrdiff_t width, ptrdiff_t height,
158  int alpha_max, int mpeg_range, int offset)
159 {
160  uint8_t transparent = 0;
161  while (height--) {
162  const uint16_t *color16 = (const uint16_t *) color;
163  const uint16_t *alpha16 = (const uint16_t *) alpha;
164  uint8_t straight = 0;
165  for (int x = 0; x < width; x++) {
166  straight |= color16[x] > alpha16[x];
167  transparent |= alpha16[x] != alpha_max;
168  }
169  if (straight)
170  return FF_ALPHA_STRAIGHT;
171  color += color_stride;
172  alpha += alpha_stride;
173  }
174  return transparent ? FF_ALPHA_TRANSPARENT : 0;
175 }
176 
177 static inline int
178 ff_detect_alpha16_limited_c(const uint8_t *color, ptrdiff_t color_stride,
179  const uint8_t *alpha, ptrdiff_t alpha_stride,
180  ptrdiff_t width, ptrdiff_t height,
181  int alpha_max, int mpeg_range, int offset)
182 {
183  uint8_t transparent = 0;
184  while (height--) {
185  const uint16_t *color16 = (const uint16_t *) color;
186  const uint16_t *alpha16 = (const uint16_t *) alpha;
187  for (int x = 0; x < width; x++) {
188  if ((int64_t) alpha_max * color16[x] - offset > (int64_t) mpeg_range * alpha16[x])
189  return FF_ALPHA_STRAIGHT;
190  transparent |= alpha16[x] != alpha_max;
191  }
192  color += color_stride;
193  alpha += alpha_stride;
194  }
195  return transparent ? FF_ALPHA_TRANSPARENT : 0;
196 }
197 
198 static av_cold inline void
201 {
203  if (color_range == AVCOL_RANGE_JPEG) {
205  } else {
207  }
208 
209 #if ARCH_AARCH64
211 #elif ARCH_X86
213 #endif
214 }
215 
216 #endif /* AVFILTER_COLORDETECTDSP_H */
color
Definition: vf_paletteuse.c:513
int64_t
long long int64_t
Definition: coverity.c:34
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:767
data
const char data[16]
Definition: mxf.c:149
val
static double val(void *priv, double ch)
Definition: aeval.c:77
avassert.h
av_cold
#define av_cold
Definition: attributes.h:90
FFAlphaDetect
FFAlphaDetect
Definition: vf_colordetectdsp.h:31
color16
static av_always_inline void color16(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)
Definition: vf_waveform.c:1743
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
av_assume
#define av_assume(cond)
Definition: avassert.h:111
FF_ALPHA_TRANSPARENT
@ FF_ALPHA_TRANSPARENT
alpha < alpha_max
Definition: vf_colordetectdsp.h:34
FF_ALPHA_NONE
@ FF_ALPHA_NONE
Definition: vf_colordetectdsp.h:32
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_color_detect_dsp_init_x86
void ff_color_detect_dsp_init_x86(FFColorDetectDSPContext *dsp, int depth, enum AVColorRange color_range)
Definition: vf_colordetect_init.c:80
ff_detect_range_impl_c
static int ff_detect_range_impl_c(const uint8_t *data, ptrdiff_t stride, ptrdiff_t width, ptrdiff_t height, uint8_t mpeg_min, uint8_t mpeg_max)
Definition: vf_colordetectdsp.h:57
height
#define height
Definition: dsp.h:89
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
color
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:97
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
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
stride
#define stride
Definition: h264pred_template.c:536
pixfmt.h
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
ff_color_detect_dsp_init
static av_cold void ff_color_detect_dsp_init(FFColorDetectDSPContext *dsp, int depth, enum AVColorRange color_range)
Definition: vf_colordetectdsp.h:199
ff_detect_range16_impl_c
static int ff_detect_range16_impl_c(const uint8_t *data, ptrdiff_t stride, ptrdiff_t width, ptrdiff_t height, uint16_t mpeg_min, uint16_t mpeg_max)
Definition: vf_colordetectdsp.h:84
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
FFColorDetectDSPContext
Definition: vf_colordetectdsp.h:39
ff_color_detect_dsp_init_aarch64
void ff_color_detect_dsp_init_aarch64(FFColorDetectDSPContext *dsp, int depth, enum AVColorRange color_range)
Definition: vf_colordetect_init.c:52
width
#define width
Definition: dsp.h:89
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:732
FF_ALPHA_UNDETERMINED
@ FF_ALPHA_UNDETERMINED
Definition: vf_colordetectdsp.h:33
cond
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28
FF_ALPHA_STRAIGHT
@ FF_ALPHA_STRAIGHT
alpha < pixel
Definition: vf_colordetectdsp.h:35