]> git.sesse.net Git - mlt/blob - src/modules/resample/filter_resample.c
Merge branch 'frei0r-metadata'
[mlt] / src / modules / resample / filter_resample.c
1 /*
2  * filter_resample.c -- adjust audio sample frequency
3  * Copyright (C) 2003-2004 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 #include <framework/mlt_log.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <samplerate.h>
28 #include <string.h>
29
30 // BUFFER_LEN is based on a maximum of 96KHz, 5 fps, 8 channels
31 // TODO: dynamically allocate larger buffer size
32 #define BUFFER_LEN ((96000/5) * 8 * sizeof(float))
33 #define RESAMPLE_TYPE SRC_SINC_FASTEST
34
35 /** Get the audio.
36 */
37
38 static int resample_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
39 {
40         // Get the filter service
41         mlt_filter filter = mlt_frame_pop_audio( frame );
42
43         // Get the filter properties
44         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
45
46         // Get the resample information
47         int output_rate = mlt_properties_get_int( filter_properties, "frequency" );
48
49         // If no resample frequency is specified, default to requested value
50         if ( output_rate == 0 )
51                 output_rate = *frequency;
52
53         // Get the producer's audio
54         int error = mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
55         if ( error ) return error;
56
57         // Return now if no work to do
58         if ( output_rate != *frequency && *frequency > 0 && *channels > 0 )
59         {
60                 mlt_log_debug( MLT_FILTER_SERVICE(filter), "channels %d samples %d frequency %d -> %d\n",
61                         *channels, *samples, *frequency, output_rate );
62
63                 // Do not convert to float unless we need to change the rate
64                 if ( *format != mlt_audio_f32le )
65                         frame->convert_audio( frame, buffer, format, mlt_audio_f32le );
66
67                 mlt_service_lock( MLT_FILTER_SERVICE(filter) );
68
69                 SRC_DATA data;
70                 data.data_in = *buffer;
71                 data.data_out = mlt_properties_get_data( filter_properties, "output_buffer", NULL );
72                 data.src_ratio = ( float ) output_rate / ( float ) *frequency;
73                 data.input_frames = *samples;
74                 data.output_frames = BUFFER_LEN / *channels;
75                 data.end_of_input = 0;
76
77                 SRC_STATE *state = mlt_properties_get_data( filter_properties, "state", NULL );
78                 if ( !state || mlt_properties_get_int( filter_properties, "channels" ) != *channels )
79                 {
80                         // Recreate the resampler if the number of channels changed
81                         state = src_new( RESAMPLE_TYPE, *channels, &error );
82                         mlt_properties_set_data( filter_properties, "state", state, 0, (mlt_destructor) src_delete, NULL );
83                         mlt_properties_set_int( filter_properties, "channels", *channels );
84                 }
85
86                 // Resample the audio
87                 error = src_process( state, &data );
88                 if ( !error )
89                 {
90                         // Update output variables
91                         *samples = data.output_frames_gen;
92                         *frequency = output_rate;
93                         *buffer = data.data_out;
94                 }
95                 else
96                 {
97                         mlt_log_error( MLT_FILTER_SERVICE( filter ), "%s %d,%d,%d\n", src_strerror( error ), *frequency, *samples, output_rate );
98                 }
99                 mlt_service_unlock( MLT_FILTER_SERVICE(filter) );
100         }
101
102         return error;
103 }
104
105 /** Filter processing.
106 */
107
108 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
109 {
110         if ( mlt_frame_is_test_audio( frame ) == 0 )
111         {
112                 mlt_frame_push_audio( frame, this );
113                 mlt_frame_push_audio( frame, resample_get_audio );
114         }
115
116         return frame;
117 }
118
119 /** Constructor for the filter.
120 */
121
122 mlt_filter filter_resample_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
123 {
124         mlt_filter this = mlt_filter_new( );
125         if ( this != NULL )
126         {
127                 int error;
128                 SRC_STATE *state = src_new( RESAMPLE_TYPE, 2 /* channels */, &error );
129                 if ( error == 0 )
130                 {
131                         void *output_buffer = mlt_pool_alloc( BUFFER_LEN );
132                         this->process = filter_process;
133                         if ( arg != NULL )
134                                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "frequency", atoi( arg ) );
135                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "channels", 2 );
136                         mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), "state", state, 0, (mlt_destructor)src_delete, NULL );
137                         mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), "output_buffer", output_buffer, BUFFER_LEN, mlt_pool_release, NULL );
138                 }
139                 else
140                 {
141                         fprintf( stderr, "filter_resample_init: %s\n", src_strerror( error ) );
142                 }
143         }
144         return this;
145 }