vf_xmedian.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2019 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/avstring.h"
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "libavutil/qsort.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "framesync.h"
  30. #include "video.h"
  31. typedef struct XMedianContext {
  32. const AVClass *class;
  33. const AVPixFmtDescriptor *desc;
  34. int nb_inputs;
  35. int planes;
  36. int radius;
  37. int depth;
  38. int max;
  39. int nb_planes;
  40. int linesize[4];
  41. int width[4];
  42. int height[4];
  43. AVFrame **frames;
  44. FFFrameSync fs;
  45. int (*median_frames)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  46. } XMedianContext;
  47. static int query_formats(AVFilterContext *ctx)
  48. {
  49. static const enum AVPixelFormat pixel_fmts[] = {
  50. AV_PIX_FMT_GRAY8,
  51. AV_PIX_FMT_GRAY9,
  52. AV_PIX_FMT_GRAY10,
  53. AV_PIX_FMT_GRAY12,
  54. AV_PIX_FMT_GRAY14,
  55. AV_PIX_FMT_GRAY16,
  56. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  57. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  58. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  59. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  60. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  61. AV_PIX_FMT_YUVJ411P,
  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_YUV440P10,
  65. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  66. AV_PIX_FMT_YUV440P12,
  67. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  68. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  69. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  70. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  71. AV_PIX_FMT_NONE
  72. };
  73. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  74. if (!formats)
  75. return AVERROR(ENOMEM);
  76. return ff_set_common_formats(ctx, formats);
  77. }
  78. static av_cold int init(AVFilterContext *ctx)
  79. {
  80. XMedianContext *s = ctx->priv;
  81. int ret;
  82. s->radius = s->nb_inputs / 2;
  83. s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
  84. if (!s->frames)
  85. return AVERROR(ENOMEM);
  86. for (int i = 0; i < s->nb_inputs; i++) {
  87. AVFilterPad pad = { 0 };
  88. pad.type = AVMEDIA_TYPE_VIDEO;
  89. pad.name = av_asprintf("input%d", i);
  90. if (!pad.name)
  91. return AVERROR(ENOMEM);
  92. if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
  93. av_freep(&pad.name);
  94. return ret;
  95. }
  96. }
  97. return 0;
  98. }
  99. typedef struct ThreadData {
  100. AVFrame **in, *out;
  101. } ThreadData;
  102. static int comparei(const void *p1, const void *p2)
  103. {
  104. int left = *(const int *)p1;
  105. int right = *(const int *)p2;
  106. return FFDIFFSIGN(left, right);
  107. }
  108. static int median_frames16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  109. {
  110. XMedianContext *s = ctx->priv;
  111. ThreadData *td = arg;
  112. AVFrame **in = td->in;
  113. AVFrame *out = td->out;
  114. const int nb_inputs = s->nb_inputs;
  115. const int radius = s->radius;
  116. int values[256];
  117. for (int p = 0; p < s->nb_planes; p++) {
  118. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  119. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  120. uint16_t *dst = (uint16_t *)(out->data[p] + slice_start * out->linesize[p]);
  121. if (!((1 << p) & s->planes)) {
  122. av_image_copy_plane((uint8_t *)dst, out->linesize[p],
  123. in[0]->data[p] + slice_start * in[radius]->linesize[p],
  124. in[0]->linesize[p],
  125. s->linesize[p], slice_end - slice_start);
  126. continue;
  127. }
  128. for (int y = slice_start; y < slice_end; y++) {
  129. for (int x = 0; x < s->width[p]; x++) {
  130. for (int i = 0; i < nb_inputs; i++) {
  131. const uint16_t *src = (const uint16_t *)(in[i]->data[p] + y * in[i]->linesize[p]);
  132. values[i] = src[x];
  133. }
  134. AV_QSORT(values, nb_inputs, int, comparei);
  135. if (radius & 1)
  136. dst[x] = values[radius];
  137. else
  138. dst[x] = (values[radius] + values[radius - 1]) >> 1;
  139. }
  140. dst += out->linesize[p] / 2;
  141. }
  142. }
  143. return 0;
  144. }
  145. static int median_frames8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  146. {
  147. XMedianContext *s = ctx->priv;
  148. ThreadData *td = arg;
  149. AVFrame **in = td->in;
  150. AVFrame *out = td->out;
  151. const int nb_inputs = s->nb_inputs;
  152. const int radius = s->radius;
  153. int values[256];
  154. for (int p = 0; p < s->nb_planes; p++) {
  155. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  156. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  157. uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
  158. if (!((1 << p) & s->planes)) {
  159. av_image_copy_plane(dst, out->linesize[p],
  160. in[0]->data[p] + slice_start * in[0]->linesize[p],
  161. in[0]->linesize[p],
  162. s->linesize[p], slice_end - slice_start);
  163. continue;
  164. }
  165. for (int y = slice_start; y < slice_end; y++) {
  166. for (int x = 0; x < s->width[p]; x++) {
  167. for (int i = 0; i < nb_inputs; i++)
  168. values[i] = in[i]->data[p][y * in[i]->linesize[p] + x];
  169. AV_QSORT(values, nb_inputs, int, comparei);
  170. if (radius & 1)
  171. dst[x] = values[radius];
  172. else
  173. dst[x] = (values[radius] + values[radius - 1]) >> 1;
  174. }
  175. dst += out->linesize[p];
  176. }
  177. }
  178. return 0;
  179. }
  180. static int process_frame(FFFrameSync *fs)
  181. {
  182. AVFilterContext *ctx = fs->parent;
  183. AVFilterLink *outlink = ctx->outputs[0];
  184. XMedianContext *s = fs->opaque;
  185. AVFrame **in = s->frames;
  186. AVFrame *out;
  187. ThreadData td;
  188. int i, ret;
  189. for (i = 0; i < s->nb_inputs; i++) {
  190. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  191. return ret;
  192. }
  193. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  194. if (!out)
  195. return AVERROR(ENOMEM);
  196. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  197. td.in = in;
  198. td.out = out;
  199. ctx->internal->execute(ctx, s->median_frames, &td, NULL, FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
  200. return ff_filter_frame(outlink, out);
  201. }
  202. static int config_output(AVFilterLink *outlink)
  203. {
  204. AVFilterContext *ctx = outlink->src;
  205. XMedianContext *s = ctx->priv;
  206. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  207. AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
  208. AVFilterLink *inlink = ctx->inputs[0];
  209. int height = ctx->inputs[0]->h;
  210. int width = ctx->inputs[0]->w;
  211. FFFrameSyncIn *in;
  212. int i, ret;
  213. for (int i = 1; i < s->nb_inputs; i++) {
  214. if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) {
  215. av_log(ctx, AV_LOG_ERROR, "Input %d size (%dx%d) does not match input %d size (%dx%d).\n", i, ctx->inputs[i]->w, ctx->inputs[i]->h, 0, width, height);
  216. return AVERROR(EINVAL);
  217. }
  218. }
  219. s->desc = av_pix_fmt_desc_get(outlink->format);
  220. if (!s->desc)
  221. return AVERROR_BUG;
  222. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  223. s->depth = s->desc->comp[0].depth;
  224. s->max = (1 << s->depth) - 1;
  225. if (s->depth <= 8)
  226. s->median_frames = median_frames8;
  227. else
  228. s->median_frames = median_frames16;
  229. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  230. return ret;
  231. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
  232. s->width[0] = s->width[3] = inlink->w;
  233. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  234. s->height[0] = s->height[3] = inlink->h;
  235. outlink->w = width;
  236. outlink->h = height;
  237. outlink->frame_rate = frame_rate;
  238. outlink->sample_aspect_ratio = sar;
  239. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  240. return ret;
  241. in = s->fs.in;
  242. s->fs.opaque = s;
  243. s->fs.on_event = process_frame;
  244. for (i = 0; i < s->nb_inputs; i++) {
  245. AVFilterLink *inlink = ctx->inputs[i];
  246. in[i].time_base = inlink->time_base;
  247. in[i].sync = 1;
  248. in[i].before = EXT_STOP;
  249. in[i].after = EXT_STOP;
  250. }
  251. ret = ff_framesync_configure(&s->fs);
  252. outlink->time_base = s->fs.time_base;
  253. return ret;
  254. }
  255. static av_cold void uninit(AVFilterContext *ctx)
  256. {
  257. XMedianContext *s = ctx->priv;
  258. ff_framesync_uninit(&s->fs);
  259. av_freep(&s->frames);
  260. for (int i = 0; i < ctx->nb_inputs; i++)
  261. av_freep(&ctx->input_pads[i].name);
  262. }
  263. static int activate(AVFilterContext *ctx)
  264. {
  265. XMedianContext *s = ctx->priv;
  266. return ff_framesync_activate(&s->fs);
  267. }
  268. #define OFFSET(x) offsetof(XMedianContext, x)
  269. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  270. static const AVOption xmedian_options[] = {
  271. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 3, 255, .flags = FLAGS },
  272. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags = FLAGS },
  273. { NULL },
  274. };
  275. static const AVFilterPad outputs[] = {
  276. {
  277. .name = "default",
  278. .type = AVMEDIA_TYPE_VIDEO,
  279. .config_props = config_output,
  280. },
  281. { NULL }
  282. };
  283. AVFILTER_DEFINE_CLASS(xmedian);
  284. AVFilter ff_vf_xmedian = {
  285. .name = "xmedian",
  286. .description = NULL_IF_CONFIG_SMALL("Pick median pixels from several video inputs."),
  287. .priv_size = sizeof(XMedianContext),
  288. .priv_class = &xmedian_class,
  289. .query_formats = query_formats,
  290. .outputs = outputs,
  291. .init = init,
  292. .uninit = uninit,
  293. .activate = activate,
  294. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
  295. };