rework set default transtion, nested proxy edl fixes, doubleclick proxy media fix...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / pluginset.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 "edl.h"
23 #include "edlsession.h"
24 #include "filexml.h"
25 #include "keyframe.h"
26 #include "keyframes.h"
27 #include "plugin.h"
28 #include "pluginautos.h"
29 #include "pluginset.h"
30 #include "track.h"
31 #include "transportque.inc"
32
33 #include <string.h>
34
35 PluginSet::PluginSet(EDL *edl, Track *track)
36  : Edits(edl, track)
37 {
38         record = 1;
39 }
40
41 PluginSet::~PluginSet()
42 {
43         while(last) delete last;
44 }
45
46
47 PluginSet& PluginSet::operator=(PluginSet& plugins)
48 {
49 printf("PluginSet::operator= 1\n");
50         copy_from(&plugins);
51         return *this;
52 }
53
54 void PluginSet::copy_from(PluginSet *src)
55 {
56         while(last) delete last;
57         for(Plugin *current = (Plugin*)src->first; current; current = (Plugin*)NEXT)
58         {
59                 Plugin *new_plugin;
60                 append(new_plugin = (Plugin*)create_edit());
61                 new_plugin->copy_from(current);
62         }
63         this->record = src->record;
64 }
65
66 Plugin* PluginSet::get_first_plugin()
67 {
68 // Called when a new pluginset is added.
69 // Get first non-silence plugin in the plugin set.
70         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
71         {
72                 if(current && current->plugin_type != PLUGIN_NONE)
73                 {
74                         return current;
75                 }
76         }
77         return 0;
78 }
79
80 int64_t PluginSet::plugin_change_duration(int64_t input_position,
81         int64_t input_length,
82         int reverse)
83 {
84         int result = input_length;
85         Edit *current;
86
87         if(reverse)
88         {
89                 int input_start = input_position - input_length;
90                 for(current = last; current; current = PREVIOUS)
91                 {
92                         int start = current->startproject;
93                         int end = start + current->length;
94                         if(end > input_start && end < input_position)
95                         {
96                                 result = input_position - end;
97                                 return result;
98                         }
99                         else
100                         if(start > input_start && start < input_position)
101                         {
102                                 result = input_position - start;
103                                 return result;
104                         }
105                 }
106         }
107         else
108         {
109                 int input_end = input_position + input_length;
110                 for(current = first; current; current = NEXT)
111                 {
112                         int start = current->startproject;
113                         int end = start + current->length;
114                         if(start > input_position && start < input_end)
115                         {
116                                 result = start - input_position;
117                                 return result;
118                         }
119                         else
120                         if(end > input_position && end < input_end)
121                         {
122                                 result = end - input_position;
123                                 return result;
124                         }
125                 }
126         }
127         return input_length;
128 }
129
130 void PluginSet::synchronize_params(PluginSet *plugin_set)
131 {
132         for(Plugin *this_plugin = (Plugin*)first, *that_plugin = (Plugin*)plugin_set->first;
133                 this_plugin && that_plugin;
134                 this_plugin = (Plugin*)this_plugin->next, that_plugin = (Plugin*)that_plugin->next)
135         {
136                 this_plugin->synchronize_params(that_plugin);
137         }
138 }
139
140 Plugin* PluginSet::insert_plugin(const char *title,
141         int64_t unit_position,
142         int64_t unit_length,
143         int plugin_type,
144         SharedLocation *shared_location,
145         KeyFrame *default_keyframe,
146         int do_optimize)
147 {
148         Plugin *plugin = (Plugin*)create_silence(unit_position, unit_position + unit_length);
149         if(title) strcpy(plugin->title, title);
150         if(shared_location) plugin->shared_location = *shared_location;
151         plugin->plugin_type = plugin_type;
152
153         if(default_keyframe)
154                 *plugin->keyframes->default_auto = *default_keyframe;
155         plugin->keyframes->default_auto->position = unit_position;
156
157 // May delete the plugin we just added so not desirable while loading.
158         if(do_optimize) optimize();
159         return plugin;
160 }
161
162 Edit* PluginSet::create_edit()
163 {
164         Plugin* result = new Plugin(edl, this, "");
165         return result;
166 }
167
168 Edit* PluginSet::insert_edit_after(Edit *previous_edit)
169 {
170         Plugin *current = new Plugin(edl, this, "");
171         List<Edit>::insert_after(previous_edit, current);
172         return (Edit*)current;
173 }
174
175 KeyFrame *PluginSet::nearest_keyframe(int64_t pos, int dir)
176 {
177         Plugin *plugin = (Plugin*)editof(pos, dir, 0);
178         if( !plugin ) return 0;
179         KeyFrame *keyframe = (KeyFrame *)(dir == PLAY_FORWARD ?
180                 plugin->keyframes->nearest_after(pos) :
181                 plugin->keyframes->nearest_before(pos));
182         return keyframe;
183 }
184
185 int PluginSet::get_number()
186 {
187         return track->plugin_set.number_of(this);
188 }
189
190 void PluginSet::clear(int64_t start, int64_t end, int edit_autos)
191 {
192         if(edit_autos)
193         {
194 // Clear keyframes
195                 for(Plugin *current = (Plugin*)first;
196                         current;
197                         current = (Plugin*)NEXT)
198                 {
199                         current->keyframes->clear(start, end, 1);
200                 }
201         }
202
203 // Clear edits
204         Edits::clear(start, end);
205 }
206
207 //void PluginSet::clear_recursive(int64_t start, int64_t end)
208 //{
209 //printf("PluginSet::clear_recursive 1\n");
210 //      clear(start, end, 1);
211 //}
212
213 void PluginSet::shift_keyframes_recursive(int64_t position, int64_t length)
214 {
215 // Plugin keyframes are shifted in shift_effects
216 }
217
218 void PluginSet::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
219 {
220 // Effects are shifted in length extension
221 }
222
223
224 void PluginSet::clear_keyframes(int64_t start, int64_t end)
225 {
226         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
227         {
228                 current->clear_keyframes(start, end);
229         }
230 }
231
232 void PluginSet::copy_keyframes(int64_t start,
233         int64_t end,
234         FileXML *file,
235         int default_only,
236         int active_only)
237 {
238         file->tag.set_title("PLUGINSET");
239         file->append_tag();
240         file->append_newline();
241
242         for(Plugin *current = (Plugin*)first;
243                 current;
244                 current = (Plugin*)NEXT)
245         {
246                 current->copy_keyframes(start, end, file, default_only, active_only);
247         }
248
249         file->tag.set_title("/PLUGINSET");
250         file->append_tag();
251         file->append_newline();
252 }
253
254
255 void PluginSet::paste_keyframes(int64_t start,
256         int64_t length,
257         FileXML *file,
258         int default_only,
259         int active_only)
260 {
261         int result = 0;
262         int first_keyframe = 1;
263         Plugin *current;
264
265
266         while(!result)
267         {
268                 result = file->read_tag();
269
270                 if(!result)
271                 {
272                         if(file->tag.title_is("/PLUGINSET"))
273                                 result = 1;
274                         else
275                         if(file->tag.title_is("KEYFRAME"))
276                         {
277                                 int64_t position = file->tag.get_property("POSITION", 0);
278                                 if(first_keyframe && default_only)
279                                 {
280                                         position = start;
281                                 }
282                                 else
283                                 {
284                                         position += start;
285                                 }
286
287 // Get plugin owning keyframe
288                                 for(current = (Plugin*)last;
289                                         current;
290                                         current = (Plugin*)PREVIOUS)
291                                 {
292 // We want keyframes to exist beyond the end of the last plugin to
293 // make editing intuitive, but it will always be possible to
294 // paste keyframes from one plugin into an incompatible plugin.
295                                         if(position >= current->startproject)
296                                         {
297                                                 KeyFrame *keyframe = 0;
298                                                 if(file->tag.get_property("DEFAULT", 0) || default_only)
299                                                 {
300                                                         keyframe = (KeyFrame*)current->keyframes->default_auto;
301                                                 }
302                                                 else
303                                                 if(!default_only)
304                                                 {
305                                                         keyframe =
306                                                                 (KeyFrame*)current->keyframes->insert_auto(position);
307                                                 }
308
309                                                 if(keyframe)
310                                                 {
311                                                         keyframe->load(file);
312                                                         keyframe->position = position;
313                                                 }
314                                                 break;
315                                         }
316                                 }
317
318                                 first_keyframe = 0;
319                         }
320                 }
321         }
322 }
323
324 void PluginSet::shift_effects(int64_t start, int64_t length, int edit_autos)
325 {
326         for(Plugin *current = (Plugin*)first;
327                 current;
328                 current = (Plugin*)NEXT)
329         {
330 // Shift beginning of this effect
331                 if(current->startproject >= start)
332                 {
333                         current->startproject += length;
334                 }
335                 else
336 // Extend end of this effect.
337 // In loading new files, the effect should extend to fill the entire track.
338 // In muting, the effect must extend to fill the gap if another effect follows.
339 // The user should use Settings->edit effects to disable this.
340                 if(current->startproject + current->length >= start)
341                 {
342                         current->length += length;
343                 }
344
345 // Shift keyframes in this effect.
346 // If the default keyframe lands on the starting point, it must be shifted
347 // since the effect start is shifted.
348                 if(edit_autos && current->keyframes->default_auto->position >= start)
349                         current->keyframes->default_auto->position += length;
350
351                 if(edit_autos) current->keyframes->paste_silence(start, start + length);
352         }
353 }
354
355 void PluginSet::copy(int64_t start, int64_t end, FileXML *file)
356 {
357         file->tag.set_title("PLUGINSET");
358         file->tag.set_property("RECORD", record);
359         file->append_tag();
360         file->append_newline();
361
362         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
363         {
364                 current->copy(start, end, file);
365         }
366
367         file->tag.set_title("/PLUGINSET");
368         file->append_tag();
369         file->append_newline();
370 }
371
372 void PluginSet::save(FileXML *file)
373 {
374         copy(0, length(), file);
375 }
376
377 void PluginSet::load(FileXML *file, uint32_t load_flags)
378 {
379         int result = 0;
380 // Current plugin being amended
381         Plugin *plugin = (Plugin*)first;
382         int64_t startproject = 0;
383
384         record = file->tag.get_property("RECORD", record);
385         do{
386                 result = file->read_tag();
387
388
389                 if(!result)
390                 {
391                         if(file->tag.title_is("/PLUGINSET"))
392                         {
393                                 result = 1;
394                         }
395                         else
396                         if(file->tag.title_is("PLUGIN"))
397                         {
398                                 int64_t length = file->tag.get_property("LENGTH", (int64_t)0);
399                                 int plugin_type = file->tag.get_property("TYPE", 1);
400                                 char title[BCTEXTLEN];
401                                 title[0] = 0;
402                                 file->tag.get_property("TITLE", title);
403                                 Plugin::fix_plugin_title(title);
404                                 SharedLocation shared_location;
405                                 shared_location.load(file);
406
407
408                                 if(load_flags & LOAD_EDITS)
409                                 {
410                                         plugin = insert_plugin(title,
411                                                 startproject,
412                                                 length,
413                                                 plugin_type,
414                                                 &shared_location,
415                                                 0,
416                                                 0);
417                                         plugin->load(file);
418                                         startproject += length;
419                                 }
420                                 else
421                                 if(load_flags & LOAD_AUTOMATION)
422                                 {
423                                         if(plugin)
424                                         {
425                                                 plugin->load(file);
426                                                 plugin = (Plugin*)plugin->next;
427                                         }
428                                 }
429                         }
430                 }
431         } while(!result);
432
433         optimize();
434 }
435
436 int PluginSet::optimize()
437 {
438         int result = 1;
439         Plugin *current_edit;
440
441
442 // Delete keyframes out of range
443         for(current_edit = (Plugin*)first;
444                 current_edit;
445                 current_edit = (Plugin*)current_edit->next)
446         {
447                 current_edit->keyframes->default_auto->position = 0;
448                 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
449                         current_keyframe; )
450                 {
451                         KeyFrame *previous_keyframe = (KeyFrame*)current_keyframe->previous;
452                         if(current_keyframe->position >
453                                 current_edit->startproject + current_edit->length ||
454                                 current_keyframe->position < current_edit->startproject)
455                         {
456                                 delete current_keyframe;
457                         }
458                         current_keyframe = previous_keyframe;
459                 }
460         }
461
462 // Insert silence between plugins
463         for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
464         {
465                 if(current->previous)
466                 {
467                         Plugin *previous = (Plugin*)PREVIOUS;
468
469                         if(current->startproject -
470                                 previous->startproject -
471                                 previous->length > 0)
472                         {
473                                 Plugin *new_plugin = (Plugin*)create_edit();
474                                 insert_before(current, new_plugin);
475                                 new_plugin->startproject = previous->startproject +
476                                         previous->length;
477                                 new_plugin->length = current->startproject -
478                                         previous->startproject -
479                                         previous->length;
480                         }
481                 }
482                 else
483                 if(current->startproject > 0)
484                 {
485                         Plugin *new_plugin = (Plugin*)create_edit();
486                         insert_before(current, new_plugin);
487                         new_plugin->length = current->startproject;
488                 }
489         }
490
491
492 // delete 0 length plugins
493         while(result)
494         {
495                 result = 0;
496
497                 for(current_edit = (Plugin*)first; !result && current_edit; ) {
498                         Plugin* next = (Plugin*)current_edit->next;
499                         if(current_edit->length == 0) {
500                                 delete current_edit;
501                                 result = 1;
502                         }
503                         current_edit = next;
504                 }
505
506
507 // merge identical plugins with same keyframes
508                 for( current_edit = (Plugin*)first;
509                         !result && current_edit && current_edit->next; ) {
510                         Plugin *next_edit = (Plugin*)current_edit->next;
511
512 // plugins identical
513                         if(next_edit->identical(current_edit)) {
514                                 current_edit->length += next_edit->length;
515 // Merge keyframes
516                                 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
517                                         source;
518                                         source = (KeyFrame*)source->next) {
519                                         KeyFrame *dest = new KeyFrame(edl, current_edit->keyframes);
520                                         *dest = *source;
521                                         current_edit->keyframes->append(dest);
522                                 }
523                                 remove(next_edit);
524                                 result = 1;
525                                 continue;
526                         }
527
528                         current_edit = next_edit;
529
530 // delete last edit if 0 length or silence
531                         if( last && (last->silence() || !last->length) ) {
532                                 delete last;
533                                 result = 1;
534                         }
535                 }
536         }
537
538         return 0;
539 }
540
541
542
543
544
545 void PluginSet::dump(FILE *fp)
546 {
547         fprintf(fp,"   PLUGIN_SET:\n");
548         for(Plugin *current = (Plugin*)first; current; current =  (Plugin*)NEXT)
549                 current->dump(fp);
550 }