History

avfilter/af_anlmdn: fix possible array overflow and increase options limits

Paul B Mahol committed 4 years ago

Commit 41cae501 parent 248986a2 files · +24 −19Browse files

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/af_anlmdn.c+22−17
@@ -33,8 +33,6 @@ · 32 unmodified lines
33#define WEIGHT_LUT_NBITS 2033#define WEIGHT_LUT_NBITS 20
34#define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)34#define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
35 35
36#define SQR(x) ((x) * (x))
37
38typedef struct AudioNLMeansContext {36typedef struct AudioNLMeansContext {
39 const AVClass *class;37 const AVClass *class;
40 38
@@ -75,8 +73,8 @@ enum OutModes { · 34 unmodified lines
75#define AFT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM73#define AFT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
76 74
77static const AVOption anlmdn_options[] = {75static const AVOption anlmdn_options[] = {
78 { "strength", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10, AFT },76 { "strength", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10000, AFT },
79 { "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10, AFT },77 { "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10000, AFT },
80 { "patch", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },78 { "patch", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },
81 { "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },79 { "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },
82 { "research", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AFT },80 { "research", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AFT },
@@ -86,19 +84,26 @@ static const AVOption anlmdn_options[] = { · 3 unmodified lines
86 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AFT, "mode" },84 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AFT, "mode" },
87 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AFT, "mode" },85 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AFT, "mode" },
88 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE},0, 0, AFT, "mode" },86 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE},0, 0, AFT, "mode" },
89 { "smooth", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 15, AFT },87 { "smooth", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 1000, AFT },
90 { "m", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 15, AFT },88 { "m", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 1000, AFT },
91 { NULL }89 { NULL }
92};90};
93 91
94AVFILTER_DEFINE_CLASS(anlmdn);92AVFILTER_DEFINE_CLASS(anlmdn);
95 93
94static inline float sqrdiff(float x, float y)
95{
96 const float diff = x - y;
97
98 return diff * diff;
99}
100
96static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)101static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
97{102{
98 float distance = 0.;103 float distance = 0.;
99 104
100 for (int k = -K; k <= K; k++)105 for (int k = -K; k <= K; k++)
101 distance += SQR(f1[k] - f2[k]);106 distance += sqrdiff(f1[k], f2[k]);
102 107
103 return distance;108 return distance;
104}109}
@@ -110,7 +115,7 @@ static void compute_cache_c(float *cache, const float *f, · 5 unmodified lines
110 int v = 0;115 int v = 0;
111 116
112 for (int j = jj; j < jj + S; j++, v++)117 for (int j = jj; j < jj + S; j++, v++)
113 cache[v] += -SQR(f[i - K - 1] - f[j - K - 1]) + SQR(f[i + K] - f[j + K]);118 cache[v] += -sqrdiff(f[i - K - 1], f[j - K - 1]) + sqrdiff(f[i + K], f[j + K]);
114}119}
115 120
116void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)121void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)
@@ -213,7 +218,9 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) · 96 unmodified lines
213 float *cache = (float *)s->cache->extended_data[ch];218 float *cache = (float *)s->cache->extended_data[ch];
214 const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);219 const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
215 float *dst = (float *)out->extended_data[ch] + s->offset;220 float *dst = (float *)out->extended_data[ch] + s->offset;
216 const float smooth = s->m;221 const float *const weight_lut = s->weight_lut;
222 const float pdiff_lut_scale = s->pdiff_lut_scale;
223 const float smooth = fminf(s->m, WEIGHT_LUT_SIZE / pdiff_lut_scale);
217 224
218 for (int i = S; i < s->H + S; i++) {225 for (int i = S; i < s->H + S; i++) {
219 float P = 0.f, Q = 0.f;226 float P = 0.f, Q = 0.f;
@@ -231,26 +238,24 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) · 11 unmodified lines
231 }238 }
232 239
233 for (int j = 0; j < 2 * S && !ctx->is_disabled; j++) {240 for (int j = 0; j < 2 * S && !ctx->is_disabled; j++) {
234 const float distance = cache[j];241 float distance = cache[j];
235 unsigned weight_lut_idx;242 unsigned weight_lut_idx;
236 float w;243 float w;
237 244
238 if (distance < 0.f) {245 if (distance < 0.f)
239 cache[j] = 0.f;246 cache[j] = distance = 0.f;
240 continue;
241 }
242 w = distance * sw;247 w = distance * sw;
243 if (w >= smooth)248 if (w >= smooth)
244 continue;249 continue;
245 weight_lut_idx = w * s->pdiff_lut_scale;250 weight_lut_idx = w * pdiff_lut_scale;
246 av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);251 av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);
247 w = s->weight_lut[weight_lut_idx];252 w = weight_lut[weight_lut_idx];
248 P += w * f[i - S + j + (j >= S)];253 P += w * f[i - S + j + (j >= S)];
249 Q += w;254 Q += w;
250 }255 }
251 256
252 P += f[i];257 P += f[i];
253 Q += 1;258 Q += 1.f;
254 259
255 switch (om) {260 switch (om) {
256 case IN_MODE: dst[i - S] = f[i]; break;261 case IN_MODE: dst[i - S] = f[i]; break;

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