static int config_output(AVFilterLink *outlink) { AVFilterContext *ctx = outlink->src; SpectrumSynthContext *s = ctx->priv; int width = ctx->inputs[0]->w; int height = ctx->inputs[0]->h; AVRational time_base = ctx->inputs[0]->time_base; AVRational frame_rate = ctx->inputs[0]->frame_rate; int i, ch, fft_bits; float factor, overlap; outlink->sample_rate = s->sample_rate; outlink->time_base = (AVRational){1, s->sample_rate}; if (width != ctx->inputs[1]->w || height != ctx->inputs[1]->h) { av_log(ctx, AV_LOG_ERROR, "Magnitude and Phase sizes differ (%dx%d vs %dx%d).\n", width, height, ctx->inputs[1]->w, ctx->inputs[1]->h); return AVERROR_INVALIDDATA; } else if (av_cmp_q(time_base, ctx->inputs[1]->time_base) != 0) { av_log(ctx, AV_LOG_ERROR, "Magnitude and Phase time bases differ (%d/%d vs %d/%d).\n", time_base.num, time_base.den, ctx->inputs[1]->time_base.num, ctx->inputs[1]->time_base.den); return AVERROR_INVALIDDATA; } else if (av_cmp_q(frame_rate, ctx->inputs[1]->frame_rate) != 0) { av_log(ctx, AV_LOG_ERROR, "Magnitude and Phase framerates differ (%d/%d vs %d/%d).\n", frame_rate.num, frame_rate.den, ctx->inputs[1]->frame_rate.num, ctx->inputs[1]->frame_rate.den); return AVERROR_INVALIDDATA; } s->size = s->orientation == VERTICAL ? height / s->channels : width / s->channels; s->xend = s->orientation == VERTICAL ? width : height; for (fft_bits = 1; 1 << fft_bits < 2 * s->size; fft_bits++); s->win_size = 1 << fft_bits; s->nb_freq = 1 << (fft_bits - 1); s->fft = av_fft_init(fft_bits, 1); if (!s->fft) { av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. " "The window size might be too high.\n"); return AVERROR(EINVAL); } s->fft_data = av_calloc(s->channels, sizeof(*s->fft_data)); if (!s->fft_data) return AVERROR(ENOMEM); for (ch = 0; ch < s->channels; ch++) { s->fft_data[ch] = av_calloc(s->win_size, sizeof(**s->fft_data)); if (!s->fft_data[ch]) return AVERROR(ENOMEM); } s->buffer = ff_get_audio_buffer(outlink, s->win_size * 2); if (!s->buffer) return AVERROR(ENOMEM); /* pre-calc windowing function */ s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size, sizeof(*s->window_func_lut)); if (!s->window_func_lut) return AVERROR(ENOMEM); ff_generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap); if (s->overlap == 1) s->overlap = overlap; s->hop_size = (1 - s->overlap) * s->win_size; for (factor = 0, i = 0; i < s->win_size; i++) { factor += s->window_func_lut[i] * s->window_func_lut[i]; } s->factor = (factor / s->win_size) / FFMAX(1 / (1 - s->overlap) - 1, 1); return 0; }
static int config_input(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; AFFTFiltContext *s = ctx->priv; char *saveptr = NULL; int ret = 0, ch, i; float overlap; char *args; const char *last_expr = "1"; s->fft = av_fft_init(s->fft_bits, 0); s->ifft = av_fft_init(s->fft_bits, 1); if (!s->fft || !s->ifft) return AVERROR(ENOMEM); s->window_size = 1 << s->fft_bits; s->fft_data = av_calloc(inlink->channels, sizeof(*s->fft_data)); if (!s->fft_data) return AVERROR(ENOMEM); for (ch = 0; ch < inlink->channels; ch++) { s->fft_data[ch] = av_calloc(s->window_size, sizeof(**s->fft_data)); if (!s->fft_data[ch]) return AVERROR(ENOMEM); } s->real = av_calloc(inlink->channels, sizeof(*s->real)); if (!s->real) return AVERROR(ENOMEM); s->imag = av_calloc(inlink->channels, sizeof(*s->imag)); if (!s->imag) return AVERROR(ENOMEM); args = av_strdup(s->real_str); if (!args) return AVERROR(ENOMEM); for (ch = 0; ch < inlink->channels; ch++) { char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr); ret = av_expr_parse(&s->real[ch], arg ? arg : last_expr, var_names, NULL, NULL, NULL, NULL, 0, ctx); if (ret < 0) break; if (arg) last_expr = arg; s->nb_exprs++; } av_free(args); args = av_strdup(s->img_str ? s->img_str : s->real_str); if (!args) return AVERROR(ENOMEM); for (ch = 0; ch < inlink->channels; ch++) { char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr); ret = av_expr_parse(&s->imag[ch], arg ? arg : last_expr, var_names, NULL, NULL, NULL, NULL, 0, ctx); if (ret < 0) break; if (arg) last_expr = arg; } av_free(args); s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size); if (!s->fifo) return AVERROR(ENOMEM); s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size, sizeof(*s->window_func_lut)); if (!s->window_func_lut) return AVERROR(ENOMEM); ff_generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap); if (s->overlap == 1) s->overlap = overlap; for (s->win_scale = 0, i = 0; i < s->window_size; i++) { s->win_scale += s->window_func_lut[i] * s->window_func_lut[i]; } s->hop_size = s->window_size * (1 - s->overlap); if (s->hop_size <= 0) return AVERROR(EINVAL); s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2); if (!s->buffer) return AVERROR(ENOMEM); return ret; }
static int config_output(AVFilterLink *outlink) { AVFilterContext *ctx = outlink->src; AVFilterLink *inlink = ctx->inputs[0]; ShowFreqsContext *s = ctx->priv; float overlap; int i; s->nb_freq = 1 << (s->fft_bits - 1); s->win_size = s->nb_freq << 1; av_audio_fifo_free(s->fifo); av_fft_end(s->fft); s->fft = av_fft_init(s->fft_bits, 0); if (!s->fft) { av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. " "The window size might be too high.\n"); return AVERROR(ENOMEM); } /* FFT buffers: x2 for each (display) channel buffer. * Note: we use free and malloc instead of a realloc-like function to * make sure the buffer is aligned in memory for the FFT functions. */ for (i = 0; i < s->nb_channels; i++) { av_freep(&s->fft_data[i]); av_freep(&s->avg_data[i]); } av_freep(&s->fft_data); av_freep(&s->avg_data); s->nb_channels = inlink->channels; s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data)); if (!s->fft_data) return AVERROR(ENOMEM); s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data)); if (!s->fft_data) return AVERROR(ENOMEM); for (i = 0; i < s->nb_channels; i++) { s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data)); s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data)); if (!s->fft_data[i] || !s->avg_data[i]) return AVERROR(ENOMEM); } /* pre-calc windowing function */ s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size, sizeof(*s->window_func_lut)); if (!s->window_func_lut) return AVERROR(ENOMEM); ff_generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap); if (s->overlap == 1.) s->overlap = overlap; s->skip_samples = (1. - s->overlap) * s->win_size; if (s->skip_samples < 1) { av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap); return AVERROR(EINVAL); } for (s->scale = 0, i = 0; i < s->win_size; i++) { s->scale += s->window_func_lut[i] * s->window_func_lut[i]; } outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap)); outlink->sample_aspect_ratio = (AVRational){1,1}; outlink->w = s->w; outlink->h = s->h; s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size); if (!s->fifo) return AVERROR(ENOMEM); return 0; }