Unified · Split
| @@ -21,12 +21,12 @@ · 20 unmodified lines | ||
| 21 | 21 | #include <float.h> |
| 22 | 22 | |
| 23 | 23 | #include "libavutil/avassert.h" |
| 24 | −#include "libavutil/audio_fifo.h" | |
| 25 | 24 | #include "libavutil/avstring.h" |
| 26 | 25 | #include "libavutil/opt.h" |
| 27 | 26 | #include "avfilter.h" |
| 28 | 27 | #include "audio.h" |
| 29 | 28 | #include "formats.h" |
| 29 | +#include "filters.h" | |
| 30 | 30 | |
| 31 | 31 | #include "af_anlmdndsp.h" |
| 32 | 32 | |
| @@ -50,14 +50,8 @@ typedef struct AudioNLMeansContext { · 17 unmodified lines | ||
| 50 | 50 | int N; |
| 51 | 51 | int H; |
| 52 | 52 | |
| 53 | − int offset; | |
| 54 | − AVFrame *in; | |
| 55 | 53 | AVFrame *cache; |
| 56 | − | |
| 57 | − int64_t pts; | |
| 58 | − | |
| 59 | − AVAudioFifo *fifo; | |
| 60 | − int eof_left; | |
| 54 | + AVFrame *window; | |
| 61 | 55 | |
| 62 | 56 | AudioNLMDNDSPContext dsp; |
| 63 | 57 | } AudioNLMeansContext; |
| @@ -132,7 +126,6 @@ static int config_filter(AVFilterContext *ctx) · 68 unmodified lines | ||
| 132 | 126 | AudioNLMeansContext *s = ctx->priv; |
| 133 | 127 | AVFilterLink *outlink = ctx->outputs[0]; |
| 134 | 128 | int newK, newS, newH, newN; |
| 135 | − AVFrame *new_in, *new_cache; | |
| 136 | 129 | |
| 137 | 130 | newK = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE); |
| 138 | 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 | 136 | av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", newK, newS, newH, newN); |
| 144 | 137 | |
| 145 | 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 | 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 | 144 | av_frame_free(&s->cache); |
| 149 | 145 | s->cache = new_cache; |
| 150 | 146 | } else { |
| @@ -154,6 +150,21 @@ static int config_filter(AVFilterContext *ctx) · 3 unmodified lines | ||
| 154 | 150 | if (!s->cache) |
| 155 | 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 | 168 | s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE; |
| 158 | 169 | for (int i = 0; i < WEIGHT_LUT_SIZE; i++) { |
| 159 | 170 | float w = -i / s->pdiff_lut_scale; |
| @@ -161,18 +172,6 @@ static int config_filter(AVFilterContext *ctx) · 1 unmodified line | ||
| 161 | 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 | 175 | s->K = newK; |
| 177 | 176 | s->S = newS; |
| 178 | 177 | s->H = newH; |
| @@ -187,21 +186,10 @@ static int config_output(AVFilterLink *outlink) · 8 unmodified lines | ||
| 187 | 186 | AudioNLMeansContext *s = ctx->priv; |
| 188 | 187 | int ret; |
| 189 | 188 | |
| 190 | − s->eof_left = -1; | |
| 191 | − s->pts = AV_NOPTS_VALUE; | |
| 192 | − | |
| 193 | 189 | ret = config_filter(ctx); |
| 194 | 190 | if (ret < 0) |
| 195 | 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 | 193 | ff_anlmdn_init(&s->dsp); |
| 206 | 194 | |
| 207 | 195 | return 0; |
| @@ -214,10 +202,10 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) · 6 unmodified lines | ||
| 214 | 202 | const int S = s->S; |
| 215 | 203 | const int K = s->K; |
| 216 | 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 | 206 | float *cache = (float *)s->cache->extended_data[ch]; |
| 219 | 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 | 209 | const float *const weight_lut = s->weight_lut; |
| 222 | 210 | const float pdiff_lut_scale = s->pdiff_lut_scale; |
| 223 | 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 | 260 | AVFilterContext *ctx = inlink->dst; |
| 273 | 261 | AVFilterLink *outlink = ctx->outputs[0]; |
| 274 | 262 | AudioNLMeansContext *s = ctx->priv; |
| 275 | − AVFrame *out = NULL; | |
| 276 | − int available, wanted, ret; | |
| 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; | |
| 263 | + const int offset = s->N - s->H; | |
| 264 | + AVFrame *out; | |
| 288 | 265 | |
| 289 | − if (wanted >= s->H && available >= s->N) { | |
| 290 | − out = ff_get_audio_buffer(outlink, wanted); | |
| 291 | − if (!out) | |
| 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); | |
| 266 | + out = ff_get_audio_buffer(outlink, in->nb_samples); | |
| 267 | + if (!out) | |
| 268 | + return AVERROR(ENOMEM); | |
| 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; | |
| 305 | − available -= s->H; | |
| 273 | + memmove(src, &src[s->H], offset * sizeof(float)); | |
| 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) { | |
| 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 | − } | |
| 278 | + ff_filter_execute(ctx, filter_channel, out, NULL, inlink->channels); | |
| 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 | 289 | AudioNLMeansContext *s = ctx->priv; |
| 327 | − int ret; | |
| 328 | − | |
| 329 | − ret = ff_request_frame(ctx->inputs[0]); | |
| 290 | + AVFrame *in = NULL; | |
| 291 | + int ret = 0, status; | |
| 292 | + int64_t pts; | |
| 330 | 293 | |
| 331 | − if (ret == AVERROR_EOF && s->eof_left != 0) { | |
| 332 | − AVFrame *in; | |
| 294 | + FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); | |
| 333 | 295 | |
| 334 | − if (s->eof_left < 0) | |
| 335 | − s->eof_left = av_audio_fifo_size(s->fifo) - (s->S + s->K); | |
| 336 | − if (s->eof_left <= 0) | |
| 337 | − return AVERROR_EOF; | |
| 338 | − in = ff_get_audio_buffer(outlink, s->H); | |
| 339 | − if (!in) | |
| 340 | − return AVERROR(ENOMEM); | |
| 296 | + ret = ff_inlink_consume_samples(inlink, s->H, s->H, &in); | |
| 297 | + if (ret < 0) | |
| 298 | + return ret; | |
| 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 | 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 | 321 | if (ret < 0) |
| 355 | 322 | return ret; |
| 356 | 323 | |
| 357 | − ret = config_filter(ctx); | |
| 358 | − if (ret < 0) | |
| 359 | − return ret; | |
| 360 | − | |
| 361 | − return 0; | |
| 324 | + return config_filter(ctx); | |
| 362 | 325 | } |
| 363 | 326 | |
| 364 | 327 | static av_cold void uninit(AVFilterContext *ctx) |
| 365 | 328 | { |
| 366 | 329 | AudioNLMeansContext *s = ctx->priv; |
| 367 | 330 | |
| 368 | − av_audio_fifo_free(s->fifo); | |
| 369 | − av_frame_free(&s->in); | |
| 370 | 331 | av_frame_free(&s->cache); |
| 332 | + av_frame_free(&s->window); | |
| 371 | 333 | } |
| 372 | 334 | |
| 373 | 335 | static const AVFilterPad inputs[] = { |
| 374 | 336 | { |
| 375 | 337 | .name = "default", |
| 376 | 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 | 344 | .name = "default", |
| 384 | 345 | .type = AVMEDIA_TYPE_AUDIO, |
| 385 | 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 | 352 | .description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."), |
| 393 | 353 | .priv_size = sizeof(AudioNLMeansContext), |
| 394 | 354 | .priv_class = &anlmdn_class, |
| 355 | + .activate = activate, | |
| 395 | 356 | .uninit = uninit, |
| 396 | 357 | FILTER_INPUTS(inputs), |
| 397 | 358 | FILTER_OUTPUTS(outputs), |