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
3333 #define WEIGHT_LUT_NBITS 20
3434 #define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
3535
36−#define SQR(x) ((x) * (x))
37−
3836 typedef struct AudioNLMeansContext {
3937 const AVClass *class;
4038
@@ -75,8 +73,8 @@ enum OutModes { · 34 unmodified lines
7573 #define AFT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
7674
7775 static const AVOption anlmdn_options[] = {
78− { "strength", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10, AFT },
79− { "s", "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 },
77+ { "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10000, AFT },
8078 { "patch", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },
8179 { "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },
8280 { "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
8684 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AFT, "mode" },
8785 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AFT, "mode" },
8886 { "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 },
90− { "m", "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 },
88+ { "m", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 1000, AFT },
9189 { NULL }
9290 };
9391
9492 AVFILTER_DEFINE_CLASS(anlmdn);
9593
94+static inline float sqrdiff(float x, float y)
95+{
96+ const float diff = x - y;
97+
98+ return diff * diff;
99+}
100+
96101 static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
97102 {
98103 float distance = 0.;
99104
100105 for (int k = -K; k <= K; k++)
101− distance += SQR(f1[k] - f2[k]);
106+ distance += sqrdiff(f1[k], f2[k]);
102107
103108 return distance;
104109 }
@@ -110,7 +115,7 @@ static void compute_cache_c(float *cache, const float *f, · 5 unmodified lines
110115 int v = 0;
111116
112117 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]);
114119 }
115120
116121 void 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
213218 float *cache = (float *)s->cache->extended_data[ch];
214219 const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
215220 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);
217224
218225 for (int i = S; i < s->H + S; i++) {
219226 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
231238 }
232239
233240 for (int j = 0; j < 2 * S && !ctx->is_disabled; j++) {
234− const float distance = cache[j];
241+ float distance = cache[j];
235242 unsigned weight_lut_idx;
236243 float w;
237244
238− if (distance < 0.f) {
239− cache[j] = 0.f;
240− continue;
241− }
245+ if (distance < 0.f)
246+ cache[j] = distance = 0.f;
242247 w = distance * sw;
243248 if (w >= smooth)
244249 continue;
245− weight_lut_idx = w * s->pdiff_lut_scale;
250+ weight_lut_idx = w * pdiff_lut_scale;
246251 av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);
247− w = s->weight_lut[weight_lut_idx];
252+ w = weight_lut[weight_lut_idx];
248253 P += w * f[i - S + j + (j >= S)];
249254 Q += w;
250255 }
251256
252257 P += f[i];
253− Q += 1;
258+ Q += 1.f;
254259
255260 switch (om) {
256261 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