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.

301 lines
9.3KB

  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 (alpha) {
  103. frame->data[1][frame->linesize[1] * y + x] = 128;
  104. frame->data[2][frame->linesize[2] * y + x] = 128;
  105. }
  106. }
  107. }
  108. return 0;
  109. }
  110. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  111. {
  112. AVFilterContext *avctx = link->dst;
  113. ChromakeyContext *ctx = avctx->priv;
  114. int res;
  115. if (res = avctx->internal->execute(avctx, ctx->do_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
  116. return res;
  117. return ff_filter_frame(avctx->outputs[0], frame);
  118. }
  119. #define FIXNUM(x) lrint((x) * (1 << 10))
  120. #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)
  121. #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)
  122. static av_cold int initialize_chromakey(AVFilterContext *avctx)
  123. {
  124. ChromakeyContext *ctx = avctx->priv;
  125. if (ctx->is_yuv) {
  126. ctx->chromakey_uv[0] = ctx->chromakey_rgba[1];
  127. ctx->chromakey_uv[1] = ctx->chromakey_rgba[2];
  128. } else {
  129. ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba);
  130. ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba);
  131. }
  132. if (!strcmp(avctx->filter->name, "chromakey")) {
  133. ctx->do_slice = do_chromakey_slice;
  134. } else {
  135. ctx->do_slice = do_chromahold_slice;
  136. }
  137. return 0;
  138. }
  139. static av_cold int query_formats(AVFilterContext *avctx)
  140. {
  141. static const enum AVPixelFormat pixel_fmts[] = {
  142. AV_PIX_FMT_YUVA420P,
  143. AV_PIX_FMT_YUVA422P,
  144. AV_PIX_FMT_YUVA444P,
  145. AV_PIX_FMT_NONE
  146. };
  147. static const enum AVPixelFormat hold_pixel_fmts[] = {
  148. AV_PIX_FMT_YUV420P,
  149. AV_PIX_FMT_YUV422P,
  150. AV_PIX_FMT_YUV444P,
  151. AV_PIX_FMT_YUVA420P,
  152. AV_PIX_FMT_YUVA422P,
  153. AV_PIX_FMT_YUVA444P,
  154. AV_PIX_FMT_NONE
  155. };
  156. AVFilterFormats *formats = NULL;
  157. formats = ff_make_format_list(!strcmp(avctx->filter->name, "chromahold") ? hold_pixel_fmts : pixel_fmts);
  158. if (!formats)
  159. return AVERROR(ENOMEM);
  160. return ff_set_common_formats(avctx, formats);
  161. }
  162. static av_cold int config_input(AVFilterLink *inlink)
  163. {
  164. AVFilterContext *avctx = inlink->dst;
  165. ChromakeyContext *ctx = avctx->priv;
  166. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  167. ctx->hsub_log2 = desc->log2_chroma_w;
  168. ctx->vsub_log2 = desc->log2_chroma_h;
  169. return 0;
  170. }
  171. static const AVFilterPad chromakey_inputs[] = {
  172. {
  173. .name = "default",
  174. .type = AVMEDIA_TYPE_VIDEO,
  175. .needs_writable = 1,
  176. .filter_frame = filter_frame,
  177. .config_props = config_input,
  178. },
  179. { NULL }
  180. };
  181. static const AVFilterPad chromakey_outputs[] = {
  182. {
  183. .name = "default",
  184. .type = AVMEDIA_TYPE_VIDEO,
  185. },
  186. { NULL }
  187. };
  188. #define OFFSET(x) offsetof(ChromakeyContext, x)
  189. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  190. static const AVOption chromakey_options[] = {
  191. { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  192. { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
  193. { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
  194. { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  195. { NULL }
  196. };
  197. AVFILTER_DEFINE_CLASS(chromakey);
  198. AVFilter ff_vf_chromakey = {
  199. .name = "chromakey",
  200. .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
  201. .priv_size = sizeof(ChromakeyContext),
  202. .priv_class = &chromakey_class,
  203. .init = initialize_chromakey,
  204. .query_formats = query_formats,
  205. .inputs = chromakey_inputs,
  206. .outputs = chromakey_outputs,
  207. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  208. };
  209. static const AVOption chromahold_options[] = {
  210. { "color", "set the chromahold key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  211. { "similarity", "set the chromahold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
  212. { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  213. { NULL }
  214. };
  215. static const AVFilterPad chromahold_inputs[] = {
  216. {
  217. .name = "default",
  218. .type = AVMEDIA_TYPE_VIDEO,
  219. .needs_writable = 1,
  220. .filter_frame = filter_frame,
  221. .config_props = config_input,
  222. },
  223. { NULL }
  224. };
  225. static const AVFilterPad chromahold_outputs[] = {
  226. {
  227. .name = "default",
  228. .type = AVMEDIA_TYPE_VIDEO,
  229. },
  230. { NULL }
  231. };
  232. AVFILTER_DEFINE_CLASS(chromahold);
  233. AVFilter ff_vf_chromahold = {
  234. .name = "chromahold",
  235. .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray."),
  236. .priv_size = sizeof(ChromakeyContext),
  237. .priv_class = &chromahold_class,
  238. .init = initialize_chromakey,
  239. .query_formats = query_formats,
  240. .inputs = chromahold_inputs,
  241. .outputs = chromahold_outputs,
  242. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  243. };