History

avfilter/vf_bilateral: simplify code a little

Make alpha_ calculation faster.

Paul B Mahol committed 6 years ago

Commit fa8345c1 parent 36e51c11 file · +11 −15Browse files

Unified · Split

libavfilter/vf_bilateral.c+11−15
@@ -41,6 +41,7 @@ typedef struct BilateralContext { · 40 unmodified lines
4141 int planewidth[4];
4242 int planeheight[4];
4343
44+ float alpha;
4445 float range_table[65536];
4546
4647 float *img_out_f;
@@ -99,10 +100,11 @@ static int config_input(AVFilterLink *inlink) · 52 unmodified lines
99100
100101 s->depth = desc->comp[0].depth;
101102 inv_sigma_range = 1.0f / (s->sigmaR * ((1 << s->depth) - 1));
103+ s->alpha = expf(-sqrtf(2.f) / s->sigmaS);
102104
103105 //compute a lookup table
104106 for (int i = 0; i < (1 << s->depth); i++)
105− s->range_table[i] = expf(-i * inv_sigma_range);
107+ s->range_table[i] = s->alpha * expf(-i * inv_sigma_range);
106108
107109 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
108110 s->planewidth[0] = s->planewidth[3] = inlink->w;
@@ -144,10 +146,10 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t * · 35 unmodified lines
144146 float *map_factor_a = s->map_factor_a, *map_factor_b = s->map_factor_b; \
145147 float *slice_factor_a = s->slice_factor_a, *slice_factor_b = s->slice_factor_b; \
146148 float *line_factor_a = s->line_factor_a, *line_factor_b = s->line_factor_b; \
147− float *range_table = s->range_table; \
148− float alpha = expf(-sqrtf(2.f) / sigma_spatial); \
149+ const float *range_table = s->range_table; \
150+ const float alpha = s->alpha; \
149151 float ypr, ycr, *ycy, *ypy, *xcy, fp, fc; \
150− float inv_alpha_ = 1 - alpha; \
152+ const float inv_alpha_ = 1.f - alpha; \
151153 float *ycf, *ypf, *xcf, *in_factor; \
152154 const type *tcy, *tpy; \
153155 int h1; \
@@ -165,14 +167,13 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t * · 11 unmodified lines
165167 *temp_factor_x++ = fp = 1; \
166168 \
167169 for (int x = 1; x < width; x++) { \
168− float weight, alpha_; \
170+ float alpha_; \
169171 int range_dist; \
170172 type tcr = *texture_x++; \
171173 type dr = abs(tcr - tpr); \
172174 \
173175 range_dist = dr; \
174− weight = range_table[range_dist]; \
175− alpha_ = weight*alpha; \
176+ alpha_ = range_table[range_dist]; \
176177 *temp_x++ = ycr = inv_alpha_*(*in_x++) + alpha_*ypr; \
177178 tpr = tcr; \
178179 ypr = ycr; \
@@ -190,8 +191,7 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t * · 11 unmodified lines
190191 type tcr = *--texture_x; \
191192 type dr = abs(tcr - tpr); \
192193 int range_dist = dr; \
193− float weight = range_table[range_dist]; \
194− float alpha_ = weight * alpha; \
194+ float alpha_ = range_table[range_dist]; \
195195 \
196196 ycr = inv_alpha_ * (*--in_x) + alpha_ * ypr; \
197197 --temp_x; *temp_x = 0.5f*((*temp_x) + ycr); \
@@ -206,8 +206,6 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t * · 8 unmodified lines
206206 } \
207207 memcpy(img_out_f, img_temp, sizeof(float) * width); \
208208 \
209− alpha = expf(-sqrtf(2.f) / sigma_spatial); \
210− inv_alpha_ = 1 - alpha; \
211209 in_factor = map_factor_a; \
212210 memcpy(map_factor_b, in_factor, sizeof(float) * width); \
213211 for (int y = 1; y < height; y++) { \
@@ -223,8 +221,7 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t * · 9 unmodified lines
223221 for (int x = 0; x < width; x++) { \
224222 type dr = abs((*tcy++) - (*tpy++)); \
225223 int range_dist = dr; \
226− float weight = range_table[range_dist]; \
227− float alpha_ = weight*alpha; \
224+ float alpha_ = range_table[range_dist]; \
228225 \
229226 *ycy++ = inv_alpha_*(*xcy++) + alpha_*(*ypy++); \
230227 *ycf++ = inv_alpha_*(*xcf++) + alpha_*(*ypf++); \
@@ -263,8 +260,7 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t * · 32 unmodified lines
263260 for (int x = 0; x < width; x++) { \
264261 type dr = abs((*tcy++) - (*tpy++)); \
265262 int range_dist = dr; \
266− float weight = range_table[range_dist]; \
267− float alpha_ = weight*alpha; \
263+ float alpha_ = range_table[range_dist]; \
268264 float ycc, fcc = inv_alpha_*(*xcf++) + alpha_*(*ypf_++); \
269265 \
270266 *ycf_++ = fcc; \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t