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.

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