]> git.sesse.net Git - mlt/blob - src/modules/videostab/stab/estimate.c
fix to compile an MAC (point 1)
[mlt] / src / modules / videostab / stab / estimate.c
1 /*
2  * Video stabilizer
3  *
4  * Copyright (c) 2008 Lenny <leonardo.masoni@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <math.h>
20 #include <string.h>
21 #if !defined(__DARWIN__)
22 #include <values.h>
23 #endif
24
25 #include "estimate.h"
26 #include "vector.h"
27 #include "utils.h"
28
29 es_ctx *es_init(int nc, int nr) {
30
31     es_ctx *es = (es_ctx *)malloc(sizeof(es_ctx));
32
33     es->tc = KLTCreateTrackingContext();
34
35     es->tc->sequentialMode = TRUE;
36     es->tc->min_eigenvalue = 8;
37     es->tc->verbose = FALSE;
38
39     KLTChangeTCPyramid(es->tc, 31);
40
41     KLTUpdateTCBorder(es->tc);
42
43     es->fr[0] = (KLT_PixelType *)malloc(nc * nr * sizeof(KLT_PixelType));
44     es->fr[1] = (KLT_PixelType *)malloc(nc * nr * sizeof(KLT_PixelType));
45
46     es->fl = KLTCreateFeatureList(64);
47
48     es->dv = (vc *)malloc(64 * sizeof(vc));
49     es->nv = 0;
50
51     es->nc = nc;
52     es->nr = nr;
53     
54     es->ff = FALSE;
55
56     return es;
57 }
58
59 vc es_estimate(es_ctx *es, unsigned char *fr) {
60
61     KLT_PixelType *t;
62     int is, id;
63
64     t = es->fr[0]; es->fr[0] = es->fr[1]; es->fr[1] = t;
65
66     for (is = 0, id = 0; id < es->nc * es->nr; is += 3, id ++)
67         es->fr[1][id] = (fr[is + 0] * 30 + fr[is + 1] * 59 + fr[is + 2] * 11) / 100;
68
69     if (es->ff == FALSE) {
70
71         es->ff = TRUE;
72
73     } else {
74
75         vc bv = vc_set(0.0, 0.0);
76         float be = MAXFLOAT;
77
78         int i, i2;
79
80         KLTSelectGoodFeatures(
81             es->tc, es->fr[0], es->nc, es->nr, es->fl
82             );
83             
84         for (i = 0; i < es->fl->nFeatures; i ++)
85             es->dv[i] = vc_set(es->fl->feature[i]->x, es->fl->feature[i]->y);
86
87         KLTTrackFeatures(
88             es->tc, es->fr[0], es->fr[1], es->nc, es->nr, es->fl
89             );
90
91         es->nv = 0;
92
93         for (i = 0; i < es->fl->nFeatures; i ++) {
94
95             if (es->fl->feature[i]->val == KLT_TRACKED) {
96             
97                 es->dv[es->nv] = vc_set(
98                     es->fl->feature[i]->x - es->dv[i].x,
99                     es->fl->feature[i]->y - es->dv[i].y
100                     );            
101
102                 es->nv ++;
103             }
104         }
105
106         for (i = 0; i < es->nv; i ++) {
107
108             float ce = 0.0;
109
110             for (i2 = 0; i2 < es->nv; i2 ++)
111                 ce += vc_len(vc_sub(es->dv[i2], es->dv[i]));
112             
113             if (ce < be) {
114             
115                 bv = es->dv[i];
116                 be = ce;
117             }
118         }
119
120         return bv;
121     }
122     
123     return vc_zero();
124 }
125
126 void es_free(es_ctx *es) {
127
128     free(es->dv);
129
130     free(es->fr[0]);
131     free(es->fr[1]);
132     
133     KLTFreeFeatureList(es->fl);
134     KLTFreeTrackingContext(es->tc);
135     
136     free(es);
137 }
138