FFmpeg
Data Structures | Macros | Functions | Variables
vf_removelogo.c File Reference
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
#include "video.h"
#include "bbox.h"
#include "lavfutils.h"
#include "lswsutils.h"

Go to the source code of this file.

Data Structures

struct  RemovelogoContext
 This code implements a filter to remove annoying TV logos and other annoying images placed onto a video stream. More...
 

Macros

#define OFFSET(x)   offsetof(RemovelogoContext, x)
 
#define FLAGS   AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
#define apply_mask_fudge_factor(x)   (((x) >> 2) + (x))
 Choose a slightly larger mask size to improve performance. More...
 
#define SHOW_LOGO_INFO(mask_type)
 

Functions

 AVFILTER_DEFINE_CLASS (removelogo)
 
static void convert_mask_to_strength_mask (uint8_t *data, int linesize, int w, int h, int min_val, int *max_mask_size)
 Pre-process an image to give distance information. More...
 
static int load_mask (uint8_t **mask, int *w, int *h, const char *filename, void *log_ctx)
 
static void generate_half_size_image (const uint8_t *src_data, int src_linesize, uint8_t *dst_data, int dst_linesize, int src_w, int src_h, int *max_mask_size)
 Generate a scaled down image with half width, height, and intensity. More...
 
static av_cold int init (AVFilterContext *ctx)
 
static int config_props_input (AVFilterLink *inlink)
 
static unsigned int blur_pixel (int ***mask, const uint8_t *mask_data, int mask_linesize, uint8_t *image_data, int image_linesize, int w, int h, int x, int y)
 Blur image. More...
 
static void blur_image (int ***mask, const uint8_t *src_data, int src_linesize, uint8_t *dst_data, int dst_linesize, const uint8_t *mask_data, int mask_linesize, int w, int h, int direct, FFBoundingBox *bbox)
 Blur image plane using a mask. More...
 
static int filter_frame (AVFilterLink *inlink, AVFrame *inpicref)
 
static av_cold void uninit (AVFilterContext *ctx)
 

Variables

static const AVOption removelogo_options []
 
static const AVFilterPad removelogo_inputs []
 
static const AVFilterPad removelogo_outputs []
 
const AVFilter ff_vf_removelogo
 

Detailed Description

Advanced blur-based logo removing filter

This filter loads an image mask file showing where a logo is and uses a blur transform to remove the logo.

Based on the libmpcodecs remove-logo filter by Robert Edele.

Definition in file vf_removelogo.c.

Macro Definition Documentation

◆ OFFSET

#define OFFSET (   x)    offsetof(RemovelogoContext, x)

Definition at line 97 of file vf_removelogo.c.

◆ FLAGS

Definition at line 98 of file vf_removelogo.c.

◆ apply_mask_fudge_factor

#define apply_mask_fudge_factor (   x)    (((x) >> 2) + (x))

Choose a slightly larger mask size to improve performance.

This function maps the absolute minimum mask size needed to the mask size we'll actually use. f(x) = x (the smallest that will work) will produce the sharpest results, but will be quite jittery. f(x) = 1.25x (what I'm using) is a good tradeoff in my opinion. This will calculate only at init-time, so you can put a long expression here without effecting performance.

Definition at line 117 of file vf_removelogo.c.

◆ SHOW_LOGO_INFO

#define SHOW_LOGO_INFO (   mask_type)
Value:
av_log(ctx, AV_LOG_VERBOSE, #mask_type " x1:%d x2:%d y1:%d y2:%d max_mask_size:%d\n", \
s->mask_type##_mask_bbox.x1, s->mask_type##_mask_bbox.x2, \
s->mask_type##_mask_bbox.y1, s->mask_type##_mask_bbox.y2, \
mask_type##_max_mask_size);

Function Documentation

◆ AVFILTER_DEFINE_CLASS()

AVFILTER_DEFINE_CLASS ( removelogo  )

◆ convert_mask_to_strength_mask()

static void convert_mask_to_strength_mask ( uint8_t *  data,
int  linesize,
int  w,
int  h,
int  min_val,
int max_mask_size 
)
static

Pre-process an image to give distance information.

This function takes a bitmap image and converts it in place into a distance image. A distance image is zero for pixels outside of the logo and is the Manhattan distance (|dx| + |dy|) from the logo edge for pixels inside of the logo. This will overestimate the distance, but that is safe, and is far easier to implement than a proper pythagorean distance since I'm using a modified erosion algorithm to compute the distances.

Parameters
maskimage which will be converted from a greyscale image into a distance image.

Definition at line 133 of file vf_removelogo.c.

Referenced by generate_half_size_image(), and init().

◆ load_mask()

static int load_mask ( uint8_t **  mask,
int w,
int h,
const char *  filename,
void *  log_ctx 
)
static

Definition at line 206 of file vf_removelogo.c.

Referenced by init().

◆ generate_half_size_image()

static void generate_half_size_image ( const uint8_t *  src_data,
int  src_linesize,
uint8_t *  dst_data,
int  dst_linesize,
int  src_w,
int  src_h,
int max_mask_size 
)
static

Generate a scaled down image with half width, height, and intensity.

This function not only scales down an image, but halves the value in each pixel too. The purpose of this is to produce a chroma filter image out of a luma filter image. The pixel values store the distance to the edge of the logo and halving the dimensions halves the distance. This function rounds up, because a downwards rounding error could cause the filter to fail, but an upwards rounding error will only cause a minor amount of excess blur in the chroma planes.

Definition at line 247 of file vf_removelogo.c.

Referenced by init().

◆ init()

static av_cold int init ( AVFilterContext ctx)
static

Definition at line 273 of file vf_removelogo.c.

◆ config_props_input()

static int config_props_input ( AVFilterLink inlink)
static

Definition at line 350 of file vf_removelogo.c.

◆ blur_pixel()

static unsigned int blur_pixel ( int ***  mask,
const uint8_t *  mask_data,
int  mask_linesize,
uint8_t *  image_data,
int  image_linesize,
int  w,
int  h,
int  x,
int  y 
)
static

Blur image.

It takes a pixel that is inside the mask and blurs it. It does so by finding the average of all the pixels within the mask and outside of the mask.

Parameters
mask_datathe mask plane to use for averaging
image_datathe image plane to blur
wwidth of the image
hheight of the image
xx-coordinate of the pixel to blur
yy-coordinate of the pixel to blur

Definition at line 379 of file vf_removelogo.c.

Referenced by blur_image().

◆ blur_image()

static void blur_image ( int ***  mask,
const uint8_t *  src_data,
int  src_linesize,
uint8_t *  dst_data,
int  dst_linesize,
const uint8_t *  mask_data,
int  mask_linesize,
int  w,
int  h,
int  direct,
FFBoundingBox bbox 
)
static

Blur image plane using a mask.

Parameters
sourceThe image to have it's logo removed.
destinationWhere the output image will be stored.
source_strideHow far apart (in memory) two consecutive lines are.
destinationSame as source_stride, but for the destination image.
widthWidth of the image. This is the same for source and destination.
heightHeight of the image. This is the same for source and destination.
is_image_directIf the image is direct, then source and destination are the same and we can save a lot of time by not copying pixels that haven't changed.
filterThe image that stores the distance to the edge of the logo for each pixel.
logo_start_xsmallest x-coordinate that contains at least 1 logo pixel.
logo_start_ysmallest y-coordinate that contains at least 1 logo pixel.
logo_end_xlargest x-coordinate that contains at least 1 logo pixel.
logo_end_ylargest y-coordinate that contains at least 1 logo pixel.

This function processes an entire plane. Pixels outside of the logo are copied to the output without change, and pixels inside the logo have the de-blurring function applied.

Definition at line 452 of file vf_removelogo.c.

Referenced by filter_frame().

◆ filter_frame()

static int filter_frame ( AVFilterLink inlink,
AVFrame inpicref 
)
static

Definition at line 486 of file vf_removelogo.c.

◆ uninit()

static av_cold void uninit ( AVFilterContext ctx)
static

Definition at line 527 of file vf_removelogo.c.

Variable Documentation

◆ removelogo_options

const AVOption removelogo_options[]
static
Initial value:
= {
{ "filename", "set bitmap filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
{ "f", "set bitmap filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
{ NULL }
}

Definition at line 99 of file vf_removelogo.c.

◆ removelogo_inputs

const AVFilterPad removelogo_inputs[]
static
Initial value:
= {
{
.name = "default",
.config_props = config_props_input,
.filter_frame = filter_frame,
},
}

Definition at line 549 of file vf_removelogo.c.

◆ removelogo_outputs

const AVFilterPad removelogo_outputs[]
static
Initial value:
= {
{
.name = "default",
},
}

Definition at line 558 of file vf_removelogo.c.

◆ ff_vf_removelogo

const AVFilter ff_vf_removelogo
Initial value:
= {
.name = "removelogo",
.description = NULL_IF_CONFIG_SMALL("Remove a TV logo based on a mask image."),
.priv_size = sizeof(RemovelogoContext),
.init = init,
.priv_class = &removelogo_class,
}

Definition at line 565 of file vf_removelogo.c.

removelogo_inputs
static const AVFilterPad removelogo_inputs[]
Definition: vf_removelogo.c:549
OFFSET
#define OFFSET(x)
Definition: vf_removelogo.c:97
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_removelogo.c:486
config_props_input
static int config_props_input(AVFilterLink *inlink)
Definition: vf_removelogo.c:350
s
#define s(width, name)
Definition: cbs_vp9.c:256
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
NULL
#define NULL
Definition: coverity.c:32
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:152
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: internal.h:180
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_removelogo.c:527
mask_type
mask_type
Definition: iff.c:40
RemovelogoContext
This code implements a filter to remove annoying TV logos and other annoying images placed onto a vid...
Definition: vf_removelogo.c:82
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_removelogo.c:273
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
removelogo_outputs
static const AVFilterPad removelogo_outputs[]
Definition: vf_removelogo.c:558
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
FLAGS
#define FLAGS
Definition: vf_removelogo.c:98
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229