forked from georgmartius/vid.stab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.c
562 lines (506 loc) · 16.4 KB
/
serialize.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
* serialize.c
*
* Copyright (C) Georg Martius - January 2013
* georg dot martius at web dot de
*
* This file is part of vid.stab video stabilization library
*
* vid.stab 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.
*
* vid.stab 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 <assert.h>
#include <string.h>
#include "serialize.h"
#include "transformtype.h"
#include "transformtype_operations.h"
#include "motiondetect.h"
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
defined(__BIG_ENDIAN__) || \
defined(__ARMEB__) || \
defined(__THUMBEB__) || \
defined(__AARCH64EB__) || \
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
// It's a big-endian target architecture
#define __IS_BIG_ENDIAN__
#ifdef __APPLE__
// Make sure that byte swap functions are not already defined.
#if !defined(bswap_16)
// Mac OS X / Darwin features
#include <libkern/OSByteOrder.h>
#define __bswap_16(x) OSSwapInt16(x)
#define __bswap_32(x) OSSwapInt32(x)
#define byteSwapDouble(x) OSSwapInt64(x)
#endif
#else
#include <byteswap.h>
static double byteSwapDouble(double v)
{
char in[8], out[8];
double result;
memcpy(in, &v, 8);
out[0] = in[7];
out[1] = in[6];
out[2] = in[5];
out[3] = in[4];
out[4] = in[3];
out[5] = in[2];
out[6] = in[1];
out[7] = in[0];
memcpy(&result, out, 8);
return result;
}
#endif
#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
defined(__LITTLE_ENDIAN__) || \
defined(__ARMEL__) || \
defined(__THUMBEL__) || \
defined(__AARCH64EL__) || \
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
// It's a little-endian target architecture
#define __IS_LITTLE_ENDIAN__
#else
#error "I don't know what architecture this is!"
#endif
const char* modname = "vid.stab - serialization";
int vsPrepareFileText(const VSMotionDetect* md, FILE* f);
int vsPrepareFileBinary(const VSMotionDetect* md, FILE* f);
int vsWriteToFileText(const VSMotionDetect* md, FILE* f, const LocalMotions* lms);
int vsWriteToFileBinary(const VSMotionDetect* md, FILE* f, const LocalMotions* lms);
int vsStoreLocalmotionsText(FILE* f, const LocalMotions* lms);
int vsStoreLocalmotionsBinary(FILE* f, const LocalMotions* lms);
int storeLocalmotionText(FILE* f, const LocalMotion* lm);
int storeLocalmotionBinary(FILE* f, const LocalMotion* lm);
LocalMotions vsRestoreLocalmotionsText(FILE* f);
LocalMotions vsRestoreLocalmotionsBinary(FILE* f);
LocalMotion restoreLocalmotionText(FILE* f);
LocalMotion restoreLocalmotionBinary(FILE* f);
int vsReadFileVersionText(FILE* f);
int vsReadFileVersionBinary(FILE* f);
int vsReadFromFileText(FILE* f, LocalMotions* lms);
int vsReadFromFileBinary(FILE* f, LocalMotions* lms);
int readInt16(int16_t* i, FILE* f){
int result = fread(i, sizeof(int16_t), 1, f);
#ifdef __IS_BIG_ENDIAN__
if(result>0) *i = __bswap_16(*i);
#endif
return result;
}
int readInt32(int32_t* i, FILE* f){
int result = fread(i, sizeof(int32_t), 1, f);
#ifdef __IS_BIG_ENDIAN__
if(result>0) *i = __bswap_32(*i);
#endif
return result;
}
int readDouble(double* d, FILE* f){
int result = fread(d, sizeof(double), 1, f);
#ifdef __IS_BIG_ENDIAN__
if(result>0) *d = byteSwapDouble(*d);
#endif
return result;
}
int writeInt16(const int16_t* i, FILE* f){
int16_t val = *i;
#ifdef __IS_BIG_ENDIAN__
val = __bswap_16(val);
#endif
return fwrite((const void*)&val, sizeof(int16_t), 1, f);
}
int writeInt32(const int32_t* i, FILE* f){
int32_t val = *i;
#ifdef __IS_BIG_ENDIAN__
val = __bswap_32(val);
#endif
return fwrite((const void*)&val, sizeof(int32_t), 1, f);
}
int writeDouble(const double* d, FILE* f){
double val = *d;
#ifdef __IS_BIG_ENDIAN__
val = byteSwapDouble(val);
#endif
return fwrite((const void*)&val, sizeof(double), 1, f);
}
int storeLocalmotion(FILE* f, const LocalMotion* lm, int serializationMode){
if(serializationMode == BINARY_SERIALIZATION_MODE){
return storeLocalmotionBinary(f, lm);
} else {
return storeLocalmotionText(f, lm);
}
}
int storeLocalmotionText(FILE* f, const LocalMotion* lm) {
return fprintf(f,"(LM %hi %hi %hi %hi %hi %lf %lf)", lm->v.x,lm->v.y,lm->f.x,lm->f.y,lm->f.size,
lm->contrast, lm->match);
}
int storeLocalmotionBinary(FILE* f, const LocalMotion* lm) {
if (writeInt16(&lm->v.x, f)<=0) return 0;
if (writeInt16(&lm->v.y, f)<=0) return 0;
if (writeInt16(&lm->f.x, f)<=0) return 0;
if (writeInt16(&lm->f.y, f)<=0) return 0;
if (writeInt16(&lm->f.size, f)<=0) return 0;
if (writeDouble(&lm->contrast, f)<=0) return 0;
if (writeDouble(&lm->match, f)<=0) return 0;
return 1;
}
/// restore local motion from file
LocalMotion restoreLocalmotion(FILE* f, const int serializationMode){
if(serializationMode == BINARY_SERIALIZATION_MODE) {
return restoreLocalmotionBinary(f);
} else {
return restoreLocalmotionText(f);
}
}
LocalMotion restoreLocalmotionText(FILE* f){
LocalMotion lm;
int c;
if(fscanf(f,"(LM %hi %hi %hi %hi %hi %lf %lf", &lm.v.x,&lm.v.y,&lm.f.x,&lm.f.y,&lm.f.size,
&lm.contrast, &lm.match) != 7) {
vs_log_error(modname, "Cannot parse localmotion!\n");
return null_localmotion();
}
while((c=fgetc(f)) && c!=')' && c!=EOF);
if(c==EOF){
vs_log_error(modname, "Cannot parse localmotion missing ')'!\n");
return null_localmotion();
}
return lm;
}
LocalMotion restoreLocalmotionBinary(FILE* f){
LocalMotion lm;
if (readInt16(&lm.v.x, f)<=0) goto parse_error_handling;
if (readInt16(&lm.v.y, f)<=0) goto parse_error_handling;
if (readInt16(&lm.f.x, f)<=0) goto parse_error_handling;
if (readInt16(&lm.f.y, f)<=0) goto parse_error_handling;
if (readInt16(&lm.f.size, f)<=0) goto parse_error_handling;
if (readDouble(&lm.contrast, f)<=0) goto parse_error_handling;
if (readDouble(&lm.match, f)<=0) goto parse_error_handling;
return lm;
parse_error_handling:
vs_log_error(modname, "Cannot parse localmotion!\n");
return null_localmotion();
}
int vsStoreLocalmotions(FILE* f, const LocalMotions* lms, const int serializationMode){
if(serializationMode == BINARY_SERIALIZATION_MODE) {
return vsStoreLocalmotionsBinary(f, lms);
} else {
return vsStoreLocalmotionsText(f, lms);
}
}
int vsStoreLocalmotionsText(FILE* f, const LocalMotions* lms){
int len = vs_vector_size(lms);
int i;
fprintf(f,"List %i [",len);
for (i=0; i<len; i++){
if(i>0) fprintf(f,",");
if(storeLocalmotion(f,LMGet(lms,i),ASCII_SERIALIZATION_MODE) <= 0) return 0;
}
fprintf(f,"]");
return 1;
}
int vsStoreLocalmotionsBinary(FILE* f, const LocalMotions* lms){
const int len = vs_vector_size(lms);
int i;
if(writeInt32(&len, f)<=0) return 0;
for (i=0; i<len; i++){
if(storeLocalmotion(f,LMGet(lms,i),BINARY_SERIALIZATION_MODE) <= 0) return 0;
}
return 1;
}
/// restores local motions from file
LocalMotions vsRestoreLocalmotions(FILE* f, const int serializationMode){
if(serializationMode == BINARY_SERIALIZATION_MODE) {
return vsRestoreLocalmotionsBinary(f);
} else {
return vsRestoreLocalmotionsText(f);
}
}
LocalMotions vsRestoreLocalmotionsText(FILE* f){
LocalMotions lms;
int i;
int c;
int len;
vs_vector_init(&lms,0);
if(fscanf(f,"List %i [", &len) != 1) {
vs_log_error(modname, "Cannot parse localmotions list expect 'List len ['!\n");
return lms;
}
if (len>0){
vs_vector_init(&lms,len);
for (i=0; i<len; i++){
if(i>0) while((c=fgetc(f)) && c!=',' && c!=EOF);
LocalMotion lm = restoreLocalmotion(f,ASCII_SERIALIZATION_MODE);
vs_vector_append_dup(&lms,&lm,sizeof(LocalMotion));
}
}
if(len != vs_vector_size(&lms)){
vs_log_error(modname, "Cannot parse the given number of localmotions!\n");
return lms;
}
while((c=fgetc(f)) && c!=']' && c!=EOF);
if(c==EOF){
vs_log_error(modname, "Cannot parse localmotions list missing ']'!\n");
return lms;
}
return lms;
}
LocalMotions vsRestoreLocalmotionsBinary(FILE* f){
LocalMotions lms;
int i;
int len;
vs_vector_init(&lms,0);
if(readInt32(&len, f) <= 0) {
vs_log_error(modname, "Cannot parse localmotions list!\n");
return lms;
}
if (len>0){
vs_vector_init(&lms,len);
for (i=0; i<len; i++){
LocalMotion lm = restoreLocalmotion(f,BINARY_SERIALIZATION_MODE);
vs_vector_append_dup(&lms,&lm,sizeof(LocalMotion));
}
}
if(len != vs_vector_size(&lms)){
vs_log_error(modname, "Cannot parse the given number of localmotions!\n");
}
return lms;
}
int vsPrepareFile(const VSMotionDetect* md, FILE* f){
if(md->serializationMode == BINARY_SERIALIZATION_MODE) {
return vsPrepareFileBinary(md, f);
} else {
return vsPrepareFileText(md, f);
}
}
int vsPrepareFileText(const VSMotionDetect* md, FILE* f){
if(!f) return VS_ERROR;
fprintf(f, "VID.STAB %i\n", LIBVIDSTAB_FILE_FORMAT_VERSION);
fprintf(f, "# accuracy = %d\n", md->conf.accuracy);
fprintf(f, "# shakiness = %d\n", md->conf.shakiness);
fprintf(f, "# stepsize = %d\n", md->conf.stepSize);
fprintf(f, "# mincontrast = %f\n", md->conf.contrastThreshold);
return VS_OK;
}
int vsPrepareFileBinary(const VSMotionDetect* md, FILE* f){
static const unsigned char kFileFormatVersion = LIBVIDSTAB_FILE_FORMAT_VERSION;
if(!f) return VS_ERROR;
fprintf(f, "TRF%hhu", kFileFormatVersion);
writeInt32(&md->conf.accuracy, f);
writeInt32(&md->conf.shakiness, f);
writeInt32(&md->conf.stepSize, f);
writeDouble(&md->conf.contrastThreshold, f);
return VS_OK;
}
int vsWriteToFile(const VSMotionDetect* md, FILE* f, const LocalMotions* lms){
if(md->serializationMode == BINARY_SERIALIZATION_MODE) {
return vsWriteToFileBinary(md, f, lms);
} else {
return vsWriteToFileText(md, f, lms);
}
}
int vsWriteToFileText(const VSMotionDetect* md, FILE* f, const LocalMotions* lms){
if(!f || !lms) return VS_ERROR;
if(fprintf(f, "Frame %i (", md->frameNum)>0
&& vsStoreLocalmotions(f, lms, ASCII_SERIALIZATION_MODE)>0 && fprintf(f, ")\n"))
return VS_OK;
else
return VS_ERROR;
}
int vsWriteToFileBinary(const VSMotionDetect* md, FILE* f, const LocalMotions* lms){
if(!f || !lms) return VS_ERROR;
if(writeInt32(&md->frameNum, f)<=0) return VS_ERROR;
if(vsStoreLocalmotions(f, lms, BINARY_SERIALIZATION_MODE)<=0) return VS_ERROR;
return VS_OK;
}
int vsGuessSerializationMode(FILE* f){
int serializationMode = ASCII_SERIALIZATION_MODE;
const int pos = ftell(f);
if(fgetc(f) == 'T'
&& fgetc(f) == 'R'
&& fgetc(f) == 'F') {
serializationMode = BINARY_SERIALIZATION_MODE;
}
fseek(f, pos, SEEK_SET);
return serializationMode;
}
/// reads the header of the file and return the version number
int vsReadFileVersion(FILE* f, const int serializationMode){
if(serializationMode == BINARY_SERIALIZATION_MODE) {
return vsReadFileVersionBinary(f);
} else {
return vsReadFileVersionText(f);
}
}
int vsReadFileVersionText(FILE* f){
if(!f) return VS_ERROR;
int version;
if(fscanf(f, "VID.STAB %i\n", &version)!=LIBVIDSTAB_FILE_FORMAT_VERSION)
return VS_ERROR;
else return version;
}
int vsReadFileVersionBinary(FILE* f){
if(!f) return VS_ERROR;
unsigned char version;
VSMotionDetectConfig conf;
if(fscanf(f, "TRF%hhu", &version)!=LIBVIDSTAB_FILE_FORMAT_VERSION) goto parse_error_handling;
if(readInt32(&conf.accuracy, f)<=0) goto parse_error_handling;
if(readInt32(&conf.shakiness, f)<=0) goto parse_error_handling;
if(readInt32(&conf.stepSize, f)<=0) goto parse_error_handling;
if(readDouble(&conf.contrastThreshold, f)<=0) goto parse_error_handling;
return version;
parse_error_handling:
return VS_ERROR;
}
int vsReadFromFile(FILE* f, LocalMotions* lms, const int serializationMode){
if(serializationMode == BINARY_SERIALIZATION_MODE) {
return vsReadFromFileBinary(f,lms);
} else {
return vsReadFromFileText(f,lms);
}
}
int vsReadFromFileText(FILE* f, LocalMotions* lms){
int c = fgetc(f);
if(c=='F') {
int num;
if(fscanf(f,"rame %i (", &num)!=1) {
vs_log_error(modname,"cannot read file, expect 'Frame num (...'");
return VS_ERROR;
}
*lms = vsRestoreLocalmotions(f,ASCII_SERIALIZATION_MODE);
if(fscanf(f,")\n")<0) {
vs_log_error(modname,"cannot read file, expect '...)'");
return VS_ERROR;
}
return num;
} else if(c=='#') {
char l[1024];
if(fgets(l, sizeof(l), f)==0) return VS_ERROR;
return vsReadFromFile(f,lms,ASCII_SERIALIZATION_MODE);
} else if(c=='\n' || c==' ') {
return vsReadFromFile(f,lms,ASCII_SERIALIZATION_MODE);
} else if(c==EOF) {
return VS_ERROR;
} else {
vs_log_error(modname,"cannot read frame local motions from file, got %c (%i)",
c, (int) c);
return VS_ERROR;
}
}
int vsReadFromFileBinary(FILE* f, LocalMotions* lms){
int frameNum;
if(readInt32(&frameNum, f)<=0) return VS_ERROR;
*lms = vsRestoreLocalmotions(f, BINARY_SERIALIZATION_MODE);
return frameNum;
}
int vsReadLocalMotionsFile(FILE* f, VSManyLocalMotions* mlms){
const int serializationMode = vsGuessSerializationMode(f);
int version = vsReadFileVersion(f, serializationMode);
if(version<1) // old format or unknown
return VS_ERROR;
if(version>1){
vs_log_error(modname,"Version of VID.STAB file too large: got %i, expect <= 1",
version);
return VS_ERROR;
}
assert(mlms);
// initial number of frames, but it will automatically be increaseed
vs_vector_init(mlms,1024);
int index;
int oldindex = 0;
LocalMotions lms;
while((index = vsReadFromFile(f,&lms,serializationMode)) != VS_ERROR){
if(index > oldindex+1){
vs_log_info(modname,"VID.STAB file: index of frames is not continuous %i -< %i",
oldindex, index);
}
if(index<1){
vs_log_info(modname,"VID.STAB file: Frame number < 1 (%i)", index);
} else {
vs_vector_set_dup(mlms,index-1,&lms, sizeof(LocalMotions));
}
oldindex=index;
}
return VS_OK;
}
/**
* vsReadOldTransforms: read transforms file (Deprecated format)
* The format is as follows:
* Lines with # at the beginning are comments and will be ignored
* Data lines have 5 columns seperated by space or tab containing
* time, x-translation, y-translation, alpha-rotation, extra
* where time and extra are integers
* and the latter is unused at the moment
*
* Parameters:
* f: file description
* trans: place to store the transforms
* Return value:
* number of transforms read
* Preconditions: f is opened
*/
int vsReadOldTransforms(const VSTransformData* td, FILE* f , VSTransformations* trans)
{
char l[1024];
int s = 0;
int i = 0;
int ti; // time (ignored)
VSTransform t;
while (fgets(l, sizeof(l), f)) {
t = null_transform();
if (l[0] == '#')
continue; // ignore comments
if (strlen(l) == 0)
continue; // ignore empty lines
// try new format
if (sscanf(l, "%i %lf %lf %lf %lf %i", &ti, &t.x, &t.y, &t.alpha,
&t.zoom, &t.extra) != 6) {
if (sscanf(l, "%i %lf %lf %lf %i", &ti, &t.x, &t.y, &t.alpha,
&t.extra) != 5) {
vs_log_error(td->conf.modName, "Cannot parse line: %s", l);
return 0;
}
t.zoom=0;
}
if (i>=s) { // resize transform array
if (s == 0)
s = 256;
else
s*=2;
/* vs_log_info(td->modName, "resize: %i\n", s); */
trans->ts = vs_realloc(trans->ts, sizeof(VSTransform)* s);
if (!trans->ts) {
vs_log_error(td->conf.modName, "Cannot allocate memory"
" for transformations: %i\n", s);
return 0;
}
}
trans->ts[i] = t;
i++;
}
trans->len = i;
return i;
}
// t = vsSimpleMotionsToTransform(md, &localmotions);
/*
* 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:
*/