annotate libmpcodecs/vf_cropdetect.c @ 8694:79c0205ab4b5

sending 10l to Filip
author attila
date Wed, 01 Jan 2003 14:16:02 +0000
parents c4434bdf6e51
children e9a2af584986
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
1 #include <stdio.h>
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
2 #include <stdlib.h>
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
3 #include <string.h>
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
4 #include <inttypes.h>
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
5
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
6 #include "../config.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
7 #include "../mp_msg.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
8
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
9 #include "img_format.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
10 #include "mp_image.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
11 #include "vf.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
12
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
13 #include "../libvo/fastmemcpy.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
14 #include "../postproc/rgb2rgb.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
15
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
16 struct vf_priv_s {
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
17 int x1,y1,x2,y2;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
18 int limit;
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
19 int fno;
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
20 };
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
21
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
22 static int checkline(unsigned char* src,int stride,int len,int bpp){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
23 int total=0;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
24 int div=len;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
25 switch(bpp){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
26 case 1:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
27 while(--len>=0){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
28 total+=src[0]; src+=stride;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
29 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
30 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
31 case 3:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
32 case 4:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
33 while(--len>=0){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
34 total+=src[0]+src[1]+src[2]; src+=stride;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
35 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
36 div*=3;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
37 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
38 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
39 total/=div;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
40 // printf("total=%d\n",total);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
41 return total;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
42 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
43
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
44 //===========================================================================//
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
45
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
46 static int config(struct vf_instance_s* vf,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
47 int width, int height, int d_width, int d_height,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
48 unsigned int flags, unsigned int outfmt){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
49 vf->priv->x1=width;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
50 vf->priv->y1=height;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
51 vf->priv->x2=0;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
52 vf->priv->y2=0;
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
53 vf->priv->fno=0;
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
54 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
55 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
56
7368
a894e99c1e51 changing return type of put_image void->int
arpi
parents: 6610
diff changeset
57 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
58 mp_image_t *dmpi;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
59 int bpp=mpi->bpp/8;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
60 int x,y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
61
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
62 // hope we'll get DR buffer:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
63 dmpi=vf_get_image(vf->next,mpi->imgfmt,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
64 MP_IMGTYPE_EXPORT, 0,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
65 mpi->w, mpi->h);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
66
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
67 dmpi->planes[0]=mpi->planes[0];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
68 dmpi->planes[1]=mpi->planes[1];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
69 dmpi->planes[2]=mpi->planes[2];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
70 dmpi->stride[0]=mpi->stride[0];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
71 dmpi->stride[1]=mpi->stride[1];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
72 dmpi->stride[2]=mpi->stride[2];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
73 dmpi->width=mpi->width;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
74 dmpi->height=mpi->height;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
75
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
76 if(++vf->priv->fno>2){ // ignore first 2 frames - they may be empty
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
77
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
78 for(y=0;y<vf->priv->y1;y++){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
79 if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
80 vf->priv->y1=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
81 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
82 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
83 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
84
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
85 for(y=mpi->h-1;y>vf->priv->y2;y--){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
86 if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
87 vf->priv->y2=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
88 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
89 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
90 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
91
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
92 for(y=0;y<vf->priv->x1;y++){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
93 if(checkline(mpi->planes[0]+bpp*y,mpi->stride[0],mpi->h,bpp)>vf->priv->limit){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
94 vf->priv->x1=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
95 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
96 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
97 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
98
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
99 for(y=mpi->w-1;y>vf->priv->x2;y--){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
100 if(checkline(mpi->planes[0]+bpp*y,mpi->stride[0],mpi->h,bpp)>vf->priv->limit){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
101 vf->priv->x2=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
102 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
103 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
104 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
105
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
106 x=(vf->priv->x1+1)&(~1);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
107 y=(vf->priv->y1+1)&(~1);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
108
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
109 printf("crop area: X: %d..%d Y: %d..%d (-vop crop=%d:%d:%d:%d)\n",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
110 vf->priv->x1,vf->priv->x2,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
111 vf->priv->y1,vf->priv->y2,
6117
0c33406d2a83 small fix
arpi
parents: 6061
diff changeset
112 (vf->priv->x2+1-x)&(~1),(vf->priv->y2+1-y)&(~1),x,y
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
113 );
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
114
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
115 }
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
116
7368
a894e99c1e51 changing return type of put_image void->int
arpi
parents: 6610
diff changeset
117 return vf_next_put_image(vf,dmpi);
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
118 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
119
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
120 //===========================================================================//
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
121
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
122 static int open(vf_instance_t *vf, char* args){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
123 vf->config=config;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
124 vf->put_image=put_image;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
125 vf->priv=malloc(sizeof(struct vf_priv_s));
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
126 vf->priv->limit=24; // should be option
6610
242d1b98a15f parameter overrides limit
arpi
parents: 6118
diff changeset
127 if(args) vf->priv->limit=atoi(args);
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
128 return 1;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
129 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
130
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
131 vf_info_t vf_info_cropdetect = {
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
132 "autodetect crop size",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
133 "cropdetect",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
134 "A'rpi",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
135 "",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
136 open
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
137 };
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
138
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
139 //===========================================================================//