vf_delogo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2013 Jean Delvare <khali@linux-fr.org>
  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;
  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;
  73. logo_y1 = logo_y + yclipt;
  74. logo_y2 = logo_y + logo_h - yclipb;
  75. topleft = src+logo_y1 * src_linesize+logo_x1;
  76. topright = src+logo_y1 * src_linesize+logo_x2-1;
  77. botleft = src+(logo_y2-1) * 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-1; 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-1; x++, xdst++, xsrc++) {
  92. /* Weighted interpolation based on relative distances, taking SAR into account */
  93. weightl = (uint64_t) (logo_x2-1-x) * (y-logo_y1) * (logo_y2-1-y) * sar.den;
  94. weightr = (uint64_t)(x-logo_x1) * (y-logo_y1) * (logo_y2-1-y) * sar.den;
  95. weightt = (uint64_t)(x-logo_x1) * (logo_x2-1-x) * (logo_y2-1-y) * sar.num;
  96. weightb = (uint64_t)(x-logo_x1) * (logo_x2-1-x) * (y-logo_y1) * sar.num;
  97. interp =
  98. left_sample * weightl
  99. +
  100. right_sample * weightr
  101. +
  102. (topleft[x-logo_x1] +
  103. topleft[x-logo_x1-1] +
  104. topleft[x-logo_x1+1]) * weightt
  105. +
  106. (botleft[x-logo_x1] +
  107. botleft[x-logo_x1-1] +
  108. botleft[x-logo_x1+1]) * weightb;
  109. interp /= (weightl + weightr + weightt + weightb) * 3U;
  110. if (y >= logo_y+band && y < logo_y+logo_h-band &&
  111. x >= logo_x+band && x < logo_x+logo_w-band) {
  112. *xdst = interp;
  113. } else {
  114. unsigned dist = 0;
  115. if (x < logo_x+band)
  116. dist = FFMAX(dist, logo_x-x+band);
  117. else if (x >= logo_x+logo_w-band)
  118. dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
  119. if (y < logo_y+band)
  120. dist = FFMAX(dist, logo_y-y+band);
  121. else if (y >= logo_y+logo_h-band)
  122. dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
  123. *xdst = (*xsrc*dist + interp*(band-dist))/band;
  124. if (show && (dist == band-1))
  125. *xdst = 0;
  126. }
  127. }
  128. dst += dst_linesize;
  129. src += src_linesize;
  130. }
  131. }
  132. typedef struct DelogoContext {
  133. const AVClass *class;
  134. int x, y, w, h, band, show;
  135. } DelogoContext;
  136. #define OFFSET(x) offsetof(DelogoContext, x)
  137. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  138. static const AVOption delogo_options[]= {
  139. { "x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  140. { "y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  141. { "w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  142. { "h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  143. { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, 1, INT_MAX, FLAGS },
  144. { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, 1, INT_MAX, FLAGS },
  145. { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  146. { NULL }
  147. };
  148. AVFILTER_DEFINE_CLASS(delogo);
  149. static int query_formats(AVFilterContext *ctx)
  150. {
  151. static const enum AVPixelFormat pix_fmts[] = {
  152. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  153. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  154. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  155. AV_PIX_FMT_NONE
  156. };
  157. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  158. return 0;
  159. }
  160. static av_cold int init(AVFilterContext *ctx)
  161. {
  162. DelogoContext *s = ctx->priv;
  163. #define CHECK_UNSET_OPT(opt) \
  164. if (s->opt == -1) { \
  165. av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  166. return AVERROR(EINVAL); \
  167. }
  168. CHECK_UNSET_OPT(x);
  169. CHECK_UNSET_OPT(y);
  170. CHECK_UNSET_OPT(w);
  171. CHECK_UNSET_OPT(h);
  172. av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  173. s->x, s->y, s->w, s->h, s->band, s->show);
  174. s->w += s->band*2;
  175. s->h += s->band*2;
  176. s->x -= s->band;
  177. s->y -= s->band;
  178. return 0;
  179. }
  180. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  181. {
  182. DelogoContext *s = inlink->dst->priv;
  183. AVFilterLink *outlink = inlink->dst->outputs[0];
  184. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  185. AVFrame *out;
  186. int hsub0 = desc->log2_chroma_w;
  187. int vsub0 = desc->log2_chroma_h;
  188. int direct = 0;
  189. int plane;
  190. AVRational sar;
  191. if (av_frame_is_writable(in)) {
  192. direct = 1;
  193. out = in;
  194. } else {
  195. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  196. if (!out) {
  197. av_frame_free(&in);
  198. return AVERROR(ENOMEM);
  199. }
  200. av_frame_copy_props(out, in);
  201. }
  202. sar = in->sample_aspect_ratio;
  203. /* Assume square pixels if SAR is unknown */
  204. if (!sar.num)
  205. sar.num = sar.den = 1;
  206. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
  207. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  208. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  209. apply_delogo(out->data[plane], out->linesize[plane],
  210. in ->data[plane], in ->linesize[plane],
  211. FF_CEIL_RSHIFT(inlink->w, hsub),
  212. FF_CEIL_RSHIFT(inlink->h, vsub),
  213. sar, s->x>>hsub, s->y>>vsub,
  214. /* Up and left borders were rounded down, inject lost bits
  215. * into width and height to avoid error accumulation */
  216. FF_CEIL_RSHIFT(s->w + (s->x & ((1<<hsub)-1)), hsub),
  217. FF_CEIL_RSHIFT(s->h + (s->y & ((1<<vsub)-1)), vsub),
  218. s->band>>FFMIN(hsub, vsub),
  219. s->show, direct);
  220. }
  221. if (!direct)
  222. av_frame_free(&in);
  223. return ff_filter_frame(outlink, out);
  224. }
  225. static const AVFilterPad avfilter_vf_delogo_inputs[] = {
  226. {
  227. .name = "default",
  228. .type = AVMEDIA_TYPE_VIDEO,
  229. .filter_frame = filter_frame,
  230. },
  231. { NULL }
  232. };
  233. static const AVFilterPad avfilter_vf_delogo_outputs[] = {
  234. {
  235. .name = "default",
  236. .type = AVMEDIA_TYPE_VIDEO,
  237. },
  238. { NULL }
  239. };
  240. AVFilter ff_vf_delogo = {
  241. .name = "delogo",
  242. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  243. .priv_size = sizeof(DelogoContext),
  244. .priv_class = &delogo_class,
  245. .init = init,
  246. .query_formats = query_formats,
  247. .inputs = avfilter_vf_delogo_inputs,
  248. .outputs = avfilter_vf_delogo_outputs,
  249. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  250. };