From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 1054C47967 for ; Thu, 28 Dec 2023 00:38:50 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B10F868CA1F; Thu, 28 Dec 2023 02:38:48 +0200 (EET) Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5D33468C1DF for ; Thu, 28 Dec 2023 02:38:42 +0200 (EET) Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-50e7aed09adso2907180e87.0 for ; Wed, 27 Dec 2023 16:38:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703723921; x=1704328721; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=4FySfW6igDjGSoBYa/rZrr/znfwOkS8O7N7cqlWaNwg=; b=BEzkxHJ2Q7+kOU/mkDL7/K4dti54da3uweRYGyzQOyW9EzVH9Sh8itZC4zJ1t8nhSc T1OjMZrN4o/nOiK2o3kcTz2iHYe4/BSPdq6tfbiN4CgeBcpqow23IvhX09FKarcJlMYE KQMpS7lBB6o1abbyj0sJitiv3tUShPIYpScXoqD1ayRCkKbAUkDON1O8QLsr5IPh1KWB hL0aXapANrpwDRHXRi8eXO9QCeyujz7OguK2BQvu2rnEhtW1AkzbwEuJpSasA89Bw1mq Z5H3zFytDYbEA6g+Wlskhf/C2UKn7kB/+aeb5ba8/9vNXbL/+kEvECNU2LAg/o8VT1vw LuDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703723921; x=1704328721; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4FySfW6igDjGSoBYa/rZrr/znfwOkS8O7N7cqlWaNwg=; b=MlX8WyzzoNrxsJe5pQuwAsl1oNL2NC2QOj4llYpTLHYRO0zsEJm2dIQEGKJwzKwdZa fyFsBW9xCIBaDM+uL3pxsqKlZ66HWi7gx3v3vchsWhO9Ai6rS+YdyzWYk5vnTgko2OtA U+XmYOU8q9ifpjek1Mv346WtnPlhoEG9uY71mrsOYp0Vm3paqUlD45PO1wh3ZrgeUYNI lCF0U9JuLUjm5GZWbtqvZY7TjTXReuXBofbKUjflDjhwhKSrNxXYiaSyOiB9PbCIRf68 oGNYWffttve1arSRR8fc78UztLcr55iE3UgFsVLR4kZlJw0XOjZy5kKiEulshOFiPB/2 tYkw== X-Gm-Message-State: AOJu0YzgRGKaVeOOS4lgI5/pxxjn1gEWtTMBaYu/lIG6IAE7kmK6Du14 5bFlQCG6MmMA6/JjaJpS81Dp62jXk2I= X-Google-Smtp-Source: AGHT+IGH0AfYTzgKw4TK9JdAMSHE9JJKMdLCL2+lyBk6UVDIQfFDuIzCgS8raGS7JvTHKSzoN/TP6g== X-Received: by 2002:ac2:5475:0:b0:50e:786a:2cb0 with SMTP id e21-20020ac25475000000b0050e786a2cb0mr2335153lfn.102.1703723920891; Wed, 27 Dec 2023 16:38:40 -0800 (PST) Received: from mariano (dynamic-adsl-84-220-189-10.clienti.tiscali.it. [84.220.189.10]) by smtp.gmail.com with ESMTPSA id ex15-20020a170907954f00b00a26ae85cfeasm6184758ejc.28.2023.12.27.16.38.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Dec 2023 16:38:40 -0800 (PST) Received: by mariano (Postfix, from userid 1000) id 9FBFFBFCDC; Thu, 28 Dec 2023 01:38:39 +0100 (CET) From: Stefano Sabatini To: FFmpeg development discussions and patches Date: Thu, 28 Dec 2023 01:38:39 +0100 Message-Id: <20231228003839.860894-1-stefasab@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression 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: Stefano Sabatini Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: This is useful to simulate random jitter. --- Changelog | 1 + doc/filters.texi | 10 +++++++++- libavfilter/setpts.c | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Changelog b/Changelog index 424bfc11af..ed01c53264 100644 --- a/Changelog +++ b/Changelog @@ -15,6 +15,7 @@ version : - tiltandshift filter - qrencode filter and qrencodesrc source - quirc filter +- lavfi/setpts: introduce rand() function in expression version 6.1: - libaribcaption decoder diff --git a/doc/filters.texi b/doc/filters.texi index d1f95b9781..1d9a5d6c7d 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -30946,7 +30946,7 @@ The expression which is evaluated for each frame to construct its timestamp. @end table The expression is evaluated through the eval API and can contain the following -constants: +constants and functions: @table @option @item FRAME_RATE, FR @@ -31010,6 +31010,8 @@ The timebase of the input timestamps. @item T_CHANGE Time of the first frame after command was applied or time of the first frame if no commands. +@item rand(min, max) +a random number included between min and max @end table @subsection Examples @@ -31021,6 +31023,12 @@ Start counting PTS from zero setpts=PTS-STARTPTS @end example +@item +Apply a random jitter effect of +/-100 TB units: +@example +setpts=PTS+100rand(-100\,100) +@end example + @item Apply fast motion effect: @example diff --git a/libavfilter/setpts.c b/libavfilter/setpts.c index 88a8d6af86..0f24a900b3 100644 --- a/libavfilter/setpts.c +++ b/libavfilter/setpts.c @@ -32,6 +32,8 @@ #include "libavutil/internal.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" +#include "libavutil/lfg.h" +#include "libavutil/random_seed.h" #include "libavutil/time.h" #include "audio.h" #include "avfilter.h" @@ -101,18 +103,39 @@ typedef struct SetPTSContext { AVExpr *expr; double var_values[VAR_VARS_NB]; enum AVMediaType type; + AVLFG lfg; } SetPTSContext; #define V(name_) \ setpts->var_values[VAR_##name_] +static double drand(void *ctx, double min, double max) +{ + SetPTSContext *setpts = ((AVFilterContext *)ctx)->priv; + + return min + (max-min) / UINT_MAX * av_lfg_get(&setpts->lfg); +} + +typedef double (*eval_func2)(void *, double a, double b); + +static const eval_func2 fun2[] = { + drand, + NULL +}; + +static const char *const fun2_names[] = { + "rand" +}; + static av_cold int init(AVFilterContext *ctx) { SetPTSContext *setpts = ctx->priv; int ret; + av_lfg_init(&setpts->lfg, av_get_random_seed()); + if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str, - var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) { + var_names, NULL, NULL, fun2_names, fun2, 0, ctx)) < 0) { av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str); return ret; } @@ -126,6 +149,7 @@ static av_cold int init(AVFilterContext *ctx) V(STARTPTS) = NAN; V(STARTT) = NAN; V(T_CHANGE) = NAN; + return 0; } @@ -159,8 +183,10 @@ static inline char *double2int64str(char *buf, double v) return buf; } -static double eval_pts(SetPTSContext *setpts, AVFilterLink *inlink, AVFrame *frame, int64_t pts) +static double eval_pts(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *frame, int64_t pts) { + SetPTSContext *setpts = ctx->priv; + if (isnan(V(STARTPTS))) { V(STARTPTS) = TS2D(pts); V(STARTT ) = TS2T(pts, inlink->time_base); @@ -186,17 +212,18 @@ FF_ENABLE_DEPRECATION_WARNINGS } } - return av_expr_eval(setpts->expr, setpts->var_values, NULL); + return av_expr_eval(setpts->expr, setpts->var_values, ctx); } #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v) static int filter_frame(AVFilterLink *inlink, AVFrame *frame) { - SetPTSContext *setpts = inlink->dst->priv; + AVFilterContext *ctx = inlink->dst; + SetPTSContext *setpts = ctx->priv; int64_t in_pts = frame->pts; double d; - d = eval_pts(setpts, inlink, frame, frame->pts); + d = eval_pts(ctx, inlink, frame, frame->pts); frame->pts = D2TS(d); av_log(inlink->dst, AV_LOG_TRACE, @@ -250,7 +277,7 @@ static int activate(AVFilterContext *ctx) return filter_frame(inlink, in); if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { - double d = eval_pts(setpts, inlink, NULL, pts); + double d = eval_pts(ctx, inlink, NULL, pts); av_log(ctx, AV_LOG_TRACE, "N:EOF PTS:%s T:%f -> PTS:%s T:%f\n", d2istr(V(PTS)), V(T), d2istr(d), TS2T(d, inlink->time_base)); -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".