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