vf_delogo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2013, 2015 Jean Delvare <jdelvare@suse.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. /**
  23. * @file
  24. * A very simple tv station logo remover
  25. * Originally imported from MPlayer libmpcodecs/vf_delogo.c,
  26. * the algorithm was later improved.
  27. */
  28. #include "libavutil/common.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "avfilter.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. /**
  37. * Apply a simple delogo algorithm to the image in src and put the
  38. * result in dst.
  39. *
  40. * The algorithm is only applied to the region specified by the logo
  41. * parameters.
  42. *
  43. * @param w width of the input image
  44. * @param h height of the input image
  45. * @param logo_x x coordinate of the top left corner of the logo region
  46. * @param logo_y y coordinate of the top left corner of the logo region
  47. * @param logo_w width of the logo
  48. * @param logo_h height of the logo
  49. * @param band the size of the band around the processed area
  50. * @param show show a rectangle around the processed area, useful for
  51. * parameters tweaking
  52. * @param direct if non-zero perform in-place processing
  53. */
  54. static void apply_delogo(uint8_t *dst, int dst_linesize,
  55. uint8_t *src, int src_linesize,
  56. int w, int h, AVRational sar,
  57. int logo_x, int logo_y, int logo_w, int logo_h,
  58. unsigned int band, int show, int direct)
  59. {
  60. int x, y;
  61. uint64_t interp, weightl, weightr, weightt, weightb, weight;
  62. uint8_t *xdst, *xsrc;
  63. uint8_t *topleft, *botleft, *topright;
  64. unsigned int left_sample, right_sample;
  65. int xclipl, xclipr, yclipt, yclipb;
  66. int logo_x1, logo_x2, logo_y1, logo_y2;
  67. xclipl = FFMAX(-logo_x, 0);
  68. xclipr = FFMAX(logo_x+logo_w-w, 0);
  69. yclipt = FFMAX(-logo_y, 0);
  70. yclipb = FFMAX(logo_y+logo_h-h, 0);
  71. logo_x1 = logo_x + xclipl;
  72. logo_x2 = logo_x + logo_w - xclipr - 1;
  73. logo_y1 = logo_y + yclipt;
  74. logo_y2 = logo_y + logo_h - yclipb - 1;
  75. topleft = src+logo_y1 * src_linesize+logo_x1;
  76. topright = src+logo_y1 * src_linesize+logo_x2;
  77. botleft = src+logo_y2 * src_linesize+logo_x1;
  78. if (!direct)
  79. av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
  80. dst += (logo_y1 + 1) * dst_linesize;
  81. src += (logo_y1 + 1) * src_linesize;
  82. for (y = logo_y1+1; y < logo_y2; y++) {
  83. left_sample = topleft[src_linesize*(y-logo_y1)] +
  84. topleft[src_linesize*(y-logo_y1-1)] +
  85. topleft[src_linesize*(y-logo_y1+1)];
  86. right_sample = topright[src_linesize*(y-logo_y1)] +
  87. topright[src_linesize*(y-logo_y1-1)] +
  88. topright[src_linesize*(y-logo_y1+1)];
  89. for (x = logo_x1+1,
  90. xdst = dst+logo_x1+1,
  91. xsrc = src+logo_x1+1; x < logo_x2; x++, xdst++, xsrc++) {
  92. if (show && (y == logo_y1+1 || y == logo_y2-1 ||
  93. x == logo_x1+1 || x == logo_x2-1)) {
  94. *xdst = 0;
  95. continue;
  96. }
  97. /* Weighted interpolation based on relative distances, taking SAR into account */
  98. weightl = (uint64_t) (logo_x2-x) * (y-logo_y1) * (logo_y2-y) * sar.den;
  99. weightr = (uint64_t)(x-logo_x1) * (y-logo_y1) * (logo_y2-y) * sar.den;
  100. weightt = (uint64_t)(x-logo_x1) * (logo_x2-x) * (logo_y2-y) * sar.num;
  101. weightb = (uint64_t)(x-logo_x1) * (logo_x2-x) * (y-logo_y1) * sar.num;
  102. interp =
  103. left_sample * weightl
  104. +
  105. right_sample * weightr
  106. +
  107. (topleft[x-logo_x1] +
  108. topleft[x-logo_x1-1] +
  109. topleft[x-logo_x1+1]) * weightt
  110. +
  111. (botleft[x-logo_x1] +
  112. botleft[x-logo_x1-1] +
  113. botleft[x-logo_x1+1]) * weightb;
  114. weight = (weightl + weightr + weightt + weightb) * 3U;
  115. interp = ROUNDED_DIV(interp, weight);
  116. if (y >= logo_y+band && y < logo_y+logo_h-band &&
  117. x >= logo_x+band && x < logo_x+logo_w-band) {
  118. *xdst = interp;
  119. } else {
  120. unsigned dist = 0;
  121. if (x < logo_x+band)
  122. dist = FFMAX(dist, logo_x-x+band);
  123. else if (x >= logo_x+logo_w-band)
  124. dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
  125. if (y < logo_y+band)
  126. dist = FFMAX(dist, logo_y-y+band);
  127. else if (y >= logo_y+logo_h-band)
  128. dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
  129. *xdst = (*xsrc*dist + interp*(band-dist))/band;
  130. }
  131. }
  132. dst += dst_linesize;
  133. src += src_linesize;
  134. }
  135. }
  136. typedef struct DelogoContext {
  137. const AVClass *class;
  138. int x, y, w, h, band, show;
  139. } DelogoContext;
  140. #define OFFSET(x) offsetof(DelogoContext, x)
  141. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  142. static const AVOption delogo_options[]= {
  143. { "x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  144. { "y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  145. { "w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  146. { "h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  147. #if LIBAVFILTER_VERSION_MAJOR < 7
  148. /* Actual default value for band/t is 1, set in init */
  149. { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  150. { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  151. #endif
  152. { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, FLAGS },
  153. { NULL }
  154. };
  155. AVFILTER_DEFINE_CLASS(delogo);
  156. static int query_formats(AVFilterContext *ctx)
  157. {
  158. static const enum AVPixelFormat pix_fmts[] = {
  159. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  160. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  161. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  162. AV_PIX_FMT_NONE
  163. };
  164. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  165. if (!fmts_list)
  166. return AVERROR(ENOMEM);
  167. return ff_set_common_formats(ctx, fmts_list);
  168. }
  169. static av_cold int init(AVFilterContext *ctx)
  170. {
  171. DelogoContext *s = ctx->priv;
  172. #define CHECK_UNSET_OPT(opt) \
  173. if (s->opt == -1) { \
  174. av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  175. return AVERROR(EINVAL); \
  176. }
  177. CHECK_UNSET_OPT(x);
  178. CHECK_UNSET_OPT(y);
  179. CHECK_UNSET_OPT(w);
  180. CHECK_UNSET_OPT(h);
  181. #if LIBAVFILTER_VERSION_MAJOR < 7
  182. if (s->band == 0) { /* Unset, use default */
  183. av_log(ctx, AV_LOG_WARNING, "Note: default band value was changed from 4 to 1.\n");
  184. s->band = 1;
  185. } else if (s->band != 1) {
  186. av_log(ctx, AV_LOG_WARNING, "Option band is deprecated.\n");
  187. }
  188. #else
  189. s->band = 1;
  190. #endif
  191. av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  192. s->x, s->y, s->w, s->h, s->band, s->show);
  193. s->w += s->band*2;
  194. s->h += s->band*2;
  195. s->x -= s->band;
  196. s->y -= s->band;
  197. return 0;
  198. }
  199. static int config_input(AVFilterLink *inlink)
  200. {
  201. DelogoContext *s = inlink->dst->priv;
  202. /* Check whether the logo area fits in the frame */
  203. if (s->x + (s->band - 1) < 0 || s->x + s->w - (s->band*2 - 2) > inlink->w ||
  204. s->y + (s->band - 1) < 0 || s->y + s->h - (s->band*2 - 2) > inlink->h) {
  205. av_log(s, AV_LOG_ERROR, "Logo area is outside of the frame.\n");
  206. return AVERROR(EINVAL);
  207. }
  208. return 0;
  209. }
  210. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  211. {
  212. DelogoContext *s = inlink->dst->priv;
  213. AVFilterLink *outlink = inlink->dst->outputs[0];
  214. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  215. AVFrame *out;
  216. int hsub0 = desc->log2_chroma_w;
  217. int vsub0 = desc->log2_chroma_h;
  218. int direct = 0;
  219. int plane;
  220. AVRational sar;
  221. if (av_frame_is_writable(in)) {
  222. direct = 1;
  223. out = in;
  224. } else {
  225. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  226. if (!out) {
  227. av_frame_free(&in);
  228. return AVERROR(ENOMEM);
  229. }
  230. av_frame_copy_props(out, in);
  231. }
  232. sar = in->sample_aspect_ratio;
  233. /* Assume square pixels if SAR is unknown */
  234. if (!sar.num)
  235. sar.num = sar.den = 1;
  236. for (plane = 0; plane < desc->nb_components; plane++) {
  237. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  238. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  239. apply_delogo(out->data[plane], out->linesize[plane],
  240. in ->data[plane], in ->linesize[plane],
  241. AV_CEIL_RSHIFT(inlink->w, hsub),
  242. AV_CEIL_RSHIFT(inlink->h, vsub),
  243. sar, s->x>>hsub, s->y>>vsub,
  244. /* Up and left borders were rounded down, inject lost bits
  245. * into width and height to avoid error accumulation */
  246. AV_CEIL_RSHIFT(s->w + (s->x & ((1<<hsub)-1)), hsub),
  247. AV_CEIL_RSHIFT(s->h + (s->y & ((1<<vsub)-1)), vsub),
  248. s->band>>FFMIN(hsub, vsub),
  249. s->show, direct);
  250. }
  251. if (!direct)
  252. av_frame_free(&in);
  253. return ff_filter_frame(outlink, out);
  254. }
  255. static const AVFilterPad avfilter_vf_delogo_inputs[] = {
  256. {
  257. .name = "default",
  258. .type = AVMEDIA_TYPE_VIDEO,
  259. .filter_frame = filter_frame,
  260. .config_props = config_input,
  261. },
  262. { NULL }
  263. };
  264. static const AVFilterPad avfilter_vf_delogo_outputs[] = {
  265. {
  266. .name = "default",
  267. .type = AVMEDIA_TYPE_VIDEO,
  268. },
  269. { NULL }
  270. };
  271. AVFilter ff_vf_delogo = {
  272. .name = "delogo",
  273. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  274. .priv_size = sizeof(DelogoContext),
  275. .priv_class = &delogo_class,
  276. .init = init,
  277. .query_formats = query_formats,
  278. .inputs = avfilter_vf_delogo_inputs,
  279. .outputs = avfilter_vf_delogo_outputs,
  280. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  281. };