r105: This commit was manufactured by cvs2svn to create tag
[cinelerra_cv/mob.git] / hvirtual / cinelerra / pluginset.C
blobf3ed7f40dc0973d32cd2e310413cabf0f7179439
1 #include "edl.h"
2 #include "edlsession.h"
3 #include "filexml.h"
4 #include "keyframe.h"
5 #include "keyframes.h"
6 #include "plugin.h"
7 #include "pluginautos.h"
8 #include "pluginset.h"
9 #include "track.h"
11 #include <string.h>
13 PluginSet::PluginSet(EDL *edl, Track *track)
14  : Edits(edl, track)
16         record = 1;
19 PluginSet::~PluginSet()
21         while(last) delete last;
25 PluginSet& PluginSet::operator=(PluginSet& plugins)
27 //printf("PluginSet::operator= : you should call copy_from instead, to allow conditional copies.\n");
28         copy_from(&plugins);
29         return *this;
32 void PluginSet::copy_from(PluginSet *src)
34         while(last) delete last;
35         for(Plugin *current = (Plugin*)src->first; current; current = (Plugin*)NEXT)
36         {
37                 Plugin *new_plugin;
38                 append(new_plugin = (Plugin*)create_edit());
39                 new_plugin->copy_from(current);
40         }
41         this->record = src->record;
44 Plugin* PluginSet::get_first_plugin()
46 // Called when a new pluginset is added.
47 // Get first non-silence plugin in the plugin set.
48         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
49         {
50                 if(current && current->plugin_type != PLUGIN_NONE)
51                 {
52                         return current;
53                 }
54         }
55         return 0;
58 int64_t PluginSet::plugin_change_duration(int64_t input_position, 
59         int64_t input_length, 
60         int reverse)
62         int result = input_length;
63         Edit *current;
65         if(reverse)
66         {
67                 int input_start = input_position - input_length;
68                 for(current = last; current; current = PREVIOUS)
69                 {
70                         int start = current->startproject;
71                         int end = start + current->length;
72                         if(end > input_start && end < input_position)
73                         {
74                                 result = input_position - end;
75                                 return result;
76                         }
77                         else
78                         if(start > input_start && start < input_position)
79                         {
80                                 result = input_position - start;
81                                 return result;
82                         }
83                 }
84         }
85         else
86         {
87                 int input_end = input_position + input_length;
88                 for(current = first; current; current = NEXT)
89                 {
90                         int start = current->startproject;
91                         int end = start + current->length;
92                         if(start > input_position && start < input_end)
93                         {
94                                 result = start - input_position;
95                                 return result;
96                         }
97                         else
98                         if(end > input_position && end < input_end)
99                         {
100                                 result = end - input_position;
101                                 return result;
102                         }
103                 }
104         }
105         return input_length;
108 void PluginSet::synchronize_params(PluginSet *plugin_set)
110         for(Plugin *this_plugin = (Plugin*)first, *that_plugin = (Plugin*)plugin_set->first;
111                 this_plugin && that_plugin;
112                 this_plugin = (Plugin*)this_plugin->next, that_plugin = (Plugin*)that_plugin->next)
113         {
114                 this_plugin->synchronize_params(that_plugin);
115         }
118 Plugin* PluginSet::insert_plugin(char *title, 
119         int64_t unit_position, 
120         int64_t unit_length,
121         int plugin_type,
122         SharedLocation *shared_location,
123         KeyFrame *default_keyframe,
124         int do_optimize)
126         Plugin *plugin = (Plugin*)paste_silence(unit_position, 
127                 unit_position + unit_length);
130         if(title) strcpy(plugin->title, title);
132         if(shared_location) plugin->shared_location = *shared_location;
134         plugin->plugin_type = plugin_type;
136         if(default_keyframe) 
137                 *plugin->keyframes->default_auto = *default_keyframe;
138         plugin->keyframes->default_auto->position = unit_position;
140 // May delete the plugin we just added so not desirable while loading.
141         if(do_optimize) optimize();
142         return plugin;
145 Edit* PluginSet::create_edit()
147         Plugin* result = new Plugin(edl, this, "");
148         return result;
151 Edit* PluginSet::insert_edit_after(Edit *previous_edit)
153         Plugin *current = new Plugin(edl, this, "");
154         List<Edit>::insert_after(previous_edit, current);
155         return (Edit*)current;
159 int PluginSet::get_number()
161         return track->plugin_set.number_of(this);
164 void PluginSet::clear(int64_t start, int64_t end)
166 // Clear keyframes
167         for(Plugin *current = (Plugin*)first;
168                 current;
169                 current = (Plugin*)NEXT)
170         {
171                 current->keyframes->clear(start, end, 1);
172         }
174 // Clear edits
175         Edits::clear(start, end);
178 void PluginSet::clear_recursive(int64_t start, int64_t end)
180 //printf("PluginSet::clear_recursive 1\n");
181         clear(start, end);
184 void PluginSet::shift_keyframes_recursive(int64_t position, int64_t length)
186 // Plugin keyframes are shifted in shift_effects
189 void PluginSet::shift_effects_recursive(int64_t position, int64_t length)
191 // Effects are shifted in length extension
192 //      shift_effects(position, length);
196 void PluginSet::clear_keyframes(int64_t start, int64_t end)
198         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
199         {
200                 current->clear_keyframes(start, end);
201         }
204 void PluginSet::copy_keyframes(int64_t start, 
205         int64_t end, 
206         FileXML *file, 
207         int default_only,
208         int autos_only)
210         file->tag.set_title("PLUGINSET");       
211         file->append_tag();
212         file->append_newline();
214         for(Plugin *current = (Plugin*)first; 
215                 current; 
216                 current = (Plugin*)NEXT)
217         {
218                 current->copy_keyframes(start, end, file, default_only, autos_only);
219         }
221         file->tag.set_title("/PLUGINSET");      
222         file->append_tag();
223         file->append_newline();
226 void PluginSet::paste_keyframes(int64_t start, 
227         int64_t length, 
228         FileXML *file, 
229         int default_only)
231         int result = 0;
232         Plugin *current;
233         
234         while(!result)
235         {
236                 result = file->read_tag();
238                 if(!result)
239                 {
240                         if(file->tag.title_is("/PLUGINSET"))
241                                 result = 1;
242                         else
243                         if(file->tag.title_is("KEYFRAME"))
244                         {
245                                 int64_t position = file->tag.get_property("POSITION", 0);
246                                 position += start;
248 // Get plugin owning keyframe
249                                 for(current = (Plugin*)last; 
250                                         current;
251                                         current = (Plugin*)PREVIOUS)
252                                 {
253 // We want keyframes to exist beyond the end of the last plugin to
254 // make editing intuitive, but it will always be possible to 
255 // paste keyframes from one plugin into an incompatible plugin.
256                                         if(position >= current->startproject)
257                                         {
258                                                 KeyFrame *keyframe;
259                                                 if(file->tag.get_property("DEFAULT", 0) || default_only)
260                                                 {
261                                                         keyframe = (KeyFrame*)current->keyframes->default_auto;
262                                                 }
263                                                 else
264                                                 {
265                                                         keyframe = 
266                                                                 (KeyFrame*)current->keyframes->insert_auto(position);
267                                                 }
268                                                 keyframe->load(file);
269                                                 keyframe->position = position;
270                                                 break;
271                                         }
272                                 }
274                         }
275                 }
276         }
279 void PluginSet::shift_effects(int64_t start, int64_t length)
281         for(Plugin *current = (Plugin*)first;
282                 current;
283                 current = (Plugin*)NEXT)
284         {
285 // Shift beginning of this effect
286                 if(current->startproject >= start)
287                 {
288                         current->startproject += length;
289                 }
290                 else
291 // Extend end of this effect
292                 if(current->startproject + current->length >= start)
293                 {
294                         current->length += length;
295                 }
297 // Shift keyframes in this effect
298                 if(current->keyframes->default_auto->position >= start)
299                         current->keyframes->default_auto->position += length;
300                 current->keyframes->paste_silence(start, start + length);
301         }
304 void PluginSet::copy(int64_t start, int64_t end, FileXML *file)
306         file->tag.set_title("PLUGINSET");       
307         file->tag.set_property("RECORD", record);
308         file->append_tag();
309         file->append_newline();
311         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
312         {
313                 current->copy(start, end, file);
314         }
316         file->tag.set_title("/PLUGINSET");      
317         file->append_tag();
318         file->append_newline();
321 void PluginSet::save(FileXML *file)
323         copy(0, length(), file);
326 void PluginSet::load(FileXML *file, uint32_t load_flags)
328         int result = 0;
329 // Current plugin being amended
330         Plugin *plugin = (Plugin*)first;
331         int64_t startproject = 0;
333         record = file->tag.get_property("RECORD", record);
334         do{
335                 result = file->read_tag();
338                 if(!result)
339                 {
340                         if(file->tag.title_is("/PLUGINSET"))
341                         {
342                                 result = 1;
343                         }
344                         else
345                         if(file->tag.title_is("PLUGIN"))
346                         {
347                                 int64_t length = file->tag.get_property("LENGTH", (int64_t)0);
348                                 int plugin_type = file->tag.get_property("TYPE", 1);
349                                 char title[BCTEXTLEN];
350                                 title[0] = 0;
351                                 file->tag.get_property("TITLE", title);
352                                 SharedLocation shared_location;
353                                 shared_location.load(file);
356                                 if(load_flags & LOAD_EDITS)
357                                 {
358                                         plugin = insert_plugin(title, 
359                                                 startproject, 
360                                                 length,
361                                                 plugin_type,
362                                                 &shared_location,
363                                                 0,
364                                                 0);
365                                         plugin->load(file);
366                                         startproject += length;
367                                 }
368                                 else
369                                 if(load_flags & LOAD_AUTOMATION)
370                                 {
371                                         if(plugin)
372                                         {
373                                                 plugin->load(file);
374                                                 plugin = (Plugin*)plugin->next;
375                                         }
376                                 }
377                         }
378                 }
379         }while(!result);
384 int PluginSet::optimize()
386         int result = 1;
387         Plugin *current_edit;
389 // Delete keyframes out of range
390         for(current_edit = (Plugin*)first;
391                 current_edit; 
392                 current_edit = (Plugin*)current_edit->next)
393         {
394                 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
395                         current_keyframe; )
396                 {
397                         KeyFrame *previous_keyframe = (KeyFrame*)current_keyframe->previous;
398                         if(current_keyframe->position >= 
399                                 current_edit->startproject + current_edit->length ||
400                                 current_keyframe->position < current_edit->startproject)
401                         {
402                                 delete current_keyframe;
403                         }
404                         current_keyframe = previous_keyframe;
405                 }
406         }
408 // Insert silence between plugins
409         for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
410         {
411                 if(current->previous)
412                 {
413                         Plugin *previous = (Plugin*)PREVIOUS;
415                         if(current->startproject - 
416                                 previous->startproject - 
417                                 previous->length > 0)
418                         {
419                                 Plugin *new_plugin = (Plugin*)create_edit();
420                                 insert_before(current, new_plugin);
421                                 new_plugin->startproject = previous->startproject + 
422                                         previous->length;
423                                 new_plugin->length = current->startproject - 
424                                         previous->startproject - 
425                                         previous->length;
426                         }
427                 }
428                 else
429                 if(current->startproject > 0)
430                 {
431                         Plugin *new_plugin = (Plugin*)create_edit();
432                         insert_before(current, new_plugin);
433                         new_plugin->length = current->startproject;
434                 }
435         }
438 // delete 0 length plugins
439         while(result)
440         {
441                 result = 0;
443                 for(current_edit = (Plugin*)first; 
444                         current_edit && !result; )
445                 {
446                         if(current_edit->length == 0)
447                         {
448                                 Plugin* next = (Plugin*)current_edit->next;
449                                 delete current_edit;
450                                 result = 1;
451                                 current_edit = next;
452                         }
453                         else
454                                 current_edit = (Plugin*)current_edit->next;
455                 }
458 // merge identical plugins with same keyframes
459                 for(current_edit = (Plugin*)first; 
460                         current_edit && current_edit->next && !result; )
461                 {
462                         Plugin *next_edit = (Plugin*)current_edit->next;
465 // plugins identical
466                         if(next_edit->identical(current_edit))
467                 {
468                         current_edit->length += next_edit->length;
469 // Merge keyframes
470                                 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
471                                         source;
472                                         source = (KeyFrame*)source->next)
473                                 {
474                                         KeyFrame *dest = new KeyFrame(edl, current_edit->keyframes);
475                                         *dest = *source;
476                                         current_edit->keyframes->append(dest);
477                                 }
478                         remove(next_edit);
479                         result = 1;
480                 }
482                 current_edit = (Plugin*)current_edit->next;
483                 }
485 // delete last edit if 0 length or silence
486                 if(last)
487                 {
488                         if(last->silence() || !last->length)
489                         {
490                                 delete last;
491                                 result = 1;
492                         }
493                 }
494         }
496         return 0;
503 void PluginSet::dump()
505         printf("   PLUGIN_SET:\n");
506         for(Plugin *current = (Plugin*)first; current; current =  (Plugin*)NEXT)
507                 current->dump();