yading@10: /* yading@10: * Copyright (c) 2005 Robert Edele yading@10: * Copyright (c) 2012 Stefano Sabatini yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * Advanced blur-based logo removing filter yading@10: * yading@10: * This filter loads an image mask file showing where a logo is and yading@10: * uses a blur transform to remove the logo. yading@10: * yading@10: * Based on the libmpcodecs remove-logo filter by Robert Edele. yading@10: */ yading@10: yading@10: /** yading@10: * This code implements a filter to remove annoying TV logos and other annoying yading@10: * images placed onto a video stream. It works by filling in the pixels that yading@10: * comprise the logo with neighboring pixels. The transform is very loosely yading@10: * based on a gaussian blur, but it is different enough to merit its own yading@10: * paragraph later on. It is a major improvement on the old delogo filter as it yading@10: * both uses a better blurring algorithm and uses a bitmap to use an arbitrary yading@10: * and generally much tighter fitting shape than a rectangle. yading@10: * yading@10: * The logo removal algorithm has two key points. The first is that it yading@10: * distinguishes between pixels in the logo and those not in the logo by using yading@10: * the passed-in bitmap. Pixels not in the logo are copied over directly without yading@10: * being modified and they also serve as source pixels for the logo yading@10: * fill-in. Pixels inside the logo have the mask applied. yading@10: * yading@10: * At init-time the bitmap is reprocessed internally, and the distance to the yading@10: * nearest edge of the logo (Manhattan distance), along with a little extra to yading@10: * remove rough edges, is stored in each pixel. This is done using an in-place yading@10: * erosion algorithm, and incrementing each pixel that survives any given yading@10: * erosion. Once every pixel is eroded, the maximum value is recorded, and a yading@10: * set of masks from size 0 to this size are generaged. The masks are circular yading@10: * binary masks, where each pixel within a radius N (where N is the size of the yading@10: * mask) is a 1, and all other pixels are a 0. Although a gaussian mask would be yading@10: * more mathematically accurate, a binary mask works better in practice because yading@10: * we generally do not use the central pixels in the mask (because they are in yading@10: * the logo region), and thus a gaussian mask will cause too little blur and yading@10: * thus a very unstable image. yading@10: * yading@10: * The mask is applied in a special way. Namely, only pixels in the mask that yading@10: * line up to pixels outside the logo are used. The dynamic mask size means that yading@10: * the mask is just big enough so that the edges touch pixels outside the logo, yading@10: * so the blurring is kept to a minimum and at least the first boundary yading@10: * condition is met (that the image function itself is continuous), even if the yading@10: * second boundary condition (that the derivative of the image function is yading@10: * continuous) is not met. A masking algorithm that does preserve the second yading@10: * boundary coundition (perhaps something based on a highly-modified bi-cubic yading@10: * algorithm) should offer even better results on paper, but the noise in a yading@10: * typical TV signal should make anything based on derivatives hopelessly noisy. yading@10: */ yading@10: yading@10: #include "libavutil/imgutils.h" yading@10: #include "libavutil/opt.h" yading@10: #include "avfilter.h" yading@10: #include "formats.h" yading@10: #include "internal.h" yading@10: #include "video.h" yading@10: #include "bbox.h" yading@10: #include "lavfutils.h" yading@10: #include "lswsutils.h" yading@10: yading@10: typedef struct { yading@10: const AVClass *class; yading@10: char *filename; yading@10: /* Stores our collection of masks. The first is for an array of yading@10: the second for the y axis, and the third for the x axis. */ yading@10: int ***mask; yading@10: int max_mask_size; yading@10: int mask_w, mask_h; yading@10: yading@10: uint8_t *full_mask_data; yading@10: FFBoundingBox full_mask_bbox; yading@10: uint8_t *half_mask_data; yading@10: FFBoundingBox half_mask_bbox; yading@10: } RemovelogoContext; yading@10: yading@10: #define OFFSET(x) offsetof(RemovelogoContext, x) yading@10: #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM yading@10: static const AVOption removelogo_options[] = { yading@10: { "filename", "set bitmap filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, yading@10: { "f", "set bitmap filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, yading@10: { NULL } yading@10: }; yading@10: yading@10: AVFILTER_DEFINE_CLASS(removelogo); yading@10: yading@10: /** yading@10: * Choose a slightly larger mask size to improve performance. yading@10: * yading@10: * This function maps the absolute minimum mask size needed to the yading@10: * mask size we'll actually use. f(x) = x (the smallest that will yading@10: * work) will produce the sharpest results, but will be quite yading@10: * jittery. f(x) = 1.25x (what I'm using) is a good tradeoff in my yading@10: * opinion. This will calculate only at init-time, so you can put a yading@10: * long expression here without effecting performance. yading@10: */ yading@10: #define apply_mask_fudge_factor(x) (((x) >> 2) + x) yading@10: yading@10: /** yading@10: * Pre-process an image to give distance information. yading@10: * yading@10: * This function takes a bitmap image and converts it in place into a yading@10: * distance image. A distance image is zero for pixels outside of the yading@10: * logo and is the Manhattan distance (|dx| + |dy|) from the logo edge yading@10: * for pixels inside of the logo. This will overestimate the distance, yading@10: * but that is safe, and is far easier to implement than a proper yading@10: * pythagorean distance since I'm using a modified erosion algorithm yading@10: * to compute the distances. yading@10: * yading@10: * @param mask image which will be converted from a greyscale image yading@10: * into a distance image. yading@10: */ yading@10: static void convert_mask_to_strength_mask(uint8_t *data, int linesize, yading@10: int w, int h, int min_val, yading@10: int *max_mask_size) yading@10: { yading@10: int x, y; yading@10: yading@10: /* How many times we've gone through the loop. Used in the yading@10: in-place erosion algorithm and to get us max_mask_size later on. */ yading@10: int current_pass = 0; yading@10: yading@10: /* set all non-zero values to 1 */ yading@10: for (y = 0; y < h; y++) yading@10: for (x = 0; x < w; x++) yading@10: data[y*linesize + x] = data[y*linesize + x] > min_val; yading@10: yading@10: /* For each pass, if a pixel is itself the same value as the yading@10: current pass, and its four neighbors are too, then it is yading@10: incremented. If no pixels are incremented by the end of the yading@10: pass, then we go again. Edge pixels are counted as always yading@10: excluded (this should be true anyway for any sane mask, but if yading@10: it isn't this will ensure that we eventually exit). */ yading@10: while (1) { yading@10: /* If this doesn't get set by the end of this pass, then we're done. */ yading@10: int has_anything_changed = 0; yading@10: uint8_t *current_pixel0 = data, *current_pixel; yading@10: current_pass++; yading@10: yading@10: for (y = 1; y < h-1; y++) { yading@10: current_pixel = current_pixel0; yading@10: for (x = 1; x < w-1; x++) { yading@10: /* Apply the in-place erosion transform. It is based yading@10: on the following two premises: yading@10: 1 - Any pixel that fails 1 erosion will fail all yading@10: future erosions. yading@10: yading@10: 2 - Only pixels having survived all erosions up to yading@10: the present will be >= to current_pass. yading@10: It doesn't matter if it survived the current pass, yading@10: failed it, or hasn't been tested yet. By using >= yading@10: instead of ==, we allow the algorithm to work in yading@10: place. */ yading@10: if ( *current_pixel >= current_pass && yading@10: *(current_pixel + 1) >= current_pass && yading@10: *(current_pixel - 1) >= current_pass && yading@10: *(current_pixel + w) >= current_pass && yading@10: *(current_pixel - w) >= current_pass) { yading@10: /* Increment the value since it still has not been yading@10: * eroded, as evidenced by the if statement that yading@10: * just evaluated to true. */ yading@10: (*current_pixel)++; yading@10: has_anything_changed = 1; yading@10: } yading@10: current_pixel++; yading@10: } yading@10: current_pixel0 += linesize; yading@10: } yading@10: if (!has_anything_changed) yading@10: break; yading@10: } yading@10: yading@10: /* Apply the fudge factor, which will increase the size of the yading@10: * mask a little to reduce jitter at the cost of more blur. */ yading@10: for (y = 1; y < h - 1; y++) yading@10: for (x = 1; x < w - 1; x++) yading@10: data[(y * linesize) + x] = apply_mask_fudge_factor(data[(y * linesize) + x]); yading@10: yading@10: /* As a side-effect, we now know the maximum mask size, which yading@10: * we'll use to generate our masks. */ yading@10: /* Apply the fudge factor to this number too, since we must ensure yading@10: * that enough masks are generated. */ yading@10: *max_mask_size = apply_mask_fudge_factor(current_pass + 1); yading@10: } yading@10: yading@10: static int query_formats(AVFilterContext *ctx) yading@10: { yading@10: static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE }; yading@10: ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); yading@10: return 0; yading@10: } yading@10: yading@10: static int load_mask(uint8_t **mask, int *w, int *h, yading@10: const char *filename, void *log_ctx) yading@10: { yading@10: int ret; yading@10: enum AVPixelFormat pix_fmt; yading@10: uint8_t *src_data[4], *gray_data[4]; yading@10: int src_linesize[4], gray_linesize[4]; yading@10: yading@10: /* load image from file */ yading@10: if ((ret = ff_load_image(src_data, src_linesize, w, h, &pix_fmt, filename, log_ctx)) < 0) yading@10: return ret; yading@10: yading@10: /* convert the image to GRAY8 */ yading@10: if ((ret = ff_scale_image(gray_data, gray_linesize, *w, *h, AV_PIX_FMT_GRAY8, yading@10: src_data, src_linesize, *w, *h, pix_fmt, yading@10: log_ctx)) < 0) yading@10: goto end; yading@10: yading@10: /* copy mask to a newly allocated array */ yading@10: *mask = av_malloc(*w * *h); yading@10: if (!*mask) yading@10: ret = AVERROR(ENOMEM); yading@10: av_image_copy_plane(*mask, *w, gray_data[0], gray_linesize[0], *w, *h); yading@10: yading@10: end: yading@10: av_free(src_data[0]); yading@10: av_free(gray_data[0]); yading@10: return ret; yading@10: } yading@10: yading@10: /** yading@10: * Generate a scaled down image with half width, height, and intensity. yading@10: * yading@10: * This function not only scales down an image, but halves the value yading@10: * in each pixel too. The purpose of this is to produce a chroma yading@10: * filter image out of a luma filter image. The pixel values store the yading@10: * distance to the edge of the logo and halving the dimensions halves yading@10: * the distance. This function rounds up, because a downwards rounding yading@10: * error could cause the filter to fail, but an upwards rounding error yading@10: * will only cause a minor amount of excess blur in the chroma planes. yading@10: */ yading@10: static void generate_half_size_image(const uint8_t *src_data, int src_linesize, yading@10: uint8_t *dst_data, int dst_linesize, yading@10: int src_w, int src_h, yading@10: int *max_mask_size) yading@10: { yading@10: int x, y; yading@10: yading@10: /* Copy over the image data, using the average of 4 pixels for to yading@10: * calculate each downsampled pixel. */ yading@10: for (y = 0; y < src_h/2; y++) { yading@10: for (x = 0; x < src_w/2; x++) { yading@10: /* Set the pixel if there exists a non-zero value in the yading@10: * source pixels, else clear it. */ yading@10: dst_data[(y * dst_linesize) + x] = yading@10: src_data[((y << 1) * src_linesize) + (x << 1)] || yading@10: src_data[((y << 1) * src_linesize) + (x << 1) + 1] || yading@10: src_data[(((y << 1) + 1) * src_linesize) + (x << 1)] || yading@10: src_data[(((y << 1) + 1) * src_linesize) + (x << 1) + 1]; yading@10: dst_data[(y * dst_linesize) + x] = FFMIN(1, dst_data[(y * dst_linesize) + x]); yading@10: } yading@10: } yading@10: yading@10: convert_mask_to_strength_mask(dst_data, dst_linesize, yading@10: src_w/2, src_h/2, 0, max_mask_size); yading@10: } yading@10: yading@10: static av_cold int init(AVFilterContext *ctx) yading@10: { yading@10: RemovelogoContext *removelogo = ctx->priv; yading@10: int ***mask; yading@10: int ret = 0; yading@10: int a, b, c, w, h; yading@10: int full_max_mask_size, half_max_mask_size; yading@10: yading@10: if (!removelogo->filename) { yading@10: av_log(ctx, AV_LOG_ERROR, "The bitmap file name is mandatory\n"); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: /* Load our mask image. */ yading@10: if ((ret = load_mask(&removelogo->full_mask_data, &w, &h, removelogo->filename, ctx)) < 0) yading@10: return ret; yading@10: removelogo->mask_w = w; yading@10: removelogo->mask_h = h; yading@10: yading@10: convert_mask_to_strength_mask(removelogo->full_mask_data, w, w, h, yading@10: 16, &full_max_mask_size); yading@10: yading@10: /* Create the scaled down mask image for the chroma planes. */ yading@10: if (!(removelogo->half_mask_data = av_mallocz(w/2 * h/2))) yading@10: return AVERROR(ENOMEM); yading@10: generate_half_size_image(removelogo->full_mask_data, w, yading@10: removelogo->half_mask_data, w/2, yading@10: w, h, &half_max_mask_size); yading@10: yading@10: removelogo->max_mask_size = FFMAX(full_max_mask_size, half_max_mask_size); yading@10: yading@10: /* Create a circular mask for each size up to max_mask_size. When yading@10: the filter is applied, the mask size is determined on a pixel yading@10: by pixel basis, with pixels nearer the edge of the logo getting yading@10: smaller mask sizes. */ yading@10: mask = (int ***)av_malloc(sizeof(int **) * (removelogo->max_mask_size + 1)); yading@10: if (!mask) yading@10: return AVERROR(ENOMEM); yading@10: yading@10: for (a = 0; a <= removelogo->max_mask_size; a++) { yading@10: mask[a] = (int **)av_malloc(sizeof(int *) * ((a * 2) + 1)); yading@10: if (!mask[a]) yading@10: return AVERROR(ENOMEM); yading@10: for (b = -a; b <= a; b++) { yading@10: mask[a][b + a] = (int *)av_malloc(sizeof(int) * ((a * 2) + 1)); yading@10: if (!mask[a][b + a]) yading@10: return AVERROR(ENOMEM); yading@10: for (c = -a; c <= a; c++) { yading@10: if ((b * b) + (c * c) <= (a * a)) /* Circular 0/1 mask. */ yading@10: mask[a][b + a][c + a] = 1; yading@10: else yading@10: mask[a][b + a][c + a] = 0; yading@10: } yading@10: } yading@10: } yading@10: removelogo->mask = mask; yading@10: yading@10: /* Calculate our bounding rectangles, which determine in what yading@10: * region the logo resides for faster processing. */ yading@10: ff_calculate_bounding_box(&removelogo->full_mask_bbox, removelogo->full_mask_data, w, w, h, 0); yading@10: ff_calculate_bounding_box(&removelogo->half_mask_bbox, removelogo->half_mask_data, w/2, w/2, h/2, 0); yading@10: yading@10: #define SHOW_LOGO_INFO(mask_type) \ yading@10: av_log(ctx, AV_LOG_VERBOSE, #mask_type " x1:%d x2:%d y1:%d y2:%d max_mask_size:%d\n", \ yading@10: removelogo->mask_type##_mask_bbox.x1, removelogo->mask_type##_mask_bbox.x2, \ yading@10: removelogo->mask_type##_mask_bbox.y1, removelogo->mask_type##_mask_bbox.y2, \ yading@10: mask_type##_max_mask_size); yading@10: SHOW_LOGO_INFO(full); yading@10: SHOW_LOGO_INFO(half); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int config_props_input(AVFilterLink *inlink) yading@10: { yading@10: AVFilterContext *ctx = inlink->dst; yading@10: RemovelogoContext *removelogo = ctx->priv; yading@10: yading@10: if (inlink->w != removelogo->mask_w || inlink->h != removelogo->mask_h) { yading@10: av_log(ctx, AV_LOG_INFO, yading@10: "Mask image size %dx%d does not match with the input video size %dx%d\n", yading@10: removelogo->mask_w, removelogo->mask_h, inlink->w, inlink->h); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: /** yading@10: * Blur image. yading@10: * yading@10: * It takes a pixel that is inside the mask and blurs it. It does so yading@10: * by finding the average of all the pixels within the mask and yading@10: * outside of the mask. yading@10: * yading@10: * @param mask_data the mask plane to use for averaging yading@10: * @param image_data the image plane to blur yading@10: * @param w width of the image yading@10: * @param h height of the image yading@10: * @param x x-coordinate of the pixel to blur yading@10: * @param y y-coordinate of the pixel to blur yading@10: */ yading@10: static unsigned int blur_pixel(int ***mask, yading@10: const uint8_t *mask_data, int mask_linesize, yading@10: uint8_t *image_data, int image_linesize, yading@10: int w, int h, int x, int y) yading@10: { yading@10: /* Mask size tells how large a circle to use. The radius is about yading@10: * (slightly larger than) mask size. */ yading@10: int mask_size; yading@10: int start_posx, start_posy, end_posx, end_posy; yading@10: int i, j; yading@10: unsigned int accumulator = 0, divisor = 0; yading@10: /* What pixel we are reading out of the circular blur mask. */ yading@10: const uint8_t *image_read_position; yading@10: /* What pixel we are reading out of the filter image. */ yading@10: const uint8_t *mask_read_position; yading@10: yading@10: /* Prepare our bounding rectangle and clip it if need be. */ yading@10: mask_size = mask_data[y * mask_linesize + x]; yading@10: start_posx = FFMAX(0, x - mask_size); yading@10: start_posy = FFMAX(0, y - mask_size); yading@10: end_posx = FFMIN(w - 1, x + mask_size); yading@10: end_posy = FFMIN(h - 1, y + mask_size); yading@10: yading@10: image_read_position = image_data + image_linesize * start_posy + start_posx; yading@10: mask_read_position = mask_data + mask_linesize * start_posy + start_posx; yading@10: yading@10: for (j = start_posy; j <= end_posy; j++) { yading@10: for (i = start_posx; i <= end_posx; i++) { yading@10: /* Check if this pixel is in the mask or not. Only use the yading@10: * pixel if it is not. */ yading@10: if (!(*mask_read_position) && mask[mask_size][i - start_posx][j - start_posy]) { yading@10: accumulator += *image_read_position; yading@10: divisor++; yading@10: } yading@10: yading@10: image_read_position++; yading@10: mask_read_position++; yading@10: } yading@10: yading@10: image_read_position += (image_linesize - ((end_posx + 1) - start_posx)); yading@10: mask_read_position += (mask_linesize - ((end_posx + 1) - start_posx)); yading@10: } yading@10: yading@10: /* If divisor is 0, it means that not a single pixel is outside of yading@10: the logo, so we have no data. Else we need to normalise the yading@10: data using the divisor. */ yading@10: return divisor == 0 ? 255: yading@10: (accumulator + (divisor / 2)) / divisor; /* divide, taking into account average rounding error */ yading@10: } yading@10: yading@10: /** yading@10: * Blur image plane using a mask. yading@10: * yading@10: * @param source The image to have it's logo removed. yading@10: * @param destination Where the output image will be stored. yading@10: * @param source_stride How far apart (in memory) two consecutive lines are. yading@10: * @param destination Same as source_stride, but for the destination image. yading@10: * @param width Width of the image. This is the same for source and destination. yading@10: * @param height Height of the image. This is the same for source and destination. yading@10: * @param is_image_direct If the image is direct, then source and destination are yading@10: * the same and we can save a lot of time by not copying pixels that yading@10: * haven't changed. yading@10: * @param filter The image that stores the distance to the edge of the logo for yading@10: * each pixel. yading@10: * @param logo_start_x smallest x-coordinate that contains at least 1 logo pixel. yading@10: * @param logo_start_y smallest y-coordinate that contains at least 1 logo pixel. yading@10: * @param logo_end_x largest x-coordinate that contains at least 1 logo pixel. yading@10: * @param logo_end_y largest y-coordinate that contains at least 1 logo pixel. yading@10: * yading@10: * This function processes an entire plane. Pixels outside of the logo are copied yading@10: * to the output without change, and pixels inside the logo have the de-blurring yading@10: * function applied. yading@10: */ yading@10: static void blur_image(int ***mask, yading@10: const uint8_t *src_data, int src_linesize, yading@10: uint8_t *dst_data, int dst_linesize, yading@10: const uint8_t *mask_data, int mask_linesize, yading@10: int w, int h, int direct, yading@10: FFBoundingBox *bbox) yading@10: { yading@10: int x, y; yading@10: uint8_t *dst_line; yading@10: const uint8_t *src_line; yading@10: yading@10: if (!direct) yading@10: av_image_copy_plane(dst_data, dst_linesize, src_data, src_linesize, w, h); yading@10: yading@10: for (y = bbox->y1; y <= bbox->y2; y++) { yading@10: src_line = src_data + src_linesize * y; yading@10: dst_line = dst_data + dst_linesize * y; yading@10: yading@10: for (x = bbox->x1; x <= bbox->x2; x++) { yading@10: if (mask_data[y * mask_linesize + x]) { yading@10: /* Only process if we are in the mask. */ yading@10: dst_line[x] = blur_pixel(mask, yading@10: mask_data, mask_linesize, yading@10: dst_data, dst_linesize, yading@10: w, h, x, y); yading@10: } else { yading@10: /* Else just copy the data. */ yading@10: if (!direct) yading@10: dst_line[x] = src_line[x]; yading@10: } yading@10: } yading@10: } yading@10: } yading@10: yading@10: static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref) yading@10: { yading@10: RemovelogoContext *removelogo = inlink->dst->priv; yading@10: AVFilterLink *outlink = inlink->dst->outputs[0]; yading@10: AVFrame *outpicref; yading@10: int direct = 0; yading@10: yading@10: if (av_frame_is_writable(inpicref)) { yading@10: direct = 1; yading@10: outpicref = inpicref; yading@10: } else { yading@10: outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h); yading@10: if (!outpicref) { yading@10: av_frame_free(&inpicref); yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: av_frame_copy_props(outpicref, inpicref); yading@10: } yading@10: yading@10: blur_image(removelogo->mask, yading@10: inpicref ->data[0], inpicref ->linesize[0], yading@10: outpicref->data[0], outpicref->linesize[0], yading@10: removelogo->full_mask_data, inlink->w, yading@10: inlink->w, inlink->h, direct, &removelogo->full_mask_bbox); yading@10: blur_image(removelogo->mask, yading@10: inpicref ->data[1], inpicref ->linesize[1], yading@10: outpicref->data[1], outpicref->linesize[1], yading@10: removelogo->half_mask_data, inlink->w/2, yading@10: inlink->w/2, inlink->h/2, direct, &removelogo->half_mask_bbox); yading@10: blur_image(removelogo->mask, yading@10: inpicref ->data[2], inpicref ->linesize[2], yading@10: outpicref->data[2], outpicref->linesize[2], yading@10: removelogo->half_mask_data, inlink->w/2, yading@10: inlink->w/2, inlink->h/2, direct, &removelogo->half_mask_bbox); yading@10: yading@10: if (!direct) yading@10: av_frame_free(&inpicref); yading@10: yading@10: return ff_filter_frame(outlink, outpicref); yading@10: } yading@10: yading@10: static void uninit(AVFilterContext *ctx) yading@10: { yading@10: RemovelogoContext *removelogo = ctx->priv; yading@10: int a, b; yading@10: yading@10: av_freep(&removelogo->full_mask_data); yading@10: av_freep(&removelogo->half_mask_data); yading@10: yading@10: if (removelogo->mask) { yading@10: /* Loop through each mask. */ yading@10: for (a = 0; a <= removelogo->max_mask_size; a++) { yading@10: /* Loop through each scanline in a mask. */ yading@10: for (b = -a; b <= a; b++) { yading@10: av_free(removelogo->mask[a][b + a]); /* Free a scanline. */ yading@10: } yading@10: av_free(removelogo->mask[a]); yading@10: } yading@10: /* Free the array of pointers pointing to the masks. */ yading@10: av_freep(&removelogo->mask); yading@10: } yading@10: } yading@10: yading@10: static const AVFilterPad removelogo_inputs[] = { yading@10: { yading@10: .name = "default", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .get_video_buffer = ff_null_get_video_buffer, yading@10: .config_props = config_props_input, yading@10: .filter_frame = filter_frame, yading@10: }, yading@10: { NULL } yading@10: }; yading@10: yading@10: static const AVFilterPad removelogo_outputs[] = { yading@10: { yading@10: .name = "default", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: }, yading@10: { NULL } yading@10: }; yading@10: yading@10: AVFilter avfilter_vf_removelogo = { yading@10: .name = "removelogo", yading@10: .description = NULL_IF_CONFIG_SMALL("Remove a TV logo based on a mask image."), yading@10: .priv_size = sizeof(RemovelogoContext), yading@10: .init = init, yading@10: .uninit = uninit, yading@10: .query_formats = query_formats, yading@10: .inputs = removelogo_inputs, yading@10: .outputs = removelogo_outputs, yading@10: .priv_class = &removelogo_class, yading@10: };