From patchwork Thu Sep 7 19:29: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: 5041 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.15.201 with SMTP id 70csp583820jao; Thu, 7 Sep 2017 12:35:06 -0700 (PDT) X-Received: by 10.28.131.197 with SMTP id f188mr8128wmd.71.1504812906073; Thu, 07 Sep 2017 12:35:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1504812906; cv=none; d=google.com; s=arc-20160816; b=CZvxRVOpYGGqP347WnQbyd84OjfpwY9Dja8/orcQJntICt1qCmbOql62dQz0FTkucK nf3QcUPQmKNJcRREj5VIHQZSCQt1FkdsKMJaRM59QGbl9JQc9QjXQ8oQKkkuUO/1crHn D+lsDBI0z0BDvTRWLXXbJqJoBDPwZPrJfh5Pk1EP8BtbJdH4HJxIDC5TTdYfoWPpR+Cc Q0LvxRzXc1GnU03WxeAFuTotTwuiRGt+lTdZQbCuaJkcWVBuZieGzv7TZQRe3e+KbvIh bV0xN1KZxtTmnQEeXCYylz9zoNPpmQJVNCqEX/rXehp2dZKtfx+8yqgNGGlkmKYPba8g axkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=UuoqB5dY/h1Bz9kwkAk5ax/e31Lnhm6pnf3hqqfvjGo=; b=N8/N7Fn9muue8IU/lrF93sxJvPIp45mLHHQQENDSXeuzRF1t8FNABdVrWoZVHH4hQX 6z4xrWhPeygkoJzUXBzDWoGZVjJSzBelyqoD+QhCAE4hDY6I240uxSnbQsRfsKGRphIO 5FYMAHLSsNMAEzIXPQZo+IwBqrLpArPqgAOExHlKPZWkddWVuxxM0KLtBVU0ZgovmGZr JkFFOkag17Sk6k5Zz6hezaJkJ2B8yZFuB15mx4mQITG4zgnhAVlihmshGxyu6j4c3zVl sJy07BD8I2TBpIRuOwddcTZL3k4Njom6O3fsqEPKVv3ZluhMsMyCKmdxv+kviNnHuEeK X3sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=vAHafn1v; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u68si37885wmd.61.2017.09.07.12.35.05; Thu, 07 Sep 2017 12:35:06 -0700 (PDT) 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 header.s=20161025 header.b=vAHafn1v; 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 D29E5689DC3; Thu, 7 Sep 2017 22:35:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A84AE689DB2 for ; Thu, 7 Sep 2017 22:34:53 +0300 (EEST) Received: by mail-wm0-f68.google.com with SMTP id l19so355057wmi.1 for ; Thu, 07 Sep 2017 12:34:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=m/PxQ5bl2I9g+L7b0UP7sen3sjsa3pwlzS10C24s5yw=; b=vAHafn1vxfX61j4QEdqjXndL3b0qfBaEMcNv4LT3y0yBW95D/qR9NHaeGaHcCg4rVC 7GfYFCD1ZWOf+QBleWnECOaf94gWJtjeRQ94VFT6hEtNKpO7zXoYP+pg1SPvbSxs2+a7 KIWu/6m/TM+AOORtG7iU3FmbK4rOW83AW9/Y24VPQeTTvy0EuFYWNJsr81eTP3H1SoIe nLXCVd3V6lTCgFbJn907UJvYJ9Sx/lZPD5UKclk/s1FmvLPDyXi0JSTyf0hRwFxUBCwy e6RJcFZtkYL6txzJQQbSqoSYlDRTu1u8Um/tWkk3GwgNZuPBC/H27kwmg62IbLnnMTwa VVDg== 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=m/PxQ5bl2I9g+L7b0UP7sen3sjsa3pwlzS10C24s5yw=; b=PHTSf8sfdjUJ2KCf8bOQtZ2JD+M9xFWiF9uokJaK7KPBfO7RqqpBmvrWYEMeKcjEWA zVTHpfmX7VTQaZ3FTmxGcf11MHrdDLZo4Vca9VdJnCi7n72U4bI+Vubt6AB+qt+qcaj7 g6TywNb8tjpTCK1YsSTLLbhtLq5PkWDt4sJW3Nsu9UDnrw4C4YGw0KFoFXmIg4KHAbtT vRANDVPTZlZIqpU9OAtCsXV5ZPcKHC+Y7++Xye/ZmvNTLF+hOt65Qxc/sa7CAMlkoX8m EfP9nENWjdwG1QosCEK1CSMsOvfoZ6Q8vNO9R5cXCv8VbOmWg7lEd88GtxJDt6i8o94e 8zLw== X-Gm-Message-State: AHPjjUjgc76L5Twy4p7sGrSPeICnuf+HTWnCnDcvr7Yt6T/C6zFnE4+h b3C2tkgk0y1Bo6Us X-Google-Smtp-Source: AOwi7QD2fe0KesQ3dIBk513q/iEGbAA7dDbExEiCSLjCtyGbDfvElrAUknov1ZIxZUeObKaAc2vGlQ== X-Received: by 10.28.152.70 with SMTP id a67mr6649wme.132.1504812585670; Thu, 07 Sep 2017 12:29:45 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id z51sm96128wrz.80.2017.09.07.12.29.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Sep 2017 12:29:45 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Thu, 7 Sep 2017 21:29:33 +0200 Message-Id: <20170907192933.18523-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avfilter: add starfield video source 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 --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vsrc_starfield.c | 191 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 libavfilter/vsrc_starfield.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 1e460ab..4cc99f0 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -352,6 +352,7 @@ OBJS-$(CONFIG_NULLSRC_FILTER) += vsrc_testsrc.o OBJS-$(CONFIG_RGBTESTSRC_FILTER) += vsrc_testsrc.o OBJS-$(CONFIG_SMPTEBARS_FILTER) += vsrc_testsrc.o OBJS-$(CONFIG_SMPTEHDBARS_FILTER) += vsrc_testsrc.o +OBJS-$(CONFIG_STARFIELD_FILTER) += vsrc_starfield.o OBJS-$(CONFIG_TESTSRC_FILTER) += vsrc_testsrc.o OBJS-$(CONFIG_TESTSRC2_FILTER) += vsrc_testsrc.o OBJS-$(CONFIG_YUVTESTSRC_FILTER) += vsrc_testsrc.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 9a2cfea..f1f87ca 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -363,6 +363,7 @@ static void register_all(void) REGISTER_FILTER(RGBTESTSRC, rgbtestsrc, vsrc); REGISTER_FILTER(SMPTEBARS, smptebars, vsrc); REGISTER_FILTER(SMPTEHDBARS, smptehdbars, vsrc); + REGISTER_FILTER(STARFIELD, starfield, vsrc); REGISTER_FILTER(TESTSRC, testsrc, vsrc); REGISTER_FILTER(TESTSRC2, testsrc2, vsrc); REGISTER_FILTER(YUVTESTSRC, yuvtestsrc, vsrc); diff --git a/libavfilter/vsrc_starfield.c b/libavfilter/vsrc_starfield.c new file mode 100644 index 0000000..96b8667 --- /dev/null +++ b/libavfilter/vsrc_starfield.c @@ -0,0 +1,191 @@ +/* + * 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/internal.h" +#include "libavutil/lfg.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/random_seed.h" +#include "libavutil/avstring.h" +#include "avfilter.h" +#include "internal.h" +#include "formats.h" +#include "video.h" + +typedef struct Star { + int x, y; + AVRational z; + unsigned r, g, b; +} Star; + +typedef struct StarFieldContext { + const AVClass *class; + + int w, h; + uint64_t pts; + AVRational frame_rate; + AVRational speed; + int nb_stars; + int64_t seed; + + AVLFG lfg; + Star *stars; +} StarFieldContext; + +#define OFFSET(x) offsetof(StarFieldContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption starfield_options[] = { + { "size", "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 }, + { "stars", "set stars number", OFFSET(nb_stars), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, 81920, FLAGS }, + { "seed", "set seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, INT64_MAX, FLAGS }, + { "speed", "set speed", OFFSET(speed), AV_OPT_TYPE_RATIONAL, {.dbl = 1.01}, 1, 2, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(starfield); + +static void init_star(StarFieldContext *s, Star *star, int depth) +{ + star->x = av_lfg_get(&s->lfg) - INT_MAX; + star->y = av_lfg_get(&s->lfg) - INT_MAX; + star->z.num = star->z.den = 1; + star->r = av_lfg_get(&s->lfg) >> av_log2(depth / 32); + star->g = av_lfg_get(&s->lfg) >> av_log2(depth / 32); + star->b = av_lfg_get(&s->lfg) >> av_log2(depth / 32); +} + +static av_cold int init(AVFilterContext *ctx) +{ + StarFieldContext *s = ctx->priv; + int i; + + s->stars = av_calloc(s->nb_stars, sizeof(*s->stars)); + if (!s->stars) + return AVERROR(ENOMEM); + + if (s->seed == -1) + s->seed = av_get_random_seed(); + av_lfg_init(&s->lfg, s->seed); + + for (i = 0; i < s->nb_stars; i++) { + init_star(s, &s->stars[i], i+1); + } + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + StarFieldContext *s = ctx->priv; + + av_freep(&s->stars); +} + +static int config_props(AVFilterLink *outlink) +{ + StarFieldContext *s = outlink->src->priv; + + outlink->w = s->w; + outlink->h = s->h; + outlink->time_base = av_inv_q(s->frame_rate); + + return 0; +} + +static void fill_picture(AVFilterContext *ctx, AVFrame *out) +{ + StarFieldContext *s = ctx->priv; + const int divx = UINT_MAX / out->width; + const int divy = UINT_MAX / out->height; + const int hw = out->width >> 1; + const int hh = out->height >> 1; + const int linesize = out->linesize[0]; + int i; + + for (i = 0; i < out->height; i++) { + memset(out->data[0] + out->linesize[0] * i, 0, out->width * 4); + } + + for (i = 0; i < s->nb_stars; i++) { + Star *star = &s->stars[i]; + int x, y; + + x = av_rescale(star->x / divx, star->z.num, star->z.den) + hw; + y = av_rescale(star->y / divy, star->z.num, star->z.den) + hh; + if (x < 0 || x >= out->width || + y < 0 || y >= out->height) { + init_star(s, &s->stars[i], i+1); + continue; + } + + out->data[0][y * linesize + x * 4 + 0] = av_clip_uint8((av_rescale(star->r, star->z.num, star->z.den) >> 24)); + out->data[0][y * linesize + x * 4 + 1] = av_clip_uint8((av_rescale(star->g, star->z.num, star->z.den) >> 24)); + out->data[0][y * linesize + x * 4 + 2] = av_clip_uint8((av_rescale(star->b, star->z.num, star->z.den) >> 24)); + + star->z = av_mul_q(star->z, s->speed); + } +} + +static int request_frame(AVFilterLink *outlink) +{ + StarFieldContext *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}; + fill_picture(outlink->src, out); + + out->pts = s->pts++; + + return ff_filter_frame(outlink, out); +} + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB0, 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 const AVFilterPad starfield_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .request_frame = request_frame, + .config_props = config_props, + }, + { NULL } +}; + +AVFilter ff_vsrc_starfield = { + .name = "starfield", + .description = NULL_IF_CONFIG_SMALL("Create retro 3D star field."), + .priv_size = sizeof(StarFieldContext), + .priv_class = &starfield_class, + .init = init, + .uninit = uninit, + .query_formats = query_formats, + .inputs = NULL, + .outputs = starfield_outputs, +};