7#include "libavcodec/avcodec.h"
24 int excluderow,
int excludecol,
int excludewidth,
int excludeheight)
33 const size_t srcwidth = src->linesize[0];
35 memset(sgm, 0, srcwidth * srcheight *
sizeof(*sgm));
36 int rr2 = srcheight - 1;
37 int cc2 = srcwidth - 1;
38 for (
int rr = 0; rr < rr2; rr++)
40 for (
int cc = 0; cc < cc2; cc++)
42 if (!
rrccinrect(rr, cc, excluderow, excludecol,
43 excludewidth, excludeheight))
45 uchar *rr0 = &src->data[0][(rr * srcwidth) + cc];
46 uchar *rr1 = &src->data[0][((rr + 1) * srcwidth) + cc];
47 int dx = rr1[1] - rr0[0];
48 int dy = rr1[0] - rr0[1];
49 sgm[(rr * srcwidth) + cc] = (dx * dx) + (dy * dy);
58sgm_init(
unsigned int *sgm,
const AVFrame *src,
int srcheight)
66 return *(
unsigned int*)aa - *(
unsigned int*)bb;
71 int extratop,
int extraright,
72 [[maybe_unused]]
int extrabottom,
74 const unsigned int *sgm,
unsigned int *sgmsorted,
int percentile,
75 int excluderow,
int excludecol,
int excludewidth,
int excludeheight)
85 static constexpr int kMinThresholdPct = 95;
87 const int dstwidth = dst->linesize[0];
88 const int padded_width = extraleft + dstwidth + extraright;
97 for (
int rr = 0; rr < dstheight; rr++)
99 for (
int cc = 0; cc < dstwidth; cc++)
101 if (!
rrccinrect(rr, cc, excluderow, excludecol,
102 excludewidth, excludeheight))
104 sgmsorted[nn++] = sgm[((extratop + rr) * padded_width) +
110 int dstnn = dstwidth * dstheight;
113 (min(max(0, excluderow + excludeheight), dstheight) -
114 min(max(0, excluderow), dstheight)) *
115 (min(max(0, excludecol + excludewidth), dstwidth) -
116 min(max(0, excludecol), dstwidth)));
118 memset(dst->data[0], 0, dstnn *
sizeof(*dst->data[0]));
128 int ii = percentile * nn / 100;
129 uint thresholdval = sgmsorted[ii];
136 for ( ; first > 0 && sgmsorted[first] == thresholdval; first--) ;
137 if (sgmsorted[first] != thresholdval)
139 if (first * 100 / nn < kMinThresholdPct)
143 for ( ; last < last2 && sgmsorted[last] == thresholdval;
145 if (sgmsorted[last] != thresholdval)
148 uint newthresholdval = sgmsorted[std::min(last + 1, nn - 1)];
149 if (thresholdval == newthresholdval)
155 thresholdval = newthresholdval;
159 for (
int rr = 0; rr < dstheight; rr++)
161 for (
int cc = 0; cc < dstwidth; cc++)
163 if (!
rrccinrect(rr, cc, excluderow, excludecol,
164 excludewidth, excludeheight) &&
165 sgm[((extratop + rr) * padded_width) + extraleft + cc] >=
167 dst->data[0][(rr * dstwidth) + cc] = UCHAR_MAX;
174int edge_mark_uniform(
AVFrame *dst,
int dstheight,
int extramargin,
175 const unsigned int *sgm,
unsigned int *sgmsorted,
179 extramargin, extramargin, extramargin, extramargin,
180 sgm, sgmsorted, percentile, 0, 0, 0, 0);
185 const unsigned int *sgm,
unsigned int *sgmsorted,
int percentile,
186 int excluderow,
int excludecol,
int excludewidth,
int excludeheight)
189 extramargin, extramargin, extramargin, extramargin,
190 sgm, sgmsorted, percentile,
191 excluderow, excludecol, excludewidth, excludeheight);
198 [[maybe_unused]]
int col,
199 [[maybe_unused]]
int width,
200 [[maybe_unused]]
int height)
virtual int setExcludeArea(int row, int col, int width, int height)
int edge_mark_uniform_exclude(AVFrame *dst, int dstheight, int extramargin, const unsigned int *sgm, unsigned int *sgmsorted, int percentile, int excluderow, int excludecol, int excludewidth, int excludeheight)
static int edge_mark(AVFrame *dst, int dstheight, int extratop, int extraright, int extrabottom, int extraleft, const unsigned int *sgm, unsigned int *sgmsorted, int percentile, int excluderow, int excludecol, int excludewidth, int excludeheight)
unsigned int * sgm_init_exclude(unsigned int *sgm, const AVFrame *src, int srcheight, int excluderow, int excludecol, int excludewidth, int excludeheight)
static int sort_ascending(const void *aa, const void *bb)
bool rrccinrect(int rr, int cc, int rrow, int rcol, int rwidth, int rheight)