FFmpeg  4.4.4
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 "filters.h"
23 #include "formats.h"
24 #include "video.h"
25 #include "internal.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/parseutils.h"
30 #include "libavutil/lfg.h"
31 #include "libavutil/random_seed.h"
32 #include <float.h>
33 #include <math.h>
34 
35 typedef struct GradientsContext {
36  const AVClass *class;
37  int w, h;
38  int type;
40  int64_t pts;
41  int64_t duration; ///< duration expressed in microseconds
42  float speed;
43 
45  int nb_colors;
46  int x0, y0, x1, y1;
47  float fx0, fy0, fx1, fy1;
48 
49  int64_t seed;
50 
52  int (*draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
54 
55 #define OFFSET(x) offsetof(GradientsContext, x)
56 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57 
58 static const AVOption gradients_options[] = {
59  {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
60  {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
61  {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
62  {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
63  {"c0", "set 1st color", OFFSET(color_rgba[0]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
64  {"c1", "set 2nd color", OFFSET(color_rgba[1]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
65  {"c2", "set 3rd color", OFFSET(color_rgba[2]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
66  {"c3", "set 4th color", OFFSET(color_rgba[3]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
67  {"c4", "set 5th color", OFFSET(color_rgba[4]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
68  {"c5", "set 6th color", OFFSET(color_rgba[5]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
69  {"c6", "set 7th color", OFFSET(color_rgba[6]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
70  {"c7", "set 8th color", OFFSET(color_rgba[7]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
71  {"x0", "set gradient line source x0", OFFSET(x0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
72  {"y0", "set gradient line source y0", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
73  {"x1", "set gradient line destination x1", OFFSET(x1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
74  {"y1", "set gradient line destination y1", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
75  {"nb_colors", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
76  {"n", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
77  {"seed", "set the seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
78  {"duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },\
79  {"d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },\
80  {"speed", "set gradients rotation speed", OFFSET(speed), AV_OPT_TYPE_FLOAT,{.dbl=0.01}, 0.00001, 1, FLAGS },\
81  {NULL},
82 };
83 
85 
87 {
88  static const enum AVPixelFormat pix_fmts[] = {
92  };
93 
95  if (!fmts_list)
96  return AVERROR(ENOMEM);
97  return ff_set_common_formats(ctx, fmts_list);
98 }
99 
100 static uint32_t lerp_color(uint8_t c0[4], uint8_t c1[4], float x)
101 {
102  const float y = 1.f - x;
103 
104  return (lrintf(c0[0] * y + c1[0] * x)) << 0 |
105  (lrintf(c0[1] * y + c1[1] * x)) << 8 |
106  (lrintf(c0[2] * y + c1[2] * x)) << 16 |
107  (lrintf(c0[3] * y + c1[3] * x)) << 24;
108 }
109 
110 static uint64_t lerp_color16(uint8_t c0[4], uint8_t c1[4], float x)
111 {
112  const float y = 1.f - x;
113 
114  return (llrintf((c0[0] * y + c1[0] * x) * 256)) << 0 |
115  (llrintf((c0[1] * y + c1[1] * x) * 256)) << 16 |
116  (llrintf((c0[2] * y + c1[2] * x) * 256)) << 32 |
117  (llrintf((c0[3] * y + c1[3] * x) * 256)) << 48;
118 }
119 
120 static uint32_t lerp_colors(uint8_t arr[3][4], int nb_colors, float step)
121 {
122  float scl;
123  int i;
124 
125  if (nb_colors == 1 || step <= 0.0) {
126  return arr[0][0] | (arr[0][1] << 8) | (arr[0][2] << 16) | (arr[0][3] << 24);
127  } else if (step >= 1.0) {
128  i = nb_colors - 1;
129  return arr[i][0] | (arr[i][1] << 8) | (arr[i][2] << 16) | (arr[i][3] << 24);
130  }
131 
132  scl = step * (nb_colors - 1);
133  i = floorf(scl);
134 
135  return lerp_color(arr[i], arr[i + 1], scl - i);
136 }
137 
138 static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, float step)
139 {
140  float scl;
141  int i;
142 
143  if (nb_colors == 1 || step <= 0.0) {
144  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);
145  } else if (step >= 1.0) {
146  i = nb_colors - 1;
147  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);
148  }
149 
150  scl = step * (nb_colors - 1);
151  i = floorf(scl);
152 
153  return lerp_color16(arr[i], arr[i + 1], scl - i);
154 }
155 
156 static float project(float origin_x, float origin_y,
157  float dest_x, float dest_y,
158  int point_x, int point_y)
159 {
160  // Rise and run of line.
161  float od_x = dest_x - origin_x;
162  float od_y = dest_y - origin_y;
163 
164  // Distance-squared of line.
165  float od_s_q = od_x * od_x + od_y * od_y;
166 
167  // Rise and run of projection.
168  float op_x = point_x - origin_x;
169  float op_y = point_y - origin_y;
170  float op_x_od = op_x * od_x + op_y * od_y;
171 
172  // Normalize and clamp range.
173  return av_clipf(op_x_od / od_s_q, 0.f, 1.f);
174 }
175 
176 static int draw_gradients_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
177 {
178  GradientsContext *s = ctx->priv;
179  AVFrame *frame = arg;
180  const int width = frame->width;
181  const int height = frame->height;
182  const int start = (height * job ) / nb_jobs;
183  const int end = (height * (job+1)) / nb_jobs;
184  const int linesize = frame->linesize[0] / 4;
185  uint32_t *dst = (uint32_t *)frame->data[0] + start * linesize;
186 
187  for (int y = start; y < end; y++) {
188  for (int x = 0; x < width; x++) {
189  float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y);
190  dst[x] = lerp_colors(s->color_rgba, s->nb_colors, factor);
191  }
192 
193  dst += linesize;
194  }
195 
196  return 0;
197 }
198 
199 static int draw_gradients_slice16(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
200 {
201  GradientsContext *s = ctx->priv;
202  AVFrame *frame = arg;
203  const int width = frame->width;
204  const int height = frame->height;
205  const int start = (height * job ) / nb_jobs;
206  const int end = (height * (job+1)) / nb_jobs;
207  const int linesize = frame->linesize[0] / 8;
208  uint64_t *dst = (uint64_t *)frame->data[0] + start * linesize;
209 
210  for (int y = start; y < end; y++) {
211  for (int x = 0; x < width; x++) {
212  float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y);
213  dst[x] = lerp_colors16(s->color_rgba, s->nb_colors, factor);
214  }
215 
216  dst += linesize;
217  }
218 
219  return 0;
220 }static int config_output(AVFilterLink *inlink)
221 {
222  AVFilterContext *ctx = inlink->src;
223  GradientsContext *s = ctx->priv;
225 
226  if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
227  return AVERROR(EINVAL);
228 
229  inlink->w = s->w;
230  inlink->h = s->h;
231  inlink->time_base = av_inv_q(s->frame_rate);
232  inlink->sample_aspect_ratio = (AVRational) {1, 1};
233  if (s->seed == -1)
234  s->seed = av_get_random_seed();
235  av_lfg_init(&s->lfg, s->seed);
236 
237  s->draw_slice = desc->comp[0].depth == 16 ? draw_gradients_slice16 : draw_gradients_slice;
238 
239  if (s->x0 < 0 || s->x0 >= s->w)
240  s->x0 = av_lfg_get(&s->lfg) % s->w;
241  if (s->y0 < 0 || s->y0 >= s->h)
242  s->y0 = av_lfg_get(&s->lfg) % s->h;
243  if (s->x1 < 0 || s->x1 >= s->w)
244  s->x1 = av_lfg_get(&s->lfg) % s->w;
245  if (s->y1 < 0 || s->y1 >= s->h)
246  s->y1 = av_lfg_get(&s->lfg) % s->h;
247 
248  return 0;
249 }
250 
252 {
253  GradientsContext *s = ctx->priv;
254  AVFilterLink *outlink = ctx->outputs[0];
255 
256  if (s->duration >= 0 &&
257  av_rescale_q(s->pts, outlink->time_base, AV_TIME_BASE_Q) >= s->duration) {
258  ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
259  return 0;
260  }
261 
262  if (ff_outlink_frame_wanted(outlink)) {
263  AVFrame *frame = ff_get_video_buffer(outlink, s->w, s->h);
264  float angle = fmodf(s->pts * s->speed, 2.f * M_PI);
265  const float w2 = s->w / 2.f;
266  const float h2 = s->h / 2.f;
267 
268  s->fx0 = (s->x0 - w2) * cosf(angle) - (s->y0 - h2) * sinf(angle) + w2;
269  s->fy0 = (s->x0 - w2) * sinf(angle) + (s->y0 - h2) * cosf(angle) + h2;
270 
271  s->fx1 = (s->x1 - w2) * cosf(angle) - (s->y1 - h2) * sinf(angle) + w2;
272  s->fy1 = (s->x1 - w2) * sinf(angle) + (s->y1 - h2) * cosf(angle) + h2;
273 
274  if (!frame)
275  return AVERROR(ENOMEM);
276 
277  frame->key_frame = 1;
278  frame->interlaced_frame = 0;
281  frame->pts = s->pts++;
282 
283  ctx->internal->execute(ctx, s->draw_slice, frame, NULL,
284  FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
285 
286  return ff_filter_frame(outlink, frame);
287  }
288 
289  return FFERROR_NOT_READY;
290 }
291 
292 static const AVFilterPad gradients_outputs[] = {
293  {
294  .name = "default",
295  .type = AVMEDIA_TYPE_VIDEO,
296  .config_props = config_output,
297  },
298  { NULL }
299 };
300 
302  .name = "gradients",
303  .description = NULL_IF_CONFIG_SMALL("Draw a gradients."),
304  .priv_size = sizeof(GradientsContext),
305  .priv_class = &gradients_class,
307  .inputs = NULL,
309  .activate = activate,
311 };
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:1096
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define av_clipf
Definition: common.h:170
#define NULL
Definition: coverity.c:32
static __device__ float floorf(float a)
Definition: cuda_runtime.h:172
static AVFrame * frame
int
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition: filters.h:172
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_DURATION
Definition: opt.h:239
@ 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
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
@ AV_OPT_TYPE_COLOR
Definition: opt.h:240
#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_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
@ 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:317
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
for(j=16;j >0;--j)
misc image utilities
int i
Definition: input.c:407
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:66
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
#define sinf(x)
Definition: libm.h:419
#define cosf(x)
Definition: libm.h:78
#define llrintf(x)
Definition: libm.h:399
#define lrintf(x)
Definition: libm_mips.h:70
const char * desc
Definition: libsvtav1.c:79
uint8_t w
Definition: llviddspenc.c:39
#define M_PI
Definition: mathematics.h:52
static const uint64_t c1
Definition: murmur3.c:51
AVOptions.
misc parsing utilities
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:389
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
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 key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
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 interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:465
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int64_t duration
duration expressed in microseconds
int(* draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
AVRational frame_rate
uint8_t color_rgba[8][4]
int64_t duration
Definition: movenc.c:64
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
static const int factor[16]
Definition: vf_pp7.c:77
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 uint32_t lerp_colors(uint8_t arr[3][4], int nb_colors, float step)
AVFilter ff_vsrc_gradients
AVFILTER_DEFINE_CLASS(gradients)
static int config_output(AVFilterLink *inlink)
static int query_formats(AVFilterContext *ctx)
#define FLAGS
static int draw_gradients_slice16(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
static uint32_t lerp_color(uint8_t c0[4], uint8_t c1[4], float x)
static int draw_gradients_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
static int activate(AVFilterContext *ctx)
static float project(float origin_x, float origin_y, float dest_x, float dest_y, int point_x, int point_y)
static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, float step)
static uint64_t lerp_color16(uint8_t c0[4], uint8_t c1[4], float x)
static const AVOption gradients_options[]
#define OFFSET(x)
static const AVFilterPad gradients_outputs[]