vf_despill.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2017 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 "libavutil/opt.h"
  21. #include "libavutil/imgutils.h"
  22. #include "avfilter.h"
  23. #include "formats.h"
  24. #include "internal.h"
  25. #include "video.h"
  26. typedef struct DespillContext {
  27. const AVClass *class;
  28. int co[4]; /* color offsets rgba */
  29. int alpha;
  30. int type;
  31. float spillmix;
  32. float spillexpand;
  33. float redscale;
  34. float greenscale;
  35. float bluescale;
  36. float brightness;
  37. } DespillContext;
  38. static int do_despill_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  39. {
  40. DespillContext *s = ctx->priv;
  41. AVFrame *frame = arg;
  42. const int ro = s->co[0], go = s->co[1], bo = s->co[2], ao = s->co[3];
  43. const int slice_start = (frame->height * jobnr) / nb_jobs;
  44. const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
  45. const float brightness = s->brightness;
  46. const float redscale = s->redscale;
  47. const float greenscale = s->greenscale;
  48. const float bluescale = s->bluescale;
  49. const float spillmix = s->spillmix;
  50. const float factor = (1.f - spillmix) * (1.f - s->spillexpand);
  51. float red, green, blue;
  52. int x, y;
  53. for (y = slice_start; y < slice_end; y++) {
  54. uint8_t *dst = frame->data[0] + y * frame->linesize[0];
  55. for (x = 0; x < frame->width; x++) {
  56. float spillmap;
  57. red = dst[x * 4 + ro] / 255.f;
  58. green = dst[x * 4 + go] / 255.f;
  59. blue = dst[x * 4 + bo] / 255.f;
  60. if (s->type) {
  61. spillmap = FFMAX(blue - (red * spillmix + green * factor), 0.f);
  62. } else {
  63. spillmap = FFMAX(green - (red * spillmix + blue * factor), 0.f);
  64. }
  65. red = FFMAX(red + spillmap * redscale + brightness * spillmap, 0.f);
  66. green = FFMAX(green + spillmap * greenscale + brightness * spillmap, 0.f);
  67. blue = FFMAX(blue + spillmap * bluescale + brightness * spillmap, 0.f);
  68. dst[x * 4 + ro] = av_clip_uint8(red * 255);
  69. dst[x * 4 + go] = av_clip_uint8(green * 255);
  70. dst[x * 4 + bo] = av_clip_uint8(blue * 255);
  71. if (s->alpha) {
  72. spillmap = 1.f - spillmap;
  73. dst[x * 4 + ao] = av_clip_uint8(spillmap * 255);
  74. }
  75. }
  76. }
  77. return 0;
  78. }
  79. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  80. {
  81. AVFilterContext *ctx = link->dst;
  82. int ret;
  83. if (ret = av_frame_make_writable(frame))
  84. return ret;
  85. if (ret = ctx->internal->execute(ctx, do_despill_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(ctx))))
  86. return ret;
  87. return ff_filter_frame(ctx->outputs[0], frame);
  88. }
  89. static av_cold int config_output(AVFilterLink *outlink)
  90. {
  91. AVFilterContext *ctx = outlink->src;
  92. DespillContext *s = ctx->priv;
  93. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  94. int i;
  95. for (i = 0; i < 4; ++i)
  96. s->co[i] = desc->comp[i].offset;
  97. return 0;
  98. }
  99. static av_cold int query_formats(AVFilterContext *ctx)
  100. {
  101. static const enum AVPixelFormat pixel_fmts[] = {
  102. AV_PIX_FMT_ARGB,
  103. AV_PIX_FMT_RGBA,
  104. AV_PIX_FMT_ABGR,
  105. AV_PIX_FMT_BGRA,
  106. AV_PIX_FMT_NONE
  107. };
  108. AVFilterFormats *formats = NULL;
  109. formats = ff_make_format_list(pixel_fmts);
  110. if (!formats)
  111. return AVERROR(ENOMEM);
  112. return ff_set_common_formats(ctx, formats);
  113. }
  114. static const AVFilterPad despill_inputs[] = {
  115. {
  116. .name = "default",
  117. .type = AVMEDIA_TYPE_VIDEO,
  118. .filter_frame = filter_frame,
  119. },
  120. { NULL }
  121. };
  122. static const AVFilterPad despill_outputs[] = {
  123. {
  124. .name = "default",
  125. .type = AVMEDIA_TYPE_VIDEO,
  126. .config_props = config_output,
  127. },
  128. { NULL }
  129. };
  130. #define OFFSET(x) offsetof(DespillContext, x)
  131. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  132. static const AVOption despill_options[] = {
  133. { "type", "set the screen type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "type" },
  134. { "green", "greenscreen", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
  135. { "blue", "bluescreen", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
  136. { "mix", "set the spillmap mix", OFFSET(spillmix), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  137. { "expand", "set the spillmap expand", OFFSET(spillexpand), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, FLAGS },
  138. { "red", "set red scale", OFFSET(redscale), AV_OPT_TYPE_FLOAT, {.dbl=0}, -100, 100, FLAGS },
  139. { "green", "set green scale", OFFSET(greenscale), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -100, 100, FLAGS },
  140. { "blue", "set blue scale", OFFSET(bluescale), AV_OPT_TYPE_FLOAT, {.dbl=0}, -100, 100, FLAGS },
  141. { "brightness", "set brightness", OFFSET(brightness), AV_OPT_TYPE_FLOAT, {.dbl=0}, -10, 10, FLAGS },
  142. { "alpha", "change alpha component", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  143. { NULL }
  144. };
  145. AVFILTER_DEFINE_CLASS(despill);
  146. AVFilter ff_vf_despill = {
  147. .name = "despill",
  148. .description = NULL_IF_CONFIG_SMALL("Despill video."),
  149. .priv_size = sizeof(DespillContext),
  150. .priv_class = &despill_class,
  151. .query_formats = query_formats,
  152. .inputs = despill_inputs,
  153. .outputs = despill_outputs,
  154. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  155. };