forked from georgmartius/vid.stab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformtype.c
489 lines (446 loc) · 13 KB
/
transformtype.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
* transformtype.c
*
* Copyright (C) Georg Martius - June 2007
*
* This file is part of transcode, a video stream processing tool
*
* transcode is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* transcode is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "transformtype.h"
#include "transformtype_operations.h"
#include "vidstabdefines.h"
/***********************************************************************
* helper functions to create and operate with transforms.
* all functions are non-destructive
*/
/* create an initialized transform*/
VSTransform new_transform(double x, double y, double alpha,
double zoom, double barrel, double rshutter, int extra)
{
VSTransform t;
t.x = x;
t.y = y;
while (alpha < -M_PI){
alpha += 2.0*M_PI;
}
while (alpha > M_PI){
alpha -= 2.0*M_PI;
}
t.alpha = alpha;
t.zoom = zoom;
t.barrel = barrel;
t.rshutter = rshutter;
t.extra = extra;
return t;
}
/* create a zero initialized transform*/
VSTransform null_transform(void)
{
return new_transform(0, 0, 0, 0, 0, 0, 0);
}
/* adds two transforms */
VSTransform add_transforms(const VSTransform* t1, const VSTransform* t2)
{
VSTransform t;
t.x = t1->x + t2->x;
t.y = t1->y + t2->y;
t.alpha = t1->alpha + t2->alpha;
t.zoom = t1->zoom + t2->zoom;
t.barrel = t1->barrel + t2->barrel;
t.rshutter = t1->rshutter + t2->rshutter;
t.extra = t1->extra || t2->extra;
return t;
}
/* like add_transform but with non-pointer signature */
VSTransform add_transforms_(const VSTransform t1, const VSTransform t2)
{
return add_transforms(&t1, &t2);
}
/* subtracts two transforms */
VSTransform sub_transforms(const VSTransform* t1, const VSTransform* t2)
{
VSTransform t;
t.x = t1->x - t2->x;
t.y = t1->y - t2->y;
t.alpha = t1->alpha - t2->alpha;
t.zoom = t1->zoom - t2->zoom;
t.barrel = t1->barrel - t2->barrel;
t.rshutter = t1->rshutter - t2->rshutter;
t.extra = t1->extra || t2->extra;
return t;
}
/* multiplies a transforms with a scalar */
VSTransform mult_transform(const VSTransform* t1, double f)
{
VSTransform t;
t.x = t1->x * f;
t.y = t1->y * f;
t.alpha = t1->alpha * f;
t.zoom = t1->zoom * f;
t.barrel = t1->barrel * f;
t.rshutter = t1->rshutter * f;
t.extra = t1->extra;
return t;
}
/* like mult_transform but with non-pointer signature */
VSTransform mult_transform_(const VSTransform t1, double f)
{
return mult_transform(&t1,f);
}
void storeVSTransform(FILE* f, const VSTransform* t){
fprintf(f,"Trans %lf %lf %lf %lf %i\n", t->x, t->y, t->alpha, t->zoom, t->extra);
}
PreparedTransform prepare_transform(const VSTransform* t, const VSFrameInfo* fi){
PreparedTransform pt;
pt.t = t;
double z = 1.0+t->zoom/100.0;
pt.zcos_a = z*cos(t->alpha); // scaled cos
pt.zsin_a = z*sin(t->alpha); // scaled sin
pt.c_x = fi->width / 2;
pt.c_y = fi->height / 2;
return pt;
}
Vec transform_vec(const PreparedTransform* pt, const Vec* v){
double x,y;
transform_vec_double(&x, &y, pt, v);
Vec res = {x,y};
return res;
}
void transform_vec_double(double* x, double* y, const PreparedTransform* pt, const Vec* v){
double rx = v->x - pt->c_x;
double ry = v->y - pt->c_y;
*x = pt->zcos_a * rx + pt->zsin_a * ry + pt->t->x + pt->c_x;
*y = -pt->zsin_a * rx + pt->zcos_a * ry + pt->t->y + pt->c_y;
}
Vec sub_vec(Vec v1, Vec v2){
Vec r = {v1.x - v2.x, v1.y - v2.y};
return r;
}
Vec add_vec(Vec v1, Vec v2){
Vec r = {v1.x + v2.x, v1.y + v2.y};
return r;
}
Vec field_to_vec(Field f){
Vec r = {f.x , f.y};
return r;
}
/* compares a transform with respect to x (for sort function) */
int cmp_trans_x(const void *t1, const void* t2)
{
double a = ((VSTransform*)t1)->x;
double b = ((VSTransform*)t2)->x;
return a < b ? -1 : ( a > b ? 1 : 0 );
}
/* compares a transform with respect to y (for sort function) */
int cmp_trans_y(const void *t1, const void* t2)
{
double a = ((VSTransform*)t1)->y;
double b = ((VSTransform*)t2)->y;
return a < b ? -1 : ( a > b ? 1: 0 );
}
/* static int cmp_trans_alpha(const void *t1, const void* t2){ */
/* double a = ((VSTransform*)t1)->alpha; */
/* double b = ((VSTransform*)t2)->alpha; */
/* return a < b ? -1 : ( a > b ? 1 : 0 ); */
/* } */
/* compares two double values (for sort function)*/
int cmp_double(const void *t1, const void* t2)
{
double a = *((double*)t1);
double b = *((double*)t2);
return a < b ? -1 : ( a > b ? 1 : 0 );
}
/* compares two int values (for sort function)*/
int cmp_int(const void *t1, const void* t2)
{
int a = *((int*)t1);
int b = *((int*)t2);
return a < b ? -1 : ( a > b ? 1 : 0 );
}
/**
* median_xy_transform: calulcates the median of an array
* of transforms, considering only x and y
*
* Parameters:
* transforms: array of transforms.
* len: length of array
* Return value:
* A new transform with x and y beeing the median of
* all transforms. alpha and other fields are 0.
* Preconditions:
* len>0
* Side effects:
* None
*/
VSTransform median_xy_transform(const VSTransform* transforms, int len)
{
VSTransform* ts = vs_malloc(sizeof(VSTransform) * len);
VSTransform t = null_transform();
memcpy(ts,transforms, sizeof(VSTransform)*len );
int half = len/2;
qsort(ts, len, sizeof(VSTransform), cmp_trans_x);
t.x = len % 2 == 0 ? ts[half].x : (ts[half].x + ts[half+1].x)/2;
qsort(ts, len, sizeof(VSTransform), cmp_trans_y);
t.y = len % 2 == 0 ? ts[half].y : (ts[half].y + ts[half+1].y)/2;
vs_free(ts);
return t;
}
/**
* cleanmean_xy_transform: calulcates the cleaned mean of an array
* of transforms, considering only x and y
*
* Parameters:
* transforms: array of transforms.
* len: length of array
* Return value:
* A new transform with x and y beeing the cleaned mean
* (meaning upper and lower pentile are removed) of
* all transforms. alpha and other fields are 0.
* Preconditions:
* len>0
* Side effects:
* None
*/
VSTransform cleanmean_xy_transform(const VSTransform* transforms, int len)
{
VSTransform* ts = vs_malloc(sizeof(VSTransform) * len);
VSTransform t = null_transform();
int i, cut = len / 5;
memcpy(ts, transforms, sizeof(VSTransform) * len);
qsort(ts,len, sizeof(VSTransform), cmp_trans_x);
for (i = cut; i < len - cut; i++){ // all but cutted
t.x += ts[i].x;
}
qsort(ts, len, sizeof(VSTransform), cmp_trans_y);
for (i = cut; i < len - cut; i++){ // all but cutted
t.y += ts[i].y;
}
vs_free(ts);
return mult_transform(&t, 1.0 / (len - (2.0 * cut)));
}
/**
* calulcates the cleaned maximum and minimum of an array of transforms,
* considerung only x and y
* It cuts off the upper and lower x-th percentil
*
* Parameters:
* transforms: array of transforms.
* len: length of array
* percentil: the x-th percentil to cut off
* min: pointer to min (return value)
* max: pointer to max (return value)
* Return value:
* call by reference in min and max
* Preconditions:
* len>0, 0<=percentil<50
* Side effects:
* only on min and max
*/
void cleanmaxmin_xy_transform(const VSTransform* transforms, int len,
int percentil,
VSTransform* min, VSTransform* max){
VSTransform* ts = vs_malloc(sizeof(VSTransform) * len);
int cut = len * percentil / 100;
memcpy(ts, transforms, sizeof(VSTransform) * len);
qsort(ts,len, sizeof(VSTransform), cmp_trans_x);
min->x = ts[cut].x;
max->x = ts[len-cut-1].x;
qsort(ts, len, sizeof(VSTransform), cmp_trans_y);
min->y = ts[cut].y;
max->y = ts[len-cut-1].y;
vs_free(ts);
}
/* calculates the required zoom value to have no borders visible
*/
double transform_get_required_zoom(const VSTransform* transform, int width, int height){
return 100.0*(2.0*VS_MAX(fabs(transform->x)/width,fabs(transform->y)/height) // translation part
+ fabs(sin(transform->alpha))); // rotation part
}
/**
* media: median of a double array
*
* Parameters:
* ds: array of values
* len: length of array
* Return value:
* the median value of the array
* Preconditions: len>0
* Side effects: ds will be sorted!
*/
double median(double* ds, int len)
{
int half=len/2;
qsort(ds,len, sizeof(double), cmp_double);
return len % 2 == 0 ? ds[half] : (ds[half] + ds[half+1])/2;
}
/** square of a number */
double sqr(double x){ return x*x; }
/**
* mean: mean of a double array
*
* Parameters:
* ds: array of values
* len: length of array
* Return value: the mean value of the array
* Preconditions: len>0
* Side effects: None
*/
double mean(const double* ds, int len)
{
double sum=0;
int i = 0;
for (i = 0; i < len; i++)
sum += ds[i];
return sum / len;
}
/**
* stddev: standard deviation of a double array
*
* Parameters:
* ds: array of values
* len: length of array
* mean: mean of the array (@see mean())
* Return value: the standard deviation value of the array
* Preconditions: len>0
* Side effects: None
*/
double stddev(const double* ds, int len, double mean)
{
double sum=0;
int i = 0;
for (i = 0; i < len; i++)
sum += sqr(ds[i]-mean);
return sqrt(sum / len);
}
/**
* cleanmean: mean with cutted upper and lower pentile
*
* Parameters:
* ds: array of values
* len: length of array
* minimum: minimal value (after cleaning) if not NULL
* maximum: maximal value (after cleaning) if not NULL
* Return value:
* the mean value of the array without the upper
* and lower pentile (20% each)
* and minimum and maximum without the pentiles
* Preconditions: len>0
* Side effects: ds will be sorted!
*/
double cleanmean(double* ds, int len, double* minimum, double* maximum)
{
int cut = len / 5;
double sum = 0;
int i = 0;
qsort(ds, len, sizeof(double), cmp_double);
for (i = cut; i < len - cut; i++) { // all but first and last
sum += ds[i];
}
if (minimum)
*minimum = ds[cut];
if (maximum)
*maximum = ds[len-cut-1];
return sum / (len - (2.0 * cut));
}
/************************************************/
/***************LOCALMOTION**********************/
LocalMotion null_localmotion(){
LocalMotion lm;
memset(&lm,0,sizeof(lm));
return lm;
}
int* localmotions_getx(const LocalMotions* localmotions){
int len = vs_vector_size(localmotions);
int* xs = vs_malloc(sizeof(int) * len);
int i;
for (i=0; i<len; i++){
xs[i]=LMGet(localmotions,i)->v.x;
}
return xs;
}
int* localmotions_gety(const LocalMotions* localmotions){
int len = vs_vector_size(localmotions);
int* ys = vs_malloc(sizeof(int) * len);
int i;
for (i=0; i<len; i++){
ys[i]=LMGet(localmotions,i)->v.y;
}
return ys;
}
LocalMotion sub_localmotion(const LocalMotion* lm1, const LocalMotion* lm2){
LocalMotion res = *lm1;
res.v.x -= lm2->v.x;
res.v.y -= lm2->v.y;
return res;
}
/**
* cleanmean_localmotions: calulcates the cleaned mean of a vector
* of local motions considering
*
* Parameters:
* localmotions : vs_vector of local motions
* Return value:
* A localmotion with vec with x and y being the cleaned mean
* (meaning upper and lower pentile are removed) of
* all local motions. all other fields are 0.
* Preconditions:
* size of vector >0
* Side effects:
* None
*/
LocalMotion cleanmean_localmotions(const LocalMotions* localmotions)
{
int len = vs_vector_size(localmotions);
int i, cut = len / 5;
int* xs = localmotions_getx(localmotions);
int* ys = localmotions_gety(localmotions);
LocalMotion m = null_localmotion();
m.v.x=0; m.v.y=0;
qsort(xs,len, sizeof(int), cmp_int);
for (i = cut; i < len - cut; i++){ // all but cutted
m.v.x += xs[i];
}
qsort(ys, len, sizeof(int), cmp_int);
for (i = cut; i < len - cut; i++){ // all but cutted
m.v.y += ys[i];
}
vs_free(xs);
vs_free(ys);
m.v.x/=(len - (2.0 * cut));
m.v.y/=(len - (2.0 * cut));
return m;
}
VSArray localmotionsGetMatch(const LocalMotions* localmotions){
VSArray m = vs_array_new(vs_vector_size(localmotions));
for (int i=0; i<m.len; i++){
m.dat[i]=LMGet(localmotions,i)->match;
}
return m;
}
/*
* Local variables:
* c-file-style: "stroustrup"
* c-file-offsets: ((case-label . *) (statement-case-intro . *))
* indent-tabs-mode: nil
* c-basic-offset: 2 t
* End:
*
* vim: expandtab shiftwidth=2:
*/