[FFmpeg-devel] [PATCH] lavfi: phasescope filter
Stefano Sabatini
stefasab at gmail.com
Thu Mar 28 23:46:38 CET 2013
On date Monday 2013-03-25 22:23:51 +0000, Paul B Mahol encoded:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>
> TODO:
> documentation, more modes, colors, sample formats, channel layouts and fancy output.
>
> ---
> libavfilter/Makefile | 1 +
> libavfilter/allfilters.c | 1 +
> libavfilter/avf_phasescope.c | 214 +++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 216 insertions(+)
> create mode 100644 libavfilter/avf_phasescope.c
>
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 690b1cb..40edb36 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -212,6 +212,7 @@ OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/pullup.o
>
> # multimedia filters
> OBJS-$(CONFIG_CONCAT_FILTER) += avf_concat.o
> +OBJS-$(CONFIG_PHASESCOPE_FILTER) += avf_phasescope.o
> OBJS-$(CONFIG_SHOWSPECTRUM_FILTER) += avf_showspectrum.o
> OBJS-$(CONFIG_SHOWWAVES_FILTER) += avf_showwaves.o
>
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 45a67e5..eaf0127 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -179,6 +179,7 @@ void avfilter_register_all(void)
>
> /* multimedia filters */
> REGISTER_FILTER(CONCAT, concat, avf);
> + REGISTER_FILTER(PHASESCOPE, phasescope, avf);
> REGISTER_FILTER(SHOWSPECTRUM, showspectrum, avf);
> REGISTER_FILTER(SHOWWAVES, showwaves, avf);
>
> diff --git a/libavfilter/avf_phasescope.c b/libavfilter/avf_phasescope.c
> new file mode 100644
> index 0000000..9b4e967
> --- /dev/null
> +++ b/libavfilter/avf_phasescope.c
> @@ -0,0 +1,214 @@
> +/*
> + * 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
> + */
> +
> +/**
> + * @file
> + * audio to video multimedia phase scope filter
> + */
> +
> +#include "libavutil/channel_layout.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/parseutils.h"
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "audio.h"
> +#include "video.h"
> +#include "internal.h"
> +
> +typedef struct PhaseScopeContext {
> + const AVClass *class;
> + AVFrame *outpicref;
> + int w, h;
> + int nb_samples;
> + int contrast;
> + int dissolve;
> + AVRational frame_rate;
> +} PhaseScopeContext;
> +
> +#define OFFSET(x) offsetof(PhaseScopeContext, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +
> +static const AVOption phasescope_options[] = {
> + { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
> + { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
> + { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="320x240"}, 0, 0, FLAGS },
> + { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="320x240"}, 0, 0, FLAGS },
> + { "contrast", "set contrast", OFFSET(contrast), AV_OPT_TYPE_INT, {.i64=255}, 1, 255, FLAGS },
> + { "dissolve", "set dissolve speed", OFFSET(dissolve), AV_OPT_TYPE_INT, {.i64=1}, 0, 255, FLAGS },
> + { NULL },
> +};
> +
> +AVFILTER_DEFINE_CLASS(phasescope);
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> + PhaseScopeContext *p = ctx->priv;
> + int err;
> +
> + p->class = &phasescope_class;
> + av_opt_set_defaults(p);
> +
> + if ((err = av_set_options_string(p, args, "=", ":")) < 0)
> + return err;
> +
> + return 0;
> +}
shorthand, as already noted by Clement.
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> + PhaseScopeContext *p = ctx->priv;
> +
> + av_frame_free(&p->outpicref);
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> + AVFilterFormats *formats = NULL;
> + AVFilterChannelLayouts *layout = NULL;
> + AVFilterLink *inlink = ctx->inputs[0];
> + AVFilterLink *outlink = ctx->outputs[0];
> + static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
> + static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
> +
> + formats = ff_make_format_list(sample_fmts);
> + if (!formats)
> + return AVERROR(ENOMEM);
> + ff_formats_ref(formats, &inlink->out_formats);
> +
> + ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
> + ff_channel_layouts_ref(layout, &inlink->out_channel_layouts);
> +
> + formats = ff_all_samplerates();
> + if (!formats)
> + return AVERROR(ENOMEM);
> + ff_formats_ref(formats, &inlink->out_samplerates);
> +
> + formats = ff_make_format_list(pix_fmts);
> + if (!formats)
> + return AVERROR(ENOMEM);
> + ff_formats_ref(formats, &outlink->in_formats);
> +
> + return 0;
> +}
> +
> +static int config_input(AVFilterLink *inlink)
> +{
> + AVFilterContext *ctx = inlink->dst;
> + PhaseScopeContext *p = ctx->priv;
> +
> + p->nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(p->frame_rate)) + 0.5);
> + inlink->partial_buf_size =
> + inlink->min_samples =
> + inlink->max_samples = p->nb_samples;
> +
> + return 0;
> +}
> +
> +static int config_output(AVFilterLink *outlink)
> +{
> + PhaseScopeContext *p = outlink->src->priv;
> +
> + outlink->w = p->w;
> + outlink->h = p->h;
> + outlink->sample_aspect_ratio = (AVRational){1,1};
> + outlink->frame_rate = p->frame_rate;
> + p->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
> + if (!p->outpicref)
> + return AVERROR(ENOMEM);
> + memset(p->outpicref->data[0], 0, outlink->h * p->outpicref->linesize[0]);
> + memset(p->outpicref->data[1], 128, outlink->h * p->outpicref->linesize[1]);
> + memset(p->outpicref->data[2], 128, outlink->h * p->outpicref->linesize[2]);
Note: setting the background color might be cool and almost for free
if you use ff_fill_rectangle(), not blocking of course.
> +
> + return 0;
> +}
> +
> +static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
> +{
> + AVFilterContext *ctx = inlink->dst;
> + AVFilterLink *outlink = ctx->outputs[0];
> + PhaseScopeContext *p = ctx->priv;
> + const int nb_samples = insamples->nb_samples;
> + const int linesize = p->outpicref->linesize[0];
> + const int hw = p->w / 2;
> + const int hh = p->h / 2;
> + int16_t *ptr = (int16_t *)insamples->data[0];
> + int i, j, chan, v, ret;
> + uint8_t *dst;
> +
> + p->outpicref->pts = insamples->pts;
> +
> + if (p->dissolve) {
> + for (i = 0; i < p->h; i++) {
> + for (j = 0; j < p->w; j++) {
> + dst = &p->outpicref->data[0][i * linesize + j];
> + v = *dst;
> + *dst = FFMAX(v - p->dissolve, 0);
*dst = FFMAX(*dst - p->dissolve, 0)?
> + }
> + }
> + }
> +
> + for (i = 0; i < nb_samples; i++) {
> + int x, y;
> + for (chan = 0; chan < 2; chan++, ptr += 2) {
> + x = ((ptr[1] - (int)ptr[0]) / (float)UINT16_MAX + 1) * hw;
> + y = ((ptr[0] + (int)ptr[1]) / (float)UINT16_MAX + 1) * hh;
> + dst = &p->outpicref->data[0][y * linesize + x];
Can you explain the mathematical principle, or provide a link for it?
> + *dst = FFMIN(*dst + p->contrast, 255);
> + }
> +
> + }
> +
> + av_frame_free(&insamples);
> +
> + ret = ff_filter_frame(outlink, av_frame_clone(p->outpicref));
> +
> + return ret;
return ff_filter_frame(...);
> +}
> +
> +static const AVFilterPad phasescope_inputs[] = {
> + {
> + .name = "default",
> + .type = AVMEDIA_TYPE_AUDIO,
> + .config_props = config_input,
> + .filter_frame = filter_frame,
> + },
> + { NULL }
> +};
> +
> +static const AVFilterPad phasescope_outputs[] = {
> + {
> + .name = "default",
> + .type = AVMEDIA_TYPE_VIDEO,
> + .config_props = config_output,
> + },
> + { NULL }
> +};
> +
> +AVFilter avfilter_avf_phasescope = {
> + .name = "phasescope",
> + .description = NULL_IF_CONFIG_SMALL("Audio phase scope."),
"Display audio phase scope."
[...]
Nice and interesting work.
--
FFmpeg = Fierce Fancy Multipurpose Peaceful Elastic Game
More information about the ffmpeg-devel
mailing list