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.

414 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. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
  56. static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
  57. {
  58. int ret;
  59. AVExpr *old = NULL;
  60. if (*pexpr)
  61. old = *pexpr;
  62. ret = av_expr_parse(pexpr, expr, var_names, NULL, NULL, NULL, NULL, 0, log_ctx);
  63. if (ret < 0) {
  64. av_log(log_ctx, AV_LOG_ERROR,
  65. "Error when parsing the expression '%s' for %s\n",
  66. expr, option);
  67. *pexpr = old;
  68. return ret;
  69. }
  70. av_expr_free(old);
  71. return 0;
  72. }
  73. /**
  74. * Apply a simple delogo algorithm to the image in src and put the
  75. * result in dst.
  76. *
  77. * The algorithm is only applied to the region specified by the logo
  78. * parameters.
  79. *
  80. * @param w width of the input image
  81. * @param h height of the input image
  82. * @param logo_x x coordinate of the top left corner of the logo region
  83. * @param logo_y y coordinate of the top left corner of the logo region
  84. * @param logo_w width of the logo
  85. * @param logo_h height of the logo
  86. * @param band the size of the band around the processed area
  87. * @param show show a rectangle around the processed area, useful for
  88. * parameters tweaking
  89. * @param direct if non-zero perform in-place processing
  90. */
  91. static void apply_delogo(uint8_t *dst, int dst_linesize,
  92. uint8_t *src, int src_linesize,
  93. int w, int h, AVRational sar,
  94. int logo_x, int logo_y, int logo_w, int logo_h,
  95. unsigned int band, int show, int direct)
  96. {
  97. int x, y;
  98. uint64_t interp, weightl, weightr, weightt, weightb, weight;
  99. uint8_t *xdst, *xsrc;
  100. uint8_t *topleft, *botleft, *topright;
  101. unsigned int left_sample, right_sample;
  102. int xclipl, xclipr, yclipt, yclipb;
  103. int logo_x1, logo_x2, logo_y1, logo_y2;
  104. xclipl = FFMAX(-logo_x, 0);
  105. xclipr = FFMAX(logo_x+logo_w-w, 0);
  106. yclipt = FFMAX(-logo_y, 0);
  107. yclipb = FFMAX(logo_y+logo_h-h, 0);
  108. logo_x1 = logo_x + xclipl;
  109. logo_x2 = logo_x + logo_w - xclipr - 1;
  110. logo_y1 = logo_y + yclipt;
  111. logo_y2 = logo_y + logo_h - yclipb - 1;
  112. topleft = src+logo_y1 * src_linesize+logo_x1;
  113. topright = src+logo_y1 * src_linesize+logo_x2;
  114. botleft = src+logo_y2 * src_linesize+logo_x1;
  115. if (!direct)
  116. av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
  117. dst += (logo_y1 + 1) * dst_linesize;
  118. src += (logo_y1 + 1) * src_linesize;
  119. for (y = logo_y1+1; y < logo_y2; y++) {
  120. left_sample = topleft[src_linesize*(y-logo_y1)] +
  121. topleft[src_linesize*(y-logo_y1-1)] +
  122. topleft[src_linesize*(y-logo_y1+1)];
  123. right_sample = topright[src_linesize*(y-logo_y1)] +
  124. topright[src_linesize*(y-logo_y1-1)] +
  125. topright[src_linesize*(y-logo_y1+1)];
  126. for (x = logo_x1+1,
  127. xdst = dst+logo_x1+1,
  128. xsrc = src+logo_x1+1; x < logo_x2; x++, xdst++, xsrc++) {
  129. if (show && (y == logo_y1+1 || y == logo_y2-1 ||
  130. x == logo_x1+1 || x == logo_x2-1)) {
  131. *xdst = 0;
  132. continue;
  133. }
  134. /* Weighted interpolation based on relative distances, taking SAR into account */
  135. weightl = (uint64_t) (logo_x2-x) * (y-logo_y1) * (logo_y2-y) * sar.den;
  136. weightr = (uint64_t)(x-logo_x1) * (y-logo_y1) * (logo_y2-y) * sar.den;
  137. weightt = (uint64_t)(x-logo_x1) * (logo_x2-x) * (logo_y2-y) * sar.num;
  138. weightb = (uint64_t)(x-logo_x1) * (logo_x2-x) * (y-logo_y1) * sar.num;
  139. interp =
  140. left_sample * weightl
  141. +
  142. right_sample * weightr
  143. +
  144. (topleft[x-logo_x1] +
  145. topleft[x-logo_x1-1] +
  146. topleft[x-logo_x1+1]) * weightt
  147. +
  148. (botleft[x-logo_x1] +
  149. botleft[x-logo_x1-1] +
  150. botleft[x-logo_x1+1]) * weightb;
  151. weight = (weightl + weightr + weightt + weightb) * 3U;
  152. interp = (interp + (weight >> 1)) / weight;
  153. if (y >= logo_y+band && y < logo_y+logo_h-band &&
  154. x >= logo_x+band && x < logo_x+logo_w-band) {
  155. *xdst = interp;
  156. } else {
  157. unsigned dist = 0;
  158. if (x < logo_x+band)
  159. dist = FFMAX(dist, logo_x-x+band);
  160. else if (x >= logo_x+logo_w-band)
  161. dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
  162. if (y < logo_y+band)
  163. dist = FFMAX(dist, logo_y-y+band);
  164. else if (y >= logo_y+logo_h-band)
  165. dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
  166. *xdst = (*xsrc*dist + interp*(band-dist))/band;
  167. }
  168. }
  169. dst += dst_linesize;
  170. src += src_linesize;
  171. }
  172. }
  173. typedef struct DelogoContext {
  174. const AVClass *class;
  175. int x, y, w, h, band, show;
  176. char *x_expr, *y_expr, *w_expr, *h_expr;
  177. AVExpr *x_pexpr, *y_pexpr, *w_pexpr, *h_pexpr;
  178. double var_values[VAR_VARS_NB];
  179. } DelogoContext;
  180. #define OFFSET(x) offsetof(DelogoContext, x)
  181. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  182. static const AVOption delogo_options[]= {
  183. { "x", "set logo x position", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS },
  184. { "y", "set logo y position", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS },
  185. { "w", "set logo width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS },
  186. { "h", "set logo height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS },
  187. { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  188. { NULL }
  189. };
  190. AVFILTER_DEFINE_CLASS(delogo);
  191. static av_cold void uninit(AVFilterContext *ctx)
  192. {
  193. DelogoContext *s = ctx->priv;
  194. av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
  195. av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
  196. av_expr_free(s->w_pexpr); s->w_pexpr = NULL;
  197. av_expr_free(s->h_pexpr); s->h_pexpr = NULL;
  198. }
  199. static int query_formats(AVFilterContext *ctx)
  200. {
  201. static const enum AVPixelFormat pix_fmts[] = {
  202. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  203. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  204. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  205. AV_PIX_FMT_NONE
  206. };
  207. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  208. if (!fmts_list)
  209. return AVERROR(ENOMEM);
  210. return ff_set_common_formats(ctx, fmts_list);
  211. }
  212. static av_cold int init(AVFilterContext *ctx)
  213. {
  214. DelogoContext *s = ctx->priv;
  215. int ret = 0;
  216. if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
  217. (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0 ||
  218. (ret = set_expr(&s->w_pexpr, s->w_expr, "w", ctx)) < 0 ||
  219. (ret = set_expr(&s->h_pexpr, s->h_expr, "h", ctx)) < 0 )
  220. return ret;
  221. s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
  222. s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
  223. s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
  224. s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
  225. #define CHECK_UNSET_OPT(opt) \
  226. if (s->opt == -1) { \
  227. av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  228. return AVERROR(EINVAL); \
  229. }
  230. CHECK_UNSET_OPT(x);
  231. CHECK_UNSET_OPT(y);
  232. CHECK_UNSET_OPT(w);
  233. CHECK_UNSET_OPT(h);
  234. s->band = 1;
  235. av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  236. s->x, s->y, s->w, s->h, s->band, s->show);
  237. s->w += s->band*2;
  238. s->h += s->band*2;
  239. s->x -= s->band;
  240. s->y -= s->band;
  241. return 0;
  242. }
  243. static int config_input(AVFilterLink *inlink)
  244. {
  245. DelogoContext *s = inlink->dst->priv;
  246. /* Check whether the logo area fits in the frame */
  247. if (s->x + (s->band - 1) < 0 || s->x + s->w - (s->band*2 - 2) > inlink->w ||
  248. s->y + (s->band - 1) < 0 || s->y + s->h - (s->band*2 - 2) > inlink->h) {
  249. av_log(s, AV_LOG_ERROR, "Logo area is outside of the frame.\n");
  250. return AVERROR(EINVAL);
  251. }
  252. return 0;
  253. }
  254. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  255. {
  256. DelogoContext *s = inlink->dst->priv;
  257. AVFilterLink *outlink = inlink->dst->outputs[0];
  258. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  259. AVFrame *out;
  260. int hsub0 = desc->log2_chroma_w;
  261. int vsub0 = desc->log2_chroma_h;
  262. int direct = 0;
  263. int plane;
  264. AVRational sar;
  265. int ret;
  266. s->var_values[VAR_N] = inlink->frame_count_out;
  267. s->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
  268. s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
  269. s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
  270. s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
  271. s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
  272. if (s->x + (s->band - 1) <= 0 || s->x + s->w - (s->band*2 - 2) > inlink->w ||
  273. s->y + (s->band - 1) <= 0 || s->y + s->h - (s->band*2 - 2) > inlink->h) {
  274. av_log(s, AV_LOG_WARNING, "Logo area is outside of the frame,"
  275. " auto set the area inside of the frame\n");
  276. }
  277. if (s->x + (s->band - 1) <= 0)
  278. s->x = 1 + s->band;
  279. if (s->y + (s->band - 1) <= 0)
  280. s->y = 1 + s->band;
  281. if (s->x + s->w - (s->band*2 - 2) > inlink->w)
  282. s->w = inlink->w - s->x - (s->band*2 - 2);
  283. if (s->y + s->h - (s->band*2 - 2) > inlink->h)
  284. s->h = inlink->h - s->y - (s->band*2 - 2);
  285. ret = config_input(inlink);
  286. if (ret < 0) {
  287. av_frame_free(&in);
  288. return ret;
  289. }
  290. s->w += s->band*2;
  291. s->h += s->band*2;
  292. s->x -= s->band;
  293. s->y -= s->band;
  294. if (av_frame_is_writable(in)) {
  295. direct = 1;
  296. out = in;
  297. } else {
  298. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  299. if (!out) {
  300. av_frame_free(&in);
  301. return AVERROR(ENOMEM);
  302. }
  303. av_frame_copy_props(out, in);
  304. }
  305. sar = in->sample_aspect_ratio;
  306. /* Assume square pixels if SAR is unknown */
  307. if (!sar.num)
  308. sar.num = sar.den = 1;
  309. for (plane = 0; plane < desc->nb_components; plane++) {
  310. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  311. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  312. apply_delogo(out->data[plane], out->linesize[plane],
  313. in ->data[plane], in ->linesize[plane],
  314. AV_CEIL_RSHIFT(inlink->w, hsub),
  315. AV_CEIL_RSHIFT(inlink->h, vsub),
  316. sar, s->x>>hsub, s->y>>vsub,
  317. /* Up and left borders were rounded down, inject lost bits
  318. * into width and height to avoid error accumulation */
  319. AV_CEIL_RSHIFT(s->w + (s->x & ((1<<hsub)-1)), hsub),
  320. AV_CEIL_RSHIFT(s->h + (s->y & ((1<<vsub)-1)), vsub),
  321. s->band>>FFMIN(hsub, vsub),
  322. s->show, direct);
  323. }
  324. if (!direct)
  325. av_frame_free(&in);
  326. return ff_filter_frame(outlink, out);
  327. }
  328. static const AVFilterPad avfilter_vf_delogo_inputs[] = {
  329. {
  330. .name = "default",
  331. .type = AVMEDIA_TYPE_VIDEO,
  332. .filter_frame = filter_frame,
  333. .config_props = config_input,
  334. },
  335. { NULL }
  336. };
  337. static const AVFilterPad avfilter_vf_delogo_outputs[] = {
  338. {
  339. .name = "default",
  340. .type = AVMEDIA_TYPE_VIDEO,
  341. },
  342. { NULL }
  343. };
  344. AVFilter ff_vf_delogo = {
  345. .name = "delogo",
  346. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  347. .priv_size = sizeof(DelogoContext),
  348. .priv_class = &delogo_class,
  349. .init = init,
  350. .uninit = uninit,
  351. .query_formats = query_formats,
  352. .inputs = avfilter_vf_delogo_inputs,
  353. .outputs = avfilter_vf_delogo_outputs,
  354. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  355. };