fix awdw solo vicon crash, fix nested clip for binfolders, open edit edl
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / edl.C
index 4b6ca03a4ee8127eb33030038c32455941fc91ed..130f80bb97c4557b1a26ff1b8587d0db21ef0e72 100644 (file)
@@ -34,6 +34,7 @@
 #include "edits.h"
 #include "edl.h"
 #include "edlsession.h"
+#include "file.h"
 #include "filexml.h"
 #include "floatauto.h"
 #include "floatautos.h"
@@ -211,7 +212,7 @@ int EDL::read_xml(FileXML *file, uint32_t load_flags)
                                file->tag.title_is("/CLIP_EDL") ||
                                file->tag.title_is("/NESTED_EDL") ||
                                file->tag.title_is("/VWINDOW_EDL") ) {
-                               result = 1;
+                               break;
                        }
                        else
                        if( file->tag.title_is("CLIPBOARD") ) {
@@ -600,7 +601,7 @@ void EDL::copy_indexables(EDL *edl)
        }
 }
 
-EDL *EDL::new_nested(EDL *edl, const char *path)
+EDL *EDL::new_nested_edl(EDL *edl, const char *path)
 {
        EDL *nested = new EDL;  // no parent for nested edl
        nested->create_objects();
@@ -613,6 +614,22 @@ EDL *EDL::new_nested(EDL *edl, const char *path)
        return nested;
 }
 
+EDL *EDL::get_nested_edl()
+{
+       Track *track = tracks->first;
+       Edit *edit = track ? track->edits->first : 0;
+       EDL *nested = edit && !edit->next && !edit->asset ? edit->nested_edl : 0;
+       while( nested && (track=track->next)!=0 ) {
+               Edit *edit = track->edits->first;
+               if( !edit || edit->next ||
+                   ( edit->nested_edl != nested &&
+                     strcmp(edit->nested_edl->path, nested->path) ) )
+                       nested = 0;
+       }
+       return nested;
+}
+
+
 EDL *EDL::create_nested_clip(EDL *nested)
 {
        EDL *new_edl = new EDL(this);  // parent for clip edl
@@ -623,14 +640,51 @@ EDL *EDL::create_nested_clip(EDL *nested)
 
 void EDL::create_nested(EDL *nested)
 {
+       int video_tracks = 0, audio_tracks = 0;
+       for( Track *track=nested->tracks->first; track!=0; track=track->next ) {
+               if( track->data_type == TRACK_VIDEO && track->record ) ++video_tracks;
+               if( track->data_type == TRACK_AUDIO && track->record ) ++audio_tracks;
+       }
+// renderengine properties
+       if( video_tracks > 0 )
+               video_tracks = 1;
+       if( audio_tracks > 0 )
+               audio_tracks = nested->session->audio_channels;
+       
 // Keep frame rate, sample rate, and output size unchanged.
 // Nest all video & audio outputs
+       session->video_channels = video_tracks;
+       session->audio_channels = audio_tracks;
        session->video_tracks = 1;
-       session->audio_tracks = nested->session->audio_channels;
+       session->audio_tracks = audio_tracks;
        create_default_tracks();
        insert_asset(0, nested, 0, 0, 0);
 }
 
+void EDL::overwrite_clip(EDL *clip)
+{
+       int folder = folder_no;
+       char clip_title[BCTEXTLEN], clip_notes[BCTEXTLEN], clip_icon[BCSTRLEN];
+       if( parent_edl ) {
+               strcpy(clip_title, local_session->clip_title);
+               strcpy(clip_notes, local_session->clip_notes);
+               strcpy(clip_icon,  local_session->clip_icon);
+       }
+       copy_all(clip);
+       folder_no = folder;
+       if( parent_edl ) {
+               strcpy(local_session->clip_title, clip_title);
+               strcpy(local_session->clip_notes, clip_notes);
+               strcpy(local_session->clip_icon, clip_icon);
+       }
+       if( !clip_icon[0] ) return;
+// discard old clip icon to reconstruct 
+       char clip_icon_path[BCTEXTLEN];
+       snprintf(clip_icon_path, sizeof(clip_icon_path),
+               "%s/%s", File::get_config_path(), clip_icon);
+       remove(clip_icon_path);
+}
+
 void EDL::retrack()
 {
        int min_w = session->output_w, min_h = session->output_h;
@@ -1158,7 +1212,7 @@ void EDL::insert_asset(Asset *asset,
 
        if( new_nested_edl ) {
                length = new_nested_edl->tracks->total_length();
-               layers = 1;
+               layers = new_nested_edl->session->video_channels;
                channels = new_nested_edl->session->audio_channels;
        }
 
@@ -1871,7 +1925,7 @@ EDL *EDL::selected_edits_to_clip(int packed,
                        }
                        int64_t clip_start_pos = startproject;
                        Edit *clip_edit = new Edit(new_edl, new_track);
-                       clip_edit->copy_from(edit);
+                       clip_edit->clone_from(edit);
                        clip_edit->startproject = startproject;
                        startproject += clip_edit->length;
                        new_track->edits->append(clip_edit);
@@ -2001,7 +2055,7 @@ void EDL::paste_edits(EDL *clip, Track *first_track, double position, int overwr
                                if( overwrite )
                                        track->edits->clear(start, end);
                                Edit *dst = track->edits->insert_new_edit(start);
-                               dst->copy_from(edit);
+                               dst->clone_from(edit);
                                dst->startproject = start;
                                dst->is_selected = 1;
                                while( (dst=dst->next) != 0 )
@@ -2080,6 +2134,7 @@ void EDL::paste_edits(EDL *clip, Track *first_track, double position, int overwr
                                        Plugin *new_plugin = dst_plugin_set->insert_plugin(plugin->title,
                                                start, end-start, plugin->plugin_type, &plugin->shared_location,
                                                (KeyFrame*)plugin->keyframes->default_auto, 0);
+                                       new_plugin->on = plugin->on;
                                        KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
                                        for( ; keyframe; keyframe=(KeyFrame*)keyframe->next ) {
                                                int64_t keyframe_pos = pos + keyframe->position;
@@ -2128,7 +2183,10 @@ void EDL::replace_assets(ArrayList<Indexable*> &orig_idxbls, ArrayList<Asset*> &
                        int i = orig_idxbls.size();
                        while( --i>=0 && strcmp(orig_idxbls[i]->path, idxbl->path) );
                        if( i < 0 ) continue;
-                       edit->asset = assets->update((Asset*)new_assets[i]);
+                       Asset *new_asset = new_assets[i];
+                       if( track->data_type == TRACK_VIDEO && !new_asset->video_data ) continue;
+                       if( track->data_type == TRACK_AUDIO && !new_asset->audio_data ) continue;
+                       edit->asset = assets->update(new_assets[i]);
                }
        }
        if( !parent_edl ) {
@@ -2137,3 +2195,162 @@ void EDL::replace_assets(ArrayList<Indexable*> &orig_idxbls, ArrayList<Asset*> &
        }
 }
 
+
+int EDL::collect_effects(EDL *&group)
+{
+// to remap shared plugins in copied plugin stack
+       edl_shared_list shared_map;
+       int ret = 0;
+       EDL *new_edl = new EDL(parent_edl ? parent_edl : this);
+       new_edl->create_objects();
+       Tracks *new_tracks = new_edl->tracks;
+       Track *track = tracks->first;
+       for( ; !ret && track; track=track->next ) {
+               if( track->data_type != TRACK_AUDIO &&
+                   track->data_type != TRACK_VIDEO ) continue;
+               Edit *edit = track->edits->first;
+               while( edit && !edit->is_selected ) edit = edit->next;
+               if( !edit ) continue;
+               if( !track->record ) { ret = COLLECT_EFFECTS_RECORD;  break; } 
+               Track *new_track = 0;
+               edl_shared *location = 0;
+               int64_t start_pos = edit->startproject;
+               int64_t end_pos = start_pos + edit->length;
+               int pluginsets = track->plugin_set.size();
+               for( int i=0; i<pluginsets; ++i ) {
+                       PluginSet *plugins = track->plugin_set[i];
+                       Plugin *plugin = (Plugin *)plugins->first;
+                       for( ; plugin; plugin=(Plugin *)plugin->next ) {
+                               if( plugin->silence() ) continue;
+                               if( start_pos < plugin->startproject+plugin->length ) break;
+                       }
+                       if( !plugin || plugin->startproject >= end_pos ) continue;
+                       if( !location ) {
+                               location = &shared_map.append();
+                               location->trk = tracks->number_of(track);
+                       }
+                       location->append(i);
+                       if( !new_track )
+                               new_track = track->data_type == TRACK_AUDIO ?
+                                       new_tracks->add_audio_track(0, 0) :
+                                       new_tracks->add_video_track(0, 0) ;
+                       PluginSet *new_plugins = new PluginSet(new_edl, new_track);
+                       new_track->plugin_set.append(new_plugins);
+                       Plugin *new_plugin = (Plugin *)new_plugins->create_edit();
+                       new_plugin->copy_base(plugin);
+                       new_plugins->append(new_plugin);
+                       KeyFrame *keyframe = plugin->keyframes->
+                               get_prev_keyframe(start_pos, PLAY_FORWARD);
+                       KeyFrame *default_auto = (KeyFrame *)
+                               new_plugin->keyframes->default_auto;
+                       default_auto->copy_from(keyframe);
+                       default_auto->position = 0;
+                       default_auto->is_default = 1;
+               }
+               if( !new_track ) { ret = COLLECT_EFFECTS_MISSING;  break; }
+               while( (edit=edit->next) && !edit->is_selected );
+               if( edit ) ret = COLLECT_EFFECTS_MULTIPLE;
+       }
+       track = new_edl->tracks->first;
+       if( !ret && !track ) ret = COLLECT_EFFECTS_EMPTY;
+// remap shared plugins in copied new_edl
+       for( ; !ret && track; track=track->next ) {
+               int pluginsets = track->plugin_set.size();
+               for( int i=0; i<pluginsets; ++i ) {
+                       PluginSet *plugins = track->plugin_set[i];
+                       Plugin *plugin = (Plugin *)plugins->first;
+                       for( ; plugin; plugin=(Plugin *)plugin->next ) {
+                               if( plugin->plugin_type != PLUGIN_SHAREDPLUGIN ) continue;
+                               int trk = plugin->shared_location.module;
+                               int set = plugin->shared_location.plugin;
+                               int m = shared_map.size(), n = -1;
+                               while( --m>=0 && shared_map[m].trk!=trk );
+                               if( m >= 0 ) {
+                                       edl_shared &location = shared_map[m];
+                                       n = location.size();
+                                       while( --n>=0 && location[n]!=set );
+                               }
+                               if( n < 0 ) { ret = COLLECT_EFFECTS_MASTER;  break; }
+                               plugin->shared_location.module = m;
+                               plugin->shared_location.plugin = n;
+                       }
+               }
+       }
+       if( !ret )
+               group = new_edl;
+       else
+               new_edl->remove_user();
+       return ret;
+}
+
+void edl_SharedLocations::add(int trk, int plg)
+{
+       SharedLocation &s = append();
+       s.module = trk;  s.plugin = plg;
+}
+
+// inserts pluginsets in group to first selected edit in tracks
+int EDL::insert_effects(EDL *group, Track *first_track)
+{
+       edl_SharedLocations edl_locs, new_locs;
+       Track *new_track = group->tracks->first;
+       if( !first_track ) first_track = tracks->first;
+       Track *track = first_track;
+       for( ; track && new_track; track=track->next ) {
+               Edit *edit = track->edits->first;
+               while( edit && !edit->is_selected ) edit = edit->next;
+               if( !edit ) continue;
+               if( !track->record ) return INSERT_EFFECTS_RECORD;
+               if( track->data_type != new_track->data_type ) return INSERT_EFFECTS_TYPE;
+               int gtrk = group->tracks->number_of(new_track);
+               int trk = tracks->number_of(track);
+               int psz = track->plugin_set.size();
+               int new_pluginsets = new_track->plugin_set.size();
+               for( int i=0; i<new_pluginsets; ++psz, ++i ) {
+                       new_locs.add(gtrk, i);
+                       edl_locs.add(trk, psz);
+               }
+               while( (edit=edit->next) && !edit->is_selected );
+               if( edit ) return INSERT_EFFECTS_MULTIPLE;
+               new_track = new_track->next;
+       }
+       if( new_track ) return INSERT_EFFECTS_MISSING;
+       for( ; track; track=track->next ) {
+               Edit *edit = track->edits->first;
+               while( edit && !edit->is_selected ) edit = edit->next;
+               if( edit ) return INSERT_EFFECTS_EXTRA;
+       }
+       new_track = group->tracks->first;
+       track = first_track;
+       for( ; track && new_track; track=track->next ) {
+               if( !track->record ) continue;
+               Edit *edit = track->edits->first;
+               while( edit && !edit->is_selected ) edit = edit->next;
+               if( !edit ) continue;
+               int64_t start_pos = edit->startproject;
+               int64_t end_pos = start_pos + edit->length;
+               int new_pluginsets = new_track->plugin_set.size();
+               for( int i=0; i<new_pluginsets; ++i ) {
+                       Plugin *new_plugin = (Plugin *)new_track->plugin_set[i]->first;
+                       if( !new_plugin ) continue;
+                       PluginSet *plugins = new PluginSet(this, track);
+                       track->plugin_set.append(plugins);
+                       SharedLocation shared_location;
+                       if( new_plugin->plugin_type == PLUGIN_SHAREDPLUGIN ) {
+                               SharedLocation &new_loc = new_plugin->shared_location;
+                               int k = new_locs.size();
+                               while( --k>=0 && !new_loc.equivalent(&new_locs[k]) );
+                               if( k < 0 ) return INSERT_EFFECTS_MASTER;
+                               shared_location.copy_from(&edl_locs[k]);
+                       }
+                       KeyFrame *default_keyframe = (KeyFrame *)new_plugin->keyframes->default_auto;
+                       plugins->insert_plugin(new_plugin->title,
+                               start_pos, end_pos - start_pos, new_plugin->plugin_type,
+                               &shared_location, default_keyframe, 0);
+                       track->expand_view = 1;
+               }
+               new_track = new_track->next;
+       }
+       return 0;
+}
+