vf_delogo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/imgutils.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "avfilter.h"
  30. /**
  31. * Apply a simple delogo algorithm to the image in dst and put the
  32. * result in src.
  33. *
  34. * The algorithm is only applied to the region specified by the logo
  35. * parameters.
  36. *
  37. * @param w width of the input image
  38. * @param h height of the input image
  39. * @param logo_x x coordinate of the top left corner of the logo region
  40. * @param logo_y y coordinate of the top left corner of the logo region
  41. * @param logo_w width of the logo
  42. * @param logo_h height of the logo
  43. * @param band the size of the band around the processed area
  44. * @param show show a rectangle around the processed area, useful for
  45. * parameters tweaking
  46. * @param direct if non-zero perform in-place processing
  47. */
  48. static void apply_delogo(uint8_t *dst, int dst_linesize,
  49. uint8_t *src, int src_linesize,
  50. int w, int h,
  51. int logo_x, int logo_y, int logo_w, int logo_h,
  52. int band, int show, int direct)
  53. {
  54. int x, y;
  55. int interp, dist;
  56. uint8_t *xdst, *xsrc;
  57. uint8_t *topleft, *botleft, *topright;
  58. int xclipl, xclipr, yclipt, yclipb;
  59. int logo_x1, logo_x2, logo_y1, logo_y2;
  60. xclipl = FFMAX(-logo_x, 0);
  61. xclipr = FFMAX(logo_x+logo_w-w, 0);
  62. yclipt = FFMAX(-logo_y, 0);
  63. yclipb = FFMAX(logo_y+logo_h-h, 0);
  64. logo_x1 = logo_x + xclipl;
  65. logo_x2 = logo_x + logo_w - xclipr;
  66. logo_y1 = logo_y + yclipt;
  67. logo_y2 = logo_y + logo_h - yclipb;
  68. topleft = src+logo_y1 * src_linesize+logo_x1;
  69. topright = src+logo_y1 * src_linesize+logo_x2-1;
  70. botleft = src+(logo_y2-1) * src_linesize+logo_x1;
  71. dst += (logo_y1+1)*dst_linesize;
  72. src += (logo_y1+1)*src_linesize;
  73. if (!direct)
  74. av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
  75. for (y = logo_y1+1; y < logo_y2-1; y++) {
  76. for (x = logo_x1+1,
  77. xdst = dst+logo_x1+1,
  78. xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
  79. interp =
  80. (topleft[src_linesize*(y-logo_y -yclipt)] +
  81. topleft[src_linesize*(y-logo_y-1-yclipt)] +
  82. topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
  83. +
  84. (topright[src_linesize*(y-logo_y-yclipt)] +
  85. topright[src_linesize*(y-logo_y-1-yclipt)] +
  86. topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
  87. +
  88. (topleft[x-logo_x-xclipl] +
  89. topleft[x-logo_x-1-xclipl] +
  90. topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
  91. +
  92. (botleft[x-logo_x-xclipl] +
  93. botleft[x-logo_x-1-xclipl] +
  94. botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
  95. interp /= 6;
  96. if (y >= logo_y+band && y < logo_y+logo_h-band &&
  97. x >= logo_x+band && x < logo_x+logo_w-band) {
  98. *xdst = interp;
  99. } else {
  100. dist = 0;
  101. if (x < logo_x+band)
  102. dist = FFMAX(dist, logo_x-x+band);
  103. else if (x >= logo_x+logo_w-band)
  104. dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
  105. if (y < logo_y+band)
  106. dist = FFMAX(dist, logo_y-y+band);
  107. else if (y >= logo_y+logo_h-band)
  108. dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
  109. *xdst = (*xsrc*dist + interp*(band-dist))/band;
  110. if (show && (dist == band-1))
  111. *xdst = 0;
  112. }
  113. }
  114. dst += dst_linesize;
  115. src += src_linesize;
  116. }
  117. }
  118. typedef struct {
  119. const AVClass *class;
  120. int x, y, w, h, band, show;
  121. } DelogoContext;
  122. #define OFFSET(x) offsetof(DelogoContext, x)
  123. static const AVOption delogo_options[]= {
  124. {"x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
  125. {"y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
  126. {"w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
  127. {"h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
  128. {"band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.dbl= 4}, -1, INT_MAX },
  129. {"t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.dbl= 4}, -1, INT_MAX },
  130. {"show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, {.dbl= 0}, 0, 1 },
  131. {NULL},
  132. };
  133. static const char *delogo_get_name(void *ctx)
  134. {
  135. return "delogo";
  136. }
  137. static const AVClass delogo_class = {
  138. .class_name = "DelogoContext",
  139. .item_name = delogo_get_name,
  140. .option = delogo_options,
  141. };
  142. static int query_formats(AVFilterContext *ctx)
  143. {
  144. enum PixelFormat pix_fmts[] = {
  145. PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
  146. PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  147. PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
  148. PIX_FMT_NONE
  149. };
  150. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  151. return 0;
  152. }
  153. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  154. {
  155. DelogoContext *delogo = ctx->priv;
  156. int ret = 0;
  157. delogo->class = &delogo_class;
  158. av_opt_set_defaults(delogo);
  159. if (args)
  160. ret = sscanf(args, "%d:%d:%d:%d:%d",
  161. &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
  162. if (ret == 5) {
  163. if (delogo->band < 0)
  164. delogo->show = 1;
  165. } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0) {
  166. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  167. return ret;
  168. }
  169. #define CHECK_UNSET_OPT(opt) \
  170. if (delogo->opt == -1) { \
  171. av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  172. return AVERROR(EINVAL); \
  173. }
  174. CHECK_UNSET_OPT(x);
  175. CHECK_UNSET_OPT(y);
  176. CHECK_UNSET_OPT(w);
  177. CHECK_UNSET_OPT(h);
  178. if (delogo->show)
  179. delogo->band = 4;
  180. av_log(ctx, AV_LOG_INFO, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  181. delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
  182. delogo->w += delogo->band*2;
  183. delogo->h += delogo->band*2;
  184. delogo->x -= delogo->band;
  185. delogo->y -= delogo->band;
  186. return 0;
  187. }
  188. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  189. {
  190. AVFilterLink *outlink = inlink->dst->outputs[0];
  191. AVFilterBufferRef *outpicref;
  192. if (inpicref->perms & AV_PERM_PRESERVE) {
  193. outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
  194. outlink->w, outlink->h);
  195. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  196. outpicref->video->w = outlink->w;
  197. outpicref->video->h = outlink->h;
  198. } else
  199. outpicref = inpicref;
  200. outlink->out_buf = outpicref;
  201. avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
  202. }
  203. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  204. static void end_frame(AVFilterLink *inlink)
  205. {
  206. DelogoContext *delogo = inlink->dst->priv;
  207. AVFilterLink *outlink = inlink->dst->outputs[0];
  208. AVFilterBufferRef *inpicref = inlink ->cur_buf;
  209. AVFilterBufferRef *outpicref = outlink->out_buf;
  210. int direct = inpicref == outpicref;
  211. int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  212. int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  213. int plane;
  214. for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
  215. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  216. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  217. apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
  218. inpicref ->data[plane], inpicref ->linesize[plane],
  219. inlink->w>>hsub, inlink->h>>vsub,
  220. delogo->x>>hsub, delogo->y>>vsub,
  221. delogo->w>>hsub, delogo->h>>vsub,
  222. delogo->band>>FFMIN(hsub, vsub),
  223. delogo->show, direct);
  224. }
  225. avfilter_draw_slice(outlink, 0, inlink->h, 1);
  226. avfilter_end_frame(outlink);
  227. avfilter_unref_buffer(inpicref);
  228. if (!direct)
  229. avfilter_unref_buffer(outpicref);
  230. }
  231. AVFilter avfilter_vf_delogo = {
  232. .name = "delogo",
  233. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  234. .priv_size = sizeof(DelogoContext),
  235. .init = init,
  236. .query_formats = query_formats,
  237. .inputs = (const AVFilterPad[]) {{ .name = "default",
  238. .type = AVMEDIA_TYPE_VIDEO,
  239. .get_video_buffer = avfilter_null_get_video_buffer,
  240. .start_frame = start_frame,
  241. .draw_slice = null_draw_slice,
  242. .end_frame = end_frame,
  243. .min_perms = AV_PERM_WRITE | AV_PERM_READ,
  244. .rej_perms = AV_PERM_PRESERVE },
  245. { .name = NULL}},
  246. .outputs = (const AVFilterPad[]) {{ .name = "default",
  247. .type = AVMEDIA_TYPE_VIDEO, },
  248. { .name = NULL}},
  249. };