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 int16_t *f = (int16_t*) *buffer + from;
54 int16_t *t = (int16_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 int32_t *f = (int32_t*) *buffer + from * *samples;
73 int32_t *t = (int32_t*) *buffer + to * *samples;
77 int32_t *x = malloc( *samples * sizeof(int32_t) );
78 memcpy( x, t, *samples * sizeof(int32_t) );
79 memcpy( t, f, *samples * sizeof(int32_t) );
80 memcpy( f, x, *samples * sizeof(int32_t) );
85 memcpy( t, f, *samples * sizeof(int32_t) );
92 int32_t *f = (int32_t*) *buffer + from;
93 int32_t *t = (int32_t*) *buffer + to;
98 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
105 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
109 case mlt_audio_float:
111 float *f = (float*) *buffer + from * *samples;
112 float *t = (float*) *buffer + to * *samples;
116 float *x = malloc( *samples * sizeof(float) );
117 memcpy( x, t, *samples * sizeof(float) );
118 memcpy( t, f, *samples * sizeof(float) );
119 memcpy( f, x, *samples * sizeof(float) );
124 memcpy( t, f, *samples * sizeof(float) );
129 mlt_log_error( MLT_FILTER_SERVICE( filter ), "Invalid audio format\n" );
136 /** Filter processing.
139 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
141 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
142 mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
144 // Propogate the parameters
145 mlt_properties_set_int( frame_props, "channelcopy.to", mlt_properties_get_int( properties, "to" ) );
146 mlt_properties_set_int( frame_props, "channelcopy.from", mlt_properties_get_int( properties, "from" ) );
147 mlt_properties_set_int( frame_props, "channelcopy.swap", mlt_properties_get_int( properties, "swap" ) );
149 // Override the get_audio method
150 mlt_frame_push_audio( frame, this );
151 mlt_frame_push_audio( frame, filter_get_audio );
156 /** Constructor for the filter.
159 mlt_filter filter_channelcopy_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
161 mlt_filter this = mlt_filter_new( );
164 this->process = filter_process;
166 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", atoi( arg ) );
168 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", 1 );
169 if ( strcmp(id, "channelswap") == 0 )
170 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "swap", 1 );