]> git.sesse.net Git - mlt/blob - src/modules/core/filter_audiochannels.c
Fix uninitialized var in audiochannels filter.
[mlt] / src / modules / core / filter_audiochannels.c
1 /*
2  * filter_audiochannels.c -- convert from one audio format to another
3  * Copyright (C) 2009 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 <string.h>
26
27 static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
28 {
29         // Used to return number of channels in the source
30         int channels_avail = *channels;
31
32         // Get the producer's audio
33         int error = mlt_frame_get_audio( frame, buffer, format, frequency, &channels_avail, samples );
34         if ( error ) return error;
35
36         if ( channels_avail < *channels )
37         {
38                 int size = mlt_audio_format_size( *format, *samples, *channels );
39                 int16_t *new_buffer = mlt_pool_alloc( size );
40
41                 // Duplicate the existing channels
42                 if ( *format == mlt_audio_s16 )
43                 {
44                         int i, j, k = 0;
45                         for ( i = 0; i < *samples; i++ )
46                         {
47                                 for ( j = 0; j < *channels; j++ )
48                                 {
49                                         new_buffer[ ( i * *channels ) + j ]     = ((int16_t*)(*buffer))[ ( i * channels_avail ) + k ];
50                                         k = ( k + 1 ) % channels_avail;
51                                 }
52                         }
53                 }
54                 else
55                 {
56                         // non-interleaved
57                         int size_avail = mlt_audio_format_size( *format, *samples, channels_avail );
58                         int32_t *p = (int32_t*) new_buffer;
59                         int i = *channels / channels_avail;
60                         while ( i-- )
61                         {
62                                 memcpy( p, *buffer, size_avail );
63                                 p += size_avail / sizeof(*p);
64                         }
65                         i = *channels % channels_avail;
66                         if ( i )
67                         {
68                                 size_avail = mlt_audio_format_size( *format, *samples, i );
69                                 memcpy( p, *buffer, size_avail );
70                         }
71                 }
72                 // Update the audio buffer now - destroys the old
73                 mlt_frame_set_audio( frame, new_buffer, *format, size, mlt_pool_release );
74                 *buffer = new_buffer;
75         }
76         else if ( channels_avail > *channels )
77         {
78                 int size = mlt_audio_format_size( *format, *samples, *channels );
79                 int16_t *new_buffer = mlt_pool_alloc( size );
80
81                 // Drop all but the first *channels
82                 if ( *format == mlt_audio_s16 )
83                 {
84                         int i, j;
85                         for ( i = 0; i < *samples; i++ )
86                                 for ( j = 0; j < *channels; j++ )
87                                         new_buffer[ ( i * *channels ) + j ]     = ((int16_t*)(*buffer))[ ( i * channels_avail ) + j ];
88                 }
89                 else
90                 {
91                         // non-interleaved
92                         memcpy( new_buffer, *buffer, size );
93                 }
94                 // Update the audio buffer now - destroys the old
95                 mlt_frame_set_audio( frame, new_buffer, *format, size, mlt_pool_release );
96                 *buffer = new_buffer;
97         }
98         return error;
99 }
100
101 /** Filter processing.
102 */
103
104 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
105 {
106         mlt_frame_push_audio( frame, filter_get_audio );
107         return frame;
108 }
109
110 /** Constructor for the filter.
111 */
112
113 mlt_filter filter_audiochannels_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
114 {
115         mlt_filter filter = mlt_filter_new();
116         if ( filter )
117                 filter->process = filter_process;
118         return filter;
119 }