prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / cinelerra / edl.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
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  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "asset.h"
23 #include "assets.h"
24 #include "atrack.h"
25 #include "autoconf.h"
26 #include "automation.h"
27 #include "awindowgui.inc"
28 #include "bcsignals.h"
29 #include "clip.h"
30 #include "bccmodels.h"
31 #include "bchash.h"
32 #include "edits.h"
33 #include "edl.h"
34 #include "edlsession.h"
35 #include "filexml.h"
36 #include "format.inc"
37 #include "guicast.h"
38 #include "indexstate.h"
39 #include "labels.h"
40 #include "localsession.h"
41 #include "mutex.h"
42 #include "nestededls.h"
43 #include "panauto.h"
44 #include "panautos.h"
45 #include "playbackconfig.h"
46 #include "playabletracks.h"
47 #include "plugin.h"
48 #include "preferences.h"
49 #include "recordconfig.h"
50 #include "recordlabel.h"
51 #include "sharedlocation.h"
52 #include "theme.h"
53 #include "tracks.h"
54 #include "transportque.inc"
55 #include "vedit.h"
56 #include "vtrack.h"
57
58
59
60
61 Mutex* EDL::id_lock = 0;
62
63
64
65 EDL::EDL(EDL *parent_edl)
66  : Indexable(0)
67 {
68         this->parent_edl = parent_edl;
69         tracks = 0;
70         labels = 0;
71         local_session = 0;
72 //      vwindow_edl = 0;
73 //      vwindow_edl_shared = 0;
74
75         folders.set_array_delete();
76
77         new_folder(CLIP_FOLDER);
78
79         new_folder(MEDIA_FOLDER);
80
81         id = next_id();
82         path[0] = 0;
83 }
84
85
86 EDL::~EDL()
87 {
88
89         if(tracks)
90         {
91                 delete tracks;
92         }
93         if(labels)
94         {
95                 delete labels;
96         }
97
98         if(local_session)
99         {
100                 delete local_session;
101         }
102
103
104         remove_vwindow_edls();
105
106 //      if(vwindow_edl && !vwindow_edl_shared)
107 //              vwindow_edl->Garbage::remove_user();
108
109         if(!parent_edl)
110         {
111                 delete assets;
112                 delete session;
113         }
114
115
116         folders.remove_all_objects();
117         for(int i = 0; i < clips.size(); i++)
118                 clips.get(i)->Garbage::remove_user();
119         clips.remove_all();
120         delete nested_edls;
121 }
122
123
124
125 void EDL::create_objects()
126 {
127         tracks = new Tracks(this);
128         if(!parent_edl)
129         {
130                 assets = new Assets(this);
131                 session = new EDLSession(this);
132         }
133         else
134         {
135                 assets = parent_edl->assets;
136                 session = parent_edl->session;
137         }
138
139         local_session = new LocalSession(this);
140         labels = new Labels(this, "LABELS");
141         nested_edls = new NestedEDLs;
142 //      last_playback_position = 0;
143 }
144
145 EDL& EDL::operator=(EDL &edl)
146 {
147 printf("EDL::operator= 1\n");
148         copy_all(&edl);
149         return *this;
150 }
151
152 int EDL::load_defaults(BC_Hash *defaults)
153 {
154         if(!parent_edl)
155                 session->load_defaults(defaults);
156
157         local_session->load_defaults(defaults);
158         return 0;
159 }
160
161 int EDL::save_defaults(BC_Hash *defaults)
162 {
163         if(!parent_edl)
164                 session->save_defaults(defaults);
165
166         local_session->save_defaults(defaults);
167         return 0;
168 }
169
170 void EDL::boundaries()
171 {
172         session->boundaries();
173         local_session->boundaries();
174 }
175
176 int EDL::create_default_tracks()
177 {
178
179         for(int i = 0; i < session->video_tracks; i++)
180         {
181                 tracks->add_video_track(0, 0);
182         }
183         for(int i = 0; i < session->audio_tracks; i++)
184         {
185                 tracks->add_audio_track(0, 0);
186         }
187         return 0;
188 }
189
190 int EDL::load_xml(FileXML *file,
191         uint32_t load_flags)
192 {
193         int result = 0;
194 // Track numbering offset for replacing undo data.
195         int track_offset = 0;
196
197 // Clear objects
198         folders.remove_all_objects();
199
200         if((load_flags & LOAD_ALL) == LOAD_ALL)
201         {
202                 remove_vwindow_edls();
203         }
204
205
206 // Search for start of master EDL.
207
208 // The parent_edl test caused clip creation to fail since those XML files
209 // contained an EDL tag.
210
211 // The parent_edl test is required to make EDL loading work because
212 // when loading an EDL the EDL tag is already read by the parent.
213
214         if(!parent_edl)
215         {
216                 do{
217                   result = file->read_tag();
218                 }while(!result &&
219                         !file->tag.title_is("XML") &&
220                         !file->tag.title_is("EDL"));
221         }
222
223         if(!result)
224         {
225 // Get path for backups
226 //              path[0] = 0;
227                 file->tag.get_property("path", path);
228
229 // Erase everything
230                 if((load_flags & LOAD_ALL) == LOAD_ALL ||
231                         (load_flags & LOAD_EDITS) == LOAD_EDITS)
232                 {
233                         while(tracks->last) delete tracks->last;
234                 }
235
236                 if((load_flags & LOAD_ALL) == LOAD_ALL)
237                 {
238                         for(int i = 0; i < clips.size(); i++)
239                                 clips.get(i)->Garbage::remove_user();
240                         clips.remove_all();
241                 }
242
243                 if(load_flags & LOAD_TIMEBAR)
244                 {
245                         while(labels->last) delete labels->last;
246                         local_session->unset_inpoint();
247                         local_session->unset_outpoint();
248                 }
249
250 // This was originally in LocalSession::load_xml
251                 if(load_flags & LOAD_SESSION)
252                 {
253                         local_session->clipboard_length = 0;
254                 }
255
256                 do{
257                         result = file->read_tag();
258
259                         if(!result)
260                         {
261                                 if(file->tag.title_is("/XML") ||
262                                         file->tag.title_is("/EDL") ||
263                                         file->tag.title_is("/CLIP_EDL") ||
264                                         file->tag.title_is("/VWINDOW_EDL"))
265                                 {
266                                         result = 1;
267                                 }
268                                 else
269                                 if(file->tag.title_is("CLIPBOARD"))
270                                 {
271                                         local_session->clipboard_length =
272                                                 file->tag.get_property("LENGTH", (double)0);
273                                 }
274                                 else
275                                 if(file->tag.title_is("VIDEO"))
276                                 {
277                                         if((load_flags & LOAD_VCONFIG) &&
278                                                 (load_flags & LOAD_SESSION))
279                                                 session->load_video_config(file, 0, load_flags);
280                                 }
281                                 else
282                                 if(file->tag.title_is("AUDIO"))
283                                 {
284                                         if((load_flags & LOAD_ACONFIG) &&
285                                                 (load_flags & LOAD_SESSION))
286                                                 session->load_audio_config(file, 0, load_flags);
287                                 }
288                                 else
289                                 if(file->tag.title_is("FOLDER"))
290                                 {
291                                         char folder[BCTEXTLEN];
292                                         strcpy(folder, file->read_text());
293                                         new_folder(folder);
294                                 }
295                                 else
296                                 if(file->tag.title_is("ASSETS"))
297                                 {
298                                         if(load_flags & LOAD_ASSETS)
299                                                 assets->load(file, load_flags);
300                                 }
301                                 else
302                                 if(file->tag.title_is(labels->xml_tag))
303                                 {
304                                         if(load_flags & LOAD_TIMEBAR)
305                                                 labels->load(file, load_flags);
306                                 }
307                                 else
308                                 if(file->tag.title_is("LOCALSESSION"))
309                                 {
310                                         if((load_flags & LOAD_SESSION) ||
311                                                 (load_flags & LOAD_TIMEBAR))
312                                                 local_session->load_xml(file, load_flags);
313                                 }
314                                 else
315                                 if(file->tag.title_is("SESSION"))
316                                 {
317                                         if((load_flags & LOAD_SESSION) &&
318                                                 !parent_edl)
319                                                 session->load_xml(file, 0, load_flags);
320                                 }
321                                 else
322                                 if(file->tag.title_is("TRACK"))
323                                 {
324                                         tracks->load(file, track_offset, load_flags);
325                                 }
326                                 else
327 // Sub EDL.
328 // Causes clip creation to fail because that involves an opening EDL tag.
329                                 if(file->tag.title_is("CLIP_EDL") && !parent_edl)
330                                 {
331                                         EDL *new_edl = new EDL(this);
332                                         new_edl->create_objects();
333                                         new_edl->load_xml(file, LOAD_ALL);
334
335                                         if((load_flags & LOAD_ALL) == LOAD_ALL)
336                                                 clips.append(new_edl);
337                                         else
338                                                 new_edl->Garbage::remove_user();
339                                 }
340                                 else
341                                 if(file->tag.title_is("VWINDOW_EDL") && !parent_edl)
342                                 {
343                                         EDL *new_edl = new EDL(this);
344                                         new_edl->create_objects();
345                                         new_edl->load_xml(file, LOAD_ALL);
346
347
348                                         if((load_flags & LOAD_ALL) == LOAD_ALL)
349                                         {
350 //                                              if(vwindow_edl && !vwindow_edl_shared)
351 //                                                      vwindow_edl->Garbage::remove_user();
352 //                                              vwindow_edl_shared = 0;
353 //                                              vwindow_edl = new_edl;
354
355                                                 append_vwindow_edl(new_edl, 0);
356
357                                         }
358                                         else
359 // Discard if not replacing EDL
360                                         {
361                                                 new_edl->Garbage::remove_user();
362                                                 new_edl = 0;
363                                         }
364                                 }
365                         }
366                 }while(!result);
367         }
368         boundaries();
369 //dump();
370
371         return 0;
372 }
373
374 // Output path is the path of the output file if name truncation is desired.
375 // It is a "" if complete names should be used.
376 // Called recursively by copy for clips, thus the string can't be terminated.
377 // The string is not terminated in this call.
378 int EDL::save_xml(FileXML *file,
379         const char *output_path,
380         int is_clip,
381         int is_vwindow)
382 {
383         copy(0,
384                 tracks->total_length(),
385                 1,
386                 is_clip,
387                 is_vwindow,
388                 file,
389                 output_path,
390                 0);
391         return 0;
392 }
393
394 int EDL::copy_all(EDL *edl)
395 {
396         if(this == edl) return 0;
397         update_index(edl);
398         nested_edls->clear();
399         copy_session(edl);
400         copy_assets(edl);
401         copy_clips(edl);
402         tracks->copy_from(edl->tracks);
403         labels->copy_from(edl->labels);
404         return 0;
405 }
406
407 void EDL::copy_clips(EDL *edl)
408 {
409         if(this == edl) return;
410
411         remove_vwindow_edls();
412
413 //      if(vwindow_edl && !vwindow_edl_shared)
414 //              vwindow_edl->Garbage::remove_user();
415 //      vwindow_edl = 0;
416 //      vwindow_edl_shared = 0;
417
418         for(int i = 0; i < edl->total_vwindow_edls(); i++)
419         {
420                 EDL *new_edl = new EDL(this);
421                 new_edl->create_objects();
422                 new_edl->copy_all(edl->get_vwindow_edl(i));
423                 append_vwindow_edl(new_edl, 0);
424         }
425
426         for(int i = 0; i < clips.size(); i++)
427                 clips.get(i)->Garbage::remove_user();
428         clips.remove_all();
429         for(int i = 0; i < edl->clips.total; i++)
430         {
431                 add_clip(edl->clips.values[i]);
432         }
433 }
434
435 void EDL::copy_assets(EDL *edl)
436 {
437         if(this == edl) return;
438
439         if(!parent_edl)
440         {
441                 assets->copy_from(edl->assets);
442         }
443 }
444
445 void EDL::copy_session(EDL *edl, int session_only)
446 {
447         if(this == edl) return;
448
449         if(!session_only)
450         {
451                 strcpy(this->path, edl->path);
452 //printf("EDL::copy_session %p %s\n", this, this->path);
453
454                 folders.remove_all_objects();
455                 for(int i = 0; i < edl->folders.total; i++)
456                 {
457                         char *new_folder;
458                         folders.append(new_folder = new char[strlen(edl->folders.values[i]) + 1]);
459                         strcpy(new_folder, edl->folders.values[i]);
460                 }
461         }
462
463         if(!parent_edl)
464         {
465                 session->copy(edl->session);
466         }
467
468         if(!session_only)
469         {
470                 local_session->copy_from(edl->local_session);
471         }
472 }
473
474 int EDL::copy_assets(double start,
475         double end,
476         FileXML *file,
477         int all,
478         const char *output_path)
479 {
480         ArrayList<Asset*> asset_list;
481         Track* current;
482
483         file->tag.set_title("ASSETS");
484         file->append_tag();
485         file->append_newline();
486
487 // Copy everything for a save
488         if(all)
489         {
490                 for(Asset *asset = assets->first;
491                         asset;
492                         asset = asset->next)
493                 {
494                         asset_list.append(asset);
495                 }
496         }
497         else
498 // Copy just the ones being used.
499         {
500                 for(current = tracks->first;
501                         current;
502                         current = NEXT)
503                 {
504                         if(current->record)
505                         {
506                                 current->copy_assets(start,
507                                         end,
508                                         &asset_list);
509                         }
510                 }
511         }
512
513 // Paths relativised here
514         for(int i = 0; i < asset_list.total; i++)
515         {
516                 asset_list.values[i]->write(file,
517                         0,
518                         output_path);
519         }
520
521         file->tag.set_title("/ASSETS");
522         file->append_tag();
523         file->append_newline();
524         file->append_newline();
525         return 0;
526 }
527
528 int EDL::copy(double start,
529         double end,
530         int all,
531         int is_clip,
532         int is_vwindow,
533         FileXML *file,
534         const char *output_path,
535         int rewind_it)
536 {
537 //printf("EDL::copy 1\n");
538 // begin file
539         if(is_clip)
540                 file->tag.set_title("CLIP_EDL");
541         else
542         if(is_vwindow)
543                 file->tag.set_title("VWINDOW_EDL");
544         else
545         {
546                 file->tag.set_title("EDL");
547                 file->tag.set_property("VERSION", CINELERRA_VERSION);
548 // Save path for restoration of the project title from a backup.
549                 if(this->path[0])
550                 {
551                         file->tag.set_property("PATH", path);
552                 }
553         }
554
555         file->append_tag();
556         file->append_newline();
557
558 // Set clipboard samples only if copying to clipboard
559         if(!all)
560         {
561                 file->tag.set_title("CLIPBOARD");
562                 file->tag.set_property("LENGTH", end - start);
563                 file->append_tag();
564                 file->append_newline();
565                 file->append_newline();
566         }
567 //printf("EDL::copy 1\n");
568
569 // Sessions
570         local_session->save_xml(file, start);
571
572 //printf("EDL::copy 1\n");
573
574 // Top level stuff.
575 //      if(!parent_edl)
576         {
577 // Need to copy all this from child EDL if pasting is desired.
578 // Session
579                 session->save_xml(file);
580                 session->save_video_config(file);
581                 session->save_audio_config(file);
582
583 // Folders
584                 for(int i = 0; i < folders.total; i++)
585                 {
586                         file->tag.set_title("FOLDER");
587                         file->append_tag();
588                         file->append_text(folders.values[i]);
589                         file->tag.set_title("/FOLDER");
590                         file->append_tag();
591                         file->append_newline();
592                 }
593
594 // Media
595 // Don't replicate all assets for every clip.
596 // The assets for the clips are probably in the mane EDL.
597                 if(!is_clip)
598                         copy_assets(start,
599                                 end,
600                                 file,
601                                 all,
602                                 output_path);
603
604 // Clips
605 // Don't want this if using clipboard
606                 if(all)
607                 {
608                         for(int i = 0; i < total_vwindow_edls(); i++)
609                         {
610                                 get_vwindow_edl(i)->save_xml(file,
611                                         output_path,
612                                         0,
613                                         1);
614                         }
615
616                         for(int i = 0; i < clips.total; i++)
617                                 clips.values[i]->save_xml(file,
618                                         output_path,
619                                         1,
620                                         0);
621                 }
622
623                 file->append_newline();
624                 file->append_newline();
625         }
626
627
628 //printf("EDL::copy 1\n");
629
630         labels->copy(start, end, file);
631 //printf("EDL::copy 1\n");
632         tracks->copy(start, end, all, file, output_path);
633 //printf("EDL::copy 2\n");
634
635 // terminate file
636         if(is_clip)
637                 file->tag.set_title("/CLIP_EDL");
638         else
639         if(is_vwindow)
640                 file->tag.set_title("/VWINDOW_EDL");
641         else
642                 file->tag.set_title("/EDL");
643         file->append_tag();
644         file->append_newline();
645
646
647 // For editing operations we want to rewind it for immediate pasting.
648 // For clips and saving to disk leave it alone.
649         if(rewind_it)
650         {
651                 file->terminate_string();
652                 file->rewind();
653         }
654         return 0;
655 }
656
657 void EDL::rechannel()
658 {
659         for(Track *current = tracks->first; current; current = NEXT)
660         {
661                 if(current->data_type == TRACK_AUDIO)
662                 {
663                         PanAutos *autos = (PanAutos*)current->automation->autos[AUTOMATION_PAN];
664                         ((PanAuto*)autos->default_auto)->rechannel();
665                         for(PanAuto *keyframe = (PanAuto*)autos->first;
666                                 keyframe;
667                                 keyframe = (PanAuto*)keyframe->next)
668                         {
669                                 keyframe->rechannel();
670                         }
671                 }
672         }
673 }
674
675 void EDL::resample(double old_rate, double new_rate, int data_type)
676 {
677         for(Track *current = tracks->first; current; current = NEXT)
678         {
679                 if(current->data_type == data_type)
680                 {
681                         current->resample(old_rate, new_rate);
682                 }
683         }
684 }
685
686
687 void EDL::synchronize_params(EDL *edl)
688 {
689         local_session->synchronize_params(edl->local_session);
690         for(Track *this_track = tracks->first, *that_track = edl->tracks->first;
691                 this_track && that_track;
692                 this_track = this_track->next,
693                 that_track = that_track->next)
694         {
695                 this_track->synchronize_params(that_track);
696         }
697 }
698
699 int EDL::trim_selection(double start,
700         double end,
701         int edit_labels,
702         int edit_plugins,
703         int edit_autos)
704 {
705         if(start != end)
706         {
707 // clear the data
708                 clear(0,
709                         start,
710                         edit_labels,
711                         edit_plugins,
712                         edit_autos);
713                 clear(end - start,
714                         tracks->total_length(),
715                         edit_labels,
716                         edit_plugins,
717                         edit_autos);
718         }
719         return 0;
720 }
721
722
723 int EDL::equivalent(double position1, double position2)
724 {
725         double threshold = (double).5 / session->frame_rate;
726         if(session->cursor_on_frames)
727                 threshold = (double).5 / session->frame_rate;
728         else
729                 threshold = (double)1 / session->sample_rate;
730
731         if(fabs(position2 - position1) < threshold)
732         return 1;
733     else
734         return 0;
735 }
736
737 double EDL::equivalent_output(EDL *edl)
738 {
739         double result = -1;
740         session->equivalent_output(edl->session, &result);
741         tracks->equivalent_output(edl->tracks, &result);
742         return result;
743 }
744
745
746 void EDL::set_path(char *path)
747 {
748         strcpy(this->path, path);
749 }
750
751 void EDL::set_inpoint(double position)
752 {
753         if(equivalent(local_session->get_inpoint(), position) &&
754                 local_session->get_inpoint() >= 0)
755         {
756                 local_session->unset_inpoint();
757         }
758         else
759         {
760                 local_session->set_inpoint(align_to_frame(position, 0));
761                 if(local_session->get_outpoint() <= local_session->get_inpoint())
762                         local_session->unset_outpoint();
763         }
764 }
765
766 void EDL::set_outpoint(double position)
767 {
768         if(equivalent(local_session->get_outpoint(), position) &&
769                 local_session->get_outpoint() >= 0)
770         {
771                 local_session->unset_outpoint();
772         }
773         else
774         {
775                 local_session->set_outpoint(align_to_frame(position, 0));
776                 if(local_session->get_inpoint() >= local_session->get_outpoint())
777                         local_session->unset_inpoint();
778         }
779 }
780
781
782 int EDL::clear(double start,
783         double end,
784         int clear_labels,
785         int clear_plugins,
786         int edit_autos)
787 {
788         if(start == end)
789         {
790                 double distance = 0;
791                 tracks->clear_handle(start,
792                         end,
793                         distance,
794                         clear_labels,
795                         clear_plugins,
796                         edit_autos);
797                 if(clear_labels && distance > 0)
798                         labels->paste_silence(start,
799                                 start + distance);
800         }
801         else
802         {
803                 tracks->clear(start,
804                         end,
805                         clear_plugins,
806                         edit_autos);
807                 if(clear_labels)
808                         labels->clear(start,
809                                 end,
810                                 1);
811         }
812
813 // Need to put at beginning so a subsequent paste operation starts at the
814 // right position.
815         double position = local_session->get_selectionstart();
816         local_session->set_selectionend(position);
817         local_session->set_selectionstart(position);
818         return 0;
819 }
820
821 void EDL::modify_edithandles(double oldposition,
822         double newposition,
823         int currentend,
824         int handle_mode,
825         int edit_labels,
826         int edit_plugins,
827         int edit_autos)
828 {
829         tracks->modify_edithandles(oldposition,
830                 newposition,
831                 currentend,
832                 handle_mode,
833                 edit_labels,
834                 edit_plugins,
835                 edit_autos);
836         labels->modify_handles(oldposition,
837                 newposition,
838                 currentend,
839                 handle_mode,
840                 edit_labels);
841 }
842
843 void EDL::modify_pluginhandles(double oldposition,
844         double newposition,
845         int currentend,
846         int handle_mode,
847         int edit_labels,
848         int edit_autos,
849         Edits *trim_edits)
850 {
851         tracks->modify_pluginhandles(oldposition,
852                 newposition,
853                 currentend,
854                 handle_mode,
855                 edit_labels,
856                 edit_autos,
857                 trim_edits);
858         optimize();
859 }
860
861 void EDL::paste_silence(double start,
862         double end,
863         int edit_labels,
864         int edit_plugins,
865         int edit_autos)
866 {
867         if(edit_labels)
868                 labels->paste_silence(start, end);
869         tracks->paste_silence(start,
870                 end,
871                 edit_plugins,
872                 edit_autos);
873 }
874
875
876 void EDL::remove_from_project(ArrayList<EDL*> *clips)
877 {
878         for(int i = 0; i < clips->size(); i++)
879         {
880                 for(int j = 0; j < this->clips.size(); j++)
881                 {
882                         if(this->clips.get(j) == clips->values[i])
883                         {
884                                 EDL *clip = this->clips.get(j);
885                                 this->clips.remove(clip);
886                                 clip->Garbage::remove_user();
887                         }
888                 }
889         }
890 }
891
892 void EDL::remove_from_project(ArrayList<Indexable*> *assets)
893 {
894 // Remove from clips
895         if(!parent_edl)
896                 for(int j = 0; j < clips.total; j++)
897                 {
898                         clips.values[j]->remove_from_project(assets);
899                 }
900
901 // Remove from VWindow EDLs
902         for(int i = 0; i < total_vwindow_edls(); i++)
903                 get_vwindow_edl(i)->remove_from_project(assets);
904
905         for(int i = 0; i < assets->size(); i++)
906         {
907 // Remove from tracks
908                 for(Track *track = tracks->first; track; track = track->next)
909                 {
910                         track->remove_asset(assets->get(i));
911                 }
912
913 // Remove from assets
914                 if(!parent_edl && assets->get(i)->is_asset)
915                 {
916                         this->assets->remove_asset((Asset*)assets->get(i));
917                 }
918                 else
919                 if(!parent_edl && !assets->get(i)->is_asset)
920                 {
921                         this->nested_edls->remove_edl((EDL*)assets->get(i));
922                 }
923         }
924 }
925
926 void EDL::update_assets(EDL *src)
927 {
928         for(Asset *current = src->assets->first;
929                 current;
930                 current = NEXT)
931         {
932                 assets->update(current);
933         }
934 }
935
936 int EDL::get_tracks_height(Theme *theme)
937 {
938         int total_pixels = 0;
939         for(Track *current = tracks->first;
940                 current;
941                 current = NEXT)
942         {
943                 total_pixels += current->vertical_span(theme);
944         }
945         return total_pixels;
946 }
947
948 int64_t EDL::get_tracks_width()
949 {
950         int64_t total_pixels = 0;
951         for(Track *current = tracks->first;
952                 current;
953                 current = NEXT)
954         {
955                 int64_t pixels = current->horizontal_span();
956                 if(pixels > total_pixels) total_pixels = pixels;
957         }
958 //printf("EDL::get_tracks_width %d\n", total_pixels);
959         return total_pixels;
960 }
961
962 // int EDL::calculate_output_w(int single_channel)
963 // {
964 //      if(single_channel) return session->output_w;
965 //
966 //      int widest = 0;
967 //      for(int i = 0; i < session->video_channels; i++)
968 //      {
969 //              if(session->vchannel_x[i] + session->output_w > widest) widest = session->vchannel_x[i] + session->output_w;
970 //      }
971 //      return widest;
972 // }
973 //
974 // int EDL::calculate_output_h(int single_channel)
975 // {
976 //      if(single_channel) return session->output_h;
977 //
978 //      int tallest = 0;
979 //      for(int i = 0; i < session->video_channels; i++)
980 //      {
981 //              if(session->vchannel_y[i] + session->output_h > tallest) tallest = session->vchannel_y[i] + session->output_h;
982 //      }
983 //      return tallest;
984 // }
985
986 // Get the total output size scaled to aspect ratio
987 void EDL::calculate_conformed_dimensions(int single_channel, float &w, float &h)
988 {
989         w = session->output_w;
990         h = session->output_h;
991
992         if((float)session->output_w / session->output_h > get_aspect_ratio())
993         {
994                 h = (float)h *
995                         (session->output_w / get_aspect_ratio() / session->output_h);
996         }
997         else
998         {
999                 w = (float)w *
1000                         (h * get_aspect_ratio() / session->output_w);
1001         }
1002 }
1003
1004 float EDL::get_aspect_ratio()
1005 {
1006         return session->aspect_w / session->aspect_h;
1007 }
1008
1009 int EDL::dump(FILE *fp)
1010 {
1011         if(parent_edl)
1012                 fprintf(fp,"CLIP\n");
1013         else
1014                 fprintf(fp,"EDL\n");
1015         fprintf(fp,"  clip_title: %s\n"
1016                 "  parent_edl: %p\n", local_session->clip_title, parent_edl);
1017         fprintf(fp,"  selectionstart %f\n  selectionend %f\n  loop_start %f\n  loop_end %f\n",
1018                 local_session->get_selectionstart(1),
1019                 local_session->get_selectionend(1),
1020                 local_session->loop_start,
1021                 local_session->loop_end);
1022         for(int i = 0; i < TOTAL_PANES; i++)
1023         {
1024                 fprintf(fp,"  pane %d view_start=%jd track_start=%jd\n", i,
1025                         local_session->view_start[i],
1026                         local_session->track_start[i]);
1027         }
1028
1029         if(!parent_edl)
1030         {
1031                 fprintf(fp,"audio_channels: %d audio_tracks: %d sample_rate: " _LD "\n",
1032                         session->audio_channels,
1033                         session->audio_tracks,
1034                         session->sample_rate);
1035                 fprintf(fp,"  video_channels: %d\n"
1036                         "  video_tracks: %d\n"
1037                         "  frame_rate: %.2f\n"
1038                         "  frames_per_foot: %.2f\n"
1039                         "  output_w: %d\n"
1040                         "  output_h: %d\n"
1041                         "  aspect_w: %f\n"
1042                         "  aspect_h: %f\n"
1043                         "  color_model: %d\n",
1044                                 session->video_channels,
1045                                 session->video_tracks,
1046                                 session->frame_rate,
1047                                 session->frames_per_foot,
1048                                 session->output_w,
1049                                 session->output_h,
1050                                 session->aspect_w,
1051                                 session->aspect_h,
1052                                 session->color_model);
1053
1054                 fprintf(fp," CLIPS\n");
1055                 fprintf(fp,"  total: %d\n", clips.total);
1056
1057                 for(int i = 0; i < clips.total; i++)
1058                 {
1059                         fprintf(fp,"\n\n");
1060                         clips.values[i]->dump(fp);
1061                         fprintf(fp,"\n\n");
1062                 }
1063
1064                 fprintf(fp," VWINDOW EDLS\n");
1065                 fprintf(fp,"  total: %d\n", total_vwindow_edls());
1066
1067                 for(int i = 0; i < total_vwindow_edls(); i++)
1068                 {
1069                         fprintf(fp,"   %s\n", get_vwindow_edl(i)->local_session->clip_title);
1070                 }
1071
1072                 fprintf(fp," ASSETS\n");
1073                 assets->dump(fp);
1074         }
1075         fprintf(fp," LABELS\n");
1076         labels->dump(fp);
1077         fprintf(fp," TRACKS\n");
1078         tracks->dump(fp);
1079 //printf("EDL::dump 2\n");
1080         return 0;
1081 }
1082
1083 EDL* EDL::add_clip(EDL *edl)
1084 {
1085 // Copy argument.  New edls are deleted from MWindow::load_filenames.
1086         EDL *new_edl = new EDL(this);
1087         new_edl->create_objects();
1088         new_edl->copy_all(edl);
1089         clips.append(new_edl);
1090         return new_edl;
1091 }
1092
1093 void EDL::insert_asset(Asset *asset,
1094         EDL *nested_edl,
1095         double position,
1096         Track *first_track,
1097         RecordLabels *labels)
1098 {
1099 // Insert asset into asset table
1100         Asset *new_asset = 0;
1101         EDL *new_nested_edl = 0;
1102
1103         if(asset) new_asset = assets->update(asset);
1104         if(nested_edl) new_nested_edl = nested_edls->get_copy(nested_edl);
1105
1106 // Paste video
1107         int vtrack = 0;
1108         Track *current = first_track ? first_track : tracks->first;
1109
1110
1111 // Fix length of single frame
1112         double length = 0.;
1113         int layers = 0;
1114         int channels = 0;
1115
1116         if(new_nested_edl)
1117         {
1118                 length = new_nested_edl->tracks->total_playable_length();
1119                 layers = 1;
1120                 channels = new_nested_edl->session->audio_channels;
1121         }
1122
1123         if(new_asset)
1124         {
1125 // Insert 1 frame for undefined length
1126                 if(new_asset->video_length < 0)
1127                         length = 1.0 / session->frame_rate;
1128                 else
1129                 if(new_asset->frame_rate > 0)
1130                         length = ((double)new_asset->video_length / new_asset->frame_rate);
1131                 else
1132                         length = 1.0 / session->frame_rate;
1133                 layers = new_asset->layers;
1134                 channels = new_asset->channels;
1135         }
1136
1137         for( ;
1138                 current && vtrack < layers;
1139                 current = NEXT)
1140         {
1141                 if(!current->record ||
1142                         current->data_type != TRACK_VIDEO)
1143                         continue;
1144
1145                 current->insert_asset(new_asset,
1146                         new_nested_edl,
1147                         length,
1148                         position,
1149                         vtrack);
1150
1151                 vtrack++;
1152         }
1153
1154         int atrack = 0;
1155         if(new_asset)
1156         {
1157                 if(new_asset->audio_length < 0)
1158                 {
1159 // Insert 1 frame for undefined length & video
1160                         if(new_asset->video_data)
1161                                 length = (double)1.0 / new_asset->frame_rate;
1162                         else
1163 // Insert 1 second for undefined length & no video
1164                                 length = 1.0;
1165                 }
1166                 else
1167                         length = (double)new_asset->audio_length /
1168                                         new_asset->sample_rate;
1169         }
1170
1171         for(current = tracks->first;
1172                 current && atrack < channels;
1173                 current = NEXT)
1174         {
1175                 if(!current->record ||
1176                         current->data_type != TRACK_AUDIO)
1177                         continue;
1178
1179                 current->insert_asset(new_asset,
1180                         new_nested_edl,
1181                         length,
1182                         position,
1183                         atrack);
1184
1185
1186                 atrack++;
1187         }
1188
1189 // Insert labels from a recording window.
1190         if(labels)
1191         {
1192                 for(RecordLabel *label = labels->first; label; label = label->next)
1193                 {
1194                         this->labels->toggle_label(label->position, label->position);
1195                 }
1196         }
1197 }
1198
1199
1200
1201 void EDL::set_index_file(Indexable *indexable)
1202 {
1203         if(indexable->is_asset)
1204                 assets->update_index((Asset*)indexable);
1205         else
1206                 nested_edls->update_index((EDL*)indexable);
1207 }
1208
1209 void EDL::optimize()
1210 {
1211 //printf("EDL::optimize 1\n");
1212         if(local_session->preview_start < 0) local_session->preview_start = 0;
1213         double length = tracks->total_length();
1214         if(local_session->preview_end > length) local_session->preview_end = length;
1215         if(local_session->preview_start >= local_session->preview_end ) {
1216                 local_session->preview_start = 0;
1217                 local_session->preview_end = length;
1218         }
1219         for(Track *current = tracks->first; current; current = NEXT)
1220                 current->optimize();
1221 }
1222
1223 int EDL::next_id()
1224 {
1225         id_lock->lock("EDL::next_id");
1226         int result = EDLSession::current_id++;
1227         id_lock->unlock();
1228         return result;
1229 }
1230
1231 void EDL::get_shared_plugins(Track *source,
1232         ArrayList<SharedLocation*> *plugin_locations,
1233         int omit_recordable,
1234         int data_type)
1235 {
1236         for(Track *track = tracks->first; track; track = track->next)
1237         {
1238                 if(!track->record || !omit_recordable)
1239                 {
1240                         if(track != source &&
1241                                 track->data_type == data_type)
1242                         {
1243                                 for(int i = 0; i < track->plugin_set.total; i++)
1244                                 {
1245                                         Plugin *plugin = track->get_current_plugin(
1246                                                 local_session->get_selectionstart(1),
1247                                                 i,
1248                                                 PLAY_FORWARD,
1249                                                 1,
1250                                                 0);
1251                                         if(plugin && plugin->plugin_type == PLUGIN_STANDALONE)
1252                                         {
1253                                                 plugin_locations->append(new SharedLocation(tracks->number_of(track), i));
1254                                         }
1255                                 }
1256                         }
1257                 }
1258         }
1259 }
1260
1261 void EDL::get_shared_tracks(Track *track,
1262         ArrayList<SharedLocation*> *module_locations,
1263         int omit_recordable,
1264         int data_type)
1265 {
1266         for(Track *current = tracks->first; current; current = NEXT)
1267         {
1268                 if(!omit_recordable || !current->record)
1269                 {
1270                         if(current != track &&
1271                                 current->data_type == data_type)
1272                         {
1273                                 module_locations->append(new SharedLocation(tracks->number_of(current), 0));
1274                         }
1275                 }
1276         }
1277 }
1278
1279 // Convert position to frames if cursor alignment is enabled
1280 double EDL::align_to_frame(double position, int round)
1281 {
1282 //printf("EDL::align_to_frame 1 %f\n", position);
1283         if(session->cursor_on_frames)
1284         {
1285 // Seconds -> Frames
1286                 double temp = (double)position * session->frame_rate;
1287 //printf("EDL::align_to_frame 2 %f\n", temp);
1288
1289 // Assert some things
1290                 if(session->sample_rate == 0)
1291                         printf("EDL::align_to_frame: sample_rate == 0\n");
1292
1293                 if(session->frame_rate == 0)
1294                         printf("EDL::align_to_frame: frame_rate == 0\n");
1295
1296 // Round frames
1297 // Always round down negative numbers
1298 // but round up only if requested
1299                 if(round)
1300                 {
1301                         temp = Units::round(temp);
1302                 }
1303                 else
1304                 {
1305 //                      if(temp < 0)
1306 //                      {
1307 //                              temp -= 0.5;
1308 //                      }
1309 //                      else
1310                                 temp = Units::to_int64(temp);
1311                 }
1312 //printf("EDL::align_to_frame 3 %f\n", temp);
1313
1314 // Frames -> Seconds
1315                 temp /= session->frame_rate;
1316
1317 //printf("EDL::align_to_frame 5 %f\n", temp);
1318
1319                 return temp;
1320         }
1321 //printf("EDL::align_to_frame 3 %d\n", position);
1322
1323
1324         return position;
1325 }
1326
1327
1328 void EDL::new_folder(const char *folder)
1329 {
1330         for(int i = 0; i < folders.total; i++)
1331         {
1332                 if(!strcasecmp(folders.values[i], folder)) return;
1333         }
1334
1335         char *new_folder;
1336         folders.append(new_folder = new char[strlen(folder) + 1]);
1337         strcpy(new_folder, folder);
1338 }
1339
1340 void EDL::delete_folder(char *folder)
1341 {
1342         int i;
1343         for(i = 0; i < folders.total; i++)
1344         {
1345                 if(!strcasecmp(folders.values[i], folder))
1346                 {
1347                         break;
1348                 }
1349         }
1350
1351         if(i < folders.total) delete folders.values[i];
1352
1353         for( ; i < folders.total - 1; i++)
1354         {
1355                 folders.values[i] = folders.values[i + 1];
1356         }
1357 }
1358
1359 int EDL::get_use_vconsole(VEdit* *playable_edit,
1360         int64_t position,
1361         int direction,
1362         PlayableTracks *playable_tracks)
1363 {
1364         int share_playable_tracks = 1;
1365         int result = 0;
1366         VTrack *playable_track = 0;
1367         const int debug = 0;
1368         *playable_edit = 0;
1369
1370 // Calculate playable tracks when being called as a nested EDL
1371         if(!playable_tracks)
1372         {
1373                 share_playable_tracks = 0;
1374                 playable_tracks = new PlayableTracks(this,
1375                         position,
1376                         direction,
1377                         TRACK_VIDEO,
1378                         1);
1379         }
1380
1381
1382 // Total number of playable tracks is 1
1383         if(playable_tracks->size() != 1)
1384         {
1385                 result = 1;
1386         }
1387         else
1388         {
1389                 playable_track = (VTrack*)playable_tracks->get(0);
1390         }
1391
1392 // Don't need playable tracks anymore
1393         if(!share_playable_tracks)
1394         {
1395                 delete playable_tracks;
1396         }
1397
1398 if(debug) printf("EDL::get_use_vconsole %d playable_tracks->size()=%d\n",
1399 __LINE__,
1400 playable_tracks->size());
1401         if(result) return 1;
1402
1403
1404 // Test mutual conditions between direct copy rendering and this.
1405         if(!playable_track->direct_copy_possible(position,
1406                 direction,
1407                 1))
1408                 return 1;
1409 if(debug) printf("EDL::get_use_vconsole %d\n", __LINE__);
1410
1411         *playable_edit = (VEdit*)playable_track->edits->editof(position,
1412                 direction,
1413                 0);
1414 // No edit at current location
1415         if(!*playable_edit) return 1;
1416 if(debug) printf("EDL::get_use_vconsole %d\n", __LINE__);
1417
1418
1419 // Edit is nested EDL
1420         if((*playable_edit)->nested_edl)
1421         {
1422 // Test nested EDL
1423                 EDL *nested_edl = (*playable_edit)->nested_edl;
1424                 int64_t nested_position = (int64_t)((position -
1425                                 (*playable_edit)->startproject +
1426                                 (*playable_edit)->startsource) *
1427                         nested_edl->session->frame_rate /
1428                         session->frame_rate);
1429
1430
1431                 VEdit *playable_edit_temp = 0;
1432                 if(session->output_w != nested_edl->session->output_w ||
1433                         session->output_h != nested_edl->session->output_h ||
1434                         nested_edl->get_use_vconsole(&playable_edit_temp,
1435                                 nested_position,
1436                                 direction,
1437                                 0))
1438                         return 1;
1439
1440                 return 0;
1441         }
1442
1443 if(debug) printf("EDL::get_use_vconsole %d\n", __LINE__);
1444 // Edit is not a nested EDL
1445 // Edit is silence
1446         if(!(*playable_edit)->asset) return 1;
1447 if(debug) printf("EDL::get_use_vconsole %d\n", __LINE__);
1448
1449
1450 // Asset and output device must have the same dimensions
1451         if((*playable_edit)->asset->width != session->output_w ||
1452                 (*playable_edit)->asset->height != session->output_h)
1453                 return 1;
1454
1455
1456 if(debug) printf("EDL::get_use_vconsole %d\n", __LINE__);
1457
1458
1459
1460 // If we get here the frame is going to be directly copied.  Whether it is
1461 // decompressed in hardware depends on the colormodel.
1462         return 0;
1463 }
1464
1465
1466 // For Indexable
1467 int EDL::get_audio_channels()
1468 {
1469         return session->audio_channels;
1470 }
1471
1472 int EDL::get_sample_rate()
1473 {
1474         return session->sample_rate;
1475 }
1476
1477 int64_t EDL::get_audio_samples()
1478 {
1479         return (int64_t)(tracks->total_playable_length() *
1480                 session->sample_rate);
1481 }
1482
1483 int EDL::have_audio()
1484 {
1485         return 1;
1486 }
1487
1488 int EDL::have_video()
1489 {
1490         return 1;
1491 }
1492
1493
1494 int EDL::get_w()
1495 {
1496         return session->output_w;
1497 }
1498
1499 int EDL::get_h()
1500 {
1501         return session->output_h;
1502 }
1503
1504 double EDL::get_frame_rate()
1505 {
1506         return session->frame_rate;
1507 }
1508
1509 int EDL::get_video_layers()
1510 {
1511         return 1;
1512 }
1513
1514 int64_t EDL::get_video_frames()
1515 {
1516         return (int64_t)(tracks->total_playable_length() *
1517                 session->frame_rate);
1518 }
1519
1520
1521 void EDL::remove_vwindow_edls()
1522 {
1523         for(int i = 0; i < total_vwindow_edls(); i++)
1524         {
1525                 get_vwindow_edl(i)->Garbage::remove_user();
1526         }
1527         vwindow_edls.remove_all();
1528 }
1529
1530 void EDL::remove_vwindow_edl(EDL *edl)
1531 {
1532         if(vwindow_edls.number_of(edl) >= 0)
1533         {
1534                 edl->Garbage::remove_user();
1535
1536                 vwindow_edls.remove(edl);
1537         }
1538 }
1539
1540
1541 EDL* EDL::get_vwindow_edl(int number)
1542 {
1543         return vwindow_edls.get(number);
1544 }
1545
1546 int EDL::total_vwindow_edls()
1547 {
1548         return vwindow_edls.size();
1549 }
1550
1551 void EDL::append_vwindow_edl(EDL *edl, int increase_counter)
1552 {
1553         if(vwindow_edls.number_of(edl) >= 0) return;
1554
1555         if(increase_counter) edl->Garbage::add_user();
1556         vwindow_edls.append(edl);
1557 }
1558
1559