You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
11KB

  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. #include "avfilter.h"
  21. #include "filters.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. typedef struct GradientsContext {
  34. const AVClass *class;
  35. int w, h;
  36. int type;
  37. AVRational frame_rate;
  38. int64_t pts;
  39. int64_t duration; ///< duration expressed in microseconds
  40. float speed;
  41. uint8_t color_rgba[8][4];
  42. int nb_colors;
  43. int x0, y0, x1, y1;
  44. float fx0, fy0, fx1, fy1;
  45. int64_t seed;
  46. AVLFG lfg;
  47. int (*draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
  48. } GradientsContext;
  49. #define OFFSET(x) offsetof(GradientsContext, x)
  50. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  51. static const AVOption gradients_options[] = {
  52. {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
  53. {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
  54. {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  55. {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  56. {"c0", "set 1st color", OFFSET(color_rgba[0]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
  57. {"c1", "set 2nd color", OFFSET(color_rgba[1]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
  58. {"c2", "set 3rd color", OFFSET(color_rgba[2]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
  59. {"c3", "set 4th color", OFFSET(color_rgba[3]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
  60. {"c4", "set 5th color", OFFSET(color_rgba[4]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
  61. {"c5", "set 6th color", OFFSET(color_rgba[5]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
  62. {"c6", "set 7th color", OFFSET(color_rgba[6]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
  63. {"c7", "set 8th color", OFFSET(color_rgba[7]), AV_OPT_TYPE_COLOR, {.str = "random"}, 0, 0, FLAGS },
  64. {"x0", "set gradient line source x0", OFFSET(x0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
  65. {"y0", "set gradient line source y0", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
  66. {"x1", "set gradient line destination x1", OFFSET(x1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
  67. {"y1", "set gradient line destination y1", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
  68. {"nb_colors", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
  69. {"n", "set the number of colors", OFFSET(nb_colors), AV_OPT_TYPE_INT, {.i64=2}, 2, 8, FLAGS },
  70. {"seed", "set the seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
  71. {"duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },\
  72. {"d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },\
  73. {"speed", "set gradients rotation speed", OFFSET(speed), AV_OPT_TYPE_FLOAT,{.dbl=0.01}, 0.00001, 1, FLAGS },\
  74. {NULL},
  75. };
  76. AVFILTER_DEFINE_CLASS(gradients);
  77. static int query_formats(AVFilterContext *ctx)
  78. {
  79. static const enum AVPixelFormat pix_fmts[] = {
  80. AV_PIX_FMT_RGBA,
  81. AV_PIX_FMT_RGBA64,
  82. AV_PIX_FMT_NONE
  83. };
  84. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  85. if (!fmts_list)
  86. return AVERROR(ENOMEM);
  87. return ff_set_common_formats(ctx, fmts_list);
  88. }
  89. static uint32_t lerp_color(uint8_t c0[4], uint8_t c1[4], float x)
  90. {
  91. const float y = 1.f - x;
  92. return (lrintf(c0[0] * y + c1[0] * x)) << 0 |
  93. (lrintf(c0[1] * y + c1[1] * x)) << 8 |
  94. (lrintf(c0[2] * y + c1[2] * x)) << 16 |
  95. (lrintf(c0[3] * y + c1[3] * x)) << 24;
  96. }
  97. static uint64_t lerp_color16(uint8_t c0[4], uint8_t c1[4], float x)
  98. {
  99. const float y = 1.f - x;
  100. return (llrintf((c0[0] * y + c1[0] * x) * 256)) << 0 |
  101. (llrintf((c0[1] * y + c1[1] * x) * 256)) << 16 |
  102. (llrintf((c0[2] * y + c1[2] * x) * 256)) << 32 |
  103. (llrintf((c0[3] * y + c1[3] * x) * 256)) << 48;
  104. }
  105. static uint32_t lerp_colors(uint8_t arr[3][4], int nb_colors, float step)
  106. {
  107. float scl;
  108. int i;
  109. if (nb_colors == 1 || step <= 0.0) {
  110. return arr[0][0] | (arr[0][1] << 8) | (arr[0][2] << 16) | (arr[0][3] << 24);
  111. } else if (step >= 1.0) {
  112. i = nb_colors - 1;
  113. return arr[i][0] | (arr[i][1] << 8) | (arr[i][2] << 16) | (arr[i][3] << 24);
  114. }
  115. scl = step * (nb_colors - 1);
  116. i = floorf(scl);
  117. return lerp_color(arr[i], arr[i + 1], scl - i);
  118. }
  119. static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, float step)
  120. {
  121. float scl;
  122. int i;
  123. if (nb_colors == 1 || step <= 0.0) {
  124. 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);
  125. } else if (step >= 1.0) {
  126. i = nb_colors - 1;
  127. 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);
  128. }
  129. scl = step * (nb_colors - 1);
  130. i = floorf(scl);
  131. return lerp_color16(arr[i], arr[i + 1], scl - i);
  132. }
  133. static float project(float origin_x, float origin_y,
  134. float dest_x, float dest_y,
  135. int point_x, int point_y)
  136. {
  137. // Rise and run of line.
  138. float od_x = dest_x - origin_x;
  139. float od_y = dest_y - origin_y;
  140. // Distance-squared of line.
  141. float od_s_q = od_x * od_x + od_y * od_y;
  142. // Rise and run of projection.
  143. float op_x = point_x - origin_x;
  144. float op_y = point_y - origin_y;
  145. float op_x_od = op_x * od_x + op_y * od_y;
  146. // Normalize and clamp range.
  147. return av_clipf(op_x_od / od_s_q, 0.f, 1.f);
  148. }
  149. static int draw_gradients_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  150. {
  151. GradientsContext *s = ctx->priv;
  152. AVFrame *frame = arg;
  153. const int width = frame->width;
  154. const int height = frame->height;
  155. const int start = (height * job ) / nb_jobs;
  156. const int end = (height * (job+1)) / nb_jobs;
  157. const int linesize = frame->linesize[0] / 4;
  158. uint32_t *dst = (uint32_t *)frame->data[0] + start * linesize;
  159. for (int y = start; y < end; y++) {
  160. for (int x = 0; x < width; x++) {
  161. float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y);
  162. dst[x] = lerp_colors(s->color_rgba, s->nb_colors, factor);
  163. }
  164. dst += linesize;
  165. }
  166. return 0;
  167. }
  168. static int draw_gradients_slice16(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  169. {
  170. GradientsContext *s = ctx->priv;
  171. AVFrame *frame = arg;
  172. const int width = frame->width;
  173. const int height = frame->height;
  174. const int start = (height * job ) / nb_jobs;
  175. const int end = (height * (job+1)) / nb_jobs;
  176. const int linesize = frame->linesize[0] / 8;
  177. uint64_t *dst = (uint64_t *)frame->data[0] + start * linesize;
  178. for (int y = start; y < end; y++) {
  179. for (int x = 0; x < width; x++) {
  180. float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y);
  181. dst[x] = lerp_colors16(s->color_rgba, s->nb_colors, factor);
  182. }
  183. dst += linesize;
  184. }
  185. return 0;
  186. }static int config_output(AVFilterLink *inlink)
  187. {
  188. AVFilterContext *ctx = inlink->src;
  189. GradientsContext *s = ctx->priv;
  190. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  191. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  192. return AVERROR(EINVAL);
  193. inlink->w = s->w;
  194. inlink->h = s->h;
  195. inlink->time_base = av_inv_q(s->frame_rate);
  196. inlink->sample_aspect_ratio = (AVRational) {1, 1};
  197. if (s->seed == -1)
  198. s->seed = av_get_random_seed();
  199. av_lfg_init(&s->lfg, s->seed);
  200. s->draw_slice = desc->comp[0].depth == 16 ? draw_gradients_slice16 : draw_gradients_slice;
  201. if (s->x0 < 0 || s->x0 >= s->w)
  202. s->x0 = av_lfg_get(&s->lfg) % s->w;
  203. if (s->y0 < 0 || s->y0 >= s->h)
  204. s->y0 = av_lfg_get(&s->lfg) % s->h;
  205. if (s->x1 < 0 || s->x1 >= s->w)
  206. s->x1 = av_lfg_get(&s->lfg) % s->w;
  207. if (s->y1 < 0 || s->y1 >= s->h)
  208. s->y1 = av_lfg_get(&s->lfg) % s->h;
  209. return 0;
  210. }
  211. static int activate(AVFilterContext *ctx)
  212. {
  213. GradientsContext *s = ctx->priv;
  214. AVFilterLink *outlink = ctx->outputs[0];
  215. if (s->duration >= 0 &&
  216. av_rescale_q(s->pts, outlink->time_base, AV_TIME_BASE_Q) >= s->duration) {
  217. ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
  218. return 0;
  219. }
  220. if (ff_outlink_frame_wanted(outlink)) {
  221. AVFrame *frame = ff_get_video_buffer(outlink, s->w, s->h);
  222. float angle = fmodf(s->pts * s->speed, 2.f * M_PI);
  223. const float w2 = s->w / 2.f;
  224. const float h2 = s->h / 2.f;
  225. s->fx0 = (s->x0 - w2) * cosf(angle) - (s->y0 - h2) * sinf(angle) + w2;
  226. s->fy0 = (s->x0 - w2) * sinf(angle) + (s->y0 - h2) * cosf(angle) + h2;
  227. s->fx1 = (s->x1 - w2) * cosf(angle) - (s->y1 - h2) * sinf(angle) + w2;
  228. s->fy1 = (s->x1 - w2) * sinf(angle) + (s->y1 - h2) * cosf(angle) + h2;
  229. if (!frame)
  230. return AVERROR(ENOMEM);
  231. frame->key_frame = 1;
  232. frame->interlaced_frame = 0;
  233. frame->pict_type = AV_PICTURE_TYPE_I;
  234. frame->sample_aspect_ratio = (AVRational) {1, 1};
  235. frame->pts = s->pts++;
  236. ctx->internal->execute(ctx, s->draw_slice, frame, NULL,
  237. FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  238. return ff_filter_frame(outlink, frame);
  239. }
  240. return FFERROR_NOT_READY;
  241. }
  242. static const AVFilterPad gradients_outputs[] = {
  243. {
  244. .name = "default",
  245. .type = AVMEDIA_TYPE_VIDEO,
  246. .config_props = config_output,
  247. },
  248. { NULL }
  249. };
  250. AVFilter ff_vsrc_gradients = {
  251. .name = "gradients",
  252. .description = NULL_IF_CONFIG_SMALL("Draw a gradients."),
  253. .priv_size = sizeof(GradientsContext),
  254. .priv_class = &gradients_class,
  255. .query_formats = query_formats,
  256. .inputs = NULL,
  257. .outputs = gradients_outputs,
  258. .activate = activate,
  259. .flags = AVFILTER_FLAG_SLICE_THREADS,
  260. };