vf_delogo.c 10 KB

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