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.

413 lines
14KB

  1. /*
  2. * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2013, 2015 Jean Delvare <jdelvare@suse.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. /**
  23. * @file
  24. * A very simple tv station logo remover
  25. * Originally imported from MPlayer libmpcodecs/vf_delogo.c,
  26. * the algorithm was later improved.
  27. */
  28. #include "libavutil/common.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/eval.h"
  33. #include "avfilter.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. static const char * const var_names[] = {
  38. "x",
  39. "y",
  40. "w",
  41. "h",
  42. "n", ///< number of frame
  43. "t", ///< timestamp expressed in seconds
  44. NULL
  45. };
  46. enum var_name {
  47. VAR_X,
  48. VAR_Y,
  49. VAR_W,
  50. VAR_H,
  51. VAR_N,
  52. VAR_T,
  53. VAR_VARS_NB
  54. };
  55. static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
  56. {
  57. int ret;
  58. AVExpr *old = NULL;
  59. if (*pexpr)
  60. old = *pexpr;
  61. ret = av_expr_parse(pexpr, expr, var_names, NULL, NULL, NULL, NULL, 0, log_ctx);
  62. if (ret < 0) {
  63. av_log(log_ctx, AV_LOG_ERROR,
  64. "Error when parsing the expression '%s' for %s\n",
  65. expr, option);
  66. *pexpr = old;
  67. return ret;
  68. }
  69. av_expr_free(old);
  70. return 0;
  71. }
  72. /**
  73. * Apply a simple delogo algorithm to the image in src and put the
  74. * result in dst.
  75. *
  76. * The algorithm is only applied to the region specified by the logo
  77. * parameters.
  78. *
  79. * @param w width of the input image
  80. * @param h height of the input image
  81. * @param logo_x x coordinate of the top left corner of the logo region
  82. * @param logo_y y coordinate of the top left corner of the logo region
  83. * @param logo_w width of the logo
  84. * @param logo_h height of the logo
  85. * @param band the size of the band around the processed area
  86. * @param show show a rectangle around the processed area, useful for
  87. * parameters tweaking
  88. * @param direct if non-zero perform in-place processing
  89. */
  90. static void apply_delogo(uint8_t *dst, int dst_linesize,
  91. uint8_t *src, int src_linesize,
  92. int w, int h, AVRational sar,
  93. int logo_x, int logo_y, int logo_w, int logo_h,
  94. unsigned int band, int show, int direct)
  95. {
  96. int x, y;
  97. uint64_t interp, weightl, weightr, weightt, weightb, weight;
  98. uint8_t *xdst, *xsrc;
  99. uint8_t *topleft, *botleft, *topright;
  100. unsigned int left_sample, right_sample;
  101. int xclipl, xclipr, yclipt, yclipb;
  102. int logo_x1, logo_x2, logo_y1, logo_y2;
  103. xclipl = FFMAX(-logo_x, 0);
  104. xclipr = FFMAX(logo_x+logo_w-w, 0);
  105. yclipt = FFMAX(-logo_y, 0);
  106. yclipb = FFMAX(logo_y+logo_h-h, 0);
  107. logo_x1 = logo_x + xclipl;
  108. logo_x2 = logo_x + logo_w - xclipr - 1;
  109. logo_y1 = logo_y + yclipt;
  110. logo_y2 = logo_y + logo_h - yclipb - 1;
  111. topleft = src+logo_y1 * src_linesize+logo_x1;
  112. topright = src+logo_y1 * src_linesize+logo_x2;
  113. botleft = src+logo_y2 * src_linesize+logo_x1;
  114. if (!direct)
  115. av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
  116. dst += (logo_y1 + 1) * dst_linesize;
  117. src += (logo_y1 + 1) * src_linesize;
  118. for (y = logo_y1+1; y < logo_y2; y++) {
  119. left_sample = topleft[src_linesize*(y-logo_y1)] +
  120. topleft[src_linesize*(y-logo_y1-1)] +
  121. topleft[src_linesize*(y-logo_y1+1)];
  122. right_sample = topright[src_linesize*(y-logo_y1)] +
  123. topright[src_linesize*(y-logo_y1-1)] +
  124. topright[src_linesize*(y-logo_y1+1)];
  125. for (x = logo_x1+1,
  126. xdst = dst+logo_x1+1,
  127. xsrc = src+logo_x1+1; x < logo_x2; x++, xdst++, xsrc++) {
  128. if (show && (y == logo_y1+1 || y == logo_y2-1 ||
  129. x == logo_x1+1 || x == logo_x2-1)) {
  130. *xdst = 0;
  131. continue;
  132. }
  133. /* Weighted interpolation based on relative distances, taking SAR into account */
  134. weightl = (uint64_t) (logo_x2-x) * (y-logo_y1) * (logo_y2-y) * sar.den;
  135. weightr = (uint64_t)(x-logo_x1) * (y-logo_y1) * (logo_y2-y) * sar.den;
  136. weightt = (uint64_t)(x-logo_x1) * (logo_x2-x) * (logo_y2-y) * sar.num;
  137. weightb = (uint64_t)(x-logo_x1) * (logo_x2-x) * (y-logo_y1) * sar.num;
  138. interp =
  139. left_sample * weightl
  140. +
  141. right_sample * weightr
  142. +
  143. (topleft[x-logo_x1] +
  144. topleft[x-logo_x1-1] +
  145. topleft[x-logo_x1+1]) * weightt
  146. +
  147. (botleft[x-logo_x1] +
  148. botleft[x-logo_x1-1] +
  149. botleft[x-logo_x1+1]) * weightb;
  150. weight = (weightl + weightr + weightt + weightb) * 3U;
  151. interp = (interp + (weight >> 1)) / weight;
  152. if (y >= logo_y+band && y < logo_y+logo_h-band &&
  153. x >= logo_x+band && x < logo_x+logo_w-band) {
  154. *xdst = interp;
  155. } else {
  156. unsigned dist = 0;
  157. if (x < logo_x+band)
  158. dist = FFMAX(dist, logo_x-x+band);
  159. else if (x >= logo_x+logo_w-band)
  160. dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
  161. if (y < logo_y+band)
  162. dist = FFMAX(dist, logo_y-y+band);
  163. else if (y >= logo_y+logo_h-band)
  164. dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
  165. *xdst = (*xsrc*dist + interp*(band-dist))/band;
  166. }
  167. }
  168. dst += dst_linesize;
  169. src += src_linesize;
  170. }
  171. }
  172. typedef struct DelogoContext {
  173. const AVClass *class;
  174. int x, y, w, h, band, show;
  175. char *x_expr, *y_expr, *w_expr, *h_expr;
  176. AVExpr *x_pexpr, *y_pexpr, *w_pexpr, *h_pexpr;
  177. double var_values[VAR_VARS_NB];
  178. } DelogoContext;
  179. #define OFFSET(x) offsetof(DelogoContext, x)
  180. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  181. static const AVOption delogo_options[]= {
  182. { "x", "set logo x position", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS },
  183. { "y", "set logo y position", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS },
  184. { "w", "set logo width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS },
  185. { "h", "set logo height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS },
  186. { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  187. { NULL }
  188. };
  189. AVFILTER_DEFINE_CLASS(delogo);
  190. static av_cold void uninit(AVFilterContext *ctx)
  191. {
  192. DelogoContext *s = ctx->priv;
  193. av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
  194. av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
  195. av_expr_free(s->w_pexpr); s->w_pexpr = NULL;
  196. av_expr_free(s->h_pexpr); s->h_pexpr = NULL;
  197. }
  198. static int query_formats(AVFilterContext *ctx)
  199. {
  200. static const enum AVPixelFormat pix_fmts[] = {
  201. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  202. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  203. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  204. AV_PIX_FMT_NONE
  205. };
  206. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  207. if (!fmts_list)
  208. return AVERROR(ENOMEM);
  209. return ff_set_common_formats(ctx, fmts_list);
  210. }
  211. static av_cold int init(AVFilterContext *ctx)
  212. {
  213. DelogoContext *s = ctx->priv;
  214. int ret = 0;
  215. if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
  216. (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0 ||
  217. (ret = set_expr(&s->w_pexpr, s->w_expr, "w", ctx)) < 0 ||
  218. (ret = set_expr(&s->h_pexpr, s->h_expr, "h", ctx)) < 0 )
  219. return ret;
  220. s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
  221. s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
  222. s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
  223. s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
  224. #define CHECK_UNSET_OPT(opt) \
  225. if (s->opt == -1) { \
  226. av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  227. return AVERROR(EINVAL); \
  228. }
  229. CHECK_UNSET_OPT(x);
  230. CHECK_UNSET_OPT(y);
  231. CHECK_UNSET_OPT(w);
  232. CHECK_UNSET_OPT(h);
  233. s->band = 1;
  234. av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  235. s->x, s->y, s->w, s->h, s->band, s->show);
  236. s->w += s->band*2;
  237. s->h += s->band*2;
  238. s->x -= s->band;
  239. s->y -= s->band;
  240. return 0;
  241. }
  242. static int config_input(AVFilterLink *inlink)
  243. {
  244. DelogoContext *s = inlink->dst->priv;
  245. /* Check whether the logo area fits in the frame */
  246. if (s->x + (s->band - 1) < 0 || s->x + s->w - (s->band*2 - 2) > inlink->w ||
  247. s->y + (s->band - 1) < 0 || s->y + s->h - (s->band*2 - 2) > inlink->h) {
  248. av_log(s, AV_LOG_ERROR, "Logo area is outside of the frame.\n");
  249. return AVERROR(EINVAL);
  250. }
  251. return 0;
  252. }
  253. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  254. {
  255. DelogoContext *s = inlink->dst->priv;
  256. AVFilterLink *outlink = inlink->dst->outputs[0];
  257. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  258. AVFrame *out;
  259. int hsub0 = desc->log2_chroma_w;
  260. int vsub0 = desc->log2_chroma_h;
  261. int direct = 0;
  262. int plane;
  263. AVRational sar;
  264. int ret;
  265. s->var_values[VAR_N] = inlink->frame_count_out;
  266. s->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
  267. s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
  268. s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
  269. s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
  270. s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
  271. if (s->x + (s->band - 1) <= 0 || s->x + s->w - (s->band*2 - 2) > inlink->w ||
  272. s->y + (s->band - 1) <= 0 || s->y + s->h - (s->band*2 - 2) > inlink->h) {
  273. av_log(s, AV_LOG_WARNING, "Logo area is outside of the frame,"
  274. " auto set the area inside of the frame\n");
  275. }
  276. if (s->x + (s->band - 1) <= 0)
  277. s->x = 1 + s->band;
  278. if (s->y + (s->band - 1) <= 0)
  279. s->y = 1 + s->band;
  280. if (s->x + s->w - (s->band*2 - 2) > inlink->w)
  281. s->w = inlink->w - s->x - (s->band*2 - 2);
  282. if (s->y + s->h - (s->band*2 - 2) > inlink->h)
  283. s->h = inlink->h - s->y - (s->band*2 - 2);
  284. ret = config_input(inlink);
  285. if (ret < 0) {
  286. av_frame_free(&in);
  287. return ret;
  288. }
  289. s->w += s->band*2;
  290. s->h += s->band*2;
  291. s->x -= s->band;
  292. s->y -= s->band;
  293. if (av_frame_is_writable(in)) {
  294. direct = 1;
  295. out = in;
  296. } else {
  297. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  298. if (!out) {
  299. av_frame_free(&in);
  300. return AVERROR(ENOMEM);
  301. }
  302. av_frame_copy_props(out, in);
  303. }
  304. sar = in->sample_aspect_ratio;
  305. /* Assume square pixels if SAR is unknown */
  306. if (!sar.num)
  307. sar.num = sar.den = 1;
  308. for (plane = 0; plane < desc->nb_components; plane++) {
  309. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  310. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  311. apply_delogo(out->data[plane], out->linesize[plane],
  312. in ->data[plane], in ->linesize[plane],
  313. AV_CEIL_RSHIFT(inlink->w, hsub),
  314. AV_CEIL_RSHIFT(inlink->h, vsub),
  315. sar, s->x>>hsub, s->y>>vsub,
  316. /* Up and left borders were rounded down, inject lost bits
  317. * into width and height to avoid error accumulation */
  318. AV_CEIL_RSHIFT(s->w + (s->x & ((1<<hsub)-1)), hsub),
  319. AV_CEIL_RSHIFT(s->h + (s->y & ((1<<vsub)-1)), vsub),
  320. s->band>>FFMIN(hsub, vsub),
  321. s->show, direct);
  322. }
  323. if (!direct)
  324. av_frame_free(&in);
  325. return ff_filter_frame(outlink, out);
  326. }
  327. static const AVFilterPad avfilter_vf_delogo_inputs[] = {
  328. {
  329. .name = "default",
  330. .type = AVMEDIA_TYPE_VIDEO,
  331. .filter_frame = filter_frame,
  332. .config_props = config_input,
  333. },
  334. { NULL }
  335. };
  336. static const AVFilterPad avfilter_vf_delogo_outputs[] = {
  337. {
  338. .name = "default",
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. },
  341. { NULL }
  342. };
  343. AVFilter ff_vf_delogo = {
  344. .name = "delogo",
  345. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  346. .priv_size = sizeof(DelogoContext),
  347. .priv_class = &delogo_class,
  348. .init = init,
  349. .uninit = uninit,
  350. .query_formats = query_formats,
  351. .inputs = avfilter_vf_delogo_inputs,
  352. .outputs = avfilter_vf_delogo_outputs,
  353. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  354. };