• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 = (topleft[src_linesize*(y-logo_y  -yclipt)]   +
00090                       topleft[src_linesize*(y-logo_y-1-yclipt)]   +
00091                       topleft[src_linesize*(y-logo_y+1-yclipt)])  * (logo_w-(x-logo_x))/logo_w
00092                    + (topright[src_linesize*(y-logo_y-yclipt)]    +
00093                       topright[src_linesize*(y-logo_y-1-yclipt)]  +
00094                       topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
00095                    + (topleft[x-logo_x-xclipl]                    +
00096                       topleft[x-logo_x-1-xclipl]                  +
00097                       topleft[x-logo_x+1-xclipl])                 * (logo_h-(y-logo_y))/logo_h
00098                    + (botleft[x-logo_x-xclipl]                    +
00099                       botleft[x-logo_x-1-xclipl]                  +
00100                       botleft[x-logo_x+1-xclipl])                 * (y-logo_y)/logo_h;
00101             interp /= 6;
00102 
00103             if (y >= logo_y+band && y < logo_y+logo_h-band &&
00104                 x >= logo_x+band && x < logo_x+logo_w-band) {
00105                 *xdst = interp;
00106             } else {
00107                 dist = 0;
00108                 if      (x < logo_x+band)
00109                     dist = FFMAX(dist, logo_x-x+band);
00110                 else if (x >= logo_x+logo_w-band)
00111                     dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
00112 
00113                 if      (y < logo_y+band)
00114                     dist = FFMAX(dist, logo_y-y+band);
00115                 else if (y >= logo_y+logo_h-band)
00116                     dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
00117 
00118                 *xdst = (*xsrc*dist + interp*(band-dist))/band;
00119                 if (show && (dist == band-1))
00120                     *xdst = 0;
00121             }
00122         }
00123 
00124         dst += dst_linesize;
00125         src += src_linesize;
00126     }
00127 }
00128 
00129 typedef struct {
00130     const AVClass *class;
00131     int x, y, w, h, band, show;
00132 }  DelogoContext;
00133 
00134 #define OFFSET(x) offsetof(DelogoContext, x)
00135 
00136 static const AVOption delogo_options[]= {
00137     {"x",    "set logo x position",       OFFSET(x),    FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
00138     {"y",    "set logo y position",       OFFSET(y),    FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
00139     {"w",    "set logo width",            OFFSET(w),    FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
00140     {"h",    "set logo height",           OFFSET(h),    FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
00141     {"band", "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, { 4}, -1, INT_MAX },
00142     {"t",    "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, { 4}, -1, INT_MAX },
00143     {"show", "show delogo area",          OFFSET(show), FF_OPT_TYPE_INT, { 0},  0, 1       },
00144     {NULL},
00145 };
00146 
00147 static const char *delogo_get_name(void *ctx)
00148 {
00149     return "delogo";
00150 }
00151 
00152 static const AVClass delogo_class = {
00153     .class_name = "DelogoContext",
00154     .item_name  = delogo_get_name,
00155     .option     = delogo_options,
00156 };
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     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00168     return 0;
00169 }
00170 
00171 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
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         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00187         return ret;
00188     }
00189 
00190 #define CHECK_UNSET_OPT(opt)                                            \
00191     if (delogo->opt == -1) {                                            \
00192         av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
00193         return AVERROR(EINVAL);                                         \
00194     }
00195     CHECK_UNSET_OPT(x);
00196     CHECK_UNSET_OPT(y);
00197     CHECK_UNSET_OPT(w);
00198     CHECK_UNSET_OPT(h);
00199 
00200     if (delogo->show)
00201         delogo->band = 4;
00202 
00203     av_log(ctx, AV_LOG_DEBUG, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
00204            delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
00205 
00206     delogo->w += delogo->band*2;
00207     delogo->h += delogo->band*2;
00208     delogo->x -= delogo->band;
00209     delogo->y -= delogo->band;
00210 
00211     return 0;
00212 }
00213 
00214 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00215 {
00216     AVFilterLink *outlink = inlink->dst->outputs[0];
00217     AVFilterBufferRef *outpicref;
00218 
00219     if (inpicref->perms & AV_PERM_PRESERVE) {
00220         outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
00221                                               outlink->w, outlink->h);
00222         avfilter_copy_buffer_ref_props(outpicref, inpicref);
00223         outpicref->video->w = outlink->w;
00224         outpicref->video->h = outlink->h;
00225     } else
00226         outpicref = inpicref;
00227 
00228     outlink->out_buf = outpicref;
00229     avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
00230 }
00231 
00232 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
00233 
00234 static void end_frame(AVFilterLink *inlink)
00235 {
00236     DelogoContext *delogo = inlink->dst->priv;
00237     AVFilterLink *outlink = inlink->dst->outputs[0];
00238     AVFilterBufferRef *inpicref  = inlink ->cur_buf;
00239     AVFilterBufferRef *outpicref = outlink->out_buf;
00240     int direct = inpicref == outpicref;
00241     int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
00242     int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
00243     int plane;
00244 
00245     for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
00246         int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
00247         int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
00248 
00249         apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
00250                      inpicref ->data[plane], inpicref ->linesize[plane],
00251                      inlink->w>>hsub, inlink->h>>vsub,
00252                      delogo->x>>hsub, delogo->y>>vsub,
00253                      delogo->w>>hsub, delogo->h>>vsub,
00254                      delogo->band>>FFMIN(hsub, vsub),
00255                      delogo->show, direct);
00256     }
00257 
00258     avfilter_draw_slice(outlink, 0, inlink->h, 1);
00259     avfilter_end_frame(outlink);
00260     avfilter_unref_buffer(inpicref);
00261     if (!direct)
00262         avfilter_unref_buffer(outpicref);
00263 }
00264 
00265 AVFilter avfilter_vf_delogo = {
00266     .name          = "delogo",
00267     .description   = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
00268     .priv_size     = sizeof(DelogoContext),
00269     .init          = init,
00270     .query_formats = query_formats,
00271 
00272     .inputs    = (AVFilterPad[]) {{ .name             = "default",
00273                                     .type             = AVMEDIA_TYPE_VIDEO,
00274                                     .get_video_buffer = avfilter_null_get_video_buffer,
00275                                     .start_frame      = start_frame,
00276                                     .draw_slice       = null_draw_slice,
00277                                     .end_frame        = end_frame,
00278                                     .min_perms        = AV_PERM_WRITE | AV_PERM_READ,
00279                                     .rej_perms        = AV_PERM_PRESERVE },
00280                                   { .name = NULL}},
00281     .outputs   = (AVFilterPad[]) {{ .name             = "default",
00282                                     .type             = AVMEDIA_TYPE_VIDEO, },
00283                                   { .name = NULL}},
00284 };
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1