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.

276 lines
9.2KB

  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/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 =
  84. (topleft[src_linesize*(y-logo_y -yclipt)] +
  85. topleft[src_linesize*(y-logo_y-1-yclipt)] +
  86. topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
  87. +
  88. (topright[src_linesize*(y-logo_y-yclipt)] +
  89. topright[src_linesize*(y-logo_y-1-yclipt)] +
  90. topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
  91. +
  92. (topleft[x-logo_x-xclipl] +
  93. topleft[x-logo_x-1-xclipl] +
  94. topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
  95. +
  96. (botleft[x-logo_x-xclipl] +
  97. botleft[x-logo_x-1-xclipl] +
  98. botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
  99. interp /= 6;
  100. if (y >= logo_y+band && y < logo_y+logo_h-band &&
  101. x >= logo_x+band && x < logo_x+logo_w-band) {
  102. *xdst = interp;
  103. } else {
  104. dist = 0;
  105. if (x < logo_x+band)
  106. dist = FFMAX(dist, logo_x-x+band);
  107. else if (x >= logo_x+logo_w-band)
  108. dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
  109. if (y < logo_y+band)
  110. dist = FFMAX(dist, logo_y-y+band);
  111. else if (y >= logo_y+logo_h-band)
  112. dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
  113. *xdst = (*xsrc*dist + interp*(band-dist))/band;
  114. if (show && (dist == band-1))
  115. *xdst = 0;
  116. }
  117. }
  118. dst += dst_linesize;
  119. src += src_linesize;
  120. }
  121. }
  122. typedef struct {
  123. const AVClass *class;
  124. int x, y, w, h, band, show;
  125. } DelogoContext;
  126. #define OFFSET(x) offsetof(DelogoContext, x)
  127. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  128. static const AVOption delogo_options[]= {
  129. { "x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  130. { "y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  131. { "w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  132. { "h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  133. { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
  134. { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
  135. { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  136. { NULL },
  137. };
  138. AVFILTER_DEFINE_CLASS(delogo);
  139. static int query_formats(AVFilterContext *ctx)
  140. {
  141. static const enum AVPixelFormat pix_fmts[] = {
  142. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  143. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  144. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  145. AV_PIX_FMT_NONE
  146. };
  147. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  148. return 0;
  149. }
  150. static av_cold int init(AVFilterContext *ctx)
  151. {
  152. DelogoContext *s = ctx->priv;
  153. #define CHECK_UNSET_OPT(opt) \
  154. if (s->opt == -1) { \
  155. av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  156. return AVERROR(EINVAL); \
  157. }
  158. CHECK_UNSET_OPT(x);
  159. CHECK_UNSET_OPT(y);
  160. CHECK_UNSET_OPT(w);
  161. CHECK_UNSET_OPT(h);
  162. if (s->band < 0 || s->show) {
  163. s->show = 1;
  164. s->band = 4;
  165. }
  166. av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  167. s->x, s->y, s->w, s->h, s->band, s->show);
  168. s->w += s->band*2;
  169. s->h += s->band*2;
  170. s->x -= s->band;
  171. s->y -= s->band;
  172. return 0;
  173. }
  174. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  175. {
  176. DelogoContext *s = inlink->dst->priv;
  177. AVFilterLink *outlink = inlink->dst->outputs[0];
  178. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  179. AVFrame *out;
  180. int hsub0 = desc->log2_chroma_w;
  181. int vsub0 = desc->log2_chroma_h;
  182. int direct = 0;
  183. int plane;
  184. if (av_frame_is_writable(in)) {
  185. direct = 1;
  186. out = in;
  187. } else {
  188. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  189. if (!out) {
  190. av_frame_free(&in);
  191. return AVERROR(ENOMEM);
  192. }
  193. av_frame_copy_props(out, in);
  194. }
  195. for (plane = 0; plane < 4 && in->data[plane]; plane++) {
  196. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  197. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  198. apply_delogo(out->data[plane], out->linesize[plane],
  199. in ->data[plane], in ->linesize[plane],
  200. FF_CEIL_RSHIFT(inlink->w, hsub),
  201. FF_CEIL_RSHIFT(inlink->h, vsub),
  202. s->x>>hsub, s->y>>vsub,
  203. FF_CEIL_RSHIFT(s->w, hsub),
  204. FF_CEIL_RSHIFT(s->h, vsub),
  205. s->band>>FFMIN(hsub, vsub),
  206. s->show, direct);
  207. }
  208. if (!direct)
  209. av_frame_free(&in);
  210. return ff_filter_frame(outlink, out);
  211. }
  212. static const AVFilterPad avfilter_vf_delogo_inputs[] = {
  213. {
  214. .name = "default",
  215. .type = AVMEDIA_TYPE_VIDEO,
  216. .get_video_buffer = ff_null_get_video_buffer,
  217. .filter_frame = filter_frame,
  218. },
  219. { NULL }
  220. };
  221. static const AVFilterPad avfilter_vf_delogo_outputs[] = {
  222. {
  223. .name = "default",
  224. .type = AVMEDIA_TYPE_VIDEO,
  225. },
  226. { NULL }
  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. .priv_class = &delogo_class,
  233. .init = init,
  234. .query_formats = query_formats,
  235. .inputs = avfilter_vf_delogo_inputs,
  236. .outputs = avfilter_vf_delogo_outputs,
  237. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  238. };