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.

285 lines
9.7KB

  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,
  57. int logo_x, int logo_y, int logo_w, int logo_h,
  58. int band, int show, int direct)
  59. {
  60. int x, y, dist;
  61. uint64_t interp, weightl, weightr, weightt, weightb;
  62. uint8_t *xdst, *xsrc;
  63. uint8_t *topleft, *botleft, *topright;
  64. int xclipl, xclipr, yclipt, yclipb;
  65. int logo_x1, logo_x2, logo_y1, logo_y2;
  66. xclipl = FFMAX(-logo_x, 0);
  67. xclipr = FFMAX(logo_x+logo_w-w, 0);
  68. yclipt = FFMAX(-logo_y, 0);
  69. yclipb = FFMAX(logo_y+logo_h-h, 0);
  70. logo_x1 = logo_x + xclipl;
  71. logo_x2 = logo_x + logo_w - xclipr;
  72. logo_y1 = logo_y + yclipt;
  73. logo_y2 = logo_y + logo_h - yclipb;
  74. topleft = src+logo_y1 * src_linesize+logo_x1;
  75. topright = src+logo_y1 * src_linesize+logo_x2-1;
  76. botleft = src+(logo_y2-1) * src_linesize+logo_x1;
  77. if (!direct)
  78. av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
  79. dst += (logo_y1 + 1) * dst_linesize;
  80. src += (logo_y1 + 1) * src_linesize;
  81. for (y = logo_y1+1; y < logo_y2-1; y++) {
  82. for (x = logo_x1+1,
  83. xdst = dst+logo_x1+1,
  84. xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
  85. /* Weighted interpolation based on relative distances */
  86. weightl = (uint64_t) (logo_x2-1-x) * (y-logo_y1) * (logo_y2-1-y);
  87. weightr = (uint64_t)(x-logo_x1) * (y-logo_y1) * (logo_y2-1-y);
  88. weightt = (uint64_t)(x-logo_x1) * (logo_x2-1-x) * (logo_y2-1-y);
  89. weightb = (uint64_t)(x-logo_x1) * (logo_x2-1-x) * (y-logo_y1);
  90. interp =
  91. (topleft[src_linesize*(y-logo_y -yclipt)] +
  92. topleft[src_linesize*(y-logo_y-1-yclipt)] +
  93. topleft[src_linesize*(y-logo_y+1-yclipt)]) * weightl
  94. +
  95. (topright[src_linesize*(y-logo_y-yclipt)] +
  96. topright[src_linesize*(y-logo_y-1-yclipt)] +
  97. topright[src_linesize*(y-logo_y+1-yclipt)]) * weightr
  98. +
  99. (topleft[x-logo_x-xclipl] +
  100. topleft[x-logo_x-1-xclipl] +
  101. topleft[x-logo_x+1-xclipl]) * weightt
  102. +
  103. (botleft[x-logo_x-xclipl] +
  104. botleft[x-logo_x-1-xclipl] +
  105. botleft[x-logo_x+1-xclipl]) * weightb;
  106. interp /= (weightl + weightr + weightt + weightb) * 3U;
  107. if (y >= logo_y+band && y < logo_y+logo_h-band &&
  108. x >= logo_x+band && x < logo_x+logo_w-band) {
  109. *xdst = interp;
  110. } else {
  111. dist = 0;
  112. if (x < logo_x+band)
  113. dist = FFMAX(dist, logo_x-x+band);
  114. else if (x >= logo_x+logo_w-band)
  115. dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
  116. if (y < logo_y+band)
  117. dist = FFMAX(dist, logo_y-y+band);
  118. else if (y >= logo_y+logo_h-band)
  119. dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
  120. *xdst = (*xsrc*dist + interp*(band-dist))/band;
  121. if (show && (dist == band-1))
  122. *xdst = 0;
  123. }
  124. }
  125. dst += dst_linesize;
  126. src += src_linesize;
  127. }
  128. }
  129. typedef struct {
  130. const AVClass *class;
  131. int x, y, w, h, band, show;
  132. } DelogoContext;
  133. #define OFFSET(x) offsetof(DelogoContext, x)
  134. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  135. static const AVOption delogo_options[]= {
  136. { "x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  137. { "y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  138. { "w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  139. { "h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  140. { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
  141. { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
  142. { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  143. { NULL },
  144. };
  145. AVFILTER_DEFINE_CLASS(delogo);
  146. static int query_formats(AVFilterContext *ctx)
  147. {
  148. static const enum AVPixelFormat pix_fmts[] = {
  149. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  150. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  151. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  152. AV_PIX_FMT_NONE
  153. };
  154. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  155. return 0;
  156. }
  157. static av_cold int init(AVFilterContext *ctx)
  158. {
  159. DelogoContext *s = ctx->priv;
  160. #define CHECK_UNSET_OPT(opt) \
  161. if (s->opt == -1) { \
  162. av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  163. return AVERROR(EINVAL); \
  164. }
  165. CHECK_UNSET_OPT(x);
  166. CHECK_UNSET_OPT(y);
  167. CHECK_UNSET_OPT(w);
  168. CHECK_UNSET_OPT(h);
  169. if (s->band < 0 || s->show) {
  170. s->show = 1;
  171. s->band = 4;
  172. }
  173. av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  174. s->x, s->y, s->w, s->h, s->band, s->show);
  175. s->w += s->band*2;
  176. s->h += s->band*2;
  177. s->x -= s->band;
  178. s->y -= s->band;
  179. return 0;
  180. }
  181. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  182. {
  183. DelogoContext *s = inlink->dst->priv;
  184. AVFilterLink *outlink = inlink->dst->outputs[0];
  185. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  186. AVFrame *out;
  187. int hsub0 = desc->log2_chroma_w;
  188. int vsub0 = desc->log2_chroma_h;
  189. int direct = 0;
  190. int plane;
  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. for (plane = 0; plane < 4 && in->data[plane]; plane++) {
  203. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  204. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  205. apply_delogo(out->data[plane], out->linesize[plane],
  206. in ->data[plane], in ->linesize[plane],
  207. FF_CEIL_RSHIFT(inlink->w, hsub),
  208. FF_CEIL_RSHIFT(inlink->h, vsub),
  209. s->x>>hsub, s->y>>vsub,
  210. FF_CEIL_RSHIFT(s->w, hsub),
  211. FF_CEIL_RSHIFT(s->h, vsub),
  212. s->band>>FFMIN(hsub, vsub),
  213. s->show, direct);
  214. }
  215. if (!direct)
  216. av_frame_free(&in);
  217. return ff_filter_frame(outlink, out);
  218. }
  219. static const AVFilterPad avfilter_vf_delogo_inputs[] = {
  220. {
  221. .name = "default",
  222. .type = AVMEDIA_TYPE_VIDEO,
  223. .get_video_buffer = ff_null_get_video_buffer,
  224. .filter_frame = filter_frame,
  225. },
  226. { NULL }
  227. };
  228. static const AVFilterPad avfilter_vf_delogo_outputs[] = {
  229. {
  230. .name = "default",
  231. .type = AVMEDIA_TYPE_VIDEO,
  232. },
  233. { NULL }
  234. };
  235. AVFilter avfilter_vf_delogo = {
  236. .name = "delogo",
  237. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  238. .priv_size = sizeof(DelogoContext),
  239. .priv_class = &delogo_class,
  240. .init = init,
  241. .query_formats = query_formats,
  242. .inputs = avfilter_vf_delogo_inputs,
  243. .outputs = avfilter_vf_delogo_outputs,
  244. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  245. };