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.
28 #include "img_format.h"
40 static int checkline(unsigned char* src
,int stride
,int len
,int bpp
){
46 total
+=src
[0]; src
+=stride
;
52 total
+=src
[0]+src
[1]+src
[2]; src
+=stride
;
58 // printf("total=%d\n",total);
62 //===========================================================================//
64 static int config(struct vf_instance_s
* vf
,
65 int width
, int height
, int d_width
, int d_height
,
66 unsigned int flags
, unsigned int outfmt
){
67 vf
->priv
->x1
=width
- 1;
68 vf
->priv
->y1
=height
- 1;
72 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
75 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
78 int w
,h
,x
,y
,shrink_by
;
80 // hope we'll get DR buffer:
81 dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
85 dmpi
->planes
[0]=mpi
->planes
[0];
86 dmpi
->planes
[1]=mpi
->planes
[1];
87 dmpi
->planes
[2]=mpi
->planes
[2];
88 dmpi
->stride
[0]=mpi
->stride
[0];
89 dmpi
->stride
[1]=mpi
->stride
[1];
90 dmpi
->stride
[2]=mpi
->stride
[2];
91 dmpi
->width
=mpi
->width
;
92 dmpi
->height
=mpi
->height
;
94 if(++vf
->priv
->fno
>0){ // ignore first 2 frames - they may be empty
96 // Reset the crop area every reset_count frames, if reset_count is > 0
97 if(vf
->priv
->reset_count
> 0 && vf
->priv
->fno
> vf
->priv
->reset_count
){
98 vf
->priv
->x1
=mpi
->w
-1;
99 vf
->priv
->y1
=mpi
->h
-1;
105 for(y
=0;y
<vf
->priv
->y1
;y
++){
106 if(checkline(mpi
->planes
[0]+mpi
->stride
[0]*y
,bpp
,mpi
->w
,bpp
)>vf
->priv
->limit
){
112 for(y
=mpi
->h
-1;y
>vf
->priv
->y2
;y
--){
113 if(checkline(mpi
->planes
[0]+mpi
->stride
[0]*y
,bpp
,mpi
->w
,bpp
)>vf
->priv
->limit
){
119 for(y
=0;y
<vf
->priv
->x1
;y
++){
120 if(checkline(mpi
->planes
[0]+bpp
*y
,mpi
->stride
[0],mpi
->h
,bpp
)>vf
->priv
->limit
){
126 for(y
=mpi
->w
-1;y
>vf
->priv
->x2
;y
--){
127 if(checkline(mpi
->planes
[0]+bpp
*y
,mpi
->stride
[0],mpi
->h
,bpp
)>vf
->priv
->limit
){
133 // round x and y (up), important for yuv colorspaces
134 // make sure they stay rounded!
135 x
=(vf
->priv
->x1
+1)&(~1);
136 y
=(vf
->priv
->y1
+1)&(~1);
138 w
= vf
->priv
->x2
- x
+ 1;
139 h
= vf
->priv
->y2
- y
+ 1;
141 // w and h must be divisible by 2 as well because of yuv
142 // colorspace problems.
143 if (vf
->priv
->round
<= 1)
144 vf
->priv
->round
= 16;
145 if (vf
->priv
->round
% 2)
146 vf
->priv
->round
*= 2;
148 shrink_by
= w
% vf
->priv
->round
;
150 x
+= (shrink_by
/ 2 + 1) & ~1;
152 shrink_by
= h
% vf
->priv
->round
;
154 y
+= (shrink_by
/ 2 + 1) & ~1;
156 mp_msg(MSGT_VFILTER
, MSGL_INFO
, MSGTR_MPCODECS_CropArea
,
157 vf
->priv
->x1
,vf
->priv
->x2
,
158 vf
->priv
->y1
,vf
->priv
->y2
,
164 return vf_next_put_image(vf
,dmpi
, pts
);
167 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
) {
169 // the default limit value works only right with YV12 right now.
171 return vf_next_query_format(vf
, fmt
);
175 //===========================================================================//
177 static int open(vf_instance_t
*vf
, char* args
){
179 vf
->put_image
=put_image
;
180 vf
->query_format
=query_format
;
181 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
182 vf
->priv
->limit
=24; // should be option
184 vf
->priv
->reset_count
= 0;
185 if(args
) sscanf(args
, "%d:%d:%d",
188 &vf
->priv
->reset_count
);
192 const vf_info_t vf_info_cropdetect
= {
193 "autodetect crop size",
201 //===========================================================================//