]> git.sesse.net Git - mlt/blob - src/modules/jackrack/filter_ladspa.c
Fix crash at end of some files with audio_index=all.
[mlt] / src / modules / jackrack / filter_ladspa.c
1 /*
2  * filter_ladspa.c -- filter audio through LADSPA plugins
3  * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
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 Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <pthread.h>
30 #include <string.h>
31
32 #include "jack_rack.h"
33
34 #define BUFFER_LEN 10000
35
36 static jack_rack_t* initialise_jack_rack( mlt_properties properties, int channels )
37 {
38         jack_rack_t *jackrack = NULL;
39         char *resource = mlt_properties_get( properties, "resource" );
40         if ( !resource && mlt_properties_get( properties, "src" ) )
41                 resource = mlt_properties_get( properties, "src" );
42
43         // Propogate these for the Jack processing callback
44         mlt_properties_set_int( properties, "channels", channels );
45
46         // Start JackRack
47         if ( resource )
48         {
49                 // Create JackRack without Jack client name so that it only uses LADSPA
50                 jackrack = jack_rack_new( NULL, channels );
51                 mlt_properties_set_data( properties, "jackrack", jackrack, 0,
52                         (mlt_destructor) jack_rack_destroy, NULL );
53                 jack_rack_open_file( jackrack, resource );
54         }
55         return jackrack;
56 }
57
58
59 /** Get the audio.
60 */
61
62 static int ladspa_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
63 {
64         // Get the filter service
65         mlt_filter filter = mlt_frame_pop_audio( frame );
66
67         // Get the filter properties
68         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
69
70         // Get the producer's audio
71         *format = mlt_audio_float;
72         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
73         
74         // Initialise LADSPA if needed
75         jack_rack_t *jackrack = mlt_properties_get_data( filter_properties, "jackrack", NULL );
76         if ( jackrack == NULL )
77         {
78                 sample_rate = *frequency; // global inside jack_rack
79                 jackrack = initialise_jack_rack( filter_properties, *channels );
80         }
81                 
82         // Get the filter-specific properties
83         LADSPA_Data **input_buffers  = mlt_pool_alloc( sizeof( LADSPA_Data* ) * *channels );
84         LADSPA_Data **output_buffers = mlt_pool_alloc( sizeof( LADSPA_Data* ) * *channels );
85
86         int i;
87         for ( i = 0; i < *channels; i++ )
88         {
89                 input_buffers[i]  = (LADSPA_Data*) *buffer + i * *samples;
90                 output_buffers[i] = (LADSPA_Data*) *buffer + i * *samples;
91         }
92
93         // Do LADSPA processing
94         int error = jackrack && process_ladspa( jackrack->procinfo, *samples, input_buffers, output_buffers );
95
96         mlt_pool_release( input_buffers );
97         mlt_pool_release( output_buffers );
98
99         return error;
100 }
101
102
103 /** Filter processing.
104 */
105
106 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
107 {
108         if ( mlt_frame_is_test_audio( frame ) == 0 )
109         {
110                 mlt_frame_push_audio( frame, this );
111                 mlt_frame_push_audio( frame, ladspa_get_audio );
112         }
113
114         return frame;
115 }
116
117 /** Constructor for the filter.
118 */
119
120 mlt_filter filter_ladspa_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
121 {
122         mlt_filter this = mlt_filter_new( );
123         if ( this != NULL )
124         {
125                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
126                 this->process = filter_process;
127                 mlt_properties_set( properties, "resource", arg );
128         }
129         return this;
130 }