-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMediaTimeline.cpp
14359 lines (13655 loc) · 593 KB
/
MediaTimeline.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2023-2024 CodeWin
This program 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.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "MediaTimeline.h"
#include "MediaInfo.h"
#include <imgui_helper.h>
#include <imgui_extra_widget.h>
#include <imgui_fft.h>
#include <implot.h>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <vector>
#include <utility>
#include <ThreadUtils.h>
#include <MatUtilsImVecHelper.h>
#include "EventStackFilter.h"
#include "TextureManager.h"
#include "MatUtils.h"
#include "Logger.h"
#include "DebugHelper.h"
const MediaTimeline::audio_band_config DEFAULT_BAND_CFG[10] = {
{ 32, 32, 0 }, { 64, 64, 0 },
{ 125, 125, 0 }, { 250, 250, 0 },
{ 500, 500, 0 }, { 1000, 1000, 0 },
{ 2000, 2000, 0 }, { 4000, 4000, 0 },
{ 8000, 8000, 0 }, { 16000, 16000, 0 },
};
static bool TimelineButton(ImDrawList *draw_list, const char * label, ImVec2 pos, ImVec2 size, std::string tooltips = "", ImVec4 hover_color = ImVec4(0.5f, 0.5f, 0.75f, 1.0f))
{
ImGuiIO &io = ImGui::GetIO();
ImRect rect(pos, pos + size);
bool overButton = rect.Contains(io.MousePos);
if (overButton)
draw_list->AddRectFilled(rect.Min, rect.Max, ImGui::GetColorU32(hover_color), 2);
ImVec4 color = ImVec4(1.f, 1.f, 1.f, 1.f);
ImGui::SetWindowFontScale(0.75);
draw_list->AddText(pos, ImGui::GetColorU32(color), label);
ImGui::SetWindowFontScale(1.0);
if (overButton && !tooltips.empty() && ImGui::BeginTooltip())
{
ImGui::TextUnformatted(tooltips.c_str());
ImGui::EndTooltip();
}
return overButton;
}
static int64_t alignTime(int64_t time, const MediaCore::Ratio& rate, bool useCeil = false)
{
if (rate.den && rate.num)
{
const float frame_index_f = (double)time * rate.num / ((double)rate.den * 1000.0);
const int64_t frame_index = useCeil ? (int64_t)ceil(frame_index_f) : (int64_t)floor(frame_index_f);
time = round((double)frame_index * 1000 * rate.den / rate.num);
}
return time;
}
static int64_t frameTime(MediaCore::Ratio rate)
{
if (rate.den && rate.num)
return rate.den * 1000 / rate.num;
else
return 0;
}
static void frameStepTime(int64_t& time, int32_t offset, MediaCore::Ratio rate)
{
if (rate.den && rate.num)
{
int64_t frame_index = (int64_t)floor((double)time * (double)rate.num / (double)rate.den / 1000.0 + 0.5);
frame_index += offset;
time = frame_index * 1000 * rate.den / rate.num;
}
}
static bool waveFrameResample(float * wave, int samples, int size, int start_offset, int size_max, int zoom, ImGui::ImMat& plot_frame_max, ImGui::ImMat& plot_frame_min)
{
bool min_max = samples > 16;
plot_frame_max.create_type(size, 1, 1, IM_DT_FLOAT32);
plot_frame_min.create_type(size, 1, 1, IM_DT_FLOAT32);
float * out_channel_data_max = (float *)plot_frame_max.data;
float * out_channel_data_min = (float *)plot_frame_min.data;
for (int i = 0; i < size; i++)
{
float max_val = -FLT_MAX;
float min_val = FLT_MAX;
if (!min_max)
{
if (i * samples + start_offset < size_max)
min_val = max_val = wave[i * samples + start_offset];
}
else
{
for (int n = 0; n < samples; n += zoom)
{
if (i * samples + n + start_offset >= size_max)
break;
float val = wave[i * samples + n + start_offset];
if (max_val < val) max_val = val;
if (min_val > val) min_val = val;
}
if (max_val < 0 && min_val < 0)
{
max_val = min_val;
}
else if (max_val > 0 && min_val > 0)
{
min_val = max_val;
}
}
out_channel_data_max[i] = ImMin(max_val, 1.f);
out_channel_data_min[i] = ImMax(min_val, -1.f);
}
return min_max;
}
static void waveformToMat(const MediaCore::Overview::Waveform::Holder wavefrom, ImGui::ImMat& mat, ImVec2 wave_size)
{
int channels = wavefrom->pcm.size();
if (channels > 2) channels = 2;
int channel_height = wave_size.y / channels;
ImVec2 channel_size(wave_size.x, channel_height);
float wave_range = fmax(fabs(wavefrom->minSample), fabs(wavefrom->maxSample));
mat.create(wave_size.x, wave_size.y, 4, (size_t)1, 4);
ImGui::PushStyleColor(ImGuiCol_PlotLines, ImVec4(0.f, 1.f, 0.f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_PlotHistogram, ImVec4(0.f, 1.f, 0.f, 0.75f));
for (int i = 0; i < channels; i++)
{
ImGui::PlotMat(mat, ImVec2(0, channel_height * i), &wavefrom->pcm[i][0], wavefrom->pcm[i].size(), 0, -wave_range / 2, wave_range / 2, channel_size, sizeof(float), true, true);
}
ImGui::PopStyleColor(2);
}
namespace MediaTimeline
{
/***********************************************************************************************************
* ID Generator Member Functions
***********************************************************************************************************/
int64_t IDGenerator::GenerateID()
{
return m_State ++;
}
void IDGenerator::SetState(int64_t state)
{
m_State = state;
}
int64_t IDGenerator::State() const
{
return m_State;
}
} // namespace MediaTimeline
namespace MediaTimeline
{
/***********************************************************************************************************
* MediaItem Struct Member Functions
***********************************************************************************************************/
MediaItem::MediaItem(const std::string& name, const std::string& path, uint32_t type, void* handle)
: mName(name), mPath(path), mMediaType(type), mHandle(handle)
{
TimeLine* timeline = (TimeLine*)handle;
mID = timeline ? timeline->m_IDGenerator.GenerateID() : ImGui::get_current_time_usec();
mTxMgr = timeline->mTxMgr;
}
MediaItem::MediaItem(MediaCore::MediaParser::Holder hParser, void* handle)
: mHandle(handle), mhParser(hParser)
{
TimeLine* timeline = (TimeLine*)handle;
mID = timeline ? timeline->m_IDGenerator.GenerateID() : ImGui::get_current_time_usec();
auto pVidstm = hParser->HasVideo() ? hParser->GetBestVideoStream() : nullptr;
mMediaType = hParser->HasVideo() ? (hParser->IsImageSequence() ? MEDIA_SUBTYPE_VIDEO_IMAGE_SEQUENCE
: (pVidstm->isImage ? MEDIA_SUBTYPE_VIDEO_IMAGE : MEDIA_VIDEO))
: (hParser->HasAudio() ? MEDIA_AUDIO : MEDIA_UNKNOWN);
mTxMgr = timeline->mTxMgr;
mPath = hParser->GetUrl();
mName = ImGuiHelper::path_filename(mPath);
}
MediaItem::~MediaItem()
{
for (auto texture : mWaveformTextures) ImGui::ImDestroyTexture(&texture);
mWaveformTextures.clear();
mMediaOverview = nullptr;
mMediaThumbnail.clear();
}
bool MediaItem::ChangeSource(const std::string& name, const std::string& path)
{
ReleaseItem();
mName = name;
mPath = path;
return Initialize();
}
bool MediaItem::Initialize()
{
mValid = false;
if (mPath.empty() || !ImGuiHelper::file_exists(mPath))
return false;
TimeLine* timeline = (TimeLine*)mHandle;
if (IS_TEXT(mMediaType))
{
mValid = true;
}
else
{
if (!mhParser)
{
mhParser = MediaCore::MediaParser::CreateInstance();
if (IS_IMAGESEQ(mMediaType))
mhParser->OpenImageSequence({25000, 1000}, mPath, ".+[_\\-]([[:digit:]]{1,})\\.(png|jpg|tiff|webp|jpeg|bmp)", false, true);
else
mhParser->Open(mPath);
if (!mhParser->IsOpened())
return false;
}
mMediaOverview = MediaCore::Overview::CreateInstance();
mMediaOverview->EnableHwAccel(timeline->mHardwareCodec);
RenderUtils::TextureManager::TexturePoolAttributes tTxPoolAttrs;
if (mTxMgr->GetTexturePoolAttributes(VIDEOITEM_OVERVIEW_GRID_TEXTURE_POOL_NAME, tTxPoolAttrs))
mMediaOverview->SetSnapshotSize(tTxPoolAttrs.tTxSize.x, tTxPoolAttrs.tTxSize.y);
else
mMediaOverview->SetSnapshotResizeFactor(0.05, 0.05);
if (!mMediaOverview->Open(mhParser, 64))
return false;
mSrcLength = mMediaOverview->GetMediaInfo()->duration * 1000;
mValid = true;
}
return true;
}
void MediaItem::ReleaseItem()
{
mMediaOverview = nullptr;
mMediaThumbnail.clear();
mSrcLength = 0;
mValid = false;
}
void MediaItem::UpdateThumbnail()
{
if (mMediaOverview && mMediaOverview->IsOpened())
{
auto count = mMediaOverview->GetSnapshotCount();
if (mMediaThumbnail.size() >= count)
return;
std::vector<ImGui::ImMat> snapshots;
if (mMediaOverview->GetSnapshots(snapshots))
{
for (int i = 0; i < snapshots.size(); i++)
{
if (i >= mMediaThumbnail.size() && !snapshots[i].empty())
{
auto hTx = mTxMgr->GetGridTextureFromPool(VIDEOITEM_OVERVIEW_GRID_TEXTURE_POOL_NAME);
if (hTx)
{
hTx->RenderMatToTexture(snapshots[i]);
mMediaThumbnail.push_back(hTx);
}
}
}
}
}
}
} //namespace MediaTimeline
namespace MediaTimeline
{
/***********************************************************************************************************
* Event track Struct Member Functions
***********************************************************************************************************/
EventTrack::EventTrack(int64_t id, void* handle)
{
TimeLine * timeline = (TimeLine *)handle;
mID = timeline ? timeline->m_IDGenerator.GenerateID() : ImGui::get_current_time_usec();
mClipID = id;
mHandle = handle;
}
EventTrack* EventTrack::Load(const imgui_json::value& value, void * handle)
{
Clip * clip = (Clip *)handle;
int64_t clip_id = -1;
if (value.contains("ClipID"))
{
auto& val = value["ClipID"];
if (val.is_number()) clip_id = val.get<imgui_json::number>();
}
EventTrack * new_track = new EventTrack(clip_id, clip->mHandle);
if (new_track)
{
if (value.contains("ID"))
{
auto& val = value["ID"];
if (val.is_number()) new_track->mID = val.get<imgui_json::number>();
}
if (value.contains("Expanded"))
{
auto& val = value["Expanded"];
if (val.is_boolean()) new_track->mExpanded = val.get<imgui_json::boolean>();
}
// load and check event into track
const imgui_json::array* eventIDArray = nullptr;
if (imgui_json::GetPtrTo(value, "EventIDS", eventIDArray))
{
for (auto& id_val : *eventIDArray)
{
int64_t event_id = id_val.get<imgui_json::number>();
new_track->m_Events.push_back(event_id);
}
}
}
return new_track;
}
void EventTrack::Save(imgui_json::value& value)
{
value["ID"] = imgui_json::number(mID);
value["ClipID"] = imgui_json::number(mClipID);
value["Expanded"] = imgui_json::boolean(mExpanded);
// save event ids
imgui_json::value events;
for (auto event : m_Events)
{
imgui_json::value event_id_value = imgui_json::number(event);
events.push_back(event_id_value);
}
if (m_Events.size() > 0) value["EventIDS"] = events;
}
bool EventTrack::DrawContent(ImDrawList *draw_list, ImRect rect, int event_height, int curve_height, int64_t view_start, int64_t view_end, float pixelWidthMS, bool editable, bool& changed)
{
bool mouse_hold = false;
TimeLine * timeline = (TimeLine *)mHandle;
if (!timeline)
return mouse_hold;
auto clip = timeline->FindClipByID(mClipID);
if (!clip || !clip->mEventStack)
return mouse_hold;
ImGui::PushClipRect(rect.Min, rect.Max, true);
ImGui::SetCursorScreenPos(rect.Min);
bool mouse_clicked = false;
bool curve_hovered = false;
// draw events
for (auto event_id : m_Events)
{
bool draw_event = false;
int64_t curve_start = 0;
int64_t curve_end = 0;
float cursor_start = 0;
float cursor_end = 0;
ImDrawFlags flag = ImDrawFlags_RoundCornersNone;
auto event = clip->mEventStack->GetEvent(event_id);
if (!event) continue;
if (event->IsInRange(view_start) && event->End() <= view_end)
{
/***********************************************************
* ----------------------------------------
* XXXXXXXX|XXXXXXXXXXXXXXXXXXXXXX|
* ----------------------------------------
************************************************************/
cursor_start = rect.Min.x;
cursor_end = rect.Min.x + (event->End() - view_start) * pixelWidthMS;
curve_start = view_start - event->Start();
curve_end = event->End() - event->Start();
draw_event = true;
flag |= ImDrawFlags_RoundCornersRight;
}
else if (event->Start() >= view_start && event->End() <= view_end)
{
/***********************************************************
* ----------------------------------------
* |XXXXXXXXXXXXXXXXXXXXXX|
* ----------------------------------------
************************************************************/
cursor_start = rect.Min.x + (event->Start() - view_start) * pixelWidthMS;
cursor_end = rect.Min.x + (event->End() - view_start) * pixelWidthMS;
curve_start = 0;
curve_end = event->End() - event->Start();
draw_event = true;
flag |= ImDrawFlags_RoundCornersAll;
}
else if (event->Start() >= view_start && event->IsInRange(view_end))
{
/***********************************************************
* ----------------------------------------
* |XXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXX
* ----------------------------------------
************************************************************/
cursor_start = rect.Min.x + (event->Start() - view_start) * pixelWidthMS;
cursor_end = rect.Max.x;
curve_start = 0;
curve_end = view_end - event->Start();
draw_event = true;
flag |= ImDrawFlags_RoundCornersLeft;
}
else if (event->Start() <= view_start && event->End() >= view_end)
{
/***********************************************************
* ----------------------------------------
* XXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXX
* ----------------------------------------
************************************************************/
cursor_start = rect.Min.x;
cursor_end = rect.Max.x;
curve_start = view_start - event->Start();
curve_end = view_end - event->Start();
draw_event = true;
}
if (event->Start() == view_start)
flag |= ImDrawFlags_RoundCornersLeft;
if (event->End() == view_end)
flag |= ImDrawFlags_RoundCornersRight;
ImVec2 event_pos_min = ImVec2(cursor_start, rect.Min.y);
ImVec2 event_pos_max = ImVec2(cursor_end, rect.Min.y + event_height);
ImRect event_rect(event_pos_min, event_pos_max);
if (!event_rect.Overlaps(rect))
{
draw_event = false;
}
if (event_rect.GetWidth() < 1)
{
draw_event = false;
}
if (draw_event && cursor_end > cursor_start)
{
auto pBP = event->GetBp();
bool is_event_valid = pBP && pBP->Blueprint_IsExecutable();
bool is_select = event->Status() & EVENT_SELECTED;
bool is_hovered = event->Status() & EVENT_HOVERED;
if (is_select)
draw_list->AddRect(event_pos_min, event_pos_max, IM_COL32(0,255,0,224), 4, flag, 2.0f);
else
draw_list->AddRect(event_pos_min, event_pos_max, IM_COL32(128,128,255,224), 4, flag, 2.0f);
auto event_color = is_event_valid ? (is_hovered ? IM_COL32(64, 64, 192, 128) : IM_COL32(32, 32, 192, 128)) : IM_COL32(192, 32, 32, 128);
draw_list->AddRectFilled(event_pos_min, event_pos_max, event_color, 4, flag);
if (pBP)
{
auto nodes = pBP->m_Document->m_Blueprint.GetNodes();
ImGui::SetCursorScreenPos(event_pos_min);
draw_list->PushClipRect(event_pos_min, event_pos_max, true);
if (!nodes.empty() && event_pos_max.x - event_pos_min.x < 24)
{
auto center_point = ImVec2(event_pos_min.x + (event_pos_max.x - event_pos_min.x) / 2, event_pos_min.y + (event_pos_max.y - event_pos_min.y) / 2);
draw_list->AddCircleFilled(center_point, 4, IM_COL32_WHITE, 16);
}
else
{
int count = 0;
for (auto node : nodes)
{
if (!IS_ENTRY_EXIT_NODE(node->GetType()))
{
ImGui::SetCursorScreenPos(event_pos_min + ImVec2(16 * count, 0));
node->DrawNodeLogo(ImGui::GetCurrentContext(), ImVec2(24, 24));
count ++;
}
}
}
draw_list->PopClipRect();
}
if (is_hovered && editable)
{
if (!mouse_clicked && ImGui::IsMouseClicked(ImGuiMouseButton_Left) && !ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
{
SelectEvent(event, false);
mouse_clicked = true;
}
// TODO::Dicky event draw tooltips
//event->DrawTooltips();
}
}
if (mExpanded && draw_event)
{
ImVec2 curve_pos_min = event_pos_min + ImVec2(0, event_height);
ImVec2 curve_pos_max = event_pos_max + ImVec2(0, curve_height);
ImRect curve_rect(curve_pos_min, curve_pos_max);
curve_hovered |= curve_rect.Contains(ImGui::GetMousePos());
ImGui::SetCursorScreenPos(curve_pos_min);
ImGui::PushID(event_id);
if (ImGui::BeginChild("##event_curve", curve_rect.GetSize(), false, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings))
{
ImVec2 sub_window_pos = ImGui::GetCursorScreenPos();
ImVec2 sub_window_size = ImGui::GetWindowSize();
draw_list->AddRectFilled(sub_window_pos, sub_window_pos + sub_window_size, COL_BLACK_DARK);
bool _changed = false;
auto pKP = event->GetKeyPoint();
if (pKP)
{
float current_time = timeline->mCurrentTime - clip->Start();
const auto frameRate = timeline->mhMediaSettings->VideoOutFrameRate();
ImVec2 alignT = { (float)frameRate.num, (float)frameRate.den*1000 };
pKP->SetCurveAlign(alignT, ImGui::ImCurveEdit::DIM_T);
mouse_hold |= ImGui::ImCurveEdit::Edit( nullptr,
pKP,
sub_window_size,
ImGui::GetID("##video_filter_event_keypoint_editor"),
editable,
current_time,
curve_start,
curve_end,
CURVE_EDIT_FLAG_VALUE_LIMITED | CURVE_EDIT_FLAG_MOVE_CURVE | CURVE_EDIT_FLAG_KEEP_BEGIN_END | CURVE_EDIT_FLAG_DOCK_BEGIN_END,
nullptr, // clippingRect
&_changed
);
if (_changed) { timeline->RefreshPreview(); changed |= _changed; }
}
}
ImGui::EndChild();
ImGui::PopID();
}
}
if (m_Events.empty())
mExpanded = false;
else if (rect.Contains(ImGui::GetMousePos()) && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && !curve_hovered && editable)
mExpanded = !mExpanded;
ImGui::PopClipRect();
return mouse_hold;
}
void EventTrack::SelectEvent(MEC::Event::Holder event, bool appand)
{
TimeLine * timeline = (TimeLine *)mHandle;
if (!timeline || !event)
return;
auto clip = timeline->FindClipByID(mClipID);
if (!clip)
return;
bool selected = true;
bool is_selected = event->Status() & EVENT_SELECTED;
if (appand && is_selected)
{
selected = false;
}
if (clip->mEventStack)
{
auto event_list = clip->mEventStack->GetEventList();
for (auto _event : event_list)
{
if (_event->Id() != event->Id())
{
if (!appand)
{
_event->SetStatus(EVENT_SELECTED_BIT, selected ? 0 : 1);
if (selected) _event->SetStatus(EVENT_NEED_SCROLL, 0);
}
}
}
}
event->SetStatus(EVENT_SELECTED_BIT, selected ? 1 : 0);
if (selected)
{
clip->bAttributeScrolling = false;
event->SetStatus(EVENT_NEED_SCROLL, 1);
}
if (clip->mEventStack)
{
clip->mEventStack->SetEditingEvent(selected ? event->Id() : -1);
}
}
MEC::Event::Holder EventTrack::FindPreviousEvent(int64_t id)
{
MEC::Event::Holder found_event = nullptr;
TimeLine * timeline = (TimeLine *)mHandle;
if (!timeline) return found_event;
auto clip = timeline->FindClipByID(mClipID);
if (!clip) return found_event;
auto event = clip->FindEventByID(id);
if (!event) return found_event;
auto iter = std::find_if(m_Events.begin(), m_Events.end(), [id](const int64_t e) {
return e == id;
});
if (iter == m_Events.begin() || iter == m_Events.end())
return found_event;
found_event = clip->FindEventByID(*(iter - 1));
return found_event;
}
MEC::Event::Holder EventTrack::FindNextEvent(int64_t id)
{
MEC::Event::Holder found_event = nullptr;
TimeLine * timeline = (TimeLine *)mHandle;
if (!timeline) return found_event;
auto clip = timeline->FindClipByID(mClipID);
if (!clip) return found_event;
auto event = clip->FindEventByID(id);
if (!event) return found_event;
auto iter = std::find_if(m_Events.begin(), m_Events.end(), [id](const int64_t e) {
return e == id;
});
if (iter == m_Events.end() || iter == m_Events.end() - 1)
return found_event;
found_event = clip->FindEventByID(*(iter + 1));
return found_event;
}
int64_t EventTrack::FindEventSpace(int64_t time)
{
int64_t space = -1;
TimeLine * timeline = (TimeLine *)mHandle;
if (!timeline) return space;
auto clip = timeline->FindClipByID(mClipID);
if (!clip) return space;
for (auto event_id : m_Events)
{
auto event = clip->FindEventByID(event_id);
if (event && event->Start() >= time)
{
space = event->Start() - time;
break;
}
}
return space;
}
void EventTrack::Update()
{
TimeLine * timeline = (TimeLine *)mHandle;
if (!timeline) return;
auto clip = timeline->FindClipByID(mClipID);
if (!clip) return;
auto clip_track = timeline->FindTrackByClipID(clip->mID);
if (clip_track) timeline->RefreshTrackView({clip_track->mID});
// sort m_Events by start time
std::sort(m_Events.begin(), m_Events.end(), [clip](const int64_t a, const int64_t b) {
auto a_event = clip->FindEventByID(a);
auto b_event = clip->FindEventByID(b);
if (a_event && b_event)
return a_event->Start() < b_event->Start();
else
return false;
});
}
} //namespace MediaTimeline
namespace MediaTimeline
{
/***********************************************************************************************************
* Clip Struct Member Functions
***********************************************************************************************************/
Clip::Clip(TimeLine* pOwner, uint32_t u32Type) : mHandle(pOwner), mType(u32Type)
{
mID = pOwner ? pOwner->m_IDGenerator.GenerateID() : ImGui::get_current_time_usec();
}
Clip::Clip(TimeLine* pOwner, uint32_t u32Type, const std::string& strName, int64_t i64Start, int64_t i64End, int64_t i64StartOffset, int64_t i64EndOffset)
: mHandle(pOwner), mType(u32Type), mName(strName), mStart(i64Start), mEnd(i64End), mStartOffset(i64StartOffset), mEndOffset(i64EndOffset)
{
mID = pOwner ? pOwner->m_IDGenerator.GenerateID() : ImGui::get_current_time_usec();
}
Clip::~Clip()
{
}
bool Clip::LoadFromJson(const imgui_json::value& j)
{
std::string strAttrName;
strAttrName = "ID";
if (j.contains(strAttrName) && j[strAttrName].is_number())
mID = j[strAttrName].get<imgui_json::number>();
else
{
Logger::Log(Logger::Error) << "FAILED to perform 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found. Clip json is:" << std::endl;
Logger::Log(Logger::Error) << j.dump() << std::endl << std::endl;
return false;
}
strAttrName = "Type";
if (j.contains(strAttrName) && j[strAttrName].is_number())
mType = j[strAttrName].get<imgui_json::number>();
else
{
Logger::Log(Logger::Error) << "FAILED to perform 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found. Clip json is:" << std::endl;
Logger::Log(Logger::Error) << j.dump() << std::endl << std::endl;
return false;
}
strAttrName = "MediaID";
if (j.contains(strAttrName) && j[strAttrName].is_number())
mMediaID = j[strAttrName].get<imgui_json::number>();
else
{
Logger::Log(Logger::Error) << "FAILED to perform 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found. Clip json is:" << std::endl;
Logger::Log(Logger::Error) << j.dump() << std::endl << std::endl;
return false;
}
strAttrName = "GroupID";
if (j.contains(strAttrName) && j[strAttrName].is_number())
mGroupID = j[strAttrName].get<imgui_json::number>();
else
{
Logger::Log(Logger::WARN) << "ABNORMAL json in 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found." << std::endl;
}
strAttrName = "Start";
if (j.contains(strAttrName) && j[strAttrName].is_number())
mStart = j[strAttrName].get<imgui_json::number>();
else
{
Logger::Log(Logger::Error) << "FAILED to perform 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found. Clip json is:" << std::endl;
Logger::Log(Logger::Error) << j.dump() << std::endl << std::endl;
return false;
}
strAttrName = "End";
if (j.contains(strAttrName) && j[strAttrName].is_number())
mEnd = j[strAttrName].get<imgui_json::number>();
else
{
Logger::Log(Logger::Error) << "FAILED to perform 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found. Clip json is:" << std::endl;
Logger::Log(Logger::Error) << j.dump() << std::endl << std::endl;
return false;
}
strAttrName = "StartOffset";
if (j.contains(strAttrName) && j[strAttrName].is_number())
mStartOffset = j[strAttrName].get<imgui_json::number>();
else
{
Logger::Log(Logger::Error) << "FAILED to perform 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found. Clip json is:" << std::endl;
Logger::Log(Logger::Error) << j.dump() << std::endl << std::endl;
return false;
}
strAttrName = "EndOffset";
if (j.contains(strAttrName) && j[strAttrName].is_number())
mEndOffset = j[strAttrName].get<imgui_json::number>();
else
{
Logger::Log(Logger::Error) << "FAILED to perform 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found. Clip json is:" << std::endl;
Logger::Log(Logger::Error) << j.dump() << std::endl << std::endl;
return false;
}
strAttrName = "Path";
if (j.contains(strAttrName) && j[strAttrName].is_string())
mPath = j[strAttrName].get<imgui_json::string>();
else
{
Logger::Log(Logger::WARN) << "ABNORMAL json in 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found." << std::endl;
}
strAttrName = "Name";
if (j.contains(strAttrName) && j[strAttrName].is_string())
mName = j[strAttrName].get<imgui_json::string>();
else
{
Logger::Log(Logger::Error) << "FAILED to perform 'Clip::LoadFromJson()'! No attribute '" << strAttrName << "' can be found. Clip json is:" << std::endl;
Logger::Log(Logger::Error) << j.dump() << std::endl << std::endl;
return false;
}
// load event tracks
strAttrName = "EventTracks";
if (j.contains(strAttrName) && j[strAttrName].is_array())
{
const auto& jnEventTracks = j[strAttrName].get<imgui_json::array>();
for (const auto& jnEvtTrack : jnEventTracks)
{
EventTrack* pEvtTrack = EventTrack::Load(jnEvtTrack, this);
if (pEvtTrack)
mEventTracks.push_back(pEvtTrack);
else
{
Logger::Log(Logger::WARN) << "FAILED to restore 'EventTrack' instance for 'Clip (id=" << mID << ", path=" << mPath <<", type=" << (int)mType
<< ")'! Event track json is:" << std::endl;
Logger::Log(Logger::WARN) << jnEvtTrack.dump() << std::endl << std::endl;
}
}
}
// save a copy of the json for this clip
mClipJson = j;
return true;
}
imgui_json::value Clip::SaveAsJson()
{
imgui_json::value j;
// save clip global info
j["ID"] = imgui_json::number(mID);
j["MediaID"] = imgui_json::number(mMediaID);
j["GroupID"] = imgui_json::number(mGroupID);
j["Type"] = imgui_json::number(mType);
j["Path"] = mPath;
j["Name"] = mName;
j["Start"] = imgui_json::number(mStart);
j["End"] = imgui_json::number(mEnd);
j["StartOffset"] = imgui_json::number(mStartOffset);
j["EndOffset"] = imgui_json::number(mEndOffset);
// jnClip["Selected"] = imgui_json::boolean(bSelected);
// save event track
imgui_json::array jnEvtTracks;
for (const auto pEvtTrack : mEventTracks)
{
imgui_json::value jnEvtTrack;
pEvtTrack->Save(jnEvtTrack);
jnEvtTracks.push_back(jnEvtTrack);
}
if (!jnEvtTracks.empty())
j["EventTracks"] = jnEvtTracks;
mClipJson = j;
return std::move(j);
}
int64_t Clip::Cropping(int64_t& diffTime, int type)
{
TimeLine * timeline = (TimeLine *)mHandle;
if (!timeline)
return 0;
auto track = timeline->FindTrackByClipID(mID);
if (!track || track->mLocked)
return 0;
if (IS_DUMMY(mType))
return 0;
int64_t diff = mDragAnchorTime != -1 ? diffTime - (type == 0 ? Start() : End()) + mDragAnchorTime : diffTime;
diff = timeline->AlignTime(diff);
if (diff == 0)
return 0;
int64_t length = Length();
int64_t old_offset = mStartOffset;
int64_t offset_time = 0;
// get all clip time march point
std::vector<int64_t> unselected_start_points;
std::vector<int64_t> unselected_end_points;
if (timeline->bMovingAttract)
{
for (auto clip : timeline->m_Clips)
{
// align clip end to timeline frame rate
int64_t _start = clip->mStart;
int64_t _end = clip->mEnd;
if (clip->mID != mID)
{
unselected_start_points.push_back(_start);
unselected_end_points.push_back(_end);
}
}
}
const auto frameRate = timeline->mhMediaSettings->VideoOutFrameRate();
int64_t frame_time = frameTime(frameRate) * 2; // 2 frames ?
int64_t attract_docking_gap = std::max((int64_t)(timeline->attract_docking_pixels / timeline->msPixelWidthTarget), frame_time);
int64_t min_gap = INT64_MAX;
timeline->mConnectedPoints = -1;
timeline->mConnectingPoints = -1;
timeline->mClipConnected = false;
if (type == 0)
{
// check start point
for (auto _point_start : unselected_start_points)
{
if (abs(mStart - _point_start) <= attract_docking_gap)
{
if (abs(min_gap) > abs(mStart - _point_start))
{
min_gap = mStart - _point_start;
timeline->mConnectingPoints = mStart;
timeline->mConnectedPoints = _point_start;
}
}
}
for (auto _point_end : unselected_end_points)
{
if (abs(mStart - _point_end) <= attract_docking_gap)
{
if (abs(min_gap) > abs(mStart - _point_end))
{
min_gap = mStart - _point_end;
timeline->mConnectingPoints = mStart;
timeline->mConnectedPoints = _point_end;
}
}
}
}
else
{
// check start point
for (auto _point_start : unselected_start_points)
{
if (abs(mEnd - _point_start) <= attract_docking_gap)
{
if (abs(min_gap) > abs(mEnd - _point_start))
{
min_gap = mEnd - _point_start;
timeline->mConnectingPoints = mEnd;
timeline->mConnectedPoints = _point_start;
}
}
}
for (auto _point_end : unselected_end_points)
{
if (abs(mEnd - _point_end) <= attract_docking_gap)
{
if (abs(min_gap) > abs(mEnd - _point_end))
{
min_gap = mEnd - _point_end;
timeline->mConnectingPoints = mEnd;
timeline->mConnectedPoints = _point_end;
}
}
}
}
if (timeline->mConnectedPoints != -1 && timeline->mConnectingPoints != -1 && min_gap != INT64_MAX)
{
auto dist = timeline->mConnectingPoints + diff - timeline->mConnectedPoints;
if (abs(dist) > abs(min_gap))
{
// leaving attracted point
if (abs(dist) < attract_docking_gap / timeline->disattract_docking_rate)
{
offset_time = -diff;
diff = 0;
timeline->mClipConnected = true;
}
}
else
if (abs(dist) <= abs(min_gap))
{
// closing attracted point
offset_time = -min_gap - diff;
diff = -min_gap;
timeline->mClipConnected = true;
}
else if (timeline->mConnectingPoints == timeline->mConnectedPoints)
{
// touch attracted point
if (abs(diff) < attract_docking_gap / timeline->disattract_docking_rate)
{
offset_time = -diff;
diff = 0;
timeline->mClipConnected = true;
}
}
}
// cropping start
if (type == 0)
{
// for clips that have length limitation
if ((IS_VIDEO(mType) && !IS_IMAGE(mType)) || IS_AUDIO(mType))
{
if (diff + mStartOffset < 0)
diff = -mStartOffset; // mStartOffset can NOT be NEGATIVE
int64_t newStart = mStart + diff;
if (newStart >= mEnd)
{
newStart = timeline->AlignTimeToPrevFrame(mEnd);
diff = newStart - mStart;
}
int64_t newStartOffset = mStartOffset + diff;
assert(newStartOffset >= 0);
int64_t newLength = length-diff;
assert(newLength > 0);
mStart = newStart;
mStartOffset = newStartOffset;
}
// for clips that have no length limitation
else
{
int64_t newStart = mStart + diff;
if (newStart >= mEnd)
{
newStart = timeline->AlignTimeToPrevFrame(mEnd);
diff = newStart - mStart;
}
int64_t newLength = length-diff;
assert(newLength > 0);