00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/common.h"
00029 #include "libavutil/imgutils.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/pixdesc.h"
00032 #include "avfilter.h"
00033 #include "formats.h"
00034 #include "internal.h"
00035 #include "video.h"
00036
00055 static void apply_delogo(uint8_t *dst, int dst_linesize,
00056 uint8_t *src, int src_linesize,
00057 int w, int h,
00058 int logo_x, int logo_y, int logo_w, int logo_h,
00059 int band, int show, int direct)
00060 {
00061 int x, y;
00062 int interp, dist;
00063 uint8_t *xdst, *xsrc;
00064
00065 uint8_t *topleft, *botleft, *topright;
00066 int xclipl, xclipr, yclipt, yclipb;
00067 int logo_x1, logo_x2, logo_y1, logo_y2;
00068
00069 xclipl = FFMAX(-logo_x, 0);
00070 xclipr = FFMAX(logo_x+logo_w-w, 0);
00071 yclipt = FFMAX(-logo_y, 0);
00072 yclipb = FFMAX(logo_y+logo_h-h, 0);
00073
00074 logo_x1 = logo_x + xclipl;
00075 logo_x2 = logo_x + logo_w - xclipr;
00076 logo_y1 = logo_y + yclipt;
00077 logo_y2 = logo_y + logo_h - yclipb;
00078
00079 topleft = src+logo_y1 * src_linesize+logo_x1;
00080 topright = src+logo_y1 * src_linesize+logo_x2-1;
00081 botleft = src+(logo_y2-1) * src_linesize+logo_x1;
00082
00083 dst += (logo_y1+1)*dst_linesize;
00084 src += (logo_y1+1)*src_linesize;
00085
00086 if (!direct)
00087 av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
00088
00089 for (y = logo_y1+1; y < logo_y2-1; y++) {
00090 for (x = logo_x1+1,
00091 xdst = dst+logo_x1+1,
00092 xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
00093 interp =
00094 (topleft[src_linesize*(y-logo_y -yclipt)] +
00095 topleft[src_linesize*(y-logo_y-1-yclipt)] +
00096 topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
00097 +
00098 (topright[src_linesize*(y-logo_y-yclipt)] +
00099 topright[src_linesize*(y-logo_y-1-yclipt)] +
00100 topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
00101 +
00102 (topleft[x-logo_x-xclipl] +
00103 topleft[x-logo_x-1-xclipl] +
00104 topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
00105 +
00106 (botleft[x-logo_x-xclipl] +
00107 botleft[x-logo_x-1-xclipl] +
00108 botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
00109 interp /= 6;
00110
00111 if (y >= logo_y+band && y < logo_y+logo_h-band &&
00112 x >= logo_x+band && x < logo_x+logo_w-band) {
00113 *xdst = interp;
00114 } else {
00115 dist = 0;
00116 if (x < logo_x+band)
00117 dist = FFMAX(dist, logo_x-x+band);
00118 else if (x >= logo_x+logo_w-band)
00119 dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
00120
00121 if (y < logo_y+band)
00122 dist = FFMAX(dist, logo_y-y+band);
00123 else if (y >= logo_y+logo_h-band)
00124 dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
00125
00126 *xdst = (*xsrc*dist + interp*(band-dist))/band;
00127 if (show && (dist == band-1))
00128 *xdst = 0;
00129 }
00130 }
00131
00132 dst += dst_linesize;
00133 src += src_linesize;
00134 }
00135 }
00136
00137 typedef struct {
00138 const AVClass *class;
00139 int x, y, w, h, band, show;
00140 } DelogoContext;
00141
00142 #define OFFSET(x) offsetof(DelogoContext, x)
00143 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
00144
00145 static const AVOption delogo_options[]= {
00146 {"x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
00147 {"y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
00148 {"w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
00149 {"h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
00150 {"band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.i64 = 4}, -1, INT_MAX, FLAGS},
00151 {"t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.i64 = 4}, -1, INT_MAX, FLAGS},
00152 {"show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS},
00153 {NULL},
00154 };
00155
00156 AVFILTER_DEFINE_CLASS(delogo);
00157
00158 static int query_formats(AVFilterContext *ctx)
00159 {
00160 enum PixelFormat pix_fmts[] = {
00161 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
00162 PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUV440P,
00163 PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
00164 PIX_FMT_NONE
00165 };
00166
00167 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00168 return 0;
00169 }
00170
00171 static av_cold int init(AVFilterContext *ctx, const char *args)
00172 {
00173 DelogoContext *delogo = ctx->priv;
00174 int ret = 0;
00175
00176 delogo->class = &delogo_class;
00177 av_opt_set_defaults(delogo);
00178
00179 if (args)
00180 ret = sscanf(args, "%d:%d:%d:%d:%d",
00181 &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
00182 if (ret == 5) {
00183 if (delogo->band < 0)
00184 delogo->show = 1;
00185 } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0)
00186 return ret;
00187
00188 #define CHECK_UNSET_OPT(opt) \
00189 if (delogo->opt == -1) { \
00190 av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
00191 return AVERROR(EINVAL); \
00192 }
00193 CHECK_UNSET_OPT(x);
00194 CHECK_UNSET_OPT(y);
00195 CHECK_UNSET_OPT(w);
00196 CHECK_UNSET_OPT(h);
00197
00198 if (delogo->show)
00199 delogo->band = 4;
00200
00201 av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
00202 delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
00203
00204 delogo->w += delogo->band*2;
00205 delogo->h += delogo->band*2;
00206 delogo->x -= delogo->band;
00207 delogo->y -= delogo->band;
00208
00209 return 0;
00210 }
00211
00212 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00213 {
00214 return 0;
00215 }
00216
00217 static int end_frame(AVFilterLink *inlink)
00218 {
00219 DelogoContext *delogo = inlink->dst->priv;
00220 AVFilterLink *outlink = inlink->dst->outputs[0];
00221 AVFilterBufferRef *inpicref = inlink ->cur_buf;
00222 AVFilterBufferRef *outpicref = outlink->out_buf;
00223 int direct = inpicref->buf == outpicref->buf;
00224 int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
00225 int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
00226 int plane;
00227 int ret;
00228
00229 for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
00230 int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
00231 int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
00232
00233 apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
00234 inpicref ->data[plane], inpicref ->linesize[plane],
00235 inlink->w>>hsub, inlink->h>>vsub,
00236 delogo->x>>hsub, delogo->y>>vsub,
00237 delogo->w>>hsub, delogo->h>>vsub,
00238 delogo->band>>FFMIN(hsub, vsub),
00239 delogo->show, direct);
00240 }
00241
00242 if ((ret = ff_draw_slice(outlink, 0, inlink->h, 1)) < 0 ||
00243 (ret = ff_end_frame(outlink)) < 0)
00244 return ret;
00245 return 0;
00246 }
00247
00248 AVFilter avfilter_vf_delogo = {
00249 .name = "delogo",
00250 .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
00251 .priv_size = sizeof(DelogoContext),
00252 .init = init,
00253 .query_formats = query_formats,
00254
00255 .inputs = (const AVFilterPad[]) {{ .name = "default",
00256 .type = AVMEDIA_TYPE_VIDEO,
00257 .get_video_buffer = ff_null_get_video_buffer,
00258 .start_frame = ff_inplace_start_frame,
00259 .draw_slice = null_draw_slice,
00260 .end_frame = end_frame,
00261 .min_perms = AV_PERM_WRITE | AV_PERM_READ },
00262 { .name = NULL}},
00263 .outputs = (const AVFilterPad[]) {{ .name = "default",
00264 .type = AVMEDIA_TYPE_VIDEO, },
00265 { .name = NULL}},
00266 };