2 * filter_channelcopy.c -- copy one audio channel to another
3 * Copyright (C) 2003-2012 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_log.h>
32 static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
34 // Get the properties of the a frame
35 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
37 // Get the filter service
38 mlt_filter filter = mlt_frame_pop_audio( frame );
40 int from = mlt_properties_get_int( properties, "channelcopy.from" );
41 int to = mlt_properties_get_int( properties, "channelcopy.to" );
42 int swap = mlt_properties_get_int( properties, "channelcopy.swap" );
44 // Get the producer's audio
45 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
47 // Copy channels as necessary
53 uint8_t *f = (uint8_t*) *buffer + from;
54 uint8_t *t = (uint8_t*) *buffer + to;
59 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
66 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
72 int16_t *f = (int16_t*) *buffer + from;
73 int16_t *t = (int16_t*) *buffer + to;
78 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
85 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
91 int32_t *f = (int32_t*) *buffer + from * *samples;
92 int32_t *t = (int32_t*) *buffer + to * *samples;
96 int32_t *x = malloc( *samples * sizeof(int32_t) );
97 memcpy( x, t, *samples * sizeof(int32_t) );
98 memcpy( t, f, *samples * sizeof(int32_t) );
99 memcpy( f, x, *samples * sizeof(int32_t) );
104 memcpy( t, f, *samples * sizeof(int32_t) );
108 case mlt_audio_s32le:
109 case mlt_audio_f32le:
111 int32_t *f = (int32_t*) *buffer + from;
112 int32_t *t = (int32_t*) *buffer + to;
117 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
124 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
128 case mlt_audio_float:
130 float *f = (float*) *buffer + from * *samples;
131 float *t = (float*) *buffer + to * *samples;
135 float *x = malloc( *samples * sizeof(float) );
136 memcpy( x, t, *samples * sizeof(float) );
137 memcpy( t, f, *samples * sizeof(float) );
138 memcpy( f, x, *samples * sizeof(float) );
143 memcpy( t, f, *samples * sizeof(float) );
148 mlt_log_error( MLT_FILTER_SERVICE( filter ), "Invalid audio format\n" );
155 /** Filter processing.
158 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
160 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
161 mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
163 // Propogate the parameters
164 mlt_properties_set_int( frame_props, "channelcopy.to", mlt_properties_get_int( properties, "to" ) );
165 mlt_properties_set_int( frame_props, "channelcopy.from", mlt_properties_get_int( properties, "from" ) );
166 mlt_properties_set_int( frame_props, "channelcopy.swap", mlt_properties_get_int( properties, "swap" ) );
168 // Override the get_audio method
169 mlt_frame_push_audio( frame, this );
170 mlt_frame_push_audio( frame, filter_get_audio );
175 /** Constructor for the filter.
178 mlt_filter filter_channelcopy_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
180 mlt_filter this = mlt_filter_new( );
183 this->process = filter_process;
185 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", atoi( arg ) );
187 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", 1 );
188 if ( strcmp(id, "channelswap") == 0 )
189 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "swap", 1 );