]> git.sesse.net Git - ffmpeg/blob - libavfilter/libmpcodecs/vf_rectangle.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / libmpcodecs / vf_rectangle.c
1 /*
2  * This file is part of MPlayer.
3  *
4  * MPlayer is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * MPlayer is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "mp_image.h"
23 #include "mp_msg.h"
24 #include "vf.h"
25
26 #include "libvo/fastmemcpy.h"
27 #include "libavutil/common.h"
28
29 struct vf_priv_s {
30     int x, y, w, h;
31 };
32
33 static int
34 config(struct vf_instance *vf,
35        int width, int height, int d_width, int d_height,
36        unsigned int flags, unsigned int outfmt)
37 {
38     if (vf->priv->w < 0 || width < vf->priv->w)
39         vf->priv->w = width;
40     if (vf->priv->h < 0 || height < vf->priv->h)
41         vf->priv->h = height;
42     if (vf->priv->x < 0)
43         vf->priv->x = (width - vf->priv->w) / 2;
44     if (vf->priv->y < 0)
45         vf->priv->y = (height - vf->priv->h) / 2;
46     if (vf->priv->w + vf->priv->x > width
47         || vf->priv->h + vf->priv->y > height) {
48         mp_msg(MSGT_VFILTER,MSGL_WARN,"rectangle: bad position/width/height - rectangle area is out of the original!\n");
49         return 0;
50     }
51     return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt);
52 }
53
54 static int
55 control(struct vf_instance *vf, int request, void *data)
56 {
57     const int *const tmp = data;
58     switch(request){
59     case VFCTRL_CHANGE_RECTANGLE:
60         switch (tmp[0]){
61         case 0:
62             vf->priv->w += tmp[1];
63             return 1;
64             break;
65         case 1:
66             vf->priv->h += tmp[1];
67             return 1;
68             break;
69         case 2:
70             vf->priv->x += tmp[1];
71             return 1;
72             break;
73         case 3:
74             vf->priv->y += tmp[1];
75             return 1;
76             break;
77         default:
78             mp_msg(MSGT_VFILTER,MSGL_FATAL,"Unknown param %d \n", tmp[0]);
79             return 0;
80         }
81     }
82     return vf_next_control(vf, request, data);
83     return 0;
84 }
85 static int
86 put_image(struct vf_instance *vf, mp_image_t* mpi, double pts){
87     mp_image_t* dmpi;
88     unsigned int bpp = mpi->bpp / 8;
89     int x, y, w, h;
90     dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_TEMP,
91                         MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
92                         mpi->w, mpi->h);
93
94     memcpy_pic(dmpi->planes[0],mpi->planes[0],mpi->w*bpp, mpi->h,
95                dmpi->stride[0],mpi->stride[0]);
96     if(mpi->flags&MP_IMGFLAG_PLANAR && mpi->flags&MP_IMGFLAG_YUV){
97         memcpy_pic(dmpi->planes[1],mpi->planes[1],
98                    mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift,
99                    dmpi->stride[1],mpi->stride[1]);
100         memcpy_pic(dmpi->planes[2],mpi->planes[2],
101                    mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift,
102                    dmpi->stride[2],mpi->stride[2]);
103     }
104
105     /* Draw the rectangle */
106
107     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);
108
109     x = FFMIN(vf->priv->x, dmpi->width);
110     x = FFMAX(x, 0);
111
112     w = vf->priv->x + vf->priv->w - 1 - x;
113     w = FFMIN(w, dmpi->width - x);
114     w = FFMAX(w, 0);
115
116     y = FFMIN(vf->priv->y, dmpi->height);
117     y = FFMAX(y, 0);
118
119     h = vf->priv->y + vf->priv->h - 1 - y;
120     h = FFMIN(h, dmpi->height - y);
121     h = FFMAX(h, 0);
122
123     if (0 <= vf->priv->y && vf->priv->y <= dmpi->height) {
124         unsigned char *p = dmpi->planes[0] + y * dmpi->stride[0] + x * bpp;
125         unsigned int count = w * bpp;
126         while (count--)
127             p[count] = 0xff - p[count];
128     }
129     if (h != 1 && vf->priv->y + vf->priv->h - 1 <= mpi->height) {
130         unsigned char *p = dmpi->planes[0] + (vf->priv->y + vf->priv->h - 1) * dmpi->stride[0] + x * bpp;
131         unsigned int count = w * bpp;
132         while (count--)
133             p[count] = 0xff - p[count];
134     }
135     if (0 <= vf->priv->x  && vf->priv->x <= dmpi->width) {
136         unsigned char *p = dmpi->planes[0] + y * dmpi->stride[0] + x * bpp;
137         unsigned int count = h;
138         while (count--) {
139             unsigned int i = bpp;
140             while (i--)
141                 p[i] = 0xff - p[i];
142             p += dmpi->stride[0];
143         }
144     }
145     if (w != 1 && vf->priv->x + vf->priv->w - 1 <= mpi->width) {
146         unsigned char *p = dmpi->planes[0] + y * dmpi->stride[0] + (vf->priv->x + vf->priv->w - 1) * bpp;
147         unsigned int count = h;
148         while (count--) {
149             unsigned int i = bpp;
150             while (i--)
151                 p[i] = 0xff - p[i];
152             p += dmpi->stride[0];
153         }
154     }
155     return vf_next_put_image(vf, dmpi, pts);
156 }
157
158 static int
159 vf_open(vf_instance_t *vf, char *args) {
160     vf->config = config;
161     vf->control = control;
162     vf->put_image = put_image;
163     vf->priv = malloc(sizeof(struct vf_priv_s));
164     vf->priv->x = -1;
165     vf->priv->y = -1;
166     vf->priv->w = -1;
167     vf->priv->h = -1;
168     if (args)
169         sscanf(args, "%d:%d:%d:%d",
170                &vf->priv->w, &vf->priv->h, &vf->priv->x, &vf->priv->y);
171     return 1;
172 }
173
174 const vf_info_t vf_info_rectangle = {
175     "draw rectangle",
176     "rectangle",
177     "Kim Minh Kaplan",
178     "",
179     vf_open,
180     NULL
181 };