]> git.sesse.net Git - mlt/blob - src/modules/jackrack/plugin.c
jack_rack_add_plugin(): handle null pointer
[mlt] / src / modules / jackrack / plugin.c
1 /*
2  * JACK Rack
3  *
4  * Original:
5  * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
6  *
7  * Modification for MLT:
8  * Copyright (C) 2004 Ushodaya Enterprises Limited
9  * Author: Dan Dennedy <dan@dennedy.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ladspa.h>
29 #include <dlfcn.h>
30 #include <ctype.h>
31
32 #include <glib.h>
33
34 #include "plugin.h"
35 #include "jack_rack.h"
36 #include "process.h"
37 #include "framework/mlt_log.h"
38
39 #define CONTROL_FIFO_SIZE   128
40
41
42
43 /* swap over the jack ports in two plugins */
44 static void
45 plugin_swap_aux_ports (plugin_t * plugin, plugin_t * other)
46 {
47   guint copy;
48   jack_port_t ** aux_ports_tmp;
49   
50   for (copy = 0; copy < plugin->copies; copy++)
51     {
52       aux_ports_tmp = other->holders[copy].aux_ports;
53       other->holders[copy].aux_ports = plugin->holders[copy].aux_ports;
54       plugin->holders[copy].aux_ports = aux_ports_tmp;
55     }
56 }
57
58 /** connect up the ladspa instance's input buffers to the previous
59     plugin's audio memory.  make sure to check that plugin->prev
60     exists. */
61 void
62 plugin_connect_input_ports (plugin_t * plugin, LADSPA_Data ** inputs)
63 {
64   gint copy;
65   unsigned long channel;
66   unsigned long rack_channel;
67
68   if (!plugin || !inputs)
69     return;
70
71   rack_channel = 0;
72   for (copy = 0; copy < plugin->copies; copy++)
73     {
74       for (channel = 0; channel < plugin->desc->channels; channel++)
75         {
76           plugin->descriptor->
77             connect_port (plugin->holders[copy].instance,
78                           plugin->desc->audio_input_port_indicies[channel],
79                           inputs[rack_channel]);
80           rack_channel++;
81         }
82     }
83   
84   plugin->audio_input_memory = inputs;
85 }
86
87 /** connect up a plugin's output ports to its own audio_output_memory output memory */
88 void
89 plugin_connect_output_ports (plugin_t * plugin)
90 {
91   gint copy;
92   unsigned long channel;
93   unsigned long rack_channel = 0;
94
95   if (!plugin)
96     return;
97
98
99   for (copy = 0; copy < plugin->copies; copy++)
100     {
101       for (channel = 0; channel < plugin->desc->channels; channel++)
102         {
103           plugin->descriptor->
104             connect_port (plugin->holders[copy].instance,
105                           plugin->desc->audio_output_port_indicies[channel],
106                           plugin->audio_output_memory[rack_channel]);
107           rack_channel++;
108         }
109     }
110 }
111
112 void
113 process_add_plugin (process_info_t * procinfo, plugin_t * plugin)
114 {
115
116   /* sort out list pointers */
117   plugin->next = NULL;
118   plugin->prev = procinfo->chain_end;
119
120   if (procinfo->chain_end)
121     procinfo->chain_end->next = plugin;
122   else
123     procinfo->chain = plugin;
124
125   procinfo->chain_end = plugin;
126
127 }
128
129
130 /** remove a plugin from the chain */
131 plugin_t *
132 process_remove_plugin (process_info_t * procinfo, plugin_t *plugin)
133 {
134   /* sort out chain pointers */
135   if (plugin->prev)
136     plugin->prev->next = plugin->next;
137   else
138     procinfo->chain = plugin->next;
139
140   if (plugin->next)
141     plugin->next->prev = plugin->prev;
142   else
143     procinfo->chain_end = plugin->prev;
144     
145   /* sort out the aux ports */
146   if (procinfo->jack_client && plugin->desc->aux_channels > 0)
147     {
148       plugin_t * other;
149       
150       for (other = plugin->next; other; other = other->next)
151         if (other->desc->id == plugin->desc->id)
152           plugin_swap_aux_ports (plugin, other);
153     }
154
155   return plugin;
156 }
157
158 /** enable/disable a plugin */
159 void
160 process_ablise_plugin (process_info_t * procinfo, plugin_t *plugin, gboolean enable)
161 {
162   plugin->enabled = enable;
163 }
164
165 /** enable/disable a plugin */
166 void
167 process_ablise_plugin_wet_dry (process_info_t * procinfo, plugin_t *plugin, gboolean enable)
168 {
169   plugin->wet_dry_enabled = enable;
170 }
171
172 /** move a plugin up or down one place in the chain */
173 void
174 process_move_plugin (process_info_t * procinfo, plugin_t *plugin, gint up)
175 {
176   /* other plugins in the chain */
177   plugin_t *pp = NULL, *p, *n, *nn = NULL;
178
179   /* note that we should never recieve an illogical move request
180      ie, there will always be at least 1 plugin before for an up
181      request or 1 plugin after for a down request */
182
183   /* these are pointers to the plugins surrounding the specified one:
184      { pp, p, plugin, n, nn } which makes things much clearer than
185      tptr, tptr2 etc */
186   p = plugin->prev;
187   if (p) pp = p->prev;
188   n = plugin->next;
189   if (n) nn = n->next;
190
191   if (up)
192     {
193       if (!p)
194         return;
195
196       if (pp)
197         pp->next = plugin;
198       else
199         procinfo->chain = plugin;
200
201       p->next = n;
202       p->prev = plugin;
203
204       plugin->prev = pp;
205       plugin->next = p;
206
207       if (n)
208         n->prev = p;
209       else
210         procinfo->chain_end = p;
211
212     }
213   else
214     {
215       if (!n)
216         return;
217
218       if (p)
219         p->next = n;
220       else
221         procinfo->chain = n;
222
223       n->prev = p;
224       n->next = plugin;
225
226       plugin->prev = n;
227       plugin->next = nn;
228
229       if (nn)
230         nn->prev = plugin;
231       else
232         procinfo->chain_end = plugin;
233     }
234   
235   if (procinfo->jack_client && plugin->desc->aux_channels > 0)
236     {
237       plugin_t * other;
238       other = up ? plugin->next : plugin->prev;
239       
240       /* swap around the jack ports */
241       if (other->desc->id == plugin->desc->id)
242         plugin_swap_aux_ports (plugin, other);
243     }
244 }
245
246 /** exchange an existing plugin for a newly created one */
247 plugin_t *
248 process_change_plugin (process_info_t * procinfo,
249                        plugin_t *plugin, plugin_t * new_plugin)
250 {
251   new_plugin->next = plugin->next;
252   new_plugin->prev = plugin->prev;
253
254   if (plugin->prev)
255     plugin->prev->next = new_plugin;
256   else
257     procinfo->chain = new_plugin;
258
259   if (plugin->next)
260     plugin->next->prev = new_plugin;
261   else
262     procinfo->chain_end = new_plugin;
263
264   /* sort out the aux ports */
265   if (procinfo->jack_client && plugin->desc->aux_channels > 0)
266     {
267       plugin_t * other;
268       
269       for (other = plugin->next; other; other = other->next)
270         if (other->desc->id == plugin->desc->id)
271           plugin_swap_aux_ports (plugin, other);
272     }
273
274   return plugin;
275 }
276
277
278 /******************************************
279  ************* non RT stuff ***************
280  ******************************************/
281
282
283 static int
284 plugin_open_plugin (plugin_desc_t * desc,
285                     void ** dl_handle_ptr,
286                     const LADSPA_Descriptor ** descriptor_ptr)
287 {
288   void * dl_handle;
289   const char * dlerr;
290   LADSPA_Descriptor_Function get_descriptor;
291     
292   /* open the object file */
293   dl_handle = dlopen (desc->object_file, RTLD_NOW|RTLD_GLOBAL);
294   if (!dl_handle)
295     {
296       mlt_log_warning( NULL, "%s: error opening shared object file '%s': %s\n",
297                __FUNCTION__, desc->object_file, dlerror());
298       return 1;
299     }
300   
301   
302   /* get the get_descriptor function */
303   dlerror (); /* clear the error report */
304   
305   get_descriptor = (LADSPA_Descriptor_Function)
306     dlsym (dl_handle, "ladspa_descriptor");
307   
308   dlerr = dlerror();
309   if (dlerr)
310     {
311       mlt_log_warning( NULL, "%s: error finding descriptor symbol in object file '%s': %s\n",
312                __FUNCTION__, desc->object_file, dlerr);
313       dlclose (dl_handle);
314       return 1;
315     }
316   
317   *descriptor_ptr = get_descriptor (desc->index);
318   *dl_handle_ptr = dl_handle;
319   
320   return 0;
321 }
322
323 static int
324 plugin_instantiate (const LADSPA_Descriptor * descriptor,
325                     unsigned long plugin_index,
326                     gint copies,
327                     LADSPA_Handle * instances)
328 {
329   gint i;
330   
331   for (i = 0; i < copies; i++)
332     {
333       instances[i] = descriptor->instantiate (descriptor, sample_rate);
334       
335       if (!instances[i])
336         {
337           unsigned long d;
338  
339           for (d = 0; d < i; d++)
340             descriptor->cleanup (instances[d]);
341           
342           return 1;
343         }
344     }
345   
346   return 0;
347 }
348
349 static void
350 plugin_create_aux_ports (plugin_t * plugin, guint copy, jack_rack_t * jack_rack)
351 {
352   plugin_desc_t * desc;
353 //  plugin_slot_t * slot;
354   unsigned long aux_channel = 1;
355   unsigned long plugin_index = 1;
356   unsigned long i;
357   char port_name[64];
358   char * plugin_name;
359   char * ptr;
360 //  GList * list;
361   ladspa_holder_t * holder;
362   
363   desc = plugin->desc;
364   holder = plugin->holders + copy;
365   
366   holder->aux_ports = g_malloc (sizeof (jack_port_t *) * desc->aux_channels);
367   
368   /* make the plugin name jack worthy */
369   ptr = plugin_name = g_strndup (plugin->desc->name, 7);
370   while (*ptr != '\0')
371     {
372       if (*ptr == ' ')
373         *ptr = '_';
374       else
375         *ptr = tolower (*ptr);
376       
377       ptr++;
378     }
379
380 /*      
381   for (list = jack_rack->slots; list; list = g_list_next (list))
382     {
383       slot = (plugin_slot_t *) list->data;
384       
385       if (slot->plugin->desc->id == plugin->desc->id)
386         plugin_index++;
387     }
388 */
389       
390   for (i = 0; i < desc->aux_channels; i++, aux_channel++)
391     {
392       sprintf (port_name, "%s_%ld-%d_%c%ld",
393                plugin_name,
394                plugin_index,
395                copy + 1,
396                desc->aux_are_input ? 'i' : 'o',
397                aux_channel);
398       
399       holder->aux_ports[i] =
400         jack_port_register (jack_rack->procinfo->jack_client,
401                             port_name,
402                             JACK_DEFAULT_AUDIO_TYPE,
403                             desc->aux_are_input ? JackPortIsInput : JackPortIsOutput,
404                             0);
405       
406       if (!holder->aux_ports[i])
407         {
408           mlt_log_panic( NULL, "Could not register jack port '%s'; aborting\n", port_name);
409         }
410     }
411   
412   g_free (plugin_name);
413 }
414
415 static LADSPA_Data unused_control_port_output;
416
417 static void
418 plugin_init_holder (plugin_t * plugin,
419                     guint copy,
420                     LADSPA_Handle instance,
421                     jack_rack_t * jack_rack)
422 {
423   unsigned long i;
424   plugin_desc_t * desc;
425   ladspa_holder_t * holder;
426   
427   desc = plugin->desc;
428   holder = plugin->holders + copy;
429   
430   holder->instance = instance;
431   
432   if (desc->control_port_count > 0)
433     {
434       holder->ui_control_fifos    = g_malloc (sizeof (lff_t) * desc->control_port_count);
435       holder->control_memory = g_malloc (sizeof (LADSPA_Data) * desc->control_port_count);
436     }
437   else
438     {
439       holder->ui_control_fifos  = NULL;
440       holder->control_memory = NULL;
441     }
442   
443   for (i = 0; i < desc->control_port_count; i++)
444     {
445       lff_init (holder->ui_control_fifos + i, CONTROL_FIFO_SIZE, sizeof (LADSPA_Data));
446       holder->control_memory[i] =
447         plugin_desc_get_default_control_value (desc, desc->control_port_indicies[i], sample_rate);        
448       
449       plugin->descriptor->
450         connect_port (instance, desc->control_port_indicies[i], holder->control_memory + i);
451     }
452   
453   for (i = 0; i < desc->port_count; i++)
454     {
455       if (!LADSPA_IS_PORT_CONTROL (desc->port_descriptors[i]))
456         continue;
457       
458       if (LADSPA_IS_PORT_OUTPUT (desc->port_descriptors[i]))
459         plugin->descriptor-> connect_port (instance, i, &unused_control_port_output);
460     }
461   
462   if (jack_rack->procinfo->jack_client && plugin->desc->aux_channels > 0)
463     plugin_create_aux_ports (plugin, copy, jack_rack);
464   
465   if (plugin->descriptor->activate)
466     plugin->descriptor->activate (instance);
467 }
468
469
470 plugin_t *
471 plugin_new (plugin_desc_t * desc, jack_rack_t * jack_rack)
472 {
473   void * dl_handle;
474   const LADSPA_Descriptor * descriptor;
475   LADSPA_Handle * instances;
476   gint copies;
477   unsigned long i;
478   int err;
479   plugin_t * plugin;
480   
481   /* open the plugin */
482   err = plugin_open_plugin (desc, &dl_handle, &descriptor);
483   if (err)
484     return NULL;
485
486   /* create the instances */
487   copies = plugin_desc_get_copies (desc, jack_rack->channels);
488   instances = g_malloc (sizeof (LADSPA_Handle) * copies);
489
490   err = plugin_instantiate (descriptor, desc->index, copies, instances);
491   if (err)
492     {
493       g_free (instances);
494       dlclose (dl_handle);
495       return NULL;
496     }
497   
498
499   plugin = g_malloc (sizeof (plugin_t));
500   
501   plugin->descriptor = descriptor;
502   plugin->dl_handle = dl_handle;
503   plugin->desc = desc;
504   plugin->copies = copies;
505   plugin->enabled = FALSE;
506   plugin->next = NULL;
507   plugin->prev = NULL;
508   plugin->wet_dry_enabled = FALSE;
509   plugin->jack_rack = jack_rack;
510   
511   /* create audio memory and wet/dry stuff */
512   plugin->audio_output_memory   = g_malloc (sizeof (LADSPA_Data *) * jack_rack->channels);
513   plugin->wet_dry_fifos  = g_malloc (sizeof (lff_t) * jack_rack->channels);
514   plugin->wet_dry_values = g_malloc (sizeof (LADSPA_Data) * jack_rack->channels);
515   
516   for (i = 0; i < jack_rack->channels; i++)
517     {
518       plugin->audio_output_memory[i] = g_malloc (sizeof (LADSPA_Data) * buffer_size);
519       lff_init (plugin->wet_dry_fifos + i, CONTROL_FIFO_SIZE, sizeof (LADSPA_Data));
520       plugin->wet_dry_values[i] = 1.0;
521     }
522   
523   /* create holders and fill them out */
524   plugin->holders = g_malloc (sizeof (ladspa_holder_t) * copies);
525   for (i = 0; i < copies; i++)
526     plugin_init_holder (plugin, i, instances[i], jack_rack);
527   
528   return plugin;
529 }
530
531
532 void
533 plugin_destroy (plugin_t * plugin)
534 {
535   unsigned long i, j;
536   int err;
537
538   /* destroy holders */
539   for (i = 0; i < plugin->copies; i++)
540     {
541       if (plugin->descriptor->deactivate)
542         plugin->descriptor->deactivate (plugin->holders[i].instance);
543       
544 /*      if (plugin->descriptor->cleanup)
545         plugin->descriptor->cleanup (plugin->holders[i].instance); */
546       
547       if (plugin->desc->control_port_count > 0)
548         {
549           for (j = 0; j < plugin->desc->control_port_count; j++)
550             {
551               lff_free (plugin->holders[i].ui_control_fifos + j);
552             }
553           g_free (plugin->holders[i].ui_control_fifos);
554           g_free (plugin->holders[i].control_memory);
555         }
556       
557       /* aux ports */
558       if (plugin->jack_rack->procinfo->jack_client && plugin->desc->aux_channels > 0)
559         {
560           for (j = 0; j < plugin->desc->aux_channels; j++)
561             {
562               err = jack_port_unregister (plugin->jack_rack->procinfo->jack_client,
563                                           plugin->holders[i].aux_ports[j]);
564           
565               if (err)
566                 mlt_log_warning( NULL, "%s: could not unregister jack port\n", __FUNCTION__);
567             }
568        
569           g_free (plugin->holders[i].aux_ports);
570         }
571     }
572     
573   g_free (plugin->holders);
574   
575   for (i = 0; i < plugin->jack_rack->channels; i++)
576     {
577       g_free (plugin->audio_output_memory[i]);
578       lff_free (plugin->wet_dry_fifos + i);
579     }
580     
581   g_free (plugin->audio_output_memory);
582   g_free (plugin->wet_dry_fifos);
583   g_free (plugin->wet_dry_values);
584   
585   err = dlclose (plugin->dl_handle);
586   if (err)
587     {
588       mlt_log_warning( NULL, "%s: error closing shared object '%s': %s\n",
589                __FUNCTION__, plugin->desc->object_file, dlerror ());
590     }
591    
592   g_free (plugin);
593 }
594
595
596 /* EOF */