annotate libmpcodecs/vf_cropdetect.c @ 24622:fa3923706f17

some whitespace cosmetics r22854: Remove empty section. r22951: Move netstream documentation into TOOLS/README. r23100: Update AMR instructions. r23161: Remove outdated and wrong references to codecs.conf. r23225: The GUI no longer depends on libpng. r23226: MJPEG decoding does not depend on libjpeg.
author kraymer
date Fri, 28 Sep 2007 01:28:42 +0000
parents f8d4f8eff72b
children 00fff9a3b735
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
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16107
diff changeset
6 #include "config.h"
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16107
diff changeset
7 #include "mp_msg.h"
18004
bcd805923554 Part2 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu with LOTS of modifications by me
reynaldo
parents: 17906
diff changeset
8 #include "help_mp.h"
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
9
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
10 #include "img_format.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
11 #include "mp_image.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
12 #include "vf.h"
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
13
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
14 struct vf_priv_s {
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
15 int x1,y1,x2,y2;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
16 int limit;
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
17 int round;
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
18 int fno;
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
19 };
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 static int checkline(unsigned char* src,int stride,int len,int bpp){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
22 int total=0;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
23 int div=len;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
24 switch(bpp){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
25 case 1:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
26 while(--len>=0){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
27 total+=src[0]; src+=stride;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
28 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
29 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
30 case 3:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
31 case 4:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
32 while(--len>=0){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
33 total+=src[0]+src[1]+src[2]; src+=stride;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
34 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
35 div*=3;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
36 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
37 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
38 total/=div;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
39 // printf("total=%d\n",total);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
40 return total;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
41 }
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 static int config(struct vf_instance_s* vf,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
46 int width, int height, int d_width, int d_height,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
47 unsigned int flags, unsigned int outfmt){
14588
36b24b57ab53 x1 and y1 give last used position, must be < width/height
reimar
parents: 13233
diff changeset
48 vf->priv->x1=width - 1;
36b24b57ab53 x1 and y1 give last used position, must be < width/height
reimar
parents: 13233
diff changeset
49 vf->priv->y1=height - 1;
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
50 vf->priv->x2=0;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
51 vf->priv->y2=0;
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
52 vf->priv->fno=0;
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
53 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
54 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
55
17906
20aca9baf5d8 passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents: 17012
diff changeset
56 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
57 mp_image_t *dmpi;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
58 int bpp=mpi->bpp/8;
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
59 int w,h,x,y,shrink_by;
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
60
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
61 // hope we'll get DR buffer:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
62 dmpi=vf_get_image(vf->next,mpi->imgfmt,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
63 MP_IMGTYPE_EXPORT, 0,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
64 mpi->w, mpi->h);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
65
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
66 dmpi->planes[0]=mpi->planes[0];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
67 dmpi->planes[1]=mpi->planes[1];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
68 dmpi->planes[2]=mpi->planes[2];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
69 dmpi->stride[0]=mpi->stride[0];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
70 dmpi->stride[1]=mpi->stride[1];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
71 dmpi->stride[2]=mpi->stride[2];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
72 dmpi->width=mpi->width;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
73 dmpi->height=mpi->height;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
74
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
75 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
76
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
77 for(y=0;y<vf->priv->y1;y++){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
78 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
79 vf->priv->y1=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
80 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
81 }
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 for(y=mpi->h-1;y>vf->priv->y2;y--){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
85 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
86 vf->priv->y2=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
87 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
88 }
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 for(y=0;y<vf->priv->x1;y++){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
92 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
93 vf->priv->x1=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
94 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
95 }
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 for(y=mpi->w-1;y>vf->priv->x2;y--){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
99 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
100 vf->priv->x2=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
101 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
102 }
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
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
105 // round x and y (up), important for yuv colorspaces
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
106 // make sure they stay rounded!
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
107 x=(vf->priv->x1+1)&(~1);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
108 y=(vf->priv->y1+1)&(~1);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
109
14588
36b24b57ab53 x1 and y1 give last used position, must be < width/height
reimar
parents: 13233
diff changeset
110 w = vf->priv->x2 - x + 1;
36b24b57ab53 x1 and y1 give last used position, must be < width/height
reimar
parents: 13233
diff changeset
111 h = vf->priv->y2 - y + 1;
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
112
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
113 // w and h must be divisible by 2 as well because of yuv
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
114 // colorspace problems.
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
115 if (vf->priv->round <= 1)
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
116 vf->priv->round = 16;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
117 if (vf->priv->round % 2)
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
118 vf->priv->round *= 2;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
119
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
120 shrink_by = w % vf->priv->round;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
121 w -= shrink_by;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
122 x += (shrink_by / 2 + 1) & ~1;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
123
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
124 shrink_by = h % vf->priv->round;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
125 h -= shrink_by;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
126 y += (shrink_by / 2 + 1) & ~1;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
127
18004
bcd805923554 Part2 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu with LOTS of modifications by me
reynaldo
parents: 17906
diff changeset
128 mp_msg(MSGT_VFILTER, MSGL_INFO, MSGTR_MPCODECS_CropArea,
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
129 vf->priv->x1,vf->priv->x2,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
130 vf->priv->y1,vf->priv->y2,
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
131 w,h,x,y);
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
132
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
133
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
134 }
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
135
17906
20aca9baf5d8 passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents: 17012
diff changeset
136 return vf_next_put_image(vf,dmpi, pts);
6061
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
16107
8d4a3c9001de restrict to YV12 since the default limit does not work well for anything else.
reimar
parents: 14588
diff changeset
139 static int query_format(struct vf_instance_s* vf, unsigned int fmt) {
8d4a3c9001de restrict to YV12 since the default limit does not work well for anything else.
reimar
parents: 14588
diff changeset
140 switch(fmt) {
8d4a3c9001de restrict to YV12 since the default limit does not work well for anything else.
reimar
parents: 14588
diff changeset
141 // the default limit value works only right with YV12 right now.
8d4a3c9001de restrict to YV12 since the default limit does not work well for anything else.
reimar
parents: 14588
diff changeset
142 case IMGFMT_YV12:
8d4a3c9001de restrict to YV12 since the default limit does not work well for anything else.
reimar
parents: 14588
diff changeset
143 return vf_next_query_format(vf, fmt);
8d4a3c9001de restrict to YV12 since the default limit does not work well for anything else.
reimar
parents: 14588
diff changeset
144 }
8d4a3c9001de restrict to YV12 since the default limit does not work well for anything else.
reimar
parents: 14588
diff changeset
145 return 0;
8d4a3c9001de restrict to YV12 since the default limit does not work well for anything else.
reimar
parents: 14588
diff changeset
146 }
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
147 //===========================================================================//
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
148
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
149 static int open(vf_instance_t *vf, char* args){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
150 vf->config=config;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
151 vf->put_image=put_image;
16107
8d4a3c9001de restrict to YV12 since the default limit does not work well for anything else.
reimar
parents: 14588
diff changeset
152 vf->query_format=query_format;
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
153 vf->priv=malloc(sizeof(struct vf_priv_s));
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
154 vf->priv->limit=24; // should be option
13233
3b85939d9f0a missing initialization of round
reimar
parents: 13205
diff changeset
155 vf->priv->round = 0;
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
156 if(args) sscanf(args, "%d:%d",
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
157 &vf->priv->limit,
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
158 &vf->priv->round);
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
159 return 1;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
160 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
161
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
162 vf_info_t vf_info_cropdetect = {
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
163 "autodetect crop size",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
164 "cropdetect",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
165 "A'rpi",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
166 "",
9593
e9a2af584986 Add the new -vf option wich is the same as vop in reverse order.
albeu
parents: 7472
diff changeset
167 open,
e9a2af584986 Add the new -vf option wich is the same as vop in reverse order.
albeu
parents: 7472
diff changeset
168 NULL
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
169 };
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
170
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
171 //===========================================================================//