]> git.sesse.net Git - mlt/blob - src/modules/videostab/stabilize.h
Fix compile error on Windows.
[mlt] / src / modules / videostab / stabilize.h
1 /*
2  *  filter_stabilize.c
3  *
4  *  Copyright (C) Georg Martius - June 2007
5  *   georg dot martius at web dot de  
6  *
7  *  This file is part of transcode, a video stream processing tool
8  *      
9  *  transcode is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2, or (at your option)
12  *  any later version.
13  *   
14  *  transcode 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
25 /* Typical call:
26  *  transcode -V -J stabilize=shakiness=5:show=1,preview 
27  *         -i inp.mpeg -y null,null -o dummy
28  *  all parameters are optional
29 */
30
31 #define MOD_NAME    "filter_stabilize.so"
32 #define MOD_VERSION "v0.75 (2010-04-07)"
33 #define MOD_CAP     "extracts relative transformations of \n\
34     subsequent frames (used for stabilization together with the\n\
35     transform filter in a second pass)"
36 #define MOD_AUTHOR  "Georg Martius"
37
38 /* Ideas:
39  - Try OpenCL/Cuda, this should work great
40  - use smoothing on the frames and then use gradient decent!
41  - stepsize could be adapted (maybe to check only one field with large
42    stepsize and use the maximally required for the other fields
43 */
44
45 #define MOD_FEATURES \
46     TC_MODULE_FEATURE_FILTER|TC_MODULE_FEATURE_VIDEO
47 #define MOD_FLAGS  \
48     TC_MODULE_FLAG_RECONFIGURABLE | TC_MODULE_FLAG_DELAY
49   
50
51 #include "transform.h"
52
53 #include <math.h>
54 #include <libgen.h>
55 #include <stdio.h>
56 #include "tlist.h"
57 #include <framework/mlt_types.h>
58
59 /* if defined we are very verbose and generate files to analyse
60  * this is really just for debugging and development */
61 // #define STABVERBOSE
62
63
64 typedef struct _field {
65     int x;     // middle position x
66     int y;     // middle position y
67     int size;  // size of field
68 } Field;
69
70 // structure that contains the contrast and the index of a field
71 typedef struct _contrast_idx {
72     double contrast;
73     int index;
74 } contrast_idx;
75
76 /* private date structure of this filter*/
77 typedef struct _stab_data {
78     int framesize;  // size of frame buffer in bytes (prev)
79     unsigned char* curr; // current frame buffer (only pointer)
80     unsigned char* currcopy; // copy of the current frame needed for drawing
81     unsigned char* prev; // frame buffer for last frame (copied)
82     unsigned char* grayimage; // frame buffer for last frame (copied)
83     short hasSeenOneFrame; // true if we have a valid previous frame
84
85     int width, height;
86         mlt_image_format pixelformat;
87
88     /* list of transforms*/
89     //TCList* transs;
90     tlist* transs;
91
92     Field* fields;
93
94
95     /* Options */
96     /* maximum number of pixels we expect the shift of subsequent frames */
97     int maxshift; 
98     int stepsize; // stepsize of field transformation detection
99     int allowmax; // 1 if maximal shift is allowed
100     int algo;     // algorithm to use
101     int field_num;  // number of measurement fields
102     int maxfields;  // maximum number of fields used (selected by contrast)
103     int field_size; // size    = min(sd->width, sd->height)/10;
104     int field_rows; // number of rows
105     /* if 1 and 2 then the fields and transforms are shown in the frames */
106     int show; 
107     /* measurement fields with lower contrast are discarded */
108     double contrast_threshold;            
109     /* maximal difference in angles of fields */
110     double maxanglevariation;
111     /* meta parameter for maxshift and fieldsize between 1 and 10 */
112     int shakiness;   
113     int accuracy;   // meta parameter for number of fields between 1 and 10
114   
115     int t;
116
117     char conf_str[1024];
118 } StabData;
119
120 /* type for a function that calculates the transformation of a certain field 
121  */
122 typedef Transform (*calcFieldTransFunc)(StabData*, const Field*, int);
123
124 /* type for a function that calculates the contrast of a certain field 
125  */
126 typedef double (*contrastSubImgFunc)(StabData* sd, const Field* field);
127
128 static const char stabilize_help[] = ""
129     "Overview:\n"
130     "    Generates a file with relative transform information\n"
131     "     (translation, rotation) about subsequent frames."
132     " See also transform.\n" 
133     "Options\n"
134     "    'result'      path to the file used to write the transforms\n"
135     "                  (def:inputfile.stab)\n"
136     "    'shakiness'   how shaky is the video and how quick is the camera?\n"
137     "                  1: little (fast) 10: very strong/quick (slow) (def: 4)\n"
138     "    'accuracy'    accuracy of detection process (>=shakiness)\n"
139     "                  1: low (fast) 15: high (slow) (def: 4)\n"
140     "    'stepsize'    stepsize of search process, region around minimum \n"
141     "                  is scanned with 1 pixel resolution (def: 6)\n"
142     "    'algo'        0: brute force (translation only);\n"
143     "                  1: small measurement fields (def)\n"
144     "    'mincontrast' below this contrast a field is discarded (0-1) (def: 0.3)\n"
145     "    'show'        0: draw nothing (def); 1,2: show fields and transforms\n"
146     "                  in the resulting frames. Consider the 'preview' filter\n"
147     "    'help'        print this help message\n";
148
149 int initFields(StabData* sd);
150 double compareImg(unsigned char* I1, unsigned char* I2, 
151                   int width, int height,  int bytesPerPixel, int d_x, int d_y);
152 double compareSubImg(unsigned char* const I1, unsigned char* const I2, 
153                      const Field* field, 
154                      int width, int height, int bytesPerPixel,int d_x,int d_y);
155 double contrastSubImgYUV(StabData* sd, const Field* field);
156 double contrastSubImgRGB(StabData* sd, const Field* field);
157 double contrastSubImg(unsigned char* const I, const Field* field, 
158                       int width, int height, int bytesPerPixel);
159 int cmp_contrast_idx(const void *ci1, const void* ci2);
160 tlist* selectfields(StabData* sd, contrastSubImgFunc contrastfunc);
161
162 Transform calcShiftRGBSimple(StabData* sd);
163 Transform calcShiftYUVSimple(StabData* sd);
164 double calcAngle(StabData* sd, Field* field, Transform* t,
165                  int center_x, int center_y);
166 Transform calcFieldTransYUV(StabData* sd, const Field* field, 
167                             int fieldnum);
168 Transform calcFieldTransRGB(StabData* sd, const Field* field, 
169                             int fieldnum);
170 Transform calcTransFields(StabData* sd, calcFieldTransFunc fieldfunc,
171                           contrastSubImgFunc contrastfunc);
172
173
174 void drawFieldScanArea(StabData* sd, const Field* field, const Transform* t);
175 void drawField(StabData* sd, const Field* field, const Transform* t);
176 void drawFieldTrans(StabData* sd, const Field* field, const Transform* t);
177 void drawBox(unsigned char* I, int width, int height, int bytesPerPixel, 
178              int x, int y, int sizex, int sizey, unsigned char color);
179 void addTrans(StabData* sd, Transform sl);
180
181 int stabilize_configure(StabData* instance);
182 int stabilize_stop(StabData* instance);
183
184 int stabilize_filter_video(StabData* instance, unsigned char *frame,mlt_image_format imageformat);
185
186