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.

346 lines
12KB

  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 MaskedClampContext {
  31. const AVClass *class;
  32. int planes;
  33. int undershoot;
  34. int overshoot;
  35. int width[4], height[4];
  36. int nb_planes;
  37. int depth;
  38. FFFrameSync fs;
  39. void (*maskedclamp)(const uint8_t *bsrc, const uint8_t *osrc,
  40. const uint8_t *msrc, uint8_t *dst,
  41. ptrdiff_t blinesize, ptrdiff_t darklinesize,
  42. ptrdiff_t brightlinesize, ptrdiff_t destlinesize,
  43. int w, int h, int undershoot, int overshoot);
  44. } MaskedClampContext;
  45. static const AVOption maskedclamp_options[] = {
  46. { "undershoot", "set undershoot", OFFSET(undershoot), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  47. { "overshoot", "set overshoot", OFFSET(overshoot), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_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_GBRAP12, AV_PIX_FMT_GBRAP16,
  71. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, 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. int p;
  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. for (p = 0; p < s->nb_planes; p++) {
  98. if (!((1 << p) & s->planes)) {
  99. av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
  100. s->width[p], s->height[p]);
  101. continue;
  102. }
  103. s->maskedclamp(base->data[p], dark->data[p],
  104. bright->data[p], out->data[p],
  105. base->linesize[p], dark->linesize[p],
  106. bright->linesize[p], out->linesize[p],
  107. s->width[p], s->height[p],
  108. s->undershoot, s->overshoot);
  109. }
  110. }
  111. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  112. return ff_filter_frame(outlink, out);
  113. }
  114. static void maskedclamp8(const uint8_t *bsrc, const uint8_t *darksrc,
  115. const uint8_t *brightsrc, uint8_t *dst,
  116. ptrdiff_t blinesize, ptrdiff_t darklinesize,
  117. ptrdiff_t brightlinesize, ptrdiff_t dlinesize,
  118. int w, int h,
  119. int undershoot, int overshoot)
  120. {
  121. int x, y;
  122. for (y = 0; y < h; y++) {
  123. for (x = 0; x < w; x++) {
  124. if (bsrc[x] < darksrc[x] - undershoot)
  125. dst[x] = darksrc[x] - undershoot;
  126. else if (bsrc[x] > brightsrc[x] + overshoot)
  127. dst[x] = brightsrc[x] + overshoot;
  128. else
  129. dst[x] = bsrc[x];
  130. }
  131. dst += dlinesize;
  132. bsrc += blinesize;
  133. darksrc += darklinesize;
  134. brightsrc += brightlinesize;
  135. }
  136. }
  137. static void maskedclamp16(const uint8_t *bbsrc, const uint8_t *oosrc,
  138. const uint8_t *mmsrc, uint8_t *ddst,
  139. ptrdiff_t blinesize, ptrdiff_t darklinesize,
  140. ptrdiff_t brightlinesize, ptrdiff_t dlinesize,
  141. int w, int h,
  142. int undershoot, int overshoot)
  143. {
  144. const uint16_t *bsrc = (const uint16_t *)bbsrc;
  145. const uint16_t *darksrc = (const uint16_t *)oosrc;
  146. const uint16_t *brightsrc = (const uint16_t *)mmsrc;
  147. uint16_t *dst = (uint16_t *)ddst;
  148. int x, y;
  149. dlinesize /= 2;
  150. blinesize /= 2;
  151. darklinesize /= 2;
  152. brightlinesize /= 2;
  153. for (y = 0; y < h; y++) {
  154. for (x = 0; x < w; x++) {
  155. if (bsrc[x] < darksrc[x] - undershoot)
  156. dst[x] = darksrc[x] - undershoot;
  157. else if (bsrc[x] > brightsrc[x] + overshoot)
  158. dst[x] = brightsrc[x] + overshoot;
  159. else
  160. dst[x] = bsrc[x];
  161. }
  162. dst += dlinesize;
  163. bsrc += blinesize;
  164. darksrc += darklinesize;
  165. brightsrc += brightlinesize;
  166. }
  167. }
  168. static int config_input(AVFilterLink *inlink)
  169. {
  170. AVFilterContext *ctx = inlink->dst;
  171. MaskedClampContext *s = ctx->priv;
  172. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  173. int vsub, hsub;
  174. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  175. hsub = desc->log2_chroma_w;
  176. vsub = desc->log2_chroma_h;
  177. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  178. s->height[0] = s->height[3] = inlink->h;
  179. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  180. s->width[0] = s->width[3] = inlink->w;
  181. s->depth = desc->comp[0].depth;
  182. if (desc->comp[0].depth == 8)
  183. s->maskedclamp = maskedclamp8;
  184. else
  185. s->maskedclamp = maskedclamp16;
  186. return 0;
  187. }
  188. static int config_output(AVFilterLink *outlink)
  189. {
  190. AVFilterContext *ctx = outlink->src;
  191. MaskedClampContext *s = ctx->priv;
  192. AVFilterLink *base = ctx->inputs[0];
  193. AVFilterLink *dark = ctx->inputs[1];
  194. AVFilterLink *bright = ctx->inputs[2];
  195. FFFrameSyncIn *in;
  196. int ret;
  197. if (base->format != dark->format ||
  198. base->format != bright->format) {
  199. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  200. return AVERROR(EINVAL);
  201. }
  202. if (base->w != dark->w ||
  203. base->h != dark->h ||
  204. base->sample_aspect_ratio.num != dark->sample_aspect_ratio.num ||
  205. base->sample_aspect_ratio.den != dark->sample_aspect_ratio.den ||
  206. base->w != bright->w ||
  207. base->h != bright->h ||
  208. base->sample_aspect_ratio.num != bright->sample_aspect_ratio.num ||
  209. base->sample_aspect_ratio.den != bright->sample_aspect_ratio.den) {
  210. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  211. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  212. "second input link %s parameters (%dx%d, SAR %d:%d) "
  213. "and/or third input link %s parameters (%dx%d, SAR %d:%d)\n",
  214. ctx->input_pads[0].name, base->w, base->h,
  215. base->sample_aspect_ratio.num,
  216. base->sample_aspect_ratio.den,
  217. ctx->input_pads[1].name, dark->w, dark->h,
  218. dark->sample_aspect_ratio.num,
  219. dark->sample_aspect_ratio.den,
  220. ctx->input_pads[2].name, bright->w, bright->h,
  221. bright->sample_aspect_ratio.num,
  222. bright->sample_aspect_ratio.den);
  223. return AVERROR(EINVAL);
  224. }
  225. outlink->w = base->w;
  226. outlink->h = base->h;
  227. outlink->time_base = base->time_base;
  228. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  229. outlink->frame_rate = base->frame_rate;
  230. if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
  231. return ret;
  232. in = s->fs.in;
  233. in[0].time_base = base->time_base;
  234. in[1].time_base = dark->time_base;
  235. in[2].time_base = bright->time_base;
  236. in[0].sync = 1;
  237. in[0].before = EXT_STOP;
  238. in[0].after = EXT_INFINITY;
  239. in[1].sync = 1;
  240. in[1].before = EXT_STOP;
  241. in[1].after = EXT_INFINITY;
  242. in[2].sync = 1;
  243. in[2].before = EXT_STOP;
  244. in[2].after = EXT_INFINITY;
  245. s->fs.opaque = s;
  246. s->fs.on_event = process_frame;
  247. return ff_framesync_configure(&s->fs);
  248. }
  249. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  250. {
  251. MaskedClampContext *s = inlink->dst->priv;
  252. return ff_framesync_filter_frame(&s->fs, inlink, buf);
  253. }
  254. static int request_frame(AVFilterLink *outlink)
  255. {
  256. MaskedClampContext *s = outlink->src->priv;
  257. return ff_framesync_request_frame(&s->fs, outlink);
  258. }
  259. static av_cold void uninit(AVFilterContext *ctx)
  260. {
  261. MaskedClampContext *s = ctx->priv;
  262. ff_framesync_uninit(&s->fs);
  263. }
  264. static const AVFilterPad maskedclamp_inputs[] = {
  265. {
  266. .name = "base",
  267. .type = AVMEDIA_TYPE_VIDEO,
  268. .filter_frame = filter_frame,
  269. .config_props = config_input,
  270. },
  271. {
  272. .name = "dark",
  273. .type = AVMEDIA_TYPE_VIDEO,
  274. .filter_frame = filter_frame,
  275. },
  276. {
  277. .name = "bright",
  278. .type = AVMEDIA_TYPE_VIDEO,
  279. .filter_frame = filter_frame,
  280. },
  281. { NULL }
  282. };
  283. static const AVFilterPad maskedclamp_outputs[] = {
  284. {
  285. .name = "default",
  286. .type = AVMEDIA_TYPE_VIDEO,
  287. .config_props = config_output,
  288. .request_frame = request_frame,
  289. },
  290. { NULL }
  291. };
  292. AVFilter ff_vf_maskedclamp = {
  293. .name = "maskedclamp",
  294. .description = NULL_IF_CONFIG_SMALL("Clamp first stream with second stream and third stream."),
  295. .priv_size = sizeof(MaskedClampContext),
  296. .uninit = uninit,
  297. .query_formats = query_formats,
  298. .inputs = maskedclamp_inputs,
  299. .outputs = maskedclamp_outputs,
  300. .priv_class = &maskedclamp_class,
  301. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  302. };