forked from Motion-Project/motion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg.c
1374 lines (1146 loc) · 42.4 KB
/
alg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* alg.c
*
* Detect changes in a video stream.
* Copyright 2001 by Jeroen Vreeken (pe1rxq@amsat.org)
* This software is distributed under the GNU public license version 2
* See also the file 'COPYING'.
*
*/
#include "motion.h"
#include "alg.h"
#ifdef __MMX__
#define HAVE_MMX
#include "mmx.h"
#endif
#define MAX2(x, y) ((x) > (y) ? (x) : (y))
#define MAX3(x, y, z) ((x) > (y) ? ((x) > (z) ? (x) : (z)) : ((y) > (z) ? (y) : (z)))
/**
* alg_locate_center_size
* Locates the center and size of the movement.
*/
void alg_locate_center_size(struct images *imgs, int width, int height, struct coord *cent)
{
unsigned char *out = imgs->img_motion.image_norm;
int *labels = imgs->labels;
int x, y, centc = 0, xdist = 0, ydist = 0;
cent->x = 0;
cent->y = 0;
cent->maxx = 0;
cent->maxy = 0;
cent->minx = width;
cent->miny = height;
/* If Labeling enabled - locate center of largest labelgroup. */
if (imgs->labelsize_max) {
/* Locate largest labelgroup */
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
if (*(labels++) & 32768) {
cent->x += x;
cent->y += y;
centc++;
}
}
}
} else {
/* Locate movement */
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
if (*(out++)) {
cent->x += x;
cent->y += y;
centc++;
}
}
}
}
if (centc) {
cent->x = cent->x / centc;
cent->y = cent->y / centc;
}
/* Now we find the size of the Motion. */
/* First reset pointers back to initial value. */
centc = 0;
labels = imgs->labels;
out = imgs->img_motion.image_norm;
/* If Labeling then we find the area around largest labelgroup instead. */
if (imgs->labelsize_max) {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
if (*(labels++) & 32768) {
if (x > cent->x)
xdist += x - cent->x;
else if (x < cent->x)
xdist += cent->x - x;
if (y > cent->y)
ydist += y - cent->y;
else if (y < cent->y)
ydist += cent->y - y;
centc++;
}
}
}
} else {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
if (*(out++)) {
if (x > cent->x)
xdist += x - cent->x;
else if (x < cent->x)
xdist += cent->x - x;
if (y > cent->y)
ydist += y - cent->y;
else if (y < cent->y)
ydist += cent->y - y;
centc++;
}
}
}
}
if (centc) {
cent->minx = cent->x - xdist / centc * 2;
cent->maxx = cent->x + xdist / centc * 2;
/*
* Make the box a little bigger in y direction to make sure the
* heads fit in so we multiply by 3 instead of 2 which seems to
* to work well in practical.
*/
cent->miny = cent->y - ydist / centc * 3;
cent->maxy = cent->y + ydist / centc * 2;
}
if (cent->maxx > width - 1)
cent->maxx = width - 1;
else if (cent->maxx < 0)
cent->maxx = 0;
if (cent->maxy > height - 1)
cent->maxy = height - 1;
else if (cent->maxy < 0)
cent->maxy = 0;
if (cent->minx > width - 1)
cent->minx = width - 1;
else if (cent->minx < 0)
cent->minx = 0;
if (cent->miny > height - 1)
cent->miny = height - 1;
else if (cent->miny < 0)
cent->miny = 0;
/* Align for better locate box handling */
cent->minx += cent->minx % 2;
cent->miny += cent->miny % 2;
cent->maxx -= cent->maxx % 2;
cent->maxy -= cent->maxy % 2;
cent->width = cent->maxx - cent->minx;
cent->height = cent->maxy - cent->miny;
/*
* We want to center Y coordinate to be the center of the action.
* The head of a person is important so we correct the cent.y coordinate
* to match the correction to include a persons head that we just did above.
*/
cent->y = (cent->miny + cent->maxy) / 2;
}
/**
* alg_draw_location
* Draws a box around the movement.
*/
void alg_draw_location(struct coord *cent, struct images *imgs, int width, unsigned char *new,
int style, int mode, int process_thisframe)
{
unsigned char *out = imgs->img_motion.image_norm;
int x, y;
out = imgs->img_motion.image_norm;
/* Debug image always gets a 'normal' box. */
if ((mode == LOCATE_BOTH) && process_thisframe) {
int width_miny = width * cent->miny;
int width_maxy = width * cent->maxy;
for (x = cent->minx; x <= cent->maxx; x++) {
int width_miny_x = x + width_miny;
int width_maxy_x = x + width_maxy;
out[width_miny_x] =~out[width_miny_x];
out[width_maxy_x] =~out[width_maxy_x];
}
for (y = cent->miny; y <= cent->maxy; y++) {
int width_minx_y = cent->minx + y * width;
int width_maxx_y = cent->maxx + y * width;
out[width_minx_y] =~out[width_minx_y];
out[width_maxx_y] =~out[width_maxx_y];
}
}
if (style == LOCATE_BOX) { /* Draw a box on normal images. */
int width_miny = width * cent->miny;
int width_maxy = width * cent->maxy;
for (x = cent->minx; x <= cent->maxx; x++) {
int width_miny_x = x + width_miny;
int width_maxy_x = x + width_maxy;
new[width_miny_x] =~new[width_miny_x];
new[width_maxy_x] =~new[width_maxy_x];
}
for (y = cent->miny; y <= cent->maxy; y++) {
int width_minx_y = cent->minx + y * width;
int width_maxx_y = cent->maxx + y * width;
new[width_minx_y] =~new[width_minx_y];
new[width_maxx_y] =~new[width_maxx_y];
}
} else if (style == LOCATE_CROSS) { /* Draw a cross on normal images. */
int centy = cent->y * width;
for (x = cent->x - 10; x <= cent->x + 10; x++) {
new[centy + x] =~new[centy + x];
out[centy + x] =~out[centy + x];
}
for (y = cent->y - 10; y <= cent->y + 10; y++) {
new[cent->x + y * width] =~new[cent->x + y * width];
out[cent->x + y * width] =~out[cent->x + y * width];
}
}
}
/**
* alg_draw_red_location
* Draws a RED box around the movement.
*/
void alg_draw_red_location(struct coord *cent, struct images *imgs, int width, unsigned char *new,
int style, int mode, int process_thisframe)
{
unsigned char *out = imgs->img_motion.image_norm;
unsigned char *new_u, *new_v;
int x, y, v, cwidth, cblock;
cwidth = width / 2;
cblock = imgs->motionsize / 4;
x = imgs->motionsize;
v = x + cblock;
out = imgs->img_motion.image_norm;
new_u = new + x;
new_v = new + v;
/* Debug image always gets a 'normal' box. */
if ((mode == LOCATE_BOTH) && process_thisframe) {
int width_miny = width * cent->miny;
int width_maxy = width * cent->maxy;
for (x = cent->minx; x <= cent->maxx; x++) {
int width_miny_x = x + width_miny;
int width_maxy_x = x + width_maxy;
out[width_miny_x] =~out[width_miny_x];
out[width_maxy_x] =~out[width_maxy_x];
}
for (y = cent->miny; y <= cent->maxy; y++) {
int width_minx_y = cent->minx + y * width;
int width_maxx_y = cent->maxx + y * width;
out[width_minx_y] =~out[width_minx_y];
out[width_maxx_y] =~out[width_maxx_y];
}
}
if (style == LOCATE_REDBOX) { /* Draw a red box on normal images. */
int width_miny = width * cent->miny;
int width_maxy = width * cent->maxy;
int cwidth_miny = cwidth * (cent->miny / 2);
int cwidth_maxy = cwidth * (cent->maxy / 2);
for (x = cent->minx + 2; x <= cent->maxx - 2; x += 2) {
int width_miny_x = x + width_miny;
int width_maxy_x = x + width_maxy;
int cwidth_miny_x = x / 2 + cwidth_miny;
int cwidth_maxy_x = x / 2 + cwidth_maxy;
new_u[cwidth_miny_x] = 128;
new_u[cwidth_maxy_x] = 128;
new_v[cwidth_miny_x] = 255;
new_v[cwidth_maxy_x] = 255;
new[width_miny_x] = 128;
new[width_maxy_x] = 128;
new[width_miny_x + 1] = 128;
new[width_maxy_x + 1] = 128;
new[width_miny_x + width] = 128;
new[width_maxy_x + width] = 128;
new[width_miny_x + 1 + width] = 128;
new[width_maxy_x + 1 + width] = 128;
}
for (y = cent->miny; y <= cent->maxy; y += 2) {
int width_minx_y = cent->minx + y * width;
int width_maxx_y = cent->maxx + y * width;
int cwidth_minx_y = (cent->minx / 2) + (y / 2) * cwidth;
int cwidth_maxx_y = (cent->maxx / 2) + (y / 2) * cwidth;
new_u[cwidth_minx_y] = 128;
new_u[cwidth_maxx_y] = 128;
new_v[cwidth_minx_y] = 255;
new_v[cwidth_maxx_y] = 255;
new[width_minx_y] = 128;
new[width_maxx_y] = 128;
new[width_minx_y + width] = 128;
new[width_maxx_y + width] = 128;
new[width_minx_y + 1] = 128;
new[width_maxx_y + 1] = 128;
new[width_minx_y + width + 1] = 128;
new[width_maxx_y + width + 1] = 128;
}
} else if (style == LOCATE_REDCROSS) { /* Draw a red cross on normal images. */
int cwidth_maxy = cwidth * (cent->y / 2);
for (x = cent->x - 10; x <= cent->x + 10; x += 2) {
int cwidth_maxy_x = x / 2 + cwidth_maxy;
new_u[cwidth_maxy_x] = 128;
new_v[cwidth_maxy_x] = 255;
}
for (y = cent->y - 10; y <= cent->y + 10; y += 2) {
int cwidth_minx_y = (cent->x / 2) + (y / 2) * cwidth;
new_u[cwidth_minx_y] = 128;
new_v[cwidth_minx_y] = 255;
}
}
}
#define NORM 100
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define DIFF(x, y) (ABS((x)-(y)))
#define NDIFF(x, y) (ABS(x) * NORM / (ABS(x) + 2 * DIFF(x, y)))
/**
* alg_noise_tune
*
*/
void alg_noise_tune(struct context *cnt, unsigned char *new)
{
struct images *imgs = &cnt->imgs;
int i;
unsigned char *ref = imgs->ref;
int diff, sum = 0, count = 0;
unsigned char *mask = imgs->mask;
unsigned char *smartmask = imgs->smartmask_final;
i = imgs->motionsize;
for (; i > 0; i--) {
diff = ABS(*ref - *new);
if (mask)
diff = ((diff * *mask++) / 255);
if (*smartmask) {
sum += diff + 1;
count++;
}
ref++;
new++;
smartmask++;
}
if (count > 3) /* Avoid divide by zero. */
sum /= count / 3;
/* 5: safe, 4: regular, 3: more sensitive */
cnt->noise = 4 + (cnt->noise + sum) / 2;
}
/**
* alg_threshold_tune
*
*/
void alg_threshold_tune(struct context *cnt, int diffs, int motion)
{
int i;
int sum = 0, top = diffs;
if (!diffs)
return;
if (motion)
diffs = cnt->threshold / 4;
for (i = 0; i < THRESHOLD_TUNE_LENGTH - 1; i++) {
sum += cnt->diffs_last[i];
if (cnt->diffs_last[i + 1] && !motion)
cnt->diffs_last[i] = cnt->diffs_last[i + 1];
else
cnt->diffs_last[i] = cnt->threshold / 4;
if (cnt->diffs_last[i] > top)
top = cnt->diffs_last[i];
}
sum += cnt->diffs_last[i];
cnt->diffs_last[i] = diffs;
sum /= THRESHOLD_TUNE_LENGTH / 4;
if (sum < top * 2)
sum = top * 2;
if (sum < cnt->conf.threshold)
cnt->threshold = (cnt->threshold + sum) / 2;
}
/*
* Labeling by Joerg Weber. Based on an idea from Hubert Mara.
* Floodfill enhanced by Ian McConnel based on code from
* http://www.acm.org/pubs/tog/GraphicsGems/
* http://www.codeproject.com/gdi/QuickFill.asp
* Filled horizontal segment of scanline y for xl <= x <= xr.
* Parent segment was on line y - dy. dy = 1 or -1
*/
#define MAXS 10000 /* max depth of stack */
#define PUSH(Y, XL, XR, DY) /* push new segment on stack */ \
if (sp<stack+MAXS && Y+(DY) >= 0 && Y+(DY) < height) \
{sp->y = Y; sp->xl = XL; sp->xr = XR; sp->dy = DY; sp++;}
#define POP(Y, XL, XR, DY) /* pop segment off stack */ \
{sp--; Y = sp->y+(DY = sp->dy); XL = sp->xl; XR = sp->xr;}
typedef struct {
short y, xl, xr, dy;
} Segment;
/**
* iflood
*
*/
static int iflood(int x, int y, int width, int height,
unsigned char *out, int *labels, int newvalue, int oldvalue)
{
int l, x1, x2, dy;
Segment stack[MAXS], *sp = stack; /* Stack of filled segments. */
int count = 0;
if (x < 0 || x >= width || y < 0 || y >= height)
return 0;
PUSH(y, x, x, 1); /* Needed in some cases. */
PUSH(y+1, x, x, -1); /* Seed segment (popped 1st). */
while (sp > stack) {
/* Pop segment off stack and fill a neighboring scan line. */
POP(y, x1, x2, dy);
/*
* Segment of scan line y-dy for x1<=x<=x2 was previously filled,
* now explore adjacent pixels in scan line y
*/
for (x = x1; x >= 0 && out[y * width + x] != 0 && labels[y * width + x] == oldvalue; x--) {
labels[y * width + x] = newvalue;
count++;
}
if (x >= x1)
goto skip;
l = x + 1;
if (l < x1)
PUSH(y, l, x1 - 1, -dy); /* Leak on left? */
x = x1 + 1;
do {
for (; x < width && out[y * width + x] != 0 && labels[y * width + x] == oldvalue; x++) {
labels[y * width + x] = newvalue;
count++;
}
PUSH(y, l, x - 1, dy);
if (x > x2 + 1)
PUSH(y, x2 + 1, x - 1, -dy); /* Leak on right? */
skip:
for (x++; x <= x2 && !(out[y * width + x] != 0 && labels[y * width + x] == oldvalue); x++);
l = x;
} while (x <= x2);
}
return count;
}
/**
* alg_labeling
*
*/
static int alg_labeling(struct context *cnt)
{
struct images *imgs = &cnt->imgs;
unsigned char *out = imgs->img_motion.image_norm;
int *labels = imgs->labels;
int ix, iy, pixelpos;
int width = imgs->width;
int height = imgs->height;
int labelsize = 0;
int current_label = 2;
/* Keep track of the area just under the threshold. */
int max_under = 0;
cnt->current_image->total_labels = 0;
imgs->labelsize_max = 0;
/* ALL labels above threshold are counted as labelgroup. */
imgs->labelgroup_max = 0;
imgs->labels_above = 0;
/* Init: 0 means no label set / not checked. */
memset(labels, 0, width * height * sizeof(*labels));
pixelpos = 0;
for (iy = 0; iy < height - 1; iy++) {
for (ix = 0; ix < width - 1; ix++, pixelpos++) {
/* No motion - no label */
if (out[pixelpos] == 0) {
labels[pixelpos] = 1;
continue;
}
/* Already visited by iflood */
if (labels[pixelpos] > 0)
continue;
labelsize = iflood(ix, iy, width, height, out, labels, current_label, 0);
if (labelsize > 0) {
//MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO, "Label: %i (%i) Size: %i (%i,%i)",
// current_label, cnt->current_image->total_labels,
// labelsize, ix, iy);
/* Label above threshold? Mark it again (add 32768 to labelnumber). */
if (labelsize > cnt->threshold) {
labelsize = iflood(ix, iy, width, height, out, labels, current_label + 32768, current_label);
imgs->labelgroup_max += labelsize;
imgs->labels_above++;
} else if(max_under < labelsize)
max_under = labelsize;
if (imgs->labelsize_max < labelsize) {
imgs->labelsize_max = labelsize;
imgs->largest_label = current_label;
}
cnt->current_image->total_labels++;
current_label++;
}
}
pixelpos++; /* Compensate for ix < width - 1 */
}
//MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO, "%i Labels found. Largest connected Area: %i Pixel(s). "
// "Largest Label: %i", imgs->largest_label, imgs->labelsize_max,
// cnt->current_image->total_labels);
/* Return group of significant labels or if that's none, the next largest
* group (which is under the threshold, but especially for setup gives an
* idea how close it was).
*/
return imgs->labelgroup_max ? imgs->labelgroup_max : max_under;
}
/**
* dilate9
* Dilates a 3x3 box.
*/
static int dilate9(unsigned char *img, int width, int height, void *buffer)
{
/*
* - row1, row2 and row3 represent lines in the temporary buffer.
* - Window is a sliding window containing max values of the columns
* in the 3x3 matrix.
* - width is an index into the sliding window (this is faster than
* doing modulo 3 on i).
* - blob keeps the current max value.
*/
int y, i, sum = 0, widx;
unsigned char *row1, *row2, *row3, *rowTemp,*yp;
unsigned char window[3], blob, latest;
/* Set up row pointers in the temporary buffer. */
row1 = buffer;
row2 = row1 + width;
row3 = row2 + width;
/* Init rows 2 and 3. */
memset(row2, 0, width);
memcpy(row3, img, width);
/* Pointer to the current row in img. */
yp = img;
for (y = 0; y < height; y++) {
/* Move down one step; row 1 becomes the previous row 2 and so on. */
rowTemp = row1;
row1 = row2;
row2 = row3;
row3 = rowTemp;
/* If we're at the last row, fill with zeros, otherwise copy from img. */
if (y == height - 1)
memset(row3, 0, width);
else
memcpy(row3, yp+width, width);
/* Init slots 0 and 1 in the moving window. */
window[0] = MAX3(row1[0], row2[0], row3[0]);
window[1] = MAX3(row1[1], row2[1], row3[1]);
/* Init blob to the current max, and set window index. */
blob = MAX2(window[0], window[1]);
widx = 2;
/*
* Iterate over the current row; index i is off by one to eliminate
* a lot of +1es in the loop.
*/
for (i = 2; i <= width - 1; i++) {
/* Get the max value of the next column in the 3x3 matrix. */
latest = window[widx] = MAX3(row1[i], row2[i], row3[i]);
/*
* If the value is larger than the current max, use it. Otherwise,
* calculate a new max (because the new value may not be the max.
*/
if (latest >= blob)
blob = latest;
else
blob = MAX3(window[0], window[1], window[2]);
/* Write the max value (blob) to the image. */
if (blob != 0) {
*(yp + i - 1) = blob;
sum++;
}
/* Wrap around the window index if necessary. */
if (++widx == 3)
widx = 0;
}
/* Store zeros in the vertical sides. */
*yp = *(yp + width - 1) = 0;
yp += width;
}
return sum;
}
/**
* dilate5
* Dilates a + shape.
*/
static int dilate5(unsigned char *img, int width, int height, void *buffer)
{
/*
* - row1, row2 and row3 represent lines in the temporary buffer.
* - mem holds the max value of the overlapping part of two + shapes.
*/
int y, i, sum = 0;
unsigned char *row1, *row2, *row3, *rowTemp, *yp;
unsigned char blob, mem, latest;
/* Set up row pointers in the temporary buffer. */
row1 = buffer;
row2 = row1 + width;
row3 = row2 + width;
/* Init rows 2 and 3. */
memset(row2, 0, width);
memcpy(row3, img, width);
/* Pointer to the current row in img. */
yp = img;
for (y = 0; y < height; y++) {
/* Move down one step; row 1 becomes the previous row 2 and so on. */
rowTemp = row1;
row1 = row2;
row2 = row3;
row3 = rowTemp;
/* If we're at the last row, fill with zeros, otherwise copy from img. */
if (y == height - 1)
memset(row3, 0, width);
else
memcpy(row3, yp + width, width);
/* Init mem and set blob to force an evaluation of the entire + shape. */
mem = MAX2(row2[0], row2[1]);
blob = 1; /* dummy value, must be > 0 */
for (i = 1; i < width - 1; i++) {
/* Get the max value of the "right edge" of the + shape. */
latest = MAX3(row1[i], row2[i + 1], row3[i]);
if (blob == 0) {
/* In case the last blob is zero, only latest matters. */
blob = latest;
mem = row2[i + 1];
} else {
/* Otherwise, we have to check both latest and mem. */
blob = MAX2(mem, latest);
mem = MAX2(row2[i], row2[i + 1]);
}
/* Write the max value (blob) to the image. */
if (blob != 0) {
*(yp + i) = blob;
sum++;
}
}
/* Store zeros in the vertical sides. */
*yp = *(yp + width - 1) = 0;
yp += width;
}
return sum;
}
/**
* erode9
* Erodes a 3x3 box.
*/
static int erode9(unsigned char *img, int width, int height, void *buffer, unsigned char flag)
{
int y, i, sum = 0;
char *Row1,*Row2,*Row3;
Row1 = buffer;
Row2 = Row1 + width;
Row3 = Row1 + 2 * width;
memset(Row2, flag, width);
memcpy(Row3, img, width);
for (y = 0; y < height; y++) {
memcpy(Row1, Row2, width);
memcpy(Row2, Row3, width);
if (y == height-1)
memset(Row3, flag, width);
else
memcpy(Row3, img + (y+1) * width, width);
for (i = width - 2; i >= 1; i--) {
if (Row1[i - 1] == 0 ||
Row1[i] == 0 ||
Row1[i + 1] == 0 ||
Row2[i - 1] == 0 ||
Row2[i] == 0 ||
Row2[i + 1] == 0 ||
Row3[i - 1] == 0 ||
Row3[i] == 0 ||
Row3[i + 1] == 0)
img[y * width + i] = 0;
else
sum++;
}
img[y * width] = img[y * width + width - 1] = flag;
}
return sum;
}
/**
* erode5
* Erodes in a + shape.
*/
static int erode5(unsigned char *img, int width, int height, void *buffer, unsigned char flag)
{
int y, i, sum = 0;
char *Row1,*Row2,*Row3;
Row1 = buffer;
Row2 = Row1 + width;
Row3 = Row1 + 2 * width;
memset(Row2, flag, width);
memcpy(Row3, img, width);
for (y = 0; y < height; y++) {
memcpy(Row1, Row2, width);
memcpy(Row2, Row3, width);
if (y == height-1)
memset(Row3, flag, width);
else
memcpy(Row3, img + (y + 1) * width, width);
for (i = width - 2; i >= 1; i--) {
if (Row1[i] == 0 ||
Row2[i - 1] == 0 ||
Row2[i] == 0 ||
Row2[i + 1] == 0 ||
Row3[i] == 0)
img[y * width + i] = 0;
else
sum++;
}
img[y * width] = img[y * width + width - 1] = flag;
}
return sum;
}
/**
* alg_despeckle
* Despeckling routine to remove noisy detections.
*/
int alg_despeckle(struct context *cnt, int olddiffs)
{
int diffs = 0;
unsigned char *out = cnt->imgs.img_motion.image_norm;
int width = cnt->imgs.width;
int height = cnt->imgs.height;
int done = 0, i, len = strlen(cnt->conf.despeckle_filter);
unsigned char *common_buffer = cnt->imgs.common_buffer;
for (i = 0; i < len; i++) {
switch (cnt->conf.despeckle_filter[i]) {
case 'E':
if ((diffs = erode9(out, width, height, common_buffer, 0)) == 0)
i = len;
done = 1;
break;
case 'e':
if ((diffs = erode5(out, width, height, common_buffer, 0)) == 0)
i = len;
done = 1;
break;
case 'D':
diffs = dilate9(out, width, height, common_buffer);
done = 1;
break;
case 'd':
diffs = dilate5(out, width, height, common_buffer);
done = 1;
break;
/* No further despeckle after labeling! */
case 'l':
diffs = alg_labeling(cnt);
i = len;
done = 2;
break;
}
}
/* If conf.despeckle_filter contains any valid action EeDdl */
if (done) {
if (done != 2)
cnt->imgs.labelsize_max = 0; // Disable Labeling
return diffs;
} else {
cnt->imgs.labelsize_max = 0; // Disable Labeling
}
return olddiffs;
}
/**
* alg_tune_smartmask
* Generates actual smartmask. Calculate sensitivity based on motion.
*/
void alg_tune_smartmask(struct context *cnt)
{
int i, diff;
int motionsize = cnt->imgs.motionsize;
unsigned char *smartmask = cnt->imgs.smartmask;
unsigned char *smartmask_final = cnt->imgs.smartmask_final;
int *smartmask_buffer = cnt->imgs.smartmask_buffer;
int sensitivity = cnt->lastrate * (11 - cnt->smartmask_speed);
for (i = 0; i < motionsize; i++) {
/* Decrease smart_mask sensitivity every 5*speed seconds only. */
if (smartmask[i] > 0)
smartmask[i]--;
/* Increase smart_mask sensitivity based on the buffered values. */
diff = smartmask_buffer[i]/sensitivity;
if (diff) {
if (smartmask[i] <= diff + 80)
smartmask[i] += diff;
else
smartmask[i] = 80;
smartmask_buffer[i] %= sensitivity;
}
/* Transfer raw mask to the final stage when above trigger value. */
if (smartmask[i] > 20)
smartmask_final[i] = 0;
else
smartmask_final[i] = 255;
}
/* Further expansion (here:erode due to inverted logic!) of the mask. */
diff = erode9(smartmask_final, cnt->imgs.width, cnt->imgs.height,
cnt->imgs.common_buffer, 255);
diff = erode5(smartmask_final, cnt->imgs.width, cnt->imgs.height,
cnt->imgs.common_buffer, 255);
}
/* Increment for *smartmask_buffer in alg_diff_standard. */
#define SMARTMASK_SENSITIVITY_INCR 5
/**
* alg_diff_standard
*
*/
int alg_diff_standard(struct context *cnt, unsigned char *new)
{
struct images *imgs = &cnt->imgs;
int i, diffs = 0;
int noise = cnt->noise;
int smartmask_speed = cnt->smartmask_speed;
unsigned char *ref = imgs->ref;
unsigned char *out = imgs->img_motion.image_norm;
unsigned char *mask = imgs->mask;
unsigned char *smartmask_final = imgs->smartmask_final;
int *smartmask_buffer = imgs->smartmask_buffer;
#ifdef HAVE_MMX
mmx_t mmtemp; /* Used for transferring to/from memory. */
int unload; /* Counter for unloading diff counts. */
#endif
i = imgs->motionsize;
memset(out + i, 128, i / 2); /* Motion pictures are now b/w i.o. green */
/*
* Keeping this memset in the MMX case when zeroes are necessarily
* written anyway seems to be beneficial in terms of speed. Perhaps a
* cache thing?
*/
memset(out, 0, i);
#ifdef HAVE_MMX
/*
* NOTE: The Pentium has two instruction pipes: U and V. I have grouped MMX
* instructions in pairs according to how I think they will be scheduled in
* the U and V pipes. Due to pairing constraints, the V pipe will sometimes
* be empty (for example, memory access always goes into the U pipe).
*
* The following MMX registers are kept throughout the loop:
* mm5 - 8 separate diff counters (unloaded periodically)
* mm6 - mask: 00ff 00ff 00ff 00ff
* mm7 - noise level as 8 packed bytes
*
* -- Per Jonsson
*/
/*
* To avoid a div, we work with differences multiplied by 255 in the
* default case and *mask otherwise. Thus, the limit to compare with is
* 255 * (noise + 1) - 1).
*/
mmtemp.uw[0] = mmtemp.uw[1] = mmtemp.uw[2] = mmtemp.uw[3] =
(unsigned short)(noise * 255 + 254);
/*
* Reset mm5 to zero, set the mm6 mask, and store the multiplied noise
* level as four words in mm7.
*/
movq_m2r(mmtemp, mm7); /* U */
pcmpeqb_r2r(mm6, mm6); /* V */
pxor_r2r(mm5, mm5); /* U */
psrlw_i2r(8, mm6); /* V */
/*
* We must unload mm5 every 255th round, because the diffs accumulate
* in each packed byte, which can hold at most 255 diffs before it
* gets saturated.
*/
unload = 255;
for (; i > 7; i -= 8) {