-
Notifications
You must be signed in to change notification settings - Fork 261
/
Timeline_Tests.cpp
610 lines (505 loc) · 15.2 KB
/
Timeline_Tests.cpp
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/**
* @file
* @brief Unit tests for openshot::Timeline
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/
/* LICENSE
*
* Copyright (c) 2008-2019 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "UnitTest++.h"
// Prevent name clashes with juce::UnitTest
#define DONT_SET_USING_JUCE_NAMESPACE 1
#include "OpenShot.h"
using namespace std;
using namespace openshot;
SUITE(Timeline)
{
TEST(Constructor)
{
// Create a default fraction (should be 1/1)
Fraction fps(30000,1000);
Timeline t1(640, 480, fps, 44100, 2, LAYOUT_STEREO);
// Check values
CHECK_EQUAL(640, t1.info.width);
CHECK_EQUAL(480, t1.info.height);
// Create a default fraction (should be 1/1)
Timeline t2(300, 240, fps, 44100, 2, LAYOUT_STEREO);
// Check values
CHECK_EQUAL(300, t2.info.width);
CHECK_EQUAL(240, t2.info.height);
}
TEST(Width_and_Height_Functions)
{
// Create a default fraction (should be 1/1)
Fraction fps(30000,1000);
Timeline t1(640, 480, fps, 44100, 2, LAYOUT_STEREO);
// Check values
CHECK_EQUAL(640, t1.info.width);
CHECK_EQUAL(480, t1.info.height);
// Set width
t1.info.width = 600;
// Check values
CHECK_EQUAL(600, t1.info.width);
CHECK_EQUAL(480, t1.info.height);
// Set height
t1.info.height = 400;
// Check values
CHECK_EQUAL(600, t1.info.width);
CHECK_EQUAL(400, t1.info.height);
}
TEST(Framerate)
{
// Create a default fraction (should be 1/1)
Fraction fps(24,1);
Timeline t1(640, 480, fps, 44100, 2, LAYOUT_STEREO);
// Check values
CHECK_CLOSE(24.0f, t1.info.fps.ToFloat(), 0.00001);
}
TEST(Check_Two_Track_Video)
{
// Create a reader
stringstream path;
path << TEST_MEDIA_PATH << "test.mp4";
Clip clip_video(path.str());
clip_video.Layer(0);
clip_video.Position(0.0);
stringstream path_overlay;
path_overlay << TEST_MEDIA_PATH << "front3.png";
Clip clip_overlay(path_overlay.str());
clip_overlay.Layer(1);
clip_overlay.Position(0.05); // Delay the overlay by 0.05 seconds
clip_overlay.End(0.5); // Make the duration of the overlay 1/2 second
// Create a timeline
Timeline t(1280, 720, Fraction(30, 1), 44100, 2, LAYOUT_STEREO);
// Add clips
t.AddClip(&clip_video);
t.AddClip(&clip_overlay);
// Open Timeline
t.Open();
// Get frame
std::shared_ptr<Frame> f = t.GetFrame(1);
// Get the image data
int pixel_row = 200;
int pixel_index = 230 * 4; // pixel 230 (4 bytes per pixel)
// Check image properties
CHECK_CLOSE(21, (int)f->GetPixels(pixel_row)[pixel_index], 5);
CHECK_CLOSE(191, (int)f->GetPixels(pixel_row)[pixel_index + 1], 5);
CHECK_CLOSE(0, (int)f->GetPixels(pixel_row)[pixel_index + 2], 5);
CHECK_CLOSE(255, (int)f->GetPixels(pixel_row)[pixel_index + 3], 5);
// Get frame
f = t.GetFrame(2);
// Check image properties
CHECK_CLOSE(176, (int)f->GetPixels(pixel_row)[pixel_index], 5);
CHECK_CLOSE(0, (int)f->GetPixels(pixel_row)[pixel_index + 1], 5);
CHECK_CLOSE(186, (int)f->GetPixels(pixel_row)[pixel_index + 2], 5);
CHECK_CLOSE(255, (int)f->GetPixels(pixel_row)[pixel_index + 3], 5);
// Get frame
f = t.GetFrame(3);
// Check image properties
CHECK_CLOSE(23, (int)f->GetPixels(pixel_row)[pixel_index], 5);
CHECK_CLOSE(190, (int)f->GetPixels(pixel_row)[pixel_index + 1], 5);
CHECK_CLOSE(0, (int)f->GetPixels(pixel_row)[pixel_index + 2], 5);
CHECK_CLOSE(255, (int)f->GetPixels(pixel_row)[pixel_index + 3], 5);
// Get frame
f = t.GetFrame(24);
// Check image properties
CHECK_CLOSE(186, (int)f->GetPixels(pixel_row)[pixel_index], 5);
CHECK_CLOSE(106, (int)f->GetPixels(pixel_row)[pixel_index + 1], 5);
CHECK_CLOSE(0, (int)f->GetPixels(pixel_row)[pixel_index + 2], 5);
CHECK_CLOSE(255, (int)f->GetPixels(pixel_row)[pixel_index + 3], 5);
// Get frame
f = t.GetFrame(5);
// Check image properties
CHECK_CLOSE(23, (int)f->GetPixels(pixel_row)[pixel_index], 5);
CHECK_CLOSE(190, (int)f->GetPixels(pixel_row)[pixel_index + 1], 5);
CHECK_CLOSE(0, (int)f->GetPixels(pixel_row)[pixel_index + 2], 5);
CHECK_CLOSE(255, (int)f->GetPixels(pixel_row)[pixel_index + 3], 5);
// Get frame
f = t.GetFrame(25);
// Check image properties
CHECK_CLOSE(0, (int)f->GetPixels(pixel_row)[pixel_index], 5);
CHECK_CLOSE(94, (int)f->GetPixels(pixel_row)[pixel_index + 1], 5);
CHECK_CLOSE(186, (int)f->GetPixels(pixel_row)[pixel_index + 2], 5);
CHECK_CLOSE(255, (int)f->GetPixels(pixel_row)[pixel_index + 3], 5);
// Get frame
f = t.GetFrame(4);
// Check image properties
CHECK_CLOSE(176, (int)f->GetPixels(pixel_row)[pixel_index], 5);
CHECK_CLOSE(0, (int)f->GetPixels(pixel_row)[pixel_index + 1], 5);
CHECK_CLOSE(186, (int)f->GetPixels(pixel_row)[pixel_index + 2], 5);
CHECK_CLOSE(255, (int)f->GetPixels(pixel_row)[pixel_index + 3], 5);
// Close reader
t.Close();
}
TEST(Clip_Order)
{
// Create a timeline
Timeline t(640, 480, Fraction(30, 1), 44100, 2, LAYOUT_STEREO);
// Add some clips out of order
stringstream path_top;
path_top << TEST_MEDIA_PATH << "front3.png";
Clip clip_top(path_top.str());
clip_top.Layer(2);
t.AddClip(&clip_top);
stringstream path_middle;
path_middle << TEST_MEDIA_PATH << "front.png";
Clip clip_middle(path_middle.str());
clip_middle.Layer(0);
t.AddClip(&clip_middle);
stringstream path_bottom;
path_bottom << TEST_MEDIA_PATH << "back.png";
Clip clip_bottom(path_bottom.str());
clip_bottom.Layer(1);
t.AddClip(&clip_bottom);
// Open Timeline
t.Open();
// Loop through Clips and check order (they should have been sorted into the correct order)
// Bottom layer to top layer, then by position.
list<Clip*>::iterator clip_itr;
list<Clip*> clips = t.Clips();
int counter = 0;
for (clip_itr=clips.begin(); clip_itr != clips.end(); ++clip_itr)
{
// Get clip object from the iterator
Clip *clip = (*clip_itr);
switch (counter) {
case 0:
CHECK_EQUAL(0, clip->Layer());
break;
case 1:
CHECK_EQUAL(1, clip->Layer());
break;
case 2:
CHECK_EQUAL(2, clip->Layer());
break;
}
// increment counter
counter++;
}
// Add another clip
stringstream path_middle1;
path_middle1 << TEST_MEDIA_PATH << "interlaced.png";
Clip clip_middle1(path_middle1.str());
clip_middle1.Layer(1);
clip_middle1.Position(0.5);
t.AddClip(&clip_middle1);
// Loop through clips again, and re-check order
counter = 0;
clips = t.Clips();
for (clip_itr=clips.begin(); clip_itr != clips.end(); ++clip_itr)
{
// Get clip object from the iterator
Clip *clip = (*clip_itr);
switch (counter) {
case 0:
CHECK_EQUAL(0, clip->Layer());
break;
case 1:
CHECK_EQUAL(1, clip->Layer());
CHECK_CLOSE(0.0, clip->Position(), 0.0001);
break;
case 2:
CHECK_EQUAL(1, clip->Layer());
CHECK_CLOSE(0.5, clip->Position(), 0.0001);
break;
case 3:
CHECK_EQUAL(2, clip->Layer());
break;
}
// increment counter
counter++;
}
// Close reader
t.Close();
}
TEST(Effect_Order)
{
// Create a timeline
Timeline t(640, 480, Fraction(30, 1), 44100, 2, LAYOUT_STEREO);
// Add some effects out of order
Negate effect_top;
effect_top.Id("C");
effect_top.Layer(2);
t.AddEffect(&effect_top);
Negate effect_middle;
effect_middle.Id("A");
effect_middle.Layer(0);
t.AddEffect(&effect_middle);
Negate effect_bottom;
effect_bottom.Id("B");
effect_bottom.Layer(1);
t.AddEffect(&effect_bottom);
// Open Timeline
t.Open();
// Loop through effects and check order (they should have been sorted into the correct order)
// Bottom layer to top layer, then by position, and then by order.
list<EffectBase*>::iterator effect_itr;
list<EffectBase*> effects = t.Effects();
int counter = 0;
for (effect_itr=effects.begin(); effect_itr != effects.end(); ++effect_itr)
{
// Get clip object from the iterator
EffectBase *effect = (*effect_itr);
switch (counter) {
case 0:
CHECK_EQUAL(0, effect->Layer());
CHECK_EQUAL("A", effect->Id());
CHECK_EQUAL(0, effect->Order());
break;
case 1:
CHECK_EQUAL(1, effect->Layer());
CHECK_EQUAL("B", effect->Id());
CHECK_EQUAL(0, effect->Order());
break;
case 2:
CHECK_EQUAL(2, effect->Layer());
CHECK_EQUAL("C", effect->Id());
CHECK_EQUAL(0, effect->Order());
break;
}
// increment counter
counter++;
}
// Add some more effects out of order
Negate effect_top1;
effect_top1.Id("B-2");
effect_top1.Layer(1);
effect_top1.Position(0.5);
effect_top1.Order(2);
t.AddEffect(&effect_top1);
Negate effect_middle1;
effect_middle1.Id("B-3");
effect_middle1.Layer(1);
effect_middle1.Position(0.5);
effect_middle1.Order(1);
t.AddEffect(&effect_middle1);
Negate effect_bottom1;
effect_bottom1.Id("B-1");
effect_bottom1.Layer(1);
effect_bottom1.Position(0);
effect_bottom1.Order(3);
t.AddEffect(&effect_bottom1);
// Loop through effects again, and re-check order
effects = t.Effects();
counter = 0;
for (effect_itr=effects.begin(); effect_itr != effects.end(); ++effect_itr)
{
// Get clip object from the iterator
EffectBase *effect = (*effect_itr);
switch (counter) {
case 0:
CHECK_EQUAL(0, effect->Layer());
CHECK_EQUAL("A", effect->Id());
CHECK_EQUAL(0, effect->Order());
break;
case 1:
CHECK_EQUAL(1, effect->Layer());
CHECK_EQUAL("B-1", effect->Id());
CHECK_CLOSE(0.0, effect->Position(), 0.0001);
CHECK_EQUAL(3, effect->Order());
break;
case 2:
CHECK_EQUAL(1, effect->Layer());
CHECK_EQUAL("B", effect->Id());
CHECK_CLOSE(0.0, effect->Position(), 0.0001);
CHECK_EQUAL(0, effect->Order());
break;
case 3:
CHECK_EQUAL(1, effect->Layer());
CHECK_EQUAL("B-2", effect->Id());
CHECK_CLOSE(0.5, effect->Position(), 0.0001);
CHECK_EQUAL(2, effect->Order());
break;
case 4:
CHECK_EQUAL(1, effect->Layer());
CHECK_EQUAL("B-3", effect->Id());
CHECK_CLOSE(0.5, effect->Position(), 0.0001);
CHECK_EQUAL(1, effect->Order());
break;
case 5:
CHECK_EQUAL(2, effect->Layer());
CHECK_EQUAL("C", effect->Id());
CHECK_EQUAL(0, effect->Order());
break;
}
// increment counter
counter++;
}
// Close reader
t.Close();
}
TEST(GetClip_by_id)
{
Timeline t(640, 480, Fraction(30, 1), 44100, 2, LAYOUT_STEREO);
stringstream path1;
path1 << TEST_MEDIA_PATH << "interlaced.png";
auto media_path1 = path1.str();
stringstream path2;
path2 << TEST_MEDIA_PATH << "front.png";
auto media_path2 = path2.str();
Clip clip1(media_path1);
std::string clip1_id("CLIP00001");
clip1.Id(clip1_id);
clip1.Layer(1);
Clip clip2(media_path2);
std::string clip2_id("CLIP00002");
clip2.Id(clip2_id);
clip2.Layer(2);
clip2.Waveform(true);
t.AddClip(&clip1);
t.AddClip(&clip2);
// We explicitly want to get returned a Clip*, here
Clip* matched = t.GetClip(clip1_id);
CHECK_EQUAL(clip1_id, matched->Id());
CHECK_EQUAL(1, matched->Layer());
Clip* matched2 = t.GetClip(clip2_id);
CHECK_EQUAL(clip2_id, matched2->Id());
CHECK_EQUAL(false, matched2->Layer() < 2);
Clip* matched3 = t.GetClip("BAD_ID");
CHECK_EQUAL(true, matched3 == nullptr);
// Ensure we can access the Clip API interfaces after lookup
CHECK_EQUAL(false, matched->Waveform());
CHECK_EQUAL(true, matched2->Waveform());
}
TEST(GetClipEffect_by_id)
{
Timeline t(640, 480, Fraction(30, 1), 44100, 2, LAYOUT_STEREO);
stringstream path1;
path1 << TEST_MEDIA_PATH << "interlaced.png";
auto media_path1 = path1.str();
// Create a clip, nothing special
Clip clip1(media_path1);
std::string clip1_id("CLIP00001");
clip1.Id(clip1_id);
clip1.Layer(1);
// Add a blur effect
Keyframe horizontal_radius(5.0);
Keyframe vertical_radius(5.0);
Keyframe sigma(3.0);
Keyframe iterations(3.0);
Blur blur1(horizontal_radius, vertical_radius, sigma, iterations);
std::string blur1_id("EFFECT00011");
blur1.Id(blur1_id);
clip1.AddEffect(&blur1);
// A second clip, different layer
Clip clip2(media_path1);
std::string clip2_id("CLIP00002");
clip2.Id(clip2_id);
clip2.Layer(2);
// Some effects for clip2
Negate neg2;
std::string neg2_id("EFFECT00021");
neg2.Id(neg2_id);
neg2.Layer(2);
clip2.AddEffect(&neg2);
Blur blur2(horizontal_radius, vertical_radius, sigma, iterations);
std::string blur2_id("EFFECT00022");
blur2.Id(blur2_id);
blur2.Layer(2);
clip2.AddEffect(&blur2);
t.AddClip(&clip1);
// Check that we can look up clip1's effect
auto match1 = t.GetClipEffect("EFFECT00011");
CHECK_EQUAL(blur1_id, match1->Id());
// clip2 hasn't been added yet, shouldn't be found
match1 = t.GetClipEffect(blur2_id);
CHECK_EQUAL(true, match1 == nullptr);
t.AddClip(&clip2);
// Check that blur2 can now be found via clip2
match1 = t.GetClipEffect(blur2_id);
CHECK_EQUAL(blur2_id, match1->Id());
CHECK_EQUAL(2, match1->Layer());
}
TEST(GetEffect_by_id)
{
Timeline t(640, 480, Fraction(30, 1), 44100, 2, LAYOUT_STEREO);
// Create a timeline effect
Keyframe horizontal_radius(5.0);
Keyframe vertical_radius(5.0);
Keyframe sigma(3.0);
Keyframe iterations(3.0);
Blur blur1(horizontal_radius, vertical_radius, sigma, iterations);
std::string blur1_id("EFFECT00011");
blur1.Id(blur1_id);
blur1.Layer(1);
t.AddEffect(&blur1);
auto match1 = t.GetEffect(blur1_id);
CHECK_EQUAL(blur1_id, match1->Id());
CHECK_EQUAL(1, match1->Layer());
match1 = t.GetEffect("NOSUCHNAME");
CHECK_EQUAL(true, match1 == nullptr);
}
TEST(Effect_Blur)
{
// Create a timeline
Timeline t(640, 480, Fraction(30, 1), 44100, 2, LAYOUT_STEREO);
stringstream path_top;
path_top << TEST_MEDIA_PATH << "interlaced.png";
Clip clip_top(path_top.str());
clip_top.Layer(2);
t.AddClip(&clip_top);
// Add some effects out of order
Keyframe horizontal_radius(5.0);
Keyframe vertical_radius(5.0);
Keyframe sigma(3.0);
Keyframe iterations(3.0);
Blur blur(horizontal_radius, vertical_radius, sigma, iterations);
blur.Id("B");
blur.Layer(2);
t.AddEffect(&blur);
// Open Timeline
t.Open();
// Get frame
std::shared_ptr<Frame> f = t.GetFrame(1);
// Close reader
t.Close();
}
TEST(GetMaxFrame_GetMaxTime)
{
// Create a timeline
Timeline t(640, 480, Fraction(30, 1), 44100, 2, LAYOUT_STEREO);
stringstream path1;
path1 << TEST_MEDIA_PATH << "interlaced.png";
Clip clip1(path1.str());
clip1.Layer(1);
clip1.Position(50);
clip1.End(45);
t.AddClip(&clip1);
CHECK_CLOSE(95.0, t.GetMaxTime(), 0.001);
CHECK_EQUAL(95 * 30 + 1, t.GetMaxFrame());
Clip clip2(path1.str());
clip2.Layer(2);
clip2.Position(0);
clip2.End(55);
t.AddClip(&clip2);
CHECK_EQUAL(95 * 30 + 1, t.GetMaxFrame());
CHECK_CLOSE(95.0, t.GetMaxTime(), 0.001);
clip2.Position(100);
clip1.Position(80);
CHECK_EQUAL(155 * 30 + 1, t.GetMaxFrame());
CHECK_CLOSE(155.0, t.GetMaxTime(), 0.001);
t.RemoveClip(&clip2);
CHECK_EQUAL(125 * 30 + 1, t.GetMaxFrame());
CHECK_CLOSE(125.0, t.GetMaxTime(), 0.001);
}
} // SUITE