FFmpeg  1.2.12
vf_removelogo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 Robert Edele <yartrebo@earthlink.net>
3  * Copyright (c) 2012 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
72 #include "libavutil/imgutils.h"
73 #include "avfilter.h"
74 #include "formats.h"
75 #include "internal.h"
76 #include "video.h"
77 #include "bbox.h"
78 #include "lavfutils.h"
79 #include "lswsutils.h"
80 
81 typedef struct {
82  /* Stores our collection of masks. The first is for an array of
83  the second for the y axis, and the third for the x axis. */
84  int ***mask;
86  int mask_w, mask_h;
87 
93 
104 #define apply_mask_fudge_factor(x) (((x) >> 2) + x)
105 
120 static void convert_mask_to_strength_mask(uint8_t *data, int linesize,
121  int w, int h, int min_val,
122  int *max_mask_size)
123 {
124  int x, y;
125 
126  /* How many times we've gone through the loop. Used in the
127  in-place erosion algorithm and to get us max_mask_size later on. */
128  int current_pass = 0;
129 
130  /* set all non-zero values to 1 */
131  for (y = 0; y < h; y++)
132  for (x = 0; x < w; x++)
133  data[y*linesize + x] = data[y*linesize + x] > min_val;
134 
135  /* For each pass, if a pixel is itself the same value as the
136  current pass, and its four neighbors are too, then it is
137  incremented. If no pixels are incremented by the end of the
138  pass, then we go again. Edge pixels are counted as always
139  excluded (this should be true anyway for any sane mask, but if
140  it isn't this will ensure that we eventually exit). */
141  while (1) {
142  /* If this doesn't get set by the end of this pass, then we're done. */
143  int has_anything_changed = 0;
144  uint8_t *current_pixel0 = data, *current_pixel;
145  current_pass++;
146 
147  for (y = 1; y < h-1; y++) {
148  current_pixel = current_pixel0;
149  for (x = 1; x < w-1; x++) {
150  /* Apply the in-place erosion transform. It is based
151  on the following two premises:
152  1 - Any pixel that fails 1 erosion will fail all
153  future erosions.
154 
155  2 - Only pixels having survived all erosions up to
156  the present will be >= to current_pass.
157  It doesn't matter if it survived the current pass,
158  failed it, or hasn't been tested yet. By using >=
159  instead of ==, we allow the algorithm to work in
160  place. */
161  if ( *current_pixel >= current_pass &&
162  *(current_pixel + 1) >= current_pass &&
163  *(current_pixel - 1) >= current_pass &&
164  *(current_pixel + w) >= current_pass &&
165  *(current_pixel - w) >= current_pass) {
166  /* Increment the value since it still has not been
167  * eroded, as evidenced by the if statement that
168  * just evaluated to true. */
169  (*current_pixel)++;
170  has_anything_changed = 1;
171  }
172  current_pixel++;
173  }
174  current_pixel0 += linesize;
175  }
176  if (!has_anything_changed)
177  break;
178  }
179 
180  /* Apply the fudge factor, which will increase the size of the
181  * mask a little to reduce jitter at the cost of more blur. */
182  for (y = 1; y < h - 1; y++)
183  for (x = 1; x < w - 1; x++)
184  data[(y * linesize) + x] = apply_mask_fudge_factor(data[(y * linesize) + x]);
185 
186  /* As a side-effect, we now know the maximum mask size, which
187  * we'll use to generate our masks. */
188  /* Apply the fudge factor to this number too, since we must ensure
189  * that enough masks are generated. */
190  *max_mask_size = apply_mask_fudge_factor(current_pass + 1);
191 }
192 
194 {
195  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
197  return 0;
198 }
199 
200 static int load_mask(uint8_t **mask, int *w, int *h,
201  const char *filename, void *log_ctx)
202 {
203  int ret;
204  enum AVPixelFormat pix_fmt;
205  uint8_t *src_data[4], *gray_data[4];
206  int src_linesize[4], gray_linesize[4];
207 
208  /* load image from file */
209  if ((ret = ff_load_image(src_data, src_linesize, w, h, &pix_fmt, filename, log_ctx)) < 0)
210  return ret;
211 
212  /* convert the image to GRAY8 */
213  if ((ret = ff_scale_image(gray_data, gray_linesize, *w, *h, AV_PIX_FMT_GRAY8,
214  src_data, src_linesize, *w, *h, pix_fmt,
215  log_ctx)) < 0)
216  goto end;
217 
218  /* copy mask to a newly allocated array */
219  *mask = av_malloc(*w * *h);
220  if (!*mask)
221  ret = AVERROR(ENOMEM);
222  av_image_copy_plane(*mask, *w, gray_data[0], gray_linesize[0], *w, *h);
223 
224 end:
225  av_free(src_data[0]);
226  av_free(gray_data[0]);
227  return ret;
228 }
229 
241 static void generate_half_size_image(const uint8_t *src_data, int src_linesize,
242  uint8_t *dst_data, int dst_linesize,
243  int src_w, int src_h,
244  int *max_mask_size)
245 {
246  int x, y;
247 
248  /* Copy over the image data, using the average of 4 pixels for to
249  * calculate each downsampled pixel. */
250  for (y = 0; y < src_h/2; y++) {
251  for (x = 0; x < src_w/2; x++) {
252  /* Set the pixel if there exists a non-zero value in the
253  * source pixels, else clear it. */
254  dst_data[(y * dst_linesize) + x] =
255  src_data[((y << 1) * src_linesize) + (x << 1)] ||
256  src_data[((y << 1) * src_linesize) + (x << 1) + 1] ||
257  src_data[(((y << 1) + 1) * src_linesize) + (x << 1)] ||
258  src_data[(((y << 1) + 1) * src_linesize) + (x << 1) + 1];
259  dst_data[(y * dst_linesize) + x] = FFMIN(1, dst_data[(y * dst_linesize) + x]);
260  }
261  }
262 
263  convert_mask_to_strength_mask(dst_data, dst_linesize,
264  src_w/2, src_h/2, 0, max_mask_size);
265 }
266 
267 static av_cold int init(AVFilterContext *ctx, const char *args)
268 {
269  RemovelogoContext *removelogo = ctx->priv;
270  int ***mask;
271  int ret = 0;
272  int a, b, c, w, h;
273  int full_max_mask_size, half_max_mask_size;
274 
275  if (!args) {
276  av_log(ctx, AV_LOG_ERROR, "An image file must be specified as argument\n");
277  return AVERROR(EINVAL);
278  }
279 
280  /* Load our mask image. */
281  if ((ret = load_mask(&removelogo->full_mask_data, &w, &h, args, ctx)) < 0)
282  return ret;
283  removelogo->mask_w = w;
284  removelogo->mask_h = h;
285 
286  convert_mask_to_strength_mask(removelogo->full_mask_data, w, w, h,
287  16, &full_max_mask_size);
288 
289  /* Create the scaled down mask image for the chroma planes. */
290  if (!(removelogo->half_mask_data = av_mallocz(w/2 * h/2)))
291  return AVERROR(ENOMEM);
293  removelogo->half_mask_data, w/2,
294  w, h, &half_max_mask_size);
295 
296  removelogo->max_mask_size = FFMAX(full_max_mask_size, half_max_mask_size);
297 
298  /* Create a circular mask for each size up to max_mask_size. When
299  the filter is applied, the mask size is determined on a pixel
300  by pixel basis, with pixels nearer the edge of the logo getting
301  smaller mask sizes. */
302  mask = (int ***)av_malloc(sizeof(int **) * (removelogo->max_mask_size + 1));
303  if (!mask)
304  return AVERROR(ENOMEM);
305 
306  for (a = 0; a <= removelogo->max_mask_size; a++) {
307  mask[a] = (int **)av_malloc(sizeof(int *) * ((a * 2) + 1));
308  if (!mask[a])
309  return AVERROR(ENOMEM);
310  for (b = -a; b <= a; b++) {
311  mask[a][b + a] = (int *)av_malloc(sizeof(int) * ((a * 2) + 1));
312  if (!mask[a][b + a])
313  return AVERROR(ENOMEM);
314  for (c = -a; c <= a; c++) {
315  if ((b * b) + (c * c) <= (a * a)) /* Circular 0/1 mask. */
316  mask[a][b + a][c + a] = 1;
317  else
318  mask[a][b + a][c + a] = 0;
319  }
320  }
321  }
322  removelogo->mask = mask;
323 
324  /* Calculate our bounding rectangles, which determine in what
325  * region the logo resides for faster processing. */
326  ff_calculate_bounding_box(&removelogo->full_mask_bbox, removelogo->full_mask_data, w, w, h, 0);
327  ff_calculate_bounding_box(&removelogo->half_mask_bbox, removelogo->half_mask_data, w/2, w/2, h/2, 0);
328 
329 #define SHOW_LOGO_INFO(mask_type) \
330  av_log(ctx, AV_LOG_VERBOSE, #mask_type " x1:%d x2:%d y1:%d y2:%d max_mask_size:%d\n", \
331  removelogo->mask_type##_mask_bbox.x1, removelogo->mask_type##_mask_bbox.x2, \
332  removelogo->mask_type##_mask_bbox.y1, removelogo->mask_type##_mask_bbox.y2, \
333  mask_type##_max_mask_size);
334  SHOW_LOGO_INFO(full);
335  SHOW_LOGO_INFO(half);
336 
337  return 0;
338 }
339 
340 static int config_props_input(AVFilterLink *inlink)
341 {
342  AVFilterContext *ctx = inlink->dst;
343  RemovelogoContext *removelogo = ctx->priv;
344 
345  if (inlink->w != removelogo->mask_w || inlink->h != removelogo->mask_h) {
346  av_log(ctx, AV_LOG_INFO,
347  "Mask image size %dx%d does not match with the input video size %dx%d\n",
348  removelogo->mask_w, removelogo->mask_h, inlink->w, inlink->h);
349  return AVERROR(EINVAL);
350  }
351 
352  return 0;
353 }
354 
369 static unsigned int blur_pixel(int ***mask,
370  const uint8_t *mask_data, int mask_linesize,
371  uint8_t *image_data, int image_linesize,
372  int w, int h, int x, int y)
373 {
374  /* Mask size tells how large a circle to use. The radius is about
375  * (slightly larger than) mask size. */
376  int mask_size;
377  int start_posx, start_posy, end_posx, end_posy;
378  int i, j;
379  unsigned int accumulator = 0, divisor = 0;
380  /* What pixel we are reading out of the circular blur mask. */
381  const uint8_t *image_read_position;
382  /* What pixel we are reading out of the filter image. */
383  const uint8_t *mask_read_position;
384 
385  /* Prepare our bounding rectangle and clip it if need be. */
386  mask_size = mask_data[y * mask_linesize + x];
387  start_posx = FFMAX(0, x - mask_size);
388  start_posy = FFMAX(0, y - mask_size);
389  end_posx = FFMIN(w - 1, x + mask_size);
390  end_posy = FFMIN(h - 1, y + mask_size);
391 
392  image_read_position = image_data + image_linesize * start_posy + start_posx;
393  mask_read_position = mask_data + mask_linesize * start_posy + start_posx;
394 
395  for (j = start_posy; j <= end_posy; j++) {
396  for (i = start_posx; i <= end_posx; i++) {
397  /* Check if this pixel is in the mask or not. Only use the
398  * pixel if it is not. */
399  if (!(*mask_read_position) && mask[mask_size][i - start_posx][j - start_posy]) {
400  accumulator += *image_read_position;
401  divisor++;
402  }
403 
404  image_read_position++;
405  mask_read_position++;
406  }
407 
408  image_read_position += (image_linesize - ((end_posx + 1) - start_posx));
409  mask_read_position += (mask_linesize - ((end_posx + 1) - start_posx));
410  }
411 
412  /* If divisor is 0, it means that not a single pixel is outside of
413  the logo, so we have no data. Else we need to normalise the
414  data using the divisor. */
415  return divisor == 0 ? 255:
416  (accumulator + (divisor / 2)) / divisor; /* divide, taking into account average rounding error */
417 }
418 
442 static void blur_image(int ***mask,
443  const uint8_t *src_data, int src_linesize,
444  uint8_t *dst_data, int dst_linesize,
445  const uint8_t *mask_data, int mask_linesize,
446  int w, int h, int direct,
447  FFBoundingBox *bbox)
448 {
449  int x, y;
450  uint8_t *dst_line;
451  const uint8_t *src_line;
452 
453  if (!direct)
454  av_image_copy_plane(dst_data, dst_linesize, src_data, src_linesize, w, h);
455 
456  for (y = bbox->y1; y <= bbox->y2; y++) {
457  src_line = src_data + src_linesize * y;
458  dst_line = dst_data + dst_linesize * y;
459 
460  for (x = bbox->x1; x <= bbox->x2; x++) {
461  if (mask_data[y * mask_linesize + x]) {
462  /* Only process if we are in the mask. */
463  dst_line[x] = blur_pixel(mask,
464  mask_data, mask_linesize,
465  dst_data, dst_linesize,
466  w, h, x, y);
467  } else {
468  /* Else just copy the data. */
469  if (!direct)
470  dst_line[x] = src_line[x];
471  }
472  }
473  }
474 }
475 
476 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
477 {
478  RemovelogoContext *removelogo = inlink->dst->priv;
479  AVFilterLink *outlink = inlink->dst->outputs[0];
480  AVFilterBufferRef *outpicref;
481  int direct = 0;
482 
483  if (inpicref->perms & AV_PERM_WRITE) {
484  direct = 1;
485  outpicref = inpicref;
486  } else {
487  outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
488  if (!outpicref) {
489  avfilter_unref_bufferp(&inpicref);
490  return AVERROR(ENOMEM);
491  }
492  avfilter_copy_buffer_ref_props(outpicref, inpicref);
493  }
494 
495  blur_image(removelogo->mask,
496  inpicref ->data[0], inpicref ->linesize[0],
497  outpicref->data[0], outpicref->linesize[0],
498  removelogo->full_mask_data, inlink->w,
499  inlink->w, inlink->h, direct, &removelogo->full_mask_bbox);
500  blur_image(removelogo->mask,
501  inpicref ->data[1], inpicref ->linesize[1],
502  outpicref->data[1], outpicref->linesize[1],
503  removelogo->half_mask_data, inlink->w/2,
504  inlink->w/2, inlink->h/2, direct, &removelogo->half_mask_bbox);
505  blur_image(removelogo->mask,
506  inpicref ->data[2], inpicref ->linesize[2],
507  outpicref->data[2], outpicref->linesize[2],
508  removelogo->half_mask_data, inlink->w/2,
509  inlink->w/2, inlink->h/2, direct, &removelogo->half_mask_bbox);
510 
511  if (!direct)
512  avfilter_unref_bufferp(&inpicref);
513 
514  return ff_filter_frame(outlink, outpicref);
515 }
516 
517 static void uninit(AVFilterContext *ctx)
518 {
519  RemovelogoContext *removelogo = ctx->priv;
520  int a, b;
521 
522  av_freep(&removelogo->full_mask_data);
523  av_freep(&removelogo->half_mask_data);
524 
525  if (removelogo->mask) {
526  /* Loop through each mask. */
527  for (a = 0; a <= removelogo->max_mask_size; a++) {
528  /* Loop through each scanline in a mask. */
529  for (b = -a; b <= a; b++) {
530  av_free(removelogo->mask[a][b + a]); /* Free a scanline. */
531  }
532  av_free(removelogo->mask[a]);
533  }
534  /* Free the array of pointers pointing to the masks. */
535  av_freep(&removelogo->mask);
536  }
537 }
538 
539 static const AVFilterPad removelogo_inputs[] = {
540  {
541  .name = "default",
542  .type = AVMEDIA_TYPE_VIDEO,
543  .get_video_buffer = ff_null_get_video_buffer,
544  .config_props = config_props_input,
545  .filter_frame = filter_frame,
546  .min_perms = AV_PERM_READ,
547  },
548  { NULL }
549 };
550 
551 static const AVFilterPad removelogo_outputs[] = {
552  {
553  .name = "default",
554  .type = AVMEDIA_TYPE_VIDEO,
555  },
556  { NULL }
557 };
558 
560  .name = "removelogo",
561  .description = NULL_IF_CONFIG_SMALL("Remove a TV logo based on a mask image."),
562  .priv_size = sizeof(RemovelogoContext),
563  .init = init,
564  .uninit = uninit,
566  .inputs = removelogo_inputs,
567  .outputs = removelogo_outputs,
568 };