avfilter/vf_bilateral: simplify code a little

Make alpha_ calculation faster.
This commit is contained in:
Paul B Mahol 2020-07-17 20:29:48 +02:00
parent 36e51c190b
commit fa8345cf05

View File

@ -41,6 +41,7 @@ typedef struct BilateralContext {
int planewidth[4]; int planewidth[4];
int planeheight[4]; int planeheight[4];
float alpha;
float range_table[65536]; float range_table[65536];
float *img_out_f; float *img_out_f;
@ -99,10 +100,11 @@ static int config_input(AVFilterLink *inlink)
s->depth = desc->comp[0].depth; s->depth = desc->comp[0].depth;
inv_sigma_range = 1.0f / (s->sigmaR * ((1 << s->depth) - 1)); inv_sigma_range = 1.0f / (s->sigmaR * ((1 << s->depth) - 1));
s->alpha = expf(-sqrtf(2.f) / s->sigmaS);
//compute a lookup table //compute a lookup table
for (int i = 0; i < (1 << s->depth); i++) for (int i = 0; i < (1 << s->depth); i++)
s->range_table[i] = expf(-i * inv_sigma_range); s->range_table[i] = s->alpha * expf(-i * inv_sigma_range);
s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
s->planewidth[0] = s->planewidth[3] = inlink->w; s->planewidth[0] = s->planewidth[3] = inlink->w;
@ -144,10 +146,10 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
float *map_factor_a = s->map_factor_a, *map_factor_b = s->map_factor_b; \ float *map_factor_a = s->map_factor_a, *map_factor_b = s->map_factor_b; \
float *slice_factor_a = s->slice_factor_a, *slice_factor_b = s->slice_factor_b; \ float *slice_factor_a = s->slice_factor_a, *slice_factor_b = s->slice_factor_b; \
float *line_factor_a = s->line_factor_a, *line_factor_b = s->line_factor_b; \ float *line_factor_a = s->line_factor_a, *line_factor_b = s->line_factor_b; \
float *range_table = s->range_table; \ const float *range_table = s->range_table; \
float alpha = expf(-sqrtf(2.f) / sigma_spatial); \ const float alpha = s->alpha; \
float ypr, ycr, *ycy, *ypy, *xcy, fp, fc; \ float ypr, ycr, *ycy, *ypy, *xcy, fp, fc; \
float inv_alpha_ = 1 - alpha; \ const float inv_alpha_ = 1.f - alpha; \
float *ycf, *ypf, *xcf, *in_factor; \ float *ycf, *ypf, *xcf, *in_factor; \
const type *tcy, *tpy; \ const type *tcy, *tpy; \
int h1; \ int h1; \
@ -165,14 +167,13 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
*temp_factor_x++ = fp = 1; \ *temp_factor_x++ = fp = 1; \
\ \
for (int x = 1; x < width; x++) { \ for (int x = 1; x < width; x++) { \
float weight, alpha_; \ float alpha_; \
int range_dist; \ int range_dist; \
type tcr = *texture_x++; \ type tcr = *texture_x++; \
type dr = abs(tcr - tpr); \ type dr = abs(tcr - tpr); \
\ \
range_dist = dr; \ range_dist = dr; \
weight = range_table[range_dist]; \ alpha_ = range_table[range_dist]; \
alpha_ = weight*alpha; \
*temp_x++ = ycr = inv_alpha_*(*in_x++) + alpha_*ypr; \ *temp_x++ = ycr = inv_alpha_*(*in_x++) + alpha_*ypr; \
tpr = tcr; \ tpr = tcr; \
ypr = ycr; \ ypr = ycr; \
@ -190,8 +191,7 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
type tcr = *--texture_x; \ type tcr = *--texture_x; \
type dr = abs(tcr - tpr); \ type dr = abs(tcr - tpr); \
int range_dist = dr; \ int range_dist = dr; \
float weight = range_table[range_dist]; \ float alpha_ = range_table[range_dist]; \
float alpha_ = weight * alpha; \
\ \
ycr = inv_alpha_ * (*--in_x) + alpha_ * ypr; \ ycr = inv_alpha_ * (*--in_x) + alpha_ * ypr; \
--temp_x; *temp_x = 0.5f*((*temp_x) + ycr); \ --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 *
} \ } \
memcpy(img_out_f, img_temp, sizeof(float) * width); \ memcpy(img_out_f, img_temp, sizeof(float) * width); \
\ \
alpha = expf(-sqrtf(2.f) / sigma_spatial); \
inv_alpha_ = 1 - alpha; \
in_factor = map_factor_a; \ in_factor = map_factor_a; \
memcpy(map_factor_b, in_factor, sizeof(float) * width); \ memcpy(map_factor_b, in_factor, sizeof(float) * width); \
for (int y = 1; y < height; y++) { \ for (int y = 1; y < height; y++) { \
@ -223,8 +221,7 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
for (int x = 0; x < width; x++) { \ for (int x = 0; x < width; x++) { \
type dr = abs((*tcy++) - (*tpy++)); \ type dr = abs((*tcy++) - (*tpy++)); \
int range_dist = dr; \ int range_dist = dr; \
float weight = range_table[range_dist]; \ float alpha_ = range_table[range_dist]; \
float alpha_ = weight*alpha; \
\ \
*ycy++ = inv_alpha_*(*xcy++) + alpha_*(*ypy++); \ *ycy++ = inv_alpha_*(*xcy++) + alpha_*(*ypy++); \
*ycf++ = inv_alpha_*(*xcf++) + alpha_*(*ypf++); \ *ycf++ = inv_alpha_*(*xcf++) + alpha_*(*ypf++); \
@ -263,8 +260,7 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
for (int x = 0; x < width; x++) { \ for (int x = 0; x < width; x++) { \
type dr = abs((*tcy++) - (*tpy++)); \ type dr = abs((*tcy++) - (*tpy++)); \
int range_dist = dr; \ int range_dist = dr; \
float weight = range_table[range_dist]; \ float alpha_ = range_table[range_dist]; \
float alpha_ = weight*alpha; \
float ycc, fcc = inv_alpha_*(*xcf++) + alpha_*(*ypf_++); \ float ycc, fcc = inv_alpha_*(*xcf++) + alpha_*(*ypf_++); \
\ \
*ycf_++ = fcc; \ *ycf_++ = fcc; \