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.

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