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.

200 lines
6.1KB

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