vo_gl: Some small whitespace and {} cosmetics
[mplayer/glamo.git] / libmpcodecs / vf_cropdetect.c
blob2401db19d422d88a13c622a95b7ecd77733513e1
1 /*
2 * This file is part of MPlayer.
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.
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.
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
24 #include "config.h"
25 #include "mp_msg.h"
27 #include "img_format.h"
28 #include "mp_image.h"
29 #include "vf.h"
31 struct vf_priv_s {
32 int x1,y1,x2,y2;
33 int limit;
34 int round;
35 int reset_count;
36 int fno;
39 static int checkline(unsigned char* src,int stride,int len,int bpp){
40 int total=0;
41 int div=len;
42 switch(bpp){
43 case 1:
44 while(--len>=0){
45 total+=src[0]; src+=stride;
47 break;
48 case 3:
49 case 4:
50 while(--len>=0){
51 total+=src[0]+src[1]+src[2]; src+=stride;
53 div*=3;
54 break;
56 total/=div;
57 // printf("total=%d\n",total);
58 return total;
61 //===========================================================================//
63 static int config(struct vf_instance *vf,
64 int width, int height, int d_width, int d_height,
65 unsigned int flags, unsigned int outfmt){
66 vf->priv->x1=width - 1;
67 vf->priv->y1=height - 1;
68 vf->priv->x2=0;
69 vf->priv->y2=0;
70 vf->priv->fno=-2;
71 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
74 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
75 mp_image_t *dmpi;
76 int bpp=mpi->bpp/8;
77 int w,h,x,y,shrink_by;
79 // hope we'll get DR buffer:
80 dmpi=vf_get_image(vf->next,mpi->imgfmt,
81 MP_IMGTYPE_EXPORT, 0,
82 mpi->w, mpi->h);
84 dmpi->planes[0]=mpi->planes[0];
85 dmpi->planes[1]=mpi->planes[1];
86 dmpi->planes[2]=mpi->planes[2];
87 dmpi->stride[0]=mpi->stride[0];
88 dmpi->stride[1]=mpi->stride[1];
89 dmpi->stride[2]=mpi->stride[2];
90 dmpi->width=mpi->width;
91 dmpi->height=mpi->height;
93 if(++vf->priv->fno>0){ // ignore first 2 frames - they may be empty
95 // Reset the crop area every reset_count frames, if reset_count is > 0
96 if(vf->priv->reset_count > 0 && vf->priv->fno > vf->priv->reset_count){
97 vf->priv->x1=mpi->w-1;
98 vf->priv->y1=mpi->h-1;
99 vf->priv->x2=0;
100 vf->priv->y2=0;
101 vf->priv->fno=1;
104 for(y=0;y<vf->priv->y1;y++){
105 if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
106 vf->priv->y1=y;
107 break;
111 for(y=mpi->h-1;y>vf->priv->y2;y--){
112 if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
113 vf->priv->y2=y;
114 break;
118 for(y=0;y<vf->priv->x1;y++){
119 if(checkline(mpi->planes[0]+bpp*y,mpi->stride[0],mpi->h,bpp)>vf->priv->limit){
120 vf->priv->x1=y;
121 break;
125 for(y=mpi->w-1;y>vf->priv->x2;y--){
126 if(checkline(mpi->planes[0]+bpp*y,mpi->stride[0],mpi->h,bpp)>vf->priv->limit){
127 vf->priv->x2=y;
128 break;
132 // round x and y (up), important for yuv colorspaces
133 // make sure they stay rounded!
134 x=(vf->priv->x1+1)&(~1);
135 y=(vf->priv->y1+1)&(~1);
137 w = vf->priv->x2 - x + 1;
138 h = vf->priv->y2 - y + 1;
140 // w and h must be divisible by 2 as well because of yuv
141 // colorspace problems.
142 if (vf->priv->round <= 1)
143 vf->priv->round = 16;
144 if (vf->priv->round % 2)
145 vf->priv->round *= 2;
147 shrink_by = w % vf->priv->round;
148 w -= shrink_by;
149 x += (shrink_by / 2 + 1) & ~1;
151 shrink_by = h % vf->priv->round;
152 h -= shrink_by;
153 y += (shrink_by / 2 + 1) & ~1;
155 mp_tmsg(MSGT_VFILTER, MSGL_INFO, "[CROP] Crop area: X: %d..%d Y: %d..%d (-vf crop=%d:%d:%d:%d).\n",
156 vf->priv->x1,vf->priv->x2,
157 vf->priv->y1,vf->priv->y2,
158 w,h,x,y);
163 return vf_next_put_image(vf,dmpi, pts);
166 static int query_format(struct vf_instance *vf, unsigned int fmt) {
167 switch(fmt) {
168 // the default limit value works only right with YV12 right now.
169 case IMGFMT_YV12:
170 return vf_next_query_format(vf, fmt);
172 return 0;
174 //===========================================================================//
176 static int vf_open(vf_instance_t *vf, char *args){
177 vf->config=config;
178 vf->put_image=put_image;
179 vf->query_format=query_format;
180 vf->priv=malloc(sizeof(struct vf_priv_s));
181 vf->priv->limit=24; // should be option
182 vf->priv->round = 0;
183 vf->priv->reset_count = 0;
184 if(args) sscanf(args, "%d:%d:%d",
185 &vf->priv->limit,
186 &vf->priv->round,
187 &vf->priv->reset_count);
188 return 1;
191 const vf_info_t vf_info_cropdetect = {
192 "autodetect crop size",
193 "cropdetect",
194 "A'rpi",
196 vf_open,
197 NULL
200 //===========================================================================//