[FFmpeg-devel] [PATCH] lavfi: add perms and aperms filters.

Clément Bœsch ubitux at gmail.com
Thu Mar 14 22:06:15 CET 2013


On Thu, Mar 14, 2013 at 09:22:46PM +0100, Clément Bœsch wrote:
> On Wed, Mar 13, 2013 at 06:43:28AM +0100, Clément Bœsch wrote:
> > On Wed, Mar 13, 2013 at 06:08:53AM +0100, Clément Bœsch wrote:
> > > TODO: bump micro
> > > TODO: Changelog
> > > ---
> > > Is kind of helpful to test with gradfun, hue, etc. Could be part of FATE. I
> > > could also add a random setting if someone is interested (note that if part of
> > > FATE, it will be useful to have a real random, but that might be problematic in
> > > a library)
> > > ---
> > >  doc/filters.texi         |  21 +++++++
> > >  libavfilter/Makefile     |   2 +
> > >  libavfilter/allfilters.c |   2 +
> > >  libavfilter/f_perms.c    | 140 +++++++++++++++++++++++++++++++++++++++++++++++
> > >  4 files changed, 165 insertions(+)
> > >  create mode 100644 libavfilter/f_perms.c
> > > 
> > 
> > New version with the random and simplifications.
> > 
> 
> Ping.
> 
> I moved to the AVOption, and changed various things. Comments welcome. I'd
> like to push this in the next days so we can consider integrating them to
> FATE for various filters.
> 

Something was not working as expected with RO -> RW, patch updated.
And extra bonus FATE patch. I'll push all of this in 3 days if I see no
comments.

-- 
Clément B.
-------------- next part --------------
From 0b49709256743328e9fb2dd483324a50934fce5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Wed, 13 Mar 2013 05:59:20 +0100
Subject: [PATCH 1/2] lavfi: add perms and aperms filters.

TODO: bump micro
TODO: Changelog
---
 doc/filters.texi         |  37 ++++++++++
 libavfilter/Makefile     |   2 +
 libavfilter/allfilters.c |   2 +
 libavfilter/f_perms.c    | 186 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 227 insertions(+)
 create mode 100644 libavfilter/f_perms.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 3c5955e..41903c9 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -662,6 +662,7 @@ afade=t=out:ss=875:d=25
 @end example
 @end itemize
 
+ at anchor{aformat}
 @section aformat
 
 Set output format constraints for the input audio. The framework will
@@ -3028,6 +3029,7 @@ framework.
 
 The filter does not take parameters.
 
+ at anchor{format}
 @section format
 
 Convert the input video to one of the specified pixel formats.
@@ -6085,6 +6087,41 @@ tools.
 
 Below is a description of the currently available multimedia filters.
 
+ at section aperms, perms
+Set read/write permissions for the output frames.
+
+These filters are mainly aimed at developers to test direct path in the
+following filter in the filtergraph.
+
+The filters accept parameters as a list of @var{key}=@var{value} pairs,
+separated by ":". If the key of the first options is omitted, the argument is
+assumed to be the @var{mode}.
+
+A description of the accepted parameters follows.
+
+ at table @option
+ at item mode
+Select the permissions mode.
+
+ at table @option
+ at item none
+Do nothing. This is the default.
+ at item ro
+Set all the output frames read-only.
+ at item rw
+Set all the output frames directly writable.
+ at item toggle
+Make the frame read-only if writable, and writable if read-only.
+ at item random
+Set each output frame read-only or writable randomly.
+ at end table
+ at end table
+
+Note: in case of auto-inserted filter between the permission filter and the
+following one, the permission might not be received as expected in that
+following filter. Inserting a @ref{format} or @ref{aformat} filter before the
+perms/aperms filter can avoid this problem.
+
 @section aselect, select
 Select frames to pass in output.
 
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index a1d58d3..2758c8e 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -56,6 +56,7 @@ OBJS-$(CONFIG_AMERGE_FILTER)                 += af_amerge.o
 OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
 OBJS-$(CONFIG_APAD_FILTER)                   += af_apad.o
+OBJS-$(CONFIG_APERMS_FILTER)                 += f_perms.o
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
 OBJS-$(CONFIG_ASELECT_FILTER)                += f_select.o
 OBJS-$(CONFIG_ASENDCMD_FILTER)               += f_sendcmd.o
@@ -139,6 +140,7 @@ OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
 OBJS-$(CONFIG_OCV_FILTER)                    += vf_libopencv.o
 OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
+OBJS-$(CONFIG_PERMS_FILTER)                  += f_perms.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
 OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 51d7996..ed5eb1b 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -75,6 +75,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(HIGHPASS,       highpass,       af);
     REGISTER_FILTER(JOIN,           join,           af);
     REGISTER_FILTER(LOWPASS,        lowpass,        af);
+    REGISTER_FILTER(APERMS,         aperms,         af);
     REGISTER_FILTER(PAN,            pan,            af);
     REGISTER_FILTER(RESAMPLE,       resample,       af);
     REGISTER_FILTER(SILENCEDETECT,  silencedetect,  af);
@@ -135,6 +136,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(OCV,            ocv,            vf);
     REGISTER_FILTER(OVERLAY,        overlay,        vf);
     REGISTER_FILTER(PAD,            pad,            vf);
+    REGISTER_FILTER(PERMS,          perms,          vf);
     REGISTER_FILTER(PIXDESCTEST,    pixdesctest,    vf);
     REGISTER_FILTER(PP,             pp,             vf);
     REGISTER_FILTER(REMOVELOGO,     removelogo,     vf);
diff --git a/libavfilter/f_perms.c b/libavfilter/f_perms.c
new file mode 100644
index 0000000..7eba6fe
--- /dev/null
+++ b/libavfilter/f_perms.c
@@ -0,0 +1,186 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/lfg.h"
+#include "libavutil/opt.h"
+#include "libavutil/random_seed.h"
+#include "audio.h"
+#include "video.h"
+
+enum mode {
+    MODE_DONOTHING,
+    MODE_RO,
+    MODE_RW,
+    MODE_TOGGLE,
+    MODE_RANDOM,
+    NB_MODES
+};
+
+typedef struct {
+    const AVClass *class;
+    AVLFG lfg;
+    enum mode mode;
+} PermsContext;
+
+#define OFFSET(x) offsetof(PermsContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption options[] = {
+    { "mode", "select permissions mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_DONOTHING}, MODE_DONOTHING, NB_MODES-1, FLAGS, "mode" },
+        { "none",   "do nothing",                       0, AV_OPT_TYPE_CONST, {.i64 = MODE_DONOTHING},  INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "ro",     "set all output frames read-only",  0, AV_OPT_TYPE_CONST, {.i64 = MODE_RO},         INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "rw",     "set all output frames writable",   0, AV_OPT_TYPE_CONST, {.i64 = MODE_RW},         INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "toggle", "switch permissions",               0, AV_OPT_TYPE_CONST, {.i64 = MODE_TOGGLE},     INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "random", "set permissions randomly",         0, AV_OPT_TYPE_CONST, {.i64 = MODE_RANDOM},     INT_MIN, INT_MAX, FLAGS, "mode" },
+    { NULL }
+};
+
+static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
+{
+    int ret;
+    PermsContext *perms = ctx->priv;
+    static const char *shorthand[] = { "mode", NULL };
+
+    perms->class = class;
+    av_opt_set_defaults(perms);
+
+    if ((ret = av_opt_set_from_string(perms, args, shorthand, "=", ":")) < 0)
+        return ret;
+
+    if (perms->mode == MODE_RANDOM)
+        av_lfg_init(&perms->lfg, av_get_random_seed());
+
+    av_opt_free(perms);
+    return 0;
+}
+
+enum perm                        {  RO,   RW  };
+static const char *perm_str[2] = { "RO", "RW" };
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    int ret;
+    AVFilterContext *ctx = inlink->dst;
+    PermsContext *perms = ctx->priv;
+    AVFrame *out = frame;
+    enum perm in_perm = av_frame_is_writable(frame) ? RW : RO;
+    enum perm out_perm;
+
+    switch (perms->mode) {
+    case MODE_TOGGLE:   out_perm = in_perm == RO ? RW : RO;                 break;
+    case MODE_RANDOM:   out_perm = av_lfg_get(&perms->lfg) & 1 ? RW : RO;   break;
+    case MODE_RO:       out_perm = RO;                                      break;
+    case MODE_RW:       out_perm = RW;                                      break;
+    default:            out_perm = in_perm;                                 break;
+    }
+
+    av_log(ctx, AV_LOG_VERBOSE, "%s -> %s%s\n",
+           perm_str[in_perm], perm_str[out_perm],
+           in_perm == out_perm ? " (no-op)" : "");
+
+    if (in_perm == RO && out_perm == RW) {
+        if ((ret = av_frame_make_writable(frame)) < 0)
+            return ret;
+    } else if (in_perm == RW && out_perm == RO) {
+        out = av_frame_clone(frame);
+        if (!out)
+            return AVERROR(ENOMEM);
+    }
+
+    ret = ff_filter_frame(ctx->outputs[0], out);
+
+    if (in_perm == RW && out_perm == RO)
+        av_frame_free(&frame);
+    return ret;
+}
+
+#if CONFIG_APERMS_FILTER
+
+#define aperms_options options
+AVFILTER_DEFINE_CLASS(aperms);
+
+static av_cold int aperms_init(AVFilterContext *ctx, const char *args)
+{
+    return init(ctx, args, &aperms_class);
+}
+
+static const AVFilterPad aperms_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad aperms_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_AUDIO,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_af_aperms = {
+    .name        = "aperms",
+    .description = NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
+    .init        = aperms_init,
+    .priv_size   = sizeof(PermsContext),
+    .inputs      = aperms_inputs,
+    .outputs     = aperms_outputs,
+    .priv_class  = &aperms_class,
+};
+#endif /* CONFIG_APERMS_FILTER */
+
+#if CONFIG_PERMS_FILTER
+
+#define perms_options options
+AVFILTER_DEFINE_CLASS(perms);
+
+static av_cold int perms_init(AVFilterContext *ctx, const char *args)
+{
+    return init(ctx, args, &perms_class);
+}
+
+static const AVFilterPad perms_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad perms_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_perms = {
+    .name        = "perms",
+    .description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
+    .init        = perms_init,
+    .priv_size   = sizeof(PermsContext),
+    .inputs      = perms_inputs,
+    .outputs     = perms_outputs,
+    .priv_class  = &perms_class,
+};
+#endif /* CONFIG_PERMS_FILTER */
-- 
1.8.1.5

-------------- next part --------------
From 01ba7a8149c5fe5a7358d23ed1ee9343d6e15487 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Thu, 14 Mar 2013 21:59:42 +0100
Subject: [PATCH 2/2] fate: test both direct and indirect paths in hqdn3d and
 gradfun.

---
 tests/fate/filter.mak | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tests/fate/filter.mak b/tests/fate/filter.mak
index 139ba38..3b36bd1 100644
--- a/tests/fate/filter.mak
+++ b/tests/fate/filter.mak
@@ -45,12 +45,12 @@ fate-filter-yadif-mode1: CMD = framecrc -flags bitexact -idct simple -i $(SAMPLE
 FATE_FILTER-$(CONFIG_YADIF_FILTER) += $(FATE_YADIF)
 
 FATE_HQDN3D += fate-filter-hqdn3d
-fate-filter-hqdn3d: CMD = framecrc -idct simple -i $(SAMPLES)/smjpeg/scenwin.mjpg -vf hqdn3d -an
-FATE_FILTER-$(call ALLYES, SMJPEG_DEMUXER MJPEG_DECODER HQDN3D_FILTER) += $(FATE_HQDN3D)
+fate-filter-hqdn3d: CMD = framecrc -idct simple -i $(SAMPLES)/smjpeg/scenwin.mjpg -vf perms=random,hqdn3d -an
+FATE_FILTER-$(call ALLYES, SMJPEG_DEMUXER MJPEG_DECODER PERMS_FILTER HQDN3D_FILTER) += $(FATE_HQDN3D)
 
 FATE_GRADFUN += fate-filter-gradfun
-fate-filter-gradfun: CMD = framecrc -i $(SAMPLES)/vmd/12.vmd -vf "sws_flags=+accurate_rnd+bitexact;gradfun=10:8" -an -frames:v 20
-FATE_FILTER-$(call ALLYES, VMD_DEMUXER VMDVIDEO_DECODER GRADFUN_FILTER) += $(FATE_GRADFUN)
+fate-filter-gradfun: CMD = framecrc -i $(SAMPLES)/vmd/12.vmd -vf "sws_flags=+accurate_rnd+bitexact;format=gray,perms=random,gradfun=10:8" -an -frames:v 20
+FATE_FILTER-$(call ALLYES, VMD_DEMUXER VMDVIDEO_DECODER FORMAT_FILTER PERMS_FILTER GRADFUN_FILTER) += $(FATE_GRADFUN)
 
 FATE_SAMPLES_AVCONV += $(FATE_FILTER-yes)
 
-- 
1.8.1.5

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130314/fe325a9c/attachment.asc>


More information about the ffmpeg-devel mailing list