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.

454 lines
15KB

  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 "libavutil/intreadwrite.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. typedef struct ChromakeyContext {
  28. const AVClass *class;
  29. uint8_t chromakey_rgba[4];
  30. uint16_t chromakey_uv[2];
  31. float similarity;
  32. float blend;
  33. int is_yuv;
  34. int depth;
  35. int mid;
  36. int max;
  37. int hsub_log2;
  38. int vsub_log2;
  39. int (*do_slice)(AVFilterContext *ctx, void *arg,
  40. int jobnr, int nb_jobs);
  41. } ChromakeyContext;
  42. static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
  43. {
  44. double diff = 0.0;
  45. int du, dv, i;
  46. for (i = 0; i < 9; ++i) {
  47. du = (int)u[i] - ctx->chromakey_uv[0];
  48. dv = (int)v[i] - ctx->chromakey_uv[1];
  49. diff += sqrt((du * du + dv * dv) / (255.0 * 255.0 * 2));
  50. }
  51. diff /= 9.0;
  52. if (ctx->blend > 0.0001) {
  53. return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
  54. } else {
  55. return (diff > ctx->similarity) ? 255 : 0;
  56. }
  57. }
  58. static uint16_t do_chromakey_pixel16(ChromakeyContext *ctx, uint16_t u[9], uint16_t v[9])
  59. {
  60. double max = ctx->max;
  61. double diff = 0.0;
  62. int du, dv, i;
  63. for (i = 0; i < 9; ++i) {
  64. du = (int)u[i] - ctx->chromakey_uv[0];
  65. dv = (int)v[i] - ctx->chromakey_uv[1];
  66. diff += sqrt((du * du + dv * dv) / (max * max * 2));
  67. }
  68. diff /= 9.0;
  69. if (ctx->blend > 0.0001) {
  70. return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * max;
  71. } else {
  72. return (diff > ctx->similarity) ? max : 0;
  73. }
  74. }
  75. 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)
  76. {
  77. if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
  78. return;
  79. x >>= hsub_log2;
  80. y >>= vsub_log2;
  81. *u = frame->data[1][frame->linesize[1] * y + x];
  82. *v = frame->data[2][frame->linesize[2] * y + x];
  83. }
  84. static av_always_inline void get_pixel16_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint16_t *u, uint16_t *v)
  85. {
  86. if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
  87. return;
  88. x >>= hsub_log2;
  89. y >>= vsub_log2;
  90. *u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]);
  91. *v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]);
  92. }
  93. static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  94. {
  95. AVFrame *frame = arg;
  96. const int slice_start = (frame->height * jobnr) / nb_jobs;
  97. const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
  98. ChromakeyContext *ctx = avctx->priv;
  99. int x, y, xo, yo;
  100. uint8_t u[9], v[9];
  101. memset(u, ctx->chromakey_uv[0], sizeof(u));
  102. memset(v, ctx->chromakey_uv[1], sizeof(v));
  103. for (y = slice_start; y < slice_end; ++y) {
  104. for (x = 0; x < frame->width; ++x) {
  105. for (yo = 0; yo < 3; ++yo) {
  106. for (xo = 0; xo < 3; ++xo) {
  107. get_pixel_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
  108. }
  109. }
  110. frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v);
  111. }
  112. }
  113. return 0;
  114. }
  115. static int do_chromakey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  116. {
  117. AVFrame *frame = arg;
  118. const int slice_start = (frame->height * jobnr) / nb_jobs;
  119. const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
  120. ChromakeyContext *ctx = avctx->priv;
  121. int x, y, xo, yo;
  122. uint16_t u[9], v[9];
  123. for (int i = 0; i < 9; i++) {
  124. u[i] = ctx->chromakey_uv[0];
  125. v[i] = ctx->chromakey_uv[1];
  126. }
  127. for (y = slice_start; y < slice_end; ++y) {
  128. for (x = 0; x < frame->width; ++x) {
  129. uint16_t *dst = (uint16_t *)(frame->data[3] + frame->linesize[3] * y);
  130. for (yo = 0; yo < 3; ++yo) {
  131. for (xo = 0; xo < 3; ++xo) {
  132. get_pixel16_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
  133. }
  134. }
  135. dst[x] = do_chromakey_pixel16(ctx, u, v);
  136. }
  137. }
  138. return 0;
  139. }
  140. static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  141. {
  142. ChromakeyContext *ctx = avctx->priv;
  143. AVFrame *frame = arg;
  144. const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
  145. const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
  146. int x, y, alpha;
  147. for (y = slice_start; y < slice_end; ++y) {
  148. for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
  149. int u = frame->data[1][frame->linesize[1] * y + x];
  150. int v = frame->data[2][frame->linesize[2] * y + x];
  151. double diff;
  152. int du, dv;
  153. du = u - ctx->chromakey_uv[0];
  154. dv = v - ctx->chromakey_uv[1];
  155. diff = sqrt((du * du + dv * dv) / (255.0 * 255.0));
  156. alpha = diff > ctx->similarity;
  157. if (ctx->blend > 0.0001) {
  158. double f = 1. - av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0);
  159. frame->data[1][frame->linesize[1] * y + x] = 128 + (u - 128) * f;
  160. frame->data[2][frame->linesize[2] * y + x] = 128 + (v - 128) * f;
  161. } else if (alpha) {
  162. frame->data[1][frame->linesize[1] * y + x] = 128;
  163. frame->data[2][frame->linesize[2] * y + x] = 128;
  164. }
  165. }
  166. }
  167. return 0;
  168. }
  169. static int do_chromahold16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  170. {
  171. ChromakeyContext *ctx = avctx->priv;
  172. AVFrame *frame = arg;
  173. const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
  174. const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
  175. const int mid = ctx->mid;
  176. double max = ctx->max;
  177. int x, y, alpha;
  178. for (y = slice_start; y < slice_end; ++y) {
  179. for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
  180. int u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]);
  181. int v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]);
  182. double diff;
  183. int du, dv;
  184. du = u - ctx->chromakey_uv[0];
  185. dv = v - ctx->chromakey_uv[1];
  186. diff = sqrt((du * du + dv * dv) / (max * max));
  187. alpha = diff > ctx->similarity;
  188. if (ctx->blend > 0.0001) {
  189. double f = 1. - av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0);
  190. AV_WN16(&frame->data[1][frame->linesize[1] * y + 2 * x], mid + (u - mid) * f);
  191. AV_WN16(&frame->data[2][frame->linesize[2] * y + 2 * x], mid + (v - mid) * f);
  192. } else if (alpha) {
  193. AV_WN16(&frame->data[1][frame->linesize[1] * y + 2 * x], mid);
  194. AV_WN16(&frame->data[2][frame->linesize[2] * y + 2 * x], mid);
  195. }
  196. }
  197. }
  198. return 0;
  199. }
  200. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  201. {
  202. AVFilterContext *avctx = link->dst;
  203. ChromakeyContext *ctx = avctx->priv;
  204. int res;
  205. if (res = avctx->internal->execute(avctx, ctx->do_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
  206. return res;
  207. return ff_filter_frame(avctx->outputs[0], frame);
  208. }
  209. #define FIXNUM(x) lrint((x) * (1 << 10))
  210. #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)
  211. #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)
  212. static av_cold int config_output(AVFilterLink *outlink)
  213. {
  214. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  215. AVFilterContext *avctx = outlink->src;
  216. ChromakeyContext *ctx = avctx->priv;
  217. int factor;
  218. ctx->depth = desc->comp[0].depth;
  219. ctx->mid = 1 << (ctx->depth - 1);
  220. ctx->max = (1 << ctx->depth) - 1;
  221. factor = 1 << (ctx->depth - 8);
  222. if (ctx->is_yuv) {
  223. ctx->chromakey_uv[0] = ctx->chromakey_rgba[1] * factor;
  224. ctx->chromakey_uv[1] = ctx->chromakey_rgba[2] * factor;
  225. } else {
  226. ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba) * factor;
  227. ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba) * factor;
  228. }
  229. if (!strcmp(avctx->filter->name, "chromakey")) {
  230. ctx->do_slice = ctx->depth <= 8 ? do_chromakey_slice : do_chromakey16_slice;
  231. } else {
  232. ctx->do_slice = ctx->depth <= 8 ? do_chromahold_slice: do_chromahold16_slice;
  233. }
  234. return 0;
  235. }
  236. static av_cold int query_formats(AVFilterContext *avctx)
  237. {
  238. static const enum AVPixelFormat pixel_fmts[] = {
  239. AV_PIX_FMT_YUVA420P,
  240. AV_PIX_FMT_YUVA422P,
  241. AV_PIX_FMT_YUVA444P,
  242. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  243. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  244. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  245. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  246. AV_PIX_FMT_NONE
  247. };
  248. static const enum AVPixelFormat hold_pixel_fmts[] = {
  249. AV_PIX_FMT_YUV420P,
  250. AV_PIX_FMT_YUV422P,
  251. AV_PIX_FMT_YUV444P,
  252. AV_PIX_FMT_YUVA420P,
  253. AV_PIX_FMT_YUVA422P,
  254. AV_PIX_FMT_YUVA444P,
  255. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  256. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  257. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  258. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  259. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  260. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  261. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  262. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  263. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  264. AV_PIX_FMT_NONE
  265. };
  266. AVFilterFormats *formats = NULL;
  267. formats = ff_make_format_list(!strcmp(avctx->filter->name, "chromahold") ? hold_pixel_fmts : pixel_fmts);
  268. if (!formats)
  269. return AVERROR(ENOMEM);
  270. return ff_set_common_formats(avctx, formats);
  271. }
  272. static av_cold int config_input(AVFilterLink *inlink)
  273. {
  274. AVFilterContext *avctx = inlink->dst;
  275. ChromakeyContext *ctx = avctx->priv;
  276. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  277. ctx->hsub_log2 = desc->log2_chroma_w;
  278. ctx->vsub_log2 = desc->log2_chroma_h;
  279. return 0;
  280. }
  281. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  282. char *res, int res_len, int flags)
  283. {
  284. int ret;
  285. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  286. if (ret < 0)
  287. return ret;
  288. return config_output(ctx->outputs[0]);
  289. }
  290. static const AVFilterPad chromakey_inputs[] = {
  291. {
  292. .name = "default",
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .needs_writable = 1,
  295. .filter_frame = filter_frame,
  296. .config_props = config_input,
  297. },
  298. { NULL }
  299. };
  300. static const AVFilterPad chromakey_outputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .config_props = config_output,
  305. },
  306. { NULL }
  307. };
  308. #define OFFSET(x) offsetof(ChromakeyContext, x)
  309. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  310. static const AVOption chromakey_options[] = {
  311. { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
  312. { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
  313. { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
  314. { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  315. { NULL }
  316. };
  317. AVFILTER_DEFINE_CLASS(chromakey);
  318. AVFilter ff_vf_chromakey = {
  319. .name = "chromakey",
  320. .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
  321. .priv_size = sizeof(ChromakeyContext),
  322. .priv_class = &chromakey_class,
  323. .query_formats = query_formats,
  324. .inputs = chromakey_inputs,
  325. .outputs = chromakey_outputs,
  326. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  327. .process_command = process_command,
  328. };
  329. static const AVOption chromahold_options[] = {
  330. { "color", "set the chromahold key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
  331. { "similarity", "set the chromahold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
  332. { "blend", "set the chromahold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
  333. { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  334. { NULL }
  335. };
  336. static const AVFilterPad chromahold_inputs[] = {
  337. {
  338. .name = "default",
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .needs_writable = 1,
  341. .filter_frame = filter_frame,
  342. .config_props = config_input,
  343. },
  344. { NULL }
  345. };
  346. static const AVFilterPad chromahold_outputs[] = {
  347. {
  348. .name = "default",
  349. .type = AVMEDIA_TYPE_VIDEO,
  350. .config_props = config_output,
  351. },
  352. { NULL }
  353. };
  354. AVFILTER_DEFINE_CLASS(chromahold);
  355. AVFilter ff_vf_chromahold = {
  356. .name = "chromahold",
  357. .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray."),
  358. .priv_size = sizeof(ChromakeyContext),
  359. .priv_class = &chromahold_class,
  360. .query_formats = query_formats,
  361. .inputs = chromahold_inputs,
  362. .outputs = chromahold_outputs,
  363. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  364. .process_command = process_command,
  365. };