FFmpeg
vf_colordetect.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_COLORDETECT_H
20 #define AVFILTER_COLORDETECT_H
21 
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 #include <libavutil/avassert.h>
26 #include <libavutil/macros.h>
27 #include <libavutil/pixfmt.h>
28 
32  FF_ALPHA_TRANSPARENT = 1 << 0, ///< alpha < alpha_max
33  FF_ALPHA_STRAIGHT = (1 << 1) | FF_ALPHA_TRANSPARENT, ///< alpha < pixel
34  /* No way to positively identify premultiplied alpha */
35 };
36 
37 typedef struct FFColorDetectDSPContext {
38  /* Returns 1 if an out-of-range value was detected, 0 otherwise */
39  int (*detect_range)(const uint8_t *data, ptrdiff_t stride,
40  ptrdiff_t width, ptrdiff_t height,
41  int mpeg_min, int mpeg_max);
42 
43  /* Returns an FFAlphaDetect enum value */
44  int (*detect_alpha)(const uint8_t *color, ptrdiff_t color_stride,
45  const uint8_t *alpha, ptrdiff_t alpha_stride,
46  ptrdiff_t width, ptrdiff_t height,
47  int alpha_max, int mpeg_range, int offset);
49 
52 
55 
56 static inline int ff_detect_range_impl_c(const uint8_t *data, ptrdiff_t stride,
57  ptrdiff_t width, ptrdiff_t height,
58  uint8_t mpeg_min, uint8_t mpeg_max)
59 {
60  while (height--) {
61  uint8_t cond = 0;
62  for (int x = 0; x < width; x++) {
63  const uint8_t val = data[x];
64  cond |= val < mpeg_min || val > mpeg_max;
65  }
66  if (cond)
67  return 1;
68  data += stride;
69  }
70 
71  return 0;
72 }
73 
74 static inline int ff_detect_range_c(const uint8_t *data, ptrdiff_t stride,
75  ptrdiff_t width, ptrdiff_t height,
76  int mpeg_min, int mpeg_max)
77 {
78  av_assume(mpeg_min >= 0 && mpeg_min <= UINT8_MAX);
79  av_assume(mpeg_max >= 0 && mpeg_max <= UINT8_MAX);
80  return ff_detect_range_impl_c(data, stride, width, height, mpeg_min, mpeg_max);
81 }
82 
83 static inline int ff_detect_range16_impl_c(const uint8_t *data, ptrdiff_t stride,
84  ptrdiff_t width, ptrdiff_t height,
85  uint16_t mpeg_min, uint16_t mpeg_max)
86 {
87  while (height--) {
88  const uint16_t *data16 = (const uint16_t *) data;
89  uint8_t cond = 0;
90  for (int x = 0; x < width; x++) {
91  const uint16_t val = data16[x];
92  cond |= val < mpeg_min || val > mpeg_max;
93  }
94  if (cond)
95  return 1;
96  data += stride;
97  }
98 
99  return 0;
100 }
101 
102 static inline int ff_detect_range16_c(const uint8_t *data, ptrdiff_t stride,
103  ptrdiff_t width, ptrdiff_t height,
104  int mpeg_min, int mpeg_max)
105 {
106  av_assume(mpeg_min >= 0 && mpeg_min <= UINT16_MAX);
107  av_assume(mpeg_max >= 0 && mpeg_max <= UINT16_MAX);
108  return ff_detect_range16_impl_c(data, stride, width, height, mpeg_min, mpeg_max);
109 }
110 
111 static inline int
112 ff_detect_alpha_full_c(const uint8_t *color, ptrdiff_t color_stride,
113  const uint8_t *alpha, ptrdiff_t alpha_stride,
114  ptrdiff_t width, ptrdiff_t height,
115  int alpha_max, int mpeg_range, int offset)
116 {
117  uint8_t transparent = 0;
118  while (height--) {
119  uint8_t straight = 0;
120  for (int x = 0; x < width; x++) {
121  straight |= color[x] > alpha[x];
122  transparent |= alpha[x] != alpha_max;
123  }
124  if (straight)
125  return FF_ALPHA_STRAIGHT;
126  color += color_stride;
127  alpha += alpha_stride;
128  }
129  return transparent ? FF_ALPHA_TRANSPARENT : 0;
130 }
131 
132 static inline int
133 ff_detect_alpha_limited_c(const uint8_t *color, ptrdiff_t color_stride,
134  const uint8_t *alpha, ptrdiff_t alpha_stride,
135  ptrdiff_t width, ptrdiff_t height,
136  int alpha_max, int mpeg_range, int offset)
137 {
138  uint8_t transparent = 0;
139  while (height--) {
140  uint8_t straight = 0;
141  for (int x = 0; x < width; x++) {
142  straight |= alpha_max * color[x] - offset > mpeg_range * alpha[x];
143  transparent |= alpha[x] != alpha_max;
144  }
145  if (straight)
146  return FF_ALPHA_STRAIGHT;
147  color += color_stride;
148  alpha += alpha_stride;
149  }
150  return transparent ? FF_ALPHA_TRANSPARENT : 0;
151 }
152 
153 static inline int
154 ff_detect_alpha16_full_c(const uint8_t *color, ptrdiff_t color_stride,
155  const uint8_t *alpha, ptrdiff_t alpha_stride,
156  ptrdiff_t width, ptrdiff_t height,
157  int alpha_max, int mpeg_range, int offset)
158 {
159  uint8_t transparent = 0;
160  while (height--) {
161  const uint16_t *color16 = (const uint16_t *) color;
162  const uint16_t *alpha16 = (const uint16_t *) alpha;
163  uint8_t straight = 0;
164  for (int x = 0; x < width; x++) {
165  straight |= color16[x] > alpha16[x];
166  transparent |= alpha16[x] != alpha_max;
167  }
168  if (straight)
169  return FF_ALPHA_STRAIGHT;
170  color += color_stride;
171  alpha += alpha_stride;
172  }
173  return transparent ? FF_ALPHA_TRANSPARENT : 0;
174 }
175 
176 static inline int
177 ff_detect_alpha16_limited_c(const uint8_t *color, ptrdiff_t color_stride,
178  const uint8_t *alpha, ptrdiff_t alpha_stride,
179  ptrdiff_t width, ptrdiff_t height,
180  int alpha_max, int mpeg_range, int offset)
181 {
182  uint8_t transparent = 0;
183  while (height--) {
184  const uint16_t *color16 = (const uint16_t *) color;
185  const uint16_t *alpha16 = (const uint16_t *) alpha;
186  for (int x = 0; x < width; x++) {
187  if ((int64_t) alpha_max * color16[x] - offset > (int64_t) mpeg_range * alpha16[x])
188  return FF_ALPHA_STRAIGHT;
189  transparent |= alpha16[x] != alpha_max;
190  }
191  color += color_stride;
192  alpha += alpha_stride;
193  }
194  return transparent ? FF_ALPHA_TRANSPARENT : 0;
195 }
196 
197 #endif /* AVFILTER_COLORDETECT_H */
FF_ALPHA_TRANSPARENT
@ FF_ALPHA_TRANSPARENT
alpha < alpha_max
Definition: vf_colordetect.h:32
color
Definition: vf_paletteuse.c:513
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_colordetect.h:133
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_colordetect.h:83
int64_t
long long int64_t
Definition: coverity.c:34
data
const char data[16]
Definition: mxf.c:149
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_colordetect.h:177
macros.h
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_colordetect.h:154
val
static double val(void *priv, double ch)
Definition: aeval.c:77
avassert.h
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_colordetect.h:112
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
color_range
color_range
Definition: vf_selectivecolor.c:43
av_assume
#define av_assume(cond)
Definition: avassert.h:111
FFAlphaDetect
FFAlphaDetect
Definition: vf_colordetect.h:29
FF_ALPHA_STRAIGHT
@ FF_ALPHA_STRAIGHT
alpha < pixel
Definition: vf_colordetect.h:33
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:79
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_colordetect.h:102
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_colordetect.h:44
FF_ALPHA_UNDETERMINED
@ FF_ALPHA_UNDETERMINED
Definition: vf_colordetect.h:31
height
#define height
Definition: dsp.h:89
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
ff_color_detect_dsp_init
void ff_color_detect_dsp_init(FFColorDetectDSPContext *dsp, int depth, enum AVColorRange color_range)
Definition: vf_colordetect.c:229
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_colordetect.h:39
stride
#define stride
Definition: h264pred_template.c:536
pixfmt.h
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_colordetect.h:56
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
FFColorDetectDSPContext
Definition: vf_colordetect.h:37
width
#define width
Definition: dsp.h:89
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:732
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_colordetect.h:74
cond
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28
FF_ALPHA_NONE
@ FF_ALPHA_NONE
Definition: vf_colordetect.h:30