FFmpeg  4.4.9
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 "formats.h"
29 #include "video.h"
30 #include "internal.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/parseutils.h"
35 #include "libavutil/lfg.h"
36 #include "libavutil/random_seed.h"
37 #include <float.h>
38 #include <math.h>
39 
40 typedef struct SierpinskiContext {
41  const AVClass *class;
42  int w, h;
43  int type;
45  uint64_t pts;
46 
48  int jump;
49 
50  int pos_x, pos_y;
51  int dest_x, dest_y;
52 
54  int (*draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
56 
57 #define OFFSET(x) offsetof(SierpinskiContext, x)
58 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59 
60 static const AVOption sierpinski_options[] = {
61  {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
62  {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
63  {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
64  {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
65  {"seed", "set the seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
66  {"jump", "set the jump", OFFSET(jump), AV_OPT_TYPE_INT, {.i64=100}, 1, 10000, FLAGS },
67  {"type","set fractal type",OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "type" },
68  {"carpet", "sierpinski carpet", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
69  {"triangle", "sierpinski triangle", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
70  {NULL},
71 };
72 
74 
76 {
77  static const enum AVPixelFormat pix_fmts[] = {
80  };
81 
83  if (!fmts_list)
84  return AVERROR(ENOMEM);
85  return ff_set_common_formats(ctx, fmts_list);
86 }
87 
88 static int fill_sierpinski(SierpinskiContext *s, int x, int y)
89 {
90  int pos_x = x + s->pos_x;
91  int pos_y = y + s->pos_y;
92 
93  while (pos_x != 0 && pos_y != 0) {
94  if (FFABS(pos_x % 3) == 1 && FFABS(pos_y % 3) == 1)
95  return 1;
96 
97  pos_x /= 3;
98  pos_y /= 3;
99  }
100 
101  return 0;
102 }
103 
104 static int draw_triangle_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
105 {
106  SierpinskiContext *s = ctx->priv;
107  AVFrame *frame = arg;
108  const int width = frame->width;
109  const int height = frame->height;
110  const int start = ff_slice_pos(height, job, nb_jobs);
111  const int end = ff_slice_pos(height, job + 1, nb_jobs);
112  uint8_t *dst = frame->data[0] + start * frame->linesize[0];
113 
114  for (int y = start; y < end; y++) {
115  for (int x = 0; x < width; x++) {
116  if ((s->pos_x + x) & (s->pos_y + y)) {
117  AV_WL32(&dst[x*4], 0x00000000);
118  } else {
119  AV_WL32(&dst[x*4], 0xFFFFFFFF);
120  }
121  }
122 
123  dst += frame->linesize[0];
124  }
125 
126  return 0;
127 }
128 
129 static int draw_carpet_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
130 {
131  SierpinskiContext *s = ctx->priv;
132  AVFrame *frame = arg;
133  const int width = frame->width;
134  const int height = frame->height;
135  const int start = ff_slice_pos(height, job, nb_jobs);
136  const int end = ff_slice_pos(height, job + 1, nb_jobs);
137  uint8_t *dst = frame->data[0] + start * frame->linesize[0];
138 
139  for (int y = start; y < end; y++) {
140  for (int x = 0; x < width; x++) {
141  if (fill_sierpinski(s, x, y)) {
142  AV_WL32(&dst[x*4], 0x00000000);
143  } else {
144  AV_WL32(&dst[x*4], 0xFFFFFFFF);
145  }
146  }
147 
148  dst += frame->linesize[0];
149  }
150 
151  return 0;
152 }
153 
154 static int config_output(AVFilterLink *inlink)
155 {
156  AVFilterContext *ctx = inlink->src;
157  SierpinskiContext *s = ctx->priv;
158 
159  if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
160  return AVERROR(EINVAL);
161 
162  inlink->w = s->w;
163  inlink->h = s->h;
164  inlink->time_base = av_inv_q(s->frame_rate);
165  inlink->sample_aspect_ratio = (AVRational) {1, 1};
166  if (s->seed == -1)
167  s->seed = av_get_random_seed();
168  av_lfg_init(&s->lfg, s->seed);
169 
170  s->draw_slice = s->type ? draw_triangle_slice : draw_carpet_slice;
171 
172  return 0;
173 }
174 
176 {
177  SierpinskiContext *s = ctx->priv;
178  AVFilterLink *outlink = ctx->outputs[0];
179 
180  if (s->pos_x == s->dest_x && s->pos_y == s->dest_y) {
181  unsigned int rnd = av_lfg_get(&s->lfg);
182  int mod = 2 * s->jump + 1;
183 
184  s->dest_x += (int)((rnd & 0xffff) % mod) - s->jump;
185  s->dest_y += (int)((rnd >> 16) % mod) - s->jump;
186  } else {
187  if (s->pos_x < s->dest_x)
188  s->pos_x++;
189  else if (s->pos_x > s->dest_x)
190  s->pos_x--;
191 
192  if (s->pos_y < s->dest_y)
193  s->pos_y++;
194  else if (s->pos_y > s->dest_y)
195  s->pos_y--;
196  }
197 
198  ctx->internal->execute(ctx, s->draw_slice, frame, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
199 }
200 
202 {
203  SierpinskiContext *s = link->src->priv;
204  AVFrame *frame = ff_get_video_buffer(link, s->w, s->h);
205 
206  if (!frame)
207  return AVERROR(ENOMEM);
208 
210  frame->pts = s->pts++;
211 
212  draw_sierpinski(link->src, frame);
213 
214  return ff_filter_frame(link, frame);
215 }
216 
217 static const AVFilterPad sierpinski_outputs[] = {
218  {
219  .name = "default",
220  .type = AVMEDIA_TYPE_VIDEO,
221  .request_frame = sierpinski_request_frame,
222  .config_props = config_output,
223  },
224  { NULL }
225 };
226 
228  .name = "sierpinski",
229  .description = NULL_IF_CONFIG_SMALL("Render a Sierpinski fractal."),
230  .priv_size = sizeof(SierpinskiContext),
231  .priv_class = &sierpinski_class,
233  .inputs = NULL,
236 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:805
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define rnd()
Definition: checkasm.h:117
#define FFMIN(a, b)
Definition: common.h:105
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static AVFrame * frame
int
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition: filters.h:282
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_IMAGE_SIZE
offset must point to two consecutive integers
Definition: opt.h:235
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:238
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
@ AV_OPT_TYPE_INT
Definition: opt.h:225
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
#define AVERROR(e)
Definition: error.h:43
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:320
cl_device_type type
misc image utilities
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
const char * arg
Definition: jacosubdec.c:67
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
uint8_t w
Definition: llviddspenc.c:39
AVOptions.
misc parsing utilities
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:377
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
void * priv
private data for use by the filter
Definition: avfilter.h:356
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:406
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
AVOption.
Definition: opt.h:248
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVRational frame_rate
int(* draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
static int mod(int a, int b)
Modulo operation with only positive remainders.
Definition: vf_v360.c:749
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:104
static unsigned int seed
Definition: videogen.c:78
static int draw_carpet_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
static void draw_sierpinski(AVFilterContext *ctx, AVFrame *frame)
static int sierpinski_request_frame(AVFilterLink *link)
static int draw_triangle_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
static int config_output(AVFilterLink *inlink)
static int query_formats(AVFilterContext *ctx)
#define FLAGS
AVFILTER_DEFINE_CLASS(sierpinski)
static const AVFilterPad sierpinski_outputs[]
#define OFFSET(x)
AVFilter ff_vsrc_sierpinski
static const AVOption sierpinski_options[]
static int fill_sierpinski(SierpinskiContext *s, int x, int y)