Signed-off-by: Paul B Mahol <one...@gmail.com> --- doc/filters.texi | 28 +++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_iir.c | 524 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 554 insertions(+) create mode 100644 libavfilter/vf_iir.c
diff --git a/doc/filters.texi b/doc/filters.texi index 66c0f87e47..635179edb9 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -13735,6 +13735,34 @@ further computations. This allows inserting the idet filter as a low computation method to clean up the interlaced flag @end table +@section iir +Apply 2D Infinite Impulse Response filter to input video stream. + +The filter accepts the following options: + +@table @option +@item hn +@item vn +Set horizontal and vertical numerator coefficients of transfer function. + +@item hd +@item vd +Set horizontal and vertical denominator coefficients of transfer function. + +@item planes +Set which planes to filter. Default is all. Allowed range is from 0 to 15. +@end table + +@subsection Examples + +@itemize +@item +Apply lowpass variant: +@example +iir=hn=0.001268 0.002536 0.001268:hd=1.050338 -1.994927 0.949662:vn=0.001268 0.002536 0.001268:vd=1.050338 -1.994927 0.949662 +@end example +@end itemize + @section il Deinterleave or interleave fields. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 49c0c8342b..772971521c 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -303,6 +303,7 @@ OBJS-$(CONFIG_HWUPLOAD_FILTER) += vf_hwupload.o OBJS-$(CONFIG_HYSTERESIS_FILTER) += vf_hysteresis.o framesync.o OBJS-$(CONFIG_IDENTITY_FILTER) += vf_identity.o OBJS-$(CONFIG_IDET_FILTER) += vf_idet.o +OBJS-$(CONFIG_IIR_FILTER) += vf_iir.o OBJS-$(CONFIG_IL_FILTER) += vf_il.o OBJS-$(CONFIG_INFLATE_FILTER) += vf_neighbor.o OBJS-$(CONFIG_INTERLACE_FILTER) += vf_tinterlace.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index ae74f9c891..e0276eb98c 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -288,6 +288,7 @@ extern const AVFilter ff_vf_hwupload_cuda; extern const AVFilter ff_vf_hysteresis; extern const AVFilter ff_vf_identity; extern const AVFilter ff_vf_idet; +extern const AVFilter ff_vf_iir; extern const AVFilter ff_vf_il; extern const AVFilter ff_vf_inflate; extern const AVFilter ff_vf_interlace; diff --git a/libavfilter/vf_iir.c b/libavfilter/vf_iir.c new file mode 100644 index 0000000000..8b9dfcc4b4 --- /dev/null +++ b/libavfilter/vf_iir.c @@ -0,0 +1,524 @@ +/* + * Copyright (c) 2021 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/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavutil/avstring.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct IIRContext { + const AVClass *class; + + char *h_num_str; + char *h_den_str; + char *v_num_str; + char *v_den_str; + + int h_nb_num; + int h_nb_den; + int v_nb_num; + int v_nb_den; + + float *hnum; + float *hden; + float *vnum; + float *vden; + + float **honum; + float **hoden; + float **vonum; + float **voden; + + int depth; + int flt; + int planes; + int nb_planes; + int nb_threads; + + float *in, *out; + + int linesize[4]; + int planewidth[4]; + int planeheight[4]; +} IIRContext; + +#define OFFSET(x) offsetof(IIRContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption iir_options[] = { + { "hn", "set horizontal numerator", OFFSET(h_num_str), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, + { "hd", "set horizotnal denominator", OFFSET(h_den_str), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, + { "vn", "set vertical numerator", OFFSET(v_num_str), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, + { "vd", "set vertical denominator", OFFSET(v_den_str), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, + { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(iir); + +typedef struct ThreadData { + int height; + int width; + + float *dst; + float *src; + int dst_linesize; + int src_linesize; +} ThreadData; + +static void horiz_slice(float *dst, float *src, + int dst_linesize, int src_linesize, + int nb_num, int nb_den, + const float *num, const float *den, + float *onum, float *oden, + int width, int height) +{ + for (int y = 0; y < height; y++) { + for (int i = 0; i < nb_num; i++) + onum[i] = src[0]; + for (int i = 0; i < nb_den; i++) + oden[i] = src[0]; + + for (int x = 0; x < width; x++) { + float sample = 0.f; + + memmove(&onum[1], &onum[0], (nb_num - 1) * sizeof(*onum)); + memmove(&oden[1], &oden[0], (nb_den - 1) * sizeof(*oden)); + onum[0] = src[x]; + for (int i = 0; i < nb_num; i++) + sample += num[i] * onum[i]; + + for (int i = 1; i < nb_den; i++) + sample -= den[i] * oden[i]; + + dst[x] = oden[0] = sample; + } + + dst += dst_linesize; + src += src_linesize; + } +} + +static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + IIRContext *s = ctx->priv; + ThreadData *td = arg; + const int height = td->height; + const int width = td->width; + const int slice_start = (height * jobnr ) / nb_jobs; + const int slice_end = (height * (jobnr+1)) / nb_jobs; + + horiz_slice(td->dst + td->dst_linesize * slice_start, + td->src + td->src_linesize * slice_start, + td->dst_linesize, td->src_linesize, + s->h_nb_num, s->h_nb_den, + s->hnum, s->hden, s->honum[jobnr], s->hoden[jobnr], + width, slice_end - slice_start); + + return 0; +} + +static void do_vertical_columns(float *pdst, float *psrc, + int dst_linesize, int src_linesize, + int nb_num, int nb_den, + const float *num, const float *den, + float *onum, float *oden, + int width, int height) +{ + for (int x = 0; x < width; x++) { + float *src = psrc; + float *dst = pdst; + + for (int i = 0; i < nb_num; i++) + onum[i] = src[x]; + for (int i = 0; i < nb_den; i++) + oden[i] = src[x]; + + for (int y = 0; y < height; y++) { + float sample = 0.f; + + memmove(&onum[1], &onum[0], (nb_num - 1) * sizeof(*onum)); + memmove(&oden[1], &oden[0], (nb_den - 1) * sizeof(*oden)); + onum[0] = src[x]; + for (int i = 0; i < nb_num; i++) + sample += num[i] * onum[i]; + + for (int i = 1; i < nb_den; i++) + sample -= den[i] * oden[i]; + + dst[x] = oden[0] = sample; + + dst += dst_linesize; + src += src_linesize; + } + } +} + +static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + IIRContext *s = ctx->priv; + ThreadData *td = arg; + const int height = td->height; + const int width = td->width; + const int slice_start = (width * jobnr ) / nb_jobs; + const int slice_end = (width * (jobnr+1)) / nb_jobs; + + do_vertical_columns(td->dst + slice_start, + td->src + slice_start, + td->dst_linesize, td->src_linesize, + s->v_nb_num, s->v_nb_den, + s->vnum, s->vden, s->vonum[jobnr], s->voden[jobnr], + slice_end - slice_start, height); + + return 0; +} + +static void iir2d(AVFilterContext *ctx, float *out, float *in, + int out_linesize, int in_linesize, + int width, int height) +{ + IIRContext *s = ctx->priv; + ThreadData td; + + td.width = width; + td.height = height; + td.dst = out; + td.src = in; + td.dst_linesize = out_linesize; + td.src_linesize = in_linesize; + ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, s->nb_threads)); + td.src = out; + td.src_linesize = out_linesize; + ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, s->nb_threads)); +} + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, + AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, + AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, + AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, + AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, + AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, + AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, + AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, + AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, + AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12, + AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, + AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, + AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, + AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16, + AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32, + AV_PIX_FMT_GRAYF32, + AV_PIX_FMT_NONE + }; + + return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); +} + +static void count_coefficients(char *item_str, int *nb_items) +{ + char *p; + + if (!item_str) + return; + + *nb_items = 1; + for (p = item_str; *p; p++) { + if (*p == ' ') + (*nb_items)++; + } +} + +static int read_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, float *dst) +{ + char *p, *arg, *old_str, *saveptr = NULL; + int i; + + p = old_str = av_strdup(item_str); + if (!p) + return AVERROR(ENOMEM); + + for (i = 0; i < nb_items; i++) { + if (!(arg = av_strtok(p, " ", &saveptr))) + break; + + p = NULL; + if (av_sscanf(arg, "%f", &dst[i]) != 1) { + av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg); + av_freep(&old_str); + return AVERROR(EINVAL); + } + } + + av_freep(&old_str); + + return 0; +} + +static void normalize_coefficients(float *num, int nb_num, float *den, int nb_den) +{ + float div = den[0]; + + for (int i = 0; i < nb_num; i++) + num[i] /= div; + + for (int i = 0; i < nb_den; i++) + den[i] /= div; +} + +static int config_input(AVFilterLink *inlink) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + AVFilterContext *ctx = inlink->dst; + IIRContext *s = ctx->priv; + int ret; + + if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0) + return ret; + + s->depth = desc->comp[0].depth; + s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT); + + s->in = av_malloc_array(FFALIGN(inlink->w, 64), FFALIGN(inlink->h, 64) * sizeof(*s->in)); + s->out = av_malloc_array(FFALIGN(inlink->w, 64), FFALIGN(inlink->h, 64) * sizeof(*s->out)); + if (!s->in || !s->out) + return AVERROR(ENOMEM); + + s->nb_threads = ff_filter_get_nb_threads(ctx); + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->planewidth[0] = s->planewidth[3] = inlink->w; + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->planeheight[0] = s->planeheight[3] = inlink->h; + + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + + count_coefficients(s->h_num_str, &s->h_nb_num); + count_coefficients(s->h_den_str, &s->h_nb_den); + count_coefficients(s->v_num_str, &s->v_nb_num); + count_coefficients(s->v_den_str, &s->v_nb_den); + + if (!s->h_nb_num || !s->h_nb_den || + !s->v_nb_num || !s->v_nb_den) + return AVERROR(EINVAL); + + s->hnum = av_calloc(s->h_nb_num, sizeof(*s->hnum)); + s->hden = av_calloc(s->h_nb_den, sizeof(*s->hden)); + s->honum = av_calloc(s->nb_threads, sizeof(*s->honum)); + s->hoden = av_calloc(s->nb_threads, sizeof(*s->hoden)); + s->vnum = av_calloc(s->h_nb_num, sizeof(*s->vnum)); + s->vden = av_calloc(s->h_nb_den, sizeof(*s->vden)); + s->vonum = av_calloc(s->nb_threads, sizeof(*s->vonum)); + s->voden = av_calloc(s->nb_threads, sizeof(*s->voden)); + if (!s->hnum || !s->hden || !s->honum || !s->hoden || + !s->vnum || !s->vden || !s->vonum || !s->voden) + return AVERROR(ENOMEM); + + for (int i = 0; i < s->nb_threads; i++) { + s->honum[i] = av_calloc(s->h_nb_num, sizeof(**s->honum)); + s->hoden[i] = av_calloc(s->h_nb_den, sizeof(**s->hoden)); + s->vonum[i] = av_calloc(s->v_nb_num, sizeof(**s->vonum)); + s->voden[i] = av_calloc(s->v_nb_den, sizeof(**s->voden)); + if (!s->honum[i] || !s->hoden[i] || + !s->vonum[i] || !s->voden[i]) + return AVERROR(ENOMEM); + } + + ret = read_coefficients(ctx, s->h_num_str, s->h_nb_num, s->hnum); + if (ret < 0) + return ret; + ret = read_coefficients(ctx, s->h_den_str, s->h_nb_den, s->hden); + if (ret < 0) + return ret; + ret = read_coefficients(ctx, s->v_num_str, s->v_nb_num, s->vnum); + if (ret < 0) + return ret; + ret = read_coefficients(ctx, s->v_den_str, s->v_nb_den, s->vden); + if (ret < 0) + return ret; + + normalize_coefficients(s->hnum, s->h_nb_num, s->hden, s->h_nb_den); + normalize_coefficients(s->vnum, s->v_nb_num, s->vden, s->v_nb_den); + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + IIRContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out; + int plane; + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); + + for (plane = 0; plane < s->nb_planes; plane++) { + const int height = s->planeheight[plane]; + const int width = s->planewidth[plane]; + const uint8_t *src = in->data[plane]; + const uint16_t *src16 = (const uint16_t *)in->data[plane]; + uint8_t *dst = out->data[plane]; + uint16_t *dst16 = (uint16_t *)out->data[plane]; + int fin_linesize; + int fout_linesize; + float *fout, *fin; + + if (!(s->planes & (1 << plane))) { + av_image_copy_plane(out->data[plane], out->linesize[plane], + in->data[plane], in->linesize[plane], + s->linesize[plane], height); + continue; + } + + if (s->flt) { + fin = (float *)in->data[plane]; + fout = (float *)out->data[plane]; + fin_linesize = in->linesize[plane] / 4; + fout_linesize = out->linesize[plane] / 4; + } else { + float *bptr = s->in; + + if (s->depth == 8) { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + bptr[x] = src[x]; + } + bptr += width; + src += in->linesize[plane]; + } + } else { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + bptr[x] = src16[x]; + } + bptr += width; + src16 += in->linesize[plane] / 2; + } + } + + fin = s->in; + fout = s->out; + fin_linesize = width; + fout_linesize = width; + } + + iir2d(ctx, fout, fin, fout_linesize, fin_linesize, + s->planewidth[plane], s->planeheight[plane]); + + if (!s->flt) { + float *bptr = s->out; + + if (s->depth == 8) { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + dst[x] = bptr[x]; + } + bptr += width; + dst += out->linesize[plane]; + } + } else { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + dst16[x] = bptr[x]; + } + bptr += width; + dst16 += out->linesize[plane] / 2; + } + } + } + } + + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + IIRContext *s = ctx->priv; + + av_freep(&s->in); + av_freep(&s->out); + + av_freep(&s->hnum); + av_freep(&s->hden); + av_freep(&s->vnum); + av_freep(&s->vden); + + for (int i = 0; i < s->nb_threads; i++) { + if (s->honum) + av_freep(&s->honum[i]); + if (s->hoden) + av_freep(&s->hoden[i]); + if (s->vonum) + av_freep(&s->vonum[i]); + if (s->voden) + av_freep(&s->voden[i]); + } + + av_freep(&s->honum); + av_freep(&s->hoden); + av_freep(&s->vonum); + av_freep(&s->voden); +} + +static const AVFilterPad iir_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad iir_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +const AVFilter ff_vf_iir = { + .name = "iir", + .description = NULL_IF_CONFIG_SMALL("Apply 2D Infinite Impulse Response filter with supplied coefficients."), + .priv_size = sizeof(IIRContext), + .priv_class = &iir_class, + .uninit = uninit, + .query_formats = query_formats, + .inputs = iir_inputs, + .outputs = iir_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, +}; -- 2.17.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-requ...@ffmpeg.org with subject "unsubscribe".