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.

286 lines
10KB

  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/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 = (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. + (topright[src_linesize*(y-logo_y-yclipt)] +
  84. topright[src_linesize*(y-logo_y-1-yclipt)] +
  85. topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
  86. + (topleft[x-logo_x-xclipl] +
  87. topleft[x-logo_x-1-xclipl] +
  88. topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
  89. + (botleft[x-logo_x-xclipl] +
  90. botleft[x-logo_x-1-xclipl] +
  91. botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
  92. interp /= 6;
  93. if (y >= logo_y+band && y < logo_y+logo_h-band &&
  94. x >= logo_x+band && x < logo_x+logo_w-band) {
  95. *xdst = interp;
  96. } else {
  97. dist = 0;
  98. if (x < logo_x+band)
  99. dist = FFMAX(dist, logo_x-x+band);
  100. else if (x >= logo_x+logo_w-band)
  101. dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
  102. if (y < logo_y+band)
  103. dist = FFMAX(dist, logo_y-y+band);
  104. else if (y >= logo_y+logo_h-band)
  105. dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
  106. *xdst = (*xsrc*dist + interp*(band-dist))/band;
  107. if (show && (dist == band-1))
  108. *xdst = 0;
  109. }
  110. }
  111. dst += dst_linesize;
  112. src += src_linesize;
  113. }
  114. }
  115. typedef struct {
  116. const AVClass *class;
  117. int x, y, w, h, band, show;
  118. } DelogoContext;
  119. #define OFFSET(x) offsetof(DelogoContext, x)
  120. static const AVOption delogo_options[]= {
  121. {"x", "set logo x position", OFFSET(x), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
  122. {"y", "set logo y position", OFFSET(y), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
  123. {"w", "set logo width", OFFSET(w), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
  124. {"h", "set logo height", OFFSET(h), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
  125. {"band", "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, { 4}, -1, INT_MAX },
  126. {"t", "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, { 4}, -1, INT_MAX },
  127. {"show", "show delogo area", OFFSET(show), FF_OPT_TYPE_INT, { 0}, 0, 1 },
  128. {NULL},
  129. };
  130. static const char *delogo_get_name(void *ctx)
  131. {
  132. return "delogo";
  133. }
  134. static const AVClass delogo_class = {
  135. .class_name = "DelogoContext",
  136. .item_name = delogo_get_name,
  137. .option = delogo_options,
  138. };
  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. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  148. return 0;
  149. }
  150. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  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. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  164. return ret;
  165. }
  166. #define CHECK_UNSET_OPT(opt) \
  167. if (delogo->opt == -1) { \
  168. av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  169. return AVERROR(EINVAL); \
  170. }
  171. CHECK_UNSET_OPT(x);
  172. CHECK_UNSET_OPT(y);
  173. CHECK_UNSET_OPT(w);
  174. CHECK_UNSET_OPT(h);
  175. if (delogo->show)
  176. delogo->band = 4;
  177. av_log(ctx, AV_LOG_DEBUG, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  178. delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
  179. delogo->w += delogo->band*2;
  180. delogo->h += delogo->band*2;
  181. delogo->x -= delogo->band;
  182. delogo->y -= delogo->band;
  183. return 0;
  184. }
  185. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  186. {
  187. AVFilterLink *outlink = inlink->dst->outputs[0];
  188. AVFilterBufferRef *outpicref;
  189. if (inpicref->perms & AV_PERM_PRESERVE) {
  190. outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
  191. outlink->w, outlink->h);
  192. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  193. outpicref->video->w = outlink->w;
  194. outpicref->video->h = outlink->h;
  195. } else
  196. outpicref = inpicref;
  197. outlink->out_buf = outpicref;
  198. avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
  199. }
  200. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  201. static void end_frame(AVFilterLink *inlink)
  202. {
  203. DelogoContext *delogo = inlink->dst->priv;
  204. AVFilterLink *outlink = inlink->dst->outputs[0];
  205. AVFilterBufferRef *inpicref = inlink ->cur_buf;
  206. AVFilterBufferRef *outpicref = outlink->out_buf;
  207. int direct = inpicref == outpicref;
  208. int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  209. int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  210. int plane;
  211. for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
  212. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  213. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  214. apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
  215. inpicref ->data[plane], inpicref ->linesize[plane],
  216. inlink->w>>hsub, inlink->h>>vsub,
  217. delogo->x>>hsub, delogo->y>>vsub,
  218. delogo->w>>hsub, delogo->h>>vsub,
  219. delogo->band>>FFMIN(hsub, vsub),
  220. delogo->show, direct);
  221. }
  222. avfilter_draw_slice(outlink, 0, inlink->h, 1);
  223. avfilter_end_frame(outlink);
  224. avfilter_unref_buffer(inpicref);
  225. if (!direct)
  226. avfilter_unref_buffer(outpicref);
  227. }
  228. AVFilter avfilter_vf_delogo = {
  229. .name = "delogo",
  230. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  231. .priv_size = sizeof(DelogoContext),
  232. .init = init,
  233. .query_formats = query_formats,
  234. .inputs = (AVFilterPad[]) {{ .name = "default",
  235. .type = AVMEDIA_TYPE_VIDEO,
  236. .get_video_buffer = ff_null_get_video_buffer,
  237. .start_frame = start_frame,
  238. .draw_slice = null_draw_slice,
  239. .end_frame = end_frame,
  240. .min_perms = AV_PERM_WRITE | AV_PERM_READ,
  241. .rej_perms = AV_PERM_PRESERVE },
  242. { .name = NULL}},
  243. .outputs = (AVFilterPad[]) {{ .name = "default",
  244. .type = AVMEDIA_TYPE_VIDEO, },
  245. { .name = NULL}},
  246. };