vf_delogo.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. if (!direct)
  76. av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
  77. dst += (logo_y1 + 1) * dst_linesize;
  78. src += (logo_y1 + 1) * src_linesize;
  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 = (topleft[src_linesize*(y-logo_y -yclipt)] +
  84. topleft[src_linesize*(y-logo_y-1-yclipt)] +
  85. topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
  86. + (topright[src_linesize*(y-logo_y-yclipt)] +
  87. topright[src_linesize*(y-logo_y-1-yclipt)] +
  88. topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
  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. + (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 DelogoContext {
  119. const AVClass *class;
  120. int x, y, w, h, band, show;
  121. } DelogoContext;
  122. #define OFFSET(x) offsetof(DelogoContext, x)
  123. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  124. static const AVOption delogo_options[]= {
  125. { "x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  126. { "y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  127. { "w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  128. { "h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  129. { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
  130. { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
  131. { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  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 AVPixelFormat pix_fmts[] = {
  146. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  147. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  148. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  149. AV_PIX_FMT_NONE
  150. };
  151. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  152. return 0;
  153. }
  154. static av_cold int init(AVFilterContext *ctx)
  155. {
  156. DelogoContext *s = ctx->priv;
  157. #define CHECK_UNSET_OPT(opt) \
  158. if (s->opt == -1) { \
  159. av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  160. return AVERROR(EINVAL); \
  161. }
  162. CHECK_UNSET_OPT(x);
  163. CHECK_UNSET_OPT(y);
  164. CHECK_UNSET_OPT(w);
  165. CHECK_UNSET_OPT(h);
  166. if (s->show)
  167. s->band = 4;
  168. av_log(ctx, AV_LOG_DEBUG, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  169. s->x, s->y, s->w, s->h, s->band, s->show);
  170. s->w += s->band*2;
  171. s->h += s->band*2;
  172. s->x -= s->band;
  173. s->y -= s->band;
  174. return 0;
  175. }
  176. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  177. {
  178. DelogoContext *s = inlink->dst->priv;
  179. AVFilterLink *outlink = inlink->dst->outputs[0];
  180. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  181. AVFrame *out;
  182. int hsub0 = desc->log2_chroma_w;
  183. int vsub0 = desc->log2_chroma_h;
  184. int direct = 0;
  185. int plane;
  186. if (av_frame_is_writable(in)) {
  187. direct = 1;
  188. out = in;
  189. } else {
  190. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  191. if (!out) {
  192. av_frame_free(&in);
  193. return AVERROR(ENOMEM);
  194. }
  195. av_frame_copy_props(out, in);
  196. out->width = outlink->w;
  197. out->height = outlink->h;
  198. }
  199. for (plane = 0; plane < 4 && in->data[plane]; plane++) {
  200. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  201. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  202. apply_delogo(out->data[plane], out->linesize[plane],
  203. in ->data[plane], in ->linesize[plane],
  204. inlink->w>>hsub, inlink->h>>vsub,
  205. s->x>>hsub, s->y>>vsub,
  206. s->w>>hsub, s->h>>vsub,
  207. s->band>>FFMIN(hsub, vsub),
  208. s->show, direct);
  209. }
  210. if (!direct)
  211. av_frame_free(&in);
  212. return ff_filter_frame(outlink, out);
  213. }
  214. static const AVFilterPad avfilter_vf_delogo_inputs[] = {
  215. {
  216. .name = "default",
  217. .type = AVMEDIA_TYPE_VIDEO,
  218. .get_video_buffer = ff_null_get_video_buffer,
  219. .filter_frame = filter_frame,
  220. },
  221. { NULL }
  222. };
  223. static const AVFilterPad avfilter_vf_delogo_outputs[] = {
  224. {
  225. .name = "default",
  226. .type = AVMEDIA_TYPE_VIDEO,
  227. },
  228. { NULL }
  229. };
  230. AVFilter ff_vf_delogo = {
  231. .name = "delogo",
  232. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  233. .priv_size = sizeof(DelogoContext),
  234. .priv_class = &delogo_class,
  235. .init = init,
  236. .query_formats = query_formats,
  237. .inputs = avfilter_vf_delogo_inputs,
  238. .outputs = avfilter_vf_delogo_outputs,
  239. };