dbda78c10612085bb960d17727ae60bf2fe7b4a3
[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 "file.h"
38 #include "filexml.h"
39 #include "floatauto.h"
40 #include "floatautos.h"
41 #include "guicast.h"
42 #include "keyframe.h"
43 #include "keyframes.h"
44 #include "indexstate.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 *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::get_nested_edl()
618 {
619         Track *track = tracks->first;
620         Edit *edit = track ? track->edits->first : 0;
621         EDL *nested = edit && !edit->next && !edit->asset ? edit->nested_edl : 0;
622         while( nested && (track=track->next)!=0 ) {
623                 Edit *edit = track->edits->first;
624                 if( !edit || edit->next ||
625                     ( edit->nested_edl != nested &&
626                       strcmp(edit->nested_edl->path, nested->path) ) )
627                         nested = 0;
628         }
629         return nested;
630 }
631
632
633 EDL *EDL::create_nested_clip(EDL *nested)
634 {
635         EDL *new_edl = new EDL(this);  // parent for clip edl
636         new_edl->create_objects();
637         new_edl->create_nested(nested);
638         return new_edl;
639 }
640
641 void EDL::create_nested(EDL *nested)
642 {
643         int video_tracks = 0, audio_tracks = 0;
644         for( Track *track=nested->tracks->first; track!=0; track=track->next ) {
645                 if( track->data_type == TRACK_VIDEO && track->record ) ++video_tracks;
646                 if( track->data_type == TRACK_AUDIO && track->record ) ++audio_tracks;
647         }
648 // renderengine properties
649         if( video_tracks > 0 )
650                 video_tracks = 1;
651         if( audio_tracks > 0 )
652                 audio_tracks = nested->session->audio_channels;
653         
654 // Keep frame rate, sample rate, and output size unchanged.
655 // Nest all video & audio outputs
656         session->video_channels = video_tracks;
657         session->audio_channels = audio_tracks;
658         session->video_tracks = 1;
659         session->audio_tracks = audio_tracks;
660         create_default_tracks();
661         insert_asset(0, nested, 0, 0, 0);
662 }
663
664 void EDL::overwrite_clip(EDL *clip)
665 {
666         int folder = folder_no;
667         char clip_title[BCTEXTLEN];  strcpy(clip_title, local_session->clip_title);
668         char clip_notes[BCTEXTLEN];  strcpy(clip_notes, local_session->clip_notes);
669         char clip_icon[BCSTRLEN];    strcpy(clip_icon,  local_session->clip_icon);
670         copy_all(clip);
671         folder_no = folder;
672         strcpy(local_session->clip_title, clip_title);
673         strcpy(local_session->clip_notes, clip_notes);
674         strcpy(local_session->clip_icon, clip_icon);
675         if( !clip_icon[0] ) return;
676 // discard old clip icon to reconstruct 
677         char clip_icon_path[BCTEXTLEN];
678         snprintf(clip_icon_path, sizeof(clip_icon_path),
679                 "%s/%s", File::get_config_path(), clip_icon);
680         remove(clip_icon_path);
681 }
682
683 void EDL::retrack()
684 {
685         int min_w = session->output_w, min_h = session->output_h;
686         for( Track *track=tracks->first; track!=0; track=track->next ) {
687                 if( track->data_type != TRACK_VIDEO ) continue;
688                 int w = min_w, h = min_h;
689                 for( Edit *current=track->edits->first; current!=0; current=NEXT ) {
690                         Indexable* indexable = current->get_source();
691                         if( !indexable ) continue;
692                         int edit_w = indexable->get_w(), edit_h = indexable->get_h();
693                         if( w < edit_w ) w = edit_w;
694                         if( h < edit_h ) h = edit_h;
695                 }
696                 if( track->track_w == w && track->track_h == h ) continue;
697                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->
698                         translate_masks( (w - track->track_w) / 2, (h - track->track_h) / 2);
699                 track->track_w = w;  track->track_h = h;
700         }
701 }
702
703 void EDL::rechannel()
704 {
705         for( Track *current=tracks->first; current; current=NEXT ) {
706                 if( current->data_type == TRACK_AUDIO ) {
707                         PanAutos *autos = (PanAutos*)current->automation->autos[AUTOMATION_PAN];
708                         ((PanAuto*)autos->default_auto)->rechannel();
709                         for( PanAuto *keyframe = (PanAuto*)autos->first;
710                              keyframe; keyframe = (PanAuto*)keyframe->next ) {
711                                 keyframe->rechannel();
712                         }
713                 }
714         }
715 }
716
717 void EDL::resample(double old_rate, double new_rate, int data_type)
718 {
719         for( Track *current=tracks->first; current; current=NEXT ) {
720                 if( current->data_type == data_type ) {
721                         current->resample(old_rate, new_rate);
722                 }
723         }
724 }
725
726
727 void EDL::synchronize_params(EDL *edl)
728 {
729         local_session->synchronize_params(edl->local_session);
730         for( Track *this_track=tracks->first, *that_track=edl->tracks->first;
731              this_track && that_track;
732              this_track=this_track->next, that_track=that_track->next ) {
733                 this_track->synchronize_params(that_track);
734         }
735 }
736
737 int EDL::trim_selection(double start,
738         double end,
739         int edit_labels,
740         int edit_plugins,
741         int edit_autos)
742 {
743         if( start != end ) {
744 // clear the data
745                 clear(0,
746                         start,
747                         edit_labels,
748                         edit_plugins,
749                         edit_autos);
750                 clear(end - start,
751                         tracks->total_length(),
752                         edit_labels,
753                         edit_plugins,
754                         edit_autos);
755         }
756         return 0;
757 }
758
759
760 int EDL::equivalent(double position1, double position2)
761 {
762         double threshold = session->cursor_on_frames ?
763                 0.5 / session->frame_rate : 1.0 / session->sample_rate;
764         return fabs(position2 - position1) < threshold ? 1 : 0;
765 }
766
767 double EDL::equivalent_output(EDL *edl)
768 {
769         double result = -1;
770         session->equivalent_output(edl->session, &result);
771         tracks->equivalent_output(edl->tracks, &result);
772         return result;
773 }
774
775
776 void EDL::set_path(const char *path)
777 {
778         if( &this->path[0] == path ) return;
779         strcpy(this->path, path);
780 }
781
782 void EDL::set_inpoint(double position)
783 {
784         if( equivalent(local_session->get_inpoint(), position) &&
785                 local_session->get_inpoint() >= 0 ) {
786                 local_session->unset_inpoint();
787         }
788         else {
789                 local_session->set_inpoint(align_to_frame(position, 0));
790                 if( local_session->get_outpoint() <= local_session->get_inpoint() )
791                         local_session->unset_outpoint();
792         }
793 }
794
795 void EDL::set_outpoint(double position)
796 {
797         if( equivalent(local_session->get_outpoint(), position) &&
798                 local_session->get_outpoint() >= 0 ) {
799                 local_session->unset_outpoint();
800         }
801         else {
802                 local_session->set_outpoint(align_to_frame(position, 0));
803                 if( local_session->get_inpoint() >= local_session->get_outpoint() )
804                         local_session->unset_inpoint();
805         }
806 }
807
808 void EDL::unset_inoutpoint()
809 {
810         local_session->unset_inpoint();
811         local_session->unset_outpoint();
812 }
813
814 int EDL::blade(double position)
815 {
816         return tracks->blade(position);
817 }
818
819 int EDL::clear(double start, double end,
820         int clear_labels, int clear_plugins, int edit_autos)
821 {
822         if( start == end ) {
823                 double distance = 0;
824                 tracks->clear_handle(start,
825                         end,
826                         distance,
827                         clear_labels,
828                         clear_plugins,
829                         edit_autos);
830                 if( clear_labels && distance > 0 )
831                         labels->paste_silence(start,
832                                 start + distance);
833         }
834         else {
835                 tracks->clear(start,
836                         end,
837                         clear_plugins,
838                         edit_autos);
839                 if( clear_labels )
840                         labels->clear(start,
841                                 end,
842                                 1);
843         }
844
845 // Need to put at beginning so a subsequent paste operation starts at the
846 // right position.
847         double position = local_session->get_selectionstart();
848         local_session->set_selectionend(position);
849         local_session->set_selectionstart(position);
850         return 0;
851 }
852
853 int EDL::clear_hard_edges(double start, double end)
854 {
855         return tracks->clear_hard_edges(start, end);
856 }
857
858 static int dead_edit_cmp(Edit**ap, Edit**bp)
859 {
860         Edit *a = *ap, *b = *bp;
861         if( a->track != b->track ) return 0;
862         return a->startproject > b->startproject ? -1 : 1;
863 }
864
865 void EDL::delete_edits(ArrayList<Edit*> *edits, int collapse)
866 {
867         edits->sort(dead_edit_cmp);
868         for( int i=0; i<edits->size(); ++i ) {
869                 Edit *edit = edits->get(i);
870                 Track *track = edit->track;
871                 int64_t start = edit->startproject;
872                 int64_t length = edit->length;
873                 int64_t end = start + length;
874                 if( session->autos_follow_edits ) {
875                         track->automation->clear(start, end, 0, collapse);
876                 }
877                 if( session->plugins_follow_edits ) {
878                         for( int k=0; k<track->plugin_set.size(); ++k ) {
879                                 PluginSet *plugin_set = track->plugin_set[k];
880                                 plugin_set->clear(start, end, 1);
881                                 if( !collapse )
882                                         plugin_set->paste_silence(start, end, 1);
883                                 plugin_set->optimize();
884                         }
885                 }
886                 Edit *dead_edit = edit;
887                 if( collapse ) {
888                         while( (edit=edit->next) )
889                                 edit->startproject -= length;
890                 }
891                 delete dead_edit;
892         }
893         optimize();
894 }
895
896 class Range {
897 public:
898         static int cmp(Range *ap, Range *bp);
899         double start, end;
900         bool operator ==(Range &that) { return this->start == that.start; }
901         bool operator >(Range &that) { return this->start > that.start; }
902 };
903 int Range::cmp(Range *ap, Range *bp) {
904         return ap->start < bp->start ? -1 : ap->start == bp->start ? 0 : 1;
905 }
906
907 static void get_edit_regions(ArrayList<Edit*> *edits, ArrayList<Range> &regions)
908 {
909 // move edit inclusive labels by regions
910         for( int i=0; i<edits->size(); ++i ) {
911                 Edit *edit = edits->get(i);
912                 double pos = edit->track->from_units(edit->startproject);
913                 double end = edit->track->from_units(edit->startproject + edit->length);
914                 int n = regions.size(), k = n;
915                 while( --k >= 0 ) {
916                         Range &range = regions[k];
917                         if( pos >= range.end ) continue;
918                         if( range.start >= end ) continue;
919                         int expand = 0;
920                         if( range.start > pos ) { range.start = pos;  expand = 1; }
921                         if( range.end < end ) { range.end = end;  expand = 1; }
922                         if( !expand ) break;
923                         k = n;
924                 }
925                 if( k < 0 ) {
926                         Range &range = regions.append();
927                         range.start = pos;  range.end = end;
928                 }
929         }
930         regions.sort(Range::cmp);
931 }
932
933 void EDL::delete_edit_labels(ArrayList<Edit*> *edits, int collapse)
934 {
935         ArrayList<Range> regions;
936         get_edit_regions(edits, regions);
937         int n = regions.size(), k = n;
938         while( --k >= 0 ) {
939                 Range &range = regions[k];
940                 labels->clear(range.start, range.end, collapse);
941         }
942 }
943
944 void EDL::move_edit_labels(ArrayList<Edit*> *edits, double dist)
945 {
946         ArrayList<Range> regions;
947         get_edit_regions(edits, regions);
948         int n = regions.size(), k = n;
949         Labels moved(this, 0);
950         while( --k >= 0 ) {
951                 Range &range = regions[k];
952                 Label *label = labels->label_of(range.start);
953                 for( Label *next=0; label && label->position <= range.end; label=next ) {
954                         next = label->next;
955                         labels->remove_pointer(label);
956                         label->position += dist;
957                         moved.append(label);
958                 }
959                 Label *current = labels->first;
960                 while( (label=moved.first) ) {
961                         moved.remove_pointer(label);
962                         while( current && current->position < label->position )
963                                 current = current->next;
964                         if( current && current->position == label->position ) {
965                                 delete label;  continue;
966                         }
967                         labels->insert_before(current, label);
968                 }
969         }
970 }
971
972 void EDL::modify_edithandles(double oldposition, double newposition,
973         int currentend, int handle_mode, int edit_labels,
974         int edit_plugins, int edit_autos, int group_id)
975 {
976         tracks->modify_edithandles(oldposition, newposition,
977                 currentend, handle_mode, edit_labels,
978                 edit_plugins, edit_autos, group_id);
979 }
980
981 void EDL::modify_pluginhandles(double oldposition, double newposition,
982         int currentend, int handle_mode, int edit_labels,
983         int edit_autos, Edits *trim_edits)
984 {
985         tracks->modify_pluginhandles(oldposition, newposition,
986                 currentend, handle_mode, edit_labels,
987                 edit_autos, trim_edits);
988         optimize();
989 }
990
991 void EDL::paste_silence(double start, double end,
992         int edit_labels, int edit_plugins, int edit_autos)
993 {
994         if( edit_labels )
995                 labels->paste_silence(start, end);
996         tracks->paste_silence(start, end, edit_plugins, edit_autos);
997 }
998
999
1000 void EDL::remove_from_project(ArrayList<EDL*> *clips)
1001 {
1002         for( int i=0; i<clips->size(); ++i ) {
1003                 this->clips.remove_clip(clips->get(i));
1004         }
1005 }
1006
1007 void EDL::remove_from_project(ArrayList<Indexable*> *assets)
1008 {
1009 // Remove from clips
1010         if( !parent_edl )
1011                 for( int j=0; j<clips.size(); ++j ) {
1012                         clips[j]->remove_from_project(assets);
1013                 }
1014
1015 // Remove from VWindow EDLs
1016         for( int i=0; i<total_vwindow_edls(); ++i )
1017                 get_vwindow_edl(i)->remove_from_project(assets);
1018
1019         for( int i=0; i<assets->size(); ++i ) {
1020 // Remove from tracks
1021                 for( Track *track=tracks->first; track; track=track->next ) {
1022                         track->remove_asset(assets->get(i));
1023                 }
1024
1025 // Remove from assets
1026                 if( !parent_edl && assets->get(i)->is_asset ) {
1027                         this->assets->remove_asset((Asset*)assets->get(i));
1028                 }
1029                 else
1030                 if( !parent_edl && !assets->get(i)->is_asset ) {
1031                         this->nested_edls.remove_clip((EDL*)assets->get(i));
1032                 }
1033         }
1034 }
1035
1036 void EDL::update_assets(EDL *src)
1037 {
1038         for( Asset *current=src->assets->first; current; current=NEXT ) {
1039                 assets->update(current);
1040         }
1041 }
1042
1043 int EDL::get_tracks_height(Theme *theme)
1044 {
1045         int total_pixels = 0;
1046         for( Track *current=tracks->first; current; current=NEXT ) {
1047                 total_pixels += current->vertical_span(theme);
1048         }
1049         return total_pixels;
1050 }
1051
1052 int64_t EDL::get_tracks_width()
1053 {
1054         int64_t total_pixels = 0;
1055         for( Track *current=tracks->first; current; current=NEXT ) {
1056                 int64_t pixels = current->horizontal_span();
1057                 if( pixels > total_pixels ) total_pixels = pixels;
1058         }
1059 //printf("EDL::get_tracks_width %d\n", total_pixels);
1060         return total_pixels;
1061 }
1062
1063 // int EDL::calculate_output_w(int single_channel)
1064 // {
1065 //      if( single_channel ) return session->output_w;
1066 //
1067 //      int widest = 0;
1068 //      for( int i=0; i<session->video_channels; ++i )
1069 //      {
1070 //              if( session->vchannel_x[i] + session->output_w > widest ) widest = session->vchannel_x[i] + session->output_w;
1071 //      }
1072 //      return widest;
1073 // }
1074 //
1075 // int EDL::calculate_output_h(int single_channel)
1076 // {
1077 //      if( single_channel ) return session->output_h;
1078 //
1079 //      int tallest = 0;
1080 //      for( int i=0; i<session->video_channels; ++i )
1081 //      {
1082 //              if( session->vchannel_y[i] + session->output_h > tallest ) tallest = session->vchannel_y[i] + session->output_h;
1083 //      }
1084 //      return tallest;
1085 // }
1086
1087 // Get the total output size scaled to aspect ratio
1088 void EDL::calculate_conformed_dimensions(int single_channel, float &w, float &h)
1089 {
1090         if( (float)session->output_w / session->output_h > get_aspect_ratio() )
1091                 h = (w = session->output_w) / get_aspect_ratio();
1092         else
1093                 w = (h = session->output_h) * get_aspect_ratio();
1094 }
1095
1096 float EDL::get_aspect_ratio()
1097 {
1098         return session->aspect_w / session->aspect_h;
1099 }
1100
1101 int EDL::dump(FILE *fp)
1102 {
1103         if( parent_edl )
1104                 fprintf(fp,"CLIP\n");
1105         else
1106                 fprintf(fp,"EDL\n");
1107         fprintf(fp,"  clip_title: %s\n"
1108                 "  parent_edl: %p\n", local_session->clip_title, parent_edl);
1109         fprintf(fp,"  selectionstart %f\n  selectionend %f\n  loop_start %f\n  loop_end %f\n",
1110                 local_session->get_selectionstart(1),
1111                 local_session->get_selectionend(1),
1112                 local_session->loop_start,
1113                 local_session->loop_end);
1114         for( int i=0; i<TOTAL_PANES; ++i ) {
1115                 fprintf(fp,"  pane %d view_start=%jd track_start=%d\n", i,
1116                         local_session->view_start[i],
1117                         local_session->track_start[i]);
1118         }
1119
1120         if( !parent_edl ) {
1121                 fprintf(fp,"audio_channels: %d audio_tracks: %d sample_rate: %jd\n",
1122                         session->audio_channels,
1123                         session->audio_tracks,
1124                         session->sample_rate);
1125                 fprintf(fp,"  video_channels: %d\n"
1126                         "  video_tracks: %d\n"
1127                         "  frame_rate: %.2f\n"
1128                         "  frames_per_foot: %.2f\n"
1129                         "  output_w: %d\n"
1130                         "  output_h: %d\n"
1131                         "  aspect_w: %f\n"
1132                         "  aspect_h: %f\n"
1133                         "  color_model: %d\n",
1134                                 session->video_channels,
1135                                 session->video_tracks,
1136                                 session->frame_rate,
1137                                 session->frames_per_foot,
1138                                 session->output_w,
1139                                 session->output_h,
1140                                 session->aspect_w,
1141                                 session->aspect_h,
1142                                 session->color_model);
1143
1144                 fprintf(fp," CLIPS");
1145                 fprintf(fp,"  total: %d\n", clips.size());
1146                 for( int i=0; i<clips.size(); ++i ) {
1147                         fprintf(fp,"\n\n");
1148                         clips[i]->dump(fp);
1149                         fprintf(fp,"\n\n");
1150                 }
1151                 fprintf(fp," NESTED_EDLS");
1152                 fprintf(fp,"  total: %d\n", nested_edls.size());
1153                 for( int i=0; i<nested_edls.size(); ++i )
1154                         fprintf(fp,"   %s\n", nested_edls[i]->path);
1155
1156                 fprintf(fp," VWINDOW EDLS");
1157                 fprintf(fp,"  total: %d\n", total_vwindow_edls());
1158
1159                 for( int i=0; i<total_vwindow_edls(); ++i ) {
1160                         fprintf(fp,"   %s\n", get_vwindow_edl(i)->local_session->clip_title);
1161                 }
1162
1163                 fprintf(fp," ASSETS\n");
1164                 assets->dump(fp);
1165         }
1166         fprintf(fp," LABELS\n");
1167         labels->dump(fp);
1168         fprintf(fp," TRACKS\n");
1169         tracks->dump(fp);
1170 //printf("EDL::dump 2\n");
1171         return 0;
1172 }
1173
1174 EDL* EDL::add_clip(EDL *edl)
1175 {
1176 // Copy argument.  New edls are deleted from MWindow::load_filenames.
1177         EDL *new_edl = new EDL(this);
1178         new_edl->create_objects();
1179         new_edl->copy_all(edl);
1180         new_edl->folder_no = AW_CLIP_FOLDER;
1181         clips.append(new_edl);
1182         return new_edl;
1183 }
1184
1185 void EDL::insert_asset(Asset *asset,
1186         EDL *nested_edl,
1187         double position,
1188         Track *first_track,
1189         RecordLabels *labels)
1190 {
1191 // Insert asset into asset table
1192         Asset *new_asset = 0;
1193         EDL *new_nested_edl = 0;
1194
1195         if( asset ) new_asset = assets->update(asset);
1196         if( nested_edl ) new_nested_edl = nested_edls.get_nested(nested_edl);
1197
1198 // Paste video
1199         int vtrack = 0;
1200         Track *current = first_track ? first_track : tracks->first;
1201
1202
1203 // Fix length of single frame
1204         double length = 0.;
1205         int layers = 0;
1206         int channels = 0;
1207
1208         if( new_nested_edl ) {
1209                 length = new_nested_edl->tracks->total_length();
1210                 layers = new_nested_edl->session->video_channels;
1211                 channels = new_nested_edl->session->audio_channels;
1212         }
1213
1214         if( new_asset ) {
1215 // Insert 1 frame for undefined length
1216                 if( new_asset->video_length < 0 ) {
1217                         length = session->si_useduration ?
1218                                 session->si_duration :
1219                                 1.0 / session->frame_rate;
1220                 }
1221                 else {
1222                         length = new_asset->frame_rate > 0 ?
1223                                 (double)new_asset->video_length / new_asset->frame_rate :
1224                                 1.0 / session->frame_rate;
1225                 }
1226                 layers = new_asset->layers;
1227                 channels = new_asset->channels;
1228         }
1229
1230         for( ; current && vtrack<layers; current=NEXT ) {
1231                 if( !current->record || current->data_type != TRACK_VIDEO ) continue;
1232                 current->insert_asset(new_asset, new_nested_edl,
1233                         length, position, vtrack++);
1234         }
1235
1236         int atrack = 0;
1237         if( new_asset ) {
1238                 if( new_asset->audio_length < 0 ) {
1239 // Insert 1 frame for undefined length & video
1240                         if( new_asset->video_data )
1241                                 length = (double)1.0 / new_asset->frame_rate;
1242                         else
1243 // Insert 1 second for undefined length & no video
1244                                 length = 1.0;
1245                 }
1246                 else
1247                         length = (double)new_asset->audio_length /
1248                                         new_asset->sample_rate;
1249         }
1250
1251         current = tracks->first;
1252         for( ; current && atrack < channels; current=NEXT ) {
1253                 if( !current->record || current->data_type != TRACK_AUDIO ) continue;
1254                 current->insert_asset(new_asset, new_nested_edl,
1255                         length, position, atrack++);
1256         }
1257
1258 // Insert labels from a recording window.
1259         if( labels ) {
1260                 for( RecordLabel *label=labels->first; label; label=label->next ) {
1261                         this->labels->toggle_label(label->position, label->position);
1262                 }
1263         }
1264 }
1265
1266
1267
1268 void EDL::set_index_file(Indexable *indexable)
1269 {
1270         if( indexable->is_asset )
1271                 assets->update_index((Asset*)indexable);
1272         else
1273                 nested_edls.update_index((EDL*)indexable);
1274 }
1275
1276 void EDL::optimize()
1277 {
1278 //printf("EDL::optimize 1\n");
1279         double length = tracks->total_length();
1280         double preview_start = local_session->preview_start;
1281         double preview_end = local_session->preview_end;
1282         if( preview_end < 0 || preview_end > length )
1283                 preview_end = length;
1284         if( preview_start == 0 && preview_end >= length )
1285                 local_session->preview_end = -1;
1286         if( preview_start > preview_end )
1287                 local_session->preview_start = preview_end;
1288         for( Track *current=tracks->first; current; current=NEXT )
1289                 current->optimize();
1290 }
1291
1292 int EDL::next_id()
1293 {
1294         static Mutex id_lock;
1295         id_lock.lock("EDL::next_id");
1296         int result = EDLSession::current_id++;
1297         id_lock.unlock();
1298         return result;
1299 }
1300
1301 void EDL::get_shared_plugins(Track *source,
1302         ArrayList<SharedLocation*> *plugin_locations,
1303         int omit_recordable,
1304         int data_type)
1305 {
1306         for( Track *track=tracks->first; track; track=track->next ) {
1307                 if( track->record && omit_recordable ) continue;
1308                 if( track == source || track->data_type != data_type ) continue;
1309                 for( int i=0; i<track->plugin_set.size(); ++i ) {
1310                         Plugin *plugin = track->get_current_plugin(
1311                                 local_session->get_selectionstart(1),
1312                                 i, PLAY_FORWARD, 1, 0);
1313                         if( plugin && plugin->plugin_type != PLUGIN_STANDALONE ) continue;
1314                         plugin_locations->append(new SharedLocation(tracks->number_of(track), i));
1315                 }
1316         }
1317 }
1318
1319 void EDL::get_shared_tracks(Track *track,
1320         ArrayList<SharedLocation*> *module_locations,
1321         int omit_recordable, int data_type)
1322 {
1323         for( Track *current=tracks->first; current; current=NEXT ) {
1324                 if( omit_recordable && current->record ) continue;
1325                 if( current == track || current->data_type != data_type ) continue;
1326                 module_locations->append(new SharedLocation(tracks->number_of(current), 0));
1327         }
1328 }
1329
1330 // aligned frame time, account for sample truncation
1331 double EDL::frame_align(double position, int round)
1332 {
1333         if( !round && session->sample_rate > 0 ) {
1334                 int64_t sample_pos = position * session->sample_rate;
1335                 position = (sample_pos+2.) / session->sample_rate;
1336         }
1337         int64_t frame_pos = (position * session->frame_rate + (round ? 0.5 : 1e-6));
1338         return frame_pos / session->frame_rate;
1339 }
1340
1341 // Convert position to frames if alignment is enabled.
1342 double EDL::align_to_frame(double position, int round)
1343 {
1344         if( session->cursor_on_frames )
1345                 position = frame_align(position, round);
1346         return position;
1347 }
1348
1349
1350 BinFolder *EDL::get_folder(int no)
1351 {
1352         for( int i=0; i<folders.size(); ++i ) {
1353                 BinFolder *fp = folders[i];
1354                 if( no == fp->awindow_folder ) return fp;
1355         }
1356         return 0;
1357 }
1358
1359 int EDL::get_folder_number(const char *title)
1360 {
1361         for( int i=0; i<AWINDOW_FOLDERS; ++i ) {
1362                 if( !strcmp(title, AWindowGUI::folder_names[i]) )
1363                         return i;
1364         }
1365         for( int i=0; i<folders.size(); ++i ) {
1366                 if( !strcmp(title, folders[i]->title) )
1367                         return folders[i]->awindow_folder;
1368         }
1369         return AW_NO_FOLDER;
1370 }
1371
1372 const char *EDL::get_folder_name(int no)
1373 {
1374         if( no >= 0 && no<AWINDOW_FOLDERS )
1375                 return AWindowGUI::folder_names[no];
1376         BinFolder *fp = get_folder(no);
1377         return !fp ? "" : fp->title;
1378 }
1379
1380 int EDL::new_folder(const char *title, int is_clips)
1381 {
1382         if( !title[0] ) return 1;
1383         int ret = get_folder_number(title);
1384         if( ret >= 0 ) return 1;
1385         int idx = AWINDOW_FOLDERS;
1386         for( int i=0; i<folders.size(); ++i ) {
1387                 BinFolder *fp = folders[i];
1388                 int no = fp->awindow_folder;
1389                 if( no >= idx ) idx = no+1;
1390         }
1391         folders.append(new BinFolder(idx, is_clips, title));
1392         return 0;
1393 }
1394
1395 int EDL::delete_folder(const char *title)
1396 {
1397         int k = folders.size();
1398         while( --k >= 0 && strcmp(title, folders[k]->title) );
1399         if( k >= 0 )
1400                 folders.remove_object_number(k);
1401         return k;
1402 }
1403
1404 int EDL::get_use_vconsole(VEdit* *playable_edit,
1405         int64_t position, int direction, PlayableTracks *playable_tracks)
1406 {
1407         int share_playable_tracks = 1;
1408         int result = 0;
1409         VTrack *playable_track = 0;
1410         const int debug = 0;
1411         *playable_edit = 0;
1412
1413 // Calculate playable tracks when being called as a nested EDL
1414         if( !playable_tracks ) {
1415                 share_playable_tracks = 0;
1416                 playable_tracks = new PlayableTracks(this,
1417                         position, direction, TRACK_VIDEO, 1);
1418         }
1419
1420
1421 // Total number of playable tracks is 1
1422         if( playable_tracks->size() != 1 ) {
1423                 result = 1;
1424         }
1425         else {
1426                 playable_track = (VTrack*)playable_tracks->get(0);
1427         }
1428
1429 // Don't need playable tracks anymore
1430         if( !share_playable_tracks ) {
1431                 delete playable_tracks;
1432         }
1433
1434 if( debug ) printf("EDL::get_use_vconsole %d playable_tracks->size()=%d\n",
1435  __LINE__, playable_tracks->size());
1436         if( result ) return 1;
1437
1438
1439 // Test mutual conditions between direct copy rendering and this.
1440         if( !playable_track->direct_copy_possible(position,
1441                 direction,
1442                 1) )
1443                 return 1;
1444 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1445
1446         *playable_edit = (VEdit*)playable_track->edits->editof(position,
1447                 direction, 0);
1448 // No edit at current location
1449         if( !*playable_edit ) return 1;
1450 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1451
1452
1453 // Edit is nested EDL
1454         if( (*playable_edit)->nested_edl ) {
1455 // Test nested EDL
1456                 EDL *nested_edl = (*playable_edit)->nested_edl;
1457                 int64_t nested_position = (int64_t)((position -
1458                                 (*playable_edit)->startproject +
1459                                 (*playable_edit)->startsource) *
1460                         nested_edl->session->frame_rate /
1461                         session->frame_rate);
1462
1463
1464                 VEdit *playable_edit_temp = 0;
1465                 if( session->output_w != nested_edl->session->output_w ||
1466                         session->output_h != nested_edl->session->output_h ||
1467                         nested_edl->get_use_vconsole(&playable_edit_temp,
1468                                 nested_position,
1469                                 direction,
1470                                 0) )
1471                         return 1;
1472
1473                 return 0;
1474         }
1475
1476 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1477 // Edit is not a nested EDL
1478         Asset *asset = (*playable_edit)->asset;
1479 // Edit is silence
1480         if( !asset ) return 1;
1481 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1482
1483 // Asset and output device must have the same dimensions
1484         if( asset->width != session->output_w ||
1485             asset->height != session->output_h )
1486                 return 1;
1487
1488 if( debug ) printf("EDL::get_use_vconsole %d\n", __LINE__);
1489 // If we get here the frame is going to be directly copied.  Whether it is
1490 // decompressed in hardware depends on the colormodel.
1491         return 0;
1492 }
1493
1494
1495 // For Indexable
1496 int EDL::get_audio_channels()
1497 {
1498         return session->audio_channels;
1499 }
1500
1501 int EDL::get_sample_rate()
1502 {
1503         return session->sample_rate;
1504 }
1505
1506 int64_t EDL::get_audio_samples()
1507 {
1508         return (int64_t)(tracks->total_length() *
1509                 session->sample_rate);
1510 }
1511
1512 int EDL::have_audio()
1513 {
1514         return 1;
1515 }
1516
1517 int EDL::have_video()
1518 {
1519         return 1;
1520 }
1521
1522
1523 int EDL::get_w()
1524 {
1525         return session->output_w;
1526 }
1527
1528 int EDL::get_h()
1529 {
1530         return session->output_h;
1531 }
1532
1533 double EDL::get_frame_rate()
1534 {
1535         return session->frame_rate;
1536 }
1537
1538 int EDL::get_video_layers()
1539 {
1540         return 1;
1541 }
1542
1543 int64_t EDL::get_video_frames()
1544 {
1545         return (int64_t)(tracks->total_length() *
1546                 session->frame_rate);
1547 }
1548
1549 void EDL::remove_vwindow_edls()
1550 {
1551         for( int i=0; i<total_vwindow_edls(); ++i ) {
1552                 get_vwindow_edl(i)->remove_user();
1553         }
1554         vwindow_edls.remove_all();
1555 }
1556
1557 void EDL::remove_vwindow_edl(EDL *edl)
1558 {
1559         if( vwindow_edls.number_of(edl) >= 0 ) {
1560                 edl->remove_user();
1561                 vwindow_edls.remove(edl);
1562         }
1563 }
1564
1565
1566 EDL* EDL::get_vwindow_edl(int number)
1567 {
1568         return vwindow_edls.get(number);
1569 }
1570
1571 int EDL::total_vwindow_edls()
1572 {
1573         return vwindow_edls.size();
1574 }
1575
1576 void EDL::append_vwindow_edl(EDL *edl, int increase_counter)
1577 {
1578         if(vwindow_edls.number_of(edl) >= 0) return;
1579
1580         if(increase_counter) edl->add_user();
1581         vwindow_edls.append(edl);
1582 }
1583
1584
1585 double EDL::next_edit(double position)
1586 {
1587         Units::fix_double(&position);
1588         double new_position = tracks->total_length();
1589
1590         double max_rate = get_frame_rate();
1591         int sample_rate = get_sample_rate();
1592         if( sample_rate > max_rate ) max_rate = sample_rate;
1593         double min_movement = max_rate > 0 ? 1. / max_rate : 1e-6;
1594
1595 // Test for edit handles after position
1596         for( Track *track=tracks->first; track; track=track->next ) {
1597                 if( !track->record ) continue;
1598                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1599                         double edit_end = track->from_units(edit->startproject + edit->length);
1600                         Units::fix_double(&edit_end);
1601                         if( fabs(edit_end-position) < min_movement ) continue;
1602                         if( edit_end > position && edit_end < new_position )
1603                                 new_position = edit_end;
1604                 }
1605         }
1606         return new_position;
1607 }
1608
1609 double EDL::prev_edit(double position)
1610 {
1611         Units::fix_double(&position);
1612         double new_position = -1;
1613
1614         double max_rate = get_frame_rate();
1615         int sample_rate = get_sample_rate();
1616         if( sample_rate > max_rate ) max_rate = sample_rate;
1617         double min_movement = max_rate > 0 ? 1. / max_rate : 1e-6;
1618
1619 // Test for edit handles before cursor position
1620         for( Track *track=tracks->first; track; track=track->next ) {
1621                 if( !track->record ) continue;
1622                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1623                         double edit_end = track->from_units(edit->startproject);
1624                         Units::fix_double(&edit_end);
1625                         if( fabs(edit_end-position) < min_movement ) continue;
1626                         if( edit_end < position && edit_end > new_position )
1627                                 new_position = edit_end;
1628                 }
1629         }
1630         return new_position;
1631 }
1632
1633 double EDL::skip_silence(double position)
1634 {
1635         double result = position, nearest = DBL_MAX;
1636         for( Track *track=tracks->first; track; track=track->next ) {
1637                 if( !track->play ) continue;
1638                 Edit *edit = track->edits->editof(position, PLAY_FORWARD, 0);
1639                 while( edit && edit->silence() ) edit = edit->next;
1640                 if( !edit ) continue;
1641                 double pos = track->from_units(edit->startproject);
1642                 if( pos > position && pos < nearest )
1643                         nearest = result = pos;
1644         }
1645         return result;
1646 }
1647
1648 void EDL::rescale_proxy(int orig_scale, int new_scale)
1649 {
1650         if( orig_scale == new_scale ) return;
1651 // project size
1652         float orig_w = (float)session->output_w * orig_scale;
1653         float orig_h = (float)session->output_h * orig_scale;
1654         if( !parent_edl ) {
1655                 session->output_w = Units::round(orig_w / new_scale);
1656                 session->output_h = Units::round(orig_h / new_scale);
1657         }
1658
1659 // track sizes
1660         for( Track *track=tracks->first; track; track=track->next ) {
1661                 if( track->data_type != TRACK_VIDEO ) continue;
1662                 orig_w = (float)track->track_w * orig_scale;
1663                 orig_h = (float)track->track_h * orig_scale;
1664                 track->track_w = Units::round(orig_w / new_scale);
1665                 track->track_h = Units::round(orig_h / new_scale);
1666                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->
1667                         set_proxy(orig_scale, new_scale);
1668                 ((FloatAutos*)track->automation->autos[AUTOMATION_CAMERA_X])->
1669                         set_proxy(orig_scale, new_scale);
1670                 ((FloatAutos*)track->automation->autos[AUTOMATION_CAMERA_Y])->
1671                         set_proxy(orig_scale, new_scale);
1672                 ((FloatAutos*)track->automation->autos[AUTOMATION_PROJECTOR_X])->
1673                         set_proxy(orig_scale, new_scale);
1674                 ((FloatAutos*)track->automation->autos[AUTOMATION_PROJECTOR_Y])->
1675                         set_proxy(orig_scale, new_scale);
1676         }
1677 }
1678
1679 void EDL::set_proxy(int new_scale, int new_use_scaler,
1680         ArrayList<Indexable*> *orig_assets, ArrayList<Indexable*> *proxy_assets)
1681 {
1682         int orig_scale = session->proxy_scale;
1683         session->proxy_scale = new_scale;
1684         int orig_use_scaler = session->proxy_use_scaler;
1685         session->proxy_use_scaler = new_use_scaler;
1686         if( orig_use_scaler ) orig_scale = 1;
1687         int scale = new_use_scaler ? new_scale : 1;
1688         int asset_scale = new_scale == 1 && !new_use_scaler ? 0 : scale;
1689 // change original assets to proxy assets
1690         int folder_no = new_use_scaler || new_scale != 1 ? AW_PROXY_FOLDER : AW_MEDIA_FOLDER;
1691         for( int i=0,n=proxy_assets->size(); i<n; ++i ) {
1692                 Indexable *proxy_idxbl = proxy_assets->get(i);
1693                 proxy_idxbl->folder_no = folder_no;
1694                 if( !proxy_idxbl->is_asset ) continue;
1695                 Asset *proxy_asset = assets->update((Asset *)proxy_idxbl);
1696                 proxy_asset->width = proxy_asset->actual_width * scale;
1697                 proxy_asset->height = proxy_asset->actual_height * scale;
1698                 proxy_asset->proxy_scale = asset_scale;
1699         }
1700 // rescale to full size asset in read_frame
1701         if( new_use_scaler ) new_scale = 1;
1702         rescale_proxy(orig_scale, new_scale);
1703
1704 // replace track contents
1705         for( Track *track=tracks->first; track; track=track->next ) {
1706                 if( track->data_type != TRACK_VIDEO ) continue;
1707                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1708                         Indexable *idxbl = (Indexable *)edit->asset;
1709                         if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
1710                         if( !idxbl ) continue;
1711                         int i = orig_assets->size();
1712                         while( --i>=0 && strcmp(orig_assets->get(i)->path, idxbl->path) );
1713                         if( i < 0 ) continue;
1714                         Indexable *proxy_idxbl = proxy_assets->get(i);
1715                         Asset *proxy_asset = proxy_idxbl->is_asset ?
1716                                 assets->update((Asset *)proxy_idxbl) : 0;
1717                         EDL *proxy_edl = !proxy_idxbl->is_asset ?
1718                                 (EDL *)proxy_idxbl : 0;
1719                         edit->asset = proxy_asset;
1720                         edit->nested_edl = proxy_edl;
1721                 }
1722         }
1723         for( int j=0,n=clips.size(); j<n; ++j ) {
1724                 EDL *clip = clips[j];
1725                 int has_proxy = 0;
1726                 for( Track *track=clip->tracks->first; track; track=track->next ) {
1727                         if( track->data_type != TRACK_VIDEO ) continue;
1728                         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1729                                 Indexable *idxbl = (Indexable *)edit->asset;
1730                                 if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
1731                                 if( !idxbl ) continue;
1732                                 int i = orig_assets->size();
1733                                 while( --i>=0 && strcmp(orig_assets->get(i)->path, idxbl->path) );
1734                                 if( i < 0 ) continue;
1735                                 Indexable *proxy_idxbl = proxy_assets->get(i);
1736                                 Asset *proxy_asset = proxy_idxbl->is_asset ?
1737                                         assets->update((Asset *)proxy_idxbl) : 0;
1738                                 EDL *proxy_edl = !proxy_idxbl->is_asset ?
1739                                         (EDL *)proxy_idxbl : 0;
1740                                 edit->asset = proxy_asset;
1741                                 edit->nested_edl = proxy_edl;
1742                                 has_proxy = 1;
1743                         }
1744                 }
1745                 if( has_proxy && !orig_use_scaler )
1746                         clip->rescale_proxy(orig_scale, new_scale);
1747         }
1748 }
1749
1750 void EDL::add_proxy(ArrayList<Indexable*> *orig_assets, ArrayList<Indexable*> *proxy_assets)
1751 {
1752         int asset_scale = session->proxy_scale;
1753         if( asset_scale == 1 ) asset_scale = 0;
1754         int scale = !asset_scale ? 1 : asset_scale;
1755 // update proxy geom using scale
1756         for( int i=0; i<proxy_assets->size(); ++i ) {
1757                 Asset *proxy_asset = (Asset *)proxy_assets->get(i);
1758                 proxy_asset->proxy_scale = asset_scale;
1759                 proxy_asset->width = proxy_asset->actual_width * scale;
1760                 proxy_asset->height = proxy_asset->actual_height * scale;
1761         }
1762
1763 // change original assets to proxy assets
1764         for( int i=0,n=proxy_assets->size(); i<n; ++i ) {
1765                 Asset *proxy_asset = assets->update((Asset *)proxy_assets->get(i));
1766                 proxy_asset->folder_no = AW_PROXY_FOLDER;
1767 // replace track contents
1768                 for( Track *track=tracks->first; track; track=track->next ) {
1769                         if( track->data_type != TRACK_VIDEO ) continue;
1770                         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1771                                 if( !edit->asset ) continue;
1772                                 if( !strcmp(edit->asset->path, orig_assets->get(i)->path) ) {
1773                                         edit->asset = proxy_asset;
1774                                 }
1775                         }
1776                 }
1777         }
1778 }
1779
1780 Asset *EDL::get_proxy_asset()
1781 {
1782         return folder_no == AW_PROXY_FOLDER ?
1783                 tracks->first->edits->first->asset : 0;
1784 }
1785
1786 Track *EDL::add_new_track(int data_type)
1787 {
1788         Track *new_track = 0;
1789         switch( data_type ) {
1790         case TRACK_VIDEO:
1791                 ++session->video_tracks;
1792                 new_track = tracks->add_video_track(0, 0);
1793                 break;
1794         case TRACK_AUDIO:
1795                 ++session->audio_tracks;
1796                 new_track = tracks->add_audio_track(0, 0);
1797                 break;
1798         case TRACK_SUBTITLE:
1799                 new_track = tracks->add_subttl_track(0, 0);
1800                 break;
1801         }
1802         return new_track;
1803 }
1804
1805 double EDL::get_cursor_position(int cursor_x, int pane_no)
1806 {
1807         return (double)cursor_x * local_session->zoom_sample / session->sample_rate +
1808                 (double)local_session->view_start[pane_no] *
1809                         local_session->zoom_sample / session->sample_rate;
1810 }
1811 int64_t EDL::get_position_cursorx(double position, int pane_no)
1812 {
1813         return (int64_t)(position * session->sample_rate / local_session->zoom_sample)
1814                         - local_session->view_start[pane_no];
1815 }
1816
1817 int EDL::in_use(Indexable *indexable)
1818 {
1819         for( Track *track=tracks->first; track; track=track->next ) {
1820                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1821                         Indexable *idxbl = (Indexable *)edit->asset;
1822                         if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
1823                         if( !idxbl ) continue;
1824                         if( idxbl->id == indexable->id ) return 1;
1825                         if( !indexable->is_asset != !idxbl->is_asset ) continue;
1826                         if( !strcmp(idxbl->path, indexable->path) ) return 1;
1827                 }
1828         }
1829         for( int i=0; i<clips.size(); ++i )
1830                 if( clips[i]->in_use(indexable) ) return 1;
1831         for( int i=0; i<nested_edls.size(); ++i )
1832                 if( nested_edls[i]->in_use(indexable) ) return 1;
1833         return 0;
1834 }
1835
1836 int EDL::regroup(int next_id)
1837 {
1838         ArrayList<int> new_groups;
1839         for( Track *track=tracks->first; track; track=track->next ) {
1840                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1841                         if( !edit->group_id ) continue;
1842                         int k = new_groups.size();
1843                         while( --k >= 0 && new_groups[k] != edit->group_id );
1844                         if( k >= 0 ) continue;
1845                         new_groups.append(edit->group_id);
1846                 }
1847         }
1848         for( Track *track=tracks->first; track; track=track->next ) {
1849                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1850                         if( !edit->group_id ) continue;
1851                         int k = new_groups.size();
1852                         while( --k >= 0 && new_groups[k] != edit->group_id );
1853                         if( k < 0 ) continue;
1854                         edit->group_id = k + next_id;
1855                 }
1856         }
1857         return new_groups.size();
1858 }
1859
1860 EDL *EDL::selected_edits_to_clip(int packed,
1861                 double *start_position, Track **start_track,
1862                 int edit_labels, int edit_autos, int edit_plugins)
1863 {
1864         double start = DBL_MAX, end = DBL_MIN;
1865         Track *first_track=0, *last_track = 0;
1866         for( Track *track=tracks->first; track; track=track->next ) {
1867                 if( !track->record ) continue;
1868                 int empty = 1;
1869                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1870                         if( !edit->is_selected || edit->silence() ) continue;
1871                         double edit_pos = track->from_units(edit->startproject);
1872                         if( start > edit_pos ) start = edit_pos;
1873                         if( end < (edit_pos+=edit->length) ) end = edit_pos;
1874                         empty = 0;
1875                 }
1876                 if( empty ) continue;
1877                 if( !first_track ) first_track = track;
1878                 last_track = track;
1879         }
1880         if( start_position ) *start_position = start;
1881         if( start_track ) *start_track = first_track;
1882         if( !first_track ) return 0;
1883         EDL *new_edl = new EDL();
1884         new_edl->create_objects();
1885         new_edl->copy_session(this);
1886         const char *text = _("new_edl edit");
1887         new_edl->set_path(text);
1888         strcpy(new_edl->local_session->clip_title, text);
1889         strcpy(new_edl->local_session->clip_notes, text);
1890         new_edl->session->video_tracks = 0;
1891         new_edl->session->audio_tracks = 0;
1892         for( Track *track=tracks->first; track; track=track->next ) {
1893                 if( !track->record ) continue;
1894                 if( first_track ) {
1895                         if( first_track != track ) continue;
1896                         first_track = 0;
1897                 }
1898                 Track *new_track = 0;
1899                 if( !packed )
1900                         new_track = new_edl->add_new_track(track->data_type);
1901                 int64_t start_pos = track->to_units(start, 0);
1902                 int64_t end_pos = track->to_units(end, 0);
1903                 int64_t startproject = 0;
1904                 Edit *edit = track->edits->first;
1905                 for( ; edit; edit=edit->next ) {
1906                         if( !edit->is_selected || edit->silence() ) continue;
1907                         if( edit->startproject < start_pos ) continue;
1908                         if( edit->startproject >= end_pos ) break;
1909                         int64_t edit_start_pos = edit->startproject;
1910                         int64_t edit_end_pos = edit->startproject + edit->length;
1911                         if( !new_track )
1912                                 new_track = new_edl->add_new_track(track->data_type);
1913                         int64_t edit_pos = edit_start_pos - start_pos;
1914                         if( !packed && edit_pos > startproject ) {
1915                                 Edit *silence = new Edit(new_edl, new_track);
1916                                 silence->startproject = startproject;
1917                                 silence->length = edit_pos - startproject;
1918                                 new_track->edits->append(silence);
1919                                 startproject = edit_pos;
1920                         }
1921                         int64_t clip_start_pos = startproject;
1922                         Edit *clip_edit = new Edit(new_edl, new_track);
1923                         clip_edit->copy_from(edit);
1924                         clip_edit->startproject = startproject;
1925                         startproject += clip_edit->length;
1926                         new_track->edits->append(clip_edit);
1927                         if( edit_labels ) {
1928                                 double edit_start = track->from_units(edit_start_pos);
1929                                 double edit_end = track->from_units(edit_end_pos);
1930                                 double clip_start = new_track->from_units(clip_start_pos);
1931                                 Label *label = labels->first;
1932                                 for( ; label; label=label->next ) {
1933                                         if( label->position < edit_start ) continue;
1934                                         if( label->position >= edit_end ) break;
1935                                         double clip_position = label->position - edit_start + clip_start;
1936                                         Label *clip_label = new_edl->labels->first;
1937                                         while( clip_label && clip_label->position<clip_position )
1938                                                 clip_label = clip_label->next;
1939                                         if( clip_label && clip_label->position == clip_position ) continue;
1940                                         Label *new_label = new Label(new_edl,
1941                                                 new_edl->labels, clip_position, label->textstr);
1942                                         new_edl->labels->insert_before(clip_label, new_label);
1943                                 }
1944                         }
1945                         if( edit_autos ) {
1946                                 Automation *automation = track->automation;
1947                                 Automation *new_automation = new_track->automation;
1948                                 for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
1949                                         Autos *autos = automation->autos[i];
1950                                         if( !autos ) continue;
1951                                         Autos *new_autos = new_automation->autos[i];
1952                                         new_autos->default_auto->copy_from(autos->default_auto);
1953                                         Auto *aut0 = autos->first;
1954                                         for( ; aut0; aut0=aut0->next ) {
1955                                                 if( aut0->position < edit_start_pos ) continue;
1956                                                 if( aut0->position >= edit_end_pos ) break;
1957                                                 Auto *new_auto = new_autos->new_auto();
1958                                                 new_auto->copy_from(aut0);
1959                                                 int64_t clip_position = aut0->position - edit_start_pos + clip_start_pos;
1960                                                 new_auto->position = clip_position;
1961                                                 new_autos->append(new_auto);
1962                                         }
1963                                 }
1964                         }
1965                         if( edit_plugins ) {
1966                                 while( new_track->plugin_set.size() < track->plugin_set.size() )
1967                                         new_track->plugin_set.append(0);
1968                                 for( int i=0; i<track->plugin_set.total; ++i ) {
1969                                         PluginSet *plugin_set = track->plugin_set[i];
1970                                         if( !plugin_set ) continue;
1971                                         PluginSet *new_plugin_set = new_track->plugin_set[i];
1972                                         if( !new_plugin_set ) {
1973                                                 new_plugin_set = new PluginSet(new_edl, new_track);
1974                                                 new_track->plugin_set[i] = new_plugin_set;
1975                                         }
1976                                         Plugin *plugin = (Plugin*)plugin_set->first;
1977                                         int64_t startplugin = new_plugin_set->length();
1978                                         for( ; plugin ; plugin=(Plugin*)plugin->next ) {
1979                                                 if( plugin->silence() ) continue;
1980                                                 int64_t plugin_start_pos = plugin->startproject;
1981                                                 int64_t plugin_end_pos = plugin_start_pos + plugin->length;
1982                                                 if( plugin_end_pos < start_pos ) continue;
1983                                                 if( plugin_start_pos > end_pos ) break;
1984                                                 if( plugin_start_pos < edit_start_pos )
1985                                                         plugin_start_pos = edit_start_pos;
1986                                                 if( plugin_end_pos > edit_end_pos )
1987                                                         plugin_end_pos = edit_end_pos;
1988                                                 if( plugin_start_pos >= plugin_end_pos ) continue;
1989                                                 int64_t plugin_pos = plugin_start_pos - start_pos;
1990                                                 if( !packed && plugin_pos > startplugin ) {
1991                                                         Plugin *silence = new Plugin(new_edl, new_track, "");
1992                                                         silence->startproject = startplugin;
1993                                                         silence->length = plugin_pos - startplugin;
1994                                                         new_plugin_set->append(silence);
1995                                                         startplugin = plugin_pos;
1996                                                 }
1997                                                 Plugin *new_plugin = new Plugin(new_edl, new_track, plugin->title);
1998                                                 new_plugin->copy_base(plugin);
1999                                                 new_plugin->startproject = startplugin;
2000                                                 new_plugin->length = plugin_end_pos - plugin_start_pos;
2001                                                 startplugin += new_plugin->length;
2002                                                 new_plugin_set->append(new_plugin);
2003                                                 KeyFrames *keyframes = plugin->keyframes;
2004                                                 KeyFrames *new_keyframes = new_plugin->keyframes;
2005                                                 new_keyframes->default_auto->copy_from(keyframes->default_auto);
2006                                                 new_keyframes->default_auto->position = new_plugin->startproject;
2007                                                 KeyFrame *keyframe = (KeyFrame*)keyframes->first;
2008                                                 for( ; keyframe; keyframe=(KeyFrame*)keyframe->next ) {
2009                                                         if( keyframe->position < edit_start_pos ) continue;
2010                                                         if( keyframe->position >= edit_end_pos ) break;
2011                                                         KeyFrame *clip_keyframe = new KeyFrame(new_edl, new_keyframes);
2012                                                         clip_keyframe->copy_from(keyframe);
2013                                                         int64_t key_position = keyframe->position - start_pos;
2014                                                         if( packed )
2015                                                                 key_position += new_plugin->startproject - plugin_pos;
2016                                                         clip_keyframe->position = key_position;
2017                                                         new_keyframes->append(clip_keyframe);
2018                                                 }
2019                                         }
2020                                 }
2021                         }
2022                 }
2023                 if( last_track == track ) break;
2024         }
2025         return new_edl;
2026 }
2027
2028 EDL *EDL::selected_edits_to_clip(int packed, double *start_position, Track **start_track)
2029 {
2030         return selected_edits_to_clip(packed, start_position, start_track,
2031                 session->labels_follow_edits,
2032                 session->autos_follow_edits,
2033                 session->plugins_follow_edits);
2034 }
2035
2036 void EDL::paste_edits(EDL *clip, Track *first_track, double position, int overwrite,
2037                 int edit_edits, int edit_labels, int edit_autos, int edit_plugins)
2038 {
2039         if( !first_track )
2040                 first_track = tracks->first;
2041         Track *src = clip->tracks->first;
2042         for( Track *track=first_track; track && src; track=track->next ) {
2043                 if( !track->record ) continue;
2044                 int64_t pos = track->to_units(position, 0);
2045                 if( edit_edits ) {
2046                         for( Edit *edit=src->edits->first; edit; edit=edit->next ) {
2047                                 if( edit->silence() ) continue;
2048                                 int64_t start = pos + edit->startproject;
2049                                 int64_t len = edit->length, end = start + len;
2050                                 if( overwrite )
2051                                         track->edits->clear(start, end);
2052                                 Edit *dst = track->edits->insert_new_edit(start);
2053                                 dst->copy_from(edit);
2054                                 dst->startproject = start;
2055                                 dst->is_selected = 1;
2056                                 while( (dst=dst->next) != 0 )
2057                                         dst->startproject += edit->length;
2058                                 if( overwrite ) continue;
2059                                 if( edit_labels && track == first_track ) {
2060                                         double dst_pos = track->from_units(start);
2061                                         double dst_len = track->from_units(len);
2062                                         for( Label *label=labels->first; label; label=label->next ) {
2063                                                 if( label->position >= dst_pos )
2064                                                         label->position += dst_len;
2065                                         }
2066                                 }
2067                                 if( edit_autos ) {
2068                                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2069                                                 Autos *autos = track->automation->autos[i];
2070                                                 if( !autos ) continue;
2071                                                 for( Auto *aut0=autos->first; aut0; aut0=aut0->next ) {
2072                                                         if( aut0->position >= start )
2073                                                                 aut0->position += edit->length;
2074                                                 }
2075                                         }
2076                                 }
2077                                 if( edit_plugins ) {
2078                                         for( int i=0; i<track->plugin_set.size(); ++i ) {
2079                                                 PluginSet *plugin_set = track->plugin_set[i];
2080                                                 Plugin *plugin = (Plugin *)plugin_set->first;
2081                                                 for( ; plugin; plugin=(Plugin *)plugin->next ) {
2082                                                         if( plugin->startproject >= start )
2083                                                                 plugin->startproject += edit->length;
2084                                                         else if( plugin->startproject+plugin->length > end )
2085                                                                 plugin->length += edit->length;
2086                                                         Auto *default_keyframe = plugin->keyframes->default_auto;
2087                                                         if( default_keyframe->position >= start )
2088                                                                 default_keyframe->position += edit->length;
2089                                                         KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
2090                                                         for( ; keyframe; keyframe=(KeyFrame*)keyframe->next ) {
2091                                                                 if( keyframe->position >= start )
2092                                                                         keyframe->position += edit->length;
2093                                                         }
2094                                                 }
2095                                                 plugin_set->optimize();
2096                                         }
2097                                 }
2098                         }
2099                 }
2100                 if( edit_autos ) {
2101                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2102                                 Autos *src_autos = src->automation->autos[i];
2103                                 if( !src_autos ) continue;
2104                                 Autos *autos = track->automation->autos[i];
2105                                 for( Auto *aut0=src_autos->first; aut0; aut0=aut0->next ) {
2106                                         int64_t auto_pos = pos + aut0->position;
2107                                         autos->insert_auto(auto_pos, aut0);
2108                                 }
2109                         }
2110                 }
2111                 if( edit_plugins ) {
2112                         for( int i=0; i<src->plugin_set.size(); ++i ) {
2113                                 PluginSet *plugin_set = src->plugin_set[i];
2114                                 if( !plugin_set ) continue;
2115                                 while( i >= track->plugin_set.size() )
2116                                         track->plugin_set.append(0);
2117                                 PluginSet *dst_plugin_set = track->plugin_set[i];
2118                                 if( !dst_plugin_set ) {
2119                                         dst_plugin_set = new PluginSet(this, track);
2120                                         track->plugin_set[i] = dst_plugin_set;
2121                                 }
2122                                 Plugin *plugin = (Plugin *)plugin_set->first;
2123                                 if( plugin ) track->expand_view = 1;
2124                                 for( ; plugin; plugin=(Plugin *)plugin->next ) {
2125                                         int64_t start = pos + plugin->startproject;
2126                                         int64_t end = start + plugin->length;
2127                                         if( overwrite || edit_edits )
2128                                                 dst_plugin_set->clear(start, end, 1);
2129                                         Plugin *new_plugin = dst_plugin_set->insert_plugin(plugin->title,
2130                                                 start, end-start, plugin->plugin_type, &plugin->shared_location,
2131                                                 (KeyFrame*)plugin->keyframes->default_auto, 0);
2132                                         KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
2133                                         for( ; keyframe; keyframe=(KeyFrame*)keyframe->next ) {
2134                                                 int64_t keyframe_pos = pos + keyframe->position;
2135                                                 new_plugin->keyframes->insert_auto(keyframe_pos, keyframe);
2136                                         }
2137                                         while( (new_plugin=(Plugin *)new_plugin->next) ) {
2138                                                 KeyFrame *keyframe = (KeyFrame*)new_plugin->keyframes->first;
2139                                                 for( ; keyframe; keyframe=(KeyFrame*)keyframe->next )
2140                                                         keyframe->position += plugin->length;
2141                                         }
2142                                 }
2143                         }
2144                 }
2145                 src = src->next;
2146         }
2147         if( edit_labels ) {
2148                 Label *edl_label = labels->first;
2149                 for( Label *label=clip->labels->first; label; label=label->next ) {
2150                         double label_pos = position + label->position;
2151                         int exists = 0;
2152                         while( edl_label &&
2153                                 !(exists=equivalent(edl_label->position, label_pos)) &&
2154                                 edl_label->position < position ) edl_label = edl_label->next;
2155                         if( exists ) continue;
2156                         labels->insert_before(edl_label,
2157                                 new Label(this, labels, label_pos, label->textstr));
2158                 }
2159         }
2160         optimize();
2161 }
2162
2163 void EDL::paste_edits(EDL *clip, Track *first_track, double position, int overwrite)
2164 {
2165         paste_edits(clip, first_track, position, overwrite, 1,
2166                 session->labels_follow_edits,
2167                 session->autos_follow_edits,
2168                 session->plugins_follow_edits);
2169 }
2170
2171 void EDL::replace_assets(ArrayList<Indexable*> &orig_idxbls, ArrayList<Asset*> &new_assets)
2172 {
2173         for( Track *track=tracks->first; track; track=track->next ) {
2174                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
2175                         Indexable *idxbl = (Indexable *)edit->asset;
2176                         if( !idxbl ) continue;
2177                         int i = orig_idxbls.size();
2178                         while( --i>=0 && strcmp(orig_idxbls[i]->path, idxbl->path) );
2179                         if( i < 0 ) continue;
2180                         Asset *new_asset = new_assets[i];
2181                         if( track->data_type == TRACK_VIDEO && !new_asset->video_data ) continue;
2182                         if( track->data_type == TRACK_AUDIO && !new_asset->audio_data ) continue;
2183                         edit->asset = assets->update(new_assets[i]);
2184                 }
2185         }
2186         if( !parent_edl ) {
2187                 for( int j=0,n=clips.size(); j<n; ++j )
2188                         clips[j]->replace_assets(orig_idxbls, new_assets);
2189         }
2190 }
2191