]> git.sesse.net Git - mlt/blob - src/modules/core/filter_channelcopy.c
Fix composite_copy_region on locale using comma for decimal.
[mlt] / src / modules / core / filter_channelcopy.c
1 /*
2  * filter_channelcopy.c -- copy one audio channel to another
3  * Copyright (C) 2003-2004 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_s16:
52                 {
53                         int16_t *f = (int16_t*) *buffer + from;
54                         int16_t *t = (int16_t*) *buffer + to;
55                         int16_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_s32:
71                 {
72                         int32_t *f = (int32_t*) *buffer + from * *samples;
73                         int32_t *t = (int32_t*) *buffer + to * *samples;
74
75                         if ( swap )
76                         {
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) );
81                                 free( x );
82                         }
83                         else
84                         {
85                                 memcpy( t, f, *samples * sizeof(int32_t) );
86                         }
87                         break;
88                 }
89                 case mlt_audio_float:
90                 {
91                         float *f = (float*) *buffer + from * *samples;
92                         float *t = (float*) *buffer + to * *samples;
93
94                         if ( swap )
95                         {
96                                 float *x = malloc( *samples * sizeof(float) );
97                                 memcpy( x, t, *samples * sizeof(float) );
98                                 memcpy( t, f, *samples * sizeof(float) );
99                                 memcpy( f, x, *samples * sizeof(float) );
100                                 free( x );
101                         }
102                         else
103                         {
104                                 memcpy( t, f, *samples * sizeof(float) );
105                         }
106                         break;
107                 }
108                 default:
109                         mlt_log_error( MLT_FILTER_SERVICE( filter ), "Invalid audio format\n" );
110                         break;
111         }
112
113         return 0;
114 }
115
116 /** Filter processing.
117 */
118
119 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
120 {
121         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
122         mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
123
124         // Propogate the parameters
125         mlt_properties_set_int( frame_props, "channelcopy.to", mlt_properties_get_int( properties, "to" ) );
126         mlt_properties_set_int( frame_props, "channelcopy.from", mlt_properties_get_int( properties, "from" ) );
127         mlt_properties_set_int( frame_props, "channelcopy.swap", mlt_properties_get_int( properties, "swap" ) );
128
129         // Override the get_audio method
130         mlt_frame_push_audio( frame, this );
131         mlt_frame_push_audio( frame, filter_get_audio );
132
133         return frame;
134 }
135
136 /** Constructor for the filter.
137 */
138
139 mlt_filter filter_channelcopy_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
140 {
141         mlt_filter this = mlt_filter_new( );
142         if ( this != NULL )
143         {
144                 this->process = filter_process;
145                 if ( arg != NULL )
146                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", atoi( arg ) );
147                 else
148                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", 1 );
149                 if ( strcmp(id, "channelswap") == 0 )
150                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "swap", 1 );
151         }
152         return this;
153 }