annotate libmpcodecs/vf_cropdetect.c @ 14104:0e334ea33ebf

MPlayer-translations mailing list created.
author diego
date Sun, 05 Dec 2004 13:07:57 +0000
parents 3b85939d9f0a
children 36b24b57ab53
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;
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
19 int round;
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
20 int fno;
6061
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
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
23 static int checkline(unsigned char* src,int stride,int len,int bpp){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
24 int total=0;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
25 int div=len;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
26 switch(bpp){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
27 case 1:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
28 while(--len>=0){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
29 total+=src[0]; src+=stride;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
30 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
31 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
32 case 3:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
33 case 4:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
34 while(--len>=0){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
35 total+=src[0]+src[1]+src[2]; src+=stride;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
36 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
37 div*=3;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
38 break;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
39 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
40 total/=div;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
41 // printf("total=%d\n",total);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
42 return total;
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
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
47 static int config(struct vf_instance_s* vf,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
48 int width, int height, int d_width, int d_height,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
49 unsigned int flags, unsigned int outfmt){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
50 vf->priv->x1=width;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
51 vf->priv->y1=height;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
52 vf->priv->x2=0;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
53 vf->priv->y2=0;
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
54 vf->priv->fno=0;
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
55 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
56 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
57
7368
a894e99c1e51 changing return type of put_image void->int
arpi
parents: 6610
diff changeset
58 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
59 mp_image_t *dmpi;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
60 int bpp=mpi->bpp/8;
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
61 int w,h,x,y,shrink_by;
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
62
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
63 // hope we'll get DR buffer:
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
64 dmpi=vf_get_image(vf->next,mpi->imgfmt,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
65 MP_IMGTYPE_EXPORT, 0,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
66 mpi->w, mpi->h);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
67
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
68 dmpi->planes[0]=mpi->planes[0];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
69 dmpi->planes[1]=mpi->planes[1];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
70 dmpi->planes[2]=mpi->planes[2];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
71 dmpi->stride[0]=mpi->stride[0];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
72 dmpi->stride[1]=mpi->stride[1];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
73 dmpi->stride[2]=mpi->stride[2];
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
74 dmpi->width=mpi->width;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
75 dmpi->height=mpi->height;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
76
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
77 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
78
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
79 for(y=0;y<vf->priv->y1;y++){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
80 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
81 vf->priv->y1=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
82 break;
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
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
86 for(y=mpi->h-1;y>vf->priv->y2;y--){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
87 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
88 vf->priv->y2=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
89 break;
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
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
93 for(y=0;y<vf->priv->x1;y++){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
94 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
95 vf->priv->x1=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
96 break;
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
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
100 for(y=mpi->w-1;y>vf->priv->x2;y--){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
101 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
102 vf->priv->x2=y;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
103 break;
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
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
107 // round x and y (up), important for yuv colorspaces
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
108 // make sure they stay rounded!
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
109 x=(vf->priv->x1+1)&(~1);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
110 y=(vf->priv->y1+1)&(~1);
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
111
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
112 w = vf->priv->x2 - x;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
113 h = vf->priv->y2 - y;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
114
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
115 // 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
116 // colorspace problems.
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
117 if (vf->priv->round <= 1)
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
118 vf->priv->round = 16;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
119 if (vf->priv->round % 2)
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
120 vf->priv->round *= 2;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
121
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
122 shrink_by = w % vf->priv->round;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
123 w -= shrink_by;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
124 x += (shrink_by / 2 + 1) & ~1;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
125
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
126 shrink_by = h % vf->priv->round;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
127 h -= shrink_by;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
128 y += (shrink_by / 2 + 1) & ~1;
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
129
11261
835822ce4bb1 -vop ---> -vf
diego
parents: 9593
diff changeset
130 printf("crop area: X: %d..%d Y: %d..%d (-vf crop=%d:%d:%d:%d)\n",
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
131 vf->priv->x1,vf->priv->x2,
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
132 vf->priv->y1,vf->priv->y2,
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
133 w,h,x,y);
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
134
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
135
6118
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
136 }
f869ece65aab ignore first 2 frames
arpi
parents: 6117
diff changeset
137
7368
a894e99c1e51 changing return type of put_image void->int
arpi
parents: 6610
diff changeset
138 return vf_next_put_image(vf,dmpi);
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
139 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
140
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
141 //===========================================================================//
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
142
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
143 static int open(vf_instance_t *vf, char* args){
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
144 vf->config=config;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
145 vf->put_image=put_image;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
146 vf->priv=malloc(sizeof(struct vf_priv_s));
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
147 vf->priv->limit=24; // should be option
13233
3b85939d9f0a missing initialization of round
reimar
parents: 13205
diff changeset
148 vf->priv->round = 0;
13205
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
149 if(args) sscanf(args, "%d:%d",
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
150 &vf->priv->limit,
08bb01a9905a Adds rounding parameter for width and height values returned.
reimar
parents: 11261
diff changeset
151 &vf->priv->round);
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
152 return 1;
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
153 }
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
154
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
155 vf_info_t vf_info_cropdetect = {
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
156 "autodetect crop size",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
157 "cropdetect",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
158 "A'rpi",
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
159 "",
9593
e9a2af584986 Add the new -vf option wich is the same as vop in reverse order.
albeu
parents: 7472
diff changeset
160 open,
e9a2af584986 Add the new -vf option wich is the same as vop in reverse order.
albeu
parents: 7472
diff changeset
161 NULL
6061
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
162 };
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
163
5343ef6b8c23 new filter, to detect best crop size
arpi
parents:
diff changeset
164 //===========================================================================//