vf_chromakey.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 ChromakeyContext {
  27. const AVClass *class;
  28. uint8_t chromakey_rgba[4];
  29. uint8_t chromakey_uv[2];
  30. float similarity;
  31. float blend;
  32. int is_yuv;
  33. int hsub_log2;
  34. int vsub_log2;
  35. } ChromakeyContext;
  36. static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
  37. {
  38. double diff = 0.0;
  39. int du, dv, i;
  40. for (i = 0; i < 9; ++i) {
  41. du = (int)u[i] - ctx->chromakey_uv[0];
  42. dv = (int)v[i] - ctx->chromakey_uv[1];
  43. diff += sqrt((du * du + dv * dv) / (255.0 * 255.0));
  44. }
  45. diff /= 9.0;
  46. if (ctx->blend > 0.0001) {
  47. return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
  48. } else {
  49. return (diff > ctx->similarity) ? 255 : 0;
  50. }
  51. }
  52. static av_always_inline void get_pixel_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint8_t *u, uint8_t *v)
  53. {
  54. if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
  55. return;
  56. x >>= hsub_log2;
  57. y >>= vsub_log2;
  58. *u = frame->data[1][frame->linesize[1] * y + x];
  59. *v = frame->data[2][frame->linesize[2] * y + x];
  60. }
  61. static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  62. {
  63. AVFrame *frame = arg;
  64. const int slice_start = (frame->height * jobnr) / nb_jobs;
  65. const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
  66. ChromakeyContext *ctx = avctx->priv;
  67. int x, y, xo, yo;
  68. uint8_t u[9], v[9];
  69. memset(u, ctx->chromakey_uv[0], sizeof(u));
  70. memset(v, ctx->chromakey_uv[1], sizeof(v));
  71. for (y = slice_start; y < slice_end; ++y) {
  72. for (x = 0; x < frame->width; ++x) {
  73. for (yo = 0; yo < 3; ++yo) {
  74. for (xo = 0; xo < 3; ++xo) {
  75. get_pixel_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
  76. }
  77. }
  78. frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v);
  79. }
  80. }
  81. return 0;
  82. }
  83. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  84. {
  85. AVFilterContext *avctx = link->dst;
  86. int res;
  87. if (res = avctx->internal->execute(avctx, do_chromakey_slice, frame, NULL, FFMIN(frame->height, avctx->graph->nb_threads)))
  88. return res;
  89. return ff_filter_frame(avctx->outputs[0], frame);
  90. }
  91. #define FIXNUM(x) lrint((x) * (1 << 10))
  92. #define RGB_TO_U(rgb) (((- FIXNUM(0.16874) * rgb[0] - FIXNUM(0.33126) * rgb[1] + FIXNUM(0.50000) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
  93. #define RGB_TO_V(rgb) ((( FIXNUM(0.50000) * rgb[0] - FIXNUM(0.41869) * rgb[1] - FIXNUM(0.08131) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
  94. static av_cold int initialize_chromakey(AVFilterContext *avctx)
  95. {
  96. ChromakeyContext *ctx = avctx->priv;
  97. if (ctx->is_yuv) {
  98. ctx->chromakey_uv[0] = ctx->chromakey_rgba[1];
  99. ctx->chromakey_uv[1] = ctx->chromakey_rgba[2];
  100. } else {
  101. ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba);
  102. ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba);
  103. }
  104. return 0;
  105. }
  106. static av_cold int query_formats(AVFilterContext *avctx)
  107. {
  108. static const enum AVPixelFormat pixel_fmts[] = {
  109. AV_PIX_FMT_YUVA420P,
  110. AV_PIX_FMT_YUVA422P,
  111. AV_PIX_FMT_YUVA444P,
  112. AV_PIX_FMT_NONE
  113. };
  114. AVFilterFormats *formats = NULL;
  115. formats = ff_make_format_list(pixel_fmts);
  116. if (!formats)
  117. return AVERROR(ENOMEM);
  118. return ff_set_common_formats(avctx, formats);
  119. }
  120. static av_cold int config_input(AVFilterLink *inlink)
  121. {
  122. AVFilterContext *avctx = inlink->dst;
  123. ChromakeyContext *ctx = avctx->priv;
  124. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  125. ctx->hsub_log2 = desc->log2_chroma_w;
  126. ctx->vsub_log2 = desc->log2_chroma_h;
  127. return 0;
  128. }
  129. static const AVFilterPad chromakey_inputs[] = {
  130. {
  131. .name = "default",
  132. .type = AVMEDIA_TYPE_VIDEO,
  133. .needs_writable = 1,
  134. .filter_frame = filter_frame,
  135. .config_props = config_input,
  136. },
  137. { NULL }
  138. };
  139. static const AVFilterPad chromakey_outputs[] = {
  140. {
  141. .name = "default",
  142. .type = AVMEDIA_TYPE_VIDEO,
  143. },
  144. { NULL }
  145. };
  146. #define OFFSET(x) offsetof(ChromakeyContext, x)
  147. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  148. static const AVOption chromakey_options[] = {
  149. { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  150. { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
  151. { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
  152. { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  153. { NULL }
  154. };
  155. AVFILTER_DEFINE_CLASS(chromakey);
  156. AVFilter ff_vf_chromakey = {
  157. .name = "chromakey",
  158. .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
  159. .priv_size = sizeof(ChromakeyContext),
  160. .priv_class = &chromakey_class,
  161. .init = initialize_chromakey,
  162. .query_formats = query_formats,
  163. .inputs = chromakey_inputs,
  164. .outputs = chromakey_outputs,
  165. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  166. };