From patchwork Mon Oct 17 18:16:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muhammad Faiz X-Patchwork-Id: 1036 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.133 with SMTP id o127csp405636vsd; Mon, 17 Oct 2016 11:18:30 -0700 (PDT) X-Received: by 10.194.235.165 with SMTP id un5mr12240387wjc.200.1476728310113; Mon, 17 Oct 2016 11:18:30 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id kq1si43010283wjb.150.2016.10.17.11.18.29; Mon, 17 Oct 2016 11:18:30 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BEABD6891EE; Mon, 17 Oct 2016 21:18:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pa0-f65.google.com (mail-pa0-f65.google.com [209.85.220.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 202B4687EED for ; Mon, 17 Oct 2016 21:18:19 +0300 (EEST) Received: by mail-pa0-f65.google.com with SMTP id hh10so9484904pac.0 for ; Mon, 17 Oct 2016 11:18:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=xDvfLQ9pRPk8NsTh4bL70AGQBzE4sywoMFCbfBNs5n8=; b=sBjNdG+ErnEc2WRvhMVJeEqDmZGPoIAnJeIkBDKmbOdSFV3mBDY6xN2a1+ay6Aferx IDOiLp++5BXEmPmUO1B23NrQ5qsHZGN+rJCb23C4rhQs7kaCa2YE0xC+LVoj2AhLQ6WV UMZlmOk9od927sP2AJNsI5fOblf3tQXVwxgxTrq00oldujusXBzZDP6IrusSTrS3RZTk x3Fe7EMME8Jy+O4EF/wb8vbjdlz5/mpDRowK7yV0xqjhMApCHFbsTjvoc6gqcPn7K8xn f0aiTovvRmPyxm+ull6Kp+KjKe8kVJOcMHkdsZtPv46Ai6Deoh9fmd27+p/+D95DNhr7 PAZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=xDvfLQ9pRPk8NsTh4bL70AGQBzE4sywoMFCbfBNs5n8=; b=dZlTBJBGay2Y1KGJp3Cgnj3kC3sH8BkgLL8qKg2xCWiU+62Ph20Wx8VbdZRk0I9ZVZ yVBkVy0Qh2NGG35M95Utqy8p3dyFk+qvc/EBZZFNsrd+2IoHA4TBygzCXuHRLxo4XQ+m /D6EbVeT+rfNPXdygdzBH8yXbvIxdaJl1mhRm8eH/OBrrEJ/A0eVYG8Yo8zcEQZI1TlT UHtYW6y6pbCVZhCF1nxYvZgKl0wB2nBmIBKGxgls2Jbi7KM603SYSGguVJdJr3dqVo1B lwoc3bU6LwzUVpNzexUURSoF4WVc8MHitz/ygiqQsUhHnbCcRsy5slrYf+IgIzzYS35K xRxA== X-Gm-Message-State: AA6/9RlcItIg2dFKZZ+kEYcMLSCwLp2bvfgp8bmcFowU8EmBDQ5FfOaN98ctoLo9j8EQDw== X-Received: by 10.66.52.112 with SMTP id s16mr223046pao.197.1476728299717; Mon, 17 Oct 2016 11:18:19 -0700 (PDT) Received: from localhost.localdomain.localdomain ([114.120.238.94]) by smtp.gmail.com with ESMTPSA id o9sm43437507pac.27.2016.10.17.11.18.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Oct 2016 11:18:19 -0700 (PDT) From: Muhammad Faiz To: ffmpeg-devel@ffmpeg.org Date: Tue, 18 Oct 2016 01:16:04 +0700 Message-Id: <1476728164-886-1-git-send-email-mfcc64@gmail.com> X-Mailer: git-send-email 2.5.0 Subject: [FFmpeg-devel] [PATCH] avfilter/firequalizer: add dumpfile and dumpscale option X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Muhammad Faiz MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Muhammad Faiz --- doc/filters.texi | 7 ++++ libavfilter/af_firequalizer.c | 97 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 90 insertions(+), 14 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index 8fda8a1..9fc8c4f 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -2576,6 +2576,13 @@ logarithmic (in octave scale where 20 Hz is 0) frequency, linear gain @item loglog logarithmic frequency, logarithmic gain @end table + +@item dumpfile +Set file for dumping, suitable for gnuplot. + +@item dumpscale +Set scale for dumpfile. Acceptable values are same with scale option. +Default is linlog. @end table @subsection Examples diff --git a/libavfilter/af_firequalizer.c b/libavfilter/af_firequalizer.c index aed3e28..6868066 100644 --- a/libavfilter/af_firequalizer.c +++ b/libavfilter/af_firequalizer.c @@ -65,6 +65,7 @@ typedef struct { typedef struct { const AVClass *class; + RDFTContext *analysis_rdft; RDFTContext *analysis_irdft; RDFTContext *rdft; RDFTContext *irdft; @@ -72,6 +73,7 @@ typedef struct { int rdft_len; float *analysis_buf; + float *dump_buf; float *kernel_tmp_buf; float *kernel_buf; float *conv_buf; @@ -93,6 +95,8 @@ typedef struct { int multi; int zero_phase; int scale; + char *dumpfile; + int dumpscale; int nb_gain_entry; int gain_entry_err; @@ -126,6 +130,8 @@ static const AVOption firequalizer_options[] = { { "linlog", "linear-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLOG }, 0, 0, FLAGS, "scale" }, { "loglin", "logarithmic-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLIN }, 0, 0, FLAGS, "scale" }, { "loglog", "logarithmic-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLOG }, 0, 0, FLAGS, "scale" }, + { "dumpfile", "set dump file", OFFSET(dumpfile), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, + { "dumpscale", "set dump scale", OFFSET(dumpscale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, "scale" }, { NULL } }; @@ -133,12 +139,14 @@ AVFILTER_DEFINE_CLASS(firequalizer); static void common_uninit(FIREqualizerContext *s) { + av_rdft_end(s->analysis_rdft); av_rdft_end(s->analysis_irdft); av_rdft_end(s->rdft); av_rdft_end(s->irdft); - s->analysis_irdft = s->rdft = s->irdft = NULL; + s->analysis_rdft = s->analysis_irdft = s->rdft = s->irdft = NULL; av_freep(&s->analysis_buf); + av_freep(&s->dump_buf); av_freep(&s->kernel_tmp_buf); av_freep(&s->kernel_buf); av_freep(&s->conv_buf); @@ -223,6 +231,53 @@ static void fast_convolute(FIREqualizerContext *s, const float *kernel_buf, floa } } +static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch) +{ + FIREqualizerContext *s = ctx->priv; + int rate = ctx->inputs[0]->sample_rate; + int xlog = s->dumpscale == SCALE_LOGLIN || s->dumpscale == SCALE_LOGLOG; + int ylog = s->dumpscale == SCALE_LINLOG || s->dumpscale == SCALE_LOGLOG; + int x; + int center = s->fir_len / 2; + double delay = s->zero_phase ? 0.0 : (double) center / rate; + double vx, ya, yb; + + s->analysis_buf[0] *= s->rdft_len/2; + for (x = 1; x <= center; x++) { + s->analysis_buf[x] *= s->rdft_len/2; + s->analysis_buf[s->analysis_rdft_len - x] *= s->rdft_len/2; + } + + if (ch) + fprintf(fp, "\n\n"); + + fprintf(fp, "# time[%d] (time amplitude)\n", ch); + + for (x = center; x > 0; x--) + fprintf(fp, "%15.10f %15.10f\n", delay - (double) x / rate, (double) s->analysis_buf[s->analysis_rdft_len - x]); + + for (x = 0; x <= center; x++) + fprintf(fp, "%15.10f %15.10f\n", delay + (double)x / rate , (double) s->analysis_buf[x]); + + av_rdft_calc(s->analysis_rdft, s->analysis_buf); + + fprintf(fp, "\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch); + + for (x = 0; x <= s->analysis_rdft_len/2; x++) { + int i = (x == s->analysis_rdft_len/2) ? 1 : 2 * x; + vx = (double)x * rate / s->analysis_rdft_len; + if (xlog) + vx = log2(0.05*vx); + ya = s->dump_buf[i]; + yb = s->analysis_buf[i]; + if (ylog) { + ya = 20.0 * log10(fabs(ya)); + yb = 20.0 * log10(fabs(yb)); + } + fprintf(fp, "%17.10f %17.10f %17.10f\n", vx, ya, yb); + } +} + static double entry_func(void *p, double freq, double gain) { AVFilterContext *ctx = p; @@ -332,6 +387,7 @@ static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *g int ret, k, center, ch; int xlog = s->scale == SCALE_LOGLIN || s->scale == SCALE_LOGLOG; int ylog = s->scale == SCALE_LINLOG || s->scale == SCALE_LOGLOG; + FILE *dump_fp = NULL; s->nb_gain_entry = 0; s->gain_entry_err = 0; @@ -352,10 +408,14 @@ static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *g if (ret < 0) return ret; + if (s->dumpfile && (!s->dump_buf || !s->analysis_rdft || !(dump_fp = fopen(s->dumpfile, "w")))) + av_log(ctx, AV_LOG_WARNING, "dumping failed.\n"); + vars[VAR_CHS] = inlink->channels; vars[VAR_CHLAYOUT] = inlink->channel_layout; vars[VAR_SR] = inlink->sample_rate; for (ch = 0; ch < inlink->channels; ch++) { + float *rdft_buf = s->kernel_tmp_buf + ch * s->rdft_len; double result; vars[VAR_CH] = ch; vars[VAR_CHID] = av_channel_layout_extract_channel(inlink->channel_layout, ch); @@ -380,6 +440,9 @@ static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *g s->analysis_buf[2*k+1] = 0.0; } + if (s->dump_buf) + memcpy(s->dump_buf, s->analysis_buf, s->analysis_rdft_len * sizeof(*s->analysis_buf)); + av_rdft_calc(s->analysis_irdft, s->analysis_buf); center = s->fir_len / 2; @@ -421,35 +484,36 @@ static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *g av_assert0(0); } s->analysis_buf[k] *= (2.0/s->analysis_rdft_len) * (2.0/s->rdft_len) * win; + if (k) + s->analysis_buf[s->analysis_rdft_len - k] = s->analysis_buf[k]; } - for (k = 0; k < center - k; k++) { - float tmp = s->analysis_buf[k]; - s->analysis_buf[k] = s->analysis_buf[center - k]; - s->analysis_buf[center - k] = tmp; - } - - for (k = 1; k <= center; k++) - s->analysis_buf[center + k] = s->analysis_buf[center - k]; - - memset(s->analysis_buf + s->fir_len, 0, (s->rdft_len - s->fir_len) * sizeof(*s->analysis_buf)); - av_rdft_calc(s->rdft, s->analysis_buf); + memset(s->analysis_buf + center + 1, 0, (s->analysis_rdft_len - s->fir_len) * sizeof(*s->analysis_buf)); + memcpy(rdft_buf, s->analysis_buf + s->analysis_rdft_len - center, center * sizeof(*s->analysis_buf)); + memcpy(rdft_buf + center, s->analysis_buf, (s->rdft_len - center) * sizeof(*s->analysis_buf)); + av_rdft_calc(s->rdft, rdft_buf); for (k = 0; k < s->rdft_len; k++) { - if (isnan(s->analysis_buf[k]) || isinf(s->analysis_buf[k])) { + if (isnan(rdft_buf[k]) || isinf(rdft_buf[k])) { av_log(ctx, AV_LOG_ERROR, "filter kernel contains nan or infinity.\n"); av_expr_free(gain_expr); + if (dump_fp) + fclose(dump_fp); return AVERROR(EINVAL); } } - memcpy(s->kernel_tmp_buf + ch * s->rdft_len, s->analysis_buf, s->rdft_len * sizeof(*s->analysis_buf)); + if (dump_fp) + dump_fir(ctx, dump_fp, ch); + if (!s->multi) break; } memcpy(s->kernel_buf, s->kernel_tmp_buf, (s->multi ? inlink->channels : 1) * s->rdft_len * sizeof(*s->kernel_buf)); av_expr_free(gain_expr); + if (dump_fp) + fclose(dump_fp); return 0; } @@ -499,6 +563,11 @@ static int config_input(AVFilterLink *inlink) if (!(s->analysis_irdft = av_rdft_init(rdft_bits, IDFT_C2R))) return AVERROR(ENOMEM); + if (s->dumpfile) { + s->analysis_rdft = av_rdft_init(rdft_bits, DFT_R2C); + s->dump_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->dump_buf)); + } + s->analysis_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->analysis_buf)); s->kernel_tmp_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_tmp_buf)); s->kernel_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_buf));