FFmpeg  4.3.8
vsrc_gradients.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 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 #include "avfilter.h"
22 #include "formats.h"
23 #include "video.h"
24 #include "internal.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 #include "libavutil/lfg.h"
30 #include "libavutil/random_seed.h"
31 #include <float.h>
32 #include <math.h>
33 
34 typedef struct GradientsContext {
35  const AVClass *class;
36  int w, h;
37  int type;
39  uint64_t pts;
40 
42  int nb_colors;
43  int x0, y0, x1, y1;
44  float fx0, fy0, fx1, fy1;
45 
47 
49  int (*draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
51 
52 #define OFFSET(x) offsetof(GradientsContext, x)
53 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54 
55 static const AVOption gradients_options[] = {
56  {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
57  {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
58  {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
59  {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
60  {"c0", "set 1st color", OFFSET(color_rgba[0]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
61  {"c1", "set 2nd color", OFFSET(color_rgba[1]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
62  {"c2", "set 3rd color", OFFSET(color_rgba[2]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
63  {"c3", "set 4th color", OFFSET(color_rgba[3]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
64  {"c4", "set 5th color", OFFSET(color_rgba[4]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
65  {"c5", "set 6th color", OFFSET(color_rgba[5]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
66  {"c6", "set 7th color", OFFSET(color_rgba[6]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
67  {"c7", "set 8th color", OFFSET(color_rgba[7]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
68  {"x0", "set gradient line source x0", OFFSET(x0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
69  {"y0", "set gradient line source y0", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
70  {"x1", "set gradient line destination x1", OFFSET(x1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
71  {"y1", "set gradient line destination y1", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
72  {"nb_colors", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
73  {"n", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
74  {"seed", "set the seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
75  {NULL},
76 };
77 
78 AVFILTER_DEFINE_CLASS(gradients);
79 
81 {
82  static const enum AVPixelFormat pix_fmts[] = {
86  };
87 
88  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
89  if (!fmts_list)
90  return AVERROR(ENOMEM);
91  return ff_set_common_formats(ctx, fmts_list);
92 }
93 
94 static uint32_t lerp_color(uint8_t c0[4], uint8_t c1[4], float x)
95 {
96  const float y = 1.f - x;
97 
98  return (lrint(c0[0] * y + c1[0] * x)) << 0 |
99  (lrint(c0[1] * y + c1[1] * x)) << 8 |
100  (lrint(c0[2] * y + c1[2] * x)) << 16 |
101  (lrint(c0[3] * y + c1[3] * x)) << 24;
102 }
103 
104 static uint64_t lerp_color16(uint8_t c0[4], uint8_t c1[4], float x)
105 {
106  const float y = 1.f - x;
107 
108  return (llrint((c0[0] * y + c1[0] * x) * 256)) << 0 |
109  (llrint((c0[1] * y + c1[1] * x) * 256)) << 16 |
110  (llrint((c0[2] * y + c1[2] * x) * 256)) << 32 |
111  (llrint((c0[3] * y + c1[3] * x) * 256)) << 48;
112 }
113 
114 static uint32_t lerp_colors(uint8_t arr[3][4], int nb_colors, float step)
115 {
116  float scl;
117  int i;
118 
119  if (nb_colors == 1 || step <= 0.0) {
120  return arr[0][0] | (arr[0][1] << 8) | (arr[0][2] << 16) | (arr[0][3] << 24);
121  } else if (step >= 1.0) {
122  i = nb_colors - 1;
123  return arr[i][0] | (arr[i][1] << 8) | (arr[i][2] << 16) | (arr[i][3] << 24);
124  }
125 
126  scl = step * (nb_colors - 1);
127  i = floorf(scl);
128 
129  return lerp_color(arr[i], arr[i + 1], scl - i);
130 }
131 
132 static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, float step)
133 {
134  float scl;
135  int i;
136 
137  if (nb_colors == 1 || step <= 0.0) {
138  return ((uint64_t)arr[0][0] << 8) | ((uint64_t)arr[0][1] << 24) | ((uint64_t)arr[0][2] << 40) | ((uint64_t)arr[0][3] << 56);
139  } else if (step >= 1.0) {
140  i = nb_colors - 1;
141  return ((uint64_t)arr[i][0] << 8) | ((uint64_t)arr[i][1] << 24) | ((uint64_t)arr[i][2] << 40) | ((uint64_t)arr[i][3] << 56);
142  }
143 
144  scl = step * (nb_colors - 1);
145  i = floorf(scl);
146 
147  return lerp_color16(arr[i], arr[i + 1], scl - i);
148 }
149 
150 static float project(float origin_x, float origin_y,
151  float dest_x, float dest_y,
152  int point_x, int point_y)
153 {
154  // Rise and run of line.
155  float od_x = dest_x - origin_x;
156  float od_y = dest_y - origin_y;
157 
158  // Distance-squared of line.
159  float od_s_q = od_x * od_x + od_y * od_y;
160 
161  // Rise and run of projection.
162  float op_x = point_x - origin_x;
163  float op_y = point_y - origin_y;
164  float op_x_od = op_x * od_x + op_y * od_y;
165 
166  // Normalize and clamp range.
167  return av_clipf(op_x_od / od_s_q, 0.f, 1.f);
168 }
169 
170 static int draw_gradients_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
171 {
172  GradientsContext *s = ctx->priv;
173  AVFrame *frame = arg;
174  const int width = frame->width;
175  const int height = frame->height;
176  const int start = (height * job ) / nb_jobs;
177  const int end = (height * (job+1)) / nb_jobs;
178  const int linesize = frame->linesize[0] / 4;
179  uint32_t *dst = (uint32_t *)frame->data[0] + start * linesize;
180 
181  for (int y = start; y < end; y++) {
182  for (int x = 0; x < width; x++) {
183  float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y);
184  dst[x] = lerp_colors(s->color_rgba, s->nb_colors, factor);;
185  }
186 
187  dst += linesize;
188  }
189 
190  return 0;
191 }
192 
193 static int draw_gradients_slice16(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
194 {
195  GradientsContext *s = ctx->priv;
196  AVFrame *frame = arg;
197  const int width = frame->width;
198  const int height = frame->height;
199  const int start = (height * job ) / nb_jobs;
200  const int end = (height * (job+1)) / nb_jobs;
201  const int linesize = frame->linesize[0] / 8;
202  uint64_t *dst = (uint64_t *)frame->data[0] + start * linesize;
203 
204  for (int y = start; y < end; y++) {
205  for (int x = 0; x < width; x++) {
206  float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y);
207  dst[x] = lerp_colors16(s->color_rgba, s->nb_colors, factor);;
208  }
209 
210  dst += linesize;
211  }
212 
213  return 0;
214 }static int config_output(AVFilterLink *inlink)
215 {
216  AVFilterContext *ctx = inlink->src;
217  GradientsContext *s = ctx->priv;
219 
220  if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
221  return AVERROR(EINVAL);
222 
223  inlink->w = s->w;
224  inlink->h = s->h;
225  inlink->time_base = av_inv_q(s->frame_rate);
226  inlink->sample_aspect_ratio = (AVRational) {1, 1};
227  if (s->seed == -1)
228  s->seed = av_get_random_seed();
229  av_lfg_init(&s->lfg, s->seed);
230 
232 
233  if (s->x0 < 0 || s->x0 >= s->w)
234  s->x0 = av_lfg_get(&s->lfg) % s->w;
235  if (s->y0 < 0 || s->y0 >= s->h)
236  s->y0 = av_lfg_get(&s->lfg) % s->h;
237  if (s->x1 < 0 || s->x1 >= s->w)
238  s->x1 = av_lfg_get(&s->lfg) % s->w;
239  if (s->y1 < 0 || s->y1 >= s->h)
240  s->y1 = av_lfg_get(&s->lfg) % s->h;
241 
242  return 0;
243 }
244 
246 {
247  AVFilterContext *ctx = outlink->src;
248  GradientsContext *s = ctx->priv;
249  AVFrame *frame = ff_get_video_buffer(outlink, s->w, s->h);
250  float angle = fmodf(s->pts / 100.f, 2.f * M_PI);
251  const float w2 = s->w / 2.f;
252  const float h2 = s->h / 2.f;
253 
254  s->fx0 = (s->x0 - w2) * cosf(angle) - (s->y0 - h2) * sinf(angle) + w2;
255  s->fy0 = (s->x0 - w2) * sinf(angle) + (s->y0 - h2) * cosf(angle) + h2;
256 
257  s->fx1 = (s->x1 - w2) * cosf(angle) - (s->y1 - h2) * sinf(angle) + w2;
258  s->fy1 = (s->x1 - w2) * sinf(angle) + (s->y1 - h2) * cosf(angle) + h2;
259 
260  if (!frame)
261  return AVERROR(ENOMEM);
262 
263  frame->sample_aspect_ratio = (AVRational) {1, 1};
264  frame->pts = s->pts++;
265 
266  ctx->internal->execute(ctx, s->draw_slice, frame, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
267 
268  return ff_filter_frame(outlink, frame);
269 }
270 
271 static const AVFilterPad gradients_outputs[] = {
272  {
273  .name = "default",
274  .type = AVMEDIA_TYPE_VIDEO,
275  .request_frame = gradients_request_frame,
276  .config_props = config_output,
277  },
278  { NULL }
279 };
280 
282  .name = "gradients",
283  .description = NULL_IF_CONFIG_SMALL("Draw a gradients."),
284  .priv_size = sizeof(GradientsContext),
285  .priv_class = &gradients_class,
287  .inputs = NULL,
288  .outputs = gradients_outputs,
290 };
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2549
static int draw_gradients_slice16(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AVOption.
Definition: opt.h:246
misc image utilities
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:79
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:387
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
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:300
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
AVOptions.
#define f(width, name)
Definition: cbs_vp9.c:255
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:393
#define cosf(x)
Definition: libm.h:78
static AVFrame * frame
#define height
static const uint64_t c1
Definition: murmur3.c:49
#define OFFSET(x)
static int gradients_request_frame(AVFilterLink *outlink)
A filter pad used for either input or output.
Definition: internal.h:54
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
int width
Definition: frame.h:358
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:605
AVFilter ff_vsrc_gradients
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
const char * arg
Definition: jacosubdec.c:66
static uint32_t lerp_colors(uint8_t arr[3][4], int nb_colors, float step)
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
static float project(float origin_x, float origin_y, float dest_x, float dest_y, int point_x, int point_y)
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:282
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:784
static const AVOption gradients_options[]
#define FFMIN(a, b)
Definition: common.h:96
#define FLAGS
#define width
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static int draw_gradients_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
#define sinf(x)
Definition: libm.h:419
static uint32_t lerp_color(uint8_t c0[4], uint8_t c1[4], float x)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
long long int64_t
Definition: coverity.c:34
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:388
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
#define llrint(x)
Definition: libm.h:394
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
Rational number (pair of numerator and denominator).
Definition: rational.h:58
offset must point to AVRational
Definition: opt.h:236
static const int factor[16]
Definition: vf_pp7.c:75
const char * name
Filter name.
Definition: avfilter.h:148
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
offset must point to two consecutive integers
Definition: opt.h:233
misc parsing utilities
AVFILTER_DEFINE_CLASS(gradients)
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
#define flags(name, subs,...)
Definition: cbs_av1.c:576
AVRational frame_rate
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
int
static int config_output(AVFilterLink *inlink)
static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, float step)
uint8_t color_rgba[8][4]
avfilter_execute_func * execute
Definition: internal.h:144
static uint64_t lerp_color16(uint8_t c0[4], uint8_t c1[4], float x)
static const AVFilterPad gradients_outputs[]
A list of supported formats for one end of a filter link.
Definition: formats.h:64
#define lrint
Definition: tablegen.h:53
An instance of a filter.
Definition: avfilter.h:338
int(* draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
static int query_formats(AVFilterContext *ctx)
int height
Definition: frame.h:358
#define M_PI
Definition: mathematics.h:52
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
internal API functions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
for(j=16;j >0;--j)