vf_chromakey.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. int (*do_slice)(AVFilterContext *ctx, void *arg,
  36. int jobnr, int nb_jobs);
  37. } ChromakeyContext;
  38. static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
  39. {
  40. double diff = 0.0;
  41. int du, dv, i;
  42. for (i = 0; i < 9; ++i) {
  43. du = (int)u[i] - ctx->chromakey_uv[0];
  44. dv = (int)v[i] - ctx->chromakey_uv[1];
  45. diff += sqrt((du * du + dv * dv) / (255.0 * 255.0));
  46. }
  47. diff /= 9.0;
  48. if (ctx->blend > 0.0001) {
  49. return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
  50. } else {
  51. return (diff > ctx->similarity) ? 255 : 0;
  52. }
  53. }
  54. 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)
  55. {
  56. if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
  57. return;
  58. x >>= hsub_log2;
  59. y >>= vsub_log2;
  60. *u = frame->data[1][frame->linesize[1] * y + x];
  61. *v = frame->data[2][frame->linesize[2] * y + x];
  62. }
  63. static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  64. {
  65. AVFrame *frame = arg;
  66. const int slice_start = (frame->height * jobnr) / nb_jobs;
  67. const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
  68. ChromakeyContext *ctx = avctx->priv;
  69. int x, y, xo, yo;
  70. uint8_t u[9], v[9];
  71. memset(u, ctx->chromakey_uv[0], sizeof(u));
  72. memset(v, ctx->chromakey_uv[1], sizeof(v));
  73. for (y = slice_start; y < slice_end; ++y) {
  74. for (x = 0; x < frame->width; ++x) {
  75. for (yo = 0; yo < 3; ++yo) {
  76. for (xo = 0; xo < 3; ++xo) {
  77. get_pixel_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
  78. }
  79. }
  80. frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v);
  81. }
  82. }
  83. return 0;
  84. }
  85. static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  86. {
  87. ChromakeyContext *ctx = avctx->priv;
  88. AVFrame *frame = arg;
  89. const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
  90. const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
  91. int x, y, alpha;
  92. for (y = slice_start; y < slice_end; ++y) {
  93. for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
  94. int u = frame->data[1][frame->linesize[1] * y + x];
  95. int v = frame->data[2][frame->linesize[2] * y + x];
  96. double diff;
  97. int du, dv;
  98. du = u - ctx->chromakey_uv[0];
  99. dv = v - ctx->chromakey_uv[1];
  100. diff = sqrt((du * du + dv * dv) / (255.0 * 255.0));
  101. alpha = diff > ctx->similarity;
  102. if (ctx->blend > 0.0001) {
  103. double f = 1. - av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0);
  104. frame->data[1][frame->linesize[1] * y + x] = 128 + (u - 128) * f;
  105. frame->data[2][frame->linesize[2] * y + x] = 128 + (v - 128) * f;
  106. } else if (alpha) {
  107. frame->data[1][frame->linesize[1] * y + x] = 128;
  108. frame->data[2][frame->linesize[2] * y + x] = 128;
  109. }
  110. }
  111. }
  112. return 0;
  113. }
  114. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  115. {
  116. AVFilterContext *avctx = link->dst;
  117. ChromakeyContext *ctx = avctx->priv;
  118. int res;
  119. if (res = avctx->internal->execute(avctx, ctx->do_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
  120. return res;
  121. return ff_filter_frame(avctx->outputs[0], frame);
  122. }
  123. #define FIXNUM(x) lrint((x) * (1 << 10))
  124. #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)
  125. #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)
  126. static av_cold int initialize_chromakey(AVFilterContext *avctx)
  127. {
  128. ChromakeyContext *ctx = avctx->priv;
  129. if (ctx->is_yuv) {
  130. ctx->chromakey_uv[0] = ctx->chromakey_rgba[1];
  131. ctx->chromakey_uv[1] = ctx->chromakey_rgba[2];
  132. } else {
  133. ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba);
  134. ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba);
  135. }
  136. if (!strcmp(avctx->filter->name, "chromakey")) {
  137. ctx->do_slice = do_chromakey_slice;
  138. } else {
  139. ctx->do_slice = do_chromahold_slice;
  140. }
  141. return 0;
  142. }
  143. static av_cold int query_formats(AVFilterContext *avctx)
  144. {
  145. static const enum AVPixelFormat pixel_fmts[] = {
  146. AV_PIX_FMT_YUVA420P,
  147. AV_PIX_FMT_YUVA422P,
  148. AV_PIX_FMT_YUVA444P,
  149. AV_PIX_FMT_NONE
  150. };
  151. static const enum AVPixelFormat hold_pixel_fmts[] = {
  152. AV_PIX_FMT_YUV420P,
  153. AV_PIX_FMT_YUV422P,
  154. AV_PIX_FMT_YUV444P,
  155. AV_PIX_FMT_YUVA420P,
  156. AV_PIX_FMT_YUVA422P,
  157. AV_PIX_FMT_YUVA444P,
  158. AV_PIX_FMT_NONE
  159. };
  160. AVFilterFormats *formats = NULL;
  161. formats = ff_make_format_list(!strcmp(avctx->filter->name, "chromahold") ? hold_pixel_fmts : pixel_fmts);
  162. if (!formats)
  163. return AVERROR(ENOMEM);
  164. return ff_set_common_formats(avctx, formats);
  165. }
  166. static av_cold int config_input(AVFilterLink *inlink)
  167. {
  168. AVFilterContext *avctx = inlink->dst;
  169. ChromakeyContext *ctx = avctx->priv;
  170. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  171. ctx->hsub_log2 = desc->log2_chroma_w;
  172. ctx->vsub_log2 = desc->log2_chroma_h;
  173. return 0;
  174. }
  175. static const AVFilterPad chromakey_inputs[] = {
  176. {
  177. .name = "default",
  178. .type = AVMEDIA_TYPE_VIDEO,
  179. .needs_writable = 1,
  180. .filter_frame = filter_frame,
  181. .config_props = config_input,
  182. },
  183. { NULL }
  184. };
  185. static const AVFilterPad chromakey_outputs[] = {
  186. {
  187. .name = "default",
  188. .type = AVMEDIA_TYPE_VIDEO,
  189. },
  190. { NULL }
  191. };
  192. #define OFFSET(x) offsetof(ChromakeyContext, x)
  193. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  194. static const AVOption chromakey_options[] = {
  195. { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  196. { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
  197. { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
  198. { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  199. { NULL }
  200. };
  201. AVFILTER_DEFINE_CLASS(chromakey);
  202. AVFilter ff_vf_chromakey = {
  203. .name = "chromakey",
  204. .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
  205. .priv_size = sizeof(ChromakeyContext),
  206. .priv_class = &chromakey_class,
  207. .init = initialize_chromakey,
  208. .query_formats = query_formats,
  209. .inputs = chromakey_inputs,
  210. .outputs = chromakey_outputs,
  211. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  212. };
  213. static const AVOption chromahold_options[] = {
  214. { "color", "set the chromahold key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  215. { "similarity", "set the chromahold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
  216. { "blend", "set the chromahold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
  217. { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  218. { NULL }
  219. };
  220. static const AVFilterPad chromahold_inputs[] = {
  221. {
  222. .name = "default",
  223. .type = AVMEDIA_TYPE_VIDEO,
  224. .needs_writable = 1,
  225. .filter_frame = filter_frame,
  226. .config_props = config_input,
  227. },
  228. { NULL }
  229. };
  230. static const AVFilterPad chromahold_outputs[] = {
  231. {
  232. .name = "default",
  233. .type = AVMEDIA_TYPE_VIDEO,
  234. },
  235. { NULL }
  236. };
  237. AVFILTER_DEFINE_CLASS(chromahold);
  238. AVFilter ff_vf_chromahold = {
  239. .name = "chromahold",
  240. .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray."),
  241. .priv_size = sizeof(ChromakeyContext),
  242. .priv_class = &chromahold_class,
  243. .init = initialize_chromakey,
  244. .query_formats = query_formats,
  245. .inputs = chromahold_inputs,
  246. .outputs = chromahold_outputs,
  247. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  248. };