1 /*
2  *  motiondetect.h
3  *
4  *  Copyright (C) Georg Martius - February 2011
5  *   georg dot martius at web dot de
6  *  Copyright (C) Alexey Osipov - Jule 2011
7  *   simba at lerlan dot ru
8  *   speed optimizations (threshold, spiral, SSE, asm)
9  *
10  *  This file is part of vid.stab video stabilization library
11  *
12  *  vid.stab is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License,
14  *  as published by the Free Software Foundation; either version 2, or
15  *  (at your option) any later version.
16  *
17  *  vid.stab is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with GNU Make; see the file COPYING.  If not, write to
24  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  */
27 
28 #ifndef MOTIONDETECT_H
29 #define MOTIONDETECT_H
30 
31 #include <stddef.h>
32 #include <stdlib.h>
33 
34 #include "transformtype.h"
35 #include "vidstabdefines.h"
36 #include "vsvector.h"
37 #include "frameinfo.h"
38 
39 typedef struct _vsmotiondetectconfig {
40   /* meta parameter for maxshift and fieldsize between 1 and 15 */
41   int         shakiness;
42   int         accuracy;         // meta parameter for number of fields between 1 and 10
43   int         stepSize;         // stepsize of field transformation detection
44   int         algo;             // deprecated
45   int         virtualTripod;
46   /* if 1 and 2 then the fields and transforms are shown in the frames */
47   int         show;
48   /* measurement fields with lower contrast are discarded */
49   double      contrastThreshold;
50   const char* modName;          // module name (used for logging)
51 } VSMotionDetectConfig;
52 
53 /** structure for motion detection fields */
54 typedef struct _vsmotiondetectfields {
55   /* maximum number of pixels we expect the shift of subsequent frames */
56   int maxShift;
57   int stepSize;                 // stepsize for detection
58   int fieldNum;                 // number of measurement fields
59   int maxFields;                // maximum number of fields used (selected by contrast)
60   double contrastThreshold;     // fields with lower contrast are discarded
61   int fieldSize;                // size = min(md->width, md->height)/10;
62   int fieldRows;                // number of rows
63   Field* fields;                // measurement fields
64   short useOffset;              // if true then the offset us used
65   VSTransform offset;           // offset for detection (e.g. known from coarse scan)
66 } VSMotionDetectFields;
67 
68 /** data structure for motion detection part of deshaking*/
69 typedef struct _vsmotiondetect {
70   VSFrameInfo fi;
71 
72   VSMotionDetectConfig conf;
73 
74   VSMotionDetectFields fieldscoarse;
75   VSMotionDetectFields fieldsfine;
76 
77   VSFrame curr;                 // blurred version of current frame buffer
78   VSFrame currorig;             // current frame buffer (original) (only pointer)
79   VSFrame currtmp;              // temporary buffer for blurring
80   VSFrame prev;                 // frame buffer for last frame (copied)
81   short hasSeenOneFrame;        // true if we have a valid previous frame
82   int initialized;              // 1 if initialized and 2 if configured
83 
84   int frameNum;
85 } VSMotionDetect;
86 
87 static const char vs_motiondetect_help[] = ""
88     "Overview:\n"
89     "    Generates a file with relative transform information\n"
90     "     (translation, rotation) about subsequent frames."
91     " See also transform.\n"
92     "Options\n"
93     "    'result'      path to the file used to write the transforms\n"
94     "                  (def:inputfile.stab)\n"
95     "    'shakiness'   how shaky is the video and how quick is the camera?\n"
96     "                  1: little (fast) 10: very strong/quick (slow) (def: 5)\n"
97     "    'accuracy'    accuracy of detection process (>=shakiness)\n"
98     "                  1: low (fast) 15: high (slow) (def: 9)\n"
99     "    'stepsize'    stepsize of search process, region around minimum \n"
100     "                  is scanned with 1 pixel resolution (def: 6)\n"
101     "    'mincontrast' below this contrast a field is discarded (0-1) (def: 0.3)\n"
102     "    'tripod'      virtual tripod mode (if >0): motion is compared to a \n"
103     "                  reference frame (frame # is the value) (def: 0)\n"
104     "    'show'        0: draw nothing (def); 1,2: show fields and transforms\n"
105     "                  in the resulting frames. Consider the 'preview' filter\n"
106     "    'help'        print this help message\n";
107 
108 
109 /** returns the default config
110  */
111 VSMotionDetectConfig vsMotionDetectGetDefaultConfig(const char* modName);
112 
113 /** initialized the VSMotionDetect structure and allocates memory
114  *  for the frames and stuff
115  *  @return VS_OK on success otherwise VS_ERROR
116  */
117 int vsMotionDetectInit(VSMotionDetect* md, const VSMotionDetectConfig* conf,
118                        const VSFrameInfo* fi);
119 
120 /**
121  *  Performs a motion detection step
122  *  Only the new current frame is given. The last frame
123  *  is stored internally
124  *  @param motions: calculated local motions. (must be deleted manually)
125  * */
126 int vsMotionDetection(VSMotionDetect* md, LocalMotions* motions, VSFrame *frame);
127 
128 /** Deletes internal data structures.
129  * In order to use the VSMotionDetect again, you have to call vsMotionDetectInit
130  */
131 void vsMotionDetectionCleanup(VSMotionDetect* md);
132 
133 /// returns the current config
134 void vsMotionDetectGetConfig(VSMotionDetectConfig* conf, const VSMotionDetect* md);
135 
136 /// returns the frame info
137 const VSFrameInfo* vsMotionDetectGetFrameInfo(const VSMotionDetect* md);
138 
139 #endif  /* MOTIONDETECT_H */
140 
141 /*
142  * Local variables:
143  *   c-file-style: "stroustrup"
144  *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
145  *   indent-tabs-mode: nil
146  *   c-basic-offset: 2 t
147  * End:
148  *
149  * vim: expandtab shiftwidth=2:
150  */
151