From patchwork Sun May 3 19:16:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 19453 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 1D2C844B89D for ; Sun, 3 May 2020 22:44:10 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E540668BF13; Sun, 3 May 2020 22:44:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A3A7368BA1D for ; Sun, 3 May 2020 22:44:03 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id o27so13080335wra.12 for ; Sun, 03 May 2020 12:44:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=S+9GmfLfOGtSkX4zHHYEAL/qtMCNqAVBio64MbtsZHQ=; b=l3rG1OtUq0JZE7JvDHrU9hFAOkfrWMExyHAzBa/DvZdfdxPrUztuFJFP3P8zScbaSi B0kMtuGnA/W3wRZMpfLvDLl2OUyn+0XUkmpX6X4HndAaY8zWdy4oIEEwxgfO3iBE+S26 5h0cOu43yNFLMPa6miSRZBp0zLGVi8qz/feUEnPIy6khKul05kdPyu+MCm7vlaxcoxrL 2Pfd7s1ktL4CmtQ5eeE9W3mr2fikaDZmtfNOuW57hH+hnCu3GIyfPtNu62OTzEJ9coIo I5TcCxH4LrZZEj/y67G8ycQckMyZicLwGvBSdzUYctd9caGUD4rywtVbJ8my0/zllg/4 smrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=S+9GmfLfOGtSkX4zHHYEAL/qtMCNqAVBio64MbtsZHQ=; b=KzVOTwsp43Y6z0788ev0V7yxvVayqqCyqS0Ei3sp8lEdVByEosbWPD4KKZ8C3YtAhB sFWLl8MmY6TGVPfAhYXaUrSp8a7hrdy6p1UT1Cla1Y3jJ4wRoZ5G2tXYtNdWiEK9wWPs okAvtThEH4MqBWEWCpZXLWcBsc/wN8Lv8jLgsikz6j7tDmhUnx6bjrxxcmpMQYzkL/Tr sCaCPBIMmuRPc+asb1UcEbiBE9VdoFKPqo5NHI8JI9iRMWXskC5V6RJlHbRJB73hIgia q7m9hK5PhWhz1TME1GH10i7TiVT5INoBQ//+SLkWeBLpuDSquC3jOw9A1SSb9l/ldjet 7ZQg== X-Gm-Message-State: AGi0Pub7vOd8mubI+eWtYIQJXm1G+2kGnn832G8TbR/2+8CWg75+UMc3 YBd36v/YdMdQUoaZEGdCbGUxtoGf X-Google-Smtp-Source: APiQypKt5est9D9PKLeSoWb5NmOpjSXugBd3DExKfj+99H3qsq+iEgAaBQoLBD4u3nmdO+z3Lo83ag== X-Received: by 2002:adf:e2c2:: with SMTP id d2mr16370877wrj.55.1588533375537; Sun, 03 May 2020 12:16:15 -0700 (PDT) Received: from localhost.localdomain ([94.250.167.28]) by smtp.gmail.com with ESMTPSA id q8sm16144199wrp.58.2020.05.03.12.16.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 May 2020 12:16:14 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 3 May 2020 21:16:01 +0200 Message-Id: <20200503191601.15344-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [RFC][PATCH] avfilter: add speechnorm filter 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavfilter/Makefile | 1 + libavfilter/af_speechnorm.c | 381 ++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 3 files changed, 383 insertions(+) create mode 100644 libavfilter/af_speechnorm.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index f982afe15f..421a01753e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -134,6 +134,7 @@ OBJS-$(CONFIG_SIDECHAINGATE_FILTER) += af_agate.o OBJS-$(CONFIG_SILENCEDETECT_FILTER) += af_silencedetect.o OBJS-$(CONFIG_SILENCEREMOVE_FILTER) += af_silenceremove.o OBJS-$(CONFIG_SOFALIZER_FILTER) += af_sofalizer.o +OBJS-$(CONFIG_SPEECHNORM_FILTER) += af_speechnorm.o OBJS-$(CONFIG_STEREOTOOLS_FILTER) += af_stereotools.o OBJS-$(CONFIG_STEREOWIDEN_FILTER) += af_stereowiden.o OBJS-$(CONFIG_SUPEREQUALIZER_FILTER) += af_superequalizer.o diff --git a/libavfilter/af_speechnorm.c b/libavfilter/af_speechnorm.c new file mode 100644 index 0000000000..52fc8e6e42 --- /dev/null +++ b/libavfilter/af_speechnorm.c @@ -0,0 +1,381 @@ +/* + * Speech Normalizer + * Copyright (c) 2020 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Speech Normalizer + */ + +#include + +#include "libavutil/avassert.h" +#include "libavutil/opt.h" + +#define FF_BUFQUEUE_SIZE (1024) +#include "bufferqueue.h" + +#include "audio.h" +#include "avfilter.h" +#include "filters.h" +#include "internal.h" + +#define MAX_ITEMS 882000 + +typedef struct PeriodItem { + int size; + int type; + double max_peak; +} PeriodItem; + +typedef struct SpeechNormalizerContext { + const AVClass *class; + + double peak_value; + double max_amplification; + double threshold_value; + double feedback; + double decay; + int channels; + + int max_period; + int eof; + int64_t pts; + int state[12]; + + PeriodItem pi[12][MAX_ITEMS]; + double gain_state[12]; + int pi_start[12]; + int pi_end[12]; + + struct FFBufQueue queue; +} SpeechNormalizerContext; + +#define OFFSET(x) offsetof(SpeechNormalizerContext, x) +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +static const AVOption speechnorm_options[] = { + { "peak", "set the peak value", OFFSET(peak_value), AV_OPT_TYPE_DOUBLE, {.dbl = 0.95}, 0.0, 1.0, FLAGS }, + { "p", "set the peak value", OFFSET(peak_value), AV_OPT_TYPE_DOUBLE, {.dbl = 0.95}, 0.0, 1.0, FLAGS }, + { "maxgain", "set the max amplification", OFFSET(max_amplification), AV_OPT_TYPE_DOUBLE, {.dbl = 2.0}, 1.0, 10.0, FLAGS }, + { "m", "set the max amplification", OFFSET(max_amplification), AV_OPT_TYPE_DOUBLE, {.dbl = 2.0}, 1.0, 10.0, FLAGS }, + { "threshold", "set the threshold value", OFFSET(threshold_value), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0.0, 1.0, FLAGS }, + { "t", "set the threshold value", OFFSET(threshold_value), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0.0, 1.0, FLAGS }, + { "feedback", "set the feedback value", OFFSET(feedback), AV_OPT_TYPE_DOUBLE, {.dbl = 0.001}, 0.0, 1.0, FLAGS }, + { "f", "set the feedback value", OFFSET(feedback), AV_OPT_TYPE_DOUBLE, {.dbl = 0.001}, 0.0, 1.0, FLAGS }, + { "decay", "set the decay value", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl = 0.999}, 0.0, 1.0, FLAGS }, + { "d", "set the decay value", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl = 0.999}, 0.0, 1.0, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(speechnorm); + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats; + AVFilterChannelLayouts *layouts; + static const enum AVSampleFormat sample_fmts[] = { + AV_SAMPLE_FMT_DBLP, + AV_SAMPLE_FMT_NONE + }; + int ret; + + layouts = ff_all_channel_counts(); + if (!layouts) + return AVERROR(ENOMEM); + ret = ff_set_common_channel_layouts(ctx, layouts); + if (ret < 0) + return ret; + + formats = ff_make_format_list(sample_fmts); + if (!formats) + return AVERROR(ENOMEM); + ret = ff_set_common_formats(ctx, formats); + if (ret < 0) + return ret; + + formats = ff_all_samplerates(); + if (!formats) + return AVERROR(ENOMEM); + return ff_set_common_samplerates(ctx, formats); +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + SpeechNormalizerContext *s = ctx->priv; + + s->max_period = inlink->sample_rate / (2 * 20); + s->channels = inlink->channels; + for (int ch = 0; ch < s->channels; ch++) + s->state[ch] = -1; + + return 0; +} + +static int get_pi_samples(PeriodItem *pi, int start, int end, int mode) +{ + int sum; + + if (mode && pi[start].type == 0) + return 0; + + sum = pi[start].size; + av_assert0(sum > 0); + while (start != end) { + start++; + if (start >= MAX_ITEMS) + start = 0; + if (mode && pi[start].type == 0) + break; + av_assert0(pi[start].size > 0); + sum += pi[start].size; + if (pi[start].type == 0) + break; + } + + return sum; +} + +static int consume_pi(PeriodItem *pi, int start, int end, int nb_samples) +{ + int sum; + + sum = pi[start].size; + av_assert0(pi[start].size > 0); + while (sum < nb_samples) { + av_assert0(pi[start].type == 1); + av_assert0(start != end); + start++; + if (start >= MAX_ITEMS) + start = 0; + av_assert0(pi[start].size > 0); + sum += pi[start].size; + } + + av_assert0(pi[start].size >= sum - nb_samples); + pi[start].size = sum - nb_samples; + av_assert0(pi[start].size >= 0); + if (pi[start].size == 0 && start != end) { + start++; + if (start >= MAX_ITEMS) + start = 0; + } + + return start; +} + +static int get_queued_samples(SpeechNormalizerContext *s) +{ + int sum = 0; + + for (int i = 0; i < s->queue.available; i++) { + AVFrame *frame = ff_bufqueue_peek(&s->queue, i); + sum += frame->nb_samples; + } + + return sum; +} + +static int filter_frame(AVFilterContext *ctx) +{ + SpeechNormalizerContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + AVFilterLink *inlink = ctx->inputs[0]; + int min_pi_nb_samples; + AVFrame *in = NULL; + int ret; + + for (int f = 0; f < ff_inlink_queued_frames(inlink); f++) { + ret = ff_inlink_consume_frame(inlink, &in); + if (ret < 0) + return ret; + if (ret == 0) + break; + + ff_bufqueue_add(ctx, &s->queue, in); + + for (int ch = 0; ch < inlink->channels; ch++) { + const double *src = (const double *)in->extended_data[ch]; + int n = 0; + + if (s->state[ch] < 0) + s->state[ch] = src[0] >= 0.; + + while (n < in->nb_samples) { + if (s->state[ch] != (src[n] >= 0.) || s->pi[ch][s->pi_end[ch]].size > s->max_period) { + s->state[ch] = src[n] >= 0.; + av_assert0(s->pi[ch][s->pi_end[ch]].size > 0); + s->pi[ch][s->pi_end[ch]].type = 1; + s->pi_end[ch]++; + if (s->pi_end[ch] >= MAX_ITEMS) + s->pi_end[ch] = 0; + s->pi[ch][s->pi_end[ch]].max_peak = DBL_MIN; + s->pi[ch][s->pi_end[ch]].type = 0; + s->pi[ch][s->pi_end[ch]].size = 0; + av_assert0(s->pi_end[ch] != s->pi_start[ch]); + } + + if (src[n] >= 0.) { + while (src[n] >= 0.) { + s->pi[ch][s->pi_end[ch]].max_peak = FFMAX(s->pi[ch][s->pi_end[ch]].max_peak, FFABS(src[n])); + s->pi[ch][s->pi_end[ch]].size++; + n++; + if (n >= in->nb_samples) + break; + } + } else { + while (src[n] < 0.) { + s->pi[ch][s->pi_end[ch]].max_peak = FFMAX(s->pi[ch][s->pi_end[ch]].max_peak, FFABS(src[n])); + s->pi[ch][s->pi_end[ch]].size++; + n++; + if (n >= in->nb_samples) + break; + } + } + } + } + } + + if (s->queue.available > 0) { + in = ff_bufqueue_peek(&s->queue, 0); + if (!in) + return 1; + } else { + return 1; + } + + min_pi_nb_samples = get_pi_samples(s->pi[0], s->pi_start[0], s->pi_end[0], 1); + for (int ch = 1; ch < inlink->channels; ch++) { + min_pi_nb_samples = FFMIN(min_pi_nb_samples, get_pi_samples(s->pi[ch], s->pi_start[ch], s->pi_end[ch], 1)); + } + + if (min_pi_nb_samples >= in->nb_samples) { + int nb_samples = get_queued_samples(s); + + in = ff_bufqueue_get(&s->queue); + + av_frame_make_writable(in); + + nb_samples -= in->nb_samples; + + for (int ch = 0; ch < inlink->channels; ch++) { + double *src = (double *)in->extended_data[ch]; + int start = s->pi_start[ch]; + int offset = 0; + double gain; + + for (int n = 0; n < in->nb_samples; n++) { + if (n >= offset) { + if (s->pi[ch][start].max_peak > s->threshold_value) + gain = FFMIN(s->max_amplification, s->peak_value / s->pi[ch][start].max_peak); + else + gain = 1.; + av_assert0(s->pi[ch][start].size > 0); + offset += s->pi[ch][start++].size; + if (start >= MAX_ITEMS) + start = 0; + } + s->gain_state[ch] = FFMIN(gain, gain * s->feedback + s->gain_state[ch] * s->decay); + src[n] *= s->gain_state[ch]; + } + } + + for (int ch = 0; ch < inlink->channels; ch++) { + s->pi_start[ch] = consume_pi(s->pi[ch], s->pi_start[ch], s->pi_end[ch], in->nb_samples); + } + + for (int ch = 0; ch < inlink->channels; ch++) { + int pi_nb_samples = get_pi_samples(s->pi[ch], s->pi_start[ch], s->pi_end[ch], 0); + + if (nb_samples != pi_nb_samples) { + av_assert0(0); + } + } + + return ff_filter_frame(outlink, in); + } + + return 1; +} + +static int activate(AVFilterContext *ctx) +{ + AVFilterLink *inlink = ctx->inputs[0]; + AVFilterLink *outlink = ctx->outputs[0]; + SpeechNormalizerContext *s = ctx->priv; + int ret = 0, status; + int64_t pts; + + FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); + + ret = filter_frame(ctx); + if (ret <= 0) + return ret; + + if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) { + if (status == AVERROR_EOF) + s->eof = 1; + } + + if (s->eof && ff_inlink_queued_samples(inlink) == 0) { + ff_outlink_set_status(outlink, AVERROR_EOF, s->pts); + return 0; + } + + if (!s->eof) + FF_FILTER_FORWARD_WANTED(outlink, inlink); + + return FFERROR_NOT_READY; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ +} + +static const AVFilterPad avfilter_af_speechnorm_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad avfilter_af_speechnorm_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + }, + { NULL } +}; + +AVFilter ff_af_speechnorm = { + .name = "speechnorm", + .description = NULL_IF_CONFIG_SMALL("Speech Normalizer."), + .query_formats = query_formats, + .priv_size = sizeof(SpeechNormalizerContext), + .priv_class = &speechnorm_class, + .activate = activate, + .uninit = uninit, + .inputs = avfilter_af_speechnorm_inputs, + .outputs = avfilter_af_speechnorm_outputs, + .process_command = ff_filter_process_command, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 1b94501da0..fbe9633a99 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -128,6 +128,7 @@ extern AVFilter ff_af_sidechaingate; extern AVFilter ff_af_silencedetect; extern AVFilter ff_af_silenceremove; extern AVFilter ff_af_sofalizer; +extern AVFilter ff_af_speechnorm; extern AVFilter ff_af_stereotools; extern AVFilter ff_af_stereowiden; extern AVFilter ff_af_superequalizer;