forked from billw2/pikrellcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion.c
1748 lines (1573 loc) · 49.2 KB
/
motion.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
/* PiKrellCam
|
| Copyright (C) 2015-2020 Bill Wilson [email protected]
|
| PiKrellCam is free software: you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by
| the Free Software Foundation, either version 3 of the License, or
| (at your option) any later version.
|
| PiKrellCam is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
| or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
| License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program. If not, see http://www.gnu.org/licenses/
|
| This file is part of PiKrellCam.
*/
#include "pikrellcam.h"
#include <dirent.h>
#define EXPMA_SMOOTHING 0.01
#define SMALL_OBJECT_COUNT 15
#define IN_BOX_COUNT_MIN (2 * cvec->mag2_count)
MotionFrame motion_frame;
static CompositeVector zero_cvec;
static void
get_composite_vector(MotionFrame *mf, MotionRegion *mreg)
{
CompositeVector *cvec, tvec;
MotionVector *mv;
Area *area;
char *mt, *mtb;
int x, y, mag2, mb_index, mvmag2, dot, cos2,
x0, y0, x1, y1;
int16_t *pm, *pp, *pn;
cvec = &mreg->vector;
*cvec = zero_cvec;
tvec = zero_cvec;
mreg->reject_count = 0;
mreg->sparkle_count = 0;
/* Don't look at frame perimeter blocks except when excluding sparkles.
| (They have limited vector direction).
*/
if ((y0 = mreg->y) == 0)
y0 = 1;
if ((y1 = mreg->y + mreg->dy) >= mf->height)
y1 = mf->height - 1;
if ((x0 = mreg->x) == 0)
x0 = 1;
if ((x1 = mreg->x + mreg->dx) >= mf->width)
x1 = mf->width - 1;
/* First pass, filter out any vector < mag2_limit
*/
for (y = y0; y < y1; ++y)
{
for (x = x0; x < x1; ++x)
{
mb_index = mf->width * y + x;
mv = &mf->vectors[mb_index];
mag2 = mv->vx * mv->vx + mv->vy * mv->vy;
if (mag2 >= mf->mag2_limit)
{
*(mf->trigger + mb_index) = mag2;
}
}
}
/* Remove isolated sparkles & build the initial composite vector which
| will then consist of motion vector clustering of at least count 2.
| Camera can produce large sparkle counts during dim light (dusk/dawn).
*/
for (y = y0; y < y1; ++y)
{
for (x = x0; x < x1; ++x)
{
pm = mf->trigger + mf->width * y + x;
pp = pm - mf->width;
pn = pm + mf->width;
if ( *pm
&& !*(pm - 1) && !*(pm + 1)
&& !*(pp - 1) && !*pp && !*(pp + 1)
&& !*(pn - 1) && !*pn && !*(pn + 1)
)
{
*pm = 1; /* mag2 value becomes a sparkle flag */
mf->sparkle_count += 1;
mreg->sparkle_count += 1;
}
if (*pm > 1)
{
mv = &mf->vectors[mf->width * y + x];
tvec.mag2_count += 1;
tvec.vx += mv->vx;
tvec.vy += mv->vy;
tvec.x += x;
tvec.y += y;
mf->any_count += 1;
}
}
}
/* If sparkle noise, override configured limit_count (for dusk/dawn times).
| In regions with no motion, this reduces chances of a spurious reject.
| In regions with motion, this tries to raise limit_count above the noise
| background, but sensitivity is reduced.
*/
x = SMALL_OBJECT_COUNT;
if (mf->mag2_limit_count < x)
{
mf->mag2_limit_count += 2 * mreg->sparkle_count / 3;
if (mf->mag2_limit_count > x)
mf->mag2_limit_count = x;
}
/* If we are left with enough counts for a composite vector, filter out
| motion vectors not pointing in the composite directon.
| Vectors of sufficient mag2 but not pointing in the right direction
| will be rejects and their count can be large for noisy frames.
| Dot product to allow a spread,
| (avoiding sqrt() to get magnitude and scale by 100 for integer math):
|
| cos(a) = (v1 dot v2) / mag(v1) * mag(v2)) # cos(25) = 0.906
| 100 * cos(25)^2 = 82 = 100 * (v1 dot v2)^2 / mag(v1)^2 * mag(v2)^2)
*/
if (tvec.mag2_count >= mf->mag2_limit_count)
{
tvec.vx /= tvec.mag2_count;
tvec.vy /= tvec.mag2_count;
tvec.mag2 = tvec.vx * tvec.vx + tvec.vy * tvec.vy;
for (y = y0; y < y1; ++y)
{
for (x = x0; x < x1; ++x)
{
mb_index = mf->width * y + x;
mvmag2 = *(mf->trigger + mb_index);
if (mvmag2 <= 1)
continue;
mv = &mf->vectors[mb_index];
dot = tvec.vx * mv->vx + tvec.vy * mv->vy;
if ( dot > 0 /* angle at least < 90 deg */
&& tvec.mag2 > 0
&& (cos2 = 100 * dot * dot / (tvec.mag2 * mvmag2)) >= 82
)
{
cvec->mag2_count += 1;
cvec->vx += mv->vx;
cvec->vy += mv->vy;
cvec->x += x;
cvec->y += y;
area = &mf->motion_area;
if (area->x0 == 0 || area->x0 > x)
area->x0 = x;
if (area->x1 < x)
area->x1 = x;
if (area->y0 == 0 || area->y0 > y)
area->y0 = y;
if (area->y1 < y)
area->y1 = y;
}
else
{
*(mf->trigger + mf->width * y + x) = 2; /* reject flag */
mf->reject_count += 1;
mreg->reject_count += 1;
}
}
}
}
/* Now every vector in mag2_count has magnitude >= mag2_limit and points
| with some spread in the same direction. If there is enough of them,
| we have a final composite vector. But look at the motion vector
| distribution/concentration within the region to filter for final
| motion detection.
*/
mreg->motion = MOTION_NONE;
if (cvec->mag2_count >= mf->mag2_limit_count)
{
cvec->x /= cvec->mag2_count;
cvec->y /= cvec->mag2_count;
cvec->vx /= cvec->mag2_count;
cvec->vy /= cvec->mag2_count;
cvec->mag2 = cvec->vx * cvec->vx + cvec->vy * cvec->vy;
/* Set a vertical flag. The idea was vertical filtering would be
| usefull for rain, but it turns out it's just as likely the
| vectors will match in some random direction and reject counts
| are a better rain filter, but look again later.
*/
cvec->vertical =
(cvec->vy * cvec->vy > 20 * cvec->vx * cvec->vx) ? TRUE : FALSE;
if (cvec->vertical)
mf->vertical_count += 1;
/* Get a box that will hold at least 2x mag2_count vectors
| and count the vectors within the box.
*/
for (cvec->box_w = 4, cvec->box_h = 4;
cvec->box_w * cvec->box_h <= IN_BOX_COUNT_MIN; )
{
if (cvec->box_h <= cvec->box_w)
cvec->box_h += 2;
else
cvec->box_w += 2;
}
cvec->in_box_count = 0;
cvec->in_box_rejects = 0;
for (y = cvec->y - cvec->box_h / 2; y <= cvec->y + cvec->box_h / 2; ++y)
{
for (x = cvec->x - cvec->box_w / 2; x <= cvec->x + cvec->box_w / 2; ++x)
{
int trig = *(mf->trigger + mf->width * y + x);
if (trig > 2)
cvec->in_box_count += 1;
else if (trig == 2)
cvec->in_box_rejects += 1;
}
}
/* Filter out smaller fast moving objects which can be fast bird fly
| bys or close flying insects.
| For smaller objects, always enforce reject_count rejections
| (filters noisy frames: rain, camera burps).
| Comparison ratios here are empirical from looking at
| motion-debug output wrt drawn OSD motion vector frames.
*/
if ( !cvec->vertical
|| !pikrellcam.motion_vertical_filter
)
{
if (cvec->mag2_count < SMALL_OBJECT_COUNT)
{
if ( cvec->in_box_count > cvec->mag2_count * 8 / 10
&& cvec->mag2 < 5 * mf->mag2_limit
&& mreg->reject_count < cvec->mag2_count / 2
)
mreg->motion = MOTION_TYPE_DIR_SMALL;
}
else
if ( cvec->in_box_count > cvec->mag2_count * 7 / 10
|| ( cvec->in_box_count > cvec->mag2_count * 5 / 10
&& mreg->reject_count < cvec->mag2_count * 4 / 10
)
)
mreg->motion = MOTION_TYPE_DIR_NORMAL;
}
/* Flag it if a density of count+rejects is sufficient for a burst
| detect (which still has to pass a total count check).
*/
if (mf->any_count_expma < 100.0)
{
if (cvec->in_box_count + cvec->in_box_rejects > 4 * IN_BOX_COUNT_MIN / 10)
mreg->motion |= MOTION_TYPE_BURST_DENSITY;
}
else if (mf->any_count_expma < 300.0)
{
if (cvec->in_box_count + cvec->in_box_rejects > 7 * IN_BOX_COUNT_MIN / 10)
mreg->motion |= MOTION_TYPE_BURST_DENSITY;
}
else
if (cvec->in_box_count + cvec->in_box_rejects > 9 * IN_BOX_COUNT_MIN / 10)
mreg->motion |= MOTION_TYPE_BURST_DENSITY;
if (pikrellcam.verbose_motion)
{
printf(
"cvec[%d]: x,y(%d,%d) dx,dy(%d,%d) mag2,count,lim(%d,%d,%d) rej,spkl,vert(%d,%d,%d)\n",
mreg->region_number,
cvec->x, cvec->y, cvec->vx, cvec->vy, cvec->mag2,
cvec->mag2_count, mf->mag2_limit_count, mreg->reject_count,
mreg->sparkle_count, cvec->vertical);
mt = "----";
mtb = "------";
if (mreg->motion & MOTION_TYPE_DIR_SMALL)
mt = "dirS";
else if (mreg->motion & MOTION_TYPE_DIR_NORMAL)
mt = "dirN";
if (mreg->motion & MOTION_TYPE_BURST_DENSITY)
mtb = "burstD";
/* in_box density is what counts are compared to, not in_size
*/
printf(
" box:%dx%d in_box[cnt:%d rej:%d] density[dir:%.1f burst:%.1f] **motion[%s,%s]\n",
cvec->box_w, cvec->box_h,
cvec->in_box_count, cvec->in_box_rejects,
(float) cvec->in_box_count / (float) cvec->mag2_count,
(float) (cvec->in_box_count + cvec->in_box_rejects)
/ (float) IN_BOX_COUNT_MIN,
mt, mtb);
}
}
else
*cvec = zero_cvec;
}
static void
motion_stats_write(VideoCircularBuffer *vcb, MotionFrame *mf)
{
CompositeVector *frame_vec = &mf->frame_vector;
if (!vcb->motion_stats_file)
return;
if (vcb->motion_stats_do_header)
fprintf(vcb->motion_stats_file,
"time, x, y, dx, dy, magnitude, count\n"
"# width %d height %d\n",
mf->width, mf->height);
vcb->motion_stats_do_header = FALSE;
fprintf(vcb->motion_stats_file,
"%6.3f, %3d, %3d, %3d, %3d, %3.0f, %4d\n",
(float) vcb->frame_count / (float) pikrellcam.camera_adjust.video_fps,
frame_vec->x, frame_vec->y, -frame_vec->vx, -frame_vec->vy,
sqrt((float)frame_vec->mag2), frame_vec->mag2_count);
}
void
motion_events_write(MotionFrame *mf, int type, float detect_time)
{
static FILE *f;
CompositeVector *cvec, *frame_vec = &mf->frame_vector;
MotionRegion *mreg;
PresetPosition *pos;
PresetSettings *settings = NULL;
SList *mrlist;
int burst, i, pan, tilt;
boolean dir_motion;
if (type & MOTION_EVENTS_HEADER)
{
f = fopen(pikrellcam.motion_events_filename, "w");
fprintf(f, "<header>\n");
fprintf(f, "video %s\n", pikrellcam.video_pathname);
fprintf(f, "frame %d %d\n", mf->width, mf->height);
servo_get_position(&pan, &tilt);
pos = preset_find_at_position(pan, tilt);
if (pos)
{
fprintf(f, "preset %d %d\n", pikrellcam.preset_position_index + 1,
pos ? pos->settings_index + 1 : 1);
settings = (PresetSettings *)slist_nth_data(pos->settings_list,
pos->settings_index);
if (settings)
{
fprintf(f, "magnitude_limit %d\n", settings->mag_limit);
fprintf(f, "magnitude_count %d\n", settings->mag_limit_count);
fprintf(f, "burst_count %d\n", settings->burst_count);
fprintf(f, "burst_frames %d\n", settings->burst_frames);
}
}
if (pikrellcam.have_servos)
{
fprintf(f, "pan %d\n", pan);
fprintf(f, "tilt %d\n", tilt);
}
fprintf(f, "</header>\n");
}
// if (f && ((vcb->state & VCB_STATE_MOTION_RECORD) || start))
if (f && (type & MOTION_EVENTS_DETECT))
{
fprintf(f, "<motion %6.3f >\n", detect_time);
// (float) vcb->frame_count / (float) pikrellcam.camera_adjust.video_fps);
fprintf(f, "f %3d %3d %3d %3d %3.0f %4d\n",
frame_vec->x, frame_vec->y, -frame_vec->vx, -frame_vec->vy,
sqrt((float)frame_vec->mag2), frame_vec->mag2_count);
if (mf->motion_status & MOTION_BURST)
{
burst = frame_vec->mag2_count + mf->reject_count;
fprintf(f, "b %3d\n", burst);
}
if (mf->motion_status & MOTION_AUDIO)
fprintf(f, "a %3d\n", pikrellcam.audio_level_event);
if (mf->motion_status & MOTION_FIFO)
fprintf(f, "e %s\n", mf->fifo_trigger_code);
if (mf->motion_status & MOTION_DIRECTION)
{
for (i = 0, mrlist = mf->motion_region_list; mrlist;
mrlist = mrlist->next, ++i)
{
mreg = (MotionRegion *) mrlist->data;
dir_motion = mreg->motion
& (MOTION_TYPE_DIR_SMALL | MOTION_TYPE_DIR_NORMAL);
if (!dir_motion)
continue;
cvec = &mreg->vector;
fprintf(f, "%d %3d %3d %3d %3d %3.0f %4d\n",
i, cvec->x, cvec->y, -cvec->vx, -cvec->vy,
sqrt((float)cvec->mag2), cvec->mag2_count);
}
}
fprintf(f, "</motion>\n");
fflush(f);
}
else if (f && (type & MOTION_EVENTS_STILL))
fprintf(f, "<still %s>\n", pikrellcam.still_last);
else if (f && (type & MOTION_EVENTS_END))
{
fprintf(f, "<end>\n");
fclose(f);
f = NULL;
}
/* else a MANUAL record state */
}
void
motion_detects_fifo_write(MotionFrame *mf)
{
CompositeVector *cvec, *frame_vec = &mf->frame_vector;
MotionRegion *mreg;
SList *mrlist;
char buf[256];
int burst, i;
boolean dir_motion;
static int fd = -1;
static boolean warned = FALSE;
if (!mf)
{
if (!pikrellcam.motion_detects_fifo_enable && fd >= 0)
{
write(fd, "<off>\n", 6);
close(fd);
fd = -1;
}
else if (pikrellcam.motion_detects_fifo_enable && fd < 0)
fd = open(pikrellcam.motion_detects_fifo, O_WRONLY | O_NONBLOCK);
return;
}
if (!pikrellcam.motion_detects_fifo_enable)
return;
if (fd < 0)
fd = open(pikrellcam.motion_detects_fifo, O_WRONLY | O_NONBLOCK);
if (fd < 0)
{
if (!warned)
log_printf("Open failed: %s. %m\n", pikrellcam.motion_detects_fifo);
warned = TRUE;
return;
}
else
warned = FALSE;
i = snprintf(buf, sizeof(buf), "<motion %d.%d >\n",
(int) pikrellcam.tv_now.tv_sec,
(int) pikrellcam.tv_now.tv_usec / 100000);
write(fd, buf, i);
/* Write same data as motion_events_write() but to fd for the nonblocking
| fifo instead of a buffered FILE.
*/
snprintf(buf, sizeof(buf), "f %3d %3d %3d %3d %3.0f %4d\n",
frame_vec->x, frame_vec->y, -frame_vec->vx, -frame_vec->vy,
sqrt((float)frame_vec->mag2), frame_vec->mag2_count);
write(fd, buf, strlen(buf));
if (mf->motion_status & MOTION_BURST)
{
burst = frame_vec->mag2_count + mf->reject_count;
snprintf(buf, sizeof(buf), "b %3d\n", burst);
write(fd, buf, strlen(buf));
}
if (mf->motion_status & MOTION_AUDIO)
{
snprintf(buf, sizeof(buf), "a %3d\n", pikrellcam.audio_level_event);
write(fd, buf, strlen(buf));
}
if (mf->motion_status & MOTION_FIFO)
{
snprintf(buf, sizeof(buf), "e %s\n", mf->fifo_trigger_code);
write(fd, buf, strlen(buf));
}
if (mf->motion_status & MOTION_DIRECTION)
{
for (i = 0, mrlist = mf->motion_region_list; mrlist;
mrlist = mrlist->next, ++i)
{
mreg = (MotionRegion *)mrlist->data;
dir_motion = mreg->motion
& (MOTION_TYPE_DIR_SMALL | MOTION_TYPE_DIR_NORMAL);
if (!dir_motion)
continue;
cvec = &mreg->vector;
snprintf(buf, sizeof(buf), "%d %3d %3d %3d %3d %3.0f %4d\n",
i, cvec->x, cvec->y, -cvec->vx, -cvec->vy,
sqrt((float)cvec->mag2), cvec->mag2_count);
write(fd, buf, strlen(buf));
}
}
write(fd, "</motion>\n", 10);
}
static char *
motion_stills_pathname(MotionFrame *mf, time_t t)
{
char *tag, *name_format, *path, buf[50];
snprintf(buf, sizeof(buf), "%d", pikrellcam.motion_stills_sequence);
if (mf->motion_status & (MOTION_DIRECTION | MOTION_BURST))
asprintf(&name_format, "motion_%s",
pikrellcam.motion_stills_name_format);
else
{
if (mf->motion_status & MOTION_FIFO)
tag = mf->fifo_trigger_code ? mf->fifo_trigger_code : "FIFO";
else if (mf->motion_status & MOTION_AUDIO)
tag = "Audio";
else
tag = "Unknown";
asprintf(&name_format, "ext-%s_%s", tag,
pikrellcam.motion_stills_name_format);
}
path = media_pathname(pikrellcam.still_dir, name_format, t,
'N', buf, 'H', pikrellcam.hostname);
free(name_format);
return path;
}
void
motion_stills_start(MotionFrame *mf)
{
PiKrellCam *pkc = &pikrellcam;
char *path;
motion_events_write(mf, MOTION_EVENTS_START, 0);
pkc->motion_stills_start_tv = pkc->tv_now;
pkc->motion_still_tv = pkc->tv_now;
pkc->motion_stills_capture_time = pkc->t_now;
pkc->motion_stills_sequence += 1;
pkc->config_media_sequence_modified = TRUE;
pkc->motion_stills_count = 1;
path = motion_stills_pathname(mf, pkc->motion_stills_capture_time);
make_preview_pathname(path);
if (mf->motion_status & (MOTION_DIRECTION | MOTION_BURST))
pikrellcam.external_motion_still = FALSE;
else
pikrellcam.external_motion_still = TRUE;
pikrellcam.do_preview_save = TRUE;
pikrellcam.do_thumb_convert = TRUE;
pikrellcam.thumb_convert_done = FALSE;
pikrellcam.do_motion_still = TRUE;
pikrellcam.motion_still_pathname = path;
log_printf("motion stills start: %s\n", path);
pkc->motion_stills_record = TRUE;
}
void
motion_stills_capture(MotionFrame *mf)
{
PiKrellCam *pkc = &pikrellcam;
char *path;
float period_usec, capture_diff_usec;
struct timeval tv_diff;
if (pikrellcam.motion_still_pathname)
return; /* previous capture not initiated */
timersub(&pkc->tv_now, &pkc->motion_still_tv, &tv_diff);
capture_diff_usec = (float)(tv_diff.tv_sec * 1e6) + (float)tv_diff.tv_usec;
period_usec = 60.0 / (float) pkc->motion_stills_per_minute * 1e6;
if (capture_diff_usec >= period_usec)
{
pkc->motion_still_tv = pkc->tv_now;
pkc->motion_stills_capture_time = pkc->t_now;
path = motion_stills_pathname(mf, pkc->motion_stills_capture_time);
make_preview_pathname(path);
mf->preview_frame_vector = mf->frame_vector;
mf->preview_motion_area = mf->motion_area;
if (mf->motion_status & (MOTION_DIRECTION | MOTION_BURST))
pikrellcam.external_motion_still = FALSE;
else
pikrellcam.external_motion_still = TRUE;
/* Don't switch the camera into still capture mode until the motion
| detecting I420 frame has converted to a mjpeg.jpg suitable for a
| preview_save() and thumb_convert(). Set flags for mjpeg_callback()
| so it uses the right frame for thumb/preview and then schedule the
| still_capture(). motion_still_pathname used as holdoff so can't
| start another pipeline until previous still_capture() has started.
*/
pikrellcam.do_preview_save = TRUE;
pikrellcam.do_thumb_convert = TRUE;
pikrellcam.do_motion_still = TRUE;
pikrellcam.motion_still_pathname = path;
log_printf("motion stills capture: %s.\n", path);
pikrellcam.motion_stills_count += 1;
}
}
void
motion_stills_stop(void)
{
char *motion_end_cmd;
if (!pikrellcam.motion_stills_record)
return;
pikrellcam.motion_stills_record = FALSE;
pikrellcam.external_motion_still = FALSE;
motion_events_write(&motion_frame, MOTION_EVENTS_END, 0);
if (*pikrellcam.on_motion_end_cmd)
{
motion_end_cmd = expand_command(pikrellcam.on_motion_end_cmd, NULL);
exec_no_wait(motion_end_cmd, NULL, TRUE);
free(motion_end_cmd);
}
log_printf("motion stills end.\n");
}
void
motion_frame_process(VideoCircularBuffer *vcb, MotionFrame *mf)
{
MotionRegion *mreg;
CompositeVector *cvec, *frame_vec;
struct timeval tv_diff;
SList *mrlist;
int motion_count, fail_count, direction_motion;
boolean burst_density_pass, motion_enabled;
char tbuf[50], *msg;
int x0, y0, x1, y1, t;
float detect_time;
static int mfp_number, motion_burst_frame;
/* Allow some startup camera settle time before motion detecting.
*/
if (pikrellcam.t_now < pikrellcam.t_start + 3)
return;
mf->motion_status = MOTION_NONE;
mf->sparkle_count = 0;
mf->reject_count = 0;
mf->any_count = 0;
mf->vertical_count = 0;
memset(mf->trigger, 0, MF_TRIGGER_SIZE);
mf->motion_area.x0 = mf->motion_area.y0 = mf->motion_area.x1 = mf->motion_area.y1 = 0;
mf->mag2_limit = pikrellcam.motion_magnitude_limit * pikrellcam.motion_magnitude_limit;
mf->mag2_limit_count = pikrellcam.motion_magnitude_limit_count;
pthread_mutex_lock(&mf->region_list_mutex);
for (mrlist = mf->motion_region_list; mrlist; mrlist = mrlist->next)
{
mreg = (MotionRegion *) mrlist->data;
get_composite_vector(mf, mreg);
/* Revert any dynamic mag2 adjust done in get_composite_vector().
*/
mf->mag2_limit = pikrellcam.motion_magnitude_limit * pikrellcam.motion_magnitude_limit;
mf->mag2_limit_count = pikrellcam.motion_magnitude_limit_count;
}
motion_count = 0;
fail_count = 0;
burst_density_pass = FALSE;
mf->frame_vector = zero_cvec;
frame_vec = &mf->frame_vector;
mf->cvec_count = 0;
for (mrlist = mf->motion_region_list; mrlist; mrlist = mrlist->next)
{
mreg = (MotionRegion *) mrlist->data;
cvec = &mreg->vector;
direction_motion = mreg->motion & (MOTION_TYPE_DIR_SMALL | MOTION_TYPE_DIR_NORMAL);
if ( cvec->mag2_count > 0
&& !direction_motion
)
fail_count += 1;
else if (direction_motion)
motion_count += 1;
if (mreg->motion & MOTION_TYPE_BURST_DENSITY)
burst_density_pass = TRUE;
if (cvec->mag2_count > 0)
{
frame_vec->x += cvec->x;
frame_vec->y += cvec->y;
frame_vec->vx += cvec->vx;
frame_vec->vy += cvec->vy;
frame_vec->mag2_count += cvec->mag2_count;
mf->cvec_count += 1;
}
}
/* The frame composite vector is the vector sum of all the region composite
| vectors and the box is formed from the max dx and dy of the region
| vector box extents from the frame vector center. This vector box is
| likely smaller than the geometric box recorded in the motion_area values.
*/
if (mf->cvec_count > 0)
{
frame_vec->x /= mf->cvec_count;
frame_vec->y /= mf->cvec_count;
frame_vec->vx /= mf->cvec_count;
frame_vec->vy /= mf->cvec_count;
frame_vec->mag2 =
frame_vec->vx * frame_vec->vx + frame_vec->vy * frame_vec->vy;
x0 = y0 = x1 = y1 = 0;
for (mrlist = mf->motion_region_list; mrlist; mrlist = mrlist->next)
{
mreg = (MotionRegion *) mrlist->data;
cvec = &mreg->vector;
if (cvec->mag2_count == 0)
continue;
t = cvec->x - cvec->box_w / 2;
if (x0 == 0 || t < x0)
x0 = t;
t = cvec->x + cvec->box_w / 2;
if (t > x1)
x1 = t;
t = cvec->y - cvec->box_h / 2;
if (y0 == 0 || t < y0)
y0 = t;
t = cvec->y + cvec->box_h / 2;
if (t > y1)
y1 = t;
}
frame_vec->box_w = 2 * MAX(frame_vec->x - x0, x1 - frame_vec->x);
frame_vec->box_h = 2 * MAX(frame_vec->y - y0, y1 - frame_vec->y);
}
pthread_mutex_unlock(&mf->region_list_mutex);
/* To be used (large sparkle counts after sunset / before sunrise).
*/
mf->sparkle_expma = EXPMA_SMOOTHING * (float) mf->sparkle_count +
(1.0 - EXPMA_SMOOTHING) * mf->sparkle_expma;
mf->motion_status = MOTION_NONE;
/* Burst motion triggers on counts above the any_count_expma
| background count noise.
*/
if ( burst_density_pass
&& frame_vec->mag2_count + mf->reject_count >
pikrellcam.motion_burst_count + (int) mf->any_count_expma
)
{
if (motion_burst_frame < pikrellcam.motion_burst_frames)
++motion_burst_frame;
mf->motion_status = MOTION_PENDING_BURST;
}
else
{
/* any_count_expma is background noise counts so exclude from it
| activity that caused any pass or fail cvecs. It already
| excludes sparkles.
*/
if (motion_count == 0)
mf->any_count_expma = 0.02 * (float) mf->any_count +
(1.0 - 0.02) * mf->any_count_expma;
if (motion_burst_frame > 0)
--motion_burst_frame;
}
if ( motion_count > 0
&& fail_count == 0
)
{
if ( !(vcb->state & VCB_STATE_MOTION_RECORD)
&& !pikrellcam.motion_stills_record
&& mf->frame_window == 0
&& pikrellcam.motion_times.confirm_gap > 0
)
{
mf->frame_window = pikrellcam.camera_adjust.video_fps *
pikrellcam.motion_times.confirm_gap / pikrellcam.mjpeg_divider;
mf->motion_status |= MOTION_PENDING_DIR;
}
else
mf->motion_status = (MOTION_DETECTED | MOTION_DIRECTION);
}
if (motion_burst_frame == pikrellcam.motion_burst_frames)
{
mf->motion_status &= ~(MOTION_PENDING_DIR | MOTION_PENDING_BURST);
mf->motion_status |= (MOTION_DETECTED | MOTION_BURST);
mf->frame_window = 0;
}
if (mf->fifo_trigger)
{
mf->fifo_trigger = FALSE;
mf->motion_status &= ~(MOTION_PENDING_DIR | MOTION_PENDING_BURST);
mf->motion_status |= (MOTION_DETECTED | MOTION_FIFO);
mf->frame_window = 0;
}
pikrellcam.audio_level_event = pikrellcam.audio_level * 100 / INT16_MAX;
if ( pikrellcam.audio_trigger_video
&& pikrellcam.audio_level_event >= pikrellcam.audio_trigger_level
)
{
mf->motion_status &= ~(MOTION_PENDING_DIR | MOTION_PENDING_BURST);
mf->motion_status |= (MOTION_DETECTED | MOTION_AUDIO);
mf->frame_window = 0;
}
else
pikrellcam.audio_level_event = 0;
pikrellcam.audio_level = 0;
if (mf->frame_window > 0)
mf->frame_window -= 1;
if ( pikrellcam.verbose_motion
&& frame_vec->mag2_count > 0
)
{
printf(
"frame: x,y(%d,%d) dx,dy(%d,%d) mag2:%d count,rej(%d,%d) any[%d,%.1f] burst:%d\n",
frame_vec->x, frame_vec->y, frame_vec->vx, frame_vec->vy,
frame_vec->mag2, frame_vec->mag2_count, mf->reject_count,
mf->any_count, mf->any_count_expma, motion_burst_frame);
}
if (pikrellcam.verbose_motion && (fail_count > 0 || motion_count > 0))
{
strftime(tbuf, sizeof(tbuf), "%T", &pikrellcam.tm_local);
msg = "";
if ((mf->motion_status & (MOTION_DIRECTION | MOTION_BURST))
== (MOTION_DIRECTION | MOTION_BURST))
msg = "***MOTION BOTH***";
else if (mf->motion_status & MOTION_DIRECTION)
msg = "***MOTION DIRECTION***";
else if (mf->motion_status & MOTION_BURST)
msg = "***MOTION BURST***";
else if (mf->motion_status & MOTION_FIFO)
msg = "***MOTION EXTERNAL***";
else
{
if ((mf->motion_status & (MOTION_PENDING_DIR | MOTION_PENDING_BURST))
== (MOTION_PENDING_DIR | MOTION_PENDING_BURST))
msg = "***motion pending (dir|burst)***";
else if (mf->motion_status & MOTION_PENDING_DIR)
msg = "***motion pending (dir)***";
else if (mf->motion_status & MOTION_PENDING_BURST)
msg = "***motion pending (burst)***";
}
printf("%s [%d] regions[motion:%d fail:%d] spkl[%d,%.1f] window:%d %s\n\n",
tbuf, mfp_number, motion_count, fail_count,
mf->sparkle_count, mf->sparkle_expma,
mf->frame_window, msg);
}
++mfp_number;
if (motion_burst_frame == pikrellcam.motion_burst_frames)
motion_burst_frame = 0;
motion_enabled = ( ( mf->motion_enable
|| (mf->fifo_trigger_mode & FIFO_TRIG_MODE_ENABLE)
)
&& !pikrellcam.servo_moving
&& pikrellcam.zoom_motion_holdoff == 0
&& (pikrellcam.on_preset || pikrellcam.motion_off_preset)
);
if (pikrellcam.zoom_motion_holdoff > 0)
--pikrellcam.zoom_motion_holdoff;
if ( ( (mf->motion_status & MOTION_DETECTED)
&& motion_enabled
&& (mf->fifo_trigger_mode & FIFO_TRIG_MODE_TIMES) == 0
)
|| ( (mf->motion_status & MOTION_FIFO) && motion_enabled)
)
{
pikrellcam.motion_last_detect_time = pikrellcam.t_now;
/* Motion recording ignored if a manual record is in progress.
| If loop recording, no motion stills recording and turn the
| loop video into a motion video.
*/
if ( ( vcb->state == VCB_STATE_NONE
&& !pikrellcam.motion_stills_record
)
|| ( (vcb->state & VCB_STATE_LOOP_RECORD)
&& !(vcb->state & VCB_STATE_MOTION_RECORD)
)
)
{
mf->preview_frame_vector = mf->frame_vector;
if (mf->motion_status & (MOTION_DIRECTION | MOTION_BURST))
mf->preview_motion_area = mf->motion_area;
else /* (MOTION_FIFO | MOTION_AUDIO)*/
pikrellcam.external_motion_record_event = TRUE;
if ( !(vcb->state & VCB_STATE_LOOP_RECORD)
&& !pikrellcam.motion_stills_enable
)
video_record_start(vcb, VCB_STATE_MOTION_RECORD_START);
else
{
/* Either a current loop video to be turned into a motion
| video or start motion stills recording.
*/
if (vcb->state & VCB_STATE_LOOP_RECORD)
{
vcb->state |= VCB_STATE_MOTION_RECORD;
motion_events_write(&motion_frame, MOTION_EVENTS_START,
(float) vcb->frame_count
/ (float) pikrellcam.camera_adjust.video_fps);
pikrellcam.do_preview_save = TRUE;
pikrellcam.do_thumb_convert = TRUE;
}
else if (pikrellcam.motion_stills_enable)
motion_stills_start(mf);
if (*pikrellcam.on_motion_begin_cmd)
event_add("motion begin", pikrellcam.t_now, 0,
event_motion_begin_cmd,
pikrellcam.on_motion_begin_cmd);
}
pikrellcam.video_notify = FALSE;
pikrellcam.still_notify = FALSE;
pikrellcam.timelapse_notify = FALSE;
mf->first_detect = mf->motion_status;
mf->direction_detects = (mf->motion_status & MOTION_DIRECTION) ? 1 : 0;
mf->burst_detects = (mf->motion_status & MOTION_BURST) ? 1 : 0;
mf->max_burst_count = 0;
mf->fifo_detects = (mf->motion_status & MOTION_FIFO) ? 1 : 0;
mf->audio_detects = (mf->motion_status & MOTION_AUDIO) ? 1 : 0;
if (pikrellcam.verbose_motion && !pikrellcam.verbose)
printf("***Motion record start: %s\n\n", pikrellcam.video_pathname);
}
else if ( vcb->state & VCB_STATE_MOTION_RECORD
|| pikrellcam.motion_stills_record
)
{
/* Already recording, so each motion trigger bumps up the record
| time to now + post capture time.
| If mode "best" and better composite vector, save a new preview.
*/
pikrellcam.motion_sync_time = pikrellcam.t_now
+ pikrellcam.motion_times.post_capture;
if (mf->motion_status & (MOTION_DIRECTION | MOTION_BURST))
pikrellcam.external_motion_record_event = FALSE;
if (mf->motion_status & MOTION_DIRECTION)
++mf->direction_detects;
if (mf->motion_status & MOTION_BURST)
{
++mf->burst_detects;
if (mf->max_burst_count < frame_vec->mag2_count + mf->reject_count)
mf->max_burst_count = frame_vec->mag2_count + mf->reject_count;
}
if (mf->motion_status & MOTION_AUDIO)
++mf->audio_detects;
if (mf->motion_status & MOTION_FIFO)
++mf->fifo_detects;
if (vcb->state & VCB_STATE_MOTION_RECORD)
detect_time = (float) vcb->frame_count
/ (float) pikrellcam.camera_adjust.video_fps;
else
{
timersub(&pikrellcam.tv_now,
&pikrellcam.motion_stills_start_tv, &tv_diff);
detect_time = (float) tv_diff.tv_sec