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