00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include "mp_image.h"
00023 #include "mp_msg.h"
00024 #include "vf.h"
00025
00026 #include "libvo/fastmemcpy.h"
00027 #include "libavutil/common.h"
00028
00029 struct vf_priv_s {
00030 int x, y, w, h;
00031 };
00032
00033 static int
00034 config(struct vf_instance *vf,
00035 int width, int height, int d_width, int d_height,
00036 unsigned int flags, unsigned int outfmt)
00037 {
00038 if (vf->priv->w < 0 || width < vf->priv->w)
00039 vf->priv->w = width;
00040 if (vf->priv->h < 0 || height < vf->priv->h)
00041 vf->priv->h = height;
00042 if (vf->priv->x < 0)
00043 vf->priv->x = (width - vf->priv->w) / 2;
00044 if (vf->priv->y < 0)
00045 vf->priv->y = (height - vf->priv->h) / 2;
00046 if (vf->priv->w + vf->priv->x > width
00047 || vf->priv->h + vf->priv->y > height) {
00048 mp_msg(MSGT_VFILTER,MSGL_WARN,"rectangle: bad position/width/height - rectangle area is out of the original!\n");
00049 return 0;
00050 }
00051 return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt);
00052 }
00053
00054 static int
00055 control(struct vf_instance *vf, int request, void *data)
00056 {
00057 const int *const tmp = data;
00058 switch(request){
00059 case VFCTRL_CHANGE_RECTANGLE:
00060 switch (tmp[0]){
00061 case 0:
00062 vf->priv->w += tmp[1];
00063 return 1;
00064 break;
00065 case 1:
00066 vf->priv->h += tmp[1];
00067 return 1;
00068 break;
00069 case 2:
00070 vf->priv->x += tmp[1];
00071 return 1;
00072 break;
00073 case 3:
00074 vf->priv->y += tmp[1];
00075 return 1;
00076 break;
00077 default:
00078 mp_msg(MSGT_VFILTER,MSGL_FATAL,"Unknown param %d \n", tmp[0]);
00079 return 0;
00080 }
00081 }
00082 return vf_next_control(vf, request, data);
00083 return 0;
00084 }
00085 static int
00086 put_image(struct vf_instance *vf, mp_image_t* mpi, double pts){
00087 mp_image_t* dmpi;
00088 unsigned int bpp = mpi->bpp / 8;
00089 int x, y, w, h;
00090 dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_TEMP,
00091 MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
00092 mpi->w, mpi->h);
00093
00094 memcpy_pic(dmpi->planes[0],mpi->planes[0],mpi->w*bpp, mpi->h,
00095 dmpi->stride[0],mpi->stride[0]);
00096 if(mpi->flags&MP_IMGFLAG_PLANAR && mpi->flags&MP_IMGFLAG_YUV){
00097 memcpy_pic(dmpi->planes[1],mpi->planes[1],
00098 mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift,
00099 dmpi->stride[1],mpi->stride[1]);
00100 memcpy_pic(dmpi->planes[2],mpi->planes[2],
00101 mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift,
00102 dmpi->stride[2],mpi->stride[2]);
00103 }
00104
00105
00106
00107 mp_msg(MSGT_VFILTER,MSGL_INFO, "rectangle: -vf rectangle=%d:%d:%d:%d \n", vf->priv->w, vf->priv->h, vf->priv->x, vf->priv->y);
00108
00109 x = FFMIN(vf->priv->x, dmpi->width);
00110 x = FFMAX(x, 0);
00111
00112 w = vf->priv->x + vf->priv->w - 1 - x;
00113 w = FFMIN(w, dmpi->width - x);
00114 w = FFMAX(w, 0);
00115
00116 y = FFMIN(vf->priv->y, dmpi->height);
00117 y = FFMAX(y, 0);
00118
00119 h = vf->priv->y + vf->priv->h - 1 - y;
00120 h = FFMIN(h, dmpi->height - y);
00121 h = FFMAX(h, 0);
00122
00123 if (0 <= vf->priv->y && vf->priv->y <= dmpi->height) {
00124 unsigned char *p = dmpi->planes[0] + y * dmpi->stride[0] + x * bpp;
00125 unsigned int count = w * bpp;
00126 while (count--)
00127 p[count] = 0xff - p[count];
00128 }
00129 if (h != 1 && vf->priv->y + vf->priv->h - 1 <= mpi->height) {
00130 unsigned char *p = dmpi->planes[0] + (vf->priv->y + vf->priv->h - 1) * dmpi->stride[0] + x * bpp;
00131 unsigned int count = w * bpp;
00132 while (count--)
00133 p[count] = 0xff - p[count];
00134 }
00135 if (0 <= vf->priv->x && vf->priv->x <= dmpi->width) {
00136 unsigned char *p = dmpi->planes[0] + y * dmpi->stride[0] + x * bpp;
00137 unsigned int count = h;
00138 while (count--) {
00139 unsigned int i = bpp;
00140 while (i--)
00141 p[i] = 0xff - p[i];
00142 p += dmpi->stride[0];
00143 }
00144 }
00145 if (w != 1 && vf->priv->x + vf->priv->w - 1 <= mpi->width) {
00146 unsigned char *p = dmpi->planes[0] + y * dmpi->stride[0] + (vf->priv->x + vf->priv->w - 1) * bpp;
00147 unsigned int count = h;
00148 while (count--) {
00149 unsigned int i = bpp;
00150 while (i--)
00151 p[i] = 0xff - p[i];
00152 p += dmpi->stride[0];
00153 }
00154 }
00155 return vf_next_put_image(vf, dmpi, pts);
00156 }
00157
00158 static int
00159 vf_open(vf_instance_t *vf, char *args) {
00160 vf->config = config;
00161 vf->control = control;
00162 vf->put_image = put_image;
00163 vf->priv = malloc(sizeof(struct vf_priv_s));
00164 vf->priv->x = -1;
00165 vf->priv->y = -1;
00166 vf->priv->w = -1;
00167 vf->priv->h = -1;
00168 if (args)
00169 sscanf(args, "%d:%d:%d:%d",
00170 &vf->priv->w, &vf->priv->h, &vf->priv->x, &vf->priv->y);
00171 return 1;
00172 }
00173
00174 const vf_info_t vf_info_rectangle = {
00175 "draw rectangle",
00176 "rectangle",
00177 "Kim Minh Kaplan",
00178 "",
00179 vf_open,
00180 NULL
00181 };