]> git.sesse.net Git - mlt/blob - src/modules/core/filter_channelcopy.c
refactor dissolve_yuv() to use composite_line_yuv()
[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_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_s32le:
90                 case mlt_audio_f32le:
91                 {
92                         int32_t *f = (int32_t*) *buffer + from;
93                         int32_t *t = (int32_t*) *buffer + to;
94                         int32_t x;
95                         int i;
96
97                         if ( swap )
98                                 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
99                                 {
100                                         x = *t;
101                                         *t = *f;
102                                         *f = x;
103                                 }
104                         else
105                                 for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
106                                         *t = *f;
107                         break;
108                 }
109                 case mlt_audio_float:
110                 {
111                         float *f = (float*) *buffer + from * *samples;
112                         float *t = (float*) *buffer + to * *samples;
113
114                         if ( swap )
115                         {
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) );
120                                 free( x );
121                         }
122                         else
123                         {
124                                 memcpy( t, f, *samples * sizeof(float) );
125                         }
126                         break;
127                 }
128                 default:
129                         mlt_log_error( MLT_FILTER_SERVICE( filter ), "Invalid audio format\n" );
130                         break;
131         }
132
133         return 0;
134 }
135
136 /** Filter processing.
137 */
138
139 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
140 {
141         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
142         mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
143
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" ) );
148
149         // Override the get_audio method
150         mlt_frame_push_audio( frame, this );
151         mlt_frame_push_audio( frame, filter_get_audio );
152
153         return frame;
154 }
155
156 /** Constructor for the filter.
157 */
158
159 mlt_filter filter_channelcopy_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
160 {
161         mlt_filter this = mlt_filter_new( );
162         if ( this != NULL )
163         {
164                 this->process = filter_process;
165                 if ( arg != NULL )
166                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", atoi( arg ) );
167                 else
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 );
171         }
172         return this;
173 }