From 9c943509bbe5b4119513462a678d49e0d6bba1f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= Date: Mon, 14 May 2012 19:03:19 +0200 Subject: [PATCH] lavfi/WIP: vf geq. --- configure | 1 + libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_geq.c | 211 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 libavfilter/vf_geq.c diff --git a/configure b/configure index 115e8b1..14c1570 100755 --- a/configure +++ b/configure @@ -1939,6 +1939,7 @@ frei0r_filter_deps="frei0r dlopen" frei0r_filter_extralibs='$ldl' frei0r_src_filter_deps="frei0r dlopen" frei0r_src_filter_extralibs='$ldl' +geq_filter_deps="gpl" hqdn3d_filter_deps="gpl" movie_filter_deps="avcodec avformat" mp_filter_deps="gpl avcodec swscale postproc inline_asm" diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 5fbfe17..0d9e9e2 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -105,6 +105,7 @@ OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o OBJS-$(CONFIG_FRAMESTEP_FILTER) += vf_framestep.o OBJS-$(CONFIG_FPS_FILTER) += vf_fps.o OBJS-$(CONFIG_FREI0R_FILTER) += vf_frei0r.o +OBJS-$(CONFIG_GEQ_FILTER) += vf_geq.o OBJS-$(CONFIG_GRADFUN_FILTER) += vf_gradfun.o OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o OBJS-$(CONFIG_HQDN3D_FILTER) += vf_hqdn3d.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index fef40e9..88e8d68 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -97,6 +97,7 @@ void avfilter_register_all(void) REGISTER_FILTER (FPS, fps, vf); REGISTER_FILTER (FRAMESTEP, framestep, vf); REGISTER_FILTER (FREI0R, frei0r, vf); + REGISTER_FILTER (GEQ, geq, vf); REGISTER_FILTER (GRADFUN, gradfun, vf); REGISTER_FILTER (HFLIP, hflip, vf); REGISTER_FILTER (HQDN3D, hqdn3d, vf); diff --git a/libavfilter/vf_geq.c b/libavfilter/vf_geq.c new file mode 100644 index 0000000..6c4ff8c --- /dev/null +++ b/libavfilter/vf_geq.c @@ -0,0 +1,211 @@ +/* + * Copyright (C) 2006 Michael Niedermayer + * Copyright (C) 2012 Clément Bœsch + * + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** + * @file + * XXX + */ + +#include "libavcodec/avcodec.h" +#include "libavutil/eval.h" +#include "libavutil/avstring.h" +#include "libavutil/pixdesc.h" +#include "internal.h" +#include "video.h" + +typedef struct { + AVExpr *e[3]; ///< expressions for each plane + int framenum; ///< frame counter + AVFilterBufferRef *picref; ///< current input buffer + int hsub, vsub; +} GEQContext; + +static inline double getpix(void *priv, double x, double y, int plane) +{ + int xi, yi; + GEQContext *geq = priv; + AVFilterBufferRef *picref = geq->picref; + const uint8_t *src = picref->data[plane]; + const int linesize = picref->linesize[plane]; + const int w = picref->video->w >> (plane ? geq->hsub : 0); + const int h = picref->video->h >> (plane ? geq->vsub : 0); + + xi = x = FFMIN(FFMAX(x, 0), w - 1); + yi = y = FFMIN(FFMAX(y, 0), h - 1); + + x -= xi; + y -= yi; + + return (1-y)*((1-x)*src[xi + yi * linesize] + x*src[xi + 1 + yi * linesize]) + + y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]); +} + +//TODO: cubic interpolate +//TODO: keep the last few frames +static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); } +static double cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); } +static double cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); } + +static const char *const var_names[] = { "X", "Y", "W", "H", "N", "SW", "SH", NULL }; +enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_VARS_NB }; + +static av_cold int geq_init(AVFilterContext *ctx, const char *args) +{ + GEQContext *geq = ctx->priv; + char *args1 = av_strdup(args); + char *bufptr = NULL; + char *arg = args1; + const char *expr[3] = {0}; + int plane, ret = 0; + + if (!args1) { + ret = AVERROR(ENOMEM); + goto end; + } + + for (plane = 0; plane < 3; plane++, arg = NULL) { + static double (*p[])(void *, double, double) = { lum, cb, cr }; + static const char *const func2_names[] = { "lum", "cb", "cr", "p", NULL }; + double (*func2[])(void *, double, double) = { lum, cb, cr, p[plane], NULL }; + + expr[plane] = av_strtok(arg, ":", &bufptr); + if (!expr[plane]) { + if (plane == 0) { + av_log(ctx, AV_LOG_ERROR, "At least one expression must be specified\n"); + ret = AVERROR(EINVAL); + goto end; + } + expr[plane] = expr[plane - 1]; + } + ret = av_expr_parse(&geq->e[plane], expr[plane], var_names, NULL, NULL, + func2_names, func2, 0, ctx); + if (ret < 0) + goto end; + } + +end: + av_free(args1); + return ret; +} + +static int geq_query_formats(AVFilterContext *ctx) +{ + static const enum PixelFormat pix_fmts[] = { + AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, + AV_PIX_FMT_YUVA420P, + AV_PIX_FMT_NONE + }; + ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); + return 0; +} + +static int geq_config_props(AVFilterLink *inlink) +{ + GEQContext *geq = inlink->dst->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + + geq->hsub = desc->log2_chroma_w; + geq->vsub = desc->log2_chroma_h; + return 0; +} + +static int geq_end_frame(AVFilterLink *inlink) +{ + int ret, plane; + GEQContext *geq = inlink->dst->priv; + AVFilterLink *outlink = inlink->dst->outputs[0]; + AVFilterBufferRef *outpicref = outlink->out_buf; + double values[VAR_VARS_NB] = { + [VAR_N] = geq->framenum++, + }; + + geq->picref = inlink->cur_buf; + + for (plane = 0; plane < 3; plane++) { + int x, y; + uint8_t *dst = outpicref->data[plane]; + const int linesize = outpicref->linesize[plane]; + const int w = inlink->w >> (plane ? geq->hsub : 0); + const int h = inlink->h >> (plane ? geq->vsub : 0); + + values[VAR_W] = w; + values[VAR_H] = h; + values[VAR_SW] = w / (double)inlink->w; + values[VAR_SH] = h / (double)inlink->h; + + for (y = 0; y < h; y++) { + values[VAR_Y] = y; + for (x = 0; x < w; x++) { + values[VAR_X] = x; + dst[x] = av_expr_eval(geq->e[plane], values, geq); + } + dst += linesize; + } + } + + if ((ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 || + (ret = ff_end_frame(outlink)) < 0) + return ret; + return 0; +} + +static av_cold void geq_uninit(AVFilterContext *ctx) +{ + int i; + GEQContext *geq = ctx->priv; + + for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++) + av_expr_free(geq->e[i]); +} + +static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; } + +static const AVFilterPad avfilter_vf_geq_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .draw_slice = null_draw_slice, + .config_props = geq_config_props, + .end_frame = geq_end_frame, + .min_perms = /* AV_PERM_WRITE | */ AV_PERM_READ, + }, + { NULL } +}; + +static const AVFilterPad avfilter_vf_geq_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFilter avfilter_vf_geq = { + .name = "geq", + .description = NULL_IF_CONFIG_SMALL("GEQ"), + .priv_size = sizeof(GEQContext), + .init = geq_init, + .uninit = geq_uninit, + .query_formats = geq_query_formats, + .inputs = avfilter_vf_geq_inputs, + .outputs = avfilter_vf_geq_outputs, +}; -- 1.7.10.4