4 * Copyright (C) Georg Martius - June 2007
5 * georg dot martius at web dot de
7 * This file is part of transcode, a video stream processing tool
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)
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.
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.
26 * transcode -V -J stabilize=shakiness=5:show=1,preview
27 * -i inp.mpeg -y null,null -o dummy
28 * all parameters are optional
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"
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
45 #define MOD_FEATURES \
46 TC_MODULE_FEATURE_FILTER|TC_MODULE_FEATURE_VIDEO
48 TC_MODULE_FLAG_RECONFIGURABLE | TC_MODULE_FLAG_DELAY
51 #include "transform.h"
57 #include <framework/mlt_types.h>
59 /* if defined we are very verbose and generate files to analyse
60 * this is really just for debugging and development */
61 // #define STABVERBOSE
64 typedef struct _field {
65 int x; // middle position x
66 int y; // middle position y
67 int size; // size of field
70 // structure that contains the contrast and the index of a field
71 typedef struct _contrast_idx {
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
86 mlt_image_format pixelformat;
88 /* list of transforms*/
96 /* maximum number of pixels we expect the shift of subsequent frames */
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 */
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 */
113 int accuracy; // meta parameter for number of fields between 1 and 10
120 /* type for a function that calculates the transformation of a certain field
122 typedef Transform (*calcFieldTransFunc)(StabData*, const Field*, int);
124 /* type for a function that calculates the contrast of a certain field
126 typedef double (*contrastSubImgFunc)(StabData* sd, const Field* field);
128 static const char stabilize_help[] = ""
130 " Generates a file with relative transform information\n"
131 " (translation, rotation) about subsequent frames."
132 " See also transform.\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";
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,
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);
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,
168 Transform calcFieldTransRGB(StabData* sd, const Field* field,
170 Transform calcTransFields(StabData* sd, calcFieldTransFunc fieldfunc,
171 contrastSubImgFunc contrastfunc);
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);
181 int stabilize_configure(StabData* instance);
182 int stabilize_stop(StabData* instance);
184 int stabilize_filter_video(StabData* instance, unsigned char *frame,mlt_image_format imageformat);