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.
27 #include "img_format.h"
39 static int checkline(unsigned char* src
,int stride
,int len
,int bpp
){
45 total
+=src
[0]; src
+=stride
;
51 total
+=src
[0]+src
[1]+src
[2]; src
+=stride
;
57 // printf("total=%d\n",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;
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
){
77 int w
,h
,x
,y
,shrink_by
;
79 // hope we'll get DR buffer:
80 dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
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;
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
){
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
){
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
){
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
){
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
;
149 x
+= (shrink_by
/ 2 + 1) & ~1;
151 shrink_by
= h
% vf
->priv
->round
;
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
,
163 return vf_next_put_image(vf
,dmpi
, pts
);
166 static int query_format(struct vf_instance
*vf
, unsigned int fmt
) {
168 // the default limit value works only right with YV12 right now.
170 return vf_next_query_format(vf
, fmt
);
174 //===========================================================================//
176 static int vf_open(vf_instance_t
*vf
, char *args
){
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
183 vf
->priv
->reset_count
= 0;
184 if(args
) sscanf(args
, "%d:%d:%d",
187 &vf
->priv
->reset_count
);
191 const vf_info_t vf_info_cropdetect
= {
192 "autodetect crop size",
200 //===========================================================================//