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.

291 lines
10KB

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