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.

412 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 = ROUNDED_DIV(interp, 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" }, CHAR_MIN, CHAR_MAX, FLAGS },
  184. { "y", "set logo y position", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
  185. { "w", "set logo width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
  186. { "h", "set logo height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
  187. #if LIBAVFILTER_VERSION_MAJOR < 7
  188. /* Actual default value for band/t is 1, set in init */
  189. { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  190. { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  191. #endif
  192. { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, FLAGS },
  193. { NULL }
  194. };
  195. AVFILTER_DEFINE_CLASS(delogo);
  196. static void uninit(AVFilterContext *ctx)
  197. {
  198. DelogoContext *s = ctx->priv;
  199. av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
  200. av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
  201. av_expr_free(s->w_pexpr); s->w_pexpr = NULL;
  202. av_expr_free(s->h_pexpr); s->h_pexpr = NULL;
  203. }
  204. static int query_formats(AVFilterContext *ctx)
  205. {
  206. static const enum AVPixelFormat pix_fmts[] = {
  207. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  208. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  209. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  210. AV_PIX_FMT_NONE
  211. };
  212. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  213. if (!fmts_list)
  214. return AVERROR(ENOMEM);
  215. return ff_set_common_formats(ctx, fmts_list);
  216. }
  217. static av_cold int init(AVFilterContext *ctx)
  218. {
  219. DelogoContext *s = ctx->priv;
  220. int ret = 0;
  221. if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
  222. (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0 ||
  223. (ret = set_expr(&s->w_pexpr, s->w_expr, "w", ctx)) < 0 ||
  224. (ret = set_expr(&s->h_pexpr, s->h_expr, "h", ctx)) < 0 )
  225. return ret;
  226. s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
  227. s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
  228. s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
  229. s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
  230. #define CHECK_UNSET_OPT(opt) \
  231. if (s->opt == -1) { \
  232. av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
  233. return AVERROR(EINVAL); \
  234. }
  235. CHECK_UNSET_OPT(x);
  236. CHECK_UNSET_OPT(y);
  237. CHECK_UNSET_OPT(w);
  238. CHECK_UNSET_OPT(h);
  239. #if LIBAVFILTER_VERSION_MAJOR < 7
  240. if (s->band == 0) { /* Unset, use default */
  241. av_log(ctx, AV_LOG_WARNING, "Note: default band value was changed from 4 to 1.\n");
  242. s->band = 1;
  243. } else if (s->band != 1) {
  244. av_log(ctx, AV_LOG_WARNING, "Option band is deprecated.\n");
  245. }
  246. #else
  247. s->band = 1;
  248. #endif
  249. av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
  250. s->x, s->y, s->w, s->h, s->band, s->show);
  251. s->w += s->band*2;
  252. s->h += s->band*2;
  253. s->x -= s->band;
  254. s->y -= s->band;
  255. return 0;
  256. }
  257. static int config_input(AVFilterLink *inlink)
  258. {
  259. DelogoContext *s = inlink->dst->priv;
  260. /* Check whether the logo area fits in the frame */
  261. if (s->x + (s->band - 1) < 0 || s->x + s->w - (s->band*2 - 2) > inlink->w ||
  262. s->y + (s->band - 1) < 0 || s->y + s->h - (s->band*2 - 2) > inlink->h) {
  263. av_log(s, AV_LOG_ERROR, "Logo area is outside of the frame.\n");
  264. return AVERROR(EINVAL);
  265. }
  266. return 0;
  267. }
  268. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  269. {
  270. DelogoContext *s = inlink->dst->priv;
  271. AVFilterLink *outlink = inlink->dst->outputs[0];
  272. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  273. AVFrame *out;
  274. int hsub0 = desc->log2_chroma_w;
  275. int vsub0 = desc->log2_chroma_h;
  276. int direct = 0;
  277. int plane;
  278. AVRational sar;
  279. int ret;
  280. s->var_values[VAR_N] = inlink->frame_count_out;
  281. s->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
  282. s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
  283. s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
  284. s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
  285. s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
  286. ret = config_input(inlink);
  287. if (ret < 0) {
  288. av_frame_free(&in);
  289. return ret;
  290. }
  291. s->w += s->band*2;
  292. s->h += s->band*2;
  293. s->x -= s->band;
  294. s->y -= s->band;
  295. if (av_frame_is_writable(in)) {
  296. direct = 1;
  297. out = in;
  298. } else {
  299. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  300. if (!out) {
  301. av_frame_free(&in);
  302. return AVERROR(ENOMEM);
  303. }
  304. av_frame_copy_props(out, in);
  305. }
  306. sar = in->sample_aspect_ratio;
  307. /* Assume square pixels if SAR is unknown */
  308. if (!sar.num)
  309. sar.num = sar.den = 1;
  310. for (plane = 0; plane < desc->nb_components; plane++) {
  311. int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
  312. int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
  313. apply_delogo(out->data[plane], out->linesize[plane],
  314. in ->data[plane], in ->linesize[plane],
  315. AV_CEIL_RSHIFT(inlink->w, hsub),
  316. AV_CEIL_RSHIFT(inlink->h, vsub),
  317. sar, s->x>>hsub, s->y>>vsub,
  318. /* Up and left borders were rounded down, inject lost bits
  319. * into width and height to avoid error accumulation */
  320. AV_CEIL_RSHIFT(s->w + (s->x & ((1<<hsub)-1)), hsub),
  321. AV_CEIL_RSHIFT(s->h + (s->y & ((1<<vsub)-1)), vsub),
  322. s->band>>FFMIN(hsub, vsub),
  323. s->show, direct);
  324. }
  325. if (!direct)
  326. av_frame_free(&in);
  327. return ff_filter_frame(outlink, out);
  328. }
  329. static const AVFilterPad avfilter_vf_delogo_inputs[] = {
  330. {
  331. .name = "default",
  332. .type = AVMEDIA_TYPE_VIDEO,
  333. .filter_frame = filter_frame,
  334. .config_props = config_input,
  335. },
  336. { NULL }
  337. };
  338. static const AVFilterPad avfilter_vf_delogo_outputs[] = {
  339. {
  340. .name = "default",
  341. .type = AVMEDIA_TYPE_VIDEO,
  342. },
  343. { NULL }
  344. };
  345. AVFilter ff_vf_delogo = {
  346. .name = "delogo",
  347. .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
  348. .priv_size = sizeof(DelogoContext),
  349. .priv_class = &delogo_class,
  350. .init = init,
  351. .uninit = uninit,
  352. .query_formats = query_formats,
  353. .inputs = avfilter_vf_delogo_inputs,
  354. .outputs = avfilter_vf_delogo_outputs,
  355. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  356. };