640092d1c6b309f2c45c64798411f9fd4b3dc6bc
[goodguy/cinelerra.git] / cinelerra-5.1 / 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.h"
28 #include "bccmodels.h"
29 #include "bchash.h"
30 #include "bcsignals.h"
31 #include "clip.h"
32 #include "cstrdup.h"
33 #include "clipedls.h"
34 #include "edits.h"
35 #include "edl.h"
36 #include "edlsession.h"
37 #include "filexml.h"
38 #include "floatauto.h"
39 #include "floatautos.h"
40 #include "guicast.h"
41 #include "keyframe.h"
42 #include "keyframes.h"
43 #include "indexstate.h"
44 #include "interlacemodes.h"
45 #include "labels.h"
46 #include "localsession.h"
47 #include "maskautos.h"
48 #include "mutex.h"
49 #include "panauto.h"
50 #include "panautos.h"
51 #include "playbackconfig.h"
52 #include "playabletracks.h"
53 #include "plugin.h"
54 #include "pluginset.h"
55 #include "preferences.h"
56 #include "recordconfig.h"
57 #include "recordlabel.h"
58 #include "sharedlocation.h"
59 #include "theme.h"
60 #include "tracks.h"
61 #include "transportque.inc"
62 #include "versioninfo.h"
63 #include "vedit.h"
64 #include "vtrack.h"
65
66
67
68
69 EDL::EDL(EDL *parent_edl)
70  : Indexable(0)
71 {
72         this->parent_edl = parent_edl;
73         tracks = 0;
74         labels = 0;
75         local_session = 0;
76         id = next_id();
77         path[0] = 0;
78 }
79
80
81 EDL::~EDL()
82 {
83
84         delete tracks;
85         delete labels;
86         delete local_session;
87         remove_vwindow_edls();
88         if( !parent_edl ) {
89                 delete assets;
90                 delete session;
91         }
92 }
93
94
95 void EDL::create_objects()
96 {
97         tracks = new Tracks(this);
98         assets = !parent_edl ? new Assets(this) : parent_edl->assets;
99         session = !parent_edl ? new EDLSession(this) : parent_edl->session;
100         local_session = new LocalSession(this);
101         labels = new Labels(this, "LABELS");
102 }
103
104 EDL& EDL::operator=(EDL &edl)
105 {
106 printf("EDL::operator= 1\n");
107         copy_all(&edl);
108         return *this;
109 }
110
111 int EDL::load_defaults(BC_Hash *defaults)
112 {
113         if( !parent_edl )
114                 session->load_defaults(defaults);
115
116         local_session->load_defaults(defaults);
117         return 0;
118 }
119
120 int EDL::save_defaults(BC_Hash *defaults)
121 {
122         if( !parent_edl )
123                 session->save_defaults(defaults);
124
125         local_session->save_defaults(defaults);
126         return 0;
127 }
128
129 void EDL::boundaries()
130 {
131         session->boundaries();
132         local_session->boundaries();
133 }
134
135 int EDL::create_default_tracks()
136 {
137
138         for( int i=0; i<session->video_tracks; ++i ) {
139                 tracks->add_video_track(0, 0);
140         }
141         for( int i=0; i<session->audio_tracks; ++i ) {
142                 tracks->add_audio_track(0, 0);
143         }
144         return 0;
145 }
146
147 int EDL::load_xml(FileXML *file, uint32_t load_flags)
148 {
149         int result = 0;
150         folders.clear();
151
152         if( (load_flags & LOAD_ALL) == LOAD_ALL ) {
153                 remove_vwindow_edls();
154         }
155
156
157 // Search for start of master EDL.
158
159 // The parent_edl test caused clip creation to fail since those XML files
160 // contained an EDL tag.
161
162 // The parent_edl test is required to make EDL loading work because
163 // when loading an EDL the EDL tag is already read by the parent.
164
165         if( !parent_edl ) {
166                 do {
167                   result = file->read_tag();
168                 } while(!result &&
169                         !file->tag.title_is("XML") &&
170                         !file->tag.title_is("EDL"));
171         }
172         return result ? result : read_xml(file, load_flags);
173 }
174
175 int EDL::read_xml(FileXML *file, uint32_t load_flags)
176 {
177         int result = 0;
178 // Track numbering offset for replacing undo data.
179         int track_offset = 0;
180
181 // Get path for backups
182         file->tag.get_property("path", path);
183
184 // Erase everything
185         if( (load_flags & LOAD_ALL) == LOAD_ALL ||
186                 (load_flags & LOAD_EDITS) == LOAD_EDITS ) {
187                 while(tracks->last) delete tracks->last;
188         }
189
190         if( (load_flags & LOAD_ALL) == LOAD_ALL ) {
191                 clips.clear();
192                 mixers.remove_all_objects();
193         }
194
195         if( load_flags & LOAD_TIMEBAR ) {
196                 while(labels->last) delete labels->last;
197                 local_session->unset_inpoint();
198                 local_session->unset_outpoint();
199         }
200
201 // This was originally in LocalSession::load_xml
202         if( load_flags & LOAD_SESSION ) {
203                 local_session->clipboard_length = 0;
204         }
205
206         do {
207                 result = file->read_tag();
208
209                 if( !result ) {
210                         if( file->tag.title_is("/XML") ||
211                                 file->tag.title_is("/EDL") ||
212                                 file->tag.title_is("/CLIP_EDL") ||
213                                 file->tag.title_is("/NESTED_EDL") ||
214                                 file->tag.title_is("/VWINDOW_EDL") ) {
215                                 result = 1;
216                         }
217                         else
218                         if( file->tag.title_is("CLIPBOARD") ) {
219                                 local_session->clipboard_length =
220                                         file->tag.get_property("LENGTH", (double)0);
221                         }
222                         else
223                         if( file->tag.title_is("VIDEO") ) {
224                                 if( (load_flags & LOAD_VCONFIG) &&
225                                         (load_flags & LOAD_SESSION) )
226                                         session->load_video_config(file, 0, load_flags);
227                                 else
228                                         result = file->skip_tag();
229                         }
230                         else
231                         if( file->tag.title_is("AUDIO") ) {
232                                 if( (load_flags & LOAD_ACONFIG) &&
233                                         (load_flags & LOAD_SESSION) )
234                                         session->load_audio_config(file, 0, load_flags);
235                                 else
236                                         result = file->skip_tag();
237                         }
238                         else
239                         if( file->tag.title_is("FOLDERS") ) {
240                                 result = folders.load_xml(file);
241                         }
242                         else
243                         if( file->tag.title_is("MIXERS") ) {
244                                 if( (load_flags & LOAD_SESSION) )
245                                         mixers.load(file);
246                                 else
247                                         result = file->skip_tag();
248                         }
249                         else
250                         if( file->tag.title_is("ASSETS") ) {
251                                 if( load_flags & LOAD_ASSETS )
252                                         assets->load(file, load_flags);
253                                 else
254                                         result = file->skip_tag();
255                         }
256                         else
257                         if( file->tag.title_is(labels->xml_tag) ) {
258                                 if( load_flags & LOAD_TIMEBAR )
259                                         labels->load(file, load_flags);
260                                 else
261                                         result = file->skip_tag();
262                         }
263                         else
264                         if( file->tag.title_is("LOCALSESSION") ) {
265                                 if( (load_flags & LOAD_SESSION) ||
266                                         (load_flags & LOAD_TIMEBAR) )
267                                         local_session->load_xml(file, load_flags);
268                                 else
269                                         result = file->skip_tag();
270                         }
271                         else
272                         if( file->tag.title_is("SESSION") ) {
273                                 if( (load_flags & LOAD_SESSION) &&
274                                         !parent_edl )
275                                         session->load_xml(file, 0, load_flags);
276                                 else
277                                         result = file->skip_tag();
278                         }
279                         else
280                         if( file->tag.title_is("TRACK") ) {
281                                 tracks->load(file, track_offset, load_flags);
282                         }
283                         else
284 // Sub EDL.
285 // Causes clip creation to fail because that involves an opening EDL tag.
286                         if( file->tag.title_is("CLIP_EDL") && !parent_edl ) {
287                                 EDL *new_edl = new EDL(this);
288                                 new_edl->create_objects();
289                                 new_edl->read_xml(file, LOAD_ALL);
290                                 if( (load_flags & LOAD_ALL) == LOAD_ALL )
291                                         clips.add_clip(new_edl);
292                                 new_edl->remove_user();
293                         }
294                         else
295                         if( file->tag.title_is("NESTED_EDL") ) {
296                                 EDL *nested_edl = new EDL;
297                                 nested_edl->create_objects();
298                                 nested_edl->read_xml(file, LOAD_ALL);
299                                 if( (load_flags & LOAD_ALL) == LOAD_ALL )
300                                         nested_edls.get_nested(nested_edl);
301                                 nested_edl->remove_user();
302                         }
303                         else
304                         if( file->tag.title_is("VWINDOW_EDL") && !parent_edl ) {
305                                 EDL *new_edl = new EDL(this);
306                                 new_edl->create_objects();
307                                 new_edl->read_xml(file, LOAD_ALL);
308
309
310                                 if( (load_flags & LOAD_ALL) == LOAD_ALL ) {
311 //                                              if( vwindow_edl && !vwindow_edl_shared )
312 //                                                      vwindow_edl->remove_user();
313 //                                              vwindow_edl_shared = 0;
314 //                                              vwindow_edl = new_edl;
315
316                                         append_vwindow_edl(new_edl, 0);
317
318                                 }
319                                 else
320 // Discard if not replacing EDL
321                                 {
322                                         new_edl->remove_user();
323                                         new_edl = 0;
324                                 }
325                         }
326                 }
327         } while(!result);
328
329         boundaries();
330 //dump();
331
332         return 0;
333 }
334
335 // Output path is the path of the output file if name truncation is desired.
336 // It is a "" if complete names should be used.
337 // Called recursively by copy for clips, thus the string can't be terminated.
338 // The string is not terminated in this call.
339 int EDL::save_xml(FileXML *file, const char *output_path)
340 {
341         copy(COPY_EDL, file, output_path, 0);
342         return 0;
343 }
344
345 int EDL::copy_all(EDL *edl)
346 {
347         if( this == edl ) return 0;
348         folder_no = edl->folder_no;
349         update_index(edl);
350         copy_session(edl);
351         copy_assets(edl);
352         copy_clips(edl);
353         copy_nested(edl);
354         copy_mixers(edl);
355         tracks->copy_from(edl->tracks);
356         labels->copy_from(edl->labels);
357         return 0;
358 }
359
360 void EDL::copy_clips(EDL *edl)
361 {
362         if( this == edl ) return;
363
364         remove_vwindow_edls();
365
366 //      if( vwindow_edl && !vwindow_edl_shared )
367 //              vwindow_edl->remove_user();
368 //      vwindow_edl = 0;
369 //      vwindow_edl_shared = 0;
370
371         for( int i=0; i<edl->total_vwindow_edls(); ++i ) {
372                 EDL *new_edl = new EDL(this);
373                 new_edl->create_objects();
374                 new_edl->copy_all(edl->get_vwindow_edl(i));
375                 append_vwindow_edl(new_edl, 0);
376         }
377
378         clips.clear();
379         for( int i=0; i<edl->clips.size(); ++i ) add_clip(edl->clips[i]);
380 }
381
382 void EDL::copy_nested(EDL *edl)
383 {
384         if( this == edl ) return;
385         nested_edls.copy_nested(edl->nested_edls);
386 }
387
388 void EDL::copy_assets(EDL *edl)
389 {
390         if( this == edl ) return;
391
392         if( !parent_edl ) {
393                 assets->copy_from(edl->assets);
394         }
395 }
396
397 void EDL::copy_mixers(EDL *edl)
398 {
399         if( this == edl ) return;
400         mixers.copy_from(edl->mixers);
401 }
402
403 void EDL::copy_session(EDL *edl, int session_only)
404 {
405         if( this == edl ) return;
406
407         if( !session_only ) {
408                 strcpy(this->path, edl->path);
409                 folders.copy_from(&edl->folders);
410         }
411
412         if( !parent_edl ) {
413                 session->copy(edl->session);
414         }
415
416         if( session_only <= 0 ) {
417                 local_session->copy_from(edl->local_session);
418         }
419 }
420
421 int EDL::copy_assets(int copy_flags, double start, double end,
422                 FileXML *file, const char *output_path)
423 {
424         ArrayList<Asset*> asset_list;
425         Track* current;
426
427         file->tag.set_title("ASSETS");
428         file->append_tag();
429         file->append_newline();
430
431 // Copy everything for a save
432         if( (copy_flags & COPY_ALL_ASSETS) ) {
433                 for( Asset *asset=assets->first; asset; asset=asset->next ) {
434                         asset_list.append(asset);
435                 }
436         }
437         if( (copy_flags & COPY_USED_ASSETS) ) {
438 // Copy just the ones being used.
439                 for( current = tracks->first; current; current = NEXT ) {
440                         if( !current->record ) continue;
441                         current->copy_assets(start, end, &asset_list);
442                 }
443         }
444
445 // Paths relativised here
446         for( int i=0; i<asset_list.size(); ++i ) {
447                 asset_list[i]->write(file, 0, output_path);
448         }
449
450         file->tag.set_title("/ASSETS");
451         file->append_tag();
452         file->append_newline();
453         file->append_newline();
454         return 0;
455 }
456
457
458 int EDL::copy(int copy_flags, double start, double end,
459         FileXML *file, const char *output_path, int rewind_it)
460 {
461         file->tag.set_title("EDL");
462         file->tag.set_property("VERSION", CINELERRA_VERSION);
463 // Save path for restoration of the project title from a backup.
464         if( this->path[0] ) file->tag.set_property("PATH", path);
465         return copy_xml(copy_flags, start, end, file, "/EDL", output_path, rewind_it);
466 }
467 int EDL::copy(int copy_flags, FileXML *file, const char *output_path, int rewind_it)
468 {
469         return copy(copy_flags, 0., tracks->total_length(),
470                 file, output_path, rewind_it);
471 }
472
473 int EDL::copy_clip(int copy_flags, double start, double end,
474         FileXML *file, const char *output_path, int rewind_it)
475 {
476         file->tag.set_title("CLIP_EDL");
477         return copy_xml(copy_flags, start, end, file, "/CLIP_EDL", output_path, rewind_it);
478 }
479 int EDL::copy_clip(int copy_flags, FileXML *file, const char *output_path, int rewind_it)
480 {
481         return copy_clip(copy_flags, 0., tracks->total_length(),
482                 file, output_path, rewind_it);
483 }
484
485 int EDL::copy_nested(int copy_flags, double start, double end,
486         FileXML *file, const char *output_path, int rewind_it)
487 {
488         file->tag.set_title("NESTED_EDL");
489         if( this->path[0] ) file->tag.set_property("PATH", path);
490         return copy_xml(copy_flags, start, end, file, "/NESTED_EDL", output_path, rewind_it);
491 }
492 int EDL::copy_nested(int copy_flags, FileXML *file, const char *output_path, int rewind_it)
493 {
494         return copy_nested(copy_flags, 0., tracks->total_length(),
495                 file, output_path, rewind_it);
496 }
497
498 int EDL::copy_vwindow(int copy_flags, double start, double end,
499         FileXML *file, const char *output_path, int rewind_it)
500 {
501         file->tag.set_title("VWINDOW_EDL");
502         return copy_xml(copy_flags, start, end, file, "/VWINDOW_EDL", output_path, rewind_it);
503 }
504 int EDL::copy_vwindow(int copy_flags, FileXML *file, const char *output_path, int rewind_it)
505 {
506         return copy_vwindow(copy_flags, 0., tracks->total_length(),
507                 file, output_path, rewind_it);
508 }
509
510 int EDL::copy_xml(int copy_flags, double start, double end,
511          FileXML *file, const char *closer, const char *output_path,
512         int rewind_it)
513 {
514         file->append_tag();
515         file->append_newline();
516 // Set clipboard samples only if copying to clipboard
517         if( (copy_flags & COPY_LENGTH) ) {
518                 file->tag.set_title("CLIPBOARD");
519                 file->tag.set_property("LENGTH", end - start);
520                 file->append_tag();
521                 file->tag.set_title("/CLIPBOARD");
522                 file->append_tag();
523                 file->append_newline();
524                 file->append_newline();
525         }
526 //printf("EDL::copy 1\n");
527
528 // Sessions
529         if( (copy_flags & COPY_LOCAL_SESSION) )
530                 local_session->save_xml(file, start);
531
532 // Top level stuff.
533 // Need to copy all this from child EDL if pasting is desired.
534
535         if( (copy_flags & COPY_SESSION) )
536                 session->save_xml(file);
537
538         if( (copy_flags & COPY_VIDEO_CONFIG) )
539                 session->save_video_config(file);
540
541         if( (copy_flags & COPY_AUDIO_CONFIG) )
542                 session->save_audio_config(file);
543
544         if( (copy_flags & COPY_FOLDERS) )
545                 folders.save_xml(file);
546
547         if( (copy_flags & (COPY_ALL_ASSETS | COPY_USED_ASSETS)) )
548                 copy_assets(copy_flags, start, end, file, output_path);
549
550         if( (copy_flags & COPY_NESTED_EDL) ) {
551                 for( int i=0; i<nested_edls.size(); ++i )
552                         nested_edls[i]->copy_nested(copy_flags,
553                                 file, output_path, 0);
554         }
555 // Clips
556         if( (copy_flags & COPY_CLIPS) ) {
557                 for( int i=0; i<clips.size(); ++i )
558                         clips[i]->copy_clip(copy_flags, file, output_path, 0);
559         }
560
561         if( (copy_flags & COPY_VWINDOWS) ) {
562                 for( int i=0; i<total_vwindow_edls(); ++i )
563                         get_vwindow_edl(i)->copy_vwindow(copy_flags,
564                                 file, output_path, 0);
565         }
566
567         if( (copy_flags & COPY_MIXERS) )
568                 mixers.save(file);
569
570         file->append_newline();
571         file->append_newline();
572
573         if( (copy_flags & COPY_LABELS) )
574                 labels->copy(start, end, file);
575
576         tracks->copy(copy_flags, start, end, file, output_path);
577
578 // terminate file
579         file->tag.set_title(closer);
580         file->append_tag();
581         file->append_newline();
582
583 // For editing operations we want to rewind it for immediate pasting.
584 // For clips and saving to disk leave it alone.
585         if( rewind_it ) {
586                 file->terminate_string();
587                 file->rewind();
588         }
589         return 0;
590 }
591
592 void EDL::copy_indexables(EDL *edl)
593 {
594         for( Track *track=edl->tracks->first; track; track=track->next ) {
595                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
596                         if( edit->asset )
597                                 assets->update(edit->asset);
598                         if( edit->nested_edl )
599                                 nested_edls.get_nested(edit->nested_edl);
600                 }
601         }
602 }
603
604 EDL *EDL::new_nested(EDL *edl, const char *path)
605 {
606         EDL *nested = new EDL;  // no parent for nested edl
607         nested->create_objects();
608         nested->copy_session(edl);
609         nested->set_path(path);
610         nested->update_index(edl);
611         nested->copy_indexables(edl);
612         nested->tracks->copy_from(edl->tracks);
613         nested_edls.append(nested);
614         return nested;
615 }
616
617 EDL *EDL::create_nested_clip(EDL *nested)
618 {
619         EDL *new_edl = new EDL(this);  // parent for clip edl
620         new_edl->create_objects();
621         new_edl->create_nested(nested);
622         return new_edl;
623 }
624
625 void EDL::create_nested(EDL *nested)
626 {
627 // Keep frame rate, sample rate, and output size unchanged.
628 // Nest all video & audio outputs
629         session->video_tracks = 1;
630         session->audio_tracks = nested->session->audio_channels;
631         create_default_tracks();
632         insert_asset(0, nested, 0, 0, 0);
633 }
634
635 void EDL::retrack()
636 {
637         int min_w = session->output_w, min_h = session->output_h;
638         for( Track *track=tracks->first; track!=0; track=track->next ) {
639                 if( track->data_type != TRACK_VIDEO ) continue;
640                 int w = min_w, h = min_h;
641                 for( Edit *current=track->edits->first; current!=0; current=NEXT ) {
642                         Indexable* indexable = current->get_source();
643                         if( !indexable ) continue;
644                         int edit_w = indexable->get_w(), edit_h = indexable->get_h();
645                         if( w < edit_w ) w = edit_w;
646                         if( h < edit_h ) h = edit_h;
647                 }
648                 if( track->track_w == w && track->track_h == h ) continue;
649                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->
650                         translate_masks( (w - track->track_w) / 2, (h - track->track_h) / 2);
651                 track->track_w = w;  track->track_h = h;
652         }
653 }
654
655 void EDL::rechannel()
656 {
657         for( Track *current=tracks->first; current; current=NEXT ) {
658                 if( current->data_type == TRACK_AUDIO ) {
659                         PanAutos *autos = (PanAutos*)current->automation->autos[AUTOMATION_PAN];
660                         ((PanAuto*)autos->default_auto)->rechannel();
661                         for( PanAuto *keyframe = (PanAuto*)autos->first;
662                              keyframe; keyframe = (PanAuto*)keyframe->next ) {
663                                 keyframe->rechannel();
664                         }
665                 }
666         }
667 }
668
669 void EDL::resample(double old_rate, double new_rate, int data_type)
670 {
671         for( Track *current=tracks->first; current; current=NEXT ) {
672                 if( current->data_type == data_type ) {
673                         current->resample(old_rate, new_rate);
674                 }
675         }
676 }
677
678
679 void EDL::synchronize_params(EDL *edl)
680 {
681         local_session->synchronize_params(edl->local_session);
682         for( Track *this_track=tracks->first, *that_track=edl->tracks->first;
683              this_track && that_track;
684              this_track=this_track->next, that_track=that_track->next ) {
685                 this_track->synchronize_params(that_track);
686         }
687 }
688
689 int EDL::trim_selection(double start,
690         double end,
691         int edit_labels,
692         int edit_plugins,
693         int edit_autos)
694 {
695         if( start != end ) {
696 // clear the data
697                 clear(0,
698                         start,
699                         edit_labels,
700                         edit_plugins,
701                         edit_autos);
702                 clear(end - start,
703                         tracks->total_length(),
704                         edit_labels,
705                         edit_plugins,
706                         edit_autos);
707         }
708         return 0;
709 }
710
711
712 int EDL::equivalent(double position1, double position2)
713 {
714         double threshold = session->cursor_on_frames ?
715                 0.5 / session->frame_rate : 1.0 / session->sample_rate;
716         return fabs(position2 - position1) < threshold ? 1 : 0;
717 }
718
719 double EDL::equivalent_output(EDL *edl)
720 {
721         double result = -1;
722         session->equivalent_output(edl->session, &result);
723         tracks->equivalent_output(edl->tracks, &result);
724         return result;
725 }
726
727
728 void EDL::set_path(const char *path)
729 {
730         if( &this->path[0] == path ) return;
731         strcpy(this->path, path);
732 }
733
734 void EDL::set_inpoint(double position)
735 {
736         if( equivalent(local_session->get_inpoint(), position) &&
737                 local_session->get_inpoint() >= 0 ) {
738                 local_session->unset_inpoint();
739         }
740         else {
741                 local_session->set_inpoint(align_to_frame(position, 0));
742                 if( local_session->get_outpoint() <= local_session->get_inpoint() )
743                         local_session->unset_outpoint();
744         }
745 }
746
747 void EDL::set_outpoint(double position)
748 {
749         if( equivalent(local_session->get_outpoint(), position) &&
750                 local_session->get_outpoint() >= 0 ) {
751                 local_session->unset_outpoint();
752         }
753         else {
754                 local_session->set_outpoint(align_to_frame(position, 0));
755                 if( local_session->get_inpoint() >= local_session->get_outpoint() )
756                         local_session->unset_inpoint();
757         }
758 }
759
760 void EDL::unset_inoutpoint()
761 {
762         local_session->unset_inpoint();
763         local_session->unset_outpoint();
764 }
765
766 int EDL::blade(double position)
767 {
768         return tracks->blade(position);
769 }
770
771 int EDL::clear(double start, double end,
772         int clear_labels, int clear_plugins, int edit_autos)
773 {
774         if( start == end ) {
775                 double distance = 0;
776                 tracks->clear_handle(start,
777                         end,
778                         distance,
779                         clear_labels,
780                         clear_plugins,
781                         edit_autos);
782                 if( clear_labels && distance > 0 )
783                         labels->paste_silence(start,
784                                 start + distance);
785         }
786         else {
787                 tracks->clear(start,
788                         end,
789                         clear_plugins,
790                         edit_autos);
791                 if( clear_labels )
792                         labels->clear(start,
793                                 end,
794                                 1);
795         }
796
797 // Need to put at beginning so a subsequent paste operation starts at the
798 // right position.
799         double position = local_session->get_selectionstart();
800         local_session->set_selectionend(position);
801         local_session->set_selectionstart(position);
802         return 0;
803 }
804
805 int EDL::clear_hard_edges(double start, double end)
806 {
807         return tracks->clear_hard_edges(start, end);
808 }
809
810 static int dead_edit_cmp(Edit**ap, Edit**bp)
811 {
812         Edit *a = *ap, *b = *bp;
813         if( a->track != b->track ) return 0;
814         return a->startproject > b->startproject ? -1 : 1;
815 }
816
817 void EDL::delete_edits(ArrayList<Edit*> *edits, int collapse)
818 {
819         edits->sort(dead_edit_cmp);
820         for( int i=0; i<edits->size(); ++i ) {
821                 Edit *edit = edits->get(i);
822                 Track *track = edit->track;
823                 int64_t start = edit->startproject;
824                 int64_t length = edit->length;
825                 int64_t end = start + length;
826                 if( session->autos_follow_edits ) {
827                         track->automation->clear(start, end, 0, collapse);
828                 }
829                 if( session->plugins_follow_edits ) {
830                         for( int k=0; k<track->plugin_set.size(); ++k ) {
831                                 PluginSet *plugin_set = track->plugin_set[k];
832                                 plugin_set->clear(start, end, 1);
833                                 if( !collapse )
834                                         plugin_set->paste_silence(start, end, 1);
835                                 plugin_set->optimize();
836                         }
837                 }
838                 Edit *dead_edit = edit;
839                 if( collapse ) {
840                         while( (edit=edit->next) )
841                                 edit->startproject -= length;
842                 }
843                 delete dead_edit;
844         }
845         optimize();
846 }
847
848 class Range {
849 public:
850         static int cmp(Range *ap, Range *bp);
851         double start, end;
852         bool operator ==(Range &that) { return this->start == that.start; }
853         bool operator >(Range &that) { return this->start > that.start; }
854 };
855 int Range::cmp(Range *ap, Range *bp) {
856         return ap->start < bp->start ? -1 : ap->start == bp->start ? 0 : 1;
857 }
858
859 static void get_edit_regions(ArrayList<Edit*> *edits, ArrayList<Range> &regions)
860 {
861 // move edit inclusive labels by regions
862         for( int i=0; i<edits->size(); ++i ) {
863                 Edit *edit = edits->get(i);
864                 double pos = edit->track->from_units(edit->startproject);
865                 double end = edit->track->from_units(edit->startproject + edit->length);
866                 int n = regions.size(), k = n;
867                 while( --k >= 0 ) {
868                         Range &range = regions[k];
869                         if( pos >= range.end ) continue;
870                         if( range.start >= end ) continue;
871                         int expand = 0;
872                         if( range.start > pos ) { range.start = pos;  expand = 1; }
873                         if( range.end < end ) { range.end = end;  expand = 1; }
874                         if( !expand ) break;
875                         k = n;
876                 }
877                 if( k < 0 ) {
878                         Range &range = regions.append();
879                         range.start = pos;  range.end = end;
880                 }
881         }
882         regions.sort(Range::cmp);
883 }
884
885 void EDL::delete_edit_labels(ArrayList<Edit*> *edits, int collapse)
886 {
887         ArrayList<Range> regions;
888         get_edit_regions(edits, regions);
889         int n = regions.size(), k = n;
890         while( --k >= 0 ) {
891                 Range &range = regions[k];
892                 labels->clear(range.start, range.end, collapse);
893         }
894 }
895
896 void EDL::move_edit_labels(ArrayList<Edit*> *edits, double dist)
897 {
898         ArrayList<Range> regions;
899         get_edit_regions(edits, regions);
900         int n = regions.size(), k = n;
901         Labels moved(this, 0);
902         while( --k >= 0 ) {
903                 Range &range = regions[k];
904                 Label *label = labels->label_of(range.start);
905                 for( Label *next=0; label && label->position <= range.end; label=next ) {
906                         next = label->next;
907                         labels->remove_pointer(label);
908                         label->position += dist;
909                         moved.append(label);
910                 }
911                 Label *current = labels->first;
912                 while( (label=moved.first) ) {
913                         moved.remove_pointer(label);
914                         while( current && current->position < label->position )
915                                 current = current->next;
916                         if( current && current->position == label->position ) {
917                                 delete label;  continue;
918                         }
919                         labels->insert_before(current, label);
920                 }
921         }
922 }
923
924 void EDL::modify_edithandles(double oldposition, double newposition,
925         int currentend, int handle_mode, int edit_labels,
926         int edit_plugins, int edit_autos, int group_id)
927 {
928         tracks->modify_edithandles(oldposition, newposition,
929                 currentend, handle_mode, edit_labels,
930                 edit_plugins, edit_autos, group_id);
931 }
932
933 void EDL::modify_pluginhandles(double oldposition, double newposition,
934         int currentend, int handle_mode, int edit_labels,
935         int edit_autos, Edits *trim_edits)
936 {
937         tracks->modify_pluginhandles(oldposition, newposition,
938                 currentend, handle_mode, edit_labels,
939                 edit_autos, trim_edits);
940         optimize();
941 }
942
943 void EDL::paste_silence(double start, double end,
944         int edit_labels, int edit_plugins, int edit_autos)
945 {
946         if( edit_labels )
947                 labels->paste_silence(start, end);
948         tracks->paste_silence(start, end, edit_plugins, edit_autos);
949 }
950
951
952 void EDL::remove_from_project(ArrayList<EDL*> *clips)
953 {
954         for( int i=0; i<clips->size(); ++i ) {
955                 this->clips.remove_clip(clips->get(i));
956         }
957 }
958
959 void EDL::remove_from_project(ArrayList<Indexable*> *assets)
960 {
961 // Remove from clips
962         if( !parent_edl )
963                 for( int j=0; j<clips.size(); ++j ) {
964                         clips[j]->remove_from_project(assets);
965                 }
966
967 // Remove from VWindow EDLs
968         for( int i=0; i<total_vwindow_edls(); ++i )
969                 get_vwindow_edl(i)->remove_from_project(assets);
970
971         for( int i=0; i<assets->size(); ++i ) {
972 // Remove from tracks
973                 for( Track *track=tracks->first; track; track=track->next ) {
974                         track->remove_asset(assets->get(i));
975                 }
976
977 // Remove from assets
978                 if( !parent_edl && assets->get(i)->is_asset ) {
979                         this->assets->remove_asset((Asset*)assets->get(i));
980                 }
981                 else
982                 if( !parent_edl && !assets->get(i)->is_asset ) {
983                         this->nested_edls.remove_clip((EDL*)assets->get(i));
984                 }
985         }
986 }
987
988 void EDL::update_assets(EDL *src)
989 {
990         for( Asset *current=src->assets->first; current; current=NEXT ) {
991                 assets->update(current);
992         }
993 }
994
995 int EDL::get_tracks_height(Theme *theme)
996 {
997         int total_pixels = 0;
998         for( Track *current=tracks->first; current; current=NEXT ) {
999                 total_pixels += current->vertical_span(theme);
1000         }
1001         return total_pixels;
1002 }
1003
1004 int64_t EDL::get_tracks_width()
1005 {
1006         int64_t total_pixels = 0;
1007         for( Track *current=tracks->first; current; current=NEXT ) {
1008                 int64_t pixels = current->horizontal_span();
1009                 if( pixels > total_pixels ) total_pixels = pixels;
1010         }
1011 //printf("EDL::get_tracks_width %d\n", total_pixels);
1012         return total_pixels;
1013 }
1014
1015 // int EDL::calculate_output_w(int single_channel)
1016 // {
1017 //      if( single_channel ) return session->output_w;
1018 //
1019 //      int widest = 0;
1020 //      for( int i=0; i<session->video_channels; ++i )
1021 //      {
1022 //              if( session->vchannel_x[i] + session->output_w > widest ) widest = session->vchannel_x[i] + session->output_w;
1023 //      }
1024 //      return widest;
1025 // }
1026 //
1027 // int EDL::calculate_output_h(int single_channel)
1028 // {
1029 //      if( single_channel ) return session->output_h;
1030 //
1031 //      int tallest = 0;
1032 //      for( int i=0; i<session->video_channels; ++i )
1033 //      {
1034 //              if( session->vchannel_y[i] + session->output_h > tallest ) tallest = session->vchannel_y[i] + session->output_h;
1035 //      }
1036 //      return tallest;
1037 // }
1038
1039 // Get the total output size scaled to aspect ratio
1040 void EDL::calculate_conformed_dimensions(int single_channel, float &w, float &h)
1041 {
1042         if( (float)session->output_w / session->output_h > get_aspect_ratio() )
1043                 h = (w = session->output_w) / get_aspect_ratio();
1044         else
1045                 w = (h = session->output_h) * get_aspect_ratio();
1046 }
1047
1048 float EDL::get_aspect_ratio()
1049 {
1050         return session->aspect_w / session->aspect_h;
1051 }
1052
1053 int EDL::dump(FILE *fp)
1054 {
1055         if( parent_edl )
1056                 fprintf(fp,"CLIP\n");
1057         else
1058                 fprintf(fp,"EDL\n");
1059         fprintf(fp,"  clip_title: %s\n"
1060                 "  parent_edl: %p\n", local_session->clip_title, parent_edl);
1061         fprintf(fp,"  selectionstart %f\n  selectionend %f\n  loop_start %f\n  loop_end %f\n",
1062                 local_session->get_selectionstart(1),
1063                 local_session->get_selectionend(1),
1064                 local_session->loop_start,
1065                 local_session->loop_end);
1066         for( int i=0; i<TOTAL_PANES; ++i ) {
1067                 fprintf(fp,"  pane %d view_start=%jd track_start=%d\n", i,
1068                         local_session->view_start[i],
1069                         local_session->track_start[i]);
1070         }
1071
1072         if( !parent_edl ) {
1073                 fprintf(fp,"audio_channels: %d audio_tracks: %d sample_rate: %jd\n",
1074                         session->audio_channels,
1075                         session->audio_tracks,
1076                         session->sample_rate);
1077                 fprintf(fp,"  video_channels: %d\n"
1078                         "  video_tracks: %d\n"
1079                         "  frame_rate: %.2f\n"
1080                         "  frames_per_foot: %.2f\n"
1081                         "  output_w: %d\n"
1082                         "  output_h: %d\n"
1083                         "  aspect_w: %f\n"
1084                         "  aspect_h: %f\n"
1085                         "  color_model: %d\n",
1086                                 session->video_channels,
1087                                 session->video_tracks,
1088                                 session->frame_rate,
1089                                 session->frames_per_foot,
1090                                 session->output_w,
1091                                 session->output_h,
1092                                 session->aspect_w,
1093                                 session->aspect_h,
1094                                 session->color_model);
1095
1096                 fprintf(fp," CLIPS");
1097                 fprintf(fp,"  total: %d\n", clips.size());
1098                 for( int i=0; i<clips.size(); ++i ) {
1099                         fprintf(fp,"\n\n");
1100                         clips[i]->dump(fp);
1101                         fprintf(fp,"\n\n");
1102                 }
1103                 fprintf(fp," NESTED_EDLS");
1104                 fprintf(fp,"  total: %d\n", nested_edls.size());
1105                 for( int i=0; i<nested_edls.size(); ++i )
1106                         fprintf(fp,"   %s\n", nested_edls[i]->path);
1107
1108                 fprintf(fp," VWINDOW EDLS");
1109                 fprintf(fp,"  total: %d\n", total_vwindow_edls());
1110
1111                 for( int i=0; i<total_vwindow_edls(); ++i ) {
1112                         fprintf(fp,"   %s\n", get_vwindow_edl(i)->local_session->clip_title);
1113                 }
1114
1115                 fprintf(fp," ASSETS\n");
1116                 assets->dump(fp);
1117         }
1118         fprintf(fp," LABELS\n");
1119         labels->dump(fp);
1120         fprintf(fp," TRACKS\n");
1121         tracks->dump(fp);
1122 //printf("EDL::dump 2\n");
1123         return 0;
1124 }
1125
1126 EDL* EDL::add_clip(EDL *edl)
1127 {
1128 // Copy argument.  New edls are deleted from MWindow::load_filenames.
1129         EDL *new_edl = new EDL(this);
1130         new_edl->create_objects();
1131         new_edl->copy_all(edl);
1132         new_edl->folder_no = AW_CLIP_FOLDER;
1133         clips.append(new_edl);
1134         return new_edl;
1135 }
1136
1137 void EDL::insert_asset(Asset *asset,
1138         EDL *nested_edl,
1139         double position,
1140         Track *first_track,
1141         RecordLabels *labels)
1142 {
1143 // Insert asset into asset table
1144         Asset *new_asset = 0;
1145         EDL *new_nested_edl = 0;
1146
1147         if( asset ) new_asset = assets->update(asset);
1148         if( nested_edl ) new_nested_edl = nested_edls.get_nested(nested_edl);
1149
1150 // Paste video
1151         int vtrack = 0;
1152         Track *current = first_track ? first_track : tracks->first;
1153
1154
1155 // Fix length of single frame
1156         double length = 0.;
1157         int layers = 0;
1158         int channels = 0;
1159
1160         if( new_nested_edl ) {
1161                 length = new_nested_edl->tracks->total_length();
1162                 layers = 1;
1163                 channels = new_nested_edl->session->audio_channels;
1164         }
1165
1166         if( new_asset ) {
1167 // Insert 1 frame for undefined length
1168                 if( new_asset->video_length < 0 ) {
1169                         length = session->si_useduration ?
1170                                 session->si_duration :
1171                                 1.0 / session->frame_rate;
1172                 }
1173                 else {
1174                         length = new_asset->frame_rate > 0 ?
1175                                 (double)new_asset->video_length / new_asset->frame_rate :
1176                                 1.0 / session->frame_rate;
1177                 }
1178                 layers = new_asset->layers;
1179                 channels = new_asset->channels;
1180         }
1181
1182         for( ; current && vtrack<layers; current=NEXT ) {
1183                 if( !current->record || current->data_type != TRACK_VIDEO ) continue;
1184                 current->insert_asset(new_asset, new_nested_edl,
1185                         length, position, vtrack++);
1186         }
1187
1188         int atrack = 0;
1189         if( new_asset ) {
1190                 if( new_asset->audio_length < 0 ) {
1191 // Insert 1 frame for undefined length & video
1192                         if( new_asset->video_data )
1193                                 length = (double)1.0 / new_asset->frame_rate;
1194                         else
1195 // Insert 1 second for undefined length & no video
1196                                 length = 1.0;
1197                 }
1198                 else
1199                         length = (double)new_asset->audio_length /
1200                                         new_asset->sample_rate;
1201         }
1202
1203         current = tracks->first;
1204         for( ; current && atrack < channels; current=NEXT ) {
1205                 if( !current->record || current->data_type != TRACK_AUDIO ) continue;
1206                 current->insert_asset(new_asset, new_nested_edl,
1207                         length, position, atrack++);
1208         }
1209
1210 // Insert labels from a recording window.
1211         if( labels ) {
1212                 for( RecordLabel *label=labels->first; label; label=label->next ) {
1213                         this->labels->toggle_label(label->position, label->position);
1214                 }
1215         }
1216 }
1217
1218
1219
1220 void EDL::set_index_file(Indexable *indexable)
1221 {
1222         if( indexable->is_asset )
1223                 assets->update_index((Asset*)indexable);
1224         else
1225                 nested_edls.update_index((EDL*)indexable);
1226 }
1227
1228 void EDL::optimize()
1229 {
1230 //printf("EDL::optimize 1\n");
1231         double length = tracks->total_length();
1232         double preview_start = local_session->preview_start;
1233         double preview_end = local_session->preview_end;
1234         if( preview_end < 0 || preview_end > length )
1235                 preview_end = length;
1236         if( preview_start == 0 && preview_end >= length )
1237                 local_session->preview_end = -1;
1238         if( preview_start > preview_end )
1239                 local_session->preview_start = preview_end;
1240         for( Track *current=tracks->first; current; current=NEXT )
1241                 current->optimize();
1242 }
1243
1244 int EDL::next_id()
1245 {
1246         static Mutex id_lock;
1247         id_lock.lock("EDL::next_id");
1248         int result = EDLSession::current_id++;
1249         id_lock.unlock();
1250         return result;
1251 }
1252
1253 void EDL::get_shared_plugins(Track *source,
1254         ArrayList<SharedLocation*> *plugin_locations,
1255         int omit_recordable,
1256         int data_type)
1257 {
1258         for( Track *track=tracks->first; track; track=track->next ) {
1259                 if( track->record && omit_recordable ) continue;
1260                 if( track == source || track->data_type != data_type ) continue;
1261                 for( int i=0; i<track->plugin_set.size(); ++i ) {
1262                         Plugin *plugin = track->get_current_plugin(
1263                                 local_session->get_selectionstart(1),
1264                                 i, PLAY_FORWARD, 1, 0);
1265                         if( plugin && plugin->plugin_type != PLUGIN_STANDALONE ) continue;
1266                         plugin_locations->append(new SharedLocation(tracks->number_of(track), i));
1267                 }
1268         }
1269 }
1270
1271 void EDL::get_shared_tracks(Track *track,
1272         ArrayList<SharedLocation*> *module_locations,
1273         int omit_recordable, int data_type)
1274 {
1275         for( Track *current=tracks->first; current; current=NEXT ) {
1276                 if( omit_recordable && current->record ) continue;
1277                 if( current == track || current->data_type != data_type ) continue;
1278                 module_locations->append(new SharedLocation(tracks->number_of(current), 0));
1279         }
1280 }
1281
1282 // aligned frame time, account for sample truncation
1283 double EDL::frame_align(double position, int round)
1284 {
1285         if( !round && session->sample_rate > 0 ) {
1286                 int64_t sample_pos = position * session->sample_rate;
1287                 position = (sample_pos+2.) / session->sample_rate;
1288         }
1289         int64_t frame_pos = (position * session->frame_rate + (round ? 0.5 : 1e-6));
1290         return frame_pos / session->frame_rate;
1291 }
1292
1293 // Convert position to frames if alignment is enabled.
1294 double EDL::align_to_frame(double position, int round)
1295 {
1296         if( session->cursor_on_frames )
1297                 position = frame_align(position, round);
1298         return position;
1299 }
1300
1301
1302 BinFolder *EDL::get_folder(int no)
1303 {
1304         for( int i=0; i<folders.size(); ++i ) {
1305                 BinFolder *fp = folders[i];
1306                 if( no == fp->awindow_folder ) return fp;
1307         }
1308         return 0;
1309 }
1310
1311 int EDL::get_folder_number(const char *title)
1312 {
1313         for( int i=0; i<AWINDOW_FOLDERS; ++i ) {
1314                 if( !strcmp(title, AWindowGUI::folder_names[i]) )
1315                         return i;
1316         }
1317         for( int i=0; i<folders.size(); ++i ) {
1318                 if( !strcmp(title, folders[i]->title) )
1319                         return folders[i]->awindow_folder;
1320         }
1321         return AW_NO_FOLDER;
1322 }
1323
1324 const char *EDL::get_folder_name(int no)
1325 {
1326         if( no >= 0 && no<AWINDOW_FOLDERS )
1327                 return AWindowGUI::folder_names[no];
1328         BinFolder *fp = get_folder(no);
1329         return !fp ? "" : fp->title;
1330 }
1331
1332 int EDL::new_folder(const char *title, int is_clips)
1333 {
1334         if( !title[0] ) return 1;
1335         int ret = get_folder_number(title);
1336         if( ret >= 0 ) return 1;
1337         int idx = AWINDOW_FOLDERS;
1338         for( int i=0; i<folders.size(); ++i ) {
1339                 BinFolder *fp = folders[i];
1340                 int no = fp->awindow_folder;
1341                 if( no >= idx ) idx = no+1;
1342         }
1343         folders.append(new BinFolder(idx, is_clips, title));
1344         return 0;
1345 }
1346
1347 int EDL::delete_folder(const char *title)
1348 {
1349         int k = folders.size();
1350         while( --k >= 0 && strcmp(title, folders[k]->title) );
1351         if( k >= 0 )
1352                 folders.remove_object_number(k);
1353         return k;
1354 }
1355
1356 int EDL::get_use_vconsole(VEdit* *playable_edit,
1357         int64_t position, int direction, PlayableTracks *playable_tracks)
1358 {
1359         int share_playable_tracks = 1;
1360         int result = 0;
1361         VTrack *playable_track = 0;
1362         const int debug = 0;
1363         *playable_edit = 0;
1364
1365 // Calculate playable tracks when being called as a nested EDL
1366         if( !playable_tracks ) {
1367                 share_playable_tracks = 0;
1368                 playable_tracks = new PlayableTracks(this,
1369                         position, direction, TRACK_VIDEO, 1);
1370         }
1371
1372
1373 // Total number of playable tracks is 1
1374         if( playable_tracks->size() != 1 ) {
1375                 result = 1;
1376         }
1377         else {
1378                 playable_track = (VTrack*)playable_tracks->get(0);
1379         }
1380
1381 // Don't need playable tracks anymore
1382         if( !share_playable_tracks ) {
1383                 delete playable_tracks;
1384         }
1385
1386 if( debug ) printf("EDL::get_use_vconsole %d playable_tracks->size()=%d\n",
1387  __LINE__, playable_tracks->size());
1388         if( result ) return 1;
1389
1390
1391 // Test mutual conditions between direct copy rendering and this.
1392         if( !playable_track->direct_copy_possible(position,
1393                 direction,
1394                 1) )
1395                 return 1;
1396 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1397
1398         *playable_edit = (VEdit*)playable_track->edits->editof(position,
1399                 direction, 0);
1400 // No edit at current location
1401         if( !*playable_edit ) return 1;
1402 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1403
1404
1405 // Edit is nested EDL
1406         if( (*playable_edit)->nested_edl ) {
1407 // Test nested EDL
1408                 EDL *nested_edl = (*playable_edit)->nested_edl;
1409                 int64_t nested_position = (int64_t)((position -
1410                                 (*playable_edit)->startproject +
1411                                 (*playable_edit)->startsource) *
1412                         nested_edl->session->frame_rate /
1413                         session->frame_rate);
1414
1415
1416                 VEdit *playable_edit_temp = 0;
1417                 if( session->output_w != nested_edl->session->output_w ||
1418                         session->output_h != nested_edl->session->output_h ||
1419                         nested_edl->get_use_vconsole(&playable_edit_temp,
1420                                 nested_position,
1421                                 direction,
1422                                 0) )
1423                         return 1;
1424
1425                 return 0;
1426         }
1427
1428 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1429 // Edit is not a nested EDL
1430         Asset *asset = (*playable_edit)->asset;
1431 // Edit is silence
1432         if( !asset ) return 1;
1433 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1434
1435 // Asset and output device must have the same dimensions
1436         if( asset->width != session->output_w ||
1437             asset->height != session->output_h )
1438                 return 1;
1439
1440
1441 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1442 // Asset and output device must have same resulting de-interlacing method
1443         if( ilaceautofixmethod2(session->interlace_mode,
1444             asset->interlace_autofixoption, asset->interlace_mode,
1445             asset->interlace_fixmethod) != ILACE_FIXMETHOD_NONE )
1446                 return 1;
1447
1448 // If we get here the frame is going to be directly copied.  Whether it is
1449 // decompressed in hardware depends on the colormodel.
1450         return 0;
1451 }
1452
1453
1454 // For Indexable
1455 int EDL::get_audio_channels()
1456 {
1457         return session->audio_channels;
1458 }
1459
1460 int EDL::get_sample_rate()
1461 {
1462         return session->sample_rate;
1463 }
1464
1465 int64_t EDL::get_audio_samples()
1466 {
1467         return (int64_t)(tracks->total_length() *
1468                 session->sample_rate);
1469 }
1470
1471 int EDL::have_audio()
1472 {
1473         return 1;
1474 }
1475
1476 int EDL::have_video()
1477 {
1478         return 1;
1479 }
1480
1481
1482 int EDL::get_w()
1483 {
1484         return session->output_w;
1485 }
1486
1487 int EDL::get_h()
1488 {
1489         return session->output_h;
1490 }
1491
1492 double EDL::get_frame_rate()
1493 {
1494         return session->frame_rate;
1495 }
1496
1497 int EDL::get_video_layers()
1498 {
1499         return 1;
1500 }
1501
1502 int64_t EDL::get_video_frames()
1503 {
1504         return (int64_t)(tracks->total_length() *
1505                 session->frame_rate);
1506 }
1507
1508 void EDL::remove_vwindow_edls()
1509 {
1510         for( int i=0; i<total_vwindow_edls(); ++i ) {
1511                 get_vwindow_edl(i)->remove_user();
1512         }
1513         vwindow_edls.remove_all();
1514 }
1515
1516 void EDL::remove_vwindow_edl(EDL *edl)
1517 {
1518         if( vwindow_edls.number_of(edl) >= 0 ) {
1519                 edl->remove_user();
1520                 vwindow_edls.remove(edl);
1521         }
1522 }
1523
1524
1525 EDL* EDL::get_vwindow_edl(int number)
1526 {
1527         return vwindow_edls.get(number);
1528 }
1529
1530 int EDL::total_vwindow_edls()
1531 {
1532         return vwindow_edls.size();
1533 }
1534
1535 void EDL::append_vwindow_edl(EDL *edl, int increase_counter)
1536 {
1537         if(vwindow_edls.number_of(edl) >= 0) return;
1538
1539         if(increase_counter) edl->add_user();
1540         vwindow_edls.append(edl);
1541 }
1542
1543
1544 double EDL::next_edit(double position)
1545 {
1546         Units::fix_double(&position);
1547         double new_position = tracks->total_length();
1548
1549         double max_rate = get_frame_rate();
1550         int sample_rate = get_sample_rate();
1551         if( sample_rate > max_rate ) max_rate = sample_rate;
1552         double min_movement = max_rate > 0 ? 1. / max_rate : 1e-6;
1553
1554 // Test for edit handles after position
1555         for( Track *track=tracks->first; track; track=track->next ) {
1556                 if( !track->record ) continue;
1557                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1558                         double edit_end = track->from_units(edit->startproject + edit->length);
1559                         Units::fix_double(&edit_end);
1560                         if( fabs(edit_end-position) < min_movement ) continue;
1561                         if( edit_end > position && edit_end < new_position )
1562                                 new_position = edit_end;
1563                 }
1564         }
1565         return new_position;
1566 }
1567
1568 double EDL::prev_edit(double position)
1569 {
1570         Units::fix_double(&position);
1571         double new_position = -1;
1572
1573         double max_rate = get_frame_rate();
1574         int sample_rate = get_sample_rate();
1575         if( sample_rate > max_rate ) max_rate = sample_rate;
1576         double min_movement = max_rate > 0 ? 1. / max_rate : 1e-6;
1577
1578 // Test for edit handles before cursor position
1579         for( Track *track=tracks->first; track; track=track->next ) {
1580                 if( !track->record ) continue;
1581                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1582                         double edit_end = track->from_units(edit->startproject);
1583                         Units::fix_double(&edit_end);
1584                         if( fabs(edit_end-position) < min_movement ) continue;
1585                         if( edit_end < position && edit_end > new_position )
1586                                 new_position = edit_end;
1587                 }
1588         }
1589         return new_position;
1590 }
1591
1592 double EDL::skip_silence(double position)
1593 {
1594         double result = position, nearest = DBL_MAX;
1595         for( Track *track=tracks->first; track; track=track->next ) {
1596                 if( !track->play ) continue;
1597                 Edit *edit = track->edits->editof(position, PLAY_FORWARD, 0);
1598                 while( edit && edit->silence() ) edit = edit->next;
1599                 if( !edit ) continue;
1600                 double pos = track->from_units(edit->startproject);
1601                 if( pos > position && pos < nearest )
1602                         nearest = result = pos;
1603         }
1604         return result;
1605 }
1606
1607 void EDL::rescale_proxy(int orig_scale, int new_scale)
1608 {
1609         if( orig_scale == new_scale ) return;
1610 // project size
1611         float orig_w = (float)session->output_w * orig_scale;
1612         float orig_h = (float)session->output_h * orig_scale;
1613         if( !parent_edl ) {
1614                 session->output_w = Units::round(orig_w / new_scale);
1615                 session->output_h = Units::round(orig_h / new_scale);
1616         }
1617
1618 // track sizes
1619         for( Track *track=tracks->first; track; track=track->next ) {
1620                 if( track->data_type != TRACK_VIDEO ) continue;
1621                 orig_w = (float)track->track_w * orig_scale;
1622                 orig_h = (float)track->track_h * orig_scale;
1623                 track->track_w = Units::round(orig_w / new_scale);
1624                 track->track_h = Units::round(orig_h / new_scale);
1625                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->
1626                         set_proxy(orig_scale, new_scale);
1627                 ((FloatAutos*)track->automation->autos[AUTOMATION_CAMERA_X])->
1628                         set_proxy(orig_scale, new_scale);
1629                 ((FloatAutos*)track->automation->autos[AUTOMATION_CAMERA_Y])->
1630                         set_proxy(orig_scale, new_scale);
1631                 ((FloatAutos*)track->automation->autos[AUTOMATION_PROJECTOR_X])->
1632                         set_proxy(orig_scale, new_scale);
1633                 ((FloatAutos*)track->automation->autos[AUTOMATION_PROJECTOR_Y])->
1634                         set_proxy(orig_scale, new_scale);
1635         }
1636 }
1637
1638 void EDL::set_proxy(int new_scale, int use_scaler,
1639         ArrayList<Indexable*> *orig_assets, ArrayList<Indexable*> *proxy_assets)
1640 {
1641         int orig_scale = session->proxy_scale;
1642         int orig_use_scaler = session->proxy_use_scaler;
1643
1644 // rescale to full size asset in read_frame
1645         session->proxy_scale = new_scale;
1646         session->proxy_use_scaler = use_scaler;
1647
1648         if( use_scaler ) {
1649                 for( int i=0; i<proxy_assets->size(); ++i ) {
1650                         Asset *proxy_asset = (Asset *)proxy_assets->get(i);
1651                         proxy_asset->width = orig_assets->get(i)->get_w();
1652                         proxy_asset->height = orig_assets->get(i)->get_h();
1653                 }
1654                 new_scale = 1;
1655         }
1656
1657         if( !orig_use_scaler )
1658                 rescale_proxy(orig_scale, new_scale);
1659
1660 // change original assets to proxy assets
1661         int folder_no = use_scaler || new_scale != 1 ? AW_PROXY_FOLDER : AW_MEDIA_FOLDER;
1662         for( int i=0,n=proxy_assets->size(); i<n; ++i ) {
1663                 Indexable *proxy_idxbl = proxy_assets->get(i);
1664                 proxy_idxbl->folder_no = folder_no;
1665                 if( !proxy_idxbl->is_asset ) continue;
1666                 Asset *proxy_asset = assets->update((Asset *)proxy_idxbl);
1667                 if( proxy_asset == (Asset *)proxy_idxbl ) continue;
1668                 proxy_asset->width = proxy_idxbl->get_w();
1669                 proxy_asset->height = proxy_idxbl->get_h();
1670         }
1671 // replace track contents
1672         for( Track *track=tracks->first; track; track=track->next ) {
1673                 if( track->data_type != TRACK_VIDEO ) continue;
1674                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1675                         Indexable *idxbl = (Indexable *)edit->asset;
1676                         if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
1677                         if( !idxbl ) continue;
1678                         int i = orig_assets->size();
1679                         while( --i>=0 && strcmp(orig_assets->get(i)->path, idxbl->path) );
1680                         if( i < 0 ) continue;
1681                         Indexable *proxy_idxbl = proxy_assets->get(i);
1682                         Asset *proxy_asset = proxy_idxbl->is_asset ?
1683                                 assets->update((Asset *)proxy_idxbl) : 0;
1684                         EDL *proxy_edl = !proxy_idxbl->is_asset ?
1685                                 (EDL *)proxy_idxbl : 0;
1686                         edit->asset = proxy_asset;
1687                         edit->nested_edl = proxy_edl;
1688                 }
1689         }
1690         for( int j=0,n=clips.size(); j<n; ++j ) {
1691                 EDL *clip = clips[j];
1692                 int has_proxy = 0;
1693                 for( Track *track=clip->tracks->first; track; track=track->next ) {
1694                         if( track->data_type != TRACK_VIDEO ) continue;
1695                         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1696                                 Indexable *idxbl = (Indexable *)edit->asset;
1697                                 if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
1698                                 if( !idxbl ) continue;
1699                                 int i = orig_assets->size();
1700                                 while( --i>=0 && strcmp(orig_assets->get(i)->path, idxbl->path) );
1701                                 if( i < 0 ) continue;
1702                                 Indexable *proxy_idxbl = proxy_assets->get(i);
1703                                 Asset *proxy_asset = proxy_idxbl->is_asset ?
1704                                         assets->update((Asset *)proxy_idxbl) : 0;
1705                                 EDL *proxy_edl = !proxy_idxbl->is_asset ?
1706                                         (EDL *)proxy_idxbl : 0;
1707                                 edit->asset = proxy_asset;
1708                                 edit->nested_edl = proxy_edl;
1709                                 has_proxy = 1;
1710                         }
1711                 }
1712                 if( has_proxy && !orig_use_scaler )
1713                         clip->rescale_proxy(orig_scale, new_scale);
1714         }
1715 }
1716
1717 void EDL::add_proxy(int use_scaler,
1718         ArrayList<Indexable*> *orig_assets, ArrayList<Indexable*> *proxy_assets)
1719 {
1720         if( use_scaler ) {
1721                 for( int i=0,n=proxy_assets->size(); i<n; ++i ) {
1722                         Asset *proxy_asset = (Asset *)proxy_assets->get(i);
1723                         proxy_asset->width = orig_assets->get(i)->get_w();
1724                         proxy_asset->height = orig_assets->get(i)->get_h();
1725                 }
1726         }
1727
1728 // change original assets to proxy assets
1729         for( int i=0,n=proxy_assets->size(); i<n; ++i ) {
1730                 Asset *proxy_asset = assets->update((Asset *)proxy_assets->get(i));
1731                 proxy_asset->folder_no = AW_PROXY_FOLDER;
1732 // replace track contents
1733                 for( Track *track=tracks->first; track; track=track->next ) {
1734                         if( track->data_type != TRACK_VIDEO ) continue;
1735                         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1736                                 if( !edit->asset ) continue;
1737                                 if( !strcmp(edit->asset->path, orig_assets->get(i)->path) ) {
1738                                         edit->asset = proxy_asset;
1739                                 }
1740                         }
1741                 }
1742         }
1743 }
1744
1745 Asset *EDL::get_proxy_asset()
1746 {
1747         return folder_no == AW_PROXY_FOLDER ?
1748                 tracks->first->edits->first->asset : 0;
1749 }
1750
1751 Track *EDL::add_new_track(int data_type)
1752 {
1753         Track *new_track = 0;
1754         switch( data_type ) {
1755         case TRACK_VIDEO:
1756                 ++session->video_tracks;
1757                 new_track = tracks->add_video_track(0, 0);
1758                 break;
1759         case TRACK_AUDIO:
1760                 ++session->audio_tracks;
1761                 new_track = tracks->add_audio_track(0, 0);
1762                 break;
1763         case TRACK_SUBTITLE:
1764                 new_track = tracks->add_subttl_track(0, 0);
1765                 break;
1766         }
1767         return new_track;
1768 }
1769
1770 double EDL::get_cursor_position(int cursor_x, int pane_no)
1771 {
1772         return (double)cursor_x * local_session->zoom_sample / session->sample_rate +
1773                 (double)local_session->view_start[pane_no] *
1774                         local_session->zoom_sample / session->sample_rate;
1775 }
1776 int64_t EDL::get_position_cursorx(double position, int pane_no)
1777 {
1778         return (int64_t)(position * session->sample_rate / local_session->zoom_sample)
1779                         - local_session->view_start[pane_no];
1780 }
1781
1782 int EDL::in_use(Indexable *indexable)
1783 {
1784         for( Track *track=tracks->first; track; track=track->next ) {
1785                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1786                         Indexable *idxbl = (Indexable *)edit->asset;
1787                         if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
1788                         if( !idxbl ) continue;
1789                         if( idxbl->id == indexable->id ) return 1;
1790                         if( !indexable->is_asset != !idxbl->is_asset ) continue;
1791                         if( !strcmp(idxbl->path, indexable->path) ) return 1;
1792                 }
1793         }
1794         for( int i=0; i<clips.size(); ++i )
1795                 if( clips[i]->in_use(indexable) ) return 1;
1796         for( int i=0; i<nested_edls.size(); ++i )
1797                 if( nested_edls[i]->in_use(indexable) ) return 1;
1798         return 0;
1799 }
1800
1801 int EDL::regroup(int next_id)
1802 {
1803         ArrayList<int> new_groups;
1804         for( Track *track=tracks->first; track; track=track->next ) {
1805                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1806                         if( !edit->group_id ) continue;
1807                         int k = new_groups.size();
1808                         while( --k >= 0 && new_groups[k] != edit->group_id );
1809                         if( k >= 0 ) continue;
1810                         new_groups.append(edit->group_id);
1811                 }
1812         }
1813         for( Track *track=tracks->first; track; track=track->next ) {
1814                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1815                         if( !edit->group_id ) continue;
1816                         int k = new_groups.size();
1817                         while( --k >= 0 && new_groups[k] != edit->group_id );
1818                         if( k < 0 ) continue;
1819                         edit->group_id = k + next_id;
1820                 }
1821         }
1822         return new_groups.size();
1823 }
1824
1825 EDL *EDL::selected_edits_to_clip(int packed,
1826                 double *start_position, Track **start_track,
1827                 int edit_labels, int edit_autos, int edit_plugins)
1828 {
1829         double start = DBL_MAX, end = DBL_MIN;
1830         Track *first_track=0, *last_track = 0;
1831         for( Track *track=tracks->first; track; track=track->next ) {
1832                 if( !track->record ) continue;
1833                 int empty = 1;
1834                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1835                         if( !edit->is_selected || edit->silence() ) continue;
1836                         double edit_pos = track->from_units(edit->startproject);
1837                         if( start > edit_pos ) start = edit_pos;
1838                         if( end < (edit_pos+=edit->length) ) end = edit_pos;
1839                         empty = 0;
1840                 }
1841                 if( empty ) continue;
1842                 if( !first_track ) first_track = track;
1843                 last_track = track;
1844         }
1845         if( start_position ) *start_position = start;
1846         if( start_track ) *start_track = first_track;
1847         if( !first_track ) return 0;
1848         EDL *new_edl = new EDL();
1849         new_edl->create_objects();
1850         new_edl->copy_session(this);
1851         const char *text = _("new_edl edit");
1852         new_edl->set_path(text);
1853         strcpy(new_edl->local_session->clip_title, text);
1854         strcpy(new_edl->local_session->clip_notes, text);
1855         new_edl->session->video_tracks = 0;
1856         new_edl->session->audio_tracks = 0;
1857         for( Track *track=tracks->first; track; track=track->next ) {
1858                 if( !track->record ) continue;
1859                 if( first_track ) {
1860                         if( first_track != track ) continue;
1861                         first_track = 0;
1862                 }
1863                 Track *new_track = 0;
1864                 if( !packed )
1865                         new_track = new_edl->add_new_track(track->data_type);
1866                 int64_t start_pos = track->to_units(start, 0);
1867                 int64_t end_pos = track->to_units(end, 0);
1868                 int64_t startproject = 0;
1869                 Edit *edit = track->edits->first;
1870                 for( ; edit; edit=edit->next ) {
1871                         if( !edit->is_selected || edit->silence() ) continue;
1872                         if( edit->startproject < start_pos ) continue;
1873                         if( edit->startproject >= end_pos ) break;
1874                         int64_t edit_start_pos = edit->startproject;
1875                         int64_t edit_end_pos = edit->startproject + edit->length;
1876                         if( !new_track )
1877                                 new_track = new_edl->add_new_track(track->data_type);
1878                         int64_t edit_pos = edit_start_pos - start_pos;
1879                         if( !packed && edit_pos > startproject ) {
1880                                 Edit *silence = new Edit(new_edl, new_track);
1881                                 silence->startproject = startproject;
1882                                 silence->length = edit_pos - startproject;
1883                                 new_track->edits->append(silence);
1884                                 startproject = edit_pos;
1885                         }
1886                         int64_t clip_start_pos = startproject;
1887                         Edit *clip_edit = new Edit(new_edl, new_track);
1888                         clip_edit->copy_from(edit);
1889                         clip_edit->startproject = startproject;
1890                         startproject += clip_edit->length;
1891                         new_track->edits->append(clip_edit);
1892                         if( edit_labels ) {
1893                                 double edit_start = track->from_units(edit_start_pos);
1894                                 double edit_end = track->from_units(edit_end_pos);
1895                                 double clip_start = new_track->from_units(clip_start_pos);
1896                                 Label *label = labels->first;
1897                                 for( ; label; label=label->next ) {
1898                                         if( label->position < edit_start ) continue;
1899                                         if( label->position >= edit_end ) break;
1900                                         double clip_position = label->position - edit_start + clip_start;
1901                                         Label *clip_label = new_edl->labels->first;
1902                                         while( clip_label && clip_label->position<clip_position )
1903                                                 clip_label = clip_label->next;
1904                                         if( clip_label && clip_label->position == clip_position ) continue;
1905                                         Label *new_label = new Label(new_edl,
1906                                                 new_edl->labels, clip_position, label->textstr);
1907                                         new_edl->labels->insert_before(clip_label, new_label);
1908                                 }
1909                         }
1910                         if( edit_autos ) {
1911                                 Automation *automation = track->automation;
1912                                 Automation *new_automation = new_track->automation;
1913                                 for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
1914                                         Autos *autos = automation->autos[i];
1915                                         if( !autos ) continue;
1916                                         Autos *new_autos = new_automation->autos[i];
1917                                         new_autos->default_auto->copy_from(autos->default_auto);
1918                                         Auto *aut0 = autos->first;
1919                                         for( ; aut0; aut0=aut0->next ) {
1920                                                 if( aut0->position < edit_start_pos ) continue;
1921                                                 if( aut0->position >= edit_end_pos ) break;
1922                                                 Auto *new_auto = new_autos->new_auto();
1923                                                 new_auto->copy_from(aut0);
1924                                                 int64_t clip_position = aut0->position - edit_start_pos + clip_start_pos;
1925                                                 new_auto->position = clip_position;
1926                                                 new_autos->append(new_auto);
1927                                         }
1928                                 }
1929                         }
1930                         if( edit_plugins ) {
1931                                 while( new_track->plugin_set.size() < track->plugin_set.size() )
1932                                         new_track->plugin_set.append(0);
1933                                 for( int i=0; i<track->plugin_set.total; ++i ) {
1934                                         PluginSet *plugin_set = track->plugin_set[i];
1935                                         if( !plugin_set ) continue;
1936                                         PluginSet *new_plugin_set = new_track->plugin_set[i];
1937                                         if( !new_plugin_set ) {
1938                                                 new_plugin_set = new PluginSet(new_edl, new_track);
1939                                                 new_track->plugin_set[i] = new_plugin_set;
1940                                         }
1941                                         Plugin *plugin = (Plugin*)plugin_set->first;
1942                                         int64_t startplugin = new_plugin_set->length();
1943                                         for( ; plugin ; plugin=(Plugin*)plugin->next ) {
1944                                                 if( plugin->silence() ) continue;
1945                                                 int64_t plugin_start_pos = plugin->startproject;
1946                                                 int64_t plugin_end_pos = plugin_start_pos + plugin->length;
1947                                                 if( plugin_end_pos < start_pos ) continue;
1948                                                 if( plugin_start_pos > end_pos ) break;
1949                                                 if( plugin_start_pos < edit_start_pos )
1950                                                         plugin_start_pos = edit_start_pos;
1951                                                 if( plugin_end_pos > edit_end_pos )
1952                                                         plugin_end_pos = edit_end_pos;
1953                                                 if( plugin_start_pos >= plugin_end_pos ) continue;
1954                                                 int64_t plugin_pos = plugin_start_pos - start_pos;
1955                                                 if( !packed && plugin_pos > startplugin ) {
1956                                                         Plugin *silence = new Plugin(new_edl, new_track, "");
1957                                                         silence->startproject = startplugin;
1958                                                         silence->length = plugin_pos - startplugin;
1959                                                         new_plugin_set->append(silence);
1960                                                         startplugin = plugin_pos;
1961                                                 }
1962                                                 Plugin *new_plugin = new Plugin(new_edl, new_track, plugin->title);
1963                                                 new_plugin->copy_base(plugin);
1964                                                 new_plugin->startproject = startplugin;
1965                                                 new_plugin->length = plugin_end_pos - plugin_start_pos;
1966                                                 startplugin += new_plugin->length;
1967                                                 new_plugin_set->append(new_plugin);
1968                                                 KeyFrames *keyframes = plugin->keyframes;
1969                                                 KeyFrames *new_keyframes = new_plugin->keyframes;
1970                                                 new_keyframes->default_auto->copy_from(keyframes->default_auto);
1971                                                 new_keyframes->default_auto->position = new_plugin->startproject;
1972                                                 KeyFrame *keyframe = (KeyFrame*)keyframes->first;
1973                                                 for( ; keyframe; keyframe=(KeyFrame*)keyframe->next ) {
1974                                                         if( keyframe->position < edit_start_pos ) continue;
1975                                                         if( keyframe->position >= edit_end_pos ) break;
1976                                                         KeyFrame *clip_keyframe = new KeyFrame(new_edl, new_keyframes);
1977                                                         clip_keyframe->copy_from(keyframe);
1978                                                         int64_t key_position = keyframe->position - start_pos;
1979                                                         if( packed )
1980                                                                 key_position += new_plugin->startproject - plugin_pos;
1981                                                         clip_keyframe->position = key_position;
1982                                                         new_keyframes->append(clip_keyframe);
1983                                                 }
1984                                         }
1985                                 }
1986                         }
1987                 }
1988                 if( last_track == track ) break;
1989         }
1990         return new_edl;
1991 }
1992
1993 EDL *EDL::selected_edits_to_clip(int packed, double *start_position, Track **start_track)
1994 {
1995         return selected_edits_to_clip(packed, start_position, start_track,
1996                 session->labels_follow_edits,
1997                 session->autos_follow_edits,
1998                 session->plugins_follow_edits);
1999 }
2000
2001 void EDL::paste_edits(EDL *clip, Track *first_track, double position, int overwrite,
2002                 int edit_edits, int edit_labels, int edit_autos, int edit_plugins)
2003 {
2004         if( !first_track )
2005                 first_track = tracks->first;
2006         Track *src = clip->tracks->first;
2007         for( Track *track=first_track; track && src; track=track->next ) {
2008                 if( !track->record ) continue;
2009                 int64_t pos = track->to_units(position, 0);
2010                 if( edit_edits ) {
2011                         for( Edit *edit=src->edits->first; edit; edit=edit->next ) {
2012                                 if( edit->silence() ) continue;
2013                                 int64_t start = pos + edit->startproject;
2014                                 int64_t len = edit->length, end = start + len;
2015                                 if( overwrite )
2016                                         track->edits->clear(start, end);
2017                                 Edit *dst = track->edits->insert_new_edit(start);
2018                                 dst->copy_from(edit);
2019                                 dst->startproject = start;
2020                                 dst->is_selected = 1;
2021                                 while( (dst=dst->next) != 0 )
2022                                         dst->startproject += edit->length;
2023                                 if( overwrite ) continue;
2024                                 if( edit_labels && track == first_track ) {
2025                                         double dst_pos = track->from_units(start);
2026                                         double dst_len = track->from_units(len);
2027                                         for( Label *label=labels->first; label; label=label->next ) {
2028                                                 if( label->position >= dst_pos )
2029                                                         label->position += dst_len;
2030                                         }
2031                                 }
2032                                 if( edit_autos ) {
2033                                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2034                                                 Autos *autos = track->automation->autos[i];
2035                                                 if( !autos ) continue;
2036                                                 for( Auto *aut0=autos->first; aut0; aut0=aut0->next ) {
2037                                                         if( aut0->position >= start )
2038                                                                 aut0->position += edit->length;
2039                                                 }
2040                                         }
2041                                 }
2042                                 if( edit_plugins ) {
2043                                         for( int i=0; i<track->plugin_set.size(); ++i ) {
2044                                                 PluginSet *plugin_set = track->plugin_set[i];
2045                                                 Plugin *plugin = (Plugin *)plugin_set->first;
2046                                                 for( ; plugin; plugin=(Plugin *)plugin->next ) {
2047                                                         if( plugin->startproject >= start )
2048                                                                 plugin->startproject += edit->length;
2049                                                         else if( plugin->startproject+plugin->length > end )
2050                                                                 plugin->length += edit->length;
2051                                                         Auto *default_keyframe = plugin->keyframes->default_auto;
2052                                                         if( default_keyframe->position >= start )
2053                                                                 default_keyframe->position += edit->length;
2054                                                         KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
2055                                                         for( ; keyframe; keyframe=(KeyFrame*)keyframe->next ) {
2056                                                                 if( keyframe->position >= start )
2057                                                                         keyframe->position += edit->length;
2058                                                         }
2059                                                 }
2060                                                 plugin_set->optimize();
2061                                         }
2062                                 }
2063                         }
2064                 }
2065                 if( edit_autos ) {
2066                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2067                                 Autos *src_autos = src->automation->autos[i];
2068                                 if( !src_autos ) continue;
2069                                 Autos *autos = track->automation->autos[i];
2070                                 for( Auto *aut0=src_autos->first; aut0; aut0=aut0->next ) {
2071                                         int64_t auto_pos = pos + aut0->position;
2072                                         autos->insert_auto(auto_pos, aut0);
2073                                 }
2074                         }
2075                 }
2076                 if( edit_plugins ) {
2077                         for( int i=0; i<src->plugin_set.size(); ++i ) {
2078                                 PluginSet *plugin_set = src->plugin_set[i];
2079                                 if( !plugin_set ) continue;
2080                                 while( i >= track->plugin_set.size() )
2081                                         track->plugin_set.append(0);
2082                                 PluginSet *dst_plugin_set = track->plugin_set[i];
2083                                 if( !dst_plugin_set ) {
2084                                         dst_plugin_set = new PluginSet(this, track);
2085                                         track->plugin_set[i] = dst_plugin_set;
2086                                 }
2087                                 Plugin *plugin = (Plugin *)plugin_set->first;
2088                                 if( plugin ) track->expand_view = 1;
2089                                 for( ; plugin; plugin=(Plugin *)plugin->next ) {
2090                                         int64_t start = pos + plugin->startproject;
2091                                         int64_t end = start + plugin->length;
2092                                         if( overwrite || edit_edits )
2093                                                 dst_plugin_set->clear(start, end, 1);
2094                                         Plugin *new_plugin = dst_plugin_set->insert_plugin(plugin->title,
2095                                                 start, end-start, plugin->plugin_type, &plugin->shared_location,
2096                                                 (KeyFrame*)plugin->keyframes->default_auto, 0);
2097                                         KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
2098                                         for( ; keyframe; keyframe=(KeyFrame*)keyframe->next ) {
2099                                                 int64_t keyframe_pos = pos + keyframe->position;
2100                                                 new_plugin->keyframes->insert_auto(keyframe_pos, keyframe);
2101                                         }
2102                                         while( (new_plugin=(Plugin *)new_plugin->next) ) {
2103                                                 KeyFrame *keyframe = (KeyFrame*)new_plugin->keyframes->first;
2104                                                 for( ; keyframe; keyframe=(KeyFrame*)keyframe->next )
2105                                                         keyframe->position += plugin->length;
2106                                         }
2107                                 }
2108                         }
2109                 }
2110                 src = src->next;
2111         }
2112         if( edit_labels ) {
2113                 Label *edl_label = labels->first;
2114                 for( Label *label=clip->labels->first; label; label=label->next ) {
2115                         double label_pos = position + label->position;
2116                         int exists = 0;
2117                         while( edl_label &&
2118                                 !(exists=equivalent(edl_label->position, label_pos)) &&
2119                                 edl_label->position < position ) edl_label = edl_label->next;
2120                         if( exists ) continue;
2121                         labels->insert_before(edl_label,
2122                                 new Label(this, labels, label_pos, label->textstr));
2123                 }
2124         }
2125         optimize();
2126 }
2127
2128 void EDL::paste_edits(EDL *clip, Track *first_track, double position, int overwrite)
2129 {
2130         paste_edits(clip, first_track, position, overwrite, 1,
2131                 session->labels_follow_edits,
2132                 session->autos_follow_edits,
2133                 session->plugins_follow_edits);
2134 }
2135