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