[FFmpeg-devel] [PATCH] extractplanes filter
Paul B Mahol
onemda at gmail.com
Tue Apr 30 23:35:34 CEST 2013
Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_extractplanes.c | 179 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 181 insertions(+)
create mode 100644 libavfilter/vf_extractplanes.c
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index d4ecf95..3e8e5c5 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -120,6 +120,7 @@ OBJS-$(CONFIG_DESHAKE_FILTER) += vf_deshake.o
OBJS-$(CONFIG_DRAWBOX_FILTER) += vf_drawbox.o
OBJS-$(CONFIG_DRAWTEXT_FILTER) += vf_drawtext.o
OBJS-$(CONFIG_EDGEDETECT_FILTER) += vf_edgedetect.o
+OBJS-$(CONFIG_EXTRACTPLANES_FILTER) += vf_extractplanes.o
OBJS-$(CONFIG_FADE_FILTER) += vf_fade.o
OBJS-$(CONFIG_FIELD_FILTER) += vf_field.o
OBJS-$(CONFIG_FIELDMATCH_FILTER) += vf_fieldmatch.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 97e558b..a193d4c 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -118,6 +118,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(DRAWBOX, drawbox, vf);
REGISTER_FILTER(DRAWTEXT, drawtext, vf);
REGISTER_FILTER(EDGEDETECT, edgedetect, vf);
+ REGISTER_FILTER(EXTRACTPLANES, extractplanes, vf);
REGISTER_FILTER(FADE, fade, vf);
REGISTER_FILTER(FIELD, field, vf);
REGISTER_FILTER(FIELDMATCH, fieldmatch, vf);
diff --git a/libavfilter/vf_extractplanes.c b/libavfilter/vf_extractplanes.c
new file mode 100644
index 0000000..79363d8
--- /dev/null
+++ b/libavfilter/vf_extractplanes.c
@@ -0,0 +1,179 @@
+/*
+ * Copyright (c) 2013 Paul B Mahol
+ *
+ * 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/opt.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "internal.h"
+
+typedef struct {
+ const AVClass *class;
+ int flags;
+ int map[4];
+} ExtractPlanesContext;
+
+#define OFFSET(x) offsetof(ExtractPlanesContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption extractplanes_options[] = {
+ { "planes", "set planes", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 1, (1<<4)-1, FLAGS, "flags"},
+ { "y", "set luma", 0, AV_OPT_TYPE_CONST, {.i64=1<<0}, 0, 0, FLAGS, "flags"},
+ { "u", "set u", 0, AV_OPT_TYPE_CONST, {.i64=1<<1}, 0, 0, FLAGS, "flags"},
+ { "v", "set v", 0, AV_OPT_TYPE_CONST, {.i64=1<<2}, 0, 0, FLAGS, "flags"},
+ { "g", "set green", 0, AV_OPT_TYPE_CONST, {.i64=1<<0}, 0, 0, FLAGS, "flags"},
+ { "b", "set blue", 0, AV_OPT_TYPE_CONST, {.i64=1<<1}, 0, 0, FLAGS, "flags"},
+ { "r", "set red", 0, AV_OPT_TYPE_CONST, {.i64=1<<2}, 0, 0, FLAGS, "flags"},
+ { "a", "set alpha", 0, AV_OPT_TYPE_CONST, {.i64=1<<3}, 0, 0, FLAGS, "flags"},
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(extractplanes);
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static const enum AVPixelFormat in_pixfmts[] = {
+ AV_PIX_FMT_YUV410P,
+ AV_PIX_FMT_YUV411P,
+ AV_PIX_FMT_YUV440P,
+ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
+ AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
+ AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
+ AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
+ AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
+ AV_PIX_FMT_GRAY8A,
+ AV_PIX_FMT_GBRP,
+ AV_PIX_FMT_NONE,
+ };
+ static const enum AVPixelFormat out_pixfmts[] = {
+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE,
+ };
+ int i;
+
+ ff_formats_ref(ff_make_format_list(in_pixfmts), &ctx->inputs[0]->out_formats);
+ for (i = 0; i < ctx->nb_outputs; i++)
+ ff_formats_ref(ff_make_format_list(out_pixfmts), &ctx->outputs[i]->in_formats);
+ return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+ AVFilterContext *ctx = outlink->src;
+ AVFilterLink *inlink = ctx->inputs[0];
+ ExtractPlanesContext *e = ctx->priv;
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+ int output;
+
+ sscanf(outlink->srcpad->name, "out%d", &output);
+
+ if (e->map[output] == 1 || e->map[output] == 2) {
+ outlink->h = inlink->h >> desc->log2_chroma_h;
+ outlink->w = inlink->w >> desc->log2_chroma_w;
+ }
+
+ return 0;
+}
+
+static int init(AVFilterContext *ctx)
+{
+ ExtractPlanesContext *e = ctx->priv;
+ int i;
+
+ for (i = 0; i < 4; i++) {
+ char name[32];
+ AVFilterPad pad = { 0 };
+
+ if (!(e->flags & (1 << i)))
+ continue;
+
+ e->map[ctx->nb_outputs] = i;
+ snprintf(name, sizeof(name), "out%d", ctx->nb_outputs);
+ pad.name = av_strdup(name);
+ if (!pad.name)
+ return AVERROR(ENOMEM);
+ pad.type = AVMEDIA_TYPE_VIDEO;
+ pad.config_props = config_output;
+
+ ff_insert_outpad(ctx, ctx->nb_outputs, &pad);
+ }
+
+ return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+ AVFilterContext *ctx = inlink->dst;
+ ExtractPlanesContext *e = ctx->priv;
+ int i, ret = AVERROR_EOF;
+
+ for (i = 0; i < ctx->nb_outputs; i++) {
+ const int idx = e->map[i];
+ AVFilterLink *outlink = ctx->outputs[i];
+ AVFrame *out;
+
+ if (outlink->closed || !frame->data[idx])
+ continue;
+
+ out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+ if (!out) {
+ ret = AVERROR(ENOMEM);
+ break;
+ }
+ av_frame_copy_props(out, frame);
+
+ av_image_copy_plane(out->data[0], out->linesize[0],
+ frame->data[idx], frame->linesize[idx],
+ outlink->w, outlink->h);
+ ret = ff_filter_frame(outlink, out);
+ if (ret < 0)
+ break;
+ }
+ av_frame_free(&frame);
+ return ret;
+}
+
+static void uninit(AVFilterContext *ctx)
+{
+ int i;
+
+ for (i = 0; i < ctx->nb_outputs; i++)
+ av_freep(&ctx->output_pads[i].name);
+}
+
+static const AVFilterPad extractplanes_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame,
+ },
+ { NULL }
+};
+
+AVFilter avfilter_vf_extractplanes = {
+ .name = "extractplanes",
+ .description = NULL_IF_CONFIG_SMALL("Extract planes as grayscale frames."),
+ .priv_size = sizeof(ExtractPlanesContext),
+ .priv_class = &extractplanes_class,
+ .init = init,
+ .uninit = uninit,
+ .query_formats = query_formats,
+ .inputs = extractplanes_inputs,
+ .outputs = NULL,
+ .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
+};
--
1.7.11.2
More information about the ffmpeg-devel
mailing list