Only compile fastmemcpybench on x86.
[mplayer/glamo.git] / libmpcodecs / vf_cropdetect.c
blob31389d5fb0c080f092927af265f287d308780072
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "help_mp.h"
10 #include "img_format.h"
11 #include "mp_image.h"
12 #include "vf.h"
14 struct vf_priv_s {
15 int x1,y1,x2,y2;
16 int limit;
17 int round;
18 int fno;
21 static int checkline(unsigned char* src,int stride,int len,int bpp){
22 int total=0;
23 int div=len;
24 switch(bpp){
25 case 1:
26 while(--len>=0){
27 total+=src[0]; src+=stride;
29 break;
30 case 3:
31 case 4:
32 while(--len>=0){
33 total+=src[0]+src[1]+src[2]; src+=stride;
35 div*=3;
36 break;
38 total/=div;
39 // printf("total=%d\n",total);
40 return total;
43 //===========================================================================//
45 static int config(struct vf_instance_s* vf,
46 int width, int height, int d_width, int d_height,
47 unsigned int flags, unsigned int outfmt){
48 vf->priv->x1=width - 1;
49 vf->priv->y1=height - 1;
50 vf->priv->x2=0;
51 vf->priv->y2=0;
52 vf->priv->fno=0;
53 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
56 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
57 mp_image_t *dmpi;
58 int bpp=mpi->bpp/8;
59 int w,h,x,y,shrink_by;
61 // hope we'll get DR buffer:
62 dmpi=vf_get_image(vf->next,mpi->imgfmt,
63 MP_IMGTYPE_EXPORT, 0,
64 mpi->w, mpi->h);
66 dmpi->planes[0]=mpi->planes[0];
67 dmpi->planes[1]=mpi->planes[1];
68 dmpi->planes[2]=mpi->planes[2];
69 dmpi->stride[0]=mpi->stride[0];
70 dmpi->stride[1]=mpi->stride[1];
71 dmpi->stride[2]=mpi->stride[2];
72 dmpi->width=mpi->width;
73 dmpi->height=mpi->height;
75 if(++vf->priv->fno>2){ // ignore first 2 frames - they may be empty
77 for(y=0;y<vf->priv->y1;y++){
78 if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
79 vf->priv->y1=y;
80 break;
84 for(y=mpi->h-1;y>vf->priv->y2;y--){
85 if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
86 vf->priv->y2=y;
87 break;
91 for(y=0;y<vf->priv->x1;y++){
92 if(checkline(mpi->planes[0]+bpp*y,mpi->stride[0],mpi->h,bpp)>vf->priv->limit){
93 vf->priv->x1=y;
94 break;
98 for(y=mpi->w-1;y>vf->priv->x2;y--){
99 if(checkline(mpi->planes[0]+bpp*y,mpi->stride[0],mpi->h,bpp)>vf->priv->limit){
100 vf->priv->x2=y;
101 break;
105 // round x and y (up), important for yuv colorspaces
106 // make sure they stay rounded!
107 x=(vf->priv->x1+1)&(~1);
108 y=(vf->priv->y1+1)&(~1);
110 w = vf->priv->x2 - x + 1;
111 h = vf->priv->y2 - y + 1;
113 // w and h must be divisible by 2 as well because of yuv
114 // colorspace problems.
115 if (vf->priv->round <= 1)
116 vf->priv->round = 16;
117 if (vf->priv->round % 2)
118 vf->priv->round *= 2;
120 shrink_by = w % vf->priv->round;
121 w -= shrink_by;
122 x += (shrink_by / 2 + 1) & ~1;
124 shrink_by = h % vf->priv->round;
125 h -= shrink_by;
126 y += (shrink_by / 2 + 1) & ~1;
128 mp_msg(MSGT_VFILTER, MSGL_INFO, MSGTR_MPCODECS_CropArea,
129 vf->priv->x1,vf->priv->x2,
130 vf->priv->y1,vf->priv->y2,
131 w,h,x,y);
136 return vf_next_put_image(vf,dmpi, pts);
139 static int query_format(struct vf_instance_s* vf, unsigned int fmt) {
140 switch(fmt) {
141 // the default limit value works only right with YV12 right now.
142 case IMGFMT_YV12:
143 return vf_next_query_format(vf, fmt);
145 return 0;
147 //===========================================================================//
149 static int open(vf_instance_t *vf, char* args){
150 vf->config=config;
151 vf->put_image=put_image;
152 vf->query_format=query_format;
153 vf->priv=malloc(sizeof(struct vf_priv_s));
154 vf->priv->limit=24; // should be option
155 vf->priv->round = 0;
156 if(args) sscanf(args, "%d:%d",
157 &vf->priv->limit,
158 &vf->priv->round);
159 return 1;
162 const vf_info_t vf_info_cropdetect = {
163 "autodetect crop size",
164 "cropdetect",
165 "A'rpi",
167 open,
168 NULL
171 //===========================================================================//