From patchwork Sat Jan 28 23:37:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 2353 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp856704vsb; Sat, 28 Jan 2017 15:38:04 -0800 (PST) X-Received: by 10.223.135.163 with SMTP id b32mr12816542wrb.184.1485646684738; Sat, 28 Jan 2017 15:38:04 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 79si7903281wmy.138.2017.01.28.15.38.04; Sat, 28 Jan 2017 15:38:04 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4ED3C68A930; Sun, 29 Jan 2017 01:38:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6C92368A8EE for ; Sun, 29 Jan 2017 01:37:53 +0200 (EET) Received: by mail-wm0-f66.google.com with SMTP id v77so6637760wmv.0 for ; Sat, 28 Jan 2017 15:37:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=a6c10uvBmKMrzZCcaZtuiQ16QK4EGpYawbYivmUKSac=; b=fgrxqIzhm5FuTouvLelyRFYtBCPdjzSdGCBkBQpY+4xLRCVWMHtVTw2wVW3EFqvn6i 0wDmRbi6lTcV+aC0di7w/YbjkBu5SB1NME/9FgoNtdY52MEZIxP5AGUr7auCf4IBj8jN nQU3wtVLgKWdwftum1w5xjk1mlI7kLoVFIvWr4QN0lWceDg/63nqM1CoT909aYTkjnJk RWXWELRL3EZnb/+DicRdRGWz3NmJTKkUy9F4ExM5XQ0HzFJwfFfZ/3qLAmQT0J+wL+1q uDVQD0cBbFmE/2lCBU1z96PAKF1fuTktA3cqv6lBdR3/52svpxOm6VKgJz2NauDIRmFy AqyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=a6c10uvBmKMrzZCcaZtuiQ16QK4EGpYawbYivmUKSac=; b=SEkSBfkSiivnvlm2RW+ZFSmUT+r/iY+zD6PtmfVWXdK0QfLTeaile3Qt5eI7RhaCRh HQwlHi2ylSffzgVwdEHXVSINGNRxk+eNJDPuaorWAjkCDOK7MweGGZZD1IZBe7GOdOm8 xEKwsvWQe7EpB5DENrkSq0XQZNZ9OyG9T++lBDkAWTUUGWFT0rDVN1ekqO/Bpdb7Xzzd 5kfEKDr4uZ2fAUoc0zUxWzlCHRfgsOOkS3Q+LZx3zS5wzwGZUVVMC32zOAQUE/r1h/Wo yf61xA5IjuLqcwr1GK73/ZoMhSRfM4cprArjV6ZRN4Qydv5VzYnZqYQsxYP/l2JCvtn4 QUeA== X-Gm-Message-State: AIkVDXJu8JXgvb5ZCRQspU2ibpTOLGOc7e7upbXaULhgK+zqXYIi7ttD2zdi1x86hggHBQ== X-Received: by 10.28.139.74 with SMTP id n71mr8962971wmd.139.1485646674441; Sat, 28 Jan 2017 15:37:54 -0800 (PST) Received: from localhost.localdomain ([37.244.246.24]) by smtp.gmail.com with ESMTPSA id o2sm15010749wra.42.2017.01.28.15.37.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 28 Jan 2017 15:37:53 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 29 Jan 2017 00:37:33 +0100 Message-Id: <20170128233733.2231-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avfilter: add mkmap source filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 39 ++++++++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vsrc_mkmap.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 204 insertions(+) create mode 100644 libavfilter/vsrc_mkmap.c diff --git a/doc/filters.texi b/doc/filters.texi index cd1aaab..d1790ac 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -15248,6 +15248,45 @@ Set the initial y position. Must be a floating point value between -100 and 100. Default value is -0.131825904205311970493132056385139. @end table +@section mkmap + +Generate map of pixels for one of axis, either X or Y. Mainly useful +as providing input frames for remap filter. + +This source accepts the following options: + +@table @option + +@item size, s +Set the size of the output video. For the syntax of this option, check the +@ref{video size syntax,,"Video size" section in the ffmpeg-utils manual,ffmpeg-utils}. + +@item rate, r +Set the video rate, that is the number of frames generated per second. +Default is 25. + +@item expr, e +Set map expression. + +The expressions can contain the following constants and functions: + +@table @option +@item W +@item H +The output width and height. + +@item X +@item Y +The current column and row. + +@item N +The current frame number, starting from 0. + +@item T +Time of the current frame, expressed in seconds. +@end table +@end table + @section mptestsrc Generate various test patterns, as generated by the MPlayer test filter. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 91621d9..68e1923 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -321,6 +321,7 @@ OBJS-$(CONFIG_FREI0R_SRC_FILTER) += vf_frei0r.o OBJS-$(CONFIG_HALDCLUTSRC_FILTER) += vsrc_testsrc.o OBJS-$(CONFIG_LIFE_FILTER) += vsrc_life.o OBJS-$(CONFIG_MANDELBROT_FILTER) += vsrc_mandelbrot.o +OBJS-$(CONFIG_MKMAP_FILTER) += vsrc_mkmap.o OBJS-$(CONFIG_MPTESTSRC_FILTER) += vsrc_mptestsrc.o OBJS-$(CONFIG_NULLSRC_FILTER) += vsrc_testsrc.o OBJS-$(CONFIG_RGBTESTSRC_FILTER) += vsrc_testsrc.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index d71d14a..1d8ca94 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -336,6 +336,7 @@ void avfilter_register_all(void) REGISTER_FILTER(HALDCLUTSRC, haldclutsrc, vsrc); REGISTER_FILTER(LIFE, life, vsrc); REGISTER_FILTER(MANDELBROT, mandelbrot, vsrc); + REGISTER_FILTER(MKMAP, mkmap, vsrc); REGISTER_FILTER(MPTESTSRC, mptestsrc, vsrc); REGISTER_FILTER(NULLSRC, nullsrc, vsrc); REGISTER_FILTER(RGBTESTSRC, rgbtestsrc, vsrc); diff --git a/libavfilter/vsrc_mkmap.c b/libavfilter/vsrc_mkmap.c new file mode 100644 index 0000000..e699c5f --- /dev/null +++ b/libavfilter/vsrc_mkmap.c @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2017 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/eval.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct MkMapContext { + const AVClass *class; + + int w, h; + AVRational frame_rate; + char *expr_str; + + AVExpr *expr; + int64_t pts; +} MkMapContext; + +static const char *const var_names[] = { + "W", "H", "X", "Y", "N", "T", + NULL +}; + +enum var_name { + VAR_W, VAR_H, VAR_X, VAR_Y, VAR_N, VAR_T, + VAR_VARS_NB +}; + +#define OFFSET(x) offsetof(MkMapContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption mkmap_options[] = { + { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, FLAGS }, + { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, FLAGS }, + { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS }, + { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS }, + { "expr", "set map expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = "Y*W+X" }, 0, 0, FLAGS }, + { "e", "set map expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = "Y*W+X" }, 0, 0, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(mkmap); + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_GRAY16, + AV_PIX_FMT_NONE + }; + + AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); + if (!fmts_list) + return AVERROR(ENOMEM); + return ff_set_common_formats(ctx, fmts_list); +} + +static int mkmap(MkMapContext *s, AVFrame *out, AVFilterLink *outlink) +{ + double var_values[VAR_VARS_NB]; + uint16_t *dst; + int ret, x, y; + + av_expr_free(s->expr); + s->expr = NULL; + ret = av_expr_parse(&s->expr, s->expr_str, + var_names, NULL, NULL, NULL, NULL, 0, s); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, + "Error when parsing the expression '%s'.\n", + s->expr_str); + return AVERROR(EINVAL); + } + + var_values[VAR_N] = outlink->frame_count_out, + var_values[VAR_T] = out->pts * av_q2d(outlink->time_base), + var_values[VAR_W] = out->width; + var_values[VAR_H] = out->height; + + dst = (uint16_t *)out->data[0]; + for (y = 0; y < out->height; y++) { + var_values[VAR_Y] = y; + for (x = 0; x < out->width; x++) { + var_values[VAR_X] = x; + dst[x] = av_expr_eval(s->expr, var_values, s); + } + dst += out->linesize[0] / 2; + } + out->pts++; + + return ret; +} + +static int config_props(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + MkMapContext *s = ctx->priv; + + outlink->w = s->w; + outlink->h = s->h; + outlink->time_base = av_inv_q(s->frame_rate); + + return 0; +} + +static int request_frame(AVFilterLink *outlink) +{ + MkMapContext *s = outlink->src->priv; + AVFrame *out = ff_get_video_buffer(outlink, s->w, s->h); + if (!out) + return AVERROR(ENOMEM); + out->sample_aspect_ratio = (AVRational) {1, 1}; + mkmap(s, out, outlink); + return ff_filter_frame(outlink, out); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + MkMapContext *s = ctx->priv; + + av_expr_free(s->expr); + s->expr = NULL; +} + +static const AVFilterPad mkmap_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .request_frame = request_frame, + .config_props = config_props, + }, + { NULL } +}; + +AVFilter ff_vsrc_mkmap = { + .name = "mkmap", + .description = NULL_IF_CONFIG_SMALL("Make mapping for pixels."), + .priv_size = sizeof(MkMapContext), + .priv_class = &mkmap_class, + .uninit = uninit, + .query_formats = query_formats, + .inputs = NULL, + .outputs = mkmap_outputs, +};