vf_maskfun.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  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/imgutils.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "libavutil/opt.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. typedef struct MaskFunContext {
  28. const AVClass *class;
  29. int low, high;
  30. int planes;
  31. int fill;
  32. int sum;
  33. int linesize[4];
  34. int width[4], height[4];
  35. int nb_planes;
  36. int depth;
  37. int max;
  38. uint64_t max_sum;
  39. AVFrame *empty;
  40. int (*getsum)(AVFilterContext *ctx, AVFrame *out);
  41. int (*maskfun)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  42. } MaskFunContext;
  43. #define OFFSET(x) offsetof(MaskFunContext, x)
  44. #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  45. static const AVOption maskfun_options[] = {
  46. { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VF },
  47. { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VF },
  48. { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, VF },
  49. { "fill", "set fill value", OFFSET(fill), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, VF },
  50. { "sum", "set sum value", OFFSET(sum), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VF },
  51. { NULL }
  52. };
  53. AVFILTER_DEFINE_CLASS(maskfun);
  54. static int query_formats(AVFilterContext *ctx)
  55. {
  56. static const enum AVPixelFormat pix_fmts[] = {
  57. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  58. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  59. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  60. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  61. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  62. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  63. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  64. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  65. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  66. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  67. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  68. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  69. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  70. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  71. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  72. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  73. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  74. AV_PIX_FMT_NONE
  75. };
  76. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  77. }
  78. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  79. {
  80. AVFilterContext *ctx = inlink->dst;
  81. MaskFunContext *s = ctx->priv;
  82. AVFilterLink *outlink = ctx->outputs[0];
  83. if (s->getsum(ctx, frame)) {
  84. AVFrame *out = av_frame_clone(s->empty);
  85. if (!out) {
  86. av_frame_free(&frame);
  87. return AVERROR(ENOMEM);
  88. }
  89. out->pts = frame->pts;
  90. av_frame_free(&frame);
  91. return ff_filter_frame(outlink, out);
  92. }
  93. ctx->internal->execute(ctx, s->maskfun, frame, NULL,
  94. FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
  95. return ff_filter_frame(outlink, frame);
  96. }
  97. #define GETSUM(name, type, div) \
  98. static int getsum##name(AVFilterContext *ctx, AVFrame *out) \
  99. { \
  100. MaskFunContext *s = ctx->priv; \
  101. uint64_t sum = 0; \
  102. int p; \
  103. \
  104. for (p = 0; p < s->nb_planes; p++) { \
  105. const int linesize = out->linesize[p] / div; \
  106. const int w = s->width[p]; \
  107. const int h = s->height[p]; \
  108. type *dst = (type *)out->data[p]; \
  109. \
  110. if (!((1 << p) & s->planes)) \
  111. continue; \
  112. \
  113. for (int y = 0; y < h; y++) { \
  114. for (int x = 0; x < w; x++) \
  115. sum += dst[x]; \
  116. if (sum >= s->max_sum) \
  117. return 1; \
  118. dst += linesize; \
  119. } \
  120. } \
  121. \
  122. return 0; \
  123. }
  124. GETSUM(8, uint8_t, 1)
  125. GETSUM(16, uint16_t, 2)
  126. #define MASKFUN(name, type, div) \
  127. static int maskfun##name(AVFilterContext *ctx, void *arg, \
  128. int jobnr, int nb_jobs) \
  129. { \
  130. MaskFunContext *s = ctx->priv; \
  131. AVFrame *out = arg; \
  132. const int low = s->low; \
  133. const int high = s->high; \
  134. const int max = s->max; \
  135. int p; \
  136. \
  137. for (p = 0; p < s->nb_planes; p++) { \
  138. const int linesize = out->linesize[p] / div; \
  139. const int w = s->width[p]; \
  140. const int h = s->height[p]; \
  141. const int slice_start = (h * jobnr) / nb_jobs; \
  142. const int slice_end = (h * (jobnr+1)) / nb_jobs; \
  143. type *dst = (type *)out->data[p] + slice_start * linesize; \
  144. \
  145. if (!((1 << p) & s->planes)) \
  146. continue; \
  147. \
  148. for (int y = slice_start; y < slice_end; y++) { \
  149. for (int x = 0; x < w; x++) { \
  150. if (dst[x] <= low) \
  151. dst[x] = 0; \
  152. else if (dst[x] > high) \
  153. dst[x] = max; \
  154. } \
  155. \
  156. dst += linesize; \
  157. } \
  158. } \
  159. \
  160. return 0; \
  161. }
  162. MASKFUN(8, uint8_t, 1)
  163. MASKFUN(16, uint16_t, 2)
  164. static int config_input(AVFilterLink *inlink)
  165. {
  166. AVFilterContext *ctx = inlink->dst;
  167. MaskFunContext *s = ctx->priv;
  168. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  169. int vsub, hsub, ret;
  170. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  171. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  172. return ret;
  173. hsub = desc->log2_chroma_w;
  174. vsub = desc->log2_chroma_h;
  175. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  176. s->height[0] = s->height[3] = inlink->h;
  177. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  178. s->width[0] = s->width[3] = inlink->w;
  179. s->depth = desc->comp[0].depth;
  180. s->max = (1 << s->depth) - 1;
  181. s->fill = FFMIN(s->fill, s->max);
  182. if (s->depth == 8) {
  183. s->maskfun = maskfun8;
  184. s->getsum = getsum8;
  185. } else {
  186. s->maskfun = maskfun16;
  187. s->getsum = getsum16;
  188. }
  189. s->empty = ff_get_video_buffer(inlink, inlink->w, inlink->h);
  190. if (!s->empty)
  191. return AVERROR(ENOMEM);
  192. if (s->depth == 8) {
  193. for (int p = 0; p < s->nb_planes; p++) {
  194. uint8_t *dst = s->empty->data[p];
  195. for (int y = 0; y < s->height[p]; y++) {
  196. memset(dst, s->fill, s->width[p]);
  197. dst += s->empty->linesize[p];
  198. }
  199. }
  200. } else {
  201. for (int p = 0; p < s->nb_planes; p++) {
  202. uint16_t *dst = (uint16_t *)s->empty->data[p];
  203. for (int y = 0; y < s->height[p]; y++) {
  204. for (int x = 0; x < s->width[p]; x++)
  205. dst[x] = s->fill;
  206. dst += s->empty->linesize[p] / 2;
  207. }
  208. }
  209. }
  210. s->max_sum = 0;
  211. for (int p = 0; p < s->nb_planes; p++) {
  212. if (!((1 << p) & s->planes))
  213. continue;
  214. s->max_sum += (uint64_t)s->sum * s->width[p] * s->height[p];
  215. }
  216. return 0;
  217. }
  218. static const AVFilterPad maskfun_inputs[] = {
  219. {
  220. .name = "default",
  221. .type = AVMEDIA_TYPE_VIDEO,
  222. .filter_frame = filter_frame,
  223. .config_props = config_input,
  224. .needs_writable = 1,
  225. },
  226. { NULL }
  227. };
  228. static const AVFilterPad maskfun_outputs[] = {
  229. {
  230. .name = "default",
  231. .type = AVMEDIA_TYPE_VIDEO,
  232. },
  233. { NULL }
  234. };
  235. AVFilter ff_vf_maskfun = {
  236. .name = "maskfun",
  237. .description = NULL_IF_CONFIG_SMALL("Create Mask."),
  238. .priv_size = sizeof(MaskFunContext),
  239. .query_formats = query_formats,
  240. .inputs = maskfun_inputs,
  241. .outputs = maskfun_outputs,
  242. .priv_class = &maskfun_class,
  243. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  244. };