vf_colorkey.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2015 Timo Rothenpieler <timo@rothenpieler.org>
  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 ColorkeyContext {
  27. const AVClass *class;
  28. /* color offsets rgba */
  29. int co[4];
  30. uint8_t colorkey_rgba[4];
  31. float similarity;
  32. float blend;
  33. } ColorkeyContext;
  34. static uint8_t do_colorkey_pixel(ColorkeyContext *ctx, uint8_t r, uint8_t g, uint8_t b)
  35. {
  36. int dr = (int)r - ctx->colorkey_rgba[0];
  37. int dg = (int)g - ctx->colorkey_rgba[1];
  38. int db = (int)b - ctx->colorkey_rgba[2];
  39. double diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0));
  40. if (ctx->blend > 0.0001) {
  41. return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
  42. } else {
  43. return (diff > ctx->similarity) ? 255 : 0;
  44. }
  45. }
  46. static int do_colorkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  47. {
  48. AVFrame *frame = arg;
  49. const int slice_start = (frame->height * jobnr) / nb_jobs;
  50. const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
  51. ColorkeyContext *ctx = avctx->priv;
  52. int o, x, y;
  53. for (y = slice_start; y < slice_end; ++y) {
  54. for (x = 0; x < frame->width; ++x) {
  55. o = frame->linesize[0] * y + x * 4;
  56. frame->data[0][o + ctx->co[3]] =
  57. do_colorkey_pixel(ctx,
  58. frame->data[0][o + ctx->co[0]],
  59. frame->data[0][o + ctx->co[1]],
  60. frame->data[0][o + ctx->co[2]]);
  61. }
  62. }
  63. return 0;
  64. }
  65. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  66. {
  67. AVFilterContext *avctx = link->dst;
  68. int res;
  69. if (res = av_frame_make_writable(frame))
  70. return res;
  71. if (res = avctx->internal->execute(avctx, do_colorkey_slice, frame, NULL, FFMIN(frame->height, avctx->graph->nb_threads)))
  72. return res;
  73. return ff_filter_frame(avctx->outputs[0], frame);
  74. }
  75. static av_cold int config_output(AVFilterLink *outlink)
  76. {
  77. AVFilterContext *avctx = outlink->src;
  78. ColorkeyContext *ctx = avctx->priv;
  79. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  80. int i;
  81. outlink->w = avctx->inputs[0]->w;
  82. outlink->h = avctx->inputs[0]->h;
  83. outlink->time_base = avctx->inputs[0]->time_base;
  84. for (i = 0; i < 4; ++i)
  85. ctx->co[i] = desc->comp[i].offset;
  86. return 0;
  87. }
  88. static av_cold int query_formats(AVFilterContext *avctx)
  89. {
  90. static const enum AVPixelFormat pixel_fmts[] = {
  91. AV_PIX_FMT_ARGB,
  92. AV_PIX_FMT_RGBA,
  93. AV_PIX_FMT_ABGR,
  94. AV_PIX_FMT_BGRA,
  95. AV_PIX_FMT_NONE
  96. };
  97. AVFilterFormats *formats = NULL;
  98. formats = ff_make_format_list(pixel_fmts);
  99. if (!formats)
  100. return AVERROR(ENOMEM);
  101. return ff_set_common_formats(avctx, formats);
  102. }
  103. static const AVFilterPad colorkey_inputs[] = {
  104. {
  105. .name = "default",
  106. .type = AVMEDIA_TYPE_VIDEO,
  107. .filter_frame = filter_frame,
  108. },
  109. { NULL }
  110. };
  111. static const AVFilterPad colorkey_outputs[] = {
  112. {
  113. .name = "default",
  114. .type = AVMEDIA_TYPE_VIDEO,
  115. .config_props = config_output,
  116. },
  117. { NULL }
  118. };
  119. #define OFFSET(x) offsetof(ColorkeyContext, x)
  120. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  121. static const AVOption colorkey_options[] = {
  122. { "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  123. { "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
  124. { "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
  125. { NULL }
  126. };
  127. AVFILTER_DEFINE_CLASS(colorkey);
  128. AVFilter ff_vf_colorkey = {
  129. .name = "colorkey",
  130. .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
  131. .priv_size = sizeof(ColorkeyContext),
  132. .priv_class = &colorkey_class,
  133. .query_formats = query_formats,
  134. .inputs = colorkey_inputs,
  135. .outputs = colorkey_outputs,
  136. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  137. };