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.

464 lines
14KB

  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 nb_frames;
  36. int planes;
  37. float percentile;
  38. int tmedian;
  39. int radius;
  40. int index;
  41. int depth;
  42. int max;
  43. int nb_planes;
  44. int linesize[4];
  45. int width[4];
  46. int height[4];
  47. AVFrame **frames;
  48. FFFrameSync fs;
  49. int (*median_frames)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  50. } XMedianContext;
  51. static int query_formats(AVFilterContext *ctx)
  52. {
  53. static const enum AVPixelFormat pixel_fmts[] = {
  54. AV_PIX_FMT_GRAY8,
  55. AV_PIX_FMT_GRAY9,
  56. AV_PIX_FMT_GRAY10,
  57. AV_PIX_FMT_GRAY12,
  58. AV_PIX_FMT_GRAY14,
  59. AV_PIX_FMT_GRAY16,
  60. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  61. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  62. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  63. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  64. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  65. AV_PIX_FMT_YUVJ411P,
  66. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  67. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  68. AV_PIX_FMT_YUV440P10,
  69. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  70. AV_PIX_FMT_YUV440P12,
  71. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  72. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  73. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  74. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  75. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  76. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
  77. AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
  78. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  79. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  80. AV_PIX_FMT_NONE
  81. };
  82. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  83. if (!formats)
  84. return AVERROR(ENOMEM);
  85. return ff_set_common_formats(ctx, formats);
  86. }
  87. static av_cold int init(AVFilterContext *ctx)
  88. {
  89. XMedianContext *s = ctx->priv;
  90. int ret;
  91. s->tmedian = !strcmp(ctx->filter->name, "tmedian");
  92. if (!s->tmedian) {
  93. s->radius = s->nb_inputs / 2;
  94. } else {
  95. s->nb_inputs = s->radius * 2 + 1;
  96. }
  97. if (s->nb_inputs & 1)
  98. s->index = s->radius * 2.f * s->percentile;
  99. else
  100. s->index = av_clip(s->radius * 2.f * s->percentile, 1, s->nb_inputs - 1);
  101. s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
  102. if (!s->frames)
  103. return AVERROR(ENOMEM);
  104. for (int i = 0; i < s->nb_inputs && !s->tmedian; i++) {
  105. AVFilterPad pad = { 0 };
  106. pad.type = AVMEDIA_TYPE_VIDEO;
  107. pad.name = av_asprintf("input%d", i);
  108. if (!pad.name)
  109. return AVERROR(ENOMEM);
  110. if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
  111. av_freep(&pad.name);
  112. return ret;
  113. }
  114. }
  115. return 0;
  116. }
  117. typedef struct ThreadData {
  118. AVFrame **in, *out;
  119. } ThreadData;
  120. static int comparei(const void *p1, const void *p2)
  121. {
  122. int left = *(const int *)p1;
  123. int right = *(const int *)p2;
  124. return FFDIFFSIGN(left, right);
  125. }
  126. static int median_frames16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  127. {
  128. XMedianContext *s = ctx->priv;
  129. ThreadData *td = arg;
  130. AVFrame **in = td->in;
  131. AVFrame *out = td->out;
  132. const int nb_inputs = s->nb_inputs;
  133. const int radius = s->radius;
  134. const int index = s->index;
  135. int values[256];
  136. for (int p = 0; p < s->nb_planes; p++) {
  137. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  138. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  139. uint16_t *dst = (uint16_t *)(out->data[p] + slice_start * out->linesize[p]);
  140. if (!((1 << p) & s->planes)) {
  141. av_image_copy_plane((uint8_t *)dst, out->linesize[p],
  142. in[radius]->data[p] + slice_start * in[radius]->linesize[p],
  143. in[radius]->linesize[p],
  144. s->linesize[p], slice_end - slice_start);
  145. continue;
  146. }
  147. for (int y = slice_start; y < slice_end; y++) {
  148. for (int x = 0; x < s->width[p]; x++) {
  149. for (int i = 0; i < nb_inputs; i++) {
  150. const uint16_t *src = (const uint16_t *)(in[i]->data[p] + y * in[i]->linesize[p]);
  151. values[i] = src[x];
  152. }
  153. AV_QSORT(values, nb_inputs, int, comparei);
  154. if (nb_inputs & 1)
  155. dst[x] = values[index];
  156. else
  157. dst[x] = (values[index] + values[index - 1]) >> 1;
  158. }
  159. dst += out->linesize[p] / 2;
  160. }
  161. }
  162. return 0;
  163. }
  164. static int median_frames8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  165. {
  166. XMedianContext *s = ctx->priv;
  167. ThreadData *td = arg;
  168. AVFrame **in = td->in;
  169. AVFrame *out = td->out;
  170. const int nb_inputs = s->nb_inputs;
  171. const int radius = s->radius;
  172. const int index = s->index;
  173. int values[256];
  174. for (int p = 0; p < s->nb_planes; p++) {
  175. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  176. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  177. uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
  178. if (!((1 << p) & s->planes)) {
  179. av_image_copy_plane(dst, out->linesize[p],
  180. in[radius]->data[p] + slice_start * in[radius]->linesize[p],
  181. in[radius]->linesize[p],
  182. s->linesize[p], slice_end - slice_start);
  183. continue;
  184. }
  185. for (int y = slice_start; y < slice_end; y++) {
  186. for (int x = 0; x < s->width[p]; x++) {
  187. for (int i = 0; i < nb_inputs; i++)
  188. values[i] = in[i]->data[p][y * in[i]->linesize[p] + x];
  189. AV_QSORT(values, nb_inputs, int, comparei);
  190. if (nb_inputs & 1)
  191. dst[x] = values[index];
  192. else
  193. dst[x] = (values[index] + values[index - 1]) >> 1;
  194. }
  195. dst += out->linesize[p];
  196. }
  197. }
  198. return 0;
  199. }
  200. static int process_frame(FFFrameSync *fs)
  201. {
  202. AVFilterContext *ctx = fs->parent;
  203. AVFilterLink *outlink = ctx->outputs[0];
  204. XMedianContext *s = fs->opaque;
  205. AVFrame **in = s->frames;
  206. AVFrame *out;
  207. ThreadData td;
  208. int i, ret;
  209. for (i = 0; i < s->nb_inputs; i++) {
  210. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  211. return ret;
  212. }
  213. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  214. if (!out)
  215. return AVERROR(ENOMEM);
  216. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  217. td.in = in;
  218. td.out = out;
  219. ctx->internal->execute(ctx, s->median_frames, &td, NULL, FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
  220. return ff_filter_frame(outlink, out);
  221. }
  222. static int config_output(AVFilterLink *outlink)
  223. {
  224. AVFilterContext *ctx = outlink->src;
  225. XMedianContext *s = ctx->priv;
  226. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  227. AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
  228. AVFilterLink *inlink = ctx->inputs[0];
  229. int height = ctx->inputs[0]->h;
  230. int width = ctx->inputs[0]->w;
  231. FFFrameSyncIn *in;
  232. int i, ret;
  233. for (int i = 1; i < s->nb_inputs && !s->tmedian; i++) {
  234. if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) {
  235. 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);
  236. return AVERROR(EINVAL);
  237. }
  238. }
  239. s->desc = av_pix_fmt_desc_get(outlink->format);
  240. if (!s->desc)
  241. return AVERROR_BUG;
  242. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  243. s->depth = s->desc->comp[0].depth;
  244. s->max = (1 << s->depth) - 1;
  245. if (s->depth <= 8)
  246. s->median_frames = median_frames8;
  247. else
  248. s->median_frames = median_frames16;
  249. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  250. return ret;
  251. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
  252. s->width[0] = s->width[3] = inlink->w;
  253. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  254. s->height[0] = s->height[3] = inlink->h;
  255. if (s->tmedian)
  256. return 0;
  257. outlink->w = width;
  258. outlink->h = height;
  259. outlink->frame_rate = frame_rate;
  260. outlink->sample_aspect_ratio = sar;
  261. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  262. return ret;
  263. in = s->fs.in;
  264. s->fs.opaque = s;
  265. s->fs.on_event = process_frame;
  266. for (i = 0; i < s->nb_inputs; i++) {
  267. AVFilterLink *inlink = ctx->inputs[i];
  268. in[i].time_base = inlink->time_base;
  269. in[i].sync = 1;
  270. in[i].before = EXT_STOP;
  271. in[i].after = EXT_STOP;
  272. }
  273. ret = ff_framesync_configure(&s->fs);
  274. outlink->time_base = s->fs.time_base;
  275. return ret;
  276. }
  277. static av_cold void uninit(AVFilterContext *ctx)
  278. {
  279. XMedianContext *s = ctx->priv;
  280. ff_framesync_uninit(&s->fs);
  281. for (int i = 0; i < ctx->nb_inputs && !s->tmedian; i++)
  282. av_freep(&ctx->input_pads[i].name);
  283. for (int i = 0; i < s->nb_frames && s->frames && s->tmedian; i++)
  284. av_frame_free(&s->frames[i]);
  285. av_freep(&s->frames);
  286. }
  287. static int activate(AVFilterContext *ctx)
  288. {
  289. XMedianContext *s = ctx->priv;
  290. return ff_framesync_activate(&s->fs);
  291. }
  292. #define OFFSET(x) offsetof(XMedianContext, x)
  293. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  294. static const AVOption xmedian_options[] = {
  295. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 3, 255, .flags = FLAGS },
  296. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags = FLAGS },
  297. { "percentile", "set percentile", OFFSET(percentile),AV_OPT_TYPE_FLOAT,{.dbl=0.5}, 0, 1, .flags = FLAGS },
  298. { NULL },
  299. };
  300. static const AVFilterPad outputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .config_props = config_output,
  305. },
  306. { NULL }
  307. };
  308. #if CONFIG_XMEDIAN_FILTER
  309. AVFILTER_DEFINE_CLASS(xmedian);
  310. AVFilter ff_vf_xmedian = {
  311. .name = "xmedian",
  312. .description = NULL_IF_CONFIG_SMALL("Pick median pixels from several video inputs."),
  313. .priv_size = sizeof(XMedianContext),
  314. .priv_class = &xmedian_class,
  315. .query_formats = query_formats,
  316. .outputs = outputs,
  317. .init = init,
  318. .uninit = uninit,
  319. .activate = activate,
  320. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
  321. };
  322. #endif /* CONFIG_XMEDIAN_FILTER */
  323. #if CONFIG_TMEDIAN_FILTER
  324. static int tmedian_filter_frame(AVFilterLink *inlink, AVFrame *in)
  325. {
  326. AVFilterContext *ctx = inlink->dst;
  327. AVFilterLink *outlink = ctx->outputs[0];
  328. XMedianContext *s = ctx->priv;
  329. ThreadData td;
  330. AVFrame *out;
  331. if (s->nb_frames < s->nb_inputs) {
  332. s->frames[s->nb_frames] = in;
  333. s->nb_frames++;
  334. if (s->nb_frames < s->nb_inputs)
  335. return 0;
  336. } else {
  337. av_frame_free(&s->frames[0]);
  338. memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1));
  339. s->frames[s->nb_inputs - 1] = in;
  340. }
  341. if (ctx->is_disabled) {
  342. out = av_frame_clone(s->frames[0]);
  343. if (!out)
  344. return AVERROR(ENOMEM);
  345. return ff_filter_frame(outlink, out);
  346. }
  347. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  348. if (!out)
  349. return AVERROR(ENOMEM);
  350. out->pts = s->frames[0]->pts;
  351. td.out = out;
  352. td.in = s->frames;
  353. ctx->internal->execute(ctx, s->median_frames, &td, NULL, FFMIN(s->height[0], ff_filter_get_nb_threads(ctx)));
  354. return ff_filter_frame(outlink, out);
  355. }
  356. static const AVOption tmedian_options[] = {
  357. { "radius", "set median filter radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 127, .flags = FLAGS },
  358. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags = FLAGS },
  359. { "percentile", "set percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, .flags = FLAGS },
  360. { NULL },
  361. };
  362. static const AVFilterPad tmedian_inputs[] = {
  363. {
  364. .name = "default",
  365. .type = AVMEDIA_TYPE_VIDEO,
  366. .filter_frame = tmedian_filter_frame,
  367. },
  368. { NULL }
  369. };
  370. static const AVFilterPad tmedian_outputs[] = {
  371. {
  372. .name = "default",
  373. .type = AVMEDIA_TYPE_VIDEO,
  374. .config_props = config_output,
  375. },
  376. { NULL }
  377. };
  378. AVFILTER_DEFINE_CLASS(tmedian);
  379. AVFilter ff_vf_tmedian = {
  380. .name = "tmedian",
  381. .description = NULL_IF_CONFIG_SMALL("Pick median pixels from successive frames."),
  382. .priv_size = sizeof(XMedianContext),
  383. .priv_class = &tmedian_class,
  384. .query_formats = query_formats,
  385. .inputs = tmedian_inputs,
  386. .outputs = tmedian_outputs,
  387. .init = init,
  388. .uninit = uninit,
  389. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  390. };
  391. #endif /* CONFIG_TMEDIAN_FILTER */