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