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.

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