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.

357 lines
11KB

  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_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  72. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
  73. AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
  74. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  75. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  76. AV_PIX_FMT_NONE
  77. };
  78. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  79. if (!formats)
  80. return AVERROR(ENOMEM);
  81. return ff_set_common_formats(ctx, formats);
  82. }
  83. static av_cold int init(AVFilterContext *ctx)
  84. {
  85. XMedianContext *s = ctx->priv;
  86. int ret;
  87. s->radius = s->nb_inputs / 2;
  88. s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
  89. if (!s->frames)
  90. return AVERROR(ENOMEM);
  91. for (int i = 0; i < s->nb_inputs; i++) {
  92. AVFilterPad pad = { 0 };
  93. pad.type = AVMEDIA_TYPE_VIDEO;
  94. pad.name = av_asprintf("input%d", i);
  95. if (!pad.name)
  96. return AVERROR(ENOMEM);
  97. if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
  98. av_freep(&pad.name);
  99. return ret;
  100. }
  101. }
  102. return 0;
  103. }
  104. typedef struct ThreadData {
  105. AVFrame **in, *out;
  106. } ThreadData;
  107. static int comparei(const void *p1, const void *p2)
  108. {
  109. int left = *(const int *)p1;
  110. int right = *(const int *)p2;
  111. return FFDIFFSIGN(left, right);
  112. }
  113. static int median_frames16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  114. {
  115. XMedianContext *s = ctx->priv;
  116. ThreadData *td = arg;
  117. AVFrame **in = td->in;
  118. AVFrame *out = td->out;
  119. const int nb_inputs = s->nb_inputs;
  120. const int radius = s->radius;
  121. int values[256];
  122. for (int p = 0; p < s->nb_planes; p++) {
  123. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  124. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  125. uint16_t *dst = (uint16_t *)(out->data[p] + slice_start * out->linesize[p]);
  126. if (!((1 << p) & s->planes)) {
  127. av_image_copy_plane((uint8_t *)dst, out->linesize[p],
  128. in[0]->data[p] + slice_start * in[radius]->linesize[p],
  129. in[0]->linesize[p],
  130. s->linesize[p], slice_end - slice_start);
  131. continue;
  132. }
  133. for (int y = slice_start; y < slice_end; y++) {
  134. for (int x = 0; x < s->width[p]; x++) {
  135. for (int i = 0; i < nb_inputs; i++) {
  136. const uint16_t *src = (const uint16_t *)(in[i]->data[p] + y * in[i]->linesize[p]);
  137. values[i] = src[x];
  138. }
  139. AV_QSORT(values, nb_inputs, int, comparei);
  140. if (radius & 1)
  141. dst[x] = values[radius];
  142. else
  143. dst[x] = (values[radius] + values[radius - 1]) >> 1;
  144. }
  145. dst += out->linesize[p] / 2;
  146. }
  147. }
  148. return 0;
  149. }
  150. static int median_frames8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  151. {
  152. XMedianContext *s = ctx->priv;
  153. ThreadData *td = arg;
  154. AVFrame **in = td->in;
  155. AVFrame *out = td->out;
  156. const int nb_inputs = s->nb_inputs;
  157. const int radius = s->radius;
  158. int values[256];
  159. for (int p = 0; p < s->nb_planes; p++) {
  160. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  161. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  162. uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
  163. if (!((1 << p) & s->planes)) {
  164. av_image_copy_plane(dst, out->linesize[p],
  165. in[0]->data[p] + slice_start * in[0]->linesize[p],
  166. in[0]->linesize[p],
  167. s->linesize[p], slice_end - slice_start);
  168. continue;
  169. }
  170. for (int y = slice_start; y < slice_end; y++) {
  171. for (int x = 0; x < s->width[p]; x++) {
  172. for (int i = 0; i < nb_inputs; i++)
  173. values[i] = in[i]->data[p][y * in[i]->linesize[p] + x];
  174. AV_QSORT(values, nb_inputs, int, comparei);
  175. if (radius & 1)
  176. dst[x] = values[radius];
  177. else
  178. dst[x] = (values[radius] + values[radius - 1]) >> 1;
  179. }
  180. dst += out->linesize[p];
  181. }
  182. }
  183. return 0;
  184. }
  185. static int process_frame(FFFrameSync *fs)
  186. {
  187. AVFilterContext *ctx = fs->parent;
  188. AVFilterLink *outlink = ctx->outputs[0];
  189. XMedianContext *s = fs->opaque;
  190. AVFrame **in = s->frames;
  191. AVFrame *out;
  192. ThreadData td;
  193. int i, ret;
  194. for (i = 0; i < s->nb_inputs; i++) {
  195. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  196. return ret;
  197. }
  198. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  199. if (!out)
  200. return AVERROR(ENOMEM);
  201. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  202. td.in = in;
  203. td.out = out;
  204. ctx->internal->execute(ctx, s->median_frames, &td, NULL, FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
  205. return ff_filter_frame(outlink, out);
  206. }
  207. static int config_output(AVFilterLink *outlink)
  208. {
  209. AVFilterContext *ctx = outlink->src;
  210. XMedianContext *s = ctx->priv;
  211. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  212. AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
  213. AVFilterLink *inlink = ctx->inputs[0];
  214. int height = ctx->inputs[0]->h;
  215. int width = ctx->inputs[0]->w;
  216. FFFrameSyncIn *in;
  217. int i, ret;
  218. for (int i = 1; i < s->nb_inputs; i++) {
  219. if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) {
  220. 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);
  221. return AVERROR(EINVAL);
  222. }
  223. }
  224. s->desc = av_pix_fmt_desc_get(outlink->format);
  225. if (!s->desc)
  226. return AVERROR_BUG;
  227. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  228. s->depth = s->desc->comp[0].depth;
  229. s->max = (1 << s->depth) - 1;
  230. if (s->depth <= 8)
  231. s->median_frames = median_frames8;
  232. else
  233. s->median_frames = median_frames16;
  234. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  235. return ret;
  236. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
  237. s->width[0] = s->width[3] = inlink->w;
  238. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  239. s->height[0] = s->height[3] = inlink->h;
  240. outlink->w = width;
  241. outlink->h = height;
  242. outlink->frame_rate = frame_rate;
  243. outlink->sample_aspect_ratio = sar;
  244. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  245. return ret;
  246. in = s->fs.in;
  247. s->fs.opaque = s;
  248. s->fs.on_event = process_frame;
  249. for (i = 0; i < s->nb_inputs; i++) {
  250. AVFilterLink *inlink = ctx->inputs[i];
  251. in[i].time_base = inlink->time_base;
  252. in[i].sync = 1;
  253. in[i].before = EXT_STOP;
  254. in[i].after = EXT_STOP;
  255. }
  256. ret = ff_framesync_configure(&s->fs);
  257. outlink->time_base = s->fs.time_base;
  258. return ret;
  259. }
  260. static av_cold void uninit(AVFilterContext *ctx)
  261. {
  262. XMedianContext *s = ctx->priv;
  263. ff_framesync_uninit(&s->fs);
  264. av_freep(&s->frames);
  265. for (int i = 0; i < ctx->nb_inputs; i++)
  266. av_freep(&ctx->input_pads[i].name);
  267. }
  268. static int activate(AVFilterContext *ctx)
  269. {
  270. XMedianContext *s = ctx->priv;
  271. return ff_framesync_activate(&s->fs);
  272. }
  273. #define OFFSET(x) offsetof(XMedianContext, x)
  274. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  275. static const AVOption xmedian_options[] = {
  276. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 3, 255, .flags = FLAGS },
  277. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags = FLAGS },
  278. { NULL },
  279. };
  280. static const AVFilterPad outputs[] = {
  281. {
  282. .name = "default",
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. .config_props = config_output,
  285. },
  286. { NULL }
  287. };
  288. AVFILTER_DEFINE_CLASS(xmedian);
  289. AVFilter ff_vf_xmedian = {
  290. .name = "xmedian",
  291. .description = NULL_IF_CONFIG_SMALL("Pick median pixels from several video inputs."),
  292. .priv_size = sizeof(XMedianContext),
  293. .priv_class = &xmedian_class,
  294. .query_formats = query_formats,
  295. .outputs = outputs,
  296. .init = init,
  297. .uninit = uninit,
  298. .activate = activate,
  299. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
  300. };