Merge ../mlt
[melted] / 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
24 #include <stdio.h>
25 #include <stdlib.h>
26 #define __USE_ISOC99 1
27 #include <math.h>
28
29 /** Get the audio.
30 */
31
32 static int filter_get_audio( mlt_frame frame, int16_t **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 int i, j;
37 int from = mlt_properties_get_int( properties, "channelcopy.from" );
38 int to = mlt_properties_get_int( properties, "channelcopy.to" );
39
40 // Get the producer's audio
41 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
42
43 // Duplicate channels as necessary
44 {
45 int size = *channels * *samples * 2;
46 int16_t *new_buffer = mlt_pool_alloc( size );
47
48 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
49
50 // Duplicate the existing channels
51 for ( i = 0; i < *samples; i++ )
52 {
53 for ( j = 0; j < *channels; j++ )
54 {
55 new_buffer[ ( i * *channels ) + j ] = (*buffer)[ ( i * *channels ) + ( j == to ? from : j ) ];
56 }
57 }
58 *buffer = new_buffer;
59 }
60 return 0;
61 }
62
63 /** Filter processing.
64 */
65
66 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
67 {
68 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
69 mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
70
71 // Propogate the parameters
72 mlt_properties_set_int( frame_props, "channelcopy.to", mlt_properties_get_int( properties, "to" ) );
73 mlt_properties_set_int( frame_props, "channelcopy.from", mlt_properties_get_int( properties, "from" ) );
74
75 // Override the get_audio method
76 mlt_frame_push_audio( frame, filter_get_audio );
77
78 return frame;
79 }
80
81 /** Constructor for the filter.
82 */
83
84 mlt_filter filter_channelcopy_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
85 {
86 mlt_filter this = mlt_filter_new( );
87 if ( this != NULL )
88 {
89 this->process = filter_process;
90 if ( arg != NULL )
91 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", atoi( arg ) );
92 else
93 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "to", 1 );
94 }
95 return this;
96 }