]> git.sesse.net Git - mlt/blob - src/modules/core/filter_channelcopy.c
A little debugging.
[mlt] / src / modules / core / filter_channelcopy.c
1 /*
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>
5  *
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.
10  *
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.
15  *
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
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 <string.h>
28
29 /** Get the audio.
30 */
31
32 static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
33 {
34         // Get the properties of the a frame
35         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
36
37         // Get the filter service
38         mlt_filter filter = mlt_frame_pop_audio( frame );
39
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" );
43
44         // Get the producer's audio
45         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
46
47         // Copy channels as necessary
48         if ( from != to)
49         switch ( *format )
50         {
51                 case mlt_audio_u8:
52                 {
53                         uint8_t *f = (uint8_t*) *buffer + from;
54                         uint8_t *t = (uint8_t*) *buffer + to;
55                         uint8_t x;
56                         int i;
57
58                         if ( swap )
59                                 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
60                                 {
61                                         x = *t;
62                                         *t = *f;
63                                         *f = x;
64                                 }
65                         else
66                                 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
67                                         *t = *f;
68                         break;
69                 }
70                 case mlt_audio_s16:
71                 {
72                         int16_t *f = (int16_t*) *buffer + from;
73                         int16_t *t = (int16_t*) *buffer + to;
74                         int16_t x;
75                         int i;
76
77                         if ( swap )
78                                 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
79                                 {
80                                         x = *t;
81                                         *t = *f;
82                                         *f = x;
83                                 }
84                         else
85                                 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
86                                         *t = *f;
87                         break;
88                 }
89                 case mlt_audio_s32:
90                 {
91                         int32_t *f = (int32_t*) *buffer + from * *samples;
92                         int32_t *t = (int32_t*) *buffer + to * *samples;
93
94                         if ( swap )
95                         {
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) );
100                                 free( x );
101                         }
102                         else
103                         {
104                                 memcpy( t, f, *samples * sizeof(int32_t) );
105                         }
106                         break;
107                 }
108                 case mlt_audio_s32le:
109                 case mlt_audio_f32le:
110                 {
111                         int32_t *f = (int32_t*) *buffer + from;
112                         int32_t *t = (int32_t*) *buffer + to;
113                         int32_t x;
114                         int i;
115
116                         if ( swap )
117                                 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
118                                 {
119                                         x = *t;
120                                         *t = *f;
121                                         *f = x;
122                                 }
123                         else
124                                 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
125                                         *t = *f;
126                         break;
127                 }
128                 case mlt_audio_float:
129                 {
130                         float *f = (float*) *buffer + from * *samples;
131                         float *t = (float*) *buffer + to * *samples;
132
133                         if ( swap )
134                         {
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) );
139                                 free( x );
140                         }
141                         else
142                         {
143                                 memcpy( t, f, *samples * sizeof(float) );
144                         }
145                         break;
146                 }
147                 default:
148                         mlt_log_error( MLT_FILTER_SERVICE( filter ), "Invalid audio format\n" );
149                         break;
150         }
151
152         return 0;
153 }
154
155 /** Filter processing.
156 */
157
158 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
159 {
160         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
161         mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
162
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" ) );
167
168         // Override the get_audio method
169         mlt_frame_push_audio( frame, this );
170         mlt_frame_push_audio( frame, filter_get_audio );
171
172         return frame;
173 }
174
175 /** Constructor for the filter.
176 */
177
178 mlt_filter filter_channelcopy_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
179 {
180         mlt_filter this = mlt_filter_new( );
181         if ( this != NULL )
182         {
183                 this->process = filter_process;
184                 if ( arg != NULL )
185                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", atoi( arg ) );
186                 else
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 );
190         }
191         return this;
192 }