vf_delogo.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. /**
  22. * @file
  23. * A very simple tv station logo remover
  24. * Ported from MPlayer libmpcodecs/vf_delogo.c.
  25. */
  26. #include "libavutil/common.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. /**
  35. * Apply a simple delogo algorithm to the image in dst and put the
  36. * result in src.
  37. *
  38. * The algorithm is only applied to the region specified by the logo
  39. * parameters.
  40. *
  41. * @param w width of the input image
  42. * @param h height of the input image
  43. * @param logo_x x coordinate of the top left corner of the logo region
  44. * @param logo_y y coordinate of the top left corner of the logo region
  45. * @param logo_w width of the logo
  46. * @param logo_h height of the logo
  47. * @param band the size of the band around the processed area
  48. * @param show show a rectangle around the processed area, useful for
  49. * parameters tweaking
  50. * @param direct if non-zero perform in-place processing
  51. */
  52. static void apply_delogo(uint8_t *dst, int dst_linesize,
  53. uint8_t *src, int src_linesize,
  54. int w, int h,
  55. int logo_x, int logo_y, int logo_w, int logo_h,
  56. int band, int show, int direct)
  57. {
  58. int x, y;
  59. int interp, dist;
  60. uint8_t *xdst, *xsrc;
  61. uint8_t *topleft, *botleft, *topright;
  62. int xclipl, xclipr, yclipt, yclipb;
  63. int logo_x1, logo_x2, logo_y1, logo_y2;
  64. xclipl = FFMAX(-logo_x, 0);
  65. xclipr = FFMAX(logo_x+logo_w-w, 0);
  66. yclipt = FFMAX(-logo_y, 0);
  67. yclipb = FFMAX(logo_y+logo_h-h, 0);
  68. logo_x1 = logo_x + xclipl;
  69. logo_x2 = logo_x + logo_w - xclipr;
  70. logo_y1 = logo_y + yclipt;
  71. logo_y2 = logo_y + logo_h - yclipb;
  72. topleft = src+logo_y1 * src_linesize+logo_x1;
  73. topright = src+logo_y1 * src_linesize+logo_x2-1;
  74. botleft = src+(logo_y2-1) * src_linesize+logo_x1;
  75. dst += (logo_y1+1)*dst_linesize;
  76. src += (logo_y1+1)*src_linesize;
  77. if (!direct)
  78. av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
  79. for (y = logo_y1+1; y < logo_y2-1; y++) {
  80. for (x = logo_x1+1,
  81. xdst = dst+logo_x1+1,
  82. xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
  83. interp =
  84. (topleft[src_linesize*(y-logo_y -yclipt)] +
  85. topleft[src_linesize*(y-logo_y-1-yclipt)] +
  86. topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
  87. +
  88. (topright[src_linesize*(y-logo_y-yclipt)] +
  89. topright[src_linesize*(y-logo_y-1-yclipt)] +
  90. topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
  91. +
  92. (topleft[x-logo_x-xclipl] +
  93. topleft[x-logo_x-1-xclipl] +
  94. topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
  95. +
  96. (botleft[x-logo_x-xclipl] +
  97. botleft[x-logo_x-1-xclipl] +
  98. botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
  99. interp /= 6;
  100. if (y >= logo_y+band && y < logo_y+logo_h-band &&
  101. x >= logo_x+band && x < logo_x+logo_w-band) {
  102. *xdst = interp;
  103. } else {
  104. dist = 0;
  105. if (x < logo_x+band)
  106. dist = FFMAX(dist, logo_x-x+band);
  107. else if (x >= logo_x+logo_w-band)
  108. dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
  109. if (y < logo_y+band)
  110. dist = FFMAX(dist, logo_y-y+band);
  111. else if (y >= logo_y+logo_h-band)
  112. dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
  113. *xdst = (*xsrc*dist + interp*(band-dist))/band;
  114. if (show && (dist == band-1))
  115. *xdst = 0;
  116. }
  117. }
  118. dst += dst_linesize;
  119. src += src_linesize;
  120. }
  121. }
  122. typedef struct {
  123. const AVClass *class;
  124. int x, y, w, h, band, show;
  125. } DelogoContext;
  126. #define OFFSET(x) offsetof(DelogoContext, x)
  127. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  128. static const AVOption delogo_options[]= {
  129. {"x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
  130. {"y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
  131. {"w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
  132. {"h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
  133. {"band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.i64 = 4}, -1, INT_MAX, FLAGS},
  134. {"t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.i64 = 4}, -1, INT_MAX, FLAGS},
  135. {"show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS},
  136. {NULL},
  137. };
  138. AVFILTER_DEFINE_CLASS(delogo);
  139. static int query_formats(AVFilterContext *ctx)
  140. {
  141. enum PixelFormat pix_fmts[] = {
  142. PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
  143. PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  144. PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
  145. PIX_FMT_NONE
  146. };
  147. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  148. return 0;
  149. }
  150. static av_cold int init(AVFilterContext *ctx, const char *args)
  151. {
  152. DelogoContext *delogo = ctx->priv;
  153. int ret = 0;
  154. delogo->class = &delogo_class;
  155. av_opt_set_defaults(delogo);
  156. if (args)
  157. ret = sscanf(args, "%d:%d:%d:%d:%d",
  158. &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
  159. if (ret == 5) {
  160. if (delogo->band < 0)
  161. delogo->show = 1;
  162. } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0)
  163. return ret;
  164. #define CHECK_UNSET_OPT(opt) \
  165. if (delogo->opt == -1) { \
  166. av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  167. return AVERROR(EINVAL); \
  168. }
  169. CHECK_UNSET_OPT(x);
  170. CHECK_UNSET_OPT(y);
  171. CHECK_UNSET_OPT(w);
  172. CHECK_UNSET_OPT(h);
  173. if (delogo->show)
  174. delogo->band = 4;
  175. av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  176. delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
  177. delogo->w += delogo->band*2;
  178. delogo->h += delogo->band*2;
  179. delogo->x -= delogo->band;
  180. delogo->y -= delogo->band;
  181. return 0;
  182. }
  183. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  184. {
  185. return 0;
  186. }
  187. static int end_frame(AVFilterLink *inlink)
  188. {
  189. DelogoContext *delogo = inlink->dst->priv;
  190. AVFilterLink *outlink = inlink->dst->outputs[0];
  191. AVFilterBufferRef *inpicref = inlink ->cur_buf;
  192. AVFilterBufferRef *outpicref = outlink->out_buf;
  193. int direct = inpicref->buf == outpicref->buf;
  194. int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  195. int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  196. int plane;
  197. int ret;
  198. for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
  199. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  200. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  201. apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
  202. inpicref ->data[plane], inpicref ->linesize[plane],
  203. inlink->w>>hsub, inlink->h>>vsub,
  204. delogo->x>>hsub, delogo->y>>vsub,
  205. delogo->w>>hsub, delogo->h>>vsub,
  206. delogo->band>>FFMIN(hsub, vsub),
  207. delogo->show, direct);
  208. }
  209. if ((ret = ff_draw_slice(outlink, 0, inlink->h, 1)) < 0 ||
  210. (ret = ff_end_frame(outlink)) < 0)
  211. return ret;
  212. return 0;
  213. }
  214. AVFilter avfilter_vf_delogo = {
  215. .name = "delogo",
  216. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  217. .priv_size = sizeof(DelogoContext),
  218. .init = init,
  219. .query_formats = query_formats,
  220. .inputs = (const AVFilterPad[]) {{ .name = "default",
  221. .type = AVMEDIA_TYPE_VIDEO,
  222. .get_video_buffer = ff_null_get_video_buffer,
  223. .start_frame = ff_inplace_start_frame,
  224. .draw_slice = null_draw_slice,
  225. .end_frame = end_frame,
  226. .min_perms = AV_PERM_WRITE | AV_PERM_READ },
  227. { .name = NULL}},
  228. .outputs = (const AVFilterPad[]) {{ .name = "default",
  229. .type = AVMEDIA_TYPE_VIDEO, },
  230. { .name = NULL}},
  231. };