FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vsrc_gradients.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 0 215 0.0%
Functions: 0 12 0.0%
Branches: 0 83 0.0%

Line Branch Exec Source
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;
39 AVRational frame_rate;
40 int64_t pts;
41 int64_t duration; ///< duration expressed in microseconds
42 float speed;
43
44 uint8_t color_rgba[8][4];
45 float color_rgbaf[8][4];
46 int nb_colors;
47 int x0, y0, x1, y1;
48 float fx0, fy0, fx1, fy1;
49
50 int64_t seed;
51
52 AVLFG lfg;
53 int (*draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
54 } GradientsContext;
55
56 #define OFFSET(x) offsetof(GradientsContext, x)
57 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58
59 static const AVOption gradients_options[] = {
60 {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
61 {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
62 {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
63 {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
64 {"c0", "set 1st color", OFFSET(color_rgba[0]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
65 {"c1", "set 2nd color", OFFSET(color_rgba[1]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
66 {"c2", "set 3rd color", OFFSET(color_rgba[2]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
67 {"c3", "set 4th color", OFFSET(color_rgba[3]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
68 {"c4", "set 5th color", OFFSET(color_rgba[4]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
69 {"c5", "set 6th color", OFFSET(color_rgba[5]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
70 {"c6", "set 7th color", OFFSET(color_rgba[6]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
71 {"c7", "set 8th color", OFFSET(color_rgba[7]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
72 {"x0", "set gradient line source x0", OFFSET(x0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
73 {"y0", "set gradient line source y0", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
74 {"x1", "set gradient line destination x1", OFFSET(x1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
75 {"y1", "set gradient line destination y1", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
76 {"nb_colors", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
77 {"n", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
78 {"seed", "set the seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
79 {"duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
80 {"d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
81 {"speed", "set gradients rotation speed", OFFSET(speed), AV_OPT_TYPE_FLOAT,{.dbl=0.01}, 0.00001, 1, FLAGS },
82 {"type", "set gradient type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "type" },
83 {"t", "set gradient type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "type" },
84 {"linear", "set gradient type", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
85 {"radial", "set gradient type", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
86 {"circular", "set gradient type", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "type" },
87 {"spiral", "set gradient type", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "type" },
88 {NULL},
89 };
90
91 AVFILTER_DEFINE_CLASS(gradients);
92
93 static float lerpf(float a, float b, float x)
94 {
95 const float y = 1.f - x;
96
97 return a * y + b * x;
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, int nb_wrap_colors, float step)
121 {
122 float scl;
123 int i, j;
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_wrap_colors - 1);
133 i = floorf(scl);
134 j = i + 1;
135 if (i >= nb_colors - 1) {
136 i = nb_colors - 1;
137 j = 0;
138 }
139
140 return lerp_color(arr[i], arr[j], scl - i);
141 }
142
143 static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, int nb_wrap_colors, float step)
144 {
145 float scl;
146 int i, j;
147
148 if (nb_colors == 1 || step <= 0.0) {
149 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);
150 } else if (step >= 1.0) {
151 i = nb_colors - 1;
152 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);
153 }
154
155 scl = step * (nb_wrap_colors - 1);
156 i = floorf(scl);
157 j = i + 1;
158 if (i >= nb_colors - 1) {
159 i = nb_colors - 1;
160 j = 0;
161 }
162
163 return lerp_color16(arr[i], arr[j], scl - i);
164 }
165
166 static void lerp_colors32(float arr[3][4], int nb_colors,
167 int nb_wrap_colors, float step,
168 float *r, float *g, float *b, float *a)
169 {
170 float scl, x;
171 int i, j;
172
173 if (nb_colors == 1 || step <= 0.0) {
174 *r = arr[0][0];
175 *g = arr[0][1];
176 *b = arr[0][2];
177 *a = arr[0][3];
178 return;
179 } else if (step >= 1.0) {
180 i = nb_colors - 1;
181 *r = arr[i][0];
182 *g = arr[i][1];
183 *b = arr[i][2];
184 *a = arr[i][3];
185 return;
186 }
187
188 scl = step * (nb_wrap_colors - 1);
189 i = floorf(scl);
190 x = scl - i;
191 j = i + 1;
192 if (i >= nb_colors - 1) {
193 i = nb_colors - 1;
194 j = 0;
195 }
196
197 *r = lerpf(arr[i][0], arr[j][0], x);
198 *g = lerpf(arr[i][1], arr[j][1], x);
199 *b = lerpf(arr[i][2], arr[j][2], x);
200 *a = lerpf(arr[i][3], arr[j][3], x);
201 }
202
203 static float project(float origin_x, float origin_y,
204 float dest_x, float dest_y,
205 float point_x, float point_y, int type)
206 {
207 float op_x = point_x - origin_x;
208 float op_y = point_y - origin_y;
209 float od_x = dest_x - origin_x;
210 float od_y = dest_y - origin_y;
211 float op_x_od;
212 float od_s_q;
213
214 switch (type) {
215 case 0:
216 od_s_q = od_x * od_x + od_y * od_y;
217 break;
218 case 1:
219 od_s_q = sqrtf(od_x * od_x + od_y * od_y);
220 break;
221 case 2:
222 case 3:
223 od_s_q = M_PI * 2.f;
224 break;
225 }
226
227 switch (type) {
228 case 0:
229 op_x_od = op_x * od_x + op_y * od_y;
230 break;
231 case 1:
232 op_x_od = sqrtf(op_x * op_x + op_y * op_y);
233 break;
234 case 2:
235 op_x_od = atan2f(op_x, op_y) + M_PI;
236 break;
237 case 3:
238 op_x_od = fmodf(atan2f(op_x, op_y) + M_PI + point_x / fmaxf(origin_x, dest_x), 2.f * M_PI);
239 break;
240 }
241
242 // Normalize and clamp range.
243 return av_clipf(op_x_od / od_s_q, 0.f, 1.f);
244 }
245
246 static int draw_gradients_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
247 {
248 GradientsContext *s = ctx->priv;
249 AVFrame *frame = arg;
250 const int width = frame->width;
251 const int height = frame->height;
252 const int start = (height * job ) / nb_jobs;
253 const int end = (height * (job+1)) / nb_jobs;
254 const int linesize = frame->linesize[0] / 4;
255 uint32_t *dst = (uint32_t *)frame->data[0] + start * linesize;
256
257 for (int y = start; y < end; y++) {
258 for (int x = 0; x < width; x++) {
259 float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y, s->type);
260 dst[x] = lerp_colors(s->color_rgba, s->nb_colors, s->nb_colors + (s->type >= 2), factor);
261 }
262
263 dst += linesize;
264 }
265
266 return 0;
267 }
268
269 static int draw_gradients_slice16(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
270 {
271 GradientsContext *s = ctx->priv;
272 AVFrame *frame = arg;
273 const int width = frame->width;
274 const int height = frame->height;
275 const int start = (height * job ) / nb_jobs;
276 const int end = (height * (job+1)) / nb_jobs;
277 const int linesize = frame->linesize[0] / 8;
278 uint64_t *dst = (uint64_t *)frame->data[0] + start * linesize;
279
280 for (int y = start; y < end; y++) {
281 for (int x = 0; x < width; x++) {
282 float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y, s->type);
283 dst[x] = lerp_colors16(s->color_rgba, s->nb_colors, s->nb_colors + s->type >= 2, factor);
284 }
285
286 dst += linesize;
287 }
288
289 return 0;
290 }
291
292 static int draw_gradients_slice32_planar(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
293 {
294 GradientsContext *s = ctx->priv;
295 AVFrame *frame = arg;
296 const int width = frame->width;
297 const int height = frame->height;
298 const int start = (height * job ) / nb_jobs;
299 const int end = (height * (job+1)) / nb_jobs;
300 const int linesize_g = frame->linesize[0] / 4;
301 const int linesize_b = frame->linesize[1] / 4;
302 const int linesize_r = frame->linesize[2] / 4;
303 const int linesize_a = frame->linesize[3] / 4;
304 float *dst_g = (float *)frame->data[0] + start * linesize_g;
305 float *dst_b = (float *)frame->data[0] + start * linesize_b;
306 float *dst_r = (float *)frame->data[0] + start * linesize_r;
307 float *dst_a = (float *)frame->data[0] + start * linesize_a;
308
309 for (int y = start; y < end; y++) {
310 for (int x = 0; x < width; x++) {
311 float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y, s->type);
312 lerp_colors32(s->color_rgbaf, s->nb_colors, s->nb_colors + s->type >= 2 ,factor,
313 &dst_r[x], &dst_g[x], &dst_b[x], &dst_a[x]);
314 }
315
316 dst_g += linesize_g;
317 dst_b += linesize_b;
318 dst_r += linesize_r;
319 dst_a += linesize_a;
320 }
321
322 return 0;
323 }
324
325 static int config_output(AVFilterLink *outlink)
326 {
327 AVFilterContext *ctx = outlink->src;
328 GradientsContext *s = ctx->priv;
329 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
330
331 if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
332 return AVERROR(EINVAL);
333
334 outlink->w = s->w;
335 outlink->h = s->h;
336 outlink->time_base = av_inv_q(s->frame_rate);
337 outlink->sample_aspect_ratio = (AVRational) {1, 1};
338 outlink->frame_rate = s->frame_rate;
339 if (s->seed == -1)
340 s->seed = av_get_random_seed();
341 av_lfg_init(&s->lfg, s->seed);
342
343 switch (desc->comp[0].depth) {
344 case 8:
345 s->draw_slice = draw_gradients_slice;
346 break;
347 case 16:
348 s->draw_slice = draw_gradients_slice16;
349 break;
350 case 32:
351 s->draw_slice = draw_gradients_slice32_planar;
352 break;
353 default:
354 return AVERROR_BUG;
355 }
356
357 if (s->x0 < 0 || s->x0 >= s->w)
358 s->x0 = av_lfg_get(&s->lfg) % s->w;
359 if (s->y0 < 0 || s->y0 >= s->h)
360 s->y0 = av_lfg_get(&s->lfg) % s->h;
361 if (s->x1 < 0 || s->x1 >= s->w)
362 s->x1 = av_lfg_get(&s->lfg) % s->w;
363 if (s->y1 < 0 || s->y1 >= s->h)
364 s->y1 = av_lfg_get(&s->lfg) % s->h;
365
366 for (int n = 0; n < 8; n++) {
367 for (int c = 0; c < 4; c++)
368 s->color_rgbaf[n][c] = s->color_rgba[n][c] / 255.f;
369 }
370
371 return 0;
372 }
373
374 static int activate(AVFilterContext *ctx)
375 {
376 GradientsContext *s = ctx->priv;
377 AVFilterLink *outlink = ctx->outputs[0];
378
379 if (s->duration >= 0 &&
380 av_rescale_q(s->pts, outlink->time_base, AV_TIME_BASE_Q) >= s->duration) {
381 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
382 return 0;
383 }
384
385 if (ff_outlink_frame_wanted(outlink)) {
386 AVFrame *frame = ff_get_video_buffer(outlink, s->w, s->h);
387 float angle = fmodf(s->pts * s->speed, 2.f * M_PI);
388 const float w2 = s->w / 2.f;
389 const float h2 = s->h / 2.f;
390
391 s->fx0 = (s->x0 - w2) * cosf(angle) - (s->y0 - h2) * sinf(angle) + w2;
392 s->fy0 = (s->x0 - w2) * sinf(angle) + (s->y0 - h2) * cosf(angle) + h2;
393
394 s->fx1 = (s->x1 - w2) * cosf(angle) - (s->y1 - h2) * sinf(angle) + w2;
395 s->fy1 = (s->x1 - w2) * sinf(angle) + (s->y1 - h2) * cosf(angle) + h2;
396
397 if (!frame)
398 return AVERROR(ENOMEM);
399
400 frame->key_frame = 1;
401 frame->interlaced_frame = 0;
402 frame->pict_type = AV_PICTURE_TYPE_I;
403 frame->sample_aspect_ratio = (AVRational) {1, 1};
404 frame->pts = s->pts++;
405
406 ff_filter_execute(ctx, s->draw_slice, frame, NULL,
407 FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
408
409 return ff_filter_frame(outlink, frame);
410 }
411
412 return FFERROR_NOT_READY;
413 }
414
415 static const AVFilterPad gradients_outputs[] = {
416 {
417 .name = "default",
418 .type = AVMEDIA_TYPE_VIDEO,
419 .config_props = config_output,
420 },
421 };
422
423 const AVFilter ff_vsrc_gradients = {
424 .name = "gradients",
425 .description = NULL_IF_CONFIG_SMALL("Draw a gradients."),
426 .priv_size = sizeof(GradientsContext),
427 .priv_class = &gradients_class,
428 .inputs = NULL,
429 FILTER_OUTPUTS(gradients_outputs),
430 FILTER_PIXFMTS(AV_PIX_FMT_RGBA, AV_PIX_FMT_RGBA64, AV_PIX_FMT_GBRAPF32),
431 .activate = activate,
432 .flags = AVFILTER_FLAG_SLICE_THREADS,
433 };
434