2 * Copyright (c) 2020 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
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"
34 typedef struct GradientsContext {
38 AVRational frame_rate;
40 int64_t duration; ///< duration expressed in microseconds
43 uint8_t color_rgba[8][4];
46 float fx0, fy0, fx1, fy1;
51 int (*draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
54 #define OFFSET(x) offsetof(GradientsContext, x)
55 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57 static const AVOption gradients_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 {"c0", "set 1st color", OFFSET(color_rgba[0]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
63 {"c1", "set 2nd color", OFFSET(color_rgba[1]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
64 {"c2", "set 3rd color", OFFSET(color_rgba[2]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
65 {"c3", "set 4th color", OFFSET(color_rgba[3]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
66 {"c4", "set 5th color", OFFSET(color_rgba[4]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
67 {"c5", "set 6th color", OFFSET(color_rgba[5]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
68 {"c6", "set 7th color", OFFSET(color_rgba[6]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
69 {"c7", "set 8th color", OFFSET(color_rgba[7]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
70 {"x0", "set gradient line source x0", OFFSET(x0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
71 {"y0", "set gradient line source y0", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
72 {"x1", "set gradient line destination x1", OFFSET(x1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
73 {"y1", "set gradient line destination y1", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
74 {"nb_colors", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
75 {"n", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
76 {"seed", "set the seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
77 {"duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },\
78 {"d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },\
79 {"speed", "set gradients rotation speed", OFFSET(speed), AV_OPT_TYPE_FLOAT,{.dbl=0.01}, 0.00001, 1, FLAGS },\
83 AVFILTER_DEFINE_CLASS(gradients);
85 static int query_formats(AVFilterContext *ctx)
87 static const enum AVPixelFormat pix_fmts[] = {
93 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
95 return AVERROR(ENOMEM);
96 return ff_set_common_formats(ctx, fmts_list);
99 static uint32_t lerp_color(uint8_t c0[4], uint8_t c1[4], float x)
101 const float y = 1.f - x;
103 return (lrintf(c0[0] * y + c1[0] * x)) << 0 |
104 (lrintf(c0[1] * y + c1[1] * x)) << 8 |
105 (lrintf(c0[2] * y + c1[2] * x)) << 16 |
106 (lrintf(c0[3] * y + c1[3] * x)) << 24;
109 static uint64_t lerp_color16(uint8_t c0[4], uint8_t c1[4], float x)
111 const float y = 1.f - x;
113 return (llrintf((c0[0] * y + c1[0] * x) * 256)) << 0 |
114 (llrintf((c0[1] * y + c1[1] * x) * 256)) << 16 |
115 (llrintf((c0[2] * y + c1[2] * x) * 256)) << 32 |
116 (llrintf((c0[3] * y + c1[3] * x) * 256)) << 48;
119 static uint32_t lerp_colors(uint8_t arr[3][4], int nb_colors, float step)
124 if (nb_colors == 1 || step <= 0.0) {
125 return arr[0][0] | (arr[0][1] << 8) | (arr[0][2] << 16) | (arr[0][3] << 24);
126 } else if (step >= 1.0) {
128 return arr[i][0] | (arr[i][1] << 8) | (arr[i][2] << 16) | (arr[i][3] << 24);
131 scl = step * (nb_colors - 1);
134 return lerp_color(arr[i], arr[i + 1], scl - i);
137 static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, float step)
142 if (nb_colors == 1 || step <= 0.0) {
143 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);
144 } else if (step >= 1.0) {
146 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);
149 scl = step * (nb_colors - 1);
152 return lerp_color16(arr[i], arr[i + 1], scl - i);
155 static float project(float origin_x, float origin_y,
156 float dest_x, float dest_y,
157 int point_x, int point_y)
159 // Rise and run of line.
160 float od_x = dest_x - origin_x;
161 float od_y = dest_y - origin_y;
163 // Distance-squared of line.
164 float od_s_q = od_x * od_x + od_y * od_y;
166 // Rise and run of projection.
167 float op_x = point_x - origin_x;
168 float op_y = point_y - origin_y;
169 float op_x_od = op_x * od_x + op_y * od_y;
171 // Normalize and clamp range.
172 return av_clipf(op_x_od / od_s_q, 0.f, 1.f);
175 static int draw_gradients_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
177 GradientsContext *s = ctx->priv;
178 AVFrame *frame = arg;
179 const int width = frame->width;
180 const int height = frame->height;
181 const int start = (height * job ) / nb_jobs;
182 const int end = (height * (job+1)) / nb_jobs;
183 const int linesize = frame->linesize[0] / 4;
184 uint32_t *dst = (uint32_t *)frame->data[0] + start * linesize;
186 for (int y = start; y < end; y++) {
187 for (int x = 0; x < width; x++) {
188 float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y);
189 dst[x] = lerp_colors(s->color_rgba, s->nb_colors, factor);;
198 static int draw_gradients_slice16(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
200 GradientsContext *s = ctx->priv;
201 AVFrame *frame = arg;
202 const int width = frame->width;
203 const int height = frame->height;
204 const int start = (height * job ) / nb_jobs;
205 const int end = (height * (job+1)) / nb_jobs;
206 const int linesize = frame->linesize[0] / 8;
207 uint64_t *dst = (uint64_t *)frame->data[0] + start * linesize;
209 for (int y = start; y < end; y++) {
210 for (int x = 0; x < width; x++) {
211 float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y);
212 dst[x] = lerp_colors16(s->color_rgba, s->nb_colors, factor);;
219 }static int config_output(AVFilterLink *inlink)
221 AVFilterContext *ctx = inlink->src;
222 GradientsContext *s = ctx->priv;
223 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
225 if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
226 return AVERROR(EINVAL);
230 inlink->time_base = av_inv_q(s->frame_rate);
231 inlink->sample_aspect_ratio = (AVRational) {1, 1};
233 s->seed = av_get_random_seed();
234 av_lfg_init(&s->lfg, s->seed);
236 s->draw_slice = desc->comp[0].depth == 16 ? draw_gradients_slice16 : draw_gradients_slice;
238 if (s->x0 < 0 || s->x0 >= s->w)
239 s->x0 = av_lfg_get(&s->lfg) % s->w;
240 if (s->y0 < 0 || s->y0 >= s->h)
241 s->y0 = av_lfg_get(&s->lfg) % s->h;
242 if (s->x1 < 0 || s->x1 >= s->w)
243 s->x1 = av_lfg_get(&s->lfg) % s->w;
244 if (s->y1 < 0 || s->y1 >= s->h)
245 s->y1 = av_lfg_get(&s->lfg) % s->h;
250 static int gradients_request_frame(AVFilterLink *outlink)
252 AVFilterContext *ctx = outlink->src;
253 GradientsContext *s = ctx->priv;
254 AVFrame *frame = ff_get_video_buffer(outlink, s->w, s->h);
255 float angle = fmodf(s->pts * s->speed, 2.f * M_PI);
256 const float w2 = s->w / 2.f;
257 const float h2 = s->h / 2.f;
259 if (s->duration >= 0 &&
260 av_rescale_q(s->pts, outlink->time_base, AV_TIME_BASE_Q) >= s->duration)
263 s->fx0 = (s->x0 - w2) * cosf(angle) - (s->y0 - h2) * sinf(angle) + w2;
264 s->fy0 = (s->x0 - w2) * sinf(angle) + (s->y0 - h2) * cosf(angle) + h2;
266 s->fx1 = (s->x1 - w2) * cosf(angle) - (s->y1 - h2) * sinf(angle) + w2;
267 s->fy1 = (s->x1 - w2) * sinf(angle) + (s->y1 - h2) * cosf(angle) + h2;
270 return AVERROR(ENOMEM);
272 frame->key_frame = 1;
273 frame->interlaced_frame = 0;
274 frame->pict_type = AV_PICTURE_TYPE_I;
275 frame->sample_aspect_ratio = (AVRational) {1, 1};
276 frame->pts = s->pts++;
278 ctx->internal->execute(ctx, s->draw_slice, frame, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
280 return ff_filter_frame(outlink, frame);
283 static const AVFilterPad gradients_outputs[] = {
286 .type = AVMEDIA_TYPE_VIDEO,
287 .request_frame = gradients_request_frame,
288 .config_props = config_output,
293 AVFilter ff_vsrc_gradients = {
295 .description = NULL_IF_CONFIG_SMALL("Draw a gradients."),
296 .priv_size = sizeof(GradientsContext),
297 .priv_class = &gradients_class,
298 .query_formats = query_formats,
300 .outputs = gradients_outputs,
301 .flags = AVFILTER_FLAG_SLICE_THREADS,