Showing the changes to libavfilter (1 of 1 file). Show all
Unified · Split
| @@ -21,12 +21,12 @@ · 20 unmodified lines | |||
| 21 | #include <float.h> | 21 | #include <float.h> |
| 22 | 22 | ||
| 23 | #include "libavutil/avassert.h" | 23 | #include "libavutil/avassert.h" |
| 24 | #include "libavutil/audio_fifo.h" | ||
| 25 | #include "libavutil/avstring.h" | 24 | #include "libavutil/avstring.h" |
| 26 | #include "libavutil/opt.h" | 25 | #include "libavutil/opt.h" |
| 27 | #include "avfilter.h" | 26 | #include "avfilter.h" |
| 28 | #include "audio.h" | 27 | #include "audio.h" |
| 29 | #include "formats.h" | 28 | #include "formats.h" |
| 29 | #include "filters.h" | ||
| 30 | 30 | ||
| 31 | #include "af_anlmdndsp.h" | 31 | #include "af_anlmdndsp.h" |
| 32 | 32 | ||
| @@ -50,14 +50,8 @@ typedef struct AudioNLMeansContext { · 17 unmodified lines | |||
| 50 | int N; | 50 | int N; |
| 51 | int H; | 51 | int H; |
| 52 | 52 | ||
| 53 | int offset; | ||
| 54 | AVFrame *in; | ||
| 55 | AVFrame *cache; | 53 | AVFrame *cache; |
| 56 | 54 | AVFrame *window; | |
| 57 | int64_t pts; | ||
| 58 | |||
| 59 | AVAudioFifo *fifo; | ||
| 60 | int eof_left; | ||
| 61 | 55 | ||
| 62 | AudioNLMDNDSPContext dsp; | 56 | AudioNLMDNDSPContext dsp; |
| 63 | } AudioNLMeansContext; | 57 | } AudioNLMeansContext; |
| @@ -132,7 +126,6 @@ static int config_filter(AVFilterContext *ctx) · 68 unmodified lines | |||
| 132 | AudioNLMeansContext *s = ctx->priv; | 126 | AudioNLMeansContext *s = ctx->priv; |
| 133 | AVFilterLink *outlink = ctx->outputs[0]; | 127 | AVFilterLink *outlink = ctx->outputs[0]; |
| 134 | int newK, newS, newH, newN; | 128 | int newK, newS, newH, newN; |
| 135 | AVFrame *new_in, *new_cache; | ||
| 136 | 129 | ||
| 137 | newK = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE); | 130 | newK = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE); |
| 138 | newS = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE); | 131 | newS = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE); |
| @@ -143,8 +136,11 @@ static int config_filter(AVFilterContext *ctx) · 4 unmodified lines | |||
| 143 | av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", newK, newS, newH, newN); | 136 | av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", newK, newS, newH, newN); |
| 144 | 137 | ||
| 145 | if (!s->cache || s->cache->nb_samples < newS * 2) { | 138 | if (!s->cache || s->cache->nb_samples < newS * 2) { |
| 146 | new_cache = ff_get_audio_buffer(outlink, newS * 2); | 139 | AVFrame *new_cache = ff_get_audio_buffer(outlink, newS * 2); |
| 147 | if (new_cache) { | 140 | if (new_cache) { |
| 141 | if (s->cache) | ||
| 142 | av_samples_copy(new_cache->extended_data, s->cache->extended_data, 0, 0, | ||
| 143 | s->cache->nb_samples, new_cache->channels, new_cache->format); | ||
| 148 | av_frame_free(&s->cache); | 144 | av_frame_free(&s->cache); |
| 149 | s->cache = new_cache; | 145 | s->cache = new_cache; |
| 150 | } else { | 146 | } else { |
| @@ -154,6 +150,21 @@ static int config_filter(AVFilterContext *ctx) · 3 unmodified lines | |||
| 154 | if (!s->cache) | 150 | if (!s->cache) |
| 155 | return AVERROR(ENOMEM); | 151 | return AVERROR(ENOMEM); |
| 156 | 152 | ||
| 153 | if (!s->window || s->window->nb_samples < newN) { | ||
| 154 | AVFrame *new_window = ff_get_audio_buffer(outlink, newN); | ||
| 155 | if (new_window) { | ||
| 156 | if (s->window) | ||
| 157 | av_samples_copy(new_window->extended_data, s->window->extended_data, 0, 0, | ||
| 158 | s->window->nb_samples, new_window->channels, new_window->format); | ||
| 159 | av_frame_free(&s->window); | ||
| 160 | s->window = new_window; | ||
| 161 | } else { | ||
| 162 | return AVERROR(ENOMEM); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | if (!s->window) | ||
| 166 | return AVERROR(ENOMEM); | ||
| 167 | |||
| 157 | s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE; | 168 | s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE; |
| 158 | for (int i = 0; i < WEIGHT_LUT_SIZE; i++) { | 169 | for (int i = 0; i < WEIGHT_LUT_SIZE; i++) { |
| 159 | float w = -i / s->pdiff_lut_scale; | 170 | float w = -i / s->pdiff_lut_scale; |
| @@ -161,18 +172,6 @@ static int config_filter(AVFilterContext *ctx) · 1 unmodified line | |||
| 161 | s->weight_lut[i] = expf(w); | 172 | s->weight_lut[i] = expf(w); |
| 162 | } | 173 | } |
| 163 | 174 | ||
| 164 | if (!s->in || s->in->nb_samples < newN) { | ||
| 165 | new_in = ff_get_audio_buffer(outlink, newN); | ||
| 166 | if (new_in) { | ||
| 167 | av_frame_free(&s->in); | ||
| 168 | s->in = new_in; | ||
| 169 | } else { | ||
| 170 | return AVERROR(ENOMEM); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | if (!s->in) | ||
| 174 | return AVERROR(ENOMEM); | ||
| 175 | |||
| 176 | s->K = newK; | 175 | s->K = newK; |
| 177 | s->S = newS; | 176 | s->S = newS; |
| 178 | s->H = newH; | 177 | s->H = newH; |
| @@ -187,21 +186,10 @@ static int config_output(AVFilterLink *outlink) · 8 unmodified lines | |||
| 187 | AudioNLMeansContext *s = ctx->priv; | 186 | AudioNLMeansContext *s = ctx->priv; |
| 188 | int ret; | 187 | int ret; |
| 189 | 188 | ||
| 190 | s->eof_left = -1; | ||
| 191 | s->pts = AV_NOPTS_VALUE; | ||
| 192 | |||
| 193 | ret = config_filter(ctx); | 189 | ret = config_filter(ctx); |
| 194 | if (ret < 0) | 190 | if (ret < 0) |
| 195 | return ret; | 191 | return ret; |
| 196 | 192 | ||
| 197 | s->fifo = av_audio_fifo_alloc(outlink->format, outlink->channels, s->N); | ||
| 198 | if (!s->fifo) | ||
| 199 | return AVERROR(ENOMEM); | ||
| 200 | |||
| 201 | ret = av_audio_fifo_write(s->fifo, (void **)s->in->extended_data, s->K + s->S); | ||
| 202 | if (ret < 0) | ||
| 203 | return ret; | ||
| 204 | |||
| 205 | ff_anlmdn_init(&s->dsp); | 193 | ff_anlmdn_init(&s->dsp); |
| 206 | 194 | ||
| 207 | return 0; | 195 | return 0; |
| @@ -214,10 +202,10 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) · 6 unmodified lines | |||
| 214 | const int S = s->S; | 202 | const int S = s->S; |
| 215 | const int K = s->K; | 203 | const int K = s->K; |
| 216 | const int om = s->om; | 204 | const int om = s->om; |
| 217 | const float *f = (const float *)(s->in->extended_data[ch]) + K; | 205 | const float *f = (const float *)(s->window->extended_data[ch]) + K; |
| 218 | float *cache = (float *)s->cache->extended_data[ch]; | 206 | float *cache = (float *)s->cache->extended_data[ch]; |
| 219 | const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a); | 207 | const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a); |
| 220 | float *dst = (float *)out->extended_data[ch] + s->offset; | 208 | float *dst = (float *)out->extended_data[ch]; |
| 221 | const float *const weight_lut = s->weight_lut; | 209 | const float *const weight_lut = s->weight_lut; |
| 222 | const float pdiff_lut_scale = s->pdiff_lut_scale; | 210 | const float pdiff_lut_scale = s->pdiff_lut_scale; |
| 223 | const float smooth = fminf(s->m, WEIGHT_LUT_SIZE / pdiff_lut_scale); | 211 | const float smooth = fminf(s->m, WEIGHT_LUT_SIZE / pdiff_lut_scale); |
| @@ -272,77 +260,56 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 48 unmodified lines | |||
| 272 | AVFilterContext *ctx = inlink->dst; | 260 | AVFilterContext *ctx = inlink->dst; |
| 273 | AVFilterLink *outlink = ctx->outputs[0]; | 261 | AVFilterLink *outlink = ctx->outputs[0]; |
| 274 | AudioNLMeansContext *s = ctx->priv; | 262 | AudioNLMeansContext *s = ctx->priv; |
| 275 | AVFrame *out = NULL; | 263 | const int offset = s->N - s->H; |
| 276 | int available, wanted, ret; | 264 | AVFrame *out; |
| 277 | |||
| 278 | if (s->pts == AV_NOPTS_VALUE) | ||
| 279 | s->pts = in->pts; | ||
| 280 | |||
| 281 | ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data, | ||
| 282 | in->nb_samples); | ||
| 283 | av_frame_free(&in); | ||
| 284 | |||
| 285 | s->offset = 0; | ||
| 286 | available = av_audio_fifo_size(s->fifo); | ||
| 287 | wanted = (available / s->H) * s->H; | ||
| 288 | 265 | ||
| 289 | if (wanted >= s->H && available >= s->N) { | 266 | out = ff_get_audio_buffer(outlink, in->nb_samples); |
| 290 | out = ff_get_audio_buffer(outlink, wanted); | 267 | if (!out) |
| 291 | if (!out) | 268 | return AVERROR(ENOMEM); |
| 292 | return AVERROR(ENOMEM); | ||
| 293 | } | ||
| 294 | |||
| 295 | while (available >= s->N) { | ||
| 296 | ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data, s->N); | ||
| 297 | if (ret < 0) | ||
| 298 | break; | ||
| 299 | |||
| 300 | ff_filter_execute(ctx, filter_channel, out, NULL, inlink->channels); | ||
| 301 | 269 | ||
| 302 | av_audio_fifo_drain(s->fifo, s->H); | 270 | for (int ch = 0; ch < in->channels; ch++) { |
| 271 | float *src = (float *)s->window->extended_data[ch]; | ||
| 303 | 272 | ||
| 304 | s->offset += s->H; | 273 | memmove(src, &src[s->H], offset * sizeof(float)); |
| 305 | available -= s->H; | 274 | memcpy(&src[offset], in->extended_data[ch], in->nb_samples * sizeof(float)); |
| 275 | memset(&src[offset + in->nb_samples], 0, (s->H - in->nb_samples) * sizeof(float)); | ||
| 306 | } | 276 | } |
| 307 | 277 | ||
| 308 | if (out) { | 278 | ff_filter_execute(ctx, filter_channel, out, NULL, inlink->channels); |
| 309 | out->pts = s->pts; | ||
| 310 | out->nb_samples = s->offset; | ||
| 311 | if (s->eof_left >= 0) { | ||
| 312 | out->nb_samples = FFMIN(s->eof_left, s->offset); | ||
| 313 | s->eof_left -= out->nb_samples; | ||
| 314 | } | ||
| 315 | s->pts += av_rescale_q(s->offset, (AVRational){1, outlink->sample_rate}, outlink->time_base); | ||
| 316 | |||
| 317 | return ff_filter_frame(outlink, out); | ||
| 318 | } | ||
| 319 | 279 | ||
| 320 | return ret; | 280 | out->pts = in->pts; |
| 281 | av_frame_free(&in); | ||
| 282 | return ff_filter_frame(outlink, out); | ||
| 321 | } | 283 | } |
| 322 | 284 | ||
| 323 | static int request_frame(AVFilterLink *outlink) | 285 | static int activate(AVFilterContext *ctx) |
| 324 | { | 286 | { |
| 325 | AVFilterContext *ctx = outlink->src; | 287 | AVFilterLink *inlink = ctx->inputs[0]; |
| 288 | AVFilterLink *outlink = ctx->outputs[0]; | ||
| 326 | AudioNLMeansContext *s = ctx->priv; | 289 | AudioNLMeansContext *s = ctx->priv; |
| 327 | int ret; | 290 | AVFrame *in = NULL; |
| 328 | 291 | int ret = 0, status; | |
| 329 | ret = ff_request_frame(ctx->inputs[0]); | 292 | int64_t pts; |
| 330 | 293 | ||
| 331 | if (ret == AVERROR_EOF && s->eof_left != 0) { | 294 | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
| 332 | AVFrame *in; | ||
| 333 | 295 | ||
| 334 | if (s->eof_left < 0) | 296 | ret = ff_inlink_consume_samples(inlink, s->H, s->H, &in); |
| 335 | s->eof_left = av_audio_fifo_size(s->fifo) - (s->S + s->K); | 297 | if (ret < 0) |
| 336 | if (s->eof_left <= 0) | 298 | return ret; |
| 337 | return AVERROR_EOF; | ||
| 338 | in = ff_get_audio_buffer(outlink, s->H); | ||
| 339 | if (!in) | ||
| 340 | return AVERROR(ENOMEM); | ||
| 341 | 299 | ||
| 342 | return filter_frame(ctx->inputs[0], in); | 300 | if (ret > 0) { |
| 301 | return filter_frame(inlink, in); | ||
| 302 | } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { | ||
| 303 | ff_outlink_set_status(outlink, status, pts); | ||
| 304 | return 0; | ||
| 305 | } else { | ||
| 306 | if (ff_inlink_queued_samples(inlink) >= s->H) { | ||
| 307 | ff_filter_set_ready(ctx, 10); | ||
| 308 | } else if (ff_outlink_frame_wanted(outlink)) { | ||
| 309 | ff_inlink_request_frame(inlink); | ||
| 310 | } | ||
| 311 | return 0; | ||
| 343 | } | 312 | } |
| 344 | |||
| 345 | return ret; | ||
| 346 | } | 313 | } |
| 347 | 314 | ||
| 348 | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, | 315 | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, |
| @@ -354,27 +321,21 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar · 5 unmodified lines | |||
| 354 | if (ret < 0) | 321 | if (ret < 0) |
| 355 | return ret; | 322 | return ret; |
| 356 | 323 | ||
| 357 | ret = config_filter(ctx); | 324 | return config_filter(ctx); |
| 358 | if (ret < 0) | ||
| 359 | return ret; | ||
| 360 | |||
| 361 | return 0; | ||
| 362 | } | 325 | } |
| 363 | 326 | ||
| 364 | static av_cold void uninit(AVFilterContext *ctx) | 327 | static av_cold void uninit(AVFilterContext *ctx) |
| 365 | { | 328 | { |
| 366 | AudioNLMeansContext *s = ctx->priv; | 329 | AudioNLMeansContext *s = ctx->priv; |
| 367 | 330 | ||
| 368 | av_audio_fifo_free(s->fifo); | ||
| 369 | av_frame_free(&s->in); | ||
| 370 | av_frame_free(&s->cache); | 331 | av_frame_free(&s->cache); |
| 332 | av_frame_free(&s->window); | ||
| 371 | } | 333 | } |
| 372 | 334 | ||
| 373 | static const AVFilterPad inputs[] = { | 335 | static const AVFilterPad inputs[] = { |
| 374 | { | 336 | { |
| 375 | .name = "default", | 337 | .name = "default", |
| 376 | .type = AVMEDIA_TYPE_AUDIO, | 338 | .type = AVMEDIA_TYPE_AUDIO, |
| 377 | .filter_frame = filter_frame, | ||
| 378 | }, | 339 | }, |
| 379 | }; | 340 | }; |
| 380 | 341 | ||
| @@ -383,7 +344,6 @@ static const AVFilterPad outputs[] = { · 2 unmodified lines | |||
| 383 | .name = "default", | 344 | .name = "default", |
| 384 | .type = AVMEDIA_TYPE_AUDIO, | 345 | .type = AVMEDIA_TYPE_AUDIO, |
| 385 | .config_props = config_output, | 346 | .config_props = config_output, |
| 386 | .request_frame = request_frame, | ||
| 387 | }, | 347 | }, |
| 388 | }; | 348 | }; |
| 389 | 349 | ||
| @@ -392,6 +352,7 @@ const AVFilter ff_af_anlmdn = { · 2 unmodified lines | |||
| 392 | .description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."), | 352 | .description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."), |
| 393 | .priv_size = sizeof(AudioNLMeansContext), | 353 | .priv_size = sizeof(AudioNLMeansContext), |
| 394 | .priv_class = &anlmdn_class, | 354 | .priv_class = &anlmdn_class, |
| 355 | .activate = activate, | ||
| 395 | .uninit = uninit, | 356 | .uninit = uninit, |
| 396 | FILTER_INPUTS(inputs), | 357 | FILTER_INPUTS(inputs), |
| 397 | FILTER_OUTPUTS(outputs), | 358 | FILTER_OUTPUTS(outputs), |