1 /*
2  *  transform.h
3  *
4  *  Copyright (C) Georg Martius - June 2007 - 2011
5  *   georg dot martius at web dot de
6  *
7  *  This file is part of vid.stab video stabilization library
8  *
9  *  vid.stab is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License,
11  *  as published by the Free Software Foundation; either version 2, or
12  *  (at your option) any later version.
13  *
14  *  vid.stab is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with GNU Make; see the file COPYING.  If not, write to
21  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24 #ifndef __TRANSFORM_H
25 #define __TRANSFORM_H
26 
27 #include <math.h>
28 #include <libgen.h>
29 #include "transformtype.h"
30 #include "frameinfo.h"
31 #include "vidstabdefines.h"
32 #ifdef TESTING
33 #include "transformfloat.h"
34 #endif
35 
36 
37 typedef struct _vstransformations {
38     VSTransform* ts; // array of transformations
39     int current;   // index to current transformation
40     int len;       // length of trans array
41     short warned_end; // whether we warned that there is no transform left
42 } VSTransformations;
43 
44 typedef struct _vsslidingavgtrans {
45     VSTransform avg; // average transformation
46     VSTransform accum; // accumulator for relative to absolute conversion
47     double zoomavg;     // average zoom value
48     short initialized; // whether it was initialized or not
49 } VSSlidingAvgTrans;
50 
51 
52 /// interpolation types
53 typedef enum { VS_Zero, VS_Linear, VS_BiLinear, VS_BiCubic, VS_NBInterPolTypes} VSInterpolType;
54 
55 /// returns a name for the interpolation type
56 const char* getInterpolationTypeName(VSInterpolType type);
57 
58 typedef enum { VSKeepBorder = 0, VSCropBorder } VSBorderType;
59 typedef enum { VSOptimalL1 = 0, VSGaussian, VSAvg } VSCamPathAlgo;
60 
61 /**
62  * interpolate: general interpolation function pointer for one channel image data
63  *              for fixed point numbers/calculations
64  * Parameters:
65  *             rv: destination pixel (call by reference)
66  *            x,y: the source coordinates in the image img. Note this
67  *                 are real-value coordinates (in fixed point format 24.8),
68  *                 that's why we interpolate
69  *            img: source image
70  *   width,height: dimension of image
71  *            def: default value if coordinates are out of range
72  * Return value:  None
73  */
74 typedef void (*vsInterpolateFun)(uint8_t *rv, int32_t x, int32_t y,
75                                  const uint8_t *img, int linesize,
76                                  int width, int height, uint8_t def);
77 
78 typedef struct _VSTransformConfig {
79 
80     /* whether to consider transforms as relative (to previous frame)
81      * or absolute transforms
82      */
83     int            relative;
84     /* number of frames (forward and backward)
85      * to use for smoothing transforms */
86     int            smoothing;
87     VSBorderType   crop;        // 1: black bg, 0: keep border from last frame(s)
88     int            invert;      // 1: invert transforms, 0: nothing
89     double         zoom;        // percentage to zoom: 0->no zooming 10:zoom in 10%
90     int            optZoom;     // 2: optimal adaptive zoom 1: optimal static zoom, 0: nothing
91     double         zoomSpeed;   // for adaptive zoom: zoom per frame in percent
92     VSInterpolType interpolType; // type of interpolation: 0->Zero,1->Lin,2->BiLin,3->Sqr
93     int            maxShift;    // maximum number of pixels we will shift
94     double         maxAngle;    // maximum angle in rad
95     const char*    modName;     // module name (used for logging)
96     int            verbose;     // level of logging
97     // if 1 then the simple but fast method to termine the global motion is used
98     int            simpleMotionCalculation;
99     int            storeTransforms; // stores calculated transforms to file
100     int            smoothZoom;   // if 1 the zooming is also smoothed. Typically not recommended.
101     VSCamPathAlgo  camPathAlgo;  // algorithm to use for camera path optimization
102 } VSTransformConfig;
103 
104 typedef struct _VSTransformData {
105     VSFrameInfo fiSrc;
106     VSFrameInfo fiDest;
107 
108     VSFrame src;         // copy of the current frame buffer
109     VSFrame destbuf;     // pointer to an additional buffer or
110                          // to the destination buffer (depending on crop)
111     VSFrame dest;        // pointer to the destination buffer
112 
113     short srcMalloced;   // 1 if the source buffer was internally malloced
114 
115     vsInterpolateFun interpolate; // pointer to interpolation function
116 #ifdef TESTING
117     _FLT(vsInterpolateFun) _FLT(interpolate);
118 #endif
119 
120     /* Options */
121     VSTransformConfig conf;
122 
123     int initialized; // 1 if initialized and 2 if configured
124 } VSTransformData;
125 
126 
127 static const char vs_transform_help[] = ""
128     "Overview\n"
129     "    Reads a file with transform information for each frame\n"
130     "     and applies them. See also filter stabilize.\n"
131     "Options\n"
132     "    'input'     path to the file used to read the transforms\n"
133     "                (def: inputfile.trf)\n"
134     "    'smoothing' number of frames*2 + 1 used for lowpass filtering \n"
135     "                used for stabilizing (def: 10)\n"
136     "    'maxshift'  maximal number of pixels to translate image\n"
137     "                (def: -1 no limit)\n"
138     "    'maxangle'  maximal angle in rad to rotate image (def: -1 no limit)\n"
139     "    'crop'      0: keep border (def), 1: black background\n"
140     "    'invert'    1: invert transforms(def: 0)\n"
141     "    'relative'  consider transforms as 0: absolute, 1: relative (def)\n"
142     "    'zoom'      percentage to zoom >0: zoom in, <0 zoom out (def: 0)\n"
143     "    'optzoom'   0: nothing, 1: determine optimal static zoom (def)\n"
144     "                i.e. no (or only little) border should be visible.\n"
145     "                2: determine optimal adaptive zoom\n"
146     "                Note that the value given at 'zoom' is added to the \n"
147     "                here calculated one\n"
148     "    'zoomspeed' for adaptive zoom: zoom per frame in percent \n"
149     "    'interpol'  type of interpolation: 0: no interpolation, \n"
150     "                1: linear (horizontal), 2: bi-linear (def), \n"
151     "                3: bi-cubic\n"
152     "    'sharpen'   amount of sharpening: 0: no sharpening (def: 0.8)\n"
153     "                uses filter unsharp with 5x5 matrix\n"
154     "    'tripod'    virtual tripod mode (=relative=0:smoothing=0)\n"
155     "    'help'      print this help message\n";
156 
157 /** returns the default config
158  */
159 VSTransformConfig vsTransformGetDefaultConfig(const char* modName);
160 
161 /** initialized the VSTransformData structure using the config and allocates memory
162  *  for the frames and stuff
163  *  @return VS_OK on success otherwise VS_ERROR
164  */
165 int vsTransformDataInit(VSTransformData* td, const VSTransformConfig* conf,
166                         const VSFrameInfo* fi_src, const VSFrameInfo* fi_dest);
167 
168 
169 /** Deletes internal data structures.
170  * In order to use the VSTransformData again, you have to call vsTransformDataInit
171  */
172 void vsTransformDataCleanup(VSTransformData* td);
173 
174 /// returns the current config
175 void vsTransformGetConfig(VSTransformConfig* conf, const VSTransformData* td);
176 
177 /// returns the frame info for the src
178 const VSFrameInfo* vsTransformGetSrcFrameInfo(const VSTransformData* td);
179 /// returns the frame info for the dest
180 const VSFrameInfo* vsTransformGetDestFrameInfo(const VSTransformData* td);
181 
182 
183 /// initializes VSTransformations structure
184 void vsTransformationsInit(VSTransformations* trans);
185 /// deletes VSTransformations internal memory
186 void vsTransformationsCleanup(VSTransformations* trans);
187 
188 /// return next Transform and increases internal counter
189 VSTransform vsGetNextTransform(const VSTransformData* td, VSTransformations* trans);
190 
191 /** preprocesses the list of transforms all at once. Here the deshaking is calculated!
192  */
193 int vsPreprocessTransforms(VSTransformData* td, VSTransformations* trans);
194 
195 /**
196  * vsLowPassTransforms: single step smoothing of transforms, using only the past.
197  *  see also vsPreprocessTransforms. */
198 VSTransform vsLowPassTransforms(VSTransformData* td, VSSlidingAvgTrans* mem,
199                             const VSTransform* trans);
200 
201 /** call this function to prepare for a next transformation (transformPacked/transformPlanar)
202     and supply the src frame buffer and the frame to write to. These can be the same pointer
203     for an inplace operation (working on framebuffer directly)
204  */
205 int vsTransformPrepare(VSTransformData* td, const VSFrame* src, VSFrame* dest);
206 
207 /// does the actual transformation
208 int vsDoTransform(VSTransformData* td, VSTransform t);
209 
210 
211 /** call this function to finish the transformation of a frame (transformPacked/transformPlanar)
212  */
213 int vsTransformFinish(VSTransformData* td);
214 
215 
216 #endif
217 
218 /*
219  * Local variables:
220  *   c-file-style: "stroustrup"
221  *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
222  *   indent-tabs-mode: nil
223  * End:
224  *
225  * vim: expandtab shiftwidth=4:
226  */
227