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.

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