vf_maskedclamp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2016 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. #include "framesync.h"
  28. #define OFFSET(x) offsetof(MaskedClampContext, x)
  29. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  30. typedef struct ThreadData {
  31. AVFrame *b, *o, *m, *d;
  32. } ThreadData;
  33. typedef struct MaskedClampContext {
  34. const AVClass *class;
  35. int planes;
  36. int undershoot;
  37. int overshoot;
  38. int linesize[4];
  39. int width[4], height[4];
  40. int nb_planes;
  41. int depth;
  42. FFFrameSync fs;
  43. int (*maskedclamp)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  44. } MaskedClampContext;
  45. static const AVOption maskedclamp_options[] = {
  46. { "undershoot", "set undershoot", OFFSET(undershoot), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
  47. { "overshoot", "set overshoot", OFFSET(overshoot), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
  48. { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  49. { NULL }
  50. };
  51. AVFILTER_DEFINE_CLASS(maskedclamp);
  52. static int query_formats(AVFilterContext *ctx)
  53. {
  54. static const enum AVPixelFormat pix_fmts[] = {
  55. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  56. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  57. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  58. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  59. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  60. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  61. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  62. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  63. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  64. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  65. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  66. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  67. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  68. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  69. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  70. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  71. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  72. AV_PIX_FMT_NONE
  73. };
  74. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  75. }
  76. static int process_frame(FFFrameSync *fs)
  77. {
  78. AVFilterContext *ctx = fs->parent;
  79. MaskedClampContext *s = fs->opaque;
  80. AVFilterLink *outlink = ctx->outputs[0];
  81. AVFrame *out, *base, *dark, *bright;
  82. int ret;
  83. if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
  84. (ret = ff_framesync_get_frame(&s->fs, 1, &dark, 0)) < 0 ||
  85. (ret = ff_framesync_get_frame(&s->fs, 2, &bright, 0)) < 0)
  86. return ret;
  87. if (ctx->is_disabled) {
  88. out = av_frame_clone(base);
  89. if (!out)
  90. return AVERROR(ENOMEM);
  91. } else {
  92. ThreadData td;
  93. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  94. if (!out)
  95. return AVERROR(ENOMEM);
  96. av_frame_copy_props(out, base);
  97. td.b = base;
  98. td.o = dark;
  99. td.m = bright;
  100. td.d = out;
  101. ctx->internal->execute(ctx, s->maskedclamp, &td, NULL, FFMIN(s->height[0],
  102. ff_filter_get_nb_threads(ctx)));
  103. }
  104. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  105. return ff_filter_frame(outlink, out);
  106. }
  107. static int maskedclamp8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  108. {
  109. MaskedClampContext *s = ctx->priv;
  110. ThreadData *td = arg;
  111. int p;
  112. for (p = 0; p < s->nb_planes; p++) {
  113. const ptrdiff_t blinesize = td->b->linesize[p];
  114. const ptrdiff_t brightlinesize = td->m->linesize[p];
  115. const ptrdiff_t darklinesize = td->o->linesize[p];
  116. const ptrdiff_t dlinesize = td->d->linesize[p];
  117. const int w = s->width[p];
  118. const int h = s->height[p];
  119. const int slice_start = (h * jobnr) / nb_jobs;
  120. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  121. const uint8_t *bsrc = td->b->data[p] + slice_start * blinesize;
  122. const uint8_t *darksrc = td->o->data[p] + slice_start * darklinesize;
  123. const uint8_t *brightsrc = td->m->data[p] + slice_start * brightlinesize;
  124. uint8_t *dst = td->d->data[p] + slice_start * dlinesize;
  125. const int undershoot = s->undershoot;
  126. const int overshoot = s->overshoot;
  127. int x, y;
  128. if (!((1 << p) & s->planes)) {
  129. av_image_copy_plane(dst, dlinesize, bsrc, blinesize,
  130. s->linesize[p], slice_end - slice_start);
  131. continue;
  132. }
  133. for (y = slice_start; y < slice_end; y++) {
  134. for (x = 0; x < w; x++) {
  135. if (bsrc[x] < darksrc[x] - undershoot)
  136. dst[x] = darksrc[x] - undershoot;
  137. else if (bsrc[x] > brightsrc[x] + overshoot)
  138. dst[x] = brightsrc[x] + overshoot;
  139. else
  140. dst[x] = bsrc[x];
  141. }
  142. dst += dlinesize;
  143. bsrc += blinesize;
  144. darksrc += darklinesize;
  145. brightsrc += brightlinesize;
  146. }
  147. }
  148. return 0;
  149. }
  150. static int maskedclamp16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  151. {
  152. MaskedClampContext *s = ctx->priv;
  153. ThreadData *td = arg;
  154. int p;
  155. for (p = 0; p < s->nb_planes; p++) {
  156. const ptrdiff_t blinesize = td->b->linesize[p] / 2;
  157. const ptrdiff_t brightlinesize = td->m->linesize[p] / 2;
  158. const ptrdiff_t darklinesize = td->o->linesize[p] / 2;
  159. const ptrdiff_t dlinesize = td->d->linesize[p] / 2;
  160. const int w = s->width[p];
  161. const int h = s->height[p];
  162. const int slice_start = (h * jobnr) / nb_jobs;
  163. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  164. const uint16_t *bsrc = (const uint16_t *)td->b->data[p] + slice_start * blinesize;
  165. const uint16_t *darksrc = (const uint16_t *)td->o->data[p] + slice_start * darklinesize;
  166. const uint16_t *brightsrc = (const uint16_t *)td->m->data[p] + slice_start * brightlinesize;
  167. uint16_t *dst = (uint16_t *)td->d->data[p] + slice_start * dlinesize;
  168. const int undershoot = s->undershoot;
  169. const int overshoot = s->overshoot;
  170. int x, y;
  171. if (!((1 << p) & s->planes)) {
  172. av_image_copy_plane((uint8_t *)dst, dlinesize, (const uint8_t *)bsrc, blinesize,
  173. s->linesize[p], slice_end - slice_start);
  174. continue;
  175. }
  176. for (y = slice_start; y < slice_end; y++) {
  177. for (x = 0; x < w; x++) {
  178. if (bsrc[x] < darksrc[x] - undershoot)
  179. dst[x] = darksrc[x] - undershoot;
  180. else if (bsrc[x] > brightsrc[x] + overshoot)
  181. dst[x] = brightsrc[x] + overshoot;
  182. else
  183. dst[x] = bsrc[x];
  184. }
  185. dst += dlinesize;
  186. bsrc += blinesize;
  187. darksrc += darklinesize;
  188. brightsrc += brightlinesize;
  189. }
  190. }
  191. return 0;
  192. }
  193. static int config_input(AVFilterLink *inlink)
  194. {
  195. AVFilterContext *ctx = inlink->dst;
  196. MaskedClampContext *s = ctx->priv;
  197. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  198. int vsub, hsub, ret;
  199. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  200. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  201. return ret;
  202. hsub = desc->log2_chroma_w;
  203. vsub = desc->log2_chroma_h;
  204. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  205. s->height[0] = s->height[3] = inlink->h;
  206. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  207. s->width[0] = s->width[3] = inlink->w;
  208. s->depth = desc->comp[0].depth;
  209. if (desc->comp[0].depth == 8)
  210. s->maskedclamp = maskedclamp8;
  211. else
  212. s->maskedclamp = maskedclamp16;
  213. return 0;
  214. }
  215. static int config_output(AVFilterLink *outlink)
  216. {
  217. AVFilterContext *ctx = outlink->src;
  218. MaskedClampContext *s = ctx->priv;
  219. AVFilterLink *base = ctx->inputs[0];
  220. AVFilterLink *dark = ctx->inputs[1];
  221. AVFilterLink *bright = ctx->inputs[2];
  222. FFFrameSyncIn *in;
  223. int ret;
  224. if (base->format != dark->format ||
  225. base->format != bright->format) {
  226. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  227. return AVERROR(EINVAL);
  228. }
  229. if (base->w != dark->w || base->h != dark->h ||
  230. base->w != bright->w || base->h != bright->h) {
  231. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  232. "(size %dx%d) do not match the corresponding "
  233. "second input link %s parameters (%dx%d) "
  234. "and/or third input link %s parameters (size %dx%d)\n",
  235. ctx->input_pads[0].name, base->w, base->h,
  236. ctx->input_pads[1].name, dark->w, dark->h,
  237. ctx->input_pads[2].name, bright->w, bright->h);
  238. return AVERROR(EINVAL);
  239. }
  240. outlink->w = base->w;
  241. outlink->h = base->h;
  242. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  243. outlink->frame_rate = base->frame_rate;
  244. if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
  245. return ret;
  246. in = s->fs.in;
  247. in[0].time_base = base->time_base;
  248. in[1].time_base = dark->time_base;
  249. in[2].time_base = bright->time_base;
  250. in[0].sync = 1;
  251. in[0].before = EXT_STOP;
  252. in[0].after = EXT_INFINITY;
  253. in[1].sync = 1;
  254. in[1].before = EXT_STOP;
  255. in[1].after = EXT_INFINITY;
  256. in[2].sync = 1;
  257. in[2].before = EXT_STOP;
  258. in[2].after = EXT_INFINITY;
  259. s->fs.opaque = s;
  260. s->fs.on_event = process_frame;
  261. ret = ff_framesync_configure(&s->fs);
  262. outlink->time_base = s->fs.time_base;
  263. return ret;
  264. }
  265. static int activate(AVFilterContext *ctx)
  266. {
  267. MaskedClampContext *s = ctx->priv;
  268. return ff_framesync_activate(&s->fs);
  269. }
  270. static av_cold void uninit(AVFilterContext *ctx)
  271. {
  272. MaskedClampContext *s = ctx->priv;
  273. ff_framesync_uninit(&s->fs);
  274. }
  275. static const AVFilterPad maskedclamp_inputs[] = {
  276. {
  277. .name = "base",
  278. .type = AVMEDIA_TYPE_VIDEO,
  279. .config_props = config_input,
  280. },
  281. {
  282. .name = "dark",
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. },
  285. {
  286. .name = "bright",
  287. .type = AVMEDIA_TYPE_VIDEO,
  288. },
  289. { NULL }
  290. };
  291. static const AVFilterPad maskedclamp_outputs[] = {
  292. {
  293. .name = "default",
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .config_props = config_output,
  296. },
  297. { NULL }
  298. };
  299. AVFilter ff_vf_maskedclamp = {
  300. .name = "maskedclamp",
  301. .description = NULL_IF_CONFIG_SMALL("Clamp first stream with second stream and third stream."),
  302. .priv_size = sizeof(MaskedClampContext),
  303. .uninit = uninit,
  304. .activate = activate,
  305. .query_formats = query_formats,
  306. .inputs = maskedclamp_inputs,
  307. .outputs = maskedclamp_outputs,
  308. .priv_class = &maskedclamp_class,
  309. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  310. };