00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <inttypes.h>
00027 #include <math.h>
00028
00029 #include "mp_msg.h"
00030 #include "cpudetect.h"
00031 #include "img_format.h"
00032 #include "mp_image.h"
00033 #include "vf.h"
00034 #include "libvo/fastmemcpy.h"
00035
00036
00037
00038 struct vf_priv_s {
00039 unsigned int outfmt;
00040 int xoff, yoff, lw, lh, band, show;
00041 };
00042
00043 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
00044 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
00045
00046 static void delogo(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height,
00047 int logo_x, int logo_y, int logo_w, int logo_h, int band, int show, int direct) {
00048 int y, x;
00049 int interp, dist;
00050 uint8_t *xdst, *xsrc;
00051
00052 uint8_t *topleft, *botleft, *topright;
00053 int xclipl, xclipr, yclipt, yclipb;
00054 int logo_x1, logo_x2, logo_y1, logo_y2;
00055
00056 xclipl = MAX(-logo_x, 0);
00057 xclipr = MAX(logo_x+logo_w-width, 0);
00058 yclipt = MAX(-logo_y, 0);
00059 yclipb = MAX(logo_y+logo_h-height, 0);
00060
00061 logo_x1 = logo_x + xclipl;
00062 logo_x2 = logo_x + logo_w - xclipr;
00063 logo_y1 = logo_y + yclipt;
00064 logo_y2 = logo_y + logo_h - yclipb;
00065
00066 topleft = src+logo_y1*srcStride+logo_x1;
00067 topright = src+logo_y1*srcStride+logo_x2-1;
00068 botleft = src+(logo_y2-1)*srcStride+logo_x1;
00069
00070 if (!direct) memcpy_pic(dst, src, width, height, dstStride, srcStride);
00071
00072 dst += (logo_y1+1)*dstStride;
00073 src += (logo_y1+1)*srcStride;
00074
00075 for(y = logo_y1+1; y < logo_y2-1; y++)
00076 {
00077 for (x = logo_x1+1, xdst = dst+logo_x1+1, xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
00078 interp = ((topleft[srcStride*(y-logo_y-yclipt)]
00079 + topleft[srcStride*(y-logo_y-1-yclipt)]
00080 + topleft[srcStride*(y-logo_y+1-yclipt)])*(logo_w-(x-logo_x))/logo_w
00081 + (topright[srcStride*(y-logo_y-yclipt)]
00082 + topright[srcStride*(y-logo_y-1-yclipt)]
00083 + topright[srcStride*(y-logo_y+1-yclipt)])*(x-logo_x)/logo_w
00084 + (topleft[x-logo_x-xclipl]
00085 + topleft[x-logo_x-1-xclipl]
00086 + topleft[x-logo_x+1-xclipl])*(logo_h-(y-logo_y))/logo_h
00087 + (botleft[x-logo_x-xclipl]
00088 + botleft[x-logo_x-1-xclipl]
00089 + botleft[x-logo_x+1-xclipl])*(y-logo_y)/logo_h
00090 )/6;
00091
00092
00093
00094
00095
00096 if (y >= logo_y+band && y < logo_y+logo_h-band && x >= logo_x+band && x < logo_x+logo_w-band) {
00097 *xdst = interp;
00098 } else {
00099 dist = 0;
00100 if (x < logo_x+band) dist = MAX(dist, logo_x-x+band);
00101 else if (x >= logo_x+logo_w-band) dist = MAX(dist, x-(logo_x+logo_w-1-band));
00102 if (y < logo_y+band) dist = MAX(dist, logo_y-y+band);
00103 else if (y >= logo_y+logo_h-band) dist = MAX(dist, y-(logo_y+logo_h-1-band));
00104 *xdst = (*xsrc*dist + interp*(band-dist))/band;
00105 if (show && (dist == band-1)) *xdst = 0;
00106 }
00107 }
00108
00109 dst+= dstStride;
00110 src+= srcStride;
00111 }
00112 }
00113
00114 static int config(struct vf_instance *vf,
00115 int width, int height, int d_width, int d_height,
00116 unsigned int flags, unsigned int outfmt){
00117
00118 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
00119 }
00120
00121
00122 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
00123 if(mpi->flags&MP_IMGFLAG_PRESERVE) return;
00124 if(mpi->imgfmt!=vf->priv->outfmt) return;
00125
00126 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
00127 mpi->type, mpi->flags, mpi->w, mpi->h);
00128 mpi->planes[0]=vf->dmpi->planes[0];
00129 mpi->stride[0]=vf->dmpi->stride[0];
00130 mpi->width=vf->dmpi->width;
00131 if(mpi->flags&MP_IMGFLAG_PLANAR){
00132 mpi->planes[1]=vf->dmpi->planes[1];
00133 mpi->planes[2]=vf->dmpi->planes[2];
00134 mpi->stride[1]=vf->dmpi->stride[1];
00135 mpi->stride[2]=vf->dmpi->stride[2];
00136 }
00137 mpi->flags|=MP_IMGFLAG_DIRECT;
00138 }
00139
00140 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
00141 mp_image_t *dmpi;
00142
00143 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
00144
00145 vf->dmpi=vf_get_image(vf->next,vf->priv->outfmt,
00146 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
00147 mpi->w,mpi->h);
00148 }
00149 dmpi= vf->dmpi;
00150
00151 delogo(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h,
00152 vf->priv->xoff, vf->priv->yoff, vf->priv->lw, vf->priv->lh, vf->priv->band, vf->priv->show,
00153 mpi->flags&MP_IMGFLAG_DIRECT);
00154 delogo(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2,
00155 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show,
00156 mpi->flags&MP_IMGFLAG_DIRECT);
00157 delogo(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2,
00158 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show,
00159 mpi->flags&MP_IMGFLAG_DIRECT);
00160
00161 vf_clone_mpi_attributes(dmpi, mpi);
00162
00163 return vf_next_put_image(vf,dmpi, pts);
00164 }
00165
00166 static void uninit(struct vf_instance *vf){
00167 if(!vf->priv) return;
00168
00169 free(vf->priv);
00170 vf->priv=NULL;
00171 }
00172
00173
00174
00175 static int query_format(struct vf_instance *vf, unsigned int fmt){
00176 switch(fmt)
00177 {
00178 case IMGFMT_YV12:
00179 case IMGFMT_I420:
00180 case IMGFMT_IYUV:
00181 return vf_next_query_format(vf,vf->priv->outfmt);
00182 }
00183 return 0;
00184 }
00185
00186 static const unsigned int fmt_list[]={
00187 IMGFMT_YV12,
00188 IMGFMT_I420,
00189 IMGFMT_IYUV,
00190 0
00191 };
00192
00193 static int vf_open(vf_instance_t *vf, char *args){
00194 int res=0;
00195 vf->config=config;
00196 vf->put_image=put_image;
00197 vf->get_image=get_image;
00198 vf->query_format=query_format;
00199 vf->uninit=uninit;
00200
00201 vf->priv=malloc(sizeof(struct vf_priv_s));
00202 memset(vf->priv, 0, sizeof(struct vf_priv_s));
00203
00204 if (args) res = sscanf(args, "%d:%d:%d:%d:%d",
00205 &vf->priv->xoff, &vf->priv->yoff,
00206 &vf->priv->lw, &vf->priv->lh,
00207 &vf->priv->band);
00208
00209 if (res != 5) {
00210 mp_msg(MSGT_VFILTER, MSGL_ERR, "deLogo: syntax is \"delogo=xoff:yoff:width:height:band\"\n");
00211 uninit(vf);
00212 return 0;
00213 }
00214
00215 mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d x %d, %d x %d, band = %d\n",
00216 vf->priv->xoff, vf->priv->yoff,
00217 vf->priv->lw, vf->priv->lh,
00218 vf->priv->band);
00219
00220 vf->priv->show = 0;
00221
00222 if (vf->priv->band < 0) {
00223 vf->priv->band = 4;
00224 vf->priv->show = 1;
00225 }
00226
00227
00228 vf->priv->lw += vf->priv->band*2;
00229 vf->priv->lh += vf->priv->band*2;
00230 vf->priv->xoff -= vf->priv->band;
00231 vf->priv->yoff -= vf->priv->band;
00232
00233
00234 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
00235 if(!vf->priv->outfmt)
00236 {
00237 uninit(vf);
00238 return 0;
00239 }
00240
00241 return 1;
00242 }
00243
00244 const vf_info_t vf_info_delogo = {
00245 "simple logo remover",
00246 "delogo",
00247 "Jindrich Makovicka, Alex Beregszaszi",
00248 "",
00249 vf_open,
00250 };
00251
00252