From patchwork Fri Jul 23 09:33:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 29030 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp1234660ios; Fri, 23 Jul 2021 02:34:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqFXoHy6JOc7mdCyP3u0niWXicg2bEHIjBXtnuxCx4IjdcLcsEElDvZNgQQszZXVG+PSmM X-Received: by 2002:a05:6402:17d3:: with SMTP id s19mr4482866edy.49.1627032862622; Fri, 23 Jul 2021 02:34:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627032862; cv=none; d=google.com; s=arc-20160816; b=PqPIkSLcANDFVF2bFFjzqKxAaOSgGYiI1Z0EyIB2Zh+YBphB3VDNJ/u/LlXqCx79JC 4tmjYdzRgUFScOOZylgSt9f90iU/Nw3zioZQJvhX9VJ87JWjoBKRKvZsW5YifVcP4HWj ly16OfvHoW40+ZyiH+LMYA6zYFhYlTOdzRd2JhtXW6NbXfB5BLUdphrjr9Ew0AzmN6ot Fii3GERI3KjxnPRJT6lCcF47DEFqnuMZkjbIGEMSi0uLBam8MCM2YE8BD7Xan3J0koBH NOh4H6sr6/lAx4ucInX/E0Qukz0sfqp3TFDjDobT0h9C6ZVEsiFHqv3KWqybVZN3nEIH XDtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=qNl6kaO7ogb6i5cXaw0HS+5BKgcYN3nx8ySjNg1xwI8=; b=iGLR2aOwkuhX58gUxNUOzgD3bE9EbwcrqiduVYjH6Q5v8GAZdvmDnutNHrg7WBhuhC klO0HM41pl24iSPQOl3FA99oNnPx4gzfpUqgkwgxwzhkHpLAqM7aaP95nQn0UWKqArzo GCOirWFMckwYzXlw97KB8ZxaqPL38pnP9xW4YA8LrdqTpvrTvhjtSYNQH6TvWkFmW7/G nImfDgiKJ27HphZXOJ57tp37z82h+/34N2xG3r1QSu7FM2fp8KXH1VJOJx8LPwY0aNDe Hn+86IoPFl9+IEL8HxPrrmGZZpyYDVi6kOvNHwIT43CCU+Ub54ivOJE+ZQYA+zKux8GM 3OFA== ARC-Authentication-Results: i=1; mx.google.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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v22si33762896ejc.505.2021.07.23.02.34.22; Fri, 23 Jul 2021 02:34:22 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 54BF168A2FE; Fri, 23 Jul 2021 12:34:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 42B8A689CDF for ; Fri, 23 Jul 2021 12:34:10 +0300 (EEST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4GWPKF6xtJzQkC5 for ; Fri, 23 Jul 2021 11:34:09 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id mWAEB_dUKLAd for ; Fri, 23 Jul 2021 11:34:06 +0200 (CEST) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Fri, 23 Jul 2021 15:03:40 +0530 Message-Id: <20210723093341.2000-1-ffmpeg@gyani.pro> MIME-Version: 1.0 X-Rspamd-Queue-Id: 004521845 X-Rspamd-UID: f8c480 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/noise: don't force non-zero value for amount 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: F1Y2eeEUNLKJ Currently, the user is forced to accept some noise in packet payload, even if not requested. --- doc/bitstream_filters.texi | 13 +++++-------- libavcodec/noise_bsf.c | 21 +++++++++++++++------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index d10842ae47..2b84bda1fc 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -534,20 +534,17 @@ container. Can be used for fuzzing or testing error resilience/concealment. Parameters: @table @option @item amount -A numeral string, whose value is related to how often output bytes will -be modified. Therefore, values below or equal to 0 are forbidden, and -the lower the more frequent bytes will be modified, with 1 meaning -every byte is modified. +Accepts a positive integer. Lower the value, more frequently bytes will be modified, +with @var{1} meaning every byte is modified. Default is @var{0}. @item dropamount -A numeral string, whose value is related to how often packets will be dropped. -Therefore, values below or equal to 0 are forbidden, and the lower the more -frequent packets will be dropped, with 1 meaning every packet is dropped. +Accepts a positive integer. Lower the value, more frequently packets will be dropped, +with @var{1} meaning every packet is dropped. Default is @var{0}. @end table The following example applies the modification to every byte but does not drop any packets. @example -ffmpeg -i INPUT -c copy -bsf noise[=1] output.mkv +ffmpeg -i INPUT -c copy -bsf noise=amount=1 output.mkv @end example @section null diff --git a/libavcodec/noise_bsf.c b/libavcodec/noise_bsf.c index 6ebd369633..c1b0203442 100644 --- a/libavcodec/noise_bsf.c +++ b/libavcodec/noise_bsf.c @@ -33,20 +33,28 @@ typedef struct NoiseContext { unsigned int state; } NoiseContext; -static int noise(AVBSFContext *ctx, AVPacket *pkt) +static int noise_init(AVBSFContext *ctx) { NoiseContext *s = ctx->priv_data; - int amount = s->amount > 0 ? s->amount : (s->state % 10001 + 1); - int i, ret; - if (amount <= 0) + if (!s->amount && !s->dropamount) { + av_log(ctx, AV_LOG_ERROR, "At least one of amount and dropamount must be set.\n"); return AVERROR(EINVAL); + } + + return 0; +} + +static int noise(AVBSFContext *ctx, AVPacket *pkt) +{ + NoiseContext *s = ctx->priv_data; + int i, ret; ret = ff_bsf_get_packet_ref(ctx, pkt); if (ret < 0) return ret; - if (s->dropamount > 0 && s->state % s->dropamount == 0) { + if (s->dropamount && s->state % s->dropamount == 0) { s->state++; av_packet_unref(pkt); return AVERROR(EAGAIN); @@ -60,7 +68,7 @@ static int noise(AVBSFContext *ctx, AVPacket *pkt) for (i = 0; i < pkt->size; i++) { s->state += pkt->data[i] + 1; - if (s->state % amount == 0) + if (s->amount && s->state % s->amount == 0) pkt->data[i] = s->state; } @@ -86,5 +94,6 @@ const AVBitStreamFilter ff_noise_bsf = { .name = "noise", .priv_data_size = sizeof(NoiseContext), .priv_class = &noise_class, + .init = noise_init, .filter = noise, }; From patchwork Fri Jul 23 09:33:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 29031 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp1234731ios; Fri, 23 Jul 2021 02:34:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMIV6gBe1q+gplexx0PKPh0QUz/zS2h4k4WPtcx61wsW5ggzathi3bQ2oubgVpoeyqheEH X-Received: by 2002:a17:906:8306:: with SMTP id j6mr3737802ejx.505.1627032873765; Fri, 23 Jul 2021 02:34:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627032873; cv=none; d=google.com; s=arc-20160816; b=kigW0eDyIKvR53BxHLHXopOc6fOEJ9eTJNPPujycBPPJxZEGQ4fwqFZUrSlD702Suj OKRXVujTwQ84kjDzw65PrdAr2F3JTBB/NT3e4kLxANvCUU9HUNi1VgyIwTXejbPi9i6f AaFK0NU/waXx3I5kNWKcAqKL5gHIdBrKUqXO5GSNqBrsVJ6IhrtqYF0j1C5Fr3ixhfwv JHnC0ilKLKzDixsPrkrEXrVJyPqYjuIoX3zN+nUQzwoBAblPCXQnYxOjhqGnjB03eA0h YkgaPMX8sGJAgnREl4x0i2RPAzFhBQNyAzx1KzMMp+ccNTsXnUf4JyR2DydacCGgfXou VPBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=mEaRl4sUda9Nvda+SSDtjRtf4ny6f4dx2Pj05v5FGW0=; b=K7lKZnI/hYBaLmTbLbsF3e1Y6y2VsXr9ArVwasKVUbLhNwjLuU3hJhJCAZXvr8ymy1 CD69CKajJEoTbr8H18CrZbfnu9xrFTXpQ8k1ZNlzJQRqmn1m/va6vmXS0JiqZD6qviL8 tb4uUePjt3e7nP2lZvsNwzu5z2FLhRNdXRMqGg4tc+mg5qv01kWY/bFpmASLi+8JnE89 aVGbHyflHozsT9yzruOCuuJbwiqZOmNv8geAB9OiZ3Y1lVPr7yWpopbRkCiM4ZDQMe9t e8dxMzbapZMJzQDZokcOzOjMQEQWc1tIA1Gxghkbq+43GqfUGHJipC1H4MZkVBtWqk2/ jhHg== ARC-Authentication-Results: i=1; mx.google.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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id cw15si34090042ejc.604.2021.07.23.02.34.33; Fri, 23 Jul 2021 02:34:33 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 595E668ACE2; Fri, 23 Jul 2021 12:34:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 49C2268A922 for ; Fri, 23 Jul 2021 12:34:10 +0300 (EEST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4GWPKG2s3mzQjgX for ; Fri, 23 Jul 2021 11:34:10 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id r66pKGUXDmVA for ; Fri, 23 Jul 2021 11:34:07 +0200 (CEST) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Fri, 23 Jul 2021 15:03:41 +0530 Message-Id: <20210723093341.2000-2-ffmpeg@gyani.pro> In-Reply-To: <20210723093341.2000-1-ffmpeg@gyani.pro> References: <20210723093341.2000-1-ffmpeg@gyani.pro> MIME-Version: 1.0 X-Rspamd-Queue-Id: 948EA1849 X-Rspamd-UID: afcb82 Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/noise: allow various cadence for dropping packets 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: qgdEH26wpnYB Packets can now be dropped at fixed intervals or with varying degrees of irregularity --- doc/bitstream_filters.texi | 22 +++++++++++++ libavcodec/noise_bsf.c | 65 ++++++++++++++++++++++++++++++-------- libavcodec/version.h | 2 +- 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index 2b84bda1fc..4ab6cc15ab 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -539,6 +539,22 @@ with @var{1} meaning every byte is modified. Default is @var{0}. @item dropamount Accepts a positive integer. Lower the value, more frequently packets will be dropped, with @var{1} meaning every packet is dropped. Default is @var{0}. +@item drop_pattern +Regulate how packets are dropped, starting with packet index of @var{drop_offset}. +Possible values are +@table @option +@item 0 (@emph{default}) +Drop at fixed intervals of @var{dropamount}, e.g. with dropamount @var{5} and drop_offset @var{2}, +packets no. @code{2, 7, 12...} will be dropped. +@item 1 +Drop a random packet in each interval of size @var{dropamount}. +@item 2 +Drop a random packet in roughly inverse frequency to @var{dropamount}. Over a short range of packets, +the drop frequency is less stable than pattern value @var{1}. +@end table +@item drop_offset +Accepts a positive integer. Represents the index of the first packet at which drop evaluations +start. Default is @var{0}. @end table The following example applies the modification to every byte but does not drop @@ -547,6 +563,12 @@ any packets. ffmpeg -i INPUT -c copy -bsf noise=amount=1 output.mkv @end example +The following example drops one random packet every 5 packets starting with packet #2 and modifies +roughly every 3rd byte of remaining packets. +@example +ffmpeg -i INPUT -c copy -bsf noise=amount=3:dropamount=5:drop_pattern=1:drop_offset=2 output.mkv +@end example + @section null This bitstream filter passes the packets through unchanged. diff --git a/libavcodec/noise_bsf.c b/libavcodec/noise_bsf.c index c1b0203442..83118c12cf 100644 --- a/libavcodec/noise_bsf.c +++ b/libavcodec/noise_bsf.c @@ -30,7 +30,12 @@ typedef struct NoiseContext { const AVClass *class; int amount; int dropamount; + int drop_pattern; + int drop_offset; + unsigned int state; + unsigned int pkt_idx; + unsigned int drop_idx; } NoiseContext; static int noise_init(AVBSFContext *ctx) @@ -48,31 +53,61 @@ static int noise_init(AVBSFContext *ctx) static int noise(AVBSFContext *ctx, AVPacket *pkt) { NoiseContext *s = ctx->priv_data; - int i, ret; + int i, ret, interval_pos, drop; ret = ff_bsf_get_packet_ref(ctx, pkt); if (ret < 0) return ret; - if (s->dropamount && s->state % s->dropamount == 0) { - s->state++; - av_packet_unref(pkt); - return AVERROR(EAGAIN); + if (s->dropamount && s->pkt_idx >= s->drop_offset) { + + interval_pos = (s->pkt_idx - s->drop_offset) % s->dropamount; + + switch (s->drop_pattern) { + case 0: + drop = interval_pos == 0; + break; + case 1: + if (interval_pos == 0) + s->drop_idx = s->pkt_idx + s->state % s->dropamount; + drop = s->pkt_idx == s->drop_idx; + break; + case 2: + drop = s->state % s->dropamount == 0; + break; + } + + if (drop) { + av_log(ctx, AV_LOG_VERBOSE, "Stream #%d packet #%d with pts %"PRId64" dropped.\n", pkt->stream_index, s->pkt_idx, pkt->pts); + s->state++; + av_packet_unref(pkt); + ret = AVERROR(EAGAIN); + goto done; + } } - ret = av_packet_make_writable(pkt); - if (ret < 0) { - av_packet_unref(pkt); - return ret; + if (s->amount) { + ret = av_packet_make_writable(pkt); + if (ret < 0) { + av_packet_unref(pkt); + goto done; + } } - for (i = 0; i < pkt->size; i++) { - s->state += pkt->data[i] + 1; - if (s->amount && s->state % s->amount == 0) - pkt->data[i] = s->state; + if (s->amount || s->drop_pattern) { + + for (i = 0; i < pkt->size; i++) { + s->state += pkt->data[i] + 1; + if (s->amount && s->state % s->amount == 0) + pkt->data[i] = s->state; + } } - return 0; + ret = 0; + +done: + s->pkt_idx++; + return ret; } #define OFFSET(x) offsetof(NoiseContext, x) @@ -80,6 +115,8 @@ static int noise(AVBSFContext *ctx, AVPacket *pkt) static const AVOption options[] = { { "amount", NULL, OFFSET(amount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, { "dropamount", NULL, OFFSET(dropamount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, + { "drop_pattern", NULL, OFFSET(drop_pattern), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, FLAGS }, + { "drop_offset", NULL, OFFSET(drop_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, { NULL }, }; diff --git a/libavcodec/version.h b/libavcodec/version.h index c660f70669..91325ce4e7 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -29,7 +29,7 @@ #define LIBAVCODEC_VERSION_MAJOR 59 #define LIBAVCODEC_VERSION_MINOR 3 -#define LIBAVCODEC_VERSION_MICRO 101 +#define LIBAVCODEC_VERSION_MICRO 102 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \