CHDK_DE Vorschauversion  Trunk Rev. 6014
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
motion_detector.c-Dateireferenz
#include "camera_info.h"
#include "action_stack.h"
#include "console.h"
#include "keyboard.h"
#include "clock.h"
#include "viewport.h"
#include "debug_led.h"
#include "gui.h"
#include "gui_draw.h"
#include "script_api.h"
#include "script.h"
#include "motion_detector.h"
#include "module_def.h"
#include "gui_lang.h"
+ Include-Abhängigkeitsdiagramm für motion_detector.c:

gehe zum Quellcode dieser Datei

Datenstrukturen

struct  motion_detector_s
 

Makrodefinitionen

#define MD_XY2IDX(x, y)   ((y)*motion_detector.columns+(x))
 
#define MOTION_DETECTOR_CELLS   1024
 
#define md_save_calls_history()
 
#define mx_dump_memory(x)
 

Aufzählungen

enum  {
  MD_MEASURE_MODE_U =0, MD_MEASURE_MODE_Y =1, MD_MEASURE_MODE_V =2, MD_MEASURE_MODE_R =3,
  MD_MEASURE_MODE_G =4, MD_MEASURE_MODE_B =5
}
 
enum  { MD_DO_IMMEDIATE_SHOOT =1, MD_MAKE_DEBUG_LOG_FILE =2, MD_MAKE_RAM_DUMP_FILE =4, MD_NO_SHUTTER_RELEASE_ON_SHOOT =8 }
 
enum  { MD_REGION_NONE =0, MD_REGION_INCLUDE =1, MD_REGION_EXCLUDE =2 }
 

Funktionen

static int md_detect_motion (void)
 
static void time_counter_capture (time_counter *t)
 
static int action_stack_AS_MOTION_DETECTOR ()
 
static void md_kbd_sched_immediate_shoot (int no_release)
 
static int clip (int v)
 
void md_close_motion_detector ()
 
int md_init_motion_detector (int columns, int rows, int pixel_measure_mode, int detection_timeout, int measure_interval, int threshold, int draw_grid, int clipping_region_mode, int clipping_region_column1, int clipping_region_row1, int clipping_region_column2, int clipping_region_row2, int parameters, int pixels_step, int msecs_before_trigger)
 
static int md_running ()
 
static int md_measure_y ()
 
static int md_measure_u ()
 
static int md_measure_v ()
 
static int md_measure_r ()
 
static int md_measure_g ()
 
static int md_measure_b ()
 
int md_get_cell_val (int column, int row)
 
int md_get_cell_diff (int column, int row)
 
void md_draw_grid ()
 
int _module_unloader ()
 
int _module_can_unload ()
 

Variablen

static struct motion_detector_s motion_detector
 
static unsigned char * img
 
static int x_start
 
static int x_end
 
static int x_step
 
static int y_start
 
static int y_end
 
static int y_step
 
libmotiondetect_sym _libmotiondetect
 
ModuleInfo _module_info
 

Makro-Dokumentation

#define md_save_calls_history ( )

Definiert in Zeile 430 der Datei motion_detector.c.

#define MD_XY2IDX (   x,
  y 
)    ((y)*motion_detector.columns+(x))

Definiert in Zeile 50 der Datei motion_detector.c.

#define MOTION_DETECTOR_CELLS   1024

Definiert in Zeile 78 der Datei motion_detector.c.

#define mx_dump_memory (   x)

Definiert in Zeile 431 der Datei motion_detector.c.

Dokumentation der Aufzählungstypen

anonymous enum
Aufzählungswerte
MD_MEASURE_MODE_U 
MD_MEASURE_MODE_Y 
MD_MEASURE_MODE_V 
MD_MEASURE_MODE_R 
MD_MEASURE_MODE_G 
MD_MEASURE_MODE_B 

Definiert in Zeile 52 der Datei motion_detector.c.

anonymous enum
Aufzählungswerte
MD_DO_IMMEDIATE_SHOOT 
MD_MAKE_DEBUG_LOG_FILE 
MD_MAKE_RAM_DUMP_FILE 
MD_NO_SHUTTER_RELEASE_ON_SHOOT 

Definiert in Zeile 63 der Datei motion_detector.c.

anonymous enum
Aufzählungswerte
MD_REGION_NONE 
MD_REGION_INCLUDE 
MD_REGION_EXCLUDE 

Definiert in Zeile 71 der Datei motion_detector.c.

72 {
76 };

Dokumentation der Funktionen

int _module_can_unload ( )

Definiert in Zeile 879 der Datei motion_detector.c.

int _module_unloader ( )

Definiert in Zeile 873 der Datei motion_detector.c.

874 {
876  return 0;
877 }
static int action_stack_AS_MOTION_DETECTOR ( )
static

Definiert in Zeile 135 der Datei motion_detector.c.

136 {
137  // MD testing with AF LED
139  {
140  if (camera_info.perf.af_led_on == 0)
141  {
145  }
146  else if (camera_info.perf.af_led_on > 0)
147  {
149  }
150  }
151 
152  if (md_detect_motion() == 0)
153  {
155  {
157  }
158 
159  // MD testing with AF LED
161  {
164  {
166  }
167  }
168 
169  // We need to recover the motion detector's
170  // result and push it onto the thread's stack.
172 
173  action_pop_func(0);
174  return 1;
175  }
176  return 0;
177 }
static int clip ( int  v)
static

Definiert in Zeile 200 der Datei motion_detector.c.

201 {
202  if (v<0) return 0;
203  if (v>255) return 255;
204  return v;
205 }
void md_close_motion_detector ( )

Definiert in Zeile 208 der Datei motion_detector.c.

209 {
210 }
static int md_detect_motion ( void  )
static

Definiert in Zeile 602 der Datei motion_detector.c.

603 {
604  int idx, tick, rv;
605 
606  register int col, row;
607 
608  if(!md_running())
609  {
610  return 0;
611  }
612 
613  tick = get_tick_count();
614  rv = 1;
615 
616 #ifdef OPT_MD_DEBUG
617  if(motion_detector.comp_calls_cnt < MD_REC_CALLS_CNT)
618  {
619  motion_detector.comp_calls[motion_detector.comp_calls_cnt]=tick;
620  }
621  motion_detector.comp_calls_cnt++;
622 #endif
623 
625  {
628  return 0;
629  }
630 
632  {
633  // wait for the next time
634  return 1;
635  }
636 
638 
640  if (!img) return 0;
641 
642 #ifdef OPT_MD_DEBUG
643  if(motion_detector.comp_calls_cnt==50 && (motion_detector.parameters & MD_MAKE_RAM_DUMP_FILE) != 0 )
644  {
645  mx_dump_memory((char*)img);
646  }
647 #endif
648 
650 
651  img += vid_get_viewport_image_offset(); // offset into viewport for when image size != viewport size (e.g. 16:9 image on 4:3 LCD)
652 
653  int vp_bw = vid_get_viewport_byte_width();
654  int vp_h = vid_get_viewport_height_proper();
655 
656 #ifdef THUMB_FW
657  int vp_w = vid_get_viewport_width_proper();
660 #else
661  int vp_w = vid_get_viewport_width_proper() / 2; // Row width in 3 byte units (half UYVYYY block)
664 #endif
665 
667 
668  for (idx=0, row=0; row < motion_detector.rows; row++)
669  {
670  // Calc img y start and end offsets (use same height for all cells so 'points' is consistent)
671  y_start = ((row * vp_h) / motion_detector.rows) * vp_bw;
672  y_end = y_start + ((vp_h / motion_detector.rows) * vp_bw);
673 
674  for (col=0; col < motion_detector.columns; col++, idx++)
675  {
676  int in_clipping_region=0;
677 
682  {
683  in_clipping_region=1;
684  }
685 
686  int curr = 0;
687  int diff = 0;
688 
689  if (
691  (motion_detector.clipping_region_mode==MD_REGION_EXCLUDE && in_clipping_region==0) ||
692  (motion_detector.clipping_region_mode==MD_REGION_INCLUDE && in_clipping_region==1)
693  )
694  {
695  // Calc img x start and end offsets (use same width for all cells so 'points' is consistent)
696 #ifdef THUMB_FW
697  x_start = ((col * vp_w) / motion_detector.columns) * 2;
698  x_end = x_start + ((vp_w / motion_detector.columns) * 2);
699 #else
700  x_start = ((col * vp_w) / motion_detector.columns) * 3;
701  x_end = x_start + ((vp_w / motion_detector.columns) * 3);
702 #endif
703 
704  // Do mode check and call function to do inner loops for mode
705  // See comments above on mode calulations
707  {
708  case MD_MEASURE_MODE_Y:
709  curr += md_measure_y();
710  break;
711  case MD_MEASURE_MODE_U:
712  curr += md_measure_u();
713  break;
714  case MD_MEASURE_MODE_V:
715  curr += md_measure_v();
716  break;
717  case MD_MEASURE_MODE_R:
718  curr += md_measure_r();
719  break;
720  case MD_MEASURE_MODE_G:
721  curr += md_measure_g();
722  break;
723  case MD_MEASURE_MODE_B:
724  curr += md_measure_b();
725  break;
726  }
727 
728  diff = (curr - motion_detector.prev[idx]) / motion_detector.points;
729  if (diff < 0) diff = -diff;
730  if ((diff > motion_detector.threshold) &&
732  {
734  }
735  }
736 
737  motion_detector.diff[idx] = diff;
738  motion_detector.prev[idx] = curr;
739  }
740  }
741 
743  {
747  }
748  else if ( motion_detector.detected_cells > 0 )
749  {
751  {
754 
756  {
757  //make shoot
759  }
760  rv = 0;
761  }
762  }
763 
764  return rv;
765 }
void md_draw_grid ( )

Definiert in Zeile 789 der Datei motion_detector.c.

790 {
791  int col, row;
792  int i;
793  char mdbuff[8];
794 
796  {
797  return;
798  }
799 
800  int xoffset = vid_get_viewport_display_xoffset(); // used when image size != viewport size
801  int yoffset = vid_get_viewport_display_yoffset(); // used when image size != viewport size
802 
803  // display area size
804  int x_size = camera_screen.width-xoffset * 2;
805  int y_size = camera_screen.height-yoffset * 2;
806 
807  // initial display offsets
808  int y_start, y_end = yoffset;
809  int x_start, x_end;
810 
811  for (i=0, row=0; row < motion_detector.rows && camera_info.state.state_kbd_script_run; row++)
812  {
813  // Calc display start and end offsets
814  y_start = y_end; // reuse last end value as new start value
815  y_end = yoffset + ((row + 1) * y_size) / motion_detector.rows;
816 
817  x_end = xoffset;
818 
819  for (col=0; col < motion_detector.columns; col++, i++)
820  {
821  // Calc display x start and end offsets
822  x_start = x_end; // reuse last end value as new start value
823  x_end = xoffset + ((col + 1) * x_size) / motion_detector.columns;
824 
825  int in_clipping_region = 0;
830  )
831  {
832  in_clipping_region = 1;
833  }
834 
835  if ((motion_detector.clipping_region_mode==MD_REGION_EXCLUDE && in_clipping_region==0) ||
836  (motion_detector.clipping_region_mode==MD_REGION_INCLUDE && in_clipping_region==1) ||
838  {
839  int diff = motion_detector.diff[i];
840 
842  if (diff > motion_detector.threshold)
843  {
845  }
846 
847  if (motion_detector.draw_grid & 2)
848  {
849  sprintf(mdbuff,"%-3d", diff);
850  draw_string(x_start+4, y_start+2, mdbuff, c);
851  }
852 
853  if (motion_detector.draw_grid & 1)
854  {
855  draw_rectangle(x_start+2, y_start+2, x_end-2, y_end-2, c, RECT_BORDER1);
856  }
857  }
858  }
859  }
860 }
int md_get_cell_diff ( int  column,
int  row 
)

Definiert in Zeile 778 der Datei motion_detector.c.

779 {
780  if ((column<1 || column > motion_detector.columns) ||
781  (row<1 || row > motion_detector.rows))
782  {
783  return 0;
784  }
785 
786  return motion_detector.diff[ MD_XY2IDX(column-1,row-1) ];
787 }
int md_get_cell_val ( int  column,
int  row 
)

Definiert in Zeile 767 der Datei motion_detector.c.

768 {
769  if ((column<1 || column > motion_detector.columns) ||
770  (row<1 || row > motion_detector.rows))
771  {
772  return 0;
773  }
774 
775  return motion_detector.prev[ MD_XY2IDX(column-1,row-1) ]/motion_detector.points ;
776 }
int md_init_motion_detector ( int  columns,
int  rows,
int  pixel_measure_mode,
int  detection_timeout,
int  measure_interval,
int  threshold,
int  draw_grid,
int  clipping_region_mode,
int  clipping_region_column1,
int  clipping_region_row1,
int  clipping_region_column2,
int  clipping_region_row2,
int  parameters,
int  pixels_step,
int  msecs_before_trigger 
)

Definiert in Zeile 213 der Datei motion_detector.c.

230 {
231 #ifdef OPT_MD_DEBUG
232  motion_detector.comp_calls_cnt=0;
233 #endif
234 
235  if( pixel_measure_mode != MD_MEASURE_MODE_Y
236  && pixel_measure_mode != MD_MEASURE_MODE_U
237  && pixel_measure_mode != MD_MEASURE_MODE_V
238  && pixel_measure_mode != MD_MEASURE_MODE_R
239  && pixel_measure_mode != MD_MEASURE_MODE_G
240  && pixel_measure_mode != MD_MEASURE_MODE_B
241  )
242  {
243  pixel_measure_mode = MD_MEASURE_MODE_Y;
244  }
245 
246  // Sanity check on grid size
247  if (columns < 1) columns = 3;
248  if (rows < 1) rows = 3;
249  // If too many grid cells, reduce larger of columns and rows until it fits
250  while ((columns * rows) > MOTION_DETECTOR_CELLS)
251  {
252  if (columns > rows) columns--;
253  else rows--;
254  }
255 
256  if(msecs_before_trigger<0)
257  {
258  msecs_before_trigger=0;
259  }
260 
261  if (pixels_step<1)
262  {
263  pixels_step=1;
264  }
265 #ifdef THUMB_FW
266  if (((pixel_measure_mode == MD_MEASURE_MODE_U) || (pixel_measure_mode == MD_MEASURE_MODE_V)) && (pixels_step < 2))
267  {
268  pixels_step = 2; //uv is sampled every 4 bytes in X, prevent double counting
269  }
270 #endif
271 
272  if(detection_timeout<0)
273  {
274  detection_timeout=0;
275  }
276 
277  if(measure_interval<0)
278  {
279  measure_interval=0;
280  }
281 
282  if(threshold<0)
283  {
284  threshold=0;
285  }
286 
287  motion_detector.msecs_before_trigger = msecs_before_trigger;
288  motion_detector.parameters = parameters;
289  motion_detector.pixels_step = pixels_step;
290  motion_detector.columns = columns;
291  motion_detector.rows = rows;
293 
294  motion_detector.pixel_measure_mode = pixel_measure_mode;
295  motion_detector.timeout = detection_timeout;
296  motion_detector.measure_interval = measure_interval;
297  motion_detector.threshold = threshold;
298  motion_detector.draw_grid = draw_grid;
299 
300  if (clipping_region_column1>clipping_region_column2)
301  {
302  motion_detector.clipping_region_column2 = clipping_region_column1;
303  motion_detector.clipping_region_column1 = clipping_region_column2;
304  }
305  else
306  {
307  motion_detector.clipping_region_column2 = clipping_region_column2;
308  motion_detector.clipping_region_column1 = clipping_region_column1;
309  }
310 
311  if (clipping_region_row1>clipping_region_row2)
312  {
313  motion_detector.clipping_region_row2 = clipping_region_row1;
314  motion_detector.clipping_region_row1 = clipping_region_row2;
315  }
316  else
317  {
318  motion_detector.clipping_region_row2 = clipping_region_row2;
319  motion_detector.clipping_region_row1 = clipping_region_row1;
320  }
321 
322  if (clipping_region_mode!=MD_REGION_NONE && clipping_region_mode!=MD_REGION_INCLUDE && clipping_region_mode!=MD_REGION_EXCLUDE)
323  {
324  clipping_region_mode=MD_REGION_NONE;
325  }
326  motion_detector.clipping_region_mode = clipping_region_mode;
327 
331 
333 
335 
336  camera_info.perf.af_led_on = 100;
339 
340  return 1;
341 }
static void md_kbd_sched_immediate_shoot ( int  no_release)
static

Definiert in Zeile 179 der Datei motion_detector.c.

180 {
181  action_pop_func(0);// REMOVE MD ITEM
182 
183  // stack operations are reversed!
184  if (!no_release) // only release shutter if allowed
185  {
187  }
190  action_push_func(action_stack_AS_MOTION_DETECTOR); // it will removed right after exit from this function
191  kbd_key_press(KEY_SHOOT_FULL); // not a stack operation... pressing right now
192 
193  // MD testing with AF LED
195  {
197  }
198 }
static int md_measure_b ( )
static

Definiert in Zeile 577 der Datei motion_detector.c.

578 {
579  register int x, y, uvx, cy, cu;
580  register int curr = 0;
581 
582  for (y=y_start; y<y_end; y+=y_step)
583  {
584  for (x=x_start; x<x_end; x+=x_step)
585  {
586  cy = img[y + x + 1];
587  // Calc offset to U & V components in uvx
588 #ifdef THUMB_FW
589  uvx = x & 0xFFFFFFFC; // U is in 1st two bytes of each 4 byte block V is in 2nd two bytes
590  cu = (int)img[y + uvx] - 128;
591 #else
592  uvx = (x&1)?x-3:x;
593  cu = (signed char)img[y + uvx];
594 #endif
595  curr += clip(((cy<<12) + cu*7258 + 2048)>>12); // B
596  }
597  }
598 
599  return curr;
600 }
static int md_measure_g ( )
static

Definiert in Zeile 550 der Datei motion_detector.c.

551 {
552  register int x, y, uvx, cy, cu, cv;
553  register int curr = 0;
554 
555  for (y=y_start; y<y_end; y+=y_step)
556  {
557  for (x=x_start; x<x_end; x+=x_step)
558  {
559  cy = img[y + x + 1];
560  // Calc offset to U & V components in uvx
561 #ifdef THUMB_FW
562  uvx = x & 0xFFFFFFFC; // U is in 1st two bytes of each 4 byte block V is in 2nd two bytes
563  cu = (int)img[y + uvx] - 128;
564  cv = (int)img[y + uvx + 2] - 128;
565 #else
566  uvx = (x&1)?x-3:x;
567  cu = (signed char)img[y + uvx];
568  cv = (signed char)img[y + uvx + 2];
569 #endif
570  curr += clip(((cy<<12) - cu*1411 - cv*2925 + 2048)>>12); // G
571  }
572  }
573 
574  return curr;
575 }
static int md_measure_r ( )
static

Definiert in Zeile 525 der Datei motion_detector.c.

526 {
527  register int x, y, uvx, cy, cv;
528  register int curr = 0;
529 
530  for (y=y_start; y<y_end; y+=y_step)
531  {
532  for (x=x_start; x<x_end; x+=x_step)
533  {
534  cy = img[y + x + 1];
535  // Calc offset to U & V components in uvx
536 #ifdef THUMB_FW
537  uvx = x & 0xFFFFFFFC; // U is in 1st two bytes of each 4 byte block V is in 2nd two bytes
538  cv = (int)img[y + uvx + 2] - 128;
539 #else
540  uvx = (x&1)?x-3:x;
541  cv = (signed char)img[y + uvx + 2];
542 #endif
543  curr += clip(((cy<<12) + cv*5743 + 2048)>>12); // R
544  }
545  }
546 
547  return curr;
548 }
static int md_measure_u ( )
static

Definiert in Zeile 479 der Datei motion_detector.c.

480 {
481  register int x, y, uvx;
482  register int curr = 0;
483 
484  for (y=y_start; y<y_end; y+=y_step)
485  {
486  for (x=x_start; x<x_end; x+=x_step)
487  {
488  // Calc offset to U & V components in uvx
489 #ifdef THUMB_FW
490  uvx = x & 0xFFFFFFFC; // U is in 1st two bytes of each 4 byte block V is in 2nd two bytes
491  curr += (int)img[y + uvx] - 128; //U
492 #else
493  uvx = (x&1)?x-3:x;
494  curr += (signed char)img[y + uvx]; //U
495 #endif
496  }
497  }
498 
499  return curr;
500 }
static int md_measure_v ( )
static

Definiert in Zeile 502 der Datei motion_detector.c.

503 {
504  register int x, y, uvx;
505  register int curr = 0;
506 
507  for (y=y_start; y<y_end; y+=y_step)
508  {
509  for (x=x_start; x<x_end; x+=x_step)
510  {
511  // Calc offset to U & V components in uvx
512 #ifdef THUMB_FW
513  uvx = x & 0xFFFFFFFC; // U is in 1st two bytes of each 4 byte block V is in 2nd two bytes
514  curr += (int)img[y + uvx + 2] - 128; //V
515 #else
516  uvx = (x&1)?x-3:x;
517  curr += (signed char)img[y + uvx + 2]; //V
518 #endif
519  }
520  }
521 
522  return curr;
523 }
static int md_measure_y ( )
static

Definiert in Zeile 463 der Datei motion_detector.c.

464 {
465  register int x, y;
466  register int curr = 0;
467 
468  for (y=y_start; y<y_end; y+=y_step)
469  {
470  for (x=x_start; x<x_end; x+=x_step)
471  {
472  curr += img[y + x + 1]; //Y always 2nd byte in each block
473  }
474  }
475 
476  return curr;
477 }
static int md_running ( )
static

Definiert in Zeile 435 der Datei motion_detector.c.

436 {
437  return motion_detector.running;
438 }
static void time_counter_capture ( time_counter t)
static

Definiert in Zeile 123 der Datei motion_detector.c.

124 {
125  t->last = get_tick_count() - t->tick_count;
126  if (t->last < t->min)
127  t->min = t->last;
128  if (t->last > t->max)
129  t->max = t->last;
130  t->sum += t->last;
131  t->count++;
132 }

Variablen-Dokumentation

unsigned char* img
static

Definiert in Zeile 441 der Datei motion_detector.c.

struct motion_detector_s motion_detector
static

Definiert in Zeile 121 der Datei motion_detector.c.

int x_end
static

Definiert in Zeile 442 der Datei motion_detector.c.

int x_start
static

Definiert in Zeile 442 der Datei motion_detector.c.

int x_step
static

Definiert in Zeile 442 der Datei motion_detector.c.

int y_end
static

Definiert in Zeile 443 der Datei motion_detector.c.

int y_start
static

Definiert in Zeile 443 der Datei motion_detector.c.

int y_step
static

Definiert in Zeile 443 der Datei motion_detector.c.