From patchwork Thu Jan 20 11:36:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lacko X-Patchwork-Id: 33722 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5742847iog; Thu, 20 Jan 2022 03:38:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJykDho630GhFYJI4IuiEeI0MjXDU/QXL1gyYXGKdDlmAYepoa8Wozt22QuqfFyFZhKvNavh X-Received: by 2002:a17:906:6d95:: with SMTP id h21mr27475596ejt.190.1642678696510; Thu, 20 Jan 2022 03:38:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642678696; cv=none; d=google.com; s=arc-20160816; b=dtxg6gZObS+XZpv7SobTkFIMxcdjQIExZpioS/rFGczLqRESlm0WD5oQPSYxdXMTmw srHsDnqG6MvnvxKw+hRfUaSJi88vndkONeJtVvkvj6q7wnBaacInHJAMxH2CAnPb+Z4V OInbrBKlYQZLjMEDsQQycJ0xoZd/LebXOq38yheaKhwRAv5BlSRik+gcMXeV5B6uDvqW DbDOneIBjbiXiNAZJlQUNG+eDxnjvrDTEfCXKTgHufhm37fmdwcWnuOvaHSXYteea//m M+93VFzoAJbOQ1qeTBEKM5HkK6jfnFuBP+X6Q+a7aa4SIm3AaB4HYkY11xofe6CqgSoT Eb6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=uFM9au+nfaLiZWv2a27Nv+TqSdUo9VPT5akR9SfTCJ4=; b=TxOOfsUXzSSyPYGXZQUjnniaU1mNpEFIB2JDOaJrAtqMrx4Kdh9ySzhN6Qcqi+8QjV 2paShJnxa0ilffEg28as3M6fPT622seE7W+FOiezYeSU/na9SlKE2CwIPdNUu0NtU0st u+Kabt3JUaxojzL4nfnYbn9EN7vWk4nVTAKcLRy5RBzx0i/UEtHeV9lFiUltfhgpRQ5A cN+z4LemTcqgsVfOPJa/Z4QzULwl1y1TzdJOC2i6YJfYo0T9FqGchw1iZA+qQmS8aLkj nvUIzNuXAe9Lo2PquwbsThPQsF/mKeVv8InDeva6HZVXJs7UFis3y6NAl1wSvj7NjIXT aybw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=T6z3BAYC; 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 y5si2313159eda.154.2022.01.20.03.38.15; Thu, 20 Jan 2022 03:38:16 -0800 (PST) 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=20210112 header.b=T6z3BAYC; 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 30BCE68B2BB; Thu, 20 Jan 2022 13:38:11 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 15ED768B23B for ; Thu, 20 Jan 2022 13:38:05 +0200 (EET) Received: by mail-ed1-f54.google.com with SMTP id m11so27284310edi.13 for ; Thu, 20 Jan 2022 03:38:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=FbI818Z8jlp9XE+1YlMo38CfeJORr3oHId0gSJjyhSk=; b=T6z3BAYCG1pCtK9+jQaED0DZIZjriG+fZfsDUXXSRt+q9I6kw5HpYtgGBtu8gnuHG5 syxJJ6gkT5cJh8bHcZfrk0Q2VgjBu/aFPKJc61e1Gm1HVBf30/ieOge32vkoGg1IvYFt sxca9XgyOI+9F1Qd/+mIh2zDJSzNbx377MErG1vfBuLY00ggEzOWAYCVoY5+cUWH6bsY l9PAAlZH91UgqDUxFNnUNhyNUUAoUrdHLXLN104r16XL4DRZsLDD5WuFqW3SRitDD2R5 XssxAdxaebW0jrt9kv35So2TbV5A62yoTsA9nEOKjuz7Gnx9qlM4rBFQTbKs/pT+LTfs jYXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=FbI818Z8jlp9XE+1YlMo38CfeJORr3oHId0gSJjyhSk=; b=2UWHKrdiU95Tq+2w7VnzMgzCiDptOYeKyintWkiF66YP9A2eTlhcPsqAIECKoR7+t6 pGN8Tsjs6QgOKaqFFUKVMeSkDazGkH//FBlUFnIb2ofiEHa0mP1S5FoTUG2mAvO3BJQN TQ4sbnFFeySmcVJI+ghhjC2HylhXDqt81KrRMBd3V+ccg7NAGJjVHjB+DMsDlhZztCD7 cijVF+yKtyTr3bDaE6zgyfG+GUfwTrOQEzaS3DMSSczhuP6PlC7or+mr5AxbzmJbes2W AoNctRK4E0Y4xJJInpaiFY+KIWn2FhTCDNJGL3X1JC88NZUIaf4Ten/2c59TpMgnzp1i L8jA== X-Gm-Message-State: AOAM532Ynq3cSnrY7Ck+3uZhe3VfIlxhczqVZbBgV8x/mMRth77cxg96 59PrHF3YG05fUm9QahfJKO2/7nERprc= X-Received: by 2002:a17:907:a411:: with SMTP id sg17mr29761106ejc.386.1642678683103; Thu, 20 Jan 2022 03:38:03 -0800 (PST) Received: from DEIWO-PC.brno. (ip-94-113-225-198.net.upcbroadband.cz. [94.113.225.198]) by smtp.gmail.com with ESMTPSA id i6sm906233eja.132.2022.01.20.03.38.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 03:38:02 -0800 (PST) From: David Lacko To: ffmpeg-devel@ffmpeg.org Date: Thu, 20 Jan 2022 12:36:17 +0100 Message-Id: <20220120113617.27481-1-deiwo101@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avfilter/adelay: Add command support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: David Lacko Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ltXctKSPCczr Adds command 'delays' to the adelay filter. This command accepts same values as the option with one difference, to apply delay to all channels prefix 'all:' to the argument is accepted. Signed-off-by: David Lacko --- libavfilter/af_adelay.c | 182 ++++++++++++++++++++++++++++++++++------ 1 file changed, 156 insertions(+), 26 deletions(-) diff --git a/libavfilter/af_adelay.c b/libavfilter/af_adelay.c index ed8a8ae739..382be3dca2 100644 --- a/libavfilter/af_adelay.c +++ b/libavfilter/af_adelay.c @@ -31,6 +31,7 @@ typedef struct ChanDelay { int64_t delay; size_t delay_index; size_t index; + unsigned int samples_size; uint8_t *samples; } ChanDelay; @@ -48,13 +49,14 @@ typedef struct AudioDelayContext { void (*delay_channel)(ChanDelay *d, int nb_samples, const uint8_t *src, uint8_t *dst); + int (*resize_channel_samples)(ChanDelay *d, int64_t new_delay); } AudioDelayContext; #define OFFSET(x) offsetof(AudioDelayContext, x) #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM static const AVOption adelay_options[] = { - { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A }, + { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A | AV_OPT_FLAG_RUNTIME_PARAM }, { "all", "use last available delay for remained channels", OFFSET(all), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A }, { NULL } }; @@ -96,11 +98,93 @@ DELAY(s32, int32_t, 0) DELAY(flt, float, 0) DELAY(dbl, double, 0) +#define CHANGE_DELAY(name, type, fill) \ +static int resize_samples_## name ##p(ChanDelay *d, int64_t new_delay) \ +{ \ + type *samples; \ + \ + if (new_delay == d->delay) { \ + return 0; \ + } \ + \ + if (new_delay == 0) { \ + av_freep(&d->samples); \ + d->samples_size = 0; \ + d->delay = 0; \ + d->index = 0; \ + d->delay_index = 0; \ + return 0; \ + } \ + \ + samples = (type *) av_fast_realloc(d->samples, &d->samples_size, new_delay * sizeof(type)); \ + if (!samples) { \ + return AVERROR(ENOMEM); \ + } \ + \ + if (new_delay < d->delay) { \ + if (d->index > new_delay) { \ + d->index -= new_delay; \ + memmove(samples, &samples[new_delay], d->index * sizeof(type)); \ + d->delay_index = new_delay; \ + } else if (d->delay_index > d->index) { \ + memmove(&samples[d->index], &samples[d->index+(d->delay-new_delay)], \ + (new_delay - d->index) * sizeof(type)); \ + d->delay_index -= d->delay - new_delay; \ + } \ + } else { \ + size_t block_size; \ + if (d->delay_index >= d->delay) { \ + block_size = (d->delay - d->index) * sizeof(type); \ + memmove(&samples[d->index+(new_delay - d->delay)], &samples[d->index], block_size); \ + d->delay_index = new_delay; \ + } else { \ + d->delay_index += new_delay - d->delay; \ + } \ + block_size = (new_delay - d->delay) * sizeof(type); \ + memset(&samples[d->index], fill, block_size); \ + } \ + d->delay = new_delay; \ + d->samples = (void *) samples; \ + return 0; \ +} + +CHANGE_DELAY(u8, uint8_t, 0x80) +CHANGE_DELAY(s16, int16_t, 0) +CHANGE_DELAY(s32, int32_t, 0) +CHANGE_DELAY(flt, float, 0) +CHANGE_DELAY(dbl, double, 0) + +static int parse_delays(char *p, char **saveptr, int64_t *result, AVFilterContext *ctx, int sample_rate) { + float delay, div; + int ret; + char *arg; + char type = 0; + + if (!(arg = av_strtok(p, "|", saveptr))) + return 1; + + ret = av_sscanf(arg, "%"SCNd64"%c", result, &type); + if (ret != 2 || type != 'S') { + div = type == 's' ? 1.0 : 1000.0; + if (av_sscanf(arg, "%f", &delay) != 1) { + av_log(ctx, AV_LOG_ERROR, "Invalid syntax for delay.\n"); + return AVERROR(EINVAL); + } + *result = delay * sample_rate / div; + } + + if (*result < 0) { + av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n"); + return AVERROR(EINVAL); + } + return 0; +} + static int config_input(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; AudioDelayContext *s = ctx->priv; - char *p, *arg, *saveptr = NULL; + char *p, *saveptr = NULL; int i; s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay)); @@ -112,29 +196,14 @@ static int config_input(AVFilterLink *inlink) p = s->delays; for (i = 0; i < s->nb_delays; i++) { ChanDelay *d = &s->chandelay[i]; - float delay, div; - char type = 0; int ret; - if (!(arg = av_strtok(p, "|", &saveptr))) + ret = parse_delays(p, &saveptr, &d->delay, ctx, inlink->sample_rate); + if (ret == 1) break; - + else if (ret < 0) + return ret; p = NULL; - - ret = av_sscanf(arg, "%"SCNd64"%c", &d->delay, &type); - if (ret != 2 || type != 'S') { - div = type == 's' ? 1.0 : 1000.0; - if (av_sscanf(arg, "%f", &delay) != 1) { - av_log(ctx, AV_LOG_ERROR, "Invalid syntax for delay.\n"); - return AVERROR(EINVAL); - } - d->delay = delay * inlink->sample_rate / div; - } - - if (d->delay < 0) { - av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n"); - return AVERROR(EINVAL); - } } if (s->all && i) { @@ -171,21 +240,81 @@ static int config_input(AVFilterLink *inlink) d->samples = av_malloc_array(d->delay, s->block_align); if (!d->samples) return AVERROR(ENOMEM); + d->samples_size = d->delay * s->block_align; s->max_delay = FFMAX(s->max_delay, d->delay); } switch (inlink->format) { - case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break; - case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break; - case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break; - case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break; - case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break; + case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; + s->resize_channel_samples = resize_samples_u8p; break; + case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; + s->resize_channel_samples = resize_samples_s16p; break; + case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; + s->resize_channel_samples = resize_samples_s32p; break; + case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; + s->resize_channel_samples = resize_samples_fltp; break; + case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; + s->resize_channel_samples = resize_samples_dblp; break; } return 0; } +static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, + char *res, int res_len, int flags) +{ + int ret = AVERROR(ENOSYS); + AVFilterLink *inlink = ctx->inputs[0]; + AudioDelayContext *s = ctx->priv; + + if (!strcmp(cmd, "delays")) { + int64_t delay; + char *p, *saveptr = NULL; + int64_t all_delay = -1; + int64_t max_delay = 0; + char *args_cpy = av_strdup(args); + if (args_cpy == NULL) { + return AVERROR(ENOMEM); + } + + ret = 0; + p = args_cpy; + + if (!strncmp(args, "all:", 4)) { + p = &args_cpy[4]; + ret = parse_delays(p, &saveptr, &all_delay, ctx, inlink->sample_rate); + if (ret == 1) + ret = AVERROR(EINVAL); + else if (ret == 0) + delay = all_delay; + } + + if (!ret) { + for (int i = 0; i < s->nb_delays; i++) { + ChanDelay *d = &s->chandelay[i]; + + if (all_delay < 0) { + ret = parse_delays(p, &saveptr, &delay, ctx, inlink->sample_rate); + if (ret != 0) { + ret = 0; + break; + } + p = NULL; + } + + ret = s->resize_channel_samples(d, delay); + if (ret) + break; + max_delay = FFMAX(max_delay, d->delay); + } + s->max_delay = FFMAX(s->max_delay, max_delay); + } + av_freep(&args_cpy); + } + return ret; +} + static int filter_frame(AVFilterLink *inlink, AVFrame *frame) { AVFilterContext *ctx = inlink->dst; @@ -330,4 +459,5 @@ const AVFilter ff_af_adelay = { FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP), .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, + .process_command = process_command, };