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

Clément Bœsch ubitux at gmail.com
Wed Mar 13 06:08:53 CET 2013


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

diff --git a/doc/filters.texi b/doc/filters.texi
index 4610bde..1c52b40 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -6082,6 +6082,27 @@ 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 filter parameter can be one of the following:
+
+ at table @option
+ at item readonly, ro
+Set output frame as non-writable.
+ at item readwrite, rw
+Make the output frame directly writable.
+ at item toggle
+Make the frame read-only if writable, and writable if read-only.
+ at end table
+
+Note: in case of auto-inserted filter between the permission filter and the
+following, the permission will not be the one expected. Inserting a format or
+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..b8b991c 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -80,6 +80,7 @@ OBJS-$(CONFIG_HIGHPASS_FILTER)               += af_biquads.o
 OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
 OBJS-$(CONFIG_LOWPASS_FILTER)                += af_biquads.o
 OBJS-$(CONFIG_PAN_FILTER)                    += af_pan.o
+OBJS-$(CONFIG_APERMS_FILTER)                 += f_perms.o
 OBJS-$(CONFIG_RESAMPLE_FILTER)               += af_resample.o
 OBJS-$(CONFIG_SILENCEDETECT_FILTER)          += af_silencedetect.o
 OBJS-$(CONFIG_TREBLE_FILTER)                 += af_biquads.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..9421ffa
--- /dev/null
+++ b/libavfilter/f_perms.c
@@ -0,0 +1,140 @@
+/*
+ * 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 "avfilter.h"
+#include "internal.h"
+#include "audio.h"
+#include "video.h"
+
+enum perm { RO, RW, TOGGLE };
+
+static const char *perm_str[2] = {"RO", "RW"};
+
+typedef struct {
+    enum perm p;
+} PermsContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args)
+{
+    PermsContext *perms = ctx->priv;
+
+    if (!args) {
+        av_log(ctx, AV_LOG_ERROR, "A \"rw\", \"ro\" or \"toggle\" parameter is required.\n");
+        return AVERROR(EINVAL);
+    }
+    if (!strcmp(args, "ro") || !strcmp(args, "readonly")) {
+        perms->p = RO;
+    } else if (!strcmp(args, "rw") || !strcmp(args, "readwrite")) {
+        perms->p = RW;
+    } else if (!strcmp(args, "toggle")) {
+        perms->p = TOGGLE;
+    } else {
+        av_log(ctx, AV_LOG_ERROR, "Unrecognized \"%s\" permission\n", args);
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    AVFilterContext *ctx = inlink->dst;
+    PermsContext *perms = ctx->priv;
+    AVFrame *out = frame;
+    int out_perm, in_perm = av_frame_is_writable(frame) ? RW : RO;
+    int ret;
+
+    if (perms->p == TOGGLE)
+        out_perm = in_perm == RO ? RW : RO;
+    else
+        out_perm = perms->p;
+
+    av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n",
+           perm_str[in_perm], perm_str[out_perm]);
+
+    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 (out != frame)
+        av_frame_free(&frame);
+    return ret;
+}
+
+#if CONFIG_APERMS_FILTER
+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        = init,
+    .priv_size   = sizeof(PermsContext),
+    .inputs      = aperms_inputs,
+    .outputs     = aperms_outputs,
+};
+#endif /* CONFIG_APERMS_FILTER */
+
+#if CONFIG_PERMS_FILTER
+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        = init,
+    .priv_size   = sizeof(PermsContext),
+    .inputs      = perms_inputs,
+    .outputs     = perms_outputs,
+};
+#endif /* CONFIG_PERMS_FILTER */
-- 
1.8.1.5



More information about the ffmpeg-devel mailing list