add new boxblur plugin, mods to videoscope, fix segv for menu btns kfrm-tweak/kfrm...
[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         if( first && pos < first->startproject )
164                 pos = first->startproject;
165         else if( last && pos > last->startproject+last->length )
166                 pos = last->startproject+last->length;
167         Plugin *plugin = (Plugin*)editof(pos, dir, 0);
168         if( !plugin ) return 0;
169         KeyFrame *keyframe = (KeyFrame *)(dir == PLAY_FORWARD ?
170                 plugin->keyframes->nearest_after(pos) :
171                 plugin->keyframes->nearest_before(pos));
172         return keyframe;
173 }
174
175 int PluginSet::get_number()
176 {
177         return track->plugin_set.number_of(this);
178 }
179
180 void PluginSet::clear(int64_t start, int64_t end, int edit_autos)
181 {
182         if(edit_autos)
183         {
184 // Clear keyframes
185                 for(Plugin *current = (Plugin*)first;
186                         current;
187                         current = (Plugin*)NEXT)
188                 {
189                         current->keyframes->clear(start, end, 1);
190                 }
191         }
192
193 // Clear edits
194         Edits::clear(start, end);
195 }
196
197 //void PluginSet::clear_recursive(int64_t start, int64_t end)
198 //{
199 //printf("PluginSet::clear_recursive 1\n");
200 //      clear(start, end, 1);
201 //}
202
203 void PluginSet::shift_keyframes_recursive(int64_t position, int64_t length)
204 {
205 // Plugin keyframes are shifted in shift_effects
206 }
207
208 void PluginSet::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
209 {
210 // Effects are shifted in length extension
211 }
212
213
214 void PluginSet::clear_keyframes(int64_t start, int64_t end)
215 {
216         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
217         {
218                 current->clear_keyframes(start, end);
219         }
220 }
221
222 void PluginSet::copy_keyframes(int64_t start,
223         int64_t end,
224         FileXML *file,
225         int default_only,
226         int active_only)
227 {
228         file->tag.set_title("PLUGINSET");
229         file->append_tag();
230         file->append_newline();
231
232         for(Plugin *current = (Plugin*)first;
233                 current;
234                 current = (Plugin*)NEXT)
235         {
236                 current->copy_keyframes(start, end, file, default_only, active_only);
237         }
238
239         file->tag.set_title("/PLUGINSET");
240         file->append_tag();
241         file->append_newline();
242 }
243
244
245 void PluginSet::paste_keyframes(int64_t start,
246         int64_t length,
247         FileXML *file,
248         int default_only,
249         int active_only)
250 {
251         int result = 0;
252         int first_keyframe = 1;
253         Plugin *current;
254
255
256         while(!result)
257         {
258                 result = file->read_tag();
259
260                 if(!result)
261                 {
262                         if(file->tag.title_is("/PLUGINSET"))
263                                 result = 1;
264                         else
265                         if(file->tag.title_is("KEYFRAME"))
266                         {
267                                 int64_t position = file->tag.get_property("POSITION", 0);
268                                 if(first_keyframe && default_only)
269                                 {
270                                         position = start;
271                                 }
272                                 else
273                                 {
274                                         position += start;
275                                 }
276
277 // Get plugin owning keyframe
278                                 for(current = (Plugin*)last;
279                                         current;
280                                         current = (Plugin*)PREVIOUS)
281                                 {
282 // We want keyframes to exist beyond the end of the last plugin to
283 // make editing intuitive, but it will always be possible to
284 // paste keyframes from one plugin into an incompatible plugin.
285                                         if(position >= current->startproject)
286                                         {
287                                                 KeyFrame *keyframe = 0;
288                                                 if(file->tag.get_property("DEFAULT", 0) || default_only)
289                                                 {
290                                                         keyframe = (KeyFrame*)current->keyframes->default_auto;
291                                                 }
292                                                 else
293                                                 if(!default_only)
294                                                 {
295                                                         keyframe =
296                                                                 (KeyFrame*)current->keyframes->insert_auto(position);
297                                                 }
298
299                                                 if(keyframe)
300                                                 {
301                                                         keyframe->load(file);
302                                                         keyframe->position = position;
303                                                 }
304                                                 break;
305                                         }
306                                 }
307
308                                 first_keyframe = 0;
309                         }
310                 }
311         }
312 }
313
314 void PluginSet::shift_effects(int64_t start, int64_t length, int edit_autos)
315 {
316         for(Plugin *current = (Plugin*)first;
317                 current;
318                 current = (Plugin*)NEXT)
319         {
320 // Shift beginning of this effect
321                 if(current->startproject >= start)
322                 {
323                         current->startproject += length;
324                 }
325                 else
326 // Extend end of this effect.
327 // In loading new files, the effect should extend to fill the entire track.
328 // In muting, the effect must extend to fill the gap if another effect follows.
329 // The user should use Settings->edit effects to disable this.
330                 if(current->startproject + current->length >= start)
331                 {
332                         current->length += length;
333                 }
334
335 // Shift keyframes in this effect.
336 // If the default keyframe lands on the starting point, it must be shifted
337 // since the effect start is shifted.
338                 if(edit_autos && current->keyframes->default_auto->position >= start)
339                         current->keyframes->default_auto->position += length;
340
341                 if(edit_autos) current->keyframes->paste_silence(start, start + length);
342         }
343 }
344
345 void PluginSet::paste_silence(int64_t start, int64_t end, int edit_autos)
346 {
347         Plugin *new_plugin = (Plugin *) insert_new_edit(start);
348         int64_t length = end - start;
349         new_plugin->length = length;
350         while( (new_plugin=(Plugin *)new_plugin->next) != 0 ) {
351                 new_plugin->startproject += length;
352                 if( !edit_autos ) continue;
353                 new_plugin->keyframes->default_auto->position += length;
354                 new_plugin->keyframes->paste_silence(start, end);
355         }
356 }
357
358 void PluginSet::copy(int64_t start, int64_t end, FileXML *file)
359 {
360         file->tag.set_title("PLUGINSET");
361         file->tag.set_property("RECORD", record);
362         file->append_tag();
363         file->append_newline();
364
365         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
366         {
367                 current->copy(start, end, file);
368         }
369
370         file->tag.set_title("/PLUGINSET");
371         file->append_tag();
372         file->append_newline();
373 }
374
375 void PluginSet::save(FileXML *file)
376 {
377         copy(0, length(), file);
378 }
379
380 void PluginSet::load(FileXML *file, uint32_t load_flags)
381 {
382         int result = 0;
383 // Current plugin being amended
384         Plugin *plugin = (Plugin*)first;
385         int64_t startproject = 0;
386
387         record = file->tag.get_property("RECORD", record);
388         do{
389                 result = file->read_tag();
390
391
392                 if(!result)
393                 {
394                         if(file->tag.title_is("/PLUGINSET"))
395                         {
396                                 result = 1;
397                         }
398                         else
399                         if(file->tag.title_is("PLUGIN"))
400                         {
401                                 int64_t length = file->tag.get_property("LENGTH", (int64_t)0);
402                                 int plugin_type = file->tag.get_property("TYPE", 1);
403                                 char title[BCTEXTLEN];
404                                 title[0] = 0;
405                                 file->tag.get_property("TITLE", title);
406                                 Plugin::fix_plugin_title(title);
407                                 SharedLocation shared_location;
408                                 shared_location.load(file);
409
410
411                                 if(load_flags & LOAD_EDITS)
412                                 {
413                                         plugin = insert_plugin(title,
414                                                 startproject,
415                                                 length,
416                                                 plugin_type,
417                                                 &shared_location,
418                                                 0,
419                                                 0);
420                                         plugin->load(file);
421                                         startproject += length;
422                                 }
423                                 else
424                                 if(load_flags & LOAD_AUTOMATION)
425                                 {
426                                         if(plugin)
427                                         {
428                                                 plugin->load(file);
429                                                 plugin = (Plugin*)plugin->next;
430                                         }
431                                 }
432                         }
433                 }
434         } while(!result);
435
436         optimize();
437 }
438
439 int PluginSet::optimize()
440 {
441         int result = 1;
442         Plugin *current_edit;
443
444 // trim plugins before position 0
445         while( first && first->startproject+first->length < 0 )
446                 delete first;
447         if( first && first->startproject < 0 ) {
448                 first->length += first->startproject;
449                 first->startproject = 0;
450         }
451
452 // Delete keyframes out of range
453         for(current_edit = (Plugin*)first;
454                 current_edit;
455                 current_edit = (Plugin*)current_edit->next)
456         {
457                 current_edit->keyframes->default_auto->position = 0;
458                 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
459                         current_keyframe; )
460                 {
461                         KeyFrame *previous_keyframe = (KeyFrame*)current_keyframe->previous;
462                         if(current_keyframe->position >
463                                 current_edit->startproject + current_edit->length ||
464                                 current_keyframe->position < current_edit->startproject)
465                         {
466                                 delete current_keyframe;
467                         }
468                         current_keyframe = previous_keyframe;
469                 }
470         }
471
472 // Insert silence between plugins
473         for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
474         {
475                 if(current->previous)
476                 {
477                         Plugin *previous = (Plugin*)PREVIOUS;
478
479                         if(current->startproject -
480                                 previous->startproject -
481                                 previous->length > 0)
482                         {
483                                 Plugin *new_plugin = (Plugin*)create_edit();
484                                 insert_before(current, new_plugin);
485                                 new_plugin->startproject = previous->startproject +
486                                         previous->length;
487                                 new_plugin->length = current->startproject -
488                                         previous->startproject -
489                                         previous->length;
490                         }
491                 }
492                 else
493                 if(current->startproject > 0)
494                 {
495                         Plugin *new_plugin = (Plugin*)create_edit();
496                         insert_before(current, new_plugin);
497                         new_plugin->length = current->startproject;
498                 }
499         }
500
501
502 // delete 0 length plugins
503         while(result)
504         {
505                 result = 0;
506
507                 for(current_edit = (Plugin*)first; !result && current_edit; ) {
508                         Plugin* next = (Plugin*)current_edit->next;
509                         if(current_edit->length == 0) {
510                                 delete current_edit;
511                                 result = 1;
512                         }
513                         current_edit = next;
514                 }
515
516
517 // merge identical plugins with same keyframes
518                 for( current_edit = (Plugin*)first;
519                         !result && current_edit && current_edit->next; ) {
520                         Plugin *next_edit = (Plugin*)current_edit->next;
521
522 // plugins identical
523                         if(next_edit->identical(current_edit)) {
524                                 current_edit->length += next_edit->length;
525 // Merge keyframes
526                                 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
527                                         source;
528                                         source = (KeyFrame*)source->next) {
529                                         KeyFrame *dest = new KeyFrame(edl, current_edit->keyframes);
530                                         dest->copy_from(source);
531                                         current_edit->keyframes->append(dest);
532                                 }
533                                 remove(next_edit);
534                                 result = 1;
535                                 continue;
536                         }
537
538                         current_edit = next_edit;
539
540 // delete last edit if 0 length or silence
541                         if( last && (last->silence() || !last->length) ) {
542                                 delete last;
543                                 result = 1;
544                         }
545                 }
546         }
547
548         return 0;
549 }
550
551
552
553
554
555 void PluginSet::dump(FILE *fp)
556 {
557         fprintf(fp,"   PLUGIN_SET:\n");
558         for(Plugin *current = (Plugin*)first; current; current =  (Plugin*)NEXT)
559                 current->dump(fp);
560 }