initial commit
[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 "colormodels.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
398         index_state->copy_from(edl->index_state);
399         nested_edls->clear();
400         copy_session(edl);
401         copy_assets(edl);
402         copy_clips(edl);
403         tracks->copy_from(edl->tracks);
404         labels->copy_from(edl->labels);
405         return 0;
406 }
407
408 void EDL::copy_clips(EDL *edl)
409 {
410         if(this == edl) return;
411
412         remove_vwindow_edls();
413
414 //      if(vwindow_edl && !vwindow_edl_shared) 
415 //              vwindow_edl->Garbage::remove_user();
416 //      vwindow_edl = 0;
417 //      vwindow_edl_shared = 0;
418
419         for(int i = 0; i < edl->total_vwindow_edls(); i++)
420         {
421                 EDL *new_edl = new EDL(this);
422                 new_edl->create_objects();
423                 new_edl->copy_all(edl->get_vwindow_edl(i));
424                 append_vwindow_edl(new_edl, 0);
425         }
426
427         for(int i = 0; i < clips.size(); i++)
428                 clips.get(i)->Garbage::remove_user();
429         clips.remove_all();
430         for(int i = 0; i < edl->clips.total; i++)
431         {
432                 add_clip(edl->clips.values[i]);
433         }
434 }
435
436 void EDL::copy_assets(EDL *edl)
437 {
438         if(this == edl) return;
439
440         if(!parent_edl)
441         {
442                 assets->copy_from(edl->assets);
443         }
444 }
445
446 void EDL::copy_session(EDL *edl, int session_only)
447 {
448         if(this == edl) return;
449
450         if(!session_only)
451         {
452                 strcpy(this->path, edl->path);
453 //printf("EDL::copy_session %p %s\n", this, this->path);
454
455                 folders.remove_all_objects();
456                 for(int i = 0; i < edl->folders.total; i++)
457                 {
458                         char *new_folder;
459                         folders.append(new_folder = new char[strlen(edl->folders.values[i]) + 1]);
460                         strcpy(new_folder, edl->folders.values[i]);
461                 }
462         }
463
464         if(!parent_edl)
465         {
466                 session->copy(edl->session);
467         }
468
469         if(!session_only)
470         {
471                 local_session->copy_from(edl->local_session);
472         }
473 }
474
475 int EDL::copy_assets(double start, 
476         double end, 
477         FileXML *file, 
478         int all, 
479         const char *output_path)
480 {
481         ArrayList<Asset*> asset_list;
482         Track* current;
483
484         file->tag.set_title("ASSETS");
485         file->append_tag();
486         file->append_newline();
487
488 // Copy everything for a save
489         if(all)
490         {
491                 for(Asset *asset = assets->first;
492                         asset;
493                         asset = asset->next)
494                 {
495                         asset_list.append(asset);
496                 }
497         }
498         else
499 // Copy just the ones being used.
500         {
501                 for(current = tracks->first; 
502                         current; 
503                         current = NEXT)
504                 {
505                         if(current->record)
506                         {
507                                 current->copy_assets(start, 
508                                         end, 
509                                         &asset_list);
510                         }
511                 }
512         }
513
514 // Paths relativised here
515         for(int i = 0; i < asset_list.total; i++)
516         {
517                 asset_list.values[i]->write(file, 
518                         0, 
519                         output_path);
520         }
521
522         file->tag.set_title("/ASSETS");
523         file->append_tag();
524         file->append_newline();
525         file->append_newline();
526         return 0;
527 }
528
529 int EDL::copy(double start, 
530         double end, 
531         int all, 
532         int is_clip,
533         int is_vwindow,
534         FileXML *file, 
535         const char *output_path,
536         int rewind_it)
537 {
538 //printf("EDL::copy 1\n");
539 // begin file
540         if(is_clip)
541                 file->tag.set_title("CLIP_EDL");
542         else
543         if(is_vwindow)
544                 file->tag.set_title("VWINDOW_EDL");
545         else
546         {
547                 file->tag.set_title("EDL");
548                 file->tag.set_property("VERSION", CINELERRA_VERSION);
549 // Save path for restoration of the project title from a backup.
550                 if(this->path[0])
551                 {
552                         file->tag.set_property("PATH", path);
553                 }
554         }
555
556         file->append_tag();
557         file->append_newline();
558
559 // Set clipboard samples only if copying to clipboard
560         if(!all)
561         {
562                 file->tag.set_title("CLIPBOARD");
563                 file->tag.set_property("LENGTH", end - start);
564                 file->append_tag();
565                 file->append_newline();
566                 file->append_newline();
567         }
568 //printf("EDL::copy 1\n");
569
570 // Sessions
571         local_session->save_xml(file, start);
572
573 //printf("EDL::copy 1\n");
574
575 // Top level stuff.
576 //      if(!parent_edl)
577         {
578 // Need to copy all this from child EDL if pasting is desired.
579 // Session
580                 session->save_xml(file);
581                 session->save_video_config(file);
582                 session->save_audio_config(file);
583
584 // Folders
585                 for(int i = 0; i < folders.total; i++)
586                 {
587                         file->tag.set_title("FOLDER");
588                         file->append_tag();
589                         file->append_text(folders.values[i]);
590                         file->tag.set_title("/FOLDER");
591                         file->append_tag();
592                         file->append_newline();
593                 }
594
595 // Media
596 // Don't replicate all assets for every clip.
597 // The assets for the clips are probably in the mane EDL.
598                 if(!is_clip)
599                         copy_assets(start, 
600                                 end, 
601                                 file, 
602                                 all, 
603                                 output_path);
604
605 // Clips
606 // Don't want this if using clipboard
607                 if(all)
608                 {
609                         for(int i = 0; i < total_vwindow_edls(); i++)
610                         {
611                                 get_vwindow_edl(i)->save_xml(file, 
612                                         output_path,
613                                         0,
614                                         1);
615                         }
616
617                         for(int i = 0; i < clips.total; i++)
618                                 clips.values[i]->save_xml(file, 
619                                         output_path,
620                                         1,
621                                         0);
622                 }
623
624                 file->append_newline();
625                 file->append_newline();
626         }
627
628
629 //printf("EDL::copy 1\n");
630
631         labels->copy(start, end, file);
632 //printf("EDL::copy 1\n");
633         tracks->copy(start, end, all, file, output_path);
634 //printf("EDL::copy 2\n");
635
636 // terminate file
637         if(is_clip)
638                 file->tag.set_title("/CLIP_EDL");
639         else
640         if(is_vwindow)
641                 file->tag.set_title("/VWINDOW_EDL");
642         else
643                 file->tag.set_title("/EDL");
644         file->append_tag();
645         file->append_newline();
646
647
648 // For editing operations we want to rewind it for immediate pasting.
649 // For clips and saving to disk leave it alone.
650         if(rewind_it)
651         {
652                 file->terminate_string();
653                 file->rewind();
654         }
655         return 0;
656 }
657
658 void EDL::rechannel()
659 {
660         for(Track *current = tracks->first; current; current = NEXT)
661         {
662                 if(current->data_type == TRACK_AUDIO)
663                 {
664                         PanAutos *autos = (PanAutos*)current->automation->autos[AUTOMATION_PAN];
665                         ((PanAuto*)autos->default_auto)->rechannel();
666                         for(PanAuto *keyframe = (PanAuto*)autos->first;
667                                 keyframe;
668                                 keyframe = (PanAuto*)keyframe->next)
669                         {
670                                 keyframe->rechannel();
671                         }
672                 }
673         }
674 }
675
676 void EDL::resample(double old_rate, double new_rate, int data_type)
677 {
678         for(Track *current = tracks->first; current; current = NEXT)
679         {
680                 if(current->data_type == data_type)
681                 {
682                         current->resample(old_rate, new_rate);
683                 }
684         }
685 }
686
687
688 void EDL::synchronize_params(EDL *edl)
689 {
690         local_session->synchronize_params(edl->local_session);
691         for(Track *this_track = tracks->first, *that_track = edl->tracks->first; 
692                 this_track && that_track; 
693                 this_track = this_track->next,
694                 that_track = that_track->next)
695         {
696                 this_track->synchronize_params(that_track);
697         }
698 }
699
700 int EDL::trim_selection(double start, 
701         double end,
702         int edit_labels,
703         int edit_plugins,
704         int edit_autos)
705 {
706         if(start != end)
707         {
708 // clear the data
709                 clear(0, 
710                         start,
711                         edit_labels,
712                         edit_plugins,
713                         edit_autos);
714                 clear(end - start, 
715                         tracks->total_length(),
716                         edit_labels,
717                         edit_plugins,
718                         edit_autos);
719         }
720         return 0;
721 }
722
723
724 int EDL::equivalent(double position1, double position2)
725 {
726         double threshold = (double).5 / session->frame_rate;
727         if(session->cursor_on_frames) 
728                 threshold = (double).5 / session->frame_rate;
729         else
730                 threshold = (double)1 / session->sample_rate;
731
732         if(fabs(position2 - position1) < threshold)
733         return 1;
734     else
735         return 0;
736 }
737
738 double EDL::equivalent_output(EDL *edl)
739 {
740         double result = -1;
741         session->equivalent_output(edl->session, &result);
742         tracks->equivalent_output(edl->tracks, &result);
743         return result;
744 }
745
746
747 void EDL::set_path(char *path)
748 {
749         strcpy(this->path, path);
750 }
751
752 void EDL::set_inpoint(double position)
753 {
754         if(equivalent(local_session->get_inpoint(), position) && 
755                 local_session->get_inpoint() >= 0)
756         {
757                 local_session->unset_inpoint();
758         }
759         else
760         {
761                 local_session->set_inpoint(align_to_frame(position, 0));
762                 if(local_session->get_outpoint() <= local_session->get_inpoint()) 
763                         local_session->unset_outpoint();
764         }
765 }
766
767 void EDL::set_outpoint(double position)
768 {
769         if(equivalent(local_session->get_outpoint(), position) && 
770                 local_session->get_outpoint() >= 0)
771         {
772                 local_session->unset_outpoint();
773         }
774         else
775         {
776                 local_session->set_outpoint(align_to_frame(position, 0));
777                 if(local_session->get_inpoint() >= local_session->get_outpoint()) 
778                         local_session->unset_inpoint();
779         }
780 }
781
782
783 int EDL::clear(double start, 
784         double end, 
785         int clear_labels,
786         int clear_plugins,
787         int edit_autos)
788 {
789         if(start == end)
790         {
791                 double distance = 0;
792                 tracks->clear_handle(start, 
793                         end,
794                         distance, 
795                         clear_labels,
796                         clear_plugins,
797                         edit_autos);
798                 if(clear_labels && distance > 0)
799                         labels->paste_silence(start, 
800                                 start + distance);
801         }
802         else
803         {
804                 tracks->clear(start, 
805                         end,
806                         clear_plugins,
807                         edit_autos);
808                 if(clear_labels) 
809                         labels->clear(start, 
810                                 end, 
811                                 1);
812         }
813
814 // Need to put at beginning so a subsequent paste operation starts at the
815 // right position.
816         double position = local_session->get_selectionstart();
817         local_session->set_selectionend(position);
818         local_session->set_selectionstart(position);
819         return 0;
820 }
821
822 void EDL::modify_edithandles(double oldposition, 
823         double newposition, 
824         int currentend,
825         int handle_mode,
826         int edit_labels,
827         int edit_plugins,
828         int edit_autos)
829 {
830         tracks->modify_edithandles(oldposition, 
831                 newposition, 
832                 currentend,
833                 handle_mode,
834                 edit_labels, 
835                 edit_plugins,
836                 edit_autos);
837         labels->modify_handles(oldposition, 
838                 newposition, 
839                 currentend,
840                 handle_mode,
841                 edit_labels);
842 }
843
844 void EDL::modify_pluginhandles(double oldposition, 
845         double newposition, 
846         int currentend, 
847         int handle_mode,
848         int edit_labels,
849         int edit_autos,
850         Edits *trim_edits)
851 {
852         tracks->modify_pluginhandles(oldposition, 
853                 newposition, 
854                 currentend, 
855                 handle_mode,
856                 edit_labels,
857                 edit_autos,
858                 trim_edits);
859         optimize();
860 }
861
862 void EDL::paste_silence(double start, 
863         double end, 
864         int edit_labels, 
865         int edit_plugins,
866         int edit_autos)
867 {
868         if(edit_labels) 
869                 labels->paste_silence(start, end);
870         tracks->paste_silence(start, 
871                 end, 
872                 edit_plugins,
873                 edit_autos);
874 }
875
876
877 void EDL::remove_from_project(ArrayList<EDL*> *clips)
878 {
879         for(int i = 0; i < clips->size(); i++)
880         {
881                 for(int j = 0; j < this->clips.size(); j++)
882                 {
883                         if(this->clips.get(j) == clips->values[i])
884                         {
885                                 this->clips.get(j)->Garbage::remove_user();
886                                 this->clips.remove(this->clips.get(j));
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                 vwindow_edls.remove(edl);
1536         }
1537 }
1538
1539
1540 EDL* EDL::get_vwindow_edl(int number)
1541 {
1542         return vwindow_edls.get(number);
1543 }
1544
1545 int EDL::total_vwindow_edls()
1546 {
1547         return vwindow_edls.size();
1548 }
1549
1550 void EDL::append_vwindow_edl(EDL *edl, int increase_counter)
1551 {
1552         if(vwindow_edls.number_of(edl) >= 0) return;
1553
1554         if(increase_counter) edl->Garbage::add_user();
1555         vwindow_edls.append(edl);
1556 }
1557
1558