]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/simple.c
Simple: fix memleak on quit
[vlc] / modules / audio_filter / channel_mixer / simple.c
1 /*****************************************************************************
2  * simple.c : simple channel mixer plug-in
3  *****************************************************************************
4  * Copyright (C) 2002, 2004, 2006-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_aout.h>
34 #include <vlc_filter.h>
35 #include <vlc_block.h>
36 #include <assert.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  OpenFilter( vlc_object_t * );
42 static void CloseFilter( vlc_object_t * );
43
44 vlc_module_begin ()
45     set_description( N_("Audio filter for simple channel mixing") )
46     set_category( CAT_AUDIO )
47     set_subcategory( SUBCAT_AUDIO_MISC )
48     set_capability( "audio converter", 10 )
49     set_callbacks( OpenFilter, CloseFilter );
50 vlc_module_end ()
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output );
56
57 struct filter_sys_t
58 {
59     void (*pf_dowork)(filter_t *, block_t *, block_t * );
60 };
61
62 static block_t *Filter( filter_t *, block_t * );
63
64 static void DoWork_7_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
65     float *p_dest = (float *)p_out_buf->p_buffer;
66     const float *p_src = (const float *)p_in_buf->p_buffer;
67     for( int i = p_in_buf->i_nb_samples; i--; )
68     {
69         *p_dest++ = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
70         *p_dest++ = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
71
72         p_src += 7;
73
74         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
75     }
76 }
77
78 static void DoWork_5_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
79     float *p_dest = (float *)p_out_buf->p_buffer;
80     const float *p_src = (const float *)p_in_buf->p_buffer;
81     for( int i = p_in_buf->i_nb_samples; i--; )
82     {
83         *p_dest++ = p_src[0] + 0.7071 * (p_src[4] + p_src[2]);
84         *p_dest++ = p_src[1] + 0.7071 * (p_src[4] + p_src[3]);
85
86         p_src += 5;
87
88         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
89     }
90 }
91
92 static void DoWork_4_0_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
93     VLC_UNUSED(p_filter);
94     float *p_dest = (float *)p_out_buf->p_buffer;
95     const float *p_src = (const float *)p_in_buf->p_buffer;
96     for( int i = p_in_buf->i_nb_samples; i--; )
97     {
98         *p_dest++ = p_src[2] + p_src[3] + 0.5 * p_src[0];
99         *p_dest++ = p_src[2] + p_src[3] + 0.5 * p_src[1];
100         p_src += 4;
101     }
102 }
103
104 static void DoWork_3_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
105     float *p_dest = (float *)p_out_buf->p_buffer;
106     const float *p_src = (const float *)p_in_buf->p_buffer;
107     for( int i = p_in_buf->i_nb_samples; i--; )
108     {
109         *p_dest++ = p_src[2] + 0.5 * p_src[0];
110         *p_dest++ = p_src[2] + 0.5 * p_src[1];
111
112         p_src += 3;
113
114         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
115     }
116 }
117
118 static void DoWork_7_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
119     float *p_dest = (float *)p_out_buf->p_buffer;
120     const float *p_src = (const float *)p_in_buf->p_buffer;
121     for( int i = p_in_buf->i_nb_samples; i--; )
122     {
123         *p_dest++ = p_src[6] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 8 + p_src[3] / 8 + p_src[4] / 8 + p_src[5] / 8;
124
125         p_src += 7;
126
127         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
128     }
129 }
130
131 static void DoWork_5_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
132     float *p_dest = (float *)p_out_buf->p_buffer;
133     const float *p_src = (const float *)p_in_buf->p_buffer;
134     for( int i = p_in_buf->i_nb_samples; i--; )
135     {
136         *p_dest++ = 0.7071 * (p_src[0] + p_src[1]) + p_src[4] + 0.5f * (p_src[2] + p_src[3]);
137
138         p_src += 5;
139
140         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
141     }
142 }
143
144 static void DoWork_4_0_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
145     VLC_UNUSED(p_filter);
146     float *p_dest = (float *)p_out_buf->p_buffer;
147     const float *p_src = (const float *)p_in_buf->p_buffer;
148     for( int i = p_in_buf->i_nb_samples; i--; )
149     {
150         *p_dest++ = p_src[2] + p_src[3] + p_src[0] / 4 + p_src[1] / 4;
151         p_src += 4;
152     }
153 }
154
155 static void DoWork_3_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
156     float *p_dest = (float *)p_out_buf->p_buffer;
157     const float *p_src = (const float *)p_in_buf->p_buffer;
158     for( int i = p_in_buf->i_nb_samples; i--; )
159     {
160         *p_dest++ = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
161
162         p_src += 3;
163
164         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
165     }
166 }
167
168 static void DoWork_2_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
169     VLC_UNUSED(p_filter);
170     float *p_dest = (float *)p_out_buf->p_buffer;
171     const float *p_src = (const float *)p_in_buf->p_buffer;
172     for( int i = p_in_buf->i_nb_samples; i--; )
173     {
174         *p_dest++ = p_src[0] / 2 + p_src[1] / 2;
175
176         p_src += 2;
177     }
178 }
179
180 static void DoWork_7_x_to_4_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
181     float *p_dest = (float *)p_out_buf->p_buffer;
182     const float *p_src = (const float *)p_in_buf->p_buffer;
183     for( int i = p_in_buf->i_nb_samples; i--; )
184     {
185         *p_dest++ = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
186         *p_dest++ = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
187         *p_dest++ = p_src[2] / 6 +  p_src[4];
188         *p_dest++ = p_src[3] / 6 +  p_src[5];
189
190         p_src += 7;
191
192         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
193     }
194 }
195
196 static void DoWork_5_x_to_4_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
197     float *p_dest = (float *)p_out_buf->p_buffer;
198     const float *p_src = (const float *)p_in_buf->p_buffer;
199     for( int i = p_in_buf->i_nb_samples; i--; )
200     {
201         *p_dest++ = p_src[0] + p_src[4] * 0.7071;
202         *p_dest++ = p_src[1] + p_src[4] * 0.7071;
203         *p_dest++ = p_src[2];
204         *p_dest++ = p_src[3];
205
206         p_src += 5;
207
208         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
209     }
210 }
211
212 static void DoWork_7_x_to_5_x( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
213     float *p_dest = (float *)p_out_buf->p_buffer;
214     const float *p_src = (const float *)p_in_buf->p_buffer;
215     for( int i = p_in_buf->i_nb_samples; i--; )
216     {
217         *p_dest++ = p_src[0];
218         *p_dest++ = p_src[1];
219         *p_dest++ = (p_src[2] + p_src[4]) * 0.5;
220         *p_dest++ = (p_src[3] + p_src[5]) * 0.5;
221         *p_dest++ = p_src[6];
222
223         p_src += 7;
224
225         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE &&
226             p_filter->fmt_out.audio.i_physical_channels & AOUT_CHAN_LFE )
227             *p_dest++ = *p_src++;
228         else if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
229     }
230 }
231
232
233 /*****************************************************************************
234  * OpenFilter:
235  *****************************************************************************/
236 static int OpenFilter( vlc_object_t *p_this )
237 {
238     filter_t *p_filter = (filter_t *)p_this;
239     filter_sys_t *p_sys;
240
241     audio_format_t fmt_in  = p_filter->fmt_in.audio;
242     audio_format_t fmt_out = p_filter->fmt_out.audio;
243
244     fmt_in.i_format = p_filter->fmt_in.i_codec;
245     fmt_out.i_format = p_filter->fmt_out.i_codec;
246
247     if( !IsSupported( &fmt_in, &fmt_out ) )
248         return VLC_EGENERIC;
249
250     p_filter->p_sys = malloc( sizeof(*p_sys) );
251     if( unlikely(!p_filter->p_sys) )
252         return VLC_ENOMEM;
253
254     p_filter->pf_audio_filter = Filter;
255
256     const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
257     const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
258     const bool b_input_5_0 = !b_input_7_0 &&
259                              ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
260                                (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
261     const bool b_input_4_center_rear =  !b_input_7_0 && !b_input_5_0 &&
262                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
263     const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear &&
264                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;
265
266     if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
267     {
268         if( b_input_7_0 )
269             p_filter->p_sys->pf_dowork = DoWork_7_x_to_2_0;
270         else if( b_input_5_0 )
271             p_filter->p_sys->pf_dowork = DoWork_5_x_to_2_0;
272         else if( b_input_4_center_rear )
273             p_filter->p_sys->pf_dowork = DoWork_4_0_to_2_0;
274         else if( b_input_3_0 )
275             p_filter->p_sys->pf_dowork = DoWork_3_x_to_2_0;
276     }
277     else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
278     {
279         if( b_input_7_0 )
280             p_filter->p_sys->pf_dowork = DoWork_7_x_to_1_0;
281         else if( b_input_5_0 )
282             p_filter->p_sys->pf_dowork = DoWork_5_x_to_1_0;
283         else if( b_input_4_center_rear )
284             p_filter->p_sys->pf_dowork = DoWork_4_0_to_1_0;
285         else if( b_input_3_0 )
286             p_filter->p_sys->pf_dowork = DoWork_3_x_to_1_0;
287         else
288             p_filter->p_sys->pf_dowork = DoWork_2_x_to_1_0;
289     }
290     else if(p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0)
291     {
292         if( b_input_7_0 )
293             p_filter->p_sys->pf_dowork = DoWork_7_x_to_4_0;
294         else
295             p_filter->p_sys->pf_dowork = DoWork_5_x_to_4_0;
296     }
297     else
298     {
299         assert( b_input_7_0 );
300         p_filter->p_sys->pf_dowork = DoWork_7_x_to_5_x;
301     }
302
303     return VLC_SUCCESS;
304 }
305
306 static void CloseFilter( vlc_object_t *p_this )
307 {
308     filter_t *p_filter = (filter_t *) p_this;
309     filter_sys_t *p_sys = p_filter->p_sys;
310     free( p_sys );
311 }
312
313 /*****************************************************************************
314  * Filter:
315  *****************************************************************************/
316 static block_t *Filter( filter_t *p_filter, block_t *p_block )
317 {
318     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
319     if( !p_block || !p_block->i_nb_samples )
320     {
321         if( p_block )
322             block_Release( p_block );
323         return NULL;
324     }
325
326     size_t i_out_size = p_block->i_nb_samples *
327       p_filter->fmt_out.audio.i_bitspersample *
328         p_filter->fmt_out.audio.i_channels / 8;
329
330     block_t *p_out = block_Alloc( i_out_size );
331     if( !p_out )
332     {
333         msg_Warn( p_filter, "can't get output buffer" );
334         block_Release( p_block );
335         return NULL;
336     }
337
338     p_out->i_nb_samples = p_block->i_nb_samples;
339     p_out->i_dts = p_block->i_dts;
340     p_out->i_pts = p_block->i_pts;
341     p_out->i_length = p_block->i_length;
342
343     int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
344     int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
345     p_out->i_nb_samples = p_block->i_nb_samples;
346     p_out->i_buffer = p_block->i_buffer * i_output_nb / i_input_nb;
347
348     p_sys->pf_dowork( p_filter, p_block, p_out );
349
350     block_Release( p_block );
351
352     return p_out;
353 }
354
355 /*****************************************************************************
356  * Helpers:
357  *****************************************************************************/
358 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
359 {
360     if( p_input->i_format != VLC_CODEC_FL32 ||
361           p_input->i_format != p_output->i_format ||
362           p_input->i_rate != p_output->i_rate )
363         return false;
364
365     if( p_input->i_physical_channels == p_output->i_physical_channels &&
366         p_input->i_original_channels == p_output->i_original_channels )
367     {
368         return false;
369     }
370
371     /* Only conversion to Mono, Stereo and 4.0 right now */
372     if( p_output->i_physical_channels != AOUT_CHAN_CENTER &&
373         p_output->i_physical_channels != AOUT_CHANS_2_0 &&
374         p_output->i_physical_channels != AOUT_CHANS_4_0 &&
375         p_output->i_physical_channels != AOUT_CHANS_5_1 )
376     {
377         return false;
378     }
379
380     /* Only from 7/7.1/5/5.1/3/3.1/2.0
381      * XXX 5.X rear and middle are handled the same way */
382     if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
383         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0 &&
384         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0_MIDDLE &&
385         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_3_0 &&
386         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_4_CENTER_REAR &&
387          p_input->i_physical_channels != AOUT_CHANS_2_0 )
388     {
389         return false;
390     }
391
392     /* Only if we downmix */
393     if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )
394         return false;
395
396     return true;
397 }
398