9 #include "../../cinelerra/mediadb.inc"
15 #define SFRM_SZ (SWIDTH*SHEIGHT)
18 mean(uint8_t *dat, int n)
21 for( int i=0; i<n; ++i, ++dat ) s += *dat;
26 variance(uint8_t *dat, double m, int n)
29 for( int i=0; i<n; ++i, ++dat ) {
33 return (ss - s*s / n) / (n-1);
37 std_dev(uint8_t *dat, double m, int n)
39 return sqrt(variance(dat,m,n));
43 center(uint8_t *dat, int n, int stride)
46 for( int i=0; i<n; dat+=stride ) { s += *dat; ss += ++i * *dat; }
47 return s > 0 ? (double)ss / s : n / 2.;
51 centroid(uint8_t *dat, int w, int h, double *xx, double *yy)
55 for( int i=h; --i>=0; dp+=w ) x += center(dp, w, 1);
56 for( int i=w; --i>=0; ) y += center(dat+i, h, w);
57 *xx = x / h; *yy = y / w;
61 static inline int clip(int v, int mn, int mx)
63 return v<mn ? mn : v>mx ? mx : v;
66 void show_diff(uint8_t *kp0, uint8_t *kp1)
68 int w=SWIDTH, h=SHEIGHT;
69 uint8_t tgt[3*SFRM_SZ];
70 uint8_t *ap = tgt, *bp = tgt+1*SFRM_SZ, *dp = tgt+2*SFRM_SZ;
71 memcpy(ap, kp0, SFRM_SZ); memcpy(bp, kp1, SFRM_SZ);
72 for( int i=SFRM_SZ; --i>=0; ++dp,++ap,++bp ) *dp = clip(*ap-*bp+128, 0 ,255);
73 FILE *fp = popen("xv -geom 600x1125 -","w");
74 fprintf(fp,"P5\n%d %d\n255\n",w,3*h);
79 int64_t compare(uint8_t *ap, uint8_t *bp, int sz)
82 for( int i=sz; --i>=0; ++ap,++bp ) v += fabs(*ap-*bp);
86 double fmean, fsd, fcx, fcy;
88 int video_compares = 0, frame_compares = 0;
90 int64_t diff(uint8_t *a, uint8_t *b, int w, int h, int dx, int dy, int bias)
92 int axofs = dx < 0 ? 0 : dx;
93 int ayofs = w * (dy < 0 ? 0 : dy);
94 int aofs = ayofs + axofs;
95 int bxofs = dx < 0 ? -dx : 0;
96 int byofs = w * (dy < 0 ? -dy : 0);
97 int bofs = byofs + bxofs;
98 uint8_t *ap = a + aofs, *bp = b + bofs;
99 int ierr = 0, ww = w-abs(dx), hh = h-abs(dy);
100 for( int y=hh; --y>=0; ap+=w, bp+=w ) {
102 for( int x=ww; --x>=0; ++a, ++b ) { ierr += abs(*a-bias - *b); }
108 compare(Db::ObjectLoc &loc)
111 Video_frameLoc &frame = *(Video_frameLoc*)&loc;
112 double dm = frame.Frame_mean()-fmean;
113 if( fabs(dm) > MEDIA_MEAN_ERRLMT ) return LONG_MAX;
114 double ds = frame.Frame_std_dev()-fsd;
115 if( fabs(ds) > MEDIA_STDDEV_ERRLMT ) return LONG_MAX;
116 double dx = frame.Frame_cx()-fcx;
117 if( fabs(dx) > MEDIA_XCENTER_ERRLMT ) return LONG_MAX;
118 double dy = frame.Frame_cy()-fcy;
119 if( fabs(dy) > MEDIA_YCENTER_ERRLMT ) return LONG_MAX;
120 uint8_t *dat = frame._Frame_data();
121 //int64_t err = cmpr(frm, dat, SFRM_SZ);
122 int64_t err = diff(frm, dat, SWIDTH, SHEIGHT, lround(dx), lround(dy), lround(dm));
123 //show_diff(frm, frame.Frame_data());
129 int main(int ac, char **av)
131 int ret; setbuf(stdout,0);
134 //db.access(av[1], 34543, 0);
135 if( !db.opened() || db.error() ) exit(1);
137 FILE *fp = fopen(av[2],"r");
138 for( int i=3; --i>=0; ) {
139 for( int ch=fgetc(fp); ch>=0 && ch!='\n'; ch=fgetc(fp) );
142 int w = SWIDTH, h = SHEIGHT;
143 fread(frm,1,SFRM_SZ,fp);
145 fmean = mean(frm, SFRM_SZ);
146 fsd = std_dev(frm, fmean, SFRM_SZ);
147 centroid(frm, w, h, &fcx, &fcy);
149 if( (ret = Video_frameLoc::ikey_Frame_weight(db.video_frame, fkey).Locate()) != 0 ) {
150 printf(" not found, ret = %d\n",ret);
153 uint32_t lid = db.video_frame.id(), lfid = lid;
154 printf("frm %d mean %f, std_dev %f, cxy %f+%f = %f\n", lid, fmean, fsd, fcx, fcy, fkey);
155 int64_t lerr = compare(db.video_frame);
156 int64_t pixlmt = SFRM_SZ*errlmt;
158 printf("+ 0frame %d / %f\n", db.video_frame.id(),(double)lerr/SFRM_SZ);
159 //show_diff(frm, db.video_frame._Frame_data());
161 Video_frameLoc prev(db.video_frame), next(db.video_frame);
162 Video_frameLoc::rkey_Frame_weight prev_key(prev), next_key(next);
163 Db::pgRef next_loc; next_key.NextLoc(next_loc);
165 for( int i=1; i<=dist; ++i ) {
166 if( !prev_key.Locate(Db::keyLT) ) {
167 int64_t err = compare(prev);
168 uint32_t prev_id = prev.id();
169 if( err < pixlmt && lfid != prev_id )
170 printf("-%3dframe %d / %f\n",i, prev_id,(double)err/SFRM_SZ);
171 if( lerr > err ) { lerr = err; lid = prev_id; }
173 if( !next_key.Next(next_loc) ) {
174 int64_t err = compare(next);
175 uint32_t next_id = next.id();
176 if( err < pixlmt && lfid != next_id )
177 printf("+%3dframe %d / %f\n",i, next_id,(double)err/SFRM_SZ);
178 if( lerr > err ) { lerr = err; lid = next_id; }
181 printf("%c frame %d / %f, compares %d/%d = %f\n",
182 lerr<pixlmt ? '*' : '#', lid, (double)lerr/SFRM_SZ,
183 frame_compares, video_compares, (double)frame_compares/video_compares);