From patchwork Thu Mar 29 12:05:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 8222 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp1603045jad; Thu, 29 Mar 2018 05:06:06 -0700 (PDT) X-Google-Smtp-Source: AIpwx49IjTfQFXukqrCFTvgIPRA5dFzMtHlLVILfZK+nB09srwfemTxFL+ifVyFu/CwZ+6NJX7cg X-Received: by 10.28.105.193 with SMTP id z62mr6083816wmh.143.1522325166102; Thu, 29 Mar 2018 05:06:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522325166; cv=none; d=google.com; s=arc-20160816; b=ox783f6wxD7BzZlWw/ab/Td54S7DFlyjVDSdSO1y2ZmKjelhSxvupAq4sriA/bdnmU 7qQMeOMlunmLBAyY07o7xr4N+0w3jNWBv/bCDHC3Mjxg2DEZl7Tm2RCCfYL7/g0GUpCP 7Quj7hdjCOOMYHceYlgJRsAb+VHH97yZM4196odFyEaLFKNwu3OCL5OrUoXAe6eJeNu3 FG4zck3dB3PnuRMKidCDDq2+dRv20e06ARL6bWiXh78LJ0el+UhT9euFj3sj9pjVYh2W usPUpeC/8RM2MaLuRj0Rs49XD3pfKRf+aR1iQaq0JAYhE6ss7rKgpVjTAhK2svEFbSK4 bWlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=9Ptrl67sXp0ptio27nZ27807fQnHXeww3yQoWM5hVbc=; b=E9JEq9FcAcNmOvtSqB9m+8p6O/1KQTz7ziw32USTYjAWa89Vz3jAecLLIXvltJNp/1 1SWvBO6kxYfzG6IUTe3yviIgVdA2b61dR/aSXnW72IFoJ7ZRx5EX7FgYQ11Wel6N5U+n BaLNPBbtYhmSIuDEs/HU/RFHgcPdsR5ZWiH9cSKmsSOXimWxqYDWrZmJcuYObmFJu+Xk YBN6Pu5FWRjCBPdxzjHbOQfI9z/6eCAE5ulSG7E9I++g9IpxmVit8YgA4bAoj9h4skdM HUlMKZHfCOfDO0S1g5NHf+zBXBh04reSdgaWtLK1vzMVsXwixjCgUfPRMqf28fxAxhQX Xlog== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=YjrezEJm; 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 sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a69si1268514wme.3.2018.03.29.05.06.05; Thu, 29 Mar 2018 05:06:06 -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 header.s=20161025 header.b=YjrezEJm; 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 sp=QUARANTINE 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 AD73C689CA0; Thu, 29 Mar 2018 15:05:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 07DCC68971A for ; Thu, 29 Mar 2018 15:05:39 +0300 (EEST) Received: by mail-wm0-f66.google.com with SMTP id v21so10423166wmc.1 for ; Thu, 29 Mar 2018 05:05:57 -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=kwxLh1v5hvmqY/Xpma0sgfjW7fBAlU9/KuwKmmp/Rjw=; b=YjrezEJmKNJWNfn437dLYiJnWbjLq0jG3x5n3iG/iqaqoJiruC1M6B1UT07ToQnzYy i9nh99mFLxZpeAAOX+INNdCQyIUEZVhO1MvpNPMSJY8OPqKwBFPnr0vV/9IorNvcgzdx ERuXX9tvA84YsWOrbDFbB3/2hYDoN+Cc7oTFrdNyt9SdERJr1ux3JUOFz+kBdghcrVC6 3PKi5Y6fp1RWNXRuOSoORLGdStP8wVWxmDSBnyOH554f5EFeYiOAaJizzhbxe/Jc0Zc2 QTqVyDLCCFtqZVPWxHw4VA+lWUFD9sFDc1t+4HGo4uohsNWEMr4cdvr2VTfVFFwAU7TR 7WIQ== 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=kwxLh1v5hvmqY/Xpma0sgfjW7fBAlU9/KuwKmmp/Rjw=; b=ChLl8t7t9MMFuqJ994pJqAXvxnDJpsvdBJsvHCef+FPdGLgojORUFrfCrvtMCH0/0/ PXDKx2MZz6fPD4021AEh8j8AOQtePl3v7mXPtVxUyE1NXTICtJX61AO6AO1BeDezZKF7 x66MNudIfiQ1Ul8ue3BZTPFlCGpG8eGsG9DdMthZppEvoi6DioxtPFfhbYG1JZcZ0OB0 Fbkz734qHtyYEl4XSzMsooeNMFhOSE51KJqq8aUOxFBpORAYk3UuioEDpkUT3brIQJen Q52Rx7pgxaMocbqwFjfhJpTcNJ3Tl5mwOTQDkt1gQpAfNTqhAHZNuJRHYrqxo8MCPDtM VD7Q== X-Gm-Message-State: AElRT7FFkvmfQ3deSGrZDdCcHoSh6By6alGrgMnE+3XYpLzFuLTNpXo6 PCAPgEJSpMJwnjgOFNJT6PW6BQ== X-Received: by 10.28.48.149 with SMTP id w143mr6203630wmw.88.1522325156825; Thu, 29 Mar 2018 05:05:56 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id m62sm2189922wmi.19.2018.03.29.05.05.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Mar 2018 05:05:56 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Thu, 29 Mar 2018 14:05:12 +0200 Message-Id: <20180329120512.13242-1-onemda@gmail.com> X-Mailer: git-send-email 2.11.0 Subject: [FFmpeg-devel] [PATCH] avfilter/af_amix: add weights 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 3 +++ libavfilter/af_amix.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/doc/filters.texi b/doc/filters.texi index 5c119c0151..6cbb7255f4 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -1350,6 +1350,9 @@ The duration of the first input. The transition time, in seconds, for volume renormalization when an input stream ends. The default value is 2 seconds. +@item weights +Specify weight of each input audio stream as sequence. +Each weight is separated by space. By default all inputs have same weight. @end table @section anequalizer diff --git a/libavfilter/af_amix.c b/libavfilter/af_amix.c index ec764375c0..9fa504765f 100644 --- a/libavfilter/af_amix.c +++ b/libavfilter/af_amix.c @@ -162,6 +162,7 @@ typedef struct MixContext { int active_inputs; /**< number of input currently active */ int duration_mode; /**< mode for determining duration */ float dropout_transition; /**< transition time when an input drops out */ + char *weights_str; /**< string for custom weights for every input */ int nb_channels; /**< number of channels */ int sample_rate; /**< sample rate */ @@ -169,6 +170,7 @@ typedef struct MixContext { AVAudioFifo **fifos; /**< audio fifo for each input */ uint8_t *input_state; /**< current state of each input */ float *input_scale; /**< mixing scale factor for each input */ + float *weights; /**< custom weights for every input */ float scale_norm; /**< normalization factor for all inputs */ int64_t next_pts; /**< calculated pts for next output frame */ FrameList *frame_list; /**< list of frame info for the first input */ @@ -188,6 +190,8 @@ static const AVOption amix_options[] = { { "dropout_transition", "Transition time, in seconds, for volume " "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 }, { NULL } }; @@ -202,6 +206,7 @@ AVFILTER_DEFINE_CLASS(amix); */ static void calculate_scales(MixContext *s, int nb_samples) { + float weight_sum = 0.f; int i; if (s->scale_norm > s->active_inputs) { @@ -209,9 +214,13 @@ static void calculate_scales(MixContext *s, int nb_samples) s->scale_norm = FFMAX(s->scale_norm, s->active_inputs); } + for (i = 0; i < s->nb_inputs; i++) + if (s->input_state[i] & INPUT_ON) + weight_sum += s->weights[i]; + for (i = 0; i < s->nb_inputs; i++) { if (s->input_state[i] & INPUT_ON) - s->input_scale[i] = 1.0f / s->scale_norm; + s->input_scale[i] = s->weights[i] / weight_sum * s->active_inputs / s->scale_norm; else s->input_scale[i] = 0.0f; } @@ -487,6 +496,8 @@ static int activate(AVFilterContext *ctx) static av_cold int init(AVFilterContext *ctx) { MixContext *s = ctx->priv; + char *p, *arg, *saveptr = NULL; + float last_weight = 1.f; int i, ret; for (i = 0; i < s->nb_inputs; i++) { @@ -507,6 +518,24 @@ static av_cold int init(AVFilterContext *ctx) if (!s->fdsp) return AVERROR(ENOMEM); + s->weights = av_mallocz_array(s->nb_inputs, sizeof(*s->weights)); + if (!s->weights) + return AVERROR(ENOMEM); + + p = s->weights_str; + for (i = 0; i < s->nb_inputs; i++) { + if (!(arg = av_strtok(p, " ", &saveptr))) + break; + + p = NULL; + sscanf(arg, "%f", &last_weight); + s->weights[i] = last_weight; + } + + for (; i < s->nb_inputs; i++) { + s->weights[i] = last_weight; + } + return 0; } @@ -524,6 +553,7 @@ static av_cold void uninit(AVFilterContext *ctx) av_freep(&s->frame_list); av_freep(&s->input_state); av_freep(&s->input_scale); + av_freep(&s->weights); av_freep(&s->fdsp); for (i = 0; i < ctx->nb_inputs; i++)