FFmpeg
vsrc_sierpinski.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * Sierpinski carpet fractal renderer
24  */
25 
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "video.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/lfg.h"
33 #include "libavutil/random_seed.h"
34 #include <float.h>
35 #include <math.h>
36 
37 typedef struct SierpinskiContext {
38  const AVClass *class;
39  int w, h;
40  int type;
42  uint64_t pts;
43 
45  int jump;
46 
47  int pos_x, pos_y;
48  int dest_x, dest_y;
49 
51  int (*draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
53 
54 #define OFFSET(x) offsetof(SierpinskiContext, x)
55 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
56 
57 static const AVOption sierpinski_options[] = {
58  {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
59  {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
60  {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
61  {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
62  {"seed", "set the seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
63  {"jump", "set the jump", OFFSET(jump), AV_OPT_TYPE_INT, {.i64=100}, 1, 10000, FLAGS },
64  {"type","set fractal type",OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "type" },
65  {"carpet", "sierpinski carpet", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "type" },
66  {"triangle", "sierpinski triangle", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "type" },
67  {NULL},
68 };
69 
70 AVFILTER_DEFINE_CLASS(sierpinski);
71 
72 static int fill_sierpinski(SierpinskiContext *s, int x, int y)
73 {
74  int pos_x = x + s->pos_x;
75  int pos_y = y + s->pos_y;
76 
77  while (pos_x != 0 && pos_y != 0) {
78  if (FFABS(pos_x % 3) == 1 && FFABS(pos_y % 3) == 1)
79  return 1;
80 
81  pos_x /= 3;
82  pos_y /= 3;
83  }
84 
85  return 0;
86 }
87 
88 static int draw_triangle_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
89 {
90  SierpinskiContext *s = ctx->priv;
91  AVFrame *frame = arg;
92  const int width = frame->width;
93  const int height = frame->height;
94  const int start = (height * job ) / nb_jobs;
95  const int end = (height * (job+1)) / nb_jobs;
96  uint8_t *dst = frame->data[0] + start * frame->linesize[0];
97 
98  for (int y = start; y < end; y++) {
99  for (int x = 0; x < width; x++) {
100  if ((s->pos_x + x) & (s->pos_y + y)) {
101  AV_WL32(&dst[x*4], 0x00000000);
102  } else {
103  AV_WL32(&dst[x*4], 0xFFFFFFFF);
104  }
105  }
106 
107  dst += frame->linesize[0];
108  }
109 
110  return 0;
111 }
112 
113 static int draw_carpet_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
114 {
115  SierpinskiContext *s = ctx->priv;
116  AVFrame *frame = arg;
117  const int width = frame->width;
118  const int height = frame->height;
119  const int start = (height * job ) / nb_jobs;
120  const int end = (height * (job+1)) / nb_jobs;
121  uint8_t *dst = frame->data[0] + start * frame->linesize[0];
122 
123  for (int y = start; y < end; y++) {
124  for (int x = 0; x < width; x++) {
125  if (fill_sierpinski(s, x, y)) {
126  AV_WL32(&dst[x*4], 0x00000000);
127  } else {
128  AV_WL32(&dst[x*4], 0xFFFFFFFF);
129  }
130  }
131 
132  dst += frame->linesize[0];
133  }
134 
135  return 0;
136 }
137 
138 static int config_output(AVFilterLink *outlink)
139 {
140  AVFilterContext *ctx = outlink->src;
141  FilterLink *l = ff_filter_link(outlink);
142  SierpinskiContext *s = ctx->priv;
143 
144  if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
145  return AVERROR(EINVAL);
146 
147  outlink->w = s->w;
148  outlink->h = s->h;
149  outlink->time_base = av_inv_q(s->frame_rate);
150  outlink->sample_aspect_ratio = (AVRational) {1, 1};
151  l->frame_rate = s->frame_rate;
152  if (s->seed == -1)
153  s->seed = av_get_random_seed();
154  av_lfg_init(&s->lfg, s->seed);
155 
156  s->draw_slice = s->type ? draw_triangle_slice : draw_carpet_slice;
157 
158  return 0;
159 }
160 
162 {
163  SierpinskiContext *s = ctx->priv;
164  AVFilterLink *outlink = ctx->outputs[0];
165 
166  if (s->pos_x == s->dest_x && s->pos_y == s->dest_y) {
167  unsigned int rnd = av_lfg_get(&s->lfg);
168  int mod = 2 * s->jump + 1;
169 
170  s->dest_x += (int)((rnd & 0xffff) % mod) - s->jump;
171  s->dest_y += (int)((rnd >> 16) % mod) - s->jump;
172  } else {
173  if (s->pos_x < s->dest_x)
174  s->pos_x++;
175  else if (s->pos_x > s->dest_x)
176  s->pos_x--;
177 
178  if (s->pos_y < s->dest_y)
179  s->pos_y++;
180  else if (s->pos_y > s->dest_y)
181  s->pos_y--;
182  }
183 
184  ff_filter_execute(ctx, s->draw_slice, frame, NULL,
185  FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
186 }
187 
189 {
190  SierpinskiContext *s = link->src->priv;
191  AVFrame *frame = ff_get_video_buffer(link, s->w, s->h);
192 
193  if (!frame)
194  return AVERROR(ENOMEM);
195 
196  frame->sample_aspect_ratio = (AVRational) {1, 1};
197  frame->pts = s->pts++;
198  frame->duration = 1;
199 
200  draw_sierpinski(link->src, frame);
201 
202  return ff_filter_frame(link, frame);
203 }
204 
205 static const AVFilterPad sierpinski_outputs[] = {
206  {
207  .name = "default",
208  .type = AVMEDIA_TYPE_VIDEO,
209  .request_frame = sierpinski_request_frame,
210  .config_props = config_output,
211  },
212 };
213 
215  .name = "sierpinski",
216  .description = NULL_IF_CONFIG_SMALL("Render a Sierpinski fractal."),
217  .priv_size = sizeof(SierpinskiContext),
218  .priv_class = &sierpinski_class,
219  .inputs = NULL,
223 };
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(sierpinski)
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
SierpinskiContext::jump
int jump
Definition: vsrc_sierpinski.c:45
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:422
SierpinskiContext::pts
uint64_t pts
Definition: vsrc_sierpinski.c:42
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
sierpinski_options
static const AVOption sierpinski_options[]
Definition: vsrc_sierpinski.c:57
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition: opt.h:315
int64_t
long long int64_t
Definition: coverity.c:34
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:429
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:167
config_output
static int config_output(AVFilterLink *outlink)
Definition: vsrc_sierpinski.c:138
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
rnd
#define rnd()
Definition: checkasm.h:173
draw_carpet_slice
static int draw_carpet_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
Definition: vsrc_sierpinski.c:113
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
AV_PIX_FMT_0BGR32
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:480
lfg.h
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition: opt.h:263
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FLAGS
#define FLAGS
Definition: vsrc_sierpinski.c:55
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
arg
const char * arg
Definition: jacosubdec.c:67
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition: opt.h:303
SierpinskiContext::pos_y
int pos_y
Definition: vsrc_sierpinski.c:47
ff_vsrc_sierpinski
const AVFilter ff_vsrc_sierpinski
Definition: vsrc_sierpinski.c:214
draw_sierpinski
static void draw_sierpinski(AVFilterContext *ctx, AVFrame *frame)
Definition: vsrc_sierpinski.c:161
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
seed
static unsigned int seed
Definition: videogen.c:78
SierpinskiContext::dest_y
int dest_y
Definition: vsrc_sierpinski.c:48
SierpinskiContext::dest_x
int dest_x
Definition: vsrc_sierpinski.c:48
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
SierpinskiContext::type
int type
Definition: vsrc_sierpinski.c:40
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
SierpinskiContext::h
int h
Definition: vsrc_sierpinski.c:39
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
height
#define height
Definition: dsp.h:85
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
sierpinski_outputs
static const AVFilterPad sierpinski_outputs[]
Definition: vsrc_sierpinski.c:205
SierpinskiContext::draw_slice
int(* draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
Definition: vsrc_sierpinski.c:51
SierpinskiContext::pos_x
int pos_x
Definition: vsrc_sierpinski.c:47
SierpinskiContext::w
int w
Definition: vsrc_sierpinski.c:39
SierpinskiContext::seed
int64_t seed
Definition: vsrc_sierpinski.c:44
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:841
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
mod
static int mod(int a, int b)
Modulo operation with only positive remainders.
Definition: vf_v360.c:755
SierpinskiContext::frame_rate
AVRational frame_rate
Definition: vsrc_sierpinski.c:41
AVFilter
Filter definition.
Definition: avfilter.h:201
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
random_seed.h
draw_triangle_slice
static int draw_triangle_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
Definition: vsrc_sierpinski.c:88
ff_filter_execute
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: avfilter.c:1667
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
fill_sierpinski
static int fill_sierpinski(SierpinskiContext *s, int x, int y)
Definition: vsrc_sierpinski.c:72
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:152
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
SierpinskiContext::lfg
AVLFG lfg
Definition: vsrc_sierpinski.c:50
imgutils.h
av_image_check_size
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:318
width
#define width
Definition: dsp.h:85
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: filters.h:252
sierpinski_request_frame
static int sierpinski_request_frame(AVFilterLink *link)
Definition: vsrc_sierpinski.c:188
SierpinskiContext
Definition: vsrc_sierpinski.c:37
OFFSET
#define OFFSET(x)
Definition: vsrc_sierpinski.c:54