From patchwork Tue Apr 28 10:05:24 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: 19311 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 A68964488AF for ; Tue, 28 Apr 2020 13:05:39 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 804F368BE41; Tue, 28 Apr 2020 13:05:39 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1A9C368BDFB for ; Tue, 28 Apr 2020 13:05:34 +0300 (EEST) Received: by mail-wm1-f46.google.com with SMTP id h4so2055926wmb.4 for ; Tue, 28 Apr 2020 03:05:34 -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=UFaJEER+7mVd5RFgOwMKEsK2I7jIwPDQi6AVrdVmxtw=; b=vhiA89UZGSozQIijwCK/nLS4snF41KVeulmrHa3xgccxudT2TX1A/hgdWotdSd3pYp bDAGCSYKbu6vUHnj3NR5pywh3MGSq582P8h1HShjwN61OozFf/RRex5nG3zVIjLwpTfW AIDtLqSinP2fjowM0ueMhICKQM2Hm4YZqIXYNdDBE7JHNAYLRzz0dZaxFehr8NjIjFGa IrcGR53Y+bOOyImjcuR7peYXzP+v/3ndu3hnVQ+84uYy+x3Rc/ovonmCOEKsrjgC+tta 1Cko8tdShyXjA/lYkerq6AkCrRcjtP2BfFFEpqO3ACfl1bBU9WkYtd6MGCzoBFXJmKiQ 6Tww== 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=UFaJEER+7mVd5RFgOwMKEsK2I7jIwPDQi6AVrdVmxtw=; b=f8O+NXm7BRTuV1AIGfYbWDylaQpHJbpuM6ckYPDbtGatA95mWw8f/w0Q9sZaN4/zuW sQ8jzlKQ7jaJOTd2INkh9r4i17/gbqIovlc8YBqQ5VB//V1cSZuoSgzpJRWjZruf1C6M T0VQ/quDenKWTQ38GlQkV2VBDJcHINxpT8OnXm32B8CP5ohQWy+rnzdgRv3DLm0SQuAB hzdfF/Y5Gm0/tClwzCcpf/KjAuJZCa5ZzEjkOeBQtZGcYE4KK1qYXTx+hfxt+1ablTJb sk+uKEspck5QmCD32iefouTRoj80o/ZuJdDjqYxwBh17/AoBg2rhigRSTFKcM6mwglOM /Fkg== X-Gm-Message-State: AGi0Pua0m8//u+itdZOfaQUEBbp6TpalMpj7im2RaKJJv6TIQ2x8qNM3 QqZUdIWe3uXUdYjHMzxTpNhKiNc5 X-Google-Smtp-Source: APiQypKsKip8mX1tGlriU/VfWmzwtzTbCFrBsSKR3/W9PlevVDdou6g5zNd0+DUm/dZ0VzLFJQT8UA== X-Received: by 2002:a7b:c1c4:: with SMTP id a4mr3808286wmj.86.1588068332733; Tue, 28 Apr 2020 03:05:32 -0700 (PDT) Received: from localhost.localdomain ([77.237.109.227]) by smtp.gmail.com with ESMTPSA id f23sm2601537wml.4.2020.04.28.03.05.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2020 03:05:32 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 12:05:24 +0200 Message-Id: <20200428100524.10122-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter/af_amix: make weights option runtime configured 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 --- doc/filters.texi | 8 +++++ libavfilter/af_amix.c | 70 ++++++++++++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index 55edd564bf..fe661c0b4b 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -1703,6 +1703,14 @@ Specify weight of each input audio stream as sequence. Each weight is separated by space. By default all inputs have same weight. @end table +@subsection Commands + +This filter supports the following commands: +@table @option +@item weights +Syntax is same as option with same name. +@end table + @section amultiply Multiply first audio stream with second audio stream and store result diff --git a/libavfilter/af_amix.c b/libavfilter/af_amix.c index c09f8b034b..32aa80e112 100644 --- a/libavfilter/af_amix.c +++ b/libavfilter/af_amix.c @@ -181,6 +181,7 @@ typedef struct MixContext { #define OFFSET(x) offsetof(MixContext, x) #define A AV_OPT_FLAG_AUDIO_PARAM #define F AV_OPT_FLAG_FILTERING_PARAM +#define T AV_OPT_FLAG_RUNTIME_PARAM static const AVOption amix_options[] = { { "inputs", "Number of inputs.", OFFSET(nb_inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT16_MAX, A|F }, @@ -193,7 +194,7 @@ static const AVOption amix_options[] = { "renormalization when an input stream ends.", OFFSET(dropout_transition), AV_OPT_TYPE_FLOAT, { .dbl = 2.0 }, 0, INT_MAX, A|F }, { "weights", "Set weight for each input.", - OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, A|F }, + OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, A|F|T }, { NULL } }; @@ -504,12 +505,38 @@ static int activate(AVFilterContext *ctx) return 0; } -static av_cold int init(AVFilterContext *ctx) +static void parse_weights(AVFilterContext *ctx) { MixContext *s = ctx->priv; float last_weight = 1.f; - int i, ret; char *p; + int i; + + s->weight_sum = 0.f; + p = s->weights_str; + for (i = 0; i < s->nb_inputs; i++) { + last_weight = av_strtod(p, &p); + s->weights[i] = last_weight; + printf("%g\n", last_weight); + s->weight_sum += FFABS(last_weight); + if (p && *p) { + p++; + } else { + i++; + break; + } + } + + for (; i < s->nb_inputs; i++) { + s->weights[i] = last_weight; + s->weight_sum += FFABS(last_weight); + } +} + +static av_cold int init(AVFilterContext *ctx) +{ + MixContext *s = ctx->priv; + int i, ret; for (i = 0; i < s->nb_inputs; i++) { AVFilterPad pad = { 0 }; @@ -533,23 +560,7 @@ static av_cold int init(AVFilterContext *ctx) if (!s->weights) return AVERROR(ENOMEM); - p = s->weights_str; - for (i = 0; i < s->nb_inputs; i++) { - last_weight = av_strtod(p, &p); - s->weights[i] = last_weight; - s->weight_sum += FFABS(last_weight); - if (p && *p) { - p++; - } else { - i++; - break; - } - } - - for (; i < s->nb_inputs; i++) { - s->weights[i] = last_weight; - s->weight_sum += FFABS(last_weight); - } + parse_weights(ctx); return 0; } @@ -604,6 +615,24 @@ fail: return ret; } +static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, + char *res, int res_len, int flags) +{ + MixContext *s = ctx->priv; + int ret; + + ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); + if (ret < 0) + return ret; + + parse_weights(ctx); + for (int i = 0; i < s->nb_inputs; i++) + s->scale_norm[i] = s->weight_sum / FFABS(s->weights[i]); + calculate_scales(s, 0); + + return 0; +} + static const AVFilterPad avfilter_af_amix_outputs[] = { { .name = "default", @@ -624,5 +653,6 @@ AVFilter ff_af_amix = { .query_formats = query_formats, .inputs = NULL, .outputs = avfilter_af_amix_outputs, + .process_command = process_command, .flags = AVFILTER_FLAG_DYNAMIC_INPUTS, };